2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-dpif.h"
20 #include "ofproto/ofproto-provider.h"
27 #include "byte-order.h"
28 #include "connectivity.h"
34 #include "dynamic-string.h"
35 #include "fail-open.h"
36 #include "guarded-list.h"
40 #include "mac-learning.h"
41 #include "mcast-snooping.h"
42 #include "meta-flow.h"
43 #include "multipath.h"
44 #include "netdev-vport.h"
49 #include "odp-execute.h"
52 #include "ofp-actions.h"
53 #include "ofp-parse.h"
54 #include "ofp-print.h"
55 #include "ofproto-dpif-ipfix.h"
56 #include "ofproto-dpif-mirror.h"
57 #include "ofproto-dpif-monitor.h"
58 #include "ofproto-dpif-rid.h"
59 #include "ofproto-dpif-sflow.h"
60 #include "ofproto-dpif-upcall.h"
61 #include "ofproto-dpif-xlate.h"
62 #include "poll-loop.h"
64 #include "ovs-router.h"
70 #include "unaligned.h"
72 #include "vlan-bitmap.h"
73 #include "openvswitch/vlog.h"
75 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
77 COVERAGE_DEFINE(ofproto_dpif_expired);
78 COVERAGE_DEFINE(packet_in_overflow);
87 * - Do include packets and bytes from datapath flows which have not
88 * recently been processed by a revalidator. */
89 struct ovs_mutex stats_mutex;
90 struct dpif_flow_stats stats OVS_GUARDED;
92 /* In non-NULL, will point to a new rule (for which a reference is held) to
93 * which all the stats updates should be forwarded. This exists only
94 * transitionally when flows are replaced.
96 * Protected by stats_mutex. If both 'rule->stats_mutex' and
97 * 'rule->new_rule->stats_mutex' must be held together, acquire them in that
99 struct rule_dpif *new_rule OVS_GUARDED;
101 /* If non-zero then the recirculation id that has
102 * been allocated for use with this rule.
103 * The recirculation id and associated internal flow should
104 * be freed when the rule is freed */
108 /* RULE_CAST() depends on this. */
109 BUILD_ASSERT_DECL(offsetof(struct rule_dpif, up) == 0);
111 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
112 long long int *used);
113 static struct rule_dpif *rule_dpif_cast(const struct rule *);
114 static void rule_expire(struct rule_dpif *);
121 * - Do include packets and bytes from datapath flows which have not
122 * recently been processed by a revalidator. */
123 struct ovs_mutex stats_mutex;
124 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
125 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
129 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
130 struct ofproto_dpif *ofproto; /* Owning ofproto. */
131 void *aux; /* Key supplied by ofproto's client. */
132 char *name; /* Identifier for log messages. */
135 struct ovs_list ports; /* Contains "struct ofport"s. */
136 enum port_vlan_mode vlan_mode; /* VLAN mode */
137 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
138 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
139 * NULL if all VLANs are trunked. */
140 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
141 struct bond *bond; /* Nonnull iff more than one port. */
142 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
145 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
148 static void bundle_remove(struct ofport *);
149 static void bundle_update(struct ofbundle *);
150 static void bundle_destroy(struct ofbundle *);
151 static void bundle_del_port(struct ofport_dpif *);
152 static void bundle_run(struct ofbundle *);
153 static void bundle_wait(struct ofbundle *);
154 static void bundle_flush_macs(struct ofbundle *, bool);
155 static void bundle_move(struct ofbundle *, struct ofbundle *);
157 static void stp_run(struct ofproto_dpif *ofproto);
158 static void stp_wait(struct ofproto_dpif *ofproto);
159 static int set_stp_port(struct ofport *,
160 const struct ofproto_port_stp_settings *);
162 static void rstp_run(struct ofproto_dpif *ofproto);
163 static void set_rstp_port(struct ofport *,
164 const struct ofproto_port_rstp_settings *);
167 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
171 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
172 struct ovs_list bundle_node;/* In struct ofbundle's "ports" list. */
173 struct cfm *cfm; /* Connectivity Fault Management, if any. */
174 struct bfd *bfd; /* BFD, if any. */
175 struct lldp *lldp; /* lldp, if any. */
176 bool may_enable; /* May be enabled in bonds. */
177 bool is_tunnel; /* This port is a tunnel. */
178 bool is_layer3; /* This is a layer 3 port. */
179 long long int carrier_seq; /* Carrier status changes. */
180 struct ofport_dpif *peer; /* Peer if patch port. */
183 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
184 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
185 long long int stp_state_entered;
187 /* Rapid Spanning Tree. */
188 struct rstp_port *rstp_port; /* Rapid Spanning Tree Protocol, if any. */
189 enum rstp_state rstp_state; /* Always RSTP_DISABLED if RSTP not in use. */
191 /* Queue to DSCP mapping. */
192 struct ofproto_port_queue *qdscp;
195 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
197 * This is deprecated. It is only for compatibility with broken device
198 * drivers in old versions of Linux that do not properly support VLANs when
199 * VLAN devices are not used. When broken device drivers are no longer in
200 * widespread use, we will delete these interfaces. */
201 ofp_port_t realdev_ofp_port;
205 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
207 * This is deprecated. It is only for compatibility with broken device drivers
208 * in old versions of Linux that do not properly support VLANs when VLAN
209 * devices are not used. When broken device drivers are no longer in
210 * widespread use, we will delete these interfaces. */
211 struct vlan_splinter {
212 struct hmap_node realdev_vid_node;
213 struct hmap_node vlandev_node;
214 ofp_port_t realdev_ofp_port;
215 ofp_port_t vlandev_ofp_port;
219 static void vsp_remove(struct ofport_dpif *);
220 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
222 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
225 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
228 static struct ofport_dpif *
229 ofport_dpif_cast(const struct ofport *ofport)
231 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
234 static void port_run(struct ofport_dpif *);
235 static int set_bfd(struct ofport *, const struct smap *);
236 static int set_cfm(struct ofport *, const struct cfm_settings *);
237 static int set_lldp(struct ofport *ofport_, const struct smap *cfg);
238 static void ofport_update_peer(struct ofport_dpif *);
240 /* Reasons that we might need to revalidate every datapath flow, and
241 * corresponding coverage counters.
243 * A value of 0 means that there is no need to revalidate.
245 * It would be nice to have some cleaner way to integrate with coverage
246 * counters, but with only a few reasons I guess this is good enough for
248 enum revalidate_reason {
249 REV_RECONFIGURE = 1, /* Switch configuration changed. */
250 REV_STP, /* Spanning tree protocol port status change. */
251 REV_RSTP, /* RSTP port status change. */
252 REV_BOND, /* Bonding changed. */
253 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
254 REV_FLOW_TABLE, /* Flow table changed. */
255 REV_MAC_LEARNING, /* Mac learning changed. */
256 REV_MCAST_SNOOPING, /* Multicast snooping changed. */
258 COVERAGE_DEFINE(rev_reconfigure);
259 COVERAGE_DEFINE(rev_stp);
260 COVERAGE_DEFINE(rev_rstp);
261 COVERAGE_DEFINE(rev_bond);
262 COVERAGE_DEFINE(rev_port_toggled);
263 COVERAGE_DEFINE(rev_flow_table);
264 COVERAGE_DEFINE(rev_mac_learning);
265 COVERAGE_DEFINE(rev_mcast_snooping);
267 /* All datapaths of a given type share a single dpif backer instance. */
274 struct ovs_rwlock odp_to_ofport_lock;
275 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
277 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
279 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
281 bool recv_set_enable; /* Enables or disables receiving packets. */
283 /* Version string of the datapath stored in OVSDB. */
284 char *dp_version_string;
286 /* Datapath feature support. */
287 struct dpif_backer_support support;
288 struct atomic_count tnl_count;
291 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
292 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
294 struct ofproto_dpif {
295 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
297 struct dpif_backer *backer;
299 ATOMIC(cls_version_t) tables_version; /* For classifier lookups. */
301 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
303 /* Special OpenFlow rules. */
304 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
305 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
306 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
309 struct netflow *netflow;
310 struct dpif_sflow *sflow;
311 struct dpif_ipfix *ipfix;
312 struct hmap bundles; /* Contains "struct ofbundle"s. */
313 struct mac_learning *ml;
314 struct mcast_snooping *ms;
315 bool has_bonded_bundles;
317 struct mbridge *mbridge;
319 struct ovs_mutex stats_mutex;
320 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
321 * consumed in userspace. */
325 long long int stp_last_tick;
327 /* Rapid Spanning Tree. */
329 long long int rstp_last_tick;
331 /* VLAN splinters. */
332 struct ovs_mutex vsp_mutex;
333 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
334 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
337 struct sset ports; /* Set of standard port names. */
338 struct sset ghost_ports; /* Ports with no datapath port. */
339 struct sset port_poll_set; /* Queued names for port_poll() reply. */
340 int port_poll_errno; /* Last errno for port_poll() reply. */
341 uint64_t change_seq; /* Connectivity status changes. */
344 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
345 struct seq *pins_seq; /* For notifying 'pins' reception. */
349 /* All existing ofproto_dpif instances, indexed by ->up.name. */
350 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
352 static bool ofproto_use_tnl_push_pop = true;
353 static void ofproto_unixctl_init(void);
355 static inline struct ofproto_dpif *
356 ofproto_dpif_cast(const struct ofproto *ofproto)
358 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
359 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
363 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
365 return ofproto->backer->support.max_mpls_depth;
369 ofproto_dpif_get_enable_recirc(const struct ofproto_dpif *ofproto)
371 return ofproto->backer->support.recirc;
375 ofproto_dpif_get_enable_ufid(struct dpif_backer *backer)
377 return backer->support.ufid;
380 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
381 const struct dp_packet *packet,
382 const struct ofpact[], size_t ofpacts_len,
385 /* Global variables. */
386 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
388 /* Initial mappings of port to bridge mappings. */
389 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
391 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
394 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
395 struct ofputil_flow_mod *fm)
397 ofproto_flow_mod(&ofproto->up, fm);
400 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
401 * Takes ownership of 'pin' and pin->packet. */
403 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
404 struct ofproto_packet_in *pin)
406 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
407 COVERAGE_INC(packet_in_overflow);
408 free(CONST_CAST(void *, pin->up.packet));
412 /* Wakes up main thread for packet-in I/O. */
413 seq_change(ofproto->pins_seq);
416 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
417 * packet rather than to send the packet to the controller.
419 * This function returns false to indicate that a packet_in message
420 * for a "table-miss" should be sent to at least one controller.
421 * False otherwise. */
423 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
425 return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
428 /* Factory functions. */
431 init(const struct shash *iface_hints)
433 struct shash_node *node;
435 /* Make a local copy, since we don't own 'iface_hints' elements. */
436 SHASH_FOR_EACH(node, iface_hints) {
437 const struct iface_hint *orig_hint = node->data;
438 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
440 new_hint->br_name = xstrdup(orig_hint->br_name);
441 new_hint->br_type = xstrdup(orig_hint->br_type);
442 new_hint->ofp_port = orig_hint->ofp_port;
444 shash_add(&init_ofp_ports, node->name, new_hint);
449 enumerate_types(struct sset *types)
451 dp_enumerate_types(types);
455 enumerate_names(const char *type, struct sset *names)
457 struct ofproto_dpif *ofproto;
460 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
461 if (strcmp(type, ofproto->up.type)) {
464 sset_add(names, ofproto->up.name);
471 del(const char *type, const char *name)
476 error = dpif_open(name, type, &dpif);
478 error = dpif_delete(dpif);
485 port_open_type(const char *datapath_type, const char *port_type)
487 return dpif_port_open_type(datapath_type, port_type);
490 /* Type functions. */
492 static void process_dpif_port_changes(struct dpif_backer *);
493 static void process_dpif_all_ports_changed(struct dpif_backer *);
494 static void process_dpif_port_change(struct dpif_backer *,
495 const char *devname);
496 static void process_dpif_port_error(struct dpif_backer *, int error);
498 static struct ofproto_dpif *
499 lookup_ofproto_dpif_by_port_name(const char *name)
501 struct ofproto_dpif *ofproto;
503 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
504 if (sset_contains(&ofproto->ports, name)) {
513 type_run(const char *type)
515 struct dpif_backer *backer;
517 backer = shash_find_data(&all_dpif_backers, type);
519 /* This is not necessarily a problem, since backers are only
520 * created on demand. */
525 if (dpif_run(backer->dpif)) {
526 backer->need_revalidate = REV_RECONFIGURE;
529 udpif_run(backer->udpif);
531 /* If vswitchd started with other_config:flow_restore_wait set as "true",
532 * and the configuration has now changed to "false", enable receiving
533 * packets from the datapath. */
534 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
537 backer->recv_set_enable = true;
539 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
541 VLOG_ERR("Failed to enable receiving packets in dpif.");
544 dpif_flow_flush(backer->dpif);
545 backer->need_revalidate = REV_RECONFIGURE;
548 if (backer->recv_set_enable) {
549 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
552 dpif_poll_threads_set(backer->dpif, n_dpdk_rxqs, pmd_cpu_mask);
554 if (backer->need_revalidate) {
555 struct ofproto_dpif *ofproto;
556 struct simap_node *node;
557 struct simap tmp_backers;
559 /* Handle tunnel garbage collection. */
560 simap_init(&tmp_backers);
561 simap_swap(&backer->tnl_backers, &tmp_backers);
563 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
564 struct ofport_dpif *iter;
566 if (backer != ofproto->backer) {
570 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
571 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
574 if (!iter->is_tunnel) {
578 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
579 namebuf, sizeof namebuf);
580 node = simap_find(&tmp_backers, dp_port);
582 simap_put(&backer->tnl_backers, dp_port, node->data);
583 simap_delete(&tmp_backers, node);
584 node = simap_find(&backer->tnl_backers, dp_port);
586 node = simap_find(&backer->tnl_backers, dp_port);
588 odp_port_t odp_port = ODPP_NONE;
590 if (!dpif_port_add(backer->dpif, iter->up.netdev,
592 simap_put(&backer->tnl_backers, dp_port,
593 odp_to_u32(odp_port));
594 node = simap_find(&backer->tnl_backers, dp_port);
599 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
600 if (tnl_port_reconfigure(iter, iter->up.netdev,
602 ovs_native_tunneling_is_on(ofproto), dp_port)) {
603 backer->need_revalidate = REV_RECONFIGURE;
608 SIMAP_FOR_EACH (node, &tmp_backers) {
609 dpif_port_del(backer->dpif, u32_to_odp(node->data));
611 simap_destroy(&tmp_backers);
613 switch (backer->need_revalidate) {
614 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
615 case REV_STP: COVERAGE_INC(rev_stp); break;
616 case REV_RSTP: COVERAGE_INC(rev_rstp); break;
617 case REV_BOND: COVERAGE_INC(rev_bond); break;
618 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
619 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
620 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
621 case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
623 backer->need_revalidate = 0;
625 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
626 struct ofport_dpif *ofport;
627 struct ofbundle *bundle;
629 if (ofproto->backer != backer) {
634 xlate_ofproto_set(ofproto, ofproto->up.name,
635 ofproto->backer->dpif, ofproto->ml,
636 ofproto->stp, ofproto->rstp, ofproto->ms,
637 ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
639 ofproto->up.forward_bpdu,
640 connmgr_has_in_band(ofproto->up.connmgr),
641 &ofproto->backer->support);
643 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
644 xlate_bundle_set(ofproto, bundle, bundle->name,
645 bundle->vlan_mode, bundle->vlan,
646 bundle->trunks, bundle->use_priority_tags,
647 bundle->bond, bundle->lacp,
651 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
652 int stp_port = ofport->stp_port
653 ? stp_port_no(ofport->stp_port)
655 xlate_ofport_set(ofproto, ofport->bundle, ofport,
656 ofport->up.ofp_port, ofport->odp_port,
657 ofport->up.netdev, ofport->cfm, ofport->bfd,
658 ofport->lldp, ofport->peer, stp_port,
659 ofport->rstp_port, ofport->qdscp,
660 ofport->n_qdscp, ofport->up.pp.config,
661 ofport->up.pp.state, ofport->is_tunnel,
667 udpif_revalidate(backer->udpif);
670 process_dpif_port_changes(backer);
675 /* Check for and handle port changes in 'backer''s dpif. */
677 process_dpif_port_changes(struct dpif_backer *backer)
683 error = dpif_port_poll(backer->dpif, &devname);
689 process_dpif_all_ports_changed(backer);
693 process_dpif_port_change(backer, devname);
698 process_dpif_port_error(backer, error);
705 process_dpif_all_ports_changed(struct dpif_backer *backer)
707 struct ofproto_dpif *ofproto;
708 struct dpif_port dpif_port;
709 struct dpif_port_dump dump;
710 struct sset devnames;
713 sset_init(&devnames);
714 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
715 if (ofproto->backer == backer) {
716 struct ofport *ofport;
718 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
719 sset_add(&devnames, netdev_get_name(ofport->netdev));
723 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
724 sset_add(&devnames, dpif_port.name);
727 SSET_FOR_EACH (devname, &devnames) {
728 process_dpif_port_change(backer, devname);
730 sset_destroy(&devnames);
734 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
736 struct ofproto_dpif *ofproto;
737 struct dpif_port port;
739 /* Don't report on the datapath's device. */
740 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
744 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
745 &all_ofproto_dpifs) {
746 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
751 ofproto = lookup_ofproto_dpif_by_port_name(devname);
752 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
753 /* The port was removed. If we know the datapath,
754 * report it through poll_set(). If we don't, it may be
755 * notifying us of a removal we initiated, so ignore it.
756 * If there's a pending ENOBUFS, let it stand, since
757 * everything will be reevaluated. */
758 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
759 sset_add(&ofproto->port_poll_set, devname);
760 ofproto->port_poll_errno = 0;
762 } else if (!ofproto) {
763 /* The port was added, but we don't know with which
764 * ofproto we should associate it. Delete it. */
765 dpif_port_del(backer->dpif, port.port_no);
767 struct ofport_dpif *ofport;
769 ofport = ofport_dpif_cast(shash_find_data(
770 &ofproto->up.port_by_name, devname));
772 && ofport->odp_port != port.port_no
773 && !odp_port_to_ofport(backer, port.port_no))
775 /* 'ofport''s datapath port number has changed from
776 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
777 * structures to match. */
778 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
779 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
780 ofport->odp_port = port.port_no;
781 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
782 hash_odp_port(port.port_no));
783 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
784 backer->need_revalidate = REV_RECONFIGURE;
787 dpif_port_destroy(&port);
790 /* Propagate 'error' to all ofprotos based on 'backer'. */
792 process_dpif_port_error(struct dpif_backer *backer, int error)
794 struct ofproto_dpif *ofproto;
796 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
797 if (ofproto->backer == backer) {
798 sset_clear(&ofproto->port_poll_set);
799 ofproto->port_poll_errno = error;
805 type_wait(const char *type)
807 struct dpif_backer *backer;
809 backer = shash_find_data(&all_dpif_backers, type);
811 /* This is not necessarily a problem, since backers are only
812 * created on demand. */
816 dpif_wait(backer->dpif);
819 /* Basic life-cycle. */
821 static int add_internal_flows(struct ofproto_dpif *);
823 static struct ofproto *
826 struct ofproto_dpif *ofproto = xzalloc(sizeof *ofproto);
831 dealloc(struct ofproto *ofproto_)
833 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
838 close_dpif_backer(struct dpif_backer *backer)
840 ovs_assert(backer->refcount > 0);
842 if (--backer->refcount) {
846 udpif_destroy(backer->udpif);
848 simap_destroy(&backer->tnl_backers);
849 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
850 hmap_destroy(&backer->odp_to_ofport_map);
851 shash_find_and_delete(&all_dpif_backers, backer->type);
853 free(backer->dp_version_string);
854 dpif_close(backer->dpif);
858 /* Datapath port slated for removal from datapath. */
860 struct ovs_list list_node;
864 static bool check_variable_length_userdata(struct dpif_backer *backer);
865 static void check_support(struct dpif_backer *backer);
868 open_dpif_backer(const char *type, struct dpif_backer **backerp)
870 struct dpif_backer *backer;
871 struct dpif_port_dump port_dump;
872 struct dpif_port port;
873 struct shash_node *node;
874 struct ovs_list garbage_list;
875 struct odp_garbage *garbage;
884 backer = shash_find_data(&all_dpif_backers, type);
891 backer_name = xasprintf("ovs-%s", type);
893 /* Remove any existing datapaths, since we assume we're the only
894 * userspace controlling the datapath. */
896 dp_enumerate_names(type, &names);
897 SSET_FOR_EACH(name, &names) {
898 struct dpif *old_dpif;
900 /* Don't remove our backer if it exists. */
901 if (!strcmp(name, backer_name)) {
905 if (dpif_open(name, type, &old_dpif)) {
906 VLOG_WARN("couldn't open old datapath %s to remove it", name);
908 dpif_delete(old_dpif);
909 dpif_close(old_dpif);
912 sset_destroy(&names);
914 backer = xmalloc(sizeof *backer);
916 error = dpif_create_and_open(backer_name, type, &backer->dpif);
919 VLOG_ERR("failed to open datapath of type %s: %s", type,
920 ovs_strerror(error));
924 backer->udpif = udpif_create(backer, backer->dpif);
926 backer->type = xstrdup(type);
927 backer->refcount = 1;
928 hmap_init(&backer->odp_to_ofport_map);
929 ovs_rwlock_init(&backer->odp_to_ofport_lock);
930 backer->need_revalidate = 0;
931 simap_init(&backer->tnl_backers);
932 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
935 if (backer->recv_set_enable) {
936 dpif_flow_flush(backer->dpif);
939 /* Loop through the ports already on the datapath and remove any
940 * that we don't need anymore. */
941 list_init(&garbage_list);
942 dpif_port_dump_start(&port_dump, backer->dpif);
943 while (dpif_port_dump_next(&port_dump, &port)) {
944 node = shash_find(&init_ofp_ports, port.name);
945 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
946 garbage = xmalloc(sizeof *garbage);
947 garbage->odp_port = port.port_no;
948 list_push_front(&garbage_list, &garbage->list_node);
951 dpif_port_dump_done(&port_dump);
953 LIST_FOR_EACH_POP (garbage, list_node, &garbage_list) {
954 dpif_port_del(backer->dpif, garbage->odp_port);
958 shash_add(&all_dpif_backers, type, backer);
960 check_support(backer);
961 atomic_count_init(&backer->tnl_count, 0);
963 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
965 VLOG_ERR("failed to listen on datapath of type %s: %s",
966 type, ovs_strerror(error));
967 close_dpif_backer(backer);
971 if (backer->recv_set_enable) {
972 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
975 /* This check fails if performed before udpif threads have been set,
976 * as the kernel module checks that the 'pid' in userspace action
978 backer->support.variable_length_userdata
979 = check_variable_length_userdata(backer);
980 backer->dp_version_string = dpif_get_dp_version(backer->dpif);
986 ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
988 return ofproto_use_tnl_push_pop && ofproto->backer->support.tnl_push_pop &&
989 atomic_count_get(&ofproto->backer->tnl_count);
992 /* Tests whether 'backer''s datapath supports recirculation. Only newer
993 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
994 * features on older datapaths that don't support this feature.
996 * Returns false if 'backer' definitely does not support recirculation, true if
997 * it seems to support recirculation or if at least the error we get is
1000 check_recirc(struct dpif_backer *backer)
1003 struct odputil_keybuf keybuf;
1006 struct odp_flow_key_parms odp_parms = {
1011 memset(&flow, 0, sizeof flow);
1015 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1016 odp_flow_key_from_flow(&odp_parms, &key);
1017 enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
1020 if (enable_recirc) {
1021 VLOG_INFO("%s: Datapath supports recirculation",
1022 dpif_name(backer->dpif));
1024 VLOG_INFO("%s: Datapath does not support recirculation",
1025 dpif_name(backer->dpif));
1028 return enable_recirc;
1031 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
1032 * some flow attributes for datapaths that support this feature.
1034 * Returns true if 'dpif' supports UFID for flow operations.
1035 * Returns false if 'dpif' does not support UFID. */
1037 check_ufid(struct dpif_backer *backer)
1040 struct odputil_keybuf keybuf;
1044 struct odp_flow_key_parms odp_parms = {
1048 memset(&flow, 0, sizeof flow);
1049 flow.dl_type = htons(0x1234);
1051 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1052 odp_flow_key_from_flow(&odp_parms, &key);
1053 dpif_flow_hash(backer->dpif, key.data, key.size, &ufid);
1055 enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, &ufid);
1058 VLOG_INFO("%s: Datapath supports unique flow ids",
1059 dpif_name(backer->dpif));
1061 VLOG_INFO("%s: Datapath does not support unique flow ids",
1062 dpif_name(backer->dpif));
1067 /* Tests whether 'backer''s datapath supports variable-length
1068 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1069 * to disable some features on older datapaths that don't support this
1072 * Returns false if 'backer' definitely does not support variable-length
1073 * userdata, true if it seems to support them or if at least the error we get
1076 check_variable_length_userdata(struct dpif_backer *backer)
1078 struct eth_header *eth;
1079 struct ofpbuf actions;
1080 struct dpif_execute execute;
1081 struct dp_packet packet;
1085 /* Compose a userspace action that will cause an ERANGE error on older
1086 * datapaths that don't support variable-length userdata.
1088 * We really test for using userdata longer than 8 bytes, but older
1089 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1090 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1091 * test for that instead as a proxy for longer userdata support. */
1092 ofpbuf_init(&actions, 64);
1093 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1094 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1095 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1096 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1097 nl_msg_end_nested(&actions, start);
1099 /* Compose a dummy ethernet packet. */
1100 dp_packet_init(&packet, ETH_HEADER_LEN);
1101 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1102 eth->eth_type = htons(0x1234);
1104 /* Execute the actions. On older datapaths this fails with ERANGE, on
1105 * newer datapaths it succeeds. */
1106 execute.actions = actions.data;
1107 execute.actions_len = actions.size;
1108 execute.packet = &packet;
1109 execute.needs_help = false;
1110 execute.probe = true;
1112 error = dpif_execute(backer->dpif, &execute);
1114 dp_packet_uninit(&packet);
1115 ofpbuf_uninit(&actions);
1122 /* Variable-length userdata is not supported. */
1123 VLOG_WARN("%s: datapath does not support variable-length userdata "
1124 "feature (needs Linux 3.10+ or kernel module from OVS "
1125 "1..11+). The NXAST_SAMPLE action will be ignored.",
1126 dpif_name(backer->dpif));
1130 /* Something odd happened. We're not sure whether variable-length
1131 * userdata is supported. Default to "yes". */
1132 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1133 dpif_name(backer->dpif), ovs_strerror(error));
1138 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1140 * Returns the number of elements in a struct flow's mpls_lse field
1141 * if the datapath supports at least that many entries in an
1143 * Otherwise returns the number of MPLS push actions supported by
1146 check_max_mpls_depth(struct dpif_backer *backer)
1151 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1152 struct odputil_keybuf keybuf;
1154 struct odp_flow_key_parms odp_parms = {
1158 memset(&flow, 0, sizeof flow);
1159 flow.dl_type = htons(ETH_TYPE_MPLS);
1160 flow_set_mpls_bos(&flow, n, 1);
1162 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1163 odp_flow_key_from_flow(&odp_parms, &key);
1164 if (!dpif_probe_feature(backer->dpif, "MPLS", &key, NULL)) {
1169 VLOG_INFO("%s: MPLS label stack length probed as %d",
1170 dpif_name(backer->dpif), n);
1174 /* Tests whether 'backer''s datapath supports masked data in
1175 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1176 * datapaths that don't support this feature. */
1178 check_masked_set_action(struct dpif_backer *backer)
1180 struct eth_header *eth;
1181 struct ofpbuf actions;
1182 struct dpif_execute execute;
1183 struct dp_packet packet;
1185 struct ovs_key_ethernet key, mask;
1187 /* Compose a set action that will cause an EINVAL error on older
1188 * datapaths that don't support masked set actions.
1189 * Avoid using a full mask, as it could be translated to a non-masked
1190 * set action instead. */
1191 ofpbuf_init(&actions, 64);
1192 memset(&key, 0x53, sizeof key);
1193 memset(&mask, 0x7f, sizeof mask);
1194 commit_masked_set_action(&actions, OVS_KEY_ATTR_ETHERNET, &key, &mask,
1197 /* Compose a dummy ethernet packet. */
1198 dp_packet_init(&packet, ETH_HEADER_LEN);
1199 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1200 eth->eth_type = htons(0x1234);
1202 /* Execute the actions. On older datapaths this fails with EINVAL, on
1203 * newer datapaths it succeeds. */
1204 execute.actions = actions.data;
1205 execute.actions_len = actions.size;
1206 execute.packet = &packet;
1207 execute.needs_help = false;
1208 execute.probe = true;
1210 error = dpif_execute(backer->dpif, &execute);
1212 dp_packet_uninit(&packet);
1213 ofpbuf_uninit(&actions);
1216 /* Masked set action is not supported. */
1217 VLOG_INFO("%s: datapath does not support masked set action feature.",
1218 dpif_name(backer->dpif));
1224 check_support(struct dpif_backer *backer)
1226 /* This feature needs to be tested after udpif threads are set. */
1227 backer->support.variable_length_userdata = false;
1229 backer->support.recirc = check_recirc(backer);
1230 backer->support.max_mpls_depth = check_max_mpls_depth(backer);
1231 backer->support.masked_set_action = check_masked_set_action(backer);
1232 backer->support.ufid = check_ufid(backer);
1233 backer->support.tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
1237 construct(struct ofproto *ofproto_)
1239 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1240 struct shash_node *node, *next;
1243 /* Tunnel module can get used right after the udpif threads are running. */
1244 ofproto_tunnel_init();
1246 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1251 atomic_init(&ofproto->tables_version, CLS_MIN_VERSION);
1252 ofproto->netflow = NULL;
1253 ofproto->sflow = NULL;
1254 ofproto->ipfix = NULL;
1255 ofproto->stp = NULL;
1256 ofproto->rstp = NULL;
1257 ofproto->dump_seq = 0;
1258 hmap_init(&ofproto->bundles);
1259 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1261 ofproto->mbridge = mbridge_create();
1262 ofproto->has_bonded_bundles = false;
1263 ofproto->lacp_enabled = false;
1264 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1265 ovs_mutex_init(&ofproto->vsp_mutex);
1267 guarded_list_init(&ofproto->pins);
1269 ofproto_unixctl_init();
1271 hmap_init(&ofproto->vlandev_map);
1272 hmap_init(&ofproto->realdev_vid_map);
1274 sset_init(&ofproto->ports);
1275 sset_init(&ofproto->ghost_ports);
1276 sset_init(&ofproto->port_poll_set);
1277 ofproto->port_poll_errno = 0;
1278 ofproto->change_seq = 0;
1279 ofproto->pins_seq = seq_create();
1280 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1283 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1284 struct iface_hint *iface_hint = node->data;
1286 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1287 /* Check if the datapath already has this port. */
1288 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1289 sset_add(&ofproto->ports, node->name);
1292 free(iface_hint->br_name);
1293 free(iface_hint->br_type);
1295 shash_delete(&init_ofp_ports, node);
1299 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1300 hash_string(ofproto->up.name, 0));
1301 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1303 ofproto_init_tables(ofproto_, N_TABLES);
1304 error = add_internal_flows(ofproto);
1306 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1312 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1313 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1319 match_init_catchall(&match);
1320 match_set_reg(&match, 0, id);
1322 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1324 *rulep = error ? NULL : rule_dpif_cast(rule);
1330 add_internal_flows(struct ofproto_dpif *ofproto)
1332 struct ofpact_controller *controller;
1333 uint64_t ofpacts_stub[128 / 8];
1334 struct ofpbuf ofpacts;
1335 struct rule *unused_rulep OVS_UNUSED;
1340 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1343 controller = ofpact_put_CONTROLLER(&ofpacts);
1344 controller->max_len = UINT16_MAX;
1345 controller->controller_id = 0;
1346 controller->reason = OFPR_NO_MATCH;
1347 ofpact_pad(&ofpacts);
1349 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1350 &ofproto->miss_rule);
1355 ofpbuf_clear(&ofpacts);
1356 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1357 &ofproto->no_packet_in_rule);
1362 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1363 &ofproto->drop_frags_rule);
1368 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1369 * zero when reaching this rule.
1371 * (priority=2), recirc_id=0, actions=drop
1373 ofpbuf_clear(&ofpacts);
1374 match_init_catchall(&match);
1375 match_set_recirc_id(&match, 0);
1376 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1382 destruct(struct ofproto *ofproto_)
1384 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1385 struct ofproto_packet_in *pin;
1386 struct rule_dpif *rule;
1387 struct oftable *table;
1388 struct ovs_list pins;
1390 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1392 xlate_remove_ofproto(ofproto);
1395 /* Ensure that the upcall processing threads have no remaining references
1396 * to the ofproto or anything in it. */
1397 udpif_synchronize(ofproto->backer->udpif);
1399 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1401 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1402 CLS_FOR_EACH (rule, up.cr, &table->cls) {
1403 ofproto_rule_delete(&ofproto->up, &rule->up);
1407 guarded_list_pop_all(&ofproto->pins, &pins);
1408 LIST_FOR_EACH_POP (pin, list_node, &pins) {
1409 free(CONST_CAST(void *, pin->up.packet));
1412 guarded_list_destroy(&ofproto->pins);
1414 recirc_free_ofproto(ofproto, ofproto->up.name);
1416 mbridge_unref(ofproto->mbridge);
1418 netflow_unref(ofproto->netflow);
1419 dpif_sflow_unref(ofproto->sflow);
1420 dpif_ipfix_unref(ofproto->ipfix);
1421 hmap_destroy(&ofproto->bundles);
1422 mac_learning_unref(ofproto->ml);
1423 mcast_snooping_unref(ofproto->ms);
1425 hmap_destroy(&ofproto->vlandev_map);
1426 hmap_destroy(&ofproto->realdev_vid_map);
1428 sset_destroy(&ofproto->ports);
1429 sset_destroy(&ofproto->ghost_ports);
1430 sset_destroy(&ofproto->port_poll_set);
1432 ovs_mutex_destroy(&ofproto->stats_mutex);
1433 ovs_mutex_destroy(&ofproto->vsp_mutex);
1435 seq_destroy(ofproto->pins_seq);
1437 close_dpif_backer(ofproto->backer);
1441 run(struct ofproto *ofproto_)
1443 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1444 uint64_t new_seq, new_dump_seq;
1446 if (mbridge_need_revalidate(ofproto->mbridge)) {
1447 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1448 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1449 mac_learning_flush(ofproto->ml);
1450 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1451 mcast_snooping_mdb_flush(ofproto->ms);
1454 /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1455 * flow restore. Even though nothing is processed during flow restore,
1456 * all queued 'pins' will be handled immediately when flow restore
1458 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1460 /* Do not perform any periodic activity required by 'ofproto' while
1461 * waiting for flow restore to complete. */
1462 if (!ofproto_get_flow_restore_wait()) {
1463 struct ofproto_packet_in *pin;
1464 struct ovs_list pins;
1466 guarded_list_pop_all(&ofproto->pins, &pins);
1467 LIST_FOR_EACH_POP (pin, list_node, &pins) {
1468 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1469 free(CONST_CAST(void *, pin->up.packet));
1474 if (ofproto->netflow) {
1475 netflow_run(ofproto->netflow);
1477 if (ofproto->sflow) {
1478 dpif_sflow_run(ofproto->sflow);
1480 if (ofproto->ipfix) {
1481 dpif_ipfix_run(ofproto->ipfix);
1484 new_seq = seq_read(connectivity_seq_get());
1485 if (ofproto->change_seq != new_seq) {
1486 struct ofport_dpif *ofport;
1488 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1492 ofproto->change_seq = new_seq;
1494 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1495 struct ofbundle *bundle;
1497 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1504 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1505 if (mac_learning_run(ofproto->ml)) {
1506 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1508 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1510 if (mcast_snooping_run(ofproto->ms)) {
1511 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1514 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1515 if (ofproto->dump_seq != new_dump_seq) {
1516 struct rule *rule, *next_rule;
1518 /* We know stats are relatively fresh, so now is a good time to do some
1520 ofproto->dump_seq = new_dump_seq;
1522 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1524 ovs_mutex_lock(&ofproto_mutex);
1525 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1526 &ofproto->up.expirable) {
1527 rule_expire(rule_dpif_cast(rule));
1529 ovs_mutex_unlock(&ofproto_mutex);
1531 /* All outstanding data in existing flows has been accounted, so it's a
1532 * good time to do bond rebalancing. */
1533 if (ofproto->has_bonded_bundles) {
1534 struct ofbundle *bundle;
1536 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1538 bond_rebalance(bundle->bond);
1547 wait(struct ofproto *ofproto_)
1549 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1551 if (ofproto_get_flow_restore_wait()) {
1555 if (ofproto->sflow) {
1556 dpif_sflow_wait(ofproto->sflow);
1558 if (ofproto->ipfix) {
1559 dpif_ipfix_wait(ofproto->ipfix);
1561 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1562 struct ofbundle *bundle;
1564 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1565 bundle_wait(bundle);
1568 if (ofproto->netflow) {
1569 netflow_wait(ofproto->netflow);
1571 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1572 mac_learning_wait(ofproto->ml);
1573 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1574 mcast_snooping_wait(ofproto->ms);
1576 if (ofproto->backer->need_revalidate) {
1577 /* Shouldn't happen, but if it does just go around again. */
1578 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1579 poll_immediate_wake();
1582 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1583 seq_wait(ofproto->pins_seq, ofproto->pins_seqno);
1587 type_get_memory_usage(const char *type, struct simap *usage)
1589 struct dpif_backer *backer;
1591 backer = shash_find_data(&all_dpif_backers, type);
1593 udpif_get_memory_usage(backer->udpif, usage);
1598 flush(struct ofproto *ofproto_)
1600 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1601 struct dpif_backer *backer = ofproto->backer;
1604 udpif_flush(backer->udpif);
1609 query_tables(struct ofproto *ofproto,
1610 struct ofputil_table_features *features,
1611 struct ofputil_table_stats *stats)
1613 strcpy(features->name, "classifier");
1618 for (i = 0; i < ofproto->n_tables; i++) {
1619 unsigned long missed, matched;
1621 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1622 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1624 stats[i].matched_count = matched;
1625 stats[i].lookup_count = matched + missed;
1631 set_tables_version(struct ofproto *ofproto_, cls_version_t version)
1633 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1635 atomic_store_relaxed(&ofproto->tables_version, version);
1639 static struct ofport *
1642 struct ofport_dpif *port = xzalloc(sizeof *port);
1647 port_dealloc(struct ofport *port_)
1649 struct ofport_dpif *port = ofport_dpif_cast(port_);
1654 port_construct(struct ofport *port_)
1656 struct ofport_dpif *port = ofport_dpif_cast(port_);
1657 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1658 const struct netdev *netdev = port->up.netdev;
1659 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1660 const char *dp_port_name;
1661 struct dpif_port dpif_port;
1664 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1665 port->bundle = NULL;
1669 port->may_enable = false;
1670 port->stp_port = NULL;
1671 port->stp_state = STP_DISABLED;
1672 port->rstp_port = NULL;
1673 port->rstp_state = RSTP_DISABLED;
1674 port->is_tunnel = false;
1678 port->realdev_ofp_port = 0;
1679 port->vlandev_vid = 0;
1680 port->carrier_seq = netdev_get_carrier_resets(netdev);
1681 port->is_layer3 = netdev_vport_is_layer3(netdev);
1683 if (netdev_vport_is_patch(netdev)) {
1684 /* By bailing out here, we don't submit the port to the sFlow module
1685 * to be considered for counter polling export. This is correct
1686 * because the patch port represents an interface that sFlow considers
1687 * to be "internal" to the switch as a whole, and therefore not a
1688 * candidate for counter polling. */
1689 port->odp_port = ODPP_NONE;
1690 ofport_update_peer(port);
1694 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1695 error = dpif_port_query_by_name(ofproto->backer->dpif, dp_port_name,
1701 port->odp_port = dpif_port.port_no;
1703 if (netdev_get_tunnel_config(netdev)) {
1704 atomic_count_inc(&ofproto->backer->tnl_count);
1705 error = tnl_port_add(port, port->up.netdev, port->odp_port,
1706 ovs_native_tunneling_is_on(ofproto), dp_port_name);
1708 atomic_count_dec(&ofproto->backer->tnl_count);
1709 dpif_port_destroy(&dpif_port);
1713 port->is_tunnel = true;
1714 if (ofproto->ipfix) {
1715 dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1718 /* Sanity-check that a mapping doesn't already exist. This
1719 * shouldn't happen for non-tunnel ports. */
1720 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1721 VLOG_ERR("port %s already has an OpenFlow port number",
1723 dpif_port_destroy(&dpif_port);
1727 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1728 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1729 hash_odp_port(port->odp_port));
1730 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1732 dpif_port_destroy(&dpif_port);
1734 if (ofproto->sflow) {
1735 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1742 port_destruct(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 char *devname = netdev_get_name(port->up.netdev);
1747 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1748 const char *dp_port_name;
1750 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1752 xlate_ofport_remove(port);
1755 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1757 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1758 /* The underlying device is still there, so delete it. This
1759 * happens when the ofproto is being destroyed, since the caller
1760 * assumes that removal of attached ports will happen as part of
1762 if (!port->is_tunnel) {
1763 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1768 port->peer->peer = NULL;
1772 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1773 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1774 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1775 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1778 if (port->is_tunnel) {
1779 atomic_count_dec(&ofproto->backer->tnl_count);
1782 if (port->is_tunnel && ofproto->ipfix) {
1783 dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1787 sset_find_and_delete(&ofproto->ports, devname);
1788 sset_find_and_delete(&ofproto->ghost_ports, devname);
1789 bundle_remove(port_);
1790 set_cfm(port_, NULL);
1791 set_bfd(port_, NULL);
1792 set_lldp(port_, NULL);
1793 if (port->stp_port) {
1794 stp_port_disable(port->stp_port);
1796 set_rstp_port(port_, NULL);
1797 if (ofproto->sflow) {
1798 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1805 port_modified(struct ofport *port_)
1807 struct ofport_dpif *port = ofport_dpif_cast(port_);
1808 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1809 const char *dp_port_name;
1810 struct netdev *netdev = port->up.netdev;
1812 if (port->bundle && port->bundle->bond) {
1813 bond_slave_set_netdev(port->bundle->bond, port, netdev);
1817 cfm_set_netdev(port->cfm, netdev);
1821 bfd_set_netdev(port->bfd, netdev);
1824 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1825 port->lldp, port->up.pp.hw_addr);
1827 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1829 if (port->is_tunnel) {
1830 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1832 if (tnl_port_reconfigure(port, netdev, port->odp_port,
1833 ovs_native_tunneling_is_on(ofproto),
1835 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1839 ofport_update_peer(port);
1843 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1845 struct ofport_dpif *port = ofport_dpif_cast(port_);
1846 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1847 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1849 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1850 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1851 OFPUTIL_PC_NO_PACKET_IN)) {
1852 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1854 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1855 bundle_update(port->bundle);
1861 set_sflow(struct ofproto *ofproto_,
1862 const struct ofproto_sflow_options *sflow_options)
1864 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1865 struct dpif_sflow *ds = ofproto->sflow;
1867 if (sflow_options) {
1868 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
1870 struct ofport_dpif *ofport;
1872 ds = ofproto->sflow = dpif_sflow_create();
1873 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1874 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1877 dpif_sflow_set_options(ds, sflow_options);
1878 if (dpif_sflow_get_probability(ds) != old_probability) {
1879 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1883 dpif_sflow_unref(ds);
1884 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1885 ofproto->sflow = NULL;
1893 struct ofproto *ofproto_,
1894 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1895 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1896 size_t n_flow_exporters_options)
1898 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1899 struct dpif_ipfix *di = ofproto->ipfix;
1900 bool has_options = bridge_exporter_options || flow_exporters_options;
1901 bool new_di = false;
1903 if (has_options && !di) {
1904 di = ofproto->ipfix = dpif_ipfix_create();
1909 /* Call set_options in any case to cleanly flush the flow
1910 * caches in the last exporters that are to be destroyed. */
1911 dpif_ipfix_set_options(
1912 di, bridge_exporter_options, flow_exporters_options,
1913 n_flow_exporters_options);
1915 /* Add tunnel ports only when a new ipfix created */
1916 if (new_di == true) {
1917 struct ofport_dpif *ofport;
1918 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1919 if (ofport->is_tunnel == true) {
1920 dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
1926 dpif_ipfix_unref(di);
1927 ofproto->ipfix = NULL;
1935 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1937 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1938 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1939 struct cfm *old = ofport->cfm;
1944 ofport->cfm = cfm_create(ofport->up.netdev);
1947 if (cfm_configure(ofport->cfm, s)) {
1954 cfm_unref(ofport->cfm);
1957 if (ofport->cfm != old) {
1958 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1960 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1961 ofport->lldp, ofport->up.pp.hw_addr);
1966 cfm_status_changed(struct ofport *ofport_)
1968 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1970 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
1974 get_cfm_status(const struct ofport *ofport_,
1975 struct cfm_status *status)
1977 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1981 cfm_get_status(ofport->cfm, status);
1990 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1992 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1993 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1997 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1998 cfg, ofport->up.netdev);
1999 if (ofport->bfd != old) {
2000 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2002 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2003 ofport->lldp, ofport->up.pp.hw_addr);
2008 bfd_status_changed(struct ofport *ofport_)
2010 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2012 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2016 get_bfd_status(struct ofport *ofport_, struct smap *smap)
2018 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2022 bfd_get_status(ofport->bfd, smap);
2031 set_lldp(struct ofport *ofport_,
2032 const struct smap *cfg)
2034 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2038 if (!ofport->lldp) {
2039 struct ofproto_dpif *ofproto;
2041 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2042 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2043 ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2046 if (!lldp_configure(ofport->lldp, cfg)) {
2051 lldp_unref(ofport->lldp);
2052 ofport->lldp = NULL;
2055 ofproto_dpif_monitor_port_update(ofport,
2059 ofport->up.pp.hw_addr);
2064 get_lldp_status(const struct ofport *ofport_,
2065 struct lldp_status *status OVS_UNUSED)
2067 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2069 return ofport->lldp ? true : false;
2073 set_aa(struct ofproto *ofproto OVS_UNUSED,
2074 const struct aa_settings *s)
2076 return aa_configure(s);
2080 aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2081 const struct aa_mapping_settings *s)
2083 return aa_mapping_register(aux, s);
2087 aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2089 return aa_mapping_unregister(aux);
2093 aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2095 return aa_get_vlan_queued(list);
2099 aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2101 return aa_get_vlan_queue_size();
2105 /* Spanning Tree. */
2107 /* Called while rstp_mutex is held. */
2109 rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
2111 struct ofproto_dpif *ofproto = ofproto_;
2112 struct ofport_dpif *ofport = ofport_;
2113 struct eth_header *eth = dp_packet_l2(pkt);
2115 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2116 if (eth_addr_is_zero(eth->eth_src)) {
2117 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2118 "does not have a configured source MAC address.",
2119 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
2121 ofproto_dpif_send_packet(ofport, pkt);
2123 dp_packet_delete(pkt);
2127 send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
2129 struct ofproto_dpif *ofproto = ofproto_;
2130 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2131 struct ofport_dpif *ofport;
2133 ofport = stp_port_get_aux(sp);
2135 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2136 ofproto->up.name, port_num);
2138 struct eth_header *eth = dp_packet_l2(pkt);
2140 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2141 if (eth_addr_is_zero(eth->eth_src)) {
2142 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2143 "with unknown MAC", ofproto->up.name, port_num);
2145 ofproto_dpif_send_packet(ofport, pkt);
2148 dp_packet_delete(pkt);
2151 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2153 set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2155 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2157 /* Only revalidate flows if the configuration changed. */
2158 if (!s != !ofproto->rstp) {
2159 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2163 if (!ofproto->rstp) {
2164 ofproto->rstp = rstp_create(ofproto_->name, s->address,
2165 rstp_send_bpdu_cb, ofproto);
2166 ofproto->rstp_last_tick = time_msec();
2168 rstp_set_bridge_address(ofproto->rstp, s->address);
2169 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2170 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2171 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2172 s->force_protocol_version);
2173 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2174 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2175 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2176 s->transmit_hold_count);
2178 struct ofport *ofport;
2179 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2180 set_rstp_port(ofport, NULL);
2182 rstp_unref(ofproto->rstp);
2183 ofproto->rstp = NULL;
2188 get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2190 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2192 if (ofproto->rstp) {
2194 s->root_id = rstp_get_root_id(ofproto->rstp);
2195 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2196 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2197 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2198 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2199 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2206 update_rstp_port_state(struct ofport_dpif *ofport)
2208 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2209 enum rstp_state state;
2211 /* Figure out new state. */
2212 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2216 if (ofport->rstp_state != state) {
2217 enum ofputil_port_state of_state;
2220 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2221 netdev_get_name(ofport->up.netdev),
2222 rstp_state_name(ofport->rstp_state),
2223 rstp_state_name(state));
2225 if (rstp_learn_in_state(ofport->rstp_state)
2226 != rstp_learn_in_state(state)) {
2227 /* XXX: Learning action flows should also be flushed. */
2228 if (ofport->bundle) {
2229 if (!rstp_shift_root_learned_address(ofproto->rstp)
2230 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2231 bundle_flush_macs(ofport->bundle, false);
2235 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2236 != rstp_forward_in_state(state);
2238 ofproto->backer->need_revalidate = REV_RSTP;
2239 ofport->rstp_state = state;
2241 if (fwd_change && ofport->bundle) {
2242 bundle_update(ofport->bundle);
2245 /* Update the RSTP state bits in the OpenFlow port description. */
2246 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2247 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2248 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2249 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2251 ofproto_port_set_state(&ofport->up, of_state);
2256 rstp_run(struct ofproto_dpif *ofproto)
2258 if (ofproto->rstp) {
2259 long long int now = time_msec();
2260 long long int elapsed = now - ofproto->rstp_last_tick;
2261 struct rstp_port *rp;
2262 struct ofport_dpif *ofport;
2264 /* Every second, decrease the values of the timers. */
2265 if (elapsed >= 1000) {
2266 rstp_tick_timers(ofproto->rstp);
2267 ofproto->rstp_last_tick = now;
2270 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2271 update_rstp_port_state(ofport);
2275 /* FIXME: This check should be done on-event (i.e., when setting
2276 * p->fdb_flush) and not periodically.
2278 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2279 if (!rstp_shift_root_learned_address(ofproto->rstp)
2280 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2281 bundle_flush_macs(ofport->bundle, false);
2285 if (rstp_shift_root_learned_address(ofproto->rstp)) {
2286 bundle_move(((struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp))->bundle,
2287 ((struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp))->bundle);
2288 rstp_reset_root_changed(ofproto->rstp);
2293 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2295 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2297 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2299 /* Only revalidate flows if the configuration changed. */
2300 if (!s != !ofproto->stp) {
2301 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2305 if (!ofproto->stp) {
2306 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2307 send_bpdu_cb, ofproto);
2308 ofproto->stp_last_tick = time_msec();
2311 stp_set_bridge_id(ofproto->stp, s->system_id);
2312 stp_set_bridge_priority(ofproto->stp, s->priority);
2313 stp_set_hello_time(ofproto->stp, s->hello_time);
2314 stp_set_max_age(ofproto->stp, s->max_age);
2315 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2317 struct ofport *ofport;
2319 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2320 set_stp_port(ofport, NULL);
2323 stp_unref(ofproto->stp);
2324 ofproto->stp = NULL;
2331 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2333 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2337 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2338 s->designated_root = stp_get_designated_root(ofproto->stp);
2339 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2348 update_stp_port_state(struct ofport_dpif *ofport)
2350 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2351 enum stp_state state;
2353 /* Figure out new state. */
2354 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2358 if (ofport->stp_state != state) {
2359 enum ofputil_port_state of_state;
2362 VLOG_DBG("port %s: STP state changed from %s to %s",
2363 netdev_get_name(ofport->up.netdev),
2364 stp_state_name(ofport->stp_state),
2365 stp_state_name(state));
2366 if (stp_learn_in_state(ofport->stp_state)
2367 != stp_learn_in_state(state)) {
2368 /* xxx Learning action flows should also be flushed. */
2369 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2370 mac_learning_flush(ofproto->ml);
2371 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2372 mcast_snooping_mdb_flush(ofproto->ms);
2374 fwd_change = stp_forward_in_state(ofport->stp_state)
2375 != stp_forward_in_state(state);
2377 ofproto->backer->need_revalidate = REV_STP;
2378 ofport->stp_state = state;
2379 ofport->stp_state_entered = time_msec();
2381 if (fwd_change && ofport->bundle) {
2382 bundle_update(ofport->bundle);
2385 /* Update the STP state bits in the OpenFlow port description. */
2386 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2387 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2388 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2389 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2390 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2392 ofproto_port_set_state(&ofport->up, of_state);
2396 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2397 * caller is responsible for assigning STP port numbers and ensuring
2398 * there are no duplicates. */
2400 set_stp_port(struct ofport *ofport_,
2401 const struct ofproto_port_stp_settings *s)
2403 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2404 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2405 struct stp_port *sp = ofport->stp_port;
2407 if (!s || !s->enable) {
2409 ofport->stp_port = NULL;
2410 stp_port_disable(sp);
2411 update_stp_port_state(ofport);
2414 } else if (sp && stp_port_no(sp) != s->port_num
2415 && ofport == stp_port_get_aux(sp)) {
2416 /* The port-id changed, so disable the old one if it's not
2417 * already in use by another port. */
2418 stp_port_disable(sp);
2421 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2423 /* Set name before enabling the port so that debugging messages can print
2425 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2426 stp_port_enable(sp);
2428 stp_port_set_aux(sp, ofport);
2429 stp_port_set_priority(sp, s->priority);
2430 stp_port_set_path_cost(sp, s->path_cost);
2432 update_stp_port_state(ofport);
2438 get_stp_port_status(struct ofport *ofport_,
2439 struct ofproto_port_stp_status *s)
2441 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2442 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2443 struct stp_port *sp = ofport->stp_port;
2445 if (!ofproto->stp || !sp) {
2451 s->port_id = stp_port_get_id(sp);
2452 s->state = stp_port_get_state(sp);
2453 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2454 s->role = stp_port_get_role(sp);
2460 get_stp_port_stats(struct ofport *ofport_,
2461 struct ofproto_port_stp_stats *s)
2463 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2464 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2465 struct stp_port *sp = ofport->stp_port;
2467 if (!ofproto->stp || !sp) {
2473 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2479 stp_run(struct ofproto_dpif *ofproto)
2482 long long int now = time_msec();
2483 long long int elapsed = now - ofproto->stp_last_tick;
2484 struct stp_port *sp;
2487 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2488 ofproto->stp_last_tick = now;
2490 while (stp_get_changed_port(ofproto->stp, &sp)) {
2491 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2494 update_stp_port_state(ofport);
2498 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2499 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2500 mac_learning_flush(ofproto->ml);
2501 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2502 mcast_snooping_mdb_flush(ofproto->ms);
2508 stp_wait(struct ofproto_dpif *ofproto)
2511 poll_timer_wait(1000);
2515 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2516 * caller is responsible for assigning RSTP port numbers and ensuring
2517 * there are no duplicates. */
2519 set_rstp_port(struct ofport *ofport_,
2520 const struct ofproto_port_rstp_settings *s)
2522 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2523 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2524 struct rstp_port *rp = ofport->rstp_port;
2526 if (!s || !s->enable) {
2528 rstp_port_unref(rp);
2529 ofport->rstp_port = NULL;
2530 update_rstp_port_state(ofport);
2535 /* Check if need to add a new port. */
2537 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2540 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
2541 s->admin_edge_port, s->auto_edge,
2542 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2544 update_rstp_port_state(ofport);
2545 /* Synchronize operational status. */
2546 rstp_port_set_mac_operational(rp, ofport->may_enable);
2550 get_rstp_port_status(struct ofport *ofport_,
2551 struct ofproto_port_rstp_status *s)
2553 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2554 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2555 struct rstp_port *rp = ofport->rstp_port;
2557 if (!ofproto->rstp || !rp) {
2563 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2564 &s->designated_bridge_id, &s->designated_port_id,
2565 &s->designated_path_cost, &s->tx_count,
2566 &s->rx_count, &s->error_count, &s->uptime);
2571 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2574 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2575 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2577 if (ofport->n_qdscp != n_qdscp
2578 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2579 n_qdscp * sizeof *qdscp))) {
2580 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2581 free(ofport->qdscp);
2582 ofport->qdscp = n_qdscp
2583 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2585 ofport->n_qdscp = n_qdscp;
2593 /* Expires all MAC learning entries associated with 'bundle' and forces its
2594 * ofproto to revalidate every flow.
2596 * Normally MAC learning entries are removed only from the ofproto associated
2597 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2598 * are removed from every ofproto. When patch ports and SLB bonds are in use
2599 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2600 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2601 * with the host from which it migrated. */
2603 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2605 struct ofproto_dpif *ofproto = bundle->ofproto;
2606 struct mac_learning *ml = ofproto->ml;
2607 struct mac_entry *mac, *next_mac;
2609 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2610 ovs_rwlock_wrlock(&ml->rwlock);
2611 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2612 if (mac_entry_get_port(ml, mac) == bundle) {
2614 struct ofproto_dpif *o;
2616 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2618 struct mac_entry *e;
2620 ovs_rwlock_wrlock(&o->ml->rwlock);
2621 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2623 mac_learning_expire(o->ml, e);
2625 ovs_rwlock_unlock(&o->ml->rwlock);
2630 mac_learning_expire(ml, mac);
2633 ovs_rwlock_unlock(&ml->rwlock);
2637 bundle_move(struct ofbundle *old, struct ofbundle *new)
2639 struct ofproto_dpif *ofproto = old->ofproto;
2640 struct mac_learning *ml = ofproto->ml;
2641 struct mac_entry *mac, *next_mac;
2643 ovs_assert(new->ofproto == old->ofproto);
2645 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2646 ovs_rwlock_wrlock(&ml->rwlock);
2647 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2648 if (mac_entry_get_port(ml, mac) == old) {
2649 mac_entry_set_port(ml, mac, new);
2652 ovs_rwlock_unlock(&ml->rwlock);
2655 static struct ofbundle *
2656 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2658 struct ofbundle *bundle;
2660 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2661 &ofproto->bundles) {
2662 if (bundle->aux == aux) {
2670 bundle_update(struct ofbundle *bundle)
2672 struct ofport_dpif *port;
2674 bundle->floodable = true;
2675 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2676 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2678 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2679 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2680 bundle->floodable = false;
2687 bundle_del_port(struct ofport_dpif *port)
2689 struct ofbundle *bundle = port->bundle;
2691 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2693 list_remove(&port->bundle_node);
2694 port->bundle = NULL;
2697 lacp_slave_unregister(bundle->lacp, port);
2700 bond_slave_unregister(bundle->bond, port);
2703 bundle_update(bundle);
2707 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2708 struct lacp_slave_settings *lacp)
2710 struct ofport_dpif *port;
2712 port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
2717 if (port->bundle != bundle) {
2718 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2720 bundle_remove(&port->up);
2723 port->bundle = bundle;
2724 list_push_back(&bundle->ports, &port->bundle_node);
2725 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2727 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2728 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2729 bundle->floodable = false;
2733 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2734 lacp_slave_register(bundle->lacp, port, lacp);
2741 bundle_destroy(struct ofbundle *bundle)
2743 struct ofproto_dpif *ofproto;
2744 struct ofport_dpif *port, *next_port;
2750 ofproto = bundle->ofproto;
2751 mbridge_unregister_bundle(ofproto->mbridge, bundle);
2754 xlate_bundle_remove(bundle);
2757 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2758 bundle_del_port(port);
2761 bundle_flush_macs(bundle, true);
2762 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2764 free(bundle->trunks);
2765 lacp_unref(bundle->lacp);
2766 bond_unref(bundle->bond);
2771 bundle_set(struct ofproto *ofproto_, void *aux,
2772 const struct ofproto_bundle_settings *s)
2774 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2775 bool need_flush = false;
2776 struct ofport_dpif *port;
2777 struct ofbundle *bundle;
2778 unsigned long *trunks;
2784 bundle_destroy(bundle_lookup(ofproto, aux));
2788 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2789 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2791 bundle = bundle_lookup(ofproto, aux);
2793 bundle = xmalloc(sizeof *bundle);
2795 bundle->ofproto = ofproto;
2796 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2797 hash_pointer(aux, 0));
2799 bundle->name = NULL;
2801 list_init(&bundle->ports);
2802 bundle->vlan_mode = PORT_VLAN_TRUNK;
2804 bundle->trunks = NULL;
2805 bundle->use_priority_tags = s->use_priority_tags;
2806 bundle->lacp = NULL;
2807 bundle->bond = NULL;
2809 bundle->floodable = true;
2810 mbridge_register_bundle(ofproto->mbridge, bundle);
2813 if (!bundle->name || strcmp(s->name, bundle->name)) {
2815 bundle->name = xstrdup(s->name);
2820 ofproto->lacp_enabled = true;
2821 if (!bundle->lacp) {
2822 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2823 bundle->lacp = lacp_create();
2825 lacp_configure(bundle->lacp, s->lacp);
2827 lacp_unref(bundle->lacp);
2828 bundle->lacp = NULL;
2831 /* Update set of ports. */
2833 for (i = 0; i < s->n_slaves; i++) {
2834 if (!bundle_add_port(bundle, s->slaves[i],
2835 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2839 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2840 struct ofport_dpif *next_port;
2842 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2843 for (i = 0; i < s->n_slaves; i++) {
2844 if (s->slaves[i] == port->up.ofp_port) {
2849 bundle_del_port(port);
2853 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2855 if (list_is_empty(&bundle->ports)) {
2856 bundle_destroy(bundle);
2860 /* Set VLAN tagging mode */
2861 if (s->vlan_mode != bundle->vlan_mode
2862 || s->use_priority_tags != bundle->use_priority_tags) {
2863 bundle->vlan_mode = s->vlan_mode;
2864 bundle->use_priority_tags = s->use_priority_tags;
2869 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2870 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2872 if (vlan != bundle->vlan) {
2873 bundle->vlan = vlan;
2877 /* Get trunked VLANs. */
2878 switch (s->vlan_mode) {
2879 case PORT_VLAN_ACCESS:
2883 case PORT_VLAN_TRUNK:
2884 trunks = CONST_CAST(unsigned long *, s->trunks);
2887 case PORT_VLAN_NATIVE_UNTAGGED:
2888 case PORT_VLAN_NATIVE_TAGGED:
2889 if (vlan != 0 && (!s->trunks
2890 || !bitmap_is_set(s->trunks, vlan)
2891 || bitmap_is_set(s->trunks, 0))) {
2892 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2894 trunks = bitmap_clone(s->trunks, 4096);
2896 trunks = bitmap_allocate1(4096);
2898 bitmap_set1(trunks, vlan);
2899 bitmap_set0(trunks, 0);
2901 trunks = CONST_CAST(unsigned long *, s->trunks);
2908 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2909 free(bundle->trunks);
2910 if (trunks == s->trunks) {
2911 bundle->trunks = vlan_bitmap_clone(trunks);
2913 bundle->trunks = trunks;
2918 if (trunks != s->trunks) {
2923 if (!list_is_short(&bundle->ports)) {
2924 bundle->ofproto->has_bonded_bundles = true;
2926 if (bond_reconfigure(bundle->bond, s->bond)) {
2927 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2930 bundle->bond = bond_create(s->bond, ofproto);
2931 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2934 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2935 bond_slave_register(bundle->bond, port,
2936 port->up.ofp_port, port->up.netdev);
2939 bond_unref(bundle->bond);
2940 bundle->bond = NULL;
2943 /* If we changed something that would affect MAC learning, un-learn
2944 * everything on this port and force flow revalidation. */
2946 bundle_flush_macs(bundle, false);
2953 bundle_remove(struct ofport *port_)
2955 struct ofport_dpif *port = ofport_dpif_cast(port_);
2956 struct ofbundle *bundle = port->bundle;
2959 bundle_del_port(port);
2960 if (list_is_empty(&bundle->ports)) {
2961 bundle_destroy(bundle);
2962 } else if (list_is_short(&bundle->ports)) {
2963 bond_unref(bundle->bond);
2964 bundle->bond = NULL;
2970 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2972 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2973 struct ofport_dpif *port = port_;
2974 uint8_t ea[ETH_ADDR_LEN];
2977 error = netdev_get_etheraddr(port->up.netdev, ea);
2979 struct dp_packet packet;
2982 dp_packet_init(&packet, 0);
2983 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2985 memcpy(packet_pdu, pdu, pdu_size);
2987 ofproto_dpif_send_packet(port, &packet);
2988 dp_packet_uninit(&packet);
2990 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2991 "%s (%s)", port->bundle->name,
2992 netdev_get_name(port->up.netdev), ovs_strerror(error));
2997 bundle_send_learning_packets(struct ofbundle *bundle)
2999 struct ofproto_dpif *ofproto = bundle->ofproto;
3000 int error, n_packets, n_errors;
3001 struct mac_entry *e;
3003 struct ovs_list list_node;
3004 struct ofport_dpif *port;
3005 struct dp_packet *pkt;
3007 struct ovs_list packets;
3009 list_init(&packets);
3010 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3011 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3012 if (mac_entry_get_port(ofproto->ml, e) != bundle) {
3013 pkt_node = xmalloc(sizeof *pkt_node);
3014 pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
3016 (void **)&pkt_node->port);
3017 list_push_back(&packets, &pkt_node->list_node);
3020 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3022 error = n_packets = n_errors = 0;
3023 LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
3026 ret = ofproto_dpif_send_packet(pkt_node->port, pkt_node->pkt);
3027 dp_packet_delete(pkt_node->pkt);
3037 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3038 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3039 "packets, last error was: %s",
3040 bundle->name, n_errors, n_packets, ovs_strerror(error));
3042 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3043 bundle->name, n_packets);
3048 bundle_run(struct ofbundle *bundle)
3051 lacp_run(bundle->lacp, send_pdu_cb);
3054 struct ofport_dpif *port;
3056 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3057 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
3060 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3061 bundle->ofproto->backer->need_revalidate = REV_BOND;
3064 if (bond_should_send_learning_packets(bundle->bond)) {
3065 bundle_send_learning_packets(bundle);
3071 bundle_wait(struct ofbundle *bundle)
3074 lacp_wait(bundle->lacp);
3077 bond_wait(bundle->bond);
3084 mirror_set__(struct ofproto *ofproto_, void *aux,
3085 const struct ofproto_mirror_settings *s)
3087 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3088 struct ofbundle **srcs, **dsts;
3093 mirror_destroy(ofproto->mbridge, aux);
3097 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3098 dsts = xmalloc(s->n_dsts * sizeof *dsts);
3100 for (i = 0; i < s->n_srcs; i++) {
3101 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
3104 for (i = 0; i < s->n_dsts; i++) {
3105 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
3108 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3109 s->n_dsts, s->src_vlans,
3110 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
3117 mirror_get_stats__(struct ofproto *ofproto, void *aux,
3118 uint64_t *packets, uint64_t *bytes)
3120 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3125 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3127 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3128 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3129 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
3130 mac_learning_flush(ofproto->ml);
3132 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3137 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
3139 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3140 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3141 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
3145 forward_bpdu_changed(struct ofproto *ofproto_)
3147 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3148 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3152 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3155 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3156 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3157 mac_learning_set_idle_time(ofproto->ml, idle_time);
3158 mac_learning_set_max_entries(ofproto->ml, max_entries);
3159 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3162 /* Configures multicast snooping on 'ofport' using the settings
3163 * defined in 's'. */
3165 set_mcast_snooping(struct ofproto *ofproto_,
3166 const struct ofproto_mcast_snooping_settings *s)
3168 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3170 /* Only revalidate flows if the configuration changed. */
3171 if (!s != !ofproto->ms) {
3172 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3177 ofproto->ms = mcast_snooping_create();
3180 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3181 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3182 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3183 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3184 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3186 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3188 mcast_snooping_unref(ofproto->ms);
3195 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3197 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3198 const struct ofproto_mcast_snooping_port_settings *s)
3200 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3201 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3203 if (ofproto->ms && s) {
3204 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3205 mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3206 mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3208 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3216 struct ofport_dpif *
3217 ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
3219 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3220 return ofport ? ofport_dpif_cast(ofport) : NULL;
3224 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3225 struct ofproto_port *ofproto_port,
3226 struct dpif_port *dpif_port)
3228 ofproto_port->name = dpif_port->name;
3229 ofproto_port->type = dpif_port->type;
3230 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3234 ofport_update_peer(struct ofport_dpif *ofport)
3236 const struct ofproto_dpif *ofproto;
3237 struct dpif_backer *backer;
3240 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3244 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
3245 backer->need_revalidate = REV_RECONFIGURE;
3248 ofport->peer->peer = NULL;
3249 ofport->peer = NULL;
3252 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3257 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3258 struct ofport *peer_ofport;
3259 struct ofport_dpif *peer;
3262 if (ofproto->backer != backer) {
3266 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3271 peer = ofport_dpif_cast(peer_ofport);
3272 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3273 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3275 ofport->peer = peer;
3276 ofport->peer->peer = ofport;
3286 port_run(struct ofport_dpif *ofport)
3288 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3289 bool carrier_changed = carrier_seq != ofport->carrier_seq;
3290 bool enable = netdev_get_carrier(ofport->up.netdev);
3291 bool cfm_enable = false;
3292 bool bfd_enable = false;
3294 ofport->carrier_seq = carrier_seq;
3297 int cfm_opup = cfm_get_opup(ofport->cfm);
3299 cfm_enable = !cfm_get_fault(ofport->cfm);
3301 if (cfm_opup >= 0) {
3302 cfm_enable = cfm_enable && cfm_opup;
3307 bfd_enable = bfd_forwarding(ofport->bfd);
3310 if (ofport->bfd || ofport->cfm) {
3311 enable = enable && (cfm_enable || bfd_enable);
3314 if (ofport->bundle) {
3315 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3316 if (carrier_changed) {
3317 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3321 if (ofport->may_enable != enable) {
3322 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3324 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3326 if (ofport->rstp_port) {
3327 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3331 ofport->may_enable = enable;
3335 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3336 struct ofproto_port *ofproto_port)
3338 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3339 struct dpif_port dpif_port;
3342 if (sset_contains(&ofproto->ghost_ports, devname)) {
3343 const char *type = netdev_get_type_from_name(devname);
3345 /* We may be called before ofproto->up.port_by_name is populated with
3346 * the appropriate ofport. For this reason, we must get the name and
3347 * type from the netdev layer directly. */
3349 const struct ofport *ofport;
3351 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3352 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3353 ofproto_port->name = xstrdup(devname);
3354 ofproto_port->type = xstrdup(type);
3360 if (!sset_contains(&ofproto->ports, devname)) {
3363 error = dpif_port_query_by_name(ofproto->backer->dpif,
3364 devname, &dpif_port);
3366 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3372 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3374 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3375 const char *devname = netdev_get_name(netdev);
3376 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3377 const char *dp_port_name;
3379 if (netdev_vport_is_patch(netdev)) {
3380 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3384 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3385 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3386 odp_port_t port_no = ODPP_NONE;
3389 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3393 if (netdev_get_tunnel_config(netdev)) {
3394 simap_put(&ofproto->backer->tnl_backers,
3395 dp_port_name, odp_to_u32(port_no));
3399 if (netdev_get_tunnel_config(netdev)) {
3400 sset_add(&ofproto->ghost_ports, devname);
3402 sset_add(&ofproto->ports, devname);
3408 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3410 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3411 struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
3418 sset_find_and_delete(&ofproto->ghost_ports,
3419 netdev_get_name(ofport->up.netdev));
3420 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3421 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3422 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3424 /* The caller is going to close ofport->up.netdev. If this is a
3425 * bonded port, then the bond is using that netdev, so remove it
3426 * from the bond. The client will need to reconfigure everything
3427 * after deleting ports, so then the slave will get re-added. */
3428 bundle_remove(&ofport->up);
3435 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3437 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3440 error = netdev_get_stats(ofport->up.netdev, stats);
3442 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3443 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3445 ovs_mutex_lock(&ofproto->stats_mutex);
3446 /* ofproto->stats.tx_packets represents packets that we created
3447 * internally and sent to some port (e.g. packets sent with
3448 * ofproto_dpif_send_packet()). Account for them as if they had
3449 * come from OFPP_LOCAL and got forwarded. */
3451 if (stats->rx_packets != UINT64_MAX) {
3452 stats->rx_packets += ofproto->stats.tx_packets;
3455 if (stats->rx_bytes != UINT64_MAX) {
3456 stats->rx_bytes += ofproto->stats.tx_bytes;
3459 /* ofproto->stats.rx_packets represents packets that were received on
3460 * some port and we processed internally and dropped (e.g. STP).
3461 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3463 if (stats->tx_packets != UINT64_MAX) {
3464 stats->tx_packets += ofproto->stats.rx_packets;
3467 if (stats->tx_bytes != UINT64_MAX) {
3468 stats->tx_bytes += ofproto->stats.rx_bytes;
3470 ovs_mutex_unlock(&ofproto->stats_mutex);
3477 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3479 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3480 if (ofport->bundle && ofport->bundle->lacp) {
3481 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3488 struct port_dump_state {
3493 struct ofproto_port port;
3498 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3500 *statep = xzalloc(sizeof(struct port_dump_state));
3505 port_dump_next(const struct ofproto *ofproto_, void *state_,
3506 struct ofproto_port *port)
3508 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3509 struct port_dump_state *state = state_;
3510 const struct sset *sset;
3511 struct sset_node *node;
3513 if (state->has_port) {
3514 ofproto_port_destroy(&state->port);
3515 state->has_port = false;
3517 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3518 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3521 error = port_query_by_name(ofproto_, node->name, &state->port);
3523 *port = state->port;
3524 state->has_port = true;
3526 } else if (error != ENODEV) {
3531 if (!state->ghost) {
3532 state->ghost = true;
3535 return port_dump_next(ofproto_, state_, port);
3542 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3544 struct port_dump_state *state = state_;
3546 if (state->has_port) {
3547 ofproto_port_destroy(&state->port);
3554 port_poll(const struct ofproto *ofproto_, char **devnamep)
3556 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3558 if (ofproto->port_poll_errno) {
3559 int error = ofproto->port_poll_errno;
3560 ofproto->port_poll_errno = 0;
3564 if (sset_is_empty(&ofproto->port_poll_set)) {
3568 *devnamep = sset_pop(&ofproto->port_poll_set);
3573 port_poll_wait(const struct ofproto *ofproto_)
3575 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3576 dpif_port_poll_wait(ofproto->backer->dpif);
3580 port_is_lacp_current(const struct ofport *ofport_)
3582 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3583 return (ofport->bundle && ofport->bundle->lacp
3584 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3588 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3589 * then delete it entirely. */
3591 rule_expire(struct rule_dpif *rule)
3592 OVS_REQUIRES(ofproto_mutex)
3594 uint16_t hard_timeout, idle_timeout;
3595 long long int now = time_msec();
3598 hard_timeout = rule->up.hard_timeout;
3599 idle_timeout = rule->up.idle_timeout;
3601 /* Has 'rule' expired? */
3603 long long int modified;
3605 ovs_mutex_lock(&rule->up.mutex);
3606 modified = rule->up.modified;
3607 ovs_mutex_unlock(&rule->up.mutex);
3609 if (now > modified + hard_timeout * 1000) {
3610 reason = OFPRR_HARD_TIMEOUT;
3614 if (reason < 0 && idle_timeout) {
3617 ovs_mutex_lock(&rule->stats_mutex);
3618 used = rule->stats.used;
3619 ovs_mutex_unlock(&rule->stats_mutex);
3621 if (now > used + idle_timeout * 1000) {
3622 reason = OFPRR_IDLE_TIMEOUT;
3627 COVERAGE_INC(ofproto_dpif_expired);
3628 ofproto_rule_expire(&rule->up, reason);
3632 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3633 * 'flow' must reflect the data in 'packet'. */
3635 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3636 const struct flow *flow,
3637 struct rule_dpif *rule,
3638 const struct ofpact *ofpacts, size_t ofpacts_len,
3639 struct dp_packet *packet)
3641 struct dpif_flow_stats stats;
3642 struct xlate_out xout;
3643 struct xlate_in xin;
3645 struct dpif_execute execute;
3648 ovs_assert((rule != NULL) != (ofpacts != NULL));
3650 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3653 rule_dpif_credit_stats(rule, &stats);
3656 xlate_in_init(&xin, ofproto, flow, flow->in_port.ofp_port, rule,
3657 stats.tcp_flags, packet);
3658 xin.ofpacts = ofpacts;
3659 xin.ofpacts_len = ofpacts_len;
3660 xin.resubmit_stats = &stats;
3661 xlate_actions(&xin, &xout);
3663 execute.actions = xout.odp_actions->data;
3664 execute.actions_len = xout.odp_actions->size;
3666 pkt_metadata_from_flow(&packet->md, flow);
3667 execute.packet = packet;
3668 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3669 execute.probe = false;
3671 /* Fix up in_port. */
3672 in_port = flow->in_port.ofp_port;
3673 if (in_port == OFPP_NONE) {
3674 in_port = OFPP_LOCAL;
3676 execute.packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3678 error = dpif_execute(ofproto->backer->dpif, &execute);
3680 xlate_out_uninit(&xout);
3686 rule_dpif_credit_stats(struct rule_dpif *rule,
3687 const struct dpif_flow_stats *stats)
3689 ovs_mutex_lock(&rule->stats_mutex);
3690 if (OVS_UNLIKELY(rule->new_rule)) {
3691 rule_dpif_credit_stats(rule->new_rule, stats);
3693 rule->stats.n_packets += stats->n_packets;
3694 rule->stats.n_bytes += stats->n_bytes;
3695 rule->stats.used = MAX(rule->stats.used, stats->used);
3697 ovs_mutex_unlock(&rule->stats_mutex);
3701 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3702 OVS_REQUIRES(rule->up.mutex)
3704 return rule->up.flow_cookie;
3708 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3709 uint16_t hard_timeout)
3711 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3714 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3715 * be read until the calling thread quiesces. */
3716 const struct rule_actions *
3717 rule_dpif_get_actions(const struct rule_dpif *rule)
3719 return rule_get_actions(&rule->up);
3722 /* Sets 'rule''s recirculation id. */
3724 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3725 OVS_REQUIRES(rule->up.mutex)
3727 ovs_assert(!rule->recirc_id || rule->recirc_id == id);
3728 if (rule->recirc_id == id) {
3729 /* Release the new reference to the same id. */
3732 rule->recirc_id = id;
3736 /* Sets 'rule''s recirculation id. */
3738 rule_set_recirc_id(struct rule *rule_, uint32_t id)
3740 struct rule_dpif *rule = rule_dpif_cast(rule_);
3742 ovs_mutex_lock(&rule->up.mutex);
3743 rule_dpif_set_recirc_id(rule, id);
3744 ovs_mutex_unlock(&rule->up.mutex);
3748 ofproto_dpif_get_tables_version(struct ofproto_dpif *ofproto OVS_UNUSED)
3750 cls_version_t version;
3752 atomic_read_relaxed(&ofproto->tables_version, &version);
3757 /* The returned rule (if any) is valid at least until the next RCU quiescent
3758 * period. If the rule needs to stay around longer, a non-zero 'take_ref'
3759 * must be passed in to cause a reference to be taken on it.
3761 * 'flow' is non-const to allow for temporary modifications during the lookup.
3762 * Any changes are restored before returning. */
3763 static struct rule_dpif *
3764 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, cls_version_t version,
3765 uint8_t table_id, struct flow *flow,
3766 struct flow_wildcards *wc, bool take_ref)
3768 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3769 const struct cls_rule *cls_rule;
3770 struct rule_dpif *rule;
3773 cls_rule = classifier_lookup(cls, version, flow, wc);
3775 rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3777 /* Try again if the rule was released before we get the reference. */
3778 } while (rule && take_ref && !rule_dpif_try_ref(rule));
3783 /* Look up 'flow' in 'ofproto''s classifier version 'version', starting from
3784 * table '*table_id'. Returns the rule that was found, which may be one of the
3785 * special rules according to packet miss hadling. If 'may_packet_in' is
3786 * false, returning of the miss_rule (which issues packet ins for the
3787 * controller) is avoided. Updates 'wc', if nonnull, to reflect the fields
3788 * that were used during the lookup.
3790 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3791 * if none is found then the table miss configuration for that table is
3792 * honored, which can result in additional lookups in other OpenFlow tables.
3793 * In this case the function updates '*table_id' to reflect the final OpenFlow
3794 * table that was searched.
3796 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3799 * The rule is returned in '*rule', which is valid at least until the next
3800 * RCU quiescent period. If the '*rule' needs to stay around longer,
3801 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3802 * on it before this returns.
3804 * 'in_port' allows the lookup to take place as if the in port had the value
3805 * 'in_port'. This is needed for resubmit action support.
3807 * 'flow' is non-const to allow for temporary modifications during the lookup.
3808 * Any changes are restored before returning. */
3810 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3811 cls_version_t version, struct flow *flow,
3812 struct flow_wildcards *wc, bool take_ref,
3813 const struct dpif_flow_stats *stats,
3814 uint8_t *table_id, ofp_port_t in_port,
3815 bool may_packet_in, bool honor_table_miss)
3817 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
3818 ofp_port_t old_in_port = flow->in_port.ofp_port;
3819 enum ofputil_table_miss miss_config;
3820 struct rule_dpif *rule;
3823 /* We always unwildcard nw_frag (for IP), so they
3824 * need not be unwildcarded here. */
3825 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3826 && ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3827 if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3828 /* We must pretend that transport ports are unavailable. */
3829 flow->tp_src = htons(0);
3830 flow->tp_dst = htons(0);
3832 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
3833 * Use the drop_frags_rule (which cannot disappear). */
3834 rule = ofproto->drop_frags_rule;
3836 rule_dpif_ref(rule);
3839 struct oftable *tbl = &ofproto->up.tables[*table_id];
3842 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
3848 /* Look up a flow with 'in_port' as the input port. Then restore the
3849 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3850 * have surprising behavior). */
3851 flow->in_port.ofp_port = in_port;
3853 /* Our current implementation depends on n_tables == N_TABLES, and
3854 * TBL_INTERNAL being the last table. */
3855 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
3857 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
3859 for (next_id = *table_id;
3860 next_id < ofproto->up.n_tables;
3861 next_id++, next_id += (next_id == TBL_INTERNAL))
3863 *table_id = next_id;
3864 rule = rule_dpif_lookup_in_table(ofproto, version, next_id, flow, wc,
3867 struct oftable *tbl = &ofproto->up.tables[next_id];
3870 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
3871 stats->n_packets, &orig);
3874 goto out; /* Match. */
3876 if (honor_table_miss) {
3877 miss_config = ofproto_table_get_miss_config(&ofproto->up,
3879 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
3886 rule = ofproto->no_packet_in_rule;
3887 if (may_packet_in) {
3888 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
3889 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
3890 struct ofport_dpif *port;
3892 port = ofp_port_to_ofport(ofproto, old_in_port);
3894 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3896 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
3897 rule = ofproto->miss_rule;
3899 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
3900 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3901 rule = ofproto->miss_rule;
3905 rule_dpif_ref(rule);
3908 /* Restore port numbers, as they may have been modified above. */
3909 flow->tp_src = old_tp_src;
3910 flow->tp_dst = old_tp_dst;
3911 /* Restore the old in port. */
3912 flow->in_port.ofp_port = old_in_port;
3918 complete_operation(struct rule_dpif *rule)
3919 OVS_REQUIRES(ofproto_mutex)
3921 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3923 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3926 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3928 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3931 static struct rule *
3934 struct rule_dpif *rule = xzalloc(sizeof *rule);
3939 rule_dealloc(struct rule *rule_)
3941 struct rule_dpif *rule = rule_dpif_cast(rule_);
3946 rule_construct(struct rule *rule_)
3947 OVS_NO_THREAD_SAFETY_ANALYSIS
3949 struct rule_dpif *rule = rule_dpif_cast(rule_);
3950 ovs_mutex_init_adaptive(&rule->stats_mutex);
3951 rule->stats.n_packets = 0;
3952 rule->stats.n_bytes = 0;
3953 rule->stats.used = rule->up.modified;
3954 rule->recirc_id = 0;
3955 rule->new_rule = NULL;
3961 rule_insert(struct rule *rule_, struct rule *old_rule_, bool forward_stats)
3962 OVS_REQUIRES(ofproto_mutex)
3964 struct rule_dpif *rule = rule_dpif_cast(rule_);
3966 if (old_rule_ && forward_stats) {
3967 struct rule_dpif *old_rule = rule_dpif_cast(old_rule_);
3969 ovs_assert(!old_rule->new_rule);
3971 /* Take a reference to the new rule, and refer all stats updates from
3972 * the old rule to the new rule. */
3973 rule_dpif_ref(rule);
3975 ovs_mutex_lock(&old_rule->stats_mutex);
3976 ovs_mutex_lock(&rule->stats_mutex);
3977 old_rule->new_rule = rule; /* Forward future stats. */
3978 rule->stats = old_rule->stats; /* Transfer stats to the new rule. */
3979 ovs_mutex_unlock(&rule->stats_mutex);
3980 ovs_mutex_unlock(&old_rule->stats_mutex);
3983 complete_operation(rule);
3987 rule_delete(struct rule *rule_)
3988 OVS_REQUIRES(ofproto_mutex)
3990 struct rule_dpif *rule = rule_dpif_cast(rule_);
3991 complete_operation(rule);
3995 rule_destruct(struct rule *rule_)
3996 OVS_NO_THREAD_SAFETY_ANALYSIS
3998 struct rule_dpif *rule = rule_dpif_cast(rule_);
4000 ovs_mutex_destroy(&rule->stats_mutex);
4001 /* Release reference to the new rule, if any. */
4002 if (rule->new_rule) {
4003 rule_dpif_unref(rule->new_rule);
4005 if (rule->recirc_id) {
4006 recirc_free_id(rule->recirc_id);
4011 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4012 long long int *used)
4014 struct rule_dpif *rule = rule_dpif_cast(rule_);
4016 ovs_mutex_lock(&rule->stats_mutex);
4017 if (OVS_UNLIKELY(rule->new_rule)) {
4018 rule_get_stats(&rule->new_rule->up, packets, bytes, used);
4020 *packets = rule->stats.n_packets;
4021 *bytes = rule->stats.n_bytes;
4022 *used = rule->stats.used;
4024 ovs_mutex_unlock(&rule->stats_mutex);
4028 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
4029 struct dp_packet *packet)
4031 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4033 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
4037 rule_execute(struct rule *rule, const struct flow *flow,
4038 struct dp_packet *packet)
4040 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
4041 dp_packet_delete(packet);
4045 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4047 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4050 static struct ofgroup *
4053 struct group_dpif *group = xzalloc(sizeof *group);
4058 group_dealloc(struct ofgroup *group_)
4060 struct group_dpif *group = group_dpif_cast(group_);
4065 group_construct_stats(struct group_dpif *group)
4066 OVS_REQUIRES(group->stats_mutex)
4068 struct ofputil_bucket *bucket;
4069 const struct ovs_list *buckets;
4071 group->packet_count = 0;
4072 group->byte_count = 0;
4074 group_dpif_get_buckets(group, &buckets);
4075 LIST_FOR_EACH (bucket, list_node, buckets) {
4076 bucket->stats.packet_count = 0;
4077 bucket->stats.byte_count = 0;
4082 group_dpif_credit_stats(struct group_dpif *group,
4083 struct ofputil_bucket *bucket,
4084 const struct dpif_flow_stats *stats)
4086 ovs_mutex_lock(&group->stats_mutex);
4087 group->packet_count += stats->n_packets;
4088 group->byte_count += stats->n_bytes;
4090 bucket->stats.packet_count += stats->n_packets;
4091 bucket->stats.byte_count += stats->n_bytes;
4092 } else { /* Credit to all buckets */
4093 const struct ovs_list *buckets;
4095 group_dpif_get_buckets(group, &buckets);
4096 LIST_FOR_EACH (bucket, list_node, buckets) {
4097 bucket->stats.packet_count += stats->n_packets;
4098 bucket->stats.byte_count += stats->n_bytes;
4101 ovs_mutex_unlock(&group->stats_mutex);
4105 group_construct(struct ofgroup *group_)
4107 struct group_dpif *group = group_dpif_cast(group_);
4109 ovs_mutex_init_adaptive(&group->stats_mutex);
4110 ovs_mutex_lock(&group->stats_mutex);
4111 group_construct_stats(group);
4112 ovs_mutex_unlock(&group->stats_mutex);
4117 group_destruct(struct ofgroup *group_)
4119 struct group_dpif *group = group_dpif_cast(group_);
4120 ovs_mutex_destroy(&group->stats_mutex);
4124 group_modify(struct ofgroup *group_)
4126 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
4128 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4134 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4136 struct group_dpif *group = group_dpif_cast(group_);
4137 struct ofputil_bucket *bucket;
4138 const struct ovs_list *buckets;
4139 struct bucket_counter *bucket_stats;
4141 ovs_mutex_lock(&group->stats_mutex);
4142 ogs->packet_count = group->packet_count;
4143 ogs->byte_count = group->byte_count;
4145 group_dpif_get_buckets(group, &buckets);
4146 bucket_stats = ogs->bucket_stats;
4147 LIST_FOR_EACH (bucket, list_node, buckets) {
4148 bucket_stats->packet_count = bucket->stats.packet_count;
4149 bucket_stats->byte_count = bucket->stats.byte_count;
4152 ovs_mutex_unlock(&group->stats_mutex);
4157 /* If the group exists, this function increments the groups's reference count.
4159 * Make sure to call group_dpif_unref() after no longer needing to maintain
4160 * a reference to the group. */
4162 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4163 struct group_dpif **group)
4165 struct ofgroup *ofgroup;
4168 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4169 *group = found ? group_dpif_cast(ofgroup) : NULL;
4175 group_dpif_get_buckets(const struct group_dpif *group,
4176 const struct ovs_list **buckets)
4178 *buckets = &group->up.buckets;
4181 enum ofp11_group_type
4182 group_dpif_get_type(const struct group_dpif *group)
4184 return group->up.type;
4188 group_dpif_get_selection_method(const struct group_dpif *group)
4190 return group->up.props.selection_method;
4193 /* Sends 'packet' out 'ofport'.
4194 * May modify 'packet'.
4195 * Returns 0 if successful, otherwise a positive errno value. */
4197 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4199 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4202 error = xlate_send_packet(ofport, packet);
4204 ovs_mutex_lock(&ofproto->stats_mutex);
4205 ofproto->stats.tx_packets++;
4206 ofproto->stats.tx_bytes += dp_packet_size(packet);
4207 ovs_mutex_unlock(&ofproto->stats_mutex);
4212 group_dpif_get_selection_method_param(const struct group_dpif *group)
4214 return group->up.props.selection_method_param;
4217 const struct field_array *
4218 group_dpif_get_fields(const struct group_dpif *group)
4220 return &group->up.props.fields;
4223 /* Return the version string of the datapath that backs up
4227 get_datapath_version(const struct ofproto *ofproto_)
4229 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4231 return ofproto->backer->dp_version_string;
4235 set_frag_handling(struct ofproto *ofproto_,
4236 enum ofp_config_flags frag_handling)
4238 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4239 if (frag_handling != OFPC_FRAG_REASM) {
4240 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4248 packet_out(struct ofproto *ofproto_, struct dp_packet *packet,
4249 const struct flow *flow,
4250 const struct ofpact *ofpacts, size_t ofpacts_len)
4252 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4254 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4255 ofpacts_len, packet);
4262 set_netflow(struct ofproto *ofproto_,
4263 const struct netflow_options *netflow_options)
4265 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4267 if (netflow_options) {
4268 if (!ofproto->netflow) {
4269 ofproto->netflow = netflow_create();
4270 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4272 return netflow_set_options(ofproto->netflow, netflow_options);
4273 } else if (ofproto->netflow) {
4274 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4275 netflow_unref(ofproto->netflow);
4276 ofproto->netflow = NULL;
4283 get_netflow_ids(const struct ofproto *ofproto_,
4284 uint8_t *engine_type, uint8_t *engine_id)
4286 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4288 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4291 static struct ofproto_dpif *
4292 ofproto_dpif_lookup(const char *name)
4294 struct ofproto_dpif *ofproto;
4296 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4297 hash_string(name, 0), &all_ofproto_dpifs) {
4298 if (!strcmp(ofproto->up.name, name)) {
4306 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4307 const char *argv[], void *aux OVS_UNUSED)
4309 struct ofproto_dpif *ofproto;
4312 ofproto = ofproto_dpif_lookup(argv[1]);
4314 unixctl_command_reply_error(conn, "no such bridge");
4317 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4318 mac_learning_flush(ofproto->ml);
4319 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4321 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4322 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4323 mac_learning_flush(ofproto->ml);
4324 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4328 unixctl_command_reply(conn, "table successfully flushed");
4332 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4333 const char *argv[], void *aux OVS_UNUSED)
4335 struct ofproto_dpif *ofproto;
4338 ofproto = ofproto_dpif_lookup(argv[1]);
4340 unixctl_command_reply_error(conn, "no such bridge");
4344 if (!mcast_snooping_enabled(ofproto->ms)) {
4345 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4348 mcast_snooping_mdb_flush(ofproto->ms);
4350 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4351 if (!mcast_snooping_enabled(ofproto->ms)) {
4354 mcast_snooping_mdb_flush(ofproto->ms);
4358 unixctl_command_reply(conn, "table successfully flushed");
4361 static struct ofport_dpif *
4362 ofbundle_get_a_port(const struct ofbundle *bundle)
4364 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
4369 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4370 const char *argv[], void *aux OVS_UNUSED)
4372 struct ds ds = DS_EMPTY_INITIALIZER;
4373 const struct ofproto_dpif *ofproto;
4374 const struct mac_entry *e;
4376 ofproto = ofproto_dpif_lookup(argv[1]);
4378 unixctl_command_reply_error(conn, "no such bridge");
4382 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4383 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4384 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4385 struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
4386 char name[OFP_MAX_PORT_NAME_LEN];
4388 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4390 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
4391 name, e->vlan, ETH_ADDR_ARGS(e->mac),
4392 mac_entry_age(ofproto->ml, e));
4394 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4395 unixctl_command_reply(conn, ds_cstr(&ds));
4400 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4401 int argc OVS_UNUSED,
4403 void *aux OVS_UNUSED)
4405 struct ds ds = DS_EMPTY_INITIALIZER;
4406 const struct ofproto_dpif *ofproto;
4407 const struct ofbundle *bundle;
4408 const struct mcast_group *grp;
4409 struct mcast_group_bundle *b;
4410 struct mcast_mrouter_bundle *mrouter;
4412 ofproto = ofproto_dpif_lookup(argv[1]);
4414 unixctl_command_reply_error(conn, "no such bridge");
4418 if (!mcast_snooping_enabled(ofproto->ms)) {
4419 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4423 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
4424 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4425 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4426 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
4427 char name[OFP_MAX_PORT_NAME_LEN];
4430 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4432 ds_put_format(&ds, "%5s %4d "IP_FMT" %3d\n",
4433 name, grp->vlan, IP_ARGS(grp->ip4),
4434 mcast_bundle_age(ofproto->ms, b));
4438 /* ports connected to multicast routers */
4439 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
4440 char name[OFP_MAX_PORT_NAME_LEN];
4442 bundle = mrouter->port;
4443 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4445 ds_put_format(&ds, "%5s %4d querier %3d\n",
4446 name, mrouter->vlan,
4447 mcast_mrouter_age(ofproto->ms, mrouter));
4449 ovs_rwlock_unlock(&ofproto->ms->rwlock);
4450 unixctl_command_reply(conn, ds_cstr(&ds));
4455 struct xlate_out xout;
4456 struct xlate_in xin;
4457 const struct flow *key;
4459 struct flow_wildcards wc;
4464 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4466 const struct rule_actions *actions;
4469 ds_put_char_multiple(result, '\t', level);
4471 ds_put_cstr(result, "No match\n");
4475 ovs_mutex_lock(&rule->up.mutex);
4476 cookie = rule->up.flow_cookie;
4477 ovs_mutex_unlock(&rule->up.mutex);
4479 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4480 rule ? rule->up.table_id : 0, ntohll(cookie));
4481 cls_rule_format(&rule->up.cr, result);
4482 ds_put_char(result, '\n');
4484 actions = rule_dpif_get_actions(rule);
4486 ds_put_char_multiple(result, '\t', level);
4487 ds_put_cstr(result, "OpenFlow actions=");
4488 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4489 ds_put_char(result, '\n');
4493 trace_format_flow(struct ds *result, int level, const char *title,
4494 struct trace_ctx *trace)
4496 ds_put_char_multiple(result, '\t', level);
4497 ds_put_format(result, "%s: ", title);
4498 /* Do not report unchanged flows for resubmits. */
4499 if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4500 || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4501 ds_put_cstr(result, "unchanged");
4503 flow_format(result, &trace->xin.flow);
4504 trace->flow = trace->xin.flow;
4506 ds_put_char(result, '\n');
4510 trace_format_regs(struct ds *result, int level, const char *title,
4511 struct trace_ctx *trace)
4515 ds_put_char_multiple(result, '\t', level);
4516 ds_put_format(result, "%s:", title);
4517 for (i = 0; i < FLOW_N_REGS; i++) {
4518 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4520 ds_put_char(result, '\n');
4524 trace_format_odp(struct ds *result, int level, const char *title,
4525 struct trace_ctx *trace)
4527 struct ofpbuf *odp_actions = trace->xout.odp_actions;
4529 ds_put_char_multiple(result, '\t', level);
4530 ds_put_format(result, "%s: ", title);
4531 format_odp_actions(result, odp_actions->data, odp_actions->size);
4532 ds_put_char(result, '\n');
4536 trace_format_megaflow(struct ds *result, int level, const char *title,
4537 struct trace_ctx *trace)
4541 ds_put_char_multiple(result, '\t', level);
4542 ds_put_format(result, "%s: ", title);
4543 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
4544 match_init(&match, trace->key, &trace->wc);
4545 match_format(&match, result, OFP_DEFAULT_PRIORITY);
4546 ds_put_char(result, '\n');
4549 static void trace_report(struct xlate_in *, int recurse,
4550 const char *format, ...)
4551 OVS_PRINTF_FORMAT(3, 4);
4552 static void trace_report_valist(struct xlate_in *, int recurse,
4553 const char *format, va_list args)
4554 OVS_PRINTF_FORMAT(3, 0);
4557 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
4559 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4560 struct ds *result = trace->result;
4563 if (rule == xin->ofproto->miss_rule) {
4564 trace_report(xin, recurse,
4565 "No match, flow generates \"packet in\"s.");
4566 } else if (rule == xin->ofproto->no_packet_in_rule) {
4567 trace_report(xin, recurse, "No match, packets dropped because "
4568 "OFPPC_NO_PACKET_IN is set on in_port.");
4569 } else if (rule == xin->ofproto->drop_frags_rule) {
4570 trace_report(xin, recurse, "Packets dropped because they are IP "
4571 "fragments and the fragment handling mode is "
4576 ds_put_char(result, '\n');
4578 trace_format_flow(result, recurse, "Resubmitted flow", trace);
4579 trace_format_regs(result, recurse, "Resubmitted regs", trace);
4580 trace_format_odp(result, recurse, "Resubmitted odp", trace);
4581 trace_format_megaflow(result, recurse, "Resubmitted megaflow", trace);
4583 trace_format_rule(result, recurse, rule);
4587 trace_report_valist(struct xlate_in *xin, int recurse,
4588 const char *format, va_list args)
4590 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4591 struct ds *result = trace->result;
4593 ds_put_char_multiple(result, '\t', recurse);
4594 ds_put_format_valist(result, format, args);
4595 ds_put_char(result, '\n');
4599 trace_report(struct xlate_in *xin, int recurse, const char *format, ...)
4603 va_start(args, format);
4604 trace_report_valist(xin, recurse, format, args);
4608 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4609 * forms are supported:
4611 * - [dpname] odp_flow [-generate | packet]
4612 * - bridge br_flow [-generate | packet]
4614 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4615 * returns a nonnull malloced error message. */
4616 static char * OVS_WARN_UNUSED_RESULT
4617 parse_flow_and_packet(int argc, const char *argv[],
4618 struct ofproto_dpif **ofprotop, struct flow *flow,
4619 struct dp_packet **packetp)
4621 const struct dpif_backer *backer = NULL;
4622 const char *error = NULL;
4624 struct simap port_names = SIMAP_INITIALIZER(&port_names);
4625 struct dp_packet *packet;
4626 struct ofpbuf odp_key;
4627 struct ofpbuf odp_mask;
4629 ofpbuf_init(&odp_key, 0);
4630 ofpbuf_init(&odp_mask, 0);
4632 /* Handle "-generate" or a hex string as the last argument. */
4633 if (!strcmp(argv[argc - 1], "-generate")) {
4634 packet = dp_packet_new(0);
4637 error = eth_from_hex(argv[argc - 1], &packet);
4640 } else if (argc == 4) {
4641 /* The 3-argument form must end in "-generate' or a hex string. */
4647 /* odp_flow can have its in_port specified as a name instead of port no.
4648 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4649 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4650 * we need to create a simap of name to port no. */
4652 const char *dp_type;
4653 if (!strncmp(argv[1], "ovs-", 4)) {
4654 dp_type = argv[1] + 4;
4658 backer = shash_find_data(&all_dpif_backers, dp_type);
4659 } else if (argc == 2) {
4660 struct shash_node *node;
4661 if (shash_count(&all_dpif_backers) == 1) {
4662 node = shash_first(&all_dpif_backers);
4663 backer = node->data;
4666 error = "Syntax error";
4669 if (backer && backer->dpif) {
4670 struct dpif_port dpif_port;
4671 struct dpif_port_dump port_dump;
4672 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4673 simap_put(&port_names, dpif_port.name,
4674 odp_to_u32(dpif_port.port_no));
4678 /* Parse the flow and determine whether a datapath or
4679 * bridge is specified. If function odp_flow_key_from_string()
4680 * returns 0, the flow is a odp_flow. If function
4681 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4682 if (!odp_flow_from_string(argv[argc - 1], &port_names,
4683 &odp_key, &odp_mask)) {
4685 error = "Cannot find the datapath";
4689 if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
4690 error = "Failed to parse datapath flow key";
4694 *ofprotop = xlate_lookup_ofproto(backer, flow,
4695 &flow->in_port.ofp_port);
4696 if (*ofprotop == NULL) {
4697 error = "Invalid datapath flow";
4701 vsp_adjust_flow(*ofprotop, flow, NULL);
4704 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4707 m_err = xasprintf("Bad openflow flow syntax: %s", err);
4712 error = "Must specify bridge name";
4716 *ofprotop = ofproto_dpif_lookup(argv[1]);
4718 error = "Unknown bridge name";
4724 /* Generate a packet, if requested. */
4726 if (!dp_packet_size(packet)) {
4727 flow_compose(packet, flow);
4729 /* Use the metadata from the flow and the packet argument
4730 * to reconstruct the flow. */
4731 pkt_metadata_from_flow(&packet->md, flow);
4732 flow_extract(packet, flow);
4737 if (error && !m_err) {
4738 m_err = xstrdup(error);
4741 dp_packet_delete(packet);
4745 ofpbuf_uninit(&odp_key);
4746 ofpbuf_uninit(&odp_mask);
4747 simap_destroy(&port_names);
4752 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4753 void *aux OVS_UNUSED)
4755 struct ofproto_dpif *ofproto;
4756 struct dp_packet *packet;
4760 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4765 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4766 unixctl_command_reply(conn, ds_cstr(&result));
4767 ds_destroy(&result);
4768 dp_packet_delete(packet);
4770 unixctl_command_reply_error(conn, error);
4776 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4777 const char *argv[], void *aux OVS_UNUSED)
4779 enum ofputil_protocol usable_protocols;
4780 struct ofproto_dpif *ofproto;
4781 bool enforce_consistency;
4782 struct ofpbuf ofpacts;
4783 struct dp_packet *packet;
4788 /* Three kinds of error return values! */
4794 ofpbuf_init(&ofpacts, 0);
4796 /* Parse actions. */
4797 error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
4799 unixctl_command_reply_error(conn, error);
4804 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4805 * consistency between the flow and the actions. With -consistent, we
4806 * enforce consistency even for a flow supported in OpenFlow 1.0. */
4807 if (!strcmp(argv[1], "-consistent")) {
4808 enforce_consistency = true;
4812 enforce_consistency = false;
4815 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4817 unixctl_command_reply_error(conn, error);
4822 /* Do the same checks as handle_packet_out() in ofproto.c.
4824 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
4825 * strictly correct because these actions aren't in any table, but it's OK
4826 * because it 'table_id' is used only to check goto_table instructions, but
4827 * packet-outs take a list of actions and therefore it can't include
4830 * We skip the "meter" check here because meter is an instruction, not an
4831 * action, and thus cannot appear in ofpacts. */
4832 in_port = ofp_to_u16(flow.in_port.ofp_port);
4833 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
4834 unixctl_command_reply_error(conn, "invalid in_port");
4837 if (enforce_consistency) {
4838 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
4839 &flow, u16_to_ofp(ofproto->up.max_ports),
4840 0, 0, usable_protocols);
4842 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
4843 u16_to_ofp(ofproto->up.max_ports), 0, 0,
4849 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4850 unixctl_command_reply_error(conn, ds_cstr(&result));
4854 ofproto_trace(ofproto, &flow, packet,
4855 ofpacts.data, ofpacts.size, &result);
4856 unixctl_command_reply(conn, ds_cstr(&result));
4859 ds_destroy(&result);
4860 dp_packet_delete(packet);
4861 ofpbuf_uninit(&ofpacts);
4864 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4865 * description of the results to 'ds'.
4867 * The trace follows a packet with the specified 'flow' through the flow
4868 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4869 * side effects (if it is nonnull then its flow must be 'flow').
4871 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4872 * trace, otherwise the actions are determined by a flow table lookup. */
4874 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4875 const struct dp_packet *packet,
4876 const struct ofpact ofpacts[], size_t ofpacts_len,
4879 struct trace_ctx trace;
4881 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4882 ds_put_cstr(ds, "Flow: ");
4883 flow_format(ds, flow);
4884 ds_put_char(ds, '\n');
4886 flow_wildcards_init_catchall(&trace.wc);
4889 trace.key = flow; /* Original flow key, used for megaflow. */
4890 trace.flow = *flow; /* May be modified by actions. */
4891 xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
4892 ntohs(flow->tcp_flags), packet);
4893 trace.xin.ofpacts = ofpacts;
4894 trace.xin.ofpacts_len = ofpacts_len;
4895 trace.xin.resubmit_hook = trace_resubmit;
4896 trace.xin.report_hook = trace_report_valist;
4898 xlate_actions(&trace.xin, &trace.xout);
4900 ds_put_char(ds, '\n');
4901 trace_format_flow(ds, 0, "Final flow", &trace);
4902 trace_format_megaflow(ds, 0, "Megaflow", &trace);
4904 ds_put_cstr(ds, "Datapath actions: ");
4905 format_odp_actions(ds, trace.xout.odp_actions->data,
4906 trace.xout.odp_actions->size);
4908 if (trace.xout.slow) {
4909 enum slow_path_reason slow;
4911 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4912 "slow path because it:");
4914 slow = trace.xout.slow;
4916 enum slow_path_reason bit = rightmost_1bit(slow);
4918 ds_put_format(ds, "\n\t- %s.",
4919 slow_path_reason_to_explanation(bit));
4925 xlate_out_uninit(&trace.xout);
4928 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4929 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4930 * to destroy 'ofproto_shash' and free the returned value. */
4931 static const struct shash_node **
4932 get_ofprotos(struct shash *ofproto_shash)
4934 const struct ofproto_dpif *ofproto;
4936 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4937 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4938 shash_add_nocopy(ofproto_shash, name, ofproto);
4941 return shash_sort(ofproto_shash);
4945 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4946 const char *argv[] OVS_UNUSED,
4947 void *aux OVS_UNUSED)
4949 struct ds ds = DS_EMPTY_INITIALIZER;
4950 struct shash ofproto_shash;
4951 const struct shash_node **sorted_ofprotos;
4954 shash_init(&ofproto_shash);
4955 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4956 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4957 const struct shash_node *node = sorted_ofprotos[i];
4958 ds_put_format(&ds, "%s\n", node->name);
4961 shash_destroy(&ofproto_shash);
4962 free(sorted_ofprotos);
4964 unixctl_command_reply(conn, ds_cstr(&ds));
4969 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4971 const struct shash_node **ofprotos;
4972 struct dpif_dp_stats dp_stats;
4973 struct shash ofproto_shash;
4976 dpif_get_dp_stats(backer->dpif, &dp_stats);
4978 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4979 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4981 shash_init(&ofproto_shash);
4982 ofprotos = get_ofprotos(&ofproto_shash);
4983 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4984 struct ofproto_dpif *ofproto = ofprotos[i]->data;
4985 const struct shash_node **ports;
4988 if (ofproto->backer != backer) {
4992 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4994 ports = shash_sort(&ofproto->up.port_by_name);
4995 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4996 const struct shash_node *node = ports[j];
4997 struct ofport *ofport = node->data;
4999 odp_port_t odp_port;
5001 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5004 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5005 if (odp_port != ODPP_NONE) {
5006 ds_put_format(ds, "%"PRIu32":", odp_port);
5008 ds_put_cstr(ds, "none:");
5011 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5014 if (!netdev_get_config(ofport->netdev, &config)) {
5015 const struct smap_node **nodes;
5018 nodes = smap_sort(&config);
5019 for (i = 0; i < smap_count(&config); i++) {
5020 const struct smap_node *node = nodes[i];
5021 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5022 node->key, node->value);
5026 smap_destroy(&config);
5028 ds_put_char(ds, ')');
5029 ds_put_char(ds, '\n');
5033 shash_destroy(&ofproto_shash);
5038 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5039 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5041 struct ds ds = DS_EMPTY_INITIALIZER;
5042 const struct shash_node **backers;
5045 backers = shash_sort(&all_dpif_backers);
5046 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5047 dpif_show_backer(backers[i]->data, &ds);
5051 unixctl_command_reply(conn, ds_cstr(&ds));
5056 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5057 int argc OVS_UNUSED, const char *argv[],
5058 void *aux OVS_UNUSED)
5060 const struct ofproto_dpif *ofproto;
5062 struct ds ds = DS_EMPTY_INITIALIZER;
5063 bool verbosity = false;
5065 struct dpif_port dpif_port;
5066 struct dpif_port_dump port_dump;
5067 struct hmap portno_names;
5069 struct dpif_flow_dump *flow_dump;
5070 struct dpif_flow_dump_thread *flow_dump_thread;
5074 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5076 unixctl_command_reply_error(conn, "no such bridge");
5080 if (argc > 2 && !strcmp(argv[1], "-m")) {
5084 hmap_init(&portno_names);
5085 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5086 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5090 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
5091 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5092 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5095 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5096 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5101 odp_format_ufid(&f.ufid, &ds);
5102 ds_put_cstr(&ds, " ");
5104 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5105 &portno_names, &ds, verbosity);
5106 ds_put_cstr(&ds, ", ");
5107 dpif_flow_stats_format(&f.stats, &ds);
5108 ds_put_cstr(&ds, ", actions:");
5109 format_odp_actions(&ds, f.actions, f.actions_len);
5110 ds_put_char(&ds, '\n');
5112 dpif_flow_dump_thread_destroy(flow_dump_thread);
5113 error = dpif_flow_dump_destroy(flow_dump);
5117 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5118 unixctl_command_reply_error(conn, ds_cstr(&ds));
5120 unixctl_command_reply(conn, ds_cstr(&ds));
5122 odp_portno_names_destroy(&portno_names);
5123 hmap_destroy(&portno_names);
5128 ofproto_revalidate_all_backers(void)
5130 const struct shash_node **backers;
5133 backers = shash_sort(&all_dpif_backers);
5134 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5135 struct dpif_backer *backer = backers[i]->data;
5136 backer->need_revalidate = REV_RECONFIGURE;
5142 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5143 const char *argv[], void *aux OVS_UNUSED)
5145 if (!strcasecmp(argv[1], "off")) {
5146 ofproto_use_tnl_push_pop = false;
5147 unixctl_command_reply(conn, "Tunnel push-pop off");
5148 ofproto_revalidate_all_backers();
5149 } else if (!strcasecmp(argv[1], "on")) {
5150 ofproto_use_tnl_push_pop = true;
5151 unixctl_command_reply(conn, "Tunnel push-pop on");
5152 ofproto_revalidate_all_backers();
5157 ofproto_unixctl_init(void)
5159 static bool registered;
5165 unixctl_command_register(
5167 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5168 1, 3, ofproto_unixctl_trace, NULL);
5169 unixctl_command_register(
5170 "ofproto/trace-packet-out",
5171 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5172 2, 6, ofproto_unixctl_trace_actions, NULL);
5173 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5174 ofproto_unixctl_fdb_flush, NULL);
5175 unixctl_command_register("fdb/show", "bridge", 1, 1,
5176 ofproto_unixctl_fdb_show, NULL);
5177 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5178 ofproto_unixctl_mcast_snooping_flush, NULL);
5179 unixctl_command_register("mdb/show", "bridge", 1, 1,
5180 ofproto_unixctl_mcast_snooping_show, NULL);
5181 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5182 ofproto_unixctl_dpif_dump_dps, NULL);
5183 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5185 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5186 ofproto_unixctl_dpif_dump_flows, NULL);
5188 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5189 disable_tnl_push_pop, NULL);
5192 /* Returns true if 'table' is the table used for internal rules,
5193 * false otherwise. */
5195 table_is_internal(uint8_t table_id)
5197 return table_id == TBL_INTERNAL;
5200 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5202 * This is deprecated. It is only for compatibility with broken device drivers
5203 * in old versions of Linux that do not properly support VLANs when VLAN
5204 * devices are not used. When broken device drivers are no longer in
5205 * widespread use, we will delete these interfaces. */
5208 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
5210 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5211 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5213 if (realdev_ofp_port == ofport->realdev_ofp_port
5214 && vid == ofport->vlandev_vid) {
5218 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5220 if (ofport->realdev_ofp_port) {
5223 if (realdev_ofp_port && ofport->bundle) {
5224 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5225 * themselves be part of a bundle. */
5226 bundle_set(ofport_->ofproto, ofport->bundle, NULL);
5229 ofport->realdev_ofp_port = realdev_ofp_port;
5230 ofport->vlandev_vid = vid;
5232 if (realdev_ofp_port) {
5233 vsp_add(ofport, realdev_ofp_port, vid);
5240 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
5242 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
5246 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
5247 OVS_EXCLUDED(ofproto->vsp_mutex)
5249 /* hmap_is_empty is thread safe. */
5250 return !hmap_is_empty(&ofproto->realdev_vid_map);
5255 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
5256 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5257 OVS_REQUIRES(ofproto->vsp_mutex)
5259 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5260 int vid = vlan_tci_to_vid(vlan_tci);
5261 const struct vlan_splinter *vsp;
5263 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5264 hash_realdev_vid(realdev_ofp_port, vid),
5265 &ofproto->realdev_vid_map) {
5266 if (vsp->realdev_ofp_port == realdev_ofp_port
5267 && vsp->vid == vid) {
5268 return vsp->vlandev_ofp_port;
5272 return realdev_ofp_port;
5275 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5276 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5277 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
5278 * 'vlan_tci' 9, it would return the port number of eth0.9.
5280 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5281 * function just returns its 'realdev_ofp_port' argument. */
5283 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5284 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5285 OVS_EXCLUDED(ofproto->vsp_mutex)
5289 /* hmap_is_empty is thread safe, see if we can return immediately. */
5290 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
5291 return realdev_ofp_port;
5293 ovs_mutex_lock(&ofproto->vsp_mutex);
5294 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
5295 ovs_mutex_unlock(&ofproto->vsp_mutex);
5299 static struct vlan_splinter *
5300 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
5302 struct vlan_splinter *vsp;
5304 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
5305 hash_ofp_port(vlandev_ofp_port),
5306 &ofproto->vlandev_map) {
5307 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5315 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5316 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5317 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
5318 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5319 * eth0 and store 9 in '*vid'.
5321 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5322 * VLAN device. Unless VLAN splinters are enabled, this is what this function
5325 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5326 ofp_port_t vlandev_ofp_port, int *vid)
5327 OVS_REQUIRES(ofproto->vsp_mutex)
5329 if (!hmap_is_empty(&ofproto->vlandev_map)) {
5330 const struct vlan_splinter *vsp;
5332 vsp = vlandev_find(ofproto, vlandev_ofp_port);
5337 return vsp->realdev_ofp_port;
5343 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5344 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
5345 * 'flow->in_port' to the "real" device backing the VLAN device, sets
5346 * 'flow->vlan_tci' to the VLAN VID, and returns true. Optionally pushes the
5347 * appropriate VLAN on 'packet' if provided. Otherwise (which is always the
5348 * case unless VLAN splinters are enabled), returns false without making any
5351 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow,
5352 struct dp_packet *packet)
5353 OVS_EXCLUDED(ofproto->vsp_mutex)
5358 /* hmap_is_empty is thread safe. */
5359 if (hmap_is_empty(&ofproto->vlandev_map)) {
5363 ovs_mutex_lock(&ofproto->vsp_mutex);
5364 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
5365 ovs_mutex_unlock(&ofproto->vsp_mutex);
5370 /* Cause the flow to be processed as if it came in on the real device with
5371 * the VLAN device's VLAN ID. */
5372 flow->in_port.ofp_port = realdev;
5373 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
5376 /* Make the packet resemble the flow, so that it gets sent to an
5377 * OpenFlow controller properly, so that it looks correct for sFlow,
5378 * and so that flow_extract() will get the correct vlan_tci if it is
5379 * called on 'packet'. */
5380 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
5387 vsp_remove(struct ofport_dpif *port)
5389 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5390 struct vlan_splinter *vsp;
5392 ovs_mutex_lock(&ofproto->vsp_mutex);
5393 vsp = vlandev_find(ofproto, port->up.ofp_port);
5395 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5396 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5399 port->realdev_ofp_port = 0;
5401 VLOG_ERR("missing vlan device record");
5403 ovs_mutex_unlock(&ofproto->vsp_mutex);
5407 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
5409 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5411 ovs_mutex_lock(&ofproto->vsp_mutex);
5412 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
5413 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
5414 == realdev_ofp_port)) {
5415 struct vlan_splinter *vsp;
5417 vsp = xmalloc(sizeof *vsp);
5418 vsp->realdev_ofp_port = realdev_ofp_port;
5419 vsp->vlandev_ofp_port = port->up.ofp_port;
5422 port->realdev_ofp_port = realdev_ofp_port;
5424 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
5425 hash_ofp_port(port->up.ofp_port));
5426 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
5427 hash_realdev_vid(realdev_ofp_port, vid));
5429 VLOG_ERR("duplicate vlan device record");
5431 ovs_mutex_unlock(&ofproto->vsp_mutex);
5435 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5437 const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
5438 return ofport ? ofport->odp_port : ODPP_NONE;
5441 struct ofport_dpif *
5442 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5444 struct ofport_dpif *port;
5446 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5447 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5448 &backer->odp_to_ofport_map) {
5449 if (port->odp_port == odp_port) {
5450 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5455 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5460 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5462 struct ofport_dpif *port;
5464 port = odp_port_to_ofport(ofproto->backer, odp_port);
5465 if (port && &ofproto->up == port->up.ofproto) {
5466 return port->up.ofp_port;
5473 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5474 const struct match *match, int priority,
5475 uint16_t idle_timeout,
5476 const struct ofpbuf *ofpacts,
5477 struct rule **rulep)
5479 struct ofputil_flow_mod fm;
5480 struct rule_dpif *rule;
5484 fm.priority = priority;
5485 fm.new_cookie = htonll(0);
5486 fm.cookie = htonll(0);
5487 fm.cookie_mask = htonll(0);
5488 fm.modify_cookie = false;
5489 fm.table_id = TBL_INTERNAL;
5490 fm.command = OFPFC_ADD;
5491 fm.idle_timeout = idle_timeout;
5492 fm.hard_timeout = 0;
5496 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5497 fm.ofpacts = ofpacts->data;
5498 fm.ofpacts_len = ofpacts->size;
5500 error = ofproto_flow_mod(&ofproto->up, &fm);
5502 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5503 ofperr_to_string(error));
5508 rule = rule_dpif_lookup_in_table(ofproto,
5509 ofproto_dpif_get_tables_version(ofproto),
5510 TBL_INTERNAL, &fm.match.flow,
5511 &fm.match.wc, false);
5521 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5522 struct match *match, int priority)
5524 struct ofputil_flow_mod fm;
5528 fm.priority = priority;
5529 fm.new_cookie = htonll(0);
5530 fm.cookie = htonll(0);
5531 fm.cookie_mask = htonll(0);
5532 fm.modify_cookie = false;
5533 fm.table_id = TBL_INTERNAL;
5534 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5535 fm.command = OFPFC_DELETE_STRICT;
5537 error = ofproto_flow_mod(&ofproto->up, &fm);
5539 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5540 ofperr_to_string(error));
5547 const struct ofproto_class ofproto_dpif_class = {
5561 NULL, /* get_memory_usage. */
5562 type_get_memory_usage,
5581 port_is_lacp_current,
5582 port_get_lacp_stats,
5583 NULL, /* rule_choose_table */
5607 aa_vlan_get_queue_size,
5614 get_stp_port_status,
5619 get_rstp_port_status,
5626 is_mirror_output_bundle,
5627 forward_bpdu_changed,
5628 set_mac_table_config,
5630 set_mcast_snooping_port,
5632 NULL, /* meter_get_features */
5633 NULL, /* meter_set */
5634 NULL, /* meter_get */
5635 NULL, /* meter_del */
5636 group_alloc, /* group_alloc */
5637 group_construct, /* group_construct */
5638 group_destruct, /* group_destruct */
5639 group_dealloc, /* group_dealloc */
5640 group_modify, /* group_modify */
5641 group_get_stats, /* group_get_stats */
5642 get_datapath_version, /* get_datapath_version */