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"
61 #include "poll-loop.h"
67 #include "unaligned.h"
69 #include "vlan-bitmap.h"
72 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
74 COVERAGE_DEFINE(ofproto_dpif_expired);
75 COVERAGE_DEFINE(packet_in_overflow);
77 /* No bfd/cfm status change. */
78 #define NO_STATUS_CHANGE -1
87 * - Do include packets and bytes from datapath flows which have not
88 * recently been processed by a revalidator. */
89 struct ovs_mutex stats_mutex;
90 struct dpif_flow_stats stats OVS_GUARDED;
93 /* RULE_CAST() depends on this. */
94 BUILD_ASSERT_DECL(offsetof(struct rule_dpif, up) == 0);
96 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes,
98 static struct rule_dpif *rule_dpif_cast(const struct rule *);
99 static void rule_expire(struct rule_dpif *);
106 * - Do include packets and bytes from datapath flows which have not
107 * recently been processed by a revalidator. */
108 struct ovs_mutex stats_mutex;
109 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
110 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
111 struct bucket_counter *bucket_stats OVS_GUARDED; /* Bucket statistics. */
115 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
116 struct ofproto_dpif *ofproto; /* Owning ofproto. */
117 void *aux; /* Key supplied by ofproto's client. */
118 char *name; /* Identifier for log messages. */
121 struct list ports; /* Contains "struct ofport"s. */
122 enum port_vlan_mode vlan_mode; /* VLAN mode */
123 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
124 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
125 * NULL if all VLANs are trunked. */
126 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
127 struct bond *bond; /* Nonnull iff more than one port. */
128 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
131 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
134 static void bundle_remove(struct ofport *);
135 static void bundle_update(struct ofbundle *);
136 static void bundle_destroy(struct ofbundle *);
137 static void bundle_del_port(struct ofport_dpif *);
138 static void bundle_run(struct ofbundle *);
139 static void bundle_wait(struct ofbundle *);
141 static void stp_run(struct ofproto_dpif *ofproto);
142 static void stp_wait(struct ofproto_dpif *ofproto);
143 static int set_stp_port(struct ofport *,
144 const struct ofproto_port_stp_settings *);
147 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
151 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
152 struct list bundle_node; /* In struct ofbundle's "ports" list. */
153 struct cfm *cfm; /* Connectivity Fault Management, if any. */
154 struct bfd *bfd; /* BFD, if any. */
155 bool may_enable; /* May be enabled in bonds. */
156 bool is_tunnel; /* This port is a tunnel. */
157 bool is_layer3; /* This is a layer 3 port. */
158 long long int carrier_seq; /* Carrier status changes. */
159 struct ofport_dpif *peer; /* Peer if patch port. */
162 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
163 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
164 long long int stp_state_entered;
166 /* Queue to DSCP mapping. */
167 struct ofproto_port_queue *qdscp;
170 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
172 * This is deprecated. It is only for compatibility with broken device
173 * drivers in old versions of Linux that do not properly support VLANs when
174 * VLAN devices are not used. When broken device drivers are no longer in
175 * widespread use, we will delete these interfaces. */
176 ofp_port_t realdev_ofp_port;
180 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
182 * This is deprecated. It is only for compatibility with broken device drivers
183 * in old versions of Linux that do not properly support VLANs when VLAN
184 * devices are not used. When broken device drivers are no longer in
185 * widespread use, we will delete these interfaces. */
186 struct vlan_splinter {
187 struct hmap_node realdev_vid_node;
188 struct hmap_node vlandev_node;
189 ofp_port_t realdev_ofp_port;
190 ofp_port_t vlandev_ofp_port;
194 static void vsp_remove(struct ofport_dpif *);
195 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
197 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
200 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
203 static struct ofport_dpif *
204 ofport_dpif_cast(const struct ofport *ofport)
206 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
209 static void port_run(struct ofport_dpif *);
210 static int set_bfd(struct ofport *, const struct smap *);
211 static int set_cfm(struct ofport *, const struct cfm_settings *);
212 static void ofport_update_peer(struct ofport_dpif *);
214 struct dpif_completion {
215 struct list list_node;
216 struct ofoperation *op;
219 /* Reasons that we might need to revalidate every datapath flow, and
220 * corresponding coverage counters.
222 * A value of 0 means that there is no need to revalidate.
224 * It would be nice to have some cleaner way to integrate with coverage
225 * counters, but with only a few reasons I guess this is good enough for
227 enum revalidate_reason {
228 REV_RECONFIGURE = 1, /* Switch configuration changed. */
229 REV_STP, /* Spanning tree protocol port status change. */
230 REV_BOND, /* Bonding changed. */
231 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
232 REV_FLOW_TABLE, /* Flow table changed. */
233 REV_MAC_LEARNING, /* Mac learning changed. */
235 COVERAGE_DEFINE(rev_reconfigure);
236 COVERAGE_DEFINE(rev_stp);
237 COVERAGE_DEFINE(rev_bond);
238 COVERAGE_DEFINE(rev_port_toggled);
239 COVERAGE_DEFINE(rev_flow_table);
240 COVERAGE_DEFINE(rev_mac_learning);
242 /* Stores mapping between 'recirc_id' and 'ofproto-dpif'. */
243 struct dpif_backer_recirc_node {
244 struct hmap_node hmap_node;
245 struct ofproto_dpif *ofproto;
249 /* All datapaths of a given type share a single dpif backer instance. */
256 struct ovs_rwlock odp_to_ofport_lock;
257 struct hmap odp_to_ofport_map OVS_GUARDED; /* Contains "struct ofport"s. */
259 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
261 enum revalidate_reason need_revalidate; /* Revalidate all flows. */
263 bool recv_set_enable; /* Enables or disables receiving packets. */
266 struct recirc_id_pool *rid_pool; /* Recirculation ID pool. */
267 struct hmap recirc_map; /* Map of 'recirc_id's to 'ofproto's. */
268 struct ovs_mutex recirc_mutex; /* Protects 'recirc_map'. */
269 bool enable_recirc; /* True if the datapath supports recirculation */
271 /* True if the datapath supports variable-length
272 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions.
273 * False if the datapath supports only 8-byte (or shorter) userdata. */
274 bool variable_length_userdata;
276 /* Maximum number of MPLS label stack entries that the datapath supports
278 size_t max_mpls_depth;
281 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
282 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
284 struct ofproto_dpif {
285 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
287 struct dpif_backer *backer;
289 uint64_t dump_seq; /* Last read of udpif_dump_seq(). */
291 /* Special OpenFlow rules. */
292 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
293 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
294 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
297 struct netflow *netflow;
298 struct dpif_sflow *sflow;
299 struct dpif_ipfix *ipfix;
300 struct hmap bundles; /* Contains "struct ofbundle"s. */
301 struct mac_learning *ml;
302 bool has_bonded_bundles;
304 struct mbridge *mbridge;
306 struct ovs_mutex stats_mutex;
307 struct netdev_stats stats OVS_GUARDED; /* To account packets generated and
308 * consumed in userspace. */
312 long long int stp_last_tick;
314 /* VLAN splinters. */
315 struct ovs_mutex vsp_mutex;
316 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
317 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
320 struct sset ports; /* Set of standard port names. */
321 struct sset ghost_ports; /* Ports with no datapath port. */
322 struct sset port_poll_set; /* Queued names for port_poll() reply. */
323 int port_poll_errno; /* Last errno for port_poll() reply. */
324 uint64_t change_seq; /* Connectivity status changes. */
327 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
328 struct seq *pins_seq; /* For notifying 'pins' reception. */
332 /* All existing ofproto_dpif instances, indexed by ->up.name. */
333 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
335 static void ofproto_dpif_unixctl_init(void);
337 static inline struct ofproto_dpif *
338 ofproto_dpif_cast(const struct ofproto *ofproto)
340 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
341 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
345 ofproto_dpif_get_max_mpls_depth(const struct ofproto_dpif *ofproto)
347 return ofproto->backer->max_mpls_depth;
351 ofproto_dpif_get_enable_recirc(const struct ofproto_dpif *ofproto)
353 return ofproto->backer->enable_recirc;
356 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
357 ofp_port_t ofp_port);
358 static void ofproto_trace(struct ofproto_dpif *, struct flow *,
359 const struct ofpbuf *packet,
360 const struct ofpact[], size_t ofpacts_len,
363 /* Global variables. */
364 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
366 /* Initial mappings of port to bridge mappings. */
367 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
369 /* Executes 'fm'. The caller retains ownership of 'fm' and everything in
372 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
373 struct ofputil_flow_mod *fm)
375 ofproto_flow_mod(&ofproto->up, fm);
378 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
379 * Takes ownership of 'pin' and pin->packet. */
381 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
382 struct ofproto_packet_in *pin)
384 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
385 COVERAGE_INC(packet_in_overflow);
386 free(CONST_CAST(void *, pin->up.packet));
390 /* Wakes up main thread for packet-in I/O. */
391 seq_change(ofproto->pins_seq);
394 /* The default "table-miss" behaviour for OpenFlow1.3+ is to drop the
395 * packet rather than to send the packet to the controller.
397 * This function returns false to indicate that a packet_in message
398 * for a "table-miss" should be sent to at least one controller.
399 * False otherwise. */
401 ofproto_dpif_wants_packet_in_on_miss(struct ofproto_dpif *ofproto)
403 return connmgr_wants_packet_in_on_miss(ofproto->up.connmgr);
406 /* Factory functions. */
409 init(const struct shash *iface_hints)
411 struct shash_node *node;
413 /* Make a local copy, since we don't own 'iface_hints' elements. */
414 SHASH_FOR_EACH(node, iface_hints) {
415 const struct iface_hint *orig_hint = node->data;
416 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
418 new_hint->br_name = xstrdup(orig_hint->br_name);
419 new_hint->br_type = xstrdup(orig_hint->br_type);
420 new_hint->ofp_port = orig_hint->ofp_port;
422 shash_add(&init_ofp_ports, node->name, new_hint);
427 enumerate_types(struct sset *types)
429 dp_enumerate_types(types);
433 enumerate_names(const char *type, struct sset *names)
435 struct ofproto_dpif *ofproto;
438 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
439 if (strcmp(type, ofproto->up.type)) {
442 sset_add(names, ofproto->up.name);
449 del(const char *type, const char *name)
454 error = dpif_open(name, type, &dpif);
456 error = dpif_delete(dpif);
463 port_open_type(const char *datapath_type, const char *port_type)
465 return dpif_port_open_type(datapath_type, port_type);
468 /* Type functions. */
470 static void process_dpif_port_changes(struct dpif_backer *);
471 static void process_dpif_all_ports_changed(struct dpif_backer *);
472 static void process_dpif_port_change(struct dpif_backer *,
473 const char *devname);
474 static void process_dpif_port_error(struct dpif_backer *, int error);
476 static struct ofproto_dpif *
477 lookup_ofproto_dpif_by_port_name(const char *name)
479 struct ofproto_dpif *ofproto;
481 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
482 if (sset_contains(&ofproto->ports, name)) {
491 type_run(const char *type)
493 struct dpif_backer *backer;
495 backer = shash_find_data(&all_dpif_backers, type);
497 /* This is not necessarily a problem, since backers are only
498 * created on demand. */
502 dpif_run(backer->dpif);
504 /* If vswitchd started with other_config:flow_restore_wait set as "true",
505 * and the configuration has now changed to "false", enable receiving
506 * packets from the datapath. */
507 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
510 backer->recv_set_enable = true;
512 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
514 VLOG_ERR("Failed to enable receiving packets in dpif.");
517 dpif_flow_flush(backer->dpif);
518 backer->need_revalidate = REV_RECONFIGURE;
521 if (backer->recv_set_enable) {
522 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
525 if (backer->need_revalidate) {
526 struct ofproto_dpif *ofproto;
527 struct simap_node *node;
528 struct simap tmp_backers;
530 /* Handle tunnel garbage collection. */
531 simap_init(&tmp_backers);
532 simap_swap(&backer->tnl_backers, &tmp_backers);
534 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
535 struct ofport_dpif *iter;
537 if (backer != ofproto->backer) {
541 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
542 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
545 if (!iter->is_tunnel) {
549 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
550 namebuf, sizeof namebuf);
551 node = simap_find(&tmp_backers, dp_port);
553 simap_put(&backer->tnl_backers, dp_port, node->data);
554 simap_delete(&tmp_backers, node);
555 node = simap_find(&backer->tnl_backers, dp_port);
557 node = simap_find(&backer->tnl_backers, dp_port);
559 odp_port_t odp_port = ODPP_NONE;
561 if (!dpif_port_add(backer->dpif, iter->up.netdev,
563 simap_put(&backer->tnl_backers, dp_port,
564 odp_to_u32(odp_port));
565 node = simap_find(&backer->tnl_backers, dp_port);
570 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
571 if (tnl_port_reconfigure(iter, iter->up.netdev,
573 backer->need_revalidate = REV_RECONFIGURE;
578 SIMAP_FOR_EACH (node, &tmp_backers) {
579 dpif_port_del(backer->dpif, u32_to_odp(node->data));
581 simap_destroy(&tmp_backers);
583 switch (backer->need_revalidate) {
584 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
585 case REV_STP: COVERAGE_INC(rev_stp); break;
586 case REV_BOND: COVERAGE_INC(rev_bond); break;
587 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
588 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
589 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
591 backer->need_revalidate = 0;
593 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
594 struct ofport_dpif *ofport;
595 struct ofbundle *bundle;
597 if (ofproto->backer != backer) {
601 ovs_rwlock_wrlock(&xlate_rwlock);
602 xlate_ofproto_set(ofproto, ofproto->up.name,
603 ofproto->backer->dpif, ofproto->miss_rule,
604 ofproto->no_packet_in_rule, ofproto->ml,
605 ofproto->stp, ofproto->mbridge,
606 ofproto->sflow, ofproto->ipfix,
607 ofproto->netflow, ofproto->up.frag_handling,
608 ofproto->up.forward_bpdu,
609 connmgr_has_in_band(ofproto->up.connmgr),
610 ofproto->backer->enable_recirc,
611 ofproto->backer->variable_length_userdata,
612 ofproto->backer->max_mpls_depth);
614 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
615 xlate_bundle_set(ofproto, bundle, bundle->name,
616 bundle->vlan_mode, bundle->vlan,
617 bundle->trunks, bundle->use_priority_tags,
618 bundle->bond, bundle->lacp,
622 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
623 int stp_port = ofport->stp_port
624 ? stp_port_no(ofport->stp_port)
626 xlate_ofport_set(ofproto, ofport->bundle, ofport,
627 ofport->up.ofp_port, ofport->odp_port,
628 ofport->up.netdev, ofport->cfm,
629 ofport->bfd, ofport->peer, stp_port,
630 ofport->qdscp, ofport->n_qdscp,
631 ofport->up.pp.config, ofport->up.pp.state,
632 ofport->is_tunnel, ofport->may_enable);
634 ovs_rwlock_unlock(&xlate_rwlock);
637 udpif_revalidate(backer->udpif);
640 process_dpif_port_changes(backer);
645 /* Check for and handle port changes in 'backer''s dpif. */
647 process_dpif_port_changes(struct dpif_backer *backer)
653 error = dpif_port_poll(backer->dpif, &devname);
659 process_dpif_all_ports_changed(backer);
663 process_dpif_port_change(backer, devname);
668 process_dpif_port_error(backer, error);
675 process_dpif_all_ports_changed(struct dpif_backer *backer)
677 struct ofproto_dpif *ofproto;
678 struct dpif_port dpif_port;
679 struct dpif_port_dump dump;
680 struct sset devnames;
683 sset_init(&devnames);
684 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
685 if (ofproto->backer == backer) {
686 struct ofport *ofport;
688 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
689 sset_add(&devnames, netdev_get_name(ofport->netdev));
693 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
694 sset_add(&devnames, dpif_port.name);
697 SSET_FOR_EACH (devname, &devnames) {
698 process_dpif_port_change(backer, devname);
700 sset_destroy(&devnames);
704 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
706 struct ofproto_dpif *ofproto;
707 struct dpif_port port;
709 /* Don't report on the datapath's device. */
710 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
714 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
715 &all_ofproto_dpifs) {
716 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
721 ofproto = lookup_ofproto_dpif_by_port_name(devname);
722 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
723 /* The port was removed. If we know the datapath,
724 * report it through poll_set(). If we don't, it may be
725 * notifying us of a removal we initiated, so ignore it.
726 * If there's a pending ENOBUFS, let it stand, since
727 * everything will be reevaluated. */
728 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
729 sset_add(&ofproto->port_poll_set, devname);
730 ofproto->port_poll_errno = 0;
732 } else if (!ofproto) {
733 /* The port was added, but we don't know with which
734 * ofproto we should associate it. Delete it. */
735 dpif_port_del(backer->dpif, port.port_no);
737 struct ofport_dpif *ofport;
739 ofport = ofport_dpif_cast(shash_find_data(
740 &ofproto->up.port_by_name, devname));
742 && ofport->odp_port != port.port_no
743 && !odp_port_to_ofport(backer, port.port_no))
745 /* 'ofport''s datapath port number has changed from
746 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
747 * structures to match. */
748 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
749 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
750 ofport->odp_port = port.port_no;
751 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
752 hash_odp_port(port.port_no));
753 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
754 backer->need_revalidate = REV_RECONFIGURE;
757 dpif_port_destroy(&port);
760 /* Propagate 'error' to all ofprotos based on 'backer'. */
762 process_dpif_port_error(struct dpif_backer *backer, int error)
764 struct ofproto_dpif *ofproto;
766 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
767 if (ofproto->backer == backer) {
768 sset_clear(&ofproto->port_poll_set);
769 ofproto->port_poll_errno = error;
775 type_wait(const char *type)
777 struct dpif_backer *backer;
779 backer = shash_find_data(&all_dpif_backers, type);
781 /* This is not necessarily a problem, since backers are only
782 * created on demand. */
786 dpif_wait(backer->dpif);
789 /* Basic life-cycle. */
791 static int add_internal_flows(struct ofproto_dpif *);
793 static struct ofproto *
796 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
801 dealloc(struct ofproto *ofproto_)
803 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
807 /* Called when 'ofproto' is destructed. Checks for and clears any
810 dpif_backer_recirc_clear_ofproto(struct dpif_backer *backer,
811 struct ofproto_dpif *ofproto)
813 struct dpif_backer_recirc_node *node;
815 ovs_mutex_lock(&backer->recirc_mutex);
816 HMAP_FOR_EACH (node, hmap_node, &backer->recirc_map) {
817 if (node->ofproto == ofproto) {
818 VLOG_ERR("recirc_id %"PRIu32", not freed when ofproto (%s) "
819 "is destructed", node->recirc_id, ofproto->up.name);
820 hmap_remove(&backer->recirc_map, &node->hmap_node);
821 ovsrcu_postpone(free, node);
824 ovs_mutex_unlock(&backer->recirc_mutex);
828 close_dpif_backer(struct dpif_backer *backer)
830 ovs_assert(backer->refcount > 0);
832 if (--backer->refcount) {
836 udpif_destroy(backer->udpif);
838 simap_destroy(&backer->tnl_backers);
839 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
840 hmap_destroy(&backer->odp_to_ofport_map);
841 shash_find_and_delete(&all_dpif_backers, backer->type);
842 recirc_id_pool_destroy(backer->rid_pool);
843 hmap_destroy(&backer->recirc_map);
844 ovs_mutex_destroy(&backer->recirc_mutex);
846 dpif_close(backer->dpif);
850 /* Datapath port slated for removal from datapath. */
852 struct list list_node;
856 static bool check_variable_length_userdata(struct dpif_backer *backer);
857 static size_t check_max_mpls_depth(struct dpif_backer *backer);
858 static bool check_recirc(struct dpif_backer *backer);
861 open_dpif_backer(const char *type, struct dpif_backer **backerp)
863 struct dpif_backer *backer;
864 struct dpif_port_dump port_dump;
865 struct dpif_port port;
866 struct shash_node *node;
867 struct list garbage_list;
868 struct odp_garbage *garbage, *next;
875 backer = shash_find_data(&all_dpif_backers, type);
882 backer_name = xasprintf("ovs-%s", type);
884 /* Remove any existing datapaths, since we assume we're the only
885 * userspace controlling the datapath. */
887 dp_enumerate_names(type, &names);
888 SSET_FOR_EACH(name, &names) {
889 struct dpif *old_dpif;
891 /* Don't remove our backer if it exists. */
892 if (!strcmp(name, backer_name)) {
896 if (dpif_open(name, type, &old_dpif)) {
897 VLOG_WARN("couldn't open old datapath %s to remove it", name);
899 dpif_delete(old_dpif);
900 dpif_close(old_dpif);
903 sset_destroy(&names);
905 backer = xmalloc(sizeof *backer);
907 error = dpif_create_and_open(backer_name, type, &backer->dpif);
910 VLOG_ERR("failed to open datapath of type %s: %s", type,
911 ovs_strerror(error));
915 backer->udpif = udpif_create(backer, backer->dpif);
917 backer->type = xstrdup(type);
918 backer->refcount = 1;
919 hmap_init(&backer->odp_to_ofport_map);
920 ovs_rwlock_init(&backer->odp_to_ofport_lock);
921 backer->need_revalidate = 0;
922 simap_init(&backer->tnl_backers);
923 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
926 if (backer->recv_set_enable) {
927 dpif_flow_flush(backer->dpif);
930 /* Loop through the ports already on the datapath and remove any
931 * that we don't need anymore. */
932 list_init(&garbage_list);
933 dpif_port_dump_start(&port_dump, backer->dpif);
934 while (dpif_port_dump_next(&port_dump, &port)) {
935 node = shash_find(&init_ofp_ports, port.name);
936 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
937 garbage = xmalloc(sizeof *garbage);
938 garbage->odp_port = port.port_no;
939 list_push_front(&garbage_list, &garbage->list_node);
942 dpif_port_dump_done(&port_dump);
944 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
945 dpif_port_del(backer->dpif, garbage->odp_port);
946 list_remove(&garbage->list_node);
950 shash_add(&all_dpif_backers, type, backer);
952 backer->enable_recirc = check_recirc(backer);
953 backer->variable_length_userdata = check_variable_length_userdata(backer);
954 backer->max_mpls_depth = check_max_mpls_depth(backer);
955 backer->rid_pool = recirc_id_pool_create();
956 ovs_mutex_init(&backer->recirc_mutex);
957 hmap_init(&backer->recirc_map);
959 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
961 VLOG_ERR("failed to listen on datapath of type %s: %s",
962 type, ovs_strerror(error));
963 close_dpif_backer(backer);
967 if (backer->recv_set_enable) {
968 udpif_set_threads(backer->udpif, n_handlers, n_revalidators);
974 /* Tests whether 'backer''s datapath supports recirculation. Only newer
975 * datapaths support OVS_KEY_ATTR_RECIRC_ID in keys. We need to disable some
976 * features on older datapaths that don't support this feature.
978 * Returns false if 'backer' definitely does not support recirculation, true if
979 * it seems to support recirculation or if at least the error we get is
982 check_recirc(struct dpif_backer *backer)
985 struct odputil_keybuf keybuf;
988 bool enable_recirc = false;
990 memset(&flow, 0, sizeof flow);
994 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
995 odp_flow_key_from_flow(&key, &flow, NULL, 0);
997 error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE,
998 ofpbuf_data(&key), ofpbuf_size(&key), NULL, 0, NULL,
1000 if (error && error != EEXIST) {
1001 if (error != EINVAL) {
1002 VLOG_WARN("%s: Reciculation flow probe failed (%s)",
1003 dpif_name(backer->dpif), ovs_strerror(error));
1008 error = dpif_flow_del(backer->dpif, ofpbuf_data(&key), ofpbuf_size(&key),
1011 VLOG_WARN("%s: failed to delete recirculation feature probe flow",
1012 dpif_name(backer->dpif));
1015 enable_recirc = true;
1018 if (enable_recirc) {
1019 VLOG_INFO("%s: Datapath supports recirculation",
1020 dpif_name(backer->dpif));
1022 VLOG_INFO("%s: Datapath does not support recirculation",
1023 dpif_name(backer->dpif));
1026 return enable_recirc;
1029 /* Tests whether 'backer''s datapath supports variable-length
1030 * OVS_USERSPACE_ATTR_USERDATA in OVS_ACTION_ATTR_USERSPACE actions. We need
1031 * to disable some features on older datapaths that don't support this
1034 * Returns false if 'backer' definitely does not support variable-length
1035 * userdata, true if it seems to support them or if at least the error we get
1038 check_variable_length_userdata(struct dpif_backer *backer)
1040 struct eth_header *eth;
1041 struct ofpbuf actions;
1042 struct dpif_execute execute;
1043 struct ofpbuf packet;
1047 /* Compose a userspace action that will cause an ERANGE error on older
1048 * datapaths that don't support variable-length userdata.
1050 * We really test for using userdata longer than 8 bytes, but older
1051 * datapaths accepted these, silently truncating the userdata to 8 bytes.
1052 * The same older datapaths rejected userdata shorter than 8 bytes, so we
1053 * test for that instead as a proxy for longer userdata support. */
1054 ofpbuf_init(&actions, 64);
1055 start = nl_msg_start_nested(&actions, OVS_ACTION_ATTR_USERSPACE);
1056 nl_msg_put_u32(&actions, OVS_USERSPACE_ATTR_PID,
1057 dpif_port_get_pid(backer->dpif, ODPP_NONE, 0));
1058 nl_msg_put_unspec_zero(&actions, OVS_USERSPACE_ATTR_USERDATA, 4);
1059 nl_msg_end_nested(&actions, start);
1061 /* Compose a dummy ethernet packet. */
1062 ofpbuf_init(&packet, ETH_HEADER_LEN);
1063 eth = ofpbuf_put_zeros(&packet, ETH_HEADER_LEN);
1064 eth->eth_type = htons(0x1234);
1066 /* Execute the actions. On older datapaths this fails with ERANGE, on
1067 * newer datapaths it succeeds. */
1068 execute.actions = ofpbuf_data(&actions);
1069 execute.actions_len = ofpbuf_size(&actions);
1070 execute.packet = &packet;
1071 execute.md = PKT_METADATA_INITIALIZER(0);
1072 execute.needs_help = false;
1074 error = dpif_execute(backer->dpif, &execute);
1076 ofpbuf_uninit(&packet);
1077 ofpbuf_uninit(&actions);
1084 /* Variable-length userdata is not supported. */
1085 VLOG_WARN("%s: datapath does not support variable-length userdata "
1086 "feature (needs Linux 3.10+ or kernel module from OVS "
1087 "1..11+). The NXAST_SAMPLE action will be ignored.",
1088 dpif_name(backer->dpif));
1092 /* Something odd happened. We're not sure whether variable-length
1093 * userdata is supported. Default to "yes". */
1094 VLOG_WARN("%s: variable-length userdata feature probe failed (%s)",
1095 dpif_name(backer->dpif), ovs_strerror(error));
1100 /* Tests the MPLS label stack depth supported by 'backer''s datapath.
1102 * Returns the number of elements in a struct flow's mpls_lse field
1103 * if the datapath supports at least that many entries in an
1105 * Otherwise returns the number of MPLS push actions supported by
1108 check_max_mpls_depth(struct dpif_backer *backer)
1113 for (n = 0; n < FLOW_MAX_MPLS_LABELS; n++) {
1114 struct odputil_keybuf keybuf;
1118 memset(&flow, 0, sizeof flow);
1119 flow.dl_type = htons(ETH_TYPE_MPLS);
1120 flow_set_mpls_bos(&flow, n, 1);
1122 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1123 odp_flow_key_from_flow(&key, &flow, NULL, 0);
1125 error = dpif_flow_put(backer->dpif, DPIF_FP_CREATE,
1126 ofpbuf_data(&key), ofpbuf_size(&key), NULL, 0, NULL, 0, NULL);
1127 if (error && error != EEXIST) {
1128 if (error != EINVAL) {
1129 VLOG_WARN("%s: MPLS stack length feature probe failed (%s)",
1130 dpif_name(backer->dpif), ovs_strerror(error));
1135 error = dpif_flow_del(backer->dpif, ofpbuf_data(&key), ofpbuf_size(&key), NULL);
1137 VLOG_WARN("%s: failed to delete MPLS feature probe flow",
1138 dpif_name(backer->dpif));
1142 VLOG_INFO("%s: MPLS label stack length probed as %d",
1143 dpif_name(backer->dpif), n);
1148 construct(struct ofproto *ofproto_)
1150 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1151 struct shash_node *node, *next;
1154 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1159 ofproto->netflow = NULL;
1160 ofproto->sflow = NULL;
1161 ofproto->ipfix = NULL;
1162 ofproto->stp = NULL;
1163 ofproto->dump_seq = 0;
1164 hmap_init(&ofproto->bundles);
1165 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1166 ofproto->mbridge = mbridge_create();
1167 ofproto->has_bonded_bundles = false;
1168 ofproto->lacp_enabled = false;
1169 ovs_mutex_init_adaptive(&ofproto->stats_mutex);
1170 ovs_mutex_init(&ofproto->vsp_mutex);
1172 guarded_list_init(&ofproto->pins);
1174 ofproto_dpif_unixctl_init();
1176 hmap_init(&ofproto->vlandev_map);
1177 hmap_init(&ofproto->realdev_vid_map);
1179 sset_init(&ofproto->ports);
1180 sset_init(&ofproto->ghost_ports);
1181 sset_init(&ofproto->port_poll_set);
1182 ofproto->port_poll_errno = 0;
1183 ofproto->change_seq = 0;
1184 ofproto->pins_seq = seq_create();
1185 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1188 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1189 struct iface_hint *iface_hint = node->data;
1191 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1192 /* Check if the datapath already has this port. */
1193 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1194 sset_add(&ofproto->ports, node->name);
1197 free(iface_hint->br_name);
1198 free(iface_hint->br_type);
1200 shash_delete(&init_ofp_ports, node);
1204 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1205 hash_string(ofproto->up.name, 0));
1206 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1208 ofproto_init_tables(ofproto_, N_TABLES);
1209 error = add_internal_flows(ofproto);
1211 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1217 add_internal_miss_flow(struct ofproto_dpif *ofproto, int id,
1218 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1224 match_init_catchall(&match);
1225 match_set_reg(&match, 0, id);
1227 error = ofproto_dpif_add_internal_flow(ofproto, &match, 0, ofpacts, &rule);
1228 *rulep = error ? NULL : rule_dpif_cast(rule);
1234 add_internal_flows(struct ofproto_dpif *ofproto)
1236 struct ofpact_controller *controller;
1237 uint64_t ofpacts_stub[128 / 8];
1238 struct ofpbuf ofpacts;
1239 struct rule *unused_rulep OVS_UNUSED;
1240 struct ofpact_resubmit *resubmit;
1245 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1248 controller = ofpact_put_CONTROLLER(&ofpacts);
1249 controller->max_len = UINT16_MAX;
1250 controller->controller_id = 0;
1251 controller->reason = OFPR_NO_MATCH;
1252 ofpact_pad(&ofpacts);
1254 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1255 &ofproto->miss_rule);
1260 ofpbuf_clear(&ofpacts);
1261 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1262 &ofproto->no_packet_in_rule);
1267 error = add_internal_miss_flow(ofproto, id++, &ofpacts,
1268 &ofproto->drop_frags_rule);
1273 /* Drop any run away non-recirc rule lookups. Recirc_id has to be
1274 * zero when reaching this rule.
1276 * (priority=2), recirc_id=0, actions=drop
1278 ofpbuf_clear(&ofpacts);
1279 match_init_catchall(&match);
1280 match_set_recirc_id(&match, 0);
1281 error = ofproto_dpif_add_internal_flow(ofproto, &match, 2, &ofpacts,
1287 /* Continue rule lookups for not-matched recirc rules from table 0.
1289 * (priority=1), actions=resubmit(, 0)
1291 resubmit = ofpact_put_RESUBMIT(&ofpacts);
1292 resubmit->ofpact.compat = 0;
1293 resubmit->in_port = OFPP_IN_PORT;
1294 resubmit->table_id = 0;
1296 match_init_catchall(&match);
1298 error = ofproto_dpif_add_internal_flow(ofproto, &match, 1, &ofpacts,
1305 destruct(struct ofproto *ofproto_)
1307 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1308 struct rule_dpif *rule, *next_rule;
1309 struct ofproto_packet_in *pin, *next_pin;
1310 struct oftable *table;
1313 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1314 ovs_rwlock_wrlock(&xlate_rwlock);
1315 xlate_remove_ofproto(ofproto);
1316 ovs_rwlock_unlock(&xlate_rwlock);
1318 /* Ensure that the upcall processing threads have no remaining references
1319 * to the ofproto or anything in it. */
1320 udpif_synchronize(ofproto->backer->udpif);
1322 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1324 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1325 struct cls_cursor cursor;
1327 fat_rwlock_rdlock(&table->cls.rwlock);
1328 cls_cursor_init(&cursor, &table->cls, NULL);
1329 fat_rwlock_unlock(&table->cls.rwlock);
1330 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1331 ofproto_rule_delete(&ofproto->up, &rule->up);
1335 guarded_list_pop_all(&ofproto->pins, &pins);
1336 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1337 list_remove(&pin->list_node);
1338 free(CONST_CAST(void *, pin->up.packet));
1341 guarded_list_destroy(&ofproto->pins);
1343 dpif_backer_recirc_clear_ofproto(ofproto->backer, ofproto);
1345 mbridge_unref(ofproto->mbridge);
1347 netflow_unref(ofproto->netflow);
1348 dpif_sflow_unref(ofproto->sflow);
1349 hmap_destroy(&ofproto->bundles);
1350 mac_learning_unref(ofproto->ml);
1352 hmap_destroy(&ofproto->vlandev_map);
1353 hmap_destroy(&ofproto->realdev_vid_map);
1355 sset_destroy(&ofproto->ports);
1356 sset_destroy(&ofproto->ghost_ports);
1357 sset_destroy(&ofproto->port_poll_set);
1359 ovs_mutex_destroy(&ofproto->stats_mutex);
1360 ovs_mutex_destroy(&ofproto->vsp_mutex);
1362 seq_destroy(ofproto->pins_seq);
1364 close_dpif_backer(ofproto->backer);
1368 run(struct ofproto *ofproto_)
1370 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1371 uint64_t new_seq, new_dump_seq;
1373 if (mbridge_need_revalidate(ofproto->mbridge)) {
1374 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1375 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1376 mac_learning_flush(ofproto->ml);
1377 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1380 /* Always updates the ofproto->pins_seqno to avoid frequent wakeup during
1381 * flow restore. Even though nothing is processed during flow restore,
1382 * all queued 'pins' will be handled immediately when flow restore
1384 ofproto->pins_seqno = seq_read(ofproto->pins_seq);
1386 /* Do not perform any periodic activity required by 'ofproto' while
1387 * waiting for flow restore to complete. */
1388 if (!ofproto_get_flow_restore_wait()) {
1389 struct ofproto_packet_in *pin, *next_pin;
1392 guarded_list_pop_all(&ofproto->pins, &pins);
1393 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1394 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1395 list_remove(&pin->list_node);
1396 free(CONST_CAST(void *, pin->up.packet));
1401 if (ofproto->netflow) {
1402 netflow_run(ofproto->netflow);
1404 if (ofproto->sflow) {
1405 dpif_sflow_run(ofproto->sflow);
1407 if (ofproto->ipfix) {
1408 dpif_ipfix_run(ofproto->ipfix);
1411 new_seq = seq_read(connectivity_seq_get());
1412 if (ofproto->change_seq != new_seq) {
1413 struct ofport_dpif *ofport;
1415 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1419 ofproto->change_seq = new_seq;
1421 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1422 struct ofbundle *bundle;
1424 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1430 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1431 if (mac_learning_run(ofproto->ml)) {
1432 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1434 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1436 new_dump_seq = seq_read(udpif_dump_seq(ofproto->backer->udpif));
1437 if (ofproto->dump_seq != new_dump_seq) {
1438 struct rule *rule, *next_rule;
1440 /* We know stats are relatively fresh, so now is a good time to do some
1442 ofproto->dump_seq = new_dump_seq;
1444 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
1446 ovs_mutex_lock(&ofproto_mutex);
1447 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
1448 &ofproto->up.expirable) {
1449 rule_expire(rule_dpif_cast(rule));
1451 ovs_mutex_unlock(&ofproto_mutex);
1453 /* All outstanding data in existing flows has been accounted, so it's a
1454 * good time to do bond rebalancing. */
1455 if (ofproto->has_bonded_bundles) {
1456 struct ofbundle *bundle;
1458 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1460 bond_rebalance(bundle->bond);
1470 wait(struct ofproto *ofproto_)
1472 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1474 if (ofproto_get_flow_restore_wait()) {
1478 if (ofproto->sflow) {
1479 dpif_sflow_wait(ofproto->sflow);
1481 if (ofproto->ipfix) {
1482 dpif_ipfix_wait(ofproto->ipfix);
1484 if (ofproto->lacp_enabled || ofproto->has_bonded_bundles) {
1485 struct ofbundle *bundle;
1487 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1488 bundle_wait(bundle);
1491 if (ofproto->netflow) {
1492 netflow_wait(ofproto->netflow);
1494 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1495 mac_learning_wait(ofproto->ml);
1496 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1498 if (ofproto->backer->need_revalidate) {
1499 /* Shouldn't happen, but if it does just go around again. */
1500 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1501 poll_immediate_wake();
1504 seq_wait(udpif_dump_seq(ofproto->backer->udpif), ofproto->dump_seq);
1505 seq_wait(ofproto->pins_seq, ofproto->pins_seqno);
1509 type_get_memory_usage(const char *type, struct simap *usage)
1511 struct dpif_backer *backer;
1513 backer = shash_find_data(&all_dpif_backers, type);
1515 udpif_get_memory_usage(backer->udpif, usage);
1520 flush(struct ofproto *ofproto_)
1522 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1523 struct dpif_backer *backer = ofproto->backer;
1526 udpif_flush(backer->udpif);
1531 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1532 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1534 *arp_match_ip = true;
1535 *actions = (OFPUTIL_A_OUTPUT |
1536 OFPUTIL_A_SET_VLAN_VID |
1537 OFPUTIL_A_SET_VLAN_PCP |
1538 OFPUTIL_A_STRIP_VLAN |
1539 OFPUTIL_A_SET_DL_SRC |
1540 OFPUTIL_A_SET_DL_DST |
1541 OFPUTIL_A_SET_NW_SRC |
1542 OFPUTIL_A_SET_NW_DST |
1543 OFPUTIL_A_SET_NW_TOS |
1544 OFPUTIL_A_SET_TP_SRC |
1545 OFPUTIL_A_SET_TP_DST |
1550 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1552 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1553 struct dpif_dp_stats s;
1554 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1558 strcpy(ots->name, "classifier");
1560 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1561 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes, &used);
1562 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes,
1564 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes,
1566 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1567 ots->lookup_count = htonll(n_lookup);
1568 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1571 static struct ofport *
1574 struct ofport_dpif *port = xmalloc(sizeof *port);
1579 port_dealloc(struct ofport *port_)
1581 struct ofport_dpif *port = ofport_dpif_cast(port_);
1586 port_construct(struct ofport *port_)
1588 struct ofport_dpif *port = ofport_dpif_cast(port_);
1589 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1590 const struct netdev *netdev = port->up.netdev;
1591 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1592 struct dpif_port dpif_port;
1595 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1596 port->bundle = NULL;
1599 port->may_enable = true;
1600 port->stp_port = NULL;
1601 port->stp_state = STP_DISABLED;
1602 port->is_tunnel = false;
1606 port->realdev_ofp_port = 0;
1607 port->vlandev_vid = 0;
1608 port->carrier_seq = netdev_get_carrier_resets(netdev);
1609 port->is_layer3 = netdev_vport_is_layer3(netdev);
1611 if (netdev_vport_is_patch(netdev)) {
1612 /* By bailing out here, we don't submit the port to the sFlow module
1613 * to be considered for counter polling export. This is correct
1614 * because the patch port represents an interface that sFlow considers
1615 * to be "internal" to the switch as a whole, and therefore not an
1616 * candidate for counter polling. */
1617 port->odp_port = ODPP_NONE;
1618 ofport_update_peer(port);
1622 error = dpif_port_query_by_name(ofproto->backer->dpif,
1623 netdev_vport_get_dpif_port(netdev, namebuf,
1630 port->odp_port = dpif_port.port_no;
1632 if (netdev_get_tunnel_config(netdev)) {
1633 tnl_port_add(port, port->up.netdev, port->odp_port);
1634 port->is_tunnel = true;
1636 /* Sanity-check that a mapping doesn't already exist. This
1637 * shouldn't happen for non-tunnel ports. */
1638 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1639 VLOG_ERR("port %s already has an OpenFlow port number",
1641 dpif_port_destroy(&dpif_port);
1645 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1646 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1647 hash_odp_port(port->odp_port));
1648 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1650 dpif_port_destroy(&dpif_port);
1652 if (ofproto->sflow) {
1653 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1660 port_destruct(struct ofport *port_)
1662 struct ofport_dpif *port = ofport_dpif_cast(port_);
1663 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1664 const char *devname = netdev_get_name(port->up.netdev);
1665 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1666 const char *dp_port_name;
1668 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1669 ovs_rwlock_wrlock(&xlate_rwlock);
1670 xlate_ofport_remove(port);
1671 ovs_rwlock_unlock(&xlate_rwlock);
1673 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1675 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1676 /* The underlying device is still there, so delete it. This
1677 * happens when the ofproto is being destroyed, since the caller
1678 * assumes that removal of attached ports will happen as part of
1680 if (!port->is_tunnel) {
1681 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1686 port->peer->peer = NULL;
1690 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1691 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1692 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1693 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1697 sset_find_and_delete(&ofproto->ports, devname);
1698 sset_find_and_delete(&ofproto->ghost_ports, devname);
1699 bundle_remove(port_);
1700 set_cfm(port_, NULL);
1701 set_bfd(port_, NULL);
1702 if (port->stp_port) {
1703 stp_port_disable(port->stp_port);
1705 if (ofproto->sflow) {
1706 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1713 port_modified(struct ofport *port_)
1715 struct ofport_dpif *port = ofport_dpif_cast(port_);
1717 if (port->bundle && port->bundle->bond) {
1718 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1722 cfm_set_netdev(port->cfm, port->up.netdev);
1726 bfd_set_netdev(port->bfd, port->up.netdev);
1729 ofproto_dpif_monitor_port_update(port, port->bfd, port->cfm,
1730 port->up.pp.hw_addr);
1732 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1734 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1738 ofport_update_peer(port);
1742 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1744 struct ofport_dpif *port = ofport_dpif_cast(port_);
1745 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1746 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1748 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1749 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1750 OFPUTIL_PC_NO_PACKET_IN)) {
1751 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1753 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1754 bundle_update(port->bundle);
1760 set_sflow(struct ofproto *ofproto_,
1761 const struct ofproto_sflow_options *sflow_options)
1763 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1764 struct dpif_sflow *ds = ofproto->sflow;
1766 if (sflow_options) {
1767 uint32_t old_probability = ds ? dpif_sflow_get_probability(ds) : 0;
1769 struct ofport_dpif *ofport;
1771 ds = ofproto->sflow = dpif_sflow_create();
1772 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1773 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1776 dpif_sflow_set_options(ds, sflow_options);
1777 if (dpif_sflow_get_probability(ds) != old_probability) {
1778 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1782 dpif_sflow_unref(ds);
1783 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1784 ofproto->sflow = NULL;
1792 struct ofproto *ofproto_,
1793 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1794 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1795 size_t n_flow_exporters_options)
1797 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1798 struct dpif_ipfix *di = ofproto->ipfix;
1799 bool has_options = bridge_exporter_options || flow_exporters_options;
1801 if (has_options && !di) {
1802 di = ofproto->ipfix = dpif_ipfix_create();
1806 /* Call set_options in any case to cleanly flush the flow
1807 * caches in the last exporters that are to be destroyed. */
1808 dpif_ipfix_set_options(
1809 di, bridge_exporter_options, flow_exporters_options,
1810 n_flow_exporters_options);
1813 dpif_ipfix_unref(di);
1814 ofproto->ipfix = NULL;
1822 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1824 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1829 struct ofproto_dpif *ofproto;
1831 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1832 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1833 ofport->cfm = cfm_create(ofport->up.netdev);
1836 if (cfm_configure(ofport->cfm, s)) {
1843 cfm_unref(ofport->cfm);
1846 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1847 ofport->up.pp.hw_addr);
1852 get_cfm_status(const struct ofport *ofport_, bool force,
1853 struct ofproto_cfm_status *status)
1855 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1859 if (cfm_check_status_change(ofport->cfm) || force) {
1860 status->faults = cfm_get_fault(ofport->cfm);
1861 status->flap_count = cfm_get_flap_count(ofport->cfm);
1862 status->remote_opstate = cfm_get_opup(ofport->cfm);
1863 status->health = cfm_get_health(ofport->cfm);
1864 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1866 ret = NO_STATUS_CHANGE;
1876 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1878 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1879 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1883 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
1884 cfg, ofport->up.netdev);
1885 if (ofport->bfd != old) {
1886 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1888 ofproto_dpif_monitor_port_update(ofport, ofport->bfd, ofport->cfm,
1889 ofport->up.pp.hw_addr);
1894 get_bfd_status(struct ofport *ofport_, bool force, struct smap *smap)
1896 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1900 if (bfd_check_status_change(ofport->bfd) || force) {
1901 bfd_get_status(ofport->bfd, smap);
1903 ret = NO_STATUS_CHANGE;
1912 /* Spanning Tree. */
1915 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1917 struct ofproto_dpif *ofproto = ofproto_;
1918 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1919 struct ofport_dpif *ofport;
1921 ofport = stp_port_get_aux(sp);
1923 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1924 ofproto->up.name, port_num);
1926 struct eth_header *eth = ofpbuf_l2(pkt);
1928 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1929 if (eth_addr_is_zero(eth->eth_src)) {
1930 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1931 "with unknown MAC", ofproto->up.name, port_num);
1933 ofproto_dpif_send_packet(ofport, pkt);
1939 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1941 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1943 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1945 /* Only revalidate flows if the configuration changed. */
1946 if (!s != !ofproto->stp) {
1947 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1951 if (!ofproto->stp) {
1952 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1953 send_bpdu_cb, ofproto);
1954 ofproto->stp_last_tick = time_msec();
1957 stp_set_bridge_id(ofproto->stp, s->system_id);
1958 stp_set_bridge_priority(ofproto->stp, s->priority);
1959 stp_set_hello_time(ofproto->stp, s->hello_time);
1960 stp_set_max_age(ofproto->stp, s->max_age);
1961 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1963 struct ofport *ofport;
1965 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1966 set_stp_port(ofport, NULL);
1969 stp_unref(ofproto->stp);
1970 ofproto->stp = NULL;
1977 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1979 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1983 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1984 s->designated_root = stp_get_designated_root(ofproto->stp);
1985 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1994 update_stp_port_state(struct ofport_dpif *ofport)
1996 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1997 enum stp_state state;
1999 /* Figure out new state. */
2000 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2004 if (ofport->stp_state != state) {
2005 enum ofputil_port_state of_state;
2008 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
2009 netdev_get_name(ofport->up.netdev),
2010 stp_state_name(ofport->stp_state),
2011 stp_state_name(state));
2012 if (stp_learn_in_state(ofport->stp_state)
2013 != stp_learn_in_state(state)) {
2014 /* xxx Learning action flows should also be flushed. */
2015 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2016 mac_learning_flush(ofproto->ml);
2017 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2019 fwd_change = stp_forward_in_state(ofport->stp_state)
2020 != stp_forward_in_state(state);
2022 ofproto->backer->need_revalidate = REV_STP;
2023 ofport->stp_state = state;
2024 ofport->stp_state_entered = time_msec();
2026 if (fwd_change && ofport->bundle) {
2027 bundle_update(ofport->bundle);
2030 /* Update the STP state bits in the OpenFlow port description. */
2031 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2032 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2033 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2034 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2035 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2037 ofproto_port_set_state(&ofport->up, of_state);
2041 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2042 * caller is responsible for assigning STP port numbers and ensuring
2043 * there are no duplicates. */
2045 set_stp_port(struct ofport *ofport_,
2046 const struct ofproto_port_stp_settings *s)
2048 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2049 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2050 struct stp_port *sp = ofport->stp_port;
2052 if (!s || !s->enable) {
2054 ofport->stp_port = NULL;
2055 stp_port_disable(sp);
2056 update_stp_port_state(ofport);
2059 } else if (sp && stp_port_no(sp) != s->port_num
2060 && ofport == stp_port_get_aux(sp)) {
2061 /* The port-id changed, so disable the old one if it's not
2062 * already in use by another port. */
2063 stp_port_disable(sp);
2066 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2067 stp_port_enable(sp);
2069 stp_port_set_aux(sp, ofport);
2070 stp_port_set_priority(sp, s->priority);
2071 stp_port_set_path_cost(sp, s->path_cost);
2073 update_stp_port_state(ofport);
2079 get_stp_port_status(struct ofport *ofport_,
2080 struct ofproto_port_stp_status *s)
2082 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2083 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2084 struct stp_port *sp = ofport->stp_port;
2086 if (!ofproto->stp || !sp) {
2092 s->port_id = stp_port_get_id(sp);
2093 s->state = stp_port_get_state(sp);
2094 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2095 s->role = stp_port_get_role(sp);
2101 get_stp_port_stats(struct ofport *ofport_,
2102 struct ofproto_port_stp_stats *s)
2104 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2105 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2106 struct stp_port *sp = ofport->stp_port;
2108 if (!ofproto->stp || !sp) {
2114 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2120 stp_run(struct ofproto_dpif *ofproto)
2123 long long int now = time_msec();
2124 long long int elapsed = now - ofproto->stp_last_tick;
2125 struct stp_port *sp;
2128 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2129 ofproto->stp_last_tick = now;
2131 while (stp_get_changed_port(ofproto->stp, &sp)) {
2132 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2135 update_stp_port_state(ofport);
2139 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2140 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2141 mac_learning_flush(ofproto->ml);
2142 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2148 stp_wait(struct ofproto_dpif *ofproto)
2151 poll_timer_wait(1000);
2156 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2159 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2160 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2162 if (ofport->n_qdscp != n_qdscp
2163 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2164 n_qdscp * sizeof *qdscp))) {
2165 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2166 free(ofport->qdscp);
2167 ofport->qdscp = n_qdscp
2168 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2170 ofport->n_qdscp = n_qdscp;
2178 /* Expires all MAC learning entries associated with 'bundle' and forces its
2179 * ofproto to revalidate every flow.
2181 * Normally MAC learning entries are removed only from the ofproto associated
2182 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2183 * are removed from every ofproto. When patch ports and SLB bonds are in use
2184 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2185 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2186 * with the host from which it migrated. */
2188 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2190 struct ofproto_dpif *ofproto = bundle->ofproto;
2191 struct mac_learning *ml = ofproto->ml;
2192 struct mac_entry *mac, *next_mac;
2194 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2195 ovs_rwlock_wrlock(&ml->rwlock);
2196 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2197 if (mac->port.p == bundle) {
2199 struct ofproto_dpif *o;
2201 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2203 struct mac_entry *e;
2205 ovs_rwlock_wrlock(&o->ml->rwlock);
2206 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2208 mac_learning_expire(o->ml, e);
2210 ovs_rwlock_unlock(&o->ml->rwlock);
2215 mac_learning_expire(ml, mac);
2218 ovs_rwlock_unlock(&ml->rwlock);
2221 static struct ofbundle *
2222 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2224 struct ofbundle *bundle;
2226 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2227 &ofproto->bundles) {
2228 if (bundle->aux == aux) {
2236 bundle_update(struct ofbundle *bundle)
2238 struct ofport_dpif *port;
2240 bundle->floodable = true;
2241 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2242 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2244 || !stp_forward_in_state(port->stp_state)) {
2245 bundle->floodable = false;
2252 bundle_del_port(struct ofport_dpif *port)
2254 struct ofbundle *bundle = port->bundle;
2256 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2258 list_remove(&port->bundle_node);
2259 port->bundle = NULL;
2262 lacp_slave_unregister(bundle->lacp, port);
2265 bond_slave_unregister(bundle->bond, port);
2268 bundle_update(bundle);
2272 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2273 struct lacp_slave_settings *lacp)
2275 struct ofport_dpif *port;
2277 port = get_ofp_port(bundle->ofproto, ofp_port);
2282 if (port->bundle != bundle) {
2283 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2285 bundle_remove(&port->up);
2288 port->bundle = bundle;
2289 list_push_back(&bundle->ports, &port->bundle_node);
2290 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2292 || !stp_forward_in_state(port->stp_state)) {
2293 bundle->floodable = false;
2297 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2298 lacp_slave_register(bundle->lacp, port, lacp);
2305 bundle_destroy(struct ofbundle *bundle)
2307 struct ofproto_dpif *ofproto;
2308 struct ofport_dpif *port, *next_port;
2314 ofproto = bundle->ofproto;
2315 mbridge_unregister_bundle(ofproto->mbridge, bundle);
2317 ovs_rwlock_wrlock(&xlate_rwlock);
2318 xlate_bundle_remove(bundle);
2319 ovs_rwlock_unlock(&xlate_rwlock);
2321 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2322 bundle_del_port(port);
2325 bundle_flush_macs(bundle, true);
2326 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2328 free(bundle->trunks);
2329 lacp_unref(bundle->lacp);
2330 bond_unref(bundle->bond);
2335 bundle_set(struct ofproto *ofproto_, void *aux,
2336 const struct ofproto_bundle_settings *s)
2338 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2339 bool need_flush = false;
2340 struct ofport_dpif *port;
2341 struct ofbundle *bundle;
2342 unsigned long *trunks;
2348 bundle_destroy(bundle_lookup(ofproto, aux));
2352 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2353 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2355 bundle = bundle_lookup(ofproto, aux);
2357 bundle = xmalloc(sizeof *bundle);
2359 bundle->ofproto = ofproto;
2360 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2361 hash_pointer(aux, 0));
2363 bundle->name = NULL;
2365 list_init(&bundle->ports);
2366 bundle->vlan_mode = PORT_VLAN_TRUNK;
2368 bundle->trunks = NULL;
2369 bundle->use_priority_tags = s->use_priority_tags;
2370 bundle->lacp = NULL;
2371 bundle->bond = NULL;
2373 bundle->floodable = true;
2374 mbridge_register_bundle(ofproto->mbridge, bundle);
2377 if (!bundle->name || strcmp(s->name, bundle->name)) {
2379 bundle->name = xstrdup(s->name);
2384 ofproto->lacp_enabled = true;
2385 if (!bundle->lacp) {
2386 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2387 bundle->lacp = lacp_create();
2389 lacp_configure(bundle->lacp, s->lacp);
2391 lacp_unref(bundle->lacp);
2392 bundle->lacp = NULL;
2395 /* Update set of ports. */
2397 for (i = 0; i < s->n_slaves; i++) {
2398 if (!bundle_add_port(bundle, s->slaves[i],
2399 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2403 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2404 struct ofport_dpif *next_port;
2406 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2407 for (i = 0; i < s->n_slaves; i++) {
2408 if (s->slaves[i] == port->up.ofp_port) {
2413 bundle_del_port(port);
2417 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2419 if (list_is_empty(&bundle->ports)) {
2420 bundle_destroy(bundle);
2424 /* Set VLAN tagging mode */
2425 if (s->vlan_mode != bundle->vlan_mode
2426 || s->use_priority_tags != bundle->use_priority_tags) {
2427 bundle->vlan_mode = s->vlan_mode;
2428 bundle->use_priority_tags = s->use_priority_tags;
2433 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2434 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2436 if (vlan != bundle->vlan) {
2437 bundle->vlan = vlan;
2441 /* Get trunked VLANs. */
2442 switch (s->vlan_mode) {
2443 case PORT_VLAN_ACCESS:
2447 case PORT_VLAN_TRUNK:
2448 trunks = CONST_CAST(unsigned long *, s->trunks);
2451 case PORT_VLAN_NATIVE_UNTAGGED:
2452 case PORT_VLAN_NATIVE_TAGGED:
2453 if (vlan != 0 && (!s->trunks
2454 || !bitmap_is_set(s->trunks, vlan)
2455 || bitmap_is_set(s->trunks, 0))) {
2456 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2458 trunks = bitmap_clone(s->trunks, 4096);
2460 trunks = bitmap_allocate1(4096);
2462 bitmap_set1(trunks, vlan);
2463 bitmap_set0(trunks, 0);
2465 trunks = CONST_CAST(unsigned long *, s->trunks);
2472 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2473 free(bundle->trunks);
2474 if (trunks == s->trunks) {
2475 bundle->trunks = vlan_bitmap_clone(trunks);
2477 bundle->trunks = trunks;
2482 if (trunks != s->trunks) {
2487 if (!list_is_short(&bundle->ports)) {
2488 bundle->ofproto->has_bonded_bundles = true;
2490 if (bond_reconfigure(bundle->bond, s->bond)) {
2491 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2494 bundle->bond = bond_create(s->bond, ofproto);
2495 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2498 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2499 bond_slave_register(bundle->bond, port,
2500 port->up.ofp_port, port->up.netdev);
2503 bond_unref(bundle->bond);
2504 bundle->bond = NULL;
2507 /* If we changed something that would affect MAC learning, un-learn
2508 * everything on this port and force flow revalidation. */
2510 bundle_flush_macs(bundle, false);
2517 bundle_remove(struct ofport *port_)
2519 struct ofport_dpif *port = ofport_dpif_cast(port_);
2520 struct ofbundle *bundle = port->bundle;
2523 bundle_del_port(port);
2524 if (list_is_empty(&bundle->ports)) {
2525 bundle_destroy(bundle);
2526 } else if (list_is_short(&bundle->ports)) {
2527 bond_unref(bundle->bond);
2528 bundle->bond = NULL;
2534 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2536 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2537 struct ofport_dpif *port = port_;
2538 uint8_t ea[ETH_ADDR_LEN];
2541 error = netdev_get_etheraddr(port->up.netdev, ea);
2543 struct ofpbuf packet;
2546 ofpbuf_init(&packet, 0);
2547 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2549 memcpy(packet_pdu, pdu, pdu_size);
2551 ofproto_dpif_send_packet(port, &packet);
2552 ofpbuf_uninit(&packet);
2554 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2555 "%s (%s)", port->bundle->name,
2556 netdev_get_name(port->up.netdev), ovs_strerror(error));
2561 bundle_send_learning_packets(struct ofbundle *bundle)
2563 struct ofproto_dpif *ofproto = bundle->ofproto;
2564 struct ofpbuf *learning_packet;
2565 int error, n_packets, n_errors;
2566 struct mac_entry *e;
2567 struct list packets;
2569 list_init(&packets);
2570 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2571 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2572 if (e->port.p != bundle) {
2575 learning_packet = bond_compose_learning_packet(bundle->bond,
2578 /* Temporarily use 'frame' as a private pointer (see below). */
2579 ovs_assert(learning_packet->frame == ofpbuf_data(learning_packet));
2580 learning_packet->frame = port_void;
2581 list_push_back(&packets, &learning_packet->list_node);
2584 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2586 error = n_packets = n_errors = 0;
2587 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2589 void *port_void = learning_packet->frame;
2591 /* Restore 'frame'. */
2592 learning_packet->frame = ofpbuf_data(learning_packet);
2593 ret = ofproto_dpif_send_packet(port_void, learning_packet);
2600 ofpbuf_list_delete(&packets);
2603 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2604 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2605 "packets, last error was: %s",
2606 bundle->name, n_errors, n_packets, ovs_strerror(error));
2608 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2609 bundle->name, n_packets);
2614 bundle_run(struct ofbundle *bundle)
2617 lacp_run(bundle->lacp, send_pdu_cb);
2620 struct ofport_dpif *port;
2622 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2623 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2626 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2627 bundle->ofproto->backer->need_revalidate = REV_BOND;
2630 if (bond_should_send_learning_packets(bundle->bond)) {
2631 bundle_send_learning_packets(bundle);
2637 bundle_wait(struct ofbundle *bundle)
2640 lacp_wait(bundle->lacp);
2643 bond_wait(bundle->bond);
2650 mirror_set__(struct ofproto *ofproto_, void *aux,
2651 const struct ofproto_mirror_settings *s)
2653 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2654 struct ofbundle **srcs, **dsts;
2659 mirror_destroy(ofproto->mbridge, aux);
2663 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2664 dsts = xmalloc(s->n_dsts * sizeof *dsts);
2666 for (i = 0; i < s->n_srcs; i++) {
2667 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2670 for (i = 0; i < s->n_dsts; i++) {
2671 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2674 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2675 s->n_dsts, s->src_vlans,
2676 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2683 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2684 uint64_t *packets, uint64_t *bytes)
2686 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2691 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2693 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2694 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2695 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2696 mac_learning_flush(ofproto->ml);
2698 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2703 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2705 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2706 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2707 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2711 forward_bpdu_changed(struct ofproto *ofproto_)
2713 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2714 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2718 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2721 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2722 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2723 mac_learning_set_idle_time(ofproto->ml, idle_time);
2724 mac_learning_set_max_entries(ofproto->ml, max_entries);
2725 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2730 static struct ofport_dpif *
2731 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2733 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2734 return ofport ? ofport_dpif_cast(ofport) : NULL;
2738 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2739 struct ofproto_port *ofproto_port,
2740 struct dpif_port *dpif_port)
2742 ofproto_port->name = dpif_port->name;
2743 ofproto_port->type = dpif_port->type;
2744 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2748 ofport_update_peer(struct ofport_dpif *ofport)
2750 const struct ofproto_dpif *ofproto;
2751 struct dpif_backer *backer;
2754 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2758 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2759 backer->need_revalidate = REV_RECONFIGURE;
2762 ofport->peer->peer = NULL;
2763 ofport->peer = NULL;
2766 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2771 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2772 struct ofport *peer_ofport;
2773 struct ofport_dpif *peer;
2776 if (ofproto->backer != backer) {
2780 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2785 peer = ofport_dpif_cast(peer_ofport);
2786 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2787 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2789 ofport->peer = peer;
2790 ofport->peer->peer = ofport;
2800 port_run(struct ofport_dpif *ofport)
2802 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2803 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2804 bool enable = netdev_get_carrier(ofport->up.netdev);
2805 bool cfm_enable = false;
2806 bool bfd_enable = false;
2808 ofport->carrier_seq = carrier_seq;
2811 int cfm_opup = cfm_get_opup(ofport->cfm);
2813 cfm_enable = !cfm_get_fault(ofport->cfm);
2815 if (cfm_opup >= 0) {
2816 cfm_enable = cfm_enable && cfm_opup;
2821 bfd_enable = bfd_forwarding(ofport->bfd);
2824 if (ofport->bfd || ofport->cfm) {
2825 enable = enable && (cfm_enable || bfd_enable);
2828 if (ofport->bundle) {
2829 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2830 if (carrier_changed) {
2831 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2835 if (ofport->may_enable != enable) {
2836 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2837 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2840 ofport->may_enable = enable;
2844 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2845 struct ofproto_port *ofproto_port)
2847 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2848 struct dpif_port dpif_port;
2851 if (sset_contains(&ofproto->ghost_ports, devname)) {
2852 const char *type = netdev_get_type_from_name(devname);
2854 /* We may be called before ofproto->up.port_by_name is populated with
2855 * the appropriate ofport. For this reason, we must get the name and
2856 * type from the netdev layer directly. */
2858 const struct ofport *ofport;
2860 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2861 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2862 ofproto_port->name = xstrdup(devname);
2863 ofproto_port->type = xstrdup(type);
2869 if (!sset_contains(&ofproto->ports, devname)) {
2872 error = dpif_port_query_by_name(ofproto->backer->dpif,
2873 devname, &dpif_port);
2875 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
2881 port_add(struct ofproto *ofproto_, struct netdev *netdev)
2883 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2884 const char *devname = netdev_get_name(netdev);
2885 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
2886 const char *dp_port_name;
2888 if (netdev_vport_is_patch(netdev)) {
2889 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
2893 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
2894 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
2895 odp_port_t port_no = ODPP_NONE;
2898 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
2902 if (netdev_get_tunnel_config(netdev)) {
2903 simap_put(&ofproto->backer->tnl_backers,
2904 dp_port_name, odp_to_u32(port_no));
2908 if (netdev_get_tunnel_config(netdev)) {
2909 sset_add(&ofproto->ghost_ports, devname);
2911 sset_add(&ofproto->ports, devname);
2917 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
2919 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2920 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
2927 sset_find_and_delete(&ofproto->ghost_ports,
2928 netdev_get_name(ofport->up.netdev));
2929 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2930 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
2931 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
2933 /* The caller is going to close ofport->up.netdev. If this is a
2934 * bonded port, then the bond is using that netdev, so remove it
2935 * from the bond. The client will need to reconfigure everything
2936 * after deleting ports, so then the slave will get re-added. */
2937 bundle_remove(&ofport->up);
2944 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
2946 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2949 error = netdev_get_stats(ofport->up.netdev, stats);
2951 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
2952 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2954 ovs_mutex_lock(&ofproto->stats_mutex);
2955 /* ofproto->stats.tx_packets represents packets that we created
2956 * internally and sent to some port (e.g. packets sent with
2957 * ofproto_dpif_send_packet()). Account for them as if they had
2958 * come from OFPP_LOCAL and got forwarded. */
2960 if (stats->rx_packets != UINT64_MAX) {
2961 stats->rx_packets += ofproto->stats.tx_packets;
2964 if (stats->rx_bytes != UINT64_MAX) {
2965 stats->rx_bytes += ofproto->stats.tx_bytes;
2968 /* ofproto->stats.rx_packets represents packets that were received on
2969 * some port and we processed internally and dropped (e.g. STP).
2970 * Account for them as if they had been forwarded to OFPP_LOCAL. */
2972 if (stats->tx_packets != UINT64_MAX) {
2973 stats->tx_packets += ofproto->stats.rx_packets;
2976 if (stats->tx_bytes != UINT64_MAX) {
2977 stats->tx_bytes += ofproto->stats.rx_bytes;
2979 ovs_mutex_unlock(&ofproto->stats_mutex);
2985 struct port_dump_state {
2990 struct ofproto_port port;
2995 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
2997 *statep = xzalloc(sizeof(struct port_dump_state));
3002 port_dump_next(const struct ofproto *ofproto_, void *state_,
3003 struct ofproto_port *port)
3005 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3006 struct port_dump_state *state = state_;
3007 const struct sset *sset;
3008 struct sset_node *node;
3010 if (state->has_port) {
3011 ofproto_port_destroy(&state->port);
3012 state->has_port = false;
3014 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3015 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3018 error = port_query_by_name(ofproto_, node->name, &state->port);
3020 *port = state->port;
3021 state->has_port = true;
3023 } else if (error != ENODEV) {
3028 if (!state->ghost) {
3029 state->ghost = true;
3032 return port_dump_next(ofproto_, state_, port);
3039 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3041 struct port_dump_state *state = state_;
3043 if (state->has_port) {
3044 ofproto_port_destroy(&state->port);
3051 port_poll(const struct ofproto *ofproto_, char **devnamep)
3053 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3055 if (ofproto->port_poll_errno) {
3056 int error = ofproto->port_poll_errno;
3057 ofproto->port_poll_errno = 0;
3061 if (sset_is_empty(&ofproto->port_poll_set)) {
3065 *devnamep = sset_pop(&ofproto->port_poll_set);
3070 port_poll_wait(const struct ofproto *ofproto_)
3072 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3073 dpif_port_poll_wait(ofproto->backer->dpif);
3077 port_is_lacp_current(const struct ofport *ofport_)
3079 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3080 return (ofport->bundle && ofport->bundle->lacp
3081 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3085 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3086 * then delete it entirely. */
3088 rule_expire(struct rule_dpif *rule)
3089 OVS_REQUIRES(ofproto_mutex)
3091 uint16_t hard_timeout, idle_timeout;
3092 long long int now = time_msec();
3095 ovs_assert(!rule->up.pending);
3097 hard_timeout = rule->up.hard_timeout;
3098 idle_timeout = rule->up.idle_timeout;
3100 /* Has 'rule' expired? */
3102 long long int modified;
3104 ovs_mutex_lock(&rule->up.mutex);
3105 modified = rule->up.modified;
3106 ovs_mutex_unlock(&rule->up.mutex);
3108 if (now > modified + hard_timeout * 1000) {
3109 reason = OFPRR_HARD_TIMEOUT;
3113 if (reason < 0 && idle_timeout) {
3116 ovs_mutex_lock(&rule->stats_mutex);
3117 used = rule->stats.used;
3118 ovs_mutex_unlock(&rule->stats_mutex);
3120 if (now > used + idle_timeout * 1000) {
3121 reason = OFPRR_IDLE_TIMEOUT;
3126 COVERAGE_INC(ofproto_dpif_expired);
3127 ofproto_rule_expire(&rule->up, reason);
3131 /* Executes, within 'ofproto', the actions in 'rule' or 'ofpacts' on 'packet'.
3132 * 'flow' must reflect the data in 'packet'. */
3134 ofproto_dpif_execute_actions(struct ofproto_dpif *ofproto,
3135 const struct flow *flow,
3136 struct rule_dpif *rule,
3137 const struct ofpact *ofpacts, size_t ofpacts_len,
3138 struct ofpbuf *packet)
3140 struct dpif_flow_stats stats;
3141 struct xlate_out xout;
3142 struct xlate_in xin;
3144 struct dpif_execute execute;
3147 ovs_assert((rule != NULL) != (ofpacts != NULL));
3149 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
3152 rule_dpif_credit_stats(rule, &stats);
3155 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
3156 xin.ofpacts = ofpacts;
3157 xin.ofpacts_len = ofpacts_len;
3158 xin.resubmit_stats = &stats;
3159 xlate_actions(&xin, &xout);
3161 execute.actions = ofpbuf_data(&xout.odp_actions);
3162 execute.actions_len = ofpbuf_size(&xout.odp_actions);
3163 execute.packet = packet;
3164 execute.md = pkt_metadata_from_flow(flow);
3165 execute.needs_help = (xout.slow & SLOW_ACTION) != 0;
3167 /* Fix up in_port. */
3168 in_port = flow->in_port.ofp_port;
3169 if (in_port == OFPP_NONE) {
3170 in_port = OFPP_LOCAL;
3172 execute.md.in_port.odp_port = ofp_port_to_odp_port(ofproto, in_port);
3174 error = dpif_execute(ofproto->backer->dpif, &execute);
3176 xlate_out_uninit(&xout);
3182 rule_dpif_credit_stats(struct rule_dpif *rule,
3183 const struct dpif_flow_stats *stats)
3185 ovs_mutex_lock(&rule->stats_mutex);
3186 rule->stats.n_packets += stats->n_packets;
3187 rule->stats.n_bytes += stats->n_bytes;
3188 rule->stats.used = MAX(rule->stats.used, stats->used);
3189 ovs_mutex_unlock(&rule->stats_mutex);
3193 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
3194 OVS_REQUIRES(rule->up.mutex)
3196 return rule->up.flow_cookie;
3200 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
3201 uint16_t hard_timeout)
3203 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
3206 /* Returns 'rule''s actions. The caller owns a reference on the returned
3207 * actions and must eventually release it (with rule_actions_unref()) to avoid
3209 const struct rule_actions *
3210 rule_dpif_get_actions(const struct rule_dpif *rule)
3212 return rule_get_actions(&rule->up);
3215 /* Lookup 'flow' in table 0 of 'ofproto''s classifier.
3216 * If 'wc' is non-null, sets the fields that were relevant as part of
3217 * the lookup. Returns the table_id where a match or miss occurred.
3219 * The return value will be zero unless there was a miss and
3220 * OFPTC11_TABLE_MISS_CONTINUE is in effect for the sequence of tables
3221 * where misses occur.
3223 * The rule is returned in '*rule', which is valid at least until the next
3224 * RCU quiescent period. If the '*rule' needs to stay around longer,
3225 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3226 * on it before this returns. */
3228 rule_dpif_lookup(struct ofproto_dpif *ofproto, struct flow *flow,
3229 struct flow_wildcards *wc, struct rule_dpif **rule,
3232 enum rule_dpif_lookup_verdict verdict;
3233 enum ofputil_port_config config = 0;
3236 if (ofproto_dpif_get_enable_recirc(ofproto)) {
3237 /* Always exactly match recirc_id since datapath supports
3240 wc->masks.recirc_id = UINT32_MAX;
3242 table_id = rule_dpif_lookup_get_init_table_id(flow);
3247 verdict = rule_dpif_lookup_from_table(ofproto, flow, wc, true,
3248 &table_id, rule, take_ref);
3251 case RULE_DPIF_LOOKUP_VERDICT_MATCH:
3253 case RULE_DPIF_LOOKUP_VERDICT_CONTROLLER: {
3254 struct ofport_dpif *port;
3256 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
3258 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
3259 flow->in_port.ofp_port);
3261 config = port ? port->up.pp.config : 0;
3264 case RULE_DPIF_LOOKUP_VERDICT_DROP:
3265 config = OFPUTIL_PC_NO_PACKET_IN;
3267 case RULE_DPIF_LOOKUP_VERDICT_DEFAULT:
3268 if (!connmgr_wants_packet_in_on_miss(ofproto->up.connmgr)) {
3269 config = OFPUTIL_PC_NO_PACKET_IN;
3276 choose_miss_rule(config, ofproto->miss_rule,
3277 ofproto->no_packet_in_rule, rule, take_ref);
3281 /* The returned rule is valid at least until the next RCU quiescent period.
3282 * If the '*rule' needs to stay around longer, a non-zero 'take_ref' must be
3283 * passed in to cause a reference to be taken on it before this returns. */
3284 static struct rule_dpif *
3285 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto, uint8_t table_id,
3286 const struct flow *flow, struct flow_wildcards *wc,
3289 struct classifier *cls = &ofproto->up.tables[table_id].cls;
3290 const struct cls_rule *cls_rule;
3291 struct rule_dpif *rule;
3293 fat_rwlock_rdlock(&cls->rwlock);
3294 if (ofproto->up.frag_handling != OFPC_FRAG_NX_MATCH) {
3296 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
3297 if (is_ip_any(flow)) {
3298 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
3302 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
3303 if (ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
3304 /* We must pretend that transport ports are unavailable. */
3305 struct flow ofpc_normal_flow = *flow;
3306 ofpc_normal_flow.tp_src = htons(0);
3307 ofpc_normal_flow.tp_dst = htons(0);
3308 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
3310 /* Must be OFPC_FRAG_DROP (we don't have OFPC_FRAG_REASM). */
3311 cls_rule = &ofproto->drop_frags_rule->up.cr;
3314 cls_rule = classifier_lookup(cls, flow, wc);
3317 cls_rule = classifier_lookup(cls, flow, wc);
3320 rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
3322 rule_dpif_ref(rule);
3324 fat_rwlock_unlock(&cls->rwlock);
3329 /* Look up 'flow' in 'ofproto''s classifier starting from table '*table_id'.
3330 * Stores the rule that was found in '*rule', or NULL if none was found.
3331 * Updates 'wc', if nonnull, to reflect the fields that were used during the
3334 * If 'honor_table_miss' is true, the first lookup occurs in '*table_id', but
3335 * if none is found then the table miss configuration for that table is
3336 * honored, which can result in additional lookups in other OpenFlow tables.
3337 * In this case the function updates '*table_id' to reflect the final OpenFlow
3338 * table that was searched.
3340 * If 'honor_table_miss' is false, then only one table lookup occurs, in
3345 * - RULE_DPIF_LOOKUP_VERDICT_MATCH if a rule (in '*rule') was found.
3347 * - RULE_OFPTC_TABLE_MISS_CONTROLLER if no rule was found and either:
3348 * + 'honor_table_miss' is false
3349 * + a table miss configuration specified that the packet should be
3350 * sent to the controller in this case.
3352 * - RULE_DPIF_LOOKUP_VERDICT_DROP if no rule was found, 'honor_table_miss'
3353 * is true and a table miss configuration specified that the packet
3354 * should be dropped in this case.
3356 * - RULE_DPIF_LOOKUP_VERDICT_DEFAULT if no rule was found,
3357 * 'honor_table_miss' is true and a table miss configuration has
3358 * not been specified in this case.
3360 * The rule is returned in '*rule', which is valid at least until the next
3361 * RCU quiescent period. If the '*rule' needs to stay around longer,
3362 * a non-zero 'take_ref' must be passed in to cause a reference to be taken
3363 * on it before this returns. */
3364 enum rule_dpif_lookup_verdict
3365 rule_dpif_lookup_from_table(struct ofproto_dpif *ofproto,
3366 const struct flow *flow,
3367 struct flow_wildcards *wc,
3368 bool honor_table_miss,
3369 uint8_t *table_id, struct rule_dpif **rule,
3374 for (next_id = *table_id;
3375 next_id < ofproto->up.n_tables;
3376 next_id++, next_id += (next_id == TBL_INTERNAL))
3378 *table_id = next_id;
3379 *rule = rule_dpif_lookup_in_table(ofproto, *table_id, flow, wc,
3382 return RULE_DPIF_LOOKUP_VERDICT_MATCH;
3383 } else if (!honor_table_miss) {
3384 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3386 switch (ofproto_table_get_config(&ofproto->up, *table_id)) {
3387 case OFPROTO_TABLE_MISS_CONTINUE:
3390 case OFPROTO_TABLE_MISS_CONTROLLER:
3391 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3393 case OFPROTO_TABLE_MISS_DROP:
3394 return RULE_DPIF_LOOKUP_VERDICT_DROP;
3396 case OFPROTO_TABLE_MISS_DEFAULT:
3397 return RULE_DPIF_LOOKUP_VERDICT_DEFAULT;
3402 return RULE_DPIF_LOOKUP_VERDICT_CONTROLLER;
3405 /* Given a port configuration (specified as zero if there's no port), chooses
3406 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
3409 * The rule is returned in '*rule', which is valid at least until the next
3410 * RCU quiescent period. If the '*rule' needs to stay around longer,
3411 * a reference must be taken on it (rule_dpif_ref()).
3414 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
3415 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule,
3418 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
3420 rule_dpif_ref(*rule);
3425 complete_operation(struct rule_dpif *rule)
3426 OVS_REQUIRES(ofproto_mutex)
3428 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3430 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3431 ofoperation_complete(rule->up.pending, 0);
3434 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
3436 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
3439 static struct rule *
3442 struct rule_dpif *rule = xmalloc(sizeof *rule);
3447 rule_dealloc(struct rule *rule_)
3449 struct rule_dpif *rule = rule_dpif_cast(rule_);
3454 rule_construct(struct rule *rule_)
3455 OVS_NO_THREAD_SAFETY_ANALYSIS
3457 struct rule_dpif *rule = rule_dpif_cast(rule_);
3458 ovs_mutex_init_adaptive(&rule->stats_mutex);
3459 rule->stats.n_packets = 0;
3460 rule->stats.n_bytes = 0;
3461 rule->stats.used = rule->up.modified;
3466 rule_insert(struct rule *rule_)
3467 OVS_REQUIRES(ofproto_mutex)
3469 struct rule_dpif *rule = rule_dpif_cast(rule_);
3470 complete_operation(rule);
3474 rule_delete(struct rule *rule_)
3475 OVS_REQUIRES(ofproto_mutex)
3477 struct rule_dpif *rule = rule_dpif_cast(rule_);
3478 complete_operation(rule);
3482 rule_destruct(struct rule *rule_)
3484 struct rule_dpif *rule = rule_dpif_cast(rule_);
3485 ovs_mutex_destroy(&rule->stats_mutex);
3489 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes,
3490 long long int *used)
3492 struct rule_dpif *rule = rule_dpif_cast(rule_);
3494 ovs_mutex_lock(&rule->stats_mutex);
3495 *packets = rule->stats.n_packets;
3496 *bytes = rule->stats.n_bytes;
3497 *used = rule->stats.used;
3498 ovs_mutex_unlock(&rule->stats_mutex);
3502 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
3503 struct ofpbuf *packet)
3505 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3507 ofproto_dpif_execute_actions(ofproto, flow, rule, NULL, 0, packet);
3511 rule_execute(struct rule *rule, const struct flow *flow,
3512 struct ofpbuf *packet)
3514 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
3515 ofpbuf_delete(packet);
3520 rule_modify_actions(struct rule *rule_, bool reset_counters)
3521 OVS_REQUIRES(ofproto_mutex)
3523 struct rule_dpif *rule = rule_dpif_cast(rule_);
3525 if (reset_counters) {
3526 ovs_mutex_lock(&rule->stats_mutex);
3527 rule->stats.n_packets = 0;
3528 rule->stats.n_bytes = 0;
3529 ovs_mutex_unlock(&rule->stats_mutex);
3532 complete_operation(rule);
3535 static struct group_dpif *group_dpif_cast(const struct ofgroup *group)
3537 return group ? CONTAINER_OF(group, struct group_dpif, up) : NULL;
3540 static struct ofgroup *
3543 struct group_dpif *group = xzalloc(sizeof *group);
3548 group_dealloc(struct ofgroup *group_)
3550 struct group_dpif *group = group_dpif_cast(group_);
3555 group_construct_stats(struct group_dpif *group)
3556 OVS_REQUIRES(group->stats_mutex)
3558 group->packet_count = 0;
3559 group->byte_count = 0;
3560 if (!group->bucket_stats) {
3561 group->bucket_stats = xcalloc(group->up.n_buckets,
3562 sizeof *group->bucket_stats);
3564 memset(group->bucket_stats, 0, group->up.n_buckets *
3565 sizeof *group->bucket_stats);
3570 group_construct(struct ofgroup *group_)
3572 struct group_dpif *group = group_dpif_cast(group_);
3573 const struct ofputil_bucket *bucket;
3575 /* Prevent group chaining because our locking structure makes it hard to
3576 * implement deadlock-free. (See xlate_group_resource_check().) */
3577 LIST_FOR_EACH (bucket, list_node, &group->up.buckets) {
3578 const struct ofpact *a;
3580 OFPACT_FOR_EACH (a, bucket->ofpacts, bucket->ofpacts_len) {
3581 if (a->type == OFPACT_GROUP) {
3582 return OFPERR_OFPGMFC_CHAINING_UNSUPPORTED;
3587 ovs_mutex_init_adaptive(&group->stats_mutex);
3588 ovs_mutex_lock(&group->stats_mutex);
3589 group_construct_stats(group);
3590 ovs_mutex_unlock(&group->stats_mutex);
3595 group_destruct__(struct group_dpif *group)
3596 OVS_REQUIRES(group->stats_mutex)
3598 free(group->bucket_stats);
3599 group->bucket_stats = NULL;
3603 group_destruct(struct ofgroup *group_)
3605 struct group_dpif *group = group_dpif_cast(group_);
3606 ovs_mutex_lock(&group->stats_mutex);
3607 group_destruct__(group);
3608 ovs_mutex_unlock(&group->stats_mutex);
3609 ovs_mutex_destroy(&group->stats_mutex);
3613 group_modify(struct ofgroup *group_, struct ofgroup *victim_)
3615 struct ofproto_dpif *ofproto = ofproto_dpif_cast(group_->ofproto);
3616 struct group_dpif *group = group_dpif_cast(group_);
3617 struct group_dpif *victim = group_dpif_cast(victim_);
3619 ovs_mutex_lock(&group->stats_mutex);
3620 if (victim->up.n_buckets < group->up.n_buckets) {
3621 group_destruct__(group);
3623 group_construct_stats(group);
3624 ovs_mutex_unlock(&group->stats_mutex);
3626 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
3632 group_get_stats(const struct ofgroup *group_, struct ofputil_group_stats *ogs)
3634 struct group_dpif *group = group_dpif_cast(group_);
3636 ovs_mutex_lock(&group->stats_mutex);
3637 ogs->packet_count = group->packet_count;
3638 ogs->byte_count = group->byte_count;
3639 memcpy(ogs->bucket_stats, group->bucket_stats,
3640 group->up.n_buckets * sizeof *group->bucket_stats);
3641 ovs_mutex_unlock(&group->stats_mutex);
3647 group_dpif_lookup(struct ofproto_dpif *ofproto, uint32_t group_id,
3648 struct group_dpif **group)
3649 OVS_TRY_RDLOCK(true, (*group)->up.rwlock)
3651 struct ofgroup *ofgroup;
3655 found = ofproto_group_lookup(&ofproto->up, group_id, &ofgroup);
3656 *group = found ? group_dpif_cast(ofgroup) : NULL;
3662 group_dpif_release(struct group_dpif *group)
3663 OVS_RELEASES(group->up.rwlock)
3665 ofproto_group_release(&group->up);
3669 group_dpif_get_buckets(const struct group_dpif *group,
3670 const struct list **buckets)
3672 *buckets = &group->up.buckets;
3675 enum ofp11_group_type
3676 group_dpif_get_type(const struct group_dpif *group)
3678 return group->up.type;
3681 /* Sends 'packet' out 'ofport'.
3682 * May modify 'packet'.
3683 * Returns 0 if successful, otherwise a positive errno value. */
3685 ofproto_dpif_send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
3687 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3690 error = xlate_send_packet(ofport, packet);
3692 ovs_mutex_lock(&ofproto->stats_mutex);
3693 ofproto->stats.tx_packets++;
3694 ofproto->stats.tx_bytes += ofpbuf_size(packet);
3695 ovs_mutex_unlock(&ofproto->stats_mutex);
3700 set_frag_handling(struct ofproto *ofproto_,
3701 enum ofp_config_flags frag_handling)
3703 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3704 if (frag_handling != OFPC_FRAG_REASM) {
3705 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3713 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
3714 const struct flow *flow,
3715 const struct ofpact *ofpacts, size_t ofpacts_len)
3717 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3719 ofproto_dpif_execute_actions(ofproto, flow, NULL, ofpacts,
3720 ofpacts_len, packet);
3727 set_netflow(struct ofproto *ofproto_,
3728 const struct netflow_options *netflow_options)
3730 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3732 if (netflow_options) {
3733 if (!ofproto->netflow) {
3734 ofproto->netflow = netflow_create();
3735 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3737 return netflow_set_options(ofproto->netflow, netflow_options);
3738 } else if (ofproto->netflow) {
3739 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3740 netflow_unref(ofproto->netflow);
3741 ofproto->netflow = NULL;
3748 get_netflow_ids(const struct ofproto *ofproto_,
3749 uint8_t *engine_type, uint8_t *engine_id)
3751 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3753 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
3756 static struct ofproto_dpif *
3757 ofproto_dpif_lookup(const char *name)
3759 struct ofproto_dpif *ofproto;
3761 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
3762 hash_string(name, 0), &all_ofproto_dpifs) {
3763 if (!strcmp(ofproto->up.name, name)) {
3771 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
3772 const char *argv[], void *aux OVS_UNUSED)
3774 struct ofproto_dpif *ofproto;
3777 ofproto = ofproto_dpif_lookup(argv[1]);
3779 unixctl_command_reply_error(conn, "no such bridge");
3782 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3783 mac_learning_flush(ofproto->ml);
3784 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3786 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3787 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
3788 mac_learning_flush(ofproto->ml);
3789 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3793 unixctl_command_reply(conn, "table successfully flushed");
3796 static struct ofport_dpif *
3797 ofbundle_get_a_port(const struct ofbundle *bundle)
3799 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
3804 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
3805 const char *argv[], void *aux OVS_UNUSED)
3807 struct ds ds = DS_EMPTY_INITIALIZER;
3808 const struct ofproto_dpif *ofproto;
3809 const struct mac_entry *e;
3811 ofproto = ofproto_dpif_lookup(argv[1]);
3813 unixctl_command_reply_error(conn, "no such bridge");
3817 ds_put_cstr(&ds, " port VLAN MAC Age\n");
3818 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
3819 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
3820 struct ofbundle *bundle = e->port.p;
3821 char name[OFP_MAX_PORT_NAME_LEN];
3823 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
3825 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
3826 name, e->vlan, ETH_ADDR_ARGS(e->mac),
3827 mac_entry_age(ofproto->ml, e));
3829 ovs_rwlock_unlock(&ofproto->ml->rwlock);
3830 unixctl_command_reply(conn, ds_cstr(&ds));
3835 struct xlate_out xout;
3836 struct xlate_in xin;
3837 const struct flow *key;
3839 struct flow_wildcards wc;
3844 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
3846 const struct rule_actions *actions;
3849 ds_put_char_multiple(result, '\t', level);
3851 ds_put_cstr(result, "No match\n");
3855 ovs_mutex_lock(&rule->up.mutex);
3856 cookie = rule->up.flow_cookie;
3857 ovs_mutex_unlock(&rule->up.mutex);
3859 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
3860 rule ? rule->up.table_id : 0, ntohll(cookie));
3861 cls_rule_format(&rule->up.cr, result);
3862 ds_put_char(result, '\n');
3864 actions = rule_dpif_get_actions(rule);
3866 ds_put_char_multiple(result, '\t', level);
3867 ds_put_cstr(result, "OpenFlow actions=");
3868 ofpacts_format(actions->ofpacts, actions->ofpacts_len, result);
3869 ds_put_char(result, '\n');
3873 trace_format_flow(struct ds *result, int level, const char *title,
3874 struct trace_ctx *trace)
3876 ds_put_char_multiple(result, '\t', level);
3877 ds_put_format(result, "%s: ", title);
3878 /* Do not report unchanged flows for resubmits. */
3879 if ((level > 0 && flow_equal(&trace->xin.flow, &trace->flow))
3880 || (level == 0 && flow_equal(&trace->xin.flow, trace->key))) {
3881 ds_put_cstr(result, "unchanged");
3883 flow_format(result, &trace->xin.flow);
3884 trace->flow = trace->xin.flow;
3886 ds_put_char(result, '\n');
3890 trace_format_regs(struct ds *result, int level, const char *title,
3891 struct trace_ctx *trace)
3895 ds_put_char_multiple(result, '\t', level);
3896 ds_put_format(result, "%s:", title);
3897 for (i = 0; i < FLOW_N_REGS; i++) {
3898 ds_put_format(result, " reg%"PRIuSIZE"=0x%"PRIx32, i, trace->flow.regs[i]);
3900 ds_put_char(result, '\n');
3904 trace_format_odp(struct ds *result, int level, const char *title,
3905 struct trace_ctx *trace)
3907 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
3909 ds_put_char_multiple(result, '\t', level);
3910 ds_put_format(result, "%s: ", title);
3911 format_odp_actions(result, ofpbuf_data(odp_actions),
3912 ofpbuf_size(odp_actions));
3913 ds_put_char(result, '\n');
3917 trace_format_megaflow(struct ds *result, int level, const char *title,
3918 struct trace_ctx *trace)
3922 ds_put_char_multiple(result, '\t', level);
3923 ds_put_format(result, "%s: ", title);
3924 flow_wildcards_or(&trace->wc, &trace->xout.wc, &trace->wc);
3925 match_init(&match, trace->key, &trace->wc);
3926 match_format(&match, result, OFP_DEFAULT_PRIORITY);
3927 ds_put_char(result, '\n');
3931 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
3933 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3934 struct ds *result = trace->result;
3936 ds_put_char(result, '\n');
3937 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
3938 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
3939 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
3940 trace_format_megaflow(result, recurse + 1, "Resubmitted megaflow", trace);
3941 trace_format_rule(result, recurse + 1, rule);
3945 trace_report(struct xlate_in *xin, const char *s, int recurse)
3947 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
3948 struct ds *result = trace->result;
3950 ds_put_char_multiple(result, '\t', recurse);
3951 ds_put_cstr(result, s);
3952 ds_put_char(result, '\n');
3955 /* Parses the 'argc' elements of 'argv', ignoring argv[0]. The following
3956 * forms are supported:
3958 * - [dpname] odp_flow [-generate | packet]
3959 * - bridge br_flow [-generate | packet]
3961 * On success, initializes '*ofprotop' and 'flow' and returns NULL. On failure
3962 * returns a nonnull malloced error message. */
3963 static char * WARN_UNUSED_RESULT
3964 parse_flow_and_packet(int argc, const char *argv[],
3965 struct ofproto_dpif **ofprotop, struct flow *flow,
3966 struct ofpbuf **packetp)
3968 const struct dpif_backer *backer = NULL;
3969 const char *error = NULL;
3971 struct simap port_names = SIMAP_INITIALIZER(&port_names);
3972 struct ofpbuf *packet;
3973 struct ofpbuf odp_key;
3974 struct ofpbuf odp_mask;
3976 ofpbuf_init(&odp_key, 0);
3977 ofpbuf_init(&odp_mask, 0);
3979 /* Handle "-generate" or a hex string as the last argument. */
3980 if (!strcmp(argv[argc - 1], "-generate")) {
3981 packet = ofpbuf_new(0);
3984 error = eth_from_hex(argv[argc - 1], &packet);
3987 } else if (argc == 4) {
3988 /* The 3-argument form must end in "-generate' or a hex string. */
3994 /* odp_flow can have its in_port specified as a name instead of port no.
3995 * We do not yet know whether a given flow is a odp_flow or a br_flow.
3996 * But, to know whether a flow is odp_flow through odp_flow_from_string(),
3997 * we need to create a simap of name to port no. */
3999 const char *dp_type;
4000 if (!strncmp(argv[1], "ovs-", 4)) {
4001 dp_type = argv[1] + 4;
4005 backer = shash_find_data(&all_dpif_backers, dp_type);
4006 } else if (argc == 2) {
4007 struct shash_node *node;
4008 if (shash_count(&all_dpif_backers) == 1) {
4009 node = shash_first(&all_dpif_backers);
4010 backer = node->data;
4013 error = "Syntax error";
4016 if (backer && backer->dpif) {
4017 struct dpif_port dpif_port;
4018 struct dpif_port_dump port_dump;
4019 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, backer->dpif) {
4020 simap_put(&port_names, dpif_port.name,
4021 odp_to_u32(dpif_port.port_no));
4025 /* Parse the flow and determine whether a datapath or
4026 * bridge is specified. If function odp_flow_key_from_string()
4027 * returns 0, the flow is a odp_flow. If function
4028 * parse_ofp_exact_flow() returns NULL, the flow is a br_flow. */
4029 if (!odp_flow_from_string(argv[argc - 1], &port_names,
4030 &odp_key, &odp_mask)) {
4032 error = "Cannot find the datapath";
4036 if (xlate_receive(backer, NULL, ofpbuf_data(&odp_key),
4037 ofpbuf_size(&odp_key), flow,
4038 ofprotop, NULL, NULL, NULL, NULL)) {
4039 error = "Invalid datapath flow";
4043 char *err = parse_ofp_exact_flow(flow, NULL, argv[argc - 1], NULL);
4046 m_err = xasprintf("Bad flow syntax: %s", err);
4051 error = "Must specify bridge name";
4055 *ofprotop = ofproto_dpif_lookup(argv[1]);
4057 error = "Unknown bridge name";
4063 /* Generate a packet, if requested. */
4065 if (!ofpbuf_size(packet)) {
4066 flow_compose(packet, flow);
4068 struct pkt_metadata md = pkt_metadata_from_flow(flow);
4070 /* Use the metadata from the flow and the packet argument
4071 * to reconstruct the flow. */
4072 flow_extract(packet, &md, flow);
4077 if (error && !m_err) {
4078 m_err = xstrdup(error);
4081 ofpbuf_delete(packet);
4085 ofpbuf_uninit(&odp_key);
4086 ofpbuf_uninit(&odp_mask);
4087 simap_destroy(&port_names);
4092 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
4093 void *aux OVS_UNUSED)
4095 struct ofproto_dpif *ofproto;
4096 struct ofpbuf *packet;
4100 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4105 ofproto_trace(ofproto, &flow, packet, NULL, 0, &result);
4106 unixctl_command_reply(conn, ds_cstr(&result));
4107 ds_destroy(&result);
4108 ofpbuf_delete(packet);
4110 unixctl_command_reply_error(conn, error);
4116 ofproto_unixctl_trace_actions(struct unixctl_conn *conn, int argc,
4117 const char *argv[], void *aux OVS_UNUSED)
4119 enum ofputil_protocol usable_protocols;
4120 struct ofproto_dpif *ofproto;
4121 bool enforce_consistency;
4122 struct ofpbuf ofpacts;
4123 struct ofpbuf *packet;
4128 /* Three kinds of error return values! */
4134 ofpbuf_init(&ofpacts, 0);
4136 /* Parse actions. */
4137 error = parse_ofpacts(argv[--argc], &ofpacts, &usable_protocols);
4139 unixctl_command_reply_error(conn, error);
4144 /* OpenFlow 1.1 and later suggest that the switch enforces certain forms of
4145 * consistency between the flow and the actions. With -consistent, we
4146 * enforce consistency even for a flow supported in OpenFlow 1.0. */
4147 if (!strcmp(argv[1], "-consistent")) {
4148 enforce_consistency = true;
4152 enforce_consistency = false;
4155 error = parse_flow_and_packet(argc, argv, &ofproto, &flow, &packet);
4157 unixctl_command_reply_error(conn, error);
4162 /* Do the same checks as handle_packet_out() in ofproto.c.
4164 * We pass a 'table_id' of 0 to ofproto_check_ofpacts(), which isn't
4165 * strictly correct because these actions aren't in any table, but it's OK
4166 * because it 'table_id' is used only to check goto_table instructions, but
4167 * packet-outs take a list of actions and therefore it can't include
4170 * We skip the "meter" check here because meter is an instruction, not an
4171 * action, and thus cannot appear in ofpacts. */
4172 in_port = ofp_to_u16(flow.in_port.ofp_port);
4173 if (in_port >= ofproto->up.max_ports && in_port < ofp_to_u16(OFPP_MAX)) {
4174 unixctl_command_reply_error(conn, "invalid in_port");
4177 if (enforce_consistency) {
4178 retval = ofpacts_check_consistency(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts),
4179 &flow, u16_to_ofp(ofproto->up.max_ports),
4180 0, 0, usable_protocols);
4182 retval = ofpacts_check(ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &flow,
4183 u16_to_ofp(ofproto->up.max_ports), 0, 0,
4189 ds_put_format(&result, "Bad actions: %s", ofperr_to_string(retval));
4190 unixctl_command_reply_error(conn, ds_cstr(&result));
4194 ofproto_trace(ofproto, &flow, packet,
4195 ofpbuf_data(&ofpacts), ofpbuf_size(&ofpacts), &result);
4196 unixctl_command_reply(conn, ds_cstr(&result));
4199 ds_destroy(&result);
4200 ofpbuf_delete(packet);
4201 ofpbuf_uninit(&ofpacts);
4204 /* Implements a "trace" through 'ofproto''s flow table, appending a textual
4205 * description of the results to 'ds'.
4207 * The trace follows a packet with the specified 'flow' through the flow
4208 * table. 'packet' may be nonnull to trace an actual packet, with consequent
4209 * side effects (if it is nonnull then its flow must be 'flow').
4211 * If 'ofpacts' is nonnull then its 'ofpacts_len' bytes specify the actions to
4212 * trace, otherwise the actions are determined by a flow table lookup. */
4214 ofproto_trace(struct ofproto_dpif *ofproto, struct flow *flow,
4215 const struct ofpbuf *packet,
4216 const struct ofpact ofpacts[], size_t ofpacts_len,
4219 struct rule_dpif *rule;
4220 struct trace_ctx trace;
4222 ds_put_format(ds, "Bridge: %s\n", ofproto->up.name);
4223 ds_put_cstr(ds, "Flow: ");
4224 flow_format(ds, flow);
4225 ds_put_char(ds, '\n');
4227 flow_wildcards_init_catchall(&trace.wc);
4231 rule_dpif_lookup(ofproto, flow, &trace.wc, &rule, false);
4233 trace_format_rule(ds, 0, rule);
4234 if (rule == ofproto->miss_rule) {
4235 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
4236 } else if (rule == ofproto->no_packet_in_rule) {
4237 ds_put_cstr(ds, "\nNo match, packets dropped because "
4238 "OFPPC_NO_PACKET_IN is set on in_port.\n");
4239 } else if (rule == ofproto->drop_frags_rule) {
4240 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
4241 "and the fragment handling mode is \"drop\".\n");
4245 if (rule || ofpacts) {
4247 trace.key = flow; /* Original flow key, used for megaflow. */
4248 trace.flow = *flow; /* May be modified by actions. */
4249 xlate_in_init(&trace.xin, ofproto, flow, rule, ntohs(flow->tcp_flags),
4252 trace.xin.ofpacts = ofpacts;
4253 trace.xin.ofpacts_len = ofpacts_len;
4255 trace.xin.resubmit_hook = trace_resubmit;
4256 trace.xin.report_hook = trace_report;
4258 xlate_actions(&trace.xin, &trace.xout);
4260 ds_put_char(ds, '\n');
4261 trace_format_flow(ds, 0, "Final flow", &trace);
4262 trace_format_megaflow(ds, 0, "Megaflow", &trace);
4264 ds_put_cstr(ds, "Datapath actions: ");
4265 format_odp_actions(ds, ofpbuf_data(&trace.xout.odp_actions),
4266 ofpbuf_size(&trace.xout.odp_actions));
4268 if (trace.xout.slow) {
4269 enum slow_path_reason slow;
4271 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
4272 "slow path because it:");
4274 slow = trace.xout.slow;
4276 enum slow_path_reason bit = rightmost_1bit(slow);
4278 ds_put_format(ds, "\n\t- %s.",
4279 slow_path_reason_to_explanation(bit));
4285 xlate_out_uninit(&trace.xout);
4289 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
4290 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
4291 * to destroy 'ofproto_shash' and free the returned value. */
4292 static const struct shash_node **
4293 get_ofprotos(struct shash *ofproto_shash)
4295 const struct ofproto_dpif *ofproto;
4297 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4298 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
4299 shash_add_nocopy(ofproto_shash, name, ofproto);
4302 return shash_sort(ofproto_shash);
4306 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
4307 const char *argv[] OVS_UNUSED,
4308 void *aux OVS_UNUSED)
4310 struct ds ds = DS_EMPTY_INITIALIZER;
4311 struct shash ofproto_shash;
4312 const struct shash_node **sorted_ofprotos;
4315 shash_init(&ofproto_shash);
4316 sorted_ofprotos = get_ofprotos(&ofproto_shash);
4317 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4318 const struct shash_node *node = sorted_ofprotos[i];
4319 ds_put_format(&ds, "%s\n", node->name);
4322 shash_destroy(&ofproto_shash);
4323 free(sorted_ofprotos);
4325 unixctl_command_reply(conn, ds_cstr(&ds));
4330 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
4332 const struct shash_node **ofprotos;
4333 struct dpif_dp_stats dp_stats;
4334 struct shash ofproto_shash;
4337 dpif_get_dp_stats(backer->dpif, &dp_stats);
4339 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
4340 dpif_name(backer->dpif), dp_stats.n_hit, dp_stats.n_missed);
4342 shash_init(&ofproto_shash);
4343 ofprotos = get_ofprotos(&ofproto_shash);
4344 for (i = 0; i < shash_count(&ofproto_shash); i++) {
4345 struct ofproto_dpif *ofproto = ofprotos[i]->data;
4346 const struct shash_node **ports;
4349 if (ofproto->backer != backer) {
4353 ds_put_format(ds, "\t%s:\n", ofproto->up.name);
4355 ports = shash_sort(&ofproto->up.port_by_name);
4356 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
4357 const struct shash_node *node = ports[j];
4358 struct ofport *ofport = node->data;
4360 odp_port_t odp_port;
4362 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
4365 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
4366 if (odp_port != ODPP_NONE) {
4367 ds_put_format(ds, "%"PRIu32":", odp_port);
4369 ds_put_cstr(ds, "none:");
4372 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
4375 if (!netdev_get_config(ofport->netdev, &config)) {
4376 const struct smap_node **nodes;
4379 nodes = smap_sort(&config);
4380 for (i = 0; i < smap_count(&config); i++) {
4381 const struct smap_node *node = nodes[i];
4382 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
4383 node->key, node->value);
4387 smap_destroy(&config);
4389 ds_put_char(ds, ')');
4390 ds_put_char(ds, '\n');
4394 shash_destroy(&ofproto_shash);
4399 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
4400 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
4402 struct ds ds = DS_EMPTY_INITIALIZER;
4403 const struct shash_node **backers;
4406 backers = shash_sort(&all_dpif_backers);
4407 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
4408 dpif_show_backer(backers[i]->data, &ds);
4412 unixctl_command_reply(conn, ds_cstr(&ds));
4417 ofproto_dpif_contains_flow(const struct ofproto_dpif *ofproto,
4418 const struct nlattr *key, size_t key_len)
4420 struct ofproto_dpif *ofp;
4423 xlate_receive(ofproto->backer, NULL, key, key_len, &flow, &ofp,
4424 NULL, NULL, NULL, NULL);
4425 return ofp == ofproto;
4429 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
4430 int argc OVS_UNUSED, const char *argv[],
4431 void *aux OVS_UNUSED)
4433 struct ds ds = DS_EMPTY_INITIALIZER;
4434 const struct dpif_flow_stats *stats;
4435 const struct ofproto_dpif *ofproto;
4436 struct dpif_flow_dump flow_dump;
4437 const struct nlattr *actions;
4438 const struct nlattr *mask;
4439 const struct nlattr *key;
4443 bool verbosity = false;
4444 struct dpif_port dpif_port;
4445 struct dpif_port_dump port_dump;
4446 struct hmap portno_names;
4450 ofproto = ofproto_dpif_lookup(argv[argc - 1]);
4452 unixctl_command_reply_error(conn, "no such bridge");
4456 if (argc > 2 && !strcmp(argv[1], "-m")) {
4460 hmap_init(&portno_names);
4461 DPIF_PORT_FOR_EACH (&dpif_port, &port_dump, ofproto->backer->dpif) {
4462 odp_portno_names_set(&portno_names, dpif_port.port_no, dpif_port.name);
4466 error = dpif_flow_dump_start(&flow_dump, ofproto->backer->dpif);
4470 dpif_flow_dump_state_init(ofproto->backer->dpif, &state);
4471 while (dpif_flow_dump_next(&flow_dump, state, &key, &key_len,
4472 &mask, &mask_len, &actions, &actions_len,
4474 if (!ofproto_dpif_contains_flow(ofproto, key, key_len)) {
4478 odp_flow_format(key, key_len, mask, mask_len, &portno_names, &ds,
4480 ds_put_cstr(&ds, ", ");
4481 dpif_flow_stats_format(stats, &ds);
4482 ds_put_cstr(&ds, ", actions:");
4483 format_odp_actions(&ds, actions, actions_len);
4484 ds_put_char(&ds, '\n');
4486 dpif_flow_dump_state_uninit(ofproto->backer->dpif, state);
4487 error = dpif_flow_dump_done(&flow_dump);
4492 ds_put_format(&ds, "dpif/dump_flows failed: %s", ovs_strerror(errno));
4493 unixctl_command_reply_error(conn, ds_cstr(&ds));
4495 unixctl_command_reply(conn, ds_cstr(&ds));
4497 odp_portno_names_destroy(&portno_names);
4498 hmap_destroy(&portno_names);
4503 ofproto_dpif_unixctl_init(void)
4505 static bool registered;
4511 unixctl_command_register(
4513 "{[dp_name] odp_flow | bridge br_flow} [-generate|packet]",
4514 1, 3, ofproto_unixctl_trace, NULL);
4515 unixctl_command_register(
4516 "ofproto/trace-packet-out",
4517 "[-consistent] {[dp_name] odp_flow | bridge br_flow} [-generate|packet] actions",
4518 2, 6, ofproto_unixctl_trace_actions, NULL);
4519 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
4520 ofproto_unixctl_fdb_flush, NULL);
4521 unixctl_command_register("fdb/show", "bridge", 1, 1,
4522 ofproto_unixctl_fdb_show, NULL);
4523 unixctl_command_register("dpif/dump-dps", "", 0, 0,
4524 ofproto_unixctl_dpif_dump_dps, NULL);
4525 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
4527 unixctl_command_register("dpif/dump-flows", "[-m] bridge", 1, 2,
4528 ofproto_unixctl_dpif_dump_flows, NULL);
4531 /* Returns true if 'table' is the table used for internal rules,
4532 * false otherwise. */
4534 table_is_internal(uint8_t table_id)
4536 return table_id == TBL_INTERNAL;
4539 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
4541 * This is deprecated. It is only for compatibility with broken device drivers
4542 * in old versions of Linux that do not properly support VLANs when VLAN
4543 * devices are not used. When broken device drivers are no longer in
4544 * widespread use, we will delete these interfaces. */
4547 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
4549 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
4550 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
4552 if (realdev_ofp_port == ofport->realdev_ofp_port
4553 && vid == ofport->vlandev_vid) {
4557 ofproto->backer->need_revalidate = REV_RECONFIGURE;
4559 if (ofport->realdev_ofp_port) {
4562 if (realdev_ofp_port && ofport->bundle) {
4563 /* vlandevs are enslaved to their realdevs, so they are not allowed to
4564 * themselves be part of a bundle. */
4565 bundle_set(ofport_->ofproto, ofport->bundle, NULL);
4568 ofport->realdev_ofp_port = realdev_ofp_port;
4569 ofport->vlandev_vid = vid;
4571 if (realdev_ofp_port) {
4572 vsp_add(ofport, realdev_ofp_port, vid);
4579 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
4581 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
4585 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
4586 OVS_EXCLUDED(ofproto->vsp_mutex)
4588 /* hmap_is_empty is thread safe. */
4589 return !hmap_is_empty(&ofproto->realdev_vid_map);
4594 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
4595 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4596 OVS_REQUIRES(ofproto->vsp_mutex)
4598 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
4599 int vid = vlan_tci_to_vid(vlan_tci);
4600 const struct vlan_splinter *vsp;
4602 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
4603 hash_realdev_vid(realdev_ofp_port, vid),
4604 &ofproto->realdev_vid_map) {
4605 if (vsp->realdev_ofp_port == realdev_ofp_port
4606 && vsp->vid == vid) {
4607 return vsp->vlandev_ofp_port;
4611 return realdev_ofp_port;
4614 /* Returns the OFP port number of the Linux VLAN device that corresponds to
4615 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
4616 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
4617 * 'vlan_tci' 9, it would return the port number of eth0.9.
4619 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
4620 * function just returns its 'realdev_ofp_port' argument. */
4622 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
4623 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
4624 OVS_EXCLUDED(ofproto->vsp_mutex)
4628 /* hmap_is_empty is thread safe, see if we can return immediately. */
4629 if (hmap_is_empty(&ofproto->realdev_vid_map)) {
4630 return realdev_ofp_port;
4632 ovs_mutex_lock(&ofproto->vsp_mutex);
4633 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
4634 ovs_mutex_unlock(&ofproto->vsp_mutex);
4638 static struct vlan_splinter *
4639 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
4641 struct vlan_splinter *vsp;
4643 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
4644 hash_ofp_port(vlandev_ofp_port),
4645 &ofproto->vlandev_map) {
4646 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
4654 /* Returns the OpenFlow port number of the "real" device underlying the Linux
4655 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
4656 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
4657 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
4658 * eth0 and store 9 in '*vid'.
4660 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
4661 * VLAN device. Unless VLAN splinters are enabled, this is what this function
4664 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
4665 ofp_port_t vlandev_ofp_port, int *vid)
4666 OVS_REQUIRES(ofproto->vsp_mutex)
4668 if (!hmap_is_empty(&ofproto->vlandev_map)) {
4669 const struct vlan_splinter *vsp;
4671 vsp = vlandev_find(ofproto, vlandev_ofp_port);
4676 return vsp->realdev_ofp_port;
4682 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
4683 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
4684 * 'flow->in_port' to the "real" device backing the VLAN device, sets
4685 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
4686 * always the case unless VLAN splinters are enabled), returns false without
4687 * making any changes. */
4689 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
4690 OVS_EXCLUDED(ofproto->vsp_mutex)
4695 /* hmap_is_empty is thread safe. */
4696 if (hmap_is_empty(&ofproto->vlandev_map)) {
4700 ovs_mutex_lock(&ofproto->vsp_mutex);
4701 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
4702 ovs_mutex_unlock(&ofproto->vsp_mutex);
4707 /* Cause the flow to be processed as if it came in on the real device with
4708 * the VLAN device's VLAN ID. */
4709 flow->in_port.ofp_port = realdev;
4710 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
4715 vsp_remove(struct ofport_dpif *port)
4717 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4718 struct vlan_splinter *vsp;
4720 ovs_mutex_lock(&ofproto->vsp_mutex);
4721 vsp = vlandev_find(ofproto, port->up.ofp_port);
4723 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
4724 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
4727 port->realdev_ofp_port = 0;
4729 VLOG_ERR("missing vlan device record");
4731 ovs_mutex_unlock(&ofproto->vsp_mutex);
4735 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
4737 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
4739 ovs_mutex_lock(&ofproto->vsp_mutex);
4740 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
4741 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
4742 == realdev_ofp_port)) {
4743 struct vlan_splinter *vsp;
4745 vsp = xmalloc(sizeof *vsp);
4746 vsp->realdev_ofp_port = realdev_ofp_port;
4747 vsp->vlandev_ofp_port = port->up.ofp_port;
4750 port->realdev_ofp_port = realdev_ofp_port;
4752 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
4753 hash_ofp_port(port->up.ofp_port));
4754 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
4755 hash_realdev_vid(realdev_ofp_port, vid));
4757 VLOG_ERR("duplicate vlan device record");
4759 ovs_mutex_unlock(&ofproto->vsp_mutex);
4763 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
4765 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
4766 return ofport ? ofport->odp_port : ODPP_NONE;
4769 struct ofport_dpif *
4770 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
4772 struct ofport_dpif *port;
4774 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
4775 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
4776 &backer->odp_to_ofport_map) {
4777 if (port->odp_port == odp_port) {
4778 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4783 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
4788 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
4790 struct ofport_dpif *port;
4792 port = odp_port_to_ofport(ofproto->backer, odp_port);
4793 if (port && &ofproto->up == port->up.ofproto) {
4794 return port->up.ofp_port;
4800 struct ofproto_dpif *
4801 ofproto_dpif_recirc_get_ofproto(const struct dpif_backer *backer,
4804 struct dpif_backer_recirc_node *node;
4806 ovs_mutex_lock(&backer->recirc_mutex);
4807 node = CONTAINER_OF(hmap_first_with_hash(&backer->recirc_map, recirc_id),
4808 struct dpif_backer_recirc_node, hmap_node);
4809 ovs_mutex_unlock(&backer->recirc_mutex);
4811 return node ? node->ofproto : NULL;
4815 ofproto_dpif_alloc_recirc_id(struct ofproto_dpif *ofproto)
4817 struct dpif_backer *backer = ofproto->backer;
4818 uint32_t recirc_id = recirc_id_alloc(backer->rid_pool);
4821 struct dpif_backer_recirc_node *node = xmalloc(sizeof *node);
4823 node->recirc_id = recirc_id;
4824 node->ofproto = ofproto;
4826 ovs_mutex_lock(&backer->recirc_mutex);
4827 hmap_insert(&backer->recirc_map, &node->hmap_node, node->recirc_id);
4828 ovs_mutex_unlock(&backer->recirc_mutex);
4835 ofproto_dpif_free_recirc_id(struct ofproto_dpif *ofproto, uint32_t recirc_id)
4837 struct dpif_backer *backer = ofproto->backer;
4838 struct dpif_backer_recirc_node *node;
4840 ovs_mutex_lock(&backer->recirc_mutex);
4841 node = CONTAINER_OF(hmap_first_with_hash(&backer->recirc_map, recirc_id),
4842 struct dpif_backer_recirc_node, hmap_node);
4844 hmap_remove(&backer->recirc_map, &node->hmap_node);
4845 ovs_mutex_unlock(&backer->recirc_mutex);
4846 recirc_id_free(backer->rid_pool, node->recirc_id);
4848 if (node->ofproto != ofproto) {
4849 VLOG_ERR("recirc_id %"PRIu32", freed by incorrect ofproto (%s),"
4850 " expect ofproto (%s)", node->recirc_id, ofproto->up.name,
4851 node->ofproto->up.name);
4854 /* RCU postpone the free, since other threads may be referring
4855 * to 'node' at same time. */
4856 ovsrcu_postpone(free, node);
4858 ovs_mutex_unlock(&backer->recirc_mutex);
4863 ofproto_dpif_add_internal_flow(struct ofproto_dpif *ofproto,
4864 const struct match *match, int priority,
4865 const struct ofpbuf *ofpacts,
4866 struct rule **rulep)
4868 struct ofputil_flow_mod fm;
4869 struct rule_dpif *rule;
4873 fm.priority = priority;
4874 fm.new_cookie = htonll(0);
4875 fm.cookie = htonll(0);
4876 fm.cookie_mask = htonll(0);
4877 fm.modify_cookie = false;
4878 fm.table_id = TBL_INTERNAL;
4879 fm.command = OFPFC_ADD;
4880 fm.idle_timeout = 0;
4881 fm.hard_timeout = 0;
4884 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
4885 fm.ofpacts = ofpbuf_data(ofpacts);
4886 fm.ofpacts_len = ofpbuf_size(ofpacts);
4888 error = ofproto_flow_mod(&ofproto->up, &fm);
4890 VLOG_ERR_RL(&rl, "failed to add internal flow (%s)",
4891 ofperr_to_string(error));
4896 rule = rule_dpif_lookup_in_table(ofproto, TBL_INTERNAL, &fm.match.flow,
4897 &fm.match.wc, false);
4907 ofproto_dpif_delete_internal_flow(struct ofproto_dpif *ofproto,
4908 struct match *match, int priority)
4910 struct ofputil_flow_mod fm;
4914 fm.priority = priority;
4915 fm.new_cookie = htonll(0);
4916 fm.cookie = htonll(0);
4917 fm.cookie_mask = htonll(0);
4918 fm.modify_cookie = false;
4919 fm.table_id = TBL_INTERNAL;
4920 fm.flags = OFPUTIL_FF_HIDDEN_FIELDS | OFPUTIL_FF_NO_READONLY;
4921 fm.command = OFPFC_DELETE_STRICT;
4923 error = ofproto_flow_mod(&ofproto->up, &fm);
4925 VLOG_ERR_RL(&rl, "failed to delete internal flow (%s)",
4926 ofperr_to_string(error));
4933 const struct ofproto_class ofproto_dpif_class = {
4947 NULL, /* get_memory_usage. */
4948 type_get_memory_usage,
4967 port_is_lacp_current,
4968 NULL, /* rule_choose_table */
4977 rule_modify_actions,
4991 get_stp_port_status,
4999 is_mirror_output_bundle,
5000 forward_bpdu_changed,
5001 set_mac_table_config,
5003 NULL, /* meter_get_features */
5004 NULL, /* meter_set */
5005 NULL, /* meter_get */
5006 NULL, /* meter_del */
5007 group_alloc, /* group_alloc */
5008 group_construct, /* group_construct */
5009 group_destruct, /* group_destruct */
5010 group_dealloc, /* group_dealloc */
5011 group_modify, /* group_modify */
5012 group_get_stats, /* group_get_stats */