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