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