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.
23 #include "byte-order.h"
24 #include "connectivity.h"
29 #include "fail-open.h"
30 #include "guarded-list.h"
34 #include "mac-learning.h"
35 #include "mcast-snooping.h"
36 #include "multipath.h"
37 #include "netdev-vport.h"
42 #include "odp-execute.h"
43 #include "ofproto/ofproto-dpif.h"
44 #include "ofproto/ofproto-provider.h"
45 #include "ofproto-dpif-ipfix.h"
46 #include "ofproto-dpif-mirror.h"
47 #include "ofproto-dpif-monitor.h"
48 #include "ofproto-dpif-rid.h"
49 #include "ofproto-dpif-sflow.h"
50 #include "ofproto-dpif-upcall.h"
51 #include "ofproto-dpif-xlate.h"
52 #include "openvswitch/ofp-actions.h"
53 #include "openvswitch/dynamic-string.h"
54 #include "openvswitch/meta-flow.h"
55 #include "openvswitch/ofp-parse.h"
56 #include "openvswitch/ofp-print.h"
57 #include "openvswitch/ofp-util.h"
58 #include "openvswitch/ofpbuf.h"
59 #include "openvswitch/vlog.h"
62 #include "ovs-router.h"
63 #include "poll-loop.h"
69 #include "unaligned.h"
71 #include "vlan-bitmap.h"
73 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
75 COVERAGE_DEFINE(ofproto_dpif_expired);
76 COVERAGE_DEFINE(packet_in_overflow);
85 * - Do include packets and bytes from datapath flows which have not
86 * recently been processed by a revalidator. */
87 struct ovs_mutex stats_mutex;
88 struct dpif_flow_stats stats OVS_GUARDED;
90 /* In non-NULL, will point to a new rule (for which a reference is held) to
91 * which all the stats updates should be forwarded. This exists only
92 * transitionally when flows are replaced.
94 * Protected by stats_mutex. If both 'rule->stats_mutex' and
95 * 'rule->new_rule->stats_mutex' must be held together, acquire them in that
97 struct rule_dpif *new_rule OVS_GUARDED;
99 /* If non-zero then the recirculation id that has
100 * been allocated for use with this rule.
101 * The recirculation id and associated internal flow should
102 * be freed when the rule is freed */
106 /* RULE_CAST() depends on this. */
107 BUILD_ASSERT_DECL(offsetof(struct rule_dpif, up) == 0);
109 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
110 long long int *used);
111 static struct rule_dpif *rule_dpif_cast(const struct rule *);
112 static void rule_expire(struct rule_dpif *, long long now);
119 * - Do include packets and bytes from datapath flows which have not
120 * recently been processed by a revalidator. */
121 struct ovs_mutex stats_mutex;
122 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
123 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
127 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
128 struct ofproto_dpif *ofproto; /* Owning ofproto. */
129 void *aux; /* Key supplied by ofproto's client. */
130 char *name; /* Identifier for log messages. */
133 struct ovs_list ports; /* Contains "struct ofport"s. */
134 enum port_vlan_mode vlan_mode; /* VLAN mode */
135 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
136 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
137 * NULL if all VLANs are trunked. */
138 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
139 struct bond *bond; /* Nonnull iff more than one port. */
140 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
143 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
146 static void bundle_remove(struct ofport *);
147 static void bundle_update(struct ofbundle *);
148 static void bundle_destroy(struct ofbundle *);
149 static void bundle_del_port(struct ofport_dpif *);
150 static void bundle_run(struct ofbundle *);
151 static void bundle_wait(struct ofbundle *);
152 static void bundle_flush_macs(struct ofbundle *, bool);
153 static void bundle_move(struct ofbundle *, struct ofbundle *);
155 static void stp_run(struct ofproto_dpif *ofproto);
156 static void stp_wait(struct ofproto_dpif *ofproto);
157 static int set_stp_port(struct ofport *,
158 const struct ofproto_port_stp_settings *);
160 static void rstp_run(struct ofproto_dpif *ofproto);
161 static void set_rstp_port(struct ofport *,
162 const struct ofproto_port_rstp_settings *);
165 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
169 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
170 struct ovs_list bundle_node;/* In struct ofbundle's "ports" list. */
171 struct cfm *cfm; /* Connectivity Fault Management, if any. */
172 struct bfd *bfd; /* BFD, if any. */
173 struct lldp *lldp; /* lldp, if any. */
174 bool may_enable; /* May be enabled in bonds. */
175 bool is_tunnel; /* This port is a tunnel. */
176 bool is_layer3; /* This is a layer 3 port. */
177 long long int carrier_seq; /* Carrier status changes. */
178 struct ofport_dpif *peer; /* Peer if patch port. */
181 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
182 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
183 long long int stp_state_entered;
185 /* Rapid Spanning Tree. */
186 struct rstp_port *rstp_port; /* Rapid Spanning Tree Protocol, if any. */
187 enum rstp_state rstp_state; /* Always RSTP_DISABLED if RSTP not in use. */
189 /* Queue to DSCP mapping. */
190 struct ofproto_port_queue *qdscp;
194 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
197 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
200 static struct ofport_dpif *
201 ofport_dpif_cast(const struct ofport *ofport)
203 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
206 static void port_run(struct ofport_dpif *);
207 static int set_bfd(struct ofport *, const struct smap *);
208 static int set_cfm(struct ofport *, const struct cfm_settings *);
209 static int set_lldp(struct ofport *ofport_, const struct smap *cfg);
210 static void ofport_update_peer(struct ofport_dpif *);
212 /* Reasons that we might need to revalidate every datapath flow, and
213 * corresponding coverage counters.
215 * A value of 0 means that there is no need to revalidate.
217 * It would be nice to have some cleaner way to integrate with coverage
218 * counters, but with only a few reasons I guess this is good enough for
220 enum revalidate_reason {
221 REV_RECONFIGURE = 1, /* Switch configuration changed. */
222 REV_STP, /* Spanning tree protocol port status change. */
223 REV_RSTP, /* RSTP port status change. */
224 REV_BOND, /* Bonding changed. */
225 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
226 REV_FLOW_TABLE, /* Flow table changed. */
227 REV_MAC_LEARNING, /* Mac learning changed. */
228 REV_MCAST_SNOOPING, /* Multicast snooping changed. */
230 COVERAGE_DEFINE(rev_reconfigure);
231 COVERAGE_DEFINE(rev_stp);
232 COVERAGE_DEFINE(rev_rstp);
233 COVERAGE_DEFINE(rev_bond);
234 COVERAGE_DEFINE(rev_port_toggled);
235 COVERAGE_DEFINE(rev_flow_table);
236 COVERAGE_DEFINE(rev_mac_learning);
237 COVERAGE_DEFINE(rev_mcast_snooping);
239 /* All datapaths of a given type share a single dpif backer instance. */
246 struct ovs_rwlock odp_to_ofport_lock;
247 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
249 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
251 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
253 bool recv_set_enable; /* Enables or disables receiving packets. */
255 /* Version string of the datapath stored in OVSDB. */
256 char *dp_version_string;
258 /* Datapath feature support. */
259 struct dpif_backer_support support;
260 struct atomic_count tnl_count;
263 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
264 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
266 struct ofproto_dpif {
267 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
269 struct dpif_backer *backer;
271 /* Unique identifier for this instantiation of this bridge in this running
275 ATOMIC(cls_version_t) tables_version; /* For classifier lookups. */
277 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
279 /* Special OpenFlow rules. */
280 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
281 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
282 struct rule_dpif *drop_frags_rule; /* Used in OFPUTIL_FRAG_DROP mode. */
285 struct netflow *netflow;
286 struct dpif_sflow *sflow;
287 struct dpif_ipfix *ipfix;
288 struct hmap bundles; /* Contains "struct ofbundle"s. */
289 struct mac_learning *ml;
290 struct mcast_snooping *ms;
291 bool has_bonded_bundles;
293 struct mbridge *mbridge;
295 struct ovs_mutex stats_mutex;
296 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
297 * consumed in userspace. */
301 long long int stp_last_tick;
303 /* Rapid Spanning Tree. */
305 long long int rstp_last_tick;
308 struct sset ports; /* Set of standard port names. */
309 struct sset ghost_ports; /* Ports with no datapath port. */
310 struct sset port_poll_set; /* Queued names for port_poll() reply. */
311 int port_poll_errno; /* Last errno for port_poll() reply. */
312 uint64_t change_seq; /* Connectivity status changes. */
315 struct guarded_list ams; /* Contains "struct ofproto_async_msgs"s. */
316 struct seq *ams_seq; /* For notifying 'ams' reception. */
320 /* All existing ofproto_dpif instances, indexed by ->up.name. */
321 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
323 static bool ofproto_use_tnl_push_pop = true;
324 static void ofproto_unixctl_init(void);
326 static inline struct ofproto_dpif *
327 ofproto_dpif_cast(const struct ofproto *ofproto)
329 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
330 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
334 ofproto_dpif_get_enable_ufid(const struct dpif_backer *backer)
336 return backer->support.ufid;
339 struct dpif_backer_support *
340 ofproto_dpif_get_support(const struct ofproto_dpif *ofproto)
342 return &ofproto->backer->support;
345 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
346 const struct dp_packet *packet,
347 const struct ofpact[], size_t ofpacts_len,
350 /* Global variables. */
351 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
353 /* Initial mappings of port to bridge mappings. */
354 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
356 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
359 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
360 const struct ofputil_flow_mod *fm)
362 struct ofproto_flow_mod ofm;
364 /* Multiple threads may do this for the same 'fm' at the same time.
365 * Allocate ofproto_flow_mod with execution context from stack.
367 * Note: This copy could be avoided by making ofproto_flow_mod more
368 * complex, but that may not be desireable, and a learn action is not that
369 * fast to begin with. */
371 ofproto_flow_mod(&ofproto->up, &ofm);
374 /* Appends 'am' to the queue of asynchronous messages to be sent to the
375 * controller. Takes ownership of 'am' and any data it points to. */
377 ofproto_dpif_send_async_msg(struct ofproto_dpif *ofproto,
378 struct ofproto_async_msg *am)
380 if (!guarded_list_push_back(&ofproto->ams, &am->list_node, 1024)) {
381 COVERAGE_INC(packet_in_overflow);
382 ofproto_async_msg_free(am);
385 /* Wakes up main thread for packet-in I/O. */
386 seq_change(ofproto->ams_seq);
389 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
390 * packet rather than to send the packet to the controller.
392 * This function returns false to indicate that a packet_in message
393 * for a "table-miss" should be sent to at least one controller.
394 * False otherwise. */
396 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
398 return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
401 /* Factory functions. */
404 init(const struct shash *iface_hints)
406 struct shash_node *node;
408 /* Make a local copy, since we don't own 'iface_hints' elements. */
409 SHASH_FOR_EACH(node, iface_hints) {
410 const struct iface_hint *orig_hint = node->data;
411 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
413 new_hint->br_name = xstrdup(orig_hint->br_name);
414 new_hint->br_type = xstrdup(orig_hint->br_type);
415 new_hint->ofp_port = orig_hint->ofp_port;
417 shash_add(&init_ofp_ports, node->name, new_hint);
420 ofproto_unixctl_init();
425 enumerate_types(struct sset *types)
427 dp_enumerate_types(types);
431 enumerate_names(const char *type, struct sset *names)
433 struct ofproto_dpif *ofproto;
436 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
437 if (strcmp(type, ofproto->up.type)) {
440 sset_add(names, ofproto->up.name);
447 del(const char *type, const char *name)
452 error = dpif_open(name, type, &dpif);
454 error = dpif_delete(dpif);
461 port_open_type(const char *datapath_type, const char *port_type)
463 return dpif_port_open_type(datapath_type, port_type);
466 /* Type functions. */
468 static void process_dpif_port_changes(struct dpif_backer *);
469 static void process_dpif_all_ports_changed(struct dpif_backer *);
470 static void process_dpif_port_change(struct dpif_backer *,
471 const char *devname);
472 static void process_dpif_port_error(struct dpif_backer *, int error);
474 static struct ofproto_dpif *
475 lookup_ofproto_dpif_by_port_name(const char *name)
477 struct ofproto_dpif *ofproto;
479 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
480 if (sset_contains(&ofproto->ports, name)) {
489 ofproto_dpif_backer_enabled(struct dpif_backer* backer)
491 return backer->recv_set_enable;
495 type_run(const char *type)
497 struct dpif_backer *backer;
499 backer = shash_find_data(&all_dpif_backers, type);
501 /* This is not necessarily a problem, since backers are only
502 * created on demand. */
506 /* This must be called before dpif_run() */
507 dpif_poll_threads_set(backer->dpif, pmd_cpu_mask);
509 if (dpif_run(backer->dpif)) {
510 backer->need_revalidate = REV_RECONFIGURE;
513 udpif_run(backer->udpif);
515 /* If vswitchd started with other_config:flow_restore_wait set as "true",
516 * and the configuration has now changed to "false", enable receiving
517 * packets from the datapath. */
518 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
521 backer->recv_set_enable = true;
523 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
525 VLOG_ERR("Failed to enable receiving packets in dpif.");
528 dpif_flow_flush(backer->dpif);
529 backer->need_revalidate = REV_RECONFIGURE;
532 if (backer->recv_set_enable) {
533 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
536 if (backer->need_revalidate) {
537 struct ofproto_dpif *ofproto;
538 struct simap_node *node;
539 struct simap tmp_backers;
541 /* Handle tunnel garbage collection. */
542 simap_init(&tmp_backers);
543 simap_swap(&backer->tnl_backers, &tmp_backers);
545 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
546 struct ofport_dpif *iter;
548 if (backer != ofproto->backer) {
552 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
553 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
556 if (!iter->is_tunnel) {
560 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
561 namebuf, sizeof namebuf);
562 node = simap_find(&tmp_backers, dp_port);
564 simap_put(&backer->tnl_backers, dp_port, node->data);
565 simap_delete(&tmp_backers, node);
566 node = simap_find(&backer->tnl_backers, dp_port);
568 node = simap_find(&backer->tnl_backers, dp_port);
570 odp_port_t odp_port = ODPP_NONE;
572 if (!dpif_port_add(backer->dpif, iter->up.netdev,
574 simap_put(&backer->tnl_backers, dp_port,
575 odp_to_u32(odp_port));
576 node = simap_find(&backer->tnl_backers, dp_port);
581 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
582 if (tnl_port_reconfigure(iter, iter->up.netdev,
584 ovs_native_tunneling_is_on(ofproto), dp_port)) {
585 backer->need_revalidate = REV_RECONFIGURE;
590 SIMAP_FOR_EACH (node, &tmp_backers) {
591 dpif_port_del(backer->dpif, u32_to_odp(node->data));
593 simap_destroy(&tmp_backers);
595 switch (backer->need_revalidate) {
596 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
597 case REV_STP: COVERAGE_INC(rev_stp); break;
598 case REV_RSTP: COVERAGE_INC(rev_rstp); break;
599 case REV_BOND: COVERAGE_INC(rev_bond); break;
600 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
601 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
602 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
603 case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
605 backer->need_revalidate = 0;
607 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
608 struct ofport_dpif *ofport;
609 struct ofbundle *bundle;
611 if (ofproto->backer != backer) {
616 xlate_ofproto_set(ofproto, ofproto->up.name,
617 ofproto->backer->dpif, ofproto->ml,
618 ofproto->stp, ofproto->rstp, ofproto->ms,
619 ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
621 ofproto->up.forward_bpdu,
622 connmgr_has_in_band(ofproto->up.connmgr),
623 &ofproto->backer->support);
625 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
626 xlate_bundle_set(ofproto, bundle, bundle->name,
627 bundle->vlan_mode, bundle->vlan,
628 bundle->trunks, bundle->use_priority_tags,
629 bundle->bond, bundle->lacp,
633 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
634 int stp_port = ofport->stp_port
635 ? stp_port_no(ofport->stp_port)
637 xlate_ofport_set(ofproto, ofport->bundle, ofport,
638 ofport->up.ofp_port, ofport->odp_port,
639 ofport->up.netdev, ofport->cfm, ofport->bfd,
640 ofport->lldp, ofport->peer, stp_port,
641 ofport->rstp_port, ofport->qdscp,
642 ofport->n_qdscp, ofport->up.pp.config,
643 ofport->up.pp.state, ofport->is_tunnel,
649 udpif_revalidate(backer->udpif);
652 process_dpif_port_changes(backer);
657 /* Check for and handle port changes in 'backer''s dpif. */
659 process_dpif_port_changes(struct dpif_backer *backer)
665 error = dpif_port_poll(backer->dpif, &devname);
671 process_dpif_all_ports_changed(backer);
675 process_dpif_port_change(backer, devname);
680 process_dpif_port_error(backer, error);
687 process_dpif_all_ports_changed(struct dpif_backer *backer)
689 struct ofproto_dpif *ofproto;
690 struct dpif_port dpif_port;
691 struct dpif_port_dump dump;
692 struct sset devnames;
695 sset_init(&devnames);
696 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
697 if (ofproto->backer == backer) {
698 struct ofport *ofport;
700 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
701 sset_add(&devnames, netdev_get_name(ofport->netdev));
705 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
706 sset_add(&devnames, dpif_port.name);
709 SSET_FOR_EACH (devname, &devnames) {
710 process_dpif_port_change(backer, devname);
712 sset_destroy(&devnames);
716 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
718 struct ofproto_dpif *ofproto;
719 struct dpif_port port;
721 /* Don't report on the datapath's device. */
722 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
726 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
727 &all_ofproto_dpifs) {
728 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
733 ofproto = lookup_ofproto_dpif_by_port_name(devname);
734 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
735 /* The port was removed. If we know the datapath,
736 * report it through poll_set(). If we don't, it may be
737 * notifying us of a removal we initiated, so ignore it.
738 * If there's a pending ENOBUFS, let it stand, since
739 * everything will be reevaluated. */
740 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
741 sset_add(&ofproto->port_poll_set, devname);
742 ofproto->port_poll_errno = 0;
744 } else if (!ofproto) {
745 /* The port was added, but we don't know with which
746 * ofproto we should associate it. Delete it. */
747 dpif_port_del(backer->dpif, port.port_no);
749 struct ofport_dpif *ofport;
751 ofport = ofport_dpif_cast(shash_find_data(
752 &ofproto->up.port_by_name, devname));
754 && ofport->odp_port != port.port_no
755 && !odp_port_to_ofport(backer, port.port_no))
757 /* 'ofport''s datapath port number has changed from
758 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
759 * structures to match. */
760 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
761 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
762 ofport->odp_port = port.port_no;
763 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
764 hash_odp_port(port.port_no));
765 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
766 backer->need_revalidate = REV_RECONFIGURE;
769 dpif_port_destroy(&port);
772 /* Propagate 'error' to all ofprotos based on 'backer'. */
774 process_dpif_port_error(struct dpif_backer *backer, int error)
776 struct ofproto_dpif *ofproto;
778 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
779 if (ofproto->backer == backer) {
780 sset_clear(&ofproto->port_poll_set);
781 ofproto->port_poll_errno = error;
787 type_wait(const char *type)
789 struct dpif_backer *backer;
791 backer = shash_find_data(&all_dpif_backers, type);
793 /* This is not necessarily a problem, since backers are only
794 * created on demand. */
798 dpif_wait(backer->dpif);
801 /* Basic life-cycle. */
803 static int add_internal_flows(struct ofproto_dpif *);
805 static struct ofproto *
808 struct ofproto_dpif *ofproto = xzalloc(sizeof *ofproto);
813 dealloc(struct ofproto *ofproto_)
815 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
820 close_dpif_backer(struct dpif_backer *backer)
822 ovs_assert(backer->refcount > 0);
824 if (--backer->refcount) {
828 udpif_destroy(backer->udpif);
830 simap_destroy(&backer->tnl_backers);
831 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
832 hmap_destroy(&backer->odp_to_ofport_map);
833 shash_find_and_delete(&all_dpif_backers, backer->type);
835 free(backer->dp_version_string);
836 dpif_close(backer->dpif);
840 /* Datapath port slated for removal from datapath. */
842 struct ovs_list list_node;
846 static bool check_variable_length_userdata(struct dpif_backer *backer);
847 static void check_support(struct dpif_backer *backer);
850 open_dpif_backer(const char *type, struct dpif_backer **backerp)
852 struct dpif_backer *backer;
853 struct dpif_port_dump port_dump;
854 struct dpif_port port;
855 struct shash_node *node;
856 struct ovs_list garbage_list;
857 struct odp_garbage *garbage;
864 backer = shash_find_data(&all_dpif_backers, type);
871 backer_name = xasprintf("ovs-%s", type);
873 /* Remove any existing datapaths, since we assume we're the only
874 * userspace controlling the datapath. */
876 dp_enumerate_names(type, &names);
877 SSET_FOR_EACH(name, &names) {
878 struct dpif *old_dpif;
880 /* Don't remove our backer if it exists. */
881 if (!strcmp(name, backer_name)) {
885 if (dpif_open(name, type, &old_dpif)) {
886 VLOG_WARN("couldn't open old datapath %s to remove it", name);
888 dpif_delete(old_dpif);
889 dpif_close(old_dpif);
892 sset_destroy(&names);
894 backer = xmalloc(sizeof *backer);
896 error = dpif_create_and_open(backer_name, type, &backer->dpif);
899 VLOG_ERR("failed to open datapath of type %s: %s", type,
900 ovs_strerror(error));
904 backer->udpif = udpif_create(backer, backer->dpif);
906 backer->type = xstrdup(type);
907 backer->refcount = 1;
908 hmap_init(&backer->odp_to_ofport_map);
909 ovs_rwlock_init(&backer->odp_to_ofport_lock);
910 backer->need_revalidate = 0;
911 simap_init(&backer->tnl_backers);
912 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
915 if (backer->recv_set_enable) {
916 dpif_flow_flush(backer->dpif);
919 /* Loop through the ports already on the datapath and remove any
920 * that we don't need anymore. */
921 ovs_list_init(&garbage_list);
922 dpif_port_dump_start(&port_dump, backer->dpif);
923 while (dpif_port_dump_next(&port_dump, &port)) {
924 node = shash_find(&init_ofp_ports, port.name);
925 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
926 garbage = xmalloc(sizeof *garbage);
927 garbage->odp_port = port.port_no;
928 ovs_list_push_front(&garbage_list, &garbage->list_node);
931 dpif_port_dump_done(&port_dump);
933 LIST_FOR_EACH_POP (garbage, list_node, &garbage_list) {
934 dpif_port_del(backer->dpif, garbage->odp_port);
938 shash_add(&all_dpif_backers, type, backer);
940 check_support(backer);
941 atomic_count_init(&backer->tnl_count, 0);
943 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
945 VLOG_ERR("failed to listen on datapath of type %s: %s",
946 type, ovs_strerror(error));
947 close_dpif_backer(backer);
951 if (backer->recv_set_enable) {
952 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
955 /* This check fails if performed before udpif threads have been set,
956 * as the kernel module checks that the 'pid' in userspace action
958 backer->support.variable_length_userdata
959 = check_variable_length_userdata(backer);
960 backer->dp_version_string = dpif_get_dp_version(backer->dpif);
966 ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
968 return ofproto_use_tnl_push_pop && ofproto->backer->support.tnl_push_pop &&
969 atomic_count_get(&ofproto->backer->tnl_count);
972 /* Tests whether 'backer''s datapath supports recirculation. Only newer
973 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
974 * features on older datapaths that don't support this feature.
976 * Returns false if 'backer' definitely does not support recirculation, true if
977 * it seems to support recirculation or if at least the error we get is
980 check_recirc(struct dpif_backer *backer)
983 struct odputil_keybuf keybuf;
986 struct odp_flow_key_parms odp_parms = {
993 memset(&flow, 0, sizeof flow);
997 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
998 odp_flow_key_from_flow(&odp_parms, &key);
999 enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
1002 if (enable_recirc) {
1003 VLOG_INFO("%s: Datapath supports recirculation",
1004 dpif_name(backer->dpif));
1006 VLOG_INFO("%s: Datapath does not support recirculation",
1007 dpif_name(backer->dpif));
1010 return enable_recirc;
1013 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
1014 * some flow attributes for datapaths that support this feature.
1016 * Returns true if 'dpif' supports UFID for flow operations.
1017 * Returns false if 'dpif' does not support UFID. */
1019 check_ufid(struct dpif_backer *backer)
1022 struct odputil_keybuf keybuf;
1026 struct odp_flow_key_parms odp_parms = {
1030 memset(&flow, 0, sizeof flow);
1031 flow.dl_type = htons(0x1234);
1033 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1034 odp_flow_key_from_flow(&odp_parms, &key);
1035 dpif_flow_hash(backer->dpif, key.data, key.size, &ufid);
1037 enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, &ufid);
1040 VLOG_INFO("%s: Datapath supports unique flow ids",
1041 dpif_name(backer->dpif));
1043 VLOG_INFO("%s: Datapath does not support unique flow ids",
1044 dpif_name(backer->dpif));
1049 /* Tests whether 'backer''s datapath supports variable-length
1050 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1051 * to disable some features on older datapaths that don't support this
1054 * Returns false if 'backer' definitely does not support variable-length
1055 * userdata, true if it seems to support them or if at least the error we get
1058 check_variable_length_userdata(struct dpif_backer *backer)
1060 struct eth_header *eth;
1061 struct ofpbuf actions;
1062 struct dpif_execute execute;
1063 struct dp_packet packet;
1068 /* Compose a userspace action that will cause an ERANGE error on older
1069 * datapaths that don't support variable-length userdata.
1071 * We really test for using userdata longer than 8 bytes, but older
1072 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1073 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1074 * test for that instead as a proxy for longer userdata support. */
1075 ofpbuf_init(&actions, 64);
1076 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1077 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1078 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1079 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1080 nl_msg_end_nested(&actions, start);
1082 /* Compose a dummy ethernet packet. */
1083 dp_packet_init(&packet, ETH_HEADER_LEN);
1084 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1085 eth->eth_type = htons(0x1234);
1087 flow_extract(&packet, &flow);
1089 /* Execute the actions. On older datapaths this fails with ERANGE, on
1090 * newer datapaths it succeeds. */
1091 execute.actions = actions.data;
1092 execute.actions_len = actions.size;
1093 execute.packet = &packet;
1094 execute.flow = &flow;
1095 execute.needs_help = false;
1096 execute.probe = true;
1099 error = dpif_execute(backer->dpif, &execute);
1101 dp_packet_uninit(&packet);
1102 ofpbuf_uninit(&actions);
1109 /* Variable-length userdata is not supported. */
1110 VLOG_WARN("%s: datapath does not support variable-length userdata "
1111 "feature (needs Linux 3.10+ or kernel module from OVS "
1112 "1..11+). The NXAST_SAMPLE action will be ignored.",
1113 dpif_name(backer->dpif));
1117 /* Something odd happened. We're not sure whether variable-length
1118 * userdata is supported. Default to "yes". */
1119 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1120 dpif_name(backer->dpif), ovs_strerror(error));
1125 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1127 * Returns the number of elements in a struct flow's mpls_lse field
1128 * if the datapath supports at least that many entries in an
1130 * Otherwise returns the number of MPLS push actions supported by
1133 check_max_mpls_depth(struct dpif_backer *backer)
1138 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1139 struct odputil_keybuf keybuf;
1141 struct odp_flow_key_parms odp_parms = {
1145 memset(&flow, 0, sizeof flow);
1146 flow.dl_type = htons(ETH_TYPE_MPLS);
1147 flow_set_mpls_bos(&flow, n, 1);
1149 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1150 odp_flow_key_from_flow(&odp_parms, &key);
1151 if (!dpif_probe_feature(backer->dpif, "MPLS", &key, NULL)) {
1156 VLOG_INFO("%s: MPLS label stack length probed as %d",
1157 dpif_name(backer->dpif), n);
1161 /* Tests whether 'backer''s datapath supports masked data in
1162 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1163 * datapaths that don't support this feature. */
1165 check_masked_set_action(struct dpif_backer *backer)
1167 struct eth_header *eth;
1168 struct ofpbuf actions;
1169 struct dpif_execute execute;
1170 struct dp_packet packet;
1173 struct ovs_key_ethernet key, mask;
1175 /* Compose a set action that will cause an EINVAL error on older
1176 * datapaths that don't support masked set actions.
1177 * Avoid using a full mask, as it could be translated to a non-masked
1178 * set action instead. */
1179 ofpbuf_init(&actions, 64);
1180 memset(&key, 0x53, sizeof key);
1181 memset(&mask, 0x7f, sizeof mask);
1182 commit_masked_set_action(&actions, OVS_KEY_ATTR_ETHERNET, &key, &mask,
1185 /* Compose a dummy ethernet packet. */
1186 dp_packet_init(&packet, ETH_HEADER_LEN);
1187 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1188 eth->eth_type = htons(0x1234);
1190 flow_extract(&packet, &flow);
1192 /* Execute the actions. On older datapaths this fails with EINVAL, on
1193 * newer datapaths it succeeds. */
1194 execute.actions = actions.data;
1195 execute.actions_len = actions.size;
1196 execute.packet = &packet;
1197 execute.flow = &flow;
1198 execute.needs_help = false;
1199 execute.probe = true;
1202 error = dpif_execute(backer->dpif, &execute);
1204 dp_packet_uninit(&packet);
1205 ofpbuf_uninit(&actions);
1208 /* Masked set action is not supported. */
1209 VLOG_INFO("%s: datapath does not support masked set action feature.",
1210 dpif_name(backer->dpif));
1215 /* Tests whether 'backer''s datapath supports truncation of a packet in
1216 * OVS_ACTION_ATTR_TRUNC. We need to disable some features on older
1217 * datapaths that don't support this feature. */
1219 check_trunc_action(struct dpif_backer *backer)
1221 struct eth_header *eth;
1222 struct ofpbuf actions;
1223 struct dpif_execute execute;
1224 struct dp_packet packet;
1225 struct ovs_action_trunc *trunc;
1229 /* Compose an action with output(port:1,
1230 * max_len:OVS_ACTION_OUTPUT_MIN + 1).
1231 * This translates to one truncate action and one output action. */
1232 ofpbuf_init(&actions, 64);
1233 trunc = nl_msg_put_unspec_uninit(&actions,
1234 OVS_ACTION_ATTR_TRUNC, sizeof *trunc);
1236 trunc->max_len = ETH_HEADER_LEN + 1;
1237 nl_msg_put_odp_port(&actions, OVS_ACTION_ATTR_OUTPUT, u32_to_odp(1));
1239 /* Compose a dummy Ethernet packet. */
1240 dp_packet_init(&packet, ETH_HEADER_LEN);
1241 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1242 eth->eth_type = htons(0x1234);
1244 flow_extract(&packet, &flow);
1246 /* Execute the actions. On older datapaths this fails with EINVAL, on
1247 * newer datapaths it succeeds. */
1248 execute.actions = actions.data;
1249 execute.actions_len = actions.size;
1250 execute.packet = &packet;
1251 execute.flow = &flow;
1252 execute.needs_help = false;
1253 execute.probe = true;
1256 error = dpif_execute(backer->dpif, &execute);
1258 dp_packet_uninit(&packet);
1259 ofpbuf_uninit(&actions);
1262 VLOG_INFO("%s: Datapath does not support truncate action",
1263 dpif_name(backer->dpif));
1265 VLOG_INFO("%s: Datapath supports truncate action",
1266 dpif_name(backer->dpif));
1272 #define CHECK_FEATURE__(NAME, SUPPORT, FIELD, VALUE) \
1274 check_##NAME(struct dpif_backer *backer) \
1277 struct odputil_keybuf keybuf; \
1278 struct ofpbuf key; \
1280 struct odp_flow_key_parms odp_parms = { \
1287 memset(&flow, 0, sizeof flow); \
1288 flow.FIELD = VALUE; \
1290 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf); \
1291 odp_flow_key_from_flow(&odp_parms, &key); \
1292 enable = dpif_probe_feature(backer->dpif, #NAME, &key, NULL); \
1295 VLOG_INFO("%s: Datapath supports "#NAME, dpif_name(backer->dpif)); \
1297 VLOG_INFO("%s: Datapath does not support "#NAME, \
1298 dpif_name(backer->dpif)); \
1303 #define CHECK_FEATURE(FIELD) CHECK_FEATURE__(FIELD, FIELD, FIELD, 1)
1305 CHECK_FEATURE(ct_state)
1306 CHECK_FEATURE(ct_zone)
1307 CHECK_FEATURE(ct_mark)
1308 CHECK_FEATURE__(ct_label, ct_label, ct_label.u64.lo, 1)
1309 CHECK_FEATURE__(ct_state_nat, ct_state, ct_state, CS_TRACKED|CS_SRC_NAT)
1311 #undef CHECK_FEATURE
1312 #undef CHECK_FEATURE__
1315 check_support(struct dpif_backer *backer)
1317 /* This feature needs to be tested after udpif threads are set. */
1318 backer->support.variable_length_userdata = false;
1320 backer->support.odp.recirc = check_recirc(backer);
1321 backer->support.odp.max_mpls_depth = check_max_mpls_depth(backer);
1322 backer->support.masked_set_action = check_masked_set_action(backer);
1323 backer->support.trunc = check_trunc_action(backer);
1324 backer->support.ufid = check_ufid(backer);
1325 backer->support.tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
1327 backer->support.odp.ct_state = check_ct_state(backer);
1328 backer->support.odp.ct_zone = check_ct_zone(backer);
1329 backer->support.odp.ct_mark = check_ct_mark(backer);
1330 backer->support.odp.ct_label = check_ct_label(backer);
1332 backer->support.odp.ct_state_nat = check_ct_state_nat(backer);
1336 construct(struct ofproto *ofproto_)
1338 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1339 struct shash_node *node, *next;
1342 /* Tunnel module can get used right after the udpif threads are running. */
1343 ofproto_tunnel_init();
1345 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1350 uuid_generate(&ofproto->uuid);
1351 atomic_init(&ofproto->tables_version, CLS_MIN_VERSION);
1352 ofproto->netflow = NULL;
1353 ofproto->sflow = NULL;
1354 ofproto->ipfix = NULL;
1355 ofproto->stp = NULL;
1356 ofproto->rstp = NULL;
1357 ofproto->dump_seq = 0;
1358 hmap_init(&ofproto->bundles);
1359 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1361 ofproto->mbridge = mbridge_create();
1362 ofproto->has_bonded_bundles = false;
1363 ofproto->lacp_enabled = false;
1364 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1366 guarded_list_init(&ofproto->ams);
1368 sset_init(&ofproto->ports);
1369 sset_init(&ofproto->ghost_ports);
1370 sset_init(&ofproto->port_poll_set);
1371 ofproto->port_poll_errno = 0;
1372 ofproto->change_seq = 0;
1373 ofproto->ams_seq = seq_create();
1374 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
1377 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1378 struct iface_hint *iface_hint = node->data;
1380 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1381 /* Check if the datapath already has this port. */
1382 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1383 sset_add(&ofproto->ports, node->name);
1386 free(iface_hint->br_name);
1387 free(iface_hint->br_type);
1389 shash_delete(&init_ofp_ports, node);
1393 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1394 hash_string(ofproto->up.name, 0));
1395 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1397 ofproto_init_tables(ofproto_, N_TABLES);
1398 error = add_internal_flows(ofproto);
1400 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1406 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1407 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1413 match_init_catchall(&match);
1414 match_set_reg(&match, 0, id);
1416 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1418 *rulep = error ? NULL : rule_dpif_cast(rule);
1424 add_internal_flows(struct ofproto_dpif *ofproto)
1426 struct ofpact_controller *controller;
1427 uint64_t ofpacts_stub[128 / 8];
1428 struct ofpbuf ofpacts;
1429 struct rule *unused_rulep OVS_UNUSED;
1434 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1437 controller = ofpact_put_CONTROLLER(&ofpacts);
1438 controller->max_len = UINT16_MAX;
1439 controller->controller_id = 0;
1440 controller->reason = OFPR_IMPLICIT_MISS;
1441 ofpact_finish_CONTROLLER(&ofpacts, &controller);
1443 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1444 &ofproto->miss_rule);
1449 ofpbuf_clear(&ofpacts);
1450 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1451 &ofproto->no_packet_in_rule);
1456 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1457 &ofproto->drop_frags_rule);
1462 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1463 * zero when reaching this rule.
1465 * (priority=2), recirc_id=0, actions=drop
1467 ofpbuf_clear(&ofpacts);
1468 match_init_catchall(&match);
1469 match_set_recirc_id(&match, 0);
1470 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1476 destruct(struct ofproto *ofproto_)
1478 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1479 struct ofproto_async_msg *am;
1480 struct rule_dpif *rule;
1481 struct oftable *table;
1482 struct ovs_list ams;
1484 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1486 xlate_remove_ofproto(ofproto);
1489 /* Ensure that the upcall processing threads have no remaining references
1490 * to the ofproto or anything in it. */
1491 udpif_synchronize(ofproto->backer->udpif);
1493 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1495 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1496 CLS_FOR_EACH (rule, up.cr, &table->cls) {
1497 ofproto_rule_delete(&ofproto->up, &rule->up);
1500 ofproto_group_delete_all(&ofproto->up);
1502 guarded_list_pop_all(&ofproto->ams, &ams);
1503 LIST_FOR_EACH_POP (am, list_node, &ams) {
1504 ofproto_async_msg_free(am);
1506 guarded_list_destroy(&ofproto->ams);
1508 recirc_free_ofproto(ofproto, ofproto->up.name);
1510 mbridge_unref(ofproto->mbridge);
1512 netflow_unref(ofproto->netflow);
1513 dpif_sflow_unref(ofproto->sflow);
1514 dpif_ipfix_unref(ofproto->ipfix);
1515 hmap_destroy(&ofproto->bundles);
1516 mac_learning_unref(ofproto->ml);
1517 mcast_snooping_unref(ofproto->ms);
1519 sset_destroy(&ofproto->ports);
1520 sset_destroy(&ofproto->ghost_ports);
1521 sset_destroy(&ofproto->port_poll_set);
1523 ovs_mutex_destroy(&ofproto->stats_mutex);
1525 seq_destroy(ofproto->ams_seq);
1527 close_dpif_backer(ofproto->backer);
1531 run(struct ofproto *ofproto_)
1533 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1534 uint64_t new_seq, new_dump_seq;
1536 if (mbridge_need_revalidate(ofproto->mbridge)) {
1537 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1538 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1539 mac_learning_flush(ofproto->ml);
1540 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1541 mcast_snooping_mdb_flush(ofproto->ms);
1544 /* Always updates the ofproto->ams_seqno to avoid frequent wakeup during
1545 * flow restore. Even though nothing is processed during flow restore,
1546 * all queued 'ams' will be handled immediately when flow restore
1548 ofproto->ams_seqno = seq_read(ofproto->ams_seq);
1550 /* Do not perform any periodic activity required by 'ofproto' while
1551 * waiting for flow restore to complete. */
1552 if (!ofproto_get_flow_restore_wait()) {
1553 struct ofproto_async_msg *am;
1554 struct ovs_list ams;
1556 guarded_list_pop_all(&ofproto->ams, &ams);
1557 LIST_FOR_EACH_POP (am, list_node, &ams) {
1558 connmgr_send_async_msg(ofproto->up.connmgr, am);
1559 ofproto_async_msg_free(am);
1563 if (ofproto->netflow) {
1564 netflow_run(ofproto->netflow);
1566 if (ofproto->sflow) {
1567 dpif_sflow_run(ofproto->sflow);
1569 if (ofproto->ipfix) {
1570 dpif_ipfix_run(ofproto->ipfix);
1573 new_seq = seq_read(connectivity_seq_get());
1574 if (ofproto->change_seq != new_seq) {
1575 struct ofport_dpif *ofport;
1577 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1581 ofproto->change_seq = new_seq;
1583 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1584 struct ofbundle *bundle;
1586 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1593 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1594 if (mac_learning_run(ofproto->ml)) {
1595 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1597 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1599 if (mcast_snooping_run(ofproto->ms)) {
1600 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1603 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1604 if (ofproto->dump_seq != new_dump_seq) {
1605 struct rule *rule, *next_rule;
1606 long long now = time_msec();
1608 /* We know stats are relatively fresh, so now is a good time to do some
1610 ofproto->dump_seq = new_dump_seq;
1612 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1614 ovs_mutex_lock(&ofproto_mutex);
1615 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1616 &ofproto->up.expirable) {
1617 rule_expire(rule_dpif_cast(rule), now);
1619 ovs_mutex_unlock(&ofproto_mutex);
1621 /* All outstanding data in existing flows has been accounted, so it's a
1622 * good time to do bond rebalancing. */
1623 if (ofproto->has_bonded_bundles) {
1624 struct ofbundle *bundle;
1626 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1628 bond_rebalance(bundle->bond);
1637 ofproto_dpif_wait(struct ofproto *ofproto_)
1639 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1641 if (ofproto_get_flow_restore_wait()) {
1645 if (ofproto->sflow) {
1646 dpif_sflow_wait(ofproto->sflow);
1648 if (ofproto->ipfix) {
1649 dpif_ipfix_wait(ofproto->ipfix);
1651 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1652 struct ofbundle *bundle;
1654 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1655 bundle_wait(bundle);
1658 if (ofproto->netflow) {
1659 netflow_wait(ofproto->netflow);
1661 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1662 mac_learning_wait(ofproto->ml);
1663 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1664 mcast_snooping_wait(ofproto->ms);
1666 if (ofproto->backer->need_revalidate) {
1667 poll_immediate_wake();
1670 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1671 seq_wait(ofproto->ams_seq, ofproto->ams_seqno);
1675 type_get_memory_usage(const char *type, struct simap *usage)
1677 struct dpif_backer *backer;
1679 backer = shash_find_data(&all_dpif_backers, type);
1681 udpif_get_memory_usage(backer->udpif, usage);
1686 flush(struct ofproto *ofproto_)
1688 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1689 struct dpif_backer *backer = ofproto->backer;
1692 udpif_flush(backer->udpif);
1697 query_tables(struct ofproto *ofproto,
1698 struct ofputil_table_features *features,
1699 struct ofputil_table_stats *stats)
1701 strcpy(features->name, "classifier");
1706 for (i = 0; i < ofproto->n_tables; i++) {
1707 unsigned long missed, matched;
1709 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1710 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1712 stats[i].matched_count = matched;
1713 stats[i].lookup_count = matched + missed;
1719 set_tables_version(struct ofproto *ofproto_, cls_version_t version)
1721 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1723 atomic_store_relaxed(&ofproto->tables_version, version);
1724 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
1727 static struct ofport *
1730 struct ofport_dpif *port = xzalloc(sizeof *port);
1735 port_dealloc(struct ofport *port_)
1737 struct ofport_dpif *port = ofport_dpif_cast(port_);
1742 port_construct(struct ofport *port_)
1744 struct ofport_dpif *port = ofport_dpif_cast(port_);
1745 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1746 const struct netdev *netdev = port->up.netdev;
1747 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1748 const char *dp_port_name;
1749 struct dpif_port dpif_port;
1752 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1753 port->bundle = NULL;
1757 port->may_enable = false;
1758 port->stp_port = NULL;
1759 port->stp_state = STP_DISABLED;
1760 port->rstp_port = NULL;
1761 port->rstp_state = RSTP_DISABLED;
1762 port->is_tunnel = false;
1766 port->carrier_seq = netdev_get_carrier_resets(netdev);
1767 port->is_layer3 = netdev_vport_is_layer3(netdev);
1769 if (netdev_vport_is_patch(netdev)) {
1770 /* By bailing out here, we don't submit the port to the sFlow module
1771 * to be considered for counter polling export. This is correct
1772 * because the patch port represents an interface that sFlow considers
1773 * to be "internal" to the switch as a whole, and therefore not a
1774 * candidate for counter polling. */
1775 port->odp_port = ODPP_NONE;
1776 ofport_update_peer(port);
1780 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1781 error = dpif_port_query_by_name(ofproto->backer->dpif, dp_port_name,
1787 port->odp_port = dpif_port.port_no;
1789 if (netdev_get_tunnel_config(netdev)) {
1790 atomic_count_inc(&ofproto->backer->tnl_count);
1791 error = tnl_port_add(port, port->up.netdev, port->odp_port,
1792 ovs_native_tunneling_is_on(ofproto), dp_port_name);
1794 atomic_count_dec(&ofproto->backer->tnl_count);
1795 dpif_port_destroy(&dpif_port);
1799 port->is_tunnel = true;
1800 if (ofproto->ipfix) {
1801 dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1804 /* Sanity-check that a mapping doesn't already exist. This
1805 * shouldn't happen for non-tunnel ports. */
1806 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1807 VLOG_ERR("port %s already has an OpenFlow port number",
1809 dpif_port_destroy(&dpif_port);
1813 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1814 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1815 hash_odp_port(port->odp_port));
1816 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1818 dpif_port_destroy(&dpif_port);
1820 if (ofproto->sflow) {
1821 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1828 port_destruct(struct ofport *port_, bool del)
1830 struct ofport_dpif *port = ofport_dpif_cast(port_);
1831 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1832 const char *devname = netdev_get_name(port->up.netdev);
1833 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1834 const char *dp_port_name;
1836 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1838 xlate_ofport_remove(port);
1841 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1843 if (del && dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1844 /* The underlying device is still there, so delete it. This
1845 * happens when the ofproto is being destroyed, since the caller
1846 * assumes that removal of attached ports will happen as part of
1848 if (!port->is_tunnel) {
1849 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1854 port->peer->peer = NULL;
1858 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1859 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1860 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1861 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1864 if (port->is_tunnel) {
1865 atomic_count_dec(&ofproto->backer->tnl_count);
1868 if (port->is_tunnel && ofproto->ipfix) {
1869 dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1873 sset_find_and_delete(&ofproto->ports, devname);
1874 sset_find_and_delete(&ofproto->ghost_ports, devname);
1875 bundle_remove(port_);
1876 set_cfm(port_, NULL);
1877 set_bfd(port_, NULL);
1878 set_lldp(port_, NULL);
1879 if (port->stp_port) {
1880 stp_port_disable(port->stp_port);
1882 set_rstp_port(port_, NULL);
1883 if (ofproto->sflow) {
1884 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1891 port_modified(struct ofport *port_)
1893 struct ofport_dpif *port = ofport_dpif_cast(port_);
1894 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1895 const char *dp_port_name;
1896 struct netdev *netdev = port->up.netdev;
1898 if (port->bundle && port->bundle->bond) {
1899 bond_slave_set_netdev(port->bundle->bond, port, netdev);
1903 cfm_set_netdev(port->cfm, netdev);
1907 bfd_set_netdev(port->bfd, netdev);
1910 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1911 port->lldp, &port->up.pp.hw_addr);
1913 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1915 if (port->is_tunnel) {
1916 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1918 if (tnl_port_reconfigure(port, netdev, port->odp_port,
1919 ovs_native_tunneling_is_on(ofproto),
1921 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1925 ofport_update_peer(port);
1929 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1931 struct ofport_dpif *port = ofport_dpif_cast(port_);
1932 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1933 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1935 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1936 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1937 OFPUTIL_PC_NO_PACKET_IN)) {
1938 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1940 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1941 bundle_update(port->bundle);
1947 set_sflow(struct ofproto *ofproto_,
1948 const struct ofproto_sflow_options *sflow_options)
1950 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1951 struct dpif_sflow *ds = ofproto->sflow;
1953 if (sflow_options) {
1954 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
1956 struct ofport_dpif *ofport;
1958 ds = ofproto->sflow = dpif_sflow_create();
1959 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1960 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1963 dpif_sflow_set_options(ds, sflow_options);
1964 if (dpif_sflow_get_probability(ds) != old_probability) {
1965 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1969 dpif_sflow_unref(ds);
1970 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1971 ofproto->sflow = NULL;
1979 struct ofproto *ofproto_,
1980 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1981 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1982 size_t n_flow_exporters_options)
1984 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1985 struct dpif_ipfix *di = ofproto->ipfix;
1986 bool has_options = bridge_exporter_options || flow_exporters_options;
1987 bool new_di = false;
1989 if (has_options && !di) {
1990 di = ofproto->ipfix = dpif_ipfix_create();
1995 /* Call set_options in any case to cleanly flush the flow
1996 * caches in the last exporters that are to be destroyed. */
1997 dpif_ipfix_set_options(
1998 di, bridge_exporter_options, flow_exporters_options,
1999 n_flow_exporters_options);
2001 /* Add tunnel ports only when a new ipfix created */
2002 if (new_di == true) {
2003 struct ofport_dpif *ofport;
2004 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
2005 if (ofport->is_tunnel == true) {
2006 dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
2012 dpif_ipfix_unref(di);
2013 ofproto->ipfix = NULL;
2021 get_ipfix_stats(const struct ofproto *ofproto_,
2023 struct ovs_list *replies)
2025 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2026 struct dpif_ipfix *di = ofproto->ipfix;
2029 return OFPERR_NXST_NOT_CONFIGURED;
2032 return dpif_ipfix_get_stats(di, bridge_ipfix, replies);
2036 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
2038 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2039 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2040 struct cfm *old = ofport->cfm;
2045 ofport->cfm = cfm_create(ofport->up.netdev);
2048 if (cfm_configure(ofport->cfm, s)) {
2055 cfm_unref(ofport->cfm);
2058 if (ofport->cfm != old) {
2059 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2061 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2062 ofport->lldp, &ofport->up.pp.hw_addr);
2067 cfm_status_changed(struct ofport *ofport_)
2069 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2071 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
2075 get_cfm_status(const struct ofport *ofport_,
2076 struct cfm_status *status)
2078 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2082 cfm_get_status(ofport->cfm, status);
2091 set_bfd(struct ofport *ofport_, const struct smap *cfg)
2093 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2094 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2098 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2099 cfg, ofport->up.netdev);
2100 if (ofport->bfd != old) {
2101 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2103 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2104 ofport->lldp, &ofport->up.pp.hw_addr);
2109 bfd_status_changed(struct ofport *ofport_)
2111 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2113 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2117 get_bfd_status(struct ofport *ofport_, struct smap *smap)
2119 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2123 bfd_get_status(ofport->bfd, smap);
2132 set_lldp(struct ofport *ofport_,
2133 const struct smap *cfg)
2135 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2139 if (!ofport->lldp) {
2140 struct ofproto_dpif *ofproto;
2142 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2143 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2144 ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2147 if (!lldp_configure(ofport->lldp, cfg)) {
2152 lldp_unref(ofport->lldp);
2153 ofport->lldp = NULL;
2156 ofproto_dpif_monitor_port_update(ofport,
2160 &ofport->up.pp.hw_addr);
2165 get_lldp_status(const struct ofport *ofport_,
2166 struct lldp_status *status OVS_UNUSED)
2168 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2170 return ofport->lldp ? true : false;
2174 set_aa(struct ofproto *ofproto OVS_UNUSED,
2175 const struct aa_settings *s)
2177 return aa_configure(s);
2181 aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2182 const struct aa_mapping_settings *s)
2184 return aa_mapping_register(aux, s);
2188 aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2190 return aa_mapping_unregister(aux);
2194 aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2196 return aa_get_vlan_queued(list);
2200 aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2202 return aa_get_vlan_queue_size();
2206 /* Spanning Tree. */
2208 /* Called while rstp_mutex is held. */
2210 rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
2212 struct ofproto_dpif *ofproto = ofproto_;
2213 struct ofport_dpif *ofport = ofport_;
2214 struct eth_header *eth = dp_packet_l2(pkt);
2216 netdev_get_etheraddr(ofport->up.netdev, ð->eth_src);
2217 if (eth_addr_is_zero(eth->eth_src)) {
2218 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2219 "does not have a configured source MAC address.",
2220 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
2222 ofproto_dpif_send_packet(ofport, false, pkt);
2224 dp_packet_delete(pkt);
2228 send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
2230 struct ofproto_dpif *ofproto = ofproto_;
2231 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2232 struct ofport_dpif *ofport;
2234 ofport = stp_port_get_aux(sp);
2236 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2237 ofproto->up.name, port_num);
2239 struct eth_header *eth = dp_packet_l2(pkt);
2241 netdev_get_etheraddr(ofport->up.netdev, ð->eth_src);
2242 if (eth_addr_is_zero(eth->eth_src)) {
2243 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2244 "with unknown MAC", ofproto->up.name, port_num);
2246 ofproto_dpif_send_packet(ofport, false, pkt);
2249 dp_packet_delete(pkt);
2252 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2254 set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2256 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2258 /* Only revalidate flows if the configuration changed. */
2259 if (!s != !ofproto->rstp) {
2260 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2264 if (!ofproto->rstp) {
2265 ofproto->rstp = rstp_create(ofproto_->name, s->address,
2266 rstp_send_bpdu_cb, ofproto);
2267 ofproto->rstp_last_tick = time_msec();
2269 rstp_set_bridge_address(ofproto->rstp, s->address);
2270 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2271 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2272 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2273 s->force_protocol_version);
2274 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2275 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2276 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2277 s->transmit_hold_count);
2279 struct ofport *ofport;
2280 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2281 set_rstp_port(ofport, NULL);
2283 rstp_unref(ofproto->rstp);
2284 ofproto->rstp = NULL;
2289 get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2291 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2293 if (ofproto->rstp) {
2295 s->root_id = rstp_get_root_id(ofproto->rstp);
2296 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2297 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2298 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2299 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2300 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2307 update_rstp_port_state(struct ofport_dpif *ofport)
2309 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2310 enum rstp_state state;
2312 /* Figure out new state. */
2313 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2317 if (ofport->rstp_state != state) {
2318 enum ofputil_port_state of_state;
2321 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2322 netdev_get_name(ofport->up.netdev),
2323 rstp_state_name(ofport->rstp_state),
2324 rstp_state_name(state));
2326 if (rstp_learn_in_state(ofport->rstp_state)
2327 != rstp_learn_in_state(state)) {
2328 /* XXX: Learning action flows should also be flushed. */
2329 if (ofport->bundle) {
2330 if (!rstp_shift_root_learned_address(ofproto->rstp)
2331 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2332 bundle_flush_macs(ofport->bundle, false);
2336 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2337 != rstp_forward_in_state(state);
2339 ofproto->backer->need_revalidate = REV_RSTP;
2340 ofport->rstp_state = state;
2342 if (fwd_change && ofport->bundle) {
2343 bundle_update(ofport->bundle);
2346 /* Update the RSTP state bits in the OpenFlow port description. */
2347 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2348 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2349 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2350 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2352 ofproto_port_set_state(&ofport->up, of_state);
2357 rstp_run(struct ofproto_dpif *ofproto)
2359 if (ofproto->rstp) {
2360 long long int now = time_msec();
2361 long long int elapsed = now - ofproto->rstp_last_tick;
2362 struct rstp_port *rp;
2363 struct ofport_dpif *ofport;
2365 /* Every second, decrease the values of the timers. */
2366 if (elapsed >= 1000) {
2367 rstp_tick_timers(ofproto->rstp);
2368 ofproto->rstp_last_tick = now;
2371 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2372 update_rstp_port_state(ofport);
2376 /* FIXME: This check should be done on-event (i.e., when setting
2377 * p->fdb_flush) and not periodically.
2379 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2380 if (!rstp_shift_root_learned_address(ofproto->rstp)
2381 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2382 bundle_flush_macs(ofport->bundle, false);
2386 if (rstp_shift_root_learned_address(ofproto->rstp)) {
2387 struct ofport_dpif *old_root_aux =
2388 (struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp);
2389 struct ofport_dpif *new_root_aux =
2390 (struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp);
2391 if (old_root_aux != NULL && new_root_aux != NULL) {
2392 bundle_move(old_root_aux->bundle, new_root_aux->bundle);
2393 rstp_reset_root_changed(ofproto->rstp);
2399 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2401 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2403 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2405 /* Only revalidate flows if the configuration changed. */
2406 if (!s != !ofproto->stp) {
2407 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2411 if (!ofproto->stp) {
2412 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2413 send_bpdu_cb, ofproto);
2414 ofproto->stp_last_tick = time_msec();
2417 stp_set_bridge_id(ofproto->stp, s->system_id);
2418 stp_set_bridge_priority(ofproto->stp, s->priority);
2419 stp_set_hello_time(ofproto->stp, s->hello_time);
2420 stp_set_max_age(ofproto->stp, s->max_age);
2421 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2423 struct ofport *ofport;
2425 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2426 set_stp_port(ofport, NULL);
2429 stp_unref(ofproto->stp);
2430 ofproto->stp = NULL;
2437 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2439 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2443 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2444 s->designated_root = stp_get_designated_root(ofproto->stp);
2445 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2454 update_stp_port_state(struct ofport_dpif *ofport)
2456 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2457 enum stp_state state;
2459 /* Figure out new state. */
2460 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2464 if (ofport->stp_state != state) {
2465 enum ofputil_port_state of_state;
2468 VLOG_DBG("port %s: STP state changed from %s to %s",
2469 netdev_get_name(ofport->up.netdev),
2470 stp_state_name(ofport->stp_state),
2471 stp_state_name(state));
2472 if (stp_learn_in_state(ofport->stp_state)
2473 != stp_learn_in_state(state)) {
2474 /* xxx Learning action flows should also be flushed. */
2475 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2476 mac_learning_flush(ofproto->ml);
2477 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2478 mcast_snooping_mdb_flush(ofproto->ms);
2480 fwd_change = stp_forward_in_state(ofport->stp_state)
2481 != stp_forward_in_state(state);
2483 ofproto->backer->need_revalidate = REV_STP;
2484 ofport->stp_state = state;
2485 ofport->stp_state_entered = time_msec();
2487 if (fwd_change && ofport->bundle) {
2488 bundle_update(ofport->bundle);
2491 /* Update the STP state bits in the OpenFlow port description. */
2492 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2493 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2494 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2495 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2496 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2498 ofproto_port_set_state(&ofport->up, of_state);
2502 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2503 * caller is responsible for assigning STP port numbers and ensuring
2504 * there are no duplicates. */
2506 set_stp_port(struct ofport *ofport_,
2507 const struct ofproto_port_stp_settings *s)
2509 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2510 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2511 struct stp_port *sp = ofport->stp_port;
2513 if (!s || !s->enable) {
2515 ofport->stp_port = NULL;
2516 stp_port_disable(sp);
2517 update_stp_port_state(ofport);
2520 } else if (sp && stp_port_no(sp) != s->port_num
2521 && ofport == stp_port_get_aux(sp)) {
2522 /* The port-id changed, so disable the old one if it's not
2523 * already in use by another port. */
2524 stp_port_disable(sp);
2527 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2529 /* Set name before enabling the port so that debugging messages can print
2531 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2532 stp_port_enable(sp);
2534 stp_port_set_aux(sp, ofport);
2535 stp_port_set_priority(sp, s->priority);
2536 stp_port_set_path_cost(sp, s->path_cost);
2538 update_stp_port_state(ofport);
2544 get_stp_port_status(struct ofport *ofport_,
2545 struct ofproto_port_stp_status *s)
2547 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2548 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2549 struct stp_port *sp = ofport->stp_port;
2551 if (!ofproto->stp || !sp) {
2557 s->port_id = stp_port_get_id(sp);
2558 s->state = stp_port_get_state(sp);
2559 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2560 s->role = stp_port_get_role(sp);
2566 get_stp_port_stats(struct ofport *ofport_,
2567 struct ofproto_port_stp_stats *s)
2569 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2570 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2571 struct stp_port *sp = ofport->stp_port;
2573 if (!ofproto->stp || !sp) {
2579 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2585 stp_run(struct ofproto_dpif *ofproto)
2588 long long int now = time_msec();
2589 long long int elapsed = now - ofproto->stp_last_tick;
2590 struct stp_port *sp;
2593 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2594 ofproto->stp_last_tick = now;
2596 while (stp_get_changed_port(ofproto->stp, &sp)) {
2597 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2600 update_stp_port_state(ofport);
2604 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2605 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2606 mac_learning_flush(ofproto->ml);
2607 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2608 mcast_snooping_mdb_flush(ofproto->ms);
2614 stp_wait(struct ofproto_dpif *ofproto)
2617 poll_timer_wait(1000);
2621 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2622 * caller is responsible for assigning RSTP port numbers and ensuring
2623 * there are no duplicates. */
2625 set_rstp_port(struct ofport *ofport_,
2626 const struct ofproto_port_rstp_settings *s)
2628 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2629 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2630 struct rstp_port *rp = ofport->rstp_port;
2632 if (!s || !s->enable) {
2634 rstp_port_set_aux(rp, NULL);
2635 rstp_port_set_state(rp, RSTP_DISABLED);
2636 rstp_port_set_mac_operational(rp, false);
2637 ofport->rstp_port = NULL;
2638 rstp_port_unref(rp);
2639 update_rstp_port_state(ofport);
2644 /* Check if need to add a new port. */
2646 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2649 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
2650 s->admin_edge_port, s->auto_edge,
2651 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2653 update_rstp_port_state(ofport);
2654 /* Synchronize operational status. */
2655 rstp_port_set_mac_operational(rp, ofport->may_enable);
2659 get_rstp_port_status(struct ofport *ofport_,
2660 struct ofproto_port_rstp_status *s)
2662 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2663 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2664 struct rstp_port *rp = ofport->rstp_port;
2666 if (!ofproto->rstp || !rp) {
2672 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2673 &s->designated_bridge_id, &s->designated_port_id,
2674 &s->designated_path_cost, &s->tx_count,
2675 &s->rx_count, &s->error_count, &s->uptime);
2680 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2683 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2684 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2686 if (ofport->n_qdscp != n_qdscp
2687 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2688 n_qdscp * sizeof *qdscp))) {
2689 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2690 free(ofport->qdscp);
2691 ofport->qdscp = n_qdscp
2692 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2694 ofport->n_qdscp = n_qdscp;
2702 /* Expires all MAC learning entries associated with 'bundle' and forces its
2703 * ofproto to revalidate every flow.
2705 * Normally MAC learning entries are removed only from the ofproto associated
2706 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2707 * are removed from every ofproto. When patch ports and SLB bonds are in use
2708 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2709 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2710 * with the host from which it migrated. */
2712 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2714 struct ofproto_dpif *ofproto = bundle->ofproto;
2715 struct mac_learning *ml = ofproto->ml;
2716 struct mac_entry *mac, *next_mac;
2718 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2719 ovs_rwlock_wrlock(&ml->rwlock);
2720 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2721 if (mac_entry_get_port(ml, mac) == bundle) {
2723 struct ofproto_dpif *o;
2725 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2727 struct mac_entry *e;
2729 ovs_rwlock_wrlock(&o->ml->rwlock);
2730 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2732 mac_learning_expire(o->ml, e);
2734 ovs_rwlock_unlock(&o->ml->rwlock);
2739 mac_learning_expire(ml, mac);
2742 ovs_rwlock_unlock(&ml->rwlock);
2746 bundle_move(struct ofbundle *old, struct ofbundle *new)
2748 struct ofproto_dpif *ofproto = old->ofproto;
2749 struct mac_learning *ml = ofproto->ml;
2750 struct mac_entry *mac, *next_mac;
2752 ovs_assert(new->ofproto == old->ofproto);
2754 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2755 ovs_rwlock_wrlock(&ml->rwlock);
2756 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2757 if (mac_entry_get_port(ml, mac) == old) {
2758 mac_entry_set_port(ml, mac, new);
2761 ovs_rwlock_unlock(&ml->rwlock);
2764 static struct ofbundle *
2765 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2767 struct ofbundle *bundle;
2769 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2770 &ofproto->bundles) {
2771 if (bundle->aux == aux) {
2779 bundle_update(struct ofbundle *bundle)
2781 struct ofport_dpif *port;
2783 bundle->floodable = true;
2784 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2785 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2787 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2788 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2789 bundle->floodable = false;
2796 bundle_del_port(struct ofport_dpif *port)
2798 struct ofbundle *bundle = port->bundle;
2800 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2802 ovs_list_remove(&port->bundle_node);
2803 port->bundle = NULL;
2806 lacp_slave_unregister(bundle->lacp, port);
2809 bond_slave_unregister(bundle->bond, port);
2812 bundle_update(bundle);
2816 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2817 struct lacp_slave_settings *lacp)
2819 struct ofport_dpif *port;
2821 port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
2826 if (port->bundle != bundle) {
2827 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2829 bundle_remove(&port->up);
2832 port->bundle = bundle;
2833 ovs_list_push_back(&bundle->ports, &port->bundle_node);
2834 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2836 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2837 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2838 bundle->floodable = false;
2842 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2843 lacp_slave_register(bundle->lacp, port, lacp);
2850 bundle_destroy(struct ofbundle *bundle)
2852 struct ofproto_dpif *ofproto;
2853 struct ofport_dpif *port, *next_port;
2859 ofproto = bundle->ofproto;
2860 mbridge_unregister_bundle(ofproto->mbridge, bundle);
2863 xlate_bundle_remove(bundle);
2866 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2867 bundle_del_port(port);
2870 bundle_flush_macs(bundle, true);
2871 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2873 free(bundle->trunks);
2874 lacp_unref(bundle->lacp);
2875 bond_unref(bundle->bond);
2880 bundle_set(struct ofproto *ofproto_, void *aux,
2881 const struct ofproto_bundle_settings *s)
2883 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2884 bool need_flush = false;
2885 struct ofport_dpif *port;
2886 struct ofbundle *bundle;
2887 unsigned long *trunks;
2893 bundle_destroy(bundle_lookup(ofproto, aux));
2897 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2898 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2900 bundle = bundle_lookup(ofproto, aux);
2902 bundle = xmalloc(sizeof *bundle);
2904 bundle->ofproto = ofproto;
2905 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2906 hash_pointer(aux, 0));
2908 bundle->name = NULL;
2910 ovs_list_init(&bundle->ports);
2911 bundle->vlan_mode = PORT_VLAN_TRUNK;
2913 bundle->trunks = NULL;
2914 bundle->use_priority_tags = s->use_priority_tags;
2915 bundle->lacp = NULL;
2916 bundle->bond = NULL;
2918 bundle->floodable = true;
2919 mbridge_register_bundle(ofproto->mbridge, bundle);
2922 if (!bundle->name || strcmp(s->name, bundle->name)) {
2924 bundle->name = xstrdup(s->name);
2929 ofproto->lacp_enabled = true;
2930 if (!bundle->lacp) {
2931 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2932 bundle->lacp = lacp_create();
2934 lacp_configure(bundle->lacp, s->lacp);
2936 lacp_unref(bundle->lacp);
2937 bundle->lacp = NULL;
2940 /* Update set of ports. */
2942 for (i = 0; i < s->n_slaves; i++) {
2943 if (!bundle_add_port(bundle, s->slaves[i],
2944 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2948 if (!ok || ovs_list_size(&bundle->ports) != s->n_slaves) {
2949 struct ofport_dpif *next_port;
2951 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2952 for (i = 0; i < s->n_slaves; i++) {
2953 if (s->slaves[i] == port->up.ofp_port) {
2958 bundle_del_port(port);
2962 ovs_assert(ovs_list_size(&bundle->ports) <= s->n_slaves);
2964 if (ovs_list_is_empty(&bundle->ports)) {
2965 bundle_destroy(bundle);
2969 /* Set VLAN tagging mode */
2970 if (s->vlan_mode != bundle->vlan_mode
2971 || s->use_priority_tags != bundle->use_priority_tags) {
2972 bundle->vlan_mode = s->vlan_mode;
2973 bundle->use_priority_tags = s->use_priority_tags;
2978 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2979 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2981 if (vlan != bundle->vlan) {
2982 bundle->vlan = vlan;
2986 /* Get trunked VLANs. */
2987 switch (s->vlan_mode) {
2988 case PORT_VLAN_ACCESS:
2992 case PORT_VLAN_TRUNK:
2993 trunks = CONST_CAST(unsigned long *, s->trunks);
2996 case PORT_VLAN_NATIVE_UNTAGGED:
2997 case PORT_VLAN_NATIVE_TAGGED:
2998 if (vlan != 0 && (!s->trunks
2999 || !bitmap_is_set(s->trunks, vlan)
3000 || bitmap_is_set(s->trunks, 0))) {
3001 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
3003 trunks = bitmap_clone(s->trunks, 4096);
3005 trunks = bitmap_allocate1(4096);
3007 bitmap_set1(trunks, vlan);
3008 bitmap_set0(trunks, 0);
3010 trunks = CONST_CAST(unsigned long *, s->trunks);
3017 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
3018 free(bundle->trunks);
3019 if (trunks == s->trunks) {
3020 bundle->trunks = vlan_bitmap_clone(trunks);
3022 bundle->trunks = trunks;
3027 if (trunks != s->trunks) {
3032 if (!ovs_list_is_short(&bundle->ports)) {
3033 bundle->ofproto->has_bonded_bundles = true;
3035 if (bond_reconfigure(bundle->bond, s->bond)) {
3036 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3039 bundle->bond = bond_create(s->bond, ofproto);
3040 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3043 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3044 bond_slave_register(bundle->bond, port,
3045 port->up.ofp_port, port->up.netdev);
3048 bond_unref(bundle->bond);
3049 bundle->bond = NULL;
3052 /* If we changed something that would affect MAC learning, un-learn
3053 * everything on this port and force flow revalidation. */
3055 bundle_flush_macs(bundle, false);
3062 bundle_remove(struct ofport *port_)
3064 struct ofport_dpif *port = ofport_dpif_cast(port_);
3065 struct ofbundle *bundle = port->bundle;
3068 bundle_del_port(port);
3069 if (ovs_list_is_empty(&bundle->ports)) {
3070 bundle_destroy(bundle);
3071 } else if (ovs_list_is_short(&bundle->ports)) {
3072 bond_unref(bundle->bond);
3073 bundle->bond = NULL;
3079 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
3081 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
3082 struct ofport_dpif *port = port_;
3086 error = netdev_get_etheraddr(port->up.netdev, &ea);
3088 struct dp_packet packet;
3091 dp_packet_init(&packet, 0);
3092 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
3094 memcpy(packet_pdu, pdu, pdu_size);
3096 ofproto_dpif_send_packet(port, false, &packet);
3097 dp_packet_uninit(&packet);
3099 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
3100 "%s (%s)", port->bundle->name,
3101 netdev_get_name(port->up.netdev), ovs_strerror(error));
3106 bundle_send_learning_packets(struct ofbundle *bundle)
3108 struct ofproto_dpif *ofproto = bundle->ofproto;
3109 int error, n_packets, n_errors;
3110 struct mac_entry *e;
3112 struct ovs_list list_node;
3113 struct ofport_dpif *port;
3114 struct dp_packet *pkt;
3116 struct ovs_list packets;
3118 ovs_list_init(&packets);
3119 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3120 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3121 if (mac_entry_get_port(ofproto->ml, e) != bundle) {
3122 pkt_node = xmalloc(sizeof *pkt_node);
3123 pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
3125 (void **)&pkt_node->port);
3126 ovs_list_push_back(&packets, &pkt_node->list_node);
3129 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3131 error = n_packets = n_errors = 0;
3132 LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
3135 ret = ofproto_dpif_send_packet(pkt_node->port, false, pkt_node->pkt);
3136 dp_packet_delete(pkt_node->pkt);
3146 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3147 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3148 "packets, last error was: %s",
3149 bundle->name, n_errors, n_packets, ovs_strerror(error));
3151 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3152 bundle->name, n_packets);
3157 bundle_run(struct ofbundle *bundle)
3160 lacp_run(bundle->lacp, send_pdu_cb);
3163 struct ofport_dpif *port;
3165 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3166 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
3169 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3170 bundle->ofproto->backer->need_revalidate = REV_BOND;
3173 if (bond_should_send_learning_packets(bundle->bond)) {
3174 bundle_send_learning_packets(bundle);
3180 bundle_wait(struct ofbundle *bundle)
3183 lacp_wait(bundle->lacp);
3186 bond_wait(bundle->bond);
3193 mirror_set__(struct ofproto *ofproto_, void *aux,
3194 const struct ofproto_mirror_settings *s)
3196 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3197 struct ofbundle **srcs, **dsts;
3202 mirror_destroy(ofproto->mbridge, aux);
3206 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3207 dsts = xmalloc(s->n_dsts * sizeof *dsts);
3209 for (i = 0; i < s->n_srcs; i++) {
3210 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
3213 for (i = 0; i < s->n_dsts; i++) {
3214 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
3217 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3218 s->n_dsts, s->src_vlans,
3219 bundle_lookup(ofproto, s->out_bundle),
3220 s->snaplen, s->out_vlan);
3227 mirror_get_stats__(struct ofproto *ofproto, void *aux,
3228 uint64_t *packets, uint64_t *bytes)
3230 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3235 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3237 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3238 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3239 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
3240 mac_learning_flush(ofproto->ml);
3242 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3247 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
3249 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3250 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3251 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
3255 forward_bpdu_changed(struct ofproto *ofproto_)
3257 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3258 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3262 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3265 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3266 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3267 mac_learning_set_idle_time(ofproto->ml, idle_time);
3268 mac_learning_set_max_entries(ofproto->ml, max_entries);
3269 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3272 /* Configures multicast snooping on 'ofport' using the settings
3273 * defined in 's'. */
3275 set_mcast_snooping(struct ofproto *ofproto_,
3276 const struct ofproto_mcast_snooping_settings *s)
3278 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3280 /* Only revalidate flows if the configuration changed. */
3281 if (!s != !ofproto->ms) {
3282 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3287 ofproto->ms = mcast_snooping_create();
3290 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3291 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3292 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3293 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3294 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3296 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3298 mcast_snooping_unref(ofproto->ms);
3305 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3307 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3308 const struct ofproto_mcast_snooping_port_settings *s)
3310 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3311 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3313 if (ofproto->ms && s) {
3314 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3315 mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3316 mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3318 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3326 struct ofport_dpif *
3327 ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
3329 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3330 return ofport ? ofport_dpif_cast(ofport) : NULL;
3334 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3335 struct ofproto_port *ofproto_port,
3336 struct dpif_port *dpif_port)
3338 ofproto_port->name = dpif_port->name;
3339 ofproto_port->type = dpif_port->type;
3340 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3344 ofport_update_peer(struct ofport_dpif *ofport)
3346 const struct ofproto_dpif *ofproto;
3347 struct dpif_backer *backer;
3350 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3354 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
3355 backer->need_revalidate = REV_RECONFIGURE;
3358 ofport->peer->peer = NULL;
3359 ofport->peer = NULL;
3362 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3367 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3368 struct ofport *peer_ofport;
3369 struct ofport_dpif *peer;
3372 if (ofproto->backer != backer) {
3376 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3381 peer = ofport_dpif_cast(peer_ofport);
3382 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3383 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3385 ofport->peer = peer;
3386 ofport->peer->peer = ofport;
3396 port_run(struct ofport_dpif *ofport)
3398 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3399 bool carrier_changed = carrier_seq != ofport->carrier_seq;
3400 bool enable = netdev_get_carrier(ofport->up.netdev);
3401 bool cfm_enable = false;
3402 bool bfd_enable = false;
3404 ofport->carrier_seq = carrier_seq;
3407 int cfm_opup = cfm_get_opup(ofport->cfm);
3409 cfm_enable = !cfm_get_fault(ofport->cfm);
3411 if (cfm_opup >= 0) {
3412 cfm_enable = cfm_enable && cfm_opup;
3417 bfd_enable = bfd_forwarding(ofport->bfd);
3420 if (ofport->bfd || ofport->cfm) {
3421 enable = enable && (cfm_enable || bfd_enable);
3424 if (ofport->bundle) {
3425 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3426 if (carrier_changed) {
3427 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3431 if (ofport->may_enable != enable) {
3432 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3434 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3436 if (ofport->rstp_port) {
3437 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3441 ofport->may_enable = enable;
3445 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3446 struct ofproto_port *ofproto_port)
3448 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3449 struct dpif_port dpif_port;
3452 if (sset_contains(&ofproto->ghost_ports, devname)) {
3453 const char *type = netdev_get_type_from_name(devname);
3455 /* We may be called before ofproto->up.port_by_name is populated with
3456 * the appropriate ofport. For this reason, we must get the name and
3457 * type from the netdev layer directly. */
3459 const struct ofport *ofport;
3461 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3462 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3463 ofproto_port->name = xstrdup(devname);
3464 ofproto_port->type = xstrdup(type);
3470 if (!sset_contains(&ofproto->ports, devname)) {
3473 error = dpif_port_query_by_name(ofproto->backer->dpif,
3474 devname, &dpif_port);
3476 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3482 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3484 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3485 const char *devname = netdev_get_name(netdev);
3486 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3487 const char *dp_port_name;
3489 if (netdev_vport_is_patch(netdev)) {
3490 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3494 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3495 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3496 odp_port_t port_no = ODPP_NONE;
3499 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3503 if (netdev_get_tunnel_config(netdev)) {
3504 simap_put(&ofproto->backer->tnl_backers,
3505 dp_port_name, odp_to_u32(port_no));
3509 if (netdev_get_tunnel_config(netdev)) {
3510 sset_add(&ofproto->ghost_ports, devname);
3512 sset_add(&ofproto->ports, devname);
3518 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3520 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3521 struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
3528 sset_find_and_delete(&ofproto->ghost_ports,
3529 netdev_get_name(ofport->up.netdev));
3530 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3531 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3532 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3534 /* The caller is going to close ofport->up.netdev. If this is a
3535 * bonded port, then the bond is using that netdev, so remove it
3536 * from the bond. The client will need to reconfigure everything
3537 * after deleting ports, so then the slave will get re-added. */
3538 bundle_remove(&ofport->up);
3545 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3547 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3550 error = netdev_get_stats(ofport->up.netdev, stats);
3552 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3553 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3555 ovs_mutex_lock(&ofproto->stats_mutex);
3556 /* ofproto->stats.tx_packets represents packets that we created
3557 * internally and sent to some port (e.g. packets sent with
3558 * ofproto_dpif_send_packet()). Account for them as if they had
3559 * come from OFPP_LOCAL and got forwarded. */
3561 if (stats->rx_packets != UINT64_MAX) {
3562 stats->rx_packets += ofproto->stats.tx_packets;
3565 if (stats->rx_bytes != UINT64_MAX) {
3566 stats->rx_bytes += ofproto->stats.tx_bytes;
3569 /* ofproto->stats.rx_packets represents packets that were received on
3570 * some port and we processed internally and dropped (e.g. STP).
3571 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3573 if (stats->tx_packets != UINT64_MAX) {
3574 stats->tx_packets += ofproto->stats.rx_packets;
3577 if (stats->tx_bytes != UINT64_MAX) {
3578 stats->tx_bytes += ofproto->stats.rx_bytes;
3580 ovs_mutex_unlock(&ofproto->stats_mutex);
3587 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3589 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3590 if (ofport->bundle && ofport->bundle->lacp) {
3591 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3598 struct port_dump_state {
3599 struct sset_position pos;
3602 struct ofproto_port port;
3607 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3609 *statep = xzalloc(sizeof(struct port_dump_state));
3614 port_dump_next(const struct ofproto *ofproto_, void *state_,
3615 struct ofproto_port *port)
3617 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3618 struct port_dump_state *state = state_;
3619 const struct sset *sset;
3620 struct sset_node *node;
3622 if (state->has_port) {
3623 ofproto_port_destroy(&state->port);
3624 state->has_port = false;
3626 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3627 while ((node = sset_at_position(sset, &state->pos))) {
3630 error = port_query_by_name(ofproto_, node->name, &state->port);
3632 *port = state->port;
3633 state->has_port = true;
3635 } else if (error != ENODEV) {
3640 if (!state->ghost) {
3641 state->ghost = true;
3642 memset(&state->pos, 0, sizeof state->pos);
3643 return port_dump_next(ofproto_, state_, port);
3650 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3652 struct port_dump_state *state = state_;
3654 if (state->has_port) {
3655 ofproto_port_destroy(&state->port);
3662 port_poll(const struct ofproto *ofproto_, char **devnamep)
3664 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3666 if (ofproto->port_poll_errno) {
3667 int error = ofproto->port_poll_errno;
3668 ofproto->port_poll_errno = 0;
3672 if (sset_is_empty(&ofproto->port_poll_set)) {
3676 *devnamep = sset_pop(&ofproto->port_poll_set);
3681 port_poll_wait(const struct ofproto *ofproto_)
3683 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3684 dpif_port_poll_wait(ofproto->backer->dpif);
3688 port_is_lacp_current(const struct ofport *ofport_)
3690 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3691 return (ofport->bundle && ofport->bundle->lacp
3692 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3696 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3697 * then delete it entirely. */
3699 rule_expire(struct rule_dpif *rule, long long now)
3700 OVS_REQUIRES(ofproto_mutex)
3702 uint16_t hard_timeout, idle_timeout;
3705 hard_timeout = rule->up.hard_timeout;
3706 idle_timeout = rule->up.idle_timeout;
3708 /* Has 'rule' expired? */
3710 long long int modified;
3712 ovs_mutex_lock(&rule->up.mutex);
3713 modified = rule->up.modified;
3714 ovs_mutex_unlock(&rule->up.mutex);
3716 if (now > modified + hard_timeout * 1000) {
3717 reason = OFPRR_HARD_TIMEOUT;
3721 if (reason < 0 && idle_timeout) {
3724 ovs_mutex_lock(&rule->stats_mutex);
3725 used = rule->stats.used;
3726 ovs_mutex_unlock(&rule->stats_mutex);
3728 if (now > used + idle_timeout * 1000) {
3729 reason = OFPRR_IDLE_TIMEOUT;
3734 COVERAGE_INC(ofproto_dpif_expired);
3735 ofproto_rule_expire(&rule->up, reason);
3740 ofproto_dpif_set_packet_odp_port(const struct ofproto_dpif *ofproto,
3741 ofp_port_t in_port, struct dp_packet *packet)
3743 if (in_port == OFPP_NONE) {
3744 in_port = OFPP_LOCAL;
3746 packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3750 ofproto_dpif_execute_actions__(struct ofproto_dpif *ofproto,
3751 const struct flow *flow,
3752 struct rule_dpif *rule,
3753 const struct ofpact *ofpacts, size_t ofpacts_len,
3754 int indentation, int depth, int resubmits,
3755 struct dp_packet *packet)
3757 struct dpif_flow_stats stats;
3758 struct xlate_out xout;
3759 struct xlate_in xin;
3760 struct dpif_execute execute;
3763 ovs_assert((rule != NULL) != (ofpacts != NULL));
3765 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3768 rule_dpif_credit_stats(rule, &stats);
3771 uint64_t odp_actions_stub[1024 / 8];
3772 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
3773 xlate_in_init(&xin, ofproto, flow, flow->in_port.ofp_port, rule,
3774 stats.tcp_flags, packet, NULL, &odp_actions);
3775 xin.ofpacts = ofpacts;
3776 xin.ofpacts_len = ofpacts_len;
3777 xin.resubmit_stats = &stats;
3778 xin.indentation = indentation;
3780 xin.resubmits = resubmits;
3781 if (xlate_actions(&xin, &xout) != XLATE_OK) {
3786 execute.actions = odp_actions.data;
3787 execute.actions_len = odp_actions.size;
3789 pkt_metadata_from_flow(&packet->md, flow);
3790 execute.packet = packet;
3791 execute.flow = flow;
3792 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3793 execute.probe = false;
3796 /* Fix up in_port. */
3797 ofproto_dpif_set_packet_odp_port(ofproto, flow->in_port.ofp_port, packet);
3799 error = dpif_execute(ofproto->backer->dpif, &execute);
3801 xlate_out_uninit(&xout);
3802 ofpbuf_uninit(&odp_actions);
3807 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3808 * 'flow' must reflect the data in 'packet'. */
3810 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3811 const struct flow *flow,
3812 struct rule_dpif *rule,
3813 const struct ofpact *ofpacts, size_t ofpacts_len,
3814 struct dp_packet *packet)
3816 return ofproto_dpif_execute_actions__(ofproto, flow, rule, ofpacts,
3817 ofpacts_len, 0, 0, 0, packet);
3821 rule_dpif_credit_stats(struct rule_dpif *rule,
3822 const struct dpif_flow_stats *stats)
3824 ovs_mutex_lock(&rule->stats_mutex);
3825 if (OVS_UNLIKELY(rule->new_rule)) {
3826 rule_dpif_credit_stats(rule->new_rule, stats);
3828 rule->stats.n_packets += stats->n_packets;
3829 rule->stats.n_bytes += stats->n_bytes;
3830 rule->stats.used = MAX(rule->stats.used, stats->used);
3832 ovs_mutex_unlock(&rule->stats_mutex);
3836 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3837 OVS_REQUIRES(rule->up.mutex)
3839 return rule->up.flow_cookie;
3843 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3844 uint16_t hard_timeout)
3846 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3849 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3850 * be read until the calling thread quiesces. */
3851 const struct rule_actions *
3852 rule_dpif_get_actions(const struct rule_dpif *rule)
3854 return rule_get_actions(&rule->up);
3857 /* Sets 'rule''s recirculation id. */
3859 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3860 OVS_REQUIRES(rule->up.mutex)
3862 ovs_assert(!rule->recirc_id || rule->recirc_id == id);
3863 if (rule->recirc_id == id) {
3864 /* Release the new reference to the same id. */
3867 rule->recirc_id = id;
3871 /* Sets 'rule''s recirculation id. */
3873 rule_set_recirc_id(struct rule *rule_, uint32_t id)
3875 struct rule_dpif *rule = rule_dpif_cast(rule_);
3877 ovs_mutex_lock(&rule->up.mutex);
3878 rule_dpif_set_recirc_id(rule, id);
3879 ovs_mutex_unlock(&rule->up.mutex);
3883 ofproto_dpif_get_tables_version(struct ofproto_dpif *ofproto OVS_UNUSED)
3885 cls_version_t version;
3887 atomic_read_relaxed(&ofproto->tables_version, &version);
3892 /* The returned rule (if any) is valid at least until the next RCU quiescent
3893 * period. If the rule needs to stay around longer, the caller should take
3896 * 'flow' is non-const to allow for temporary modifications during the lookup.
3897 * Any changes are restored before returning. */
3898 static struct rule_dpif *
3899 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, cls_version_t version,
3900 uint8_t table_id, struct flow *flow,
3901 struct flow_wildcards *wc)
3903 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3904 return rule_dpif_cast(rule_from_cls_rule(classifier_lookup(cls, version,
3908 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
3909 * table '*table_id'. Returns the rule that was found, which may be one of the
3910 * special rules according to packet miss hadling. If 'may_packet_in' is
3911 * false, returning of the miss_rule (which issues packet ins for the
3912 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
3913 * that were used during the lookup.
3915 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3916 * if none is found then the table miss configuration for that table is
3917 * honored, which can result in additional lookups in other OpenFlow tables.
3918 * In this case the function updates '*table_id' to reflect the final OpenFlow
3919 * table that was searched.
3921 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3924 * The rule is returned in '*rule', which is valid at least until the next
3925 * RCU quiescent period. If the '*rule' needs to stay around longer, the
3926 * caller must take a reference.
3928 * 'in_port' allows the lookup to take place as if the in port had the value
3929 * 'in_port'. This is needed for resubmit action support.
3931 * 'flow' is non-const to allow for temporary modifications during the lookup.
3932 * Any changes are restored before returning. */
3934 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3935 cls_version_t version, struct flow *flow,
3936 struct flow_wildcards *wc,
3937 const struct dpif_flow_stats *stats,
3938 uint8_t *table_id, ofp_port_t in_port,
3939 bool may_packet_in, bool honor_table_miss)
3941 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
3942 ofp_port_t old_in_port = flow->in_port.ofp_port;
3943 enum ofputil_table_miss miss_config;
3944 struct rule_dpif *rule;
3947 /* We always unwildcard nw_frag (for IP), so they
3948 * need not be unwildcarded here. */
3949 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3950 && ofproto->up.frag_handling != OFPUTIL_FRAG_NX_MATCH) {
3951 if (ofproto->up.frag_handling == OFPUTIL_FRAG_NORMAL) {
3952 /* We must pretend that transport ports are unavailable. */
3953 flow->tp_src = htons(0);
3954 flow->tp_dst = htons(0);
3956 /* Must be OFPUTIL_FRAG_DROP (we don't have OFPUTIL_FRAG_REASM).
3957 * Use the drop_frags_rule (which cannot disappear). */
3958 rule = ofproto->drop_frags_rule;
3960 struct oftable *tbl = &ofproto->up.tables[*table_id];
3963 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
3969 /* Look up a flow with 'in_port' as the input port. Then restore the
3970 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3971 * have surprising behavior). */
3972 flow->in_port.ofp_port = in_port;
3974 /* Our current implementation depends on n_tables == N_TABLES, and
3975 * TBL_INTERNAL being the last table. */
3976 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
3978 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
3980 for (next_id = *table_id;
3981 next_id < ofproto->up.n_tables;
3982 next_id++, next_id += (next_id == TBL_INTERNAL))
3984 *table_id = next_id;
3985 rule = rule_dpif_lookup_in_table(ofproto, version, next_id, flow, wc);
3987 struct oftable *tbl = &ofproto->up.tables[next_id];
3990 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
3991 stats->n_packets, &orig);
3994 goto out; /* Match. */
3996 if (honor_table_miss) {
3997 miss_config = ofproto_table_get_miss_config(&ofproto->up,
3999 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
4006 rule = ofproto->no_packet_in_rule;
4007 if (may_packet_in) {
4008 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
4009 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
4010 struct ofport_dpif *port;
4012 port = ofp_port_to_ofport(ofproto, old_in_port);
4014 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
4016 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
4017 rule = ofproto->miss_rule;
4019 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
4020 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
4021 rule = ofproto->miss_rule;
4025 /* Restore port numbers, as they may have been modified above. */
4026 flow->tp_src = old_tp_src;
4027 flow->tp_dst = old_tp_dst;
4028 /* Restore the old in port. */
4029 flow->in_port.ofp_port = old_in_port;
4034 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
4036 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
4039 static struct rule *
4042 struct rule_dpif *rule = xzalloc(sizeof *rule);
4047 rule_dealloc(struct rule *rule_)
4049 struct rule_dpif *rule = rule_dpif_cast(rule_);
4054 check_mask(struct ofproto_dpif *ofproto, const struct miniflow *flow)
4056 const struct odp_support *support;
4057 uint16_t ct_state, ct_zone;
4061 support = &ofproto_dpif_get_support(ofproto)->odp;
4062 ct_state = MINIFLOW_GET_U16(flow, ct_state);
4063 if (support->ct_state && support->ct_zone && support->ct_mark
4064 && support->ct_label && support->ct_state_nat) {
4065 return ct_state & CS_UNSUPPORTED_MASK ? OFPERR_OFPBMC_BAD_MASK : 0;
4068 ct_zone = MINIFLOW_GET_U16(flow, ct_zone);
4069 ct_mark = MINIFLOW_GET_U32(flow, ct_mark);
4070 ct_label = MINIFLOW_GET_U128(flow, ct_label);
4072 if ((ct_state && !support->ct_state)
4073 || (ct_state & CS_UNSUPPORTED_MASK)
4074 || ((ct_state & (CS_SRC_NAT | CS_DST_NAT)) && !support->ct_state_nat)
4075 || (ct_zone && !support->ct_zone)
4076 || (ct_mark && !support->ct_mark)
4077 || (!ovs_u128_is_zero(ct_label) && !support->ct_label)) {
4078 return OFPERR_OFPBMC_BAD_MASK;
4085 check_actions(const struct ofproto_dpif *ofproto,
4086 const struct rule_actions *const actions)
4088 const struct ofpact *ofpact;
4090 OFPACT_FOR_EACH (ofpact, actions->ofpacts, actions->ofpacts_len) {
4091 const struct odp_support *support;
4092 const struct ofpact_conntrack *ct;
4093 const struct ofpact *a;
4095 if (ofpact->type != OFPACT_CT) {
4099 ct = CONTAINER_OF(ofpact, struct ofpact_conntrack, ofpact);
4100 support = &ofproto_dpif_get_support(ofproto)->odp;
4102 if (!support->ct_state) {
4103 return OFPERR_OFPBAC_BAD_TYPE;
4105 if ((ct->zone_imm || ct->zone_src.field) && !support->ct_zone) {
4106 return OFPERR_OFPBAC_BAD_ARGUMENT;
4109 OFPACT_FOR_EACH(a, ct->actions, ofpact_ct_get_action_len(ct)) {
4110 const struct mf_field *dst = ofpact_get_mf_dst(a);
4112 if (a->type == OFPACT_NAT && !support->ct_state_nat) {
4113 /* The backer doesn't seem to support the NAT bits in
4114 * 'ct_state': assume that it doesn't support the NAT
4116 return OFPERR_OFPBAC_BAD_TYPE;
4118 if (dst && ((dst->id == MFF_CT_MARK && !support->ct_mark)
4119 || (dst->id == MFF_CT_LABEL && !support->ct_label))) {
4120 return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
4129 rule_check(struct rule *rule)
4131 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->ofproto);
4134 err = check_mask(ofproto, &rule->cr.match.mask->masks);
4138 return check_actions(ofproto, rule->actions);
4142 rule_construct(struct rule *rule_)
4143 OVS_NO_THREAD_SAFETY_ANALYSIS
4145 struct rule_dpif *rule = rule_dpif_cast(rule_);
4148 error = rule_check(rule_);
4153 ovs_mutex_init_adaptive(&rule->stats_mutex);
4154 rule->stats.n_packets = 0;
4155 rule->stats.n_bytes = 0;
4156 rule->stats.used = rule->up.modified;
4157 rule->recirc_id = 0;
4158 rule->new_rule = NULL;
4164 rule_insert(struct rule *rule_, struct rule *old_rule_, bool forward_stats)
4165 OVS_REQUIRES(ofproto_mutex)
4167 struct rule_dpif *rule = rule_dpif_cast(rule_);
4169 if (old_rule_ && forward_stats) {
4170 struct rule_dpif *old_rule = rule_dpif_cast(old_rule_);
4172 ovs_assert(!old_rule->new_rule);
4174 /* Take a reference to the new rule, and refer all stats updates from
4175 * the old rule to the new rule. */
4176 rule_dpif_ref(rule);
4178 ovs_mutex_lock(&old_rule->stats_mutex);
4179 ovs_mutex_lock(&rule->stats_mutex);
4180 old_rule->new_rule = rule; /* Forward future stats. */
4181 rule->stats = old_rule->stats; /* Transfer stats to the new rule. */
4182 ovs_mutex_unlock(&rule->stats_mutex);
4183 ovs_mutex_unlock(&old_rule->stats_mutex);
4188 rule_destruct(struct rule *rule_)
4189 OVS_NO_THREAD_SAFETY_ANALYSIS
4191 struct rule_dpif *rule = rule_dpif_cast(rule_);
4193 ovs_mutex_destroy(&rule->stats_mutex);
4194 /* Release reference to the new rule, if any. */
4195 if (rule->new_rule) {
4196 rule_dpif_unref(rule->new_rule);
4198 if (rule->recirc_id) {
4199 recirc_free_id(rule->recirc_id);
4204 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4205 long long int *used)
4207 struct rule_dpif *rule = rule_dpif_cast(rule_);
4209 ovs_mutex_lock(&rule->stats_mutex);
4210 if (OVS_UNLIKELY(rule->new_rule)) {
4211 rule_get_stats(&rule->new_rule->up, packets, bytes, used);
4213 *packets = rule->stats.n_packets;
4214 *bytes = rule->stats.n_bytes;
4215 *used = rule->stats.used;
4217 ovs_mutex_unlock(&rule->stats_mutex);
4221 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
4222 struct dp_packet *packet)
4224 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4226 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
4230 rule_execute(struct rule *rule, const struct flow *flow,
4231 struct dp_packet *packet)
4233 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
4234 dp_packet_delete(packet);
4238 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4240 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4243 static struct ofgroup *
4246 struct group_dpif *group = xzalloc(sizeof *group);
4251 group_dealloc(struct ofgroup *group_)
4253 struct group_dpif *group = group_dpif_cast(group_);
4258 group_construct_stats(struct group_dpif *group)
4259 OVS_REQUIRES(group->stats_mutex)
4261 struct ofputil_bucket *bucket;
4262 const struct ovs_list *buckets;
4264 group->packet_count = 0;
4265 group->byte_count = 0;
4267 group_dpif_get_buckets(group, &buckets);
4268 LIST_FOR_EACH (bucket, list_node, buckets) {
4269 bucket->stats.packet_count = 0;
4270 bucket->stats.byte_count = 0;
4275 group_dpif_credit_stats(struct group_dpif *group,
4276 struct ofputil_bucket *bucket,
4277 const struct dpif_flow_stats *stats)
4279 ovs_mutex_lock(&group->stats_mutex);
4280 group->packet_count += stats->n_packets;
4281 group->byte_count += stats->n_bytes;
4283 bucket->stats.packet_count += stats->n_packets;
4284 bucket->stats.byte_count += stats->n_bytes;
4285 } else { /* Credit to all buckets */
4286 const struct ovs_list *buckets;
4288 group_dpif_get_buckets(group, &buckets);
4289 LIST_FOR_EACH (bucket, list_node, buckets) {
4290 bucket->stats.packet_count += stats->n_packets;
4291 bucket->stats.byte_count += stats->n_bytes;
4294 ovs_mutex_unlock(&group->stats_mutex);
4298 group_construct(struct ofgroup *group_)
4300 struct group_dpif *group = group_dpif_cast(group_);
4302 ovs_mutex_init_adaptive(&group->stats_mutex);
4303 ovs_mutex_lock(&group->stats_mutex);
4304 group_construct_stats(group);
4305 ovs_mutex_unlock(&group->stats_mutex);
4310 group_destruct(struct ofgroup *group_)
4312 struct group_dpif *group = group_dpif_cast(group_);
4313 ovs_mutex_destroy(&group->stats_mutex);
4317 group_modify(struct ofgroup *group_)
4319 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
4321 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4327 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4329 struct group_dpif *group = group_dpif_cast(group_);
4330 struct ofputil_bucket *bucket;
4331 const struct ovs_list *buckets;
4332 struct bucket_counter *bucket_stats;
4334 ovs_mutex_lock(&group->stats_mutex);
4335 ogs->packet_count = group->packet_count;
4336 ogs->byte_count = group->byte_count;
4338 group_dpif_get_buckets(group, &buckets);
4339 bucket_stats = ogs->bucket_stats;
4340 LIST_FOR_EACH (bucket, list_node, buckets) {
4341 bucket_stats->packet_count = bucket->stats.packet_count;
4342 bucket_stats->byte_count = bucket->stats.byte_count;
4345 ovs_mutex_unlock(&group->stats_mutex);
4350 /* If the group exists, this function increments the groups's reference count.
4352 * Make sure to call group_dpif_unref() after no longer needing to maintain
4353 * a reference to the group. */
4355 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4356 struct group_dpif **group)
4358 struct ofgroup *ofgroup;
4361 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4362 *group = found ? group_dpif_cast(ofgroup) : NULL;
4368 group_dpif_get_buckets(const struct group_dpif *group,
4369 const struct ovs_list **buckets)
4371 *buckets = &group->up.buckets;
4374 enum ofp11_group_type
4375 group_dpif_get_type(const struct group_dpif *group)
4377 return group->up.type;
4381 group_dpif_get_selection_method(const struct group_dpif *group)
4383 return group->up.props.selection_method;
4386 /* Sends 'packet' out 'ofport'. If 'port' is a tunnel and that tunnel type
4387 * supports a notion of an OAM flag, sets it if 'oam' is true.
4388 * May modify 'packet'.
4389 * Returns 0 if successful, otherwise a positive errno value. */
4391 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, bool oam,
4392 struct dp_packet *packet)
4394 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4397 error = xlate_send_packet(ofport, oam, packet);
4399 ovs_mutex_lock(&ofproto->stats_mutex);
4400 ofproto->stats.tx_packets++;
4401 ofproto->stats.tx_bytes += dp_packet_size(packet);
4402 ovs_mutex_unlock(&ofproto->stats_mutex);
4407 group_dpif_get_selection_method_param(const struct group_dpif *group)
4409 return group->up.props.selection_method_param;
4412 const struct field_array *
4413 group_dpif_get_fields(const struct group_dpif *group)
4415 return &group->up.props.fields;
4418 /* Return the version string of the datapath that backs up
4422 get_datapath_version(const struct ofproto *ofproto_)
4424 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4426 return ofproto->backer->dp_version_string;
4430 set_frag_handling(struct ofproto *ofproto_,
4431 enum ofputil_frag_handling frag_handling)
4433 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4434 if (frag_handling != OFPUTIL_FRAG_REASM) {
4435 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4443 packet_out(struct ofproto *ofproto_, struct dp_packet *packet,
4444 const struct flow *flow,
4445 const struct ofpact *ofpacts, size_t ofpacts_len)
4447 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4449 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4450 ofpacts_len, packet);
4455 nxt_resume(struct ofproto *ofproto_,
4456 const struct ofputil_packet_in_private *pin)
4458 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4460 /* Translate pin into datapath actions. */
4461 uint64_t odp_actions_stub[1024 / 8];
4462 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
4463 enum slow_path_reason slow;
4464 enum ofperr error = xlate_resume(ofproto, pin, &odp_actions, &slow);
4466 /* Steal 'pin->packet' and put it into a dp_packet. */
4467 struct dp_packet packet;
4468 dp_packet_init(&packet, pin->public.packet_len);
4469 dp_packet_put(&packet, pin->public.packet, pin->public.packet_len);
4471 pkt_metadata_from_flow(&packet.md, &pin->public.flow_metadata.flow);
4473 /* Fix up in_port. */
4474 ofproto_dpif_set_packet_odp_port(ofproto,
4475 pin->public.flow_metadata.flow.in_port.ofp_port,
4478 struct flow headers;
4479 flow_extract(&packet, &headers);
4481 /* Execute the datapath actions on the packet. */
4482 struct dpif_execute execute = {
4483 .actions = odp_actions.data,
4484 .actions_len = odp_actions.size,
4485 .needs_help = (slow & SLOW_ACTION) != 0,
4489 dpif_execute(ofproto->backer->dpif, &execute);
4492 ofpbuf_uninit(&odp_actions);
4493 dp_packet_uninit(&packet);
4501 set_netflow(struct ofproto *ofproto_,
4502 const struct netflow_options *netflow_options)
4504 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4506 if (netflow_options) {
4507 if (!ofproto->netflow) {
4508 ofproto->netflow = netflow_create();
4509 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4511 return netflow_set_options(ofproto->netflow, netflow_options);
4512 } else if (ofproto->netflow) {
4513 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4514 netflow_unref(ofproto->netflow);
4515 ofproto->netflow = NULL;
4522 get_netflow_ids(const struct ofproto *ofproto_,
4523 uint8_t *engine_type, uint8_t *engine_id)
4525 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4527 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4530 static struct ofproto_dpif *
4531 ofproto_dpif_lookup(const char *name)
4533 struct ofproto_dpif *ofproto;
4535 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4536 hash_string(name, 0), &all_ofproto_dpifs) {
4537 if (!strcmp(ofproto->up.name, name)) {
4545 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4546 const char *argv[], void *aux OVS_UNUSED)
4548 struct ofproto_dpif *ofproto;
4551 ofproto = ofproto_dpif_lookup(argv[1]);
4553 unixctl_command_reply_error(conn, "no such bridge");
4556 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4557 mac_learning_flush(ofproto->ml);
4558 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4560 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4561 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4562 mac_learning_flush(ofproto->ml);
4563 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4567 unixctl_command_reply(conn, "table successfully flushed");
4571 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4572 const char *argv[], void *aux OVS_UNUSED)
4574 struct ofproto_dpif *ofproto;
4577 ofproto = ofproto_dpif_lookup(argv[1]);
4579 unixctl_command_reply_error(conn, "no such bridge");
4583 if (!mcast_snooping_enabled(ofproto->ms)) {
4584 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4587 mcast_snooping_mdb_flush(ofproto->ms);
4589 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4590 if (!mcast_snooping_enabled(ofproto->ms)) {
4593 mcast_snooping_mdb_flush(ofproto->ms);
4597 unixctl_command_reply(conn, "table successfully flushed");
4600 static struct ofport_dpif *
4601 ofbundle_get_a_port(const struct ofbundle *bundle)
4603 return CONTAINER_OF(ovs_list_front(&bundle->ports), struct ofport_dpif,
4608 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4609 const char *argv[], void *aux OVS_UNUSED)
4611 struct ds ds = DS_EMPTY_INITIALIZER;
4612 const struct ofproto_dpif *ofproto;
4613 const struct mac_entry *e;
4615 ofproto = ofproto_dpif_lookup(argv[1]);
4617 unixctl_command_reply_error(conn, "no such bridge");
4621 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4622 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4623 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4624 struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
4625 char name[OFP_MAX_PORT_NAME_LEN];
4627 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4629 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
4630 name, e->vlan, ETH_ADDR_ARGS(e->mac),
4631 mac_entry_age(ofproto->ml, e));
4633 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4634 unixctl_command_reply(conn, ds_cstr(&ds));
4639 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4640 int argc OVS_UNUSED,
4642 void *aux OVS_UNUSED)
4644 struct ds ds = DS_EMPTY_INITIALIZER;
4645 const struct ofproto_dpif *ofproto;
4646 const struct ofbundle *bundle;
4647 const struct mcast_group *grp;
4648 struct mcast_group_bundle *b;
4649 struct mcast_mrouter_bundle *mrouter;
4651 ofproto = ofproto_dpif_lookup(argv[1]);
4653 unixctl_command_reply_error(conn, "no such bridge");
4657 if (!mcast_snooping_enabled(ofproto->ms)) {
4658 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4662 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
4663 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4664 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4665 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
4666 char name[OFP_MAX_PORT_NAME_LEN];
4669 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4671 ds_put_format(&ds, "%5s %4d ", name, grp->vlan);
4672 ipv6_format_mapped(&grp->addr, &ds);
4673 ds_put_format(&ds, " %3d\n",
4674 mcast_bundle_age(ofproto->ms, b));
4678 /* ports connected to multicast routers */
4679 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
4680 char name[OFP_MAX_PORT_NAME_LEN];
4682 bundle = mrouter->port;
4683 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4685 ds_put_format(&ds, "%5s %4d querier %3d\n",
4686 name, mrouter->vlan,
4687 mcast_mrouter_age(ofproto->ms, mrouter));
4689 ovs_rwlock_unlock(&ofproto->ms->rwlock);
4690 unixctl_command_reply(conn, ds_cstr(&ds));
4695 struct xlate_out xout;
4696 struct xlate_in xin;
4697 const struct flow *key;
4700 struct flow_wildcards wc;
4701 struct ofpbuf odp_actions;
4705 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4707 const struct rule_actions *actions;
4710 ds_put_char_multiple(result, '\t', level);
4712 ds_put_cstr(result, "No match\n");
4716 ovs_mutex_lock(&rule->up.mutex);
4717 cookie = rule->up.flow_cookie;
4718 ovs_mutex_unlock(&rule->up.mutex);
4720 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4721 rule ? rule->up.table_id : 0, ntohll(cookie));
4722 cls_rule_format(&rule->up.cr, result);
4723 ds_put_char(result, '\n');
4725 actions = rule_dpif_get_actions(rule);
4727 ds_put_char_multiple(result, '\t', level);
4728 ds_put_cstr(result, "OpenFlow actions=");
4729 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4730 ds_put_char(result, '\n');
4734 trace_format_flow(struct ds *result, int level, const char *title,
4735 struct trace_ctx *trace)
4737 ds_put_char_multiple(result, '\t', level);
4738 ds_put_format(result, "%s: ", title);
4739 /* Do not report unchanged flows for resubmits. */
4740 if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4741 || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4742 ds_put_cstr(result, "unchanged");
4744 flow_format(result, &trace->xin.flow);
4745 trace->flow = trace->xin.flow;
4747 ds_put_char(result, '\n');
4751 trace_format_regs(struct ds *result, int level, const char *title,
4752 struct trace_ctx *trace)
4756 ds_put_char_multiple(result, '\t', level);
4757 ds_put_format(result, "%s:", title);
4758 for (i = 0; i < FLOW_N_REGS; i++) {
4759 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4761 ds_put_char(result, '\n');
4765 trace_format_odp(struct ds *result, int level, const char *title,
4766 struct trace_ctx *trace)
4768 struct ofpbuf *odp_actions = &trace->odp_actions;
4770 ds_put_char_multiple(result, '\t', level);
4771 ds_put_format(result, "%s: ", title);
4772 format_odp_actions(result, odp_actions->data, odp_actions->size);
4773 ds_put_char(result, '\n');
4777 trace_format_megaflow(struct ds *result, int level, const char *title,
4778 struct trace_ctx *trace)
4782 ds_put_char_multiple(result, '\t', level);
4783 ds_put_format(result, "%s: ", title);
4784 match_init(&match, trace->key, &trace->wc);
4785 match_format(&match, result, OFP_DEFAULT_PRIORITY);
4786 ds_put_char(result, '\n');
4789 static void trace_report(struct xlate_in *, int indentation,
4790 const char *format, ...)
4791 OVS_PRINTF_FORMAT(3, 4);
4792 static void trace_report_valist(struct xlate_in *, int indentation,
4793 const char *format, va_list args)
4794 OVS_PRINTF_FORMAT(3, 0);
4797 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int indentation)
4799 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4800 struct ds *result = trace->result;
4803 if (rule == xin->ofproto->miss_rule) {
4804 trace_report(xin, indentation,
4805 "No match, flow generates \"packet in\"s.");
4806 } else if (rule == xin->ofproto->no_packet_in_rule) {
4807 trace_report(xin, indentation, "No match, packets dropped because "
4808 "OFPPC_NO_PACKET_IN is set on in_port.");
4809 } else if (rule == xin->ofproto->drop_frags_rule) {
4810 trace_report(xin, indentation,
4811 "Packets dropped because they are IP fragments and "
4812 "the fragment handling mode is \"drop\".");
4816 ds_put_char(result, '\n');
4818 trace_format_flow(result, indentation, "Resubmitted flow", trace);
4819 trace_format_regs(result, indentation, "Resubmitted regs", trace);
4820 trace_format_odp(result, indentation, "Resubmitted odp", trace);
4821 trace_format_megaflow(result, indentation, "Resubmitted megaflow",
4824 trace_format_rule(result, indentation, rule);
4828 trace_report_valist(struct xlate_in *xin, int indentation,
4829 const char *format, va_list args)
4831 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4832 struct ds *result = trace->result;
4834 ds_put_char_multiple(result, '\t', indentation);
4835 ds_put_format_valist(result, format, args);
4836 ds_put_char(result, '\n');
4840 trace_report(struct xlate_in *xin, int indentation, const char *format, ...)
4844 va_start(args, format);
4845 trace_report_valist(xin, indentation, format, args);
4849 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4850 * forms are supported:
4852 * - [dpname] odp_flow [-generate | packet]
4853 * - bridge br_flow [-generate | packet]
4855 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4856 * returns a nonnull malloced error message. */
4857 static char * OVS_WARN_UNUSED_RESULT
4858 parse_flow_and_packet(int argc, const char *argv[],
4859 struct ofproto_dpif **ofprotop, struct flow *flow,
4860 struct dp_packet **packetp)
4862 const struct dpif_backer *backer = NULL;
4863 const char *error = NULL;
4865 struct simap port_names = SIMAP_INITIALIZER(&port_names);
4866 struct dp_packet *packet;
4867 struct ofpbuf odp_key;
4868 struct ofpbuf odp_mask;
4870 ofpbuf_init(&odp_key, 0);
4871 ofpbuf_init(&odp_mask, 0);
4873 /* Handle "-generate" or a hex string as the last argument. */
4874 if (!strcmp(argv[argc - 1], "-generate")) {
4875 packet = dp_packet_new(0);
4878 error = eth_from_hex(argv[argc - 1], &packet);
4881 } else if (argc == 4) {
4882 /* The 3-argument form must end in "-generate' or a hex string. */
4888 /* odp_flow can have its in_port specified as a name instead of port no.
4889 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4890 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4891 * we need to create a simap of name to port no. */
4893 const char *dp_type;
4894 if (!strncmp(argv[1], "ovs-", 4)) {
4895 dp_type = argv[1] + 4;
4899 backer = shash_find_data(&all_dpif_backers, dp_type);
4900 } else if (argc == 2) {
4901 struct shash_node *node;
4902 if (shash_count(&all_dpif_backers) == 1) {
4903 node = shash_first(&all_dpif_backers);
4904 backer = node->data;
4907 error = "Syntax error";
4910 if (backer && backer->dpif) {
4911 struct dpif_port dpif_port;
4912 struct dpif_port_dump port_dump;
4913 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4914 simap_put(&port_names, dpif_port.name,
4915 odp_to_u32(dpif_port.port_no));
4919 /* Parse the flow and determine whether a datapath or
4920 * bridge is specified. If function odp_flow_key_from_string()
4921 * returns 0, the flow is a odp_flow. If function
4922 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4923 if (!odp_flow_from_string(argv[argc - 1], &port_names,
4924 &odp_key, &odp_mask)) {
4926 error = "Cannot find the datapath";
4930 if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
4931 error = "Failed to parse datapath flow key";
4935 *ofprotop = xlate_lookup_ofproto(backer, flow,
4936 &flow->in_port.ofp_port);
4937 if (*ofprotop == NULL) {
4938 error = "Invalid datapath flow";
4942 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4945 m_err = xasprintf("Bad openflow flow syntax: %s", err);
4950 error = "Must specify bridge name";
4954 *ofprotop = ofproto_dpif_lookup(argv[1]);
4956 error = "Unknown bridge name";
4962 /* Generate a packet, if requested. */
4964 if (!dp_packet_size(packet)) {
4965 flow_compose(packet, flow);
4967 /* Use the metadata from the flow and the packet argument
4968 * to reconstruct the flow. */
4969 pkt_metadata_from_flow(&packet->md, flow);
4970 flow_extract(packet, flow);
4975 if (error && !m_err) {
4976 m_err = xstrdup(error);
4979 dp_packet_delete(packet);
4983 ofpbuf_uninit(&odp_key);
4984 ofpbuf_uninit(&odp_mask);
4985 simap_destroy(&port_names);
4990 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4991 void *aux OVS_UNUSED)
4993 struct ofproto_dpif *ofproto;
4994 struct dp_packet *packet;
4998 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
5003 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
5004 unixctl_command_reply(conn, ds_cstr(&result));
5005 ds_destroy(&result);
5006 dp_packet_delete(packet);
5008 unixctl_command_reply_error(conn, error);
5014 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
5015 const char *argv[], void *aux OVS_UNUSED)
5017 enum ofputil_protocol usable_protocols;
5018 struct ofproto_dpif *ofproto;
5019 bool enforce_consistency;
5020 struct ofpbuf ofpacts;
5021 struct dp_packet *packet;
5026 /* Three kinds of error return values! */
5032 ofpbuf_init(&ofpacts, 0);
5034 /* Parse actions. */
5035 error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
5037 unixctl_command_reply_error(conn, error);
5042 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
5043 * consistency between the flow and the actions. With -consistent, we
5044 * enforce consistency even for a flow supported in OpenFlow 1.0. */
5045 if (!strcmp(argv[1], "-consistent")) {
5046 enforce_consistency = true;
5050 enforce_consistency = false;
5053 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
5055 unixctl_command_reply_error(conn, error);
5060 /* Do the same checks as handle_packet_out() in ofproto.c.
5062 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
5063 * strictly correct because these actions aren't in any table, but it's OK
5064 * because it 'table_id' is used only to check goto_table instructions, but
5065 * packet-outs take a list of actions and therefore it can't include
5068 * We skip the "meter" check here because meter is an instruction, not an
5069 * action, and thus cannot appear in ofpacts. */
5070 in_port = ofp_to_u16(flow.in_port.ofp_port);
5071 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
5072 unixctl_command_reply_error(conn, "invalid in_port");
5075 if (enforce_consistency) {
5076 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
5077 u16_to_ofp(ofproto->up.max_ports),
5078 0, ofproto->up.n_tables,
5081 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
5082 u16_to_ofp(ofproto->up.max_ports), 0,
5083 ofproto->up.n_tables, &usable_protocols);
5086 retval = ofproto_check_ofpacts(&ofproto->up, ofpacts.data,
5092 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
5093 unixctl_command_reply_error(conn, ds_cstr(&result));
5097 ofproto_trace(ofproto, &flow, packet,
5098 ofpacts.data, ofpacts.size, &result);
5099 unixctl_command_reply(conn, ds_cstr(&result));
5102 ds_destroy(&result);
5103 dp_packet_delete(packet);
5104 ofpbuf_uninit(&ofpacts);
5107 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
5108 * description of the results to 'ds'.
5110 * The trace follows a packet with the specified 'flow' through the flow
5111 * table. 'packet' may be nonnull to trace an actual packet, with consequent
5112 * side effects (if it is nonnull then its flow must be 'flow').
5114 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
5115 * trace, otherwise the actions are determined by a flow table lookup. */
5117 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
5118 const struct dp_packet *packet,
5119 const struct ofpact ofpacts[], size_t ofpacts_len,
5122 struct trace_ctx trace;
5123 enum xlate_error error;
5125 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
5126 ds_put_cstr(ds, "Flow: ");
5127 flow_format(ds, flow);
5128 ds_put_char(ds, '\n');
5130 ofpbuf_init(&trace.odp_actions, 0);
5133 trace.key = flow; /* Original flow key, used for megaflow. */
5134 trace.flow = *flow; /* May be modified by actions. */
5135 xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
5136 ntohs(flow->tcp_flags), packet, &trace.wc,
5137 &trace.odp_actions);
5138 trace.xin.ofpacts = ofpacts;
5139 trace.xin.ofpacts_len = ofpacts_len;
5140 trace.xin.resubmit_hook = trace_resubmit;
5141 trace.xin.report_hook = trace_report_valist;
5143 error = xlate_actions(&trace.xin, &trace.xout);
5144 ds_put_char(ds, '\n');
5145 trace.xin.flow.actset_output = 0;
5146 trace_format_flow(ds, 0, "Final flow", &trace);
5147 trace_format_megaflow(ds, 0, "Megaflow", &trace);
5149 ds_put_cstr(ds, "Datapath actions: ");
5150 format_odp_actions(ds, trace.odp_actions.data, trace.odp_actions.size);
5152 if (error != XLATE_OK) {
5153 ds_put_format(ds, "\nTranslation failed (%s), packet is dropped.\n",
5154 xlate_strerror(error));
5155 } else if (trace.xout.slow) {
5156 enum slow_path_reason slow;
5158 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
5159 "slow path because it:");
5161 slow = trace.xout.slow;
5163 enum slow_path_reason bit = rightmost_1bit(slow);
5165 ds_put_format(ds, "\n\t- %s.",
5166 slow_path_reason_to_explanation(bit));
5172 xlate_out_uninit(&trace.xout);
5173 ofpbuf_uninit(&trace.odp_actions);
5176 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5177 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5178 * to destroy 'ofproto_shash' and free the returned value. */
5179 static const struct shash_node **
5180 get_ofprotos(struct shash *ofproto_shash)
5182 const struct ofproto_dpif *ofproto;
5184 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5185 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5186 shash_add_nocopy(ofproto_shash, name, ofproto);
5189 return shash_sort(ofproto_shash);
5193 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5194 const char *argv[] OVS_UNUSED,
5195 void *aux OVS_UNUSED)
5197 struct ds ds = DS_EMPTY_INITIALIZER;
5198 struct shash ofproto_shash;
5199 const struct shash_node **sorted_ofprotos;
5202 shash_init(&ofproto_shash);
5203 sorted_ofprotos = get_ofprotos(&ofproto_shash);
5204 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5205 const struct shash_node *node = sorted_ofprotos[i];
5206 ds_put_format(&ds, "%s\n", node->name);
5209 shash_destroy(&ofproto_shash);
5210 free(sorted_ofprotos);
5212 unixctl_command_reply(conn, ds_cstr(&ds));
5217 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5219 const struct shash_node **ofprotos;
5220 struct dpif_dp_stats dp_stats;
5221 struct shash ofproto_shash;
5224 dpif_get_dp_stats(backer->dpif, &dp_stats);
5226 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5227 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
5229 shash_init(&ofproto_shash);
5230 ofprotos = get_ofprotos(&ofproto_shash);
5231 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5232 struct ofproto_dpif *ofproto = ofprotos[i]->data;
5233 const struct shash_node **ports;
5236 if (ofproto->backer != backer) {
5240 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
5242 ports = shash_sort(&ofproto->up.port_by_name);
5243 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5244 const struct shash_node *node = ports[j];
5245 struct ofport *ofport = node->data;
5247 odp_port_t odp_port;
5249 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5252 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5253 if (odp_port != ODPP_NONE) {
5254 ds_put_format(ds, "%"PRIu32":", odp_port);
5256 ds_put_cstr(ds, "none:");
5259 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5262 if (!netdev_get_config(ofport->netdev, &config)) {
5263 const struct smap_node **nodes;
5266 nodes = smap_sort(&config);
5267 for (i = 0; i < smap_count(&config); i++) {
5268 const struct smap_node *node = nodes[i];
5269 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5270 node->key, node->value);
5274 smap_destroy(&config);
5276 ds_put_char(ds, ')');
5277 ds_put_char(ds, '\n');
5281 shash_destroy(&ofproto_shash);
5286 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5287 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5289 struct ds ds = DS_EMPTY_INITIALIZER;
5290 const struct shash_node **backers;
5293 backers = shash_sort(&all_dpif_backers);
5294 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5295 dpif_show_backer(backers[i]->data, &ds);
5299 unixctl_command_reply(conn, ds_cstr(&ds));
5304 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5305 int argc OVS_UNUSED, const char *argv[],
5306 void *aux OVS_UNUSED)
5308 const struct ofproto_dpif *ofproto;
5310 struct ds ds = DS_EMPTY_INITIALIZER;
5311 bool verbosity = false;
5313 struct dpif_port dpif_port;
5314 struct dpif_port_dump port_dump;
5315 struct hmap portno_names;
5317 struct dpif_flow_dump *flow_dump;
5318 struct dpif_flow_dump_thread *flow_dump_thread;
5322 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5324 unixctl_command_reply_error(conn, "no such bridge");
5328 if (argc > 2 && !strcmp(argv[1], "-m")) {
5332 hmap_init(&portno_names);
5333 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5334 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5338 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
5339 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5340 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5343 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5344 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5349 odp_format_ufid(&f.ufid, &ds);
5350 ds_put_cstr(&ds, " ");
5352 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5353 &portno_names, &ds, verbosity);
5354 ds_put_cstr(&ds, ", ");
5355 dpif_flow_stats_format(&f.stats, &ds);
5356 ds_put_cstr(&ds, ", actions:");
5357 format_odp_actions(&ds, f.actions, f.actions_len);
5358 ds_put_char(&ds, '\n');
5360 dpif_flow_dump_thread_destroy(flow_dump_thread);
5361 error = dpif_flow_dump_destroy(flow_dump);
5365 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5366 unixctl_command_reply_error(conn, ds_cstr(&ds));
5368 unixctl_command_reply(conn, ds_cstr(&ds));
5370 odp_portno_names_destroy(&portno_names);
5371 hmap_destroy(&portno_names);
5376 ofproto_revalidate_all_backers(void)
5378 const struct shash_node **backers;
5381 backers = shash_sort(&all_dpif_backers);
5382 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5383 struct dpif_backer *backer = backers[i]->data;
5384 backer->need_revalidate = REV_RECONFIGURE;
5390 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5391 const char *argv[], void *aux OVS_UNUSED)
5393 if (!strcasecmp(argv[1], "off")) {
5394 ofproto_use_tnl_push_pop = false;
5395 unixctl_command_reply(conn, "Tunnel push-pop off");
5396 ofproto_revalidate_all_backers();
5397 } else if (!strcasecmp(argv[1], "on")) {
5398 ofproto_use_tnl_push_pop = true;
5399 unixctl_command_reply(conn, "Tunnel push-pop on");
5400 ofproto_revalidate_all_backers();
5402 unixctl_command_reply_error(conn, "Invalid argument");
5407 disable_datapath_truncate(struct unixctl_conn *conn OVS_UNUSED,
5408 int argc OVS_UNUSED,
5409 const char *argv[] OVS_UNUSED,
5410 void *aux OVS_UNUSED)
5412 const struct shash_node **backers;
5415 backers = shash_sort(&all_dpif_backers);
5416 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5417 struct dpif_backer *backer = backers[i]->data;
5418 backer->support.trunc = false;
5421 unixctl_command_reply(conn, "Datapath truncate action diabled");
5425 ofproto_unixctl_init(void)
5427 static bool registered;
5433 unixctl_command_register(
5435 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5436 1, 3, ofproto_unixctl_trace, NULL);
5437 unixctl_command_register(
5438 "ofproto/trace-packet-out",
5439 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5440 2, 6, ofproto_unixctl_trace_actions, NULL);
5441 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5442 ofproto_unixctl_fdb_flush, NULL);
5443 unixctl_command_register("fdb/show", "bridge", 1, 1,
5444 ofproto_unixctl_fdb_show, NULL);
5445 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5446 ofproto_unixctl_mcast_snooping_flush, NULL);
5447 unixctl_command_register("mdb/show", "bridge", 1, 1,
5448 ofproto_unixctl_mcast_snooping_show, NULL);
5449 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5450 ofproto_unixctl_dpif_dump_dps, NULL);
5451 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5453 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5454 ofproto_unixctl_dpif_dump_flows, NULL);
5456 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5457 disable_tnl_push_pop, NULL);
5459 unixctl_command_register("dpif/disable-truncate", "", 0, 0,
5460 disable_datapath_truncate, NULL);
5463 /* Returns true if 'table' is the table used for internal rules,
5464 * false otherwise. */
5466 table_is_internal(uint8_t table_id)
5468 return table_id == TBL_INTERNAL;
5473 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5475 const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
5476 return ofport ? ofport->odp_port : ODPP_NONE;
5479 struct ofport_dpif *
5480 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5482 struct ofport_dpif *port;
5484 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5485 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5486 &backer->odp_to_ofport_map) {
5487 if (port->odp_port == odp_port) {
5488 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5493 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5498 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5500 struct ofport_dpif *port;
5502 port = odp_port_to_ofport(ofproto->backer, odp_port);
5503 if (port && &ofproto->up == port->up.ofproto) {
5504 return port->up.ofp_port;
5511 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5512 const struct match *match, int priority,
5513 uint16_t idle_timeout,
5514 const struct ofpbuf *ofpacts,
5515 struct rule **rulep)
5517 struct ofproto_flow_mod ofm;
5518 struct rule_dpif *rule;
5521 ofm.fm = (struct ofputil_flow_mod) {
5523 .priority = priority,
5524 .table_id = TBL_INTERNAL,
5525 .command = OFPFC_ADD,
5526 .idle_timeout = idle_timeout,
5527 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5528 .ofpacts = ofpacts->data,
5529 .ofpacts_len = ofpacts->size,
5530 .delete_reason = OVS_OFPRR_NONE,
5533 error = ofproto_flow_mod(&ofproto->up, &ofm);
5535 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5536 ofperr_to_string(error));
5541 rule = rule_dpif_lookup_in_table(ofproto,
5542 ofproto_dpif_get_tables_version(ofproto),
5543 TBL_INTERNAL, &ofm.fm.match.flow,
5554 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5555 struct match *match, int priority)
5557 struct ofproto_flow_mod ofm;
5560 ofm.fm = (struct ofputil_flow_mod) {
5562 .priority = priority,
5563 .table_id = TBL_INTERNAL,
5564 .flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY,
5565 .command = OFPFC_DELETE_STRICT,
5568 error = ofproto_flow_mod(&ofproto->up, &ofm);
5570 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5571 ofperr_to_string(error));
5579 ofproto_dpif_get_uuid(const struct ofproto_dpif *ofproto)
5581 return &ofproto->uuid;
5584 const struct ofproto_class ofproto_dpif_class = {
5598 NULL, /* get_memory_usage. */
5599 type_get_memory_usage,
5618 port_is_lacp_current,
5619 port_get_lacp_stats,
5620 NULL, /* rule_choose_table */
5624 NULL, /* rule_delete */
5646 aa_vlan_get_queue_size,
5653 get_stp_port_status,
5658 get_rstp_port_status,
5665 is_mirror_output_bundle,
5666 forward_bpdu_changed,
5667 set_mac_table_config,
5669 set_mcast_snooping_port,
5670 NULL, /* meter_get_features */
5671 NULL, /* meter_set */
5672 NULL, /* meter_get */
5673 NULL, /* meter_del */
5674 group_alloc, /* group_alloc */
5675 group_construct, /* group_construct */
5676 group_destruct, /* group_destruct */
5677 group_dealloc, /* group_dealloc */
5678 group_modify, /* group_modify */
5679 group_get_stats, /* group_get_stats */
5680 get_datapath_version, /* get_datapath_version */