]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/ofproto-dpif-upcall.c
tests: Add mirror-related keywords to all the mirroring tests.
[mirror_ovs.git] / ofproto / ofproto-dpif-upcall.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
2 *
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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. */
14
15 #include <config.h>
16 #include "ofproto-dpif-upcall.h"
17
18 #include <errno.h>
19 #include <stdbool.h>
20 #include <inttypes.h>
21
22 #include "connmgr.h"
23 #include "coverage.h"
24 #include "cmap.h"
25 #include "dpif.h"
26 #include "dynamic-string.h"
27 #include "fail-open.h"
28 #include "guarded-list.h"
29 #include "latch.h"
30 #include "list.h"
31 #include "netlink.h"
32 #include "ofpbuf.h"
33 #include "ofproto-dpif-ipfix.h"
34 #include "ofproto-dpif-sflow.h"
35 #include "ofproto-dpif-xlate.h"
36 #include "ovs-rcu.h"
37 #include "packets.h"
38 #include "poll-loop.h"
39 #include "seq.h"
40 #include "unixctl.h"
41 #include "openvswitch/vlog.h"
42
43 #define MAX_QUEUE_LENGTH 512
44 #define UPCALL_MAX_BATCH 64
45 #define REVALIDATE_MAX_BATCH 50
46
47 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall);
48
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);
54
55 /* A thread that reads upcalls from dpif, forwards each upcall's packet,
56 * and possibly sets up a kernel flow as a cache. */
57 struct handler {
58 struct udpif *udpif; /* Parent udpif. */
59 pthread_t thread; /* Thread ID. */
60 uint32_t handler_id; /* Handler id. */
61 };
62
63 /* In the absence of a multiple-writer multiple-reader datastructure for
64 * storing udpif_keys ("ukeys"), we use a large number of cmaps, each with its
65 * own lock for writing. */
66 #define N_UMAPS 512 /* per udpif. */
67 struct umap {
68 struct ovs_mutex mutex; /* Take for writing to the following. */
69 struct cmap cmap; /* Datapath flow keys. */
70 };
71
72 /* A thread that processes datapath flows, updates OpenFlow statistics, and
73 * updates or removes them if necessary.
74 *
75 * Revalidator threads operate in two phases: "dump" and "sweep". In between
76 * each phase, all revalidators sync up so that all revalidator threads are
77 * either in one phase or the other, but not a combination.
78 *
79 * During the dump phase, revalidators fetch flows from the datapath and
80 * attribute the statistics to OpenFlow rules. Each datapath flow has a
81 * corresponding ukey which caches the most recently seen statistics. If
82 * a flow needs to be deleted (for example, because it is unused over a
83 * period of time), revalidator threads may delete the flow during the
84 * dump phase. The datapath is not guaranteed to reliably dump all flows
85 * from the datapath, and there is no mapping between datapath flows to
86 * revalidators, so a particular flow may be handled by zero or more
87 * revalidators during a single dump phase. To avoid duplicate attribution
88 * of statistics, ukeys are never deleted during this phase.
89 *
90 * During the sweep phase, each revalidator takes ownership of a different
91 * slice of umaps and sweeps through all ukeys in those umaps to figure out
92 * whether they need to be deleted. During this phase, revalidators may
93 * fetch individual flows which were not dumped during the dump phase to
94 * validate them and attribute statistics.
95 */
96 struct revalidator {
97 struct udpif *udpif; /* Parent udpif. */
98 pthread_t thread; /* Thread ID. */
99 unsigned int id; /* ovsthread_id_self(). */
100 };
101
102 /* An upcall handler for ofproto_dpif.
103 *
104 * udpif keeps records of two kind of logically separate units:
105 *
106 * upcall handling
107 * ---------------
108 *
109 * - An array of 'struct handler's for upcall handling and flow
110 * installation.
111 *
112 * flow revalidation
113 * -----------------
114 *
115 * - Revalidation threads which read the datapath flow table and maintains
116 * them.
117 */
118 struct udpif {
119 struct ovs_list list_node; /* In all_udpifs list. */
120
121 struct dpif *dpif; /* Datapath handle. */
122 struct dpif_backer *backer; /* Opaque dpif_backer pointer. */
123
124 struct handler *handlers; /* Upcall handlers. */
125 size_t n_handlers;
126
127 struct revalidator *revalidators; /* Flow revalidators. */
128 size_t n_revalidators;
129
130 struct latch exit_latch; /* Tells child threads to exit. */
131
132 /* Revalidation. */
133 struct seq *reval_seq; /* Incremented to force revalidation. */
134 bool reval_exit; /* Set by leader on 'exit_latch. */
135 struct ovs_barrier reval_barrier; /* Barrier used by revalidators. */
136 struct dpif_flow_dump *dump; /* DPIF flow dump state. */
137 long long int dump_duration; /* Duration of the last flow dump. */
138 struct seq *dump_seq; /* Increments each dump iteration. */
139 atomic_bool enable_ufid; /* If true, skip dumping flow attrs. */
140
141 /* These variables provide a mechanism for the main thread to pause
142 * all revalidation without having to completely shut the threads down.
143 * 'pause_latch' is shared between the main thread and the lead
144 * revalidator thread, so when it is desirable to halt revalidation, the
145 * main thread will set the latch. 'pause' and 'pause_barrier' are shared
146 * by revalidator threads. The lead revalidator will set 'pause' when it
147 * observes the latch has been set, and this will cause all revalidator
148 * threads to wait on 'pause_barrier' at the beginning of the next
149 * revalidation round. */
150 bool pause; /* Set by leader on 'pause_latch. */
151 struct latch pause_latch; /* Set to force revalidators pause. */
152 struct ovs_barrier pause_barrier; /* Barrier used to pause all */
153 /* revalidators by main thread. */
154
155 /* There are 'N_UMAPS' maps containing 'struct udpif_key' elements.
156 *
157 * During the flow dump phase, revalidators insert into these with a random
158 * distribution. During the garbage collection phase, each revalidator
159 * takes care of garbage collecting a slice of these maps. */
160 struct umap *ukeys;
161
162 /* Datapath flow statistics. */
163 unsigned int max_n_flows;
164 unsigned int avg_n_flows;
165
166 /* Following fields are accessed and modified by different threads. */
167 atomic_uint flow_limit; /* Datapath flow hard limit. */
168
169 /* n_flows_mutex prevents multiple threads updating these concurrently. */
170 atomic_uint n_flows; /* Number of flows in the datapath. */
171 atomic_llong n_flows_timestamp; /* Last time n_flows was updated. */
172 struct ovs_mutex n_flows_mutex;
173
174 /* Following fields are accessed and modified only from the main thread. */
175 struct unixctl_conn **conns; /* Connections waiting on dump_seq. */
176 uint64_t conn_seq; /* Corresponds to 'dump_seq' when
177 conns[n_conns-1] was stored. */
178 size_t n_conns; /* Number of connections waiting. */
179 };
180
181 enum upcall_type {
182 BAD_UPCALL, /* Some kind of bug somewhere. */
183 MISS_UPCALL, /* A flow miss. */
184 SFLOW_UPCALL, /* sFlow sample. */
185 FLOW_SAMPLE_UPCALL, /* Per-flow sampling. */
186 IPFIX_UPCALL /* Per-bridge sampling. */
187 };
188
189 enum reval_result {
190 UKEY_KEEP,
191 UKEY_DELETE,
192 UKEY_MODIFY
193 };
194
195 struct upcall {
196 struct ofproto_dpif *ofproto; /* Parent ofproto. */
197 const struct recirc_id_node *recirc; /* Recirculation context. */
198 bool have_recirc_ref; /* Reference held on recirc ctx? */
199
200 /* The flow and packet are only required to be constant when using
201 * dpif-netdev. If a modification is absolutely necessary, a const cast
202 * may be used with other datapaths. */
203 const struct flow *flow; /* Parsed representation of the packet. */
204 const ovs_u128 *ufid; /* Unique identifier for 'flow'. */
205 unsigned pmd_id; /* Datapath poll mode driver id. */
206 const struct dp_packet *packet; /* Packet associated with this upcall. */
207 ofp_port_t in_port; /* OpenFlow in port, or OFPP_NONE. */
208 uint16_t mru; /* If !0, Maximum receive unit of
209 fragmented IP packet */
210
211 enum dpif_upcall_type type; /* Datapath type of the upcall. */
212 const struct nlattr *userdata; /* Userdata for DPIF_UC_ACTION Upcalls. */
213 const struct nlattr *actions; /* Flow actions in DPIF_UC_ACTION Upcalls. */
214
215 bool xout_initialized; /* True if 'xout' must be uninitialized. */
216 struct xlate_out xout; /* Result of xlate_actions(). */
217 struct ofpbuf odp_actions; /* Datapath actions from xlate_actions(). */
218 struct flow_wildcards wc; /* Dependencies that megaflow must match. */
219 struct ofpbuf put_actions; /* Actions 'put' in the fastpath. */
220
221 struct dpif_ipfix *ipfix; /* IPFIX pointer or NULL. */
222 struct dpif_sflow *sflow; /* SFlow pointer or NULL. */
223
224 bool vsp_adjusted; /* 'packet' and 'flow' were adjusted for
225 VLAN splinters if true. */
226
227 struct udpif_key *ukey; /* Revalidator flow cache. */
228 bool ukey_persists; /* Set true to keep 'ukey' beyond the
229 lifetime of this upcall. */
230
231 uint64_t dump_seq; /* udpif->dump_seq at translation time. */
232 uint64_t reval_seq; /* udpif->reval_seq at translation time. */
233
234 /* Not used by the upcall callback interface. */
235 const struct nlattr *key; /* Datapath flow key. */
236 size_t key_len; /* Datapath flow key length. */
237 const struct nlattr *out_tun_key; /* Datapath output tunnel key. */
238
239 uint64_t odp_actions_stub[1024 / 8]; /* Stub for odp_actions. */
240 };
241
242 /* 'udpif_key's are responsible for tracking the little bit of state udpif
243 * needs to do flow expiration which can't be pulled directly from the
244 * datapath. They may be created by any handler or revalidator thread at any
245 * time, and read by any revalidator during the dump phase. They are however
246 * each owned by a single revalidator which takes care of destroying them
247 * during the garbage-collection phase.
248 *
249 * The mutex within the ukey protects some members of the ukey. The ukey
250 * itself is protected by RCU and is held within a umap in the parent udpif.
251 * Adding or removing a ukey from a umap is only safe when holding the
252 * corresponding umap lock. */
253 struct udpif_key {
254 struct cmap_node cmap_node; /* In parent revalidator 'ukeys' map. */
255
256 /* These elements are read only once created, and therefore aren't
257 * protected by a mutex. */
258 const struct nlattr *key; /* Datapath flow key. */
259 size_t key_len; /* Length of 'key'. */
260 const struct nlattr *mask; /* Datapath flow mask. */
261 size_t mask_len; /* Length of 'mask'. */
262 ovs_u128 ufid; /* Unique flow identifier. */
263 bool ufid_present; /* True if 'ufid' is in datapath. */
264 uint32_t hash; /* Pre-computed hash for 'key'. */
265 unsigned pmd_id; /* Datapath poll mode driver id. */
266
267 struct ovs_mutex mutex; /* Guards the following. */
268 struct dpif_flow_stats stats OVS_GUARDED; /* Last known stats.*/
269 long long int created OVS_GUARDED; /* Estimate of creation time. */
270 uint64_t dump_seq OVS_GUARDED; /* Tracks udpif->dump_seq. */
271 uint64_t reval_seq OVS_GUARDED; /* Tracks udpif->reval_seq. */
272 bool flow_exists OVS_GUARDED; /* Ensures flows are only deleted
273 once. */
274 /* Datapath flow actions as nlattrs. Protected by RCU. Read with
275 * ukey_get_actions(), and write with ukey_set_actions(). */
276 OVSRCU_TYPE(struct ofpbuf *) actions;
277
278 struct xlate_cache *xcache OVS_GUARDED; /* Cache for xlate entries that
279 * are affected by this ukey.
280 * Used for stats and learning.*/
281 union {
282 struct odputil_keybuf buf;
283 struct nlattr nla;
284 } keybuf, maskbuf;
285
286 uint32_t key_recirc_id; /* Non-zero if reference is held by the ukey. */
287 struct recirc_refs recircs; /* Action recirc IDs with references held. */
288 };
289
290 /* Datapath operation with optional ukey attached. */
291 struct ukey_op {
292 struct udpif_key *ukey;
293 struct dpif_flow_stats stats; /* Stats for 'op'. */
294 struct dpif_op dop; /* Flow operation. */
295 };
296
297 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
298 static struct ovs_list all_udpifs = OVS_LIST_INITIALIZER(&all_udpifs);
299
300 static size_t recv_upcalls(struct handler *);
301 static int process_upcall(struct udpif *, struct upcall *,
302 struct ofpbuf *odp_actions, struct flow_wildcards *);
303 static void handle_upcalls(struct udpif *, struct upcall *, size_t n_upcalls);
304 static void udpif_stop_threads(struct udpif *);
305 static void udpif_start_threads(struct udpif *, size_t n_handlers,
306 size_t n_revalidators);
307 static void udpif_pause_revalidators(struct udpif *);
308 static void udpif_resume_revalidators(struct udpif *);
309 static void *udpif_upcall_handler(void *);
310 static void *udpif_revalidator(void *);
311 static unsigned long udpif_get_n_flows(struct udpif *);
312 static void revalidate(struct revalidator *);
313 static void revalidator_pause(struct revalidator *);
314 static void revalidator_sweep(struct revalidator *);
315 static void revalidator_purge(struct revalidator *);
316 static void upcall_unixctl_show(struct unixctl_conn *conn, int argc,
317 const char *argv[], void *aux);
318 static void upcall_unixctl_disable_megaflows(struct unixctl_conn *, int argc,
319 const char *argv[], void *aux);
320 static void upcall_unixctl_enable_megaflows(struct unixctl_conn *, int argc,
321 const char *argv[], void *aux);
322 static void upcall_unixctl_disable_ufid(struct unixctl_conn *, int argc,
323 const char *argv[], void *aux);
324 static void upcall_unixctl_enable_ufid(struct unixctl_conn *, int argc,
325 const char *argv[], void *aux);
326 static void upcall_unixctl_set_flow_limit(struct unixctl_conn *conn, int argc,
327 const char *argv[], void *aux);
328 static void upcall_unixctl_dump_wait(struct unixctl_conn *conn, int argc,
329 const char *argv[], void *aux);
330 static void upcall_unixctl_purge(struct unixctl_conn *conn, int argc,
331 const char *argv[], void *aux);
332
333 static struct udpif_key *ukey_create_from_upcall(struct upcall *,
334 struct flow_wildcards *);
335 static int ukey_create_from_dpif_flow(const struct udpif *,
336 const struct dpif_flow *,
337 struct udpif_key **);
338 static void ukey_get_actions(struct udpif_key *, const struct nlattr **actions,
339 size_t *size);
340 static bool ukey_install_start(struct udpif *, struct udpif_key *ukey);
341 static bool ukey_install_finish(struct udpif_key *ukey, int error);
342 static bool ukey_install(struct udpif *udpif, struct udpif_key *ukey);
343 static struct udpif_key *ukey_lookup(struct udpif *udpif,
344 const ovs_u128 *ufid,
345 const unsigned pmd_id);
346 static int ukey_acquire(struct udpif *, const struct dpif_flow *,
347 struct udpif_key **result, int *error);
348 static void ukey_delete__(struct udpif_key *);
349 static void ukey_delete(struct umap *, struct udpif_key *);
350 static enum upcall_type classify_upcall(enum dpif_upcall_type type,
351 const struct nlattr *userdata);
352
353 static int upcall_receive(struct upcall *, const struct dpif_backer *,
354 const struct dp_packet *packet, enum dpif_upcall_type,
355 const struct nlattr *userdata, const struct flow *,
356 const unsigned int mru,
357 const ovs_u128 *ufid, const unsigned pmd_id);
358 static void upcall_uninit(struct upcall *);
359
360 static upcall_callback upcall_cb;
361 static dp_purge_callback dp_purge_cb;
362
363 static atomic_bool enable_megaflows = ATOMIC_VAR_INIT(true);
364 static atomic_bool enable_ufid = ATOMIC_VAR_INIT(true);
365
366 void
367 udpif_init(void)
368 {
369 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
370 if (ovsthread_once_start(&once)) {
371 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show,
372 NULL);
373 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
374 upcall_unixctl_disable_megaflows, NULL);
375 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
376 upcall_unixctl_enable_megaflows, NULL);
377 unixctl_command_register("upcall/disable-ufid", "", 0, 0,
378 upcall_unixctl_disable_ufid, NULL);
379 unixctl_command_register("upcall/enable-ufid", "", 0, 0,
380 upcall_unixctl_enable_ufid, NULL);
381 unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
382 upcall_unixctl_set_flow_limit, NULL);
383 unixctl_command_register("revalidator/wait", "", 0, 0,
384 upcall_unixctl_dump_wait, NULL);
385 unixctl_command_register("revalidator/purge", "", 0, 0,
386 upcall_unixctl_purge, NULL);
387 ovsthread_once_done(&once);
388 }
389 }
390
391 struct udpif *
392 udpif_create(struct dpif_backer *backer, struct dpif *dpif)
393 {
394 struct udpif *udpif = xzalloc(sizeof *udpif);
395
396 udpif->dpif = dpif;
397 udpif->backer = backer;
398 atomic_init(&udpif->flow_limit, MIN(ofproto_flow_limit, 10000));
399 udpif->reval_seq = seq_create();
400 udpif->dump_seq = seq_create();
401 latch_init(&udpif->exit_latch);
402 latch_init(&udpif->pause_latch);
403 list_push_back(&all_udpifs, &udpif->list_node);
404 atomic_init(&udpif->enable_ufid, false);
405 atomic_init(&udpif->n_flows, 0);
406 atomic_init(&udpif->n_flows_timestamp, LLONG_MIN);
407 ovs_mutex_init(&udpif->n_flows_mutex);
408 udpif->ukeys = xmalloc(N_UMAPS * sizeof *udpif->ukeys);
409 for (int i = 0; i < N_UMAPS; i++) {
410 cmap_init(&udpif->ukeys[i].cmap);
411 ovs_mutex_init(&udpif->ukeys[i].mutex);
412 }
413
414 dpif_register_upcall_cb(dpif, upcall_cb, udpif);
415 dpif_register_dp_purge_cb(dpif, dp_purge_cb, udpif);
416
417 return udpif;
418 }
419
420 void
421 udpif_run(struct udpif *udpif)
422 {
423 if (udpif->conns && udpif->conn_seq != seq_read(udpif->dump_seq)) {
424 int i;
425
426 for (i = 0; i < udpif->n_conns; i++) {
427 unixctl_command_reply(udpif->conns[i], NULL);
428 }
429 free(udpif->conns);
430 udpif->conns = NULL;
431 udpif->n_conns = 0;
432 }
433 }
434
435 void
436 udpif_destroy(struct udpif *udpif)
437 {
438 udpif_stop_threads(udpif);
439
440 for (int i = 0; i < N_UMAPS; i++) {
441 cmap_destroy(&udpif->ukeys[i].cmap);
442 ovs_mutex_destroy(&udpif->ukeys[i].mutex);
443 }
444 free(udpif->ukeys);
445 udpif->ukeys = NULL;
446
447 list_remove(&udpif->list_node);
448 latch_destroy(&udpif->exit_latch);
449 latch_destroy(&udpif->pause_latch);
450 seq_destroy(udpif->reval_seq);
451 seq_destroy(udpif->dump_seq);
452 ovs_mutex_destroy(&udpif->n_flows_mutex);
453 free(udpif);
454 }
455
456 /* Stops the handler and revalidator threads, must be enclosed in
457 * ovsrcu quiescent state unless when destroying udpif. */
458 static void
459 udpif_stop_threads(struct udpif *udpif)
460 {
461 if (udpif && (udpif->n_handlers != 0 || udpif->n_revalidators != 0)) {
462 size_t i;
463
464 latch_set(&udpif->exit_latch);
465
466 for (i = 0; i < udpif->n_handlers; i++) {
467 struct handler *handler = &udpif->handlers[i];
468
469 xpthread_join(handler->thread, NULL);
470 }
471
472 for (i = 0; i < udpif->n_revalidators; i++) {
473 xpthread_join(udpif->revalidators[i].thread, NULL);
474 }
475
476 dpif_disable_upcall(udpif->dpif);
477
478 for (i = 0; i < udpif->n_revalidators; i++) {
479 struct revalidator *revalidator = &udpif->revalidators[i];
480
481 /* Delete ukeys, and delete all flows from the datapath to prevent
482 * double-counting stats. */
483 revalidator_purge(revalidator);
484 }
485
486 latch_poll(&udpif->exit_latch);
487
488 ovs_barrier_destroy(&udpif->reval_barrier);
489 ovs_barrier_destroy(&udpif->pause_barrier);
490
491 free(udpif->revalidators);
492 udpif->revalidators = NULL;
493 udpif->n_revalidators = 0;
494
495 free(udpif->handlers);
496 udpif->handlers = NULL;
497 udpif->n_handlers = 0;
498 }
499 }
500
501 /* Starts the handler and revalidator threads, must be enclosed in
502 * ovsrcu quiescent state. */
503 static void
504 udpif_start_threads(struct udpif *udpif, size_t n_handlers,
505 size_t n_revalidators)
506 {
507 if (udpif && n_handlers && n_revalidators) {
508 size_t i;
509 bool enable_ufid;
510
511 udpif->n_handlers = n_handlers;
512 udpif->n_revalidators = n_revalidators;
513
514 udpif->handlers = xzalloc(udpif->n_handlers * sizeof *udpif->handlers);
515 for (i = 0; i < udpif->n_handlers; i++) {
516 struct handler *handler = &udpif->handlers[i];
517
518 handler->udpif = udpif;
519 handler->handler_id = i;
520 handler->thread = ovs_thread_create(
521 "handler", udpif_upcall_handler, handler);
522 }
523
524 enable_ufid = ofproto_dpif_get_enable_ufid(udpif->backer);
525 atomic_init(&udpif->enable_ufid, enable_ufid);
526 dpif_enable_upcall(udpif->dpif);
527
528 ovs_barrier_init(&udpif->reval_barrier, udpif->n_revalidators);
529 ovs_barrier_init(&udpif->pause_barrier, udpif->n_revalidators + 1);
530 udpif->reval_exit = false;
531 udpif->pause = false;
532 udpif->revalidators = xzalloc(udpif->n_revalidators
533 * sizeof *udpif->revalidators);
534 for (i = 0; i < udpif->n_revalidators; i++) {
535 struct revalidator *revalidator = &udpif->revalidators[i];
536
537 revalidator->udpif = udpif;
538 revalidator->thread = ovs_thread_create(
539 "revalidator", udpif_revalidator, revalidator);
540 }
541 }
542 }
543
544 /* Pauses all revalidators. Should only be called by the main thread.
545 * When function returns, all revalidators are paused and will proceed
546 * only after udpif_resume_revalidators() is called. */
547 static void
548 udpif_pause_revalidators(struct udpif *udpif)
549 {
550 if (ofproto_dpif_backer_enabled(udpif->backer)) {
551 latch_set(&udpif->pause_latch);
552 ovs_barrier_block(&udpif->pause_barrier);
553 }
554 }
555
556 /* Resumes the pausing of revalidators. Should only be called by the
557 * main thread. */
558 static void
559 udpif_resume_revalidators(struct udpif *udpif)
560 {
561 if (ofproto_dpif_backer_enabled(udpif->backer)) {
562 latch_poll(&udpif->pause_latch);
563 ovs_barrier_block(&udpif->pause_barrier);
564 }
565 }
566
567 /* Tells 'udpif' how many threads it should use to handle upcalls.
568 * 'n_handlers' and 'n_revalidators' can never be zero. 'udpif''s
569 * datapath handle must have packet reception enabled before starting
570 * threads. */
571 void
572 udpif_set_threads(struct udpif *udpif, size_t n_handlers,
573 size_t n_revalidators)
574 {
575 ovs_assert(udpif);
576 ovs_assert(n_handlers && n_revalidators);
577
578 ovsrcu_quiesce_start();
579 if (udpif->n_handlers != n_handlers
580 || udpif->n_revalidators != n_revalidators) {
581 udpif_stop_threads(udpif);
582 }
583
584 if (!udpif->handlers && !udpif->revalidators) {
585 int error;
586
587 error = dpif_handlers_set(udpif->dpif, n_handlers);
588 if (error) {
589 VLOG_ERR("failed to configure handlers in dpif %s: %s",
590 dpif_name(udpif->dpif), ovs_strerror(error));
591 return;
592 }
593
594 udpif_start_threads(udpif, n_handlers, n_revalidators);
595 }
596 ovsrcu_quiesce_end();
597 }
598
599 /* Waits for all ongoing upcall translations to complete. This ensures that
600 * there are no transient references to any removed ofprotos (or other
601 * objects). In particular, this should be called after an ofproto is removed
602 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
603 void
604 udpif_synchronize(struct udpif *udpif)
605 {
606 /* This is stronger than necessary. It would be sufficient to ensure
607 * (somehow) that each handler and revalidator thread had passed through
608 * its main loop once. */
609 size_t n_handlers = udpif->n_handlers;
610 size_t n_revalidators = udpif->n_revalidators;
611
612 ovsrcu_quiesce_start();
613 udpif_stop_threads(udpif);
614 udpif_start_threads(udpif, n_handlers, n_revalidators);
615 ovsrcu_quiesce_end();
616 }
617
618 /* Notifies 'udpif' that something changed which may render previous
619 * xlate_actions() results invalid. */
620 void
621 udpif_revalidate(struct udpif *udpif)
622 {
623 seq_change(udpif->reval_seq);
624 }
625
626 /* Returns a seq which increments every time 'udpif' pulls stats from the
627 * datapath. Callers can use this to get a sense of when might be a good time
628 * to do periodic work which relies on relatively up to date statistics. */
629 struct seq *
630 udpif_dump_seq(struct udpif *udpif)
631 {
632 return udpif->dump_seq;
633 }
634
635 void
636 udpif_get_memory_usage(struct udpif *udpif, struct simap *usage)
637 {
638 size_t i;
639
640 simap_increase(usage, "handlers", udpif->n_handlers);
641
642 simap_increase(usage, "revalidators", udpif->n_revalidators);
643 for (i = 0; i < N_UMAPS; i++) {
644 simap_increase(usage, "udpif keys", cmap_count(&udpif->ukeys[i].cmap));
645 }
646 }
647
648 /* Remove flows from a single datapath. */
649 void
650 udpif_flush(struct udpif *udpif)
651 {
652 size_t n_handlers, n_revalidators;
653
654 n_handlers = udpif->n_handlers;
655 n_revalidators = udpif->n_revalidators;
656
657 ovsrcu_quiesce_start();
658
659 udpif_stop_threads(udpif);
660 dpif_flow_flush(udpif->dpif);
661 udpif_start_threads(udpif, n_handlers, n_revalidators);
662
663 ovsrcu_quiesce_end();
664 }
665
666 /* Removes all flows from all datapaths. */
667 static void
668 udpif_flush_all_datapaths(void)
669 {
670 struct udpif *udpif;
671
672 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
673 udpif_flush(udpif);
674 }
675 }
676
677 static bool
678 udpif_use_ufid(struct udpif *udpif)
679 {
680 bool enable;
681
682 atomic_read_relaxed(&enable_ufid, &enable);
683 return enable && ofproto_dpif_get_enable_ufid(udpif->backer);
684 }
685
686 \f
687 static unsigned long
688 udpif_get_n_flows(struct udpif *udpif)
689 {
690 long long int time, now;
691 unsigned long flow_count;
692
693 now = time_msec();
694 atomic_read_relaxed(&udpif->n_flows_timestamp, &time);
695 if (time < now - 100 && !ovs_mutex_trylock(&udpif->n_flows_mutex)) {
696 struct dpif_dp_stats stats;
697
698 atomic_store_relaxed(&udpif->n_flows_timestamp, now);
699 dpif_get_dp_stats(udpif->dpif, &stats);
700 flow_count = stats.n_flows;
701 atomic_store_relaxed(&udpif->n_flows, flow_count);
702 ovs_mutex_unlock(&udpif->n_flows_mutex);
703 } else {
704 atomic_read_relaxed(&udpif->n_flows, &flow_count);
705 }
706 return flow_count;
707 }
708
709 /* The upcall handler thread tries to read a batch of UPCALL_MAX_BATCH
710 * upcalls from dpif, processes the batch and installs corresponding flows
711 * in dpif. */
712 static void *
713 udpif_upcall_handler(void *arg)
714 {
715 struct handler *handler = arg;
716 struct udpif *udpif = handler->udpif;
717
718 while (!latch_is_set(&handler->udpif->exit_latch)) {
719 if (recv_upcalls(handler)) {
720 poll_immediate_wake();
721 } else {
722 dpif_recv_wait(udpif->dpif, handler->handler_id);
723 latch_wait(&udpif->exit_latch);
724 }
725 poll_block();
726 }
727
728 return NULL;
729 }
730
731 static size_t
732 recv_upcalls(struct handler *handler)
733 {
734 struct udpif *udpif = handler->udpif;
735 uint64_t recv_stubs[UPCALL_MAX_BATCH][512 / 8];
736 struct ofpbuf recv_bufs[UPCALL_MAX_BATCH];
737 struct dpif_upcall dupcalls[UPCALL_MAX_BATCH];
738 struct upcall upcalls[UPCALL_MAX_BATCH];
739 struct flow flows[UPCALL_MAX_BATCH];
740 size_t n_upcalls, i;
741
742 n_upcalls = 0;
743 while (n_upcalls < UPCALL_MAX_BATCH) {
744 struct ofpbuf *recv_buf = &recv_bufs[n_upcalls];
745 struct dpif_upcall *dupcall = &dupcalls[n_upcalls];
746 struct upcall *upcall = &upcalls[n_upcalls];
747 struct flow *flow = &flows[n_upcalls];
748 unsigned int mru;
749 int error;
750
751 ofpbuf_use_stub(recv_buf, recv_stubs[n_upcalls],
752 sizeof recv_stubs[n_upcalls]);
753 if (dpif_recv(udpif->dpif, handler->handler_id, dupcall, recv_buf)) {
754 ofpbuf_uninit(recv_buf);
755 break;
756 }
757
758 if (odp_flow_key_to_flow(dupcall->key, dupcall->key_len, flow)
759 == ODP_FIT_ERROR) {
760 goto free_dupcall;
761 }
762
763 if (dupcall->mru) {
764 mru = nl_attr_get_u16(dupcall->mru);
765 } else {
766 mru = 0;
767 }
768
769 error = upcall_receive(upcall, udpif->backer, &dupcall->packet,
770 dupcall->type, dupcall->userdata, flow, mru,
771 &dupcall->ufid, PMD_ID_NULL);
772 if (error) {
773 if (error == ENODEV) {
774 /* Received packet on datapath port for which we couldn't
775 * associate an ofproto. This can happen if a port is removed
776 * while traffic is being received. Print a rate-limited
777 * message in case it happens frequently. */
778 dpif_flow_put(udpif->dpif, DPIF_FP_CREATE, dupcall->key,
779 dupcall->key_len, NULL, 0, NULL, 0,
780 &dupcall->ufid, PMD_ID_NULL, NULL);
781 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
782 "port %"PRIu32, flow->in_port.odp_port);
783 }
784 goto free_dupcall;
785 }
786
787 upcall->key = dupcall->key;
788 upcall->key_len = dupcall->key_len;
789 upcall->ufid = &dupcall->ufid;
790
791 upcall->out_tun_key = dupcall->out_tun_key;
792 upcall->actions = dupcall->actions;
793
794 if (vsp_adjust_flow(upcall->ofproto, flow, &dupcall->packet)) {
795 upcall->vsp_adjusted = true;
796 }
797
798 pkt_metadata_from_flow(&dupcall->packet.md, flow);
799 flow_extract(&dupcall->packet, flow);
800
801 error = process_upcall(udpif, upcall,
802 &upcall->odp_actions, &upcall->wc);
803 if (error) {
804 goto cleanup;
805 }
806
807 n_upcalls++;
808 continue;
809
810 cleanup:
811 upcall_uninit(upcall);
812 free_dupcall:
813 dp_packet_uninit(&dupcall->packet);
814 ofpbuf_uninit(recv_buf);
815 }
816
817 if (n_upcalls) {
818 handle_upcalls(handler->udpif, upcalls, n_upcalls);
819 for (i = 0; i < n_upcalls; i++) {
820 dp_packet_uninit(&dupcalls[i].packet);
821 ofpbuf_uninit(&recv_bufs[i]);
822 upcall_uninit(&upcalls[i]);
823 }
824 }
825
826 return n_upcalls;
827 }
828
829 static void *
830 udpif_revalidator(void *arg)
831 {
832 /* Used by all revalidators. */
833 struct revalidator *revalidator = arg;
834 struct udpif *udpif = revalidator->udpif;
835 bool leader = revalidator == &udpif->revalidators[0];
836
837 /* Used only by the leader. */
838 long long int start_time = 0;
839 uint64_t last_reval_seq = 0;
840 size_t n_flows = 0;
841
842 revalidator->id = ovsthread_id_self();
843 for (;;) {
844 if (leader) {
845 uint64_t reval_seq;
846
847 recirc_run(); /* Recirculation cleanup. */
848
849 reval_seq = seq_read(udpif->reval_seq);
850 last_reval_seq = reval_seq;
851
852 n_flows = udpif_get_n_flows(udpif);
853 udpif->max_n_flows = MAX(n_flows, udpif->max_n_flows);
854 udpif->avg_n_flows = (udpif->avg_n_flows + n_flows) / 2;
855
856 /* Only the leader checks the pause latch to prevent a race where
857 * some threads think it's false and proceed to block on
858 * reval_barrier and others think it's true and block indefinitely
859 * on the pause_barrier */
860 udpif->pause = latch_is_set(&udpif->pause_latch);
861
862 /* Only the leader checks the exit latch to prevent a race where
863 * some threads think it's true and exit and others think it's
864 * false and block indefinitely on the reval_barrier */
865 udpif->reval_exit = latch_is_set(&udpif->exit_latch);
866
867 start_time = time_msec();
868 if (!udpif->reval_exit) {
869 bool terse_dump;
870
871 terse_dump = udpif_use_ufid(udpif);
872 udpif->dump = dpif_flow_dump_create(udpif->dpif, terse_dump);
873 }
874 }
875
876 /* Wait for the leader to start the flow dump. */
877 ovs_barrier_block(&udpif->reval_barrier);
878 if (udpif->pause) {
879 revalidator_pause(revalidator);
880 }
881
882 if (udpif->reval_exit) {
883 break;
884 }
885 revalidate(revalidator);
886
887 /* Wait for all flows to have been dumped before we garbage collect. */
888 ovs_barrier_block(&udpif->reval_barrier);
889 revalidator_sweep(revalidator);
890
891 /* Wait for all revalidators to finish garbage collection. */
892 ovs_barrier_block(&udpif->reval_barrier);
893
894 if (leader) {
895 unsigned int flow_limit;
896 long long int duration;
897
898 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
899
900 dpif_flow_dump_destroy(udpif->dump);
901 seq_change(udpif->dump_seq);
902
903 duration = MAX(time_msec() - start_time, 1);
904 udpif->dump_duration = duration;
905 if (duration > 2000) {
906 flow_limit /= duration / 1000;
907 } else if (duration > 1300) {
908 flow_limit = flow_limit * 3 / 4;
909 } else if (duration < 1000 && n_flows > 2000
910 && flow_limit < n_flows * 1000 / duration) {
911 flow_limit += 1000;
912 }
913 flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
914 atomic_store_relaxed(&udpif->flow_limit, flow_limit);
915
916 if (duration > 2000) {
917 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
918 duration);
919 }
920
921 poll_timer_wait_until(start_time + MIN(ofproto_max_idle, 500));
922 seq_wait(udpif->reval_seq, last_reval_seq);
923 latch_wait(&udpif->exit_latch);
924 latch_wait(&udpif->pause_latch);
925 poll_block();
926 }
927 }
928
929 return NULL;
930 }
931 \f
932 static enum upcall_type
933 classify_upcall(enum dpif_upcall_type type, const struct nlattr *userdata)
934 {
935 union user_action_cookie cookie;
936 size_t userdata_len;
937
938 /* First look at the upcall type. */
939 switch (type) {
940 case DPIF_UC_ACTION:
941 break;
942
943 case DPIF_UC_MISS:
944 return MISS_UPCALL;
945
946 case DPIF_N_UC_TYPES:
947 default:
948 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, type);
949 return BAD_UPCALL;
950 }
951
952 /* "action" upcalls need a closer look. */
953 if (!userdata) {
954 VLOG_WARN_RL(&rl, "action upcall missing cookie");
955 return BAD_UPCALL;
956 }
957 userdata_len = nl_attr_get_size(userdata);
958 if (userdata_len < sizeof cookie.type
959 || userdata_len > sizeof cookie) {
960 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
961 userdata_len);
962 return BAD_UPCALL;
963 }
964 memset(&cookie, 0, sizeof cookie);
965 memcpy(&cookie, nl_attr_get(userdata), userdata_len);
966 if (userdata_len == MAX(8, sizeof cookie.sflow)
967 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
968 return SFLOW_UPCALL;
969 } else if (userdata_len == MAX(8, sizeof cookie.slow_path)
970 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
971 return MISS_UPCALL;
972 } else if (userdata_len == MAX(8, sizeof cookie.flow_sample)
973 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
974 return FLOW_SAMPLE_UPCALL;
975 } else if (userdata_len == MAX(8, sizeof cookie.ipfix)
976 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
977 return IPFIX_UPCALL;
978 } else {
979 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
980 " and size %"PRIuSIZE, cookie.type, userdata_len);
981 return BAD_UPCALL;
982 }
983 }
984
985 /* Calculates slow path actions for 'xout'. 'buf' must statically be
986 * initialized with at least 128 bytes of space. */
987 static void
988 compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
989 const struct flow *flow, odp_port_t odp_in_port,
990 struct ofpbuf *buf)
991 {
992 union user_action_cookie cookie;
993 odp_port_t port;
994 uint32_t pid;
995
996 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
997 cookie.slow_path.unused = 0;
998 cookie.slow_path.reason = xout->slow;
999
1000 port = xout->slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)
1001 ? ODPP_NONE
1002 : odp_in_port;
1003 pid = dpif_port_get_pid(udpif->dpif, port, flow_hash_5tuple(flow, 0));
1004 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
1005 ODPP_NONE, false, buf);
1006 }
1007
1008 /* If there is no error, the upcall must be destroyed with upcall_uninit()
1009 * before quiescing, as the referred objects are guaranteed to exist only
1010 * until the calling thread quiesces. Otherwise, do not call upcall_uninit()
1011 * since the 'upcall->put_actions' remains uninitialized. */
1012 static int
1013 upcall_receive(struct upcall *upcall, const struct dpif_backer *backer,
1014 const struct dp_packet *packet, enum dpif_upcall_type type,
1015 const struct nlattr *userdata, const struct flow *flow,
1016 const unsigned int mru,
1017 const ovs_u128 *ufid, const unsigned pmd_id)
1018 {
1019 int error;
1020
1021 error = xlate_lookup(backer, flow, &upcall->ofproto, &upcall->ipfix,
1022 &upcall->sflow, NULL, &upcall->in_port);
1023 if (error) {
1024 return error;
1025 }
1026
1027 upcall->recirc = NULL;
1028 upcall->have_recirc_ref = false;
1029 upcall->flow = flow;
1030 upcall->packet = packet;
1031 upcall->ufid = ufid;
1032 upcall->pmd_id = pmd_id;
1033 upcall->type = type;
1034 upcall->userdata = userdata;
1035 ofpbuf_use_stub(&upcall->odp_actions, upcall->odp_actions_stub,
1036 sizeof upcall->odp_actions_stub);
1037 ofpbuf_init(&upcall->put_actions, 0);
1038
1039 upcall->xout_initialized = false;
1040 upcall->vsp_adjusted = false;
1041 upcall->ukey_persists = false;
1042
1043 upcall->ukey = NULL;
1044 upcall->key = NULL;
1045 upcall->key_len = 0;
1046 upcall->mru = mru;
1047
1048 upcall->out_tun_key = NULL;
1049 upcall->actions = NULL;
1050
1051 return 0;
1052 }
1053
1054 static void
1055 upcall_xlate(struct udpif *udpif, struct upcall *upcall,
1056 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
1057 {
1058 struct dpif_flow_stats stats;
1059 struct xlate_in xin;
1060
1061 stats.n_packets = 1;
1062 stats.n_bytes = dp_packet_size(upcall->packet);
1063 stats.used = time_msec();
1064 stats.tcp_flags = ntohs(upcall->flow->tcp_flags);
1065
1066 xlate_in_init(&xin, upcall->ofproto, upcall->flow, upcall->in_port, NULL,
1067 stats.tcp_flags, upcall->packet, wc, odp_actions);
1068
1069 if (upcall->type == DPIF_UC_MISS) {
1070 xin.resubmit_stats = &stats;
1071
1072 if (xin.frozen_state) {
1073 /* We may install a datapath flow only if we get a reference to the
1074 * recirculation context (otherwise we could have recirculation
1075 * upcalls using recirculation ID for which no context can be
1076 * found). We may still execute the flow's actions even if we
1077 * don't install the flow. */
1078 upcall->recirc = recirc_id_node_from_state(xin.frozen_state);
1079 upcall->have_recirc_ref = recirc_id_node_try_ref_rcu(upcall->recirc);
1080 }
1081 } else {
1082 /* For non-miss upcalls, we are either executing actions (one of which
1083 * is an userspace action) for an upcall, in which case the stats have
1084 * already been taken care of, or there's a flow in the datapath which
1085 * this packet was accounted to. Presumably the revalidators will deal
1086 * with pushing its stats eventually. */
1087 }
1088
1089 upcall->dump_seq = seq_read(udpif->dump_seq);
1090 upcall->reval_seq = seq_read(udpif->reval_seq);
1091 xlate_actions(&xin, &upcall->xout);
1092 upcall->xout_initialized = true;
1093
1094 if (!upcall->xout.slow) {
1095 ofpbuf_use_const(&upcall->put_actions,
1096 odp_actions->data, odp_actions->size);
1097 } else {
1098 /* upcall->put_actions already initialized by upcall_receive(). */
1099 compose_slow_path(udpif, &upcall->xout, upcall->flow,
1100 upcall->flow->in_port.odp_port,
1101 &upcall->put_actions);
1102 }
1103
1104 /* This function is also called for slow-pathed flows. As we are only
1105 * going to create new datapath flows for actual datapath misses, there is
1106 * no point in creating a ukey otherwise. */
1107 if (upcall->type == DPIF_UC_MISS) {
1108 upcall->ukey = ukey_create_from_upcall(upcall, wc);
1109 }
1110 }
1111
1112 static void
1113 upcall_uninit(struct upcall *upcall)
1114 {
1115 if (upcall) {
1116 if (upcall->xout_initialized) {
1117 xlate_out_uninit(&upcall->xout);
1118 }
1119 ofpbuf_uninit(&upcall->odp_actions);
1120 ofpbuf_uninit(&upcall->put_actions);
1121 if (upcall->ukey) {
1122 if (!upcall->ukey_persists) {
1123 ukey_delete__(upcall->ukey);
1124 }
1125 } else if (upcall->have_recirc_ref) {
1126 /* The reference was transferred to the ukey if one was created. */
1127 recirc_id_node_unref(upcall->recirc);
1128 }
1129 }
1130 }
1131
1132 static int
1133 upcall_cb(const struct dp_packet *packet, const struct flow *flow, ovs_u128 *ufid,
1134 unsigned pmd_id, enum dpif_upcall_type type,
1135 const struct nlattr *userdata, struct ofpbuf *actions,
1136 struct flow_wildcards *wc, struct ofpbuf *put_actions, void *aux)
1137 {
1138 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1139 struct udpif *udpif = aux;
1140 unsigned int flow_limit;
1141 struct upcall upcall;
1142 bool megaflow;
1143 int error;
1144
1145 atomic_read_relaxed(&enable_megaflows, &megaflow);
1146 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
1147
1148 error = upcall_receive(&upcall, udpif->backer, packet, type, userdata,
1149 flow, 0, ufid, pmd_id);
1150 if (error) {
1151 return error;
1152 }
1153
1154 error = process_upcall(udpif, &upcall, actions, wc);
1155 if (error) {
1156 goto out;
1157 }
1158
1159 if (upcall.xout.slow && put_actions) {
1160 ofpbuf_put(put_actions, upcall.put_actions.data,
1161 upcall.put_actions.size);
1162 }
1163
1164 if (OVS_UNLIKELY(!megaflow)) {
1165 flow_wildcards_init_for_packet(wc, flow);
1166 }
1167
1168 if (udpif_get_n_flows(udpif) >= flow_limit) {
1169 VLOG_WARN_RL(&rl, "upcall_cb failure: datapath flow limit reached");
1170 error = ENOSPC;
1171 goto out;
1172 }
1173
1174 /* Prevent miss flow installation if the key has recirculation ID but we
1175 * were not able to get a reference on it. */
1176 if (type == DPIF_UC_MISS && upcall.recirc && !upcall.have_recirc_ref) {
1177 VLOG_WARN_RL(&rl, "upcall_cb failure: no reference for recirc flow");
1178 error = ENOSPC;
1179 goto out;
1180 }
1181
1182 if (upcall.ukey && !ukey_install(udpif, upcall.ukey)) {
1183 VLOG_WARN_RL(&rl, "upcall_cb failure: ukey installation fails");
1184 error = ENOSPC;
1185 }
1186 out:
1187 if (!error) {
1188 upcall.ukey_persists = true;
1189 }
1190 upcall_uninit(&upcall);
1191 return error;
1192 }
1193
1194 static int
1195 process_upcall(struct udpif *udpif, struct upcall *upcall,
1196 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
1197 {
1198 const struct nlattr *userdata = upcall->userdata;
1199 const struct dp_packet *packet = upcall->packet;
1200 const struct flow *flow = upcall->flow;
1201
1202 switch (classify_upcall(upcall->type, userdata)) {
1203 case MISS_UPCALL:
1204 upcall_xlate(udpif, upcall, odp_actions, wc);
1205 return 0;
1206
1207 case SFLOW_UPCALL:
1208 if (upcall->sflow) {
1209 union user_action_cookie cookie;
1210 const struct nlattr *actions;
1211 size_t actions_len = 0;
1212 struct dpif_sflow_actions sflow_actions;
1213 memset(&sflow_actions, 0, sizeof sflow_actions);
1214 memset(&cookie, 0, sizeof cookie);
1215 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.sflow);
1216 if (upcall->actions) {
1217 /* Actions were passed up from datapath. */
1218 actions = nl_attr_get(upcall->actions);
1219 actions_len = nl_attr_get_size(upcall->actions);
1220 if (actions && actions_len) {
1221 dpif_sflow_read_actions(flow, actions, actions_len,
1222 &sflow_actions);
1223 }
1224 }
1225 if (actions_len == 0) {
1226 /* Lookup actions in userspace cache. */
1227 struct udpif_key *ukey = ukey_lookup(udpif, upcall->ufid,
1228 upcall->pmd_id);
1229 if (ukey) {
1230 ukey_get_actions(ukey, &actions, &actions_len);
1231 dpif_sflow_read_actions(flow, actions, actions_len,
1232 &sflow_actions);
1233 }
1234 }
1235 dpif_sflow_received(upcall->sflow, packet, flow,
1236 flow->in_port.odp_port, &cookie,
1237 actions_len > 0 ? &sflow_actions : NULL);
1238 }
1239 break;
1240
1241 case IPFIX_UPCALL:
1242 if (upcall->ipfix) {
1243 union user_action_cookie cookie;
1244 struct flow_tnl output_tunnel_key;
1245
1246 memset(&cookie, 0, sizeof cookie);
1247 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.ipfix);
1248
1249 if (upcall->out_tun_key) {
1250 odp_tun_key_from_attr(upcall->out_tun_key, false,
1251 &output_tunnel_key);
1252 }
1253 dpif_ipfix_bridge_sample(upcall->ipfix, packet, flow,
1254 flow->in_port.odp_port,
1255 cookie.ipfix.output_odp_port,
1256 upcall->out_tun_key ?
1257 &output_tunnel_key : NULL);
1258 }
1259 break;
1260
1261 case FLOW_SAMPLE_UPCALL:
1262 if (upcall->ipfix) {
1263 union user_action_cookie cookie;
1264
1265 memset(&cookie, 0, sizeof cookie);
1266 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.flow_sample);
1267
1268 /* The flow reflects exactly the contents of the packet.
1269 * Sample the packet using it. */
1270 dpif_ipfix_flow_sample(upcall->ipfix, packet, flow,
1271 cookie.flow_sample.collector_set_id,
1272 cookie.flow_sample.probability,
1273 cookie.flow_sample.obs_domain_id,
1274 cookie.flow_sample.obs_point_id);
1275 }
1276 break;
1277
1278 case BAD_UPCALL:
1279 break;
1280 }
1281
1282 return EAGAIN;
1283 }
1284
1285 static void
1286 handle_upcalls(struct udpif *udpif, struct upcall *upcalls,
1287 size_t n_upcalls)
1288 {
1289 struct dpif_op *opsp[UPCALL_MAX_BATCH * 2];
1290 struct ukey_op ops[UPCALL_MAX_BATCH * 2];
1291 unsigned int flow_limit;
1292 size_t n_ops, n_opsp, i;
1293 bool may_put;
1294
1295 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
1296
1297 may_put = udpif_get_n_flows(udpif) < flow_limit;
1298
1299 /* Handle the packets individually in order of arrival.
1300 *
1301 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
1302 * processes received packets for these protocols.
1303 *
1304 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1305 * controller.
1306 *
1307 * The loop fills 'ops' with an array of operations to execute in the
1308 * datapath. */
1309 n_ops = 0;
1310 for (i = 0; i < n_upcalls; i++) {
1311 struct upcall *upcall = &upcalls[i];
1312 const struct dp_packet *packet = upcall->packet;
1313 struct ukey_op *op;
1314
1315 if (upcall->vsp_adjusted) {
1316 /* This packet was received on a VLAN splinter port. We added a
1317 * VLAN to the packet to make the packet resemble the flow, but the
1318 * actions were composed assuming that the packet contained no
1319 * VLAN. So, we must remove the VLAN header from the packet before
1320 * trying to execute the actions. */
1321 if (upcall->odp_actions.size) {
1322 eth_pop_vlan(CONST_CAST(struct dp_packet *, upcall->packet));
1323 }
1324
1325 /* Remove the flow vlan tags inserted by vlan splinter logic
1326 * to ensure megaflow masks generated match the data path flow. */
1327 CONST_CAST(struct flow *, upcall->flow)->vlan_tci = 0;
1328 }
1329
1330 /* Do not install a flow into the datapath if:
1331 *
1332 * - The datapath already has too many flows.
1333 *
1334 * - We received this packet via some flow installed in the kernel
1335 * already.
1336 *
1337 * - Upcall was a recirculation but we do not have a reference to
1338 * to the recirculation ID. */
1339 if (may_put && upcall->type == DPIF_UC_MISS &&
1340 (!upcall->recirc || upcall->have_recirc_ref)) {
1341 struct udpif_key *ukey = upcall->ukey;
1342
1343 upcall->ukey_persists = true;
1344 op = &ops[n_ops++];
1345
1346 op->ukey = ukey;
1347 op->dop.type = DPIF_OP_FLOW_PUT;
1348 op->dop.u.flow_put.flags = DPIF_FP_CREATE;
1349 op->dop.u.flow_put.key = ukey->key;
1350 op->dop.u.flow_put.key_len = ukey->key_len;
1351 op->dop.u.flow_put.mask = ukey->mask;
1352 op->dop.u.flow_put.mask_len = ukey->mask_len;
1353 op->dop.u.flow_put.ufid = upcall->ufid;
1354 op->dop.u.flow_put.stats = NULL;
1355 ukey_get_actions(ukey, &op->dop.u.flow_put.actions,
1356 &op->dop.u.flow_put.actions_len);
1357 }
1358
1359 if (upcall->odp_actions.size) {
1360 op = &ops[n_ops++];
1361 op->ukey = NULL;
1362 op->dop.type = DPIF_OP_EXECUTE;
1363 op->dop.u.execute.packet = CONST_CAST(struct dp_packet *, packet);
1364 odp_key_to_pkt_metadata(upcall->key, upcall->key_len,
1365 &op->dop.u.execute.packet->md);
1366 op->dop.u.execute.actions = upcall->odp_actions.data;
1367 op->dop.u.execute.actions_len = upcall->odp_actions.size;
1368 op->dop.u.execute.needs_help = (upcall->xout.slow & SLOW_ACTION) != 0;
1369 op->dop.u.execute.probe = false;
1370 op->dop.u.execute.mtu = upcall->mru;
1371 }
1372 }
1373
1374 /* Execute batch.
1375 *
1376 * We install ukeys before installing the flows, locking them for exclusive
1377 * access by this thread for the period of installation. This ensures that
1378 * other threads won't attempt to delete the flows as we are creating them.
1379 */
1380 n_opsp = 0;
1381 for (i = 0; i < n_ops; i++) {
1382 struct udpif_key *ukey = ops[i].ukey;
1383
1384 if (ukey) {
1385 /* If we can't install the ukey, don't install the flow. */
1386 if (!ukey_install_start(udpif, ukey)) {
1387 ukey_delete__(ukey);
1388 ops[i].ukey = NULL;
1389 continue;
1390 }
1391 }
1392 opsp[n_opsp++] = &ops[i].dop;
1393 }
1394 dpif_operate(udpif->dpif, opsp, n_opsp);
1395 for (i = 0; i < n_ops; i++) {
1396 if (ops[i].ukey) {
1397 ukey_install_finish(ops[i].ukey, ops[i].dop.error);
1398 }
1399 }
1400 }
1401
1402 static uint32_t
1403 get_ukey_hash(const ovs_u128 *ufid, const unsigned pmd_id)
1404 {
1405 return hash_2words(ufid->u32[0], pmd_id);
1406 }
1407
1408 static struct udpif_key *
1409 ukey_lookup(struct udpif *udpif, const ovs_u128 *ufid, const unsigned pmd_id)
1410 {
1411 struct udpif_key *ukey;
1412 int idx = get_ukey_hash(ufid, pmd_id) % N_UMAPS;
1413 struct cmap *cmap = &udpif->ukeys[idx].cmap;
1414
1415 CMAP_FOR_EACH_WITH_HASH (ukey, cmap_node,
1416 get_ukey_hash(ufid, pmd_id), cmap) {
1417 if (ovs_u128_equals(&ukey->ufid, ufid)) {
1418 return ukey;
1419 }
1420 }
1421 return NULL;
1422 }
1423
1424 /* Provides safe lockless access of RCU protected 'ukey->actions'. Callers may
1425 * alternatively access the field directly if they take 'ukey->mutex'. */
1426 static void
1427 ukey_get_actions(struct udpif_key *ukey, const struct nlattr **actions, size_t *size)
1428 {
1429 const struct ofpbuf *buf = ovsrcu_get(struct ofpbuf *, &ukey->actions);
1430 *actions = buf->data;
1431 *size = buf->size;
1432 }
1433
1434 static void
1435 ukey_set_actions(struct udpif_key *ukey, const struct ofpbuf *actions)
1436 {
1437 ovsrcu_postpone(ofpbuf_delete,
1438 ovsrcu_get_protected(struct ofpbuf *, &ukey->actions));
1439 ovsrcu_set(&ukey->actions, ofpbuf_clone(actions));
1440 }
1441
1442 static struct udpif_key *
1443 ukey_create__(const struct nlattr *key, size_t key_len,
1444 const struct nlattr *mask, size_t mask_len,
1445 bool ufid_present, const ovs_u128 *ufid,
1446 const unsigned pmd_id, const struct ofpbuf *actions,
1447 uint64_t dump_seq, uint64_t reval_seq, long long int used,
1448 uint32_t key_recirc_id, struct xlate_out *xout)
1449 OVS_NO_THREAD_SAFETY_ANALYSIS
1450 {
1451 struct udpif_key *ukey = xmalloc(sizeof *ukey);
1452
1453 memcpy(&ukey->keybuf, key, key_len);
1454 ukey->key = &ukey->keybuf.nla;
1455 ukey->key_len = key_len;
1456 memcpy(&ukey->maskbuf, mask, mask_len);
1457 ukey->mask = &ukey->maskbuf.nla;
1458 ukey->mask_len = mask_len;
1459 ukey->ufid_present = ufid_present;
1460 ukey->ufid = *ufid;
1461 ukey->pmd_id = pmd_id;
1462 ukey->hash = get_ukey_hash(&ukey->ufid, pmd_id);
1463
1464 ovsrcu_init(&ukey->actions, NULL);
1465 ukey_set_actions(ukey, actions);
1466
1467 ovs_mutex_init(&ukey->mutex);
1468 ukey->dump_seq = dump_seq;
1469 ukey->reval_seq = reval_seq;
1470 ukey->flow_exists = false;
1471 ukey->created = time_msec();
1472 memset(&ukey->stats, 0, sizeof ukey->stats);
1473 ukey->stats.used = used;
1474 ukey->xcache = NULL;
1475
1476 ukey->key_recirc_id = key_recirc_id;
1477 recirc_refs_init(&ukey->recircs);
1478 if (xout) {
1479 /* Take ownership of the action recirc id references. */
1480 recirc_refs_swap(&ukey->recircs, &xout->recircs);
1481 }
1482
1483 return ukey;
1484 }
1485
1486 static struct udpif_key *
1487 ukey_create_from_upcall(struct upcall *upcall, struct flow_wildcards *wc)
1488 {
1489 struct odputil_keybuf keystub, maskstub;
1490 struct ofpbuf keybuf, maskbuf;
1491 bool megaflow;
1492 struct odp_flow_key_parms odp_parms = {
1493 .flow = upcall->flow,
1494 .mask = &wc->masks,
1495 };
1496
1497 odp_parms.support = ofproto_dpif_get_support(upcall->ofproto)->odp;
1498 if (upcall->key_len) {
1499 ofpbuf_use_const(&keybuf, upcall->key, upcall->key_len);
1500 } else {
1501 /* dpif-netdev doesn't provide a netlink-formatted flow key in the
1502 * upcall, so convert the upcall's flow here. */
1503 ofpbuf_use_stack(&keybuf, &keystub, sizeof keystub);
1504 odp_parms.odp_in_port = upcall->flow->in_port.odp_port;
1505 odp_flow_key_from_flow(&odp_parms, &keybuf);
1506 }
1507
1508 atomic_read_relaxed(&enable_megaflows, &megaflow);
1509 ofpbuf_use_stack(&maskbuf, &maskstub, sizeof maskstub);
1510 if (megaflow) {
1511 odp_parms.odp_in_port = ODPP_NONE;
1512 odp_parms.key_buf = &keybuf;
1513
1514 odp_flow_key_from_mask(&odp_parms, &maskbuf);
1515 }
1516
1517 return ukey_create__(keybuf.data, keybuf.size, maskbuf.data, maskbuf.size,
1518 true, upcall->ufid, upcall->pmd_id,
1519 &upcall->put_actions, upcall->dump_seq,
1520 upcall->reval_seq, 0,
1521 upcall->have_recirc_ref ? upcall->recirc->id : 0,
1522 &upcall->xout);
1523 }
1524
1525 static int
1526 ukey_create_from_dpif_flow(const struct udpif *udpif,
1527 const struct dpif_flow *flow,
1528 struct udpif_key **ukey)
1529 {
1530 struct dpif_flow full_flow;
1531 struct ofpbuf actions;
1532 uint64_t dump_seq, reval_seq;
1533 uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
1534 const struct nlattr *a;
1535 unsigned int left;
1536
1537 if (!flow->key_len || !flow->actions_len) {
1538 struct ofpbuf buf;
1539 int err;
1540
1541 /* If the key or actions were not provided by the datapath, fetch the
1542 * full flow. */
1543 ofpbuf_use_stack(&buf, &stub, sizeof stub);
1544 err = dpif_flow_get(udpif->dpif, NULL, 0, &flow->ufid,
1545 flow->pmd_id, &buf, &full_flow);
1546 if (err) {
1547 return err;
1548 }
1549 flow = &full_flow;
1550 }
1551
1552 /* Check the flow actions for recirculation action. As recirculation
1553 * relies on OVS userspace internal state, we need to delete all old
1554 * datapath flows with either a non-zero recirc_id in the key, or any
1555 * recirculation actions upon OVS restart. */
1556 NL_ATTR_FOR_EACH_UNSAFE (a, left, flow->key, flow->key_len) {
1557 if (nl_attr_type(a) == OVS_KEY_ATTR_RECIRC_ID
1558 && nl_attr_get_u32(a) != 0) {
1559 return EINVAL;
1560 }
1561 }
1562 NL_ATTR_FOR_EACH_UNSAFE (a, left, flow->actions, flow->actions_len) {
1563 if (nl_attr_type(a) == OVS_ACTION_ATTR_RECIRC) {
1564 return EINVAL;
1565 }
1566 }
1567
1568 dump_seq = seq_read(udpif->dump_seq);
1569 reval_seq = seq_read(udpif->reval_seq);
1570 ofpbuf_use_const(&actions, &flow->actions, flow->actions_len);
1571 *ukey = ukey_create__(flow->key, flow->key_len,
1572 flow->mask, flow->mask_len, flow->ufid_present,
1573 &flow->ufid, flow->pmd_id, &actions, dump_seq,
1574 reval_seq, flow->stats.used, 0, NULL);
1575
1576 return 0;
1577 }
1578
1579 /* Attempts to insert a ukey into the shared ukey maps.
1580 *
1581 * On success, returns true, installs the ukey and returns it in a locked
1582 * state. Otherwise, returns false. */
1583 static bool
1584 ukey_install_start(struct udpif *udpif, struct udpif_key *new_ukey)
1585 OVS_TRY_LOCK(true, new_ukey->mutex)
1586 {
1587 struct umap *umap;
1588 struct udpif_key *old_ukey;
1589 uint32_t idx;
1590 bool locked = false;
1591
1592 idx = new_ukey->hash % N_UMAPS;
1593 umap = &udpif->ukeys[idx];
1594 ovs_mutex_lock(&umap->mutex);
1595 old_ukey = ukey_lookup(udpif, &new_ukey->ufid, new_ukey->pmd_id);
1596 if (old_ukey) {
1597 /* Uncommon case: A ukey is already installed with the same UFID. */
1598 if (old_ukey->key_len == new_ukey->key_len
1599 && !memcmp(old_ukey->key, new_ukey->key, new_ukey->key_len)) {
1600 COVERAGE_INC(handler_duplicate_upcall);
1601 } else {
1602 struct ds ds = DS_EMPTY_INITIALIZER;
1603
1604 odp_format_ufid(&old_ukey->ufid, &ds);
1605 ds_put_cstr(&ds, " ");
1606 odp_flow_key_format(old_ukey->key, old_ukey->key_len, &ds);
1607 ds_put_cstr(&ds, "\n");
1608 odp_format_ufid(&new_ukey->ufid, &ds);
1609 ds_put_cstr(&ds, " ");
1610 odp_flow_key_format(new_ukey->key, new_ukey->key_len, &ds);
1611
1612 VLOG_WARN_RL(&rl, "Conflicting ukey for flows:\n%s", ds_cstr(&ds));
1613 ds_destroy(&ds);
1614 }
1615 } else {
1616 ovs_mutex_lock(&new_ukey->mutex);
1617 cmap_insert(&umap->cmap, &new_ukey->cmap_node, new_ukey->hash);
1618 locked = true;
1619 }
1620 ovs_mutex_unlock(&umap->mutex);
1621
1622 return locked;
1623 }
1624
1625 static void
1626 ukey_install_finish__(struct udpif_key *ukey) OVS_REQUIRES(ukey->mutex)
1627 {
1628 ukey->flow_exists = true;
1629 }
1630
1631 static bool
1632 ukey_install_finish(struct udpif_key *ukey, int error)
1633 OVS_RELEASES(ukey->mutex)
1634 {
1635 if (!error) {
1636 ukey_install_finish__(ukey);
1637 }
1638 ovs_mutex_unlock(&ukey->mutex);
1639
1640 return !error;
1641 }
1642
1643 static bool
1644 ukey_install(struct udpif *udpif, struct udpif_key *ukey)
1645 {
1646 /* The usual way to keep 'ukey->flow_exists' in sync with the datapath is
1647 * to call ukey_install_start(), install the corresponding datapath flow,
1648 * then call ukey_install_finish(). The netdev interface using upcall_cb()
1649 * doesn't provide a function to separately finish the flow installation,
1650 * so we perform the operations together here.
1651 *
1652 * This is fine currently, as revalidator threads will only delete this
1653 * ukey during revalidator_sweep() and only if the dump_seq is mismatched.
1654 * It is unlikely for a revalidator thread to advance dump_seq and reach
1655 * the next GC phase between ukey creation and flow installation. */
1656 return ukey_install_start(udpif, ukey) && ukey_install_finish(ukey, 0);
1657 }
1658
1659 /* Searches for a ukey in 'udpif->ukeys' that matches 'flow' and attempts to
1660 * lock the ukey. If the ukey does not exist, create it.
1661 *
1662 * Returns 0 on success, setting *result to the matching ukey and returning it
1663 * in a locked state. Otherwise, returns an errno and clears *result. EBUSY
1664 * indicates that another thread is handling this flow. Other errors indicate
1665 * an unexpected condition creating a new ukey.
1666 *
1667 * *error is an output parameter provided to appease the threadsafety analyser,
1668 * and its value matches the return value. */
1669 static int
1670 ukey_acquire(struct udpif *udpif, const struct dpif_flow *flow,
1671 struct udpif_key **result, int *error)
1672 OVS_TRY_LOCK(0, (*result)->mutex)
1673 {
1674 struct udpif_key *ukey;
1675 int retval;
1676
1677 ukey = ukey_lookup(udpif, &flow->ufid, flow->pmd_id);
1678 if (ukey) {
1679 retval = ovs_mutex_trylock(&ukey->mutex);
1680 } else {
1681 /* Usually we try to avoid installing flows from revalidator threads,
1682 * because locking on a umap may cause handler threads to block.
1683 * However there are certain cases, like when ovs-vswitchd is
1684 * restarted, where it is desirable to handle flows that exist in the
1685 * datapath gracefully (ie, don't just clear the datapath). */
1686 bool install;
1687
1688 retval = ukey_create_from_dpif_flow(udpif, flow, &ukey);
1689 if (retval) {
1690 goto done;
1691 }
1692 install = ukey_install_start(udpif, ukey);
1693 if (install) {
1694 ukey_install_finish__(ukey);
1695 retval = 0;
1696 } else {
1697 ukey_delete__(ukey);
1698 retval = EBUSY;
1699 }
1700 }
1701
1702 done:
1703 *error = retval;
1704 if (retval) {
1705 *result = NULL;
1706 } else {
1707 *result = ukey;
1708 }
1709 return retval;
1710 }
1711
1712 static void
1713 ukey_delete__(struct udpif_key *ukey)
1714 OVS_NO_THREAD_SAFETY_ANALYSIS
1715 {
1716 if (ukey) {
1717 if (ukey->key_recirc_id) {
1718 recirc_free_id(ukey->key_recirc_id);
1719 }
1720 recirc_refs_unref(&ukey->recircs);
1721 xlate_cache_delete(ukey->xcache);
1722 ofpbuf_delete(ovsrcu_get(struct ofpbuf *, &ukey->actions));
1723 ovs_mutex_destroy(&ukey->mutex);
1724 free(ukey);
1725 }
1726 }
1727
1728 static void
1729 ukey_delete(struct umap *umap, struct udpif_key *ukey)
1730 OVS_REQUIRES(umap->mutex)
1731 {
1732 cmap_remove(&umap->cmap, &ukey->cmap_node, ukey->hash);
1733 ovsrcu_postpone(ukey_delete__, ukey);
1734 }
1735
1736 static bool
1737 should_revalidate(const struct udpif *udpif, uint64_t packets,
1738 long long int used)
1739 {
1740 long long int metric, now, duration;
1741
1742 if (udpif->dump_duration < 200) {
1743 /* We are likely to handle full revalidation for the flows. */
1744 return true;
1745 }
1746
1747 /* Calculate the mean time between seeing these packets. If this
1748 * exceeds the threshold, then delete the flow rather than performing
1749 * costly revalidation for flows that aren't being hit frequently.
1750 *
1751 * This is targeted at situations where the dump_duration is high (~1s),
1752 * and revalidation is triggered by a call to udpif_revalidate(). In
1753 * these situations, revalidation of all flows causes fluctuations in the
1754 * flow_limit due to the interaction with the dump_duration and max_idle.
1755 * This tends to result in deletion of low-throughput flows anyway, so
1756 * skip the revalidation and just delete those flows. */
1757 packets = MAX(packets, 1);
1758 now = MAX(used, time_msec());
1759 duration = now - used;
1760 metric = duration / packets;
1761
1762 if (metric < 200) {
1763 /* The flow is receiving more than ~5pps, so keep it. */
1764 return true;
1765 }
1766 return false;
1767 }
1768
1769 /* Verifies that the datapath actions of 'ukey' are still correct, and pushes
1770 * 'stats' for it.
1771 *
1772 * Returns a recommended action for 'ukey', options include:
1773 * UKEY_DELETE The ukey should be deleted.
1774 * UKEY_KEEP The ukey is fine as is.
1775 * UKEY_MODIFY The ukey's actions should be changed but is otherwise
1776 * fine. Callers should change the actions to those found
1777 * in the caller supplied 'odp_actions' buffer. The
1778 * recirculation references can be found in 'recircs' and
1779 * must be handled by the caller.
1780 *
1781 * If the result is UKEY_MODIFY, then references to all recirc_ids used by the
1782 * new flow will be held within 'recircs' (which may be none).
1783 *
1784 * The caller is responsible for both initializing 'recircs' prior this call,
1785 * and ensuring any references are eventually freed.
1786 */
1787 static enum reval_result
1788 revalidate_ukey(struct udpif *udpif, struct udpif_key *ukey,
1789 const struct dpif_flow_stats *stats,
1790 struct ofpbuf *odp_actions, uint64_t reval_seq,
1791 struct recirc_refs *recircs)
1792 OVS_REQUIRES(ukey->mutex)
1793 {
1794 struct xlate_out xout, *xoutp;
1795 struct netflow *netflow;
1796 struct ofproto_dpif *ofproto;
1797 struct dpif_flow_stats push;
1798 struct flow flow;
1799 struct flow_wildcards dp_mask, wc;
1800 enum reval_result result;
1801 ofp_port_t ofp_in_port;
1802 struct xlate_in xin;
1803 long long int last_used;
1804 int error;
1805 bool need_revalidate;
1806
1807 result = UKEY_DELETE;
1808 xoutp = NULL;
1809 netflow = NULL;
1810
1811 ofpbuf_clear(odp_actions);
1812 need_revalidate = (ukey->reval_seq != reval_seq);
1813 last_used = ukey->stats.used;
1814 push.used = stats->used;
1815 push.tcp_flags = stats->tcp_flags;
1816 push.n_packets = (stats->n_packets > ukey->stats.n_packets
1817 ? stats->n_packets - ukey->stats.n_packets
1818 : 0);
1819 push.n_bytes = (stats->n_bytes > ukey->stats.n_bytes
1820 ? stats->n_bytes - ukey->stats.n_bytes
1821 : 0);
1822
1823 if (need_revalidate && last_used
1824 && !should_revalidate(udpif, push.n_packets, last_used)) {
1825 goto exit;
1826 }
1827
1828 /* We will push the stats, so update the ukey stats cache. */
1829 ukey->stats = *stats;
1830 if (!push.n_packets && !need_revalidate) {
1831 result = UKEY_KEEP;
1832 goto exit;
1833 }
1834
1835 if (ukey->xcache && !need_revalidate) {
1836 xlate_push_stats(ukey->xcache, &push);
1837 result = UKEY_KEEP;
1838 goto exit;
1839 }
1840
1841 if (odp_flow_key_to_flow(ukey->key, ukey->key_len, &flow)
1842 == ODP_FIT_ERROR) {
1843 goto exit;
1844 }
1845
1846 error = xlate_lookup(udpif->backer, &flow, &ofproto, NULL, NULL, &netflow,
1847 &ofp_in_port);
1848 if (error) {
1849 goto exit;
1850 }
1851
1852 if (need_revalidate) {
1853 xlate_cache_clear(ukey->xcache);
1854 }
1855 if (!ukey->xcache) {
1856 ukey->xcache = xlate_cache_new();
1857 }
1858
1859 xlate_in_init(&xin, ofproto, &flow, ofp_in_port, NULL, push.tcp_flags,
1860 NULL, need_revalidate ? &wc : NULL, odp_actions);
1861 if (push.n_packets) {
1862 xin.resubmit_stats = &push;
1863 xin.may_learn = true;
1864 }
1865 xin.xcache = ukey->xcache;
1866 xlate_actions(&xin, &xout);
1867 xoutp = &xout;
1868
1869 if (!need_revalidate) {
1870 result = UKEY_KEEP;
1871 goto exit;
1872 }
1873
1874 if (xout.slow) {
1875 ofpbuf_clear(odp_actions);
1876 compose_slow_path(udpif, &xout, &flow, flow.in_port.odp_port,
1877 odp_actions);
1878 }
1879
1880 if (odp_flow_key_to_mask(ukey->mask, ukey->mask_len, ukey->key,
1881 ukey->key_len, &dp_mask, &flow)
1882 == ODP_FIT_ERROR) {
1883 goto exit;
1884 }
1885
1886 /* Do not modify if any bit is wildcarded by the installed datapath flow,
1887 * but not the newly revalidated wildcard mask (wc), i.e., if revalidation
1888 * tells that the datapath flow is now too generic and must be narrowed
1889 * down. Note that we do not know if the datapath has ignored any of the
1890 * wildcarded bits, so we may be overtly conservative here. */
1891 if (flow_wildcards_has_extra(&dp_mask, &wc)) {
1892 goto exit;
1893 }
1894
1895 if (!ofpbuf_equal(odp_actions,
1896 ovsrcu_get(struct ofpbuf *, &ukey->actions))) {
1897 /* The datapath mask was OK, but the actions seem to have changed.
1898 * Let's modify it in place. */
1899 result = UKEY_MODIFY;
1900 /* Transfer recirc action ID references to the caller. */
1901 recirc_refs_swap(recircs, &xoutp->recircs);
1902 goto exit;
1903 }
1904
1905 result = UKEY_KEEP;
1906
1907 exit:
1908 if (result != UKEY_DELETE) {
1909 ukey->reval_seq = reval_seq;
1910 }
1911 if (netflow && result == UKEY_DELETE) {
1912 netflow_flow_clear(netflow, &flow);
1913 }
1914 xlate_out_uninit(xoutp);
1915 return result;
1916 }
1917
1918 static void
1919 delete_op_init__(struct udpif *udpif, struct ukey_op *op,
1920 const struct dpif_flow *flow)
1921 {
1922 op->ukey = NULL;
1923 op->dop.type = DPIF_OP_FLOW_DEL;
1924 op->dop.u.flow_del.key = flow->key;
1925 op->dop.u.flow_del.key_len = flow->key_len;
1926 op->dop.u.flow_del.ufid = flow->ufid_present ? &flow->ufid : NULL;
1927 op->dop.u.flow_del.pmd_id = flow->pmd_id;
1928 op->dop.u.flow_del.stats = &op->stats;
1929 op->dop.u.flow_del.terse = udpif_use_ufid(udpif);
1930 }
1931
1932 static void
1933 delete_op_init(struct udpif *udpif, struct ukey_op *op, struct udpif_key *ukey)
1934 {
1935 op->ukey = ukey;
1936 op->dop.type = DPIF_OP_FLOW_DEL;
1937 op->dop.u.flow_del.key = ukey->key;
1938 op->dop.u.flow_del.key_len = ukey->key_len;
1939 op->dop.u.flow_del.ufid = ukey->ufid_present ? &ukey->ufid : NULL;
1940 op->dop.u.flow_del.pmd_id = ukey->pmd_id;
1941 op->dop.u.flow_del.stats = &op->stats;
1942 op->dop.u.flow_del.terse = udpif_use_ufid(udpif);
1943 }
1944
1945 static void
1946 modify_op_init(struct ukey_op *op, struct udpif_key *ukey)
1947 {
1948 op->ukey = ukey;
1949 op->dop.type = DPIF_OP_FLOW_PUT;
1950 op->dop.u.flow_put.flags = DPIF_FP_MODIFY;
1951 op->dop.u.flow_put.key = ukey->key;
1952 op->dop.u.flow_put.key_len = ukey->key_len;
1953 op->dop.u.flow_put.mask = ukey->mask;
1954 op->dop.u.flow_put.mask_len = ukey->mask_len;
1955 op->dop.u.flow_put.ufid = &ukey->ufid;
1956 op->dop.u.flow_put.pmd_id = ukey->pmd_id;
1957 op->dop.u.flow_put.stats = NULL;
1958 ukey_get_actions(ukey, &op->dop.u.flow_put.actions,
1959 &op->dop.u.flow_put.actions_len);
1960 }
1961
1962 /* Executes datapath operations 'ops' and attributes stats retrieved from the
1963 * datapath as part of those operations. */
1964 static void
1965 push_dp_ops(struct udpif *udpif, struct ukey_op *ops, size_t n_ops)
1966 {
1967 struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
1968 size_t i;
1969
1970 ovs_assert(n_ops <= REVALIDATE_MAX_BATCH);
1971 for (i = 0; i < n_ops; i++) {
1972 opsp[i] = &ops[i].dop;
1973 }
1974 dpif_operate(udpif->dpif, opsp, n_ops);
1975
1976 for (i = 0; i < n_ops; i++) {
1977 struct ukey_op *op = &ops[i];
1978 struct dpif_flow_stats *push, *stats, push_buf;
1979
1980 stats = op->dop.u.flow_del.stats;
1981 push = &push_buf;
1982
1983 if (op->dop.type != DPIF_OP_FLOW_DEL) {
1984 /* Only deleted flows need their stats pushed. */
1985 continue;
1986 }
1987
1988 if (op->dop.error) {
1989 /* flow_del error, 'stats' is unusable. */
1990 continue;
1991 }
1992
1993 if (op->ukey) {
1994 ovs_mutex_lock(&op->ukey->mutex);
1995 push->used = MAX(stats->used, op->ukey->stats.used);
1996 push->tcp_flags = stats->tcp_flags | op->ukey->stats.tcp_flags;
1997 push->n_packets = stats->n_packets - op->ukey->stats.n_packets;
1998 push->n_bytes = stats->n_bytes - op->ukey->stats.n_bytes;
1999 ovs_mutex_unlock(&op->ukey->mutex);
2000 } else {
2001 push = stats;
2002 }
2003
2004 if (push->n_packets || netflow_exists()) {
2005 const struct nlattr *key = op->dop.u.flow_del.key;
2006 size_t key_len = op->dop.u.flow_del.key_len;
2007 struct ofproto_dpif *ofproto;
2008 struct netflow *netflow;
2009 ofp_port_t ofp_in_port;
2010 struct flow flow;
2011 int error;
2012
2013 if (op->ukey) {
2014 ovs_mutex_lock(&op->ukey->mutex);
2015 if (op->ukey->xcache) {
2016 xlate_push_stats(op->ukey->xcache, push);
2017 ovs_mutex_unlock(&op->ukey->mutex);
2018 continue;
2019 }
2020 ovs_mutex_unlock(&op->ukey->mutex);
2021 key = op->ukey->key;
2022 key_len = op->ukey->key_len;
2023 }
2024
2025 if (odp_flow_key_to_flow(key, key_len, &flow)
2026 == ODP_FIT_ERROR) {
2027 continue;
2028 }
2029
2030 error = xlate_lookup(udpif->backer, &flow, &ofproto, NULL, NULL,
2031 &netflow, &ofp_in_port);
2032 if (!error) {
2033 struct xlate_in xin;
2034
2035 xlate_in_init(&xin, ofproto, &flow, ofp_in_port, NULL,
2036 push->tcp_flags, NULL, NULL, NULL);
2037 xin.resubmit_stats = push->n_packets ? push : NULL;
2038 xin.may_learn = push->n_packets > 0;
2039 xlate_actions_for_side_effects(&xin);
2040
2041 if (netflow) {
2042 netflow_flow_clear(netflow, &flow);
2043 }
2044 }
2045 }
2046 }
2047 }
2048
2049 /* Executes datapath operations 'ops', attributes stats retrieved from the
2050 * datapath, and deletes ukeys corresponding to deleted flows. */
2051 static void
2052 push_ukey_ops(struct udpif *udpif, struct umap *umap,
2053 struct ukey_op *ops, size_t n_ops)
2054 {
2055 int i;
2056
2057 push_dp_ops(udpif, ops, n_ops);
2058 ovs_mutex_lock(&umap->mutex);
2059 for (i = 0; i < n_ops; i++) {
2060 if (ops[i].dop.type == DPIF_OP_FLOW_DEL) {
2061 ukey_delete(umap, ops[i].ukey);
2062 }
2063 }
2064 ovs_mutex_unlock(&umap->mutex);
2065 }
2066
2067 static void
2068 log_unexpected_flow(const struct dpif_flow *flow, int error)
2069 {
2070 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 60);
2071 struct ds ds = DS_EMPTY_INITIALIZER;
2072
2073 ds_put_format(&ds, "Failed to acquire udpif_key corresponding to "
2074 "unexpected flow (%s): ", ovs_strerror(error));
2075 odp_format_ufid(&flow->ufid, &ds);
2076 VLOG_WARN_RL(&rl, "%s", ds_cstr(&ds));
2077 }
2078
2079 static void
2080 reval_op_init(struct ukey_op *op, enum reval_result result,
2081 struct udpif *udpif, struct udpif_key *ukey,
2082 struct recirc_refs *recircs, struct ofpbuf *odp_actions)
2083 {
2084 if (result == UKEY_DELETE) {
2085 delete_op_init(udpif, op, ukey);
2086 } else if (result == UKEY_MODIFY) {
2087 /* Store the new recircs. */
2088 recirc_refs_swap(&ukey->recircs, recircs);
2089 /* Release old recircs. */
2090 recirc_refs_unref(recircs);
2091 /* ukey->key_recirc_id remains, as the key is the same as before. */
2092
2093 ukey_set_actions(ukey, odp_actions);
2094 modify_op_init(op, ukey);
2095 }
2096 }
2097
2098 static void
2099 revalidate(struct revalidator *revalidator)
2100 {
2101 uint64_t odp_actions_stub[1024 / 8];
2102 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
2103
2104 struct udpif *udpif = revalidator->udpif;
2105 struct dpif_flow_dump_thread *dump_thread;
2106 uint64_t dump_seq, reval_seq;
2107 unsigned int flow_limit;
2108
2109 dump_seq = seq_read(udpif->dump_seq);
2110 reval_seq = seq_read(udpif->reval_seq);
2111 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
2112 dump_thread = dpif_flow_dump_thread_create(udpif->dump);
2113 for (;;) {
2114 struct ukey_op ops[REVALIDATE_MAX_BATCH];
2115 int n_ops = 0;
2116
2117 struct dpif_flow flows[REVALIDATE_MAX_BATCH];
2118 const struct dpif_flow *f;
2119 int n_dumped;
2120
2121 long long int max_idle;
2122 long long int now;
2123 size_t n_dp_flows;
2124 bool kill_them_all;
2125
2126 n_dumped = dpif_flow_dump_next(dump_thread, flows, ARRAY_SIZE(flows));
2127 if (!n_dumped) {
2128 break;
2129 }
2130
2131 now = time_msec();
2132
2133 /* In normal operation we want to keep flows around until they have
2134 * been idle for 'ofproto_max_idle' milliseconds. However:
2135 *
2136 * - If the number of datapath flows climbs above 'flow_limit',
2137 * drop that down to 100 ms to try to bring the flows down to
2138 * the limit.
2139 *
2140 * - If the number of datapath flows climbs above twice
2141 * 'flow_limit', delete all the datapath flows as an emergency
2142 * measure. (We reassess this condition for the next batch of
2143 * datapath flows, so we will recover before all the flows are
2144 * gone.) */
2145 n_dp_flows = udpif_get_n_flows(udpif);
2146 kill_them_all = n_dp_flows > flow_limit * 2;
2147 max_idle = n_dp_flows > flow_limit ? 100 : ofproto_max_idle;
2148
2149 for (f = flows; f < &flows[n_dumped]; f++) {
2150 long long int used = f->stats.used;
2151 struct recirc_refs recircs = RECIRC_REFS_EMPTY_INITIALIZER;
2152 enum reval_result result;
2153 struct udpif_key *ukey;
2154 bool already_dumped;
2155 int error;
2156
2157 if (ukey_acquire(udpif, f, &ukey, &error)) {
2158 if (error == EBUSY) {
2159 /* Another thread is processing this flow, so don't bother
2160 * processing it.*/
2161 COVERAGE_INC(upcall_ukey_contention);
2162 } else {
2163 log_unexpected_flow(f, error);
2164 if (error != ENOENT) {
2165 delete_op_init__(udpif, &ops[n_ops++], f);
2166 }
2167 }
2168 continue;
2169 }
2170
2171 already_dumped = ukey->dump_seq == dump_seq;
2172 if (already_dumped) {
2173 /* The flow has already been handled during this flow dump
2174 * operation. Skip it. */
2175 if (ukey->xcache) {
2176 COVERAGE_INC(dumped_duplicate_flow);
2177 } else {
2178 COVERAGE_INC(dumped_new_flow);
2179 }
2180 ovs_mutex_unlock(&ukey->mutex);
2181 continue;
2182 }
2183
2184 if (!used) {
2185 used = ukey->created;
2186 }
2187 if (kill_them_all || (used && used < now - max_idle)) {
2188 result = UKEY_DELETE;
2189 } else {
2190 result = revalidate_ukey(udpif, ukey, &f->stats, &odp_actions,
2191 reval_seq, &recircs);
2192 }
2193 ukey->dump_seq = dump_seq;
2194 ukey->flow_exists = result != UKEY_DELETE;
2195
2196 if (result != UKEY_KEEP) {
2197 /* Takes ownership of 'recircs'. */
2198 reval_op_init(&ops[n_ops++], result, udpif, ukey, &recircs,
2199 &odp_actions);
2200 }
2201 ovs_mutex_unlock(&ukey->mutex);
2202 }
2203
2204 if (n_ops) {
2205 /* Push datapath ops but defer ukey deletion to 'sweep' phase. */
2206 push_dp_ops(udpif, ops, n_ops);
2207 }
2208 ovsrcu_quiesce();
2209 }
2210 dpif_flow_dump_thread_destroy(dump_thread);
2211 ofpbuf_uninit(&odp_actions);
2212 }
2213
2214 /* Pauses the 'revalidator', can only proceed after main thread
2215 * calls udpif_resume_revalidators(). */
2216 static void
2217 revalidator_pause(struct revalidator *revalidator)
2218 {
2219 /* The first block is for sync'ing the pause with main thread. */
2220 ovs_barrier_block(&revalidator->udpif->pause_barrier);
2221 /* The second block is for pausing until main thread resumes. */
2222 ovs_barrier_block(&revalidator->udpif->pause_barrier);
2223 }
2224
2225 static void
2226 revalidator_sweep__(struct revalidator *revalidator, bool purge)
2227 {
2228 struct udpif *udpif;
2229 uint64_t dump_seq, reval_seq;
2230 int slice;
2231
2232 udpif = revalidator->udpif;
2233 dump_seq = seq_read(udpif->dump_seq);
2234 reval_seq = seq_read(udpif->reval_seq);
2235 slice = revalidator - udpif->revalidators;
2236 ovs_assert(slice < udpif->n_revalidators);
2237
2238 for (int i = slice; i < N_UMAPS; i += udpif->n_revalidators) {
2239 uint64_t odp_actions_stub[1024 / 8];
2240 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
2241
2242 struct ukey_op ops[REVALIDATE_MAX_BATCH];
2243 struct udpif_key *ukey;
2244 struct umap *umap = &udpif->ukeys[i];
2245 size_t n_ops = 0;
2246
2247 CMAP_FOR_EACH(ukey, cmap_node, &umap->cmap) {
2248 bool flow_exists;
2249
2250 /* Handler threads could be holding a ukey lock while it installs a
2251 * new flow, so don't hang around waiting for access to it. */
2252 if (ovs_mutex_trylock(&ukey->mutex)) {
2253 continue;
2254 }
2255 flow_exists = ukey->flow_exists;
2256 if (flow_exists) {
2257 struct recirc_refs recircs = RECIRC_REFS_EMPTY_INITIALIZER;
2258 bool seq_mismatch = (ukey->dump_seq != dump_seq
2259 && ukey->reval_seq != reval_seq);
2260 enum reval_result result;
2261
2262 if (purge) {
2263 result = UKEY_DELETE;
2264 } else if (!seq_mismatch) {
2265 result = UKEY_KEEP;
2266 } else {
2267 struct dpif_flow_stats stats;
2268 COVERAGE_INC(revalidate_missed_dp_flow);
2269 memset(&stats, 0, sizeof stats);
2270 result = revalidate_ukey(udpif, ukey, &stats, &odp_actions,
2271 reval_seq, &recircs);
2272 }
2273 if (result != UKEY_KEEP) {
2274 /* Clears 'recircs' if filled by revalidate_ukey(). */
2275 reval_op_init(&ops[n_ops++], result, udpif, ukey, &recircs,
2276 &odp_actions);
2277 }
2278 }
2279 ovs_mutex_unlock(&ukey->mutex);
2280
2281 if (!flow_exists) {
2282 /* The common flow deletion case involves deletion of the flow
2283 * during the dump phase and ukey deletion here. */
2284 ovs_mutex_lock(&umap->mutex);
2285 ukey_delete(umap, ukey);
2286 ovs_mutex_unlock(&umap->mutex);
2287 }
2288
2289 if (n_ops == REVALIDATE_MAX_BATCH) {
2290 /* Update/delete missed flows and clean up corresponding ukeys
2291 * if necessary. */
2292 push_ukey_ops(udpif, umap, ops, n_ops);
2293 n_ops = 0;
2294 }
2295 }
2296
2297 if (n_ops) {
2298 push_ukey_ops(udpif, umap, ops, n_ops);
2299 }
2300
2301 ofpbuf_uninit(&odp_actions);
2302 ovsrcu_quiesce();
2303 }
2304 }
2305
2306 static void
2307 revalidator_sweep(struct revalidator *revalidator)
2308 {
2309 revalidator_sweep__(revalidator, false);
2310 }
2311
2312 static void
2313 revalidator_purge(struct revalidator *revalidator)
2314 {
2315 revalidator_sweep__(revalidator, true);
2316 }
2317
2318 /* In reaction to dpif purge, purges all 'ukey's with same 'pmd_id'. */
2319 static void
2320 dp_purge_cb(void *aux, unsigned pmd_id)
2321 {
2322 struct udpif *udpif = aux;
2323 size_t i;
2324
2325 udpif_pause_revalidators(udpif);
2326 for (i = 0; i < N_UMAPS; i++) {
2327 struct ukey_op ops[REVALIDATE_MAX_BATCH];
2328 struct udpif_key *ukey;
2329 struct umap *umap = &udpif->ukeys[i];
2330 size_t n_ops = 0;
2331
2332 CMAP_FOR_EACH(ukey, cmap_node, &umap->cmap) {
2333 if (ukey->pmd_id == pmd_id) {
2334 delete_op_init(udpif, &ops[n_ops++], ukey);
2335 if (n_ops == REVALIDATE_MAX_BATCH) {
2336 push_ukey_ops(udpif, umap, ops, n_ops);
2337 n_ops = 0;
2338 }
2339 }
2340 }
2341
2342 if (n_ops) {
2343 push_ukey_ops(udpif, umap, ops, n_ops);
2344 }
2345
2346 ovsrcu_quiesce();
2347 }
2348 udpif_resume_revalidators(udpif);
2349 }
2350 \f
2351 static void
2352 upcall_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
2353 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2354 {
2355 struct ds ds = DS_EMPTY_INITIALIZER;
2356 struct udpif *udpif;
2357
2358 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
2359 unsigned int flow_limit;
2360 bool ufid_enabled;
2361 size_t i;
2362
2363 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
2364 ufid_enabled = udpif_use_ufid(udpif);
2365
2366 ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
2367 ds_put_format(&ds, "\tflows : (current %lu)"
2368 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
2369 udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
2370 ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
2371 ds_put_format(&ds, "\tufid enabled : ");
2372 if (ufid_enabled) {
2373 ds_put_format(&ds, "true\n");
2374 } else {
2375 ds_put_format(&ds, "false\n");
2376 }
2377 ds_put_char(&ds, '\n');
2378
2379 for (i = 0; i < n_revalidators; i++) {
2380 struct revalidator *revalidator = &udpif->revalidators[i];
2381 int j, elements = 0;
2382
2383 for (j = i; j < N_UMAPS; j += n_revalidators) {
2384 elements += cmap_count(&udpif->ukeys[j].cmap);
2385 }
2386 ds_put_format(&ds, "\t%u: (keys %d)\n", revalidator->id, elements);
2387 }
2388 }
2389
2390 unixctl_command_reply(conn, ds_cstr(&ds));
2391 ds_destroy(&ds);
2392 }
2393
2394 /* Disable using the megaflows.
2395 *
2396 * This command is only needed for advanced debugging, so it's not
2397 * documented in the man page. */
2398 static void
2399 upcall_unixctl_disable_megaflows(struct unixctl_conn *conn,
2400 int argc OVS_UNUSED,
2401 const char *argv[] OVS_UNUSED,
2402 void *aux OVS_UNUSED)
2403 {
2404 atomic_store_relaxed(&enable_megaflows, false);
2405 udpif_flush_all_datapaths();
2406 unixctl_command_reply(conn, "megaflows disabled");
2407 }
2408
2409 /* Re-enable using megaflows.
2410 *
2411 * This command is only needed for advanced debugging, so it's not
2412 * documented in the man page. */
2413 static void
2414 upcall_unixctl_enable_megaflows(struct unixctl_conn *conn,
2415 int argc OVS_UNUSED,
2416 const char *argv[] OVS_UNUSED,
2417 void *aux OVS_UNUSED)
2418 {
2419 atomic_store_relaxed(&enable_megaflows, true);
2420 udpif_flush_all_datapaths();
2421 unixctl_command_reply(conn, "megaflows enabled");
2422 }
2423
2424 /* Disable skipping flow attributes during flow dump.
2425 *
2426 * This command is only needed for advanced debugging, so it's not
2427 * documented in the man page. */
2428 static void
2429 upcall_unixctl_disable_ufid(struct unixctl_conn *conn, int argc OVS_UNUSED,
2430 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2431 {
2432 atomic_store_relaxed(&enable_ufid, false);
2433 unixctl_command_reply(conn, "Datapath dumping tersely using UFID disabled");
2434 }
2435
2436 /* Re-enable skipping flow attributes during flow dump.
2437 *
2438 * This command is only needed for advanced debugging, so it's not documented
2439 * in the man page. */
2440 static void
2441 upcall_unixctl_enable_ufid(struct unixctl_conn *conn, int argc OVS_UNUSED,
2442 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2443 {
2444 atomic_store_relaxed(&enable_ufid, true);
2445 unixctl_command_reply(conn, "Datapath dumping tersely using UFID enabled "
2446 "for supported datapaths");
2447 }
2448
2449 /* Set the flow limit.
2450 *
2451 * This command is only needed for advanced debugging, so it's not
2452 * documented in the man page. */
2453 static void
2454 upcall_unixctl_set_flow_limit(struct unixctl_conn *conn,
2455 int argc OVS_UNUSED,
2456 const char *argv[] OVS_UNUSED,
2457 void *aux OVS_UNUSED)
2458 {
2459 struct ds ds = DS_EMPTY_INITIALIZER;
2460 struct udpif *udpif;
2461 unsigned int flow_limit = atoi(argv[1]);
2462
2463 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
2464 atomic_store_relaxed(&udpif->flow_limit, flow_limit);
2465 }
2466 ds_put_format(&ds, "set flow_limit to %u\n", flow_limit);
2467 unixctl_command_reply(conn, ds_cstr(&ds));
2468 ds_destroy(&ds);
2469 }
2470
2471 static void
2472 upcall_unixctl_dump_wait(struct unixctl_conn *conn,
2473 int argc OVS_UNUSED,
2474 const char *argv[] OVS_UNUSED,
2475 void *aux OVS_UNUSED)
2476 {
2477 if (list_is_singleton(&all_udpifs)) {
2478 struct udpif *udpif = NULL;
2479 size_t len;
2480
2481 udpif = OBJECT_CONTAINING(list_front(&all_udpifs), udpif, list_node);
2482 len = (udpif->n_conns + 1) * sizeof *udpif->conns;
2483 udpif->conn_seq = seq_read(udpif->dump_seq);
2484 udpif->conns = xrealloc(udpif->conns, len);
2485 udpif->conns[udpif->n_conns++] = conn;
2486 } else {
2487 unixctl_command_reply_error(conn, "can't wait on multiple udpifs.");
2488 }
2489 }
2490
2491 static void
2492 upcall_unixctl_purge(struct unixctl_conn *conn, int argc OVS_UNUSED,
2493 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2494 {
2495 struct udpif *udpif;
2496
2497 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
2498 int n;
2499
2500 for (n = 0; n < udpif->n_revalidators; n++) {
2501 revalidator_purge(&udpif->revalidators[n]);
2502 }
2503 }
2504 unixctl_command_reply(conn, "");
2505 }