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