2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-dpif.h"
20 #include "ofproto/ofproto-provider.h"
27 #include "byte-order.h"
28 #include "connectivity.h"
33 #include "dynamic-string.h"
34 #include "fail-open.h"
35 #include "guarded-list.h"
39 #include "mac-learning.h"
40 #include "meta-flow.h"
41 #include "multipath.h"
42 #include "netdev-vport.h"
47 #include "odp-execute.h"
50 #include "ofp-actions.h"
51 #include "ofp-parse.h"
52 #include "ofp-print.h"
53 #include "ofproto-dpif-ipfix.h"
54 #include "ofproto-dpif-mirror.h"
55 #include "ofproto-dpif-monitor.h"
56 #include "ofproto-dpif-sflow.h"
57 #include "ofproto-dpif-upcall.h"
58 #include "ofproto-dpif-xlate.h"
59 #include "poll-loop.h"
65 #include "unaligned.h"
67 #include "vlan-bitmap.h"
70 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
72 COVERAGE_DEFINE(ofproto_dpif_expired);
73 COVERAGE_DEFINE(packet_in_overflow);
75 /* Number of implemented OpenFlow tables. */
76 enum { N_TABLES = 255 };
77 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
78 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
87 * - Do include packets and bytes from datapath flows which have not
88 * recently been processed by a revalidator. */
89 struct ovs_mutex stats_mutex;
90 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
91 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
94 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes);
95 static struct rule_dpif *rule_dpif_cast(const struct rule *);
96 static void rule_expire(struct rule_dpif *);
103 * - Do include packets and bytes from datapath flows which have not
104 * recently been processed by a revalidator. */
105 struct ovs_mutex stats_mutex;
106 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
107 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
108 struct bucket_counter *bucket_stats OVS_GUARDED; /* Bucket statistics. */
112 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
113 struct ofproto_dpif *ofproto; /* Owning ofproto. */
114 void *aux; /* Key supplied by ofproto's client. */
115 char *name; /* Identifier for log messages. */
118 struct list ports; /* Contains "struct ofport"s. */
119 enum port_vlan_mode vlan_mode; /* VLAN mode */
120 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
121 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
122 * NULL if all VLANs are trunked. */
123 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
124 struct bond *bond; /* Nonnull iff more than one port. */
125 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
128 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
131 static void bundle_remove(struct ofport *);
132 static void bundle_update(struct ofbundle *);
133 static void bundle_destroy(struct ofbundle *);
134 static void bundle_del_port(struct ofport_dpif *);
135 static void bundle_run(struct ofbundle *);
136 static void bundle_wait(struct ofbundle *);
138 static void stp_run(struct ofproto_dpif *ofproto);
139 static void stp_wait(struct ofproto_dpif *ofproto);
140 static int set_stp_port(struct ofport *,
141 const struct ofproto_port_stp_settings *);
144 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
148 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
149 struct list bundle_node; /* In struct ofbundle's "ports" list. */
150 struct cfm *cfm; /* Connectivity Fault Management, if any. */
151 struct bfd *bfd; /* BFD, if any. */
152 bool may_enable; /* May be enabled in bonds. */
153 bool is_tunnel; /* This port is a tunnel. */
154 bool is_layer3; /* This is a layer 3 port. */
155 long long int carrier_seq; /* Carrier status changes. */
156 struct ofport_dpif *peer; /* Peer if patch port. */
159 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
160 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
161 long long int stp_state_entered;
163 /* Queue to DSCP mapping. */
164 struct ofproto_port_queue *qdscp;
167 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
169 * This is deprecated. It is only for compatibility with broken device
170 * drivers in old versions of Linux that do not properly support VLANs when
171 * VLAN devices are not used. When broken device drivers are no longer in
172 * widespread use, we will delete these interfaces. */
173 ofp_port_t realdev_ofp_port;
177 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
179 * This is deprecated. It is only for compatibility with broken device drivers
180 * in old versions of Linux that do not properly support VLANs when VLAN
181 * devices are not used. When broken device drivers are no longer in
182 * widespread use, we will delete these interfaces. */
183 struct vlan_splinter {
184 struct hmap_node realdev_vid_node;
185 struct hmap_node vlandev_node;
186 ofp_port_t realdev_ofp_port;
187 ofp_port_t vlandev_ofp_port;
191 static void vsp_remove(struct ofport_dpif *);
192 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
194 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
197 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
200 static struct ofport_dpif *
201 ofport_dpif_cast(const struct ofport *ofport)
203 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
206 static void port_run(struct ofport_dpif *);
207 static int set_bfd(struct ofport *, const struct smap *);
208 static int set_cfm(struct ofport *, const struct cfm_settings *);
209 static void ofport_update_peer(struct ofport_dpif *);
211 struct dpif_completion {
212 struct list list_node;
213 struct ofoperation *op;
216 /* Reasons that we might need to revalidate every datapath flow, and
217 * corresponding coverage counters.
219 * A value of 0 means that there is no need to revalidate.
221 * It would be nice to have some cleaner way to integrate with coverage
222 * counters, but with only a few reasons I guess this is good enough for
224 enum revalidate_reason {
225 REV_RECONFIGURE = 1, /* Switch configuration changed. */
226 REV_STP, /* Spanning tree protocol port status change. */
227 REV_BOND, /* Bonding changed. */
228 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
229 REV_FLOW_TABLE, /* Flow table changed. */
230 REV_MAC_LEARNING, /* Mac learning changed. */
232 COVERAGE_DEFINE(rev_reconfigure);
233 COVERAGE_DEFINE(rev_stp);
234 COVERAGE_DEFINE(rev_bond);
235 COVERAGE_DEFINE(rev_port_toggled);
236 COVERAGE_DEFINE(rev_flow_table);
237 COVERAGE_DEFINE(rev_mac_learning);
239 /* All datapaths of a given type share a single dpif backer instance. */
246 struct ovs_rwlock odp_to_ofport_lock;
247 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
249 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
251 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
253 bool recv_set_enable; /* Enables or disables receiving packets. */
256 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
257 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
259 struct ofproto_dpif {
260 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
262 struct dpif_backer *backer;
264 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
266 /* Special OpenFlow rules. */
267 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
268 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
269 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
272 struct netflow *netflow;
273 struct dpif_sflow *sflow;
274 struct dpif_ipfix *ipfix;
275 struct hmap bundles; /* Contains "struct ofbundle"s. */
276 struct mac_learning *ml;
277 bool has_bonded_bundles;
279 struct mbridge *mbridge;
281 struct ovs_mutex stats_mutex;
282 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
283 * consumed in userspace. */
287 long long int stp_last_tick;
289 /* VLAN splinters. */
290 struct ovs_mutex vsp_mutex;
291 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
292 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
295 struct sset ports; /* Set of standard port names. */
296 struct sset ghost_ports; /* Ports with no datapath port. */
297 struct sset port_poll_set; /* Queued names for port_poll() reply. */
298 int port_poll_errno; /* Last errno for port_poll() reply. */
299 uint64_t change_seq; /* Connectivity status changes. */
302 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
305 /* All existing ofproto_dpif instances, indexed by ->up.name. */
306 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
308 static void ofproto_dpif_unixctl_init(void);
310 static inline struct ofproto_dpif *
311 ofproto_dpif_cast(const struct ofproto *ofproto)
313 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
314 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
317 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
318 ofp_port_t ofp_port);
319 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
320 const struct ofpbuf *packet,
321 const struct ofpact[], size_t ofpacts_len,
324 /* Global variables. */
325 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
327 /* Initial mappings of port to bridge mappings. */
328 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
330 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
333 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
334 struct ofputil_flow_mod *fm)
336 ofproto_flow_mod(&ofproto->up, fm);
339 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
340 * Takes ownership of 'pin' and pin->packet. */
342 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
343 struct ofproto_packet_in *pin)
345 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
346 COVERAGE_INC(packet_in_overflow);
347 free(CONST_CAST(void *, pin->up.packet));
352 /* Factory functions. */
355 init(const struct shash *iface_hints)
357 struct shash_node *node;
359 /* Make a local copy, since we don't own 'iface_hints' elements. */
360 SHASH_FOR_EACH(node, iface_hints) {
361 const struct iface_hint *orig_hint = node->data;
362 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
364 new_hint->br_name = xstrdup(orig_hint->br_name);
365 new_hint->br_type = xstrdup(orig_hint->br_type);
366 new_hint->ofp_port = orig_hint->ofp_port;
368 shash_add(&init_ofp_ports, node->name, new_hint);
373 enumerate_types(struct sset *types)
375 dp_enumerate_types(types);
379 enumerate_names(const char *type, struct sset *names)
381 struct ofproto_dpif *ofproto;
384 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
385 if (strcmp(type, ofproto->up.type)) {
388 sset_add(names, ofproto->up.name);
395 del(const char *type, const char *name)
400 error = dpif_open(name, type, &dpif);
402 error = dpif_delete(dpif);
409 port_open_type(const char *datapath_type, const char *port_type)
411 return dpif_port_open_type(datapath_type, port_type);
414 /* Type functions. */
416 static void process_dpif_port_changes(struct dpif_backer *);
417 static void process_dpif_all_ports_changed(struct dpif_backer *);
418 static void process_dpif_port_change(struct dpif_backer *,
419 const char *devname);
420 static void process_dpif_port_error(struct dpif_backer *, int error);
422 static struct ofproto_dpif *
423 lookup_ofproto_dpif_by_port_name(const char *name)
425 struct ofproto_dpif *ofproto;
427 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
428 if (sset_contains(&ofproto->ports, name)) {
437 type_run(const char *type)
439 struct dpif_backer *backer;
441 backer = shash_find_data(&all_dpif_backers, type);
443 /* This is not necessarily a problem, since backers are only
444 * created on demand. */
448 dpif_run(backer->dpif);
450 /* If vswitchd started with other_config:flow_restore_wait set as "true",
451 * and the configuration has now changed to "false", enable receiving
452 * packets from the datapath. */
453 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
456 backer->recv_set_enable = true;
458 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
460 VLOG_ERR("Failed to enable receiving packets in dpif.");
463 dpif_flow_flush(backer->dpif);
464 backer->need_revalidate = REV_RECONFIGURE;
467 if (backer->recv_set_enable) {
468 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
471 if (backer->need_revalidate) {
472 struct ofproto_dpif *ofproto;
473 struct simap_node *node;
474 struct simap tmp_backers;
476 /* Handle tunnel garbage collection. */
477 simap_init(&tmp_backers);
478 simap_swap(&backer->tnl_backers, &tmp_backers);
480 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
481 struct ofport_dpif *iter;
483 if (backer != ofproto->backer) {
487 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
488 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
491 if (!iter->is_tunnel) {
495 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
496 namebuf, sizeof namebuf);
497 node = simap_find(&tmp_backers, dp_port);
499 simap_put(&backer->tnl_backers, dp_port, node->data);
500 simap_delete(&tmp_backers, node);
501 node = simap_find(&backer->tnl_backers, dp_port);
503 node = simap_find(&backer->tnl_backers, dp_port);
505 odp_port_t odp_port = ODPP_NONE;
507 if (!dpif_port_add(backer->dpif, iter->up.netdev,
509 simap_put(&backer->tnl_backers, dp_port,
510 odp_to_u32(odp_port));
511 node = simap_find(&backer->tnl_backers, dp_port);
516 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
517 if (tnl_port_reconfigure(iter, iter->up.netdev,
519 backer->need_revalidate = REV_RECONFIGURE;
524 SIMAP_FOR_EACH (node, &tmp_backers) {
525 dpif_port_del(backer->dpif, u32_to_odp(node->data));
527 simap_destroy(&tmp_backers);
529 switch (backer->need_revalidate) {
530 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
531 case REV_STP: COVERAGE_INC(rev_stp); break;
532 case REV_BOND: COVERAGE_INC(rev_bond); break;
533 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
534 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
535 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
537 backer->need_revalidate = 0;
539 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
540 struct ofport_dpif *ofport;
541 struct ofbundle *bundle;
543 if (ofproto->backer != backer) {
547 ovs_rwlock_wrlock(&xlate_rwlock);
548 xlate_ofproto_set(ofproto, ofproto->up.name,
549 ofproto->backer->dpif, ofproto->miss_rule,
550 ofproto->no_packet_in_rule, ofproto->ml,
551 ofproto->stp, ofproto->mbridge,
552 ofproto->sflow, ofproto->ipfix,
553 ofproto->netflow, ofproto->up.frag_handling,
554 ofproto->up.forward_bpdu,
555 connmgr_has_in_band(ofproto->up.connmgr));
557 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
558 xlate_bundle_set(ofproto, bundle, bundle->name,
559 bundle->vlan_mode, bundle->vlan,
560 bundle->trunks, bundle->use_priority_tags,
561 bundle->bond, bundle->lacp,
565 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
566 int stp_port = ofport->stp_port
567 ? stp_port_no(ofport->stp_port)
569 xlate_ofport_set(ofproto, ofport->bundle, ofport,
570 ofport->up.ofp_port, ofport->odp_port,
571 ofport->up.netdev, ofport->cfm,
572 ofport->bfd, ofport->peer, stp_port,
573 ofport->qdscp, ofport->n_qdscp,
574 ofport->up.pp.config, ofport->up.pp.state,
575 ofport->is_tunnel, ofport->may_enable);
577 ovs_rwlock_unlock(&xlate_rwlock);
580 udpif_revalidate(backer->udpif);
583 process_dpif_port_changes(backer);
588 /* Check for and handle port changes in 'backer''s dpif. */
590 process_dpif_port_changes(struct dpif_backer *backer)
596 error = dpif_port_poll(backer->dpif, &devname);
602 process_dpif_all_ports_changed(backer);
606 process_dpif_port_change(backer, devname);
611 process_dpif_port_error(backer, error);
618 process_dpif_all_ports_changed(struct dpif_backer *backer)
620 struct ofproto_dpif *ofproto;
621 struct dpif_port dpif_port;
622 struct dpif_port_dump dump;
623 struct sset devnames;
626 sset_init(&devnames);
627 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
628 if (ofproto->backer == backer) {
629 struct ofport *ofport;
631 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
632 sset_add(&devnames, netdev_get_name(ofport->netdev));
636 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
637 sset_add(&devnames, dpif_port.name);
640 SSET_FOR_EACH (devname, &devnames) {
641 process_dpif_port_change(backer, devname);
643 sset_destroy(&devnames);
647 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
649 struct ofproto_dpif *ofproto;
650 struct dpif_port port;
652 /* Don't report on the datapath's device. */
653 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
657 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
658 &all_ofproto_dpifs) {
659 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
664 ofproto = lookup_ofproto_dpif_by_port_name(devname);
665 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
666 /* The port was removed. If we know the datapath,
667 * report it through poll_set(). If we don't, it may be
668 * notifying us of a removal we initiated, so ignore it.
669 * If there's a pending ENOBUFS, let it stand, since
670 * everything will be reevaluated. */
671 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
672 sset_add(&ofproto->port_poll_set, devname);
673 ofproto->port_poll_errno = 0;
675 } else if (!ofproto) {
676 /* The port was added, but we don't know with which
677 * ofproto we should associate it. Delete it. */
678 dpif_port_del(backer->dpif, port.port_no);
680 struct ofport_dpif *ofport;
682 ofport = ofport_dpif_cast(shash_find_data(
683 &ofproto->up.port_by_name, devname));
685 && ofport->odp_port != port.port_no
686 && !odp_port_to_ofport(backer, port.port_no))
688 /* 'ofport''s datapath port number has changed from
689 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
690 * structures to match. */
691 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
692 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
693 ofport->odp_port = port.port_no;
694 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
695 hash_odp_port(port.port_no));
696 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
697 backer->need_revalidate = REV_RECONFIGURE;
700 dpif_port_destroy(&port);
703 /* Propagate 'error' to all ofprotos based on 'backer'. */
705 process_dpif_port_error(struct dpif_backer *backer, int error)
707 struct ofproto_dpif *ofproto;
709 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
710 if (ofproto->backer == backer) {
711 sset_clear(&ofproto->port_poll_set);
712 ofproto->port_poll_errno = error;
718 type_wait(const char *type)
720 struct dpif_backer *backer;
722 backer = shash_find_data(&all_dpif_backers, type);
724 /* This is not necessarily a problem, since backers are only
725 * created on demand. */
729 dpif_wait(backer->dpif);
732 /* Basic life-cycle. */
734 static int add_internal_flows(struct ofproto_dpif *);
736 static struct ofproto *
739 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
744 dealloc(struct ofproto *ofproto_)
746 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
751 close_dpif_backer(struct dpif_backer *backer)
753 ovs_assert(backer->refcount > 0);
755 if (--backer->refcount) {
759 udpif_destroy(backer->udpif);
761 simap_destroy(&backer->tnl_backers);
762 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
763 hmap_destroy(&backer->odp_to_ofport_map);
764 shash_find_and_delete(&all_dpif_backers, backer->type);
766 dpif_close(backer->dpif);
771 /* Datapath port slated for removal from datapath. */
773 struct list list_node;
778 open_dpif_backer(const char *type, struct dpif_backer **backerp)
780 struct dpif_backer *backer;
781 struct dpif_port_dump port_dump;
782 struct dpif_port port;
783 struct shash_node *node;
784 struct list garbage_list;
785 struct odp_garbage *garbage, *next;
791 backer = shash_find_data(&all_dpif_backers, type);
798 backer_name = xasprintf("ovs-%s", type);
800 /* Remove any existing datapaths, since we assume we're the only
801 * userspace controlling the datapath. */
803 dp_enumerate_names(type, &names);
804 SSET_FOR_EACH(name, &names) {
805 struct dpif *old_dpif;
807 /* Don't remove our backer if it exists. */
808 if (!strcmp(name, backer_name)) {
812 if (dpif_open(name, type, &old_dpif)) {
813 VLOG_WARN("couldn't open old datapath %s to remove it", name);
815 dpif_delete(old_dpif);
816 dpif_close(old_dpif);
819 sset_destroy(&names);
821 backer = xmalloc(sizeof *backer);
823 error = dpif_create_and_open(backer_name, type, &backer->dpif);
826 VLOG_ERR("failed to open datapath of type %s: %s", type,
827 ovs_strerror(error));
831 backer->udpif = udpif_create(backer, backer->dpif);
833 backer->type = xstrdup(type);
834 backer->refcount = 1;
835 hmap_init(&backer->odp_to_ofport_map);
836 ovs_rwlock_init(&backer->odp_to_ofport_lock);
837 backer->need_revalidate = 0;
838 simap_init(&backer->tnl_backers);
839 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
842 if (backer->recv_set_enable) {
843 dpif_flow_flush(backer->dpif);
846 /* Loop through the ports already on the datapath and remove any
847 * that we don't need anymore. */
848 list_init(&garbage_list);
849 dpif_port_dump_start(&port_dump, backer->dpif);
850 while (dpif_port_dump_next(&port_dump, &port)) {
851 node = shash_find(&init_ofp_ports, port.name);
852 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
853 garbage = xmalloc(sizeof *garbage);
854 garbage->odp_port = port.port_no;
855 list_push_front(&garbage_list, &garbage->list_node);
858 dpif_port_dump_done(&port_dump);
860 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
861 dpif_port_del(backer->dpif, garbage->odp_port);
862 list_remove(&garbage->list_node);
866 shash_add(&all_dpif_backers, type, backer);
868 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
870 VLOG_ERR("failed to listen on datapath of type %s: %s",
871 type, ovs_strerror(error));
872 close_dpif_backer(backer);
876 if (backer->recv_set_enable) {
877 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
884 construct(struct ofproto *ofproto_)
886 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
887 struct shash_node *node, *next;
890 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
895 ofproto->netflow = NULL;
896 ofproto->sflow = NULL;
897 ofproto->ipfix = NULL;
899 ofproto->dump_seq = 0;
900 hmap_init(&ofproto->bundles);
901 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
902 ofproto->mbridge = mbridge_create();
903 ofproto->has_bonded_bundles = false;
904 ofproto->lacp_enabled = false;
905 ovs_mutex_init(&ofproto->stats_mutex);
906 ovs_mutex_init(&ofproto->vsp_mutex);
908 guarded_list_init(&ofproto->pins);
910 ofproto_dpif_unixctl_init();
912 hmap_init(&ofproto->vlandev_map);
913 hmap_init(&ofproto->realdev_vid_map);
915 sset_init(&ofproto->ports);
916 sset_init(&ofproto->ghost_ports);
917 sset_init(&ofproto->port_poll_set);
918 ofproto->port_poll_errno = 0;
919 ofproto->change_seq = 0;
921 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
922 struct iface_hint *iface_hint = node->data;
924 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
925 /* Check if the datapath already has this port. */
926 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
927 sset_add(&ofproto->ports, node->name);
930 free(iface_hint->br_name);
931 free(iface_hint->br_type);
933 shash_delete(&init_ofp_ports, node);
937 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
938 hash_string(ofproto->up.name, 0));
939 memset(&ofproto->stats, 0, sizeof ofproto->stats);
941 ofproto_init_tables(ofproto_, N_TABLES);
942 error = add_internal_flows(ofproto);
943 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
949 add_internal_flow(struct ofproto_dpif *ofproto, int id,
950 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
952 struct ofputil_flow_mod fm;
955 match_init_catchall(&fm.match);
957 match_set_reg(&fm.match, 0, id);
958 fm.new_cookie = htonll(0);
959 fm.cookie = htonll(0);
960 fm.cookie_mask = htonll(0);
961 fm.modify_cookie = false;
962 fm.table_id = TBL_INTERNAL;
963 fm.command = OFPFC_ADD;
969 fm.ofpacts = ofpacts->data;
970 fm.ofpacts_len = ofpacts->size;
972 error = ofproto_flow_mod(&ofproto->up, &fm);
974 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
975 id, ofperr_to_string(error));
979 if (rule_dpif_lookup_in_table(ofproto, &fm.match.flow, NULL, TBL_INTERNAL,
981 rule_dpif_unref(*rulep);
990 add_internal_flows(struct ofproto_dpif *ofproto)
992 struct ofpact_controller *controller;
993 uint64_t ofpacts_stub[128 / 8];
994 struct ofpbuf ofpacts;
998 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1001 controller = ofpact_put_CONTROLLER(&ofpacts);
1002 controller->max_len = UINT16_MAX;
1003 controller->controller_id = 0;
1004 controller->reason = OFPR_NO_MATCH;
1005 ofpact_pad(&ofpacts);
1007 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1012 ofpbuf_clear(&ofpacts);
1013 error = add_internal_flow(ofproto, id++, &ofpacts,
1014 &ofproto->no_packet_in_rule);
1019 error = add_internal_flow(ofproto, id++, &ofpacts,
1020 &ofproto->drop_frags_rule);
1025 destruct(struct ofproto *ofproto_)
1027 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1028 struct rule_dpif *rule, *next_rule;
1029 struct ofproto_packet_in *pin, *next_pin;
1030 struct oftable *table;
1033 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1034 ovs_rwlock_wrlock(&xlate_rwlock);
1035 xlate_remove_ofproto(ofproto);
1036 ovs_rwlock_unlock(&xlate_rwlock);
1038 /* Discard any flow_miss_batches queued up for 'ofproto', avoiding a
1039 * use-after-free error. */
1040 udpif_revalidate(ofproto->backer->udpif);
1042 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1044 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1045 struct cls_cursor cursor;
1047 fat_rwlock_rdlock(&table->cls.rwlock);
1048 cls_cursor_init(&cursor, &table->cls, NULL);
1049 fat_rwlock_unlock(&table->cls.rwlock);
1050 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1051 ofproto_rule_delete(&ofproto->up, &rule->up);
1055 guarded_list_pop_all(&ofproto->pins, &pins);
1056 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1057 list_remove(&pin->list_node);
1058 free(CONST_CAST(void *, pin->up.packet));
1061 guarded_list_destroy(&ofproto->pins);
1063 mbridge_unref(ofproto->mbridge);
1065 netflow_unref(ofproto->netflow);
1066 dpif_sflow_unref(ofproto->sflow);
1067 hmap_destroy(&ofproto->bundles);
1068 mac_learning_unref(ofproto->ml);
1070 hmap_destroy(&ofproto->vlandev_map);
1071 hmap_destroy(&ofproto->realdev_vid_map);
1073 sset_destroy(&ofproto->ports);
1074 sset_destroy(&ofproto->ghost_ports);
1075 sset_destroy(&ofproto->port_poll_set);
1077 ovs_mutex_destroy(&ofproto->stats_mutex);
1078 ovs_mutex_destroy(&ofproto->vsp_mutex);
1080 close_dpif_backer(ofproto->backer);
1084 run(struct ofproto *ofproto_)
1086 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1087 uint64_t new_seq, new_dump_seq;
1089 if (mbridge_need_revalidate(ofproto->mbridge)) {
1090 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1091 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1092 mac_learning_flush(ofproto->ml);
1093 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1096 /* Do not perform any periodic activity required by 'ofproto' while
1097 * waiting for flow restore to complete. */
1098 if (!ofproto_get_flow_restore_wait()) {
1099 struct ofproto_packet_in *pin, *next_pin;
1102 guarded_list_pop_all(&ofproto->pins, &pins);
1103 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1104 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1105 list_remove(&pin->list_node);
1106 free(CONST_CAST(void *, pin->up.packet));
1111 if (ofproto->netflow) {
1112 netflow_run(ofproto->netflow);
1114 if (ofproto->sflow) {
1115 dpif_sflow_run(ofproto->sflow);
1117 if (ofproto->ipfix) {
1118 dpif_ipfix_run(ofproto->ipfix);
1121 new_seq = seq_read(connectivity_seq_get());
1122 if (ofproto->change_seq != new_seq) {
1123 struct ofport_dpif *ofport;
1125 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1129 ofproto->change_seq = new_seq;
1131 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1132 struct ofbundle *bundle;
1134 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1140 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1141 if (mac_learning_run(ofproto->ml)) {
1142 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1144 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1146 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1147 if (ofproto->dump_seq != new_dump_seq) {
1148 struct rule *rule, *next_rule;
1150 /* We know stats are relatively fresh, so now is a good time to do some
1152 ofproto->dump_seq = new_dump_seq;
1154 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1156 ovs_mutex_lock(&ofproto_mutex);
1157 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1158 &ofproto->up.expirable) {
1159 rule_expire(rule_dpif_cast(rule));
1161 ovs_mutex_unlock(&ofproto_mutex);
1163 /* All outstanding data in existing flows has been accounted, so it's a
1164 * good time to do bond rebalancing. */
1165 if (ofproto->has_bonded_bundles) {
1166 struct ofbundle *bundle;
1168 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1170 bond_rebalance(bundle->bond);
1180 wait(struct ofproto *ofproto_)
1182 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1184 if (ofproto_get_flow_restore_wait()) {
1188 if (ofproto->sflow) {
1189 dpif_sflow_wait(ofproto->sflow);
1191 if (ofproto->ipfix) {
1192 dpif_ipfix_wait(ofproto->ipfix);
1194 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1195 struct ofbundle *bundle;
1197 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1198 bundle_wait(bundle);
1201 if (ofproto->netflow) {
1202 netflow_wait(ofproto->netflow);
1204 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1205 mac_learning_wait(ofproto->ml);
1206 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1208 if (ofproto->backer->need_revalidate) {
1209 /* Shouldn't happen, but if it does just go around again. */
1210 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1211 poll_immediate_wake();
1214 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1218 type_get_memory_usage(const char *type, struct simap *usage)
1220 struct dpif_backer *backer;
1222 backer = shash_find_data(&all_dpif_backers, type);
1224 udpif_get_memory_usage(backer->udpif, usage);
1229 flush(struct ofproto *ofproto OVS_UNUSED)
1235 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1236 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1238 *arp_match_ip = true;
1239 *actions = (OFPUTIL_A_OUTPUT |
1240 OFPUTIL_A_SET_VLAN_VID |
1241 OFPUTIL_A_SET_VLAN_PCP |
1242 OFPUTIL_A_STRIP_VLAN |
1243 OFPUTIL_A_SET_DL_SRC |
1244 OFPUTIL_A_SET_DL_DST |
1245 OFPUTIL_A_SET_NW_SRC |
1246 OFPUTIL_A_SET_NW_DST |
1247 OFPUTIL_A_SET_NW_TOS |
1248 OFPUTIL_A_SET_TP_SRC |
1249 OFPUTIL_A_SET_TP_DST |
1254 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1256 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1257 struct dpif_dp_stats s;
1258 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1261 strcpy(ots->name, "classifier");
1263 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1264 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes);
1265 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes);
1266 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes);
1268 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1269 ots->lookup_count = htonll(n_lookup);
1270 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1273 static struct ofport *
1276 struct ofport_dpif *port = xmalloc(sizeof *port);
1281 port_dealloc(struct ofport *port_)
1283 struct ofport_dpif *port = ofport_dpif_cast(port_);
1288 port_construct(struct ofport *port_)
1290 struct ofport_dpif *port = ofport_dpif_cast(port_);
1291 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1292 const struct netdev *netdev = port->up.netdev;
1293 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1294 struct dpif_port dpif_port;
1297 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1298 port->bundle = NULL;
1301 port->may_enable = true;
1302 port->stp_port = NULL;
1303 port->stp_state = STP_DISABLED;
1304 port->is_tunnel = false;
1308 port->realdev_ofp_port = 0;
1309 port->vlandev_vid = 0;
1310 port->carrier_seq = netdev_get_carrier_resets(netdev);
1311 port->is_layer3 = netdev_vport_is_layer3(netdev);
1313 if (netdev_vport_is_patch(netdev)) {
1314 /* By bailing out here, we don't submit the port to the sFlow module
1315 * to be considered for counter polling export. This is correct
1316 * because the patch port represents an interface that sFlow considers
1317 * to be "internal" to the switch as a whole, and therefore not an
1318 * candidate for counter polling. */
1319 port->odp_port = ODPP_NONE;
1320 ofport_update_peer(port);
1324 error = dpif_port_query_by_name(ofproto->backer->dpif,
1325 netdev_vport_get_dpif_port(netdev, namebuf,
1332 port->odp_port = dpif_port.port_no;
1334 if (netdev_get_tunnel_config(netdev)) {
1335 tnl_port_add(port, port->up.netdev, port->odp_port);
1336 port->is_tunnel = true;
1338 /* Sanity-check that a mapping doesn't already exist. This
1339 * shouldn't happen for non-tunnel ports. */
1340 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1341 VLOG_ERR("port %s already has an OpenFlow port number",
1343 dpif_port_destroy(&dpif_port);
1347 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1348 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1349 hash_odp_port(port->odp_port));
1350 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1352 dpif_port_destroy(&dpif_port);
1354 if (ofproto->sflow) {
1355 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1362 port_destruct(struct ofport *port_)
1364 struct ofport_dpif *port = ofport_dpif_cast(port_);
1365 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1366 const char *devname = netdev_get_name(port->up.netdev);
1367 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1368 const char *dp_port_name;
1370 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1371 ovs_rwlock_wrlock(&xlate_rwlock);
1372 xlate_ofport_remove(port);
1373 ovs_rwlock_unlock(&xlate_rwlock);
1375 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1377 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1378 /* The underlying device is still there, so delete it. This
1379 * happens when the ofproto is being destroyed, since the caller
1380 * assumes that removal of attached ports will happen as part of
1382 if (!port->is_tunnel) {
1383 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1388 port->peer->peer = NULL;
1392 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1393 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1394 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1395 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1399 sset_find_and_delete(&ofproto->ports, devname);
1400 sset_find_and_delete(&ofproto->ghost_ports, devname);
1401 bundle_remove(port_);
1402 set_cfm(port_, NULL);
1403 set_bfd(port_, NULL);
1404 if (port->stp_port) {
1405 stp_port_disable(port->stp_port);
1407 if (ofproto->sflow) {
1408 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1415 port_modified(struct ofport *port_)
1417 struct ofport_dpif *port = ofport_dpif_cast(port_);
1419 if (port->bundle && port->bundle->bond) {
1420 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1424 cfm_set_netdev(port->cfm, port->up.netdev);
1428 bfd_set_netdev(port->bfd, port->up.netdev);
1431 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1432 port->up.pp.hw_addr);
1434 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1436 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1440 ofport_update_peer(port);
1444 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1446 struct ofport_dpif *port = ofport_dpif_cast(port_);
1447 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1448 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1450 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1451 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1452 OFPUTIL_PC_NO_PACKET_IN)) {
1453 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1455 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1456 bundle_update(port->bundle);
1462 set_sflow(struct ofproto *ofproto_,
1463 const struct ofproto_sflow_options *sflow_options)
1465 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1466 struct dpif_sflow *ds = ofproto->sflow;
1468 if (sflow_options) {
1470 struct ofport_dpif *ofport;
1472 ds = ofproto->sflow = dpif_sflow_create();
1473 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1474 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1476 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1478 dpif_sflow_set_options(ds, sflow_options);
1481 dpif_sflow_unref(ds);
1482 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1483 ofproto->sflow = NULL;
1491 struct ofproto *ofproto_,
1492 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1493 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1494 size_t n_flow_exporters_options)
1496 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1497 struct dpif_ipfix *di = ofproto->ipfix;
1498 bool has_options = bridge_exporter_options || flow_exporters_options;
1500 if (has_options && !di) {
1501 di = ofproto->ipfix = dpif_ipfix_create();
1505 /* Call set_options in any case to cleanly flush the flow
1506 * caches in the last exporters that are to be destroyed. */
1507 dpif_ipfix_set_options(
1508 di, bridge_exporter_options, flow_exporters_options,
1509 n_flow_exporters_options);
1512 dpif_ipfix_unref(di);
1513 ofproto->ipfix = NULL;
1521 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1523 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1528 struct ofproto_dpif *ofproto;
1530 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1531 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1532 ofport->cfm = cfm_create(ofport->up.netdev);
1535 if (cfm_configure(ofport->cfm, s)) {
1542 cfm_unref(ofport->cfm);
1545 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1546 ofport->up.pp.hw_addr);
1551 get_cfm_status(const struct ofport *ofport_,
1552 struct ofproto_cfm_status *status)
1554 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1557 status->faults = cfm_get_fault(ofport->cfm);
1558 status->flap_count = cfm_get_flap_count(ofport->cfm);
1559 status->remote_opstate = cfm_get_opup(ofport->cfm);
1560 status->health = cfm_get_health(ofport->cfm);
1561 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1569 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1571 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1572 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1576 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1577 cfg, ofport->up.netdev);
1578 if (ofport->bfd != old) {
1579 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1581 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1582 ofport->up.pp.hw_addr);
1587 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1589 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1592 bfd_get_status(ofport->bfd, smap);
1599 /* Spanning Tree. */
1602 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1604 struct ofproto_dpif *ofproto = ofproto_;
1605 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1606 struct ofport_dpif *ofport;
1608 ofport = stp_port_get_aux(sp);
1610 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1611 ofproto->up.name, port_num);
1613 struct eth_header *eth = pkt->l2;
1615 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1616 if (eth_addr_is_zero(eth->eth_src)) {
1617 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1618 "with unknown MAC", ofproto->up.name, port_num);
1620 ofproto_dpif_send_packet(ofport, pkt);
1626 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1628 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1630 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1632 /* Only revalidate flows if the configuration changed. */
1633 if (!s != !ofproto->stp) {
1634 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1638 if (!ofproto->stp) {
1639 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1640 send_bpdu_cb, ofproto);
1641 ofproto->stp_last_tick = time_msec();
1644 stp_set_bridge_id(ofproto->stp, s->system_id);
1645 stp_set_bridge_priority(ofproto->stp, s->priority);
1646 stp_set_hello_time(ofproto->stp, s->hello_time);
1647 stp_set_max_age(ofproto->stp, s->max_age);
1648 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1650 struct ofport *ofport;
1652 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1653 set_stp_port(ofport, NULL);
1656 stp_unref(ofproto->stp);
1657 ofproto->stp = NULL;
1664 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1666 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1670 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1671 s->designated_root = stp_get_designated_root(ofproto->stp);
1672 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1681 update_stp_port_state(struct ofport_dpif *ofport)
1683 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1684 enum stp_state state;
1686 /* Figure out new state. */
1687 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1691 if (ofport->stp_state != state) {
1692 enum ofputil_port_state of_state;
1695 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1696 netdev_get_name(ofport->up.netdev),
1697 stp_state_name(ofport->stp_state),
1698 stp_state_name(state));
1699 if (stp_learn_in_state(ofport->stp_state)
1700 != stp_learn_in_state(state)) {
1701 /* xxx Learning action flows should also be flushed. */
1702 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1703 mac_learning_flush(ofproto->ml);
1704 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1706 fwd_change = stp_forward_in_state(ofport->stp_state)
1707 != stp_forward_in_state(state);
1709 ofproto->backer->need_revalidate = REV_STP;
1710 ofport->stp_state = state;
1711 ofport->stp_state_entered = time_msec();
1713 if (fwd_change && ofport->bundle) {
1714 bundle_update(ofport->bundle);
1717 /* Update the STP state bits in the OpenFlow port description. */
1718 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1719 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1720 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1721 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1722 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1724 ofproto_port_set_state(&ofport->up, of_state);
1728 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1729 * caller is responsible for assigning STP port numbers and ensuring
1730 * there are no duplicates. */
1732 set_stp_port(struct ofport *ofport_,
1733 const struct ofproto_port_stp_settings *s)
1735 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1736 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1737 struct stp_port *sp = ofport->stp_port;
1739 if (!s || !s->enable) {
1741 ofport->stp_port = NULL;
1742 stp_port_disable(sp);
1743 update_stp_port_state(ofport);
1746 } else if (sp && stp_port_no(sp) != s->port_num
1747 && ofport == stp_port_get_aux(sp)) {
1748 /* The port-id changed, so disable the old one if it's not
1749 * already in use by another port. */
1750 stp_port_disable(sp);
1753 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1754 stp_port_enable(sp);
1756 stp_port_set_aux(sp, ofport);
1757 stp_port_set_priority(sp, s->priority);
1758 stp_port_set_path_cost(sp, s->path_cost);
1760 update_stp_port_state(ofport);
1766 get_stp_port_status(struct ofport *ofport_,
1767 struct ofproto_port_stp_status *s)
1769 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1770 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1771 struct stp_port *sp = ofport->stp_port;
1773 if (!ofproto->stp || !sp) {
1779 s->port_id = stp_port_get_id(sp);
1780 s->state = stp_port_get_state(sp);
1781 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1782 s->role = stp_port_get_role(sp);
1788 get_stp_port_stats(struct ofport *ofport_,
1789 struct ofproto_port_stp_stats *s)
1791 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1792 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1793 struct stp_port *sp = ofport->stp_port;
1795 if (!ofproto->stp || !sp) {
1801 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1807 stp_run(struct ofproto_dpif *ofproto)
1810 long long int now = time_msec();
1811 long long int elapsed = now - ofproto->stp_last_tick;
1812 struct stp_port *sp;
1815 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1816 ofproto->stp_last_tick = now;
1818 while (stp_get_changed_port(ofproto->stp, &sp)) {
1819 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1822 update_stp_port_state(ofport);
1826 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1827 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1828 mac_learning_flush(ofproto->ml);
1829 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1835 stp_wait(struct ofproto_dpif *ofproto)
1838 poll_timer_wait(1000);
1843 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
1846 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1847 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1849 if (ofport->n_qdscp != n_qdscp
1850 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
1851 n_qdscp * sizeof *qdscp))) {
1852 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1853 free(ofport->qdscp);
1854 ofport->qdscp = n_qdscp
1855 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
1857 ofport->n_qdscp = n_qdscp;
1865 /* Expires all MAC learning entries associated with 'bundle' and forces its
1866 * ofproto to revalidate every flow.
1868 * Normally MAC learning entries are removed only from the ofproto associated
1869 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
1870 * are removed from every ofproto. When patch ports and SLB bonds are in use
1871 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
1872 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
1873 * with the host from which it migrated. */
1875 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
1877 struct ofproto_dpif *ofproto = bundle->ofproto;
1878 struct mac_learning *ml = ofproto->ml;
1879 struct mac_entry *mac, *next_mac;
1881 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1882 ovs_rwlock_wrlock(&ml->rwlock);
1883 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1884 if (mac->port.p == bundle) {
1886 struct ofproto_dpif *o;
1888 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
1890 struct mac_entry *e;
1892 ovs_rwlock_wrlock(&o->ml->rwlock);
1893 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
1895 mac_learning_expire(o->ml, e);
1897 ovs_rwlock_unlock(&o->ml->rwlock);
1902 mac_learning_expire(ml, mac);
1905 ovs_rwlock_unlock(&ml->rwlock);
1908 static struct ofbundle *
1909 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1911 struct ofbundle *bundle;
1913 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1914 &ofproto->bundles) {
1915 if (bundle->aux == aux) {
1923 bundle_update(struct ofbundle *bundle)
1925 struct ofport_dpif *port;
1927 bundle->floodable = true;
1928 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
1929 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
1931 || !stp_forward_in_state(port->stp_state)) {
1932 bundle->floodable = false;
1939 bundle_del_port(struct ofport_dpif *port)
1941 struct ofbundle *bundle = port->bundle;
1943 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
1945 list_remove(&port->bundle_node);
1946 port->bundle = NULL;
1949 lacp_slave_unregister(bundle->lacp, port);
1952 bond_slave_unregister(bundle->bond, port);
1955 bundle_update(bundle);
1959 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
1960 struct lacp_slave_settings *lacp)
1962 struct ofport_dpif *port;
1964 port = get_ofp_port(bundle->ofproto, ofp_port);
1969 if (port->bundle != bundle) {
1970 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
1972 bundle_remove(&port->up);
1975 port->bundle = bundle;
1976 list_push_back(&bundle->ports, &port->bundle_node);
1977 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
1979 || !stp_forward_in_state(port->stp_state)) {
1980 bundle->floodable = false;
1984 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
1985 lacp_slave_register(bundle->lacp, port, lacp);
1992 bundle_destroy(struct ofbundle *bundle)
1994 struct ofproto_dpif *ofproto;
1995 struct ofport_dpif *port, *next_port;
2001 ofproto = bundle->ofproto;
2002 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2004 ovs_rwlock_wrlock(&xlate_rwlock);
2005 xlate_bundle_remove(bundle);
2006 ovs_rwlock_unlock(&xlate_rwlock);
2008 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2009 bundle_del_port(port);
2012 bundle_flush_macs(bundle, true);
2013 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2015 free(bundle->trunks);
2016 lacp_unref(bundle->lacp);
2017 bond_unref(bundle->bond);
2022 bundle_set(struct ofproto *ofproto_, void *aux,
2023 const struct ofproto_bundle_settings *s)
2025 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2026 bool need_flush = false;
2027 struct ofport_dpif *port;
2028 struct ofbundle *bundle;
2029 unsigned long *trunks;
2035 bundle_destroy(bundle_lookup(ofproto, aux));
2039 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2040 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2042 bundle = bundle_lookup(ofproto, aux);
2044 bundle = xmalloc(sizeof *bundle);
2046 bundle->ofproto = ofproto;
2047 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2048 hash_pointer(aux, 0));
2050 bundle->name = NULL;
2052 list_init(&bundle->ports);
2053 bundle->vlan_mode = PORT_VLAN_TRUNK;
2055 bundle->trunks = NULL;
2056 bundle->use_priority_tags = s->use_priority_tags;
2057 bundle->lacp = NULL;
2058 bundle->bond = NULL;
2060 bundle->floodable = true;
2061 mbridge_register_bundle(ofproto->mbridge, bundle);
2064 if (!bundle->name || strcmp(s->name, bundle->name)) {
2066 bundle->name = xstrdup(s->name);
2071 ofproto->lacp_enabled = true;
2072 if (!bundle->lacp) {
2073 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2074 bundle->lacp = lacp_create();
2076 lacp_configure(bundle->lacp, s->lacp);
2078 lacp_unref(bundle->lacp);
2079 bundle->lacp = NULL;
2082 /* Update set of ports. */
2084 for (i = 0; i < s->n_slaves; i++) {
2085 if (!bundle_add_port(bundle, s->slaves[i],
2086 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2090 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2091 struct ofport_dpif *next_port;
2093 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2094 for (i = 0; i < s->n_slaves; i++) {
2095 if (s->slaves[i] == port->up.ofp_port) {
2100 bundle_del_port(port);
2104 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2106 if (list_is_empty(&bundle->ports)) {
2107 bundle_destroy(bundle);
2111 /* Set VLAN tagging mode */
2112 if (s->vlan_mode != bundle->vlan_mode
2113 || s->use_priority_tags != bundle->use_priority_tags) {
2114 bundle->vlan_mode = s->vlan_mode;
2115 bundle->use_priority_tags = s->use_priority_tags;
2120 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2121 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2123 if (vlan != bundle->vlan) {
2124 bundle->vlan = vlan;
2128 /* Get trunked VLANs. */
2129 switch (s->vlan_mode) {
2130 case PORT_VLAN_ACCESS:
2134 case PORT_VLAN_TRUNK:
2135 trunks = CONST_CAST(unsigned long *, s->trunks);
2138 case PORT_VLAN_NATIVE_UNTAGGED:
2139 case PORT_VLAN_NATIVE_TAGGED:
2140 if (vlan != 0 && (!s->trunks
2141 || !bitmap_is_set(s->trunks, vlan)
2142 || bitmap_is_set(s->trunks, 0))) {
2143 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2145 trunks = bitmap_clone(s->trunks, 4096);
2147 trunks = bitmap_allocate1(4096);
2149 bitmap_set1(trunks, vlan);
2150 bitmap_set0(trunks, 0);
2152 trunks = CONST_CAST(unsigned long *, s->trunks);
2159 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2160 free(bundle->trunks);
2161 if (trunks == s->trunks) {
2162 bundle->trunks = vlan_bitmap_clone(trunks);
2164 bundle->trunks = trunks;
2169 if (trunks != s->trunks) {
2174 if (!list_is_short(&bundle->ports)) {
2175 bundle->ofproto->has_bonded_bundles = true;
2177 if (bond_reconfigure(bundle->bond, s->bond)) {
2178 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2181 bundle->bond = bond_create(s->bond);
2182 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2185 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2186 bond_slave_register(bundle->bond, port, port->up.netdev);
2189 bond_unref(bundle->bond);
2190 bundle->bond = NULL;
2193 /* If we changed something that would affect MAC learning, un-learn
2194 * everything on this port and force flow revalidation. */
2196 bundle_flush_macs(bundle, false);
2203 bundle_remove(struct ofport *port_)
2205 struct ofport_dpif *port = ofport_dpif_cast(port_);
2206 struct ofbundle *bundle = port->bundle;
2209 bundle_del_port(port);
2210 if (list_is_empty(&bundle->ports)) {
2211 bundle_destroy(bundle);
2212 } else if (list_is_short(&bundle->ports)) {
2213 bond_unref(bundle->bond);
2214 bundle->bond = NULL;
2220 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2222 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2223 struct ofport_dpif *port = port_;
2224 uint8_t ea[ETH_ADDR_LEN];
2227 error = netdev_get_etheraddr(port->up.netdev, ea);
2229 struct ofpbuf packet;
2232 ofpbuf_init(&packet, 0);
2233 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2235 memcpy(packet_pdu, pdu, pdu_size);
2237 ofproto_dpif_send_packet(port, &packet);
2238 ofpbuf_uninit(&packet);
2240 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2241 "%s (%s)", port->bundle->name,
2242 netdev_get_name(port->up.netdev), ovs_strerror(error));
2247 bundle_send_learning_packets(struct ofbundle *bundle)
2249 struct ofproto_dpif *ofproto = bundle->ofproto;
2250 struct ofpbuf *learning_packet;
2251 int error, n_packets, n_errors;
2252 struct mac_entry *e;
2253 struct list packets;
2255 list_init(&packets);
2256 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2257 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2258 if (e->port.p != bundle) {
2261 learning_packet = bond_compose_learning_packet(bundle->bond,
2264 learning_packet->private_p = port_void;
2265 list_push_back(&packets, &learning_packet->list_node);
2268 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2270 error = n_packets = n_errors = 0;
2271 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2274 ret = ofproto_dpif_send_packet(learning_packet->private_p, learning_packet);
2281 ofpbuf_list_delete(&packets);
2284 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2285 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2286 "packets, last error was: %s",
2287 bundle->name, n_errors, n_packets, ovs_strerror(error));
2289 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2290 bundle->name, n_packets);
2295 bundle_run(struct ofbundle *bundle)
2298 lacp_run(bundle->lacp, send_pdu_cb);
2301 struct ofport_dpif *port;
2303 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2304 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2307 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2308 bundle->ofproto->backer->need_revalidate = REV_BOND;
2311 if (bond_should_send_learning_packets(bundle->bond)) {
2312 bundle_send_learning_packets(bundle);
2318 bundle_wait(struct ofbundle *bundle)
2321 lacp_wait(bundle->lacp);
2324 bond_wait(bundle->bond);
2331 mirror_set__(struct ofproto *ofproto_, void *aux,
2332 const struct ofproto_mirror_settings *s)
2334 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2335 struct ofbundle **srcs, **dsts;
2340 mirror_destroy(ofproto->mbridge, aux);
2344 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2345 dsts = xmalloc(s->n_dsts * sizeof *dsts);
2347 for (i = 0; i < s->n_srcs; i++) {
2348 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2351 for (i = 0; i < s->n_dsts; i++) {
2352 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2355 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2356 s->n_dsts, s->src_vlans,
2357 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2364 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2365 uint64_t *packets, uint64_t *bytes)
2367 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2372 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2374 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2375 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2376 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2377 mac_learning_flush(ofproto->ml);
2379 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2384 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2386 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2387 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2388 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2392 forward_bpdu_changed(struct ofproto *ofproto_)
2394 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2395 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2399 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2402 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2403 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2404 mac_learning_set_idle_time(ofproto->ml, idle_time);
2405 mac_learning_set_max_entries(ofproto->ml, max_entries);
2406 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2411 static struct ofport_dpif *
2412 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2414 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2415 return ofport ? ofport_dpif_cast(ofport) : NULL;
2419 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2420 struct ofproto_port *ofproto_port,
2421 struct dpif_port *dpif_port)
2423 ofproto_port->name = dpif_port->name;
2424 ofproto_port->type = dpif_port->type;
2425 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2429 ofport_update_peer(struct ofport_dpif *ofport)
2431 const struct ofproto_dpif *ofproto;
2432 struct dpif_backer *backer;
2435 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2439 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2440 backer->need_revalidate = REV_RECONFIGURE;
2443 ofport->peer->peer = NULL;
2444 ofport->peer = NULL;
2447 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2452 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2453 struct ofport *peer_ofport;
2454 struct ofport_dpif *peer;
2457 if (ofproto->backer != backer) {
2461 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2466 peer = ofport_dpif_cast(peer_ofport);
2467 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2468 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2470 ofport->peer = peer;
2471 ofport->peer->peer = ofport;
2481 port_run(struct ofport_dpif *ofport)
2483 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2484 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2485 bool enable = netdev_get_carrier(ofport->up.netdev);
2486 bool cfm_enable = false;
2487 bool bfd_enable = false;
2489 ofport->carrier_seq = carrier_seq;
2492 int cfm_opup = cfm_get_opup(ofport->cfm);
2494 cfm_enable = !cfm_get_fault(ofport->cfm);
2496 if (cfm_opup >= 0) {
2497 cfm_enable = cfm_enable && cfm_opup;
2502 bfd_enable = bfd_forwarding(ofport->bfd);
2505 if (ofport->bfd || ofport->cfm) {
2506 enable = enable && (cfm_enable || bfd_enable);
2509 if (ofport->bundle) {
2510 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2511 if (carrier_changed) {
2512 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2516 if (ofport->may_enable != enable) {
2517 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2518 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2521 ofport->may_enable = enable;
2525 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2526 struct ofproto_port *ofproto_port)
2528 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2529 struct dpif_port dpif_port;
2532 if (sset_contains(&ofproto->ghost_ports, devname)) {
2533 const char *type = netdev_get_type_from_name(devname);
2535 /* We may be called before ofproto->up.port_by_name is populated with
2536 * the appropriate ofport. For this reason, we must get the name and
2537 * type from the netdev layer directly. */
2539 const struct ofport *ofport;
2541 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2542 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2543 ofproto_port->name = xstrdup(devname);
2544 ofproto_port->type = xstrdup(type);
2550 if (!sset_contains(&ofproto->ports, devname)) {
2553 error = dpif_port_query_by_name(ofproto->backer->dpif,
2554 devname, &dpif_port);
2556 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2562 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2564 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2565 const char *devname = netdev_get_name(netdev);
2566 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2567 const char *dp_port_name;
2569 if (netdev_vport_is_patch(netdev)) {
2570 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2574 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2575 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2576 odp_port_t port_no = ODPP_NONE;
2579 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2583 if (netdev_get_tunnel_config(netdev)) {
2584 simap_put(&ofproto->backer->tnl_backers,
2585 dp_port_name, odp_to_u32(port_no));
2589 if (netdev_get_tunnel_config(netdev)) {
2590 sset_add(&ofproto->ghost_ports, devname);
2592 sset_add(&ofproto->ports, devname);
2598 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2600 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2601 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2608 sset_find_and_delete(&ofproto->ghost_ports,
2609 netdev_get_name(ofport->up.netdev));
2610 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2611 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2612 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2614 /* The caller is going to close ofport->up.netdev. If this is a
2615 * bonded port, then the bond is using that netdev, so remove it
2616 * from the bond. The client will need to reconfigure everything
2617 * after deleting ports, so then the slave will get re-added. */
2618 bundle_remove(&ofport->up);
2625 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2627 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2630 error = netdev_get_stats(ofport->up.netdev, stats);
2632 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2633 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2635 ovs_mutex_lock(&ofproto->stats_mutex);
2636 /* ofproto->stats.tx_packets represents packets that we created
2637 * internally and sent to some port (e.g. packets sent with
2638 * ofproto_dpif_send_packet()). Account for them as if they had
2639 * come from OFPP_LOCAL and got forwarded. */
2641 if (stats->rx_packets != UINT64_MAX) {
2642 stats->rx_packets += ofproto->stats.tx_packets;
2645 if (stats->rx_bytes != UINT64_MAX) {
2646 stats->rx_bytes += ofproto->stats.tx_bytes;
2649 /* ofproto->stats.rx_packets represents packets that were received on
2650 * some port and we processed internally and dropped (e.g. STP).
2651 * Account for them as if they had been forwarded to OFPP_LOCAL. */
2653 if (stats->tx_packets != UINT64_MAX) {
2654 stats->tx_packets += ofproto->stats.rx_packets;
2657 if (stats->tx_bytes != UINT64_MAX) {
2658 stats->tx_bytes += ofproto->stats.rx_bytes;
2660 ovs_mutex_unlock(&ofproto->stats_mutex);
2666 struct port_dump_state {
2671 struct ofproto_port port;
2676 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
2678 *statep = xzalloc(sizeof(struct port_dump_state));
2683 port_dump_next(const struct ofproto *ofproto_, void *state_,
2684 struct ofproto_port *port)
2686 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2687 struct port_dump_state *state = state_;
2688 const struct sset *sset;
2689 struct sset_node *node;
2691 if (state->has_port) {
2692 ofproto_port_destroy(&state->port);
2693 state->has_port = false;
2695 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
2696 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
2699 error = port_query_by_name(ofproto_, node->name, &state->port);
2701 *port = state->port;
2702 state->has_port = true;
2704 } else if (error != ENODEV) {
2709 if (!state->ghost) {
2710 state->ghost = true;
2713 return port_dump_next(ofproto_, state_, port);
2720 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2722 struct port_dump_state *state = state_;
2724 if (state->has_port) {
2725 ofproto_port_destroy(&state->port);
2732 port_poll(const struct ofproto *ofproto_, char **devnamep)
2734 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2736 if (ofproto->port_poll_errno) {
2737 int error = ofproto->port_poll_errno;
2738 ofproto->port_poll_errno = 0;
2742 if (sset_is_empty(&ofproto->port_poll_set)) {
2746 *devnamep = sset_pop(&ofproto->port_poll_set);
2751 port_poll_wait(const struct ofproto *ofproto_)
2753 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2754 dpif_port_poll_wait(ofproto->backer->dpif);
2758 port_is_lacp_current(const struct ofport *ofport_)
2760 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2761 return (ofport->bundle && ofport->bundle->lacp
2762 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2766 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2767 * then delete it entirely. */
2769 rule_expire(struct rule_dpif *rule)
2770 OVS_REQUIRES(ofproto_mutex)
2772 uint16_t idle_timeout, hard_timeout;
2773 long long int now = time_msec();
2776 ovs_assert(!rule->up.pending);
2778 /* Has 'rule' expired? */
2779 ovs_mutex_lock(&rule->up.mutex);
2780 hard_timeout = rule->up.hard_timeout;
2781 idle_timeout = rule->up.idle_timeout;
2782 if (hard_timeout && now > rule->up.modified + hard_timeout * 1000) {
2783 reason = OFPRR_HARD_TIMEOUT;
2784 } else if (idle_timeout && now > rule->up.used + idle_timeout * 1000) {
2785 reason = OFPRR_IDLE_TIMEOUT;
2789 ovs_mutex_unlock(&rule->up.mutex);
2792 COVERAGE_INC(ofproto_dpif_expired);
2793 ofproto_rule_expire(&rule->up, reason);
2797 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
2798 * 'flow' must reflect the data in 'packet'. */
2800 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
2801 const struct flow *flow,
2802 struct rule_dpif *rule,
2803 const struct ofpact *ofpacts, size_t ofpacts_len,
2804 struct ofpbuf *packet)
2806 struct odputil_keybuf keybuf;
2807 struct dpif_flow_stats stats;
2808 struct xlate_out xout;
2809 struct xlate_in xin;
2814 ovs_assert((rule != NULL) != (ofpacts != NULL));
2816 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
2818 rule_dpif_credit_stats(rule, &stats);
2821 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
2822 xin.ofpacts = ofpacts;
2823 xin.ofpacts_len = ofpacts_len;
2824 xin.resubmit_stats = &stats;
2825 xlate_actions(&xin, &xout);
2827 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2828 in_port = flow->in_port.ofp_port;
2829 if (in_port == OFPP_NONE) {
2830 in_port = OFPP_LOCAL;
2832 odp_flow_key_from_flow(&key, flow, ofp_port_to_odp_port(ofproto, in_port));
2834 error = dpif_execute(ofproto->backer->dpif, key.data, key.size,
2835 xout.odp_actions.data, xout.odp_actions.size, packet,
2836 (xout.slow & SLOW_ACTION) != 0);
2837 xlate_out_uninit(&xout);
2843 rule_dpif_credit_stats(struct rule_dpif *rule,
2844 const struct dpif_flow_stats *stats)
2846 ovs_mutex_lock(&rule->stats_mutex);
2847 rule->packet_count += stats->n_packets;
2848 rule->byte_count += stats->n_bytes;
2849 rule->up.used = MAX(rule->up.used, stats->used);
2850 ovs_mutex_unlock(&rule->stats_mutex);
2854 rule_dpif_is_fail_open(const struct rule_dpif *rule)
2856 return is_fail_open_rule(&rule->up);
2860 rule_dpif_is_table_miss(const struct rule_dpif *rule)
2862 return rule_is_table_miss(&rule->up);
2866 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
2867 OVS_REQUIRES(rule->up.mutex)
2869 return rule->up.flow_cookie;
2873 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
2874 uint16_t hard_timeout)
2876 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
2879 /* Returns 'rule''s actions. The caller owns a reference on the returned
2880 * actions and must eventually release it (with rule_actions_unref()) to avoid
2882 struct rule_actions *
2883 rule_dpif_get_actions(const struct rule_dpif *rule)
2885 return rule_get_actions(&rule->up);
2888 /* Lookup 'flow' in 'ofproto''s classifier. If 'wc' is non-null, sets
2889 * the fields that were relevant as part of the lookup. */
2891 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2892 struct flow_wildcards *wc, struct rule_dpif **rule)
2894 struct ofport_dpif *port;
2896 if (rule_dpif_lookup_in_table(ofproto, flow, wc, 0, rule)) {
2899 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
2901 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
2902 flow->in_port.ofp_port);
2905 choose_miss_rule(port ? port->up.pp.config : 0, ofproto->miss_rule,
2906 ofproto->no_packet_in_rule, rule);
2910 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto,
2911 const struct flow *flow, struct flow_wildcards *wc,
2912 uint8_t table_id, struct rule_dpif **rule)
2914 const struct cls_rule *cls_rule;
2915 struct classifier *cls;
2919 if (table_id >= N_TABLES) {
2924 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
2925 if (is_ip_any(flow)) {
2926 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
2930 cls = &ofproto->up.tables[table_id].cls;
2931 fat_rwlock_rdlock(&cls->rwlock);
2932 frag = (flow->nw_frag & FLOW_NW_FRAG_ANY) != 0;
2933 if (frag && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
2934 /* We must pretend that transport ports are unavailable. */
2935 struct flow ofpc_normal_flow = *flow;
2936 ofpc_normal_flow.tp_src = htons(0);
2937 ofpc_normal_flow.tp_dst = htons(0);
2938 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
2939 } else if (frag && ofproto->up.frag_handling == OFPC_FRAG_DROP) {
2940 cls_rule = &ofproto->drop_frags_rule->up.cr;
2941 /* Frag mask in wc already set above. */
2943 cls_rule = classifier_lookup(cls, flow, wc);
2946 *rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
2947 rule_dpif_ref(*rule);
2948 fat_rwlock_unlock(&cls->rwlock);
2950 return *rule != NULL;
2953 /* Given a port configuration (specified as zero if there's no port), chooses
2954 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
2955 * flow table miss. */
2957 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
2958 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
2960 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
2961 rule_dpif_ref(*rule);
2965 rule_dpif_ref(struct rule_dpif *rule)
2968 ofproto_rule_ref(&rule->up);
2973 rule_dpif_unref(struct rule_dpif *rule)
2976 ofproto_rule_unref(&rule->up);
2981 complete_operation(struct rule_dpif *rule)
2982 OVS_REQUIRES(ofproto_mutex)
2984 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
2986 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
2987 ofoperation_complete(rule->up.pending, 0);
2990 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
2992 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
2995 static struct rule *
2998 struct rule_dpif *rule = xmalloc(sizeof *rule);
3003 rule_dealloc(struct rule *rule_)
3005 struct rule_dpif *rule = rule_dpif_cast(rule_);
3010 rule_construct(struct rule *rule_)
3012 struct rule_dpif *rule = rule_dpif_cast(rule_);
3013 ovs_mutex_init(&rule->stats_mutex);
3014 ovs_mutex_lock(&rule->stats_mutex);
3015 rule->packet_count = 0;
3016 rule->byte_count = 0;
3017 ovs_mutex_unlock(&rule->stats_mutex);
3022 rule_insert(struct rule *rule_)
3023 OVS_REQUIRES(ofproto_mutex)
3025 struct rule_dpif *rule = rule_dpif_cast(rule_);
3026 complete_operation(rule);
3030 rule_delete(struct rule *rule_)
3031 OVS_REQUIRES(ofproto_mutex)
3033 struct rule_dpif *rule = rule_dpif_cast(rule_);
3034 complete_operation(rule);
3038 rule_destruct(struct rule *rule_)
3040 struct rule_dpif *rule = rule_dpif_cast(rule_);
3041 ovs_mutex_destroy(&rule->stats_mutex);
3045 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3047 struct rule_dpif *rule = rule_dpif_cast(rule_);
3049 ovs_mutex_lock(&rule->stats_mutex);
3050 *packets = rule->packet_count;
3051 *bytes = rule->byte_count;
3052 ovs_mutex_unlock(&rule->stats_mutex);
3056 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3057 struct ofpbuf *packet)
3059 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3061 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3065 rule_execute(struct rule *rule, const struct flow *flow,
3066 struct ofpbuf *packet)
3068 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3069 ofpbuf_delete(packet);
3074 rule_modify_actions(struct rule *rule_, bool reset_counters)
3075 OVS_REQUIRES(ofproto_mutex)
3077 struct rule_dpif *rule = rule_dpif_cast(rule_);
3079 if (reset_counters) {
3080 ovs_mutex_lock(&rule->stats_mutex);
3081 rule->packet_count = 0;
3082 rule->byte_count = 0;
3083 ovs_mutex_unlock(&rule->stats_mutex);
3086 complete_operation(rule);
3089 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3091 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3094 static struct ofgroup *
3097 struct group_dpif *group = xzalloc(sizeof *group);
3102 group_dealloc(struct ofgroup *group_)
3104 struct group_dpif *group = group_dpif_cast(group_);
3109 group_construct_stats(struct group_dpif *group)
3110 OVS_REQUIRES(group->stats_mutex)
3112 group->packet_count = 0;
3113 group->byte_count = 0;
3114 if (!group->bucket_stats) {
3115 group->bucket_stats = xcalloc(group->up.n_buckets,
3116 sizeof *group->bucket_stats);
3118 memset(group->bucket_stats, 0, group->up.n_buckets *
3119 sizeof *group->bucket_stats);
3124 group_construct(struct ofgroup *group_)
3126 struct group_dpif *group = group_dpif_cast(group_);
3127 ovs_mutex_init(&group->stats_mutex);
3128 ovs_mutex_lock(&group->stats_mutex);
3129 group_construct_stats(group);
3130 ovs_mutex_unlock(&group->stats_mutex);
3135 group_destruct__(struct group_dpif *group)
3136 OVS_REQUIRES(group->stats_mutex)
3138 free(group->bucket_stats);
3139 group->bucket_stats = NULL;
3143 group_destruct(struct ofgroup *group_)
3145 struct group_dpif *group = group_dpif_cast(group_);
3146 ovs_mutex_lock(&group->stats_mutex);
3147 group_destruct__(group);
3148 ovs_mutex_unlock(&group->stats_mutex);
3149 ovs_mutex_destroy(&group->stats_mutex);
3153 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3155 struct group_dpif *group = group_dpif_cast(group_);
3156 struct group_dpif *victim = group_dpif_cast(victim_);
3158 ovs_mutex_lock(&group->stats_mutex);
3159 if (victim->up.n_buckets < group->up.n_buckets) {
3160 group_destruct__(group);
3162 group_construct_stats(group);
3163 ovs_mutex_unlock(&group->stats_mutex);
3169 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3171 struct group_dpif *group = group_dpif_cast(group_);
3173 ovs_mutex_lock(&group->stats_mutex);
3174 ogs->packet_count = group->packet_count;
3175 ogs->byte_count = group->byte_count;
3176 memcpy(ogs->bucket_stats, group->bucket_stats,
3177 group->up.n_buckets * sizeof *group->bucket_stats);
3178 ovs_mutex_unlock(&group->stats_mutex);
3184 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3185 struct group_dpif **group)
3186 OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3188 struct ofgroup *ofgroup;
3192 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3193 *group = found ? group_dpif_cast(ofgroup) : NULL;
3199 group_dpif_release(struct group_dpif *group)
3200 OVS_RELEASES(group->up.rwlock)
3202 ofproto_group_release(&group->up);
3206 group_dpif_get_buckets(const struct group_dpif *group,
3207 const struct list **buckets)
3209 *buckets = &group->up.buckets;
3212 enum ofp11_group_type
3213 group_dpif_get_type(const struct group_dpif *group)
3215 return group->up.type;
3218 /* Sends 'packet' out 'ofport'.
3219 * May modify 'packet'.
3220 * Returns 0 if successful, otherwise a positive errno value. */
3222 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3224 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3227 error = xlate_send_packet(ofport, packet);
3229 ovs_mutex_lock(&ofproto->stats_mutex);
3230 ofproto->stats.tx_packets++;
3231 ofproto->stats.tx_bytes += packet->size;
3232 ovs_mutex_unlock(&ofproto->stats_mutex);
3237 set_frag_handling(struct ofproto *ofproto_,
3238 enum ofp_config_flags frag_handling)
3240 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3241 if (frag_handling != OFPC_FRAG_REASM) {
3242 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3250 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3251 const struct flow *flow,
3252 const struct ofpact *ofpacts, size_t ofpacts_len)
3254 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3256 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3257 ofpacts_len, packet);
3264 set_netflow(struct ofproto *ofproto_,
3265 const struct netflow_options *netflow_options)
3267 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3269 if (netflow_options) {
3270 if (!ofproto->netflow) {
3271 ofproto->netflow = netflow_create();
3272 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3274 return netflow_set_options(ofproto->netflow, netflow_options);
3275 } else if (ofproto->netflow) {
3276 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3277 netflow_unref(ofproto->netflow);
3278 ofproto->netflow = NULL;
3285 get_netflow_ids(const struct ofproto *ofproto_,
3286 uint8_t *engine_type, uint8_t *engine_id)
3288 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3290 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3293 static struct ofproto_dpif *
3294 ofproto_dpif_lookup(const char *name)
3296 struct ofproto_dpif *ofproto;
3298 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3299 hash_string(name, 0), &all_ofproto_dpifs) {
3300 if (!strcmp(ofproto->up.name, name)) {
3308 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3309 const char *argv[], void *aux OVS_UNUSED)
3311 struct ofproto_dpif *ofproto;
3314 ofproto = ofproto_dpif_lookup(argv[1]);
3316 unixctl_command_reply_error(conn, "no such bridge");
3319 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3320 mac_learning_flush(ofproto->ml);
3321 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3323 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3324 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3325 mac_learning_flush(ofproto->ml);
3326 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3330 unixctl_command_reply(conn, "table successfully flushed");
3333 static struct ofport_dpif *
3334 ofbundle_get_a_port(const struct ofbundle *bundle)
3336 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3341 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3342 const char *argv[], void *aux OVS_UNUSED)
3344 struct ds ds = DS_EMPTY_INITIALIZER;
3345 const struct ofproto_dpif *ofproto;
3346 const struct mac_entry *e;
3348 ofproto = ofproto_dpif_lookup(argv[1]);
3350 unixctl_command_reply_error(conn, "no such bridge");
3354 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3355 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3356 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3357 struct ofbundle *bundle = e->port.p;
3358 char name[OFP_MAX_PORT_NAME_LEN];
3360 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3362 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
3363 name, e->vlan, ETH_ADDR_ARGS(e->mac),
3364 mac_entry_age(ofproto->ml, e));
3366 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3367 unixctl_command_reply(conn, ds_cstr(&ds));
3372 struct xlate_out xout;
3373 struct xlate_in xin;
3379 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
3381 struct rule_actions *actions;
3384 ds_put_char_multiple(result, '\t', level);
3386 ds_put_cstr(result, "No match\n");
3390 ovs_mutex_lock(&rule->up.mutex);
3391 cookie = rule->up.flow_cookie;
3392 ovs_mutex_unlock(&rule->up.mutex);
3394 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3395 rule ? rule->up.table_id : 0, ntohll(cookie));
3396 cls_rule_format(&rule->up.cr, result);
3397 ds_put_char(result, '\n');
3399 actions = rule_dpif_get_actions(rule);
3401 ds_put_char_multiple(result, '\t', level);
3402 ds_put_cstr(result, "OpenFlow actions=");
3403 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
3404 ds_put_char(result, '\n');
3406 rule_actions_unref(actions);
3410 trace_format_flow(struct ds *result, int level, const char *title,
3411 struct trace_ctx *trace)
3413 ds_put_char_multiple(result, '\t', level);
3414 ds_put_format(result, "%s: ", title);
3415 if (flow_equal(&trace->xin.flow, &trace->flow)) {
3416 ds_put_cstr(result, "unchanged");
3418 flow_format(result, &trace->xin.flow);
3419 trace->flow = trace->xin.flow;
3421 ds_put_char(result, '\n');
3425 trace_format_regs(struct ds *result, int level, const char *title,
3426 struct trace_ctx *trace)
3430 ds_put_char_multiple(result, '\t', level);
3431 ds_put_format(result, "%s:", title);
3432 for (i = 0; i < FLOW_N_REGS; i++) {
3433 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
3435 ds_put_char(result, '\n');
3439 trace_format_odp(struct ds *result, int level, const char *title,
3440 struct trace_ctx *trace)
3442 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
3444 ds_put_char_multiple(result, '\t', level);
3445 ds_put_format(result, "%s: ", title);
3446 format_odp_actions(result, odp_actions->data, odp_actions->size);
3447 ds_put_char(result, '\n');
3451 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3453 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3454 struct ds *result = trace->result;
3456 ds_put_char(result, '\n');
3457 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3458 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3459 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
3460 trace_format_rule(result, recurse + 1, rule);
3464 trace_report(struct xlate_in *xin, const char *s, int recurse)
3466 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3467 struct ds *result = trace->result;
3469 ds_put_char_multiple(result, '\t', recurse);
3470 ds_put_cstr(result, s);
3471 ds_put_char(result, '\n');
3474 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
3475 * forms are supported:
3477 * - [dpname] odp_flow [-generate | packet]
3478 * - bridge br_flow [-generate | packet]
3480 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
3481 * returns a nonnull malloced error message. */
3482 static char * WARN_UNUSED_RESULT
3483 parse_flow_and_packet(int argc, const char *argv[],
3484 struct ofproto_dpif **ofprotop, struct flow *flow,
3485 struct ofpbuf **packetp)
3487 const struct dpif_backer *backer = NULL;
3488 const char *error = NULL;
3490 struct simap port_names = SIMAP_INITIALIZER(&port_names);
3491 struct ofpbuf *packet;
3492 struct ofpbuf odp_key;
3493 struct ofpbuf odp_mask;
3495 ofpbuf_init(&odp_key, 0);
3496 ofpbuf_init(&odp_mask, 0);
3498 /* Handle "-generate" or a hex string as the last argument. */
3499 if (!strcmp(argv[argc - 1], "-generate")) {
3500 packet = ofpbuf_new(0);
3503 error = eth_from_hex(argv[argc - 1], &packet);
3506 } else if (argc == 4) {
3507 /* The 3-argument form must end in "-generate' or a hex string. */
3513 /* odp_flow can have its in_port specified as a name instead of port no.
3514 * We do not yet know whether a given flow is a odp_flow or a br_flow.
3515 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3516 * we need to create a simap of name to port no. */
3518 const char *dp_type;
3519 if (!strncmp(argv[1], "ovs-", 4)) {
3520 dp_type = argv[1] + 4;
3524 backer = shash_find_data(&all_dpif_backers, dp_type);
3525 } else if (argc == 2) {
3526 struct shash_node *node;
3527 if (shash_count(&all_dpif_backers) == 1) {
3528 node = shash_first(&all_dpif_backers);
3529 backer = node->data;
3532 error = "Syntax error";
3535 if (backer && backer->dpif) {
3536 struct dpif_port dpif_port;
3537 struct dpif_port_dump port_dump;
3538 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
3539 simap_put(&port_names, dpif_port.name,
3540 odp_to_u32(dpif_port.port_no));
3544 /* Parse the flow and determine whether a datapath or
3545 * bridge is specified. If function odp_flow_key_from_string()
3546 * returns 0, the flow is a odp_flow. If function
3547 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
3548 if (!odp_flow_from_string(argv[argc - 1], &port_names,
3549 &odp_key, &odp_mask)) {
3551 error = "Cannot find the datapath";
3555 if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, flow,
3556 NULL, ofprotop, NULL, NULL, NULL, NULL)) {
3557 error = "Invalid datapath flow";
3561 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
3564 m_err = xasprintf("Bad flow syntax: %s", err);
3569 error = "Must specify bridge name";
3573 *ofprotop = ofproto_dpif_lookup(argv[1]);
3575 error = "Unknown bridge name";
3581 /* Generate a packet, if requested. */
3583 if (!packet->size) {
3584 flow_compose(packet, flow);
3586 union flow_in_port in_port = flow->in_port;
3588 /* Use the metadata from the flow and the packet argument
3589 * to reconstruct the flow. */
3590 flow_extract(packet, flow->skb_priority, flow->pkt_mark, NULL,
3596 if (error && !m_err) {
3597 m_err = xstrdup(error);
3600 ofpbuf_delete(packet);
3604 ofpbuf_uninit(&odp_key);
3605 ofpbuf_uninit(&odp_mask);
3606 simap_destroy(&port_names);
3611 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
3612 void *aux OVS_UNUSED)
3614 struct ofproto_dpif *ofproto;
3615 struct ofpbuf *packet;
3619 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3624 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
3625 unixctl_command_reply(conn, ds_cstr(&result));
3626 ds_destroy(&result);
3627 ofpbuf_delete(packet);
3629 unixctl_command_reply_error(conn, error);
3635 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
3636 const char *argv[], void *aux OVS_UNUSED)
3638 enum ofputil_protocol usable_protocols;
3639 struct ofproto_dpif *ofproto;
3640 bool enforce_consistency;
3641 struct ofpbuf ofpacts;
3642 struct ofpbuf *packet;
3647 /* Three kinds of error return values! */
3653 ofpbuf_init(&ofpacts, 0);
3655 /* Parse actions. */
3656 error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
3658 unixctl_command_reply_error(conn, error);
3663 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
3664 * consistency between the flow and the actions. With -consistent, we
3665 * enforce consistency even for a flow supported in OpenFlow 1.0. */
3666 if (!strcmp(argv[1], "-consistent")) {
3667 enforce_consistency = true;
3671 enforce_consistency = false;
3674 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3676 unixctl_command_reply_error(conn, error);
3681 /* Do the same checks as handle_packet_out() in ofproto.c.
3683 * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
3684 * strictly correct because these actions aren't in any table, but it's OK
3685 * because it 'table_id' is used only to check goto_table instructions, but
3686 * packet-outs take a list of actions and therefore it can't include
3689 * We skip the "meter" check here because meter is an instruction, not an
3690 * action, and thus cannot appear in ofpacts. */
3691 in_port = ofp_to_u16(flow.in_port.ofp_port);
3692 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
3693 unixctl_command_reply_error(conn, "invalid in_port");
3696 if (enforce_consistency) {
3697 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
3698 u16_to_ofp(ofproto->up.max_ports),
3699 0, 0, usable_protocols);
3701 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
3702 u16_to_ofp(ofproto->up.max_ports), 0, 0,
3708 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
3709 unixctl_command_reply_error(conn, ds_cstr(&result));
3713 ofproto_trace(ofproto, &flow, packet, ofpacts.data, ofpacts.size, &result);
3714 unixctl_command_reply(conn, ds_cstr(&result));
3717 ds_destroy(&result);
3718 ofpbuf_delete(packet);
3719 ofpbuf_uninit(&ofpacts);
3722 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
3723 * description of the results to 'ds'.
3725 * The trace follows a packet with the specified 'flow' through the flow
3726 * table. 'packet' may be nonnull to trace an actual packet, with consequent
3727 * side effects (if it is nonnull then its flow must be 'flow').
3729 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
3730 * trace, otherwise the actions are determined by a flow table lookup. */
3732 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
3733 const struct ofpbuf *packet,
3734 const struct ofpact ofpacts[], size_t ofpacts_len,
3737 struct rule_dpif *rule;
3738 struct flow_wildcards wc;
3740 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
3741 ds_put_cstr(ds, "Flow: ");
3742 flow_format(ds, flow);
3743 ds_put_char(ds, '\n');
3745 flow_wildcards_init_catchall(&wc);
3749 rule_dpif_lookup(ofproto, flow, &wc, &rule);
3751 trace_format_rule(ds, 0, rule);
3752 if (rule == ofproto->miss_rule) {
3753 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
3754 } else if (rule == ofproto->no_packet_in_rule) {
3755 ds_put_cstr(ds, "\nNo match, packets dropped because "
3756 "OFPPC_NO_PACKET_IN is set on in_port.\n");
3757 } else if (rule == ofproto->drop_frags_rule) {
3758 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
3759 "and the fragment handling mode is \"drop\".\n");
3763 if (rule || ofpacts) {
3764 uint64_t odp_actions_stub[1024 / 8];
3765 struct ofpbuf odp_actions;
3766 struct trace_ctx trace;
3770 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
3773 ofpbuf_use_stub(&odp_actions,
3774 odp_actions_stub, sizeof odp_actions_stub);
3775 xlate_in_init(&trace.xin, ofproto, flow, rule, tcp_flags, packet);
3777 trace.xin.ofpacts = ofpacts;
3778 trace.xin.ofpacts_len = ofpacts_len;
3780 trace.xin.resubmit_hook = trace_resubmit;
3781 trace.xin.report_hook = trace_report;
3783 xlate_actions(&trace.xin, &trace.xout);
3784 flow_wildcards_or(&trace.xout.wc, &trace.xout.wc, &wc);
3786 ds_put_char(ds, '\n');
3787 trace_format_flow(ds, 0, "Final flow", &trace);
3789 match_init(&match, flow, &trace.xout.wc);
3790 ds_put_cstr(ds, "Relevant fields: ");
3791 match_format(&match, ds, OFP_DEFAULT_PRIORITY);
3792 ds_put_char(ds, '\n');
3794 ds_put_cstr(ds, "Datapath actions: ");
3795 format_odp_actions(ds, trace.xout.odp_actions.data,
3796 trace.xout.odp_actions.size);
3798 if (trace.xout.slow) {
3799 enum slow_path_reason slow;
3801 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
3802 "slow path because it:");
3804 slow = trace.xout.slow;
3806 enum slow_path_reason bit = rightmost_1bit(slow);
3808 ds_put_format(ds, "\n\t- %s.",
3809 slow_path_reason_to_explanation(bit));
3815 xlate_out_uninit(&trace.xout);
3818 rule_dpif_unref(rule);
3821 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
3822 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
3823 * to destroy 'ofproto_shash' and free the returned value. */
3824 static const struct shash_node **
3825 get_ofprotos(struct shash *ofproto_shash)
3827 const struct ofproto_dpif *ofproto;
3829 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3830 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
3831 shash_add_nocopy(ofproto_shash, name, ofproto);
3834 return shash_sort(ofproto_shash);
3838 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
3839 const char *argv[] OVS_UNUSED,
3840 void *aux OVS_UNUSED)
3842 struct ds ds = DS_EMPTY_INITIALIZER;
3843 struct shash ofproto_shash;
3844 const struct shash_node **sorted_ofprotos;
3847 shash_init(&ofproto_shash);
3848 sorted_ofprotos = get_ofprotos(&ofproto_shash);
3849 for (i = 0; i < shash_count(&ofproto_shash); i++) {
3850 const struct shash_node *node = sorted_ofprotos[i];
3851 ds_put_format(&ds, "%s\n", node->name);
3854 shash_destroy(&ofproto_shash);
3855 free(sorted_ofprotos);
3857 unixctl_command_reply(conn, ds_cstr(&ds));
3862 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
3864 const struct shash_node **ofprotos;
3865 struct dpif_dp_stats dp_stats;
3866 struct shash ofproto_shash;
3869 dpif_get_dp_stats(backer->dpif, &dp_stats);
3871 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
3872 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
3874 shash_init(&ofproto_shash);
3875 ofprotos = get_ofprotos(&ofproto_shash);
3876 for (i = 0; i < shash_count(&ofproto_shash); i++) {
3877 struct ofproto_dpif *ofproto = ofprotos[i]->data;
3878 const struct shash_node **ports;
3881 if (ofproto->backer != backer) {
3885 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
3887 ports = shash_sort(&ofproto->up.port_by_name);
3888 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
3889 const struct shash_node *node = ports[j];
3890 struct ofport *ofport = node->data;
3892 odp_port_t odp_port;
3894 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
3897 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
3898 if (odp_port != ODPP_NONE) {
3899 ds_put_format(ds, "%"PRIu32":", odp_port);
3901 ds_put_cstr(ds, "none:");
3904 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
3907 if (!netdev_get_config(ofport->netdev, &config)) {
3908 const struct smap_node **nodes;
3911 nodes = smap_sort(&config);
3912 for (i = 0; i < smap_count(&config); i++) {
3913 const struct smap_node *node = nodes[i];
3914 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
3915 node->key, node->value);
3919 smap_destroy(&config);
3921 ds_put_char(ds, ')');
3922 ds_put_char(ds, '\n');
3926 shash_destroy(&ofproto_shash);
3931 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3932 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
3934 struct ds ds = DS_EMPTY_INITIALIZER;
3935 const struct shash_node **backers;
3938 backers = shash_sort(&all_dpif_backers);
3939 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
3940 dpif_show_backer(backers[i]->data, &ds);
3944 unixctl_command_reply(conn, ds_cstr(&ds));
3949 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
3950 const struct nlattr *key, size_t key_len)
3952 enum odp_key_fitness fitness;
3953 struct ofproto_dpif *ofp;
3956 xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &fitness, &ofp,
3957 NULL, NULL, NULL, NULL);
3958 return ofp == ofproto;
3962 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
3963 int argc OVS_UNUSED, const char *argv[],
3964 void *aux OVS_UNUSED)
3966 struct ds ds = DS_EMPTY_INITIALIZER;
3967 const struct dpif_flow_stats *stats;
3968 const struct ofproto_dpif *ofproto;
3969 struct dpif_flow_dump flow_dump;
3970 const struct nlattr *actions;
3971 const struct nlattr *mask;
3972 const struct nlattr *key;
3977 ofproto = ofproto_dpif_lookup(argv[1]);
3979 unixctl_command_reply_error(conn, "no such bridge");
3984 dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
3985 while (dpif_flow_dump_next(&flow_dump, &key, &key_len, &mask, &mask_len,
3986 &actions, &actions_len, &stats)) {
3987 if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
3991 odp_flow_format(key, key_len, mask, mask_len, NULL, &ds, false);
3992 ds_put_cstr(&ds, ", ");
3993 dpif_flow_stats_format(stats, &ds);
3994 ds_put_cstr(&ds, ", actions:");
3995 format_odp_actions(&ds, actions, actions_len);
3996 ds_put_char(&ds, '\n');
3999 if (dpif_flow_dump_done(&flow_dump)) {
4001 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4002 unixctl_command_reply_error(conn, ds_cstr(&ds));
4004 unixctl_command_reply(conn, ds_cstr(&ds));
4010 ofproto_dpif_unixctl_init(void)
4012 static bool registered;
4018 unixctl_command_register(
4020 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4021 1, 3, ofproto_unixctl_trace, NULL);
4022 unixctl_command_register(
4023 "ofproto/trace-packet-out",
4024 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4025 2, 6, ofproto_unixctl_trace_actions, NULL);
4026 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4027 ofproto_unixctl_fdb_flush, NULL);
4028 unixctl_command_register("fdb/show", "bridge", 1, 1,
4029 ofproto_unixctl_fdb_show, NULL);
4030 unixctl_command_register("dpif/dump-dps", "", 0, 0,
4031 ofproto_unixctl_dpif_dump_dps, NULL);
4032 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4034 unixctl_command_register("dpif/dump-flows", "bridge", 1, 1,
4035 ofproto_unixctl_dpif_dump_flows, NULL);
4038 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4040 * This is deprecated. It is only for compatibility with broken device drivers
4041 * in old versions of Linux that do not properly support VLANs when VLAN
4042 * devices are not used. When broken device drivers are no longer in
4043 * widespread use, we will delete these interfaces. */
4046 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4048 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4049 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4051 if (realdev_ofp_port == ofport->realdev_ofp_port
4052 && vid == ofport->vlandev_vid) {
4056 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4058 if (ofport->realdev_ofp_port) {
4061 if (realdev_ofp_port && ofport->bundle) {
4062 /* vlandevs are enslaved to their realdevs, so they are not allowed to
4063 * themselves be part of a bundle. */
4064 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
4067 ofport->realdev_ofp_port = realdev_ofp_port;
4068 ofport->vlandev_vid = vid;
4070 if (realdev_ofp_port) {
4071 vsp_add(ofport, realdev_ofp_port, vid);
4078 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4080 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4084 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4085 OVS_EXCLUDED(ofproto->vsp_mutex)
4089 ovs_mutex_lock(&ofproto->vsp_mutex);
4090 ret = !hmap_is_empty(&ofproto->realdev_vid_map);
4091 ovs_mutex_unlock(&ofproto->vsp_mutex);
4096 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4097 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4098 OVS_REQUIRES(ofproto->vsp_mutex)
4100 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4101 int vid = vlan_tci_to_vid(vlan_tci);
4102 const struct vlan_splinter *vsp;
4104 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4105 hash_realdev_vid(realdev_ofp_port, vid),
4106 &ofproto->realdev_vid_map) {
4107 if (vsp->realdev_ofp_port == realdev_ofp_port
4108 && vsp->vid == vid) {
4109 return vsp->vlandev_ofp_port;
4113 return realdev_ofp_port;
4116 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4117 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4118 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
4119 * 'vlan_tci' 9, it would return the port number of eth0.9.
4121 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4122 * function just returns its 'realdev_ofp_port' argument. */
4124 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4125 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4126 OVS_EXCLUDED(ofproto->vsp_mutex)
4130 ovs_mutex_lock(&ofproto->vsp_mutex);
4131 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4132 ovs_mutex_unlock(&ofproto->vsp_mutex);
4136 static struct vlan_splinter *
4137 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4139 struct vlan_splinter *vsp;
4141 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4142 hash_ofp_port(vlandev_ofp_port),
4143 &ofproto->vlandev_map) {
4144 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4152 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4153 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4154 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
4155 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4156 * eth0 and store 9 in '*vid'.
4158 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4159 * VLAN device. Unless VLAN splinters are enabled, this is what this function
4162 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4163 ofp_port_t vlandev_ofp_port, int *vid)
4164 OVS_REQUIRES(ofproto->vsp_mutex)
4166 if (!hmap_is_empty(&ofproto->vlandev_map)) {
4167 const struct vlan_splinter *vsp;
4169 vsp = vlandev_find(ofproto, vlandev_ofp_port);
4174 return vsp->realdev_ofp_port;
4180 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4181 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
4182 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4183 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
4184 * always the case unless VLAN splinters are enabled), returns false without
4185 * making any changes. */
4187 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4188 OVS_EXCLUDED(ofproto->vsp_mutex)
4193 ovs_mutex_lock(&ofproto->vsp_mutex);
4194 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4195 ovs_mutex_unlock(&ofproto->vsp_mutex);
4200 /* Cause the flow to be processed as if it came in on the real device with
4201 * the VLAN device's VLAN ID. */
4202 flow->in_port.ofp_port = realdev;
4203 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4208 vsp_remove(struct ofport_dpif *port)
4210 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4211 struct vlan_splinter *vsp;
4213 ovs_mutex_lock(&ofproto->vsp_mutex);
4214 vsp = vlandev_find(ofproto, port->up.ofp_port);
4216 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4217 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4220 port->realdev_ofp_port = 0;
4222 VLOG_ERR("missing vlan device record");
4224 ovs_mutex_unlock(&ofproto->vsp_mutex);
4228 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4230 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4232 ovs_mutex_lock(&ofproto->vsp_mutex);
4233 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4234 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4235 == realdev_ofp_port)) {
4236 struct vlan_splinter *vsp;
4238 vsp = xmalloc(sizeof *vsp);
4239 vsp->realdev_ofp_port = realdev_ofp_port;
4240 vsp->vlandev_ofp_port = port->up.ofp_port;
4243 port->realdev_ofp_port = realdev_ofp_port;
4245 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4246 hash_ofp_port(port->up.ofp_port));
4247 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4248 hash_realdev_vid(realdev_ofp_port, vid));
4250 VLOG_ERR("duplicate vlan device record");
4252 ovs_mutex_unlock(&ofproto->vsp_mutex);
4256 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4258 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4259 return ofport ? ofport->odp_port : ODPP_NONE;
4262 struct ofport_dpif *
4263 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4265 struct ofport_dpif *port;
4267 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4268 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4269 &backer->odp_to_ofport_map) {
4270 if (port->odp_port == odp_port) {
4271 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4276 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4281 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4283 struct ofport_dpif *port;
4285 port = odp_port_to_ofport(ofproto->backer, odp_port);
4286 if (port && &ofproto->up == port->up.ofproto) {
4287 return port->up.ofp_port;
4293 const struct ofproto_class ofproto_dpif_class = {
4307 NULL, /* get_memory_usage. */
4308 type_get_memory_usage,
4327 port_is_lacp_current,
4328 NULL, /* rule_choose_table */
4337 rule_modify_actions,
4351 get_stp_port_status,
4359 is_mirror_output_bundle,
4360 forward_bpdu_changed,
4361 set_mac_table_config,
4363 NULL, /* meter_get_features */
4364 NULL, /* meter_set */
4365 NULL, /* meter_get */
4366 NULL, /* meter_del */
4367 group_alloc, /* group_alloc */
4368 group_construct, /* group_construct */
4369 group_destruct, /* group_destruct */
4370 group_dealloc, /* group_dealloc */
4371 group_modify, /* group_modify */
4372 group_get_stats, /* group_get_stats */