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 struct dpif_flow_stats stats OVS_GUARDED;
93 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
95 static struct rule_dpif *rule_dpif_cast(const struct rule *);
96 static void rule_expire(struct rule_dpif *);
103 * - Do include packets and bytes from datapath flows which have not
104 * recently been processed by a revalidator. */
105 struct ovs_mutex stats_mutex;
106 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
107 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
108 struct bucket_counter *bucket_stats OVS_GUARDED; /* Bucket statistics. */
112 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
113 struct ofproto_dpif *ofproto; /* Owning ofproto. */
114 void *aux; /* Key supplied by ofproto's client. */
115 char *name; /* Identifier for log messages. */
118 struct list ports; /* Contains "struct ofport"s. */
119 enum port_vlan_mode vlan_mode; /* VLAN mode */
120 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
121 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
122 * NULL if all VLANs are trunked. */
123 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
124 struct bond *bond; /* Nonnull iff more than one port. */
125 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
128 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
131 static void bundle_remove(struct ofport *);
132 static void bundle_update(struct ofbundle *);
133 static void bundle_destroy(struct ofbundle *);
134 static void bundle_del_port(struct ofport_dpif *);
135 static void bundle_run(struct ofbundle *);
136 static void bundle_wait(struct ofbundle *);
138 static void stp_run(struct ofproto_dpif *ofproto);
139 static void stp_wait(struct ofproto_dpif *ofproto);
140 static int set_stp_port(struct ofport *,
141 const struct ofproto_port_stp_settings *);
144 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
148 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
149 struct list bundle_node; /* In struct ofbundle's "ports" list. */
150 struct cfm *cfm; /* Connectivity Fault Management, if any. */
151 struct bfd *bfd; /* BFD, if any. */
152 bool may_enable; /* May be enabled in bonds. */
153 bool is_tunnel; /* This port is a tunnel. */
154 bool is_layer3; /* This is a layer 3 port. */
155 long long int carrier_seq; /* Carrier status changes. */
156 struct ofport_dpif *peer; /* Peer if patch port. */
159 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
160 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
161 long long int stp_state_entered;
163 /* Queue to DSCP mapping. */
164 struct ofproto_port_queue *qdscp;
167 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
169 * This is deprecated. It is only for compatibility with broken device
170 * drivers in old versions of Linux that do not properly support VLANs when
171 * VLAN devices are not used. When broken device drivers are no longer in
172 * widespread use, we will delete these interfaces. */
173 ofp_port_t realdev_ofp_port;
177 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
179 * This is deprecated. It is only for compatibility with broken device drivers
180 * in old versions of Linux that do not properly support VLANs when VLAN
181 * devices are not used. When broken device drivers are no longer in
182 * widespread use, we will delete these interfaces. */
183 struct vlan_splinter {
184 struct hmap_node realdev_vid_node;
185 struct hmap_node vlandev_node;
186 ofp_port_t realdev_ofp_port;
187 ofp_port_t vlandev_ofp_port;
191 static void vsp_remove(struct ofport_dpif *);
192 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
194 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
197 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
200 static struct ofport_dpif *
201 ofport_dpif_cast(const struct ofport *ofport)
203 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
206 static void port_run(struct ofport_dpif *);
207 static int set_bfd(struct ofport *, const struct smap *);
208 static int set_cfm(struct ofport *, const struct cfm_settings *);
209 static void ofport_update_peer(struct ofport_dpif *);
211 struct dpif_completion {
212 struct list list_node;
213 struct ofoperation *op;
216 /* Reasons that we might need to revalidate every datapath flow, and
217 * corresponding coverage counters.
219 * A value of 0 means that there is no need to revalidate.
221 * It would be nice to have some cleaner way to integrate with coverage
222 * counters, but with only a few reasons I guess this is good enough for
224 enum revalidate_reason {
225 REV_RECONFIGURE = 1, /* Switch configuration changed. */
226 REV_STP, /* Spanning tree protocol port status change. */
227 REV_BOND, /* Bonding changed. */
228 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
229 REV_FLOW_TABLE, /* Flow table changed. */
230 REV_MAC_LEARNING, /* Mac learning changed. */
232 COVERAGE_DEFINE(rev_reconfigure);
233 COVERAGE_DEFINE(rev_stp);
234 COVERAGE_DEFINE(rev_bond);
235 COVERAGE_DEFINE(rev_port_toggled);
236 COVERAGE_DEFINE(rev_flow_table);
237 COVERAGE_DEFINE(rev_mac_learning);
239 /* All datapaths of a given type share a single dpif backer instance. */
246 struct ovs_rwlock odp_to_ofport_lock;
247 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
249 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
251 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
253 bool recv_set_enable; /* Enables or disables receiving packets. */
255 /* True if the datapath supports variable-length
256 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
257 * False if the datapath supports only 8-byte (or shorter) userdata. */
258 bool variable_length_userdata;
260 /* Maximum number of MPLS label stack entries that the datapath supports
262 size_t max_mpls_depth;
265 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
266 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
268 struct ofproto_dpif {
269 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
271 struct dpif_backer *backer;
273 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
275 /* Special OpenFlow rules. */
276 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
277 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
278 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
281 struct netflow *netflow;
282 struct dpif_sflow *sflow;
283 struct dpif_ipfix *ipfix;
284 struct hmap bundles; /* Contains "struct ofbundle"s. */
285 struct mac_learning *ml;
286 bool has_bonded_bundles;
288 struct mbridge *mbridge;
290 struct ovs_mutex stats_mutex;
291 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
292 * consumed in userspace. */
296 long long int stp_last_tick;
298 /* VLAN splinters. */
299 struct ovs_mutex vsp_mutex;
300 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
301 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
304 struct sset ports; /* Set of standard port names. */
305 struct sset ghost_ports; /* Ports with no datapath port. */
306 struct sset port_poll_set; /* Queued names for port_poll() reply. */
307 int port_poll_errno; /* Last errno for port_poll() reply. */
308 uint64_t change_seq; /* Connectivity status changes. */
311 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
314 /* All existing ofproto_dpif instances, indexed by ->up.name. */
315 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
317 static void ofproto_dpif_unixctl_init(void);
319 static inline struct ofproto_dpif *
320 ofproto_dpif_cast(const struct ofproto *ofproto)
322 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
323 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
327 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
329 return ofproto->backer->max_mpls_depth;
332 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
333 ofp_port_t ofp_port);
334 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
335 const struct ofpbuf *packet,
336 const struct ofpact[], size_t ofpacts_len,
339 /* Global variables. */
340 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
342 /* Initial mappings of port to bridge mappings. */
343 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
345 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
348 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
349 struct ofputil_flow_mod *fm)
351 ofproto_flow_mod(&ofproto->up, fm);
354 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
355 * Takes ownership of 'pin' and pin->packet. */
357 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
358 struct ofproto_packet_in *pin)
360 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
361 COVERAGE_INC(packet_in_overflow);
362 free(CONST_CAST(void *, pin->up.packet));
367 /* Factory functions. */
370 init(const struct shash *iface_hints)
372 struct shash_node *node;
374 /* Make a local copy, since we don't own 'iface_hints' elements. */
375 SHASH_FOR_EACH(node, iface_hints) {
376 const struct iface_hint *orig_hint = node->data;
377 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
379 new_hint->br_name = xstrdup(orig_hint->br_name);
380 new_hint->br_type = xstrdup(orig_hint->br_type);
381 new_hint->ofp_port = orig_hint->ofp_port;
383 shash_add(&init_ofp_ports, node->name, new_hint);
388 enumerate_types(struct sset *types)
390 dp_enumerate_types(types);
394 enumerate_names(const char *type, struct sset *names)
396 struct ofproto_dpif *ofproto;
399 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
400 if (strcmp(type, ofproto->up.type)) {
403 sset_add(names, ofproto->up.name);
410 del(const char *type, const char *name)
415 error = dpif_open(name, type, &dpif);
417 error = dpif_delete(dpif);
424 port_open_type(const char *datapath_type, const char *port_type)
426 return dpif_port_open_type(datapath_type, port_type);
429 /* Type functions. */
431 static void process_dpif_port_changes(struct dpif_backer *);
432 static void process_dpif_all_ports_changed(struct dpif_backer *);
433 static void process_dpif_port_change(struct dpif_backer *,
434 const char *devname);
435 static void process_dpif_port_error(struct dpif_backer *, int error);
437 static struct ofproto_dpif *
438 lookup_ofproto_dpif_by_port_name(const char *name)
440 struct ofproto_dpif *ofproto;
442 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
443 if (sset_contains(&ofproto->ports, name)) {
452 type_run(const char *type)
454 struct dpif_backer *backer;
456 backer = shash_find_data(&all_dpif_backers, type);
458 /* This is not necessarily a problem, since backers are only
459 * created on demand. */
463 dpif_run(backer->dpif);
465 /* If vswitchd started with other_config:flow_restore_wait set as "true",
466 * and the configuration has now changed to "false", enable receiving
467 * packets from the datapath. */
468 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
471 backer->recv_set_enable = true;
473 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
475 VLOG_ERR("Failed to enable receiving packets in dpif.");
478 dpif_flow_flush(backer->dpif);
479 backer->need_revalidate = REV_RECONFIGURE;
482 if (backer->recv_set_enable) {
483 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
486 if (backer->need_revalidate) {
487 struct ofproto_dpif *ofproto;
488 struct simap_node *node;
489 struct simap tmp_backers;
491 /* Handle tunnel garbage collection. */
492 simap_init(&tmp_backers);
493 simap_swap(&backer->tnl_backers, &tmp_backers);
495 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
496 struct ofport_dpif *iter;
498 if (backer != ofproto->backer) {
502 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
503 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
506 if (!iter->is_tunnel) {
510 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
511 namebuf, sizeof namebuf);
512 node = simap_find(&tmp_backers, dp_port);
514 simap_put(&backer->tnl_backers, dp_port, node->data);
515 simap_delete(&tmp_backers, node);
516 node = simap_find(&backer->tnl_backers, dp_port);
518 node = simap_find(&backer->tnl_backers, dp_port);
520 odp_port_t odp_port = ODPP_NONE;
522 if (!dpif_port_add(backer->dpif, iter->up.netdev,
524 simap_put(&backer->tnl_backers, dp_port,
525 odp_to_u32(odp_port));
526 node = simap_find(&backer->tnl_backers, dp_port);
531 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
532 if (tnl_port_reconfigure(iter, iter->up.netdev,
534 backer->need_revalidate = REV_RECONFIGURE;
539 SIMAP_FOR_EACH (node, &tmp_backers) {
540 dpif_port_del(backer->dpif, u32_to_odp(node->data));
542 simap_destroy(&tmp_backers);
544 switch (backer->need_revalidate) {
545 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
546 case REV_STP: COVERAGE_INC(rev_stp); break;
547 case REV_BOND: COVERAGE_INC(rev_bond); break;
548 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
549 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
550 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
552 backer->need_revalidate = 0;
554 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
555 struct ofport_dpif *ofport;
556 struct ofbundle *bundle;
558 if (ofproto->backer != backer) {
562 ovs_rwlock_wrlock(&xlate_rwlock);
563 xlate_ofproto_set(ofproto, ofproto->up.name,
564 ofproto->backer->dpif, ofproto->miss_rule,
565 ofproto->no_packet_in_rule, ofproto->ml,
566 ofproto->stp, ofproto->mbridge,
567 ofproto->sflow, ofproto->ipfix,
568 ofproto->netflow, ofproto->up.frag_handling,
569 ofproto->up.forward_bpdu,
570 connmgr_has_in_band(ofproto->up.connmgr),
571 ofproto->backer->variable_length_userdata,
572 ofproto->backer->max_mpls_depth);
574 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
575 xlate_bundle_set(ofproto, bundle, bundle->name,
576 bundle->vlan_mode, bundle->vlan,
577 bundle->trunks, bundle->use_priority_tags,
578 bundle->bond, bundle->lacp,
582 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
583 int stp_port = ofport->stp_port
584 ? stp_port_no(ofport->stp_port)
586 xlate_ofport_set(ofproto, ofport->bundle, ofport,
587 ofport->up.ofp_port, ofport->odp_port,
588 ofport->up.netdev, ofport->cfm,
589 ofport->bfd, ofport->peer, stp_port,
590 ofport->qdscp, ofport->n_qdscp,
591 ofport->up.pp.config, ofport->up.pp.state,
592 ofport->is_tunnel, ofport->may_enable);
594 ovs_rwlock_unlock(&xlate_rwlock);
597 udpif_revalidate(backer->udpif);
600 process_dpif_port_changes(backer);
605 /* Check for and handle port changes in 'backer''s dpif. */
607 process_dpif_port_changes(struct dpif_backer *backer)
613 error = dpif_port_poll(backer->dpif, &devname);
619 process_dpif_all_ports_changed(backer);
623 process_dpif_port_change(backer, devname);
628 process_dpif_port_error(backer, error);
635 process_dpif_all_ports_changed(struct dpif_backer *backer)
637 struct ofproto_dpif *ofproto;
638 struct dpif_port dpif_port;
639 struct dpif_port_dump dump;
640 struct sset devnames;
643 sset_init(&devnames);
644 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
645 if (ofproto->backer == backer) {
646 struct ofport *ofport;
648 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
649 sset_add(&devnames, netdev_get_name(ofport->netdev));
653 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
654 sset_add(&devnames, dpif_port.name);
657 SSET_FOR_EACH (devname, &devnames) {
658 process_dpif_port_change(backer, devname);
660 sset_destroy(&devnames);
664 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
666 struct ofproto_dpif *ofproto;
667 struct dpif_port port;
669 /* Don't report on the datapath's device. */
670 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
674 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
675 &all_ofproto_dpifs) {
676 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
681 ofproto = lookup_ofproto_dpif_by_port_name(devname);
682 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
683 /* The port was removed. If we know the datapath,
684 * report it through poll_set(). If we don't, it may be
685 * notifying us of a removal we initiated, so ignore it.
686 * If there's a pending ENOBUFS, let it stand, since
687 * everything will be reevaluated. */
688 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
689 sset_add(&ofproto->port_poll_set, devname);
690 ofproto->port_poll_errno = 0;
692 } else if (!ofproto) {
693 /* The port was added, but we don't know with which
694 * ofproto we should associate it. Delete it. */
695 dpif_port_del(backer->dpif, port.port_no);
697 struct ofport_dpif *ofport;
699 ofport = ofport_dpif_cast(shash_find_data(
700 &ofproto->up.port_by_name, devname));
702 && ofport->odp_port != port.port_no
703 && !odp_port_to_ofport(backer, port.port_no))
705 /* 'ofport''s datapath port number has changed from
706 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
707 * structures to match. */
708 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
709 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
710 ofport->odp_port = port.port_no;
711 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
712 hash_odp_port(port.port_no));
713 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
714 backer->need_revalidate = REV_RECONFIGURE;
717 dpif_port_destroy(&port);
720 /* Propagate 'error' to all ofprotos based on 'backer'. */
722 process_dpif_port_error(struct dpif_backer *backer, int error)
724 struct ofproto_dpif *ofproto;
726 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
727 if (ofproto->backer == backer) {
728 sset_clear(&ofproto->port_poll_set);
729 ofproto->port_poll_errno = error;
735 type_wait(const char *type)
737 struct dpif_backer *backer;
739 backer = shash_find_data(&all_dpif_backers, type);
741 /* This is not necessarily a problem, since backers are only
742 * created on demand. */
746 dpif_wait(backer->dpif);
749 /* Basic life-cycle. */
751 static int add_internal_flows(struct ofproto_dpif *);
753 static struct ofproto *
756 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
761 dealloc(struct ofproto *ofproto_)
763 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
768 close_dpif_backer(struct dpif_backer *backer)
770 ovs_assert(backer->refcount > 0);
772 if (--backer->refcount) {
776 udpif_destroy(backer->udpif);
778 simap_destroy(&backer->tnl_backers);
779 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
780 hmap_destroy(&backer->odp_to_ofport_map);
781 shash_find_and_delete(&all_dpif_backers, backer->type);
783 dpif_close(backer->dpif);
788 /* Datapath port slated for removal from datapath. */
790 struct list list_node;
794 static bool check_variable_length_userdata(struct dpif_backer *backer);
795 static size_t check_max_mpls_depth(struct dpif_backer *backer);
798 open_dpif_backer(const char *type, struct dpif_backer **backerp)
800 struct dpif_backer *backer;
801 struct dpif_port_dump port_dump;
802 struct dpif_port port;
803 struct shash_node *node;
804 struct list garbage_list;
805 struct odp_garbage *garbage, *next;
811 backer = shash_find_data(&all_dpif_backers, type);
818 backer_name = xasprintf("ovs-%s", type);
820 /* Remove any existing datapaths, since we assume we're the only
821 * userspace controlling the datapath. */
823 dp_enumerate_names(type, &names);
824 SSET_FOR_EACH(name, &names) {
825 struct dpif *old_dpif;
827 /* Don't remove our backer if it exists. */
828 if (!strcmp(name, backer_name)) {
832 if (dpif_open(name, type, &old_dpif)) {
833 VLOG_WARN("couldn't open old datapath %s to remove it", name);
835 dpif_delete(old_dpif);
836 dpif_close(old_dpif);
839 sset_destroy(&names);
841 backer = xmalloc(sizeof *backer);
843 error = dpif_create_and_open(backer_name, type, &backer->dpif);
846 VLOG_ERR("failed to open datapath of type %s: %s", type,
847 ovs_strerror(error));
851 backer->udpif = udpif_create(backer, backer->dpif);
853 backer->type = xstrdup(type);
854 backer->refcount = 1;
855 hmap_init(&backer->odp_to_ofport_map);
856 ovs_rwlock_init(&backer->odp_to_ofport_lock);
857 backer->need_revalidate = 0;
858 simap_init(&backer->tnl_backers);
859 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
862 if (backer->recv_set_enable) {
863 dpif_flow_flush(backer->dpif);
866 /* Loop through the ports already on the datapath and remove any
867 * that we don't need anymore. */
868 list_init(&garbage_list);
869 dpif_port_dump_start(&port_dump, backer->dpif);
870 while (dpif_port_dump_next(&port_dump, &port)) {
871 node = shash_find(&init_ofp_ports, port.name);
872 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
873 garbage = xmalloc(sizeof *garbage);
874 garbage->odp_port = port.port_no;
875 list_push_front(&garbage_list, &garbage->list_node);
878 dpif_port_dump_done(&port_dump);
880 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
881 dpif_port_del(backer->dpif, garbage->odp_port);
882 list_remove(&garbage->list_node);
886 shash_add(&all_dpif_backers, type, backer);
888 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
890 VLOG_ERR("failed to listen on datapath of type %s: %s",
891 type, ovs_strerror(error));
892 close_dpif_backer(backer);
895 backer->variable_length_userdata = check_variable_length_userdata(backer);
896 backer->max_mpls_depth = check_max_mpls_depth(backer);
898 if (backer->recv_set_enable) {
899 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
905 /* Tests whether 'backer''s datapath supports variable-length
906 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
907 * to disable some features on older datapaths that don't support this
910 * Returns false if 'backer' definitely does not support variable-length
911 * userdata, true if it seems to support them or if at least the error we get
914 check_variable_length_userdata(struct dpif_backer *backer)
916 struct eth_header *eth;
917 struct ofpbuf actions;
918 struct dpif_execute execute;
919 struct ofpbuf packet;
923 /* Compose a userspace action that will cause an ERANGE error on older
924 * datapaths that don't support variable-length userdata.
926 * We really test for using userdata longer than 8 bytes, but older
927 * datapaths accepted these, silently truncating the userdata to 8 bytes.
928 * The same older datapaths rejected userdata shorter than 8 bytes, so we
929 * test for that instead as a proxy for longer userdata support. */
930 ofpbuf_init(&actions, 64);
931 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
932 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
933 dpif_port_get_pid(backer->dpif, ODPP_NONE));
934 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
935 nl_msg_end_nested(&actions, start);
937 /* Compose a dummy ethernet packet. */
938 ofpbuf_init(&packet, ETH_HEADER_LEN);
939 eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
940 eth->eth_type = htons(0x1234);
942 /* Execute the actions. On older datapaths this fails with ERANGE, on
943 * newer datapaths it succeeds. */
944 execute.actions = actions.data;
945 execute.actions_len = actions.size;
946 execute.packet = &packet;
947 execute.md = PKT_METADATA_INITIALIZER(0);
948 execute.needs_help = false;
950 error = dpif_execute(backer->dpif, &execute);
952 ofpbuf_uninit(&packet);
953 ofpbuf_uninit(&actions);
957 /* Variable-length userdata is supported.
959 * Purge received packets to avoid processing the nonsense packet we
960 * sent to userspace, then report success. */
961 dpif_recv_purge(backer->dpif);
965 /* Variable-length userdata is not supported. */
966 VLOG_WARN("%s: datapath does not support variable-length userdata "
967 "feature (needs Linux 3.10+ or kernel module from OVS "
968 "1..11+). The NXAST_SAMPLE action will be ignored.",
969 dpif_name(backer->dpif));
973 /* Something odd happened. We're not sure whether variable-length
974 * userdata is supported. Default to "yes". */
975 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
976 dpif_name(backer->dpif), ovs_strerror(error));
981 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
983 * Returns the number of elements in a struct flow's mpls_lse field
984 * if the datapath supports at least that many entries in an
986 * Otherwise returns the number of MPLS push actions supported by
989 check_max_mpls_depth(struct dpif_backer *backer)
994 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
995 struct odputil_keybuf keybuf;
999 memset(&flow, 0, sizeof flow);
1000 flow.dl_type = htons(ETH_TYPE_MPLS);
1001 flow_set_mpls_bos(&flow, n, 1);
1003 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1004 odp_flow_key_from_flow(&key, &flow, 0);
1006 error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
1007 key.data, key.size, NULL, 0, NULL, 0, NULL);
1008 if (error && error != EEXIST) {
1009 if (error != EINVAL) {
1010 VLOG_WARN("%s: MPLS stack length feature probe failed (%s)",
1011 dpif_name(backer->dpif), ovs_strerror(error));
1016 error = dpif_flow_del(backer->dpif, key.data, key.size, NULL);
1018 VLOG_WARN("%s: failed to delete MPLS feature probe flow",
1019 dpif_name(backer->dpif));
1023 VLOG_INFO("%s: MPLS label stack length probed as %d",
1024 dpif_name(backer->dpif), n);
1029 construct(struct ofproto *ofproto_)
1031 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1032 struct shash_node *node, *next;
1035 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1040 ofproto->netflow = NULL;
1041 ofproto->sflow = NULL;
1042 ofproto->ipfix = NULL;
1043 ofproto->stp = NULL;
1044 ofproto->dump_seq = 0;
1045 hmap_init(&ofproto->bundles);
1046 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1047 ofproto->mbridge = mbridge_create();
1048 ofproto->has_bonded_bundles = false;
1049 ofproto->lacp_enabled = false;
1050 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1051 ovs_mutex_init(&ofproto->vsp_mutex);
1053 guarded_list_init(&ofproto->pins);
1055 ofproto_dpif_unixctl_init();
1057 hmap_init(&ofproto->vlandev_map);
1058 hmap_init(&ofproto->realdev_vid_map);
1060 sset_init(&ofproto->ports);
1061 sset_init(&ofproto->ghost_ports);
1062 sset_init(&ofproto->port_poll_set);
1063 ofproto->port_poll_errno = 0;
1064 ofproto->change_seq = 0;
1066 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1067 struct iface_hint *iface_hint = node->data;
1069 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1070 /* Check if the datapath already has this port. */
1071 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1072 sset_add(&ofproto->ports, node->name);
1075 free(iface_hint->br_name);
1076 free(iface_hint->br_type);
1078 shash_delete(&init_ofp_ports, node);
1082 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1083 hash_string(ofproto->up.name, 0));
1084 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1086 ofproto_init_tables(ofproto_, N_TABLES);
1087 error = add_internal_flows(ofproto);
1088 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1094 add_internal_flow(struct ofproto_dpif *ofproto, int id,
1095 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1097 struct ofputil_flow_mod fm;
1100 match_init_catchall(&fm.match);
1102 match_set_reg(&fm.match, 0, id);
1103 fm.new_cookie = htonll(0);
1104 fm.cookie = htonll(0);
1105 fm.cookie_mask = htonll(0);
1106 fm.modify_cookie = false;
1107 fm.table_id = TBL_INTERNAL;
1108 fm.command = OFPFC_ADD;
1109 fm.idle_timeout = 0;
1110 fm.hard_timeout = 0;
1114 fm.ofpacts = ofpacts->data;
1115 fm.ofpacts_len = ofpacts->size;
1117 error = ofproto_flow_mod(&ofproto->up, &fm);
1119 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1120 id, ofperr_to_string(error));
1124 if (rule_dpif_lookup_in_table(ofproto, &fm.match.flow, NULL, TBL_INTERNAL,
1126 rule_dpif_unref(*rulep);
1135 add_internal_flows(struct ofproto_dpif *ofproto)
1137 struct ofpact_controller *controller;
1138 uint64_t ofpacts_stub[128 / 8];
1139 struct ofpbuf ofpacts;
1143 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1146 controller = ofpact_put_CONTROLLER(&ofpacts);
1147 controller->max_len = UINT16_MAX;
1148 controller->controller_id = 0;
1149 controller->reason = OFPR_NO_MATCH;
1150 ofpact_pad(&ofpacts);
1152 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1157 ofpbuf_clear(&ofpacts);
1158 error = add_internal_flow(ofproto, id++, &ofpacts,
1159 &ofproto->no_packet_in_rule);
1164 error = add_internal_flow(ofproto, id++, &ofpacts,
1165 &ofproto->drop_frags_rule);
1170 destruct(struct ofproto *ofproto_)
1172 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1173 struct rule_dpif *rule, *next_rule;
1174 struct ofproto_packet_in *pin, *next_pin;
1175 struct oftable *table;
1178 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1179 ovs_rwlock_wrlock(&xlate_rwlock);
1180 xlate_remove_ofproto(ofproto);
1181 ovs_rwlock_unlock(&xlate_rwlock);
1183 /* Ensure that the upcall processing threads have no remaining references
1184 * to the ofproto or anything in it. */
1185 udpif_synchronize(ofproto->backer->udpif);
1187 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1189 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1190 struct cls_cursor cursor;
1192 fat_rwlock_rdlock(&table->cls.rwlock);
1193 cls_cursor_init(&cursor, &table->cls, NULL);
1194 fat_rwlock_unlock(&table->cls.rwlock);
1195 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1196 ofproto_rule_delete(&ofproto->up, &rule->up);
1200 guarded_list_pop_all(&ofproto->pins, &pins);
1201 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1202 list_remove(&pin->list_node);
1203 free(CONST_CAST(void *, pin->up.packet));
1206 guarded_list_destroy(&ofproto->pins);
1208 mbridge_unref(ofproto->mbridge);
1210 netflow_unref(ofproto->netflow);
1211 dpif_sflow_unref(ofproto->sflow);
1212 hmap_destroy(&ofproto->bundles);
1213 mac_learning_unref(ofproto->ml);
1215 hmap_destroy(&ofproto->vlandev_map);
1216 hmap_destroy(&ofproto->realdev_vid_map);
1218 sset_destroy(&ofproto->ports);
1219 sset_destroy(&ofproto->ghost_ports);
1220 sset_destroy(&ofproto->port_poll_set);
1222 ovs_mutex_destroy(&ofproto->stats_mutex);
1223 ovs_mutex_destroy(&ofproto->vsp_mutex);
1225 close_dpif_backer(ofproto->backer);
1229 run(struct ofproto *ofproto_)
1231 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1232 uint64_t new_seq, new_dump_seq;
1234 if (mbridge_need_revalidate(ofproto->mbridge)) {
1235 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1236 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1237 mac_learning_flush(ofproto->ml);
1238 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1241 /* Do not perform any periodic activity required by 'ofproto' while
1242 * waiting for flow restore to complete. */
1243 if (!ofproto_get_flow_restore_wait()) {
1244 struct ofproto_packet_in *pin, *next_pin;
1247 guarded_list_pop_all(&ofproto->pins, &pins);
1248 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1249 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1250 list_remove(&pin->list_node);
1251 free(CONST_CAST(void *, pin->up.packet));
1256 if (ofproto->netflow) {
1257 netflow_run(ofproto->netflow);
1259 if (ofproto->sflow) {
1260 dpif_sflow_run(ofproto->sflow);
1262 if (ofproto->ipfix) {
1263 dpif_ipfix_run(ofproto->ipfix);
1266 new_seq = seq_read(connectivity_seq_get());
1267 if (ofproto->change_seq != new_seq) {
1268 struct ofport_dpif *ofport;
1270 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1274 ofproto->change_seq = new_seq;
1276 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1277 struct ofbundle *bundle;
1279 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1285 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1286 if (mac_learning_run(ofproto->ml)) {
1287 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1289 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1291 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1292 if (ofproto->dump_seq != new_dump_seq) {
1293 struct rule *rule, *next_rule;
1295 /* We know stats are relatively fresh, so now is a good time to do some
1297 ofproto->dump_seq = new_dump_seq;
1299 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1301 ovs_mutex_lock(&ofproto_mutex);
1302 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1303 &ofproto->up.expirable) {
1304 rule_expire(rule_dpif_cast(rule));
1306 ovs_mutex_unlock(&ofproto_mutex);
1308 /* All outstanding data in existing flows has been accounted, so it's a
1309 * good time to do bond rebalancing. */
1310 if (ofproto->has_bonded_bundles) {
1311 struct ofbundle *bundle;
1313 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1315 bond_rebalance(bundle->bond);
1325 wait(struct ofproto *ofproto_)
1327 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1329 if (ofproto_get_flow_restore_wait()) {
1333 if (ofproto->sflow) {
1334 dpif_sflow_wait(ofproto->sflow);
1336 if (ofproto->ipfix) {
1337 dpif_ipfix_wait(ofproto->ipfix);
1339 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1340 struct ofbundle *bundle;
1342 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1343 bundle_wait(bundle);
1346 if (ofproto->netflow) {
1347 netflow_wait(ofproto->netflow);
1349 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1350 mac_learning_wait(ofproto->ml);
1351 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1353 if (ofproto->backer->need_revalidate) {
1354 /* Shouldn't happen, but if it does just go around again. */
1355 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1356 poll_immediate_wake();
1359 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1363 type_get_memory_usage(const char *type, struct simap *usage)
1365 struct dpif_backer *backer;
1367 backer = shash_find_data(&all_dpif_backers, type);
1369 udpif_get_memory_usage(backer->udpif, usage);
1374 flush(struct ofproto *ofproto OVS_UNUSED)
1380 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1381 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1383 *arp_match_ip = true;
1384 *actions = (OFPUTIL_A_OUTPUT |
1385 OFPUTIL_A_SET_VLAN_VID |
1386 OFPUTIL_A_SET_VLAN_PCP |
1387 OFPUTIL_A_STRIP_VLAN |
1388 OFPUTIL_A_SET_DL_SRC |
1389 OFPUTIL_A_SET_DL_DST |
1390 OFPUTIL_A_SET_NW_SRC |
1391 OFPUTIL_A_SET_NW_DST |
1392 OFPUTIL_A_SET_NW_TOS |
1393 OFPUTIL_A_SET_TP_SRC |
1394 OFPUTIL_A_SET_TP_DST |
1399 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1401 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1402 struct dpif_dp_stats s;
1403 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1407 strcpy(ots->name, "classifier");
1409 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1410 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes, &used);
1411 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes,
1413 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes,
1415 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1416 ots->lookup_count = htonll(n_lookup);
1417 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1420 static struct ofport *
1423 struct ofport_dpif *port = xmalloc(sizeof *port);
1428 port_dealloc(struct ofport *port_)
1430 struct ofport_dpif *port = ofport_dpif_cast(port_);
1435 port_construct(struct ofport *port_)
1437 struct ofport_dpif *port = ofport_dpif_cast(port_);
1438 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1439 const struct netdev *netdev = port->up.netdev;
1440 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1441 struct dpif_port dpif_port;
1444 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1445 port->bundle = NULL;
1448 port->may_enable = true;
1449 port->stp_port = NULL;
1450 port->stp_state = STP_DISABLED;
1451 port->is_tunnel = false;
1455 port->realdev_ofp_port = 0;
1456 port->vlandev_vid = 0;
1457 port->carrier_seq = netdev_get_carrier_resets(netdev);
1458 port->is_layer3 = netdev_vport_is_layer3(netdev);
1460 if (netdev_vport_is_patch(netdev)) {
1461 /* By bailing out here, we don't submit the port to the sFlow module
1462 * to be considered for counter polling export. This is correct
1463 * because the patch port represents an interface that sFlow considers
1464 * to be "internal" to the switch as a whole, and therefore not an
1465 * candidate for counter polling. */
1466 port->odp_port = ODPP_NONE;
1467 ofport_update_peer(port);
1471 error = dpif_port_query_by_name(ofproto->backer->dpif,
1472 netdev_vport_get_dpif_port(netdev, namebuf,
1479 port->odp_port = dpif_port.port_no;
1481 if (netdev_get_tunnel_config(netdev)) {
1482 tnl_port_add(port, port->up.netdev, port->odp_port);
1483 port->is_tunnel = true;
1485 /* Sanity-check that a mapping doesn't already exist. This
1486 * shouldn't happen for non-tunnel ports. */
1487 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1488 VLOG_ERR("port %s already has an OpenFlow port number",
1490 dpif_port_destroy(&dpif_port);
1494 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1495 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1496 hash_odp_port(port->odp_port));
1497 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1499 dpif_port_destroy(&dpif_port);
1501 if (ofproto->sflow) {
1502 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1509 port_destruct(struct ofport *port_)
1511 struct ofport_dpif *port = ofport_dpif_cast(port_);
1512 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1513 const char *devname = netdev_get_name(port->up.netdev);
1514 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1515 const char *dp_port_name;
1517 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1518 ovs_rwlock_wrlock(&xlate_rwlock);
1519 xlate_ofport_remove(port);
1520 ovs_rwlock_unlock(&xlate_rwlock);
1522 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1524 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1525 /* The underlying device is still there, so delete it. This
1526 * happens when the ofproto is being destroyed, since the caller
1527 * assumes that removal of attached ports will happen as part of
1529 if (!port->is_tunnel) {
1530 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1535 port->peer->peer = NULL;
1539 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1540 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1541 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1542 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1546 sset_find_and_delete(&ofproto->ports, devname);
1547 sset_find_and_delete(&ofproto->ghost_ports, devname);
1548 bundle_remove(port_);
1549 set_cfm(port_, NULL);
1550 set_bfd(port_, NULL);
1551 if (port->stp_port) {
1552 stp_port_disable(port->stp_port);
1554 if (ofproto->sflow) {
1555 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1562 port_modified(struct ofport *port_)
1564 struct ofport_dpif *port = ofport_dpif_cast(port_);
1566 if (port->bundle && port->bundle->bond) {
1567 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1571 cfm_set_netdev(port->cfm, port->up.netdev);
1575 bfd_set_netdev(port->bfd, port->up.netdev);
1578 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1579 port->up.pp.hw_addr);
1581 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1583 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1587 ofport_update_peer(port);
1591 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1593 struct ofport_dpif *port = ofport_dpif_cast(port_);
1594 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1595 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1597 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1598 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1599 OFPUTIL_PC_NO_PACKET_IN)) {
1600 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1602 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1603 bundle_update(port->bundle);
1609 set_sflow(struct ofproto *ofproto_,
1610 const struct ofproto_sflow_options *sflow_options)
1612 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1613 struct dpif_sflow *ds = ofproto->sflow;
1615 if (sflow_options) {
1617 struct ofport_dpif *ofport;
1619 ds = ofproto->sflow = dpif_sflow_create();
1620 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1621 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1623 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1625 dpif_sflow_set_options(ds, sflow_options);
1628 dpif_sflow_unref(ds);
1629 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1630 ofproto->sflow = NULL;
1638 struct ofproto *ofproto_,
1639 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1640 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1641 size_t n_flow_exporters_options)
1643 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1644 struct dpif_ipfix *di = ofproto->ipfix;
1645 bool has_options = bridge_exporter_options || flow_exporters_options;
1647 if (has_options && !di) {
1648 di = ofproto->ipfix = dpif_ipfix_create();
1652 /* Call set_options in any case to cleanly flush the flow
1653 * caches in the last exporters that are to be destroyed. */
1654 dpif_ipfix_set_options(
1655 di, bridge_exporter_options, flow_exporters_options,
1656 n_flow_exporters_options);
1659 dpif_ipfix_unref(di);
1660 ofproto->ipfix = NULL;
1668 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1670 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1675 struct ofproto_dpif *ofproto;
1677 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1678 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1679 ofport->cfm = cfm_create(ofport->up.netdev);
1682 if (cfm_configure(ofport->cfm, s)) {
1689 cfm_unref(ofport->cfm);
1692 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1693 ofport->up.pp.hw_addr);
1698 get_cfm_status(const struct ofport *ofport_,
1699 struct ofproto_cfm_status *status)
1701 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1704 status->faults = cfm_get_fault(ofport->cfm);
1705 status->flap_count = cfm_get_flap_count(ofport->cfm);
1706 status->remote_opstate = cfm_get_opup(ofport->cfm);
1707 status->health = cfm_get_health(ofport->cfm);
1708 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1716 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1718 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1719 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1723 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1724 cfg, ofport->up.netdev);
1725 if (ofport->bfd != old) {
1726 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1728 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1729 ofport->up.pp.hw_addr);
1734 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1736 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1739 bfd_get_status(ofport->bfd, smap);
1746 /* Spanning Tree. */
1749 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1751 struct ofproto_dpif *ofproto = ofproto_;
1752 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1753 struct ofport_dpif *ofport;
1755 ofport = stp_port_get_aux(sp);
1757 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1758 ofproto->up.name, port_num);
1760 struct eth_header *eth = pkt->l2;
1762 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1763 if (eth_addr_is_zero(eth->eth_src)) {
1764 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1765 "with unknown MAC", ofproto->up.name, port_num);
1767 ofproto_dpif_send_packet(ofport, pkt);
1773 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1775 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1777 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1779 /* Only revalidate flows if the configuration changed. */
1780 if (!s != !ofproto->stp) {
1781 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1785 if (!ofproto->stp) {
1786 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1787 send_bpdu_cb, ofproto);
1788 ofproto->stp_last_tick = time_msec();
1791 stp_set_bridge_id(ofproto->stp, s->system_id);
1792 stp_set_bridge_priority(ofproto->stp, s->priority);
1793 stp_set_hello_time(ofproto->stp, s->hello_time);
1794 stp_set_max_age(ofproto->stp, s->max_age);
1795 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1797 struct ofport *ofport;
1799 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1800 set_stp_port(ofport, NULL);
1803 stp_unref(ofproto->stp);
1804 ofproto->stp = NULL;
1811 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1813 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1817 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1818 s->designated_root = stp_get_designated_root(ofproto->stp);
1819 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1828 update_stp_port_state(struct ofport_dpif *ofport)
1830 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1831 enum stp_state state;
1833 /* Figure out new state. */
1834 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1838 if (ofport->stp_state != state) {
1839 enum ofputil_port_state of_state;
1842 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1843 netdev_get_name(ofport->up.netdev),
1844 stp_state_name(ofport->stp_state),
1845 stp_state_name(state));
1846 if (stp_learn_in_state(ofport->stp_state)
1847 != stp_learn_in_state(state)) {
1848 /* xxx Learning action flows should also be flushed. */
1849 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1850 mac_learning_flush(ofproto->ml);
1851 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1853 fwd_change = stp_forward_in_state(ofport->stp_state)
1854 != stp_forward_in_state(state);
1856 ofproto->backer->need_revalidate = REV_STP;
1857 ofport->stp_state = state;
1858 ofport->stp_state_entered = time_msec();
1860 if (fwd_change && ofport->bundle) {
1861 bundle_update(ofport->bundle);
1864 /* Update the STP state bits in the OpenFlow port description. */
1865 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1866 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1867 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1868 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1869 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1871 ofproto_port_set_state(&ofport->up, of_state);
1875 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1876 * caller is responsible for assigning STP port numbers and ensuring
1877 * there are no duplicates. */
1879 set_stp_port(struct ofport *ofport_,
1880 const struct ofproto_port_stp_settings *s)
1882 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1883 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1884 struct stp_port *sp = ofport->stp_port;
1886 if (!s || !s->enable) {
1888 ofport->stp_port = NULL;
1889 stp_port_disable(sp);
1890 update_stp_port_state(ofport);
1893 } else if (sp && stp_port_no(sp) != s->port_num
1894 && ofport == stp_port_get_aux(sp)) {
1895 /* The port-id changed, so disable the old one if it's not
1896 * already in use by another port. */
1897 stp_port_disable(sp);
1900 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1901 stp_port_enable(sp);
1903 stp_port_set_aux(sp, ofport);
1904 stp_port_set_priority(sp, s->priority);
1905 stp_port_set_path_cost(sp, s->path_cost);
1907 update_stp_port_state(ofport);
1913 get_stp_port_status(struct ofport *ofport_,
1914 struct ofproto_port_stp_status *s)
1916 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1917 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1918 struct stp_port *sp = ofport->stp_port;
1920 if (!ofproto->stp || !sp) {
1926 s->port_id = stp_port_get_id(sp);
1927 s->state = stp_port_get_state(sp);
1928 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1929 s->role = stp_port_get_role(sp);
1935 get_stp_port_stats(struct ofport *ofport_,
1936 struct ofproto_port_stp_stats *s)
1938 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1939 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1940 struct stp_port *sp = ofport->stp_port;
1942 if (!ofproto->stp || !sp) {
1948 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1954 stp_run(struct ofproto_dpif *ofproto)
1957 long long int now = time_msec();
1958 long long int elapsed = now - ofproto->stp_last_tick;
1959 struct stp_port *sp;
1962 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1963 ofproto->stp_last_tick = now;
1965 while (stp_get_changed_port(ofproto->stp, &sp)) {
1966 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1969 update_stp_port_state(ofport);
1973 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1974 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1975 mac_learning_flush(ofproto->ml);
1976 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1982 stp_wait(struct ofproto_dpif *ofproto)
1985 poll_timer_wait(1000);
1990 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
1993 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1994 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1996 if (ofport->n_qdscp != n_qdscp
1997 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
1998 n_qdscp * sizeof *qdscp))) {
1999 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2000 free(ofport->qdscp);
2001 ofport->qdscp = n_qdscp
2002 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2004 ofport->n_qdscp = n_qdscp;
2012 /* Expires all MAC learning entries associated with 'bundle' and forces its
2013 * ofproto to revalidate every flow.
2015 * Normally MAC learning entries are removed only from the ofproto associated
2016 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2017 * are removed from every ofproto. When patch ports and SLB bonds are in use
2018 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2019 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2020 * with the host from which it migrated. */
2022 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2024 struct ofproto_dpif *ofproto = bundle->ofproto;
2025 struct mac_learning *ml = ofproto->ml;
2026 struct mac_entry *mac, *next_mac;
2028 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2029 ovs_rwlock_wrlock(&ml->rwlock);
2030 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2031 if (mac->port.p == bundle) {
2033 struct ofproto_dpif *o;
2035 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2037 struct mac_entry *e;
2039 ovs_rwlock_wrlock(&o->ml->rwlock);
2040 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2042 mac_learning_expire(o->ml, e);
2044 ovs_rwlock_unlock(&o->ml->rwlock);
2049 mac_learning_expire(ml, mac);
2052 ovs_rwlock_unlock(&ml->rwlock);
2055 static struct ofbundle *
2056 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2058 struct ofbundle *bundle;
2060 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2061 &ofproto->bundles) {
2062 if (bundle->aux == aux) {
2070 bundle_update(struct ofbundle *bundle)
2072 struct ofport_dpif *port;
2074 bundle->floodable = true;
2075 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2076 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2078 || !stp_forward_in_state(port->stp_state)) {
2079 bundle->floodable = false;
2086 bundle_del_port(struct ofport_dpif *port)
2088 struct ofbundle *bundle = port->bundle;
2090 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2092 list_remove(&port->bundle_node);
2093 port->bundle = NULL;
2096 lacp_slave_unregister(bundle->lacp, port);
2099 bond_slave_unregister(bundle->bond, port);
2102 bundle_update(bundle);
2106 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2107 struct lacp_slave_settings *lacp)
2109 struct ofport_dpif *port;
2111 port = get_ofp_port(bundle->ofproto, ofp_port);
2116 if (port->bundle != bundle) {
2117 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2119 bundle_remove(&port->up);
2122 port->bundle = bundle;
2123 list_push_back(&bundle->ports, &port->bundle_node);
2124 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2126 || !stp_forward_in_state(port->stp_state)) {
2127 bundle->floodable = false;
2131 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2132 lacp_slave_register(bundle->lacp, port, lacp);
2139 bundle_destroy(struct ofbundle *bundle)
2141 struct ofproto_dpif *ofproto;
2142 struct ofport_dpif *port, *next_port;
2148 ofproto = bundle->ofproto;
2149 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2151 ovs_rwlock_wrlock(&xlate_rwlock);
2152 xlate_bundle_remove(bundle);
2153 ovs_rwlock_unlock(&xlate_rwlock);
2155 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2156 bundle_del_port(port);
2159 bundle_flush_macs(bundle, true);
2160 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2162 free(bundle->trunks);
2163 lacp_unref(bundle->lacp);
2164 bond_unref(bundle->bond);
2169 bundle_set(struct ofproto *ofproto_, void *aux,
2170 const struct ofproto_bundle_settings *s)
2172 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2173 bool need_flush = false;
2174 struct ofport_dpif *port;
2175 struct ofbundle *bundle;
2176 unsigned long *trunks;
2182 bundle_destroy(bundle_lookup(ofproto, aux));
2186 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2187 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2189 bundle = bundle_lookup(ofproto, aux);
2191 bundle = xmalloc(sizeof *bundle);
2193 bundle->ofproto = ofproto;
2194 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2195 hash_pointer(aux, 0));
2197 bundle->name = NULL;
2199 list_init(&bundle->ports);
2200 bundle->vlan_mode = PORT_VLAN_TRUNK;
2202 bundle->trunks = NULL;
2203 bundle->use_priority_tags = s->use_priority_tags;
2204 bundle->lacp = NULL;
2205 bundle->bond = NULL;
2207 bundle->floodable = true;
2208 mbridge_register_bundle(ofproto->mbridge, bundle);
2211 if (!bundle->name || strcmp(s->name, bundle->name)) {
2213 bundle->name = xstrdup(s->name);
2218 ofproto->lacp_enabled = true;
2219 if (!bundle->lacp) {
2220 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2221 bundle->lacp = lacp_create();
2223 lacp_configure(bundle->lacp, s->lacp);
2225 lacp_unref(bundle->lacp);
2226 bundle->lacp = NULL;
2229 /* Update set of ports. */
2231 for (i = 0; i < s->n_slaves; i++) {
2232 if (!bundle_add_port(bundle, s->slaves[i],
2233 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2237 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2238 struct ofport_dpif *next_port;
2240 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2241 for (i = 0; i < s->n_slaves; i++) {
2242 if (s->slaves[i] == port->up.ofp_port) {
2247 bundle_del_port(port);
2251 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2253 if (list_is_empty(&bundle->ports)) {
2254 bundle_destroy(bundle);
2258 /* Set VLAN tagging mode */
2259 if (s->vlan_mode != bundle->vlan_mode
2260 || s->use_priority_tags != bundle->use_priority_tags) {
2261 bundle->vlan_mode = s->vlan_mode;
2262 bundle->use_priority_tags = s->use_priority_tags;
2267 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2268 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2270 if (vlan != bundle->vlan) {
2271 bundle->vlan = vlan;
2275 /* Get trunked VLANs. */
2276 switch (s->vlan_mode) {
2277 case PORT_VLAN_ACCESS:
2281 case PORT_VLAN_TRUNK:
2282 trunks = CONST_CAST(unsigned long *, s->trunks);
2285 case PORT_VLAN_NATIVE_UNTAGGED:
2286 case PORT_VLAN_NATIVE_TAGGED:
2287 if (vlan != 0 && (!s->trunks
2288 || !bitmap_is_set(s->trunks, vlan)
2289 || bitmap_is_set(s->trunks, 0))) {
2290 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2292 trunks = bitmap_clone(s->trunks, 4096);
2294 trunks = bitmap_allocate1(4096);
2296 bitmap_set1(trunks, vlan);
2297 bitmap_set0(trunks, 0);
2299 trunks = CONST_CAST(unsigned long *, s->trunks);
2306 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2307 free(bundle->trunks);
2308 if (trunks == s->trunks) {
2309 bundle->trunks = vlan_bitmap_clone(trunks);
2311 bundle->trunks = trunks;
2316 if (trunks != s->trunks) {
2321 if (!list_is_short(&bundle->ports)) {
2322 bundle->ofproto->has_bonded_bundles = true;
2324 if (bond_reconfigure(bundle->bond, s->bond)) {
2325 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2328 bundle->bond = bond_create(s->bond);
2329 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2332 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2333 bond_slave_register(bundle->bond, port, port->up.netdev);
2336 bond_unref(bundle->bond);
2337 bundle->bond = NULL;
2340 /* If we changed something that would affect MAC learning, un-learn
2341 * everything on this port and force flow revalidation. */
2343 bundle_flush_macs(bundle, false);
2350 bundle_remove(struct ofport *port_)
2352 struct ofport_dpif *port = ofport_dpif_cast(port_);
2353 struct ofbundle *bundle = port->bundle;
2356 bundle_del_port(port);
2357 if (list_is_empty(&bundle->ports)) {
2358 bundle_destroy(bundle);
2359 } else if (list_is_short(&bundle->ports)) {
2360 bond_unref(bundle->bond);
2361 bundle->bond = NULL;
2367 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2369 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2370 struct ofport_dpif *port = port_;
2371 uint8_t ea[ETH_ADDR_LEN];
2374 error = netdev_get_etheraddr(port->up.netdev, ea);
2376 struct ofpbuf packet;
2379 ofpbuf_init(&packet, 0);
2380 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2382 memcpy(packet_pdu, pdu, pdu_size);
2384 ofproto_dpif_send_packet(port, &packet);
2385 ofpbuf_uninit(&packet);
2387 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2388 "%s (%s)", port->bundle->name,
2389 netdev_get_name(port->up.netdev), ovs_strerror(error));
2394 bundle_send_learning_packets(struct ofbundle *bundle)
2396 struct ofproto_dpif *ofproto = bundle->ofproto;
2397 struct ofpbuf *learning_packet;
2398 int error, n_packets, n_errors;
2399 struct mac_entry *e;
2400 struct list packets;
2402 list_init(&packets);
2403 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2404 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2405 if (e->port.p != bundle) {
2408 learning_packet = bond_compose_learning_packet(bundle->bond,
2411 learning_packet->private_p = port_void;
2412 list_push_back(&packets, &learning_packet->list_node);
2415 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2417 error = n_packets = n_errors = 0;
2418 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2421 ret = ofproto_dpif_send_packet(learning_packet->private_p, learning_packet);
2428 ofpbuf_list_delete(&packets);
2431 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2432 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2433 "packets, last error was: %s",
2434 bundle->name, n_errors, n_packets, ovs_strerror(error));
2436 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2437 bundle->name, n_packets);
2442 bundle_run(struct ofbundle *bundle)
2445 lacp_run(bundle->lacp, send_pdu_cb);
2448 struct ofport_dpif *port;
2450 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2451 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2454 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2455 bundle->ofproto->backer->need_revalidate = REV_BOND;
2458 if (bond_should_send_learning_packets(bundle->bond)) {
2459 bundle_send_learning_packets(bundle);
2465 bundle_wait(struct ofbundle *bundle)
2468 lacp_wait(bundle->lacp);
2471 bond_wait(bundle->bond);
2478 mirror_set__(struct ofproto *ofproto_, void *aux,
2479 const struct ofproto_mirror_settings *s)
2481 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2482 struct ofbundle **srcs, **dsts;
2487 mirror_destroy(ofproto->mbridge, aux);
2491 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2492 dsts = xmalloc(s->n_dsts * sizeof *dsts);
2494 for (i = 0; i < s->n_srcs; i++) {
2495 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2498 for (i = 0; i < s->n_dsts; i++) {
2499 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2502 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2503 s->n_dsts, s->src_vlans,
2504 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2511 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2512 uint64_t *packets, uint64_t *bytes)
2514 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2519 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2521 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2522 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2523 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2524 mac_learning_flush(ofproto->ml);
2526 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2531 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2533 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2534 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2535 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2539 forward_bpdu_changed(struct ofproto *ofproto_)
2541 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2542 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2546 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2549 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2550 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2551 mac_learning_set_idle_time(ofproto->ml, idle_time);
2552 mac_learning_set_max_entries(ofproto->ml, max_entries);
2553 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2558 static struct ofport_dpif *
2559 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2561 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2562 return ofport ? ofport_dpif_cast(ofport) : NULL;
2566 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2567 struct ofproto_port *ofproto_port,
2568 struct dpif_port *dpif_port)
2570 ofproto_port->name = dpif_port->name;
2571 ofproto_port->type = dpif_port->type;
2572 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2576 ofport_update_peer(struct ofport_dpif *ofport)
2578 const struct ofproto_dpif *ofproto;
2579 struct dpif_backer *backer;
2582 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2586 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2587 backer->need_revalidate = REV_RECONFIGURE;
2590 ofport->peer->peer = NULL;
2591 ofport->peer = NULL;
2594 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2599 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2600 struct ofport *peer_ofport;
2601 struct ofport_dpif *peer;
2604 if (ofproto->backer != backer) {
2608 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2613 peer = ofport_dpif_cast(peer_ofport);
2614 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2615 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2617 ofport->peer = peer;
2618 ofport->peer->peer = ofport;
2628 port_run(struct ofport_dpif *ofport)
2630 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2631 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2632 bool enable = netdev_get_carrier(ofport->up.netdev);
2633 bool cfm_enable = false;
2634 bool bfd_enable = false;
2636 ofport->carrier_seq = carrier_seq;
2639 int cfm_opup = cfm_get_opup(ofport->cfm);
2641 cfm_enable = !cfm_get_fault(ofport->cfm);
2643 if (cfm_opup >= 0) {
2644 cfm_enable = cfm_enable && cfm_opup;
2649 bfd_enable = bfd_forwarding(ofport->bfd);
2652 if (ofport->bfd || ofport->cfm) {
2653 enable = enable && (cfm_enable || bfd_enable);
2656 if (ofport->bundle) {
2657 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2658 if (carrier_changed) {
2659 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2663 if (ofport->may_enable != enable) {
2664 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2665 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2668 ofport->may_enable = enable;
2672 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2673 struct ofproto_port *ofproto_port)
2675 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2676 struct dpif_port dpif_port;
2679 if (sset_contains(&ofproto->ghost_ports, devname)) {
2680 const char *type = netdev_get_type_from_name(devname);
2682 /* We may be called before ofproto->up.port_by_name is populated with
2683 * the appropriate ofport. For this reason, we must get the name and
2684 * type from the netdev layer directly. */
2686 const struct ofport *ofport;
2688 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2689 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2690 ofproto_port->name = xstrdup(devname);
2691 ofproto_port->type = xstrdup(type);
2697 if (!sset_contains(&ofproto->ports, devname)) {
2700 error = dpif_port_query_by_name(ofproto->backer->dpif,
2701 devname, &dpif_port);
2703 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2709 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2711 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2712 const char *devname = netdev_get_name(netdev);
2713 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2714 const char *dp_port_name;
2716 if (netdev_vport_is_patch(netdev)) {
2717 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2721 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2722 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2723 odp_port_t port_no = ODPP_NONE;
2726 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2730 if (netdev_get_tunnel_config(netdev)) {
2731 simap_put(&ofproto->backer->tnl_backers,
2732 dp_port_name, odp_to_u32(port_no));
2736 if (netdev_get_tunnel_config(netdev)) {
2737 sset_add(&ofproto->ghost_ports, devname);
2739 sset_add(&ofproto->ports, devname);
2745 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2747 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2748 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2755 sset_find_and_delete(&ofproto->ghost_ports,
2756 netdev_get_name(ofport->up.netdev));
2757 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2758 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2759 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2761 /* The caller is going to close ofport->up.netdev. If this is a
2762 * bonded port, then the bond is using that netdev, so remove it
2763 * from the bond. The client will need to reconfigure everything
2764 * after deleting ports, so then the slave will get re-added. */
2765 bundle_remove(&ofport->up);
2772 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2774 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2777 error = netdev_get_stats(ofport->up.netdev, stats);
2779 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2780 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2782 ovs_mutex_lock(&ofproto->stats_mutex);
2783 /* ofproto->stats.tx_packets represents packets that we created
2784 * internally and sent to some port (e.g. packets sent with
2785 * ofproto_dpif_send_packet()). Account for them as if they had
2786 * come from OFPP_LOCAL and got forwarded. */
2788 if (stats->rx_packets != UINT64_MAX) {
2789 stats->rx_packets += ofproto->stats.tx_packets;
2792 if (stats->rx_bytes != UINT64_MAX) {
2793 stats->rx_bytes += ofproto->stats.tx_bytes;
2796 /* ofproto->stats.rx_packets represents packets that were received on
2797 * some port and we processed internally and dropped (e.g. STP).
2798 * Account for them as if they had been forwarded to OFPP_LOCAL. */
2800 if (stats->tx_packets != UINT64_MAX) {
2801 stats->tx_packets += ofproto->stats.rx_packets;
2804 if (stats->tx_bytes != UINT64_MAX) {
2805 stats->tx_bytes += ofproto->stats.rx_bytes;
2807 ovs_mutex_unlock(&ofproto->stats_mutex);
2813 struct port_dump_state {
2818 struct ofproto_port port;
2823 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
2825 *statep = xzalloc(sizeof(struct port_dump_state));
2830 port_dump_next(const struct ofproto *ofproto_, void *state_,
2831 struct ofproto_port *port)
2833 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2834 struct port_dump_state *state = state_;
2835 const struct sset *sset;
2836 struct sset_node *node;
2838 if (state->has_port) {
2839 ofproto_port_destroy(&state->port);
2840 state->has_port = false;
2842 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
2843 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
2846 error = port_query_by_name(ofproto_, node->name, &state->port);
2848 *port = state->port;
2849 state->has_port = true;
2851 } else if (error != ENODEV) {
2856 if (!state->ghost) {
2857 state->ghost = true;
2860 return port_dump_next(ofproto_, state_, port);
2867 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2869 struct port_dump_state *state = state_;
2871 if (state->has_port) {
2872 ofproto_port_destroy(&state->port);
2879 port_poll(const struct ofproto *ofproto_, char **devnamep)
2881 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2883 if (ofproto->port_poll_errno) {
2884 int error = ofproto->port_poll_errno;
2885 ofproto->port_poll_errno = 0;
2889 if (sset_is_empty(&ofproto->port_poll_set)) {
2893 *devnamep = sset_pop(&ofproto->port_poll_set);
2898 port_poll_wait(const struct ofproto *ofproto_)
2900 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2901 dpif_port_poll_wait(ofproto->backer->dpif);
2905 port_is_lacp_current(const struct ofport *ofport_)
2907 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2908 return (ofport->bundle && ofport->bundle->lacp
2909 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2913 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2914 * then delete it entirely. */
2916 rule_expire(struct rule_dpif *rule)
2917 OVS_REQUIRES(ofproto_mutex)
2919 uint16_t hard_timeout, idle_timeout;
2920 long long int now = time_msec();
2923 ovs_assert(!rule->up.pending);
2925 hard_timeout = rule->up.hard_timeout;
2926 idle_timeout = rule->up.idle_timeout;
2928 /* Has 'rule' expired? */
2930 long long int modified;
2932 ovs_mutex_lock(&rule->up.mutex);
2933 modified = rule->up.modified;
2934 ovs_mutex_unlock(&rule->up.mutex);
2936 if (now > modified + hard_timeout * 1000) {
2937 reason = OFPRR_HARD_TIMEOUT;
2941 if (reason < 0 && idle_timeout) {
2944 ovs_mutex_lock(&rule->stats_mutex);
2945 used = rule->stats.used;
2946 ovs_mutex_unlock(&rule->stats_mutex);
2948 if (now > used + idle_timeout * 1000) {
2949 reason = OFPRR_IDLE_TIMEOUT;
2954 COVERAGE_INC(ofproto_dpif_expired);
2955 ofproto_rule_expire(&rule->up, reason);
2959 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
2960 * 'flow' must reflect the data in 'packet'. */
2962 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
2963 const struct flow *flow,
2964 struct rule_dpif *rule,
2965 const struct ofpact *ofpacts, size_t ofpacts_len,
2966 struct ofpbuf *packet)
2968 struct dpif_flow_stats stats;
2969 struct xlate_out xout;
2970 struct xlate_in xin;
2972 struct dpif_execute execute;
2975 ovs_assert((rule != NULL) != (ofpacts != NULL));
2977 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
2979 rule_dpif_credit_stats(rule, &stats);
2982 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
2983 xin.ofpacts = ofpacts;
2984 xin.ofpacts_len = ofpacts_len;
2985 xin.resubmit_stats = &stats;
2986 xlate_actions(&xin, &xout);
2988 in_port = flow->in_port.ofp_port;
2989 if (in_port == OFPP_NONE) {
2990 in_port = OFPP_LOCAL;
2992 execute.actions = xout.odp_actions.data;
2993 execute.actions_len = xout.odp_actions.size;
2994 execute.packet = packet;
2995 execute.md.tunnel = flow->tunnel;
2996 execute.md.skb_priority = flow->skb_priority;
2997 execute.md.pkt_mark = flow->pkt_mark;
2998 execute.md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
2999 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3001 error = dpif_execute(ofproto->backer->dpif, &execute);
3003 xlate_out_uninit(&xout);
3009 rule_dpif_credit_stats(struct rule_dpif *rule,
3010 const struct dpif_flow_stats *stats)
3012 ovs_mutex_lock(&rule->stats_mutex);
3013 rule->stats.n_packets += stats->n_packets;
3014 rule->stats.n_bytes += stats->n_bytes;
3015 rule->stats.used = MAX(rule->stats.used, stats->used);
3016 ovs_mutex_unlock(&rule->stats_mutex);
3020 rule_dpif_is_fail_open(const struct rule_dpif *rule)
3022 return is_fail_open_rule(&rule->up);
3026 rule_dpif_is_table_miss(const struct rule_dpif *rule)
3028 return rule_is_table_miss(&rule->up);
3032 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3033 OVS_REQUIRES(rule->up.mutex)
3035 return rule->up.flow_cookie;
3039 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3040 uint16_t hard_timeout)
3042 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3045 /* Returns 'rule''s actions. The caller owns a reference on the returned
3046 * actions and must eventually release it (with rule_actions_unref()) to avoid
3048 struct rule_actions *
3049 rule_dpif_get_actions(const struct rule_dpif *rule)
3051 return rule_get_actions(&rule->up);
3054 /* Lookup 'flow' in 'ofproto''s classifier. If 'wc' is non-null, sets
3055 * the fields that were relevant as part of the lookup. */
3057 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3058 struct flow_wildcards *wc, struct rule_dpif **rule)
3060 struct ofport_dpif *port;
3062 if (rule_dpif_lookup_in_table(ofproto, flow, wc, 0, rule)) {
3065 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
3067 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3068 flow->in_port.ofp_port);
3071 choose_miss_rule(port ? port->up.pp.config : 0, ofproto->miss_rule,
3072 ofproto->no_packet_in_rule, rule);
3076 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto,
3077 const struct flow *flow, struct flow_wildcards *wc,
3078 uint8_t table_id, struct rule_dpif **rule)
3080 const struct cls_rule *cls_rule;
3081 struct classifier *cls;
3085 if (table_id >= N_TABLES) {
3090 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3091 if (is_ip_any(flow)) {
3092 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3096 cls = &ofproto->up.tables[table_id].cls;
3097 fat_rwlock_rdlock(&cls->rwlock);
3098 frag = (flow->nw_frag & FLOW_NW_FRAG_ANY) != 0;
3099 if (frag && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3100 /* We must pretend that transport ports are unavailable. */
3101 struct flow ofpc_normal_flow = *flow;
3102 ofpc_normal_flow.tp_src = htons(0);
3103 ofpc_normal_flow.tp_dst = htons(0);
3104 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3105 } else if (frag && ofproto->up.frag_handling == OFPC_FRAG_DROP) {
3106 cls_rule = &ofproto->drop_frags_rule->up.cr;
3107 /* Frag mask in wc already set above. */
3109 cls_rule = classifier_lookup(cls, flow, wc);
3112 *rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3113 rule_dpif_ref(*rule);
3114 fat_rwlock_unlock(&cls->rwlock);
3116 return *rule != NULL;
3119 /* Given a port configuration (specified as zero if there's no port), chooses
3120 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3121 * flow table miss. */
3123 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3124 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
3126 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
3127 rule_dpif_ref(*rule);
3131 rule_dpif_ref(struct rule_dpif *rule)
3134 ofproto_rule_ref(&rule->up);
3139 rule_dpif_unref(struct rule_dpif *rule)
3142 ofproto_rule_unref(&rule->up);
3147 complete_operation(struct rule_dpif *rule)
3148 OVS_REQUIRES(ofproto_mutex)
3150 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3152 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3153 ofoperation_complete(rule->up.pending, 0);
3156 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3158 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3161 static struct rule *
3164 struct rule_dpif *rule = xmalloc(sizeof *rule);
3169 rule_dealloc(struct rule *rule_)
3171 struct rule_dpif *rule = rule_dpif_cast(rule_);
3176 rule_construct(struct rule *rule_)
3177 OVS_NO_THREAD_SAFETY_ANALYSIS
3179 struct rule_dpif *rule = rule_dpif_cast(rule_);
3180 ovs_mutex_init_adaptive(&rule->stats_mutex);
3181 rule->stats.n_packets = 0;
3182 rule->stats.n_bytes = 0;
3183 rule->stats.used = rule->up.modified;
3188 rule_insert(struct rule *rule_)
3189 OVS_REQUIRES(ofproto_mutex)
3191 struct rule_dpif *rule = rule_dpif_cast(rule_);
3192 complete_operation(rule);
3196 rule_delete(struct rule *rule_)
3197 OVS_REQUIRES(ofproto_mutex)
3199 struct rule_dpif *rule = rule_dpif_cast(rule_);
3200 complete_operation(rule);
3204 rule_destruct(struct rule *rule_)
3206 struct rule_dpif *rule = rule_dpif_cast(rule_);
3207 ovs_mutex_destroy(&rule->stats_mutex);
3211 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3212 long long int *used)
3214 struct rule_dpif *rule = rule_dpif_cast(rule_);
3216 ovs_mutex_lock(&rule->stats_mutex);
3217 *packets = rule->stats.n_packets;
3218 *bytes = rule->stats.n_bytes;
3219 *used = rule->stats.used;
3220 ovs_mutex_unlock(&rule->stats_mutex);
3224 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3225 struct ofpbuf *packet)
3227 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3229 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3233 rule_execute(struct rule *rule, const struct flow *flow,
3234 struct ofpbuf *packet)
3236 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3237 ofpbuf_delete(packet);
3242 rule_modify_actions(struct rule *rule_, bool reset_counters)
3243 OVS_REQUIRES(ofproto_mutex)
3245 struct rule_dpif *rule = rule_dpif_cast(rule_);
3247 if (reset_counters) {
3248 ovs_mutex_lock(&rule->stats_mutex);
3249 rule->stats.n_packets = 0;
3250 rule->stats.n_bytes = 0;
3251 ovs_mutex_unlock(&rule->stats_mutex);
3254 complete_operation(rule);
3257 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3259 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3262 static struct ofgroup *
3265 struct group_dpif *group = xzalloc(sizeof *group);
3270 group_dealloc(struct ofgroup *group_)
3272 struct group_dpif *group = group_dpif_cast(group_);
3277 group_construct_stats(struct group_dpif *group)
3278 OVS_REQUIRES(group->stats_mutex)
3280 group->packet_count = 0;
3281 group->byte_count = 0;
3282 if (!group->bucket_stats) {
3283 group->bucket_stats = xcalloc(group->up.n_buckets,
3284 sizeof *group->bucket_stats);
3286 memset(group->bucket_stats, 0, group->up.n_buckets *
3287 sizeof *group->bucket_stats);
3292 group_construct(struct ofgroup *group_)
3294 struct group_dpif *group = group_dpif_cast(group_);
3295 const struct ofputil_bucket *bucket;
3297 /* Prevent group chaining because our locking structure makes it hard to
3298 * implement deadlock-free. (See xlate_group_resource_check().) */
3299 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
3300 const struct ofpact *a;
3302 OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
3303 if (a->type == OFPACT_GROUP) {
3304 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
3309 ovs_mutex_init_adaptive(&group->stats_mutex);
3310 ovs_mutex_lock(&group->stats_mutex);
3311 group_construct_stats(group);
3312 ovs_mutex_unlock(&group->stats_mutex);
3317 group_destruct__(struct group_dpif *group)
3318 OVS_REQUIRES(group->stats_mutex)
3320 free(group->bucket_stats);
3321 group->bucket_stats = NULL;
3325 group_destruct(struct ofgroup *group_)
3327 struct group_dpif *group = group_dpif_cast(group_);
3328 ovs_mutex_lock(&group->stats_mutex);
3329 group_destruct__(group);
3330 ovs_mutex_unlock(&group->stats_mutex);
3331 ovs_mutex_destroy(&group->stats_mutex);
3335 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3337 struct group_dpif *group = group_dpif_cast(group_);
3338 struct group_dpif *victim = group_dpif_cast(victim_);
3340 ovs_mutex_lock(&group->stats_mutex);
3341 if (victim->up.n_buckets < group->up.n_buckets) {
3342 group_destruct__(group);
3344 group_construct_stats(group);
3345 ovs_mutex_unlock(&group->stats_mutex);
3351 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3353 struct group_dpif *group = group_dpif_cast(group_);
3355 ovs_mutex_lock(&group->stats_mutex);
3356 ogs->packet_count = group->packet_count;
3357 ogs->byte_count = group->byte_count;
3358 memcpy(ogs->bucket_stats, group->bucket_stats,
3359 group->up.n_buckets * sizeof *group->bucket_stats);
3360 ovs_mutex_unlock(&group->stats_mutex);
3366 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3367 struct group_dpif **group)
3368 OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3370 struct ofgroup *ofgroup;
3374 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3375 *group = found ? group_dpif_cast(ofgroup) : NULL;
3381 group_dpif_release(struct group_dpif *group)
3382 OVS_RELEASES(group->up.rwlock)
3384 ofproto_group_release(&group->up);
3388 group_dpif_get_buckets(const struct group_dpif *group,
3389 const struct list **buckets)
3391 *buckets = &group->up.buckets;
3394 enum ofp11_group_type
3395 group_dpif_get_type(const struct group_dpif *group)
3397 return group->up.type;
3400 /* Sends 'packet' out 'ofport'.
3401 * May modify 'packet'.
3402 * Returns 0 if successful, otherwise a positive errno value. */
3404 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3406 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3409 error = xlate_send_packet(ofport, packet);
3411 ovs_mutex_lock(&ofproto->stats_mutex);
3412 ofproto->stats.tx_packets++;
3413 ofproto->stats.tx_bytes += packet->size;
3414 ovs_mutex_unlock(&ofproto->stats_mutex);
3419 set_frag_handling(struct ofproto *ofproto_,
3420 enum ofp_config_flags frag_handling)
3422 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3423 if (frag_handling != OFPC_FRAG_REASM) {
3424 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3432 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3433 const struct flow *flow,
3434 const struct ofpact *ofpacts, size_t ofpacts_len)
3436 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3438 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3439 ofpacts_len, packet);
3446 set_netflow(struct ofproto *ofproto_,
3447 const struct netflow_options *netflow_options)
3449 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3451 if (netflow_options) {
3452 if (!ofproto->netflow) {
3453 ofproto->netflow = netflow_create();
3454 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3456 return netflow_set_options(ofproto->netflow, netflow_options);
3457 } else if (ofproto->netflow) {
3458 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3459 netflow_unref(ofproto->netflow);
3460 ofproto->netflow = NULL;
3467 get_netflow_ids(const struct ofproto *ofproto_,
3468 uint8_t *engine_type, uint8_t *engine_id)
3470 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3472 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3475 static struct ofproto_dpif *
3476 ofproto_dpif_lookup(const char *name)
3478 struct ofproto_dpif *ofproto;
3480 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3481 hash_string(name, 0), &all_ofproto_dpifs) {
3482 if (!strcmp(ofproto->up.name, name)) {
3490 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3491 const char *argv[], void *aux OVS_UNUSED)
3493 struct ofproto_dpif *ofproto;
3496 ofproto = ofproto_dpif_lookup(argv[1]);
3498 unixctl_command_reply_error(conn, "no such bridge");
3501 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3502 mac_learning_flush(ofproto->ml);
3503 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3505 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3506 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3507 mac_learning_flush(ofproto->ml);
3508 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3512 unixctl_command_reply(conn, "table successfully flushed");
3515 static struct ofport_dpif *
3516 ofbundle_get_a_port(const struct ofbundle *bundle)
3518 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3523 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3524 const char *argv[], void *aux OVS_UNUSED)
3526 struct ds ds = DS_EMPTY_INITIALIZER;
3527 const struct ofproto_dpif *ofproto;
3528 const struct mac_entry *e;
3530 ofproto = ofproto_dpif_lookup(argv[1]);
3532 unixctl_command_reply_error(conn, "no such bridge");
3536 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3537 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3538 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3539 struct ofbundle *bundle = e->port.p;
3540 char name[OFP_MAX_PORT_NAME_LEN];
3542 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3544 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
3545 name, e->vlan, ETH_ADDR_ARGS(e->mac),
3546 mac_entry_age(ofproto->ml, e));
3548 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3549 unixctl_command_reply(conn, ds_cstr(&ds));
3554 struct xlate_out xout;
3555 struct xlate_in xin;
3557 struct flow_wildcards wc;
3562 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
3564 struct rule_actions *actions;
3567 ds_put_char_multiple(result, '\t', level);
3569 ds_put_cstr(result, "No match\n");
3573 ovs_mutex_lock(&rule->up.mutex);
3574 cookie = rule->up.flow_cookie;
3575 ovs_mutex_unlock(&rule->up.mutex);
3577 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3578 rule ? rule->up.table_id : 0, ntohll(cookie));
3579 cls_rule_format(&rule->up.cr, result);
3580 ds_put_char(result, '\n');
3582 actions = rule_dpif_get_actions(rule);
3584 ds_put_char_multiple(result, '\t', level);
3585 ds_put_cstr(result, "OpenFlow actions=");
3586 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
3587 ds_put_char(result, '\n');
3589 rule_actions_unref(actions);
3593 trace_format_flow(struct ds *result, int level, const char *title,
3594 struct trace_ctx *trace)
3596 ds_put_char_multiple(result, '\t', level);
3597 ds_put_format(result, "%s: ", title);
3598 if (flow_equal(&trace->xin.flow, &trace->flow)) {
3599 ds_put_cstr(result, "unchanged");
3601 flow_format(result, &trace->xin.flow);
3602 trace->flow = trace->xin.flow;
3604 ds_put_char(result, '\n');
3608 trace_format_regs(struct ds *result, int level, const char *title,
3609 struct trace_ctx *trace)
3613 ds_put_char_multiple(result, '\t', level);
3614 ds_put_format(result, "%s:", title);
3615 for (i = 0; i < FLOW_N_REGS; i++) {
3616 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
3618 ds_put_char(result, '\n');
3622 trace_format_odp(struct ds *result, int level, const char *title,
3623 struct trace_ctx *trace)
3625 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
3627 ds_put_char_multiple(result, '\t', level);
3628 ds_put_format(result, "%s: ", title);
3629 format_odp_actions(result, odp_actions->data, odp_actions->size);
3630 ds_put_char(result, '\n');
3634 trace_format_megaflow(struct ds *result, int level, const char *title,
3635 struct trace_ctx *trace)
3639 ds_put_char_multiple(result, '\t', level);
3640 ds_put_format(result, "%s: ", title);
3641 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
3642 match_init(&match, &trace->flow, &trace->wc);
3643 match_format(&match, result, OFP_DEFAULT_PRIORITY);
3644 ds_put_char(result, '\n');
3648 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3650 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3651 struct ds *result = trace->result;
3653 ds_put_char(result, '\n');
3654 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3655 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3656 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
3657 trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
3658 trace_format_rule(result, recurse + 1, rule);
3662 trace_report(struct xlate_in *xin, const char *s, int recurse)
3664 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3665 struct ds *result = trace->result;
3667 ds_put_char_multiple(result, '\t', recurse);
3668 ds_put_cstr(result, s);
3669 ds_put_char(result, '\n');
3672 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
3673 * forms are supported:
3675 * - [dpname] odp_flow [-generate | packet]
3676 * - bridge br_flow [-generate | packet]
3678 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
3679 * returns a nonnull malloced error message. */
3680 static char * WARN_UNUSED_RESULT
3681 parse_flow_and_packet(int argc, const char *argv[],
3682 struct ofproto_dpif **ofprotop, struct flow *flow,
3683 struct ofpbuf **packetp)
3685 const struct dpif_backer *backer = NULL;
3686 const char *error = NULL;
3688 struct simap port_names = SIMAP_INITIALIZER(&port_names);
3689 struct ofpbuf *packet;
3690 struct ofpbuf odp_key;
3691 struct ofpbuf odp_mask;
3693 ofpbuf_init(&odp_key, 0);
3694 ofpbuf_init(&odp_mask, 0);
3696 /* Handle "-generate" or a hex string as the last argument. */
3697 if (!strcmp(argv[argc - 1], "-generate")) {
3698 packet = ofpbuf_new(0);
3701 error = eth_from_hex(argv[argc - 1], &packet);
3704 } else if (argc == 4) {
3705 /* The 3-argument form must end in "-generate' or a hex string. */
3711 /* odp_flow can have its in_port specified as a name instead of port no.
3712 * We do not yet know whether a given flow is a odp_flow or a br_flow.
3713 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3714 * we need to create a simap of name to port no. */
3716 const char *dp_type;
3717 if (!strncmp(argv[1], "ovs-", 4)) {
3718 dp_type = argv[1] + 4;
3722 backer = shash_find_data(&all_dpif_backers, dp_type);
3723 } else if (argc == 2) {
3724 struct shash_node *node;
3725 if (shash_count(&all_dpif_backers) == 1) {
3726 node = shash_first(&all_dpif_backers);
3727 backer = node->data;
3730 error = "Syntax error";
3733 if (backer && backer->dpif) {
3734 struct dpif_port dpif_port;
3735 struct dpif_port_dump port_dump;
3736 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
3737 simap_put(&port_names, dpif_port.name,
3738 odp_to_u32(dpif_port.port_no));
3742 /* Parse the flow and determine whether a datapath or
3743 * bridge is specified. If function odp_flow_key_from_string()
3744 * returns 0, the flow is a odp_flow. If function
3745 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
3746 if (!odp_flow_from_string(argv[argc - 1], &port_names,
3747 &odp_key, &odp_mask)) {
3749 error = "Cannot find the datapath";
3753 if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, flow,
3754 ofprotop, NULL, NULL, NULL, NULL)) {
3755 error = "Invalid datapath flow";
3759 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
3762 m_err = xasprintf("Bad flow syntax: %s", err);
3767 error = "Must specify bridge name";
3771 *ofprotop = ofproto_dpif_lookup(argv[1]);
3773 error = "Unknown bridge name";
3779 /* Generate a packet, if requested. */
3781 if (!packet->size) {
3782 flow_compose(packet, flow);
3784 union flow_in_port in_port = flow->in_port;
3785 struct pkt_metadata md;
3787 /* Use the metadata from the flow and the packet argument
3788 * to reconstruct the flow. */
3789 pkt_metadata_init(&md, NULL, flow->skb_priority,
3790 flow->pkt_mark, &in_port);
3792 flow_extract(packet, &md, flow);
3797 if (error && !m_err) {
3798 m_err = xstrdup(error);
3801 ofpbuf_delete(packet);
3805 ofpbuf_uninit(&odp_key);
3806 ofpbuf_uninit(&odp_mask);
3807 simap_destroy(&port_names);
3812 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
3813 void *aux OVS_UNUSED)
3815 struct ofproto_dpif *ofproto;
3816 struct ofpbuf *packet;
3820 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3825 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
3826 unixctl_command_reply(conn, ds_cstr(&result));
3827 ds_destroy(&result);
3828 ofpbuf_delete(packet);
3830 unixctl_command_reply_error(conn, error);
3836 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
3837 const char *argv[], void *aux OVS_UNUSED)
3839 enum ofputil_protocol usable_protocols;
3840 struct ofproto_dpif *ofproto;
3841 bool enforce_consistency;
3842 struct ofpbuf ofpacts;
3843 struct ofpbuf *packet;
3848 /* Three kinds of error return values! */
3854 ofpbuf_init(&ofpacts, 0);
3856 /* Parse actions. */
3857 error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
3859 unixctl_command_reply_error(conn, error);
3864 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
3865 * consistency between the flow and the actions. With -consistent, we
3866 * enforce consistency even for a flow supported in OpenFlow 1.0. */
3867 if (!strcmp(argv[1], "-consistent")) {
3868 enforce_consistency = true;
3872 enforce_consistency = false;
3875 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3877 unixctl_command_reply_error(conn, error);
3882 /* Do the same checks as handle_packet_out() in ofproto.c.
3884 * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
3885 * strictly correct because these actions aren't in any table, but it's OK
3886 * because it 'table_id' is used only to check goto_table instructions, but
3887 * packet-outs take a list of actions and therefore it can't include
3890 * We skip the "meter" check here because meter is an instruction, not an
3891 * action, and thus cannot appear in ofpacts. */
3892 in_port = ofp_to_u16(flow.in_port.ofp_port);
3893 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
3894 unixctl_command_reply_error(conn, "invalid in_port");
3897 if (enforce_consistency) {
3898 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
3899 u16_to_ofp(ofproto->up.max_ports),
3900 0, 0, usable_protocols);
3902 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
3903 u16_to_ofp(ofproto->up.max_ports), 0, 0,
3909 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
3910 unixctl_command_reply_error(conn, ds_cstr(&result));
3914 ofproto_trace(ofproto, &flow, packet, ofpacts.data, ofpacts.size, &result);
3915 unixctl_command_reply(conn, ds_cstr(&result));
3918 ds_destroy(&result);
3919 ofpbuf_delete(packet);
3920 ofpbuf_uninit(&ofpacts);
3923 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
3924 * description of the results to 'ds'.
3926 * The trace follows a packet with the specified 'flow' through the flow
3927 * table. 'packet' may be nonnull to trace an actual packet, with consequent
3928 * side effects (if it is nonnull then its flow must be 'flow').
3930 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
3931 * trace, otherwise the actions are determined by a flow table lookup. */
3933 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
3934 const struct ofpbuf *packet,
3935 const struct ofpact ofpacts[], size_t ofpacts_len,
3938 struct rule_dpif *rule;
3939 struct trace_ctx trace;
3941 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
3942 ds_put_cstr(ds, "Flow: ");
3943 flow_format(ds, flow);
3944 ds_put_char(ds, '\n');
3946 flow_wildcards_init_catchall(&trace.wc);
3950 rule_dpif_lookup(ofproto, flow, &trace.wc, &rule);
3952 trace_format_rule(ds, 0, rule);
3953 if (rule == ofproto->miss_rule) {
3954 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
3955 } else if (rule == ofproto->no_packet_in_rule) {
3956 ds_put_cstr(ds, "\nNo match, packets dropped because "
3957 "OFPPC_NO_PACKET_IN is set on in_port.\n");
3958 } else if (rule == ofproto->drop_frags_rule) {
3959 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
3960 "and the fragment handling mode is \"drop\".\n");
3964 if (rule || ofpacts) {
3967 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
3970 xlate_in_init(&trace.xin, ofproto, flow, rule, tcp_flags, packet);
3972 trace.xin.ofpacts = ofpacts;
3973 trace.xin.ofpacts_len = ofpacts_len;
3975 trace.xin.resubmit_hook = trace_resubmit;
3976 trace.xin.report_hook = trace_report;
3978 xlate_actions(&trace.xin, &trace.xout);
3980 ds_put_char(ds, '\n');
3981 trace_format_flow(ds, 0, "Final flow", &trace);
3982 trace_format_megaflow(ds, 0, "Megaflow", &trace);
3984 ds_put_cstr(ds, "Datapath actions: ");
3985 format_odp_actions(ds, trace.xout.odp_actions.data,
3986 trace.xout.odp_actions.size);
3988 if (trace.xout.slow) {
3989 enum slow_path_reason slow;
3991 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
3992 "slow path because it:");
3994 slow = trace.xout.slow;
3996 enum slow_path_reason bit = rightmost_1bit(slow);
3998 ds_put_format(ds, "\n\t- %s.",
3999 slow_path_reason_to_explanation(bit));
4005 xlate_out_uninit(&trace.xout);
4008 rule_dpif_unref(rule);
4011 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4012 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4013 * to destroy 'ofproto_shash' and free the returned value. */
4014 static const struct shash_node **
4015 get_ofprotos(struct shash *ofproto_shash)
4017 const struct ofproto_dpif *ofproto;
4019 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4020 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4021 shash_add_nocopy(ofproto_shash, name, ofproto);
4024 return shash_sort(ofproto_shash);
4028 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4029 const char *argv[] OVS_UNUSED,
4030 void *aux OVS_UNUSED)
4032 struct ds ds = DS_EMPTY_INITIALIZER;
4033 struct shash ofproto_shash;
4034 const struct shash_node **sorted_ofprotos;
4037 shash_init(&ofproto_shash);
4038 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4039 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4040 const struct shash_node *node = sorted_ofprotos[i];
4041 ds_put_format(&ds, "%s\n", node->name);
4044 shash_destroy(&ofproto_shash);
4045 free(sorted_ofprotos);
4047 unixctl_command_reply(conn, ds_cstr(&ds));
4052 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4054 const struct shash_node **ofprotos;
4055 struct dpif_dp_stats dp_stats;
4056 struct shash ofproto_shash;
4059 dpif_get_dp_stats(backer->dpif, &dp_stats);
4061 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4062 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4064 shash_init(&ofproto_shash);
4065 ofprotos = get_ofprotos(&ofproto_shash);
4066 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4067 struct ofproto_dpif *ofproto = ofprotos[i]->data;
4068 const struct shash_node **ports;
4071 if (ofproto->backer != backer) {
4075 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4077 ports = shash_sort(&ofproto->up.port_by_name);
4078 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4079 const struct shash_node *node = ports[j];
4080 struct ofport *ofport = node->data;
4082 odp_port_t odp_port;
4084 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4087 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4088 if (odp_port != ODPP_NONE) {
4089 ds_put_format(ds, "%"PRIu32":", odp_port);
4091 ds_put_cstr(ds, "none:");
4094 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
4097 if (!netdev_get_config(ofport->netdev, &config)) {
4098 const struct smap_node **nodes;
4101 nodes = smap_sort(&config);
4102 for (i = 0; i < smap_count(&config); i++) {
4103 const struct smap_node *node = nodes[i];
4104 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4105 node->key, node->value);
4109 smap_destroy(&config);
4111 ds_put_char(ds, ')');
4112 ds_put_char(ds, '\n');
4116 shash_destroy(&ofproto_shash);
4121 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4122 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4124 struct ds ds = DS_EMPTY_INITIALIZER;
4125 const struct shash_node **backers;
4128 backers = shash_sort(&all_dpif_backers);
4129 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4130 dpif_show_backer(backers[i]->data, &ds);
4134 unixctl_command_reply(conn, ds_cstr(&ds));
4139 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4140 const struct nlattr *key, size_t key_len)
4142 struct ofproto_dpif *ofp;
4145 xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &ofp,
4146 NULL, NULL, NULL, NULL);
4147 return ofp == ofproto;
4151 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4152 int argc OVS_UNUSED, const char *argv[],
4153 void *aux OVS_UNUSED)
4155 struct ds ds = DS_EMPTY_INITIALIZER;
4156 const struct dpif_flow_stats *stats;
4157 const struct ofproto_dpif *ofproto;
4158 struct dpif_flow_dump flow_dump;
4159 const struct nlattr *actions;
4160 const struct nlattr *mask;
4161 const struct nlattr *key;
4165 bool verbosity = false;
4166 struct dpif_port dpif_port;
4167 struct dpif_port_dump port_dump;
4168 struct hmap portno_names;
4172 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4174 unixctl_command_reply_error(conn, "no such bridge");
4178 if (argc > 2 && !strcmp(argv[1], "-m")) {
4182 hmap_init(&portno_names);
4183 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4184 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4188 error = dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4192 dpif_flow_dump_state_init(ofproto->backer->dpif, &state);
4193 while (dpif_flow_dump_next(&flow_dump, state, &key, &key_len,
4194 &mask, &mask_len, &actions, &actions_len,
4196 if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
4200 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4202 ds_put_cstr(&ds, ", ");
4203 dpif_flow_stats_format(stats, &ds);
4204 ds_put_cstr(&ds, ", actions:");
4205 format_odp_actions(&ds, actions, actions_len);
4206 ds_put_char(&ds, '\n');
4208 dpif_flow_dump_state_uninit(ofproto->backer->dpif, state);
4209 error = dpif_flow_dump_done(&flow_dump);
4214 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4215 unixctl_command_reply_error(conn, ds_cstr(&ds));
4217 unixctl_command_reply(conn, ds_cstr(&ds));
4219 odp_portno_names_destroy(&portno_names);
4220 hmap_destroy(&portno_names);
4225 ofproto_dpif_unixctl_init(void)
4227 static bool registered;
4233 unixctl_command_register(
4235 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4236 1, 3, ofproto_unixctl_trace, NULL);
4237 unixctl_command_register(
4238 "ofproto/trace-packet-out",
4239 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4240 2, 6, ofproto_unixctl_trace_actions, NULL);
4241 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4242 ofproto_unixctl_fdb_flush, NULL);
4243 unixctl_command_register("fdb/show", "bridge", 1, 1,
4244 ofproto_unixctl_fdb_show, NULL);
4245 unixctl_command_register("dpif/dump-dps", "", 0, 0,
4246 ofproto_unixctl_dpif_dump_dps, NULL);
4247 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4249 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4250 ofproto_unixctl_dpif_dump_flows, NULL);
4253 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4255 * This is deprecated. It is only for compatibility with broken device drivers
4256 * in old versions of Linux that do not properly support VLANs when VLAN
4257 * devices are not used. When broken device drivers are no longer in
4258 * widespread use, we will delete these interfaces. */
4261 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4263 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4264 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4266 if (realdev_ofp_port == ofport->realdev_ofp_port
4267 && vid == ofport->vlandev_vid) {
4271 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4273 if (ofport->realdev_ofp_port) {
4276 if (realdev_ofp_port && ofport->bundle) {
4277 /* vlandevs are enslaved to their realdevs, so they are not allowed to
4278 * themselves be part of a bundle. */
4279 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
4282 ofport->realdev_ofp_port = realdev_ofp_port;
4283 ofport->vlandev_vid = vid;
4285 if (realdev_ofp_port) {
4286 vsp_add(ofport, realdev_ofp_port, vid);
4293 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4295 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4299 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4300 OVS_EXCLUDED(ofproto->vsp_mutex)
4302 /* hmap_is_empty is thread safe. */
4303 return !hmap_is_empty(&ofproto->realdev_vid_map);
4307 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4308 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4309 OVS_REQUIRES(ofproto->vsp_mutex)
4311 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4312 int vid = vlan_tci_to_vid(vlan_tci);
4313 const struct vlan_splinter *vsp;
4315 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4316 hash_realdev_vid(realdev_ofp_port, vid),
4317 &ofproto->realdev_vid_map) {
4318 if (vsp->realdev_ofp_port == realdev_ofp_port
4319 && vsp->vid == vid) {
4320 return vsp->vlandev_ofp_port;
4324 return realdev_ofp_port;
4327 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4328 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4329 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
4330 * 'vlan_tci' 9, it would return the port number of eth0.9.
4332 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4333 * function just returns its 'realdev_ofp_port' argument. */
4335 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4336 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4337 OVS_EXCLUDED(ofproto->vsp_mutex)
4341 /* hmap_is_empty is thread safe, see if we can return immediately. */
4342 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
4343 return realdev_ofp_port;
4345 ovs_mutex_lock(&ofproto->vsp_mutex);
4346 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4347 ovs_mutex_unlock(&ofproto->vsp_mutex);
4351 static struct vlan_splinter *
4352 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4354 struct vlan_splinter *vsp;
4356 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4357 hash_ofp_port(vlandev_ofp_port),
4358 &ofproto->vlandev_map) {
4359 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4367 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4368 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4369 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
4370 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4371 * eth0 and store 9 in '*vid'.
4373 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4374 * VLAN device. Unless VLAN splinters are enabled, this is what this function
4377 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4378 ofp_port_t vlandev_ofp_port, int *vid)
4379 OVS_REQUIRES(ofproto->vsp_mutex)
4381 if (!hmap_is_empty(&ofproto->vlandev_map)) {
4382 const struct vlan_splinter *vsp;
4384 vsp = vlandev_find(ofproto, vlandev_ofp_port);
4389 return vsp->realdev_ofp_port;
4395 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4396 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
4397 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4398 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
4399 * always the case unless VLAN splinters are enabled), returns false without
4400 * making any changes. */
4402 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4403 OVS_EXCLUDED(ofproto->vsp_mutex)
4408 /* hmap_is_empty is thread safe. */
4409 if (hmap_is_empty(&ofproto->vlandev_map)) {
4413 ovs_mutex_lock(&ofproto->vsp_mutex);
4414 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4415 ovs_mutex_unlock(&ofproto->vsp_mutex);
4420 /* Cause the flow to be processed as if it came in on the real device with
4421 * the VLAN device's VLAN ID. */
4422 flow->in_port.ofp_port = realdev;
4423 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4428 vsp_remove(struct ofport_dpif *port)
4430 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4431 struct vlan_splinter *vsp;
4433 ovs_mutex_lock(&ofproto->vsp_mutex);
4434 vsp = vlandev_find(ofproto, port->up.ofp_port);
4436 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4437 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4440 port->realdev_ofp_port = 0;
4442 VLOG_ERR("missing vlan device record");
4444 ovs_mutex_unlock(&ofproto->vsp_mutex);
4448 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4450 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4452 ovs_mutex_lock(&ofproto->vsp_mutex);
4453 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4454 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4455 == realdev_ofp_port)) {
4456 struct vlan_splinter *vsp;
4458 vsp = xmalloc(sizeof *vsp);
4459 vsp->realdev_ofp_port = realdev_ofp_port;
4460 vsp->vlandev_ofp_port = port->up.ofp_port;
4463 port->realdev_ofp_port = realdev_ofp_port;
4465 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4466 hash_ofp_port(port->up.ofp_port));
4467 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4468 hash_realdev_vid(realdev_ofp_port, vid));
4470 VLOG_ERR("duplicate vlan device record");
4472 ovs_mutex_unlock(&ofproto->vsp_mutex);
4476 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4478 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4479 return ofport ? ofport->odp_port : ODPP_NONE;
4482 struct ofport_dpif *
4483 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4485 struct ofport_dpif *port;
4487 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4488 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4489 &backer->odp_to_ofport_map) {
4490 if (port->odp_port == odp_port) {
4491 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4496 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4501 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4503 struct ofport_dpif *port;
4505 port = odp_port_to_ofport(ofproto->backer, odp_port);
4506 if (port && &ofproto->up == port->up.ofproto) {
4507 return port->up.ofp_port;
4513 const struct ofproto_class ofproto_dpif_class = {
4527 NULL, /* get_memory_usage. */
4528 type_get_memory_usage,
4547 port_is_lacp_current,
4548 NULL, /* rule_choose_table */
4557 rule_modify_actions,
4571 get_stp_port_status,
4579 is_mirror_output_bundle,
4580 forward_bpdu_changed,
4581 set_mac_table_config,
4583 NULL, /* meter_get_features */
4584 NULL, /* meter_set */
4585 NULL, /* meter_get */
4586 NULL, /* meter_del */
4587 group_alloc, /* group_alloc */
4588 group_construct, /* group_construct */
4589 group_destruct, /* group_destruct */
4590 group_dealloc, /* group_dealloc */
4591 group_modify, /* group_modify */
4592 group_get_stats, /* group_get_stats */