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"
36 #include "poll-loop.h"
41 #define MAX_QUEUE_LENGTH 512
42 #define FLOW_MISS_MAX_BATCH 50
43 #define REVALIDATE_MAX_BATCH 50
46 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall);
48 COVERAGE_DEFINE(upcall_queue_overflow);
50 /* A thread that processes each upcall handed to it by the dispatcher thread,
51 * forwards the upcall's packet, and possibly sets up a kernel flow as a
54 struct udpif *udpif; /* Parent udpif. */
55 pthread_t thread; /* Thread ID. */
56 char *name; /* Thread name. */
58 struct ovs_mutex mutex; /* Mutex guarding the following. */
60 /* Atomic queue of unprocessed upcalls. */
61 struct list upcalls OVS_GUARDED;
62 size_t n_upcalls OVS_GUARDED;
64 bool need_signal; /* Only changed by the dispatcher. */
66 pthread_cond_t wake_cond; /* Wakes 'thread' while holding
70 /* A thread that processes each kernel flow handed to it by the flow_dumper
71 * thread, updates OpenFlow statistics, and updates or removes the kernel flow
74 struct udpif *udpif; /* Parent udpif. */
75 char *name; /* Thread name. */
77 pthread_t thread; /* Thread ID. */
78 struct hmap ukeys; /* Datapath flow keys. */
82 struct ovs_mutex mutex; /* Mutex guarding the following. */
83 pthread_cond_t wake_cond;
84 struct list udumps OVS_GUARDED; /* Unprocessed udumps. */
85 size_t n_udumps OVS_GUARDED; /* Number of unprocessed udumps. */
88 /* An upcall handler for ofproto_dpif.
90 * udpif has two logically separate pieces:
92 * - A "dispatcher" thread that reads upcalls from the kernel and dispatches
93 * them to one of several "handler" threads (see struct handler).
95 * - A "flow_dumper" thread that reads the kernel flow table and dispatches
96 * flows to one of several "revalidator" threads (see struct
99 struct list list_node; /* In all_udpifs list. */
101 struct dpif *dpif; /* Datapath handle. */
102 struct dpif_backer *backer; /* Opaque dpif_backer pointer. */
104 uint32_t secret; /* Random seed for upcall hash. */
106 pthread_t dispatcher; /* Dispatcher thread ID. */
107 pthread_t flow_dumper; /* Flow dumper thread ID. */
109 struct handler *handlers; /* Upcall handlers. */
112 struct revalidator *revalidators; /* Flow revalidators. */
113 size_t n_revalidators;
115 uint64_t last_reval_seq; /* 'reval_seq' at last revalidation. */
116 struct seq *reval_seq; /* Incremented to force revalidation. */
118 struct seq *dump_seq; /* Increments each dump iteration. */
120 struct latch exit_latch; /* Tells child threads to exit. */
122 long long int dump_duration; /* Duration of the last flow dump. */
124 /* Datapath flow statistics. */
125 unsigned int max_n_flows;
126 unsigned int avg_n_flows;
128 /* Following fields are accessed and modified by different threads. */
129 atomic_uint flow_limit; /* Datapath flow hard limit. */
131 /* n_flows_mutex prevents multiple threads updating these concurrently. */
132 atomic_uint64_t n_flows; /* Number of flows in the datapath. */
133 atomic_llong n_flows_timestamp; /* Last time n_flows was updated. */
134 struct ovs_mutex n_flows_mutex;
138 BAD_UPCALL, /* Some kind of bug somewhere. */
139 MISS_UPCALL, /* A flow miss. */
140 SFLOW_UPCALL, /* sFlow sample. */
141 FLOW_SAMPLE_UPCALL, /* Per-flow sampling. */
142 IPFIX_UPCALL /* Per-bridge sampling. */
146 struct list list_node; /* For queuing upcalls. */
147 struct flow_miss *flow_miss; /* This upcall's flow_miss. */
149 /* Raw upcall plus data for keeping track of the memory backing it. */
150 struct dpif_upcall dpif_upcall; /* As returned by dpif_recv() */
151 struct ofpbuf upcall_buf; /* Owns some data in 'dpif_upcall'. */
152 uint64_t upcall_stub[512 / 8]; /* Buffer to reduce need for malloc(). */
155 /* 'udpif_key's are responsible for tracking the little bit of state udpif
156 * needs to do flow expiration which can't be pulled directly from the
157 * datapath. They are owned, created by, maintained, and destroyed by a single
158 * revalidator making them easy to efficiently handle with multiple threads. */
160 struct hmap_node hmap_node; /* In parent revalidator 'ukeys' map. */
162 struct nlattr *key; /* Datapath flow key. */
163 size_t key_len; /* Length of 'key'. */
165 struct dpif_flow_stats stats; /* Stats at most recent flow dump. */
166 long long int created; /* Estimation of creation time. */
168 bool mark; /* Used by mark and sweep GC algorithm. */
170 struct odputil_keybuf key_buf; /* Memory for 'key'. */
173 /* 'udpif_flow_dump's hold the state associated with one iteration in a flow
174 * dump operation. This is created by the flow_dumper thread and handed to the
175 * appropriate revalidator thread to be processed. */
176 struct udpif_flow_dump {
177 struct list list_node;
179 struct nlattr *key; /* Datapath flow key. */
180 size_t key_len; /* Length of 'key'. */
181 uint32_t key_hash; /* Hash of 'key'. */
183 struct odputil_keybuf mask_buf;
184 struct nlattr *mask; /* Datapath mask for 'key'. */
185 size_t mask_len; /* Length of 'mask'. */
187 struct dpif_flow_stats stats; /* Stats pulled from the datapath. */
189 bool need_revalidate; /* Key needs revalidation? */
191 struct odputil_keybuf key_buf;
194 /* Flow miss batching.
196 * Some dpifs implement operations faster when you hand them off in a batch.
197 * To allow batching, "struct flow_miss" queues the dpif-related work needed
198 * for a given flow. Each "struct flow_miss" corresponds to sending one or
199 * more packets, plus possibly installing the flow in the dpif. */
201 struct hmap_node hmap_node;
202 struct ofproto_dpif *ofproto;
205 enum odp_key_fitness key_fitness;
206 const struct nlattr *key;
208 enum dpif_upcall_type upcall_type;
209 struct dpif_flow_stats stats;
210 odp_port_t odp_in_port;
212 uint64_t slow_path_buf[128 / 8];
213 struct odputil_keybuf mask_buf;
215 struct xlate_out xout;
220 static void upcall_destroy(struct upcall *);
222 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
223 static struct list all_udpifs = LIST_INITIALIZER(&all_udpifs);
225 static void recv_upcalls(struct udpif *);
226 static void handle_upcalls(struct handler *handler, struct list *upcalls);
227 static void *udpif_flow_dumper(void *);
228 static void *udpif_dispatcher(void *);
229 static void *udpif_upcall_handler(void *);
230 static void *udpif_revalidator(void *);
231 static uint64_t udpif_get_n_flows(struct udpif *);
232 static void revalidate_udumps(struct revalidator *, struct list *udumps);
233 static void revalidator_sweep(struct revalidator *);
234 static void upcall_unixctl_show(struct unixctl_conn *conn, int argc,
235 const char *argv[], void *aux);
236 static void upcall_unixctl_disable_megaflows(struct unixctl_conn *, int argc,
237 const char *argv[], void *aux);
238 static void upcall_unixctl_enable_megaflows(struct unixctl_conn *, int argc,
239 const char *argv[], void *aux);
240 static void ukey_delete(struct revalidator *, struct udpif_key *);
242 static atomic_bool enable_megaflows = ATOMIC_VAR_INIT(true);
245 udpif_create(struct dpif_backer *backer, struct dpif *dpif)
247 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
248 struct udpif *udpif = xzalloc(sizeof *udpif);
250 if (ovsthread_once_start(&once)) {
251 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show,
253 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
254 upcall_unixctl_disable_megaflows, NULL);
255 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
256 upcall_unixctl_enable_megaflows, NULL);
257 ovsthread_once_done(&once);
261 udpif->backer = backer;
262 atomic_init(&udpif->flow_limit, MIN(ofproto_flow_limit, 10000));
263 udpif->secret = random_uint32();
264 udpif->reval_seq = seq_create();
265 udpif->dump_seq = seq_create();
266 latch_init(&udpif->exit_latch);
267 list_push_back(&all_udpifs, &udpif->list_node);
268 atomic_init(&udpif->n_flows, 0);
269 atomic_init(&udpif->n_flows_timestamp, LLONG_MIN);
270 ovs_mutex_init(&udpif->n_flows_mutex);
276 udpif_destroy(struct udpif *udpif)
278 udpif_set_threads(udpif, 0, 0);
281 list_remove(&udpif->list_node);
282 latch_destroy(&udpif->exit_latch);
283 seq_destroy(udpif->reval_seq);
284 seq_destroy(udpif->dump_seq);
285 atomic_destroy(&udpif->flow_limit);
286 atomic_destroy(&udpif->n_flows);
287 atomic_destroy(&udpif->n_flows_timestamp);
288 ovs_mutex_destroy(&udpif->n_flows_mutex);
292 /* Tells 'udpif' how many threads it should use to handle upcalls. Disables
293 * all threads if 'n_handlers' and 'n_revalidators' is zero. 'udpif''s
294 * datapath handle must have packet reception enabled before starting threads.
297 udpif_set_threads(struct udpif *udpif, size_t n_handlers,
298 size_t n_revalidators)
300 /* Stop the old threads (if any). */
301 if (udpif->handlers &&
302 (udpif->n_handlers != n_handlers
303 || udpif->n_revalidators != n_revalidators)) {
306 latch_set(&udpif->exit_latch);
308 for (i = 0; i < udpif->n_handlers; i++) {
309 struct handler *handler = &udpif->handlers[i];
311 ovs_mutex_lock(&handler->mutex);
312 xpthread_cond_signal(&handler->wake_cond);
313 ovs_mutex_unlock(&handler->mutex);
314 xpthread_join(handler->thread, NULL);
317 for (i = 0; i < udpif->n_revalidators; i++) {
318 struct revalidator *revalidator = &udpif->revalidators[i];
320 ovs_mutex_lock(&revalidator->mutex);
321 xpthread_cond_signal(&revalidator->wake_cond);
322 ovs_mutex_unlock(&revalidator->mutex);
323 xpthread_join(revalidator->thread, NULL);
326 xpthread_join(udpif->flow_dumper, NULL);
327 xpthread_join(udpif->dispatcher, NULL);
329 for (i = 0; i < udpif->n_revalidators; i++) {
330 struct revalidator *revalidator = &udpif->revalidators[i];
331 struct udpif_flow_dump *udump, *next_udump;
332 struct udpif_key *ukey, *next_ukey;
334 LIST_FOR_EACH_SAFE (udump, next_udump, list_node,
335 &revalidator->udumps) {
336 list_remove(&udump->list_node);
340 HMAP_FOR_EACH_SAFE (ukey, next_ukey, hmap_node,
341 &revalidator->ukeys) {
342 ukey_delete(revalidator, ukey);
344 hmap_destroy(&revalidator->ukeys);
345 ovs_mutex_destroy(&revalidator->mutex);
347 free(revalidator->name);
350 for (i = 0; i < udpif->n_handlers; i++) {
351 struct handler *handler = &udpif->handlers[i];
352 struct upcall *miss, *next;
354 LIST_FOR_EACH_SAFE (miss, next, list_node, &handler->upcalls) {
355 list_remove(&miss->list_node);
356 upcall_destroy(miss);
358 ovs_mutex_destroy(&handler->mutex);
360 xpthread_cond_destroy(&handler->wake_cond);
363 latch_poll(&udpif->exit_latch);
365 free(udpif->revalidators);
366 udpif->revalidators = NULL;
367 udpif->n_revalidators = 0;
369 free(udpif->handlers);
370 udpif->handlers = NULL;
371 udpif->n_handlers = 0;
374 /* Start new threads (if necessary). */
375 if (!udpif->handlers && n_handlers) {
378 udpif->n_handlers = n_handlers;
379 udpif->n_revalidators = n_revalidators;
381 udpif->handlers = xzalloc(udpif->n_handlers * sizeof *udpif->handlers);
382 for (i = 0; i < udpif->n_handlers; i++) {
383 struct handler *handler = &udpif->handlers[i];
385 handler->udpif = udpif;
386 list_init(&handler->upcalls);
387 handler->need_signal = false;
388 xpthread_cond_init(&handler->wake_cond, NULL);
389 ovs_mutex_init(&handler->mutex);
390 xpthread_create(&handler->thread, NULL, udpif_upcall_handler,
394 udpif->revalidators = xzalloc(udpif->n_revalidators
395 * sizeof *udpif->revalidators);
396 for (i = 0; i < udpif->n_revalidators; i++) {
397 struct revalidator *revalidator = &udpif->revalidators[i];
399 revalidator->udpif = udpif;
400 list_init(&revalidator->udumps);
401 hmap_init(&revalidator->ukeys);
402 ovs_mutex_init(&revalidator->mutex);
403 xpthread_cond_init(&revalidator->wake_cond, NULL);
404 xpthread_create(&revalidator->thread, NULL, udpif_revalidator,
407 xpthread_create(&udpif->dispatcher, NULL, udpif_dispatcher, udpif);
408 xpthread_create(&udpif->flow_dumper, NULL, udpif_flow_dumper, udpif);
412 /* Notifies 'udpif' that something changed which may render previous
413 * xlate_actions() results invalid. */
415 udpif_revalidate(struct udpif *udpif)
417 seq_change(udpif->reval_seq);
420 /* Returns a seq which increments every time 'udpif' pulls stats from the
421 * datapath. Callers can use this to get a sense of when might be a good time
422 * to do periodic work which relies on relatively up to date statistics. */
424 udpif_dump_seq(struct udpif *udpif)
426 return udpif->dump_seq;
430 udpif_get_memory_usage(struct udpif *udpif, struct simap *usage)
434 simap_increase(usage, "dispatchers", 1);
435 simap_increase(usage, "flow_dumpers", 1);
437 simap_increase(usage, "handlers", udpif->n_handlers);
438 for (i = 0; i < udpif->n_handlers; i++) {
439 struct handler *handler = &udpif->handlers[i];
440 ovs_mutex_lock(&handler->mutex);
441 simap_increase(usage, "handler upcalls", handler->n_upcalls);
442 ovs_mutex_unlock(&handler->mutex);
445 simap_increase(usage, "revalidators", udpif->n_revalidators);
446 for (i = 0; i < udpif->n_revalidators; i++) {
447 struct revalidator *revalidator = &udpif->revalidators[i];
448 ovs_mutex_lock(&revalidator->mutex);
449 simap_increase(usage, "revalidator dumps", revalidator->n_udumps);
451 /* XXX: This isn't technically thread safe because the revalidator
452 * ukeys maps isn't protected by a mutex since it's per thread. */
453 simap_increase(usage, "revalidator keys",
454 hmap_count(&revalidator->ukeys));
455 ovs_mutex_unlock(&revalidator->mutex);
459 /* Removes all flows from all datapaths. */
465 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
466 dpif_flow_flush(udpif->dpif);
470 /* Destroys and deallocates 'upcall'. */
472 upcall_destroy(struct upcall *upcall)
475 ofpbuf_uninit(&upcall->dpif_upcall.packet);
476 ofpbuf_uninit(&upcall->upcall_buf);
482 udpif_get_n_flows(struct udpif *udpif)
484 long long int time, now;
488 atomic_read(&udpif->n_flows_timestamp, &time);
489 if (time < now - 100 && !ovs_mutex_trylock(&udpif->n_flows_mutex)) {
490 struct dpif_dp_stats stats;
492 atomic_store(&udpif->n_flows_timestamp, now);
493 dpif_get_dp_stats(udpif->dpif, &stats);
494 flow_count = stats.n_flows;
495 atomic_store(&udpif->n_flows, flow_count);
496 ovs_mutex_unlock(&udpif->n_flows_mutex);
498 atomic_read(&udpif->n_flows, &flow_count);
503 /* The dispatcher thread is responsible for receiving upcalls from the kernel,
504 * assigning them to a upcall_handler thread. */
506 udpif_dispatcher(void *arg)
508 struct udpif *udpif = arg;
510 set_subprogram_name("dispatcher");
511 while (!latch_is_set(&udpif->exit_latch)) {
513 dpif_recv_wait(udpif->dpif);
514 latch_wait(&udpif->exit_latch);
522 udpif_flow_dumper(void *arg)
524 struct udpif *udpif = arg;
526 set_subprogram_name("flow_dumper");
527 while (!latch_is_set(&udpif->exit_latch)) {
528 const struct dpif_flow_stats *stats;
529 long long int start_time, duration;
530 const struct nlattr *key, *mask;
531 struct dpif_flow_dump dump;
532 size_t key_len, mask_len;
533 unsigned int flow_limit;
534 bool need_revalidate;
538 reval_seq = seq_read(udpif->reval_seq);
539 need_revalidate = udpif->last_reval_seq != reval_seq;
540 udpif->last_reval_seq = reval_seq;
542 n_flows = udpif_get_n_flows(udpif);
543 udpif->max_n_flows = MAX(n_flows, udpif->max_n_flows);
544 udpif->avg_n_flows = (udpif->avg_n_flows + n_flows) / 2;
546 start_time = time_msec();
547 dpif_flow_dump_start(&dump, udpif->dpif);
548 while (dpif_flow_dump_next(&dump, &key, &key_len, &mask, &mask_len,
550 && !latch_is_set(&udpif->exit_latch)) {
551 struct udpif_flow_dump *udump = xmalloc(sizeof *udump);
552 struct revalidator *revalidator;
554 udump->key_hash = hash_bytes(key, key_len, udpif->secret);
555 memcpy(&udump->key_buf, key, key_len);
556 udump->key = (struct nlattr *) &udump->key_buf;
557 udump->key_len = key_len;
559 memcpy(&udump->mask_buf, mask, mask_len);
560 udump->mask = (struct nlattr *) &udump->mask_buf;
561 udump->mask_len = mask_len;
563 udump->stats = *stats;
564 udump->need_revalidate = need_revalidate;
566 revalidator = &udpif->revalidators[udump->key_hash
567 % udpif->n_revalidators];
569 ovs_mutex_lock(&revalidator->mutex);
570 while (revalidator->n_udumps >= REVALIDATE_MAX_BATCH * 3
571 && !latch_is_set(&udpif->exit_latch)) {
572 ovs_mutex_cond_wait(&revalidator->wake_cond,
573 &revalidator->mutex);
575 list_push_back(&revalidator->udumps, &udump->list_node);
576 revalidator->n_udumps++;
577 xpthread_cond_signal(&revalidator->wake_cond);
578 ovs_mutex_unlock(&revalidator->mutex);
580 dpif_flow_dump_done(&dump);
582 /* Let all the revalidators finish and garbage collect. */
583 seq_change(udpif->dump_seq);
584 for (i = 0; i < udpif->n_revalidators; i++) {
585 struct revalidator *revalidator = &udpif->revalidators[i];
586 ovs_mutex_lock(&revalidator->mutex);
587 xpthread_cond_signal(&revalidator->wake_cond);
588 ovs_mutex_unlock(&revalidator->mutex);
591 for (i = 0; i < udpif->n_revalidators; i++) {
592 struct revalidator *revalidator = &udpif->revalidators[i];
594 ovs_mutex_lock(&revalidator->mutex);
595 while (revalidator->dump_seq != seq_read(udpif->dump_seq)
596 && !latch_is_set(&udpif->exit_latch)) {
597 ovs_mutex_cond_wait(&revalidator->wake_cond,
598 &revalidator->mutex);
600 ovs_mutex_unlock(&revalidator->mutex);
603 duration = MAX(time_msec() - start_time, 1);
604 udpif->dump_duration = duration;
605 atomic_read(&udpif->flow_limit, &flow_limit);
606 if (duration > 2000) {
607 flow_limit /= duration / 1000;
608 } else if (duration > 1300) {
609 flow_limit = flow_limit * 3 / 4;
610 } else if (duration < 1000 && n_flows > 2000
611 && flow_limit < n_flows * 1000 / duration) {
614 flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
615 atomic_store(&udpif->flow_limit, flow_limit);
617 if (duration > 2000) {
618 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
622 poll_timer_wait_until(start_time + MIN(MAX_IDLE, 500));
623 seq_wait(udpif->reval_seq, udpif->last_reval_seq);
624 latch_wait(&udpif->exit_latch);
631 /* The miss handler thread is responsible for processing miss upcalls retrieved
632 * by the dispatcher thread. Once finished it passes the processed miss
633 * upcalls to ofproto-dpif where they're installed in the datapath. */
635 udpif_upcall_handler(void *arg)
637 struct handler *handler = arg;
639 handler->name = xasprintf("handler_%u", ovsthread_id_self());
640 set_subprogram_name("%s", handler->name);
642 while (!latch_is_set(&handler->udpif->exit_latch)) {
643 struct list misses = LIST_INITIALIZER(&misses);
646 ovs_mutex_lock(&handler->mutex);
647 if (!handler->n_upcalls) {
648 ovs_mutex_cond_wait(&handler->wake_cond, &handler->mutex);
651 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
652 if (handler->n_upcalls) {
653 handler->n_upcalls--;
654 list_push_back(&misses, list_pop_front(&handler->upcalls));
659 ovs_mutex_unlock(&handler->mutex);
661 handle_upcalls(handler, &misses);
670 udpif_revalidator(void *arg)
672 struct revalidator *revalidator = arg;
674 revalidator->name = xasprintf("revalidator_%u", ovsthread_id_self());
675 set_subprogram_name("%s", revalidator->name);
677 struct list udumps = LIST_INITIALIZER(&udumps);
678 struct udpif *udpif = revalidator->udpif;
681 ovs_mutex_lock(&revalidator->mutex);
682 if (latch_is_set(&udpif->exit_latch)) {
683 ovs_mutex_unlock(&revalidator->mutex);
687 if (!revalidator->n_udumps) {
688 if (revalidator->dump_seq != seq_read(udpif->dump_seq)) {
689 revalidator->dump_seq = seq_read(udpif->dump_seq);
690 revalidator_sweep(revalidator);
692 ovs_mutex_cond_wait(&revalidator->wake_cond,
693 &revalidator->mutex);
697 for (i = 0; i < REVALIDATE_MAX_BATCH && revalidator->n_udumps; i++) {
698 list_push_back(&udumps, list_pop_front(&revalidator->udumps));
699 revalidator->n_udumps--;
702 /* Wake up the flow dumper. */
703 xpthread_cond_signal(&revalidator->wake_cond);
704 ovs_mutex_unlock(&revalidator->mutex);
706 if (!list_is_empty(&udumps)) {
707 revalidate_udumps(revalidator, &udumps);
714 static enum upcall_type
715 classify_upcall(const struct upcall *upcall)
717 const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
718 union user_action_cookie cookie;
721 /* First look at the upcall type. */
722 switch (dpif_upcall->type) {
729 case DPIF_N_UC_TYPES:
731 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
736 /* "action" upcalls need a closer look. */
737 if (!dpif_upcall->userdata) {
738 VLOG_WARN_RL(&rl, "action upcall missing cookie");
741 userdata_len = nl_attr_get_size(dpif_upcall->userdata);
742 if (userdata_len < sizeof cookie.type
743 || userdata_len > sizeof cookie) {
744 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
748 memset(&cookie, 0, sizeof cookie);
749 memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
750 if (userdata_len == sizeof cookie.sflow
751 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
753 } else if (userdata_len == sizeof cookie.slow_path
754 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
756 } else if (userdata_len == sizeof cookie.flow_sample
757 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
758 return FLOW_SAMPLE_UPCALL;
759 } else if (userdata_len == sizeof cookie.ipfix
760 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
763 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
764 " and size %"PRIuSIZE, cookie.type, userdata_len);
770 recv_upcalls(struct udpif *udpif)
775 uint32_t hash = udpif->secret;
776 struct handler *handler;
777 struct upcall *upcall;
778 size_t n_bytes, left;
782 upcall = xmalloc(sizeof *upcall);
783 ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
784 sizeof upcall->upcall_stub);
785 error = dpif_recv(udpif->dpif, &upcall->dpif_upcall,
786 &upcall->upcall_buf);
788 /* upcall_destroy() can only be called on successfully received
790 ofpbuf_uninit(&upcall->upcall_buf);
796 NL_ATTR_FOR_EACH (nla, left, upcall->dpif_upcall.key,
797 upcall->dpif_upcall.key_len) {
798 enum ovs_key_attr type = nl_attr_type(nla);
799 if (type == OVS_KEY_ATTR_IN_PORT
800 || type == OVS_KEY_ATTR_TCP
801 || type == OVS_KEY_ATTR_UDP) {
802 if (nl_attr_get_size(nla) == 4) {
803 hash = mhash_add(hash, nl_attr_get_u32(nla));
807 "Netlink attribute with incorrect size.");
811 hash = mhash_finish(hash, n_bytes);
813 handler = &udpif->handlers[hash % udpif->n_handlers];
815 ovs_mutex_lock(&handler->mutex);
816 if (handler->n_upcalls < MAX_QUEUE_LENGTH) {
817 list_push_back(&handler->upcalls, &upcall->list_node);
818 if (handler->n_upcalls == 0) {
819 handler->need_signal = true;
821 handler->n_upcalls++;
822 if (handler->need_signal &&
823 handler->n_upcalls >= FLOW_MISS_MAX_BATCH) {
824 handler->need_signal = false;
825 xpthread_cond_signal(&handler->wake_cond);
827 ovs_mutex_unlock(&handler->mutex);
828 if (!VLOG_DROP_DBG(&rl)) {
829 struct ds ds = DS_EMPTY_INITIALIZER;
831 odp_flow_key_format(upcall->dpif_upcall.key,
832 upcall->dpif_upcall.key_len,
834 VLOG_DBG("dispatcher: enqueue (%s)", ds_cstr(&ds));
838 ovs_mutex_unlock(&handler->mutex);
839 COVERAGE_INC(upcall_queue_overflow);
840 upcall_destroy(upcall);
844 for (n = 0; n < udpif->n_handlers; ++n) {
845 struct handler *handler = &udpif->handlers[n];
847 if (handler->need_signal) {
848 handler->need_signal = false;
849 ovs_mutex_lock(&handler->mutex);
850 xpthread_cond_signal(&handler->wake_cond);
851 ovs_mutex_unlock(&handler->mutex);
856 /* Calculates slow path actions for 'xout'. 'buf' must statically be
857 * initialized with at least 128 bytes of space. */
859 compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
860 odp_port_t odp_in_port, struct ofpbuf *buf)
862 union user_action_cookie cookie;
866 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
867 cookie.slow_path.unused = 0;
868 cookie.slow_path.reason = xout->slow;
870 port = xout->slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)
873 pid = dpif_port_get_pid(udpif->dpif, port);
874 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, buf);
877 static struct flow_miss *
878 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
879 const struct flow *flow, uint32_t hash)
881 struct flow_miss *miss;
883 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
884 if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
893 handle_upcalls(struct handler *handler, struct list *upcalls)
895 struct hmap misses = HMAP_INITIALIZER(&misses);
896 struct udpif *udpif = handler->udpif;
898 struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH];
899 struct dpif_op *opsp[FLOW_MISS_MAX_BATCH * 2];
900 struct dpif_op ops[FLOW_MISS_MAX_BATCH * 2];
901 struct flow_miss *miss, *next_miss;
902 struct upcall *upcall, *next;
903 size_t n_misses, n_ops, i;
904 unsigned int flow_limit;
905 bool fail_open, may_put;
906 enum upcall_type type;
908 atomic_read(&udpif->flow_limit, &flow_limit);
909 may_put = udpif_get_n_flows(udpif) < flow_limit;
911 /* Extract the flow from each upcall. Construct in 'misses' a hash table
912 * that maps each unique flow to a 'struct flow_miss'.
914 * Most commonly there is a single packet per flow_miss, but there are
915 * several reasons why there might be more than one, e.g.:
917 * - The dpif packet interface does not support TSO (or UFO, etc.), so a
918 * large packet sent to userspace is split into a sequence of smaller
921 * - A stream of quickly arriving packets in an established "slow-pathed"
924 * - Rarely, a stream of quickly arriving packets in a flow not yet
925 * established. (This is rare because most protocols do not send
926 * multiple back-to-back packets before receiving a reply from the
927 * other end of the connection, which gives OVS a chance to set up a
931 LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
932 struct dpif_upcall *dupcall = &upcall->dpif_upcall;
933 struct flow_miss *miss = &miss_buf[n_misses];
934 struct ofpbuf *packet = &dupcall->packet;
935 struct flow_miss *existing_miss;
936 struct ofproto_dpif *ofproto;
937 struct dpif_sflow *sflow;
938 struct dpif_ipfix *ipfix;
939 odp_port_t odp_in_port;
943 error = xlate_receive(udpif->backer, packet, dupcall->key,
944 dupcall->key_len, &flow, &miss->key_fitness,
945 &ofproto, &ipfix, &sflow, NULL, &odp_in_port);
947 if (error == ENODEV) {
948 /* Received packet on datapath port for which we couldn't
949 * associate an ofproto. This can happen if a port is removed
950 * while traffic is being received. Print a rate-limited
951 * message in case it happens frequently. Install a drop flow
952 * so that future packets of the flow are inexpensively dropped
954 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
955 "port %"PRIu32, odp_in_port);
956 dpif_flow_put(udpif->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
957 dupcall->key, dupcall->key_len, NULL, 0, NULL, 0,
960 list_remove(&upcall->list_node);
961 upcall_destroy(upcall);
965 type = classify_upcall(upcall);
966 if (type == MISS_UPCALL) {
969 flow_extract(packet, flow.skb_priority, flow.pkt_mark,
970 &flow.tunnel, &flow.in_port, &miss->flow);
972 hash = flow_hash(&miss->flow, 0);
973 existing_miss = flow_miss_find(&misses, ofproto, &miss->flow,
975 if (!existing_miss) {
976 hmap_insert(&misses, &miss->hmap_node, hash);
977 miss->ofproto = ofproto;
978 miss->key = dupcall->key;
979 miss->key_len = dupcall->key_len;
980 miss->upcall_type = dupcall->type;
981 miss->stats.n_packets = 0;
982 miss->stats.n_bytes = 0;
983 miss->stats.used = time_msec();
984 miss->stats.tcp_flags = 0;
985 miss->odp_in_port = odp_in_port;
990 miss = existing_miss;
992 miss->stats.tcp_flags |= packet_get_tcp_flags(packet, &miss->flow);
993 miss->stats.n_bytes += packet->size;
994 miss->stats.n_packets++;
996 upcall->flow_miss = miss;
1003 union user_action_cookie cookie;
1005 memset(&cookie, 0, sizeof cookie);
1006 memcpy(&cookie, nl_attr_get(dupcall->userdata),
1007 sizeof cookie.sflow);
1008 dpif_sflow_received(sflow, packet, &flow, odp_in_port,
1014 dpif_ipfix_bridge_sample(ipfix, packet, &flow);
1017 case FLOW_SAMPLE_UPCALL:
1019 union user_action_cookie cookie;
1021 memset(&cookie, 0, sizeof cookie);
1022 memcpy(&cookie, nl_attr_get(dupcall->userdata),
1023 sizeof cookie.flow_sample);
1025 /* The flow reflects exactly the contents of the packet.
1026 * Sample the packet using it. */
1027 dpif_ipfix_flow_sample(ipfix, packet, &flow,
1028 cookie.flow_sample.collector_set_id,
1029 cookie.flow_sample.probability,
1030 cookie.flow_sample.obs_domain_id,
1031 cookie.flow_sample.obs_point_id);
1040 dpif_ipfix_unref(ipfix);
1041 dpif_sflow_unref(sflow);
1043 list_remove(&upcall->list_node);
1044 upcall_destroy(upcall);
1047 /* Initialize each 'struct flow_miss's ->xout.
1049 * We do this per-flow_miss rather than per-packet because, most commonly,
1050 * all the packets in a flow can use the same translation.
1052 * We can't do this in the previous loop because we need the TCP flags for
1053 * all the packets in each miss. */
1055 HMAP_FOR_EACH (miss, hmap_node, &misses) {
1056 struct xlate_in xin;
1058 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL,
1059 miss->stats.tcp_flags, NULL);
1060 xin.may_learn = true;
1062 if (miss->upcall_type == DPIF_UC_MISS) {
1063 xin.resubmit_stats = &miss->stats;
1065 /* For non-miss upcalls, there's a flow in the datapath which this
1066 * packet was accounted to. Presumably the revalidators will deal
1067 * with pushing its stats eventually. */
1070 xlate_actions(&xin, &miss->xout);
1071 fail_open = fail_open || miss->xout.fail_open;
1074 /* Now handle the packets individually in order of arrival. In the common
1075 * case each packet of a miss can share the same actions, but slow-pathed
1076 * packets need to be translated individually:
1078 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
1079 * processes received packets for these protocols.
1081 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1084 * The loop fills 'ops' with an array of operations to execute in the
1087 LIST_FOR_EACH (upcall, list_node, upcalls) {
1088 struct flow_miss *miss = upcall->flow_miss;
1089 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
1091 ovs_be16 flow_vlan_tci;
1093 /* Save a copy of flow.vlan_tci in case it is changed to
1094 * generate proper mega flow masks for VLAN splinter flows. */
1095 flow_vlan_tci = miss->flow.vlan_tci;
1097 if (miss->xout.slow) {
1098 struct xlate_in xin;
1100 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL, 0, packet);
1101 xlate_actions_for_side_effects(&xin);
1104 if (miss->flow.in_port.ofp_port
1105 != vsp_realdev_to_vlandev(miss->ofproto,
1106 miss->flow.in_port.ofp_port,
1107 miss->flow.vlan_tci)) {
1108 /* This packet was received on a VLAN splinter port. We
1109 * added a VLAN to the packet to make the packet resemble
1110 * the flow, but the actions were composed assuming that
1111 * the packet contained no VLAN. So, we must remove the
1112 * VLAN header from the packet before trying to execute the
1114 if (miss->xout.odp_actions.size) {
1115 eth_pop_vlan(packet);
1118 /* Remove the flow vlan tags inserted by vlan splinter logic
1119 * to ensure megaflow masks generated match the data path flow. */
1120 miss->flow.vlan_tci = 0;
1123 /* Do not install a flow into the datapath if:
1125 * - The datapath already has too many flows.
1127 * - An earlier iteration of this loop already put the same flow.
1129 * - We received this packet via some flow installed in the kernel
1133 && upcall->dpif_upcall.type == DPIF_UC_MISS) {
1139 atomic_read(&enable_megaflows, &megaflow);
1140 ofpbuf_use_stack(&mask, &miss->mask_buf, sizeof miss->mask_buf);
1142 odp_flow_key_from_mask(&mask, &miss->xout.wc.masks,
1143 &miss->flow, UINT32_MAX);
1147 op->type = DPIF_OP_FLOW_PUT;
1148 op->u.flow_put.flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1149 op->u.flow_put.key = miss->key;
1150 op->u.flow_put.key_len = miss->key_len;
1151 op->u.flow_put.mask = mask.data;
1152 op->u.flow_put.mask_len = mask.size;
1153 op->u.flow_put.stats = NULL;
1155 if (!miss->xout.slow) {
1156 op->u.flow_put.actions = miss->xout.odp_actions.data;
1157 op->u.flow_put.actions_len = miss->xout.odp_actions.size;
1161 ofpbuf_use_stack(&buf, miss->slow_path_buf,
1162 sizeof miss->slow_path_buf);
1163 compose_slow_path(udpif, &miss->xout, miss->odp_in_port, &buf);
1164 op->u.flow_put.actions = buf.data;
1165 op->u.flow_put.actions_len = buf.size;
1170 * The 'miss' may be shared by multiple upcalls. Restore
1171 * the saved flow vlan_tci field before processing the next
1173 miss->flow.vlan_tci = flow_vlan_tci;
1175 if (miss->xout.odp_actions.size) {
1178 op->type = DPIF_OP_EXECUTE;
1179 op->u.execute.packet = packet;
1180 odp_key_to_pkt_metadata(miss->key, miss->key_len,
1182 op->u.execute.actions = miss->xout.odp_actions.data;
1183 op->u.execute.actions_len = miss->xout.odp_actions.size;
1184 op->u.execute.needs_help = (miss->xout.slow & SLOW_ACTION) != 0;
1188 /* Special case for fail-open mode.
1190 * If we are in fail-open mode, but we are connected to a controller too,
1191 * then we should send the packet up to the controller in the hope that it
1192 * will try to set up a flow and thereby allow us to exit fail-open.
1194 * See the top-level comment in fail-open.c for more information.
1196 * Copy packets before they are modified by execution. */
1198 LIST_FOR_EACH (upcall, list_node, upcalls) {
1199 struct flow_miss *miss = upcall->flow_miss;
1200 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
1201 struct ofproto_packet_in *pin;
1203 pin = xmalloc(sizeof *pin);
1204 pin->up.packet = xmemdup(packet->data, packet->size);
1205 pin->up.packet_len = packet->size;
1206 pin->up.reason = OFPR_NO_MATCH;
1207 pin->up.table_id = 0;
1208 pin->up.cookie = OVS_BE64_MAX;
1209 flow_get_metadata(&miss->flow, &pin->up.fmd);
1210 pin->send_len = 0; /* Not used for flow table misses. */
1211 pin->generated_by_table_miss = false;
1212 ofproto_dpif_send_packet_in(miss->ofproto, pin);
1216 /* Execute batch. */
1217 for (i = 0; i < n_ops; i++) {
1220 dpif_operate(udpif->dpif, opsp, n_ops);
1222 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &misses) {
1223 hmap_remove(&misses, &miss->hmap_node);
1224 xlate_out_uninit(&miss->xout);
1226 hmap_destroy(&misses);
1228 LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
1229 list_remove(&upcall->list_node);
1230 upcall_destroy(upcall);
1234 static struct udpif_key *
1235 ukey_lookup(struct revalidator *revalidator, struct udpif_flow_dump *udump)
1237 struct udpif_key *ukey;
1239 HMAP_FOR_EACH_WITH_HASH (ukey, hmap_node, udump->key_hash,
1240 &revalidator->ukeys) {
1241 if (ukey->key_len == udump->key_len
1242 && !memcmp(ukey->key, udump->key, udump->key_len)) {
1250 ukey_delete(struct revalidator *revalidator, struct udpif_key *ukey)
1252 hmap_remove(&revalidator->ukeys, &ukey->hmap_node);
1257 revalidate_ukey(struct udpif *udpif, struct udpif_flow_dump *udump,
1258 struct udpif_key *ukey)
1260 struct ofpbuf xout_actions, *actions;
1261 uint64_t slow_path_buf[128 / 8];
1262 struct xlate_out xout, *xoutp;
1263 struct flow flow, udump_mask;
1264 struct ofproto_dpif *ofproto;
1265 struct dpif_flow_stats push;
1266 uint32_t *udump32, *xout32;
1267 odp_port_t odp_in_port;
1268 struct xlate_in xin;
1277 /* If we don't need to revalidate, we can simply push the stats contained
1278 * in the udump, otherwise we'll have to get the actions so we can check
1280 if (udump->need_revalidate) {
1281 if (dpif_flow_get(udpif->dpif, ukey->key, ukey->key_len, &actions,
1287 push.used = udump->stats.used;
1288 push.tcp_flags = udump->stats.tcp_flags;
1289 push.n_packets = udump->stats.n_packets > ukey->stats.n_packets
1290 ? udump->stats.n_packets - ukey->stats.n_packets
1292 push.n_bytes = udump->stats.n_bytes > ukey->stats.n_bytes
1293 ? udump->stats.n_bytes - ukey->stats.n_bytes
1295 ukey->stats = udump->stats;
1297 if (!push.n_packets && !udump->need_revalidate) {
1302 error = xlate_receive(udpif->backer, NULL, ukey->key, ukey->key_len, &flow,
1303 NULL, &ofproto, NULL, NULL, NULL, &odp_in_port);
1308 xlate_in_init(&xin, ofproto, &flow, NULL, push.tcp_flags, NULL);
1309 xin.resubmit_stats = push.n_packets ? &push : NULL;
1310 xin.may_learn = push.n_packets > 0;
1311 xin.skip_wildcards = !udump->need_revalidate;
1312 xlate_actions(&xin, &xout);
1315 if (!udump->need_revalidate) {
1321 ofpbuf_use_const(&xout_actions, xout.odp_actions.data,
1322 xout.odp_actions.size);
1324 ofpbuf_use_stack(&xout_actions, slow_path_buf, sizeof slow_path_buf);
1325 compose_slow_path(udpif, &xout, odp_in_port, &xout_actions);
1328 if (!ofpbuf_equal(&xout_actions, actions)) {
1332 if (odp_flow_key_to_mask(udump->mask, udump->mask_len, &udump_mask, &flow)
1337 /* Since the kernel is free to ignore wildcarded bits in the mask, we can't
1338 * directly check that the masks are the same. Instead we check that the
1339 * mask in the kernel is more specific i.e. less wildcarded, than what
1340 * we've calculated here. This guarantees we don't catch any packets we
1341 * shouldn't with the megaflow. */
1342 udump32 = (uint32_t *) &udump_mask;
1343 xout32 = (uint32_t *) &xout.wc.masks;
1344 for (i = 0; i < FLOW_U32S; i++) {
1345 if ((udump32[i] | xout32[i]) != udump32[i]) {
1352 ofpbuf_delete(actions);
1353 xlate_out_uninit(xoutp);
1358 revalidate_udumps(struct revalidator *revalidator, struct list *udumps)
1360 struct udpif *udpif = revalidator->udpif;
1363 struct dpif_flow_stats ukey_stats; /* Stats stored in the ukey. */
1364 struct dpif_flow_stats stats; /* Stats for 'op'. */
1365 struct dpif_op op; /* Flow del operation. */
1366 } ops[REVALIDATE_MAX_BATCH];
1368 struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
1369 struct udpif_flow_dump *udump, *next_udump;
1370 size_t n_ops, i, n_flows;
1371 unsigned int flow_limit;
1372 long long int max_idle;
1375 atomic_read(&udpif->flow_limit, &flow_limit);
1377 n_flows = udpif_get_n_flows(udpif);
1380 max_idle = MAX_IDLE;
1381 if (n_flows > flow_limit) {
1382 must_del = n_flows > 2 * flow_limit;
1387 LIST_FOR_EACH_SAFE (udump, next_udump, list_node, udumps) {
1388 long long int used, now;
1389 struct udpif_key *ukey;
1392 ukey = ukey_lookup(revalidator, udump);
1394 used = udump->stats.used;
1395 if (!used && ukey) {
1396 used = ukey->created;
1399 if (must_del || (used && used < now - max_idle)) {
1400 struct dpif_flow_stats *ukey_stats = &ops[n_ops].ukey_stats;
1401 struct dpif_op *op = &ops[n_ops].op;
1403 op->type = DPIF_OP_FLOW_DEL;
1404 op->u.flow_del.key = udump->key;
1405 op->u.flow_del.key_len = udump->key_len;
1406 op->u.flow_del.stats = &ops[n_ops].stats;
1410 *ukey_stats = ukey->stats;
1411 ukey_delete(revalidator, ukey);
1413 memset(ukey_stats, 0, sizeof *ukey_stats);
1420 ukey = xmalloc(sizeof *ukey);
1422 ukey->key = (struct nlattr *) &ukey->key_buf;
1423 memcpy(ukey->key, udump->key, udump->key_len);
1424 ukey->key_len = udump->key_len;
1426 ukey->created = used ? used : now;
1427 memset(&ukey->stats, 0, sizeof ukey->stats);
1431 hmap_insert(&revalidator->ukeys, &ukey->hmap_node,
1436 if (!revalidate_ukey(udpif, udump, ukey)) {
1437 dpif_flow_del(udpif->dpif, udump->key, udump->key_len, NULL);
1438 ukey_delete(revalidator, ukey);
1441 list_remove(&udump->list_node);
1445 for (i = 0; i < n_ops; i++) {
1446 opsp[i] = &ops[i].op;
1448 dpif_operate(udpif->dpif, opsp, n_ops);
1450 for (i = 0; i < n_ops; i++) {
1451 struct dpif_flow_stats push, *stats, *ukey_stats;
1453 ukey_stats = &ops[i].ukey_stats;
1454 stats = ops[i].op.u.flow_del.stats;
1455 push.used = MAX(stats->used, ukey_stats->used);
1456 push.tcp_flags = stats->tcp_flags | ukey_stats->tcp_flags;
1457 push.n_packets = stats->n_packets - ukey_stats->n_packets;
1458 push.n_bytes = stats->n_bytes - ukey_stats->n_bytes;
1460 if (push.n_packets || netflow_exists()) {
1461 struct ofproto_dpif *ofproto;
1462 struct netflow *netflow;
1465 if (!xlate_receive(udpif->backer, NULL, ops[i].op.u.flow_del.key,
1466 ops[i].op.u.flow_del.key_len, &flow, NULL,
1467 &ofproto, NULL, NULL, &netflow, NULL)) {
1468 struct xlate_in xin;
1470 xlate_in_init(&xin, ofproto, &flow, NULL, push.tcp_flags,
1472 xin.resubmit_stats = push.n_packets ? &push : NULL;
1473 xin.may_learn = push.n_packets > 0;
1474 xin.skip_wildcards = true;
1475 xlate_actions_for_side_effects(&xin);
1478 netflow_expire(netflow, &flow);
1479 netflow_flow_clear(netflow, &flow);
1480 netflow_unref(netflow);
1486 LIST_FOR_EACH_SAFE (udump, next_udump, list_node, udumps) {
1487 list_remove(&udump->list_node);
1493 revalidator_sweep(struct revalidator *revalidator)
1495 struct udpif_key *ukey, *next;
1497 HMAP_FOR_EACH_SAFE (ukey, next, hmap_node, &revalidator->ukeys) {
1501 ukey_delete(revalidator, ukey);
1507 upcall_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1508 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
1510 struct ds ds = DS_EMPTY_INITIALIZER;
1511 struct udpif *udpif;
1513 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1514 unsigned int flow_limit;
1517 atomic_read(&udpif->flow_limit, &flow_limit);
1519 ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
1520 ds_put_format(&ds, "\tflows : (current %"PRIu64")"
1521 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
1522 udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
1523 ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
1525 ds_put_char(&ds, '\n');
1526 for (i = 0; i < udpif->n_handlers; i++) {
1527 struct handler *handler = &udpif->handlers[i];
1529 ovs_mutex_lock(&handler->mutex);
1530 ds_put_format(&ds, "\t%s: (upcall queue %"PRIuSIZE")\n",
1531 handler->name, handler->n_upcalls);
1532 ovs_mutex_unlock(&handler->mutex);
1535 ds_put_char(&ds, '\n');
1536 for (i = 0; i < n_revalidators; i++) {
1537 struct revalidator *revalidator = &udpif->revalidators[i];
1539 /* XXX: The result of hmap_count(&revalidator->ukeys) may not be
1540 * accurate because it's not protected by the revalidator mutex. */
1541 ovs_mutex_lock(&revalidator->mutex);
1542 ds_put_format(&ds, "\t%s: (dump queue %"PRIuSIZE") (keys %"PRIuSIZE
1543 ")\n", revalidator->name, revalidator->n_udumps,
1544 hmap_count(&revalidator->ukeys));
1545 ovs_mutex_unlock(&revalidator->mutex);
1549 unixctl_command_reply(conn, ds_cstr(&ds));
1553 /* Disable using the megaflows.
1555 * This command is only needed for advanced debugging, so it's not
1556 * documented in the man page. */
1558 upcall_unixctl_disable_megaflows(struct unixctl_conn *conn,
1559 int argc OVS_UNUSED,
1560 const char *argv[] OVS_UNUSED,
1561 void *aux OVS_UNUSED)
1563 atomic_store(&enable_megaflows, false);
1565 unixctl_command_reply(conn, "megaflows disabled");
1568 /* Re-enable using megaflows.
1570 * This command is only needed for advanced debugging, so it's not
1571 * documented in the man page. */
1573 upcall_unixctl_enable_megaflows(struct unixctl_conn *conn,
1574 int argc OVS_UNUSED,
1575 const char *argv[] OVS_UNUSED,
1576 void *aux OVS_UNUSED)
1578 atomic_store(&enable_megaflows, true);
1580 unixctl_command_reply(conn, "megaflows enabled");