]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/ofproto-dpif-upcall.c
ofproto-dpif-upcall: Transition ukey on dp_ops error.
[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->rt_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->rt_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 NULL);
897 }
898 }
899
900 /* Wait for the leader to start the flow dump. */
901 ovs_barrier_block(&udpif->reval_barrier);
902 if (udpif->pause) {
903 revalidator_pause(revalidator);
904 }
905
906 if (udpif->reval_exit) {
907 break;
908 }
909 revalidate(revalidator);
910
911 /* Wait for all flows to have been dumped before we garbage collect. */
912 ovs_barrier_block(&udpif->reval_barrier);
913 revalidator_sweep(revalidator);
914
915 /* Wait for all revalidators to finish garbage collection. */
916 ovs_barrier_block(&udpif->reval_barrier);
917
918 if (leader) {
919 unsigned int flow_limit;
920 long long int duration;
921
922 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
923
924 dpif_flow_dump_destroy(udpif->dump);
925 seq_change(udpif->dump_seq);
926
927 duration = MAX(time_msec() - start_time, 1);
928 udpif->dump_duration = duration;
929 if (duration > 2000) {
930 flow_limit /= duration / 1000;
931 } else if (duration > 1300) {
932 flow_limit = flow_limit * 3 / 4;
933 } else if (duration < 1000 && n_flows > 2000
934 && flow_limit < n_flows * 1000 / duration) {
935 flow_limit += 1000;
936 }
937 flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
938 atomic_store_relaxed(&udpif->flow_limit, flow_limit);
939
940 if (duration > 2000) {
941 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
942 duration);
943 }
944
945 poll_timer_wait_until(start_time + MIN(ofproto_max_idle, 500));
946 seq_wait(udpif->reval_seq, last_reval_seq);
947 latch_wait(&udpif->exit_latch);
948 latch_wait(&udpif->pause_latch);
949 poll_block();
950
951 if (!latch_is_set(&udpif->pause_latch) &&
952 !latch_is_set(&udpif->exit_latch)) {
953 long long int now = time_msec();
954 /* Block again if we are woken up within 5ms of the last start
955 * time. */
956 start_time += 5;
957
958 if (now < start_time) {
959 poll_timer_wait_until(start_time);
960 latch_wait(&udpif->exit_latch);
961 latch_wait(&udpif->pause_latch);
962 poll_block();
963 }
964 }
965 }
966 }
967
968 return NULL;
969 }
970 \f
971 static enum upcall_type
972 classify_upcall(enum dpif_upcall_type type, const struct nlattr *userdata)
973 {
974 union user_action_cookie cookie;
975 size_t userdata_len;
976
977 /* First look at the upcall type. */
978 switch (type) {
979 case DPIF_UC_ACTION:
980 break;
981
982 case DPIF_UC_MISS:
983 return MISS_UPCALL;
984
985 case DPIF_N_UC_TYPES:
986 default:
987 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, type);
988 return BAD_UPCALL;
989 }
990
991 /* "action" upcalls need a closer look. */
992 if (!userdata) {
993 VLOG_WARN_RL(&rl, "action upcall missing cookie");
994 return BAD_UPCALL;
995 }
996 userdata_len = nl_attr_get_size(userdata);
997 if (userdata_len < sizeof cookie.type
998 || userdata_len > sizeof cookie) {
999 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
1000 userdata_len);
1001 return BAD_UPCALL;
1002 }
1003 memset(&cookie, 0, sizeof cookie);
1004 memcpy(&cookie, nl_attr_get(userdata), userdata_len);
1005 if (userdata_len == MAX(8, sizeof cookie.sflow)
1006 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
1007 return SFLOW_UPCALL;
1008 } else if (userdata_len == MAX(8, sizeof cookie.slow_path)
1009 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
1010 return MISS_UPCALL;
1011 } else if (userdata_len == MAX(8, sizeof cookie.flow_sample)
1012 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
1013 return FLOW_SAMPLE_UPCALL;
1014 } else if (userdata_len == MAX(8, sizeof cookie.ipfix)
1015 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
1016 return IPFIX_UPCALL;
1017 } else {
1018 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
1019 " and size %"PRIuSIZE, cookie.type, userdata_len);
1020 return BAD_UPCALL;
1021 }
1022 }
1023
1024 /* Calculates slow path actions for 'xout'. 'buf' must statically be
1025 * initialized with at least 128 bytes of space. */
1026 static void
1027 compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
1028 const struct flow *flow, odp_port_t odp_in_port,
1029 struct ofpbuf *buf, uint32_t slowpath_meter_id,
1030 uint32_t controller_meter_id)
1031 {
1032 union user_action_cookie cookie;
1033 odp_port_t port;
1034 uint32_t pid;
1035
1036 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
1037 cookie.slow_path.unused = 0;
1038 cookie.slow_path.reason = xout->slow;
1039
1040 port = xout->slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)
1041 ? ODPP_NONE
1042 : odp_in_port;
1043 pid = dpif_port_get_pid(udpif->dpif, port, flow_hash_5tuple(flow, 0));
1044
1045 size_t offset;
1046 size_t ac_offset;
1047 uint32_t meter_id = xout->slow & SLOW_CONTROLLER ? controller_meter_id
1048 : slowpath_meter_id;
1049
1050 if (meter_id != UINT32_MAX) {
1051 /* If slowpath meter is configured, generate clone(meter, userspace)
1052 * action. */
1053 offset = nl_msg_start_nested(buf, OVS_ACTION_ATTR_SAMPLE);
1054 nl_msg_put_u32(buf, OVS_SAMPLE_ATTR_PROBABILITY, UINT32_MAX);
1055 ac_offset = nl_msg_start_nested(buf, OVS_SAMPLE_ATTR_ACTIONS);
1056 nl_msg_put_u32(buf, OVS_ACTION_ATTR_METER, meter_id);
1057 }
1058
1059 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path,
1060 ODPP_NONE, false, buf);
1061
1062 if (meter_id != UINT32_MAX) {
1063 nl_msg_end_nested(buf, ac_offset);
1064 nl_msg_end_nested(buf, offset);
1065 }
1066 }
1067
1068 /* If there is no error, the upcall must be destroyed with upcall_uninit()
1069 * before quiescing, as the referred objects are guaranteed to exist only
1070 * until the calling thread quiesces. Otherwise, do not call upcall_uninit()
1071 * since the 'upcall->put_actions' remains uninitialized. */
1072 static int
1073 upcall_receive(struct upcall *upcall, const struct dpif_backer *backer,
1074 const struct dp_packet *packet, enum dpif_upcall_type type,
1075 const struct nlattr *userdata, const struct flow *flow,
1076 const unsigned int mru,
1077 const ovs_u128 *ufid, const unsigned pmd_id)
1078 {
1079 int error;
1080
1081 error = xlate_lookup(backer, flow, &upcall->ofproto, &upcall->ipfix,
1082 &upcall->sflow, NULL, &upcall->in_port);
1083 if (error) {
1084 return error;
1085 }
1086
1087 upcall->recirc = NULL;
1088 upcall->have_recirc_ref = false;
1089 upcall->flow = flow;
1090 upcall->packet = packet;
1091 upcall->ufid = ufid;
1092 upcall->pmd_id = pmd_id;
1093 upcall->type = type;
1094 upcall->userdata = userdata;
1095 ofpbuf_use_stub(&upcall->odp_actions, upcall->odp_actions_stub,
1096 sizeof upcall->odp_actions_stub);
1097 ofpbuf_init(&upcall->put_actions, 0);
1098
1099 upcall->xout_initialized = false;
1100 upcall->ukey_persists = false;
1101
1102 upcall->ukey = NULL;
1103 upcall->key = NULL;
1104 upcall->key_len = 0;
1105 upcall->mru = mru;
1106
1107 upcall->out_tun_key = NULL;
1108 upcall->actions = NULL;
1109
1110 return 0;
1111 }
1112
1113 static void
1114 upcall_xlate(struct udpif *udpif, struct upcall *upcall,
1115 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
1116 {
1117 struct dpif_flow_stats stats;
1118 struct xlate_in xin;
1119
1120 stats.n_packets = 1;
1121 stats.n_bytes = dp_packet_size(upcall->packet);
1122 stats.used = time_msec();
1123 stats.tcp_flags = ntohs(upcall->flow->tcp_flags);
1124
1125 xlate_in_init(&xin, upcall->ofproto,
1126 ofproto_dpif_get_tables_version(upcall->ofproto),
1127 upcall->flow, upcall->in_port, NULL,
1128 stats.tcp_flags, upcall->packet, wc, odp_actions);
1129
1130 if (upcall->type == DPIF_UC_MISS) {
1131 xin.resubmit_stats = &stats;
1132
1133 if (xin.frozen_state) {
1134 /* We may install a datapath flow only if we get a reference to the
1135 * recirculation context (otherwise we could have recirculation
1136 * upcalls using recirculation ID for which no context can be
1137 * found). We may still execute the flow's actions even if we
1138 * don't install the flow. */
1139 upcall->recirc = recirc_id_node_from_state(xin.frozen_state);
1140 upcall->have_recirc_ref = recirc_id_node_try_ref_rcu(upcall->recirc);
1141 }
1142 } else {
1143 /* For non-miss upcalls, we are either executing actions (one of which
1144 * is an userspace action) for an upcall, in which case the stats have
1145 * already been taken care of, or there's a flow in the datapath which
1146 * this packet was accounted to. Presumably the revalidators will deal
1147 * with pushing its stats eventually. */
1148 }
1149
1150 upcall->dump_seq = seq_read(udpif->dump_seq);
1151 upcall->reval_seq = seq_read(udpif->reval_seq);
1152
1153 xlate_actions(&xin, &upcall->xout);
1154 if (wc) {
1155 /* Convert the input port wildcard from OFP to ODP format. There's no
1156 * real way to do this for arbitrary bitmasks since the numbering spaces
1157 * aren't the same. However, flow translation always exact matches the
1158 * whole thing, so we can do the same here. */
1159 WC_MASK_FIELD(wc, in_port.odp_port);
1160 }
1161
1162 upcall->xout_initialized = true;
1163
1164 if (!upcall->xout.slow) {
1165 ofpbuf_use_const(&upcall->put_actions,
1166 odp_actions->data, odp_actions->size);
1167 } else {
1168 uint32_t smid = upcall->ofproto->up.slowpath_meter_id;
1169 uint32_t cmid = upcall->ofproto->up.controller_meter_id;
1170 /* upcall->put_actions already initialized by upcall_receive(). */
1171 compose_slow_path(udpif, &upcall->xout, upcall->flow,
1172 upcall->flow->in_port.odp_port,
1173 &upcall->put_actions, smid, cmid);
1174 }
1175
1176 /* This function is also called for slow-pathed flows. As we are only
1177 * going to create new datapath flows for actual datapath misses, there is
1178 * no point in creating a ukey otherwise. */
1179 if (upcall->type == DPIF_UC_MISS) {
1180 upcall->ukey = ukey_create_from_upcall(upcall, wc);
1181 }
1182 }
1183
1184 static void
1185 upcall_uninit(struct upcall *upcall)
1186 {
1187 if (upcall) {
1188 if (upcall->xout_initialized) {
1189 xlate_out_uninit(&upcall->xout);
1190 }
1191 ofpbuf_uninit(&upcall->odp_actions);
1192 ofpbuf_uninit(&upcall->put_actions);
1193 if (upcall->ukey) {
1194 if (!upcall->ukey_persists) {
1195 ukey_delete__(upcall->ukey);
1196 }
1197 } else if (upcall->have_recirc_ref) {
1198 /* The reference was transferred to the ukey if one was created. */
1199 recirc_id_node_unref(upcall->recirc);
1200 }
1201 }
1202 }
1203
1204 /* If there are less flows than the limit, and this is a miss upcall which
1205 *
1206 * - Has no recirc_id, OR
1207 * - Has a recirc_id and we can get a reference on the recirc ctx,
1208 *
1209 * Then we should install the flow (true). Otherwise, return false. */
1210 static bool
1211 should_install_flow(struct udpif *udpif, struct upcall *upcall)
1212 {
1213 unsigned int flow_limit;
1214
1215 if (upcall->type != DPIF_UC_MISS) {
1216 return false;
1217 } else if (upcall->recirc && !upcall->have_recirc_ref) {
1218 VLOG_DBG_RL(&rl, "upcall: no reference for recirc flow");
1219 return false;
1220 }
1221
1222 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
1223 if (udpif_get_n_flows(udpif) >= flow_limit) {
1224 VLOG_WARN_RL(&rl, "upcall: datapath flow limit reached");
1225 return false;
1226 }
1227
1228 return true;
1229 }
1230
1231 static int
1232 upcall_cb(const struct dp_packet *packet, const struct flow *flow, ovs_u128 *ufid,
1233 unsigned pmd_id, enum dpif_upcall_type type,
1234 const struct nlattr *userdata, struct ofpbuf *actions,
1235 struct flow_wildcards *wc, struct ofpbuf *put_actions, void *aux)
1236 {
1237 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1238 struct udpif *udpif = aux;
1239 struct upcall upcall;
1240 bool megaflow;
1241 int error;
1242
1243 atomic_read_relaxed(&enable_megaflows, &megaflow);
1244
1245 error = upcall_receive(&upcall, udpif->backer, packet, type, userdata,
1246 flow, 0, ufid, pmd_id);
1247 if (error) {
1248 return error;
1249 }
1250
1251 error = process_upcall(udpif, &upcall, actions, wc);
1252 if (error) {
1253 goto out;
1254 }
1255
1256 if (upcall.xout.slow && put_actions) {
1257 ofpbuf_put(put_actions, upcall.put_actions.data,
1258 upcall.put_actions.size);
1259 }
1260
1261 if (OVS_UNLIKELY(!megaflow && wc)) {
1262 flow_wildcards_init_for_packet(wc, flow);
1263 }
1264
1265 if (!should_install_flow(udpif, &upcall)) {
1266 error = ENOSPC;
1267 goto out;
1268 }
1269
1270 if (upcall.ukey && !ukey_install(udpif, upcall.ukey)) {
1271 VLOG_WARN_RL(&rl, "upcall_cb failure: ukey installation fails");
1272 error = ENOSPC;
1273 }
1274 out:
1275 if (!error) {
1276 upcall.ukey_persists = true;
1277 }
1278 upcall_uninit(&upcall);
1279 return error;
1280 }
1281
1282 static size_t
1283 dpif_get_actions(struct udpif *udpif, struct upcall *upcall,
1284 const struct nlattr **actions)
1285 {
1286 size_t actions_len = 0;
1287
1288 if (upcall->actions) {
1289 /* Actions were passed up from datapath. */
1290 *actions = nl_attr_get(upcall->actions);
1291 actions_len = nl_attr_get_size(upcall->actions);
1292 }
1293
1294 if (actions_len == 0) {
1295 /* Lookup actions in userspace cache. */
1296 struct udpif_key *ukey = ukey_lookup(udpif, upcall->ufid,
1297 upcall->pmd_id);
1298 if (ukey) {
1299 ukey_get_actions(ukey, actions, &actions_len);
1300 }
1301 }
1302
1303 return actions_len;
1304 }
1305
1306 static size_t
1307 dpif_read_actions(struct udpif *udpif, struct upcall *upcall,
1308 const struct flow *flow, enum upcall_type type,
1309 void *upcall_data)
1310 {
1311 const struct nlattr *actions = NULL;
1312 size_t actions_len = dpif_get_actions(udpif, upcall, &actions);
1313
1314 if (!actions || !actions_len) {
1315 return 0;
1316 }
1317
1318 switch (type) {
1319 case SFLOW_UPCALL:
1320 dpif_sflow_read_actions(flow, actions, actions_len, upcall_data);
1321 break;
1322 case FLOW_SAMPLE_UPCALL:
1323 case IPFIX_UPCALL:
1324 dpif_ipfix_read_actions(flow, actions, actions_len, upcall_data);
1325 break;
1326 case BAD_UPCALL:
1327 case MISS_UPCALL:
1328 default:
1329 break;
1330 }
1331
1332 return actions_len;
1333 }
1334
1335 static int
1336 process_upcall(struct udpif *udpif, struct upcall *upcall,
1337 struct ofpbuf *odp_actions, struct flow_wildcards *wc)
1338 {
1339 const struct nlattr *userdata = upcall->userdata;
1340 const struct dp_packet *packet = upcall->packet;
1341 const struct flow *flow = upcall->flow;
1342 size_t actions_len = 0;
1343 enum upcall_type upcall_type = classify_upcall(upcall->type, userdata);
1344
1345 switch (upcall_type) {
1346 case MISS_UPCALL:
1347 upcall_xlate(udpif, upcall, odp_actions, wc);
1348 return 0;
1349
1350 case SFLOW_UPCALL:
1351 if (upcall->sflow) {
1352 union user_action_cookie cookie;
1353 struct dpif_sflow_actions sflow_actions;
1354
1355 memset(&sflow_actions, 0, sizeof sflow_actions);
1356 memset(&cookie, 0, sizeof cookie);
1357 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.sflow);
1358
1359 actions_len = dpif_read_actions(udpif, upcall, flow, upcall_type,
1360 &sflow_actions);
1361 dpif_sflow_received(upcall->sflow, packet, flow,
1362 flow->in_port.odp_port, &cookie,
1363 actions_len > 0 ? &sflow_actions : NULL);
1364 }
1365 break;
1366
1367 case IPFIX_UPCALL:
1368 if (upcall->ipfix) {
1369 union user_action_cookie cookie;
1370 struct flow_tnl output_tunnel_key;
1371 struct dpif_ipfix_actions ipfix_actions;
1372
1373 memset(&cookie, 0, sizeof cookie);
1374 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.ipfix);
1375 memset(&ipfix_actions, 0, sizeof ipfix_actions);
1376
1377 if (upcall->out_tun_key) {
1378 odp_tun_key_from_attr(upcall->out_tun_key, &output_tunnel_key);
1379 }
1380
1381 actions_len = dpif_read_actions(udpif, upcall, flow, upcall_type,
1382 &ipfix_actions);
1383 dpif_ipfix_bridge_sample(upcall->ipfix, packet, flow,
1384 flow->in_port.odp_port,
1385 cookie.ipfix.output_odp_port,
1386 upcall->out_tun_key ?
1387 &output_tunnel_key : NULL,
1388 actions_len > 0 ? &ipfix_actions: NULL);
1389 }
1390 break;
1391
1392 case FLOW_SAMPLE_UPCALL:
1393 if (upcall->ipfix) {
1394 union user_action_cookie cookie;
1395 struct flow_tnl output_tunnel_key;
1396 struct dpif_ipfix_actions ipfix_actions;
1397
1398 memset(&cookie, 0, sizeof cookie);
1399 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.flow_sample);
1400 memset(&ipfix_actions, 0, sizeof ipfix_actions);
1401
1402 if (upcall->out_tun_key) {
1403 odp_tun_key_from_attr(upcall->out_tun_key, &output_tunnel_key);
1404 }
1405
1406 actions_len = dpif_read_actions(udpif, upcall, flow, upcall_type,
1407 &ipfix_actions);
1408 /* The flow reflects exactly the contents of the packet.
1409 * Sample the packet using it. */
1410 dpif_ipfix_flow_sample(upcall->ipfix, packet, flow,
1411 &cookie, flow->in_port.odp_port,
1412 upcall->out_tun_key ?
1413 &output_tunnel_key : NULL,
1414 actions_len > 0 ? &ipfix_actions: NULL);
1415 }
1416 break;
1417
1418 case BAD_UPCALL:
1419 break;
1420 }
1421
1422 return EAGAIN;
1423 }
1424
1425 static void
1426 handle_upcalls(struct udpif *udpif, struct upcall *upcalls,
1427 size_t n_upcalls)
1428 {
1429 struct dpif_op *opsp[UPCALL_MAX_BATCH * 2];
1430 struct ukey_op ops[UPCALL_MAX_BATCH * 2];
1431 size_t n_ops, n_opsp, i;
1432
1433 /* Handle the packets individually in order of arrival.
1434 *
1435 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, SLOW_BFD, and SLOW_LLDP,
1436 * translation is what processes received packets for these
1437 * protocols.
1438 *
1439 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1440 * controller.
1441 *
1442 * - For SLOW_ACTION, translation executes the actions directly.
1443 *
1444 * The loop fills 'ops' with an array of operations to execute in the
1445 * datapath. */
1446 n_ops = 0;
1447 for (i = 0; i < n_upcalls; i++) {
1448 struct upcall *upcall = &upcalls[i];
1449 const struct dp_packet *packet = upcall->packet;
1450 struct ukey_op *op;
1451
1452 if (should_install_flow(udpif, upcall)) {
1453 struct udpif_key *ukey = upcall->ukey;
1454
1455 if (ukey_install(udpif, ukey)) {
1456 upcall->ukey_persists = true;
1457 put_op_init(&ops[n_ops++], ukey, DPIF_FP_CREATE);
1458 }
1459 }
1460
1461 if (upcall->odp_actions.size) {
1462 op = &ops[n_ops++];
1463 op->ukey = NULL;
1464 op->dop.type = DPIF_OP_EXECUTE;
1465 op->dop.u.execute.packet = CONST_CAST(struct dp_packet *, packet);
1466 op->dop.u.execute.flow = upcall->flow;
1467 odp_key_to_dp_packet(upcall->key, upcall->key_len,
1468 op->dop.u.execute.packet);
1469 op->dop.u.execute.actions = upcall->odp_actions.data;
1470 op->dop.u.execute.actions_len = upcall->odp_actions.size;
1471 op->dop.u.execute.needs_help = (upcall->xout.slow & SLOW_ACTION) != 0;
1472 op->dop.u.execute.probe = false;
1473 op->dop.u.execute.mtu = upcall->mru;
1474 }
1475 }
1476
1477 /* Execute batch. */
1478 n_opsp = 0;
1479 for (i = 0; i < n_ops; i++) {
1480 opsp[n_opsp++] = &ops[i].dop;
1481 }
1482 dpif_operate(udpif->dpif, opsp, n_opsp);
1483 for (i = 0; i < n_ops; i++) {
1484 struct udpif_key *ukey = ops[i].ukey;
1485
1486 if (ukey) {
1487 ovs_mutex_lock(&ukey->mutex);
1488 if (ops[i].dop.error) {
1489 transition_ukey(ukey, UKEY_EVICTED);
1490 } else if (ukey->state < UKEY_OPERATIONAL) {
1491 transition_ukey(ukey, UKEY_OPERATIONAL);
1492 }
1493 ovs_mutex_unlock(&ukey->mutex);
1494 }
1495 }
1496 }
1497
1498 static uint32_t
1499 get_ukey_hash(const ovs_u128 *ufid, const unsigned pmd_id)
1500 {
1501 return hash_2words(ufid->u32[0], pmd_id);
1502 }
1503
1504 static struct udpif_key *
1505 ukey_lookup(struct udpif *udpif, const ovs_u128 *ufid, const unsigned pmd_id)
1506 {
1507 struct udpif_key *ukey;
1508 int idx = get_ukey_hash(ufid, pmd_id) % N_UMAPS;
1509 struct cmap *cmap = &udpif->ukeys[idx].cmap;
1510
1511 CMAP_FOR_EACH_WITH_HASH (ukey, cmap_node,
1512 get_ukey_hash(ufid, pmd_id), cmap) {
1513 if (ovs_u128_equals(ukey->ufid, *ufid)) {
1514 return ukey;
1515 }
1516 }
1517 return NULL;
1518 }
1519
1520 /* Provides safe lockless access of RCU protected 'ukey->actions'. Callers may
1521 * alternatively access the field directly if they take 'ukey->mutex'. */
1522 static void
1523 ukey_get_actions(struct udpif_key *ukey, const struct nlattr **actions, size_t *size)
1524 {
1525 const struct ofpbuf *buf = ovsrcu_get(struct ofpbuf *, &ukey->actions);
1526 *actions = buf->data;
1527 *size = buf->size;
1528 }
1529
1530 static void
1531 ukey_set_actions(struct udpif_key *ukey, const struct ofpbuf *actions)
1532 {
1533 ovsrcu_postpone(ofpbuf_delete,
1534 ovsrcu_get_protected(struct ofpbuf *, &ukey->actions));
1535 ovsrcu_set(&ukey->actions, ofpbuf_clone(actions));
1536 }
1537
1538 static struct udpif_key *
1539 ukey_create__(const struct nlattr *key, size_t key_len,
1540 const struct nlattr *mask, size_t mask_len,
1541 bool ufid_present, const ovs_u128 *ufid,
1542 const unsigned pmd_id, const struct ofpbuf *actions,
1543 uint64_t dump_seq, uint64_t reval_seq, long long int used,
1544 uint32_t key_recirc_id, struct xlate_out *xout)
1545 OVS_NO_THREAD_SAFETY_ANALYSIS
1546 {
1547 struct udpif_key *ukey = xmalloc(sizeof *ukey);
1548
1549 memcpy(&ukey->keybuf, key, key_len);
1550 ukey->key = &ukey->keybuf.nla;
1551 ukey->key_len = key_len;
1552 memcpy(&ukey->maskbuf, mask, mask_len);
1553 ukey->mask = &ukey->maskbuf.nla;
1554 ukey->mask_len = mask_len;
1555 ukey->ufid_present = ufid_present;
1556 ukey->ufid = *ufid;
1557 ukey->pmd_id = pmd_id;
1558 ukey->hash = get_ukey_hash(&ukey->ufid, pmd_id);
1559
1560 ovsrcu_init(&ukey->actions, NULL);
1561 ukey_set_actions(ukey, actions);
1562
1563 ovs_mutex_init(&ukey->mutex);
1564 ukey->dump_seq = dump_seq;
1565 ukey->reval_seq = reval_seq;
1566 ukey->state = UKEY_CREATED;
1567 ukey->state_thread = ovsthread_id_self();
1568 ukey->state_where = OVS_SOURCE_LOCATOR;
1569 ukey->created = time_msec();
1570 memset(&ukey->stats, 0, sizeof ukey->stats);
1571 ukey->stats.used = used;
1572 ukey->xcache = NULL;
1573
1574 ukey->key_recirc_id = key_recirc_id;
1575 recirc_refs_init(&ukey->recircs);
1576 if (xout) {
1577 /* Take ownership of the action recirc id references. */
1578 recirc_refs_swap(&ukey->recircs, &xout->recircs);
1579 }
1580
1581 return ukey;
1582 }
1583
1584 static struct udpif_key *
1585 ukey_create_from_upcall(struct upcall *upcall, struct flow_wildcards *wc)
1586 {
1587 struct odputil_keybuf keystub, maskstub;
1588 struct ofpbuf keybuf, maskbuf;
1589 bool megaflow;
1590 struct odp_flow_key_parms odp_parms = {
1591 .flow = upcall->flow,
1592 .mask = wc ? &wc->masks : NULL,
1593 };
1594
1595 odp_parms.support = upcall->ofproto->backer->rt_support.odp;
1596 if (upcall->key_len) {
1597 ofpbuf_use_const(&keybuf, upcall->key, upcall->key_len);
1598 } else {
1599 /* dpif-netdev doesn't provide a netlink-formatted flow key in the
1600 * upcall, so convert the upcall's flow here. */
1601 ofpbuf_use_stack(&keybuf, &keystub, sizeof keystub);
1602 odp_flow_key_from_flow(&odp_parms, &keybuf);
1603 }
1604
1605 atomic_read_relaxed(&enable_megaflows, &megaflow);
1606 ofpbuf_use_stack(&maskbuf, &maskstub, sizeof maskstub);
1607 if (megaflow && wc) {
1608 odp_parms.key_buf = &keybuf;
1609 odp_flow_key_from_mask(&odp_parms, &maskbuf);
1610 }
1611
1612 return ukey_create__(keybuf.data, keybuf.size, maskbuf.data, maskbuf.size,
1613 true, upcall->ufid, upcall->pmd_id,
1614 &upcall->put_actions, upcall->dump_seq,
1615 upcall->reval_seq, 0,
1616 upcall->have_recirc_ref ? upcall->recirc->id : 0,
1617 &upcall->xout);
1618 }
1619
1620 static int
1621 ukey_create_from_dpif_flow(const struct udpif *udpif,
1622 const struct dpif_flow *flow,
1623 struct udpif_key **ukey)
1624 {
1625 struct dpif_flow full_flow;
1626 struct ofpbuf actions;
1627 uint64_t dump_seq, reval_seq;
1628 uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
1629 const struct nlattr *a;
1630 unsigned int left;
1631
1632 if (!flow->key_len || !flow->actions_len) {
1633 struct ofpbuf buf;
1634 int err;
1635
1636 /* If the key or actions were not provided by the datapath, fetch the
1637 * full flow. */
1638 ofpbuf_use_stack(&buf, &stub, sizeof stub);
1639 err = dpif_flow_get(udpif->dpif, flow->key, flow->key_len,
1640 flow->ufid_present ? &flow->ufid : NULL,
1641 flow->pmd_id, &buf, &full_flow);
1642 if (err) {
1643 return err;
1644 }
1645 flow = &full_flow;
1646 }
1647
1648 /* Check the flow actions for recirculation action. As recirculation
1649 * relies on OVS userspace internal state, we need to delete all old
1650 * datapath flows with either a non-zero recirc_id in the key, or any
1651 * recirculation actions upon OVS restart. */
1652 NL_ATTR_FOR_EACH (a, left, flow->key, flow->key_len) {
1653 if (nl_attr_type(a) == OVS_KEY_ATTR_RECIRC_ID
1654 && nl_attr_get_u32(a) != 0) {
1655 return EINVAL;
1656 }
1657 }
1658 NL_ATTR_FOR_EACH (a, left, flow->actions, flow->actions_len) {
1659 if (nl_attr_type(a) == OVS_ACTION_ATTR_RECIRC) {
1660 return EINVAL;
1661 }
1662 }
1663
1664 dump_seq = seq_read(udpif->dump_seq);
1665 reval_seq = seq_read(udpif->reval_seq) - 1; /* Ensure revalidation. */
1666 ofpbuf_use_const(&actions, &flow->actions, flow->actions_len);
1667 *ukey = ukey_create__(flow->key, flow->key_len,
1668 flow->mask, flow->mask_len, flow->ufid_present,
1669 &flow->ufid, flow->pmd_id, &actions, dump_seq,
1670 reval_seq, flow->stats.used, 0, NULL);
1671
1672 return 0;
1673 }
1674
1675 static bool
1676 try_ukey_replace(struct umap *umap, struct udpif_key *old_ukey,
1677 struct udpif_key *new_ukey)
1678 OVS_REQUIRES(umap->mutex)
1679 OVS_TRY_LOCK(true, new_ukey->mutex)
1680 {
1681 bool replaced = false;
1682
1683 if (!ovs_mutex_trylock(&old_ukey->mutex)) {
1684 if (old_ukey->state == UKEY_EVICTED) {
1685 /* The flow was deleted during the current revalidator dump,
1686 * but its ukey won't be fully cleaned up until the sweep phase.
1687 * In the mean time, we are receiving upcalls for this traffic.
1688 * Expedite the (new) flow install by replacing the ukey. */
1689 ovs_mutex_lock(&new_ukey->mutex);
1690 cmap_replace(&umap->cmap, &old_ukey->cmap_node,
1691 &new_ukey->cmap_node, new_ukey->hash);
1692 ovsrcu_postpone(ukey_delete__, old_ukey);
1693 transition_ukey(old_ukey, UKEY_DELETED);
1694 transition_ukey(new_ukey, UKEY_VISIBLE);
1695 replaced = true;
1696 }
1697 ovs_mutex_unlock(&old_ukey->mutex);
1698 }
1699
1700 if (replaced) {
1701 COVERAGE_INC(upcall_ukey_replace);
1702 } else {
1703 COVERAGE_INC(handler_duplicate_upcall);
1704 }
1705 return replaced;
1706 }
1707
1708 /* Attempts to insert a ukey into the shared ukey maps.
1709 *
1710 * On success, returns true, installs the ukey and returns it in a locked
1711 * state. Otherwise, returns false. */
1712 static bool
1713 ukey_install__(struct udpif *udpif, struct udpif_key *new_ukey)
1714 OVS_TRY_LOCK(true, new_ukey->mutex)
1715 {
1716 struct umap *umap;
1717 struct udpif_key *old_ukey;
1718 uint32_t idx;
1719 bool locked = false;
1720
1721 idx = new_ukey->hash % N_UMAPS;
1722 umap = &udpif->ukeys[idx];
1723 ovs_mutex_lock(&umap->mutex);
1724 old_ukey = ukey_lookup(udpif, &new_ukey->ufid, new_ukey->pmd_id);
1725 if (old_ukey) {
1726 /* Uncommon case: A ukey is already installed with the same UFID. */
1727 if (old_ukey->key_len == new_ukey->key_len
1728 && !memcmp(old_ukey->key, new_ukey->key, new_ukey->key_len)) {
1729 locked = try_ukey_replace(umap, old_ukey, new_ukey);
1730 } else {
1731 struct ds ds = DS_EMPTY_INITIALIZER;
1732
1733 odp_format_ufid(&old_ukey->ufid, &ds);
1734 ds_put_cstr(&ds, " ");
1735 odp_flow_key_format(old_ukey->key, old_ukey->key_len, &ds);
1736 ds_put_cstr(&ds, "\n");
1737 odp_format_ufid(&new_ukey->ufid, &ds);
1738 ds_put_cstr(&ds, " ");
1739 odp_flow_key_format(new_ukey->key, new_ukey->key_len, &ds);
1740
1741 VLOG_WARN_RL(&rl, "Conflicting ukey for flows:\n%s", ds_cstr(&ds));
1742 ds_destroy(&ds);
1743 }
1744 } else {
1745 ovs_mutex_lock(&new_ukey->mutex);
1746 cmap_insert(&umap->cmap, &new_ukey->cmap_node, new_ukey->hash);
1747 transition_ukey(new_ukey, UKEY_VISIBLE);
1748 locked = true;
1749 }
1750 ovs_mutex_unlock(&umap->mutex);
1751
1752 return locked;
1753 }
1754
1755 static void
1756 transition_ukey_at(struct udpif_key *ukey, enum ukey_state dst,
1757 const char *where)
1758 OVS_REQUIRES(ukey->mutex)
1759 {
1760 if (dst < ukey->state) {
1761 VLOG_ABORT("Invalid ukey transition %d->%d (last transitioned from "
1762 "thread %u at %s)", ukey->state, dst, ukey->state_thread,
1763 ukey->state_where);
1764 }
1765 if (ukey->state == dst && dst == UKEY_OPERATIONAL) {
1766 return;
1767 }
1768
1769 /* Valid state transitions:
1770 * UKEY_CREATED -> UKEY_VISIBLE
1771 * Ukey is now visible in the umap.
1772 * UKEY_VISIBLE -> UKEY_OPERATIONAL
1773 * A handler has installed the flow, and the flow is in the datapath.
1774 * UKEY_VISIBLE -> UKEY_EVICTING
1775 * A handler installs the flow, then revalidator sweeps the ukey before
1776 * the flow is dumped. Most likely the flow was installed; start trying
1777 * to delete it.
1778 * UKEY_VISIBLE -> UKEY_EVICTED
1779 * A handler attempts to install the flow, but the datapath rejects it.
1780 * Consider that the datapath has already destroyed it.
1781 * UKEY_OPERATIONAL -> UKEY_EVICTING
1782 * A revalidator decides to evict the datapath flow.
1783 * UKEY_EVICTING -> UKEY_EVICTED
1784 * A revalidator has evicted the datapath flow.
1785 * UKEY_EVICTED -> UKEY_DELETED
1786 * A revalidator has removed the ukey from the umap and is deleting it.
1787 */
1788 if (ukey->state == dst - 1 || (ukey->state == UKEY_VISIBLE &&
1789 dst < UKEY_DELETED)) {
1790 ukey->state = dst;
1791 } else {
1792 struct ds ds = DS_EMPTY_INITIALIZER;
1793
1794 odp_format_ufid(&ukey->ufid, &ds);
1795 VLOG_WARN_RL(&rl, "Invalid state transition for ukey %s: %d -> %d",
1796 ds_cstr(&ds), ukey->state, dst);
1797 ds_destroy(&ds);
1798 }
1799 ukey->state_thread = ovsthread_id_self();
1800 ukey->state_where = where;
1801 }
1802
1803 static bool
1804 ukey_install(struct udpif *udpif, struct udpif_key *ukey)
1805 {
1806 bool installed;
1807
1808 installed = ukey_install__(udpif, ukey);
1809 if (installed) {
1810 ovs_mutex_unlock(&ukey->mutex);
1811 }
1812
1813 return installed;
1814 }
1815
1816 /* Searches for a ukey in 'udpif->ukeys' that matches 'flow' and attempts to
1817 * lock the ukey. If the ukey does not exist, create it.
1818 *
1819 * Returns 0 on success, setting *result to the matching ukey and returning it
1820 * in a locked state. Otherwise, returns an errno and clears *result. EBUSY
1821 * indicates that another thread is handling this flow. Other errors indicate
1822 * an unexpected condition creating a new ukey.
1823 *
1824 * *error is an output parameter provided to appease the threadsafety analyser,
1825 * and its value matches the return value. */
1826 static int
1827 ukey_acquire(struct udpif *udpif, const struct dpif_flow *flow,
1828 struct udpif_key **result, int *error)
1829 OVS_TRY_LOCK(0, (*result)->mutex)
1830 {
1831 struct udpif_key *ukey;
1832 int retval;
1833
1834 ukey = ukey_lookup(udpif, &flow->ufid, flow->pmd_id);
1835 if (ukey) {
1836 retval = ovs_mutex_trylock(&ukey->mutex);
1837 } else {
1838 /* Usually we try to avoid installing flows from revalidator threads,
1839 * because locking on a umap may cause handler threads to block.
1840 * However there are certain cases, like when ovs-vswitchd is
1841 * restarted, where it is desirable to handle flows that exist in the
1842 * datapath gracefully (ie, don't just clear the datapath). */
1843 bool install;
1844
1845 retval = ukey_create_from_dpif_flow(udpif, flow, &ukey);
1846 if (retval) {
1847 goto done;
1848 }
1849 install = ukey_install__(udpif, ukey);
1850 if (install) {
1851 retval = 0;
1852 } else {
1853 ukey_delete__(ukey);
1854 retval = EBUSY;
1855 }
1856 }
1857
1858 done:
1859 *error = retval;
1860 if (retval) {
1861 *result = NULL;
1862 } else {
1863 *result = ukey;
1864 }
1865 return retval;
1866 }
1867
1868 static void
1869 ukey_delete__(struct udpif_key *ukey)
1870 OVS_NO_THREAD_SAFETY_ANALYSIS
1871 {
1872 if (ukey) {
1873 if (ukey->key_recirc_id) {
1874 recirc_free_id(ukey->key_recirc_id);
1875 }
1876 recirc_refs_unref(&ukey->recircs);
1877 xlate_cache_delete(ukey->xcache);
1878 ofpbuf_delete(ovsrcu_get(struct ofpbuf *, &ukey->actions));
1879 ovs_mutex_destroy(&ukey->mutex);
1880 free(ukey);
1881 }
1882 }
1883
1884 static void
1885 ukey_delete(struct umap *umap, struct udpif_key *ukey)
1886 OVS_REQUIRES(umap->mutex)
1887 {
1888 ovs_mutex_lock(&ukey->mutex);
1889 if (ukey->state < UKEY_DELETED) {
1890 cmap_remove(&umap->cmap, &ukey->cmap_node, ukey->hash);
1891 ovsrcu_postpone(ukey_delete__, ukey);
1892 transition_ukey(ukey, UKEY_DELETED);
1893 }
1894 ovs_mutex_unlock(&ukey->mutex);
1895 }
1896
1897 static bool
1898 should_revalidate(const struct udpif *udpif, uint64_t packets,
1899 long long int used)
1900 {
1901 long long int metric, now, duration;
1902
1903 if (!used) {
1904 /* Always revalidate the first time a flow is dumped. */
1905 return true;
1906 }
1907
1908 if (udpif->dump_duration < 200) {
1909 /* We are likely to handle full revalidation for the flows. */
1910 return true;
1911 }
1912
1913 /* Calculate the mean time between seeing these packets. If this
1914 * exceeds the threshold, then delete the flow rather than performing
1915 * costly revalidation for flows that aren't being hit frequently.
1916 *
1917 * This is targeted at situations where the dump_duration is high (~1s),
1918 * and revalidation is triggered by a call to udpif_revalidate(). In
1919 * these situations, revalidation of all flows causes fluctuations in the
1920 * flow_limit due to the interaction with the dump_duration and max_idle.
1921 * This tends to result in deletion of low-throughput flows anyway, so
1922 * skip the revalidation and just delete those flows. */
1923 packets = MAX(packets, 1);
1924 now = MAX(used, time_msec());
1925 duration = now - used;
1926 metric = duration / packets;
1927
1928 if (metric < 200) {
1929 /* The flow is receiving more than ~5pps, so keep it. */
1930 return true;
1931 }
1932 return false;
1933 }
1934
1935 struct reval_context {
1936 /* Optional output parameters */
1937 struct flow_wildcards *wc;
1938 struct ofpbuf *odp_actions;
1939 struct netflow **netflow;
1940 struct xlate_cache *xcache;
1941
1942 /* Required output parameters */
1943 struct xlate_out xout;
1944 struct flow flow;
1945 };
1946
1947 /* Translates 'key' into a flow, populating 'ctx' as it goes along.
1948 *
1949 * Returns 0 on success, otherwise a positive errno value.
1950 *
1951 * The caller is responsible for uninitializing ctx->xout on success.
1952 */
1953 static int
1954 xlate_key(struct udpif *udpif, const struct nlattr *key, unsigned int len,
1955 const struct dpif_flow_stats *push, struct reval_context *ctx)
1956 {
1957 struct ofproto_dpif *ofproto;
1958 ofp_port_t ofp_in_port;
1959 struct xlate_in xin;
1960 int error;
1961
1962 if (odp_flow_key_to_flow(key, len, &ctx->flow) == ODP_FIT_ERROR) {
1963 return EINVAL;
1964 }
1965
1966 error = xlate_lookup(udpif->backer, &ctx->flow, &ofproto, NULL, NULL,
1967 ctx->netflow, &ofp_in_port);
1968 if (error) {
1969 return error;
1970 }
1971
1972 xlate_in_init(&xin, ofproto, ofproto_dpif_get_tables_version(ofproto),
1973 &ctx->flow, ofp_in_port, NULL, push->tcp_flags,
1974 NULL, ctx->wc, ctx->odp_actions);
1975 if (push->n_packets) {
1976 xin.resubmit_stats = push;
1977 xin.allow_side_effects = true;
1978 }
1979 xin.xcache = ctx->xcache;
1980 xlate_actions(&xin, &ctx->xout);
1981
1982 return 0;
1983 }
1984
1985 static int
1986 xlate_ukey(struct udpif *udpif, const struct udpif_key *ukey,
1987 uint16_t tcp_flags, struct reval_context *ctx)
1988 {
1989 struct dpif_flow_stats push = {
1990 .tcp_flags = tcp_flags,
1991 };
1992 return xlate_key(udpif, ukey->key, ukey->key_len, &push, ctx);
1993 }
1994
1995 static int
1996 populate_xcache(struct udpif *udpif, struct udpif_key *ukey,
1997 uint16_t tcp_flags)
1998 OVS_REQUIRES(ukey->mutex)
1999 {
2000 struct reval_context ctx = {
2001 .odp_actions = NULL,
2002 .netflow = NULL,
2003 .wc = NULL,
2004 };
2005 int error;
2006
2007 ovs_assert(!ukey->xcache);
2008 ukey->xcache = ctx.xcache = xlate_cache_new();
2009 error = xlate_ukey(udpif, ukey, tcp_flags, &ctx);
2010 if (error) {
2011 return error;
2012 }
2013 xlate_out_uninit(&ctx.xout);
2014
2015 return 0;
2016 }
2017
2018 static enum reval_result
2019 revalidate_ukey__(struct udpif *udpif, const struct udpif_key *ukey,
2020 uint16_t tcp_flags, struct ofpbuf *odp_actions,
2021 struct recirc_refs *recircs, struct xlate_cache *xcache)
2022 {
2023 struct xlate_out *xoutp;
2024 struct netflow *netflow;
2025 struct flow_wildcards dp_mask, wc;
2026 enum reval_result result;
2027 struct reval_context ctx = {
2028 .odp_actions = odp_actions,
2029 .netflow = &netflow,
2030 .xcache = xcache,
2031 .wc = &wc,
2032 };
2033
2034 result = UKEY_DELETE;
2035 xoutp = NULL;
2036 netflow = NULL;
2037
2038 if (xlate_ukey(udpif, ukey, tcp_flags, &ctx)) {
2039 goto exit;
2040 }
2041 xoutp = &ctx.xout;
2042
2043 if (xoutp->avoid_caching) {
2044 goto exit;
2045 }
2046
2047 if (xoutp->slow) {
2048 struct ofproto_dpif *ofproto;
2049 ofproto = xlate_lookup_ofproto(udpif->backer, &ctx.flow, NULL);
2050 uint32_t smid = ofproto ? ofproto->up.slowpath_meter_id : UINT32_MAX;
2051 uint32_t cmid = ofproto ? ofproto->up.controller_meter_id : UINT32_MAX;
2052
2053 ofpbuf_clear(odp_actions);
2054 compose_slow_path(udpif, xoutp, &ctx.flow, ctx.flow.in_port.odp_port,
2055 odp_actions, smid, cmid);
2056 }
2057
2058 if (odp_flow_key_to_mask(ukey->mask, ukey->mask_len, &dp_mask, &ctx.flow)
2059 == ODP_FIT_ERROR) {
2060 goto exit;
2061 }
2062
2063 /* Do not modify if any bit is wildcarded by the installed datapath flow,
2064 * but not the newly revalidated wildcard mask (wc), i.e., if revalidation
2065 * tells that the datapath flow is now too generic and must be narrowed
2066 * down. Note that we do not know if the datapath has ignored any of the
2067 * wildcarded bits, so we may be overtly conservative here. */
2068 if (flow_wildcards_has_extra(&dp_mask, ctx.wc)) {
2069 goto exit;
2070 }
2071
2072 if (!ofpbuf_equal(odp_actions,
2073 ovsrcu_get(struct ofpbuf *, &ukey->actions))) {
2074 /* The datapath mask was OK, but the actions seem to have changed.
2075 * Let's modify it in place. */
2076 result = UKEY_MODIFY;
2077 /* Transfer recirc action ID references to the caller. */
2078 recirc_refs_swap(recircs, &xoutp->recircs);
2079 goto exit;
2080 }
2081
2082 result = UKEY_KEEP;
2083
2084 exit:
2085 if (netflow && result == UKEY_DELETE) {
2086 netflow_flow_clear(netflow, &ctx.flow);
2087 }
2088 xlate_out_uninit(xoutp);
2089 return result;
2090 }
2091
2092 /* Verifies that the datapath actions of 'ukey' are still correct, and pushes
2093 * 'stats' for it.
2094 *
2095 * Returns a recommended action for 'ukey', options include:
2096 * UKEY_DELETE The ukey should be deleted.
2097 * UKEY_KEEP The ukey is fine as is.
2098 * UKEY_MODIFY The ukey's actions should be changed but is otherwise
2099 * fine. Callers should change the actions to those found
2100 * in the caller supplied 'odp_actions' buffer. The
2101 * recirculation references can be found in 'recircs' and
2102 * must be handled by the caller.
2103 *
2104 * If the result is UKEY_MODIFY, then references to all recirc_ids used by the
2105 * new flow will be held within 'recircs' (which may be none).
2106 *
2107 * The caller is responsible for both initializing 'recircs' prior this call,
2108 * and ensuring any references are eventually freed.
2109 */
2110 static enum reval_result
2111 revalidate_ukey(struct udpif *udpif, struct udpif_key *ukey,
2112 const struct dpif_flow_stats *stats,
2113 struct ofpbuf *odp_actions, uint64_t reval_seq,
2114 struct recirc_refs *recircs)
2115 OVS_REQUIRES(ukey->mutex)
2116 {
2117 bool need_revalidate = ukey->reval_seq != reval_seq;
2118 enum reval_result result = UKEY_DELETE;
2119 struct dpif_flow_stats push;
2120
2121 ofpbuf_clear(odp_actions);
2122
2123 push.used = stats->used;
2124 push.tcp_flags = stats->tcp_flags;
2125 push.n_packets = (stats->n_packets > ukey->stats.n_packets
2126 ? stats->n_packets - ukey->stats.n_packets
2127 : 0);
2128 push.n_bytes = (stats->n_bytes > ukey->stats.n_bytes
2129 ? stats->n_bytes - ukey->stats.n_bytes
2130 : 0);
2131
2132 if (need_revalidate) {
2133 if (should_revalidate(udpif, push.n_packets, ukey->stats.used)) {
2134 if (!ukey->xcache) {
2135 ukey->xcache = xlate_cache_new();
2136 } else {
2137 xlate_cache_clear(ukey->xcache);
2138 }
2139 result = revalidate_ukey__(udpif, ukey, push.tcp_flags,
2140 odp_actions, recircs, ukey->xcache);
2141 } /* else delete; too expensive to revalidate */
2142 } else if (!push.n_packets || ukey->xcache
2143 || !populate_xcache(udpif, ukey, push.tcp_flags)) {
2144 result = UKEY_KEEP;
2145 }
2146
2147 /* Stats for deleted flows will be attributed upon flow deletion. Skip. */
2148 if (result != UKEY_DELETE) {
2149 xlate_push_stats(ukey->xcache, &push);
2150 ukey->stats = *stats;
2151 ukey->reval_seq = reval_seq;
2152 }
2153
2154 return result;
2155 }
2156
2157 static void
2158 delete_op_init__(struct udpif *udpif, struct ukey_op *op,
2159 const struct dpif_flow *flow)
2160 {
2161 op->ukey = NULL;
2162 op->dop.type = DPIF_OP_FLOW_DEL;
2163 op->dop.u.flow_del.key = flow->key;
2164 op->dop.u.flow_del.key_len = flow->key_len;
2165 op->dop.u.flow_del.ufid = flow->ufid_present ? &flow->ufid : NULL;
2166 op->dop.u.flow_del.pmd_id = flow->pmd_id;
2167 op->dop.u.flow_del.stats = &op->stats;
2168 op->dop.u.flow_del.terse = udpif_use_ufid(udpif);
2169 }
2170
2171 static void
2172 delete_op_init(struct udpif *udpif, struct ukey_op *op, struct udpif_key *ukey)
2173 {
2174 op->ukey = ukey;
2175 op->dop.type = DPIF_OP_FLOW_DEL;
2176 op->dop.u.flow_del.key = ukey->key;
2177 op->dop.u.flow_del.key_len = ukey->key_len;
2178 op->dop.u.flow_del.ufid = ukey->ufid_present ? &ukey->ufid : NULL;
2179 op->dop.u.flow_del.pmd_id = ukey->pmd_id;
2180 op->dop.u.flow_del.stats = &op->stats;
2181 op->dop.u.flow_del.terse = udpif_use_ufid(udpif);
2182 }
2183
2184 static void
2185 put_op_init(struct ukey_op *op, struct udpif_key *ukey,
2186 enum dpif_flow_put_flags flags)
2187 {
2188 op->ukey = ukey;
2189 op->dop.type = DPIF_OP_FLOW_PUT;
2190 op->dop.u.flow_put.flags = flags;
2191 op->dop.u.flow_put.key = ukey->key;
2192 op->dop.u.flow_put.key_len = ukey->key_len;
2193 op->dop.u.flow_put.mask = ukey->mask;
2194 op->dop.u.flow_put.mask_len = ukey->mask_len;
2195 op->dop.u.flow_put.ufid = ukey->ufid_present ? &ukey->ufid : NULL;
2196 op->dop.u.flow_put.pmd_id = ukey->pmd_id;
2197 op->dop.u.flow_put.stats = NULL;
2198 ukey_get_actions(ukey, &op->dop.u.flow_put.actions,
2199 &op->dop.u.flow_put.actions_len);
2200 }
2201
2202 /* Executes datapath operations 'ops' and attributes stats retrieved from the
2203 * datapath as part of those operations. */
2204 static void
2205 push_dp_ops(struct udpif *udpif, struct ukey_op *ops, size_t n_ops)
2206 {
2207 struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
2208 size_t i;
2209
2210 ovs_assert(n_ops <= REVALIDATE_MAX_BATCH);
2211 for (i = 0; i < n_ops; i++) {
2212 opsp[i] = &ops[i].dop;
2213 }
2214 dpif_operate(udpif->dpif, opsp, n_ops);
2215
2216 for (i = 0; i < n_ops; i++) {
2217 struct ukey_op *op = &ops[i];
2218 struct dpif_flow_stats *push, *stats, push_buf;
2219
2220 stats = op->dop.u.flow_del.stats;
2221 push = &push_buf;
2222
2223 if (op->dop.type != DPIF_OP_FLOW_DEL) {
2224 /* Only deleted flows need their stats pushed. */
2225 continue;
2226 }
2227
2228 if (op->dop.error) {
2229 /* flow_del error, 'stats' is unusable. */
2230 if (op->ukey) {
2231 ovs_mutex_lock(&op->ukey->mutex);
2232 transition_ukey(op->ukey, UKEY_EVICTED);
2233 ovs_mutex_unlock(&op->ukey->mutex);
2234 }
2235 continue;
2236 }
2237
2238 if (op->ukey) {
2239 ovs_mutex_lock(&op->ukey->mutex);
2240 transition_ukey(op->ukey, UKEY_EVICTED);
2241 push->used = MAX(stats->used, op->ukey->stats.used);
2242 push->tcp_flags = stats->tcp_flags | op->ukey->stats.tcp_flags;
2243 push->n_packets = stats->n_packets - op->ukey->stats.n_packets;
2244 push->n_bytes = stats->n_bytes - op->ukey->stats.n_bytes;
2245 ovs_mutex_unlock(&op->ukey->mutex);
2246 } else {
2247 push = stats;
2248 }
2249
2250 if (push->n_packets || netflow_exists()) {
2251 const struct nlattr *key = op->dop.u.flow_del.key;
2252 size_t key_len = op->dop.u.flow_del.key_len;
2253 struct netflow *netflow;
2254 struct reval_context ctx = {
2255 .netflow = &netflow,
2256 };
2257 int error;
2258
2259 if (op->ukey) {
2260 ovs_mutex_lock(&op->ukey->mutex);
2261 if (op->ukey->xcache) {
2262 xlate_push_stats(op->ukey->xcache, push);
2263 ovs_mutex_unlock(&op->ukey->mutex);
2264 continue;
2265 }
2266 ovs_mutex_unlock(&op->ukey->mutex);
2267 key = op->ukey->key;
2268 key_len = op->ukey->key_len;
2269 }
2270
2271 error = xlate_key(udpif, key, key_len, push, &ctx);
2272 if (error) {
2273 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2274
2275 VLOG_WARN_RL(&rl, "xlate_key failed (%s)!",
2276 ovs_strerror(error));
2277 } else {
2278 xlate_out_uninit(&ctx.xout);
2279 if (netflow) {
2280 netflow_flow_clear(netflow, &ctx.flow);
2281 }
2282 }
2283 }
2284 }
2285 }
2286
2287 /* Executes datapath operations 'ops', attributes stats retrieved from the
2288 * datapath, and deletes ukeys corresponding to deleted flows. */
2289 static void
2290 push_ukey_ops(struct udpif *udpif, struct umap *umap,
2291 struct ukey_op *ops, size_t n_ops)
2292 {
2293 int i;
2294
2295 push_dp_ops(udpif, ops, n_ops);
2296 ovs_mutex_lock(&umap->mutex);
2297 for (i = 0; i < n_ops; i++) {
2298 if (ops[i].dop.type == DPIF_OP_FLOW_DEL) {
2299 ukey_delete(umap, ops[i].ukey);
2300 }
2301 }
2302 ovs_mutex_unlock(&umap->mutex);
2303 }
2304
2305 static void
2306 log_unexpected_flow(const struct dpif_flow *flow, int error)
2307 {
2308 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 60);
2309 struct ds ds = DS_EMPTY_INITIALIZER;
2310
2311 ds_put_format(&ds, "Failed to acquire udpif_key corresponding to "
2312 "unexpected flow (%s): ", ovs_strerror(error));
2313 odp_format_ufid(&flow->ufid, &ds);
2314 VLOG_WARN_RL(&rl, "%s", ds_cstr(&ds));
2315 ds_destroy(&ds);
2316 }
2317
2318 static void
2319 reval_op_init(struct ukey_op *op, enum reval_result result,
2320 struct udpif *udpif, struct udpif_key *ukey,
2321 struct recirc_refs *recircs, struct ofpbuf *odp_actions)
2322 OVS_REQUIRES(ukey->mutex)
2323 {
2324 if (result == UKEY_DELETE) {
2325 delete_op_init(udpif, op, ukey);
2326 transition_ukey(ukey, UKEY_EVICTING);
2327 } else if (result == UKEY_MODIFY) {
2328 /* Store the new recircs. */
2329 recirc_refs_swap(&ukey->recircs, recircs);
2330 /* Release old recircs. */
2331 recirc_refs_unref(recircs);
2332 /* ukey->key_recirc_id remains, as the key is the same as before. */
2333
2334 ukey_set_actions(ukey, odp_actions);
2335 put_op_init(op, ukey, DPIF_FP_MODIFY);
2336 }
2337 }
2338
2339 static void
2340 revalidate(struct revalidator *revalidator)
2341 {
2342 uint64_t odp_actions_stub[1024 / 8];
2343 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
2344
2345 struct udpif *udpif = revalidator->udpif;
2346 struct dpif_flow_dump_thread *dump_thread;
2347 uint64_t dump_seq, reval_seq;
2348 unsigned int flow_limit;
2349
2350 dump_seq = seq_read(udpif->dump_seq);
2351 reval_seq = seq_read(udpif->reval_seq);
2352 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
2353 dump_thread = dpif_flow_dump_thread_create(udpif->dump);
2354 for (;;) {
2355 struct ukey_op ops[REVALIDATE_MAX_BATCH];
2356 int n_ops = 0;
2357
2358 struct dpif_flow flows[REVALIDATE_MAX_BATCH];
2359 const struct dpif_flow *f;
2360 int n_dumped;
2361
2362 long long int max_idle;
2363 long long int now;
2364 size_t n_dp_flows;
2365 bool kill_them_all;
2366
2367 n_dumped = dpif_flow_dump_next(dump_thread, flows, ARRAY_SIZE(flows));
2368 if (!n_dumped) {
2369 break;
2370 }
2371
2372 now = time_msec();
2373
2374 /* In normal operation we want to keep flows around until they have
2375 * been idle for 'ofproto_max_idle' milliseconds. However:
2376 *
2377 * - If the number of datapath flows climbs above 'flow_limit',
2378 * drop that down to 100 ms to try to bring the flows down to
2379 * the limit.
2380 *
2381 * - If the number of datapath flows climbs above twice
2382 * 'flow_limit', delete all the datapath flows as an emergency
2383 * measure. (We reassess this condition for the next batch of
2384 * datapath flows, so we will recover before all the flows are
2385 * gone.) */
2386 n_dp_flows = udpif_get_n_flows(udpif);
2387 kill_them_all = n_dp_flows > flow_limit * 2;
2388 max_idle = n_dp_flows > flow_limit ? 100 : ofproto_max_idle;
2389
2390 for (f = flows; f < &flows[n_dumped]; f++) {
2391 long long int used = f->stats.used;
2392 struct recirc_refs recircs = RECIRC_REFS_EMPTY_INITIALIZER;
2393 enum reval_result result;
2394 struct udpif_key *ukey;
2395 bool already_dumped;
2396 int error;
2397
2398 if (ukey_acquire(udpif, f, &ukey, &error)) {
2399 if (error == EBUSY) {
2400 /* Another thread is processing this flow, so don't bother
2401 * processing it.*/
2402 COVERAGE_INC(upcall_ukey_contention);
2403 } else {
2404 log_unexpected_flow(f, error);
2405 if (error != ENOENT) {
2406 delete_op_init__(udpif, &ops[n_ops++], f);
2407 }
2408 }
2409 continue;
2410 }
2411
2412 already_dumped = ukey->dump_seq == dump_seq;
2413 if (already_dumped) {
2414 /* The flow has already been handled during this flow dump
2415 * operation. Skip it. */
2416 if (ukey->xcache) {
2417 COVERAGE_INC(dumped_duplicate_flow);
2418 } else {
2419 COVERAGE_INC(dumped_new_flow);
2420 }
2421 ovs_mutex_unlock(&ukey->mutex);
2422 continue;
2423 }
2424
2425 if (ukey->state <= UKEY_OPERATIONAL) {
2426 /* The flow is now confirmed to be in the datapath. */
2427 transition_ukey(ukey, UKEY_OPERATIONAL);
2428 } else {
2429 VLOG_INFO("Unexpected ukey transition from state %d "
2430 "(last transitioned from thread %u at %s)",
2431 ukey->state, ukey->state_thread, ukey->state_where);
2432 ovs_mutex_unlock(&ukey->mutex);
2433 continue;
2434 }
2435
2436 if (!used) {
2437 used = ukey->created;
2438 }
2439 if (kill_them_all || (used && used < now - max_idle)) {
2440 result = UKEY_DELETE;
2441 } else {
2442 result = revalidate_ukey(udpif, ukey, &f->stats, &odp_actions,
2443 reval_seq, &recircs);
2444 }
2445 ukey->dump_seq = dump_seq;
2446
2447 if (result != UKEY_KEEP) {
2448 /* Takes ownership of 'recircs'. */
2449 reval_op_init(&ops[n_ops++], result, udpif, ukey, &recircs,
2450 &odp_actions);
2451 }
2452 ovs_mutex_unlock(&ukey->mutex);
2453 }
2454
2455 if (n_ops) {
2456 /* Push datapath ops but defer ukey deletion to 'sweep' phase. */
2457 push_dp_ops(udpif, ops, n_ops);
2458 }
2459 ovsrcu_quiesce();
2460 }
2461 dpif_flow_dump_thread_destroy(dump_thread);
2462 ofpbuf_uninit(&odp_actions);
2463 }
2464
2465 /* Pauses the 'revalidator', can only proceed after main thread
2466 * calls udpif_resume_revalidators(). */
2467 static void
2468 revalidator_pause(struct revalidator *revalidator)
2469 {
2470 /* The first block is for sync'ing the pause with main thread. */
2471 ovs_barrier_block(&revalidator->udpif->pause_barrier);
2472 /* The second block is for pausing until main thread resumes. */
2473 ovs_barrier_block(&revalidator->udpif->pause_barrier);
2474 }
2475
2476 static void
2477 revalidator_sweep__(struct revalidator *revalidator, bool purge)
2478 {
2479 struct udpif *udpif;
2480 uint64_t dump_seq, reval_seq;
2481 int slice;
2482
2483 udpif = revalidator->udpif;
2484 dump_seq = seq_read(udpif->dump_seq);
2485 reval_seq = seq_read(udpif->reval_seq);
2486 slice = revalidator - udpif->revalidators;
2487 ovs_assert(slice < udpif->n_revalidators);
2488
2489 for (int i = slice; i < N_UMAPS; i += udpif->n_revalidators) {
2490 uint64_t odp_actions_stub[1024 / 8];
2491 struct ofpbuf odp_actions = OFPBUF_STUB_INITIALIZER(odp_actions_stub);
2492
2493 struct ukey_op ops[REVALIDATE_MAX_BATCH];
2494 struct udpif_key *ukey;
2495 struct umap *umap = &udpif->ukeys[i];
2496 size_t n_ops = 0;
2497
2498 CMAP_FOR_EACH(ukey, cmap_node, &umap->cmap) {
2499 enum ukey_state ukey_state;
2500
2501 /* Handler threads could be holding a ukey lock while it installs a
2502 * new flow, so don't hang around waiting for access to it. */
2503 if (ovs_mutex_trylock(&ukey->mutex)) {
2504 continue;
2505 }
2506 ukey_state = ukey->state;
2507 if (ukey_state == UKEY_OPERATIONAL
2508 || (ukey_state == UKEY_VISIBLE && purge)) {
2509 struct recirc_refs recircs = RECIRC_REFS_EMPTY_INITIALIZER;
2510 bool seq_mismatch = (ukey->dump_seq != dump_seq
2511 && ukey->reval_seq != reval_seq);
2512 enum reval_result result;
2513
2514 if (purge) {
2515 result = UKEY_DELETE;
2516 } else if (!seq_mismatch) {
2517 result = UKEY_KEEP;
2518 } else {
2519 struct dpif_flow_stats stats;
2520 COVERAGE_INC(revalidate_missed_dp_flow);
2521 memset(&stats, 0, sizeof stats);
2522 result = revalidate_ukey(udpif, ukey, &stats, &odp_actions,
2523 reval_seq, &recircs);
2524 }
2525 if (result != UKEY_KEEP) {
2526 /* Clears 'recircs' if filled by revalidate_ukey(). */
2527 reval_op_init(&ops[n_ops++], result, udpif, ukey, &recircs,
2528 &odp_actions);
2529 }
2530 }
2531 ovs_mutex_unlock(&ukey->mutex);
2532
2533 if (ukey_state == UKEY_EVICTED) {
2534 /* The common flow deletion case involves deletion of the flow
2535 * during the dump phase and ukey deletion here. */
2536 ovs_mutex_lock(&umap->mutex);
2537 ukey_delete(umap, ukey);
2538 ovs_mutex_unlock(&umap->mutex);
2539 }
2540
2541 if (n_ops == REVALIDATE_MAX_BATCH) {
2542 /* Update/delete missed flows and clean up corresponding ukeys
2543 * if necessary. */
2544 push_ukey_ops(udpif, umap, ops, n_ops);
2545 n_ops = 0;
2546 }
2547 }
2548
2549 if (n_ops) {
2550 push_ukey_ops(udpif, umap, ops, n_ops);
2551 }
2552
2553 ofpbuf_uninit(&odp_actions);
2554 ovsrcu_quiesce();
2555 }
2556 }
2557
2558 static void
2559 revalidator_sweep(struct revalidator *revalidator)
2560 {
2561 revalidator_sweep__(revalidator, false);
2562 }
2563
2564 static void
2565 revalidator_purge(struct revalidator *revalidator)
2566 {
2567 revalidator_sweep__(revalidator, true);
2568 }
2569
2570 /* In reaction to dpif purge, purges all 'ukey's with same 'pmd_id'. */
2571 static void
2572 dp_purge_cb(void *aux, unsigned pmd_id)
2573 OVS_NO_THREAD_SAFETY_ANALYSIS
2574 {
2575 struct udpif *udpif = aux;
2576 size_t i;
2577
2578 udpif_pause_revalidators(udpif);
2579 for (i = 0; i < N_UMAPS; i++) {
2580 struct ukey_op ops[REVALIDATE_MAX_BATCH];
2581 struct udpif_key *ukey;
2582 struct umap *umap = &udpif->ukeys[i];
2583 size_t n_ops = 0;
2584
2585 CMAP_FOR_EACH(ukey, cmap_node, &umap->cmap) {
2586 if (ukey->pmd_id == pmd_id) {
2587 delete_op_init(udpif, &ops[n_ops++], ukey);
2588 transition_ukey(ukey, UKEY_EVICTING);
2589
2590 if (n_ops == REVALIDATE_MAX_BATCH) {
2591 push_ukey_ops(udpif, umap, ops, n_ops);
2592 n_ops = 0;
2593 }
2594 }
2595 }
2596
2597 if (n_ops) {
2598 push_ukey_ops(udpif, umap, ops, n_ops);
2599 }
2600
2601 ovsrcu_quiesce();
2602 }
2603 udpif_resume_revalidators(udpif);
2604 }
2605 \f
2606 static void
2607 upcall_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
2608 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2609 {
2610 struct ds ds = DS_EMPTY_INITIALIZER;
2611 struct udpif *udpif;
2612
2613 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
2614 unsigned int flow_limit;
2615 bool ufid_enabled;
2616 size_t i;
2617
2618 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
2619 ufid_enabled = udpif_use_ufid(udpif);
2620
2621 ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
2622 ds_put_format(&ds, "\tflows : (current %lu)"
2623 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
2624 udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
2625 ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
2626 ds_put_format(&ds, "\tufid enabled : ");
2627 if (ufid_enabled) {
2628 ds_put_format(&ds, "true\n");
2629 } else {
2630 ds_put_format(&ds, "false\n");
2631 }
2632 ds_put_char(&ds, '\n');
2633
2634 for (i = 0; i < n_revalidators; i++) {
2635 struct revalidator *revalidator = &udpif->revalidators[i];
2636 int j, elements = 0;
2637
2638 for (j = i; j < N_UMAPS; j += n_revalidators) {
2639 elements += cmap_count(&udpif->ukeys[j].cmap);
2640 }
2641 ds_put_format(&ds, "\t%u: (keys %d)\n", revalidator->id, elements);
2642 }
2643 }
2644
2645 unixctl_command_reply(conn, ds_cstr(&ds));
2646 ds_destroy(&ds);
2647 }
2648
2649 /* Disable using the megaflows.
2650 *
2651 * This command is only needed for advanced debugging, so it's not
2652 * documented in the man page. */
2653 static void
2654 upcall_unixctl_disable_megaflows(struct unixctl_conn *conn,
2655 int argc OVS_UNUSED,
2656 const char *argv[] OVS_UNUSED,
2657 void *aux OVS_UNUSED)
2658 {
2659 atomic_store_relaxed(&enable_megaflows, false);
2660 udpif_flush_all_datapaths();
2661 unixctl_command_reply(conn, "megaflows disabled");
2662 }
2663
2664 /* Re-enable using megaflows.
2665 *
2666 * This command is only needed for advanced debugging, so it's not
2667 * documented in the man page. */
2668 static void
2669 upcall_unixctl_enable_megaflows(struct unixctl_conn *conn,
2670 int argc OVS_UNUSED,
2671 const char *argv[] OVS_UNUSED,
2672 void *aux OVS_UNUSED)
2673 {
2674 atomic_store_relaxed(&enable_megaflows, true);
2675 udpif_flush_all_datapaths();
2676 unixctl_command_reply(conn, "megaflows enabled");
2677 }
2678
2679 /* Disable skipping flow attributes during flow dump.
2680 *
2681 * This command is only needed for advanced debugging, so it's not
2682 * documented in the man page. */
2683 static void
2684 upcall_unixctl_disable_ufid(struct unixctl_conn *conn, int argc OVS_UNUSED,
2685 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2686 {
2687 atomic_store_relaxed(&enable_ufid, false);
2688 unixctl_command_reply(conn, "Datapath dumping tersely using UFID disabled");
2689 }
2690
2691 /* Re-enable skipping flow attributes during flow dump.
2692 *
2693 * This command is only needed for advanced debugging, so it's not documented
2694 * in the man page. */
2695 static void
2696 upcall_unixctl_enable_ufid(struct unixctl_conn *conn, int argc OVS_UNUSED,
2697 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2698 {
2699 atomic_store_relaxed(&enable_ufid, true);
2700 unixctl_command_reply(conn, "Datapath dumping tersely using UFID enabled "
2701 "for supported datapaths");
2702 }
2703
2704 /* Set the flow limit.
2705 *
2706 * This command is only needed for advanced debugging, so it's not
2707 * documented in the man page. */
2708 static void
2709 upcall_unixctl_set_flow_limit(struct unixctl_conn *conn,
2710 int argc OVS_UNUSED,
2711 const char *argv[],
2712 void *aux OVS_UNUSED)
2713 {
2714 struct ds ds = DS_EMPTY_INITIALIZER;
2715 struct udpif *udpif;
2716 unsigned int flow_limit = atoi(argv[1]);
2717
2718 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
2719 atomic_store_relaxed(&udpif->flow_limit, flow_limit);
2720 }
2721 ds_put_format(&ds, "set flow_limit to %u\n", flow_limit);
2722 unixctl_command_reply(conn, ds_cstr(&ds));
2723 ds_destroy(&ds);
2724 }
2725
2726 static void
2727 upcall_unixctl_dump_wait(struct unixctl_conn *conn,
2728 int argc OVS_UNUSED,
2729 const char *argv[] OVS_UNUSED,
2730 void *aux OVS_UNUSED)
2731 {
2732 if (ovs_list_is_singleton(&all_udpifs)) {
2733 struct udpif *udpif = NULL;
2734 size_t len;
2735
2736 udpif = OBJECT_CONTAINING(ovs_list_front(&all_udpifs), udpif, list_node);
2737 len = (udpif->n_conns + 1) * sizeof *udpif->conns;
2738 udpif->conn_seq = seq_read(udpif->dump_seq);
2739 udpif->conns = xrealloc(udpif->conns, len);
2740 udpif->conns[udpif->n_conns++] = conn;
2741 } else {
2742 unixctl_command_reply_error(conn, "can't wait on multiple udpifs.");
2743 }
2744 }
2745
2746 static void
2747 upcall_unixctl_purge(struct unixctl_conn *conn, int argc OVS_UNUSED,
2748 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
2749 {
2750 struct udpif *udpif;
2751
2752 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
2753 int n;
2754
2755 for (n = 0; n < udpif->n_revalidators; n++) {
2756 revalidator_purge(&udpif->revalidators[n]);
2757 }
2758 }
2759 unixctl_command_reply(conn, "");
2760 }