2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-dpif.h"
20 #include "ofproto/ofproto-provider.h"
27 #include "byte-order.h"
28 #include "connectivity.h"
33 #include "dynamic-string.h"
34 #include "fail-open.h"
35 #include "guarded-list.h"
39 #include "mac-learning.h"
40 #include "meta-flow.h"
41 #include "multipath.h"
42 #include "netdev-vport.h"
47 #include "odp-execute.h"
50 #include "ofp-actions.h"
51 #include "ofp-parse.h"
52 #include "ofp-print.h"
53 #include "ofproto-dpif-ipfix.h"
54 #include "ofproto-dpif-mirror.h"
55 #include "ofproto-dpif-monitor.h"
56 #include "ofproto-dpif-sflow.h"
57 #include "ofproto-dpif-upcall.h"
58 #include "ofproto-dpif-xlate.h"
59 #include "poll-loop.h"
65 #include "unaligned.h"
67 #include "vlan-bitmap.h"
70 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
72 COVERAGE_DEFINE(ofproto_dpif_expired);
73 COVERAGE_DEFINE(packet_in_overflow);
75 /* Number of implemented OpenFlow tables. */
76 enum { N_TABLES = 255 };
77 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
78 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
87 * - Do include packets and bytes from datapath flows which have not
88 * recently been processed by a revalidator. */
89 struct ovs_mutex stats_mutex;
90 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
91 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
94 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes);
95 static struct rule_dpif *rule_dpif_cast(const struct rule *);
96 static void rule_expire(struct rule_dpif *);
103 * - Do include packets and bytes from datapath flows which have not
104 * recently been processed by a revalidator. */
105 struct ovs_mutex stats_mutex;
106 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
107 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
108 struct bucket_counter *bucket_stats OVS_GUARDED; /* Bucket statistics. */
112 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
113 struct ofproto_dpif *ofproto; /* Owning ofproto. */
114 void *aux; /* Key supplied by ofproto's client. */
115 char *name; /* Identifier for log messages. */
118 struct list ports; /* Contains "struct ofport"s. */
119 enum port_vlan_mode vlan_mode; /* VLAN mode */
120 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
121 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
122 * NULL if all VLANs are trunked. */
123 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
124 struct bond *bond; /* Nonnull iff more than one port. */
125 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
128 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
131 static void bundle_remove(struct ofport *);
132 static void bundle_update(struct ofbundle *);
133 static void bundle_destroy(struct ofbundle *);
134 static void bundle_del_port(struct ofport_dpif *);
135 static void bundle_run(struct ofbundle *);
136 static void bundle_wait(struct ofbundle *);
138 static void stp_run(struct ofproto_dpif *ofproto);
139 static void stp_wait(struct ofproto_dpif *ofproto);
140 static int set_stp_port(struct ofport *,
141 const struct ofproto_port_stp_settings *);
144 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
148 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
149 struct list bundle_node; /* In struct ofbundle's "ports" list. */
150 struct cfm *cfm; /* Connectivity Fault Management, if any. */
151 struct bfd *bfd; /* BFD, if any. */
152 bool may_enable; /* May be enabled in bonds. */
153 bool is_tunnel; /* This port is a tunnel. */
154 bool is_layer3; /* This is a layer 3 port. */
155 long long int carrier_seq; /* Carrier status changes. */
156 struct ofport_dpif *peer; /* Peer if patch port. */
159 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
160 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
161 long long int stp_state_entered;
163 /* Queue to DSCP mapping. */
164 struct ofproto_port_queue *qdscp;
167 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
169 * This is deprecated. It is only for compatibility with broken device
170 * drivers in old versions of Linux that do not properly support VLANs when
171 * VLAN devices are not used. When broken device drivers are no longer in
172 * widespread use, we will delete these interfaces. */
173 ofp_port_t realdev_ofp_port;
177 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
179 * This is deprecated. It is only for compatibility with broken device drivers
180 * in old versions of Linux that do not properly support VLANs when VLAN
181 * devices are not used. When broken device drivers are no longer in
182 * widespread use, we will delete these interfaces. */
183 struct vlan_splinter {
184 struct hmap_node realdev_vid_node;
185 struct hmap_node vlandev_node;
186 ofp_port_t realdev_ofp_port;
187 ofp_port_t vlandev_ofp_port;
191 static void vsp_remove(struct ofport_dpif *);
192 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
194 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
197 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
200 static struct ofport_dpif *
201 ofport_dpif_cast(const struct ofport *ofport)
203 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
206 static void port_run(struct ofport_dpif *);
207 static int set_bfd(struct ofport *, const struct smap *);
208 static int set_cfm(struct ofport *, const struct cfm_settings *);
209 static void ofport_update_peer(struct ofport_dpif *);
211 struct dpif_completion {
212 struct list list_node;
213 struct ofoperation *op;
216 /* Reasons that we might need to revalidate every datapath flow, and
217 * corresponding coverage counters.
219 * A value of 0 means that there is no need to revalidate.
221 * It would be nice to have some cleaner way to integrate with coverage
222 * counters, but with only a few reasons I guess this is good enough for
224 enum revalidate_reason {
225 REV_RECONFIGURE = 1, /* Switch configuration changed. */
226 REV_STP, /* Spanning tree protocol port status change. */
227 REV_BOND, /* Bonding changed. */
228 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
229 REV_FLOW_TABLE, /* Flow table changed. */
230 REV_MAC_LEARNING, /* Mac learning changed. */
232 COVERAGE_DEFINE(rev_reconfigure);
233 COVERAGE_DEFINE(rev_stp);
234 COVERAGE_DEFINE(rev_bond);
235 COVERAGE_DEFINE(rev_port_toggled);
236 COVERAGE_DEFINE(rev_flow_table);
237 COVERAGE_DEFINE(rev_mac_learning);
239 /* All datapaths of a given type share a single dpif backer instance. */
246 struct ovs_rwlock odp_to_ofport_lock;
247 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
249 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
251 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
253 bool recv_set_enable; /* Enables or disables receiving packets. */
255 /* True if the datapath supports variable-length
256 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
257 * False if the datapath supports only 8-byte (or shorter) userdata. */
258 bool variable_length_userdata;
261 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
262 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
264 struct ofproto_dpif {
265 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
267 struct dpif_backer *backer;
269 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
271 /* Special OpenFlow rules. */
272 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
273 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
274 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
277 struct netflow *netflow;
278 struct dpif_sflow *sflow;
279 struct dpif_ipfix *ipfix;
280 struct hmap bundles; /* Contains "struct ofbundle"s. */
281 struct mac_learning *ml;
282 bool has_bonded_bundles;
284 struct mbridge *mbridge;
286 struct ovs_mutex stats_mutex;
287 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
288 * consumed in userspace. */
292 long long int stp_last_tick;
294 /* VLAN splinters. */
295 struct ovs_mutex vsp_mutex;
296 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
297 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
300 struct sset ports; /* Set of standard port names. */
301 struct sset ghost_ports; /* Ports with no datapath port. */
302 struct sset port_poll_set; /* Queued names for port_poll() reply. */
303 int port_poll_errno; /* Last errno for port_poll() reply. */
304 uint64_t change_seq; /* Connectivity status changes. */
307 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
310 /* All existing ofproto_dpif instances, indexed by ->up.name. */
311 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
313 static void ofproto_dpif_unixctl_init(void);
315 static inline struct ofproto_dpif *
316 ofproto_dpif_cast(const struct ofproto *ofproto)
318 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
319 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
322 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
323 ofp_port_t ofp_port);
324 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
325 const struct ofpbuf *packet,
326 const struct ofpact[], size_t ofpacts_len,
329 /* Global variables. */
330 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
332 /* Initial mappings of port to bridge mappings. */
333 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
335 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
338 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
339 struct ofputil_flow_mod *fm)
341 ofproto_flow_mod(&ofproto->up, fm);
344 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
345 * Takes ownership of 'pin' and pin->packet. */
347 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
348 struct ofproto_packet_in *pin)
350 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
351 COVERAGE_INC(packet_in_overflow);
352 free(CONST_CAST(void *, pin->up.packet));
357 /* Factory functions. */
360 init(const struct shash *iface_hints)
362 struct shash_node *node;
364 /* Make a local copy, since we don't own 'iface_hints' elements. */
365 SHASH_FOR_EACH(node, iface_hints) {
366 const struct iface_hint *orig_hint = node->data;
367 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
369 new_hint->br_name = xstrdup(orig_hint->br_name);
370 new_hint->br_type = xstrdup(orig_hint->br_type);
371 new_hint->ofp_port = orig_hint->ofp_port;
373 shash_add(&init_ofp_ports, node->name, new_hint);
378 enumerate_types(struct sset *types)
380 dp_enumerate_types(types);
384 enumerate_names(const char *type, struct sset *names)
386 struct ofproto_dpif *ofproto;
389 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
390 if (strcmp(type, ofproto->up.type)) {
393 sset_add(names, ofproto->up.name);
400 del(const char *type, const char *name)
405 error = dpif_open(name, type, &dpif);
407 error = dpif_delete(dpif);
414 port_open_type(const char *datapath_type, const char *port_type)
416 return dpif_port_open_type(datapath_type, port_type);
419 /* Type functions. */
421 static void process_dpif_port_changes(struct dpif_backer *);
422 static void process_dpif_all_ports_changed(struct dpif_backer *);
423 static void process_dpif_port_change(struct dpif_backer *,
424 const char *devname);
425 static void process_dpif_port_error(struct dpif_backer *, int error);
427 static struct ofproto_dpif *
428 lookup_ofproto_dpif_by_port_name(const char *name)
430 struct ofproto_dpif *ofproto;
432 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
433 if (sset_contains(&ofproto->ports, name)) {
442 type_run(const char *type)
444 struct dpif_backer *backer;
446 backer = shash_find_data(&all_dpif_backers, type);
448 /* This is not necessarily a problem, since backers are only
449 * created on demand. */
453 dpif_run(backer->dpif);
455 /* If vswitchd started with other_config:flow_restore_wait set as "true",
456 * and the configuration has now changed to "false", enable receiving
457 * packets from the datapath. */
458 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
461 backer->recv_set_enable = true;
463 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
465 VLOG_ERR("Failed to enable receiving packets in dpif.");
468 dpif_flow_flush(backer->dpif);
469 backer->need_revalidate = REV_RECONFIGURE;
472 if (backer->recv_set_enable) {
473 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
476 if (backer->need_revalidate) {
477 struct ofproto_dpif *ofproto;
478 struct simap_node *node;
479 struct simap tmp_backers;
481 /* Handle tunnel garbage collection. */
482 simap_init(&tmp_backers);
483 simap_swap(&backer->tnl_backers, &tmp_backers);
485 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
486 struct ofport_dpif *iter;
488 if (backer != ofproto->backer) {
492 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
493 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
496 if (!iter->is_tunnel) {
500 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
501 namebuf, sizeof namebuf);
502 node = simap_find(&tmp_backers, dp_port);
504 simap_put(&backer->tnl_backers, dp_port, node->data);
505 simap_delete(&tmp_backers, node);
506 node = simap_find(&backer->tnl_backers, dp_port);
508 node = simap_find(&backer->tnl_backers, dp_port);
510 odp_port_t odp_port = ODPP_NONE;
512 if (!dpif_port_add(backer->dpif, iter->up.netdev,
514 simap_put(&backer->tnl_backers, dp_port,
515 odp_to_u32(odp_port));
516 node = simap_find(&backer->tnl_backers, dp_port);
521 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
522 if (tnl_port_reconfigure(iter, iter->up.netdev,
524 backer->need_revalidate = REV_RECONFIGURE;
529 SIMAP_FOR_EACH (node, &tmp_backers) {
530 dpif_port_del(backer->dpif, u32_to_odp(node->data));
532 simap_destroy(&tmp_backers);
534 switch (backer->need_revalidate) {
535 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
536 case REV_STP: COVERAGE_INC(rev_stp); break;
537 case REV_BOND: COVERAGE_INC(rev_bond); break;
538 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
539 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
540 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
542 backer->need_revalidate = 0;
544 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
545 struct ofport_dpif *ofport;
546 struct ofbundle *bundle;
548 if (ofproto->backer != backer) {
552 ovs_rwlock_wrlock(&xlate_rwlock);
553 xlate_ofproto_set(ofproto, ofproto->up.name,
554 ofproto->backer->dpif, ofproto->miss_rule,
555 ofproto->no_packet_in_rule, ofproto->ml,
556 ofproto->stp, ofproto->mbridge,
557 ofproto->sflow, ofproto->ipfix,
558 ofproto->netflow, ofproto->up.frag_handling,
559 ofproto->up.forward_bpdu,
560 connmgr_has_in_band(ofproto->up.connmgr),
561 ofproto->backer->variable_length_userdata);
563 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
564 xlate_bundle_set(ofproto, bundle, bundle->name,
565 bundle->vlan_mode, bundle->vlan,
566 bundle->trunks, bundle->use_priority_tags,
567 bundle->bond, bundle->lacp,
571 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
572 int stp_port = ofport->stp_port
573 ? stp_port_no(ofport->stp_port)
575 xlate_ofport_set(ofproto, ofport->bundle, ofport,
576 ofport->up.ofp_port, ofport->odp_port,
577 ofport->up.netdev, ofport->cfm,
578 ofport->bfd, ofport->peer, stp_port,
579 ofport->qdscp, ofport->n_qdscp,
580 ofport->up.pp.config, ofport->up.pp.state,
581 ofport->is_tunnel, ofport->may_enable);
583 ovs_rwlock_unlock(&xlate_rwlock);
586 udpif_revalidate(backer->udpif);
589 process_dpif_port_changes(backer);
594 /* Check for and handle port changes in 'backer''s dpif. */
596 process_dpif_port_changes(struct dpif_backer *backer)
602 error = dpif_port_poll(backer->dpif, &devname);
608 process_dpif_all_ports_changed(backer);
612 process_dpif_port_change(backer, devname);
617 process_dpif_port_error(backer, error);
624 process_dpif_all_ports_changed(struct dpif_backer *backer)
626 struct ofproto_dpif *ofproto;
627 struct dpif_port dpif_port;
628 struct dpif_port_dump dump;
629 struct sset devnames;
632 sset_init(&devnames);
633 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
634 if (ofproto->backer == backer) {
635 struct ofport *ofport;
637 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
638 sset_add(&devnames, netdev_get_name(ofport->netdev));
642 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
643 sset_add(&devnames, dpif_port.name);
646 SSET_FOR_EACH (devname, &devnames) {
647 process_dpif_port_change(backer, devname);
649 sset_destroy(&devnames);
653 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
655 struct ofproto_dpif *ofproto;
656 struct dpif_port port;
658 /* Don't report on the datapath's device. */
659 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
663 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
664 &all_ofproto_dpifs) {
665 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
670 ofproto = lookup_ofproto_dpif_by_port_name(devname);
671 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
672 /* The port was removed. If we know the datapath,
673 * report it through poll_set(). If we don't, it may be
674 * notifying us of a removal we initiated, so ignore it.
675 * If there's a pending ENOBUFS, let it stand, since
676 * everything will be reevaluated. */
677 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
678 sset_add(&ofproto->port_poll_set, devname);
679 ofproto->port_poll_errno = 0;
681 } else if (!ofproto) {
682 /* The port was added, but we don't know with which
683 * ofproto we should associate it. Delete it. */
684 dpif_port_del(backer->dpif, port.port_no);
686 struct ofport_dpif *ofport;
688 ofport = ofport_dpif_cast(shash_find_data(
689 &ofproto->up.port_by_name, devname));
691 && ofport->odp_port != port.port_no
692 && !odp_port_to_ofport(backer, port.port_no))
694 /* 'ofport''s datapath port number has changed from
695 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
696 * structures to match. */
697 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
698 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
699 ofport->odp_port = port.port_no;
700 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
701 hash_odp_port(port.port_no));
702 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
703 backer->need_revalidate = REV_RECONFIGURE;
706 dpif_port_destroy(&port);
709 /* Propagate 'error' to all ofprotos based on 'backer'. */
711 process_dpif_port_error(struct dpif_backer *backer, int error)
713 struct ofproto_dpif *ofproto;
715 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
716 if (ofproto->backer == backer) {
717 sset_clear(&ofproto->port_poll_set);
718 ofproto->port_poll_errno = error;
724 type_wait(const char *type)
726 struct dpif_backer *backer;
728 backer = shash_find_data(&all_dpif_backers, type);
730 /* This is not necessarily a problem, since backers are only
731 * created on demand. */
735 dpif_wait(backer->dpif);
738 /* Basic life-cycle. */
740 static int add_internal_flows(struct ofproto_dpif *);
742 static struct ofproto *
745 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
750 dealloc(struct ofproto *ofproto_)
752 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
757 close_dpif_backer(struct dpif_backer *backer)
759 ovs_assert(backer->refcount > 0);
761 if (--backer->refcount) {
765 udpif_destroy(backer->udpif);
767 simap_destroy(&backer->tnl_backers);
768 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
769 hmap_destroy(&backer->odp_to_ofport_map);
770 shash_find_and_delete(&all_dpif_backers, backer->type);
772 dpif_close(backer->dpif);
777 /* Datapath port slated for removal from datapath. */
779 struct list list_node;
783 static bool check_variable_length_userdata(struct dpif_backer *backer);
786 open_dpif_backer(const char *type, struct dpif_backer **backerp)
788 struct dpif_backer *backer;
789 struct dpif_port_dump port_dump;
790 struct dpif_port port;
791 struct shash_node *node;
792 struct list garbage_list;
793 struct odp_garbage *garbage, *next;
799 backer = shash_find_data(&all_dpif_backers, type);
806 backer_name = xasprintf("ovs-%s", type);
808 /* Remove any existing datapaths, since we assume we're the only
809 * userspace controlling the datapath. */
811 dp_enumerate_names(type, &names);
812 SSET_FOR_EACH(name, &names) {
813 struct dpif *old_dpif;
815 /* Don't remove our backer if it exists. */
816 if (!strcmp(name, backer_name)) {
820 if (dpif_open(name, type, &old_dpif)) {
821 VLOG_WARN("couldn't open old datapath %s to remove it", name);
823 dpif_delete(old_dpif);
824 dpif_close(old_dpif);
827 sset_destroy(&names);
829 backer = xmalloc(sizeof *backer);
831 error = dpif_create_and_open(backer_name, type, &backer->dpif);
834 VLOG_ERR("failed to open datapath of type %s: %s", type,
835 ovs_strerror(error));
839 backer->udpif = udpif_create(backer, backer->dpif);
841 backer->type = xstrdup(type);
842 backer->refcount = 1;
843 hmap_init(&backer->odp_to_ofport_map);
844 ovs_rwlock_init(&backer->odp_to_ofport_lock);
845 backer->need_revalidate = 0;
846 simap_init(&backer->tnl_backers);
847 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
850 if (backer->recv_set_enable) {
851 dpif_flow_flush(backer->dpif);
854 /* Loop through the ports already on the datapath and remove any
855 * that we don't need anymore. */
856 list_init(&garbage_list);
857 dpif_port_dump_start(&port_dump, backer->dpif);
858 while (dpif_port_dump_next(&port_dump, &port)) {
859 node = shash_find(&init_ofp_ports, port.name);
860 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
861 garbage = xmalloc(sizeof *garbage);
862 garbage->odp_port = port.port_no;
863 list_push_front(&garbage_list, &garbage->list_node);
866 dpif_port_dump_done(&port_dump);
868 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
869 dpif_port_del(backer->dpif, garbage->odp_port);
870 list_remove(&garbage->list_node);
874 shash_add(&all_dpif_backers, type, backer);
876 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
878 VLOG_ERR("failed to listen on datapath of type %s: %s",
879 type, ovs_strerror(error));
880 close_dpif_backer(backer);
883 backer->variable_length_userdata = check_variable_length_userdata(backer);
885 if (backer->recv_set_enable) {
886 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
892 /* Tests whether 'backer''s datapath supports variable-length
893 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
894 * to disable some features on older datapaths that don't support this
897 * Returns false if 'backer' definitely does not support variable-length
898 * userdata, true if it seems to support them or if at least the error we get
901 check_variable_length_userdata(struct dpif_backer *backer)
903 struct eth_header *eth;
904 struct ofpbuf actions;
905 struct dpif_execute execute;
906 struct ofpbuf packet;
910 /* Compose a userspace action that will cause an ERANGE error on older
911 * datapaths that don't support variable-length userdata.
913 * We really test for using userdata longer than 8 bytes, but older
914 * datapaths accepted these, silently truncating the userdata to 8 bytes.
915 * The same older datapaths rejected userdata shorter than 8 bytes, so we
916 * test for that instead as a proxy for longer userdata support. */
917 ofpbuf_init(&actions, 64);
918 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
919 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
920 dpif_port_get_pid(backer->dpif, ODPP_NONE));
921 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
922 nl_msg_end_nested(&actions, start);
924 /* Compose a dummy ethernet packet. */
925 ofpbuf_init(&packet, ETH_HEADER_LEN);
926 eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
927 eth->eth_type = htons(0x1234);
929 /* Execute the actions. On older datapaths this fails with ERANGE, on
930 * newer datapaths it succeeds. */
931 execute.actions = actions.data;
932 execute.actions_len = actions.size;
933 execute.packet = &packet;
934 execute.md = PKT_METADATA_INITIALIZER(0);
935 execute.needs_help = false;
937 error = dpif_execute(backer->dpif, &execute);
939 ofpbuf_uninit(&packet);
940 ofpbuf_uninit(&actions);
944 /* Variable-length userdata is supported.
946 * Purge received packets to avoid processing the nonsense packet we
947 * sent to userspace, then report success. */
948 dpif_recv_purge(backer->dpif);
952 /* Variable-length userdata is not supported. */
953 VLOG_WARN("%s: datapath does not support variable-length userdata "
954 "feature (needs Linux 3.10+ or kernel module from OVS "
955 "1..11+). The NXAST_SAMPLE action will be ignored.",
956 dpif_name(backer->dpif));
960 /* Something odd happened. We're not sure whether variable-length
961 * userdata is supported. Default to "yes". */
962 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
963 dpif_name(backer->dpif), ovs_strerror(error));
969 construct(struct ofproto *ofproto_)
971 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
972 struct shash_node *node, *next;
975 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
980 ofproto->netflow = NULL;
981 ofproto->sflow = NULL;
982 ofproto->ipfix = NULL;
984 ofproto->dump_seq = 0;
985 hmap_init(&ofproto->bundles);
986 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
987 ofproto->mbridge = mbridge_create();
988 ofproto->has_bonded_bundles = false;
989 ofproto->lacp_enabled = false;
990 ovs_mutex_init(&ofproto->stats_mutex);
991 ovs_mutex_init(&ofproto->vsp_mutex);
993 guarded_list_init(&ofproto->pins);
995 ofproto_dpif_unixctl_init();
997 hmap_init(&ofproto->vlandev_map);
998 hmap_init(&ofproto->realdev_vid_map);
1000 sset_init(&ofproto->ports);
1001 sset_init(&ofproto->ghost_ports);
1002 sset_init(&ofproto->port_poll_set);
1003 ofproto->port_poll_errno = 0;
1004 ofproto->change_seq = 0;
1006 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1007 struct iface_hint *iface_hint = node->data;
1009 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1010 /* Check if the datapath already has this port. */
1011 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1012 sset_add(&ofproto->ports, node->name);
1015 free(iface_hint->br_name);
1016 free(iface_hint->br_type);
1018 shash_delete(&init_ofp_ports, node);
1022 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1023 hash_string(ofproto->up.name, 0));
1024 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1026 ofproto_init_tables(ofproto_, N_TABLES);
1027 error = add_internal_flows(ofproto);
1028 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1034 add_internal_flow(struct ofproto_dpif *ofproto, int id,
1035 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1037 struct ofputil_flow_mod fm;
1040 match_init_catchall(&fm.match);
1042 match_set_reg(&fm.match, 0, id);
1043 fm.new_cookie = htonll(0);
1044 fm.cookie = htonll(0);
1045 fm.cookie_mask = htonll(0);
1046 fm.modify_cookie = false;
1047 fm.table_id = TBL_INTERNAL;
1048 fm.command = OFPFC_ADD;
1049 fm.idle_timeout = 0;
1050 fm.hard_timeout = 0;
1054 fm.ofpacts = ofpacts->data;
1055 fm.ofpacts_len = ofpacts->size;
1057 error = ofproto_flow_mod(&ofproto->up, &fm);
1059 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1060 id, ofperr_to_string(error));
1064 if (rule_dpif_lookup_in_table(ofproto, &fm.match.flow, NULL, TBL_INTERNAL,
1066 rule_dpif_unref(*rulep);
1075 add_internal_flows(struct ofproto_dpif *ofproto)
1077 struct ofpact_controller *controller;
1078 uint64_t ofpacts_stub[128 / 8];
1079 struct ofpbuf ofpacts;
1083 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1086 controller = ofpact_put_CONTROLLER(&ofpacts);
1087 controller->max_len = UINT16_MAX;
1088 controller->controller_id = 0;
1089 controller->reason = OFPR_NO_MATCH;
1090 ofpact_pad(&ofpacts);
1092 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1097 ofpbuf_clear(&ofpacts);
1098 error = add_internal_flow(ofproto, id++, &ofpacts,
1099 &ofproto->no_packet_in_rule);
1104 error = add_internal_flow(ofproto, id++, &ofpacts,
1105 &ofproto->drop_frags_rule);
1110 destruct(struct ofproto *ofproto_)
1112 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1113 struct rule_dpif *rule, *next_rule;
1114 struct ofproto_packet_in *pin, *next_pin;
1115 struct oftable *table;
1118 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1119 ovs_rwlock_wrlock(&xlate_rwlock);
1120 xlate_remove_ofproto(ofproto);
1121 ovs_rwlock_unlock(&xlate_rwlock);
1123 /* Discard any flow_miss_batches queued up for 'ofproto', avoiding a
1124 * use-after-free error. */
1125 udpif_revalidate(ofproto->backer->udpif);
1127 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1129 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1130 struct cls_cursor cursor;
1132 fat_rwlock_rdlock(&table->cls.rwlock);
1133 cls_cursor_init(&cursor, &table->cls, NULL);
1134 fat_rwlock_unlock(&table->cls.rwlock);
1135 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1136 ofproto_rule_delete(&ofproto->up, &rule->up);
1140 guarded_list_pop_all(&ofproto->pins, &pins);
1141 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1142 list_remove(&pin->list_node);
1143 free(CONST_CAST(void *, pin->up.packet));
1146 guarded_list_destroy(&ofproto->pins);
1148 mbridge_unref(ofproto->mbridge);
1150 netflow_unref(ofproto->netflow);
1151 dpif_sflow_unref(ofproto->sflow);
1152 hmap_destroy(&ofproto->bundles);
1153 mac_learning_unref(ofproto->ml);
1155 hmap_destroy(&ofproto->vlandev_map);
1156 hmap_destroy(&ofproto->realdev_vid_map);
1158 sset_destroy(&ofproto->ports);
1159 sset_destroy(&ofproto->ghost_ports);
1160 sset_destroy(&ofproto->port_poll_set);
1162 ovs_mutex_destroy(&ofproto->stats_mutex);
1163 ovs_mutex_destroy(&ofproto->vsp_mutex);
1165 close_dpif_backer(ofproto->backer);
1169 run(struct ofproto *ofproto_)
1171 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1172 uint64_t new_seq, new_dump_seq;
1174 if (mbridge_need_revalidate(ofproto->mbridge)) {
1175 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1176 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1177 mac_learning_flush(ofproto->ml);
1178 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1181 /* Do not perform any periodic activity required by 'ofproto' while
1182 * waiting for flow restore to complete. */
1183 if (!ofproto_get_flow_restore_wait()) {
1184 struct ofproto_packet_in *pin, *next_pin;
1187 guarded_list_pop_all(&ofproto->pins, &pins);
1188 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1189 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1190 list_remove(&pin->list_node);
1191 free(CONST_CAST(void *, pin->up.packet));
1196 if (ofproto->netflow) {
1197 netflow_run(ofproto->netflow);
1199 if (ofproto->sflow) {
1200 dpif_sflow_run(ofproto->sflow);
1202 if (ofproto->ipfix) {
1203 dpif_ipfix_run(ofproto->ipfix);
1206 new_seq = seq_read(connectivity_seq_get());
1207 if (ofproto->change_seq != new_seq) {
1208 struct ofport_dpif *ofport;
1210 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1214 ofproto->change_seq = new_seq;
1216 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1217 struct ofbundle *bundle;
1219 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1225 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1226 if (mac_learning_run(ofproto->ml)) {
1227 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1229 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1231 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1232 if (ofproto->dump_seq != new_dump_seq) {
1233 struct rule *rule, *next_rule;
1235 /* We know stats are relatively fresh, so now is a good time to do some
1237 ofproto->dump_seq = new_dump_seq;
1239 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1241 ovs_mutex_lock(&ofproto_mutex);
1242 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1243 &ofproto->up.expirable) {
1244 rule_expire(rule_dpif_cast(rule));
1246 ovs_mutex_unlock(&ofproto_mutex);
1248 /* All outstanding data in existing flows has been accounted, so it's a
1249 * good time to do bond rebalancing. */
1250 if (ofproto->has_bonded_bundles) {
1251 struct ofbundle *bundle;
1253 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1255 bond_rebalance(bundle->bond);
1265 wait(struct ofproto *ofproto_)
1267 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1269 if (ofproto_get_flow_restore_wait()) {
1273 if (ofproto->sflow) {
1274 dpif_sflow_wait(ofproto->sflow);
1276 if (ofproto->ipfix) {
1277 dpif_ipfix_wait(ofproto->ipfix);
1279 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1280 struct ofbundle *bundle;
1282 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1283 bundle_wait(bundle);
1286 if (ofproto->netflow) {
1287 netflow_wait(ofproto->netflow);
1289 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1290 mac_learning_wait(ofproto->ml);
1291 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1293 if (ofproto->backer->need_revalidate) {
1294 /* Shouldn't happen, but if it does just go around again. */
1295 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1296 poll_immediate_wake();
1299 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1303 type_get_memory_usage(const char *type, struct simap *usage)
1305 struct dpif_backer *backer;
1307 backer = shash_find_data(&all_dpif_backers, type);
1309 udpif_get_memory_usage(backer->udpif, usage);
1314 flush(struct ofproto *ofproto OVS_UNUSED)
1320 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1321 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1323 *arp_match_ip = true;
1324 *actions = (OFPUTIL_A_OUTPUT |
1325 OFPUTIL_A_SET_VLAN_VID |
1326 OFPUTIL_A_SET_VLAN_PCP |
1327 OFPUTIL_A_STRIP_VLAN |
1328 OFPUTIL_A_SET_DL_SRC |
1329 OFPUTIL_A_SET_DL_DST |
1330 OFPUTIL_A_SET_NW_SRC |
1331 OFPUTIL_A_SET_NW_DST |
1332 OFPUTIL_A_SET_NW_TOS |
1333 OFPUTIL_A_SET_TP_SRC |
1334 OFPUTIL_A_SET_TP_DST |
1339 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1341 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1342 struct dpif_dp_stats s;
1343 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1346 strcpy(ots->name, "classifier");
1348 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1349 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes);
1350 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes);
1351 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes);
1353 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1354 ots->lookup_count = htonll(n_lookup);
1355 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1358 static struct ofport *
1361 struct ofport_dpif *port = xmalloc(sizeof *port);
1366 port_dealloc(struct ofport *port_)
1368 struct ofport_dpif *port = ofport_dpif_cast(port_);
1373 port_construct(struct ofport *port_)
1375 struct ofport_dpif *port = ofport_dpif_cast(port_);
1376 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1377 const struct netdev *netdev = port->up.netdev;
1378 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1379 struct dpif_port dpif_port;
1382 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1383 port->bundle = NULL;
1386 port->may_enable = true;
1387 port->stp_port = NULL;
1388 port->stp_state = STP_DISABLED;
1389 port->is_tunnel = false;
1393 port->realdev_ofp_port = 0;
1394 port->vlandev_vid = 0;
1395 port->carrier_seq = netdev_get_carrier_resets(netdev);
1396 port->is_layer3 = netdev_vport_is_layer3(netdev);
1398 if (netdev_vport_is_patch(netdev)) {
1399 /* By bailing out here, we don't submit the port to the sFlow module
1400 * to be considered for counter polling export. This is correct
1401 * because the patch port represents an interface that sFlow considers
1402 * to be "internal" to the switch as a whole, and therefore not an
1403 * candidate for counter polling. */
1404 port->odp_port = ODPP_NONE;
1405 ofport_update_peer(port);
1409 error = dpif_port_query_by_name(ofproto->backer->dpif,
1410 netdev_vport_get_dpif_port(netdev, namebuf,
1417 port->odp_port = dpif_port.port_no;
1419 if (netdev_get_tunnel_config(netdev)) {
1420 tnl_port_add(port, port->up.netdev, port->odp_port);
1421 port->is_tunnel = true;
1423 /* Sanity-check that a mapping doesn't already exist. This
1424 * shouldn't happen for non-tunnel ports. */
1425 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1426 VLOG_ERR("port %s already has an OpenFlow port number",
1428 dpif_port_destroy(&dpif_port);
1432 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1433 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1434 hash_odp_port(port->odp_port));
1435 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1437 dpif_port_destroy(&dpif_port);
1439 if (ofproto->sflow) {
1440 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1447 port_destruct(struct ofport *port_)
1449 struct ofport_dpif *port = ofport_dpif_cast(port_);
1450 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1451 const char *devname = netdev_get_name(port->up.netdev);
1452 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1453 const char *dp_port_name;
1455 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1456 ovs_rwlock_wrlock(&xlate_rwlock);
1457 xlate_ofport_remove(port);
1458 ovs_rwlock_unlock(&xlate_rwlock);
1460 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1462 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1463 /* The underlying device is still there, so delete it. This
1464 * happens when the ofproto is being destroyed, since the caller
1465 * assumes that removal of attached ports will happen as part of
1467 if (!port->is_tunnel) {
1468 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1473 port->peer->peer = NULL;
1477 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1478 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1479 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1480 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1484 sset_find_and_delete(&ofproto->ports, devname);
1485 sset_find_and_delete(&ofproto->ghost_ports, devname);
1486 bundle_remove(port_);
1487 set_cfm(port_, NULL);
1488 set_bfd(port_, NULL);
1489 if (ofproto->sflow) {
1490 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1497 port_modified(struct ofport *port_)
1499 struct ofport_dpif *port = ofport_dpif_cast(port_);
1501 if (port->bundle && port->bundle->bond) {
1502 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1506 cfm_set_netdev(port->cfm, port->up.netdev);
1510 bfd_set_netdev(port->bfd, port->up.netdev);
1513 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1514 port->up.pp.hw_addr);
1516 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1518 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1522 ofport_update_peer(port);
1526 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1528 struct ofport_dpif *port = ofport_dpif_cast(port_);
1529 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1530 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1532 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1533 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1534 OFPUTIL_PC_NO_PACKET_IN)) {
1535 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1537 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1538 bundle_update(port->bundle);
1544 set_sflow(struct ofproto *ofproto_,
1545 const struct ofproto_sflow_options *sflow_options)
1547 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1548 struct dpif_sflow *ds = ofproto->sflow;
1550 if (sflow_options) {
1552 struct ofport_dpif *ofport;
1554 ds = ofproto->sflow = dpif_sflow_create();
1555 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1556 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1558 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1560 dpif_sflow_set_options(ds, sflow_options);
1563 dpif_sflow_unref(ds);
1564 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1565 ofproto->sflow = NULL;
1573 struct ofproto *ofproto_,
1574 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1575 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1576 size_t n_flow_exporters_options)
1578 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1579 struct dpif_ipfix *di = ofproto->ipfix;
1580 bool has_options = bridge_exporter_options || flow_exporters_options;
1582 if (has_options && !di) {
1583 di = ofproto->ipfix = dpif_ipfix_create();
1587 /* Call set_options in any case to cleanly flush the flow
1588 * caches in the last exporters that are to be destroyed. */
1589 dpif_ipfix_set_options(
1590 di, bridge_exporter_options, flow_exporters_options,
1591 n_flow_exporters_options);
1594 dpif_ipfix_unref(di);
1595 ofproto->ipfix = NULL;
1603 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1605 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1610 struct ofproto_dpif *ofproto;
1612 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1613 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1614 ofport->cfm = cfm_create(ofport->up.netdev);
1617 if (cfm_configure(ofport->cfm, s)) {
1624 cfm_unref(ofport->cfm);
1627 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1628 ofport->up.pp.hw_addr);
1633 get_cfm_status(const struct ofport *ofport_,
1634 struct ofproto_cfm_status *status)
1636 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1639 status->faults = cfm_get_fault(ofport->cfm);
1640 status->flap_count = cfm_get_flap_count(ofport->cfm);
1641 status->remote_opstate = cfm_get_opup(ofport->cfm);
1642 status->health = cfm_get_health(ofport->cfm);
1643 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1651 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1653 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1654 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1658 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1659 cfg, ofport->up.netdev);
1660 if (ofport->bfd != old) {
1661 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1663 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1664 ofport->up.pp.hw_addr);
1669 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1671 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1674 bfd_get_status(ofport->bfd, smap);
1681 /* Spanning Tree. */
1684 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1686 struct ofproto_dpif *ofproto = ofproto_;
1687 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1688 struct ofport_dpif *ofport;
1690 ofport = stp_port_get_aux(sp);
1692 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1693 ofproto->up.name, port_num);
1695 struct eth_header *eth = pkt->l2;
1697 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1698 if (eth_addr_is_zero(eth->eth_src)) {
1699 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1700 "with unknown MAC", ofproto->up.name, port_num);
1702 ofproto_dpif_send_packet(ofport, pkt);
1708 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1710 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1712 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1714 /* Only revalidate flows if the configuration changed. */
1715 if (!s != !ofproto->stp) {
1716 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1720 if (!ofproto->stp) {
1721 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1722 send_bpdu_cb, ofproto);
1723 ofproto->stp_last_tick = time_msec();
1726 stp_set_bridge_id(ofproto->stp, s->system_id);
1727 stp_set_bridge_priority(ofproto->stp, s->priority);
1728 stp_set_hello_time(ofproto->stp, s->hello_time);
1729 stp_set_max_age(ofproto->stp, s->max_age);
1730 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1732 struct ofport *ofport;
1734 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1735 set_stp_port(ofport, NULL);
1738 stp_unref(ofproto->stp);
1739 ofproto->stp = NULL;
1746 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1748 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1752 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1753 s->designated_root = stp_get_designated_root(ofproto->stp);
1754 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1763 update_stp_port_state(struct ofport_dpif *ofport)
1765 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1766 enum stp_state state;
1768 /* Figure out new state. */
1769 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1773 if (ofport->stp_state != state) {
1774 enum ofputil_port_state of_state;
1777 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1778 netdev_get_name(ofport->up.netdev),
1779 stp_state_name(ofport->stp_state),
1780 stp_state_name(state));
1781 if (stp_learn_in_state(ofport->stp_state)
1782 != stp_learn_in_state(state)) {
1783 /* xxx Learning action flows should also be flushed. */
1784 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1785 mac_learning_flush(ofproto->ml);
1786 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1788 fwd_change = stp_forward_in_state(ofport->stp_state)
1789 != stp_forward_in_state(state);
1791 ofproto->backer->need_revalidate = REV_STP;
1792 ofport->stp_state = state;
1793 ofport->stp_state_entered = time_msec();
1795 if (fwd_change && ofport->bundle) {
1796 bundle_update(ofport->bundle);
1799 /* Update the STP state bits in the OpenFlow port description. */
1800 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1801 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1802 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1803 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1804 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1806 ofproto_port_set_state(&ofport->up, of_state);
1810 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1811 * caller is responsible for assigning STP port numbers and ensuring
1812 * there are no duplicates. */
1814 set_stp_port(struct ofport *ofport_,
1815 const struct ofproto_port_stp_settings *s)
1817 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1818 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1819 struct stp_port *sp = ofport->stp_port;
1821 if (!s || !s->enable) {
1823 ofport->stp_port = NULL;
1824 stp_port_disable(sp);
1825 update_stp_port_state(ofport);
1828 } else if (sp && stp_port_no(sp) != s->port_num
1829 && ofport == stp_port_get_aux(sp)) {
1830 /* The port-id changed, so disable the old one if it's not
1831 * already in use by another port. */
1832 stp_port_disable(sp);
1835 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1836 stp_port_enable(sp);
1838 stp_port_set_aux(sp, ofport);
1839 stp_port_set_priority(sp, s->priority);
1840 stp_port_set_path_cost(sp, s->path_cost);
1842 update_stp_port_state(ofport);
1848 get_stp_port_status(struct ofport *ofport_,
1849 struct ofproto_port_stp_status *s)
1851 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1852 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1853 struct stp_port *sp = ofport->stp_port;
1855 if (!ofproto->stp || !sp) {
1861 s->port_id = stp_port_get_id(sp);
1862 s->state = stp_port_get_state(sp);
1863 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1864 s->role = stp_port_get_role(sp);
1870 get_stp_port_stats(struct ofport *ofport_,
1871 struct ofproto_port_stp_stats *s)
1873 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1874 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1875 struct stp_port *sp = ofport->stp_port;
1877 if (!ofproto->stp || !sp) {
1883 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1889 stp_run(struct ofproto_dpif *ofproto)
1892 long long int now = time_msec();
1893 long long int elapsed = now - ofproto->stp_last_tick;
1894 struct stp_port *sp;
1897 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1898 ofproto->stp_last_tick = now;
1900 while (stp_get_changed_port(ofproto->stp, &sp)) {
1901 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1904 update_stp_port_state(ofport);
1908 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1909 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1910 mac_learning_flush(ofproto->ml);
1911 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1917 stp_wait(struct ofproto_dpif *ofproto)
1920 poll_timer_wait(1000);
1925 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
1928 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1929 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1931 if (ofport->n_qdscp != n_qdscp
1932 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
1933 n_qdscp * sizeof *qdscp))) {
1934 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1935 free(ofport->qdscp);
1936 ofport->qdscp = n_qdscp
1937 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
1939 ofport->n_qdscp = n_qdscp;
1947 /* Expires all MAC learning entries associated with 'bundle' and forces its
1948 * ofproto to revalidate every flow.
1950 * Normally MAC learning entries are removed only from the ofproto associated
1951 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
1952 * are removed from every ofproto. When patch ports and SLB bonds are in use
1953 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
1954 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
1955 * with the host from which it migrated. */
1957 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
1959 struct ofproto_dpif *ofproto = bundle->ofproto;
1960 struct mac_learning *ml = ofproto->ml;
1961 struct mac_entry *mac, *next_mac;
1963 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1964 ovs_rwlock_wrlock(&ml->rwlock);
1965 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1966 if (mac->port.p == bundle) {
1968 struct ofproto_dpif *o;
1970 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
1972 struct mac_entry *e;
1974 ovs_rwlock_wrlock(&o->ml->rwlock);
1975 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
1977 mac_learning_expire(o->ml, e);
1979 ovs_rwlock_unlock(&o->ml->rwlock);
1984 mac_learning_expire(ml, mac);
1987 ovs_rwlock_unlock(&ml->rwlock);
1990 static struct ofbundle *
1991 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1993 struct ofbundle *bundle;
1995 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
1996 &ofproto->bundles) {
1997 if (bundle->aux == aux) {
2005 bundle_update(struct ofbundle *bundle)
2007 struct ofport_dpif *port;
2009 bundle->floodable = true;
2010 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2011 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2013 || !stp_forward_in_state(port->stp_state)) {
2014 bundle->floodable = false;
2021 bundle_del_port(struct ofport_dpif *port)
2023 struct ofbundle *bundle = port->bundle;
2025 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2027 list_remove(&port->bundle_node);
2028 port->bundle = NULL;
2031 lacp_slave_unregister(bundle->lacp, port);
2034 bond_slave_unregister(bundle->bond, port);
2037 bundle_update(bundle);
2041 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2042 struct lacp_slave_settings *lacp)
2044 struct ofport_dpif *port;
2046 port = get_ofp_port(bundle->ofproto, ofp_port);
2051 if (port->bundle != bundle) {
2052 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2054 bundle_remove(&port->up);
2057 port->bundle = bundle;
2058 list_push_back(&bundle->ports, &port->bundle_node);
2059 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2061 || !stp_forward_in_state(port->stp_state)) {
2062 bundle->floodable = false;
2066 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2067 lacp_slave_register(bundle->lacp, port, lacp);
2074 bundle_destroy(struct ofbundle *bundle)
2076 struct ofproto_dpif *ofproto;
2077 struct ofport_dpif *port, *next_port;
2083 ofproto = bundle->ofproto;
2084 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2086 ovs_rwlock_wrlock(&xlate_rwlock);
2087 xlate_bundle_remove(bundle);
2088 ovs_rwlock_unlock(&xlate_rwlock);
2090 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2091 bundle_del_port(port);
2094 bundle_flush_macs(bundle, true);
2095 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2097 free(bundle->trunks);
2098 lacp_unref(bundle->lacp);
2099 bond_unref(bundle->bond);
2104 bundle_set(struct ofproto *ofproto_, void *aux,
2105 const struct ofproto_bundle_settings *s)
2107 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2108 bool need_flush = false;
2109 struct ofport_dpif *port;
2110 struct ofbundle *bundle;
2111 unsigned long *trunks;
2117 bundle_destroy(bundle_lookup(ofproto, aux));
2121 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2122 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2124 bundle = bundle_lookup(ofproto, aux);
2126 bundle = xmalloc(sizeof *bundle);
2128 bundle->ofproto = ofproto;
2129 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2130 hash_pointer(aux, 0));
2132 bundle->name = NULL;
2134 list_init(&bundle->ports);
2135 bundle->vlan_mode = PORT_VLAN_TRUNK;
2137 bundle->trunks = NULL;
2138 bundle->use_priority_tags = s->use_priority_tags;
2139 bundle->lacp = NULL;
2140 bundle->bond = NULL;
2142 bundle->floodable = true;
2143 mbridge_register_bundle(ofproto->mbridge, bundle);
2146 if (!bundle->name || strcmp(s->name, bundle->name)) {
2148 bundle->name = xstrdup(s->name);
2153 ofproto->lacp_enabled = true;
2154 if (!bundle->lacp) {
2155 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2156 bundle->lacp = lacp_create();
2158 lacp_configure(bundle->lacp, s->lacp);
2160 lacp_unref(bundle->lacp);
2161 bundle->lacp = NULL;
2164 /* Update set of ports. */
2166 for (i = 0; i < s->n_slaves; i++) {
2167 if (!bundle_add_port(bundle, s->slaves[i],
2168 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2172 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2173 struct ofport_dpif *next_port;
2175 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2176 for (i = 0; i < s->n_slaves; i++) {
2177 if (s->slaves[i] == port->up.ofp_port) {
2182 bundle_del_port(port);
2186 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2188 if (list_is_empty(&bundle->ports)) {
2189 bundle_destroy(bundle);
2193 /* Set VLAN tagging mode */
2194 if (s->vlan_mode != bundle->vlan_mode
2195 || s->use_priority_tags != bundle->use_priority_tags) {
2196 bundle->vlan_mode = s->vlan_mode;
2197 bundle->use_priority_tags = s->use_priority_tags;
2202 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2203 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2205 if (vlan != bundle->vlan) {
2206 bundle->vlan = vlan;
2210 /* Get trunked VLANs. */
2211 switch (s->vlan_mode) {
2212 case PORT_VLAN_ACCESS:
2216 case PORT_VLAN_TRUNK:
2217 trunks = CONST_CAST(unsigned long *, s->trunks);
2220 case PORT_VLAN_NATIVE_UNTAGGED:
2221 case PORT_VLAN_NATIVE_TAGGED:
2222 if (vlan != 0 && (!s->trunks
2223 || !bitmap_is_set(s->trunks, vlan)
2224 || bitmap_is_set(s->trunks, 0))) {
2225 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2227 trunks = bitmap_clone(s->trunks, 4096);
2229 trunks = bitmap_allocate1(4096);
2231 bitmap_set1(trunks, vlan);
2232 bitmap_set0(trunks, 0);
2234 trunks = CONST_CAST(unsigned long *, s->trunks);
2241 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2242 free(bundle->trunks);
2243 if (trunks == s->trunks) {
2244 bundle->trunks = vlan_bitmap_clone(trunks);
2246 bundle->trunks = trunks;
2251 if (trunks != s->trunks) {
2256 if (!list_is_short(&bundle->ports)) {
2257 bundle->ofproto->has_bonded_bundles = true;
2259 if (bond_reconfigure(bundle->bond, s->bond)) {
2260 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2263 bundle->bond = bond_create(s->bond);
2264 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2267 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2268 bond_slave_register(bundle->bond, port, port->up.netdev);
2271 bond_unref(bundle->bond);
2272 bundle->bond = NULL;
2275 /* If we changed something that would affect MAC learning, un-learn
2276 * everything on this port and force flow revalidation. */
2278 bundle_flush_macs(bundle, false);
2285 bundle_remove(struct ofport *port_)
2287 struct ofport_dpif *port = ofport_dpif_cast(port_);
2288 struct ofbundle *bundle = port->bundle;
2291 bundle_del_port(port);
2292 if (list_is_empty(&bundle->ports)) {
2293 bundle_destroy(bundle);
2294 } else if (list_is_short(&bundle->ports)) {
2295 bond_unref(bundle->bond);
2296 bundle->bond = NULL;
2302 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2304 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2305 struct ofport_dpif *port = port_;
2306 uint8_t ea[ETH_ADDR_LEN];
2309 error = netdev_get_etheraddr(port->up.netdev, ea);
2311 struct ofpbuf packet;
2314 ofpbuf_init(&packet, 0);
2315 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2317 memcpy(packet_pdu, pdu, pdu_size);
2319 ofproto_dpif_send_packet(port, &packet);
2320 ofpbuf_uninit(&packet);
2322 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2323 "%s (%s)", port->bundle->name,
2324 netdev_get_name(port->up.netdev), ovs_strerror(error));
2329 bundle_send_learning_packets(struct ofbundle *bundle)
2331 struct ofproto_dpif *ofproto = bundle->ofproto;
2332 struct ofpbuf *learning_packet;
2333 int error, n_packets, n_errors;
2334 struct mac_entry *e;
2335 struct list packets;
2337 list_init(&packets);
2338 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2339 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2340 if (e->port.p != bundle) {
2343 learning_packet = bond_compose_learning_packet(bundle->bond,
2346 learning_packet->private_p = port_void;
2347 list_push_back(&packets, &learning_packet->list_node);
2350 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2352 error = n_packets = n_errors = 0;
2353 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2356 ret = ofproto_dpif_send_packet(learning_packet->private_p, learning_packet);
2363 ofpbuf_list_delete(&packets);
2366 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2367 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2368 "packets, last error was: %s",
2369 bundle->name, n_errors, n_packets, ovs_strerror(error));
2371 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2372 bundle->name, n_packets);
2377 bundle_run(struct ofbundle *bundle)
2380 lacp_run(bundle->lacp, send_pdu_cb);
2383 struct ofport_dpif *port;
2385 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2386 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2389 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2390 bundle->ofproto->backer->need_revalidate = REV_BOND;
2393 if (bond_should_send_learning_packets(bundle->bond)) {
2394 bundle_send_learning_packets(bundle);
2400 bundle_wait(struct ofbundle *bundle)
2403 lacp_wait(bundle->lacp);
2406 bond_wait(bundle->bond);
2413 mirror_set__(struct ofproto *ofproto_, void *aux,
2414 const struct ofproto_mirror_settings *s)
2416 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2417 struct ofbundle **srcs, **dsts;
2422 mirror_destroy(ofproto->mbridge, aux);
2426 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2427 dsts = xmalloc(s->n_dsts * sizeof *dsts);
2429 for (i = 0; i < s->n_srcs; i++) {
2430 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2433 for (i = 0; i < s->n_dsts; i++) {
2434 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2437 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2438 s->n_dsts, s->src_vlans,
2439 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2446 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2447 uint64_t *packets, uint64_t *bytes)
2449 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2454 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2456 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2457 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2458 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2459 mac_learning_flush(ofproto->ml);
2461 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2466 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2468 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2469 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2470 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2474 forward_bpdu_changed(struct ofproto *ofproto_)
2476 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2477 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2481 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2484 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2485 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2486 mac_learning_set_idle_time(ofproto->ml, idle_time);
2487 mac_learning_set_max_entries(ofproto->ml, max_entries);
2488 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2493 static struct ofport_dpif *
2494 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2496 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2497 return ofport ? ofport_dpif_cast(ofport) : NULL;
2501 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2502 struct ofproto_port *ofproto_port,
2503 struct dpif_port *dpif_port)
2505 ofproto_port->name = dpif_port->name;
2506 ofproto_port->type = dpif_port->type;
2507 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2511 ofport_update_peer(struct ofport_dpif *ofport)
2513 const struct ofproto_dpif *ofproto;
2514 struct dpif_backer *backer;
2517 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2521 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2522 backer->need_revalidate = REV_RECONFIGURE;
2525 ofport->peer->peer = NULL;
2526 ofport->peer = NULL;
2529 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2534 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2535 struct ofport *peer_ofport;
2536 struct ofport_dpif *peer;
2539 if (ofproto->backer != backer) {
2543 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2548 peer = ofport_dpif_cast(peer_ofport);
2549 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2550 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2552 ofport->peer = peer;
2553 ofport->peer->peer = ofport;
2563 port_run(struct ofport_dpif *ofport)
2565 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2566 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2567 bool enable = netdev_get_carrier(ofport->up.netdev);
2568 bool cfm_enable = false;
2569 bool bfd_enable = false;
2571 ofport->carrier_seq = carrier_seq;
2574 int cfm_opup = cfm_get_opup(ofport->cfm);
2576 cfm_enable = !cfm_get_fault(ofport->cfm);
2578 if (cfm_opup >= 0) {
2579 cfm_enable = cfm_enable && cfm_opup;
2584 bfd_enable = bfd_forwarding(ofport->bfd);
2587 if (ofport->bfd || ofport->cfm) {
2588 enable = enable && (cfm_enable || bfd_enable);
2591 if (ofport->bundle) {
2592 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2593 if (carrier_changed) {
2594 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2598 if (ofport->may_enable != enable) {
2599 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2600 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2603 ofport->may_enable = enable;
2607 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2608 struct ofproto_port *ofproto_port)
2610 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2611 struct dpif_port dpif_port;
2614 if (sset_contains(&ofproto->ghost_ports, devname)) {
2615 const char *type = netdev_get_type_from_name(devname);
2617 /* We may be called before ofproto->up.port_by_name is populated with
2618 * the appropriate ofport. For this reason, we must get the name and
2619 * type from the netdev layer directly. */
2621 const struct ofport *ofport;
2623 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2624 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2625 ofproto_port->name = xstrdup(devname);
2626 ofproto_port->type = xstrdup(type);
2632 if (!sset_contains(&ofproto->ports, devname)) {
2635 error = dpif_port_query_by_name(ofproto->backer->dpif,
2636 devname, &dpif_port);
2638 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2644 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2646 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2647 const char *devname = netdev_get_name(netdev);
2648 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2649 const char *dp_port_name;
2651 if (netdev_vport_is_patch(netdev)) {
2652 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2656 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2657 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2658 odp_port_t port_no = ODPP_NONE;
2661 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2665 if (netdev_get_tunnel_config(netdev)) {
2666 simap_put(&ofproto->backer->tnl_backers,
2667 dp_port_name, odp_to_u32(port_no));
2671 if (netdev_get_tunnel_config(netdev)) {
2672 sset_add(&ofproto->ghost_ports, devname);
2674 sset_add(&ofproto->ports, devname);
2680 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2682 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2683 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2690 sset_find_and_delete(&ofproto->ghost_ports,
2691 netdev_get_name(ofport->up.netdev));
2692 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2693 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2694 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2696 /* The caller is going to close ofport->up.netdev. If this is a
2697 * bonded port, then the bond is using that netdev, so remove it
2698 * from the bond. The client will need to reconfigure everything
2699 * after deleting ports, so then the slave will get re-added. */
2700 bundle_remove(&ofport->up);
2707 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2709 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2712 error = netdev_get_stats(ofport->up.netdev, stats);
2714 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2715 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2717 ovs_mutex_lock(&ofproto->stats_mutex);
2718 /* ofproto->stats.tx_packets represents packets that we created
2719 * internally and sent to some port (e.g. packets sent with
2720 * ofproto_dpif_send_packet()). Account for them as if they had
2721 * come from OFPP_LOCAL and got forwarded. */
2723 if (stats->rx_packets != UINT64_MAX) {
2724 stats->rx_packets += ofproto->stats.tx_packets;
2727 if (stats->rx_bytes != UINT64_MAX) {
2728 stats->rx_bytes += ofproto->stats.tx_bytes;
2731 /* ofproto->stats.rx_packets represents packets that were received on
2732 * some port and we processed internally and dropped (e.g. STP).
2733 * Account for them as if they had been forwarded to OFPP_LOCAL. */
2735 if (stats->tx_packets != UINT64_MAX) {
2736 stats->tx_packets += ofproto->stats.rx_packets;
2739 if (stats->tx_bytes != UINT64_MAX) {
2740 stats->tx_bytes += ofproto->stats.rx_bytes;
2742 ovs_mutex_unlock(&ofproto->stats_mutex);
2748 struct port_dump_state {
2753 struct ofproto_port port;
2758 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
2760 *statep = xzalloc(sizeof(struct port_dump_state));
2765 port_dump_next(const struct ofproto *ofproto_, void *state_,
2766 struct ofproto_port *port)
2768 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2769 struct port_dump_state *state = state_;
2770 const struct sset *sset;
2771 struct sset_node *node;
2773 if (state->has_port) {
2774 ofproto_port_destroy(&state->port);
2775 state->has_port = false;
2777 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
2778 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
2781 error = port_query_by_name(ofproto_, node->name, &state->port);
2783 *port = state->port;
2784 state->has_port = true;
2786 } else if (error != ENODEV) {
2791 if (!state->ghost) {
2792 state->ghost = true;
2795 return port_dump_next(ofproto_, state_, port);
2802 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2804 struct port_dump_state *state = state_;
2806 if (state->has_port) {
2807 ofproto_port_destroy(&state->port);
2814 port_poll(const struct ofproto *ofproto_, char **devnamep)
2816 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2818 if (ofproto->port_poll_errno) {
2819 int error = ofproto->port_poll_errno;
2820 ofproto->port_poll_errno = 0;
2824 if (sset_is_empty(&ofproto->port_poll_set)) {
2828 *devnamep = sset_pop(&ofproto->port_poll_set);
2833 port_poll_wait(const struct ofproto *ofproto_)
2835 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2836 dpif_port_poll_wait(ofproto->backer->dpif);
2840 port_is_lacp_current(const struct ofport *ofport_)
2842 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2843 return (ofport->bundle && ofport->bundle->lacp
2844 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2848 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2849 * then delete it entirely. */
2851 rule_expire(struct rule_dpif *rule)
2852 OVS_REQUIRES(ofproto_mutex)
2854 uint16_t idle_timeout, hard_timeout;
2855 long long int now = time_msec();
2858 ovs_assert(!rule->up.pending);
2860 /* Has 'rule' expired? */
2861 ovs_mutex_lock(&rule->up.mutex);
2862 hard_timeout = rule->up.hard_timeout;
2863 idle_timeout = rule->up.idle_timeout;
2864 if (hard_timeout && now > rule->up.modified + hard_timeout * 1000) {
2865 reason = OFPRR_HARD_TIMEOUT;
2866 } else if (idle_timeout && now > rule->up.used + idle_timeout * 1000) {
2867 reason = OFPRR_IDLE_TIMEOUT;
2871 ovs_mutex_unlock(&rule->up.mutex);
2874 COVERAGE_INC(ofproto_dpif_expired);
2875 ofproto_rule_expire(&rule->up, reason);
2879 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
2880 * 'flow' must reflect the data in 'packet'. */
2882 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
2883 const struct flow *flow,
2884 struct rule_dpif *rule,
2885 const struct ofpact *ofpacts, size_t ofpacts_len,
2886 struct ofpbuf *packet)
2888 struct dpif_flow_stats stats;
2889 struct xlate_out xout;
2890 struct xlate_in xin;
2892 struct dpif_execute execute;
2895 ovs_assert((rule != NULL) != (ofpacts != NULL));
2897 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
2899 rule_dpif_credit_stats(rule, &stats);
2902 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
2903 xin.ofpacts = ofpacts;
2904 xin.ofpacts_len = ofpacts_len;
2905 xin.resubmit_stats = &stats;
2906 xlate_actions(&xin, &xout);
2908 in_port = flow->in_port.ofp_port;
2909 if (in_port == OFPP_NONE) {
2910 in_port = OFPP_LOCAL;
2912 execute.actions = xout.odp_actions.data;
2913 execute.actions_len = xout.odp_actions.size;
2914 execute.packet = packet;
2915 execute.md.tunnel = flow->tunnel;
2916 execute.md.skb_priority = flow->skb_priority;
2917 execute.md.pkt_mark = flow->pkt_mark;
2918 execute.md.in_port = ofp_port_to_odp_port(ofproto, in_port);
2919 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
2921 error = dpif_execute(ofproto->backer->dpif, &execute);
2923 xlate_out_uninit(&xout);
2929 rule_dpif_credit_stats(struct rule_dpif *rule,
2930 const struct dpif_flow_stats *stats)
2932 ovs_mutex_lock(&rule->stats_mutex);
2933 rule->packet_count += stats->n_packets;
2934 rule->byte_count += stats->n_bytes;
2935 rule->up.used = MAX(rule->up.used, stats->used);
2936 ovs_mutex_unlock(&rule->stats_mutex);
2940 rule_dpif_is_fail_open(const struct rule_dpif *rule)
2942 return is_fail_open_rule(&rule->up);
2946 rule_dpif_is_table_miss(const struct rule_dpif *rule)
2948 return rule_is_table_miss(&rule->up);
2952 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
2953 OVS_REQUIRES(rule->up.mutex)
2955 return rule->up.flow_cookie;
2959 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
2960 uint16_t hard_timeout)
2962 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
2965 /* Returns 'rule''s actions. The caller owns a reference on the returned
2966 * actions and must eventually release it (with rule_actions_unref()) to avoid
2968 struct rule_actions *
2969 rule_dpif_get_actions(const struct rule_dpif *rule)
2971 return rule_get_actions(&rule->up);
2974 /* Lookup 'flow' in 'ofproto''s classifier. If 'wc' is non-null, sets
2975 * the fields that were relevant as part of the lookup. */
2977 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2978 struct flow_wildcards *wc, struct rule_dpif **rule)
2980 struct ofport_dpif *port;
2982 if (rule_dpif_lookup_in_table(ofproto, flow, wc, 0, rule)) {
2985 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
2987 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
2988 flow->in_port.ofp_port);
2991 choose_miss_rule(port ? port->up.pp.config : 0, ofproto->miss_rule,
2992 ofproto->no_packet_in_rule, rule);
2996 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto,
2997 const struct flow *flow, struct flow_wildcards *wc,
2998 uint8_t table_id, struct rule_dpif **rule)
3000 const struct cls_rule *cls_rule;
3001 struct classifier *cls;
3005 if (table_id >= N_TABLES) {
3010 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3011 if (is_ip_any(flow)) {
3012 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3016 cls = &ofproto->up.tables[table_id].cls;
3017 fat_rwlock_rdlock(&cls->rwlock);
3018 frag = (flow->nw_frag & FLOW_NW_FRAG_ANY) != 0;
3019 if (frag && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3020 /* We must pretend that transport ports are unavailable. */
3021 struct flow ofpc_normal_flow = *flow;
3022 ofpc_normal_flow.tp_src = htons(0);
3023 ofpc_normal_flow.tp_dst = htons(0);
3024 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3025 } else if (frag && ofproto->up.frag_handling == OFPC_FRAG_DROP) {
3026 cls_rule = &ofproto->drop_frags_rule->up.cr;
3027 /* Frag mask in wc already set above. */
3029 cls_rule = classifier_lookup(cls, flow, wc);
3032 *rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3033 rule_dpif_ref(*rule);
3034 fat_rwlock_unlock(&cls->rwlock);
3036 return *rule != NULL;
3039 /* Given a port configuration (specified as zero if there's no port), chooses
3040 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3041 * flow table miss. */
3043 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3044 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
3046 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
3047 rule_dpif_ref(*rule);
3051 rule_dpif_ref(struct rule_dpif *rule)
3054 ofproto_rule_ref(&rule->up);
3059 rule_dpif_unref(struct rule_dpif *rule)
3062 ofproto_rule_unref(&rule->up);
3067 complete_operation(struct rule_dpif *rule)
3068 OVS_REQUIRES(ofproto_mutex)
3070 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3072 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3073 ofoperation_complete(rule->up.pending, 0);
3076 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3078 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3081 static struct rule *
3084 struct rule_dpif *rule = xmalloc(sizeof *rule);
3089 rule_dealloc(struct rule *rule_)
3091 struct rule_dpif *rule = rule_dpif_cast(rule_);
3096 rule_construct(struct rule *rule_)
3098 struct rule_dpif *rule = rule_dpif_cast(rule_);
3099 ovs_mutex_init(&rule->stats_mutex);
3100 ovs_mutex_lock(&rule->stats_mutex);
3101 rule->packet_count = 0;
3102 rule->byte_count = 0;
3103 ovs_mutex_unlock(&rule->stats_mutex);
3108 rule_insert(struct rule *rule_)
3109 OVS_REQUIRES(ofproto_mutex)
3111 struct rule_dpif *rule = rule_dpif_cast(rule_);
3112 complete_operation(rule);
3116 rule_delete(struct rule *rule_)
3117 OVS_REQUIRES(ofproto_mutex)
3119 struct rule_dpif *rule = rule_dpif_cast(rule_);
3120 complete_operation(rule);
3124 rule_destruct(struct rule *rule_)
3126 struct rule_dpif *rule = rule_dpif_cast(rule_);
3127 ovs_mutex_destroy(&rule->stats_mutex);
3131 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3133 struct rule_dpif *rule = rule_dpif_cast(rule_);
3135 ovs_mutex_lock(&rule->stats_mutex);
3136 *packets = rule->packet_count;
3137 *bytes = rule->byte_count;
3138 ovs_mutex_unlock(&rule->stats_mutex);
3142 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3143 struct ofpbuf *packet)
3145 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3147 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3151 rule_execute(struct rule *rule, const struct flow *flow,
3152 struct ofpbuf *packet)
3154 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3155 ofpbuf_delete(packet);
3160 rule_modify_actions(struct rule *rule_, bool reset_counters)
3161 OVS_REQUIRES(ofproto_mutex)
3163 struct rule_dpif *rule = rule_dpif_cast(rule_);
3165 if (reset_counters) {
3166 ovs_mutex_lock(&rule->stats_mutex);
3167 rule->packet_count = 0;
3168 rule->byte_count = 0;
3169 ovs_mutex_unlock(&rule->stats_mutex);
3172 complete_operation(rule);
3175 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3177 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3180 static struct ofgroup *
3183 struct group_dpif *group = xzalloc(sizeof *group);
3188 group_dealloc(struct ofgroup *group_)
3190 struct group_dpif *group = group_dpif_cast(group_);
3195 group_construct_stats(struct group_dpif *group)
3196 OVS_REQUIRES(group->stats_mutex)
3198 group->packet_count = 0;
3199 group->byte_count = 0;
3200 if (!group->bucket_stats) {
3201 group->bucket_stats = xcalloc(group->up.n_buckets,
3202 sizeof *group->bucket_stats);
3204 memset(group->bucket_stats, 0, group->up.n_buckets *
3205 sizeof *group->bucket_stats);
3210 group_construct(struct ofgroup *group_)
3212 struct group_dpif *group = group_dpif_cast(group_);
3213 ovs_mutex_init(&group->stats_mutex);
3214 ovs_mutex_lock(&group->stats_mutex);
3215 group_construct_stats(group);
3216 ovs_mutex_unlock(&group->stats_mutex);
3221 group_destruct__(struct group_dpif *group)
3222 OVS_REQUIRES(group->stats_mutex)
3224 free(group->bucket_stats);
3225 group->bucket_stats = NULL;
3229 group_destruct(struct ofgroup *group_)
3231 struct group_dpif *group = group_dpif_cast(group_);
3232 ovs_mutex_lock(&group->stats_mutex);
3233 group_destruct__(group);
3234 ovs_mutex_unlock(&group->stats_mutex);
3235 ovs_mutex_destroy(&group->stats_mutex);
3239 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3241 struct group_dpif *group = group_dpif_cast(group_);
3242 struct group_dpif *victim = group_dpif_cast(victim_);
3244 ovs_mutex_lock(&group->stats_mutex);
3245 if (victim->up.n_buckets < group->up.n_buckets) {
3246 group_destruct__(group);
3248 group_construct_stats(group);
3249 ovs_mutex_unlock(&group->stats_mutex);
3255 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3257 struct group_dpif *group = group_dpif_cast(group_);
3259 ovs_mutex_lock(&group->stats_mutex);
3260 ogs->packet_count = group->packet_count;
3261 ogs->byte_count = group->byte_count;
3262 memcpy(ogs->bucket_stats, group->bucket_stats,
3263 group->up.n_buckets * sizeof *group->bucket_stats);
3264 ovs_mutex_unlock(&group->stats_mutex);
3270 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3271 struct group_dpif **group)
3272 OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3274 struct ofgroup *ofgroup;
3278 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3279 *group = found ? group_dpif_cast(ofgroup) : NULL;
3285 group_dpif_release(struct group_dpif *group)
3286 OVS_RELEASES(group->up.rwlock)
3288 ofproto_group_release(&group->up);
3292 group_dpif_get_buckets(const struct group_dpif *group,
3293 const struct list **buckets)
3295 *buckets = &group->up.buckets;
3298 enum ofp11_group_type
3299 group_dpif_get_type(const struct group_dpif *group)
3301 return group->up.type;
3304 /* Sends 'packet' out 'ofport'.
3305 * May modify 'packet'.
3306 * Returns 0 if successful, otherwise a positive errno value. */
3308 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3310 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3313 error = xlate_send_packet(ofport, packet);
3315 ovs_mutex_lock(&ofproto->stats_mutex);
3316 ofproto->stats.tx_packets++;
3317 ofproto->stats.tx_bytes += packet->size;
3318 ovs_mutex_unlock(&ofproto->stats_mutex);
3323 set_frag_handling(struct ofproto *ofproto_,
3324 enum ofp_config_flags frag_handling)
3326 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3327 if (frag_handling != OFPC_FRAG_REASM) {
3328 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3336 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3337 const struct flow *flow,
3338 const struct ofpact *ofpacts, size_t ofpacts_len)
3340 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3342 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3343 ofpacts_len, packet);
3350 set_netflow(struct ofproto *ofproto_,
3351 const struct netflow_options *netflow_options)
3353 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3355 if (netflow_options) {
3356 if (!ofproto->netflow) {
3357 ofproto->netflow = netflow_create();
3358 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3360 return netflow_set_options(ofproto->netflow, netflow_options);
3361 } else if (ofproto->netflow) {
3362 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3363 netflow_unref(ofproto->netflow);
3364 ofproto->netflow = NULL;
3371 get_netflow_ids(const struct ofproto *ofproto_,
3372 uint8_t *engine_type, uint8_t *engine_id)
3374 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3376 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3379 static struct ofproto_dpif *
3380 ofproto_dpif_lookup(const char *name)
3382 struct ofproto_dpif *ofproto;
3384 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3385 hash_string(name, 0), &all_ofproto_dpifs) {
3386 if (!strcmp(ofproto->up.name, name)) {
3394 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3395 const char *argv[], void *aux OVS_UNUSED)
3397 struct ofproto_dpif *ofproto;
3400 ofproto = ofproto_dpif_lookup(argv[1]);
3402 unixctl_command_reply_error(conn, "no such bridge");
3405 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3406 mac_learning_flush(ofproto->ml);
3407 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3409 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3410 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3411 mac_learning_flush(ofproto->ml);
3412 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3416 unixctl_command_reply(conn, "table successfully flushed");
3419 static struct ofport_dpif *
3420 ofbundle_get_a_port(const struct ofbundle *bundle)
3422 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3427 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3428 const char *argv[], void *aux OVS_UNUSED)
3430 struct ds ds = DS_EMPTY_INITIALIZER;
3431 const struct ofproto_dpif *ofproto;
3432 const struct mac_entry *e;
3434 ofproto = ofproto_dpif_lookup(argv[1]);
3436 unixctl_command_reply_error(conn, "no such bridge");
3440 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3441 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3442 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3443 struct ofbundle *bundle = e->port.p;
3444 char name[OFP_MAX_PORT_NAME_LEN];
3446 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3448 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
3449 name, e->vlan, ETH_ADDR_ARGS(e->mac),
3450 mac_entry_age(ofproto->ml, e));
3452 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3453 unixctl_command_reply(conn, ds_cstr(&ds));
3458 struct xlate_out xout;
3459 struct xlate_in xin;
3461 struct flow_wildcards wc;
3466 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
3468 struct rule_actions *actions;
3471 ds_put_char_multiple(result, '\t', level);
3473 ds_put_cstr(result, "No match\n");
3477 ovs_mutex_lock(&rule->up.mutex);
3478 cookie = rule->up.flow_cookie;
3479 ovs_mutex_unlock(&rule->up.mutex);
3481 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3482 rule ? rule->up.table_id : 0, ntohll(cookie));
3483 cls_rule_format(&rule->up.cr, result);
3484 ds_put_char(result, '\n');
3486 actions = rule_dpif_get_actions(rule);
3488 ds_put_char_multiple(result, '\t', level);
3489 ds_put_cstr(result, "OpenFlow actions=");
3490 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
3491 ds_put_char(result, '\n');
3493 rule_actions_unref(actions);
3497 trace_format_flow(struct ds *result, int level, const char *title,
3498 struct trace_ctx *trace)
3500 ds_put_char_multiple(result, '\t', level);
3501 ds_put_format(result, "%s: ", title);
3502 if (flow_equal(&trace->xin.flow, &trace->flow)) {
3503 ds_put_cstr(result, "unchanged");
3505 flow_format(result, &trace->xin.flow);
3506 trace->flow = trace->xin.flow;
3508 ds_put_char(result, '\n');
3512 trace_format_regs(struct ds *result, int level, const char *title,
3513 struct trace_ctx *trace)
3517 ds_put_char_multiple(result, '\t', level);
3518 ds_put_format(result, "%s:", title);
3519 for (i = 0; i < FLOW_N_REGS; i++) {
3520 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
3522 ds_put_char(result, '\n');
3526 trace_format_odp(struct ds *result, int level, const char *title,
3527 struct trace_ctx *trace)
3529 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
3531 ds_put_char_multiple(result, '\t', level);
3532 ds_put_format(result, "%s: ", title);
3533 format_odp_actions(result, odp_actions->data, odp_actions->size);
3534 ds_put_char(result, '\n');
3538 trace_format_megaflow(struct ds *result, int level, const char *title,
3539 struct trace_ctx *trace)
3543 ds_put_char_multiple(result, '\t', level);
3544 ds_put_format(result, "%s: ", title);
3545 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
3546 match_init(&match, &trace->flow, &trace->wc);
3547 match_format(&match, result, OFP_DEFAULT_PRIORITY);
3548 ds_put_char(result, '\n');
3552 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3554 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3555 struct ds *result = trace->result;
3557 ds_put_char(result, '\n');
3558 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3559 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3560 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
3561 trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
3562 trace_format_rule(result, recurse + 1, rule);
3566 trace_report(struct xlate_in *xin, const char *s, int recurse)
3568 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3569 struct ds *result = trace->result;
3571 ds_put_char_multiple(result, '\t', recurse);
3572 ds_put_cstr(result, s);
3573 ds_put_char(result, '\n');
3576 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
3577 * forms are supported:
3579 * - [dpname] odp_flow [-generate | packet]
3580 * - bridge br_flow [-generate | packet]
3582 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
3583 * returns a nonnull malloced error message. */
3584 static char * WARN_UNUSED_RESULT
3585 parse_flow_and_packet(int argc, const char *argv[],
3586 struct ofproto_dpif **ofprotop, struct flow *flow,
3587 struct ofpbuf **packetp)
3589 const struct dpif_backer *backer = NULL;
3590 const char *error = NULL;
3592 struct simap port_names = SIMAP_INITIALIZER(&port_names);
3593 struct ofpbuf *packet;
3594 struct ofpbuf odp_key;
3595 struct ofpbuf odp_mask;
3597 ofpbuf_init(&odp_key, 0);
3598 ofpbuf_init(&odp_mask, 0);
3600 /* Handle "-generate" or a hex string as the last argument. */
3601 if (!strcmp(argv[argc - 1], "-generate")) {
3602 packet = ofpbuf_new(0);
3605 error = eth_from_hex(argv[argc - 1], &packet);
3608 } else if (argc == 4) {
3609 /* The 3-argument form must end in "-generate' or a hex string. */
3615 /* odp_flow can have its in_port specified as a name instead of port no.
3616 * We do not yet know whether a given flow is a odp_flow or a br_flow.
3617 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3618 * we need to create a simap of name to port no. */
3620 const char *dp_type;
3621 if (!strncmp(argv[1], "ovs-", 4)) {
3622 dp_type = argv[1] + 4;
3626 backer = shash_find_data(&all_dpif_backers, dp_type);
3627 } else if (argc == 2) {
3628 struct shash_node *node;
3629 if (shash_count(&all_dpif_backers) == 1) {
3630 node = shash_first(&all_dpif_backers);
3631 backer = node->data;
3634 error = "Syntax error";
3637 if (backer && backer->dpif) {
3638 struct dpif_port dpif_port;
3639 struct dpif_port_dump port_dump;
3640 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
3641 simap_put(&port_names, dpif_port.name,
3642 odp_to_u32(dpif_port.port_no));
3646 /* Parse the flow and determine whether a datapath or
3647 * bridge is specified. If function odp_flow_key_from_string()
3648 * returns 0, the flow is a odp_flow. If function
3649 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
3650 if (!odp_flow_from_string(argv[argc - 1], &port_names,
3651 &odp_key, &odp_mask)) {
3653 error = "Cannot find the datapath";
3657 if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, flow,
3658 NULL, ofprotop, NULL, NULL, NULL, NULL)) {
3659 error = "Invalid datapath flow";
3663 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
3666 m_err = xasprintf("Bad flow syntax: %s", err);
3671 error = "Must specify bridge name";
3675 *ofprotop = ofproto_dpif_lookup(argv[1]);
3677 error = "Unknown bridge name";
3683 /* Generate a packet, if requested. */
3685 if (!packet->size) {
3686 flow_compose(packet, flow);
3688 union flow_in_port in_port = flow->in_port;
3690 /* Use the metadata from the flow and the packet argument
3691 * to reconstruct the flow. */
3692 flow_extract(packet, flow->skb_priority, flow->pkt_mark, NULL,
3698 if (error && !m_err) {
3699 m_err = xstrdup(error);
3702 ofpbuf_delete(packet);
3706 ofpbuf_uninit(&odp_key);
3707 ofpbuf_uninit(&odp_mask);
3708 simap_destroy(&port_names);
3713 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
3714 void *aux OVS_UNUSED)
3716 struct ofproto_dpif *ofproto;
3717 struct ofpbuf *packet;
3721 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3726 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
3727 unixctl_command_reply(conn, ds_cstr(&result));
3728 ds_destroy(&result);
3729 ofpbuf_delete(packet);
3731 unixctl_command_reply_error(conn, error);
3737 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
3738 const char *argv[], void *aux OVS_UNUSED)
3740 enum ofputil_protocol usable_protocols;
3741 struct ofproto_dpif *ofproto;
3742 bool enforce_consistency;
3743 struct ofpbuf ofpacts;
3744 struct ofpbuf *packet;
3749 /* Three kinds of error return values! */
3755 ofpbuf_init(&ofpacts, 0);
3757 /* Parse actions. */
3758 error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
3760 unixctl_command_reply_error(conn, error);
3765 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
3766 * consistency between the flow and the actions. With -consistent, we
3767 * enforce consistency even for a flow supported in OpenFlow 1.0. */
3768 if (!strcmp(argv[1], "-consistent")) {
3769 enforce_consistency = true;
3773 enforce_consistency = false;
3776 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3778 unixctl_command_reply_error(conn, error);
3783 /* Do the same checks as handle_packet_out() in ofproto.c.
3785 * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
3786 * strictly correct because these actions aren't in any table, but it's OK
3787 * because it 'table_id' is used only to check goto_table instructions, but
3788 * packet-outs take a list of actions and therefore it can't include
3791 * We skip the "meter" check here because meter is an instruction, not an
3792 * action, and thus cannot appear in ofpacts. */
3793 in_port = ofp_to_u16(flow.in_port.ofp_port);
3794 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
3795 unixctl_command_reply_error(conn, "invalid in_port");
3798 if (enforce_consistency) {
3799 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
3800 u16_to_ofp(ofproto->up.max_ports),
3801 0, 0, usable_protocols);
3803 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
3804 u16_to_ofp(ofproto->up.max_ports), 0, 0,
3810 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
3811 unixctl_command_reply_error(conn, ds_cstr(&result));
3815 ofproto_trace(ofproto, &flow, packet, ofpacts.data, ofpacts.size, &result);
3816 unixctl_command_reply(conn, ds_cstr(&result));
3819 ds_destroy(&result);
3820 ofpbuf_delete(packet);
3821 ofpbuf_uninit(&ofpacts);
3824 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
3825 * description of the results to 'ds'.
3827 * The trace follows a packet with the specified 'flow' through the flow
3828 * table. 'packet' may be nonnull to trace an actual packet, with consequent
3829 * side effects (if it is nonnull then its flow must be 'flow').
3831 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
3832 * trace, otherwise the actions are determined by a flow table lookup. */
3834 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
3835 const struct ofpbuf *packet,
3836 const struct ofpact ofpacts[], size_t ofpacts_len,
3839 struct rule_dpif *rule;
3840 struct trace_ctx trace;
3842 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
3843 ds_put_cstr(ds, "Flow: ");
3844 flow_format(ds, flow);
3845 ds_put_char(ds, '\n');
3847 flow_wildcards_init_catchall(&trace.wc);
3851 rule_dpif_lookup(ofproto, flow, &trace.wc, &rule);
3853 trace_format_rule(ds, 0, rule);
3854 if (rule == ofproto->miss_rule) {
3855 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
3856 } else if (rule == ofproto->no_packet_in_rule) {
3857 ds_put_cstr(ds, "\nNo match, packets dropped because "
3858 "OFPPC_NO_PACKET_IN is set on in_port.\n");
3859 } else if (rule == ofproto->drop_frags_rule) {
3860 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
3861 "and the fragment handling mode is \"drop\".\n");
3865 if (rule || ofpacts) {
3868 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
3871 xlate_in_init(&trace.xin, ofproto, flow, rule, tcp_flags, packet);
3873 trace.xin.ofpacts = ofpacts;
3874 trace.xin.ofpacts_len = ofpacts_len;
3876 trace.xin.resubmit_hook = trace_resubmit;
3877 trace.xin.report_hook = trace_report;
3879 xlate_actions(&trace.xin, &trace.xout);
3881 ds_put_char(ds, '\n');
3882 trace_format_flow(ds, 0, "Final flow", &trace);
3883 trace_format_megaflow(ds, 0, "Megaflow", &trace);
3885 ds_put_cstr(ds, "Datapath actions: ");
3886 format_odp_actions(ds, trace.xout.odp_actions.data,
3887 trace.xout.odp_actions.size);
3889 if (trace.xout.slow) {
3890 enum slow_path_reason slow;
3892 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
3893 "slow path because it:");
3895 slow = trace.xout.slow;
3897 enum slow_path_reason bit = rightmost_1bit(slow);
3899 ds_put_format(ds, "\n\t- %s.",
3900 slow_path_reason_to_explanation(bit));
3906 xlate_out_uninit(&trace.xout);
3909 rule_dpif_unref(rule);
3912 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
3913 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
3914 * to destroy 'ofproto_shash' and free the returned value. */
3915 static const struct shash_node **
3916 get_ofprotos(struct shash *ofproto_shash)
3918 const struct ofproto_dpif *ofproto;
3920 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3921 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
3922 shash_add_nocopy(ofproto_shash, name, ofproto);
3925 return shash_sort(ofproto_shash);
3929 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
3930 const char *argv[] OVS_UNUSED,
3931 void *aux OVS_UNUSED)
3933 struct ds ds = DS_EMPTY_INITIALIZER;
3934 struct shash ofproto_shash;
3935 const struct shash_node **sorted_ofprotos;
3938 shash_init(&ofproto_shash);
3939 sorted_ofprotos = get_ofprotos(&ofproto_shash);
3940 for (i = 0; i < shash_count(&ofproto_shash); i++) {
3941 const struct shash_node *node = sorted_ofprotos[i];
3942 ds_put_format(&ds, "%s\n", node->name);
3945 shash_destroy(&ofproto_shash);
3946 free(sorted_ofprotos);
3948 unixctl_command_reply(conn, ds_cstr(&ds));
3953 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
3955 const struct shash_node **ofprotos;
3956 struct dpif_dp_stats dp_stats;
3957 struct shash ofproto_shash;
3960 dpif_get_dp_stats(backer->dpif, &dp_stats);
3962 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
3963 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
3965 shash_init(&ofproto_shash);
3966 ofprotos = get_ofprotos(&ofproto_shash);
3967 for (i = 0; i < shash_count(&ofproto_shash); i++) {
3968 struct ofproto_dpif *ofproto = ofprotos[i]->data;
3969 const struct shash_node **ports;
3972 if (ofproto->backer != backer) {
3976 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
3978 ports = shash_sort(&ofproto->up.port_by_name);
3979 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
3980 const struct shash_node *node = ports[j];
3981 struct ofport *ofport = node->data;
3983 odp_port_t odp_port;
3985 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
3988 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
3989 if (odp_port != ODPP_NONE) {
3990 ds_put_format(ds, "%"PRIu32":", odp_port);
3992 ds_put_cstr(ds, "none:");
3995 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
3998 if (!netdev_get_config(ofport->netdev, &config)) {
3999 const struct smap_node **nodes;
4002 nodes = smap_sort(&config);
4003 for (i = 0; i < smap_count(&config); i++) {
4004 const struct smap_node *node = nodes[i];
4005 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4006 node->key, node->value);
4010 smap_destroy(&config);
4012 ds_put_char(ds, ')');
4013 ds_put_char(ds, '\n');
4017 shash_destroy(&ofproto_shash);
4022 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4023 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4025 struct ds ds = DS_EMPTY_INITIALIZER;
4026 const struct shash_node **backers;
4029 backers = shash_sort(&all_dpif_backers);
4030 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4031 dpif_show_backer(backers[i]->data, &ds);
4035 unixctl_command_reply(conn, ds_cstr(&ds));
4040 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4041 const struct nlattr *key, size_t key_len)
4043 enum odp_key_fitness fitness;
4044 struct ofproto_dpif *ofp;
4047 xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &fitness, &ofp,
4048 NULL, NULL, NULL, NULL);
4049 return ofp == ofproto;
4053 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4054 int argc OVS_UNUSED, const char *argv[],
4055 void *aux OVS_UNUSED)
4057 struct ds ds = DS_EMPTY_INITIALIZER;
4058 const struct dpif_flow_stats *stats;
4059 const struct ofproto_dpif *ofproto;
4060 struct dpif_flow_dump flow_dump;
4061 const struct nlattr *actions;
4062 const struct nlattr *mask;
4063 const struct nlattr *key;
4067 bool verbosity = false;
4068 struct dpif_port dpif_port;
4069 struct dpif_port_dump port_dump;
4070 struct hmap portno_names;
4072 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4074 unixctl_command_reply_error(conn, "no such bridge");
4078 if (argc > 2 && !strcmp(argv[1], "-m")) {
4082 hmap_init(&portno_names);
4083 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4084 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4088 dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4089 while (dpif_flow_dump_next(&flow_dump, &key, &key_len, &mask, &mask_len,
4090 &actions, &actions_len, &stats)) {
4091 if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
4095 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4097 ds_put_cstr(&ds, ", ");
4098 dpif_flow_stats_format(stats, &ds);
4099 ds_put_cstr(&ds, ", actions:");
4100 format_odp_actions(&ds, actions, actions_len);
4101 ds_put_char(&ds, '\n');
4104 if (dpif_flow_dump_done(&flow_dump)) {
4106 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4107 unixctl_command_reply_error(conn, ds_cstr(&ds));
4109 unixctl_command_reply(conn, ds_cstr(&ds));
4111 odp_portno_names_destroy(&portno_names);
4112 hmap_destroy(&portno_names);
4117 ofproto_dpif_unixctl_init(void)
4119 static bool registered;
4125 unixctl_command_register(
4127 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4128 1, 3, ofproto_unixctl_trace, NULL);
4129 unixctl_command_register(
4130 "ofproto/trace-packet-out",
4131 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4132 2, 6, ofproto_unixctl_trace_actions, NULL);
4133 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4134 ofproto_unixctl_fdb_flush, NULL);
4135 unixctl_command_register("fdb/show", "bridge", 1, 1,
4136 ofproto_unixctl_fdb_show, NULL);
4137 unixctl_command_register("dpif/dump-dps", "", 0, 0,
4138 ofproto_unixctl_dpif_dump_dps, NULL);
4139 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4141 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4142 ofproto_unixctl_dpif_dump_flows, NULL);
4145 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4147 * This is deprecated. It is only for compatibility with broken device drivers
4148 * in old versions of Linux that do not properly support VLANs when VLAN
4149 * devices are not used. When broken device drivers are no longer in
4150 * widespread use, we will delete these interfaces. */
4153 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4155 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4156 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4158 if (realdev_ofp_port == ofport->realdev_ofp_port
4159 && vid == ofport->vlandev_vid) {
4163 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4165 if (ofport->realdev_ofp_port) {
4168 if (realdev_ofp_port && ofport->bundle) {
4169 /* vlandevs are enslaved to their realdevs, so they are not allowed to
4170 * themselves be part of a bundle. */
4171 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
4174 ofport->realdev_ofp_port = realdev_ofp_port;
4175 ofport->vlandev_vid = vid;
4177 if (realdev_ofp_port) {
4178 vsp_add(ofport, realdev_ofp_port, vid);
4185 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4187 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4191 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4192 OVS_EXCLUDED(ofproto->vsp_mutex)
4196 ovs_mutex_lock(&ofproto->vsp_mutex);
4197 ret = !hmap_is_empty(&ofproto->realdev_vid_map);
4198 ovs_mutex_unlock(&ofproto->vsp_mutex);
4203 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4204 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4205 OVS_REQUIRES(ofproto->vsp_mutex)
4207 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4208 int vid = vlan_tci_to_vid(vlan_tci);
4209 const struct vlan_splinter *vsp;
4211 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4212 hash_realdev_vid(realdev_ofp_port, vid),
4213 &ofproto->realdev_vid_map) {
4214 if (vsp->realdev_ofp_port == realdev_ofp_port
4215 && vsp->vid == vid) {
4216 return vsp->vlandev_ofp_port;
4220 return realdev_ofp_port;
4223 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4224 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4225 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
4226 * 'vlan_tci' 9, it would return the port number of eth0.9.
4228 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4229 * function just returns its 'realdev_ofp_port' argument. */
4231 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4232 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4233 OVS_EXCLUDED(ofproto->vsp_mutex)
4237 ovs_mutex_lock(&ofproto->vsp_mutex);
4238 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4239 ovs_mutex_unlock(&ofproto->vsp_mutex);
4243 static struct vlan_splinter *
4244 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4246 struct vlan_splinter *vsp;
4248 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4249 hash_ofp_port(vlandev_ofp_port),
4250 &ofproto->vlandev_map) {
4251 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4259 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4260 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4261 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
4262 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4263 * eth0 and store 9 in '*vid'.
4265 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4266 * VLAN device. Unless VLAN splinters are enabled, this is what this function
4269 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4270 ofp_port_t vlandev_ofp_port, int *vid)
4271 OVS_REQUIRES(ofproto->vsp_mutex)
4273 if (!hmap_is_empty(&ofproto->vlandev_map)) {
4274 const struct vlan_splinter *vsp;
4276 vsp = vlandev_find(ofproto, vlandev_ofp_port);
4281 return vsp->realdev_ofp_port;
4287 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4288 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
4289 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4290 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
4291 * always the case unless VLAN splinters are enabled), returns false without
4292 * making any changes. */
4294 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4295 OVS_EXCLUDED(ofproto->vsp_mutex)
4300 ovs_mutex_lock(&ofproto->vsp_mutex);
4301 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4302 ovs_mutex_unlock(&ofproto->vsp_mutex);
4307 /* Cause the flow to be processed as if it came in on the real device with
4308 * the VLAN device's VLAN ID. */
4309 flow->in_port.ofp_port = realdev;
4310 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4315 vsp_remove(struct ofport_dpif *port)
4317 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4318 struct vlan_splinter *vsp;
4320 ovs_mutex_lock(&ofproto->vsp_mutex);
4321 vsp = vlandev_find(ofproto, port->up.ofp_port);
4323 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4324 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4327 port->realdev_ofp_port = 0;
4329 VLOG_ERR("missing vlan device record");
4331 ovs_mutex_unlock(&ofproto->vsp_mutex);
4335 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4337 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4339 ovs_mutex_lock(&ofproto->vsp_mutex);
4340 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4341 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4342 == realdev_ofp_port)) {
4343 struct vlan_splinter *vsp;
4345 vsp = xmalloc(sizeof *vsp);
4346 vsp->realdev_ofp_port = realdev_ofp_port;
4347 vsp->vlandev_ofp_port = port->up.ofp_port;
4350 port->realdev_ofp_port = realdev_ofp_port;
4352 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4353 hash_ofp_port(port->up.ofp_port));
4354 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4355 hash_realdev_vid(realdev_ofp_port, vid));
4357 VLOG_ERR("duplicate vlan device record");
4359 ovs_mutex_unlock(&ofproto->vsp_mutex);
4363 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4365 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4366 return ofport ? ofport->odp_port : ODPP_NONE;
4369 struct ofport_dpif *
4370 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4372 struct ofport_dpif *port;
4374 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4375 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4376 &backer->odp_to_ofport_map) {
4377 if (port->odp_port == odp_port) {
4378 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4383 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4388 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4390 struct ofport_dpif *port;
4392 port = odp_port_to_ofport(ofproto->backer, odp_port);
4393 if (port && &ofproto->up == port->up.ofproto) {
4394 return port->up.ofp_port;
4400 const struct ofproto_class ofproto_dpif_class = {
4414 NULL, /* get_memory_usage. */
4415 type_get_memory_usage,
4434 port_is_lacp_current,
4435 NULL, /* rule_choose_table */
4444 rule_modify_actions,
4458 get_stp_port_status,
4466 is_mirror_output_bundle,
4467 forward_bpdu_changed,
4468 set_mac_table_config,
4470 NULL, /* meter_get_features */
4471 NULL, /* meter_set */
4472 NULL, /* meter_get */
4473 NULL, /* meter_del */
4474 group_alloc, /* group_alloc */
4475 group_construct, /* group_construct */
4476 group_destruct, /* group_destruct */
4477 group_dealloc, /* group_dealloc */
4478 group_modify, /* group_modify */
4479 group_get_stats, /* group_get_stats */