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