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"
26 #include "dynamic-string.h"
27 #include "fail-open.h"
28 #include "guarded-list.h"
33 #include "ofproto-dpif-ipfix.h"
34 #include "ofproto-dpif-sflow.h"
35 #include "ofproto-dpif-xlate.h"
38 #include "poll-loop.h"
41 #include "openvswitch/vlog.h"
43 #define MAX_QUEUE_LENGTH 512
44 #define UPCALL_MAX_BATCH 64
45 #define REVALIDATE_MAX_BATCH 50
47 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall);
49 COVERAGE_DEFINE(dumped_duplicate_flow);
50 COVERAGE_DEFINE(dumped_new_flow);
51 COVERAGE_DEFINE(handler_duplicate_upcall);
52 COVERAGE_DEFINE(upcall_ukey_contention);
53 COVERAGE_DEFINE(revalidate_missed_dp_flow);
55 /* A thread that reads upcalls from dpif, forwards each upcall's packet,
56 * and possibly sets up a kernel flow as a cache. */
58 struct udpif *udpif; /* Parent udpif. */
59 pthread_t thread; /* Thread ID. */
60 uint32_t handler_id; /* Handler id. */
63 /* In the absence of a multiple-writer multiple-reader datastructure for
64 * storing ukeys, we use a large number of cmaps, each with its own lock for
66 #define N_UMAPS 512 /* per udpif. */
68 struct ovs_mutex mutex; /* Take for writing to the following. */
69 struct cmap cmap; /* Datapath flow keys. */
72 /* A thread that processes datapath flows, updates OpenFlow statistics, and
73 * updates or removes them if necessary. */
75 struct udpif *udpif; /* Parent udpif. */
76 pthread_t thread; /* Thread ID. */
77 unsigned int id; /* ovsthread_id_self(). */
80 /* An upcall handler for ofproto_dpif.
82 * udpif keeps records of two kind of logically separate units:
87 * - An array of 'struct handler's for upcall handling and flow
93 * - Revalidation threads which read the datapath flow table and maintains
97 struct ovs_list list_node; /* In all_udpifs list. */
99 struct dpif *dpif; /* Datapath handle. */
100 struct dpif_backer *backer; /* Opaque dpif_backer pointer. */
102 struct handler *handlers; /* Upcall handlers. */
105 struct revalidator *revalidators; /* Flow revalidators. */
106 size_t n_revalidators;
108 struct latch exit_latch; /* Tells child threads to exit. */
111 struct seq *reval_seq; /* Incremented to force revalidation. */
112 bool reval_exit; /* Set by leader on 'exit_latch. */
113 struct ovs_barrier reval_barrier; /* Barrier used by revalidators. */
114 struct dpif_flow_dump *dump; /* DPIF flow dump state. */
115 long long int dump_duration; /* Duration of the last flow dump. */
116 struct seq *dump_seq; /* Increments each dump iteration. */
117 atomic_bool enable_ufid; /* If true, skip dumping flow attrs. */
119 /* There are 'N_UMAPS' maps containing 'struct udpif_key' elements.
121 * During the flow dump phase, revalidators insert into these with a random
122 * distribution. During the garbage collection phase, each revalidator
123 * takes care of garbage collecting a slice of these maps. */
126 /* Datapath flow statistics. */
127 unsigned int max_n_flows;
128 unsigned int avg_n_flows;
130 /* Following fields are accessed and modified by different threads. */
131 atomic_uint flow_limit; /* Datapath flow hard limit. */
133 /* n_flows_mutex prevents multiple threads updating these concurrently. */
134 atomic_uint n_flows; /* Number of flows in the datapath. */
135 atomic_llong n_flows_timestamp; /* Last time n_flows was updated. */
136 struct ovs_mutex n_flows_mutex;
138 /* Following fields are accessed and modified only from the main thread. */
139 struct unixctl_conn **conns; /* Connections waiting on dump_seq. */
140 uint64_t conn_seq; /* Corresponds to 'dump_seq' when
141 conns[n_conns-1] was stored. */
142 size_t n_conns; /* Number of connections waiting. */
146 BAD_UPCALL, /* Some kind of bug somewhere. */
147 MISS_UPCALL, /* A flow miss. */
148 SFLOW_UPCALL, /* sFlow sample. */
149 FLOW_SAMPLE_UPCALL, /* Per-flow sampling. */
150 IPFIX_UPCALL /* Per-bridge sampling. */
154 struct ofproto_dpif *ofproto; /* Parent ofproto. */
156 /* The flow and packet are only required to be constant when using
157 * dpif-netdev. If a modification is absolutely necessary, a const cast
158 * may be used with other datapaths. */
159 const struct flow *flow; /* Parsed representation of the packet. */
160 const ovs_u128 *ufid; /* Unique identifier for 'flow'. */
161 int pmd_id; /* Datapath poll mode driver id. */
162 const struct dp_packet *packet; /* Packet associated with this upcall. */
163 ofp_port_t in_port; /* OpenFlow in port, or OFPP_NONE. */
165 enum dpif_upcall_type type; /* Datapath type of the upcall. */
166 const struct nlattr *userdata; /* Userdata for DPIF_UC_ACTION Upcalls. */
168 bool xout_initialized; /* True if 'xout' must be uninitialized. */
169 struct xlate_out xout; /* Result of xlate_actions(). */
170 struct ofpbuf put_actions; /* Actions 'put' in the fastapath. */
172 struct dpif_ipfix *ipfix; /* IPFIX pointer or NULL. */
173 struct dpif_sflow *sflow; /* SFlow pointer or NULL. */
175 bool vsp_adjusted; /* 'packet' and 'flow' were adjusted for
176 VLAN splinters if true. */
178 struct udpif_key *ukey; /* Revalidator flow cache. */
179 bool ukey_persists; /* Set true to keep 'ukey' beyond the
180 lifetime of this upcall. */
182 uint64_t dump_seq; /* udpif->dump_seq at translation time. */
183 uint64_t reval_seq; /* udpif->reval_seq at translation time. */
185 /* Not used by the upcall callback interface. */
186 const struct nlattr *key; /* Datapath flow key. */
187 size_t key_len; /* Datapath flow key length. */
188 const struct nlattr *out_tun_key; /* Datapath output tunnel key. */
191 /* 'udpif_key's are responsible for tracking the little bit of state udpif
192 * needs to do flow expiration which can't be pulled directly from the
193 * datapath. They may be created by any handler or revalidator thread at any
194 * time, and read by any revalidator during the dump phase. They are however
195 * each owned by a single revalidator which takes care of destroying them
196 * during the garbage-collection phase.
198 * The mutex within the ukey protects some members of the ukey. The ukey
199 * itself is protected by RCU and is held within a umap in the parent udpif.
200 * Adding or removing a ukey from a umap is only safe when holding the
201 * corresponding umap lock. */
203 struct cmap_node cmap_node; /* In parent revalidator 'ukeys' map. */
205 /* These elements are read only once created, and therefore aren't
206 * protected by a mutex. */
207 const struct nlattr *key; /* Datapath flow key. */
208 size_t key_len; /* Length of 'key'. */
209 const struct nlattr *mask; /* Datapath flow mask. */
210 size_t mask_len; /* Length of 'mask'. */
211 struct ofpbuf *actions; /* Datapath flow actions as nlattrs. */
212 ovs_u128 ufid; /* Unique flow identifier. */
213 bool ufid_present; /* True if 'ufid' is in datapath. */
214 uint32_t hash; /* Pre-computed hash for 'key'. */
215 int pmd_id; /* Datapath poll mode driver id. */
217 struct ovs_mutex mutex; /* Guards the following. */
218 struct dpif_flow_stats stats OVS_GUARDED; /* Last known stats.*/
219 long long int created OVS_GUARDED; /* Estimate of creation time. */
220 uint64_t dump_seq OVS_GUARDED; /* Tracks udpif->dump_seq. */
221 uint64_t reval_seq OVS_GUARDED; /* Tracks udpif->reval_seq. */
222 bool flow_exists OVS_GUARDED; /* Ensures flows are only deleted
225 struct xlate_cache *xcache OVS_GUARDED; /* Cache for xlate entries that
226 * are affected by this ukey.
227 * Used for stats and learning.*/
229 struct odputil_keybuf buf;
234 /* Datapath operation with optional ukey attached. */
236 struct udpif_key *ukey;
237 struct dpif_flow_stats stats; /* Stats for 'op'. */
238 struct dpif_op dop; /* Flow operation. */
241 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
242 static struct ovs_list all_udpifs = OVS_LIST_INITIALIZER(&all_udpifs);
244 static size_t recv_upcalls(struct handler *);
245 static int process_upcall(struct udpif *, struct upcall *,
246 struct ofpbuf *odp_actions);
247 static void handle_upcalls(struct udpif *, struct upcall *, size_t n_upcalls);
248 static void udpif_stop_threads(struct udpif *);
249 static void udpif_start_threads(struct udpif *, size_t n_handlers,
250 size_t n_revalidators);
251 static void *udpif_upcall_handler(void *);
252 static void *udpif_revalidator(void *);
253 static unsigned long udpif_get_n_flows(struct udpif *);
254 static void revalidate(struct revalidator *);
255 static void revalidator_sweep(struct revalidator *);
256 static void revalidator_purge(struct revalidator *);
257 static void upcall_unixctl_show(struct unixctl_conn *conn, int argc,
258 const char *argv[], void *aux);
259 static void upcall_unixctl_disable_megaflows(struct unixctl_conn *, int argc,
260 const char *argv[], void *aux);
261 static void upcall_unixctl_enable_megaflows(struct unixctl_conn *, int argc,
262 const char *argv[], void *aux);
263 static void upcall_unixctl_disable_ufid(struct unixctl_conn *, int argc,
264 const char *argv[], void *aux);
265 static void upcall_unixctl_enable_ufid(struct unixctl_conn *, int argc,
266 const char *argv[], void *aux);
267 static void upcall_unixctl_set_flow_limit(struct unixctl_conn *conn, int argc,
268 const char *argv[], void *aux);
269 static void upcall_unixctl_dump_wait(struct unixctl_conn *conn, int argc,
270 const char *argv[], void *aux);
271 static void upcall_unixctl_purge(struct unixctl_conn *conn, int argc,
272 const char *argv[], void *aux);
274 static struct udpif_key *ukey_create_from_upcall(const struct upcall *);
275 static int ukey_create_from_dpif_flow(const struct udpif *,
276 const struct dpif_flow *,
277 struct udpif_key **);
278 static bool ukey_install_start(struct udpif *, struct udpif_key *ukey);
279 static bool ukey_install_finish(struct udpif_key *ukey, int error);
280 static bool ukey_install(struct udpif *udpif, struct udpif_key *ukey);
281 static struct udpif_key *ukey_lookup(struct udpif *udpif,
282 const ovs_u128 *ufid);
283 static int ukey_acquire(struct udpif *, const struct dpif_flow *,
284 struct udpif_key **result, int *error);
285 static void ukey_delete__(struct udpif_key *);
286 static void ukey_delete(struct umap *, struct udpif_key *);
287 static enum upcall_type classify_upcall(enum dpif_upcall_type type,
288 const struct nlattr *userdata);
290 static int upcall_receive(struct upcall *, const struct dpif_backer *,
291 const struct dp_packet *packet, enum dpif_upcall_type,
292 const struct nlattr *userdata, const struct flow *,
293 const ovs_u128 *ufid, const int pmd_id);
294 static void upcall_uninit(struct upcall *);
296 static upcall_callback upcall_cb;
298 static atomic_bool enable_megaflows = ATOMIC_VAR_INIT(true);
299 static atomic_bool enable_ufid = ATOMIC_VAR_INIT(true);
302 udpif_create(struct dpif_backer *backer, struct dpif *dpif)
304 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
305 struct udpif *udpif = xzalloc(sizeof *udpif);
307 if (ovsthread_once_start(&once)) {
308 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show,
310 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
311 upcall_unixctl_disable_megaflows, NULL);
312 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
313 upcall_unixctl_enable_megaflows, NULL);
314 unixctl_command_register("upcall/disable-ufid", "", 0, 0,
315 upcall_unixctl_disable_ufid, NULL);
316 unixctl_command_register("upcall/enable-ufid", "", 0, 0,
317 upcall_unixctl_enable_ufid, NULL);
318 unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
319 upcall_unixctl_set_flow_limit, NULL);
320 unixctl_command_register("revalidator/wait", "", 0, 0,
321 upcall_unixctl_dump_wait, NULL);
322 unixctl_command_register("revalidator/purge", "", 0, 0,
323 upcall_unixctl_purge, NULL);
324 ovsthread_once_done(&once);
328 udpif->backer = backer;
329 atomic_init(&udpif->flow_limit, MIN(ofproto_flow_limit, 10000));
330 udpif->reval_seq = seq_create();
331 udpif->dump_seq = seq_create();
332 latch_init(&udpif->exit_latch);
333 list_push_back(&all_udpifs, &udpif->list_node);
334 atomic_init(&udpif->enable_ufid, false);
335 atomic_init(&udpif->n_flows, 0);
336 atomic_init(&udpif->n_flows_timestamp, LLONG_MIN);
337 ovs_mutex_init(&udpif->n_flows_mutex);
338 udpif->ukeys = xmalloc(N_UMAPS * sizeof *udpif->ukeys);
339 for (int i = 0; i < N_UMAPS; i++) {
340 cmap_init(&udpif->ukeys[i].cmap);
341 ovs_mutex_init(&udpif->ukeys[i].mutex);
344 dpif_register_upcall_cb(dpif, upcall_cb, udpif);
350 udpif_run(struct udpif *udpif)
352 if (udpif->conns && udpif->conn_seq != seq_read(udpif->dump_seq)) {
355 for (i = 0; i < udpif->n_conns; i++) {
356 unixctl_command_reply(udpif->conns[i], NULL);
365 udpif_destroy(struct udpif *udpif)
367 udpif_stop_threads(udpif);
369 for (int i = 0; i < N_UMAPS; i++) {
370 cmap_destroy(&udpif->ukeys[i].cmap);
371 ovs_mutex_destroy(&udpif->ukeys[i].mutex);
376 list_remove(&udpif->list_node);
377 latch_destroy(&udpif->exit_latch);
378 seq_destroy(udpif->reval_seq);
379 seq_destroy(udpif->dump_seq);
380 ovs_mutex_destroy(&udpif->n_flows_mutex);
384 /* Stops the handler and revalidator threads, must be enclosed in
385 * ovsrcu quiescent state unless when destroying udpif. */
387 udpif_stop_threads(struct udpif *udpif)
389 if (udpif && (udpif->n_handlers != 0 || udpif->n_revalidators != 0)) {
392 latch_set(&udpif->exit_latch);
394 for (i = 0; i < udpif->n_handlers; i++) {
395 struct handler *handler = &udpif->handlers[i];
397 xpthread_join(handler->thread, NULL);
400 for (i = 0; i < udpif->n_revalidators; i++) {
401 xpthread_join(udpif->revalidators[i].thread, NULL);
404 dpif_disable_upcall(udpif->dpif);
406 for (i = 0; i < udpif->n_revalidators; i++) {
407 struct revalidator *revalidator = &udpif->revalidators[i];
409 /* Delete ukeys, and delete all flows from the datapath to prevent
410 * double-counting stats. */
411 revalidator_purge(revalidator);
414 latch_poll(&udpif->exit_latch);
416 ovs_barrier_destroy(&udpif->reval_barrier);
418 free(udpif->revalidators);
419 udpif->revalidators = NULL;
420 udpif->n_revalidators = 0;
422 free(udpif->handlers);
423 udpif->handlers = NULL;
424 udpif->n_handlers = 0;
428 /* Starts the handler and revalidator threads, must be enclosed in
429 * ovsrcu quiescent state. */
431 udpif_start_threads(struct udpif *udpif, size_t n_handlers,
432 size_t n_revalidators)
434 if (udpif && n_handlers && n_revalidators) {
438 udpif->n_handlers = n_handlers;
439 udpif->n_revalidators = n_revalidators;
441 udpif->handlers = xzalloc(udpif->n_handlers * sizeof *udpif->handlers);
442 for (i = 0; i < udpif->n_handlers; i++) {
443 struct handler *handler = &udpif->handlers[i];
445 handler->udpif = udpif;
446 handler->handler_id = i;
447 handler->thread = ovs_thread_create(
448 "handler", udpif_upcall_handler, handler);
451 enable_ufid = ofproto_dpif_get_enable_ufid(udpif->backer);
452 atomic_init(&udpif->enable_ufid, enable_ufid);
453 dpif_enable_upcall(udpif->dpif);
455 ovs_barrier_init(&udpif->reval_barrier, udpif->n_revalidators);
456 udpif->reval_exit = false;
457 udpif->revalidators = xzalloc(udpif->n_revalidators
458 * sizeof *udpif->revalidators);
459 for (i = 0; i < udpif->n_revalidators; i++) {
460 struct revalidator *revalidator = &udpif->revalidators[i];
462 revalidator->udpif = udpif;
463 revalidator->thread = ovs_thread_create(
464 "revalidator", udpif_revalidator, revalidator);
469 /* Tells 'udpif' how many threads it should use to handle upcalls.
470 * 'n_handlers' and 'n_revalidators' can never be zero. 'udpif''s
471 * datapath handle must have packet reception enabled before starting
474 udpif_set_threads(struct udpif *udpif, size_t n_handlers,
475 size_t n_revalidators)
478 ovs_assert(n_handlers && n_revalidators);
480 ovsrcu_quiesce_start();
481 if (udpif->n_handlers != n_handlers
482 || udpif->n_revalidators != n_revalidators) {
483 udpif_stop_threads(udpif);
486 if (!udpif->handlers && !udpif->revalidators) {
489 error = dpif_handlers_set(udpif->dpif, n_handlers);
491 VLOG_ERR("failed to configure handlers in dpif %s: %s",
492 dpif_name(udpif->dpif), ovs_strerror(error));
496 udpif_start_threads(udpif, n_handlers, n_revalidators);
498 ovsrcu_quiesce_end();
501 /* Waits for all ongoing upcall translations to complete. This ensures that
502 * there are no transient references to any removed ofprotos (or other
503 * objects). In particular, this should be called after an ofproto is removed
504 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
506 udpif_synchronize(struct udpif *udpif)
508 /* This is stronger than necessary. It would be sufficient to ensure
509 * (somehow) that each handler and revalidator thread had passed through
510 * its main loop once. */
511 size_t n_handlers = udpif->n_handlers;
512 size_t n_revalidators = udpif->n_revalidators;
514 ovsrcu_quiesce_start();
515 udpif_stop_threads(udpif);
516 udpif_start_threads(udpif, n_handlers, n_revalidators);
517 ovsrcu_quiesce_end();
520 /* Notifies 'udpif' that something changed which may render previous
521 * xlate_actions() results invalid. */
523 udpif_revalidate(struct udpif *udpif)
525 seq_change(udpif->reval_seq);
528 /* Returns a seq which increments every time 'udpif' pulls stats from the
529 * datapath. Callers can use this to get a sense of when might be a good time
530 * to do periodic work which relies on relatively up to date statistics. */
532 udpif_dump_seq(struct udpif *udpif)
534 return udpif->dump_seq;
538 udpif_get_memory_usage(struct udpif *udpif, struct simap *usage)
542 simap_increase(usage, "handlers", udpif->n_handlers);
544 simap_increase(usage, "revalidators", udpif->n_revalidators);
545 for (i = 0; i < N_UMAPS; i++) {
546 simap_increase(usage, "udpif keys", cmap_count(&udpif->ukeys[i].cmap));
550 /* Remove flows from a single datapath. */
552 udpif_flush(struct udpif *udpif)
554 size_t n_handlers, n_revalidators;
556 n_handlers = udpif->n_handlers;
557 n_revalidators = udpif->n_revalidators;
559 ovsrcu_quiesce_start();
561 udpif_stop_threads(udpif);
562 dpif_flow_flush(udpif->dpif);
563 udpif_start_threads(udpif, n_handlers, n_revalidators);
565 ovsrcu_quiesce_end();
568 /* Removes all flows from all datapaths. */
570 udpif_flush_all_datapaths(void)
574 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
580 udpif_use_ufid(struct udpif *udpif)
584 atomic_read_relaxed(&enable_ufid, &enable);
585 return enable && ofproto_dpif_get_enable_ufid(udpif->backer);
590 udpif_get_n_flows(struct udpif *udpif)
592 long long int time, now;
593 unsigned long flow_count;
596 atomic_read_relaxed(&udpif->n_flows_timestamp, &time);
597 if (time < now - 100 && !ovs_mutex_trylock(&udpif->n_flows_mutex)) {
598 struct dpif_dp_stats stats;
600 atomic_store_relaxed(&udpif->n_flows_timestamp, now);
601 dpif_get_dp_stats(udpif->dpif, &stats);
602 flow_count = stats.n_flows;
603 atomic_store_relaxed(&udpif->n_flows, flow_count);
604 ovs_mutex_unlock(&udpif->n_flows_mutex);
606 atomic_read_relaxed(&udpif->n_flows, &flow_count);
611 /* The upcall handler thread tries to read a batch of UPCALL_MAX_BATCH
612 * upcalls from dpif, processes the batch and installs corresponding flows
615 udpif_upcall_handler(void *arg)
617 struct handler *handler = arg;
618 struct udpif *udpif = handler->udpif;
620 while (!latch_is_set(&handler->udpif->exit_latch)) {
621 if (recv_upcalls(handler)) {
622 poll_immediate_wake();
624 dpif_recv_wait(udpif->dpif, handler->handler_id);
625 latch_wait(&udpif->exit_latch);
634 recv_upcalls(struct handler *handler)
636 struct udpif *udpif = handler->udpif;
637 uint64_t recv_stubs[UPCALL_MAX_BATCH][512 / 8];
638 struct ofpbuf recv_bufs[UPCALL_MAX_BATCH];
639 struct dpif_upcall dupcalls[UPCALL_MAX_BATCH];
640 struct upcall upcalls[UPCALL_MAX_BATCH];
641 struct flow flows[UPCALL_MAX_BATCH];
645 while (n_upcalls < UPCALL_MAX_BATCH) {
646 struct ofpbuf *recv_buf = &recv_bufs[n_upcalls];
647 struct dpif_upcall *dupcall = &dupcalls[n_upcalls];
648 struct upcall *upcall = &upcalls[n_upcalls];
649 struct flow *flow = &flows[n_upcalls];
652 ofpbuf_use_stub(recv_buf, recv_stubs[n_upcalls],
653 sizeof recv_stubs[n_upcalls]);
654 if (dpif_recv(udpif->dpif, handler->handler_id, dupcall, recv_buf)) {
655 ofpbuf_uninit(recv_buf);
659 if (odp_flow_key_to_flow(dupcall->key, dupcall->key_len, flow)
664 error = upcall_receive(upcall, udpif->backer, &dupcall->packet,
665 dupcall->type, dupcall->userdata, flow,
666 &dupcall->ufid, PMD_ID_NULL);
668 if (error == ENODEV) {
669 /* Received packet on datapath port for which we couldn't
670 * associate an ofproto. This can happen if a port is removed
671 * while traffic is being received. Print a rate-limited
672 * message in case it happens frequently. */
673 dpif_flow_put(udpif->dpif, DPIF_FP_CREATE, dupcall->key,
674 dupcall->key_len, NULL, 0, NULL, 0,
675 &dupcall->ufid, PMD_ID_NULL, NULL);
676 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
677 "port %"PRIu32, flow->in_port.odp_port);
682 upcall->key = dupcall->key;
683 upcall->key_len = dupcall->key_len;
684 upcall->ufid = &dupcall->ufid;
686 upcall->out_tun_key = dupcall->out_tun_key;
688 if (vsp_adjust_flow(upcall->ofproto, flow, &dupcall->packet)) {
689 upcall->vsp_adjusted = true;
692 pkt_metadata_from_flow(&dupcall->packet.md, flow);
693 flow_extract(&dupcall->packet, flow);
695 error = process_upcall(udpif, upcall, NULL);
704 upcall_uninit(upcall);
706 dp_packet_uninit(&dupcall->packet);
707 ofpbuf_uninit(recv_buf);
711 handle_upcalls(handler->udpif, upcalls, n_upcalls);
712 for (i = 0; i < n_upcalls; i++) {
713 dp_packet_uninit(&dupcalls[i].packet);
714 ofpbuf_uninit(&recv_bufs[i]);
715 upcall_uninit(&upcalls[i]);
723 udpif_revalidator(void *arg)
725 /* Used by all revalidators. */
726 struct revalidator *revalidator = arg;
727 struct udpif *udpif = revalidator->udpif;
728 bool leader = revalidator == &udpif->revalidators[0];
730 /* Used only by the leader. */
731 long long int start_time = 0;
732 uint64_t last_reval_seq = 0;
735 revalidator->id = ovsthread_id_self();
740 reval_seq = seq_read(udpif->reval_seq);
741 last_reval_seq = reval_seq;
743 n_flows = udpif_get_n_flows(udpif);
744 udpif->max_n_flows = MAX(n_flows, udpif->max_n_flows);
745 udpif->avg_n_flows = (udpif->avg_n_flows + n_flows) / 2;
747 /* Only the leader checks the exit latch to prevent a race where
748 * some threads think it's true and exit and others think it's
749 * false and block indefinitely on the reval_barrier */
750 udpif->reval_exit = latch_is_set(&udpif->exit_latch);
752 start_time = time_msec();
753 if (!udpif->reval_exit) {
756 terse_dump = udpif_use_ufid(udpif);
757 udpif->dump = dpif_flow_dump_create(udpif->dpif, terse_dump);
761 /* Wait for the leader to start the flow dump. */
762 ovs_barrier_block(&udpif->reval_barrier);
763 if (udpif->reval_exit) {
766 revalidate(revalidator);
768 /* Wait for all flows to have been dumped before we garbage collect. */
769 ovs_barrier_block(&udpif->reval_barrier);
770 revalidator_sweep(revalidator);
772 /* Wait for all revalidators to finish garbage collection. */
773 ovs_barrier_block(&udpif->reval_barrier);
776 unsigned int flow_limit;
777 long long int duration;
779 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
781 dpif_flow_dump_destroy(udpif->dump);
782 seq_change(udpif->dump_seq);
784 duration = MAX(time_msec() - start_time, 1);
785 udpif->dump_duration = duration;
786 if (duration > 2000) {
787 flow_limit /= duration / 1000;
788 } else if (duration > 1300) {
789 flow_limit = flow_limit * 3 / 4;
790 } else if (duration < 1000 && n_flows > 2000
791 && flow_limit < n_flows * 1000 / duration) {
794 flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
795 atomic_store_relaxed(&udpif->flow_limit, flow_limit);
797 if (duration > 2000) {
798 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
802 poll_timer_wait_until(start_time + MIN(ofproto_max_idle, 500));
803 seq_wait(udpif->reval_seq, last_reval_seq);
804 latch_wait(&udpif->exit_latch);
812 static enum upcall_type
813 classify_upcall(enum dpif_upcall_type type, const struct nlattr *userdata)
815 union user_action_cookie cookie;
818 /* First look at the upcall type. */
826 case DPIF_N_UC_TYPES:
828 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, type);
832 /* "action" upcalls need a closer look. */
834 VLOG_WARN_RL(&rl, "action upcall missing cookie");
837 userdata_len = nl_attr_get_size(userdata);
838 if (userdata_len < sizeof cookie.type
839 || userdata_len > sizeof cookie) {
840 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
844 memset(&cookie, 0, sizeof cookie);
845 memcpy(&cookie, nl_attr_get(userdata), userdata_len);
846 if (userdata_len == MAX(8, sizeof cookie.sflow)
847 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
849 } else if (userdata_len == MAX(8, sizeof cookie.slow_path)
850 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
852 } else if (userdata_len == MAX(8, sizeof cookie.flow_sample)
853 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
854 return FLOW_SAMPLE_UPCALL;
855 } else if (userdata_len == MAX(8, sizeof cookie.ipfix)
856 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
859 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
860 " and size %"PRIuSIZE, cookie.type, userdata_len);
865 /* Calculates slow path actions for 'xout'. 'buf' must statically be
866 * initialized with at least 128 bytes of space. */
868 compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
869 const struct flow *flow, odp_port_t odp_in_port,
872 union user_action_cookie cookie;
876 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
877 cookie.slow_path.unused = 0;
878 cookie.slow_path.reason = xout->slow;
880 port = xout->slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)
883 pid = dpif_port_get_pid(udpif->dpif, port, flow_hash_5tuple(flow, 0));
884 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, ODPP_NONE,
888 /* If there is no error, the upcall must be destroyed with upcall_uninit()
889 * before quiescing, as the referred objects are guaranteed to exist only
890 * until the calling thread quiesces. Otherwise, do not call upcall_uninit()
891 * since the 'upcall->put_actions' remains uninitialized. */
893 upcall_receive(struct upcall *upcall, const struct dpif_backer *backer,
894 const struct dp_packet *packet, enum dpif_upcall_type type,
895 const struct nlattr *userdata, const struct flow *flow,
896 const ovs_u128 *ufid, const int pmd_id)
900 error = xlate_lookup(backer, flow, &upcall->ofproto, &upcall->ipfix,
901 &upcall->sflow, NULL, &upcall->in_port);
907 upcall->packet = packet;
909 upcall->pmd_id = pmd_id;
911 upcall->userdata = userdata;
912 ofpbuf_init(&upcall->put_actions, 0);
914 upcall->xout_initialized = false;
915 upcall->vsp_adjusted = false;
916 upcall->ukey_persists = false;
922 upcall->out_tun_key = NULL;
928 upcall_xlate(struct udpif *udpif, struct upcall *upcall,
929 struct ofpbuf *odp_actions)
931 struct dpif_flow_stats stats;
935 stats.n_bytes = dp_packet_size(upcall->packet);
936 stats.used = time_msec();
937 stats.tcp_flags = ntohs(upcall->flow->tcp_flags);
939 xlate_in_init(&xin, upcall->ofproto, upcall->flow, upcall->in_port, NULL,
940 stats.tcp_flags, upcall->packet);
941 xin.odp_actions = odp_actions;
943 if (upcall->type == DPIF_UC_MISS) {
944 xin.resubmit_stats = &stats;
946 /* For non-miss upcalls, there's a flow in the datapath which this
947 * packet was accounted to. Presumably the revalidators will deal
948 * with pushing its stats eventually. */
951 upcall->dump_seq = seq_read(udpif->dump_seq);
952 upcall->reval_seq = seq_read(udpif->reval_seq);
953 xlate_actions(&xin, &upcall->xout);
954 upcall->xout_initialized = true;
956 /* Special case for fail-open mode.
958 * If we are in fail-open mode, but we are connected to a controller too,
959 * then we should send the packet up to the controller in the hope that it
960 * will try to set up a flow and thereby allow us to exit fail-open.
962 * See the top-level comment in fail-open.c for more information.
964 * Copy packets before they are modified by execution. */
965 if (upcall->xout.fail_open) {
966 const struct dp_packet *packet = upcall->packet;
967 struct ofproto_packet_in *pin;
969 pin = xmalloc(sizeof *pin);
970 pin->up.packet = xmemdup(dp_packet_data(packet), dp_packet_size(packet));
971 pin->up.packet_len = dp_packet_size(packet);
972 pin->up.reason = OFPR_NO_MATCH;
973 pin->up.table_id = 0;
974 pin->up.cookie = OVS_BE64_MAX;
975 flow_get_metadata(upcall->flow, &pin->up.fmd);
976 pin->send_len = 0; /* Not used for flow table misses. */
977 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
978 ofproto_dpif_send_packet_in(upcall->ofproto, pin);
981 if (!upcall->xout.slow) {
982 ofpbuf_use_const(&upcall->put_actions,
983 upcall->xout.odp_actions->data,
984 upcall->xout.odp_actions->size);
986 ofpbuf_init(&upcall->put_actions, 0);
987 compose_slow_path(udpif, &upcall->xout, upcall->flow,
988 upcall->flow->in_port.odp_port,
989 &upcall->put_actions);
992 upcall->ukey = ukey_create_from_upcall(upcall);
996 upcall_uninit(struct upcall *upcall)
999 if (upcall->xout_initialized) {
1000 xlate_out_uninit(&upcall->xout);
1002 ofpbuf_uninit(&upcall->put_actions);
1003 if (!upcall->ukey_persists) {
1004 ukey_delete__(upcall->ukey);
1010 upcall_cb(const struct dp_packet *packet, const struct flow *flow, ovs_u128 *ufid,
1011 int pmd_id, enum dpif_upcall_type type,
1012 const struct nlattr *userdata, struct ofpbuf *actions,
1013 struct flow_wildcards *wc, struct ofpbuf *put_actions, void *aux)
1015 struct udpif *udpif = aux;
1016 unsigned int flow_limit;
1017 struct upcall upcall;
1021 atomic_read_relaxed(&enable_megaflows, &megaflow);
1022 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
1024 error = upcall_receive(&upcall, udpif->backer, packet, type, userdata,
1025 flow, ufid, pmd_id);
1030 error = process_upcall(udpif, &upcall, actions);
1035 if (upcall.xout.slow && put_actions) {
1036 ofpbuf_put(put_actions, upcall.put_actions.data,
1037 upcall.put_actions.size);
1040 if (OVS_LIKELY(wc)) {
1042 /* XXX: This could be avoided with sufficient API changes. */
1043 *wc = upcall.xout.wc;
1045 flow_wildcards_init_for_packet(wc, flow);
1049 if (udpif_get_n_flows(udpif) >= flow_limit) {
1054 if (upcall.ukey && !ukey_install(udpif, upcall.ukey)) {
1060 upcall.ukey_persists = true;
1062 upcall_uninit(&upcall);
1067 process_upcall(struct udpif *udpif, struct upcall *upcall,
1068 struct ofpbuf *odp_actions)
1070 const struct nlattr *userdata = upcall->userdata;
1071 const struct dp_packet *packet = upcall->packet;
1072 const struct flow *flow = upcall->flow;
1074 switch (classify_upcall(upcall->type, userdata)) {
1076 upcall_xlate(udpif, upcall, odp_actions);
1080 if (upcall->sflow) {
1081 union user_action_cookie cookie;
1083 memset(&cookie, 0, sizeof cookie);
1084 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.sflow);
1085 dpif_sflow_received(upcall->sflow, packet, flow,
1086 flow->in_port.odp_port, &cookie);
1091 if (upcall->ipfix) {
1092 union user_action_cookie cookie;
1093 struct flow_tnl output_tunnel_key;
1095 memset(&cookie, 0, sizeof cookie);
1096 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.ipfix);
1098 if (upcall->out_tun_key) {
1099 memset(&output_tunnel_key, 0, sizeof output_tunnel_key);
1100 odp_tun_key_from_attr(upcall->out_tun_key,
1101 &output_tunnel_key);
1103 dpif_ipfix_bridge_sample(upcall->ipfix, packet, flow,
1104 flow->in_port.odp_port,
1105 cookie.ipfix.output_odp_port,
1106 upcall->out_tun_key ?
1107 &output_tunnel_key : NULL);
1111 case FLOW_SAMPLE_UPCALL:
1112 if (upcall->ipfix) {
1113 union user_action_cookie cookie;
1115 memset(&cookie, 0, sizeof cookie);
1116 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.flow_sample);
1118 /* The flow reflects exactly the contents of the packet.
1119 * Sample the packet using it. */
1120 dpif_ipfix_flow_sample(upcall->ipfix, packet, flow,
1121 cookie.flow_sample.collector_set_id,
1122 cookie.flow_sample.probability,
1123 cookie.flow_sample.obs_domain_id,
1124 cookie.flow_sample.obs_point_id);
1136 handle_upcalls(struct udpif *udpif, struct upcall *upcalls,
1139 struct dpif_op *opsp[UPCALL_MAX_BATCH * 2];
1140 struct ukey_op ops[UPCALL_MAX_BATCH * 2];
1141 unsigned int flow_limit;
1142 size_t n_ops, n_opsp, i;
1146 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
1147 atomic_read_relaxed(&enable_megaflows, &megaflow);
1149 may_put = udpif_get_n_flows(udpif) < flow_limit;
1151 /* Handle the packets individually in order of arrival.
1153 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
1154 * processes received packets for these protocols.
1156 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1159 * The loop fills 'ops' with an array of operations to execute in the
1162 for (i = 0; i < n_upcalls; i++) {
1163 struct upcall *upcall = &upcalls[i];
1164 const struct dp_packet *packet = upcall->packet;
1167 if (upcall->vsp_adjusted) {
1168 /* This packet was received on a VLAN splinter port. We added a
1169 * VLAN to the packet to make the packet resemble the flow, but the
1170 * actions were composed assuming that the packet contained no
1171 * VLAN. So, we must remove the VLAN header from the packet before
1172 * trying to execute the actions. */
1173 if (upcall->xout.odp_actions->size) {
1174 eth_pop_vlan(CONST_CAST(struct dp_packet *, upcall->packet));
1177 /* Remove the flow vlan tags inserted by vlan splinter logic
1178 * to ensure megaflow masks generated match the data path flow. */
1179 CONST_CAST(struct flow *, upcall->flow)->vlan_tci = 0;
1182 /* Do not install a flow into the datapath if:
1184 * - The datapath already has too many flows.
1186 * - We received this packet via some flow installed in the kernel
1188 if (may_put && upcall->type == DPIF_UC_MISS) {
1189 struct udpif_key *ukey = upcall->ukey;
1191 upcall->ukey_persists = true;
1195 op->dop.type = DPIF_OP_FLOW_PUT;
1196 op->dop.u.flow_put.flags = DPIF_FP_CREATE;
1197 op->dop.u.flow_put.key = ukey->key;
1198 op->dop.u.flow_put.key_len = ukey->key_len;
1199 op->dop.u.flow_put.mask = ukey->mask;
1200 op->dop.u.flow_put.mask_len = ukey->mask_len;
1201 op->dop.u.flow_put.ufid = upcall->ufid;
1202 op->dop.u.flow_put.stats = NULL;
1203 op->dop.u.flow_put.actions = ukey->actions->data;
1204 op->dop.u.flow_put.actions_len = ukey->actions->size;
1207 if (upcall->xout.odp_actions->size) {
1210 op->dop.type = DPIF_OP_EXECUTE;
1211 op->dop.u.execute.packet = CONST_CAST(struct dp_packet *, packet);
1212 odp_key_to_pkt_metadata(upcall->key, upcall->key_len,
1213 &op->dop.u.execute.packet->md);
1214 op->dop.u.execute.actions = upcall->xout.odp_actions->data;
1215 op->dop.u.execute.actions_len = upcall->xout.odp_actions->size;
1216 op->dop.u.execute.needs_help = (upcall->xout.slow & SLOW_ACTION) != 0;
1217 op->dop.u.execute.probe = false;
1223 * We install ukeys before installing the flows, locking them for exclusive
1224 * access by this thread for the period of installation. This ensures that
1225 * other threads won't attempt to delete the flows as we are creating them.
1228 for (i = 0; i < n_ops; i++) {
1229 struct udpif_key *ukey = ops[i].ukey;
1232 /* If we can't install the ukey, don't install the flow. */
1233 if (!ukey_install_start(udpif, ukey)) {
1234 ukey_delete__(ukey);
1239 opsp[n_opsp++] = &ops[i].dop;
1241 dpif_operate(udpif->dpif, opsp, n_opsp);
1242 for (i = 0; i < n_ops; i++) {
1244 ukey_install_finish(ops[i].ukey, ops[i].dop.error);
1250 get_ufid_hash(const ovs_u128 *ufid)
1252 return ufid->u32[0];
1255 static struct udpif_key *
1256 ukey_lookup(struct udpif *udpif, const ovs_u128 *ufid)
1258 struct udpif_key *ukey;
1259 int idx = get_ufid_hash(ufid) % N_UMAPS;
1260 struct cmap *cmap = &udpif->ukeys[idx].cmap;
1262 CMAP_FOR_EACH_WITH_HASH (ukey, cmap_node, get_ufid_hash(ufid), cmap) {
1263 if (ovs_u128_equal(&ukey->ufid, ufid)) {
1270 static struct udpif_key *
1271 ukey_create__(const struct nlattr *key, size_t key_len,
1272 const struct nlattr *mask, size_t mask_len,
1273 bool ufid_present, const ovs_u128 *ufid,
1274 const int pmd_id, const struct ofpbuf *actions,
1275 uint64_t dump_seq, uint64_t reval_seq, long long int used)
1276 OVS_NO_THREAD_SAFETY_ANALYSIS
1278 struct udpif_key *ukey = xmalloc(sizeof *ukey);
1280 memcpy(&ukey->keybuf, key, key_len);
1281 ukey->key = &ukey->keybuf.nla;
1282 ukey->key_len = key_len;
1283 memcpy(&ukey->maskbuf, mask, mask_len);
1284 ukey->mask = &ukey->maskbuf.nla;
1285 ukey->mask_len = mask_len;
1286 ukey->ufid_present = ufid_present;
1288 ukey->pmd_id = pmd_id;
1289 ukey->hash = get_ufid_hash(&ukey->ufid);
1290 ukey->actions = ofpbuf_clone(actions);
1292 ovs_mutex_init(&ukey->mutex);
1293 ukey->dump_seq = dump_seq;
1294 ukey->reval_seq = reval_seq;
1295 ukey->flow_exists = false;
1296 ukey->created = time_msec();
1297 memset(&ukey->stats, 0, sizeof ukey->stats);
1298 ukey->stats.used = used;
1299 ukey->xcache = NULL;
1304 static struct udpif_key *
1305 ukey_create_from_upcall(const struct upcall *upcall)
1307 struct odputil_keybuf keystub, maskstub;
1308 struct ofpbuf keybuf, maskbuf;
1309 bool recirc, megaflow;
1311 if (upcall->key_len) {
1312 ofpbuf_use_const(&keybuf, upcall->key, upcall->key_len);
1314 /* dpif-netdev doesn't provide a netlink-formatted flow key in the
1315 * upcall, so convert the upcall's flow here. */
1316 ofpbuf_use_stack(&keybuf, &keystub, sizeof keystub);
1317 odp_flow_key_from_flow(&keybuf, upcall->flow, &upcall->xout.wc.masks,
1318 upcall->flow->in_port.odp_port, true);
1321 atomic_read_relaxed(&enable_megaflows, &megaflow);
1322 recirc = ofproto_dpif_get_enable_recirc(upcall->ofproto);
1323 ofpbuf_use_stack(&maskbuf, &maskstub, sizeof maskstub);
1327 max_mpls = ofproto_dpif_get_max_mpls_depth(upcall->ofproto);
1328 odp_flow_key_from_mask(&maskbuf, &upcall->xout.wc.masks, upcall->flow,
1329 UINT32_MAX, max_mpls, recirc);
1332 return ukey_create__(keybuf.data, keybuf.size, maskbuf.data, maskbuf.size,
1333 true, upcall->ufid, upcall->pmd_id,
1334 &upcall->put_actions, upcall->dump_seq,
1335 upcall->reval_seq, 0);
1339 ukey_create_from_dpif_flow(const struct udpif *udpif,
1340 const struct dpif_flow *flow,
1341 struct udpif_key **ukey)
1343 struct dpif_flow full_flow;
1344 struct ofpbuf actions;
1345 uint64_t dump_seq, reval_seq;
1346 uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
1348 if (!flow->key_len) {
1352 /* If the key was not provided by the datapath, fetch the full flow. */
1353 ofpbuf_use_stack(&buf, &stub, sizeof stub);
1354 err = dpif_flow_get(udpif->dpif, NULL, 0, &flow->ufid,
1355 flow->pmd_id, &buf, &full_flow);
1361 dump_seq = seq_read(udpif->dump_seq);
1362 reval_seq = seq_read(udpif->reval_seq);
1363 ofpbuf_use_const(&actions, &flow->actions, flow->actions_len);
1364 *ukey = ukey_create__(flow->key, flow->key_len,
1365 flow->mask, flow->mask_len, flow->ufid_present,
1366 &flow->ufid, flow->pmd_id, &actions, dump_seq,
1367 reval_seq, flow->stats.used);
1372 /* Attempts to insert a ukey into the shared ukey maps.
1374 * On success, returns true, installs the ukey and returns it in a locked
1375 * state. Otherwise, returns false. */
1377 ukey_install_start(struct udpif *udpif, struct udpif_key *new_ukey)
1378 OVS_TRY_LOCK(true, new_ukey->mutex)
1381 struct udpif_key *old_ukey;
1383 bool locked = false;
1385 idx = new_ukey->hash % N_UMAPS;
1386 umap = &udpif->ukeys[idx];
1387 ovs_mutex_lock(&umap->mutex);
1388 old_ukey = ukey_lookup(udpif, &new_ukey->ufid);
1390 /* Uncommon case: A ukey is already installed with the same UFID. */
1391 if (old_ukey->key_len == new_ukey->key_len
1392 && !memcmp(old_ukey->key, new_ukey->key, new_ukey->key_len)) {
1393 COVERAGE_INC(handler_duplicate_upcall);
1395 struct ds ds = DS_EMPTY_INITIALIZER;
1397 odp_format_ufid(&old_ukey->ufid, &ds);
1398 ds_put_cstr(&ds, " ");
1399 odp_flow_key_format(old_ukey->key, old_ukey->key_len, &ds);
1400 ds_put_cstr(&ds, "\n");
1401 odp_format_ufid(&new_ukey->ufid, &ds);
1402 ds_put_cstr(&ds, " ");
1403 odp_flow_key_format(new_ukey->key, new_ukey->key_len, &ds);
1405 VLOG_WARN_RL(&rl, "Conflicting ukey for flows:\n%s", ds_cstr(&ds));
1409 ovs_mutex_lock(&new_ukey->mutex);
1410 cmap_insert(&umap->cmap, &new_ukey->cmap_node, new_ukey->hash);
1413 ovs_mutex_unlock(&umap->mutex);
1419 ukey_install_finish__(struct udpif_key *ukey) OVS_REQUIRES(ukey->mutex)
1421 ukey->flow_exists = true;
1425 ukey_install_finish(struct udpif_key *ukey, int error)
1426 OVS_RELEASES(ukey->mutex)
1429 ukey_install_finish__(ukey);
1431 ovs_mutex_unlock(&ukey->mutex);
1437 ukey_install(struct udpif *udpif, struct udpif_key *ukey)
1439 /* The usual way to keep 'ukey->flow_exists' in sync with the datapath is
1440 * to call ukey_install_start(), install the corresponding datapath flow,
1441 * then call ukey_install_finish(). The netdev interface using upcall_cb()
1442 * doesn't provide a function to separately finish the flow installation,
1443 * so we perform the operations together here.
1445 * This is fine currently, as revalidator threads will only delete this
1446 * ukey during revalidator_sweep() and only if the dump_seq is mismatched.
1447 * It is unlikely for a revalidator thread to advance dump_seq and reach
1448 * the next GC phase between ukey creation and flow installation. */
1449 return ukey_install_start(udpif, ukey) && ukey_install_finish(ukey, 0);
1452 /* Searches for a ukey in 'udpif->ukeys' that matches 'flow' and attempts to
1453 * lock the ukey. If the ukey does not exist, create it.
1455 * Returns 0 on success, setting *result to the matching ukey and returning it
1456 * in a locked state. Otherwise, returns an errno and clears *result. EBUSY
1457 * indicates that another thread is handling this flow. Other errors indicate
1458 * an unexpected condition creating a new ukey.
1460 * *error is an output parameter provided to appease the threadsafety analyser,
1461 * and its value matches the return value. */
1463 ukey_acquire(struct udpif *udpif, const struct dpif_flow *flow,
1464 struct udpif_key **result, int *error)
1465 OVS_TRY_LOCK(0, (*result)->mutex)
1467 struct udpif_key *ukey;
1470 ukey = ukey_lookup(udpif, &flow->ufid);
1472 retval = ovs_mutex_trylock(&ukey->mutex);
1474 /* Usually we try to avoid installing flows from revalidator threads,
1475 * because locking on a umap may cause handler threads to block.
1476 * However there are certain cases, like when ovs-vswitchd is
1477 * restarted, where it is desirable to handle flows that exist in the
1478 * datapath gracefully (ie, don't just clear the datapath). */
1481 retval = ukey_create_from_dpif_flow(udpif, flow, &ukey);
1485 install = ukey_install_start(udpif, ukey);
1487 ukey_install_finish__(ukey);
1490 ukey_delete__(ukey);
1506 ukey_delete__(struct udpif_key *ukey)
1507 OVS_NO_THREAD_SAFETY_ANALYSIS
1510 xlate_cache_delete(ukey->xcache);
1511 ofpbuf_delete(ukey->actions);
1512 ovs_mutex_destroy(&ukey->mutex);
1518 ukey_delete(struct umap *umap, struct udpif_key *ukey)
1519 OVS_REQUIRES(umap->mutex)
1521 cmap_remove(&umap->cmap, &ukey->cmap_node, ukey->hash);
1522 ovsrcu_postpone(ukey_delete__, ukey);
1526 should_revalidate(const struct udpif *udpif, uint64_t packets,
1529 long long int metric, now, duration;
1531 if (udpif->dump_duration < 200) {
1532 /* We are likely to handle full revalidation for the flows. */
1536 /* Calculate the mean time between seeing these packets. If this
1537 * exceeds the threshold, then delete the flow rather than performing
1538 * costly revalidation for flows that aren't being hit frequently.
1540 * This is targeted at situations where the dump_duration is high (~1s),
1541 * and revalidation is triggered by a call to udpif_revalidate(). In
1542 * these situations, revalidation of all flows causes fluctuations in the
1543 * flow_limit due to the interaction with the dump_duration and max_idle.
1544 * This tends to result in deletion of low-throughput flows anyway, so
1545 * skip the revalidation and just delete those flows. */
1546 packets = MAX(packets, 1);
1547 now = MAX(used, time_msec());
1548 duration = now - used;
1549 metric = duration / packets;
1552 /* The flow is receiving more than ~5pps, so keep it. */
1559 revalidate_ukey(struct udpif *udpif, struct udpif_key *ukey,
1560 const struct dpif_flow_stats *stats, uint64_t reval_seq)
1561 OVS_REQUIRES(ukey->mutex)
1563 uint64_t slow_path_buf[128 / 8];
1564 struct xlate_out xout, *xoutp;
1565 struct netflow *netflow;
1566 struct ofproto_dpif *ofproto;
1567 struct dpif_flow_stats push;
1568 struct ofpbuf xout_actions;
1569 struct flow flow, dp_mask;
1570 uint64_t *dp64, *xout64;
1571 ofp_port_t ofp_in_port;
1572 struct xlate_in xin;
1573 long long int last_used;
1577 bool need_revalidate;
1583 need_revalidate = (ukey->reval_seq != reval_seq);
1584 last_used = ukey->stats.used;
1585 push.used = stats->used;
1586 push.tcp_flags = stats->tcp_flags;
1587 push.n_packets = (stats->n_packets > ukey->stats.n_packets
1588 ? stats->n_packets - ukey->stats.n_packets
1590 push.n_bytes = (stats->n_bytes > ukey->stats.n_bytes
1591 ? stats->n_bytes - ukey->stats.n_bytes
1594 if (need_revalidate && last_used
1595 && !should_revalidate(udpif, push.n_packets, last_used)) {
1600 /* We will push the stats, so update the ukey stats cache. */
1601 ukey->stats = *stats;
1602 if (!push.n_packets && !need_revalidate) {
1607 if (ukey->xcache && !need_revalidate) {
1608 xlate_push_stats(ukey->xcache, &push);
1613 if (odp_flow_key_to_flow(ukey->key, ukey->key_len, &flow)
1618 error = xlate_lookup(udpif->backer, &flow, &ofproto, NULL, NULL, &netflow,
1624 if (need_revalidate) {
1625 xlate_cache_clear(ukey->xcache);
1627 if (!ukey->xcache) {
1628 ukey->xcache = xlate_cache_new();
1631 xlate_in_init(&xin, ofproto, &flow, ofp_in_port, NULL, push.tcp_flags,
1633 if (push.n_packets) {
1634 xin.resubmit_stats = &push;
1635 xin.may_learn = true;
1637 xin.xcache = ukey->xcache;
1638 xin.skip_wildcards = !need_revalidate;
1639 xlate_actions(&xin, &xout);
1642 if (!need_revalidate) {
1648 ofpbuf_use_const(&xout_actions, xout.odp_actions->data,
1649 xout.odp_actions->size);
1651 ofpbuf_use_stack(&xout_actions, slow_path_buf, sizeof slow_path_buf);
1652 compose_slow_path(udpif, &xout, &flow, flow.in_port.odp_port,
1656 if (!ofpbuf_equal(&xout_actions, ukey->actions)) {
1660 if (odp_flow_key_to_mask(ukey->mask, ukey->mask_len, &dp_mask, &flow)
1665 /* Since the kernel is free to ignore wildcarded bits in the mask, we can't
1666 * directly check that the masks are the same. Instead we check that the
1667 * mask in the kernel is more specific i.e. less wildcarded, than what
1668 * we've calculated here. This guarantees we don't catch any packets we
1669 * shouldn't with the megaflow. */
1670 dp64 = (uint64_t *) &dp_mask;
1671 xout64 = (uint64_t *) &xout.wc.masks;
1672 for (i = 0; i < FLOW_U64S; i++) {
1673 if ((dp64[i] | xout64[i]) != dp64[i]) {
1682 ukey->reval_seq = reval_seq;
1684 if (netflow && !ok) {
1685 netflow_flow_clear(netflow, &flow);
1687 xlate_out_uninit(xoutp);
1692 delete_op_init__(struct udpif *udpif, struct ukey_op *op,
1693 const struct dpif_flow *flow)
1696 op->dop.type = DPIF_OP_FLOW_DEL;
1697 op->dop.u.flow_del.key = flow->key;
1698 op->dop.u.flow_del.key_len = flow->key_len;
1699 op->dop.u.flow_del.ufid = flow->ufid_present ? &flow->ufid : NULL;
1700 op->dop.u.flow_del.pmd_id = flow->pmd_id;
1701 op->dop.u.flow_del.stats = &op->stats;
1702 op->dop.u.flow_del.terse = udpif_use_ufid(udpif);
1706 delete_op_init(struct udpif *udpif, struct ukey_op *op, struct udpif_key *ukey)
1709 op->dop.type = DPIF_OP_FLOW_DEL;
1710 op->dop.u.flow_del.key = ukey->key;
1711 op->dop.u.flow_del.key_len = ukey->key_len;
1712 op->dop.u.flow_del.ufid = ukey->ufid_present ? &ukey->ufid : NULL;
1713 op->dop.u.flow_del.pmd_id = ukey->pmd_id;
1714 op->dop.u.flow_del.stats = &op->stats;
1715 op->dop.u.flow_del.terse = udpif_use_ufid(udpif);
1719 push_ukey_ops__(struct udpif *udpif, struct ukey_op *ops, size_t n_ops)
1721 struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
1724 ovs_assert(n_ops <= REVALIDATE_MAX_BATCH);
1725 for (i = 0; i < n_ops; i++) {
1726 opsp[i] = &ops[i].dop;
1728 dpif_operate(udpif->dpif, opsp, n_ops);
1730 for (i = 0; i < n_ops; i++) {
1731 struct ukey_op *op = &ops[i];
1732 struct dpif_flow_stats *push, *stats, push_buf;
1734 stats = op->dop.u.flow_del.stats;
1738 ovs_mutex_lock(&op->ukey->mutex);
1739 push->used = MAX(stats->used, op->ukey->stats.used);
1740 push->tcp_flags = stats->tcp_flags | op->ukey->stats.tcp_flags;
1741 push->n_packets = stats->n_packets - op->ukey->stats.n_packets;
1742 push->n_bytes = stats->n_bytes - op->ukey->stats.n_bytes;
1743 ovs_mutex_unlock(&op->ukey->mutex);
1748 if (push->n_packets || netflow_exists()) {
1749 const struct nlattr *key = op->dop.u.flow_del.key;
1750 size_t key_len = op->dop.u.flow_del.key_len;
1751 struct ofproto_dpif *ofproto;
1752 struct netflow *netflow;
1753 ofp_port_t ofp_in_port;
1758 ovs_mutex_lock(&op->ukey->mutex);
1759 if (op->ukey->xcache) {
1760 xlate_push_stats(op->ukey->xcache, push);
1761 ovs_mutex_unlock(&op->ukey->mutex);
1764 ovs_mutex_unlock(&op->ukey->mutex);
1765 key = op->ukey->key;
1766 key_len = op->ukey->key_len;
1769 if (odp_flow_key_to_flow(key, key_len, &flow)
1774 error = xlate_lookup(udpif->backer, &flow, &ofproto,
1775 NULL, NULL, &netflow, &ofp_in_port);
1777 struct xlate_in xin;
1779 xlate_in_init(&xin, ofproto, &flow, ofp_in_port, NULL,
1780 push->tcp_flags, NULL);
1781 xin.resubmit_stats = push->n_packets ? push : NULL;
1782 xin.may_learn = push->n_packets > 0;
1783 xin.skip_wildcards = true;
1784 xlate_actions_for_side_effects(&xin);
1787 netflow_flow_clear(netflow, &flow);
1795 push_ukey_ops(struct udpif *udpif, struct umap *umap,
1796 struct ukey_op *ops, size_t n_ops)
1800 push_ukey_ops__(udpif, ops, n_ops);
1801 ovs_mutex_lock(&umap->mutex);
1802 for (i = 0; i < n_ops; i++) {
1803 ukey_delete(umap, ops[i].ukey);
1805 ovs_mutex_unlock(&umap->mutex);
1809 log_unexpected_flow(const struct dpif_flow *flow, int error)
1811 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 60);
1812 struct ds ds = DS_EMPTY_INITIALIZER;
1814 ds_put_format(&ds, "Failed to acquire udpif_key corresponding to "
1815 "unexpected flow (%s): ", ovs_strerror(error));
1816 odp_format_ufid(&flow->ufid, &ds);
1817 VLOG_WARN_RL(&rl, "%s", ds_cstr(&ds));
1821 revalidate(struct revalidator *revalidator)
1823 struct udpif *udpif = revalidator->udpif;
1824 struct dpif_flow_dump_thread *dump_thread;
1825 uint64_t dump_seq, reval_seq;
1826 unsigned int flow_limit;
1828 dump_seq = seq_read(udpif->dump_seq);
1829 reval_seq = seq_read(udpif->reval_seq);
1830 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
1831 dump_thread = dpif_flow_dump_thread_create(udpif->dump);
1833 struct ukey_op ops[REVALIDATE_MAX_BATCH];
1836 struct dpif_flow flows[REVALIDATE_MAX_BATCH];
1837 const struct dpif_flow *f;
1840 long long int max_idle;
1845 n_dumped = dpif_flow_dump_next(dump_thread, flows, ARRAY_SIZE(flows));
1852 /* In normal operation we want to keep flows around until they have
1853 * been idle for 'ofproto_max_idle' milliseconds. However:
1855 * - If the number of datapath flows climbs above 'flow_limit',
1856 * drop that down to 100 ms to try to bring the flows down to
1859 * - If the number of datapath flows climbs above twice
1860 * 'flow_limit', delete all the datapath flows as an emergency
1861 * measure. (We reassess this condition for the next batch of
1862 * datapath flows, so we will recover before all the flows are
1864 n_dp_flows = udpif_get_n_flows(udpif);
1865 kill_them_all = n_dp_flows > flow_limit * 2;
1866 max_idle = n_dp_flows > flow_limit ? 100 : ofproto_max_idle;
1868 for (f = flows; f < &flows[n_dumped]; f++) {
1869 long long int used = f->stats.used;
1870 struct udpif_key *ukey;
1871 bool already_dumped, keep;
1874 if (ukey_acquire(udpif, f, &ukey, &error)) {
1875 if (error == EBUSY) {
1876 /* Another thread is processing this flow, so don't bother
1878 COVERAGE_INC(upcall_ukey_contention);
1880 log_unexpected_flow(f, error);
1881 if (error != ENOENT) {
1882 delete_op_init__(udpif, &ops[n_ops++], f);
1888 already_dumped = ukey->dump_seq == dump_seq;
1889 if (already_dumped) {
1890 /* The flow has already been handled during this flow dump
1891 * operation. Skip it. */
1893 COVERAGE_INC(dumped_duplicate_flow);
1895 COVERAGE_INC(dumped_new_flow);
1897 ovs_mutex_unlock(&ukey->mutex);
1902 used = ukey->created;
1904 if (kill_them_all || (used && used < now - max_idle)) {
1907 keep = revalidate_ukey(udpif, ukey, &f->stats, reval_seq);
1909 ukey->dump_seq = dump_seq;
1910 ukey->flow_exists = keep;
1913 delete_op_init(udpif, &ops[n_ops++], ukey);
1915 ovs_mutex_unlock(&ukey->mutex);
1919 push_ukey_ops__(udpif, ops, n_ops);
1923 dpif_flow_dump_thread_destroy(dump_thread);
1927 handle_missed_revalidation(struct udpif *udpif, uint64_t reval_seq,
1928 struct udpif_key *ukey)
1930 struct dpif_flow_stats stats;
1933 COVERAGE_INC(revalidate_missed_dp_flow);
1935 memset(&stats, 0, sizeof stats);
1936 ovs_mutex_lock(&ukey->mutex);
1937 keep = revalidate_ukey(udpif, ukey, &stats, reval_seq);
1938 ovs_mutex_unlock(&ukey->mutex);
1944 revalidator_sweep__(struct revalidator *revalidator, bool purge)
1946 struct udpif *udpif;
1947 uint64_t dump_seq, reval_seq;
1950 udpif = revalidator->udpif;
1951 dump_seq = seq_read(udpif->dump_seq);
1952 reval_seq = seq_read(udpif->reval_seq);
1953 slice = revalidator - udpif->revalidators;
1954 ovs_assert(slice < udpif->n_revalidators);
1956 for (int i = slice; i < N_UMAPS; i += udpif->n_revalidators) {
1957 struct ukey_op ops[REVALIDATE_MAX_BATCH];
1958 struct udpif_key *ukey;
1959 struct umap *umap = &udpif->ukeys[i];
1962 CMAP_FOR_EACH(ukey, cmap_node, &umap->cmap) {
1963 bool flow_exists, seq_mismatch;
1965 /* Handler threads could be holding a ukey lock while it installs a
1966 * new flow, so don't hang around waiting for access to it. */
1967 if (ovs_mutex_trylock(&ukey->mutex)) {
1970 flow_exists = ukey->flow_exists;
1971 seq_mismatch = (ukey->dump_seq != dump_seq
1972 && ukey->reval_seq != reval_seq);
1973 ovs_mutex_unlock(&ukey->mutex);
1978 && !handle_missed_revalidation(udpif, reval_seq,
1980 struct ukey_op *op = &ops[n_ops++];
1982 delete_op_init(udpif, op, ukey);
1983 if (n_ops == REVALIDATE_MAX_BATCH) {
1984 push_ukey_ops(udpif, umap, ops, n_ops);
1987 } else if (!flow_exists) {
1988 ovs_mutex_lock(&umap->mutex);
1989 ukey_delete(umap, ukey);
1990 ovs_mutex_unlock(&umap->mutex);
1995 push_ukey_ops(udpif, umap, ops, n_ops);
2002 revalidator_sweep(struct revalidator *revalidator)
2004 revalidator_sweep__(revalidator, false);
2008 revalidator_purge(struct revalidator *revalidator)
2010 revalidator_sweep__(revalidator, true);
2014 upcall_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
2015 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2017 struct ds ds = DS_EMPTY_INITIALIZER;
2018 struct udpif *udpif;
2020 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
2021 unsigned int flow_limit;
2025 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
2026 ufid_enabled = udpif_use_ufid(udpif);
2028 ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
2029 ds_put_format(&ds, "\tflows : (current %lu)"
2030 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
2031 udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
2032 ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
2033 ds_put_format(&ds, "\tufid enabled : ");
2035 ds_put_format(&ds, "true\n");
2037 ds_put_format(&ds, "false\n");
2039 ds_put_char(&ds, '\n');
2041 for (i = 0; i < n_revalidators; i++) {
2042 struct revalidator *revalidator = &udpif->revalidators[i];
2043 int j, elements = 0;
2045 for (j = i; j < N_UMAPS; j += n_revalidators) {
2046 elements += cmap_count(&udpif->ukeys[j].cmap);
2048 ds_put_format(&ds, "\t%u: (keys %d)\n", revalidator->id, elements);
2052 unixctl_command_reply(conn, ds_cstr(&ds));
2056 /* Disable using the megaflows.
2058 * This command is only needed for advanced debugging, so it's not
2059 * documented in the man page. */
2061 upcall_unixctl_disable_megaflows(struct unixctl_conn *conn,
2062 int argc OVS_UNUSED,
2063 const char *argv[] OVS_UNUSED,
2064 void *aux OVS_UNUSED)
2066 atomic_store_relaxed(&enable_megaflows, false);
2067 udpif_flush_all_datapaths();
2068 unixctl_command_reply(conn, "megaflows disabled");
2071 /* Re-enable using megaflows.
2073 * This command is only needed for advanced debugging, so it's not
2074 * documented in the man page. */
2076 upcall_unixctl_enable_megaflows(struct unixctl_conn *conn,
2077 int argc OVS_UNUSED,
2078 const char *argv[] OVS_UNUSED,
2079 void *aux OVS_UNUSED)
2081 atomic_store_relaxed(&enable_megaflows, true);
2082 udpif_flush_all_datapaths();
2083 unixctl_command_reply(conn, "megaflows enabled");
2086 /* Disable skipping flow attributes during flow dump.
2088 * This command is only needed for advanced debugging, so it's not
2089 * documented in the man page. */
2091 upcall_unixctl_disable_ufid(struct unixctl_conn *conn, int argc OVS_UNUSED,
2092 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2094 atomic_store_relaxed(&enable_ufid, false);
2095 unixctl_command_reply(conn, "Datapath dumping tersely using UFID disabled");
2098 /* Re-enable skipping flow attributes during flow dump.
2100 * This command is only needed for advanced debugging, so it's not documented
2101 * in the man page. */
2103 upcall_unixctl_enable_ufid(struct unixctl_conn *conn, int argc OVS_UNUSED,
2104 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2106 atomic_store_relaxed(&enable_ufid, true);
2107 unixctl_command_reply(conn, "Datapath dumping tersely using UFID enabled "
2108 "for supported datapaths");
2111 /* Set the flow limit.
2113 * This command is only needed for advanced debugging, so it's not
2114 * documented in the man page. */
2116 upcall_unixctl_set_flow_limit(struct unixctl_conn *conn,
2117 int argc OVS_UNUSED,
2118 const char *argv[] OVS_UNUSED,
2119 void *aux OVS_UNUSED)
2121 struct ds ds = DS_EMPTY_INITIALIZER;
2122 struct udpif *udpif;
2123 unsigned int flow_limit = atoi(argv[1]);
2125 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
2126 atomic_store_relaxed(&udpif->flow_limit, flow_limit);
2128 ds_put_format(&ds, "set flow_limit to %u\n", flow_limit);
2129 unixctl_command_reply(conn, ds_cstr(&ds));
2134 upcall_unixctl_dump_wait(struct unixctl_conn *conn,
2135 int argc OVS_UNUSED,
2136 const char *argv[] OVS_UNUSED,
2137 void *aux OVS_UNUSED)
2139 if (list_is_singleton(&all_udpifs)) {
2140 struct udpif *udpif = NULL;
2143 udpif = OBJECT_CONTAINING(list_front(&all_udpifs), udpif, list_node);
2144 len = (udpif->n_conns + 1) * sizeof *udpif->conns;
2145 udpif->conn_seq = seq_read(udpif->dump_seq);
2146 udpif->conns = xrealloc(udpif->conns, len);
2147 udpif->conns[udpif->n_conns++] = conn;
2149 unixctl_command_reply_error(conn, "can't wait on multiple udpifs.");
2154 upcall_unixctl_purge(struct unixctl_conn *conn, int argc OVS_UNUSED,
2155 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2157 struct udpif *udpif;
2159 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
2162 for (n = 0; n < udpif->n_revalidators; n++) {
2163 revalidator_purge(&udpif->revalidators[n]);
2166 unixctl_command_reply(conn, "");