2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ofproto/ofproto-dpif.h"
20 #include "ofproto/ofproto-provider.h"
27 #include "byte-order.h"
32 #include "dynamic-string.h"
33 #include "fail-open.h"
34 #include "guarded-list.h"
38 #include "mac-learning.h"
39 #include "meta-flow.h"
40 #include "multipath.h"
41 #include "netdev-vport.h"
46 #include "odp-execute.h"
49 #include "ofp-actions.h"
50 #include "ofp-parse.h"
51 #include "ofp-print.h"
52 #include "ofproto-dpif-governor.h"
53 #include "ofproto-dpif-ipfix.h"
54 #include "ofproto-dpif-mirror.h"
55 #include "ofproto-dpif-sflow.h"
56 #include "ofproto-dpif-upcall.h"
57 #include "ofproto-dpif-xlate.h"
58 #include "poll-loop.h"
63 #include "unaligned.h"
65 #include "vlan-bitmap.h"
68 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
70 COVERAGE_DEFINE(ofproto_dpif_expired);
71 COVERAGE_DEFINE(facet_changed_rule);
72 COVERAGE_DEFINE(facet_revalidate);
73 COVERAGE_DEFINE(facet_unexpected);
74 COVERAGE_DEFINE(facet_suppress);
75 COVERAGE_DEFINE(subfacet_install_fail);
76 COVERAGE_DEFINE(packet_in_overflow);
77 COVERAGE_DEFINE(flow_mod_overflow);
79 /* Number of implemented OpenFlow tables. */
80 enum { N_TABLES = 255 };
81 enum { TBL_INTERNAL = N_TABLES - 1 }; /* Used for internal hidden rules. */
82 BUILD_ASSERT_DECL(N_TABLES >= 2 && N_TABLES <= 255);
92 * - Do include packets and bytes from facets that have been deleted or
93 * whose own statistics have been folded into the rule.
95 * - Do include packets and bytes sent "by hand" that were accounted to
96 * the rule without any facet being involved (this is a rare corner
97 * case in rule_execute()).
99 * - Do not include packet or bytes that can be obtained from any facet's
100 * packet_count or byte_count member or that can be obtained from the
101 * datapath by, e.g., dpif_flow_get() for any subfacet.
103 struct ovs_mutex stats_mutex;
104 uint64_t packet_count OVS_GUARDED; /* Number of packets received. */
105 uint64_t byte_count OVS_GUARDED; /* Number of bytes received. */
108 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes);
109 static struct rule_dpif *rule_dpif_cast(const struct rule *);
112 struct hmap_node hmap_node; /* In struct ofproto's "bundles" hmap. */
113 struct ofproto_dpif *ofproto; /* Owning ofproto. */
114 void *aux; /* Key supplied by ofproto's client. */
115 char *name; /* Identifier for log messages. */
118 struct list ports; /* Contains "struct ofport"s. */
119 enum port_vlan_mode vlan_mode; /* VLAN mode */
120 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
121 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
122 * NULL if all VLANs are trunked. */
123 struct lacp *lacp; /* LACP if LACP is enabled, otherwise NULL. */
124 struct bond *bond; /* Nonnull iff more than one port. */
125 bool use_priority_tags; /* Use 802.1p tag for frames in VLAN 0? */
128 bool floodable; /* True if no port has OFPUTIL_PC_NO_FLOOD set. */
131 static void bundle_remove(struct ofport *);
132 static void bundle_update(struct ofbundle *);
133 static void bundle_destroy(struct ofbundle *);
134 static void bundle_del_port(struct ofport_dpif *);
135 static void bundle_run(struct ofbundle *);
136 static void bundle_wait(struct ofbundle *);
138 static void stp_run(struct ofproto_dpif *ofproto);
139 static void stp_wait(struct ofproto_dpif *ofproto);
140 static int set_stp_port(struct ofport *,
141 const struct ofproto_port_stp_settings *);
143 static void compose_slow_path(const struct ofproto_dpif *, const struct flow *,
144 enum slow_path_reason,
145 uint64_t *stub, size_t stub_size,
146 const struct nlattr **actionsp,
147 size_t *actions_lenp);
149 /* A subfacet (see "struct subfacet" below) has three possible installation
152 * - SF_NOT_INSTALLED: Not installed in the datapath. This will only be the
153 * case just after the subfacet is created, just before the subfacet is
154 * destroyed, or if the datapath returns an error when we try to install a
157 * - SF_FAST_PATH: The subfacet's actions are installed in the datapath.
159 * - SF_SLOW_PATH: An action that sends every packet for the subfacet through
160 * ofproto_dpif is installed in the datapath.
163 SF_NOT_INSTALLED, /* No datapath flow for this subfacet. */
164 SF_FAST_PATH, /* Full actions are installed. */
165 SF_SLOW_PATH, /* Send-to-userspace action is installed. */
168 /* A dpif flow and actions associated with a facet.
170 * See also the large comment on struct facet. */
173 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
174 struct list list_node; /* In struct facet's 'facets' list. */
175 struct facet *facet; /* Owning facet. */
176 struct dpif_backer *backer; /* Owning backer. */
178 enum odp_key_fitness key_fitness;
182 long long int used; /* Time last used; time created if not used. */
183 long long int created; /* Time created. */
185 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
186 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
188 enum subfacet_path path; /* Installed in datapath? */
191 #define SUBFACET_DESTROY_MAX_BATCH 50
193 static struct subfacet *subfacet_create(struct facet *, struct flow_miss *);
194 static struct subfacet *subfacet_find(struct dpif_backer *,
195 const struct nlattr *key, size_t key_len,
197 static void subfacet_destroy(struct subfacet *);
198 static void subfacet_destroy__(struct subfacet *);
199 static void subfacet_destroy_batch(struct dpif_backer *,
200 struct subfacet **, int n);
201 static void subfacet_reset_dp_stats(struct subfacet *,
202 struct dpif_flow_stats *);
203 static void subfacet_update_stats(struct subfacet *,
204 const struct dpif_flow_stats *);
205 static int subfacet_install(struct subfacet *,
206 const struct ofpbuf *odp_actions,
207 struct dpif_flow_stats *);
208 static void subfacet_uninstall(struct subfacet *);
210 /* A unique, non-overlapping instantiation of an OpenFlow flow.
212 * A facet associates a "struct flow", which represents the Open vSwitch
213 * userspace idea of an exact-match flow, with one or more subfacets.
214 * While the facet is created based on an exact-match flow, it is stored
215 * within the ofproto based on the wildcards that could be expressed
216 * based on the flow table and other configuration. (See the 'wc'
217 * description in "struct xlate_out" for more details.)
219 * Each subfacet tracks the datapath's idea of the flow equivalent to
220 * the facet. When the kernel module (or other dpif implementation) and
221 * Open vSwitch userspace agree on the definition of a flow key, there
222 * is exactly one subfacet per facet. If the dpif implementation
223 * supports more-specific flow matching than userspace, however, a facet
224 * can have more than one subfacet. Examples include the dpif
225 * implementation not supporting the same wildcards as userspace or some
226 * distinction in flow that userspace simply doesn't understand.
228 * Flow expiration works in terms of subfacets, so a facet must have at
229 * least one subfacet or it will never expire, leaking memory. */
232 struct ofproto_dpif *ofproto;
235 struct list subfacets;
236 long long int used; /* Time last used; time created if not used. */
239 struct flow flow; /* Flow of the creating subfacet. */
240 struct cls_rule cr; /* In 'ofproto_dpif's facets classifier. */
244 * - Do include packets and bytes sent "by hand", e.g. with
247 * - Do include packets and bytes that were obtained from the datapath
248 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
249 * DPIF_FP_ZERO_STATS).
251 * - Do not include packets or bytes that can be obtained from the
252 * datapath for any existing subfacet.
254 uint64_t packet_count; /* Number of packets received. */
255 uint64_t byte_count; /* Number of bytes received. */
257 /* Resubmit statistics. */
258 uint64_t prev_packet_count; /* Number of packets from last stats push. */
259 uint64_t prev_byte_count; /* Number of bytes from last stats push. */
260 long long int prev_used; /* Used time from last stats push. */
263 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
264 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
265 uint8_t tcp_flags; /* TCP flags seen for this 'rule'. */
267 struct xlate_out xout;
269 /* Storage for a single subfacet, to reduce malloc() time and space
270 * overhead. (A facet always has at least one subfacet and in the common
271 * case has exactly one subfacet. However, 'one_subfacet' may not
272 * always be valid, since it could have been removed after newer
273 * subfacets were pushed onto the 'subfacets' list.) */
274 struct subfacet one_subfacet;
276 long long int learn_rl; /* Rate limiter for facet_learn(). */
279 static struct facet *facet_create(const struct flow_miss *);
280 static void facet_remove(struct facet *);
281 static void facet_free(struct facet *);
283 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
284 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
285 const struct flow *);
286 static bool facet_revalidate(struct facet *);
287 static bool facet_check_consistency(struct facet *);
289 static void facet_flush_stats(struct facet *);
291 static void facet_reset_counters(struct facet *);
292 static void flow_push_stats(struct ofproto_dpif *, struct flow *,
293 struct dpif_flow_stats *, bool may_learn);
294 static void facet_push_stats(struct facet *, bool may_learn);
295 static void facet_learn(struct facet *);
296 static void facet_account(struct facet *);
297 static void push_all_stats(void);
299 static bool facet_is_controller_flow(struct facet *);
302 struct hmap_node odp_port_node; /* In dpif_backer's "odp_to_ofport_map". */
306 struct ofbundle *bundle; /* Bundle that contains this port, if any. */
307 struct list bundle_node; /* In struct ofbundle's "ports" list. */
308 struct cfm *cfm; /* Connectivity Fault Management, if any. */
309 struct bfd *bfd; /* BFD, if any. */
310 bool may_enable; /* May be enabled in bonds. */
311 bool is_tunnel; /* This port is a tunnel. */
312 long long int carrier_seq; /* Carrier status changes. */
313 struct ofport_dpif *peer; /* Peer if patch port. */
316 struct stp_port *stp_port; /* Spanning Tree Protocol, if any. */
317 enum stp_state stp_state; /* Always STP_DISABLED if STP not in use. */
318 long long int stp_state_entered;
320 /* Queue to DSCP mapping. */
321 struct ofproto_port_queue *qdscp;
324 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
326 * This is deprecated. It is only for compatibility with broken device
327 * drivers in old versions of Linux that do not properly support VLANs when
328 * VLAN devices are not used. When broken device drivers are no longer in
329 * widespread use, we will delete these interfaces. */
330 ofp_port_t realdev_ofp_port;
334 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
336 * This is deprecated. It is only for compatibility with broken device drivers
337 * in old versions of Linux that do not properly support VLANs when VLAN
338 * devices are not used. When broken device drivers are no longer in
339 * widespread use, we will delete these interfaces. */
340 struct vlan_splinter {
341 struct hmap_node realdev_vid_node;
342 struct hmap_node vlandev_node;
343 ofp_port_t realdev_ofp_port;
344 ofp_port_t vlandev_ofp_port;
348 static void vsp_remove(struct ofport_dpif *);
349 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
351 static odp_port_t ofp_port_to_odp_port(const struct ofproto_dpif *,
354 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
357 static struct ofport_dpif *
358 ofport_dpif_cast(const struct ofport *ofport)
360 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
363 static void port_run(struct ofport_dpif *);
364 static void port_run_fast(struct ofport_dpif *);
365 static void port_wait(struct ofport_dpif *);
366 static int set_bfd(struct ofport *, const struct smap *);
367 static int set_cfm(struct ofport *, const struct cfm_settings *);
368 static void ofport_update_peer(struct ofport_dpif *);
369 static void run_fast_rl(void);
370 static int run_fast(struct ofproto *);
372 struct dpif_completion {
373 struct list list_node;
374 struct ofoperation *op;
377 /* Reasons that we might need to revalidate every facet, and corresponding
380 * A value of 0 means that there is no need to revalidate.
382 * It would be nice to have some cleaner way to integrate with coverage
383 * counters, but with only a few reasons I guess this is good enough for
385 enum revalidate_reason {
386 REV_RECONFIGURE = 1, /* Switch configuration changed. */
387 REV_STP, /* Spanning tree protocol port status change. */
388 REV_BOND, /* Bonding changed. */
389 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
390 REV_FLOW_TABLE, /* Flow table changed. */
391 REV_MAC_LEARNING, /* Mac learning changed. */
392 REV_INCONSISTENCY /* Facet self-check failed. */
394 COVERAGE_DEFINE(rev_reconfigure);
395 COVERAGE_DEFINE(rev_stp);
396 COVERAGE_DEFINE(rev_bond);
397 COVERAGE_DEFINE(rev_port_toggled);
398 COVERAGE_DEFINE(rev_flow_table);
399 COVERAGE_DEFINE(rev_mac_learning);
400 COVERAGE_DEFINE(rev_inconsistency);
402 struct avg_subfacet_rates {
403 double add_rate; /* Moving average of new flows created per minute. */
404 double del_rate; /* Moving average of flows deleted per minute. */
407 /* All datapaths of a given type share a single dpif backer instance. */
413 struct timer next_expiration;
415 struct ovs_rwlock odp_to_ofport_lock;
416 struct hmap odp_to_ofport_map OVS_GUARDED; /* ODP port to ofport map. */
418 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
420 /* Facet revalidation flags applying to facets which use this backer. */
421 enum revalidate_reason need_revalidate; /* Revalidate every facet. */
423 struct hmap drop_keys; /* Set of dropped odp keys. */
424 bool recv_set_enable; /* Enables or disables receiving packets. */
426 struct hmap subfacets;
427 struct governor *governor;
429 /* Subfacet statistics.
431 * These keep track of the total number of subfacets added and deleted and
432 * flow life span. They are useful for computing the flow rates stats
433 * exposed via "ovs-appctl dpif/show". The goal is to learn about
434 * traffic patterns in ways that we can use later to improve Open vSwitch
435 * performance in new situations. */
436 long long int created; /* Time when it is created. */
437 unsigned max_n_subfacet; /* Maximum number of flows */
438 unsigned avg_n_subfacet; /* Average number of flows. */
439 long long int avg_subfacet_life; /* Average life span of subfacets. */
441 /* The average number of subfacets... */
442 struct avg_subfacet_rates hourly; /* ...over the last hour. */
443 struct avg_subfacet_rates daily; /* ...over the last day. */
444 struct avg_subfacet_rates lifetime; /* ...over the switch lifetime. */
445 long long int last_minute; /* Last time 'hourly' was updated. */
447 /* Number of subfacets added or deleted since 'last_minute'. */
448 unsigned subfacet_add_count;
449 unsigned subfacet_del_count;
451 /* Number of subfacets added or deleted from 'created' to 'last_minute.' */
452 unsigned long long int total_subfacet_add_count;
453 unsigned long long int total_subfacet_del_count;
455 /* Number of upcall handling threads. */
456 unsigned int n_handler_threads;
459 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
460 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
462 static void drop_key_clear(struct dpif_backer *);
463 static void update_moving_averages(struct dpif_backer *backer);
465 struct ofproto_dpif {
466 struct hmap_node all_ofproto_dpifs_node; /* In 'all_ofproto_dpifs'. */
468 struct dpif_backer *backer;
470 /* Special OpenFlow rules. */
471 struct rule_dpif *miss_rule; /* Sends flow table misses to controller. */
472 struct rule_dpif *no_packet_in_rule; /* Drops flow table misses. */
473 struct rule_dpif *drop_frags_rule; /* Used in OFPC_FRAG_DROP mode. */
476 struct netflow *netflow;
477 struct dpif_sflow *sflow;
478 struct dpif_ipfix *ipfix;
479 struct hmap bundles; /* Contains "struct ofbundle"s. */
480 struct mac_learning *ml;
481 bool has_bonded_bundles;
482 struct mbridge *mbridge;
485 struct classifier facets; /* Contains 'struct facet's. */
486 long long int consistency_rl;
488 struct netdev_stats stats; /* To account packets generated and consumed in
493 long long int stp_last_tick;
495 /* VLAN splinters. */
496 struct ovs_mutex vsp_mutex;
497 struct hmap realdev_vid_map OVS_GUARDED; /* (realdev,vid) -> vlandev. */
498 struct hmap vlandev_map OVS_GUARDED; /* vlandev -> (realdev,vid). */
501 struct sset ports; /* Set of standard port names. */
502 struct sset ghost_ports; /* Ports with no datapath port. */
503 struct sset port_poll_set; /* Queued names for port_poll() reply. */
504 int port_poll_errno; /* Last errno for port_poll() reply. */
506 /* Per ofproto's dpif stats. */
511 struct guarded_list flow_mods; /* Contains "struct flow_mod"s. */
512 struct guarded_list pins; /* Contains "struct ofputil_packet_in"s. */
515 /* By default, flows in the datapath are wildcarded (megaflows). They
516 * may be disabled with the "ovs-appctl dpif/disable-megaflows" command. */
517 static bool enable_megaflows = true;
519 /* All existing ofproto_dpif instances, indexed by ->up.name. */
520 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
522 static void ofproto_dpif_unixctl_init(void);
524 static inline struct ofproto_dpif *
525 ofproto_dpif_cast(const struct ofproto *ofproto)
527 ovs_assert(ofproto->ofproto_class == &ofproto_dpif_class);
528 return CONTAINER_OF(ofproto, struct ofproto_dpif, up);
531 static struct ofport_dpif *get_ofp_port(const struct ofproto_dpif *ofproto,
532 ofp_port_t ofp_port);
533 static void ofproto_trace(struct ofproto_dpif *, const struct flow *,
534 const struct ofpbuf *packet, struct ds *);
537 static void handle_upcalls(struct dpif_backer *);
539 /* Flow expiration. */
540 static int expire(struct dpif_backer *);
543 static void send_netflow_active_timeouts(struct ofproto_dpif *);
546 static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
548 /* Global variables. */
549 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
551 /* Initial mappings of port to bridge mappings. */
552 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
554 /* Executes and takes ownership of 'fm'. */
556 ofproto_dpif_flow_mod(struct ofproto_dpif *ofproto,
557 struct ofputil_flow_mod *fm)
559 if (!guarded_list_push_back(&ofproto->flow_mods, &fm->list_node, 1024)) {
560 COVERAGE_INC(flow_mod_overflow);
566 /* Appends 'pin' to the queue of "packet ins" to be sent to the controller.
567 * Takes ownership of 'pin' and pin->packet. */
569 ofproto_dpif_send_packet_in(struct ofproto_dpif *ofproto,
570 struct ofputil_packet_in *pin)
572 if (!guarded_list_push_back(&ofproto->pins, &pin->list_node, 1024)) {
573 COVERAGE_INC(packet_in_overflow);
574 free(CONST_CAST(void *, pin->packet));
579 /* Factory functions. */
582 init(const struct shash *iface_hints)
584 struct shash_node *node;
586 /* Make a local copy, since we don't own 'iface_hints' elements. */
587 SHASH_FOR_EACH(node, iface_hints) {
588 const struct iface_hint *orig_hint = node->data;
589 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
591 new_hint->br_name = xstrdup(orig_hint->br_name);
592 new_hint->br_type = xstrdup(orig_hint->br_type);
593 new_hint->ofp_port = orig_hint->ofp_port;
595 shash_add(&init_ofp_ports, node->name, new_hint);
600 enumerate_types(struct sset *types)
602 dp_enumerate_types(types);
606 enumerate_names(const char *type, struct sset *names)
608 struct ofproto_dpif *ofproto;
611 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
612 if (strcmp(type, ofproto->up.type)) {
615 sset_add(names, ofproto->up.name);
622 del(const char *type, const char *name)
627 error = dpif_open(name, type, &dpif);
629 error = dpif_delete(dpif);
636 port_open_type(const char *datapath_type, const char *port_type)
638 return dpif_port_open_type(datapath_type, port_type);
641 /* Type functions. */
643 static void process_dpif_port_changes(struct dpif_backer *);
644 static void process_dpif_all_ports_changed(struct dpif_backer *);
645 static void process_dpif_port_change(struct dpif_backer *,
646 const char *devname);
647 static void process_dpif_port_error(struct dpif_backer *, int error);
649 static struct ofproto_dpif *
650 lookup_ofproto_dpif_by_port_name(const char *name)
652 struct ofproto_dpif *ofproto;
654 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
655 if (sset_contains(&ofproto->ports, name)) {
664 type_run(const char *type)
666 static long long int push_timer = LLONG_MIN;
667 struct dpif_backer *backer;
669 backer = shash_find_data(&all_dpif_backers, type);
671 /* This is not necessarily a problem, since backers are only
672 * created on demand. */
676 dpif_run(backer->dpif);
678 /* The most natural place to push facet statistics is when they're pulled
679 * from the datapath. However, when there are many flows in the datapath,
680 * this expensive operation can occur so frequently, that it reduces our
681 * ability to quickly set up flows. To reduce the cost, we push statistics
683 if (time_msec() > push_timer) {
684 push_timer = time_msec() + 2000;
688 /* If vswitchd started with other_config:flow_restore_wait set as "true",
689 * and the configuration has now changed to "false", enable receiving
690 * packets from the datapath. */
691 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
694 backer->recv_set_enable = true;
696 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
698 udpif_recv_set(backer->udpif, 0, false);
699 VLOG_ERR("Failed to enable receiving packets in dpif.");
702 udpif_recv_set(backer->udpif, n_handler_threads,
703 backer->recv_set_enable);
704 dpif_flow_flush(backer->dpif);
705 backer->need_revalidate = REV_RECONFIGURE;
708 /* If the n_handler_threads is reconfigured, call udpif_recv_set()
709 * to reset the handler threads. */
710 if (backer->n_handler_threads != n_handler_threads) {
711 udpif_recv_set(backer->udpif, n_handler_threads,
712 backer->recv_set_enable);
713 backer->n_handler_threads = n_handler_threads;
716 if (backer->need_revalidate) {
717 struct ofproto_dpif *ofproto;
718 struct simap_node *node;
719 struct simap tmp_backers;
721 /* Handle tunnel garbage collection. */
722 simap_init(&tmp_backers);
723 simap_swap(&backer->tnl_backers, &tmp_backers);
725 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
726 struct ofport_dpif *iter;
728 if (backer != ofproto->backer) {
732 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
733 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
736 if (!iter->is_tunnel) {
740 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
741 namebuf, sizeof namebuf);
742 node = simap_find(&tmp_backers, dp_port);
744 simap_put(&backer->tnl_backers, dp_port, node->data);
745 simap_delete(&tmp_backers, node);
746 node = simap_find(&backer->tnl_backers, dp_port);
748 node = simap_find(&backer->tnl_backers, dp_port);
750 odp_port_t odp_port = ODPP_NONE;
752 if (!dpif_port_add(backer->dpif, iter->up.netdev,
754 simap_put(&backer->tnl_backers, dp_port,
755 odp_to_u32(odp_port));
756 node = simap_find(&backer->tnl_backers, dp_port);
761 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
762 if (tnl_port_reconfigure(iter, iter->up.netdev,
764 backer->need_revalidate = REV_RECONFIGURE;
769 SIMAP_FOR_EACH (node, &tmp_backers) {
770 dpif_port_del(backer->dpif, u32_to_odp(node->data));
772 simap_destroy(&tmp_backers);
774 switch (backer->need_revalidate) {
775 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
776 case REV_STP: COVERAGE_INC(rev_stp); break;
777 case REV_BOND: COVERAGE_INC(rev_bond); break;
778 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
779 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
780 case REV_MAC_LEARNING: COVERAGE_INC(rev_mac_learning); break;
781 case REV_INCONSISTENCY: COVERAGE_INC(rev_inconsistency); break;
783 backer->need_revalidate = 0;
785 /* Clear the drop_keys in case we should now be accepting some
786 * formerly dropped flows. */
787 drop_key_clear(backer);
789 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
790 struct facet *facet, *next;
791 struct ofport_dpif *ofport;
792 struct cls_cursor cursor;
793 struct ofbundle *bundle;
795 if (ofproto->backer != backer) {
799 ovs_rwlock_wrlock(&xlate_rwlock);
800 xlate_ofproto_set(ofproto, ofproto->up.name,
801 ofproto->backer->dpif, ofproto->miss_rule,
802 ofproto->no_packet_in_rule, ofproto->ml,
803 ofproto->stp, ofproto->mbridge,
804 ofproto->sflow, ofproto->ipfix,
805 ofproto->up.frag_handling,
806 ofproto->up.forward_bpdu,
807 connmgr_has_in_band(ofproto->up.connmgr),
808 ofproto->netflow != NULL);
810 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
811 xlate_bundle_set(ofproto, bundle, bundle->name,
812 bundle->vlan_mode, bundle->vlan,
813 bundle->trunks, bundle->use_priority_tags,
814 bundle->bond, bundle->lacp,
818 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
819 int stp_port = ofport->stp_port
820 ? stp_port_no(ofport->stp_port)
822 xlate_ofport_set(ofproto, ofport->bundle, ofport,
823 ofport->up.ofp_port, ofport->odp_port,
824 ofport->up.netdev, ofport->cfm,
825 ofport->bfd, ofport->peer, stp_port,
826 ofport->qdscp, ofport->n_qdscp,
827 ofport->up.pp.config, ofport->is_tunnel,
830 ovs_rwlock_unlock(&xlate_rwlock);
832 /* Only ofproto-dpif cares about the facet classifier so we just
833 * lock cls_cursor_init() to appease the thread safety analysis. */
834 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
835 cls_cursor_init(&cursor, &ofproto->facets, NULL);
836 ovs_rwlock_unlock(&ofproto->facets.rwlock);
837 CLS_CURSOR_FOR_EACH_SAFE (facet, next, cr, &cursor) {
838 facet_revalidate(facet);
843 udpif_revalidate(backer->udpif);
846 if (!backer->recv_set_enable) {
847 /* Wake up before a max of 1000ms. */
848 timer_set_duration(&backer->next_expiration, 1000);
849 } else if (timer_expired(&backer->next_expiration)) {
850 int delay = expire(backer);
851 timer_set_duration(&backer->next_expiration, delay);
854 process_dpif_port_changes(backer);
856 if (backer->governor) {
859 governor_run(backer->governor);
861 /* If the governor has shrunk to its minimum size and the number of
862 * subfacets has dwindled, then drop the governor entirely.
864 * For hysteresis, the number of subfacets to drop the governor is
865 * smaller than the number needed to trigger its creation. */
866 n_subfacets = hmap_count(&backer->subfacets);
867 if (n_subfacets * 4 < flow_eviction_threshold
868 && governor_is_idle(backer->governor)) {
869 governor_destroy(backer->governor);
870 backer->governor = NULL;
877 /* Check for and handle port changes in 'backer''s dpif. */
879 process_dpif_port_changes(struct dpif_backer *backer)
885 error = dpif_port_poll(backer->dpif, &devname);
891 process_dpif_all_ports_changed(backer);
895 process_dpif_port_change(backer, devname);
900 process_dpif_port_error(backer, error);
907 process_dpif_all_ports_changed(struct dpif_backer *backer)
909 struct ofproto_dpif *ofproto;
910 struct dpif_port dpif_port;
911 struct dpif_port_dump dump;
912 struct sset devnames;
915 sset_init(&devnames);
916 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
917 if (ofproto->backer == backer) {
918 struct ofport *ofport;
920 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
921 sset_add(&devnames, netdev_get_name(ofport->netdev));
925 DPIF_PORT_FOR_EACH (&dpif_port, &dump, backer->dpif) {
926 sset_add(&devnames, dpif_port.name);
929 SSET_FOR_EACH (devname, &devnames) {
930 process_dpif_port_change(backer, devname);
932 sset_destroy(&devnames);
936 process_dpif_port_change(struct dpif_backer *backer, const char *devname)
938 struct ofproto_dpif *ofproto;
939 struct dpif_port port;
941 /* Don't report on the datapath's device. */
942 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
946 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
947 &all_ofproto_dpifs) {
948 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
953 ofproto = lookup_ofproto_dpif_by_port_name(devname);
954 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
955 /* The port was removed. If we know the datapath,
956 * report it through poll_set(). If we don't, it may be
957 * notifying us of a removal we initiated, so ignore it.
958 * If there's a pending ENOBUFS, let it stand, since
959 * everything will be reevaluated. */
960 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
961 sset_add(&ofproto->port_poll_set, devname);
962 ofproto->port_poll_errno = 0;
964 } else if (!ofproto) {
965 /* The port was added, but we don't know with which
966 * ofproto we should associate it. Delete it. */
967 dpif_port_del(backer->dpif, port.port_no);
969 struct ofport_dpif *ofport;
971 ofport = ofport_dpif_cast(shash_find_data(
972 &ofproto->up.port_by_name, devname));
974 && ofport->odp_port != port.port_no
975 && !odp_port_to_ofport(backer, port.port_no))
977 /* 'ofport''s datapath port number has changed from
978 * 'ofport->odp_port' to 'port.port_no'. Update our internal data
979 * structures to match. */
980 ovs_rwlock_wrlock(&backer->odp_to_ofport_lock);
981 hmap_remove(&backer->odp_to_ofport_map, &ofport->odp_port_node);
982 ofport->odp_port = port.port_no;
983 hmap_insert(&backer->odp_to_ofport_map, &ofport->odp_port_node,
984 hash_odp_port(port.port_no));
985 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
986 backer->need_revalidate = REV_RECONFIGURE;
989 dpif_port_destroy(&port);
992 /* Propagate 'error' to all ofprotos based on 'backer'. */
994 process_dpif_port_error(struct dpif_backer *backer, int error)
996 struct ofproto_dpif *ofproto;
998 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
999 if (ofproto->backer == backer) {
1000 sset_clear(&ofproto->port_poll_set);
1001 ofproto->port_poll_errno = error;
1007 dpif_backer_run_fast(struct dpif_backer *backer)
1009 handle_upcalls(backer);
1015 type_run_fast(const char *type)
1017 struct dpif_backer *backer;
1019 backer = shash_find_data(&all_dpif_backers, type);
1021 /* This is not necessarily a problem, since backers are only
1022 * created on demand. */
1026 return dpif_backer_run_fast(backer);
1032 static long long int port_rl = LLONG_MIN;
1034 if (time_msec() >= port_rl) {
1035 struct ofproto_dpif *ofproto;
1037 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
1038 run_fast(&ofproto->up);
1040 port_rl = time_msec() + 200;
1045 type_wait(const char *type)
1047 struct dpif_backer *backer;
1049 backer = shash_find_data(&all_dpif_backers, type);
1051 /* This is not necessarily a problem, since backers are only
1052 * created on demand. */
1056 if (backer->governor) {
1057 governor_wait(backer->governor);
1060 timer_wait(&backer->next_expiration);
1061 dpif_wait(backer->dpif);
1062 udpif_wait(backer->udpif);
1065 /* Basic life-cycle. */
1067 static int add_internal_flows(struct ofproto_dpif *);
1069 static struct ofproto *
1072 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
1073 return &ofproto->up;
1077 dealloc(struct ofproto *ofproto_)
1079 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1084 close_dpif_backer(struct dpif_backer *backer)
1086 struct shash_node *node;
1088 ovs_assert(backer->refcount > 0);
1090 if (--backer->refcount) {
1094 drop_key_clear(backer);
1095 hmap_destroy(&backer->drop_keys);
1097 simap_destroy(&backer->tnl_backers);
1098 ovs_rwlock_destroy(&backer->odp_to_ofport_lock);
1099 hmap_destroy(&backer->odp_to_ofport_map);
1100 node = shash_find(&all_dpif_backers, backer->type);
1102 shash_delete(&all_dpif_backers, node);
1103 udpif_destroy(backer->udpif);
1104 dpif_close(backer->dpif);
1106 ovs_assert(hmap_is_empty(&backer->subfacets));
1107 hmap_destroy(&backer->subfacets);
1108 governor_destroy(backer->governor);
1113 /* Datapath port slated for removal from datapath. */
1114 struct odp_garbage {
1115 struct list list_node;
1116 odp_port_t odp_port;
1120 open_dpif_backer(const char *type, struct dpif_backer **backerp)
1122 struct dpif_backer *backer;
1123 struct dpif_port_dump port_dump;
1124 struct dpif_port port;
1125 struct shash_node *node;
1126 struct list garbage_list;
1127 struct odp_garbage *garbage, *next;
1133 backer = shash_find_data(&all_dpif_backers, type);
1140 backer_name = xasprintf("ovs-%s", type);
1142 /* Remove any existing datapaths, since we assume we're the only
1143 * userspace controlling the datapath. */
1145 dp_enumerate_names(type, &names);
1146 SSET_FOR_EACH(name, &names) {
1147 struct dpif *old_dpif;
1149 /* Don't remove our backer if it exists. */
1150 if (!strcmp(name, backer_name)) {
1154 if (dpif_open(name, type, &old_dpif)) {
1155 VLOG_WARN("couldn't open old datapath %s to remove it", name);
1157 dpif_delete(old_dpif);
1158 dpif_close(old_dpif);
1161 sset_destroy(&names);
1163 backer = xmalloc(sizeof *backer);
1165 error = dpif_create_and_open(backer_name, type, &backer->dpif);
1168 VLOG_ERR("failed to open datapath of type %s: %s", type,
1169 ovs_strerror(error));
1173 backer->udpif = udpif_create(backer, backer->dpif);
1175 backer->type = xstrdup(type);
1176 backer->governor = NULL;
1177 backer->refcount = 1;
1178 hmap_init(&backer->odp_to_ofport_map);
1179 ovs_rwlock_init(&backer->odp_to_ofport_lock);
1180 hmap_init(&backer->drop_keys);
1181 hmap_init(&backer->subfacets);
1182 timer_set_duration(&backer->next_expiration, 1000);
1183 backer->need_revalidate = 0;
1184 simap_init(&backer->tnl_backers);
1185 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
1188 if (backer->recv_set_enable) {
1189 dpif_flow_flush(backer->dpif);
1192 /* Loop through the ports already on the datapath and remove any
1193 * that we don't need anymore. */
1194 list_init(&garbage_list);
1195 dpif_port_dump_start(&port_dump, backer->dpif);
1196 while (dpif_port_dump_next(&port_dump, &port)) {
1197 node = shash_find(&init_ofp_ports, port.name);
1198 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
1199 garbage = xmalloc(sizeof *garbage);
1200 garbage->odp_port = port.port_no;
1201 list_push_front(&garbage_list, &garbage->list_node);
1204 dpif_port_dump_done(&port_dump);
1206 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
1207 dpif_port_del(backer->dpif, garbage->odp_port);
1208 list_remove(&garbage->list_node);
1212 shash_add(&all_dpif_backers, type, backer);
1214 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
1216 VLOG_ERR("failed to listen on datapath of type %s: %s",
1217 type, ovs_strerror(error));
1218 close_dpif_backer(backer);
1221 udpif_recv_set(backer->udpif, n_handler_threads,
1222 backer->recv_set_enable);
1223 backer->n_handler_threads = n_handler_threads;
1225 backer->max_n_subfacet = 0;
1226 backer->created = time_msec();
1227 backer->last_minute = backer->created;
1228 memset(&backer->hourly, 0, sizeof backer->hourly);
1229 memset(&backer->daily, 0, sizeof backer->daily);
1230 memset(&backer->lifetime, 0, sizeof backer->lifetime);
1231 backer->subfacet_add_count = 0;
1232 backer->subfacet_del_count = 0;
1233 backer->total_subfacet_add_count = 0;
1234 backer->total_subfacet_del_count = 0;
1235 backer->avg_n_subfacet = 0;
1236 backer->avg_subfacet_life = 0;
1242 construct(struct ofproto *ofproto_)
1244 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1245 struct shash_node *node, *next;
1249 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1254 max_ports = dpif_get_max_ports(ofproto->backer->dpif);
1255 ofproto_init_max_ports(ofproto_, MIN(max_ports, ofp_to_u16(OFPP_MAX)));
1257 ofproto->netflow = NULL;
1258 ofproto->sflow = NULL;
1259 ofproto->ipfix = NULL;
1260 ofproto->stp = NULL;
1261 hmap_init(&ofproto->bundles);
1262 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1263 ofproto->mbridge = mbridge_create();
1264 ofproto->has_bonded_bundles = false;
1265 ovs_mutex_init(&ofproto->vsp_mutex);
1267 classifier_init(&ofproto->facets);
1268 ofproto->consistency_rl = LLONG_MIN;
1270 guarded_list_init(&ofproto->flow_mods);
1271 guarded_list_init(&ofproto->pins);
1273 ofproto_dpif_unixctl_init();
1275 hmap_init(&ofproto->vlandev_map);
1276 hmap_init(&ofproto->realdev_vid_map);
1278 sset_init(&ofproto->ports);
1279 sset_init(&ofproto->ghost_ports);
1280 sset_init(&ofproto->port_poll_set);
1281 ofproto->port_poll_errno = 0;
1283 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1284 struct iface_hint *iface_hint = node->data;
1286 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1287 /* Check if the datapath already has this port. */
1288 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1289 sset_add(&ofproto->ports, node->name);
1292 free(iface_hint->br_name);
1293 free(iface_hint->br_type);
1295 shash_delete(&init_ofp_ports, node);
1299 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1300 hash_string(ofproto->up.name, 0));
1301 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1303 ofproto_init_tables(ofproto_, N_TABLES);
1304 error = add_internal_flows(ofproto);
1305 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1308 ofproto->n_missed = 0;
1314 add_internal_flow(struct ofproto_dpif *ofproto, int id,
1315 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1317 struct ofputil_flow_mod fm;
1320 match_init_catchall(&fm.match);
1322 match_set_reg(&fm.match, 0, id);
1323 fm.new_cookie = htonll(0);
1324 fm.cookie = htonll(0);
1325 fm.cookie_mask = htonll(0);
1326 fm.modify_cookie = false;
1327 fm.table_id = TBL_INTERNAL;
1328 fm.command = OFPFC_ADD;
1329 fm.idle_timeout = 0;
1330 fm.hard_timeout = 0;
1334 fm.ofpacts = ofpacts->data;
1335 fm.ofpacts_len = ofpacts->size;
1337 error = ofproto_flow_mod(&ofproto->up, &fm);
1339 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1340 id, ofperr_to_string(error));
1344 if (rule_dpif_lookup_in_table(ofproto, &fm.match.flow, NULL, TBL_INTERNAL,
1346 rule_dpif_unref(*rulep);
1355 add_internal_flows(struct ofproto_dpif *ofproto)
1357 struct ofpact_controller *controller;
1358 uint64_t ofpacts_stub[128 / 8];
1359 struct ofpbuf ofpacts;
1363 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1366 controller = ofpact_put_CONTROLLER(&ofpacts);
1367 controller->max_len = UINT16_MAX;
1368 controller->controller_id = 0;
1369 controller->reason = OFPR_NO_MATCH;
1370 ofpact_pad(&ofpacts);
1372 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1377 ofpbuf_clear(&ofpacts);
1378 error = add_internal_flow(ofproto, id++, &ofpacts,
1379 &ofproto->no_packet_in_rule);
1384 error = add_internal_flow(ofproto, id++, &ofpacts,
1385 &ofproto->drop_frags_rule);
1390 destruct(struct ofproto *ofproto_)
1392 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1393 struct rule_dpif *rule, *next_rule;
1394 struct ofputil_packet_in *pin, *next_pin;
1395 struct ofputil_flow_mod *fm, *next_fm;
1396 struct facet *facet, *next_facet;
1397 struct list flow_mods, pins;
1398 struct cls_cursor cursor;
1399 struct oftable *table;
1401 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
1402 cls_cursor_init(&cursor, &ofproto->facets, NULL);
1403 ovs_rwlock_unlock(&ofproto->facets.rwlock);
1404 CLS_CURSOR_FOR_EACH_SAFE (facet, next_facet, cr, &cursor) {
1405 facet_remove(facet);
1408 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1409 ovs_rwlock_wrlock(&xlate_rwlock);
1410 xlate_remove_ofproto(ofproto);
1411 ovs_rwlock_unlock(&xlate_rwlock);
1413 /* Discard any flow_miss_batches queued up for 'ofproto', avoiding a
1414 * use-after-free error. */
1415 udpif_revalidate(ofproto->backer->udpif);
1417 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1419 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1420 struct cls_cursor cursor;
1422 ovs_rwlock_wrlock(&table->cls.rwlock);
1423 cls_cursor_init(&cursor, &table->cls, NULL);
1424 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1425 ofproto_rule_delete(&ofproto->up, &table->cls, &rule->up);
1427 ovs_rwlock_unlock(&table->cls.rwlock);
1430 guarded_list_pop_all(&ofproto->flow_mods, &flow_mods);
1431 LIST_FOR_EACH_SAFE (fm, next_fm, list_node, &flow_mods) {
1432 list_remove(&fm->list_node);
1436 guarded_list_destroy(&ofproto->flow_mods);
1438 guarded_list_pop_all(&ofproto->pins, &pins);
1439 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1440 list_remove(&pin->list_node);
1441 free(CONST_CAST(void *, pin->packet));
1444 guarded_list_destroy(&ofproto->pins);
1446 mbridge_unref(ofproto->mbridge);
1448 netflow_destroy(ofproto->netflow);
1449 dpif_sflow_unref(ofproto->sflow);
1450 hmap_destroy(&ofproto->bundles);
1451 mac_learning_unref(ofproto->ml);
1453 classifier_destroy(&ofproto->facets);
1455 hmap_destroy(&ofproto->vlandev_map);
1456 hmap_destroy(&ofproto->realdev_vid_map);
1458 sset_destroy(&ofproto->ports);
1459 sset_destroy(&ofproto->ghost_ports);
1460 sset_destroy(&ofproto->port_poll_set);
1462 ovs_mutex_destroy(&ofproto->vsp_mutex);
1464 close_dpif_backer(ofproto->backer);
1468 run_fast(struct ofproto *ofproto_)
1470 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1471 struct ofputil_packet_in *pin, *next_pin;
1472 struct ofputil_flow_mod *fm, *next_fm;
1473 struct list flow_mods, pins;
1474 struct ofport_dpif *ofport;
1476 /* Do not perform any periodic activity required by 'ofproto' while
1477 * waiting for flow restore to complete. */
1478 if (ofproto_get_flow_restore_wait()) {
1482 guarded_list_pop_all(&ofproto->flow_mods, &flow_mods);
1483 LIST_FOR_EACH_SAFE (fm, next_fm, list_node, &flow_mods) {
1484 int error = ofproto_flow_mod(&ofproto->up, fm);
1485 if (error && !VLOG_DROP_WARN(&rl)) {
1486 VLOG_WARN("learning action failed to modify flow table (%s)",
1487 ofperr_get_name(error));
1490 list_remove(&fm->list_node);
1495 guarded_list_pop_all(&ofproto->pins, &pins);
1496 LIST_FOR_EACH_SAFE (pin, next_pin, list_node, &pins) {
1497 connmgr_send_packet_in(ofproto->up.connmgr, pin);
1498 list_remove(&pin->list_node);
1499 free(CONST_CAST(void *, pin->packet));
1503 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1504 port_run_fast(ofport);
1511 run(struct ofproto *ofproto_)
1513 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1514 struct ofport_dpif *ofport;
1515 struct ofbundle *bundle;
1518 if (mbridge_need_revalidate(ofproto->mbridge)) {
1519 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1520 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1521 mac_learning_flush(ofproto->ml);
1522 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1525 /* Do not perform any periodic activity below required by 'ofproto' while
1526 * waiting for flow restore to complete. */
1527 if (ofproto_get_flow_restore_wait()) {
1531 error = run_fast(ofproto_);
1536 if (ofproto->netflow) {
1537 if (netflow_run(ofproto->netflow)) {
1538 send_netflow_active_timeouts(ofproto);
1541 if (ofproto->sflow) {
1542 dpif_sflow_run(ofproto->sflow);
1544 if (ofproto->ipfix) {
1545 dpif_ipfix_run(ofproto->ipfix);
1548 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1551 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1556 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
1557 if (mac_learning_run(ofproto->ml)) {
1558 ofproto->backer->need_revalidate = REV_MAC_LEARNING;
1560 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1562 /* Check the consistency of a random facet, to aid debugging. */
1563 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
1564 if (time_msec() >= ofproto->consistency_rl
1565 && !classifier_is_empty(&ofproto->facets)
1566 && !ofproto->backer->need_revalidate) {
1567 struct cls_table *table;
1568 struct cls_rule *cr;
1569 struct facet *facet;
1571 ofproto->consistency_rl = time_msec() + 250;
1573 table = CONTAINER_OF(hmap_random_node(&ofproto->facets.tables),
1574 struct cls_table, hmap_node);
1575 cr = CONTAINER_OF(hmap_random_node(&table->rules), struct cls_rule,
1577 facet = CONTAINER_OF(cr, struct facet, cr);
1579 if (!facet_check_consistency(facet)) {
1580 ofproto->backer->need_revalidate = REV_INCONSISTENCY;
1583 ovs_rwlock_unlock(&ofproto->facets.rwlock);
1589 wait(struct ofproto *ofproto_)
1591 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1592 struct ofport_dpif *ofport;
1593 struct ofbundle *bundle;
1595 if (ofproto_get_flow_restore_wait()) {
1599 if (ofproto->sflow) {
1600 dpif_sflow_wait(ofproto->sflow);
1602 if (ofproto->ipfix) {
1603 dpif_ipfix_wait(ofproto->ipfix);
1605 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1608 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1609 bundle_wait(bundle);
1611 if (ofproto->netflow) {
1612 netflow_wait(ofproto->netflow);
1614 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
1615 mac_learning_wait(ofproto->ml);
1616 ovs_rwlock_unlock(&ofproto->ml->rwlock);
1618 if (ofproto->backer->need_revalidate) {
1619 /* Shouldn't happen, but if it does just go around again. */
1620 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1621 poll_immediate_wake();
1626 get_memory_usage(const struct ofproto *ofproto_, struct simap *usage)
1628 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1629 struct cls_cursor cursor;
1630 size_t n_subfacets = 0;
1631 struct facet *facet;
1633 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
1634 simap_increase(usage, "facets", classifier_count(&ofproto->facets));
1635 ovs_rwlock_unlock(&ofproto->facets.rwlock);
1637 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
1638 cls_cursor_init(&cursor, &ofproto->facets, NULL);
1639 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
1640 n_subfacets += list_size(&facet->subfacets);
1642 ovs_rwlock_unlock(&ofproto->facets.rwlock);
1643 simap_increase(usage, "subfacets", n_subfacets);
1647 flush(struct ofproto *ofproto_)
1649 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1650 struct subfacet *subfacet, *next_subfacet;
1651 struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
1655 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
1656 &ofproto->backer->subfacets) {
1657 if (subfacet->facet->ofproto != ofproto) {
1661 if (subfacet->path != SF_NOT_INSTALLED) {
1662 batch[n_batch++] = subfacet;
1663 if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
1664 subfacet_destroy_batch(ofproto->backer, batch, n_batch);
1668 subfacet_destroy(subfacet);
1673 subfacet_destroy_batch(ofproto->backer, batch, n_batch);
1678 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1679 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1681 *arp_match_ip = true;
1682 *actions = (OFPUTIL_A_OUTPUT |
1683 OFPUTIL_A_SET_VLAN_VID |
1684 OFPUTIL_A_SET_VLAN_PCP |
1685 OFPUTIL_A_STRIP_VLAN |
1686 OFPUTIL_A_SET_DL_SRC |
1687 OFPUTIL_A_SET_DL_DST |
1688 OFPUTIL_A_SET_NW_SRC |
1689 OFPUTIL_A_SET_NW_DST |
1690 OFPUTIL_A_SET_NW_TOS |
1691 OFPUTIL_A_SET_TP_SRC |
1692 OFPUTIL_A_SET_TP_DST |
1697 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1699 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1700 struct dpif_dp_stats s;
1701 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1704 strcpy(ots->name, "classifier");
1706 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1707 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes);
1708 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes);
1709 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes);
1711 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1712 ots->lookup_count = htonll(n_lookup);
1713 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1716 static struct ofport *
1719 struct ofport_dpif *port = xmalloc(sizeof *port);
1724 port_dealloc(struct ofport *port_)
1726 struct ofport_dpif *port = ofport_dpif_cast(port_);
1731 port_construct(struct ofport *port_)
1733 struct ofport_dpif *port = ofport_dpif_cast(port_);
1734 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1735 const struct netdev *netdev = port->up.netdev;
1736 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1737 struct dpif_port dpif_port;
1740 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1741 port->bundle = NULL;
1744 port->may_enable = true;
1745 port->stp_port = NULL;
1746 port->stp_state = STP_DISABLED;
1747 port->is_tunnel = false;
1751 port->realdev_ofp_port = 0;
1752 port->vlandev_vid = 0;
1753 port->carrier_seq = netdev_get_carrier_resets(netdev);
1755 if (netdev_vport_is_patch(netdev)) {
1756 /* By bailing out here, we don't submit the port to the sFlow module
1757 * to be considered for counter polling export. This is correct
1758 * because the patch port represents an interface that sFlow considers
1759 * to be "internal" to the switch as a whole, and therefore not an
1760 * candidate for counter polling. */
1761 port->odp_port = ODPP_NONE;
1762 ofport_update_peer(port);
1766 error = dpif_port_query_by_name(ofproto->backer->dpif,
1767 netdev_vport_get_dpif_port(netdev, namebuf,
1774 port->odp_port = dpif_port.port_no;
1776 if (netdev_get_tunnel_config(netdev)) {
1777 tnl_port_add(port, port->up.netdev, port->odp_port);
1778 port->is_tunnel = true;
1780 /* Sanity-check that a mapping doesn't already exist. This
1781 * shouldn't happen for non-tunnel ports. */
1782 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1783 VLOG_ERR("port %s already has an OpenFlow port number",
1785 dpif_port_destroy(&dpif_port);
1789 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1790 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1791 hash_odp_port(port->odp_port));
1792 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1794 dpif_port_destroy(&dpif_port);
1796 if (ofproto->sflow) {
1797 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1804 port_destruct(struct ofport *port_)
1806 struct ofport_dpif *port = ofport_dpif_cast(port_);
1807 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1808 const char *devname = netdev_get_name(port->up.netdev);
1809 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1810 const char *dp_port_name;
1812 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1813 ovs_rwlock_wrlock(&xlate_rwlock);
1814 xlate_ofport_remove(port);
1815 ovs_rwlock_unlock(&xlate_rwlock);
1817 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1819 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1820 /* The underlying device is still there, so delete it. This
1821 * happens when the ofproto is being destroyed, since the caller
1822 * assumes that removal of attached ports will happen as part of
1824 if (!port->is_tunnel) {
1825 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1830 port->peer->peer = NULL;
1834 if (port->odp_port != ODPP_NONE && !port->is_tunnel) {
1835 ovs_rwlock_wrlock(&ofproto->backer->odp_to_ofport_lock);
1836 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1837 ovs_rwlock_unlock(&ofproto->backer->odp_to_ofport_lock);
1841 sset_find_and_delete(&ofproto->ports, devname);
1842 sset_find_and_delete(&ofproto->ghost_ports, devname);
1843 bundle_remove(port_);
1844 set_cfm(port_, NULL);
1845 set_bfd(port_, NULL);
1846 if (ofproto->sflow) {
1847 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1854 port_modified(struct ofport *port_)
1856 struct ofport_dpif *port = ofport_dpif_cast(port_);
1858 if (port->bundle && port->bundle->bond) {
1859 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1863 cfm_set_netdev(port->cfm, port->up.netdev);
1867 bfd_set_netdev(port->bfd, port->up.netdev);
1870 if (port->is_tunnel && tnl_port_reconfigure(port, port->up.netdev,
1872 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1876 ofport_update_peer(port);
1880 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1882 struct ofport_dpif *port = ofport_dpif_cast(port_);
1883 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1884 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1886 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1887 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1888 OFPUTIL_PC_NO_PACKET_IN)) {
1889 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1891 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1892 bundle_update(port->bundle);
1898 set_sflow(struct ofproto *ofproto_,
1899 const struct ofproto_sflow_options *sflow_options)
1901 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1902 struct dpif_sflow *ds = ofproto->sflow;
1904 if (sflow_options) {
1906 struct ofport_dpif *ofport;
1908 ds = ofproto->sflow = dpif_sflow_create();
1909 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1910 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1912 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1914 dpif_sflow_set_options(ds, sflow_options);
1917 dpif_sflow_unref(ds);
1918 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1919 ofproto->sflow = NULL;
1927 struct ofproto *ofproto_,
1928 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1929 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1930 size_t n_flow_exporters_options)
1932 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1933 struct dpif_ipfix *di = ofproto->ipfix;
1934 bool has_options = bridge_exporter_options || flow_exporters_options;
1936 if (has_options && !di) {
1937 di = ofproto->ipfix = dpif_ipfix_create();
1941 /* Call set_options in any case to cleanly flush the flow
1942 * caches in the last exporters that are to be destroyed. */
1943 dpif_ipfix_set_options(
1944 di, bridge_exporter_options, flow_exporters_options,
1945 n_flow_exporters_options);
1948 dpif_ipfix_unref(di);
1949 ofproto->ipfix = NULL;
1957 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1959 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1966 struct ofproto_dpif *ofproto;
1968 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1969 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1970 ofport->cfm = cfm_create(ofport->up.netdev);
1973 if (cfm_configure(ofport->cfm, s)) {
1979 cfm_unref(ofport->cfm);
1985 get_cfm_status(const struct ofport *ofport_,
1986 struct ofproto_cfm_status *status)
1988 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1991 status->faults = cfm_get_fault(ofport->cfm);
1992 status->remote_opstate = cfm_get_opup(ofport->cfm);
1993 status->health = cfm_get_health(ofport->cfm);
1994 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
2002 set_bfd(struct ofport *ofport_, const struct smap *cfg)
2004 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
2005 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2009 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev),
2010 cfg, ofport->up.netdev);
2011 if (ofport->bfd != old) {
2012 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2019 get_bfd_status(struct ofport *ofport_, struct smap *smap)
2021 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2024 bfd_get_status(ofport->bfd, smap);
2031 /* Spanning Tree. */
2034 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
2036 struct ofproto_dpif *ofproto = ofproto_;
2037 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
2038 struct ofport_dpif *ofport;
2040 ofport = stp_port_get_aux(sp);
2042 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
2043 ofproto->up.name, port_num);
2045 struct eth_header *eth = pkt->l2;
2047 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
2048 if (eth_addr_is_zero(eth->eth_src)) {
2049 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
2050 "with unknown MAC", ofproto->up.name, port_num);
2052 send_packet(ofport, pkt);
2058 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
2060 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
2062 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2064 /* Only revalidate flows if the configuration changed. */
2065 if (!s != !ofproto->stp) {
2066 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2070 if (!ofproto->stp) {
2071 ofproto->stp = stp_create(ofproto_->name, s->system_id,
2072 send_bpdu_cb, ofproto);
2073 ofproto->stp_last_tick = time_msec();
2076 stp_set_bridge_id(ofproto->stp, s->system_id);
2077 stp_set_bridge_priority(ofproto->stp, s->priority);
2078 stp_set_hello_time(ofproto->stp, s->hello_time);
2079 stp_set_max_age(ofproto->stp, s->max_age);
2080 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
2082 struct ofport *ofport;
2084 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
2085 set_stp_port(ofport, NULL);
2088 stp_unref(ofproto->stp);
2089 ofproto->stp = NULL;
2096 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
2098 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2102 s->bridge_id = stp_get_bridge_id(ofproto->stp);
2103 s->designated_root = stp_get_designated_root(ofproto->stp);
2104 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
2113 update_stp_port_state(struct ofport_dpif *ofport)
2115 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2116 enum stp_state state;
2118 /* Figure out new state. */
2119 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
2123 if (ofport->stp_state != state) {
2124 enum ofputil_port_state of_state;
2127 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
2128 netdev_get_name(ofport->up.netdev),
2129 stp_state_name(ofport->stp_state),
2130 stp_state_name(state));
2131 if (stp_learn_in_state(ofport->stp_state)
2132 != stp_learn_in_state(state)) {
2133 /* xxx Learning action flows should also be flushed. */
2134 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2135 mac_learning_flush(ofproto->ml);
2136 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2138 fwd_change = stp_forward_in_state(ofport->stp_state)
2139 != stp_forward_in_state(state);
2141 ofproto->backer->need_revalidate = REV_STP;
2142 ofport->stp_state = state;
2143 ofport->stp_state_entered = time_msec();
2145 if (fwd_change && ofport->bundle) {
2146 bundle_update(ofport->bundle);
2149 /* Update the STP state bits in the OpenFlow port description. */
2150 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
2151 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
2152 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
2153 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
2154 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
2156 ofproto_port_set_state(&ofport->up, of_state);
2160 /* Configures STP on 'ofport_' using the settings defined in 's'. The
2161 * caller is responsible for assigning STP port numbers and ensuring
2162 * there are no duplicates. */
2164 set_stp_port(struct ofport *ofport_,
2165 const struct ofproto_port_stp_settings *s)
2167 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2168 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2169 struct stp_port *sp = ofport->stp_port;
2171 if (!s || !s->enable) {
2173 ofport->stp_port = NULL;
2174 stp_port_disable(sp);
2175 update_stp_port_state(ofport);
2178 } else if (sp && stp_port_no(sp) != s->port_num
2179 && ofport == stp_port_get_aux(sp)) {
2180 /* The port-id changed, so disable the old one if it's not
2181 * already in use by another port. */
2182 stp_port_disable(sp);
2185 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
2186 stp_port_enable(sp);
2188 stp_port_set_aux(sp, ofport);
2189 stp_port_set_priority(sp, s->priority);
2190 stp_port_set_path_cost(sp, s->path_cost);
2192 update_stp_port_state(ofport);
2198 get_stp_port_status(struct ofport *ofport_,
2199 struct ofproto_port_stp_status *s)
2201 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2202 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2203 struct stp_port *sp = ofport->stp_port;
2205 if (!ofproto->stp || !sp) {
2211 s->port_id = stp_port_get_id(sp);
2212 s->state = stp_port_get_state(sp);
2213 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
2214 s->role = stp_port_get_role(sp);
2215 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
2221 stp_run(struct ofproto_dpif *ofproto)
2224 long long int now = time_msec();
2225 long long int elapsed = now - ofproto->stp_last_tick;
2226 struct stp_port *sp;
2229 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
2230 ofproto->stp_last_tick = now;
2232 while (stp_get_changed_port(ofproto->stp, &sp)) {
2233 struct ofport_dpif *ofport = stp_port_get_aux(sp);
2236 update_stp_port_state(ofport);
2240 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
2241 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2242 mac_learning_flush(ofproto->ml);
2243 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2249 stp_wait(struct ofproto_dpif *ofproto)
2252 poll_timer_wait(1000);
2257 set_queues(struct ofport *ofport_, const struct ofproto_port_queue *qdscp,
2260 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2261 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2263 if (ofport->n_qdscp != n_qdscp
2264 || (n_qdscp && memcmp(ofport->qdscp, qdscp,
2265 n_qdscp * sizeof *qdscp))) {
2266 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2267 free(ofport->qdscp);
2268 ofport->qdscp = n_qdscp
2269 ? xmemdup(qdscp, n_qdscp * sizeof *qdscp)
2271 ofport->n_qdscp = n_qdscp;
2279 /* Expires all MAC learning entries associated with 'bundle' and forces its
2280 * ofproto to revalidate every flow.
2282 * Normally MAC learning entries are removed only from the ofproto associated
2283 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2284 * are removed from every ofproto. When patch ports and SLB bonds are in use
2285 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2286 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2287 * with the host from which it migrated. */
2289 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2291 struct ofproto_dpif *ofproto = bundle->ofproto;
2292 struct mac_learning *ml = ofproto->ml;
2293 struct mac_entry *mac, *next_mac;
2295 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2296 ovs_rwlock_wrlock(&ml->rwlock);
2297 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2298 if (mac->port.p == bundle) {
2300 struct ofproto_dpif *o;
2302 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2304 struct mac_entry *e;
2306 ovs_rwlock_wrlock(&o->ml->rwlock);
2307 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan);
2309 mac_learning_expire(o->ml, e);
2311 ovs_rwlock_unlock(&o->ml->rwlock);
2316 mac_learning_expire(ml, mac);
2319 ovs_rwlock_unlock(&ml->rwlock);
2322 static struct ofbundle *
2323 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2325 struct ofbundle *bundle;
2327 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2328 &ofproto->bundles) {
2329 if (bundle->aux == aux) {
2337 bundle_update(struct ofbundle *bundle)
2339 struct ofport_dpif *port;
2341 bundle->floodable = true;
2342 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2343 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2344 || !stp_forward_in_state(port->stp_state)) {
2345 bundle->floodable = false;
2352 bundle_del_port(struct ofport_dpif *port)
2354 struct ofbundle *bundle = port->bundle;
2356 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2358 list_remove(&port->bundle_node);
2359 port->bundle = NULL;
2362 lacp_slave_unregister(bundle->lacp, port);
2365 bond_slave_unregister(bundle->bond, port);
2368 bundle_update(bundle);
2372 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2373 struct lacp_slave_settings *lacp)
2375 struct ofport_dpif *port;
2377 port = get_ofp_port(bundle->ofproto, ofp_port);
2382 if (port->bundle != bundle) {
2383 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2385 bundle_remove(&port->up);
2388 port->bundle = bundle;
2389 list_push_back(&bundle->ports, &port->bundle_node);
2390 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2391 || !stp_forward_in_state(port->stp_state)) {
2392 bundle->floodable = false;
2396 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2397 lacp_slave_register(bundle->lacp, port, lacp);
2404 bundle_destroy(struct ofbundle *bundle)
2406 struct ofproto_dpif *ofproto;
2407 struct ofport_dpif *port, *next_port;
2413 ofproto = bundle->ofproto;
2414 mbridge_unregister_bundle(ofproto->mbridge, bundle->aux);
2416 ovs_rwlock_wrlock(&xlate_rwlock);
2417 xlate_bundle_remove(bundle);
2418 ovs_rwlock_unlock(&xlate_rwlock);
2420 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2421 bundle_del_port(port);
2424 bundle_flush_macs(bundle, true);
2425 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2427 free(bundle->trunks);
2428 lacp_unref(bundle->lacp);
2429 bond_unref(bundle->bond);
2434 bundle_set(struct ofproto *ofproto_, void *aux,
2435 const struct ofproto_bundle_settings *s)
2437 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2438 bool need_flush = false;
2439 struct ofport_dpif *port;
2440 struct ofbundle *bundle;
2441 unsigned long *trunks;
2447 bundle_destroy(bundle_lookup(ofproto, aux));
2451 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2452 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2454 bundle = bundle_lookup(ofproto, aux);
2456 bundle = xmalloc(sizeof *bundle);
2458 bundle->ofproto = ofproto;
2459 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2460 hash_pointer(aux, 0));
2462 bundle->name = NULL;
2464 list_init(&bundle->ports);
2465 bundle->vlan_mode = PORT_VLAN_TRUNK;
2467 bundle->trunks = NULL;
2468 bundle->use_priority_tags = s->use_priority_tags;
2469 bundle->lacp = NULL;
2470 bundle->bond = NULL;
2472 bundle->floodable = true;
2473 mbridge_register_bundle(ofproto->mbridge, bundle);
2476 if (!bundle->name || strcmp(s->name, bundle->name)) {
2478 bundle->name = xstrdup(s->name);
2483 if (!bundle->lacp) {
2484 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2485 bundle->lacp = lacp_create();
2487 lacp_configure(bundle->lacp, s->lacp);
2489 lacp_unref(bundle->lacp);
2490 bundle->lacp = NULL;
2493 /* Update set of ports. */
2495 for (i = 0; i < s->n_slaves; i++) {
2496 if (!bundle_add_port(bundle, s->slaves[i],
2497 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2501 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2502 struct ofport_dpif *next_port;
2504 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2505 for (i = 0; i < s->n_slaves; i++) {
2506 if (s->slaves[i] == port->up.ofp_port) {
2511 bundle_del_port(port);
2515 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2517 if (list_is_empty(&bundle->ports)) {
2518 bundle_destroy(bundle);
2522 /* Set VLAN tagging mode */
2523 if (s->vlan_mode != bundle->vlan_mode
2524 || s->use_priority_tags != bundle->use_priority_tags) {
2525 bundle->vlan_mode = s->vlan_mode;
2526 bundle->use_priority_tags = s->use_priority_tags;
2531 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2532 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2534 if (vlan != bundle->vlan) {
2535 bundle->vlan = vlan;
2539 /* Get trunked VLANs. */
2540 switch (s->vlan_mode) {
2541 case PORT_VLAN_ACCESS:
2545 case PORT_VLAN_TRUNK:
2546 trunks = CONST_CAST(unsigned long *, s->trunks);
2549 case PORT_VLAN_NATIVE_UNTAGGED:
2550 case PORT_VLAN_NATIVE_TAGGED:
2551 if (vlan != 0 && (!s->trunks
2552 || !bitmap_is_set(s->trunks, vlan)
2553 || bitmap_is_set(s->trunks, 0))) {
2554 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2556 trunks = bitmap_clone(s->trunks, 4096);
2558 trunks = bitmap_allocate1(4096);
2560 bitmap_set1(trunks, vlan);
2561 bitmap_set0(trunks, 0);
2563 trunks = CONST_CAST(unsigned long *, s->trunks);
2570 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2571 free(bundle->trunks);
2572 if (trunks == s->trunks) {
2573 bundle->trunks = vlan_bitmap_clone(trunks);
2575 bundle->trunks = trunks;
2580 if (trunks != s->trunks) {
2585 if (!list_is_short(&bundle->ports)) {
2586 bundle->ofproto->has_bonded_bundles = true;
2588 if (bond_reconfigure(bundle->bond, s->bond)) {
2589 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2592 bundle->bond = bond_create(s->bond);
2593 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2596 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2597 bond_slave_register(bundle->bond, port, port->up.netdev);
2600 bond_unref(bundle->bond);
2601 bundle->bond = NULL;
2604 /* If we changed something that would affect MAC learning, un-learn
2605 * everything on this port and force flow revalidation. */
2607 bundle_flush_macs(bundle, false);
2614 bundle_remove(struct ofport *port_)
2616 struct ofport_dpif *port = ofport_dpif_cast(port_);
2617 struct ofbundle *bundle = port->bundle;
2620 bundle_del_port(port);
2621 if (list_is_empty(&bundle->ports)) {
2622 bundle_destroy(bundle);
2623 } else if (list_is_short(&bundle->ports)) {
2624 bond_unref(bundle->bond);
2625 bundle->bond = NULL;
2631 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2633 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2634 struct ofport_dpif *port = port_;
2635 uint8_t ea[ETH_ADDR_LEN];
2638 error = netdev_get_etheraddr(port->up.netdev, ea);
2640 struct ofpbuf packet;
2643 ofpbuf_init(&packet, 0);
2644 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2646 memcpy(packet_pdu, pdu, pdu_size);
2648 send_packet(port, &packet);
2649 ofpbuf_uninit(&packet);
2651 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2652 "%s (%s)", port->bundle->name,
2653 netdev_get_name(port->up.netdev), ovs_strerror(error));
2658 bundle_send_learning_packets(struct ofbundle *bundle)
2660 struct ofproto_dpif *ofproto = bundle->ofproto;
2661 struct ofpbuf *learning_packet;
2662 int error, n_packets, n_errors;
2663 struct mac_entry *e;
2664 struct list packets;
2666 list_init(&packets);
2667 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
2668 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2669 if (e->port.p != bundle) {
2672 learning_packet = bond_compose_learning_packet(bundle->bond,
2675 learning_packet->private_p = port_void;
2676 list_push_back(&packets, &learning_packet->list_node);
2679 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2681 error = n_packets = n_errors = 0;
2682 LIST_FOR_EACH (learning_packet, list_node, &packets) {
2685 ret = send_packet(learning_packet->private_p, learning_packet);
2692 ofpbuf_list_delete(&packets);
2695 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2696 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2697 "packets, last error was: %s",
2698 bundle->name, n_errors, n_packets, ovs_strerror(error));
2700 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2701 bundle->name, n_packets);
2706 bundle_run(struct ofbundle *bundle)
2709 lacp_run(bundle->lacp, send_pdu_cb);
2712 struct ofport_dpif *port;
2714 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2715 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2718 if (bond_run(bundle->bond, lacp_status(bundle->lacp))) {
2719 bundle->ofproto->backer->need_revalidate = REV_BOND;
2722 if (bond_should_send_learning_packets(bundle->bond)) {
2723 bundle_send_learning_packets(bundle);
2729 bundle_wait(struct ofbundle *bundle)
2732 lacp_wait(bundle->lacp);
2735 bond_wait(bundle->bond);
2742 mirror_set__(struct ofproto *ofproto_, void *aux,
2743 const struct ofproto_mirror_settings *s)
2745 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2746 struct ofbundle **srcs, **dsts;
2751 mirror_destroy(ofproto->mbridge, aux);
2755 srcs = xmalloc(s->n_srcs * sizeof *srcs);
2756 dsts = xmalloc(s->n_dsts * sizeof *dsts);
2758 for (i = 0; i < s->n_srcs; i++) {
2759 srcs[i] = bundle_lookup(ofproto, s->srcs[i]);
2762 for (i = 0; i < s->n_dsts; i++) {
2763 dsts[i] = bundle_lookup(ofproto, s->dsts[i]);
2766 error = mirror_set(ofproto->mbridge, aux, s->name, srcs, s->n_srcs, dsts,
2767 s->n_dsts, s->src_vlans,
2768 bundle_lookup(ofproto, s->out_bundle), s->out_vlan);
2775 mirror_get_stats__(struct ofproto *ofproto, void *aux,
2776 uint64_t *packets, uint64_t *bytes)
2779 return mirror_get_stats(ofproto_dpif_cast(ofproto)->mbridge, aux, packets,
2784 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2786 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2787 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2788 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2789 mac_learning_flush(ofproto->ml);
2791 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2796 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2798 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2799 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2800 return bundle && mirror_bundle_out(ofproto->mbridge, bundle) != 0;
2804 forward_bpdu_changed(struct ofproto *ofproto_)
2806 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2807 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2811 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2814 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2815 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
2816 mac_learning_set_idle_time(ofproto->ml, idle_time);
2817 mac_learning_set_max_entries(ofproto->ml, max_entries);
2818 ovs_rwlock_unlock(&ofproto->ml->rwlock);
2823 static struct ofport_dpif *
2824 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2826 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2827 return ofport ? ofport_dpif_cast(ofport) : NULL;
2830 static struct ofport_dpif *
2831 get_odp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
2833 struct ofport_dpif *port = odp_port_to_ofport(ofproto->backer, odp_port);
2834 return port && &ofproto->up == port->up.ofproto ? port : NULL;
2838 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2839 struct ofproto_port *ofproto_port,
2840 struct dpif_port *dpif_port)
2842 ofproto_port->name = dpif_port->name;
2843 ofproto_port->type = dpif_port->type;
2844 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2848 ofport_update_peer(struct ofport_dpif *ofport)
2850 const struct ofproto_dpif *ofproto;
2851 struct dpif_backer *backer;
2854 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2858 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2859 backer->need_revalidate = REV_RECONFIGURE;
2862 ofport->peer->peer = NULL;
2863 ofport->peer = NULL;
2866 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2871 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2872 struct ofport *peer_ofport;
2873 struct ofport_dpif *peer;
2876 if (ofproto->backer != backer) {
2880 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2885 peer = ofport_dpif_cast(peer_ofport);
2886 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2887 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2889 ofport->peer = peer;
2890 ofport->peer->peer = ofport;
2900 port_run_fast(struct ofport_dpif *ofport)
2902 if (ofport->cfm && cfm_should_send_ccm(ofport->cfm)) {
2903 struct ofpbuf packet;
2905 ofpbuf_init(&packet, 0);
2906 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.pp.hw_addr);
2907 send_packet(ofport, &packet);
2908 ofpbuf_uninit(&packet);
2911 if (ofport->bfd && bfd_should_send_packet(ofport->bfd)) {
2912 struct ofpbuf packet;
2914 ofpbuf_init(&packet, 0);
2915 bfd_put_packet(ofport->bfd, &packet, ofport->up.pp.hw_addr);
2916 send_packet(ofport, &packet);
2917 ofpbuf_uninit(&packet);
2922 port_run(struct ofport_dpif *ofport)
2924 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2925 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2926 bool enable = netdev_get_carrier(ofport->up.netdev);
2927 bool cfm_enable = false;
2928 bool bfd_enable = false;
2930 ofport->carrier_seq = carrier_seq;
2932 port_run_fast(ofport);
2935 int cfm_opup = cfm_get_opup(ofport->cfm);
2937 cfm_run(ofport->cfm);
2938 cfm_enable = !cfm_get_fault(ofport->cfm);
2940 if (cfm_opup >= 0) {
2941 cfm_enable = cfm_enable && cfm_opup;
2946 bfd_run(ofport->bfd);
2947 bfd_enable = bfd_forwarding(ofport->bfd);
2950 if (ofport->bfd || ofport->cfm) {
2951 enable = enable && (cfm_enable || bfd_enable);
2954 if (ofport->bundle) {
2955 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2956 if (carrier_changed) {
2957 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2961 if (ofport->may_enable != enable) {
2962 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2963 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2966 ofport->may_enable = enable;
2970 port_wait(struct ofport_dpif *ofport)
2973 cfm_wait(ofport->cfm);
2977 bfd_wait(ofport->bfd);
2982 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2983 struct ofproto_port *ofproto_port)
2985 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2986 struct dpif_port dpif_port;
2989 if (sset_contains(&ofproto->ghost_ports, devname)) {
2990 const char *type = netdev_get_type_from_name(devname);
2992 /* We may be called before ofproto->up.port_by_name is populated with
2993 * the appropriate ofport. For this reason, we must get the name and
2994 * type from the netdev layer directly. */
2996 const struct ofport *ofport;
2998 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2999 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
3000 ofproto_port->name = xstrdup(devname);
3001 ofproto_port->type = xstrdup(type);
3007 if (!sset_contains(&ofproto->ports, devname)) {
3010 error = dpif_port_query_by_name(ofproto->backer->dpif,
3011 devname, &dpif_port);
3013 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3019 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3021 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3022 const char *devname = netdev_get_name(netdev);
3023 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3024 const char *dp_port_name;
3026 if (netdev_vport_is_patch(netdev)) {
3027 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3031 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3032 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3033 odp_port_t port_no = ODPP_NONE;
3036 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3040 if (netdev_get_tunnel_config(netdev)) {
3041 simap_put(&ofproto->backer->tnl_backers,
3042 dp_port_name, odp_to_u32(port_no));
3046 if (netdev_get_tunnel_config(netdev)) {
3047 sset_add(&ofproto->ghost_ports, devname);
3049 sset_add(&ofproto->ports, devname);
3055 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3057 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3058 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
3065 sset_find_and_delete(&ofproto->ghost_ports,
3066 netdev_get_name(ofport->up.netdev));
3067 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3068 if (!ofport->is_tunnel && !netdev_vport_is_patch(ofport->up.netdev)) {
3069 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3071 /* The caller is going to close ofport->up.netdev. If this is a
3072 * bonded port, then the bond is using that netdev, so remove it
3073 * from the bond. The client will need to reconfigure everything
3074 * after deleting ports, so then the slave will get re-added. */
3075 bundle_remove(&ofport->up);
3082 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3084 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3089 error = netdev_get_stats(ofport->up.netdev, stats);
3091 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3092 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3094 /* ofproto->stats.tx_packets represents packets that we created
3095 * internally and sent to some port (e.g. packets sent with
3096 * send_packet()). Account for them as if they had come from
3097 * OFPP_LOCAL and got forwarded. */
3099 if (stats->rx_packets != UINT64_MAX) {
3100 stats->rx_packets += ofproto->stats.tx_packets;
3103 if (stats->rx_bytes != UINT64_MAX) {
3104 stats->rx_bytes += ofproto->stats.tx_bytes;
3107 /* ofproto->stats.rx_packets represents packets that were received on
3108 * some port and we processed internally and dropped (e.g. STP).
3109 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3111 if (stats->tx_packets != UINT64_MAX) {
3112 stats->tx_packets += ofproto->stats.rx_packets;
3115 if (stats->tx_bytes != UINT64_MAX) {
3116 stats->tx_bytes += ofproto->stats.rx_bytes;
3123 struct port_dump_state {
3128 struct ofproto_port port;
3133 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3135 *statep = xzalloc(sizeof(struct port_dump_state));
3140 port_dump_next(const struct ofproto *ofproto_, void *state_,
3141 struct ofproto_port *port)
3143 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3144 struct port_dump_state *state = state_;
3145 const struct sset *sset;
3146 struct sset_node *node;
3148 if (state->has_port) {
3149 ofproto_port_destroy(&state->port);
3150 state->has_port = false;
3152 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3153 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3156 error = port_query_by_name(ofproto_, node->name, &state->port);
3158 *port = state->port;
3159 state->has_port = true;
3161 } else if (error != ENODEV) {
3166 if (!state->ghost) {
3167 state->ghost = true;
3170 return port_dump_next(ofproto_, state_, port);
3177 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3179 struct port_dump_state *state = state_;
3181 if (state->has_port) {
3182 ofproto_port_destroy(&state->port);
3189 port_poll(const struct ofproto *ofproto_, char **devnamep)
3191 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3193 if (ofproto->port_poll_errno) {
3194 int error = ofproto->port_poll_errno;
3195 ofproto->port_poll_errno = 0;
3199 if (sset_is_empty(&ofproto->port_poll_set)) {
3203 *devnamep = sset_pop(&ofproto->port_poll_set);
3208 port_poll_wait(const struct ofproto *ofproto_)
3210 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3211 dpif_port_poll_wait(ofproto->backer->dpif);
3215 port_is_lacp_current(const struct ofport *ofport_)
3217 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3218 return (ofport->bundle && ofport->bundle->lacp
3219 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3223 /* Upcall handling. */
3225 struct flow_miss_op {
3226 struct dpif_op dpif_op;
3228 uint64_t slow_stub[128 / 8]; /* Buffer for compose_slow_path() */
3229 struct xlate_out xout;
3230 bool xout_garbage; /* 'xout' needs to be uninitialized? */
3232 struct ofpbuf mask; /* Flow mask for "put" ops. */
3233 struct odputil_keybuf maskbuf;
3235 /* If this is a "put" op, then a pointer to the subfacet that should
3236 * be marked as uninstalled if the operation fails. */
3237 struct subfacet *subfacet;
3240 /* Figures out whether a flow that missed in 'ofproto', whose details are in
3241 * 'miss' masked by 'wc', is likely to be worth tracking in detail in userspace
3242 * and (usually) installing a datapath flow. The answer is usually "yes" (a
3243 * return value of true). However, for short flows the cost of bookkeeping is
3244 * much higher than the benefits, so when the datapath holds a large number of
3245 * flows we impose some heuristics to decide which flows are likely to be worth
3248 flow_miss_should_make_facet(struct flow_miss *miss)
3250 struct dpif_backer *backer = miss->ofproto->backer;
3253 switch (flow_miss_model) {
3254 case OFPROTO_HANDLE_MISS_AUTO:
3256 case OFPROTO_HANDLE_MISS_WITH_FACETS:
3258 case OFPROTO_HANDLE_MISS_WITHOUT_FACETS:
3262 if (!backer->governor) {
3265 n_subfacets = hmap_count(&backer->subfacets);
3266 if (n_subfacets * 2 <= flow_eviction_threshold) {
3270 backer->governor = governor_create();
3273 hash = flow_hash_in_wildcards(&miss->flow, &miss->xout.wc, 0);
3274 return governor_should_install_flow(backer->governor, hash,
3275 list_size(&miss->packets));
3278 /* Handles 'miss', which matches 'facet'. May add any required datapath
3279 * operations to 'ops', incrementing '*n_ops' for each new op.
3281 * All of the packets in 'miss' are considered to have arrived at time
3282 * 'miss->stats.used'. This is really important only for new facets: if we
3283 * just called time_msec() here, then the new subfacet or its packets could
3284 * look (occasionally) as though it was used some time after the facet was
3285 * used. That can make a one-packet flow look like it has a nonzero duration,
3286 * which looks odd in e.g. NetFlow statistics. */
3288 handle_flow_miss_with_facet(struct flow_miss *miss, struct facet *facet,
3289 struct flow_miss_op *ops, size_t *n_ops)
3291 enum subfacet_path want_path;
3292 struct subfacet *subfacet;
3294 facet->packet_count += miss->stats.n_packets;
3295 facet->prev_packet_count += miss->stats.n_packets;
3296 facet->byte_count += miss->stats.n_bytes;
3297 facet->prev_byte_count += miss->stats.n_bytes;
3299 want_path = facet->xout.slow ? SF_SLOW_PATH : SF_FAST_PATH;
3301 /* Don't install the flow if it's the result of the "userspace"
3302 * action for an already installed facet. This can occur when a
3303 * datapath flow with wildcards has a "userspace" action and flows
3304 * sent to userspace result in a different subfacet, which will then
3305 * be rejected as overlapping by the datapath. */
3306 if (miss->upcall_type == DPIF_UC_ACTION
3307 && !list_is_empty(&facet->subfacets)) {
3311 subfacet = subfacet_create(facet, miss);
3312 if (subfacet->path != want_path) {
3313 struct flow_miss_op *op = &ops[(*n_ops)++];
3314 struct dpif_flow_put *put = &op->dpif_op.u.flow_put;
3316 subfacet->path = want_path;
3318 ofpbuf_use_stack(&op->mask, &op->maskbuf, sizeof op->maskbuf);
3319 if (enable_megaflows) {
3320 odp_flow_key_from_mask(&op->mask, &facet->xout.wc.masks,
3321 &miss->flow, UINT32_MAX);
3324 op->xout_garbage = false;
3325 op->dpif_op.type = DPIF_OP_FLOW_PUT;
3326 op->subfacet = subfacet;
3327 put->flags = DPIF_FP_CREATE;
3328 put->key = miss->key;
3329 put->key_len = miss->key_len;
3330 put->mask = op->mask.data;
3331 put->mask_len = op->mask.size;
3333 if (want_path == SF_FAST_PATH) {
3334 put->actions = facet->xout.odp_actions.data;
3335 put->actions_len = facet->xout.odp_actions.size;
3337 compose_slow_path(facet->ofproto, &miss->flow, facet->xout.slow,
3338 op->slow_stub, sizeof op->slow_stub,
3339 &put->actions, &put->actions_len);
3345 /* Handles flow miss 'miss'. May add any required datapath operations
3346 * to 'ops', incrementing '*n_ops' for each new op. */
3348 handle_flow_miss(struct flow_miss *miss, struct flow_miss_op *ops,
3351 struct facet *facet;
3353 miss->ofproto->n_missed += list_size(&miss->packets);
3355 facet = facet_lookup_valid(miss->ofproto, &miss->flow);
3357 /* There does not exist a bijection between 'struct flow' and datapath
3358 * flow keys with fitness ODP_FIT_TO_LITTLE. This breaks a fundamental
3359 * assumption used throughout the facet and subfacet handling code.
3360 * Since we have to handle these misses in userspace anyway, we simply
3361 * skip facet creation, avoiding the problem altogether. */
3362 if (miss->key_fitness == ODP_FIT_TOO_LITTLE
3363 || !flow_miss_should_make_facet(miss)) {
3367 facet = facet_create(miss);
3369 handle_flow_miss_with_facet(miss, facet, ops, n_ops);
3372 static struct drop_key *
3373 drop_key_lookup(const struct dpif_backer *backer, const struct nlattr *key,
3376 struct drop_key *drop_key;
3378 HMAP_FOR_EACH_WITH_HASH (drop_key, hmap_node, hash_bytes(key, key_len, 0),
3379 &backer->drop_keys) {
3380 if (drop_key->key_len == key_len
3381 && !memcmp(drop_key->key, key, key_len)) {
3389 drop_key_clear(struct dpif_backer *backer)
3391 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
3392 struct drop_key *drop_key, *next;
3394 HMAP_FOR_EACH_SAFE (drop_key, next, hmap_node, &backer->drop_keys) {
3397 error = dpif_flow_del(backer->dpif, drop_key->key, drop_key->key_len,
3399 if (error && !VLOG_DROP_WARN(&rl)) {
3400 struct ds ds = DS_EMPTY_INITIALIZER;
3401 odp_flow_key_format(drop_key->key, drop_key->key_len, &ds);
3402 VLOG_WARN("Failed to delete drop key (%s) (%s)",
3403 ovs_strerror(error), ds_cstr(&ds));
3407 hmap_remove(&backer->drop_keys, &drop_key->hmap_node);
3408 drop_key_destroy(drop_key);
3411 udpif_drop_key_clear(backer->udpif);
3415 handle_flow_misses(struct dpif_backer *backer, struct flow_miss_batch *fmb)
3417 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH];
3418 struct dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH];
3419 struct flow_miss *miss;
3422 /* Process each element in the to-do list, constructing the set of
3423 * operations to batch. */
3425 HMAP_FOR_EACH (miss, hmap_node, &fmb->misses) {
3426 handle_flow_miss(miss, flow_miss_ops, &n_ops);
3428 ovs_assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
3430 /* Execute batch. */
3431 for (i = 0; i < n_ops; i++) {
3432 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
3434 dpif_operate(backer->dpif, dpif_ops, n_ops);
3436 for (i = 0; i < n_ops; i++) {
3437 if (dpif_ops[i]->error != 0
3438 && flow_miss_ops[i].dpif_op.type == DPIF_OP_FLOW_PUT
3439 && flow_miss_ops[i].subfacet) {
3440 struct subfacet *subfacet = flow_miss_ops[i].subfacet;
3442 COVERAGE_INC(subfacet_install_fail);
3444 /* Zero-out subfacet counters when installation failed, but
3445 * datapath reported hits. This should not happen and
3446 * indicates a bug, since if the datapath flow exists, we
3447 * should not be attempting to create a new subfacet. A
3448 * buggy datapath could trigger this, so just zero out the
3449 * counters and log an error. */
3450 if (subfacet->dp_packet_count || subfacet->dp_byte_count) {
3451 VLOG_ERR_RL(&rl, "failed to install subfacet for which "
3452 "datapath reported hits");
3453 subfacet->dp_packet_count = subfacet->dp_byte_count = 0;
3456 subfacet->path = SF_NOT_INSTALLED;
3462 handle_sflow_upcall(struct dpif_backer *backer,
3463 const struct dpif_upcall *upcall)
3465 struct ofproto_dpif *ofproto;
3466 union user_action_cookie cookie;
3468 odp_port_t odp_in_port;
3470 if (xlate_receive(backer, upcall->packet, upcall->key, upcall->key_len,
3471 &flow, NULL, &ofproto, &odp_in_port)
3472 || !ofproto->sflow) {
3476 memset(&cookie, 0, sizeof cookie);
3477 memcpy(&cookie, nl_attr_get(upcall->userdata), sizeof cookie.sflow);
3478 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
3479 odp_in_port, &cookie);
3483 handle_flow_sample_upcall(struct dpif_backer *backer,
3484 const struct dpif_upcall *upcall)
3486 struct ofproto_dpif *ofproto;
3487 union user_action_cookie cookie;
3490 if (xlate_receive(backer, upcall->packet, upcall->key, upcall->key_len,
3491 &flow, NULL, &ofproto, NULL)
3492 || !ofproto->ipfix) {
3496 memset(&cookie, 0, sizeof cookie);
3497 memcpy(&cookie, nl_attr_get(upcall->userdata), sizeof cookie.flow_sample);
3499 /* The flow reflects exactly the contents of the packet. Sample
3500 * the packet using it. */
3501 dpif_ipfix_flow_sample(ofproto->ipfix, upcall->packet, &flow,
3502 cookie.flow_sample.collector_set_id,
3503 cookie.flow_sample.probability,
3504 cookie.flow_sample.obs_domain_id,
3505 cookie.flow_sample.obs_point_id);
3509 handle_ipfix_upcall(struct dpif_backer *backer,
3510 const struct dpif_upcall *upcall)
3512 struct ofproto_dpif *ofproto;
3515 if (xlate_receive(backer, upcall->packet, upcall->key, upcall->key_len,
3516 &flow, NULL, &ofproto, NULL)
3517 || !ofproto->ipfix) {
3521 /* The flow reflects exactly the contents of the packet. Sample
3522 * the packet using it. */
3523 dpif_ipfix_bridge_sample(ofproto->ipfix, upcall->packet, &flow);
3527 handle_upcalls(struct dpif_backer *backer)
3529 struct flow_miss_batch *fmb;
3532 for (n_processed = 0; n_processed < FLOW_MISS_MAX_BATCH; n_processed++) {
3533 struct upcall *upcall = upcall_next(backer->udpif);
3539 switch (upcall->type) {
3541 handle_sflow_upcall(backer, &upcall->dpif_upcall);
3544 case FLOW_SAMPLE_UPCALL:
3545 handle_flow_sample_upcall(backer, &upcall->dpif_upcall);
3549 handle_ipfix_upcall(backer, &upcall->dpif_upcall);
3559 upcall_destroy(upcall);
3562 for (n_processed = 0; n_processed < FLOW_MISS_MAX_BATCH; n_processed++) {
3563 struct drop_key *drop_key = drop_key_next(backer->udpif);
3568 if (!drop_key_lookup(backer, drop_key->key, drop_key->key_len)) {
3569 hmap_insert(&backer->drop_keys, &drop_key->hmap_node,
3570 hash_bytes(drop_key->key, drop_key->key_len, 0));
3571 dpif_flow_put(backer->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
3572 drop_key->key, drop_key->key_len,
3573 NULL, 0, NULL, 0, NULL);
3575 drop_key_destroy(drop_key);
3579 fmb = flow_miss_batch_next(backer->udpif);
3581 handle_flow_misses(backer, fmb);
3582 flow_miss_batch_destroy(fmb);
3586 /* Flow expiration. */
3588 static int subfacet_max_idle(const struct dpif_backer *);
3589 static void update_stats(struct dpif_backer *);
3590 static void rule_expire(struct rule_dpif *);
3591 static void expire_subfacets(struct dpif_backer *, int dp_max_idle);
3593 /* This function is called periodically by run(). Its job is to collect
3594 * updates for the flows that have been installed into the datapath, most
3595 * importantly when they last were used, and then use that information to
3596 * expire flows that have not been used recently.
3598 * Returns the number of milliseconds after which it should be called again. */
3600 expire(struct dpif_backer *backer)
3602 struct ofproto_dpif *ofproto;
3606 /* Periodically clear out the drop keys in an effort to keep them
3607 * relatively few. */
3608 drop_key_clear(backer);
3610 /* Update stats for each flow in the backer. */
3611 update_stats(backer);
3613 n_subfacets = hmap_count(&backer->subfacets);
3615 struct subfacet *subfacet;
3616 long long int total, now;
3620 HMAP_FOR_EACH (subfacet, hmap_node, &backer->subfacets) {
3621 total += now - subfacet->created;
3623 backer->avg_subfacet_life += total / n_subfacets;
3625 backer->avg_subfacet_life /= 2;
3627 backer->avg_n_subfacet += n_subfacets;
3628 backer->avg_n_subfacet /= 2;
3630 backer->max_n_subfacet = MAX(backer->max_n_subfacet, n_subfacets);
3632 max_idle = subfacet_max_idle(backer);
3633 expire_subfacets(backer, max_idle);
3635 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
3636 struct rule *rule, *next_rule;
3638 if (ofproto->backer != backer) {
3642 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
3644 ovs_mutex_lock(&ofproto->up.expirable_mutex);
3645 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
3646 &ofproto->up.expirable) {
3647 rule_expire(rule_dpif_cast(rule));
3649 ovs_mutex_unlock(&ofproto->up.expirable_mutex);
3651 /* All outstanding data in existing flows has been accounted, so it's a
3652 * good time to do bond rebalancing. */
3653 if (ofproto->has_bonded_bundles) {
3654 struct ofbundle *bundle;
3656 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
3658 bond_rebalance(bundle->bond);
3664 return MIN(max_idle, 1000);
3667 /* Updates flow table statistics given that the datapath just reported 'stats'
3668 * as 'subfacet''s statistics. */
3670 update_subfacet_stats(struct subfacet *subfacet,
3671 const struct dpif_flow_stats *stats)
3673 struct facet *facet = subfacet->facet;
3674 struct dpif_flow_stats diff;
3676 diff.tcp_flags = stats->tcp_flags;
3677 diff.used = stats->used;
3679 if (stats->n_packets >= subfacet->dp_packet_count) {
3680 diff.n_packets = stats->n_packets - subfacet->dp_packet_count;
3682 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
3686 if (stats->n_bytes >= subfacet->dp_byte_count) {
3687 diff.n_bytes = stats->n_bytes - subfacet->dp_byte_count;
3689 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
3693 facet->ofproto->n_hit += diff.n_packets;
3694 subfacet->dp_packet_count = stats->n_packets;
3695 subfacet->dp_byte_count = stats->n_bytes;
3696 subfacet_update_stats(subfacet, &diff);
3698 if (facet->accounted_bytes < facet->byte_count) {
3700 facet_account(facet);
3701 facet->accounted_bytes = facet->byte_count;
3705 /* 'key' with length 'key_len' bytes is a flow in 'dpif' that we know nothing
3706 * about, or a flow that shouldn't be installed but was anyway. Delete it. */
3708 delete_unexpected_flow(struct dpif_backer *backer,
3709 const struct nlattr *key, size_t key_len)
3711 if (!VLOG_DROP_WARN(&rl)) {
3715 odp_flow_key_format(key, key_len, &s);
3716 VLOG_WARN("unexpected flow: %s", ds_cstr(&s));
3720 COVERAGE_INC(facet_unexpected);
3721 dpif_flow_del(backer->dpif, key, key_len, NULL);
3724 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
3726 * This function also pushes statistics updates to rules which each facet
3727 * resubmits into. Generally these statistics will be accurate. However, if a
3728 * facet changes the rule it resubmits into at some time in between
3729 * update_stats() runs, it is possible that statistics accrued to the
3730 * old rule will be incorrectly attributed to the new rule. This could be
3731 * avoided by calling update_stats() whenever rules are created or
3732 * deleted. However, the performance impact of making so many calls to the
3733 * datapath do not justify the benefit of having perfectly accurate statistics.
3735 * In addition, this function maintains per ofproto flow hit counts. The patch
3736 * port is not treated specially. e.g. A packet ingress from br0 patched into
3737 * br1 will increase the hit count of br0 by 1, however, does not affect
3738 * the hit or miss counts of br1.
3741 update_stats(struct dpif_backer *backer)
3743 const struct dpif_flow_stats *stats;
3744 struct dpif_flow_dump dump;
3745 const struct nlattr *key, *mask;
3746 size_t key_len, mask_len;
3748 dpif_flow_dump_start(&dump, backer->dpif);
3749 while (dpif_flow_dump_next(&dump, &key, &key_len,
3750 &mask, &mask_len, NULL, NULL, &stats)) {
3751 struct subfacet *subfacet;
3754 key_hash = odp_flow_key_hash(key, key_len);
3755 subfacet = subfacet_find(backer, key, key_len, key_hash);
3756 switch (subfacet ? subfacet->path : SF_NOT_INSTALLED) {
3758 update_subfacet_stats(subfacet, stats);
3762 /* Stats are updated per-packet. */
3765 case SF_NOT_INSTALLED:
3767 delete_unexpected_flow(backer, key, key_len);
3772 dpif_flow_dump_done(&dump);
3774 update_moving_averages(backer);
3777 /* Calculates and returns the number of milliseconds of idle time after which
3778 * subfacets should expire from the datapath. When a subfacet expires, we fold
3779 * its statistics into its facet, and when a facet's last subfacet expires, we
3780 * fold its statistic into its rule. */
3782 subfacet_max_idle(const struct dpif_backer *backer)
3785 * Idle time histogram.
3787 * Most of the time a switch has a relatively small number of subfacets.
3788 * When this is the case we might as well keep statistics for all of them
3789 * in userspace and to cache them in the kernel datapath for performance as
3792 * As the number of subfacets increases, the memory required to maintain
3793 * statistics about them in userspace and in the kernel becomes
3794 * significant. However, with a large number of subfacets it is likely
3795 * that only a few of them are "heavy hitters" that consume a large amount
3796 * of bandwidth. At this point, only heavy hitters are worth caching in
3797 * the kernel and maintaining in userspaces; other subfacets we can
3800 * The technique used to compute the idle time is to build a histogram with
3801 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
3802 * that is installed in the kernel gets dropped in the appropriate bucket.
3803 * After the histogram has been built, we compute the cutoff so that only
3804 * the most-recently-used 1% of subfacets (but at least
3805 * flow_eviction_threshold flows) are kept cached. At least
3806 * the most-recently-used bucket of subfacets is kept, so actually an
3807 * arbitrary number of subfacets can be kept in any given expiration run
3808 * (though the next run will delete most of those unless they receive
3811 * This requires a second pass through the subfacets, in addition to the
3812 * pass made by update_stats(), because the former function never looks at
3813 * uninstallable subfacets.
3815 enum { BUCKET_WIDTH = 100 };
3816 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
3817 int buckets[N_BUCKETS] = { 0 };
3818 int total, subtotal, bucket;
3819 struct subfacet *subfacet;
3823 total = hmap_count(&backer->subfacets);
3824 if (total <= flow_eviction_threshold) {
3825 return N_BUCKETS * BUCKET_WIDTH;
3828 /* Build histogram. */
3830 HMAP_FOR_EACH (subfacet, hmap_node, &backer->subfacets) {
3831 long long int idle = now - subfacet->used;
3832 int bucket = (idle <= 0 ? 0
3833 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
3834 : (unsigned int) idle / BUCKET_WIDTH);
3838 /* Find the first bucket whose flows should be expired. */
3839 subtotal = bucket = 0;
3841 subtotal += buckets[bucket++];
3842 } while (bucket < N_BUCKETS &&
3843 subtotal < MAX(flow_eviction_threshold, total / 100));
3845 if (VLOG_IS_DBG_ENABLED()) {
3849 ds_put_cstr(&s, "keep");
3850 for (i = 0; i < N_BUCKETS; i++) {
3852 ds_put_cstr(&s, ", drop");
3855 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
3858 VLOG_INFO("%s (msec:count)", ds_cstr(&s));
3862 return bucket * BUCKET_WIDTH;
3866 expire_subfacets(struct dpif_backer *backer, int dp_max_idle)
3868 /* Cutoff time for most flows. */
3869 long long int normal_cutoff = time_msec() - dp_max_idle;
3871 /* We really want to keep flows for special protocols around, so use a more
3872 * conservative cutoff. */
3873 long long int special_cutoff = time_msec() - 10000;
3875 struct subfacet *subfacet, *next_subfacet;
3876 struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
3880 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
3881 &backer->subfacets) {
3882 long long int cutoff;
3884 cutoff = (subfacet->facet->xout.slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP
3888 if (subfacet->used < cutoff) {
3889 if (subfacet->path != SF_NOT_INSTALLED) {
3890 batch[n_batch++] = subfacet;
3891 if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
3892 subfacet_destroy_batch(backer, batch, n_batch);
3896 subfacet_destroy(subfacet);
3902 subfacet_destroy_batch(backer, batch, n_batch);
3906 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
3907 * then delete it entirely. */
3909 rule_expire(struct rule_dpif *rule)
3911 uint16_t idle_timeout, hard_timeout;
3915 ovs_assert(!rule->up.pending);
3917 ovs_mutex_lock(&rule->up.timeout_mutex);
3918 hard_timeout = rule->up.hard_timeout;
3919 idle_timeout = rule->up.idle_timeout;
3920 ovs_mutex_unlock(&rule->up.timeout_mutex);
3922 /* Has 'rule' expired? */
3924 if (hard_timeout && now > rule->up.modified + hard_timeout * 1000) {
3925 reason = OFPRR_HARD_TIMEOUT;
3926 } else if (idle_timeout && now > rule->up.used + idle_timeout * 1000) {
3927 reason = OFPRR_IDLE_TIMEOUT;
3932 COVERAGE_INC(ofproto_dpif_expired);
3933 ofproto_rule_expire(&rule->up, reason);
3938 /* Creates and returns a new facet based on 'miss'.
3940 * The caller must already have determined that no facet with an identical
3941 * 'miss->flow' exists in 'miss->ofproto'.
3943 * 'rule' and 'xout' must have been created based on 'miss'.
3945 * 'facet'' statistics are initialized based on 'stats'.
3947 * The facet will initially have no subfacets. The caller should create (at
3948 * least) one subfacet with subfacet_create(). */
3949 static struct facet *
3950 facet_create(const struct flow_miss *miss)
3952 struct ofproto_dpif *ofproto = miss->ofproto;
3953 struct facet *facet;
3956 facet = xzalloc(sizeof *facet);
3957 facet->ofproto = miss->ofproto;
3958 facet->used = miss->stats.used;
3959 facet->flow = miss->flow;
3960 facet->learn_rl = time_msec() + 500;
3962 list_init(&facet->subfacets);
3963 netflow_flow_init(&facet->nf_flow);
3964 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
3966 xlate_out_copy(&facet->xout, &miss->xout);
3968 match_init(&match, &facet->flow, &facet->xout.wc);
3969 cls_rule_init(&facet->cr, &match, OFP_DEFAULT_PRIORITY);
3970 ovs_rwlock_wrlock(&ofproto->facets.rwlock);
3971 classifier_insert(&ofproto->facets, &facet->cr);
3972 ovs_rwlock_unlock(&ofproto->facets.rwlock);
3974 facet->nf_flow.output_iface = facet->xout.nf_output_iface;
3979 facet_free(struct facet *facet)
3982 xlate_out_uninit(&facet->xout);
3987 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
3988 * 'packet', which arrived on 'in_port'. */
3990 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
3991 const struct nlattr *odp_actions, size_t actions_len,
3992 struct ofpbuf *packet)
3994 struct odputil_keybuf keybuf;
3998 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
3999 odp_flow_key_from_flow(&key, flow,
4000 ofp_port_to_odp_port(ofproto, flow->in_port.ofp_port));
4002 error = dpif_execute(ofproto->backer->dpif, key.data, key.size,
4003 odp_actions, actions_len, packet);
4007 /* Remove 'facet' from its ofproto and free up the associated memory:
4009 * - If 'facet' was installed in the datapath, uninstalls it and updates its
4010 * rule's statistics, via subfacet_uninstall().
4012 * - Removes 'facet' from its rule and from ofproto->facets.
4015 facet_remove(struct facet *facet)
4017 struct subfacet *subfacet, *next_subfacet;
4019 ovs_assert(!list_is_empty(&facet->subfacets));
4021 /* First uninstall all of the subfacets to get final statistics. */
4022 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4023 subfacet_uninstall(subfacet);
4026 /* Flush the final stats to the rule.
4028 * This might require us to have at least one subfacet around so that we
4029 * can use its actions for accounting in facet_account(), which is why we
4030 * have uninstalled but not yet destroyed the subfacets. */
4031 facet_flush_stats(facet);
4033 /* Now we're really all done so destroy everything. */
4034 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
4035 &facet->subfacets) {
4036 subfacet_destroy__(subfacet);
4038 ovs_rwlock_wrlock(&facet->ofproto->facets.rwlock);
4039 classifier_remove(&facet->ofproto->facets, &facet->cr);
4040 ovs_rwlock_unlock(&facet->ofproto->facets.rwlock);
4041 cls_rule_destroy(&facet->cr);
4045 /* Feed information from 'facet' back into the learning table to keep it in
4046 * sync with what is actually flowing through the datapath. */
4048 facet_learn(struct facet *facet)
4050 long long int now = time_msec();
4052 if (!facet->xout.has_fin_timeout && now < facet->learn_rl) {
4056 facet->learn_rl = now + 500;
4058 if (!facet->xout.has_learn
4059 && !facet->xout.has_normal
4060 && (!facet->xout.has_fin_timeout
4061 || !(facet->tcp_flags & (TCP_FIN | TCP_RST)))) {
4065 facet_push_stats(facet, true);
4069 facet_account(struct facet *facet)
4071 const struct nlattr *a;
4076 if (!facet->xout.has_normal || !facet->ofproto->has_bonded_bundles) {
4079 n_bytes = facet->byte_count - facet->accounted_bytes;
4081 /* This loop feeds byte counters to bond_account() for rebalancing to use
4082 * as a basis. We also need to track the actual VLAN on which the packet
4083 * is going to be sent to ensure that it matches the one passed to
4084 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
4087 * We use the actions from an arbitrary subfacet because they should all
4088 * be equally valid for our purpose. */
4089 vlan_tci = facet->flow.vlan_tci;
4090 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->xout.odp_actions.data,
4091 facet->xout.odp_actions.size) {
4092 const struct ovs_action_push_vlan *vlan;
4093 struct ofport_dpif *port;
4095 switch (nl_attr_type(a)) {
4096 case OVS_ACTION_ATTR_OUTPUT:
4097 port = get_odp_port(facet->ofproto, nl_attr_get_odp_port(a));
4098 if (port && port->bundle && port->bundle->bond) {
4099 bond_account(port->bundle->bond, &facet->flow,
4100 vlan_tci_to_vid(vlan_tci), n_bytes);
4104 case OVS_ACTION_ATTR_POP_VLAN:
4105 vlan_tci = htons(0);
4108 case OVS_ACTION_ATTR_PUSH_VLAN:
4109 vlan = nl_attr_get(a);
4110 vlan_tci = vlan->vlan_tci;
4116 /* Returns true if the only action for 'facet' is to send to the controller.
4117 * (We don't report NetFlow expiration messages for such facets because they
4118 * are just part of the control logic for the network, not real traffic). */
4120 facet_is_controller_flow(struct facet *facet)
4123 struct ofproto_dpif *ofproto = facet->ofproto;
4124 const struct ofpact *ofpacts;
4125 struct rule_dpif *rule;
4129 rule_dpif_lookup(ofproto, &facet->flow, NULL, &rule);
4130 ofpacts_len = rule->up.actions->ofpacts_len;
4131 ofpacts = rule->up.actions->ofpacts;
4132 is_controller = ofpacts_len > 0
4133 && ofpacts->type == OFPACT_CONTROLLER
4134 && ofpact_next(ofpacts) >= ofpact_end(ofpacts, ofpacts_len);
4135 rule_dpif_unref(rule);
4137 return is_controller;
4142 /* Folds all of 'facet''s statistics into its rule. Also updates the
4143 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
4144 * 'facet''s statistics in the datapath should have been zeroed and folded into
4145 * its packet and byte counts before this function is called. */
4147 facet_flush_stats(struct facet *facet)
4149 struct ofproto_dpif *ofproto = facet->ofproto;
4150 struct subfacet *subfacet;
4152 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4153 ovs_assert(!subfacet->dp_byte_count);
4154 ovs_assert(!subfacet->dp_packet_count);
4157 facet_push_stats(facet, false);
4158 if (facet->accounted_bytes < facet->byte_count) {
4159 facet_account(facet);
4160 facet->accounted_bytes = facet->byte_count;
4163 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
4164 struct ofexpired expired;
4165 expired.flow = facet->flow;
4166 expired.packet_count = facet->packet_count;
4167 expired.byte_count = facet->byte_count;
4168 expired.used = facet->used;
4169 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4172 /* Reset counters to prevent double counting if 'facet' ever gets
4174 facet_reset_counters(facet);
4176 netflow_flow_clear(&facet->nf_flow);
4177 facet->tcp_flags = 0;
4180 /* Searches 'ofproto''s table of facets for one which would be responsible for
4181 * 'flow'. Returns it if found, otherwise a null pointer.
4183 * The returned facet might need revalidation; use facet_lookup_valid()
4184 * instead if that is important. */
4185 static struct facet *
4186 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
4188 struct cls_rule *cr;
4190 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
4191 cr = classifier_lookup(&ofproto->facets, flow, NULL);
4192 ovs_rwlock_unlock(&ofproto->facets.rwlock);
4193 return cr ? CONTAINER_OF(cr, struct facet, cr) : NULL;
4196 /* Searches 'ofproto''s table of facets for one capable that covers
4197 * 'flow'. Returns it if found, otherwise a null pointer.
4199 * The returned facet is guaranteed to be valid. */
4200 static struct facet *
4201 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
4203 struct facet *facet;
4205 facet = facet_find(ofproto, flow);
4207 && ofproto->backer->need_revalidate
4208 && !facet_revalidate(facet)) {
4216 facet_check_consistency(struct facet *facet)
4218 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
4220 struct xlate_out xout;
4221 struct xlate_in xin;
4223 struct rule_dpif *rule;
4226 /* Check the datapath actions for consistency. */
4227 rule_dpif_lookup(facet->ofproto, &facet->flow, NULL, &rule);
4228 xlate_in_init(&xin, facet->ofproto, &facet->flow, rule, 0, NULL);
4229 xlate_actions(&xin, &xout);
4230 rule_dpif_unref(rule);
4232 ok = ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)
4233 && facet->xout.slow == xout.slow;
4234 if (!ok && !VLOG_DROP_WARN(&rl)) {
4235 struct ds s = DS_EMPTY_INITIALIZER;
4237 flow_format(&s, &facet->flow);
4238 ds_put_cstr(&s, ": inconsistency in facet");
4240 if (!ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)) {
4241 ds_put_cstr(&s, " (actions were: ");
4242 format_odp_actions(&s, facet->xout.odp_actions.data,
4243 facet->xout.odp_actions.size);
4244 ds_put_cstr(&s, ") (correct actions: ");
4245 format_odp_actions(&s, xout.odp_actions.data,
4246 xout.odp_actions.size);
4247 ds_put_char(&s, ')');
4250 if (facet->xout.slow != xout.slow) {
4251 ds_put_format(&s, " slow path incorrect. should be %d", xout.slow);
4256 xlate_out_uninit(&xout);
4261 /* Re-searches the classifier for 'facet':
4263 * - If the rule found is different from 'facet''s current rule, moves
4264 * 'facet' to the new rule and recompiles its actions.
4266 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
4267 * where it is and recompiles its actions anyway.
4269 * - If any of 'facet''s subfacets correspond to a new flow according to
4270 * xlate_receive(), 'facet' is removed.
4272 * Returns true if 'facet' is still valid. False if 'facet' was removed. */
4274 facet_revalidate(struct facet *facet)
4276 struct ofproto_dpif *ofproto = facet->ofproto;
4277 struct rule_dpif *new_rule;
4278 struct subfacet *subfacet;
4279 struct flow_wildcards wc;
4280 struct xlate_out xout;
4281 struct xlate_in xin;
4283 COVERAGE_INC(facet_revalidate);
4285 /* Check that child subfacets still correspond to this facet. Tunnel
4286 * configuration changes could cause a subfacet's OpenFlow in_port to
4288 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4289 struct ofproto_dpif *recv_ofproto;
4290 struct flow recv_flow;
4293 error = xlate_receive(ofproto->backer, NULL, subfacet->key,
4294 subfacet->key_len, &recv_flow, NULL,
4295 &recv_ofproto, NULL);
4297 || recv_ofproto != ofproto
4298 || facet != facet_find(ofproto, &recv_flow)) {
4299 facet_remove(facet);
4304 flow_wildcards_init_catchall(&wc);
4305 rule_dpif_lookup(ofproto, &facet->flow, &wc, &new_rule);
4307 /* Calculate new datapath actions.
4309 * We do not modify any 'facet' state yet, because we might need to, e.g.,
4310 * emit a NetFlow expiration and, if so, we need to have the old state
4311 * around to properly compose it. */
4312 xlate_in_init(&xin, ofproto, &facet->flow, new_rule, 0, NULL);
4313 xlate_actions(&xin, &xout);
4314 flow_wildcards_or(&xout.wc, &xout.wc, &wc);
4316 /* A facet's slow path reason should only change under dramatic
4317 * circumstances. Rather than try to update everything, it's simpler to
4318 * remove the facet and start over.
4320 * More importantly, if a facet's wildcards change, it will be relatively
4321 * difficult to figure out if its subfacets still belong to it, and if not
4322 * which facet they may belong to. Again, to avoid the complexity, we
4323 * simply give up instead. */
4324 if (facet->xout.slow != xout.slow
4325 || memcmp(&facet->xout.wc, &xout.wc, sizeof xout.wc)) {
4326 facet_remove(facet);
4327 xlate_out_uninit(&xout);
4328 rule_dpif_unref(new_rule);
4332 if (!ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)) {
4333 LIST_FOR_EACH(subfacet, list_node, &facet->subfacets) {
4334 if (subfacet->path == SF_FAST_PATH) {
4335 struct dpif_flow_stats stats;
4337 subfacet_install(subfacet, &xout.odp_actions, &stats);
4338 subfacet_update_stats(subfacet, &stats);
4342 facet_flush_stats(facet);
4344 ofpbuf_clear(&facet->xout.odp_actions);
4345 ofpbuf_put(&facet->xout.odp_actions, xout.odp_actions.data,
4346 xout.odp_actions.size);
4349 /* Update 'facet' now that we've taken care of all the old state. */
4350 facet->xout.slow = xout.slow;
4351 facet->xout.has_learn = xout.has_learn;
4352 facet->xout.has_normal = xout.has_normal;
4353 facet->xout.has_fin_timeout = xout.has_fin_timeout;
4354 facet->xout.nf_output_iface = xout.nf_output_iface;
4355 facet->xout.mirrors = xout.mirrors;
4356 facet->nf_flow.output_iface = facet->xout.nf_output_iface;
4357 facet->used = MAX(facet->used, new_rule->up.created);
4359 xlate_out_uninit(&xout);
4360 rule_dpif_unref(new_rule);
4365 facet_reset_counters(struct facet *facet)
4367 facet->packet_count = 0;
4368 facet->byte_count = 0;
4369 facet->prev_packet_count = 0;
4370 facet->prev_byte_count = 0;
4371 facet->accounted_bytes = 0;
4375 flow_push_stats(struct ofproto_dpif *ofproto, struct flow *flow,
4376 struct dpif_flow_stats *stats, bool may_learn)
4378 struct ofport_dpif *in_port;
4379 struct rule_dpif *rule;
4380 struct xlate_in xin;
4382 in_port = get_ofp_port(ofproto, flow->in_port.ofp_port);
4383 if (in_port && in_port->is_tunnel) {
4384 netdev_vport_inc_rx(in_port->up.netdev, stats);
4387 rule_dpif_lookup(ofproto, flow, NULL, &rule);
4388 rule_dpif_credit_stats(rule, stats);
4389 xlate_in_init(&xin, ofproto, flow, rule, stats->tcp_flags, NULL);
4390 xin.resubmit_stats = stats;
4391 xin.may_learn = may_learn;
4392 xlate_actions_for_side_effects(&xin);
4393 rule_dpif_unref(rule);
4397 facet_push_stats(struct facet *facet, bool may_learn)
4399 struct dpif_flow_stats stats;
4401 ovs_assert(facet->packet_count >= facet->prev_packet_count);
4402 ovs_assert(facet->byte_count >= facet->prev_byte_count);
4403 ovs_assert(facet->used >= facet->prev_used);
4405 stats.n_packets = facet->packet_count - facet->prev_packet_count;
4406 stats.n_bytes = facet->byte_count - facet->prev_byte_count;
4407 stats.used = facet->used;
4408 stats.tcp_flags = facet->tcp_flags;
4410 if (may_learn || stats.n_packets || facet->used > facet->prev_used) {
4411 facet->prev_packet_count = facet->packet_count;
4412 facet->prev_byte_count = facet->byte_count;
4413 facet->prev_used = facet->used;
4415 netflow_flow_update_time(facet->ofproto->netflow, &facet->nf_flow,
4417 netflow_flow_update_flags(&facet->nf_flow, facet->tcp_flags);
4418 mirror_update_stats(facet->ofproto->mbridge, facet->xout.mirrors,
4419 stats.n_packets, stats.n_bytes);
4420 flow_push_stats(facet->ofproto, &facet->flow, &stats, may_learn);
4425 push_all_stats__(bool run_fast)
4427 static long long int rl = LLONG_MIN;
4428 struct ofproto_dpif *ofproto;
4430 if (time_msec() < rl) {
4434 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4435 struct cls_cursor cursor;
4436 struct facet *facet;
4438 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
4439 cls_cursor_init(&cursor, &ofproto->facets, NULL);
4440 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
4441 facet_push_stats(facet, false);
4446 ovs_rwlock_unlock(&ofproto->facets.rwlock);
4449 rl = time_msec() + 100;
4453 push_all_stats(void)
4455 push_all_stats__(true);
4459 rule_dpif_credit_stats(struct rule_dpif *rule,
4460 const struct dpif_flow_stats *stats)
4462 ovs_mutex_lock(&rule->stats_mutex);
4463 rule->packet_count += stats->n_packets;
4464 rule->byte_count += stats->n_bytes;
4465 rule->up.used = MAX(rule->up.used, stats->used);
4466 ovs_mutex_unlock(&rule->stats_mutex);
4470 rule_dpif_fail_open(const struct rule_dpif *rule)
4472 return rule->up.cr.priority == FAIL_OPEN_PRIORITY;
4476 rule_dpif_get_flow_cookie(const struct rule_dpif *rule)
4478 return rule->up.flow_cookie;
4482 rule_dpif_reduce_timeouts(struct rule_dpif *rule, uint16_t idle_timeout,
4483 uint16_t hard_timeout)
4485 ofproto_rule_reduce_timeouts(&rule->up, idle_timeout, hard_timeout);
4488 /* Returns 'rule''s actions. The caller owns a reference on the returned
4489 * actions and must eventually release it (with rule_actions_unref()) to avoid
4491 struct rule_actions *
4492 rule_dpif_get_actions(const struct rule_dpif *rule)
4494 struct rule_actions *actions;
4496 ovs_rwlock_rdlock(&rule->up.rwlock);
4497 actions = rule->up.actions;
4498 rule_actions_ref(actions);
4499 ovs_rwlock_unlock(&rule->up.rwlock);
4506 static struct subfacet *
4507 subfacet_find(struct dpif_backer *backer, const struct nlattr *key,
4508 size_t key_len, uint32_t key_hash)
4510 struct subfacet *subfacet;
4512 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
4513 &backer->subfacets) {
4514 if (subfacet->key_len == key_len
4515 && !memcmp(key, subfacet->key, key_len)) {
4523 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
4524 * 'key_fitness', 'key', and 'key_len' members in 'miss'. Returns the
4525 * existing subfacet if there is one, otherwise creates and returns a
4527 static struct subfacet *
4528 subfacet_create(struct facet *facet, struct flow_miss *miss)
4530 struct dpif_backer *backer = miss->ofproto->backer;
4531 enum odp_key_fitness key_fitness = miss->key_fitness;
4532 const struct nlattr *key = miss->key;
4533 size_t key_len = miss->key_len;
4535 struct subfacet *subfacet;
4537 key_hash = odp_flow_key_hash(key, key_len);
4539 if (list_is_empty(&facet->subfacets)) {
4540 subfacet = &facet->one_subfacet;
4542 subfacet = subfacet_find(backer, key, key_len, key_hash);
4544 if (subfacet->facet == facet) {
4548 /* This shouldn't happen. */
4549 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
4550 subfacet_destroy(subfacet);
4553 subfacet = xmalloc(sizeof *subfacet);
4556 hmap_insert(&backer->subfacets, &subfacet->hmap_node, key_hash);
4557 list_push_back(&facet->subfacets, &subfacet->list_node);
4558 subfacet->facet = facet;
4559 subfacet->key_fitness = key_fitness;
4560 subfacet->key = xmemdup(key, key_len);
4561 subfacet->key_len = key_len;
4562 subfacet->used = miss->stats.used;
4563 subfacet->created = subfacet->used;
4564 subfacet->dp_packet_count = 0;
4565 subfacet->dp_byte_count = 0;
4566 subfacet->path = SF_NOT_INSTALLED;
4567 subfacet->backer = backer;
4569 backer->subfacet_add_count++;
4573 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
4574 * its facet within 'ofproto', and frees it. */
4576 subfacet_destroy__(struct subfacet *subfacet)
4578 struct facet *facet = subfacet->facet;
4579 struct ofproto_dpif *ofproto = facet->ofproto;
4581 /* Update ofproto stats before uninstall the subfacet. */
4582 ofproto->backer->subfacet_del_count++;
4584 subfacet_uninstall(subfacet);
4585 hmap_remove(&subfacet->backer->subfacets, &subfacet->hmap_node);
4586 list_remove(&subfacet->list_node);
4587 free(subfacet->key);
4588 if (subfacet != &facet->one_subfacet) {
4593 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
4594 * last remaining subfacet in its facet destroys the facet too. */
4596 subfacet_destroy(struct subfacet *subfacet)
4598 struct facet *facet = subfacet->facet;
4600 if (list_is_singleton(&facet->subfacets)) {
4601 /* facet_remove() needs at least one subfacet (it will remove it). */
4602 facet_remove(facet);
4604 subfacet_destroy__(subfacet);
4609 subfacet_destroy_batch(struct dpif_backer *backer,
4610 struct subfacet **subfacets, int n)
4612 struct dpif_op ops[SUBFACET_DESTROY_MAX_BATCH];
4613 struct dpif_op *opsp[SUBFACET_DESTROY_MAX_BATCH];
4614 struct dpif_flow_stats stats[SUBFACET_DESTROY_MAX_BATCH];
4617 for (i = 0; i < n; i++) {
4618 ops[i].type = DPIF_OP_FLOW_DEL;
4619 ops[i].u.flow_del.key = subfacets[i]->key;
4620 ops[i].u.flow_del.key_len = subfacets[i]->key_len;
4621 ops[i].u.flow_del.stats = &stats[i];
4625 dpif_operate(backer->dpif, opsp, n);
4626 for (i = 0; i < n; i++) {
4627 subfacet_reset_dp_stats(subfacets[i], &stats[i]);
4628 subfacets[i]->path = SF_NOT_INSTALLED;
4629 subfacet_destroy(subfacets[i]);
4634 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
4635 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
4636 * in the datapath will be zeroed and 'stats' will be updated with traffic new
4637 * since 'subfacet' was last updated.
4639 * Returns 0 if successful, otherwise a positive errno value. */
4641 subfacet_install(struct subfacet *subfacet, const struct ofpbuf *odp_actions,
4642 struct dpif_flow_stats *stats)
4644 struct facet *facet = subfacet->facet;
4645 enum subfacet_path path = facet->xout.slow ? SF_SLOW_PATH : SF_FAST_PATH;
4646 const struct nlattr *actions = odp_actions->data;
4647 size_t actions_len = odp_actions->size;
4648 struct odputil_keybuf maskbuf;
4651 uint64_t slow_path_stub[128 / 8];
4652 enum dpif_flow_put_flags flags;
4655 flags = subfacet->path == SF_NOT_INSTALLED ? DPIF_FP_CREATE
4658 flags |= DPIF_FP_ZERO_STATS;
4661 if (path == SF_SLOW_PATH) {
4662 compose_slow_path(facet->ofproto, &facet->flow, facet->xout.slow,
4663 slow_path_stub, sizeof slow_path_stub,
4664 &actions, &actions_len);
4667 ofpbuf_use_stack(&mask, &maskbuf, sizeof maskbuf);
4668 if (enable_megaflows) {
4669 odp_flow_key_from_mask(&mask, &facet->xout.wc.masks,
4670 &facet->flow, UINT32_MAX);
4673 ret = dpif_flow_put(subfacet->backer->dpif, flags, subfacet->key,
4674 subfacet->key_len, mask.data, mask.size,
4675 actions, actions_len, stats);
4678 subfacet_reset_dp_stats(subfacet, stats);
4682 COVERAGE_INC(subfacet_install_fail);
4684 subfacet->path = path;
4689 /* If 'subfacet' is installed in the datapath, uninstalls it. */
4691 subfacet_uninstall(struct subfacet *subfacet)
4693 if (subfacet->path != SF_NOT_INSTALLED) {
4694 struct ofproto_dpif *ofproto = subfacet->facet->ofproto;
4695 struct dpif_flow_stats stats;
4698 error = dpif_flow_del(ofproto->backer->dpif, subfacet->key,
4699 subfacet->key_len, &stats);
4700 subfacet_reset_dp_stats(subfacet, &stats);
4702 subfacet_update_stats(subfacet, &stats);
4704 subfacet->path = SF_NOT_INSTALLED;
4706 ovs_assert(subfacet->dp_packet_count == 0);
4707 ovs_assert(subfacet->dp_byte_count == 0);
4711 /* Resets 'subfacet''s datapath statistics counters. This should be called
4712 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
4713 * non-null, it should contain the statistics returned by dpif when 'subfacet'
4714 * was reset in the datapath. 'stats' will be modified to include only
4715 * statistics new since 'subfacet' was last updated. */
4717 subfacet_reset_dp_stats(struct subfacet *subfacet,
4718 struct dpif_flow_stats *stats)
4721 && subfacet->dp_packet_count <= stats->n_packets
4722 && subfacet->dp_byte_count <= stats->n_bytes) {
4723 stats->n_packets -= subfacet->dp_packet_count;
4724 stats->n_bytes -= subfacet->dp_byte_count;
4727 subfacet->dp_packet_count = 0;
4728 subfacet->dp_byte_count = 0;
4731 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
4733 * Because of the meaning of a subfacet's counters, it only makes sense to do
4734 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
4735 * represents a packet that was sent by hand or if it represents statistics
4736 * that have been cleared out of the datapath. */
4738 subfacet_update_stats(struct subfacet *subfacet,
4739 const struct dpif_flow_stats *stats)
4741 if (stats->n_packets || stats->used > subfacet->used) {
4742 struct facet *facet = subfacet->facet;
4744 subfacet->used = MAX(subfacet->used, stats->used);
4745 facet->used = MAX(facet->used, stats->used);
4746 facet->packet_count += stats->n_packets;
4747 facet->byte_count += stats->n_bytes;
4748 facet->tcp_flags |= stats->tcp_flags;
4754 /* Lookup 'flow' in 'ofproto''s classifier. If 'wc' is non-null, sets
4755 * the fields that were relevant as part of the lookup. */
4757 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
4758 struct flow_wildcards *wc, struct rule_dpif **rule)
4760 struct ofport_dpif *port;
4762 if (rule_dpif_lookup_in_table(ofproto, flow, wc, 0, rule)) {
4765 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
4767 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
4768 flow->in_port.ofp_port);
4771 choose_miss_rule(port ? port->up.pp.config : 0, ofproto->miss_rule,
4772 ofproto->no_packet_in_rule, rule);
4776 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto,
4777 const struct flow *flow, struct flow_wildcards *wc,
4778 uint8_t table_id, struct rule_dpif **rule)
4780 struct cls_rule *cls_rule;
4781 struct classifier *cls;
4785 if (table_id >= N_TABLES) {
4790 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
4791 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
4794 cls = &ofproto->up.tables[table_id].cls;
4795 ovs_rwlock_rdlock(&cls->rwlock);
4796 frag = (flow->nw_frag & FLOW_NW_FRAG_ANY) != 0;
4797 if (frag && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
4798 /* We must pretend that transport ports are unavailable. */
4799 struct flow ofpc_normal_flow = *flow;
4800 ofpc_normal_flow.tp_src = htons(0);
4801 ofpc_normal_flow.tp_dst = htons(0);
4802 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
4803 } else if (frag && ofproto->up.frag_handling == OFPC_FRAG_DROP) {
4804 cls_rule = &ofproto->drop_frags_rule->up.cr;
4806 flow_wildcards_init_exact(wc);
4809 cls_rule = classifier_lookup(cls, flow, wc);
4812 *rule = rule_dpif_cast(rule_from_cls_rule(cls_rule));
4813 rule_dpif_ref(*rule);
4814 ovs_rwlock_unlock(&cls->rwlock);
4816 return *rule != NULL;
4819 /* Given a port configuration (specified as zero if there's no port), chooses
4820 * which of 'miss_rule' and 'no_packet_in_rule' should be used in case of a
4821 * flow table miss. */
4823 choose_miss_rule(enum ofputil_port_config config, struct rule_dpif *miss_rule,
4824 struct rule_dpif *no_packet_in_rule, struct rule_dpif **rule)
4826 *rule = config & OFPUTIL_PC_NO_PACKET_IN ? no_packet_in_rule : miss_rule;
4827 rule_dpif_ref(*rule);
4831 rule_dpif_ref(struct rule_dpif *rule)
4834 ofproto_rule_ref(&rule->up);
4839 rule_dpif_unref(struct rule_dpif *rule)
4842 ofproto_rule_unref(&rule->up);
4847 complete_operation(struct rule_dpif *rule)
4849 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4851 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
4852 ofoperation_complete(rule->up.pending, 0);
4855 static struct rule_dpif *rule_dpif_cast(const struct rule *rule)
4857 return rule ? CONTAINER_OF(rule, struct rule_dpif, up) : NULL;
4860 static struct rule *
4863 struct rule_dpif *rule = xmalloc(sizeof *rule);
4868 rule_dealloc(struct rule *rule_)
4870 struct rule_dpif *rule = rule_dpif_cast(rule_);
4875 rule_construct(struct rule *rule_)
4877 struct rule_dpif *rule = rule_dpif_cast(rule_);
4878 ovs_mutex_init(&rule->stats_mutex);
4879 ovs_mutex_lock(&rule->stats_mutex);
4880 rule->packet_count = 0;
4881 rule->byte_count = 0;
4882 ovs_mutex_unlock(&rule->stats_mutex);
4887 rule_insert(struct rule *rule_)
4889 struct rule_dpif *rule = rule_dpif_cast(rule_);
4890 complete_operation(rule);
4894 rule_delete(struct rule *rule_)
4896 struct rule_dpif *rule = rule_dpif_cast(rule_);
4897 complete_operation(rule);
4901 rule_destruct(struct rule *rule_)
4903 struct rule_dpif *rule = rule_dpif_cast(rule_);
4904 ovs_mutex_destroy(&rule->stats_mutex);
4908 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
4910 struct rule_dpif *rule = rule_dpif_cast(rule_);
4912 /* push_all_stats() can handle flow misses which, when using the learn
4913 * action, can cause rules to be added and deleted. This can corrupt our
4914 * caller's datastructures which assume that rule_get_stats() doesn't have
4915 * an impact on the flow table. To be safe, we disable miss handling. */
4916 push_all_stats__(false);
4918 /* Start from historical data for 'rule' itself that are no longer tracked
4919 * in facets. This counts, for example, facets that have expired. */
4920 ovs_mutex_lock(&rule->stats_mutex);
4921 *packets = rule->packet_count;
4922 *bytes = rule->byte_count;
4923 ovs_mutex_unlock(&rule->stats_mutex);
4927 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
4928 struct ofpbuf *packet)
4930 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
4931 struct dpif_flow_stats stats;
4932 struct xlate_out xout;
4933 struct xlate_in xin;
4935 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
4936 rule_dpif_credit_stats(rule, &stats);
4938 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
4939 xin.resubmit_stats = &stats;
4940 xlate_actions(&xin, &xout);
4942 execute_odp_actions(ofproto, flow, xout.odp_actions.data,
4943 xout.odp_actions.size, packet);
4945 xlate_out_uninit(&xout);
4949 rule_execute(struct rule *rule, const struct flow *flow,
4950 struct ofpbuf *packet)
4952 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
4953 ofpbuf_delete(packet);
4958 rule_modify_actions(struct rule *rule_, bool reset_counters)
4960 struct rule_dpif *rule = rule_dpif_cast(rule_);
4962 if (reset_counters) {
4963 ovs_mutex_lock(&rule->stats_mutex);
4964 rule->packet_count = 0;
4965 rule->byte_count = 0;
4966 ovs_mutex_unlock(&rule->stats_mutex);
4969 complete_operation(rule);
4972 /* Sends 'packet' out 'ofport'.
4973 * May modify 'packet'.
4974 * Returns 0 if successful, otherwise a positive errno value. */
4976 send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
4978 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
4979 uint64_t odp_actions_stub[1024 / 8];
4980 struct ofpbuf key, odp_actions;
4981 struct dpif_flow_stats stats;
4982 struct odputil_keybuf keybuf;
4983 struct ofpact_output output;
4984 struct xlate_out xout;
4985 struct xlate_in xin;
4987 union flow_in_port in_port_;
4990 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
4991 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4993 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
4994 in_port_.ofp_port = OFPP_NONE;
4995 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
4996 odp_flow_key_from_flow(&key, &flow, ofp_port_to_odp_port(ofproto,
4998 dpif_flow_stats_extract(&flow, packet, time_msec(), &stats);
5000 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5001 output.port = ofport->up.ofp_port;
5004 xlate_in_init(&xin, ofproto, &flow, NULL, 0, packet);
5005 xin.ofpacts_len = sizeof output;
5006 xin.ofpacts = &output.ofpact;
5007 xin.resubmit_stats = &stats;
5008 xlate_actions(&xin, &xout);
5010 error = dpif_execute(ofproto->backer->dpif,
5012 xout.odp_actions.data, xout.odp_actions.size,
5014 xlate_out_uninit(&xout);
5017 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %s (%s)",
5018 ofproto->up.name, netdev_get_name(ofport->up.netdev),
5019 ovs_strerror(error));
5022 ofproto->stats.tx_packets++;
5023 ofproto->stats.tx_bytes += packet->size;
5027 /* Composes an ODP action for a "slow path" action for 'flow' within 'ofproto'.
5028 * The action will state 'slow' as the reason that the action is in the slow
5029 * path. (This is purely informational: it allows a human viewing "ovs-dpctl
5030 * dump-flows" output to see why a flow is in the slow path.)
5032 * The 'stub_size' bytes in 'stub' will be used to store the action.
5033 * 'stub_size' must be large enough for the action.
5035 * The action and its size will be stored in '*actionsp' and '*actions_lenp',
5038 compose_slow_path(const struct ofproto_dpif *ofproto, const struct flow *flow,
5039 enum slow_path_reason slow,
5040 uint64_t *stub, size_t stub_size,
5041 const struct nlattr **actionsp, size_t *actions_lenp)
5043 union user_action_cookie cookie;
5046 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
5047 cookie.slow_path.unused = 0;
5048 cookie.slow_path.reason = slow;
5050 ofpbuf_use_stack(&buf, stub, stub_size);
5051 if (slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)) {
5052 uint32_t pid = dpif_port_get_pid(ofproto->backer->dpif,
5054 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, &buf);
5056 odp_port_t odp_port;
5059 odp_port = ofp_port_to_odp_port(ofproto, flow->in_port.ofp_port);
5060 pid = dpif_port_get_pid(ofproto->backer->dpif, odp_port);
5061 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, &buf);
5063 *actionsp = buf.data;
5064 *actions_lenp = buf.size;
5068 set_frag_handling(struct ofproto *ofproto_,
5069 enum ofp_config_flags frag_handling)
5071 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5072 if (frag_handling != OFPC_FRAG_REASM) {
5073 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5081 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5082 const struct flow *flow,
5083 const struct ofpact *ofpacts, size_t ofpacts_len)
5085 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5086 struct odputil_keybuf keybuf;
5087 struct dpif_flow_stats stats;
5088 struct xlate_out xout;
5089 struct xlate_in xin;
5093 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5094 odp_flow_key_from_flow(&key, flow,
5095 ofp_port_to_odp_port(ofproto,
5096 flow->in_port.ofp_port));
5098 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
5100 xlate_in_init(&xin, ofproto, flow, NULL, stats.tcp_flags, packet);
5101 xin.resubmit_stats = &stats;
5102 xin.ofpacts_len = ofpacts_len;
5103 xin.ofpacts = ofpacts;
5105 xlate_actions(&xin, &xout);
5106 dpif_execute(ofproto->backer->dpif, key.data, key.size,
5107 xout.odp_actions.data, xout.odp_actions.size, packet);
5108 xlate_out_uninit(&xout);
5116 set_netflow(struct ofproto *ofproto_,
5117 const struct netflow_options *netflow_options)
5119 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5121 if (netflow_options) {
5122 if (!ofproto->netflow) {
5123 ofproto->netflow = netflow_create();
5124 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5126 return netflow_set_options(ofproto->netflow, netflow_options);
5127 } else if (ofproto->netflow) {
5128 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5129 netflow_destroy(ofproto->netflow);
5130 ofproto->netflow = NULL;
5137 get_netflow_ids(const struct ofproto *ofproto_,
5138 uint8_t *engine_type, uint8_t *engine_id)
5140 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5142 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
5146 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
5148 if (!facet_is_controller_flow(facet) &&
5149 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
5150 struct subfacet *subfacet;
5151 struct ofexpired expired;
5153 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
5154 if (subfacet->path == SF_FAST_PATH) {
5155 struct dpif_flow_stats stats;
5157 subfacet_install(subfacet, &facet->xout.odp_actions,
5159 subfacet_update_stats(subfacet, &stats);
5163 expired.flow = facet->flow;
5164 expired.packet_count = facet->packet_count;
5165 expired.byte_count = facet->byte_count;
5166 expired.used = facet->used;
5167 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5172 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
5174 struct cls_cursor cursor;
5175 struct facet *facet;
5177 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
5178 cls_cursor_init(&cursor, &ofproto->facets, NULL);
5179 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
5180 send_active_timeout(ofproto, facet);
5182 ovs_rwlock_unlock(&ofproto->facets.rwlock);
5185 static struct ofproto_dpif *
5186 ofproto_dpif_lookup(const char *name)
5188 struct ofproto_dpif *ofproto;
5190 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
5191 hash_string(name, 0), &all_ofproto_dpifs) {
5192 if (!strcmp(ofproto->up.name, name)) {
5200 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
5201 const char *argv[], void *aux OVS_UNUSED)
5203 struct ofproto_dpif *ofproto;
5206 ofproto = ofproto_dpif_lookup(argv[1]);
5208 unixctl_command_reply_error(conn, "no such bridge");
5211 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
5212 mac_learning_flush(ofproto->ml);
5213 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5215 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5216 ovs_rwlock_wrlock(&ofproto->ml->rwlock);
5217 mac_learning_flush(ofproto->ml);
5218 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5222 unixctl_command_reply(conn, "table successfully flushed");
5225 static struct ofport_dpif *
5226 ofbundle_get_a_port(const struct ofbundle *bundle)
5228 return CONTAINER_OF(list_front(&bundle->ports), struct ofport_dpif,
5233 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5234 const char *argv[], void *aux OVS_UNUSED)
5236 struct ds ds = DS_EMPTY_INITIALIZER;
5237 const struct ofproto_dpif *ofproto;
5238 const struct mac_entry *e;
5240 ofproto = ofproto_dpif_lookup(argv[1]);
5242 unixctl_command_reply_error(conn, "no such bridge");
5246 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5247 ovs_rwlock_rdlock(&ofproto->ml->rwlock);
5248 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5249 struct ofbundle *bundle = e->port.p;
5250 char name[OFP_MAX_PORT_NAME_LEN];
5252 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
5254 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
5255 name, e->vlan, ETH_ADDR_ARGS(e->mac),
5256 mac_entry_age(ofproto->ml, e));
5258 ovs_rwlock_unlock(&ofproto->ml->rwlock);
5259 unixctl_command_reply(conn, ds_cstr(&ds));
5264 struct xlate_out xout;
5265 struct xlate_in xin;
5271 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
5273 ds_put_char_multiple(result, '\t', level);
5275 ds_put_cstr(result, "No match\n");
5279 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5280 rule ? rule->up.table_id : 0, ntohll(rule->up.flow_cookie));
5281 cls_rule_format(&rule->up.cr, result);
5282 ds_put_char(result, '\n');
5284 ds_put_char_multiple(result, '\t', level);
5285 ds_put_cstr(result, "OpenFlow ");
5286 ofpacts_format(rule->up.actions->ofpacts, rule->up.actions->ofpacts_len,
5288 ds_put_char(result, '\n');
5292 trace_format_flow(struct ds *result, int level, const char *title,
5293 struct trace_ctx *trace)
5295 ds_put_char_multiple(result, '\t', level);
5296 ds_put_format(result, "%s: ", title);
5297 if (flow_equal(&trace->xin.flow, &trace->flow)) {
5298 ds_put_cstr(result, "unchanged");
5300 flow_format(result, &trace->xin.flow);
5301 trace->flow = trace->xin.flow;
5303 ds_put_char(result, '\n');
5307 trace_format_regs(struct ds *result, int level, const char *title,
5308 struct trace_ctx *trace)
5312 ds_put_char_multiple(result, '\t', level);
5313 ds_put_format(result, "%s:", title);
5314 for (i = 0; i < FLOW_N_REGS; i++) {
5315 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5317 ds_put_char(result, '\n');
5321 trace_format_odp(struct ds *result, int level, const char *title,
5322 struct trace_ctx *trace)
5324 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
5326 ds_put_char_multiple(result, '\t', level);
5327 ds_put_format(result, "%s: ", title);
5328 format_odp_actions(result, odp_actions->data, odp_actions->size);
5329 ds_put_char(result, '\n');
5333 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
5335 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
5336 struct ds *result = trace->result;
5338 ds_put_char(result, '\n');
5339 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
5340 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
5341 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
5342 trace_format_rule(result, recurse + 1, rule);
5346 trace_report(struct xlate_in *xin, const char *s, int recurse)
5348 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
5349 struct ds *result = trace->result;
5351 ds_put_char_multiple(result, '\t', recurse);
5352 ds_put_cstr(result, s);
5353 ds_put_char(result, '\n');
5357 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
5358 void *aux OVS_UNUSED)
5360 const struct dpif_backer *backer;
5361 struct ofproto_dpif *ofproto;
5362 struct ofpbuf odp_key, odp_mask;
5363 struct ofpbuf *packet;
5371 ofpbuf_init(&odp_key, 0);
5372 ofpbuf_init(&odp_mask, 0);
5374 /* Handle "-generate" or a hex string as the last argument. */
5375 if (!strcmp(argv[argc - 1], "-generate")) {
5376 packet = ofpbuf_new(0);
5379 const char *error = eth_from_hex(argv[argc - 1], &packet);
5382 } else if (argc == 4) {
5383 /* The 3-argument form must end in "-generate' or a hex string. */
5384 unixctl_command_reply_error(conn, error);
5389 /* Parse the flow and determine whether a datapath or
5390 * bridge is specified. If function odp_flow_key_from_string()
5391 * returns 0, the flow is a odp_flow. If function
5392 * parse_ofp_exact_flow() returns 0, the flow is a br_flow. */
5393 if (!odp_flow_from_string(argv[argc - 1], NULL, &odp_key, &odp_mask)) {
5394 /* If the odp_flow is the second argument,
5395 * the datapath name is the first argument. */
5397 const char *dp_type;
5398 if (!strncmp(argv[1], "ovs-", 4)) {
5399 dp_type = argv[1] + 4;
5403 backer = shash_find_data(&all_dpif_backers, dp_type);
5405 unixctl_command_reply_error(conn, "Cannot find datapath "
5410 /* No datapath name specified, so there should be only one
5412 struct shash_node *node;
5413 if (shash_count(&all_dpif_backers) != 1) {
5414 unixctl_command_reply_error(conn, "Must specify datapath "
5415 "name, there is more than one type of datapath");
5418 node = shash_first(&all_dpif_backers);
5419 backer = node->data;
5422 if (xlate_receive(backer, NULL, odp_key.data, odp_key.size, &flow,
5423 NULL, &ofproto, NULL)) {
5424 unixctl_command_reply_error(conn, "Invalid datapath flow");
5427 ds_put_format(&result, "Bridge: %s\n", ofproto->up.name);
5428 } else if (!parse_ofp_exact_flow(&flow, argv[argc - 1])) {
5430 unixctl_command_reply_error(conn, "Must specify bridge name");
5434 ofproto = ofproto_dpif_lookup(argv[1]);
5436 unixctl_command_reply_error(conn, "Unknown bridge name");
5440 unixctl_command_reply_error(conn, "Bad flow syntax");
5444 /* Generate a packet, if requested. */
5446 if (!packet->size) {
5447 flow_compose(packet, &flow);
5449 union flow_in_port in_port_;
5451 in_port_ = flow.in_port;
5452 ds_put_cstr(&result, "Packet: ");
5453 s = ofp_packet_to_string(packet->data, packet->size);
5454 ds_put_cstr(&result, s);
5457 /* Use the metadata from the flow and the packet argument
5458 * to reconstruct the flow. */
5459 flow_extract(packet, flow.skb_priority, flow.pkt_mark, NULL,
5464 ofproto_trace(ofproto, &flow, packet, &result);
5465 unixctl_command_reply(conn, ds_cstr(&result));
5468 ds_destroy(&result);
5469 ofpbuf_delete(packet);
5470 ofpbuf_uninit(&odp_key);
5471 ofpbuf_uninit(&odp_mask);
5475 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
5476 const struct ofpbuf *packet, struct ds *ds)
5478 struct rule_dpif *rule;
5479 struct flow_wildcards wc;
5481 ds_put_cstr(ds, "Flow: ");
5482 flow_format(ds, flow);
5483 ds_put_char(ds, '\n');
5485 flow_wildcards_init_catchall(&wc);
5486 rule_dpif_lookup(ofproto, flow, &wc, &rule);
5488 trace_format_rule(ds, 0, rule);
5489 if (rule == ofproto->miss_rule) {
5490 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
5491 } else if (rule == ofproto->no_packet_in_rule) {
5492 ds_put_cstr(ds, "\nNo match, packets dropped because "
5493 "OFPPC_NO_PACKET_IN is set on in_port.\n");
5494 } else if (rule == ofproto->drop_frags_rule) {
5495 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
5496 "and the fragment handling mode is \"drop\".\n");
5500 uint64_t odp_actions_stub[1024 / 8];
5501 struct ofpbuf odp_actions;
5502 struct trace_ctx trace;
5506 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
5509 ofpbuf_use_stub(&odp_actions,
5510 odp_actions_stub, sizeof odp_actions_stub);
5511 xlate_in_init(&trace.xin, ofproto, flow, rule, tcp_flags, packet);
5512 trace.xin.resubmit_hook = trace_resubmit;
5513 trace.xin.report_hook = trace_report;
5515 xlate_actions(&trace.xin, &trace.xout);
5516 flow_wildcards_or(&trace.xout.wc, &trace.xout.wc, &wc);
5518 ds_put_char(ds, '\n');
5519 trace_format_flow(ds, 0, "Final flow", &trace);
5521 match_init(&match, flow, &trace.xout.wc);
5522 ds_put_cstr(ds, "Relevant fields: ");
5523 match_format(&match, ds, OFP_DEFAULT_PRIORITY);
5524 ds_put_char(ds, '\n');
5526 ds_put_cstr(ds, "Datapath actions: ");
5527 format_odp_actions(ds, trace.xout.odp_actions.data,
5528 trace.xout.odp_actions.size);
5530 if (trace.xout.slow) {
5531 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
5532 "slow path because it:");
5533 switch (trace.xout.slow) {
5535 ds_put_cstr(ds, "\n\t- Consists of CFM packets.");
5538 ds_put_cstr(ds, "\n\t- Consists of LACP packets.");
5541 ds_put_cstr(ds, "\n\t- Consists of STP packets.");
5544 ds_put_cstr(ds, "\n\t- Consists of BFD packets.");
5546 case SLOW_CONTROLLER:
5547 ds_put_cstr(ds, "\n\t- Sends \"packet-in\" messages "
5548 "to the OpenFlow controller.");
5555 xlate_out_uninit(&trace.xout);
5558 rule_dpif_unref(rule);
5561 /* Runs a self-check of flow translations in 'ofproto'. Appends a message to
5562 * 'reply' describing the results. */
5564 ofproto_dpif_self_check__(struct ofproto_dpif *ofproto, struct ds *reply)
5566 struct cls_cursor cursor;
5567 struct facet *facet;
5571 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
5572 cls_cursor_init(&cursor, &ofproto->facets, NULL);
5573 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
5574 if (!facet_check_consistency(facet)) {
5578 ovs_rwlock_unlock(&ofproto->facets.rwlock);
5580 ofproto->backer->need_revalidate = REV_INCONSISTENCY;
5584 ds_put_format(reply, "%s: self-check failed (%d errors)\n",
5585 ofproto->up.name, errors);
5587 ds_put_format(reply, "%s: self-check passed\n", ofproto->up.name);
5592 ofproto_dpif_self_check(struct unixctl_conn *conn,
5593 int argc, const char *argv[], void *aux OVS_UNUSED)
5595 struct ds reply = DS_EMPTY_INITIALIZER;
5596 struct ofproto_dpif *ofproto;
5599 ofproto = ofproto_dpif_lookup(argv[1]);
5601 unixctl_command_reply_error(conn, "Unknown ofproto (use "
5602 "ofproto/list for help)");
5605 ofproto_dpif_self_check__(ofproto, &reply);
5607 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5608 ofproto_dpif_self_check__(ofproto, &reply);
5612 unixctl_command_reply(conn, ds_cstr(&reply));
5616 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
5617 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
5618 * to destroy 'ofproto_shash' and free the returned value. */
5619 static const struct shash_node **
5620 get_ofprotos(struct shash *ofproto_shash)
5622 const struct ofproto_dpif *ofproto;
5624 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5625 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
5626 shash_add_nocopy(ofproto_shash, name, ofproto);
5629 return shash_sort(ofproto_shash);
5633 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
5634 const char *argv[] OVS_UNUSED,
5635 void *aux OVS_UNUSED)
5637 struct ds ds = DS_EMPTY_INITIALIZER;
5638 struct shash ofproto_shash;
5639 const struct shash_node **sorted_ofprotos;
5642 shash_init(&ofproto_shash);
5643 sorted_ofprotos = get_ofprotos(&ofproto_shash);
5644 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5645 const struct shash_node *node = sorted_ofprotos[i];
5646 ds_put_format(&ds, "%s\n", node->name);
5649 shash_destroy(&ofproto_shash);
5650 free(sorted_ofprotos);
5652 unixctl_command_reply(conn, ds_cstr(&ds));
5657 show_dp_rates(struct ds *ds, const char *heading,
5658 const struct avg_subfacet_rates *rates)
5660 ds_put_format(ds, "%s add rate: %5.3f/min, del rate: %5.3f/min\n",
5661 heading, rates->add_rate, rates->del_rate);
5665 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
5667 const struct shash_node **ofprotos;
5668 struct ofproto_dpif *ofproto;
5669 struct shash ofproto_shash;
5670 uint64_t n_hit, n_missed;
5671 long long int minutes;
5674 n_hit = n_missed = 0;
5675 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5676 if (ofproto->backer == backer) {
5677 n_missed += ofproto->n_missed;
5678 n_hit += ofproto->n_hit;
5682 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5683 dpif_name(backer->dpif), n_hit, n_missed);
5684 ds_put_format(ds, "\tflows: cur: %zu, avg: %u, max: %u,"
5685 " life span: %lldms\n", hmap_count(&backer->subfacets),
5686 backer->avg_n_subfacet, backer->max_n_subfacet,
5687 backer->avg_subfacet_life);
5689 minutes = (time_msec() - backer->created) / (1000 * 60);
5690 if (minutes >= 60) {
5691 show_dp_rates(ds, "\thourly avg:", &backer->hourly);
5693 if (minutes >= 60 * 24) {
5694 show_dp_rates(ds, "\tdaily avg:", &backer->daily);
5696 show_dp_rates(ds, "\toverall avg:", &backer->lifetime);
5698 shash_init(&ofproto_shash);
5699 ofprotos = get_ofprotos(&ofproto_shash);
5700 for (i = 0; i < shash_count(&ofproto_shash); i++) {
5701 struct ofproto_dpif *ofproto = ofprotos[i]->data;
5702 const struct shash_node **ports;
5705 if (ofproto->backer != backer) {
5709 ds_put_format(ds, "\t%s: hit:%"PRIu64" missed:%"PRIu64"\n",
5710 ofproto->up.name, ofproto->n_hit, ofproto->n_missed);
5712 ports = shash_sort(&ofproto->up.port_by_name);
5713 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
5714 const struct shash_node *node = ports[j];
5715 struct ofport *ofport = node->data;
5717 odp_port_t odp_port;
5719 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
5722 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
5723 if (odp_port != ODPP_NONE) {
5724 ds_put_format(ds, "%"PRIu32":", odp_port);
5726 ds_put_cstr(ds, "none:");
5729 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
5732 if (!netdev_get_config(ofport->netdev, &config)) {
5733 const struct smap_node **nodes;
5736 nodes = smap_sort(&config);
5737 for (i = 0; i < smap_count(&config); i++) {
5738 const struct smap_node *node = nodes[i];
5739 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
5740 node->key, node->value);
5744 smap_destroy(&config);
5746 ds_put_char(ds, ')');
5747 ds_put_char(ds, '\n');
5751 shash_destroy(&ofproto_shash);
5756 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5757 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
5759 struct ds ds = DS_EMPTY_INITIALIZER;
5760 const struct shash_node **backers;
5763 backers = shash_sort(&all_dpif_backers);
5764 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
5765 dpif_show_backer(backers[i]->data, &ds);
5769 unixctl_command_reply(conn, ds_cstr(&ds));
5773 /* Dump the megaflow (facet) cache. This is useful to check the
5774 * correctness of flow wildcarding, since the same mechanism is used for
5775 * both xlate caching and kernel wildcarding.
5777 * It's important to note that in the output the flow description uses
5778 * OpenFlow (OFP) ports, but the actions use datapath (ODP) ports.
5780 * This command is only needed for advanced debugging, so it's not
5781 * documented in the man page. */
5783 ofproto_unixctl_dpif_dump_megaflows(struct unixctl_conn *conn,
5784 int argc OVS_UNUSED, const char *argv[],
5785 void *aux OVS_UNUSED)
5787 struct ds ds = DS_EMPTY_INITIALIZER;
5788 const struct ofproto_dpif *ofproto;
5789 long long int now = time_msec();
5790 struct cls_cursor cursor;
5791 struct facet *facet;
5793 ofproto = ofproto_dpif_lookup(argv[1]);
5795 unixctl_command_reply_error(conn, "no such bridge");
5799 ovs_rwlock_rdlock(&ofproto->facets.rwlock);
5800 cls_cursor_init(&cursor, &ofproto->facets, NULL);
5801 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
5802 cls_rule_format(&facet->cr, &ds);
5803 ds_put_cstr(&ds, ", ");
5804 ds_put_format(&ds, "n_subfacets:%zu, ", list_size(&facet->subfacets));
5805 ds_put_format(&ds, "used:%.3fs, ", (now - facet->used) / 1000.0);
5806 ds_put_cstr(&ds, "Datapath actions: ");
5807 if (facet->xout.slow) {
5808 uint64_t slow_path_stub[128 / 8];
5809 const struct nlattr *actions;
5812 compose_slow_path(ofproto, &facet->flow, facet->xout.slow,
5813 slow_path_stub, sizeof slow_path_stub,
5814 &actions, &actions_len);
5815 format_odp_actions(&ds, actions, actions_len);
5817 format_odp_actions(&ds, facet->xout.odp_actions.data,
5818 facet->xout.odp_actions.size);
5820 ds_put_cstr(&ds, "\n");
5822 ovs_rwlock_unlock(&ofproto->facets.rwlock);
5824 ds_chomp(&ds, '\n');
5825 unixctl_command_reply(conn, ds_cstr(&ds));
5829 /* Disable using the megaflows.
5831 * This command is only needed for advanced debugging, so it's not
5832 * documented in the man page. */
5834 ofproto_unixctl_dpif_disable_megaflows(struct unixctl_conn *conn,
5835 int argc OVS_UNUSED,
5836 const char *argv[] OVS_UNUSED,
5837 void *aux OVS_UNUSED)
5839 struct ofproto_dpif *ofproto;
5841 enable_megaflows = false;
5843 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5844 flush(&ofproto->up);
5847 unixctl_command_reply(conn, "megaflows disabled");
5850 /* Re-enable using megaflows.
5852 * This command is only needed for advanced debugging, so it's not
5853 * documented in the man page. */
5855 ofproto_unixctl_dpif_enable_megaflows(struct unixctl_conn *conn,
5856 int argc OVS_UNUSED,
5857 const char *argv[] OVS_UNUSED,
5858 void *aux OVS_UNUSED)
5860 struct ofproto_dpif *ofproto;
5862 enable_megaflows = true;
5864 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5865 flush(&ofproto->up);
5868 unixctl_command_reply(conn, "megaflows enabled");
5872 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
5873 int argc OVS_UNUSED, const char *argv[],
5874 void *aux OVS_UNUSED)
5876 struct ds ds = DS_EMPTY_INITIALIZER;
5877 const struct ofproto_dpif *ofproto;
5878 struct subfacet *subfacet;
5880 ofproto = ofproto_dpif_lookup(argv[1]);
5882 unixctl_command_reply_error(conn, "no such bridge");
5886 update_stats(ofproto->backer);
5888 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->backer->subfacets) {
5889 struct facet *facet = subfacet->facet;
5890 struct odputil_keybuf maskbuf;
5893 if (facet->ofproto != ofproto) {
5897 ofpbuf_use_stack(&mask, &maskbuf, sizeof maskbuf);
5898 if (enable_megaflows) {
5899 odp_flow_key_from_mask(&mask, &facet->xout.wc.masks,
5900 &facet->flow, UINT32_MAX);
5903 odp_flow_format(subfacet->key, subfacet->key_len,
5904 mask.data, mask.size, &ds, false);
5906 ds_put_format(&ds, ", packets:%"PRIu64", bytes:%"PRIu64", used:",
5907 subfacet->dp_packet_count, subfacet->dp_byte_count);
5908 if (subfacet->used) {
5909 ds_put_format(&ds, "%.3fs",
5910 (time_msec() - subfacet->used) / 1000.0);
5912 ds_put_format(&ds, "never");
5914 if (subfacet->facet->tcp_flags) {
5915 ds_put_cstr(&ds, ", flags:");
5916 packet_format_tcp_flags(&ds, subfacet->facet->tcp_flags);
5919 ds_put_cstr(&ds, ", actions:");
5920 if (facet->xout.slow) {
5921 uint64_t slow_path_stub[128 / 8];
5922 const struct nlattr *actions;
5925 compose_slow_path(ofproto, &facet->flow, facet->xout.slow,
5926 slow_path_stub, sizeof slow_path_stub,
5927 &actions, &actions_len);
5928 format_odp_actions(&ds, actions, actions_len);
5930 format_odp_actions(&ds, facet->xout.odp_actions.data,
5931 facet->xout.odp_actions.size);
5933 ds_put_char(&ds, '\n');
5936 unixctl_command_reply(conn, ds_cstr(&ds));
5941 ofproto_unixctl_dpif_del_flows(struct unixctl_conn *conn,
5942 int argc OVS_UNUSED, const char *argv[],
5943 void *aux OVS_UNUSED)
5945 struct ds ds = DS_EMPTY_INITIALIZER;
5946 struct ofproto_dpif *ofproto;
5948 ofproto = ofproto_dpif_lookup(argv[1]);
5950 unixctl_command_reply_error(conn, "no such bridge");
5954 flush(&ofproto->up);
5956 unixctl_command_reply(conn, ds_cstr(&ds));
5961 ofproto_dpif_unixctl_init(void)
5963 static bool registered;
5969 unixctl_command_register(
5971 "[dp_name]|bridge odp_flow|br_flow [-generate|packet]",
5972 1, 3, ofproto_unixctl_trace, NULL);
5973 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
5974 ofproto_unixctl_fdb_flush, NULL);
5975 unixctl_command_register("fdb/show", "bridge", 1, 1,
5976 ofproto_unixctl_fdb_show, NULL);
5977 unixctl_command_register("ofproto/self-check", "[bridge]", 0, 1,
5978 ofproto_dpif_self_check, NULL);
5979 unixctl_command_register("dpif/dump-dps", "", 0, 0,
5980 ofproto_unixctl_dpif_dump_dps, NULL);
5981 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
5983 unixctl_command_register("dpif/dump-flows", "bridge", 1, 1,
5984 ofproto_unixctl_dpif_dump_flows, NULL);
5985 unixctl_command_register("dpif/del-flows", "bridge", 1, 1,
5986 ofproto_unixctl_dpif_del_flows, NULL);
5987 unixctl_command_register("dpif/dump-megaflows", "bridge", 1, 1,
5988 ofproto_unixctl_dpif_dump_megaflows, NULL);
5989 unixctl_command_register("dpif/disable-megaflows", "", 0, 0,
5990 ofproto_unixctl_dpif_disable_megaflows, NULL);
5991 unixctl_command_register("dpif/enable-megaflows", "", 0, 0,
5992 ofproto_unixctl_dpif_enable_megaflows, NULL);
5995 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
5997 * This is deprecated. It is only for compatibility with broken device drivers
5998 * in old versions of Linux that do not properly support VLANs when VLAN
5999 * devices are not used. When broken device drivers are no longer in
6000 * widespread use, we will delete these interfaces. */
6003 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
6005 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
6006 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
6008 if (realdev_ofp_port == ofport->realdev_ofp_port
6009 && vid == ofport->vlandev_vid) {
6013 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6015 if (ofport->realdev_ofp_port) {
6018 if (realdev_ofp_port && ofport->bundle) {
6019 /* vlandevs are enslaved to their realdevs, so they are not allowed to
6020 * themselves be part of a bundle. */
6021 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
6024 ofport->realdev_ofp_port = realdev_ofp_port;
6025 ofport->vlandev_vid = vid;
6027 if (realdev_ofp_port) {
6028 vsp_add(ofport, realdev_ofp_port, vid);
6035 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
6037 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
6041 ofproto_has_vlan_splinters(const struct ofproto_dpif *ofproto)
6042 OVS_EXCLUDED(ofproto->vsp_mutex)
6046 ovs_mutex_lock(&ofproto->vsp_mutex);
6047 ret = !hmap_is_empty(&ofproto->realdev_vid_map);
6048 ovs_mutex_unlock(&ofproto->vsp_mutex);
6053 vsp_realdev_to_vlandev__(const struct ofproto_dpif *ofproto,
6054 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
6055 OVS_REQUIRES(ofproto->vsp_mutex)
6057 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
6058 int vid = vlan_tci_to_vid(vlan_tci);
6059 const struct vlan_splinter *vsp;
6061 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
6062 hash_realdev_vid(realdev_ofp_port, vid),
6063 &ofproto->realdev_vid_map) {
6064 if (vsp->realdev_ofp_port == realdev_ofp_port
6065 && vsp->vid == vid) {
6066 return vsp->vlandev_ofp_port;
6070 return realdev_ofp_port;
6073 /* Returns the OFP port number of the Linux VLAN device that corresponds to
6074 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
6075 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
6076 * 'vlan_tci' 9, it would return the port number of eth0.9.
6078 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
6079 * function just returns its 'realdev_ofp_port' argument. */
6081 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
6082 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
6083 OVS_EXCLUDED(ofproto->vsp_mutex)
6087 ovs_mutex_lock(&ofproto->vsp_mutex);
6088 ret = vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, vlan_tci);
6089 ovs_mutex_unlock(&ofproto->vsp_mutex);
6093 static struct vlan_splinter *
6094 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
6096 struct vlan_splinter *vsp;
6098 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
6099 hash_ofp_port(vlandev_ofp_port),
6100 &ofproto->vlandev_map) {
6101 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
6109 /* Returns the OpenFlow port number of the "real" device underlying the Linux
6110 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
6111 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
6112 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
6113 * eth0 and store 9 in '*vid'.
6115 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
6116 * VLAN device. Unless VLAN splinters are enabled, this is what this function
6119 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
6120 ofp_port_t vlandev_ofp_port, int *vid)
6121 OVS_REQUIRES(ofproto->vsp_mutex)
6123 if (!hmap_is_empty(&ofproto->vlandev_map)) {
6124 const struct vlan_splinter *vsp;
6126 vsp = vlandev_find(ofproto, vlandev_ofp_port);
6131 return vsp->realdev_ofp_port;
6137 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
6138 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
6139 * 'flow->in_port' to the "real" device backing the VLAN device, sets
6140 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
6141 * always the case unless VLAN splinters are enabled), returns false without
6142 * making any changes. */
6144 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
6145 OVS_EXCLUDED(ofproto->vsp_mutex)
6150 ovs_mutex_lock(&ofproto->vsp_mutex);
6151 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
6152 ovs_mutex_unlock(&ofproto->vsp_mutex);
6157 /* Cause the flow to be processed as if it came in on the real device with
6158 * the VLAN device's VLAN ID. */
6159 flow->in_port.ofp_port = realdev;
6160 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
6165 vsp_remove(struct ofport_dpif *port)
6167 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
6168 struct vlan_splinter *vsp;
6170 ovs_mutex_lock(&ofproto->vsp_mutex);
6171 vsp = vlandev_find(ofproto, port->up.ofp_port);
6173 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
6174 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
6177 port->realdev_ofp_port = 0;
6179 VLOG_ERR("missing vlan device record");
6181 ovs_mutex_unlock(&ofproto->vsp_mutex);
6185 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
6187 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
6189 ovs_mutex_lock(&ofproto->vsp_mutex);
6190 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
6191 && (vsp_realdev_to_vlandev__(ofproto, realdev_ofp_port, htons(vid))
6192 == realdev_ofp_port)) {
6193 struct vlan_splinter *vsp;
6195 vsp = xmalloc(sizeof *vsp);
6196 vsp->realdev_ofp_port = realdev_ofp_port;
6197 vsp->vlandev_ofp_port = port->up.ofp_port;
6200 port->realdev_ofp_port = realdev_ofp_port;
6202 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
6203 hash_ofp_port(port->up.ofp_port));
6204 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
6205 hash_realdev_vid(realdev_ofp_port, vid));
6207 VLOG_ERR("duplicate vlan device record");
6209 ovs_mutex_unlock(&ofproto->vsp_mutex);
6213 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
6215 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
6216 return ofport ? ofport->odp_port : ODPP_NONE;
6219 struct ofport_dpif *
6220 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
6222 struct ofport_dpif *port;
6224 ovs_rwlock_rdlock(&backer->odp_to_ofport_lock);
6225 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
6226 &backer->odp_to_ofport_map) {
6227 if (port->odp_port == odp_port) {
6228 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
6233 ovs_rwlock_unlock(&backer->odp_to_ofport_lock);
6238 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
6240 struct ofport_dpif *port;
6242 port = odp_port_to_ofport(ofproto->backer, odp_port);
6243 if (port && &ofproto->up == port->up.ofproto) {
6244 return port->up.ofp_port;
6250 /* Compute exponentially weighted moving average, adding 'new' as the newest,
6251 * most heavily weighted element. 'base' designates the rate of decay: after
6252 * 'base' further updates, 'new''s weight in the EWMA decays to about 1/e
6255 exp_mavg(double *avg, int base, double new)
6257 *avg = (*avg * (base - 1) + new) / base;
6261 update_moving_averages(struct dpif_backer *backer)
6263 const int min_ms = 60 * 1000; /* milliseconds in one minute. */
6264 long long int minutes = (time_msec() - backer->created) / min_ms;
6267 backer->lifetime.add_rate = (double) backer->total_subfacet_add_count
6269 backer->lifetime.del_rate = (double) backer->total_subfacet_del_count
6272 backer->lifetime.add_rate = 0.0;
6273 backer->lifetime.del_rate = 0.0;
6276 /* Update hourly averages on the minute boundaries. */
6277 if (time_msec() - backer->last_minute >= min_ms) {
6278 exp_mavg(&backer->hourly.add_rate, 60, backer->subfacet_add_count);
6279 exp_mavg(&backer->hourly.del_rate, 60, backer->subfacet_del_count);
6281 /* Update daily averages on the hour boundaries. */
6282 if ((backer->last_minute - backer->created) / min_ms % 60 == 59) {
6283 exp_mavg(&backer->daily.add_rate, 24, backer->hourly.add_rate);
6284 exp_mavg(&backer->daily.del_rate, 24, backer->hourly.del_rate);
6287 backer->total_subfacet_add_count += backer->subfacet_add_count;
6288 backer->total_subfacet_del_count += backer->subfacet_del_count;
6289 backer->subfacet_add_count = 0;
6290 backer->subfacet_del_count = 0;
6291 backer->last_minute += min_ms;
6295 const struct ofproto_class ofproto_dpif_class = {
6330 port_is_lacp_current,
6331 NULL, /* rule_choose_table */
6340 rule_modify_actions,
6354 get_stp_port_status,
6361 is_mirror_output_bundle,
6362 forward_bpdu_changed,
6363 set_mac_table_config,
6365 NULL, /* meter_get_features */
6366 NULL, /* meter_set */
6367 NULL, /* meter_get */
6368 NULL, /* meter_del */
6369 NULL, /* group_alloc */
6370 NULL, /* group_construct */
6371 NULL, /* group_destruct */
6372 NULL, /* group_dealloc */
6373 NULL, /* group_modify */
6374 NULL, /* group_get_stats */