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 "mcast-snooping.h"
41 #include "meta-flow.h"
42 #include "multipath.h"
43 #include "netdev-vport.h"
48 #include "odp-execute.h"
51 #include "ofp-actions.h"
52 #include "ofp-parse.h"
53 #include "ofp-print.h"
54 #include "ofproto-dpif-ipfix.h"
55 #include "ofproto-dpif-mirror.h"
56 #include "ofproto-dpif-monitor.h"
57 #include "ofproto-dpif-rid.h"
58 #include "ofproto-dpif-sflow.h"
59 #include "ofproto-dpif-upcall.h"
60 #include "ofproto-dpif-xlate.h"
61 #include "poll-loop.h"
63 #include "ovs-router.h"
69 #include "unaligned.h"
71 #include "vlan-bitmap.h"
72 #include "openvswitch/vlog.h"
74 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
76 COVERAGE_DEFINE(ofproto_dpif_expired);
77 COVERAGE_DEFINE(packet_in_overflow);
86 * - Do include packets and bytes from datapath flows which have not
87 * recently been processed by a revalidator. */
88 struct ovs_mutex stats_mutex;
89 struct dpif_flow_stats stats OVS_GUARDED;
91 /* If non-zero then the recirculation id that has
92 * been allocated for use with this rule.
93 * The recirculation id and associated internal flow should
94 * be freed when the rule is freed */
98 /* RULE_CAST() depends on this. */
99 BUILD_ASSERT_DECL(offsetof(struct rule_dpif, up) == 0);
101 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
102 long long int *used);
103 static struct rule_dpif *rule_dpif_cast(const struct rule *);
104 static void rule_expire(struct rule_dpif *);
111 * - Do include packets and bytes from datapath flows which have not
112 * recently been processed by a revalidator. */
113 struct ovs_mutex stats_mutex;
114 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
115 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
119 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
120 struct ofproto_dpif *ofproto; /* Owning ofproto. */
121 void *aux; /* Key supplied by ofproto's client. */
122 char *name; /* Identifier for log messages. */
125 struct ovs_list ports; /* Contains "struct ofport"s. */
126 enum port_vlan_mode vlan_mode; /* VLAN mode */
127 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
128 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
129 * NULL if all VLANs are trunked. */
130 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
131 struct bond *bond; /* Nonnull iff more than one port. */
132 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
135 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
138 static void bundle_remove(struct ofport *);
139 static void bundle_update(struct ofbundle *);
140 static void bundle_destroy(struct ofbundle *);
141 static void bundle_del_port(struct ofport_dpif *);
142 static void bundle_run(struct ofbundle *);
143 static void bundle_wait(struct ofbundle *);
144 static void bundle_flush_macs(struct ofbundle *, bool);
145 static void bundle_move(struct ofbundle *, struct ofbundle *);
147 static void stp_run(struct ofproto_dpif *ofproto);
148 static void stp_wait(struct ofproto_dpif *ofproto);
149 static int set_stp_port(struct ofport *,
150 const struct ofproto_port_stp_settings *);
152 static void rstp_run(struct ofproto_dpif *ofproto);
153 static void set_rstp_port(struct ofport *,
154 const struct ofproto_port_rstp_settings *);
157 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
161 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
162 struct ovs_list bundle_node;/* In struct ofbundle's "ports" list. */
163 struct cfm *cfm; /* Connectivity Fault Management, if any. */
164 struct bfd *bfd; /* BFD, if any. */
165 bool may_enable; /* May be enabled in bonds. */
166 bool is_tunnel; /* This port is a tunnel. */
167 bool is_layer3; /* This is a layer 3 port. */
168 long long int carrier_seq; /* Carrier status changes. */
169 struct ofport_dpif *peer; /* Peer if patch port. */
172 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
173 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
174 long long int stp_state_entered;
176 /* Rapid Spanning Tree. */
177 struct rstp_port *rstp_port; /* Rapid Spanning Tree Protocol, if any. */
178 enum rstp_state rstp_state; /* Always RSTP_DISABLED if RSTP not in use. */
180 /* Queue to DSCP mapping. */
181 struct ofproto_port_queue *qdscp;
184 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
186 * This is deprecated. It is only for compatibility with broken device
187 * drivers in old versions of Linux that do not properly support VLANs when
188 * VLAN devices are not used. When broken device drivers are no longer in
189 * widespread use, we will delete these interfaces. */
190 ofp_port_t realdev_ofp_port;
194 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
196 * This is deprecated. It is only for compatibility with broken device drivers
197 * in old versions of Linux that do not properly support VLANs when VLAN
198 * devices are not used. When broken device drivers are no longer in
199 * widespread use, we will delete these interfaces. */
200 struct vlan_splinter {
201 struct hmap_node realdev_vid_node;
202 struct hmap_node vlandev_node;
203 ofp_port_t realdev_ofp_port;
204 ofp_port_t vlandev_ofp_port;
208 static void vsp_remove(struct ofport_dpif *);
209 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
211 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
214 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
217 static struct ofport_dpif *
218 ofport_dpif_cast(const struct ofport *ofport)
220 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
223 static void port_run(struct ofport_dpif *);
224 static int set_bfd(struct ofport *, const struct smap *);
225 static int set_cfm(struct ofport *, const struct cfm_settings *);
226 static void ofport_update_peer(struct ofport_dpif *);
228 /* Reasons that we might need to revalidate every datapath flow, and
229 * corresponding coverage counters.
231 * A value of 0 means that there is no need to revalidate.
233 * It would be nice to have some cleaner way to integrate with coverage
234 * counters, but with only a few reasons I guess this is good enough for
236 enum revalidate_reason {
237 REV_RECONFIGURE = 1, /* Switch configuration changed. */
238 REV_STP, /* Spanning tree protocol port status change. */
239 REV_RSTP, /* RSTP port status change. */
240 REV_BOND, /* Bonding changed. */
241 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
242 REV_FLOW_TABLE, /* Flow table changed. */
243 REV_MAC_LEARNING, /* Mac learning changed. */
244 REV_MCAST_SNOOPING, /* Multicast snooping changed. */
246 COVERAGE_DEFINE(rev_reconfigure);
247 COVERAGE_DEFINE(rev_stp);
248 COVERAGE_DEFINE(rev_rstp);
249 COVERAGE_DEFINE(rev_bond);
250 COVERAGE_DEFINE(rev_port_toggled);
251 COVERAGE_DEFINE(rev_flow_table);
252 COVERAGE_DEFINE(rev_mac_learning);
253 COVERAGE_DEFINE(rev_mcast_snooping);
255 /* Stores mapping between 'recirc_id' and 'ofproto-dpif'. */
256 struct dpif_backer_recirc_node {
257 struct cmap_node cmap_node;
258 struct ofproto_dpif *ofproto;
262 /* All datapaths of a given type share a single dpif backer instance. */
269 struct ovs_rwlock odp_to_ofport_lock;
270 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
272 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
274 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
276 bool recv_set_enable; /* Enables or disables receiving packets. */
279 struct recirc_id_pool *rid_pool; /* Recirculation ID pool. */
280 struct cmap recirc_map; /* Map of 'recirc_id's to 'ofproto's. */
281 struct ovs_mutex recirc_mutex; /* Protects 'recirc_map'. */
282 bool enable_recirc; /* True if the datapath supports recirculation */
284 /* True if the datapath supports unique flow identifiers */
287 /* True if the datapath supports variable-length
288 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
289 * False if the datapath supports only 8-byte (or shorter) userdata. */
290 bool variable_length_userdata;
292 /* True if the datapath supports masked data in OVS_ACTION_ATTR_SET
294 bool masked_set_action;
296 /* Maximum number of MPLS label stack entries that the datapath supports
298 size_t max_mpls_depth;
300 /* Version string of the datapath stored in OVSDB. */
301 char *dp_version_string;
303 /* True if the datapath supports tnl_push and pop actions. */
304 bool enable_tnl_push_pop;
305 struct atomic_count tnl_count;
308 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
309 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
311 struct ofproto_dpif {
312 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
314 struct dpif_backer *backer;
316 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
318 /* Special OpenFlow rules. */
319 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
320 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
321 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
324 struct netflow *netflow;
325 struct dpif_sflow *sflow;
326 struct dpif_ipfix *ipfix;
327 struct hmap bundles; /* Contains "struct ofbundle"s. */
328 struct mac_learning *ml;
329 struct mcast_snooping *ms;
330 bool has_bonded_bundles;
332 struct mbridge *mbridge;
334 struct ovs_mutex stats_mutex;
335 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
336 * consumed in userspace. */
340 long long int stp_last_tick;
342 /* Rapid Spanning Tree. */
344 long long int rstp_last_tick;
346 /* VLAN splinters. */
347 struct ovs_mutex vsp_mutex;
348 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
349 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
352 struct sset ports; /* Set of standard port names. */
353 struct sset ghost_ports; /* Ports with no datapath port. */
354 struct sset port_poll_set; /* Queued names for port_poll() reply. */
355 int port_poll_errno; /* Last errno for port_poll() reply. */
356 uint64_t change_seq; /* Connectivity status changes. */
359 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
360 struct seq *pins_seq; /* For notifying 'pins' reception. */
364 /* All existing ofproto_dpif instances, indexed by ->up.name. */
365 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
367 static bool ofproto_use_tnl_push_pop = true;
368 static void ofproto_unixctl_init(void);
370 static inline struct ofproto_dpif *
371 ofproto_dpif_cast(const struct ofproto *ofproto)
373 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
374 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
378 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
380 return ofproto->backer->max_mpls_depth;
384 ofproto_dpif_get_enable_recirc(const struct ofproto_dpif *ofproto)
386 return ofproto->backer->enable_recirc;
390 ofproto_dpif_get_enable_ufid(struct dpif_backer *backer)
392 return backer->enable_ufid;
395 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
396 ofp_port_t ofp_port);
397 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
398 const struct ofpbuf *packet,
399 const struct ofpact[], size_t ofpacts_len,
402 /* Global variables. */
403 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
405 /* Initial mappings of port to bridge mappings. */
406 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
408 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
411 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
412 struct ofputil_flow_mod *fm)
414 ofproto_flow_mod(&ofproto->up, fm);
417 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
418 * Takes ownership of 'pin' and pin->packet. */
420 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
421 struct ofproto_packet_in *pin)
423 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
424 COVERAGE_INC(packet_in_overflow);
425 free(CONST_CAST(void *, pin->up.packet));
429 /* Wakes up main thread for packet-in I/O. */
430 seq_change(ofproto->pins_seq);
433 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
434 * packet rather than to send the packet to the controller.
436 * This function returns false to indicate that a packet_in message
437 * for a "table-miss" should be sent to at least one controller.
438 * False otherwise. */
440 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
442 return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
445 /* Factory functions. */
448 init(const struct shash *iface_hints)
450 struct shash_node *node;
452 /* Make a local copy, since we don't own 'iface_hints' elements. */
453 SHASH_FOR_EACH(node, iface_hints) {
454 const struct iface_hint *orig_hint = node->data;
455 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
457 new_hint->br_name = xstrdup(orig_hint->br_name);
458 new_hint->br_type = xstrdup(orig_hint->br_type);
459 new_hint->ofp_port = orig_hint->ofp_port;
461 shash_add(&init_ofp_ports, node->name, new_hint);
466 enumerate_types(struct sset *types)
468 dp_enumerate_types(types);
472 enumerate_names(const char *type, struct sset *names)
474 struct ofproto_dpif *ofproto;
477 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
478 if (strcmp(type, ofproto->up.type)) {
481 sset_add(names, ofproto->up.name);
488 del(const char *type, const char *name)
493 error = dpif_open(name, type, &dpif);
495 error = dpif_delete(dpif);
502 port_open_type(const char *datapath_type, const char *port_type)
504 return dpif_port_open_type(datapath_type, port_type);
507 /* Type functions. */
509 static void process_dpif_port_changes(struct dpif_backer *);
510 static void process_dpif_all_ports_changed(struct dpif_backer *);
511 static void process_dpif_port_change(struct dpif_backer *,
512 const char *devname);
513 static void process_dpif_port_error(struct dpif_backer *, int error);
515 static struct ofproto_dpif *
516 lookup_ofproto_dpif_by_port_name(const char *name)
518 struct ofproto_dpif *ofproto;
520 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
521 if (sset_contains(&ofproto->ports, name)) {
530 type_run(const char *type)
532 struct dpif_backer *backer;
534 backer = shash_find_data(&all_dpif_backers, type);
536 /* This is not necessarily a problem, since backers are only
537 * created on demand. */
542 if (dpif_run(backer->dpif)) {
543 backer->need_revalidate = REV_RECONFIGURE;
546 udpif_run(backer->udpif);
548 /* If vswitchd started with other_config:flow_restore_wait set as "true",
549 * and the configuration has now changed to "false", enable receiving
550 * packets from the datapath. */
551 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
554 backer->recv_set_enable = true;
556 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
558 VLOG_ERR("Failed to enable receiving packets in dpif.");
561 dpif_flow_flush(backer->dpif);
562 backer->need_revalidate = REV_RECONFIGURE;
565 if (backer->recv_set_enable) {
566 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
569 dpif_poll_threads_set(backer->dpif, n_dpdk_rxqs, pmd_cpu_mask);
571 if (backer->need_revalidate) {
572 struct ofproto_dpif *ofproto;
573 struct simap_node *node;
574 struct simap tmp_backers;
576 /* Handle tunnel garbage collection. */
577 simap_init(&tmp_backers);
578 simap_swap(&backer->tnl_backers, &tmp_backers);
580 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
581 struct ofport_dpif *iter;
583 if (backer != ofproto->backer) {
587 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
588 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
591 if (!iter->is_tunnel) {
595 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
596 namebuf, sizeof namebuf);
597 node = simap_find(&tmp_backers, dp_port);
599 simap_put(&backer->tnl_backers, dp_port, node->data);
600 simap_delete(&tmp_backers, node);
601 node = simap_find(&backer->tnl_backers, dp_port);
603 node = simap_find(&backer->tnl_backers, dp_port);
605 odp_port_t odp_port = ODPP_NONE;
607 if (!dpif_port_add(backer->dpif, iter->up.netdev,
609 simap_put(&backer->tnl_backers, dp_port,
610 odp_to_u32(odp_port));
611 node = simap_find(&backer->tnl_backers, dp_port);
616 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
617 if (tnl_port_reconfigure(iter, iter->up.netdev,
619 ovs_native_tunneling_is_on(ofproto), dp_port)) {
620 backer->need_revalidate = REV_RECONFIGURE;
625 SIMAP_FOR_EACH (node, &tmp_backers) {
626 dpif_port_del(backer->dpif, u32_to_odp(node->data));
628 simap_destroy(&tmp_backers);
630 switch (backer->need_revalidate) {
631 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
632 case REV_STP: COVERAGE_INC(rev_stp); break;
633 case REV_RSTP: COVERAGE_INC(rev_rstp); break;
634 case REV_BOND: COVERAGE_INC(rev_bond); break;
635 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
636 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
637 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
638 case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
640 backer->need_revalidate = 0;
642 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
643 struct ofport_dpif *ofport;
644 struct ofbundle *bundle;
646 if (ofproto->backer != backer) {
651 xlate_ofproto_set(ofproto, ofproto->up.name,
652 ofproto->backer->dpif, ofproto->ml,
653 ofproto->stp, ofproto->rstp, ofproto->ms,
654 ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
656 ofproto->up.forward_bpdu,
657 connmgr_has_in_band(ofproto->up.connmgr),
658 ofproto->backer->enable_recirc,
659 ofproto->backer->variable_length_userdata,
660 ofproto->backer->max_mpls_depth,
661 ofproto->backer->masked_set_action);
663 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
664 xlate_bundle_set(ofproto, bundle, bundle->name,
665 bundle->vlan_mode, bundle->vlan,
666 bundle->trunks, bundle->use_priority_tags,
667 bundle->bond, bundle->lacp,
671 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
672 int stp_port = ofport->stp_port
673 ? stp_port_no(ofport->stp_port)
675 xlate_ofport_set(ofproto, ofport->bundle, ofport,
676 ofport->up.ofp_port, ofport->odp_port,
677 ofport->up.netdev, ofport->cfm,
678 ofport->bfd, ofport->peer, stp_port,
679 ofport->rstp_port, ofport->qdscp,
680 ofport->n_qdscp, ofport->up.pp.config,
681 ofport->up.pp.state, ofport->is_tunnel,
687 udpif_revalidate(backer->udpif);
690 process_dpif_port_changes(backer);
695 /* Check for and handle port changes in 'backer''s dpif. */
697 process_dpif_port_changes(struct dpif_backer *backer)
703 error = dpif_port_poll(backer->dpif, &devname);
709 process_dpif_all_ports_changed(backer);
713 process_dpif_port_change(backer, devname);
718 process_dpif_port_error(backer, error);
725 process_dpif_all_ports_changed(struct dpif_backer *backer)
727 struct ofproto_dpif *ofproto;
728 struct dpif_port dpif_port;
729 struct dpif_port_dump dump;
730 struct sset devnames;
733 sset_init(&devnames);
734 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
735 if (ofproto->backer == backer) {
736 struct ofport *ofport;
738 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
739 sset_add(&devnames, netdev_get_name(ofport->netdev));
743 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
744 sset_add(&devnames, dpif_port.name);
747 SSET_FOR_EACH (devname, &devnames) {
748 process_dpif_port_change(backer, devname);
750 sset_destroy(&devnames);
754 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
756 struct ofproto_dpif *ofproto;
757 struct dpif_port port;
759 /* Don't report on the datapath's device. */
760 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
764 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
765 &all_ofproto_dpifs) {
766 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
771 ofproto = lookup_ofproto_dpif_by_port_name(devname);
772 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
773 /* The port was removed. If we know the datapath,
774 * report it through poll_set(). If we don't, it may be
775 * notifying us of a removal we initiated, so ignore it.
776 * If there's a pending ENOBUFS, let it stand, since
777 * everything will be reevaluated. */
778 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
779 sset_add(&ofproto->port_poll_set, devname);
780 ofproto->port_poll_errno = 0;
782 } else if (!ofproto) {
783 /* The port was added, but we don't know with which
784 * ofproto we should associate it. Delete it. */
785 dpif_port_del(backer->dpif, port.port_no);
787 struct ofport_dpif *ofport;
789 ofport = ofport_dpif_cast(shash_find_data(
790 &ofproto->up.port_by_name, devname));
792 && ofport->odp_port != port.port_no
793 && !odp_port_to_ofport(backer, port.port_no))
795 /* 'ofport''s datapath port number has changed from
796 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
797 * structures to match. */
798 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
799 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
800 ofport->odp_port = port.port_no;
801 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
802 hash_odp_port(port.port_no));
803 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
804 backer->need_revalidate = REV_RECONFIGURE;
807 dpif_port_destroy(&port);
810 /* Propagate 'error' to all ofprotos based on 'backer'. */
812 process_dpif_port_error(struct dpif_backer *backer, int error)
814 struct ofproto_dpif *ofproto;
816 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
817 if (ofproto->backer == backer) {
818 sset_clear(&ofproto->port_poll_set);
819 ofproto->port_poll_errno = error;
825 type_wait(const char *type)
827 struct dpif_backer *backer;
829 backer = shash_find_data(&all_dpif_backers, type);
831 /* This is not necessarily a problem, since backers are only
832 * created on demand. */
836 dpif_wait(backer->dpif);
839 /* Basic life-cycle. */
841 static int add_internal_flows(struct ofproto_dpif *);
843 static struct ofproto *
846 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
851 dealloc(struct ofproto *ofproto_)
853 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
857 /* Called when 'ofproto' is destructed. Checks for and clears any
860 dpif_backer_recirc_clear_ofproto(struct dpif_backer *backer,
861 struct ofproto_dpif *ofproto)
863 struct dpif_backer_recirc_node *node;
865 ovs_mutex_lock(&backer->recirc_mutex);
866 CMAP_FOR_EACH (node, cmap_node, &backer->recirc_map) {
867 if (node->ofproto == ofproto) {
868 VLOG_ERR("recirc_id %"PRIu32", not freed when ofproto (%s) "
869 "is destructed", node->recirc_id, ofproto->up.name);
870 cmap_remove(&backer->recirc_map, &node->cmap_node,
872 ovsrcu_postpone(free, node);
875 ovs_mutex_unlock(&backer->recirc_mutex);
879 close_dpif_backer(struct dpif_backer *backer)
881 ovs_assert(backer->refcount > 0);
883 if (--backer->refcount) {
887 udpif_destroy(backer->udpif);
889 simap_destroy(&backer->tnl_backers);
890 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
891 hmap_destroy(&backer->odp_to_ofport_map);
892 shash_find_and_delete(&all_dpif_backers, backer->type);
893 recirc_id_pool_destroy(backer->rid_pool);
894 cmap_destroy(&backer->recirc_map);
895 ovs_mutex_destroy(&backer->recirc_mutex);
897 free(backer->dp_version_string);
898 dpif_close(backer->dpif);
902 /* Datapath port slated for removal from datapath. */
904 struct ovs_list list_node;
908 static bool check_variable_length_userdata(struct dpif_backer *backer);
909 static size_t check_max_mpls_depth(struct dpif_backer *backer);
910 static bool check_recirc(struct dpif_backer *backer);
911 static bool check_ufid(struct dpif_backer *backer);
912 static bool check_masked_set_action(struct dpif_backer *backer);
915 open_dpif_backer(const char *type, struct dpif_backer **backerp)
917 struct dpif_backer *backer;
918 struct dpif_port_dump port_dump;
919 struct dpif_port port;
920 struct shash_node *node;
921 struct ovs_list garbage_list;
922 struct odp_garbage *garbage, *next;
929 backer = shash_find_data(&all_dpif_backers, type);
936 backer_name = xasprintf("ovs-%s", type);
938 /* Remove any existing datapaths, since we assume we're the only
939 * userspace controlling the datapath. */
941 dp_enumerate_names(type, &names);
942 SSET_FOR_EACH(name, &names) {
943 struct dpif *old_dpif;
945 /* Don't remove our backer if it exists. */
946 if (!strcmp(name, backer_name)) {
950 if (dpif_open(name, type, &old_dpif)) {
951 VLOG_WARN("couldn't open old datapath %s to remove it", name);
953 dpif_delete(old_dpif);
954 dpif_close(old_dpif);
957 sset_destroy(&names);
959 backer = xmalloc(sizeof *backer);
961 error = dpif_create_and_open(backer_name, type, &backer->dpif);
964 VLOG_ERR("failed to open datapath of type %s: %s", type,
965 ovs_strerror(error));
969 backer->udpif = udpif_create(backer, backer->dpif);
971 backer->type = xstrdup(type);
972 backer->refcount = 1;
973 hmap_init(&backer->odp_to_ofport_map);
974 ovs_rwlock_init(&backer->odp_to_ofport_lock);
975 backer->need_revalidate = 0;
976 simap_init(&backer->tnl_backers);
977 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
980 if (backer->recv_set_enable) {
981 dpif_flow_flush(backer->dpif);
984 /* Loop through the ports already on the datapath and remove any
985 * that we don't need anymore. */
986 list_init(&garbage_list);
987 dpif_port_dump_start(&port_dump, backer->dpif);
988 while (dpif_port_dump_next(&port_dump, &port)) {
989 node = shash_find(&init_ofp_ports, port.name);
990 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
991 garbage = xmalloc(sizeof *garbage);
992 garbage->odp_port = port.port_no;
993 list_push_front(&garbage_list, &garbage->list_node);
996 dpif_port_dump_done(&port_dump);
998 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
999 dpif_port_del(backer->dpif, garbage->odp_port);
1000 list_remove(&garbage->list_node);
1004 shash_add(&all_dpif_backers, type, backer);
1006 backer->enable_recirc = check_recirc(backer);
1007 backer->max_mpls_depth = check_max_mpls_depth(backer);
1008 backer->masked_set_action = check_masked_set_action(backer);
1009 backer->enable_ufid = check_ufid(backer);
1010 backer->rid_pool = recirc_id_pool_create();
1011 ovs_mutex_init(&backer->recirc_mutex);
1012 cmap_init(&backer->recirc_map);
1014 backer->enable_tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
1015 atomic_count_init(&backer->tnl_count, 0);
1017 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
1019 VLOG_ERR("failed to listen on datapath of type %s: %s",
1020 type, ovs_strerror(error));
1021 close_dpif_backer(backer);
1025 if (backer->recv_set_enable) {
1026 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
1029 /* This check fails if performed before udpif threads have been set,
1030 * as the kernel module checks that the 'pid' in userspace action
1032 backer->variable_length_userdata = check_variable_length_userdata(backer);
1033 backer->dp_version_string = dpif_get_dp_version(backer->dpif);
1039 ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
1041 return ofproto_use_tnl_push_pop && ofproto->backer->enable_tnl_push_pop &&
1042 atomic_count_get(&ofproto->backer->tnl_count);
1045 /* Tests whether 'backer''s datapath supports recirculation. Only newer
1046 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
1047 * features on older datapaths that don't support this feature.
1049 * Returns false if 'backer' definitely does not support recirculation, true if
1050 * it seems to support recirculation or if at least the error we get is
1053 check_recirc(struct dpif_backer *backer)
1056 struct odputil_keybuf keybuf;
1060 memset(&flow, 0, sizeof flow);
1064 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1065 odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
1066 enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
1069 if (enable_recirc) {
1070 VLOG_INFO("%s: Datapath supports recirculation",
1071 dpif_name(backer->dpif));
1073 VLOG_INFO("%s: Datapath does not support recirculation",
1074 dpif_name(backer->dpif));
1077 return enable_recirc;
1080 /* Tests whether 'dpif' supports userspace flow ids. We can skip serializing
1081 * some flow attributes for datapaths that support this feature.
1083 * Returns true if 'dpif' supports UFID for flow operations.
1084 * Returns false if 'dpif' does not support UFID. */
1086 check_ufid(struct dpif_backer *backer)
1089 struct odputil_keybuf keybuf;
1094 memset(&flow, 0, sizeof flow);
1095 flow.dl_type = htons(0x1234);
1097 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1098 odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
1099 dpif_flow_hash(backer->dpif, ofpbuf_data(&key), ofpbuf_size(&key), &ufid);
1101 enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, &ufid);
1104 VLOG_INFO("%s: Datapath supports userspace flow ids",
1105 dpif_name(backer->dpif));
1107 VLOG_INFO("%s: Datapath does not support userspace flow ids",
1108 dpif_name(backer->dpif));
1113 /* Tests whether 'backer''s datapath supports variable-length
1114 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1115 * to disable some features on older datapaths that don't support this
1118 * Returns false if 'backer' definitely does not support variable-length
1119 * userdata, true if it seems to support them or if at least the error we get
1122 check_variable_length_userdata(struct dpif_backer *backer)
1124 struct eth_header *eth;
1125 struct ofpbuf actions;
1126 struct dpif_execute execute;
1127 struct ofpbuf packet;
1131 /* Compose a userspace action that will cause an ERANGE error on older
1132 * datapaths that don't support variable-length userdata.
1134 * We really test for using userdata longer than 8 bytes, but older
1135 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1136 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1137 * test for that instead as a proxy for longer userdata support. */
1138 ofpbuf_init(&actions, 64);
1139 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1140 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1141 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1142 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1143 nl_msg_end_nested(&actions, start);
1145 /* Compose a dummy ethernet packet. */
1146 ofpbuf_init(&packet, ETH_HEADER_LEN);
1147 eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
1148 eth->eth_type = htons(0x1234);
1150 /* Execute the actions. On older datapaths this fails with ERANGE, on
1151 * newer datapaths it succeeds. */
1152 execute.actions = ofpbuf_data(&actions);
1153 execute.actions_len = ofpbuf_size(&actions);
1154 execute.packet = &packet;
1155 execute.md = PKT_METADATA_INITIALIZER(0);
1156 execute.needs_help = false;
1157 execute.probe = true;
1159 error = dpif_execute(backer->dpif, &execute);
1161 ofpbuf_uninit(&packet);
1162 ofpbuf_uninit(&actions);
1169 /* Variable-length userdata is not supported. */
1170 VLOG_WARN("%s: datapath does not support variable-length userdata "
1171 "feature (needs Linux 3.10+ or kernel module from OVS "
1172 "1..11+). The NXAST_SAMPLE action will be ignored.",
1173 dpif_name(backer->dpif));
1177 /* Something odd happened. We're not sure whether variable-length
1178 * userdata is supported. Default to "yes". */
1179 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1180 dpif_name(backer->dpif), ovs_strerror(error));
1185 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1187 * Returns the number of elements in a struct flow's mpls_lse field
1188 * if the datapath supports at least that many entries in an
1190 * Otherwise returns the number of MPLS push actions supported by
1193 check_max_mpls_depth(struct dpif_backer *backer)
1198 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1199 struct odputil_keybuf keybuf;
1202 memset(&flow, 0, sizeof flow);
1203 flow.dl_type = htons(ETH_TYPE_MPLS);
1204 flow_set_mpls_bos(&flow, n, 1);
1206 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1207 odp_flow_key_from_flow(&key, &flow, NULL, 0, false);
1208 if (!dpif_probe_feature(backer->dpif, "MPLS", &key, NULL)) {
1213 VLOG_INFO("%s: MPLS label stack length probed as %d",
1214 dpif_name(backer->dpif), n);
1218 /* Tests whether 'backer''s datapath supports masked data in
1219 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1220 * datapaths that don't support this feature. */
1222 check_masked_set_action(struct dpif_backer *backer)
1224 struct eth_header *eth;
1225 struct ofpbuf actions;
1226 struct dpif_execute execute;
1227 struct ofpbuf packet;
1229 struct ovs_key_ethernet key, mask;
1231 /* Compose a set action that will cause an EINVAL error on older
1232 * datapaths that don't support masked set actions.
1233 * Avoid using a full mask, as it could be translated to a non-masked
1234 * set action instead. */
1235 ofpbuf_init(&actions, 64);
1236 memset(&key, 0x53, sizeof key);
1237 memset(&mask, 0x7f, sizeof mask);
1238 commit_masked_set_action(&actions, OVS_KEY_ATTR_ETHERNET, &key, &mask,
1241 /* Compose a dummy ethernet packet. */
1242 ofpbuf_init(&packet, ETH_HEADER_LEN);
1243 eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
1244 eth->eth_type = htons(0x1234);
1246 /* Execute the actions. On older datapaths this fails with EINVAL, on
1247 * newer datapaths it succeeds. */
1248 execute.actions = ofpbuf_data(&actions);
1249 execute.actions_len = ofpbuf_size(&actions);
1250 execute.packet = &packet;
1251 execute.md = PKT_METADATA_INITIALIZER(0);
1252 execute.needs_help = false;
1253 execute.probe = true;
1255 error = dpif_execute(backer->dpif, &execute);
1257 ofpbuf_uninit(&packet);
1258 ofpbuf_uninit(&actions);
1261 /* Masked set action is not supported. */
1262 VLOG_INFO("%s: datapath does not support masked set action feature.",
1263 dpif_name(backer->dpif));
1269 construct(struct ofproto *ofproto_)
1271 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1272 struct shash_node *node, *next;
1275 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1280 ofproto->netflow = NULL;
1281 ofproto->sflow = NULL;
1282 ofproto->ipfix = NULL;
1283 ofproto->stp = NULL;
1284 ofproto->rstp = NULL;
1285 ofproto->dump_seq = 0;
1286 hmap_init(&ofproto->bundles);
1287 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1289 ofproto->mbridge = mbridge_create();
1290 ofproto->has_bonded_bundles = false;
1291 ofproto->lacp_enabled = false;
1292 ofproto_tunnel_init();
1293 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1294 ovs_mutex_init(&ofproto->vsp_mutex);
1296 guarded_list_init(&ofproto->pins);
1298 ofproto_unixctl_init();
1300 hmap_init(&ofproto->vlandev_map);
1301 hmap_init(&ofproto->realdev_vid_map);
1303 sset_init(&ofproto->ports);
1304 sset_init(&ofproto->ghost_ports);
1305 sset_init(&ofproto->port_poll_set);
1306 ofproto->port_poll_errno = 0;
1307 ofproto->change_seq = 0;
1308 ofproto->pins_seq = seq_create();
1309 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1312 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1313 struct iface_hint *iface_hint = node->data;
1315 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1316 /* Check if the datapath already has this port. */
1317 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1318 sset_add(&ofproto->ports, node->name);
1321 free(iface_hint->br_name);
1322 free(iface_hint->br_type);
1324 shash_delete(&init_ofp_ports, node);
1328 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1329 hash_string(ofproto->up.name, 0));
1330 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1332 ofproto_init_tables(ofproto_, N_TABLES);
1333 error = add_internal_flows(ofproto);
1335 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1341 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1342 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1348 match_init_catchall(&match);
1349 match_set_reg(&match, 0, id);
1351 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1353 *rulep = error ? NULL : rule_dpif_cast(rule);
1359 add_internal_flows(struct ofproto_dpif *ofproto)
1361 struct ofpact_controller *controller;
1362 uint64_t ofpacts_stub[128 / 8];
1363 struct ofpbuf ofpacts;
1364 struct rule *unused_rulep OVS_UNUSED;
1365 struct ofpact_resubmit *resubmit;
1370 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1373 controller = ofpact_put_CONTROLLER(&ofpacts);
1374 controller->max_len = UINT16_MAX;
1375 controller->controller_id = 0;
1376 controller->reason = OFPR_NO_MATCH;
1377 ofpact_pad(&ofpacts);
1379 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1380 &ofproto->miss_rule);
1385 ofpbuf_clear(&ofpacts);
1386 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1387 &ofproto->no_packet_in_rule);
1392 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1393 &ofproto->drop_frags_rule);
1398 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1399 * zero when reaching this rule.
1401 * (priority=2), recirc_id=0, actions=drop
1403 ofpbuf_clear(&ofpacts);
1404 match_init_catchall(&match);
1405 match_set_recirc_id(&match, 0);
1406 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1412 /* Continue rule lookups for not-matched recirc rules from table 0.
1414 * (priority=1), actions=resubmit(, 0)
1416 resubmit = ofpact_put_RESUBMIT(&ofpacts);
1417 resubmit->in_port = OFPP_IN_PORT;
1418 resubmit->table_id = 0;
1420 match_init_catchall(&match);
1421 error = ofproto_dpif_add_internal_flow(ofproto, &match, 1, 0, &ofpacts,
1428 destruct(struct ofproto *ofproto_)
1430 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1431 struct ofproto_packet_in *pin, *next_pin;
1432 struct rule_dpif *rule;
1433 struct oftable *table;
1434 struct ovs_list pins;
1436 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1438 xlate_remove_ofproto(ofproto);
1441 /* Ensure that the upcall processing threads have no remaining references
1442 * to the ofproto or anything in it. */
1443 udpif_synchronize(ofproto->backer->udpif);
1445 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1447 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1448 CLS_FOR_EACH (rule, up.cr, &table->cls) {
1449 ofproto_rule_delete(&ofproto->up, &rule->up);
1453 guarded_list_pop_all(&ofproto->pins, &pins);
1454 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1455 list_remove(&pin->list_node);
1456 free(CONST_CAST(void *, pin->up.packet));
1459 guarded_list_destroy(&ofproto->pins);
1461 dpif_backer_recirc_clear_ofproto(ofproto->backer, ofproto);
1463 mbridge_unref(ofproto->mbridge);
1465 netflow_unref(ofproto->netflow);
1466 dpif_sflow_unref(ofproto->sflow);
1467 dpif_ipfix_unref(ofproto->ipfix);
1468 hmap_destroy(&ofproto->bundles);
1469 mac_learning_unref(ofproto->ml);
1470 mcast_snooping_unref(ofproto->ms);
1472 hmap_destroy(&ofproto->vlandev_map);
1473 hmap_destroy(&ofproto->realdev_vid_map);
1475 sset_destroy(&ofproto->ports);
1476 sset_destroy(&ofproto->ghost_ports);
1477 sset_destroy(&ofproto->port_poll_set);
1479 ovs_mutex_destroy(&ofproto->stats_mutex);
1480 ovs_mutex_destroy(&ofproto->vsp_mutex);
1482 seq_destroy(ofproto->pins_seq);
1484 close_dpif_backer(ofproto->backer);
1488 run(struct ofproto *ofproto_)
1490 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1491 uint64_t new_seq, new_dump_seq;
1493 if (mbridge_need_revalidate(ofproto->mbridge)) {
1494 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1495 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1496 mac_learning_flush(ofproto->ml);
1497 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1498 mcast_snooping_mdb_flush(ofproto->ms);
1501 /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1502 * flow restore. Even though nothing is processed during flow restore,
1503 * all queued 'pins' will be handled immediately when flow restore
1505 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1507 /* Do not perform any periodic activity required by 'ofproto' while
1508 * waiting for flow restore to complete. */
1509 if (!ofproto_get_flow_restore_wait()) {
1510 struct ofproto_packet_in *pin, *next_pin;
1511 struct ovs_list pins;
1513 guarded_list_pop_all(&ofproto->pins, &pins);
1514 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1515 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1516 list_remove(&pin->list_node);
1517 free(CONST_CAST(void *, pin->up.packet));
1522 if (ofproto->netflow) {
1523 netflow_run(ofproto->netflow);
1525 if (ofproto->sflow) {
1526 dpif_sflow_run(ofproto->sflow);
1528 if (ofproto->ipfix) {
1529 dpif_ipfix_run(ofproto->ipfix);
1532 new_seq = seq_read(connectivity_seq_get());
1533 if (ofproto->change_seq != new_seq) {
1534 struct ofport_dpif *ofport;
1536 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1540 ofproto->change_seq = new_seq;
1542 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1543 struct ofbundle *bundle;
1545 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1552 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1553 if (mac_learning_run(ofproto->ml)) {
1554 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1556 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1558 if (mcast_snooping_run(ofproto->ms)) {
1559 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1562 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1563 if (ofproto->dump_seq != new_dump_seq) {
1564 struct rule *rule, *next_rule;
1566 /* We know stats are relatively fresh, so now is a good time to do some
1568 ofproto->dump_seq = new_dump_seq;
1570 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1572 ovs_mutex_lock(&ofproto_mutex);
1573 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1574 &ofproto->up.expirable) {
1575 rule_expire(rule_dpif_cast(rule));
1577 ovs_mutex_unlock(&ofproto_mutex);
1579 /* All outstanding data in existing flows has been accounted, so it's a
1580 * good time to do bond rebalancing. */
1581 if (ofproto->has_bonded_bundles) {
1582 struct ofbundle *bundle;
1584 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1586 bond_rebalance(bundle->bond);
1595 wait(struct ofproto *ofproto_)
1597 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1599 if (ofproto_get_flow_restore_wait()) {
1603 if (ofproto->sflow) {
1604 dpif_sflow_wait(ofproto->sflow);
1606 if (ofproto->ipfix) {
1607 dpif_ipfix_wait(ofproto->ipfix);
1609 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1610 struct ofbundle *bundle;
1612 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1613 bundle_wait(bundle);
1616 if (ofproto->netflow) {
1617 netflow_wait(ofproto->netflow);
1619 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1620 mac_learning_wait(ofproto->ml);
1621 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1622 mcast_snooping_wait(ofproto->ms);
1624 if (ofproto->backer->need_revalidate) {
1625 /* Shouldn't happen, but if it does just go around again. */
1626 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1627 poll_immediate_wake();
1630 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1631 seq_wait(ofproto->pins_seq, ofproto->pins_seqno);
1635 type_get_memory_usage(const char *type, struct simap *usage)
1637 struct dpif_backer *backer;
1639 backer = shash_find_data(&all_dpif_backers, type);
1641 udpif_get_memory_usage(backer->udpif, usage);
1646 flush(struct ofproto *ofproto_)
1648 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1649 struct dpif_backer *backer = ofproto->backer;
1652 udpif_flush(backer->udpif);
1657 query_tables(struct ofproto *ofproto,
1658 struct ofputil_table_features *features,
1659 struct ofputil_table_stats *stats)
1661 strcpy(features->name, "classifier");
1666 for (i = 0; i < ofproto->n_tables; i++) {
1667 unsigned long missed, matched;
1669 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1670 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1672 stats[i].matched_count = matched;
1673 stats[i].lookup_count = matched + missed;
1678 static struct ofport *
1681 struct ofport_dpif *port = xmalloc(sizeof *port);
1686 port_dealloc(struct ofport *port_)
1688 struct ofport_dpif *port = ofport_dpif_cast(port_);
1693 port_construct(struct ofport *port_)
1695 struct ofport_dpif *port = ofport_dpif_cast(port_);
1696 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1697 const struct netdev *netdev = port->up.netdev;
1698 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1699 struct dpif_port dpif_port;
1702 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1703 port->bundle = NULL;
1706 port->may_enable = false;
1707 port->stp_port = NULL;
1708 port->stp_state = STP_DISABLED;
1709 port->rstp_port = NULL;
1710 port->rstp_state = RSTP_DISABLED;
1711 port->is_tunnel = false;
1715 port->realdev_ofp_port = 0;
1716 port->vlandev_vid = 0;
1717 port->carrier_seq = netdev_get_carrier_resets(netdev);
1718 port->is_layer3 = netdev_vport_is_layer3(netdev);
1720 if (netdev_vport_is_patch(netdev)) {
1721 /* By bailing out here, we don't submit the port to the sFlow module
1722 * to be considered for counter polling export. This is correct
1723 * because the patch port represents an interface that sFlow considers
1724 * to be "internal" to the switch as a whole, and therefore not an
1725 * candidate for counter polling. */
1726 port->odp_port = ODPP_NONE;
1727 ofport_update_peer(port);
1731 error = dpif_port_query_by_name(ofproto->backer->dpif,
1732 netdev_vport_get_dpif_port(netdev, namebuf,
1739 port->odp_port = dpif_port.port_no;
1741 if (netdev_get_tunnel_config(netdev)) {
1742 atomic_count_inc(&ofproto->backer->tnl_count);
1743 tnl_port_add(port, port->up.netdev, port->odp_port,
1744 ovs_native_tunneling_is_on(ofproto), namebuf);
1745 port->is_tunnel = true;
1746 if (ofproto->ipfix) {
1747 dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1750 /* Sanity-check that a mapping doesn't already exist. This
1751 * shouldn't happen for non-tunnel ports. */
1752 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1753 VLOG_ERR("port %s already has an OpenFlow port number",
1755 dpif_port_destroy(&dpif_port);
1759 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1760 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1761 hash_odp_port(port->odp_port));
1762 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1764 dpif_port_destroy(&dpif_port);
1766 if (ofproto->sflow) {
1767 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1774 port_destruct(struct ofport *port_)
1776 struct ofport_dpif *port = ofport_dpif_cast(port_);
1777 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1778 const char *devname = netdev_get_name(port->up.netdev);
1779 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1780 const char *dp_port_name;
1782 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1784 xlate_ofport_remove(port);
1787 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1789 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1790 /* The underlying device is still there, so delete it. This
1791 * happens when the ofproto is being destroyed, since the caller
1792 * assumes that removal of attached ports will happen as part of
1794 if (!port->is_tunnel) {
1795 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1800 port->peer->peer = NULL;
1804 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1805 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1806 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1807 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1810 if (port->is_tunnel) {
1811 atomic_count_dec(&ofproto->backer->tnl_count);
1814 if (port->is_tunnel && ofproto->ipfix) {
1815 dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1819 sset_find_and_delete(&ofproto->ports, devname);
1820 sset_find_and_delete(&ofproto->ghost_ports, devname);
1821 bundle_remove(port_);
1822 set_cfm(port_, NULL);
1823 set_bfd(port_, NULL);
1824 if (port->stp_port) {
1825 stp_port_disable(port->stp_port);
1827 set_rstp_port(port_, NULL);
1828 if (ofproto->sflow) {
1829 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1836 port_modified(struct ofport *port_)
1838 struct ofport_dpif *port = ofport_dpif_cast(port_);
1839 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1840 struct netdev *netdev = port->up.netdev;
1842 if (port->bundle && port->bundle->bond) {
1843 bond_slave_set_netdev(port->bundle->bond, port, netdev);
1847 cfm_set_netdev(port->cfm, netdev);
1851 bfd_set_netdev(port->bfd, netdev);
1854 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1855 port->up.pp.hw_addr);
1857 netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1859 if (port->is_tunnel) {
1860 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1862 if (tnl_port_reconfigure(port, netdev, port->odp_port,
1863 ovs_native_tunneling_is_on(ofproto), namebuf)) {
1864 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1868 ofport_update_peer(port);
1872 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1874 struct ofport_dpif *port = ofport_dpif_cast(port_);
1875 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1876 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1878 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1879 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1880 OFPUTIL_PC_NO_PACKET_IN)) {
1881 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1883 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1884 bundle_update(port->bundle);
1890 set_sflow(struct ofproto *ofproto_,
1891 const struct ofproto_sflow_options *sflow_options)
1893 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1894 struct dpif_sflow *ds = ofproto->sflow;
1896 if (sflow_options) {
1898 struct ofport_dpif *ofport;
1900 ds = ofproto->sflow = dpif_sflow_create();
1901 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1902 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1904 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1906 dpif_sflow_set_options(ds, sflow_options);
1909 dpif_sflow_unref(ds);
1910 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1911 ofproto->sflow = NULL;
1919 struct ofproto *ofproto_,
1920 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1921 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1922 size_t n_flow_exporters_options)
1924 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1925 struct dpif_ipfix *di = ofproto->ipfix;
1926 bool has_options = bridge_exporter_options || flow_exporters_options;
1927 bool new_di = false;
1929 if (has_options && !di) {
1930 di = ofproto->ipfix = dpif_ipfix_create();
1935 /* Call set_options in any case to cleanly flush the flow
1936 * caches in the last exporters that are to be destroyed. */
1937 dpif_ipfix_set_options(
1938 di, bridge_exporter_options, flow_exporters_options,
1939 n_flow_exporters_options);
1941 /* Add tunnel ports only when a new ipfix created */
1942 if (new_di == true) {
1943 struct ofport_dpif *ofport;
1944 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1945 if (ofport->is_tunnel == true) {
1946 dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
1952 dpif_ipfix_unref(di);
1953 ofproto->ipfix = NULL;
1961 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1963 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1968 struct ofproto_dpif *ofproto;
1970 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1971 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1972 ofport->cfm = cfm_create(ofport->up.netdev);
1975 if (cfm_configure(ofport->cfm, s)) {
1982 cfm_unref(ofport->cfm);
1985 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1986 ofport->up.pp.hw_addr);
1991 cfm_status_changed(struct ofport *ofport_)
1993 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1995 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
1999 get_cfm_status(const struct ofport *ofport_,
2000 struct cfm_status *status)
2002 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2006 cfm_get_status(ofport->cfm, status);
2015 set_bfd(struct ofport *ofport_, const struct smap *cfg)
2017 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2018 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2022 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2023 cfg, ofport->up.netdev);
2024 if (ofport->bfd != old) {
2025 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2027 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2028 ofport->up.pp.hw_addr);
2033 bfd_status_changed(struct ofport *ofport_)
2035 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2037 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2041 get_bfd_status(struct ofport *ofport_, struct smap *smap)
2043 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2047 bfd_get_status(ofport->bfd, smap);
2055 /* Spanning Tree. */
2057 /* Called while rstp_mutex is held. */
2059 rstp_send_bpdu_cb(struct ofpbuf *pkt, void *ofport_, void *ofproto_)
2061 struct ofproto_dpif *ofproto = ofproto_;
2062 struct ofport_dpif *ofport = ofport_;
2063 struct eth_header *eth = ofpbuf_l2(pkt);
2065 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2066 if (eth_addr_is_zero(eth->eth_src)) {
2067 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2068 "does not have a configured source MAC address.",
2069 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
2071 ofproto_dpif_send_packet(ofport, pkt);
2077 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
2079 struct ofproto_dpif *ofproto = ofproto_;
2080 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2081 struct ofport_dpif *ofport;
2083 ofport = stp_port_get_aux(sp);
2085 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2086 ofproto->up.name, port_num);
2088 struct eth_header *eth = ofpbuf_l2(pkt);
2090 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2091 if (eth_addr_is_zero(eth->eth_src)) {
2092 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2093 "with unknown MAC", ofproto->up.name, port_num);
2095 ofproto_dpif_send_packet(ofport, pkt);
2101 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2103 set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2105 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2107 /* Only revalidate flows if the configuration changed. */
2108 if (!s != !ofproto->rstp) {
2109 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2113 if (!ofproto->rstp) {
2114 ofproto->rstp = rstp_create(ofproto_->name, s->address,
2115 rstp_send_bpdu_cb, ofproto);
2116 ofproto->rstp_last_tick = time_msec();
2118 rstp_set_bridge_address(ofproto->rstp, s->address);
2119 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2120 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2121 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2122 s->force_protocol_version);
2123 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2124 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2125 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2126 s->transmit_hold_count);
2128 struct ofport *ofport;
2129 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2130 set_rstp_port(ofport, NULL);
2132 rstp_unref(ofproto->rstp);
2133 ofproto->rstp = NULL;
2138 get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2140 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2142 if (ofproto->rstp) {
2144 s->root_id = rstp_get_root_id(ofproto->rstp);
2145 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2146 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2147 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2148 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2149 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2156 update_rstp_port_state(struct ofport_dpif *ofport)
2158 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2159 enum rstp_state state;
2161 /* Figure out new state. */
2162 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2166 if (ofport->rstp_state != state) {
2167 enum ofputil_port_state of_state;
2170 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2171 netdev_get_name(ofport->up.netdev),
2172 rstp_state_name(ofport->rstp_state),
2173 rstp_state_name(state));
2175 if (rstp_learn_in_state(ofport->rstp_state)
2176 != rstp_learn_in_state(state)) {
2177 /* XXX: Learning action flows should also be flushed. */
2178 if (ofport->bundle) {
2179 if (!rstp_shift_root_learned_address(ofproto->rstp)
2180 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2181 bundle_flush_macs(ofport->bundle, false);
2185 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2186 != rstp_forward_in_state(state);
2188 ofproto->backer->need_revalidate = REV_RSTP;
2189 ofport->rstp_state = state;
2191 if (fwd_change && ofport->bundle) {
2192 bundle_update(ofport->bundle);
2195 /* Update the RSTP state bits in the OpenFlow port description. */
2196 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2197 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2198 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2199 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2201 ofproto_port_set_state(&ofport->up, of_state);
2206 rstp_run(struct ofproto_dpif *ofproto)
2208 if (ofproto->rstp) {
2209 long long int now = time_msec();
2210 long long int elapsed = now - ofproto->rstp_last_tick;
2211 struct rstp_port *rp;
2212 struct ofport_dpif *ofport;
2214 /* Every second, decrease the values of the timers. */
2215 if (elapsed >= 1000) {
2216 rstp_tick_timers(ofproto->rstp);
2217 ofproto->rstp_last_tick = now;
2220 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2221 update_rstp_port_state(ofport);
2225 /* FIXME: This check should be done on-event (i.e., when setting
2226 * p->fdb_flush) and not periodically.
2228 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2229 if (!rstp_shift_root_learned_address(ofproto->rstp)
2230 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2231 bundle_flush_macs(ofport->bundle, false);
2235 if (rstp_shift_root_learned_address(ofproto->rstp)) {
2236 bundle_move(((struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp))->bundle,
2237 ((struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp))->bundle);
2238 rstp_reset_root_changed(ofproto->rstp);
2243 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2245 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2247 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2249 /* Only revalidate flows if the configuration changed. */
2250 if (!s != !ofproto->stp) {
2251 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2255 if (!ofproto->stp) {
2256 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2257 send_bpdu_cb, ofproto);
2258 ofproto->stp_last_tick = time_msec();
2261 stp_set_bridge_id(ofproto->stp, s->system_id);
2262 stp_set_bridge_priority(ofproto->stp, s->priority);
2263 stp_set_hello_time(ofproto->stp, s->hello_time);
2264 stp_set_max_age(ofproto->stp, s->max_age);
2265 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2267 struct ofport *ofport;
2269 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2270 set_stp_port(ofport, NULL);
2273 stp_unref(ofproto->stp);
2274 ofproto->stp = NULL;
2281 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2283 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2287 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2288 s->designated_root = stp_get_designated_root(ofproto->stp);
2289 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2298 update_stp_port_state(struct ofport_dpif *ofport)
2300 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2301 enum stp_state state;
2303 /* Figure out new state. */
2304 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2308 if (ofport->stp_state != state) {
2309 enum ofputil_port_state of_state;
2312 VLOG_DBG("port %s: STP state changed from %s to %s",
2313 netdev_get_name(ofport->up.netdev),
2314 stp_state_name(ofport->stp_state),
2315 stp_state_name(state));
2316 if (stp_learn_in_state(ofport->stp_state)
2317 != stp_learn_in_state(state)) {
2318 /* xxx Learning action flows should also be flushed. */
2319 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2320 mac_learning_flush(ofproto->ml);
2321 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2322 mcast_snooping_mdb_flush(ofproto->ms);
2324 fwd_change = stp_forward_in_state(ofport->stp_state)
2325 != stp_forward_in_state(state);
2327 ofproto->backer->need_revalidate = REV_STP;
2328 ofport->stp_state = state;
2329 ofport->stp_state_entered = time_msec();
2331 if (fwd_change && ofport->bundle) {
2332 bundle_update(ofport->bundle);
2335 /* Update the STP state bits in the OpenFlow port description. */
2336 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2337 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2338 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2339 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2340 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2342 ofproto_port_set_state(&ofport->up, of_state);
2346 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2347 * caller is responsible for assigning STP port numbers and ensuring
2348 * there are no duplicates. */
2350 set_stp_port(struct ofport *ofport_,
2351 const struct ofproto_port_stp_settings *s)
2353 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2354 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2355 struct stp_port *sp = ofport->stp_port;
2357 if (!s || !s->enable) {
2359 ofport->stp_port = NULL;
2360 stp_port_disable(sp);
2361 update_stp_port_state(ofport);
2364 } else if (sp && stp_port_no(sp) != s->port_num
2365 && ofport == stp_port_get_aux(sp)) {
2366 /* The port-id changed, so disable the old one if it's not
2367 * already in use by another port. */
2368 stp_port_disable(sp);
2371 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2373 /* Set name before enabling the port so that debugging messages can print
2375 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2376 stp_port_enable(sp);
2378 stp_port_set_aux(sp, ofport);
2379 stp_port_set_priority(sp, s->priority);
2380 stp_port_set_path_cost(sp, s->path_cost);
2382 update_stp_port_state(ofport);
2388 get_stp_port_status(struct ofport *ofport_,
2389 struct ofproto_port_stp_status *s)
2391 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2392 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2393 struct stp_port *sp = ofport->stp_port;
2395 if (!ofproto->stp || !sp) {
2401 s->port_id = stp_port_get_id(sp);
2402 s->state = stp_port_get_state(sp);
2403 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2404 s->role = stp_port_get_role(sp);
2410 get_stp_port_stats(struct ofport *ofport_,
2411 struct ofproto_port_stp_stats *s)
2413 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2414 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2415 struct stp_port *sp = ofport->stp_port;
2417 if (!ofproto->stp || !sp) {
2423 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2429 stp_run(struct ofproto_dpif *ofproto)
2432 long long int now = time_msec();
2433 long long int elapsed = now - ofproto->stp_last_tick;
2434 struct stp_port *sp;
2437 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2438 ofproto->stp_last_tick = now;
2440 while (stp_get_changed_port(ofproto->stp, &sp)) {
2441 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2444 update_stp_port_state(ofport);
2448 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2449 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2450 mac_learning_flush(ofproto->ml);
2451 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2452 mcast_snooping_mdb_flush(ofproto->ms);
2458 stp_wait(struct ofproto_dpif *ofproto)
2461 poll_timer_wait(1000);
2465 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2466 * caller is responsible for assigning RSTP port numbers and ensuring
2467 * there are no duplicates. */
2469 set_rstp_port(struct ofport *ofport_,
2470 const struct ofproto_port_rstp_settings *s)
2472 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2473 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2474 struct rstp_port *rp = ofport->rstp_port;
2476 if (!s || !s->enable) {
2478 rstp_port_unref(rp);
2479 ofport->rstp_port = NULL;
2480 update_rstp_port_state(ofport);
2485 /* Check if need to add a new port. */
2487 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2490 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
2491 s->admin_edge_port, s->auto_edge,
2492 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2494 update_rstp_port_state(ofport);
2495 /* Synchronize operational status. */
2496 rstp_port_set_mac_operational(rp, ofport->may_enable);
2500 get_rstp_port_status(struct ofport *ofport_,
2501 struct ofproto_port_rstp_status *s)
2503 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2504 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2505 struct rstp_port *rp = ofport->rstp_port;
2507 if (!ofproto->rstp || !rp) {
2513 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2514 &s->designated_bridge_id, &s->designated_port_id,
2515 &s->designated_path_cost, &s->tx_count,
2516 &s->rx_count, &s->error_count, &s->uptime);
2521 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2524 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2525 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2527 if (ofport->n_qdscp != n_qdscp
2528 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2529 n_qdscp * sizeof *qdscp))) {
2530 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2531 free(ofport->qdscp);
2532 ofport->qdscp = n_qdscp
2533 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2535 ofport->n_qdscp = n_qdscp;
2543 /* Expires all MAC learning entries associated with 'bundle' and forces its
2544 * ofproto to revalidate every flow.
2546 * Normally MAC learning entries are removed only from the ofproto associated
2547 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2548 * are removed from every ofproto. When patch ports and SLB bonds are in use
2549 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2550 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2551 * with the host from which it migrated. */
2553 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2555 struct ofproto_dpif *ofproto = bundle->ofproto;
2556 struct mac_learning *ml = ofproto->ml;
2557 struct mac_entry *mac, *next_mac;
2559 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2560 ovs_rwlock_wrlock(&ml->rwlock);
2561 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2562 if (mac->port.p == bundle) {
2564 struct ofproto_dpif *o;
2566 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2568 struct mac_entry *e;
2570 ovs_rwlock_wrlock(&o->ml->rwlock);
2571 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2573 mac_learning_expire(o->ml, e);
2575 ovs_rwlock_unlock(&o->ml->rwlock);
2580 mac_learning_expire(ml, mac);
2583 ovs_rwlock_unlock(&ml->rwlock);
2587 bundle_move(struct ofbundle *old, struct ofbundle *new)
2589 struct ofproto_dpif *ofproto = old->ofproto;
2590 struct mac_learning *ml = ofproto->ml;
2591 struct mac_entry *mac, *next_mac;
2593 ovs_assert(new->ofproto == old->ofproto);
2595 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2596 ovs_rwlock_wrlock(&ml->rwlock);
2597 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2598 if (mac->port.p == old) {
2602 ovs_rwlock_unlock(&ml->rwlock);
2605 static struct ofbundle *
2606 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2608 struct ofbundle *bundle;
2610 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2611 &ofproto->bundles) {
2612 if (bundle->aux == aux) {
2620 bundle_update(struct ofbundle *bundle)
2622 struct ofport_dpif *port;
2624 bundle->floodable = true;
2625 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2626 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2628 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2629 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2630 bundle->floodable = false;
2637 bundle_del_port(struct ofport_dpif *port)
2639 struct ofbundle *bundle = port->bundle;
2641 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2643 list_remove(&port->bundle_node);
2644 port->bundle = NULL;
2647 lacp_slave_unregister(bundle->lacp, port);
2650 bond_slave_unregister(bundle->bond, port);
2653 bundle_update(bundle);
2657 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2658 struct lacp_slave_settings *lacp)
2660 struct ofport_dpif *port;
2662 port = get_ofp_port(bundle->ofproto, ofp_port);
2667 if (port->bundle != bundle) {
2668 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2670 bundle_remove(&port->up);
2673 port->bundle = bundle;
2674 list_push_back(&bundle->ports, &port->bundle_node);
2675 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2677 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2678 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2679 bundle->floodable = false;
2683 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2684 lacp_slave_register(bundle->lacp, port, lacp);
2691 bundle_destroy(struct ofbundle *bundle)
2693 struct ofproto_dpif *ofproto;
2694 struct ofport_dpif *port, *next_port;
2700 ofproto = bundle->ofproto;
2701 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2704 xlate_bundle_remove(bundle);
2707 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2708 bundle_del_port(port);
2711 bundle_flush_macs(bundle, true);
2712 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2714 free(bundle->trunks);
2715 lacp_unref(bundle->lacp);
2716 bond_unref(bundle->bond);
2721 bundle_set(struct ofproto *ofproto_, void *aux,
2722 const struct ofproto_bundle_settings *s)
2724 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2725 bool need_flush = false;
2726 struct ofport_dpif *port;
2727 struct ofbundle *bundle;
2728 unsigned long *trunks;
2734 bundle_destroy(bundle_lookup(ofproto, aux));
2738 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2739 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2741 bundle = bundle_lookup(ofproto, aux);
2743 bundle = xmalloc(sizeof *bundle);
2745 bundle->ofproto = ofproto;
2746 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2747 hash_pointer(aux, 0));
2749 bundle->name = NULL;
2751 list_init(&bundle->ports);
2752 bundle->vlan_mode = PORT_VLAN_TRUNK;
2754 bundle->trunks = NULL;
2755 bundle->use_priority_tags = s->use_priority_tags;
2756 bundle->lacp = NULL;
2757 bundle->bond = NULL;
2759 bundle->floodable = true;
2760 mbridge_register_bundle(ofproto->mbridge, bundle);
2763 if (!bundle->name || strcmp(s->name, bundle->name)) {
2765 bundle->name = xstrdup(s->name);
2770 ofproto->lacp_enabled = true;
2771 if (!bundle->lacp) {
2772 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2773 bundle->lacp = lacp_create();
2775 lacp_configure(bundle->lacp, s->lacp);
2777 lacp_unref(bundle->lacp);
2778 bundle->lacp = NULL;
2781 /* Update set of ports. */
2783 for (i = 0; i < s->n_slaves; i++) {
2784 if (!bundle_add_port(bundle, s->slaves[i],
2785 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2789 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2790 struct ofport_dpif *next_port;
2792 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2793 for (i = 0; i < s->n_slaves; i++) {
2794 if (s->slaves[i] == port->up.ofp_port) {
2799 bundle_del_port(port);
2803 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2805 if (list_is_empty(&bundle->ports)) {
2806 bundle_destroy(bundle);
2810 /* Set VLAN tagging mode */
2811 if (s->vlan_mode != bundle->vlan_mode
2812 || s->use_priority_tags != bundle->use_priority_tags) {
2813 bundle->vlan_mode = s->vlan_mode;
2814 bundle->use_priority_tags = s->use_priority_tags;
2819 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2820 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2822 if (vlan != bundle->vlan) {
2823 bundle->vlan = vlan;
2827 /* Get trunked VLANs. */
2828 switch (s->vlan_mode) {
2829 case PORT_VLAN_ACCESS:
2833 case PORT_VLAN_TRUNK:
2834 trunks = CONST_CAST(unsigned long *, s->trunks);
2837 case PORT_VLAN_NATIVE_UNTAGGED:
2838 case PORT_VLAN_NATIVE_TAGGED:
2839 if (vlan != 0 && (!s->trunks
2840 || !bitmap_is_set(s->trunks, vlan)
2841 || bitmap_is_set(s->trunks, 0))) {
2842 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2844 trunks = bitmap_clone(s->trunks, 4096);
2846 trunks = bitmap_allocate1(4096);
2848 bitmap_set1(trunks, vlan);
2849 bitmap_set0(trunks, 0);
2851 trunks = CONST_CAST(unsigned long *, s->trunks);
2858 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2859 free(bundle->trunks);
2860 if (trunks == s->trunks) {
2861 bundle->trunks = vlan_bitmap_clone(trunks);
2863 bundle->trunks = trunks;
2868 if (trunks != s->trunks) {
2873 if (!list_is_short(&bundle->ports)) {
2874 bundle->ofproto->has_bonded_bundles = true;
2876 if (bond_reconfigure(bundle->bond, s->bond)) {
2877 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2880 bundle->bond = bond_create(s->bond, ofproto);
2881 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2884 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2885 bond_slave_register(bundle->bond, port,
2886 port->up.ofp_port, port->up.netdev);
2889 bond_unref(bundle->bond);
2890 bundle->bond = NULL;
2893 /* If we changed something that would affect MAC learning, un-learn
2894 * everything on this port and force flow revalidation. */
2896 bundle_flush_macs(bundle, false);
2903 bundle_remove(struct ofport *port_)
2905 struct ofport_dpif *port = ofport_dpif_cast(port_);
2906 struct ofbundle *bundle = port->bundle;
2909 bundle_del_port(port);
2910 if (list_is_empty(&bundle->ports)) {
2911 bundle_destroy(bundle);
2912 } else if (list_is_short(&bundle->ports)) {
2913 bond_unref(bundle->bond);
2914 bundle->bond = NULL;
2920 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2922 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2923 struct ofport_dpif *port = port_;
2924 uint8_t ea[ETH_ADDR_LEN];
2927 error = netdev_get_etheraddr(port->up.netdev, ea);
2929 struct ofpbuf packet;
2932 ofpbuf_init(&packet, 0);
2933 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2935 memcpy(packet_pdu, pdu, pdu_size);
2937 ofproto_dpif_send_packet(port, &packet);
2938 ofpbuf_uninit(&packet);
2940 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2941 "%s (%s)", port->bundle->name,
2942 netdev_get_name(port->up.netdev), ovs_strerror(error));
2947 bundle_send_learning_packets(struct ofbundle *bundle)
2949 struct ofproto_dpif *ofproto = bundle->ofproto;
2950 struct ofpbuf *learning_packet;
2951 int error, n_packets, n_errors;
2952 struct mac_entry *e;
2953 struct ovs_list packets;
2955 list_init(&packets);
2956 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2957 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2958 if (e->port.p != bundle) {
2961 learning_packet = bond_compose_learning_packet(bundle->bond,
2964 /* Temporarily use 'frame' as a private pointer (see below). */
2965 ovs_assert(learning_packet->frame == ofpbuf_data(learning_packet));
2966 learning_packet->frame = port_void;
2967 list_push_back(&packets, &learning_packet->list_node);
2970 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2972 error = n_packets = n_errors = 0;
2973 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2975 void *port_void = learning_packet->frame;
2977 /* Restore 'frame'. */
2978 learning_packet->frame = ofpbuf_data(learning_packet);
2979 ret = ofproto_dpif_send_packet(port_void, learning_packet);
2986 ofpbuf_list_delete(&packets);
2989 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2990 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2991 "packets, last error was: %s",
2992 bundle->name, n_errors, n_packets, ovs_strerror(error));
2994 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2995 bundle->name, n_packets);
3000 bundle_run(struct ofbundle *bundle)
3003 lacp_run(bundle->lacp, send_pdu_cb);
3006 struct ofport_dpif *port;
3008 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3009 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
3012 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3013 bundle->ofproto->backer->need_revalidate = REV_BOND;
3016 if (bond_should_send_learning_packets(bundle->bond)) {
3017 bundle_send_learning_packets(bundle);
3023 bundle_wait(struct ofbundle *bundle)
3026 lacp_wait(bundle->lacp);
3029 bond_wait(bundle->bond);
3036 mirror_set__(struct ofproto *ofproto_, void *aux,
3037 const struct ofproto_mirror_settings *s)
3039 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3040 struct ofbundle **srcs, **dsts;
3045 mirror_destroy(ofproto->mbridge, aux);
3049 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3050 dsts = xmalloc(s->n_dsts * sizeof *dsts);
3052 for (i = 0; i < s->n_srcs; i++) {
3053 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
3056 for (i = 0; i < s->n_dsts; i++) {
3057 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
3060 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3061 s->n_dsts, s->src_vlans,
3062 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
3069 mirror_get_stats__(struct ofproto *ofproto, void *aux,
3070 uint64_t *packets, uint64_t *bytes)
3072 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3077 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3079 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3080 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3081 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
3082 mac_learning_flush(ofproto->ml);
3084 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3089 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
3091 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3092 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3093 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
3097 forward_bpdu_changed(struct ofproto *ofproto_)
3099 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3100 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3104 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3107 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3108 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3109 mac_learning_set_idle_time(ofproto->ml, idle_time);
3110 mac_learning_set_max_entries(ofproto->ml, max_entries);
3111 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3114 /* Configures multicast snooping on 'ofport' using the settings
3115 * defined in 's'. */
3117 set_mcast_snooping(struct ofproto *ofproto_,
3118 const struct ofproto_mcast_snooping_settings *s)
3120 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3122 /* Only revalidate flows if the configuration changed. */
3123 if (!s != !ofproto->ms) {
3124 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3129 ofproto->ms = mcast_snooping_create();
3132 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3133 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3134 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3135 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3136 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3138 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3140 mcast_snooping_unref(ofproto->ms);
3147 /* Configures multicast snooping port's flood setting on 'ofproto'. */
3149 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux, bool flood)
3151 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3152 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3155 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3156 mcast_snooping_set_port_flood(ofproto->ms, bundle->vlan, bundle,
3158 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3166 static struct ofport_dpif *
3167 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
3169 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3170 return ofport ? ofport_dpif_cast(ofport) : NULL;
3174 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3175 struct ofproto_port *ofproto_port,
3176 struct dpif_port *dpif_port)
3178 ofproto_port->name = dpif_port->name;
3179 ofproto_port->type = dpif_port->type;
3180 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3184 ofport_update_peer(struct ofport_dpif *ofport)
3186 const struct ofproto_dpif *ofproto;
3187 struct dpif_backer *backer;
3190 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3194 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
3195 backer->need_revalidate = REV_RECONFIGURE;
3198 ofport->peer->peer = NULL;
3199 ofport->peer = NULL;
3202 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3207 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3208 struct ofport *peer_ofport;
3209 struct ofport_dpif *peer;
3212 if (ofproto->backer != backer) {
3216 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3221 peer = ofport_dpif_cast(peer_ofport);
3222 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3223 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3225 ofport->peer = peer;
3226 ofport->peer->peer = ofport;
3236 port_run(struct ofport_dpif *ofport)
3238 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3239 bool carrier_changed = carrier_seq != ofport->carrier_seq;
3240 bool enable = netdev_get_carrier(ofport->up.netdev);
3241 bool cfm_enable = false;
3242 bool bfd_enable = false;
3244 ofport->carrier_seq = carrier_seq;
3247 int cfm_opup = cfm_get_opup(ofport->cfm);
3249 cfm_enable = !cfm_get_fault(ofport->cfm);
3251 if (cfm_opup >= 0) {
3252 cfm_enable = cfm_enable && cfm_opup;
3257 bfd_enable = bfd_forwarding(ofport->bfd);
3260 if (ofport->bfd || ofport->cfm) {
3261 enable = enable && (cfm_enable || bfd_enable);
3264 if (ofport->bundle) {
3265 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3266 if (carrier_changed) {
3267 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3271 if (ofport->may_enable != enable) {
3272 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3274 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3276 if (ofport->rstp_port) {
3277 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3281 ofport->may_enable = enable;
3285 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3286 struct ofproto_port *ofproto_port)
3288 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3289 struct dpif_port dpif_port;
3292 if (sset_contains(&ofproto->ghost_ports, devname)) {
3293 const char *type = netdev_get_type_from_name(devname);
3295 /* We may be called before ofproto->up.port_by_name is populated with
3296 * the appropriate ofport. For this reason, we must get the name and
3297 * type from the netdev layer directly. */
3299 const struct ofport *ofport;
3301 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3302 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3303 ofproto_port->name = xstrdup(devname);
3304 ofproto_port->type = xstrdup(type);
3310 if (!sset_contains(&ofproto->ports, devname)) {
3313 error = dpif_port_query_by_name(ofproto->backer->dpif,
3314 devname, &dpif_port);
3316 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3322 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3324 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3325 const char *devname = netdev_get_name(netdev);
3326 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3327 const char *dp_port_name;
3329 if (netdev_vport_is_patch(netdev)) {
3330 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3334 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3335 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3336 odp_port_t port_no = ODPP_NONE;
3339 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3343 if (netdev_get_tunnel_config(netdev)) {
3344 simap_put(&ofproto->backer->tnl_backers,
3345 dp_port_name, odp_to_u32(port_no));
3349 if (netdev_get_tunnel_config(netdev)) {
3350 sset_add(&ofproto->ghost_ports, devname);
3352 sset_add(&ofproto->ports, devname);
3358 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3360 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3361 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
3368 sset_find_and_delete(&ofproto->ghost_ports,
3369 netdev_get_name(ofport->up.netdev));
3370 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3371 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3372 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3374 /* The caller is going to close ofport->up.netdev. If this is a
3375 * bonded port, then the bond is using that netdev, so remove it
3376 * from the bond. The client will need to reconfigure everything
3377 * after deleting ports, so then the slave will get re-added. */
3378 bundle_remove(&ofport->up);
3385 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3387 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3390 error = netdev_get_stats(ofport->up.netdev, stats);
3392 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3393 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3395 ovs_mutex_lock(&ofproto->stats_mutex);
3396 /* ofproto->stats.tx_packets represents packets that we created
3397 * internally and sent to some port (e.g. packets sent with
3398 * ofproto_dpif_send_packet()). Account for them as if they had
3399 * come from OFPP_LOCAL and got forwarded. */
3401 if (stats->rx_packets != UINT64_MAX) {
3402 stats->rx_packets += ofproto->stats.tx_packets;
3405 if (stats->rx_bytes != UINT64_MAX) {
3406 stats->rx_bytes += ofproto->stats.tx_bytes;
3409 /* ofproto->stats.rx_packets represents packets that were received on
3410 * some port and we processed internally and dropped (e.g. STP).
3411 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3413 if (stats->tx_packets != UINT64_MAX) {
3414 stats->tx_packets += ofproto->stats.rx_packets;
3417 if (stats->tx_bytes != UINT64_MAX) {
3418 stats->tx_bytes += ofproto->stats.rx_bytes;
3420 ovs_mutex_unlock(&ofproto->stats_mutex);
3427 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3429 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3430 if (ofport->bundle && ofport->bundle->lacp) {
3431 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3438 struct port_dump_state {
3443 struct ofproto_port port;
3448 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3450 *statep = xzalloc(sizeof(struct port_dump_state));
3455 port_dump_next(const struct ofproto *ofproto_, void *state_,
3456 struct ofproto_port *port)
3458 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3459 struct port_dump_state *state = state_;
3460 const struct sset *sset;
3461 struct sset_node *node;
3463 if (state->has_port) {
3464 ofproto_port_destroy(&state->port);
3465 state->has_port = false;
3467 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3468 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3471 error = port_query_by_name(ofproto_, node->name, &state->port);
3473 *port = state->port;
3474 state->has_port = true;
3476 } else if (error != ENODEV) {
3481 if (!state->ghost) {
3482 state->ghost = true;
3485 return port_dump_next(ofproto_, state_, port);
3492 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3494 struct port_dump_state *state = state_;
3496 if (state->has_port) {
3497 ofproto_port_destroy(&state->port);
3504 port_poll(const struct ofproto *ofproto_, char **devnamep)
3506 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3508 if (ofproto->port_poll_errno) {
3509 int error = ofproto->port_poll_errno;
3510 ofproto->port_poll_errno = 0;
3514 if (sset_is_empty(&ofproto->port_poll_set)) {
3518 *devnamep = sset_pop(&ofproto->port_poll_set);
3523 port_poll_wait(const struct ofproto *ofproto_)
3525 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3526 dpif_port_poll_wait(ofproto->backer->dpif);
3530 port_is_lacp_current(const struct ofport *ofport_)
3532 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3533 return (ofport->bundle && ofport->bundle->lacp
3534 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3538 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3539 * then delete it entirely. */
3541 rule_expire(struct rule_dpif *rule)
3542 OVS_REQUIRES(ofproto_mutex)
3544 uint16_t hard_timeout, idle_timeout;
3545 long long int now = time_msec();
3548 hard_timeout = rule->up.hard_timeout;
3549 idle_timeout = rule->up.idle_timeout;
3551 /* Has 'rule' expired? */
3553 long long int modified;
3555 ovs_mutex_lock(&rule->up.mutex);
3556 modified = rule->up.modified;
3557 ovs_mutex_unlock(&rule->up.mutex);
3559 if (now > modified + hard_timeout * 1000) {
3560 reason = OFPRR_HARD_TIMEOUT;
3564 if (reason < 0 && idle_timeout) {
3567 ovs_mutex_lock(&rule->stats_mutex);
3568 used = rule->stats.used;
3569 ovs_mutex_unlock(&rule->stats_mutex);
3571 if (now > used + idle_timeout * 1000) {
3572 reason = OFPRR_IDLE_TIMEOUT;
3577 COVERAGE_INC(ofproto_dpif_expired);
3578 ofproto_rule_expire(&rule->up, reason);
3582 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3583 * 'flow' must reflect the data in 'packet'. */
3585 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3586 const struct flow *flow,
3587 struct rule_dpif *rule,
3588 const struct ofpact *ofpacts, size_t ofpacts_len,
3589 struct ofpbuf *packet)
3591 struct dpif_flow_stats stats;
3592 struct xlate_out xout;
3593 struct xlate_in xin;
3595 struct dpif_execute execute;
3598 ovs_assert((rule != NULL) != (ofpacts != NULL));
3600 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3603 rule_dpif_credit_stats(rule, &stats);
3606 xlate_in_init(&xin, ofproto, flow, flow->in_port.ofp_port, rule,
3607 stats.tcp_flags, packet);
3608 xin.ofpacts = ofpacts;
3609 xin.ofpacts_len = ofpacts_len;
3610 xin.resubmit_stats = &stats;
3611 xlate_actions(&xin, &xout);
3613 execute.actions = ofpbuf_data(xout.odp_actions);
3614 execute.actions_len = ofpbuf_size(xout.odp_actions);
3616 execute.packet = packet;
3617 execute.md = pkt_metadata_from_flow(flow);
3618 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3619 execute.probe = false;
3621 /* Fix up in_port. */
3622 in_port = flow->in_port.ofp_port;
3623 if (in_port == OFPP_NONE) {
3624 in_port = OFPP_LOCAL;
3626 execute.md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3628 error = dpif_execute(ofproto->backer->dpif, &execute);
3630 xlate_out_uninit(&xout);
3636 rule_dpif_credit_stats(struct rule_dpif *rule,
3637 const struct dpif_flow_stats *stats)
3639 ovs_mutex_lock(&rule->stats_mutex);
3640 rule->stats.n_packets += stats->n_packets;
3641 rule->stats.n_bytes += stats->n_bytes;
3642 rule->stats.used = MAX(rule->stats.used, stats->used);
3643 ovs_mutex_unlock(&rule->stats_mutex);
3647 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3648 OVS_REQUIRES(rule->up.mutex)
3650 return rule->up.flow_cookie;
3654 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3655 uint16_t hard_timeout)
3657 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3660 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3661 * be read until the calling thread quiesces. */
3662 const struct rule_actions *
3663 rule_dpif_get_actions(const struct rule_dpif *rule)
3665 return rule_get_actions(&rule->up);
3668 /* Sets 'rule''s recirculation id. */
3670 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3671 OVS_REQUIRES(rule->up.mutex)
3673 ovs_assert(!rule->recirc_id);
3674 rule->recirc_id = id;
3677 /* Returns 'rule''s recirculation id. */
3679 rule_dpif_get_recirc_id(struct rule_dpif *rule)
3680 OVS_REQUIRES(rule->up.mutex)
3682 if (!rule->recirc_id) {
3683 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3685 rule_dpif_set_recirc_id(rule, ofproto_dpif_alloc_recirc_id(ofproto));
3687 return rule->recirc_id;
3690 /* Sets 'rule''s recirculation id. */
3692 rule_set_recirc_id(struct rule *rule_, uint32_t id)
3694 struct rule_dpif *rule = rule_dpif_cast(rule_);
3696 ovs_mutex_lock(&rule->up.mutex);
3697 rule_dpif_set_recirc_id(rule, id);
3698 ovs_mutex_unlock(&rule->up.mutex);
3701 /* Lookup 'flow' in table 0 of 'ofproto''s classifier.
3702 * If 'wc' is non-null, sets the fields that were relevant as part of
3703 * the lookup. Returns the table id where a match or miss occurred via
3704 * 'table_id'. This will be zero unless there was a miss and
3705 * OFPTC11_TABLE_MISS_CONTINUE is in effect for the sequence of tables
3706 * where misses occur, or TBL_INTERNAL if the rule has a non-zero
3707 * recirculation ID, and a match was found in the internal table, or if
3708 * there was no match and one of the special rules (drop_frags_rule,
3709 * miss_rule, or no_packet_in_rule) was returned.
3711 * The return value is the found rule, which is valid at least until the next
3712 * RCU quiescent period. If the rule needs to stay around longer,
3713 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3714 * on it before this returns. */
3716 rule_dpif_lookup(struct ofproto_dpif *ofproto, struct flow *flow,
3717 struct flow_wildcards *wc, bool take_ref,
3718 const struct dpif_flow_stats *stats, uint8_t *table_id)
3722 if (ofproto_dpif_get_enable_recirc(ofproto)) {
3723 /* Always exactly match recirc_id since datapath supports
3726 wc->masks.recirc_id = UINT32_MAX;
3728 *table_id = rule_dpif_lookup_get_init_table_id(flow);
3731 return rule_dpif_lookup_from_table(ofproto, flow, wc, take_ref, stats,
3732 table_id, flow->in_port.ofp_port, true,
3736 /* The returned rule (if any) is valid at least until the next RCU quiescent
3737 * period. If the rule needs to stay around longer, a non-zero 'take_ref'
3738 * must be passed in to cause a reference to be taken on it. */
3739 static struct rule_dpif *
3740 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3741 const struct flow *flow, struct flow_wildcards *wc,
3744 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3745 const struct cls_rule *cls_rule;
3746 struct rule_dpif *rule;
3749 cls_rule = classifier_lookup(cls, flow, wc);
3751 rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3753 /* Try again if the rule was released before we get the reference. */
3754 } while (rule && take_ref && !rule_dpif_try_ref(rule));
3759 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3760 * Returns the rule that was found, which may be one of the special rules
3761 * according to packet miss hadling. If 'may_packet_in' is false, returning of
3762 * the miss_rule (which issues packet ins for the controller) is avoided.
3763 * Updates 'wc', if nonnull, to reflect the fields that were used during the
3766 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3767 * if none is found then the table miss configuration for that table is
3768 * honored, which can result in additional lookups in other OpenFlow tables.
3769 * In this case the function updates '*table_id' to reflect the final OpenFlow
3770 * table that was searched.
3772 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3775 * The rule is returned in '*rule', which is valid at least until the next
3776 * RCU quiescent period. If the '*rule' needs to stay around longer,
3777 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3778 * on it before this returns.
3780 * 'in_port' allows the lookup to take place as if the in port had the value
3781 * 'in_port'. This is needed for resubmit action support. */
3783 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto, struct flow *flow,
3784 struct flow_wildcards *wc, bool take_ref,
3785 const struct dpif_flow_stats *stats,
3786 uint8_t *table_id, ofp_port_t in_port,
3787 bool may_packet_in, bool honor_table_miss)
3789 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
3790 ofp_port_t old_in_port = flow->in_port.ofp_port;
3791 enum ofputil_table_miss miss_config;
3792 struct rule_dpif *rule;
3795 /* We always unwildcard nw_frag (for IP), so they
3796 * need not be unwildcarded here. */
3797 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3798 && ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3799 if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3800 /* We must pretend that transport ports are unavailable. */
3801 flow->tp_src = htons(0);
3802 flow->tp_dst = htons(0);
3804 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
3805 * Use the drop_frags_rule (which cannot disappear). */
3806 rule = ofproto->drop_frags_rule;
3808 rule_dpif_ref(rule);
3811 struct oftable *tbl = &ofproto->up.tables[*table_id];
3814 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
3820 /* Look up a flow with 'in_port' as the input port. Then restore the
3821 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3822 * have surprising behavior). */
3823 flow->in_port.ofp_port = in_port;
3825 /* Our current implementation depends on n_tables == N_TABLES, and
3826 * TBL_INTERNAL being the last table. */
3827 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
3829 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
3831 for (next_id = *table_id;
3832 next_id < ofproto->up.n_tables;
3833 next_id++, next_id += (next_id == TBL_INTERNAL))
3835 *table_id = next_id;
3836 rule = rule_dpif_lookup_in_table(ofproto, next_id, flow, wc, take_ref);
3838 struct oftable *tbl = &ofproto->up.tables[next_id];
3841 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
3842 stats->n_packets, &orig);
3845 goto out; /* Match. */
3847 if (honor_table_miss) {
3848 miss_config = ofproto_table_get_miss_config(&ofproto->up,
3850 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
3857 rule = ofproto->no_packet_in_rule;
3858 if (may_packet_in) {
3859 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
3860 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
3861 struct ofport_dpif *port;
3863 port = get_ofp_port(ofproto, old_in_port);
3865 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3867 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
3868 rule = ofproto->miss_rule;
3870 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
3871 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3872 rule = ofproto->miss_rule;
3876 rule_dpif_ref(rule);
3879 /* Restore port numbers, as they may have been modified above. */
3880 flow->tp_src = old_tp_src;
3881 flow->tp_dst = old_tp_dst;
3882 /* Restore the old in port. */
3883 flow->in_port.ofp_port = old_in_port;
3889 complete_operation(struct rule_dpif *rule)
3890 OVS_REQUIRES(ofproto_mutex)
3892 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3894 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3897 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3899 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3902 static struct rule *
3905 struct rule_dpif *rule = xmalloc(sizeof *rule);
3910 rule_dealloc(struct rule *rule_)
3912 struct rule_dpif *rule = rule_dpif_cast(rule_);
3917 rule_construct(struct rule *rule_)
3918 OVS_NO_THREAD_SAFETY_ANALYSIS
3920 struct rule_dpif *rule = rule_dpif_cast(rule_);
3921 ovs_mutex_init_adaptive(&rule->stats_mutex);
3922 rule->stats.n_packets = 0;
3923 rule->stats.n_bytes = 0;
3924 rule->stats.used = rule->up.modified;
3925 rule->recirc_id = 0;
3931 rule_insert(struct rule *rule_)
3932 OVS_REQUIRES(ofproto_mutex)
3934 struct rule_dpif *rule = rule_dpif_cast(rule_);
3935 complete_operation(rule);
3940 rule_delete(struct rule *rule_)
3941 OVS_REQUIRES(ofproto_mutex)
3943 struct rule_dpif *rule = rule_dpif_cast(rule_);
3944 complete_operation(rule);
3948 rule_destruct(struct rule *rule_)
3950 struct rule_dpif *rule = rule_dpif_cast(rule_);
3952 ovs_mutex_destroy(&rule->stats_mutex);
3953 if (rule->recirc_id) {
3954 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3956 ofproto_dpif_free_recirc_id(ofproto, rule->recirc_id);
3961 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3962 long long int *used)
3964 struct rule_dpif *rule = rule_dpif_cast(rule_);
3966 ovs_mutex_lock(&rule->stats_mutex);
3967 *packets = rule->stats.n_packets;
3968 *bytes = rule->stats.n_bytes;
3969 *used = rule->stats.used;
3970 ovs_mutex_unlock(&rule->stats_mutex);
3974 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3975 struct ofpbuf *packet)
3977 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3979 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3983 rule_execute(struct rule *rule, const struct flow *flow,
3984 struct ofpbuf *packet)
3986 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3987 ofpbuf_delete(packet);
3992 rule_modify_actions(struct rule *rule_, bool reset_counters)
3993 OVS_REQUIRES(ofproto_mutex)
3995 struct rule_dpif *rule = rule_dpif_cast(rule_);
3997 if (reset_counters) {
3998 ovs_mutex_lock(&rule->stats_mutex);
3999 rule->stats.n_packets = 0;
4000 rule->stats.n_bytes = 0;
4001 ovs_mutex_unlock(&rule->stats_mutex);
4004 complete_operation(rule);
4007 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4009 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4012 static struct ofgroup *
4015 struct group_dpif *group = xzalloc(sizeof *group);
4020 group_dealloc(struct ofgroup *group_)
4022 struct group_dpif *group = group_dpif_cast(group_);
4027 group_construct_stats(struct group_dpif *group)
4028 OVS_REQUIRES(group->stats_mutex)
4030 struct ofputil_bucket *bucket;
4031 const struct ovs_list *buckets;
4033 group->packet_count = 0;
4034 group->byte_count = 0;
4036 group_dpif_get_buckets(group, &buckets);
4037 LIST_FOR_EACH (bucket, list_node, buckets) {
4038 bucket->stats.packet_count = 0;
4039 bucket->stats.byte_count = 0;
4044 group_dpif_credit_stats(struct group_dpif *group,
4045 struct ofputil_bucket *bucket,
4046 const struct dpif_flow_stats *stats)
4048 ovs_mutex_lock(&group->stats_mutex);
4049 group->packet_count += stats->n_packets;
4050 group->byte_count += stats->n_bytes;
4052 bucket->stats.packet_count += stats->n_packets;
4053 bucket->stats.byte_count += stats->n_bytes;
4054 } else { /* Credit to all buckets */
4055 const struct ovs_list *buckets;
4057 group_dpif_get_buckets(group, &buckets);
4058 LIST_FOR_EACH (bucket, list_node, buckets) {
4059 bucket->stats.packet_count += stats->n_packets;
4060 bucket->stats.byte_count += stats->n_bytes;
4063 ovs_mutex_unlock(&group->stats_mutex);
4067 group_construct(struct ofgroup *group_)
4069 struct group_dpif *group = group_dpif_cast(group_);
4070 const struct ofputil_bucket *bucket;
4072 /* Prevent group chaining because our locking structure makes it hard to
4073 * implement deadlock-free. (See xlate_group_resource_check().) */
4074 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4075 const struct ofpact *a;
4077 OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
4078 if (a->type == OFPACT_GROUP) {
4079 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
4084 ovs_mutex_init_adaptive(&group->stats_mutex);
4085 ovs_mutex_lock(&group->stats_mutex);
4086 group_construct_stats(group);
4087 ovs_mutex_unlock(&group->stats_mutex);
4092 group_destruct(struct ofgroup *group_)
4094 struct group_dpif *group = group_dpif_cast(group_);
4095 ovs_mutex_destroy(&group->stats_mutex);
4099 group_modify(struct ofgroup *group_)
4101 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
4103 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4109 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4111 struct group_dpif *group = group_dpif_cast(group_);
4112 struct ofputil_bucket *bucket;
4113 const struct ovs_list *buckets;
4114 struct bucket_counter *bucket_stats;
4116 ovs_mutex_lock(&group->stats_mutex);
4117 ogs->packet_count = group->packet_count;
4118 ogs->byte_count = group->byte_count;
4120 group_dpif_get_buckets(group, &buckets);
4121 bucket_stats = ogs->bucket_stats;
4122 LIST_FOR_EACH (bucket, list_node, buckets) {
4123 bucket_stats->packet_count = bucket->stats.packet_count;
4124 bucket_stats->byte_count = bucket->stats.byte_count;
4127 ovs_mutex_unlock(&group->stats_mutex);
4132 /* If the group exists, this function increments the groups's reference count.
4134 * Make sure to call group_dpif_unref() after no longer needing to maintain
4135 * a reference to the group. */
4137 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4138 struct group_dpif **group)
4140 struct ofgroup *ofgroup;
4143 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4144 *group = found ? group_dpif_cast(ofgroup) : NULL;
4150 group_dpif_get_buckets(const struct group_dpif *group,
4151 const struct ovs_list **buckets)
4153 *buckets = &group->up.buckets;
4156 enum ofp11_group_type
4157 group_dpif_get_type(const struct group_dpif *group)
4159 return group->up.type;
4162 /* Sends 'packet' out 'ofport'.
4163 * May modify 'packet'.
4164 * Returns 0 if successful, otherwise a positive errno value. */
4166 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
4168 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4171 error = xlate_send_packet(ofport, packet);
4173 ovs_mutex_lock(&ofproto->stats_mutex);
4174 ofproto->stats.tx_packets++;
4175 ofproto->stats.tx_bytes += ofpbuf_size(packet);
4176 ovs_mutex_unlock(&ofproto->stats_mutex);
4180 /* Return the version string of the datapath that backs up
4184 get_datapath_version(const struct ofproto *ofproto_)
4186 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4188 return ofproto->backer->dp_version_string;
4192 set_frag_handling(struct ofproto *ofproto_,
4193 enum ofp_config_flags frag_handling)
4195 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4196 if (frag_handling != OFPC_FRAG_REASM) {
4197 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4205 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
4206 const struct flow *flow,
4207 const struct ofpact *ofpacts, size_t ofpacts_len)
4209 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4211 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4212 ofpacts_len, packet);
4219 set_netflow(struct ofproto *ofproto_,
4220 const struct netflow_options *netflow_options)
4222 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4224 if (netflow_options) {
4225 if (!ofproto->netflow) {
4226 ofproto->netflow = netflow_create();
4227 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4229 return netflow_set_options(ofproto->netflow, netflow_options);
4230 } else if (ofproto->netflow) {
4231 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4232 netflow_unref(ofproto->netflow);
4233 ofproto->netflow = NULL;
4240 get_netflow_ids(const struct ofproto *ofproto_,
4241 uint8_t *engine_type, uint8_t *engine_id)
4243 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4245 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4248 static struct ofproto_dpif *
4249 ofproto_dpif_lookup(const char *name)
4251 struct ofproto_dpif *ofproto;
4253 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4254 hash_string(name, 0), &all_ofproto_dpifs) {
4255 if (!strcmp(ofproto->up.name, name)) {
4263 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4264 const char *argv[], void *aux OVS_UNUSED)
4266 struct ofproto_dpif *ofproto;
4269 ofproto = ofproto_dpif_lookup(argv[1]);
4271 unixctl_command_reply_error(conn, "no such bridge");
4274 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4275 mac_learning_flush(ofproto->ml);
4276 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4278 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4279 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4280 mac_learning_flush(ofproto->ml);
4281 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4285 unixctl_command_reply(conn, "table successfully flushed");
4289 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4290 const char *argv[], void *aux OVS_UNUSED)
4292 struct ofproto_dpif *ofproto;
4295 ofproto = ofproto_dpif_lookup(argv[1]);
4297 unixctl_command_reply_error(conn, "no such bridge");
4301 if (!mcast_snooping_enabled(ofproto->ms)) {
4302 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4305 mcast_snooping_mdb_flush(ofproto->ms);
4307 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4308 if (!mcast_snooping_enabled(ofproto->ms)) {
4311 mcast_snooping_mdb_flush(ofproto->ms);
4315 unixctl_command_reply(conn, "table successfully flushed");
4318 static struct ofport_dpif *
4319 ofbundle_get_a_port(const struct ofbundle *bundle)
4321 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
4326 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4327 const char *argv[], void *aux OVS_UNUSED)
4329 struct ds ds = DS_EMPTY_INITIALIZER;
4330 const struct ofproto_dpif *ofproto;
4331 const struct mac_entry *e;
4333 ofproto = ofproto_dpif_lookup(argv[1]);
4335 unixctl_command_reply_error(conn, "no such bridge");
4339 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4340 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4341 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4342 struct ofbundle *bundle = e->port.p;
4343 char name[OFP_MAX_PORT_NAME_LEN];
4345 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4347 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
4348 name, e->vlan, ETH_ADDR_ARGS(e->mac),
4349 mac_entry_age(ofproto->ml, e));
4351 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4352 unixctl_command_reply(conn, ds_cstr(&ds));
4357 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4358 int argc OVS_UNUSED,
4360 void *aux OVS_UNUSED)
4362 struct ds ds = DS_EMPTY_INITIALIZER;
4363 const struct ofproto_dpif *ofproto;
4364 const struct ofbundle *bundle;
4365 const struct mcast_group *grp;
4366 struct mcast_group_bundle *b;
4367 struct mcast_mrouter_bundle *mrouter;
4369 ofproto = ofproto_dpif_lookup(argv[1]);
4371 unixctl_command_reply_error(conn, "no such bridge");
4375 if (!mcast_snooping_enabled(ofproto->ms)) {
4376 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4380 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
4381 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4382 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4383 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
4384 char name[OFP_MAX_PORT_NAME_LEN];
4387 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4389 ds_put_format(&ds, "%5s %4d "IP_FMT" %3d\n",
4390 name, grp->vlan, IP_ARGS(grp->ip4),
4391 mcast_bundle_age(ofproto->ms, b));
4395 /* ports connected to multicast routers */
4396 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
4397 char name[OFP_MAX_PORT_NAME_LEN];
4399 bundle = mrouter->port;
4400 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4402 ds_put_format(&ds, "%5s %4d querier %3d\n",
4403 name, mrouter->vlan,
4404 mcast_mrouter_age(ofproto->ms, mrouter));
4406 ovs_rwlock_unlock(&ofproto->ms->rwlock);
4407 unixctl_command_reply(conn, ds_cstr(&ds));
4412 struct xlate_out xout;
4413 struct xlate_in xin;
4414 const struct flow *key;
4416 struct flow_wildcards wc;
4421 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4423 const struct rule_actions *actions;
4426 ds_put_char_multiple(result, '\t', level);
4428 ds_put_cstr(result, "No match\n");
4432 ovs_mutex_lock(&rule->up.mutex);
4433 cookie = rule->up.flow_cookie;
4434 ovs_mutex_unlock(&rule->up.mutex);
4436 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4437 rule ? rule->up.table_id : 0, ntohll(cookie));
4438 cls_rule_format(&rule->up.cr, result);
4439 ds_put_char(result, '\n');
4441 actions = rule_dpif_get_actions(rule);
4443 ds_put_char_multiple(result, '\t', level);
4444 ds_put_cstr(result, "OpenFlow actions=");
4445 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4446 ds_put_char(result, '\n');
4450 trace_format_flow(struct ds *result, int level, const char *title,
4451 struct trace_ctx *trace)
4453 ds_put_char_multiple(result, '\t', level);
4454 ds_put_format(result, "%s: ", title);
4455 /* Do not report unchanged flows for resubmits. */
4456 if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4457 || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4458 ds_put_cstr(result, "unchanged");
4460 flow_format(result, &trace->xin.flow);
4461 trace->flow = trace->xin.flow;
4463 ds_put_char(result, '\n');
4467 trace_format_regs(struct ds *result, int level, const char *title,
4468 struct trace_ctx *trace)
4472 ds_put_char_multiple(result, '\t', level);
4473 ds_put_format(result, "%s:", title);
4474 for (i = 0; i < FLOW_N_REGS; i++) {
4475 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4477 ds_put_char(result, '\n');
4481 trace_format_odp(struct ds *result, int level, const char *title,
4482 struct trace_ctx *trace)
4484 struct ofpbuf *odp_actions = trace->xout.odp_actions;
4486 ds_put_char_multiple(result, '\t', level);
4487 ds_put_format(result, "%s: ", title);
4488 format_odp_actions(result, ofpbuf_data(odp_actions),
4489 ofpbuf_size(odp_actions));
4490 ds_put_char(result, '\n');
4494 trace_format_megaflow(struct ds *result, int level, const char *title,
4495 struct trace_ctx *trace)
4499 ds_put_char_multiple(result, '\t', level);
4500 ds_put_format(result, "%s: ", title);
4501 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
4502 match_init(&match, trace->key, &trace->wc);
4503 match_format(&match, result, OFP_DEFAULT_PRIORITY);
4504 ds_put_char(result, '\n');
4507 static void trace_report(struct xlate_in *xin, const char *s, int recurse);
4510 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
4512 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4513 struct ds *result = trace->result;
4516 if (rule == xin->ofproto->miss_rule) {
4517 trace_report(xin, "No match, flow generates \"packet in\"s.",
4519 } else if (rule == xin->ofproto->no_packet_in_rule) {
4520 trace_report(xin, "No match, packets dropped because "
4521 "OFPPC_NO_PACKET_IN is set on in_port.", recurse);
4522 } else if (rule == xin->ofproto->drop_frags_rule) {
4523 trace_report(xin, "Packets dropped because they are IP "
4524 "fragments and the fragment handling mode is "
4525 "\"drop\".", recurse);
4529 ds_put_char(result, '\n');
4531 trace_format_flow(result, recurse, "Resubmitted flow", trace);
4532 trace_format_regs(result, recurse, "Resubmitted regs", trace);
4533 trace_format_odp(result, recurse, "Resubmitted odp", trace);
4534 trace_format_megaflow(result, recurse, "Resubmitted megaflow", trace);
4536 trace_format_rule(result, recurse, rule);
4540 trace_report(struct xlate_in *xin, const char *s, int recurse)
4542 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4543 struct ds *result = trace->result;
4545 ds_put_char_multiple(result, '\t', recurse);
4546 ds_put_cstr(result, s);
4547 ds_put_char(result, '\n');
4550 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4551 * forms are supported:
4553 * - [dpname] odp_flow [-generate | packet]
4554 * - bridge br_flow [-generate | packet]
4556 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4557 * returns a nonnull malloced error message. */
4558 static char * OVS_WARN_UNUSED_RESULT
4559 parse_flow_and_packet(int argc, const char *argv[],
4560 struct ofproto_dpif **ofprotop, struct flow *flow,
4561 struct ofpbuf **packetp)
4563 const struct dpif_backer *backer = NULL;
4564 const char *error = NULL;
4566 struct simap port_names = SIMAP_INITIALIZER(&port_names);
4567 struct ofpbuf *packet;
4568 struct ofpbuf odp_key;
4569 struct ofpbuf odp_mask;
4571 ofpbuf_init(&odp_key, 0);
4572 ofpbuf_init(&odp_mask, 0);
4574 /* Handle "-generate" or a hex string as the last argument. */
4575 if (!strcmp(argv[argc - 1], "-generate")) {
4576 packet = ofpbuf_new(0);
4579 error = eth_from_hex(argv[argc - 1], &packet);
4582 } else if (argc == 4) {
4583 /* The 3-argument form must end in "-generate' or a hex string. */
4589 /* odp_flow can have its in_port specified as a name instead of port no.
4590 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4591 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4592 * we need to create a simap of name to port no. */
4594 const char *dp_type;
4595 if (!strncmp(argv[1], "ovs-", 4)) {
4596 dp_type = argv[1] + 4;
4600 backer = shash_find_data(&all_dpif_backers, dp_type);
4601 } else if (argc == 2) {
4602 struct shash_node *node;
4603 if (shash_count(&all_dpif_backers) == 1) {
4604 node = shash_first(&all_dpif_backers);
4605 backer = node->data;
4608 error = "Syntax error";
4611 if (backer && backer->dpif) {
4612 struct dpif_port dpif_port;
4613 struct dpif_port_dump port_dump;
4614 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4615 simap_put(&port_names, dpif_port.name,
4616 odp_to_u32(dpif_port.port_no));
4620 /* Parse the flow and determine whether a datapath or
4621 * bridge is specified. If function odp_flow_key_from_string()
4622 * returns 0, the flow is a odp_flow. If function
4623 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4624 if (!odp_flow_from_string(argv[argc - 1], &port_names,
4625 &odp_key, &odp_mask)) {
4627 error = "Cannot find the datapath";
4631 if (odp_flow_key_to_flow(ofpbuf_data(&odp_key), ofpbuf_size(&odp_key),
4632 flow) == ODP_FIT_ERROR) {
4633 error = "Failed to parse flow key";
4637 *ofprotop = xlate_lookup_ofproto(backer, flow,
4638 &flow->in_port.ofp_port);
4639 if (*ofprotop == NULL) {
4640 error = "Invalid datapath flow";
4644 vsp_adjust_flow(*ofprotop, flow, NULL);
4647 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4650 m_err = xasprintf("Bad flow syntax: %s", err);
4655 error = "Must specify bridge name";
4659 *ofprotop = ofproto_dpif_lookup(argv[1]);
4661 error = "Unknown bridge name";
4667 /* Generate a packet, if requested. */
4669 if (!ofpbuf_size(packet)) {
4670 flow_compose(packet, flow);
4672 struct pkt_metadata md = pkt_metadata_from_flow(flow);
4674 /* Use the metadata from the flow and the packet argument
4675 * to reconstruct the flow. */
4676 flow_extract(packet, &md, flow);
4681 if (error && !m_err) {
4682 m_err = xstrdup(error);
4685 ofpbuf_delete(packet);
4689 ofpbuf_uninit(&odp_key);
4690 ofpbuf_uninit(&odp_mask);
4691 simap_destroy(&port_names);
4696 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4697 void *aux OVS_UNUSED)
4699 struct ofproto_dpif *ofproto;
4700 struct ofpbuf *packet;
4704 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4709 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4710 unixctl_command_reply(conn, ds_cstr(&result));
4711 ds_destroy(&result);
4712 ofpbuf_delete(packet);
4714 unixctl_command_reply_error(conn, error);
4720 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4721 const char *argv[], void *aux OVS_UNUSED)
4723 enum ofputil_protocol usable_protocols;
4724 struct ofproto_dpif *ofproto;
4725 bool enforce_consistency;
4726 struct ofpbuf ofpacts;
4727 struct ofpbuf *packet;
4732 /* Three kinds of error return values! */
4738 ofpbuf_init(&ofpacts, 0);
4740 /* Parse actions. */
4741 error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
4743 unixctl_command_reply_error(conn, error);
4748 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4749 * consistency between the flow and the actions. With -consistent, we
4750 * enforce consistency even for a flow supported in OpenFlow 1.0. */
4751 if (!strcmp(argv[1], "-consistent")) {
4752 enforce_consistency = true;
4756 enforce_consistency = false;
4759 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4761 unixctl_command_reply_error(conn, error);
4766 /* Do the same checks as handle_packet_out() in ofproto.c.
4768 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
4769 * strictly correct because these actions aren't in any table, but it's OK
4770 * because it 'table_id' is used only to check goto_table instructions, but
4771 * packet-outs take a list of actions and therefore it can't include
4774 * We skip the "meter" check here because meter is an instruction, not an
4775 * action, and thus cannot appear in ofpacts. */
4776 in_port = ofp_to_u16(flow.in_port.ofp_port);
4777 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
4778 unixctl_command_reply_error(conn, "invalid in_port");
4781 if (enforce_consistency) {
4782 retval = ofpacts_check_consistency(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
4783 &flow, u16_to_ofp(ofproto->up.max_ports),
4784 0, 0, usable_protocols);
4786 retval = ofpacts_check(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &flow,
4787 u16_to_ofp(ofproto->up.max_ports), 0, 0,
4793 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4794 unixctl_command_reply_error(conn, ds_cstr(&result));
4798 ofproto_trace(ofproto, &flow, packet,
4799 ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &result);
4800 unixctl_command_reply(conn, ds_cstr(&result));
4803 ds_destroy(&result);
4804 ofpbuf_delete(packet);
4805 ofpbuf_uninit(&ofpacts);
4808 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4809 * description of the results to 'ds'.
4811 * The trace follows a packet with the specified 'flow' through the flow
4812 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4813 * side effects (if it is nonnull then its flow must be 'flow').
4815 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4816 * trace, otherwise the actions are determined by a flow table lookup. */
4818 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4819 const struct ofpbuf *packet,
4820 const struct ofpact ofpacts[], size_t ofpacts_len,
4823 struct trace_ctx trace;
4825 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4826 ds_put_cstr(ds, "Flow: ");
4827 flow_format(ds, flow);
4828 ds_put_char(ds, '\n');
4830 flow_wildcards_init_catchall(&trace.wc);
4833 trace.key = flow; /* Original flow key, used for megaflow. */
4834 trace.flow = *flow; /* May be modified by actions. */
4835 xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
4836 ntohs(flow->tcp_flags), packet);
4837 trace.xin.ofpacts = ofpacts;
4838 trace.xin.ofpacts_len = ofpacts_len;
4839 trace.xin.resubmit_hook = trace_resubmit;
4840 trace.xin.report_hook = trace_report;
4842 xlate_actions(&trace.xin, &trace.xout);
4844 ds_put_char(ds, '\n');
4845 trace_format_flow(ds, 0, "Final flow", &trace);
4846 trace_format_megaflow(ds, 0, "Megaflow", &trace);
4848 ds_put_cstr(ds, "Datapath actions: ");
4849 format_odp_actions(ds, ofpbuf_data(trace.xout.odp_actions),
4850 ofpbuf_size(trace.xout.odp_actions));
4852 if (trace.xout.slow) {
4853 enum slow_path_reason slow;
4855 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4856 "slow path because it:");
4858 slow = trace.xout.slow;
4860 enum slow_path_reason bit = rightmost_1bit(slow);
4862 ds_put_format(ds, "\n\t- %s.",
4863 slow_path_reason_to_explanation(bit));
4869 xlate_out_uninit(&trace.xout);
4872 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4873 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4874 * to destroy 'ofproto_shash' and free the returned value. */
4875 static const struct shash_node **
4876 get_ofprotos(struct shash *ofproto_shash)
4878 const struct ofproto_dpif *ofproto;
4880 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4881 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4882 shash_add_nocopy(ofproto_shash, name, ofproto);
4885 return shash_sort(ofproto_shash);
4889 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4890 const char *argv[] OVS_UNUSED,
4891 void *aux OVS_UNUSED)
4893 struct ds ds = DS_EMPTY_INITIALIZER;
4894 struct shash ofproto_shash;
4895 const struct shash_node **sorted_ofprotos;
4898 shash_init(&ofproto_shash);
4899 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4900 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4901 const struct shash_node *node = sorted_ofprotos[i];
4902 ds_put_format(&ds, "%s\n", node->name);
4905 shash_destroy(&ofproto_shash);
4906 free(sorted_ofprotos);
4908 unixctl_command_reply(conn, ds_cstr(&ds));
4913 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4915 const struct shash_node **ofprotos;
4916 struct dpif_dp_stats dp_stats;
4917 struct shash ofproto_shash;
4920 dpif_get_dp_stats(backer->dpif, &dp_stats);
4922 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4923 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4925 shash_init(&ofproto_shash);
4926 ofprotos = get_ofprotos(&ofproto_shash);
4927 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4928 struct ofproto_dpif *ofproto = ofprotos[i]->data;
4929 const struct shash_node **ports;
4932 if (ofproto->backer != backer) {
4936 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4938 ports = shash_sort(&ofproto->up.port_by_name);
4939 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4940 const struct shash_node *node = ports[j];
4941 struct ofport *ofport = node->data;
4943 odp_port_t odp_port;
4945 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4948 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4949 if (odp_port != ODPP_NONE) {
4950 ds_put_format(ds, "%"PRIu32":", odp_port);
4952 ds_put_cstr(ds, "none:");
4955 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
4958 if (!netdev_get_config(ofport->netdev, &config)) {
4959 const struct smap_node **nodes;
4962 nodes = smap_sort(&config);
4963 for (i = 0; i < smap_count(&config); i++) {
4964 const struct smap_node *node = nodes[i];
4965 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4966 node->key, node->value);
4970 smap_destroy(&config);
4972 ds_put_char(ds, ')');
4973 ds_put_char(ds, '\n');
4977 shash_destroy(&ofproto_shash);
4982 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4983 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4985 struct ds ds = DS_EMPTY_INITIALIZER;
4986 const struct shash_node **backers;
4989 backers = shash_sort(&all_dpif_backers);
4990 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4991 dpif_show_backer(backers[i]->data, &ds);
4995 unixctl_command_reply(conn, ds_cstr(&ds));
5000 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5001 int argc OVS_UNUSED, const char *argv[],
5002 void *aux OVS_UNUSED)
5004 const struct ofproto_dpif *ofproto;
5006 struct ds ds = DS_EMPTY_INITIALIZER;
5007 bool verbosity = false;
5009 struct dpif_port dpif_port;
5010 struct dpif_port_dump port_dump;
5011 struct hmap portno_names;
5013 struct dpif_flow_dump *flow_dump;
5014 struct dpif_flow_dump_thread *flow_dump_thread;
5018 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5020 unixctl_command_reply_error(conn, "no such bridge");
5024 if (argc > 2 && !strcmp(argv[1], "-m")) {
5028 hmap_init(&portno_names);
5029 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5030 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5034 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
5035 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5036 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5039 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5040 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5045 odp_format_ufid(&f.ufid, &ds);
5046 ds_put_cstr(&ds, " ");
5048 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5049 &portno_names, &ds, verbosity);
5050 ds_put_cstr(&ds, ", ");
5051 dpif_flow_stats_format(&f.stats, &ds);
5052 ds_put_cstr(&ds, ", actions:");
5053 format_odp_actions(&ds, f.actions, f.actions_len);
5054 ds_put_char(&ds, '\n');
5056 dpif_flow_dump_thread_destroy(flow_dump_thread);
5057 error = dpif_flow_dump_destroy(flow_dump);
5061 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5062 unixctl_command_reply_error(conn, ds_cstr(&ds));
5064 unixctl_command_reply(conn, ds_cstr(&ds));
5066 odp_portno_names_destroy(&portno_names);
5067 hmap_destroy(&portno_names);
5072 ofproto_revalidate_all_backers(void)
5074 const struct shash_node **backers;
5077 backers = shash_sort(&all_dpif_backers);
5078 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5079 struct dpif_backer *backer = backers[i]->data;
5080 backer->need_revalidate = REV_RECONFIGURE;
5086 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5087 const char *argv[], void *aux OVS_UNUSED)
5089 if (!strcasecmp(argv[1], "off")) {
5090 ofproto_use_tnl_push_pop = false;
5091 unixctl_command_reply(conn, "Tunnel push-pop off");
5092 ofproto_revalidate_all_backers();
5093 } else if (!strcasecmp(argv[1], "on")) {
5094 ofproto_use_tnl_push_pop = true;
5095 unixctl_command_reply(conn, "Tunnel push-pop on");
5096 ofproto_revalidate_all_backers();
5101 ofproto_unixctl_init(void)
5103 static bool registered;
5109 unixctl_command_register(
5111 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5112 1, 3, ofproto_unixctl_trace, NULL);
5113 unixctl_command_register(
5114 "ofproto/trace-packet-out",
5115 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5116 2, 6, ofproto_unixctl_trace_actions, NULL);
5117 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5118 ofproto_unixctl_fdb_flush, NULL);
5119 unixctl_command_register("fdb/show", "bridge", 1, 1,
5120 ofproto_unixctl_fdb_show, NULL);
5121 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5122 ofproto_unixctl_mcast_snooping_flush, NULL);
5123 unixctl_command_register("mdb/show", "bridge", 1, 1,
5124 ofproto_unixctl_mcast_snooping_show, NULL);
5125 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5126 ofproto_unixctl_dpif_dump_dps, NULL);
5127 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5129 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5130 ofproto_unixctl_dpif_dump_flows, NULL);
5132 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5133 disable_tnl_push_pop, NULL);
5136 /* Returns true if 'table' is the table used for internal rules,
5137 * false otherwise. */
5139 table_is_internal(uint8_t table_id)
5141 return table_id == TBL_INTERNAL;
5144 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5146 * This is deprecated. It is only for compatibility with broken device drivers
5147 * in old versions of Linux that do not properly support VLANs when VLAN
5148 * devices are not used. When broken device drivers are no longer in
5149 * widespread use, we will delete these interfaces. */
5152 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
5154 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5155 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5157 if (realdev_ofp_port == ofport->realdev_ofp_port
5158 && vid == ofport->vlandev_vid) {
5162 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5164 if (ofport->realdev_ofp_port) {
5167 if (realdev_ofp_port && ofport->bundle) {
5168 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5169 * themselves be part of a bundle. */
5170 bundle_set(ofport_->ofproto, ofport->bundle, NULL);
5173 ofport->realdev_ofp_port = realdev_ofp_port;
5174 ofport->vlandev_vid = vid;
5176 if (realdev_ofp_port) {
5177 vsp_add(ofport, realdev_ofp_port, vid);
5184 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
5186 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
5190 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
5191 OVS_EXCLUDED(ofproto->vsp_mutex)
5193 /* hmap_is_empty is thread safe. */
5194 return !hmap_is_empty(&ofproto->realdev_vid_map);
5199 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
5200 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5201 OVS_REQUIRES(ofproto->vsp_mutex)
5203 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5204 int vid = vlan_tci_to_vid(vlan_tci);
5205 const struct vlan_splinter *vsp;
5207 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5208 hash_realdev_vid(realdev_ofp_port, vid),
5209 &ofproto->realdev_vid_map) {
5210 if (vsp->realdev_ofp_port == realdev_ofp_port
5211 && vsp->vid == vid) {
5212 return vsp->vlandev_ofp_port;
5216 return realdev_ofp_port;
5219 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5220 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5221 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
5222 * 'vlan_tci' 9, it would return the port number of eth0.9.
5224 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5225 * function just returns its 'realdev_ofp_port' argument. */
5227 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5228 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5229 OVS_EXCLUDED(ofproto->vsp_mutex)
5233 /* hmap_is_empty is thread safe, see if we can return immediately. */
5234 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
5235 return realdev_ofp_port;
5237 ovs_mutex_lock(&ofproto->vsp_mutex);
5238 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
5239 ovs_mutex_unlock(&ofproto->vsp_mutex);
5243 static struct vlan_splinter *
5244 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
5246 struct vlan_splinter *vsp;
5248 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
5249 hash_ofp_port(vlandev_ofp_port),
5250 &ofproto->vlandev_map) {
5251 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5259 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5260 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5261 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
5262 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5263 * eth0 and store 9 in '*vid'.
5265 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5266 * VLAN device. Unless VLAN splinters are enabled, this is what this function
5269 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5270 ofp_port_t vlandev_ofp_port, int *vid)
5271 OVS_REQUIRES(ofproto->vsp_mutex)
5273 if (!hmap_is_empty(&ofproto->vlandev_map)) {
5274 const struct vlan_splinter *vsp;
5276 vsp = vlandev_find(ofproto, vlandev_ofp_port);
5281 return vsp->realdev_ofp_port;
5287 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5288 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
5289 * 'flow->in_port' to the "real" device backing the VLAN device, sets
5290 * 'flow->vlan_tci' to the VLAN VID, and returns true. Optionally pushes the
5291 * appropriate VLAN on 'packet' if provided. Otherwise (which is always the
5292 * case unless VLAN splinters are enabled), returns false without making any
5295 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow,
5296 struct ofpbuf *packet)
5297 OVS_EXCLUDED(ofproto->vsp_mutex)
5302 /* hmap_is_empty is thread safe. */
5303 if (hmap_is_empty(&ofproto->vlandev_map)) {
5307 ovs_mutex_lock(&ofproto->vsp_mutex);
5308 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
5309 ovs_mutex_unlock(&ofproto->vsp_mutex);
5314 /* Cause the flow to be processed as if it came in on the real device with
5315 * the VLAN device's VLAN ID. */
5316 flow->in_port.ofp_port = realdev;
5317 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
5320 /* Make the packet resemble the flow, so that it gets sent to an
5321 * OpenFlow controller properly, so that it looks correct for sFlow,
5322 * and so that flow_extract() will get the correct vlan_tci if it is
5323 * called on 'packet'. */
5324 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
5331 vsp_remove(struct ofport_dpif *port)
5333 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5334 struct vlan_splinter *vsp;
5336 ovs_mutex_lock(&ofproto->vsp_mutex);
5337 vsp = vlandev_find(ofproto, port->up.ofp_port);
5339 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5340 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5343 port->realdev_ofp_port = 0;
5345 VLOG_ERR("missing vlan device record");
5347 ovs_mutex_unlock(&ofproto->vsp_mutex);
5351 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
5353 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5355 ovs_mutex_lock(&ofproto->vsp_mutex);
5356 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
5357 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
5358 == realdev_ofp_port)) {
5359 struct vlan_splinter *vsp;
5361 vsp = xmalloc(sizeof *vsp);
5362 vsp->realdev_ofp_port = realdev_ofp_port;
5363 vsp->vlandev_ofp_port = port->up.ofp_port;
5366 port->realdev_ofp_port = realdev_ofp_port;
5368 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
5369 hash_ofp_port(port->up.ofp_port));
5370 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
5371 hash_realdev_vid(realdev_ofp_port, vid));
5373 VLOG_ERR("duplicate vlan device record");
5375 ovs_mutex_unlock(&ofproto->vsp_mutex);
5379 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5381 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
5382 return ofport ? ofport->odp_port : ODPP_NONE;
5385 struct ofport_dpif *
5386 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5388 struct ofport_dpif *port;
5390 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5391 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5392 &backer->odp_to_ofport_map) {
5393 if (port->odp_port == odp_port) {
5394 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5399 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5404 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5406 struct ofport_dpif *port;
5408 port = odp_port_to_ofport(ofproto->backer, odp_port);
5409 if (port && &ofproto->up == port->up.ofproto) {
5410 return port->up.ofp_port;
5416 struct ofproto_dpif *
5417 ofproto_dpif_recirc_get_ofproto(const struct dpif_backer *backer,
5420 struct dpif_backer_recirc_node *node;
5422 node = CONTAINER_OF(cmap_find(&backer->recirc_map, recirc_id),
5423 struct dpif_backer_recirc_node, cmap_node);
5425 return node ? node->ofproto : NULL;
5429 ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto)
5431 struct dpif_backer *backer = ofproto->backer;
5432 uint32_t recirc_id = recirc_id_alloc(backer->rid_pool);
5435 struct dpif_backer_recirc_node *node = xmalloc(sizeof *node);
5437 node->recirc_id = recirc_id;
5438 node->ofproto = ofproto;
5440 ovs_mutex_lock(&backer->recirc_mutex);
5441 cmap_insert(&backer->recirc_map, &node->cmap_node, node->recirc_id);
5442 ovs_mutex_unlock(&backer->recirc_mutex);
5449 ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id)
5451 struct dpif_backer *backer = ofproto->backer;
5452 struct dpif_backer_recirc_node *node;
5454 node = CONTAINER_OF(cmap_find(&backer->recirc_map, recirc_id),
5455 struct dpif_backer_recirc_node, cmap_node);
5457 ovs_mutex_lock(&backer->recirc_mutex);
5458 cmap_remove(&backer->recirc_map, &node->cmap_node, node->recirc_id);
5459 ovs_mutex_unlock(&backer->recirc_mutex);
5460 recirc_id_free(backer->rid_pool, node->recirc_id);
5461 /* RCU postpone the free, since other threads may be referring
5462 * to 'node' at same time. */
5463 ovsrcu_postpone(free, node);
5468 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5469 const struct match *match, int priority,
5470 uint16_t idle_timeout,
5471 const struct ofpbuf *ofpacts,
5472 struct rule **rulep)
5474 struct ofputil_flow_mod fm;
5475 struct rule_dpif *rule;
5479 fm.priority = priority;
5480 fm.new_cookie = htonll(0);
5481 fm.cookie = htonll(0);
5482 fm.cookie_mask = htonll(0);
5483 fm.modify_cookie = false;
5484 fm.table_id = TBL_INTERNAL;
5485 fm.command = OFPFC_ADD;
5486 fm.idle_timeout = idle_timeout;
5487 fm.hard_timeout = 0;
5491 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5492 fm.ofpacts = ofpbuf_data(ofpacts);
5493 fm.ofpacts_len = ofpbuf_size(ofpacts);
5495 error = ofproto_flow_mod(&ofproto->up, &fm);
5497 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5498 ofperr_to_string(error));
5503 rule = rule_dpif_lookup_in_table(ofproto, TBL_INTERNAL, &fm.match.flow,
5504 &fm.match.wc, false);
5514 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5515 struct match *match, int priority)
5517 struct ofputil_flow_mod fm;
5521 fm.priority = priority;
5522 fm.new_cookie = htonll(0);
5523 fm.cookie = htonll(0);
5524 fm.cookie_mask = htonll(0);
5525 fm.modify_cookie = false;
5526 fm.table_id = TBL_INTERNAL;
5527 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5528 fm.command = OFPFC_DELETE_STRICT;
5530 error = ofproto_flow_mod(&ofproto->up, &fm);
5532 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5533 ofperr_to_string(error));
5540 const struct ofproto_class ofproto_dpif_class = {
5554 NULL, /* get_memory_usage. */
5555 type_get_memory_usage,
5573 port_is_lacp_current,
5574 port_get_lacp_stats,
5575 NULL, /* rule_choose_table */
5584 NULL, /* rule_premodify_actions */
5585 rule_modify_actions,
5601 get_stp_port_status,
5606 get_rstp_port_status,
5613 is_mirror_output_bundle,
5614 forward_bpdu_changed,
5615 set_mac_table_config,
5617 set_mcast_snooping_port,
5619 NULL, /* meter_get_features */
5620 NULL, /* meter_set */
5621 NULL, /* meter_get */
5622 NULL, /* meter_del */
5623 group_alloc, /* group_alloc */
5624 group_construct, /* group_construct */
5625 group_destruct, /* group_destruct */
5626 group_dealloc, /* group_dealloc */
5627 group_modify, /* group_modify */
5628 group_get_stats, /* group_get_stats */
5629 get_datapath_version, /* get_datapath_version */