]> git.proxmox.com Git - mirror_ovs.git/blame - ofproto/ofproto-dpif-upcall.c
openvswitch.spec: Remove dependency with openvswitch-kmod.
[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"
e1ec7dd4 24#include "dpif.h"
e22d52ee 25#include "dynamic-string.h"
e1ec7dd4 26#include "fail-open.h"
05067881 27#include "guarded-list.h"
e1ec7dd4 28#include "latch.h"
e1ec7dd4
EJ
29#include "list.h"
30#include "netlink.h"
31#include "ofpbuf.h"
10e57640
EJ
32#include "ofproto-dpif-ipfix.h"
33#include "ofproto-dpif-sflow.h"
e79a6c83 34#include "ofproto-dpif-xlate.h"
0f2ea848 35#include "ovs-rcu.h"
e1ec7dd4
EJ
36#include "packets.h"
37#include "poll-loop.h"
e22d52ee
EJ
38#include "seq.h"
39#include "unixctl.h"
e1ec7dd4
EJ
40#include "vlog.h"
41
42#define MAX_QUEUE_LENGTH 512
6b31e073 43#define UPCALL_MAX_BATCH 64
e79a6c83 44#define REVALIDATE_MAX_BATCH 50
e1ec7dd4
EJ
45
46VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall);
47
ec47af51
JS
48COVERAGE_DEFINE(dumped_duplicate_flow);
49COVERAGE_DEFINE(dumped_new_flow);
3b62a9d3 50COVERAGE_DEFINE(revalidate_missed_dp_flow);
73a3c475 51
9a159f74
AW
52/* A thread that reads upcalls from dpif, forwards each upcall's packet,
53 * and possibly sets up a kernel flow as a cache. */
e1ec7dd4
EJ
54struct handler {
55 struct udpif *udpif; /* Parent udpif. */
56 pthread_t thread; /* Thread ID. */
9a159f74 57 uint32_t handler_id; /* Handler id. */
e1ec7dd4
EJ
58};
59
7d170098
EJ
60/* A thread that processes datapath flows, updates OpenFlow statistics, and
61 * updates or removes them if necessary. */
e79a6c83
EJ
62struct revalidator {
63 struct udpif *udpif; /* Parent udpif. */
e79a6c83 64 pthread_t thread; /* Thread ID. */
8ba0a522 65 unsigned int id; /* ovsthread_id_self(). */
7d170098
EJ
66 struct hmap *ukeys; /* Points into udpif->ukeys for this
67 revalidator. Used for GC phase. */
e79a6c83
EJ
68};
69
e1ec7dd4
EJ
70/* An upcall handler for ofproto_dpif.
71 *
9a159f74
AW
72 * udpif keeps records of two kind of logically separate units:
73 *
74 * upcall handling
75 * ---------------
76 *
77 * - An array of 'struct handler's for upcall handling and flow
78 * installation.
e79a6c83 79 *
9a159f74
AW
80 * flow revalidation
81 * -----------------
82 *
7d170098
EJ
83 * - Revalidation threads which read the datapath flow table and maintains
84 * them.
85 */
e1ec7dd4 86struct udpif {
e22d52ee
EJ
87 struct list list_node; /* In all_udpifs list. */
88
e1ec7dd4
EJ
89 struct dpif *dpif; /* Datapath handle. */
90 struct dpif_backer *backer; /* Opaque dpif_backer pointer. */
91
92 uint32_t secret; /* Random seed for upcall hash. */
93
10e57640 94 struct handler *handlers; /* Upcall handlers. */
e1ec7dd4
EJ
95 size_t n_handlers;
96
e79a6c83
EJ
97 struct revalidator *revalidators; /* Flow revalidators. */
98 size_t n_revalidators;
99
e79a6c83
EJ
100 struct latch exit_latch; /* Tells child threads to exit. */
101
7d170098
EJ
102 /* Revalidation. */
103 struct seq *reval_seq; /* Incremented to force revalidation. */
104 bool need_revalidate; /* As indicated by 'reval_seq'. */
105 bool reval_exit; /* Set by leader on 'exit_latch. */
d8043da7 106 struct ovs_barrier reval_barrier; /* Barrier used by revalidators. */
ac64794a 107 struct dpif_flow_dump *dump; /* DPIF flow dump state. */
e79a6c83 108 long long int dump_duration; /* Duration of the last flow dump. */
7d170098
EJ
109 struct seq *dump_seq; /* Increments each dump iteration. */
110
111 /* There are 'n_revalidators' ukey hmaps. Each revalidator retains a
112 * reference to one of these for garbage collection.
113 *
114 * During the flow dump phase, revalidators insert into these with a random
115 * distribution. During the garbage collection phase, each revalidator
116 * takes care of garbage collecting one of these hmaps. */
117 struct {
118 struct ovs_mutex mutex; /* Guards the following. */
119 struct hmap hmap OVS_GUARDED; /* Datapath flow keys. */
120 } *ukeys;
e1ec7dd4 121
e79a6c83
EJ
122 /* Datapath flow statistics. */
123 unsigned int max_n_flows;
124 unsigned int avg_n_flows;
e1ec7dd4 125
e79a6c83 126 /* Following fields are accessed and modified by different threads. */
e79a6c83 127 atomic_uint flow_limit; /* Datapath flow hard limit. */
64ca9472
JS
128
129 /* n_flows_mutex prevents multiple threads updating these concurrently. */
b482e960 130 atomic_uint n_flows; /* Number of flows in the datapath. */
64ca9472
JS
131 atomic_llong n_flows_timestamp; /* Last time n_flows was updated. */
132 struct ovs_mutex n_flows_mutex;
27f57736
JS
133
134 /* Following fields are accessed and modified only from the main thread. */
135 struct unixctl_conn **conns; /* Connections waiting on dump_seq. */
136 uint64_t conn_seq; /* Corresponds to 'dump_seq' when
137 conns[n_conns-1] was stored. */
138 size_t n_conns; /* Number of connections waiting. */
e1ec7dd4
EJ
139};
140
10e57640
EJ
141enum upcall_type {
142 BAD_UPCALL, /* Some kind of bug somewhere. */
143 MISS_UPCALL, /* A flow miss. */
144 SFLOW_UPCALL, /* sFlow sample. */
145 FLOW_SAMPLE_UPCALL, /* Per-flow sampling. */
146 IPFIX_UPCALL /* Per-bridge sampling. */
147};
148
149struct upcall {
cc377352 150 struct ofproto_dpif *ofproto; /* Parent ofproto. */
a0bab870 151
cc377352
EJ
152 /* The flow and packet are only required to be constant when using
153 * dpif-netdev. If a modification is absolutely necessary, a const cast
154 * may be used with other datapaths. */
155 const struct flow *flow; /* Parsed representation of the packet. */
156 const struct ofpbuf *packet; /* Packet associated with this upcall. */
157 ofp_port_t in_port; /* OpenFlow in port, or OFPP_NONE. */
a0bab870 158
cc377352
EJ
159 enum dpif_upcall_type type; /* Datapath type of the upcall. */
160 const struct nlattr *userdata; /* Userdata for DPIF_UC_ACTION Upcalls. */
161
162 bool xout_initialized; /* True if 'xout' must be uninitialized. */
163 struct xlate_out xout; /* Result of xlate_actions(). */
164 struct ofpbuf put_actions; /* Actions 'put' in the fastapath. */
165
dcc2c6cd
JR
166 struct dpif_ipfix *ipfix; /* IPFIX pointer or NULL. */
167 struct dpif_sflow *sflow; /* SFlow pointer or NULL. */
a0bab870 168
cc377352
EJ
169 bool vsp_adjusted; /* 'packet' and 'flow' were adjusted for
170 VLAN splinters if true. */
10e57640 171
cc377352
EJ
172 /* Not used by the upcall callback interface. */
173 const struct nlattr *key; /* Datapath flow key. */
174 size_t key_len; /* Datapath flow key length. */
8b7ea2d4 175 const struct nlattr *out_tun_key; /* Datapath output tunnel key. */
10e57640
EJ
176};
177
e79a6c83
EJ
178/* 'udpif_key's are responsible for tracking the little bit of state udpif
179 * needs to do flow expiration which can't be pulled directly from the
7d170098
EJ
180 * datapath. They may be created or maintained by any revalidator during
181 * the dump phase, but are owned by a single revalidator, and are destroyed
182 * by that revalidator during the garbage-collection phase.
183 *
184 * While some elements of a udpif_key are protected by a mutex, the ukey itself
185 * is not. Therefore it is not safe to destroy a udpif_key except when all
186 * revalidators are in garbage collection phase, or they aren't running. */
e79a6c83
EJ
187struct udpif_key {
188 struct hmap_node hmap_node; /* In parent revalidator 'ukeys' map. */
189
7d170098
EJ
190 /* These elements are read only once created, and therefore aren't
191 * protected by a mutex. */
192 const struct nlattr *key; /* Datapath flow key. */
e79a6c83 193 size_t key_len; /* Length of 'key'. */
e79a6c83 194
7d170098
EJ
195 struct ovs_mutex mutex; /* Guards the following. */
196 struct dpif_flow_stats stats OVS_GUARDED; /* Last known stats.*/
197 long long int created OVS_GUARDED; /* Estimate of creation time. */
efa08531 198 uint64_t dump_seq OVS_GUARDED; /* Tracks udpif->dump_seq. */
7d170098
EJ
199 bool flow_exists OVS_GUARDED; /* Ensures flows are only deleted
200 once. */
201
202 struct xlate_cache *xcache OVS_GUARDED; /* Cache for xlate entries that
203 * are affected by this ukey.
204 * Used for stats and learning.*/
02334943
JR
205 union {
206 struct odputil_keybuf key_buf; /* Memory for 'key'. */
207 struct nlattr key_buf_nla;
208 };
e79a6c83
EJ
209};
210
e1ec7dd4 211static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
e22d52ee 212static struct list all_udpifs = LIST_INITIALIZER(&all_udpifs);
e1ec7dd4 213
cc377352
EJ
214static size_t recv_upcalls(struct handler *);
215static int process_upcall(struct udpif *, struct upcall *,
216 struct ofpbuf *odp_actions);
6b31e073 217static void handle_upcalls(struct udpif *, struct upcall *, size_t n_upcalls);
1f867548
AW
218static void udpif_stop_threads(struct udpif *);
219static void udpif_start_threads(struct udpif *, size_t n_handlers,
220 size_t n_revalidators);
10e57640 221static void *udpif_upcall_handler(void *);
e79a6c83 222static void *udpif_revalidator(void *);
0e2a9f6f 223static unsigned long udpif_get_n_flows(struct udpif *);
7d170098 224static void revalidate(struct revalidator *);
e79a6c83 225static void revalidator_sweep(struct revalidator *);
e96a5c24 226static void revalidator_purge(struct revalidator *);
e22d52ee
EJ
227static void upcall_unixctl_show(struct unixctl_conn *conn, int argc,
228 const char *argv[], void *aux);
e79a6c83
EJ
229static void upcall_unixctl_disable_megaflows(struct unixctl_conn *, int argc,
230 const char *argv[], void *aux);
231static void upcall_unixctl_enable_megaflows(struct unixctl_conn *, int argc,
232 const char *argv[], void *aux);
94b8c324
JS
233static void upcall_unixctl_set_flow_limit(struct unixctl_conn *conn, int argc,
234 const char *argv[], void *aux);
27f57736
JS
235static void upcall_unixctl_dump_wait(struct unixctl_conn *conn, int argc,
236 const char *argv[], void *aux);
7d170098
EJ
237
238static struct udpif_key *ukey_create(const struct nlattr *key, size_t key_len,
239 long long int used);
feca8bd7
JS
240static struct udpif_key *ukey_lookup(struct udpif *udpif,
241 const struct nlattr *key, size_t key_len,
242 uint32_t hash);
243static bool ukey_acquire(struct udpif *udpif, const struct nlattr *key,
244 size_t key_len, long long int used,
245 struct udpif_key **result);
e79a6c83 246static void ukey_delete(struct revalidator *, struct udpif_key *);
cc377352
EJ
247static enum upcall_type classify_upcall(enum dpif_upcall_type type,
248 const struct nlattr *userdata);
249
cc377352
EJ
250static int upcall_receive(struct upcall *, const struct dpif_backer *,
251 const struct ofpbuf *packet, enum dpif_upcall_type,
252 const struct nlattr *userdata, const struct flow *);
253static void upcall_uninit(struct upcall *);
e79a6c83 254
623540e4
EJ
255static upcall_callback upcall_cb;
256
e79a6c83 257static atomic_bool enable_megaflows = ATOMIC_VAR_INIT(true);
e1ec7dd4
EJ
258
259struct udpif *
260udpif_create(struct dpif_backer *backer, struct dpif *dpif)
261{
e22d52ee 262 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
e1ec7dd4
EJ
263 struct udpif *udpif = xzalloc(sizeof *udpif);
264
e22d52ee
EJ
265 if (ovsthread_once_start(&once)) {
266 unixctl_command_register("upcall/show", "", 0, 0, upcall_unixctl_show,
267 NULL);
e79a6c83
EJ
268 unixctl_command_register("upcall/disable-megaflows", "", 0, 0,
269 upcall_unixctl_disable_megaflows, NULL);
270 unixctl_command_register("upcall/enable-megaflows", "", 0, 0,
271 upcall_unixctl_enable_megaflows, NULL);
94b8c324
JS
272 unixctl_command_register("upcall/set-flow-limit", "", 1, 1,
273 upcall_unixctl_set_flow_limit, NULL);
27f57736
JS
274 unixctl_command_register("revalidator/wait", "", 0, 0,
275 upcall_unixctl_dump_wait, NULL);
e22d52ee
EJ
276 ovsthread_once_done(&once);
277 }
278
e1ec7dd4
EJ
279 udpif->dpif = dpif;
280 udpif->backer = backer;
e79a6c83 281 atomic_init(&udpif->flow_limit, MIN(ofproto_flow_limit, 10000));
e1ec7dd4 282 udpif->secret = random_uint32();
d7285d74 283 udpif->reval_seq = seq_create();
e79a6c83 284 udpif->dump_seq = seq_create();
e1ec7dd4 285 latch_init(&udpif->exit_latch);
e22d52ee 286 list_push_back(&all_udpifs, &udpif->list_node);
64ca9472
JS
287 atomic_init(&udpif->n_flows, 0);
288 atomic_init(&udpif->n_flows_timestamp, LLONG_MIN);
289 ovs_mutex_init(&udpif->n_flows_mutex);
e1ec7dd4 290
623540e4 291 dpif_register_upcall_cb(dpif, upcall_cb, udpif);
6b31e073 292
e1ec7dd4
EJ
293 return udpif;
294}
295
27f57736
JS
296void
297udpif_run(struct udpif *udpif)
298{
299 if (udpif->conns && udpif->conn_seq != seq_read(udpif->dump_seq)) {
300 int i;
301
302 for (i = 0; i < udpif->n_conns; i++) {
303 unixctl_command_reply(udpif->conns[i], NULL);
304 }
305 free(udpif->conns);
306 udpif->conns = NULL;
307 udpif->n_conns = 0;
308 }
309}
310
e1ec7dd4
EJ
311void
312udpif_destroy(struct udpif *udpif)
313{
1f867548 314 udpif_stop_threads(udpif);
e1ec7dd4 315
e22d52ee 316 list_remove(&udpif->list_node);
e1ec7dd4 317 latch_destroy(&udpif->exit_latch);
d7285d74 318 seq_destroy(udpif->reval_seq);
e79a6c83 319 seq_destroy(udpif->dump_seq);
64ca9472 320 ovs_mutex_destroy(&udpif->n_flows_mutex);
e1ec7dd4
EJ
321 free(udpif);
322}
323
1f867548
AW
324/* Stops the handler and revalidator threads, must be enclosed in
325 * ovsrcu quiescent state unless when destroying udpif. */
326static void
327udpif_stop_threads(struct udpif *udpif)
e1ec7dd4 328{
3aadc5bb 329 if (udpif && (udpif->n_handlers != 0 || udpif->n_revalidators != 0)) {
e1ec7dd4
EJ
330 size_t i;
331
332 latch_set(&udpif->exit_latch);
333
e1ec7dd4
EJ
334 for (i = 0; i < udpif->n_handlers; i++) {
335 struct handler *handler = &udpif->handlers[i];
336
e79a6c83
EJ
337 xpthread_join(handler->thread, NULL);
338 }
339
340 for (i = 0; i < udpif->n_revalidators; i++) {
7d170098 341 xpthread_join(udpif->revalidators[i].thread, NULL);
e1ec7dd4
EJ
342 }
343
6b31e073
RW
344 dpif_disable_upcall(udpif->dpif);
345
e79a6c83
EJ
346 for (i = 0; i < udpif->n_revalidators; i++) {
347 struct revalidator *revalidator = &udpif->revalidators[i];
e79a6c83 348
e96a5c24
JS
349 /* Delete ukeys, and delete all flows from the datapath to prevent
350 * double-counting stats. */
351 revalidator_purge(revalidator);
7d170098
EJ
352
353 hmap_destroy(&udpif->ukeys[i].hmap);
354 ovs_mutex_destroy(&udpif->ukeys[i].mutex);
e79a6c83
EJ
355 }
356
e1ec7dd4
EJ
357 latch_poll(&udpif->exit_latch);
358
d8043da7 359 ovs_barrier_destroy(&udpif->reval_barrier);
7d170098 360
e79a6c83
EJ
361 free(udpif->revalidators);
362 udpif->revalidators = NULL;
363 udpif->n_revalidators = 0;
364
e1ec7dd4
EJ
365 free(udpif->handlers);
366 udpif->handlers = NULL;
367 udpif->n_handlers = 0;
7d170098
EJ
368
369 free(udpif->ukeys);
370 udpif->ukeys = NULL;
e1ec7dd4 371 }
1f867548 372}
e1ec7dd4 373
1f867548
AW
374/* Starts the handler and revalidator threads, must be enclosed in
375 * ovsrcu quiescent state. */
376static void
377udpif_start_threads(struct udpif *udpif, size_t n_handlers,
378 size_t n_revalidators)
379{
6f12bda3 380 if (udpif && n_handlers && n_revalidators) {
e1ec7dd4
EJ
381 size_t i;
382
383 udpif->n_handlers = n_handlers;
e79a6c83
EJ
384 udpif->n_revalidators = n_revalidators;
385
e1ec7dd4
EJ
386 udpif->handlers = xzalloc(udpif->n_handlers * sizeof *udpif->handlers);
387 for (i = 0; i < udpif->n_handlers; i++) {
388 struct handler *handler = &udpif->handlers[i];
389
390 handler->udpif = udpif;
9a159f74 391 handler->handler_id = i;
8ba0a522
BP
392 handler->thread = ovs_thread_create(
393 "handler", udpif_upcall_handler, handler);
e1ec7dd4 394 }
e1ec7dd4 395
6b31e073
RW
396 dpif_enable_upcall(udpif->dpif);
397
d8043da7 398 ovs_barrier_init(&udpif->reval_barrier, udpif->n_revalidators);
7d170098 399 udpif->reval_exit = false;
e79a6c83
EJ
400 udpif->revalidators = xzalloc(udpif->n_revalidators
401 * sizeof *udpif->revalidators);
7d170098 402 udpif->ukeys = xmalloc(sizeof *udpif->ukeys * n_revalidators);
e79a6c83
EJ
403 for (i = 0; i < udpif->n_revalidators; i++) {
404 struct revalidator *revalidator = &udpif->revalidators[i];
405
406 revalidator->udpif = udpif;
7d170098
EJ
407 hmap_init(&udpif->ukeys[i].hmap);
408 ovs_mutex_init(&udpif->ukeys[i].mutex);
409 revalidator->ukeys = &udpif->ukeys[i].hmap;
8ba0a522
BP
410 revalidator->thread = ovs_thread_create(
411 "revalidator", udpif_revalidator, revalidator);
e79a6c83 412 }
e1ec7dd4 413 }
1f867548 414}
0f2ea848 415
1f867548
AW
416/* Tells 'udpif' how many threads it should use to handle upcalls.
417 * 'n_handlers' and 'n_revalidators' can never be zero. 'udpif''s
418 * datapath handle must have packet reception enabled before starting
419 * threads. */
420void
421udpif_set_threads(struct udpif *udpif, size_t n_handlers,
422 size_t n_revalidators)
423{
3aadc5bb 424 ovs_assert(udpif);
1f867548
AW
425 ovs_assert(n_handlers && n_revalidators);
426
427 ovsrcu_quiesce_start();
3aadc5bb
AW
428 if (udpif->n_handlers != n_handlers
429 || udpif->n_revalidators != n_revalidators) {
430 udpif_stop_threads(udpif);
431 }
1f867548 432
3aadc5bb 433 if (!udpif->handlers && !udpif->revalidators) {
380fffec
AW
434 int error;
435
436 error = dpif_handlers_set(udpif->dpif, n_handlers);
437 if (error) {
438 VLOG_ERR("failed to configure handlers in dpif %s: %s",
439 dpif_name(udpif->dpif), ovs_strerror(error));
440 return;
441 }
442
3aadc5bb
AW
443 udpif_start_threads(udpif, n_handlers, n_revalidators);
444 }
0f2ea848 445 ovsrcu_quiesce_end();
e1ec7dd4
EJ
446}
447
3f142f59
BP
448/* Waits for all ongoing upcall translations to complete. This ensures that
449 * there are no transient references to any removed ofprotos (or other
450 * objects). In particular, this should be called after an ofproto is removed
451 * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
452void
453udpif_synchronize(struct udpif *udpif)
454{
455 /* This is stronger than necessary. It would be sufficient to ensure
456 * (somehow) that each handler and revalidator thread had passed through
457 * its main loop once. */
458 size_t n_handlers = udpif->n_handlers;
459 size_t n_revalidators = udpif->n_revalidators;
1f867548
AW
460
461 ovsrcu_quiesce_start();
462 udpif_stop_threads(udpif);
463 udpif_start_threads(udpif, n_handlers, n_revalidators);
464 ovsrcu_quiesce_end();
3f142f59
BP
465}
466
e1ec7dd4
EJ
467/* Notifies 'udpif' that something changed which may render previous
468 * xlate_actions() results invalid. */
469void
470udpif_revalidate(struct udpif *udpif)
471{
d7285d74 472 seq_change(udpif->reval_seq);
e79a6c83 473}
05067881 474
e79a6c83
EJ
475/* Returns a seq which increments every time 'udpif' pulls stats from the
476 * datapath. Callers can use this to get a sense of when might be a good time
477 * to do periodic work which relies on relatively up to date statistics. */
478struct seq *
479udpif_dump_seq(struct udpif *udpif)
480{
481 return udpif->dump_seq;
e1ec7dd4
EJ
482}
483
1c030aa5
EJ
484void
485udpif_get_memory_usage(struct udpif *udpif, struct simap *usage)
486{
487 size_t i;
488
1c030aa5 489 simap_increase(usage, "handlers", udpif->n_handlers);
e79a6c83
EJ
490
491 simap_increase(usage, "revalidators", udpif->n_revalidators);
492 for (i = 0; i < udpif->n_revalidators; i++) {
7d170098
EJ
493 ovs_mutex_lock(&udpif->ukeys[i].mutex);
494 simap_increase(usage, "udpif keys", hmap_count(&udpif->ukeys[i].hmap));
495 ovs_mutex_unlock(&udpif->ukeys[i].mutex);
e79a6c83 496 }
1c030aa5
EJ
497}
498
1b5b5071 499/* Remove flows from a single datapath. */
e79a6c83 500void
1b5b5071
AZ
501udpif_flush(struct udpif *udpif)
502{
503 size_t n_handlers, n_revalidators;
504
505 n_handlers = udpif->n_handlers;
506 n_revalidators = udpif->n_revalidators;
507
1f867548
AW
508 ovsrcu_quiesce_start();
509
510 udpif_stop_threads(udpif);
1b5b5071 511 dpif_flow_flush(udpif->dpif);
1f867548
AW
512 udpif_start_threads(udpif, n_handlers, n_revalidators);
513
514 ovsrcu_quiesce_end();
1b5b5071
AZ
515}
516
517/* Removes all flows from all datapaths. */
518static void
519udpif_flush_all_datapaths(void)
e79a6c83
EJ
520{
521 struct udpif *udpif;
522
523 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
1b5b5071 524 udpif_flush(udpif);
e79a6c83
EJ
525 }
526}
1b5b5071 527
e79a6c83 528\f
0e2a9f6f 529static unsigned long
64ca9472 530udpif_get_n_flows(struct udpif *udpif)
e1ec7dd4 531{
64ca9472 532 long long int time, now;
0e2a9f6f 533 unsigned long flow_count;
64ca9472
JS
534
535 now = time_msec();
b482e960 536 atomic_read_relaxed(&udpif->n_flows_timestamp, &time);
64ca9472
JS
537 if (time < now - 100 && !ovs_mutex_trylock(&udpif->n_flows_mutex)) {
538 struct dpif_dp_stats stats;
539
b482e960 540 atomic_store_relaxed(&udpif->n_flows_timestamp, now);
64ca9472
JS
541 dpif_get_dp_stats(udpif->dpif, &stats);
542 flow_count = stats.n_flows;
b482e960 543 atomic_store_relaxed(&udpif->n_flows, flow_count);
64ca9472
JS
544 ovs_mutex_unlock(&udpif->n_flows_mutex);
545 } else {
b482e960 546 atomic_read_relaxed(&udpif->n_flows, &flow_count);
64ca9472
JS
547 }
548 return flow_count;
e79a6c83 549}
e1ec7dd4 550
a0bab870 551/* The upcall handler thread tries to read a batch of UPCALL_MAX_BATCH
9a159f74
AW
552 * upcalls from dpif, processes the batch and installs corresponding flows
553 * in dpif. */
e1ec7dd4 554static void *
10e57640 555udpif_upcall_handler(void *arg)
e1ec7dd4 556{
e1ec7dd4 557 struct handler *handler = arg;
9a159f74 558 struct udpif *udpif = handler->udpif;
e1ec7dd4 559
61057e88 560 while (!latch_is_set(&handler->udpif->exit_latch)) {
cc377352 561 if (!recv_upcalls(handler)) {
9a159f74
AW
562 dpif_recv_wait(udpif->dpif, handler->handler_id);
563 latch_wait(&udpif->exit_latch);
564 poll_block();
e1ec7dd4 565 }
de80e4b6 566 coverage_clear();
e1ec7dd4 567 }
61057e88
BP
568
569 return NULL;
e1ec7dd4 570}
e79a6c83 571
cc377352
EJ
572static size_t
573recv_upcalls(struct handler *handler)
574{
575 struct udpif *udpif = handler->udpif;
576 uint64_t recv_stubs[UPCALL_MAX_BATCH][512 / 8];
577 struct ofpbuf recv_bufs[UPCALL_MAX_BATCH];
a6f4ad08 578 struct dpif_upcall dupcalls[UPCALL_MAX_BATCH];
cc377352
EJ
579 struct upcall upcalls[UPCALL_MAX_BATCH];
580 size_t n_upcalls, i;
581
582 n_upcalls = 0;
583 while (n_upcalls < UPCALL_MAX_BATCH) {
584 struct ofpbuf *recv_buf = &recv_bufs[n_upcalls];
a6f4ad08 585 struct dpif_upcall *dupcall = &dupcalls[n_upcalls];
cc377352 586 struct upcall *upcall = &upcalls[n_upcalls];
cc377352
EJ
587 struct pkt_metadata md;
588 struct flow flow;
589 int error;
590
7174c145 591 ofpbuf_use_stub(recv_buf, recv_stubs[n_upcalls],
cc377352 592 sizeof recv_stubs[n_upcalls]);
a6f4ad08 593 if (dpif_recv(udpif->dpif, handler->handler_id, dupcall, recv_buf)) {
cc377352
EJ
594 ofpbuf_uninit(recv_buf);
595 break;
596 }
597
a6f4ad08 598 if (odp_flow_key_to_flow(dupcall->key, dupcall->key_len, &flow)
cc377352
EJ
599 == ODP_FIT_ERROR) {
600 goto free_dupcall;
601 }
602
a6f4ad08
AW
603 error = upcall_receive(upcall, udpif->backer, &dupcall->packet,
604 dupcall->type, dupcall->userdata, &flow);
cc377352
EJ
605 if (error) {
606 if (error == ENODEV) {
607 /* Received packet on datapath port for which we couldn't
608 * associate an ofproto. This can happen if a port is removed
609 * while traffic is being received. Print a rate-limited
610 * message in case it happens frequently. */
a6f4ad08
AW
611 dpif_flow_put(udpif->dpif, DPIF_FP_CREATE, dupcall->key,
612 dupcall->key_len, NULL, 0, NULL, 0, NULL);
cc377352
EJ
613 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
614 "port %"PRIu32, flow.in_port.odp_port);
615 }
616 goto free_dupcall;
617 }
618
a6f4ad08
AW
619 upcall->key = dupcall->key;
620 upcall->key_len = dupcall->key_len;
cc377352 621
8b7ea2d4
WZ
622 upcall->out_tun_key = dupcall->out_tun_key;
623
a6f4ad08 624 if (vsp_adjust_flow(upcall->ofproto, &flow, &dupcall->packet)) {
cc377352
EJ
625 upcall->vsp_adjusted = true;
626 }
627
628 md = pkt_metadata_from_flow(&flow);
a6f4ad08 629 flow_extract(&dupcall->packet, &md, &flow);
cc377352
EJ
630
631 error = process_upcall(udpif, upcall, NULL);
632 if (error) {
633 goto cleanup;
634 }
635
636 n_upcalls++;
637 continue;
638
639cleanup:
640 upcall_uninit(upcall);
641free_dupcall:
a6f4ad08 642 ofpbuf_uninit(&dupcall->packet);
cc377352
EJ
643 ofpbuf_uninit(recv_buf);
644 }
645
646 if (n_upcalls) {
647 handle_upcalls(handler->udpif, upcalls, n_upcalls);
648 for (i = 0; i < n_upcalls; i++) {
a6f4ad08 649 ofpbuf_uninit(&dupcalls[i].packet);
cc377352
EJ
650 ofpbuf_uninit(&recv_bufs[i]);
651 upcall_uninit(&upcalls[i]);
652 }
653 }
654
655 return n_upcalls;
656}
657
e79a6c83
EJ
658static void *
659udpif_revalidator(void *arg)
e1ec7dd4 660{
7d170098 661 /* Used by all revalidators. */
e79a6c83 662 struct revalidator *revalidator = arg;
7d170098
EJ
663 struct udpif *udpif = revalidator->udpif;
664 bool leader = revalidator == &udpif->revalidators[0];
665
666 /* Used only by the leader. */
667 long long int start_time = 0;
668 uint64_t last_reval_seq = 0;
7d170098 669 size_t n_flows = 0;
e1ec7dd4 670
8ba0a522 671 revalidator->id = ovsthread_id_self();
e79a6c83 672 for (;;) {
7d170098
EJ
673 if (leader) {
674 uint64_t reval_seq;
e79a6c83 675
7d170098
EJ
676 reval_seq = seq_read(udpif->reval_seq);
677 udpif->need_revalidate = last_reval_seq != reval_seq;
678 last_reval_seq = reval_seq;
e79a6c83 679
7d170098
EJ
680 n_flows = udpif_get_n_flows(udpif);
681 udpif->max_n_flows = MAX(n_flows, udpif->max_n_flows);
682 udpif->avg_n_flows = (udpif->avg_n_flows + n_flows) / 2;
683
684 /* Only the leader checks the exit latch to prevent a race where
685 * some threads think it's true and exit and others think it's
686 * false and block indefinitely on the reval_barrier */
687 udpif->reval_exit = latch_is_set(&udpif->exit_latch);
688
689 start_time = time_msec();
690 if (!udpif->reval_exit) {
ac64794a 691 udpif->dump = dpif_flow_dump_create(udpif->dpif);
e79a6c83
EJ
692 }
693 }
694
7d170098 695 /* Wait for the leader to start the flow dump. */
d8043da7 696 ovs_barrier_block(&udpif->reval_barrier);
7d170098
EJ
697 if (udpif->reval_exit) {
698 break;
e79a6c83 699 }
7d170098
EJ
700 revalidate(revalidator);
701
702 /* Wait for all flows to have been dumped before we garbage collect. */
d8043da7 703 ovs_barrier_block(&udpif->reval_barrier);
7d170098
EJ
704 revalidator_sweep(revalidator);
705
706 /* Wait for all revalidators to finish garbage collection. */
d8043da7 707 ovs_barrier_block(&udpif->reval_barrier);
7d170098
EJ
708
709 if (leader) {
b482e960 710 unsigned int flow_limit;
7d170098
EJ
711 long long int duration;
712
b482e960
JR
713 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
714
ac64794a 715 dpif_flow_dump_destroy(udpif->dump);
7d170098
EJ
716 seq_change(udpif->dump_seq);
717
718 duration = MAX(time_msec() - start_time, 1);
7d170098
EJ
719 udpif->dump_duration = duration;
720 if (duration > 2000) {
721 flow_limit /= duration / 1000;
722 } else if (duration > 1300) {
723 flow_limit = flow_limit * 3 / 4;
724 } else if (duration < 1000 && n_flows > 2000
725 && flow_limit < n_flows * 1000 / duration) {
726 flow_limit += 1000;
727 }
728 flow_limit = MIN(ofproto_flow_limit, MAX(flow_limit, 1000));
b482e960 729 atomic_store_relaxed(&udpif->flow_limit, flow_limit);
e79a6c83 730
7d170098
EJ
731 if (duration > 2000) {
732 VLOG_INFO("Spent an unreasonably long %lldms dumping flows",
733 duration);
734 }
e79a6c83 735
7d170098
EJ
736 poll_timer_wait_until(start_time + MIN(ofproto_max_idle, 500));
737 seq_wait(udpif->reval_seq, last_reval_seq);
738 latch_wait(&udpif->exit_latch);
739 poll_block();
e79a6c83
EJ
740 }
741 }
742
743 return NULL;
744}
745\f
e1ec7dd4 746static enum upcall_type
cc377352 747classify_upcall(enum dpif_upcall_type type, const struct nlattr *userdata)
e1ec7dd4 748{
e1ec7dd4
EJ
749 union user_action_cookie cookie;
750 size_t userdata_len;
751
752 /* First look at the upcall type. */
cc377352 753 switch (type) {
e1ec7dd4
EJ
754 case DPIF_UC_ACTION:
755 break;
756
757 case DPIF_UC_MISS:
758 return MISS_UPCALL;
759
760 case DPIF_N_UC_TYPES:
761 default:
cc377352 762 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32, type);
e1ec7dd4
EJ
763 return BAD_UPCALL;
764 }
765
766 /* "action" upcalls need a closer look. */
cc377352 767 if (!userdata) {
e1ec7dd4
EJ
768 VLOG_WARN_RL(&rl, "action upcall missing cookie");
769 return BAD_UPCALL;
770 }
cc377352 771 userdata_len = nl_attr_get_size(userdata);
e1ec7dd4
EJ
772 if (userdata_len < sizeof cookie.type
773 || userdata_len > sizeof cookie) {
34582733 774 VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
e1ec7dd4
EJ
775 userdata_len);
776 return BAD_UPCALL;
777 }
778 memset(&cookie, 0, sizeof cookie);
cc377352 779 memcpy(&cookie, nl_attr_get(userdata), userdata_len);
f5790bf6 780 if (userdata_len == MAX(8, sizeof cookie.sflow)
e1ec7dd4
EJ
781 && cookie.type == USER_ACTION_COOKIE_SFLOW) {
782 return SFLOW_UPCALL;
f5790bf6 783 } else if (userdata_len == MAX(8, sizeof cookie.slow_path)
e1ec7dd4
EJ
784 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
785 return MISS_UPCALL;
f5790bf6 786 } else if (userdata_len == MAX(8, sizeof cookie.flow_sample)
e1ec7dd4
EJ
787 && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
788 return FLOW_SAMPLE_UPCALL;
f5790bf6 789 } else if (userdata_len == MAX(8, sizeof cookie.ipfix)
e1ec7dd4
EJ
790 && cookie.type == USER_ACTION_COOKIE_IPFIX) {
791 return IPFIX_UPCALL;
792 } else {
793 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
34582733 794 " and size %"PRIuSIZE, cookie.type, userdata_len);
e1ec7dd4
EJ
795 return BAD_UPCALL;
796 }
797}
798
e79a6c83
EJ
799/* Calculates slow path actions for 'xout'. 'buf' must statically be
800 * initialized with at least 128 bytes of space. */
801static void
802compose_slow_path(struct udpif *udpif, struct xlate_out *xout,
cc377352 803 const struct flow *flow, odp_port_t odp_in_port,
9a159f74 804 struct ofpbuf *buf)
e79a6c83
EJ
805{
806 union user_action_cookie cookie;
807 odp_port_t port;
808 uint32_t pid;
809
810 cookie.type = USER_ACTION_COOKIE_SLOW_PATH;
811 cookie.slow_path.unused = 0;
812 cookie.slow_path.reason = xout->slow;
813
814 port = xout->slow & (SLOW_CFM | SLOW_BFD | SLOW_LACP | SLOW_STP)
815 ? ODPP_NONE
816 : odp_in_port;
9a159f74 817 pid = dpif_port_get_pid(udpif->dpif, port, flow_hash_5tuple(flow, 0));
8b7ea2d4
WZ
818 odp_put_userspace_action(pid, &cookie, sizeof cookie.slow_path, ODPP_NONE,
819 buf);
e79a6c83
EJ
820}
821
3d76b86c
AW
822/* If there is no error, the upcall must be destroyed with upcall_uninit()
823 * before quiescing, as the referred objects are guaranteed to exist only
824 * until the calling thread quiesces. Otherwise, do not call upcall_uninit()
825 * since the 'upcall->put_actions' remains uninitialized. */
cc377352
EJ
826static int
827upcall_receive(struct upcall *upcall, const struct dpif_backer *backer,
828 const struct ofpbuf *packet, enum dpif_upcall_type type,
829 const struct nlattr *userdata, const struct flow *flow)
830{
831 int error;
832
5c476ea3
JR
833 error = xlate_lookup(backer, flow, &upcall->ofproto, &upcall->ipfix,
834 &upcall->sflow, NULL, &upcall->in_port);
cc377352
EJ
835 if (error) {
836 return error;
837 }
838
839 upcall->flow = flow;
840 upcall->packet = packet;
841 upcall->type = type;
842 upcall->userdata = userdata;
843 ofpbuf_init(&upcall->put_actions, 0);
844
845 upcall->xout_initialized = false;
846 upcall->vsp_adjusted = false;
847
848 upcall->key = NULL;
849 upcall->key_len = 0;
850
8b7ea2d4
WZ
851 upcall->out_tun_key = NULL;
852
cc377352
EJ
853 return 0;
854}
855
a0bab870 856static void
cc377352
EJ
857upcall_xlate(struct udpif *udpif, struct upcall *upcall,
858 struct ofpbuf *odp_actions)
e1ec7dd4 859{
cc377352 860 struct dpif_flow_stats stats;
691d39b2 861 struct xlate_in xin;
a0bab870 862
cc377352
EJ
863 stats.n_packets = 1;
864 stats.n_bytes = ofpbuf_size(upcall->packet);
865 stats.used = time_msec();
866 stats.tcp_flags = ntohs(upcall->flow->tcp_flags);
a0bab870 867
cc377352
EJ
868 xlate_in_init(&xin, upcall->ofproto, upcall->flow, upcall->in_port, NULL,
869 stats.tcp_flags, upcall->packet);
870 xin.odp_actions = odp_actions;
a0bab870 871
cc377352
EJ
872 if (upcall->type == DPIF_UC_MISS) {
873 xin.resubmit_stats = &stats;
a0bab870
RW
874 } else {
875 /* For non-miss upcalls, there's a flow in the datapath which this
876 * packet was accounted to. Presumably the revalidators will deal
877 * with pushing its stats eventually. */
e1ec7dd4
EJ
878 }
879
a0bab870 880 xlate_actions(&xin, &upcall->xout);
cc377352
EJ
881 upcall->xout_initialized = true;
882
883 /* Special case for fail-open mode.
884 *
885 * If we are in fail-open mode, but we are connected to a controller too,
886 * then we should send the packet up to the controller in the hope that it
887 * will try to set up a flow and thereby allow us to exit fail-open.
888 *
889 * See the top-level comment in fail-open.c for more information.
890 *
891 * Copy packets before they are modified by execution. */
892 if (upcall->xout.fail_open) {
893 const struct ofpbuf *packet = upcall->packet;
894 struct ofproto_packet_in *pin;
895
896 pin = xmalloc(sizeof *pin);
897 pin->up.packet = xmemdup(ofpbuf_data(packet), ofpbuf_size(packet));
898 pin->up.packet_len = ofpbuf_size(packet);
899 pin->up.reason = OFPR_NO_MATCH;
900 pin->up.table_id = 0;
901 pin->up.cookie = OVS_BE64_MAX;
902 flow_get_metadata(upcall->flow, &pin->up.fmd);
903 pin->send_len = 0; /* Not used for flow table misses. */
904 pin->miss_type = OFPROTO_PACKET_IN_NO_MISS;
905 ofproto_dpif_send_packet_in(upcall->ofproto, pin);
906 }
907
908 if (!upcall->xout.slow) {
909 ofpbuf_use_const(&upcall->put_actions,
910 ofpbuf_data(upcall->xout.odp_actions),
911 ofpbuf_size(upcall->xout.odp_actions));
912 } else {
913 ofpbuf_init(&upcall->put_actions, 0);
914 compose_slow_path(udpif, &upcall->xout, upcall->flow,
915 upcall->flow->in_port.odp_port,
916 &upcall->put_actions);
917 }
e1ec7dd4
EJ
918}
919
3eed53e9 920static void
cc377352 921upcall_uninit(struct upcall *upcall)
6b31e073 922{
cc377352
EJ
923 if (upcall) {
924 if (upcall->xout_initialized) {
925 xlate_out_uninit(&upcall->xout);
926 }
927 ofpbuf_uninit(&upcall->put_actions);
cc377352 928 }
6b31e073
RW
929}
930
623540e4
EJ
931static int
932upcall_cb(const struct ofpbuf *packet, const struct flow *flow,
933 enum dpif_upcall_type type, const struct nlattr *userdata,
934 struct ofpbuf *actions, struct flow_wildcards *wc,
935 struct ofpbuf *put_actions, void *aux)
6b31e073 936{
623540e4
EJ
937 struct udpif *udpif = aux;
938 unsigned int flow_limit;
939 struct upcall upcall;
940 bool megaflow;
941 int error;
6b31e073 942
b482e960
JR
943 atomic_read_relaxed(&enable_megaflows, &megaflow);
944 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
945
623540e4
EJ
946 error = upcall_receive(&upcall, udpif->backer, packet, type, userdata,
947 flow);
948 if (error) {
3d76b86c 949 return error;
6b31e073 950 }
6b31e073 951
623540e4
EJ
952 error = process_upcall(udpif, &upcall, actions);
953 if (error) {
954 goto out;
955 }
cc377352 956
623540e4
EJ
957 if (upcall.xout.slow && put_actions) {
958 ofpbuf_put(put_actions, ofpbuf_data(&upcall.put_actions),
959 ofpbuf_size(&upcall.put_actions));
960 }
cc377352 961
b482e960 962 if (OVS_LIKELY(wc)) {
623540e4
EJ
963 if (megaflow) {
964 /* XXX: This could be avoided with sufficient API changes. */
965 *wc = upcall.xout.wc;
966 } else {
967 memset(wc, 0xff, sizeof *wc);
968 flow_wildcards_clear_non_packet_fields(wc);
9a159f74 969 }
623540e4 970 }
9a159f74 971
623540e4
EJ
972 if (udpif_get_n_flows(udpif) >= flow_limit) {
973 error = ENOSPC;
6b31e073 974 }
623540e4
EJ
975
976out:
977 upcall_uninit(&upcall);
978 return error;
6b31e073 979}
10e57640 980
3eed53e9 981static int
cc377352
EJ
982process_upcall(struct udpif *udpif, struct upcall *upcall,
983 struct ofpbuf *odp_actions)
6b31e073 984{
cc377352
EJ
985 const struct nlattr *userdata = upcall->userdata;
986 const struct ofpbuf *packet = upcall->packet;
987 const struct flow *flow = upcall->flow;
04a19fb8 988
cc377352
EJ
989 switch (classify_upcall(upcall->type, userdata)) {
990 case MISS_UPCALL:
991 upcall_xlate(udpif, upcall, odp_actions);
992 return 0;
10e57640 993
6b31e073 994 case SFLOW_UPCALL:
cc377352 995 if (upcall->sflow) {
6b31e073
RW
996 union user_action_cookie cookie;
997
998 memset(&cookie, 0, sizeof cookie);
cc377352
EJ
999 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.sflow);
1000 dpif_sflow_received(upcall->sflow, packet, flow,
1001 flow->in_port.odp_port, &cookie);
6b31e073
RW
1002 }
1003 break;
cc377352 1004
6b31e073 1005 case IPFIX_UPCALL:
cc377352 1006 if (upcall->ipfix) {
8b7ea2d4
WZ
1007 union user_action_cookie cookie;
1008 struct flow_tnl output_tunnel_key;
1009
1010 memset(&cookie, 0, sizeof cookie);
1011 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.ipfix);
1012
1013 if (upcall->out_tun_key) {
1014 memset(&output_tunnel_key, 0, sizeof output_tunnel_key);
1015 odp_tun_key_from_attr(upcall->out_tun_key,
1016 &output_tunnel_key);
1017 }
1018 dpif_ipfix_bridge_sample(upcall->ipfix, packet, flow,
1019 flow->in_port.odp_port,
1020 cookie.ipfix.output_odp_port,
1021 upcall->out_tun_key ?
1022 &output_tunnel_key : NULL);
6b31e073
RW
1023 }
1024 break;
cc377352 1025
6b31e073 1026 case FLOW_SAMPLE_UPCALL:
cc377352 1027 if (upcall->ipfix) {
6b31e073
RW
1028 union user_action_cookie cookie;
1029
1030 memset(&cookie, 0, sizeof cookie);
cc377352 1031 memcpy(&cookie, nl_attr_get(userdata), sizeof cookie.flow_sample);
6b31e073
RW
1032
1033 /* The flow reflects exactly the contents of the packet.
1034 * Sample the packet using it. */
cc377352 1035 dpif_ipfix_flow_sample(upcall->ipfix, packet, flow,
6b31e073
RW
1036 cookie.flow_sample.collector_set_id,
1037 cookie.flow_sample.probability,
1038 cookie.flow_sample.obs_domain_id,
1039 cookie.flow_sample.obs_point_id);
e1ec7dd4 1040 }
6b31e073 1041 break;
cc377352 1042
6b31e073
RW
1043 case BAD_UPCALL:
1044 break;
6b31e073 1045 }
10e57640 1046
cc377352 1047 return EAGAIN;
9a159f74
AW
1048}
1049
1050static void
6b31e073 1051handle_upcalls(struct udpif *udpif, struct upcall *upcalls,
a0bab870 1052 size_t n_upcalls)
9a159f74 1053{
cc377352 1054 struct odputil_keybuf mask_bufs[UPCALL_MAX_BATCH];
a0bab870
RW
1055 struct dpif_op *opsp[UPCALL_MAX_BATCH * 2];
1056 struct dpif_op ops[UPCALL_MAX_BATCH * 2];
9a159f74 1057 unsigned int flow_limit;
cc377352
EJ
1058 size_t n_ops, i;
1059 bool may_put;
b482e960
JR
1060 bool megaflow;
1061
1062 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
1063 atomic_read_relaxed(&enable_megaflows, &megaflow);
9a159f74 1064
9a159f74
AW
1065 may_put = udpif_get_n_flows(udpif) < flow_limit;
1066
a0bab870 1067 /* Handle the packets individually in order of arrival.
04a19fb8
BP
1068 *
1069 * - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
1070 * processes received packets for these protocols.
1071 *
1072 * - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
1073 * controller.
1074 *
1075 * The loop fills 'ops' with an array of operations to execute in the
1076 * datapath. */
1077 n_ops = 0;
9a159f74
AW
1078 for (i = 0; i < n_upcalls; i++) {
1079 struct upcall *upcall = &upcalls[i];
cc377352 1080 const struct ofpbuf *packet = upcall->packet;
e79a6c83 1081 struct dpif_op *op;
d02c42bf 1082
cc377352
EJ
1083 if (upcall->vsp_adjusted) {
1084 /* This packet was received on a VLAN splinter port. We added a
1085 * VLAN to the packet to make the packet resemble the flow, but the
1086 * actions were composed assuming that the packet contained no
1087 * VLAN. So, we must remove the VLAN header from the packet before
1088 * trying to execute the actions. */
1089 if (ofpbuf_size(upcall->xout.odp_actions)) {
1090 eth_pop_vlan(CONST_CAST(struct ofpbuf *, upcall->packet));
d02c42bf
AZ
1091 }
1092
1093 /* Remove the flow vlan tags inserted by vlan splinter logic
1094 * to ensure megaflow masks generated match the data path flow. */
cc377352 1095 CONST_CAST(struct flow *, upcall->flow)->vlan_tci = 0;
e79a6c83 1096 }
04a19fb8 1097
73e141f9
BP
1098 /* Do not install a flow into the datapath if:
1099 *
1100 * - The datapath already has too many flows.
1101 *
73e141f9
BP
1102 * - We received this packet via some flow installed in the kernel
1103 * already. */
cc377352 1104 if (may_put && upcall->type == DPIF_UC_MISS) {
d02c42bf 1105 struct ofpbuf mask;
d02c42bf 1106
cc377352 1107 ofpbuf_use_stack(&mask, &mask_bufs[i], sizeof mask_bufs[i]);
b482e960 1108
d02c42bf 1109 if (megaflow) {
8bfd0fda 1110 size_t max_mpls;
7ce2769e 1111 bool recirc;
8bfd0fda 1112
a0bab870
RW
1113 recirc = ofproto_dpif_get_enable_recirc(upcall->ofproto);
1114 max_mpls = ofproto_dpif_get_max_mpls_depth(upcall->ofproto);
1115 odp_flow_key_from_mask(&mask, &upcall->xout.wc.masks,
cc377352 1116 upcall->flow, UINT32_MAX, max_mpls,
7ce2769e 1117 recirc);
d02c42bf
AZ
1118 }
1119
e79a6c83
EJ
1120 op = &ops[n_ops++];
1121 op->type = DPIF_OP_FLOW_PUT;
a7d1bbdc 1122 op->u.flow_put.flags = DPIF_FP_CREATE;
a0bab870
RW
1123 op->u.flow_put.key = upcall->key;
1124 op->u.flow_put.key_len = upcall->key_len;
1f317cb5
PS
1125 op->u.flow_put.mask = ofpbuf_data(&mask);
1126 op->u.flow_put.mask_len = ofpbuf_size(&mask);
e79a6c83 1127 op->u.flow_put.stats = NULL;
cc377352
EJ
1128 op->u.flow_put.actions = ofpbuf_data(&upcall->put_actions);
1129 op->u.flow_put.actions_len = ofpbuf_size(&upcall->put_actions);
e79a6c83
EJ
1130 }
1131
cc377352 1132 if (ofpbuf_size(upcall->xout.odp_actions)) {
04a19fb8
BP
1133 op = &ops[n_ops++];
1134 op->type = DPIF_OP_EXECUTE;
cc377352 1135 op->u.execute.packet = CONST_CAST(struct ofpbuf *, packet);
a0bab870 1136 odp_key_to_pkt_metadata(upcall->key, upcall->key_len,
758c456d 1137 &op->u.execute.md);
cc377352
EJ
1138 op->u.execute.actions = ofpbuf_data(upcall->xout.odp_actions);
1139 op->u.execute.actions_len = ofpbuf_size(upcall->xout.odp_actions);
a0bab870 1140 op->u.execute.needs_help = (upcall->xout.slow & SLOW_ACTION) != 0;
43f9ac0a 1141 op->u.execute.probe = false;
04a19fb8 1142 }
e1ec7dd4 1143 }
e1ec7dd4 1144
da546e07
JR
1145 /* Execute batch. */
1146 for (i = 0; i < n_ops; i++) {
1147 opsp[i] = &ops[i];
1148 }
1149 dpif_operate(udpif->dpif, opsp, n_ops);
e79a6c83
EJ
1150}
1151
7d170098 1152/* Must be called with udpif->ukeys[hash % udpif->n_revalidators].mutex. */
e79a6c83 1153static struct udpif_key *
feca8bd7
JS
1154ukey_lookup(struct udpif *udpif, const struct nlattr *key, size_t key_len,
1155 uint32_t hash)
1156 OVS_REQUIRES(udpif->ukeys->mutex)
e79a6c83
EJ
1157{
1158 struct udpif_key *ukey;
7d170098 1159 struct hmap *hmap = &udpif->ukeys[hash % udpif->n_revalidators].hmap;
e79a6c83 1160
7d170098
EJ
1161 HMAP_FOR_EACH_WITH_HASH (ukey, hmap_node, hash, hmap) {
1162 if (ukey->key_len == key_len && !memcmp(ukey->key, key, key_len)) {
e79a6c83
EJ
1163 return ukey;
1164 }
1165 }
1166 return NULL;
1167}
1168
feca8bd7
JS
1169/* Creates a ukey for 'key' and 'key_len', returning it with ukey->mutex in
1170 * a locked state. */
13bb6ed0
JS
1171static struct udpif_key *
1172ukey_create(const struct nlattr *key, size_t key_len, long long int used)
feca8bd7 1173 OVS_NO_THREAD_SAFETY_ANALYSIS
13bb6ed0
JS
1174{
1175 struct udpif_key *ukey = xmalloc(sizeof *ukey);
1176
feca8bd7 1177 ovs_mutex_init(&ukey->mutex);
02334943 1178 ukey->key = &ukey->key_buf_nla;
13bb6ed0
JS
1179 memcpy(&ukey->key_buf, key, key_len);
1180 ukey->key_len = key_len;
1181
7d170098 1182 ovs_mutex_lock(&ukey->mutex);
efa08531 1183 ukey->dump_seq = 0;
73a3c475 1184 ukey->flow_exists = true;
13bb6ed0
JS
1185 ukey->created = used ? used : time_msec();
1186 memset(&ukey->stats, 0, sizeof ukey->stats);
b256dc52 1187 ukey->xcache = NULL;
13bb6ed0
JS
1188
1189 return ukey;
1190}
1191
feca8bd7
JS
1192/* Searches for a ukey in 'udpif->ukeys' that matches 'key' and 'key_len' and
1193 * attempts to lock the ukey. If the ukey does not exist, create it.
7d170098 1194 *
feca8bd7
JS
1195 * Returns true on success, setting *result to the matching ukey and returning
1196 * it in a locked state. Otherwise, returns false and clears *result. */
7d170098 1197static bool
feca8bd7
JS
1198ukey_acquire(struct udpif *udpif, const struct nlattr *key, size_t key_len,
1199 long long int used, struct udpif_key **result)
1200 OVS_TRY_LOCK(true, (*result)->mutex)
7d170098 1201{
feca8bd7
JS
1202 struct udpif_key *ukey;
1203 uint32_t hash, idx;
1204 bool locked = false;
1205
1206 hash = hash_bytes(key, key_len, udpif->secret);
1207 idx = hash % udpif->n_revalidators;
7d170098
EJ
1208
1209 ovs_mutex_lock(&udpif->ukeys[idx].mutex);
feca8bd7
JS
1210 ukey = ukey_lookup(udpif, key, key_len, hash);
1211 if (!ukey) {
1212 ukey = ukey_create(key, key_len, used);
7d170098 1213 hmap_insert(&udpif->ukeys[idx].hmap, &ukey->hmap_node, hash);
feca8bd7
JS
1214 locked = true;
1215 } else if (!ovs_mutex_trylock(&ukey->mutex)) {
1216 locked = true;
7d170098
EJ
1217 }
1218 ovs_mutex_unlock(&udpif->ukeys[idx].mutex);
1219
feca8bd7
JS
1220 if (locked) {
1221 *result = ukey;
1222 } else {
1223 *result = NULL;
1224 }
1225 return locked;
7d170098
EJ
1226}
1227
e79a6c83
EJ
1228static void
1229ukey_delete(struct revalidator *revalidator, struct udpif_key *ukey)
7d170098 1230 OVS_NO_THREAD_SAFETY_ANALYSIS
e79a6c83 1231{
7d170098
EJ
1232 if (revalidator) {
1233 hmap_remove(revalidator->ukeys, &ukey->hmap_node);
1234 }
b256dc52 1235 xlate_cache_delete(ukey->xcache);
7d170098 1236 ovs_mutex_destroy(&ukey->mutex);
e79a6c83
EJ
1237 free(ukey);
1238}
1239
698ffe36 1240static bool
49fae772
JS
1241should_revalidate(const struct udpif *udpif, uint64_t packets,
1242 long long int used)
698ffe36
JS
1243{
1244 long long int metric, now, duration;
1245
49fae772
JS
1246 if (udpif->dump_duration < 200) {
1247 /* We are likely to handle full revalidation for the flows. */
1248 return true;
1249 }
1250
698ffe36
JS
1251 /* Calculate the mean time between seeing these packets. If this
1252 * exceeds the threshold, then delete the flow rather than performing
1253 * costly revalidation for flows that aren't being hit frequently.
1254 *
1255 * This is targeted at situations where the dump_duration is high (~1s),
1256 * and revalidation is triggered by a call to udpif_revalidate(). In
1257 * these situations, revalidation of all flows causes fluctuations in the
1258 * flow_limit due to the interaction with the dump_duration and max_idle.
1259 * This tends to result in deletion of low-throughput flows anyway, so
1260 * skip the revalidation and just delete those flows. */
1261 packets = MAX(packets, 1);
1262 now = MAX(used, time_msec());
1263 duration = now - used;
1264 metric = duration / packets;
1265
49fae772
JS
1266 if (metric < 200) {
1267 /* The flow is receiving more than ~5pps, so keep it. */
1268 return true;
698ffe36 1269 }
49fae772 1270 return false;
698ffe36
JS
1271}
1272
e79a6c83 1273static bool
7d170098 1274revalidate_ukey(struct udpif *udpif, struct udpif_key *ukey,
ac64794a 1275 const struct dpif_flow *f)
acaa8dac 1276 OVS_REQUIRES(ukey->mutex)
e79a6c83 1277{
e79a6c83
EJ
1278 uint64_t slow_path_buf[128 / 8];
1279 struct xlate_out xout, *xoutp;
42f3baca 1280 struct netflow *netflow;
e79a6c83
EJ
1281 struct ofproto_dpif *ofproto;
1282 struct dpif_flow_stats push;
7d170098
EJ
1283 struct ofpbuf xout_actions;
1284 struct flow flow, dp_mask;
1285 uint32_t *dp32, *xout32;
cc377352 1286 ofp_port_t ofp_in_port;
e79a6c83 1287 struct xlate_in xin;
698ffe36 1288 long long int last_used;
e79a6c83
EJ
1289 int error;
1290 size_t i;
0725e747 1291 bool ok;
e79a6c83
EJ
1292
1293 ok = false;
1294 xoutp = NULL;
42f3baca 1295 netflow = NULL;
e79a6c83 1296
698ffe36 1297 last_used = ukey->stats.used;
ac64794a
BP
1298 push.used = f->stats.used;
1299 push.tcp_flags = f->stats.tcp_flags;
1300 push.n_packets = (f->stats.n_packets > ukey->stats.n_packets
1301 ? f->stats.n_packets - ukey->stats.n_packets
1302 : 0);
1303 push.n_bytes = (f->stats.n_bytes > ukey->stats.n_bytes
1304 ? f->stats.n_bytes - ukey->stats.n_bytes
1305 : 0);
e79a6c83 1306
7d170098 1307 if (udpif->need_revalidate && last_used
49fae772 1308 && !should_revalidate(udpif, push.n_packets, last_used)) {
698ffe36
JS
1309 ok = false;
1310 goto exit;
1311 }
1312
28c5588e 1313 /* We will push the stats, so update the ukey stats cache. */
ac64794a 1314 ukey->stats = f->stats;
7d170098 1315 if (!push.n_packets && !udpif->need_revalidate) {
e79a6c83
EJ
1316 ok = true;
1317 goto exit;
1318 }
1319
df1a9a49 1320 if (ukey->xcache && !udpif->need_revalidate) {
0725e747 1321 xlate_push_stats(ukey->xcache, &push);
df1a9a49
JS
1322 ok = true;
1323 goto exit;
b256dc52
JS
1324 }
1325
cc377352
EJ
1326 if (odp_flow_key_to_flow(ukey->key, ukey->key_len, &flow)
1327 == ODP_FIT_ERROR) {
1328 goto exit;
1329 }
1330
5c476ea3
JR
1331 error = xlate_lookup(udpif->backer, &flow, &ofproto, NULL, NULL, &netflow,
1332 &ofp_in_port);
e79a6c83
EJ
1333 if (error) {
1334 goto exit;
1335 }
1336
df1a9a49
JS
1337 if (udpif->need_revalidate) {
1338 xlate_cache_clear(ukey->xcache);
1339 }
b256dc52
JS
1340 if (!ukey->xcache) {
1341 ukey->xcache = xlate_cache_new();
1342 }
1343
cc377352
EJ
1344 xlate_in_init(&xin, ofproto, &flow, ofp_in_port, NULL, push.tcp_flags,
1345 NULL);
0725e747
BP
1346 if (push.n_packets) {
1347 xin.resubmit_stats = &push;
1348 xin.may_learn = true;
1349 }
b256dc52 1350 xin.xcache = ukey->xcache;
7d170098 1351 xin.skip_wildcards = !udpif->need_revalidate;
e79a6c83
EJ
1352 xlate_actions(&xin, &xout);
1353 xoutp = &xout;
ddeca9a4 1354
7d170098 1355 if (!udpif->need_revalidate) {
e79a6c83
EJ
1356 ok = true;
1357 goto exit;
1358 }
1359
1360 if (!xout.slow) {
cc377352
EJ
1361 ofpbuf_use_const(&xout_actions, ofpbuf_data(xout.odp_actions),
1362 ofpbuf_size(xout.odp_actions));
05067881 1363 } else {
e79a6c83 1364 ofpbuf_use_stack(&xout_actions, slow_path_buf, sizeof slow_path_buf);
cc377352
EJ
1365 compose_slow_path(udpif, &xout, &flow, flow.in_port.odp_port,
1366 &xout_actions);
e79a6c83
EJ
1367 }
1368
ac64794a
BP
1369 if (f->actions_len != ofpbuf_size(&xout_actions)
1370 || memcmp(ofpbuf_data(&xout_actions), f->actions, f->actions_len)) {
e79a6c83
EJ
1371 goto exit;
1372 }
1373
ac64794a 1374 if (odp_flow_key_to_mask(f->mask, f->mask_len, &dp_mask, &flow)
e79a6c83
EJ
1375 == ODP_FIT_ERROR) {
1376 goto exit;
1377 }
1378
1379 /* Since the kernel is free to ignore wildcarded bits in the mask, we can't
1380 * directly check that the masks are the same. Instead we check that the
1381 * mask in the kernel is more specific i.e. less wildcarded, than what
1382 * we've calculated here. This guarantees we don't catch any packets we
1383 * shouldn't with the megaflow. */
7d170098 1384 dp32 = (uint32_t *) &dp_mask;
e79a6c83
EJ
1385 xout32 = (uint32_t *) &xout.wc.masks;
1386 for (i = 0; i < FLOW_U32S; i++) {
7d170098 1387 if ((dp32[i] | xout32[i]) != dp32[i]) {
e79a6c83
EJ
1388 goto exit;
1389 }
1390 }
1391 ok = true;
1392
1393exit:
dcc2c6cd
JR
1394 if (netflow && !ok) {
1395 netflow_flow_clear(netflow, &flow);
42f3baca 1396 }
e79a6c83
EJ
1397 xlate_out_uninit(xoutp);
1398 return ok;
1399}
1400
13bb6ed0
JS
1401struct dump_op {
1402 struct udpif_key *ukey;
13bb6ed0
JS
1403 struct dpif_flow_stats stats; /* Stats for 'op'. */
1404 struct dpif_op op; /* Flow del operation. */
1405};
1406
e79a6c83 1407static void
13bb6ed0 1408dump_op_init(struct dump_op *op, const struct nlattr *key, size_t key_len,
7d170098 1409 struct udpif_key *ukey)
13bb6ed0
JS
1410{
1411 op->ukey = ukey;
13bb6ed0
JS
1412 op->op.type = DPIF_OP_FLOW_DEL;
1413 op->op.u.flow_del.key = key;
1414 op->op.u.flow_del.key_len = key_len;
1415 op->op.u.flow_del.stats = &op->stats;
1416}
1417
1418static void
7d170098 1419push_dump_ops__(struct udpif *udpif, struct dump_op *ops, size_t n_ops)
e79a6c83 1420{
13bb6ed0
JS
1421 struct dpif_op *opsp[REVALIDATE_MAX_BATCH];
1422 size_t i;
e79a6c83 1423
13bb6ed0
JS
1424 ovs_assert(n_ops <= REVALIDATE_MAX_BATCH);
1425 for (i = 0; i < n_ops; i++) {
1426 opsp[i] = &ops[i].op;
1427 }
1428 dpif_operate(udpif->dpif, opsp, n_ops);
1429
1430 for (i = 0; i < n_ops; i++) {
1431 struct dump_op *op = &ops[i];
1432 struct dpif_flow_stats *push, *stats, push_buf;
1433
1434 stats = op->op.u.flow_del.stats;
5e73c322
JS
1435 push = &push_buf;
1436
1437 ovs_mutex_lock(&op->ukey->mutex);
1438 push->used = MAX(stats->used, op->ukey->stats.used);
1439 push->tcp_flags = stats->tcp_flags | op->ukey->stats.tcp_flags;
1440 push->n_packets = stats->n_packets - op->ukey->stats.n_packets;
1441 push->n_bytes = stats->n_bytes - op->ukey->stats.n_bytes;
1442 ovs_mutex_unlock(&op->ukey->mutex);
13bb6ed0
JS
1443
1444 if (push->n_packets || netflow_exists()) {
1445 struct ofproto_dpif *ofproto;
1446 struct netflow *netflow;
cc377352 1447 ofp_port_t ofp_in_port;
13bb6ed0 1448 struct flow flow;
5e73c322 1449 int error;
b256dc52 1450
5e73c322
JS
1451 ovs_mutex_lock(&op->ukey->mutex);
1452 if (op->ukey->xcache) {
0725e747 1453 xlate_push_stats(op->ukey->xcache, push);
7d170098 1454 ovs_mutex_unlock(&op->ukey->mutex);
5e73c322 1455 continue;
b256dc52 1456 }
5e73c322 1457 ovs_mutex_unlock(&op->ukey->mutex);
13bb6ed0 1458
cc377352
EJ
1459 if (odp_flow_key_to_flow(op->op.u.flow_del.key,
1460 op->op.u.flow_del.key_len, &flow)
1461 == ODP_FIT_ERROR) {
1462 continue;
1463 }
1464
5c476ea3
JR
1465 error = xlate_lookup(udpif->backer, &flow, &ofproto,
1466 NULL, NULL, &netflow, &ofp_in_port);
5e73c322 1467 if (!error) {
13bb6ed0
JS
1468 struct xlate_in xin;
1469
cc377352
EJ
1470 xlate_in_init(&xin, ofproto, &flow, ofp_in_port, NULL,
1471 push->tcp_flags, NULL);
13bb6ed0 1472 xin.resubmit_stats = push->n_packets ? push : NULL;
0725e747 1473 xin.may_learn = push->n_packets > 0;
13bb6ed0
JS
1474 xin.skip_wildcards = true;
1475 xlate_actions_for_side_effects(&xin);
1476
1477 if (netflow) {
13bb6ed0 1478 netflow_flow_clear(netflow, &flow);
13bb6ed0
JS
1479 }
1480 }
1481 }
1482 }
7d170098 1483}
13bb6ed0 1484
7d170098
EJ
1485static void
1486push_dump_ops(struct revalidator *revalidator,
1487 struct dump_op *ops, size_t n_ops)
1488{
1489 int i;
13bb6ed0 1490
7d170098
EJ
1491 push_dump_ops__(revalidator->udpif, ops, n_ops);
1492 for (i = 0; i < n_ops; i++) {
1493 ukey_delete(revalidator, ops[i].ukey);
13bb6ed0
JS
1494 }
1495}
1496
1497static void
7d170098 1498revalidate(struct revalidator *revalidator)
13bb6ed0
JS
1499{
1500 struct udpif *udpif = revalidator->udpif;
ac64794a 1501 struct dpif_flow_dump_thread *dump_thread;
efa08531 1502 uint64_t dump_seq;
e79a6c83 1503 unsigned int flow_limit;
e79a6c83 1504
efa08531 1505 dump_seq = seq_read(udpif->dump_seq);
b482e960 1506 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
ac64794a
BP
1507 dump_thread = dpif_flow_dump_thread_create(udpif->dump);
1508 for (;;) {
1509 struct dump_op ops[REVALIDATE_MAX_BATCH];
1510 int n_ops = 0;
e79a6c83 1511
ac64794a
BP
1512 struct dpif_flow flows[REVALIDATE_MAX_BATCH];
1513 const struct dpif_flow *f;
1514 int n_dumped;
7d170098 1515
ac64794a
BP
1516 long long int max_idle;
1517 long long int now;
1518 size_t n_dp_flows;
1519 bool kill_them_all;
e79a6c83 1520
ac64794a
BP
1521 n_dumped = dpif_flow_dump_next(dump_thread, flows, ARRAY_SIZE(flows));
1522 if (!n_dumped) {
1523 break;
73a3c475
JS
1524 }
1525
ac64794a
BP
1526 now = time_msec();
1527
1528 /* In normal operation we want to keep flows around until they have
1529 * been idle for 'ofproto_max_idle' milliseconds. However:
1530 *
1531 * - If the number of datapath flows climbs above 'flow_limit',
1532 * drop that down to 100 ms to try to bring the flows down to
1533 * the limit.
1534 *
1535 * - If the number of datapath flows climbs above twice
1536 * 'flow_limit', delete all the datapath flows as an emergency
1537 * measure. (We reassess this condition for the next batch of
1538 * datapath flows, so we will recover before all the flows are
1539 * gone.) */
1540 n_dp_flows = udpif_get_n_flows(udpif);
1541 kill_them_all = n_dp_flows > flow_limit * 2;
1542 max_idle = n_dp_flows > flow_limit ? 100 : ofproto_max_idle;
1543
1544 for (f = flows; f < &flows[n_dumped]; f++) {
1545 long long int used = f->stats.used;
feca8bd7 1546 struct udpif_key *ukey;
efa08531 1547 bool already_dumped, keep;
acaa8dac 1548
feca8bd7
JS
1549 if (!ukey_acquire(udpif, f->key, f->key_len, used, &ukey)) {
1550 /* We couldn't acquire the ukey. This means that
1551 * another revalidator is processing this flow
1552 * concurrently, so don't bother processing it. */
ec47af51 1553 COVERAGE_INC(dumped_duplicate_flow);
acaa8dac
JS
1554 continue;
1555 }
1556
efa08531 1557 already_dumped = ukey->dump_seq == dump_seq;
acaa8dac 1558 if (already_dumped) {
ec47af51
JS
1559 /* The flow has already been handled during this flow dump
1560 * operation. Skip it. */
1561 if (ukey->xcache) {
1562 COVERAGE_INC(dumped_duplicate_flow);
1563 } else {
1564 COVERAGE_INC(dumped_new_flow);
1565 }
acaa8dac
JS
1566 ovs_mutex_unlock(&ukey->mutex);
1567 continue;
1568 }
1569
1570 if (!used) {
1571 used = ukey->created;
1572 }
ac64794a 1573 if (kill_them_all || (used && used < now - max_idle)) {
efa08531 1574 keep = false;
ac64794a 1575 } else {
efa08531 1576 keep = revalidate_ukey(udpif, ukey, f);
ac64794a 1577 }
efa08531
JS
1578 ukey->dump_seq = dump_seq;
1579 ukey->flow_exists = keep;
e79a6c83 1580
efa08531 1581 if (!keep) {
ac64794a
BP
1582 dump_op_init(&ops[n_ops++], f->key, f->key_len, ukey);
1583 }
acaa8dac 1584 ovs_mutex_unlock(&ukey->mutex);
7d170098 1585 }
ad3415c0 1586
ac64794a 1587 if (n_ops) {
7d170098 1588 push_dump_ops__(udpif, ops, n_ops);
7d170098 1589 }
e79a6c83 1590 }
ac64794a 1591 dpif_flow_dump_thread_destroy(dump_thread);
e79a6c83
EJ
1592}
1593
3b62a9d3
JS
1594/* Called with exclusive access to 'revalidator' and 'ukey'. */
1595static bool
1596handle_missed_revalidation(struct revalidator *revalidator,
1597 struct udpif_key *ukey)
1598 OVS_NO_THREAD_SAFETY_ANALYSIS
1599{
1600 struct udpif *udpif = revalidator->udpif;
1601 struct dpif_flow flow;
6fe09f8c
JS
1602 struct ofpbuf buf;
1603 uint64_t stub[DPIF_FLOW_BUFSIZE / 8];
3b62a9d3
JS
1604 bool keep = false;
1605
1606 COVERAGE_INC(revalidate_missed_dp_flow);
1607
6fe09f8c 1608 ofpbuf_use_stub(&buf, &stub, sizeof stub);
3b62a9d3
JS
1609 if (!dpif_flow_get(udpif->dpif, ukey->key, ukey->key_len, &buf, &flow)) {
1610 keep = revalidate_ukey(udpif, ukey, &flow);
3b62a9d3 1611 }
6fe09f8c 1612 ofpbuf_uninit(&buf);
3b62a9d3
JS
1613
1614 return keep;
1615}
1616
e79a6c83 1617static void
e96a5c24 1618revalidator_sweep__(struct revalidator *revalidator, bool purge)
7d170098 1619 OVS_NO_THREAD_SAFETY_ANALYSIS
e79a6c83 1620{
e4b79342 1621 struct dump_op ops[REVALIDATE_MAX_BATCH];
e79a6c83 1622 struct udpif_key *ukey, *next;
e4b79342 1623 size_t n_ops;
efa08531 1624 uint64_t dump_seq;
e4b79342
JS
1625
1626 n_ops = 0;
efa08531 1627 dump_seq = seq_read(revalidator->udpif->dump_seq);
e79a6c83 1628
7d170098
EJ
1629 /* During garbage collection, this revalidator completely owns its ukeys
1630 * map, and therefore doesn't need to do any locking. */
1631 HMAP_FOR_EACH_SAFE (ukey, next, hmap_node, revalidator->ukeys) {
3b62a9d3
JS
1632 if (ukey->flow_exists
1633 && (purge
1634 || (ukey->dump_seq != dump_seq
1635 && revalidator->udpif->need_revalidate
1636 && !handle_missed_revalidation(revalidator, ukey)))) {
e4b79342
JS
1637 struct dump_op *op = &ops[n_ops++];
1638
7d170098 1639 dump_op_init(op, ukey->key, ukey->key_len, ukey);
e4b79342
JS
1640 if (n_ops == REVALIDATE_MAX_BATCH) {
1641 push_dump_ops(revalidator, ops, n_ops);
1642 n_ops = 0;
1643 }
3b62a9d3
JS
1644 } else if (!ukey->flow_exists) {
1645 ukey_delete(revalidator, ukey);
e79a6c83 1646 }
e1ec7dd4 1647 }
e4b79342
JS
1648
1649 if (n_ops) {
1650 push_dump_ops(revalidator, ops, n_ops);
1651 }
e1ec7dd4 1652}
e96a5c24
JS
1653
1654static void
1655revalidator_sweep(struct revalidator *revalidator)
1656{
1657 revalidator_sweep__(revalidator, false);
1658}
1659
1660static void
1661revalidator_purge(struct revalidator *revalidator)
1662{
1663 revalidator_sweep__(revalidator, true);
1664}
e22d52ee
EJ
1665\f
1666static void
1667upcall_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
1668 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
1669{
1670 struct ds ds = DS_EMPTY_INITIALIZER;
1671 struct udpif *udpif;
1672
1673 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
e79a6c83 1674 unsigned int flow_limit;
e22d52ee
EJ
1675 size_t i;
1676
b482e960 1677 atomic_read_relaxed(&udpif->flow_limit, &flow_limit);
e79a6c83 1678
e22d52ee 1679 ds_put_format(&ds, "%s:\n", dpif_name(udpif->dpif));
0e2a9f6f 1680 ds_put_format(&ds, "\tflows : (current %lu)"
e79a6c83
EJ
1681 " (avg %u) (max %u) (limit %u)\n", udpif_get_n_flows(udpif),
1682 udpif->avg_n_flows, udpif->max_n_flows, flow_limit);
e79a6c83
EJ
1683 ds_put_format(&ds, "\tdump duration : %lldms\n", udpif->dump_duration);
1684
e79a6c83
EJ
1685 ds_put_char(&ds, '\n');
1686 for (i = 0; i < n_revalidators; i++) {
1687 struct revalidator *revalidator = &udpif->revalidators[i];
1688
7d170098 1689 ovs_mutex_lock(&udpif->ukeys[i].mutex);
8ba0a522
BP
1690 ds_put_format(&ds, "\t%u: (keys %"PRIuSIZE")\n",
1691 revalidator->id, hmap_count(&udpif->ukeys[i].hmap));
7d170098 1692 ovs_mutex_unlock(&udpif->ukeys[i].mutex);
e79a6c83 1693 }
e22d52ee
EJ
1694 }
1695
1696 unixctl_command_reply(conn, ds_cstr(&ds));
1697 ds_destroy(&ds);
1698}
e79a6c83
EJ
1699
1700/* Disable using the megaflows.
1701 *
1702 * This command is only needed for advanced debugging, so it's not
1703 * documented in the man page. */
1704static void
1705upcall_unixctl_disable_megaflows(struct unixctl_conn *conn,
1706 int argc OVS_UNUSED,
1707 const char *argv[] OVS_UNUSED,
1708 void *aux OVS_UNUSED)
1709{
b482e960 1710 atomic_store_relaxed(&enable_megaflows, false);
1b5b5071 1711 udpif_flush_all_datapaths();
e79a6c83
EJ
1712 unixctl_command_reply(conn, "megaflows disabled");
1713}
1714
1715/* Re-enable using megaflows.
1716 *
1717 * This command is only needed for advanced debugging, so it's not
1718 * documented in the man page. */
1719static void
1720upcall_unixctl_enable_megaflows(struct unixctl_conn *conn,
1721 int argc OVS_UNUSED,
1722 const char *argv[] OVS_UNUSED,
1723 void *aux OVS_UNUSED)
1724{
b482e960 1725 atomic_store_relaxed(&enable_megaflows, true);
1b5b5071 1726 udpif_flush_all_datapaths();
e79a6c83
EJ
1727 unixctl_command_reply(conn, "megaflows enabled");
1728}
94b8c324
JS
1729
1730/* Set the flow limit.
1731 *
1732 * This command is only needed for advanced debugging, so it's not
1733 * documented in the man page. */
1734static void
1735upcall_unixctl_set_flow_limit(struct unixctl_conn *conn,
1736 int argc OVS_UNUSED,
1737 const char *argv[] OVS_UNUSED,
1738 void *aux OVS_UNUSED)
1739{
1740 struct ds ds = DS_EMPTY_INITIALIZER;
1741 struct udpif *udpif;
1742 unsigned int flow_limit = atoi(argv[1]);
1743
1744 LIST_FOR_EACH (udpif, list_node, &all_udpifs) {
b482e960 1745 atomic_store_relaxed(&udpif->flow_limit, flow_limit);
94b8c324
JS
1746 }
1747 ds_put_format(&ds, "set flow_limit to %u\n", flow_limit);
1748 unixctl_command_reply(conn, ds_cstr(&ds));
1749 ds_destroy(&ds);
1750}
27f57736
JS
1751
1752static void
1753upcall_unixctl_dump_wait(struct unixctl_conn *conn,
1754 int argc OVS_UNUSED,
1755 const char *argv[] OVS_UNUSED,
1756 void *aux OVS_UNUSED)
1757{
1758 if (list_is_singleton(&all_udpifs)) {
d72eff6c 1759 struct udpif *udpif = NULL;
27f57736
JS
1760 size_t len;
1761
1762 udpif = OBJECT_CONTAINING(list_front(&all_udpifs), udpif, list_node);
1763 len = (udpif->n_conns + 1) * sizeof *udpif->conns;
1764 udpif->conn_seq = seq_read(udpif->dump_seq);
1765 udpif->conns = xrealloc(udpif->conns, len);
1766 udpif->conns[udpif->n_conns++] = conn;
1767 } else {
1768 unixctl_command_reply_error(conn, "can't wait on multiple udpifs.");
1769 }
1770}