2 * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-dpif.h"
20 #include "ofproto/ofproto-provider.h"
27 #include "byte-order.h"
28 #include "connectivity.h"
33 #include "dynamic-string.h"
34 #include "fail-open.h"
35 #include "guarded-list.h"
39 #include "mac-learning.h"
40 #include "meta-flow.h"
41 #include "multipath.h"
42 #include "netdev-vport.h"
47 #include "odp-execute.h"
50 #include "ofp-actions.h"
51 #include "ofp-parse.h"
52 #include "ofp-print.h"
53 #include "ofproto-dpif-ipfix.h"
54 #include "ofproto-dpif-mirror.h"
55 #include "ofproto-dpif-monitor.h"
56 #include "ofproto-dpif-rid.h"
57 #include "ofproto-dpif-sflow.h"
58 #include "ofproto-dpif-upcall.h"
59 #include "ofproto-dpif-xlate.h"
60 #include "poll-loop.h"
66 #include "unaligned.h"
68 #include "vlan-bitmap.h"
71 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
73 COVERAGE_DEFINE(ofproto_dpif_expired);
74 COVERAGE_DEFINE(packet_in_overflow);
76 /* Number of implemented OpenFlow tables. */
77 enum { N_TABLES = 255 };
78 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
79 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
88 * - Do include packets and bytes from datapath flows which have not
89 * recently been processed by a revalidator. */
90 struct ovs_mutex stats_mutex;
91 struct dpif_flow_stats stats OVS_GUARDED;
94 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
96 static struct rule_dpif *rule_dpif_cast(const struct rule *);
97 static void rule_expire(struct rule_dpif *);
104 * - Do include packets and bytes from datapath flows which have not
105 * recently been processed by a revalidator. */
106 struct ovs_mutex stats_mutex;
107 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
108 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
109 struct bucket_counter *bucket_stats OVS_GUARDED; /* Bucket statistics. */
113 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
114 struct ofproto_dpif *ofproto; /* Owning ofproto. */
115 void *aux; /* Key supplied by ofproto's client. */
116 char *name; /* Identifier for log messages. */
119 struct list ports; /* Contains "struct ofport"s. */
120 enum port_vlan_mode vlan_mode; /* VLAN mode */
121 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
122 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
123 * NULL if all VLANs are trunked. */
124 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
125 struct bond *bond; /* Nonnull iff more than one port. */
126 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
129 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
132 static void bundle_remove(struct ofport *);
133 static void bundle_update(struct ofbundle *);
134 static void bundle_destroy(struct ofbundle *);
135 static void bundle_del_port(struct ofport_dpif *);
136 static void bundle_run(struct ofbundle *);
137 static void bundle_wait(struct ofbundle *);
139 static void stp_run(struct ofproto_dpif *ofproto);
140 static void stp_wait(struct ofproto_dpif *ofproto);
141 static int set_stp_port(struct ofport *,
142 const struct ofproto_port_stp_settings *);
145 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
149 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
150 struct list bundle_node; /* In struct ofbundle's "ports" list. */
151 struct cfm *cfm; /* Connectivity Fault Management, if any. */
152 struct bfd *bfd; /* BFD, if any. */
153 bool may_enable; /* May be enabled in bonds. */
154 bool is_tunnel; /* This port is a tunnel. */
155 bool is_layer3; /* This is a layer 3 port. */
156 long long int carrier_seq; /* Carrier status changes. */
157 struct ofport_dpif *peer; /* Peer if patch port. */
160 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
161 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
162 long long int stp_state_entered;
164 /* Queue to DSCP mapping. */
165 struct ofproto_port_queue *qdscp;
168 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
170 * This is deprecated. It is only for compatibility with broken device
171 * drivers in old versions of Linux that do not properly support VLANs when
172 * VLAN devices are not used. When broken device drivers are no longer in
173 * widespread use, we will delete these interfaces. */
174 ofp_port_t realdev_ofp_port;
178 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
180 * This is deprecated. It is only for compatibility with broken device drivers
181 * in old versions of Linux that do not properly support VLANs when VLAN
182 * devices are not used. When broken device drivers are no longer in
183 * widespread use, we will delete these interfaces. */
184 struct vlan_splinter {
185 struct hmap_node realdev_vid_node;
186 struct hmap_node vlandev_node;
187 ofp_port_t realdev_ofp_port;
188 ofp_port_t vlandev_ofp_port;
192 static void vsp_remove(struct ofport_dpif *);
193 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
195 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
198 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
201 static struct ofport_dpif *
202 ofport_dpif_cast(const struct ofport *ofport)
204 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
207 static void port_run(struct ofport_dpif *);
208 static int set_bfd(struct ofport *, const struct smap *);
209 static int set_cfm(struct ofport *, const struct cfm_settings *);
210 static void ofport_update_peer(struct ofport_dpif *);
212 struct dpif_completion {
213 struct list list_node;
214 struct ofoperation *op;
217 /* Reasons that we might need to revalidate every datapath flow, and
218 * corresponding coverage counters.
220 * A value of 0 means that there is no need to revalidate.
222 * It would be nice to have some cleaner way to integrate with coverage
223 * counters, but with only a few reasons I guess this is good enough for
225 enum revalidate_reason {
226 REV_RECONFIGURE = 1, /* Switch configuration changed. */
227 REV_STP, /* Spanning tree protocol port status change. */
228 REV_BOND, /* Bonding changed. */
229 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
230 REV_FLOW_TABLE, /* Flow table changed. */
231 REV_MAC_LEARNING, /* Mac learning changed. */
233 COVERAGE_DEFINE(rev_reconfigure);
234 COVERAGE_DEFINE(rev_stp);
235 COVERAGE_DEFINE(rev_bond);
236 COVERAGE_DEFINE(rev_port_toggled);
237 COVERAGE_DEFINE(rev_flow_table);
238 COVERAGE_DEFINE(rev_mac_learning);
240 /* All datapaths of a given type share a single dpif backer instance. */
247 struct ovs_rwlock odp_to_ofport_lock;
248 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
250 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
252 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
254 bool recv_set_enable; /* Enables or disables receiving packets. */
257 struct recirc_id_pool *rid_pool; /* Recirculation ID pool. */
258 bool enable_recirc; /* True if the datapath supports recirculation */
260 /* True if the datapath supports variable-length
261 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
262 * False if the datapath supports only 8-byte (or shorter) userdata. */
263 bool variable_length_userdata;
265 /* Maximum number of MPLS label stack entries that the datapath supports
267 size_t max_mpls_depth;
270 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
271 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
273 struct ofproto_dpif {
274 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
276 struct dpif_backer *backer;
278 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
280 /* Special OpenFlow rules. */
281 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
282 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
283 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
286 struct netflow *netflow;
287 struct dpif_sflow *sflow;
288 struct dpif_ipfix *ipfix;
289 struct hmap bundles; /* Contains "struct ofbundle"s. */
290 struct mac_learning *ml;
291 bool has_bonded_bundles;
293 struct mbridge *mbridge;
295 struct ovs_mutex stats_mutex;
296 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
297 * consumed in userspace. */
301 long long int stp_last_tick;
303 /* VLAN splinters. */
304 struct ovs_mutex vsp_mutex;
305 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
306 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
309 struct sset ports; /* Set of standard port names. */
310 struct sset ghost_ports; /* Ports with no datapath port. */
311 struct sset port_poll_set; /* Queued names for port_poll() reply. */
312 int port_poll_errno; /* Last errno for port_poll() reply. */
313 uint64_t change_seq; /* Connectivity status changes. */
316 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
319 /* All existing ofproto_dpif instances, indexed by ->up.name. */
320 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
322 static void ofproto_dpif_unixctl_init(void);
324 static inline struct ofproto_dpif *
325 ofproto_dpif_cast(const struct ofproto *ofproto)
327 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
328 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
332 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
334 return ofproto->backer->max_mpls_depth;
338 ofproto_dpif_get_enable_recirc(const struct ofproto_dpif *ofproto)
340 return ofproto->backer->enable_recirc;
343 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
344 ofp_port_t ofp_port);
345 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
346 const struct ofpbuf *packet,
347 const struct ofpact[], size_t ofpacts_len,
350 /* Global variables. */
351 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
353 /* Initial mappings of port to bridge mappings. */
354 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
356 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
359 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
360 struct ofputil_flow_mod *fm)
362 ofproto_flow_mod(&ofproto->up, fm);
365 /* Resets the modified time for 'rule' or an equivalent rule. If 'rule' is not
366 * in the classifier, but an equivalent rule is, unref 'rule' and ref the new
367 * rule. Otherwise if 'rule' is no longer installed in the classifier,
370 * Returns the rule whose modified time has been reset. */
372 ofproto_dpif_refresh_rule(struct rule_dpif *rule)
374 return rule_dpif_cast(ofproto_refresh_rule(&rule->up));
377 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
378 * Takes ownership of 'pin' and pin->packet. */
380 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
381 struct ofproto_packet_in *pin)
383 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
384 COVERAGE_INC(packet_in_overflow);
385 free(CONST_CAST(void *, pin->up.packet));
390 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
391 * packet rather than to send the packet to the controller.
393 * This function returns false to indicate that a packet_in message
394 * for a "table-miss" should be sent to at least one controller.
395 * False otherwise. */
397 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
399 return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
402 /* Factory functions. */
405 init(const struct shash *iface_hints)
407 struct shash_node *node;
409 /* Make a local copy, since we don't own 'iface_hints' elements. */
410 SHASH_FOR_EACH(node, iface_hints) {
411 const struct iface_hint *orig_hint = node->data;
412 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
414 new_hint->br_name = xstrdup(orig_hint->br_name);
415 new_hint->br_type = xstrdup(orig_hint->br_type);
416 new_hint->ofp_port = orig_hint->ofp_port;
418 shash_add(&init_ofp_ports, node->name, new_hint);
423 enumerate_types(struct sset *types)
425 dp_enumerate_types(types);
429 enumerate_names(const char *type, struct sset *names)
431 struct ofproto_dpif *ofproto;
434 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
435 if (strcmp(type, ofproto->up.type)) {
438 sset_add(names, ofproto->up.name);
445 del(const char *type, const char *name)
450 error = dpif_open(name, type, &dpif);
452 error = dpif_delete(dpif);
459 port_open_type(const char *datapath_type, const char *port_type)
461 return dpif_port_open_type(datapath_type, port_type);
464 /* Type functions. */
466 static void process_dpif_port_changes(struct dpif_backer *);
467 static void process_dpif_all_ports_changed(struct dpif_backer *);
468 static void process_dpif_port_change(struct dpif_backer *,
469 const char *devname);
470 static void process_dpif_port_error(struct dpif_backer *, int error);
472 static struct ofproto_dpif *
473 lookup_ofproto_dpif_by_port_name(const char *name)
475 struct ofproto_dpif *ofproto;
477 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
478 if (sset_contains(&ofproto->ports, name)) {
487 type_run(const char *type)
489 struct dpif_backer *backer;
491 backer = shash_find_data(&all_dpif_backers, type);
493 /* This is not necessarily a problem, since backers are only
494 * created on demand. */
498 dpif_run(backer->dpif);
500 /* If vswitchd started with other_config:flow_restore_wait set as "true",
501 * and the configuration has now changed to "false", enable receiving
502 * packets from the datapath. */
503 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
506 backer->recv_set_enable = true;
508 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
510 VLOG_ERR("Failed to enable receiving packets in dpif.");
513 dpif_flow_flush(backer->dpif);
514 backer->need_revalidate = REV_RECONFIGURE;
517 if (backer->recv_set_enable) {
518 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
521 if (backer->need_revalidate) {
522 struct ofproto_dpif *ofproto;
523 struct simap_node *node;
524 struct simap tmp_backers;
526 /* Handle tunnel garbage collection. */
527 simap_init(&tmp_backers);
528 simap_swap(&backer->tnl_backers, &tmp_backers);
530 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
531 struct ofport_dpif *iter;
533 if (backer != ofproto->backer) {
537 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
538 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
541 if (!iter->is_tunnel) {
545 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
546 namebuf, sizeof namebuf);
547 node = simap_find(&tmp_backers, dp_port);
549 simap_put(&backer->tnl_backers, dp_port, node->data);
550 simap_delete(&tmp_backers, node);
551 node = simap_find(&backer->tnl_backers, dp_port);
553 node = simap_find(&backer->tnl_backers, dp_port);
555 odp_port_t odp_port = ODPP_NONE;
557 if (!dpif_port_add(backer->dpif, iter->up.netdev,
559 simap_put(&backer->tnl_backers, dp_port,
560 odp_to_u32(odp_port));
561 node = simap_find(&backer->tnl_backers, dp_port);
566 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
567 if (tnl_port_reconfigure(iter, iter->up.netdev,
569 backer->need_revalidate = REV_RECONFIGURE;
574 SIMAP_FOR_EACH (node, &tmp_backers) {
575 dpif_port_del(backer->dpif, u32_to_odp(node->data));
577 simap_destroy(&tmp_backers);
579 switch (backer->need_revalidate) {
580 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
581 case REV_STP: COVERAGE_INC(rev_stp); break;
582 case REV_BOND: COVERAGE_INC(rev_bond); break;
583 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
584 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
585 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
587 backer->need_revalidate = 0;
589 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
590 struct ofport_dpif *ofport;
591 struct ofbundle *bundle;
593 if (ofproto->backer != backer) {
597 ovs_rwlock_wrlock(&xlate_rwlock);
598 xlate_ofproto_set(ofproto, ofproto->up.name,
599 ofproto->backer->dpif, ofproto->miss_rule,
600 ofproto->no_packet_in_rule, ofproto->ml,
601 ofproto->stp, ofproto->mbridge,
602 ofproto->sflow, ofproto->ipfix,
603 ofproto->netflow, ofproto->up.frag_handling,
604 ofproto->up.forward_bpdu,
605 connmgr_has_in_band(ofproto->up.connmgr),
606 ofproto->backer->enable_recirc,
607 ofproto->backer->variable_length_userdata,
608 ofproto->backer->max_mpls_depth);
610 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
611 xlate_bundle_set(ofproto, bundle, bundle->name,
612 bundle->vlan_mode, bundle->vlan,
613 bundle->trunks, bundle->use_priority_tags,
614 bundle->bond, bundle->lacp,
618 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
619 int stp_port = ofport->stp_port
620 ? stp_port_no(ofport->stp_port)
622 xlate_ofport_set(ofproto, ofport->bundle, ofport,
623 ofport->up.ofp_port, ofport->odp_port,
624 ofport->up.netdev, ofport->cfm,
625 ofport->bfd, ofport->peer, stp_port,
626 ofport->qdscp, ofport->n_qdscp,
627 ofport->up.pp.config, ofport->up.pp.state,
628 ofport->is_tunnel, ofport->may_enable);
630 ovs_rwlock_unlock(&xlate_rwlock);
633 udpif_revalidate(backer->udpif);
636 process_dpif_port_changes(backer);
641 /* Check for and handle port changes in 'backer''s dpif. */
643 process_dpif_port_changes(struct dpif_backer *backer)
649 error = dpif_port_poll(backer->dpif, &devname);
655 process_dpif_all_ports_changed(backer);
659 process_dpif_port_change(backer, devname);
664 process_dpif_port_error(backer, error);
671 process_dpif_all_ports_changed(struct dpif_backer *backer)
673 struct ofproto_dpif *ofproto;
674 struct dpif_port dpif_port;
675 struct dpif_port_dump dump;
676 struct sset devnames;
679 sset_init(&devnames);
680 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
681 if (ofproto->backer == backer) {
682 struct ofport *ofport;
684 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
685 sset_add(&devnames, netdev_get_name(ofport->netdev));
689 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
690 sset_add(&devnames, dpif_port.name);
693 SSET_FOR_EACH (devname, &devnames) {
694 process_dpif_port_change(backer, devname);
696 sset_destroy(&devnames);
700 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
702 struct ofproto_dpif *ofproto;
703 struct dpif_port port;
705 /* Don't report on the datapath's device. */
706 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
710 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
711 &all_ofproto_dpifs) {
712 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
717 ofproto = lookup_ofproto_dpif_by_port_name(devname);
718 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
719 /* The port was removed. If we know the datapath,
720 * report it through poll_set(). If we don't, it may be
721 * notifying us of a removal we initiated, so ignore it.
722 * If there's a pending ENOBUFS, let it stand, since
723 * everything will be reevaluated. */
724 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
725 sset_add(&ofproto->port_poll_set, devname);
726 ofproto->port_poll_errno = 0;
728 } else if (!ofproto) {
729 /* The port was added, but we don't know with which
730 * ofproto we should associate it. Delete it. */
731 dpif_port_del(backer->dpif, port.port_no);
733 struct ofport_dpif *ofport;
735 ofport = ofport_dpif_cast(shash_find_data(
736 &ofproto->up.port_by_name, devname));
738 && ofport->odp_port != port.port_no
739 && !odp_port_to_ofport(backer, port.port_no))
741 /* 'ofport''s datapath port number has changed from
742 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
743 * structures to match. */
744 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
745 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
746 ofport->odp_port = port.port_no;
747 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
748 hash_odp_port(port.port_no));
749 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
750 backer->need_revalidate = REV_RECONFIGURE;
753 dpif_port_destroy(&port);
756 /* Propagate 'error' to all ofprotos based on 'backer'. */
758 process_dpif_port_error(struct dpif_backer *backer, int error)
760 struct ofproto_dpif *ofproto;
762 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
763 if (ofproto->backer == backer) {
764 sset_clear(&ofproto->port_poll_set);
765 ofproto->port_poll_errno = error;
771 type_wait(const char *type)
773 struct dpif_backer *backer;
775 backer = shash_find_data(&all_dpif_backers, type);
777 /* This is not necessarily a problem, since backers are only
778 * created on demand. */
782 dpif_wait(backer->dpif);
785 /* Basic life-cycle. */
787 static int add_internal_flows(struct ofproto_dpif *);
789 static struct ofproto *
792 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
797 dealloc(struct ofproto *ofproto_)
799 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
804 close_dpif_backer(struct dpif_backer *backer)
806 ovs_assert(backer->refcount > 0);
808 if (--backer->refcount) {
812 udpif_destroy(backer->udpif);
814 simap_destroy(&backer->tnl_backers);
815 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
816 hmap_destroy(&backer->odp_to_ofport_map);
817 shash_find_and_delete(&all_dpif_backers, backer->type);
818 recirc_id_pool_destroy(backer->rid_pool);
820 dpif_close(backer->dpif);
824 /* Datapath port slated for removal from datapath. */
826 struct list list_node;
830 static bool check_variable_length_userdata(struct dpif_backer *backer);
831 static size_t check_max_mpls_depth(struct dpif_backer *backer);
832 static bool check_recirc(struct dpif_backer *backer);
835 open_dpif_backer(const char *type, struct dpif_backer **backerp)
837 struct dpif_backer *backer;
838 struct dpif_port_dump port_dump;
839 struct dpif_port port;
840 struct shash_node *node;
841 struct list garbage_list;
842 struct odp_garbage *garbage, *next;
849 backer = shash_find_data(&all_dpif_backers, type);
856 backer_name = xasprintf("ovs-%s", type);
858 /* Remove any existing datapaths, since we assume we're the only
859 * userspace controlling the datapath. */
861 dp_enumerate_names(type, &names);
862 SSET_FOR_EACH(name, &names) {
863 struct dpif *old_dpif;
865 /* Don't remove our backer if it exists. */
866 if (!strcmp(name, backer_name)) {
870 if (dpif_open(name, type, &old_dpif)) {
871 VLOG_WARN("couldn't open old datapath %s to remove it", name);
873 dpif_delete(old_dpif);
874 dpif_close(old_dpif);
877 sset_destroy(&names);
879 backer = xmalloc(sizeof *backer);
881 error = dpif_create_and_open(backer_name, type, &backer->dpif);
884 VLOG_ERR("failed to open datapath of type %s: %s", type,
885 ovs_strerror(error));
889 backer->udpif = udpif_create(backer, backer->dpif);
891 backer->type = xstrdup(type);
892 backer->refcount = 1;
893 hmap_init(&backer->odp_to_ofport_map);
894 ovs_rwlock_init(&backer->odp_to_ofport_lock);
895 backer->need_revalidate = 0;
896 simap_init(&backer->tnl_backers);
897 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
900 if (backer->recv_set_enable) {
901 dpif_flow_flush(backer->dpif);
904 /* Loop through the ports already on the datapath and remove any
905 * that we don't need anymore. */
906 list_init(&garbage_list);
907 dpif_port_dump_start(&port_dump, backer->dpif);
908 while (dpif_port_dump_next(&port_dump, &port)) {
909 node = shash_find(&init_ofp_ports, port.name);
910 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
911 garbage = xmalloc(sizeof *garbage);
912 garbage->odp_port = port.port_no;
913 list_push_front(&garbage_list, &garbage->list_node);
916 dpif_port_dump_done(&port_dump);
918 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
919 dpif_port_del(backer->dpif, garbage->odp_port);
920 list_remove(&garbage->list_node);
924 shash_add(&all_dpif_backers, type, backer);
926 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
928 VLOG_ERR("failed to listen on datapath of type %s: %s",
929 type, ovs_strerror(error));
930 close_dpif_backer(backer);
933 backer->enable_recirc = check_recirc(backer);
934 backer->variable_length_userdata = check_variable_length_userdata(backer);
935 backer->max_mpls_depth = check_max_mpls_depth(backer);
936 backer->rid_pool = recirc_id_pool_create();
938 if (backer->recv_set_enable) {
939 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
945 /* Tests whether 'backer''s datapath supports recirculation Only newer datapath
946 * supports OVS_KEY_ATTR in OVS_ACTION_ATTR_USERSPACE actions. We need to
947 * disable some features on older datapaths that don't support this feature.
949 * Returns false if 'backer' definitely does not support recirculation, true if
950 * it seems to support recirculation or if at least the error we get is
953 check_recirc(struct dpif_backer *backer)
956 struct odputil_keybuf keybuf;
959 bool enable_recirc = false;
961 memset(&flow, 0, sizeof flow);
965 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
966 odp_flow_key_from_flow(&key, &flow, NULL, 0);
968 error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
969 ofpbuf_data(&key), ofpbuf_size(&key), NULL, 0, NULL,
971 if (error && error != EEXIST) {
972 if (error != EINVAL) {
973 VLOG_WARN("%s: Reciculation flow probe failed (%s)",
974 dpif_name(backer->dpif), ovs_strerror(error));
979 error = dpif_flow_del(backer->dpif, ofpbuf_data(&key), ofpbuf_size(&key),
982 VLOG_WARN("%s: failed to delete recirculation feature probe flow",
983 dpif_name(backer->dpif));
986 enable_recirc = true;
990 VLOG_INFO("%s: Datapath supports recirculation",
991 dpif_name(backer->dpif));
993 VLOG_INFO("%s: Datapath does not support recirculation",
994 dpif_name(backer->dpif));
997 return enable_recirc;
1000 /* Tests whether 'backer''s datapath supports variable-length
1001 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1002 * to disable some features on older datapaths that don't support this
1005 * Returns false if 'backer' definitely does not support variable-length
1006 * userdata, true if it seems to support them or if at least the error we get
1009 check_variable_length_userdata(struct dpif_backer *backer)
1011 struct eth_header *eth;
1012 struct ofpbuf actions;
1013 struct dpif_execute execute;
1014 struct ofpbuf packet;
1018 /* Compose a userspace action that will cause an ERANGE error on older
1019 * datapaths that don't support variable-length userdata.
1021 * We really test for using userdata longer than 8 bytes, but older
1022 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1023 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1024 * test for that instead as a proxy for longer userdata support. */
1025 ofpbuf_init(&actions, 64);
1026 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1027 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1028 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1029 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1030 nl_msg_end_nested(&actions, start);
1032 /* Compose a dummy ethernet packet. */
1033 ofpbuf_init(&packet, ETH_HEADER_LEN);
1034 eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
1035 eth->eth_type = htons(0x1234);
1037 /* Execute the actions. On older datapaths this fails with ERANGE, on
1038 * newer datapaths it succeeds. */
1039 execute.actions = ofpbuf_data(&actions);
1040 execute.actions_len = ofpbuf_size(&actions);
1041 execute.packet = &packet;
1042 execute.md = PKT_METADATA_INITIALIZER(0);
1043 execute.needs_help = false;
1045 error = dpif_execute(backer->dpif, &execute);
1047 ofpbuf_uninit(&packet);
1048 ofpbuf_uninit(&actions);
1052 /* Variable-length userdata is supported.
1054 * Purge received packets to avoid processing the nonsense packet we
1055 * sent to userspace, then report success. */
1056 dpif_recv_purge(backer->dpif);
1060 /* Variable-length userdata is not supported. */
1061 VLOG_WARN("%s: datapath does not support variable-length userdata "
1062 "feature (needs Linux 3.10+ or kernel module from OVS "
1063 "1..11+). The NXAST_SAMPLE action will be ignored.",
1064 dpif_name(backer->dpif));
1068 /* Something odd happened. We're not sure whether variable-length
1069 * userdata is supported. Default to "yes". */
1070 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1071 dpif_name(backer->dpif), ovs_strerror(error));
1076 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1078 * Returns the number of elements in a struct flow's mpls_lse field
1079 * if the datapath supports at least that many entries in an
1081 * Otherwise returns the number of MPLS push actions supported by
1084 check_max_mpls_depth(struct dpif_backer *backer)
1089 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1090 struct odputil_keybuf keybuf;
1094 memset(&flow, 0, sizeof flow);
1095 flow.dl_type = htons(ETH_TYPE_MPLS);
1096 flow_set_mpls_bos(&flow, n, 1);
1098 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1099 odp_flow_key_from_flow(&key, &flow, NULL, 0);
1101 error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
1102 ofpbuf_data(&key), ofpbuf_size(&key), NULL, 0, NULL, 0, NULL);
1103 if (error && error != EEXIST) {
1104 if (error != EINVAL) {
1105 VLOG_WARN("%s: MPLS stack length feature probe failed (%s)",
1106 dpif_name(backer->dpif), ovs_strerror(error));
1111 error = dpif_flow_del(backer->dpif, ofpbuf_data(&key), ofpbuf_size(&key), NULL);
1113 VLOG_WARN("%s: failed to delete MPLS feature probe flow",
1114 dpif_name(backer->dpif));
1118 VLOG_INFO("%s: MPLS label stack length probed as %d",
1119 dpif_name(backer->dpif), n);
1124 construct(struct ofproto *ofproto_)
1126 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1127 struct shash_node *node, *next;
1130 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1135 ofproto->netflow = NULL;
1136 ofproto->sflow = NULL;
1137 ofproto->ipfix = NULL;
1138 ofproto->stp = NULL;
1139 ofproto->dump_seq = 0;
1140 hmap_init(&ofproto->bundles);
1141 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1142 ofproto->mbridge = mbridge_create();
1143 ofproto->has_bonded_bundles = false;
1144 ofproto->lacp_enabled = false;
1145 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1146 ovs_mutex_init(&ofproto->vsp_mutex);
1148 guarded_list_init(&ofproto->pins);
1150 ofproto_dpif_unixctl_init();
1152 hmap_init(&ofproto->vlandev_map);
1153 hmap_init(&ofproto->realdev_vid_map);
1155 sset_init(&ofproto->ports);
1156 sset_init(&ofproto->ghost_ports);
1157 sset_init(&ofproto->port_poll_set);
1158 ofproto->port_poll_errno = 0;
1159 ofproto->change_seq = 0;
1161 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1162 struct iface_hint *iface_hint = node->data;
1164 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1165 /* Check if the datapath already has this port. */
1166 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1167 sset_add(&ofproto->ports, node->name);
1170 free(iface_hint->br_name);
1171 free(iface_hint->br_type);
1173 shash_delete(&init_ofp_ports, node);
1177 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1178 hash_string(ofproto->up.name, 0));
1179 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1181 ofproto_init_tables(ofproto_, N_TABLES);
1182 error = add_internal_flows(ofproto);
1184 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1190 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1191 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1197 match_init_catchall(&match);
1198 match_set_reg(&match, 0, id);
1200 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, ofpacts, &rule);
1201 *rulep = error ? NULL : rule_dpif_cast(rule);
1207 add_internal_flows(struct ofproto_dpif *ofproto)
1209 struct ofpact_controller *controller;
1210 uint64_t ofpacts_stub[128 / 8];
1211 struct ofpbuf ofpacts;
1212 struct rule *unused_rulep OVS_UNUSED;
1213 struct ofpact_resubmit *resubmit;
1218 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1221 controller = ofpact_put_CONTROLLER(&ofpacts);
1222 controller->max_len = UINT16_MAX;
1223 controller->controller_id = 0;
1224 controller->reason = OFPR_NO_MATCH;
1225 ofpact_pad(&ofpacts);
1227 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1228 &ofproto->miss_rule);
1233 ofpbuf_clear(&ofpacts);
1234 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1235 &ofproto->no_packet_in_rule);
1240 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1241 &ofproto->drop_frags_rule);
1246 /* Continue non-recirculation rule lookups from table 0.
1248 * (priority=2), recirc=0, actions=resubmit(, 0)
1250 resubmit = ofpact_put_RESUBMIT(&ofpacts);
1251 resubmit->ofpact.compat = 0;
1252 resubmit->in_port = OFPP_IN_PORT;
1253 resubmit->table_id = 0;
1255 match_init_catchall(&match);
1256 match_set_recirc_id(&match, 0);
1258 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, &ofpacts,
1264 /* Drop any run away recirc rule lookups. Recirc_id has to be
1265 * non-zero when reaching this rule.
1267 * (priority=1), *, actions=drop
1269 ofpbuf_clear(&ofpacts);
1270 match_init_catchall(&match);
1271 error = ofproto_dpif_add_internal_flow(ofproto, &match, 1, &ofpacts,
1278 destruct(struct ofproto *ofproto_)
1280 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1281 struct rule_dpif *rule, *next_rule;
1282 struct ofproto_packet_in *pin, *next_pin;
1283 struct oftable *table;
1286 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1287 ovs_rwlock_wrlock(&xlate_rwlock);
1288 xlate_remove_ofproto(ofproto);
1289 ovs_rwlock_unlock(&xlate_rwlock);
1291 /* Ensure that the upcall processing threads have no remaining references
1292 * to the ofproto or anything in it. */
1293 udpif_synchronize(ofproto->backer->udpif);
1295 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1297 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1298 struct cls_cursor cursor;
1300 fat_rwlock_rdlock(&table->cls.rwlock);
1301 cls_cursor_init(&cursor, &table->cls, NULL);
1302 fat_rwlock_unlock(&table->cls.rwlock);
1303 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1304 ofproto_rule_delete(&ofproto->up, &rule->up);
1308 guarded_list_pop_all(&ofproto->pins, &pins);
1309 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1310 list_remove(&pin->list_node);
1311 free(CONST_CAST(void *, pin->up.packet));
1314 guarded_list_destroy(&ofproto->pins);
1316 mbridge_unref(ofproto->mbridge);
1318 netflow_unref(ofproto->netflow);
1319 dpif_sflow_unref(ofproto->sflow);
1320 hmap_destroy(&ofproto->bundles);
1321 mac_learning_unref(ofproto->ml);
1323 hmap_destroy(&ofproto->vlandev_map);
1324 hmap_destroy(&ofproto->realdev_vid_map);
1326 sset_destroy(&ofproto->ports);
1327 sset_destroy(&ofproto->ghost_ports);
1328 sset_destroy(&ofproto->port_poll_set);
1330 ovs_mutex_destroy(&ofproto->stats_mutex);
1331 ovs_mutex_destroy(&ofproto->vsp_mutex);
1333 close_dpif_backer(ofproto->backer);
1337 run(struct ofproto *ofproto_)
1339 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1340 uint64_t new_seq, new_dump_seq;
1341 const bool enable_recirc = ofproto_dpif_get_enable_recirc(ofproto);
1343 if (mbridge_need_revalidate(ofproto->mbridge)) {
1344 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1345 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1346 mac_learning_flush(ofproto->ml);
1347 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1350 /* Do not perform any periodic activity required by 'ofproto' while
1351 * waiting for flow restore to complete. */
1352 if (!ofproto_get_flow_restore_wait()) {
1353 struct ofproto_packet_in *pin, *next_pin;
1356 guarded_list_pop_all(&ofproto->pins, &pins);
1357 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1358 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1359 list_remove(&pin->list_node);
1360 free(CONST_CAST(void *, pin->up.packet));
1365 if (ofproto->netflow) {
1366 netflow_run(ofproto->netflow);
1368 if (ofproto->sflow) {
1369 dpif_sflow_run(ofproto->sflow);
1371 if (ofproto->ipfix) {
1372 dpif_ipfix_run(ofproto->ipfix);
1375 new_seq = seq_read(connectivity_seq_get());
1376 if (ofproto->change_seq != new_seq) {
1377 struct ofport_dpif *ofport;
1379 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1383 ofproto->change_seq = new_seq;
1385 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1386 struct ofbundle *bundle;
1388 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1394 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1395 if (mac_learning_run(ofproto->ml)) {
1396 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1398 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1400 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1401 if (ofproto->dump_seq != new_dump_seq) {
1402 struct rule *rule, *next_rule;
1404 /* We know stats are relatively fresh, so now is a good time to do some
1406 ofproto->dump_seq = new_dump_seq;
1408 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1410 ovs_mutex_lock(&ofproto_mutex);
1411 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1412 &ofproto->up.expirable) {
1413 rule_expire(rule_dpif_cast(rule));
1415 ovs_mutex_unlock(&ofproto_mutex);
1417 /* All outstanding data in existing flows has been accounted, so it's a
1418 * good time to do bond rebalancing. */
1419 if (enable_recirc && ofproto->has_bonded_bundles) {
1420 struct ofbundle *bundle;
1422 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1423 struct bond *bond = bundle->bond;
1425 if (bond && bond_may_recirc(bond, NULL, NULL)) {
1426 bond_recirculation_account(bond);
1427 if (bond_rebalance(bundle->bond)) {
1428 bond_update_post_recirc_rules(bond, true);
1439 wait(struct ofproto *ofproto_)
1441 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1443 if (ofproto_get_flow_restore_wait()) {
1447 if (ofproto->sflow) {
1448 dpif_sflow_wait(ofproto->sflow);
1450 if (ofproto->ipfix) {
1451 dpif_ipfix_wait(ofproto->ipfix);
1453 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1454 struct ofbundle *bundle;
1456 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1457 bundle_wait(bundle);
1460 if (ofproto->netflow) {
1461 netflow_wait(ofproto->netflow);
1463 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1464 mac_learning_wait(ofproto->ml);
1465 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1467 if (ofproto->backer->need_revalidate) {
1468 /* Shouldn't happen, but if it does just go around again. */
1469 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1470 poll_immediate_wake();
1473 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1477 type_get_memory_usage(const char *type, struct simap *usage)
1479 struct dpif_backer *backer;
1481 backer = shash_find_data(&all_dpif_backers, type);
1483 udpif_get_memory_usage(backer->udpif, usage);
1488 flush(struct ofproto *ofproto_)
1490 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1491 struct dpif_backer *backer = ofproto->backer;
1494 udpif_flush(backer->udpif);
1499 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1500 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1502 *arp_match_ip = true;
1503 *actions = (OFPUTIL_A_OUTPUT |
1504 OFPUTIL_A_SET_VLAN_VID |
1505 OFPUTIL_A_SET_VLAN_PCP |
1506 OFPUTIL_A_STRIP_VLAN |
1507 OFPUTIL_A_SET_DL_SRC |
1508 OFPUTIL_A_SET_DL_DST |
1509 OFPUTIL_A_SET_NW_SRC |
1510 OFPUTIL_A_SET_NW_DST |
1511 OFPUTIL_A_SET_NW_TOS |
1512 OFPUTIL_A_SET_TP_SRC |
1513 OFPUTIL_A_SET_TP_DST |
1518 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1520 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1521 struct dpif_dp_stats s;
1522 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1526 strcpy(ots->name, "classifier");
1528 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1529 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes, &used);
1530 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes,
1532 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes,
1534 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1535 ots->lookup_count = htonll(n_lookup);
1536 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1539 static struct ofport *
1542 struct ofport_dpif *port = xmalloc(sizeof *port);
1547 port_dealloc(struct ofport *port_)
1549 struct ofport_dpif *port = ofport_dpif_cast(port_);
1554 port_construct(struct ofport *port_)
1556 struct ofport_dpif *port = ofport_dpif_cast(port_);
1557 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1558 const struct netdev *netdev = port->up.netdev;
1559 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1560 struct dpif_port dpif_port;
1563 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1564 port->bundle = NULL;
1567 port->may_enable = true;
1568 port->stp_port = NULL;
1569 port->stp_state = STP_DISABLED;
1570 port->is_tunnel = false;
1574 port->realdev_ofp_port = 0;
1575 port->vlandev_vid = 0;
1576 port->carrier_seq = netdev_get_carrier_resets(netdev);
1577 port->is_layer3 = netdev_vport_is_layer3(netdev);
1579 if (netdev_vport_is_patch(netdev)) {
1580 /* By bailing out here, we don't submit the port to the sFlow module
1581 * to be considered for counter polling export. This is correct
1582 * because the patch port represents an interface that sFlow considers
1583 * to be "internal" to the switch as a whole, and therefore not an
1584 * candidate for counter polling. */
1585 port->odp_port = ODPP_NONE;
1586 ofport_update_peer(port);
1590 error = dpif_port_query_by_name(ofproto->backer->dpif,
1591 netdev_vport_get_dpif_port(netdev, namebuf,
1598 port->odp_port = dpif_port.port_no;
1600 if (netdev_get_tunnel_config(netdev)) {
1601 tnl_port_add(port, port->up.netdev, port->odp_port);
1602 port->is_tunnel = true;
1604 /* Sanity-check that a mapping doesn't already exist. This
1605 * shouldn't happen for non-tunnel ports. */
1606 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1607 VLOG_ERR("port %s already has an OpenFlow port number",
1609 dpif_port_destroy(&dpif_port);
1613 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1614 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1615 hash_odp_port(port->odp_port));
1616 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1618 dpif_port_destroy(&dpif_port);
1620 if (ofproto->sflow) {
1621 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1628 port_destruct(struct ofport *port_)
1630 struct ofport_dpif *port = ofport_dpif_cast(port_);
1631 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1632 const char *devname = netdev_get_name(port->up.netdev);
1633 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1634 const char *dp_port_name;
1636 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1637 ovs_rwlock_wrlock(&xlate_rwlock);
1638 xlate_ofport_remove(port);
1639 ovs_rwlock_unlock(&xlate_rwlock);
1641 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1643 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1644 /* The underlying device is still there, so delete it. This
1645 * happens when the ofproto is being destroyed, since the caller
1646 * assumes that removal of attached ports will happen as part of
1648 if (!port->is_tunnel) {
1649 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1654 port->peer->peer = NULL;
1658 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1659 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1660 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1661 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1665 sset_find_and_delete(&ofproto->ports, devname);
1666 sset_find_and_delete(&ofproto->ghost_ports, devname);
1667 bundle_remove(port_);
1668 set_cfm(port_, NULL);
1669 set_bfd(port_, NULL);
1670 if (port->stp_port) {
1671 stp_port_disable(port->stp_port);
1673 if (ofproto->sflow) {
1674 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1681 port_modified(struct ofport *port_)
1683 struct ofport_dpif *port = ofport_dpif_cast(port_);
1685 if (port->bundle && port->bundle->bond) {
1686 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1690 cfm_set_netdev(port->cfm, port->up.netdev);
1694 bfd_set_netdev(port->bfd, port->up.netdev);
1697 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1698 port->up.pp.hw_addr);
1700 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1702 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1706 ofport_update_peer(port);
1710 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1712 struct ofport_dpif *port = ofport_dpif_cast(port_);
1713 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1714 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1716 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1717 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1718 OFPUTIL_PC_NO_PACKET_IN)) {
1719 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1721 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1722 bundle_update(port->bundle);
1728 set_sflow(struct ofproto *ofproto_,
1729 const struct ofproto_sflow_options *sflow_options)
1731 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1732 struct dpif_sflow *ds = ofproto->sflow;
1734 if (sflow_options) {
1736 struct ofport_dpif *ofport;
1738 ds = ofproto->sflow = dpif_sflow_create();
1739 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1740 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1742 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1744 dpif_sflow_set_options(ds, sflow_options);
1747 dpif_sflow_unref(ds);
1748 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1749 ofproto->sflow = NULL;
1757 struct ofproto *ofproto_,
1758 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1759 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1760 size_t n_flow_exporters_options)
1762 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1763 struct dpif_ipfix *di = ofproto->ipfix;
1764 bool has_options = bridge_exporter_options || flow_exporters_options;
1766 if (has_options && !di) {
1767 di = ofproto->ipfix = dpif_ipfix_create();
1771 /* Call set_options in any case to cleanly flush the flow
1772 * caches in the last exporters that are to be destroyed. */
1773 dpif_ipfix_set_options(
1774 di, bridge_exporter_options, flow_exporters_options,
1775 n_flow_exporters_options);
1778 dpif_ipfix_unref(di);
1779 ofproto->ipfix = NULL;
1787 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1789 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1794 struct ofproto_dpif *ofproto;
1796 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1797 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1798 ofport->cfm = cfm_create(ofport->up.netdev);
1801 if (cfm_configure(ofport->cfm, s)) {
1808 cfm_unref(ofport->cfm);
1811 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1812 ofport->up.pp.hw_addr);
1817 get_cfm_status(const struct ofport *ofport_,
1818 struct ofproto_cfm_status *status)
1820 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1823 status->faults = cfm_get_fault(ofport->cfm);
1824 status->flap_count = cfm_get_flap_count(ofport->cfm);
1825 status->remote_opstate = cfm_get_opup(ofport->cfm);
1826 status->health = cfm_get_health(ofport->cfm);
1827 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1835 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1837 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1838 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1842 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1843 cfg, ofport->up.netdev);
1844 if (ofport->bfd != old) {
1845 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1847 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1848 ofport->up.pp.hw_addr);
1853 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1855 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1858 bfd_get_status(ofport->bfd, smap);
1865 /* Spanning Tree. */
1868 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1870 struct ofproto_dpif *ofproto = ofproto_;
1871 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1872 struct ofport_dpif *ofport;
1874 ofport = stp_port_get_aux(sp);
1876 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1877 ofproto->up.name, port_num);
1879 struct eth_header *eth = ofpbuf_l2(pkt);
1881 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1882 if (eth_addr_is_zero(eth->eth_src)) {
1883 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1884 "with unknown MAC", ofproto->up.name, port_num);
1886 ofproto_dpif_send_packet(ofport, pkt);
1892 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1894 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1896 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1898 /* Only revalidate flows if the configuration changed. */
1899 if (!s != !ofproto->stp) {
1900 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1904 if (!ofproto->stp) {
1905 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1906 send_bpdu_cb, ofproto);
1907 ofproto->stp_last_tick = time_msec();
1910 stp_set_bridge_id(ofproto->stp, s->system_id);
1911 stp_set_bridge_priority(ofproto->stp, s->priority);
1912 stp_set_hello_time(ofproto->stp, s->hello_time);
1913 stp_set_max_age(ofproto->stp, s->max_age);
1914 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1916 struct ofport *ofport;
1918 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1919 set_stp_port(ofport, NULL);
1922 stp_unref(ofproto->stp);
1923 ofproto->stp = NULL;
1930 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1932 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1936 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1937 s->designated_root = stp_get_designated_root(ofproto->stp);
1938 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1947 update_stp_port_state(struct ofport_dpif *ofport)
1949 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1950 enum stp_state state;
1952 /* Figure out new state. */
1953 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1957 if (ofport->stp_state != state) {
1958 enum ofputil_port_state of_state;
1961 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1962 netdev_get_name(ofport->up.netdev),
1963 stp_state_name(ofport->stp_state),
1964 stp_state_name(state));
1965 if (stp_learn_in_state(ofport->stp_state)
1966 != stp_learn_in_state(state)) {
1967 /* xxx Learning action flows should also be flushed. */
1968 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1969 mac_learning_flush(ofproto->ml);
1970 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1972 fwd_change = stp_forward_in_state(ofport->stp_state)
1973 != stp_forward_in_state(state);
1975 ofproto->backer->need_revalidate = REV_STP;
1976 ofport->stp_state = state;
1977 ofport->stp_state_entered = time_msec();
1979 if (fwd_change && ofport->bundle) {
1980 bundle_update(ofport->bundle);
1983 /* Update the STP state bits in the OpenFlow port description. */
1984 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1985 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1986 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1987 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1988 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1990 ofproto_port_set_state(&ofport->up, of_state);
1994 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1995 * caller is responsible for assigning STP port numbers and ensuring
1996 * there are no duplicates. */
1998 set_stp_port(struct ofport *ofport_,
1999 const struct ofproto_port_stp_settings *s)
2001 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2002 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2003 struct stp_port *sp = ofport->stp_port;
2005 if (!s || !s->enable) {
2007 ofport->stp_port = NULL;
2008 stp_port_disable(sp);
2009 update_stp_port_state(ofport);
2012 } else if (sp && stp_port_no(sp) != s->port_num
2013 && ofport == stp_port_get_aux(sp)) {
2014 /* The port-id changed, so disable the old one if it's not
2015 * already in use by another port. */
2016 stp_port_disable(sp);
2019 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2020 stp_port_enable(sp);
2022 stp_port_set_aux(sp, ofport);
2023 stp_port_set_priority(sp, s->priority);
2024 stp_port_set_path_cost(sp, s->path_cost);
2026 update_stp_port_state(ofport);
2032 get_stp_port_status(struct ofport *ofport_,
2033 struct ofproto_port_stp_status *s)
2035 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2036 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2037 struct stp_port *sp = ofport->stp_port;
2039 if (!ofproto->stp || !sp) {
2045 s->port_id = stp_port_get_id(sp);
2046 s->state = stp_port_get_state(sp);
2047 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2048 s->role = stp_port_get_role(sp);
2054 get_stp_port_stats(struct ofport *ofport_,
2055 struct ofproto_port_stp_stats *s)
2057 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2058 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2059 struct stp_port *sp = ofport->stp_port;
2061 if (!ofproto->stp || !sp) {
2067 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2073 stp_run(struct ofproto_dpif *ofproto)
2076 long long int now = time_msec();
2077 long long int elapsed = now - ofproto->stp_last_tick;
2078 struct stp_port *sp;
2081 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2082 ofproto->stp_last_tick = now;
2084 while (stp_get_changed_port(ofproto->stp, &sp)) {
2085 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2088 update_stp_port_state(ofport);
2092 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2093 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2094 mac_learning_flush(ofproto->ml);
2095 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2101 stp_wait(struct ofproto_dpif *ofproto)
2104 poll_timer_wait(1000);
2109 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2112 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2113 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2115 if (ofport->n_qdscp != n_qdscp
2116 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2117 n_qdscp * sizeof *qdscp))) {
2118 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2119 free(ofport->qdscp);
2120 ofport->qdscp = n_qdscp
2121 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2123 ofport->n_qdscp = n_qdscp;
2131 /* Expires all MAC learning entries associated with 'bundle' and forces its
2132 * ofproto to revalidate every flow.
2134 * Normally MAC learning entries are removed only from the ofproto associated
2135 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2136 * are removed from every ofproto. When patch ports and SLB bonds are in use
2137 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2138 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2139 * with the host from which it migrated. */
2141 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2143 struct ofproto_dpif *ofproto = bundle->ofproto;
2144 struct mac_learning *ml = ofproto->ml;
2145 struct mac_entry *mac, *next_mac;
2147 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2148 ovs_rwlock_wrlock(&ml->rwlock);
2149 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2150 if (mac->port.p == bundle) {
2152 struct ofproto_dpif *o;
2154 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2156 struct mac_entry *e;
2158 ovs_rwlock_wrlock(&o->ml->rwlock);
2159 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2161 mac_learning_expire(o->ml, e);
2163 ovs_rwlock_unlock(&o->ml->rwlock);
2168 mac_learning_expire(ml, mac);
2171 ovs_rwlock_unlock(&ml->rwlock);
2174 static struct ofbundle *
2175 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2177 struct ofbundle *bundle;
2179 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2180 &ofproto->bundles) {
2181 if (bundle->aux == aux) {
2189 bundle_update(struct ofbundle *bundle)
2191 struct ofport_dpif *port;
2193 bundle->floodable = true;
2194 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2195 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2197 || !stp_forward_in_state(port->stp_state)) {
2198 bundle->floodable = false;
2205 bundle_del_port(struct ofport_dpif *port)
2207 struct ofbundle *bundle = port->bundle;
2209 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2211 list_remove(&port->bundle_node);
2212 port->bundle = NULL;
2215 lacp_slave_unregister(bundle->lacp, port);
2218 bond_slave_unregister(bundle->bond, port);
2221 bundle_update(bundle);
2225 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2226 struct lacp_slave_settings *lacp)
2228 struct ofport_dpif *port;
2230 port = get_ofp_port(bundle->ofproto, ofp_port);
2235 if (port->bundle != bundle) {
2236 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2238 bundle_remove(&port->up);
2241 port->bundle = bundle;
2242 list_push_back(&bundle->ports, &port->bundle_node);
2243 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2245 || !stp_forward_in_state(port->stp_state)) {
2246 bundle->floodable = false;
2250 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2251 lacp_slave_register(bundle->lacp, port, lacp);
2258 bundle_destroy(struct ofbundle *bundle)
2260 struct ofproto_dpif *ofproto;
2261 struct ofport_dpif *port, *next_port;
2267 ofproto = bundle->ofproto;
2268 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2270 ovs_rwlock_wrlock(&xlate_rwlock);
2271 xlate_bundle_remove(bundle);
2272 ovs_rwlock_unlock(&xlate_rwlock);
2274 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2275 bundle_del_port(port);
2278 bundle_flush_macs(bundle, true);
2279 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2281 free(bundle->trunks);
2282 lacp_unref(bundle->lacp);
2283 bond_unref(bundle->bond);
2288 bundle_set(struct ofproto *ofproto_, void *aux,
2289 const struct ofproto_bundle_settings *s)
2291 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2292 bool need_flush = false;
2293 struct ofport_dpif *port;
2294 struct ofbundle *bundle;
2295 unsigned long *trunks;
2301 bundle_destroy(bundle_lookup(ofproto, aux));
2305 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2306 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2308 bundle = bundle_lookup(ofproto, aux);
2310 bundle = xmalloc(sizeof *bundle);
2312 bundle->ofproto = ofproto;
2313 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2314 hash_pointer(aux, 0));
2316 bundle->name = NULL;
2318 list_init(&bundle->ports);
2319 bundle->vlan_mode = PORT_VLAN_TRUNK;
2321 bundle->trunks = NULL;
2322 bundle->use_priority_tags = s->use_priority_tags;
2323 bundle->lacp = NULL;
2324 bundle->bond = NULL;
2326 bundle->floodable = true;
2327 mbridge_register_bundle(ofproto->mbridge, bundle);
2330 if (!bundle->name || strcmp(s->name, bundle->name)) {
2332 bundle->name = xstrdup(s->name);
2337 ofproto->lacp_enabled = true;
2338 if (!bundle->lacp) {
2339 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2340 bundle->lacp = lacp_create();
2342 lacp_configure(bundle->lacp, s->lacp);
2344 lacp_unref(bundle->lacp);
2345 bundle->lacp = NULL;
2348 /* Update set of ports. */
2350 for (i = 0; i < s->n_slaves; i++) {
2351 if (!bundle_add_port(bundle, s->slaves[i],
2352 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2356 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2357 struct ofport_dpif *next_port;
2359 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2360 for (i = 0; i < s->n_slaves; i++) {
2361 if (s->slaves[i] == port->up.ofp_port) {
2366 bundle_del_port(port);
2370 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2372 if (list_is_empty(&bundle->ports)) {
2373 bundle_destroy(bundle);
2377 /* Set VLAN tagging mode */
2378 if (s->vlan_mode != bundle->vlan_mode
2379 || s->use_priority_tags != bundle->use_priority_tags) {
2380 bundle->vlan_mode = s->vlan_mode;
2381 bundle->use_priority_tags = s->use_priority_tags;
2386 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2387 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2389 if (vlan != bundle->vlan) {
2390 bundle->vlan = vlan;
2394 /* Get trunked VLANs. */
2395 switch (s->vlan_mode) {
2396 case PORT_VLAN_ACCESS:
2400 case PORT_VLAN_TRUNK:
2401 trunks = CONST_CAST(unsigned long *, s->trunks);
2404 case PORT_VLAN_NATIVE_UNTAGGED:
2405 case PORT_VLAN_NATIVE_TAGGED:
2406 if (vlan != 0 && (!s->trunks
2407 || !bitmap_is_set(s->trunks, vlan)
2408 || bitmap_is_set(s->trunks, 0))) {
2409 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2411 trunks = bitmap_clone(s->trunks, 4096);
2413 trunks = bitmap_allocate1(4096);
2415 bitmap_set1(trunks, vlan);
2416 bitmap_set0(trunks, 0);
2418 trunks = CONST_CAST(unsigned long *, s->trunks);
2425 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2426 free(bundle->trunks);
2427 if (trunks == s->trunks) {
2428 bundle->trunks = vlan_bitmap_clone(trunks);
2430 bundle->trunks = trunks;
2435 if (trunks != s->trunks) {
2440 if (!list_is_short(&bundle->ports)) {
2441 bundle->ofproto->has_bonded_bundles = true;
2443 if (bond_reconfigure(bundle->bond, s->bond)) {
2444 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2447 bundle->bond = bond_create(s->bond, ofproto);
2448 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2451 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2452 bond_slave_register(bundle->bond, port,
2453 port->up.ofp_port, port->up.netdev);
2456 bond_unref(bundle->bond);
2457 bundle->bond = NULL;
2460 /* If we changed something that would affect MAC learning, un-learn
2461 * everything on this port and force flow revalidation. */
2463 bundle_flush_macs(bundle, false);
2470 bundle_remove(struct ofport *port_)
2472 struct ofport_dpif *port = ofport_dpif_cast(port_);
2473 struct ofbundle *bundle = port->bundle;
2476 bundle_del_port(port);
2477 if (list_is_empty(&bundle->ports)) {
2478 bundle_destroy(bundle);
2479 } else if (list_is_short(&bundle->ports)) {
2480 bond_unref(bundle->bond);
2481 bundle->bond = NULL;
2487 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2489 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2490 struct ofport_dpif *port = port_;
2491 uint8_t ea[ETH_ADDR_LEN];
2494 error = netdev_get_etheraddr(port->up.netdev, ea);
2496 struct ofpbuf packet;
2499 ofpbuf_init(&packet, 0);
2500 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2502 memcpy(packet_pdu, pdu, pdu_size);
2504 ofproto_dpif_send_packet(port, &packet);
2505 ofpbuf_uninit(&packet);
2507 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2508 "%s (%s)", port->bundle->name,
2509 netdev_get_name(port->up.netdev), ovs_strerror(error));
2514 bundle_send_learning_packets(struct ofbundle *bundle)
2516 struct ofproto_dpif *ofproto = bundle->ofproto;
2517 struct ofpbuf *learning_packet;
2518 int error, n_packets, n_errors;
2519 struct mac_entry *e;
2520 struct list packets;
2522 list_init(&packets);
2523 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2524 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2525 if (e->port.p != bundle) {
2528 learning_packet = bond_compose_learning_packet(bundle->bond,
2531 /* Temporarily use 'frame' as a private pointer (see below). */
2532 ovs_assert(learning_packet->frame == ofpbuf_data(learning_packet));
2533 learning_packet->frame = port_void;
2534 list_push_back(&packets, &learning_packet->list_node);
2537 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2539 error = n_packets = n_errors = 0;
2540 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2542 void *port_void = learning_packet->frame;
2544 /* Restore 'frame'. */
2545 learning_packet->frame = ofpbuf_data(learning_packet);
2546 ret = ofproto_dpif_send_packet(port_void, learning_packet);
2553 ofpbuf_list_delete(&packets);
2556 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2557 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2558 "packets, last error was: %s",
2559 bundle->name, n_errors, n_packets, ovs_strerror(error));
2561 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2562 bundle->name, n_packets);
2567 bundle_run(struct ofbundle *bundle)
2570 lacp_run(bundle->lacp, send_pdu_cb);
2573 struct ofport_dpif *port;
2575 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2576 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2579 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2580 bundle->ofproto->backer->need_revalidate = REV_BOND;
2583 if (bond_should_send_learning_packets(bundle->bond)) {
2584 bundle_send_learning_packets(bundle);
2590 bundle_wait(struct ofbundle *bundle)
2593 lacp_wait(bundle->lacp);
2596 bond_wait(bundle->bond);
2603 mirror_set__(struct ofproto *ofproto_, void *aux,
2604 const struct ofproto_mirror_settings *s)
2606 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2607 struct ofbundle **srcs, **dsts;
2612 mirror_destroy(ofproto->mbridge, aux);
2616 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2617 dsts = xmalloc(s->n_dsts * sizeof *dsts);
2619 for (i = 0; i < s->n_srcs; i++) {
2620 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2623 for (i = 0; i < s->n_dsts; i++) {
2624 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2627 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2628 s->n_dsts, s->src_vlans,
2629 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2636 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2637 uint64_t *packets, uint64_t *bytes)
2639 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2644 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2646 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2647 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2648 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2649 mac_learning_flush(ofproto->ml);
2651 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2656 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2658 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2659 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2660 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2664 forward_bpdu_changed(struct ofproto *ofproto_)
2666 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2667 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2671 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2674 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2675 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2676 mac_learning_set_idle_time(ofproto->ml, idle_time);
2677 mac_learning_set_max_entries(ofproto->ml, max_entries);
2678 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2683 static struct ofport_dpif *
2684 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2686 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2687 return ofport ? ofport_dpif_cast(ofport) : NULL;
2691 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2692 struct ofproto_port *ofproto_port,
2693 struct dpif_port *dpif_port)
2695 ofproto_port->name = dpif_port->name;
2696 ofproto_port->type = dpif_port->type;
2697 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2701 ofport_update_peer(struct ofport_dpif *ofport)
2703 const struct ofproto_dpif *ofproto;
2704 struct dpif_backer *backer;
2707 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2711 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2712 backer->need_revalidate = REV_RECONFIGURE;
2715 ofport->peer->peer = NULL;
2716 ofport->peer = NULL;
2719 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2724 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2725 struct ofport *peer_ofport;
2726 struct ofport_dpif *peer;
2729 if (ofproto->backer != backer) {
2733 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2738 peer = ofport_dpif_cast(peer_ofport);
2739 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2740 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2742 ofport->peer = peer;
2743 ofport->peer->peer = ofport;
2753 port_run(struct ofport_dpif *ofport)
2755 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2756 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2757 bool enable = netdev_get_carrier(ofport->up.netdev);
2758 bool cfm_enable = false;
2759 bool bfd_enable = false;
2761 ofport->carrier_seq = carrier_seq;
2764 int cfm_opup = cfm_get_opup(ofport->cfm);
2766 cfm_enable = !cfm_get_fault(ofport->cfm);
2768 if (cfm_opup >= 0) {
2769 cfm_enable = cfm_enable && cfm_opup;
2774 bfd_enable = bfd_forwarding(ofport->bfd);
2777 if (ofport->bfd || ofport->cfm) {
2778 enable = enable && (cfm_enable || bfd_enable);
2781 if (ofport->bundle) {
2782 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2783 if (carrier_changed) {
2784 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2788 if (ofport->may_enable != enable) {
2789 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2790 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2793 ofport->may_enable = enable;
2797 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2798 struct ofproto_port *ofproto_port)
2800 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2801 struct dpif_port dpif_port;
2804 if (sset_contains(&ofproto->ghost_ports, devname)) {
2805 const char *type = netdev_get_type_from_name(devname);
2807 /* We may be called before ofproto->up.port_by_name is populated with
2808 * the appropriate ofport. For this reason, we must get the name and
2809 * type from the netdev layer directly. */
2811 const struct ofport *ofport;
2813 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2814 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2815 ofproto_port->name = xstrdup(devname);
2816 ofproto_port->type = xstrdup(type);
2822 if (!sset_contains(&ofproto->ports, devname)) {
2825 error = dpif_port_query_by_name(ofproto->backer->dpif,
2826 devname, &dpif_port);
2828 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2834 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2836 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2837 const char *devname = netdev_get_name(netdev);
2838 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2839 const char *dp_port_name;
2841 if (netdev_vport_is_patch(netdev)) {
2842 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2846 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2847 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2848 odp_port_t port_no = ODPP_NONE;
2851 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2855 if (netdev_get_tunnel_config(netdev)) {
2856 simap_put(&ofproto->backer->tnl_backers,
2857 dp_port_name, odp_to_u32(port_no));
2861 if (netdev_get_tunnel_config(netdev)) {
2862 sset_add(&ofproto->ghost_ports, devname);
2864 sset_add(&ofproto->ports, devname);
2870 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2872 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2873 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2880 sset_find_and_delete(&ofproto->ghost_ports,
2881 netdev_get_name(ofport->up.netdev));
2882 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2883 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2884 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2886 /* The caller is going to close ofport->up.netdev. If this is a
2887 * bonded port, then the bond is using that netdev, so remove it
2888 * from the bond. The client will need to reconfigure everything
2889 * after deleting ports, so then the slave will get re-added. */
2890 bundle_remove(&ofport->up);
2897 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2899 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2902 error = netdev_get_stats(ofport->up.netdev, stats);
2904 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2905 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2907 ovs_mutex_lock(&ofproto->stats_mutex);
2908 /* ofproto->stats.tx_packets represents packets that we created
2909 * internally and sent to some port (e.g. packets sent with
2910 * ofproto_dpif_send_packet()). Account for them as if they had
2911 * come from OFPP_LOCAL and got forwarded. */
2913 if (stats->rx_packets != UINT64_MAX) {
2914 stats->rx_packets += ofproto->stats.tx_packets;
2917 if (stats->rx_bytes != UINT64_MAX) {
2918 stats->rx_bytes += ofproto->stats.tx_bytes;
2921 /* ofproto->stats.rx_packets represents packets that were received on
2922 * some port and we processed internally and dropped (e.g. STP).
2923 * Account for them as if they had been forwarded to OFPP_LOCAL. */
2925 if (stats->tx_packets != UINT64_MAX) {
2926 stats->tx_packets += ofproto->stats.rx_packets;
2929 if (stats->tx_bytes != UINT64_MAX) {
2930 stats->tx_bytes += ofproto->stats.rx_bytes;
2932 ovs_mutex_unlock(&ofproto->stats_mutex);
2938 struct port_dump_state {
2943 struct ofproto_port port;
2948 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
2950 *statep = xzalloc(sizeof(struct port_dump_state));
2955 port_dump_next(const struct ofproto *ofproto_, void *state_,
2956 struct ofproto_port *port)
2958 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2959 struct port_dump_state *state = state_;
2960 const struct sset *sset;
2961 struct sset_node *node;
2963 if (state->has_port) {
2964 ofproto_port_destroy(&state->port);
2965 state->has_port = false;
2967 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
2968 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
2971 error = port_query_by_name(ofproto_, node->name, &state->port);
2973 *port = state->port;
2974 state->has_port = true;
2976 } else if (error != ENODEV) {
2981 if (!state->ghost) {
2982 state->ghost = true;
2985 return port_dump_next(ofproto_, state_, port);
2992 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
2994 struct port_dump_state *state = state_;
2996 if (state->has_port) {
2997 ofproto_port_destroy(&state->port);
3004 port_poll(const struct ofproto *ofproto_, char **devnamep)
3006 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3008 if (ofproto->port_poll_errno) {
3009 int error = ofproto->port_poll_errno;
3010 ofproto->port_poll_errno = 0;
3014 if (sset_is_empty(&ofproto->port_poll_set)) {
3018 *devnamep = sset_pop(&ofproto->port_poll_set);
3023 port_poll_wait(const struct ofproto *ofproto_)
3025 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3026 dpif_port_poll_wait(ofproto->backer->dpif);
3030 port_is_lacp_current(const struct ofport *ofport_)
3032 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3033 return (ofport->bundle && ofport->bundle->lacp
3034 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3038 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3039 * then delete it entirely. */
3041 rule_expire(struct rule_dpif *rule)
3042 OVS_REQUIRES(ofproto_mutex)
3044 uint16_t hard_timeout, idle_timeout;
3045 long long int now = time_msec();
3048 ovs_assert(!rule->up.pending);
3050 hard_timeout = rule->up.hard_timeout;
3051 idle_timeout = rule->up.idle_timeout;
3053 /* Has 'rule' expired? */
3055 long long int modified;
3057 ovs_mutex_lock(&rule->up.mutex);
3058 modified = rule->up.modified;
3059 ovs_mutex_unlock(&rule->up.mutex);
3061 if (now > modified + hard_timeout * 1000) {
3062 reason = OFPRR_HARD_TIMEOUT;
3066 if (reason < 0 && idle_timeout) {
3069 ovs_mutex_lock(&rule->stats_mutex);
3070 used = rule->stats.used;
3071 ovs_mutex_unlock(&rule->stats_mutex);
3073 if (now > used + idle_timeout * 1000) {
3074 reason = OFPRR_IDLE_TIMEOUT;
3079 COVERAGE_INC(ofproto_dpif_expired);
3080 ofproto_rule_expire(&rule->up, reason);
3084 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3085 * 'flow' must reflect the data in 'packet'. */
3087 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3088 const struct flow *flow,
3089 struct rule_dpif *rule,
3090 const struct ofpact *ofpacts, size_t ofpacts_len,
3091 struct ofpbuf *packet)
3093 struct dpif_flow_stats stats;
3094 struct xlate_out xout;
3095 struct xlate_in xin;
3097 struct dpif_execute execute;
3100 ovs_assert((rule != NULL) != (ofpacts != NULL));
3102 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3105 rule_dpif_credit_stats(rule, &stats);
3108 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
3109 xin.ofpacts = ofpacts;
3110 xin.ofpacts_len = ofpacts_len;
3111 xin.resubmit_stats = &stats;
3112 xlate_actions(&xin, &xout);
3114 in_port = flow->in_port.ofp_port;
3115 if (in_port == OFPP_NONE) {
3116 in_port = OFPP_LOCAL;
3118 execute.actions = ofpbuf_data(&xout.odp_actions);
3119 execute.actions_len = ofpbuf_size(&xout.odp_actions);
3120 execute.packet = packet;
3121 execute.md.tunnel = flow->tunnel;
3122 execute.md.skb_priority = flow->skb_priority;
3123 execute.md.pkt_mark = flow->pkt_mark;
3124 execute.md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3125 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3127 error = dpif_execute(ofproto->backer->dpif, &execute);
3129 xlate_out_uninit(&xout);
3135 rule_dpif_credit_stats(struct rule_dpif *rule,
3136 const struct dpif_flow_stats *stats)
3138 ovs_mutex_lock(&rule->stats_mutex);
3139 rule->stats.n_packets += stats->n_packets;
3140 rule->stats.n_bytes += stats->n_bytes;
3141 rule->stats.used = MAX(rule->stats.used, stats->used);
3142 ovs_mutex_unlock(&rule->stats_mutex);
3146 rule_dpif_is_fail_open(const struct rule_dpif *rule)
3148 return is_fail_open_rule(&rule->up);
3152 rule_dpif_is_table_miss(const struct rule_dpif *rule)
3154 return rule_is_table_miss(&rule->up);
3158 rule_dpif_is_internal(const struct rule_dpif *rule)
3160 return rule_is_internal(&rule->up);
3164 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3165 OVS_REQUIRES(rule->up.mutex)
3167 return rule->up.flow_cookie;
3171 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3172 uint16_t hard_timeout)
3174 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3177 /* Returns 'rule''s actions. The caller owns a reference on the returned
3178 * actions and must eventually release it (with rule_actions_unref()) to avoid
3180 struct rule_actions *
3181 rule_dpif_get_actions(const struct rule_dpif *rule)
3183 return rule_get_actions(&rule->up);
3186 /* Lookup 'flow' in table 0 of 'ofproto''s classifier.
3187 * If 'wc' is non-null, sets the fields that were relevant as part of
3188 * the lookup. Returns the table_id where a match or miss occurred.
3190 * The return value will be zero unless there was a miss and
3191 * OFPTC11_TABLE_MISS_CONTINUE is in effect for the sequence of tables
3192 * where misses occur.
3194 * The rule is returned in '*rule', which is valid at least until the next
3195 * RCU quiescent period. If the '*rule' needs to stay around longer,
3196 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3197 * on it before this returns. */
3199 rule_dpif_lookup(struct ofproto_dpif *ofproto, struct flow *flow,
3200 struct flow_wildcards *wc, struct rule_dpif **rule,
3203 enum rule_dpif_lookup_verdict verdict;
3204 enum ofputil_port_config config = 0;
3207 if (ofproto_dpif_get_enable_recirc(ofproto)) {
3208 /* Always exactly match recirc_id since datapath supports
3211 wc->masks.recirc_id = UINT32_MAX;
3214 /* Start looking up from internal table for post recirculation flows
3215 * or packets. We can also simply send all, including normal flows
3216 * or packets to the internal table. They will not match any post
3217 * recirculation rules except the 'catch all' rule that resubmit
3220 * As an optimization, we send normal flows and packets to table 0
3221 * directly, saving one table lookup. */
3222 table_id = flow->recirc_id ? TBL_INTERNAL : 0;
3227 verdict = rule_dpif_lookup_from_table(ofproto, flow, wc, true,
3228 &table_id, rule, take_ref);
3231 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
3233 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER: {
3234 struct ofport_dpif *port;
3236 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
3238 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3239 flow->in_port.ofp_port);
3241 config = port ? port->up.pp.config : 0;
3244 case RULE_DPIF_LOOKUP_VERDICT_DROP:
3245 config = OFPUTIL_PC_NO_PACKET_IN;
3247 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
3248 if (!connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3249 config = OFPUTIL_PC_NO_PACKET_IN;
3256 choose_miss_rule(config, ofproto->miss_rule,
3257 ofproto->no_packet_in_rule, rule, take_ref);
3261 /* The returned rule is valid at least until the next RCU quiescent period.
3262 * If the '*rule' needs to stay around longer, a non-zero 'take_ref' must be
3263 * passed in to cause a reference to be taken on it before this returns. */
3264 static struct rule_dpif *
3265 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3266 const struct flow *flow, struct flow_wildcards *wc,
3269 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3270 const struct cls_rule *cls_rule;
3271 struct rule_dpif *rule;
3273 fat_rwlock_rdlock(&cls->rwlock);
3274 if (ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3276 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3277 if (is_ip_any(flow)) {
3278 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3282 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3283 if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3284 /* We must pretend that transport ports are unavailable. */
3285 struct flow ofpc_normal_flow = *flow;
3286 ofpc_normal_flow.tp_src = htons(0);
3287 ofpc_normal_flow.tp_dst = htons(0);
3288 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3290 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM). */
3291 cls_rule = &ofproto->drop_frags_rule->up.cr;
3294 cls_rule = classifier_lookup(cls, flow, wc);
3297 cls_rule = classifier_lookup(cls, flow, wc);
3300 rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3302 rule_dpif_ref(rule);
3304 fat_rwlock_unlock(&cls->rwlock);
3309 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3310 * Stores the rule that was found in '*rule', or NULL if none was found.
3311 * Updates 'wc', if nonnull, to reflect the fields that were used during the
3314 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3315 * if none is found then the table miss configuration for that table is
3316 * honored, which can result in additional lookups in other OpenFlow tables.
3317 * In this case the function updates '*table_id' to reflect the final OpenFlow
3318 * table that was searched.
3320 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3325 * - RULE_DPIF_LOOKUP_VERDICT_MATCH if a rule (in '*rule') was found.
3327 * - RULE_OFPTC_TABLE_MISS_CONTROLLER if no rule was found and either:
3328 * + 'honor_table_miss' is false
3329 * + a table miss configuration specified that the packet should be
3330 * sent to the controller in this case.
3332 * - RULE_DPIF_LOOKUP_VERDICT_DROP if no rule was found, 'honor_table_miss'
3333 * is true and a table miss configuration specified that the packet
3334 * should be dropped in this case.
3336 * - RULE_DPIF_LOOKUP_VERDICT_DEFAULT if no rule was found,
3337 * 'honor_table_miss' is true and a table miss configuration has
3338 * not been specified in this case.
3340 * The rule is returned in '*rule', which is valid at least until the next
3341 * RCU quiescent period. If the '*rule' needs to stay around longer,
3342 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3343 * on it before this returns. */
3344 enum rule_dpif_lookup_verdict
3345 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3346 const struct flow *flow,
3347 struct flow_wildcards *wc,
3348 bool honor_table_miss,
3349 uint8_t *table_id, struct rule_dpif **rule,
3354 for (next_id = *table_id;
3355 next_id < ofproto->up.n_tables;
3356 next_id++, next_id += (next_id == TBL_INTERNAL))
3358 *table_id = next_id;
3359 *rule = rule_dpif_lookup_in_table(ofproto, *table_id, flow, wc,
3362 return RULE_DPIF_LOOKUP_VERDICT_MATCH;
3363 } else if (!honor_table_miss) {
3364 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3366 switch (ofproto_table_get_config(&ofproto->up, *table_id)) {
3367 case OFPROTO_TABLE_MISS_CONTINUE:
3370 case OFPROTO_TABLE_MISS_CONTROLLER:
3371 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3373 case OFPROTO_TABLE_MISS_DROP:
3374 return RULE_DPIF_LOOKUP_VERDICT_DROP;
3376 case OFPROTO_TABLE_MISS_DEFAULT:
3377 return RULE_DPIF_LOOKUP_VERDICT_DEFAULT;
3382 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3385 /* Given a port configuration (specified as zero if there's no port), chooses
3386 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3389 * The rule is returned in '*rule', which is valid at least until the next
3390 * RCU quiescent period. If the '*rule' needs to stay around longer,
3391 * a reference must be taken on it (rule_dpif_ref()).
3394 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3395 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule,
3398 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
3400 rule_dpif_ref(*rule);
3405 rule_dpif_ref(struct rule_dpif *rule)
3408 ofproto_rule_ref(&rule->up);
3413 rule_dpif_unref(struct rule_dpif *rule)
3416 ofproto_rule_unref(&rule->up);
3421 complete_operation(struct rule_dpif *rule)
3422 OVS_REQUIRES(ofproto_mutex)
3424 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3426 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3427 ofoperation_complete(rule->up.pending, 0);
3430 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3432 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3435 static struct rule *
3438 struct rule_dpif *rule = xmalloc(sizeof *rule);
3443 rule_dealloc(struct rule *rule_)
3445 struct rule_dpif *rule = rule_dpif_cast(rule_);
3450 rule_construct(struct rule *rule_)
3451 OVS_NO_THREAD_SAFETY_ANALYSIS
3453 struct rule_dpif *rule = rule_dpif_cast(rule_);
3454 ovs_mutex_init_adaptive(&rule->stats_mutex);
3455 rule->stats.n_packets = 0;
3456 rule->stats.n_bytes = 0;
3457 rule->stats.used = rule->up.modified;
3462 rule_insert(struct rule *rule_)
3463 OVS_REQUIRES(ofproto_mutex)
3465 struct rule_dpif *rule = rule_dpif_cast(rule_);
3466 complete_operation(rule);
3470 rule_delete(struct rule *rule_)
3471 OVS_REQUIRES(ofproto_mutex)
3473 struct rule_dpif *rule = rule_dpif_cast(rule_);
3474 complete_operation(rule);
3478 rule_destruct(struct rule *rule_)
3480 struct rule_dpif *rule = rule_dpif_cast(rule_);
3481 ovs_mutex_destroy(&rule->stats_mutex);
3485 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3486 long long int *used)
3488 struct rule_dpif *rule = rule_dpif_cast(rule_);
3490 ovs_mutex_lock(&rule->stats_mutex);
3491 *packets = rule->stats.n_packets;
3492 *bytes = rule->stats.n_bytes;
3493 *used = rule->stats.used;
3494 ovs_mutex_unlock(&rule->stats_mutex);
3498 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3499 struct ofpbuf *packet)
3501 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3503 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3507 rule_execute(struct rule *rule, const struct flow *flow,
3508 struct ofpbuf *packet)
3510 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3511 ofpbuf_delete(packet);
3516 rule_modify_actions(struct rule *rule_, bool reset_counters)
3517 OVS_REQUIRES(ofproto_mutex)
3519 struct rule_dpif *rule = rule_dpif_cast(rule_);
3521 if (reset_counters) {
3522 ovs_mutex_lock(&rule->stats_mutex);
3523 rule->stats.n_packets = 0;
3524 rule->stats.n_bytes = 0;
3525 ovs_mutex_unlock(&rule->stats_mutex);
3528 complete_operation(rule);
3531 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3533 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3536 static struct ofgroup *
3539 struct group_dpif *group = xzalloc(sizeof *group);
3544 group_dealloc(struct ofgroup *group_)
3546 struct group_dpif *group = group_dpif_cast(group_);
3551 group_construct_stats(struct group_dpif *group)
3552 OVS_REQUIRES(group->stats_mutex)
3554 group->packet_count = 0;
3555 group->byte_count = 0;
3556 if (!group->bucket_stats) {
3557 group->bucket_stats = xcalloc(group->up.n_buckets,
3558 sizeof *group->bucket_stats);
3560 memset(group->bucket_stats, 0, group->up.n_buckets *
3561 sizeof *group->bucket_stats);
3566 group_construct(struct ofgroup *group_)
3568 struct group_dpif *group = group_dpif_cast(group_);
3569 const struct ofputil_bucket *bucket;
3571 /* Prevent group chaining because our locking structure makes it hard to
3572 * implement deadlock-free. (See xlate_group_resource_check().) */
3573 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
3574 const struct ofpact *a;
3576 OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
3577 if (a->type == OFPACT_GROUP) {
3578 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
3583 ovs_mutex_init_adaptive(&group->stats_mutex);
3584 ovs_mutex_lock(&group->stats_mutex);
3585 group_construct_stats(group);
3586 ovs_mutex_unlock(&group->stats_mutex);
3591 group_destruct__(struct group_dpif *group)
3592 OVS_REQUIRES(group->stats_mutex)
3594 free(group->bucket_stats);
3595 group->bucket_stats = NULL;
3599 group_destruct(struct ofgroup *group_)
3601 struct group_dpif *group = group_dpif_cast(group_);
3602 ovs_mutex_lock(&group->stats_mutex);
3603 group_destruct__(group);
3604 ovs_mutex_unlock(&group->stats_mutex);
3605 ovs_mutex_destroy(&group->stats_mutex);
3609 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3611 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
3612 struct group_dpif *group = group_dpif_cast(group_);
3613 struct group_dpif *victim = group_dpif_cast(victim_);
3615 ovs_mutex_lock(&group->stats_mutex);
3616 if (victim->up.n_buckets < group->up.n_buckets) {
3617 group_destruct__(group);
3619 group_construct_stats(group);
3620 ovs_mutex_unlock(&group->stats_mutex);
3622 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3628 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3630 struct group_dpif *group = group_dpif_cast(group_);
3632 ovs_mutex_lock(&group->stats_mutex);
3633 ogs->packet_count = group->packet_count;
3634 ogs->byte_count = group->byte_count;
3635 memcpy(ogs->bucket_stats, group->bucket_stats,
3636 group->up.n_buckets * sizeof *group->bucket_stats);
3637 ovs_mutex_unlock(&group->stats_mutex);
3643 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3644 struct group_dpif **group)
3645 OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3647 struct ofgroup *ofgroup;
3651 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3652 *group = found ? group_dpif_cast(ofgroup) : NULL;
3658 group_dpif_release(struct group_dpif *group)
3659 OVS_RELEASES(group->up.rwlock)
3661 ofproto_group_release(&group->up);
3665 group_dpif_get_buckets(const struct group_dpif *group,
3666 const struct list **buckets)
3668 *buckets = &group->up.buckets;
3671 enum ofp11_group_type
3672 group_dpif_get_type(const struct group_dpif *group)
3674 return group->up.type;
3677 /* Sends 'packet' out 'ofport'.
3678 * May modify 'packet'.
3679 * Returns 0 if successful, otherwise a positive errno value. */
3681 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3683 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3686 error = xlate_send_packet(ofport, packet);
3688 ovs_mutex_lock(&ofproto->stats_mutex);
3689 ofproto->stats.tx_packets++;
3690 ofproto->stats.tx_bytes += ofpbuf_size(packet);
3691 ovs_mutex_unlock(&ofproto->stats_mutex);
3696 set_frag_handling(struct ofproto *ofproto_,
3697 enum ofp_config_flags frag_handling)
3699 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3700 if (frag_handling != OFPC_FRAG_REASM) {
3701 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3709 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3710 const struct flow *flow,
3711 const struct ofpact *ofpacts, size_t ofpacts_len)
3713 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3715 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3716 ofpacts_len, packet);
3723 set_netflow(struct ofproto *ofproto_,
3724 const struct netflow_options *netflow_options)
3726 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3728 if (netflow_options) {
3729 if (!ofproto->netflow) {
3730 ofproto->netflow = netflow_create();
3731 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3733 return netflow_set_options(ofproto->netflow, netflow_options);
3734 } else if (ofproto->netflow) {
3735 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3736 netflow_unref(ofproto->netflow);
3737 ofproto->netflow = NULL;
3744 get_netflow_ids(const struct ofproto *ofproto_,
3745 uint8_t *engine_type, uint8_t *engine_id)
3747 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3749 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3752 static struct ofproto_dpif *
3753 ofproto_dpif_lookup(const char *name)
3755 struct ofproto_dpif *ofproto;
3757 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3758 hash_string(name, 0), &all_ofproto_dpifs) {
3759 if (!strcmp(ofproto->up.name, name)) {
3767 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3768 const char *argv[], void *aux OVS_UNUSED)
3770 struct ofproto_dpif *ofproto;
3773 ofproto = ofproto_dpif_lookup(argv[1]);
3775 unixctl_command_reply_error(conn, "no such bridge");
3778 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3779 mac_learning_flush(ofproto->ml);
3780 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3782 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3783 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3784 mac_learning_flush(ofproto->ml);
3785 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3789 unixctl_command_reply(conn, "table successfully flushed");
3792 static struct ofport_dpif *
3793 ofbundle_get_a_port(const struct ofbundle *bundle)
3795 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3800 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3801 const char *argv[], void *aux OVS_UNUSED)
3803 struct ds ds = DS_EMPTY_INITIALIZER;
3804 const struct ofproto_dpif *ofproto;
3805 const struct mac_entry *e;
3807 ofproto = ofproto_dpif_lookup(argv[1]);
3809 unixctl_command_reply_error(conn, "no such bridge");
3813 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3814 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3815 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3816 struct ofbundle *bundle = e->port.p;
3817 char name[OFP_MAX_PORT_NAME_LEN];
3819 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3821 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
3822 name, e->vlan, ETH_ADDR_ARGS(e->mac),
3823 mac_entry_age(ofproto->ml, e));
3825 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3826 unixctl_command_reply(conn, ds_cstr(&ds));
3831 struct xlate_out xout;
3832 struct xlate_in xin;
3833 const struct flow *key;
3835 struct flow_wildcards wc;
3840 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
3842 struct rule_actions *actions;
3845 ds_put_char_multiple(result, '\t', level);
3847 ds_put_cstr(result, "No match\n");
3851 ovs_mutex_lock(&rule->up.mutex);
3852 cookie = rule->up.flow_cookie;
3853 ovs_mutex_unlock(&rule->up.mutex);
3855 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3856 rule ? rule->up.table_id : 0, ntohll(cookie));
3857 cls_rule_format(&rule->up.cr, result);
3858 ds_put_char(result, '\n');
3860 actions = rule_dpif_get_actions(rule);
3862 ds_put_char_multiple(result, '\t', level);
3863 ds_put_cstr(result, "OpenFlow actions=");
3864 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
3865 ds_put_char(result, '\n');
3869 trace_format_flow(struct ds *result, int level, const char *title,
3870 struct trace_ctx *trace)
3872 ds_put_char_multiple(result, '\t', level);
3873 ds_put_format(result, "%s: ", title);
3874 /* Do not report unchanged flows for resubmits. */
3875 if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
3876 || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
3877 ds_put_cstr(result, "unchanged");
3879 flow_format(result, &trace->xin.flow);
3880 trace->flow = trace->xin.flow;
3882 ds_put_char(result, '\n');
3886 trace_format_regs(struct ds *result, int level, const char *title,
3887 struct trace_ctx *trace)
3891 ds_put_char_multiple(result, '\t', level);
3892 ds_put_format(result, "%s:", title);
3893 for (i = 0; i < FLOW_N_REGS; i++) {
3894 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
3896 ds_put_char(result, '\n');
3900 trace_format_odp(struct ds *result, int level, const char *title,
3901 struct trace_ctx *trace)
3903 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
3905 ds_put_char_multiple(result, '\t', level);
3906 ds_put_format(result, "%s: ", title);
3907 format_odp_actions(result, ofpbuf_data(odp_actions),
3908 ofpbuf_size(odp_actions));
3909 ds_put_char(result, '\n');
3913 trace_format_megaflow(struct ds *result, int level, const char *title,
3914 struct trace_ctx *trace)
3918 ds_put_char_multiple(result, '\t', level);
3919 ds_put_format(result, "%s: ", title);
3920 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
3921 match_init(&match, trace->key, &trace->wc);
3922 match_format(&match, result, OFP_DEFAULT_PRIORITY);
3923 ds_put_char(result, '\n');
3927 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3929 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3930 struct ds *result = trace->result;
3932 ds_put_char(result, '\n');
3933 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3934 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3935 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
3936 trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
3937 trace_format_rule(result, recurse + 1, rule);
3941 trace_report(struct xlate_in *xin, const char *s, int recurse)
3943 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3944 struct ds *result = trace->result;
3946 ds_put_char_multiple(result, '\t', recurse);
3947 ds_put_cstr(result, s);
3948 ds_put_char(result, '\n');
3951 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
3952 * forms are supported:
3954 * - [dpname] odp_flow [-generate | packet]
3955 * - bridge br_flow [-generate | packet]
3957 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
3958 * returns a nonnull malloced error message. */
3959 static char * WARN_UNUSED_RESULT
3960 parse_flow_and_packet(int argc, const char *argv[],
3961 struct ofproto_dpif **ofprotop, struct flow *flow,
3962 struct ofpbuf **packetp)
3964 const struct dpif_backer *backer = NULL;
3965 const char *error = NULL;
3967 struct simap port_names = SIMAP_INITIALIZER(&port_names);
3968 struct ofpbuf *packet;
3969 struct ofpbuf odp_key;
3970 struct ofpbuf odp_mask;
3972 ofpbuf_init(&odp_key, 0);
3973 ofpbuf_init(&odp_mask, 0);
3975 /* Handle "-generate" or a hex string as the last argument. */
3976 if (!strcmp(argv[argc - 1], "-generate")) {
3977 packet = ofpbuf_new(0);
3980 error = eth_from_hex(argv[argc - 1], &packet);
3983 } else if (argc == 4) {
3984 /* The 3-argument form must end in "-generate' or a hex string. */
3990 /* odp_flow can have its in_port specified as a name instead of port no.
3991 * We do not yet know whether a given flow is a odp_flow or a br_flow.
3992 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3993 * we need to create a simap of name to port no. */
3995 const char *dp_type;
3996 if (!strncmp(argv[1], "ovs-", 4)) {
3997 dp_type = argv[1] + 4;
4001 backer = shash_find_data(&all_dpif_backers, dp_type);
4002 } else if (argc == 2) {
4003 struct shash_node *node;
4004 if (shash_count(&all_dpif_backers) == 1) {
4005 node = shash_first(&all_dpif_backers);
4006 backer = node->data;
4009 error = "Syntax error";
4012 if (backer && backer->dpif) {
4013 struct dpif_port dpif_port;
4014 struct dpif_port_dump port_dump;
4015 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4016 simap_put(&port_names, dpif_port.name,
4017 odp_to_u32(dpif_port.port_no));
4021 /* Parse the flow and determine whether a datapath or
4022 * bridge is specified. If function odp_flow_key_from_string()
4023 * returns 0, the flow is a odp_flow. If function
4024 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4025 if (!odp_flow_from_string(argv[argc - 1], &port_names,
4026 &odp_key, &odp_mask)) {
4028 error = "Cannot find the datapath";
4032 if (xlate_receive(backer, NULL, ofpbuf_data(&odp_key),
4033 ofpbuf_size(&odp_key), flow,
4034 ofprotop, NULL, NULL, NULL, NULL)) {
4035 error = "Invalid datapath flow";
4039 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4042 m_err = xasprintf("Bad flow syntax: %s", err);
4047 error = "Must specify bridge name";
4051 *ofprotop = ofproto_dpif_lookup(argv[1]);
4053 error = "Unknown bridge name";
4059 /* Generate a packet, if requested. */
4061 if (!ofpbuf_size(packet)) {
4062 flow_compose(packet, flow);
4064 struct pkt_metadata md = pkt_metadata_from_flow(flow);
4066 /* Use the metadata from the flow and the packet argument
4067 * to reconstruct the flow. */
4068 flow_extract(packet, &md, flow);
4073 if (error && !m_err) {
4074 m_err = xstrdup(error);
4077 ofpbuf_delete(packet);
4081 ofpbuf_uninit(&odp_key);
4082 ofpbuf_uninit(&odp_mask);
4083 simap_destroy(&port_names);
4088 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4089 void *aux OVS_UNUSED)
4091 struct ofproto_dpif *ofproto;
4092 struct ofpbuf *packet;
4096 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4101 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4102 unixctl_command_reply(conn, ds_cstr(&result));
4103 ds_destroy(&result);
4104 ofpbuf_delete(packet);
4106 unixctl_command_reply_error(conn, error);
4112 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4113 const char *argv[], void *aux OVS_UNUSED)
4115 enum ofputil_protocol usable_protocols;
4116 struct ofproto_dpif *ofproto;
4117 bool enforce_consistency;
4118 struct ofpbuf ofpacts;
4119 struct ofpbuf *packet;
4124 /* Three kinds of error return values! */
4130 ofpbuf_init(&ofpacts, 0);
4132 /* Parse actions. */
4133 error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
4135 unixctl_command_reply_error(conn, error);
4140 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4141 * consistency between the flow and the actions. With -consistent, we
4142 * enforce consistency even for a flow supported in OpenFlow 1.0. */
4143 if (!strcmp(argv[1], "-consistent")) {
4144 enforce_consistency = true;
4148 enforce_consistency = false;
4151 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4153 unixctl_command_reply_error(conn, error);
4158 /* Do the same checks as handle_packet_out() in ofproto.c.
4160 * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
4161 * strictly correct because these actions aren't in any table, but it's OK
4162 * because it 'table_id' is used only to check goto_table instructions, but
4163 * packet-outs take a list of actions and therefore it can't include
4166 * We skip the "meter" check here because meter is an instruction, not an
4167 * action, and thus cannot appear in ofpacts. */
4168 in_port = ofp_to_u16(flow.in_port.ofp_port);
4169 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
4170 unixctl_command_reply_error(conn, "invalid in_port");
4173 if (enforce_consistency) {
4174 retval = ofpacts_check_consistency(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
4175 &flow, u16_to_ofp(ofproto->up.max_ports),
4176 0, 0, usable_protocols);
4178 retval = ofpacts_check(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &flow,
4179 u16_to_ofp(ofproto->up.max_ports), 0, 0,
4185 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4186 unixctl_command_reply_error(conn, ds_cstr(&result));
4190 ofproto_trace(ofproto, &flow, packet,
4191 ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &result);
4192 unixctl_command_reply(conn, ds_cstr(&result));
4195 ds_destroy(&result);
4196 ofpbuf_delete(packet);
4197 ofpbuf_uninit(&ofpacts);
4200 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4201 * description of the results to 'ds'.
4203 * The trace follows a packet with the specified 'flow' through the flow
4204 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4205 * side effects (if it is nonnull then its flow must be 'flow').
4207 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4208 * trace, otherwise the actions are determined by a flow table lookup. */
4210 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4211 const struct ofpbuf *packet,
4212 const struct ofpact ofpacts[], size_t ofpacts_len,
4215 struct rule_dpif *rule;
4216 struct trace_ctx trace;
4218 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4219 ds_put_cstr(ds, "Flow: ");
4220 flow_format(ds, flow);
4221 ds_put_char(ds, '\n');
4223 flow_wildcards_init_catchall(&trace.wc);
4227 rule_dpif_lookup(ofproto, flow, &trace.wc, &rule, false);
4229 trace_format_rule(ds, 0, rule);
4230 if (rule == ofproto->miss_rule) {
4231 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
4232 } else if (rule == ofproto->no_packet_in_rule) {
4233 ds_put_cstr(ds, "\nNo match, packets dropped because "
4234 "OFPPC_NO_PACKET_IN is set on in_port.\n");
4235 } else if (rule == ofproto->drop_frags_rule) {
4236 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
4237 "and the fragment handling mode is \"drop\".\n");
4241 if (rule || ofpacts) {
4243 trace.key = flow; /* Original flow key, used for megaflow. */
4244 trace.flow = *flow; /* May be modified by actions. */
4245 xlate_in_init(&trace.xin, ofproto, flow, rule, ntohs(flow->tcp_flags),
4248 trace.xin.ofpacts = ofpacts;
4249 trace.xin.ofpacts_len = ofpacts_len;
4251 trace.xin.resubmit_hook = trace_resubmit;
4252 trace.xin.report_hook = trace_report;
4254 xlate_actions(&trace.xin, &trace.xout);
4256 ds_put_char(ds, '\n');
4257 trace_format_flow(ds, 0, "Final flow", &trace);
4258 trace_format_megaflow(ds, 0, "Megaflow", &trace);
4260 ds_put_cstr(ds, "Datapath actions: ");
4261 format_odp_actions(ds, ofpbuf_data(&trace.xout.odp_actions),
4262 ofpbuf_size(&trace.xout.odp_actions));
4264 if (trace.xout.slow) {
4265 enum slow_path_reason slow;
4267 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4268 "slow path because it:");
4270 slow = trace.xout.slow;
4272 enum slow_path_reason bit = rightmost_1bit(slow);
4274 ds_put_format(ds, "\n\t- %s.",
4275 slow_path_reason_to_explanation(bit));
4281 xlate_out_uninit(&trace.xout);
4285 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4286 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4287 * to destroy 'ofproto_shash' and free the returned value. */
4288 static const struct shash_node **
4289 get_ofprotos(struct shash *ofproto_shash)
4291 const struct ofproto_dpif *ofproto;
4293 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4294 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4295 shash_add_nocopy(ofproto_shash, name, ofproto);
4298 return shash_sort(ofproto_shash);
4302 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4303 const char *argv[] OVS_UNUSED,
4304 void *aux OVS_UNUSED)
4306 struct ds ds = DS_EMPTY_INITIALIZER;
4307 struct shash ofproto_shash;
4308 const struct shash_node **sorted_ofprotos;
4311 shash_init(&ofproto_shash);
4312 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4313 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4314 const struct shash_node *node = sorted_ofprotos[i];
4315 ds_put_format(&ds, "%s\n", node->name);
4318 shash_destroy(&ofproto_shash);
4319 free(sorted_ofprotos);
4321 unixctl_command_reply(conn, ds_cstr(&ds));
4326 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4328 const struct shash_node **ofprotos;
4329 struct dpif_dp_stats dp_stats;
4330 struct shash ofproto_shash;
4333 dpif_get_dp_stats(backer->dpif, &dp_stats);
4335 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4336 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4338 shash_init(&ofproto_shash);
4339 ofprotos = get_ofprotos(&ofproto_shash);
4340 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4341 struct ofproto_dpif *ofproto = ofprotos[i]->data;
4342 const struct shash_node **ports;
4345 if (ofproto->backer != backer) {
4349 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4351 ports = shash_sort(&ofproto->up.port_by_name);
4352 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4353 const struct shash_node *node = ports[j];
4354 struct ofport *ofport = node->data;
4356 odp_port_t odp_port;
4358 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4361 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4362 if (odp_port != ODPP_NONE) {
4363 ds_put_format(ds, "%"PRIu32":", odp_port);
4365 ds_put_cstr(ds, "none:");
4368 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
4371 if (!netdev_get_config(ofport->netdev, &config)) {
4372 const struct smap_node **nodes;
4375 nodes = smap_sort(&config);
4376 for (i = 0; i < smap_count(&config); i++) {
4377 const struct smap_node *node = nodes[i];
4378 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4379 node->key, node->value);
4383 smap_destroy(&config);
4385 ds_put_char(ds, ')');
4386 ds_put_char(ds, '\n');
4390 shash_destroy(&ofproto_shash);
4395 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4396 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4398 struct ds ds = DS_EMPTY_INITIALIZER;
4399 const struct shash_node **backers;
4402 backers = shash_sort(&all_dpif_backers);
4403 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4404 dpif_show_backer(backers[i]->data, &ds);
4408 unixctl_command_reply(conn, ds_cstr(&ds));
4413 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4414 const struct nlattr *key, size_t key_len)
4416 struct ofproto_dpif *ofp;
4419 xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &ofp,
4420 NULL, NULL, NULL, NULL);
4421 return ofp == ofproto;
4425 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4426 int argc OVS_UNUSED, const char *argv[],
4427 void *aux OVS_UNUSED)
4429 struct ds ds = DS_EMPTY_INITIALIZER;
4430 const struct dpif_flow_stats *stats;
4431 const struct ofproto_dpif *ofproto;
4432 struct dpif_flow_dump flow_dump;
4433 const struct nlattr *actions;
4434 const struct nlattr *mask;
4435 const struct nlattr *key;
4439 bool verbosity = false;
4440 struct dpif_port dpif_port;
4441 struct dpif_port_dump port_dump;
4442 struct hmap portno_names;
4446 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4448 unixctl_command_reply_error(conn, "no such bridge");
4452 if (argc > 2 && !strcmp(argv[1], "-m")) {
4456 hmap_init(&portno_names);
4457 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4458 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4462 error = dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4466 dpif_flow_dump_state_init(ofproto->backer->dpif, &state);
4467 while (dpif_flow_dump_next(&flow_dump, state, &key, &key_len,
4468 &mask, &mask_len, &actions, &actions_len,
4470 if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
4474 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4476 ds_put_cstr(&ds, ", ");
4477 dpif_flow_stats_format(stats, &ds);
4478 ds_put_cstr(&ds, ", actions:");
4479 format_odp_actions(&ds, actions, actions_len);
4480 ds_put_char(&ds, '\n');
4482 dpif_flow_dump_state_uninit(ofproto->backer->dpif, state);
4483 error = dpif_flow_dump_done(&flow_dump);
4488 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4489 unixctl_command_reply_error(conn, ds_cstr(&ds));
4491 unixctl_command_reply(conn, ds_cstr(&ds));
4493 odp_portno_names_destroy(&portno_names);
4494 hmap_destroy(&portno_names);
4499 ofproto_dpif_unixctl_init(void)
4501 static bool registered;
4507 unixctl_command_register(
4509 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4510 1, 3, ofproto_unixctl_trace, NULL);
4511 unixctl_command_register(
4512 "ofproto/trace-packet-out",
4513 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4514 2, 6, ofproto_unixctl_trace_actions, NULL);
4515 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4516 ofproto_unixctl_fdb_flush, NULL);
4517 unixctl_command_register("fdb/show", "bridge", 1, 1,
4518 ofproto_unixctl_fdb_show, NULL);
4519 unixctl_command_register("dpif/dump-dps", "", 0, 0,
4520 ofproto_unixctl_dpif_dump_dps, NULL);
4521 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4523 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4524 ofproto_unixctl_dpif_dump_flows, NULL);
4528 /* Returns true if 'rule' is an internal rule, false otherwise. */
4530 rule_is_internal(const struct rule *rule)
4532 return rule->table_id == TBL_INTERNAL;
4535 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4537 * This is deprecated. It is only for compatibility with broken device drivers
4538 * in old versions of Linux that do not properly support VLANs when VLAN
4539 * devices are not used. When broken device drivers are no longer in
4540 * widespread use, we will delete these interfaces. */
4543 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4545 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4546 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4548 if (realdev_ofp_port == ofport->realdev_ofp_port
4549 && vid == ofport->vlandev_vid) {
4553 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4555 if (ofport->realdev_ofp_port) {
4558 if (realdev_ofp_port && ofport->bundle) {
4559 /* vlandevs are enslaved to their realdevs, so they are not allowed to
4560 * themselves be part of a bundle. */
4561 bundle_set(ofport_->ofproto, ofport->bundle, NULL);
4564 ofport->realdev_ofp_port = realdev_ofp_port;
4565 ofport->vlandev_vid = vid;
4567 if (realdev_ofp_port) {
4568 vsp_add(ofport, realdev_ofp_port, vid);
4575 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4577 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4581 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4582 OVS_EXCLUDED(ofproto->vsp_mutex)
4584 /* hmap_is_empty is thread safe. */
4585 return !hmap_is_empty(&ofproto->realdev_vid_map);
4590 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4591 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4592 OVS_REQUIRES(ofproto->vsp_mutex)
4594 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4595 int vid = vlan_tci_to_vid(vlan_tci);
4596 const struct vlan_splinter *vsp;
4598 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4599 hash_realdev_vid(realdev_ofp_port, vid),
4600 &ofproto->realdev_vid_map) {
4601 if (vsp->realdev_ofp_port == realdev_ofp_port
4602 && vsp->vid == vid) {
4603 return vsp->vlandev_ofp_port;
4607 return realdev_ofp_port;
4610 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4611 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4612 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
4613 * 'vlan_tci' 9, it would return the port number of eth0.9.
4615 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4616 * function just returns its 'realdev_ofp_port' argument. */
4618 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4619 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4620 OVS_EXCLUDED(ofproto->vsp_mutex)
4624 /* hmap_is_empty is thread safe, see if we can return immediately. */
4625 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
4626 return realdev_ofp_port;
4628 ovs_mutex_lock(&ofproto->vsp_mutex);
4629 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4630 ovs_mutex_unlock(&ofproto->vsp_mutex);
4634 static struct vlan_splinter *
4635 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4637 struct vlan_splinter *vsp;
4639 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4640 hash_ofp_port(vlandev_ofp_port),
4641 &ofproto->vlandev_map) {
4642 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4650 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4651 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4652 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
4653 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4654 * eth0 and store 9 in '*vid'.
4656 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4657 * VLAN device. Unless VLAN splinters are enabled, this is what this function
4660 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4661 ofp_port_t vlandev_ofp_port, int *vid)
4662 OVS_REQUIRES(ofproto->vsp_mutex)
4664 if (!hmap_is_empty(&ofproto->vlandev_map)) {
4665 const struct vlan_splinter *vsp;
4667 vsp = vlandev_find(ofproto, vlandev_ofp_port);
4672 return vsp->realdev_ofp_port;
4678 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4679 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
4680 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4681 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
4682 * always the case unless VLAN splinters are enabled), returns false without
4683 * making any changes. */
4685 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4686 OVS_EXCLUDED(ofproto->vsp_mutex)
4691 /* hmap_is_empty is thread safe. */
4692 if (hmap_is_empty(&ofproto->vlandev_map)) {
4696 ovs_mutex_lock(&ofproto->vsp_mutex);
4697 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4698 ovs_mutex_unlock(&ofproto->vsp_mutex);
4703 /* Cause the flow to be processed as if it came in on the real device with
4704 * the VLAN device's VLAN ID. */
4705 flow->in_port.ofp_port = realdev;
4706 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4711 vsp_remove(struct ofport_dpif *port)
4713 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4714 struct vlan_splinter *vsp;
4716 ovs_mutex_lock(&ofproto->vsp_mutex);
4717 vsp = vlandev_find(ofproto, port->up.ofp_port);
4719 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4720 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4723 port->realdev_ofp_port = 0;
4725 VLOG_ERR("missing vlan device record");
4727 ovs_mutex_unlock(&ofproto->vsp_mutex);
4731 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4733 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4735 ovs_mutex_lock(&ofproto->vsp_mutex);
4736 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4737 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4738 == realdev_ofp_port)) {
4739 struct vlan_splinter *vsp;
4741 vsp = xmalloc(sizeof *vsp);
4742 vsp->realdev_ofp_port = realdev_ofp_port;
4743 vsp->vlandev_ofp_port = port->up.ofp_port;
4746 port->realdev_ofp_port = realdev_ofp_port;
4748 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4749 hash_ofp_port(port->up.ofp_port));
4750 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4751 hash_realdev_vid(realdev_ofp_port, vid));
4753 VLOG_ERR("duplicate vlan device record");
4755 ovs_mutex_unlock(&ofproto->vsp_mutex);
4759 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4761 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4762 return ofport ? ofport->odp_port : ODPP_NONE;
4765 struct ofport_dpif *
4766 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4768 struct ofport_dpif *port;
4770 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4771 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4772 &backer->odp_to_ofport_map) {
4773 if (port->odp_port == odp_port) {
4774 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4779 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4784 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4786 struct ofport_dpif *port;
4788 port = odp_port_to_ofport(ofproto->backer, odp_port);
4789 if (port && &ofproto->up == port->up.ofproto) {
4790 return port->up.ofp_port;
4797 ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto)
4799 struct dpif_backer *backer = ofproto->backer;
4801 return recirc_id_alloc(backer->rid_pool);
4805 ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id)
4807 struct dpif_backer *backer = ofproto->backer;
4809 recirc_id_free(backer->rid_pool, recirc_id);
4813 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
4814 struct match *match, int priority,
4815 const struct ofpbuf *ofpacts,
4816 struct rule **rulep)
4818 struct ofputil_flow_mod fm;
4819 struct rule_dpif *rule;
4823 fm.priority = priority;
4824 fm.new_cookie = htonll(0);
4825 fm.cookie = htonll(0);
4826 fm.cookie_mask = htonll(0);
4827 fm.modify_cookie = false;
4828 fm.table_id = TBL_INTERNAL;
4829 fm.command = OFPFC_ADD;
4830 fm.idle_timeout = 0;
4831 fm.hard_timeout = 0;
4834 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
4835 fm.ofpacts = ofpbuf_data(ofpacts);
4836 fm.ofpacts_len = ofpbuf_size(ofpacts);
4838 error = ofproto_flow_mod(&ofproto->up, &fm);
4840 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
4841 ofperr_to_string(error));
4846 rule = rule_dpif_lookup_in_table(ofproto, TBL_INTERNAL, &match->flow,
4857 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
4858 struct match *match, int priority)
4860 struct ofputil_flow_mod fm;
4864 fm.priority = priority;
4865 fm.new_cookie = htonll(0);
4866 fm.cookie = htonll(0);
4867 fm.cookie_mask = htonll(0);
4868 fm.modify_cookie = false;
4869 fm.table_id = TBL_INTERNAL;
4870 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
4871 fm.command = OFPFC_DELETE_STRICT;
4873 error = ofproto_flow_mod(&ofproto->up, &fm);
4875 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
4876 ofperr_to_string(error));
4883 const struct ofproto_class ofproto_dpif_class = {
4897 NULL, /* get_memory_usage. */
4898 type_get_memory_usage,
4917 port_is_lacp_current,
4918 NULL, /* rule_choose_table */
4927 rule_modify_actions,
4941 get_stp_port_status,
4949 is_mirror_output_bundle,
4950 forward_bpdu_changed,
4951 set_mac_table_config,
4953 NULL, /* meter_get_features */
4954 NULL, /* meter_set */
4955 NULL, /* meter_get */
4956 NULL, /* meter_del */
4957 group_alloc, /* group_alloc */
4958 group_construct, /* group_construct */
4959 group_destruct, /* group_destruct */
4960 group_dealloc, /* group_dealloc */
4961 group_modify, /* group_modify */
4962 group_get_stats, /* group_get_stats */