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-rid.h"
57 #include "ofproto-dpif-sflow.h"
58 #include "ofproto-dpif-upcall.h"
59 #include "ofproto-dpif-xlate.h"
60 #include "poll-loop.h"
66 #include "unaligned.h"
68 #include "vlan-bitmap.h"
71 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
73 COVERAGE_DEFINE(ofproto_dpif_expired);
74 COVERAGE_DEFINE(packet_in_overflow);
76 /* Number of implemented OpenFlow tables. */
77 enum { N_TABLES = 255 };
78 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
79 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
88 * - Do include packets and bytes from datapath flows which have not
89 * recently been processed by a revalidator. */
90 struct ovs_mutex stats_mutex;
91 struct dpif_flow_stats stats OVS_GUARDED;
94 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
96 static struct rule_dpif *rule_dpif_cast(const struct rule *);
97 static void rule_expire(struct rule_dpif *);
104 * - Do include packets and bytes from datapath flows which have not
105 * recently been processed by a revalidator. */
106 struct ovs_mutex stats_mutex;
107 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
108 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
109 struct bucket_counter *bucket_stats OVS_GUARDED; /* Bucket statistics. */
113 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
114 struct ofproto_dpif *ofproto; /* Owning ofproto. */
115 void *aux; /* Key supplied by ofproto's client. */
116 char *name; /* Identifier for log messages. */
119 struct list ports; /* Contains "struct ofport"s. */
120 enum port_vlan_mode vlan_mode; /* VLAN mode */
121 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
122 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
123 * NULL if all VLANs are trunked. */
124 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
125 struct bond *bond; /* Nonnull iff more than one port. */
126 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
129 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
132 static void bundle_remove(struct ofport *);
133 static void bundle_update(struct ofbundle *);
134 static void bundle_destroy(struct ofbundle *);
135 static void bundle_del_port(struct ofport_dpif *);
136 static void bundle_run(struct ofbundle *);
137 static void bundle_wait(struct ofbundle *);
139 static void stp_run(struct ofproto_dpif *ofproto);
140 static void stp_wait(struct ofproto_dpif *ofproto);
141 static int set_stp_port(struct ofport *,
142 const struct ofproto_port_stp_settings *);
145 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
149 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
150 struct list bundle_node; /* In struct ofbundle's "ports" list. */
151 struct cfm *cfm; /* Connectivity Fault Management, if any. */
152 struct bfd *bfd; /* BFD, if any. */
153 bool may_enable; /* May be enabled in bonds. */
154 bool is_tunnel; /* This port is a tunnel. */
155 bool is_layer3; /* This is a layer 3 port. */
156 long long int carrier_seq; /* Carrier status changes. */
157 struct ofport_dpif *peer; /* Peer if patch port. */
160 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
161 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
162 long long int stp_state_entered;
164 /* Queue to DSCP mapping. */
165 struct ofproto_port_queue *qdscp;
168 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
170 * This is deprecated. It is only for compatibility with broken device
171 * drivers in old versions of Linux that do not properly support VLANs when
172 * VLAN devices are not used. When broken device drivers are no longer in
173 * widespread use, we will delete these interfaces. */
174 ofp_port_t realdev_ofp_port;
178 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
180 * This is deprecated. It is only for compatibility with broken device drivers
181 * in old versions of Linux that do not properly support VLANs when VLAN
182 * devices are not used. When broken device drivers are no longer in
183 * widespread use, we will delete these interfaces. */
184 struct vlan_splinter {
185 struct hmap_node realdev_vid_node;
186 struct hmap_node vlandev_node;
187 ofp_port_t realdev_ofp_port;
188 ofp_port_t vlandev_ofp_port;
192 static void vsp_remove(struct ofport_dpif *);
193 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
195 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
198 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
201 static struct ofport_dpif *
202 ofport_dpif_cast(const struct ofport *ofport)
204 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
207 static void port_run(struct ofport_dpif *);
208 static int set_bfd(struct ofport *, const struct smap *);
209 static int set_cfm(struct ofport *, const struct cfm_settings *);
210 static void ofport_update_peer(struct ofport_dpif *);
212 struct dpif_completion {
213 struct list list_node;
214 struct ofoperation *op;
217 /* Reasons that we might need to revalidate every datapath flow, and
218 * corresponding coverage counters.
220 * A value of 0 means that there is no need to revalidate.
222 * It would be nice to have some cleaner way to integrate with coverage
223 * counters, but with only a few reasons I guess this is good enough for
225 enum revalidate_reason {
226 REV_RECONFIGURE = 1, /* Switch configuration changed. */
227 REV_STP, /* Spanning tree protocol port status change. */
228 REV_BOND, /* Bonding changed. */
229 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
230 REV_FLOW_TABLE, /* Flow table changed. */
231 REV_MAC_LEARNING, /* Mac learning changed. */
233 COVERAGE_DEFINE(rev_reconfigure);
234 COVERAGE_DEFINE(rev_stp);
235 COVERAGE_DEFINE(rev_bond);
236 COVERAGE_DEFINE(rev_port_toggled);
237 COVERAGE_DEFINE(rev_flow_table);
238 COVERAGE_DEFINE(rev_mac_learning);
240 /* All datapaths of a given type share a single dpif backer instance. */
247 struct ovs_rwlock odp_to_ofport_lock;
248 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
250 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
252 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
254 bool recv_set_enable; /* Enables or disables receiving packets. */
256 struct recirc_id_pool *rid_pool; /* Recirculation ID pool. */
258 /* True if the datapath supports variable-length
259 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
260 * False if the datapath supports only 8-byte (or shorter) userdata. */
261 bool variable_length_userdata;
263 /* Maximum number of MPLS label stack entries that the datapath supports
265 size_t max_mpls_depth;
268 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
269 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
271 struct ofproto_dpif {
272 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
274 struct dpif_backer *backer;
276 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
278 /* Special OpenFlow rules. */
279 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
280 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
281 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
284 struct netflow *netflow;
285 struct dpif_sflow *sflow;
286 struct dpif_ipfix *ipfix;
287 struct hmap bundles; /* Contains "struct ofbundle"s. */
288 struct mac_learning *ml;
289 bool has_bonded_bundles;
291 struct mbridge *mbridge;
293 struct ovs_mutex stats_mutex;
294 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
295 * consumed in userspace. */
299 long long int stp_last_tick;
301 /* VLAN splinters. */
302 struct ovs_mutex vsp_mutex;
303 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
304 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
307 struct sset ports; /* Set of standard port names. */
308 struct sset ghost_ports; /* Ports with no datapath port. */
309 struct sset port_poll_set; /* Queued names for port_poll() reply. */
310 int port_poll_errno; /* Last errno for port_poll() reply. */
311 uint64_t change_seq; /* Connectivity status changes. */
314 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
317 /* All existing ofproto_dpif instances, indexed by ->up.name. */
318 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
320 static void ofproto_dpif_unixctl_init(void);
322 static inline struct ofproto_dpif *
323 ofproto_dpif_cast(const struct ofproto *ofproto)
325 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
326 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
330 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
332 return ofproto->backer->max_mpls_depth;
335 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
336 ofp_port_t ofp_port);
337 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
338 const struct ofpbuf *packet,
339 const struct ofpact[], size_t ofpacts_len,
342 /* Global variables. */
343 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
345 /* Initial mappings of port to bridge mappings. */
346 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
348 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
351 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
352 struct ofputil_flow_mod *fm)
354 ofproto_flow_mod(&ofproto->up, fm);
357 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
358 * Takes ownership of 'pin' and pin->packet. */
360 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
361 struct ofproto_packet_in *pin)
363 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
364 COVERAGE_INC(packet_in_overflow);
365 free(CONST_CAST(void *, pin->up.packet));
370 /* Factory functions. */
373 init(const struct shash *iface_hints)
375 struct shash_node *node;
377 /* Make a local copy, since we don't own 'iface_hints' elements. */
378 SHASH_FOR_EACH(node, iface_hints) {
379 const struct iface_hint *orig_hint = node->data;
380 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
382 new_hint->br_name = xstrdup(orig_hint->br_name);
383 new_hint->br_type = xstrdup(orig_hint->br_type);
384 new_hint->ofp_port = orig_hint->ofp_port;
386 shash_add(&init_ofp_ports, node->name, new_hint);
391 enumerate_types(struct sset *types)
393 dp_enumerate_types(types);
397 enumerate_names(const char *type, struct sset *names)
399 struct ofproto_dpif *ofproto;
402 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
403 if (strcmp(type, ofproto->up.type)) {
406 sset_add(names, ofproto->up.name);
413 del(const char *type, const char *name)
418 error = dpif_open(name, type, &dpif);
420 error = dpif_delete(dpif);
427 port_open_type(const char *datapath_type, const char *port_type)
429 return dpif_port_open_type(datapath_type, port_type);
432 /* Type functions. */
434 static void process_dpif_port_changes(struct dpif_backer *);
435 static void process_dpif_all_ports_changed(struct dpif_backer *);
436 static void process_dpif_port_change(struct dpif_backer *,
437 const char *devname);
438 static void process_dpif_port_error(struct dpif_backer *, int error);
440 static struct ofproto_dpif *
441 lookup_ofproto_dpif_by_port_name(const char *name)
443 struct ofproto_dpif *ofproto;
445 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
446 if (sset_contains(&ofproto->ports, name)) {
455 type_run(const char *type)
457 struct dpif_backer *backer;
459 backer = shash_find_data(&all_dpif_backers, type);
461 /* This is not necessarily a problem, since backers are only
462 * created on demand. */
466 dpif_run(backer->dpif);
468 /* If vswitchd started with other_config:flow_restore_wait set as "true",
469 * and the configuration has now changed to "false", enable receiving
470 * packets from the datapath. */
471 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
474 backer->recv_set_enable = true;
476 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
478 VLOG_ERR("Failed to enable receiving packets in dpif.");
481 dpif_flow_flush(backer->dpif);
482 backer->need_revalidate = REV_RECONFIGURE;
485 if (backer->recv_set_enable) {
486 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
489 if (backer->need_revalidate) {
490 struct ofproto_dpif *ofproto;
491 struct simap_node *node;
492 struct simap tmp_backers;
494 /* Handle tunnel garbage collection. */
495 simap_init(&tmp_backers);
496 simap_swap(&backer->tnl_backers, &tmp_backers);
498 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
499 struct ofport_dpif *iter;
501 if (backer != ofproto->backer) {
505 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
506 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
509 if (!iter->is_tunnel) {
513 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
514 namebuf, sizeof namebuf);
515 node = simap_find(&tmp_backers, dp_port);
517 simap_put(&backer->tnl_backers, dp_port, node->data);
518 simap_delete(&tmp_backers, node);
519 node = simap_find(&backer->tnl_backers, dp_port);
521 node = simap_find(&backer->tnl_backers, dp_port);
523 odp_port_t odp_port = ODPP_NONE;
525 if (!dpif_port_add(backer->dpif, iter->up.netdev,
527 simap_put(&backer->tnl_backers, dp_port,
528 odp_to_u32(odp_port));
529 node = simap_find(&backer->tnl_backers, dp_port);
534 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
535 if (tnl_port_reconfigure(iter, iter->up.netdev,
537 backer->need_revalidate = REV_RECONFIGURE;
542 SIMAP_FOR_EACH (node, &tmp_backers) {
543 dpif_port_del(backer->dpif, u32_to_odp(node->data));
545 simap_destroy(&tmp_backers);
547 switch (backer->need_revalidate) {
548 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
549 case REV_STP: COVERAGE_INC(rev_stp); break;
550 case REV_BOND: COVERAGE_INC(rev_bond); break;
551 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
552 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
553 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
555 backer->need_revalidate = 0;
557 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
558 struct ofport_dpif *ofport;
559 struct ofbundle *bundle;
561 if (ofproto->backer != backer) {
565 ovs_rwlock_wrlock(&xlate_rwlock);
566 xlate_ofproto_set(ofproto, ofproto->up.name,
567 ofproto->backer->dpif, ofproto->miss_rule,
568 ofproto->no_packet_in_rule, ofproto->ml,
569 ofproto->stp, ofproto->mbridge,
570 ofproto->sflow, ofproto->ipfix,
571 ofproto->netflow, ofproto->up.frag_handling,
572 ofproto->up.forward_bpdu,
573 connmgr_has_in_band(ofproto->up.connmgr),
574 ofproto->backer->variable_length_userdata,
575 ofproto->backer->max_mpls_depth);
577 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
578 xlate_bundle_set(ofproto, bundle, bundle->name,
579 bundle->vlan_mode, bundle->vlan,
580 bundle->trunks, bundle->use_priority_tags,
581 bundle->bond, bundle->lacp,
585 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
586 int stp_port = ofport->stp_port
587 ? stp_port_no(ofport->stp_port)
589 xlate_ofport_set(ofproto, ofport->bundle, ofport,
590 ofport->up.ofp_port, ofport->odp_port,
591 ofport->up.netdev, ofport->cfm,
592 ofport->bfd, ofport->peer, stp_port,
593 ofport->qdscp, ofport->n_qdscp,
594 ofport->up.pp.config, ofport->up.pp.state,
595 ofport->is_tunnel, ofport->may_enable);
597 ovs_rwlock_unlock(&xlate_rwlock);
600 udpif_revalidate(backer->udpif);
603 process_dpif_port_changes(backer);
608 /* Check for and handle port changes in 'backer''s dpif. */
610 process_dpif_port_changes(struct dpif_backer *backer)
616 error = dpif_port_poll(backer->dpif, &devname);
622 process_dpif_all_ports_changed(backer);
626 process_dpif_port_change(backer, devname);
631 process_dpif_port_error(backer, error);
638 process_dpif_all_ports_changed(struct dpif_backer *backer)
640 struct ofproto_dpif *ofproto;
641 struct dpif_port dpif_port;
642 struct dpif_port_dump dump;
643 struct sset devnames;
646 sset_init(&devnames);
647 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
648 if (ofproto->backer == backer) {
649 struct ofport *ofport;
651 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
652 sset_add(&devnames, netdev_get_name(ofport->netdev));
656 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
657 sset_add(&devnames, dpif_port.name);
660 SSET_FOR_EACH (devname, &devnames) {
661 process_dpif_port_change(backer, devname);
663 sset_destroy(&devnames);
667 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
669 struct ofproto_dpif *ofproto;
670 struct dpif_port port;
672 /* Don't report on the datapath's device. */
673 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
677 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
678 &all_ofproto_dpifs) {
679 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
684 ofproto = lookup_ofproto_dpif_by_port_name(devname);
685 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
686 /* The port was removed. If we know the datapath,
687 * report it through poll_set(). If we don't, it may be
688 * notifying us of a removal we initiated, so ignore it.
689 * If there's a pending ENOBUFS, let it stand, since
690 * everything will be reevaluated. */
691 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
692 sset_add(&ofproto->port_poll_set, devname);
693 ofproto->port_poll_errno = 0;
695 } else if (!ofproto) {
696 /* The port was added, but we don't know with which
697 * ofproto we should associate it. Delete it. */
698 dpif_port_del(backer->dpif, port.port_no);
700 struct ofport_dpif *ofport;
702 ofport = ofport_dpif_cast(shash_find_data(
703 &ofproto->up.port_by_name, devname));
705 && ofport->odp_port != port.port_no
706 && !odp_port_to_ofport(backer, port.port_no))
708 /* 'ofport''s datapath port number has changed from
709 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
710 * structures to match. */
711 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
712 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
713 ofport->odp_port = port.port_no;
714 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
715 hash_odp_port(port.port_no));
716 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
717 backer->need_revalidate = REV_RECONFIGURE;
720 dpif_port_destroy(&port);
723 /* Propagate 'error' to all ofprotos based on 'backer'. */
725 process_dpif_port_error(struct dpif_backer *backer, int error)
727 struct ofproto_dpif *ofproto;
729 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
730 if (ofproto->backer == backer) {
731 sset_clear(&ofproto->port_poll_set);
732 ofproto->port_poll_errno = error;
738 type_wait(const char *type)
740 struct dpif_backer *backer;
742 backer = shash_find_data(&all_dpif_backers, type);
744 /* This is not necessarily a problem, since backers are only
745 * created on demand. */
749 dpif_wait(backer->dpif);
752 /* Basic life-cycle. */
754 static int add_internal_flows(struct ofproto_dpif *);
756 static struct ofproto *
759 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
764 dealloc(struct ofproto *ofproto_)
766 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
771 close_dpif_backer(struct dpif_backer *backer)
773 ovs_assert(backer->refcount > 0);
775 if (--backer->refcount) {
779 udpif_destroy(backer->udpif);
781 simap_destroy(&backer->tnl_backers);
782 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
783 hmap_destroy(&backer->odp_to_ofport_map);
784 shash_find_and_delete(&all_dpif_backers, backer->type);
785 recirc_id_pool_destroy(backer->rid_pool);
787 dpif_close(backer->dpif);
791 /* Datapath port slated for removal from datapath. */
793 struct list list_node;
797 static bool check_variable_length_userdata(struct dpif_backer *backer);
798 static size_t check_max_mpls_depth(struct dpif_backer *backer);
801 open_dpif_backer(const char *type, struct dpif_backer **backerp)
803 struct dpif_backer *backer;
804 struct dpif_port_dump port_dump;
805 struct dpif_port port;
806 struct shash_node *node;
807 struct list garbage_list;
808 struct odp_garbage *garbage, *next;
815 backer = shash_find_data(&all_dpif_backers, type);
822 backer_name = xasprintf("ovs-%s", type);
824 /* Remove any existing datapaths, since we assume we're the only
825 * userspace controlling the datapath. */
827 dp_enumerate_names(type, &names);
828 SSET_FOR_EACH(name, &names) {
829 struct dpif *old_dpif;
831 /* Don't remove our backer if it exists. */
832 if (!strcmp(name, backer_name)) {
836 if (dpif_open(name, type, &old_dpif)) {
837 VLOG_WARN("couldn't open old datapath %s to remove it", name);
839 dpif_delete(old_dpif);
840 dpif_close(old_dpif);
843 sset_destroy(&names);
845 backer = xmalloc(sizeof *backer);
847 error = dpif_create_and_open(backer_name, type, &backer->dpif);
850 VLOG_ERR("failed to open datapath of type %s: %s", type,
851 ovs_strerror(error));
855 backer->udpif = udpif_create(backer, backer->dpif);
857 backer->type = xstrdup(type);
858 backer->refcount = 1;
859 hmap_init(&backer->odp_to_ofport_map);
860 ovs_rwlock_init(&backer->odp_to_ofport_lock);
861 backer->need_revalidate = 0;
862 simap_init(&backer->tnl_backers);
863 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
866 if (backer->recv_set_enable) {
867 dpif_flow_flush(backer->dpif);
870 /* Loop through the ports already on the datapath and remove any
871 * that we don't need anymore. */
872 list_init(&garbage_list);
873 dpif_port_dump_start(&port_dump, backer->dpif);
874 while (dpif_port_dump_next(&port_dump, &port)) {
875 node = shash_find(&init_ofp_ports, port.name);
876 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
877 garbage = xmalloc(sizeof *garbage);
878 garbage->odp_port = port.port_no;
879 list_push_front(&garbage_list, &garbage->list_node);
882 dpif_port_dump_done(&port_dump);
884 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
885 dpif_port_del(backer->dpif, garbage->odp_port);
886 list_remove(&garbage->list_node);
890 shash_add(&all_dpif_backers, type, backer);
892 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
894 VLOG_ERR("failed to listen on datapath of type %s: %s",
895 type, ovs_strerror(error));
896 close_dpif_backer(backer);
899 backer->variable_length_userdata = check_variable_length_userdata(backer);
900 backer->max_mpls_depth = check_max_mpls_depth(backer);
901 backer->rid_pool = recirc_id_pool_create();
903 if (backer->recv_set_enable) {
904 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
910 /* Tests whether 'backer''s datapath supports variable-length
911 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
912 * to disable some features on older datapaths that don't support this
915 * Returns false if 'backer' definitely does not support variable-length
916 * userdata, true if it seems to support them or if at least the error we get
919 check_variable_length_userdata(struct dpif_backer *backer)
921 struct eth_header *eth;
922 struct ofpbuf actions;
923 struct dpif_execute execute;
924 struct ofpbuf packet;
928 /* Compose a userspace action that will cause an ERANGE error on older
929 * datapaths that don't support variable-length userdata.
931 * We really test for using userdata longer than 8 bytes, but older
932 * datapaths accepted these, silently truncating the userdata to 8 bytes.
933 * The same older datapaths rejected userdata shorter than 8 bytes, so we
934 * test for that instead as a proxy for longer userdata support. */
935 ofpbuf_init(&actions, 64);
936 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
937 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
938 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
939 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
940 nl_msg_end_nested(&actions, start);
942 /* Compose a dummy ethernet packet. */
943 ofpbuf_init(&packet, ETH_HEADER_LEN);
944 eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
945 eth->eth_type = htons(0x1234);
947 /* Execute the actions. On older datapaths this fails with ERANGE, on
948 * newer datapaths it succeeds. */
949 execute.actions = actions.data;
950 execute.actions_len = actions.size;
951 execute.packet = &packet;
952 execute.md = PKT_METADATA_INITIALIZER(0);
953 execute.needs_help = false;
955 error = dpif_execute(backer->dpif, &execute);
957 ofpbuf_uninit(&packet);
958 ofpbuf_uninit(&actions);
962 /* Variable-length userdata is supported.
964 * Purge received packets to avoid processing the nonsense packet we
965 * sent to userspace, then report success. */
966 dpif_recv_purge(backer->dpif);
970 /* Variable-length userdata is not supported. */
971 VLOG_WARN("%s: datapath does not support variable-length userdata "
972 "feature (needs Linux 3.10+ or kernel module from OVS "
973 "1..11+). The NXAST_SAMPLE action will be ignored.",
974 dpif_name(backer->dpif));
978 /* Something odd happened. We're not sure whether variable-length
979 * userdata is supported. Default to "yes". */
980 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
981 dpif_name(backer->dpif), ovs_strerror(error));
986 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
988 * Returns the number of elements in a struct flow's mpls_lse field
989 * if the datapath supports at least that many entries in an
991 * Otherwise returns the number of MPLS push actions supported by
994 check_max_mpls_depth(struct dpif_backer *backer)
999 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1000 struct odputil_keybuf keybuf;
1004 memset(&flow, 0, sizeof flow);
1005 flow.dl_type = htons(ETH_TYPE_MPLS);
1006 flow_set_mpls_bos(&flow, n, 1);
1008 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1009 odp_flow_key_from_flow(&key, &flow, 0);
1011 error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
1012 key.data, key.size, NULL, 0, NULL, 0, NULL);
1013 if (error && error != EEXIST) {
1014 if (error != EINVAL) {
1015 VLOG_WARN("%s: MPLS stack length feature probe failed (%s)",
1016 dpif_name(backer->dpif), ovs_strerror(error));
1021 error = dpif_flow_del(backer->dpif, key.data, key.size, NULL);
1023 VLOG_WARN("%s: failed to delete MPLS feature probe flow",
1024 dpif_name(backer->dpif));
1028 VLOG_INFO("%s: MPLS label stack length probed as %d",
1029 dpif_name(backer->dpif), n);
1034 construct(struct ofproto *ofproto_)
1036 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1037 struct shash_node *node, *next;
1040 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1045 ofproto->netflow = NULL;
1046 ofproto->sflow = NULL;
1047 ofproto->ipfix = NULL;
1048 ofproto->stp = NULL;
1049 ofproto->dump_seq = 0;
1050 hmap_init(&ofproto->bundles);
1051 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1052 ofproto->mbridge = mbridge_create();
1053 ofproto->has_bonded_bundles = false;
1054 ofproto->lacp_enabled = false;
1055 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1056 ovs_mutex_init(&ofproto->vsp_mutex);
1058 guarded_list_init(&ofproto->pins);
1060 ofproto_dpif_unixctl_init();
1062 hmap_init(&ofproto->vlandev_map);
1063 hmap_init(&ofproto->realdev_vid_map);
1065 sset_init(&ofproto->ports);
1066 sset_init(&ofproto->ghost_ports);
1067 sset_init(&ofproto->port_poll_set);
1068 ofproto->port_poll_errno = 0;
1069 ofproto->change_seq = 0;
1071 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1072 struct iface_hint *iface_hint = node->data;
1074 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1075 /* Check if the datapath already has this port. */
1076 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1077 sset_add(&ofproto->ports, node->name);
1080 free(iface_hint->br_name);
1081 free(iface_hint->br_type);
1083 shash_delete(&init_ofp_ports, node);
1087 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1088 hash_string(ofproto->up.name, 0));
1089 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1091 ofproto_init_tables(ofproto_, N_TABLES);
1092 error = add_internal_flows(ofproto);
1093 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1099 add_internal_flow(struct ofproto_dpif *ofproto, int id,
1100 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1102 struct ofputil_flow_mod fm;
1103 struct classifier *cls;
1106 match_init_catchall(&fm.match);
1108 match_set_reg(&fm.match, 0, id);
1109 fm.new_cookie = htonll(0);
1110 fm.cookie = htonll(0);
1111 fm.cookie_mask = htonll(0);
1112 fm.modify_cookie = false;
1113 fm.table_id = TBL_INTERNAL;
1114 fm.command = OFPFC_ADD;
1115 fm.idle_timeout = 0;
1116 fm.hard_timeout = 0;
1120 fm.ofpacts = ofpacts->data;
1121 fm.ofpacts_len = ofpacts->size;
1123 error = ofproto_flow_mod(&ofproto->up, &fm);
1125 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1126 id, ofperr_to_string(error));
1130 cls = &ofproto->up.tables[TBL_INTERNAL].cls;
1131 fat_rwlock_rdlock(&cls->rwlock);
1132 *rulep = rule_dpif_cast(rule_from_cls_rule(
1133 classifier_lookup(cls, &fm.match.flow, NULL)));
1134 ovs_assert(*rulep != NULL);
1135 fat_rwlock_unlock(&cls->rwlock);
1141 add_internal_flows(struct ofproto_dpif *ofproto)
1143 struct ofpact_controller *controller;
1144 uint64_t ofpacts_stub[128 / 8];
1145 struct ofpbuf ofpacts;
1149 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1152 controller = ofpact_put_CONTROLLER(&ofpacts);
1153 controller->max_len = UINT16_MAX;
1154 controller->controller_id = 0;
1155 controller->reason = OFPR_NO_MATCH;
1156 ofpact_pad(&ofpacts);
1158 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1163 ofpbuf_clear(&ofpacts);
1164 error = add_internal_flow(ofproto, id++, &ofpacts,
1165 &ofproto->no_packet_in_rule);
1170 error = add_internal_flow(ofproto, id++, &ofpacts,
1171 &ofproto->drop_frags_rule);
1176 destruct(struct ofproto *ofproto_)
1178 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1179 struct rule_dpif *rule, *next_rule;
1180 struct ofproto_packet_in *pin, *next_pin;
1181 struct oftable *table;
1184 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1185 ovs_rwlock_wrlock(&xlate_rwlock);
1186 xlate_remove_ofproto(ofproto);
1187 ovs_rwlock_unlock(&xlate_rwlock);
1189 /* Ensure that the upcall processing threads have no remaining references
1190 * to the ofproto or anything in it. */
1191 udpif_synchronize(ofproto->backer->udpif);
1193 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1195 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1196 struct cls_cursor cursor;
1198 fat_rwlock_rdlock(&table->cls.rwlock);
1199 cls_cursor_init(&cursor, &table->cls, NULL);
1200 fat_rwlock_unlock(&table->cls.rwlock);
1201 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1202 ofproto_rule_delete(&ofproto->up, &rule->up);
1206 guarded_list_pop_all(&ofproto->pins, &pins);
1207 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1208 list_remove(&pin->list_node);
1209 free(CONST_CAST(void *, pin->up.packet));
1212 guarded_list_destroy(&ofproto->pins);
1214 mbridge_unref(ofproto->mbridge);
1216 netflow_unref(ofproto->netflow);
1217 dpif_sflow_unref(ofproto->sflow);
1218 hmap_destroy(&ofproto->bundles);
1219 mac_learning_unref(ofproto->ml);
1221 hmap_destroy(&ofproto->vlandev_map);
1222 hmap_destroy(&ofproto->realdev_vid_map);
1224 sset_destroy(&ofproto->ports);
1225 sset_destroy(&ofproto->ghost_ports);
1226 sset_destroy(&ofproto->port_poll_set);
1228 ovs_mutex_destroy(&ofproto->stats_mutex);
1229 ovs_mutex_destroy(&ofproto->vsp_mutex);
1231 close_dpif_backer(ofproto->backer);
1235 run(struct ofproto *ofproto_)
1237 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1238 uint64_t new_seq, new_dump_seq;
1240 if (mbridge_need_revalidate(ofproto->mbridge)) {
1241 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1242 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1243 mac_learning_flush(ofproto->ml);
1244 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1247 /* Do not perform any periodic activity required by 'ofproto' while
1248 * waiting for flow restore to complete. */
1249 if (!ofproto_get_flow_restore_wait()) {
1250 struct ofproto_packet_in *pin, *next_pin;
1253 guarded_list_pop_all(&ofproto->pins, &pins);
1254 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1255 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1256 list_remove(&pin->list_node);
1257 free(CONST_CAST(void *, pin->up.packet));
1262 if (ofproto->netflow) {
1263 netflow_run(ofproto->netflow);
1265 if (ofproto->sflow) {
1266 dpif_sflow_run(ofproto->sflow);
1268 if (ofproto->ipfix) {
1269 dpif_ipfix_run(ofproto->ipfix);
1272 new_seq = seq_read(connectivity_seq_get());
1273 if (ofproto->change_seq != new_seq) {
1274 struct ofport_dpif *ofport;
1276 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1280 ofproto->change_seq = new_seq;
1282 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1283 struct ofbundle *bundle;
1285 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1291 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1292 if (mac_learning_run(ofproto->ml)) {
1293 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1295 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1297 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1298 if (ofproto->dump_seq != new_dump_seq) {
1299 struct rule *rule, *next_rule;
1301 /* We know stats are relatively fresh, so now is a good time to do some
1303 ofproto->dump_seq = new_dump_seq;
1305 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1307 ovs_mutex_lock(&ofproto_mutex);
1308 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1309 &ofproto->up.expirable) {
1310 rule_expire(rule_dpif_cast(rule));
1312 ovs_mutex_unlock(&ofproto_mutex);
1314 /* All outstanding data in existing flows has been accounted, so it's a
1315 * good time to do bond rebalancing. */
1316 if (ofproto->has_bonded_bundles) {
1317 struct ofbundle *bundle;
1319 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1321 bond_rebalance(bundle->bond);
1331 wait(struct ofproto *ofproto_)
1333 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1335 if (ofproto_get_flow_restore_wait()) {
1339 if (ofproto->sflow) {
1340 dpif_sflow_wait(ofproto->sflow);
1342 if (ofproto->ipfix) {
1343 dpif_ipfix_wait(ofproto->ipfix);
1345 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1346 struct ofbundle *bundle;
1348 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1349 bundle_wait(bundle);
1352 if (ofproto->netflow) {
1353 netflow_wait(ofproto->netflow);
1355 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1356 mac_learning_wait(ofproto->ml);
1357 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1359 if (ofproto->backer->need_revalidate) {
1360 /* Shouldn't happen, but if it does just go around again. */
1361 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1362 poll_immediate_wake();
1365 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1369 type_get_memory_usage(const char *type, struct simap *usage)
1371 struct dpif_backer *backer;
1373 backer = shash_find_data(&all_dpif_backers, type);
1375 udpif_get_memory_usage(backer->udpif, usage);
1380 flush(struct ofproto *ofproto_)
1382 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1383 struct dpif_backer *backer = ofproto->backer;
1386 udpif_flush(backer->udpif);
1391 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1392 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1394 *arp_match_ip = true;
1395 *actions = (OFPUTIL_A_OUTPUT |
1396 OFPUTIL_A_SET_VLAN_VID |
1397 OFPUTIL_A_SET_VLAN_PCP |
1398 OFPUTIL_A_STRIP_VLAN |
1399 OFPUTIL_A_SET_DL_SRC |
1400 OFPUTIL_A_SET_DL_DST |
1401 OFPUTIL_A_SET_NW_SRC |
1402 OFPUTIL_A_SET_NW_DST |
1403 OFPUTIL_A_SET_NW_TOS |
1404 OFPUTIL_A_SET_TP_SRC |
1405 OFPUTIL_A_SET_TP_DST |
1410 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1412 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1413 struct dpif_dp_stats s;
1414 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1418 strcpy(ots->name, "classifier");
1420 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1421 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes, &used);
1422 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes,
1424 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes,
1426 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1427 ots->lookup_count = htonll(n_lookup);
1428 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1431 static struct ofport *
1434 struct ofport_dpif *port = xmalloc(sizeof *port);
1439 port_dealloc(struct ofport *port_)
1441 struct ofport_dpif *port = ofport_dpif_cast(port_);
1446 port_construct(struct ofport *port_)
1448 struct ofport_dpif *port = ofport_dpif_cast(port_);
1449 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1450 const struct netdev *netdev = port->up.netdev;
1451 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1452 struct dpif_port dpif_port;
1455 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1456 port->bundle = NULL;
1459 port->may_enable = true;
1460 port->stp_port = NULL;
1461 port->stp_state = STP_DISABLED;
1462 port->is_tunnel = false;
1466 port->realdev_ofp_port = 0;
1467 port->vlandev_vid = 0;
1468 port->carrier_seq = netdev_get_carrier_resets(netdev);
1469 port->is_layer3 = netdev_vport_is_layer3(netdev);
1471 if (netdev_vport_is_patch(netdev)) {
1472 /* By bailing out here, we don't submit the port to the sFlow module
1473 * to be considered for counter polling export. This is correct
1474 * because the patch port represents an interface that sFlow considers
1475 * to be "internal" to the switch as a whole, and therefore not an
1476 * candidate for counter polling. */
1477 port->odp_port = ODPP_NONE;
1478 ofport_update_peer(port);
1482 error = dpif_port_query_by_name(ofproto->backer->dpif,
1483 netdev_vport_get_dpif_port(netdev, namebuf,
1490 port->odp_port = dpif_port.port_no;
1492 if (netdev_get_tunnel_config(netdev)) {
1493 tnl_port_add(port, port->up.netdev, port->odp_port);
1494 port->is_tunnel = true;
1496 /* Sanity-check that a mapping doesn't already exist. This
1497 * shouldn't happen for non-tunnel ports. */
1498 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1499 VLOG_ERR("port %s already has an OpenFlow port number",
1501 dpif_port_destroy(&dpif_port);
1505 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1506 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1507 hash_odp_port(port->odp_port));
1508 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1510 dpif_port_destroy(&dpif_port);
1512 if (ofproto->sflow) {
1513 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1520 port_destruct(struct ofport *port_)
1522 struct ofport_dpif *port = ofport_dpif_cast(port_);
1523 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1524 const char *devname = netdev_get_name(port->up.netdev);
1525 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1526 const char *dp_port_name;
1528 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1529 ovs_rwlock_wrlock(&xlate_rwlock);
1530 xlate_ofport_remove(port);
1531 ovs_rwlock_unlock(&xlate_rwlock);
1533 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1535 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1536 /* The underlying device is still there, so delete it. This
1537 * happens when the ofproto is being destroyed, since the caller
1538 * assumes that removal of attached ports will happen as part of
1540 if (!port->is_tunnel) {
1541 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1546 port->peer->peer = NULL;
1550 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1551 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1552 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1553 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1557 sset_find_and_delete(&ofproto->ports, devname);
1558 sset_find_and_delete(&ofproto->ghost_ports, devname);
1559 bundle_remove(port_);
1560 set_cfm(port_, NULL);
1561 set_bfd(port_, NULL);
1562 if (port->stp_port) {
1563 stp_port_disable(port->stp_port);
1565 if (ofproto->sflow) {
1566 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1573 port_modified(struct ofport *port_)
1575 struct ofport_dpif *port = ofport_dpif_cast(port_);
1577 if (port->bundle && port->bundle->bond) {
1578 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1582 cfm_set_netdev(port->cfm, port->up.netdev);
1586 bfd_set_netdev(port->bfd, port->up.netdev);
1589 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1590 port->up.pp.hw_addr);
1592 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1594 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1598 ofport_update_peer(port);
1602 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1604 struct ofport_dpif *port = ofport_dpif_cast(port_);
1605 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1606 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1608 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1609 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1610 OFPUTIL_PC_NO_PACKET_IN)) {
1611 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1613 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1614 bundle_update(port->bundle);
1620 set_sflow(struct ofproto *ofproto_,
1621 const struct ofproto_sflow_options *sflow_options)
1623 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1624 struct dpif_sflow *ds = ofproto->sflow;
1626 if (sflow_options) {
1628 struct ofport_dpif *ofport;
1630 ds = ofproto->sflow = dpif_sflow_create();
1631 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1632 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1634 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1636 dpif_sflow_set_options(ds, sflow_options);
1639 dpif_sflow_unref(ds);
1640 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1641 ofproto->sflow = NULL;
1649 struct ofproto *ofproto_,
1650 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1651 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1652 size_t n_flow_exporters_options)
1654 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1655 struct dpif_ipfix *di = ofproto->ipfix;
1656 bool has_options = bridge_exporter_options || flow_exporters_options;
1658 if (has_options && !di) {
1659 di = ofproto->ipfix = dpif_ipfix_create();
1663 /* Call set_options in any case to cleanly flush the flow
1664 * caches in the last exporters that are to be destroyed. */
1665 dpif_ipfix_set_options(
1666 di, bridge_exporter_options, flow_exporters_options,
1667 n_flow_exporters_options);
1670 dpif_ipfix_unref(di);
1671 ofproto->ipfix = NULL;
1679 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1681 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1686 struct ofproto_dpif *ofproto;
1688 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1689 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1690 ofport->cfm = cfm_create(ofport->up.netdev);
1693 if (cfm_configure(ofport->cfm, s)) {
1700 cfm_unref(ofport->cfm);
1703 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1704 ofport->up.pp.hw_addr);
1709 get_cfm_status(const struct ofport *ofport_,
1710 struct ofproto_cfm_status *status)
1712 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1715 status->faults = cfm_get_fault(ofport->cfm);
1716 status->flap_count = cfm_get_flap_count(ofport->cfm);
1717 status->remote_opstate = cfm_get_opup(ofport->cfm);
1718 status->health = cfm_get_health(ofport->cfm);
1719 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1727 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1729 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1730 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1734 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1735 cfg, ofport->up.netdev);
1736 if (ofport->bfd != old) {
1737 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1739 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1740 ofport->up.pp.hw_addr);
1745 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1747 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1750 bfd_get_status(ofport->bfd, smap);
1757 /* Spanning Tree. */
1760 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1762 struct ofproto_dpif *ofproto = ofproto_;
1763 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1764 struct ofport_dpif *ofport;
1766 ofport = stp_port_get_aux(sp);
1768 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1769 ofproto->up.name, port_num);
1771 struct eth_header *eth = pkt->l2;
1773 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1774 if (eth_addr_is_zero(eth->eth_src)) {
1775 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1776 "with unknown MAC", ofproto->up.name, port_num);
1778 ofproto_dpif_send_packet(ofport, pkt);
1784 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1786 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1788 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1790 /* Only revalidate flows if the configuration changed. */
1791 if (!s != !ofproto->stp) {
1792 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1796 if (!ofproto->stp) {
1797 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1798 send_bpdu_cb, ofproto);
1799 ofproto->stp_last_tick = time_msec();
1802 stp_set_bridge_id(ofproto->stp, s->system_id);
1803 stp_set_bridge_priority(ofproto->stp, s->priority);
1804 stp_set_hello_time(ofproto->stp, s->hello_time);
1805 stp_set_max_age(ofproto->stp, s->max_age);
1806 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1808 struct ofport *ofport;
1810 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1811 set_stp_port(ofport, NULL);
1814 stp_unref(ofproto->stp);
1815 ofproto->stp = NULL;
1822 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1824 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1828 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1829 s->designated_root = stp_get_designated_root(ofproto->stp);
1830 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1839 update_stp_port_state(struct ofport_dpif *ofport)
1841 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1842 enum stp_state state;
1844 /* Figure out new state. */
1845 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1849 if (ofport->stp_state != state) {
1850 enum ofputil_port_state of_state;
1853 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1854 netdev_get_name(ofport->up.netdev),
1855 stp_state_name(ofport->stp_state),
1856 stp_state_name(state));
1857 if (stp_learn_in_state(ofport->stp_state)
1858 != stp_learn_in_state(state)) {
1859 /* xxx Learning action flows should also be flushed. */
1860 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1861 mac_learning_flush(ofproto->ml);
1862 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1864 fwd_change = stp_forward_in_state(ofport->stp_state)
1865 != stp_forward_in_state(state);
1867 ofproto->backer->need_revalidate = REV_STP;
1868 ofport->stp_state = state;
1869 ofport->stp_state_entered = time_msec();
1871 if (fwd_change && ofport->bundle) {
1872 bundle_update(ofport->bundle);
1875 /* Update the STP state bits in the OpenFlow port description. */
1876 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1877 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1878 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1879 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1880 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1882 ofproto_port_set_state(&ofport->up, of_state);
1886 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1887 * caller is responsible for assigning STP port numbers and ensuring
1888 * there are no duplicates. */
1890 set_stp_port(struct ofport *ofport_,
1891 const struct ofproto_port_stp_settings *s)
1893 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1894 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1895 struct stp_port *sp = ofport->stp_port;
1897 if (!s || !s->enable) {
1899 ofport->stp_port = NULL;
1900 stp_port_disable(sp);
1901 update_stp_port_state(ofport);
1904 } else if (sp && stp_port_no(sp) != s->port_num
1905 && ofport == stp_port_get_aux(sp)) {
1906 /* The port-id changed, so disable the old one if it's not
1907 * already in use by another port. */
1908 stp_port_disable(sp);
1911 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1912 stp_port_enable(sp);
1914 stp_port_set_aux(sp, ofport);
1915 stp_port_set_priority(sp, s->priority);
1916 stp_port_set_path_cost(sp, s->path_cost);
1918 update_stp_port_state(ofport);
1924 get_stp_port_status(struct ofport *ofport_,
1925 struct ofproto_port_stp_status *s)
1927 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1928 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1929 struct stp_port *sp = ofport->stp_port;
1931 if (!ofproto->stp || !sp) {
1937 s->port_id = stp_port_get_id(sp);
1938 s->state = stp_port_get_state(sp);
1939 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1940 s->role = stp_port_get_role(sp);
1946 get_stp_port_stats(struct ofport *ofport_,
1947 struct ofproto_port_stp_stats *s)
1949 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1950 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1951 struct stp_port *sp = ofport->stp_port;
1953 if (!ofproto->stp || !sp) {
1959 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1965 stp_run(struct ofproto_dpif *ofproto)
1968 long long int now = time_msec();
1969 long long int elapsed = now - ofproto->stp_last_tick;
1970 struct stp_port *sp;
1973 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1974 ofproto->stp_last_tick = now;
1976 while (stp_get_changed_port(ofproto->stp, &sp)) {
1977 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1980 update_stp_port_state(ofport);
1984 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1985 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1986 mac_learning_flush(ofproto->ml);
1987 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1993 stp_wait(struct ofproto_dpif *ofproto)
1996 poll_timer_wait(1000);
2001 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2004 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2005 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2007 if (ofport->n_qdscp != n_qdscp
2008 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2009 n_qdscp * sizeof *qdscp))) {
2010 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2011 free(ofport->qdscp);
2012 ofport->qdscp = n_qdscp
2013 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2015 ofport->n_qdscp = n_qdscp;
2023 /* Expires all MAC learning entries associated with 'bundle' and forces its
2024 * ofproto to revalidate every flow.
2026 * Normally MAC learning entries are removed only from the ofproto associated
2027 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2028 * are removed from every ofproto. When patch ports and SLB bonds are in use
2029 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2030 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2031 * with the host from which it migrated. */
2033 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2035 struct ofproto_dpif *ofproto = bundle->ofproto;
2036 struct mac_learning *ml = ofproto->ml;
2037 struct mac_entry *mac, *next_mac;
2039 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2040 ovs_rwlock_wrlock(&ml->rwlock);
2041 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2042 if (mac->port.p == bundle) {
2044 struct ofproto_dpif *o;
2046 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2048 struct mac_entry *e;
2050 ovs_rwlock_wrlock(&o->ml->rwlock);
2051 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2053 mac_learning_expire(o->ml, e);
2055 ovs_rwlock_unlock(&o->ml->rwlock);
2060 mac_learning_expire(ml, mac);
2063 ovs_rwlock_unlock(&ml->rwlock);
2066 static struct ofbundle *
2067 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2069 struct ofbundle *bundle;
2071 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2072 &ofproto->bundles) {
2073 if (bundle->aux == aux) {
2081 bundle_update(struct ofbundle *bundle)
2083 struct ofport_dpif *port;
2085 bundle->floodable = true;
2086 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2087 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2089 || !stp_forward_in_state(port->stp_state)) {
2090 bundle->floodable = false;
2097 bundle_del_port(struct ofport_dpif *port)
2099 struct ofbundle *bundle = port->bundle;
2101 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2103 list_remove(&port->bundle_node);
2104 port->bundle = NULL;
2107 lacp_slave_unregister(bundle->lacp, port);
2110 bond_slave_unregister(bundle->bond, port);
2113 bundle_update(bundle);
2117 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2118 struct lacp_slave_settings *lacp)
2120 struct ofport_dpif *port;
2122 port = get_ofp_port(bundle->ofproto, ofp_port);
2127 if (port->bundle != bundle) {
2128 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2130 bundle_remove(&port->up);
2133 port->bundle = bundle;
2134 list_push_back(&bundle->ports, &port->bundle_node);
2135 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2137 || !stp_forward_in_state(port->stp_state)) {
2138 bundle->floodable = false;
2142 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2143 lacp_slave_register(bundle->lacp, port, lacp);
2150 bundle_destroy(struct ofbundle *bundle)
2152 struct ofproto_dpif *ofproto;
2153 struct ofport_dpif *port, *next_port;
2159 ofproto = bundle->ofproto;
2160 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2162 ovs_rwlock_wrlock(&xlate_rwlock);
2163 xlate_bundle_remove(bundle);
2164 ovs_rwlock_unlock(&xlate_rwlock);
2166 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2167 bundle_del_port(port);
2170 bundle_flush_macs(bundle, true);
2171 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2173 free(bundle->trunks);
2174 lacp_unref(bundle->lacp);
2175 bond_unref(bundle->bond);
2180 bundle_set(struct ofproto *ofproto_, void *aux,
2181 const struct ofproto_bundle_settings *s)
2183 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2184 bool need_flush = false;
2185 struct ofport_dpif *port;
2186 struct ofbundle *bundle;
2187 unsigned long *trunks;
2193 bundle_destroy(bundle_lookup(ofproto, aux));
2197 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2198 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2200 bundle = bundle_lookup(ofproto, aux);
2202 bundle = xmalloc(sizeof *bundle);
2204 bundle->ofproto = ofproto;
2205 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2206 hash_pointer(aux, 0));
2208 bundle->name = NULL;
2210 list_init(&bundle->ports);
2211 bundle->vlan_mode = PORT_VLAN_TRUNK;
2213 bundle->trunks = NULL;
2214 bundle->use_priority_tags = s->use_priority_tags;
2215 bundle->lacp = NULL;
2216 bundle->bond = NULL;
2218 bundle->floodable = true;
2219 mbridge_register_bundle(ofproto->mbridge, bundle);
2222 if (!bundle->name || strcmp(s->name, bundle->name)) {
2224 bundle->name = xstrdup(s->name);
2229 ofproto->lacp_enabled = true;
2230 if (!bundle->lacp) {
2231 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2232 bundle->lacp = lacp_create();
2234 lacp_configure(bundle->lacp, s->lacp);
2236 lacp_unref(bundle->lacp);
2237 bundle->lacp = NULL;
2240 /* Update set of ports. */
2242 for (i = 0; i < s->n_slaves; i++) {
2243 if (!bundle_add_port(bundle, s->slaves[i],
2244 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2248 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2249 struct ofport_dpif *next_port;
2251 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2252 for (i = 0; i < s->n_slaves; i++) {
2253 if (s->slaves[i] == port->up.ofp_port) {
2258 bundle_del_port(port);
2262 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2264 if (list_is_empty(&bundle->ports)) {
2265 bundle_destroy(bundle);
2269 /* Set VLAN tagging mode */
2270 if (s->vlan_mode != bundle->vlan_mode
2271 || s->use_priority_tags != bundle->use_priority_tags) {
2272 bundle->vlan_mode = s->vlan_mode;
2273 bundle->use_priority_tags = s->use_priority_tags;
2278 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2279 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2281 if (vlan != bundle->vlan) {
2282 bundle->vlan = vlan;
2286 /* Get trunked VLANs. */
2287 switch (s->vlan_mode) {
2288 case PORT_VLAN_ACCESS:
2292 case PORT_VLAN_TRUNK:
2293 trunks = CONST_CAST(unsigned long *, s->trunks);
2296 case PORT_VLAN_NATIVE_UNTAGGED:
2297 case PORT_VLAN_NATIVE_TAGGED:
2298 if (vlan != 0 && (!s->trunks
2299 || !bitmap_is_set(s->trunks, vlan)
2300 || bitmap_is_set(s->trunks, 0))) {
2301 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2303 trunks = bitmap_clone(s->trunks, 4096);
2305 trunks = bitmap_allocate1(4096);
2307 bitmap_set1(trunks, vlan);
2308 bitmap_set0(trunks, 0);
2310 trunks = CONST_CAST(unsigned long *, s->trunks);
2317 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2318 free(bundle->trunks);
2319 if (trunks == s->trunks) {
2320 bundle->trunks = vlan_bitmap_clone(trunks);
2322 bundle->trunks = trunks;
2327 if (trunks != s->trunks) {
2332 if (!list_is_short(&bundle->ports)) {
2333 bundle->ofproto->has_bonded_bundles = true;
2335 if (bond_reconfigure(bundle->bond, s->bond)) {
2336 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2339 bundle->bond = bond_create(s->bond);
2340 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2343 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2344 bond_slave_register(bundle->bond, port, port->up.netdev);
2347 bond_unref(bundle->bond);
2348 bundle->bond = NULL;
2351 /* If we changed something that would affect MAC learning, un-learn
2352 * everything on this port and force flow revalidation. */
2354 bundle_flush_macs(bundle, false);
2361 bundle_remove(struct ofport *port_)
2363 struct ofport_dpif *port = ofport_dpif_cast(port_);
2364 struct ofbundle *bundle = port->bundle;
2367 bundle_del_port(port);
2368 if (list_is_empty(&bundle->ports)) {
2369 bundle_destroy(bundle);
2370 } else if (list_is_short(&bundle->ports)) {
2371 bond_unref(bundle->bond);
2372 bundle->bond = NULL;
2378 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2380 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2381 struct ofport_dpif *port = port_;
2382 uint8_t ea[ETH_ADDR_LEN];
2385 error = netdev_get_etheraddr(port->up.netdev, ea);
2387 struct ofpbuf packet;
2390 ofpbuf_init(&packet, 0);
2391 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2393 memcpy(packet_pdu, pdu, pdu_size);
2395 ofproto_dpif_send_packet(port, &packet);
2396 ofpbuf_uninit(&packet);
2398 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2399 "%s (%s)", port->bundle->name,
2400 netdev_get_name(port->up.netdev), ovs_strerror(error));
2405 bundle_send_learning_packets(struct ofbundle *bundle)
2407 struct ofproto_dpif *ofproto = bundle->ofproto;
2408 struct ofpbuf *learning_packet;
2409 int error, n_packets, n_errors;
2410 struct mac_entry *e;
2411 struct list packets;
2413 list_init(&packets);
2414 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2415 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2416 if (e->port.p != bundle) {
2419 learning_packet = bond_compose_learning_packet(bundle->bond,
2422 learning_packet->private_p = port_void;
2423 list_push_back(&packets, &learning_packet->list_node);
2426 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2428 error = n_packets = n_errors = 0;
2429 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2432 ret = ofproto_dpif_send_packet(learning_packet->private_p, learning_packet);
2439 ofpbuf_list_delete(&packets);
2442 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2443 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2444 "packets, last error was: %s",
2445 bundle->name, n_errors, n_packets, ovs_strerror(error));
2447 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2448 bundle->name, n_packets);
2453 bundle_run(struct ofbundle *bundle)
2456 lacp_run(bundle->lacp, send_pdu_cb);
2459 struct ofport_dpif *port;
2461 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2462 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2465 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2466 bundle->ofproto->backer->need_revalidate = REV_BOND;
2469 if (bond_should_send_learning_packets(bundle->bond)) {
2470 bundle_send_learning_packets(bundle);
2476 bundle_wait(struct ofbundle *bundle)
2479 lacp_wait(bundle->lacp);
2482 bond_wait(bundle->bond);
2489 mirror_set__(struct ofproto *ofproto_, void *aux,
2490 const struct ofproto_mirror_settings *s)
2492 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2493 struct ofbundle **srcs, **dsts;
2498 mirror_destroy(ofproto->mbridge, aux);
2502 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2503 dsts = xmalloc(s->n_dsts * sizeof *dsts);
2505 for (i = 0; i < s->n_srcs; i++) {
2506 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2509 for (i = 0; i < s->n_dsts; i++) {
2510 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2513 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2514 s->n_dsts, s->src_vlans,
2515 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2522 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2523 uint64_t *packets, uint64_t *bytes)
2525 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2530 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2532 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2533 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2534 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2535 mac_learning_flush(ofproto->ml);
2537 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2542 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2544 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2545 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2546 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2550 forward_bpdu_changed(struct ofproto *ofproto_)
2552 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2553 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2557 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2560 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2561 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2562 mac_learning_set_idle_time(ofproto->ml, idle_time);
2563 mac_learning_set_max_entries(ofproto->ml, max_entries);
2564 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2569 static struct ofport_dpif *
2570 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2572 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2573 return ofport ? ofport_dpif_cast(ofport) : NULL;
2577 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2578 struct ofproto_port *ofproto_port,
2579 struct dpif_port *dpif_port)
2581 ofproto_port->name = dpif_port->name;
2582 ofproto_port->type = dpif_port->type;
2583 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2587 ofport_update_peer(struct ofport_dpif *ofport)
2589 const struct ofproto_dpif *ofproto;
2590 struct dpif_backer *backer;
2593 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2597 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2598 backer->need_revalidate = REV_RECONFIGURE;
2601 ofport->peer->peer = NULL;
2602 ofport->peer = NULL;
2605 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2610 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2611 struct ofport *peer_ofport;
2612 struct ofport_dpif *peer;
2615 if (ofproto->backer != backer) {
2619 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2624 peer = ofport_dpif_cast(peer_ofport);
2625 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2626 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2628 ofport->peer = peer;
2629 ofport->peer->peer = ofport;
2639 port_run(struct ofport_dpif *ofport)
2641 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2642 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2643 bool enable = netdev_get_carrier(ofport->up.netdev);
2644 bool cfm_enable = false;
2645 bool bfd_enable = false;
2647 ofport->carrier_seq = carrier_seq;
2650 int cfm_opup = cfm_get_opup(ofport->cfm);
2652 cfm_enable = !cfm_get_fault(ofport->cfm);
2654 if (cfm_opup >= 0) {
2655 cfm_enable = cfm_enable && cfm_opup;
2660 bfd_enable = bfd_forwarding(ofport->bfd);
2663 if (ofport->bfd || ofport->cfm) {
2664 enable = enable && (cfm_enable || bfd_enable);
2667 if (ofport->bundle) {
2668 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2669 if (carrier_changed) {
2670 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2674 if (ofport->may_enable != enable) {
2675 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2676 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2679 ofport->may_enable = enable;
2683 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2684 struct ofproto_port *ofproto_port)
2686 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2687 struct dpif_port dpif_port;
2690 if (sset_contains(&ofproto->ghost_ports, devname)) {
2691 const char *type = netdev_get_type_from_name(devname);
2693 /* We may be called before ofproto->up.port_by_name is populated with
2694 * the appropriate ofport. For this reason, we must get the name and
2695 * type from the netdev layer directly. */
2697 const struct ofport *ofport;
2699 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2700 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2701 ofproto_port->name = xstrdup(devname);
2702 ofproto_port->type = xstrdup(type);
2708 if (!sset_contains(&ofproto->ports, devname)) {
2711 error = dpif_port_query_by_name(ofproto->backer->dpif,
2712 devname, &dpif_port);
2714 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2720 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2722 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2723 const char *devname = netdev_get_name(netdev);
2724 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2725 const char *dp_port_name;
2727 if (netdev_vport_is_patch(netdev)) {
2728 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2732 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2733 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2734 odp_port_t port_no = ODPP_NONE;
2737 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2741 if (netdev_get_tunnel_config(netdev)) {
2742 simap_put(&ofproto->backer->tnl_backers,
2743 dp_port_name, odp_to_u32(port_no));
2747 if (netdev_get_tunnel_config(netdev)) {
2748 sset_add(&ofproto->ghost_ports, devname);
2750 sset_add(&ofproto->ports, devname);
2756 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2758 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2759 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2766 sset_find_and_delete(&ofproto->ghost_ports,
2767 netdev_get_name(ofport->up.netdev));
2768 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2769 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2770 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2772 /* The caller is going to close ofport->up.netdev. If this is a
2773 * bonded port, then the bond is using that netdev, so remove it
2774 * from the bond. The client will need to reconfigure everything
2775 * after deleting ports, so then the slave will get re-added. */
2776 bundle_remove(&ofport->up);
2783 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2785 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2788 error = netdev_get_stats(ofport->up.netdev, stats);
2790 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2791 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2793 ovs_mutex_lock(&ofproto->stats_mutex);
2794 /* ofproto->stats.tx_packets represents packets that we created
2795 * internally and sent to some port (e.g. packets sent with
2796 * ofproto_dpif_send_packet()). Account for them as if they had
2797 * come from OFPP_LOCAL and got forwarded. */
2799 if (stats->rx_packets != UINT64_MAX) {
2800 stats->rx_packets += ofproto->stats.tx_packets;
2803 if (stats->rx_bytes != UINT64_MAX) {
2804 stats->rx_bytes += ofproto->stats.tx_bytes;
2807 /* ofproto->stats.rx_packets represents packets that were received on
2808 * some port and we processed internally and dropped (e.g. STP).
2809 * Account for them as if they had been forwarded to OFPP_LOCAL. */
2811 if (stats->tx_packets != UINT64_MAX) {
2812 stats->tx_packets += ofproto->stats.rx_packets;
2815 if (stats->tx_bytes != UINT64_MAX) {
2816 stats->tx_bytes += ofproto->stats.rx_bytes;
2818 ovs_mutex_unlock(&ofproto->stats_mutex);
2824 struct port_dump_state {
2829 struct ofproto_port port;
2834 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
2836 *statep = xzalloc(sizeof(struct port_dump_state));
2841 port_dump_next(const struct ofproto *ofproto_, void *state_,
2842 struct ofproto_port *port)
2844 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2845 struct port_dump_state *state = state_;
2846 const struct sset *sset;
2847 struct sset_node *node;
2849 if (state->has_port) {
2850 ofproto_port_destroy(&state->port);
2851 state->has_port = false;
2853 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
2854 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
2857 error = port_query_by_name(ofproto_, node->name, &state->port);
2859 *port = state->port;
2860 state->has_port = true;
2862 } else if (error != ENODEV) {
2867 if (!state->ghost) {
2868 state->ghost = true;
2871 return port_dump_next(ofproto_, state_, port);
2878 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2880 struct port_dump_state *state = state_;
2882 if (state->has_port) {
2883 ofproto_port_destroy(&state->port);
2890 port_poll(const struct ofproto *ofproto_, char **devnamep)
2892 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2894 if (ofproto->port_poll_errno) {
2895 int error = ofproto->port_poll_errno;
2896 ofproto->port_poll_errno = 0;
2900 if (sset_is_empty(&ofproto->port_poll_set)) {
2904 *devnamep = sset_pop(&ofproto->port_poll_set);
2909 port_poll_wait(const struct ofproto *ofproto_)
2911 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2912 dpif_port_poll_wait(ofproto->backer->dpif);
2916 port_is_lacp_current(const struct ofport *ofport_)
2918 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2919 return (ofport->bundle && ofport->bundle->lacp
2920 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2924 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2925 * then delete it entirely. */
2927 rule_expire(struct rule_dpif *rule)
2928 OVS_REQUIRES(ofproto_mutex)
2930 uint16_t hard_timeout, idle_timeout;
2931 long long int now = time_msec();
2934 ovs_assert(!rule->up.pending);
2936 hard_timeout = rule->up.hard_timeout;
2937 idle_timeout = rule->up.idle_timeout;
2939 /* Has 'rule' expired? */
2941 long long int modified;
2943 ovs_mutex_lock(&rule->up.mutex);
2944 modified = rule->up.modified;
2945 ovs_mutex_unlock(&rule->up.mutex);
2947 if (now > modified + hard_timeout * 1000) {
2948 reason = OFPRR_HARD_TIMEOUT;
2952 if (reason < 0 && idle_timeout) {
2955 ovs_mutex_lock(&rule->stats_mutex);
2956 used = rule->stats.used;
2957 ovs_mutex_unlock(&rule->stats_mutex);
2959 if (now > used + idle_timeout * 1000) {
2960 reason = OFPRR_IDLE_TIMEOUT;
2965 COVERAGE_INC(ofproto_dpif_expired);
2966 ofproto_rule_expire(&rule->up, reason);
2970 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
2971 * 'flow' must reflect the data in 'packet'. */
2973 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
2974 const struct flow *flow,
2975 struct rule_dpif *rule,
2976 const struct ofpact *ofpacts, size_t ofpacts_len,
2977 struct ofpbuf *packet)
2979 struct dpif_flow_stats stats;
2980 struct xlate_out xout;
2981 struct xlate_in xin;
2983 struct dpif_execute execute;
2986 ovs_assert((rule != NULL) != (ofpacts != NULL));
2988 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
2990 rule_dpif_credit_stats(rule, &stats);
2993 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
2994 xin.ofpacts = ofpacts;
2995 xin.ofpacts_len = ofpacts_len;
2996 xin.resubmit_stats = &stats;
2997 xlate_actions(&xin, &xout);
2999 in_port = flow->in_port.ofp_port;
3000 if (in_port == OFPP_NONE) {
3001 in_port = OFPP_LOCAL;
3003 execute.actions = xout.odp_actions.data;
3004 execute.actions_len = xout.odp_actions.size;
3005 execute.packet = packet;
3006 execute.md.tunnel = flow->tunnel;
3007 execute.md.skb_priority = flow->skb_priority;
3008 execute.md.pkt_mark = flow->pkt_mark;
3009 execute.md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3010 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3012 error = dpif_execute(ofproto->backer->dpif, &execute);
3014 xlate_out_uninit(&xout);
3020 rule_dpif_credit_stats(struct rule_dpif *rule,
3021 const struct dpif_flow_stats *stats)
3023 ovs_mutex_lock(&rule->stats_mutex);
3024 rule->stats.n_packets += stats->n_packets;
3025 rule->stats.n_bytes += stats->n_bytes;
3026 rule->stats.used = MAX(rule->stats.used, stats->used);
3027 ovs_mutex_unlock(&rule->stats_mutex);
3031 rule_dpif_is_fail_open(const struct rule_dpif *rule)
3033 return is_fail_open_rule(&rule->up);
3037 rule_dpif_is_table_miss(const struct rule_dpif *rule)
3039 return rule_is_table_miss(&rule->up);
3043 rule_dpif_is_internal(const struct rule_dpif *rule)
3045 return rule_is_internal(&rule->up);
3049 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3050 OVS_REQUIRES(rule->up.mutex)
3052 return rule->up.flow_cookie;
3056 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3057 uint16_t hard_timeout)
3059 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3062 /* Returns 'rule''s actions. The caller owns a reference on the returned
3063 * actions and must eventually release it (with rule_actions_unref()) to avoid
3065 struct rule_actions *
3066 rule_dpif_get_actions(const struct rule_dpif *rule)
3068 return rule_get_actions(&rule->up);
3071 /* Lookup 'flow' in table 0 of 'ofproto''s classifier.
3072 * If 'wc' is non-null, sets the fields that were relevant as part of
3073 * the lookup. Returns the table_id where a match or miss occurred.
3075 * The return value will be zero unless there was a miss and
3076 * OFPTC_TABLE_MISS_CONTINUE is in effect for the sequence of tables
3077 * where misses occur. */
3079 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
3080 struct flow_wildcards *wc, struct rule_dpif **rule)
3082 enum rule_dpif_lookup_verdict verdict;
3083 enum ofputil_port_config config = 0;
3084 uint8_t table_id = 0;
3086 verdict = rule_dpif_lookup_from_table(ofproto, flow, wc, true,
3090 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
3092 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER: {
3093 struct ofport_dpif *port;
3095 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
3097 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3098 flow->in_port.ofp_port);
3100 config = port ? port->up.pp.config : 0;
3103 case RULE_DPIF_LOOKUP_VERDICT_DROP:
3104 config = OFPUTIL_PC_NO_PACKET_IN;
3110 choose_miss_rule(config, ofproto->miss_rule,
3111 ofproto->no_packet_in_rule, rule);
3115 static struct rule_dpif *
3116 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3117 const struct flow *flow, struct flow_wildcards *wc)
3119 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3120 const struct cls_rule *cls_rule;
3121 struct rule_dpif *rule;
3123 fat_rwlock_rdlock(&cls->rwlock);
3124 if (ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3126 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3127 if (is_ip_any(flow)) {
3128 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3132 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3133 if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3134 /* We must pretend that transport ports are unavailable. */
3135 struct flow ofpc_normal_flow = *flow;
3136 ofpc_normal_flow.tp_src = htons(0);
3137 ofpc_normal_flow.tp_dst = htons(0);
3138 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3140 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM). */
3141 cls_rule = &ofproto->drop_frags_rule->up.cr;
3144 cls_rule = classifier_lookup(cls, flow, wc);
3147 cls_rule = classifier_lookup(cls, flow, wc);
3150 rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3151 rule_dpif_ref(rule);
3152 fat_rwlock_unlock(&cls->rwlock);
3157 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3158 * Stores the rule that was found in '*rule', or NULL if none was found.
3159 * Updates 'wc', if nonnull, to reflect the fields that were used during the
3162 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3163 * if none is found then the table miss configuration for that table is
3164 * honored, which can result in additional lookups in other OpenFlow tables.
3165 * In this case the function updates '*table_id' to reflect the final OpenFlow
3166 * table that was searched.
3168 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3173 * - RULE_DPIF_LOOKUP_VERDICT_MATCH if a rule (in '*rule') was found.
3175 * - RULE_DPIF_LOOKUP_VERDICT_DROP if no rule was found and a table miss
3176 * configuration specified that the packet should be dropped in this
3177 * case. (This occurs only if 'honor_table_miss' is true, because only in
3178 * this case does the table miss configuration matter.)
3180 * - RULE_DPIF_LOOKUP_VERDICT_CONTROLLER if no rule was found otherwise. */
3181 enum rule_dpif_lookup_verdict
3182 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3183 const struct flow *flow,
3184 struct flow_wildcards *wc,
3185 bool honor_table_miss,
3186 uint8_t *table_id, struct rule_dpif **rule)
3190 for (next_id = *table_id;
3191 next_id < ofproto->up.n_tables;
3192 next_id++, next_id += (next_id == TBL_INTERNAL))
3194 *table_id = next_id;
3195 *rule = rule_dpif_lookup_in_table(ofproto, *table_id, flow, wc);
3197 return RULE_DPIF_LOOKUP_VERDICT_MATCH;
3198 } else if (!honor_table_miss) {
3199 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3201 switch (table_get_config(&ofproto->up, *table_id)
3202 & OFPTC11_TABLE_MISS_MASK) {
3203 case OFPTC11_TABLE_MISS_CONTINUE:
3206 case OFPTC11_TABLE_MISS_CONTROLLER:
3207 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3209 case OFPTC11_TABLE_MISS_DROP:
3210 return RULE_DPIF_LOOKUP_VERDICT_DROP;
3215 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3218 /* Given a port configuration (specified as zero if there's no port), chooses
3219 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3220 * flow table miss. */
3222 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3223 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
3225 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
3226 rule_dpif_ref(*rule);
3230 rule_dpif_ref(struct rule_dpif *rule)
3233 ofproto_rule_ref(&rule->up);
3238 rule_dpif_unref(struct rule_dpif *rule)
3241 ofproto_rule_unref(&rule->up);
3246 complete_operation(struct rule_dpif *rule)
3247 OVS_REQUIRES(ofproto_mutex)
3249 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3251 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3252 ofoperation_complete(rule->up.pending, 0);
3255 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3257 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3260 static struct rule *
3263 struct rule_dpif *rule = xmalloc(sizeof *rule);
3268 rule_dealloc(struct rule *rule_)
3270 struct rule_dpif *rule = rule_dpif_cast(rule_);
3275 rule_construct(struct rule *rule_)
3276 OVS_NO_THREAD_SAFETY_ANALYSIS
3278 struct rule_dpif *rule = rule_dpif_cast(rule_);
3279 ovs_mutex_init_adaptive(&rule->stats_mutex);
3280 rule->stats.n_packets = 0;
3281 rule->stats.n_bytes = 0;
3282 rule->stats.used = rule->up.modified;
3287 rule_insert(struct rule *rule_)
3288 OVS_REQUIRES(ofproto_mutex)
3290 struct rule_dpif *rule = rule_dpif_cast(rule_);
3291 complete_operation(rule);
3295 rule_delete(struct rule *rule_)
3296 OVS_REQUIRES(ofproto_mutex)
3298 struct rule_dpif *rule = rule_dpif_cast(rule_);
3299 complete_operation(rule);
3303 rule_destruct(struct rule *rule_)
3305 struct rule_dpif *rule = rule_dpif_cast(rule_);
3306 ovs_mutex_destroy(&rule->stats_mutex);
3310 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3311 long long int *used)
3313 struct rule_dpif *rule = rule_dpif_cast(rule_);
3315 ovs_mutex_lock(&rule->stats_mutex);
3316 *packets = rule->stats.n_packets;
3317 *bytes = rule->stats.n_bytes;
3318 *used = rule->stats.used;
3319 ovs_mutex_unlock(&rule->stats_mutex);
3323 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3324 struct ofpbuf *packet)
3326 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3328 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3332 rule_execute(struct rule *rule, const struct flow *flow,
3333 struct ofpbuf *packet)
3335 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3336 ofpbuf_delete(packet);
3341 rule_modify_actions(struct rule *rule_, bool reset_counters)
3342 OVS_REQUIRES(ofproto_mutex)
3344 struct rule_dpif *rule = rule_dpif_cast(rule_);
3346 if (reset_counters) {
3347 ovs_mutex_lock(&rule->stats_mutex);
3348 rule->stats.n_packets = 0;
3349 rule->stats.n_bytes = 0;
3350 ovs_mutex_unlock(&rule->stats_mutex);
3353 complete_operation(rule);
3356 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3358 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3361 static struct ofgroup *
3364 struct group_dpif *group = xzalloc(sizeof *group);
3369 group_dealloc(struct ofgroup *group_)
3371 struct group_dpif *group = group_dpif_cast(group_);
3376 group_construct_stats(struct group_dpif *group)
3377 OVS_REQUIRES(group->stats_mutex)
3379 group->packet_count = 0;
3380 group->byte_count = 0;
3381 if (!group->bucket_stats) {
3382 group->bucket_stats = xcalloc(group->up.n_buckets,
3383 sizeof *group->bucket_stats);
3385 memset(group->bucket_stats, 0, group->up.n_buckets *
3386 sizeof *group->bucket_stats);
3391 group_construct(struct ofgroup *group_)
3393 struct group_dpif *group = group_dpif_cast(group_);
3394 const struct ofputil_bucket *bucket;
3396 /* Prevent group chaining because our locking structure makes it hard to
3397 * implement deadlock-free. (See xlate_group_resource_check().) */
3398 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
3399 const struct ofpact *a;
3401 OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
3402 if (a->type == OFPACT_GROUP) {
3403 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
3408 ovs_mutex_init_adaptive(&group->stats_mutex);
3409 ovs_mutex_lock(&group->stats_mutex);
3410 group_construct_stats(group);
3411 ovs_mutex_unlock(&group->stats_mutex);
3416 group_destruct__(struct group_dpif *group)
3417 OVS_REQUIRES(group->stats_mutex)
3419 free(group->bucket_stats);
3420 group->bucket_stats = NULL;
3424 group_destruct(struct ofgroup *group_)
3426 struct group_dpif *group = group_dpif_cast(group_);
3427 ovs_mutex_lock(&group->stats_mutex);
3428 group_destruct__(group);
3429 ovs_mutex_unlock(&group->stats_mutex);
3430 ovs_mutex_destroy(&group->stats_mutex);
3434 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3436 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
3437 struct group_dpif *group = group_dpif_cast(group_);
3438 struct group_dpif *victim = group_dpif_cast(victim_);
3440 ovs_mutex_lock(&group->stats_mutex);
3441 if (victim->up.n_buckets < group->up.n_buckets) {
3442 group_destruct__(group);
3444 group_construct_stats(group);
3445 ovs_mutex_unlock(&group->stats_mutex);
3447 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3453 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3455 struct group_dpif *group = group_dpif_cast(group_);
3457 ovs_mutex_lock(&group->stats_mutex);
3458 ogs->packet_count = group->packet_count;
3459 ogs->byte_count = group->byte_count;
3460 memcpy(ogs->bucket_stats, group->bucket_stats,
3461 group->up.n_buckets * sizeof *group->bucket_stats);
3462 ovs_mutex_unlock(&group->stats_mutex);
3468 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3469 struct group_dpif **group)
3470 OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3472 struct ofgroup *ofgroup;
3476 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3477 *group = found ? group_dpif_cast(ofgroup) : NULL;
3483 group_dpif_release(struct group_dpif *group)
3484 OVS_RELEASES(group->up.rwlock)
3486 ofproto_group_release(&group->up);
3490 group_dpif_get_buckets(const struct group_dpif *group,
3491 const struct list **buckets)
3493 *buckets = &group->up.buckets;
3496 enum ofp11_group_type
3497 group_dpif_get_type(const struct group_dpif *group)
3499 return group->up.type;
3502 /* Sends 'packet' out 'ofport'.
3503 * May modify 'packet'.
3504 * Returns 0 if successful, otherwise a positive errno value. */
3506 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3508 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3511 error = xlate_send_packet(ofport, packet);
3513 ovs_mutex_lock(&ofproto->stats_mutex);
3514 ofproto->stats.tx_packets++;
3515 ofproto->stats.tx_bytes += packet->size;
3516 ovs_mutex_unlock(&ofproto->stats_mutex);
3521 set_frag_handling(struct ofproto *ofproto_,
3522 enum ofp_config_flags frag_handling)
3524 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3525 if (frag_handling != OFPC_FRAG_REASM) {
3526 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3534 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3535 const struct flow *flow,
3536 const struct ofpact *ofpacts, size_t ofpacts_len)
3538 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3540 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3541 ofpacts_len, packet);
3548 set_netflow(struct ofproto *ofproto_,
3549 const struct netflow_options *netflow_options)
3551 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3553 if (netflow_options) {
3554 if (!ofproto->netflow) {
3555 ofproto->netflow = netflow_create();
3556 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3558 return netflow_set_options(ofproto->netflow, netflow_options);
3559 } else if (ofproto->netflow) {
3560 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3561 netflow_unref(ofproto->netflow);
3562 ofproto->netflow = NULL;
3569 get_netflow_ids(const struct ofproto *ofproto_,
3570 uint8_t *engine_type, uint8_t *engine_id)
3572 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3574 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3577 static struct ofproto_dpif *
3578 ofproto_dpif_lookup(const char *name)
3580 struct ofproto_dpif *ofproto;
3582 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3583 hash_string(name, 0), &all_ofproto_dpifs) {
3584 if (!strcmp(ofproto->up.name, name)) {
3592 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3593 const char *argv[], void *aux OVS_UNUSED)
3595 struct ofproto_dpif *ofproto;
3598 ofproto = ofproto_dpif_lookup(argv[1]);
3600 unixctl_command_reply_error(conn, "no such bridge");
3603 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3604 mac_learning_flush(ofproto->ml);
3605 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3607 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3608 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3609 mac_learning_flush(ofproto->ml);
3610 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3614 unixctl_command_reply(conn, "table successfully flushed");
3617 static struct ofport_dpif *
3618 ofbundle_get_a_port(const struct ofbundle *bundle)
3620 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3625 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3626 const char *argv[], void *aux OVS_UNUSED)
3628 struct ds ds = DS_EMPTY_INITIALIZER;
3629 const struct ofproto_dpif *ofproto;
3630 const struct mac_entry *e;
3632 ofproto = ofproto_dpif_lookup(argv[1]);
3634 unixctl_command_reply_error(conn, "no such bridge");
3638 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3639 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3640 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3641 struct ofbundle *bundle = e->port.p;
3642 char name[OFP_MAX_PORT_NAME_LEN];
3644 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3646 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
3647 name, e->vlan, ETH_ADDR_ARGS(e->mac),
3648 mac_entry_age(ofproto->ml, e));
3650 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3651 unixctl_command_reply(conn, ds_cstr(&ds));
3656 struct xlate_out xout;
3657 struct xlate_in xin;
3659 struct flow_wildcards wc;
3664 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
3666 struct rule_actions *actions;
3669 ds_put_char_multiple(result, '\t', level);
3671 ds_put_cstr(result, "No match\n");
3675 ovs_mutex_lock(&rule->up.mutex);
3676 cookie = rule->up.flow_cookie;
3677 ovs_mutex_unlock(&rule->up.mutex);
3679 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3680 rule ? rule->up.table_id : 0, ntohll(cookie));
3681 cls_rule_format(&rule->up.cr, result);
3682 ds_put_char(result, '\n');
3684 actions = rule_dpif_get_actions(rule);
3686 ds_put_char_multiple(result, '\t', level);
3687 ds_put_cstr(result, "OpenFlow actions=");
3688 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
3689 ds_put_char(result, '\n');
3693 trace_format_flow(struct ds *result, int level, const char *title,
3694 struct trace_ctx *trace)
3696 ds_put_char_multiple(result, '\t', level);
3697 ds_put_format(result, "%s: ", title);
3698 if (flow_equal(&trace->xin.flow, &trace->flow)) {
3699 ds_put_cstr(result, "unchanged");
3701 flow_format(result, &trace->xin.flow);
3702 trace->flow = trace->xin.flow;
3704 ds_put_char(result, '\n');
3708 trace_format_regs(struct ds *result, int level, const char *title,
3709 struct trace_ctx *trace)
3713 ds_put_char_multiple(result, '\t', level);
3714 ds_put_format(result, "%s:", title);
3715 for (i = 0; i < FLOW_N_REGS; i++) {
3716 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
3718 ds_put_char(result, '\n');
3722 trace_format_odp(struct ds *result, int level, const char *title,
3723 struct trace_ctx *trace)
3725 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
3727 ds_put_char_multiple(result, '\t', level);
3728 ds_put_format(result, "%s: ", title);
3729 format_odp_actions(result, odp_actions->data, odp_actions->size);
3730 ds_put_char(result, '\n');
3734 trace_format_megaflow(struct ds *result, int level, const char *title,
3735 struct trace_ctx *trace)
3739 ds_put_char_multiple(result, '\t', level);
3740 ds_put_format(result, "%s: ", title);
3741 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
3742 match_init(&match, &trace->flow, &trace->wc);
3743 match_format(&match, result, OFP_DEFAULT_PRIORITY);
3744 ds_put_char(result, '\n');
3748 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3750 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3751 struct ds *result = trace->result;
3753 ds_put_char(result, '\n');
3754 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3755 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3756 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
3757 trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
3758 trace_format_rule(result, recurse + 1, rule);
3762 trace_report(struct xlate_in *xin, const char *s, int recurse)
3764 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3765 struct ds *result = trace->result;
3767 ds_put_char_multiple(result, '\t', recurse);
3768 ds_put_cstr(result, s);
3769 ds_put_char(result, '\n');
3772 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
3773 * forms are supported:
3775 * - [dpname] odp_flow [-generate | packet]
3776 * - bridge br_flow [-generate | packet]
3778 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
3779 * returns a nonnull malloced error message. */
3780 static char * WARN_UNUSED_RESULT
3781 parse_flow_and_packet(int argc, const char *argv[],
3782 struct ofproto_dpif **ofprotop, struct flow *flow,
3783 struct ofpbuf **packetp)
3785 const struct dpif_backer *backer = NULL;
3786 const char *error = NULL;
3788 struct simap port_names = SIMAP_INITIALIZER(&port_names);
3789 struct ofpbuf *packet;
3790 struct ofpbuf odp_key;
3791 struct ofpbuf odp_mask;
3793 ofpbuf_init(&odp_key, 0);
3794 ofpbuf_init(&odp_mask, 0);
3796 /* Handle "-generate" or a hex string as the last argument. */
3797 if (!strcmp(argv[argc - 1], "-generate")) {
3798 packet = ofpbuf_new(0);
3801 error = eth_from_hex(argv[argc - 1], &packet);
3804 } else if (argc == 4) {
3805 /* The 3-argument form must end in "-generate' or a hex string. */
3811 /* odp_flow can have its in_port specified as a name instead of port no.
3812 * We do not yet know whether a given flow is a odp_flow or a br_flow.
3813 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3814 * we need to create a simap of name to port no. */
3816 const char *dp_type;
3817 if (!strncmp(argv[1], "ovs-", 4)) {
3818 dp_type = argv[1] + 4;
3822 backer = shash_find_data(&all_dpif_backers, dp_type);
3823 } else if (argc == 2) {
3824 struct shash_node *node;
3825 if (shash_count(&all_dpif_backers) == 1) {
3826 node = shash_first(&all_dpif_backers);
3827 backer = node->data;
3830 error = "Syntax error";
3833 if (backer && backer->dpif) {
3834 struct dpif_port dpif_port;
3835 struct dpif_port_dump port_dump;
3836 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
3837 simap_put(&port_names, dpif_port.name,
3838 odp_to_u32(dpif_port.port_no));
3842 /* Parse the flow and determine whether a datapath or
3843 * bridge is specified. If function odp_flow_key_from_string()
3844 * returns 0, the flow is a odp_flow. If function
3845 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
3846 if (!odp_flow_from_string(argv[argc - 1], &port_names,
3847 &odp_key, &odp_mask)) {
3849 error = "Cannot find the datapath";
3853 if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, flow,
3854 ofprotop, NULL, NULL, NULL, NULL)) {
3855 error = "Invalid datapath flow";
3859 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
3862 m_err = xasprintf("Bad flow syntax: %s", err);
3867 error = "Must specify bridge name";
3871 *ofprotop = ofproto_dpif_lookup(argv[1]);
3873 error = "Unknown bridge name";
3879 /* Generate a packet, if requested. */
3881 if (!packet->size) {
3882 flow_compose(packet, flow);
3884 struct pkt_metadata md = pkt_metadata_from_flow(flow);
3886 /* Use the metadata from the flow and the packet argument
3887 * to reconstruct the flow. */
3888 flow_extract(packet, &md, flow);
3893 if (error && !m_err) {
3894 m_err = xstrdup(error);
3897 ofpbuf_delete(packet);
3901 ofpbuf_uninit(&odp_key);
3902 ofpbuf_uninit(&odp_mask);
3903 simap_destroy(&port_names);
3908 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
3909 void *aux OVS_UNUSED)
3911 struct ofproto_dpif *ofproto;
3912 struct ofpbuf *packet;
3916 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3921 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
3922 unixctl_command_reply(conn, ds_cstr(&result));
3923 ds_destroy(&result);
3924 ofpbuf_delete(packet);
3926 unixctl_command_reply_error(conn, error);
3932 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
3933 const char *argv[], void *aux OVS_UNUSED)
3935 enum ofputil_protocol usable_protocols;
3936 struct ofproto_dpif *ofproto;
3937 bool enforce_consistency;
3938 struct ofpbuf ofpacts;
3939 struct ofpbuf *packet;
3944 /* Three kinds of error return values! */
3950 ofpbuf_init(&ofpacts, 0);
3952 /* Parse actions. */
3953 error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
3955 unixctl_command_reply_error(conn, error);
3960 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
3961 * consistency between the flow and the actions. With -consistent, we
3962 * enforce consistency even for a flow supported in OpenFlow 1.0. */
3963 if (!strcmp(argv[1], "-consistent")) {
3964 enforce_consistency = true;
3968 enforce_consistency = false;
3971 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3973 unixctl_command_reply_error(conn, error);
3978 /* Do the same checks as handle_packet_out() in ofproto.c.
3980 * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
3981 * strictly correct because these actions aren't in any table, but it's OK
3982 * because it 'table_id' is used only to check goto_table instructions, but
3983 * packet-outs take a list of actions and therefore it can't include
3986 * We skip the "meter" check here because meter is an instruction, not an
3987 * action, and thus cannot appear in ofpacts. */
3988 in_port = ofp_to_u16(flow.in_port.ofp_port);
3989 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
3990 unixctl_command_reply_error(conn, "invalid in_port");
3993 if (enforce_consistency) {
3994 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
3995 u16_to_ofp(ofproto->up.max_ports),
3996 0, 0, usable_protocols);
3998 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
3999 u16_to_ofp(ofproto->up.max_ports), 0, 0,
4005 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4006 unixctl_command_reply_error(conn, ds_cstr(&result));
4010 ofproto_trace(ofproto, &flow, packet, ofpacts.data, ofpacts.size, &result);
4011 unixctl_command_reply(conn, ds_cstr(&result));
4014 ds_destroy(&result);
4015 ofpbuf_delete(packet);
4016 ofpbuf_uninit(&ofpacts);
4019 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4020 * description of the results to 'ds'.
4022 * The trace follows a packet with the specified 'flow' through the flow
4023 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4024 * side effects (if it is nonnull then its flow must be 'flow').
4026 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4027 * trace, otherwise the actions are determined by a flow table lookup. */
4029 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
4030 const struct ofpbuf *packet,
4031 const struct ofpact ofpacts[], size_t ofpacts_len,
4034 struct rule_dpif *rule;
4035 struct trace_ctx trace;
4037 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4038 ds_put_cstr(ds, "Flow: ");
4039 flow_format(ds, flow);
4040 ds_put_char(ds, '\n');
4042 flow_wildcards_init_catchall(&trace.wc);
4046 rule_dpif_lookup(ofproto, flow, &trace.wc, &rule);
4048 trace_format_rule(ds, 0, rule);
4049 if (rule == ofproto->miss_rule) {
4050 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
4051 } else if (rule == ofproto->no_packet_in_rule) {
4052 ds_put_cstr(ds, "\nNo match, packets dropped because "
4053 "OFPPC_NO_PACKET_IN is set on in_port.\n");
4054 } else if (rule == ofproto->drop_frags_rule) {
4055 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
4056 "and the fragment handling mode is \"drop\".\n");
4060 if (rule || ofpacts) {
4063 xlate_in_init(&trace.xin, ofproto, flow, rule, ntohs(flow->tcp_flags),
4066 trace.xin.ofpacts = ofpacts;
4067 trace.xin.ofpacts_len = ofpacts_len;
4069 trace.xin.resubmit_hook = trace_resubmit;
4070 trace.xin.report_hook = trace_report;
4072 xlate_actions(&trace.xin, &trace.xout);
4074 ds_put_char(ds, '\n');
4075 trace_format_flow(ds, 0, "Final flow", &trace);
4076 trace_format_megaflow(ds, 0, "Megaflow", &trace);
4078 ds_put_cstr(ds, "Datapath actions: ");
4079 format_odp_actions(ds, trace.xout.odp_actions.data,
4080 trace.xout.odp_actions.size);
4082 if (trace.xout.slow) {
4083 enum slow_path_reason slow;
4085 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4086 "slow path because it:");
4088 slow = trace.xout.slow;
4090 enum slow_path_reason bit = rightmost_1bit(slow);
4092 ds_put_format(ds, "\n\t- %s.",
4093 slow_path_reason_to_explanation(bit));
4099 xlate_out_uninit(&trace.xout);
4102 rule_dpif_unref(rule);
4105 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4106 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4107 * to destroy 'ofproto_shash' and free the returned value. */
4108 static const struct shash_node **
4109 get_ofprotos(struct shash *ofproto_shash)
4111 const struct ofproto_dpif *ofproto;
4113 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4114 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4115 shash_add_nocopy(ofproto_shash, name, ofproto);
4118 return shash_sort(ofproto_shash);
4122 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4123 const char *argv[] OVS_UNUSED,
4124 void *aux OVS_UNUSED)
4126 struct ds ds = DS_EMPTY_INITIALIZER;
4127 struct shash ofproto_shash;
4128 const struct shash_node **sorted_ofprotos;
4131 shash_init(&ofproto_shash);
4132 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4133 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4134 const struct shash_node *node = sorted_ofprotos[i];
4135 ds_put_format(&ds, "%s\n", node->name);
4138 shash_destroy(&ofproto_shash);
4139 free(sorted_ofprotos);
4141 unixctl_command_reply(conn, ds_cstr(&ds));
4146 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4148 const struct shash_node **ofprotos;
4149 struct dpif_dp_stats dp_stats;
4150 struct shash ofproto_shash;
4153 dpif_get_dp_stats(backer->dpif, &dp_stats);
4155 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4156 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4158 shash_init(&ofproto_shash);
4159 ofprotos = get_ofprotos(&ofproto_shash);
4160 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4161 struct ofproto_dpif *ofproto = ofprotos[i]->data;
4162 const struct shash_node **ports;
4165 if (ofproto->backer != backer) {
4169 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4171 ports = shash_sort(&ofproto->up.port_by_name);
4172 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4173 const struct shash_node *node = ports[j];
4174 struct ofport *ofport = node->data;
4176 odp_port_t odp_port;
4178 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4181 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4182 if (odp_port != ODPP_NONE) {
4183 ds_put_format(ds, "%"PRIu32":", odp_port);
4185 ds_put_cstr(ds, "none:");
4188 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
4191 if (!netdev_get_config(ofport->netdev, &config)) {
4192 const struct smap_node **nodes;
4195 nodes = smap_sort(&config);
4196 for (i = 0; i < smap_count(&config); i++) {
4197 const struct smap_node *node = nodes[i];
4198 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4199 node->key, node->value);
4203 smap_destroy(&config);
4205 ds_put_char(ds, ')');
4206 ds_put_char(ds, '\n');
4210 shash_destroy(&ofproto_shash);
4215 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4216 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4218 struct ds ds = DS_EMPTY_INITIALIZER;
4219 const struct shash_node **backers;
4222 backers = shash_sort(&all_dpif_backers);
4223 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4224 dpif_show_backer(backers[i]->data, &ds);
4228 unixctl_command_reply(conn, ds_cstr(&ds));
4233 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4234 const struct nlattr *key, size_t key_len)
4236 struct ofproto_dpif *ofp;
4239 xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &ofp,
4240 NULL, NULL, NULL, NULL);
4241 return ofp == ofproto;
4245 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4246 int argc OVS_UNUSED, const char *argv[],
4247 void *aux OVS_UNUSED)
4249 struct ds ds = DS_EMPTY_INITIALIZER;
4250 const struct dpif_flow_stats *stats;
4251 const struct ofproto_dpif *ofproto;
4252 struct dpif_flow_dump flow_dump;
4253 const struct nlattr *actions;
4254 const struct nlattr *mask;
4255 const struct nlattr *key;
4259 bool verbosity = false;
4260 struct dpif_port dpif_port;
4261 struct dpif_port_dump port_dump;
4262 struct hmap portno_names;
4266 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4268 unixctl_command_reply_error(conn, "no such bridge");
4272 if (argc > 2 && !strcmp(argv[1], "-m")) {
4276 hmap_init(&portno_names);
4277 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4278 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4282 error = dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4286 dpif_flow_dump_state_init(ofproto->backer->dpif, &state);
4287 while (dpif_flow_dump_next(&flow_dump, state, &key, &key_len,
4288 &mask, &mask_len, &actions, &actions_len,
4290 if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
4294 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4296 ds_put_cstr(&ds, ", ");
4297 dpif_flow_stats_format(stats, &ds);
4298 ds_put_cstr(&ds, ", actions:");
4299 format_odp_actions(&ds, actions, actions_len);
4300 ds_put_char(&ds, '\n');
4302 dpif_flow_dump_state_uninit(ofproto->backer->dpif, state);
4303 error = dpif_flow_dump_done(&flow_dump);
4308 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4309 unixctl_command_reply_error(conn, ds_cstr(&ds));
4311 unixctl_command_reply(conn, ds_cstr(&ds));
4313 odp_portno_names_destroy(&portno_names);
4314 hmap_destroy(&portno_names);
4319 ofproto_dpif_unixctl_init(void)
4321 static bool registered;
4327 unixctl_command_register(
4329 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4330 1, 3, ofproto_unixctl_trace, NULL);
4331 unixctl_command_register(
4332 "ofproto/trace-packet-out",
4333 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4334 2, 6, ofproto_unixctl_trace_actions, NULL);
4335 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4336 ofproto_unixctl_fdb_flush, NULL);
4337 unixctl_command_register("fdb/show", "bridge", 1, 1,
4338 ofproto_unixctl_fdb_show, NULL);
4339 unixctl_command_register("dpif/dump-dps", "", 0, 0,
4340 ofproto_unixctl_dpif_dump_dps, NULL);
4341 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4343 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4344 ofproto_unixctl_dpif_dump_flows, NULL);
4348 /* Returns true if 'rule' is an internal rule, false otherwise. */
4350 rule_is_internal(const struct rule *rule)
4352 return rule->table_id == TBL_INTERNAL;
4355 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4357 * This is deprecated. It is only for compatibility with broken device drivers
4358 * in old versions of Linux that do not properly support VLANs when VLAN
4359 * devices are not used. When broken device drivers are no longer in
4360 * widespread use, we will delete these interfaces. */
4363 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4365 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4366 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4368 if (realdev_ofp_port == ofport->realdev_ofp_port
4369 && vid == ofport->vlandev_vid) {
4373 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4375 if (ofport->realdev_ofp_port) {
4378 if (realdev_ofp_port && ofport->bundle) {
4379 /* vlandevs are enslaved to their realdevs, so they are not allowed to
4380 * themselves be part of a bundle. */
4381 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
4384 ofport->realdev_ofp_port = realdev_ofp_port;
4385 ofport->vlandev_vid = vid;
4387 if (realdev_ofp_port) {
4388 vsp_add(ofport, realdev_ofp_port, vid);
4395 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4397 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4401 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4402 OVS_EXCLUDED(ofproto->vsp_mutex)
4404 /* hmap_is_empty is thread safe. */
4405 return !hmap_is_empty(&ofproto->realdev_vid_map);
4410 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4411 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4412 OVS_REQUIRES(ofproto->vsp_mutex)
4414 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4415 int vid = vlan_tci_to_vid(vlan_tci);
4416 const struct vlan_splinter *vsp;
4418 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4419 hash_realdev_vid(realdev_ofp_port, vid),
4420 &ofproto->realdev_vid_map) {
4421 if (vsp->realdev_ofp_port == realdev_ofp_port
4422 && vsp->vid == vid) {
4423 return vsp->vlandev_ofp_port;
4427 return realdev_ofp_port;
4430 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4431 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4432 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
4433 * 'vlan_tci' 9, it would return the port number of eth0.9.
4435 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4436 * function just returns its 'realdev_ofp_port' argument. */
4438 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4439 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4440 OVS_EXCLUDED(ofproto->vsp_mutex)
4444 /* hmap_is_empty is thread safe, see if we can return immediately. */
4445 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
4446 return realdev_ofp_port;
4448 ovs_mutex_lock(&ofproto->vsp_mutex);
4449 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4450 ovs_mutex_unlock(&ofproto->vsp_mutex);
4454 static struct vlan_splinter *
4455 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4457 struct vlan_splinter *vsp;
4459 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4460 hash_ofp_port(vlandev_ofp_port),
4461 &ofproto->vlandev_map) {
4462 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4470 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4471 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4472 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
4473 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4474 * eth0 and store 9 in '*vid'.
4476 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4477 * VLAN device. Unless VLAN splinters are enabled, this is what this function
4480 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4481 ofp_port_t vlandev_ofp_port, int *vid)
4482 OVS_REQUIRES(ofproto->vsp_mutex)
4484 if (!hmap_is_empty(&ofproto->vlandev_map)) {
4485 const struct vlan_splinter *vsp;
4487 vsp = vlandev_find(ofproto, vlandev_ofp_port);
4492 return vsp->realdev_ofp_port;
4498 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4499 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
4500 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4501 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
4502 * always the case unless VLAN splinters are enabled), returns false without
4503 * making any changes. */
4505 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4506 OVS_EXCLUDED(ofproto->vsp_mutex)
4511 /* hmap_is_empty is thread safe. */
4512 if (hmap_is_empty(&ofproto->vlandev_map)) {
4516 ovs_mutex_lock(&ofproto->vsp_mutex);
4517 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4518 ovs_mutex_unlock(&ofproto->vsp_mutex);
4523 /* Cause the flow to be processed as if it came in on the real device with
4524 * the VLAN device's VLAN ID. */
4525 flow->in_port.ofp_port = realdev;
4526 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4531 vsp_remove(struct ofport_dpif *port)
4533 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4534 struct vlan_splinter *vsp;
4536 ovs_mutex_lock(&ofproto->vsp_mutex);
4537 vsp = vlandev_find(ofproto, port->up.ofp_port);
4539 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4540 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4543 port->realdev_ofp_port = 0;
4545 VLOG_ERR("missing vlan device record");
4547 ovs_mutex_unlock(&ofproto->vsp_mutex);
4551 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4553 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4555 ovs_mutex_lock(&ofproto->vsp_mutex);
4556 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4557 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4558 == realdev_ofp_port)) {
4559 struct vlan_splinter *vsp;
4561 vsp = xmalloc(sizeof *vsp);
4562 vsp->realdev_ofp_port = realdev_ofp_port;
4563 vsp->vlandev_ofp_port = port->up.ofp_port;
4566 port->realdev_ofp_port = realdev_ofp_port;
4568 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4569 hash_ofp_port(port->up.ofp_port));
4570 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4571 hash_realdev_vid(realdev_ofp_port, vid));
4573 VLOG_ERR("duplicate vlan device record");
4575 ovs_mutex_unlock(&ofproto->vsp_mutex);
4579 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4581 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4582 return ofport ? ofport->odp_port : ODPP_NONE;
4585 struct ofport_dpif *
4586 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4588 struct ofport_dpif *port;
4590 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4591 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4592 &backer->odp_to_ofport_map) {
4593 if (port->odp_port == odp_port) {
4594 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4599 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4604 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4606 struct ofport_dpif *port;
4608 port = odp_port_to_ofport(ofproto->backer, odp_port);
4609 if (port && &ofproto->up == port->up.ofproto) {
4610 return port->up.ofp_port;
4617 ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto)
4619 struct dpif_backer *backer = ofproto->backer;
4621 return recirc_id_alloc(backer->rid_pool);
4625 ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id)
4627 struct dpif_backer *backer = ofproto->backer;
4629 recirc_id_free(backer->rid_pool, recirc_id);
4632 const struct ofproto_class ofproto_dpif_class = {
4646 NULL, /* get_memory_usage. */
4647 type_get_memory_usage,
4666 port_is_lacp_current,
4667 NULL, /* rule_choose_table */
4676 rule_modify_actions,
4690 get_stp_port_status,
4698 is_mirror_output_bundle,
4699 forward_bpdu_changed,
4700 set_mac_table_config,
4702 NULL, /* meter_get_features */
4703 NULL, /* meter_set */
4704 NULL, /* meter_get */
4705 NULL, /* meter_del */
4706 group_alloc, /* group_alloc */
4707 group_construct, /* group_construct */
4708 group_destruct, /* group_destruct */
4709 group_dealloc, /* group_dealloc */
4710 group_modify, /* group_modify */
4711 group_get_stats, /* group_get_stats */