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 /* If non-zero then the recirculation id that has
93 * been allocated for use with this rule.
94 * The recirculation id and associated internal flow should
95 * be freed when the rule is freed */
99 /* RULE_CAST() depends on this. */
100 BUILD_ASSERT_DECL(offsetof(struct rule_dpif, up) == 0);
102 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
103 long long int *used);
104 static struct rule_dpif *rule_dpif_cast(const struct rule *);
105 static void rule_expire(struct rule_dpif *);
112 * - Do include packets and bytes from datapath flows which have not
113 * recently been processed by a revalidator. */
114 struct ovs_mutex stats_mutex;
115 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
116 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
120 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
121 struct ofproto_dpif *ofproto; /* Owning ofproto. */
122 void *aux; /* Key supplied by ofproto's client. */
123 char *name; /* Identifier for log messages. */
126 struct ovs_list ports; /* Contains "struct ofport"s. */
127 enum port_vlan_mode vlan_mode; /* VLAN mode */
128 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
129 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
130 * NULL if all VLANs are trunked. */
131 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
132 struct bond *bond; /* Nonnull iff more than one port. */
133 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
136 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
139 static void bundle_remove(struct ofport *);
140 static void bundle_update(struct ofbundle *);
141 static void bundle_destroy(struct ofbundle *);
142 static void bundle_del_port(struct ofport_dpif *);
143 static void bundle_run(struct ofbundle *);
144 static void bundle_wait(struct ofbundle *);
145 static void bundle_flush_macs(struct ofbundle *, bool);
146 static void bundle_move(struct ofbundle *, struct ofbundle *);
148 static void stp_run(struct ofproto_dpif *ofproto);
149 static void stp_wait(struct ofproto_dpif *ofproto);
150 static int set_stp_port(struct ofport *,
151 const struct ofproto_port_stp_settings *);
153 static void rstp_run(struct ofproto_dpif *ofproto);
154 static void set_rstp_port(struct ofport *,
155 const struct ofproto_port_rstp_settings *);
158 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
162 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
163 struct ovs_list bundle_node;/* In struct ofbundle's "ports" list. */
164 struct cfm *cfm; /* Connectivity Fault Management, if any. */
165 struct bfd *bfd; /* BFD, if any. */
166 struct lldp *lldp; /* lldp, if any. */
167 bool may_enable; /* May be enabled in bonds. */
168 bool is_tunnel; /* This port is a tunnel. */
169 bool is_layer3; /* This is a layer 3 port. */
170 long long int carrier_seq; /* Carrier status changes. */
171 struct ofport_dpif *peer; /* Peer if patch port. */
174 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
175 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
176 long long int stp_state_entered;
178 /* Rapid Spanning Tree. */
179 struct rstp_port *rstp_port; /* Rapid Spanning Tree Protocol, if any. */
180 enum rstp_state rstp_state; /* Always RSTP_DISABLED if RSTP not in use. */
182 /* Queue to DSCP mapping. */
183 struct ofproto_port_queue *qdscp;
186 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
188 * This is deprecated. It is only for compatibility with broken device
189 * drivers in old versions of Linux that do not properly support VLANs when
190 * VLAN devices are not used. When broken device drivers are no longer in
191 * widespread use, we will delete these interfaces. */
192 ofp_port_t realdev_ofp_port;
196 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
198 * This is deprecated. It is only for compatibility with broken device drivers
199 * in old versions of Linux that do not properly support VLANs when VLAN
200 * devices are not used. When broken device drivers are no longer in
201 * widespread use, we will delete these interfaces. */
202 struct vlan_splinter {
203 struct hmap_node realdev_vid_node;
204 struct hmap_node vlandev_node;
205 ofp_port_t realdev_ofp_port;
206 ofp_port_t vlandev_ofp_port;
210 static void vsp_remove(struct ofport_dpif *);
211 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
213 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
216 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
219 static struct ofport_dpif *
220 ofport_dpif_cast(const struct ofport *ofport)
222 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
225 static void port_run(struct ofport_dpif *);
226 static int set_bfd(struct ofport *, const struct smap *);
227 static int set_cfm(struct ofport *, const struct cfm_settings *);
228 static int set_lldp(struct ofport *ofport_, const struct smap *cfg);
229 static void ofport_update_peer(struct ofport_dpif *);
231 /* Reasons that we might need to revalidate every datapath flow, and
232 * corresponding coverage counters.
234 * A value of 0 means that there is no need to revalidate.
236 * It would be nice to have some cleaner way to integrate with coverage
237 * counters, but with only a few reasons I guess this is good enough for
239 enum revalidate_reason {
240 REV_RECONFIGURE = 1, /* Switch configuration changed. */
241 REV_STP, /* Spanning tree protocol port status change. */
242 REV_RSTP, /* RSTP port status change. */
243 REV_BOND, /* Bonding changed. */
244 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
245 REV_FLOW_TABLE, /* Flow table changed. */
246 REV_MAC_LEARNING, /* Mac learning changed. */
247 REV_MCAST_SNOOPING, /* Multicast snooping changed. */
249 COVERAGE_DEFINE(rev_reconfigure);
250 COVERAGE_DEFINE(rev_stp);
251 COVERAGE_DEFINE(rev_rstp);
252 COVERAGE_DEFINE(rev_bond);
253 COVERAGE_DEFINE(rev_port_toggled);
254 COVERAGE_DEFINE(rev_flow_table);
255 COVERAGE_DEFINE(rev_mac_learning);
256 COVERAGE_DEFINE(rev_mcast_snooping);
258 /* Stores mapping between 'recirc_id' and 'ofproto-dpif'. */
259 struct dpif_backer_recirc_node {
260 struct cmap_node cmap_node;
261 struct ofproto_dpif *ofproto;
265 /* All datapaths of a given type share a single dpif backer instance. */
272 struct ovs_rwlock odp_to_ofport_lock;
273 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
275 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
277 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
279 bool recv_set_enable; /* Enables or disables receiving packets. */
282 struct recirc_id_pool *rid_pool; /* Recirculation ID pool. */
283 struct cmap recirc_map; /* Map of 'recirc_id's to 'ofproto's. */
284 struct ovs_mutex recirc_mutex; /* Protects 'recirc_map'. */
285 bool enable_recirc; /* True if the datapath supports recirculation */
287 /* True if the datapath supports unique flow identifiers */
290 /* True if the datapath supports variable-length
291 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
292 * False if the datapath supports only 8-byte (or shorter) userdata. */
293 bool variable_length_userdata;
295 /* True if the datapath supports masked data in OVS_ACTION_ATTR_SET
297 bool masked_set_action;
299 /* Maximum number of MPLS label stack entries that the datapath supports
301 size_t max_mpls_depth;
303 /* Version string of the datapath stored in OVSDB. */
304 char *dp_version_string;
306 /* True if the datapath supports tnl_push and pop actions. */
307 bool enable_tnl_push_pop;
308 struct atomic_count tnl_count;
311 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
312 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
314 struct ofproto_dpif {
315 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
317 struct dpif_backer *backer;
319 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
321 /* Special OpenFlow rules. */
322 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
323 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
324 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
327 struct netflow *netflow;
328 struct dpif_sflow *sflow;
329 struct dpif_ipfix *ipfix;
330 struct hmap bundles; /* Contains "struct ofbundle"s. */
331 struct mac_learning *ml;
332 struct mcast_snooping *ms;
333 bool has_bonded_bundles;
335 struct mbridge *mbridge;
337 struct ovs_mutex stats_mutex;
338 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
339 * consumed in userspace. */
343 long long int stp_last_tick;
345 /* Rapid Spanning Tree. */
347 long long int rstp_last_tick;
349 /* VLAN splinters. */
350 struct ovs_mutex vsp_mutex;
351 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
352 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
355 struct sset ports; /* Set of standard port names. */
356 struct sset ghost_ports; /* Ports with no datapath port. */
357 struct sset port_poll_set; /* Queued names for port_poll() reply. */
358 int port_poll_errno; /* Last errno for port_poll() reply. */
359 uint64_t change_seq; /* Connectivity status changes. */
362 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
363 struct seq *pins_seq; /* For notifying 'pins' reception. */
367 /* All existing ofproto_dpif instances, indexed by ->up.name. */
368 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
370 static bool ofproto_use_tnl_push_pop = true;
371 static void ofproto_unixctl_init(void);
373 static inline struct ofproto_dpif *
374 ofproto_dpif_cast(const struct ofproto *ofproto)
376 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
377 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
381 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
383 return ofproto->backer->max_mpls_depth;
387 ofproto_dpif_get_enable_recirc(const struct ofproto_dpif *ofproto)
389 return ofproto->backer->enable_recirc;
393 ofproto_dpif_get_enable_ufid(struct dpif_backer *backer)
395 return backer->enable_ufid;
398 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
399 ofp_port_t ofp_port);
400 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
401 const struct dp_packet *packet,
402 const struct ofpact[], size_t ofpacts_len,
405 /* Global variables. */
406 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
408 /* Initial mappings of port to bridge mappings. */
409 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
411 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
414 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
415 struct ofputil_flow_mod *fm)
417 ofproto_flow_mod(&ofproto->up, fm);
420 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
421 * Takes ownership of 'pin' and pin->packet. */
423 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
424 struct ofproto_packet_in *pin)
426 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
427 COVERAGE_INC(packet_in_overflow);
428 free(CONST_CAST(void *, pin->up.packet));
432 /* Wakes up main thread for packet-in I/O. */
433 seq_change(ofproto->pins_seq);
436 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
437 * packet rather than to send the packet to the controller.
439 * This function returns false to indicate that a packet_in message
440 * for a "table-miss" should be sent to at least one controller.
441 * False otherwise. */
443 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
445 return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
448 /* Factory functions. */
451 init(const struct shash *iface_hints)
453 struct shash_node *node;
455 /* Make a local copy, since we don't own 'iface_hints' elements. */
456 SHASH_FOR_EACH(node, iface_hints) {
457 const struct iface_hint *orig_hint = node->data;
458 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
460 new_hint->br_name = xstrdup(orig_hint->br_name);
461 new_hint->br_type = xstrdup(orig_hint->br_type);
462 new_hint->ofp_port = orig_hint->ofp_port;
464 shash_add(&init_ofp_ports, node->name, new_hint);
469 enumerate_types(struct sset *types)
471 dp_enumerate_types(types);
475 enumerate_names(const char *type, struct sset *names)
477 struct ofproto_dpif *ofproto;
480 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
481 if (strcmp(type, ofproto->up.type)) {
484 sset_add(names, ofproto->up.name);
491 del(const char *type, const char *name)
496 error = dpif_open(name, type, &dpif);
498 error = dpif_delete(dpif);
505 port_open_type(const char *datapath_type, const char *port_type)
507 return dpif_port_open_type(datapath_type, port_type);
510 /* Type functions. */
512 static void process_dpif_port_changes(struct dpif_backer *);
513 static void process_dpif_all_ports_changed(struct dpif_backer *);
514 static void process_dpif_port_change(struct dpif_backer *,
515 const char *devname);
516 static void process_dpif_port_error(struct dpif_backer *, int error);
518 static struct ofproto_dpif *
519 lookup_ofproto_dpif_by_port_name(const char *name)
521 struct ofproto_dpif *ofproto;
523 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
524 if (sset_contains(&ofproto->ports, name)) {
533 type_run(const char *type)
535 struct dpif_backer *backer;
537 backer = shash_find_data(&all_dpif_backers, type);
539 /* This is not necessarily a problem, since backers are only
540 * created on demand. */
545 if (dpif_run(backer->dpif)) {
546 backer->need_revalidate = REV_RECONFIGURE;
549 udpif_run(backer->udpif);
551 /* If vswitchd started with other_config:flow_restore_wait set as "true",
552 * and the configuration has now changed to "false", enable receiving
553 * packets from the datapath. */
554 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
557 backer->recv_set_enable = true;
559 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
561 VLOG_ERR("Failed to enable receiving packets in dpif.");
564 dpif_flow_flush(backer->dpif);
565 backer->need_revalidate = REV_RECONFIGURE;
568 if (backer->recv_set_enable) {
569 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
572 dpif_poll_threads_set(backer->dpif, n_dpdk_rxqs, pmd_cpu_mask);
574 if (backer->need_revalidate) {
575 struct ofproto_dpif *ofproto;
576 struct simap_node *node;
577 struct simap tmp_backers;
579 /* Handle tunnel garbage collection. */
580 simap_init(&tmp_backers);
581 simap_swap(&backer->tnl_backers, &tmp_backers);
583 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
584 struct ofport_dpif *iter;
586 if (backer != ofproto->backer) {
590 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
591 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
594 if (!iter->is_tunnel) {
598 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
599 namebuf, sizeof namebuf);
600 node = simap_find(&tmp_backers, dp_port);
602 simap_put(&backer->tnl_backers, dp_port, node->data);
603 simap_delete(&tmp_backers, node);
604 node = simap_find(&backer->tnl_backers, dp_port);
606 node = simap_find(&backer->tnl_backers, dp_port);
608 odp_port_t odp_port = ODPP_NONE;
610 if (!dpif_port_add(backer->dpif, iter->up.netdev,
612 simap_put(&backer->tnl_backers, dp_port,
613 odp_to_u32(odp_port));
614 node = simap_find(&backer->tnl_backers, dp_port);
619 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
620 if (tnl_port_reconfigure(iter, iter->up.netdev,
622 ovs_native_tunneling_is_on(ofproto), dp_port)) {
623 backer->need_revalidate = REV_RECONFIGURE;
628 SIMAP_FOR_EACH (node, &tmp_backers) {
629 dpif_port_del(backer->dpif, u32_to_odp(node->data));
631 simap_destroy(&tmp_backers);
633 switch (backer->need_revalidate) {
634 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
635 case REV_STP: COVERAGE_INC(rev_stp); break;
636 case REV_RSTP: COVERAGE_INC(rev_rstp); break;
637 case REV_BOND: COVERAGE_INC(rev_bond); break;
638 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
639 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
640 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
641 case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
643 backer->need_revalidate = 0;
645 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
646 struct ofport_dpif *ofport;
647 struct ofbundle *bundle;
649 if (ofproto->backer != backer) {
654 xlate_ofproto_set(ofproto, ofproto->up.name,
655 ofproto->backer->dpif, ofproto->ml,
656 ofproto->stp, ofproto->rstp, ofproto->ms,
657 ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
659 ofproto->up.forward_bpdu,
660 connmgr_has_in_band(ofproto->up.connmgr),
661 ofproto->backer->enable_recirc,
662 ofproto->backer->variable_length_userdata,
663 ofproto->backer->max_mpls_depth,
664 ofproto->backer->masked_set_action);
666 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
667 xlate_bundle_set(ofproto, bundle, bundle->name,
668 bundle->vlan_mode, bundle->vlan,
669 bundle->trunks, bundle->use_priority_tags,
670 bundle->bond, bundle->lacp,
674 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
675 int stp_port = ofport->stp_port
676 ? stp_port_no(ofport->stp_port)
678 xlate_ofport_set(ofproto, ofport->bundle, ofport,
679 ofport->up.ofp_port, ofport->odp_port,
680 ofport->up.netdev, ofport->cfm, ofport->bfd,
681 ofport->lldp, ofport->peer, stp_port,
682 ofport->rstp_port, ofport->qdscp,
683 ofport->n_qdscp, ofport->up.pp.config,
684 ofport->up.pp.state, ofport->is_tunnel,
690 udpif_revalidate(backer->udpif);
693 process_dpif_port_changes(backer);
698 /* Check for and handle port changes in 'backer''s dpif. */
700 process_dpif_port_changes(struct dpif_backer *backer)
706 error = dpif_port_poll(backer->dpif, &devname);
712 process_dpif_all_ports_changed(backer);
716 process_dpif_port_change(backer, devname);
721 process_dpif_port_error(backer, error);
728 process_dpif_all_ports_changed(struct dpif_backer *backer)
730 struct ofproto_dpif *ofproto;
731 struct dpif_port dpif_port;
732 struct dpif_port_dump dump;
733 struct sset devnames;
736 sset_init(&devnames);
737 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
738 if (ofproto->backer == backer) {
739 struct ofport *ofport;
741 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
742 sset_add(&devnames, netdev_get_name(ofport->netdev));
746 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
747 sset_add(&devnames, dpif_port.name);
750 SSET_FOR_EACH (devname, &devnames) {
751 process_dpif_port_change(backer, devname);
753 sset_destroy(&devnames);
757 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
759 struct ofproto_dpif *ofproto;
760 struct dpif_port port;
762 /* Don't report on the datapath's device. */
763 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
767 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
768 &all_ofproto_dpifs) {
769 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
774 ofproto = lookup_ofproto_dpif_by_port_name(devname);
775 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
776 /* The port was removed. If we know the datapath,
777 * report it through poll_set(). If we don't, it may be
778 * notifying us of a removal we initiated, so ignore it.
779 * If there's a pending ENOBUFS, let it stand, since
780 * everything will be reevaluated. */
781 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
782 sset_add(&ofproto->port_poll_set, devname);
783 ofproto->port_poll_errno = 0;
785 } else if (!ofproto) {
786 /* The port was added, but we don't know with which
787 * ofproto we should associate it. Delete it. */
788 dpif_port_del(backer->dpif, port.port_no);
790 struct ofport_dpif *ofport;
792 ofport = ofport_dpif_cast(shash_find_data(
793 &ofproto->up.port_by_name, devname));
795 && ofport->odp_port != port.port_no
796 && !odp_port_to_ofport(backer, port.port_no))
798 /* 'ofport''s datapath port number has changed from
799 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
800 * structures to match. */
801 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
802 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
803 ofport->odp_port = port.port_no;
804 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
805 hash_odp_port(port.port_no));
806 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
807 backer->need_revalidate = REV_RECONFIGURE;
810 dpif_port_destroy(&port);
813 /* Propagate 'error' to all ofprotos based on 'backer'. */
815 process_dpif_port_error(struct dpif_backer *backer, int error)
817 struct ofproto_dpif *ofproto;
819 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
820 if (ofproto->backer == backer) {
821 sset_clear(&ofproto->port_poll_set);
822 ofproto->port_poll_errno = error;
828 type_wait(const char *type)
830 struct dpif_backer *backer;
832 backer = shash_find_data(&all_dpif_backers, type);
834 /* This is not necessarily a problem, since backers are only
835 * created on demand. */
839 dpif_wait(backer->dpif);
842 /* Basic life-cycle. */
844 static int add_internal_flows(struct ofproto_dpif *);
846 static struct ofproto *
849 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
854 dealloc(struct ofproto *ofproto_)
856 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
860 /* Called when 'ofproto' is destructed. Checks for and clears any
863 dpif_backer_recirc_clear_ofproto(struct dpif_backer *backer,
864 struct ofproto_dpif *ofproto)
866 struct dpif_backer_recirc_node *node;
868 ovs_mutex_lock(&backer->recirc_mutex);
869 CMAP_FOR_EACH (node, cmap_node, &backer->recirc_map) {
870 if (node->ofproto == ofproto) {
871 VLOG_ERR("recirc_id %"PRIu32", not freed when ofproto (%s) "
872 "is destructed", node->recirc_id, ofproto->up.name);
873 cmap_remove(&backer->recirc_map, &node->cmap_node,
875 ovsrcu_postpone(free, node);
878 ovs_mutex_unlock(&backer->recirc_mutex);
882 close_dpif_backer(struct dpif_backer *backer)
884 ovs_assert(backer->refcount > 0);
886 if (--backer->refcount) {
890 udpif_destroy(backer->udpif);
892 simap_destroy(&backer->tnl_backers);
893 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
894 hmap_destroy(&backer->odp_to_ofport_map);
895 shash_find_and_delete(&all_dpif_backers, backer->type);
896 recirc_id_pool_destroy(backer->rid_pool);
897 cmap_destroy(&backer->recirc_map);
898 ovs_mutex_destroy(&backer->recirc_mutex);
900 free(backer->dp_version_string);
901 dpif_close(backer->dpif);
905 /* Datapath port slated for removal from datapath. */
907 struct ovs_list list_node;
911 static bool check_variable_length_userdata(struct dpif_backer *backer);
912 static size_t check_max_mpls_depth(struct dpif_backer *backer);
913 static bool check_recirc(struct dpif_backer *backer);
914 static bool check_ufid(struct dpif_backer *backer);
915 static bool check_masked_set_action(struct dpif_backer *backer);
918 open_dpif_backer(const char *type, struct dpif_backer **backerp)
920 struct dpif_backer *backer;
921 struct dpif_port_dump port_dump;
922 struct dpif_port port;
923 struct shash_node *node;
924 struct ovs_list garbage_list;
925 struct odp_garbage *garbage, *next;
932 backer = shash_find_data(&all_dpif_backers, type);
939 backer_name = xasprintf("ovs-%s", type);
941 /* Remove any existing datapaths, since we assume we're the only
942 * userspace controlling the datapath. */
944 dp_enumerate_names(type, &names);
945 SSET_FOR_EACH(name, &names) {
946 struct dpif *old_dpif;
948 /* Don't remove our backer if it exists. */
949 if (!strcmp(name, backer_name)) {
953 if (dpif_open(name, type, &old_dpif)) {
954 VLOG_WARN("couldn't open old datapath %s to remove it", name);
956 dpif_delete(old_dpif);
957 dpif_close(old_dpif);
960 sset_destroy(&names);
962 backer = xmalloc(sizeof *backer);
964 error = dpif_create_and_open(backer_name, type, &backer->dpif);
967 VLOG_ERR("failed to open datapath of type %s: %s", type,
968 ovs_strerror(error));
972 backer->udpif = udpif_create(backer, backer->dpif);
974 backer->type = xstrdup(type);
975 backer->refcount = 1;
976 hmap_init(&backer->odp_to_ofport_map);
977 ovs_rwlock_init(&backer->odp_to_ofport_lock);
978 backer->need_revalidate = 0;
979 simap_init(&backer->tnl_backers);
980 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
983 if (backer->recv_set_enable) {
984 dpif_flow_flush(backer->dpif);
987 /* Loop through the ports already on the datapath and remove any
988 * that we don't need anymore. */
989 list_init(&garbage_list);
990 dpif_port_dump_start(&port_dump, backer->dpif);
991 while (dpif_port_dump_next(&port_dump, &port)) {
992 node = shash_find(&init_ofp_ports, port.name);
993 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
994 garbage = xmalloc(sizeof *garbage);
995 garbage->odp_port = port.port_no;
996 list_push_front(&garbage_list, &garbage->list_node);
999 dpif_port_dump_done(&port_dump);
1001 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
1002 dpif_port_del(backer->dpif, garbage->odp_port);
1003 list_remove(&garbage->list_node);
1007 shash_add(&all_dpif_backers, type, backer);
1009 backer->enable_recirc = check_recirc(backer);
1010 backer->max_mpls_depth = check_max_mpls_depth(backer);
1011 backer->masked_set_action = check_masked_set_action(backer);
1012 backer->enable_ufid = check_ufid(backer);
1013 backer->rid_pool = recirc_id_pool_create();
1014 ovs_mutex_init(&backer->recirc_mutex);
1015 cmap_init(&backer->recirc_map);
1017 backer->enable_tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
1018 atomic_count_init(&backer->tnl_count, 0);
1020 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
1022 VLOG_ERR("failed to listen on datapath of type %s: %s",
1023 type, ovs_strerror(error));
1024 close_dpif_backer(backer);
1028 if (backer->recv_set_enable) {
1029 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
1032 /* This check fails if performed before udpif threads have been set,
1033 * as the kernel module checks that the 'pid' in userspace action
1035 backer->variable_length_userdata = check_variable_length_userdata(backer);
1036 backer->dp_version_string = dpif_get_dp_version(backer->dpif);
1042 ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
1044 return ofproto_use_tnl_push_pop && ofproto->backer->enable_tnl_push_pop &&
1045 atomic_count_get(&ofproto->backer->tnl_count);
1048 /* Tests whether 'backer''s datapath supports recirculation. Only newer
1049 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
1050 * features on older datapaths that don't support this feature.
1052 * Returns false if 'backer' definitely does not support recirculation, true if
1053 * it seems to support recirculation or if at least the error we get is
1056 check_recirc(struct dpif_backer *backer)
1059 struct odputil_keybuf keybuf;
1063 memset(&flow, 0, sizeof flow);
1067 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1068 odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
1069 enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
1072 if (enable_recirc) {
1073 VLOG_INFO("%s: Datapath supports recirculation",
1074 dpif_name(backer->dpif));
1076 VLOG_INFO("%s: Datapath does not support recirculation",
1077 dpif_name(backer->dpif));
1080 return enable_recirc;
1083 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
1084 * some flow attributes for datapaths that support this feature.
1086 * Returns true if 'dpif' supports UFID for flow operations.
1087 * Returns false if 'dpif' does not support UFID. */
1089 check_ufid(struct dpif_backer *backer)
1092 struct odputil_keybuf keybuf;
1097 memset(&flow, 0, sizeof flow);
1098 flow.dl_type = htons(0x1234);
1100 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1101 odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
1102 dpif_flow_hash(backer->dpif, key.data, key.size, &ufid);
1104 enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, &ufid);
1107 VLOG_INFO("%s: Datapath supports unique flow ids",
1108 dpif_name(backer->dpif));
1110 VLOG_INFO("%s: Datapath does not support unique flow ids",
1111 dpif_name(backer->dpif));
1116 /* Tests whether 'backer''s datapath supports variable-length
1117 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1118 * to disable some features on older datapaths that don't support this
1121 * Returns false if 'backer' definitely does not support variable-length
1122 * userdata, true if it seems to support them or if at least the error we get
1125 check_variable_length_userdata(struct dpif_backer *backer)
1127 struct eth_header *eth;
1128 struct ofpbuf actions;
1129 struct dpif_execute execute;
1130 struct dp_packet packet;
1134 /* Compose a userspace action that will cause an ERANGE error on older
1135 * datapaths that don't support variable-length userdata.
1137 * We really test for using userdata longer than 8 bytes, but older
1138 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1139 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1140 * test for that instead as a proxy for longer userdata support. */
1141 ofpbuf_init(&actions, 64);
1142 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1143 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1144 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1145 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1146 nl_msg_end_nested(&actions, start);
1148 /* Compose a dummy ethernet packet. */
1149 dp_packet_init(&packet, ETH_HEADER_LEN);
1150 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1151 eth->eth_type = htons(0x1234);
1153 /* Execute the actions. On older datapaths this fails with ERANGE, on
1154 * newer datapaths it succeeds. */
1155 execute.actions = actions.data;
1156 execute.actions_len = actions.size;
1157 execute.packet = &packet;
1158 execute.needs_help = false;
1159 execute.probe = true;
1161 error = dpif_execute(backer->dpif, &execute);
1163 dp_packet_uninit(&packet);
1164 ofpbuf_uninit(&actions);
1171 /* Variable-length userdata is not supported. */
1172 VLOG_WARN("%s: datapath does not support variable-length userdata "
1173 "feature (needs Linux 3.10+ or kernel module from OVS "
1174 "1..11+). The NXAST_SAMPLE action will be ignored.",
1175 dpif_name(backer->dpif));
1179 /* Something odd happened. We're not sure whether variable-length
1180 * userdata is supported. Default to "yes". */
1181 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1182 dpif_name(backer->dpif), ovs_strerror(error));
1187 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1189 * Returns the number of elements in a struct flow's mpls_lse field
1190 * if the datapath supports at least that many entries in an
1192 * Otherwise returns the number of MPLS push actions supported by
1195 check_max_mpls_depth(struct dpif_backer *backer)
1200 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1201 struct odputil_keybuf keybuf;
1204 memset(&flow, 0, sizeof flow);
1205 flow.dl_type = htons(ETH_TYPE_MPLS);
1206 flow_set_mpls_bos(&flow, n, 1);
1208 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1209 odp_flow_key_from_flow(&key, &flow, NULL, 0, false);
1210 if (!dpif_probe_feature(backer->dpif, "MPLS", &key, NULL)) {
1215 VLOG_INFO("%s: MPLS label stack length probed as %d",
1216 dpif_name(backer->dpif), n);
1220 /* Tests whether 'backer''s datapath supports masked data in
1221 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1222 * datapaths that don't support this feature. */
1224 check_masked_set_action(struct dpif_backer *backer)
1226 struct eth_header *eth;
1227 struct ofpbuf actions;
1228 struct dpif_execute execute;
1229 struct dp_packet packet;
1231 struct ovs_key_ethernet key, mask;
1233 /* Compose a set action that will cause an EINVAL error on older
1234 * datapaths that don't support masked set actions.
1235 * Avoid using a full mask, as it could be translated to a non-masked
1236 * set action instead. */
1237 ofpbuf_init(&actions, 64);
1238 memset(&key, 0x53, sizeof key);
1239 memset(&mask, 0x7f, sizeof mask);
1240 commit_masked_set_action(&actions, OVS_KEY_ATTR_ETHERNET, &key, &mask,
1243 /* Compose a dummy ethernet packet. */
1244 dp_packet_init(&packet, ETH_HEADER_LEN);
1245 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1246 eth->eth_type = htons(0x1234);
1248 /* Execute the actions. On older datapaths this fails with EINVAL, on
1249 * newer datapaths it succeeds. */
1250 execute.actions = actions.data;
1251 execute.actions_len = actions.size;
1252 execute.packet = &packet;
1253 execute.needs_help = false;
1254 execute.probe = true;
1256 error = dpif_execute(backer->dpif, &execute);
1258 dp_packet_uninit(&packet);
1259 ofpbuf_uninit(&actions);
1262 /* Masked set action is not supported. */
1263 VLOG_INFO("%s: datapath does not support masked set action feature.",
1264 dpif_name(backer->dpif));
1270 construct(struct ofproto *ofproto_)
1272 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1273 struct shash_node *node, *next;
1276 /* Tunnel module can get used right after the udpif threads are running. */
1277 ofproto_tunnel_init();
1279 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1284 ofproto->netflow = NULL;
1285 ofproto->sflow = NULL;
1286 ofproto->ipfix = NULL;
1287 ofproto->stp = NULL;
1288 ofproto->rstp = NULL;
1289 ofproto->dump_seq = 0;
1290 hmap_init(&ofproto->bundles);
1291 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1293 ofproto->mbridge = mbridge_create();
1294 ofproto->has_bonded_bundles = false;
1295 ofproto->lacp_enabled = false;
1296 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1297 ovs_mutex_init(&ofproto->vsp_mutex);
1299 guarded_list_init(&ofproto->pins);
1301 ofproto_unixctl_init();
1303 hmap_init(&ofproto->vlandev_map);
1304 hmap_init(&ofproto->realdev_vid_map);
1306 sset_init(&ofproto->ports);
1307 sset_init(&ofproto->ghost_ports);
1308 sset_init(&ofproto->port_poll_set);
1309 ofproto->port_poll_errno = 0;
1310 ofproto->change_seq = 0;
1311 ofproto->pins_seq = seq_create();
1312 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1315 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1316 struct iface_hint *iface_hint = node->data;
1318 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1319 /* Check if the datapath already has this port. */
1320 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1321 sset_add(&ofproto->ports, node->name);
1324 free(iface_hint->br_name);
1325 free(iface_hint->br_type);
1327 shash_delete(&init_ofp_ports, node);
1331 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1332 hash_string(ofproto->up.name, 0));
1333 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1335 ofproto_init_tables(ofproto_, N_TABLES);
1336 error = add_internal_flows(ofproto);
1338 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1344 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1345 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1351 match_init_catchall(&match);
1352 match_set_reg(&match, 0, id);
1354 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1356 *rulep = error ? NULL : rule_dpif_cast(rule);
1362 add_internal_flows(struct ofproto_dpif *ofproto)
1364 struct ofpact_controller *controller;
1365 uint64_t ofpacts_stub[128 / 8];
1366 struct ofpbuf ofpacts;
1367 struct rule *unused_rulep OVS_UNUSED;
1368 struct ofpact_resubmit *resubmit;
1373 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1376 controller = ofpact_put_CONTROLLER(&ofpacts);
1377 controller->max_len = UINT16_MAX;
1378 controller->controller_id = 0;
1379 controller->reason = OFPR_NO_MATCH;
1380 ofpact_pad(&ofpacts);
1382 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1383 &ofproto->miss_rule);
1388 ofpbuf_clear(&ofpacts);
1389 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1390 &ofproto->no_packet_in_rule);
1395 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1396 &ofproto->drop_frags_rule);
1401 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1402 * zero when reaching this rule.
1404 * (priority=2), recirc_id=0, actions=drop
1406 ofpbuf_clear(&ofpacts);
1407 match_init_catchall(&match);
1408 match_set_recirc_id(&match, 0);
1409 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1415 /* Continue rule lookups for not-matched recirc rules from table 0.
1417 * (priority=1), actions=resubmit(, 0)
1419 resubmit = ofpact_put_RESUBMIT(&ofpacts);
1420 resubmit->in_port = OFPP_IN_PORT;
1421 resubmit->table_id = 0;
1423 match_init_catchall(&match);
1424 error = ofproto_dpif_add_internal_flow(ofproto, &match, 1, 0, &ofpacts,
1431 destruct(struct ofproto *ofproto_)
1433 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1434 struct ofproto_packet_in *pin, *next_pin;
1435 struct rule_dpif *rule;
1436 struct oftable *table;
1437 struct ovs_list pins;
1439 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1441 xlate_remove_ofproto(ofproto);
1444 /* Ensure that the upcall processing threads have no remaining references
1445 * to the ofproto or anything in it. */
1446 udpif_synchronize(ofproto->backer->udpif);
1448 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1450 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1451 CLS_FOR_EACH (rule, up.cr, &table->cls) {
1452 ofproto_rule_delete(&ofproto->up, &rule->up);
1456 guarded_list_pop_all(&ofproto->pins, &pins);
1457 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1458 list_remove(&pin->list_node);
1459 free(CONST_CAST(void *, pin->up.packet));
1462 guarded_list_destroy(&ofproto->pins);
1464 dpif_backer_recirc_clear_ofproto(ofproto->backer, ofproto);
1466 mbridge_unref(ofproto->mbridge);
1468 netflow_unref(ofproto->netflow);
1469 dpif_sflow_unref(ofproto->sflow);
1470 dpif_ipfix_unref(ofproto->ipfix);
1471 hmap_destroy(&ofproto->bundles);
1472 mac_learning_unref(ofproto->ml);
1473 mcast_snooping_unref(ofproto->ms);
1475 hmap_destroy(&ofproto->vlandev_map);
1476 hmap_destroy(&ofproto->realdev_vid_map);
1478 sset_destroy(&ofproto->ports);
1479 sset_destroy(&ofproto->ghost_ports);
1480 sset_destroy(&ofproto->port_poll_set);
1482 ovs_mutex_destroy(&ofproto->stats_mutex);
1483 ovs_mutex_destroy(&ofproto->vsp_mutex);
1485 seq_destroy(ofproto->pins_seq);
1487 close_dpif_backer(ofproto->backer);
1491 run(struct ofproto *ofproto_)
1493 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1494 uint64_t new_seq, new_dump_seq;
1496 if (mbridge_need_revalidate(ofproto->mbridge)) {
1497 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1498 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1499 mac_learning_flush(ofproto->ml);
1500 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1501 mcast_snooping_mdb_flush(ofproto->ms);
1504 /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1505 * flow restore. Even though nothing is processed during flow restore,
1506 * all queued 'pins' will be handled immediately when flow restore
1508 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1510 /* Do not perform any periodic activity required by 'ofproto' while
1511 * waiting for flow restore to complete. */
1512 if (!ofproto_get_flow_restore_wait()) {
1513 struct ofproto_packet_in *pin, *next_pin;
1514 struct ovs_list pins;
1516 guarded_list_pop_all(&ofproto->pins, &pins);
1517 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1518 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1519 list_remove(&pin->list_node);
1520 free(CONST_CAST(void *, pin->up.packet));
1525 if (ofproto->netflow) {
1526 netflow_run(ofproto->netflow);
1528 if (ofproto->sflow) {
1529 dpif_sflow_run(ofproto->sflow);
1531 if (ofproto->ipfix) {
1532 dpif_ipfix_run(ofproto->ipfix);
1535 new_seq = seq_read(connectivity_seq_get());
1536 if (ofproto->change_seq != new_seq) {
1537 struct ofport_dpif *ofport;
1539 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1543 ofproto->change_seq = new_seq;
1545 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1546 struct ofbundle *bundle;
1548 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1555 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1556 if (mac_learning_run(ofproto->ml)) {
1557 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1559 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1561 if (mcast_snooping_run(ofproto->ms)) {
1562 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1565 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1566 if (ofproto->dump_seq != new_dump_seq) {
1567 struct rule *rule, *next_rule;
1569 /* We know stats are relatively fresh, so now is a good time to do some
1571 ofproto->dump_seq = new_dump_seq;
1573 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1575 ovs_mutex_lock(&ofproto_mutex);
1576 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1577 &ofproto->up.expirable) {
1578 rule_expire(rule_dpif_cast(rule));
1580 ovs_mutex_unlock(&ofproto_mutex);
1582 /* All outstanding data in existing flows has been accounted, so it's a
1583 * good time to do bond rebalancing. */
1584 if (ofproto->has_bonded_bundles) {
1585 struct ofbundle *bundle;
1587 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1589 bond_rebalance(bundle->bond);
1598 wait(struct ofproto *ofproto_)
1600 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1602 if (ofproto_get_flow_restore_wait()) {
1606 if (ofproto->sflow) {
1607 dpif_sflow_wait(ofproto->sflow);
1609 if (ofproto->ipfix) {
1610 dpif_ipfix_wait(ofproto->ipfix);
1612 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1613 struct ofbundle *bundle;
1615 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1616 bundle_wait(bundle);
1619 if (ofproto->netflow) {
1620 netflow_wait(ofproto->netflow);
1622 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1623 mac_learning_wait(ofproto->ml);
1624 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1625 mcast_snooping_wait(ofproto->ms);
1627 if (ofproto->backer->need_revalidate) {
1628 /* Shouldn't happen, but if it does just go around again. */
1629 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1630 poll_immediate_wake();
1633 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1634 seq_wait(ofproto->pins_seq, ofproto->pins_seqno);
1638 type_get_memory_usage(const char *type, struct simap *usage)
1640 struct dpif_backer *backer;
1642 backer = shash_find_data(&all_dpif_backers, type);
1644 udpif_get_memory_usage(backer->udpif, usage);
1649 flush(struct ofproto *ofproto_)
1651 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1652 struct dpif_backer *backer = ofproto->backer;
1655 udpif_flush(backer->udpif);
1660 query_tables(struct ofproto *ofproto,
1661 struct ofputil_table_features *features,
1662 struct ofputil_table_stats *stats)
1664 strcpy(features->name, "classifier");
1669 for (i = 0; i < ofproto->n_tables; i++) {
1670 unsigned long missed, matched;
1672 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1673 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1675 stats[i].matched_count = matched;
1676 stats[i].lookup_count = matched + missed;
1681 static struct ofport *
1684 struct ofport_dpif *port = xmalloc(sizeof *port);
1689 port_dealloc(struct ofport *port_)
1691 struct ofport_dpif *port = ofport_dpif_cast(port_);
1696 port_construct(struct ofport *port_)
1698 struct ofport_dpif *port = ofport_dpif_cast(port_);
1699 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1700 const struct netdev *netdev = port->up.netdev;
1701 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1702 struct dpif_port dpif_port;
1705 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1706 port->bundle = NULL;
1710 port->may_enable = false;
1711 port->stp_port = NULL;
1712 port->stp_state = STP_DISABLED;
1713 port->rstp_port = NULL;
1714 port->rstp_state = RSTP_DISABLED;
1715 port->is_tunnel = false;
1719 port->realdev_ofp_port = 0;
1720 port->vlandev_vid = 0;
1721 port->carrier_seq = netdev_get_carrier_resets(netdev);
1722 port->is_layer3 = netdev_vport_is_layer3(netdev);
1724 if (netdev_vport_is_patch(netdev)) {
1725 /* By bailing out here, we don't submit the port to the sFlow module
1726 * to be considered for counter polling export. This is correct
1727 * because the patch port represents an interface that sFlow considers
1728 * to be "internal" to the switch as a whole, and therefore not a
1729 * candidate for counter polling. */
1730 port->odp_port = ODPP_NONE;
1731 ofport_update_peer(port);
1735 error = dpif_port_query_by_name(ofproto->backer->dpif,
1736 netdev_vport_get_dpif_port(netdev, namebuf,
1743 port->odp_port = dpif_port.port_no;
1745 if (netdev_get_tunnel_config(netdev)) {
1746 atomic_count_inc(&ofproto->backer->tnl_count);
1747 tnl_port_add(port, port->up.netdev, port->odp_port,
1748 ovs_native_tunneling_is_on(ofproto), namebuf);
1749 port->is_tunnel = true;
1750 if (ofproto->ipfix) {
1751 dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1754 /* Sanity-check that a mapping doesn't already exist. This
1755 * shouldn't happen for non-tunnel ports. */
1756 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1757 VLOG_ERR("port %s already has an OpenFlow port number",
1759 dpif_port_destroy(&dpif_port);
1763 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1764 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1765 hash_odp_port(port->odp_port));
1766 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1768 dpif_port_destroy(&dpif_port);
1770 if (ofproto->sflow) {
1771 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1778 port_destruct(struct ofport *port_)
1780 struct ofport_dpif *port = ofport_dpif_cast(port_);
1781 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1782 const char *devname = netdev_get_name(port->up.netdev);
1783 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1784 const char *dp_port_name;
1786 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1788 xlate_ofport_remove(port);
1791 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1793 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1794 /* The underlying device is still there, so delete it. This
1795 * happens when the ofproto is being destroyed, since the caller
1796 * assumes that removal of attached ports will happen as part of
1798 if (!port->is_tunnel) {
1799 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1804 port->peer->peer = NULL;
1808 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1809 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1810 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1811 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1814 if (port->is_tunnel) {
1815 atomic_count_dec(&ofproto->backer->tnl_count);
1818 if (port->is_tunnel && ofproto->ipfix) {
1819 dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1823 sset_find_and_delete(&ofproto->ports, devname);
1824 sset_find_and_delete(&ofproto->ghost_ports, devname);
1825 bundle_remove(port_);
1826 set_cfm(port_, NULL);
1827 set_bfd(port_, NULL);
1828 set_lldp(port_, NULL);
1829 if (port->stp_port) {
1830 stp_port_disable(port->stp_port);
1832 set_rstp_port(port_, NULL);
1833 if (ofproto->sflow) {
1834 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1841 port_modified(struct ofport *port_)
1843 struct ofport_dpif *port = ofport_dpif_cast(port_);
1844 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1845 struct netdev *netdev = port->up.netdev;
1847 if (port->bundle && port->bundle->bond) {
1848 bond_slave_set_netdev(port->bundle->bond, port, netdev);
1852 cfm_set_netdev(port->cfm, netdev);
1856 bfd_set_netdev(port->bfd, netdev);
1859 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1860 port->lldp, port->up.pp.hw_addr);
1862 netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1864 if (port->is_tunnel) {
1865 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1867 if (tnl_port_reconfigure(port, netdev, port->odp_port,
1868 ovs_native_tunneling_is_on(ofproto), namebuf)) {
1869 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1873 ofport_update_peer(port);
1877 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1879 struct ofport_dpif *port = ofport_dpif_cast(port_);
1880 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1881 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1883 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1884 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1885 OFPUTIL_PC_NO_PACKET_IN)) {
1886 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1888 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1889 bundle_update(port->bundle);
1895 set_sflow(struct ofproto *ofproto_,
1896 const struct ofproto_sflow_options *sflow_options)
1898 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1899 struct dpif_sflow *ds = ofproto->sflow;
1901 if (sflow_options) {
1902 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
1904 struct ofport_dpif *ofport;
1906 ds = ofproto->sflow = dpif_sflow_create();
1907 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1908 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1911 dpif_sflow_set_options(ds, sflow_options);
1912 if (dpif_sflow_get_probability(ds) != old_probability) {
1913 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1917 dpif_sflow_unref(ds);
1918 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1919 ofproto->sflow = NULL;
1927 struct ofproto *ofproto_,
1928 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1929 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1930 size_t n_flow_exporters_options)
1932 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1933 struct dpif_ipfix *di = ofproto->ipfix;
1934 bool has_options = bridge_exporter_options || flow_exporters_options;
1935 bool new_di = false;
1937 if (has_options && !di) {
1938 di = ofproto->ipfix = dpif_ipfix_create();
1943 /* Call set_options in any case to cleanly flush the flow
1944 * caches in the last exporters that are to be destroyed. */
1945 dpif_ipfix_set_options(
1946 di, bridge_exporter_options, flow_exporters_options,
1947 n_flow_exporters_options);
1949 /* Add tunnel ports only when a new ipfix created */
1950 if (new_di == true) {
1951 struct ofport_dpif *ofport;
1952 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1953 if (ofport->is_tunnel == true) {
1954 dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
1960 dpif_ipfix_unref(di);
1961 ofproto->ipfix = NULL;
1969 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1971 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1976 struct ofproto_dpif *ofproto;
1978 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1979 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1980 ofport->cfm = cfm_create(ofport->up.netdev);
1983 if (cfm_configure(ofport->cfm, s)) {
1990 cfm_unref(ofport->cfm);
1993 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1994 ofport->lldp, ofport->up.pp.hw_addr);
1999 cfm_status_changed(struct ofport *ofport_)
2001 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2003 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
2007 get_cfm_status(const struct ofport *ofport_,
2008 struct cfm_status *status)
2010 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2014 cfm_get_status(ofport->cfm, status);
2023 set_bfd(struct ofport *ofport_, const struct smap *cfg)
2025 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2026 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2030 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2031 cfg, ofport->up.netdev);
2032 if (ofport->bfd != old) {
2033 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2035 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
2036 ofport->lldp, ofport->up.pp.hw_addr);
2041 bfd_status_changed(struct ofport *ofport_)
2043 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2045 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
2049 get_bfd_status(struct ofport *ofport_, struct smap *smap)
2051 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2055 bfd_get_status(ofport->bfd, smap);
2064 set_lldp(struct ofport *ofport_,
2065 const struct smap *cfg)
2067 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2071 if (!ofport->lldp) {
2072 struct ofproto_dpif *ofproto;
2074 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2075 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2076 ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2079 if (lldp_configure(ofport->lldp)) {
2086 lldp_unref(ofport->lldp);
2087 ofport->lldp = NULL;
2089 ofproto_dpif_monitor_port_update(ofport,
2093 ofport->up.pp.hw_addr);
2098 get_lldp_status(const struct ofport *ofport_,
2099 struct lldp_status *status OVS_UNUSED)
2101 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2103 return ofport->lldp ? true : false;
2107 set_aa(struct ofproto *ofproto OVS_UNUSED,
2108 const struct aa_settings *s)
2110 return aa_configure(s);
2114 aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2115 const struct aa_mapping_settings *s)
2117 return aa_mapping_register(aux, s);
2121 aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2123 return aa_mapping_unregister(aux);
2127 aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2129 return aa_get_vlan_queued(list);
2133 aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2135 return aa_get_vlan_queue_size();
2139 /* Spanning Tree. */
2141 /* Called while rstp_mutex is held. */
2143 rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
2145 struct ofproto_dpif *ofproto = ofproto_;
2146 struct ofport_dpif *ofport = ofport_;
2147 struct eth_header *eth = dp_packet_l2(pkt);
2149 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2150 if (eth_addr_is_zero(eth->eth_src)) {
2151 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2152 "does not have a configured source MAC address.",
2153 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
2155 ofproto_dpif_send_packet(ofport, pkt);
2157 dp_packet_delete(pkt);
2161 send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
2163 struct ofproto_dpif *ofproto = ofproto_;
2164 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2165 struct ofport_dpif *ofport;
2167 ofport = stp_port_get_aux(sp);
2169 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2170 ofproto->up.name, port_num);
2172 struct eth_header *eth = dp_packet_l2(pkt);
2174 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2175 if (eth_addr_is_zero(eth->eth_src)) {
2176 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2177 "with unknown MAC", ofproto->up.name, port_num);
2179 ofproto_dpif_send_packet(ofport, pkt);
2182 dp_packet_delete(pkt);
2185 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2187 set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2189 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2191 /* Only revalidate flows if the configuration changed. */
2192 if (!s != !ofproto->rstp) {
2193 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2197 if (!ofproto->rstp) {
2198 ofproto->rstp = rstp_create(ofproto_->name, s->address,
2199 rstp_send_bpdu_cb, ofproto);
2200 ofproto->rstp_last_tick = time_msec();
2202 rstp_set_bridge_address(ofproto->rstp, s->address);
2203 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2204 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2205 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2206 s->force_protocol_version);
2207 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2208 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2209 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2210 s->transmit_hold_count);
2212 struct ofport *ofport;
2213 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2214 set_rstp_port(ofport, NULL);
2216 rstp_unref(ofproto->rstp);
2217 ofproto->rstp = NULL;
2222 get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2224 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2226 if (ofproto->rstp) {
2228 s->root_id = rstp_get_root_id(ofproto->rstp);
2229 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2230 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2231 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2232 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2233 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2240 update_rstp_port_state(struct ofport_dpif *ofport)
2242 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2243 enum rstp_state state;
2245 /* Figure out new state. */
2246 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2250 if (ofport->rstp_state != state) {
2251 enum ofputil_port_state of_state;
2254 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2255 netdev_get_name(ofport->up.netdev),
2256 rstp_state_name(ofport->rstp_state),
2257 rstp_state_name(state));
2259 if (rstp_learn_in_state(ofport->rstp_state)
2260 != rstp_learn_in_state(state)) {
2261 /* XXX: Learning action flows should also be flushed. */
2262 if (ofport->bundle) {
2263 if (!rstp_shift_root_learned_address(ofproto->rstp)
2264 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2265 bundle_flush_macs(ofport->bundle, false);
2269 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2270 != rstp_forward_in_state(state);
2272 ofproto->backer->need_revalidate = REV_RSTP;
2273 ofport->rstp_state = state;
2275 if (fwd_change && ofport->bundle) {
2276 bundle_update(ofport->bundle);
2279 /* Update the RSTP state bits in the OpenFlow port description. */
2280 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2281 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2282 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2283 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2285 ofproto_port_set_state(&ofport->up, of_state);
2290 rstp_run(struct ofproto_dpif *ofproto)
2292 if (ofproto->rstp) {
2293 long long int now = time_msec();
2294 long long int elapsed = now - ofproto->rstp_last_tick;
2295 struct rstp_port *rp;
2296 struct ofport_dpif *ofport;
2298 /* Every second, decrease the values of the timers. */
2299 if (elapsed >= 1000) {
2300 rstp_tick_timers(ofproto->rstp);
2301 ofproto->rstp_last_tick = now;
2304 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2305 update_rstp_port_state(ofport);
2309 /* FIXME: This check should be done on-event (i.e., when setting
2310 * p->fdb_flush) and not periodically.
2312 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2313 if (!rstp_shift_root_learned_address(ofproto->rstp)
2314 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2315 bundle_flush_macs(ofport->bundle, false);
2319 if (rstp_shift_root_learned_address(ofproto->rstp)) {
2320 bundle_move(((struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp))->bundle,
2321 ((struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp))->bundle);
2322 rstp_reset_root_changed(ofproto->rstp);
2327 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2329 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2331 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2333 /* Only revalidate flows if the configuration changed. */
2334 if (!s != !ofproto->stp) {
2335 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2339 if (!ofproto->stp) {
2340 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2341 send_bpdu_cb, ofproto);
2342 ofproto->stp_last_tick = time_msec();
2345 stp_set_bridge_id(ofproto->stp, s->system_id);
2346 stp_set_bridge_priority(ofproto->stp, s->priority);
2347 stp_set_hello_time(ofproto->stp, s->hello_time);
2348 stp_set_max_age(ofproto->stp, s->max_age);
2349 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2351 struct ofport *ofport;
2353 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2354 set_stp_port(ofport, NULL);
2357 stp_unref(ofproto->stp);
2358 ofproto->stp = NULL;
2365 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2367 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2371 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2372 s->designated_root = stp_get_designated_root(ofproto->stp);
2373 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2382 update_stp_port_state(struct ofport_dpif *ofport)
2384 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2385 enum stp_state state;
2387 /* Figure out new state. */
2388 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2392 if (ofport->stp_state != state) {
2393 enum ofputil_port_state of_state;
2396 VLOG_DBG("port %s: STP state changed from %s to %s",
2397 netdev_get_name(ofport->up.netdev),
2398 stp_state_name(ofport->stp_state),
2399 stp_state_name(state));
2400 if (stp_learn_in_state(ofport->stp_state)
2401 != stp_learn_in_state(state)) {
2402 /* xxx Learning action flows should also be flushed. */
2403 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2404 mac_learning_flush(ofproto->ml);
2405 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2406 mcast_snooping_mdb_flush(ofproto->ms);
2408 fwd_change = stp_forward_in_state(ofport->stp_state)
2409 != stp_forward_in_state(state);
2411 ofproto->backer->need_revalidate = REV_STP;
2412 ofport->stp_state = state;
2413 ofport->stp_state_entered = time_msec();
2415 if (fwd_change && ofport->bundle) {
2416 bundle_update(ofport->bundle);
2419 /* Update the STP state bits in the OpenFlow port description. */
2420 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2421 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2422 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2423 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2424 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2426 ofproto_port_set_state(&ofport->up, of_state);
2430 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2431 * caller is responsible for assigning STP port numbers and ensuring
2432 * there are no duplicates. */
2434 set_stp_port(struct ofport *ofport_,
2435 const struct ofproto_port_stp_settings *s)
2437 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2438 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2439 struct stp_port *sp = ofport->stp_port;
2441 if (!s || !s->enable) {
2443 ofport->stp_port = NULL;
2444 stp_port_disable(sp);
2445 update_stp_port_state(ofport);
2448 } else if (sp && stp_port_no(sp) != s->port_num
2449 && ofport == stp_port_get_aux(sp)) {
2450 /* The port-id changed, so disable the old one if it's not
2451 * already in use by another port. */
2452 stp_port_disable(sp);
2455 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2457 /* Set name before enabling the port so that debugging messages can print
2459 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2460 stp_port_enable(sp);
2462 stp_port_set_aux(sp, ofport);
2463 stp_port_set_priority(sp, s->priority);
2464 stp_port_set_path_cost(sp, s->path_cost);
2466 update_stp_port_state(ofport);
2472 get_stp_port_status(struct ofport *ofport_,
2473 struct ofproto_port_stp_status *s)
2475 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2476 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2477 struct stp_port *sp = ofport->stp_port;
2479 if (!ofproto->stp || !sp) {
2485 s->port_id = stp_port_get_id(sp);
2486 s->state = stp_port_get_state(sp);
2487 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2488 s->role = stp_port_get_role(sp);
2494 get_stp_port_stats(struct ofport *ofport_,
2495 struct ofproto_port_stp_stats *s)
2497 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2498 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2499 struct stp_port *sp = ofport->stp_port;
2501 if (!ofproto->stp || !sp) {
2507 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2513 stp_run(struct ofproto_dpif *ofproto)
2516 long long int now = time_msec();
2517 long long int elapsed = now - ofproto->stp_last_tick;
2518 struct stp_port *sp;
2521 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2522 ofproto->stp_last_tick = now;
2524 while (stp_get_changed_port(ofproto->stp, &sp)) {
2525 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2528 update_stp_port_state(ofport);
2532 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2533 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2534 mac_learning_flush(ofproto->ml);
2535 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2536 mcast_snooping_mdb_flush(ofproto->ms);
2542 stp_wait(struct ofproto_dpif *ofproto)
2545 poll_timer_wait(1000);
2549 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2550 * caller is responsible for assigning RSTP port numbers and ensuring
2551 * there are no duplicates. */
2553 set_rstp_port(struct ofport *ofport_,
2554 const struct ofproto_port_rstp_settings *s)
2556 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2557 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2558 struct rstp_port *rp = ofport->rstp_port;
2560 if (!s || !s->enable) {
2562 rstp_port_unref(rp);
2563 ofport->rstp_port = NULL;
2564 update_rstp_port_state(ofport);
2569 /* Check if need to add a new port. */
2571 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2574 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
2575 s->admin_edge_port, s->auto_edge,
2576 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2578 update_rstp_port_state(ofport);
2579 /* Synchronize operational status. */
2580 rstp_port_set_mac_operational(rp, ofport->may_enable);
2584 get_rstp_port_status(struct ofport *ofport_,
2585 struct ofproto_port_rstp_status *s)
2587 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2588 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2589 struct rstp_port *rp = ofport->rstp_port;
2591 if (!ofproto->rstp || !rp) {
2597 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2598 &s->designated_bridge_id, &s->designated_port_id,
2599 &s->designated_path_cost, &s->tx_count,
2600 &s->rx_count, &s->error_count, &s->uptime);
2605 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2608 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2609 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2611 if (ofport->n_qdscp != n_qdscp
2612 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2613 n_qdscp * sizeof *qdscp))) {
2614 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2615 free(ofport->qdscp);
2616 ofport->qdscp = n_qdscp
2617 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2619 ofport->n_qdscp = n_qdscp;
2627 /* Expires all MAC learning entries associated with 'bundle' and forces its
2628 * ofproto to revalidate every flow.
2630 * Normally MAC learning entries are removed only from the ofproto associated
2631 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2632 * are removed from every ofproto. When patch ports and SLB bonds are in use
2633 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2634 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2635 * with the host from which it migrated. */
2637 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2639 struct ofproto_dpif *ofproto = bundle->ofproto;
2640 struct mac_learning *ml = ofproto->ml;
2641 struct mac_entry *mac, *next_mac;
2643 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2644 ovs_rwlock_wrlock(&ml->rwlock);
2645 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2646 if (mac_entry_get_port(ml, mac) == bundle) {
2648 struct ofproto_dpif *o;
2650 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2652 struct mac_entry *e;
2654 ovs_rwlock_wrlock(&o->ml->rwlock);
2655 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2657 mac_learning_expire(o->ml, e);
2659 ovs_rwlock_unlock(&o->ml->rwlock);
2664 mac_learning_expire(ml, mac);
2667 ovs_rwlock_unlock(&ml->rwlock);
2671 bundle_move(struct ofbundle *old, struct ofbundle *new)
2673 struct ofproto_dpif *ofproto = old->ofproto;
2674 struct mac_learning *ml = ofproto->ml;
2675 struct mac_entry *mac, *next_mac;
2677 ovs_assert(new->ofproto == old->ofproto);
2679 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2680 ovs_rwlock_wrlock(&ml->rwlock);
2681 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2682 if (mac_entry_get_port(ml, mac) == old) {
2683 mac_entry_set_port(ml, mac, new);
2686 ovs_rwlock_unlock(&ml->rwlock);
2689 static struct ofbundle *
2690 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2692 struct ofbundle *bundle;
2694 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2695 &ofproto->bundles) {
2696 if (bundle->aux == aux) {
2704 bundle_update(struct ofbundle *bundle)
2706 struct ofport_dpif *port;
2708 bundle->floodable = true;
2709 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2710 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2712 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2713 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2714 bundle->floodable = false;
2721 bundle_del_port(struct ofport_dpif *port)
2723 struct ofbundle *bundle = port->bundle;
2725 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2727 list_remove(&port->bundle_node);
2728 port->bundle = NULL;
2731 lacp_slave_unregister(bundle->lacp, port);
2734 bond_slave_unregister(bundle->bond, port);
2737 bundle_update(bundle);
2741 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2742 struct lacp_slave_settings *lacp)
2744 struct ofport_dpif *port;
2746 port = get_ofp_port(bundle->ofproto, ofp_port);
2751 if (port->bundle != bundle) {
2752 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2754 bundle_remove(&port->up);
2757 port->bundle = bundle;
2758 list_push_back(&bundle->ports, &port->bundle_node);
2759 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2761 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2762 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2763 bundle->floodable = false;
2767 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2768 lacp_slave_register(bundle->lacp, port, lacp);
2775 bundle_destroy(struct ofbundle *bundle)
2777 struct ofproto_dpif *ofproto;
2778 struct ofport_dpif *port, *next_port;
2784 ofproto = bundle->ofproto;
2785 mbridge_unregister_bundle(ofproto->mbridge, bundle);
2788 xlate_bundle_remove(bundle);
2791 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2792 bundle_del_port(port);
2795 bundle_flush_macs(bundle, true);
2796 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2798 free(bundle->trunks);
2799 lacp_unref(bundle->lacp);
2800 bond_unref(bundle->bond);
2805 bundle_set(struct ofproto *ofproto_, void *aux,
2806 const struct ofproto_bundle_settings *s)
2808 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2809 bool need_flush = false;
2810 struct ofport_dpif *port;
2811 struct ofbundle *bundle;
2812 unsigned long *trunks;
2818 bundle_destroy(bundle_lookup(ofproto, aux));
2822 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2823 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2825 bundle = bundle_lookup(ofproto, aux);
2827 bundle = xmalloc(sizeof *bundle);
2829 bundle->ofproto = ofproto;
2830 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2831 hash_pointer(aux, 0));
2833 bundle->name = NULL;
2835 list_init(&bundle->ports);
2836 bundle->vlan_mode = PORT_VLAN_TRUNK;
2838 bundle->trunks = NULL;
2839 bundle->use_priority_tags = s->use_priority_tags;
2840 bundle->lacp = NULL;
2841 bundle->bond = NULL;
2843 bundle->floodable = true;
2844 mbridge_register_bundle(ofproto->mbridge, bundle);
2847 if (!bundle->name || strcmp(s->name, bundle->name)) {
2849 bundle->name = xstrdup(s->name);
2854 ofproto->lacp_enabled = true;
2855 if (!bundle->lacp) {
2856 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2857 bundle->lacp = lacp_create();
2859 lacp_configure(bundle->lacp, s->lacp);
2861 lacp_unref(bundle->lacp);
2862 bundle->lacp = NULL;
2865 /* Update set of ports. */
2867 for (i = 0; i < s->n_slaves; i++) {
2868 if (!bundle_add_port(bundle, s->slaves[i],
2869 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2873 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2874 struct ofport_dpif *next_port;
2876 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2877 for (i = 0; i < s->n_slaves; i++) {
2878 if (s->slaves[i] == port->up.ofp_port) {
2883 bundle_del_port(port);
2887 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2889 if (list_is_empty(&bundle->ports)) {
2890 bundle_destroy(bundle);
2894 /* Set VLAN tagging mode */
2895 if (s->vlan_mode != bundle->vlan_mode
2896 || s->use_priority_tags != bundle->use_priority_tags) {
2897 bundle->vlan_mode = s->vlan_mode;
2898 bundle->use_priority_tags = s->use_priority_tags;
2903 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2904 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2906 if (vlan != bundle->vlan) {
2907 bundle->vlan = vlan;
2911 /* Get trunked VLANs. */
2912 switch (s->vlan_mode) {
2913 case PORT_VLAN_ACCESS:
2917 case PORT_VLAN_TRUNK:
2918 trunks = CONST_CAST(unsigned long *, s->trunks);
2921 case PORT_VLAN_NATIVE_UNTAGGED:
2922 case PORT_VLAN_NATIVE_TAGGED:
2923 if (vlan != 0 && (!s->trunks
2924 || !bitmap_is_set(s->trunks, vlan)
2925 || bitmap_is_set(s->trunks, 0))) {
2926 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2928 trunks = bitmap_clone(s->trunks, 4096);
2930 trunks = bitmap_allocate1(4096);
2932 bitmap_set1(trunks, vlan);
2933 bitmap_set0(trunks, 0);
2935 trunks = CONST_CAST(unsigned long *, s->trunks);
2942 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2943 free(bundle->trunks);
2944 if (trunks == s->trunks) {
2945 bundle->trunks = vlan_bitmap_clone(trunks);
2947 bundle->trunks = trunks;
2952 if (trunks != s->trunks) {
2957 if (!list_is_short(&bundle->ports)) {
2958 bundle->ofproto->has_bonded_bundles = true;
2960 if (bond_reconfigure(bundle->bond, s->bond)) {
2961 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2964 bundle->bond = bond_create(s->bond, ofproto);
2965 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2968 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2969 bond_slave_register(bundle->bond, port,
2970 port->up.ofp_port, port->up.netdev);
2973 bond_unref(bundle->bond);
2974 bundle->bond = NULL;
2977 /* If we changed something that would affect MAC learning, un-learn
2978 * everything on this port and force flow revalidation. */
2980 bundle_flush_macs(bundle, false);
2987 bundle_remove(struct ofport *port_)
2989 struct ofport_dpif *port = ofport_dpif_cast(port_);
2990 struct ofbundle *bundle = port->bundle;
2993 bundle_del_port(port);
2994 if (list_is_empty(&bundle->ports)) {
2995 bundle_destroy(bundle);
2996 } else if (list_is_short(&bundle->ports)) {
2997 bond_unref(bundle->bond);
2998 bundle->bond = NULL;
3004 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
3006 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
3007 struct ofport_dpif *port = port_;
3008 uint8_t ea[ETH_ADDR_LEN];
3011 error = netdev_get_etheraddr(port->up.netdev, ea);
3013 struct dp_packet packet;
3016 dp_packet_init(&packet, 0);
3017 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
3019 memcpy(packet_pdu, pdu, pdu_size);
3021 ofproto_dpif_send_packet(port, &packet);
3022 dp_packet_uninit(&packet);
3024 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
3025 "%s (%s)", port->bundle->name,
3026 netdev_get_name(port->up.netdev), ovs_strerror(error));
3031 bundle_send_learning_packets(struct ofbundle *bundle)
3033 struct ofproto_dpif *ofproto = bundle->ofproto;
3034 struct dp_packet *learning_packet;
3035 int error, n_packets, n_errors;
3036 struct mac_entry *e;
3037 struct ovs_list packets;
3039 list_init(&packets);
3040 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3041 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3042 if (mac_entry_get_port(ofproto->ml, e) != bundle) {
3045 learning_packet = bond_compose_learning_packet(bundle->bond,
3048 /* Temporarily use 'frame' as a private pointer (see below). */
3049 ovs_assert(learning_packet->frame == dp_packet_data(learning_packet));
3050 learning_packet->frame = port_void;
3051 list_push_back(&packets, &learning_packet->list_node);
3054 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3056 error = n_packets = n_errors = 0;
3057 LIST_FOR_EACH (learning_packet, list_node, &packets) {
3059 void *port_void = learning_packet->frame;
3061 /* Restore 'frame'. */
3062 learning_packet->frame = dp_packet_data(learning_packet);
3063 ret = ofproto_dpif_send_packet(port_void, learning_packet);
3070 dp_packet_list_delete(&packets);
3073 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3074 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3075 "packets, last error was: %s",
3076 bundle->name, n_errors, n_packets, ovs_strerror(error));
3078 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3079 bundle->name, n_packets);
3084 bundle_run(struct ofbundle *bundle)
3087 lacp_run(bundle->lacp, send_pdu_cb);
3090 struct ofport_dpif *port;
3092 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3093 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
3096 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3097 bundle->ofproto->backer->need_revalidate = REV_BOND;
3100 if (bond_should_send_learning_packets(bundle->bond)) {
3101 bundle_send_learning_packets(bundle);
3107 bundle_wait(struct ofbundle *bundle)
3110 lacp_wait(bundle->lacp);
3113 bond_wait(bundle->bond);
3120 mirror_set__(struct ofproto *ofproto_, void *aux,
3121 const struct ofproto_mirror_settings *s)
3123 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3124 struct ofbundle **srcs, **dsts;
3129 mirror_destroy(ofproto->mbridge, aux);
3133 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3134 dsts = xmalloc(s->n_dsts * sizeof *dsts);
3136 for (i = 0; i < s->n_srcs; i++) {
3137 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
3140 for (i = 0; i < s->n_dsts; i++) {
3141 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
3144 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3145 s->n_dsts, s->src_vlans,
3146 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
3153 mirror_get_stats__(struct ofproto *ofproto, void *aux,
3154 uint64_t *packets, uint64_t *bytes)
3156 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3161 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3163 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3164 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3165 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
3166 mac_learning_flush(ofproto->ml);
3168 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3173 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
3175 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3176 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3177 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
3181 forward_bpdu_changed(struct ofproto *ofproto_)
3183 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3184 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3188 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3191 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3192 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3193 mac_learning_set_idle_time(ofproto->ml, idle_time);
3194 mac_learning_set_max_entries(ofproto->ml, max_entries);
3195 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3198 /* Configures multicast snooping on 'ofport' using the settings
3199 * defined in 's'. */
3201 set_mcast_snooping(struct ofproto *ofproto_,
3202 const struct ofproto_mcast_snooping_settings *s)
3204 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3206 /* Only revalidate flows if the configuration changed. */
3207 if (!s != !ofproto->ms) {
3208 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3213 ofproto->ms = mcast_snooping_create();
3216 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3217 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3218 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3219 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3220 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3222 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3224 mcast_snooping_unref(ofproto->ms);
3231 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3233 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3234 const struct ofproto_mcast_snooping_port_settings *s)
3236 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3237 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3239 if (ofproto->ms && s) {
3240 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3241 mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3242 mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3244 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3252 static struct ofport_dpif *
3253 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
3255 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3256 return ofport ? ofport_dpif_cast(ofport) : NULL;
3260 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3261 struct ofproto_port *ofproto_port,
3262 struct dpif_port *dpif_port)
3264 ofproto_port->name = dpif_port->name;
3265 ofproto_port->type = dpif_port->type;
3266 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3270 ofport_update_peer(struct ofport_dpif *ofport)
3272 const struct ofproto_dpif *ofproto;
3273 struct dpif_backer *backer;
3276 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3280 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
3281 backer->need_revalidate = REV_RECONFIGURE;
3284 ofport->peer->peer = NULL;
3285 ofport->peer = NULL;
3288 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3293 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3294 struct ofport *peer_ofport;
3295 struct ofport_dpif *peer;
3298 if (ofproto->backer != backer) {
3302 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3307 peer = ofport_dpif_cast(peer_ofport);
3308 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3309 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3311 ofport->peer = peer;
3312 ofport->peer->peer = ofport;
3322 port_run(struct ofport_dpif *ofport)
3324 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3325 bool carrier_changed = carrier_seq != ofport->carrier_seq;
3326 bool enable = netdev_get_carrier(ofport->up.netdev);
3327 bool cfm_enable = false;
3328 bool bfd_enable = false;
3330 ofport->carrier_seq = carrier_seq;
3333 int cfm_opup = cfm_get_opup(ofport->cfm);
3335 cfm_enable = !cfm_get_fault(ofport->cfm);
3337 if (cfm_opup >= 0) {
3338 cfm_enable = cfm_enable && cfm_opup;
3343 bfd_enable = bfd_forwarding(ofport->bfd);
3346 if (ofport->bfd || ofport->cfm) {
3347 enable = enable && (cfm_enable || bfd_enable);
3350 if (ofport->bundle) {
3351 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3352 if (carrier_changed) {
3353 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3357 if (ofport->may_enable != enable) {
3358 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3360 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3362 if (ofport->rstp_port) {
3363 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3367 ofport->may_enable = enable;
3371 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3372 struct ofproto_port *ofproto_port)
3374 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3375 struct dpif_port dpif_port;
3378 if (sset_contains(&ofproto->ghost_ports, devname)) {
3379 const char *type = netdev_get_type_from_name(devname);
3381 /* We may be called before ofproto->up.port_by_name is populated with
3382 * the appropriate ofport. For this reason, we must get the name and
3383 * type from the netdev layer directly. */
3385 const struct ofport *ofport;
3387 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3388 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3389 ofproto_port->name = xstrdup(devname);
3390 ofproto_port->type = xstrdup(type);
3396 if (!sset_contains(&ofproto->ports, devname)) {
3399 error = dpif_port_query_by_name(ofproto->backer->dpif,
3400 devname, &dpif_port);
3402 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3408 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3410 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3411 const char *devname = netdev_get_name(netdev);
3412 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3413 const char *dp_port_name;
3415 if (netdev_vport_is_patch(netdev)) {
3416 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3420 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3421 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3422 odp_port_t port_no = ODPP_NONE;
3425 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3429 if (netdev_get_tunnel_config(netdev)) {
3430 simap_put(&ofproto->backer->tnl_backers,
3431 dp_port_name, odp_to_u32(port_no));
3435 if (netdev_get_tunnel_config(netdev)) {
3436 sset_add(&ofproto->ghost_ports, devname);
3438 sset_add(&ofproto->ports, devname);
3444 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3446 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3447 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
3454 sset_find_and_delete(&ofproto->ghost_ports,
3455 netdev_get_name(ofport->up.netdev));
3456 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3457 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3458 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3460 /* The caller is going to close ofport->up.netdev. If this is a
3461 * bonded port, then the bond is using that netdev, so remove it
3462 * from the bond. The client will need to reconfigure everything
3463 * after deleting ports, so then the slave will get re-added. */
3464 bundle_remove(&ofport->up);
3471 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3473 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3476 error = netdev_get_stats(ofport->up.netdev, stats);
3478 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3479 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3481 ovs_mutex_lock(&ofproto->stats_mutex);
3482 /* ofproto->stats.tx_packets represents packets that we created
3483 * internally and sent to some port (e.g. packets sent with
3484 * ofproto_dpif_send_packet()). Account for them as if they had
3485 * come from OFPP_LOCAL and got forwarded. */
3487 if (stats->rx_packets != UINT64_MAX) {
3488 stats->rx_packets += ofproto->stats.tx_packets;
3491 if (stats->rx_bytes != UINT64_MAX) {
3492 stats->rx_bytes += ofproto->stats.tx_bytes;
3495 /* ofproto->stats.rx_packets represents packets that were received on
3496 * some port and we processed internally and dropped (e.g. STP).
3497 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3499 if (stats->tx_packets != UINT64_MAX) {
3500 stats->tx_packets += ofproto->stats.rx_packets;
3503 if (stats->tx_bytes != UINT64_MAX) {
3504 stats->tx_bytes += ofproto->stats.rx_bytes;
3506 ovs_mutex_unlock(&ofproto->stats_mutex);
3513 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3515 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3516 if (ofport->bundle && ofport->bundle->lacp) {
3517 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3524 struct port_dump_state {
3529 struct ofproto_port port;
3534 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3536 *statep = xzalloc(sizeof(struct port_dump_state));
3541 port_dump_next(const struct ofproto *ofproto_, void *state_,
3542 struct ofproto_port *port)
3544 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3545 struct port_dump_state *state = state_;
3546 const struct sset *sset;
3547 struct sset_node *node;
3549 if (state->has_port) {
3550 ofproto_port_destroy(&state->port);
3551 state->has_port = false;
3553 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3554 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3557 error = port_query_by_name(ofproto_, node->name, &state->port);
3559 *port = state->port;
3560 state->has_port = true;
3562 } else if (error != ENODEV) {
3567 if (!state->ghost) {
3568 state->ghost = true;
3571 return port_dump_next(ofproto_, state_, port);
3578 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3580 struct port_dump_state *state = state_;
3582 if (state->has_port) {
3583 ofproto_port_destroy(&state->port);
3590 port_poll(const struct ofproto *ofproto_, char **devnamep)
3592 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3594 if (ofproto->port_poll_errno) {
3595 int error = ofproto->port_poll_errno;
3596 ofproto->port_poll_errno = 0;
3600 if (sset_is_empty(&ofproto->port_poll_set)) {
3604 *devnamep = sset_pop(&ofproto->port_poll_set);
3609 port_poll_wait(const struct ofproto *ofproto_)
3611 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3612 dpif_port_poll_wait(ofproto->backer->dpif);
3616 port_is_lacp_current(const struct ofport *ofport_)
3618 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3619 return (ofport->bundle && ofport->bundle->lacp
3620 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3624 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3625 * then delete it entirely. */
3627 rule_expire(struct rule_dpif *rule)
3628 OVS_REQUIRES(ofproto_mutex)
3630 uint16_t hard_timeout, idle_timeout;
3631 long long int now = time_msec();
3634 hard_timeout = rule->up.hard_timeout;
3635 idle_timeout = rule->up.idle_timeout;
3637 /* Has 'rule' expired? */
3639 long long int modified;
3641 ovs_mutex_lock(&rule->up.mutex);
3642 modified = rule->up.modified;
3643 ovs_mutex_unlock(&rule->up.mutex);
3645 if (now > modified + hard_timeout * 1000) {
3646 reason = OFPRR_HARD_TIMEOUT;
3650 if (reason < 0 && idle_timeout) {
3653 ovs_mutex_lock(&rule->stats_mutex);
3654 used = rule->stats.used;
3655 ovs_mutex_unlock(&rule->stats_mutex);
3657 if (now > used + idle_timeout * 1000) {
3658 reason = OFPRR_IDLE_TIMEOUT;
3663 COVERAGE_INC(ofproto_dpif_expired);
3664 ofproto_rule_expire(&rule->up, reason);
3668 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3669 * 'flow' must reflect the data in 'packet'. */
3671 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3672 const struct flow *flow,
3673 struct rule_dpif *rule,
3674 const struct ofpact *ofpacts, size_t ofpacts_len,
3675 struct dp_packet *packet)
3677 struct dpif_flow_stats stats;
3678 struct xlate_out xout;
3679 struct xlate_in xin;
3681 struct dpif_execute execute;
3684 ovs_assert((rule != NULL) != (ofpacts != NULL));
3686 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3689 rule_dpif_credit_stats(rule, &stats);
3692 xlate_in_init(&xin, ofproto, flow, flow->in_port.ofp_port, rule,
3693 stats.tcp_flags, packet);
3694 xin.ofpacts = ofpacts;
3695 xin.ofpacts_len = ofpacts_len;
3696 xin.resubmit_stats = &stats;
3697 xlate_actions(&xin, &xout);
3699 execute.actions = xout.odp_actions->data;
3700 execute.actions_len = xout.odp_actions->size;
3702 pkt_metadata_from_flow(&packet->md, flow);
3703 execute.packet = packet;
3704 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3705 execute.probe = false;
3707 /* Fix up in_port. */
3708 in_port = flow->in_port.ofp_port;
3709 if (in_port == OFPP_NONE) {
3710 in_port = OFPP_LOCAL;
3712 execute.packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3714 error = dpif_execute(ofproto->backer->dpif, &execute);
3716 xlate_out_uninit(&xout);
3722 rule_dpif_credit_stats(struct rule_dpif *rule,
3723 const struct dpif_flow_stats *stats)
3725 ovs_mutex_lock(&rule->stats_mutex);
3726 rule->stats.n_packets += stats->n_packets;
3727 rule->stats.n_bytes += stats->n_bytes;
3728 rule->stats.used = MAX(rule->stats.used, stats->used);
3729 ovs_mutex_unlock(&rule->stats_mutex);
3733 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3734 OVS_REQUIRES(rule->up.mutex)
3736 return rule->up.flow_cookie;
3740 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3741 uint16_t hard_timeout)
3743 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3746 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3747 * be read until the calling thread quiesces. */
3748 const struct rule_actions *
3749 rule_dpif_get_actions(const struct rule_dpif *rule)
3751 return rule_get_actions(&rule->up);
3754 /* Sets 'rule''s recirculation id. */
3756 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3757 OVS_REQUIRES(rule->up.mutex)
3759 ovs_assert(!rule->recirc_id);
3760 rule->recirc_id = id;
3763 /* Returns 'rule''s recirculation id. */
3765 rule_dpif_get_recirc_id(struct rule_dpif *rule)
3766 OVS_REQUIRES(rule->up.mutex)
3768 if (!rule->recirc_id) {
3769 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3771 rule_dpif_set_recirc_id(rule, ofproto_dpif_alloc_recirc_id(ofproto));
3773 return rule->recirc_id;
3776 /* Sets 'rule''s recirculation id. */
3778 rule_set_recirc_id(struct rule *rule_, uint32_t id)
3780 struct rule_dpif *rule = rule_dpif_cast(rule_);
3782 ovs_mutex_lock(&rule->up.mutex);
3783 rule_dpif_set_recirc_id(rule, id);
3784 ovs_mutex_unlock(&rule->up.mutex);
3787 /* Lookup 'flow' in table 0 of 'ofproto''s classifier.
3788 * If 'wc' is non-null, sets the fields that were relevant as part of
3789 * the lookup. Returns the table id where a match or miss occurred via
3790 * 'table_id'. This will be zero unless there was a miss and
3791 * OFPTC11_TABLE_MISS_CONTINUE is in effect for the sequence of tables
3792 * where misses occur, or TBL_INTERNAL if the rule has a non-zero
3793 * recirculation ID, and a match was found in the internal table, or if
3794 * there was no match and one of the special rules (drop_frags_rule,
3795 * miss_rule, or no_packet_in_rule) was returned.
3797 * The return value is the found rule, which is valid at least until the next
3798 * RCU quiescent period. If the rule needs to stay around longer,
3799 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3800 * on it before this returns. */
3802 rule_dpif_lookup(struct ofproto_dpif *ofproto, struct flow *flow,
3803 struct flow_wildcards *wc, bool take_ref,
3804 const struct dpif_flow_stats *stats, uint8_t *table_id)
3806 *table_id = rule_dpif_lookup_get_init_table_id(flow);
3808 return rule_dpif_lookup_from_table(ofproto, flow, wc, take_ref, stats,
3809 table_id, flow->in_port.ofp_port, true,
3813 /* The returned rule (if any) is valid at least until the next RCU quiescent
3814 * period. If the rule needs to stay around longer, a non-zero 'take_ref'
3815 * must be passed in to cause a reference to be taken on it.
3817 * 'flow' is non-const to allow for temporary modifications during the lookup.
3818 * Any changes are restored before returning. */
3819 static struct rule_dpif *
3820 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3821 struct flow *flow, struct flow_wildcards *wc,
3824 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3825 const struct cls_rule *cls_rule;
3826 struct rule_dpif *rule;
3829 cls_rule = classifier_lookup(cls, flow, wc);
3831 rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3833 /* Try again if the rule was released before we get the reference. */
3834 } while (rule && take_ref && !rule_dpif_try_ref(rule));
3839 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3840 * Returns the rule that was found, which may be one of the special rules
3841 * according to packet miss hadling. If 'may_packet_in' is false, returning of
3842 * the miss_rule (which issues packet ins for the controller) is avoided.
3843 * Updates 'wc', if nonnull, to reflect the fields that were used during the
3846 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3847 * if none is found then the table miss configuration for that table is
3848 * honored, which can result in additional lookups in other OpenFlow tables.
3849 * In this case the function updates '*table_id' to reflect the final OpenFlow
3850 * table that was searched.
3852 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3855 * The rule is returned in '*rule', which is valid at least until the next
3856 * RCU quiescent period. If the '*rule' needs to stay around longer,
3857 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3858 * on it before this returns.
3860 * 'in_port' allows the lookup to take place as if the in port had the value
3861 * 'in_port'. This is needed for resubmit action support.
3863 * 'flow' is non-const to allow for temporary modifications during the lookup.
3864 * Any changes are restored before returning. */
3866 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto, struct flow *flow,
3867 struct flow_wildcards *wc, bool take_ref,
3868 const struct dpif_flow_stats *stats,
3869 uint8_t *table_id, ofp_port_t in_port,
3870 bool may_packet_in, bool honor_table_miss)
3872 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
3873 ofp_port_t old_in_port = flow->in_port.ofp_port;
3874 enum ofputil_table_miss miss_config;
3875 struct rule_dpif *rule;
3878 /* We always unwildcard nw_frag (for IP), so they
3879 * need not be unwildcarded here. */
3880 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3881 && ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3882 if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3883 /* We must pretend that transport ports are unavailable. */
3884 flow->tp_src = htons(0);
3885 flow->tp_dst = htons(0);
3887 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
3888 * Use the drop_frags_rule (which cannot disappear). */
3889 rule = ofproto->drop_frags_rule;
3891 rule_dpif_ref(rule);
3894 struct oftable *tbl = &ofproto->up.tables[*table_id];
3897 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
3903 /* Look up a flow with 'in_port' as the input port. Then restore the
3904 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3905 * have surprising behavior). */
3906 flow->in_port.ofp_port = in_port;
3908 /* Our current implementation depends on n_tables == N_TABLES, and
3909 * TBL_INTERNAL being the last table. */
3910 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
3912 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
3914 for (next_id = *table_id;
3915 next_id < ofproto->up.n_tables;
3916 next_id++, next_id += (next_id == TBL_INTERNAL))
3918 *table_id = next_id;
3919 rule = rule_dpif_lookup_in_table(ofproto, next_id, flow, wc, take_ref);
3921 struct oftable *tbl = &ofproto->up.tables[next_id];
3924 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
3925 stats->n_packets, &orig);
3928 goto out; /* Match. */
3930 if (honor_table_miss) {
3931 miss_config = ofproto_table_get_miss_config(&ofproto->up,
3933 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
3940 rule = ofproto->no_packet_in_rule;
3941 if (may_packet_in) {
3942 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
3943 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
3944 struct ofport_dpif *port;
3946 port = get_ofp_port(ofproto, old_in_port);
3948 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3950 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
3951 rule = ofproto->miss_rule;
3953 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
3954 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3955 rule = ofproto->miss_rule;
3959 rule_dpif_ref(rule);
3962 /* Restore port numbers, as they may have been modified above. */
3963 flow->tp_src = old_tp_src;
3964 flow->tp_dst = old_tp_dst;
3965 /* Restore the old in port. */
3966 flow->in_port.ofp_port = old_in_port;
3972 complete_operation(struct rule_dpif *rule)
3973 OVS_REQUIRES(ofproto_mutex)
3975 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3977 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3980 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3982 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3985 static struct rule *
3988 struct rule_dpif *rule = xmalloc(sizeof *rule);
3993 rule_dealloc(struct rule *rule_)
3995 struct rule_dpif *rule = rule_dpif_cast(rule_);
4000 rule_construct(struct rule *rule_)
4001 OVS_NO_THREAD_SAFETY_ANALYSIS
4003 struct rule_dpif *rule = rule_dpif_cast(rule_);
4004 ovs_mutex_init_adaptive(&rule->stats_mutex);
4005 rule->stats.n_packets = 0;
4006 rule->stats.n_bytes = 0;
4007 rule->stats.used = rule->up.modified;
4008 rule->recirc_id = 0;
4014 rule_insert(struct rule *rule_)
4015 OVS_REQUIRES(ofproto_mutex)
4017 struct rule_dpif *rule = rule_dpif_cast(rule_);
4018 complete_operation(rule);
4023 rule_delete(struct rule *rule_)
4024 OVS_REQUIRES(ofproto_mutex)
4026 struct rule_dpif *rule = rule_dpif_cast(rule_);
4027 complete_operation(rule);
4031 rule_destruct(struct rule *rule_)
4033 struct rule_dpif *rule = rule_dpif_cast(rule_);
4035 ovs_mutex_destroy(&rule->stats_mutex);
4036 if (rule->recirc_id) {
4037 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4039 ofproto_dpif_free_recirc_id(ofproto, rule->recirc_id);
4044 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
4045 long long int *used)
4047 struct rule_dpif *rule = rule_dpif_cast(rule_);
4049 ovs_mutex_lock(&rule->stats_mutex);
4050 *packets = rule->stats.n_packets;
4051 *bytes = rule->stats.n_bytes;
4052 *used = rule->stats.used;
4053 ovs_mutex_unlock(&rule->stats_mutex);
4057 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
4058 struct dp_packet *packet)
4060 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4062 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
4066 rule_execute(struct rule *rule, const struct flow *flow,
4067 struct dp_packet *packet)
4069 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
4070 dp_packet_delete(packet);
4075 rule_modify_actions(struct rule *rule_, bool reset_counters)
4076 OVS_REQUIRES(ofproto_mutex)
4078 struct rule_dpif *rule = rule_dpif_cast(rule_);
4080 if (reset_counters) {
4081 ovs_mutex_lock(&rule->stats_mutex);
4082 rule->stats.n_packets = 0;
4083 rule->stats.n_bytes = 0;
4084 ovs_mutex_unlock(&rule->stats_mutex);
4087 complete_operation(rule);
4090 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
4092 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
4095 static struct ofgroup *
4098 struct group_dpif *group = xzalloc(sizeof *group);
4103 group_dealloc(struct ofgroup *group_)
4105 struct group_dpif *group = group_dpif_cast(group_);
4110 group_construct_stats(struct group_dpif *group)
4111 OVS_REQUIRES(group->stats_mutex)
4113 struct ofputil_bucket *bucket;
4114 const struct ovs_list *buckets;
4116 group->packet_count = 0;
4117 group->byte_count = 0;
4119 group_dpif_get_buckets(group, &buckets);
4120 LIST_FOR_EACH (bucket, list_node, buckets) {
4121 bucket->stats.packet_count = 0;
4122 bucket->stats.byte_count = 0;
4127 group_dpif_credit_stats(struct group_dpif *group,
4128 struct ofputil_bucket *bucket,
4129 const struct dpif_flow_stats *stats)
4131 ovs_mutex_lock(&group->stats_mutex);
4132 group->packet_count += stats->n_packets;
4133 group->byte_count += stats->n_bytes;
4135 bucket->stats.packet_count += stats->n_packets;
4136 bucket->stats.byte_count += stats->n_bytes;
4137 } else { /* Credit to all buckets */
4138 const struct ovs_list *buckets;
4140 group_dpif_get_buckets(group, &buckets);
4141 LIST_FOR_EACH (bucket, list_node, buckets) {
4142 bucket->stats.packet_count += stats->n_packets;
4143 bucket->stats.byte_count += stats->n_bytes;
4146 ovs_mutex_unlock(&group->stats_mutex);
4150 group_construct(struct ofgroup *group_)
4152 struct group_dpif *group = group_dpif_cast(group_);
4153 const struct ofputil_bucket *bucket;
4155 /* Prevent group chaining because our locking structure makes it hard to
4156 * implement deadlock-free. (See xlate_group_resource_check().) */
4157 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4158 const struct ofpact *a;
4160 OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
4161 if (a->type == OFPACT_GROUP) {
4162 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
4167 ovs_mutex_init_adaptive(&group->stats_mutex);
4168 ovs_mutex_lock(&group->stats_mutex);
4169 group_construct_stats(group);
4170 ovs_mutex_unlock(&group->stats_mutex);
4175 group_destruct(struct ofgroup *group_)
4177 struct group_dpif *group = group_dpif_cast(group_);
4178 ovs_mutex_destroy(&group->stats_mutex);
4182 group_modify(struct ofgroup *group_)
4184 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
4186 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4192 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4194 struct group_dpif *group = group_dpif_cast(group_);
4195 struct ofputil_bucket *bucket;
4196 const struct ovs_list *buckets;
4197 struct bucket_counter *bucket_stats;
4199 ovs_mutex_lock(&group->stats_mutex);
4200 ogs->packet_count = group->packet_count;
4201 ogs->byte_count = group->byte_count;
4203 group_dpif_get_buckets(group, &buckets);
4204 bucket_stats = ogs->bucket_stats;
4205 LIST_FOR_EACH (bucket, list_node, buckets) {
4206 bucket_stats->packet_count = bucket->stats.packet_count;
4207 bucket_stats->byte_count = bucket->stats.byte_count;
4210 ovs_mutex_unlock(&group->stats_mutex);
4215 /* If the group exists, this function increments the groups's reference count.
4217 * Make sure to call group_dpif_unref() after no longer needing to maintain
4218 * a reference to the group. */
4220 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4221 struct group_dpif **group)
4223 struct ofgroup *ofgroup;
4226 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4227 *group = found ? group_dpif_cast(ofgroup) : NULL;
4233 group_dpif_get_buckets(const struct group_dpif *group,
4234 const struct ovs_list **buckets)
4236 *buckets = &group->up.buckets;
4239 enum ofp11_group_type
4240 group_dpif_get_type(const struct group_dpif *group)
4242 return group->up.type;
4246 group_dpif_get_selection_method(const struct group_dpif *group)
4248 return group->up.props.selection_method;
4251 /* Sends 'packet' out 'ofport'.
4252 * May modify 'packet'.
4253 * Returns 0 if successful, otherwise a positive errno value. */
4255 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4257 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4260 error = xlate_send_packet(ofport, packet);
4262 ovs_mutex_lock(&ofproto->stats_mutex);
4263 ofproto->stats.tx_packets++;
4264 ofproto->stats.tx_bytes += dp_packet_size(packet);
4265 ovs_mutex_unlock(&ofproto->stats_mutex);
4269 /* Return the version string of the datapath that backs up
4273 get_datapath_version(const struct ofproto *ofproto_)
4275 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4277 return ofproto->backer->dp_version_string;
4281 set_frag_handling(struct ofproto *ofproto_,
4282 enum ofp_config_flags frag_handling)
4284 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4285 if (frag_handling != OFPC_FRAG_REASM) {
4286 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4294 packet_out(struct ofproto *ofproto_, struct dp_packet *packet,
4295 const struct flow *flow,
4296 const struct ofpact *ofpacts, size_t ofpacts_len)
4298 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4300 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4301 ofpacts_len, packet);
4308 set_netflow(struct ofproto *ofproto_,
4309 const struct netflow_options *netflow_options)
4311 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4313 if (netflow_options) {
4314 if (!ofproto->netflow) {
4315 ofproto->netflow = netflow_create();
4316 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4318 return netflow_set_options(ofproto->netflow, netflow_options);
4319 } else if (ofproto->netflow) {
4320 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4321 netflow_unref(ofproto->netflow);
4322 ofproto->netflow = NULL;
4329 get_netflow_ids(const struct ofproto *ofproto_,
4330 uint8_t *engine_type, uint8_t *engine_id)
4332 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4334 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4337 static struct ofproto_dpif *
4338 ofproto_dpif_lookup(const char *name)
4340 struct ofproto_dpif *ofproto;
4342 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4343 hash_string(name, 0), &all_ofproto_dpifs) {
4344 if (!strcmp(ofproto->up.name, name)) {
4352 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4353 const char *argv[], void *aux OVS_UNUSED)
4355 struct ofproto_dpif *ofproto;
4358 ofproto = ofproto_dpif_lookup(argv[1]);
4360 unixctl_command_reply_error(conn, "no such bridge");
4363 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4364 mac_learning_flush(ofproto->ml);
4365 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4367 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4368 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4369 mac_learning_flush(ofproto->ml);
4370 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4374 unixctl_command_reply(conn, "table successfully flushed");
4378 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4379 const char *argv[], void *aux OVS_UNUSED)
4381 struct ofproto_dpif *ofproto;
4384 ofproto = ofproto_dpif_lookup(argv[1]);
4386 unixctl_command_reply_error(conn, "no such bridge");
4390 if (!mcast_snooping_enabled(ofproto->ms)) {
4391 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4394 mcast_snooping_mdb_flush(ofproto->ms);
4396 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4397 if (!mcast_snooping_enabled(ofproto->ms)) {
4400 mcast_snooping_mdb_flush(ofproto->ms);
4404 unixctl_command_reply(conn, "table successfully flushed");
4407 static struct ofport_dpif *
4408 ofbundle_get_a_port(const struct ofbundle *bundle)
4410 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
4415 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4416 const char *argv[], void *aux OVS_UNUSED)
4418 struct ds ds = DS_EMPTY_INITIALIZER;
4419 const struct ofproto_dpif *ofproto;
4420 const struct mac_entry *e;
4422 ofproto = ofproto_dpif_lookup(argv[1]);
4424 unixctl_command_reply_error(conn, "no such bridge");
4428 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4429 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4430 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4431 struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
4432 char name[OFP_MAX_PORT_NAME_LEN];
4434 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4436 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
4437 name, e->vlan, ETH_ADDR_ARGS(e->mac),
4438 mac_entry_age(ofproto->ml, e));
4440 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4441 unixctl_command_reply(conn, ds_cstr(&ds));
4446 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4447 int argc OVS_UNUSED,
4449 void *aux OVS_UNUSED)
4451 struct ds ds = DS_EMPTY_INITIALIZER;
4452 const struct ofproto_dpif *ofproto;
4453 const struct ofbundle *bundle;
4454 const struct mcast_group *grp;
4455 struct mcast_group_bundle *b;
4456 struct mcast_mrouter_bundle *mrouter;
4458 ofproto = ofproto_dpif_lookup(argv[1]);
4460 unixctl_command_reply_error(conn, "no such bridge");
4464 if (!mcast_snooping_enabled(ofproto->ms)) {
4465 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4469 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
4470 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4471 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4472 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
4473 char name[OFP_MAX_PORT_NAME_LEN];
4476 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4478 ds_put_format(&ds, "%5s %4d "IP_FMT" %3d\n",
4479 name, grp->vlan, IP_ARGS(grp->ip4),
4480 mcast_bundle_age(ofproto->ms, b));
4484 /* ports connected to multicast routers */
4485 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
4486 char name[OFP_MAX_PORT_NAME_LEN];
4488 bundle = mrouter->port;
4489 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4491 ds_put_format(&ds, "%5s %4d querier %3d\n",
4492 name, mrouter->vlan,
4493 mcast_mrouter_age(ofproto->ms, mrouter));
4495 ovs_rwlock_unlock(&ofproto->ms->rwlock);
4496 unixctl_command_reply(conn, ds_cstr(&ds));
4501 struct xlate_out xout;
4502 struct xlate_in xin;
4503 const struct flow *key;
4505 struct flow_wildcards wc;
4510 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4512 const struct rule_actions *actions;
4515 ds_put_char_multiple(result, '\t', level);
4517 ds_put_cstr(result, "No match\n");
4521 ovs_mutex_lock(&rule->up.mutex);
4522 cookie = rule->up.flow_cookie;
4523 ovs_mutex_unlock(&rule->up.mutex);
4525 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4526 rule ? rule->up.table_id : 0, ntohll(cookie));
4527 cls_rule_format(&rule->up.cr, result);
4528 ds_put_char(result, '\n');
4530 actions = rule_dpif_get_actions(rule);
4532 ds_put_char_multiple(result, '\t', level);
4533 ds_put_cstr(result, "OpenFlow actions=");
4534 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4535 ds_put_char(result, '\n');
4539 trace_format_flow(struct ds *result, int level, const char *title,
4540 struct trace_ctx *trace)
4542 ds_put_char_multiple(result, '\t', level);
4543 ds_put_format(result, "%s: ", title);
4544 /* Do not report unchanged flows for resubmits. */
4545 if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4546 || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4547 ds_put_cstr(result, "unchanged");
4549 flow_format(result, &trace->xin.flow);
4550 trace->flow = trace->xin.flow;
4552 ds_put_char(result, '\n');
4556 trace_format_regs(struct ds *result, int level, const char *title,
4557 struct trace_ctx *trace)
4561 ds_put_char_multiple(result, '\t', level);
4562 ds_put_format(result, "%s:", title);
4563 for (i = 0; i < FLOW_N_REGS; i++) {
4564 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4566 ds_put_char(result, '\n');
4570 trace_format_odp(struct ds *result, int level, const char *title,
4571 struct trace_ctx *trace)
4573 struct ofpbuf *odp_actions = trace->xout.odp_actions;
4575 ds_put_char_multiple(result, '\t', level);
4576 ds_put_format(result, "%s: ", title);
4577 format_odp_actions(result, odp_actions->data, odp_actions->size);
4578 ds_put_char(result, '\n');
4582 trace_format_megaflow(struct ds *result, int level, const char *title,
4583 struct trace_ctx *trace)
4587 ds_put_char_multiple(result, '\t', level);
4588 ds_put_format(result, "%s: ", title);
4589 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
4590 match_init(&match, trace->key, &trace->wc);
4591 match_format(&match, result, OFP_DEFAULT_PRIORITY);
4592 ds_put_char(result, '\n');
4595 static void trace_report(struct xlate_in *xin, const char *s, int recurse);
4598 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
4600 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4601 struct ds *result = trace->result;
4604 if (rule == xin->ofproto->miss_rule) {
4605 trace_report(xin, "No match, flow generates \"packet in\"s.",
4607 } else if (rule == xin->ofproto->no_packet_in_rule) {
4608 trace_report(xin, "No match, packets dropped because "
4609 "OFPPC_NO_PACKET_IN is set on in_port.", recurse);
4610 } else if (rule == xin->ofproto->drop_frags_rule) {
4611 trace_report(xin, "Packets dropped because they are IP "
4612 "fragments and the fragment handling mode is "
4613 "\"drop\".", recurse);
4617 ds_put_char(result, '\n');
4619 trace_format_flow(result, recurse, "Resubmitted flow", trace);
4620 trace_format_regs(result, recurse, "Resubmitted regs", trace);
4621 trace_format_odp(result, recurse, "Resubmitted odp", trace);
4622 trace_format_megaflow(result, recurse, "Resubmitted megaflow", trace);
4624 trace_format_rule(result, recurse, rule);
4628 trace_report(struct xlate_in *xin, const char *s, int recurse)
4630 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4631 struct ds *result = trace->result;
4633 ds_put_char_multiple(result, '\t', recurse);
4634 ds_put_cstr(result, s);
4635 ds_put_char(result, '\n');
4638 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4639 * forms are supported:
4641 * - [dpname] odp_flow [-generate | packet]
4642 * - bridge br_flow [-generate | packet]
4644 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4645 * returns a nonnull malloced error message. */
4646 static char * OVS_WARN_UNUSED_RESULT
4647 parse_flow_and_packet(int argc, const char *argv[],
4648 struct ofproto_dpif **ofprotop, struct flow *flow,
4649 struct dp_packet **packetp)
4651 const struct dpif_backer *backer = NULL;
4652 const char *error = NULL;
4654 struct simap port_names = SIMAP_INITIALIZER(&port_names);
4655 struct dp_packet *packet;
4656 struct ofpbuf odp_key;
4657 struct ofpbuf odp_mask;
4659 ofpbuf_init(&odp_key, 0);
4660 ofpbuf_init(&odp_mask, 0);
4662 /* Handle "-generate" or a hex string as the last argument. */
4663 if (!strcmp(argv[argc - 1], "-generate")) {
4664 packet = dp_packet_new(0);
4667 error = eth_from_hex(argv[argc - 1], &packet);
4670 } else if (argc == 4) {
4671 /* The 3-argument form must end in "-generate' or a hex string. */
4677 /* odp_flow can have its in_port specified as a name instead of port no.
4678 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4679 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4680 * we need to create a simap of name to port no. */
4682 const char *dp_type;
4683 if (!strncmp(argv[1], "ovs-", 4)) {
4684 dp_type = argv[1] + 4;
4688 backer = shash_find_data(&all_dpif_backers, dp_type);
4689 } else if (argc == 2) {
4690 struct shash_node *node;
4691 if (shash_count(&all_dpif_backers) == 1) {
4692 node = shash_first(&all_dpif_backers);
4693 backer = node->data;
4696 error = "Syntax error";
4699 if (backer && backer->dpif) {
4700 struct dpif_port dpif_port;
4701 struct dpif_port_dump port_dump;
4702 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4703 simap_put(&port_names, dpif_port.name,
4704 odp_to_u32(dpif_port.port_no));
4708 /* Parse the flow and determine whether a datapath or
4709 * bridge is specified. If function odp_flow_key_from_string()
4710 * returns 0, the flow is a odp_flow. If function
4711 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4712 if (!odp_flow_from_string(argv[argc - 1], &port_names,
4713 &odp_key, &odp_mask)) {
4715 error = "Cannot find the datapath";
4719 if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
4720 error = "Failed to parse flow key";
4724 *ofprotop = xlate_lookup_ofproto(backer, flow,
4725 &flow->in_port.ofp_port);
4726 if (*ofprotop == NULL) {
4727 error = "Invalid datapath flow";
4731 vsp_adjust_flow(*ofprotop, flow, NULL);
4734 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4737 m_err = xasprintf("Bad flow syntax: %s", err);
4742 error = "Must specify bridge name";
4746 *ofprotop = ofproto_dpif_lookup(argv[1]);
4748 error = "Unknown bridge name";
4754 /* Generate a packet, if requested. */
4756 if (!dp_packet_size(packet)) {
4757 flow_compose(packet, flow);
4759 /* Use the metadata from the flow and the packet argument
4760 * to reconstruct the flow. */
4761 pkt_metadata_from_flow(&packet->md, flow);
4762 flow_extract(packet, flow);
4767 if (error && !m_err) {
4768 m_err = xstrdup(error);
4771 dp_packet_delete(packet);
4775 ofpbuf_uninit(&odp_key);
4776 ofpbuf_uninit(&odp_mask);
4777 simap_destroy(&port_names);
4782 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4783 void *aux OVS_UNUSED)
4785 struct ofproto_dpif *ofproto;
4786 struct dp_packet *packet;
4790 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4795 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4796 unixctl_command_reply(conn, ds_cstr(&result));
4797 ds_destroy(&result);
4798 dp_packet_delete(packet);
4800 unixctl_command_reply_error(conn, error);
4806 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4807 const char *argv[], void *aux OVS_UNUSED)
4809 enum ofputil_protocol usable_protocols;
4810 struct ofproto_dpif *ofproto;
4811 bool enforce_consistency;
4812 struct ofpbuf ofpacts;
4813 struct dp_packet *packet;
4818 /* Three kinds of error return values! */
4824 ofpbuf_init(&ofpacts, 0);
4826 /* Parse actions. */
4827 error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
4829 unixctl_command_reply_error(conn, error);
4834 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4835 * consistency between the flow and the actions. With -consistent, we
4836 * enforce consistency even for a flow supported in OpenFlow 1.0. */
4837 if (!strcmp(argv[1], "-consistent")) {
4838 enforce_consistency = true;
4842 enforce_consistency = false;
4845 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4847 unixctl_command_reply_error(conn, error);
4852 /* Do the same checks as handle_packet_out() in ofproto.c.
4854 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
4855 * strictly correct because these actions aren't in any table, but it's OK
4856 * because it 'table_id' is used only to check goto_table instructions, but
4857 * packet-outs take a list of actions and therefore it can't include
4860 * We skip the "meter" check here because meter is an instruction, not an
4861 * action, and thus cannot appear in ofpacts. */
4862 in_port = ofp_to_u16(flow.in_port.ofp_port);
4863 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
4864 unixctl_command_reply_error(conn, "invalid in_port");
4867 if (enforce_consistency) {
4868 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
4869 &flow, u16_to_ofp(ofproto->up.max_ports),
4870 0, 0, usable_protocols);
4872 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
4873 u16_to_ofp(ofproto->up.max_ports), 0, 0,
4879 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4880 unixctl_command_reply_error(conn, ds_cstr(&result));
4884 ofproto_trace(ofproto, &flow, packet,
4885 ofpacts.data, ofpacts.size, &result);
4886 unixctl_command_reply(conn, ds_cstr(&result));
4889 ds_destroy(&result);
4890 dp_packet_delete(packet);
4891 ofpbuf_uninit(&ofpacts);
4894 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4895 * description of the results to 'ds'.
4897 * The trace follows a packet with the specified 'flow' through the flow
4898 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4899 * side effects (if it is nonnull then its flow must be 'flow').
4901 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4902 * trace, otherwise the actions are determined by a flow table lookup. */
4904 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4905 const struct dp_packet *packet,
4906 const struct ofpact ofpacts[], size_t ofpacts_len,
4909 struct trace_ctx trace;
4911 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4912 ds_put_cstr(ds, "Flow: ");
4913 flow_format(ds, flow);
4914 ds_put_char(ds, '\n');
4916 flow_wildcards_init_catchall(&trace.wc);
4919 trace.key = flow; /* Original flow key, used for megaflow. */
4920 trace.flow = *flow; /* May be modified by actions. */
4921 xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
4922 ntohs(flow->tcp_flags), packet);
4923 trace.xin.ofpacts = ofpacts;
4924 trace.xin.ofpacts_len = ofpacts_len;
4925 trace.xin.resubmit_hook = trace_resubmit;
4926 trace.xin.report_hook = trace_report;
4928 xlate_actions(&trace.xin, &trace.xout);
4930 ds_put_char(ds, '\n');
4931 trace_format_flow(ds, 0, "Final flow", &trace);
4932 trace_format_megaflow(ds, 0, "Megaflow", &trace);
4934 ds_put_cstr(ds, "Datapath actions: ");
4935 format_odp_actions(ds, trace.xout.odp_actions->data,
4936 trace.xout.odp_actions->size);
4938 if (trace.xout.slow) {
4939 enum slow_path_reason slow;
4941 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4942 "slow path because it:");
4944 slow = trace.xout.slow;
4946 enum slow_path_reason bit = rightmost_1bit(slow);
4948 ds_put_format(ds, "\n\t- %s.",
4949 slow_path_reason_to_explanation(bit));
4955 xlate_out_uninit(&trace.xout);
4958 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4959 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4960 * to destroy 'ofproto_shash' and free the returned value. */
4961 static const struct shash_node **
4962 get_ofprotos(struct shash *ofproto_shash)
4964 const struct ofproto_dpif *ofproto;
4966 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4967 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4968 shash_add_nocopy(ofproto_shash, name, ofproto);
4971 return shash_sort(ofproto_shash);
4975 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4976 const char *argv[] OVS_UNUSED,
4977 void *aux OVS_UNUSED)
4979 struct ds ds = DS_EMPTY_INITIALIZER;
4980 struct shash ofproto_shash;
4981 const struct shash_node **sorted_ofprotos;
4984 shash_init(&ofproto_shash);
4985 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4986 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4987 const struct shash_node *node = sorted_ofprotos[i];
4988 ds_put_format(&ds, "%s\n", node->name);
4991 shash_destroy(&ofproto_shash);
4992 free(sorted_ofprotos);
4994 unixctl_command_reply(conn, ds_cstr(&ds));
4999 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5001 const struct shash_node **ofprotos;
5002 struct dpif_dp_stats dp_stats;
5003 struct shash ofproto_shash;
5006 dpif_get_dp_stats(backer->dpif, &dp_stats);
5008 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5009 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
5011 shash_init(&ofproto_shash);
5012 ofprotos = get_ofprotos(&ofproto_shash);
5013 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5014 struct ofproto_dpif *ofproto = ofprotos[i]->data;
5015 const struct shash_node **ports;
5018 if (ofproto->backer != backer) {
5022 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
5024 ports = shash_sort(&ofproto->up.port_by_name);
5025 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5026 const struct shash_node *node = ports[j];
5027 struct ofport *ofport = node->data;
5029 odp_port_t odp_port;
5031 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5034 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5035 if (odp_port != ODPP_NONE) {
5036 ds_put_format(ds, "%"PRIu32":", odp_port);
5038 ds_put_cstr(ds, "none:");
5041 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5044 if (!netdev_get_config(ofport->netdev, &config)) {
5045 const struct smap_node **nodes;
5048 nodes = smap_sort(&config);
5049 for (i = 0; i < smap_count(&config); i++) {
5050 const struct smap_node *node = nodes[i];
5051 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5052 node->key, node->value);
5056 smap_destroy(&config);
5058 ds_put_char(ds, ')');
5059 ds_put_char(ds, '\n');
5063 shash_destroy(&ofproto_shash);
5068 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5069 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5071 struct ds ds = DS_EMPTY_INITIALIZER;
5072 const struct shash_node **backers;
5075 backers = shash_sort(&all_dpif_backers);
5076 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5077 dpif_show_backer(backers[i]->data, &ds);
5081 unixctl_command_reply(conn, ds_cstr(&ds));
5086 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5087 int argc OVS_UNUSED, const char *argv[],
5088 void *aux OVS_UNUSED)
5090 const struct ofproto_dpif *ofproto;
5092 struct ds ds = DS_EMPTY_INITIALIZER;
5093 bool verbosity = false;
5095 struct dpif_port dpif_port;
5096 struct dpif_port_dump port_dump;
5097 struct hmap portno_names;
5099 struct dpif_flow_dump *flow_dump;
5100 struct dpif_flow_dump_thread *flow_dump_thread;
5104 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5106 unixctl_command_reply_error(conn, "no such bridge");
5110 if (argc > 2 && !strcmp(argv[1], "-m")) {
5114 hmap_init(&portno_names);
5115 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5116 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5120 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
5121 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5122 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5125 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5126 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5131 odp_format_ufid(&f.ufid, &ds);
5132 ds_put_cstr(&ds, " ");
5134 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5135 &portno_names, &ds, verbosity);
5136 ds_put_cstr(&ds, ", ");
5137 dpif_flow_stats_format(&f.stats, &ds);
5138 ds_put_cstr(&ds, ", actions:");
5139 format_odp_actions(&ds, f.actions, f.actions_len);
5140 ds_put_char(&ds, '\n');
5142 dpif_flow_dump_thread_destroy(flow_dump_thread);
5143 error = dpif_flow_dump_destroy(flow_dump);
5147 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5148 unixctl_command_reply_error(conn, ds_cstr(&ds));
5150 unixctl_command_reply(conn, ds_cstr(&ds));
5152 odp_portno_names_destroy(&portno_names);
5153 hmap_destroy(&portno_names);
5158 ofproto_revalidate_all_backers(void)
5160 const struct shash_node **backers;
5163 backers = shash_sort(&all_dpif_backers);
5164 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5165 struct dpif_backer *backer = backers[i]->data;
5166 backer->need_revalidate = REV_RECONFIGURE;
5172 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5173 const char *argv[], void *aux OVS_UNUSED)
5175 if (!strcasecmp(argv[1], "off")) {
5176 ofproto_use_tnl_push_pop = false;
5177 unixctl_command_reply(conn, "Tunnel push-pop off");
5178 ofproto_revalidate_all_backers();
5179 } else if (!strcasecmp(argv[1], "on")) {
5180 ofproto_use_tnl_push_pop = true;
5181 unixctl_command_reply(conn, "Tunnel push-pop on");
5182 ofproto_revalidate_all_backers();
5187 ofproto_unixctl_init(void)
5189 static bool registered;
5195 unixctl_command_register(
5197 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5198 1, 3, ofproto_unixctl_trace, NULL);
5199 unixctl_command_register(
5200 "ofproto/trace-packet-out",
5201 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5202 2, 6, ofproto_unixctl_trace_actions, NULL);
5203 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5204 ofproto_unixctl_fdb_flush, NULL);
5205 unixctl_command_register("fdb/show", "bridge", 1, 1,
5206 ofproto_unixctl_fdb_show, NULL);
5207 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5208 ofproto_unixctl_mcast_snooping_flush, NULL);
5209 unixctl_command_register("mdb/show", "bridge", 1, 1,
5210 ofproto_unixctl_mcast_snooping_show, NULL);
5211 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5212 ofproto_unixctl_dpif_dump_dps, NULL);
5213 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5215 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5216 ofproto_unixctl_dpif_dump_flows, NULL);
5218 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5219 disable_tnl_push_pop, NULL);
5222 /* Returns true if 'table' is the table used for internal rules,
5223 * false otherwise. */
5225 table_is_internal(uint8_t table_id)
5227 return table_id == TBL_INTERNAL;
5230 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5232 * This is deprecated. It is only for compatibility with broken device drivers
5233 * in old versions of Linux that do not properly support VLANs when VLAN
5234 * devices are not used. When broken device drivers are no longer in
5235 * widespread use, we will delete these interfaces. */
5238 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
5240 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5241 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5243 if (realdev_ofp_port == ofport->realdev_ofp_port
5244 && vid == ofport->vlandev_vid) {
5248 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5250 if (ofport->realdev_ofp_port) {
5253 if (realdev_ofp_port && ofport->bundle) {
5254 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5255 * themselves be part of a bundle. */
5256 bundle_set(ofport_->ofproto, ofport->bundle, NULL);
5259 ofport->realdev_ofp_port = realdev_ofp_port;
5260 ofport->vlandev_vid = vid;
5262 if (realdev_ofp_port) {
5263 vsp_add(ofport, realdev_ofp_port, vid);
5270 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
5272 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
5276 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
5277 OVS_EXCLUDED(ofproto->vsp_mutex)
5279 /* hmap_is_empty is thread safe. */
5280 return !hmap_is_empty(&ofproto->realdev_vid_map);
5285 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
5286 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5287 OVS_REQUIRES(ofproto->vsp_mutex)
5289 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5290 int vid = vlan_tci_to_vid(vlan_tci);
5291 const struct vlan_splinter *vsp;
5293 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5294 hash_realdev_vid(realdev_ofp_port, vid),
5295 &ofproto->realdev_vid_map) {
5296 if (vsp->realdev_ofp_port == realdev_ofp_port
5297 && vsp->vid == vid) {
5298 return vsp->vlandev_ofp_port;
5302 return realdev_ofp_port;
5305 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5306 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5307 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
5308 * 'vlan_tci' 9, it would return the port number of eth0.9.
5310 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5311 * function just returns its 'realdev_ofp_port' argument. */
5313 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5314 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5315 OVS_EXCLUDED(ofproto->vsp_mutex)
5319 /* hmap_is_empty is thread safe, see if we can return immediately. */
5320 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
5321 return realdev_ofp_port;
5323 ovs_mutex_lock(&ofproto->vsp_mutex);
5324 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
5325 ovs_mutex_unlock(&ofproto->vsp_mutex);
5329 static struct vlan_splinter *
5330 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
5332 struct vlan_splinter *vsp;
5334 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
5335 hash_ofp_port(vlandev_ofp_port),
5336 &ofproto->vlandev_map) {
5337 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5345 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5346 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5347 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
5348 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5349 * eth0 and store 9 in '*vid'.
5351 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5352 * VLAN device. Unless VLAN splinters are enabled, this is what this function
5355 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5356 ofp_port_t vlandev_ofp_port, int *vid)
5357 OVS_REQUIRES(ofproto->vsp_mutex)
5359 if (!hmap_is_empty(&ofproto->vlandev_map)) {
5360 const struct vlan_splinter *vsp;
5362 vsp = vlandev_find(ofproto, vlandev_ofp_port);
5367 return vsp->realdev_ofp_port;
5373 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5374 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
5375 * 'flow->in_port' to the "real" device backing the VLAN device, sets
5376 * 'flow->vlan_tci' to the VLAN VID, and returns true. Optionally pushes the
5377 * appropriate VLAN on 'packet' if provided. Otherwise (which is always the
5378 * case unless VLAN splinters are enabled), returns false without making any
5381 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow,
5382 struct dp_packet *packet)
5383 OVS_EXCLUDED(ofproto->vsp_mutex)
5388 /* hmap_is_empty is thread safe. */
5389 if (hmap_is_empty(&ofproto->vlandev_map)) {
5393 ovs_mutex_lock(&ofproto->vsp_mutex);
5394 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
5395 ovs_mutex_unlock(&ofproto->vsp_mutex);
5400 /* Cause the flow to be processed as if it came in on the real device with
5401 * the VLAN device's VLAN ID. */
5402 flow->in_port.ofp_port = realdev;
5403 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
5406 /* Make the packet resemble the flow, so that it gets sent to an
5407 * OpenFlow controller properly, so that it looks correct for sFlow,
5408 * and so that flow_extract() will get the correct vlan_tci if it is
5409 * called on 'packet'. */
5410 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
5417 vsp_remove(struct ofport_dpif *port)
5419 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5420 struct vlan_splinter *vsp;
5422 ovs_mutex_lock(&ofproto->vsp_mutex);
5423 vsp = vlandev_find(ofproto, port->up.ofp_port);
5425 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5426 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5429 port->realdev_ofp_port = 0;
5431 VLOG_ERR("missing vlan device record");
5433 ovs_mutex_unlock(&ofproto->vsp_mutex);
5437 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
5439 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5441 ovs_mutex_lock(&ofproto->vsp_mutex);
5442 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
5443 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
5444 == realdev_ofp_port)) {
5445 struct vlan_splinter *vsp;
5447 vsp = xmalloc(sizeof *vsp);
5448 vsp->realdev_ofp_port = realdev_ofp_port;
5449 vsp->vlandev_ofp_port = port->up.ofp_port;
5452 port->realdev_ofp_port = realdev_ofp_port;
5454 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
5455 hash_ofp_port(port->up.ofp_port));
5456 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
5457 hash_realdev_vid(realdev_ofp_port, vid));
5459 VLOG_ERR("duplicate vlan device record");
5461 ovs_mutex_unlock(&ofproto->vsp_mutex);
5465 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5467 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
5468 return ofport ? ofport->odp_port : ODPP_NONE;
5471 struct ofport_dpif *
5472 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5474 struct ofport_dpif *port;
5476 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5477 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5478 &backer->odp_to_ofport_map) {
5479 if (port->odp_port == odp_port) {
5480 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5485 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5490 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5492 struct ofport_dpif *port;
5494 port = odp_port_to_ofport(ofproto->backer, odp_port);
5495 if (port && &ofproto->up == port->up.ofproto) {
5496 return port->up.ofp_port;
5502 struct ofproto_dpif *
5503 ofproto_dpif_recirc_get_ofproto(const struct dpif_backer *backer,
5506 struct dpif_backer_recirc_node *node;
5508 node = CONTAINER_OF(cmap_find(&backer->recirc_map, recirc_id),
5509 struct dpif_backer_recirc_node, cmap_node);
5511 return node ? node->ofproto : NULL;
5515 ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto)
5517 struct dpif_backer *backer = ofproto->backer;
5518 uint32_t recirc_id = recirc_id_alloc(backer->rid_pool);
5521 struct dpif_backer_recirc_node *node = xmalloc(sizeof *node);
5523 node->recirc_id = recirc_id;
5524 node->ofproto = ofproto;
5526 ovs_mutex_lock(&backer->recirc_mutex);
5527 cmap_insert(&backer->recirc_map, &node->cmap_node, node->recirc_id);
5528 ovs_mutex_unlock(&backer->recirc_mutex);
5535 ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id)
5537 struct dpif_backer *backer = ofproto->backer;
5538 struct dpif_backer_recirc_node *node;
5540 node = CONTAINER_OF(cmap_find(&backer->recirc_map, recirc_id),
5541 struct dpif_backer_recirc_node, cmap_node);
5543 ovs_mutex_lock(&backer->recirc_mutex);
5544 cmap_remove(&backer->recirc_map, &node->cmap_node, node->recirc_id);
5545 ovs_mutex_unlock(&backer->recirc_mutex);
5546 recirc_id_free(backer->rid_pool, node->recirc_id);
5548 /* 'recirc_id' should never be freed by non-owning 'ofproto'. */
5549 ovs_assert(node->ofproto == ofproto);
5551 /* RCU postpone the free, since other threads may be referring
5552 * to 'node' at same time. */
5553 ovsrcu_postpone(free, node);
5558 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5559 const struct match *match, int priority,
5560 uint16_t idle_timeout,
5561 const struct ofpbuf *ofpacts,
5562 struct rule **rulep)
5564 struct ofputil_flow_mod fm;
5565 struct rule_dpif *rule;
5569 fm.priority = priority;
5570 fm.new_cookie = htonll(0);
5571 fm.cookie = htonll(0);
5572 fm.cookie_mask = htonll(0);
5573 fm.modify_cookie = false;
5574 fm.table_id = TBL_INTERNAL;
5575 fm.command = OFPFC_ADD;
5576 fm.idle_timeout = idle_timeout;
5577 fm.hard_timeout = 0;
5581 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5582 fm.ofpacts = ofpacts->data;
5583 fm.ofpacts_len = ofpacts->size;
5585 error = ofproto_flow_mod(&ofproto->up, &fm);
5587 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5588 ofperr_to_string(error));
5593 rule = rule_dpif_lookup_in_table(ofproto, TBL_INTERNAL, &fm.match.flow,
5594 &fm.match.wc, false);
5604 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5605 struct match *match, int priority)
5607 struct ofputil_flow_mod fm;
5611 fm.priority = priority;
5612 fm.new_cookie = htonll(0);
5613 fm.cookie = htonll(0);
5614 fm.cookie_mask = htonll(0);
5615 fm.modify_cookie = false;
5616 fm.table_id = TBL_INTERNAL;
5617 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5618 fm.command = OFPFC_DELETE_STRICT;
5620 error = ofproto_flow_mod(&ofproto->up, &fm);
5622 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5623 ofperr_to_string(error));
5630 const struct ofproto_class ofproto_dpif_class = {
5644 NULL, /* get_memory_usage. */
5645 type_get_memory_usage,
5663 port_is_lacp_current,
5664 port_get_lacp_stats,
5665 NULL, /* rule_choose_table */
5674 NULL, /* rule_premodify_actions */
5675 rule_modify_actions,
5691 aa_vlan_get_queue_size,
5698 get_stp_port_status,
5703 get_rstp_port_status,
5710 is_mirror_output_bundle,
5711 forward_bpdu_changed,
5712 set_mac_table_config,
5714 set_mcast_snooping_port,
5716 NULL, /* meter_get_features */
5717 NULL, /* meter_set */
5718 NULL, /* meter_get */
5719 NULL, /* meter_del */
5720 group_alloc, /* group_alloc */
5721 group_construct, /* group_construct */
5722 group_destruct, /* group_destruct */
5723 group_dealloc, /* group_dealloc */
5724 group_modify, /* group_modify */
5725 group_get_stats, /* group_get_stats */
5726 get_datapath_version, /* get_datapath_version */