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