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