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"
37 #include "mac-learning.h"
38 #include "meta-flow.h"
39 #include "multipath.h"
40 #include "netdev-vport.h"
45 #include "odp-execute.h"
48 #include "ofp-actions.h"
49 #include "ofp-parse.h"
50 #include "ofp-print.h"
51 #include "ofproto-dpif-governor.h"
52 #include "ofproto-dpif-ipfix.h"
53 #include "ofproto-dpif-sflow.h"
54 #include "ofproto-dpif-xlate.h"
55 #include "poll-loop.h"
60 #include "unaligned.h"
62 #include "vlan-bitmap.h"
65 VLOG_DEFINE_THIS_MODULE(ofproto_dpif);
67 COVERAGE_DEFINE(ofproto_dpif_expired);
68 COVERAGE_DEFINE(facet_changed_rule);
69 COVERAGE_DEFINE(facet_revalidate);
70 COVERAGE_DEFINE(facet_unexpected);
71 COVERAGE_DEFINE(facet_suppress);
72 COVERAGE_DEFINE(subfacet_install_fail);
77 static struct rule_dpif *rule_dpif_lookup(struct ofproto_dpif *,
79 struct flow_wildcards *wc);
81 static void rule_get_stats(struct rule *, uint64_t *packets, uint64_t *bytes);
82 static void rule_invalidate(const struct rule_dpif *);
84 static void mirror_destroy(struct ofmirror *);
85 static void update_mirror_stats(struct ofproto_dpif *ofproto,
86 mirror_mask_t mirrors,
87 uint64_t packets, uint64_t bytes);
89 static void bundle_remove(struct ofport *);
90 static void bundle_update(struct ofbundle *);
91 static void bundle_destroy(struct ofbundle *);
92 static void bundle_del_port(struct ofport_dpif *);
93 static void bundle_run(struct ofbundle *);
94 static void bundle_wait(struct ofbundle *);
96 static void stp_run(struct ofproto_dpif *ofproto);
97 static void stp_wait(struct ofproto_dpif *ofproto);
98 static int set_stp_port(struct ofport *,
99 const struct ofproto_port_stp_settings *);
101 static void compose_slow_path(const struct ofproto_dpif *, const struct flow *,
102 enum slow_path_reason,
103 uint64_t *stub, size_t stub_size,
104 const struct nlattr **actionsp,
105 size_t *actions_lenp);
107 /* A subfacet (see "struct subfacet" below) has three possible installation
110 * - SF_NOT_INSTALLED: Not installed in the datapath. This will only be the
111 * case just after the subfacet is created, just before the subfacet is
112 * destroyed, or if the datapath returns an error when we try to install a
115 * - SF_FAST_PATH: The subfacet's actions are installed in the datapath.
117 * - SF_SLOW_PATH: An action that sends every packet for the subfacet through
118 * ofproto_dpif is installed in the datapath.
121 SF_NOT_INSTALLED, /* No datapath flow for this subfacet. */
122 SF_FAST_PATH, /* Full actions are installed. */
123 SF_SLOW_PATH, /* Send-to-userspace action is installed. */
126 /* A dpif flow and actions associated with a facet.
128 * See also the large comment on struct facet. */
131 struct hmap_node hmap_node; /* In struct ofproto_dpif 'subfacets' list. */
132 struct list list_node; /* In struct facet's 'facets' list. */
133 struct facet *facet; /* Owning facet. */
134 struct dpif_backer *backer; /* Owning backer. */
136 enum odp_key_fitness key_fitness;
140 long long int used; /* Time last used; time created if not used. */
141 long long int created; /* Time created. */
143 uint64_t dp_packet_count; /* Last known packet count in the datapath. */
144 uint64_t dp_byte_count; /* Last known byte count in the datapath. */
146 enum subfacet_path path; /* Installed in datapath? */
149 #define SUBFACET_DESTROY_MAX_BATCH 50
151 static struct subfacet *subfacet_create(struct facet *, struct flow_miss *miss,
153 static struct subfacet *subfacet_find(struct dpif_backer *,
154 const struct nlattr *key, size_t key_len,
156 static void subfacet_destroy(struct subfacet *);
157 static void subfacet_destroy__(struct subfacet *);
158 static void subfacet_destroy_batch(struct dpif_backer *,
159 struct subfacet **, int n);
160 static void subfacet_reset_dp_stats(struct subfacet *,
161 struct dpif_flow_stats *);
162 static void subfacet_update_stats(struct subfacet *,
163 const struct dpif_flow_stats *);
164 static int subfacet_install(struct subfacet *,
165 const struct ofpbuf *odp_actions,
166 struct dpif_flow_stats *);
167 static void subfacet_uninstall(struct subfacet *);
169 /* A unique, non-overlapping instantiation of an OpenFlow flow.
171 * A facet associates a "struct flow", which represents the Open vSwitch
172 * userspace idea of an exact-match flow, with one or more subfacets.
173 * While the facet is created based on an exact-match flow, it is stored
174 * within the ofproto based on the wildcards that could be expressed
175 * based on the flow table and other configuration. (See the 'wc'
176 * description in "struct xlate_out" for more details.)
178 * Each subfacet tracks the datapath's idea of the flow equivalent to
179 * the facet. When the kernel module (or other dpif implementation) and
180 * Open vSwitch userspace agree on the definition of a flow key, there
181 * is exactly one subfacet per facet. If the dpif implementation
182 * supports more-specific flow matching than userspace, however, a facet
183 * can have more than one subfacet. Examples include the dpif
184 * implementation not supporting the same wildcards as userspace or some
185 * distinction in flow that userspace simply doesn't understand.
187 * Flow expiration works in terms of subfacets, so a facet must have at
188 * least one subfacet or it will never expire, leaking memory. */
191 struct hmap_node hmap_node; /* In owning ofproto's 'facets' hmap. */
192 struct list list_node; /* In owning rule's 'facets' list. */
193 struct rule_dpif *rule; /* Owning rule. */
196 struct list subfacets;
197 long long int used; /* Time last used; time created if not used. */
200 struct flow flow; /* Flow of the creating subfacet. */
201 struct cls_rule cr; /* In 'ofproto_dpif's facets classifier. */
205 * - Do include packets and bytes sent "by hand", e.g. with
208 * - Do include packets and bytes that were obtained from the datapath
209 * when a subfacet's statistics were reset (e.g. dpif_flow_put() with
210 * DPIF_FP_ZERO_STATS).
212 * - Do not include packets or bytes that can be obtained from the
213 * datapath for any existing subfacet.
215 uint64_t packet_count; /* Number of packets received. */
216 uint64_t byte_count; /* Number of bytes received. */
218 /* Resubmit statistics. */
219 uint64_t prev_packet_count; /* Number of packets from last stats push. */
220 uint64_t prev_byte_count; /* Number of bytes from last stats push. */
221 long long int prev_used; /* Used time from last stats push. */
224 uint64_t accounted_bytes; /* Bytes processed by facet_account(). */
225 struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
226 uint8_t tcp_flags; /* TCP flags seen for this 'rule'. */
228 struct xlate_out xout;
230 /* Storage for a single subfacet, to reduce malloc() time and space
231 * overhead. (A facet always has at least one subfacet and in the common
232 * case has exactly one subfacet. However, 'one_subfacet' may not
233 * always be valid, since it could have been removed after newer
234 * subfacets were pushed onto the 'subfacets' list.) */
235 struct subfacet one_subfacet;
237 long long int learn_rl; /* Rate limiter for facet_learn(). */
240 static struct facet *facet_create(const struct flow_miss *, struct rule_dpif *,
242 struct dpif_flow_stats *);
243 static void facet_remove(struct facet *);
244 static void facet_free(struct facet *);
246 static struct facet *facet_find(struct ofproto_dpif *, const struct flow *);
247 static struct facet *facet_lookup_valid(struct ofproto_dpif *,
248 const struct flow *);
249 static bool facet_revalidate(struct facet *);
250 static bool facet_check_consistency(struct facet *);
252 static void facet_flush_stats(struct facet *);
254 static void facet_reset_counters(struct facet *);
255 static void facet_push_stats(struct facet *, bool may_learn);
256 static void facet_learn(struct facet *);
257 static void facet_account(struct facet *);
258 static void push_all_stats(void);
260 static bool facet_is_controller_flow(struct facet *);
262 /* Node in 'ofport_dpif''s 'priorities' map. Used to maintain a map from
263 * 'priority' (the datapath's term for QoS queue) to the dscp bits which all
264 * traffic egressing the 'ofport' with that priority should be marked with. */
265 struct priority_to_dscp {
266 struct hmap_node hmap_node; /* Node in 'ofport_dpif''s 'priorities' map. */
267 uint32_t priority; /* Priority of this queue (see struct flow). */
269 uint8_t dscp; /* DSCP bits to mark outgoing traffic with. */
272 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
274 * This is deprecated. It is only for compatibility with broken device drivers
275 * in old versions of Linux that do not properly support VLANs when VLAN
276 * devices are not used. When broken device drivers are no longer in
277 * widespread use, we will delete these interfaces. */
278 struct vlan_splinter {
279 struct hmap_node realdev_vid_node;
280 struct hmap_node vlandev_node;
281 ofp_port_t realdev_ofp_port;
282 ofp_port_t vlandev_ofp_port;
286 static bool vsp_adjust_flow(const struct ofproto_dpif *, struct flow *);
287 static void vsp_remove(struct ofport_dpif *);
288 static void vsp_add(struct ofport_dpif *, ofp_port_t realdev_ofp_port, int vid);
290 static ofp_port_t odp_port_to_ofp_port(const struct ofproto_dpif *,
291 odp_port_t odp_port);
293 static struct ofport_dpif *
294 ofport_dpif_cast(const struct ofport *ofport)
296 return ofport ? CONTAINER_OF(ofport, struct ofport_dpif, up) : NULL;
299 static void port_run(struct ofport_dpif *);
300 static void port_run_fast(struct ofport_dpif *);
301 static void port_wait(struct ofport_dpif *);
302 static int set_bfd(struct ofport *, const struct smap *);
303 static int set_cfm(struct ofport *, const struct cfm_settings *);
304 static void ofport_clear_priorities(struct ofport_dpif *);
305 static void ofport_update_peer(struct ofport_dpif *);
306 static void run_fast_rl(void);
308 struct dpif_completion {
309 struct list list_node;
310 struct ofoperation *op;
313 /* Reasons that we might need to revalidate every facet, and corresponding
316 * A value of 0 means that there is no need to revalidate.
318 * It would be nice to have some cleaner way to integrate with coverage
319 * counters, but with only a few reasons I guess this is good enough for
321 enum revalidate_reason {
322 REV_RECONFIGURE = 1, /* Switch configuration changed. */
323 REV_STP, /* Spanning tree protocol port status change. */
324 REV_PORT_TOGGLED, /* Port enabled or disabled by CFM, LACP, ...*/
325 REV_FLOW_TABLE, /* Flow table changed. */
326 REV_INCONSISTENCY /* Facet self-check failed. */
328 COVERAGE_DEFINE(rev_reconfigure);
329 COVERAGE_DEFINE(rev_stp);
330 COVERAGE_DEFINE(rev_port_toggled);
331 COVERAGE_DEFINE(rev_flow_table);
332 COVERAGE_DEFINE(rev_inconsistency);
334 /* Drop keys are odp flow keys which have drop flows installed in the kernel.
335 * These are datapath flows which have no associated ofproto, if they did we
336 * would use facets. */
338 struct hmap_node hmap_node;
343 struct avg_subfacet_rates {
344 double add_rate; /* Moving average of new flows created per minute. */
345 double del_rate; /* Moving average of flows deleted per minute. */
348 /* All datapaths of a given type share a single dpif backer instance. */
353 struct timer next_expiration;
354 struct hmap odp_to_ofport_map; /* ODP port to ofport mapping. */
356 struct simap tnl_backers; /* Set of dpif ports backing tunnels. */
358 /* Facet revalidation flags applying to facets which use this backer. */
359 enum revalidate_reason need_revalidate; /* Revalidate every facet. */
360 struct tag_set revalidate_set; /* Revalidate only matching facets. */
362 struct hmap drop_keys; /* Set of dropped odp keys. */
363 bool recv_set_enable; /* Enables or disables receiving packets. */
365 struct hmap subfacets;
366 struct governor *governor;
368 /* Subfacet statistics.
370 * These keep track of the total number of subfacets added and deleted and
371 * flow life span. They are useful for computing the flow rates stats
372 * exposed via "ovs-appctl dpif/show". The goal is to learn about
373 * traffic patterns in ways that we can use later to improve Open vSwitch
374 * performance in new situations. */
375 long long int created; /* Time when it is created. */
376 unsigned max_n_subfacet; /* Maximum number of flows */
377 unsigned avg_n_subfacet; /* Average number of flows. */
378 long long int avg_subfacet_life; /* Average life span of subfacets. */
380 /* The average number of subfacets... */
381 struct avg_subfacet_rates hourly; /* ...over the last hour. */
382 struct avg_subfacet_rates daily; /* ...over the last day. */
383 struct avg_subfacet_rates lifetime; /* ...over the switch lifetime. */
384 long long int last_minute; /* Last time 'hourly' was updated. */
386 /* Number of subfacets added or deleted since 'last_minute'. */
387 unsigned subfacet_add_count;
388 unsigned subfacet_del_count;
390 /* Number of subfacets added or deleted from 'created' to 'last_minute.' */
391 unsigned long long int total_subfacet_add_count;
392 unsigned long long int total_subfacet_del_count;
395 /* All existing ofproto_backer instances, indexed by ofproto->up.type. */
396 static struct shash all_dpif_backers = SHASH_INITIALIZER(&all_dpif_backers);
398 static void drop_key_clear(struct dpif_backer *);
399 static struct ofport_dpif *
400 odp_port_to_ofport(const struct dpif_backer *, odp_port_t odp_port);
401 static void update_moving_averages(struct dpif_backer *backer);
403 /* Defer flow mod completion until "ovs-appctl ofproto/unclog"? (Useful only
404 * for debugging the asynchronous flow_mod implementation.) */
407 /* All existing ofproto_dpif instances, indexed by ->up.name. */
408 static struct hmap all_ofproto_dpifs = HMAP_INITIALIZER(&all_ofproto_dpifs);
410 static void ofproto_dpif_unixctl_init(void);
413 #define FLOW_MISS_MAX_BATCH 50
414 static int handle_upcalls(struct dpif_backer *, unsigned int max_batch);
416 /* Flow expiration. */
417 static int expire(struct dpif_backer *);
420 static void send_netflow_active_timeouts(struct ofproto_dpif *);
423 static int send_packet(const struct ofport_dpif *, struct ofpbuf *packet);
425 /* Global variables. */
426 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
428 /* Initial mappings of port to bridge mappings. */
429 static struct shash init_ofp_ports = SHASH_INITIALIZER(&init_ofp_ports);
431 /* Factory functions. */
434 init(const struct shash *iface_hints)
436 struct shash_node *node;
438 /* Make a local copy, since we don't own 'iface_hints' elements. */
439 SHASH_FOR_EACH(node, iface_hints) {
440 const struct iface_hint *orig_hint = node->data;
441 struct iface_hint *new_hint = xmalloc(sizeof *new_hint);
443 new_hint->br_name = xstrdup(orig_hint->br_name);
444 new_hint->br_type = xstrdup(orig_hint->br_type);
445 new_hint->ofp_port = orig_hint->ofp_port;
447 shash_add(&init_ofp_ports, node->name, new_hint);
452 enumerate_types(struct sset *types)
454 dp_enumerate_types(types);
458 enumerate_names(const char *type, struct sset *names)
460 struct ofproto_dpif *ofproto;
463 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
464 if (strcmp(type, ofproto->up.type)) {
467 sset_add(names, ofproto->up.name);
474 del(const char *type, const char *name)
479 error = dpif_open(name, type, &dpif);
481 error = dpif_delete(dpif);
488 port_open_type(const char *datapath_type, const char *port_type)
490 return dpif_port_open_type(datapath_type, port_type);
493 /* Type functions. */
495 static struct ofproto_dpif *
496 lookup_ofproto_dpif_by_port_name(const char *name)
498 struct ofproto_dpif *ofproto;
500 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
501 if (sset_contains(&ofproto->ports, name)) {
510 type_run(const char *type)
512 static long long int push_timer = LLONG_MIN;
513 struct dpif_backer *backer;
517 backer = shash_find_data(&all_dpif_backers, type);
519 /* This is not necessarily a problem, since backers are only
520 * created on demand. */
524 dpif_run(backer->dpif);
526 /* The most natural place to push facet statistics is when they're pulled
527 * from the datapath. However, when there are many flows in the datapath,
528 * this expensive operation can occur so frequently, that it reduces our
529 * ability to quickly set up flows. To reduce the cost, we push statistics
531 if (time_msec() > push_timer) {
532 push_timer = time_msec() + 2000;
536 /* If vswitchd started with other_config:flow_restore_wait set as "true",
537 * and the configuration has now changed to "false", enable receiving
538 * packets from the datapath. */
539 if (!backer->recv_set_enable && !ofproto_get_flow_restore_wait()) {
540 backer->recv_set_enable = true;
542 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
544 VLOG_ERR("Failed to enable receiving packets in dpif.");
547 dpif_flow_flush(backer->dpif);
548 backer->need_revalidate = REV_RECONFIGURE;
551 if (backer->need_revalidate
552 || !tag_set_is_empty(&backer->revalidate_set)) {
553 struct tag_set revalidate_set = backer->revalidate_set;
554 bool need_revalidate = backer->need_revalidate;
555 struct ofproto_dpif *ofproto;
556 struct simap_node *node;
557 struct simap tmp_backers;
559 /* Handle tunnel garbage collection. */
560 simap_init(&tmp_backers);
561 simap_swap(&backer->tnl_backers, &tmp_backers);
563 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
564 struct ofport_dpif *iter;
566 if (backer != ofproto->backer) {
570 HMAP_FOR_EACH (iter, up.hmap_node, &ofproto->up.ports) {
571 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
574 if (!iter->tnl_port) {
578 dp_port = netdev_vport_get_dpif_port(iter->up.netdev,
579 namebuf, sizeof namebuf);
580 node = simap_find(&tmp_backers, dp_port);
582 simap_put(&backer->tnl_backers, dp_port, node->data);
583 simap_delete(&tmp_backers, node);
584 node = simap_find(&backer->tnl_backers, dp_port);
586 node = simap_find(&backer->tnl_backers, dp_port);
588 odp_port_t odp_port = ODPP_NONE;
590 if (!dpif_port_add(backer->dpif, iter->up.netdev,
592 simap_put(&backer->tnl_backers, dp_port,
593 odp_to_u32(odp_port));
594 node = simap_find(&backer->tnl_backers, dp_port);
599 iter->odp_port = node ? u32_to_odp(node->data) : ODPP_NONE;
600 if (tnl_port_reconfigure(&iter->up, iter->odp_port,
602 backer->need_revalidate = REV_RECONFIGURE;
607 SIMAP_FOR_EACH (node, &tmp_backers) {
608 dpif_port_del(backer->dpif, u32_to_odp(node->data));
610 simap_destroy(&tmp_backers);
612 switch (backer->need_revalidate) {
613 case REV_RECONFIGURE: COVERAGE_INC(rev_reconfigure); break;
614 case REV_STP: COVERAGE_INC(rev_stp); break;
615 case REV_PORT_TOGGLED: COVERAGE_INC(rev_port_toggled); break;
616 case REV_FLOW_TABLE: COVERAGE_INC(rev_flow_table); break;
617 case REV_INCONSISTENCY: COVERAGE_INC(rev_inconsistency); break;
620 if (backer->need_revalidate) {
621 /* Clear the drop_keys in case we should now be accepting some
622 * formerly dropped flows. */
623 drop_key_clear(backer);
626 /* Clear the revalidation flags. */
627 tag_set_init(&backer->revalidate_set);
628 backer->need_revalidate = 0;
630 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
631 struct facet *facet, *next;
632 struct cls_cursor cursor;
634 if (ofproto->backer != backer) {
638 cls_cursor_init(&cursor, &ofproto->facets, NULL);
639 CLS_CURSOR_FOR_EACH_SAFE (facet, next, cr, &cursor) {
641 || tag_set_intersects(&revalidate_set, facet->xout.tags)) {
642 facet_revalidate(facet);
649 if (!backer->recv_set_enable) {
650 /* Wake up before a max of 1000ms. */
651 timer_set_duration(&backer->next_expiration, 1000);
652 } else if (timer_expired(&backer->next_expiration)) {
653 int delay = expire(backer);
654 timer_set_duration(&backer->next_expiration, delay);
657 /* Check for port changes in the dpif. */
658 while ((error = dpif_port_poll(backer->dpif, &devname)) == 0) {
659 struct ofproto_dpif *ofproto;
660 struct dpif_port port;
662 /* Don't report on the datapath's device. */
663 if (!strcmp(devname, dpif_base_name(backer->dpif))) {
667 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
668 &all_ofproto_dpifs) {
669 if (simap_contains(&ofproto->backer->tnl_backers, devname)) {
674 ofproto = lookup_ofproto_dpif_by_port_name(devname);
675 if (dpif_port_query_by_name(backer->dpif, devname, &port)) {
676 /* The port was removed. If we know the datapath,
677 * report it through poll_set(). If we don't, it may be
678 * notifying us of a removal we initiated, so ignore it.
679 * If there's a pending ENOBUFS, let it stand, since
680 * everything will be reevaluated. */
681 if (ofproto && ofproto->port_poll_errno != ENOBUFS) {
682 sset_add(&ofproto->port_poll_set, devname);
683 ofproto->port_poll_errno = 0;
685 } else if (!ofproto) {
686 /* The port was added, but we don't know with which
687 * ofproto we should associate it. Delete it. */
688 dpif_port_del(backer->dpif, port.port_no);
690 dpif_port_destroy(&port);
696 if (error != EAGAIN) {
697 struct ofproto_dpif *ofproto;
699 /* There was some sort of error, so propagate it to all
700 * ofprotos that use this backer. */
701 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node,
702 &all_ofproto_dpifs) {
703 if (ofproto->backer == backer) {
704 sset_clear(&ofproto->port_poll_set);
705 ofproto->port_poll_errno = error;
710 if (backer->governor) {
713 governor_run(backer->governor);
715 /* If the governor has shrunk to its minimum size and the number of
716 * subfacets has dwindled, then drop the governor entirely.
718 * For hysteresis, the number of subfacets to drop the governor is
719 * smaller than the number needed to trigger its creation. */
720 n_subfacets = hmap_count(&backer->subfacets);
721 if (n_subfacets * 4 < flow_eviction_threshold
722 && governor_is_idle(backer->governor)) {
723 governor_destroy(backer->governor);
724 backer->governor = NULL;
732 dpif_backer_run_fast(struct dpif_backer *backer, int max_batch)
736 /* If recv_set_enable is false, we should not handle upcalls. */
737 if (!backer->recv_set_enable) {
741 /* Handle one or more batches of upcalls, until there's nothing left to do
742 * or until we do a fixed total amount of work.
744 * We do work in batches because it can be much cheaper to set up a number
745 * of flows and fire off their patches all at once. We do multiple batches
746 * because in some cases handling a packet can cause another packet to be
747 * queued almost immediately as part of the return flow. Both
748 * optimizations can make major improvements on some benchmarks and
749 * presumably for real traffic as well. */
751 while (work < max_batch) {
752 int retval = handle_upcalls(backer, max_batch - work);
763 type_run_fast(const char *type)
765 struct dpif_backer *backer;
767 backer = shash_find_data(&all_dpif_backers, type);
769 /* This is not necessarily a problem, since backers are only
770 * created on demand. */
774 return dpif_backer_run_fast(backer, FLOW_MISS_MAX_BATCH);
780 static long long int port_rl = LLONG_MIN;
781 static unsigned int backer_rl = 0;
783 if (time_msec() >= port_rl) {
784 struct ofproto_dpif *ofproto;
785 struct ofport_dpif *ofport;
787 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
789 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
790 port_run_fast(ofport);
793 port_rl = time_msec() + 200;
796 /* XXX: We have to be careful not to do too much work in this function. If
797 * we call dpif_backer_run_fast() too often, or with too large a batch,
798 * performance improves signifcantly, but at a cost. It's possible for the
799 * number of flows in the datapath to increase without bound, and for poll
800 * loops to take 10s of seconds. The correct solution to this problem,
801 * long term, is to separate flow miss handling into it's own thread so it
802 * isn't affected by revalidations, and expirations. Until then, this is
803 * the best we can do. */
804 if (++backer_rl >= 10) {
805 struct shash_node *node;
808 SHASH_FOR_EACH (node, &all_dpif_backers) {
809 dpif_backer_run_fast(node->data, 1);
815 type_wait(const char *type)
817 struct dpif_backer *backer;
819 backer = shash_find_data(&all_dpif_backers, type);
821 /* This is not necessarily a problem, since backers are only
822 * created on demand. */
826 if (backer->governor) {
827 governor_wait(backer->governor);
830 timer_wait(&backer->next_expiration);
833 /* Basic life-cycle. */
835 static int add_internal_flows(struct ofproto_dpif *);
837 static struct ofproto *
840 struct ofproto_dpif *ofproto = xmalloc(sizeof *ofproto);
845 dealloc(struct ofproto *ofproto_)
847 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
852 close_dpif_backer(struct dpif_backer *backer)
854 struct shash_node *node;
856 ovs_assert(backer->refcount > 0);
858 if (--backer->refcount) {
862 drop_key_clear(backer);
863 hmap_destroy(&backer->drop_keys);
865 simap_destroy(&backer->tnl_backers);
866 hmap_destroy(&backer->odp_to_ofport_map);
867 node = shash_find(&all_dpif_backers, backer->type);
869 shash_delete(&all_dpif_backers, node);
870 dpif_close(backer->dpif);
872 ovs_assert(hmap_is_empty(&backer->subfacets));
873 hmap_destroy(&backer->subfacets);
874 governor_destroy(backer->governor);
879 /* Datapath port slated for removal from datapath. */
881 struct list list_node;
886 open_dpif_backer(const char *type, struct dpif_backer **backerp)
888 struct dpif_backer *backer;
889 struct dpif_port_dump port_dump;
890 struct dpif_port port;
891 struct shash_node *node;
892 struct list garbage_list;
893 struct odp_garbage *garbage, *next;
899 backer = shash_find_data(&all_dpif_backers, type);
906 backer_name = xasprintf("ovs-%s", type);
908 /* Remove any existing datapaths, since we assume we're the only
909 * userspace controlling the datapath. */
911 dp_enumerate_names(type, &names);
912 SSET_FOR_EACH(name, &names) {
913 struct dpif *old_dpif;
915 /* Don't remove our backer if it exists. */
916 if (!strcmp(name, backer_name)) {
920 if (dpif_open(name, type, &old_dpif)) {
921 VLOG_WARN("couldn't open old datapath %s to remove it", name);
923 dpif_delete(old_dpif);
924 dpif_close(old_dpif);
927 sset_destroy(&names);
929 backer = xmalloc(sizeof *backer);
931 error = dpif_create_and_open(backer_name, type, &backer->dpif);
934 VLOG_ERR("failed to open datapath of type %s: %s", type,
940 backer->type = xstrdup(type);
941 backer->governor = NULL;
942 backer->refcount = 1;
943 hmap_init(&backer->odp_to_ofport_map);
944 hmap_init(&backer->drop_keys);
945 hmap_init(&backer->subfacets);
946 timer_set_duration(&backer->next_expiration, 1000);
947 backer->need_revalidate = 0;
948 simap_init(&backer->tnl_backers);
949 tag_set_init(&backer->revalidate_set);
950 backer->recv_set_enable = !ofproto_get_flow_restore_wait();
953 if (backer->recv_set_enable) {
954 dpif_flow_flush(backer->dpif);
957 /* Loop through the ports already on the datapath and remove any
958 * that we don't need anymore. */
959 list_init(&garbage_list);
960 dpif_port_dump_start(&port_dump, backer->dpif);
961 while (dpif_port_dump_next(&port_dump, &port)) {
962 node = shash_find(&init_ofp_ports, port.name);
963 if (!node && strcmp(port.name, dpif_base_name(backer->dpif))) {
964 garbage = xmalloc(sizeof *garbage);
965 garbage->odp_port = port.port_no;
966 list_push_front(&garbage_list, &garbage->list_node);
969 dpif_port_dump_done(&port_dump);
971 LIST_FOR_EACH_SAFE (garbage, next, list_node, &garbage_list) {
972 dpif_port_del(backer->dpif, garbage->odp_port);
973 list_remove(&garbage->list_node);
977 shash_add(&all_dpif_backers, type, backer);
979 error = dpif_recv_set(backer->dpif, backer->recv_set_enable);
981 VLOG_ERR("failed to listen on datapath of type %s: %s",
982 type, strerror(error));
983 close_dpif_backer(backer);
987 backer->max_n_subfacet = 0;
988 backer->created = time_msec();
989 backer->last_minute = backer->created;
990 memset(&backer->hourly, 0, sizeof backer->hourly);
991 memset(&backer->daily, 0, sizeof backer->daily);
992 memset(&backer->lifetime, 0, sizeof backer->lifetime);
993 backer->subfacet_add_count = 0;
994 backer->subfacet_del_count = 0;
995 backer->total_subfacet_add_count = 0;
996 backer->total_subfacet_del_count = 0;
997 backer->avg_n_subfacet = 0;
998 backer->avg_subfacet_life = 0;
1004 construct(struct ofproto *ofproto_)
1006 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1007 struct shash_node *node, *next;
1008 odp_port_t max_ports;
1012 error = open_dpif_backer(ofproto->up.type, &ofproto->backer);
1017 max_ports = dpif_get_max_ports(ofproto->backer->dpif);
1018 ofproto_init_max_ports(ofproto_, u16_to_ofp(MIN(odp_to_u32(max_ports),
1019 ofp_to_u16(OFPP_MAX))));
1021 ofproto->netflow = NULL;
1022 ofproto->sflow = NULL;
1023 ofproto->ipfix = NULL;
1024 ofproto->stp = NULL;
1025 hmap_init(&ofproto->bundles);
1026 ofproto->ml = mac_learning_create(MAC_ENTRY_DEFAULT_IDLE_TIME);
1027 for (i = 0; i < MAX_MIRRORS; i++) {
1028 ofproto->mirrors[i] = NULL;
1030 ofproto->has_bonded_bundles = false;
1032 classifier_init(&ofproto->facets);
1033 ofproto->consistency_rl = LLONG_MIN;
1035 for (i = 0; i < N_TABLES; i++) {
1036 struct table_dpif *table = &ofproto->tables[i];
1038 table->catchall_table = NULL;
1039 table->other_table = NULL;
1040 table->basis = random_uint32();
1043 list_init(&ofproto->completions);
1045 ofproto_dpif_unixctl_init();
1047 ofproto->has_mirrors = false;
1048 ofproto->has_bundle_action = false;
1050 hmap_init(&ofproto->vlandev_map);
1051 hmap_init(&ofproto->realdev_vid_map);
1053 sset_init(&ofproto->ports);
1054 sset_init(&ofproto->ghost_ports);
1055 sset_init(&ofproto->port_poll_set);
1056 ofproto->port_poll_errno = 0;
1058 SHASH_FOR_EACH_SAFE (node, next, &init_ofp_ports) {
1059 struct iface_hint *iface_hint = node->data;
1061 if (!strcmp(iface_hint->br_name, ofproto->up.name)) {
1062 /* Check if the datapath already has this port. */
1063 if (dpif_port_exists(ofproto->backer->dpif, node->name)) {
1064 sset_add(&ofproto->ports, node->name);
1067 free(iface_hint->br_name);
1068 free(iface_hint->br_type);
1070 shash_delete(&init_ofp_ports, node);
1074 hmap_insert(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node,
1075 hash_string(ofproto->up.name, 0));
1076 memset(&ofproto->stats, 0, sizeof ofproto->stats);
1078 ofproto_init_tables(ofproto_, N_TABLES);
1079 error = add_internal_flows(ofproto);
1080 ofproto->up.tables[TBL_INTERNAL].flags = OFTABLE_HIDDEN | OFTABLE_READONLY;
1083 ofproto->n_missed = 0;
1089 add_internal_flow(struct ofproto_dpif *ofproto, int id,
1090 const struct ofpbuf *ofpacts, struct rule_dpif **rulep)
1092 struct ofputil_flow_mod fm;
1095 match_init_catchall(&fm.match);
1097 match_set_reg(&fm.match, 0, id);
1098 fm.new_cookie = htonll(0);
1099 fm.cookie = htonll(0);
1100 fm.cookie_mask = htonll(0);
1101 fm.table_id = TBL_INTERNAL;
1102 fm.command = OFPFC_ADD;
1103 fm.idle_timeout = 0;
1104 fm.hard_timeout = 0;
1108 fm.ofpacts = ofpacts->data;
1109 fm.ofpacts_len = ofpacts->size;
1111 error = ofproto_flow_mod(&ofproto->up, &fm);
1113 VLOG_ERR_RL(&rl, "failed to add internal flow %d (%s)",
1114 id, ofperr_to_string(error));
1118 *rulep = rule_dpif_lookup_in_table(ofproto, &fm.match.flow, NULL,
1120 ovs_assert(*rulep != NULL);
1126 add_internal_flows(struct ofproto_dpif *ofproto)
1128 struct ofpact_controller *controller;
1129 uint64_t ofpacts_stub[128 / 8];
1130 struct ofpbuf ofpacts;
1134 ofpbuf_use_stack(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1137 controller = ofpact_put_CONTROLLER(&ofpacts);
1138 controller->max_len = UINT16_MAX;
1139 controller->controller_id = 0;
1140 controller->reason = OFPR_NO_MATCH;
1141 ofpact_pad(&ofpacts);
1143 error = add_internal_flow(ofproto, id++, &ofpacts, &ofproto->miss_rule);
1148 ofpbuf_clear(&ofpacts);
1149 error = add_internal_flow(ofproto, id++, &ofpacts,
1150 &ofproto->no_packet_in_rule);
1155 error = add_internal_flow(ofproto, id++, &ofpacts,
1156 &ofproto->drop_frags_rule);
1161 complete_operations(struct ofproto_dpif *ofproto)
1163 struct dpif_completion *c, *next;
1165 LIST_FOR_EACH_SAFE (c, next, list_node, &ofproto->completions) {
1166 ofoperation_complete(c->op, 0);
1167 list_remove(&c->list_node);
1173 destruct(struct ofproto *ofproto_)
1175 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1176 struct rule_dpif *rule, *next_rule;
1177 struct oftable *table;
1180 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1181 hmap_remove(&all_ofproto_dpifs, &ofproto->all_ofproto_dpifs_node);
1182 complete_operations(ofproto);
1184 OFPROTO_FOR_EACH_TABLE (table, &ofproto->up) {
1185 struct cls_cursor cursor;
1187 cls_cursor_init(&cursor, &table->cls, NULL);
1188 CLS_CURSOR_FOR_EACH_SAFE (rule, next_rule, up.cr, &cursor) {
1189 ofproto_rule_destroy(&rule->up);
1193 for (i = 0; i < MAX_MIRRORS; i++) {
1194 mirror_destroy(ofproto->mirrors[i]);
1197 netflow_destroy(ofproto->netflow);
1198 dpif_sflow_destroy(ofproto->sflow);
1199 hmap_destroy(&ofproto->bundles);
1200 mac_learning_destroy(ofproto->ml);
1202 classifier_destroy(&ofproto->facets);
1204 hmap_destroy(&ofproto->vlandev_map);
1205 hmap_destroy(&ofproto->realdev_vid_map);
1207 sset_destroy(&ofproto->ports);
1208 sset_destroy(&ofproto->ghost_ports);
1209 sset_destroy(&ofproto->port_poll_set);
1211 close_dpif_backer(ofproto->backer);
1215 run_fast(struct ofproto *ofproto_)
1217 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1218 struct ofport_dpif *ofport;
1220 /* Do not perform any periodic activity required by 'ofproto' while
1221 * waiting for flow restore to complete. */
1222 if (ofproto_get_flow_restore_wait()) {
1226 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1227 port_run_fast(ofport);
1234 run(struct ofproto *ofproto_)
1236 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1237 struct ofport_dpif *ofport;
1238 struct ofbundle *bundle;
1242 complete_operations(ofproto);
1245 /* Do not perform any periodic activity below required by 'ofproto' while
1246 * waiting for flow restore to complete. */
1247 if (ofproto_get_flow_restore_wait()) {
1251 error = run_fast(ofproto_);
1256 if (ofproto->netflow) {
1257 if (netflow_run(ofproto->netflow)) {
1258 send_netflow_active_timeouts(ofproto);
1261 if (ofproto->sflow) {
1262 dpif_sflow_run(ofproto->sflow);
1265 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1268 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1273 mac_learning_run(ofproto->ml, &ofproto->backer->revalidate_set);
1275 /* Check the consistency of a random facet, to aid debugging. */
1276 if (time_msec() >= ofproto->consistency_rl
1277 && !classifier_is_empty(&ofproto->facets)
1278 && !ofproto->backer->need_revalidate) {
1279 struct cls_table *table;
1280 struct cls_rule *cr;
1281 struct facet *facet;
1283 ofproto->consistency_rl = time_msec() + 250;
1285 table = CONTAINER_OF(hmap_random_node(&ofproto->facets.tables),
1286 struct cls_table, hmap_node);
1287 cr = CONTAINER_OF(hmap_random_node(&table->rules), struct cls_rule,
1289 facet = CONTAINER_OF(cr, struct facet, cr);
1291 if (!tag_set_intersects(&ofproto->backer->revalidate_set,
1292 facet->xout.tags)) {
1293 if (!facet_check_consistency(facet)) {
1294 ofproto->backer->need_revalidate = REV_INCONSISTENCY;
1303 wait(struct ofproto *ofproto_)
1305 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1306 struct ofport_dpif *ofport;
1307 struct ofbundle *bundle;
1309 if (!clogged && !list_is_empty(&ofproto->completions)) {
1310 poll_immediate_wake();
1313 if (ofproto_get_flow_restore_wait()) {
1317 dpif_wait(ofproto->backer->dpif);
1318 dpif_recv_wait(ofproto->backer->dpif);
1319 if (ofproto->sflow) {
1320 dpif_sflow_wait(ofproto->sflow);
1322 if (!tag_set_is_empty(&ofproto->backer->revalidate_set)) {
1323 poll_immediate_wake();
1325 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1328 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
1329 bundle_wait(bundle);
1331 if (ofproto->netflow) {
1332 netflow_wait(ofproto->netflow);
1334 mac_learning_wait(ofproto->ml);
1336 if (ofproto->backer->need_revalidate) {
1337 /* Shouldn't happen, but if it does just go around again. */
1338 VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
1339 poll_immediate_wake();
1344 get_memory_usage(const struct ofproto *ofproto_, struct simap *usage)
1346 const struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1347 struct cls_cursor cursor;
1348 size_t n_subfacets = 0;
1349 struct facet *facet;
1351 simap_increase(usage, "facets", classifier_count(&ofproto->facets));
1353 cls_cursor_init(&cursor, &ofproto->facets, NULL);
1354 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
1355 n_subfacets += list_size(&facet->subfacets);
1357 simap_increase(usage, "subfacets", n_subfacets);
1361 flush(struct ofproto *ofproto_)
1363 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1364 struct subfacet *subfacet, *next_subfacet;
1365 struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
1369 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
1370 &ofproto->backer->subfacets) {
1371 if (ofproto_dpif_cast(subfacet->facet->rule->up.ofproto) != ofproto) {
1375 if (subfacet->path != SF_NOT_INSTALLED) {
1376 batch[n_batch++] = subfacet;
1377 if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
1378 subfacet_destroy_batch(ofproto->backer, batch, n_batch);
1382 subfacet_destroy(subfacet);
1387 subfacet_destroy_batch(ofproto->backer, batch, n_batch);
1392 get_features(struct ofproto *ofproto_ OVS_UNUSED,
1393 bool *arp_match_ip, enum ofputil_action_bitmap *actions)
1395 *arp_match_ip = true;
1396 *actions = (OFPUTIL_A_OUTPUT |
1397 OFPUTIL_A_SET_VLAN_VID |
1398 OFPUTIL_A_SET_VLAN_PCP |
1399 OFPUTIL_A_STRIP_VLAN |
1400 OFPUTIL_A_SET_DL_SRC |
1401 OFPUTIL_A_SET_DL_DST |
1402 OFPUTIL_A_SET_NW_SRC |
1403 OFPUTIL_A_SET_NW_DST |
1404 OFPUTIL_A_SET_NW_TOS |
1405 OFPUTIL_A_SET_TP_SRC |
1406 OFPUTIL_A_SET_TP_DST |
1411 get_tables(struct ofproto *ofproto_, struct ofp12_table_stats *ots)
1413 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1414 struct dpif_dp_stats s;
1415 uint64_t n_miss, n_no_pkt_in, n_bytes, n_dropped_frags;
1418 strcpy(ots->name, "classifier");
1420 dpif_get_dp_stats(ofproto->backer->dpif, &s);
1421 rule_get_stats(&ofproto->miss_rule->up, &n_miss, &n_bytes);
1422 rule_get_stats(&ofproto->no_packet_in_rule->up, &n_no_pkt_in, &n_bytes);
1423 rule_get_stats(&ofproto->drop_frags_rule->up, &n_dropped_frags, &n_bytes);
1425 n_lookup = s.n_hit + s.n_missed - n_dropped_frags;
1426 ots->lookup_count = htonll(n_lookup);
1427 ots->matched_count = htonll(n_lookup - n_miss - n_no_pkt_in);
1430 static struct ofport *
1433 struct ofport_dpif *port = xmalloc(sizeof *port);
1438 port_dealloc(struct ofport *port_)
1440 struct ofport_dpif *port = ofport_dpif_cast(port_);
1445 port_construct(struct ofport *port_)
1447 struct ofport_dpif *port = ofport_dpif_cast(port_);
1448 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1449 const struct netdev *netdev = port->up.netdev;
1450 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1451 struct dpif_port dpif_port;
1454 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1455 port->bundle = NULL;
1458 port->tag = tag_create_random();
1459 port->may_enable = true;
1460 port->stp_port = NULL;
1461 port->stp_state = STP_DISABLED;
1462 port->tnl_port = NULL;
1464 hmap_init(&port->priorities);
1465 port->realdev_ofp_port = 0;
1466 port->vlandev_vid = 0;
1467 port->carrier_seq = netdev_get_carrier_resets(netdev);
1469 if (netdev_vport_is_patch(netdev)) {
1470 /* By bailing out here, we don't submit the port to the sFlow module
1471 * to be considered for counter polling export. This is correct
1472 * because the patch port represents an interface that sFlow considers
1473 * to be "internal" to the switch as a whole, and therefore not an
1474 * candidate for counter polling. */
1475 port->odp_port = ODPP_NONE;
1476 ofport_update_peer(port);
1480 error = dpif_port_query_by_name(ofproto->backer->dpif,
1481 netdev_vport_get_dpif_port(netdev, namebuf,
1488 port->odp_port = dpif_port.port_no;
1490 if (netdev_get_tunnel_config(netdev)) {
1491 port->tnl_port = tnl_port_add(&port->up, port->odp_port);
1493 /* Sanity-check that a mapping doesn't already exist. This
1494 * shouldn't happen for non-tunnel ports. */
1495 if (odp_port_to_ofp_port(ofproto, port->odp_port) != OFPP_NONE) {
1496 VLOG_ERR("port %s already has an OpenFlow port number",
1498 dpif_port_destroy(&dpif_port);
1502 hmap_insert(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node,
1503 hash_odp_port(port->odp_port));
1505 dpif_port_destroy(&dpif_port);
1507 if (ofproto->sflow) {
1508 dpif_sflow_add_port(ofproto->sflow, port_, port->odp_port);
1515 port_destruct(struct ofport *port_)
1517 struct ofport_dpif *port = ofport_dpif_cast(port_);
1518 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1519 const char *devname = netdev_get_name(port->up.netdev);
1520 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
1521 const char *dp_port_name;
1523 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1525 dp_port_name = netdev_vport_get_dpif_port(port->up.netdev, namebuf,
1527 if (dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
1528 /* The underlying device is still there, so delete it. This
1529 * happens when the ofproto is being destroyed, since the caller
1530 * assumes that removal of attached ports will happen as part of
1532 if (!port->tnl_port) {
1533 dpif_port_del(ofproto->backer->dpif, port->odp_port);
1538 port->peer->peer = NULL;
1542 if (port->odp_port != ODPP_NONE && !port->tnl_port) {
1543 hmap_remove(&ofproto->backer->odp_to_ofport_map, &port->odp_port_node);
1546 tnl_port_del(port->tnl_port);
1547 sset_find_and_delete(&ofproto->ports, devname);
1548 sset_find_and_delete(&ofproto->ghost_ports, devname);
1549 bundle_remove(port_);
1550 set_cfm(port_, NULL);
1551 set_bfd(port_, NULL);
1552 if (ofproto->sflow) {
1553 dpif_sflow_del_port(ofproto->sflow, port->odp_port);
1556 ofport_clear_priorities(port);
1557 hmap_destroy(&port->priorities);
1561 port_modified(struct ofport *port_)
1563 struct ofport_dpif *port = ofport_dpif_cast(port_);
1565 if (port->bundle && port->bundle->bond) {
1566 bond_slave_set_netdev(port->bundle->bond, port, port->up.netdev);
1570 cfm_set_netdev(port->cfm, port->up.netdev);
1573 if (port->tnl_port && tnl_port_reconfigure(&port->up, port->odp_port,
1575 ofproto_dpif_cast(port->up.ofproto)->backer->need_revalidate =
1579 ofport_update_peer(port);
1583 port_reconfigured(struct ofport *port_, enum ofputil_port_config old_config)
1585 struct ofport_dpif *port = ofport_dpif_cast(port_);
1586 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
1587 enum ofputil_port_config changed = old_config ^ port->up.pp.config;
1589 if (changed & (OFPUTIL_PC_NO_RECV | OFPUTIL_PC_NO_RECV_STP |
1590 OFPUTIL_PC_NO_FWD | OFPUTIL_PC_NO_FLOOD |
1591 OFPUTIL_PC_NO_PACKET_IN)) {
1592 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1594 if (changed & OFPUTIL_PC_NO_FLOOD && port->bundle) {
1595 bundle_update(port->bundle);
1601 set_sflow(struct ofproto *ofproto_,
1602 const struct ofproto_sflow_options *sflow_options)
1604 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1605 struct dpif_sflow *ds = ofproto->sflow;
1607 if (sflow_options) {
1609 struct ofport_dpif *ofport;
1611 ds = ofproto->sflow = dpif_sflow_create();
1612 HMAP_FOR_EACH (ofport, up.hmap_node, &ofproto->up.ports) {
1613 dpif_sflow_add_port(ds, &ofport->up, ofport->odp_port);
1615 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1617 dpif_sflow_set_options(ds, sflow_options);
1620 dpif_sflow_destroy(ds);
1621 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1622 ofproto->sflow = NULL;
1630 struct ofproto *ofproto_,
1631 const struct ofproto_ipfix_bridge_exporter_options *bridge_exporter_options,
1632 const struct ofproto_ipfix_flow_exporter_options *flow_exporters_options,
1633 size_t n_flow_exporters_options)
1635 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1636 struct dpif_ipfix *di = ofproto->ipfix;
1638 if (bridge_exporter_options || flow_exporters_options) {
1640 di = ofproto->ipfix = dpif_ipfix_create();
1642 dpif_ipfix_set_options(
1643 di, bridge_exporter_options, flow_exporters_options,
1644 n_flow_exporters_options);
1647 dpif_ipfix_destroy(di);
1648 ofproto->ipfix = NULL;
1655 set_cfm(struct ofport *ofport_, const struct cfm_settings *s)
1657 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1664 struct ofproto_dpif *ofproto;
1666 ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1667 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1668 ofport->cfm = cfm_create(ofport->up.netdev);
1671 if (cfm_configure(ofport->cfm, s)) {
1677 cfm_destroy(ofport->cfm);
1683 get_cfm_status(const struct ofport *ofport_,
1684 struct ofproto_cfm_status *status)
1686 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1689 status->faults = cfm_get_fault(ofport->cfm);
1690 status->remote_opstate = cfm_get_opup(ofport->cfm);
1691 status->health = cfm_get_health(ofport->cfm);
1692 cfm_get_remote_mpids(ofport->cfm, &status->rmps, &status->n_rmps);
1700 set_bfd(struct ofport *ofport_, const struct smap *cfg)
1702 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
1703 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1707 ofport->bfd = bfd_configure(old, netdev_get_name(ofport->up.netdev), cfg);
1708 if (ofport->bfd != old) {
1709 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1716 get_bfd_status(struct ofport *ofport_, struct smap *smap)
1718 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1721 bfd_get_status(ofport->bfd, smap);
1728 /* Spanning Tree. */
1731 send_bpdu_cb(struct ofpbuf *pkt, int port_num, void *ofproto_)
1733 struct ofproto_dpif *ofproto = ofproto_;
1734 struct stp_port *sp = stp_get_port(ofproto->stp, port_num);
1735 struct ofport_dpif *ofport;
1737 ofport = stp_port_get_aux(sp);
1739 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on unknown port %d",
1740 ofproto->up.name, port_num);
1742 struct eth_header *eth = pkt->l2;
1744 netdev_get_etheraddr(ofport->up.netdev, eth->eth_src);
1745 if (eth_addr_is_zero(eth->eth_src)) {
1746 VLOG_WARN_RL(&rl, "%s: cannot send BPDU on port %d "
1747 "with unknown MAC", ofproto->up.name, port_num);
1749 send_packet(ofport, pkt);
1755 /* Configures STP on 'ofproto_' using the settings defined in 's'. */
1757 set_stp(struct ofproto *ofproto_, const struct ofproto_stp_settings *s)
1759 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1761 /* Only revalidate flows if the configuration changed. */
1762 if (!s != !ofproto->stp) {
1763 ofproto->backer->need_revalidate = REV_RECONFIGURE;
1767 if (!ofproto->stp) {
1768 ofproto->stp = stp_create(ofproto_->name, s->system_id,
1769 send_bpdu_cb, ofproto);
1770 ofproto->stp_last_tick = time_msec();
1773 stp_set_bridge_id(ofproto->stp, s->system_id);
1774 stp_set_bridge_priority(ofproto->stp, s->priority);
1775 stp_set_hello_time(ofproto->stp, s->hello_time);
1776 stp_set_max_age(ofproto->stp, s->max_age);
1777 stp_set_forward_delay(ofproto->stp, s->fwd_delay);
1779 struct ofport *ofport;
1781 HMAP_FOR_EACH (ofport, hmap_node, &ofproto->up.ports) {
1782 set_stp_port(ofport, NULL);
1785 stp_destroy(ofproto->stp);
1786 ofproto->stp = NULL;
1793 get_stp_status(struct ofproto *ofproto_, struct ofproto_stp_status *s)
1795 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
1799 s->bridge_id = stp_get_bridge_id(ofproto->stp);
1800 s->designated_root = stp_get_designated_root(ofproto->stp);
1801 s->root_path_cost = stp_get_root_path_cost(ofproto->stp);
1810 update_stp_port_state(struct ofport_dpif *ofport)
1812 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1813 enum stp_state state;
1815 /* Figure out new state. */
1816 state = ofport->stp_port ? stp_port_get_state(ofport->stp_port)
1820 if (ofport->stp_state != state) {
1821 enum ofputil_port_state of_state;
1824 VLOG_DBG_RL(&rl, "port %s: STP state changed from %s to %s",
1825 netdev_get_name(ofport->up.netdev),
1826 stp_state_name(ofport->stp_state),
1827 stp_state_name(state));
1828 if (stp_learn_in_state(ofport->stp_state)
1829 != stp_learn_in_state(state)) {
1830 /* xxx Learning action flows should also be flushed. */
1831 mac_learning_flush(ofproto->ml,
1832 &ofproto->backer->revalidate_set);
1834 fwd_change = stp_forward_in_state(ofport->stp_state)
1835 != stp_forward_in_state(state);
1837 ofproto->backer->need_revalidate = REV_STP;
1838 ofport->stp_state = state;
1839 ofport->stp_state_entered = time_msec();
1841 if (fwd_change && ofport->bundle) {
1842 bundle_update(ofport->bundle);
1845 /* Update the STP state bits in the OpenFlow port description. */
1846 of_state = ofport->up.pp.state & ~OFPUTIL_PS_STP_MASK;
1847 of_state |= (state == STP_LISTENING ? OFPUTIL_PS_STP_LISTEN
1848 : state == STP_LEARNING ? OFPUTIL_PS_STP_LEARN
1849 : state == STP_FORWARDING ? OFPUTIL_PS_STP_FORWARD
1850 : state == STP_BLOCKING ? OFPUTIL_PS_STP_BLOCK
1852 ofproto_port_set_state(&ofport->up, of_state);
1856 /* Configures STP on 'ofport_' using the settings defined in 's'. The
1857 * caller is responsible for assigning STP port numbers and ensuring
1858 * there are no duplicates. */
1860 set_stp_port(struct ofport *ofport_,
1861 const struct ofproto_port_stp_settings *s)
1863 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1864 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1865 struct stp_port *sp = ofport->stp_port;
1867 if (!s || !s->enable) {
1869 ofport->stp_port = NULL;
1870 stp_port_disable(sp);
1871 update_stp_port_state(ofport);
1874 } else if (sp && stp_port_no(sp) != s->port_num
1875 && ofport == stp_port_get_aux(sp)) {
1876 /* The port-id changed, so disable the old one if it's not
1877 * already in use by another port. */
1878 stp_port_disable(sp);
1881 sp = ofport->stp_port = stp_get_port(ofproto->stp, s->port_num);
1882 stp_port_enable(sp);
1884 stp_port_set_aux(sp, ofport);
1885 stp_port_set_priority(sp, s->priority);
1886 stp_port_set_path_cost(sp, s->path_cost);
1888 update_stp_port_state(ofport);
1894 get_stp_port_status(struct ofport *ofport_,
1895 struct ofproto_port_stp_status *s)
1897 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
1898 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
1899 struct stp_port *sp = ofport->stp_port;
1901 if (!ofproto->stp || !sp) {
1907 s->port_id = stp_port_get_id(sp);
1908 s->state = stp_port_get_state(sp);
1909 s->sec_in_state = (time_msec() - ofport->stp_state_entered) / 1000;
1910 s->role = stp_port_get_role(sp);
1911 stp_port_get_counts(sp, &s->tx_count, &s->rx_count, &s->error_count);
1917 stp_run(struct ofproto_dpif *ofproto)
1920 long long int now = time_msec();
1921 long long int elapsed = now - ofproto->stp_last_tick;
1922 struct stp_port *sp;
1925 stp_tick(ofproto->stp, MIN(INT_MAX, elapsed));
1926 ofproto->stp_last_tick = now;
1928 while (stp_get_changed_port(ofproto->stp, &sp)) {
1929 struct ofport_dpif *ofport = stp_port_get_aux(sp);
1932 update_stp_port_state(ofport);
1936 if (stp_check_and_reset_fdb_flush(ofproto->stp)) {
1937 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
1943 stp_wait(struct ofproto_dpif *ofproto)
1946 poll_timer_wait(1000);
1950 /* Returns true if STP should process 'flow'. Sets fields in 'wc' that
1951 * were used to make the determination.*/
1953 stp_should_process_flow(const struct flow *flow, struct flow_wildcards *wc)
1955 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1956 return eth_addr_equals(flow->dl_dst, eth_addr_stp);
1960 stp_process_packet(const struct ofport_dpif *ofport,
1961 const struct ofpbuf *packet)
1963 struct ofpbuf payload = *packet;
1964 struct eth_header *eth = payload.data;
1965 struct stp_port *sp = ofport->stp_port;
1967 /* Sink packets on ports that have STP disabled when the bridge has
1969 if (!sp || stp_port_get_state(sp) == STP_DISABLED) {
1973 /* Trim off padding on payload. */
1974 if (payload.size > ntohs(eth->eth_type) + ETH_HEADER_LEN) {
1975 payload.size = ntohs(eth->eth_type) + ETH_HEADER_LEN;
1978 if (ofpbuf_try_pull(&payload, ETH_HEADER_LEN + LLC_HEADER_LEN)) {
1979 stp_received_bpdu(sp, payload.data, payload.size);
1984 ofproto_dpif_queue_to_priority(const struct ofproto_dpif *ofproto,
1985 uint32_t queue_id, uint32_t *priority)
1987 return dpif_queue_to_priority(ofproto->backer->dpif, queue_id, priority);
1990 static struct priority_to_dscp *
1991 get_priority(const struct ofport_dpif *ofport, uint32_t priority)
1993 struct priority_to_dscp *pdscp;
1996 hash = hash_int(priority, 0);
1997 HMAP_FOR_EACH_IN_BUCKET (pdscp, hmap_node, hash, &ofport->priorities) {
1998 if (pdscp->priority == priority) {
2006 ofproto_dpif_dscp_from_priority(const struct ofport_dpif *ofport,
2007 uint32_t priority, uint8_t *dscp)
2009 struct priority_to_dscp *pdscp = get_priority(ofport, priority);
2010 *dscp = pdscp ? pdscp->dscp : 0;
2011 return pdscp != NULL;
2015 ofport_clear_priorities(struct ofport_dpif *ofport)
2017 struct priority_to_dscp *pdscp, *next;
2019 HMAP_FOR_EACH_SAFE (pdscp, next, hmap_node, &ofport->priorities) {
2020 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
2026 set_queues(struct ofport *ofport_,
2027 const struct ofproto_port_queue *qdscp_list,
2030 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
2031 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2032 struct hmap new = HMAP_INITIALIZER(&new);
2035 for (i = 0; i < n_qdscp; i++) {
2036 struct priority_to_dscp *pdscp;
2040 dscp = (qdscp_list[i].dscp << 2) & IP_DSCP_MASK;
2041 if (dpif_queue_to_priority(ofproto->backer->dpif, qdscp_list[i].queue,
2046 pdscp = get_priority(ofport, priority);
2048 hmap_remove(&ofport->priorities, &pdscp->hmap_node);
2050 pdscp = xmalloc(sizeof *pdscp);
2051 pdscp->priority = priority;
2053 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2056 if (pdscp->dscp != dscp) {
2058 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2061 hmap_insert(&new, &pdscp->hmap_node, hash_int(pdscp->priority, 0));
2064 if (!hmap_is_empty(&ofport->priorities)) {
2065 ofport_clear_priorities(ofport);
2066 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2069 hmap_swap(&new, &ofport->priorities);
2077 /* Expires all MAC learning entries associated with 'bundle' and forces its
2078 * ofproto to revalidate every flow.
2080 * Normally MAC learning entries are removed only from the ofproto associated
2081 * with 'bundle', but if 'all_ofprotos' is true, then the MAC learning entries
2082 * are removed from every ofproto. When patch ports and SLB bonds are in use
2083 * and a VM migration happens and the gratuitous ARPs are somehow lost, this
2084 * avoids a MAC_ENTRY_IDLE_TIME delay before the migrated VM can communicate
2085 * with the host from which it migrated. */
2087 bundle_flush_macs(struct ofbundle *bundle, bool all_ofprotos)
2089 struct ofproto_dpif *ofproto = bundle->ofproto;
2090 struct mac_learning *ml = ofproto->ml;
2091 struct mac_entry *mac, *next_mac;
2093 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2094 LIST_FOR_EACH_SAFE (mac, next_mac, lru_node, &ml->lrus) {
2095 if (mac->port.p == bundle) {
2097 struct ofproto_dpif *o;
2099 HMAP_FOR_EACH (o, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2101 struct mac_entry *e;
2103 e = mac_learning_lookup(o->ml, mac->mac, mac->vlan,
2106 mac_learning_expire(o->ml, e);
2112 mac_learning_expire(ml, mac);
2117 static struct ofbundle *
2118 bundle_lookup(const struct ofproto_dpif *ofproto, void *aux)
2120 struct ofbundle *bundle;
2122 HMAP_FOR_EACH_IN_BUCKET (bundle, hmap_node, hash_pointer(aux, 0),
2123 &ofproto->bundles) {
2124 if (bundle->aux == aux) {
2131 /* Looks up each of the 'n_auxes' pointers in 'auxes' as bundles and adds the
2132 * ones that are found to 'bundles'. */
2134 bundle_lookup_multiple(struct ofproto_dpif *ofproto,
2135 void **auxes, size_t n_auxes,
2136 struct hmapx *bundles)
2140 hmapx_init(bundles);
2141 for (i = 0; i < n_auxes; i++) {
2142 struct ofbundle *bundle = bundle_lookup(ofproto, auxes[i]);
2144 hmapx_add(bundles, bundle);
2150 bundle_update(struct ofbundle *bundle)
2152 struct ofport_dpif *port;
2154 bundle->floodable = true;
2155 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2156 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2157 || !stp_forward_in_state(port->stp_state)) {
2158 bundle->floodable = false;
2165 bundle_del_port(struct ofport_dpif *port)
2167 struct ofbundle *bundle = port->bundle;
2169 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2171 list_remove(&port->bundle_node);
2172 port->bundle = NULL;
2175 lacp_slave_unregister(bundle->lacp, port);
2178 bond_slave_unregister(bundle->bond, port);
2181 bundle_update(bundle);
2185 bundle_add_port(struct ofbundle *bundle, ofp_port_t ofp_port,
2186 struct lacp_slave_settings *lacp)
2188 struct ofport_dpif *port;
2190 port = get_ofp_port(bundle->ofproto, ofp_port);
2195 if (port->bundle != bundle) {
2196 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2198 bundle_del_port(port);
2201 port->bundle = bundle;
2202 list_push_back(&bundle->ports, &port->bundle_node);
2203 if (port->up.pp.config & OFPUTIL_PC_NO_FLOOD
2204 || !stp_forward_in_state(port->stp_state)) {
2205 bundle->floodable = false;
2209 bundle->ofproto->backer->need_revalidate = REV_RECONFIGURE;
2210 lacp_slave_register(bundle->lacp, port, lacp);
2217 bundle_destroy(struct ofbundle *bundle)
2219 struct ofproto_dpif *ofproto;
2220 struct ofport_dpif *port, *next_port;
2227 ofproto = bundle->ofproto;
2228 for (i = 0; i < MAX_MIRRORS; i++) {
2229 struct ofmirror *m = ofproto->mirrors[i];
2231 if (m->out == bundle) {
2233 } else if (hmapx_find_and_delete(&m->srcs, bundle)
2234 || hmapx_find_and_delete(&m->dsts, bundle)) {
2235 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2240 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2241 bundle_del_port(port);
2244 bundle_flush_macs(bundle, true);
2245 hmap_remove(&ofproto->bundles, &bundle->hmap_node);
2247 free(bundle->trunks);
2248 lacp_destroy(bundle->lacp);
2249 bond_destroy(bundle->bond);
2254 bundle_set(struct ofproto *ofproto_, void *aux,
2255 const struct ofproto_bundle_settings *s)
2257 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2258 bool need_flush = false;
2259 struct ofport_dpif *port;
2260 struct ofbundle *bundle;
2261 unsigned long *trunks;
2267 bundle_destroy(bundle_lookup(ofproto, aux));
2271 ovs_assert(s->n_slaves == 1 || s->bond != NULL);
2272 ovs_assert((s->lacp != NULL) == (s->lacp_slaves != NULL));
2274 bundle = bundle_lookup(ofproto, aux);
2276 bundle = xmalloc(sizeof *bundle);
2278 bundle->ofproto = ofproto;
2279 hmap_insert(&ofproto->bundles, &bundle->hmap_node,
2280 hash_pointer(aux, 0));
2282 bundle->name = NULL;
2284 list_init(&bundle->ports);
2285 bundle->vlan_mode = PORT_VLAN_TRUNK;
2287 bundle->trunks = NULL;
2288 bundle->use_priority_tags = s->use_priority_tags;
2289 bundle->lacp = NULL;
2290 bundle->bond = NULL;
2292 bundle->floodable = true;
2294 bundle->src_mirrors = 0;
2295 bundle->dst_mirrors = 0;
2296 bundle->mirror_out = 0;
2299 if (!bundle->name || strcmp(s->name, bundle->name)) {
2301 bundle->name = xstrdup(s->name);
2306 if (!bundle->lacp) {
2307 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2308 bundle->lacp = lacp_create();
2310 lacp_configure(bundle->lacp, s->lacp);
2312 lacp_destroy(bundle->lacp);
2313 bundle->lacp = NULL;
2316 /* Update set of ports. */
2318 for (i = 0; i < s->n_slaves; i++) {
2319 if (!bundle_add_port(bundle, s->slaves[i],
2320 s->lacp ? &s->lacp_slaves[i] : NULL)) {
2324 if (!ok || list_size(&bundle->ports) != s->n_slaves) {
2325 struct ofport_dpif *next_port;
2327 LIST_FOR_EACH_SAFE (port, next_port, bundle_node, &bundle->ports) {
2328 for (i = 0; i < s->n_slaves; i++) {
2329 if (s->slaves[i] == port->up.ofp_port) {
2334 bundle_del_port(port);
2338 ovs_assert(list_size(&bundle->ports) <= s->n_slaves);
2340 if (list_is_empty(&bundle->ports)) {
2341 bundle_destroy(bundle);
2345 /* Set VLAN tagging mode */
2346 if (s->vlan_mode != bundle->vlan_mode
2347 || s->use_priority_tags != bundle->use_priority_tags) {
2348 bundle->vlan_mode = s->vlan_mode;
2349 bundle->use_priority_tags = s->use_priority_tags;
2354 vlan = (s->vlan_mode == PORT_VLAN_TRUNK ? -1
2355 : s->vlan >= 0 && s->vlan <= 4095 ? s->vlan
2357 if (vlan != bundle->vlan) {
2358 bundle->vlan = vlan;
2362 /* Get trunked VLANs. */
2363 switch (s->vlan_mode) {
2364 case PORT_VLAN_ACCESS:
2368 case PORT_VLAN_TRUNK:
2369 trunks = CONST_CAST(unsigned long *, s->trunks);
2372 case PORT_VLAN_NATIVE_UNTAGGED:
2373 case PORT_VLAN_NATIVE_TAGGED:
2374 if (vlan != 0 && (!s->trunks
2375 || !bitmap_is_set(s->trunks, vlan)
2376 || bitmap_is_set(s->trunks, 0))) {
2377 /* Force trunking the native VLAN and prohibit trunking VLAN 0. */
2379 trunks = bitmap_clone(s->trunks, 4096);
2381 trunks = bitmap_allocate1(4096);
2383 bitmap_set1(trunks, vlan);
2384 bitmap_set0(trunks, 0);
2386 trunks = CONST_CAST(unsigned long *, s->trunks);
2393 if (!vlan_bitmap_equal(trunks, bundle->trunks)) {
2394 free(bundle->trunks);
2395 if (trunks == s->trunks) {
2396 bundle->trunks = vlan_bitmap_clone(trunks);
2398 bundle->trunks = trunks;
2403 if (trunks != s->trunks) {
2408 if (!list_is_short(&bundle->ports)) {
2409 bundle->ofproto->has_bonded_bundles = true;
2411 if (bond_reconfigure(bundle->bond, s->bond)) {
2412 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2415 bundle->bond = bond_create(s->bond);
2416 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2419 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2420 bond_slave_register(bundle->bond, port, port->up.netdev);
2423 bond_destroy(bundle->bond);
2424 bundle->bond = NULL;
2427 /* If we changed something that would affect MAC learning, un-learn
2428 * everything on this port and force flow revalidation. */
2430 bundle_flush_macs(bundle, false);
2437 bundle_remove(struct ofport *port_)
2439 struct ofport_dpif *port = ofport_dpif_cast(port_);
2440 struct ofbundle *bundle = port->bundle;
2443 bundle_del_port(port);
2444 if (list_is_empty(&bundle->ports)) {
2445 bundle_destroy(bundle);
2446 } else if (list_is_short(&bundle->ports)) {
2447 bond_destroy(bundle->bond);
2448 bundle->bond = NULL;
2454 send_pdu_cb(void *port_, const void *pdu, size_t pdu_size)
2456 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
2457 struct ofport_dpif *port = port_;
2458 uint8_t ea[ETH_ADDR_LEN];
2461 error = netdev_get_etheraddr(port->up.netdev, ea);
2463 struct ofpbuf packet;
2466 ofpbuf_init(&packet, 0);
2467 packet_pdu = eth_compose(&packet, eth_addr_lacp, ea, ETH_TYPE_LACP,
2469 memcpy(packet_pdu, pdu, pdu_size);
2471 send_packet(port, &packet);
2472 ofpbuf_uninit(&packet);
2474 VLOG_ERR_RL(&rl, "port %s: cannot obtain Ethernet address of iface "
2475 "%s (%s)", port->bundle->name,
2476 netdev_get_name(port->up.netdev), strerror(error));
2481 bundle_send_learning_packets(struct ofbundle *bundle)
2483 struct ofproto_dpif *ofproto = bundle->ofproto;
2484 int error, n_packets, n_errors;
2485 struct mac_entry *e;
2487 error = n_packets = n_errors = 0;
2488 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
2489 if (e->port.p != bundle) {
2490 struct ofpbuf *learning_packet;
2491 struct ofport_dpif *port;
2495 /* The assignment to "port" is unnecessary but makes "grep"ing for
2496 * struct ofport_dpif more effective. */
2497 learning_packet = bond_compose_learning_packet(bundle->bond,
2501 ret = send_packet(port, learning_packet);
2502 ofpbuf_delete(learning_packet);
2512 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2513 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
2514 "packets, last error was: %s",
2515 bundle->name, n_errors, n_packets, strerror(error));
2517 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
2518 bundle->name, n_packets);
2523 bundle_run(struct ofbundle *bundle)
2526 lacp_run(bundle->lacp, send_pdu_cb);
2529 struct ofport_dpif *port;
2531 LIST_FOR_EACH (port, bundle_node, &bundle->ports) {
2532 bond_slave_set_may_enable(bundle->bond, port, port->may_enable);
2535 bond_run(bundle->bond, &bundle->ofproto->backer->revalidate_set,
2536 lacp_status(bundle->lacp));
2537 if (bond_should_send_learning_packets(bundle->bond)) {
2538 bundle_send_learning_packets(bundle);
2544 bundle_wait(struct ofbundle *bundle)
2547 lacp_wait(bundle->lacp);
2550 bond_wait(bundle->bond);
2557 mirror_scan(struct ofproto_dpif *ofproto)
2561 for (idx = 0; idx < MAX_MIRRORS; idx++) {
2562 if (!ofproto->mirrors[idx]) {
2569 static struct ofmirror *
2570 mirror_lookup(struct ofproto_dpif *ofproto, void *aux)
2574 for (i = 0; i < MAX_MIRRORS; i++) {
2575 struct ofmirror *mirror = ofproto->mirrors[i];
2576 if (mirror && mirror->aux == aux) {
2584 /* Update the 'dup_mirrors' member of each of the ofmirrors in 'ofproto'. */
2586 mirror_update_dups(struct ofproto_dpif *ofproto)
2590 for (i = 0; i < MAX_MIRRORS; i++) {
2591 struct ofmirror *m = ofproto->mirrors[i];
2594 m->dup_mirrors = MIRROR_MASK_C(1) << i;
2598 for (i = 0; i < MAX_MIRRORS; i++) {
2599 struct ofmirror *m1 = ofproto->mirrors[i];
2606 for (j = i + 1; j < MAX_MIRRORS; j++) {
2607 struct ofmirror *m2 = ofproto->mirrors[j];
2609 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
2610 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
2611 m2->dup_mirrors |= m1->dup_mirrors;
2618 mirror_set(struct ofproto *ofproto_, void *aux,
2619 const struct ofproto_mirror_settings *s)
2621 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2622 mirror_mask_t mirror_bit;
2623 struct ofbundle *bundle;
2624 struct ofmirror *mirror;
2625 struct ofbundle *out;
2626 struct hmapx srcs; /* Contains "struct ofbundle *"s. */
2627 struct hmapx dsts; /* Contains "struct ofbundle *"s. */
2630 mirror = mirror_lookup(ofproto, aux);
2632 mirror_destroy(mirror);
2638 idx = mirror_scan(ofproto);
2640 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
2642 ofproto->up.name, MAX_MIRRORS, s->name);
2646 mirror = ofproto->mirrors[idx] = xzalloc(sizeof *mirror);
2647 mirror->ofproto = ofproto;
2650 mirror->out_vlan = -1;
2651 mirror->name = NULL;
2654 if (!mirror->name || strcmp(s->name, mirror->name)) {
2656 mirror->name = xstrdup(s->name);
2659 /* Get the new configuration. */
2660 if (s->out_bundle) {
2661 out = bundle_lookup(ofproto, s->out_bundle);
2663 mirror_destroy(mirror);
2669 out_vlan = s->out_vlan;
2671 bundle_lookup_multiple(ofproto, s->srcs, s->n_srcs, &srcs);
2672 bundle_lookup_multiple(ofproto, s->dsts, s->n_dsts, &dsts);
2674 /* If the configuration has not changed, do nothing. */
2675 if (hmapx_equals(&srcs, &mirror->srcs)
2676 && hmapx_equals(&dsts, &mirror->dsts)
2677 && vlan_bitmap_equal(mirror->vlans, s->src_vlans)
2678 && mirror->out == out
2679 && mirror->out_vlan == out_vlan)
2681 hmapx_destroy(&srcs);
2682 hmapx_destroy(&dsts);
2686 hmapx_swap(&srcs, &mirror->srcs);
2687 hmapx_destroy(&srcs);
2689 hmapx_swap(&dsts, &mirror->dsts);
2690 hmapx_destroy(&dsts);
2692 free(mirror->vlans);
2693 mirror->vlans = vlan_bitmap_clone(s->src_vlans);
2696 mirror->out_vlan = out_vlan;
2698 /* Update bundles. */
2699 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2700 HMAP_FOR_EACH (bundle, hmap_node, &mirror->ofproto->bundles) {
2701 if (hmapx_contains(&mirror->srcs, bundle)) {
2702 bundle->src_mirrors |= mirror_bit;
2704 bundle->src_mirrors &= ~mirror_bit;
2707 if (hmapx_contains(&mirror->dsts, bundle)) {
2708 bundle->dst_mirrors |= mirror_bit;
2710 bundle->dst_mirrors &= ~mirror_bit;
2713 if (mirror->out == bundle) {
2714 bundle->mirror_out |= mirror_bit;
2716 bundle->mirror_out &= ~mirror_bit;
2720 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2721 ofproto->has_mirrors = true;
2722 mac_learning_flush(ofproto->ml,
2723 &ofproto->backer->revalidate_set);
2724 mirror_update_dups(ofproto);
2730 mirror_destroy(struct ofmirror *mirror)
2732 struct ofproto_dpif *ofproto;
2733 mirror_mask_t mirror_bit;
2734 struct ofbundle *bundle;
2741 ofproto = mirror->ofproto;
2742 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2743 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
2745 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
2746 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
2747 bundle->src_mirrors &= ~mirror_bit;
2748 bundle->dst_mirrors &= ~mirror_bit;
2749 bundle->mirror_out &= ~mirror_bit;
2752 hmapx_destroy(&mirror->srcs);
2753 hmapx_destroy(&mirror->dsts);
2754 free(mirror->vlans);
2756 ofproto->mirrors[mirror->idx] = NULL;
2760 mirror_update_dups(ofproto);
2762 ofproto->has_mirrors = false;
2763 for (i = 0; i < MAX_MIRRORS; i++) {
2764 if (ofproto->mirrors[i]) {
2765 ofproto->has_mirrors = true;
2772 mirror_get_stats(struct ofproto *ofproto_, void *aux,
2773 uint64_t *packets, uint64_t *bytes)
2775 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2776 struct ofmirror *mirror = mirror_lookup(ofproto, aux);
2779 *packets = *bytes = UINT64_MAX;
2785 *packets = mirror->packet_count;
2786 *bytes = mirror->byte_count;
2792 set_flood_vlans(struct ofproto *ofproto_, unsigned long *flood_vlans)
2794 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2795 if (mac_learning_set_flood_vlans(ofproto->ml, flood_vlans)) {
2796 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
2802 is_mirror_output_bundle(const struct ofproto *ofproto_, void *aux)
2804 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2805 struct ofbundle *bundle = bundle_lookup(ofproto, aux);
2806 return bundle && bundle->mirror_out != 0;
2810 forward_bpdu_changed(struct ofproto *ofproto_)
2812 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2813 ofproto->backer->need_revalidate = REV_RECONFIGURE;
2817 set_mac_table_config(struct ofproto *ofproto_, unsigned int idle_time,
2820 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2821 mac_learning_set_idle_time(ofproto->ml, idle_time);
2822 mac_learning_set_max_entries(ofproto->ml, max_entries);
2827 struct ofport_dpif *
2828 get_ofp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
2830 struct ofport *ofport = ofproto_get_port(&ofproto->up, ofp_port);
2831 return ofport ? ofport_dpif_cast(ofport) : NULL;
2834 struct ofport_dpif *
2835 get_odp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
2837 struct ofport_dpif *port = odp_port_to_ofport(ofproto->backer, odp_port);
2838 return port && &ofproto->up == port->up.ofproto ? port : NULL;
2842 ofproto_port_from_dpif_port(struct ofproto_dpif *ofproto,
2843 struct ofproto_port *ofproto_port,
2844 struct dpif_port *dpif_port)
2846 ofproto_port->name = dpif_port->name;
2847 ofproto_port->type = dpif_port->type;
2848 ofproto_port->ofp_port = odp_port_to_ofp_port(ofproto, dpif_port->port_no);
2852 ofport_update_peer(struct ofport_dpif *ofport)
2854 const struct ofproto_dpif *ofproto;
2855 struct dpif_backer *backer;
2856 const char *peer_name;
2858 if (!netdev_vport_is_patch(ofport->up.netdev)) {
2862 backer = ofproto_dpif_cast(ofport->up.ofproto)->backer;
2863 backer->need_revalidate = REV_RECONFIGURE;
2866 ofport->peer->peer = NULL;
2867 ofport->peer = NULL;
2870 peer_name = netdev_vport_patch_peer(ofport->up.netdev);
2875 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
2876 struct ofport *peer_ofport;
2877 struct ofport_dpif *peer;
2878 const char *peer_peer;
2880 if (ofproto->backer != backer) {
2884 peer_ofport = shash_find_data(&ofproto->up.port_by_name, peer_name);
2889 peer = ofport_dpif_cast(peer_ofport);
2890 peer_peer = netdev_vport_patch_peer(peer->up.netdev);
2891 if (peer_peer && !strcmp(netdev_get_name(ofport->up.netdev),
2893 ofport->peer = peer;
2894 ofport->peer->peer = ofport;
2902 port_run_fast(struct ofport_dpif *ofport)
2904 if (ofport->cfm && cfm_should_send_ccm(ofport->cfm)) {
2905 struct ofpbuf packet;
2907 ofpbuf_init(&packet, 0);
2908 cfm_compose_ccm(ofport->cfm, &packet, ofport->up.pp.hw_addr);
2909 send_packet(ofport, &packet);
2910 ofpbuf_uninit(&packet);
2913 if (ofport->bfd && bfd_should_send_packet(ofport->bfd)) {
2914 struct ofpbuf packet;
2916 ofpbuf_init(&packet, 0);
2917 bfd_put_packet(ofport->bfd, &packet, ofport->up.pp.hw_addr);
2918 send_packet(ofport, &packet);
2919 ofpbuf_uninit(&packet);
2924 port_run(struct ofport_dpif *ofport)
2926 long long int carrier_seq = netdev_get_carrier_resets(ofport->up.netdev);
2927 bool carrier_changed = carrier_seq != ofport->carrier_seq;
2928 bool enable = netdev_get_carrier(ofport->up.netdev);
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 enable = enable && !cfm_get_fault(ofport->cfm);
2940 if (cfm_opup >= 0) {
2941 enable = enable && cfm_opup;
2946 bfd_run(ofport->bfd);
2947 enable = enable && bfd_forwarding(ofport->bfd);
2950 if (ofport->bundle) {
2951 enable = enable && lacp_slave_may_enable(ofport->bundle->lacp, ofport);
2952 if (carrier_changed) {
2953 lacp_slave_carrier_changed(ofport->bundle->lacp, ofport);
2957 if (ofport->may_enable != enable) {
2958 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
2960 if (ofproto->has_bundle_action) {
2961 ofproto->backer->need_revalidate = REV_PORT_TOGGLED;
2965 ofport->may_enable = enable;
2969 port_wait(struct ofport_dpif *ofport)
2972 cfm_wait(ofport->cfm);
2976 bfd_wait(ofport->bfd);
2981 port_query_by_name(const struct ofproto *ofproto_, const char *devname,
2982 struct ofproto_port *ofproto_port)
2984 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
2985 struct dpif_port dpif_port;
2988 if (sset_contains(&ofproto->ghost_ports, devname)) {
2989 const char *type = netdev_get_type_from_name(devname);
2991 /* We may be called before ofproto->up.port_by_name is populated with
2992 * the appropriate ofport. For this reason, we must get the name and
2993 * type from the netdev layer directly. */
2995 const struct ofport *ofport;
2997 ofport = shash_find_data(&ofproto->up.port_by_name, devname);
2998 ofproto_port->ofp_port = ofport ? ofport->ofp_port : OFPP_NONE;
2999 ofproto_port->name = xstrdup(devname);
3000 ofproto_port->type = xstrdup(type);
3006 if (!sset_contains(&ofproto->ports, devname)) {
3009 error = dpif_port_query_by_name(ofproto->backer->dpif,
3010 devname, &dpif_port);
3012 ofproto_port_from_dpif_port(ofproto, ofproto_port, &dpif_port);
3018 port_add(struct ofproto *ofproto_, struct netdev *netdev)
3020 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3021 const char *devname = netdev_get_name(netdev);
3022 char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
3023 const char *dp_port_name;
3025 if (netdev_vport_is_patch(netdev)) {
3026 sset_add(&ofproto->ghost_ports, netdev_get_name(netdev));
3030 dp_port_name = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
3031 if (!dpif_port_exists(ofproto->backer->dpif, dp_port_name)) {
3032 odp_port_t port_no = ODPP_NONE;
3035 error = dpif_port_add(ofproto->backer->dpif, netdev, &port_no);
3039 if (netdev_get_tunnel_config(netdev)) {
3040 simap_put(&ofproto->backer->tnl_backers,
3041 dp_port_name, odp_to_u32(port_no));
3045 if (netdev_get_tunnel_config(netdev)) {
3046 sset_add(&ofproto->ghost_ports, devname);
3048 sset_add(&ofproto->ports, devname);
3054 port_del(struct ofproto *ofproto_, ofp_port_t ofp_port)
3056 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3057 struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
3064 sset_find_and_delete(&ofproto->ghost_ports,
3065 netdev_get_name(ofport->up.netdev));
3066 ofproto->backer->need_revalidate = REV_RECONFIGURE;
3067 if (!ofport->tnl_port) {
3068 error = dpif_port_del(ofproto->backer->dpif, ofport->odp_port);
3070 /* The caller is going to close ofport->up.netdev. If this is a
3071 * bonded port, then the bond is using that netdev, so remove it
3072 * from the bond. The client will need to reconfigure everything
3073 * after deleting ports, so then the slave will get re-added. */
3074 bundle_remove(&ofport->up);
3081 port_get_stats(const struct ofport *ofport_, struct netdev_stats *stats)
3083 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3088 error = netdev_get_stats(ofport->up.netdev, stats);
3090 if (!error && ofport_->ofp_port == OFPP_LOCAL) {
3091 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
3093 /* ofproto->stats.tx_packets represents packets that we created
3094 * internally and sent to some port (e.g. packets sent with
3095 * send_packet()). Account for them as if they had come from
3096 * OFPP_LOCAL and got forwarded. */
3098 if (stats->rx_packets != UINT64_MAX) {
3099 stats->rx_packets += ofproto->stats.tx_packets;
3102 if (stats->rx_bytes != UINT64_MAX) {
3103 stats->rx_bytes += ofproto->stats.tx_bytes;
3106 /* ofproto->stats.rx_packets represents packets that were received on
3107 * some port and we processed internally and dropped (e.g. STP).
3108 * Account for them as if they had been forwarded to OFPP_LOCAL. */
3110 if (stats->tx_packets != UINT64_MAX) {
3111 stats->tx_packets += ofproto->stats.rx_packets;
3114 if (stats->tx_bytes != UINT64_MAX) {
3115 stats->tx_bytes += ofproto->stats.rx_bytes;
3122 struct port_dump_state {
3127 struct ofproto_port port;
3132 port_dump_start(const struct ofproto *ofproto_ OVS_UNUSED, void **statep)
3134 *statep = xzalloc(sizeof(struct port_dump_state));
3139 port_dump_next(const struct ofproto *ofproto_, void *state_,
3140 struct ofproto_port *port)
3142 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3143 struct port_dump_state *state = state_;
3144 const struct sset *sset;
3145 struct sset_node *node;
3147 if (state->has_port) {
3148 ofproto_port_destroy(&state->port);
3149 state->has_port = false;
3151 sset = state->ghost ? &ofproto->ghost_ports : &ofproto->ports;
3152 while ((node = sset_at_position(sset, &state->bucket, &state->offset))) {
3155 error = port_query_by_name(ofproto_, node->name, &state->port);
3157 *port = state->port;
3158 state->has_port = true;
3160 } else if (error != ENODEV) {
3165 if (!state->ghost) {
3166 state->ghost = true;
3169 return port_dump_next(ofproto_, state_, port);
3176 port_dump_done(const struct ofproto *ofproto_ OVS_UNUSED, void *state_)
3178 struct port_dump_state *state = state_;
3180 if (state->has_port) {
3181 ofproto_port_destroy(&state->port);
3188 port_poll(const struct ofproto *ofproto_, char **devnamep)
3190 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3192 if (ofproto->port_poll_errno) {
3193 int error = ofproto->port_poll_errno;
3194 ofproto->port_poll_errno = 0;
3198 if (sset_is_empty(&ofproto->port_poll_set)) {
3202 *devnamep = sset_pop(&ofproto->port_poll_set);
3207 port_poll_wait(const struct ofproto *ofproto_)
3209 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
3210 dpif_port_poll_wait(ofproto->backer->dpif);
3214 port_is_lacp_current(const struct ofport *ofport_)
3216 const struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
3217 return (ofport->bundle && ofport->bundle->lacp
3218 ? lacp_slave_is_current(ofport->bundle->lacp, ofport)
3222 /* Upcall handling. */
3224 /* Flow miss batching.
3226 * Some dpifs implement operations faster when you hand them off in a batch.
3227 * To allow batching, "struct flow_miss" queues the dpif-related work needed
3228 * for a given flow. Each "struct flow_miss" corresponds to sending one or
3229 * more packets, plus possibly installing the flow in the dpif.
3231 * So far we only batch the operations that affect flow setup time the most.
3232 * It's possible to batch more than that, but the benefit might be minimal. */
3234 struct hmap_node hmap_node;
3235 struct ofproto_dpif *ofproto;
3237 enum odp_key_fitness key_fitness;
3238 const struct nlattr *key;
3240 struct list packets;
3241 enum dpif_upcall_type upcall_type;
3244 struct flow_miss_op {
3245 struct dpif_op dpif_op;
3247 uint64_t slow_stub[128 / 8]; /* Buffer for compose_slow_path() */
3248 struct xlate_out xout;
3249 bool xout_garbage; /* 'xout' needs to be uninitialized? */
3251 struct ofpbuf mask; /* Flow mask for "put" ops. */
3252 struct odputil_keybuf maskbuf;
3254 /* If this is a "put" op, then a pointer to the subfacet that should
3255 * be marked as uninstalled if the operation fails. */
3256 struct subfacet *subfacet;
3259 /* Sends an OFPT_PACKET_IN message for 'packet' of type OFPR_NO_MATCH to each
3260 * OpenFlow controller as necessary according to their individual
3261 * configurations. */
3263 send_packet_in_miss(struct ofproto_dpif *ofproto, const struct ofpbuf *packet,
3264 const struct flow *flow)
3266 struct ofputil_packet_in pin;
3268 pin.packet = packet->data;
3269 pin.packet_len = packet->size;
3270 pin.reason = OFPR_NO_MATCH;
3271 pin.controller_id = 0;
3276 pin.send_len = 0; /* not used for flow table misses */
3278 flow_get_metadata(flow, &pin.fmd);
3280 connmgr_send_packet_in(ofproto->up.connmgr, &pin);
3283 static struct flow_miss *
3284 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
3285 const struct flow *flow, uint32_t hash)
3287 struct flow_miss *miss;
3289 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
3290 if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
3298 /* Partially Initializes 'op' as an "execute" operation for 'miss' and
3299 * 'packet'. The caller must initialize op->actions and op->actions_len. If
3300 * 'miss' is associated with a subfacet the caller must also initialize the
3301 * returned op->subfacet, and if anything needs to be freed after processing
3302 * the op, the caller must initialize op->garbage also. */
3304 init_flow_miss_execute_op(struct flow_miss *miss, struct ofpbuf *packet,
3305 struct flow_miss_op *op)
3307 if (miss->flow.in_port.ofp_port
3308 != vsp_realdev_to_vlandev(miss->ofproto, miss->flow.in_port.ofp_port,
3309 miss->flow.vlan_tci)) {
3310 /* This packet was received on a VLAN splinter port. We
3311 * added a VLAN to the packet to make the packet resemble
3312 * the flow, but the actions were composed assuming that
3313 * the packet contained no VLAN. So, we must remove the
3314 * VLAN header from the packet before trying to execute the
3316 eth_pop_vlan(packet);
3319 op->subfacet = NULL;
3320 op->xout_garbage = false;
3321 op->dpif_op.type = DPIF_OP_EXECUTE;
3322 op->dpif_op.u.execute.key = miss->key;
3323 op->dpif_op.u.execute.key_len = miss->key_len;
3324 op->dpif_op.u.execute.packet = packet;
3325 ofpbuf_use_stack(&op->mask, &op->maskbuf, sizeof op->maskbuf);
3328 /* Helper for handle_flow_miss_without_facet() and
3329 * handle_flow_miss_with_facet(). */
3331 handle_flow_miss_common(struct rule_dpif *rule,
3332 struct ofpbuf *packet, const struct flow *flow)
3334 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
3336 if (rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
3338 * Extra-special case for fail-open mode.
3340 * We are in fail-open mode and the packet matched the fail-open
3341 * rule, but we are connected to a controller too. We should send
3342 * the packet up to the controller in the hope that it will try to
3343 * set up a flow and thereby allow us to exit fail-open.
3345 * See the top-level comment in fail-open.c for more information.
3347 send_packet_in_miss(ofproto, packet, flow);
3351 /* Figures out whether a flow that missed in 'ofproto', whose details are in
3352 * 'miss' masked by 'wc', is likely to be worth tracking in detail in userspace
3353 * and (usually) installing a datapath flow. The answer is usually "yes" (a
3354 * return value of true). However, for short flows the cost of bookkeeping is
3355 * much higher than the benefits, so when the datapath holds a large number of
3356 * flows we impose some heuristics to decide which flows are likely to be worth
3359 flow_miss_should_make_facet(struct flow_miss *miss, struct flow_wildcards *wc)
3361 struct dpif_backer *backer = miss->ofproto->backer;
3364 if (!backer->governor) {
3367 n_subfacets = hmap_count(&backer->subfacets);
3368 if (n_subfacets * 2 <= flow_eviction_threshold) {
3372 backer->governor = governor_create();
3375 hash = flow_hash_in_wildcards(&miss->flow, wc, 0);
3376 return governor_should_install_flow(backer->governor, hash,
3377 list_size(&miss->packets));
3380 /* Handles 'miss' without creating a facet or subfacet or creating any datapath
3381 * flow. 'miss->flow' must have matched 'rule' and been xlated into 'xout'.
3382 * May add an "execute" operation to 'ops' and increment '*n_ops'. */
3384 handle_flow_miss_without_facet(struct rule_dpif *rule, struct xlate_out *xout,
3385 struct flow_miss *miss,
3386 struct flow_miss_op *ops, size_t *n_ops)
3388 struct ofpbuf *packet;
3390 LIST_FOR_EACH (packet, list_node, &miss->packets) {
3392 COVERAGE_INC(facet_suppress);
3394 handle_flow_miss_common(rule, packet, &miss->flow);
3397 struct xlate_in xin;
3399 xlate_in_init(&xin, miss->ofproto, &miss->flow, rule, 0, packet);
3400 xlate_actions_for_side_effects(&xin);
3403 if (xout->odp_actions.size) {
3404 struct flow_miss_op *op = &ops[*n_ops];
3405 struct dpif_execute *execute = &op->dpif_op.u.execute;
3407 init_flow_miss_execute_op(miss, packet, op);
3408 xlate_out_copy(&op->xout, xout);
3409 execute->actions = op->xout.odp_actions.data;
3410 execute->actions_len = op->xout.odp_actions.size;
3411 op->xout_garbage = true;
3418 /* Handles 'miss', which matches 'facet'. May add any required datapath
3419 * operations to 'ops', incrementing '*n_ops' for each new op.
3421 * All of the packets in 'miss' are considered to have arrived at time 'now'.
3422 * This is really important only for new facets: if we just called time_msec()
3423 * here, then the new subfacet or its packets could look (occasionally) as
3424 * though it was used some time after the facet was used. That can make a
3425 * one-packet flow look like it has a nonzero duration, which looks odd in
3426 * e.g. NetFlow statistics.
3428 * If non-null, 'stats' will be folded into 'facet'. */
3430 handle_flow_miss_with_facet(struct flow_miss *miss, struct facet *facet,
3431 long long int now, struct dpif_flow_stats *stats,
3432 struct flow_miss_op *ops, size_t *n_ops)
3434 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
3435 enum subfacet_path want_path;
3436 struct subfacet *subfacet;
3437 struct ofpbuf *packet;
3439 subfacet = subfacet_create(facet, miss, now);
3440 want_path = facet->xout.slow ? SF_SLOW_PATH : SF_FAST_PATH;
3442 subfacet_update_stats(subfacet, stats);
3445 LIST_FOR_EACH (packet, list_node, &miss->packets) {
3446 struct flow_miss_op *op = &ops[*n_ops];
3448 handle_flow_miss_common(facet->rule, packet, &miss->flow);
3450 if (want_path != SF_FAST_PATH) {
3451 struct xlate_in xin;
3453 xlate_in_init(&xin, ofproto, &miss->flow, facet->rule, 0, packet);
3454 xlate_actions_for_side_effects(&xin);
3457 if (facet->xout.odp_actions.size) {
3458 struct dpif_execute *execute = &op->dpif_op.u.execute;
3460 init_flow_miss_execute_op(miss, packet, op);
3461 execute->actions = facet->xout.odp_actions.data,
3462 execute->actions_len = facet->xout.odp_actions.size;
3467 if (miss->upcall_type == DPIF_UC_MISS || subfacet->path != want_path) {
3468 struct flow_miss_op *op = &ops[(*n_ops)++];
3469 struct dpif_flow_put *put = &op->dpif_op.u.flow_put;
3471 subfacet->path = want_path;
3473 ofpbuf_use_stack(&op->mask, &op->maskbuf, sizeof op->maskbuf);
3474 odp_flow_key_from_mask(&op->mask, &facet->xout.wc.masks,
3475 &miss->flow, UINT32_MAX);
3477 op->xout_garbage = false;
3478 op->dpif_op.type = DPIF_OP_FLOW_PUT;
3479 op->subfacet = subfacet;
3480 put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
3481 put->key = miss->key;
3482 put->key_len = miss->key_len;
3483 put->mask = op->mask.data;
3484 put->mask_len = op->mask.size;
3486 if (want_path == SF_FAST_PATH) {
3487 put->actions = facet->xout.odp_actions.data;
3488 put->actions_len = facet->xout.odp_actions.size;
3490 compose_slow_path(ofproto, &miss->flow, facet->xout.slow,
3491 op->slow_stub, sizeof op->slow_stub,
3492 &put->actions, &put->actions_len);
3498 /* Handles flow miss 'miss'. May add any required datapath operations
3499 * to 'ops', incrementing '*n_ops' for each new op. */
3501 handle_flow_miss(struct flow_miss *miss, struct flow_miss_op *ops,
3504 struct ofproto_dpif *ofproto = miss->ofproto;
3505 struct dpif_flow_stats stats__;
3506 struct dpif_flow_stats *stats = &stats__;
3507 struct ofpbuf *packet;
3508 struct facet *facet;
3512 memset(stats, 0, sizeof *stats);
3514 LIST_FOR_EACH (packet, list_node, &miss->packets) {
3515 stats->tcp_flags |= packet_get_tcp_flags(packet, &miss->flow);
3516 stats->n_bytes += packet->size;
3520 facet = facet_lookup_valid(ofproto, &miss->flow);
3522 struct flow_wildcards wc;
3523 struct rule_dpif *rule;
3524 struct xlate_out xout;
3525 struct xlate_in xin;
3527 flow_wildcards_init_catchall(&wc);
3528 rule = rule_dpif_lookup(ofproto, &miss->flow, &wc);
3529 rule_credit_stats(rule, stats);
3531 xlate_in_init(&xin, ofproto, &miss->flow, rule, stats->tcp_flags,
3533 xin.resubmit_stats = stats;
3534 xin.may_learn = true;
3535 xlate_actions(&xin, &xout);
3536 flow_wildcards_or(&xout.wc, &xout.wc, &wc);
3538 /* There does not exist a bijection between 'struct flow' and datapath
3539 * flow keys with fitness ODP_FIT_TO_LITTLE. This breaks a fundamental
3540 * assumption used throughout the facet and subfacet handling code.
3541 * Since we have to handle these misses in userspace anyway, we simply
3542 * skip facet creation, avoiding the problem altogether. */
3543 if (miss->key_fitness == ODP_FIT_TOO_LITTLE
3544 || !flow_miss_should_make_facet(miss, &xout.wc)) {
3545 handle_flow_miss_without_facet(rule, &xout, miss, ops, n_ops);
3549 facet = facet_create(miss, rule, &xout, stats);
3552 handle_flow_miss_with_facet(miss, facet, now, stats, ops, n_ops);
3555 static struct drop_key *
3556 drop_key_lookup(const struct dpif_backer *backer, const struct nlattr *key,
3559 struct drop_key *drop_key;
3561 HMAP_FOR_EACH_WITH_HASH (drop_key, hmap_node, hash_bytes(key, key_len, 0),
3562 &backer->drop_keys) {
3563 if (drop_key->key_len == key_len
3564 && !memcmp(drop_key->key, key, key_len)) {
3572 drop_key_clear(struct dpif_backer *backer)
3574 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
3575 struct drop_key *drop_key, *next;
3577 HMAP_FOR_EACH_SAFE (drop_key, next, hmap_node, &backer->drop_keys) {
3580 error = dpif_flow_del(backer->dpif, drop_key->key, drop_key->key_len,
3582 if (error && !VLOG_DROP_WARN(&rl)) {
3583 struct ds ds = DS_EMPTY_INITIALIZER;
3584 odp_flow_key_format(drop_key->key, drop_key->key_len, &ds);
3585 VLOG_WARN("Failed to delete drop key (%s) (%s)", strerror(error),
3590 hmap_remove(&backer->drop_keys, &drop_key->hmap_node);
3591 free(drop_key->key);
3596 /* Given a datpath, packet, and flow metadata ('backer', 'packet', and 'key'
3597 * respectively), populates 'flow' with the result of odp_flow_key_to_flow().
3598 * Optionally, if nonnull, populates 'fitnessp' with the fitness of 'flow' as
3599 * returned by odp_flow_key_to_flow(). Also, optionally populates 'ofproto'
3600 * with the ofproto_dpif, and 'odp_in_port' with the datapath in_port, that
3601 * 'packet' ingressed.
3603 * If 'ofproto' is nonnull, requires 'flow''s in_port to exist. Otherwise sets
3604 * 'flow''s in_port to OFPP_NONE.
3606 * This function does post-processing on data returned from
3607 * odp_flow_key_to_flow() to help make VLAN splinters transparent to the rest
3608 * of the upcall processing logic. In particular, if the extracted in_port is
3609 * a VLAN splinter port, it replaces flow->in_port by the "real" port, sets
3610 * flow->vlan_tci correctly for the VLAN of the VLAN splinter port, and pushes
3611 * a VLAN header onto 'packet' (if it is nonnull).
3613 * Similarly, this function also includes some logic to help with tunnels. It
3614 * may modify 'flow' as necessary to make the tunneling implementation
3615 * transparent to the upcall processing logic.
3617 * Returns 0 if successful, ENODEV if the parsed flow has no associated ofport,
3618 * or some other positive errno if there are other problems. */
3620 ofproto_receive(const struct dpif_backer *backer, struct ofpbuf *packet,
3621 const struct nlattr *key, size_t key_len,
3622 struct flow *flow, enum odp_key_fitness *fitnessp,
3623 struct ofproto_dpif **ofproto, odp_port_t *odp_in_port)
3625 const struct ofport_dpif *port;
3626 enum odp_key_fitness fitness;
3629 fitness = odp_flow_key_to_flow(key, key_len, flow);
3630 if (fitness == ODP_FIT_ERROR) {
3636 *odp_in_port = flow->in_port.odp_port;
3639 port = (tnl_port_should_receive(flow)
3640 ? ofport_dpif_cast(tnl_port_receive(flow))
3641 : odp_port_to_ofport(backer, flow->in_port.odp_port));
3642 flow->in_port.ofp_port = port ? port->up.ofp_port : OFPP_NONE;
3647 /* XXX: Since the tunnel module is not scoped per backer, for a tunnel port
3648 * it's theoretically possible that we'll receive an ofport belonging to an
3649 * entirely different datapath. In practice, this can't happen because no
3650 * platforms has two separate datapaths which each support tunneling. */
3651 ovs_assert(ofproto_dpif_cast(port->up.ofproto)->backer == backer);
3653 if (vsp_adjust_flow(ofproto_dpif_cast(port->up.ofproto), flow)) {
3655 /* Make the packet resemble the flow, so that it gets sent to
3656 * an OpenFlow controller properly, so that it looks correct
3657 * for sFlow, and so that flow_extract() will get the correct
3658 * vlan_tci if it is called on 'packet'.
3660 * The allocated space inside 'packet' probably also contains
3661 * 'key', that is, both 'packet' and 'key' are probably part of
3662 * a struct dpif_upcall (see the large comment on that
3663 * structure definition), so pushing data on 'packet' is in
3664 * general not a good idea since it could overwrite 'key' or
3665 * free it as a side effect. However, it's OK in this special
3666 * case because we know that 'packet' is inside a Netlink
3667 * attribute: pushing 4 bytes will just overwrite the 4-byte
3668 * "struct nlattr", which is fine since we don't need that
3669 * header anymore. */
3670 eth_push_vlan(packet, flow->vlan_tci);
3672 /* We can't reproduce 'key' from 'flow'. */
3673 fitness = fitness == ODP_FIT_PERFECT ? ODP_FIT_TOO_MUCH : fitness;
3678 *ofproto = ofproto_dpif_cast(port->up.ofproto);
3683 *fitnessp = fitness;
3689 handle_miss_upcalls(struct dpif_backer *backer, struct dpif_upcall *upcalls,
3692 struct dpif_upcall *upcall;
3693 struct flow_miss *miss;
3694 struct flow_miss misses[FLOW_MISS_MAX_BATCH];
3695 struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
3696 struct dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
3706 /* Construct the to-do list.
3708 * This just amounts to extracting the flow from each packet and sticking
3709 * the packets that have the same flow in the same "flow_miss" structure so
3710 * that we can process them together. */
3713 for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
3714 struct flow_miss *miss = &misses[n_misses];
3715 struct flow_miss *existing_miss;
3716 struct ofproto_dpif *ofproto;
3717 odp_port_t odp_in_port;
3722 error = ofproto_receive(backer, upcall->packet, upcall->key,
3723 upcall->key_len, &flow, &miss->key_fitness,
3724 &ofproto, &odp_in_port);
3725 if (error == ENODEV) {
3726 struct drop_key *drop_key;
3728 /* Received packet on datapath port for which we couldn't
3729 * associate an ofproto. This can happen if a port is removed
3730 * while traffic is being received. Print a rate-limited message
3731 * in case it happens frequently. Install a drop flow so
3732 * that future packets of the flow are inexpensively dropped
3734 VLOG_INFO_RL(&rl, "received packet on unassociated datapath port "
3735 "%"PRIu32, odp_in_port);
3737 drop_key = drop_key_lookup(backer, upcall->key, upcall->key_len);
3739 drop_key = xmalloc(sizeof *drop_key);
3740 drop_key->key = xmemdup(upcall->key, upcall->key_len);
3741 drop_key->key_len = upcall->key_len;
3743 hmap_insert(&backer->drop_keys, &drop_key->hmap_node,
3744 hash_bytes(drop_key->key, drop_key->key_len, 0));
3745 dpif_flow_put(backer->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
3746 drop_key->key, drop_key->key_len,
3747 NULL, 0, NULL, 0, NULL);
3755 ofproto->n_missed++;
3756 flow_extract(upcall->packet, flow.skb_priority, flow.skb_mark,
3757 &flow.tunnel, &flow.in_port, &miss->flow);
3759 /* Add other packets to a to-do list. */
3760 hash = flow_hash(&miss->flow, 0);
3761 existing_miss = flow_miss_find(&todo, ofproto, &miss->flow, hash);
3762 if (!existing_miss) {
3763 hmap_insert(&todo, &miss->hmap_node, hash);
3764 miss->ofproto = ofproto;
3765 miss->key = upcall->key;
3766 miss->key_len = upcall->key_len;
3767 miss->upcall_type = upcall->type;
3768 list_init(&miss->packets);
3772 miss = existing_miss;
3774 list_push_back(&miss->packets, &upcall->packet->list_node);
3777 /* Process each element in the to-do list, constructing the set of
3778 * operations to batch. */
3780 HMAP_FOR_EACH (miss, hmap_node, &todo) {
3781 handle_flow_miss(miss, flow_miss_ops, &n_ops);
3783 ovs_assert(n_ops <= ARRAY_SIZE(flow_miss_ops));
3785 /* Execute batch. */
3786 for (i = 0; i < n_ops; i++) {
3787 dpif_ops[i] = &flow_miss_ops[i].dpif_op;
3789 dpif_operate(backer->dpif, dpif_ops, n_ops);
3791 for (i = 0; i < n_ops; i++) {
3792 if (dpif_ops[i]->error != 0
3793 && flow_miss_ops[i].dpif_op.type == DPIF_OP_FLOW_PUT
3794 && flow_miss_ops[i].subfacet) {
3795 struct subfacet *subfacet = flow_miss_ops[i].subfacet;
3797 COVERAGE_INC(subfacet_install_fail);
3799 subfacet->path = SF_NOT_INSTALLED;
3803 if (flow_miss_ops[i].xout_garbage) {
3804 xlate_out_uninit(&flow_miss_ops[i].xout);
3807 hmap_destroy(&todo);
3810 static enum { SFLOW_UPCALL, MISS_UPCALL, BAD_UPCALL, FLOW_SAMPLE_UPCALL,
3812 classify_upcall(const struct dpif_upcall *upcall)
3814 size_t userdata_len;
3815 union user_action_cookie cookie;
3817 /* First look at the upcall type. */
3818 switch (upcall->type) {
3819 case DPIF_UC_ACTION:
3825 case DPIF_N_UC_TYPES:
3827 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, upcall->type);
3831 /* "action" upcalls need a closer look. */
3832 if (!upcall->userdata) {
3833 VLOG_WARN_RL(&rl, "action upcall missing cookie");
3836 userdata_len = nl_attr_get_size(upcall->userdata);
3837 if (userdata_len < sizeof cookie.type
3838 || userdata_len > sizeof cookie) {
3839 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %zu",
3843 memset(&cookie, 0, sizeof cookie);
3844 memcpy(&cookie, nl_attr_get(upcall->userdata), userdata_len);
3845 if (userdata_len == sizeof cookie.sflow
3846 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
3847 return SFLOW_UPCALL;
3848 } else if (userdata_len == sizeof cookie.slow_path
3849 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
3851 } else if (userdata_len == sizeof cookie.flow_sample
3852 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
3853 return FLOW_SAMPLE_UPCALL;
3854 } else if (userdata_len == sizeof cookie.ipfix
3855 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
3856 return IPFIX_UPCALL;
3858 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
3859 " and size %zu", cookie.type, userdata_len);
3865 handle_sflow_upcall(struct dpif_backer *backer,
3866 const struct dpif_upcall *upcall)
3868 struct ofproto_dpif *ofproto;
3869 union user_action_cookie cookie;
3871 odp_port_t odp_in_port;
3873 if (ofproto_receive(backer, upcall->packet, upcall->key, upcall->key_len,
3874 &flow, NULL, &ofproto, &odp_in_port)
3875 || !ofproto->sflow) {
3879 memset(&cookie, 0, sizeof cookie);
3880 memcpy(&cookie, nl_attr_get(upcall->userdata), sizeof cookie.sflow);
3881 dpif_sflow_received(ofproto->sflow, upcall->packet, &flow,
3882 odp_in_port, &cookie);
3886 handle_flow_sample_upcall(struct dpif_backer *backer,
3887 const struct dpif_upcall *upcall)
3889 struct ofproto_dpif *ofproto;
3890 union user_action_cookie cookie;
3893 if (ofproto_receive(backer, upcall->packet, upcall->key, upcall->key_len,
3894 &flow, NULL, &ofproto, NULL)
3895 || !ofproto->ipfix) {
3899 memset(&cookie, 0, sizeof cookie);
3900 memcpy(&cookie, nl_attr_get(upcall->userdata), sizeof cookie.flow_sample);
3902 /* The flow reflects exactly the contents of the packet. Sample
3903 * the packet using it. */
3904 dpif_ipfix_flow_sample(ofproto->ipfix, upcall->packet, &flow,
3905 cookie.flow_sample.collector_set_id,
3906 cookie.flow_sample.probability,
3907 cookie.flow_sample.obs_domain_id,
3908 cookie.flow_sample.obs_point_id);
3912 handle_ipfix_upcall(struct dpif_backer *backer,
3913 const struct dpif_upcall *upcall)
3915 struct ofproto_dpif *ofproto;
3918 if (ofproto_receive(backer, upcall->packet, upcall->key, upcall->key_len,
3919 &flow, NULL, &ofproto, NULL)
3920 || !ofproto->ipfix) {
3924 /* The flow reflects exactly the contents of the packet. Sample
3925 * the packet using it. */
3926 dpif_ipfix_bridge_sample(ofproto->ipfix, upcall->packet, &flow);
3930 handle_upcalls(struct dpif_backer *backer, unsigned int max_batch)
3932 struct dpif_upcall misses[FLOW_MISS_MAX_BATCH];
3933 struct ofpbuf miss_bufs[FLOW_MISS_MAX_BATCH];
3934 uint64_t miss_buf_stubs[FLOW_MISS_MAX_BATCH][4096 / 8];
3939 ovs_assert(max_batch <= FLOW_MISS_MAX_BATCH);
3942 for (n_processed = 0; n_processed < max_batch; n_processed++) {
3943 struct dpif_upcall *upcall = &misses[n_misses];
3944 struct ofpbuf *buf = &miss_bufs[n_misses];
3947 ofpbuf_use_stub(buf, miss_buf_stubs[n_misses],
3948 sizeof miss_buf_stubs[n_misses]);
3949 error = dpif_recv(backer->dpif, upcall, buf);
3955 switch (classify_upcall(upcall)) {
3957 /* Handle it later. */
3962 handle_sflow_upcall(backer, upcall);
3966 case FLOW_SAMPLE_UPCALL:
3967 handle_flow_sample_upcall(backer, upcall);
3972 handle_ipfix_upcall(backer, upcall);
3982 /* Handle deferred MISS_UPCALL processing. */
3983 handle_miss_upcalls(backer, misses, n_misses);
3984 for (i = 0; i < n_misses; i++) {
3985 ofpbuf_uninit(&miss_bufs[i]);
3991 /* Flow expiration. */
3993 static int subfacet_max_idle(const struct dpif_backer *);
3994 static void update_stats(struct dpif_backer *);
3995 static void rule_expire(struct rule_dpif *);
3996 static void expire_subfacets(struct dpif_backer *, int dp_max_idle);
3998 /* This function is called periodically by run(). Its job is to collect
3999 * updates for the flows that have been installed into the datapath, most
4000 * importantly when they last were used, and then use that information to
4001 * expire flows that have not been used recently.
4003 * Returns the number of milliseconds after which it should be called again. */
4005 expire(struct dpif_backer *backer)
4007 struct ofproto_dpif *ofproto;
4011 /* Periodically clear out the drop keys in an effort to keep them
4012 * relatively few. */
4013 drop_key_clear(backer);
4015 /* Update stats for each flow in the backer. */
4016 update_stats(backer);
4018 n_subfacets = hmap_count(&backer->subfacets);
4020 struct subfacet *subfacet;
4021 long long int total, now;
4025 HMAP_FOR_EACH (subfacet, hmap_node, &backer->subfacets) {
4026 total += now - subfacet->created;
4028 backer->avg_subfacet_life += total / n_subfacets;
4030 backer->avg_subfacet_life /= 2;
4032 backer->avg_n_subfacet += n_subfacets;
4033 backer->avg_n_subfacet /= 2;
4035 backer->max_n_subfacet = MAX(backer->max_n_subfacet, n_subfacets);
4037 max_idle = subfacet_max_idle(backer);
4038 expire_subfacets(backer, max_idle);
4040 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4041 struct rule *rule, *next_rule;
4043 if (ofproto->backer != backer) {
4047 /* Expire OpenFlow flows whose idle_timeout or hard_timeout
4049 LIST_FOR_EACH_SAFE (rule, next_rule, expirable,
4050 &ofproto->up.expirable) {
4051 rule_expire(rule_dpif_cast(rule));
4054 /* All outstanding data in existing flows has been accounted, so it's a
4055 * good time to do bond rebalancing. */
4056 if (ofproto->has_bonded_bundles) {
4057 struct ofbundle *bundle;
4059 HMAP_FOR_EACH (bundle, hmap_node, &ofproto->bundles) {
4061 bond_rebalance(bundle->bond, &backer->revalidate_set);
4067 return MIN(max_idle, 1000);
4070 /* Updates flow table statistics given that the datapath just reported 'stats'
4071 * as 'subfacet''s statistics. */
4073 update_subfacet_stats(struct subfacet *subfacet,
4074 const struct dpif_flow_stats *stats)
4076 struct facet *facet = subfacet->facet;
4077 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4078 struct dpif_flow_stats diff;
4080 diff.tcp_flags = stats->tcp_flags;
4081 diff.used = stats->used;
4083 if (stats->n_packets >= subfacet->dp_packet_count) {
4084 diff.n_packets = stats->n_packets - subfacet->dp_packet_count;
4086 VLOG_WARN_RL(&rl, "unexpected packet count from the datapath");
4090 if (stats->n_bytes >= subfacet->dp_byte_count) {
4091 diff.n_bytes = stats->n_bytes - subfacet->dp_byte_count;
4093 VLOG_WARN_RL(&rl, "unexpected byte count from datapath");
4097 ofproto->n_hit += diff.n_packets;
4098 subfacet->dp_packet_count = stats->n_packets;
4099 subfacet->dp_byte_count = stats->n_bytes;
4100 subfacet_update_stats(subfacet, &diff);
4102 if (facet->accounted_bytes < facet->byte_count) {
4104 facet_account(facet);
4105 facet->accounted_bytes = facet->byte_count;
4109 /* 'key' with length 'key_len' bytes is a flow in 'dpif' that we know nothing
4110 * about, or a flow that shouldn't be installed but was anyway. Delete it. */
4112 delete_unexpected_flow(struct dpif_backer *backer,
4113 const struct nlattr *key, size_t key_len)
4115 if (!VLOG_DROP_WARN(&rl)) {
4119 odp_flow_key_format(key, key_len, &s);
4120 VLOG_WARN("unexpected flow: %s", ds_cstr(&s));
4124 COVERAGE_INC(facet_unexpected);
4125 dpif_flow_del(backer->dpif, key, key_len, NULL);
4128 /* Update 'packet_count', 'byte_count', and 'used' members of installed facets.
4130 * This function also pushes statistics updates to rules which each facet
4131 * resubmits into. Generally these statistics will be accurate. However, if a
4132 * facet changes the rule it resubmits into at some time in between
4133 * update_stats() runs, it is possible that statistics accrued to the
4134 * old rule will be incorrectly attributed to the new rule. This could be
4135 * avoided by calling update_stats() whenever rules are created or
4136 * deleted. However, the performance impact of making so many calls to the
4137 * datapath do not justify the benefit of having perfectly accurate statistics.
4139 * In addition, this function maintains per ofproto flow hit counts. The patch
4140 * port is not treated specially. e.g. A packet ingress from br0 patched into
4141 * br1 will increase the hit count of br0 by 1, however, does not affect
4142 * the hit or miss counts of br1.
4145 update_stats(struct dpif_backer *backer)
4147 const struct dpif_flow_stats *stats;
4148 struct dpif_flow_dump dump;
4149 const struct nlattr *key, *mask;
4150 size_t key_len, mask_len;
4152 dpif_flow_dump_start(&dump, backer->dpif);
4153 while (dpif_flow_dump_next(&dump, &key, &key_len,
4154 &mask, &mask_len, NULL, NULL, &stats)) {
4155 struct subfacet *subfacet;
4158 key_hash = odp_flow_key_hash(key, key_len);
4159 subfacet = subfacet_find(backer, key, key_len, key_hash);
4160 switch (subfacet ? subfacet->path : SF_NOT_INSTALLED) {
4162 update_subfacet_stats(subfacet, stats);
4166 /* Stats are updated per-packet. */
4169 case SF_NOT_INSTALLED:
4171 delete_unexpected_flow(backer, key, key_len);
4176 dpif_flow_dump_done(&dump);
4178 update_moving_averages(backer);
4181 /* Calculates and returns the number of milliseconds of idle time after which
4182 * subfacets should expire from the datapath. When a subfacet expires, we fold
4183 * its statistics into its facet, and when a facet's last subfacet expires, we
4184 * fold its statistic into its rule. */
4186 subfacet_max_idle(const struct dpif_backer *backer)
4189 * Idle time histogram.
4191 * Most of the time a switch has a relatively small number of subfacets.
4192 * When this is the case we might as well keep statistics for all of them
4193 * in userspace and to cache them in the kernel datapath for performance as
4196 * As the number of subfacets increases, the memory required to maintain
4197 * statistics about them in userspace and in the kernel becomes
4198 * significant. However, with a large number of subfacets it is likely
4199 * that only a few of them are "heavy hitters" that consume a large amount
4200 * of bandwidth. At this point, only heavy hitters are worth caching in
4201 * the kernel and maintaining in userspaces; other subfacets we can
4204 * The technique used to compute the idle time is to build a histogram with
4205 * N_BUCKETS buckets whose width is BUCKET_WIDTH msecs each. Each subfacet
4206 * that is installed in the kernel gets dropped in the appropriate bucket.
4207 * After the histogram has been built, we compute the cutoff so that only
4208 * the most-recently-used 1% of subfacets (but at least
4209 * flow_eviction_threshold flows) are kept cached. At least
4210 * the most-recently-used bucket of subfacets is kept, so actually an
4211 * arbitrary number of subfacets can be kept in any given expiration run
4212 * (though the next run will delete most of those unless they receive
4215 * This requires a second pass through the subfacets, in addition to the
4216 * pass made by update_stats(), because the former function never looks at
4217 * uninstallable subfacets.
4219 enum { BUCKET_WIDTH = ROUND_UP(100, TIME_UPDATE_INTERVAL) };
4220 enum { N_BUCKETS = 5000 / BUCKET_WIDTH };
4221 int buckets[N_BUCKETS] = { 0 };
4222 int total, subtotal, bucket;
4223 struct subfacet *subfacet;
4227 total = hmap_count(&backer->subfacets);
4228 if (total <= flow_eviction_threshold) {
4229 return N_BUCKETS * BUCKET_WIDTH;
4232 /* Build histogram. */
4234 HMAP_FOR_EACH (subfacet, hmap_node, &backer->subfacets) {
4235 long long int idle = now - subfacet->used;
4236 int bucket = (idle <= 0 ? 0
4237 : idle >= BUCKET_WIDTH * N_BUCKETS ? N_BUCKETS - 1
4238 : (unsigned int) idle / BUCKET_WIDTH);
4242 /* Find the first bucket whose flows should be expired. */
4243 subtotal = bucket = 0;
4245 subtotal += buckets[bucket++];
4246 } while (bucket < N_BUCKETS &&
4247 subtotal < MAX(flow_eviction_threshold, total / 100));
4249 if (VLOG_IS_DBG_ENABLED()) {
4253 ds_put_cstr(&s, "keep");
4254 for (i = 0; i < N_BUCKETS; i++) {
4256 ds_put_cstr(&s, ", drop");
4259 ds_put_format(&s, " %d:%d", i * BUCKET_WIDTH, buckets[i]);
4262 VLOG_INFO("%s (msec:count)", ds_cstr(&s));
4266 return bucket * BUCKET_WIDTH;
4270 expire_subfacets(struct dpif_backer *backer, int dp_max_idle)
4272 /* Cutoff time for most flows. */
4273 long long int normal_cutoff = time_msec() - dp_max_idle;
4275 /* We really want to keep flows for special protocols around, so use a more
4276 * conservative cutoff. */
4277 long long int special_cutoff = time_msec() - 10000;
4279 struct subfacet *subfacet, *next_subfacet;
4280 struct subfacet *batch[SUBFACET_DESTROY_MAX_BATCH];
4284 HMAP_FOR_EACH_SAFE (subfacet, next_subfacet, hmap_node,
4285 &backer->subfacets) {
4286 long long int cutoff;
4288 cutoff = (subfacet->facet->xout.slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP
4292 if (subfacet->used < cutoff) {
4293 if (subfacet->path != SF_NOT_INSTALLED) {
4294 batch[n_batch++] = subfacet;
4295 if (n_batch >= SUBFACET_DESTROY_MAX_BATCH) {
4296 subfacet_destroy_batch(backer, batch, n_batch);
4300 subfacet_destroy(subfacet);
4306 subfacet_destroy_batch(backer, batch, n_batch);
4310 /* If 'rule' is an OpenFlow rule, that has expired according to OpenFlow rules,
4311 * then delete it entirely. */
4313 rule_expire(struct rule_dpif *rule)
4315 struct facet *facet, *next_facet;
4319 if (rule->up.pending) {
4320 /* We'll have to expire it later. */
4324 /* Has 'rule' expired? */
4326 if (rule->up.hard_timeout
4327 && now > rule->up.modified + rule->up.hard_timeout * 1000) {
4328 reason = OFPRR_HARD_TIMEOUT;
4329 } else if (rule->up.idle_timeout
4330 && now > rule->up.used + rule->up.idle_timeout * 1000) {
4331 reason = OFPRR_IDLE_TIMEOUT;
4336 COVERAGE_INC(ofproto_dpif_expired);
4338 /* Update stats. (This is a no-op if the rule expired due to an idle
4339 * timeout, because that only happens when the rule has no facets left.) */
4340 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
4341 facet_remove(facet);
4344 /* Get rid of the rule. */
4345 ofproto_rule_expire(&rule->up, reason);
4350 /* Creates and returns a new facet based on 'miss'.
4352 * The caller must already have determined that no facet with an identical
4353 * 'miss->flow' exists in 'miss->ofproto'.
4355 * 'rule' and 'xout' must have been created based on 'miss'.
4357 * 'facet'' statistics are initialized based on 'stats'.
4359 * The facet will initially have no subfacets. The caller should create (at
4360 * least) one subfacet with subfacet_create(). */
4361 static struct facet *
4362 facet_create(const struct flow_miss *miss, struct rule_dpif *rule,
4363 struct xlate_out *xout, struct dpif_flow_stats *stats)
4365 struct ofproto_dpif *ofproto = miss->ofproto;
4366 struct facet *facet;
4369 facet = xzalloc(sizeof *facet);
4370 facet->packet_count = facet->prev_packet_count = stats->n_packets;
4371 facet->byte_count = facet->prev_byte_count = stats->n_bytes;
4372 facet->tcp_flags = stats->tcp_flags;
4373 facet->used = stats->used;
4374 facet->flow = miss->flow;
4375 facet->learn_rl = time_msec() + 500;
4378 list_push_back(&facet->rule->facets, &facet->list_node);
4379 list_init(&facet->subfacets);
4380 netflow_flow_init(&facet->nf_flow);
4381 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow, facet->used);
4383 xlate_out_copy(&facet->xout, xout);
4385 match_init(&match, &facet->flow, &facet->xout.wc);
4386 cls_rule_init(&facet->cr, &match, OFP_DEFAULT_PRIORITY);
4387 classifier_insert(&ofproto->facets, &facet->cr);
4389 facet->nf_flow.output_iface = facet->xout.nf_output_iface;
4395 facet_free(struct facet *facet)
4398 xlate_out_uninit(&facet->xout);
4403 /* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
4404 * 'packet', which arrived on 'in_port'. */
4406 execute_odp_actions(struct ofproto_dpif *ofproto, const struct flow *flow,
4407 const struct nlattr *odp_actions, size_t actions_len,
4408 struct ofpbuf *packet)
4410 struct odputil_keybuf keybuf;
4414 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
4415 odp_flow_key_from_flow(&key, flow,
4416 ofp_port_to_odp_port(ofproto, flow->in_port.ofp_port));
4418 error = dpif_execute(ofproto->backer->dpif, key.data, key.size,
4419 odp_actions, actions_len, packet);
4423 /* Remove 'facet' from its ofproto and free up the associated memory:
4425 * - If 'facet' was installed in the datapath, uninstalls it and updates its
4426 * rule's statistics, via subfacet_uninstall().
4428 * - Removes 'facet' from its rule and from ofproto->facets.
4431 facet_remove(struct facet *facet)
4433 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4434 struct subfacet *subfacet, *next_subfacet;
4436 ovs_assert(!list_is_empty(&facet->subfacets));
4438 /* First uninstall all of the subfacets to get final statistics. */
4439 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4440 subfacet_uninstall(subfacet);
4443 /* Flush the final stats to the rule.
4445 * This might require us to have at least one subfacet around so that we
4446 * can use its actions for accounting in facet_account(), which is why we
4447 * have uninstalled but not yet destroyed the subfacets. */
4448 facet_flush_stats(facet);
4450 /* Now we're really all done so destroy everything. */
4451 LIST_FOR_EACH_SAFE (subfacet, next_subfacet, list_node,
4452 &facet->subfacets) {
4453 subfacet_destroy__(subfacet);
4455 classifier_remove(&ofproto->facets, &facet->cr);
4456 cls_rule_destroy(&facet->cr);
4457 list_remove(&facet->list_node);
4461 /* Feed information from 'facet' back into the learning table to keep it in
4462 * sync with what is actually flowing through the datapath. */
4464 facet_learn(struct facet *facet)
4466 long long int now = time_msec();
4468 if (!facet->xout.has_fin_timeout && now < facet->learn_rl) {
4472 facet->learn_rl = now + 500;
4474 if (!facet->xout.has_learn
4475 && !facet->xout.has_normal
4476 && (!facet->xout.has_fin_timeout
4477 || !(facet->tcp_flags & (TCP_FIN | TCP_RST)))) {
4481 facet_push_stats(facet, true);
4485 facet_account(struct facet *facet)
4487 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4488 const struct nlattr *a;
4493 if (!facet->xout.has_normal || !ofproto->has_bonded_bundles) {
4496 n_bytes = facet->byte_count - facet->accounted_bytes;
4498 /* This loop feeds byte counters to bond_account() for rebalancing to use
4499 * as a basis. We also need to track the actual VLAN on which the packet
4500 * is going to be sent to ensure that it matches the one passed to
4501 * bond_choose_output_slave(). (Otherwise, we will account to the wrong
4504 * We use the actions from an arbitrary subfacet because they should all
4505 * be equally valid for our purpose. */
4506 vlan_tci = facet->flow.vlan_tci;
4507 NL_ATTR_FOR_EACH_UNSAFE (a, left, facet->xout.odp_actions.data,
4508 facet->xout.odp_actions.size) {
4509 const struct ovs_action_push_vlan *vlan;
4510 struct ofport_dpif *port;
4512 switch (nl_attr_type(a)) {
4513 case OVS_ACTION_ATTR_OUTPUT:
4514 port = get_odp_port(ofproto, nl_attr_get_odp_port(a));
4515 if (port && port->bundle && port->bundle->bond) {
4516 bond_account(port->bundle->bond, &facet->flow,
4517 vlan_tci_to_vid(vlan_tci), n_bytes);
4521 case OVS_ACTION_ATTR_POP_VLAN:
4522 vlan_tci = htons(0);
4525 case OVS_ACTION_ATTR_PUSH_VLAN:
4526 vlan = nl_attr_get(a);
4527 vlan_tci = vlan->vlan_tci;
4533 /* Returns true if the only action for 'facet' is to send to the controller.
4534 * (We don't report NetFlow expiration messages for such facets because they
4535 * are just part of the control logic for the network, not real traffic). */
4537 facet_is_controller_flow(struct facet *facet)
4540 const struct rule *rule = &facet->rule->up;
4541 const struct ofpact *ofpacts = rule->ofpacts;
4542 size_t ofpacts_len = rule->ofpacts_len;
4544 if (ofpacts_len > 0 &&
4545 ofpacts->type == OFPACT_CONTROLLER &&
4546 ofpact_next(ofpacts) >= ofpact_end(ofpacts, ofpacts_len)) {
4553 /* Folds all of 'facet''s statistics into its rule. Also updates the
4554 * accounting ofhook and emits a NetFlow expiration if appropriate. All of
4555 * 'facet''s statistics in the datapath should have been zeroed and folded into
4556 * its packet and byte counts before this function is called. */
4558 facet_flush_stats(struct facet *facet)
4560 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4561 struct subfacet *subfacet;
4563 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4564 ovs_assert(!subfacet->dp_byte_count);
4565 ovs_assert(!subfacet->dp_packet_count);
4568 facet_push_stats(facet, false);
4569 if (facet->accounted_bytes < facet->byte_count) {
4570 facet_account(facet);
4571 facet->accounted_bytes = facet->byte_count;
4574 if (ofproto->netflow && !facet_is_controller_flow(facet)) {
4575 struct ofexpired expired;
4576 expired.flow = facet->flow;
4577 expired.packet_count = facet->packet_count;
4578 expired.byte_count = facet->byte_count;
4579 expired.used = facet->used;
4580 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
4583 /* Reset counters to prevent double counting if 'facet' ever gets
4585 facet_reset_counters(facet);
4587 netflow_flow_clear(&facet->nf_flow);
4588 facet->tcp_flags = 0;
4591 /* Searches 'ofproto''s table of facets for one which would be responsible for
4592 * 'flow'. Returns it if found, otherwise a null pointer.
4594 * The returned facet might need revalidation; use facet_lookup_valid()
4595 * instead if that is important. */
4596 static struct facet *
4597 facet_find(struct ofproto_dpif *ofproto, const struct flow *flow)
4599 struct cls_rule *cr = classifier_lookup(&ofproto->facets, flow, NULL);
4600 return cr ? CONTAINER_OF(cr, struct facet, cr) : NULL;
4603 /* Searches 'ofproto''s table of facets for one capable that covers
4604 * 'flow'. Returns it if found, otherwise a null pointer.
4606 * The returned facet is guaranteed to be valid. */
4607 static struct facet *
4608 facet_lookup_valid(struct ofproto_dpif *ofproto, const struct flow *flow)
4610 struct facet *facet;
4612 facet = facet_find(ofproto, flow);
4614 && (ofproto->backer->need_revalidate
4615 || tag_set_intersects(&ofproto->backer->revalidate_set,
4617 && !facet_revalidate(facet)) {
4625 facet_check_consistency(struct facet *facet)
4627 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 15);
4629 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4631 struct xlate_out xout;
4632 struct xlate_in xin;
4634 struct rule_dpif *rule;
4637 /* Check the rule for consistency. */
4638 rule = rule_dpif_lookup(ofproto, &facet->flow, NULL);
4639 if (rule != facet->rule) {
4640 if (!VLOG_DROP_WARN(&rl)) {
4641 struct ds s = DS_EMPTY_INITIALIZER;
4643 flow_format(&s, &facet->flow);
4644 ds_put_format(&s, ": facet associated with wrong rule (was "
4645 "table=%"PRIu8",", facet->rule->up.table_id);
4646 cls_rule_format(&facet->rule->up.cr, &s);
4647 ds_put_format(&s, ") (should have been table=%"PRIu8",",
4649 cls_rule_format(&rule->up.cr, &s);
4650 ds_put_char(&s, ')');
4652 VLOG_WARN("%s", ds_cstr(&s));
4658 /* Check the datapath actions for consistency. */
4659 xlate_in_init(&xin, ofproto, &facet->flow, rule, 0, NULL);
4660 xlate_actions(&xin, &xout);
4662 ok = ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)
4663 && facet->xout.slow == xout.slow;
4664 if (!ok && !VLOG_DROP_WARN(&rl)) {
4665 struct ds s = DS_EMPTY_INITIALIZER;
4667 flow_format(&s, &facet->flow);
4668 ds_put_cstr(&s, ": inconsistency in facet");
4670 if (!ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)) {
4671 ds_put_cstr(&s, " (actions were: ");
4672 format_odp_actions(&s, facet->xout.odp_actions.data,
4673 facet->xout.odp_actions.size);
4674 ds_put_cstr(&s, ") (correct actions: ");
4675 format_odp_actions(&s, xout.odp_actions.data,
4676 xout.odp_actions.size);
4677 ds_put_char(&s, ')');
4680 if (facet->xout.slow != xout.slow) {
4681 ds_put_format(&s, " slow path incorrect. should be %d", xout.slow);
4684 VLOG_WARN("%s", ds_cstr(&s));
4687 xlate_out_uninit(&xout);
4692 /* Re-searches the classifier for 'facet':
4694 * - If the rule found is different from 'facet''s current rule, moves
4695 * 'facet' to the new rule and recompiles its actions.
4697 * - If the rule found is the same as 'facet''s current rule, leaves 'facet'
4698 * where it is and recompiles its actions anyway.
4700 * - If any of 'facet''s subfacets correspond to a new flow according to
4701 * ofproto_receive(), 'facet' is removed.
4703 * Returns true if 'facet' is still valid. False if 'facet' was removed. */
4705 facet_revalidate(struct facet *facet)
4707 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4708 struct rule_dpif *new_rule;
4709 struct subfacet *subfacet;
4710 struct flow_wildcards wc;
4711 struct xlate_out xout;
4712 struct xlate_in xin;
4714 COVERAGE_INC(facet_revalidate);
4716 /* Check that child subfacets still correspond to this facet. Tunnel
4717 * configuration changes could cause a subfacet's OpenFlow in_port to
4719 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
4720 struct ofproto_dpif *recv_ofproto;
4721 struct flow recv_flow;
4724 error = ofproto_receive(ofproto->backer, NULL, subfacet->key,
4725 subfacet->key_len, &recv_flow, NULL,
4726 &recv_ofproto, NULL);
4728 || recv_ofproto != ofproto
4729 || facet != facet_find(ofproto, &recv_flow)) {
4730 facet_remove(facet);
4735 flow_wildcards_init_catchall(&wc);
4736 new_rule = rule_dpif_lookup(ofproto, &facet->flow, &wc);
4738 /* Calculate new datapath actions.
4740 * We do not modify any 'facet' state yet, because we might need to, e.g.,
4741 * emit a NetFlow expiration and, if so, we need to have the old state
4742 * around to properly compose it. */
4743 xlate_in_init(&xin, ofproto, &facet->flow, new_rule, 0, NULL);
4744 xlate_actions(&xin, &xout);
4745 flow_wildcards_or(&xout.wc, &xout.wc, &wc);
4747 /* A facet's slow path reason should only change under dramatic
4748 * circumstances. Rather than try to update everything, it's simpler to
4749 * remove the facet and start over.
4751 * More importantly, if a facet's wildcards change, it will be relatively
4752 * difficult to figure out if its subfacets still belong to it, and if not
4753 * which facet they may belong to. Again, to avoid the complexity, we
4754 * simply give up instead. */
4755 if (facet->xout.slow != xout.slow
4756 || memcmp(&facet->xout.wc, &xout.wc, sizeof xout.wc)) {
4757 facet_remove(facet);
4758 xlate_out_uninit(&xout);
4762 if (!ofpbuf_equal(&facet->xout.odp_actions, &xout.odp_actions)) {
4763 LIST_FOR_EACH(subfacet, list_node, &facet->subfacets) {
4764 if (subfacet->path == SF_FAST_PATH) {
4765 struct dpif_flow_stats stats;
4767 subfacet_install(subfacet, &xout.odp_actions, &stats);
4768 subfacet_update_stats(subfacet, &stats);
4772 facet_flush_stats(facet);
4774 ofpbuf_clear(&facet->xout.odp_actions);
4775 ofpbuf_put(&facet->xout.odp_actions, xout.odp_actions.data,
4776 xout.odp_actions.size);
4779 /* Update 'facet' now that we've taken care of all the old state. */
4780 facet->xout.tags = xout.tags;
4781 facet->xout.slow = xout.slow;
4782 facet->xout.has_learn = xout.has_learn;
4783 facet->xout.has_normal = xout.has_normal;
4784 facet->xout.has_fin_timeout = xout.has_fin_timeout;
4785 facet->xout.nf_output_iface = xout.nf_output_iface;
4786 facet->xout.mirrors = xout.mirrors;
4787 facet->nf_flow.output_iface = facet->xout.nf_output_iface;
4789 if (facet->rule != new_rule) {
4790 COVERAGE_INC(facet_changed_rule);
4791 list_remove(&facet->list_node);
4792 list_push_back(&new_rule->facets, &facet->list_node);
4793 facet->rule = new_rule;
4794 facet->used = new_rule->up.created;
4795 facet->prev_used = facet->used;
4798 xlate_out_uninit(&xout);
4803 facet_reset_counters(struct facet *facet)
4805 facet->packet_count = 0;
4806 facet->byte_count = 0;
4807 facet->prev_packet_count = 0;
4808 facet->prev_byte_count = 0;
4809 facet->accounted_bytes = 0;
4813 facet_push_stats(struct facet *facet, bool may_learn)
4815 struct dpif_flow_stats stats;
4817 ovs_assert(facet->packet_count >= facet->prev_packet_count);
4818 ovs_assert(facet->byte_count >= facet->prev_byte_count);
4819 ovs_assert(facet->used >= facet->prev_used);
4821 stats.n_packets = facet->packet_count - facet->prev_packet_count;
4822 stats.n_bytes = facet->byte_count - facet->prev_byte_count;
4823 stats.used = facet->used;
4824 stats.tcp_flags = facet->tcp_flags;
4826 if (may_learn || stats.n_packets || facet->used > facet->prev_used) {
4827 struct ofproto_dpif *ofproto =
4828 ofproto_dpif_cast(facet->rule->up.ofproto);
4830 struct ofport_dpif *in_port;
4831 struct xlate_in xin;
4833 facet->prev_packet_count = facet->packet_count;
4834 facet->prev_byte_count = facet->byte_count;
4835 facet->prev_used = facet->used;
4837 in_port = get_ofp_port(ofproto, facet->flow.in_port.ofp_port);
4838 if (in_port && in_port->tnl_port) {
4839 netdev_vport_inc_rx(in_port->up.netdev, &stats);
4842 rule_credit_stats(facet->rule, &stats);
4843 netflow_flow_update_time(ofproto->netflow, &facet->nf_flow,
4845 netflow_flow_update_flags(&facet->nf_flow, facet->tcp_flags);
4846 update_mirror_stats(ofproto, facet->xout.mirrors, stats.n_packets,
4849 xlate_in_init(&xin, ofproto, &facet->flow, facet->rule,
4850 stats.tcp_flags, NULL);
4851 xin.resubmit_stats = &stats;
4852 xin.may_learn = may_learn;
4853 xlate_actions_for_side_effects(&xin);
4858 push_all_stats__(bool run_fast)
4860 static long long int rl = LLONG_MIN;
4861 struct ofproto_dpif *ofproto;
4863 if (time_msec() < rl) {
4867 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
4868 struct cls_cursor cursor;
4869 struct facet *facet;
4871 cls_cursor_init(&cursor, &ofproto->facets, NULL);
4872 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
4873 facet_push_stats(facet, false);
4880 rl = time_msec() + 100;
4884 push_all_stats(void)
4886 push_all_stats__(true);
4890 rule_credit_stats(struct rule_dpif *rule, const struct dpif_flow_stats *stats)
4892 rule->packet_count += stats->n_packets;
4893 rule->byte_count += stats->n_bytes;
4894 ofproto_rule_update_used(&rule->up, stats->used);
4899 static struct subfacet *
4900 subfacet_find(struct dpif_backer *backer, const struct nlattr *key,
4901 size_t key_len, uint32_t key_hash)
4903 struct subfacet *subfacet;
4905 HMAP_FOR_EACH_WITH_HASH (subfacet, hmap_node, key_hash,
4906 &backer->subfacets) {
4907 if (subfacet->key_len == key_len
4908 && !memcmp(key, subfacet->key, key_len)) {
4916 /* Searches 'facet' (within 'ofproto') for a subfacet with the specified
4917 * 'key_fitness', 'key', and 'key_len' members in 'miss'. Returns the
4918 * existing subfacet if there is one, otherwise creates and returns a
4920 static struct subfacet *
4921 subfacet_create(struct facet *facet, struct flow_miss *miss,
4924 struct dpif_backer *backer = miss->ofproto->backer;
4925 enum odp_key_fitness key_fitness = miss->key_fitness;
4926 const struct nlattr *key = miss->key;
4927 size_t key_len = miss->key_len;
4929 struct subfacet *subfacet;
4931 key_hash = odp_flow_key_hash(key, key_len);
4933 if (list_is_empty(&facet->subfacets)) {
4934 subfacet = &facet->one_subfacet;
4936 subfacet = subfacet_find(backer, key, key_len, key_hash);
4938 if (subfacet->facet == facet) {
4942 /* This shouldn't happen. */
4943 VLOG_ERR_RL(&rl, "subfacet with wrong facet");
4944 subfacet_destroy(subfacet);
4947 subfacet = xmalloc(sizeof *subfacet);
4950 hmap_insert(&backer->subfacets, &subfacet->hmap_node, key_hash);
4951 list_push_back(&facet->subfacets, &subfacet->list_node);
4952 subfacet->facet = facet;
4953 subfacet->key_fitness = key_fitness;
4954 subfacet->key = xmemdup(key, key_len);
4955 subfacet->key_len = key_len;
4956 subfacet->used = now;
4957 subfacet->created = now;
4958 subfacet->dp_packet_count = 0;
4959 subfacet->dp_byte_count = 0;
4960 subfacet->path = SF_NOT_INSTALLED;
4961 subfacet->backer = backer;
4963 backer->subfacet_add_count++;
4967 /* Uninstalls 'subfacet' from the datapath, if it is installed, removes it from
4968 * its facet within 'ofproto', and frees it. */
4970 subfacet_destroy__(struct subfacet *subfacet)
4972 struct facet *facet = subfacet->facet;
4973 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
4975 /* Update ofproto stats before uninstall the subfacet. */
4976 ofproto->backer->subfacet_del_count++;
4978 subfacet_uninstall(subfacet);
4979 hmap_remove(&subfacet->backer->subfacets, &subfacet->hmap_node);
4980 list_remove(&subfacet->list_node);
4981 free(subfacet->key);
4982 if (subfacet != &facet->one_subfacet) {
4987 /* Destroys 'subfacet', as with subfacet_destroy__(), and then if this was the
4988 * last remaining subfacet in its facet destroys the facet too. */
4990 subfacet_destroy(struct subfacet *subfacet)
4992 struct facet *facet = subfacet->facet;
4994 if (list_is_singleton(&facet->subfacets)) {
4995 /* facet_remove() needs at least one subfacet (it will remove it). */
4996 facet_remove(facet);
4998 subfacet_destroy__(subfacet);
5003 subfacet_destroy_batch(struct dpif_backer *backer,
5004 struct subfacet **subfacets, int n)
5006 struct dpif_op ops[SUBFACET_DESTROY_MAX_BATCH];
5007 struct dpif_op *opsp[SUBFACET_DESTROY_MAX_BATCH];
5008 struct dpif_flow_stats stats[SUBFACET_DESTROY_MAX_BATCH];
5011 for (i = 0; i < n; i++) {
5012 ops[i].type = DPIF_OP_FLOW_DEL;
5013 ops[i].u.flow_del.key = subfacets[i]->key;
5014 ops[i].u.flow_del.key_len = subfacets[i]->key_len;
5015 ops[i].u.flow_del.stats = &stats[i];
5019 dpif_operate(backer->dpif, opsp, n);
5020 for (i = 0; i < n; i++) {
5021 subfacet_reset_dp_stats(subfacets[i], &stats[i]);
5022 subfacets[i]->path = SF_NOT_INSTALLED;
5023 subfacet_destroy(subfacets[i]);
5028 /* Updates 'subfacet''s datapath flow, setting its actions to 'actions_len'
5029 * bytes of actions in 'actions'. If 'stats' is non-null, statistics counters
5030 * in the datapath will be zeroed and 'stats' will be updated with traffic new
5031 * since 'subfacet' was last updated.
5033 * Returns 0 if successful, otherwise a positive errno value. */
5035 subfacet_install(struct subfacet *subfacet, const struct ofpbuf *odp_actions,
5036 struct dpif_flow_stats *stats)
5038 struct facet *facet = subfacet->facet;
5039 struct ofproto_dpif *ofproto = ofproto_dpif_cast(facet->rule->up.ofproto);
5040 enum subfacet_path path = facet->xout.slow ? SF_SLOW_PATH : SF_FAST_PATH;
5041 const struct nlattr *actions = odp_actions->data;
5042 size_t actions_len = odp_actions->size;
5043 struct odputil_keybuf maskbuf;
5046 uint64_t slow_path_stub[128 / 8];
5047 enum dpif_flow_put_flags flags;
5050 flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
5052 flags |= DPIF_FP_ZERO_STATS;
5055 if (path == SF_SLOW_PATH) {
5056 compose_slow_path(ofproto, &facet->flow, facet->xout.slow,
5057 slow_path_stub, sizeof slow_path_stub,
5058 &actions, &actions_len);
5061 ofpbuf_use_stack(&mask, &maskbuf, sizeof maskbuf);
5062 odp_flow_key_from_mask(&mask, &facet->xout.wc.masks,
5063 &facet->flow, UINT32_MAX);
5065 ret = dpif_flow_put(subfacet->backer->dpif, flags, subfacet->key,
5066 subfacet->key_len, mask.data, mask.size,
5067 actions, actions_len, stats);
5070 subfacet_reset_dp_stats(subfacet, stats);
5074 COVERAGE_INC(subfacet_install_fail);
5076 subfacet->path = path;
5081 /* If 'subfacet' is installed in the datapath, uninstalls it. */
5083 subfacet_uninstall(struct subfacet *subfacet)
5085 if (subfacet->path != SF_NOT_INSTALLED) {
5086 struct rule_dpif *rule = subfacet->facet->rule;
5087 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5088 struct dpif_flow_stats stats;
5091 error = dpif_flow_del(ofproto->backer->dpif, subfacet->key,
5092 subfacet->key_len, &stats);
5093 subfacet_reset_dp_stats(subfacet, &stats);
5095 subfacet_update_stats(subfacet, &stats);
5097 subfacet->path = SF_NOT_INSTALLED;
5099 ovs_assert(subfacet->dp_packet_count == 0);
5100 ovs_assert(subfacet->dp_byte_count == 0);
5104 /* Resets 'subfacet''s datapath statistics counters. This should be called
5105 * when 'subfacet''s statistics are cleared in the datapath. If 'stats' is
5106 * non-null, it should contain the statistics returned by dpif when 'subfacet'
5107 * was reset in the datapath. 'stats' will be modified to include only
5108 * statistics new since 'subfacet' was last updated. */
5110 subfacet_reset_dp_stats(struct subfacet *subfacet,
5111 struct dpif_flow_stats *stats)
5114 && subfacet->dp_packet_count <= stats->n_packets
5115 && subfacet->dp_byte_count <= stats->n_bytes) {
5116 stats->n_packets -= subfacet->dp_packet_count;
5117 stats->n_bytes -= subfacet->dp_byte_count;
5120 subfacet->dp_packet_count = 0;
5121 subfacet->dp_byte_count = 0;
5124 /* Folds the statistics from 'stats' into the counters in 'subfacet'.
5126 * Because of the meaning of a subfacet's counters, it only makes sense to do
5127 * this if 'stats' are not tracked in the datapath, that is, if 'stats'
5128 * represents a packet that was sent by hand or if it represents statistics
5129 * that have been cleared out of the datapath. */
5131 subfacet_update_stats(struct subfacet *subfacet,
5132 const struct dpif_flow_stats *stats)
5134 if (stats->n_packets || stats->used > subfacet->used) {
5135 struct facet *facet = subfacet->facet;
5137 subfacet->used = MAX(subfacet->used, stats->used);
5138 facet->used = MAX(facet->used, stats->used);
5139 facet->packet_count += stats->n_packets;
5140 facet->byte_count += stats->n_bytes;
5141 facet->tcp_flags |= stats->tcp_flags;
5147 /* Lookup 'flow' in 'ofproto''s classifier. If 'wc' is non-null, sets
5148 * the fields that were relevant as part of the lookup. */
5149 static struct rule_dpif *
5150 rule_dpif_lookup(struct ofproto_dpif *ofproto, const struct flow *flow,
5151 struct flow_wildcards *wc)
5153 struct rule_dpif *rule;
5155 rule = rule_dpif_lookup_in_table(ofproto, flow, wc, 0);
5160 return rule_dpif_miss_rule(ofproto, flow);
5164 rule_dpif_lookup_in_table(struct ofproto_dpif *ofproto,
5165 const struct flow *flow, struct flow_wildcards *wc,
5168 struct cls_rule *cls_rule;
5169 struct classifier *cls;
5172 if (table_id >= N_TABLES) {
5177 memset(&wc->masks.dl_type, 0xff, sizeof wc->masks.dl_type);
5178 wc->masks.nw_frag |= FLOW_NW_FRAG_MASK;
5181 cls = &ofproto->up.tables[table_id].cls;
5182 frag = (flow->nw_frag & FLOW_NW_FRAG_ANY) != 0;
5183 if (frag && ofproto->up.frag_handling == OFPC_FRAG_NORMAL) {
5184 /* We must pretend that transport ports are unavailable. */
5185 struct flow ofpc_normal_flow = *flow;
5186 ofpc_normal_flow.tp_src = htons(0);
5187 ofpc_normal_flow.tp_dst = htons(0);
5188 cls_rule = classifier_lookup(cls, &ofpc_normal_flow, wc);
5189 } else if (frag && ofproto->up.frag_handling == OFPC_FRAG_DROP) {
5190 cls_rule = &ofproto->drop_frags_rule->up.cr;
5192 flow_wildcards_init_exact(wc);
5195 cls_rule = classifier_lookup(cls, flow, wc);
5197 return rule_dpif_cast(rule_from_cls_rule(cls_rule));
5201 rule_dpif_miss_rule(struct ofproto_dpif *ofproto, const struct flow *flow)
5203 struct ofport_dpif *port;
5205 port = get_ofp_port(ofproto, flow->in_port.ofp_port);
5207 VLOG_WARN_RL(&rl, "packet-in on unknown OpenFlow port %"PRIu16,
5208 flow->in_port.ofp_port);
5209 return ofproto->miss_rule;
5212 if (port->up.pp.config & OFPUTIL_PC_NO_PACKET_IN) {
5213 return ofproto->no_packet_in_rule;
5215 return ofproto->miss_rule;
5219 complete_operation(struct rule_dpif *rule)
5221 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5223 rule_invalidate(rule);
5225 struct dpif_completion *c = xmalloc(sizeof *c);
5226 c->op = rule->up.pending;
5227 list_push_back(&ofproto->completions, &c->list_node);
5229 ofoperation_complete(rule->up.pending, 0);
5233 static struct rule *
5236 struct rule_dpif *rule = xmalloc(sizeof *rule);
5241 rule_dealloc(struct rule *rule_)
5243 struct rule_dpif *rule = rule_dpif_cast(rule_);
5248 rule_construct(struct rule *rule_)
5250 struct rule_dpif *rule = rule_dpif_cast(rule_);
5251 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5252 struct rule_dpif *victim;
5255 rule->packet_count = 0;
5256 rule->byte_count = 0;
5258 victim = rule_dpif_cast(ofoperation_get_victim(rule->up.pending));
5259 if (victim && !list_is_empty(&victim->facets)) {
5260 struct facet *facet;
5262 rule->facets = victim->facets;
5263 list_moved(&rule->facets);
5264 LIST_FOR_EACH (facet, list_node, &rule->facets) {
5265 /* XXX: We're only clearing our local counters here. It's possible
5266 * that quite a few packets are unaccounted for in the datapath
5267 * statistics. These will be accounted to the new rule instead of
5268 * cleared as required. This could be fixed by clearing out the
5269 * datapath statistics for this facet, but currently it doesn't
5271 facet_reset_counters(facet);
5275 /* Must avoid list_moved() in this case. */
5276 list_init(&rule->facets);
5279 table_id = rule->up.table_id;
5281 rule->tag = victim->tag;
5282 } else if (table_id == 0) {
5287 miniflow_expand(&rule->up.cr.match.flow, &flow);
5288 rule->tag = rule_calculate_tag(&flow, &rule->up.cr.match.mask,
5289 ofproto->tables[table_id].basis);
5292 complete_operation(rule);
5297 rule_destruct(struct rule *rule_)
5299 struct rule_dpif *rule = rule_dpif_cast(rule_);
5300 struct facet *facet, *next_facet;
5302 LIST_FOR_EACH_SAFE (facet, next_facet, list_node, &rule->facets) {
5303 facet_revalidate(facet);
5306 complete_operation(rule);
5310 rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
5312 struct rule_dpif *rule = rule_dpif_cast(rule_);
5314 /* push_all_stats() can handle flow misses which, when using the learn
5315 * action, can cause rules to be added and deleted. This can corrupt our
5316 * caller's datastructures which assume that rule_get_stats() doesn't have
5317 * an impact on the flow table. To be safe, we disable miss handling. */
5318 push_all_stats__(false);
5320 /* Start from historical data for 'rule' itself that are no longer tracked
5321 * in facets. This counts, for example, facets that have expired. */
5322 *packets = rule->packet_count;
5323 *bytes = rule->byte_count;
5327 rule_dpif_execute(struct rule_dpif *rule, const struct flow *flow,
5328 struct ofpbuf *packet)
5330 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5331 struct dpif_flow_stats stats;
5332 struct xlate_out xout;
5333 struct xlate_in xin;
5335 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
5336 rule_credit_stats(rule, &stats);
5338 xlate_in_init(&xin, ofproto, flow, rule, stats.tcp_flags, packet);
5339 xin.resubmit_stats = &stats;
5340 xlate_actions(&xin, &xout);
5342 execute_odp_actions(ofproto, flow, xout.odp_actions.data,
5343 xout.odp_actions.size, packet);
5345 xlate_out_uninit(&xout);
5349 rule_execute(struct rule *rule, const struct flow *flow,
5350 struct ofpbuf *packet)
5352 rule_dpif_execute(rule_dpif_cast(rule), flow, packet);
5353 ofpbuf_delete(packet);
5358 rule_modify_actions(struct rule *rule_)
5360 struct rule_dpif *rule = rule_dpif_cast(rule_);
5362 complete_operation(rule);
5365 /* Sends 'packet' out 'ofport'.
5366 * May modify 'packet'.
5367 * Returns 0 if successful, otherwise a positive errno value. */
5369 send_packet(const struct ofport_dpif *ofport, struct ofpbuf *packet)
5371 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport->up.ofproto);
5372 uint64_t odp_actions_stub[1024 / 8];
5373 struct ofpbuf key, odp_actions;
5374 struct dpif_flow_stats stats;
5375 struct odputil_keybuf keybuf;
5376 struct ofpact_output output;
5377 struct xlate_out xout;
5378 struct xlate_in xin;
5380 union flow_in_port in_port_;
5383 ofpbuf_use_stub(&odp_actions, odp_actions_stub, sizeof odp_actions_stub);
5384 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5386 /* Use OFPP_NONE as the in_port to avoid special packet processing. */
5387 in_port_.ofp_port = OFPP_NONE;
5388 flow_extract(packet, 0, 0, NULL, &in_port_, &flow);
5389 odp_flow_key_from_flow(&key, &flow, ofp_port_to_odp_port(ofproto,
5391 dpif_flow_stats_extract(&flow, packet, time_msec(), &stats);
5393 ofpact_init(&output.ofpact, OFPACT_OUTPUT, sizeof output);
5394 output.port = ofport->up.ofp_port;
5397 xlate_in_init(&xin, ofproto, &flow, NULL, 0, packet);
5398 xin.ofpacts_len = sizeof output;
5399 xin.ofpacts = &output.ofpact;
5400 xin.resubmit_stats = &stats;
5401 xlate_actions(&xin, &xout);
5403 error = dpif_execute(ofproto->backer->dpif,
5405 xout.odp_actions.data, xout.odp_actions.size,
5407 xlate_out_uninit(&xout);
5410 VLOG_WARN_RL(&rl, "%s: failed to send packet on port %s (%s)",
5411 ofproto->up.name, netdev_get_name(ofport->up.netdev),
5415 ofproto->stats.tx_packets++;
5416 ofproto->stats.tx_bytes += packet->size;
5420 /* Composes an ODP action for a "slow path" action for 'flow' within 'ofproto'.
5421 * The action will state 'slow' as the reason that the action is in the slow
5422 * path. (This is purely informational: it allows a human viewing "ovs-dpctl
5423 * dump-flows" output to see why a flow is in the slow path.)
5425 * The 'stub_size' bytes in 'stub' will be used to store the action.
5426 * 'stub_size' must be large enough for the action.
5428 * The action and its size will be stored in '*actionsp' and '*actions_lenp',
5431 compose_slow_path(const struct ofproto_dpif *ofproto, const struct flow *flow,
5432 enum slow_path_reason slow,
5433 uint64_t *stub, size_t stub_size,
5434 const struct nlattr **actionsp, size_t *actions_lenp)
5436 union user_action_cookie cookie;
5439 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
5440 cookie.slow_path.unused = 0;
5441 cookie.slow_path.reason = slow;
5443 ofpbuf_use_stack(&buf, stub, stub_size);
5444 if (slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)) {
5445 uint32_t pid = dpif_port_get_pid(ofproto->backer->dpif,
5447 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, &buf);
5449 put_userspace_action(ofproto, &buf, flow, &cookie,
5450 sizeof cookie.slow_path);
5452 *actionsp = buf.data;
5453 *actions_lenp = buf.size;
5457 put_userspace_action(const struct ofproto_dpif *ofproto,
5458 struct ofpbuf *odp_actions,
5459 const struct flow *flow,
5460 const union user_action_cookie *cookie,
5461 const size_t cookie_size)
5465 pid = dpif_port_get_pid(ofproto->backer->dpif,
5466 ofp_port_to_odp_port(ofproto,
5467 flow->in_port.ofp_port));
5469 return odp_put_userspace_action(pid, cookie, cookie_size, odp_actions);
5474 update_mirror_stats(struct ofproto_dpif *ofproto, mirror_mask_t mirrors,
5475 uint64_t packets, uint64_t bytes)
5481 for (; mirrors; mirrors = zero_rightmost_1bit(mirrors)) {
5484 m = ofproto->mirrors[mirror_mask_ffs(mirrors) - 1];
5487 /* In normal circumstances 'm' will not be NULL. However,
5488 * if mirrors are reconfigured, we can temporarily get out
5489 * of sync in facet_revalidate(). We could "correct" the
5490 * mirror list before reaching here, but doing that would
5491 * not properly account the traffic stats we've currently
5492 * accumulated for previous mirror configuration. */
5496 m->packet_count += packets;
5497 m->byte_count += bytes;
5502 /* Optimized flow revalidation.
5504 * It's a difficult problem, in general, to tell which facets need to have
5505 * their actions recalculated whenever the OpenFlow flow table changes. We
5506 * don't try to solve that general problem: for most kinds of OpenFlow flow
5507 * table changes, we recalculate the actions for every facet. This is
5508 * relatively expensive, but it's good enough if the OpenFlow flow table
5509 * doesn't change very often.
5511 * However, we can expect one particular kind of OpenFlow flow table change to
5512 * happen frequently: changes caused by MAC learning. To avoid wasting a lot
5513 * of CPU on revalidating every facet whenever MAC learning modifies the flow
5514 * table, we add a special case that applies to flow tables in which every rule
5515 * has the same form (that is, the same wildcards), except that the table is
5516 * also allowed to have a single "catch-all" flow that matches all packets. We
5517 * optimize this case by tagging all of the facets that resubmit into the table
5518 * and invalidating the same tag whenever a flow changes in that table. The
5519 * end result is that we revalidate just the facets that need it (and sometimes
5520 * a few more, but not all of the facets or even all of the facets that
5521 * resubmit to the table modified by MAC learning). */
5523 /* Calculates the tag to use for 'flow' and mask 'mask' when it is inserted
5524 * into an OpenFlow table with the given 'basis'. */
5526 rule_calculate_tag(const struct flow *flow, const struct minimask *mask,
5529 if (minimask_is_catchall(mask)) {
5532 uint32_t hash = flow_hash_in_minimask(flow, mask, secret);
5533 return tag_create_deterministic(hash);
5537 /* Following a change to OpenFlow table 'table_id' in 'ofproto', update the
5538 * taggability of that table.
5540 * This function must be called after *each* change to a flow table. If you
5541 * skip calling it on some changes then the pointer comparisons at the end can
5542 * be invalid if you get unlucky. For example, if a flow removal causes a
5543 * cls_table to be destroyed and then a flow insertion causes a cls_table with
5544 * different wildcards to be created with the same address, then this function
5545 * will incorrectly skip revalidation. */
5547 table_update_taggable(struct ofproto_dpif *ofproto, uint8_t table_id)
5549 struct table_dpif *table = &ofproto->tables[table_id];
5550 const struct oftable *oftable = &ofproto->up.tables[table_id];
5551 struct cls_table *catchall, *other;
5552 struct cls_table *t;
5554 catchall = other = NULL;
5556 switch (hmap_count(&oftable->cls.tables)) {
5558 /* We could tag this OpenFlow table but it would make the logic a
5559 * little harder and it's a corner case that doesn't seem worth it
5565 HMAP_FOR_EACH (t, hmap_node, &oftable->cls.tables) {
5566 if (cls_table_is_catchall(t)) {
5568 } else if (!other) {
5571 /* Indicate that we can't tag this by setting both tables to
5572 * NULL. (We know that 'catchall' is already NULL.) */
5579 /* Can't tag this table. */
5583 if (table->catchall_table != catchall || table->other_table != other) {
5584 table->catchall_table = catchall;
5585 table->other_table = other;
5586 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
5590 /* Given 'rule' that has changed in some way (either it is a rule being
5591 * inserted, a rule being deleted, or a rule whose actions are being
5592 * modified), marks facets for revalidation to ensure that packets will be
5593 * forwarded correctly according to the new state of the flow table.
5595 * This function must be called after *each* change to a flow table. See
5596 * the comment on table_update_taggable() for more information. */
5598 rule_invalidate(const struct rule_dpif *rule)
5600 struct ofproto_dpif *ofproto = ofproto_dpif_cast(rule->up.ofproto);
5602 table_update_taggable(ofproto, rule->up.table_id);
5604 if (!ofproto->backer->need_revalidate) {
5605 struct table_dpif *table = &ofproto->tables[rule->up.table_id];
5607 if (table->other_table && rule->tag) {
5608 tag_set_add(&ofproto->backer->revalidate_set, rule->tag);
5610 ofproto->backer->need_revalidate = REV_FLOW_TABLE;
5616 set_frag_handling(struct ofproto *ofproto_,
5617 enum ofp_config_flags frag_handling)
5619 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5620 if (frag_handling != OFPC_FRAG_REASM) {
5621 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5629 packet_out(struct ofproto *ofproto_, struct ofpbuf *packet,
5630 const struct flow *flow,
5631 const struct ofpact *ofpacts, size_t ofpacts_len)
5633 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5634 struct odputil_keybuf keybuf;
5635 struct dpif_flow_stats stats;
5636 struct xlate_out xout;
5637 struct xlate_in xin;
5641 ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
5642 odp_flow_key_from_flow(&key, flow,
5643 ofp_port_to_odp_port(ofproto,
5644 flow->in_port.ofp_port));
5646 dpif_flow_stats_extract(flow, packet, time_msec(), &stats);
5648 xlate_in_init(&xin, ofproto, flow, NULL, stats.tcp_flags, packet);
5649 xin.resubmit_stats = &stats;
5650 xin.ofpacts_len = ofpacts_len;
5651 xin.ofpacts = ofpacts;
5653 xlate_actions(&xin, &xout);
5654 dpif_execute(ofproto->backer->dpif, key.data, key.size,
5655 xout.odp_actions.data, xout.odp_actions.size, packet);
5656 xlate_out_uninit(&xout);
5664 set_netflow(struct ofproto *ofproto_,
5665 const struct netflow_options *netflow_options)
5667 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5669 if (netflow_options) {
5670 if (!ofproto->netflow) {
5671 ofproto->netflow = netflow_create();
5672 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5674 return netflow_set_options(ofproto->netflow, netflow_options);
5675 } else if (ofproto->netflow) {
5676 ofproto->backer->need_revalidate = REV_RECONFIGURE;
5677 netflow_destroy(ofproto->netflow);
5678 ofproto->netflow = NULL;
5685 get_netflow_ids(const struct ofproto *ofproto_,
5686 uint8_t *engine_type, uint8_t *engine_id)
5688 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofproto_);
5690 dpif_get_netflow_ids(ofproto->backer->dpif, engine_type, engine_id);
5694 send_active_timeout(struct ofproto_dpif *ofproto, struct facet *facet)
5696 if (!facet_is_controller_flow(facet) &&
5697 netflow_active_timeout_expired(ofproto->netflow, &facet->nf_flow)) {
5698 struct subfacet *subfacet;
5699 struct ofexpired expired;
5701 LIST_FOR_EACH (subfacet, list_node, &facet->subfacets) {
5702 if (subfacet->path == SF_FAST_PATH) {
5703 struct dpif_flow_stats stats;
5705 subfacet_install(subfacet, &facet->xout.odp_actions,
5707 subfacet_update_stats(subfacet, &stats);
5711 expired.flow = facet->flow;
5712 expired.packet_count = facet->packet_count;
5713 expired.byte_count = facet->byte_count;
5714 expired.used = facet->used;
5715 netflow_expire(ofproto->netflow, &facet->nf_flow, &expired);
5720 send_netflow_active_timeouts(struct ofproto_dpif *ofproto)
5722 struct cls_cursor cursor;
5723 struct facet *facet;
5725 cls_cursor_init(&cursor, &ofproto->facets, NULL);
5726 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
5727 send_active_timeout(ofproto, facet);
5731 static struct ofproto_dpif *
5732 ofproto_dpif_lookup(const char *name)
5734 struct ofproto_dpif *ofproto;
5736 HMAP_FOR_EACH_WITH_HASH (ofproto, all_ofproto_dpifs_node,
5737 hash_string(name, 0), &all_ofproto_dpifs) {
5738 if (!strcmp(ofproto->up.name, name)) {
5746 ofproto_unixctl_fdb_flush(struct unixctl_conn *conn, int argc,
5747 const char *argv[], void *aux OVS_UNUSED)
5749 struct ofproto_dpif *ofproto;
5752 ofproto = ofproto_dpif_lookup(argv[1]);
5754 unixctl_command_reply_error(conn, "no such bridge");
5757 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
5759 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
5760 mac_learning_flush(ofproto->ml, &ofproto->backer->revalidate_set);
5764 unixctl_command_reply(conn, "table successfully flushed");
5768 ofproto_unixctl_fdb_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
5769 const char *argv[], void *aux OVS_UNUSED)
5771 struct ds ds = DS_EMPTY_INITIALIZER;
5772 const struct ofproto_dpif *ofproto;
5773 const struct mac_entry *e;
5775 ofproto = ofproto_dpif_lookup(argv[1]);
5777 unixctl_command_reply_error(conn, "no such bridge");
5781 ds_put_cstr(&ds, " port VLAN MAC Age\n");
5782 LIST_FOR_EACH (e, lru_node, &ofproto->ml->lrus) {
5783 struct ofbundle *bundle = e->port.p;
5784 char name[OFP_MAX_PORT_NAME_LEN];
5786 ofputil_port_to_string(ofbundle_get_a_port(bundle)->up.ofp_port,
5788 ds_put_format(&ds, "%5s %4d "ETH_ADDR_FMT" %3d\n",
5789 name, e->vlan, ETH_ADDR_ARGS(e->mac),
5790 mac_entry_age(ofproto->ml, e));
5792 unixctl_command_reply(conn, ds_cstr(&ds));
5797 struct xlate_out xout;
5798 struct xlate_in xin;
5804 trace_format_rule(struct ds *result, int level, const struct rule_dpif *rule)
5806 ds_put_char_multiple(result, '\t', level);
5808 ds_put_cstr(result, "No match\n");
5812 ds_put_format(result, "Rule: table=%"PRIu8" cookie=%#"PRIx64" ",
5813 rule ? rule->up.table_id : 0, ntohll(rule->up.flow_cookie));
5814 cls_rule_format(&rule->up.cr, result);
5815 ds_put_char(result, '\n');
5817 ds_put_char_multiple(result, '\t', level);
5818 ds_put_cstr(result, "OpenFlow ");
5819 ofpacts_format(rule->up.ofpacts, rule->up.ofpacts_len, result);
5820 ds_put_char(result, '\n');
5824 trace_format_flow(struct ds *result, int level, const char *title,
5825 struct trace_ctx *trace)
5827 ds_put_char_multiple(result, '\t', level);
5828 ds_put_format(result, "%s: ", title);
5829 if (flow_equal(&trace->xin.flow, &trace->flow)) {
5830 ds_put_cstr(result, "unchanged");
5832 flow_format(result, &trace->xin.flow);
5833 trace->flow = trace->xin.flow;
5835 ds_put_char(result, '\n');
5839 trace_format_regs(struct ds *result, int level, const char *title,
5840 struct trace_ctx *trace)
5844 ds_put_char_multiple(result, '\t', level);
5845 ds_put_format(result, "%s:", title);
5846 for (i = 0; i < FLOW_N_REGS; i++) {
5847 ds_put_format(result, " reg%zu=0x%"PRIx32, i, trace->flow.regs[i]);
5849 ds_put_char(result, '\n');
5853 trace_format_odp(struct ds *result, int level, const char *title,
5854 struct trace_ctx *trace)
5856 struct ofpbuf *odp_actions = &trace->xout.odp_actions;
5858 ds_put_char_multiple(result, '\t', level);
5859 ds_put_format(result, "%s: ", title);
5860 format_odp_actions(result, odp_actions->data, odp_actions->size);
5861 ds_put_char(result, '\n');
5865 trace_resubmit(struct xlate_in *xin, struct rule_dpif *rule, int recurse)
5867 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
5868 struct ds *result = trace->result;
5870 ds_put_char(result, '\n');
5871 trace_format_flow(result, recurse + 1, "Resubmitted flow", trace);
5872 trace_format_regs(result, recurse + 1, "Resubmitted regs", trace);
5873 trace_format_odp(result, recurse + 1, "Resubmitted odp", trace);
5874 trace_format_rule(result, recurse + 1, rule);
5878 trace_report(struct xlate_in *xin, const char *s, int recurse)
5880 struct trace_ctx *trace = CONTAINER_OF(xin, struct trace_ctx, xin);
5881 struct ds *result = trace->result;
5883 ds_put_char_multiple(result, '\t', recurse);
5884 ds_put_cstr(result, s);
5885 ds_put_char(result, '\n');
5889 ofproto_unixctl_trace(struct unixctl_conn *conn, int argc, const char *argv[],
5890 void *aux OVS_UNUSED)
5892 const struct dpif_backer *backer;
5893 struct ofproto_dpif *ofproto;
5894 struct ofpbuf odp_key;
5895 struct ofpbuf *packet;
5903 ofpbuf_init(&odp_key, 0);
5905 /* Handle "-generate" or a hex string as the last argument. */
5906 if (!strcmp(argv[argc - 1], "-generate")) {
5907 packet = ofpbuf_new(0);
5910 const char *error = eth_from_hex(argv[argc - 1], &packet);
5913 } else if (argc == 4) {
5914 /* The 3-argument form must end in "-generate' or a hex string. */
5915 unixctl_command_reply_error(conn, error);
5920 /* Parse the flow and determine whether a datapath or
5921 * bridge is specified. If function odp_flow_key_from_string()
5922 * returns 0, the flow is a odp_flow. If function
5923 * parse_ofp_exact_flow() returns 0, the flow is a br_flow. */
5924 if (!odp_flow_from_string(argv[argc - 1], NULL, &odp_key, NULL)) {
5925 /* If the odp_flow is the second argument,
5926 * the datapath name is the first argument. */
5928 const char *dp_type;
5929 if (!strncmp(argv[1], "ovs-", 4)) {
5930 dp_type = argv[1] + 4;
5934 backer = shash_find_data(&all_dpif_backers, dp_type);
5936 unixctl_command_reply_error(conn, "Cannot find datapath "
5941 /* No datapath name specified, so there should be only one
5943 struct shash_node *node;
5944 if (shash_count(&all_dpif_backers) != 1) {
5945 unixctl_command_reply_error(conn, "Must specify datapath "
5946 "name, there is more than one type of datapath");
5949 node = shash_first(&all_dpif_backers);
5950 backer = node->data;
5953 /* Extract the ofproto_dpif object from the ofproto_receive()
5955 if (ofproto_receive(backer, NULL, odp_key.data,
5956 odp_key.size, &flow, NULL, &ofproto, NULL)) {
5957 unixctl_command_reply_error(conn, "Invalid datapath flow");
5960 ds_put_format(&result, "Bridge: %s\n", ofproto->up.name);
5961 } else if (!parse_ofp_exact_flow(&flow, argv[argc - 1])) {
5963 unixctl_command_reply_error(conn, "Must specify bridge name");
5967 ofproto = ofproto_dpif_lookup(argv[1]);
5969 unixctl_command_reply_error(conn, "Unknown bridge name");
5973 unixctl_command_reply_error(conn, "Bad flow syntax");
5977 /* Generate a packet, if requested. */
5979 if (!packet->size) {
5980 flow_compose(packet, &flow);
5982 union flow_in_port in_port_;
5984 in_port_ = flow.in_port;
5985 ds_put_cstr(&result, "Packet: ");
5986 s = ofp_packet_to_string(packet->data, packet->size);
5987 ds_put_cstr(&result, s);
5990 /* Use the metadata from the flow and the packet argument
5991 * to reconstruct the flow. */
5992 flow_extract(packet, flow.skb_priority, flow.skb_mark, NULL,
5997 ofproto_trace(ofproto, &flow, packet, &result);
5998 unixctl_command_reply(conn, ds_cstr(&result));
6001 ds_destroy(&result);
6002 ofpbuf_delete(packet);
6003 ofpbuf_uninit(&odp_key);
6007 ofproto_trace(struct ofproto_dpif *ofproto, const struct flow *flow,
6008 const struct ofpbuf *packet, struct ds *ds)
6010 struct rule_dpif *rule;
6012 ds_put_cstr(ds, "Flow: ");
6013 flow_format(ds, flow);
6014 ds_put_char(ds, '\n');
6016 rule = rule_dpif_lookup(ofproto, flow, NULL);
6018 trace_format_rule(ds, 0, rule);
6019 if (rule == ofproto->miss_rule) {
6020 ds_put_cstr(ds, "\nNo match, flow generates \"packet in\"s.\n");
6021 } else if (rule == ofproto->no_packet_in_rule) {
6022 ds_put_cstr(ds, "\nNo match, packets dropped because "
6023 "OFPPC_NO_PACKET_IN is set on in_port.\n");
6024 } else if (rule == ofproto->drop_frags_rule) {
6025 ds_put_cstr(ds, "\nPackets dropped because they are IP fragments "
6026 "and the fragment handling mode is \"drop\".\n");
6030 uint64_t odp_actions_stub[1024 / 8];
6031 struct ofpbuf odp_actions;
6032 struct trace_ctx trace;
6036 tcp_flags = packet ? packet_get_tcp_flags(packet, flow) : 0;
6039 ofpbuf_use_stub(&odp_actions,
6040 odp_actions_stub, sizeof odp_actions_stub);
6041 xlate_in_init(&trace.xin, ofproto, flow, rule, tcp_flags, packet);
6042 trace.xin.resubmit_hook = trace_resubmit;
6043 trace.xin.report_hook = trace_report;
6045 xlate_actions(&trace.xin, &trace.xout);
6047 ds_put_char(ds, '\n');
6048 trace_format_flow(ds, 0, "Final flow", &trace);
6050 match_init(&match, flow, &trace.xout.wc);
6051 ds_put_cstr(ds, "Relevant fields: ");
6052 match_format(&match, ds, OFP_DEFAULT_PRIORITY);
6053 ds_put_char(ds, '\n');
6055 ds_put_cstr(ds, "Datapath actions: ");
6056 format_odp_actions(ds, trace.xout.odp_actions.data,
6057 trace.xout.odp_actions.size);
6059 if (trace.xout.slow) {
6060 ds_put_cstr(ds, "\nThis flow is handled by the userspace "
6061 "slow path because it:");
6062 switch (trace.xout.slow) {
6064 ds_put_cstr(ds, "\n\t- Consists of CFM packets.");
6067 ds_put_cstr(ds, "\n\t- Consists of LACP packets.");
6070 ds_put_cstr(ds, "\n\t- Consists of STP packets.");
6073 ds_put_cstr(ds, "\n\t- Consists of BFD packets.");
6075 case SLOW_CONTROLLER:
6076 ds_put_cstr(ds, "\n\t- Sends \"packet-in\" messages "
6077 "to the OpenFlow controller.");
6084 xlate_out_uninit(&trace.xout);
6089 ofproto_dpif_clog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
6090 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6093 unixctl_command_reply(conn, NULL);
6097 ofproto_dpif_unclog(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
6098 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6101 unixctl_command_reply(conn, NULL);
6104 /* Runs a self-check of flow translations in 'ofproto'. Appends a message to
6105 * 'reply' describing the results. */
6107 ofproto_dpif_self_check__(struct ofproto_dpif *ofproto, struct ds *reply)
6109 struct cls_cursor cursor;
6110 struct facet *facet;
6114 cls_cursor_init(&cursor, &ofproto->facets, NULL);
6115 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
6116 if (!facet_check_consistency(facet)) {
6121 ofproto->backer->need_revalidate = REV_INCONSISTENCY;
6125 ds_put_format(reply, "%s: self-check failed (%d errors)\n",
6126 ofproto->up.name, errors);
6128 ds_put_format(reply, "%s: self-check passed\n", ofproto->up.name);
6133 ofproto_dpif_self_check(struct unixctl_conn *conn,
6134 int argc, const char *argv[], void *aux OVS_UNUSED)
6136 struct ds reply = DS_EMPTY_INITIALIZER;
6137 struct ofproto_dpif *ofproto;
6140 ofproto = ofproto_dpif_lookup(argv[1]);
6142 unixctl_command_reply_error(conn, "Unknown ofproto (use "
6143 "ofproto/list for help)");
6146 ofproto_dpif_self_check__(ofproto, &reply);
6148 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6149 ofproto_dpif_self_check__(ofproto, &reply);
6153 unixctl_command_reply(conn, ds_cstr(&reply));
6157 /* Store the current ofprotos in 'ofproto_shash'. Returns a sorted list
6158 * of the 'ofproto_shash' nodes. It is the responsibility of the caller
6159 * to destroy 'ofproto_shash' and free the returned value. */
6160 static const struct shash_node **
6161 get_ofprotos(struct shash *ofproto_shash)
6163 const struct ofproto_dpif *ofproto;
6165 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6166 char *name = xasprintf("%s@%s", ofproto->up.type, ofproto->up.name);
6167 shash_add_nocopy(ofproto_shash, name, ofproto);
6170 return shash_sort(ofproto_shash);
6174 ofproto_unixctl_dpif_dump_dps(struct unixctl_conn *conn, int argc OVS_UNUSED,
6175 const char *argv[] OVS_UNUSED,
6176 void *aux OVS_UNUSED)
6178 struct ds ds = DS_EMPTY_INITIALIZER;
6179 struct shash ofproto_shash;
6180 const struct shash_node **sorted_ofprotos;
6183 shash_init(&ofproto_shash);
6184 sorted_ofprotos = get_ofprotos(&ofproto_shash);
6185 for (i = 0; i < shash_count(&ofproto_shash); i++) {
6186 const struct shash_node *node = sorted_ofprotos[i];
6187 ds_put_format(&ds, "%s\n", node->name);
6190 shash_destroy(&ofproto_shash);
6191 free(sorted_ofprotos);
6193 unixctl_command_reply(conn, ds_cstr(&ds));
6198 show_dp_rates(struct ds *ds, const char *heading,
6199 const struct avg_subfacet_rates *rates)
6201 ds_put_format(ds, "%s add rate: %5.3f/min, del rate: %5.3f/min\n",
6202 heading, rates->add_rate, rates->del_rate);
6206 dpif_show_backer(const struct dpif_backer *backer, struct ds *ds)
6208 const struct shash_node **ofprotos;
6209 struct ofproto_dpif *ofproto;
6210 struct shash ofproto_shash;
6211 uint64_t n_hit, n_missed;
6212 long long int minutes;
6215 n_hit = n_missed = 0;
6216 HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) {
6217 if (ofproto->backer == backer) {
6218 n_missed += ofproto->n_missed;
6219 n_hit += ofproto->n_hit;
6223 ds_put_format(ds, "%s: hit:%"PRIu64" missed:%"PRIu64"\n",
6224 dpif_name(backer->dpif), n_hit, n_missed);
6225 ds_put_format(ds, "\tflows: cur: %zu, avg: %u, max: %u,"
6226 " life span: %lldms\n", hmap_count(&backer->subfacets),
6227 backer->avg_n_subfacet, backer->max_n_subfacet,
6228 backer->avg_subfacet_life);
6230 minutes = (time_msec() - backer->created) / (1000 * 60);
6231 if (minutes >= 60) {
6232 show_dp_rates(ds, "\thourly avg:", &backer->hourly);
6234 if (minutes >= 60 * 24) {
6235 show_dp_rates(ds, "\tdaily avg:", &backer->daily);
6237 show_dp_rates(ds, "\toverall avg:", &backer->lifetime);
6239 shash_init(&ofproto_shash);
6240 ofprotos = get_ofprotos(&ofproto_shash);
6241 for (i = 0; i < shash_count(&ofproto_shash); i++) {
6242 struct ofproto_dpif *ofproto = ofprotos[i]->data;
6243 const struct shash_node **ports;
6246 if (ofproto->backer != backer) {
6250 ds_put_format(ds, "\t%s: hit:%"PRIu64" missed:%"PRIu64"\n",
6251 ofproto->up.name, ofproto->n_hit, ofproto->n_missed);
6253 ports = shash_sort(&ofproto->up.port_by_name);
6254 for (j = 0; j < shash_count(&ofproto->up.port_by_name); j++) {
6255 const struct shash_node *node = ports[j];
6256 struct ofport *ofport = node->data;
6258 odp_port_t odp_port;
6260 ds_put_format(ds, "\t\t%s %u/", netdev_get_name(ofport->netdev),
6263 odp_port = ofp_port_to_odp_port(ofproto, ofport->ofp_port);
6264 if (odp_port != ODPP_NONE) {
6265 ds_put_format(ds, "%"PRIu32":", odp_port);
6267 ds_put_cstr(ds, "none:");
6270 ds_put_format(ds, " (%s", netdev_get_type(ofport->netdev));
6273 if (!netdev_get_config(ofport->netdev, &config)) {
6274 const struct smap_node **nodes;
6277 nodes = smap_sort(&config);
6278 for (i = 0; i < smap_count(&config); i++) {
6279 const struct smap_node *node = nodes[i];
6280 ds_put_format(ds, "%c %s=%s", i ? ',' : ':',
6281 node->key, node->value);
6285 smap_destroy(&config);
6287 ds_put_char(ds, ')');
6288 ds_put_char(ds, '\n');
6292 shash_destroy(&ofproto_shash);
6297 ofproto_unixctl_dpif_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
6298 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
6300 struct ds ds = DS_EMPTY_INITIALIZER;
6301 const struct shash_node **backers;
6304 backers = shash_sort(&all_dpif_backers);
6305 for (i = 0; i < shash_count(&all_dpif_backers); i++) {
6306 dpif_show_backer(backers[i]->data, &ds);
6310 unixctl_command_reply(conn, ds_cstr(&ds));
6314 /* Dump the megaflow (facet) cache. This is useful to check the
6315 * correctness of flow wildcarding, since the same mechanism is used for
6316 * both xlate caching and kernel wildcarding.
6318 * It's important to note that in the output the flow description uses
6319 * OpenFlow (OFP) ports, but the actions use datapath (ODP) ports.
6321 * This command is only needed for advanced debugging, so it's not
6322 * documented in the man page. */
6324 ofproto_unixctl_dpif_dump_megaflows(struct unixctl_conn *conn,
6325 int argc OVS_UNUSED, const char *argv[],
6326 void *aux OVS_UNUSED)
6328 struct ds ds = DS_EMPTY_INITIALIZER;
6329 const struct ofproto_dpif *ofproto;
6330 long long int now = time_msec();
6331 struct cls_cursor cursor;
6332 struct facet *facet;
6334 ofproto = ofproto_dpif_lookup(argv[1]);
6336 unixctl_command_reply_error(conn, "no such bridge");
6340 cls_cursor_init(&cursor, &ofproto->facets, NULL);
6341 CLS_CURSOR_FOR_EACH (facet, cr, &cursor) {
6342 cls_rule_format(&facet->cr, &ds);
6343 ds_put_cstr(&ds, ", ");
6344 ds_put_format(&ds, "n_subfacets:%zu, ", list_size(&facet->subfacets));
6345 ds_put_format(&ds, "used:%.3fs, ", (now - facet->used) / 1000.0);
6346 ds_put_cstr(&ds, "Datapath actions: ");
6347 if (facet->xout.slow) {
6348 uint64_t slow_path_stub[128 / 8];
6349 const struct nlattr *actions;
6352 compose_slow_path(ofproto, &facet->flow, facet->xout.slow,
6353 slow_path_stub, sizeof slow_path_stub,
6354 &actions, &actions_len);
6355 format_odp_actions(&ds, actions, actions_len);
6357 format_odp_actions(&ds, facet->xout.odp_actions.data,
6358 facet->xout.odp_actions.size);
6360 ds_put_cstr(&ds, "\n");
6363 ds_chomp(&ds, '\n');
6364 unixctl_command_reply(conn, ds_cstr(&ds));
6369 ofproto_unixctl_dpif_dump_flows(struct unixctl_conn *conn,
6370 int argc OVS_UNUSED, const char *argv[],
6371 void *aux OVS_UNUSED)
6373 struct ds ds = DS_EMPTY_INITIALIZER;
6374 const struct ofproto_dpif *ofproto;
6375 struct subfacet *subfacet;
6377 ofproto = ofproto_dpif_lookup(argv[1]);
6379 unixctl_command_reply_error(conn, "no such bridge");
6383 update_stats(ofproto->backer);
6385 HMAP_FOR_EACH (subfacet, hmap_node, &ofproto->backer->subfacets) {
6386 struct facet *facet = subfacet->facet;
6388 if (ofproto_dpif_cast(facet->rule->up.ofproto) != ofproto) {
6392 odp_flow_key_format(subfacet->key, subfacet->key_len, &ds);
6394 ds_put_format(&ds, ", packets:%"PRIu64", bytes:%"PRIu64", used:",
6395 subfacet->dp_packet_count, subfacet->dp_byte_count);
6396 if (subfacet->used) {
6397 ds_put_format(&ds, "%.3fs",
6398 (time_msec() - subfacet->used) / 1000.0);
6400 ds_put_format(&ds, "never");
6402 if (subfacet->facet->tcp_flags) {
6403 ds_put_cstr(&ds, ", flags:");
6404 packet_format_tcp_flags(&ds, subfacet->facet->tcp_flags);
6407 ds_put_cstr(&ds, ", actions:");
6408 if (facet->xout.slow) {
6409 uint64_t slow_path_stub[128 / 8];
6410 const struct nlattr *actions;
6413 compose_slow_path(ofproto, &facet->flow, facet->xout.slow,
6414 slow_path_stub, sizeof slow_path_stub,
6415 &actions, &actions_len);
6416 format_odp_actions(&ds, actions, actions_len);
6418 format_odp_actions(&ds, facet->xout.odp_actions.data,
6419 facet->xout.odp_actions.size);
6421 ds_put_char(&ds, '\n');
6424 unixctl_command_reply(conn, ds_cstr(&ds));
6429 ofproto_unixctl_dpif_del_flows(struct unixctl_conn *conn,
6430 int argc OVS_UNUSED, const char *argv[],
6431 void *aux OVS_UNUSED)
6433 struct ds ds = DS_EMPTY_INITIALIZER;
6434 struct ofproto_dpif *ofproto;
6436 ofproto = ofproto_dpif_lookup(argv[1]);
6438 unixctl_command_reply_error(conn, "no such bridge");
6442 flush(&ofproto->up);
6444 unixctl_command_reply(conn, ds_cstr(&ds));
6449 ofproto_dpif_unixctl_init(void)
6451 static bool registered;
6457 unixctl_command_register(
6459 "[dp_name]|bridge odp_flow|br_flow [-generate|packet]",
6460 1, 3, ofproto_unixctl_trace, NULL);
6461 unixctl_command_register("fdb/flush", "[bridge]", 0, 1,
6462 ofproto_unixctl_fdb_flush, NULL);
6463 unixctl_command_register("fdb/show", "bridge", 1, 1,
6464 ofproto_unixctl_fdb_show, NULL);
6465 unixctl_command_register("ofproto/clog", "", 0, 0,
6466 ofproto_dpif_clog, NULL);
6467 unixctl_command_register("ofproto/unclog", "", 0, 0,
6468 ofproto_dpif_unclog, NULL);
6469 unixctl_command_register("ofproto/self-check", "[bridge]", 0, 1,
6470 ofproto_dpif_self_check, NULL);
6471 unixctl_command_register("dpif/dump-dps", "", 0, 0,
6472 ofproto_unixctl_dpif_dump_dps, NULL);
6473 unixctl_command_register("dpif/show", "", 0, 0, ofproto_unixctl_dpif_show,
6475 unixctl_command_register("dpif/dump-flows", "bridge", 1, 1,
6476 ofproto_unixctl_dpif_dump_flows, NULL);
6477 unixctl_command_register("dpif/del-flows", "bridge", 1, 1,
6478 ofproto_unixctl_dpif_del_flows, NULL);
6479 unixctl_command_register("dpif/dump-megaflows", "bridge", 1, 1,
6480 ofproto_unixctl_dpif_dump_megaflows, NULL);
6483 /* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
6485 * This is deprecated. It is only for compatibility with broken device drivers
6486 * in old versions of Linux that do not properly support VLANs when VLAN
6487 * devices are not used. When broken device drivers are no longer in
6488 * widespread use, we will delete these interfaces. */
6491 set_realdev(struct ofport *ofport_, ofp_port_t realdev_ofp_port, int vid)
6493 struct ofproto_dpif *ofproto = ofproto_dpif_cast(ofport_->ofproto);
6494 struct ofport_dpif *ofport = ofport_dpif_cast(ofport_);
6496 if (realdev_ofp_port == ofport->realdev_ofp_port
6497 && vid == ofport->vlandev_vid) {
6501 ofproto->backer->need_revalidate = REV_RECONFIGURE;
6503 if (ofport->realdev_ofp_port) {
6506 if (realdev_ofp_port && ofport->bundle) {
6507 /* vlandevs are enslaved to their realdevs, so they are not allowed to
6508 * themselves be part of a bundle. */
6509 bundle_set(ofport->up.ofproto, ofport->bundle, NULL);
6512 ofport->realdev_ofp_port = realdev_ofp_port;
6513 ofport->vlandev_vid = vid;
6515 if (realdev_ofp_port) {
6516 vsp_add(ofport, realdev_ofp_port, vid);
6523 hash_realdev_vid(ofp_port_t realdev_ofp_port, int vid)
6525 return hash_2words(ofp_to_u16(realdev_ofp_port), vid);
6528 /* Returns the OFP port number of the Linux VLAN device that corresponds to
6529 * 'vlan_tci' on the network device with port number 'realdev_ofp_port' in
6530 * 'struct ofport_dpif'. For example, given 'realdev_ofp_port' of eth0 and
6531 * 'vlan_tci' 9, it would return the port number of eth0.9.
6533 * Unless VLAN splinters are enabled for port 'realdev_ofp_port', this
6534 * function just returns its 'realdev_ofp_port' argument. */
6536 vsp_realdev_to_vlandev(const struct ofproto_dpif *ofproto,
6537 ofp_port_t realdev_ofp_port, ovs_be16 vlan_tci)
6539 if (!hmap_is_empty(&ofproto->realdev_vid_map)) {
6540 int vid = vlan_tci_to_vid(vlan_tci);
6541 const struct vlan_splinter *vsp;
6543 HMAP_FOR_EACH_WITH_HASH (vsp, realdev_vid_node,
6544 hash_realdev_vid(realdev_ofp_port, vid),
6545 &ofproto->realdev_vid_map) {
6546 if (vsp->realdev_ofp_port == realdev_ofp_port
6547 && vsp->vid == vid) {
6548 return vsp->vlandev_ofp_port;
6552 return realdev_ofp_port;
6555 static struct vlan_splinter *
6556 vlandev_find(const struct ofproto_dpif *ofproto, ofp_port_t vlandev_ofp_port)
6558 struct vlan_splinter *vsp;
6560 HMAP_FOR_EACH_WITH_HASH (vsp, vlandev_node,
6561 hash_ofp_port(vlandev_ofp_port),
6562 &ofproto->vlandev_map) {
6563 if (vsp->vlandev_ofp_port == vlandev_ofp_port) {
6571 /* Returns the OpenFlow port number of the "real" device underlying the Linux
6572 * VLAN device with OpenFlow port number 'vlandev_ofp_port' and stores the
6573 * VLAN VID of the Linux VLAN device in '*vid'. For example, given
6574 * 'vlandev_ofp_port' of eth0.9, it would return the OpenFlow port number of
6575 * eth0 and store 9 in '*vid'.
6577 * Returns 0 and does not modify '*vid' if 'vlandev_ofp_port' is not a Linux
6578 * VLAN device. Unless VLAN splinters are enabled, this is what this function
6581 vsp_vlandev_to_realdev(const struct ofproto_dpif *ofproto,
6582 ofp_port_t vlandev_ofp_port, int *vid)
6584 if (!hmap_is_empty(&ofproto->vlandev_map)) {
6585 const struct vlan_splinter *vsp;
6587 vsp = vlandev_find(ofproto, vlandev_ofp_port);
6592 return vsp->realdev_ofp_port;
6598 /* Given 'flow', a flow representing a packet received on 'ofproto', checks
6599 * whether 'flow->in_port' represents a Linux VLAN device. If so, changes
6600 * 'flow->in_port' to the "real" device backing the VLAN device, sets
6601 * 'flow->vlan_tci' to the VLAN VID, and returns true. Otherwise (which is
6602 * always the case unless VLAN splinters are enabled), returns false without
6603 * making any changes. */
6605 vsp_adjust_flow(const struct ofproto_dpif *ofproto, struct flow *flow)
6610 realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port.ofp_port, &vid);
6615 /* Cause the flow to be processed as if it came in on the real device with
6616 * the VLAN device's VLAN ID. */
6617 flow->in_port.ofp_port = realdev;
6618 flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
6623 vsp_remove(struct ofport_dpif *port)
6625 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
6626 struct vlan_splinter *vsp;
6628 vsp = vlandev_find(ofproto, port->up.ofp_port);
6630 hmap_remove(&ofproto->vlandev_map, &vsp->vlandev_node);
6631 hmap_remove(&ofproto->realdev_vid_map, &vsp->realdev_vid_node);
6634 port->realdev_ofp_port = 0;
6636 VLOG_ERR("missing vlan device record");
6641 vsp_add(struct ofport_dpif *port, ofp_port_t realdev_ofp_port, int vid)
6643 struct ofproto_dpif *ofproto = ofproto_dpif_cast(port->up.ofproto);
6645 if (!vsp_vlandev_to_realdev(ofproto, port->up.ofp_port, NULL)
6646 && (vsp_realdev_to_vlandev(ofproto, realdev_ofp_port, htons(vid))
6647 == realdev_ofp_port)) {
6648 struct vlan_splinter *vsp;
6650 vsp = xmalloc(sizeof *vsp);
6651 hmap_insert(&ofproto->vlandev_map, &vsp->vlandev_node,
6652 hash_ofp_port(port->up.ofp_port));
6653 hmap_insert(&ofproto->realdev_vid_map, &vsp->realdev_vid_node,
6654 hash_realdev_vid(realdev_ofp_port, vid));
6655 vsp->realdev_ofp_port = realdev_ofp_port;
6656 vsp->vlandev_ofp_port = port->up.ofp_port;
6659 port->realdev_ofp_port = realdev_ofp_port;
6661 VLOG_ERR("duplicate vlan device record");
6666 ofp_port_to_odp_port(const struct ofproto_dpif *ofproto, ofp_port_t ofp_port)
6668 const struct ofport_dpif *ofport = get_ofp_port(ofproto, ofp_port);
6669 return ofport ? ofport->odp_port : ODPP_NONE;
6672 static struct ofport_dpif *
6673 odp_port_to_ofport(const struct dpif_backer *backer, odp_port_t odp_port)
6675 struct ofport_dpif *port;
6677 HMAP_FOR_EACH_IN_BUCKET (port, odp_port_node, hash_odp_port(odp_port),
6678 &backer->odp_to_ofport_map) {
6679 if (port->odp_port == odp_port) {
6688 odp_port_to_ofp_port(const struct ofproto_dpif *ofproto, odp_port_t odp_port)
6690 struct ofport_dpif *port;
6692 port = odp_port_to_ofport(ofproto->backer, odp_port);
6693 if (port && &ofproto->up == port->up.ofproto) {
6694 return port->up.ofp_port;
6700 /* Compute exponentially weighted moving average, adding 'new' as the newest,
6701 * most heavily weighted element. 'base' designates the rate of decay: after
6702 * 'base' further updates, 'new''s weight in the EWMA decays to about 1/e
6705 exp_mavg(double *avg, int base, double new)
6707 *avg = (*avg * (base - 1) + new) / base;
6711 update_moving_averages(struct dpif_backer *backer)
6713 const int min_ms = 60 * 1000; /* milliseconds in one minute. */
6714 long long int minutes = (time_msec() - backer->created) / min_ms;
6717 backer->lifetime.add_rate = (double) backer->total_subfacet_add_count
6719 backer->lifetime.del_rate = (double) backer->total_subfacet_del_count
6722 backer->lifetime.add_rate = 0.0;
6723 backer->lifetime.del_rate = 0.0;
6726 /* Update hourly averages on the minute boundaries. */
6727 if (time_msec() - backer->last_minute >= min_ms) {
6728 exp_mavg(&backer->hourly.add_rate, 60, backer->subfacet_add_count);
6729 exp_mavg(&backer->hourly.del_rate, 60, backer->subfacet_del_count);
6731 /* Update daily averages on the hour boundaries. */
6732 if ((backer->last_minute - backer->created) / min_ms % 60 == 59) {
6733 exp_mavg(&backer->daily.add_rate, 24, backer->hourly.add_rate);
6734 exp_mavg(&backer->daily.del_rate, 24, backer->hourly.del_rate);
6737 backer->total_subfacet_add_count += backer->subfacet_add_count;
6738 backer->total_subfacet_del_count += backer->subfacet_del_count;
6739 backer->subfacet_add_count = 0;
6740 backer->subfacet_del_count = 0;
6741 backer->last_minute += min_ms;
6745 const struct ofproto_class ofproto_dpif_class = {
6780 port_is_lacp_current,
6781 NULL, /* rule_choose_table */
6788 rule_modify_actions,
6802 get_stp_port_status,
6809 is_mirror_output_bundle,
6810 forward_bpdu_changed,
6811 set_mac_table_config,