1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License. */
16 #include "ofproto-dpif-upcall.h"
25 #include "dynamic-string.h"
26 #include "fail-open.h"
27 #include "guarded-list.h"
32 #include "ofproto-dpif-ipfix.h"
33 #include "ofproto-dpif-sflow.h"
34 #include "ofproto-dpif-xlate.h"
37 #include "poll-loop.h"
42 #define MAX_QUEUE_LENGTH 512
43 #define FLOW_MISS_MAX_BATCH 50
44 #define REVALIDATE_MAX_BATCH 50
46 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall);
48 COVERAGE_DEFINE(upcall_duplicate_flow);
50 /* A thread that reads upcalls from dpif, forwards each upcall's packet,
51 * and possibly sets up a kernel flow as a cache. */
53 struct udpif *udpif; /* Parent udpif. */
54 pthread_t thread; /* Thread ID. */
55 char *name; /* Thread name. */
56 uint32_t handler_id; /* Handler id. */
59 /* A thread that processes datapath flows, updates OpenFlow statistics, and
60 * updates or removes them if necessary. */
62 struct udpif *udpif; /* Parent udpif. */
63 char *name; /* Thread name. */
65 pthread_t thread; /* Thread ID. */
66 struct hmap *ukeys; /* Points into udpif->ukeys for this
67 revalidator. Used for GC phase. */
70 /* An upcall handler for ofproto_dpif.
72 * udpif keeps records of two kind of logically separate units:
77 * - An array of 'struct handler's for upcall handling and flow
83 * - Revalidation threads which read the datapath flow table and maintains
87 struct list list_node; /* In all_udpifs list. */
89 struct dpif *dpif; /* Datapath handle. */
90 struct dpif_backer *backer; /* Opaque dpif_backer pointer. */
92 uint32_t secret; /* Random seed for upcall hash. */
94 struct handler *handlers; /* Upcall handlers. */
97 struct revalidator *revalidators; /* Flow revalidators. */
98 size_t n_revalidators;
100 struct latch exit_latch; /* Tells child threads to exit. */
103 struct seq *reval_seq; /* Incremented to force revalidation. */
104 bool need_revalidate; /* As indicated by 'reval_seq'. */
105 bool reval_exit; /* Set by leader on 'exit_latch. */
106 pthread_barrier_t reval_barrier; /* Barrier used by revalidators. */
107 struct dpif_flow_dump dump; /* DPIF flow dump state. */
108 long long int dump_duration; /* Duration of the last flow dump. */
109 struct seq *dump_seq; /* Increments each dump iteration. */
111 /* There are 'n_revalidators' ukey hmaps. Each revalidator retains a
112 * reference to one of these for garbage collection.
114 * During the flow dump phase, revalidators insert into these with a random
115 * distribution. During the garbage collection phase, each revalidator
116 * takes care of garbage collecting one of these hmaps. */
118 struct ovs_mutex mutex; /* Guards the following. */
119 struct hmap hmap OVS_GUARDED; /* Datapath flow keys. */
122 /* Datapath flow statistics. */
123 unsigned int max_n_flows;
124 unsigned int avg_n_flows;
126 /* Following fields are accessed and modified by different threads. */
127 atomic_uint flow_limit; /* Datapath flow hard limit. */
129 /* n_flows_mutex prevents multiple threads updating these concurrently. */
130 atomic_uint64_t n_flows; /* Number of flows in the datapath. */
131 atomic_llong n_flows_timestamp; /* Last time n_flows was updated. */
132 struct ovs_mutex n_flows_mutex;
136 BAD_UPCALL, /* Some kind of bug somewhere. */
137 MISS_UPCALL, /* A flow miss. */
138 SFLOW_UPCALL, /* sFlow sample. */
139 FLOW_SAMPLE_UPCALL, /* Per-flow sampling. */
140 IPFIX_UPCALL /* Per-bridge sampling. */
144 struct flow_miss *flow_miss; /* This upcall's flow_miss. */
146 /* Raw upcall plus data for keeping track of the memory backing it. */
147 struct dpif_upcall dpif_upcall; /* As returned by dpif_recv() */
148 struct ofpbuf upcall_buf; /* Owns some data in 'dpif_upcall'. */
149 uint64_t upcall_stub[512 / 8]; /* Buffer to reduce need for malloc(). */
152 /* 'udpif_key's are responsible for tracking the little bit of state udpif
153 * needs to do flow expiration which can't be pulled directly from the
154 * datapath. They may be created or maintained by any revalidator during
155 * the dump phase, but are owned by a single revalidator, and are destroyed
156 * by that revalidator during the garbage-collection phase.
158 * While some elements of a udpif_key are protected by a mutex, the ukey itself
159 * is not. Therefore it is not safe to destroy a udpif_key except when all
160 * revalidators are in garbage collection phase, or they aren't running. */
162 struct hmap_node hmap_node; /* In parent revalidator 'ukeys' map. */
164 /* These elements are read only once created, and therefore aren't
165 * protected by a mutex. */
166 const struct nlattr *key; /* Datapath flow key. */
167 size_t key_len; /* Length of 'key'. */
169 struct ovs_mutex mutex; /* Guards the following. */
170 struct dpif_flow_stats stats OVS_GUARDED; /* Last known stats.*/
171 long long int created OVS_GUARDED; /* Estimate of creation time. */
172 bool mark OVS_GUARDED; /* For mark and sweep garbage
174 bool flow_exists OVS_GUARDED; /* Ensures flows are only deleted
177 struct xlate_cache *xcache OVS_GUARDED; /* Cache for xlate entries that
178 * are affected by this ukey.
179 * Used for stats and learning.*/
180 struct odputil_keybuf key_buf; /* Memory for 'key'. */
183 /* Flow miss batching.
185 * Some dpifs implement operations faster when you hand them off in a batch.
186 * To allow batching, "struct flow_miss" queues the dpif-related work needed
187 * for a given flow. Each "struct flow_miss" corresponds to sending one or
188 * more packets, plus possibly installing the flow in the dpif. */
190 struct hmap_node hmap_node;
191 struct ofproto_dpif *ofproto;
194 const struct nlattr *key;
196 enum dpif_upcall_type upcall_type;
197 struct dpif_flow_stats stats;
198 odp_port_t odp_in_port;
200 uint64_t slow_path_buf[128 / 8];
201 struct odputil_keybuf mask_buf;
203 struct xlate_out xout;
208 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
209 static struct list all_udpifs = LIST_INITIALIZER(&all_udpifs);
211 static size_t read_upcalls(struct handler *,
212 struct upcall upcalls[FLOW_MISS_MAX_BATCH],
213 struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH],
215 static void handle_upcalls(struct handler *, struct hmap *, struct upcall *,
217 static void udpif_stop_threads(struct udpif *);
218 static void udpif_start_threads(struct udpif *, size_t n_handlers,
219 size_t n_revalidators);
220 static void *udpif_upcall_handler(void *);
221 static void *udpif_revalidator(void *);
222 static uint64_t udpif_get_n_flows(struct udpif *);
223 static void revalidate(struct revalidator *);
224 static void revalidator_sweep(struct revalidator *);
225 static void revalidator_purge(struct revalidator *);
226 static void upcall_unixctl_show(struct unixctl_conn *conn, int argc,
227 const char *argv[], void *aux);
228 static void upcall_unixctl_disable_megaflows(struct unixctl_conn *, int argc,
229 const char *argv[], void *aux);
230 static void upcall_unixctl_enable_megaflows(struct unixctl_conn *, int argc,
231 const char *argv[], void *aux);
232 static void upcall_unixctl_set_flow_limit(struct unixctl_conn *conn, int argc,
233 const char *argv[], void *aux);
235 static struct udpif_key *ukey_create(const struct nlattr *key, size_t key_len,
237 static void ukey_delete(struct revalidator *, struct udpif_key *);
239 static atomic_bool enable_megaflows = ATOMIC_VAR_INIT(true);
242 udpif_create(struct dpif_backer *backer, struct dpif *dpif)
244 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
245 struct udpif *udpif = xzalloc(sizeof *udpif);
247 if (ovsthread_once_start(&once)) {
248 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show,
250 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
251 upcall_unixctl_disable_megaflows, NULL);
252 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
253 upcall_unixctl_enable_megaflows, NULL);
254 unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
255 upcall_unixctl_set_flow_limit, NULL);
256 ovsthread_once_done(&once);
260 udpif->backer = backer;
261 atomic_init(&udpif->flow_limit, MIN(ofproto_flow_limit, 10000));
262 udpif->secret = random_uint32();
263 udpif->reval_seq = seq_create();
264 udpif->dump_seq = seq_create();
265 latch_init(&udpif->exit_latch);
266 list_push_back(&all_udpifs, &udpif->list_node);
267 atomic_init(&udpif->n_flows, 0);
268 atomic_init(&udpif->n_flows_timestamp, LLONG_MIN);
269 ovs_mutex_init(&udpif->n_flows_mutex);
275 udpif_destroy(struct udpif *udpif)
277 udpif_stop_threads(udpif);
279 list_remove(&udpif->list_node);
280 latch_destroy(&udpif->exit_latch);
281 seq_destroy(udpif->reval_seq);
282 seq_destroy(udpif->dump_seq);
283 ovs_mutex_destroy(&udpif->n_flows_mutex);
287 /* Stops the handler and revalidator threads, must be enclosed in
288 * ovsrcu quiescent state unless when destroying udpif. */
290 udpif_stop_threads(struct udpif *udpif)
292 if (udpif && (udpif->n_handlers != 0 || udpif->n_revalidators != 0)) {
295 latch_set(&udpif->exit_latch);
297 for (i = 0; i < udpif->n_handlers; i++) {
298 struct handler *handler = &udpif->handlers[i];
300 xpthread_join(handler->thread, NULL);
303 for (i = 0; i < udpif->n_revalidators; i++) {
304 xpthread_join(udpif->revalidators[i].thread, NULL);
307 for (i = 0; i < udpif->n_revalidators; i++) {
308 struct revalidator *revalidator = &udpif->revalidators[i];
310 /* Delete ukeys, and delete all flows from the datapath to prevent
311 * double-counting stats. */
312 revalidator_purge(revalidator);
313 free(revalidator->name);
315 hmap_destroy(&udpif->ukeys[i].hmap);
316 ovs_mutex_destroy(&udpif->ukeys[i].mutex);
319 for (i = 0; i < udpif->n_handlers; i++) {
320 free(udpif->handlers[i].name);
322 latch_poll(&udpif->exit_latch);
324 xpthread_barrier_destroy(&udpif->reval_barrier);
326 free(udpif->revalidators);
327 udpif->revalidators = NULL;
328 udpif->n_revalidators = 0;
330 free(udpif->handlers);
331 udpif->handlers = NULL;
332 udpif->n_handlers = 0;
339 /* Starts the handler and revalidator threads, must be enclosed in
340 * ovsrcu quiescent state. */
342 udpif_start_threads(struct udpif *udpif, size_t n_handlers,
343 size_t n_revalidators)
345 if (udpif && n_handlers && n_revalidators) {
348 udpif->n_handlers = n_handlers;
349 udpif->n_revalidators = n_revalidators;
351 udpif->handlers = xzalloc(udpif->n_handlers * sizeof *udpif->handlers);
352 for (i = 0; i < udpif->n_handlers; i++) {
353 struct handler *handler = &udpif->handlers[i];
355 handler->udpif = udpif;
356 handler->handler_id = i;
357 xpthread_create(&handler->thread, NULL, udpif_upcall_handler,
361 xpthread_barrier_init(&udpif->reval_barrier, NULL,
362 udpif->n_revalidators);
363 udpif->reval_exit = false;
364 udpif->revalidators = xzalloc(udpif->n_revalidators
365 * sizeof *udpif->revalidators);
366 udpif->ukeys = xmalloc(sizeof *udpif->ukeys * n_revalidators);
367 for (i = 0; i < udpif->n_revalidators; i++) {
368 struct revalidator *revalidator = &udpif->revalidators[i];
370 revalidator->udpif = udpif;
371 hmap_init(&udpif->ukeys[i].hmap);
372 ovs_mutex_init(&udpif->ukeys[i].mutex);
373 revalidator->ukeys = &udpif->ukeys[i].hmap;
374 xpthread_create(&revalidator->thread, NULL, udpif_revalidator,
380 /* Tells 'udpif' how many threads it should use to handle upcalls.
381 * 'n_handlers' and 'n_revalidators' can never be zero. 'udpif''s
382 * datapath handle must have packet reception enabled before starting
385 udpif_set_threads(struct udpif *udpif, size_t n_handlers,
386 size_t n_revalidators)
391 ovs_assert(n_handlers && n_revalidators);
393 ovsrcu_quiesce_start();
394 if (udpif->n_handlers != n_handlers
395 || udpif->n_revalidators != n_revalidators) {
396 udpif_stop_threads(udpif);
399 error = dpif_handlers_set(udpif->dpif, n_handlers);
401 VLOG_ERR("failed to configure handlers in dpif %s: %s",
402 dpif_name(udpif->dpif), ovs_strerror(error));
406 if (!udpif->handlers && !udpif->revalidators) {
407 udpif_start_threads(udpif, n_handlers, n_revalidators);
409 ovsrcu_quiesce_end();
412 /* Waits for all ongoing upcall translations to complete. This ensures that
413 * there are no transient references to any removed ofprotos (or other
414 * objects). In particular, this should be called after an ofproto is removed
415 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
417 udpif_synchronize(struct udpif *udpif)
419 /* This is stronger than necessary. It would be sufficient to ensure
420 * (somehow) that each handler and revalidator thread had passed through
421 * its main loop once. */
422 size_t n_handlers = udpif->n_handlers;
423 size_t n_revalidators = udpif->n_revalidators;
425 ovsrcu_quiesce_start();
426 udpif_stop_threads(udpif);
427 udpif_start_threads(udpif, n_handlers, n_revalidators);
428 ovsrcu_quiesce_end();
431 /* Notifies 'udpif' that something changed which may render previous
432 * xlate_actions() results invalid. */
434 udpif_revalidate(struct udpif *udpif)
436 seq_change(udpif->reval_seq);
439 /* Returns a seq which increments every time 'udpif' pulls stats from the
440 * datapath. Callers can use this to get a sense of when might be a good time
441 * to do periodic work which relies on relatively up to date statistics. */
443 udpif_dump_seq(struct udpif *udpif)
445 return udpif->dump_seq;
449 udpif_get_memory_usage(struct udpif *udpif, struct simap *usage)
453 simap_increase(usage, "handlers", udpif->n_handlers);
455 simap_increase(usage, "revalidators", udpif->n_revalidators);
456 for (i = 0; i < udpif->n_revalidators; i++) {
457 ovs_mutex_lock(&udpif->ukeys[i].mutex);
458 simap_increase(usage, "udpif keys", hmap_count(&udpif->ukeys[i].hmap));
459 ovs_mutex_unlock(&udpif->ukeys[i].mutex);
463 /* Remove flows from a single datapath. */
465 udpif_flush(struct udpif *udpif)
467 size_t n_handlers, n_revalidators;
469 n_handlers = udpif->n_handlers;
470 n_revalidators = udpif->n_revalidators;
472 ovsrcu_quiesce_start();
474 udpif_stop_threads(udpif);
475 dpif_flow_flush(udpif->dpif);
476 udpif_start_threads(udpif, n_handlers, n_revalidators);
478 ovsrcu_quiesce_end();
481 /* Removes all flows from all datapaths. */
483 udpif_flush_all_datapaths(void)
487 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
494 udpif_get_n_flows(struct udpif *udpif)
496 long long int time, now;
500 atomic_read(&udpif->n_flows_timestamp, &time);
501 if (time < now - 100 && !ovs_mutex_trylock(&udpif->n_flows_mutex)) {
502 struct dpif_dp_stats stats;
504 atomic_store(&udpif->n_flows_timestamp, now);
505 dpif_get_dp_stats(udpif->dpif, &stats);
506 flow_count = stats.n_flows;
507 atomic_store(&udpif->n_flows, flow_count);
508 ovs_mutex_unlock(&udpif->n_flows_mutex);
510 atomic_read(&udpif->n_flows, &flow_count);
515 /* The upcall handler thread tries to read a batch of FLOW_MISS_MAX_BATCH
516 * upcalls from dpif, processes the batch and installs corresponding flows
519 udpif_upcall_handler(void *arg)
521 struct handler *handler = arg;
522 struct udpif *udpif = handler->udpif;
523 struct hmap misses = HMAP_INITIALIZER(&misses);
525 handler->name = xasprintf("handler_%u", ovsthread_id_self());
526 set_subprogram_name("%s", handler->name);
528 while (!latch_is_set(&handler->udpif->exit_latch)) {
529 struct upcall upcalls[FLOW_MISS_MAX_BATCH];
530 struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH];
531 struct flow_miss *miss;
534 n_upcalls = read_upcalls(handler, upcalls, miss_buf, &misses);
536 dpif_recv_wait(udpif->dpif, handler->handler_id);
537 latch_wait(&udpif->exit_latch);
540 handle_upcalls(handler, &misses, upcalls, n_upcalls);
542 HMAP_FOR_EACH (miss, hmap_node, &misses) {
543 xlate_out_uninit(&miss->xout);
546 for (i = 0; i < n_upcalls; i++) {
547 ofpbuf_uninit(&upcalls[i].dpif_upcall.packet);
548 ofpbuf_uninit(&upcalls[i].upcall_buf);
553 hmap_destroy(&misses);
559 udpif_revalidator(void *arg)
561 /* Used by all revalidators. */
562 struct revalidator *revalidator = arg;
563 struct udpif *udpif = revalidator->udpif;
564 bool leader = revalidator == &udpif->revalidators[0];
566 /* Used only by the leader. */
567 long long int start_time = 0;
568 uint64_t last_reval_seq = 0;
569 unsigned int flow_limit = 0;
572 revalidator->name = xasprintf("revalidator_%u", ovsthread_id_self());
573 set_subprogram_name("%s", revalidator->name);
578 reval_seq = seq_read(udpif->reval_seq);
579 udpif->need_revalidate = last_reval_seq != reval_seq;
580 last_reval_seq = reval_seq;
582 n_flows = udpif_get_n_flows(udpif);
583 udpif->max_n_flows = MAX(n_flows, udpif->max_n_flows);
584 udpif->avg_n_flows = (udpif->avg_n_flows + n_flows) / 2;
586 /* Only the leader checks the exit latch to prevent a race where
587 * some threads think it's true and exit and others think it's
588 * false and block indefinitely on the reval_barrier */
589 udpif->reval_exit = latch_is_set(&udpif->exit_latch);
591 start_time = time_msec();
592 if (!udpif->reval_exit) {
593 dpif_flow_dump_start(&udpif->dump, udpif->dpif);
597 /* Wait for the leader to start the flow dump. */
598 xpthread_barrier_wait(&udpif->reval_barrier);
599 if (udpif->reval_exit) {
602 revalidate(revalidator);
604 /* Wait for all flows to have been dumped before we garbage collect. */
605 xpthread_barrier_wait(&udpif->reval_barrier);
606 revalidator_sweep(revalidator);
608 /* Wait for all revalidators to finish garbage collection. */
609 xpthread_barrier_wait(&udpif->reval_barrier);
612 long long int duration;
614 dpif_flow_dump_done(&udpif->dump);
615 seq_change(udpif->dump_seq);
617 duration = MAX(time_msec() - start_time, 1);
618 atomic_read(&udpif->flow_limit, &flow_limit);
619 udpif->dump_duration = duration;
620 if (duration > 2000) {
621 flow_limit /= duration / 1000;
622 } else if (duration > 1300) {
623 flow_limit = flow_limit * 3 / 4;
624 } else if (duration < 1000 && n_flows > 2000
625 && flow_limit < n_flows * 1000 / duration) {
628 flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
629 atomic_store(&udpif->flow_limit, flow_limit);
631 if (duration > 2000) {
632 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
636 poll_timer_wait_until(start_time + MIN(ofproto_max_idle, 500));
637 seq_wait(udpif->reval_seq, last_reval_seq);
638 latch_wait(&udpif->exit_latch);
646 static enum upcall_type
647 classify_upcall(const struct upcall *upcall)
649 const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
650 union user_action_cookie cookie;
653 /* First look at the upcall type. */
654 switch (dpif_upcall->type) {
661 case DPIF_N_UC_TYPES:
663 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
668 /* "action" upcalls need a closer look. */
669 if (!dpif_upcall->userdata) {
670 VLOG_WARN_RL(&rl, "action upcall missing cookie");
673 userdata_len = nl_attr_get_size(dpif_upcall->userdata);
674 if (userdata_len < sizeof cookie.type
675 || userdata_len > sizeof cookie) {
676 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
680 memset(&cookie, 0, sizeof cookie);
681 memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
682 if (userdata_len == MAX(8, sizeof cookie.sflow)
683 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
685 } else if (userdata_len == MAX(8, sizeof cookie.slow_path)
686 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
688 } else if (userdata_len == MAX(8, sizeof cookie.flow_sample)
689 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
690 return FLOW_SAMPLE_UPCALL;
691 } else if (userdata_len == MAX(8, sizeof cookie.ipfix)
692 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
695 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
696 " and size %"PRIuSIZE, cookie.type, userdata_len);
701 /* Calculates slow path actions for 'xout'. 'buf' must statically be
702 * initialized with at least 128 bytes of space. */
704 compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
705 struct flow *flow, odp_port_t odp_in_port,
708 union user_action_cookie cookie;
712 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
713 cookie.slow_path.unused = 0;
714 cookie.slow_path.reason = xout->slow;
716 port = xout->slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)
719 pid = dpif_port_get_pid(udpif->dpif, port, flow_hash_5tuple(flow, 0));
720 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, buf);
723 static struct flow_miss *
724 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
725 const struct flow *flow, uint32_t hash)
727 struct flow_miss *miss;
729 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
730 if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
738 /* Reads and classifies upcalls. Returns the number of upcalls successfully
741 read_upcalls(struct handler *handler,
742 struct upcall upcalls[FLOW_MISS_MAX_BATCH],
743 struct flow_miss miss_buf[FLOW_MISS_MAX_BATCH],
746 struct udpif *udpif = handler->udpif;
749 size_t n_upcalls = 0;
752 * Try reading FLOW_MISS_MAX_BATCH upcalls from dpif.
754 * Extract the flow from each upcall. Construct in 'misses' a hash table
755 * that maps each unique flow to a 'struct flow_miss'.
757 * Most commonly there is a single packet per flow_miss, but there are
758 * several reasons why there might be more than one, e.g.:
760 * - The dpif packet interface does not support TSO (or UFO, etc.), so a
761 * large packet sent to userspace is split into a sequence of smaller
764 * - A stream of quickly arriving packets in an established "slow-pathed"
767 * - Rarely, a stream of quickly arriving packets in a flow not yet
768 * established. (This is rare because most protocols do not send
769 * multiple back-to-back packets before receiving a reply from the
770 * other end of the connection, which gives OVS a chance to set up a
773 for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
774 struct upcall *upcall = &upcalls[n_upcalls];
775 struct flow_miss *miss = &miss_buf[n_misses];
776 struct dpif_upcall *dupcall;
777 struct ofpbuf *packet;
778 struct flow_miss *existing_miss;
779 struct ofproto_dpif *ofproto;
780 struct dpif_sflow *sflow;
781 struct dpif_ipfix *ipfix;
783 enum upcall_type type;
784 odp_port_t odp_in_port;
787 ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
788 sizeof upcall->upcall_stub);
789 error = dpif_recv(udpif->dpif, handler->handler_id,
790 &upcall->dpif_upcall, &upcall->upcall_buf);
792 ofpbuf_uninit(&upcall->upcall_buf);
796 dupcall = &upcall->dpif_upcall;
797 packet = &dupcall->packet;
798 error = xlate_receive(udpif->backer, packet, dupcall->key,
799 dupcall->key_len, &flow,
800 &ofproto, &ipfix, &sflow, NULL, &odp_in_port);
802 if (error == ENODEV) {
803 /* Received packet on datapath port for which we couldn't
804 * associate an ofproto. This can happen if a port is removed
805 * while traffic is being received. Print a rate-limited
806 * message in case it happens frequently. Install a drop flow
807 * so that future packets of the flow are inexpensively dropped
809 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
810 "port %"PRIu32, odp_in_port);
811 dpif_flow_put(udpif->dpif, DPIF_FP_CREATE | DPIF_FP_MODIFY,
812 dupcall->key, dupcall->key_len, NULL, 0, NULL, 0,
818 type = classify_upcall(upcall);
819 if (type == MISS_UPCALL) {
821 struct pkt_metadata md = pkt_metadata_from_flow(&flow);
823 flow_extract(packet, &md, &miss->flow);
824 hash = flow_hash(&miss->flow, 0);
825 existing_miss = flow_miss_find(misses, ofproto, &miss->flow,
827 if (!existing_miss) {
828 hmap_insert(misses, &miss->hmap_node, hash);
829 miss->ofproto = ofproto;
830 miss->key = dupcall->key;
831 miss->key_len = dupcall->key_len;
832 miss->upcall_type = dupcall->type;
833 miss->stats.n_packets = 0;
834 miss->stats.n_bytes = 0;
835 miss->stats.used = time_msec();
836 miss->stats.tcp_flags = 0;
837 miss->odp_in_port = odp_in_port;
841 miss = existing_miss;
843 miss->stats.tcp_flags |= ntohs(miss->flow.tcp_flags);
844 miss->stats.n_bytes += ofpbuf_size(packet);
845 miss->stats.n_packets++;
847 upcall->flow_miss = miss;
855 union user_action_cookie cookie;
857 memset(&cookie, 0, sizeof cookie);
858 memcpy(&cookie, nl_attr_get(dupcall->userdata),
859 sizeof cookie.sflow);
860 dpif_sflow_received(sflow, packet, &flow, odp_in_port,
866 dpif_ipfix_bridge_sample(ipfix, packet, &flow);
869 case FLOW_SAMPLE_UPCALL:
871 union user_action_cookie cookie;
873 memset(&cookie, 0, sizeof cookie);
874 memcpy(&cookie, nl_attr_get(dupcall->userdata),
875 sizeof cookie.flow_sample);
877 /* The flow reflects exactly the contents of the packet.
878 * Sample the packet using it. */
879 dpif_ipfix_flow_sample(ipfix, packet, &flow,
880 cookie.flow_sample.collector_set_id,
881 cookie.flow_sample.probability,
882 cookie.flow_sample.obs_domain_id,
883 cookie.flow_sample.obs_point_id);
892 dpif_ipfix_unref(ipfix);
893 dpif_sflow_unref(sflow);
896 ofpbuf_uninit(&upcall->dpif_upcall.packet);
897 ofpbuf_uninit(&upcall->upcall_buf);
904 handle_upcalls(struct handler *handler, struct hmap *misses,
905 struct upcall *upcalls, size_t n_upcalls)
907 struct udpif *udpif = handler->udpif;
908 struct dpif_op *opsp[FLOW_MISS_MAX_BATCH * 2];
909 struct dpif_op ops[FLOW_MISS_MAX_BATCH * 2];
910 struct flow_miss *miss;
912 unsigned int flow_limit;
913 bool fail_open, may_put;
915 atomic_read(&udpif->flow_limit, &flow_limit);
916 may_put = udpif_get_n_flows(udpif) < flow_limit;
918 /* Initialize each 'struct flow_miss's ->xout.
920 * We do this per-flow_miss rather than per-packet because, most commonly,
921 * all the packets in a flow can use the same translation.
923 * We can't do this in the previous loop because we need the TCP flags for
924 * all the packets in each miss. */
926 HMAP_FOR_EACH (miss, hmap_node, misses) {
929 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL,
930 miss->stats.tcp_flags, NULL);
931 xin.may_learn = true;
933 if (miss->upcall_type == DPIF_UC_MISS) {
934 xin.resubmit_stats = &miss->stats;
936 /* For non-miss upcalls, there's a flow in the datapath which this
937 * packet was accounted to. Presumably the revalidators will deal
938 * with pushing its stats eventually. */
941 xlate_actions(&xin, &miss->xout);
942 fail_open = fail_open || miss->xout.fail_open;
945 /* Now handle the packets individually in order of arrival. In the common
946 * case each packet of a miss can share the same actions, but slow-pathed
947 * packets need to be translated individually:
949 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
950 * processes received packets for these protocols.
952 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
955 * The loop fills 'ops' with an array of operations to execute in the
958 for (i = 0; i < n_upcalls; i++) {
959 struct upcall *upcall = &upcalls[i];
960 struct flow_miss *miss = upcall->flow_miss;
961 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
963 ovs_be16 flow_vlan_tci;
965 /* Save a copy of flow.vlan_tci in case it is changed to
966 * generate proper mega flow masks for VLAN splinter flows. */
967 flow_vlan_tci = miss->flow.vlan_tci;
969 if (miss->xout.slow) {
972 xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL, 0, packet);
973 xlate_actions_for_side_effects(&xin);
976 if (miss->flow.in_port.ofp_port
977 != vsp_realdev_to_vlandev(miss->ofproto,
978 miss->flow.in_port.ofp_port,
979 miss->flow.vlan_tci)) {
980 /* This packet was received on a VLAN splinter port. We
981 * added a VLAN to the packet to make the packet resemble
982 * the flow, but the actions were composed assuming that
983 * the packet contained no VLAN. So, we must remove the
984 * VLAN header from the packet before trying to execute the
986 if (ofpbuf_size(&miss->xout.odp_actions)) {
987 eth_pop_vlan(packet);
990 /* Remove the flow vlan tags inserted by vlan splinter logic
991 * to ensure megaflow masks generated match the data path flow. */
992 miss->flow.vlan_tci = 0;
995 /* Do not install a flow into the datapath if:
997 * - The datapath already has too many flows.
999 * - An earlier iteration of this loop already put the same flow.
1001 * - We received this packet via some flow installed in the kernel
1005 && upcall->dpif_upcall.type == DPIF_UC_MISS) {
1011 atomic_read(&enable_megaflows, &megaflow);
1012 ofpbuf_use_stack(&mask, &miss->mask_buf, sizeof miss->mask_buf);
1016 max_mpls = ofproto_dpif_get_max_mpls_depth(miss->ofproto);
1017 odp_flow_key_from_mask(&mask, &miss->xout.wc.masks,
1018 &miss->flow, UINT32_MAX, max_mpls);
1022 op->type = DPIF_OP_FLOW_PUT;
1023 op->u.flow_put.flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
1024 op->u.flow_put.key = miss->key;
1025 op->u.flow_put.key_len = miss->key_len;
1026 op->u.flow_put.mask = ofpbuf_data(&mask);
1027 op->u.flow_put.mask_len = ofpbuf_size(&mask);
1028 op->u.flow_put.stats = NULL;
1030 if (!miss->xout.slow) {
1031 op->u.flow_put.actions = ofpbuf_data(&miss->xout.odp_actions);
1032 op->u.flow_put.actions_len = ofpbuf_size(&miss->xout.odp_actions);
1036 ofpbuf_use_stack(&buf, miss->slow_path_buf,
1037 sizeof miss->slow_path_buf);
1038 compose_slow_path(udpif, &miss->xout, &miss->flow,
1039 miss->odp_in_port, &buf);
1040 op->u.flow_put.actions = ofpbuf_data(&buf);
1041 op->u.flow_put.actions_len = ofpbuf_size(&buf);
1046 * The 'miss' may be shared by multiple upcalls. Restore
1047 * the saved flow vlan_tci field before processing the next
1049 miss->flow.vlan_tci = flow_vlan_tci;
1051 if (ofpbuf_size(&miss->xout.odp_actions)) {
1054 op->type = DPIF_OP_EXECUTE;
1055 op->u.execute.packet = packet;
1056 odp_key_to_pkt_metadata(miss->key, miss->key_len,
1058 op->u.execute.actions = ofpbuf_data(&miss->xout.odp_actions);
1059 op->u.execute.actions_len = ofpbuf_size(&miss->xout.odp_actions);
1060 op->u.execute.needs_help = (miss->xout.slow & SLOW_ACTION) != 0;
1064 /* Special case for fail-open mode.
1066 * If we are in fail-open mode, but we are connected to a controller too,
1067 * then we should send the packet up to the controller in the hope that it
1068 * will try to set up a flow and thereby allow us to exit fail-open.
1070 * See the top-level comment in fail-open.c for more information.
1072 * Copy packets before they are modified by execution. */
1074 for (i = 0; i < n_upcalls; i++) {
1075 struct upcall *upcall = &upcalls[i];
1076 struct flow_miss *miss = upcall->flow_miss;
1077 struct ofpbuf *packet = &upcall->dpif_upcall.packet;
1078 struct ofproto_packet_in *pin;
1080 pin = xmalloc(sizeof *pin);
1081 pin->up.packet = xmemdup(ofpbuf_data(packet), ofpbuf_size(packet));
1082 pin->up.packet_len = ofpbuf_size(packet);
1083 pin->up.reason = OFPR_NO_MATCH;
1084 pin->up.table_id = 0;
1085 pin->up.cookie = OVS_BE64_MAX;
1086 flow_get_metadata(&miss->flow, &pin->up.fmd);
1087 pin->send_len = 0; /* Not used for flow table misses. */
1088 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
1089 ofproto_dpif_send_packet_in(miss->ofproto, pin);
1093 /* Execute batch. */
1094 for (i = 0; i < n_ops; i++) {
1097 dpif_operate(udpif->dpif, opsp, n_ops);
1100 /* Must be called with udpif->ukeys[hash % udpif->n_revalidators].mutex. */
1101 static struct udpif_key *
1102 ukey_lookup__(struct udpif *udpif, const struct nlattr *key, size_t key_len,
1105 struct udpif_key *ukey;
1106 struct hmap *hmap = &udpif->ukeys[hash % udpif->n_revalidators].hmap;
1108 HMAP_FOR_EACH_WITH_HASH (ukey, hmap_node, hash, hmap) {
1109 if (ukey->key_len == key_len && !memcmp(ukey->key, key, key_len)) {
1116 static struct udpif_key *
1117 ukey_lookup(struct udpif *udpif, const struct nlattr *key, size_t key_len,
1120 struct udpif_key *ukey;
1121 uint32_t idx = hash % udpif->n_revalidators;
1123 ovs_mutex_lock(&udpif->ukeys[idx].mutex);
1124 ukey = ukey_lookup__(udpif, key, key_len, hash);
1125 ovs_mutex_unlock(&udpif->ukeys[idx].mutex);
1130 static struct udpif_key *
1131 ukey_create(const struct nlattr *key, size_t key_len, long long int used)
1133 struct udpif_key *ukey = xmalloc(sizeof *ukey);
1134 ovs_mutex_init(&ukey->mutex);
1136 ukey->key = (struct nlattr *) &ukey->key_buf;
1137 memcpy(&ukey->key_buf, key, key_len);
1138 ukey->key_len = key_len;
1140 ovs_mutex_lock(&ukey->mutex);
1142 ukey->flow_exists = true;
1143 ukey->created = used ? used : time_msec();
1144 memset(&ukey->stats, 0, sizeof ukey->stats);
1145 ukey->xcache = NULL;
1146 ovs_mutex_unlock(&ukey->mutex);
1151 /* Checks for a ukey in 'udpif->ukeys' with the same 'ukey->key' and 'hash',
1152 * and inserts 'ukey' if it does not exist.
1154 * Returns true if 'ukey' was inserted into 'udpif->ukeys', false otherwise. */
1156 udpif_insert_ukey(struct udpif *udpif, struct udpif_key *ukey, uint32_t hash)
1158 struct udpif_key *duplicate;
1159 uint32_t idx = hash % udpif->n_revalidators;
1162 ovs_mutex_lock(&udpif->ukeys[idx].mutex);
1163 duplicate = ukey_lookup__(udpif, ukey->key, ukey->key_len, hash);
1167 hmap_insert(&udpif->ukeys[idx].hmap, &ukey->hmap_node, hash);
1170 ovs_mutex_unlock(&udpif->ukeys[idx].mutex);
1176 ukey_delete(struct revalidator *revalidator, struct udpif_key *ukey)
1177 OVS_NO_THREAD_SAFETY_ANALYSIS
1180 hmap_remove(revalidator->ukeys, &ukey->hmap_node);
1182 xlate_cache_delete(ukey->xcache);
1183 ovs_mutex_destroy(&ukey->mutex);
1188 should_revalidate(uint64_t packets, long long int used)
1190 long long int metric, now, duration;
1192 /* Calculate the mean time between seeing these packets. If this
1193 * exceeds the threshold, then delete the flow rather than performing
1194 * costly revalidation for flows that aren't being hit frequently.
1196 * This is targeted at situations where the dump_duration is high (~1s),
1197 * and revalidation is triggered by a call to udpif_revalidate(). In
1198 * these situations, revalidation of all flows causes fluctuations in the
1199 * flow_limit due to the interaction with the dump_duration and max_idle.
1200 * This tends to result in deletion of low-throughput flows anyway, so
1201 * skip the revalidation and just delete those flows. */
1202 packets = MAX(packets, 1);
1203 now = MAX(used, time_msec());
1204 duration = now - used;
1205 metric = duration / packets;
1214 revalidate_ukey(struct udpif *udpif, struct udpif_key *ukey,
1215 const struct nlattr *mask, size_t mask_len,
1216 const struct nlattr *actions, size_t actions_len,
1217 const struct dpif_flow_stats *stats)
1219 uint64_t slow_path_buf[128 / 8];
1220 struct xlate_out xout, *xoutp;
1221 struct netflow *netflow;
1222 struct ofproto_dpif *ofproto;
1223 struct dpif_flow_stats push;
1224 struct ofpbuf xout_actions;
1225 struct flow flow, dp_mask;
1226 uint32_t *dp32, *xout32;
1227 odp_port_t odp_in_port;
1228 struct xlate_in xin;
1229 long long int last_used;
1238 ovs_mutex_lock(&ukey->mutex);
1239 last_used = ukey->stats.used;
1240 push.used = stats->used;
1241 push.tcp_flags = stats->tcp_flags;
1242 push.n_packets = stats->n_packets > ukey->stats.n_packets
1243 ? stats->n_packets - ukey->stats.n_packets
1245 push.n_bytes = stats->n_bytes > ukey->stats.n_bytes
1246 ? stats->n_bytes - ukey->stats.n_bytes
1249 if (!ukey->flow_exists) {
1250 /* Don't bother revalidating if the flow was already deleted. */
1254 if (udpif->need_revalidate && last_used
1255 && !should_revalidate(push.n_packets, last_used)) {
1260 /* We will push the stats, so update the ukey stats cache. */
1261 ukey->stats = *stats;
1262 if (!push.n_packets && !udpif->need_revalidate) {
1267 may_learn = push.n_packets > 0;
1268 if (ukey->xcache && !udpif->need_revalidate) {
1269 xlate_push_stats(ukey->xcache, may_learn, &push);
1274 error = xlate_receive(udpif->backer, NULL, ukey->key, ukey->key_len, &flow,
1275 &ofproto, NULL, NULL, &netflow, &odp_in_port);
1280 if (udpif->need_revalidate) {
1281 xlate_cache_clear(ukey->xcache);
1283 if (!ukey->xcache) {
1284 ukey->xcache = xlate_cache_new();
1287 xlate_in_init(&xin, ofproto, &flow, NULL, push.tcp_flags, NULL);
1288 xin.resubmit_stats = push.n_packets ? &push : NULL;
1289 xin.xcache = ukey->xcache;
1290 xin.may_learn = may_learn;
1291 xin.skip_wildcards = !udpif->need_revalidate;
1292 xlate_actions(&xin, &xout);
1295 if (!udpif->need_revalidate) {
1301 ofpbuf_use_const(&xout_actions, ofpbuf_data(&xout.odp_actions),
1302 ofpbuf_size(&xout.odp_actions));
1304 ofpbuf_use_stack(&xout_actions, slow_path_buf, sizeof slow_path_buf);
1305 compose_slow_path(udpif, &xout, &flow, odp_in_port, &xout_actions);
1308 if (actions_len != ofpbuf_size(&xout_actions)
1309 || memcmp(ofpbuf_data(&xout_actions), actions, actions_len)) {
1313 if (odp_flow_key_to_mask(mask, mask_len, &dp_mask, &flow)
1318 /* Since the kernel is free to ignore wildcarded bits in the mask, we can't
1319 * directly check that the masks are the same. Instead we check that the
1320 * mask in the kernel is more specific i.e. less wildcarded, than what
1321 * we've calculated here. This guarantees we don't catch any packets we
1322 * shouldn't with the megaflow. */
1323 dp32 = (uint32_t *) &dp_mask;
1324 xout32 = (uint32_t *) &xout.wc.masks;
1325 for (i = 0; i < FLOW_U32S; i++) {
1326 if ((dp32[i] | xout32[i]) != dp32[i]) {
1333 ovs_mutex_unlock(&ukey->mutex);
1336 netflow_expire(netflow, &flow);
1337 netflow_flow_clear(netflow, &flow);
1339 netflow_unref(netflow);
1341 xlate_out_uninit(xoutp);
1346 struct udpif_key *ukey;
1347 struct dpif_flow_stats stats; /* Stats for 'op'. */
1348 struct dpif_op op; /* Flow del operation. */
1352 dump_op_init(struct dump_op *op, const struct nlattr *key, size_t key_len,
1353 struct udpif_key *ukey)
1356 op->op.type = DPIF_OP_FLOW_DEL;
1357 op->op.u.flow_del.key = key;
1358 op->op.u.flow_del.key_len = key_len;
1359 op->op.u.flow_del.stats = &op->stats;
1363 push_dump_ops__(struct udpif *udpif, struct dump_op *ops, size_t n_ops)
1365 struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
1368 ovs_assert(n_ops <= REVALIDATE_MAX_BATCH);
1369 for (i = 0; i < n_ops; i++) {
1370 opsp[i] = &ops[i].op;
1372 dpif_operate(udpif->dpif, opsp, n_ops);
1374 for (i = 0; i < n_ops; i++) {
1375 struct dump_op *op = &ops[i];
1376 struct dpif_flow_stats *push, *stats, push_buf;
1378 stats = op->op.u.flow_del.stats;
1381 ovs_mutex_lock(&op->ukey->mutex);
1382 push->used = MAX(stats->used, op->ukey->stats.used);
1383 push->tcp_flags = stats->tcp_flags | op->ukey->stats.tcp_flags;
1384 push->n_packets = stats->n_packets - op->ukey->stats.n_packets;
1385 push->n_bytes = stats->n_bytes - op->ukey->stats.n_bytes;
1386 ovs_mutex_unlock(&op->ukey->mutex);
1391 if (push->n_packets || netflow_exists()) {
1392 struct ofproto_dpif *ofproto;
1393 struct netflow *netflow;
1397 may_learn = push->n_packets > 0;
1399 ovs_mutex_lock(&op->ukey->mutex);
1400 if (op->ukey->xcache) {
1401 xlate_push_stats(op->ukey->xcache, may_learn, push);
1402 ovs_mutex_unlock(&op->ukey->mutex);
1405 ovs_mutex_unlock(&op->ukey->mutex);
1408 if (!xlate_receive(udpif->backer, NULL, op->op.u.flow_del.key,
1409 op->op.u.flow_del.key_len, &flow, &ofproto,
1410 NULL, NULL, &netflow, NULL)) {
1411 struct xlate_in xin;
1413 xlate_in_init(&xin, ofproto, &flow, NULL, push->tcp_flags,
1415 xin.resubmit_stats = push->n_packets ? push : NULL;
1416 xin.may_learn = may_learn;
1417 xin.skip_wildcards = true;
1418 xlate_actions_for_side_effects(&xin);
1421 netflow_expire(netflow, &flow);
1422 netflow_flow_clear(netflow, &flow);
1423 netflow_unref(netflow);
1431 push_dump_ops(struct revalidator *revalidator,
1432 struct dump_op *ops, size_t n_ops)
1436 push_dump_ops__(revalidator->udpif, ops, n_ops);
1437 for (i = 0; i < n_ops; i++) {
1438 ukey_delete(revalidator, ops[i].ukey);
1443 revalidate(struct revalidator *revalidator)
1445 struct udpif *udpif = revalidator->udpif;
1447 struct dump_op ops[REVALIDATE_MAX_BATCH];
1448 const struct nlattr *key, *mask, *actions;
1449 size_t key_len, mask_len, actions_len;
1450 const struct dpif_flow_stats *stats;
1452 unsigned int flow_limit;
1458 atomic_read(&udpif->flow_limit, &flow_limit);
1460 dpif_flow_dump_state_init(udpif->dpif, &state);
1461 while (dpif_flow_dump_next(&udpif->dump, state, &key, &key_len, &mask,
1462 &mask_len, &actions, &actions_len, &stats)) {
1463 struct udpif_key *ukey;
1464 bool mark, may_destroy;
1465 long long int used, max_idle;
1469 hash = hash_bytes(key, key_len, udpif->secret);
1470 ukey = ukey_lookup(udpif, key, key_len, hash);
1473 if (!used && ukey) {
1474 ovs_mutex_lock(&ukey->mutex);
1476 if (ukey->mark || !ukey->flow_exists) {
1477 /* The flow has already been dumped. This can occasionally
1478 * occur if the datapath is changed in the middle of a flow
1479 * dump. Rather than perform the same work twice, skip the
1480 * flow this time. */
1481 ovs_mutex_unlock(&ukey->mutex);
1482 COVERAGE_INC(upcall_duplicate_flow);
1486 used = ukey->created;
1487 ovs_mutex_unlock(&ukey->mutex);
1490 n_flows = udpif_get_n_flows(udpif);
1491 max_idle = ofproto_max_idle;
1492 if (n_flows > flow_limit) {
1496 if ((used && used < now - max_idle) || n_flows > flow_limit * 2) {
1500 ukey = ukey_create(key, key_len, used);
1501 if (!udpif_insert_ukey(udpif, ukey, hash)) {
1502 /* The same ukey has already been created. This means that
1503 * another revalidator is processing this flow
1504 * concurrently, so don't bother processing it. */
1505 ukey_delete(NULL, ukey);
1510 mark = revalidate_ukey(udpif, ukey, mask, mask_len, actions,
1511 actions_len, stats);
1515 ovs_mutex_lock(&ukey->mutex);
1516 ukey->mark = ukey->flow_exists = mark;
1517 ovs_mutex_unlock(&ukey->mutex);
1521 dump_op_init(&ops[n_ops++], key, key_len, ukey);
1524 may_destroy = dpif_flow_dump_next_may_destroy_keys(&udpif->dump,
1527 /* Only update 'now' immediately before 'buffer' will be updated.
1528 * This gives us the current time relative to the time the datapath
1529 * will write into 'stats'. */
1534 /* Only do a dpif_operate when we've hit our maximum batch, or when our
1535 * memory is about to be clobbered by the next call to
1536 * dpif_flow_dump_next(). */
1537 if (n_ops == REVALIDATE_MAX_BATCH || (n_ops && may_destroy)) {
1538 push_dump_ops__(udpif, ops, n_ops);
1544 push_dump_ops__(udpif, ops, n_ops);
1547 dpif_flow_dump_state_uninit(udpif->dpif, state);
1551 revalidator_sweep__(struct revalidator *revalidator, bool purge)
1552 OVS_NO_THREAD_SAFETY_ANALYSIS
1554 struct dump_op ops[REVALIDATE_MAX_BATCH];
1555 struct udpif_key *ukey, *next;
1560 /* During garbage collection, this revalidator completely owns its ukeys
1561 * map, and therefore doesn't need to do any locking. */
1562 HMAP_FOR_EACH_SAFE (ukey, next, hmap_node, revalidator->ukeys) {
1563 if (!purge && ukey->mark) {
1565 } else if (!ukey->flow_exists) {
1566 ukey_delete(revalidator, ukey);
1568 struct dump_op *op = &ops[n_ops++];
1570 /* If we have previously seen a flow in the datapath, but didn't
1571 * see it during the most recent dump, delete it. This allows us
1572 * to clean up the ukey and keep the statistics consistent. */
1573 dump_op_init(op, ukey->key, ukey->key_len, ukey);
1574 if (n_ops == REVALIDATE_MAX_BATCH) {
1575 push_dump_ops(revalidator, ops, n_ops);
1582 push_dump_ops(revalidator, ops, n_ops);
1587 revalidator_sweep(struct revalidator *revalidator)
1589 revalidator_sweep__(revalidator, false);
1593 revalidator_purge(struct revalidator *revalidator)
1595 revalidator_sweep__(revalidator, true);
1599 upcall_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1600 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
1602 struct ds ds = DS_EMPTY_INITIALIZER;
1603 struct udpif *udpif;
1605 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1606 unsigned int flow_limit;
1609 atomic_read(&udpif->flow_limit, &flow_limit);
1611 ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
1612 ds_put_format(&ds, "\tflows : (current %"PRIu64")"
1613 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
1614 udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
1615 ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
1617 ds_put_char(&ds, '\n');
1618 for (i = 0; i < n_revalidators; i++) {
1619 struct revalidator *revalidator = &udpif->revalidators[i];
1621 ovs_mutex_lock(&udpif->ukeys[i].mutex);
1622 ds_put_format(&ds, "\t%s: (keys %"PRIuSIZE")\n", revalidator->name,
1623 hmap_count(&udpif->ukeys[i].hmap));
1624 ovs_mutex_unlock(&udpif->ukeys[i].mutex);
1628 unixctl_command_reply(conn, ds_cstr(&ds));
1632 /* Disable using the megaflows.
1634 * This command is only needed for advanced debugging, so it's not
1635 * documented in the man page. */
1637 upcall_unixctl_disable_megaflows(struct unixctl_conn *conn,
1638 int argc OVS_UNUSED,
1639 const char *argv[] OVS_UNUSED,
1640 void *aux OVS_UNUSED)
1642 atomic_store(&enable_megaflows, false);
1643 udpif_flush_all_datapaths();
1644 unixctl_command_reply(conn, "megaflows disabled");
1647 /* Re-enable using megaflows.
1649 * This command is only needed for advanced debugging, so it's not
1650 * documented in the man page. */
1652 upcall_unixctl_enable_megaflows(struct unixctl_conn *conn,
1653 int argc OVS_UNUSED,
1654 const char *argv[] OVS_UNUSED,
1655 void *aux OVS_UNUSED)
1657 atomic_store(&enable_megaflows, true);
1658 udpif_flush_all_datapaths();
1659 unixctl_command_reply(conn, "megaflows enabled");
1662 /* Set the flow limit.
1664 * This command is only needed for advanced debugging, so it's not
1665 * documented in the man page. */
1667 upcall_unixctl_set_flow_limit(struct unixctl_conn *conn,
1668 int argc OVS_UNUSED,
1669 const char *argv[] OVS_UNUSED,
1670 void *aux OVS_UNUSED)
1672 struct ds ds = DS_EMPTY_INITIALIZER;
1673 struct udpif *udpif;
1674 unsigned int flow_limit = atoi(argv[1]);
1676 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1677 atomic_store(&udpif->flow_limit, flow_limit);
1679 ds_put_format(&ds, "set flow_limit to %u\n", flow_limit);
1680 unixctl_command_reply(conn, ds_cstr(&ds));