2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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"
33 #include "dynamic-string.h"
34 #include "fail-open.h"
35 #include "guarded-list.h"
39 #include "mac-learning.h"
40 #include "meta-flow.h"
41 #include "multipath.h"
42 #include "netdev-vport.h"
47 #include "odp-execute.h"
50 #include "ofp-actions.h"
51 #include "ofp-parse.h"
52 #include "ofp-print.h"
53 #include "ofproto-dpif-ipfix.h"
54 #include "ofproto-dpif-mirror.h"
55 #include "ofproto-dpif-monitor.h"
56 #include "ofproto-dpif-sflow.h"
57 #include "ofproto-dpif-upcall.h"
58 #include "ofproto-dpif-xlate.h"
59 #include "poll-loop.h"
65 #include "unaligned.h"
67 #include "vlan-bitmap.h"
70 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
72 COVERAGE_DEFINE(ofproto_dpif_expired);
73 COVERAGE_DEFINE(packet_in_overflow);
75 /* Number of implemented OpenFlow tables. */
76 enum { N_TABLES = 255 };
77 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
78 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
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 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
91 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
92 long long int used; /* Last used time (msec). */
95 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
97 static struct rule_dpif *rule_dpif_cast(const struct rule *);
98 static void rule_expire(struct rule_dpif *);
105 * - Do include packets and bytes from datapath flows which have not
106 * recently been processed by a revalidator. */
107 struct ovs_mutex stats_mutex;
108 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
109 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
110 struct bucket_counter *bucket_stats OVS_GUARDED; /* Bucket statistics. */
114 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
115 struct ofproto_dpif *ofproto; /* Owning ofproto. */
116 void *aux; /* Key supplied by ofproto's client. */
117 char *name; /* Identifier for log messages. */
120 struct list ports; /* Contains "struct ofport"s. */
121 enum port_vlan_mode vlan_mode; /* VLAN mode */
122 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
123 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
124 * NULL if all VLANs are trunked. */
125 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
126 struct bond *bond; /* Nonnull iff more than one port. */
127 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
130 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
133 static void bundle_remove(struct ofport *);
134 static void bundle_update(struct ofbundle *);
135 static void bundle_destroy(struct ofbundle *);
136 static void bundle_del_port(struct ofport_dpif *);
137 static void bundle_run(struct ofbundle *);
138 static void bundle_wait(struct ofbundle *);
140 static void stp_run(struct ofproto_dpif *ofproto);
141 static void stp_wait(struct ofproto_dpif *ofproto);
142 static int set_stp_port(struct ofport *,
143 const struct ofproto_port_stp_settings *);
146 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
150 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
151 struct list bundle_node; /* In struct ofbundle's "ports" list. */
152 struct cfm *cfm; /* Connectivity Fault Management, if any. */
153 struct bfd *bfd; /* BFD, if any. */
154 bool may_enable; /* May be enabled in bonds. */
155 bool is_tunnel; /* This port is a tunnel. */
156 bool is_layer3; /* This is a layer 3 port. */
157 long long int carrier_seq; /* Carrier status changes. */
158 struct ofport_dpif *peer; /* Peer if patch port. */
161 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
162 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
163 long long int stp_state_entered;
165 /* Queue to DSCP mapping. */
166 struct ofproto_port_queue *qdscp;
169 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
171 * This is deprecated. It is only for compatibility with broken device
172 * drivers in old versions of Linux that do not properly support VLANs when
173 * VLAN devices are not used. When broken device drivers are no longer in
174 * widespread use, we will delete these interfaces. */
175 ofp_port_t realdev_ofp_port;
179 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
181 * This is deprecated. It is only for compatibility with broken device drivers
182 * in old versions of Linux that do not properly support VLANs when VLAN
183 * devices are not used. When broken device drivers are no longer in
184 * widespread use, we will delete these interfaces. */
185 struct vlan_splinter {
186 struct hmap_node realdev_vid_node;
187 struct hmap_node vlandev_node;
188 ofp_port_t realdev_ofp_port;
189 ofp_port_t vlandev_ofp_port;
193 static void vsp_remove(struct ofport_dpif *);
194 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
196 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
199 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
202 static struct ofport_dpif *
203 ofport_dpif_cast(const struct ofport *ofport)
205 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
208 static void port_run(struct ofport_dpif *);
209 static int set_bfd(struct ofport *, const struct smap *);
210 static int set_cfm(struct ofport *, const struct cfm_settings *);
211 static void ofport_update_peer(struct ofport_dpif *);
213 struct dpif_completion {
214 struct list list_node;
215 struct ofoperation *op;
218 /* Reasons that we might need to revalidate every datapath flow, and
219 * corresponding coverage counters.
221 * A value of 0 means that there is no need to revalidate.
223 * It would be nice to have some cleaner way to integrate with coverage
224 * counters, but with only a few reasons I guess this is good enough for
226 enum revalidate_reason {
227 REV_RECONFIGURE = 1, /* Switch configuration changed. */
228 REV_STP, /* Spanning tree protocol port status change. */
229 REV_BOND, /* Bonding changed. */
230 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
231 REV_FLOW_TABLE, /* Flow table changed. */
232 REV_MAC_LEARNING, /* Mac learning changed. */
234 COVERAGE_DEFINE(rev_reconfigure);
235 COVERAGE_DEFINE(rev_stp);
236 COVERAGE_DEFINE(rev_bond);
237 COVERAGE_DEFINE(rev_port_toggled);
238 COVERAGE_DEFINE(rev_flow_table);
239 COVERAGE_DEFINE(rev_mac_learning);
241 /* All datapaths of a given type share a single dpif backer instance. */
248 struct ovs_rwlock odp_to_ofport_lock;
249 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
251 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
253 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
255 bool recv_set_enable; /* Enables or disables receiving packets. */
257 /* True if the datapath supports variable-length
258 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
259 * False if the datapath supports only 8-byte (or shorter) userdata. */
260 bool variable_length_userdata;
262 /* Maximum number of MPLS label stack entries that the datapath supports
264 size_t max_mpls_depth;
267 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
268 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
270 struct ofproto_dpif {
271 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
273 struct dpif_backer *backer;
275 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
277 /* Special OpenFlow rules. */
278 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
279 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
280 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
283 struct netflow *netflow;
284 struct dpif_sflow *sflow;
285 struct dpif_ipfix *ipfix;
286 struct hmap bundles; /* Contains "struct ofbundle"s. */
287 struct mac_learning *ml;
288 bool has_bonded_bundles;
290 struct mbridge *mbridge;
292 struct ovs_mutex stats_mutex;
293 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
294 * consumed in userspace. */
298 long long int stp_last_tick;
300 /* VLAN splinters. */
301 struct ovs_mutex vsp_mutex;
302 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
303 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
306 struct sset ports; /* Set of standard port names. */
307 struct sset ghost_ports; /* Ports with no datapath port. */
308 struct sset port_poll_set; /* Queued names for port_poll() reply. */
309 int port_poll_errno; /* Last errno for port_poll() reply. */
310 uint64_t change_seq; /* Connectivity status changes. */
313 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
316 /* All existing ofproto_dpif instances, indexed by ->up.name. */
317 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
319 static void ofproto_dpif_unixctl_init(void);
321 static inline struct ofproto_dpif *
322 ofproto_dpif_cast(const struct ofproto *ofproto)
324 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
325 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
329 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
331 return ofproto->backer->max_mpls_depth;
334 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
335 ofp_port_t ofp_port);
336 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
337 const struct ofpbuf *packet,
338 const struct ofpact[], size_t ofpacts_len,
341 /* Global variables. */
342 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
344 /* Initial mappings of port to bridge mappings. */
345 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
347 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
350 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
351 struct ofputil_flow_mod *fm)
353 ofproto_flow_mod(&ofproto->up, fm);
356 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
357 * Takes ownership of 'pin' and pin->packet. */
359 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
360 struct ofproto_packet_in *pin)
362 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
363 COVERAGE_INC(packet_in_overflow);
364 free(CONST_CAST(void *, pin->up.packet));
369 /* Factory functions. */
372 init(const struct shash *iface_hints)
374 struct shash_node *node;
376 /* Make a local copy, since we don't own 'iface_hints' elements. */
377 SHASH_FOR_EACH(node, iface_hints) {
378 const struct iface_hint *orig_hint = node->data;
379 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
381 new_hint->br_name = xstrdup(orig_hint->br_name);
382 new_hint->br_type = xstrdup(orig_hint->br_type);
383 new_hint->ofp_port = orig_hint->ofp_port;
385 shash_add(&init_ofp_ports, node->name, new_hint);
390 enumerate_types(struct sset *types)
392 dp_enumerate_types(types);
396 enumerate_names(const char *type, struct sset *names)
398 struct ofproto_dpif *ofproto;
401 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
402 if (strcmp(type, ofproto->up.type)) {
405 sset_add(names, ofproto->up.name);
412 del(const char *type, const char *name)
417 error = dpif_open(name, type, &dpif);
419 error = dpif_delete(dpif);
426 port_open_type(const char *datapath_type, const char *port_type)
428 return dpif_port_open_type(datapath_type, port_type);
431 /* Type functions. */
433 static void process_dpif_port_changes(struct dpif_backer *);
434 static void process_dpif_all_ports_changed(struct dpif_backer *);
435 static void process_dpif_port_change(struct dpif_backer *,
436 const char *devname);
437 static void process_dpif_port_error(struct dpif_backer *, int error);
439 static struct ofproto_dpif *
440 lookup_ofproto_dpif_by_port_name(const char *name)
442 struct ofproto_dpif *ofproto;
444 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
445 if (sset_contains(&ofproto->ports, name)) {
454 type_run(const char *type)
456 struct dpif_backer *backer;
458 backer = shash_find_data(&all_dpif_backers, type);
460 /* This is not necessarily a problem, since backers are only
461 * created on demand. */
465 dpif_run(backer->dpif);
467 /* If vswitchd started with other_config:flow_restore_wait set as "true",
468 * and the configuration has now changed to "false", enable receiving
469 * packets from the datapath. */
470 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
473 backer->recv_set_enable = true;
475 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
477 VLOG_ERR("Failed to enable receiving packets in dpif.");
480 dpif_flow_flush(backer->dpif);
481 backer->need_revalidate = REV_RECONFIGURE;
484 if (backer->recv_set_enable) {
485 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
488 if (backer->need_revalidate) {
489 struct ofproto_dpif *ofproto;
490 struct simap_node *node;
491 struct simap tmp_backers;
493 /* Handle tunnel garbage collection. */
494 simap_init(&tmp_backers);
495 simap_swap(&backer->tnl_backers, &tmp_backers);
497 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
498 struct ofport_dpif *iter;
500 if (backer != ofproto->backer) {
504 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
505 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
508 if (!iter->is_tunnel) {
512 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
513 namebuf, sizeof namebuf);
514 node = simap_find(&tmp_backers, dp_port);
516 simap_put(&backer->tnl_backers, dp_port, node->data);
517 simap_delete(&tmp_backers, node);
518 node = simap_find(&backer->tnl_backers, dp_port);
520 node = simap_find(&backer->tnl_backers, dp_port);
522 odp_port_t odp_port = ODPP_NONE;
524 if (!dpif_port_add(backer->dpif, iter->up.netdev,
526 simap_put(&backer->tnl_backers, dp_port,
527 odp_to_u32(odp_port));
528 node = simap_find(&backer->tnl_backers, dp_port);
533 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
534 if (tnl_port_reconfigure(iter, iter->up.netdev,
536 backer->need_revalidate = REV_RECONFIGURE;
541 SIMAP_FOR_EACH (node, &tmp_backers) {
542 dpif_port_del(backer->dpif, u32_to_odp(node->data));
544 simap_destroy(&tmp_backers);
546 switch (backer->need_revalidate) {
547 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
548 case REV_STP: COVERAGE_INC(rev_stp); break;
549 case REV_BOND: COVERAGE_INC(rev_bond); break;
550 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
551 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
552 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
554 backer->need_revalidate = 0;
556 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
557 struct ofport_dpif *ofport;
558 struct ofbundle *bundle;
560 if (ofproto->backer != backer) {
564 ovs_rwlock_wrlock(&xlate_rwlock);
565 xlate_ofproto_set(ofproto, ofproto->up.name,
566 ofproto->backer->dpif, ofproto->miss_rule,
567 ofproto->no_packet_in_rule, ofproto->ml,
568 ofproto->stp, ofproto->mbridge,
569 ofproto->sflow, ofproto->ipfix,
570 ofproto->netflow, ofproto->up.frag_handling,
571 ofproto->up.forward_bpdu,
572 connmgr_has_in_band(ofproto->up.connmgr),
573 ofproto->backer->variable_length_userdata,
574 ofproto->backer->max_mpls_depth);
576 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
577 xlate_bundle_set(ofproto, bundle, bundle->name,
578 bundle->vlan_mode, bundle->vlan,
579 bundle->trunks, bundle->use_priority_tags,
580 bundle->bond, bundle->lacp,
584 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
585 int stp_port = ofport->stp_port
586 ? stp_port_no(ofport->stp_port)
588 xlate_ofport_set(ofproto, ofport->bundle, ofport,
589 ofport->up.ofp_port, ofport->odp_port,
590 ofport->up.netdev, ofport->cfm,
591 ofport->bfd, ofport->peer, stp_port,
592 ofport->qdscp, ofport->n_qdscp,
593 ofport->up.pp.config, ofport->up.pp.state,
594 ofport->is_tunnel, ofport->may_enable);
596 ovs_rwlock_unlock(&xlate_rwlock);
599 udpif_revalidate(backer->udpif);
602 process_dpif_port_changes(backer);
607 /* Check for and handle port changes in 'backer''s dpif. */
609 process_dpif_port_changes(struct dpif_backer *backer)
615 error = dpif_port_poll(backer->dpif, &devname);
621 process_dpif_all_ports_changed(backer);
625 process_dpif_port_change(backer, devname);
630 process_dpif_port_error(backer, error);
637 process_dpif_all_ports_changed(struct dpif_backer *backer)
639 struct ofproto_dpif *ofproto;
640 struct dpif_port dpif_port;
641 struct dpif_port_dump dump;
642 struct sset devnames;
645 sset_init(&devnames);
646 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
647 if (ofproto->backer == backer) {
648 struct ofport *ofport;
650 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
651 sset_add(&devnames, netdev_get_name(ofport->netdev));
655 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
656 sset_add(&devnames, dpif_port.name);
659 SSET_FOR_EACH (devname, &devnames) {
660 process_dpif_port_change(backer, devname);
662 sset_destroy(&devnames);
666 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
668 struct ofproto_dpif *ofproto;
669 struct dpif_port port;
671 /* Don't report on the datapath's device. */
672 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
676 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
677 &all_ofproto_dpifs) {
678 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
683 ofproto = lookup_ofproto_dpif_by_port_name(devname);
684 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
685 /* The port was removed. If we know the datapath,
686 * report it through poll_set(). If we don't, it may be
687 * notifying us of a removal we initiated, so ignore it.
688 * If there's a pending ENOBUFS, let it stand, since
689 * everything will be reevaluated. */
690 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
691 sset_add(&ofproto->port_poll_set, devname);
692 ofproto->port_poll_errno = 0;
694 } else if (!ofproto) {
695 /* The port was added, but we don't know with which
696 * ofproto we should associate it. Delete it. */
697 dpif_port_del(backer->dpif, port.port_no);
699 struct ofport_dpif *ofport;
701 ofport = ofport_dpif_cast(shash_find_data(
702 &ofproto->up.port_by_name, devname));
704 && ofport->odp_port != port.port_no
705 && !odp_port_to_ofport(backer, port.port_no))
707 /* 'ofport''s datapath port number has changed from
708 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
709 * structures to match. */
710 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
711 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
712 ofport->odp_port = port.port_no;
713 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
714 hash_odp_port(port.port_no));
715 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
716 backer->need_revalidate = REV_RECONFIGURE;
719 dpif_port_destroy(&port);
722 /* Propagate 'error' to all ofprotos based on 'backer'. */
724 process_dpif_port_error(struct dpif_backer *backer, int error)
726 struct ofproto_dpif *ofproto;
728 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
729 if (ofproto->backer == backer) {
730 sset_clear(&ofproto->port_poll_set);
731 ofproto->port_poll_errno = error;
737 type_wait(const char *type)
739 struct dpif_backer *backer;
741 backer = shash_find_data(&all_dpif_backers, type);
743 /* This is not necessarily a problem, since backers are only
744 * created on demand. */
748 dpif_wait(backer->dpif);
751 /* Basic life-cycle. */
753 static int add_internal_flows(struct ofproto_dpif *);
755 static struct ofproto *
758 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
763 dealloc(struct ofproto *ofproto_)
765 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
770 close_dpif_backer(struct dpif_backer *backer)
772 ovs_assert(backer->refcount > 0);
774 if (--backer->refcount) {
778 udpif_destroy(backer->udpif);
780 simap_destroy(&backer->tnl_backers);
781 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
782 hmap_destroy(&backer->odp_to_ofport_map);
783 shash_find_and_delete(&all_dpif_backers, backer->type);
785 dpif_close(backer->dpif);
790 /* Datapath port slated for removal from datapath. */
792 struct list list_node;
796 static bool check_variable_length_userdata(struct dpif_backer *backer);
797 static size_t check_max_mpls_depth(struct dpif_backer *backer);
800 open_dpif_backer(const char *type, struct dpif_backer **backerp)
802 struct dpif_backer *backer;
803 struct dpif_port_dump port_dump;
804 struct dpif_port port;
805 struct shash_node *node;
806 struct list garbage_list;
807 struct odp_garbage *garbage, *next;
813 backer = shash_find_data(&all_dpif_backers, type);
820 backer_name = xasprintf("ovs-%s", type);
822 /* Remove any existing datapaths, since we assume we're the only
823 * userspace controlling the datapath. */
825 dp_enumerate_names(type, &names);
826 SSET_FOR_EACH(name, &names) {
827 struct dpif *old_dpif;
829 /* Don't remove our backer if it exists. */
830 if (!strcmp(name, backer_name)) {
834 if (dpif_open(name, type, &old_dpif)) {
835 VLOG_WARN("couldn't open old datapath %s to remove it", name);
837 dpif_delete(old_dpif);
838 dpif_close(old_dpif);
841 sset_destroy(&names);
843 backer = xmalloc(sizeof *backer);
845 error = dpif_create_and_open(backer_name, type, &backer->dpif);
848 VLOG_ERR("failed to open datapath of type %s: %s", type,
849 ovs_strerror(error));
853 backer->udpif = udpif_create(backer, backer->dpif);
855 backer->type = xstrdup(type);
856 backer->refcount = 1;
857 hmap_init(&backer->odp_to_ofport_map);
858 ovs_rwlock_init(&backer->odp_to_ofport_lock);
859 backer->need_revalidate = 0;
860 simap_init(&backer->tnl_backers);
861 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
864 if (backer->recv_set_enable) {
865 dpif_flow_flush(backer->dpif);
868 /* Loop through the ports already on the datapath and remove any
869 * that we don't need anymore. */
870 list_init(&garbage_list);
871 dpif_port_dump_start(&port_dump, backer->dpif);
872 while (dpif_port_dump_next(&port_dump, &port)) {
873 node = shash_find(&init_ofp_ports, port.name);
874 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
875 garbage = xmalloc(sizeof *garbage);
876 garbage->odp_port = port.port_no;
877 list_push_front(&garbage_list, &garbage->list_node);
880 dpif_port_dump_done(&port_dump);
882 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
883 dpif_port_del(backer->dpif, garbage->odp_port);
884 list_remove(&garbage->list_node);
888 shash_add(&all_dpif_backers, type, backer);
890 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
892 VLOG_ERR("failed to listen on datapath of type %s: %s",
893 type, ovs_strerror(error));
894 close_dpif_backer(backer);
897 backer->variable_length_userdata = check_variable_length_userdata(backer);
898 backer->max_mpls_depth = check_max_mpls_depth(backer);
900 if (backer->recv_set_enable) {
901 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
907 /* Tests whether 'backer''s datapath supports variable-length
908 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
909 * to disable some features on older datapaths that don't support this
912 * Returns false if 'backer' definitely does not support variable-length
913 * userdata, true if it seems to support them or if at least the error we get
916 check_variable_length_userdata(struct dpif_backer *backer)
918 struct eth_header *eth;
919 struct ofpbuf actions;
920 struct dpif_execute execute;
921 struct ofpbuf packet;
925 /* Compose a userspace action that will cause an ERANGE error on older
926 * datapaths that don't support variable-length userdata.
928 * We really test for using userdata longer than 8 bytes, but older
929 * datapaths accepted these, silently truncating the userdata to 8 bytes.
930 * The same older datapaths rejected userdata shorter than 8 bytes, so we
931 * test for that instead as a proxy for longer userdata support. */
932 ofpbuf_init(&actions, 64);
933 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
934 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
935 dpif_port_get_pid(backer->dpif, ODPP_NONE));
936 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
937 nl_msg_end_nested(&actions, start);
939 /* Compose a dummy ethernet packet. */
940 ofpbuf_init(&packet, ETH_HEADER_LEN);
941 eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
942 eth->eth_type = htons(0x1234);
944 /* Execute the actions. On older datapaths this fails with ERANGE, on
945 * newer datapaths it succeeds. */
946 execute.actions = actions.data;
947 execute.actions_len = actions.size;
948 execute.packet = &packet;
949 execute.md = PKT_METADATA_INITIALIZER(0);
950 execute.needs_help = false;
952 error = dpif_execute(backer->dpif, &execute);
954 ofpbuf_uninit(&packet);
955 ofpbuf_uninit(&actions);
959 /* Variable-length userdata is supported.
961 * Purge received packets to avoid processing the nonsense packet we
962 * sent to userspace, then report success. */
963 dpif_recv_purge(backer->dpif);
967 /* Variable-length userdata is not supported. */
968 VLOG_WARN("%s: datapath does not support variable-length userdata "
969 "feature (needs Linux 3.10+ or kernel module from OVS "
970 "1..11+). The NXAST_SAMPLE action will be ignored.",
971 dpif_name(backer->dpif));
975 /* Something odd happened. We're not sure whether variable-length
976 * userdata is supported. Default to "yes". */
977 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
978 dpif_name(backer->dpif), ovs_strerror(error));
983 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
985 * Returns the number of elements in a struct flow's mpls_lse field
986 * if the datapath supports at least that many entries in an
988 * Otherwise returns the number of MPLS push actions supported by
991 check_max_mpls_depth(struct dpif_backer *backer)
996 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
997 struct odputil_keybuf keybuf;
1001 memset(&flow, 0, sizeof flow);
1002 flow.dl_type = htons(ETH_TYPE_MPLS);
1003 flow_set_mpls_bos(&flow, n, 1);
1005 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1006 odp_flow_key_from_flow(&key, &flow, 0);
1008 error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
1009 key.data, key.size, NULL, 0, NULL, 0, NULL);
1010 if (error && error != EEXIST) {
1011 if (error != EINVAL) {
1012 VLOG_WARN("%s: MPLS stack length feature probe failed (%s)",
1013 dpif_name(backer->dpif), ovs_strerror(error));
1018 error = dpif_flow_del(backer->dpif, key.data, key.size, NULL);
1020 VLOG_WARN("%s: failed to delete MPLS feature probe flow",
1021 dpif_name(backer->dpif));
1025 VLOG_INFO("%s: MPLS label stack length probed as %d",
1026 dpif_name(backer->dpif), n);
1031 construct(struct ofproto *ofproto_)
1033 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1034 struct shash_node *node, *next;
1037 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1042 ofproto->netflow = NULL;
1043 ofproto->sflow = NULL;
1044 ofproto->ipfix = NULL;
1045 ofproto->stp = NULL;
1046 ofproto->dump_seq = 0;
1047 hmap_init(&ofproto->bundles);
1048 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1049 ofproto->mbridge = mbridge_create();
1050 ofproto->has_bonded_bundles = false;
1051 ofproto->lacp_enabled = false;
1052 ovs_mutex_init(&ofproto->stats_mutex);
1053 ovs_mutex_init(&ofproto->vsp_mutex);
1055 guarded_list_init(&ofproto->pins);
1057 ofproto_dpif_unixctl_init();
1059 hmap_init(&ofproto->vlandev_map);
1060 hmap_init(&ofproto->realdev_vid_map);
1062 sset_init(&ofproto->ports);
1063 sset_init(&ofproto->ghost_ports);
1064 sset_init(&ofproto->port_poll_set);
1065 ofproto->port_poll_errno = 0;
1066 ofproto->change_seq = 0;
1068 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1069 struct iface_hint *iface_hint = node->data;
1071 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1072 /* Check if the datapath already has this port. */
1073 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1074 sset_add(&ofproto->ports, node->name);
1077 free(iface_hint->br_name);
1078 free(iface_hint->br_type);
1080 shash_delete(&init_ofp_ports, node);
1084 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1085 hash_string(ofproto->up.name, 0));
1086 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1088 ofproto_init_tables(ofproto_, N_TABLES);
1089 error = add_internal_flows(ofproto);
1090 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1096 add_internal_flow(struct ofproto_dpif *ofproto, int id,
1097 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1099 struct ofputil_flow_mod fm;
1102 match_init_catchall(&fm.match);
1104 match_set_reg(&fm.match, 0, id);
1105 fm.new_cookie = htonll(0);
1106 fm.cookie = htonll(0);
1107 fm.cookie_mask = htonll(0);
1108 fm.modify_cookie = false;
1109 fm.table_id = TBL_INTERNAL;
1110 fm.command = OFPFC_ADD;
1111 fm.idle_timeout = 0;
1112 fm.hard_timeout = 0;
1116 fm.ofpacts = ofpacts->data;
1117 fm.ofpacts_len = ofpacts->size;
1119 error = ofproto_flow_mod(&ofproto->up, &fm);
1121 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1122 id, ofperr_to_string(error));
1126 if (rule_dpif_lookup_in_table(ofproto, &fm.match.flow, NULL, TBL_INTERNAL,
1128 rule_dpif_unref(*rulep);
1137 add_internal_flows(struct ofproto_dpif *ofproto)
1139 struct ofpact_controller *controller;
1140 uint64_t ofpacts_stub[128 / 8];
1141 struct ofpbuf ofpacts;
1145 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1148 controller = ofpact_put_CONTROLLER(&ofpacts);
1149 controller->max_len = UINT16_MAX;
1150 controller->controller_id = 0;
1151 controller->reason = OFPR_NO_MATCH;
1152 ofpact_pad(&ofpacts);
1154 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1159 ofpbuf_clear(&ofpacts);
1160 error = add_internal_flow(ofproto, id++, &ofpacts,
1161 &ofproto->no_packet_in_rule);
1166 error = add_internal_flow(ofproto, id++, &ofpacts,
1167 &ofproto->drop_frags_rule);
1172 destruct(struct ofproto *ofproto_)
1174 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1175 struct rule_dpif *rule, *next_rule;
1176 struct ofproto_packet_in *pin, *next_pin;
1177 struct oftable *table;
1180 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1181 ovs_rwlock_wrlock(&xlate_rwlock);
1182 xlate_remove_ofproto(ofproto);
1183 ovs_rwlock_unlock(&xlate_rwlock);
1185 /* Discard any flow_miss_batches queued up for 'ofproto', avoiding a
1186 * use-after-free error. */
1187 udpif_revalidate(ofproto->backer->udpif);
1189 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1191 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1192 struct cls_cursor cursor;
1194 fat_rwlock_rdlock(&table->cls.rwlock);
1195 cls_cursor_init(&cursor, &table->cls, NULL);
1196 fat_rwlock_unlock(&table->cls.rwlock);
1197 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1198 ofproto_rule_delete(&ofproto->up, &rule->up);
1202 guarded_list_pop_all(&ofproto->pins, &pins);
1203 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1204 list_remove(&pin->list_node);
1205 free(CONST_CAST(void *, pin->up.packet));
1208 guarded_list_destroy(&ofproto->pins);
1210 mbridge_unref(ofproto->mbridge);
1212 netflow_unref(ofproto->netflow);
1213 dpif_sflow_unref(ofproto->sflow);
1214 hmap_destroy(&ofproto->bundles);
1215 mac_learning_unref(ofproto->ml);
1217 hmap_destroy(&ofproto->vlandev_map);
1218 hmap_destroy(&ofproto->realdev_vid_map);
1220 sset_destroy(&ofproto->ports);
1221 sset_destroy(&ofproto->ghost_ports);
1222 sset_destroy(&ofproto->port_poll_set);
1224 ovs_mutex_destroy(&ofproto->stats_mutex);
1225 ovs_mutex_destroy(&ofproto->vsp_mutex);
1227 close_dpif_backer(ofproto->backer);
1231 run(struct ofproto *ofproto_)
1233 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1234 uint64_t new_seq, new_dump_seq;
1236 if (mbridge_need_revalidate(ofproto->mbridge)) {
1237 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1238 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1239 mac_learning_flush(ofproto->ml);
1240 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1243 /* Do not perform any periodic activity required by 'ofproto' while
1244 * waiting for flow restore to complete. */
1245 if (!ofproto_get_flow_restore_wait()) {
1246 struct ofproto_packet_in *pin, *next_pin;
1249 guarded_list_pop_all(&ofproto->pins, &pins);
1250 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1251 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1252 list_remove(&pin->list_node);
1253 free(CONST_CAST(void *, pin->up.packet));
1258 if (ofproto->netflow) {
1259 netflow_run(ofproto->netflow);
1261 if (ofproto->sflow) {
1262 dpif_sflow_run(ofproto->sflow);
1264 if (ofproto->ipfix) {
1265 dpif_ipfix_run(ofproto->ipfix);
1268 new_seq = seq_read(connectivity_seq_get());
1269 if (ofproto->change_seq != new_seq) {
1270 struct ofport_dpif *ofport;
1272 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1276 ofproto->change_seq = new_seq;
1278 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1279 struct ofbundle *bundle;
1281 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1287 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1288 if (mac_learning_run(ofproto->ml)) {
1289 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1291 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1293 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1294 if (ofproto->dump_seq != new_dump_seq) {
1295 struct rule *rule, *next_rule;
1297 /* We know stats are relatively fresh, so now is a good time to do some
1299 ofproto->dump_seq = new_dump_seq;
1301 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1303 ovs_mutex_lock(&ofproto_mutex);
1304 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1305 &ofproto->up.expirable) {
1306 rule_expire(rule_dpif_cast(rule));
1308 ovs_mutex_unlock(&ofproto_mutex);
1310 /* All outstanding data in existing flows has been accounted, so it's a
1311 * good time to do bond rebalancing. */
1312 if (ofproto->has_bonded_bundles) {
1313 struct ofbundle *bundle;
1315 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1317 bond_rebalance(bundle->bond);
1327 wait(struct ofproto *ofproto_)
1329 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1331 if (ofproto_get_flow_restore_wait()) {
1335 if (ofproto->sflow) {
1336 dpif_sflow_wait(ofproto->sflow);
1338 if (ofproto->ipfix) {
1339 dpif_ipfix_wait(ofproto->ipfix);
1341 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1342 struct ofbundle *bundle;
1344 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1345 bundle_wait(bundle);
1348 if (ofproto->netflow) {
1349 netflow_wait(ofproto->netflow);
1351 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1352 mac_learning_wait(ofproto->ml);
1353 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1355 if (ofproto->backer->need_revalidate) {
1356 /* Shouldn't happen, but if it does just go around again. */
1357 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1358 poll_immediate_wake();
1361 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1365 type_get_memory_usage(const char *type, struct simap *usage)
1367 struct dpif_backer *backer;
1369 backer = shash_find_data(&all_dpif_backers, type);
1371 udpif_get_memory_usage(backer->udpif, usage);
1376 flush(struct ofproto *ofproto OVS_UNUSED)
1382 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1383 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1385 *arp_match_ip = true;
1386 *actions = (OFPUTIL_A_OUTPUT |
1387 OFPUTIL_A_SET_VLAN_VID |
1388 OFPUTIL_A_SET_VLAN_PCP |
1389 OFPUTIL_A_STRIP_VLAN |
1390 OFPUTIL_A_SET_DL_SRC |
1391 OFPUTIL_A_SET_DL_DST |
1392 OFPUTIL_A_SET_NW_SRC |
1393 OFPUTIL_A_SET_NW_DST |
1394 OFPUTIL_A_SET_NW_TOS |
1395 OFPUTIL_A_SET_TP_SRC |
1396 OFPUTIL_A_SET_TP_DST |
1401 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1403 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1404 struct dpif_dp_stats s;
1405 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1409 strcpy(ots->name, "classifier");
1411 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1412 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes, &used);
1413 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes,
1415 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes,
1417 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1418 ots->lookup_count = htonll(n_lookup);
1419 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1422 static struct ofport *
1425 struct ofport_dpif *port = xmalloc(sizeof *port);
1430 port_dealloc(struct ofport *port_)
1432 struct ofport_dpif *port = ofport_dpif_cast(port_);
1437 port_construct(struct ofport *port_)
1439 struct ofport_dpif *port = ofport_dpif_cast(port_);
1440 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1441 const struct netdev *netdev = port->up.netdev;
1442 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1443 struct dpif_port dpif_port;
1446 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1447 port->bundle = NULL;
1450 port->may_enable = true;
1451 port->stp_port = NULL;
1452 port->stp_state = STP_DISABLED;
1453 port->is_tunnel = false;
1457 port->realdev_ofp_port = 0;
1458 port->vlandev_vid = 0;
1459 port->carrier_seq = netdev_get_carrier_resets(netdev);
1460 port->is_layer3 = netdev_vport_is_layer3(netdev);
1462 if (netdev_vport_is_patch(netdev)) {
1463 /* By bailing out here, we don't submit the port to the sFlow module
1464 * to be considered for counter polling export. This is correct
1465 * because the patch port represents an interface that sFlow considers
1466 * to be "internal" to the switch as a whole, and therefore not an
1467 * candidate for counter polling. */
1468 port->odp_port = ODPP_NONE;
1469 ofport_update_peer(port);
1473 error = dpif_port_query_by_name(ofproto->backer->dpif,
1474 netdev_vport_get_dpif_port(netdev, namebuf,
1481 port->odp_port = dpif_port.port_no;
1483 if (netdev_get_tunnel_config(netdev)) {
1484 tnl_port_add(port, port->up.netdev, port->odp_port);
1485 port->is_tunnel = true;
1487 /* Sanity-check that a mapping doesn't already exist. This
1488 * shouldn't happen for non-tunnel ports. */
1489 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1490 VLOG_ERR("port %s already has an OpenFlow port number",
1492 dpif_port_destroy(&dpif_port);
1496 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1497 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1498 hash_odp_port(port->odp_port));
1499 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1501 dpif_port_destroy(&dpif_port);
1503 if (ofproto->sflow) {
1504 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1511 port_destruct(struct ofport *port_)
1513 struct ofport_dpif *port = ofport_dpif_cast(port_);
1514 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1515 const char *devname = netdev_get_name(port->up.netdev);
1516 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1517 const char *dp_port_name;
1519 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1520 ovs_rwlock_wrlock(&xlate_rwlock);
1521 xlate_ofport_remove(port);
1522 ovs_rwlock_unlock(&xlate_rwlock);
1524 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1526 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1527 /* The underlying device is still there, so delete it. This
1528 * happens when the ofproto is being destroyed, since the caller
1529 * assumes that removal of attached ports will happen as part of
1531 if (!port->is_tunnel) {
1532 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1537 port->peer->peer = NULL;
1541 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1542 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1543 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1544 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1548 sset_find_and_delete(&ofproto->ports, devname);
1549 sset_find_and_delete(&ofproto->ghost_ports, devname);
1550 bundle_remove(port_);
1551 set_cfm(port_, NULL);
1552 set_bfd(port_, NULL);
1553 if (ofproto->sflow) {
1554 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1561 port_modified(struct ofport *port_)
1563 struct ofport_dpif *port = ofport_dpif_cast(port_);
1565 if (port->bundle && port->bundle->bond) {
1566 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1570 cfm_set_netdev(port->cfm, port->up.netdev);
1574 bfd_set_netdev(port->bfd, port->up.netdev);
1577 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1578 port->up.pp.hw_addr);
1580 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1582 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1586 ofport_update_peer(port);
1590 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1592 struct ofport_dpif *port = ofport_dpif_cast(port_);
1593 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1594 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1596 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1597 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1598 OFPUTIL_PC_NO_PACKET_IN)) {
1599 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1601 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1602 bundle_update(port->bundle);
1608 set_sflow(struct ofproto *ofproto_,
1609 const struct ofproto_sflow_options *sflow_options)
1611 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1612 struct dpif_sflow *ds = ofproto->sflow;
1614 if (sflow_options) {
1616 struct ofport_dpif *ofport;
1618 ds = ofproto->sflow = dpif_sflow_create();
1619 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1620 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1622 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1624 dpif_sflow_set_options(ds, sflow_options);
1627 dpif_sflow_unref(ds);
1628 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1629 ofproto->sflow = NULL;
1637 struct ofproto *ofproto_,
1638 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1639 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1640 size_t n_flow_exporters_options)
1642 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1643 struct dpif_ipfix *di = ofproto->ipfix;
1644 bool has_options = bridge_exporter_options || flow_exporters_options;
1646 if (has_options && !di) {
1647 di = ofproto->ipfix = dpif_ipfix_create();
1651 /* Call set_options in any case to cleanly flush the flow
1652 * caches in the last exporters that are to be destroyed. */
1653 dpif_ipfix_set_options(
1654 di, bridge_exporter_options, flow_exporters_options,
1655 n_flow_exporters_options);
1658 dpif_ipfix_unref(di);
1659 ofproto->ipfix = NULL;
1667 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1669 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1674 struct ofproto_dpif *ofproto;
1676 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1677 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1678 ofport->cfm = cfm_create(ofport->up.netdev);
1681 if (cfm_configure(ofport->cfm, s)) {
1688 cfm_unref(ofport->cfm);
1691 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1692 ofport->up.pp.hw_addr);
1697 get_cfm_status(const struct ofport *ofport_,
1698 struct ofproto_cfm_status *status)
1700 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1703 status->faults = cfm_get_fault(ofport->cfm);
1704 status->flap_count = cfm_get_flap_count(ofport->cfm);
1705 status->remote_opstate = cfm_get_opup(ofport->cfm);
1706 status->health = cfm_get_health(ofport->cfm);
1707 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1715 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1717 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1718 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1722 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1723 cfg, ofport->up.netdev);
1724 if (ofport->bfd != old) {
1725 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1727 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1728 ofport->up.pp.hw_addr);
1733 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1735 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1738 bfd_get_status(ofport->bfd, smap);
1745 /* Spanning Tree. */
1748 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1750 struct ofproto_dpif *ofproto = ofproto_;
1751 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1752 struct ofport_dpif *ofport;
1754 ofport = stp_port_get_aux(sp);
1756 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1757 ofproto->up.name, port_num);
1759 struct eth_header *eth = pkt->l2;
1761 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1762 if (eth_addr_is_zero(eth->eth_src)) {
1763 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1764 "with unknown MAC", ofproto->up.name, port_num);
1766 ofproto_dpif_send_packet(ofport, pkt);
1772 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1774 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1776 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1778 /* Only revalidate flows if the configuration changed. */
1779 if (!s != !ofproto->stp) {
1780 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1784 if (!ofproto->stp) {
1785 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1786 send_bpdu_cb, ofproto);
1787 ofproto->stp_last_tick = time_msec();
1790 stp_set_bridge_id(ofproto->stp, s->system_id);
1791 stp_set_bridge_priority(ofproto->stp, s->priority);
1792 stp_set_hello_time(ofproto->stp, s->hello_time);
1793 stp_set_max_age(ofproto->stp, s->max_age);
1794 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1796 struct ofport *ofport;
1798 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1799 set_stp_port(ofport, NULL);
1802 stp_unref(ofproto->stp);
1803 ofproto->stp = NULL;
1810 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1812 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1816 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1817 s->designated_root = stp_get_designated_root(ofproto->stp);
1818 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1827 update_stp_port_state(struct ofport_dpif *ofport)
1829 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1830 enum stp_state state;
1832 /* Figure out new state. */
1833 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1837 if (ofport->stp_state != state) {
1838 enum ofputil_port_state of_state;
1841 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1842 netdev_get_name(ofport->up.netdev),
1843 stp_state_name(ofport->stp_state),
1844 stp_state_name(state));
1845 if (stp_learn_in_state(ofport->stp_state)
1846 != stp_learn_in_state(state)) {
1847 /* xxx Learning action flows should also be flushed. */
1848 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1849 mac_learning_flush(ofproto->ml);
1850 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1852 fwd_change = stp_forward_in_state(ofport->stp_state)
1853 != stp_forward_in_state(state);
1855 ofproto->backer->need_revalidate = REV_STP;
1856 ofport->stp_state = state;
1857 ofport->stp_state_entered = time_msec();
1859 if (fwd_change && ofport->bundle) {
1860 bundle_update(ofport->bundle);
1863 /* Update the STP state bits in the OpenFlow port description. */
1864 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1865 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1866 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1867 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1868 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1870 ofproto_port_set_state(&ofport->up, of_state);
1874 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1875 * caller is responsible for assigning STP port numbers and ensuring
1876 * there are no duplicates. */
1878 set_stp_port(struct ofport *ofport_,
1879 const struct ofproto_port_stp_settings *s)
1881 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1882 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1883 struct stp_port *sp = ofport->stp_port;
1885 if (!s || !s->enable) {
1887 ofport->stp_port = NULL;
1888 stp_port_disable(sp);
1889 update_stp_port_state(ofport);
1892 } else if (sp && stp_port_no(sp) != s->port_num
1893 && ofport == stp_port_get_aux(sp)) {
1894 /* The port-id changed, so disable the old one if it's not
1895 * already in use by another port. */
1896 stp_port_disable(sp);
1899 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1900 stp_port_enable(sp);
1902 stp_port_set_aux(sp, ofport);
1903 stp_port_set_priority(sp, s->priority);
1904 stp_port_set_path_cost(sp, s->path_cost);
1906 update_stp_port_state(ofport);
1912 get_stp_port_status(struct ofport *ofport_,
1913 struct ofproto_port_stp_status *s)
1915 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1916 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1917 struct stp_port *sp = ofport->stp_port;
1919 if (!ofproto->stp || !sp) {
1925 s->port_id = stp_port_get_id(sp);
1926 s->state = stp_port_get_state(sp);
1927 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1928 s->role = stp_port_get_role(sp);
1934 get_stp_port_stats(struct ofport *ofport_,
1935 struct ofproto_port_stp_stats *s)
1937 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1938 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1939 struct stp_port *sp = ofport->stp_port;
1941 if (!ofproto->stp || !sp) {
1947 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1953 stp_run(struct ofproto_dpif *ofproto)
1956 long long int now = time_msec();
1957 long long int elapsed = now - ofproto->stp_last_tick;
1958 struct stp_port *sp;
1961 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1962 ofproto->stp_last_tick = now;
1964 while (stp_get_changed_port(ofproto->stp, &sp)) {
1965 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1968 update_stp_port_state(ofport);
1972 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1973 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1974 mac_learning_flush(ofproto->ml);
1975 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1981 stp_wait(struct ofproto_dpif *ofproto)
1984 poll_timer_wait(1000);
1989 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
1992 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1993 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1995 if (ofport->n_qdscp != n_qdscp
1996 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
1997 n_qdscp * sizeof *qdscp))) {
1998 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1999 free(ofport->qdscp);
2000 ofport->qdscp = n_qdscp
2001 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2003 ofport->n_qdscp = n_qdscp;
2011 /* Expires all MAC learning entries associated with 'bundle' and forces its
2012 * ofproto to revalidate every flow.
2014 * Normally MAC learning entries are removed only from the ofproto associated
2015 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2016 * are removed from every ofproto. When patch ports and SLB bonds are in use
2017 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2018 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2019 * with the host from which it migrated. */
2021 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2023 struct ofproto_dpif *ofproto = bundle->ofproto;
2024 struct mac_learning *ml = ofproto->ml;
2025 struct mac_entry *mac, *next_mac;
2027 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2028 ovs_rwlock_wrlock(&ml->rwlock);
2029 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2030 if (mac->port.p == bundle) {
2032 struct ofproto_dpif *o;
2034 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2036 struct mac_entry *e;
2038 ovs_rwlock_wrlock(&o->ml->rwlock);
2039 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2041 mac_learning_expire(o->ml, e);
2043 ovs_rwlock_unlock(&o->ml->rwlock);
2048 mac_learning_expire(ml, mac);
2051 ovs_rwlock_unlock(&ml->rwlock);
2054 static struct ofbundle *
2055 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2057 struct ofbundle *bundle;
2059 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2060 &ofproto->bundles) {
2061 if (bundle->aux == aux) {
2069 bundle_update(struct ofbundle *bundle)
2071 struct ofport_dpif *port;
2073 bundle->floodable = true;
2074 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2075 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2077 || !stp_forward_in_state(port->stp_state)) {
2078 bundle->floodable = false;
2085 bundle_del_port(struct ofport_dpif *port)
2087 struct ofbundle *bundle = port->bundle;
2089 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2091 list_remove(&port->bundle_node);
2092 port->bundle = NULL;
2095 lacp_slave_unregister(bundle->lacp, port);
2098 bond_slave_unregister(bundle->bond, port);
2101 bundle_update(bundle);
2105 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2106 struct lacp_slave_settings *lacp)
2108 struct ofport_dpif *port;
2110 port = get_ofp_port(bundle->ofproto, ofp_port);
2115 if (port->bundle != bundle) {
2116 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2118 bundle_remove(&port->up);
2121 port->bundle = bundle;
2122 list_push_back(&bundle->ports, &port->bundle_node);
2123 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2125 || !stp_forward_in_state(port->stp_state)) {
2126 bundle->floodable = false;
2130 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2131 lacp_slave_register(bundle->lacp, port, lacp);
2138 bundle_destroy(struct ofbundle *bundle)
2140 struct ofproto_dpif *ofproto;
2141 struct ofport_dpif *port, *next_port;
2147 ofproto = bundle->ofproto;
2148 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2150 ovs_rwlock_wrlock(&xlate_rwlock);
2151 xlate_bundle_remove(bundle);
2152 ovs_rwlock_unlock(&xlate_rwlock);
2154 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2155 bundle_del_port(port);
2158 bundle_flush_macs(bundle, true);
2159 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2161 free(bundle->trunks);
2162 lacp_unref(bundle->lacp);
2163 bond_unref(bundle->bond);
2168 bundle_set(struct ofproto *ofproto_, void *aux,
2169 const struct ofproto_bundle_settings *s)
2171 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2172 bool need_flush = false;
2173 struct ofport_dpif *port;
2174 struct ofbundle *bundle;
2175 unsigned long *trunks;
2181 bundle_destroy(bundle_lookup(ofproto, aux));
2185 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2186 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2188 bundle = bundle_lookup(ofproto, aux);
2190 bundle = xmalloc(sizeof *bundle);
2192 bundle->ofproto = ofproto;
2193 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2194 hash_pointer(aux, 0));
2196 bundle->name = NULL;
2198 list_init(&bundle->ports);
2199 bundle->vlan_mode = PORT_VLAN_TRUNK;
2201 bundle->trunks = NULL;
2202 bundle->use_priority_tags = s->use_priority_tags;
2203 bundle->lacp = NULL;
2204 bundle->bond = NULL;
2206 bundle->floodable = true;
2207 mbridge_register_bundle(ofproto->mbridge, bundle);
2210 if (!bundle->name || strcmp(s->name, bundle->name)) {
2212 bundle->name = xstrdup(s->name);
2217 ofproto->lacp_enabled = true;
2218 if (!bundle->lacp) {
2219 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2220 bundle->lacp = lacp_create();
2222 lacp_configure(bundle->lacp, s->lacp);
2224 lacp_unref(bundle->lacp);
2225 bundle->lacp = NULL;
2228 /* Update set of ports. */
2230 for (i = 0; i < s->n_slaves; i++) {
2231 if (!bundle_add_port(bundle, s->slaves[i],
2232 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2236 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2237 struct ofport_dpif *next_port;
2239 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2240 for (i = 0; i < s->n_slaves; i++) {
2241 if (s->slaves[i] == port->up.ofp_port) {
2246 bundle_del_port(port);
2250 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2252 if (list_is_empty(&bundle->ports)) {
2253 bundle_destroy(bundle);
2257 /* Set VLAN tagging mode */
2258 if (s->vlan_mode != bundle->vlan_mode
2259 || s->use_priority_tags != bundle->use_priority_tags) {
2260 bundle->vlan_mode = s->vlan_mode;
2261 bundle->use_priority_tags = s->use_priority_tags;
2266 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2267 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2269 if (vlan != bundle->vlan) {
2270 bundle->vlan = vlan;
2274 /* Get trunked VLANs. */
2275 switch (s->vlan_mode) {
2276 case PORT_VLAN_ACCESS:
2280 case PORT_VLAN_TRUNK:
2281 trunks = CONST_CAST(unsigned long *, s->trunks);
2284 case PORT_VLAN_NATIVE_UNTAGGED:
2285 case PORT_VLAN_NATIVE_TAGGED:
2286 if (vlan != 0 && (!s->trunks
2287 || !bitmap_is_set(s->trunks, vlan)
2288 || bitmap_is_set(s->trunks, 0))) {
2289 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2291 trunks = bitmap_clone(s->trunks, 4096);
2293 trunks = bitmap_allocate1(4096);
2295 bitmap_set1(trunks, vlan);
2296 bitmap_set0(trunks, 0);
2298 trunks = CONST_CAST(unsigned long *, s->trunks);
2305 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2306 free(bundle->trunks);
2307 if (trunks == s->trunks) {
2308 bundle->trunks = vlan_bitmap_clone(trunks);
2310 bundle->trunks = trunks;
2315 if (trunks != s->trunks) {
2320 if (!list_is_short(&bundle->ports)) {
2321 bundle->ofproto->has_bonded_bundles = true;
2323 if (bond_reconfigure(bundle->bond, s->bond)) {
2324 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2327 bundle->bond = bond_create(s->bond);
2328 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2331 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2332 bond_slave_register(bundle->bond, port, port->up.netdev);
2335 bond_unref(bundle->bond);
2336 bundle->bond = NULL;
2339 /* If we changed something that would affect MAC learning, un-learn
2340 * everything on this port and force flow revalidation. */
2342 bundle_flush_macs(bundle, false);
2349 bundle_remove(struct ofport *port_)
2351 struct ofport_dpif *port = ofport_dpif_cast(port_);
2352 struct ofbundle *bundle = port->bundle;
2355 bundle_del_port(port);
2356 if (list_is_empty(&bundle->ports)) {
2357 bundle_destroy(bundle);
2358 } else if (list_is_short(&bundle->ports)) {
2359 bond_unref(bundle->bond);
2360 bundle->bond = NULL;
2366 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2368 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2369 struct ofport_dpif *port = port_;
2370 uint8_t ea[ETH_ADDR_LEN];
2373 error = netdev_get_etheraddr(port->up.netdev, ea);
2375 struct ofpbuf packet;
2378 ofpbuf_init(&packet, 0);
2379 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2381 memcpy(packet_pdu, pdu, pdu_size);
2383 ofproto_dpif_send_packet(port, &packet);
2384 ofpbuf_uninit(&packet);
2386 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2387 "%s (%s)", port->bundle->name,
2388 netdev_get_name(port->up.netdev), ovs_strerror(error));
2393 bundle_send_learning_packets(struct ofbundle *bundle)
2395 struct ofproto_dpif *ofproto = bundle->ofproto;
2396 struct ofpbuf *learning_packet;
2397 int error, n_packets, n_errors;
2398 struct mac_entry *e;
2399 struct list packets;
2401 list_init(&packets);
2402 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2403 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2404 if (e->port.p != bundle) {
2407 learning_packet = bond_compose_learning_packet(bundle->bond,
2410 learning_packet->private_p = port_void;
2411 list_push_back(&packets, &learning_packet->list_node);
2414 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2416 error = n_packets = n_errors = 0;
2417 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2420 ret = ofproto_dpif_send_packet(learning_packet->private_p, learning_packet);
2427 ofpbuf_list_delete(&packets);
2430 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2431 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2432 "packets, last error was: %s",
2433 bundle->name, n_errors, n_packets, ovs_strerror(error));
2435 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2436 bundle->name, n_packets);
2441 bundle_run(struct ofbundle *bundle)
2444 lacp_run(bundle->lacp, send_pdu_cb);
2447 struct ofport_dpif *port;
2449 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2450 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2453 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2454 bundle->ofproto->backer->need_revalidate = REV_BOND;
2457 if (bond_should_send_learning_packets(bundle->bond)) {
2458 bundle_send_learning_packets(bundle);
2464 bundle_wait(struct ofbundle *bundle)
2467 lacp_wait(bundle->lacp);
2470 bond_wait(bundle->bond);
2477 mirror_set__(struct ofproto *ofproto_, void *aux,
2478 const struct ofproto_mirror_settings *s)
2480 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2481 struct ofbundle **srcs, **dsts;
2486 mirror_destroy(ofproto->mbridge, aux);
2490 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2491 dsts = xmalloc(s->n_dsts * sizeof *dsts);
2493 for (i = 0; i < s->n_srcs; i++) {
2494 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2497 for (i = 0; i < s->n_dsts; i++) {
2498 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2501 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2502 s->n_dsts, s->src_vlans,
2503 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2510 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2511 uint64_t *packets, uint64_t *bytes)
2513 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2518 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2520 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2521 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2522 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2523 mac_learning_flush(ofproto->ml);
2525 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2530 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2532 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2533 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2534 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2538 forward_bpdu_changed(struct ofproto *ofproto_)
2540 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2541 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2545 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2548 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2549 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2550 mac_learning_set_idle_time(ofproto->ml, idle_time);
2551 mac_learning_set_max_entries(ofproto->ml, max_entries);
2552 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2557 static struct ofport_dpif *
2558 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2560 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2561 return ofport ? ofport_dpif_cast(ofport) : NULL;
2565 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2566 struct ofproto_port *ofproto_port,
2567 struct dpif_port *dpif_port)
2569 ofproto_port->name = dpif_port->name;
2570 ofproto_port->type = dpif_port->type;
2571 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2575 ofport_update_peer(struct ofport_dpif *ofport)
2577 const struct ofproto_dpif *ofproto;
2578 struct dpif_backer *backer;
2581 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2585 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2586 backer->need_revalidate = REV_RECONFIGURE;
2589 ofport->peer->peer = NULL;
2590 ofport->peer = NULL;
2593 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2598 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2599 struct ofport *peer_ofport;
2600 struct ofport_dpif *peer;
2603 if (ofproto->backer != backer) {
2607 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2612 peer = ofport_dpif_cast(peer_ofport);
2613 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2614 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2616 ofport->peer = peer;
2617 ofport->peer->peer = ofport;
2627 port_run(struct ofport_dpif *ofport)
2629 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2630 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2631 bool enable = netdev_get_carrier(ofport->up.netdev);
2632 bool cfm_enable = false;
2633 bool bfd_enable = false;
2635 ofport->carrier_seq = carrier_seq;
2638 int cfm_opup = cfm_get_opup(ofport->cfm);
2640 cfm_enable = !cfm_get_fault(ofport->cfm);
2642 if (cfm_opup >= 0) {
2643 cfm_enable = cfm_enable && cfm_opup;
2648 bfd_enable = bfd_forwarding(ofport->bfd);
2651 if (ofport->bfd || ofport->cfm) {
2652 enable = enable && (cfm_enable || bfd_enable);
2655 if (ofport->bundle) {
2656 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2657 if (carrier_changed) {
2658 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2662 if (ofport->may_enable != enable) {
2663 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2664 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2667 ofport->may_enable = enable;
2671 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2672 struct ofproto_port *ofproto_port)
2674 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2675 struct dpif_port dpif_port;
2678 if (sset_contains(&ofproto->ghost_ports, devname)) {
2679 const char *type = netdev_get_type_from_name(devname);
2681 /* We may be called before ofproto->up.port_by_name is populated with
2682 * the appropriate ofport. For this reason, we must get the name and
2683 * type from the netdev layer directly. */
2685 const struct ofport *ofport;
2687 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2688 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2689 ofproto_port->name = xstrdup(devname);
2690 ofproto_port->type = xstrdup(type);
2696 if (!sset_contains(&ofproto->ports, devname)) {
2699 error = dpif_port_query_by_name(ofproto->backer->dpif,
2700 devname, &dpif_port);
2702 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2708 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2710 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2711 const char *devname = netdev_get_name(netdev);
2712 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2713 const char *dp_port_name;
2715 if (netdev_vport_is_patch(netdev)) {
2716 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2720 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2721 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2722 odp_port_t port_no = ODPP_NONE;
2725 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2729 if (netdev_get_tunnel_config(netdev)) {
2730 simap_put(&ofproto->backer->tnl_backers,
2731 dp_port_name, odp_to_u32(port_no));
2735 if (netdev_get_tunnel_config(netdev)) {
2736 sset_add(&ofproto->ghost_ports, devname);
2738 sset_add(&ofproto->ports, devname);
2744 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2746 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2747 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2754 sset_find_and_delete(&ofproto->ghost_ports,
2755 netdev_get_name(ofport->up.netdev));
2756 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2757 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2758 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2760 /* The caller is going to close ofport->up.netdev. If this is a
2761 * bonded port, then the bond is using that netdev, so remove it
2762 * from the bond. The client will need to reconfigure everything
2763 * after deleting ports, so then the slave will get re-added. */
2764 bundle_remove(&ofport->up);
2771 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2773 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2776 error = netdev_get_stats(ofport->up.netdev, stats);
2778 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2779 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2781 ovs_mutex_lock(&ofproto->stats_mutex);
2782 /* ofproto->stats.tx_packets represents packets that we created
2783 * internally and sent to some port (e.g. packets sent with
2784 * ofproto_dpif_send_packet()). Account for them as if they had
2785 * come from OFPP_LOCAL and got forwarded. */
2787 if (stats->rx_packets != UINT64_MAX) {
2788 stats->rx_packets += ofproto->stats.tx_packets;
2791 if (stats->rx_bytes != UINT64_MAX) {
2792 stats->rx_bytes += ofproto->stats.tx_bytes;
2795 /* ofproto->stats.rx_packets represents packets that were received on
2796 * some port and we processed internally and dropped (e.g. STP).
2797 * Account for them as if they had been forwarded to OFPP_LOCAL. */
2799 if (stats->tx_packets != UINT64_MAX) {
2800 stats->tx_packets += ofproto->stats.rx_packets;
2803 if (stats->tx_bytes != UINT64_MAX) {
2804 stats->tx_bytes += ofproto->stats.rx_bytes;
2806 ovs_mutex_unlock(&ofproto->stats_mutex);
2812 struct port_dump_state {
2817 struct ofproto_port port;
2822 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
2824 *statep = xzalloc(sizeof(struct port_dump_state));
2829 port_dump_next(const struct ofproto *ofproto_, void *state_,
2830 struct ofproto_port *port)
2832 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2833 struct port_dump_state *state = state_;
2834 const struct sset *sset;
2835 struct sset_node *node;
2837 if (state->has_port) {
2838 ofproto_port_destroy(&state->port);
2839 state->has_port = false;
2841 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
2842 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
2845 error = port_query_by_name(ofproto_, node->name, &state->port);
2847 *port = state->port;
2848 state->has_port = true;
2850 } else if (error != ENODEV) {
2855 if (!state->ghost) {
2856 state->ghost = true;
2859 return port_dump_next(ofproto_, state_, port);
2866 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2868 struct port_dump_state *state = state_;
2870 if (state->has_port) {
2871 ofproto_port_destroy(&state->port);
2878 port_poll(const struct ofproto *ofproto_, char **devnamep)
2880 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2882 if (ofproto->port_poll_errno) {
2883 int error = ofproto->port_poll_errno;
2884 ofproto->port_poll_errno = 0;
2888 if (sset_is_empty(&ofproto->port_poll_set)) {
2892 *devnamep = sset_pop(&ofproto->port_poll_set);
2897 port_poll_wait(const struct ofproto *ofproto_)
2899 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2900 dpif_port_poll_wait(ofproto->backer->dpif);
2904 port_is_lacp_current(const struct ofport *ofport_)
2906 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2907 return (ofport->bundle && ofport->bundle->lacp
2908 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2912 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2913 * then delete it entirely. */
2915 rule_expire(struct rule_dpif *rule)
2916 OVS_REQUIRES(ofproto_mutex)
2918 uint16_t hard_timeout, idle_timeout;
2919 long long int now = time_msec();
2922 ovs_assert(!rule->up.pending);
2924 hard_timeout = rule->up.hard_timeout;
2925 idle_timeout = rule->up.idle_timeout;
2927 /* Has 'rule' expired? */
2929 long long int modified;
2931 ovs_mutex_lock(&rule->up.mutex);
2932 modified = rule->up.modified;
2933 ovs_mutex_unlock(&rule->up.mutex);
2935 if (now > modified + hard_timeout * 1000) {
2936 reason = OFPRR_HARD_TIMEOUT;
2940 if (reason < 0 && idle_timeout) {
2943 ovs_mutex_lock(&rule->stats_mutex);
2945 ovs_mutex_unlock(&rule->stats_mutex);
2947 if (now > used + idle_timeout * 1000) {
2948 reason = OFPRR_IDLE_TIMEOUT;
2953 COVERAGE_INC(ofproto_dpif_expired);
2954 ofproto_rule_expire(&rule->up, reason);
2958 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
2959 * 'flow' must reflect the data in 'packet'. */
2961 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
2962 const struct flow *flow,
2963 struct rule_dpif *rule,
2964 const struct ofpact *ofpacts, size_t ofpacts_len,
2965 struct ofpbuf *packet)
2967 struct dpif_flow_stats stats;
2968 struct xlate_out xout;
2969 struct xlate_in xin;
2971 struct dpif_execute execute;
2974 ovs_assert((rule != NULL) != (ofpacts != NULL));
2976 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
2978 rule_dpif_credit_stats(rule, &stats);
2981 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
2982 xin.ofpacts = ofpacts;
2983 xin.ofpacts_len = ofpacts_len;
2984 xin.resubmit_stats = &stats;
2985 xlate_actions(&xin, &xout);
2987 in_port = flow->in_port.ofp_port;
2988 if (in_port == OFPP_NONE) {
2989 in_port = OFPP_LOCAL;
2991 execute.actions = xout.odp_actions.data;
2992 execute.actions_len = xout.odp_actions.size;
2993 execute.packet = packet;
2994 execute.md.tunnel = flow->tunnel;
2995 execute.md.skb_priority = flow->skb_priority;
2996 execute.md.pkt_mark = flow->pkt_mark;
2997 execute.md.in_port = ofp_port_to_odp_port(ofproto, in_port);
2998 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3000 error = dpif_execute(ofproto->backer->dpif, &execute);
3002 xlate_out_uninit(&xout);
3008 rule_dpif_credit_stats(struct rule_dpif *rule,
3009 const struct dpif_flow_stats *stats)
3011 ovs_mutex_lock(&rule->stats_mutex);
3012 rule->packet_count += stats->n_packets;
3013 rule->byte_count += stats->n_bytes;
3014 rule->used = MAX(rule->used, stats->used);
3015 ovs_mutex_unlock(&rule->stats_mutex);
3019 rule_dpif_is_fail_open(const struct rule_dpif *rule)
3021 return is_fail_open_rule(&rule->up);
3025 rule_dpif_is_table_miss(const struct rule_dpif *rule)
3027 return rule_is_table_miss(&rule->up);
3031 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3032 OVS_REQUIRES(rule->up.mutex)
3034 return rule->up.flow_cookie;
3038 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3039 uint16_t hard_timeout)
3041 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3044 /* Returns 'rule''s actions. The caller owns a reference on the returned
3045 * actions and must eventually release it (with rule_actions_unref()) to avoid
3047 struct rule_actions *
3048 rule_dpif_get_actions(const struct rule_dpif *rule)
3050 return rule_get_actions(&rule->up);
3053 /* Lookup 'flow' in 'ofproto''s classifier. If 'wc' is non-null, sets
3054 * the fields that were relevant as part of the lookup. */
3056 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3057 struct flow_wildcards *wc, struct rule_dpif **rule)
3059 struct ofport_dpif *port;
3061 if (rule_dpif_lookup_in_table(ofproto, flow, wc, 0, rule)) {
3064 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
3066 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3067 flow->in_port.ofp_port);
3070 choose_miss_rule(port ? port->up.pp.config : 0, ofproto->miss_rule,
3071 ofproto->no_packet_in_rule, rule);
3075 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto,
3076 const struct flow *flow, struct flow_wildcards *wc,
3077 uint8_t table_id, struct rule_dpif **rule)
3079 const struct cls_rule *cls_rule;
3080 struct classifier *cls;
3084 if (table_id >= N_TABLES) {
3089 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3090 if (is_ip_any(flow)) {
3091 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3095 cls = &ofproto->up.tables[table_id].cls;
3096 fat_rwlock_rdlock(&cls->rwlock);
3097 frag = (flow->nw_frag & FLOW_NW_FRAG_ANY) != 0;
3098 if (frag && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3099 /* We must pretend that transport ports are unavailable. */
3100 struct flow ofpc_normal_flow = *flow;
3101 ofpc_normal_flow.tp_src = htons(0);
3102 ofpc_normal_flow.tp_dst = htons(0);
3103 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3104 } else if (frag && ofproto->up.frag_handling == OFPC_FRAG_DROP) {
3105 cls_rule = &ofproto->drop_frags_rule->up.cr;
3106 /* Frag mask in wc already set above. */
3108 cls_rule = classifier_lookup(cls, flow, wc);
3111 *rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3112 rule_dpif_ref(*rule);
3113 fat_rwlock_unlock(&cls->rwlock);
3115 return *rule != NULL;
3118 /* Given a port configuration (specified as zero if there's no port), chooses
3119 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3120 * flow table miss. */
3122 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3123 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
3125 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
3126 rule_dpif_ref(*rule);
3130 rule_dpif_ref(struct rule_dpif *rule)
3133 ofproto_rule_ref(&rule->up);
3138 rule_dpif_unref(struct rule_dpif *rule)
3141 ofproto_rule_unref(&rule->up);
3146 complete_operation(struct rule_dpif *rule)
3147 OVS_REQUIRES(ofproto_mutex)
3149 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3151 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3152 ofoperation_complete(rule->up.pending, 0);
3155 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3157 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3160 static struct rule *
3163 struct rule_dpif *rule = xmalloc(sizeof *rule);
3168 rule_dealloc(struct rule *rule_)
3170 struct rule_dpif *rule = rule_dpif_cast(rule_);
3175 rule_construct(struct rule *rule_)
3176 OVS_NO_THREAD_SAFETY_ANALYSIS
3178 struct rule_dpif *rule = rule_dpif_cast(rule_);
3179 ovs_mutex_init(&rule->stats_mutex);
3180 rule->packet_count = 0;
3181 rule->byte_count = 0;
3182 rule->used = rule->up.modified;
3187 rule_insert(struct rule *rule_)
3188 OVS_REQUIRES(ofproto_mutex)
3190 struct rule_dpif *rule = rule_dpif_cast(rule_);
3191 complete_operation(rule);
3195 rule_delete(struct rule *rule_)
3196 OVS_REQUIRES(ofproto_mutex)
3198 struct rule_dpif *rule = rule_dpif_cast(rule_);
3199 complete_operation(rule);
3203 rule_destruct(struct rule *rule_)
3205 struct rule_dpif *rule = rule_dpif_cast(rule_);
3206 ovs_mutex_destroy(&rule->stats_mutex);
3210 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3211 long long int *used)
3213 struct rule_dpif *rule = rule_dpif_cast(rule_);
3215 ovs_mutex_lock(&rule->stats_mutex);
3216 *packets = rule->packet_count;
3217 *bytes = rule->byte_count;
3219 ovs_mutex_unlock(&rule->stats_mutex);
3223 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3224 struct ofpbuf *packet)
3226 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3228 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3232 rule_execute(struct rule *rule, const struct flow *flow,
3233 struct ofpbuf *packet)
3235 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3236 ofpbuf_delete(packet);
3241 rule_modify_actions(struct rule *rule_, bool reset_counters)
3242 OVS_REQUIRES(ofproto_mutex)
3244 struct rule_dpif *rule = rule_dpif_cast(rule_);
3246 if (reset_counters) {
3247 ovs_mutex_lock(&rule->stats_mutex);
3248 rule->packet_count = 0;
3249 rule->byte_count = 0;
3250 ovs_mutex_unlock(&rule->stats_mutex);
3253 complete_operation(rule);
3256 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3258 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3261 static struct ofgroup *
3264 struct group_dpif *group = xzalloc(sizeof *group);
3269 group_dealloc(struct ofgroup *group_)
3271 struct group_dpif *group = group_dpif_cast(group_);
3276 group_construct_stats(struct group_dpif *group)
3277 OVS_REQUIRES(group->stats_mutex)
3279 group->packet_count = 0;
3280 group->byte_count = 0;
3281 if (!group->bucket_stats) {
3282 group->bucket_stats = xcalloc(group->up.n_buckets,
3283 sizeof *group->bucket_stats);
3285 memset(group->bucket_stats, 0, group->up.n_buckets *
3286 sizeof *group->bucket_stats);
3291 group_construct(struct ofgroup *group_)
3293 struct group_dpif *group = group_dpif_cast(group_);
3294 ovs_mutex_init(&group->stats_mutex);
3295 ovs_mutex_lock(&group->stats_mutex);
3296 group_construct_stats(group);
3297 ovs_mutex_unlock(&group->stats_mutex);
3302 group_destruct__(struct group_dpif *group)
3303 OVS_REQUIRES(group->stats_mutex)
3305 free(group->bucket_stats);
3306 group->bucket_stats = NULL;
3310 group_destruct(struct ofgroup *group_)
3312 struct group_dpif *group = group_dpif_cast(group_);
3313 ovs_mutex_lock(&group->stats_mutex);
3314 group_destruct__(group);
3315 ovs_mutex_unlock(&group->stats_mutex);
3316 ovs_mutex_destroy(&group->stats_mutex);
3320 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3322 struct group_dpif *group = group_dpif_cast(group_);
3323 struct group_dpif *victim = group_dpif_cast(victim_);
3325 ovs_mutex_lock(&group->stats_mutex);
3326 if (victim->up.n_buckets < group->up.n_buckets) {
3327 group_destruct__(group);
3329 group_construct_stats(group);
3330 ovs_mutex_unlock(&group->stats_mutex);
3336 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3338 struct group_dpif *group = group_dpif_cast(group_);
3340 ovs_mutex_lock(&group->stats_mutex);
3341 ogs->packet_count = group->packet_count;
3342 ogs->byte_count = group->byte_count;
3343 memcpy(ogs->bucket_stats, group->bucket_stats,
3344 group->up.n_buckets * sizeof *group->bucket_stats);
3345 ovs_mutex_unlock(&group->stats_mutex);
3351 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3352 struct group_dpif **group)
3353 OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3355 struct ofgroup *ofgroup;
3359 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3360 *group = found ? group_dpif_cast(ofgroup) : NULL;
3366 group_dpif_release(struct group_dpif *group)
3367 OVS_RELEASES(group->up.rwlock)
3369 ofproto_group_release(&group->up);
3373 group_dpif_get_buckets(const struct group_dpif *group,
3374 const struct list **buckets)
3376 *buckets = &group->up.buckets;
3379 enum ofp11_group_type
3380 group_dpif_get_type(const struct group_dpif *group)
3382 return group->up.type;
3385 /* Sends 'packet' out 'ofport'.
3386 * May modify 'packet'.
3387 * Returns 0 if successful, otherwise a positive errno value. */
3389 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3391 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3394 error = xlate_send_packet(ofport, packet);
3396 ovs_mutex_lock(&ofproto->stats_mutex);
3397 ofproto->stats.tx_packets++;
3398 ofproto->stats.tx_bytes += packet->size;
3399 ovs_mutex_unlock(&ofproto->stats_mutex);
3404 set_frag_handling(struct ofproto *ofproto_,
3405 enum ofp_config_flags frag_handling)
3407 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3408 if (frag_handling != OFPC_FRAG_REASM) {
3409 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3417 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3418 const struct flow *flow,
3419 const struct ofpact *ofpacts, size_t ofpacts_len)
3421 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3423 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3424 ofpacts_len, packet);
3431 set_netflow(struct ofproto *ofproto_,
3432 const struct netflow_options *netflow_options)
3434 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3436 if (netflow_options) {
3437 if (!ofproto->netflow) {
3438 ofproto->netflow = netflow_create();
3439 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3441 return netflow_set_options(ofproto->netflow, netflow_options);
3442 } else if (ofproto->netflow) {
3443 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3444 netflow_unref(ofproto->netflow);
3445 ofproto->netflow = NULL;
3452 get_netflow_ids(const struct ofproto *ofproto_,
3453 uint8_t *engine_type, uint8_t *engine_id)
3455 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3457 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3460 static struct ofproto_dpif *
3461 ofproto_dpif_lookup(const char *name)
3463 struct ofproto_dpif *ofproto;
3465 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3466 hash_string(name, 0), &all_ofproto_dpifs) {
3467 if (!strcmp(ofproto->up.name, name)) {
3475 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3476 const char *argv[], void *aux OVS_UNUSED)
3478 struct ofproto_dpif *ofproto;
3481 ofproto = ofproto_dpif_lookup(argv[1]);
3483 unixctl_command_reply_error(conn, "no such bridge");
3486 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3487 mac_learning_flush(ofproto->ml);
3488 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3490 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3491 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3492 mac_learning_flush(ofproto->ml);
3493 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3497 unixctl_command_reply(conn, "table successfully flushed");
3500 static struct ofport_dpif *
3501 ofbundle_get_a_port(const struct ofbundle *bundle)
3503 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3508 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3509 const char *argv[], void *aux OVS_UNUSED)
3511 struct ds ds = DS_EMPTY_INITIALIZER;
3512 const struct ofproto_dpif *ofproto;
3513 const struct mac_entry *e;
3515 ofproto = ofproto_dpif_lookup(argv[1]);
3517 unixctl_command_reply_error(conn, "no such bridge");
3521 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3522 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3523 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3524 struct ofbundle *bundle = e->port.p;
3525 char name[OFP_MAX_PORT_NAME_LEN];
3527 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3529 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
3530 name, e->vlan, ETH_ADDR_ARGS(e->mac),
3531 mac_entry_age(ofproto->ml, e));
3533 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3534 unixctl_command_reply(conn, ds_cstr(&ds));
3539 struct xlate_out xout;
3540 struct xlate_in xin;
3542 struct flow_wildcards wc;
3547 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
3549 struct rule_actions *actions;
3552 ds_put_char_multiple(result, '\t', level);
3554 ds_put_cstr(result, "No match\n");
3558 ovs_mutex_lock(&rule->up.mutex);
3559 cookie = rule->up.flow_cookie;
3560 ovs_mutex_unlock(&rule->up.mutex);
3562 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3563 rule ? rule->up.table_id : 0, ntohll(cookie));
3564 cls_rule_format(&rule->up.cr, result);
3565 ds_put_char(result, '\n');
3567 actions = rule_dpif_get_actions(rule);
3569 ds_put_char_multiple(result, '\t', level);
3570 ds_put_cstr(result, "OpenFlow actions=");
3571 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
3572 ds_put_char(result, '\n');
3574 rule_actions_unref(actions);
3578 trace_format_flow(struct ds *result, int level, const char *title,
3579 struct trace_ctx *trace)
3581 ds_put_char_multiple(result, '\t', level);
3582 ds_put_format(result, "%s: ", title);
3583 if (flow_equal(&trace->xin.flow, &trace->flow)) {
3584 ds_put_cstr(result, "unchanged");
3586 flow_format(result, &trace->xin.flow);
3587 trace->flow = trace->xin.flow;
3589 ds_put_char(result, '\n');
3593 trace_format_regs(struct ds *result, int level, const char *title,
3594 struct trace_ctx *trace)
3598 ds_put_char_multiple(result, '\t', level);
3599 ds_put_format(result, "%s:", title);
3600 for (i = 0; i < FLOW_N_REGS; i++) {
3601 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
3603 ds_put_char(result, '\n');
3607 trace_format_odp(struct ds *result, int level, const char *title,
3608 struct trace_ctx *trace)
3610 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
3612 ds_put_char_multiple(result, '\t', level);
3613 ds_put_format(result, "%s: ", title);
3614 format_odp_actions(result, odp_actions->data, odp_actions->size);
3615 ds_put_char(result, '\n');
3619 trace_format_megaflow(struct ds *result, int level, const char *title,
3620 struct trace_ctx *trace)
3624 ds_put_char_multiple(result, '\t', level);
3625 ds_put_format(result, "%s: ", title);
3626 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
3627 match_init(&match, &trace->flow, &trace->wc);
3628 match_format(&match, result, OFP_DEFAULT_PRIORITY);
3629 ds_put_char(result, '\n');
3633 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3635 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3636 struct ds *result = trace->result;
3638 ds_put_char(result, '\n');
3639 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3640 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3641 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
3642 trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
3643 trace_format_rule(result, recurse + 1, rule);
3647 trace_report(struct xlate_in *xin, const char *s, int recurse)
3649 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3650 struct ds *result = trace->result;
3652 ds_put_char_multiple(result, '\t', recurse);
3653 ds_put_cstr(result, s);
3654 ds_put_char(result, '\n');
3657 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
3658 * forms are supported:
3660 * - [dpname] odp_flow [-generate | packet]
3661 * - bridge br_flow [-generate | packet]
3663 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
3664 * returns a nonnull malloced error message. */
3665 static char * WARN_UNUSED_RESULT
3666 parse_flow_and_packet(int argc, const char *argv[],
3667 struct ofproto_dpif **ofprotop, struct flow *flow,
3668 struct ofpbuf **packetp)
3670 const struct dpif_backer *backer = NULL;
3671 const char *error = NULL;
3673 struct simap port_names = SIMAP_INITIALIZER(&port_names);
3674 struct ofpbuf *packet;
3675 struct ofpbuf odp_key;
3676 struct ofpbuf odp_mask;
3678 ofpbuf_init(&odp_key, 0);
3679 ofpbuf_init(&odp_mask, 0);
3681 /* Handle "-generate" or a hex string as the last argument. */
3682 if (!strcmp(argv[argc - 1], "-generate")) {
3683 packet = ofpbuf_new(0);
3686 error = eth_from_hex(argv[argc - 1], &packet);
3689 } else if (argc == 4) {
3690 /* The 3-argument form must end in "-generate' or a hex string. */
3696 /* odp_flow can have its in_port specified as a name instead of port no.
3697 * We do not yet know whether a given flow is a odp_flow or a br_flow.
3698 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3699 * we need to create a simap of name to port no. */
3701 const char *dp_type;
3702 if (!strncmp(argv[1], "ovs-", 4)) {
3703 dp_type = argv[1] + 4;
3707 backer = shash_find_data(&all_dpif_backers, dp_type);
3708 } else if (argc == 2) {
3709 struct shash_node *node;
3710 if (shash_count(&all_dpif_backers) == 1) {
3711 node = shash_first(&all_dpif_backers);
3712 backer = node->data;
3715 error = "Syntax error";
3718 if (backer && backer->dpif) {
3719 struct dpif_port dpif_port;
3720 struct dpif_port_dump port_dump;
3721 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
3722 simap_put(&port_names, dpif_port.name,
3723 odp_to_u32(dpif_port.port_no));
3727 /* Parse the flow and determine whether a datapath or
3728 * bridge is specified. If function odp_flow_key_from_string()
3729 * returns 0, the flow is a odp_flow. If function
3730 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
3731 if (!odp_flow_from_string(argv[argc - 1], &port_names,
3732 &odp_key, &odp_mask)) {
3734 error = "Cannot find the datapath";
3738 if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, flow,
3739 ofprotop, NULL, NULL, NULL, NULL)) {
3740 error = "Invalid datapath flow";
3744 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
3747 m_err = xasprintf("Bad flow syntax: %s", err);
3752 error = "Must specify bridge name";
3756 *ofprotop = ofproto_dpif_lookup(argv[1]);
3758 error = "Unknown bridge name";
3764 /* Generate a packet, if requested. */
3766 if (!packet->size) {
3767 flow_compose(packet, flow);
3769 union flow_in_port in_port = flow->in_port;
3771 /* Use the metadata from the flow and the packet argument
3772 * to reconstruct the flow. */
3773 flow_extract(packet, flow->skb_priority, flow->pkt_mark, NULL,
3779 if (error && !m_err) {
3780 m_err = xstrdup(error);
3783 ofpbuf_delete(packet);
3787 ofpbuf_uninit(&odp_key);
3788 ofpbuf_uninit(&odp_mask);
3789 simap_destroy(&port_names);
3794 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
3795 void *aux OVS_UNUSED)
3797 struct ofproto_dpif *ofproto;
3798 struct ofpbuf *packet;
3802 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3807 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
3808 unixctl_command_reply(conn, ds_cstr(&result));
3809 ds_destroy(&result);
3810 ofpbuf_delete(packet);
3812 unixctl_command_reply_error(conn, error);
3818 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
3819 const char *argv[], void *aux OVS_UNUSED)
3821 enum ofputil_protocol usable_protocols;
3822 struct ofproto_dpif *ofproto;
3823 bool enforce_consistency;
3824 struct ofpbuf ofpacts;
3825 struct ofpbuf *packet;
3830 /* Three kinds of error return values! */
3836 ofpbuf_init(&ofpacts, 0);
3838 /* Parse actions. */
3839 error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
3841 unixctl_command_reply_error(conn, error);
3846 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
3847 * consistency between the flow and the actions. With -consistent, we
3848 * enforce consistency even for a flow supported in OpenFlow 1.0. */
3849 if (!strcmp(argv[1], "-consistent")) {
3850 enforce_consistency = true;
3854 enforce_consistency = false;
3857 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3859 unixctl_command_reply_error(conn, error);
3864 /* Do the same checks as handle_packet_out() in ofproto.c.
3866 * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
3867 * strictly correct because these actions aren't in any table, but it's OK
3868 * because it 'table_id' is used only to check goto_table instructions, but
3869 * packet-outs take a list of actions and therefore it can't include
3872 * We skip the "meter" check here because meter is an instruction, not an
3873 * action, and thus cannot appear in ofpacts. */
3874 in_port = ofp_to_u16(flow.in_port.ofp_port);
3875 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
3876 unixctl_command_reply_error(conn, "invalid in_port");
3879 if (enforce_consistency) {
3880 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
3881 u16_to_ofp(ofproto->up.max_ports),
3882 0, 0, usable_protocols);
3884 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
3885 u16_to_ofp(ofproto->up.max_ports), 0, 0,
3891 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
3892 unixctl_command_reply_error(conn, ds_cstr(&result));
3896 ofproto_trace(ofproto, &flow, packet, ofpacts.data, ofpacts.size, &result);
3897 unixctl_command_reply(conn, ds_cstr(&result));
3900 ds_destroy(&result);
3901 ofpbuf_delete(packet);
3902 ofpbuf_uninit(&ofpacts);
3905 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
3906 * description of the results to 'ds'.
3908 * The trace follows a packet with the specified 'flow' through the flow
3909 * table. 'packet' may be nonnull to trace an actual packet, with consequent
3910 * side effects (if it is nonnull then its flow must be 'flow').
3912 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
3913 * trace, otherwise the actions are determined by a flow table lookup. */
3915 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
3916 const struct ofpbuf *packet,
3917 const struct ofpact ofpacts[], size_t ofpacts_len,
3920 struct rule_dpif *rule;
3921 struct trace_ctx trace;
3923 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
3924 ds_put_cstr(ds, "Flow: ");
3925 flow_format(ds, flow);
3926 ds_put_char(ds, '\n');
3928 flow_wildcards_init_catchall(&trace.wc);
3932 rule_dpif_lookup(ofproto, flow, &trace.wc, &rule);
3934 trace_format_rule(ds, 0, rule);
3935 if (rule == ofproto->miss_rule) {
3936 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
3937 } else if (rule == ofproto->no_packet_in_rule) {
3938 ds_put_cstr(ds, "\nNo match, packets dropped because "
3939 "OFPPC_NO_PACKET_IN is set on in_port.\n");
3940 } else if (rule == ofproto->drop_frags_rule) {
3941 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
3942 "and the fragment handling mode is \"drop\".\n");
3946 if (rule || ofpacts) {
3949 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
3952 xlate_in_init(&trace.xin, ofproto, flow, rule, tcp_flags, packet);
3954 trace.xin.ofpacts = ofpacts;
3955 trace.xin.ofpacts_len = ofpacts_len;
3957 trace.xin.resubmit_hook = trace_resubmit;
3958 trace.xin.report_hook = trace_report;
3960 xlate_actions(&trace.xin, &trace.xout);
3962 ds_put_char(ds, '\n');
3963 trace_format_flow(ds, 0, "Final flow", &trace);
3964 trace_format_megaflow(ds, 0, "Megaflow", &trace);
3966 ds_put_cstr(ds, "Datapath actions: ");
3967 format_odp_actions(ds, trace.xout.odp_actions.data,
3968 trace.xout.odp_actions.size);
3970 if (trace.xout.slow) {
3971 enum slow_path_reason slow;
3973 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
3974 "slow path because it:");
3976 slow = trace.xout.slow;
3978 enum slow_path_reason bit = rightmost_1bit(slow);
3980 ds_put_format(ds, "\n\t- %s.",
3981 slow_path_reason_to_explanation(bit));
3987 xlate_out_uninit(&trace.xout);
3990 rule_dpif_unref(rule);
3993 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
3994 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
3995 * to destroy 'ofproto_shash' and free the returned value. */
3996 static const struct shash_node **
3997 get_ofprotos(struct shash *ofproto_shash)
3999 const struct ofproto_dpif *ofproto;
4001 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4002 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4003 shash_add_nocopy(ofproto_shash, name, ofproto);
4006 return shash_sort(ofproto_shash);
4010 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4011 const char *argv[] OVS_UNUSED,
4012 void *aux OVS_UNUSED)
4014 struct ds ds = DS_EMPTY_INITIALIZER;
4015 struct shash ofproto_shash;
4016 const struct shash_node **sorted_ofprotos;
4019 shash_init(&ofproto_shash);
4020 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4021 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4022 const struct shash_node *node = sorted_ofprotos[i];
4023 ds_put_format(&ds, "%s\n", node->name);
4026 shash_destroy(&ofproto_shash);
4027 free(sorted_ofprotos);
4029 unixctl_command_reply(conn, ds_cstr(&ds));
4034 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4036 const struct shash_node **ofprotos;
4037 struct dpif_dp_stats dp_stats;
4038 struct shash ofproto_shash;
4041 dpif_get_dp_stats(backer->dpif, &dp_stats);
4043 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4044 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4046 shash_init(&ofproto_shash);
4047 ofprotos = get_ofprotos(&ofproto_shash);
4048 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4049 struct ofproto_dpif *ofproto = ofprotos[i]->data;
4050 const struct shash_node **ports;
4053 if (ofproto->backer != backer) {
4057 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4059 ports = shash_sort(&ofproto->up.port_by_name);
4060 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4061 const struct shash_node *node = ports[j];
4062 struct ofport *ofport = node->data;
4064 odp_port_t odp_port;
4066 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4069 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4070 if (odp_port != ODPP_NONE) {
4071 ds_put_format(ds, "%"PRIu32":", odp_port);
4073 ds_put_cstr(ds, "none:");
4076 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
4079 if (!netdev_get_config(ofport->netdev, &config)) {
4080 const struct smap_node **nodes;
4083 nodes = smap_sort(&config);
4084 for (i = 0; i < smap_count(&config); i++) {
4085 const struct smap_node *node = nodes[i];
4086 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4087 node->key, node->value);
4091 smap_destroy(&config);
4093 ds_put_char(ds, ')');
4094 ds_put_char(ds, '\n');
4098 shash_destroy(&ofproto_shash);
4103 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4104 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4106 struct ds ds = DS_EMPTY_INITIALIZER;
4107 const struct shash_node **backers;
4110 backers = shash_sort(&all_dpif_backers);
4111 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4112 dpif_show_backer(backers[i]->data, &ds);
4116 unixctl_command_reply(conn, ds_cstr(&ds));
4121 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4122 const struct nlattr *key, size_t key_len)
4124 struct ofproto_dpif *ofp;
4127 xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &ofp,
4128 NULL, NULL, NULL, NULL);
4129 return ofp == ofproto;
4133 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4134 int argc OVS_UNUSED, const char *argv[],
4135 void *aux OVS_UNUSED)
4137 struct ds ds = DS_EMPTY_INITIALIZER;
4138 const struct dpif_flow_stats *stats;
4139 const struct ofproto_dpif *ofproto;
4140 struct dpif_flow_dump flow_dump;
4141 const struct nlattr *actions;
4142 const struct nlattr *mask;
4143 const struct nlattr *key;
4147 bool verbosity = false;
4148 struct dpif_port dpif_port;
4149 struct dpif_port_dump port_dump;
4150 struct hmap portno_names;
4152 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4154 unixctl_command_reply_error(conn, "no such bridge");
4158 if (argc > 2 && !strcmp(argv[1], "-m")) {
4162 hmap_init(&portno_names);
4163 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4164 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4168 dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4169 while (dpif_flow_dump_next(&flow_dump, &key, &key_len, &mask, &mask_len,
4170 &actions, &actions_len, &stats)) {
4171 if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
4175 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4177 ds_put_cstr(&ds, ", ");
4178 dpif_flow_stats_format(stats, &ds);
4179 ds_put_cstr(&ds, ", actions:");
4180 format_odp_actions(&ds, actions, actions_len);
4181 ds_put_char(&ds, '\n');
4184 if (dpif_flow_dump_done(&flow_dump)) {
4186 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4187 unixctl_command_reply_error(conn, ds_cstr(&ds));
4189 unixctl_command_reply(conn, ds_cstr(&ds));
4191 odp_portno_names_destroy(&portno_names);
4192 hmap_destroy(&portno_names);
4197 ofproto_dpif_unixctl_init(void)
4199 static bool registered;
4205 unixctl_command_register(
4207 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4208 1, 3, ofproto_unixctl_trace, NULL);
4209 unixctl_command_register(
4210 "ofproto/trace-packet-out",
4211 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4212 2, 6, ofproto_unixctl_trace_actions, NULL);
4213 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4214 ofproto_unixctl_fdb_flush, NULL);
4215 unixctl_command_register("fdb/show", "bridge", 1, 1,
4216 ofproto_unixctl_fdb_show, NULL);
4217 unixctl_command_register("dpif/dump-dps", "", 0, 0,
4218 ofproto_unixctl_dpif_dump_dps, NULL);
4219 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4221 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4222 ofproto_unixctl_dpif_dump_flows, NULL);
4225 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4227 * This is deprecated. It is only for compatibility with broken device drivers
4228 * in old versions of Linux that do not properly support VLANs when VLAN
4229 * devices are not used. When broken device drivers are no longer in
4230 * widespread use, we will delete these interfaces. */
4233 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4235 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4236 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4238 if (realdev_ofp_port == ofport->realdev_ofp_port
4239 && vid == ofport->vlandev_vid) {
4243 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4245 if (ofport->realdev_ofp_port) {
4248 if (realdev_ofp_port && ofport->bundle) {
4249 /* vlandevs are enslaved to their realdevs, so they are not allowed to
4250 * themselves be part of a bundle. */
4251 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
4254 ofport->realdev_ofp_port = realdev_ofp_port;
4255 ofport->vlandev_vid = vid;
4257 if (realdev_ofp_port) {
4258 vsp_add(ofport, realdev_ofp_port, vid);
4265 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4267 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4271 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4272 OVS_EXCLUDED(ofproto->vsp_mutex)
4274 /* hmap_is_empty is thread safe. */
4275 return !hmap_is_empty(&ofproto->realdev_vid_map);
4279 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4280 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4281 OVS_REQUIRES(ofproto->vsp_mutex)
4283 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4284 int vid = vlan_tci_to_vid(vlan_tci);
4285 const struct vlan_splinter *vsp;
4287 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4288 hash_realdev_vid(realdev_ofp_port, vid),
4289 &ofproto->realdev_vid_map) {
4290 if (vsp->realdev_ofp_port == realdev_ofp_port
4291 && vsp->vid == vid) {
4292 return vsp->vlandev_ofp_port;
4296 return realdev_ofp_port;
4299 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4300 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4301 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
4302 * 'vlan_tci' 9, it would return the port number of eth0.9.
4304 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4305 * function just returns its 'realdev_ofp_port' argument. */
4307 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4308 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4309 OVS_EXCLUDED(ofproto->vsp_mutex)
4313 /* hmap_is_empty is thread safe, see if we can return immediately. */
4314 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
4315 return realdev_ofp_port;
4317 ovs_mutex_lock(&ofproto->vsp_mutex);
4318 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4319 ovs_mutex_unlock(&ofproto->vsp_mutex);
4323 static struct vlan_splinter *
4324 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4326 struct vlan_splinter *vsp;
4328 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4329 hash_ofp_port(vlandev_ofp_port),
4330 &ofproto->vlandev_map) {
4331 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4339 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4340 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4341 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
4342 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4343 * eth0 and store 9 in '*vid'.
4345 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4346 * VLAN device. Unless VLAN splinters are enabled, this is what this function
4349 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4350 ofp_port_t vlandev_ofp_port, int *vid)
4351 OVS_REQUIRES(ofproto->vsp_mutex)
4353 if (!hmap_is_empty(&ofproto->vlandev_map)) {
4354 const struct vlan_splinter *vsp;
4356 vsp = vlandev_find(ofproto, vlandev_ofp_port);
4361 return vsp->realdev_ofp_port;
4367 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4368 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
4369 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4370 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
4371 * always the case unless VLAN splinters are enabled), returns false without
4372 * making any changes. */
4374 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4375 OVS_EXCLUDED(ofproto->vsp_mutex)
4380 /* hmap_is_empty is thread safe. */
4381 if (hmap_is_empty(&ofproto->vlandev_map)) {
4385 ovs_mutex_lock(&ofproto->vsp_mutex);
4386 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4387 ovs_mutex_unlock(&ofproto->vsp_mutex);
4392 /* Cause the flow to be processed as if it came in on the real device with
4393 * the VLAN device's VLAN ID. */
4394 flow->in_port.ofp_port = realdev;
4395 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4400 vsp_remove(struct ofport_dpif *port)
4402 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4403 struct vlan_splinter *vsp;
4405 ovs_mutex_lock(&ofproto->vsp_mutex);
4406 vsp = vlandev_find(ofproto, port->up.ofp_port);
4408 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4409 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4412 port->realdev_ofp_port = 0;
4414 VLOG_ERR("missing vlan device record");
4416 ovs_mutex_unlock(&ofproto->vsp_mutex);
4420 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4422 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4424 ovs_mutex_lock(&ofproto->vsp_mutex);
4425 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4426 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4427 == realdev_ofp_port)) {
4428 struct vlan_splinter *vsp;
4430 vsp = xmalloc(sizeof *vsp);
4431 vsp->realdev_ofp_port = realdev_ofp_port;
4432 vsp->vlandev_ofp_port = port->up.ofp_port;
4435 port->realdev_ofp_port = realdev_ofp_port;
4437 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4438 hash_ofp_port(port->up.ofp_port));
4439 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4440 hash_realdev_vid(realdev_ofp_port, vid));
4442 VLOG_ERR("duplicate vlan device record");
4444 ovs_mutex_unlock(&ofproto->vsp_mutex);
4448 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4450 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4451 return ofport ? ofport->odp_port : ODPP_NONE;
4454 struct ofport_dpif *
4455 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4457 struct ofport_dpif *port;
4459 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4460 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4461 &backer->odp_to_ofport_map) {
4462 if (port->odp_port == odp_port) {
4463 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4468 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4473 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4475 struct ofport_dpif *port;
4477 port = odp_port_to_ofport(ofproto->backer, odp_port);
4478 if (port && &ofproto->up == port->up.ofproto) {
4479 return port->up.ofp_port;
4485 const struct ofproto_class ofproto_dpif_class = {
4499 NULL, /* get_memory_usage. */
4500 type_get_memory_usage,
4519 port_is_lacp_current,
4520 NULL, /* rule_choose_table */
4529 rule_modify_actions,
4543 get_stp_port_status,
4551 is_mirror_output_bundle,
4552 forward_bpdu_changed,
4553 set_mac_table_config,
4555 NULL, /* meter_get_features */
4556 NULL, /* meter_set */
4557 NULL, /* meter_get */
4558 NULL, /* meter_del */
4559 group_alloc, /* group_alloc */
4560 group_construct, /* group_construct */
4561 group_destruct, /* group_destruct */
4562 group_dealloc, /* group_dealloc */
4563 group_modify, /* group_modify */
4564 group_get_stats, /* group_get_stats */