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 const struct nlattr *key;
207 enum dpif_upcall_type upcall_type;
208 struct dpif_flow_stats stats;
209 odp_port_t odp_in_port;
211 uint64_t slow_path_buf[128 / 8];
212 struct odputil_keybuf mask_buf;
214 struct xlate_out xout;
219 static void upcall_destroy(struct upcall *);
221 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
222 static struct list all_udpifs = LIST_INITIALIZER(&all_udpifs);
224 static void recv_upcalls(struct udpif *);
225 static void handle_upcalls(struct handler *handler, struct list *upcalls);
226 static void *udpif_flow_dumper(void *);
227 static void *udpif_dispatcher(void *);
228 static void *udpif_upcall_handler(void *);
229 static void *udpif_revalidator(void *);
230 static uint64_t udpif_get_n_flows(struct udpif *);
231 static void revalidate_udumps(struct revalidator *, struct list *udumps);
232 static void revalidator_sweep(struct revalidator *);
233 static void revalidator_purge(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 upcall_unixctl_set_flow_limit(struct unixctl_conn *conn, int argc,
241 const char *argv[], void *aux);
242 static void ukey_delete(struct revalidator *, struct udpif_key *);
244 static atomic_bool enable_megaflows = ATOMIC_VAR_INIT(true);
247 udpif_create(struct dpif_backer *backer, struct dpif *dpif)
249 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
250 struct udpif *udpif = xzalloc(sizeof *udpif);
252 if (ovsthread_once_start(&once)) {
253 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show,
255 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
256 upcall_unixctl_disable_megaflows, NULL);
257 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
258 upcall_unixctl_enable_megaflows, NULL);
259 unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
260 upcall_unixctl_set_flow_limit, NULL);
261 ovsthread_once_done(&once);
265 udpif->backer = backer;
266 atomic_init(&udpif->flow_limit, MIN(ofproto_flow_limit, 10000));
267 udpif->secret = random_uint32();
268 udpif->reval_seq = seq_create();
269 udpif->dump_seq = seq_create();
270 latch_init(&udpif->exit_latch);
271 list_push_back(&all_udpifs, &udpif->list_node);
272 atomic_init(&udpif->n_flows, 0);
273 atomic_init(&udpif->n_flows_timestamp, LLONG_MIN);
274 ovs_mutex_init(&udpif->n_flows_mutex);
280 udpif_destroy(struct udpif *udpif)
282 udpif_set_threads(udpif, 0, 0);
285 list_remove(&udpif->list_node);
286 latch_destroy(&udpif->exit_latch);
287 seq_destroy(udpif->reval_seq);
288 seq_destroy(udpif->dump_seq);
289 atomic_destroy(&udpif->flow_limit);
290 atomic_destroy(&udpif->n_flows);
291 atomic_destroy(&udpif->n_flows_timestamp);
292 ovs_mutex_destroy(&udpif->n_flows_mutex);
296 /* Tells 'udpif' how many threads it should use to handle upcalls. Disables
297 * all threads if 'n_handlers' and 'n_revalidators' is zero. 'udpif''s
298 * datapath handle must have packet reception enabled before starting threads.
301 udpif_set_threads(struct udpif *udpif, size_t n_handlers,
302 size_t n_revalidators)
304 /* Stop the old threads (if any). */
305 if (udpif->handlers &&
306 (udpif->n_handlers != n_handlers
307 || udpif->n_revalidators != n_revalidators)) {
310 latch_set(&udpif->exit_latch);
312 for (i = 0; i < udpif->n_handlers; i++) {
313 struct handler *handler = &udpif->handlers[i];
315 ovs_mutex_lock(&handler->mutex);
316 xpthread_cond_signal(&handler->wake_cond);
317 ovs_mutex_unlock(&handler->mutex);
318 xpthread_join(handler->thread, NULL);
321 for (i = 0; i < udpif->n_revalidators; i++) {
322 struct revalidator *revalidator = &udpif->revalidators[i];
324 ovs_mutex_lock(&revalidator->mutex);
325 xpthread_cond_signal(&revalidator->wake_cond);
326 ovs_mutex_unlock(&revalidator->mutex);
327 xpthread_join(revalidator->thread, NULL);
330 xpthread_join(udpif->flow_dumper, NULL);
331 xpthread_join(udpif->dispatcher, NULL);
333 for (i = 0; i < udpif->n_revalidators; i++) {
334 struct revalidator *revalidator = &udpif->revalidators[i];
335 struct udpif_flow_dump *udump, *next_udump;
337 LIST_FOR_EACH_SAFE (udump, next_udump, list_node,
338 &revalidator->udumps) {
339 list_remove(&udump->list_node);
343 /* Delete ukeys, and delete all flows from the datapath to prevent
344 * double-counting stats. */
345 revalidator_purge(revalidator);
346 hmap_destroy(&revalidator->ukeys);
347 ovs_mutex_destroy(&revalidator->mutex);
349 free(revalidator->name);
352 for (i = 0; i < udpif->n_handlers; i++) {
353 struct handler *handler = &udpif->handlers[i];
354 struct upcall *miss, *next;
356 LIST_FOR_EACH_SAFE (miss, next, list_node, &handler->upcalls) {
357 list_remove(&miss->list_node);
358 upcall_destroy(miss);
360 ovs_mutex_destroy(&handler->mutex);
362 xpthread_cond_destroy(&handler->wake_cond);
365 latch_poll(&udpif->exit_latch);
367 free(udpif->revalidators);
368 udpif->revalidators = NULL;
369 udpif->n_revalidators = 0;
371 free(udpif->handlers);
372 udpif->handlers = NULL;
373 udpif->n_handlers = 0;
376 /* Start new threads (if necessary). */
377 if (!udpif->handlers && n_handlers) {
380 udpif->n_handlers = n_handlers;
381 udpif->n_revalidators = n_revalidators;
383 udpif->handlers = xzalloc(udpif->n_handlers * sizeof *udpif->handlers);
384 for (i = 0; i < udpif->n_handlers; i++) {
385 struct handler *handler = &udpif->handlers[i];
387 handler->udpif = udpif;
388 list_init(&handler->upcalls);
389 handler->need_signal = false;
390 xpthread_cond_init(&handler->wake_cond, NULL);
391 ovs_mutex_init(&handler->mutex);
392 xpthread_create(&handler->thread, NULL, udpif_upcall_handler,
396 udpif->revalidators = xzalloc(udpif->n_revalidators
397 * sizeof *udpif->revalidators);
398 for (i = 0; i < udpif->n_revalidators; i++) {
399 struct revalidator *revalidator = &udpif->revalidators[i];
401 revalidator->udpif = udpif;
402 list_init(&revalidator->udumps);
403 hmap_init(&revalidator->ukeys);
404 ovs_mutex_init(&revalidator->mutex);
405 xpthread_cond_init(&revalidator->wake_cond, NULL);
406 xpthread_create(&revalidator->thread, NULL, udpif_revalidator,
409 xpthread_create(&udpif->dispatcher, NULL, udpif_dispatcher, udpif);
410 xpthread_create(&udpif->flow_dumper, NULL, udpif_flow_dumper, udpif);
414 /* Waits for all ongoing upcall translations to complete. This ensures that
415 * there are no transient references to any removed ofprotos (or other
416 * objects). In particular, this should be called after an ofproto is removed
417 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
419 udpif_synchronize(struct udpif *udpif)
421 /* This is stronger than necessary. It would be sufficient to ensure
422 * (somehow) that each handler and revalidator thread had passed through
423 * its main loop once. */
424 size_t n_handlers = udpif->n_handlers;
425 size_t n_revalidators = udpif->n_revalidators;
426 udpif_set_threads(udpif, 0, 0);
427 udpif_set_threads(udpif, n_handlers, n_revalidators);
430 /* Notifies 'udpif' that something changed which may render previous
431 * xlate_actions() results invalid. */
433 udpif_revalidate(struct udpif *udpif)
435 seq_change(udpif->reval_seq);
438 /* Returns a seq which increments every time 'udpif' pulls stats from the
439 * datapath. Callers can use this to get a sense of when might be a good time
440 * to do periodic work which relies on relatively up to date statistics. */
442 udpif_dump_seq(struct udpif *udpif)
444 return udpif->dump_seq;
448 udpif_get_memory_usage(struct udpif *udpif, struct simap *usage)
452 simap_increase(usage, "dispatchers", 1);
453 simap_increase(usage, "flow_dumpers", 1);
455 simap_increase(usage, "handlers", udpif->n_handlers);
456 for (i = 0; i < udpif->n_handlers; i++) {
457 struct handler *handler = &udpif->handlers[i];
458 ovs_mutex_lock(&handler->mutex);
459 simap_increase(usage, "handler upcalls", handler->n_upcalls);
460 ovs_mutex_unlock(&handler->mutex);
463 simap_increase(usage, "revalidators", udpif->n_revalidators);
464 for (i = 0; i < udpif->n_revalidators; i++) {
465 struct revalidator *revalidator = &udpif->revalidators[i];
466 ovs_mutex_lock(&revalidator->mutex);
467 simap_increase(usage, "revalidator dumps", revalidator->n_udumps);
469 /* XXX: This isn't technically thread safe because the revalidator
470 * ukeys maps isn't protected by a mutex since it's per thread. */
471 simap_increase(usage, "revalidator keys",
472 hmap_count(&revalidator->ukeys));
473 ovs_mutex_unlock(&revalidator->mutex);
477 /* Removes all flows from all datapaths. */
483 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
484 dpif_flow_flush(udpif->dpif);
488 /* Destroys and deallocates 'upcall'. */
490 upcall_destroy(struct upcall *upcall)
493 ofpbuf_uninit(&upcall->dpif_upcall.packet);
494 ofpbuf_uninit(&upcall->upcall_buf);
500 udpif_get_n_flows(struct udpif *udpif)
502 long long int time, now;
506 atomic_read(&udpif->n_flows_timestamp, &time);
507 if (time < now - 100 && !ovs_mutex_trylock(&udpif->n_flows_mutex)) {
508 struct dpif_dp_stats stats;
510 atomic_store(&udpif->n_flows_timestamp, now);
511 dpif_get_dp_stats(udpif->dpif, &stats);
512 flow_count = stats.n_flows;
513 atomic_store(&udpif->n_flows, flow_count);
514 ovs_mutex_unlock(&udpif->n_flows_mutex);
516 atomic_read(&udpif->n_flows, &flow_count);
521 /* The dispatcher thread is responsible for receiving upcalls from the kernel,
522 * assigning them to a upcall_handler thread. */
524 udpif_dispatcher(void *arg)
526 struct udpif *udpif = arg;
528 set_subprogram_name("dispatcher");
529 while (!latch_is_set(&udpif->exit_latch)) {
531 dpif_recv_wait(udpif->dpif);
532 latch_wait(&udpif->exit_latch);
540 udpif_flow_dumper(void *arg)
542 struct udpif *udpif = arg;
544 set_subprogram_name("flow_dumper");
545 while (!latch_is_set(&udpif->exit_latch)) {
546 const struct dpif_flow_stats *stats;
547 long long int start_time, duration;
548 const struct nlattr *key, *mask;
549 struct dpif_flow_dump dump;
550 size_t key_len, mask_len;
551 unsigned int flow_limit;
552 bool need_revalidate;
557 reval_seq = seq_read(udpif->reval_seq);
558 need_revalidate = udpif->last_reval_seq != reval_seq;
559 udpif->last_reval_seq = reval_seq;
561 n_flows = udpif_get_n_flows(udpif);
562 udpif->max_n_flows = MAX(n_flows, udpif->max_n_flows);
563 udpif->avg_n_flows = (udpif->avg_n_flows + n_flows) / 2;
565 start_time = time_msec();
566 dpif_flow_dump_start(&dump, udpif->dpif);
567 dpif_flow_dump_state_init(udpif->dpif, &state);
568 while (dpif_flow_dump_next(&dump, state, &key, &key_len,
569 &mask, &mask_len, NULL, NULL, &stats)
570 && !latch_is_set(&udpif->exit_latch)) {
571 struct udpif_flow_dump *udump = xmalloc(sizeof *udump);
572 struct revalidator *revalidator;
574 udump->key_hash = hash_bytes(key, key_len, udpif->secret);
575 memcpy(&udump->key_buf, key, key_len);
576 udump->key = (struct nlattr *) &udump->key_buf;
577 udump->key_len = key_len;
579 memcpy(&udump->mask_buf, mask, mask_len);
580 udump->mask = (struct nlattr *) &udump->mask_buf;
581 udump->mask_len = mask_len;
583 udump->stats = *stats;
584 udump->need_revalidate = need_revalidate;
586 revalidator = &udpif->revalidators[udump->key_hash
587 % udpif->n_revalidators];
589 ovs_mutex_lock(&revalidator->mutex);
590 while (revalidator->n_udumps >= REVALIDATE_MAX_BATCH * 3
591 && !latch_is_set(&udpif->exit_latch)) {
592 ovs_mutex_cond_wait(&revalidator->wake_cond,
593 &revalidator->mutex);
595 list_push_back(&revalidator->udumps, &udump->list_node);
596 revalidator->n_udumps++;
597 xpthread_cond_signal(&revalidator->wake_cond);
598 ovs_mutex_unlock(&revalidator->mutex);
600 dpif_flow_dump_state_uninit(udpif->dpif, state);
601 dpif_flow_dump_done(&dump);
603 /* Let all the revalidators finish and garbage collect. */
604 seq_change(udpif->dump_seq);
605 for (i = 0; i < udpif->n_revalidators; i++) {
606 struct revalidator *revalidator = &udpif->revalidators[i];
607 ovs_mutex_lock(&revalidator->mutex);
608 xpthread_cond_signal(&revalidator->wake_cond);
609 ovs_mutex_unlock(&revalidator->mutex);
612 for (i = 0; i < udpif->n_revalidators; i++) {
613 struct revalidator *revalidator = &udpif->revalidators[i];
615 ovs_mutex_lock(&revalidator->mutex);
616 while (revalidator->dump_seq != seq_read(udpif->dump_seq)
617 && !latch_is_set(&udpif->exit_latch)) {
618 ovs_mutex_cond_wait(&revalidator->wake_cond,
619 &revalidator->mutex);
621 ovs_mutex_unlock(&revalidator->mutex);
624 duration = MAX(time_msec() - start_time, 1);
625 udpif->dump_duration = duration;
626 atomic_read(&udpif->flow_limit, &flow_limit);
627 if (duration > 2000) {
628 flow_limit /= duration / 1000;
629 } else if (duration > 1300) {
630 flow_limit = flow_limit * 3 / 4;
631 } else if (duration < 1000 && n_flows > 2000
632 && flow_limit < n_flows * 1000 / duration) {
635 flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
636 atomic_store(&udpif->flow_limit, flow_limit);
638 if (duration > 2000) {
639 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
643 poll_timer_wait_until(start_time + MIN(MAX_IDLE, 500));
644 seq_wait(udpif->reval_seq, udpif->last_reval_seq);
645 latch_wait(&udpif->exit_latch);
652 /* The miss handler thread is responsible for processing miss upcalls retrieved
653 * by the dispatcher thread. Once finished it passes the processed miss
654 * upcalls to ofproto-dpif where they're installed in the datapath. */
656 udpif_upcall_handler(void *arg)
658 struct handler *handler = arg;
660 handler->name = xasprintf("handler_%u", ovsthread_id_self());
661 set_subprogram_name("%s", handler->name);
663 while (!latch_is_set(&handler->udpif->exit_latch)) {
664 struct list misses = LIST_INITIALIZER(&misses);
667 ovs_mutex_lock(&handler->mutex);
668 if (!handler->n_upcalls) {
669 ovs_mutex_cond_wait(&handler->wake_cond, &handler->mutex);
672 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
673 if (handler->n_upcalls) {
674 handler->n_upcalls--;
675 list_push_back(&misses, list_pop_front(&handler->upcalls));
680 ovs_mutex_unlock(&handler->mutex);
682 handle_upcalls(handler, &misses);
691 udpif_revalidator(void *arg)
693 struct revalidator *revalidator = arg;
695 revalidator->name = xasprintf("revalidator_%u", ovsthread_id_self());
696 set_subprogram_name("%s", revalidator->name);
698 struct list udumps = LIST_INITIALIZER(&udumps);
699 struct udpif *udpif = revalidator->udpif;
702 ovs_mutex_lock(&revalidator->mutex);
703 if (latch_is_set(&udpif->exit_latch)) {
704 ovs_mutex_unlock(&revalidator->mutex);
708 if (!revalidator->n_udumps) {
709 if (revalidator->dump_seq != seq_read(udpif->dump_seq)) {
710 revalidator->dump_seq = seq_read(udpif->dump_seq);
711 revalidator_sweep(revalidator);
713 ovs_mutex_cond_wait(&revalidator->wake_cond,
714 &revalidator->mutex);
718 for (i = 0; i < REVALIDATE_MAX_BATCH && revalidator->n_udumps; i++) {
719 list_push_back(&udumps, list_pop_front(&revalidator->udumps));
720 revalidator->n_udumps--;
723 /* Wake up the flow dumper. */
724 xpthread_cond_signal(&revalidator->wake_cond);
725 ovs_mutex_unlock(&revalidator->mutex);
727 if (!list_is_empty(&udumps)) {
728 revalidate_udumps(revalidator, &udumps);
735 static enum upcall_type
736 classify_upcall(const struct upcall *upcall)
738 const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
739 union user_action_cookie cookie;
742 /* First look at the upcall type. */
743 switch (dpif_upcall->type) {
750 case DPIF_N_UC_TYPES:
752 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
757 /* "action" upcalls need a closer look. */
758 if (!dpif_upcall->userdata) {
759 VLOG_WARN_RL(&rl, "action upcall missing cookie");
762 userdata_len = nl_attr_get_size(dpif_upcall->userdata);
763 if (userdata_len < sizeof cookie.type
764 || userdata_len > sizeof cookie) {
765 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
769 memset(&cookie, 0, sizeof cookie);
770 memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
771 if (userdata_len == MAX(8, sizeof cookie.sflow)
772 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
774 } else if (userdata_len == MAX(8, sizeof cookie.slow_path)
775 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
777 } else if (userdata_len == MAX(8, sizeof cookie.flow_sample)
778 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
779 return FLOW_SAMPLE_UPCALL;
780 } else if (userdata_len == MAX(8, sizeof cookie.ipfix)
781 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
784 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
785 " and size %"PRIuSIZE, cookie.type, userdata_len);
791 recv_upcalls(struct udpif *udpif)
796 uint32_t hash = udpif->secret;
797 struct handler *handler;
798 struct upcall *upcall;
799 size_t n_bytes, left;
803 upcall = xmalloc(sizeof *upcall);
804 ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
805 sizeof upcall->upcall_stub);
806 error = dpif_recv(udpif->dpif, &upcall->dpif_upcall,
807 &upcall->upcall_buf);
809 /* upcall_destroy() can only be called on successfully received
811 ofpbuf_uninit(&upcall->upcall_buf);
817 NL_ATTR_FOR_EACH (nla, left, upcall->dpif_upcall.key,
818 upcall->dpif_upcall.key_len) {
819 enum ovs_key_attr type = nl_attr_type(nla);
820 if (type == OVS_KEY_ATTR_IN_PORT
821 || type == OVS_KEY_ATTR_TCP
822 || type == OVS_KEY_ATTR_UDP) {
823 if (nl_attr_get_size(nla) == 4) {
824 hash = mhash_add(hash, nl_attr_get_u32(nla));
828 "Netlink attribute with incorrect size.");
832 hash = mhash_finish(hash, n_bytes);
834 handler = &udpif->handlers[hash % udpif->n_handlers];
836 ovs_mutex_lock(&handler->mutex);
837 if (handler->n_upcalls < MAX_QUEUE_LENGTH) {
838 list_push_back(&handler->upcalls, &upcall->list_node);
839 if (handler->n_upcalls == 0) {
840 handler->need_signal = true;
842 handler->n_upcalls++;
843 if (handler->need_signal &&
844 handler->n_upcalls >= FLOW_MISS_MAX_BATCH) {
845 handler->need_signal = false;
846 xpthread_cond_signal(&handler->wake_cond);
848 ovs_mutex_unlock(&handler->mutex);
849 if (!VLOG_DROP_DBG(&rl)) {
850 struct ds ds = DS_EMPTY_INITIALIZER;
852 odp_flow_key_format(upcall->dpif_upcall.key,
853 upcall->dpif_upcall.key_len,
855 VLOG_DBG("dispatcher: enqueue (%s)", ds_cstr(&ds));
859 ovs_mutex_unlock(&handler->mutex);
860 COVERAGE_INC(upcall_queue_overflow);
861 upcall_destroy(upcall);
865 for (n = 0; n < udpif->n_handlers; ++n) {
866 struct handler *handler = &udpif->handlers[n];
868 if (handler->need_signal) {
869 handler->need_signal = false;
870 ovs_mutex_lock(&handler->mutex);
871 xpthread_cond_signal(&handler->wake_cond);
872 ovs_mutex_unlock(&handler->mutex);
877 /* Calculates slow path actions for 'xout'. 'buf' must statically be
878 * initialized with at least 128 bytes of space. */
880 compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
881 odp_port_t odp_in_port, struct ofpbuf *buf)
883 union user_action_cookie cookie;
887 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
888 cookie.slow_path.unused = 0;
889 cookie.slow_path.reason = xout->slow;
891 port = xout->slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)
894 pid = dpif_port_get_pid(udpif->dpif, port);
895 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, buf);
898 static struct flow_miss *
899 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
900 const struct flow *flow, uint32_t hash)
902 struct flow_miss *miss;
904 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
905 if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
914 handle_upcalls(struct handler *handler, struct list *upcalls)
916 struct hmap misses = HMAP_INITIALIZER(&misses);
917 struct udpif *udpif = handler->udpif;
919 struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH];
920 struct dpif_op *opsp[FLOW_MISS_MAX_BATCH * 2];
921 struct dpif_op ops[FLOW_MISS_MAX_BATCH * 2];
922 struct flow_miss *miss, *next_miss;
923 struct upcall *upcall, *next;
924 size_t n_misses, n_ops, i;
925 unsigned int flow_limit;
926 bool fail_open, may_put;
927 enum upcall_type type;
929 atomic_read(&udpif->flow_limit, &flow_limit);
930 may_put = udpif_get_n_flows(udpif) < flow_limit;
932 /* Extract the flow from each upcall. Construct in 'misses' a hash table
933 * that maps each unique flow to a 'struct flow_miss'.
935 * Most commonly there is a single packet per flow_miss, but there are
936 * several reasons why there might be more than one, e.g.:
938 * - The dpif packet interface does not support TSO (or UFO, etc.), so a
939 * large packet sent to userspace is split into a sequence of smaller
942 * - A stream of quickly arriving packets in an established "slow-pathed"
945 * - Rarely, a stream of quickly arriving packets in a flow not yet
946 * established. (This is rare because most protocols do not send
947 * multiple back-to-back packets before receiving a reply from the
948 * other end of the connection, which gives OVS a chance to set up a
952 LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
953 struct dpif_upcall *dupcall = &upcall->dpif_upcall;
954 struct flow_miss *miss = &miss_buf[n_misses];
955 struct ofpbuf *packet = &dupcall->packet;
956 struct flow_miss *existing_miss;
957 struct ofproto_dpif *ofproto;
958 struct dpif_sflow *sflow;
959 struct dpif_ipfix *ipfix;
960 odp_port_t odp_in_port;
964 error = xlate_receive(udpif->backer, packet, dupcall->key,
965 dupcall->key_len, &flow,
966 &ofproto, &ipfix, &sflow, NULL, &odp_in_port);
968 if (error == ENODEV) {
969 /* Received packet on datapath port for which we couldn't
970 * associate an ofproto. This can happen if a port is removed
971 * while traffic is being received. Print a rate-limited
972 * message in case it happens frequently. Install a drop flow
973 * so that future packets of the flow are inexpensively dropped
975 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
976 "port %"PRIu32, odp_in_port);
977 dpif_flow_put(udpif->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
978 dupcall->key, dupcall->key_len, NULL, 0, NULL, 0,
981 list_remove(&upcall->list_node);
982 upcall_destroy(upcall);
986 type = classify_upcall(upcall);
987 if (type == MISS_UPCALL) {
990 flow_extract(packet, flow.skb_priority, flow.pkt_mark,
991 &flow.tunnel, &flow.in_port, &miss->flow);
993 hash = flow_hash(&miss->flow, 0);
994 existing_miss = flow_miss_find(&misses, ofproto, &miss->flow,
996 if (!existing_miss) {
997 hmap_insert(&misses, &miss->hmap_node, hash);
998 miss->ofproto = ofproto;
999 miss->key = dupcall->key;
1000 miss->key_len = dupcall->key_len;
1001 miss->upcall_type = dupcall->type;
1002 miss->stats.n_packets = 0;
1003 miss->stats.n_bytes = 0;
1004 miss->stats.used = time_msec();
1005 miss->stats.tcp_flags = 0;
1006 miss->odp_in_port = odp_in_port;
1011 miss = existing_miss;
1013 miss->stats.tcp_flags |= packet_get_tcp_flags(packet, &miss->flow);
1014 miss->stats.n_bytes += packet->size;
1015 miss->stats.n_packets++;
1017 upcall->flow_miss = miss;
1024 union user_action_cookie cookie;
1026 memset(&cookie, 0, sizeof cookie);
1027 memcpy(&cookie, nl_attr_get(dupcall->userdata),
1028 sizeof cookie.sflow);
1029 dpif_sflow_received(sflow, packet, &flow, odp_in_port,
1035 dpif_ipfix_bridge_sample(ipfix, packet, &flow);
1038 case FLOW_SAMPLE_UPCALL:
1040 union user_action_cookie cookie;
1042 memset(&cookie, 0, sizeof cookie);
1043 memcpy(&cookie, nl_attr_get(dupcall->userdata),
1044 sizeof cookie.flow_sample);
1046 /* The flow reflects exactly the contents of the packet.
1047 * Sample the packet using it. */
1048 dpif_ipfix_flow_sample(ipfix, packet, &flow,
1049 cookie.flow_sample.collector_set_id,
1050 cookie.flow_sample.probability,
1051 cookie.flow_sample.obs_domain_id,
1052 cookie.flow_sample.obs_point_id);
1061 dpif_ipfix_unref(ipfix);
1062 dpif_sflow_unref(sflow);
1064 list_remove(&upcall->list_node);
1065 upcall_destroy(upcall);
1068 /* Initialize each 'struct flow_miss's ->xout.
1070 * We do this per-flow_miss rather than per-packet because, most commonly,
1071 * all the packets in a flow can use the same translation.
1073 * We can't do this in the previous loop because we need the TCP flags for
1074 * all the packets in each miss. */
1076 HMAP_FOR_EACH (miss, hmap_node, &misses) {
1077 struct xlate_in xin;
1079 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL,
1080 miss->stats.tcp_flags, NULL);
1081 xin.may_learn = true;
1083 if (miss->upcall_type == DPIF_UC_MISS) {
1084 xin.resubmit_stats = &miss->stats;
1086 /* For non-miss upcalls, there's a flow in the datapath which this
1087 * packet was accounted to. Presumably the revalidators will deal
1088 * with pushing its stats eventually. */
1091 xlate_actions(&xin, &miss->xout);
1092 fail_open = fail_open || miss->xout.fail_open;
1095 /* Now handle the packets individually in order of arrival. In the common
1096 * case each packet of a miss can share the same actions, but slow-pathed
1097 * packets need to be translated individually:
1099 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
1100 * processes received packets for these protocols.
1102 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1105 * The loop fills 'ops' with an array of operations to execute in the
1108 LIST_FOR_EACH (upcall, list_node, upcalls) {
1109 struct flow_miss *miss = upcall->flow_miss;
1110 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
1112 ovs_be16 flow_vlan_tci;
1114 /* Save a copy of flow.vlan_tci in case it is changed to
1115 * generate proper mega flow masks for VLAN splinter flows. */
1116 flow_vlan_tci = miss->flow.vlan_tci;
1118 if (miss->xout.slow) {
1119 struct xlate_in xin;
1121 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL, 0, packet);
1122 xlate_actions_for_side_effects(&xin);
1125 if (miss->flow.in_port.ofp_port
1126 != vsp_realdev_to_vlandev(miss->ofproto,
1127 miss->flow.in_port.ofp_port,
1128 miss->flow.vlan_tci)) {
1129 /* This packet was received on a VLAN splinter port. We
1130 * added a VLAN to the packet to make the packet resemble
1131 * the flow, but the actions were composed assuming that
1132 * the packet contained no VLAN. So, we must remove the
1133 * VLAN header from the packet before trying to execute the
1135 if (miss->xout.odp_actions.size) {
1136 eth_pop_vlan(packet);
1139 /* Remove the flow vlan tags inserted by vlan splinter logic
1140 * to ensure megaflow masks generated match the data path flow. */
1141 miss->flow.vlan_tci = 0;
1144 /* Do not install a flow into the datapath if:
1146 * - The datapath already has too many flows.
1148 * - An earlier iteration of this loop already put the same flow.
1150 * - We received this packet via some flow installed in the kernel
1154 && upcall->dpif_upcall.type == DPIF_UC_MISS) {
1160 atomic_read(&enable_megaflows, &megaflow);
1161 ofpbuf_use_stack(&mask, &miss->mask_buf, sizeof miss->mask_buf);
1165 max_mpls = ofproto_dpif_get_max_mpls_depth(miss->ofproto);
1166 odp_flow_key_from_mask(&mask, &miss->xout.wc.masks,
1167 &miss->flow, UINT32_MAX, max_mpls);
1171 op->type = DPIF_OP_FLOW_PUT;
1172 op->u.flow_put.flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1173 op->u.flow_put.key = miss->key;
1174 op->u.flow_put.key_len = miss->key_len;
1175 op->u.flow_put.mask = mask.data;
1176 op->u.flow_put.mask_len = mask.size;
1177 op->u.flow_put.stats = NULL;
1179 if (!miss->xout.slow) {
1180 op->u.flow_put.actions = miss->xout.odp_actions.data;
1181 op->u.flow_put.actions_len = miss->xout.odp_actions.size;
1185 ofpbuf_use_stack(&buf, miss->slow_path_buf,
1186 sizeof miss->slow_path_buf);
1187 compose_slow_path(udpif, &miss->xout, miss->odp_in_port, &buf);
1188 op->u.flow_put.actions = buf.data;
1189 op->u.flow_put.actions_len = buf.size;
1194 * The 'miss' may be shared by multiple upcalls. Restore
1195 * the saved flow vlan_tci field before processing the next
1197 miss->flow.vlan_tci = flow_vlan_tci;
1199 if (miss->xout.odp_actions.size) {
1202 op->type = DPIF_OP_EXECUTE;
1203 op->u.execute.packet = packet;
1204 odp_key_to_pkt_metadata(miss->key, miss->key_len,
1206 op->u.execute.actions = miss->xout.odp_actions.data;
1207 op->u.execute.actions_len = miss->xout.odp_actions.size;
1208 op->u.execute.needs_help = (miss->xout.slow & SLOW_ACTION) != 0;
1212 /* Special case for fail-open mode.
1214 * If we are in fail-open mode, but we are connected to a controller too,
1215 * then we should send the packet up to the controller in the hope that it
1216 * will try to set up a flow and thereby allow us to exit fail-open.
1218 * See the top-level comment in fail-open.c for more information.
1220 * Copy packets before they are modified by execution. */
1222 LIST_FOR_EACH (upcall, list_node, upcalls) {
1223 struct flow_miss *miss = upcall->flow_miss;
1224 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
1225 struct ofproto_packet_in *pin;
1227 pin = xmalloc(sizeof *pin);
1228 pin->up.packet = xmemdup(packet->data, packet->size);
1229 pin->up.packet_len = packet->size;
1230 pin->up.reason = OFPR_NO_MATCH;
1231 pin->up.table_id = 0;
1232 pin->up.cookie = OVS_BE64_MAX;
1233 flow_get_metadata(&miss->flow, &pin->up.fmd);
1234 pin->send_len = 0; /* Not used for flow table misses. */
1235 pin->generated_by_table_miss = false;
1236 ofproto_dpif_send_packet_in(miss->ofproto, pin);
1240 /* Execute batch. */
1241 for (i = 0; i < n_ops; i++) {
1244 dpif_operate(udpif->dpif, opsp, n_ops);
1246 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &misses) {
1247 hmap_remove(&misses, &miss->hmap_node);
1248 xlate_out_uninit(&miss->xout);
1250 hmap_destroy(&misses);
1252 LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
1253 list_remove(&upcall->list_node);
1254 upcall_destroy(upcall);
1258 static struct udpif_key *
1259 ukey_lookup(struct revalidator *revalidator, struct udpif_flow_dump *udump)
1261 struct udpif_key *ukey;
1263 HMAP_FOR_EACH_WITH_HASH (ukey, hmap_node, udump->key_hash,
1264 &revalidator->ukeys) {
1265 if (ukey->key_len == udump->key_len
1266 && !memcmp(ukey->key, udump->key, udump->key_len)) {
1273 static struct udpif_key *
1274 ukey_create(const struct nlattr *key, size_t key_len, long long int used)
1276 struct udpif_key *ukey = xmalloc(sizeof *ukey);
1278 ukey->key = (struct nlattr *) &ukey->key_buf;
1279 memcpy(&ukey->key_buf, key, key_len);
1280 ukey->key_len = key_len;
1283 ukey->created = used ? used : time_msec();
1284 memset(&ukey->stats, 0, sizeof ukey->stats);
1290 ukey_delete(struct revalidator *revalidator, struct udpif_key *ukey)
1292 hmap_remove(&revalidator->ukeys, &ukey->hmap_node);
1297 revalidate_ukey(struct udpif *udpif, struct udpif_flow_dump *udump,
1298 struct udpif_key *ukey)
1300 struct ofpbuf xout_actions, *actions;
1301 uint64_t slow_path_buf[128 / 8];
1302 struct xlate_out xout, *xoutp;
1303 struct flow flow, udump_mask;
1304 struct ofproto_dpif *ofproto;
1305 struct dpif_flow_stats push;
1306 uint32_t *udump32, *xout32;
1307 odp_port_t odp_in_port;
1308 struct xlate_in xin;
1317 /* If we don't need to revalidate, we can simply push the stats contained
1318 * in the udump, otherwise we'll have to get the actions so we can check
1320 if (udump->need_revalidate) {
1321 if (dpif_flow_get(udpif->dpif, ukey->key, ukey->key_len, &actions,
1327 push.used = udump->stats.used;
1328 push.tcp_flags = udump->stats.tcp_flags;
1329 push.n_packets = udump->stats.n_packets > ukey->stats.n_packets
1330 ? udump->stats.n_packets - ukey->stats.n_packets
1332 push.n_bytes = udump->stats.n_bytes > ukey->stats.n_bytes
1333 ? udump->stats.n_bytes - ukey->stats.n_bytes
1335 ukey->stats = udump->stats;
1337 if (!push.n_packets && !udump->need_revalidate) {
1342 error = xlate_receive(udpif->backer, NULL, ukey->key, ukey->key_len, &flow,
1343 &ofproto, NULL, NULL, NULL, &odp_in_port);
1348 xlate_in_init(&xin, ofproto, &flow, NULL, push.tcp_flags, NULL);
1349 xin.resubmit_stats = push.n_packets ? &push : NULL;
1350 xin.may_learn = push.n_packets > 0;
1351 xin.skip_wildcards = !udump->need_revalidate;
1352 xlate_actions(&xin, &xout);
1355 if (!udump->need_revalidate) {
1361 ofpbuf_use_const(&xout_actions, xout.odp_actions.data,
1362 xout.odp_actions.size);
1364 ofpbuf_use_stack(&xout_actions, slow_path_buf, sizeof slow_path_buf);
1365 compose_slow_path(udpif, &xout, odp_in_port, &xout_actions);
1368 if (!ofpbuf_equal(&xout_actions, actions)) {
1372 if (odp_flow_key_to_mask(udump->mask, udump->mask_len, &udump_mask, &flow)
1377 /* Since the kernel is free to ignore wildcarded bits in the mask, we can't
1378 * directly check that the masks are the same. Instead we check that the
1379 * mask in the kernel is more specific i.e. less wildcarded, than what
1380 * we've calculated here. This guarantees we don't catch any packets we
1381 * shouldn't with the megaflow. */
1382 udump32 = (uint32_t *) &udump_mask;
1383 xout32 = (uint32_t *) &xout.wc.masks;
1384 for (i = 0; i < FLOW_U32S; i++) {
1385 if ((udump32[i] | xout32[i]) != udump32[i]) {
1392 ofpbuf_delete(actions);
1393 xlate_out_uninit(xoutp);
1398 struct udpif_key *ukey;
1399 struct udpif_flow_dump *udump;
1400 struct dpif_flow_stats stats; /* Stats for 'op'. */
1401 struct dpif_op op; /* Flow del operation. */
1405 dump_op_init(struct dump_op *op, const struct nlattr *key, size_t key_len,
1406 struct udpif_key *ukey, struct udpif_flow_dump *udump)
1410 op->op.type = DPIF_OP_FLOW_DEL;
1411 op->op.u.flow_del.key = key;
1412 op->op.u.flow_del.key_len = key_len;
1413 op->op.u.flow_del.stats = &op->stats;
1417 push_dump_ops(struct revalidator *revalidator,
1418 struct dump_op *ops, size_t n_ops)
1420 struct udpif *udpif = revalidator->udpif;
1421 struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
1424 ovs_assert(n_ops <= REVALIDATE_MAX_BATCH);
1425 for (i = 0; i < n_ops; i++) {
1426 opsp[i] = &ops[i].op;
1428 dpif_operate(udpif->dpif, opsp, n_ops);
1430 for (i = 0; i < n_ops; i++) {
1431 struct dump_op *op = &ops[i];
1432 struct dpif_flow_stats *push, *stats, push_buf;
1434 stats = op->op.u.flow_del.stats;
1437 push->used = MAX(stats->used, op->ukey->stats.used);
1438 push->tcp_flags = stats->tcp_flags | op->ukey->stats.tcp_flags;
1439 push->n_packets = stats->n_packets - op->ukey->stats.n_packets;
1440 push->n_bytes = stats->n_bytes - op->ukey->stats.n_bytes;
1445 if (push->n_packets || netflow_exists()) {
1446 struct ofproto_dpif *ofproto;
1447 struct netflow *netflow;
1450 if (!xlate_receive(udpif->backer, NULL, op->op.u.flow_del.key,
1451 op->op.u.flow_del.key_len, &flow, &ofproto,
1452 NULL, NULL, &netflow, NULL)) {
1453 struct xlate_in xin;
1455 xlate_in_init(&xin, ofproto, &flow, NULL, push->tcp_flags,
1457 xin.resubmit_stats = push->n_packets ? push : NULL;
1458 xin.may_learn = push->n_packets > 0;
1459 xin.skip_wildcards = true;
1460 xlate_actions_for_side_effects(&xin);
1463 netflow_expire(netflow, &flow);
1464 netflow_flow_clear(netflow, &flow);
1465 netflow_unref(netflow);
1471 for (i = 0; i < n_ops; i++) {
1472 struct udpif_key *ukey;
1474 /* If there's a udump, this ukey came directly from a datapath flow
1475 * dump. Sometimes a datapath can send duplicates in flow dumps, in
1476 * which case we wouldn't want to double-free a ukey, so avoid that by
1477 * looking up the ukey again.
1479 * If there's no udump then we know what we're doing. */
1480 ukey = (ops[i].udump
1481 ? ukey_lookup(revalidator, ops[i].udump)
1484 ukey_delete(revalidator, ukey);
1490 revalidate_udumps(struct revalidator *revalidator, struct list *udumps)
1492 struct udpif *udpif = revalidator->udpif;
1494 struct dump_op ops[REVALIDATE_MAX_BATCH];
1495 struct udpif_flow_dump *udump, *next_udump;
1496 size_t n_ops, n_flows;
1497 unsigned int flow_limit;
1498 long long int max_idle;
1501 atomic_read(&udpif->flow_limit, &flow_limit);
1503 n_flows = udpif_get_n_flows(udpif);
1506 max_idle = MAX_IDLE;
1507 if (n_flows > flow_limit) {
1508 must_del = n_flows > 2 * flow_limit;
1513 LIST_FOR_EACH_SAFE (udump, next_udump, list_node, udumps) {
1514 long long int used, now;
1515 struct udpif_key *ukey;
1518 ukey = ukey_lookup(revalidator, udump);
1520 used = udump->stats.used;
1521 if (!used && ukey) {
1522 used = ukey->created;
1525 if (must_del || (used && used < now - max_idle)) {
1526 struct dump_op *dop = &ops[n_ops++];
1528 dump_op_init(dop, udump->key, udump->key_len, ukey, udump);
1533 ukey = ukey_create(udump->key, udump->key_len, used);
1534 hmap_insert(&revalidator->ukeys, &ukey->hmap_node,
1539 if (!revalidate_ukey(udpif, udump, ukey)) {
1540 dpif_flow_del(udpif->dpif, udump->key, udump->key_len, NULL);
1541 ukey_delete(revalidator, ukey);
1544 list_remove(&udump->list_node);
1548 push_dump_ops(revalidator, ops, n_ops);
1550 LIST_FOR_EACH_SAFE (udump, next_udump, list_node, udumps) {
1551 list_remove(&udump->list_node);
1557 revalidator_sweep__(struct revalidator *revalidator, bool purge)
1559 struct dump_op ops[REVALIDATE_MAX_BATCH];
1560 struct udpif_key *ukey, *next;
1565 HMAP_FOR_EACH_SAFE (ukey, next, hmap_node, &revalidator->ukeys) {
1566 if (!purge && ukey->mark) {
1569 struct dump_op *op = &ops[n_ops++];
1571 /* If we have previously seen a flow in the datapath, but didn't
1572 * see it during the most recent dump, delete it. This allows us
1573 * to clean up the ukey and keep the statistics consistent. */
1574 dump_op_init(op, ukey->key, ukey->key_len, ukey, NULL);
1575 if (n_ops == REVALIDATE_MAX_BATCH) {
1576 push_dump_ops(revalidator, ops, n_ops);
1583 push_dump_ops(revalidator, ops, n_ops);
1588 revalidator_sweep(struct revalidator *revalidator)
1590 revalidator_sweep__(revalidator, false);
1594 revalidator_purge(struct revalidator *revalidator)
1596 revalidator_sweep__(revalidator, true);
1600 upcall_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1601 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
1603 struct ds ds = DS_EMPTY_INITIALIZER;
1604 struct udpif *udpif;
1606 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1607 unsigned int flow_limit;
1610 atomic_read(&udpif->flow_limit, &flow_limit);
1612 ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
1613 ds_put_format(&ds, "\tflows : (current %"PRIu64")"
1614 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
1615 udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
1616 ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
1618 ds_put_char(&ds, '\n');
1619 for (i = 0; i < udpif->n_handlers; i++) {
1620 struct handler *handler = &udpif->handlers[i];
1622 ovs_mutex_lock(&handler->mutex);
1623 ds_put_format(&ds, "\t%s: (upcall queue %"PRIuSIZE")\n",
1624 handler->name, handler->n_upcalls);
1625 ovs_mutex_unlock(&handler->mutex);
1628 ds_put_char(&ds, '\n');
1629 for (i = 0; i < n_revalidators; i++) {
1630 struct revalidator *revalidator = &udpif->revalidators[i];
1632 /* XXX: The result of hmap_count(&revalidator->ukeys) may not be
1633 * accurate because it's not protected by the revalidator mutex. */
1634 ovs_mutex_lock(&revalidator->mutex);
1635 ds_put_format(&ds, "\t%s: (dump queue %"PRIuSIZE") (keys %"PRIuSIZE
1636 ")\n", revalidator->name, revalidator->n_udumps,
1637 hmap_count(&revalidator->ukeys));
1638 ovs_mutex_unlock(&revalidator->mutex);
1642 unixctl_command_reply(conn, ds_cstr(&ds));
1646 /* Disable using the megaflows.
1648 * This command is only needed for advanced debugging, so it's not
1649 * documented in the man page. */
1651 upcall_unixctl_disable_megaflows(struct unixctl_conn *conn,
1652 int argc OVS_UNUSED,
1653 const char *argv[] OVS_UNUSED,
1654 void *aux OVS_UNUSED)
1656 atomic_store(&enable_megaflows, false);
1658 unixctl_command_reply(conn, "megaflows disabled");
1661 /* Re-enable using megaflows.
1663 * This command is only needed for advanced debugging, so it's not
1664 * documented in the man page. */
1666 upcall_unixctl_enable_megaflows(struct unixctl_conn *conn,
1667 int argc OVS_UNUSED,
1668 const char *argv[] OVS_UNUSED,
1669 void *aux OVS_UNUSED)
1671 atomic_store(&enable_megaflows, true);
1673 unixctl_command_reply(conn, "megaflows enabled");
1676 /* Set the flow limit.
1678 * This command is only needed for advanced debugging, so it's not
1679 * documented in the man page. */
1681 upcall_unixctl_set_flow_limit(struct unixctl_conn *conn,
1682 int argc OVS_UNUSED,
1683 const char *argv[] OVS_UNUSED,
1684 void *aux OVS_UNUSED)
1686 struct ds ds = DS_EMPTY_INITIALIZER;
1687 struct udpif *udpif;
1688 unsigned int flow_limit = atoi(argv[1]);
1690 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1691 atomic_store(&udpif->flow_limit, flow_limit);
1693 ds_put_format(&ds, "set flow_limit to %u\n", flow_limit);
1694 unixctl_command_reply(conn, ds_cstr(&ds));