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