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