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