1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
16 #include "ofproto-dpif-upcall.h"
25 #include "dynamic-string.h"
26 #include "fail-open.h"
27 #include "guarded-list.h"
32 #include "ofproto-dpif-ipfix.h"
33 #include "ofproto-dpif-sflow.h"
34 #include "ofproto-dpif-xlate.h"
37 #include "poll-loop.h"
42 #define MAX_QUEUE_LENGTH 512
43 #define FLOW_MISS_MAX_BATCH 50
44 #define REVALIDATE_MAX_BATCH 50
46 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall);
48 COVERAGE_DEFINE(upcall_duplicate_flow);
49 COVERAGE_DEFINE(revalidate_missed_dp_flow);
51 /* A thread that reads upcalls from dpif, forwards each upcall's packet,
52 * and possibly sets up a kernel flow as a cache. */
54 struct udpif *udpif; /* Parent udpif. */
55 pthread_t thread; /* Thread ID. */
56 uint32_t handler_id; /* Handler id. */
59 /* A thread that processes datapath flows, updates OpenFlow statistics, and
60 * updates or removes them if necessary. */
62 struct udpif *udpif; /* Parent udpif. */
63 pthread_t thread; /* Thread ID. */
64 unsigned int id; /* ovsthread_id_self(). */
65 struct hmap *ukeys; /* Points into udpif->ukeys for this
66 revalidator. Used for GC phase. */
69 /* An upcall handler for ofproto_dpif.
71 * udpif keeps records of two kind of logically separate units:
76 * - An array of 'struct handler's for upcall handling and flow
82 * - Revalidation threads which read the datapath flow table and maintains
86 struct list list_node; /* In all_udpifs list. */
88 struct dpif *dpif; /* Datapath handle. */
89 struct dpif_backer *backer; /* Opaque dpif_backer pointer. */
91 uint32_t secret; /* Random seed for upcall hash. */
93 struct handler *handlers; /* Upcall handlers. */
96 struct revalidator *revalidators; /* Flow revalidators. */
97 size_t n_revalidators;
99 struct latch exit_latch; /* Tells child threads to exit. */
102 struct seq *reval_seq; /* Incremented to force revalidation. */
103 bool need_revalidate; /* As indicated by 'reval_seq'. */
104 bool reval_exit; /* Set by leader on 'exit_latch. */
105 struct ovs_barrier reval_barrier; /* Barrier used by revalidators. */
106 struct dpif_flow_dump dump; /* DPIF flow dump state. */
107 long long int dump_duration; /* Duration of the last flow dump. */
108 struct seq *dump_seq; /* Increments each dump iteration. */
110 /* There are 'n_revalidators' ukey hmaps. Each revalidator retains a
111 * reference to one of these for garbage collection.
113 * During the flow dump phase, revalidators insert into these with a random
114 * distribution. During the garbage collection phase, each revalidator
115 * takes care of garbage collecting one of these hmaps. */
117 struct ovs_mutex mutex; /* Guards the following. */
118 struct hmap hmap OVS_GUARDED; /* Datapath flow keys. */
121 /* Datapath flow statistics. */
122 unsigned int max_n_flows;
123 unsigned int avg_n_flows;
125 /* Following fields are accessed and modified by different threads. */
126 atomic_uint flow_limit; /* Datapath flow hard limit. */
128 /* n_flows_mutex prevents multiple threads updating these concurrently. */
129 atomic_ulong n_flows; /* Number of flows in the datapath. */
130 atomic_llong n_flows_timestamp; /* Last time n_flows was updated. */
131 struct ovs_mutex n_flows_mutex;
135 BAD_UPCALL, /* Some kind of bug somewhere. */
136 MISS_UPCALL, /* A flow miss. */
137 SFLOW_UPCALL, /* sFlow sample. */
138 FLOW_SAMPLE_UPCALL, /* Per-flow sampling. */
139 IPFIX_UPCALL /* Per-bridge sampling. */
143 struct flow_miss *flow_miss; /* This upcall's flow_miss. */
145 /* Raw upcall plus data for keeping track of the memory backing it. */
146 struct dpif_upcall dpif_upcall; /* As returned by dpif_recv() */
147 struct ofpbuf upcall_buf; /* Owns some data in 'dpif_upcall'. */
148 uint64_t upcall_stub[512 / 8]; /* Buffer to reduce need for malloc(). */
151 /* 'udpif_key's are responsible for tracking the little bit of state udpif
152 * needs to do flow expiration which can't be pulled directly from the
153 * datapath. They may be created or maintained by any revalidator during
154 * the dump phase, but are owned by a single revalidator, and are destroyed
155 * by that revalidator during the garbage-collection phase.
157 * While some elements of a udpif_key are protected by a mutex, the ukey itself
158 * is not. Therefore it is not safe to destroy a udpif_key except when all
159 * revalidators are in garbage collection phase, or they aren't running. */
161 struct hmap_node hmap_node; /* In parent revalidator 'ukeys' map. */
163 /* These elements are read only once created, and therefore aren't
164 * protected by a mutex. */
165 const struct nlattr *key; /* Datapath flow key. */
166 size_t key_len; /* Length of 'key'. */
168 struct ovs_mutex mutex; /* Guards the following. */
169 struct dpif_flow_stats stats OVS_GUARDED; /* Last known stats.*/
170 long long int created OVS_GUARDED; /* Estimate of creation time. */
171 bool mark OVS_GUARDED; /* For mark and sweep garbage
173 bool flow_exists OVS_GUARDED; /* Ensures flows are only deleted
176 struct xlate_cache *xcache OVS_GUARDED; /* Cache for xlate entries that
177 * are affected by this ukey.
178 * Used for stats and learning.*/
179 struct odputil_keybuf key_buf; /* Memory for 'key'. */
182 /* Flow miss batching.
184 * Some dpifs implement operations faster when you hand them off in a batch.
185 * To allow batching, "struct flow_miss" queues the dpif-related work needed
186 * for a given flow. Each "struct flow_miss" corresponds to sending one or
187 * more packets, plus possibly installing the flow in the dpif. */
189 struct hmap_node hmap_node;
190 struct ofproto_dpif *ofproto;
193 const struct nlattr *key;
195 enum dpif_upcall_type upcall_type;
196 struct dpif_flow_stats stats;
197 odp_port_t odp_in_port;
199 uint64_t slow_path_buf[128 / 8];
200 struct odputil_keybuf mask_buf;
202 struct xlate_out xout;
207 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
208 static struct list all_udpifs = LIST_INITIALIZER(&all_udpifs);
210 static size_t read_upcalls(struct handler *,
211 struct upcall upcalls[FLOW_MISS_MAX_BATCH],
212 struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH],
214 static void handle_upcalls(struct handler *, struct hmap *, struct upcall *,
216 static void udpif_stop_threads(struct udpif *);
217 static void udpif_start_threads(struct udpif *, size_t n_handlers,
218 size_t n_revalidators);
219 static void *udpif_upcall_handler(void *);
220 static void *udpif_revalidator(void *);
221 static unsigned long udpif_get_n_flows(struct udpif *);
222 static void revalidate(struct revalidator *);
223 static void revalidator_sweep(struct revalidator *);
224 static void revalidator_purge(struct revalidator *);
225 static void upcall_unixctl_show(struct unixctl_conn *conn, int argc,
226 const char *argv[], void *aux);
227 static void upcall_unixctl_disable_megaflows(struct unixctl_conn *, int argc,
228 const char *argv[], void *aux);
229 static void upcall_unixctl_enable_megaflows(struct unixctl_conn *, int argc,
230 const char *argv[], void *aux);
231 static void upcall_unixctl_set_flow_limit(struct unixctl_conn *conn, int argc,
232 const char *argv[], void *aux);
234 static struct udpif_key *ukey_create(const struct nlattr *key, size_t key_len,
236 static void ukey_delete(struct revalidator *, struct udpif_key *);
238 static atomic_bool enable_megaflows = ATOMIC_VAR_INIT(true);
241 udpif_create(struct dpif_backer *backer, struct dpif *dpif)
243 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
244 struct udpif *udpif = xzalloc(sizeof *udpif);
246 if (ovsthread_once_start(&once)) {
247 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show,
249 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
250 upcall_unixctl_disable_megaflows, NULL);
251 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
252 upcall_unixctl_enable_megaflows, NULL);
253 unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
254 upcall_unixctl_set_flow_limit, NULL);
255 ovsthread_once_done(&once);
259 udpif->backer = backer;
260 atomic_init(&udpif->flow_limit, MIN(ofproto_flow_limit, 10000));
261 udpif->secret = random_uint32();
262 udpif->reval_seq = seq_create();
263 udpif->dump_seq = seq_create();
264 latch_init(&udpif->exit_latch);
265 list_push_back(&all_udpifs, &udpif->list_node);
266 atomic_init(&udpif->n_flows, 0);
267 atomic_init(&udpif->n_flows_timestamp, LLONG_MIN);
268 ovs_mutex_init(&udpif->n_flows_mutex);
274 udpif_destroy(struct udpif *udpif)
276 udpif_stop_threads(udpif);
278 list_remove(&udpif->list_node);
279 latch_destroy(&udpif->exit_latch);
280 seq_destroy(udpif->reval_seq);
281 seq_destroy(udpif->dump_seq);
282 ovs_mutex_destroy(&udpif->n_flows_mutex);
286 /* Stops the handler and revalidator threads, must be enclosed in
287 * ovsrcu quiescent state unless when destroying udpif. */
289 udpif_stop_threads(struct udpif *udpif)
291 if (udpif && (udpif->n_handlers != 0 || udpif->n_revalidators != 0)) {
294 latch_set(&udpif->exit_latch);
296 for (i = 0; i < udpif->n_handlers; i++) {
297 struct handler *handler = &udpif->handlers[i];
299 xpthread_join(handler->thread, NULL);
302 for (i = 0; i < udpif->n_revalidators; i++) {
303 xpthread_join(udpif->revalidators[i].thread, NULL);
306 for (i = 0; i < udpif->n_revalidators; i++) {
307 struct revalidator *revalidator = &udpif->revalidators[i];
309 /* Delete ukeys, and delete all flows from the datapath to prevent
310 * double-counting stats. */
311 revalidator_purge(revalidator);
313 hmap_destroy(&udpif->ukeys[i].hmap);
314 ovs_mutex_destroy(&udpif->ukeys[i].mutex);
317 latch_poll(&udpif->exit_latch);
319 ovs_barrier_destroy(&udpif->reval_barrier);
321 free(udpif->revalidators);
322 udpif->revalidators = NULL;
323 udpif->n_revalidators = 0;
325 free(udpif->handlers);
326 udpif->handlers = NULL;
327 udpif->n_handlers = 0;
334 /* Starts the handler and revalidator threads, must be enclosed in
335 * ovsrcu quiescent state. */
337 udpif_start_threads(struct udpif *udpif, size_t n_handlers,
338 size_t n_revalidators)
340 if (udpif && n_handlers && n_revalidators) {
343 udpif->n_handlers = n_handlers;
344 udpif->n_revalidators = n_revalidators;
346 udpif->handlers = xzalloc(udpif->n_handlers * sizeof *udpif->handlers);
347 for (i = 0; i < udpif->n_handlers; i++) {
348 struct handler *handler = &udpif->handlers[i];
350 handler->udpif = udpif;
351 handler->handler_id = i;
352 handler->thread = ovs_thread_create(
353 "handler", udpif_upcall_handler, handler);
356 ovs_barrier_init(&udpif->reval_barrier, udpif->n_revalidators);
357 udpif->reval_exit = false;
358 udpif->revalidators = xzalloc(udpif->n_revalidators
359 * sizeof *udpif->revalidators);
360 udpif->ukeys = xmalloc(sizeof *udpif->ukeys * n_revalidators);
361 for (i = 0; i < udpif->n_revalidators; i++) {
362 struct revalidator *revalidator = &udpif->revalidators[i];
364 revalidator->udpif = udpif;
365 hmap_init(&udpif->ukeys[i].hmap);
366 ovs_mutex_init(&udpif->ukeys[i].mutex);
367 revalidator->ukeys = &udpif->ukeys[i].hmap;
368 revalidator->thread = ovs_thread_create(
369 "revalidator", udpif_revalidator, revalidator);
374 /* Tells 'udpif' how many threads it should use to handle upcalls.
375 * 'n_handlers' and 'n_revalidators' can never be zero. 'udpif''s
376 * datapath handle must have packet reception enabled before starting
379 udpif_set_threads(struct udpif *udpif, size_t n_handlers,
380 size_t n_revalidators)
383 ovs_assert(n_handlers && n_revalidators);
385 ovsrcu_quiesce_start();
386 if (udpif->n_handlers != n_handlers
387 || udpif->n_revalidators != n_revalidators) {
388 udpif_stop_threads(udpif);
391 if (!udpif->handlers && !udpif->revalidators) {
394 error = dpif_handlers_set(udpif->dpif, n_handlers);
396 VLOG_ERR("failed to configure handlers in dpif %s: %s",
397 dpif_name(udpif->dpif), ovs_strerror(error));
401 udpif_start_threads(udpif, n_handlers, n_revalidators);
403 ovsrcu_quiesce_end();
406 /* Waits for all ongoing upcall translations to complete. This ensures that
407 * there are no transient references to any removed ofprotos (or other
408 * objects). In particular, this should be called after an ofproto is removed
409 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
411 udpif_synchronize(struct udpif *udpif)
413 /* This is stronger than necessary. It would be sufficient to ensure
414 * (somehow) that each handler and revalidator thread had passed through
415 * its main loop once. */
416 size_t n_handlers = udpif->n_handlers;
417 size_t n_revalidators = udpif->n_revalidators;
419 ovsrcu_quiesce_start();
420 udpif_stop_threads(udpif);
421 udpif_start_threads(udpif, n_handlers, n_revalidators);
422 ovsrcu_quiesce_end();
425 /* Notifies 'udpif' that something changed which may render previous
426 * xlate_actions() results invalid. */
428 udpif_revalidate(struct udpif *udpif)
430 seq_change(udpif->reval_seq);
433 /* Returns a seq which increments every time 'udpif' pulls stats from the
434 * datapath. Callers can use this to get a sense of when might be a good time
435 * to do periodic work which relies on relatively up to date statistics. */
437 udpif_dump_seq(struct udpif *udpif)
439 return udpif->dump_seq;
443 udpif_get_memory_usage(struct udpif *udpif, struct simap *usage)
447 simap_increase(usage, "handlers", udpif->n_handlers);
449 simap_increase(usage, "revalidators", udpif->n_revalidators);
450 for (i = 0; i < udpif->n_revalidators; i++) {
451 ovs_mutex_lock(&udpif->ukeys[i].mutex);
452 simap_increase(usage, "udpif keys", hmap_count(&udpif->ukeys[i].hmap));
453 ovs_mutex_unlock(&udpif->ukeys[i].mutex);
457 /* Remove flows from a single datapath. */
459 udpif_flush(struct udpif *udpif)
461 size_t n_handlers, n_revalidators;
463 n_handlers = udpif->n_handlers;
464 n_revalidators = udpif->n_revalidators;
466 ovsrcu_quiesce_start();
468 udpif_stop_threads(udpif);
469 dpif_flow_flush(udpif->dpif);
470 udpif_start_threads(udpif, n_handlers, n_revalidators);
472 ovsrcu_quiesce_end();
475 /* Removes all flows from all datapaths. */
477 udpif_flush_all_datapaths(void)
481 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
488 udpif_get_n_flows(struct udpif *udpif)
490 long long int time, now;
491 unsigned long flow_count;
494 atomic_read(&udpif->n_flows_timestamp, &time);
495 if (time < now - 100 && !ovs_mutex_trylock(&udpif->n_flows_mutex)) {
496 struct dpif_dp_stats stats;
498 atomic_store(&udpif->n_flows_timestamp, now);
499 dpif_get_dp_stats(udpif->dpif, &stats);
500 flow_count = stats.n_flows;
501 atomic_store(&udpif->n_flows, flow_count);
502 ovs_mutex_unlock(&udpif->n_flows_mutex);
504 atomic_read(&udpif->n_flows, &flow_count);
509 /* The upcall handler thread tries to read a batch of FLOW_MISS_MAX_BATCH
510 * upcalls from dpif, processes the batch and installs corresponding flows
513 udpif_upcall_handler(void *arg)
515 struct handler *handler = arg;
516 struct udpif *udpif = handler->udpif;
517 struct hmap misses = HMAP_INITIALIZER(&misses);
519 while (!latch_is_set(&handler->udpif->exit_latch)) {
520 struct upcall upcalls[FLOW_MISS_MAX_BATCH];
521 struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH];
522 struct flow_miss *miss;
525 n_upcalls = read_upcalls(handler, upcalls, miss_buf, &misses);
527 dpif_recv_wait(udpif->dpif, handler->handler_id);
528 latch_wait(&udpif->exit_latch);
531 handle_upcalls(handler, &misses, upcalls, n_upcalls);
533 HMAP_FOR_EACH (miss, hmap_node, &misses) {
534 xlate_out_uninit(&miss->xout);
537 for (i = 0; i < n_upcalls; i++) {
538 ofpbuf_uninit(&upcalls[i].dpif_upcall.packet);
539 ofpbuf_uninit(&upcalls[i].upcall_buf);
544 hmap_destroy(&misses);
550 udpif_revalidator(void *arg)
552 /* Used by all revalidators. */
553 struct revalidator *revalidator = arg;
554 struct udpif *udpif = revalidator->udpif;
555 bool leader = revalidator == &udpif->revalidators[0];
557 /* Used only by the leader. */
558 long long int start_time = 0;
559 uint64_t last_reval_seq = 0;
560 unsigned int flow_limit = 0;
563 revalidator->id = ovsthread_id_self();
568 reval_seq = seq_read(udpif->reval_seq);
569 udpif->need_revalidate = last_reval_seq != reval_seq;
570 last_reval_seq = reval_seq;
572 n_flows = udpif_get_n_flows(udpif);
573 udpif->max_n_flows = MAX(n_flows, udpif->max_n_flows);
574 udpif->avg_n_flows = (udpif->avg_n_flows + n_flows) / 2;
576 /* Only the leader checks the exit latch to prevent a race where
577 * some threads think it's true and exit and others think it's
578 * false and block indefinitely on the reval_barrier */
579 udpif->reval_exit = latch_is_set(&udpif->exit_latch);
581 start_time = time_msec();
582 if (!udpif->reval_exit) {
583 dpif_flow_dump_start(&udpif->dump, udpif->dpif);
587 /* Wait for the leader to start the flow dump. */
588 ovs_barrier_block(&udpif->reval_barrier);
589 if (udpif->reval_exit) {
592 revalidate(revalidator);
594 /* Wait for all flows to have been dumped before we garbage collect. */
595 ovs_barrier_block(&udpif->reval_barrier);
596 revalidator_sweep(revalidator);
598 /* Wait for all revalidators to finish garbage collection. */
599 ovs_barrier_block(&udpif->reval_barrier);
602 long long int duration;
604 dpif_flow_dump_done(&udpif->dump);
605 seq_change(udpif->dump_seq);
607 duration = MAX(time_msec() - start_time, 1);
608 atomic_read(&udpif->flow_limit, &flow_limit);
609 udpif->dump_duration = duration;
610 if (duration > 2000) {
611 flow_limit /= duration / 1000;
612 } else if (duration > 1300) {
613 flow_limit = flow_limit * 3 / 4;
614 } else if (duration < 1000 && n_flows > 2000
615 && flow_limit < n_flows * 1000 / duration) {
618 flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
619 atomic_store(&udpif->flow_limit, flow_limit);
621 if (duration > 2000) {
622 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
626 poll_timer_wait_until(start_time + MIN(ofproto_max_idle, 500));
627 seq_wait(udpif->reval_seq, last_reval_seq);
628 latch_wait(&udpif->exit_latch);
636 static enum upcall_type
637 classify_upcall(const struct upcall *upcall)
639 const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
640 union user_action_cookie cookie;
643 /* First look at the upcall type. */
644 switch (dpif_upcall->type) {
651 case DPIF_N_UC_TYPES:
653 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
658 /* "action" upcalls need a closer look. */
659 if (!dpif_upcall->userdata) {
660 VLOG_WARN_RL(&rl, "action upcall missing cookie");
663 userdata_len = nl_attr_get_size(dpif_upcall->userdata);
664 if (userdata_len < sizeof cookie.type
665 || userdata_len > sizeof cookie) {
666 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
670 memset(&cookie, 0, sizeof cookie);
671 memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
672 if (userdata_len == MAX(8, sizeof cookie.sflow)
673 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
675 } else if (userdata_len == MAX(8, sizeof cookie.slow_path)
676 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
678 } else if (userdata_len == MAX(8, sizeof cookie.flow_sample)
679 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
680 return FLOW_SAMPLE_UPCALL;
681 } else if (userdata_len == MAX(8, sizeof cookie.ipfix)
682 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
685 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
686 " and size %"PRIuSIZE, cookie.type, userdata_len);
691 /* Calculates slow path actions for 'xout'. 'buf' must statically be
692 * initialized with at least 128 bytes of space. */
694 compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
695 struct flow *flow, odp_port_t odp_in_port,
698 union user_action_cookie cookie;
702 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
703 cookie.slow_path.unused = 0;
704 cookie.slow_path.reason = xout->slow;
706 port = xout->slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)
709 pid = dpif_port_get_pid(udpif->dpif, port, flow_hash_5tuple(flow, 0));
710 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, buf);
713 static struct flow_miss *
714 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
715 const struct flow *flow, uint32_t hash)
717 struct flow_miss *miss;
719 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
720 if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
728 /* Reads and classifies upcalls. Returns the number of upcalls successfully
731 read_upcalls(struct handler *handler,
732 struct upcall upcalls[FLOW_MISS_MAX_BATCH],
733 struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH],
736 struct udpif *udpif = handler->udpif;
739 size_t n_upcalls = 0;
742 * Try reading FLOW_MISS_MAX_BATCH upcalls from dpif.
744 * Extract the flow from each upcall. Construct in 'misses' a hash table
745 * that maps each unique flow to a 'struct flow_miss'.
747 * Most commonly there is a single packet per flow_miss, but there are
748 * several reasons why there might be more than one, e.g.:
750 * - The dpif packet interface does not support TSO (or UFO, etc.), so a
751 * large packet sent to userspace is split into a sequence of smaller
754 * - A stream of quickly arriving packets in an established "slow-pathed"
757 * - Rarely, a stream of quickly arriving packets in a flow not yet
758 * established. (This is rare because most protocols do not send
759 * multiple back-to-back packets before receiving a reply from the
760 * other end of the connection, which gives OVS a chance to set up a
763 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
764 struct upcall *upcall = &upcalls[n_upcalls];
765 struct flow_miss *miss = &miss_buf[n_misses];
766 struct dpif_upcall *dupcall;
767 struct ofpbuf *packet;
768 struct flow_miss *existing_miss;
769 struct ofproto_dpif *ofproto;
770 struct dpif_sflow *sflow;
771 struct dpif_ipfix *ipfix;
773 enum upcall_type type;
774 odp_port_t odp_in_port;
777 ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
778 sizeof upcall->upcall_stub);
779 error = dpif_recv(udpif->dpif, handler->handler_id,
780 &upcall->dpif_upcall, &upcall->upcall_buf);
782 ofpbuf_uninit(&upcall->upcall_buf);
786 dupcall = &upcall->dpif_upcall;
787 packet = &dupcall->packet;
788 error = xlate_receive(udpif->backer, packet, dupcall->key,
789 dupcall->key_len, &flow,
790 &ofproto, &ipfix, &sflow, NULL, &odp_in_port);
792 if (error == ENODEV) {
793 /* Received packet on datapath port for which we couldn't
794 * associate an ofproto. This can happen if a port is removed
795 * while traffic is being received. Print a rate-limited
796 * message in case it happens frequently. Install a drop flow
797 * so that future packets of the flow are inexpensively dropped
799 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
800 "port %"PRIu32, odp_in_port);
801 dpif_flow_put(udpif->dpif, DPIF_FP_CREATE,
802 dupcall->key, dupcall->key_len, NULL, 0, NULL, 0,
808 type = classify_upcall(upcall);
809 if (type == MISS_UPCALL) {
811 struct pkt_metadata md = pkt_metadata_from_flow(&flow);
813 flow_extract(packet, &md, &miss->flow);
814 hash = flow_hash(&miss->flow, 0);
815 existing_miss = flow_miss_find(misses, ofproto, &miss->flow,
817 if (!existing_miss) {
818 hmap_insert(misses, &miss->hmap_node, hash);
819 miss->ofproto = ofproto;
820 miss->key = dupcall->key;
821 miss->key_len = dupcall->key_len;
822 miss->upcall_type = dupcall->type;
823 miss->stats.n_packets = 0;
824 miss->stats.n_bytes = 0;
825 miss->stats.used = time_msec();
826 miss->stats.tcp_flags = 0;
827 miss->odp_in_port = odp_in_port;
831 miss = existing_miss;
833 miss->stats.tcp_flags |= ntohs(miss->flow.tcp_flags);
834 miss->stats.n_bytes += ofpbuf_size(packet);
835 miss->stats.n_packets++;
837 upcall->flow_miss = miss;
845 union user_action_cookie cookie;
847 memset(&cookie, 0, sizeof cookie);
848 memcpy(&cookie, nl_attr_get(dupcall->userdata),
849 sizeof cookie.sflow);
850 dpif_sflow_received(sflow, packet, &flow, odp_in_port,
856 dpif_ipfix_bridge_sample(ipfix, packet, &flow);
859 case FLOW_SAMPLE_UPCALL:
861 union user_action_cookie cookie;
863 memset(&cookie, 0, sizeof cookie);
864 memcpy(&cookie, nl_attr_get(dupcall->userdata),
865 sizeof cookie.flow_sample);
867 /* The flow reflects exactly the contents of the packet.
868 * Sample the packet using it. */
869 dpif_ipfix_flow_sample(ipfix, packet, &flow,
870 cookie.flow_sample.collector_set_id,
871 cookie.flow_sample.probability,
872 cookie.flow_sample.obs_domain_id,
873 cookie.flow_sample.obs_point_id);
882 dpif_ipfix_unref(ipfix);
883 dpif_sflow_unref(sflow);
886 ofpbuf_uninit(&upcall->dpif_upcall.packet);
887 ofpbuf_uninit(&upcall->upcall_buf);
894 handle_upcalls(struct handler *handler, struct hmap *misses,
895 struct upcall *upcalls, size_t n_upcalls)
897 struct udpif *udpif = handler->udpif;
898 struct dpif_op *opsp[FLOW_MISS_MAX_BATCH * 2];
899 struct dpif_op ops[FLOW_MISS_MAX_BATCH * 2];
900 struct flow_miss *miss;
902 unsigned int flow_limit;
903 bool fail_open, may_put;
905 atomic_read(&udpif->flow_limit, &flow_limit);
906 may_put = udpif_get_n_flows(udpif) < flow_limit;
908 /* Initialize each 'struct flow_miss's ->xout.
910 * We do this per-flow_miss rather than per-packet because, most commonly,
911 * all the packets in a flow can use the same translation.
913 * We can't do this in the previous loop because we need the TCP flags for
914 * all the packets in each miss. */
916 HMAP_FOR_EACH (miss, hmap_node, misses) {
919 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL,
920 miss->stats.tcp_flags, NULL);
921 xin.may_learn = true;
923 if (miss->upcall_type == DPIF_UC_MISS) {
924 xin.resubmit_stats = &miss->stats;
926 /* For non-miss upcalls, there's a flow in the datapath which this
927 * packet was accounted to. Presumably the revalidators will deal
928 * with pushing its stats eventually. */
931 xlate_actions(&xin, &miss->xout);
932 fail_open = fail_open || miss->xout.fail_open;
935 /* Now handle the packets individually in order of arrival. In the common
936 * case each packet of a miss can share the same actions, but slow-pathed
937 * packets need to be translated individually:
939 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
940 * processes received packets for these protocols.
942 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
945 * The loop fills 'ops' with an array of operations to execute in the
948 for (i = 0; i < n_upcalls; i++) {
949 struct upcall *upcall = &upcalls[i];
950 struct flow_miss *miss = upcall->flow_miss;
951 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
953 ovs_be16 flow_vlan_tci;
955 /* Save a copy of flow.vlan_tci in case it is changed to
956 * generate proper mega flow masks for VLAN splinter flows. */
957 flow_vlan_tci = miss->flow.vlan_tci;
959 if (miss->xout.slow) {
962 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL, 0, packet);
963 xlate_actions_for_side_effects(&xin);
966 if (miss->flow.in_port.ofp_port
967 != vsp_realdev_to_vlandev(miss->ofproto,
968 miss->flow.in_port.ofp_port,
969 miss->flow.vlan_tci)) {
970 /* This packet was received on a VLAN splinter port. We
971 * added a VLAN to the packet to make the packet resemble
972 * the flow, but the actions were composed assuming that
973 * the packet contained no VLAN. So, we must remove the
974 * VLAN header from the packet before trying to execute the
976 if (ofpbuf_size(&miss->xout.odp_actions)) {
977 eth_pop_vlan(packet);
980 /* Remove the flow vlan tags inserted by vlan splinter logic
981 * to ensure megaflow masks generated match the data path flow. */
982 miss->flow.vlan_tci = 0;
985 /* Do not install a flow into the datapath if:
987 * - The datapath already has too many flows.
989 * - An earlier iteration of this loop already put the same flow.
991 * - We received this packet via some flow installed in the kernel
995 && upcall->dpif_upcall.type == DPIF_UC_MISS) {
1001 atomic_read(&enable_megaflows, &megaflow);
1002 ofpbuf_use_stack(&mask, &miss->mask_buf, sizeof miss->mask_buf);
1006 max_mpls = ofproto_dpif_get_max_mpls_depth(miss->ofproto);
1007 odp_flow_key_from_mask(&mask, &miss->xout.wc.masks,
1008 &miss->flow, UINT32_MAX, max_mpls);
1012 op->type = DPIF_OP_FLOW_PUT;
1013 op->u.flow_put.flags = DPIF_FP_CREATE;
1014 op->u.flow_put.key = miss->key;
1015 op->u.flow_put.key_len = miss->key_len;
1016 op->u.flow_put.mask = ofpbuf_data(&mask);
1017 op->u.flow_put.mask_len = ofpbuf_size(&mask);
1018 op->u.flow_put.stats = NULL;
1020 if (!miss->xout.slow) {
1021 op->u.flow_put.actions = ofpbuf_data(&miss->xout.odp_actions);
1022 op->u.flow_put.actions_len = ofpbuf_size(&miss->xout.odp_actions);
1026 ofpbuf_use_stack(&buf, miss->slow_path_buf,
1027 sizeof miss->slow_path_buf);
1028 compose_slow_path(udpif, &miss->xout, &miss->flow,
1029 miss->odp_in_port, &buf);
1030 op->u.flow_put.actions = ofpbuf_data(&buf);
1031 op->u.flow_put.actions_len = ofpbuf_size(&buf);
1036 * The 'miss' may be shared by multiple upcalls. Restore
1037 * the saved flow vlan_tci field before processing the next
1039 miss->flow.vlan_tci = flow_vlan_tci;
1041 if (ofpbuf_size(&miss->xout.odp_actions)) {
1044 op->type = DPIF_OP_EXECUTE;
1045 op->u.execute.packet = packet;
1046 odp_key_to_pkt_metadata(miss->key, miss->key_len,
1048 op->u.execute.actions = ofpbuf_data(&miss->xout.odp_actions);
1049 op->u.execute.actions_len = ofpbuf_size(&miss->xout.odp_actions);
1050 op->u.execute.needs_help = (miss->xout.slow & SLOW_ACTION) != 0;
1054 /* Special case for fail-open mode.
1056 * If we are in fail-open mode, but we are connected to a controller too,
1057 * then we should send the packet up to the controller in the hope that it
1058 * will try to set up a flow and thereby allow us to exit fail-open.
1060 * See the top-level comment in fail-open.c for more information.
1062 * Copy packets before they are modified by execution. */
1064 for (i = 0; i < n_upcalls; i++) {
1065 struct upcall *upcall = &upcalls[i];
1066 struct flow_miss *miss = upcall->flow_miss;
1067 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
1068 struct ofproto_packet_in *pin;
1070 pin = xmalloc(sizeof *pin);
1071 pin->up.packet = xmemdup(ofpbuf_data(packet), ofpbuf_size(packet));
1072 pin->up.packet_len = ofpbuf_size(packet);
1073 pin->up.reason = OFPR_NO_MATCH;
1074 pin->up.table_id = 0;
1075 pin->up.cookie = OVS_BE64_MAX;
1076 flow_get_metadata(&miss->flow, &pin->up.fmd);
1077 pin->send_len = 0; /* Not used for flow table misses. */
1078 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
1079 ofproto_dpif_send_packet_in(miss->ofproto, pin);
1083 /* Execute batch. */
1084 for (i = 0; i < n_ops; i++) {
1087 dpif_operate(udpif->dpif, opsp, n_ops);
1090 /* Must be called with udpif->ukeys[hash % udpif->n_revalidators].mutex. */
1091 static struct udpif_key *
1092 ukey_lookup__(struct udpif *udpif, const struct nlattr *key, size_t key_len,
1095 struct udpif_key *ukey;
1096 struct hmap *hmap = &udpif->ukeys[hash % udpif->n_revalidators].hmap;
1098 HMAP_FOR_EACH_WITH_HASH (ukey, hmap_node, hash, hmap) {
1099 if (ukey->key_len == key_len && !memcmp(ukey->key, key, key_len)) {
1106 static struct udpif_key *
1107 ukey_lookup(struct udpif *udpif, const struct nlattr *key, size_t key_len,
1110 struct udpif_key *ukey;
1111 uint32_t idx = hash % udpif->n_revalidators;
1113 ovs_mutex_lock(&udpif->ukeys[idx].mutex);
1114 ukey = ukey_lookup__(udpif, key, key_len, hash);
1115 ovs_mutex_unlock(&udpif->ukeys[idx].mutex);
1120 static struct udpif_key *
1121 ukey_create(const struct nlattr *key, size_t key_len, long long int used)
1123 struct udpif_key *ukey = xmalloc(sizeof *ukey);
1124 ovs_mutex_init(&ukey->mutex);
1126 ukey->key = (struct nlattr *) &ukey->key_buf;
1127 memcpy(&ukey->key_buf, key, key_len);
1128 ukey->key_len = key_len;
1130 ovs_mutex_lock(&ukey->mutex);
1132 ukey->flow_exists = true;
1133 ukey->created = used ? used : time_msec();
1134 memset(&ukey->stats, 0, sizeof ukey->stats);
1135 ukey->xcache = NULL;
1136 ovs_mutex_unlock(&ukey->mutex);
1141 /* Checks for a ukey in 'udpif->ukeys' with the same 'ukey->key' and 'hash',
1142 * and inserts 'ukey' if it does not exist.
1144 * Returns true if 'ukey' was inserted into 'udpif->ukeys', false otherwise. */
1146 udpif_insert_ukey(struct udpif *udpif, struct udpif_key *ukey, uint32_t hash)
1148 struct udpif_key *duplicate;
1149 uint32_t idx = hash % udpif->n_revalidators;
1152 ovs_mutex_lock(&udpif->ukeys[idx].mutex);
1153 duplicate = ukey_lookup__(udpif, ukey->key, ukey->key_len, hash);
1157 hmap_insert(&udpif->ukeys[idx].hmap, &ukey->hmap_node, hash);
1160 ovs_mutex_unlock(&udpif->ukeys[idx].mutex);
1166 ukey_delete(struct revalidator *revalidator, struct udpif_key *ukey)
1167 OVS_NO_THREAD_SAFETY_ANALYSIS
1170 hmap_remove(revalidator->ukeys, &ukey->hmap_node);
1172 xlate_cache_delete(ukey->xcache);
1173 ovs_mutex_destroy(&ukey->mutex);
1178 should_revalidate(const struct udpif *udpif, uint64_t packets,
1181 long long int metric, now, duration;
1183 if (udpif->dump_duration < 200) {
1184 /* We are likely to handle full revalidation for the flows. */
1188 /* Calculate the mean time between seeing these packets. If this
1189 * exceeds the threshold, then delete the flow rather than performing
1190 * costly revalidation for flows that aren't being hit frequently.
1192 * This is targeted at situations where the dump_duration is high (~1s),
1193 * and revalidation is triggered by a call to udpif_revalidate(). In
1194 * these situations, revalidation of all flows causes fluctuations in the
1195 * flow_limit due to the interaction with the dump_duration and max_idle.
1196 * This tends to result in deletion of low-throughput flows anyway, so
1197 * skip the revalidation and just delete those flows. */
1198 packets = MAX(packets, 1);
1199 now = MAX(used, time_msec());
1200 duration = now - used;
1201 metric = duration / packets;
1204 /* The flow is receiving more than ~5pps, so keep it. */
1211 revalidate_ukey(struct udpif *udpif, struct udpif_key *ukey,
1212 const struct nlattr *mask, size_t mask_len,
1213 const struct nlattr *actions, size_t actions_len,
1214 const struct dpif_flow_stats *stats)
1215 OVS_REQUIRES(ukey->mutex)
1217 uint64_t slow_path_buf[128 / 8];
1218 struct xlate_out xout, *xoutp;
1219 struct netflow *netflow;
1220 struct ofproto_dpif *ofproto;
1221 struct dpif_flow_stats push;
1222 struct ofpbuf xout_actions;
1223 struct flow flow, dp_mask;
1224 uint32_t *dp32, *xout32;
1225 odp_port_t odp_in_port;
1226 struct xlate_in xin;
1227 long long int last_used;
1236 last_used = ukey->stats.used;
1237 push.used = stats->used;
1238 push.tcp_flags = stats->tcp_flags;
1239 push.n_packets = stats->n_packets > ukey->stats.n_packets
1240 ? stats->n_packets - ukey->stats.n_packets
1242 push.n_bytes = stats->n_bytes > ukey->stats.n_bytes
1243 ? stats->n_bytes - ukey->stats.n_bytes
1246 if (udpif->need_revalidate && last_used
1247 && !should_revalidate(udpif, push.n_packets, last_used)) {
1252 /* We will push the stats, so update the ukey stats cache. */
1253 ukey->stats = *stats;
1254 if (!push.n_packets && !udpif->need_revalidate) {
1259 may_learn = push.n_packets > 0;
1260 if (ukey->xcache && !udpif->need_revalidate) {
1261 xlate_push_stats(ukey->xcache, may_learn, &push);
1266 error = xlate_receive(udpif->backer, NULL, ukey->key, ukey->key_len, &flow,
1267 &ofproto, NULL, NULL, &netflow, &odp_in_port);
1272 if (udpif->need_revalidate) {
1273 xlate_cache_clear(ukey->xcache);
1275 if (!ukey->xcache) {
1276 ukey->xcache = xlate_cache_new();
1279 xlate_in_init(&xin, ofproto, &flow, NULL, push.tcp_flags, NULL);
1280 xin.resubmit_stats = push.n_packets ? &push : NULL;
1281 xin.xcache = ukey->xcache;
1282 xin.may_learn = may_learn;
1283 xin.skip_wildcards = !udpif->need_revalidate;
1284 xlate_actions(&xin, &xout);
1287 if (!udpif->need_revalidate) {
1293 ofpbuf_use_const(&xout_actions, ofpbuf_data(&xout.odp_actions),
1294 ofpbuf_size(&xout.odp_actions));
1296 ofpbuf_use_stack(&xout_actions, slow_path_buf, sizeof slow_path_buf);
1297 compose_slow_path(udpif, &xout, &flow, odp_in_port, &xout_actions);
1300 if (actions_len != ofpbuf_size(&xout_actions)
1301 || memcmp(ofpbuf_data(&xout_actions), actions, actions_len)) {
1305 if (odp_flow_key_to_mask(mask, mask_len, &dp_mask, &flow)
1310 /* Since the kernel is free to ignore wildcarded bits in the mask, we can't
1311 * directly check that the masks are the same. Instead we check that the
1312 * mask in the kernel is more specific i.e. less wildcarded, than what
1313 * we've calculated here. This guarantees we don't catch any packets we
1314 * shouldn't with the megaflow. */
1315 dp32 = (uint32_t *) &dp_mask;
1316 xout32 = (uint32_t *) &xout.wc.masks;
1317 for (i = 0; i < FLOW_U32S; i++) {
1318 if ((dp32[i] | xout32[i]) != dp32[i]) {
1327 netflow_flow_clear(netflow, &flow);
1329 netflow_unref(netflow);
1331 xlate_out_uninit(xoutp);
1336 struct udpif_key *ukey;
1337 struct dpif_flow_stats stats; /* Stats for 'op'. */
1338 struct dpif_op op; /* Flow del operation. */
1342 dump_op_init(struct dump_op *op, const struct nlattr *key, size_t key_len,
1343 struct udpif_key *ukey)
1346 op->op.type = DPIF_OP_FLOW_DEL;
1347 op->op.u.flow_del.key = key;
1348 op->op.u.flow_del.key_len = key_len;
1349 op->op.u.flow_del.stats = &op->stats;
1353 push_dump_ops__(struct udpif *udpif, struct dump_op *ops, size_t n_ops)
1355 struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
1358 ovs_assert(n_ops <= REVALIDATE_MAX_BATCH);
1359 for (i = 0; i < n_ops; i++) {
1360 opsp[i] = &ops[i].op;
1362 dpif_operate(udpif->dpif, opsp, n_ops);
1364 for (i = 0; i < n_ops; i++) {
1365 struct dump_op *op = &ops[i];
1366 struct dpif_flow_stats *push, *stats, push_buf;
1368 stats = op->op.u.flow_del.stats;
1371 ovs_mutex_lock(&op->ukey->mutex);
1372 push->used = MAX(stats->used, op->ukey->stats.used);
1373 push->tcp_flags = stats->tcp_flags | op->ukey->stats.tcp_flags;
1374 push->n_packets = stats->n_packets - op->ukey->stats.n_packets;
1375 push->n_bytes = stats->n_bytes - op->ukey->stats.n_bytes;
1376 ovs_mutex_unlock(&op->ukey->mutex);
1381 if (push->n_packets || netflow_exists()) {
1382 struct ofproto_dpif *ofproto;
1383 struct netflow *netflow;
1387 may_learn = push->n_packets > 0;
1389 ovs_mutex_lock(&op->ukey->mutex);
1390 if (op->ukey->xcache) {
1391 xlate_push_stats(op->ukey->xcache, may_learn, push);
1392 ovs_mutex_unlock(&op->ukey->mutex);
1395 ovs_mutex_unlock(&op->ukey->mutex);
1398 if (!xlate_receive(udpif->backer, NULL, op->op.u.flow_del.key,
1399 op->op.u.flow_del.key_len, &flow, &ofproto,
1400 NULL, NULL, &netflow, NULL)) {
1401 struct xlate_in xin;
1403 xlate_in_init(&xin, ofproto, &flow, NULL, push->tcp_flags,
1405 xin.resubmit_stats = push->n_packets ? push : NULL;
1406 xin.may_learn = may_learn;
1407 xin.skip_wildcards = true;
1408 xlate_actions_for_side_effects(&xin);
1411 netflow_flow_clear(netflow, &flow);
1412 netflow_unref(netflow);
1420 push_dump_ops(struct revalidator *revalidator,
1421 struct dump_op *ops, size_t n_ops)
1425 push_dump_ops__(revalidator->udpif, ops, n_ops);
1426 for (i = 0; i < n_ops; i++) {
1427 ukey_delete(revalidator, ops[i].ukey);
1432 revalidate(struct revalidator *revalidator)
1434 struct udpif *udpif = revalidator->udpif;
1436 struct dump_op ops[REVALIDATE_MAX_BATCH];
1437 const struct nlattr *key, *mask, *actions;
1438 size_t key_len, mask_len, actions_len;
1439 const struct dpif_flow_stats *stats;
1441 unsigned int flow_limit;
1447 atomic_read(&udpif->flow_limit, &flow_limit);
1449 dpif_flow_dump_state_init(udpif->dpif, &state);
1450 while (dpif_flow_dump_next(&udpif->dump, state, &key, &key_len, &mask,
1451 &mask_len, &actions, &actions_len, &stats)) {
1452 struct udpif_key *ukey;
1453 bool mark, may_destroy;
1454 long long int used, max_idle;
1458 hash = hash_bytes(key, key_len, udpif->secret);
1459 ukey = ukey_lookup(udpif, key, key_len, hash);
1463 ukey = ukey_create(key, key_len, used);
1464 if (!udpif_insert_ukey(udpif, ukey, hash)) {
1465 /* The same ukey has already been created. This means that
1466 * another revalidator is processing this flow
1467 * concurrently, so don't bother processing it. */
1468 COVERAGE_INC(upcall_duplicate_flow);
1469 ukey_delete(NULL, ukey);
1474 if (ovs_mutex_trylock(&ukey->mutex)) {
1475 /* The flow has been dumped, and is being handled by another
1476 * revalidator concurrently. This can occasionally occur if the
1477 * datapath is changed in the middle of a flow dump. Rather than
1478 * perform the same work twice, skip the flow this time. */
1479 COVERAGE_INC(upcall_duplicate_flow);
1483 if (ukey->mark || !ukey->flow_exists) {
1484 /* The flow has already been dumped and handled by another
1485 * revalidator during this flow dump operation. Skip it. */
1486 COVERAGE_INC(upcall_duplicate_flow);
1487 ovs_mutex_unlock(&ukey->mutex);
1492 used = ukey->created;
1494 n_flows = udpif_get_n_flows(udpif);
1495 max_idle = ofproto_max_idle;
1496 if (n_flows > flow_limit) {
1500 if ((used && used < now - max_idle) || n_flows > flow_limit * 2) {
1503 mark = revalidate_ukey(udpif, ukey, mask, mask_len, actions,
1504 actions_len, stats);
1506 ukey->mark = ukey->flow_exists = mark;
1509 dump_op_init(&ops[n_ops++], key, key_len, ukey);
1511 ovs_mutex_unlock(&ukey->mutex);
1514 may_destroy = dpif_flow_dump_next_may_destroy_keys(&udpif->dump,
1517 /* Only update 'now' immediately before 'buffer' will be updated.
1518 * This gives us the current time relative to the time the datapath
1519 * will write into 'stats'. */
1524 /* Only do a dpif_operate when we've hit our maximum batch, or when our
1525 * memory is about to be clobbered by the next call to
1526 * dpif_flow_dump_next(). */
1527 if (n_ops == REVALIDATE_MAX_BATCH || (n_ops && may_destroy)) {
1528 push_dump_ops__(udpif, ops, n_ops);
1534 push_dump_ops__(udpif, ops, n_ops);
1537 dpif_flow_dump_state_uninit(udpif->dpif, state);
1540 /* Called with exclusive access to 'revalidator' and 'ukey'. */
1542 handle_missed_revalidation(struct revalidator *revalidator,
1543 struct udpif_key *ukey)
1544 OVS_NO_THREAD_SAFETY_ANALYSIS
1546 struct udpif *udpif = revalidator->udpif;
1547 struct nlattr *mask, *actions;
1548 size_t mask_len, actions_len;
1549 struct dpif_flow_stats stats;
1553 COVERAGE_INC(revalidate_missed_dp_flow);
1555 if (!dpif_flow_get(udpif->dpif, ukey->key, ukey->key_len, &buf,
1556 &mask, &mask_len, &actions, &actions_len, &stats)) {
1557 keep = revalidate_ukey(udpif, ukey, mask, mask_len, actions,
1558 actions_len, &stats);
1566 revalidator_sweep__(struct revalidator *revalidator, bool purge)
1567 OVS_NO_THREAD_SAFETY_ANALYSIS
1569 struct dump_op ops[REVALIDATE_MAX_BATCH];
1570 struct udpif_key *ukey, *next;
1575 /* During garbage collection, this revalidator completely owns its ukeys
1576 * map, and therefore doesn't need to do any locking. */
1577 HMAP_FOR_EACH_SAFE (ukey, next, hmap_node, revalidator->ukeys) {
1578 if (ukey->flow_exists) {
1579 bool missed_flow = !ukey->mark;
1584 && revalidator->udpif->need_revalidate
1585 && !handle_missed_revalidation(revalidator, ukey))) {
1586 struct dump_op *op = &ops[n_ops++];
1588 dump_op_init(op, ukey->key, ukey->key_len, ukey);
1589 if (n_ops == REVALIDATE_MAX_BATCH) {
1590 push_dump_ops(revalidator, ops, n_ops);
1595 ukey_delete(revalidator, ukey);
1600 push_dump_ops(revalidator, ops, n_ops);
1605 revalidator_sweep(struct revalidator *revalidator)
1607 revalidator_sweep__(revalidator, false);
1611 revalidator_purge(struct revalidator *revalidator)
1613 revalidator_sweep__(revalidator, true);
1617 upcall_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1618 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
1620 struct ds ds = DS_EMPTY_INITIALIZER;
1621 struct udpif *udpif;
1623 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1624 unsigned int flow_limit;
1627 atomic_read(&udpif->flow_limit, &flow_limit);
1629 ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
1630 ds_put_format(&ds, "\tflows : (current %lu)"
1631 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
1632 udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
1633 ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
1635 ds_put_char(&ds, '\n');
1636 for (i = 0; i < n_revalidators; i++) {
1637 struct revalidator *revalidator = &udpif->revalidators[i];
1639 ovs_mutex_lock(&udpif->ukeys[i].mutex);
1640 ds_put_format(&ds, "\t%u: (keys %"PRIuSIZE")\n",
1641 revalidator->id, hmap_count(&udpif->ukeys[i].hmap));
1642 ovs_mutex_unlock(&udpif->ukeys[i].mutex);
1646 unixctl_command_reply(conn, ds_cstr(&ds));
1650 /* Disable using the megaflows.
1652 * This command is only needed for advanced debugging, so it's not
1653 * documented in the man page. */
1655 upcall_unixctl_disable_megaflows(struct unixctl_conn *conn,
1656 int argc OVS_UNUSED,
1657 const char *argv[] OVS_UNUSED,
1658 void *aux OVS_UNUSED)
1660 atomic_store(&enable_megaflows, false);
1661 udpif_flush_all_datapaths();
1662 unixctl_command_reply(conn, "megaflows disabled");
1665 /* Re-enable using megaflows.
1667 * This command is only needed for advanced debugging, so it's not
1668 * documented in the man page. */
1670 upcall_unixctl_enable_megaflows(struct unixctl_conn *conn,
1671 int argc OVS_UNUSED,
1672 const char *argv[] OVS_UNUSED,
1673 void *aux OVS_UNUSED)
1675 atomic_store(&enable_megaflows, true);
1676 udpif_flush_all_datapaths();
1677 unixctl_command_reply(conn, "megaflows enabled");
1680 /* Set the flow limit.
1682 * This command is only needed for advanced debugging, so it's not
1683 * documented in the man page. */
1685 upcall_unixctl_set_flow_limit(struct unixctl_conn *conn,
1686 int argc OVS_UNUSED,
1687 const char *argv[] OVS_UNUSED,
1688 void *aux OVS_UNUSED)
1690 struct ds ds = DS_EMPTY_INITIALIZER;
1691 struct udpif *udpif;
1692 unsigned int flow_limit = atoi(argv[1]);
1694 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1695 atomic_store(&udpif->flow_limit, flow_limit);
1697 ds_put_format(&ds, "set flow_limit to %u\n", flow_limit);
1698 unixctl_command_reply(conn, ds_cstr(&ds));