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 /* All datapaths of a given type share a single dpif backer instance. */
265 struct ovs_rwlock odp_to_ofport_lock;
266 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
268 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
270 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
272 bool recv_set_enable; /* Enables or disables receiving packets. */
274 /* Version string of the datapath stored in OVSDB. */
275 char *dp_version_string;
277 /* Datapath feature support. */
278 struct dpif_backer_support support;
279 struct atomic_count tnl_count;
282 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
283 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
285 struct ofproto_dpif {
286 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
288 struct dpif_backer *backer;
290 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
292 /* Special OpenFlow rules. */
293 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
294 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
295 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
298 struct netflow *netflow;
299 struct dpif_sflow *sflow;
300 struct dpif_ipfix *ipfix;
301 struct hmap bundles; /* Contains "struct ofbundle"s. */
302 struct mac_learning *ml;
303 struct mcast_snooping *ms;
304 bool has_bonded_bundles;
306 struct mbridge *mbridge;
308 struct ovs_mutex stats_mutex;
309 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
310 * consumed in userspace. */
314 long long int stp_last_tick;
316 /* Rapid Spanning Tree. */
318 long long int rstp_last_tick;
320 /* VLAN splinters. */
321 struct ovs_mutex vsp_mutex;
322 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
323 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
326 struct sset ports; /* Set of standard port names. */
327 struct sset ghost_ports; /* Ports with no datapath port. */
328 struct sset port_poll_set; /* Queued names for port_poll() reply. */
329 int port_poll_errno; /* Last errno for port_poll() reply. */
330 uint64_t change_seq; /* Connectivity status changes. */
333 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
334 struct seq *pins_seq; /* For notifying 'pins' reception. */
338 /* All existing ofproto_dpif instances, indexed by ->up.name. */
339 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
341 static bool ofproto_use_tnl_push_pop = true;
342 static void ofproto_unixctl_init(void);
344 static inline struct ofproto_dpif *
345 ofproto_dpif_cast(const struct ofproto *ofproto)
347 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
348 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
352 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
354 return ofproto->backer->support.max_mpls_depth;
358 ofproto_dpif_get_enable_recirc(const struct ofproto_dpif *ofproto)
360 return ofproto->backer->support.recirc;
364 ofproto_dpif_get_enable_ufid(struct dpif_backer *backer)
366 return backer->support.ufid;
369 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
370 const struct dp_packet *packet,
371 const struct ofpact[], size_t ofpacts_len,
374 /* Global variables. */
375 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
377 /* Initial mappings of port to bridge mappings. */
378 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
380 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
383 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
384 struct ofputil_flow_mod *fm)
386 ofproto_flow_mod(&ofproto->up, fm);
389 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
390 * Takes ownership of 'pin' and pin->packet. */
392 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
393 struct ofproto_packet_in *pin)
395 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
396 COVERAGE_INC(packet_in_overflow);
397 free(CONST_CAST(void *, pin->up.packet));
401 /* Wakes up main thread for packet-in I/O. */
402 seq_change(ofproto->pins_seq);
405 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
406 * packet rather than to send the packet to the controller.
408 * This function returns false to indicate that a packet_in message
409 * for a "table-miss" should be sent to at least one controller.
410 * False otherwise. */
412 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
414 return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
417 /* Factory functions. */
420 init(const struct shash *iface_hints)
422 struct shash_node *node;
424 /* Make a local copy, since we don't own 'iface_hints' elements. */
425 SHASH_FOR_EACH(node, iface_hints) {
426 const struct iface_hint *orig_hint = node->data;
427 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
429 new_hint->br_name = xstrdup(orig_hint->br_name);
430 new_hint->br_type = xstrdup(orig_hint->br_type);
431 new_hint->ofp_port = orig_hint->ofp_port;
433 shash_add(&init_ofp_ports, node->name, new_hint);
438 enumerate_types(struct sset *types)
440 dp_enumerate_types(types);
444 enumerate_names(const char *type, struct sset *names)
446 struct ofproto_dpif *ofproto;
449 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
450 if (strcmp(type, ofproto->up.type)) {
453 sset_add(names, ofproto->up.name);
460 del(const char *type, const char *name)
465 error = dpif_open(name, type, &dpif);
467 error = dpif_delete(dpif);
474 port_open_type(const char *datapath_type, const char *port_type)
476 return dpif_port_open_type(datapath_type, port_type);
479 /* Type functions. */
481 static void process_dpif_port_changes(struct dpif_backer *);
482 static void process_dpif_all_ports_changed(struct dpif_backer *);
483 static void process_dpif_port_change(struct dpif_backer *,
484 const char *devname);
485 static void process_dpif_port_error(struct dpif_backer *, int error);
487 static struct ofproto_dpif *
488 lookup_ofproto_dpif_by_port_name(const char *name)
490 struct ofproto_dpif *ofproto;
492 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
493 if (sset_contains(&ofproto->ports, name)) {
502 type_run(const char *type)
504 struct dpif_backer *backer;
506 backer = shash_find_data(&all_dpif_backers, type);
508 /* This is not necessarily a problem, since backers are only
509 * created on demand. */
514 if (dpif_run(backer->dpif)) {
515 backer->need_revalidate = REV_RECONFIGURE;
518 udpif_run(backer->udpif);
520 /* If vswitchd started with other_config:flow_restore_wait set as "true",
521 * and the configuration has now changed to "false", enable receiving
522 * packets from the datapath. */
523 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
526 backer->recv_set_enable = true;
528 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
530 VLOG_ERR("Failed to enable receiving packets in dpif.");
533 dpif_flow_flush(backer->dpif);
534 backer->need_revalidate = REV_RECONFIGURE;
537 if (backer->recv_set_enable) {
538 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
541 dpif_poll_threads_set(backer->dpif, n_dpdk_rxqs, pmd_cpu_mask);
543 if (backer->need_revalidate) {
544 struct ofproto_dpif *ofproto;
545 struct simap_node *node;
546 struct simap tmp_backers;
548 /* Handle tunnel garbage collection. */
549 simap_init(&tmp_backers);
550 simap_swap(&backer->tnl_backers, &tmp_backers);
552 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
553 struct ofport_dpif *iter;
555 if (backer != ofproto->backer) {
559 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
560 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
563 if (!iter->is_tunnel) {
567 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
568 namebuf, sizeof namebuf);
569 node = simap_find(&tmp_backers, dp_port);
571 simap_put(&backer->tnl_backers, dp_port, node->data);
572 simap_delete(&tmp_backers, node);
573 node = simap_find(&backer->tnl_backers, dp_port);
575 node = simap_find(&backer->tnl_backers, dp_port);
577 odp_port_t odp_port = ODPP_NONE;
579 if (!dpif_port_add(backer->dpif, iter->up.netdev,
581 simap_put(&backer->tnl_backers, dp_port,
582 odp_to_u32(odp_port));
583 node = simap_find(&backer->tnl_backers, dp_port);
588 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
589 if (tnl_port_reconfigure(iter, iter->up.netdev,
591 ovs_native_tunneling_is_on(ofproto), dp_port)) {
592 backer->need_revalidate = REV_RECONFIGURE;
597 SIMAP_FOR_EACH (node, &tmp_backers) {
598 dpif_port_del(backer->dpif, u32_to_odp(node->data));
600 simap_destroy(&tmp_backers);
602 switch (backer->need_revalidate) {
603 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
604 case REV_STP: COVERAGE_INC(rev_stp); break;
605 case REV_RSTP: COVERAGE_INC(rev_rstp); break;
606 case REV_BOND: COVERAGE_INC(rev_bond); break;
607 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
608 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
609 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
610 case REV_MCAST_SNOOPING: COVERAGE_INC(rev_mcast_snooping); break;
612 backer->need_revalidate = 0;
614 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
615 struct ofport_dpif *ofport;
616 struct ofbundle *bundle;
618 if (ofproto->backer != backer) {
623 xlate_ofproto_set(ofproto, ofproto->up.name,
624 ofproto->backer->dpif, ofproto->ml,
625 ofproto->stp, ofproto->rstp, ofproto->ms,
626 ofproto->mbridge, ofproto->sflow, ofproto->ipfix,
628 ofproto->up.forward_bpdu,
629 connmgr_has_in_band(ofproto->up.connmgr),
630 &ofproto->backer->support);
632 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
633 xlate_bundle_set(ofproto, bundle, bundle->name,
634 bundle->vlan_mode, bundle->vlan,
635 bundle->trunks, bundle->use_priority_tags,
636 bundle->bond, bundle->lacp,
640 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
641 int stp_port = ofport->stp_port
642 ? stp_port_no(ofport->stp_port)
644 xlate_ofport_set(ofproto, ofport->bundle, ofport,
645 ofport->up.ofp_port, ofport->odp_port,
646 ofport->up.netdev, ofport->cfm, ofport->bfd,
647 ofport->lldp, ofport->peer, stp_port,
648 ofport->rstp_port, ofport->qdscp,
649 ofport->n_qdscp, ofport->up.pp.config,
650 ofport->up.pp.state, ofport->is_tunnel,
656 udpif_revalidate(backer->udpif);
659 process_dpif_port_changes(backer);
664 /* Check for and handle port changes in 'backer''s dpif. */
666 process_dpif_port_changes(struct dpif_backer *backer)
672 error = dpif_port_poll(backer->dpif, &devname);
678 process_dpif_all_ports_changed(backer);
682 process_dpif_port_change(backer, devname);
687 process_dpif_port_error(backer, error);
694 process_dpif_all_ports_changed(struct dpif_backer *backer)
696 struct ofproto_dpif *ofproto;
697 struct dpif_port dpif_port;
698 struct dpif_port_dump dump;
699 struct sset devnames;
702 sset_init(&devnames);
703 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
704 if (ofproto->backer == backer) {
705 struct ofport *ofport;
707 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
708 sset_add(&devnames, netdev_get_name(ofport->netdev));
712 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
713 sset_add(&devnames, dpif_port.name);
716 SSET_FOR_EACH (devname, &devnames) {
717 process_dpif_port_change(backer, devname);
719 sset_destroy(&devnames);
723 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
725 struct ofproto_dpif *ofproto;
726 struct dpif_port port;
728 /* Don't report on the datapath's device. */
729 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
733 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
734 &all_ofproto_dpifs) {
735 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
740 ofproto = lookup_ofproto_dpif_by_port_name(devname);
741 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
742 /* The port was removed. If we know the datapath,
743 * report it through poll_set(). If we don't, it may be
744 * notifying us of a removal we initiated, so ignore it.
745 * If there's a pending ENOBUFS, let it stand, since
746 * everything will be reevaluated. */
747 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
748 sset_add(&ofproto->port_poll_set, devname);
749 ofproto->port_poll_errno = 0;
751 } else if (!ofproto) {
752 /* The port was added, but we don't know with which
753 * ofproto we should associate it. Delete it. */
754 dpif_port_del(backer->dpif, port.port_no);
756 struct ofport_dpif *ofport;
758 ofport = ofport_dpif_cast(shash_find_data(
759 &ofproto->up.port_by_name, devname));
761 && ofport->odp_port != port.port_no
762 && !odp_port_to_ofport(backer, port.port_no))
764 /* 'ofport''s datapath port number has changed from
765 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
766 * structures to match. */
767 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
768 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
769 ofport->odp_port = port.port_no;
770 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
771 hash_odp_port(port.port_no));
772 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
773 backer->need_revalidate = REV_RECONFIGURE;
776 dpif_port_destroy(&port);
779 /* Propagate 'error' to all ofprotos based on 'backer'. */
781 process_dpif_port_error(struct dpif_backer *backer, int error)
783 struct ofproto_dpif *ofproto;
785 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
786 if (ofproto->backer == backer) {
787 sset_clear(&ofproto->port_poll_set);
788 ofproto->port_poll_errno = error;
794 type_wait(const char *type)
796 struct dpif_backer *backer;
798 backer = shash_find_data(&all_dpif_backers, type);
800 /* This is not necessarily a problem, since backers are only
801 * created on demand. */
805 dpif_wait(backer->dpif);
808 /* Basic life-cycle. */
810 static int add_internal_flows(struct ofproto_dpif *);
812 static struct ofproto *
815 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
820 dealloc(struct ofproto *ofproto_)
822 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
827 close_dpif_backer(struct dpif_backer *backer)
829 ovs_assert(backer->refcount > 0);
831 if (--backer->refcount) {
835 udpif_destroy(backer->udpif);
837 simap_destroy(&backer->tnl_backers);
838 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
839 hmap_destroy(&backer->odp_to_ofport_map);
840 shash_find_and_delete(&all_dpif_backers, backer->type);
842 free(backer->dp_version_string);
843 dpif_close(backer->dpif);
847 /* Datapath port slated for removal from datapath. */
849 struct ovs_list list_node;
853 static bool check_variable_length_userdata(struct dpif_backer *backer);
854 static void check_support(struct dpif_backer *backer);
857 open_dpif_backer(const char *type, struct dpif_backer **backerp)
859 struct dpif_backer *backer;
860 struct dpif_port_dump port_dump;
861 struct dpif_port port;
862 struct shash_node *node;
863 struct ovs_list garbage_list;
864 struct odp_garbage *garbage;
873 backer = shash_find_data(&all_dpif_backers, type);
880 backer_name = xasprintf("ovs-%s", type);
882 /* Remove any existing datapaths, since we assume we're the only
883 * userspace controlling the datapath. */
885 dp_enumerate_names(type, &names);
886 SSET_FOR_EACH(name, &names) {
887 struct dpif *old_dpif;
889 /* Don't remove our backer if it exists. */
890 if (!strcmp(name, backer_name)) {
894 if (dpif_open(name, type, &old_dpif)) {
895 VLOG_WARN("couldn't open old datapath %s to remove it", name);
897 dpif_delete(old_dpif);
898 dpif_close(old_dpif);
901 sset_destroy(&names);
903 backer = xmalloc(sizeof *backer);
905 error = dpif_create_and_open(backer_name, type, &backer->dpif);
908 VLOG_ERR("failed to open datapath of type %s: %s", type,
909 ovs_strerror(error));
913 backer->udpif = udpif_create(backer, backer->dpif);
915 backer->type = xstrdup(type);
916 backer->refcount = 1;
917 hmap_init(&backer->odp_to_ofport_map);
918 ovs_rwlock_init(&backer->odp_to_ofport_lock);
919 backer->need_revalidate = 0;
920 simap_init(&backer->tnl_backers);
921 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
924 if (backer->recv_set_enable) {
925 dpif_flow_flush(backer->dpif);
928 /* Loop through the ports already on the datapath and remove any
929 * that we don't need anymore. */
930 list_init(&garbage_list);
931 dpif_port_dump_start(&port_dump, backer->dpif);
932 while (dpif_port_dump_next(&port_dump, &port)) {
933 node = shash_find(&init_ofp_ports, port.name);
934 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
935 garbage = xmalloc(sizeof *garbage);
936 garbage->odp_port = port.port_no;
937 list_push_front(&garbage_list, &garbage->list_node);
940 dpif_port_dump_done(&port_dump);
942 LIST_FOR_EACH_POP (garbage, list_node, &garbage_list) {
943 dpif_port_del(backer->dpif, garbage->odp_port);
947 shash_add(&all_dpif_backers, type, backer);
949 check_support(backer);
950 atomic_count_init(&backer->tnl_count, 0);
952 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
954 VLOG_ERR("failed to listen on datapath of type %s: %s",
955 type, ovs_strerror(error));
956 close_dpif_backer(backer);
960 if (backer->recv_set_enable) {
961 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
964 /* This check fails if performed before udpif threads have been set,
965 * as the kernel module checks that the 'pid' in userspace action
967 backer->support.variable_length_userdata
968 = check_variable_length_userdata(backer);
969 backer->dp_version_string = dpif_get_dp_version(backer->dpif);
975 ovs_native_tunneling_is_on(struct ofproto_dpif *ofproto)
977 return ofproto_use_tnl_push_pop && ofproto->backer->support.tnl_push_pop &&
978 atomic_count_get(&ofproto->backer->tnl_count);
981 /* Tests whether 'backer''s datapath supports recirculation. Only newer
982 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
983 * features on older datapaths that don't support this feature.
985 * Returns false if 'backer' definitely does not support recirculation, true if
986 * it seems to support recirculation or if at least the error we get is
989 check_recirc(struct dpif_backer *backer)
992 struct odputil_keybuf keybuf;
996 memset(&flow, 0, sizeof flow);
1000 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1001 odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
1002 enable_recirc = dpif_probe_feature(backer->dpif, "recirculation", &key,
1005 if (enable_recirc) {
1006 VLOG_INFO("%s: Datapath supports recirculation",
1007 dpif_name(backer->dpif));
1009 VLOG_INFO("%s: Datapath does not support recirculation",
1010 dpif_name(backer->dpif));
1013 return enable_recirc;
1016 /* Tests whether 'dpif' supports unique flow ids. We can skip serializing
1017 * some flow attributes for datapaths that support this feature.
1019 * Returns true if 'dpif' supports UFID for flow operations.
1020 * Returns false if 'dpif' does not support UFID. */
1022 check_ufid(struct dpif_backer *backer)
1025 struct odputil_keybuf keybuf;
1030 memset(&flow, 0, sizeof flow);
1031 flow.dl_type = htons(0x1234);
1033 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1034 odp_flow_key_from_flow(&key, &flow, NULL, 0, true);
1035 dpif_flow_hash(backer->dpif, key.data, key.size, &ufid);
1037 enable_ufid = dpif_probe_feature(backer->dpif, "UFID", &key, &ufid);
1040 VLOG_INFO("%s: Datapath supports unique flow ids",
1041 dpif_name(backer->dpif));
1043 VLOG_INFO("%s: Datapath does not support unique flow ids",
1044 dpif_name(backer->dpif));
1049 /* Tests whether 'backer''s datapath supports variable-length
1050 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1051 * to disable some features on older datapaths that don't support this
1054 * Returns false if 'backer' definitely does not support variable-length
1055 * userdata, true if it seems to support them or if at least the error we get
1058 check_variable_length_userdata(struct dpif_backer *backer)
1060 struct eth_header *eth;
1061 struct ofpbuf actions;
1062 struct dpif_execute execute;
1063 struct dp_packet packet;
1067 /* Compose a userspace action that will cause an ERANGE error on older
1068 * datapaths that don't support variable-length userdata.
1070 * We really test for using userdata longer than 8 bytes, but older
1071 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1072 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1073 * test for that instead as a proxy for longer userdata support. */
1074 ofpbuf_init(&actions, 64);
1075 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1076 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1077 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1078 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1079 nl_msg_end_nested(&actions, start);
1081 /* Compose a dummy ethernet packet. */
1082 dp_packet_init(&packet, ETH_HEADER_LEN);
1083 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1084 eth->eth_type = htons(0x1234);
1086 /* Execute the actions. On older datapaths this fails with ERANGE, on
1087 * newer datapaths it succeeds. */
1088 execute.actions = actions.data;
1089 execute.actions_len = actions.size;
1090 execute.packet = &packet;
1091 execute.needs_help = false;
1092 execute.probe = true;
1094 error = dpif_execute(backer->dpif, &execute);
1096 dp_packet_uninit(&packet);
1097 ofpbuf_uninit(&actions);
1104 /* Variable-length userdata is not supported. */
1105 VLOG_WARN("%s: datapath does not support variable-length userdata "
1106 "feature (needs Linux 3.10+ or kernel module from OVS "
1107 "1..11+). The NXAST_SAMPLE action will be ignored.",
1108 dpif_name(backer->dpif));
1112 /* Something odd happened. We're not sure whether variable-length
1113 * userdata is supported. Default to "yes". */
1114 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1115 dpif_name(backer->dpif), ovs_strerror(error));
1120 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1122 * Returns the number of elements in a struct flow's mpls_lse field
1123 * if the datapath supports at least that many entries in an
1125 * Otherwise returns the number of MPLS push actions supported by
1128 check_max_mpls_depth(struct dpif_backer *backer)
1133 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1134 struct odputil_keybuf keybuf;
1137 memset(&flow, 0, sizeof flow);
1138 flow.dl_type = htons(ETH_TYPE_MPLS);
1139 flow_set_mpls_bos(&flow, n, 1);
1141 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1142 odp_flow_key_from_flow(&key, &flow, NULL, 0, false);
1143 if (!dpif_probe_feature(backer->dpif, "MPLS", &key, NULL)) {
1148 VLOG_INFO("%s: MPLS label stack length probed as %d",
1149 dpif_name(backer->dpif), n);
1153 /* Tests whether 'backer''s datapath supports masked data in
1154 * OVS_ACTION_ATTR_SET actions. We need to disable some features on older
1155 * datapaths that don't support this feature. */
1157 check_masked_set_action(struct dpif_backer *backer)
1159 struct eth_header *eth;
1160 struct ofpbuf actions;
1161 struct dpif_execute execute;
1162 struct dp_packet packet;
1164 struct ovs_key_ethernet key, mask;
1166 /* Compose a set action that will cause an EINVAL error on older
1167 * datapaths that don't support masked set actions.
1168 * Avoid using a full mask, as it could be translated to a non-masked
1169 * set action instead. */
1170 ofpbuf_init(&actions, 64);
1171 memset(&key, 0x53, sizeof key);
1172 memset(&mask, 0x7f, sizeof mask);
1173 commit_masked_set_action(&actions, OVS_KEY_ATTR_ETHERNET, &key, &mask,
1176 /* Compose a dummy ethernet packet. */
1177 dp_packet_init(&packet, ETH_HEADER_LEN);
1178 eth = dp_packet_put_zeros(&packet, ETH_HEADER_LEN);
1179 eth->eth_type = htons(0x1234);
1181 /* Execute the actions. On older datapaths this fails with EINVAL, on
1182 * newer datapaths it succeeds. */
1183 execute.actions = actions.data;
1184 execute.actions_len = actions.size;
1185 execute.packet = &packet;
1186 execute.needs_help = false;
1187 execute.probe = true;
1189 error = dpif_execute(backer->dpif, &execute);
1191 dp_packet_uninit(&packet);
1192 ofpbuf_uninit(&actions);
1195 /* Masked set action is not supported. */
1196 VLOG_INFO("%s: datapath does not support masked set action feature.",
1197 dpif_name(backer->dpif));
1203 check_support(struct dpif_backer *backer)
1205 /* This feature needs to be tested after udpif threads are set. */
1206 backer->support.variable_length_userdata = false;
1208 backer->support.recirc = check_recirc(backer);
1209 backer->support.max_mpls_depth = check_max_mpls_depth(backer);
1210 backer->support.masked_set_action = check_masked_set_action(backer);
1211 backer->support.ufid = check_ufid(backer);
1212 backer->support.tnl_push_pop = dpif_supports_tnl_push_pop(backer->dpif);
1216 construct(struct ofproto *ofproto_)
1218 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1219 struct shash_node *node, *next;
1222 /* Tunnel module can get used right after the udpif threads are running. */
1223 ofproto_tunnel_init();
1225 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1230 ofproto->netflow = NULL;
1231 ofproto->sflow = NULL;
1232 ofproto->ipfix = NULL;
1233 ofproto->stp = NULL;
1234 ofproto->rstp = NULL;
1235 ofproto->dump_seq = 0;
1236 hmap_init(&ofproto->bundles);
1237 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1239 ofproto->mbridge = mbridge_create();
1240 ofproto->has_bonded_bundles = false;
1241 ofproto->lacp_enabled = false;
1242 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1243 ovs_mutex_init(&ofproto->vsp_mutex);
1245 guarded_list_init(&ofproto->pins);
1247 ofproto_unixctl_init();
1249 hmap_init(&ofproto->vlandev_map);
1250 hmap_init(&ofproto->realdev_vid_map);
1252 sset_init(&ofproto->ports);
1253 sset_init(&ofproto->ghost_ports);
1254 sset_init(&ofproto->port_poll_set);
1255 ofproto->port_poll_errno = 0;
1256 ofproto->change_seq = 0;
1257 ofproto->pins_seq = seq_create();
1258 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1261 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1262 struct iface_hint *iface_hint = node->data;
1264 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1265 /* Check if the datapath already has this port. */
1266 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1267 sset_add(&ofproto->ports, node->name);
1270 free(iface_hint->br_name);
1271 free(iface_hint->br_type);
1273 shash_delete(&init_ofp_ports, node);
1277 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1278 hash_string(ofproto->up.name, 0));
1279 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1281 ofproto_init_tables(ofproto_, N_TABLES);
1282 error = add_internal_flows(ofproto);
1284 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1290 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1291 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1297 match_init_catchall(&match);
1298 match_set_reg(&match, 0, id);
1300 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, 0, ofpacts,
1302 *rulep = error ? NULL : rule_dpif_cast(rule);
1308 add_internal_flows(struct ofproto_dpif *ofproto)
1310 struct ofpact_controller *controller;
1311 uint64_t ofpacts_stub[128 / 8];
1312 struct ofpbuf ofpacts;
1313 struct rule *unused_rulep OVS_UNUSED;
1318 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1321 controller = ofpact_put_CONTROLLER(&ofpacts);
1322 controller->max_len = UINT16_MAX;
1323 controller->controller_id = 0;
1324 controller->reason = OFPR_NO_MATCH;
1325 ofpact_pad(&ofpacts);
1327 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1328 &ofproto->miss_rule);
1333 ofpbuf_clear(&ofpacts);
1334 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1335 &ofproto->no_packet_in_rule);
1340 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1341 &ofproto->drop_frags_rule);
1346 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1347 * zero when reaching this rule.
1349 * (priority=2), recirc_id=0, actions=drop
1351 ofpbuf_clear(&ofpacts);
1352 match_init_catchall(&match);
1353 match_set_recirc_id(&match, 0);
1354 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, 0, &ofpacts,
1360 destruct(struct ofproto *ofproto_)
1362 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1363 struct ofproto_packet_in *pin;
1364 struct rule_dpif *rule;
1365 struct oftable *table;
1366 struct ovs_list pins;
1368 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1370 xlate_remove_ofproto(ofproto);
1373 /* Ensure that the upcall processing threads have no remaining references
1374 * to the ofproto or anything in it. */
1375 udpif_synchronize(ofproto->backer->udpif);
1377 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1379 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1380 CLS_FOR_EACH (rule, up.cr, &table->cls) {
1381 ofproto_rule_delete(&ofproto->up, &rule->up);
1385 guarded_list_pop_all(&ofproto->pins, &pins);
1386 LIST_FOR_EACH_POP (pin, list_node, &pins) {
1387 free(CONST_CAST(void *, pin->up.packet));
1390 guarded_list_destroy(&ofproto->pins);
1392 recirc_free_ofproto(ofproto, ofproto->up.name);
1394 mbridge_unref(ofproto->mbridge);
1396 netflow_unref(ofproto->netflow);
1397 dpif_sflow_unref(ofproto->sflow);
1398 dpif_ipfix_unref(ofproto->ipfix);
1399 hmap_destroy(&ofproto->bundles);
1400 mac_learning_unref(ofproto->ml);
1401 mcast_snooping_unref(ofproto->ms);
1403 hmap_destroy(&ofproto->vlandev_map);
1404 hmap_destroy(&ofproto->realdev_vid_map);
1406 sset_destroy(&ofproto->ports);
1407 sset_destroy(&ofproto->ghost_ports);
1408 sset_destroy(&ofproto->port_poll_set);
1410 ovs_mutex_destroy(&ofproto->stats_mutex);
1411 ovs_mutex_destroy(&ofproto->vsp_mutex);
1413 seq_destroy(ofproto->pins_seq);
1415 close_dpif_backer(ofproto->backer);
1419 run(struct ofproto *ofproto_)
1421 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1422 uint64_t new_seq, new_dump_seq;
1424 if (mbridge_need_revalidate(ofproto->mbridge)) {
1425 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1426 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1427 mac_learning_flush(ofproto->ml);
1428 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1429 mcast_snooping_mdb_flush(ofproto->ms);
1432 /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1433 * flow restore. Even though nothing is processed during flow restore,
1434 * all queued 'pins' will be handled immediately when flow restore
1436 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1438 /* Do not perform any periodic activity required by 'ofproto' while
1439 * waiting for flow restore to complete. */
1440 if (!ofproto_get_flow_restore_wait()) {
1441 struct ofproto_packet_in *pin;
1442 struct ovs_list pins;
1444 guarded_list_pop_all(&ofproto->pins, &pins);
1445 LIST_FOR_EACH_POP (pin, list_node, &pins) {
1446 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1447 free(CONST_CAST(void *, pin->up.packet));
1452 if (ofproto->netflow) {
1453 netflow_run(ofproto->netflow);
1455 if (ofproto->sflow) {
1456 dpif_sflow_run(ofproto->sflow);
1458 if (ofproto->ipfix) {
1459 dpif_ipfix_run(ofproto->ipfix);
1462 new_seq = seq_read(connectivity_seq_get());
1463 if (ofproto->change_seq != new_seq) {
1464 struct ofport_dpif *ofport;
1466 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1470 ofproto->change_seq = new_seq;
1472 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1473 struct ofbundle *bundle;
1475 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1482 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1483 if (mac_learning_run(ofproto->ml)) {
1484 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1486 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1488 if (mcast_snooping_run(ofproto->ms)) {
1489 ofproto->backer->need_revalidate = REV_MCAST_SNOOPING;
1492 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1493 if (ofproto->dump_seq != new_dump_seq) {
1494 struct rule *rule, *next_rule;
1496 /* We know stats are relatively fresh, so now is a good time to do some
1498 ofproto->dump_seq = new_dump_seq;
1500 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1502 ovs_mutex_lock(&ofproto_mutex);
1503 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1504 &ofproto->up.expirable) {
1505 rule_expire(rule_dpif_cast(rule));
1507 ovs_mutex_unlock(&ofproto_mutex);
1509 /* All outstanding data in existing flows has been accounted, so it's a
1510 * good time to do bond rebalancing. */
1511 if (ofproto->has_bonded_bundles) {
1512 struct ofbundle *bundle;
1514 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1516 bond_rebalance(bundle->bond);
1525 wait(struct ofproto *ofproto_)
1527 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1529 if (ofproto_get_flow_restore_wait()) {
1533 if (ofproto->sflow) {
1534 dpif_sflow_wait(ofproto->sflow);
1536 if (ofproto->ipfix) {
1537 dpif_ipfix_wait(ofproto->ipfix);
1539 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1540 struct ofbundle *bundle;
1542 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1543 bundle_wait(bundle);
1546 if (ofproto->netflow) {
1547 netflow_wait(ofproto->netflow);
1549 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1550 mac_learning_wait(ofproto->ml);
1551 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1552 mcast_snooping_wait(ofproto->ms);
1554 if (ofproto->backer->need_revalidate) {
1555 /* Shouldn't happen, but if it does just go around again. */
1556 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1557 poll_immediate_wake();
1560 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1561 seq_wait(ofproto->pins_seq, ofproto->pins_seqno);
1565 type_get_memory_usage(const char *type, struct simap *usage)
1567 struct dpif_backer *backer;
1569 backer = shash_find_data(&all_dpif_backers, type);
1571 udpif_get_memory_usage(backer->udpif, usage);
1576 flush(struct ofproto *ofproto_)
1578 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1579 struct dpif_backer *backer = ofproto->backer;
1582 udpif_flush(backer->udpif);
1587 query_tables(struct ofproto *ofproto,
1588 struct ofputil_table_features *features,
1589 struct ofputil_table_stats *stats)
1591 strcpy(features->name, "classifier");
1596 for (i = 0; i < ofproto->n_tables; i++) {
1597 unsigned long missed, matched;
1599 atomic_read_relaxed(&ofproto->tables[i].n_matched, &matched);
1600 atomic_read_relaxed(&ofproto->tables[i].n_missed, &missed);
1602 stats[i].matched_count = matched;
1603 stats[i].lookup_count = matched + missed;
1608 static struct ofport *
1611 struct ofport_dpif *port = xmalloc(sizeof *port);
1616 port_dealloc(struct ofport *port_)
1618 struct ofport_dpif *port = ofport_dpif_cast(port_);
1623 port_construct(struct ofport *port_)
1625 struct ofport_dpif *port = ofport_dpif_cast(port_);
1626 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1627 const struct netdev *netdev = port->up.netdev;
1628 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1629 struct dpif_port dpif_port;
1632 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1633 port->bundle = NULL;
1637 port->may_enable = false;
1638 port->stp_port = NULL;
1639 port->stp_state = STP_DISABLED;
1640 port->rstp_port = NULL;
1641 port->rstp_state = RSTP_DISABLED;
1642 port->is_tunnel = false;
1646 port->realdev_ofp_port = 0;
1647 port->vlandev_vid = 0;
1648 port->carrier_seq = netdev_get_carrier_resets(netdev);
1649 port->is_layer3 = netdev_vport_is_layer3(netdev);
1651 if (netdev_vport_is_patch(netdev)) {
1652 /* By bailing out here, we don't submit the port to the sFlow module
1653 * to be considered for counter polling export. This is correct
1654 * because the patch port represents an interface that sFlow considers
1655 * to be "internal" to the switch as a whole, and therefore not a
1656 * candidate for counter polling. */
1657 port->odp_port = ODPP_NONE;
1658 ofport_update_peer(port);
1662 error = dpif_port_query_by_name(ofproto->backer->dpif,
1663 netdev_vport_get_dpif_port(netdev, namebuf,
1670 port->odp_port = dpif_port.port_no;
1672 if (netdev_get_tunnel_config(netdev)) {
1673 atomic_count_inc(&ofproto->backer->tnl_count);
1674 error = tnl_port_add(port, port->up.netdev, port->odp_port,
1675 ovs_native_tunneling_is_on(ofproto), namebuf);
1677 atomic_count_dec(&ofproto->backer->tnl_count);
1678 dpif_port_destroy(&dpif_port);
1682 port->is_tunnel = true;
1683 if (ofproto->ipfix) {
1684 dpif_ipfix_add_tunnel_port(ofproto->ipfix, port_, port->odp_port);
1687 /* Sanity-check that a mapping doesn't already exist. This
1688 * shouldn't happen for non-tunnel ports. */
1689 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1690 VLOG_ERR("port %s already has an OpenFlow port number",
1692 dpif_port_destroy(&dpif_port);
1696 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1697 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1698 hash_odp_port(port->odp_port));
1699 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1701 dpif_port_destroy(&dpif_port);
1703 if (ofproto->sflow) {
1704 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1711 port_destruct(struct ofport *port_)
1713 struct ofport_dpif *port = ofport_dpif_cast(port_);
1714 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1715 const char *devname = netdev_get_name(port->up.netdev);
1716 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1717 const char *dp_port_name;
1719 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1721 xlate_ofport_remove(port);
1724 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1726 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1727 /* The underlying device is still there, so delete it. This
1728 * happens when the ofproto is being destroyed, since the caller
1729 * assumes that removal of attached ports will happen as part of
1731 if (!port->is_tunnel) {
1732 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1737 port->peer->peer = NULL;
1741 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1742 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1743 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1744 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1747 if (port->is_tunnel) {
1748 atomic_count_dec(&ofproto->backer->tnl_count);
1751 if (port->is_tunnel && ofproto->ipfix) {
1752 dpif_ipfix_del_tunnel_port(ofproto->ipfix, port->odp_port);
1756 sset_find_and_delete(&ofproto->ports, devname);
1757 sset_find_and_delete(&ofproto->ghost_ports, devname);
1758 bundle_remove(port_);
1759 set_cfm(port_, NULL);
1760 set_bfd(port_, NULL);
1761 set_lldp(port_, NULL);
1762 if (port->stp_port) {
1763 stp_port_disable(port->stp_port);
1765 set_rstp_port(port_, NULL);
1766 if (ofproto->sflow) {
1767 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1774 port_modified(struct ofport *port_)
1776 struct ofport_dpif *port = ofport_dpif_cast(port_);
1777 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1778 struct netdev *netdev = port->up.netdev;
1780 if (port->bundle && port->bundle->bond) {
1781 bond_slave_set_netdev(port->bundle->bond, port, netdev);
1785 cfm_set_netdev(port->cfm, netdev);
1789 bfd_set_netdev(port->bfd, netdev);
1792 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1793 port->lldp, port->up.pp.hw_addr);
1795 netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
1797 if (port->is_tunnel) {
1798 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1800 if (tnl_port_reconfigure(port, netdev, port->odp_port,
1801 ovs_native_tunneling_is_on(ofproto), namebuf)) {
1802 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1806 ofport_update_peer(port);
1810 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1812 struct ofport_dpif *port = ofport_dpif_cast(port_);
1813 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1814 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1816 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1817 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1818 OFPUTIL_PC_NO_PACKET_IN)) {
1819 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1821 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1822 bundle_update(port->bundle);
1828 set_sflow(struct ofproto *ofproto_,
1829 const struct ofproto_sflow_options *sflow_options)
1831 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1832 struct dpif_sflow *ds = ofproto->sflow;
1834 if (sflow_options) {
1835 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
1837 struct ofport_dpif *ofport;
1839 ds = ofproto->sflow = dpif_sflow_create();
1840 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1841 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1844 dpif_sflow_set_options(ds, sflow_options);
1845 if (dpif_sflow_get_probability(ds) != old_probability) {
1846 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1850 dpif_sflow_unref(ds);
1851 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1852 ofproto->sflow = NULL;
1860 struct ofproto *ofproto_,
1861 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1862 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1863 size_t n_flow_exporters_options)
1865 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1866 struct dpif_ipfix *di = ofproto->ipfix;
1867 bool has_options = bridge_exporter_options || flow_exporters_options;
1868 bool new_di = false;
1870 if (has_options && !di) {
1871 di = ofproto->ipfix = dpif_ipfix_create();
1876 /* Call set_options in any case to cleanly flush the flow
1877 * caches in the last exporters that are to be destroyed. */
1878 dpif_ipfix_set_options(
1879 di, bridge_exporter_options, flow_exporters_options,
1880 n_flow_exporters_options);
1882 /* Add tunnel ports only when a new ipfix created */
1883 if (new_di == true) {
1884 struct ofport_dpif *ofport;
1885 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1886 if (ofport->is_tunnel == true) {
1887 dpif_ipfix_add_tunnel_port(di, &ofport->up, ofport->odp_port);
1893 dpif_ipfix_unref(di);
1894 ofproto->ipfix = NULL;
1902 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1904 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1905 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1906 struct cfm *old = ofport->cfm;
1911 ofport->cfm = cfm_create(ofport->up.netdev);
1914 if (cfm_configure(ofport->cfm, s)) {
1921 cfm_unref(ofport->cfm);
1924 if (ofport->cfm != old) {
1925 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1927 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1928 ofport->lldp, ofport->up.pp.hw_addr);
1933 cfm_status_changed(struct ofport *ofport_)
1935 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1937 return ofport->cfm ? cfm_check_status_change(ofport->cfm) : true;
1941 get_cfm_status(const struct ofport *ofport_,
1942 struct cfm_status *status)
1944 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1948 cfm_get_status(ofport->cfm, status);
1957 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1959 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1960 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1964 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1965 cfg, ofport->up.netdev);
1966 if (ofport->bfd != old) {
1967 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1969 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1970 ofport->lldp, ofport->up.pp.hw_addr);
1975 bfd_status_changed(struct ofport *ofport_)
1977 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1979 return ofport->bfd ? bfd_check_status_change(ofport->bfd) : true;
1983 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1985 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1989 bfd_get_status(ofport->bfd, smap);
1998 set_lldp(struct ofport *ofport_,
1999 const struct smap *cfg)
2001 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2005 if (!ofport->lldp) {
2006 struct ofproto_dpif *ofproto;
2008 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2009 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2010 ofport->lldp = lldp_create(ofport->up.netdev, ofport_->mtu, cfg);
2013 if (!lldp_configure(ofport->lldp, cfg)) {
2018 lldp_unref(ofport->lldp);
2019 ofport->lldp = NULL;
2022 ofproto_dpif_monitor_port_update(ofport,
2026 ofport->up.pp.hw_addr);
2031 get_lldp_status(const struct ofport *ofport_,
2032 struct lldp_status *status OVS_UNUSED)
2034 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2036 return ofport->lldp ? true : false;
2040 set_aa(struct ofproto *ofproto OVS_UNUSED,
2041 const struct aa_settings *s)
2043 return aa_configure(s);
2047 aa_mapping_set(struct ofproto *ofproto_ OVS_UNUSED, void *aux,
2048 const struct aa_mapping_settings *s)
2050 return aa_mapping_register(aux, s);
2054 aa_mapping_unset(struct ofproto *ofproto OVS_UNUSED, void *aux)
2056 return aa_mapping_unregister(aux);
2060 aa_vlan_get_queued(struct ofproto *ofproto OVS_UNUSED, struct ovs_list *list)
2062 return aa_get_vlan_queued(list);
2066 aa_vlan_get_queue_size(struct ofproto *ofproto OVS_UNUSED)
2068 return aa_get_vlan_queue_size();
2072 /* Spanning Tree. */
2074 /* Called while rstp_mutex is held. */
2076 rstp_send_bpdu_cb(struct dp_packet *pkt, void *ofport_, void *ofproto_)
2078 struct ofproto_dpif *ofproto = ofproto_;
2079 struct ofport_dpif *ofport = ofport_;
2080 struct eth_header *eth = dp_packet_l2(pkt);
2082 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2083 if (eth_addr_is_zero(eth->eth_src)) {
2084 VLOG_WARN_RL(&rl, "%s port %d: cannot send RSTP BPDU on a port which "
2085 "does not have a configured source MAC address.",
2086 ofproto->up.name, ofp_to_u16(ofport->up.ofp_port));
2088 ofproto_dpif_send_packet(ofport, pkt);
2090 dp_packet_delete(pkt);
2094 send_bpdu_cb(struct dp_packet *pkt, int port_num, void *ofproto_)
2096 struct ofproto_dpif *ofproto = ofproto_;
2097 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2098 struct ofport_dpif *ofport;
2100 ofport = stp_port_get_aux(sp);
2102 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2103 ofproto->up.name, port_num);
2105 struct eth_header *eth = dp_packet_l2(pkt);
2107 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2108 if (eth_addr_is_zero(eth->eth_src)) {
2109 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2110 "with unknown MAC", ofproto->up.name, port_num);
2112 ofproto_dpif_send_packet(ofport, pkt);
2115 dp_packet_delete(pkt);
2118 /* Configure RSTP on 'ofproto_' using the settings defined in 's'. */
2120 set_rstp(struct ofproto *ofproto_, const struct ofproto_rstp_settings *s)
2122 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2124 /* Only revalidate flows if the configuration changed. */
2125 if (!s != !ofproto->rstp) {
2126 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2130 if (!ofproto->rstp) {
2131 ofproto->rstp = rstp_create(ofproto_->name, s->address,
2132 rstp_send_bpdu_cb, ofproto);
2133 ofproto->rstp_last_tick = time_msec();
2135 rstp_set_bridge_address(ofproto->rstp, s->address);
2136 rstp_set_bridge_priority(ofproto->rstp, s->priority);
2137 rstp_set_bridge_ageing_time(ofproto->rstp, s->ageing_time);
2138 rstp_set_bridge_force_protocol_version(ofproto->rstp,
2139 s->force_protocol_version);
2140 rstp_set_bridge_max_age(ofproto->rstp, s->bridge_max_age);
2141 rstp_set_bridge_forward_delay(ofproto->rstp, s->bridge_forward_delay);
2142 rstp_set_bridge_transmit_hold_count(ofproto->rstp,
2143 s->transmit_hold_count);
2145 struct ofport *ofport;
2146 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2147 set_rstp_port(ofport, NULL);
2149 rstp_unref(ofproto->rstp);
2150 ofproto->rstp = NULL;
2155 get_rstp_status(struct ofproto *ofproto_, struct ofproto_rstp_status *s)
2157 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2159 if (ofproto->rstp) {
2161 s->root_id = rstp_get_root_id(ofproto->rstp);
2162 s->bridge_id = rstp_get_bridge_id(ofproto->rstp);
2163 s->designated_id = rstp_get_designated_id(ofproto->rstp);
2164 s->root_path_cost = rstp_get_root_path_cost(ofproto->rstp);
2165 s->designated_port_id = rstp_get_designated_port_id(ofproto->rstp);
2166 s->bridge_port_id = rstp_get_bridge_port_id(ofproto->rstp);
2173 update_rstp_port_state(struct ofport_dpif *ofport)
2175 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2176 enum rstp_state state;
2178 /* Figure out new state. */
2179 state = ofport->rstp_port ? rstp_port_get_state(ofport->rstp_port)
2183 if (ofport->rstp_state != state) {
2184 enum ofputil_port_state of_state;
2187 VLOG_DBG("port %s: RSTP state changed from %s to %s",
2188 netdev_get_name(ofport->up.netdev),
2189 rstp_state_name(ofport->rstp_state),
2190 rstp_state_name(state));
2192 if (rstp_learn_in_state(ofport->rstp_state)
2193 != rstp_learn_in_state(state)) {
2194 /* XXX: Learning action flows should also be flushed. */
2195 if (ofport->bundle) {
2196 if (!rstp_shift_root_learned_address(ofproto->rstp)
2197 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2198 bundle_flush_macs(ofport->bundle, false);
2202 fwd_change = rstp_forward_in_state(ofport->rstp_state)
2203 != rstp_forward_in_state(state);
2205 ofproto->backer->need_revalidate = REV_RSTP;
2206 ofport->rstp_state = state;
2208 if (fwd_change && ofport->bundle) {
2209 bundle_update(ofport->bundle);
2212 /* Update the RSTP state bits in the OpenFlow port description. */
2213 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2214 of_state |= (state == RSTP_LEARNING ? OFPUTIL_PS_STP_LEARN
2215 : state == RSTP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2216 : state == RSTP_DISCARDING ? OFPUTIL_PS_STP_LISTEN
2218 ofproto_port_set_state(&ofport->up, of_state);
2223 rstp_run(struct ofproto_dpif *ofproto)
2225 if (ofproto->rstp) {
2226 long long int now = time_msec();
2227 long long int elapsed = now - ofproto->rstp_last_tick;
2228 struct rstp_port *rp;
2229 struct ofport_dpif *ofport;
2231 /* Every second, decrease the values of the timers. */
2232 if (elapsed >= 1000) {
2233 rstp_tick_timers(ofproto->rstp);
2234 ofproto->rstp_last_tick = now;
2237 while ((ofport = rstp_get_next_changed_port_aux(ofproto->rstp, &rp))) {
2238 update_rstp_port_state(ofport);
2242 /* FIXME: This check should be done on-event (i.e., when setting
2243 * p->fdb_flush) and not periodically.
2245 while ((ofport = rstp_check_and_reset_fdb_flush(ofproto->rstp, &rp))) {
2246 if (!rstp_shift_root_learned_address(ofproto->rstp)
2247 || rstp_get_old_root_aux(ofproto->rstp) != ofport) {
2248 bundle_flush_macs(ofport->bundle, false);
2252 if (rstp_shift_root_learned_address(ofproto->rstp)) {
2253 bundle_move(((struct ofport_dpif *)rstp_get_old_root_aux(ofproto->rstp))->bundle,
2254 ((struct ofport_dpif *)rstp_get_new_root_aux(ofproto->rstp))->bundle);
2255 rstp_reset_root_changed(ofproto->rstp);
2260 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2262 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2264 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2266 /* Only revalidate flows if the configuration changed. */
2267 if (!s != !ofproto->stp) {
2268 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2272 if (!ofproto->stp) {
2273 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2274 send_bpdu_cb, ofproto);
2275 ofproto->stp_last_tick = time_msec();
2278 stp_set_bridge_id(ofproto->stp, s->system_id);
2279 stp_set_bridge_priority(ofproto->stp, s->priority);
2280 stp_set_hello_time(ofproto->stp, s->hello_time);
2281 stp_set_max_age(ofproto->stp, s->max_age);
2282 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2284 struct ofport *ofport;
2286 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2287 set_stp_port(ofport, NULL);
2290 stp_unref(ofproto->stp);
2291 ofproto->stp = NULL;
2298 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2300 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2304 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2305 s->designated_root = stp_get_designated_root(ofproto->stp);
2306 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2315 update_stp_port_state(struct ofport_dpif *ofport)
2317 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2318 enum stp_state state;
2320 /* Figure out new state. */
2321 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2325 if (ofport->stp_state != state) {
2326 enum ofputil_port_state of_state;
2329 VLOG_DBG("port %s: STP state changed from %s to %s",
2330 netdev_get_name(ofport->up.netdev),
2331 stp_state_name(ofport->stp_state),
2332 stp_state_name(state));
2333 if (stp_learn_in_state(ofport->stp_state)
2334 != stp_learn_in_state(state)) {
2335 /* xxx Learning action flows should also be flushed. */
2336 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2337 mac_learning_flush(ofproto->ml);
2338 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2339 mcast_snooping_mdb_flush(ofproto->ms);
2341 fwd_change = stp_forward_in_state(ofport->stp_state)
2342 != stp_forward_in_state(state);
2344 ofproto->backer->need_revalidate = REV_STP;
2345 ofport->stp_state = state;
2346 ofport->stp_state_entered = time_msec();
2348 if (fwd_change && ofport->bundle) {
2349 bundle_update(ofport->bundle);
2352 /* Update the STP state bits in the OpenFlow port description. */
2353 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2354 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2355 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2356 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2357 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2359 ofproto_port_set_state(&ofport->up, of_state);
2363 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2364 * caller is responsible for assigning STP port numbers and ensuring
2365 * there are no duplicates. */
2367 set_stp_port(struct ofport *ofport_,
2368 const struct ofproto_port_stp_settings *s)
2370 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2371 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2372 struct stp_port *sp = ofport->stp_port;
2374 if (!s || !s->enable) {
2376 ofport->stp_port = NULL;
2377 stp_port_disable(sp);
2378 update_stp_port_state(ofport);
2381 } else if (sp && stp_port_no(sp) != s->port_num
2382 && ofport == stp_port_get_aux(sp)) {
2383 /* The port-id changed, so disable the old one if it's not
2384 * already in use by another port. */
2385 stp_port_disable(sp);
2388 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2390 /* Set name before enabling the port so that debugging messages can print
2392 stp_port_set_name(sp, netdev_get_name(ofport->up.netdev));
2393 stp_port_enable(sp);
2395 stp_port_set_aux(sp, ofport);
2396 stp_port_set_priority(sp, s->priority);
2397 stp_port_set_path_cost(sp, s->path_cost);
2399 update_stp_port_state(ofport);
2405 get_stp_port_status(struct ofport *ofport_,
2406 struct ofproto_port_stp_status *s)
2408 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2409 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2410 struct stp_port *sp = ofport->stp_port;
2412 if (!ofproto->stp || !sp) {
2418 s->port_id = stp_port_get_id(sp);
2419 s->state = stp_port_get_state(sp);
2420 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2421 s->role = stp_port_get_role(sp);
2427 get_stp_port_stats(struct ofport *ofport_,
2428 struct ofproto_port_stp_stats *s)
2430 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2431 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2432 struct stp_port *sp = ofport->stp_port;
2434 if (!ofproto->stp || !sp) {
2440 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2446 stp_run(struct ofproto_dpif *ofproto)
2449 long long int now = time_msec();
2450 long long int elapsed = now - ofproto->stp_last_tick;
2451 struct stp_port *sp;
2454 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2455 ofproto->stp_last_tick = now;
2457 while (stp_get_changed_port(ofproto->stp, &sp)) {
2458 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2461 update_stp_port_state(ofport);
2465 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2466 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2467 mac_learning_flush(ofproto->ml);
2468 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2469 mcast_snooping_mdb_flush(ofproto->ms);
2475 stp_wait(struct ofproto_dpif *ofproto)
2478 poll_timer_wait(1000);
2482 /* Configures RSTP on 'ofport_' using the settings defined in 's'. The
2483 * caller is responsible for assigning RSTP port numbers and ensuring
2484 * there are no duplicates. */
2486 set_rstp_port(struct ofport *ofport_,
2487 const struct ofproto_port_rstp_settings *s)
2489 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2490 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2491 struct rstp_port *rp = ofport->rstp_port;
2493 if (!s || !s->enable) {
2495 rstp_port_unref(rp);
2496 ofport->rstp_port = NULL;
2497 update_rstp_port_state(ofport);
2502 /* Check if need to add a new port. */
2504 rp = ofport->rstp_port = rstp_add_port(ofproto->rstp);
2507 rstp_port_set(rp, s->port_num, s->priority, s->path_cost,
2508 s->admin_edge_port, s->auto_edge,
2509 s->admin_p2p_mac_state, s->admin_port_state, s->mcheck,
2511 update_rstp_port_state(ofport);
2512 /* Synchronize operational status. */
2513 rstp_port_set_mac_operational(rp, ofport->may_enable);
2517 get_rstp_port_status(struct ofport *ofport_,
2518 struct ofproto_port_rstp_status *s)
2520 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2521 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2522 struct rstp_port *rp = ofport->rstp_port;
2524 if (!ofproto->rstp || !rp) {
2530 rstp_port_get_status(rp, &s->port_id, &s->state, &s->role,
2531 &s->designated_bridge_id, &s->designated_port_id,
2532 &s->designated_path_cost, &s->tx_count,
2533 &s->rx_count, &s->error_count, &s->uptime);
2538 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2541 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2542 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2544 if (ofport->n_qdscp != n_qdscp
2545 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2546 n_qdscp * sizeof *qdscp))) {
2547 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2548 free(ofport->qdscp);
2549 ofport->qdscp = n_qdscp
2550 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2552 ofport->n_qdscp = n_qdscp;
2560 /* Expires all MAC learning entries associated with 'bundle' and forces its
2561 * ofproto to revalidate every flow.
2563 * Normally MAC learning entries are removed only from the ofproto associated
2564 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2565 * are removed from every ofproto. When patch ports and SLB bonds are in use
2566 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2567 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2568 * with the host from which it migrated. */
2570 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2572 struct ofproto_dpif *ofproto = bundle->ofproto;
2573 struct mac_learning *ml = ofproto->ml;
2574 struct mac_entry *mac, *next_mac;
2576 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2577 ovs_rwlock_wrlock(&ml->rwlock);
2578 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2579 if (mac_entry_get_port(ml, mac) == bundle) {
2581 struct ofproto_dpif *o;
2583 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2585 struct mac_entry *e;
2587 ovs_rwlock_wrlock(&o->ml->rwlock);
2588 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2590 mac_learning_expire(o->ml, e);
2592 ovs_rwlock_unlock(&o->ml->rwlock);
2597 mac_learning_expire(ml, mac);
2600 ovs_rwlock_unlock(&ml->rwlock);
2604 bundle_move(struct ofbundle *old, struct ofbundle *new)
2606 struct ofproto_dpif *ofproto = old->ofproto;
2607 struct mac_learning *ml = ofproto->ml;
2608 struct mac_entry *mac, *next_mac;
2610 ovs_assert(new->ofproto == old->ofproto);
2612 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2613 ovs_rwlock_wrlock(&ml->rwlock);
2614 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2615 if (mac_entry_get_port(ml, mac) == old) {
2616 mac_entry_set_port(ml, mac, new);
2619 ovs_rwlock_unlock(&ml->rwlock);
2622 static struct ofbundle *
2623 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2625 struct ofbundle *bundle;
2627 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2628 &ofproto->bundles) {
2629 if (bundle->aux == aux) {
2637 bundle_update(struct ofbundle *bundle)
2639 struct ofport_dpif *port;
2641 bundle->floodable = true;
2642 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2643 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2645 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2646 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2647 bundle->floodable = false;
2654 bundle_del_port(struct ofport_dpif *port)
2656 struct ofbundle *bundle = port->bundle;
2658 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2660 list_remove(&port->bundle_node);
2661 port->bundle = NULL;
2664 lacp_slave_unregister(bundle->lacp, port);
2667 bond_slave_unregister(bundle->bond, port);
2670 bundle_update(bundle);
2674 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2675 struct lacp_slave_settings *lacp)
2677 struct ofport_dpif *port;
2679 port = ofp_port_to_ofport(bundle->ofproto, ofp_port);
2684 if (port->bundle != bundle) {
2685 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2687 bundle_remove(&port->up);
2690 port->bundle = bundle;
2691 list_push_back(&bundle->ports, &port->bundle_node);
2692 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2694 || (bundle->ofproto->stp && !stp_forward_in_state(port->stp_state))
2695 || (bundle->ofproto->rstp && !rstp_forward_in_state(port->rstp_state))) {
2696 bundle->floodable = false;
2700 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2701 lacp_slave_register(bundle->lacp, port, lacp);
2708 bundle_destroy(struct ofbundle *bundle)
2710 struct ofproto_dpif *ofproto;
2711 struct ofport_dpif *port, *next_port;
2717 ofproto = bundle->ofproto;
2718 mbridge_unregister_bundle(ofproto->mbridge, bundle);
2721 xlate_bundle_remove(bundle);
2724 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2725 bundle_del_port(port);
2728 bundle_flush_macs(bundle, true);
2729 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2731 free(bundle->trunks);
2732 lacp_unref(bundle->lacp);
2733 bond_unref(bundle->bond);
2738 bundle_set(struct ofproto *ofproto_, void *aux,
2739 const struct ofproto_bundle_settings *s)
2741 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2742 bool need_flush = false;
2743 struct ofport_dpif *port;
2744 struct ofbundle *bundle;
2745 unsigned long *trunks;
2751 bundle_destroy(bundle_lookup(ofproto, aux));
2755 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2756 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2758 bundle = bundle_lookup(ofproto, aux);
2760 bundle = xmalloc(sizeof *bundle);
2762 bundle->ofproto = ofproto;
2763 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2764 hash_pointer(aux, 0));
2766 bundle->name = NULL;
2768 list_init(&bundle->ports);
2769 bundle->vlan_mode = PORT_VLAN_TRUNK;
2771 bundle->trunks = NULL;
2772 bundle->use_priority_tags = s->use_priority_tags;
2773 bundle->lacp = NULL;
2774 bundle->bond = NULL;
2776 bundle->floodable = true;
2777 mbridge_register_bundle(ofproto->mbridge, bundle);
2780 if (!bundle->name || strcmp(s->name, bundle->name)) {
2782 bundle->name = xstrdup(s->name);
2787 ofproto->lacp_enabled = true;
2788 if (!bundle->lacp) {
2789 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2790 bundle->lacp = lacp_create();
2792 lacp_configure(bundle->lacp, s->lacp);
2794 lacp_unref(bundle->lacp);
2795 bundle->lacp = NULL;
2798 /* Update set of ports. */
2800 for (i = 0; i < s->n_slaves; i++) {
2801 if (!bundle_add_port(bundle, s->slaves[i],
2802 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2806 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2807 struct ofport_dpif *next_port;
2809 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2810 for (i = 0; i < s->n_slaves; i++) {
2811 if (s->slaves[i] == port->up.ofp_port) {
2816 bundle_del_port(port);
2820 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2822 if (list_is_empty(&bundle->ports)) {
2823 bundle_destroy(bundle);
2827 /* Set VLAN tagging mode */
2828 if (s->vlan_mode != bundle->vlan_mode
2829 || s->use_priority_tags != bundle->use_priority_tags) {
2830 bundle->vlan_mode = s->vlan_mode;
2831 bundle->use_priority_tags = s->use_priority_tags;
2836 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2837 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2839 if (vlan != bundle->vlan) {
2840 bundle->vlan = vlan;
2844 /* Get trunked VLANs. */
2845 switch (s->vlan_mode) {
2846 case PORT_VLAN_ACCESS:
2850 case PORT_VLAN_TRUNK:
2851 trunks = CONST_CAST(unsigned long *, s->trunks);
2854 case PORT_VLAN_NATIVE_UNTAGGED:
2855 case PORT_VLAN_NATIVE_TAGGED:
2856 if (vlan != 0 && (!s->trunks
2857 || !bitmap_is_set(s->trunks, vlan)
2858 || bitmap_is_set(s->trunks, 0))) {
2859 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2861 trunks = bitmap_clone(s->trunks, 4096);
2863 trunks = bitmap_allocate1(4096);
2865 bitmap_set1(trunks, vlan);
2866 bitmap_set0(trunks, 0);
2868 trunks = CONST_CAST(unsigned long *, s->trunks);
2875 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2876 free(bundle->trunks);
2877 if (trunks == s->trunks) {
2878 bundle->trunks = vlan_bitmap_clone(trunks);
2880 bundle->trunks = trunks;
2885 if (trunks != s->trunks) {
2890 if (!list_is_short(&bundle->ports)) {
2891 bundle->ofproto->has_bonded_bundles = true;
2893 if (bond_reconfigure(bundle->bond, s->bond)) {
2894 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2897 bundle->bond = bond_create(s->bond, ofproto);
2898 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2901 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2902 bond_slave_register(bundle->bond, port,
2903 port->up.ofp_port, port->up.netdev);
2906 bond_unref(bundle->bond);
2907 bundle->bond = NULL;
2910 /* If we changed something that would affect MAC learning, un-learn
2911 * everything on this port and force flow revalidation. */
2913 bundle_flush_macs(bundle, false);
2920 bundle_remove(struct ofport *port_)
2922 struct ofport_dpif *port = ofport_dpif_cast(port_);
2923 struct ofbundle *bundle = port->bundle;
2926 bundle_del_port(port);
2927 if (list_is_empty(&bundle->ports)) {
2928 bundle_destroy(bundle);
2929 } else if (list_is_short(&bundle->ports)) {
2930 bond_unref(bundle->bond);
2931 bundle->bond = NULL;
2937 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2939 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2940 struct ofport_dpif *port = port_;
2941 uint8_t ea[ETH_ADDR_LEN];
2944 error = netdev_get_etheraddr(port->up.netdev, ea);
2946 struct dp_packet packet;
2949 dp_packet_init(&packet, 0);
2950 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2952 memcpy(packet_pdu, pdu, pdu_size);
2954 ofproto_dpif_send_packet(port, &packet);
2955 dp_packet_uninit(&packet);
2957 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2958 "%s (%s)", port->bundle->name,
2959 netdev_get_name(port->up.netdev), ovs_strerror(error));
2964 bundle_send_learning_packets(struct ofbundle *bundle)
2966 struct ofproto_dpif *ofproto = bundle->ofproto;
2967 int error, n_packets, n_errors;
2968 struct mac_entry *e;
2970 struct ovs_list list_node;
2971 struct ofport_dpif *port;
2972 struct dp_packet *pkt;
2974 struct ovs_list packets;
2976 list_init(&packets);
2977 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2978 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2979 if (mac_entry_get_port(ofproto->ml, e) != bundle) {
2980 pkt_node = xmalloc(sizeof *pkt_node);
2981 pkt_node->pkt = bond_compose_learning_packet(bundle->bond,
2983 (void **)&pkt_node->port);
2984 list_push_back(&packets, &pkt_node->list_node);
2987 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2989 error = n_packets = n_errors = 0;
2990 LIST_FOR_EACH_POP (pkt_node, list_node, &packets) {
2993 ret = ofproto_dpif_send_packet(pkt_node->port, pkt_node->pkt);
2994 dp_packet_delete(pkt_node->pkt);
3004 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3005 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3006 "packets, last error was: %s",
3007 bundle->name, n_errors, n_packets, ovs_strerror(error));
3009 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3010 bundle->name, n_packets);
3015 bundle_run(struct ofbundle *bundle)
3018 lacp_run(bundle->lacp, send_pdu_cb);
3021 struct ofport_dpif *port;
3023 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
3024 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
3027 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
3028 bundle->ofproto->backer->need_revalidate = REV_BOND;
3031 if (bond_should_send_learning_packets(bundle->bond)) {
3032 bundle_send_learning_packets(bundle);
3038 bundle_wait(struct ofbundle *bundle)
3041 lacp_wait(bundle->lacp);
3044 bond_wait(bundle->bond);
3051 mirror_set__(struct ofproto *ofproto_, void *aux,
3052 const struct ofproto_mirror_settings *s)
3054 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3055 struct ofbundle **srcs, **dsts;
3060 mirror_destroy(ofproto->mbridge, aux);
3064 srcs = xmalloc(s->n_srcs * sizeof *srcs);
3065 dsts = xmalloc(s->n_dsts * sizeof *dsts);
3067 for (i = 0; i < s->n_srcs; i++) {
3068 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
3071 for (i = 0; i < s->n_dsts; i++) {
3072 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
3075 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
3076 s->n_dsts, s->src_vlans,
3077 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
3084 mirror_get_stats__(struct ofproto *ofproto, void *aux,
3085 uint64_t *packets, uint64_t *bytes)
3087 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
3092 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
3094 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3095 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3096 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
3097 mac_learning_flush(ofproto->ml);
3099 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3104 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
3106 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3107 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3108 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
3112 forward_bpdu_changed(struct ofproto *ofproto_)
3114 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3115 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3119 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
3122 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3123 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3124 mac_learning_set_idle_time(ofproto->ml, idle_time);
3125 mac_learning_set_max_entries(ofproto->ml, max_entries);
3126 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3129 /* Configures multicast snooping on 'ofport' using the settings
3130 * defined in 's'. */
3132 set_mcast_snooping(struct ofproto *ofproto_,
3133 const struct ofproto_mcast_snooping_settings *s)
3135 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3137 /* Only revalidate flows if the configuration changed. */
3138 if (!s != !ofproto->ms) {
3139 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3144 ofproto->ms = mcast_snooping_create();
3147 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3148 mcast_snooping_set_idle_time(ofproto->ms, s->idle_time);
3149 mcast_snooping_set_max_entries(ofproto->ms, s->max_entries);
3150 if (mcast_snooping_set_flood_unreg(ofproto->ms, s->flood_unreg)) {
3151 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3153 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3155 mcast_snooping_unref(ofproto->ms);
3162 /* Configures multicast snooping port's flood settings on 'ofproto'. */
3164 set_mcast_snooping_port(struct ofproto *ofproto_, void *aux,
3165 const struct ofproto_mcast_snooping_port_settings *s)
3167 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3168 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
3170 if (ofproto->ms && s) {
3171 ovs_rwlock_wrlock(&ofproto->ms->rwlock);
3172 mcast_snooping_set_port_flood(ofproto->ms, bundle, s->flood);
3173 mcast_snooping_set_port_flood_reports(ofproto->ms, bundle,
3175 ovs_rwlock_unlock(&ofproto->ms->rwlock);
3183 struct ofport_dpif *
3184 ofp_port_to_ofport(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
3186 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
3187 return ofport ? ofport_dpif_cast(ofport) : NULL;
3191 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
3192 struct ofproto_port *ofproto_port,
3193 struct dpif_port *dpif_port)
3195 ofproto_port->name = dpif_port->name;
3196 ofproto_port->type = dpif_port->type;
3197 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
3201 ofport_update_peer(struct ofport_dpif *ofport)
3203 const struct ofproto_dpif *ofproto;
3204 struct dpif_backer *backer;
3207 if (!netdev_vport_is_patch(ofport->up.netdev)) {
3211 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
3212 backer->need_revalidate = REV_RECONFIGURE;
3215 ofport->peer->peer = NULL;
3216 ofport->peer = NULL;
3219 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
3224 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3225 struct ofport *peer_ofport;
3226 struct ofport_dpif *peer;
3229 if (ofproto->backer != backer) {
3233 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
3238 peer = ofport_dpif_cast(peer_ofport);
3239 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
3240 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
3242 ofport->peer = peer;
3243 ofport->peer->peer = ofport;
3253 port_run(struct ofport_dpif *ofport)
3255 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
3256 bool carrier_changed = carrier_seq != ofport->carrier_seq;
3257 bool enable = netdev_get_carrier(ofport->up.netdev);
3258 bool cfm_enable = false;
3259 bool bfd_enable = false;
3261 ofport->carrier_seq = carrier_seq;
3264 int cfm_opup = cfm_get_opup(ofport->cfm);
3266 cfm_enable = !cfm_get_fault(ofport->cfm);
3268 if (cfm_opup >= 0) {
3269 cfm_enable = cfm_enable && cfm_opup;
3274 bfd_enable = bfd_forwarding(ofport->bfd);
3277 if (ofport->bfd || ofport->cfm) {
3278 enable = enable && (cfm_enable || bfd_enable);
3281 if (ofport->bundle) {
3282 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
3283 if (carrier_changed) {
3284 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
3288 if (ofport->may_enable != enable) {
3289 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3291 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
3293 if (ofport->rstp_port) {
3294 rstp_port_set_mac_operational(ofport->rstp_port, enable);
3298 ofport->may_enable = enable;
3302 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
3303 struct ofproto_port *ofproto_port)
3305 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3306 struct dpif_port dpif_port;
3309 if (sset_contains(&ofproto->ghost_ports, devname)) {
3310 const char *type = netdev_get_type_from_name(devname);
3312 /* We may be called before ofproto->up.port_by_name is populated with
3313 * the appropriate ofport. For this reason, we must get the name and
3314 * type from the netdev layer directly. */
3316 const struct ofport *ofport;
3318 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
3319 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3320 ofproto_port->name = xstrdup(devname);
3321 ofproto_port->type = xstrdup(type);
3327 if (!sset_contains(&ofproto->ports, devname)) {
3330 error = dpif_port_query_by_name(ofproto->backer->dpif,
3331 devname, &dpif_port);
3333 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3339 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3341 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3342 const char *devname = netdev_get_name(netdev);
3343 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3344 const char *dp_port_name;
3346 if (netdev_vport_is_patch(netdev)) {
3347 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3351 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3352 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3353 odp_port_t port_no = ODPP_NONE;
3356 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3360 if (netdev_get_tunnel_config(netdev)) {
3361 simap_put(&ofproto->backer->tnl_backers,
3362 dp_port_name, odp_to_u32(port_no));
3366 if (netdev_get_tunnel_config(netdev)) {
3367 sset_add(&ofproto->ghost_ports, devname);
3369 sset_add(&ofproto->ports, devname);
3375 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3377 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3378 struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
3385 sset_find_and_delete(&ofproto->ghost_ports,
3386 netdev_get_name(ofport->up.netdev));
3387 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3388 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3389 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3391 /* The caller is going to close ofport->up.netdev. If this is a
3392 * bonded port, then the bond is using that netdev, so remove it
3393 * from the bond. The client will need to reconfigure everything
3394 * after deleting ports, so then the slave will get re-added. */
3395 bundle_remove(&ofport->up);
3402 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3404 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3407 error = netdev_get_stats(ofport->up.netdev, stats);
3409 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3410 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3412 ovs_mutex_lock(&ofproto->stats_mutex);
3413 /* ofproto->stats.tx_packets represents packets that we created
3414 * internally and sent to some port (e.g. packets sent with
3415 * ofproto_dpif_send_packet()). Account for them as if they had
3416 * come from OFPP_LOCAL and got forwarded. */
3418 if (stats->rx_packets != UINT64_MAX) {
3419 stats->rx_packets += ofproto->stats.tx_packets;
3422 if (stats->rx_bytes != UINT64_MAX) {
3423 stats->rx_bytes += ofproto->stats.tx_bytes;
3426 /* ofproto->stats.rx_packets represents packets that were received on
3427 * some port and we processed internally and dropped (e.g. STP).
3428 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3430 if (stats->tx_packets != UINT64_MAX) {
3431 stats->tx_packets += ofproto->stats.rx_packets;
3434 if (stats->tx_bytes != UINT64_MAX) {
3435 stats->tx_bytes += ofproto->stats.rx_bytes;
3437 ovs_mutex_unlock(&ofproto->stats_mutex);
3444 port_get_lacp_stats(const struct ofport *ofport_, struct lacp_slave_stats *stats)
3446 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3447 if (ofport->bundle && ofport->bundle->lacp) {
3448 if (lacp_get_slave_stats(ofport->bundle->lacp, ofport, stats)) {
3455 struct port_dump_state {
3460 struct ofproto_port port;
3465 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3467 *statep = xzalloc(sizeof(struct port_dump_state));
3472 port_dump_next(const struct ofproto *ofproto_, void *state_,
3473 struct ofproto_port *port)
3475 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3476 struct port_dump_state *state = state_;
3477 const struct sset *sset;
3478 struct sset_node *node;
3480 if (state->has_port) {
3481 ofproto_port_destroy(&state->port);
3482 state->has_port = false;
3484 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3485 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3488 error = port_query_by_name(ofproto_, node->name, &state->port);
3490 *port = state->port;
3491 state->has_port = true;
3493 } else if (error != ENODEV) {
3498 if (!state->ghost) {
3499 state->ghost = true;
3502 return port_dump_next(ofproto_, state_, port);
3509 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3511 struct port_dump_state *state = state_;
3513 if (state->has_port) {
3514 ofproto_port_destroy(&state->port);
3521 port_poll(const struct ofproto *ofproto_, char **devnamep)
3523 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3525 if (ofproto->port_poll_errno) {
3526 int error = ofproto->port_poll_errno;
3527 ofproto->port_poll_errno = 0;
3531 if (sset_is_empty(&ofproto->port_poll_set)) {
3535 *devnamep = sset_pop(&ofproto->port_poll_set);
3540 port_poll_wait(const struct ofproto *ofproto_)
3542 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3543 dpif_port_poll_wait(ofproto->backer->dpif);
3547 port_is_lacp_current(const struct ofport *ofport_)
3549 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3550 return (ofport->bundle && ofport->bundle->lacp
3551 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3555 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3556 * then delete it entirely. */
3558 rule_expire(struct rule_dpif *rule)
3559 OVS_REQUIRES(ofproto_mutex)
3561 uint16_t hard_timeout, idle_timeout;
3562 long long int now = time_msec();
3565 hard_timeout = rule->up.hard_timeout;
3566 idle_timeout = rule->up.idle_timeout;
3568 /* Has 'rule' expired? */
3570 long long int modified;
3572 ovs_mutex_lock(&rule->up.mutex);
3573 modified = rule->up.modified;
3574 ovs_mutex_unlock(&rule->up.mutex);
3576 if (now > modified + hard_timeout * 1000) {
3577 reason = OFPRR_HARD_TIMEOUT;
3581 if (reason < 0 && idle_timeout) {
3584 ovs_mutex_lock(&rule->stats_mutex);
3585 used = rule->stats.used;
3586 ovs_mutex_unlock(&rule->stats_mutex);
3588 if (now > used + idle_timeout * 1000) {
3589 reason = OFPRR_IDLE_TIMEOUT;
3594 COVERAGE_INC(ofproto_dpif_expired);
3595 ofproto_rule_expire(&rule->up, reason);
3599 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3600 * 'flow' must reflect the data in 'packet'. */
3602 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3603 const struct flow *flow,
3604 struct rule_dpif *rule,
3605 const struct ofpact *ofpacts, size_t ofpacts_len,
3606 struct dp_packet *packet)
3608 struct dpif_flow_stats stats;
3609 struct xlate_out xout;
3610 struct xlate_in xin;
3612 struct dpif_execute execute;
3615 ovs_assert((rule != NULL) != (ofpacts != NULL));
3617 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3620 rule_dpif_credit_stats(rule, &stats);
3623 xlate_in_init(&xin, ofproto, flow, flow->in_port.ofp_port, rule,
3624 stats.tcp_flags, packet);
3625 xin.ofpacts = ofpacts;
3626 xin.ofpacts_len = ofpacts_len;
3627 xin.resubmit_stats = &stats;
3628 xlate_actions(&xin, &xout);
3630 execute.actions = xout.odp_actions->data;
3631 execute.actions_len = xout.odp_actions->size;
3633 pkt_metadata_from_flow(&packet->md, flow);
3634 execute.packet = packet;
3635 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3636 execute.probe = false;
3638 /* Fix up in_port. */
3639 in_port = flow->in_port.ofp_port;
3640 if (in_port == OFPP_NONE) {
3641 in_port = OFPP_LOCAL;
3643 execute.packet->md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3645 error = dpif_execute(ofproto->backer->dpif, &execute);
3647 xlate_out_uninit(&xout);
3653 rule_dpif_credit_stats(struct rule_dpif *rule,
3654 const struct dpif_flow_stats *stats)
3656 ovs_mutex_lock(&rule->stats_mutex);
3657 rule->stats.n_packets += stats->n_packets;
3658 rule->stats.n_bytes += stats->n_bytes;
3659 rule->stats.used = MAX(rule->stats.used, stats->used);
3660 ovs_mutex_unlock(&rule->stats_mutex);
3664 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3665 OVS_REQUIRES(rule->up.mutex)
3667 return rule->up.flow_cookie;
3671 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3672 uint16_t hard_timeout)
3674 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3677 /* Returns 'rule''s actions. The returned actions are RCU-protected, and can
3678 * be read until the calling thread quiesces. */
3679 const struct rule_actions *
3680 rule_dpif_get_actions(const struct rule_dpif *rule)
3682 return rule_get_actions(&rule->up);
3685 /* Sets 'rule''s recirculation id. */
3687 rule_dpif_set_recirc_id(struct rule_dpif *rule, uint32_t id)
3688 OVS_REQUIRES(rule->up.mutex)
3690 ovs_assert(!rule->recirc_id || rule->recirc_id == id);
3691 if (rule->recirc_id == id) {
3692 /* Release the new reference to the same id. */
3695 rule->recirc_id = id;
3699 /* Sets 'rule''s recirculation id. */
3701 rule_set_recirc_id(struct rule *rule_, uint32_t id)
3703 struct rule_dpif *rule = rule_dpif_cast(rule_);
3705 ovs_mutex_lock(&rule->up.mutex);
3706 rule_dpif_set_recirc_id(rule, id);
3707 ovs_mutex_unlock(&rule->up.mutex);
3710 /* The returned rule (if any) is valid at least until the next RCU quiescent
3711 * period. If the rule needs to stay around longer, a non-zero 'take_ref'
3712 * must be passed in to cause a reference to be taken on it.
3714 * 'flow' is non-const to allow for temporary modifications during the lookup.
3715 * Any changes are restored before returning. */
3716 static struct rule_dpif *
3717 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3718 struct flow *flow, struct flow_wildcards *wc,
3721 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3722 const struct cls_rule *cls_rule;
3723 struct rule_dpif *rule;
3726 cls_rule = classifier_lookup(cls, flow, wc);
3728 rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3730 /* Try again if the rule was released before we get the reference. */
3731 } while (rule && take_ref && !rule_dpif_try_ref(rule));
3736 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3737 * Returns the rule that was found, which may be one of the special rules
3738 * according to packet miss hadling. If 'may_packet_in' is false, returning of
3739 * the miss_rule (which issues packet ins for the controller) is avoided.
3740 * Updates 'wc', if nonnull, to reflect the fields that were used during the
3743 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3744 * if none is found then the table miss configuration for that table is
3745 * honored, which can result in additional lookups in other OpenFlow tables.
3746 * In this case the function updates '*table_id' to reflect the final OpenFlow
3747 * table that was searched.
3749 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3752 * The rule is returned in '*rule', which is valid at least until the next
3753 * RCU quiescent period. If the '*rule' needs to stay around longer,
3754 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3755 * on it before this returns.
3757 * 'in_port' allows the lookup to take place as if the in port had the value
3758 * 'in_port'. This is needed for resubmit action support.
3760 * 'flow' is non-const to allow for temporary modifications during the lookup.
3761 * Any changes are restored before returning. */
3763 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto, struct flow *flow,
3764 struct flow_wildcards *wc, bool take_ref,
3765 const struct dpif_flow_stats *stats,
3766 uint8_t *table_id, ofp_port_t in_port,
3767 bool may_packet_in, bool honor_table_miss)
3769 ovs_be16 old_tp_src = flow->tp_src, old_tp_dst = flow->tp_dst;
3770 ofp_port_t old_in_port = flow->in_port.ofp_port;
3771 enum ofputil_table_miss miss_config;
3772 struct rule_dpif *rule;
3775 /* We always unwildcard nw_frag (for IP), so they
3776 * need not be unwildcarded here. */
3777 if (flow->nw_frag & FLOW_NW_FRAG_ANY
3778 && ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3779 if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3780 /* We must pretend that transport ports are unavailable. */
3781 flow->tp_src = htons(0);
3782 flow->tp_dst = htons(0);
3784 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM).
3785 * Use the drop_frags_rule (which cannot disappear). */
3786 rule = ofproto->drop_frags_rule;
3788 rule_dpif_ref(rule);
3791 struct oftable *tbl = &ofproto->up.tables[*table_id];
3794 atomic_add_relaxed(&tbl->n_matched, stats->n_packets, &orig);
3800 /* Look up a flow with 'in_port' as the input port. Then restore the
3801 * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
3802 * have surprising behavior). */
3803 flow->in_port.ofp_port = in_port;
3805 /* Our current implementation depends on n_tables == N_TABLES, and
3806 * TBL_INTERNAL being the last table. */
3807 BUILD_ASSERT_DECL(N_TABLES == TBL_INTERNAL + 1);
3809 miss_config = OFPUTIL_TABLE_MISS_CONTINUE;
3811 for (next_id = *table_id;
3812 next_id < ofproto->up.n_tables;
3813 next_id++, next_id += (next_id == TBL_INTERNAL))
3815 *table_id = next_id;
3816 rule = rule_dpif_lookup_in_table(ofproto, next_id, flow, wc, take_ref);
3818 struct oftable *tbl = &ofproto->up.tables[next_id];
3821 atomic_add_relaxed(rule ? &tbl->n_matched : &tbl->n_missed,
3822 stats->n_packets, &orig);
3825 goto out; /* Match. */
3827 if (honor_table_miss) {
3828 miss_config = ofproto_table_get_miss_config(&ofproto->up,
3830 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE) {
3837 rule = ofproto->no_packet_in_rule;
3838 if (may_packet_in) {
3839 if (miss_config == OFPUTIL_TABLE_MISS_CONTINUE
3840 || miss_config == OFPUTIL_TABLE_MISS_CONTROLLER) {
3841 struct ofport_dpif *port;
3843 port = ofp_port_to_ofport(ofproto, old_in_port);
3845 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3847 } else if (!(port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN)) {
3848 rule = ofproto->miss_rule;
3850 } else if (miss_config == OFPUTIL_TABLE_MISS_DEFAULT &&
3851 connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3852 rule = ofproto->miss_rule;
3856 rule_dpif_ref(rule);
3859 /* Restore port numbers, as they may have been modified above. */
3860 flow->tp_src = old_tp_src;
3861 flow->tp_dst = old_tp_dst;
3862 /* Restore the old in port. */
3863 flow->in_port.ofp_port = old_in_port;
3869 complete_operation(struct rule_dpif *rule)
3870 OVS_REQUIRES(ofproto_mutex)
3872 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3874 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3877 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3879 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3882 static struct rule *
3885 struct rule_dpif *rule = xmalloc(sizeof *rule);
3890 rule_dealloc(struct rule *rule_)
3892 struct rule_dpif *rule = rule_dpif_cast(rule_);
3897 rule_construct(struct rule *rule_)
3898 OVS_NO_THREAD_SAFETY_ANALYSIS
3900 struct rule_dpif *rule = rule_dpif_cast(rule_);
3901 ovs_mutex_init_adaptive(&rule->stats_mutex);
3902 rule->stats.n_packets = 0;
3903 rule->stats.n_bytes = 0;
3904 rule->stats.used = rule->up.modified;
3905 rule->recirc_id = 0;
3911 rule_insert(struct rule *rule_)
3912 OVS_REQUIRES(ofproto_mutex)
3914 struct rule_dpif *rule = rule_dpif_cast(rule_);
3915 complete_operation(rule);
3920 rule_delete(struct rule *rule_)
3921 OVS_REQUIRES(ofproto_mutex)
3923 struct rule_dpif *rule = rule_dpif_cast(rule_);
3924 complete_operation(rule);
3928 rule_destruct(struct rule *rule_)
3930 struct rule_dpif *rule = rule_dpif_cast(rule_);
3932 ovs_mutex_destroy(&rule->stats_mutex);
3933 if (rule->recirc_id) {
3934 recirc_free_id(rule->recirc_id);
3939 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3940 long long int *used)
3942 struct rule_dpif *rule = rule_dpif_cast(rule_);
3944 ovs_mutex_lock(&rule->stats_mutex);
3945 *packets = rule->stats.n_packets;
3946 *bytes = rule->stats.n_bytes;
3947 *used = rule->stats.used;
3948 ovs_mutex_unlock(&rule->stats_mutex);
3952 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3953 struct dp_packet *packet)
3955 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3957 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3961 rule_execute(struct rule *rule, const struct flow *flow,
3962 struct dp_packet *packet)
3964 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3965 dp_packet_delete(packet);
3970 rule_modify_actions(struct rule *rule_, bool reset_counters)
3971 OVS_REQUIRES(ofproto_mutex)
3973 struct rule_dpif *rule = rule_dpif_cast(rule_);
3975 if (reset_counters) {
3976 ovs_mutex_lock(&rule->stats_mutex);
3977 rule->stats.n_packets = 0;
3978 rule->stats.n_bytes = 0;
3979 ovs_mutex_unlock(&rule->stats_mutex);
3982 complete_operation(rule);
3985 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3987 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3990 static struct ofgroup *
3993 struct group_dpif *group = xzalloc(sizeof *group);
3998 group_dealloc(struct ofgroup *group_)
4000 struct group_dpif *group = group_dpif_cast(group_);
4005 group_construct_stats(struct group_dpif *group)
4006 OVS_REQUIRES(group->stats_mutex)
4008 struct ofputil_bucket *bucket;
4009 const struct ovs_list *buckets;
4011 group->packet_count = 0;
4012 group->byte_count = 0;
4014 group_dpif_get_buckets(group, &buckets);
4015 LIST_FOR_EACH (bucket, list_node, buckets) {
4016 bucket->stats.packet_count = 0;
4017 bucket->stats.byte_count = 0;
4022 group_dpif_credit_stats(struct group_dpif *group,
4023 struct ofputil_bucket *bucket,
4024 const struct dpif_flow_stats *stats)
4026 ovs_mutex_lock(&group->stats_mutex);
4027 group->packet_count += stats->n_packets;
4028 group->byte_count += stats->n_bytes;
4030 bucket->stats.packet_count += stats->n_packets;
4031 bucket->stats.byte_count += stats->n_bytes;
4032 } else { /* Credit to all buckets */
4033 const struct ovs_list *buckets;
4035 group_dpif_get_buckets(group, &buckets);
4036 LIST_FOR_EACH (bucket, list_node, buckets) {
4037 bucket->stats.packet_count += stats->n_packets;
4038 bucket->stats.byte_count += stats->n_bytes;
4041 ovs_mutex_unlock(&group->stats_mutex);
4045 group_construct(struct ofgroup *group_)
4047 struct group_dpif *group = group_dpif_cast(group_);
4048 const struct ofputil_bucket *bucket;
4050 /* Prevent group chaining because our locking structure makes it hard to
4051 * implement deadlock-free. (See xlate_group_resource_check().) */
4052 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
4053 const struct ofpact *a;
4055 OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
4056 if (a->type == OFPACT_GROUP) {
4057 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
4062 ovs_mutex_init_adaptive(&group->stats_mutex);
4063 ovs_mutex_lock(&group->stats_mutex);
4064 group_construct_stats(group);
4065 ovs_mutex_unlock(&group->stats_mutex);
4070 group_destruct(struct ofgroup *group_)
4072 struct group_dpif *group = group_dpif_cast(group_);
4073 ovs_mutex_destroy(&group->stats_mutex);
4077 group_modify(struct ofgroup *group_)
4079 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
4081 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4087 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
4089 struct group_dpif *group = group_dpif_cast(group_);
4090 struct ofputil_bucket *bucket;
4091 const struct ovs_list *buckets;
4092 struct bucket_counter *bucket_stats;
4094 ovs_mutex_lock(&group->stats_mutex);
4095 ogs->packet_count = group->packet_count;
4096 ogs->byte_count = group->byte_count;
4098 group_dpif_get_buckets(group, &buckets);
4099 bucket_stats = ogs->bucket_stats;
4100 LIST_FOR_EACH (bucket, list_node, buckets) {
4101 bucket_stats->packet_count = bucket->stats.packet_count;
4102 bucket_stats->byte_count = bucket->stats.byte_count;
4105 ovs_mutex_unlock(&group->stats_mutex);
4110 /* If the group exists, this function increments the groups's reference count.
4112 * Make sure to call group_dpif_unref() after no longer needing to maintain
4113 * a reference to the group. */
4115 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
4116 struct group_dpif **group)
4118 struct ofgroup *ofgroup;
4121 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
4122 *group = found ? group_dpif_cast(ofgroup) : NULL;
4128 group_dpif_get_buckets(const struct group_dpif *group,
4129 const struct ovs_list **buckets)
4131 *buckets = &group->up.buckets;
4134 enum ofp11_group_type
4135 group_dpif_get_type(const struct group_dpif *group)
4137 return group->up.type;
4141 group_dpif_get_selection_method(const struct group_dpif *group)
4143 return group->up.props.selection_method;
4146 /* Sends 'packet' out 'ofport'.
4147 * May modify 'packet'.
4148 * Returns 0 if successful, otherwise a positive errno value. */
4150 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct dp_packet *packet)
4152 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4155 error = xlate_send_packet(ofport, packet);
4157 ovs_mutex_lock(&ofproto->stats_mutex);
4158 ofproto->stats.tx_packets++;
4159 ofproto->stats.tx_bytes += dp_packet_size(packet);
4160 ovs_mutex_unlock(&ofproto->stats_mutex);
4165 group_dpif_get_selection_method_param(const struct group_dpif *group)
4167 return group->up.props.selection_method_param;
4170 const struct field_array *
4171 group_dpif_get_fields(const struct group_dpif *group)
4173 return &group->up.props.fields;
4176 /* Return the version string of the datapath that backs up
4180 get_datapath_version(const struct ofproto *ofproto_)
4182 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4184 return ofproto->backer->dp_version_string;
4188 set_frag_handling(struct ofproto *ofproto_,
4189 enum ofp_config_flags frag_handling)
4191 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4192 if (frag_handling != OFPC_FRAG_REASM) {
4193 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4201 packet_out(struct ofproto *ofproto_, struct dp_packet *packet,
4202 const struct flow *flow,
4203 const struct ofpact *ofpacts, size_t ofpacts_len)
4205 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4207 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
4208 ofpacts_len, packet);
4215 set_netflow(struct ofproto *ofproto_,
4216 const struct netflow_options *netflow_options)
4218 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4220 if (netflow_options) {
4221 if (!ofproto->netflow) {
4222 ofproto->netflow = netflow_create();
4223 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4225 return netflow_set_options(ofproto->netflow, netflow_options);
4226 } else if (ofproto->netflow) {
4227 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4228 netflow_unref(ofproto->netflow);
4229 ofproto->netflow = NULL;
4236 get_netflow_ids(const struct ofproto *ofproto_,
4237 uint8_t *engine_type, uint8_t *engine_id)
4239 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
4241 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
4244 static struct ofproto_dpif *
4245 ofproto_dpif_lookup(const char *name)
4247 struct ofproto_dpif *ofproto;
4249 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
4250 hash_string(name, 0), &all_ofproto_dpifs) {
4251 if (!strcmp(ofproto->up.name, name)) {
4259 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
4260 const char *argv[], void *aux OVS_UNUSED)
4262 struct ofproto_dpif *ofproto;
4265 ofproto = ofproto_dpif_lookup(argv[1]);
4267 unixctl_command_reply_error(conn, "no such bridge");
4270 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4271 mac_learning_flush(ofproto->ml);
4272 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4274 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4275 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
4276 mac_learning_flush(ofproto->ml);
4277 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4281 unixctl_command_reply(conn, "table successfully flushed");
4285 ofproto_unixctl_mcast_snooping_flush(struct unixctl_conn *conn, int argc,
4286 const char *argv[], void *aux OVS_UNUSED)
4288 struct ofproto_dpif *ofproto;
4291 ofproto = ofproto_dpif_lookup(argv[1]);
4293 unixctl_command_reply_error(conn, "no such bridge");
4297 if (!mcast_snooping_enabled(ofproto->ms)) {
4298 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4301 mcast_snooping_mdb_flush(ofproto->ms);
4303 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4304 if (!mcast_snooping_enabled(ofproto->ms)) {
4307 mcast_snooping_mdb_flush(ofproto->ms);
4311 unixctl_command_reply(conn, "table successfully flushed");
4314 static struct ofport_dpif *
4315 ofbundle_get_a_port(const struct ofbundle *bundle)
4317 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
4322 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4323 const char *argv[], void *aux OVS_UNUSED)
4325 struct ds ds = DS_EMPTY_INITIALIZER;
4326 const struct ofproto_dpif *ofproto;
4327 const struct mac_entry *e;
4329 ofproto = ofproto_dpif_lookup(argv[1]);
4331 unixctl_command_reply_error(conn, "no such bridge");
4335 ds_put_cstr(&ds, " port VLAN MAC Age\n");
4336 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
4337 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
4338 struct ofbundle *bundle = mac_entry_get_port(ofproto->ml, e);
4339 char name[OFP_MAX_PORT_NAME_LEN];
4341 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4343 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
4344 name, e->vlan, ETH_ADDR_ARGS(e->mac),
4345 mac_entry_age(ofproto->ml, e));
4347 ovs_rwlock_unlock(&ofproto->ml->rwlock);
4348 unixctl_command_reply(conn, ds_cstr(&ds));
4353 ofproto_unixctl_mcast_snooping_show(struct unixctl_conn *conn,
4354 int argc OVS_UNUSED,
4356 void *aux OVS_UNUSED)
4358 struct ds ds = DS_EMPTY_INITIALIZER;
4359 const struct ofproto_dpif *ofproto;
4360 const struct ofbundle *bundle;
4361 const struct mcast_group *grp;
4362 struct mcast_group_bundle *b;
4363 struct mcast_mrouter_bundle *mrouter;
4365 ofproto = ofproto_dpif_lookup(argv[1]);
4367 unixctl_command_reply_error(conn, "no such bridge");
4371 if (!mcast_snooping_enabled(ofproto->ms)) {
4372 unixctl_command_reply_error(conn, "multicast snooping is disabled");
4376 ds_put_cstr(&ds, " port VLAN GROUP Age\n");
4377 ovs_rwlock_rdlock(&ofproto->ms->rwlock);
4378 LIST_FOR_EACH (grp, group_node, &ofproto->ms->group_lru) {
4379 LIST_FOR_EACH(b, bundle_node, &grp->bundle_lru) {
4380 char name[OFP_MAX_PORT_NAME_LEN];
4383 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4385 ds_put_format(&ds, "%5s %4d "IP_FMT" %3d\n",
4386 name, grp->vlan, IP_ARGS(grp->ip4),
4387 mcast_bundle_age(ofproto->ms, b));
4391 /* ports connected to multicast routers */
4392 LIST_FOR_EACH(mrouter, mrouter_node, &ofproto->ms->mrouter_lru) {
4393 char name[OFP_MAX_PORT_NAME_LEN];
4395 bundle = mrouter->port;
4396 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
4398 ds_put_format(&ds, "%5s %4d querier %3d\n",
4399 name, mrouter->vlan,
4400 mcast_mrouter_age(ofproto->ms, mrouter));
4402 ovs_rwlock_unlock(&ofproto->ms->rwlock);
4403 unixctl_command_reply(conn, ds_cstr(&ds));
4408 struct xlate_out xout;
4409 struct xlate_in xin;
4410 const struct flow *key;
4412 struct flow_wildcards wc;
4417 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
4419 const struct rule_actions *actions;
4422 ds_put_char_multiple(result, '\t', level);
4424 ds_put_cstr(result, "No match\n");
4428 ovs_mutex_lock(&rule->up.mutex);
4429 cookie = rule->up.flow_cookie;
4430 ovs_mutex_unlock(&rule->up.mutex);
4432 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
4433 rule ? rule->up.table_id : 0, ntohll(cookie));
4434 cls_rule_format(&rule->up.cr, result);
4435 ds_put_char(result, '\n');
4437 actions = rule_dpif_get_actions(rule);
4439 ds_put_char_multiple(result, '\t', level);
4440 ds_put_cstr(result, "OpenFlow actions=");
4441 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
4442 ds_put_char(result, '\n');
4446 trace_format_flow(struct ds *result, int level, const char *title,
4447 struct trace_ctx *trace)
4449 ds_put_char_multiple(result, '\t', level);
4450 ds_put_format(result, "%s: ", title);
4451 /* Do not report unchanged flows for resubmits. */
4452 if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
4453 || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
4454 ds_put_cstr(result, "unchanged");
4456 flow_format(result, &trace->xin.flow);
4457 trace->flow = trace->xin.flow;
4459 ds_put_char(result, '\n');
4463 trace_format_regs(struct ds *result, int level, const char *title,
4464 struct trace_ctx *trace)
4468 ds_put_char_multiple(result, '\t', level);
4469 ds_put_format(result, "%s:", title);
4470 for (i = 0; i < FLOW_N_REGS; i++) {
4471 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
4473 ds_put_char(result, '\n');
4477 trace_format_odp(struct ds *result, int level, const char *title,
4478 struct trace_ctx *trace)
4480 struct ofpbuf *odp_actions = trace->xout.odp_actions;
4482 ds_put_char_multiple(result, '\t', level);
4483 ds_put_format(result, "%s: ", title);
4484 format_odp_actions(result, odp_actions->data, odp_actions->size);
4485 ds_put_char(result, '\n');
4489 trace_format_megaflow(struct ds *result, int level, const char *title,
4490 struct trace_ctx *trace)
4494 ds_put_char_multiple(result, '\t', level);
4495 ds_put_format(result, "%s: ", title);
4496 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
4497 match_init(&match, trace->key, &trace->wc);
4498 match_format(&match, result, OFP_DEFAULT_PRIORITY);
4499 ds_put_char(result, '\n');
4502 static void trace_report(struct xlate_in *xin, const char *s, int recurse);
4505 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
4507 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4508 struct ds *result = trace->result;
4511 if (rule == xin->ofproto->miss_rule) {
4512 trace_report(xin, "No match, flow generates \"packet in\"s.",
4514 } else if (rule == xin->ofproto->no_packet_in_rule) {
4515 trace_report(xin, "No match, packets dropped because "
4516 "OFPPC_NO_PACKET_IN is set on in_port.", recurse);
4517 } else if (rule == xin->ofproto->drop_frags_rule) {
4518 trace_report(xin, "Packets dropped because they are IP "
4519 "fragments and the fragment handling mode is "
4520 "\"drop\".", recurse);
4524 ds_put_char(result, '\n');
4526 trace_format_flow(result, recurse, "Resubmitted flow", trace);
4527 trace_format_regs(result, recurse, "Resubmitted regs", trace);
4528 trace_format_odp(result, recurse, "Resubmitted odp", trace);
4529 trace_format_megaflow(result, recurse, "Resubmitted megaflow", trace);
4531 trace_format_rule(result, recurse, rule);
4535 trace_report(struct xlate_in *xin, const char *s, int recurse)
4537 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
4538 struct ds *result = trace->result;
4540 ds_put_char_multiple(result, '\t', recurse);
4541 ds_put_cstr(result, s);
4542 ds_put_char(result, '\n');
4545 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
4546 * forms are supported:
4548 * - [dpname] odp_flow [-generate | packet]
4549 * - bridge br_flow [-generate | packet]
4551 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
4552 * returns a nonnull malloced error message. */
4553 static char * OVS_WARN_UNUSED_RESULT
4554 parse_flow_and_packet(int argc, const char *argv[],
4555 struct ofproto_dpif **ofprotop, struct flow *flow,
4556 struct dp_packet **packetp)
4558 const struct dpif_backer *backer = NULL;
4559 const char *error = NULL;
4561 struct simap port_names = SIMAP_INITIALIZER(&port_names);
4562 struct dp_packet *packet;
4563 struct ofpbuf odp_key;
4564 struct ofpbuf odp_mask;
4566 ofpbuf_init(&odp_key, 0);
4567 ofpbuf_init(&odp_mask, 0);
4569 /* Handle "-generate" or a hex string as the last argument. */
4570 if (!strcmp(argv[argc - 1], "-generate")) {
4571 packet = dp_packet_new(0);
4574 error = eth_from_hex(argv[argc - 1], &packet);
4577 } else if (argc == 4) {
4578 /* The 3-argument form must end in "-generate' or a hex string. */
4584 /* odp_flow can have its in_port specified as a name instead of port no.
4585 * We do not yet know whether a given flow is a odp_flow or a br_flow.
4586 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
4587 * we need to create a simap of name to port no. */
4589 const char *dp_type;
4590 if (!strncmp(argv[1], "ovs-", 4)) {
4591 dp_type = argv[1] + 4;
4595 backer = shash_find_data(&all_dpif_backers, dp_type);
4596 } else if (argc == 2) {
4597 struct shash_node *node;
4598 if (shash_count(&all_dpif_backers) == 1) {
4599 node = shash_first(&all_dpif_backers);
4600 backer = node->data;
4603 error = "Syntax error";
4606 if (backer && backer->dpif) {
4607 struct dpif_port dpif_port;
4608 struct dpif_port_dump port_dump;
4609 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4610 simap_put(&port_names, dpif_port.name,
4611 odp_to_u32(dpif_port.port_no));
4615 /* Parse the flow and determine whether a datapath or
4616 * bridge is specified. If function odp_flow_key_from_string()
4617 * returns 0, the flow is a odp_flow. If function
4618 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4619 if (!odp_flow_from_string(argv[argc - 1], &port_names,
4620 &odp_key, &odp_mask)) {
4622 error = "Cannot find the datapath";
4626 if (odp_flow_key_to_flow(odp_key.data, odp_key.size, flow) == ODP_FIT_ERROR) {
4627 error = "Failed to parse datapath flow key";
4631 *ofprotop = xlate_lookup_ofproto(backer, flow,
4632 &flow->in_port.ofp_port);
4633 if (*ofprotop == NULL) {
4634 error = "Invalid datapath flow";
4638 vsp_adjust_flow(*ofprotop, flow, NULL);
4641 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4644 m_err = xasprintf("Bad openflow flow syntax: %s", err);
4649 error = "Must specify bridge name";
4653 *ofprotop = ofproto_dpif_lookup(argv[1]);
4655 error = "Unknown bridge name";
4661 /* Generate a packet, if requested. */
4663 if (!dp_packet_size(packet)) {
4664 flow_compose(packet, flow);
4666 /* Use the metadata from the flow and the packet argument
4667 * to reconstruct the flow. */
4668 pkt_metadata_from_flow(&packet->md, flow);
4669 flow_extract(packet, flow);
4674 if (error && !m_err) {
4675 m_err = xstrdup(error);
4678 dp_packet_delete(packet);
4682 ofpbuf_uninit(&odp_key);
4683 ofpbuf_uninit(&odp_mask);
4684 simap_destroy(&port_names);
4689 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4690 void *aux OVS_UNUSED)
4692 struct ofproto_dpif *ofproto;
4693 struct dp_packet *packet;
4697 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4702 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4703 unixctl_command_reply(conn, ds_cstr(&result));
4704 ds_destroy(&result);
4705 dp_packet_delete(packet);
4707 unixctl_command_reply_error(conn, error);
4713 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4714 const char *argv[], void *aux OVS_UNUSED)
4716 enum ofputil_protocol usable_protocols;
4717 struct ofproto_dpif *ofproto;
4718 bool enforce_consistency;
4719 struct ofpbuf ofpacts;
4720 struct dp_packet *packet;
4725 /* Three kinds of error return values! */
4731 ofpbuf_init(&ofpacts, 0);
4733 /* Parse actions. */
4734 error = ofpacts_parse_actions(argv[--argc], &ofpacts, &usable_protocols);
4736 unixctl_command_reply_error(conn, error);
4741 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4742 * consistency between the flow and the actions. With -consistent, we
4743 * enforce consistency even for a flow supported in OpenFlow 1.0. */
4744 if (!strcmp(argv[1], "-consistent")) {
4745 enforce_consistency = true;
4749 enforce_consistency = false;
4752 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4754 unixctl_command_reply_error(conn, error);
4759 /* Do the same checks as handle_packet_out() in ofproto.c.
4761 * We pass a 'table_id' of 0 to ofpacts_check(), which isn't
4762 * strictly correct because these actions aren't in any table, but it's OK
4763 * because it 'table_id' is used only to check goto_table instructions, but
4764 * packet-outs take a list of actions and therefore it can't include
4767 * We skip the "meter" check here because meter is an instruction, not an
4768 * action, and thus cannot appear in ofpacts. */
4769 in_port = ofp_to_u16(flow.in_port.ofp_port);
4770 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
4771 unixctl_command_reply_error(conn, "invalid in_port");
4774 if (enforce_consistency) {
4775 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
4776 &flow, u16_to_ofp(ofproto->up.max_ports),
4777 0, 0, usable_protocols);
4779 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
4780 u16_to_ofp(ofproto->up.max_ports), 0, 0,
4786 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4787 unixctl_command_reply_error(conn, ds_cstr(&result));
4791 ofproto_trace(ofproto, &flow, packet,
4792 ofpacts.data, ofpacts.size, &result);
4793 unixctl_command_reply(conn, ds_cstr(&result));
4796 ds_destroy(&result);
4797 dp_packet_delete(packet);
4798 ofpbuf_uninit(&ofpacts);
4801 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4802 * description of the results to 'ds'.
4804 * The trace follows a packet with the specified 'flow' through the flow
4805 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4806 * side effects (if it is nonnull then its flow must be 'flow').
4808 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4809 * trace, otherwise the actions are determined by a flow table lookup. */
4811 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4812 const struct dp_packet *packet,
4813 const struct ofpact ofpacts[], size_t ofpacts_len,
4816 struct trace_ctx trace;
4818 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4819 ds_put_cstr(ds, "Flow: ");
4820 flow_format(ds, flow);
4821 ds_put_char(ds, '\n');
4823 flow_wildcards_init_catchall(&trace.wc);
4826 trace.key = flow; /* Original flow key, used for megaflow. */
4827 trace.flow = *flow; /* May be modified by actions. */
4828 xlate_in_init(&trace.xin, ofproto, flow, flow->in_port.ofp_port, NULL,
4829 ntohs(flow->tcp_flags), packet);
4830 trace.xin.ofpacts = ofpacts;
4831 trace.xin.ofpacts_len = ofpacts_len;
4832 trace.xin.resubmit_hook = trace_resubmit;
4833 trace.xin.report_hook = trace_report;
4835 xlate_actions(&trace.xin, &trace.xout);
4837 ds_put_char(ds, '\n');
4838 trace_format_flow(ds, 0, "Final flow", &trace);
4839 trace_format_megaflow(ds, 0, "Megaflow", &trace);
4841 ds_put_cstr(ds, "Datapath actions: ");
4842 format_odp_actions(ds, trace.xout.odp_actions->data,
4843 trace.xout.odp_actions->size);
4845 if (trace.xout.slow) {
4846 enum slow_path_reason slow;
4848 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4849 "slow path because it:");
4851 slow = trace.xout.slow;
4853 enum slow_path_reason bit = rightmost_1bit(slow);
4855 ds_put_format(ds, "\n\t- %s.",
4856 slow_path_reason_to_explanation(bit));
4862 xlate_out_uninit(&trace.xout);
4865 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4866 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4867 * to destroy 'ofproto_shash' and free the returned value. */
4868 static const struct shash_node **
4869 get_ofprotos(struct shash *ofproto_shash)
4871 const struct ofproto_dpif *ofproto;
4873 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4874 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4875 shash_add_nocopy(ofproto_shash, name, ofproto);
4878 return shash_sort(ofproto_shash);
4882 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4883 const char *argv[] OVS_UNUSED,
4884 void *aux OVS_UNUSED)
4886 struct ds ds = DS_EMPTY_INITIALIZER;
4887 struct shash ofproto_shash;
4888 const struct shash_node **sorted_ofprotos;
4891 shash_init(&ofproto_shash);
4892 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4893 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4894 const struct shash_node *node = sorted_ofprotos[i];
4895 ds_put_format(&ds, "%s\n", node->name);
4898 shash_destroy(&ofproto_shash);
4899 free(sorted_ofprotos);
4901 unixctl_command_reply(conn, ds_cstr(&ds));
4906 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4908 const struct shash_node **ofprotos;
4909 struct dpif_dp_stats dp_stats;
4910 struct shash ofproto_shash;
4913 dpif_get_dp_stats(backer->dpif, &dp_stats);
4915 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4916 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4918 shash_init(&ofproto_shash);
4919 ofprotos = get_ofprotos(&ofproto_shash);
4920 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4921 struct ofproto_dpif *ofproto = ofprotos[i]->data;
4922 const struct shash_node **ports;
4925 if (ofproto->backer != backer) {
4929 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4931 ports = shash_sort(&ofproto->up.port_by_name);
4932 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4933 const struct shash_node *node = ports[j];
4934 struct ofport *ofport = node->data;
4936 odp_port_t odp_port;
4938 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4941 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4942 if (odp_port != ODPP_NONE) {
4943 ds_put_format(ds, "%"PRIu32":", odp_port);
4945 ds_put_cstr(ds, "none:");
4948 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
4951 if (!netdev_get_config(ofport->netdev, &config)) {
4952 const struct smap_node **nodes;
4955 nodes = smap_sort(&config);
4956 for (i = 0; i < smap_count(&config); i++) {
4957 const struct smap_node *node = nodes[i];
4958 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4959 node->key, node->value);
4963 smap_destroy(&config);
4965 ds_put_char(ds, ')');
4966 ds_put_char(ds, '\n');
4970 shash_destroy(&ofproto_shash);
4975 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4976 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4978 struct ds ds = DS_EMPTY_INITIALIZER;
4979 const struct shash_node **backers;
4982 backers = shash_sort(&all_dpif_backers);
4983 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4984 dpif_show_backer(backers[i]->data, &ds);
4988 unixctl_command_reply(conn, ds_cstr(&ds));
4993 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4994 int argc OVS_UNUSED, const char *argv[],
4995 void *aux OVS_UNUSED)
4997 const struct ofproto_dpif *ofproto;
4999 struct ds ds = DS_EMPTY_INITIALIZER;
5000 bool verbosity = false;
5002 struct dpif_port dpif_port;
5003 struct dpif_port_dump port_dump;
5004 struct hmap portno_names;
5006 struct dpif_flow_dump *flow_dump;
5007 struct dpif_flow_dump_thread *flow_dump_thread;
5011 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
5013 unixctl_command_reply_error(conn, "no such bridge");
5017 if (argc > 2 && !strcmp(argv[1], "-m")) {
5021 hmap_init(&portno_names);
5022 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
5023 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
5027 flow_dump = dpif_flow_dump_create(ofproto->backer->dpif, false);
5028 flow_dump_thread = dpif_flow_dump_thread_create(flow_dump);
5029 while (dpif_flow_dump_next(flow_dump_thread, &f, 1)) {
5032 if (odp_flow_key_to_flow(f.key, f.key_len, &flow) == ODP_FIT_ERROR
5033 || xlate_lookup_ofproto(ofproto->backer, &flow, NULL) != ofproto) {
5038 odp_format_ufid(&f.ufid, &ds);
5039 ds_put_cstr(&ds, " ");
5041 odp_flow_format(f.key, f.key_len, f.mask, f.mask_len,
5042 &portno_names, &ds, verbosity);
5043 ds_put_cstr(&ds, ", ");
5044 dpif_flow_stats_format(&f.stats, &ds);
5045 ds_put_cstr(&ds, ", actions:");
5046 format_odp_actions(&ds, f.actions, f.actions_len);
5047 ds_put_char(&ds, '\n');
5049 dpif_flow_dump_thread_destroy(flow_dump_thread);
5050 error = dpif_flow_dump_destroy(flow_dump);
5054 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
5055 unixctl_command_reply_error(conn, ds_cstr(&ds));
5057 unixctl_command_reply(conn, ds_cstr(&ds));
5059 odp_portno_names_destroy(&portno_names);
5060 hmap_destroy(&portno_names);
5065 ofproto_revalidate_all_backers(void)
5067 const struct shash_node **backers;
5070 backers = shash_sort(&all_dpif_backers);
5071 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5072 struct dpif_backer *backer = backers[i]->data;
5073 backer->need_revalidate = REV_RECONFIGURE;
5079 disable_tnl_push_pop(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
5080 const char *argv[], void *aux OVS_UNUSED)
5082 if (!strcasecmp(argv[1], "off")) {
5083 ofproto_use_tnl_push_pop = false;
5084 unixctl_command_reply(conn, "Tunnel push-pop off");
5085 ofproto_revalidate_all_backers();
5086 } else if (!strcasecmp(argv[1], "on")) {
5087 ofproto_use_tnl_push_pop = true;
5088 unixctl_command_reply(conn, "Tunnel push-pop on");
5089 ofproto_revalidate_all_backers();
5094 ofproto_unixctl_init(void)
5096 static bool registered;
5102 unixctl_command_register(
5104 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
5105 1, 3, ofproto_unixctl_trace, NULL);
5106 unixctl_command_register(
5107 "ofproto/trace-packet-out",
5108 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
5109 2, 6, ofproto_unixctl_trace_actions, NULL);
5110 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5111 ofproto_unixctl_fdb_flush, NULL);
5112 unixctl_command_register("fdb/show", "bridge", 1, 1,
5113 ofproto_unixctl_fdb_show, NULL);
5114 unixctl_command_register("mdb/flush", "[bridge]", 0, 1,
5115 ofproto_unixctl_mcast_snooping_flush, NULL);
5116 unixctl_command_register("mdb/show", "bridge", 1, 1,
5117 ofproto_unixctl_mcast_snooping_show, NULL);
5118 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5119 ofproto_unixctl_dpif_dump_dps, NULL);
5120 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5122 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
5123 ofproto_unixctl_dpif_dump_flows, NULL);
5125 unixctl_command_register("ofproto/tnl-push-pop", "[on]|[off]", 1, 1,
5126 disable_tnl_push_pop, NULL);
5129 /* Returns true if 'table' is the table used for internal rules,
5130 * false otherwise. */
5132 table_is_internal(uint8_t table_id)
5134 return table_id == TBL_INTERNAL;
5137 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5139 * This is deprecated. It is only for compatibility with broken device drivers
5140 * in old versions of Linux that do not properly support VLANs when VLAN
5141 * devices are not used. When broken device drivers are no longer in
5142 * widespread use, we will delete these interfaces. */
5145 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
5147 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
5148 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
5150 if (realdev_ofp_port == ofport->realdev_ofp_port
5151 && vid == ofport->vlandev_vid) {
5155 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5157 if (ofport->realdev_ofp_port) {
5160 if (realdev_ofp_port && ofport->bundle) {
5161 /* vlandevs are enslaved to their realdevs, so they are not allowed to
5162 * themselves be part of a bundle. */
5163 bundle_set(ofport_->ofproto, ofport->bundle, NULL);
5166 ofport->realdev_ofp_port = realdev_ofp_port;
5167 ofport->vlandev_vid = vid;
5169 if (realdev_ofp_port) {
5170 vsp_add(ofport, realdev_ofp_port, vid);
5177 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
5179 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
5183 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
5184 OVS_EXCLUDED(ofproto->vsp_mutex)
5186 /* hmap_is_empty is thread safe. */
5187 return !hmap_is_empty(&ofproto->realdev_vid_map);
5192 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
5193 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5194 OVS_REQUIRES(ofproto->vsp_mutex)
5196 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
5197 int vid = vlan_tci_to_vid(vlan_tci);
5198 const struct vlan_splinter *vsp;
5200 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
5201 hash_realdev_vid(realdev_ofp_port, vid),
5202 &ofproto->realdev_vid_map) {
5203 if (vsp->realdev_ofp_port == realdev_ofp_port
5204 && vsp->vid == vid) {
5205 return vsp->vlandev_ofp_port;
5209 return realdev_ofp_port;
5212 /* Returns the OFP port number of the Linux VLAN device that corresponds to
5213 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
5214 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
5215 * 'vlan_tci' 9, it would return the port number of eth0.9.
5217 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
5218 * function just returns its 'realdev_ofp_port' argument. */
5220 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
5221 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
5222 OVS_EXCLUDED(ofproto->vsp_mutex)
5226 /* hmap_is_empty is thread safe, see if we can return immediately. */
5227 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
5228 return realdev_ofp_port;
5230 ovs_mutex_lock(&ofproto->vsp_mutex);
5231 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
5232 ovs_mutex_unlock(&ofproto->vsp_mutex);
5236 static struct vlan_splinter *
5237 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
5239 struct vlan_splinter *vsp;
5241 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
5242 hash_ofp_port(vlandev_ofp_port),
5243 &ofproto->vlandev_map) {
5244 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
5252 /* Returns the OpenFlow port number of the "real" device underlying the Linux
5253 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
5254 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
5255 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
5256 * eth0 and store 9 in '*vid'.
5258 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
5259 * VLAN device. Unless VLAN splinters are enabled, this is what this function
5262 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
5263 ofp_port_t vlandev_ofp_port, int *vid)
5264 OVS_REQUIRES(ofproto->vsp_mutex)
5266 if (!hmap_is_empty(&ofproto->vlandev_map)) {
5267 const struct vlan_splinter *vsp;
5269 vsp = vlandev_find(ofproto, vlandev_ofp_port);
5274 return vsp->realdev_ofp_port;
5280 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
5281 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
5282 * 'flow->in_port' to the "real" device backing the VLAN device, sets
5283 * 'flow->vlan_tci' to the VLAN VID, and returns true. Optionally pushes the
5284 * appropriate VLAN on 'packet' if provided. Otherwise (which is always the
5285 * case unless VLAN splinters are enabled), returns false without making any
5288 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow,
5289 struct dp_packet *packet)
5290 OVS_EXCLUDED(ofproto->vsp_mutex)
5295 /* hmap_is_empty is thread safe. */
5296 if (hmap_is_empty(&ofproto->vlandev_map)) {
5300 ovs_mutex_lock(&ofproto->vsp_mutex);
5301 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
5302 ovs_mutex_unlock(&ofproto->vsp_mutex);
5307 /* Cause the flow to be processed as if it came in on the real device with
5308 * the VLAN device's VLAN ID. */
5309 flow->in_port.ofp_port = realdev;
5310 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
5313 /* Make the packet resemble the flow, so that it gets sent to an
5314 * OpenFlow controller properly, so that it looks correct for sFlow,
5315 * and so that flow_extract() will get the correct vlan_tci if it is
5316 * called on 'packet'. */
5317 eth_push_vlan(packet, htons(ETH_TYPE_VLAN), flow->vlan_tci);
5324 vsp_remove(struct ofport_dpif *port)
5326 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5327 struct vlan_splinter *vsp;
5329 ovs_mutex_lock(&ofproto->vsp_mutex);
5330 vsp = vlandev_find(ofproto, port->up.ofp_port);
5332 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
5333 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
5336 port->realdev_ofp_port = 0;
5338 VLOG_ERR("missing vlan device record");
5340 ovs_mutex_unlock(&ofproto->vsp_mutex);
5344 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
5346 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
5348 ovs_mutex_lock(&ofproto->vsp_mutex);
5349 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
5350 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
5351 == realdev_ofp_port)) {
5352 struct vlan_splinter *vsp;
5354 vsp = xmalloc(sizeof *vsp);
5355 vsp->realdev_ofp_port = realdev_ofp_port;
5356 vsp->vlandev_ofp_port = port->up.ofp_port;
5359 port->realdev_ofp_port = realdev_ofp_port;
5361 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
5362 hash_ofp_port(port->up.ofp_port));
5363 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
5364 hash_realdev_vid(realdev_ofp_port, vid));
5366 VLOG_ERR("duplicate vlan device record");
5368 ovs_mutex_unlock(&ofproto->vsp_mutex);
5372 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
5374 const struct ofport_dpif *ofport = ofp_port_to_ofport(ofproto, ofp_port);
5375 return ofport ? ofport->odp_port : ODPP_NONE;
5378 struct ofport_dpif *
5379 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
5381 struct ofport_dpif *port;
5383 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
5384 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
5385 &backer->odp_to_ofport_map) {
5386 if (port->odp_port == odp_port) {
5387 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5392 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
5397 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
5399 struct ofport_dpif *port;
5401 port = odp_port_to_ofport(ofproto->backer, odp_port);
5402 if (port && &ofproto->up == port->up.ofproto) {
5403 return port->up.ofp_port;
5410 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
5411 const struct match *match, int priority,
5412 uint16_t idle_timeout,
5413 const struct ofpbuf *ofpacts,
5414 struct rule **rulep)
5416 struct ofputil_flow_mod fm;
5417 struct rule_dpif *rule;
5421 fm.priority = priority;
5422 fm.new_cookie = htonll(0);
5423 fm.cookie = htonll(0);
5424 fm.cookie_mask = htonll(0);
5425 fm.modify_cookie = false;
5426 fm.table_id = TBL_INTERNAL;
5427 fm.command = OFPFC_ADD;
5428 fm.idle_timeout = idle_timeout;
5429 fm.hard_timeout = 0;
5433 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5434 fm.ofpacts = ofpacts->data;
5435 fm.ofpacts_len = ofpacts->size;
5437 error = ofproto_flow_mod(&ofproto->up, &fm);
5439 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
5440 ofperr_to_string(error));
5445 rule = rule_dpif_lookup_in_table(ofproto, TBL_INTERNAL, &fm.match.flow,
5446 &fm.match.wc, false);
5456 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
5457 struct match *match, int priority)
5459 struct ofputil_flow_mod fm;
5463 fm.priority = priority;
5464 fm.new_cookie = htonll(0);
5465 fm.cookie = htonll(0);
5466 fm.cookie_mask = htonll(0);
5467 fm.modify_cookie = false;
5468 fm.table_id = TBL_INTERNAL;
5469 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
5470 fm.command = OFPFC_DELETE_STRICT;
5472 error = ofproto_flow_mod(&ofproto->up, &fm);
5474 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
5475 ofperr_to_string(error));
5482 const struct ofproto_class ofproto_dpif_class = {
5496 NULL, /* get_memory_usage. */
5497 type_get_memory_usage,
5515 port_is_lacp_current,
5516 port_get_lacp_stats,
5517 NULL, /* rule_choose_table */
5526 NULL, /* rule_premodify_actions */
5527 rule_modify_actions,
5543 aa_vlan_get_queue_size,
5550 get_stp_port_status,
5555 get_rstp_port_status,
5562 is_mirror_output_bundle,
5563 forward_bpdu_changed,
5564 set_mac_table_config,
5566 set_mcast_snooping_port,
5568 NULL, /* meter_get_features */
5569 NULL, /* meter_set */
5570 NULL, /* meter_get */
5571 NULL, /* meter_del */
5572 group_alloc, /* group_alloc */
5573 group_construct, /* group_construct */
5574 group_destruct, /* group_destruct */
5575 group_dealloc, /* group_dealloc */
5576 group_modify, /* group_modify */
5577 group_get_stats, /* group_get_stats */
5578 get_datapath_version, /* get_datapath_version */