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_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'. */
171 struct xlate_cache *xcache; /* Cache for xlate entries that
172 * are affected by this ukey.
173 * Used for stats and learning.*/
176 /* 'udpif_flow_dump's hold the state associated with one iteration in a flow
177 * dump operation. This is created by the flow_dumper thread and handed to the
178 * appropriate revalidator thread to be processed. */
179 struct udpif_flow_dump {
180 struct list list_node;
182 struct nlattr *key; /* Datapath flow key. */
183 size_t key_len; /* Length of 'key'. */
184 uint32_t key_hash; /* Hash of 'key'. */
186 struct odputil_keybuf mask_buf;
187 struct nlattr *mask; /* Datapath mask for 'key'. */
188 size_t mask_len; /* Length of 'mask'. */
190 struct dpif_flow_stats stats; /* Stats pulled from the datapath. */
192 bool need_revalidate; /* Key needs revalidation? */
194 struct odputil_keybuf key_buf;
197 /* Flow miss batching.
199 * Some dpifs implement operations faster when you hand them off in a batch.
200 * To allow batching, "struct flow_miss" queues the dpif-related work needed
201 * for a given flow. Each "struct flow_miss" corresponds to sending one or
202 * more packets, plus possibly installing the flow in the dpif. */
204 struct hmap_node hmap_node;
205 struct ofproto_dpif *ofproto;
208 const struct nlattr *key;
210 enum dpif_upcall_type upcall_type;
211 struct dpif_flow_stats stats;
212 odp_port_t odp_in_port;
214 uint64_t slow_path_buf[128 / 8];
215 struct odputil_keybuf mask_buf;
217 struct xlate_out xout;
222 static void upcall_destroy(struct upcall *);
224 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
225 static struct list all_udpifs = LIST_INITIALIZER(&all_udpifs);
227 static void recv_upcalls(struct udpif *);
228 static void handle_upcalls(struct handler *handler, struct list *upcalls);
229 static void *udpif_flow_dumper(void *);
230 static void *udpif_dispatcher(void *);
231 static void *udpif_upcall_handler(void *);
232 static void *udpif_revalidator(void *);
233 static uint64_t udpif_get_n_flows(struct udpif *);
234 static void revalidate_udumps(struct revalidator *, struct list *udumps);
235 static void revalidator_sweep(struct revalidator *);
236 static void revalidator_purge(struct revalidator *);
237 static void upcall_unixctl_show(struct unixctl_conn *conn, int argc,
238 const char *argv[], void *aux);
239 static void upcall_unixctl_disable_megaflows(struct unixctl_conn *, int argc,
240 const char *argv[], void *aux);
241 static void upcall_unixctl_enable_megaflows(struct unixctl_conn *, int argc,
242 const char *argv[], void *aux);
243 static void upcall_unixctl_set_flow_limit(struct unixctl_conn *conn, int argc,
244 const char *argv[], void *aux);
245 static void ukey_delete(struct revalidator *, struct udpif_key *);
247 static atomic_bool enable_megaflows = ATOMIC_VAR_INIT(true);
250 udpif_create(struct dpif_backer *backer, struct dpif *dpif)
252 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
253 struct udpif *udpif = xzalloc(sizeof *udpif);
255 if (ovsthread_once_start(&once)) {
256 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show,
258 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
259 upcall_unixctl_disable_megaflows, NULL);
260 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
261 upcall_unixctl_enable_megaflows, NULL);
262 unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
263 upcall_unixctl_set_flow_limit, NULL);
264 ovsthread_once_done(&once);
268 udpif->backer = backer;
269 atomic_init(&udpif->flow_limit, MIN(ofproto_flow_limit, 10000));
270 udpif->secret = random_uint32();
271 udpif->reval_seq = seq_create();
272 udpif->dump_seq = seq_create();
273 latch_init(&udpif->exit_latch);
274 list_push_back(&all_udpifs, &udpif->list_node);
275 atomic_init(&udpif->n_flows, 0);
276 atomic_init(&udpif->n_flows_timestamp, LLONG_MIN);
277 ovs_mutex_init(&udpif->n_flows_mutex);
283 udpif_destroy(struct udpif *udpif)
285 udpif_set_threads(udpif, 0, 0);
288 list_remove(&udpif->list_node);
289 latch_destroy(&udpif->exit_latch);
290 seq_destroy(udpif->reval_seq);
291 seq_destroy(udpif->dump_seq);
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)
306 ovsrcu_quiesce_start();
307 /* Stop the old threads (if any). */
308 if (udpif->handlers &&
309 (udpif->n_handlers != n_handlers
310 || udpif->n_revalidators != n_revalidators)) {
313 latch_set(&udpif->exit_latch);
315 for (i = 0; i < udpif->n_handlers; i++) {
316 struct handler *handler = &udpif->handlers[i];
318 ovs_mutex_lock(&handler->mutex);
319 xpthread_cond_signal(&handler->wake_cond);
320 ovs_mutex_unlock(&handler->mutex);
321 xpthread_join(handler->thread, NULL);
324 for (i = 0; i < udpif->n_revalidators; i++) {
325 struct revalidator *revalidator = &udpif->revalidators[i];
327 ovs_mutex_lock(&revalidator->mutex);
328 xpthread_cond_signal(&revalidator->wake_cond);
329 ovs_mutex_unlock(&revalidator->mutex);
330 xpthread_join(revalidator->thread, NULL);
333 xpthread_join(udpif->flow_dumper, NULL);
334 xpthread_join(udpif->dispatcher, NULL);
336 for (i = 0; i < udpif->n_revalidators; i++) {
337 struct revalidator *revalidator = &udpif->revalidators[i];
338 struct udpif_flow_dump *udump, *next_udump;
340 LIST_FOR_EACH_SAFE (udump, next_udump, list_node,
341 &revalidator->udumps) {
342 list_remove(&udump->list_node);
346 /* Delete ukeys, and delete all flows from the datapath to prevent
347 * double-counting stats. */
348 revalidator_purge(revalidator);
349 hmap_destroy(&revalidator->ukeys);
350 ovs_mutex_destroy(&revalidator->mutex);
352 free(revalidator->name);
355 for (i = 0; i < udpif->n_handlers; i++) {
356 struct handler *handler = &udpif->handlers[i];
357 struct upcall *miss, *next;
359 LIST_FOR_EACH_SAFE (miss, next, list_node, &handler->upcalls) {
360 list_remove(&miss->list_node);
361 upcall_destroy(miss);
363 ovs_mutex_destroy(&handler->mutex);
365 xpthread_cond_destroy(&handler->wake_cond);
368 latch_poll(&udpif->exit_latch);
370 free(udpif->revalidators);
371 udpif->revalidators = NULL;
372 udpif->n_revalidators = 0;
374 free(udpif->handlers);
375 udpif->handlers = NULL;
376 udpif->n_handlers = 0;
379 error = dpif_handlers_set(udpif->dpif, 1);
381 VLOG_ERR("failed to configure handlers in dpif %s: %s",
382 dpif_name(udpif->dpif), ovs_strerror(error));
386 /* Start new threads (if necessary). */
387 if (!udpif->handlers && n_handlers) {
390 udpif->n_handlers = n_handlers;
391 udpif->n_revalidators = n_revalidators;
393 udpif->handlers = xzalloc(udpif->n_handlers * sizeof *udpif->handlers);
394 for (i = 0; i < udpif->n_handlers; i++) {
395 struct handler *handler = &udpif->handlers[i];
397 handler->udpif = udpif;
398 list_init(&handler->upcalls);
399 handler->need_signal = false;
400 xpthread_cond_init(&handler->wake_cond, NULL);
401 ovs_mutex_init(&handler->mutex);
402 xpthread_create(&handler->thread, NULL, udpif_upcall_handler,
406 udpif->revalidators = xzalloc(udpif->n_revalidators
407 * sizeof *udpif->revalidators);
408 for (i = 0; i < udpif->n_revalidators; i++) {
409 struct revalidator *revalidator = &udpif->revalidators[i];
411 revalidator->udpif = udpif;
412 list_init(&revalidator->udumps);
413 hmap_init(&revalidator->ukeys);
414 ovs_mutex_init(&revalidator->mutex);
415 xpthread_cond_init(&revalidator->wake_cond, NULL);
416 xpthread_create(&revalidator->thread, NULL, udpif_revalidator,
419 xpthread_create(&udpif->dispatcher, NULL, udpif_dispatcher, udpif);
420 xpthread_create(&udpif->flow_dumper, NULL, udpif_flow_dumper, udpif);
423 ovsrcu_quiesce_end();
426 /* Waits for all ongoing upcall translations to complete. This ensures that
427 * there are no transient references to any removed ofprotos (or other
428 * objects). In particular, this should be called after an ofproto is removed
429 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
431 udpif_synchronize(struct udpif *udpif)
433 /* This is stronger than necessary. It would be sufficient to ensure
434 * (somehow) that each handler and revalidator thread had passed through
435 * its main loop once. */
436 size_t n_handlers = udpif->n_handlers;
437 size_t n_revalidators = udpif->n_revalidators;
438 udpif_set_threads(udpif, 0, 0);
439 udpif_set_threads(udpif, n_handlers, n_revalidators);
442 /* Notifies 'udpif' that something changed which may render previous
443 * xlate_actions() results invalid. */
445 udpif_revalidate(struct udpif *udpif)
447 seq_change(udpif->reval_seq);
450 /* Returns a seq which increments every time 'udpif' pulls stats from the
451 * datapath. Callers can use this to get a sense of when might be a good time
452 * to do periodic work which relies on relatively up to date statistics. */
454 udpif_dump_seq(struct udpif *udpif)
456 return udpif->dump_seq;
460 udpif_get_memory_usage(struct udpif *udpif, struct simap *usage)
464 simap_increase(usage, "dispatchers", 1);
465 simap_increase(usage, "flow_dumpers", 1);
467 simap_increase(usage, "handlers", udpif->n_handlers);
468 for (i = 0; i < udpif->n_handlers; i++) {
469 struct handler *handler = &udpif->handlers[i];
470 ovs_mutex_lock(&handler->mutex);
471 simap_increase(usage, "handler upcalls", handler->n_upcalls);
472 ovs_mutex_unlock(&handler->mutex);
475 simap_increase(usage, "revalidators", udpif->n_revalidators);
476 for (i = 0; i < udpif->n_revalidators; i++) {
477 struct revalidator *revalidator = &udpif->revalidators[i];
478 ovs_mutex_lock(&revalidator->mutex);
479 simap_increase(usage, "revalidator dumps", revalidator->n_udumps);
481 /* XXX: This isn't technically thread safe because the revalidator
482 * ukeys maps isn't protected by a mutex since it's per thread. */
483 simap_increase(usage, "revalidator keys",
484 hmap_count(&revalidator->ukeys));
485 ovs_mutex_unlock(&revalidator->mutex);
489 /* Remove flows from a single datapath. */
491 udpif_flush(struct udpif *udpif)
493 size_t n_handlers, n_revalidators;
495 n_handlers = udpif->n_handlers;
496 n_revalidators = udpif->n_revalidators;
498 udpif_set_threads(udpif, 0, 0);
499 dpif_flow_flush(udpif->dpif);
500 udpif_set_threads(udpif, n_handlers, n_revalidators);
503 /* Removes all flows from all datapaths. */
505 udpif_flush_all_datapaths(void)
509 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
515 /* Destroys and deallocates 'upcall'. */
517 upcall_destroy(struct upcall *upcall)
520 ofpbuf_uninit(&upcall->dpif_upcall.packet);
521 ofpbuf_uninit(&upcall->upcall_buf);
527 udpif_get_n_flows(struct udpif *udpif)
529 long long int time, now;
533 atomic_read(&udpif->n_flows_timestamp, &time);
534 if (time < now - 100 && !ovs_mutex_trylock(&udpif->n_flows_mutex)) {
535 struct dpif_dp_stats stats;
537 atomic_store(&udpif->n_flows_timestamp, now);
538 dpif_get_dp_stats(udpif->dpif, &stats);
539 flow_count = stats.n_flows;
540 atomic_store(&udpif->n_flows, flow_count);
541 ovs_mutex_unlock(&udpif->n_flows_mutex);
543 atomic_read(&udpif->n_flows, &flow_count);
548 /* The dispatcher thread is responsible for receiving upcalls from the kernel,
549 * assigning them to a upcall_handler thread. */
551 udpif_dispatcher(void *arg)
553 struct udpif *udpif = arg;
555 set_subprogram_name("dispatcher");
556 while (!latch_is_set(&udpif->exit_latch)) {
558 dpif_recv_wait(udpif->dpif, 0);
559 latch_wait(&udpif->exit_latch);
567 udpif_flow_dumper(void *arg)
569 struct udpif *udpif = arg;
571 set_subprogram_name("flow_dumper");
572 while (!latch_is_set(&udpif->exit_latch)) {
573 const struct dpif_flow_stats *stats;
574 long long int start_time, duration;
575 const struct nlattr *key, *mask;
576 struct dpif_flow_dump dump;
577 size_t key_len, mask_len;
578 unsigned int flow_limit;
579 bool need_revalidate;
585 reval_seq = seq_read(udpif->reval_seq);
586 need_revalidate = udpif->last_reval_seq != reval_seq;
587 udpif->last_reval_seq = reval_seq;
589 n_flows = udpif_get_n_flows(udpif);
590 udpif->max_n_flows = MAX(n_flows, udpif->max_n_flows);
591 udpif->avg_n_flows = (udpif->avg_n_flows + n_flows) / 2;
593 start_time = time_msec();
594 error = dpif_flow_dump_start(&dump, udpif->dpif);
596 VLOG_INFO("Failed to start flow dump (%s)", ovs_strerror(error));
599 dpif_flow_dump_state_init(udpif->dpif, &state);
600 while (dpif_flow_dump_next(&dump, state, &key, &key_len,
601 &mask, &mask_len, NULL, NULL, &stats)
602 && !latch_is_set(&udpif->exit_latch)) {
603 struct udpif_flow_dump *udump = xmalloc(sizeof *udump);
604 struct revalidator *revalidator;
606 udump->key_hash = hash_bytes(key, key_len, udpif->secret);
607 memcpy(&udump->key_buf, key, key_len);
608 udump->key = (struct nlattr *) &udump->key_buf;
609 udump->key_len = key_len;
611 memcpy(&udump->mask_buf, mask, mask_len);
612 udump->mask = (struct nlattr *) &udump->mask_buf;
613 udump->mask_len = mask_len;
615 udump->stats = *stats;
616 udump->need_revalidate = need_revalidate;
618 revalidator = &udpif->revalidators[udump->key_hash
619 % udpif->n_revalidators];
621 ovs_mutex_lock(&revalidator->mutex);
622 while (revalidator->n_udumps >= REVALIDATE_MAX_BATCH * 3
623 && !latch_is_set(&udpif->exit_latch)) {
624 ovs_mutex_cond_wait(&revalidator->wake_cond,
625 &revalidator->mutex);
627 list_push_back(&revalidator->udumps, &udump->list_node);
628 revalidator->n_udumps++;
629 xpthread_cond_signal(&revalidator->wake_cond);
630 ovs_mutex_unlock(&revalidator->mutex);
632 dpif_flow_dump_state_uninit(udpif->dpif, state);
633 dpif_flow_dump_done(&dump);
635 /* Let all the revalidators finish and garbage collect. */
636 seq_change(udpif->dump_seq);
637 for (i = 0; i < udpif->n_revalidators; i++) {
638 struct revalidator *revalidator = &udpif->revalidators[i];
639 ovs_mutex_lock(&revalidator->mutex);
640 xpthread_cond_signal(&revalidator->wake_cond);
641 ovs_mutex_unlock(&revalidator->mutex);
644 for (i = 0; i < udpif->n_revalidators; i++) {
645 struct revalidator *revalidator = &udpif->revalidators[i];
647 ovs_mutex_lock(&revalidator->mutex);
648 while (revalidator->dump_seq != seq_read(udpif->dump_seq)
649 && !latch_is_set(&udpif->exit_latch)) {
650 ovs_mutex_cond_wait(&revalidator->wake_cond,
651 &revalidator->mutex);
653 ovs_mutex_unlock(&revalidator->mutex);
656 duration = MAX(time_msec() - start_time, 1);
657 udpif->dump_duration = duration;
658 atomic_read(&udpif->flow_limit, &flow_limit);
659 if (duration > 2000) {
660 flow_limit /= duration / 1000;
661 } else if (duration > 1300) {
662 flow_limit = flow_limit * 3 / 4;
663 } else if (duration < 1000 && n_flows > 2000
664 && flow_limit < n_flows * 1000 / duration) {
667 flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
668 atomic_store(&udpif->flow_limit, flow_limit);
670 if (duration > 2000) {
671 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
676 poll_timer_wait_until(start_time + MIN(ofproto_max_idle, 500));
677 seq_wait(udpif->reval_seq, udpif->last_reval_seq);
678 latch_wait(&udpif->exit_latch);
685 /* The miss handler thread is responsible for processing miss upcalls retrieved
686 * by the dispatcher thread. Once finished it passes the processed miss
687 * upcalls to ofproto-dpif where they're installed in the datapath. */
689 udpif_upcall_handler(void *arg)
691 struct handler *handler = arg;
693 handler->name = xasprintf("handler_%u", ovsthread_id_self());
694 set_subprogram_name("%s", handler->name);
696 while (!latch_is_set(&handler->udpif->exit_latch)) {
697 struct list misses = LIST_INITIALIZER(&misses);
700 ovs_mutex_lock(&handler->mutex);
701 /* Must check the 'exit_latch' again to make sure the main thread is
702 * not joining on the handler thread. */
703 if (!handler->n_upcalls
704 && !latch_is_set(&handler->udpif->exit_latch)) {
705 ovs_mutex_cond_wait(&handler->wake_cond, &handler->mutex);
708 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
709 if (handler->n_upcalls) {
710 handler->n_upcalls--;
711 list_push_back(&misses, list_pop_front(&handler->upcalls));
716 ovs_mutex_unlock(&handler->mutex);
718 handle_upcalls(handler, &misses);
727 udpif_revalidator(void *arg)
729 struct revalidator *revalidator = arg;
731 revalidator->name = xasprintf("revalidator_%u", ovsthread_id_self());
732 set_subprogram_name("%s", revalidator->name);
734 struct list udumps = LIST_INITIALIZER(&udumps);
735 struct udpif *udpif = revalidator->udpif;
738 ovs_mutex_lock(&revalidator->mutex);
739 if (latch_is_set(&udpif->exit_latch)) {
740 ovs_mutex_unlock(&revalidator->mutex);
744 if (!revalidator->n_udumps) {
745 if (revalidator->dump_seq != seq_read(udpif->dump_seq)) {
746 revalidator->dump_seq = seq_read(udpif->dump_seq);
747 revalidator_sweep(revalidator);
749 ovs_mutex_cond_wait(&revalidator->wake_cond,
750 &revalidator->mutex);
754 for (i = 0; i < REVALIDATE_MAX_BATCH && revalidator->n_udumps; i++) {
755 list_push_back(&udumps, list_pop_front(&revalidator->udumps));
756 revalidator->n_udumps--;
759 /* Wake up the flow dumper. */
760 xpthread_cond_signal(&revalidator->wake_cond);
761 ovs_mutex_unlock(&revalidator->mutex);
763 if (!list_is_empty(&udumps)) {
764 revalidate_udumps(revalidator, &udumps);
771 static enum upcall_type
772 classify_upcall(const struct upcall *upcall)
774 const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
775 union user_action_cookie cookie;
778 /* First look at the upcall type. */
779 switch (dpif_upcall->type) {
786 case DPIF_N_UC_TYPES:
788 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
793 /* "action" upcalls need a closer look. */
794 if (!dpif_upcall->userdata) {
795 VLOG_WARN_RL(&rl, "action upcall missing cookie");
798 userdata_len = nl_attr_get_size(dpif_upcall->userdata);
799 if (userdata_len < sizeof cookie.type
800 || userdata_len > sizeof cookie) {
801 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
805 memset(&cookie, 0, sizeof cookie);
806 memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
807 if (userdata_len == MAX(8, sizeof cookie.sflow)
808 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
810 } else if (userdata_len == MAX(8, sizeof cookie.slow_path)
811 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
813 } else if (userdata_len == MAX(8, sizeof cookie.flow_sample)
814 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
815 return FLOW_SAMPLE_UPCALL;
816 } else if (userdata_len == MAX(8, sizeof cookie.ipfix)
817 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
820 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
821 " and size %"PRIuSIZE, cookie.type, userdata_len);
827 recv_upcalls(struct udpif *udpif)
832 uint32_t hash = udpif->secret;
833 struct handler *handler;
834 struct upcall *upcall;
835 size_t n_bytes, left;
839 upcall = xmalloc(sizeof *upcall);
840 ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
841 sizeof upcall->upcall_stub);
842 error = dpif_recv(udpif->dpif, 0, &upcall->dpif_upcall,
843 &upcall->upcall_buf);
845 /* upcall_destroy() can only be called on successfully received
847 ofpbuf_uninit(&upcall->upcall_buf);
853 NL_ATTR_FOR_EACH (nla, left, upcall->dpif_upcall.key,
854 upcall->dpif_upcall.key_len) {
855 enum ovs_key_attr type = nl_attr_type(nla);
856 if (type == OVS_KEY_ATTR_IN_PORT
857 || type == OVS_KEY_ATTR_TCP
858 || type == OVS_KEY_ATTR_UDP) {
859 if (nl_attr_get_size(nla) == 4) {
860 hash = mhash_add(hash, nl_attr_get_u32(nla));
864 "Netlink attribute with incorrect size.");
868 hash = mhash_finish(hash, n_bytes);
870 handler = &udpif->handlers[hash % udpif->n_handlers];
872 ovs_mutex_lock(&handler->mutex);
873 if (handler->n_upcalls < MAX_QUEUE_LENGTH) {
874 list_push_back(&handler->upcalls, &upcall->list_node);
875 if (handler->n_upcalls == 0) {
876 handler->need_signal = true;
878 handler->n_upcalls++;
879 if (handler->need_signal &&
880 handler->n_upcalls >= FLOW_MISS_MAX_BATCH) {
881 handler->need_signal = false;
882 xpthread_cond_signal(&handler->wake_cond);
884 ovs_mutex_unlock(&handler->mutex);
885 if (!VLOG_DROP_DBG(&rl)) {
886 struct ds ds = DS_EMPTY_INITIALIZER;
888 odp_flow_key_format(upcall->dpif_upcall.key,
889 upcall->dpif_upcall.key_len,
891 VLOG_DBG("dispatcher: enqueue (%s)", ds_cstr(&ds));
895 ovs_mutex_unlock(&handler->mutex);
896 COVERAGE_INC(upcall_queue_overflow);
897 upcall_destroy(upcall);
901 for (n = 0; n < udpif->n_handlers; ++n) {
902 struct handler *handler = &udpif->handlers[n];
904 if (handler->need_signal) {
905 handler->need_signal = false;
906 ovs_mutex_lock(&handler->mutex);
907 xpthread_cond_signal(&handler->wake_cond);
908 ovs_mutex_unlock(&handler->mutex);
913 /* Calculates slow path actions for 'xout'. 'buf' must statically be
914 * initialized with at least 128 bytes of space. */
916 compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
917 odp_port_t odp_in_port, struct ofpbuf *buf)
919 union user_action_cookie cookie;
923 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
924 cookie.slow_path.unused = 0;
925 cookie.slow_path.reason = xout->slow;
927 port = xout->slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)
930 pid = dpif_port_get_pid(udpif->dpif, port, 0);
931 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, buf);
934 static struct flow_miss *
935 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
936 const struct flow *flow, uint32_t hash)
938 struct flow_miss *miss;
940 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
941 if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
950 handle_upcalls(struct handler *handler, struct list *upcalls)
952 struct hmap misses = HMAP_INITIALIZER(&misses);
953 struct udpif *udpif = handler->udpif;
955 struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH];
956 struct dpif_op *opsp[FLOW_MISS_MAX_BATCH * 2];
957 struct dpif_op ops[FLOW_MISS_MAX_BATCH * 2];
958 struct flow_miss *miss, *next_miss;
959 struct upcall *upcall, *next;
960 size_t n_misses, n_ops, i;
961 unsigned int flow_limit;
962 bool fail_open, may_put;
963 enum upcall_type type;
965 atomic_read(&udpif->flow_limit, &flow_limit);
966 may_put = udpif_get_n_flows(udpif) < flow_limit;
968 /* Extract the flow from each upcall. Construct in 'misses' a hash table
969 * that maps each unique flow to a 'struct flow_miss'.
971 * Most commonly there is a single packet per flow_miss, but there are
972 * several reasons why there might be more than one, e.g.:
974 * - The dpif packet interface does not support TSO (or UFO, etc.), so a
975 * large packet sent to userspace is split into a sequence of smaller
978 * - A stream of quickly arriving packets in an established "slow-pathed"
981 * - Rarely, a stream of quickly arriving packets in a flow not yet
982 * established. (This is rare because most protocols do not send
983 * multiple back-to-back packets before receiving a reply from the
984 * other end of the connection, which gives OVS a chance to set up a
988 LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
989 struct dpif_upcall *dupcall = &upcall->dpif_upcall;
990 struct flow_miss *miss = &miss_buf[n_misses];
991 struct ofpbuf *packet = &dupcall->packet;
992 struct flow_miss *existing_miss;
993 struct ofproto_dpif *ofproto;
994 struct dpif_sflow *sflow;
995 struct dpif_ipfix *ipfix;
996 odp_port_t odp_in_port;
1000 error = xlate_receive(udpif->backer, packet, dupcall->key,
1001 dupcall->key_len, &flow,
1002 &ofproto, &ipfix, &sflow, NULL, &odp_in_port);
1004 if (error == ENODEV) {
1005 /* Received packet on datapath port for which we couldn't
1006 * associate an ofproto. This can happen if a port is removed
1007 * while traffic is being received. Print a rate-limited
1008 * message in case it happens frequently. Install a drop flow
1009 * so that future packets of the flow are inexpensively dropped
1011 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
1012 "port %"PRIu32, odp_in_port);
1013 dpif_flow_put(udpif->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
1014 dupcall->key, dupcall->key_len, NULL, 0, NULL, 0,
1017 list_remove(&upcall->list_node);
1018 upcall_destroy(upcall);
1022 type = classify_upcall(upcall);
1023 if (type == MISS_UPCALL) {
1025 struct pkt_metadata md = pkt_metadata_from_flow(&flow);
1027 flow_extract(packet, &md, &miss->flow);
1028 hash = flow_hash(&miss->flow, 0);
1029 existing_miss = flow_miss_find(&misses, ofproto, &miss->flow,
1031 if (!existing_miss) {
1032 hmap_insert(&misses, &miss->hmap_node, hash);
1033 miss->ofproto = ofproto;
1034 miss->key = dupcall->key;
1035 miss->key_len = dupcall->key_len;
1036 miss->upcall_type = dupcall->type;
1037 miss->stats.n_packets = 0;
1038 miss->stats.n_bytes = 0;
1039 miss->stats.used = time_msec();
1040 miss->stats.tcp_flags = 0;
1041 miss->odp_in_port = odp_in_port;
1046 miss = existing_miss;
1048 miss->stats.tcp_flags |= ntohs(miss->flow.tcp_flags);
1049 miss->stats.n_bytes += ofpbuf_size(packet);
1050 miss->stats.n_packets++;
1052 upcall->flow_miss = miss;
1059 union user_action_cookie cookie;
1061 memset(&cookie, 0, sizeof cookie);
1062 memcpy(&cookie, nl_attr_get(dupcall->userdata),
1063 sizeof cookie.sflow);
1064 dpif_sflow_received(sflow, packet, &flow, odp_in_port,
1070 dpif_ipfix_bridge_sample(ipfix, packet, &flow);
1073 case FLOW_SAMPLE_UPCALL:
1075 union user_action_cookie cookie;
1077 memset(&cookie, 0, sizeof cookie);
1078 memcpy(&cookie, nl_attr_get(dupcall->userdata),
1079 sizeof cookie.flow_sample);
1081 /* The flow reflects exactly the contents of the packet.
1082 * Sample the packet using it. */
1083 dpif_ipfix_flow_sample(ipfix, packet, &flow,
1084 cookie.flow_sample.collector_set_id,
1085 cookie.flow_sample.probability,
1086 cookie.flow_sample.obs_domain_id,
1087 cookie.flow_sample.obs_point_id);
1096 dpif_ipfix_unref(ipfix);
1097 dpif_sflow_unref(sflow);
1099 list_remove(&upcall->list_node);
1100 upcall_destroy(upcall);
1103 /* Initialize each 'struct flow_miss's ->xout.
1105 * We do this per-flow_miss rather than per-packet because, most commonly,
1106 * all the packets in a flow can use the same translation.
1108 * We can't do this in the previous loop because we need the TCP flags for
1109 * all the packets in each miss. */
1111 HMAP_FOR_EACH (miss, hmap_node, &misses) {
1112 struct xlate_in xin;
1114 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL,
1115 miss->stats.tcp_flags, NULL);
1116 xin.may_learn = true;
1118 if (miss->upcall_type == DPIF_UC_MISS) {
1119 xin.resubmit_stats = &miss->stats;
1121 /* For non-miss upcalls, there's a flow in the datapath which this
1122 * packet was accounted to. Presumably the revalidators will deal
1123 * with pushing its stats eventually. */
1126 xlate_actions(&xin, &miss->xout);
1127 fail_open = fail_open || miss->xout.fail_open;
1130 /* Now handle the packets individually in order of arrival. In the common
1131 * case each packet of a miss can share the same actions, but slow-pathed
1132 * packets need to be translated individually:
1134 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
1135 * processes received packets for these protocols.
1137 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1140 * The loop fills 'ops' with an array of operations to execute in the
1143 LIST_FOR_EACH (upcall, list_node, upcalls) {
1144 struct flow_miss *miss = upcall->flow_miss;
1145 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
1147 ovs_be16 flow_vlan_tci;
1149 /* Save a copy of flow.vlan_tci in case it is changed to
1150 * generate proper mega flow masks for VLAN splinter flows. */
1151 flow_vlan_tci = miss->flow.vlan_tci;
1153 if (miss->xout.slow) {
1154 struct xlate_in xin;
1156 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL, 0, packet);
1157 xlate_actions_for_side_effects(&xin);
1160 if (miss->flow.in_port.ofp_port
1161 != vsp_realdev_to_vlandev(miss->ofproto,
1162 miss->flow.in_port.ofp_port,
1163 miss->flow.vlan_tci)) {
1164 /* This packet was received on a VLAN splinter port. We
1165 * added a VLAN to the packet to make the packet resemble
1166 * the flow, but the actions were composed assuming that
1167 * the packet contained no VLAN. So, we must remove the
1168 * VLAN header from the packet before trying to execute the
1170 if (ofpbuf_size(&miss->xout.odp_actions)) {
1171 eth_pop_vlan(packet);
1174 /* Remove the flow vlan tags inserted by vlan splinter logic
1175 * to ensure megaflow masks generated match the data path flow. */
1176 miss->flow.vlan_tci = 0;
1179 /* Do not install a flow into the datapath if:
1181 * - The datapath already has too many flows.
1183 * - An earlier iteration of this loop already put the same flow.
1185 * - We received this packet via some flow installed in the kernel
1189 && upcall->dpif_upcall.type == DPIF_UC_MISS) {
1195 atomic_read(&enable_megaflows, &megaflow);
1196 ofpbuf_use_stack(&mask, &miss->mask_buf, sizeof miss->mask_buf);
1200 max_mpls = ofproto_dpif_get_max_mpls_depth(miss->ofproto);
1201 odp_flow_key_from_mask(&mask, &miss->xout.wc.masks,
1202 &miss->flow, UINT32_MAX, max_mpls);
1206 op->type = DPIF_OP_FLOW_PUT;
1207 op->u.flow_put.flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1208 op->u.flow_put.key = miss->key;
1209 op->u.flow_put.key_len = miss->key_len;
1210 op->u.flow_put.mask = ofpbuf_data(&mask);
1211 op->u.flow_put.mask_len = ofpbuf_size(&mask);
1212 op->u.flow_put.stats = NULL;
1214 if (!miss->xout.slow) {
1215 op->u.flow_put.actions = ofpbuf_data(&miss->xout.odp_actions);
1216 op->u.flow_put.actions_len = ofpbuf_size(&miss->xout.odp_actions);
1220 ofpbuf_use_stack(&buf, miss->slow_path_buf,
1221 sizeof miss->slow_path_buf);
1222 compose_slow_path(udpif, &miss->xout, miss->odp_in_port, &buf);
1223 op->u.flow_put.actions = ofpbuf_data(&buf);
1224 op->u.flow_put.actions_len = ofpbuf_size(&buf);
1229 * The 'miss' may be shared by multiple upcalls. Restore
1230 * the saved flow vlan_tci field before processing the next
1232 miss->flow.vlan_tci = flow_vlan_tci;
1234 if (ofpbuf_size(&miss->xout.odp_actions)) {
1237 op->type = DPIF_OP_EXECUTE;
1238 op->u.execute.packet = packet;
1239 odp_key_to_pkt_metadata(miss->key, miss->key_len,
1241 op->u.execute.actions = ofpbuf_data(&miss->xout.odp_actions);
1242 op->u.execute.actions_len = ofpbuf_size(&miss->xout.odp_actions);
1243 op->u.execute.needs_help = (miss->xout.slow & SLOW_ACTION) != 0;
1247 /* Special case for fail-open mode.
1249 * If we are in fail-open mode, but we are connected to a controller too,
1250 * then we should send the packet up to the controller in the hope that it
1251 * will try to set up a flow and thereby allow us to exit fail-open.
1253 * See the top-level comment in fail-open.c for more information.
1255 * Copy packets before they are modified by execution. */
1257 LIST_FOR_EACH (upcall, list_node, upcalls) {
1258 struct flow_miss *miss = upcall->flow_miss;
1259 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
1260 struct ofproto_packet_in *pin;
1262 pin = xmalloc(sizeof *pin);
1263 pin->up.packet = xmemdup(ofpbuf_data(packet), ofpbuf_size(packet));
1264 pin->up.packet_len = ofpbuf_size(packet);
1265 pin->up.reason = OFPR_NO_MATCH;
1266 pin->up.table_id = 0;
1267 pin->up.cookie = OVS_BE64_MAX;
1268 flow_get_metadata(&miss->flow, &pin->up.fmd);
1269 pin->send_len = 0; /* Not used for flow table misses. */
1270 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
1271 ofproto_dpif_send_packet_in(miss->ofproto, pin);
1275 /* Execute batch. */
1276 for (i = 0; i < n_ops; i++) {
1279 dpif_operate(udpif->dpif, opsp, n_ops);
1281 HMAP_FOR_EACH_SAFE (miss, next_miss, hmap_node, &misses) {
1282 hmap_remove(&misses, &miss->hmap_node);
1283 xlate_out_uninit(&miss->xout);
1285 hmap_destroy(&misses);
1287 LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
1288 list_remove(&upcall->list_node);
1289 upcall_destroy(upcall);
1293 static struct udpif_key *
1294 ukey_lookup(struct revalidator *revalidator, struct udpif_flow_dump *udump)
1296 struct udpif_key *ukey;
1298 HMAP_FOR_EACH_WITH_HASH (ukey, hmap_node, udump->key_hash,
1299 &revalidator->ukeys) {
1300 if (ukey->key_len == udump->key_len
1301 && !memcmp(ukey->key, udump->key, udump->key_len)) {
1308 static struct udpif_key *
1309 ukey_create(const struct nlattr *key, size_t key_len, long long int used)
1311 struct udpif_key *ukey = xmalloc(sizeof *ukey);
1313 ukey->key = (struct nlattr *) &ukey->key_buf;
1314 memcpy(&ukey->key_buf, key, key_len);
1315 ukey->key_len = key_len;
1318 ukey->created = used ? used : time_msec();
1319 memset(&ukey->stats, 0, sizeof ukey->stats);
1320 ukey->xcache = NULL;
1326 ukey_delete(struct revalidator *revalidator, struct udpif_key *ukey)
1328 hmap_remove(&revalidator->ukeys, &ukey->hmap_node);
1329 xlate_cache_delete(ukey->xcache);
1334 revalidate_ukey(struct udpif *udpif, struct udpif_flow_dump *udump,
1335 struct udpif_key *ukey)
1337 struct ofpbuf xout_actions, *actions;
1338 uint64_t slow_path_buf[128 / 8];
1339 struct xlate_out xout, *xoutp;
1340 struct netflow *netflow;
1341 struct flow flow, udump_mask;
1342 struct ofproto_dpif *ofproto;
1343 struct dpif_flow_stats push;
1344 uint32_t *udump32, *xout32;
1345 odp_port_t odp_in_port;
1346 struct xlate_in xin;
1355 may_learn = push.n_packets > 0;
1357 /* If we don't need to revalidate, we can simply push the stats contained
1358 * in the udump, otherwise we'll have to get the actions so we can check
1360 if (udump->need_revalidate) {
1361 if (dpif_flow_get(udpif->dpif, ukey->key, ukey->key_len, &actions,
1367 push.used = udump->stats.used;
1368 push.tcp_flags = udump->stats.tcp_flags;
1369 push.n_packets = udump->stats.n_packets > ukey->stats.n_packets
1370 ? udump->stats.n_packets - ukey->stats.n_packets
1372 push.n_bytes = udump->stats.n_bytes > ukey->stats.n_bytes
1373 ? udump->stats.n_bytes - ukey->stats.n_bytes
1375 ukey->stats = udump->stats;
1377 if (!push.n_packets && !udump->need_revalidate) {
1382 if (ukey->xcache && !udump->need_revalidate) {
1383 xlate_push_stats(ukey->xcache, may_learn, &push);
1388 error = xlate_receive(udpif->backer, NULL, ukey->key, ukey->key_len, &flow,
1389 &ofproto, NULL, NULL, &netflow, &odp_in_port);
1394 if (udump->need_revalidate) {
1395 xlate_cache_clear(ukey->xcache);
1397 if (!ukey->xcache) {
1398 ukey->xcache = xlate_cache_new();
1401 xlate_in_init(&xin, ofproto, &flow, NULL, push.tcp_flags, NULL);
1402 xin.resubmit_stats = push.n_packets ? &push : NULL;
1403 xin.xcache = ukey->xcache;
1404 xin.may_learn = may_learn;
1405 xin.skip_wildcards = !udump->need_revalidate;
1406 xlate_actions(&xin, &xout);
1409 if (!udump->need_revalidate) {
1415 ofpbuf_use_const(&xout_actions, ofpbuf_data(&xout.odp_actions),
1416 ofpbuf_size(&xout.odp_actions));
1418 ofpbuf_use_stack(&xout_actions, slow_path_buf, sizeof slow_path_buf);
1419 compose_slow_path(udpif, &xout, odp_in_port, &xout_actions);
1422 if (!ofpbuf_equal(&xout_actions, actions)) {
1426 if (odp_flow_key_to_mask(udump->mask, udump->mask_len, &udump_mask, &flow)
1431 /* Since the kernel is free to ignore wildcarded bits in the mask, we can't
1432 * directly check that the masks are the same. Instead we check that the
1433 * mask in the kernel is more specific i.e. less wildcarded, than what
1434 * we've calculated here. This guarantees we don't catch any packets we
1435 * shouldn't with the megaflow. */
1436 udump32 = (uint32_t *) &udump_mask;
1437 xout32 = (uint32_t *) &xout.wc.masks;
1438 for (i = 0; i < FLOW_U32S; i++) {
1439 if ((udump32[i] | xout32[i]) != udump32[i]) {
1448 netflow_expire(netflow, &flow);
1449 netflow_flow_clear(netflow, &flow);
1451 netflow_unref(netflow);
1453 ofpbuf_delete(actions);
1454 xlate_out_uninit(xoutp);
1459 struct udpif_key *ukey;
1460 struct udpif_flow_dump *udump;
1461 struct dpif_flow_stats stats; /* Stats for 'op'. */
1462 struct dpif_op op; /* Flow del operation. */
1466 dump_op_init(struct dump_op *op, const struct nlattr *key, size_t key_len,
1467 struct udpif_key *ukey, struct udpif_flow_dump *udump)
1471 op->op.type = DPIF_OP_FLOW_DEL;
1472 op->op.u.flow_del.key = key;
1473 op->op.u.flow_del.key_len = key_len;
1474 op->op.u.flow_del.stats = &op->stats;
1478 push_dump_ops(struct revalidator *revalidator,
1479 struct dump_op *ops, size_t n_ops)
1481 struct udpif *udpif = revalidator->udpif;
1482 struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
1485 ovs_assert(n_ops <= REVALIDATE_MAX_BATCH);
1486 for (i = 0; i < n_ops; i++) {
1487 opsp[i] = &ops[i].op;
1489 dpif_operate(udpif->dpif, opsp, n_ops);
1491 for (i = 0; i < n_ops; i++) {
1492 struct dump_op *op = &ops[i];
1493 struct dpif_flow_stats *push, *stats, push_buf;
1495 stats = op->op.u.flow_del.stats;
1498 push->used = MAX(stats->used, op->ukey->stats.used);
1499 push->tcp_flags = stats->tcp_flags | op->ukey->stats.tcp_flags;
1500 push->n_packets = stats->n_packets - op->ukey->stats.n_packets;
1501 push->n_bytes = stats->n_bytes - op->ukey->stats.n_bytes;
1506 if (push->n_packets || netflow_exists()) {
1507 struct ofproto_dpif *ofproto;
1508 struct netflow *netflow;
1512 may_learn = push->n_packets > 0;
1513 if (op->ukey && op->ukey->xcache) {
1514 xlate_push_stats(op->ukey->xcache, may_learn, push);
1518 if (!xlate_receive(udpif->backer, NULL, op->op.u.flow_del.key,
1519 op->op.u.flow_del.key_len, &flow, &ofproto,
1520 NULL, NULL, &netflow, NULL)) {
1521 struct xlate_in xin;
1523 xlate_in_init(&xin, ofproto, &flow, NULL, push->tcp_flags,
1525 xin.resubmit_stats = push->n_packets ? push : NULL;
1526 xin.may_learn = may_learn;
1527 xin.skip_wildcards = true;
1528 xlate_actions_for_side_effects(&xin);
1531 netflow_expire(netflow, &flow);
1532 netflow_flow_clear(netflow, &flow);
1533 netflow_unref(netflow);
1539 for (i = 0; i < n_ops; i++) {
1540 struct udpif_key *ukey;
1542 /* If there's a udump, this ukey came directly from a datapath flow
1543 * dump. Sometimes a datapath can send duplicates in flow dumps, in
1544 * which case we wouldn't want to double-free a ukey, so avoid that by
1545 * looking up the ukey again.
1547 * If there's no udump then we know what we're doing. */
1548 ukey = (ops[i].udump
1549 ? ukey_lookup(revalidator, ops[i].udump)
1552 ukey_delete(revalidator, ukey);
1558 revalidate_udumps(struct revalidator *revalidator, struct list *udumps)
1560 struct udpif *udpif = revalidator->udpif;
1562 struct dump_op ops[REVALIDATE_MAX_BATCH];
1563 struct udpif_flow_dump *udump, *next_udump;
1564 size_t n_ops, n_flows;
1565 unsigned int flow_limit;
1566 long long int max_idle;
1569 atomic_read(&udpif->flow_limit, &flow_limit);
1571 n_flows = udpif_get_n_flows(udpif);
1574 max_idle = ofproto_max_idle;
1575 if (n_flows > flow_limit) {
1576 must_del = n_flows > 2 * flow_limit;
1581 LIST_FOR_EACH_SAFE (udump, next_udump, list_node, udumps) {
1582 long long int used, now;
1583 struct udpif_key *ukey;
1586 ukey = ukey_lookup(revalidator, udump);
1588 used = udump->stats.used;
1589 if (!used && ukey) {
1590 used = ukey->created;
1593 if (must_del || (used && used < now - max_idle)) {
1594 struct dump_op *dop = &ops[n_ops++];
1596 dump_op_init(dop, udump->key, udump->key_len, ukey, udump);
1601 ukey = ukey_create(udump->key, udump->key_len, used);
1602 hmap_insert(&revalidator->ukeys, &ukey->hmap_node,
1607 if (!revalidate_ukey(udpif, udump, ukey)) {
1608 dpif_flow_del(udpif->dpif, udump->key, udump->key_len, NULL);
1609 ukey_delete(revalidator, ukey);
1612 list_remove(&udump->list_node);
1616 push_dump_ops(revalidator, ops, n_ops);
1618 LIST_FOR_EACH_SAFE (udump, next_udump, list_node, udumps) {
1619 list_remove(&udump->list_node);
1625 revalidator_sweep__(struct revalidator *revalidator, bool purge)
1627 struct dump_op ops[REVALIDATE_MAX_BATCH];
1628 struct udpif_key *ukey, *next;
1633 HMAP_FOR_EACH_SAFE (ukey, next, hmap_node, &revalidator->ukeys) {
1634 if (!purge && ukey->mark) {
1637 struct dump_op *op = &ops[n_ops++];
1639 /* If we have previously seen a flow in the datapath, but didn't
1640 * see it during the most recent dump, delete it. This allows us
1641 * to clean up the ukey and keep the statistics consistent. */
1642 dump_op_init(op, ukey->key, ukey->key_len, ukey, NULL);
1643 if (n_ops == REVALIDATE_MAX_BATCH) {
1644 push_dump_ops(revalidator, ops, n_ops);
1651 push_dump_ops(revalidator, ops, n_ops);
1656 revalidator_sweep(struct revalidator *revalidator)
1658 revalidator_sweep__(revalidator, false);
1662 revalidator_purge(struct revalidator *revalidator)
1664 revalidator_sweep__(revalidator, true);
1668 upcall_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1669 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
1671 struct ds ds = DS_EMPTY_INITIALIZER;
1672 struct udpif *udpif;
1674 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1675 unsigned int flow_limit;
1678 atomic_read(&udpif->flow_limit, &flow_limit);
1680 ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
1681 ds_put_format(&ds, "\tflows : (current %"PRIu64")"
1682 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
1683 udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
1684 ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
1686 ds_put_char(&ds, '\n');
1687 for (i = 0; i < udpif->n_handlers; i++) {
1688 struct handler *handler = &udpif->handlers[i];
1690 ovs_mutex_lock(&handler->mutex);
1691 ds_put_format(&ds, "\t%s: (upcall queue %"PRIuSIZE")\n",
1692 handler->name, handler->n_upcalls);
1693 ovs_mutex_unlock(&handler->mutex);
1696 ds_put_char(&ds, '\n');
1697 for (i = 0; i < n_revalidators; i++) {
1698 struct revalidator *revalidator = &udpif->revalidators[i];
1700 /* XXX: The result of hmap_count(&revalidator->ukeys) may not be
1701 * accurate because it's not protected by the revalidator mutex. */
1702 ovs_mutex_lock(&revalidator->mutex);
1703 ds_put_format(&ds, "\t%s: (dump queue %"PRIuSIZE") (keys %"PRIuSIZE
1704 ")\n", revalidator->name, revalidator->n_udumps,
1705 hmap_count(&revalidator->ukeys));
1706 ovs_mutex_unlock(&revalidator->mutex);
1710 unixctl_command_reply(conn, ds_cstr(&ds));
1714 /* Disable using the megaflows.
1716 * This command is only needed for advanced debugging, so it's not
1717 * documented in the man page. */
1719 upcall_unixctl_disable_megaflows(struct unixctl_conn *conn,
1720 int argc OVS_UNUSED,
1721 const char *argv[] OVS_UNUSED,
1722 void *aux OVS_UNUSED)
1724 atomic_store(&enable_megaflows, false);
1725 udpif_flush_all_datapaths();
1726 unixctl_command_reply(conn, "megaflows disabled");
1729 /* Re-enable using megaflows.
1731 * This command is only needed for advanced debugging, so it's not
1732 * documented in the man page. */
1734 upcall_unixctl_enable_megaflows(struct unixctl_conn *conn,
1735 int argc OVS_UNUSED,
1736 const char *argv[] OVS_UNUSED,
1737 void *aux OVS_UNUSED)
1739 atomic_store(&enable_megaflows, true);
1740 udpif_flush_all_datapaths();
1741 unixctl_command_reply(conn, "megaflows enabled");
1744 /* Set the flow limit.
1746 * This command is only needed for advanced debugging, so it's not
1747 * documented in the man page. */
1749 upcall_unixctl_set_flow_limit(struct unixctl_conn *conn,
1750 int argc OVS_UNUSED,
1751 const char *argv[] OVS_UNUSED,
1752 void *aux OVS_UNUSED)
1754 struct ds ds = DS_EMPTY_INITIALIZER;
1755 struct udpif *udpif;
1756 unsigned int flow_limit = atoi(argv[1]);
1758 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1759 atomic_store(&udpif->flow_limit, flow_limit);
1761 ds_put_format(&ds, "set flow_limit to %u\n", flow_limit);
1762 unixctl_command_reply(conn, ds_cstr(&ds));