2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 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"
34 #include "dynamic-string.h"
35 #include "fail-open.h"
36 #include "guarded-list.h"
40 #include "mac-learning.h"
41 #include "mcast-snooping.h"
42 #include "meta-flow.h"
43 #include "multipath.h"
44 #include "netdev-vport.h"
49 #include "odp-execute.h"
52 #include "ofp-actions.h"
53 #include "ofp-parse.h"
54 #include "ofp-print.h"
55 #include "ofproto-dpif-ipfix.h"
56 #include "ofproto-dpif-mirror.h"
57 #include "ofproto-dpif-monitor.h"
58 #include "ofproto-dpif-rid.h"
59 #include "ofproto-dpif-sflow.h"
60 #include "ofproto-dpif-upcall.h"
61 #include "ofproto-dpif-xlate.h"
62 #include "poll-loop.h"
64 #include "ovs-router.h"
70 #include "unaligned.h"
72 #include "vlan-bitmap.h"
73 #include "openvswitch/vlog.h"
75 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
77 COVERAGE_DEFINE(ofproto_dpif_expired);
78 COVERAGE_DEFINE(packet_in_overflow);
87 * - Do include packets and bytes from datapath flows which have not
88 * recently been processed by a revalidator. */
89 struct ovs_mutex stats_mutex;
90 struct dpif_flow_stats stats OVS_GUARDED;
92 /* In non-NULL, will point to a new rule (for which a reference is held) to
93 * which all the stats updates should be forwarded. This exists only
94 * transitionally when flows are replaced.
96 * Protected by stats_mutex. If both 'rule->stats_mutex' and
97 * 'rule->new_rule->stats_mutex' must be held together, acquire them in that
99 struct rule_dpif *new_rule OVS_GUARDED;
101 /* If non-zero then the recirculation id that has
102 * been allocated for use with this rule.
103 * The recirculation id and associated internal flow should
104 * be freed when the rule is freed */
108 /* RULE_CAST() depends on this. */
109 BUILD_ASSERT_DECL(offsetof(struct rule_dpif, up) == 0);
111 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
112 long long int *used);
113 static struct rule_dpif *rule_dpif_cast(const struct rule *);
114 static void rule_expire(struct rule_dpif *);
121 * - Do include packets and bytes from datapath flows which have not
122 * recently been processed by a revalidator. */
123 struct ovs_mutex stats_mutex;
124 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
125 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
129 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
130 struct ofproto_dpif *ofproto; /* Owning ofproto. */
131 void *aux; /* Key supplied by ofproto's client. */
132 char *name; /* Identifier for log messages. */
135 struct ovs_list ports; /* Contains "struct ofport"s. */
136 enum port_vlan_mode vlan_mode; /* VLAN mode */
137 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
138 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
139 * NULL if all VLANs are trunked. */
140 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
141 struct bond *bond; /* Nonnull iff more than one port. */
142 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
145 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
148 static void bundle_remove(struct ofport *);
149 static void bundle_update(struct ofbundle *);
150 static void bundle_destroy(struct ofbundle *);
151 static void bundle_del_port(struct ofport_dpif *);
152 static void bundle_run(struct ofbundle *);
153 static void bundle_wait(struct ofbundle *);
154 static void bundle_flush_macs(struct ofbundle *, bool);
155 static void bundle_move(struct ofbundle *, struct ofbundle *);
157 static void stp_run(struct ofproto_dpif *ofproto);
158 static void stp_wait(struct ofproto_dpif *ofproto);
159 static int set_stp_port(struct ofport *,
160 const struct ofproto_port_stp_settings *);
162 static void rstp_run(struct ofproto_dpif *ofproto);
163 static void set_rstp_port(struct ofport *,
164 const struct ofproto_port_rstp_settings *);
167 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
171 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
172 struct ovs_list bundle_node;/* In struct ofbundle's "ports" list. */
173 struct cfm *cfm; /* Connectivity Fault Management, if any. */
174 struct bfd *bfd; /* BFD, if any. */
175 struct lldp *lldp; /* lldp, if any. */
176 bool may_enable; /* May be enabled in bonds. */
177 bool is_tunnel; /* This port is a tunnel. */
178 bool is_layer3; /* This is a layer 3 port. */
179 long long int carrier_seq; /* Carrier status changes. */
180 struct ofport_dpif *peer; /* Peer if patch port. */
183 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
184 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
185 long long int stp_state_entered;
187 /* Rapid Spanning Tree. */
188 struct rstp_port *rstp_port; /* Rapid Spanning Tree Protocol, if any. */
189 enum rstp_state rstp_state; /* Always RSTP_DISABLED if RSTP not in use. */
191 /* Queue to DSCP mapping. */
192 struct ofproto_port_queue *qdscp;
195 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
197 * This is deprecated. It is only for compatibility with broken device
198 * drivers in old versions of Linux that do not properly support VLANs when
199 * VLAN devices are not used. When broken device drivers are no longer in
200 * widespread use, we will delete these interfaces. */
201 ofp_port_t realdev_ofp_port;
205 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
207 * This is deprecated. It is only for compatibility with broken device drivers
208 * in old versions of Linux that do not properly support VLANs when VLAN
209 * devices are not used. When broken device drivers are no longer in
210 * widespread use, we will delete these interfaces. */
211 struct vlan_splinter {
212 struct hmap_node realdev_vid_node;
213 struct hmap_node vlandev_node;
214 ofp_port_t realdev_ofp_port;
215 ofp_port_t vlandev_ofp_port;
219 static void vsp_remove(struct ofport_dpif *);
220 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
222 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
225 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
228 static struct ofport_dpif *
229 ofport_dpif_cast(const struct ofport *ofport)
231 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
234 static void port_run(struct ofport_dpif *);
235 static int set_bfd(struct ofport *, const struct smap *);
236 static int set_cfm(struct ofport *, const struct cfm_settings *);
237 static int set_lldp(struct ofport *ofport_, const struct smap *cfg);
238 static void ofport_update_peer(struct ofport_dpif *);
240 /* Reasons that we might need to revalidate every datapath flow, and
241 * corresponding coverage counters.
243 * A value of 0 means that there is no need to revalidate.
245 * It would be nice to have some cleaner way to integrate with coverage
246 * counters, but with only a few reasons I guess this is good enough for
248 enum revalidate_reason {
249 REV_RECONFIGURE = 1, /* Switch configuration changed. */
250 REV_STP, /* Spanning tree protocol port status change. */
251 REV_RSTP, /* RSTP port status change. */
252 REV_BOND, /* Bonding changed. */
253 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
254 REV_FLOW_TABLE, /* Flow table changed. */
255 REV_MAC_LEARNING, /* Mac learning changed. */
256 REV_MCAST_SNOOPING, /* Multicast snooping changed. */
258 COVERAGE_DEFINE(rev_reconfigure);
259 COVERAGE_DEFINE(rev_stp);
260 COVERAGE_DEFINE(rev_rstp);
261 COVERAGE_DEFINE(rev_bond);
262 COVERAGE_DEFINE(rev_port_toggled);
263 COVERAGE_DEFINE(rev_flow_table);
264 COVERAGE_DEFINE(rev_mac_learning);
265 COVERAGE_DEFINE(rev_mcast_snooping);
267 /* All datapaths of a given type share a single dpif backer instance. */
274 struct ovs_rwlock odp_to_ofport_lock;
275 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
277 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
279 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
281 bool recv_set_enable; /* Enables or disables receiving packets. */
283 /* Version string of the datapath stored in OVSDB. */
284 char *dp_version_string;
286 /* Datapath feature support. */
287 struct dpif_backer_support support;
288 struct atomic_count tnl_count;
291 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
292 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
294 struct ofproto_dpif {
295 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
297 struct dpif_backer *backer;
299 /* Unique identifier for this instantiation of this bridge in this running
303 ATOMIC(cls_version_t) tables_version; /* For classifier lookups. */
305 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
307 /* Special OpenFlow rules. */
308 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
309 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
310 struct rule_dpif *drop_frags_rule; /* Used in OFPUTIL_FRAG_DROP mode. */
313 struct netflow *netflow;
314 struct dpif_sflow *sflow;
315 struct dpif_ipfix *ipfix;
316 struct hmap bundles; /* Contains "struct ofbundle"s. */
317 struct mac_learning *ml;
318 struct mcast_snooping *ms;
319 bool has_bonded_bundles;
321 struct mbridge *mbridge;
323 struct ovs_mutex stats_mutex;
324 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
325 * consumed in userspace. */
329 long long int stp_last_tick;
331 /* Rapid Spanning Tree. */
333 long long int rstp_last_tick;
335 /* VLAN splinters. */
336 struct ovs_mutex vsp_mutex;
337 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
338 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
341 struct sset ports; /* Set of standard port names. */
342 struct sset ghost_ports; /* Ports with no datapath port. */
343 struct sset port_poll_set; /* Queued names for port_poll() reply. */
344 int port_poll_errno; /* Last errno for port_poll() reply. */
345 uint64_t change_seq; /* Connectivity status changes. */
348 struct guarded_list ams; /* Contains "struct ofproto_async_msgs"s. */
349 struct seq *ams_seq; /* For notifying 'ams' reception. */
353 /* All existing ofproto_dpif instances, indexed by ->up.name. */
354 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
356 static bool ofproto_use_tnl_push_pop = true;
357 static void ofproto_unixctl_init(void);
359 static inline struct ofproto_dpif *
360 ofproto_dpif_cast(const struct ofproto *ofproto)
362 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
363 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
367 ofproto_dpif_get_enable_ufid(const struct dpif_backer *backer)
369 return backer->support.ufid;
372 struct dpif_backer_support *
373 ofproto_dpif_get_support(const struct ofproto_dpif *ofproto)
375 return &ofproto->backer->support;
378 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
379 const struct dp_packet *packet,
380 const struct ofpact[], size_t ofpacts_len,
383 /* Global variables. */
384 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
386 /* Initial mappings of port to bridge mappings. */
387 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
389 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
392 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
393 const struct ofputil_flow_mod *fm)
395 struct ofproto_flow_mod ofm;
397 /* Multiple threads may do this for the same 'fm' at the same time.
398 * Allocate ofproto_flow_mod with execution context from stack.
400 * Note: This copy could be avoided by making ofproto_flow_mod more
401 * complex, but that may not be desireable, and a learn action is not that
402 * fast to begin with. */
404 ofproto_flow_mod(&ofproto->up, &ofm);
407 /* Appends 'am' to the queue of asynchronous messages to be sent to the
408 * controller. Takes ownership of 'am' and any data it points to. */
410 ofproto_dpif_send_async_msg(struct ofproto_dpif *ofproto,
411 struct ofproto_async_msg *am)
413 if (!guarded_list_push_back(&ofproto->ams, &am->list_node, 1024)) {
414 COVERAGE_INC(packet_in_overflow);
415 ofproto_async_msg_free(am);
418 /* Wakes up main thread for packet-in I/O. */
419 seq_change(ofproto->ams_seq);
422 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
423 * packet rather than to send the packet to the controller.
425 * This function returns false to indicate that a packet_in message
426 * for a "table-miss" should be sent to at least one controller.
427 * False otherwise. */
429 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
431 return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
434 /* Factory functions. */
437 init(const struct shash *iface_hints)
439 struct shash_node *node;
441 /* Make a local copy, since we don't own 'iface_hints' elements. */
442 SHASH_FOR_EACH(node, iface_hints) {
443 const struct iface_hint *orig_hint = node->data;
444 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
446 new_hint->br_name = xstrdup(orig_hint->br_name);
447 new_hint->br_type = xstrdup(orig_hint->br_type);
448 new_hint->ofp_port = orig_hint->ofp_port;
450 shash_add(&init_ofp_ports, node->name, new_hint);
453 ofproto_unixctl_init();
458 enumerate_types(struct sset *types)
460 dp_enumerate_types(types);
464 enumerate_names(const char *type, struct sset *names)
466 struct ofproto_dpif *ofproto;
469 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
470 if (strcmp(type, ofproto->up.type)) {
473 sset_add(names, ofproto->up.name);
480 del(const char *type, const char *name)
485 error = dpif_open(name, type, &dpif);
487 error = dpif_delete(dpif);
494 port_open_type(const char *datapath_type, const char *port_type)
496 return dpif_port_open_type(datapath_type, port_type);
499 /* Type functions. */
501 static void process_dpif_port_changes(struct dpif_backer *);
502 static void process_dpif_all_ports_changed(struct dpif_backer *);
503 static void process_dpif_port_change(struct dpif_backer *,
504 const char *devname);
505 static void process_dpif_port_error(struct dpif_backer *, int error);
507 static struct ofproto_dpif *
508 lookup_ofproto_dpif_by_port_name(const char *name)
510 struct ofproto_dpif *ofproto;
512 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
513 if (sset_contains(&ofproto->ports, name)) {
522 ofproto_dpif_backer_enabled(struct dpif_backer* backer)
524 return backer->recv_set_enable;
528 type_run(const char *type)
530 struct dpif_backer *backer;
532 backer = shash_find_data(&all_dpif_backers, type);
534 /* This is not necessarily a problem, since backers are only
535 * created on demand. */
540 if (dpif_run(backer->dpif)) {
541 backer->need_revalidate = REV_RECONFIGURE;
544 udpif_run(backer->udpif);
546 /* If vswitchd started with other_config:flow_restore_wait set as "true",
547 * and the configuration has now changed to "false", enable receiving
548 * packets from the datapath. */
549 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
552 backer->recv_set_enable = true;
554 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
556 VLOG_ERR("Failed to enable receiving packets in dpif.");
559 dpif_flow_flush(backer->dpif);
560 backer->need_revalidate = REV_RECONFIGURE;
563 if (backer->recv_set_enable) {
564 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
567 dpif_poll_threads_set(backer->dpif, pmd_cpu_mask);
569 if (backer->need_revalidate) {
570 struct ofproto_dpif *ofproto;
571 struct simap_node *node;
572 struct simap tmp_backers;
574 /* Handle tunnel garbage collection. */
575 simap_init(&tmp_backers);
576 simap_swap(&backer->tnl_backers, &tmp_backers);
578 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
579 struct ofport_dpif *iter;
581 if (backer != ofproto->backer) {
585 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
586 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
589 if (!iter->is_tunnel) {
593 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
594 namebuf, sizeof namebuf);
595 node = simap_find(&tmp_backers, dp_port);
597 simap_put(&backer->tnl_backers, dp_port, node->data);
598 simap_delete(&tmp_backers, node);
599 node = simap_find(&backer->tnl_backers, dp_port);
601 node = simap_find(&backer->tnl_backers, dp_port);
603 odp_port_t odp_port = ODPP_NONE;
605 if (!dpif_port_add(backer->dpif, iter->up.netdev,
607 simap_put(&backer->tnl_backers, dp_port,
608 odp_to_u32(odp_port));
609 node = simap_find(&backer->tnl_backers, dp_port);
614 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
615 if (tnl_port_reconfigure(iter, iter->up.netdev,
617 ovs_native_tunneling_is_on(ofproto), dp_port)) {
618 backer->need_revalidate = REV_RECONFIGURE;
623 SIMAP_FOR_EACH (node, &tmp_backers) {
624 dpif_port_del(backer->dpif, u32_to_odp(node->data));
626 simap_destroy(&tmp_backers);
628 switch (backer->need_revalidate) {
629 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
630 case REV_STP: COVERAGE_INC(rev_stp); break;
631 case REV_RSTP: COVERAGE_INC(rev_rstp); break;
632 case REV_BOND: COVERAGE_INC(rev_bond); break;
633 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
634 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
635 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
636 case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
638 backer->need_revalidate = 0;
640 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
641 struct ofport_dpif *ofport;
642 struct ofbundle *bundle;
644 if (ofproto->backer != backer) {
649 xlate_ofproto_set(ofproto, ofproto->up.name,
650 ofproto->backer->dpif, ofproto->ml,
651 ofproto->stp, ofproto->rstp, ofproto->ms,
652 ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
654 ofproto->up.forward_bpdu,
655 connmgr_has_in_band(ofproto->up.connmgr),
656 &ofproto->backer->support);
658 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
659 xlate_bundle_set(ofproto, bundle, bundle->name,
660 bundle->vlan_mode, bundle->vlan,
661 bundle->trunks, bundle->use_priority_tags,
662 bundle->bond, bundle->lacp,
666 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
667 int stp_port = ofport->stp_port
668 ? stp_port_no(ofport->stp_port)
670 xlate_ofport_set(ofproto, ofport->bundle, ofport,
671 ofport->up.ofp_port, ofport->odp_port,
672 ofport->up.netdev, ofport->cfm, ofport->bfd,
673 ofport->lldp, ofport->peer, stp_port,
674 ofport->rstp_port, ofport->qdscp,
675 ofport->n_qdscp, ofport->up.pp.config,
676 ofport->up.pp.state, ofport->is_tunnel,
682 udpif_revalidate(backer->udpif);
685 process_dpif_port_changes(backer);
690 /* Check for and handle port changes in 'backer''s dpif. */
692 process_dpif_port_changes(struct dpif_backer *backer)
698 error = dpif_port_poll(backer->dpif, &devname);
704 process_dpif_all_ports_changed(backer);
708 process_dpif_port_change(backer, devname);
713 process_dpif_port_error(backer, error);
720 process_dpif_all_ports_changed(struct dpif_backer *backer)
722 struct ofproto_dpif *ofproto;
723 struct dpif_port dpif_port;
724 struct dpif_port_dump dump;
725 struct sset devnames;
728 sset_init(&devnames);
729 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
730 if (ofproto->backer == backer) {
731 struct ofport *ofport;
733 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
734 sset_add(&devnames, netdev_get_name(ofport->netdev));
738 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
739 sset_add(&devnames, dpif_port.name);
742 SSET_FOR_EACH (devname, &devnames) {
743 process_dpif_port_change(backer, devname);
745 sset_destroy(&devnames);
749 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
751 struct ofproto_dpif *ofproto;
752 struct dpif_port port;
754 /* Don't report on the datapath's device. */
755 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
759 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
760 &all_ofproto_dpifs) {
761 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
766 ofproto = lookup_ofproto_dpif_by_port_name(devname);
767 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
768 /* The port was removed. If we know the datapath,
769 * report it through poll_set(). If we don't, it may be
770 * notifying us of a removal we initiated, so ignore it.
771 * If there's a pending ENOBUFS, let it stand, since
772 * everything will be reevaluated. */
773 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
774 sset_add(&ofproto->port_poll_set, devname);
775 ofproto->port_poll_errno = 0;
777 } else if (!ofproto) {
778 /* The port was added, but we don't know with which
779 * ofproto we should associate it. Delete it. */
780 dpif_port_del(backer->dpif, port.port_no);
782 struct ofport_dpif *ofport;
784 ofport = ofport_dpif_cast(shash_find_data(
785 &ofproto->up.port_by_name, devname));
787 && ofport->odp_port != port.port_no
788 && !odp_port_to_ofport(backer, port.port_no))
790 /* 'ofport''s datapath port number has changed from
791 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
792 * structures to match. */
793 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
794 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
795 ofport->odp_port = port.port_no;
796 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
797 hash_odp_port(port.port_no));
798 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
799 backer->need_revalidate = REV_RECONFIGURE;
802 dpif_port_destroy(&port);
805 /* Propagate 'error' to all ofprotos based on 'backer'. */
807 process_dpif_port_error(struct dpif_backer *backer, int error)
809 struct ofproto_dpif *ofproto;
811 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
812 if (ofproto->backer == backer) {
813 sset_clear(&ofproto->port_poll_set);
814 ofproto->port_poll_errno = error;
820 type_wait(const char *type)
822 struct dpif_backer *backer;
824 backer = shash_find_data(&all_dpif_backers, type);
826 /* This is not necessarily a problem, since backers are only
827 * created on demand. */
831 dpif_wait(backer->dpif);
834 /* Basic life-cycle. */
836 static int add_internal_flows(struct ofproto_dpif *);
838 static struct ofproto *
841 struct ofproto_dpif *ofproto = xzalloc(sizeof *ofproto);
846 dealloc(struct ofproto *ofproto_)
848 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
853 close_dpif_backer(struct dpif_backer *backer)
855 ovs_assert(backer->refcount > 0);
857 if (--backer->refcount) {
861 udpif_destroy(backer->udpif);
863 simap_destroy(&backer->tnl_backers);
864 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
865 hmap_destroy(&backer->odp_to_ofport_map);
866 shash_find_and_delete(&all_dpif_backers, backer->type);
868 free(backer->dp_version_string);
869 dpif_close(backer->dpif);
873 /* Datapath port slated for removal from datapath. */
875 struct ovs_list list_node;
879 static bool check_variable_length_userdata(struct dpif_backer *backer);
880 static void check_support(struct dpif_backer *backer);
883 open_dpif_backer(const char *type, struct dpif_backer **backerp)
885 struct dpif_backer *backer;
886 struct dpif_port_dump port_dump;
887 struct dpif_port port;
888 struct shash_node *node;
889 struct ovs_list garbage_list;
890 struct odp_garbage *garbage;
899 backer = shash_find_data(&all_dpif_backers, type);
906 backer_name = xasprintf("ovs-%s", type);
908 /* Remove any existing datapaths, since we assume we're the only
909 * userspace controlling the datapath. */
911 dp_enumerate_names(type, &names);
912 SSET_FOR_EACH(name, &names) {
913 struct dpif *old_dpif;
915 /* Don't remove our backer if it exists. */
916 if (!strcmp(name, backer_name)) {
920 if (dpif_open(name, type, &old_dpif)) {
921 VLOG_WARN("couldn't open old datapath %s to remove it", name);
923 dpif_delete(old_dpif);
924 dpif_close(old_dpif);
927 sset_destroy(&names);
929 backer = xmalloc(sizeof *backer);
931 error = dpif_create_and_open(backer_name, type, &backer->dpif);
934 VLOG_ERR("failed to open datapath of type %s: %s", type,
935 ovs_strerror(error));
939 backer->udpif = udpif_create(backer, backer->dpif);
941 backer->type = xstrdup(type);
942 backer->refcount = 1;
943 hmap_init(&backer->odp_to_ofport_map);
944 ovs_rwlock_init(&backer->odp_to_ofport_lock);
945 backer->need_revalidate = 0;
946 simap_init(&backer->tnl_backers);
947 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
950 if (backer->recv_set_enable) {
951 dpif_flow_flush(backer->dpif);
954 /* Loop through the ports already on the datapath and remove any
955 * that we don't need anymore. */
956 list_init(&garbage_list);
957 dpif_port_dump_start(&port_dump, backer->dpif);
958 while (dpif_port_dump_next(&port_dump, &port)) {
959 node = shash_find(&init_ofp_ports, port.name);
960 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
961 garbage = xmalloc(sizeof *garbage);
962 garbage->odp_port = port.port_no;
963 list_push_front(&garbage_list, &garbage->list_node);
966 dpif_port_dump_done(&port_dump);
968 LIST_FOR_EACH_POP (garbage, list_node, &garbage_list) {
969 dpif_port_del(backer->dpif, garbage->odp_port);
973 shash_add(&all_dpif_backers, type, backer);
975 check_support(backer);
976 atomic_count_init(&backer->tnl_count, 0);
978 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
980 VLOG_ERR("failed to listen on datapath of type %s: %s",
981 type, ovs_strerror(error));
982 close_dpif_backer(backer);
986 if (backer->recv_set_enable) {
987 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
990 /* This check fails if performed before udpif threads have been set,
991 * as the kernel module checks that the 'pid' in userspace action
993 backer->support.variable_length_userdata
994 = check_variable_length_userdata(backer);
995 backer->dp_version_string = dpif_get_dp_version(backer->dpif);
1001 ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
1003 return ofproto_use_tnl_push_pop && ofproto->backer->support.tnl_push_pop &&
1004 atomic_count_get(&ofproto->backer->tnl_count);
1007 /* Tests whether 'backer''s datapath supports recirculation. Only newer
1008 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
1009 * features on older datapaths that don't support this feature.
1011 * Returns false if 'backer' definitely does not support recirculation, true if
1012 * it seems to support recirculation or if at least the error we get is
1015 check_recirc(struct dpif_backer *backer)
1018 struct odputil_keybuf keybuf;
1021 struct odp_flow_key_parms odp_parms = {
1028 memset(&flow, 0, sizeof flow);
1032 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1033 odp_flow_key_from_flow(&odp_parms, &key);
1034 enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
1037 if (enable_recirc) {
1038 VLOG_INFO("%s: Datapath supports recirculation",
1039 dpif_name(backer->dpif));
1041 VLOG_INFO("%s: Datapath does not support recirculation",
1042 dpif_name(backer->dpif));
1045 return enable_recirc;
1048 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
1049 * some flow attributes for datapaths that support this feature.
1051 * Returns true if 'dpif' supports UFID for flow operations.
1052 * Returns false if 'dpif' does not support UFID. */
1054 check_ufid(struct dpif_backer *backer)
1057 struct odputil_keybuf keybuf;
1061 struct odp_flow_key_parms odp_parms = {
1065 memset(&flow, 0, sizeof flow);
1066 flow.dl_type = htons(0x1234);
1068 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1069 odp_flow_key_from_flow(&odp_parms, &key);
1070 dpif_flow_hash(backer->dpif, key.data, key.size, &ufid);
1072 enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, &ufid);
1075 VLOG_INFO("%s: Datapath supports unique flow ids",
1076 dpif_name(backer->dpif));
1078 VLOG_INFO("%s: Datapath does not support unique flow ids",
1079 dpif_name(backer->dpif));
1084 /* Tests whether 'backer''s datapath supports variable-length
1085 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1086 * to disable some features on older datapaths that don't support this
1089 * Returns false if 'backer' definitely does not support variable-length
1090 * userdata, true if it seems to support them or if at least the error we get
1093 check_variable_length_userdata(struct dpif_backer *backer)
1095 struct eth_header *eth;
1096 struct ofpbuf actions;
1097 struct dpif_execute execute;
1098 struct dp_packet packet;
1102 /* Compose a userspace action that will cause an ERANGE error on older
1103 * datapaths that don't support variable-length userdata.
1105 * We really test for using userdata longer than 8 bytes, but older
1106 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1107 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1108 * test for that instead as a proxy for longer userdata support. */
1109 ofpbuf_init(&actions, 64);
1110 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1111 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1112 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1113 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1114 nl_msg_end_nested(&actions, start);
1116 /* Compose a dummy ethernet packet. */
1117 dp_packet_init(&packet, ETH_HEADER_LEN);
1118 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1119 eth->eth_type = htons(0x1234);
1121 /* Execute the actions. On older datapaths this fails with ERANGE, on
1122 * newer datapaths it succeeds. */
1123 execute.actions = actions.data;
1124 execute.actions_len = actions.size;
1125 execute.packet = &packet;
1126 execute.needs_help = false;
1127 execute.probe = true;
1130 error = dpif_execute(backer->dpif, &execute);
1132 dp_packet_uninit(&packet);
1133 ofpbuf_uninit(&actions);
1140 /* Variable-length userdata is not supported. */
1141 VLOG_WARN("%s: datapath does not support variable-length userdata "
1142 "feature (needs Linux 3.10+ or kernel module from OVS "
1143 "1..11+). The NXAST_SAMPLE action will be ignored.",
1144 dpif_name(backer->dpif));
1148 /* Something odd happened. We're not sure whether variable-length
1149 * userdata is supported. Default to "yes". */
1150 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1151 dpif_name(backer->dpif), ovs_strerror(error));
1156 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1158 * Returns the number of elements in a struct flow's mpls_lse field
1159 * if the datapath supports at least that many entries in an
1161 * Otherwise returns the number of MPLS push actions supported by
1164 check_max_mpls_depth(struct dpif_backer *backer)
1169 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1170 struct odputil_keybuf keybuf;
1172 struct odp_flow_key_parms odp_parms = {
1176 memset(&flow, 0, sizeof flow);
1177 flow.dl_type = htons(ETH_TYPE_MPLS);
1178 flow_set_mpls_bos(&flow, n, 1);
1180 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1181 odp_flow_key_from_flow(&odp_parms, &key);
1182 if (!dpif_probe_feature(backer->dpif, "MPLS", &key, NULL)) {
1187 VLOG_INFO("%s: MPLS label stack length probed as %d",
1188 dpif_name(backer->dpif), n);
1192 /* Tests whether 'backer''s datapath supports masked data in
1193 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1194 * datapaths that don't support this feature. */
1196 check_masked_set_action(struct dpif_backer *backer)
1198 struct eth_header *eth;
1199 struct ofpbuf actions;
1200 struct dpif_execute execute;
1201 struct dp_packet packet;
1203 struct ovs_key_ethernet key, mask;
1205 /* Compose a set action that will cause an EINVAL error on older
1206 * datapaths that don't support masked set actions.
1207 * Avoid using a full mask, as it could be translated to a non-masked
1208 * set action instead. */
1209 ofpbuf_init(&actions, 64);
1210 memset(&key, 0x53, sizeof key);
1211 memset(&mask, 0x7f, sizeof mask);
1212 commit_masked_set_action(&actions, OVS_KEY_ATTR_ETHERNET, &key, &mask,
1215 /* Compose a dummy ethernet packet. */
1216 dp_packet_init(&packet, ETH_HEADER_LEN);
1217 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1218 eth->eth_type = htons(0x1234);
1220 /* Execute the actions. On older datapaths this fails with EINVAL, on
1221 * newer datapaths it succeeds. */
1222 execute.actions = actions.data;
1223 execute.actions_len = actions.size;
1224 execute.packet = &packet;
1225 execute.needs_help = false;
1226 execute.probe = true;
1229 error = dpif_execute(backer->dpif, &execute);
1231 dp_packet_uninit(&packet);
1232 ofpbuf_uninit(&actions);
1235 /* Masked set action is not supported. */
1236 VLOG_INFO("%s: datapath does not support masked set action feature.",
1237 dpif_name(backer->dpif));
1242 #define CHECK_FEATURE__(NAME, SUPPORT, FIELD, VALUE) \
1244 check_##NAME(struct dpif_backer *backer) \
1247 struct odputil_keybuf keybuf; \
1248 struct ofpbuf key; \
1250 struct odp_flow_key_parms odp_parms = { \
1257 memset(&flow, 0, sizeof flow); \
1258 flow.FIELD = VALUE; \
1260 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); \
1261 odp_flow_key_from_flow(&odp_parms, &key); \
1262 enable = dpif_probe_feature(backer->dpif, #NAME, &key, NULL); \
1265 VLOG_INFO("%s: Datapath supports "#NAME, dpif_name(backer->dpif)); \
1267 VLOG_INFO("%s: Datapath does not support "#NAME, \
1268 dpif_name(backer->dpif)); \
1273 #define CHECK_FEATURE(FIELD) CHECK_FEATURE__(FIELD, FIELD, FIELD, 1)
1275 CHECK_FEATURE(ct_state)
1276 CHECK_FEATURE(ct_zone)
1277 CHECK_FEATURE(ct_mark)
1278 CHECK_FEATURE__(ct_label, ct_label, ct_label.u64.lo, 1)
1279 CHECK_FEATURE__(ct_state_nat, ct_state, ct_state, CS_TRACKED|CS_SRC_NAT)
1281 #undef CHECK_FEATURE
1282 #undef CHECK_FEATURE__
1285 check_support(struct dpif_backer *backer)
1287 /* This feature needs to be tested after udpif threads are set. */
1288 backer->support.variable_length_userdata = false;
1290 backer->support.odp.recirc = check_recirc(backer);
1291 backer->support.odp.max_mpls_depth = check_max_mpls_depth(backer);
1292 backer->support.masked_set_action = check_masked_set_action(backer);
1293 backer->support.ufid = check_ufid(backer);
1294 backer->support.tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
1296 backer->support.odp.ct_state = check_ct_state(backer);
1297 backer->support.odp.ct_zone = check_ct_zone(backer);
1298 backer->support.odp.ct_mark = check_ct_mark(backer);
1299 backer->support.odp.ct_label = check_ct_label(backer);
1301 backer->support.odp.ct_state_nat = check_ct_state_nat(backer);
1305 construct(struct ofproto *ofproto_)
1307 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1308 struct shash_node *node, *next;
1311 /* Tunnel module can get used right after the udpif threads are running. */
1312 ofproto_tunnel_init();
1314 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1319 uuid_generate(&ofproto->uuid);
1320 atomic_init(&ofproto->tables_version, CLS_MIN_VERSION);
1321 ofproto->netflow = NULL;
1322 ofproto->sflow = NULL;
1323 ofproto->ipfix = NULL;
1324 ofproto->stp = NULL;
1325 ofproto->rstp = NULL;
1326 ofproto->dump_seq = 0;
1327 hmap_init(&ofproto->bundles);
1328 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1330 ofproto->mbridge = mbridge_create();
1331 ofproto->has_bonded_bundles = false;
1332 ofproto->lacp_enabled = false;
1333 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1334 ovs_mutex_init(&ofproto->vsp_mutex);
1336 guarded_list_init(&ofproto->ams);
1338 hmap_init(&ofproto->vlandev_map);
1339 hmap_init(&ofproto->realdev_vid_map);
1341 sset_init(&ofproto->ports);
1342 sset_init(&ofproto->ghost_ports);
1343 sset_init(&ofproto->port_poll_set);
1344 ofproto->port_poll_errno = 0;
1345 ofproto->change_seq = 0;
1346 ofproto->ams_seq = seq_create();
1347 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
1350 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1351 struct iface_hint *iface_hint = node->data;
1353 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1354 /* Check if the datapath already has this port. */
1355 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1356 sset_add(&ofproto->ports, node->name);
1359 free(iface_hint->br_name);
1360 free(iface_hint->br_type);
1362 shash_delete(&init_ofp_ports, node);
1366 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1367 hash_string(ofproto->up.name, 0));
1368 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1370 ofproto_init_tables(ofproto_, N_TABLES);
1371 error = add_internal_flows(ofproto);
1373 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1379 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1380 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1386 match_init_catchall(&match);
1387 match_set_reg(&match, 0, id);
1389 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1391 *rulep = error ? NULL : rule_dpif_cast(rule);
1397 add_internal_flows(struct ofproto_dpif *ofproto)
1399 struct ofpact_controller *controller;
1400 uint64_t ofpacts_stub[128 / 8];
1401 struct ofpbuf ofpacts;
1402 struct rule *unused_rulep OVS_UNUSED;
1407 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1410 controller = ofpact_put_CONTROLLER(&ofpacts);
1411 controller->max_len = UINT16_MAX;
1412 controller->controller_id = 0;
1413 controller->reason = OFPR_IMPLICIT_MISS;
1414 ofpact_finish(&ofpacts, &controller->ofpact);
1416 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1417 &ofproto->miss_rule);
1422 ofpbuf_clear(&ofpacts);
1423 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1424 &ofproto->no_packet_in_rule);
1429 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1430 &ofproto->drop_frags_rule);
1435 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1436 * zero when reaching this rule.
1438 * (priority=2), recirc_id=0, actions=drop
1440 ofpbuf_clear(&ofpacts);
1441 match_init_catchall(&match);
1442 match_set_recirc_id(&match, 0);
1443 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1449 destruct(struct ofproto *ofproto_)
1451 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1452 struct ofproto_async_msg *am;
1453 struct rule_dpif *rule;
1454 struct oftable *table;
1455 struct ovs_list ams;
1457 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1459 xlate_remove_ofproto(ofproto);
1462 /* Ensure that the upcall processing threads have no remaining references
1463 * to the ofproto or anything in it. */
1464 udpif_synchronize(ofproto->backer->udpif);
1466 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1468 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1469 CLS_FOR_EACH (rule, up.cr, &table->cls) {
1470 ofproto_rule_delete(&ofproto->up, &rule->up);
1473 ofproto_group_delete_all(&ofproto->up);
1475 guarded_list_pop_all(&ofproto->ams, &ams);
1476 LIST_FOR_EACH_POP (am, list_node, &ams) {
1477 ofproto_async_msg_free(am);
1479 guarded_list_destroy(&ofproto->ams);
1481 recirc_free_ofproto(ofproto, ofproto->up.name);
1483 mbridge_unref(ofproto->mbridge);
1485 netflow_unref(ofproto->netflow);
1486 dpif_sflow_unref(ofproto->sflow);
1487 dpif_ipfix_unref(ofproto->ipfix);
1488 hmap_destroy(&ofproto->bundles);
1489 mac_learning_unref(ofproto->ml);
1490 mcast_snooping_unref(ofproto->ms);
1492 hmap_destroy(&ofproto->vlandev_map);
1493 hmap_destroy(&ofproto->realdev_vid_map);
1495 sset_destroy(&ofproto->ports);
1496 sset_destroy(&ofproto->ghost_ports);
1497 sset_destroy(&ofproto->port_poll_set);
1499 ovs_mutex_destroy(&ofproto->stats_mutex);
1500 ovs_mutex_destroy(&ofproto->vsp_mutex);
1502 seq_destroy(ofproto->ams_seq);
1504 close_dpif_backer(ofproto->backer);
1508 run(struct ofproto *ofproto_)
1510 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1511 uint64_t new_seq, new_dump_seq;
1513 if (mbridge_need_revalidate(ofproto->mbridge)) {
1514 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1515 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1516 mac_learning_flush(ofproto->ml);
1517 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1518 mcast_snooping_mdb_flush(ofproto->ms);
1521 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
1522 * flow restore. Even though nothing is processed during flow restore,
1523 * all queued 'ams' will be handled immediately when flow restore
1525 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
1527 /* Do not perform any periodic activity required by 'ofproto' while
1528 * waiting for flow restore to complete. */
1529 if (!ofproto_get_flow_restore_wait()) {
1530 struct ofproto_async_msg *am;
1531 struct ovs_list ams;
1533 guarded_list_pop_all(&ofproto->ams, &ams);
1534 LIST_FOR_EACH_POP (am, list_node, &ams) {
1535 connmgr_send_async_msg(ofproto->up.connmgr, am);
1536 ofproto_async_msg_free(am);
1540 if (ofproto->netflow) {
1541 netflow_run(ofproto->netflow);
1543 if (ofproto->sflow) {
1544 dpif_sflow_run(ofproto->sflow);
1546 if (ofproto->ipfix) {
1547 dpif_ipfix_run(ofproto->ipfix);
1550 new_seq = seq_read(connectivity_seq_get());
1551 if (ofproto->change_seq != new_seq) {
1552 struct ofport_dpif *ofport;
1554 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1558 ofproto->change_seq = new_seq;
1560 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1561 struct ofbundle *bundle;
1563 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1570 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1571 if (mac_learning_run(ofproto->ml)) {
1572 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1574 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1576 if (mcast_snooping_run(ofproto->ms)) {
1577 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1580 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1581 if (ofproto->dump_seq != new_dump_seq) {
1582 struct rule *rule, *next_rule;
1584 /* We know stats are relatively fresh, so now is a good time to do some
1586 ofproto->dump_seq = new_dump_seq;
1588 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1590 ovs_mutex_lock(&ofproto_mutex);
1591 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1592 &ofproto->up.expirable) {
1593 rule_expire(rule_dpif_cast(rule));
1595 ovs_mutex_unlock(&ofproto_mutex);
1597 /* All outstanding data in existing flows has been accounted, so it's a
1598 * good time to do bond rebalancing. */
1599 if (ofproto->has_bonded_bundles) {
1600 struct ofbundle *bundle;
1602 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1604 bond_rebalance(bundle->bond);
1613 wait(struct ofproto *ofproto_)
1615 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1617 if (ofproto_get_flow_restore_wait()) {
1621 if (ofproto->sflow) {
1622 dpif_sflow_wait(ofproto->sflow);
1624 if (ofproto->ipfix) {
1625 dpif_ipfix_wait(ofproto->ipfix);
1627 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1628 struct ofbundle *bundle;
1630 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1631 bundle_wait(bundle);
1634 if (ofproto->netflow) {
1635 netflow_wait(ofproto->netflow);
1637 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1638 mac_learning_wait(ofproto->ml);
1639 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1640 mcast_snooping_wait(ofproto->ms);
1642 if (ofproto->backer->need_revalidate) {
1643 poll_immediate_wake();
1646 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1647 seq_wait(ofproto->ams_seq, ofproto->ams_seqno);
1651 type_get_memory_usage(const char *type, struct simap *usage)
1653 struct dpif_backer *backer;
1655 backer = shash_find_data(&all_dpif_backers, type);
1657 udpif_get_memory_usage(backer->udpif, usage);
1662 flush(struct ofproto *ofproto_)
1664 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1665 struct dpif_backer *backer = ofproto->backer;
1668 udpif_flush(backer->udpif);
1673 query_tables(struct ofproto *ofproto,
1674 struct ofputil_table_features *features,
1675 struct ofputil_table_stats *stats)
1677 strcpy(features->name, "classifier");
1682 for (i = 0; i < ofproto->n_tables; i++) {
1683 unsigned long missed, matched;
1685 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1686 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1688 stats[i].matched_count = matched;
1689 stats[i].lookup_count = matched + missed;
1695 set_tables_version(struct ofproto *ofproto_, cls_version_t version)
1697 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1699 atomic_store_relaxed(&ofproto->tables_version, version);
1703 static struct ofport *
1706 struct ofport_dpif *port = xzalloc(sizeof *port);
1711 port_dealloc(struct ofport *port_)
1713 struct ofport_dpif *port = ofport_dpif_cast(port_);
1718 port_construct(struct ofport *port_)
1720 struct ofport_dpif *port = ofport_dpif_cast(port_);
1721 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1722 const struct netdev *netdev = port->up.netdev;
1723 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1724 const char *dp_port_name;
1725 struct dpif_port dpif_port;
1728 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1729 port->bundle = NULL;
1733 port->may_enable = false;
1734 port->stp_port = NULL;
1735 port->stp_state = STP_DISABLED;
1736 port->rstp_port = NULL;
1737 port->rstp_state = RSTP_DISABLED;
1738 port->is_tunnel = false;
1742 port->realdev_ofp_port = 0;
1743 port->vlandev_vid = 0;
1744 port->carrier_seq = netdev_get_carrier_resets(netdev);
1745 port->is_layer3 = netdev_vport_is_layer3(netdev);
1747 if (netdev_vport_is_patch(netdev)) {
1748 /* By bailing out here, we don't submit the port to the sFlow module
1749 * to be considered for counter polling export. This is correct
1750 * because the patch port represents an interface that sFlow considers
1751 * to be "internal" to the switch as a whole, and therefore not a
1752 * candidate for counter polling. */
1753 port->odp_port = ODPP_NONE;
1754 ofport_update_peer(port);
1758 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1759 error = dpif_port_query_by_name(ofproto->backer->dpif, dp_port_name,
1765 port->odp_port = dpif_port.port_no;
1767 if (netdev_get_tunnel_config(netdev)) {
1768 atomic_count_inc(&ofproto->backer->tnl_count);
1769 error = tnl_port_add(port, port->up.netdev, port->odp_port,
1770 ovs_native_tunneling_is_on(ofproto), dp_port_name);
1772 atomic_count_dec(&ofproto->backer->tnl_count);
1773 dpif_port_destroy(&dpif_port);
1777 port->is_tunnel = true;
1778 if (ofproto->ipfix) {
1779 dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1782 /* Sanity-check that a mapping doesn't already exist. This
1783 * shouldn't happen for non-tunnel ports. */
1784 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1785 VLOG_ERR("port %s already has an OpenFlow port number",
1787 dpif_port_destroy(&dpif_port);
1791 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1792 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1793 hash_odp_port(port->odp_port));
1794 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1796 dpif_port_destroy(&dpif_port);
1798 if (ofproto->sflow) {
1799 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1806 port_destruct(struct ofport *port_, bool del)
1808 struct ofport_dpif *port = ofport_dpif_cast(port_);
1809 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1810 const char *devname = netdev_get_name(port->up.netdev);
1811 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1812 const char *dp_port_name;
1814 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1816 xlate_ofport_remove(port);
1819 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1821 if (del && dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1822 /* The underlying device is still there, so delete it. This
1823 * happens when the ofproto is being destroyed, since the caller
1824 * assumes that removal of attached ports will happen as part of
1826 if (!port->is_tunnel) {
1827 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1832 port->peer->peer = NULL;
1836 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1837 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1838 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1839 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1842 if (port->is_tunnel) {
1843 atomic_count_dec(&ofproto->backer->tnl_count);
1846 if (port->is_tunnel && ofproto->ipfix) {
1847 dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1851 sset_find_and_delete(&ofproto->ports, devname);
1852 sset_find_and_delete(&ofproto->ghost_ports, devname);
1853 bundle_remove(port_);
1854 set_cfm(port_, NULL);
1855 set_bfd(port_, NULL);
1856 set_lldp(port_, NULL);
1857 if (port->stp_port) {
1858 stp_port_disable(port->stp_port);
1860 set_rstp_port(port_, NULL);
1861 if (ofproto->sflow) {
1862 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1869 port_modified(struct ofport *port_)
1871 struct ofport_dpif *port = ofport_dpif_cast(port_);
1872 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1873 const char *dp_port_name;
1874 struct netdev *netdev = port->up.netdev;
1876 if (port->bundle && port->bundle->bond) {
1877 bond_slave_set_netdev(port->bundle->bond, port, netdev);
1881 cfm_set_netdev(port->cfm, netdev);
1885 bfd_set_netdev(port->bfd, netdev);
1888 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1889 port->lldp, &port->up.pp.hw_addr);
1891 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1893 if (port->is_tunnel) {
1894 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1896 if (tnl_port_reconfigure(port, netdev, port->odp_port,
1897 ovs_native_tunneling_is_on(ofproto),
1899 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1903 ofport_update_peer(port);
1907 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1909 struct ofport_dpif *port = ofport_dpif_cast(port_);
1910 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1911 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1913 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1914 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1915 OFPUTIL_PC_NO_PACKET_IN)) {
1916 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1918 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1919 bundle_update(port->bundle);
1925 set_sflow(struct ofproto *ofproto_,
1926 const struct ofproto_sflow_options *sflow_options)
1928 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1929 struct dpif_sflow *ds = ofproto->sflow;
1931 if (sflow_options) {
1932 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
1934 struct ofport_dpif *ofport;
1936 ds = ofproto->sflow = dpif_sflow_create();
1937 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1938 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1941 dpif_sflow_set_options(ds, sflow_options);
1942 if (dpif_sflow_get_probability(ds) != old_probability) {
1943 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1947 dpif_sflow_unref(ds);
1948 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1949 ofproto->sflow = NULL;
1957 struct ofproto *ofproto_,
1958 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1959 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1960 size_t n_flow_exporters_options)
1962 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1963 struct dpif_ipfix *di = ofproto->ipfix;
1964 bool has_options = bridge_exporter_options || flow_exporters_options;
1965 bool new_di = false;
1967 if (has_options && !di) {
1968 di = ofproto->ipfix = dpif_ipfix_create();
1973 /* Call set_options in any case to cleanly flush the flow
1974 * caches in the last exporters that are to be destroyed. */
1975 dpif_ipfix_set_options(
1976 di, bridge_exporter_options, flow_exporters_options,
1977 n_flow_exporters_options);
1979 /* Add tunnel ports only when a new ipfix created */
1980 if (new_di == true) {
1981 struct ofport_dpif *ofport;
1982 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1983 if (ofport->is_tunnel == true) {
1984 dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
1990 dpif_ipfix_unref(di);
1991 ofproto->ipfix = NULL;
1999 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
2001 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2002 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2003 struct cfm *old = ofport->cfm;
2008 ofport->cfm = cfm_create(ofport->up.netdev);
2011 if (cfm_configure(ofport->cfm, s)) {
2018 cfm_unref(ofport->cfm);
2021 if (ofport->cfm != old) {
2022 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2024 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2025 ofport->lldp, &ofport->up.pp.hw_addr);
2030 cfm_status_changed(struct ofport *ofport_)
2032 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2034 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
2038 get_cfm_status(const struct ofport *ofport_,
2039 struct cfm_status *status)
2041 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2045 cfm_get_status(ofport->cfm, status);
2054 set_bfd(struct ofport *ofport_, const struct smap *cfg)
2056 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2057 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2061 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2062 cfg, ofport->up.netdev);
2063 if (ofport->bfd != old) {
2064 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2066 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2067 ofport->lldp, &ofport->up.pp.hw_addr);
2072 bfd_status_changed(struct ofport *ofport_)
2074 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2076 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2080 get_bfd_status(struct ofport *ofport_, struct smap *smap)
2082 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2086 bfd_get_status(ofport->bfd, smap);
2095 set_lldp(struct ofport *ofport_,
2096 const struct smap *cfg)
2098 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2102 if (!ofport->lldp) {
2103 struct ofproto_dpif *ofproto;
2105 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2106 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2107 ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2110 if (!lldp_configure(ofport->lldp, cfg)) {
2115 lldp_unref(ofport->lldp);
2116 ofport->lldp = NULL;
2119 ofproto_dpif_monitor_port_update(ofport,
2123 &ofport->up.pp.hw_addr);
2128 get_lldp_status(const struct ofport *ofport_,
2129 struct lldp_status *status OVS_UNUSED)
2131 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2133 return ofport->lldp ? true : false;
2137 set_aa(struct ofproto *ofproto OVS_UNUSED,
2138 const struct aa_settings *s)
2140 return aa_configure(s);
2144 aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2145 const struct aa_mapping_settings *s)
2147 return aa_mapping_register(aux, s);
2151 aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2153 return aa_mapping_unregister(aux);
2157 aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2159 return aa_get_vlan_queued(list);
2163 aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2165 return aa_get_vlan_queue_size();
2169 /* Spanning Tree. */
2171 /* Called while rstp_mutex is held. */
2173 rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
2175 struct ofproto_dpif *ofproto = ofproto_;
2176 struct ofport_dpif *ofport = ofport_;
2177 struct eth_header *eth = dp_packet_l2(pkt);
2179 netdev_get_etheraddr(ofport->up.netdev, ð->eth_src);
2180 if (eth_addr_is_zero(eth->eth_src)) {
2181 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2182 "does not have a configured source MAC address.",
2183 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
2185 ofproto_dpif_send_packet(ofport, pkt);
2187 dp_packet_delete(pkt);
2191 send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
2193 struct ofproto_dpif *ofproto = ofproto_;
2194 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2195 struct ofport_dpif *ofport;
2197 ofport = stp_port_get_aux(sp);
2199 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2200 ofproto->up.name, port_num);
2202 struct eth_header *eth = dp_packet_l2(pkt);
2204 netdev_get_etheraddr(ofport->up.netdev, ð->eth_src);
2205 if (eth_addr_is_zero(eth->eth_src)) {
2206 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2207 "with unknown MAC", ofproto->up.name, port_num);
2209 ofproto_dpif_send_packet(ofport, pkt);
2212 dp_packet_delete(pkt);
2215 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2217 set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2219 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2221 /* Only revalidate flows if the configuration changed. */
2222 if (!s != !ofproto->rstp) {
2223 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2227 if (!ofproto->rstp) {
2228 ofproto->rstp = rstp_create(ofproto_->name, s->address,
2229 rstp_send_bpdu_cb, ofproto);
2230 ofproto->rstp_last_tick = time_msec();
2232 rstp_set_bridge_address(ofproto->rstp, s->address);
2233 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2234 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2235 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2236 s->force_protocol_version);
2237 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2238 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2239 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2240 s->transmit_hold_count);
2242 struct ofport *ofport;
2243 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2244 set_rstp_port(ofport, NULL);
2246 rstp_unref(ofproto->rstp);
2247 ofproto->rstp = NULL;
2252 get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2254 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2256 if (ofproto->rstp) {
2258 s->root_id = rstp_get_root_id(ofproto->rstp);
2259 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2260 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2261 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2262 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2263 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2270 update_rstp_port_state(struct ofport_dpif *ofport)
2272 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2273 enum rstp_state state;
2275 /* Figure out new state. */
2276 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2280 if (ofport->rstp_state != state) {
2281 enum ofputil_port_state of_state;
2284 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2285 netdev_get_name(ofport->up.netdev),
2286 rstp_state_name(ofport->rstp_state),
2287 rstp_state_name(state));
2289 if (rstp_learn_in_state(ofport->rstp_state)
2290 != rstp_learn_in_state(state)) {
2291 /* XXX: Learning action flows should also be flushed. */
2292 if (ofport->bundle) {
2293 if (!rstp_shift_root_learned_address(ofproto->rstp)
2294 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2295 bundle_flush_macs(ofport->bundle, false);
2299 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2300 != rstp_forward_in_state(state);
2302 ofproto->backer->need_revalidate = REV_RSTP;
2303 ofport->rstp_state = state;
2305 if (fwd_change && ofport->bundle) {
2306 bundle_update(ofport->bundle);
2309 /* Update the RSTP state bits in the OpenFlow port description. */
2310 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2311 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2312 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2313 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2315 ofproto_port_set_state(&ofport->up, of_state);
2320 rstp_run(struct ofproto_dpif *ofproto)
2322 if (ofproto->rstp) {
2323 long long int now = time_msec();
2324 long long int elapsed = now - ofproto->rstp_last_tick;
2325 struct rstp_port *rp;
2326 struct ofport_dpif *ofport;
2328 /* Every second, decrease the values of the timers. */
2329 if (elapsed >= 1000) {
2330 rstp_tick_timers(ofproto->rstp);
2331 ofproto->rstp_last_tick = now;
2334 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2335 update_rstp_port_state(ofport);
2339 /* FIXME: This check should be done on-event (i.e., when setting
2340 * p->fdb_flush) and not periodically.
2342 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2343 if (!rstp_shift_root_learned_address(ofproto->rstp)
2344 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2345 bundle_flush_macs(ofport->bundle, false);
2349 if (rstp_shift_root_learned_address(ofproto->rstp)) {
2350 struct ofport_dpif *old_root_aux =
2351 (struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp);
2352 struct ofport_dpif *new_root_aux =
2353 (struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp);
2354 if (old_root_aux != NULL && new_root_aux != NULL) {
2355 bundle_move(old_root_aux->bundle, new_root_aux->bundle);
2356 rstp_reset_root_changed(ofproto->rstp);
2362 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2364 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2366 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2368 /* Only revalidate flows if the configuration changed. */
2369 if (!s != !ofproto->stp) {
2370 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2374 if (!ofproto->stp) {
2375 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2376 send_bpdu_cb, ofproto);
2377 ofproto->stp_last_tick = time_msec();
2380 stp_set_bridge_id(ofproto->stp, s->system_id);
2381 stp_set_bridge_priority(ofproto->stp, s->priority);
2382 stp_set_hello_time(ofproto->stp, s->hello_time);
2383 stp_set_max_age(ofproto->stp, s->max_age);
2384 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2386 struct ofport *ofport;
2388 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2389 set_stp_port(ofport, NULL);
2392 stp_unref(ofproto->stp);
2393 ofproto->stp = NULL;
2400 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2402 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2406 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2407 s->designated_root = stp_get_designated_root(ofproto->stp);
2408 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2417 update_stp_port_state(struct ofport_dpif *ofport)
2419 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2420 enum stp_state state;
2422 /* Figure out new state. */
2423 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2427 if (ofport->stp_state != state) {
2428 enum ofputil_port_state of_state;
2431 VLOG_DBG("port %s: STP state changed from %s to %s",
2432 netdev_get_name(ofport->up.netdev),
2433 stp_state_name(ofport->stp_state),
2434 stp_state_name(state));
2435 if (stp_learn_in_state(ofport->stp_state)
2436 != stp_learn_in_state(state)) {
2437 /* xxx Learning action flows should also be flushed. */
2438 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2439 mac_learning_flush(ofproto->ml);
2440 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2441 mcast_snooping_mdb_flush(ofproto->ms);
2443 fwd_change = stp_forward_in_state(ofport->stp_state)
2444 != stp_forward_in_state(state);
2446 ofproto->backer->need_revalidate = REV_STP;
2447 ofport->stp_state = state;
2448 ofport->stp_state_entered = time_msec();
2450 if (fwd_change && ofport->bundle) {
2451 bundle_update(ofport->bundle);
2454 /* Update the STP state bits in the OpenFlow port description. */
2455 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2456 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2457 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2458 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2459 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2461 ofproto_port_set_state(&ofport->up, of_state);
2465 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2466 * caller is responsible for assigning STP port numbers and ensuring
2467 * there are no duplicates. */
2469 set_stp_port(struct ofport *ofport_,
2470 const struct ofproto_port_stp_settings *s)
2472 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2473 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2474 struct stp_port *sp = ofport->stp_port;
2476 if (!s || !s->enable) {
2478 ofport->stp_port = NULL;
2479 stp_port_disable(sp);
2480 update_stp_port_state(ofport);
2483 } else if (sp && stp_port_no(sp) != s->port_num
2484 && ofport == stp_port_get_aux(sp)) {
2485 /* The port-id changed, so disable the old one if it's not
2486 * already in use by another port. */
2487 stp_port_disable(sp);
2490 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2492 /* Set name before enabling the port so that debugging messages can print
2494 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2495 stp_port_enable(sp);
2497 stp_port_set_aux(sp, ofport);
2498 stp_port_set_priority(sp, s->priority);
2499 stp_port_set_path_cost(sp, s->path_cost);
2501 update_stp_port_state(ofport);
2507 get_stp_port_status(struct ofport *ofport_,
2508 struct ofproto_port_stp_status *s)
2510 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2511 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2512 struct stp_port *sp = ofport->stp_port;
2514 if (!ofproto->stp || !sp) {
2520 s->port_id = stp_port_get_id(sp);
2521 s->state = stp_port_get_state(sp);
2522 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2523 s->role = stp_port_get_role(sp);
2529 get_stp_port_stats(struct ofport *ofport_,
2530 struct ofproto_port_stp_stats *s)
2532 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2533 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2534 struct stp_port *sp = ofport->stp_port;
2536 if (!ofproto->stp || !sp) {
2542 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2548 stp_run(struct ofproto_dpif *ofproto)
2551 long long int now = time_msec();
2552 long long int elapsed = now - ofproto->stp_last_tick;
2553 struct stp_port *sp;
2556 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2557 ofproto->stp_last_tick = now;
2559 while (stp_get_changed_port(ofproto->stp, &sp)) {
2560 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2563 update_stp_port_state(ofport);
2567 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2568 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2569 mac_learning_flush(ofproto->ml);
2570 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2571 mcast_snooping_mdb_flush(ofproto->ms);
2577 stp_wait(struct ofproto_dpif *ofproto)
2580 poll_timer_wait(1000);
2584 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2585 * caller is responsible for assigning RSTP port numbers and ensuring
2586 * there are no duplicates. */
2588 set_rstp_port(struct ofport *ofport_,
2589 const struct ofproto_port_rstp_settings *s)
2591 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2592 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2593 struct rstp_port *rp = ofport->rstp_port;
2595 if (!s || !s->enable) {
2597 rstp_port_set_aux(rp, NULL);
2598 rstp_port_set_state(rp, RSTP_DISABLED);
2599 rstp_port_set_mac_operational(rp, false);
2600 ofport->rstp_port = NULL;
2601 rstp_port_unref(rp);
2602 update_rstp_port_state(ofport);
2607 /* Check if need to add a new port. */
2609 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2612 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
2613 s->admin_edge_port, s->auto_edge,
2614 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2616 update_rstp_port_state(ofport);
2617 /* Synchronize operational status. */
2618 rstp_port_set_mac_operational(rp, ofport->may_enable);
2622 get_rstp_port_status(struct ofport *ofport_,
2623 struct ofproto_port_rstp_status *s)
2625 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2626 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2627 struct rstp_port *rp = ofport->rstp_port;
2629 if (!ofproto->rstp || !rp) {
2635 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2636 &s->designated_bridge_id, &s->designated_port_id,
2637 &s->designated_path_cost, &s->tx_count,
2638 &s->rx_count, &s->error_count, &s->uptime);
2643 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2646 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2647 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2649 if (ofport->n_qdscp != n_qdscp
2650 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2651 n_qdscp * sizeof *qdscp))) {
2652 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2653 free(ofport->qdscp);
2654 ofport->qdscp = n_qdscp
2655 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2657 ofport->n_qdscp = n_qdscp;
2665 /* Expires all MAC learning entries associated with 'bundle' and forces its
2666 * ofproto to revalidate every flow.
2668 * Normally MAC learning entries are removed only from the ofproto associated
2669 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2670 * are removed from every ofproto. When patch ports and SLB bonds are in use
2671 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2672 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2673 * with the host from which it migrated. */
2675 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2677 struct ofproto_dpif *ofproto = bundle->ofproto;
2678 struct mac_learning *ml = ofproto->ml;
2679 struct mac_entry *mac, *next_mac;
2681 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2682 ovs_rwlock_wrlock(&ml->rwlock);
2683 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2684 if (mac_entry_get_port(ml, mac) == bundle) {
2686 struct ofproto_dpif *o;
2688 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2690 struct mac_entry *e;
2692 ovs_rwlock_wrlock(&o->ml->rwlock);
2693 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2695 mac_learning_expire(o->ml, e);
2697 ovs_rwlock_unlock(&o->ml->rwlock);
2702 mac_learning_expire(ml, mac);
2705 ovs_rwlock_unlock(&ml->rwlock);
2709 bundle_move(struct ofbundle *old, struct ofbundle *new)
2711 struct ofproto_dpif *ofproto = old->ofproto;
2712 struct mac_learning *ml = ofproto->ml;
2713 struct mac_entry *mac, *next_mac;
2715 ovs_assert(new->ofproto == old->ofproto);
2717 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2718 ovs_rwlock_wrlock(&ml->rwlock);
2719 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2720 if (mac_entry_get_port(ml, mac) == old) {
2721 mac_entry_set_port(ml, mac, new);
2724 ovs_rwlock_unlock(&ml->rwlock);
2727 static struct ofbundle *
2728 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2730 struct ofbundle *bundle;
2732 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2733 &ofproto->bundles) {
2734 if (bundle->aux == aux) {
2742 bundle_update(struct ofbundle *bundle)
2744 struct ofport_dpif *port;
2746 bundle->floodable = true;
2747 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2748 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2750 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2751 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2752 bundle->floodable = false;
2759 bundle_del_port(struct ofport_dpif *port)
2761 struct ofbundle *bundle = port->bundle;
2763 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2765 list_remove(&port->bundle_node);
2766 port->bundle = NULL;
2769 lacp_slave_unregister(bundle->lacp, port);
2772 bond_slave_unregister(bundle->bond, port);
2775 bundle_update(bundle);
2779 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2780 struct lacp_slave_settings *lacp)
2782 struct ofport_dpif *port;
2784 port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
2789 if (port->bundle != bundle) {
2790 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2792 bundle_remove(&port->up);
2795 port->bundle = bundle;
2796 list_push_back(&bundle->ports, &port->bundle_node);
2797 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2799 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2800 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2801 bundle->floodable = false;
2805 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2806 lacp_slave_register(bundle->lacp, port, lacp);
2813 bundle_destroy(struct ofbundle *bundle)
2815 struct ofproto_dpif *ofproto;
2816 struct ofport_dpif *port, *next_port;
2822 ofproto = bundle->ofproto;
2823 mbridge_unregister_bundle(ofproto->mbridge, bundle);
2826 xlate_bundle_remove(bundle);
2829 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2830 bundle_del_port(port);
2833 bundle_flush_macs(bundle, true);
2834 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2836 free(bundle->trunks);
2837 lacp_unref(bundle->lacp);
2838 bond_unref(bundle->bond);
2843 bundle_set(struct ofproto *ofproto_, void *aux,
2844 const struct ofproto_bundle_settings *s)
2846 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2847 bool need_flush = false;
2848 struct ofport_dpif *port;
2849 struct ofbundle *bundle;
2850 unsigned long *trunks;
2856 bundle_destroy(bundle_lookup(ofproto, aux));
2860 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2861 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2863 bundle = bundle_lookup(ofproto, aux);
2865 bundle = xmalloc(sizeof *bundle);
2867 bundle->ofproto = ofproto;
2868 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2869 hash_pointer(aux, 0));
2871 bundle->name = NULL;
2873 list_init(&bundle->ports);
2874 bundle->vlan_mode = PORT_VLAN_TRUNK;
2876 bundle->trunks = NULL;
2877 bundle->use_priority_tags = s->use_priority_tags;
2878 bundle->lacp = NULL;
2879 bundle->bond = NULL;
2881 bundle->floodable = true;
2882 mbridge_register_bundle(ofproto->mbridge, bundle);
2885 if (!bundle->name || strcmp(s->name, bundle->name)) {
2887 bundle->name = xstrdup(s->name);
2892 ofproto->lacp_enabled = true;
2893 if (!bundle->lacp) {
2894 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2895 bundle->lacp = lacp_create();
2897 lacp_configure(bundle->lacp, s->lacp);
2899 lacp_unref(bundle->lacp);
2900 bundle->lacp = NULL;
2903 /* Update set of ports. */
2905 for (i = 0; i < s->n_slaves; i++) {
2906 if (!bundle_add_port(bundle, s->slaves[i],
2907 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2911 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2912 struct ofport_dpif *next_port;
2914 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2915 for (i = 0; i < s->n_slaves; i++) {
2916 if (s->slaves[i] == port->up.ofp_port) {
2921 bundle_del_port(port);
2925 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2927 if (list_is_empty(&bundle->ports)) {
2928 bundle_destroy(bundle);
2932 /* Set VLAN tagging mode */
2933 if (s->vlan_mode != bundle->vlan_mode
2934 || s->use_priority_tags != bundle->use_priority_tags) {
2935 bundle->vlan_mode = s->vlan_mode;
2936 bundle->use_priority_tags = s->use_priority_tags;
2941 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2942 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2944 if (vlan != bundle->vlan) {
2945 bundle->vlan = vlan;
2949 /* Get trunked VLANs. */
2950 switch (s->vlan_mode) {
2951 case PORT_VLAN_ACCESS:
2955 case PORT_VLAN_TRUNK:
2956 trunks = CONST_CAST(unsigned long *, s->trunks);
2959 case PORT_VLAN_NATIVE_UNTAGGED:
2960 case PORT_VLAN_NATIVE_TAGGED:
2961 if (vlan != 0 && (!s->trunks
2962 || !bitmap_is_set(s->trunks, vlan)
2963 || bitmap_is_set(s->trunks, 0))) {
2964 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2966 trunks = bitmap_clone(s->trunks, 4096);
2968 trunks = bitmap_allocate1(4096);
2970 bitmap_set1(trunks, vlan);
2971 bitmap_set0(trunks, 0);
2973 trunks = CONST_CAST(unsigned long *, s->trunks);
2980 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2981 free(bundle->trunks);
2982 if (trunks == s->trunks) {
2983 bundle->trunks = vlan_bitmap_clone(trunks);
2985 bundle->trunks = trunks;
2990 if (trunks != s->trunks) {
2995 if (!list_is_short(&bundle->ports)) {
2996 bundle->ofproto->has_bonded_bundles = true;
2998 if (bond_reconfigure(bundle->bond, s->bond)) {
2999 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3002 bundle->bond = bond_create(s->bond, ofproto);
3003 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3006 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3007 bond_slave_register(bundle->bond, port,
3008 port->up.ofp_port, port->up.netdev);
3011 bond_unref(bundle->bond);
3012 bundle->bond = NULL;
3015 /* If we changed something that would affect MAC learning, un-learn
3016 * everything on this port and force flow revalidation. */
3018 bundle_flush_macs(bundle, false);
3025 bundle_remove(struct ofport *port_)
3027 struct ofport_dpif *port = ofport_dpif_cast(port_);
3028 struct ofbundle *bundle = port->bundle;
3031 bundle_del_port(port);
3032 if (list_is_empty(&bundle->ports)) {
3033 bundle_destroy(bundle);
3034 } else if (list_is_short(&bundle->ports)) {
3035 bond_unref(bundle->bond);
3036 bundle->bond = NULL;
3042 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
3044 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
3045 struct ofport_dpif *port = port_;
3049 error = netdev_get_etheraddr(port->up.netdev, &ea);
3051 struct dp_packet packet;
3054 dp_packet_init(&packet, 0);
3055 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
3057 memcpy(packet_pdu, pdu, pdu_size);
3059 ofproto_dpif_send_packet(port, &packet);
3060 dp_packet_uninit(&packet);
3062 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
3063 "%s (%s)", port->bundle->name,
3064 netdev_get_name(port->up.netdev), ovs_strerror(error));
3069 bundle_send_learning_packets(struct ofbundle *bundle)
3071 struct ofproto_dpif *ofproto = bundle->ofproto;
3072 int error, n_packets, n_errors;
3073 struct mac_entry *e;
3075 struct ovs_list list_node;
3076 struct ofport_dpif *port;
3077 struct dp_packet *pkt;
3079 struct ovs_list packets;
3081 list_init(&packets);
3082 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3083 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3084 if (mac_entry_get_port(ofproto->ml, e) != bundle) {
3085 pkt_node = xmalloc(sizeof *pkt_node);
3086 pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
3088 (void **)&pkt_node->port);
3089 list_push_back(&packets, &pkt_node->list_node);
3092 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3094 error = n_packets = n_errors = 0;
3095 LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
3098 ret = ofproto_dpif_send_packet(pkt_node->port, pkt_node->pkt);
3099 dp_packet_delete(pkt_node->pkt);
3109 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3110 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3111 "packets, last error was: %s",
3112 bundle->name, n_errors, n_packets, ovs_strerror(error));
3114 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3115 bundle->name, n_packets);
3120 bundle_run(struct ofbundle *bundle)
3123 lacp_run(bundle->lacp, send_pdu_cb);
3126 struct ofport_dpif *port;
3128 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3129 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
3132 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3133 bundle->ofproto->backer->need_revalidate = REV_BOND;
3136 if (bond_should_send_learning_packets(bundle->bond)) {
3137 bundle_send_learning_packets(bundle);
3143 bundle_wait(struct ofbundle *bundle)
3146 lacp_wait(bundle->lacp);
3149 bond_wait(bundle->bond);
3156 mirror_set__(struct ofproto *ofproto_, void *aux,
3157 const struct ofproto_mirror_settings *s)
3159 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3160 struct ofbundle **srcs, **dsts;
3165 mirror_destroy(ofproto->mbridge, aux);
3169 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3170 dsts = xmalloc(s->n_dsts * sizeof *dsts);
3172 for (i = 0; i < s->n_srcs; i++) {
3173 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
3176 for (i = 0; i < s->n_dsts; i++) {
3177 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
3180 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3181 s->n_dsts, s->src_vlans,
3182 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
3189 mirror_get_stats__(struct ofproto *ofproto, void *aux,
3190 uint64_t *packets, uint64_t *bytes)
3192 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3197 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3199 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3200 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3201 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
3202 mac_learning_flush(ofproto->ml);
3204 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3209 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
3211 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3212 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3213 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
3217 forward_bpdu_changed(struct ofproto *ofproto_)
3219 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3220 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3224 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3227 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3228 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3229 mac_learning_set_idle_time(ofproto->ml, idle_time);
3230 mac_learning_set_max_entries(ofproto->ml, max_entries);
3231 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3234 /* Configures multicast snooping on 'ofport' using the settings
3235 * defined in 's'. */
3237 set_mcast_snooping(struct ofproto *ofproto_,
3238 const struct ofproto_mcast_snooping_settings *s)
3240 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3242 /* Only revalidate flows if the configuration changed. */
3243 if (!s != !ofproto->ms) {
3244 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3249 ofproto->ms = mcast_snooping_create();
3252 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3253 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3254 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3255 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3256 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3258 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3260 mcast_snooping_unref(ofproto->ms);
3267 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3269 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3270 const struct ofproto_mcast_snooping_port_settings *s)
3272 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3273 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3275 if (ofproto->ms && s) {
3276 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3277 mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3278 mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3280 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3288 struct ofport_dpif *
3289 ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
3291 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3292 return ofport ? ofport_dpif_cast(ofport) : NULL;
3296 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3297 struct ofproto_port *ofproto_port,
3298 struct dpif_port *dpif_port)
3300 ofproto_port->name = dpif_port->name;
3301 ofproto_port->type = dpif_port->type;
3302 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3306 ofport_update_peer(struct ofport_dpif *ofport)
3308 const struct ofproto_dpif *ofproto;
3309 struct dpif_backer *backer;
3312 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3316 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
3317 backer->need_revalidate = REV_RECONFIGURE;
3320 ofport->peer->peer = NULL;
3321 ofport->peer = NULL;
3324 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3329 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3330 struct ofport *peer_ofport;
3331 struct ofport_dpif *peer;
3334 if (ofproto->backer != backer) {
3338 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3343 peer = ofport_dpif_cast(peer_ofport);
3344 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3345 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3347 ofport->peer = peer;
3348 ofport->peer->peer = ofport;
3358 port_run(struct ofport_dpif *ofport)
3360 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3361 bool carrier_changed = carrier_seq != ofport->carrier_seq;
3362 bool enable = netdev_get_carrier(ofport->up.netdev);
3363 bool cfm_enable = false;
3364 bool bfd_enable = false;
3366 ofport->carrier_seq = carrier_seq;
3369 int cfm_opup = cfm_get_opup(ofport->cfm);
3371 cfm_enable = !cfm_get_fault(ofport->cfm);
3373 if (cfm_opup >= 0) {
3374 cfm_enable = cfm_enable && cfm_opup;
3379 bfd_enable = bfd_forwarding(ofport->bfd);
3382 if (ofport->bfd || ofport->cfm) {
3383 enable = enable && (cfm_enable || bfd_enable);
3386 if (ofport->bundle) {
3387 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3388 if (carrier_changed) {
3389 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3393 if (ofport->may_enable != enable) {
3394 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3396 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3398 if (ofport->rstp_port) {
3399 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3403 ofport->may_enable = enable;
3407 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3408 struct ofproto_port *ofproto_port)
3410 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3411 struct dpif_port dpif_port;
3414 if (sset_contains(&ofproto->ghost_ports, devname)) {
3415 const char *type = netdev_get_type_from_name(devname);
3417 /* We may be called before ofproto->up.port_by_name is populated with
3418 * the appropriate ofport. For this reason, we must get the name and
3419 * type from the netdev layer directly. */
3421 const struct ofport *ofport;
3423 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3424 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3425 ofproto_port->name = xstrdup(devname);
3426 ofproto_port->type = xstrdup(type);
3432 if (!sset_contains(&ofproto->ports, devname)) {
3435 error = dpif_port_query_by_name(ofproto->backer->dpif,
3436 devname, &dpif_port);
3438 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3444 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3446 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3447 const char *devname = netdev_get_name(netdev);
3448 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3449 const char *dp_port_name;
3451 if (netdev_vport_is_patch(netdev)) {
3452 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3456 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3457 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3458 odp_port_t port_no = ODPP_NONE;
3461 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3465 if (netdev_get_tunnel_config(netdev)) {
3466 simap_put(&ofproto->backer->tnl_backers,
3467 dp_port_name, odp_to_u32(port_no));
3471 if (netdev_get_tunnel_config(netdev)) {
3472 sset_add(&ofproto->ghost_ports, devname);
3474 sset_add(&ofproto->ports, devname);
3480 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3482 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3483 struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
3490 sset_find_and_delete(&ofproto->ghost_ports,
3491 netdev_get_name(ofport->up.netdev));
3492 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3493 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3494 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3496 /* The caller is going to close ofport->up.netdev. If this is a
3497 * bonded port, then the bond is using that netdev, so remove it
3498 * from the bond. The client will need to reconfigure everything
3499 * after deleting ports, so then the slave will get re-added. */
3500 bundle_remove(&ofport->up);
3507 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3509 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3512 error = netdev_get_stats(ofport->up.netdev, stats);
3514 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3515 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3517 ovs_mutex_lock(&ofproto->stats_mutex);
3518 /* ofproto->stats.tx_packets represents packets that we created
3519 * internally and sent to some port (e.g. packets sent with
3520 * ofproto_dpif_send_packet()). Account for them as if they had
3521 * come from OFPP_LOCAL and got forwarded. */
3523 if (stats->rx_packets != UINT64_MAX) {
3524 stats->rx_packets += ofproto->stats.tx_packets;
3527 if (stats->rx_bytes != UINT64_MAX) {
3528 stats->rx_bytes += ofproto->stats.tx_bytes;
3531 /* ofproto->stats.rx_packets represents packets that were received on
3532 * some port and we processed internally and dropped (e.g. STP).
3533 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3535 if (stats->tx_packets != UINT64_MAX) {
3536 stats->tx_packets += ofproto->stats.rx_packets;
3539 if (stats->tx_bytes != UINT64_MAX) {
3540 stats->tx_bytes += ofproto->stats.rx_bytes;
3542 ovs_mutex_unlock(&ofproto->stats_mutex);
3549 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3551 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3552 if (ofport->bundle && ofport->bundle->lacp) {
3553 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3560 struct port_dump_state {
3565 struct ofproto_port port;
3570 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3572 *statep = xzalloc(sizeof(struct port_dump_state));
3577 port_dump_next(const struct ofproto *ofproto_, void *state_,
3578 struct ofproto_port *port)
3580 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3581 struct port_dump_state *state = state_;
3582 const struct sset *sset;
3583 struct sset_node *node;
3585 if (state->has_port) {
3586 ofproto_port_destroy(&state->port);
3587 state->has_port = false;
3589 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3590 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3593 error = port_query_by_name(ofproto_, node->name, &state->port);
3595 *port = state->port;
3596 state->has_port = true;
3598 } else if (error != ENODEV) {
3603 if (!state->ghost) {
3604 state->ghost = true;
3607 return port_dump_next(ofproto_, state_, port);
3614 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3616 struct port_dump_state *state = state_;
3618 if (state->has_port) {
3619 ofproto_port_destroy(&state->port);
3626 port_poll(const struct ofproto *ofproto_, char **devnamep)
3628 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3630 if (ofproto->port_poll_errno) {
3631 int error = ofproto->port_poll_errno;
3632 ofproto->port_poll_errno = 0;
3636 if (sset_is_empty(&ofproto->port_poll_set)) {
3640 *devnamep = sset_pop(&ofproto->port_poll_set);
3645 port_poll_wait(const struct ofproto *ofproto_)
3647 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3648 dpif_port_poll_wait(ofproto->backer->dpif);
3652 port_is_lacp_current(const struct ofport *ofport_)
3654 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3655 return (ofport->bundle && ofport->bundle->lacp
3656 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3660 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3661 * then delete it entirely. */
3663 rule_expire(struct rule_dpif *rule)
3664 OVS_REQUIRES(ofproto_mutex)
3666 uint16_t hard_timeout, idle_timeout;
3667 long long int now = time_msec();
3670 hard_timeout = rule->up.hard_timeout;
3671 idle_timeout = rule->up.idle_timeout;
3673 /* Has 'rule' expired? */
3675 long long int modified;
3677 ovs_mutex_lock(&rule->up.mutex);
3678 modified = rule->up.modified;
3679 ovs_mutex_unlock(&rule->up.mutex);
3681 if (now > modified + hard_timeout * 1000) {
3682 reason = OFPRR_HARD_TIMEOUT;
3686 if (reason < 0 && idle_timeout) {
3689 ovs_mutex_lock(&rule->stats_mutex);
3690 used = rule->stats.used;
3691 ovs_mutex_unlock(&rule->stats_mutex);
3693 if (now > used + idle_timeout * 1000) {
3694 reason = OFPRR_IDLE_TIMEOUT;
3699 COVERAGE_INC(ofproto_dpif_expired);
3700 ofproto_rule_expire(&rule->up, reason);
3705 ofproto_dpif_execute_actions__(struct ofproto_dpif *ofproto,
3706 const struct flow *flow,
3707 struct rule_dpif *rule,
3708 const struct ofpact *ofpacts, size_t ofpacts_len,
3709 int recurse, int resubmits,
3710 struct dp_packet *packet)
3712 struct dpif_flow_stats stats;
3713 struct xlate_out xout;
3714 struct xlate_in xin;
3716 struct dpif_execute execute;
3719 ovs_assert((rule != NULL) != (ofpacts != NULL));
3721 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3724 rule_dpif_credit_stats(rule, &stats);
3727 uint64_t odp_actions_stub[1024 / 8];
3728 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
3729 xlate_in_init(&xin, ofproto, flow, flow->in_port.ofp_port, rule,
3730 stats.tcp_flags, packet, NULL, &odp_actions);
3731 xin.ofpacts = ofpacts;
3732 xin.ofpacts_len = ofpacts_len;
3733 xin.resubmit_stats = &stats;
3734 xin.recurse = recurse;
3735 xin.resubmits = resubmits;
3736 if (xlate_actions(&xin, &xout) != XLATE_OK) {
3741 execute.actions = odp_actions.data;
3742 execute.actions_len = odp_actions.size;
3744 pkt_metadata_from_flow(&packet->md, flow);
3745 execute.packet = packet;
3746 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3747 execute.probe = false;
3750 /* Fix up in_port. */
3751 in_port = flow->in_port.ofp_port;
3752 if (in_port == OFPP_NONE) {
3753 in_port = OFPP_LOCAL;
3755 execute.packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3757 error = dpif_execute(ofproto->backer->dpif, &execute);
3759 xlate_out_uninit(&xout);
3760 ofpbuf_uninit(&odp_actions);
3765 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3766 * 'flow' must reflect the data in 'packet'. */
3768 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3769 const struct flow *flow,
3770 struct rule_dpif *rule,
3771 const struct ofpact *ofpacts, size_t ofpacts_len,
3772 struct dp_packet *packet)
3774 return ofproto_dpif_execute_actions__(ofproto, flow, rule, ofpacts,
3775 ofpacts_len, 0, 0, packet);
3779 rule_dpif_credit_stats(struct rule_dpif *rule,
3780 const struct dpif_flow_stats *stats)
3782 ovs_mutex_lock(&rule->stats_mutex);
3783 if (OVS_UNLIKELY(rule->new_rule)) {
3784 rule_dpif_credit_stats(rule->new_rule, stats);
3786 rule->stats.n_packets += stats->n_packets;
3787 rule->stats.n_bytes += stats->n_bytes;
3788 rule->stats.used = MAX(rule->stats.used, stats->used);
3790 ovs_mutex_unlock(&rule->stats_mutex);
3794 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3795 OVS_REQUIRES(rule->up.mutex)
3797 return rule->up.flow_cookie;
3801 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3802 uint16_t hard_timeout)
3804 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3807 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3808 * be read until the calling thread quiesces. */
3809 const struct rule_actions *
3810 rule_dpif_get_actions(const struct rule_dpif *rule)
3812 return rule_get_actions(&rule->up);
3815 /* Sets 'rule''s recirculation id. */
3817 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3818 OVS_REQUIRES(rule->up.mutex)
3820 ovs_assert(!rule->recirc_id || rule->recirc_id == id);
3821 if (rule->recirc_id == id) {
3822 /* Release the new reference to the same id. */
3825 rule->recirc_id = id;
3829 /* Sets 'rule''s recirculation id. */
3831 rule_set_recirc_id(struct rule *rule_, uint32_t id)
3833 struct rule_dpif *rule = rule_dpif_cast(rule_);
3835 ovs_mutex_lock(&rule->up.mutex);
3836 rule_dpif_set_recirc_id(rule, id);
3837 ovs_mutex_unlock(&rule->up.mutex);
3841 ofproto_dpif_get_tables_version(struct ofproto_dpif *ofproto OVS_UNUSED)
3843 cls_version_t version;
3845 atomic_read_relaxed(&ofproto->tables_version, &version);
3850 /* The returned rule (if any) is valid at least until the next RCU quiescent
3851 * period. If the rule needs to stay around longer, the caller should take
3854 * 'flow' is non-const to allow for temporary modifications during the lookup.
3855 * Any changes are restored before returning. */
3856 static struct rule_dpif *
3857 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, cls_version_t version,
3858 uint8_t table_id, struct flow *flow,
3859 struct flow_wildcards *wc)
3861 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3862 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls, version,
3866 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
3867 * table '*table_id'. Returns the rule that was found, which may be one of the
3868 * special rules according to packet miss hadling. If 'may_packet_in' is
3869 * false, returning of the miss_rule (which issues packet ins for the
3870 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
3871 * that were used during the lookup.
3873 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3874 * if none is found then the table miss configuration for that table is
3875 * honored, which can result in additional lookups in other OpenFlow tables.
3876 * In this case the function updates '*table_id' to reflect the final OpenFlow
3877 * table that was searched.
3879 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3882 * The rule is returned in '*rule', which is valid at least until the next
3883 * RCU quiescent period. If the '*rule' needs to stay around longer, the
3884 * caller must take a reference.
3886 * 'in_port' allows the lookup to take place as if the in port had the value
3887 * 'in_port'. This is needed for resubmit action support.
3889 * 'flow' is non-const to allow for temporary modifications during the lookup.
3890 * Any changes are restored before returning. */
3892 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3893 cls_version_t version, struct flow *flow,
3894 struct flow_wildcards *wc,
3895 const struct dpif_flow_stats *stats,
3896 uint8_t *table_id, ofp_port_t in_port,
3897 bool may_packet_in, bool honor_table_miss)
3899 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
3900 ofp_port_t old_in_port = flow->in_port.ofp_port;
3901 enum ofputil_table_miss miss_config;
3902 struct rule_dpif *rule;
3905 /* We always unwildcard nw_frag (for IP), so they
3906 * need not be unwildcarded here. */
3907 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3908 && ofproto->up.frag_handling != OFPUTIL_FRAG_NX_MATCH) {
3909 if (ofproto->up.frag_handling == OFPUTIL_FRAG_NORMAL) {
3910 /* We must pretend that transport ports are unavailable. */
3911 flow->tp_src = htons(0);
3912 flow->tp_dst = htons(0);
3914 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
3915 * Use the drop_frags_rule (which cannot disappear). */
3916 rule = ofproto->drop_frags_rule;
3918 struct oftable *tbl = &ofproto->up.tables[*table_id];
3921 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
3927 /* Look up a flow with 'in_port' as the input port. Then restore the
3928 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3929 * have surprising behavior). */
3930 flow->in_port.ofp_port = in_port;
3932 /* Our current implementation depends on n_tables == N_TABLES, and
3933 * TBL_INTERNAL being the last table. */
3934 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
3936 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
3938 for (next_id = *table_id;
3939 next_id < ofproto->up.n_tables;
3940 next_id++, next_id += (next_id == TBL_INTERNAL))
3942 *table_id = next_id;
3943 rule = rule_dpif_lookup_in_table(ofproto, version, next_id, flow, wc);
3945 struct oftable *tbl = &ofproto->up.tables[next_id];
3948 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
3949 stats->n_packets, &orig);
3952 goto out; /* Match. */
3954 if (honor_table_miss) {
3955 miss_config = ofproto_table_get_miss_config(&ofproto->up,
3957 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
3964 rule = ofproto->no_packet_in_rule;
3965 if (may_packet_in) {
3966 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
3967 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
3968 struct ofport_dpif *port;
3970 port = ofp_port_to_ofport(ofproto, old_in_port);
3972 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3974 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
3975 rule = ofproto->miss_rule;
3977 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
3978 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3979 rule = ofproto->miss_rule;
3983 /* Restore port numbers, as they may have been modified above. */
3984 flow->tp_src = old_tp_src;
3985 flow->tp_dst = old_tp_dst;
3986 /* Restore the old in port. */
3987 flow->in_port.ofp_port = old_in_port;
3993 complete_operation(struct rule_dpif *rule)
3994 OVS_REQUIRES(ofproto_mutex)
3996 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3998 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4001 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
4003 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
4006 static struct rule *
4009 struct rule_dpif *rule = xzalloc(sizeof *rule);
4014 rule_dealloc(struct rule *rule_)
4016 struct rule_dpif *rule = rule_dpif_cast(rule_);
4021 check_mask(struct ofproto_dpif *ofproto, const struct miniflow *flow)
4023 const struct odp_support *support;
4024 uint16_t ct_state, ct_zone;
4028 support = &ofproto_dpif_get_support(ofproto)->odp;
4029 ct_state = MINIFLOW_GET_U16(flow, ct_state);
4030 if (support->ct_state && support->ct_zone && support->ct_mark
4031 && support->ct_label && support->ct_state_nat) {
4032 return ct_state & CS_UNSUPPORTED_MASK ? OFPERR_OFPBMC_BAD_MASK : 0;
4035 ct_zone = MINIFLOW_GET_U16(flow, ct_zone);
4036 ct_mark = MINIFLOW_GET_U32(flow, ct_mark);
4037 ct_label = MINIFLOW_GET_U128(flow, ct_label);
4039 if ((ct_state && !support->ct_state)
4040 || (ct_state & CS_UNSUPPORTED_MASK)
4041 || ((ct_state & (CS_SRC_NAT | CS_DST_NAT)) && !support->ct_state_nat)
4042 || (ct_zone && !support->ct_zone)
4043 || (ct_mark && !support->ct_mark)
4044 || (!ovs_u128_is_zero(&ct_label) && !support->ct_label)) {
4045 return OFPERR_OFPBMC_BAD_MASK;
4052 check_actions(const struct ofproto_dpif *ofproto,
4053 const struct rule_actions *const actions)
4055 const struct ofpact *ofpact;
4057 OFPACT_FOR_EACH (ofpact, actions->ofpacts, actions->ofpacts_len) {
4058 const struct odp_support *support;
4059 const struct ofpact_conntrack *ct;
4060 const struct ofpact *a;
4062 if (ofpact->type != OFPACT_CT) {
4066 ct = CONTAINER_OF(ofpact, struct ofpact_conntrack, ofpact);
4067 support = &ofproto_dpif_get_support(ofproto)->odp;
4069 if (!support->ct_state) {
4070 return OFPERR_OFPBAC_BAD_TYPE;
4072 if ((ct->zone_imm || ct->zone_src.field) && !support->ct_zone) {
4073 return OFPERR_OFPBAC_BAD_ARGUMENT;
4076 OFPACT_FOR_EACH(a, ct->actions, ofpact_ct_get_action_len(ct)) {
4077 const struct mf_field *dst = ofpact_get_mf_dst(a);
4079 if (a->type == OFPACT_NAT && !support->ct_state_nat) {
4080 /* The backer doesn't seem to support the NAT bits in
4081 * 'ct_state': assume that it doesn't support the NAT
4083 return OFPERR_OFPBAC_BAD_TYPE;
4085 if (dst && ((dst->id == MFF_CT_MARK && !support->ct_mark)
4086 || (dst->id == MFF_CT_LABEL && !support->ct_label))) {
4087 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
4096 rule_check(struct rule *rule)
4098 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->ofproto);
4101 err = check_mask(ofproto, &rule->cr.match.mask->masks);
4105 return check_actions(ofproto, rule->actions);
4109 rule_construct(struct rule *rule_)
4110 OVS_NO_THREAD_SAFETY_ANALYSIS
4112 struct rule_dpif *rule = rule_dpif_cast(rule_);
4115 error = rule_check(rule_);
4120 ovs_mutex_init_adaptive(&rule->stats_mutex);
4121 rule->stats.n_packets = 0;
4122 rule->stats.n_bytes = 0;
4123 rule->stats.used = rule->up.modified;
4124 rule->recirc_id = 0;
4125 rule->new_rule = NULL;
4131 rule_insert(struct rule *rule_, struct rule *old_rule_, bool forward_stats)
4132 OVS_REQUIRES(ofproto_mutex)
4134 struct rule_dpif *rule = rule_dpif_cast(rule_);
4136 if (old_rule_ && forward_stats) {
4137 struct rule_dpif *old_rule = rule_dpif_cast(old_rule_);
4139 ovs_assert(!old_rule->new_rule);
4141 /* Take a reference to the new rule, and refer all stats updates from
4142 * the old rule to the new rule. */
4143 rule_dpif_ref(rule);
4145 ovs_mutex_lock(&old_rule->stats_mutex);
4146 ovs_mutex_lock(&rule->stats_mutex);
4147 old_rule->new_rule = rule; /* Forward future stats. */
4148 rule->stats = old_rule->stats; /* Transfer stats to the new rule. */
4149 ovs_mutex_unlock(&rule->stats_mutex);
4150 ovs_mutex_unlock(&old_rule->stats_mutex);
4153 complete_operation(rule);
4157 rule_delete(struct rule *rule_)
4158 OVS_REQUIRES(ofproto_mutex)
4160 struct rule_dpif *rule = rule_dpif_cast(rule_);
4161 complete_operation(rule);
4165 rule_destruct(struct rule *rule_)
4166 OVS_NO_THREAD_SAFETY_ANALYSIS
4168 struct rule_dpif *rule = rule_dpif_cast(rule_);
4170 ovs_mutex_destroy(&rule->stats_mutex);
4171 /* Release reference to the new rule, if any. */
4172 if (rule->new_rule) {
4173 rule_dpif_unref(rule->new_rule);
4175 if (rule->recirc_id) {
4176 recirc_free_id(rule->recirc_id);
4181 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4182 long long int *used)
4184 struct rule_dpif *rule = rule_dpif_cast(rule_);
4186 ovs_mutex_lock(&rule->stats_mutex);
4187 if (OVS_UNLIKELY(rule->new_rule)) {
4188 rule_get_stats(&rule->new_rule->up, packets, bytes, used);
4190 *packets = rule->stats.n_packets;
4191 *bytes = rule->stats.n_bytes;
4192 *used = rule->stats.used;
4194 ovs_mutex_unlock(&rule->stats_mutex);
4198 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
4199 struct dp_packet *packet)
4201 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4203 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
4207 rule_execute(struct rule *rule, const struct flow *flow,
4208 struct dp_packet *packet)
4210 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
4211 dp_packet_delete(packet);
4215 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4217 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4220 static struct ofgroup *
4223 struct group_dpif *group = xzalloc(sizeof *group);
4228 group_dealloc(struct ofgroup *group_)
4230 struct group_dpif *group = group_dpif_cast(group_);
4235 group_construct_stats(struct group_dpif *group)
4236 OVS_REQUIRES(group->stats_mutex)
4238 struct ofputil_bucket *bucket;
4239 const struct ovs_list *buckets;
4241 group->packet_count = 0;
4242 group->byte_count = 0;
4244 group_dpif_get_buckets(group, &buckets);
4245 LIST_FOR_EACH (bucket, list_node, buckets) {
4246 bucket->stats.packet_count = 0;
4247 bucket->stats.byte_count = 0;
4252 group_dpif_credit_stats(struct group_dpif *group,
4253 struct ofputil_bucket *bucket,
4254 const struct dpif_flow_stats *stats)
4256 ovs_mutex_lock(&group->stats_mutex);
4257 group->packet_count += stats->n_packets;
4258 group->byte_count += stats->n_bytes;
4260 bucket->stats.packet_count += stats->n_packets;
4261 bucket->stats.byte_count += stats->n_bytes;
4262 } else { /* Credit to all buckets */
4263 const struct ovs_list *buckets;
4265 group_dpif_get_buckets(group, &buckets);
4266 LIST_FOR_EACH (bucket, list_node, buckets) {
4267 bucket->stats.packet_count += stats->n_packets;
4268 bucket->stats.byte_count += stats->n_bytes;
4271 ovs_mutex_unlock(&group->stats_mutex);
4275 group_construct(struct ofgroup *group_)
4277 struct group_dpif *group = group_dpif_cast(group_);
4279 ovs_mutex_init_adaptive(&group->stats_mutex);
4280 ovs_mutex_lock(&group->stats_mutex);
4281 group_construct_stats(group);
4282 ovs_mutex_unlock(&group->stats_mutex);
4287 group_destruct(struct ofgroup *group_)
4289 struct group_dpif *group = group_dpif_cast(group_);
4290 ovs_mutex_destroy(&group->stats_mutex);
4294 group_modify(struct ofgroup *group_)
4296 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
4298 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4304 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4306 struct group_dpif *group = group_dpif_cast(group_);
4307 struct ofputil_bucket *bucket;
4308 const struct ovs_list *buckets;
4309 struct bucket_counter *bucket_stats;
4311 ovs_mutex_lock(&group->stats_mutex);
4312 ogs->packet_count = group->packet_count;
4313 ogs->byte_count = group->byte_count;
4315 group_dpif_get_buckets(group, &buckets);
4316 bucket_stats = ogs->bucket_stats;
4317 LIST_FOR_EACH (bucket, list_node, buckets) {
4318 bucket_stats->packet_count = bucket->stats.packet_count;
4319 bucket_stats->byte_count = bucket->stats.byte_count;
4322 ovs_mutex_unlock(&group->stats_mutex);
4327 /* If the group exists, this function increments the groups's reference count.
4329 * Make sure to call group_dpif_unref() after no longer needing to maintain
4330 * a reference to the group. */
4332 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4333 struct group_dpif **group)
4335 struct ofgroup *ofgroup;
4338 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4339 *group = found ? group_dpif_cast(ofgroup) : NULL;
4345 group_dpif_get_buckets(const struct group_dpif *group,
4346 const struct ovs_list **buckets)
4348 *buckets = &group->up.buckets;
4351 enum ofp11_group_type
4352 group_dpif_get_type(const struct group_dpif *group)
4354 return group->up.type;
4358 group_dpif_get_selection_method(const struct group_dpif *group)
4360 return group->up.props.selection_method;
4363 /* Sends 'packet' out 'ofport'.
4364 * May modify 'packet'.
4365 * Returns 0 if successful, otherwise a positive errno value. */
4367 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4369 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4372 error = xlate_send_packet(ofport, packet);
4374 ovs_mutex_lock(&ofproto->stats_mutex);
4375 ofproto->stats.tx_packets++;
4376 ofproto->stats.tx_bytes += dp_packet_size(packet);
4377 ovs_mutex_unlock(&ofproto->stats_mutex);
4382 group_dpif_get_selection_method_param(const struct group_dpif *group)
4384 return group->up.props.selection_method_param;
4387 const struct field_array *
4388 group_dpif_get_fields(const struct group_dpif *group)
4390 return &group->up.props.fields;
4393 /* Return the version string of the datapath that backs up
4397 get_datapath_version(const struct ofproto *ofproto_)
4399 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4401 return ofproto->backer->dp_version_string;
4405 set_frag_handling(struct ofproto *ofproto_,
4406 enum ofputil_frag_handling frag_handling)
4408 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4409 if (frag_handling != OFPUTIL_FRAG_REASM) {
4410 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4418 packet_out(struct ofproto *ofproto_, struct dp_packet *packet,
4419 const struct flow *flow,
4420 const struct ofpact *ofpacts, size_t ofpacts_len)
4422 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4424 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4425 ofpacts_len, packet);
4430 nxt_resume(struct ofproto *ofproto_,
4431 const struct ofputil_packet_in_private *pin)
4433 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4435 /* Translate pin into datapath actions. */
4436 uint64_t odp_actions_stub[1024 / 8];
4437 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
4438 enum slow_path_reason slow;
4439 enum ofperr error = xlate_resume(ofproto, pin, &odp_actions, &slow);
4441 /* Steal 'pin->packet' and put it into a dp_packet. */
4442 struct dp_packet packet;
4443 dp_packet_init(&packet, pin->public.packet_len);
4444 dp_packet_put(&packet, pin->public.packet, pin->public.packet_len);
4446 /* Execute the datapath actions on the packet. */
4447 struct dpif_execute execute = {
4448 .actions = odp_actions.data,
4449 .actions_len = odp_actions.size,
4450 .needs_help = (slow & SLOW_ACTION) != 0,
4453 dpif_execute(ofproto->backer->dpif, &execute);
4456 ofpbuf_uninit(&odp_actions);
4457 dp_packet_uninit(&packet);
4465 set_netflow(struct ofproto *ofproto_,
4466 const struct netflow_options *netflow_options)
4468 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4470 if (netflow_options) {
4471 if (!ofproto->netflow) {
4472 ofproto->netflow = netflow_create();
4473 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4475 return netflow_set_options(ofproto->netflow, netflow_options);
4476 } else if (ofproto->netflow) {
4477 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4478 netflow_unref(ofproto->netflow);
4479 ofproto->netflow = NULL;
4486 get_netflow_ids(const struct ofproto *ofproto_,
4487 uint8_t *engine_type, uint8_t *engine_id)
4489 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4491 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4494 static struct ofproto_dpif *
4495 ofproto_dpif_lookup(const char *name)
4497 struct ofproto_dpif *ofproto;
4499 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4500 hash_string(name, 0), &all_ofproto_dpifs) {
4501 if (!strcmp(ofproto->up.name, name)) {
4509 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4510 const char *argv[], void *aux OVS_UNUSED)
4512 struct ofproto_dpif *ofproto;
4515 ofproto = ofproto_dpif_lookup(argv[1]);
4517 unixctl_command_reply_error(conn, "no such bridge");
4520 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4521 mac_learning_flush(ofproto->ml);
4522 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4524 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4525 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4526 mac_learning_flush(ofproto->ml);
4527 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4531 unixctl_command_reply(conn, "table successfully flushed");
4535 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4536 const char *argv[], void *aux OVS_UNUSED)
4538 struct ofproto_dpif *ofproto;
4541 ofproto = ofproto_dpif_lookup(argv[1]);
4543 unixctl_command_reply_error(conn, "no such bridge");
4547 if (!mcast_snooping_enabled(ofproto->ms)) {
4548 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4551 mcast_snooping_mdb_flush(ofproto->ms);
4553 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4554 if (!mcast_snooping_enabled(ofproto->ms)) {
4557 mcast_snooping_mdb_flush(ofproto->ms);
4561 unixctl_command_reply(conn, "table successfully flushed");
4564 static struct ofport_dpif *
4565 ofbundle_get_a_port(const struct ofbundle *bundle)
4567 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
4572 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4573 const char *argv[], void *aux OVS_UNUSED)
4575 struct ds ds = DS_EMPTY_INITIALIZER;
4576 const struct ofproto_dpif *ofproto;
4577 const struct mac_entry *e;
4579 ofproto = ofproto_dpif_lookup(argv[1]);
4581 unixctl_command_reply_error(conn, "no such bridge");
4585 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4586 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4587 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4588 struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
4589 char name[OFP_MAX_PORT_NAME_LEN];
4591 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4593 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
4594 name, e->vlan, ETH_ADDR_ARGS(e->mac),
4595 mac_entry_age(ofproto->ml, e));
4597 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4598 unixctl_command_reply(conn, ds_cstr(&ds));
4603 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4604 int argc OVS_UNUSED,
4606 void *aux OVS_UNUSED)
4608 struct ds ds = DS_EMPTY_INITIALIZER;
4609 const struct ofproto_dpif *ofproto;
4610 const struct ofbundle *bundle;
4611 const struct mcast_group *grp;
4612 struct mcast_group_bundle *b;
4613 struct mcast_mrouter_bundle *mrouter;
4615 ofproto = ofproto_dpif_lookup(argv[1]);
4617 unixctl_command_reply_error(conn, "no such bridge");
4621 if (!mcast_snooping_enabled(ofproto->ms)) {
4622 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4626 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
4627 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4628 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4629 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
4630 char name[OFP_MAX_PORT_NAME_LEN];
4633 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4635 ds_put_format(&ds, "%5s %4d ", name, grp->vlan);
4636 ipv6_format_mapped(&grp->addr, &ds);
4637 ds_put_format(&ds, " %3d\n",
4638 mcast_bundle_age(ofproto->ms, b));
4642 /* ports connected to multicast routers */
4643 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
4644 char name[OFP_MAX_PORT_NAME_LEN];
4646 bundle = mrouter->port;
4647 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4649 ds_put_format(&ds, "%5s %4d querier %3d\n",
4650 name, mrouter->vlan,
4651 mcast_mrouter_age(ofproto->ms, mrouter));
4653 ovs_rwlock_unlock(&ofproto->ms->rwlock);
4654 unixctl_command_reply(conn, ds_cstr(&ds));
4659 struct xlate_out xout;
4660 struct xlate_in xin;
4661 const struct flow *key;
4664 struct flow_wildcards wc;
4665 struct ofpbuf odp_actions;
4669 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4671 const struct rule_actions *actions;
4674 ds_put_char_multiple(result, '\t', level);
4676 ds_put_cstr(result, "No match\n");
4680 ovs_mutex_lock(&rule->up.mutex);
4681 cookie = rule->up.flow_cookie;
4682 ovs_mutex_unlock(&rule->up.mutex);
4684 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4685 rule ? rule->up.table_id : 0, ntohll(cookie));
4686 cls_rule_format(&rule->up.cr, result);
4687 ds_put_char(result, '\n');
4689 actions = rule_dpif_get_actions(rule);
4691 ds_put_char_multiple(result, '\t', level);
4692 ds_put_cstr(result, "OpenFlow actions=");
4693 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4694 ds_put_char(result, '\n');
4698 trace_format_flow(struct ds *result, int level, const char *title,
4699 struct trace_ctx *trace)
4701 ds_put_char_multiple(result, '\t', level);
4702 ds_put_format(result, "%s: ", title);
4703 /* Do not report unchanged flows for resubmits. */
4704 if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4705 || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4706 ds_put_cstr(result, "unchanged");
4708 flow_format(result, &trace->xin.flow);
4709 trace->flow = trace->xin.flow;
4711 ds_put_char(result, '\n');
4715 trace_format_regs(struct ds *result, int level, const char *title,
4716 struct trace_ctx *trace)
4720 ds_put_char_multiple(result, '\t', level);
4721 ds_put_format(result, "%s:", title);
4722 for (i = 0; i < FLOW_N_REGS; i++) {
4723 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4725 ds_put_char(result, '\n');
4729 trace_format_odp(struct ds *result, int level, const char *title,
4730 struct trace_ctx *trace)
4732 struct ofpbuf *odp_actions = &trace->odp_actions;
4734 ds_put_char_multiple(result, '\t', level);
4735 ds_put_format(result, "%s: ", title);
4736 format_odp_actions(result, odp_actions->data, odp_actions->size);
4737 ds_put_char(result, '\n');
4741 trace_format_megaflow(struct ds *result, int level, const char *title,
4742 struct trace_ctx *trace)
4746 ds_put_char_multiple(result, '\t', level);
4747 ds_put_format(result, "%s: ", title);
4748 match_init(&match, trace->key, &trace->wc);
4749 match_format(&match, result, OFP_DEFAULT_PRIORITY);
4750 ds_put_char(result, '\n');
4753 static void trace_report(struct xlate_in *, int recurse,
4754 const char *format, ...)
4755 OVS_PRINTF_FORMAT(3, 4);
4756 static void trace_report_valist(struct xlate_in *, int recurse,
4757 const char *format, va_list args)
4758 OVS_PRINTF_FORMAT(3, 0);
4761 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
4763 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4764 struct ds *result = trace->result;
4767 if (rule == xin->ofproto->miss_rule) {
4768 trace_report(xin, recurse,
4769 "No match, flow generates \"packet in\"s.");
4770 } else if (rule == xin->ofproto->no_packet_in_rule) {
4771 trace_report(xin, recurse, "No match, packets dropped because "
4772 "OFPPC_NO_PACKET_IN is set on in_port.");
4773 } else if (rule == xin->ofproto->drop_frags_rule) {
4774 trace_report(xin, recurse, "Packets dropped because they are IP "
4775 "fragments and the fragment handling mode is "
4780 ds_put_char(result, '\n');
4782 trace_format_flow(result, recurse, "Resubmitted flow", trace);
4783 trace_format_regs(result, recurse, "Resubmitted regs", trace);
4784 trace_format_odp(result, recurse, "Resubmitted odp", trace);
4785 trace_format_megaflow(result, recurse, "Resubmitted megaflow", trace);
4787 trace_format_rule(result, recurse, rule);
4791 trace_report_valist(struct xlate_in *xin, int recurse,
4792 const char *format, va_list args)
4794 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4795 struct ds *result = trace->result;
4797 ds_put_char_multiple(result, '\t', recurse);
4798 ds_put_format_valist(result, format, args);
4799 ds_put_char(result, '\n');
4803 trace_report(struct xlate_in *xin, int recurse, const char *format, ...)
4807 va_start(args, format);
4808 trace_report_valist(xin, recurse, format, args);
4812 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4813 * forms are supported:
4815 * - [dpname] odp_flow [-generate | packet]
4816 * - bridge br_flow [-generate | packet]
4818 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4819 * returns a nonnull malloced error message. */
4820 static char * OVS_WARN_UNUSED_RESULT
4821 parse_flow_and_packet(int argc, const char *argv[],
4822 struct ofproto_dpif **ofprotop, struct flow *flow,
4823 struct dp_packet **packetp)
4825 const struct dpif_backer *backer = NULL;
4826 const char *error = NULL;
4828 struct simap port_names = SIMAP_INITIALIZER(&port_names);
4829 struct dp_packet *packet;
4830 struct ofpbuf odp_key;
4831 struct ofpbuf odp_mask;
4833 ofpbuf_init(&odp_key, 0);
4834 ofpbuf_init(&odp_mask, 0);
4836 /* Handle "-generate" or a hex string as the last argument. */
4837 if (!strcmp(argv[argc - 1], "-generate")) {
4838 packet = dp_packet_new(0);
4841 error = eth_from_hex(argv[argc - 1], &packet);
4844 } else if (argc == 4) {
4845 /* The 3-argument form must end in "-generate' or a hex string. */
4851 /* odp_flow can have its in_port specified as a name instead of port no.
4852 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4853 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4854 * we need to create a simap of name to port no. */
4856 const char *dp_type;
4857 if (!strncmp(argv[1], "ovs-", 4)) {
4858 dp_type = argv[1] + 4;
4862 backer = shash_find_data(&all_dpif_backers, dp_type);
4863 } else if (argc == 2) {
4864 struct shash_node *node;
4865 if (shash_count(&all_dpif_backers) == 1) {
4866 node = shash_first(&all_dpif_backers);
4867 backer = node->data;
4870 error = "Syntax error";
4873 if (backer && backer->dpif) {
4874 struct dpif_port dpif_port;
4875 struct dpif_port_dump port_dump;
4876 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4877 simap_put(&port_names, dpif_port.name,
4878 odp_to_u32(dpif_port.port_no));
4882 /* Parse the flow and determine whether a datapath or
4883 * bridge is specified. If function odp_flow_key_from_string()
4884 * returns 0, the flow is a odp_flow. If function
4885 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4886 if (!odp_flow_from_string(argv[argc - 1], &port_names,
4887 &odp_key, &odp_mask)) {
4889 error = "Cannot find the datapath";
4893 if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
4894 error = "Failed to parse datapath flow key";
4898 *ofprotop = xlate_lookup_ofproto(backer, flow,
4899 &flow->in_port.ofp_port);
4900 if (*ofprotop == NULL) {
4901 error = "Invalid datapath flow";
4905 vsp_adjust_flow(*ofprotop, flow, NULL);
4908 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4911 m_err = xasprintf("Bad openflow flow syntax: %s", err);
4916 error = "Must specify bridge name";
4920 *ofprotop = ofproto_dpif_lookup(argv[1]);
4922 error = "Unknown bridge name";
4928 /* Generate a packet, if requested. */
4930 if (!dp_packet_size(packet)) {
4931 flow_compose(packet, flow);
4933 /* Use the metadata from the flow and the packet argument
4934 * to reconstruct the flow. */
4935 pkt_metadata_from_flow(&packet->md, flow);
4936 flow_extract(packet, flow);
4941 if (error && !m_err) {
4942 m_err = xstrdup(error);
4945 dp_packet_delete(packet);
4949 ofpbuf_uninit(&odp_key);
4950 ofpbuf_uninit(&odp_mask);
4951 simap_destroy(&port_names);
4956 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4957 void *aux OVS_UNUSED)
4959 struct ofproto_dpif *ofproto;
4960 struct dp_packet *packet;
4964 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4969 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4970 unixctl_command_reply(conn, ds_cstr(&result));
4971 ds_destroy(&result);
4972 dp_packet_delete(packet);
4974 unixctl_command_reply_error(conn, error);
4980 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4981 const char *argv[], void *aux OVS_UNUSED)
4983 enum ofputil_protocol usable_protocols;
4984 struct ofproto_dpif *ofproto;
4985 bool enforce_consistency;
4986 struct ofpbuf ofpacts;
4987 struct dp_packet *packet;
4992 /* Three kinds of error return values! */
4998 ofpbuf_init(&ofpacts, 0);
5000 /* Parse actions. */
5001 error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
5003 unixctl_command_reply_error(conn, error);
5008 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
5009 * consistency between the flow and the actions. With -consistent, we
5010 * enforce consistency even for a flow supported in OpenFlow 1.0. */
5011 if (!strcmp(argv[1], "-consistent")) {
5012 enforce_consistency = true;
5016 enforce_consistency = false;
5019 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
5021 unixctl_command_reply_error(conn, error);
5026 /* Do the same checks as handle_packet_out() in ofproto.c.
5028 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
5029 * strictly correct because these actions aren't in any table, but it's OK
5030 * because it 'table_id' is used only to check goto_table instructions, but
5031 * packet-outs take a list of actions and therefore it can't include
5034 * We skip the "meter" check here because meter is an instruction, not an
5035 * action, and thus cannot appear in ofpacts. */
5036 in_port = ofp_to_u16(flow.in_port.ofp_port);
5037 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
5038 unixctl_command_reply_error(conn, "invalid in_port");
5041 if (enforce_consistency) {
5042 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
5043 u16_to_ofp(ofproto->up.max_ports),
5044 0, ofproto->up.n_tables,
5047 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
5048 u16_to_ofp(ofproto->up.max_ports), 0,
5049 ofproto->up.n_tables, &usable_protocols);
5052 retval = ofproto_check_ofpacts(&ofproto->up, ofpacts.data,
5058 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
5059 unixctl_command_reply_error(conn, ds_cstr(&result));
5063 ofproto_trace(ofproto, &flow, packet,
5064 ofpacts.data, ofpacts.size, &result);
5065 unixctl_command_reply(conn, ds_cstr(&result));
5068 ds_destroy(&result);
5069 dp_packet_delete(packet);
5070 ofpbuf_uninit(&ofpacts);
5073 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
5074 * description of the results to 'ds'.
5076 * The trace follows a packet with the specified 'flow' through the flow
5077 * table. 'packet' may be nonnull to trace an actual packet, with consequent
5078 * side effects (if it is nonnull then its flow must be 'flow').
5080 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
5081 * trace, otherwise the actions are determined by a flow table lookup. */
5083 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
5084 const struct dp_packet *packet,
5085 const struct ofpact ofpacts[], size_t ofpacts_len,
5088 struct trace_ctx trace;
5089 enum xlate_error error;
5091 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
5092 ds_put_cstr(ds, "Flow: ");
5093 flow_format(ds, flow);
5094 ds_put_char(ds, '\n');
5096 ofpbuf_init(&trace.odp_actions, 0);
5099 trace.key = flow; /* Original flow key, used for megaflow. */
5100 trace.flow = *flow; /* May be modified by actions. */
5101 xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
5102 ntohs(flow->tcp_flags), packet, &trace.wc,
5103 &trace.odp_actions);
5104 trace.xin.ofpacts = ofpacts;
5105 trace.xin.ofpacts_len = ofpacts_len;
5106 trace.xin.resubmit_hook = trace_resubmit;
5107 trace.xin.report_hook = trace_report_valist;
5109 error = xlate_actions(&trace.xin, &trace.xout);
5110 ds_put_char(ds, '\n');
5111 trace_format_flow(ds, 0, "Final flow", &trace);
5112 trace_format_megaflow(ds, 0, "Megaflow", &trace);
5114 ds_put_cstr(ds, "Datapath actions: ");
5115 format_odp_actions(ds, trace.odp_actions.data, trace.odp_actions.size);
5117 if (error != XLATE_OK) {
5118 ds_put_format(ds, "\nTranslation failed (%s), packet is dropped.\n",
5119 xlate_strerror(error));
5120 } else if (trace.xout.slow) {
5121 enum slow_path_reason slow;
5123 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
5124 "slow path because it:");
5126 slow = trace.xout.slow;
5128 enum slow_path_reason bit = rightmost_1bit(slow);
5130 ds_put_format(ds, "\n\t- %s.",
5131 slow_path_reason_to_explanation(bit));
5137 xlate_out_uninit(&trace.xout);
5138 ofpbuf_uninit(&trace.odp_actions);
5141 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5142 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5143 * to destroy 'ofproto_shash' and free the returned value. */
5144 static const struct shash_node **
5145 get_ofprotos(struct shash *ofproto_shash)
5147 const struct ofproto_dpif *ofproto;
5149 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5150 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5151 shash_add_nocopy(ofproto_shash, name, ofproto);
5154 return shash_sort(ofproto_shash);
5158 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5159 const char *argv[] OVS_UNUSED,
5160 void *aux OVS_UNUSED)
5162 struct ds ds = DS_EMPTY_INITIALIZER;
5163 struct shash ofproto_shash;
5164 const struct shash_node **sorted_ofprotos;
5167 shash_init(&ofproto_shash);
5168 sorted_ofprotos = get_ofprotos(&ofproto_shash);
5169 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5170 const struct shash_node *node = sorted_ofprotos[i];
5171 ds_put_format(&ds, "%s\n", node->name);
5174 shash_destroy(&ofproto_shash);
5175 free(sorted_ofprotos);
5177 unixctl_command_reply(conn, ds_cstr(&ds));
5182 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5184 const struct shash_node **ofprotos;
5185 struct dpif_dp_stats dp_stats;
5186 struct shash ofproto_shash;
5189 dpif_get_dp_stats(backer->dpif, &dp_stats);
5191 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5192 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
5194 shash_init(&ofproto_shash);
5195 ofprotos = get_ofprotos(&ofproto_shash);
5196 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5197 struct ofproto_dpif *ofproto = ofprotos[i]->data;
5198 const struct shash_node **ports;
5201 if (ofproto->backer != backer) {
5205 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
5207 ports = shash_sort(&ofproto->up.port_by_name);
5208 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5209 const struct shash_node *node = ports[j];
5210 struct ofport *ofport = node->data;
5212 odp_port_t odp_port;
5214 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5217 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5218 if (odp_port != ODPP_NONE) {
5219 ds_put_format(ds, "%"PRIu32":", odp_port);
5221 ds_put_cstr(ds, "none:");
5224 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5227 if (!netdev_get_config(ofport->netdev, &config)) {
5228 const struct smap_node **nodes;
5231 nodes = smap_sort(&config);
5232 for (i = 0; i < smap_count(&config); i++) {
5233 const struct smap_node *node = nodes[i];
5234 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5235 node->key, node->value);
5239 smap_destroy(&config);
5241 ds_put_char(ds, ')');
5242 ds_put_char(ds, '\n');
5246 shash_destroy(&ofproto_shash);
5251 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5252 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5254 struct ds ds = DS_EMPTY_INITIALIZER;
5255 const struct shash_node **backers;
5258 backers = shash_sort(&all_dpif_backers);
5259 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5260 dpif_show_backer(backers[i]->data, &ds);
5264 unixctl_command_reply(conn, ds_cstr(&ds));
5269 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5270 int argc OVS_UNUSED, const char *argv[],
5271 void *aux OVS_UNUSED)
5273 const struct ofproto_dpif *ofproto;
5275 struct ds ds = DS_EMPTY_INITIALIZER;
5276 bool verbosity = false;
5278 struct dpif_port dpif_port;
5279 struct dpif_port_dump port_dump;
5280 struct hmap portno_names;
5282 struct dpif_flow_dump *flow_dump;
5283 struct dpif_flow_dump_thread *flow_dump_thread;
5287 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5289 unixctl_command_reply_error(conn, "no such bridge");
5293 if (argc > 2 && !strcmp(argv[1], "-m")) {
5297 hmap_init(&portno_names);
5298 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5299 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5303 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
5304 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5305 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5308 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5309 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5314 odp_format_ufid(&f.ufid, &ds);
5315 ds_put_cstr(&ds, " ");
5317 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5318 &portno_names, &ds, verbosity);
5319 ds_put_cstr(&ds, ", ");
5320 dpif_flow_stats_format(&f.stats, &ds);
5321 ds_put_cstr(&ds, ", actions:");
5322 format_odp_actions(&ds, f.actions, f.actions_len);
5323 ds_put_char(&ds, '\n');
5325 dpif_flow_dump_thread_destroy(flow_dump_thread);
5326 error = dpif_flow_dump_destroy(flow_dump);
5330 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5331 unixctl_command_reply_error(conn, ds_cstr(&ds));
5333 unixctl_command_reply(conn, ds_cstr(&ds));
5335 odp_portno_names_destroy(&portno_names);
5336 hmap_destroy(&portno_names);
5341 ofproto_revalidate_all_backers(void)
5343 const struct shash_node **backers;
5346 backers = shash_sort(&all_dpif_backers);
5347 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5348 struct dpif_backer *backer = backers[i]->data;
5349 backer->need_revalidate = REV_RECONFIGURE;
5355 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5356 const char *argv[], void *aux OVS_UNUSED)
5358 if (!strcasecmp(argv[1], "off")) {
5359 ofproto_use_tnl_push_pop = false;
5360 unixctl_command_reply(conn, "Tunnel push-pop off");
5361 ofproto_revalidate_all_backers();
5362 } else if (!strcasecmp(argv[1], "on")) {
5363 ofproto_use_tnl_push_pop = true;
5364 unixctl_command_reply(conn, "Tunnel push-pop on");
5365 ofproto_revalidate_all_backers();
5367 unixctl_command_reply_error(conn, "Invalid argument");
5372 ofproto_unixctl_init(void)
5374 static bool registered;
5380 unixctl_command_register(
5382 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5383 1, 3, ofproto_unixctl_trace, NULL);
5384 unixctl_command_register(
5385 "ofproto/trace-packet-out",
5386 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5387 2, 6, ofproto_unixctl_trace_actions, NULL);
5388 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5389 ofproto_unixctl_fdb_flush, NULL);
5390 unixctl_command_register("fdb/show", "bridge", 1, 1,
5391 ofproto_unixctl_fdb_show, NULL);
5392 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5393 ofproto_unixctl_mcast_snooping_flush, NULL);
5394 unixctl_command_register("mdb/show", "bridge", 1, 1,
5395 ofproto_unixctl_mcast_snooping_show, NULL);
5396 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5397 ofproto_unixctl_dpif_dump_dps, NULL);
5398 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5400 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5401 ofproto_unixctl_dpif_dump_flows, NULL);
5403 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5404 disable_tnl_push_pop, NULL);
5407 /* Returns true if 'table' is the table used for internal rules,
5408 * false otherwise. */
5410 table_is_internal(uint8_t table_id)
5412 return table_id == TBL_INTERNAL;
5415 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5417 * This is deprecated. It is only for compatibility with broken device drivers
5418 * in old versions of Linux that do not properly support VLANs when VLAN
5419 * devices are not used. When broken device drivers are no longer in
5420 * widespread use, we will delete these interfaces. */
5423 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
5425 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5426 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5428 if (realdev_ofp_port == ofport->realdev_ofp_port
5429 && vid == ofport->vlandev_vid) {
5433 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5435 if (ofport->realdev_ofp_port) {
5438 if (realdev_ofp_port && ofport->bundle) {
5439 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5440 * themselves be part of a bundle. */
5441 bundle_set(ofport_->ofproto, ofport->bundle, NULL);
5444 ofport->realdev_ofp_port = realdev_ofp_port;
5445 ofport->vlandev_vid = vid;
5447 if (realdev_ofp_port) {
5448 vsp_add(ofport, realdev_ofp_port, vid);
5455 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
5457 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
5461 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
5462 OVS_EXCLUDED(ofproto->vsp_mutex)
5464 /* hmap_is_empty is thread safe. */
5465 return !hmap_is_empty(&ofproto->realdev_vid_map);
5470 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
5471 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5472 OVS_REQUIRES(ofproto->vsp_mutex)
5474 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5475 int vid = vlan_tci_to_vid(vlan_tci);
5476 const struct vlan_splinter *vsp;
5478 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5479 hash_realdev_vid(realdev_ofp_port, vid),
5480 &ofproto->realdev_vid_map) {
5481 if (vsp->realdev_ofp_port == realdev_ofp_port
5482 && vsp->vid == vid) {
5483 return vsp->vlandev_ofp_port;
5487 return realdev_ofp_port;
5490 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5491 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5492 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
5493 * 'vlan_tci' 9, it would return the port number of eth0.9.
5495 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5496 * function just returns its 'realdev_ofp_port' argument. */
5498 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5499 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5500 OVS_EXCLUDED(ofproto->vsp_mutex)
5504 /* hmap_is_empty is thread safe, see if we can return immediately. */
5505 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
5506 return realdev_ofp_port;
5508 ovs_mutex_lock(&ofproto->vsp_mutex);
5509 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
5510 ovs_mutex_unlock(&ofproto->vsp_mutex);
5514 static struct vlan_splinter *
5515 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
5517 struct vlan_splinter *vsp;
5519 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
5520 hash_ofp_port(vlandev_ofp_port),
5521 &ofproto->vlandev_map) {
5522 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5530 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5531 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5532 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
5533 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5534 * eth0 and store 9 in '*vid'.
5536 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5537 * VLAN device. Unless VLAN splinters are enabled, this is what this function
5540 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5541 ofp_port_t vlandev_ofp_port, int *vid)
5542 OVS_REQUIRES(ofproto->vsp_mutex)
5544 if (!hmap_is_empty(&ofproto->vlandev_map)) {
5545 const struct vlan_splinter *vsp;
5547 vsp = vlandev_find(ofproto, vlandev_ofp_port);
5552 return vsp->realdev_ofp_port;
5558 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5559 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
5560 * 'flow->in_port' to the "real" device backing the VLAN device, sets
5561 * 'flow->vlan_tci' to the VLAN VID, and returns true. Optionally pushes the
5562 * appropriate VLAN on 'packet' if provided. Otherwise (which is always the
5563 * case unless VLAN splinters are enabled), returns false without making any
5566 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow,
5567 struct dp_packet *packet)
5568 OVS_EXCLUDED(ofproto->vsp_mutex)
5573 /* hmap_is_empty is thread safe. */
5574 if (hmap_is_empty(&ofproto->vlandev_map)) {
5578 ovs_mutex_lock(&ofproto->vsp_mutex);
5579 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
5580 ovs_mutex_unlock(&ofproto->vsp_mutex);
5585 /* Cause the flow to be processed as if it came in on the real device with
5586 * the VLAN device's VLAN ID. */
5587 flow->in_port.ofp_port = realdev;
5588 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
5591 /* Make the packet resemble the flow, so that it gets sent to an
5592 * OpenFlow controller properly, so that it looks correct for sFlow,
5593 * and so that flow_extract() will get the correct vlan_tci if it is
5594 * called on 'packet'. */
5595 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
5602 vsp_remove(struct ofport_dpif *port)
5604 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5605 struct vlan_splinter *vsp;
5607 ovs_mutex_lock(&ofproto->vsp_mutex);
5608 vsp = vlandev_find(ofproto, port->up.ofp_port);
5610 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5611 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5614 port->realdev_ofp_port = 0;
5616 VLOG_ERR("missing vlan device record");
5618 ovs_mutex_unlock(&ofproto->vsp_mutex);
5622 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
5624 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5626 ovs_mutex_lock(&ofproto->vsp_mutex);
5627 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
5628 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
5629 == realdev_ofp_port)) {
5630 struct vlan_splinter *vsp;
5632 vsp = xmalloc(sizeof *vsp);
5633 vsp->realdev_ofp_port = realdev_ofp_port;
5634 vsp->vlandev_ofp_port = port->up.ofp_port;
5637 port->realdev_ofp_port = realdev_ofp_port;
5639 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
5640 hash_ofp_port(port->up.ofp_port));
5641 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
5642 hash_realdev_vid(realdev_ofp_port, vid));
5644 VLOG_ERR("duplicate vlan device record");
5646 ovs_mutex_unlock(&ofproto->vsp_mutex);
5650 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5652 const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
5653 return ofport ? ofport->odp_port : ODPP_NONE;
5656 struct ofport_dpif *
5657 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5659 struct ofport_dpif *port;
5661 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5662 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5663 &backer->odp_to_ofport_map) {
5664 if (port->odp_port == odp_port) {
5665 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5670 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5675 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5677 struct ofport_dpif *port;
5679 port = odp_port_to_ofport(ofproto->backer, odp_port);
5680 if (port && &ofproto->up == port->up.ofproto) {
5681 return port->up.ofp_port;
5688 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5689 const struct match *match, int priority,
5690 uint16_t idle_timeout,
5691 const struct ofpbuf *ofpacts,
5692 struct rule **rulep)
5694 struct ofproto_flow_mod ofm;
5695 struct rule_dpif *rule;
5698 ofm.fm = (struct ofputil_flow_mod) {
5700 .priority = priority,
5701 .table_id = TBL_INTERNAL,
5702 .command = OFPFC_ADD,
5703 .idle_timeout = idle_timeout,
5704 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5705 .ofpacts = ofpacts->data,
5706 .ofpacts_len = ofpacts->size,
5707 .delete_reason = OVS_OFPRR_NONE,
5710 error = ofproto_flow_mod(&ofproto->up, &ofm);
5712 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5713 ofperr_to_string(error));
5718 rule = rule_dpif_lookup_in_table(ofproto,
5719 ofproto_dpif_get_tables_version(ofproto),
5720 TBL_INTERNAL, &ofm.fm.match.flow,
5731 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5732 struct match *match, int priority)
5734 struct ofproto_flow_mod ofm;
5737 ofm.fm = (struct ofputil_flow_mod) {
5739 .priority = priority,
5740 .table_id = TBL_INTERNAL,
5741 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5742 .command = OFPFC_DELETE_STRICT,
5745 error = ofproto_flow_mod(&ofproto->up, &ofm);
5747 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5748 ofperr_to_string(error));
5756 ofproto_dpif_get_uuid(const struct ofproto_dpif *ofproto)
5758 return &ofproto->uuid;
5761 const struct ofproto_class ofproto_dpif_class = {
5775 NULL, /* get_memory_usage. */
5776 type_get_memory_usage,
5795 port_is_lacp_current,
5796 port_get_lacp_stats,
5797 NULL, /* rule_choose_table */
5822 aa_vlan_get_queue_size,
5829 get_stp_port_status,
5834 get_rstp_port_status,
5841 is_mirror_output_bundle,
5842 forward_bpdu_changed,
5843 set_mac_table_config,
5845 set_mcast_snooping_port,
5847 NULL, /* meter_get_features */
5848 NULL, /* meter_set */
5849 NULL, /* meter_get */
5850 NULL, /* meter_del */
5851 group_alloc, /* group_alloc */
5852 group_construct, /* group_construct */
5853 group_destruct, /* group_destruct */
5854 group_dealloc, /* group_dealloc */
5855 group_modify, /* group_modify */
5856 group_get_stats, /* group_get_stats */
5857 get_datapath_version, /* get_datapath_version */