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