2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 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"
45 #include "netlink-socket.h"
48 #include "odp-execute.h"
51 #include "ofp-actions.h"
52 #include "ofp-parse.h"
53 #include "ofp-print.h"
54 #include "ofproto-dpif-ipfix.h"
55 #include "ofproto-dpif-mirror.h"
56 #include "ofproto-dpif-monitor.h"
57 #include "ofproto-dpif-sflow.h"
58 #include "ofproto-dpif-upcall.h"
59 #include "ofproto-dpif-xlate.h"
60 #include "poll-loop.h"
66 #include "unaligned.h"
68 #include "vlan-bitmap.h"
71 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
73 COVERAGE_DEFINE(ofproto_dpif_expired);
74 COVERAGE_DEFINE(packet_in_overflow);
76 /* Number of implemented OpenFlow tables. */
77 enum { N_TABLES = 255 };
78 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
79 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
88 * - Do include packets and bytes from datapath flows which have not
89 * recently been processed by a revalidator. */
90 struct ovs_mutex stats_mutex;
91 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
92 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
95 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes);
96 static struct rule_dpif *rule_dpif_cast(const struct rule *);
97 static void rule_expire(struct rule_dpif *);
104 * - Do include packets and bytes from datapath flows which have not
105 * recently been processed by a revalidator. */
106 struct ovs_mutex stats_mutex;
107 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
108 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
109 struct bucket_counter *bucket_stats OVS_GUARDED; /* Bucket statistics. */
113 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
114 struct ofproto_dpif *ofproto; /* Owning ofproto. */
115 void *aux; /* Key supplied by ofproto's client. */
116 char *name; /* Identifier for log messages. */
119 struct list ports; /* Contains "struct ofport"s. */
120 enum port_vlan_mode vlan_mode; /* VLAN mode */
121 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
122 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
123 * NULL if all VLANs are trunked. */
124 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
125 struct bond *bond; /* Nonnull iff more than one port. */
126 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
129 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
132 static void bundle_remove(struct ofport *);
133 static void bundle_update(struct ofbundle *);
134 static void bundle_destroy(struct ofbundle *);
135 static void bundle_del_port(struct ofport_dpif *);
136 static void bundle_run(struct ofbundle *);
137 static void bundle_wait(struct ofbundle *);
139 static void stp_run(struct ofproto_dpif *ofproto);
140 static void stp_wait(struct ofproto_dpif *ofproto);
141 static int set_stp_port(struct ofport *,
142 const struct ofproto_port_stp_settings *);
145 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
149 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
150 struct list bundle_node; /* In struct ofbundle's "ports" list. */
151 struct cfm *cfm; /* Connectivity Fault Management, if any. */
152 struct bfd *bfd; /* BFD, if any. */
153 bool may_enable; /* May be enabled in bonds. */
154 bool is_tunnel; /* This port is a tunnel. */
155 bool is_layer3; /* This is a layer 3 port. */
156 long long int carrier_seq; /* Carrier status changes. */
157 struct ofport_dpif *peer; /* Peer if patch port. */
160 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
161 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
162 long long int stp_state_entered;
164 /* Queue to DSCP mapping. */
165 struct ofproto_port_queue *qdscp;
168 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
170 * This is deprecated. It is only for compatibility with broken device
171 * drivers in old versions of Linux that do not properly support VLANs when
172 * VLAN devices are not used. When broken device drivers are no longer in
173 * widespread use, we will delete these interfaces. */
174 ofp_port_t realdev_ofp_port;
178 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
180 * This is deprecated. It is only for compatibility with broken device drivers
181 * in old versions of Linux that do not properly support VLANs when VLAN
182 * devices are not used. When broken device drivers are no longer in
183 * widespread use, we will delete these interfaces. */
184 struct vlan_splinter {
185 struct hmap_node realdev_vid_node;
186 struct hmap_node vlandev_node;
187 ofp_port_t realdev_ofp_port;
188 ofp_port_t vlandev_ofp_port;
192 static void vsp_remove(struct ofport_dpif *);
193 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
195 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
198 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
201 static struct ofport_dpif *
202 ofport_dpif_cast(const struct ofport *ofport)
204 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
207 static void port_run(struct ofport_dpif *);
208 static int set_bfd(struct ofport *, const struct smap *);
209 static int set_cfm(struct ofport *, const struct cfm_settings *);
210 static void ofport_update_peer(struct ofport_dpif *);
212 struct dpif_completion {
213 struct list list_node;
214 struct ofoperation *op;
217 /* Reasons that we might need to revalidate every datapath flow, and
218 * corresponding coverage counters.
220 * A value of 0 means that there is no need to revalidate.
222 * It would be nice to have some cleaner way to integrate with coverage
223 * counters, but with only a few reasons I guess this is good enough for
225 enum revalidate_reason {
226 REV_RECONFIGURE = 1, /* Switch configuration changed. */
227 REV_STP, /* Spanning tree protocol port status change. */
228 REV_BOND, /* Bonding changed. */
229 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
230 REV_FLOW_TABLE, /* Flow table changed. */
231 REV_MAC_LEARNING, /* Mac learning changed. */
233 COVERAGE_DEFINE(rev_reconfigure);
234 COVERAGE_DEFINE(rev_stp);
235 COVERAGE_DEFINE(rev_bond);
236 COVERAGE_DEFINE(rev_port_toggled);
237 COVERAGE_DEFINE(rev_flow_table);
238 COVERAGE_DEFINE(rev_mac_learning);
240 /* All datapaths of a given type share a single dpif backer instance. */
247 struct ovs_rwlock odp_to_ofport_lock;
248 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
250 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
252 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
254 bool recv_set_enable; /* Enables or disables receiving packets. */
256 /* True if the datapath supports variable-length
257 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
258 * False if the datapath supports only 8-byte (or shorter) userdata. */
259 bool variable_length_userdata;
262 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
263 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
265 struct ofproto_dpif {
266 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
268 struct dpif_backer *backer;
270 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
272 /* Special OpenFlow rules. */
273 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
274 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
275 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
278 struct netflow *netflow;
279 struct dpif_sflow *sflow;
280 struct dpif_ipfix *ipfix;
281 struct hmap bundles; /* Contains "struct ofbundle"s. */
282 struct mac_learning *ml;
283 bool has_bonded_bundles;
285 struct mbridge *mbridge;
287 struct ovs_mutex stats_mutex;
288 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
289 * consumed in userspace. */
293 long long int stp_last_tick;
295 /* VLAN splinters. */
296 struct ovs_mutex vsp_mutex;
297 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
298 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
301 struct sset ports; /* Set of standard port names. */
302 struct sset ghost_ports; /* Ports with no datapath port. */
303 struct sset port_poll_set; /* Queued names for port_poll() reply. */
304 int port_poll_errno; /* Last errno for port_poll() reply. */
305 uint64_t change_seq; /* Connectivity status changes. */
308 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
311 /* All existing ofproto_dpif instances, indexed by ->up.name. */
312 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
314 static void ofproto_dpif_unixctl_init(void);
316 static inline struct ofproto_dpif *
317 ofproto_dpif_cast(const struct ofproto *ofproto)
319 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
320 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
323 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
324 ofp_port_t ofp_port);
325 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
326 const struct ofpbuf *packet,
327 const struct ofpact[], size_t ofpacts_len,
330 /* Global variables. */
331 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
333 /* Initial mappings of port to bridge mappings. */
334 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
336 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
339 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
340 struct ofputil_flow_mod *fm)
342 ofproto_flow_mod(&ofproto->up, fm);
345 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
346 * Takes ownership of 'pin' and pin->packet. */
348 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
349 struct ofproto_packet_in *pin)
351 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
352 COVERAGE_INC(packet_in_overflow);
353 free(CONST_CAST(void *, pin->up.packet));
358 /* Factory functions. */
361 init(const struct shash *iface_hints)
363 struct shash_node *node;
365 /* Make a local copy, since we don't own 'iface_hints' elements. */
366 SHASH_FOR_EACH(node, iface_hints) {
367 const struct iface_hint *orig_hint = node->data;
368 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
370 new_hint->br_name = xstrdup(orig_hint->br_name);
371 new_hint->br_type = xstrdup(orig_hint->br_type);
372 new_hint->ofp_port = orig_hint->ofp_port;
374 shash_add(&init_ofp_ports, node->name, new_hint);
379 enumerate_types(struct sset *types)
381 dp_enumerate_types(types);
385 enumerate_names(const char *type, struct sset *names)
387 struct ofproto_dpif *ofproto;
390 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
391 if (strcmp(type, ofproto->up.type)) {
394 sset_add(names, ofproto->up.name);
401 del(const char *type, const char *name)
406 error = dpif_open(name, type, &dpif);
408 error = dpif_delete(dpif);
415 port_open_type(const char *datapath_type, const char *port_type)
417 return dpif_port_open_type(datapath_type, port_type);
420 /* Type functions. */
422 static void process_dpif_port_changes(struct dpif_backer *);
423 static void process_dpif_all_ports_changed(struct dpif_backer *);
424 static void process_dpif_port_change(struct dpif_backer *,
425 const char *devname);
426 static void process_dpif_port_error(struct dpif_backer *, int error);
428 static struct ofproto_dpif *
429 lookup_ofproto_dpif_by_port_name(const char *name)
431 struct ofproto_dpif *ofproto;
433 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
434 if (sset_contains(&ofproto->ports, name)) {
443 type_run(const char *type)
445 struct dpif_backer *backer;
447 backer = shash_find_data(&all_dpif_backers, type);
449 /* This is not necessarily a problem, since backers are only
450 * created on demand. */
454 dpif_run(backer->dpif);
456 /* If vswitchd started with other_config:flow_restore_wait set as "true",
457 * and the configuration has now changed to "false", enable receiving
458 * packets from the datapath. */
459 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
462 backer->recv_set_enable = true;
464 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
466 VLOG_ERR("Failed to enable receiving packets in dpif.");
469 dpif_flow_flush(backer->dpif);
470 backer->need_revalidate = REV_RECONFIGURE;
473 if (backer->recv_set_enable) {
474 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
477 if (backer->need_revalidate) {
478 struct ofproto_dpif *ofproto;
479 struct simap_node *node;
480 struct simap tmp_backers;
482 /* Handle tunnel garbage collection. */
483 simap_init(&tmp_backers);
484 simap_swap(&backer->tnl_backers, &tmp_backers);
486 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
487 struct ofport_dpif *iter;
489 if (backer != ofproto->backer) {
493 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
494 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
497 if (!iter->is_tunnel) {
501 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
502 namebuf, sizeof namebuf);
503 node = simap_find(&tmp_backers, dp_port);
505 simap_put(&backer->tnl_backers, dp_port, node->data);
506 simap_delete(&tmp_backers, node);
507 node = simap_find(&backer->tnl_backers, dp_port);
509 node = simap_find(&backer->tnl_backers, dp_port);
511 odp_port_t odp_port = ODPP_NONE;
513 if (!dpif_port_add(backer->dpif, iter->up.netdev,
515 simap_put(&backer->tnl_backers, dp_port,
516 odp_to_u32(odp_port));
517 node = simap_find(&backer->tnl_backers, dp_port);
522 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
523 if (tnl_port_reconfigure(iter, iter->up.netdev,
525 backer->need_revalidate = REV_RECONFIGURE;
530 SIMAP_FOR_EACH (node, &tmp_backers) {
531 dpif_port_del(backer->dpif, u32_to_odp(node->data));
533 simap_destroy(&tmp_backers);
535 switch (backer->need_revalidate) {
536 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
537 case REV_STP: COVERAGE_INC(rev_stp); break;
538 case REV_BOND: COVERAGE_INC(rev_bond); break;
539 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
540 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
541 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
543 backer->need_revalidate = 0;
545 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
546 struct ofport_dpif *ofport;
547 struct ofbundle *bundle;
549 if (ofproto->backer != backer) {
553 ovs_rwlock_wrlock(&xlate_rwlock);
554 xlate_ofproto_set(ofproto, ofproto->up.name,
555 ofproto->backer->dpif, ofproto->miss_rule,
556 ofproto->no_packet_in_rule, ofproto->ml,
557 ofproto->stp, ofproto->mbridge,
558 ofproto->sflow, ofproto->ipfix,
559 ofproto->netflow, ofproto->up.frag_handling,
560 ofproto->up.forward_bpdu,
561 connmgr_has_in_band(ofproto->up.connmgr),
562 ofproto->backer->variable_length_userdata);
564 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
565 xlate_bundle_set(ofproto, bundle, bundle->name,
566 bundle->vlan_mode, bundle->vlan,
567 bundle->trunks, bundle->use_priority_tags,
568 bundle->bond, bundle->lacp,
572 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
573 int stp_port = ofport->stp_port
574 ? stp_port_no(ofport->stp_port)
576 xlate_ofport_set(ofproto, ofport->bundle, ofport,
577 ofport->up.ofp_port, ofport->odp_port,
578 ofport->up.netdev, ofport->cfm,
579 ofport->bfd, ofport->peer, stp_port,
580 ofport->qdscp, ofport->n_qdscp,
581 ofport->up.pp.config, ofport->up.pp.state,
582 ofport->is_tunnel, ofport->may_enable);
584 ovs_rwlock_unlock(&xlate_rwlock);
587 udpif_revalidate(backer->udpif);
590 process_dpif_port_changes(backer);
595 /* Check for and handle port changes in 'backer''s dpif. */
597 process_dpif_port_changes(struct dpif_backer *backer)
603 error = dpif_port_poll(backer->dpif, &devname);
609 process_dpif_all_ports_changed(backer);
613 process_dpif_port_change(backer, devname);
618 process_dpif_port_error(backer, error);
625 process_dpif_all_ports_changed(struct dpif_backer *backer)
627 struct ofproto_dpif *ofproto;
628 struct dpif_port dpif_port;
629 struct dpif_port_dump dump;
630 struct sset devnames;
633 sset_init(&devnames);
634 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
635 if (ofproto->backer == backer) {
636 struct ofport *ofport;
638 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
639 sset_add(&devnames, netdev_get_name(ofport->netdev));
643 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
644 sset_add(&devnames, dpif_port.name);
647 SSET_FOR_EACH (devname, &devnames) {
648 process_dpif_port_change(backer, devname);
650 sset_destroy(&devnames);
654 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
656 struct ofproto_dpif *ofproto;
657 struct dpif_port port;
659 /* Don't report on the datapath's device. */
660 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
664 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
665 &all_ofproto_dpifs) {
666 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
671 ofproto = lookup_ofproto_dpif_by_port_name(devname);
672 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
673 /* The port was removed. If we know the datapath,
674 * report it through poll_set(). If we don't, it may be
675 * notifying us of a removal we initiated, so ignore it.
676 * If there's a pending ENOBUFS, let it stand, since
677 * everything will be reevaluated. */
678 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
679 sset_add(&ofproto->port_poll_set, devname);
680 ofproto->port_poll_errno = 0;
682 } else if (!ofproto) {
683 /* The port was added, but we don't know with which
684 * ofproto we should associate it. Delete it. */
685 dpif_port_del(backer->dpif, port.port_no);
687 struct ofport_dpif *ofport;
689 ofport = ofport_dpif_cast(shash_find_data(
690 &ofproto->up.port_by_name, devname));
692 && ofport->odp_port != port.port_no
693 && !odp_port_to_ofport(backer, port.port_no))
695 /* 'ofport''s datapath port number has changed from
696 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
697 * structures to match. */
698 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
699 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
700 ofport->odp_port = port.port_no;
701 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
702 hash_odp_port(port.port_no));
703 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
704 backer->need_revalidate = REV_RECONFIGURE;
707 dpif_port_destroy(&port);
710 /* Propagate 'error' to all ofprotos based on 'backer'. */
712 process_dpif_port_error(struct dpif_backer *backer, int error)
714 struct ofproto_dpif *ofproto;
716 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
717 if (ofproto->backer == backer) {
718 sset_clear(&ofproto->port_poll_set);
719 ofproto->port_poll_errno = error;
725 type_wait(const char *type)
727 struct dpif_backer *backer;
729 backer = shash_find_data(&all_dpif_backers, type);
731 /* This is not necessarily a problem, since backers are only
732 * created on demand. */
736 dpif_wait(backer->dpif);
739 /* Basic life-cycle. */
741 static int add_internal_flows(struct ofproto_dpif *);
743 static struct ofproto *
746 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
751 dealloc(struct ofproto *ofproto_)
753 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
758 close_dpif_backer(struct dpif_backer *backer)
760 ovs_assert(backer->refcount > 0);
762 if (--backer->refcount) {
766 udpif_destroy(backer->udpif);
768 simap_destroy(&backer->tnl_backers);
769 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
770 hmap_destroy(&backer->odp_to_ofport_map);
771 shash_find_and_delete(&all_dpif_backers, backer->type);
773 dpif_close(backer->dpif);
778 /* Datapath port slated for removal from datapath. */
780 struct list list_node;
784 static bool check_variable_length_userdata(struct dpif_backer *backer);
787 open_dpif_backer(const char *type, struct dpif_backer **backerp)
789 struct dpif_backer *backer;
790 struct dpif_port_dump port_dump;
791 struct dpif_port port;
792 struct shash_node *node;
793 struct list garbage_list;
794 struct odp_garbage *garbage, *next;
800 backer = shash_find_data(&all_dpif_backers, type);
807 backer_name = xasprintf("ovs-%s", type);
809 /* Remove any existing datapaths, since we assume we're the only
810 * userspace controlling the datapath. */
812 dp_enumerate_names(type, &names);
813 SSET_FOR_EACH(name, &names) {
814 struct dpif *old_dpif;
816 /* Don't remove our backer if it exists. */
817 if (!strcmp(name, backer_name)) {
821 if (dpif_open(name, type, &old_dpif)) {
822 VLOG_WARN("couldn't open old datapath %s to remove it", name);
824 dpif_delete(old_dpif);
825 dpif_close(old_dpif);
828 sset_destroy(&names);
830 backer = xmalloc(sizeof *backer);
832 error = dpif_create_and_open(backer_name, type, &backer->dpif);
835 VLOG_ERR("failed to open datapath of type %s: %s", type,
836 ovs_strerror(error));
840 backer->udpif = udpif_create(backer, backer->dpif);
842 backer->type = xstrdup(type);
843 backer->refcount = 1;
844 hmap_init(&backer->odp_to_ofport_map);
845 ovs_rwlock_init(&backer->odp_to_ofport_lock);
846 backer->need_revalidate = 0;
847 simap_init(&backer->tnl_backers);
848 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
851 if (backer->recv_set_enable) {
852 dpif_flow_flush(backer->dpif);
855 /* Loop through the ports already on the datapath and remove any
856 * that we don't need anymore. */
857 list_init(&garbage_list);
858 dpif_port_dump_start(&port_dump, backer->dpif);
859 while (dpif_port_dump_next(&port_dump, &port)) {
860 node = shash_find(&init_ofp_ports, port.name);
861 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
862 garbage = xmalloc(sizeof *garbage);
863 garbage->odp_port = port.port_no;
864 list_push_front(&garbage_list, &garbage->list_node);
867 dpif_port_dump_done(&port_dump);
869 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
870 dpif_port_del(backer->dpif, garbage->odp_port);
871 list_remove(&garbage->list_node);
875 shash_add(&all_dpif_backers, type, backer);
877 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
879 VLOG_ERR("failed to listen on datapath of type %s: %s",
880 type, ovs_strerror(error));
881 close_dpif_backer(backer);
884 backer->variable_length_userdata = check_variable_length_userdata(backer);
886 if (backer->recv_set_enable) {
887 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
893 /* Tests whether 'backer''s datapath supports variable-length
894 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
895 * to disable some features on older datapaths that don't support this
898 * Returns false if 'backer' definitely does not support variable-length
899 * userdata, true if it seems to support them or if at least the error we get
902 check_variable_length_userdata(struct dpif_backer *backer)
904 struct eth_header *eth;
905 struct ofpbuf actions;
907 struct ofpbuf packet;
911 /* Compose a userspace action that will cause an ERANGE error on older
912 * datapaths that don't support variable-length userdata.
914 * We really test for using userdata longer than 8 bytes, but older
915 * datapaths accepted these, silently truncating the userdata to 8 bytes.
916 * The same older datapaths rejected userdata shorter than 8 bytes, so we
917 * test for that instead as a proxy for longer userdata support. */
918 ofpbuf_init(&actions, 64);
919 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
920 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
921 dpif_port_get_pid(backer->dpif, ODPP_NONE));
922 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
923 nl_msg_end_nested(&actions, start);
925 /* Compose an ODP flow key. The key is arbitrary but it must match the
926 * packet that we compose later. */
927 ofpbuf_init(&key, 64);
928 nl_msg_put_u32(&key, OVS_KEY_ATTR_IN_PORT, 0);
929 nl_msg_put_unspec_zero(&key, OVS_KEY_ATTR_ETHERNET,
930 sizeof(struct ovs_key_ethernet));
931 nl_msg_put_be16(&key, OVS_KEY_ATTR_ETHERTYPE, htons(0x1234));
933 /* Compose a packet that matches the key. */
934 ofpbuf_init(&packet, ETH_HEADER_LEN);
935 eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
936 eth->eth_type = htons(0x1234);
938 /* Execute the actions. On older datapaths this fails with -ERANGE, on
939 * newer datapaths it succeeds. */
940 error = dpif_execute(backer->dpif, key.data, key.size,
941 actions.data, actions.size, &packet, false);
943 ofpbuf_uninit(&packet);
945 ofpbuf_uninit(&actions);
949 /* Variable-length userdata is supported.
951 * Purge received packets to avoid processing the nonsense packet we
952 * sent to userspace, then report success. */
953 dpif_recv_purge(backer->dpif);
957 /* Variable-length userdata is not supported. */
958 VLOG_WARN("%s: datapath does not support variable-length userdata "
959 "feature (needs Linux 3.10+ or kernel module from OVS "
960 "1..11+). The NXAST_SAMPLE action will be ignored.",
961 dpif_name(backer->dpif));
965 /* Something odd happened. We're not sure whether variable-length
966 * userdata is supported. Default to "yes". */
967 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
968 dpif_name(backer->dpif), ovs_strerror(error));
974 construct(struct ofproto *ofproto_)
976 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
977 struct shash_node *node, *next;
980 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
985 ofproto->netflow = NULL;
986 ofproto->sflow = NULL;
987 ofproto->ipfix = NULL;
989 ofproto->dump_seq = 0;
990 hmap_init(&ofproto->bundles);
991 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
992 ofproto->mbridge = mbridge_create();
993 ofproto->has_bonded_bundles = false;
994 ofproto->lacp_enabled = false;
995 ovs_mutex_init(&ofproto->stats_mutex);
996 ovs_mutex_init(&ofproto->vsp_mutex);
998 guarded_list_init(&ofproto->pins);
1000 ofproto_dpif_unixctl_init();
1002 hmap_init(&ofproto->vlandev_map);
1003 hmap_init(&ofproto->realdev_vid_map);
1005 sset_init(&ofproto->ports);
1006 sset_init(&ofproto->ghost_ports);
1007 sset_init(&ofproto->port_poll_set);
1008 ofproto->port_poll_errno = 0;
1009 ofproto->change_seq = 0;
1011 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1012 struct iface_hint *iface_hint = node->data;
1014 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1015 /* Check if the datapath already has this port. */
1016 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1017 sset_add(&ofproto->ports, node->name);
1020 free(iface_hint->br_name);
1021 free(iface_hint->br_type);
1023 shash_delete(&init_ofp_ports, node);
1027 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1028 hash_string(ofproto->up.name, 0));
1029 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1031 ofproto_init_tables(ofproto_, N_TABLES);
1032 error = add_internal_flows(ofproto);
1033 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1039 add_internal_flow(struct ofproto_dpif *ofproto, int id,
1040 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1042 struct ofputil_flow_mod fm;
1045 match_init_catchall(&fm.match);
1047 match_set_reg(&fm.match, 0, id);
1048 fm.new_cookie = htonll(0);
1049 fm.cookie = htonll(0);
1050 fm.cookie_mask = htonll(0);
1051 fm.modify_cookie = false;
1052 fm.table_id = TBL_INTERNAL;
1053 fm.command = OFPFC_ADD;
1054 fm.idle_timeout = 0;
1055 fm.hard_timeout = 0;
1059 fm.ofpacts = ofpacts->data;
1060 fm.ofpacts_len = ofpacts->size;
1062 error = ofproto_flow_mod(&ofproto->up, &fm);
1064 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1065 id, ofperr_to_string(error));
1069 if (rule_dpif_lookup_in_table(ofproto, &fm.match.flow, NULL, TBL_INTERNAL,
1071 rule_dpif_unref(*rulep);
1080 add_internal_flows(struct ofproto_dpif *ofproto)
1082 struct ofpact_controller *controller;
1083 uint64_t ofpacts_stub[128 / 8];
1084 struct ofpbuf ofpacts;
1088 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1091 controller = ofpact_put_CONTROLLER(&ofpacts);
1092 controller->max_len = UINT16_MAX;
1093 controller->controller_id = 0;
1094 controller->reason = OFPR_NO_MATCH;
1095 ofpact_pad(&ofpacts);
1097 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1102 ofpbuf_clear(&ofpacts);
1103 error = add_internal_flow(ofproto, id++, &ofpacts,
1104 &ofproto->no_packet_in_rule);
1109 error = add_internal_flow(ofproto, id++, &ofpacts,
1110 &ofproto->drop_frags_rule);
1115 destruct(struct ofproto *ofproto_)
1117 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1118 struct rule_dpif *rule, *next_rule;
1119 struct ofproto_packet_in *pin, *next_pin;
1120 struct oftable *table;
1123 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1124 ovs_rwlock_wrlock(&xlate_rwlock);
1125 xlate_remove_ofproto(ofproto);
1126 ovs_rwlock_unlock(&xlate_rwlock);
1128 /* Discard any flow_miss_batches queued up for 'ofproto', avoiding a
1129 * use-after-free error. */
1130 udpif_revalidate(ofproto->backer->udpif);
1132 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1134 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1135 struct cls_cursor cursor;
1137 ovs_rwlock_rdlock(&table->cls.rwlock);
1138 cls_cursor_init(&cursor, &table->cls, NULL);
1139 ovs_rwlock_unlock(&table->cls.rwlock);
1140 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1141 ofproto_rule_delete(&ofproto->up, &rule->up);
1145 guarded_list_pop_all(&ofproto->pins, &pins);
1146 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1147 list_remove(&pin->list_node);
1148 free(CONST_CAST(void *, pin->up.packet));
1151 guarded_list_destroy(&ofproto->pins);
1153 mbridge_unref(ofproto->mbridge);
1155 netflow_unref(ofproto->netflow);
1156 dpif_sflow_unref(ofproto->sflow);
1157 hmap_destroy(&ofproto->bundles);
1158 mac_learning_unref(ofproto->ml);
1160 hmap_destroy(&ofproto->vlandev_map);
1161 hmap_destroy(&ofproto->realdev_vid_map);
1163 sset_destroy(&ofproto->ports);
1164 sset_destroy(&ofproto->ghost_ports);
1165 sset_destroy(&ofproto->port_poll_set);
1167 ovs_mutex_destroy(&ofproto->stats_mutex);
1168 ovs_mutex_destroy(&ofproto->vsp_mutex);
1170 close_dpif_backer(ofproto->backer);
1174 run(struct ofproto *ofproto_)
1176 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1177 uint64_t new_seq, new_dump_seq;
1179 if (mbridge_need_revalidate(ofproto->mbridge)) {
1180 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1181 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1182 mac_learning_flush(ofproto->ml);
1183 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1186 /* Do not perform any periodic activity required by 'ofproto' while
1187 * waiting for flow restore to complete. */
1188 if (!ofproto_get_flow_restore_wait()) {
1189 struct ofproto_packet_in *pin, *next_pin;
1192 guarded_list_pop_all(&ofproto->pins, &pins);
1193 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1194 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1195 list_remove(&pin->list_node);
1196 free(CONST_CAST(void *, pin->up.packet));
1201 if (ofproto->netflow) {
1202 netflow_run(ofproto->netflow);
1204 if (ofproto->sflow) {
1205 dpif_sflow_run(ofproto->sflow);
1207 if (ofproto->ipfix) {
1208 dpif_ipfix_run(ofproto->ipfix);
1211 new_seq = seq_read(connectivity_seq_get());
1212 if (ofproto->change_seq != new_seq) {
1213 struct ofport_dpif *ofport;
1215 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1219 ofproto->change_seq = new_seq;
1221 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1222 struct ofbundle *bundle;
1224 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1230 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1231 if (mac_learning_run(ofproto->ml)) {
1232 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1234 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1236 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1237 if (ofproto->dump_seq != new_dump_seq) {
1238 struct rule *rule, *next_rule;
1240 /* We know stats are relatively fresh, so now is a good time to do some
1242 ofproto->dump_seq = new_dump_seq;
1244 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1246 ovs_mutex_lock(&ofproto_mutex);
1247 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1248 &ofproto->up.expirable) {
1249 rule_expire(rule_dpif_cast(rule));
1251 ovs_mutex_unlock(&ofproto_mutex);
1253 /* All outstanding data in existing flows has been accounted, so it's a
1254 * good time to do bond rebalancing. */
1255 if (ofproto->has_bonded_bundles) {
1256 struct ofbundle *bundle;
1258 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1260 bond_rebalance(bundle->bond);
1270 wait(struct ofproto *ofproto_)
1272 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1274 if (ofproto_get_flow_restore_wait()) {
1278 if (ofproto->sflow) {
1279 dpif_sflow_wait(ofproto->sflow);
1281 if (ofproto->ipfix) {
1282 dpif_ipfix_wait(ofproto->ipfix);
1284 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1285 struct ofbundle *bundle;
1287 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1288 bundle_wait(bundle);
1291 if (ofproto->netflow) {
1292 netflow_wait(ofproto->netflow);
1294 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1295 mac_learning_wait(ofproto->ml);
1296 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1298 if (ofproto->backer->need_revalidate) {
1299 /* Shouldn't happen, but if it does just go around again. */
1300 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1301 poll_immediate_wake();
1304 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1308 type_get_memory_usage(const char *type, struct simap *usage)
1310 struct dpif_backer *backer;
1312 backer = shash_find_data(&all_dpif_backers, type);
1314 udpif_get_memory_usage(backer->udpif, usage);
1319 flush(struct ofproto *ofproto OVS_UNUSED)
1325 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1326 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1328 *arp_match_ip = true;
1329 *actions = (OFPUTIL_A_OUTPUT |
1330 OFPUTIL_A_SET_VLAN_VID |
1331 OFPUTIL_A_SET_VLAN_PCP |
1332 OFPUTIL_A_STRIP_VLAN |
1333 OFPUTIL_A_SET_DL_SRC |
1334 OFPUTIL_A_SET_DL_DST |
1335 OFPUTIL_A_SET_NW_SRC |
1336 OFPUTIL_A_SET_NW_DST |
1337 OFPUTIL_A_SET_NW_TOS |
1338 OFPUTIL_A_SET_TP_SRC |
1339 OFPUTIL_A_SET_TP_DST |
1344 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1346 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1347 struct dpif_dp_stats s;
1348 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1351 strcpy(ots->name, "classifier");
1353 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1354 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes);
1355 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes);
1356 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes);
1358 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1359 ots->lookup_count = htonll(n_lookup);
1360 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1363 static struct ofport *
1366 struct ofport_dpif *port = xmalloc(sizeof *port);
1371 port_dealloc(struct ofport *port_)
1373 struct ofport_dpif *port = ofport_dpif_cast(port_);
1378 port_construct(struct ofport *port_)
1380 struct ofport_dpif *port = ofport_dpif_cast(port_);
1381 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1382 const struct netdev *netdev = port->up.netdev;
1383 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1384 struct dpif_port dpif_port;
1387 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1388 port->bundle = NULL;
1391 port->may_enable = true;
1392 port->stp_port = NULL;
1393 port->stp_state = STP_DISABLED;
1394 port->is_tunnel = false;
1398 port->realdev_ofp_port = 0;
1399 port->vlandev_vid = 0;
1400 port->carrier_seq = netdev_get_carrier_resets(netdev);
1401 port->is_layer3 = netdev_vport_is_layer3(netdev);
1403 if (netdev_vport_is_patch(netdev)) {
1404 /* By bailing out here, we don't submit the port to the sFlow module
1405 * to be considered for counter polling export. This is correct
1406 * because the patch port represents an interface that sFlow considers
1407 * to be "internal" to the switch as a whole, and therefore not an
1408 * candidate for counter polling. */
1409 port->odp_port = ODPP_NONE;
1410 ofport_update_peer(port);
1414 error = dpif_port_query_by_name(ofproto->backer->dpif,
1415 netdev_vport_get_dpif_port(netdev, namebuf,
1422 port->odp_port = dpif_port.port_no;
1424 if (netdev_get_tunnel_config(netdev)) {
1425 tnl_port_add(port, port->up.netdev, port->odp_port);
1426 port->is_tunnel = true;
1428 /* Sanity-check that a mapping doesn't already exist. This
1429 * shouldn't happen for non-tunnel ports. */
1430 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1431 VLOG_ERR("port %s already has an OpenFlow port number",
1433 dpif_port_destroy(&dpif_port);
1437 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1438 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1439 hash_odp_port(port->odp_port));
1440 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1442 dpif_port_destroy(&dpif_port);
1444 if (ofproto->sflow) {
1445 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1452 port_destruct(struct ofport *port_)
1454 struct ofport_dpif *port = ofport_dpif_cast(port_);
1455 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1456 const char *devname = netdev_get_name(port->up.netdev);
1457 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1458 const char *dp_port_name;
1460 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1461 ovs_rwlock_wrlock(&xlate_rwlock);
1462 xlate_ofport_remove(port);
1463 ovs_rwlock_unlock(&xlate_rwlock);
1465 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1467 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1468 /* The underlying device is still there, so delete it. This
1469 * happens when the ofproto is being destroyed, since the caller
1470 * assumes that removal of attached ports will happen as part of
1472 if (!port->is_tunnel) {
1473 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1478 port->peer->peer = NULL;
1482 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1483 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1484 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1485 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1489 sset_find_and_delete(&ofproto->ports, devname);
1490 sset_find_and_delete(&ofproto->ghost_ports, devname);
1491 bundle_remove(port_);
1492 set_cfm(port_, NULL);
1493 set_bfd(port_, NULL);
1494 if (ofproto->sflow) {
1495 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1502 port_modified(struct ofport *port_)
1504 struct ofport_dpif *port = ofport_dpif_cast(port_);
1506 if (port->bundle && port->bundle->bond) {
1507 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1511 cfm_set_netdev(port->cfm, port->up.netdev);
1515 bfd_set_netdev(port->bfd, port->up.netdev);
1518 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1519 port->up.pp.hw_addr);
1521 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1523 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1527 ofport_update_peer(port);
1531 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1533 struct ofport_dpif *port = ofport_dpif_cast(port_);
1534 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1535 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1537 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1538 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1539 OFPUTIL_PC_NO_PACKET_IN)) {
1540 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1542 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1543 bundle_update(port->bundle);
1549 set_sflow(struct ofproto *ofproto_,
1550 const struct ofproto_sflow_options *sflow_options)
1552 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1553 struct dpif_sflow *ds = ofproto->sflow;
1555 if (sflow_options) {
1557 struct ofport_dpif *ofport;
1559 ds = ofproto->sflow = dpif_sflow_create();
1560 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1561 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1563 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1565 dpif_sflow_set_options(ds, sflow_options);
1568 dpif_sflow_unref(ds);
1569 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1570 ofproto->sflow = NULL;
1578 struct ofproto *ofproto_,
1579 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1580 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1581 size_t n_flow_exporters_options)
1583 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1584 struct dpif_ipfix *di = ofproto->ipfix;
1585 bool has_options = bridge_exporter_options || flow_exporters_options;
1587 if (has_options && !di) {
1588 di = ofproto->ipfix = dpif_ipfix_create();
1592 /* Call set_options in any case to cleanly flush the flow
1593 * caches in the last exporters that are to be destroyed. */
1594 dpif_ipfix_set_options(
1595 di, bridge_exporter_options, flow_exporters_options,
1596 n_flow_exporters_options);
1599 dpif_ipfix_unref(di);
1600 ofproto->ipfix = NULL;
1608 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1610 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1615 struct ofproto_dpif *ofproto;
1617 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1618 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1619 ofport->cfm = cfm_create(ofport->up.netdev);
1622 if (cfm_configure(ofport->cfm, s)) {
1629 cfm_unref(ofport->cfm);
1632 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1633 ofport->up.pp.hw_addr);
1638 get_cfm_status(const struct ofport *ofport_,
1639 struct ofproto_cfm_status *status)
1641 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1644 status->faults = cfm_get_fault(ofport->cfm);
1645 status->flap_count = cfm_get_flap_count(ofport->cfm);
1646 status->remote_opstate = cfm_get_opup(ofport->cfm);
1647 status->health = cfm_get_health(ofport->cfm);
1648 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1656 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1658 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1659 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1663 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1664 cfg, ofport->up.netdev);
1665 if (ofport->bfd != old) {
1666 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1668 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1669 ofport->up.pp.hw_addr);
1674 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1676 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1679 bfd_get_status(ofport->bfd, smap);
1686 /* Spanning Tree. */
1689 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1691 struct ofproto_dpif *ofproto = ofproto_;
1692 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1693 struct ofport_dpif *ofport;
1695 ofport = stp_port_get_aux(sp);
1697 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1698 ofproto->up.name, port_num);
1700 struct eth_header *eth = pkt->l2;
1702 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1703 if (eth_addr_is_zero(eth->eth_src)) {
1704 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1705 "with unknown MAC", ofproto->up.name, port_num);
1707 ofproto_dpif_send_packet(ofport, pkt);
1713 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1715 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1717 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1719 /* Only revalidate flows if the configuration changed. */
1720 if (!s != !ofproto->stp) {
1721 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1725 if (!ofproto->stp) {
1726 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1727 send_bpdu_cb, ofproto);
1728 ofproto->stp_last_tick = time_msec();
1731 stp_set_bridge_id(ofproto->stp, s->system_id);
1732 stp_set_bridge_priority(ofproto->stp, s->priority);
1733 stp_set_hello_time(ofproto->stp, s->hello_time);
1734 stp_set_max_age(ofproto->stp, s->max_age);
1735 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1737 struct ofport *ofport;
1739 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1740 set_stp_port(ofport, NULL);
1743 stp_unref(ofproto->stp);
1744 ofproto->stp = NULL;
1751 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1753 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1757 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1758 s->designated_root = stp_get_designated_root(ofproto->stp);
1759 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1768 update_stp_port_state(struct ofport_dpif *ofport)
1770 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1771 enum stp_state state;
1773 /* Figure out new state. */
1774 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1778 if (ofport->stp_state != state) {
1779 enum ofputil_port_state of_state;
1782 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1783 netdev_get_name(ofport->up.netdev),
1784 stp_state_name(ofport->stp_state),
1785 stp_state_name(state));
1786 if (stp_learn_in_state(ofport->stp_state)
1787 != stp_learn_in_state(state)) {
1788 /* xxx Learning action flows should also be flushed. */
1789 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1790 mac_learning_flush(ofproto->ml);
1791 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1793 fwd_change = stp_forward_in_state(ofport->stp_state)
1794 != stp_forward_in_state(state);
1796 ofproto->backer->need_revalidate = REV_STP;
1797 ofport->stp_state = state;
1798 ofport->stp_state_entered = time_msec();
1800 if (fwd_change && ofport->bundle) {
1801 bundle_update(ofport->bundle);
1804 /* Update the STP state bits in the OpenFlow port description. */
1805 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1806 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1807 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1808 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1809 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1811 ofproto_port_set_state(&ofport->up, of_state);
1815 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1816 * caller is responsible for assigning STP port numbers and ensuring
1817 * there are no duplicates. */
1819 set_stp_port(struct ofport *ofport_,
1820 const struct ofproto_port_stp_settings *s)
1822 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1823 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1824 struct stp_port *sp = ofport->stp_port;
1826 if (!s || !s->enable) {
1828 ofport->stp_port = NULL;
1829 stp_port_disable(sp);
1830 update_stp_port_state(ofport);
1833 } else if (sp && stp_port_no(sp) != s->port_num
1834 && ofport == stp_port_get_aux(sp)) {
1835 /* The port-id changed, so disable the old one if it's not
1836 * already in use by another port. */
1837 stp_port_disable(sp);
1840 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1841 stp_port_enable(sp);
1843 stp_port_set_aux(sp, ofport);
1844 stp_port_set_priority(sp, s->priority);
1845 stp_port_set_path_cost(sp, s->path_cost);
1847 update_stp_port_state(ofport);
1853 get_stp_port_status(struct ofport *ofport_,
1854 struct ofproto_port_stp_status *s)
1856 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1857 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1858 struct stp_port *sp = ofport->stp_port;
1860 if (!ofproto->stp || !sp) {
1866 s->port_id = stp_port_get_id(sp);
1867 s->state = stp_port_get_state(sp);
1868 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1869 s->role = stp_port_get_role(sp);
1875 get_stp_port_stats(struct ofport *ofport_,
1876 struct ofproto_port_stp_stats *s)
1878 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1879 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1880 struct stp_port *sp = ofport->stp_port;
1882 if (!ofproto->stp || !sp) {
1888 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1894 stp_run(struct ofproto_dpif *ofproto)
1897 long long int now = time_msec();
1898 long long int elapsed = now - ofproto->stp_last_tick;
1899 struct stp_port *sp;
1902 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1903 ofproto->stp_last_tick = now;
1905 while (stp_get_changed_port(ofproto->stp, &sp)) {
1906 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1909 update_stp_port_state(ofport);
1913 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1914 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1915 mac_learning_flush(ofproto->ml);
1916 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1922 stp_wait(struct ofproto_dpif *ofproto)
1925 poll_timer_wait(1000);
1930 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
1933 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1934 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1936 if (ofport->n_qdscp != n_qdscp
1937 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
1938 n_qdscp * sizeof *qdscp))) {
1939 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1940 free(ofport->qdscp);
1941 ofport->qdscp = n_qdscp
1942 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
1944 ofport->n_qdscp = n_qdscp;
1952 /* Expires all MAC learning entries associated with 'bundle' and forces its
1953 * ofproto to revalidate every flow.
1955 * Normally MAC learning entries are removed only from the ofproto associated
1956 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
1957 * are removed from every ofproto. When patch ports and SLB bonds are in use
1958 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
1959 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
1960 * with the host from which it migrated. */
1962 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
1964 struct ofproto_dpif *ofproto = bundle->ofproto;
1965 struct mac_learning *ml = ofproto->ml;
1966 struct mac_entry *mac, *next_mac;
1968 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1969 ovs_rwlock_wrlock(&ml->rwlock);
1970 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
1971 if (mac->port.p == bundle) {
1973 struct ofproto_dpif *o;
1975 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
1977 struct mac_entry *e;
1979 ovs_rwlock_wrlock(&o->ml->rwlock);
1980 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
1982 mac_learning_expire(o->ml, e);
1984 ovs_rwlock_unlock(&o->ml->rwlock);
1989 mac_learning_expire(ml, mac);
1992 ovs_rwlock_unlock(&ml->rwlock);
1995 static struct ofbundle *
1996 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
1998 struct ofbundle *bundle;
2000 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2001 &ofproto->bundles) {
2002 if (bundle->aux == aux) {
2010 bundle_update(struct ofbundle *bundle)
2012 struct ofport_dpif *port;
2014 bundle->floodable = true;
2015 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2016 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2018 || !stp_forward_in_state(port->stp_state)) {
2019 bundle->floodable = false;
2026 bundle_del_port(struct ofport_dpif *port)
2028 struct ofbundle *bundle = port->bundle;
2030 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2032 list_remove(&port->bundle_node);
2033 port->bundle = NULL;
2036 lacp_slave_unregister(bundle->lacp, port);
2039 bond_slave_unregister(bundle->bond, port);
2042 bundle_update(bundle);
2046 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2047 struct lacp_slave_settings *lacp)
2049 struct ofport_dpif *port;
2051 port = get_ofp_port(bundle->ofproto, ofp_port);
2056 if (port->bundle != bundle) {
2057 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2059 bundle_remove(&port->up);
2062 port->bundle = bundle;
2063 list_push_back(&bundle->ports, &port->bundle_node);
2064 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2066 || !stp_forward_in_state(port->stp_state)) {
2067 bundle->floodable = false;
2071 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2072 lacp_slave_register(bundle->lacp, port, lacp);
2079 bundle_destroy(struct ofbundle *bundle)
2081 struct ofproto_dpif *ofproto;
2082 struct ofport_dpif *port, *next_port;
2088 ofproto = bundle->ofproto;
2089 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2091 ovs_rwlock_wrlock(&xlate_rwlock);
2092 xlate_bundle_remove(bundle);
2093 ovs_rwlock_unlock(&xlate_rwlock);
2095 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2096 bundle_del_port(port);
2099 bundle_flush_macs(bundle, true);
2100 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2102 free(bundle->trunks);
2103 lacp_unref(bundle->lacp);
2104 bond_unref(bundle->bond);
2109 bundle_set(struct ofproto *ofproto_, void *aux,
2110 const struct ofproto_bundle_settings *s)
2112 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2113 bool need_flush = false;
2114 struct ofport_dpif *port;
2115 struct ofbundle *bundle;
2116 unsigned long *trunks;
2122 bundle_destroy(bundle_lookup(ofproto, aux));
2126 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2127 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2129 bundle = bundle_lookup(ofproto, aux);
2131 bundle = xmalloc(sizeof *bundle);
2133 bundle->ofproto = ofproto;
2134 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2135 hash_pointer(aux, 0));
2137 bundle->name = NULL;
2139 list_init(&bundle->ports);
2140 bundle->vlan_mode = PORT_VLAN_TRUNK;
2142 bundle->trunks = NULL;
2143 bundle->use_priority_tags = s->use_priority_tags;
2144 bundle->lacp = NULL;
2145 bundle->bond = NULL;
2147 bundle->floodable = true;
2148 mbridge_register_bundle(ofproto->mbridge, bundle);
2151 if (!bundle->name || strcmp(s->name, bundle->name)) {
2153 bundle->name = xstrdup(s->name);
2158 ofproto->lacp_enabled = true;
2159 if (!bundle->lacp) {
2160 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2161 bundle->lacp = lacp_create();
2163 lacp_configure(bundle->lacp, s->lacp);
2165 lacp_unref(bundle->lacp);
2166 bundle->lacp = NULL;
2169 /* Update set of ports. */
2171 for (i = 0; i < s->n_slaves; i++) {
2172 if (!bundle_add_port(bundle, s->slaves[i],
2173 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2177 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2178 struct ofport_dpif *next_port;
2180 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2181 for (i = 0; i < s->n_slaves; i++) {
2182 if (s->slaves[i] == port->up.ofp_port) {
2187 bundle_del_port(port);
2191 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2193 if (list_is_empty(&bundle->ports)) {
2194 bundle_destroy(bundle);
2198 /* Set VLAN tagging mode */
2199 if (s->vlan_mode != bundle->vlan_mode
2200 || s->use_priority_tags != bundle->use_priority_tags) {
2201 bundle->vlan_mode = s->vlan_mode;
2202 bundle->use_priority_tags = s->use_priority_tags;
2207 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2208 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2210 if (vlan != bundle->vlan) {
2211 bundle->vlan = vlan;
2215 /* Get trunked VLANs. */
2216 switch (s->vlan_mode) {
2217 case PORT_VLAN_ACCESS:
2221 case PORT_VLAN_TRUNK:
2222 trunks = CONST_CAST(unsigned long *, s->trunks);
2225 case PORT_VLAN_NATIVE_UNTAGGED:
2226 case PORT_VLAN_NATIVE_TAGGED:
2227 if (vlan != 0 && (!s->trunks
2228 || !bitmap_is_set(s->trunks, vlan)
2229 || bitmap_is_set(s->trunks, 0))) {
2230 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2232 trunks = bitmap_clone(s->trunks, 4096);
2234 trunks = bitmap_allocate1(4096);
2236 bitmap_set1(trunks, vlan);
2237 bitmap_set0(trunks, 0);
2239 trunks = CONST_CAST(unsigned long *, s->trunks);
2246 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2247 free(bundle->trunks);
2248 if (trunks == s->trunks) {
2249 bundle->trunks = vlan_bitmap_clone(trunks);
2251 bundle->trunks = trunks;
2256 if (trunks != s->trunks) {
2261 if (!list_is_short(&bundle->ports)) {
2262 bundle->ofproto->has_bonded_bundles = true;
2264 if (bond_reconfigure(bundle->bond, s->bond)) {
2265 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2268 bundle->bond = bond_create(s->bond);
2269 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2272 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2273 bond_slave_register(bundle->bond, port, port->up.netdev);
2276 bond_unref(bundle->bond);
2277 bundle->bond = NULL;
2280 /* If we changed something that would affect MAC learning, un-learn
2281 * everything on this port and force flow revalidation. */
2283 bundle_flush_macs(bundle, false);
2290 bundle_remove(struct ofport *port_)
2292 struct ofport_dpif *port = ofport_dpif_cast(port_);
2293 struct ofbundle *bundle = port->bundle;
2296 bundle_del_port(port);
2297 if (list_is_empty(&bundle->ports)) {
2298 bundle_destroy(bundle);
2299 } else if (list_is_short(&bundle->ports)) {
2300 bond_unref(bundle->bond);
2301 bundle->bond = NULL;
2307 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2309 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2310 struct ofport_dpif *port = port_;
2311 uint8_t ea[ETH_ADDR_LEN];
2314 error = netdev_get_etheraddr(port->up.netdev, ea);
2316 struct ofpbuf packet;
2319 ofpbuf_init(&packet, 0);
2320 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2322 memcpy(packet_pdu, pdu, pdu_size);
2324 ofproto_dpif_send_packet(port, &packet);
2325 ofpbuf_uninit(&packet);
2327 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2328 "%s (%s)", port->bundle->name,
2329 netdev_get_name(port->up.netdev), ovs_strerror(error));
2334 bundle_send_learning_packets(struct ofbundle *bundle)
2336 struct ofproto_dpif *ofproto = bundle->ofproto;
2337 struct ofpbuf *learning_packet;
2338 int error, n_packets, n_errors;
2339 struct mac_entry *e;
2340 struct list packets;
2342 list_init(&packets);
2343 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2344 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2345 if (e->port.p != bundle) {
2348 learning_packet = bond_compose_learning_packet(bundle->bond,
2351 learning_packet->private_p = port_void;
2352 list_push_back(&packets, &learning_packet->list_node);
2355 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2357 error = n_packets = n_errors = 0;
2358 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2361 ret = ofproto_dpif_send_packet(learning_packet->private_p, learning_packet);
2368 ofpbuf_list_delete(&packets);
2371 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2372 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2373 "packets, last error was: %s",
2374 bundle->name, n_errors, n_packets, ovs_strerror(error));
2376 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2377 bundle->name, n_packets);
2382 bundle_run(struct ofbundle *bundle)
2385 lacp_run(bundle->lacp, send_pdu_cb);
2388 struct ofport_dpif *port;
2390 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2391 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2394 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2395 bundle->ofproto->backer->need_revalidate = REV_BOND;
2398 if (bond_should_send_learning_packets(bundle->bond)) {
2399 bundle_send_learning_packets(bundle);
2405 bundle_wait(struct ofbundle *bundle)
2408 lacp_wait(bundle->lacp);
2411 bond_wait(bundle->bond);
2418 mirror_set__(struct ofproto *ofproto_, void *aux,
2419 const struct ofproto_mirror_settings *s)
2421 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2422 struct ofbundle **srcs, **dsts;
2427 mirror_destroy(ofproto->mbridge, aux);
2431 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2432 dsts = xmalloc(s->n_dsts * sizeof *dsts);
2434 for (i = 0; i < s->n_srcs; i++) {
2435 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2438 for (i = 0; i < s->n_dsts; i++) {
2439 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2442 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2443 s->n_dsts, s->src_vlans,
2444 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2451 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2452 uint64_t *packets, uint64_t *bytes)
2454 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2459 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2461 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2462 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2463 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2464 mac_learning_flush(ofproto->ml);
2466 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2471 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2473 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2474 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2475 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2479 forward_bpdu_changed(struct ofproto *ofproto_)
2481 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2482 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2486 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2489 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2490 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2491 mac_learning_set_idle_time(ofproto->ml, idle_time);
2492 mac_learning_set_max_entries(ofproto->ml, max_entries);
2493 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2498 static struct ofport_dpif *
2499 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2501 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2502 return ofport ? ofport_dpif_cast(ofport) : NULL;
2506 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2507 struct ofproto_port *ofproto_port,
2508 struct dpif_port *dpif_port)
2510 ofproto_port->name = dpif_port->name;
2511 ofproto_port->type = dpif_port->type;
2512 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2516 ofport_update_peer(struct ofport_dpif *ofport)
2518 const struct ofproto_dpif *ofproto;
2519 struct dpif_backer *backer;
2522 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2526 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2527 backer->need_revalidate = REV_RECONFIGURE;
2530 ofport->peer->peer = NULL;
2531 ofport->peer = NULL;
2534 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2539 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2540 struct ofport *peer_ofport;
2541 struct ofport_dpif *peer;
2544 if (ofproto->backer != backer) {
2548 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2553 peer = ofport_dpif_cast(peer_ofport);
2554 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2555 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2557 ofport->peer = peer;
2558 ofport->peer->peer = ofport;
2568 port_run(struct ofport_dpif *ofport)
2570 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2571 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2572 bool enable = netdev_get_carrier(ofport->up.netdev);
2573 bool cfm_enable = false;
2574 bool bfd_enable = false;
2576 ofport->carrier_seq = carrier_seq;
2579 int cfm_opup = cfm_get_opup(ofport->cfm);
2581 cfm_enable = !cfm_get_fault(ofport->cfm);
2583 if (cfm_opup >= 0) {
2584 cfm_enable = cfm_enable && cfm_opup;
2589 bfd_enable = bfd_forwarding(ofport->bfd);
2592 if (ofport->bfd || ofport->cfm) {
2593 enable = enable && (cfm_enable || bfd_enable);
2596 if (ofport->bundle) {
2597 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2598 if (carrier_changed) {
2599 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2603 if (ofport->may_enable != enable) {
2604 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2605 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2608 ofport->may_enable = enable;
2612 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2613 struct ofproto_port *ofproto_port)
2615 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2616 struct dpif_port dpif_port;
2619 if (sset_contains(&ofproto->ghost_ports, devname)) {
2620 const char *type = netdev_get_type_from_name(devname);
2622 /* We may be called before ofproto->up.port_by_name is populated with
2623 * the appropriate ofport. For this reason, we must get the name and
2624 * type from the netdev layer directly. */
2626 const struct ofport *ofport;
2628 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2629 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2630 ofproto_port->name = xstrdup(devname);
2631 ofproto_port->type = xstrdup(type);
2637 if (!sset_contains(&ofproto->ports, devname)) {
2640 error = dpif_port_query_by_name(ofproto->backer->dpif,
2641 devname, &dpif_port);
2643 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2649 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2651 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2652 const char *devname = netdev_get_name(netdev);
2653 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2654 const char *dp_port_name;
2656 if (netdev_vport_is_patch(netdev)) {
2657 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2661 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2662 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2663 odp_port_t port_no = ODPP_NONE;
2666 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2670 if (netdev_get_tunnel_config(netdev)) {
2671 simap_put(&ofproto->backer->tnl_backers,
2672 dp_port_name, odp_to_u32(port_no));
2676 if (netdev_get_tunnel_config(netdev)) {
2677 sset_add(&ofproto->ghost_ports, devname);
2679 sset_add(&ofproto->ports, devname);
2685 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2687 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2688 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2695 sset_find_and_delete(&ofproto->ghost_ports,
2696 netdev_get_name(ofport->up.netdev));
2697 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2698 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2699 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2701 /* The caller is going to close ofport->up.netdev. If this is a
2702 * bonded port, then the bond is using that netdev, so remove it
2703 * from the bond. The client will need to reconfigure everything
2704 * after deleting ports, so then the slave will get re-added. */
2705 bundle_remove(&ofport->up);
2712 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2714 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2717 error = netdev_get_stats(ofport->up.netdev, stats);
2719 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2720 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2722 ovs_mutex_lock(&ofproto->stats_mutex);
2723 /* ofproto->stats.tx_packets represents packets that we created
2724 * internally and sent to some port (e.g. packets sent with
2725 * ofproto_dpif_send_packet()). Account for them as if they had
2726 * come from OFPP_LOCAL and got forwarded. */
2728 if (stats->rx_packets != UINT64_MAX) {
2729 stats->rx_packets += ofproto->stats.tx_packets;
2732 if (stats->rx_bytes != UINT64_MAX) {
2733 stats->rx_bytes += ofproto->stats.tx_bytes;
2736 /* ofproto->stats.rx_packets represents packets that were received on
2737 * some port and we processed internally and dropped (e.g. STP).
2738 * Account for them as if they had been forwarded to OFPP_LOCAL. */
2740 if (stats->tx_packets != UINT64_MAX) {
2741 stats->tx_packets += ofproto->stats.rx_packets;
2744 if (stats->tx_bytes != UINT64_MAX) {
2745 stats->tx_bytes += ofproto->stats.rx_bytes;
2747 ovs_mutex_unlock(&ofproto->stats_mutex);
2753 struct port_dump_state {
2758 struct ofproto_port port;
2763 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
2765 *statep = xzalloc(sizeof(struct port_dump_state));
2770 port_dump_next(const struct ofproto *ofproto_, void *state_,
2771 struct ofproto_port *port)
2773 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2774 struct port_dump_state *state = state_;
2775 const struct sset *sset;
2776 struct sset_node *node;
2778 if (state->has_port) {
2779 ofproto_port_destroy(&state->port);
2780 state->has_port = false;
2782 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
2783 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
2786 error = port_query_by_name(ofproto_, node->name, &state->port);
2788 *port = state->port;
2789 state->has_port = true;
2791 } else if (error != ENODEV) {
2796 if (!state->ghost) {
2797 state->ghost = true;
2800 return port_dump_next(ofproto_, state_, port);
2807 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2809 struct port_dump_state *state = state_;
2811 if (state->has_port) {
2812 ofproto_port_destroy(&state->port);
2819 port_poll(const struct ofproto *ofproto_, char **devnamep)
2821 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2823 if (ofproto->port_poll_errno) {
2824 int error = ofproto->port_poll_errno;
2825 ofproto->port_poll_errno = 0;
2829 if (sset_is_empty(&ofproto->port_poll_set)) {
2833 *devnamep = sset_pop(&ofproto->port_poll_set);
2838 port_poll_wait(const struct ofproto *ofproto_)
2840 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2841 dpif_port_poll_wait(ofproto->backer->dpif);
2845 port_is_lacp_current(const struct ofport *ofport_)
2847 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2848 return (ofport->bundle && ofport->bundle->lacp
2849 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
2853 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
2854 * then delete it entirely. */
2856 rule_expire(struct rule_dpif *rule)
2857 OVS_REQUIRES(ofproto_mutex)
2859 uint16_t idle_timeout, hard_timeout;
2860 long long int now = time_msec();
2863 ovs_assert(!rule->up.pending);
2865 /* Has 'rule' expired? */
2866 ovs_mutex_lock(&rule->up.mutex);
2867 hard_timeout = rule->up.hard_timeout;
2868 idle_timeout = rule->up.idle_timeout;
2869 if (hard_timeout && now > rule->up.modified + hard_timeout * 1000) {
2870 reason = OFPRR_HARD_TIMEOUT;
2871 } else if (idle_timeout && now > rule->up.used + idle_timeout * 1000) {
2872 reason = OFPRR_IDLE_TIMEOUT;
2876 ovs_mutex_unlock(&rule->up.mutex);
2879 COVERAGE_INC(ofproto_dpif_expired);
2880 ofproto_rule_expire(&rule->up, reason);
2884 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
2885 * 'flow' must reflect the data in 'packet'. */
2887 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
2888 const struct flow *flow,
2889 struct rule_dpif *rule,
2890 const struct ofpact *ofpacts, size_t ofpacts_len,
2891 struct ofpbuf *packet)
2893 struct odputil_keybuf keybuf;
2894 struct dpif_flow_stats stats;
2895 struct xlate_out xout;
2896 struct xlate_in xin;
2901 ovs_assert((rule != NULL) != (ofpacts != NULL));
2903 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
2905 rule_dpif_credit_stats(rule, &stats);
2908 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
2909 xin.ofpacts = ofpacts;
2910 xin.ofpacts_len = ofpacts_len;
2911 xin.resubmit_stats = &stats;
2912 xlate_actions(&xin, &xout);
2914 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
2915 in_port = flow->in_port.ofp_port;
2916 if (in_port == OFPP_NONE) {
2917 in_port = OFPP_LOCAL;
2919 odp_flow_key_from_flow(&key, flow, ofp_port_to_odp_port(ofproto, in_port));
2921 error = dpif_execute(ofproto->backer->dpif, key.data, key.size,
2922 xout.odp_actions.data, xout.odp_actions.size, packet,
2923 (xout.slow & SLOW_ACTION) != 0);
2924 xlate_out_uninit(&xout);
2930 rule_dpif_credit_stats(struct rule_dpif *rule,
2931 const struct dpif_flow_stats *stats)
2933 ovs_mutex_lock(&rule->stats_mutex);
2934 rule->packet_count += stats->n_packets;
2935 rule->byte_count += stats->n_bytes;
2936 rule->up.used = MAX(rule->up.used, stats->used);
2937 ovs_mutex_unlock(&rule->stats_mutex);
2941 rule_dpif_is_fail_open(const struct rule_dpif *rule)
2943 return is_fail_open_rule(&rule->up);
2947 rule_dpif_is_table_miss(const struct rule_dpif *rule)
2949 return rule_is_table_miss(&rule->up);
2953 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
2954 OVS_REQUIRES(rule->up.mutex)
2956 return rule->up.flow_cookie;
2960 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
2961 uint16_t hard_timeout)
2963 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
2966 /* Returns 'rule''s actions. The caller owns a reference on the returned
2967 * actions and must eventually release it (with rule_actions_unref()) to avoid
2969 struct rule_actions *
2970 rule_dpif_get_actions(const struct rule_dpif *rule)
2972 return rule_get_actions(&rule->up);
2975 /* Lookup 'flow' in 'ofproto''s classifier. If 'wc' is non-null, sets
2976 * the fields that were relevant as part of the lookup. */
2978 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
2979 struct flow_wildcards *wc, struct rule_dpif **rule)
2981 struct ofport_dpif *port;
2983 if (rule_dpif_lookup_in_table(ofproto, flow, wc, 0, rule)) {
2986 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
2988 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
2989 flow->in_port.ofp_port);
2992 choose_miss_rule(port ? port->up.pp.config : 0, ofproto->miss_rule,
2993 ofproto->no_packet_in_rule, rule);
2997 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto,
2998 const struct flow *flow, struct flow_wildcards *wc,
2999 uint8_t table_id, struct rule_dpif **rule)
3001 const struct cls_rule *cls_rule;
3002 struct classifier *cls;
3006 if (table_id >= N_TABLES) {
3011 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3012 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3015 cls = &ofproto->up.tables[table_id].cls;
3016 ovs_rwlock_rdlock(&cls->rwlock);
3017 frag = (flow->nw_frag & FLOW_NW_FRAG_ANY) != 0;
3018 if (frag && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3019 /* We must pretend that transport ports are unavailable. */
3020 struct flow ofpc_normal_flow = *flow;
3021 ofpc_normal_flow.tp_src = htons(0);
3022 ofpc_normal_flow.tp_dst = htons(0);
3023 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3024 } else if (frag && ofproto->up.frag_handling == OFPC_FRAG_DROP) {
3025 cls_rule = &ofproto->drop_frags_rule->up.cr;
3026 /* Frag mask in wc already set above. */
3028 cls_rule = classifier_lookup(cls, flow, wc);
3031 *rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3032 rule_dpif_ref(*rule);
3033 ovs_rwlock_unlock(&cls->rwlock);
3035 return *rule != NULL;
3038 /* Given a port configuration (specified as zero if there's no port), chooses
3039 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3040 * flow table miss. */
3042 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3043 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
3045 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
3046 rule_dpif_ref(*rule);
3050 rule_dpif_ref(struct rule_dpif *rule)
3053 ofproto_rule_ref(&rule->up);
3058 rule_dpif_unref(struct rule_dpif *rule)
3061 ofproto_rule_unref(&rule->up);
3066 complete_operation(struct rule_dpif *rule)
3067 OVS_REQUIRES(ofproto_mutex)
3069 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3071 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3072 ofoperation_complete(rule->up.pending, 0);
3075 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3077 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3080 static struct rule *
3083 struct rule_dpif *rule = xmalloc(sizeof *rule);
3088 rule_dealloc(struct rule *rule_)
3090 struct rule_dpif *rule = rule_dpif_cast(rule_);
3095 rule_construct(struct rule *rule_)
3097 struct rule_dpif *rule = rule_dpif_cast(rule_);
3098 ovs_mutex_init(&rule->stats_mutex);
3099 ovs_mutex_lock(&rule->stats_mutex);
3100 rule->packet_count = 0;
3101 rule->byte_count = 0;
3102 ovs_mutex_unlock(&rule->stats_mutex);
3107 rule_insert(struct rule *rule_)
3108 OVS_REQUIRES(ofproto_mutex)
3110 struct rule_dpif *rule = rule_dpif_cast(rule_);
3111 complete_operation(rule);
3115 rule_delete(struct rule *rule_)
3116 OVS_REQUIRES(ofproto_mutex)
3118 struct rule_dpif *rule = rule_dpif_cast(rule_);
3119 complete_operation(rule);
3123 rule_destruct(struct rule *rule_)
3125 struct rule_dpif *rule = rule_dpif_cast(rule_);
3126 ovs_mutex_destroy(&rule->stats_mutex);
3130 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
3132 struct rule_dpif *rule = rule_dpif_cast(rule_);
3134 ovs_mutex_lock(&rule->stats_mutex);
3135 *packets = rule->packet_count;
3136 *bytes = rule->byte_count;
3137 ovs_mutex_unlock(&rule->stats_mutex);
3141 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3142 struct ofpbuf *packet)
3144 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3146 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3150 rule_execute(struct rule *rule, const struct flow *flow,
3151 struct ofpbuf *packet)
3153 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3154 ofpbuf_delete(packet);
3159 rule_modify_actions(struct rule *rule_, bool reset_counters)
3160 OVS_REQUIRES(ofproto_mutex)
3162 struct rule_dpif *rule = rule_dpif_cast(rule_);
3164 if (reset_counters) {
3165 ovs_mutex_lock(&rule->stats_mutex);
3166 rule->packet_count = 0;
3167 rule->byte_count = 0;
3168 ovs_mutex_unlock(&rule->stats_mutex);
3171 complete_operation(rule);
3174 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3176 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3179 static struct ofgroup *
3182 struct group_dpif *group = xzalloc(sizeof *group);
3187 group_dealloc(struct ofgroup *group_)
3189 struct group_dpif *group = group_dpif_cast(group_);
3194 group_construct_stats(struct group_dpif *group)
3195 OVS_REQUIRES(group->stats_mutex)
3197 group->packet_count = 0;
3198 group->byte_count = 0;
3199 if (!group->bucket_stats) {
3200 group->bucket_stats = xcalloc(group->up.n_buckets,
3201 sizeof *group->bucket_stats);
3203 memset(group->bucket_stats, 0, group->up.n_buckets *
3204 sizeof *group->bucket_stats);
3209 group_construct(struct ofgroup *group_)
3211 struct group_dpif *group = group_dpif_cast(group_);
3212 ovs_mutex_init(&group->stats_mutex);
3213 ovs_mutex_lock(&group->stats_mutex);
3214 group_construct_stats(group);
3215 ovs_mutex_unlock(&group->stats_mutex);
3220 group_destruct__(struct group_dpif *group)
3221 OVS_REQUIRES(group->stats_mutex)
3223 free(group->bucket_stats);
3224 group->bucket_stats = NULL;
3228 group_destruct(struct ofgroup *group_)
3230 struct group_dpif *group = group_dpif_cast(group_);
3231 ovs_mutex_lock(&group->stats_mutex);
3232 group_destruct__(group);
3233 ovs_mutex_unlock(&group->stats_mutex);
3234 ovs_mutex_destroy(&group->stats_mutex);
3238 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3240 struct group_dpif *group = group_dpif_cast(group_);
3241 struct group_dpif *victim = group_dpif_cast(victim_);
3243 ovs_mutex_lock(&group->stats_mutex);
3244 if (victim->up.n_buckets < group->up.n_buckets) {
3245 group_destruct__(group);
3247 group_construct_stats(group);
3248 ovs_mutex_unlock(&group->stats_mutex);
3254 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3256 struct group_dpif *group = group_dpif_cast(group_);
3258 ovs_mutex_lock(&group->stats_mutex);
3259 ogs->packet_count = group->packet_count;
3260 ogs->byte_count = group->byte_count;
3261 memcpy(ogs->bucket_stats, group->bucket_stats,
3262 group->up.n_buckets * sizeof *group->bucket_stats);
3263 ovs_mutex_unlock(&group->stats_mutex);
3269 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3270 struct group_dpif **group)
3271 OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3273 struct ofgroup *ofgroup;
3277 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3278 *group = found ? group_dpif_cast(ofgroup) : NULL;
3284 group_dpif_release(struct group_dpif *group)
3285 OVS_RELEASES(group->up.rwlock)
3287 ofproto_group_release(&group->up);
3291 group_dpif_get_buckets(const struct group_dpif *group,
3292 const struct list **buckets)
3294 *buckets = &group->up.buckets;
3297 enum ofp11_group_type
3298 group_dpif_get_type(const struct group_dpif *group)
3300 return group->up.type;
3303 /* Sends 'packet' out 'ofport'.
3304 * May modify 'packet'.
3305 * Returns 0 if successful, otherwise a positive errno value. */
3307 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3309 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3312 error = xlate_send_packet(ofport, packet);
3314 ovs_mutex_lock(&ofproto->stats_mutex);
3315 ofproto->stats.tx_packets++;
3316 ofproto->stats.tx_bytes += packet->size;
3317 ovs_mutex_unlock(&ofproto->stats_mutex);
3322 set_frag_handling(struct ofproto *ofproto_,
3323 enum ofp_config_flags frag_handling)
3325 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3326 if (frag_handling != OFPC_FRAG_REASM) {
3327 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3335 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3336 const struct flow *flow,
3337 const struct ofpact *ofpacts, size_t ofpacts_len)
3339 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3341 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3342 ofpacts_len, packet);
3349 set_netflow(struct ofproto *ofproto_,
3350 const struct netflow_options *netflow_options)
3352 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3354 if (netflow_options) {
3355 if (!ofproto->netflow) {
3356 ofproto->netflow = netflow_create();
3357 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3359 return netflow_set_options(ofproto->netflow, netflow_options);
3360 } else if (ofproto->netflow) {
3361 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3362 netflow_unref(ofproto->netflow);
3363 ofproto->netflow = NULL;
3370 get_netflow_ids(const struct ofproto *ofproto_,
3371 uint8_t *engine_type, uint8_t *engine_id)
3373 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3375 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3378 static struct ofproto_dpif *
3379 ofproto_dpif_lookup(const char *name)
3381 struct ofproto_dpif *ofproto;
3383 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3384 hash_string(name, 0), &all_ofproto_dpifs) {
3385 if (!strcmp(ofproto->up.name, name)) {
3393 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3394 const char *argv[], void *aux OVS_UNUSED)
3396 struct ofproto_dpif *ofproto;
3399 ofproto = ofproto_dpif_lookup(argv[1]);
3401 unixctl_command_reply_error(conn, "no such bridge");
3404 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3405 mac_learning_flush(ofproto->ml);
3406 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3408 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3409 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3410 mac_learning_flush(ofproto->ml);
3411 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3415 unixctl_command_reply(conn, "table successfully flushed");
3418 static struct ofport_dpif *
3419 ofbundle_get_a_port(const struct ofbundle *bundle)
3421 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3426 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3427 const char *argv[], void *aux OVS_UNUSED)
3429 struct ds ds = DS_EMPTY_INITIALIZER;
3430 const struct ofproto_dpif *ofproto;
3431 const struct mac_entry *e;
3433 ofproto = ofproto_dpif_lookup(argv[1]);
3435 unixctl_command_reply_error(conn, "no such bridge");
3439 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3440 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3441 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3442 struct ofbundle *bundle = e->port.p;
3443 char name[OFP_MAX_PORT_NAME_LEN];
3445 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3447 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
3448 name, e->vlan, ETH_ADDR_ARGS(e->mac),
3449 mac_entry_age(ofproto->ml, e));
3451 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3452 unixctl_command_reply(conn, ds_cstr(&ds));
3457 struct xlate_out xout;
3458 struct xlate_in xin;
3464 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
3466 struct rule_actions *actions;
3469 ds_put_char_multiple(result, '\t', level);
3471 ds_put_cstr(result, "No match\n");
3475 ovs_mutex_lock(&rule->up.mutex);
3476 cookie = rule->up.flow_cookie;
3477 ovs_mutex_unlock(&rule->up.mutex);
3479 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3480 rule ? rule->up.table_id : 0, ntohll(cookie));
3481 cls_rule_format(&rule->up.cr, result);
3482 ds_put_char(result, '\n');
3484 actions = rule_dpif_get_actions(rule);
3486 ds_put_char_multiple(result, '\t', level);
3487 ds_put_cstr(result, "OpenFlow actions=");
3488 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
3489 ds_put_char(result, '\n');
3491 rule_actions_unref(actions);
3495 trace_format_flow(struct ds *result, int level, const char *title,
3496 struct trace_ctx *trace)
3498 ds_put_char_multiple(result, '\t', level);
3499 ds_put_format(result, "%s: ", title);
3500 if (flow_equal(&trace->xin.flow, &trace->flow)) {
3501 ds_put_cstr(result, "unchanged");
3503 flow_format(result, &trace->xin.flow);
3504 trace->flow = trace->xin.flow;
3506 ds_put_char(result, '\n');
3510 trace_format_regs(struct ds *result, int level, const char *title,
3511 struct trace_ctx *trace)
3515 ds_put_char_multiple(result, '\t', level);
3516 ds_put_format(result, "%s:", title);
3517 for (i = 0; i < FLOW_N_REGS; i++) {
3518 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
3520 ds_put_char(result, '\n');
3524 trace_format_odp(struct ds *result, int level, const char *title,
3525 struct trace_ctx *trace)
3527 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
3529 ds_put_char_multiple(result, '\t', level);
3530 ds_put_format(result, "%s: ", title);
3531 format_odp_actions(result, odp_actions->data, odp_actions->size);
3532 ds_put_char(result, '\n');
3536 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3538 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3539 struct ds *result = trace->result;
3541 ds_put_char(result, '\n');
3542 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3543 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3544 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
3545 trace_format_rule(result, recurse + 1, rule);
3549 trace_report(struct xlate_in *xin, const char *s, int recurse)
3551 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3552 struct ds *result = trace->result;
3554 ds_put_char_multiple(result, '\t', recurse);
3555 ds_put_cstr(result, s);
3556 ds_put_char(result, '\n');
3559 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
3560 * forms are supported:
3562 * - [dpname] odp_flow [-generate | packet]
3563 * - bridge br_flow [-generate | packet]
3565 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
3566 * returns a nonnull malloced error message. */
3567 static char * WARN_UNUSED_RESULT
3568 parse_flow_and_packet(int argc, const char *argv[],
3569 struct ofproto_dpif **ofprotop, struct flow *flow,
3570 struct ofpbuf **packetp)
3572 const struct dpif_backer *backer = NULL;
3573 const char *error = NULL;
3575 struct simap port_names = SIMAP_INITIALIZER(&port_names);
3576 struct ofpbuf *packet;
3577 struct ofpbuf odp_key;
3578 struct ofpbuf odp_mask;
3580 ofpbuf_init(&odp_key, 0);
3581 ofpbuf_init(&odp_mask, 0);
3583 /* Handle "-generate" or a hex string as the last argument. */
3584 if (!strcmp(argv[argc - 1], "-generate")) {
3585 packet = ofpbuf_new(0);
3588 error = eth_from_hex(argv[argc - 1], &packet);
3591 } else if (argc == 4) {
3592 /* The 3-argument form must end in "-generate' or a hex string. */
3598 /* odp_flow can have its in_port specified as a name instead of port no.
3599 * We do not yet know whether a given flow is a odp_flow or a br_flow.
3600 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3601 * we need to create a simap of name to port no. */
3603 const char *dp_type;
3604 if (!strncmp(argv[1], "ovs-", 4)) {
3605 dp_type = argv[1] + 4;
3609 backer = shash_find_data(&all_dpif_backers, dp_type);
3610 } else if (argc == 2) {
3611 struct shash_node *node;
3612 if (shash_count(&all_dpif_backers) == 1) {
3613 node = shash_first(&all_dpif_backers);
3614 backer = node->data;
3617 error = "Syntax error";
3620 if (backer && backer->dpif) {
3621 struct dpif_port dpif_port;
3622 struct dpif_port_dump port_dump;
3623 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
3624 simap_put(&port_names, dpif_port.name,
3625 odp_to_u32(dpif_port.port_no));
3629 /* Parse the flow and determine whether a datapath or
3630 * bridge is specified. If function odp_flow_key_from_string()
3631 * returns 0, the flow is a odp_flow. If function
3632 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
3633 if (!odp_flow_from_string(argv[argc - 1], &port_names,
3634 &odp_key, &odp_mask)) {
3636 error = "Cannot find the datapath";
3640 if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, flow,
3641 NULL, ofprotop, NULL, NULL, NULL, NULL)) {
3642 error = "Invalid datapath flow";
3646 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
3649 m_err = xasprintf("Bad flow syntax: %s", err);
3654 error = "Must specify bridge name";
3658 *ofprotop = ofproto_dpif_lookup(argv[1]);
3660 error = "Unknown bridge name";
3666 /* Generate a packet, if requested. */
3668 if (!packet->size) {
3669 flow_compose(packet, flow);
3671 union flow_in_port in_port = flow->in_port;
3673 /* Use the metadata from the flow and the packet argument
3674 * to reconstruct the flow. */
3675 flow_extract(packet, flow->skb_priority, flow->pkt_mark, NULL,
3681 if (error && !m_err) {
3682 m_err = xstrdup(error);
3685 ofpbuf_delete(packet);
3689 ofpbuf_uninit(&odp_key);
3690 ofpbuf_uninit(&odp_mask);
3691 simap_destroy(&port_names);
3696 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
3697 void *aux OVS_UNUSED)
3699 struct ofproto_dpif *ofproto;
3700 struct ofpbuf *packet;
3704 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3709 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
3710 unixctl_command_reply(conn, ds_cstr(&result));
3711 ds_destroy(&result);
3712 ofpbuf_delete(packet);
3714 unixctl_command_reply_error(conn, error);
3720 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
3721 const char *argv[], void *aux OVS_UNUSED)
3723 enum ofputil_protocol usable_protocols;
3724 struct ofproto_dpif *ofproto;
3725 bool enforce_consistency;
3726 struct ofpbuf ofpacts;
3727 struct ofpbuf *packet;
3732 /* Three kinds of error return values! */
3738 ofpbuf_init(&ofpacts, 0);
3740 /* Parse actions. */
3741 error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
3743 unixctl_command_reply_error(conn, error);
3748 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
3749 * consistency between the flow and the actions. With -consistent, we
3750 * enforce consistency even for a flow supported in OpenFlow 1.0. */
3751 if (!strcmp(argv[1], "-consistent")) {
3752 enforce_consistency = true;
3756 enforce_consistency = false;
3759 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
3761 unixctl_command_reply_error(conn, error);
3766 /* Do the same checks as handle_packet_out() in ofproto.c.
3768 * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
3769 * strictly correct because these actions aren't in any table, but it's OK
3770 * because it 'table_id' is used only to check goto_table instructions, but
3771 * packet-outs take a list of actions and therefore it can't include
3774 * We skip the "meter" check here because meter is an instruction, not an
3775 * action, and thus cannot appear in ofpacts. */
3776 in_port = ofp_to_u16(flow.in_port.ofp_port);
3777 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
3778 unixctl_command_reply_error(conn, "invalid in_port");
3781 if (enforce_consistency) {
3782 retval = ofpacts_check_consistency(ofpacts.data, ofpacts.size, &flow,
3783 u16_to_ofp(ofproto->up.max_ports),
3784 0, 0, usable_protocols);
3786 retval = ofpacts_check(ofpacts.data, ofpacts.size, &flow,
3787 u16_to_ofp(ofproto->up.max_ports), 0, 0,
3793 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
3794 unixctl_command_reply_error(conn, ds_cstr(&result));
3798 ofproto_trace(ofproto, &flow, packet, ofpacts.data, ofpacts.size, &result);
3799 unixctl_command_reply(conn, ds_cstr(&result));
3802 ds_destroy(&result);
3803 ofpbuf_delete(packet);
3804 ofpbuf_uninit(&ofpacts);
3807 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
3808 * description of the results to 'ds'.
3810 * The trace follows a packet with the specified 'flow' through the flow
3811 * table. 'packet' may be nonnull to trace an actual packet, with consequent
3812 * side effects (if it is nonnull then its flow must be 'flow').
3814 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
3815 * trace, otherwise the actions are determined by a flow table lookup. */
3817 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
3818 const struct ofpbuf *packet,
3819 const struct ofpact ofpacts[], size_t ofpacts_len,
3822 struct rule_dpif *rule;
3823 struct flow_wildcards wc;
3825 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
3826 ds_put_cstr(ds, "Flow: ");
3827 flow_format(ds, flow);
3828 ds_put_char(ds, '\n');
3830 flow_wildcards_init_catchall(&wc);
3834 rule_dpif_lookup(ofproto, flow, &wc, &rule);
3836 trace_format_rule(ds, 0, rule);
3837 if (rule == ofproto->miss_rule) {
3838 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
3839 } else if (rule == ofproto->no_packet_in_rule) {
3840 ds_put_cstr(ds, "\nNo match, packets dropped because "
3841 "OFPPC_NO_PACKET_IN is set on in_port.\n");
3842 } else if (rule == ofproto->drop_frags_rule) {
3843 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
3844 "and the fragment handling mode is \"drop\".\n");
3848 if (rule || ofpacts) {
3849 uint64_t odp_actions_stub[1024 / 8];
3850 struct ofpbuf odp_actions;
3851 struct trace_ctx trace;
3855 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
3858 ofpbuf_use_stub(&odp_actions,
3859 odp_actions_stub, sizeof odp_actions_stub);
3860 xlate_in_init(&trace.xin, ofproto, flow, rule, tcp_flags, packet);
3862 trace.xin.ofpacts = ofpacts;
3863 trace.xin.ofpacts_len = ofpacts_len;
3865 trace.xin.resubmit_hook = trace_resubmit;
3866 trace.xin.report_hook = trace_report;
3868 xlate_actions(&trace.xin, &trace.xout);
3869 flow_wildcards_or(&trace.xout.wc, &trace.xout.wc, &wc);
3871 ds_put_char(ds, '\n');
3872 trace_format_flow(ds, 0, "Final flow", &trace);
3874 match_init(&match, flow, &trace.xout.wc);
3875 ds_put_cstr(ds, "Relevant fields: ");
3876 match_format(&match, ds, OFP_DEFAULT_PRIORITY);
3877 ds_put_char(ds, '\n');
3879 ds_put_cstr(ds, "Datapath actions: ");
3880 format_odp_actions(ds, trace.xout.odp_actions.data,
3881 trace.xout.odp_actions.size);
3883 if (trace.xout.slow) {
3884 enum slow_path_reason slow;
3886 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
3887 "slow path because it:");
3889 slow = trace.xout.slow;
3891 enum slow_path_reason bit = rightmost_1bit(slow);
3893 ds_put_format(ds, "\n\t- %s.",
3894 slow_path_reason_to_explanation(bit));
3900 xlate_out_uninit(&trace.xout);
3903 rule_dpif_unref(rule);
3906 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
3907 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
3908 * to destroy 'ofproto_shash' and free the returned value. */
3909 static const struct shash_node **
3910 get_ofprotos(struct shash *ofproto_shash)
3912 const struct ofproto_dpif *ofproto;
3914 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3915 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
3916 shash_add_nocopy(ofproto_shash, name, ofproto);
3919 return shash_sort(ofproto_shash);
3923 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
3924 const char *argv[] OVS_UNUSED,
3925 void *aux OVS_UNUSED)
3927 struct ds ds = DS_EMPTY_INITIALIZER;
3928 struct shash ofproto_shash;
3929 const struct shash_node **sorted_ofprotos;
3932 shash_init(&ofproto_shash);
3933 sorted_ofprotos = get_ofprotos(&ofproto_shash);
3934 for (i = 0; i < shash_count(&ofproto_shash); i++) {
3935 const struct shash_node *node = sorted_ofprotos[i];
3936 ds_put_format(&ds, "%s\n", node->name);
3939 shash_destroy(&ofproto_shash);
3940 free(sorted_ofprotos);
3942 unixctl_command_reply(conn, ds_cstr(&ds));
3947 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
3949 const struct shash_node **ofprotos;
3950 struct dpif_dp_stats dp_stats;
3951 struct shash ofproto_shash;
3954 dpif_get_dp_stats(backer->dpif, &dp_stats);
3956 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
3957 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
3959 shash_init(&ofproto_shash);
3960 ofprotos = get_ofprotos(&ofproto_shash);
3961 for (i = 0; i < shash_count(&ofproto_shash); i++) {
3962 struct ofproto_dpif *ofproto = ofprotos[i]->data;
3963 const struct shash_node **ports;
3966 if (ofproto->backer != backer) {
3970 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
3972 ports = shash_sort(&ofproto->up.port_by_name);
3973 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
3974 const struct shash_node *node = ports[j];
3975 struct ofport *ofport = node->data;
3977 odp_port_t odp_port;
3979 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
3982 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
3983 if (odp_port != ODPP_NONE) {
3984 ds_put_format(ds, "%"PRIu32":", odp_port);
3986 ds_put_cstr(ds, "none:");
3989 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
3992 if (!netdev_get_config(ofport->netdev, &config)) {
3993 const struct smap_node **nodes;
3996 nodes = smap_sort(&config);
3997 for (i = 0; i < smap_count(&config); i++) {
3998 const struct smap_node *node = nodes[i];
3999 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4000 node->key, node->value);
4004 smap_destroy(&config);
4006 ds_put_char(ds, ')');
4007 ds_put_char(ds, '\n');
4011 shash_destroy(&ofproto_shash);
4016 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4017 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4019 struct ds ds = DS_EMPTY_INITIALIZER;
4020 const struct shash_node **backers;
4023 backers = shash_sort(&all_dpif_backers);
4024 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4025 dpif_show_backer(backers[i]->data, &ds);
4029 unixctl_command_reply(conn, ds_cstr(&ds));
4034 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4035 const struct nlattr *key, size_t key_len)
4037 enum odp_key_fitness fitness;
4038 struct ofproto_dpif *ofp;
4041 xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &fitness, &ofp,
4042 NULL, NULL, NULL, NULL);
4043 return ofp == ofproto;
4047 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4048 int argc OVS_UNUSED, const char *argv[],
4049 void *aux OVS_UNUSED)
4051 struct ds ds = DS_EMPTY_INITIALIZER;
4052 const struct dpif_flow_stats *stats;
4053 const struct ofproto_dpif *ofproto;
4054 struct dpif_flow_dump flow_dump;
4055 const struct nlattr *actions;
4056 const struct nlattr *mask;
4057 const struct nlattr *key;
4061 bool verbosity = false;
4062 struct dpif_port dpif_port;
4063 struct dpif_port_dump port_dump;
4064 struct hmap portno_names;
4066 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4068 unixctl_command_reply_error(conn, "no such bridge");
4072 if (argc > 2 && !strcmp(argv[1], "-m")) {
4076 hmap_init(&portno_names);
4077 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4078 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4082 dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4083 while (dpif_flow_dump_next(&flow_dump, &key, &key_len, &mask, &mask_len,
4084 &actions, &actions_len, &stats)) {
4085 if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
4089 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4091 ds_put_cstr(&ds, ", ");
4092 dpif_flow_stats_format(stats, &ds);
4093 ds_put_cstr(&ds, ", actions:");
4094 format_odp_actions(&ds, actions, actions_len);
4095 ds_put_char(&ds, '\n');
4098 if (dpif_flow_dump_done(&flow_dump)) {
4100 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4101 unixctl_command_reply_error(conn, ds_cstr(&ds));
4103 unixctl_command_reply(conn, ds_cstr(&ds));
4105 odp_portno_names_destroy(&portno_names);
4106 hmap_destroy(&portno_names);
4111 ofproto_dpif_unixctl_init(void)
4113 static bool registered;
4119 unixctl_command_register(
4121 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4122 1, 3, ofproto_unixctl_trace, NULL);
4123 unixctl_command_register(
4124 "ofproto/trace-packet-out",
4125 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4126 2, 6, ofproto_unixctl_trace_actions, NULL);
4127 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4128 ofproto_unixctl_fdb_flush, NULL);
4129 unixctl_command_register("fdb/show", "bridge", 1, 1,
4130 ofproto_unixctl_fdb_show, NULL);
4131 unixctl_command_register("dpif/dump-dps", "", 0, 0,
4132 ofproto_unixctl_dpif_dump_dps, NULL);
4133 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4135 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4136 ofproto_unixctl_dpif_dump_flows, NULL);
4139 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4141 * This is deprecated. It is only for compatibility with broken device drivers
4142 * in old versions of Linux that do not properly support VLANs when VLAN
4143 * devices are not used. When broken device drivers are no longer in
4144 * widespread use, we will delete these interfaces. */
4147 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4149 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4150 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4152 if (realdev_ofp_port == ofport->realdev_ofp_port
4153 && vid == ofport->vlandev_vid) {
4157 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4159 if (ofport->realdev_ofp_port) {
4162 if (realdev_ofp_port && ofport->bundle) {
4163 /* vlandevs are enslaved to their realdevs, so they are not allowed to
4164 * themselves be part of a bundle. */
4165 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
4168 ofport->realdev_ofp_port = realdev_ofp_port;
4169 ofport->vlandev_vid = vid;
4171 if (realdev_ofp_port) {
4172 vsp_add(ofport, realdev_ofp_port, vid);
4179 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4181 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4185 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4186 OVS_EXCLUDED(ofproto->vsp_mutex)
4190 ovs_mutex_lock(&ofproto->vsp_mutex);
4191 ret = !hmap_is_empty(&ofproto->realdev_vid_map);
4192 ovs_mutex_unlock(&ofproto->vsp_mutex);
4197 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4198 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4199 OVS_REQUIRES(ofproto->vsp_mutex)
4201 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4202 int vid = vlan_tci_to_vid(vlan_tci);
4203 const struct vlan_splinter *vsp;
4205 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4206 hash_realdev_vid(realdev_ofp_port, vid),
4207 &ofproto->realdev_vid_map) {
4208 if (vsp->realdev_ofp_port == realdev_ofp_port
4209 && vsp->vid == vid) {
4210 return vsp->vlandev_ofp_port;
4214 return realdev_ofp_port;
4217 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4218 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4219 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
4220 * 'vlan_tci' 9, it would return the port number of eth0.9.
4222 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4223 * function just returns its 'realdev_ofp_port' argument. */
4225 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4226 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4227 OVS_EXCLUDED(ofproto->vsp_mutex)
4231 ovs_mutex_lock(&ofproto->vsp_mutex);
4232 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4233 ovs_mutex_unlock(&ofproto->vsp_mutex);
4237 static struct vlan_splinter *
4238 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4240 struct vlan_splinter *vsp;
4242 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4243 hash_ofp_port(vlandev_ofp_port),
4244 &ofproto->vlandev_map) {
4245 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4253 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4254 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4255 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
4256 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4257 * eth0 and store 9 in '*vid'.
4259 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4260 * VLAN device. Unless VLAN splinters are enabled, this is what this function
4263 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4264 ofp_port_t vlandev_ofp_port, int *vid)
4265 OVS_REQUIRES(ofproto->vsp_mutex)
4267 if (!hmap_is_empty(&ofproto->vlandev_map)) {
4268 const struct vlan_splinter *vsp;
4270 vsp = vlandev_find(ofproto, vlandev_ofp_port);
4275 return vsp->realdev_ofp_port;
4281 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4282 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
4283 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4284 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
4285 * always the case unless VLAN splinters are enabled), returns false without
4286 * making any changes. */
4288 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4289 OVS_EXCLUDED(ofproto->vsp_mutex)
4294 ovs_mutex_lock(&ofproto->vsp_mutex);
4295 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4296 ovs_mutex_unlock(&ofproto->vsp_mutex);
4301 /* Cause the flow to be processed as if it came in on the real device with
4302 * the VLAN device's VLAN ID. */
4303 flow->in_port.ofp_port = realdev;
4304 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4309 vsp_remove(struct ofport_dpif *port)
4311 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4312 struct vlan_splinter *vsp;
4314 ovs_mutex_lock(&ofproto->vsp_mutex);
4315 vsp = vlandev_find(ofproto, port->up.ofp_port);
4317 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4318 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4321 port->realdev_ofp_port = 0;
4323 VLOG_ERR("missing vlan device record");
4325 ovs_mutex_unlock(&ofproto->vsp_mutex);
4329 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4331 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4333 ovs_mutex_lock(&ofproto->vsp_mutex);
4334 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4335 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4336 == realdev_ofp_port)) {
4337 struct vlan_splinter *vsp;
4339 vsp = xmalloc(sizeof *vsp);
4340 vsp->realdev_ofp_port = realdev_ofp_port;
4341 vsp->vlandev_ofp_port = port->up.ofp_port;
4344 port->realdev_ofp_port = realdev_ofp_port;
4346 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4347 hash_ofp_port(port->up.ofp_port));
4348 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4349 hash_realdev_vid(realdev_ofp_port, vid));
4351 VLOG_ERR("duplicate vlan device record");
4353 ovs_mutex_unlock(&ofproto->vsp_mutex);
4357 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4359 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4360 return ofport ? ofport->odp_port : ODPP_NONE;
4363 struct ofport_dpif *
4364 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4366 struct ofport_dpif *port;
4368 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4369 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4370 &backer->odp_to_ofport_map) {
4371 if (port->odp_port == odp_port) {
4372 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4377 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4382 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4384 struct ofport_dpif *port;
4386 port = odp_port_to_ofport(ofproto->backer, odp_port);
4387 if (port && &ofproto->up == port->up.ofproto) {
4388 return port->up.ofp_port;
4394 const struct ofproto_class ofproto_dpif_class = {
4408 NULL, /* get_memory_usage. */
4409 type_get_memory_usage,
4428 port_is_lacp_current,
4429 NULL, /* rule_choose_table */
4438 rule_modify_actions,
4452 get_stp_port_status,
4460 is_mirror_output_bundle,
4461 forward_bpdu_changed,
4462 set_mac_table_config,
4464 NULL, /* meter_get_features */
4465 NULL, /* meter_set */
4466 NULL, /* meter_get */
4467 NULL, /* meter_del */
4468 group_alloc, /* group_alloc */
4469 group_construct, /* group_construct */
4470 group_destruct, /* group_destruct */
4471 group_dealloc, /* group_dealloc */
4472 group_modify, /* group_modify */
4473 group_get_stats, /* group_get_stats */