ofproto-dpif-upcall: Add memory usage stats.
[sliver-openvswitch.git] / ofproto / ofproto-dpif-upcall.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 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 "dynamic-string.h"
25 #include "dpif.h"
26 #include "fail-open.h"
27 #include "guarded-list.h"
28 #include "latch.h"
29 #include "seq.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 "packets.h"
36 #include "poll-loop.h"
37 #include "vlog.h"
38
39 #define MAX_QUEUE_LENGTH 512
40
41 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall);
42
43 COVERAGE_DEFINE(drop_queue_overflow);
44 COVERAGE_DEFINE(upcall_queue_overflow);
45 COVERAGE_DEFINE(fmb_queue_overflow);
46 COVERAGE_DEFINE(fmb_queue_revalidated);
47
48 /* A thread that processes each upcall handed to it by the dispatcher thread,
49  * forwards the upcall's packet, and then queues it to the main ofproto_dpif
50  * to possibly set up a kernel flow as a cache. */
51 struct handler {
52     struct udpif *udpif;               /* Parent udpif. */
53     pthread_t thread;                  /* Thread ID. */
54
55     struct ovs_mutex mutex;            /* Mutex guarding the following. */
56
57     /* Atomic queue of unprocessed upcalls. */
58     struct list upcalls OVS_GUARDED;
59     size_t n_upcalls OVS_GUARDED;
60
61     bool need_signal;                  /* Only changed by the dispatcher. */
62
63     pthread_cond_t wake_cond;          /* Wakes 'thread' while holding
64                                           'mutex'. */
65 };
66
67 /* An upcall handler for ofproto_dpif.
68  *
69  * udpif is implemented as a "dispatcher" thread that reads upcalls from the
70  * kernel.  It processes each upcall just enough to figure out its next
71  * destination.  For a "miss" upcall (MISS_UPCALL), this is one of several
72  * "handler" threads (see struct handler).  Other upcalls are queued to the
73  * main ofproto_dpif. */
74 struct udpif {
75     struct dpif *dpif;                 /* Datapath handle. */
76     struct dpif_backer *backer;        /* Opaque dpif_backer pointer. */
77
78     uint32_t secret;                   /* Random seed for upcall hash. */
79
80     pthread_t dispatcher;              /* Dispatcher thread ID. */
81
82     struct handler *handlers;          /* Upcall handlers. */
83     size_t n_handlers;
84
85     /* Queues to pass up to ofproto-dpif. */
86     struct guarded_list drop_keys; /* "struct drop key"s. */
87     struct guarded_list fmbs;      /* "struct flow_miss_batch"es. */
88
89     struct seq *wait_seq;
90     struct seq *reval_seq;
91
92     struct latch exit_latch; /* Tells child threads to exit. */
93 };
94
95 enum upcall_type {
96     BAD_UPCALL,                 /* Some kind of bug somewhere. */
97     MISS_UPCALL,                /* A flow miss.  */
98     SFLOW_UPCALL,               /* sFlow sample. */
99     FLOW_SAMPLE_UPCALL,         /* Per-flow sampling. */
100     IPFIX_UPCALL                /* Per-bridge sampling. */
101 };
102
103 struct upcall {
104     struct list list_node;          /* For queuing upcalls. */
105     struct flow_miss *flow_miss;    /* This upcall's flow_miss. */
106
107     /* Raw upcall plus data for keeping track of the memory backing it. */
108     struct dpif_upcall dpif_upcall; /* As returned by dpif_recv() */
109     struct ofpbuf upcall_buf;       /* Owns some data in 'dpif_upcall'. */
110     uint64_t upcall_stub[512 / 8];  /* Buffer to reduce need for malloc(). */
111 };
112
113 static void upcall_destroy(struct upcall *);
114
115 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
116
117 static void recv_upcalls(struct udpif *);
118 static void handle_upcalls(struct udpif *, struct list *upcalls);
119 static void miss_destroy(struct flow_miss *);
120 static void *udpif_dispatcher(void *);
121 static void *udpif_upcall_handler(void *);
122
123 struct udpif *
124 udpif_create(struct dpif_backer *backer, struct dpif *dpif)
125 {
126     struct udpif *udpif = xzalloc(sizeof *udpif);
127
128     udpif->dpif = dpif;
129     udpif->backer = backer;
130     udpif->secret = random_uint32();
131     udpif->wait_seq = seq_create();
132     udpif->reval_seq = seq_create();
133     latch_init(&udpif->exit_latch);
134     guarded_list_init(&udpif->drop_keys);
135     guarded_list_init(&udpif->fmbs);
136
137     return udpif;
138 }
139
140 void
141 udpif_destroy(struct udpif *udpif)
142 {
143     struct flow_miss_batch *fmb;
144     struct drop_key *drop_key;
145
146     udpif_recv_set(udpif, 0, false);
147
148     while ((drop_key = drop_key_next(udpif))) {
149         drop_key_destroy(drop_key);
150     }
151
152     while ((fmb = flow_miss_batch_next(udpif))) {
153         flow_miss_batch_destroy(fmb);
154     }
155
156     guarded_list_destroy(&udpif->drop_keys);
157     guarded_list_destroy(&udpif->fmbs);
158     latch_destroy(&udpif->exit_latch);
159     seq_destroy(udpif->wait_seq);
160     seq_destroy(udpif->reval_seq);
161     free(udpif);
162 }
163
164 /* Tells 'udpif' to begin or stop handling flow misses depending on the value
165  * of 'enable'.  'n_handlers' is the number of upcall_handler threads to
166  * create.  Passing 'n_handlers' as zero is equivalent to passing 'enable' as
167  * false. */
168 void
169 udpif_recv_set(struct udpif *udpif, size_t n_handlers, bool enable)
170 {
171     n_handlers = enable ? n_handlers : 0;
172
173     /* Stop the old threads (if any). */
174     if (udpif->handlers && udpif->n_handlers != n_handlers) {
175         size_t i;
176
177         latch_set(&udpif->exit_latch);
178
179         /* Wake the handlers so they can exit. */
180         for (i = 0; i < udpif->n_handlers; i++) {
181             struct handler *handler = &udpif->handlers[i];
182
183             ovs_mutex_lock(&handler->mutex);
184             xpthread_cond_signal(&handler->wake_cond);
185             ovs_mutex_unlock(&handler->mutex);
186         }
187
188         xpthread_join(udpif->dispatcher, NULL);
189         for (i = 0; i < udpif->n_handlers; i++) {
190             struct handler *handler = &udpif->handlers[i];
191             struct upcall *miss, *next;
192
193             xpthread_join(handler->thread, NULL);
194
195             ovs_mutex_lock(&handler->mutex);
196             LIST_FOR_EACH_SAFE (miss, next, list_node, &handler->upcalls) {
197                 list_remove(&miss->list_node);
198                 upcall_destroy(miss);
199             }
200             ovs_mutex_unlock(&handler->mutex);
201             ovs_mutex_destroy(&handler->mutex);
202
203             xpthread_cond_destroy(&handler->wake_cond);
204         }
205         latch_poll(&udpif->exit_latch);
206
207         free(udpif->handlers);
208         udpif->handlers = NULL;
209         udpif->n_handlers = 0;
210     }
211
212     /* Start new threads (if necessary). */
213     if (!udpif->handlers && n_handlers) {
214         size_t i;
215
216         udpif->n_handlers = n_handlers;
217         udpif->handlers = xzalloc(udpif->n_handlers * sizeof *udpif->handlers);
218         for (i = 0; i < udpif->n_handlers; i++) {
219             struct handler *handler = &udpif->handlers[i];
220
221             handler->udpif = udpif;
222             list_init(&handler->upcalls);
223             handler->need_signal = false;
224             xpthread_cond_init(&handler->wake_cond, NULL);
225             ovs_mutex_init(&handler->mutex);
226             xpthread_create(&handler->thread, NULL, udpif_upcall_handler,
227                             handler);
228         }
229         xpthread_create(&udpif->dispatcher, NULL, udpif_dispatcher, udpif);
230     }
231 }
232
233 void
234 udpif_wait(struct udpif *udpif)
235 {
236     uint64_t seq = seq_read(udpif->wait_seq);
237     if (!guarded_list_is_empty(&udpif->drop_keys) ||
238         !guarded_list_is_empty(&udpif->fmbs)) {
239         poll_immediate_wake();
240     } else {
241         seq_wait(udpif->wait_seq, seq);
242     }
243 }
244
245 /* Notifies 'udpif' that something changed which may render previous
246  * xlate_actions() results invalid. */
247 void
248 udpif_revalidate(struct udpif *udpif)
249 {
250     struct flow_miss_batch *fmb, *next_fmb;
251     struct list fmbs;
252
253     /* Since we remove each miss on revalidation, their statistics won't be
254      * accounted to the appropriate 'facet's in the upper layer.  In most
255      * cases, this is alright because we've already pushed the stats to the
256      * relevant rules. */
257     seq_change(udpif->reval_seq);
258
259     guarded_list_pop_all(&udpif->fmbs, &fmbs);
260     LIST_FOR_EACH_SAFE (fmb, next_fmb, list_node, &fmbs) {
261         list_remove(&fmb->list_node);
262         flow_miss_batch_destroy(fmb);
263     }
264
265     udpif_drop_key_clear(udpif);
266 }
267
268 void
269 udpif_get_memory_usage(struct udpif *udpif, struct simap *usage)
270 {
271     size_t i;
272
273     simap_increase(usage, "dispatchers", 1);
274     simap_increase(usage, "flow_dumpers", 1);
275
276     simap_increase(usage, "handlers", udpif->n_handlers);
277     for (i = 0; i < udpif->n_handlers; i++) {
278         struct handler *handler = &udpif->handlers[i];
279         ovs_mutex_lock(&handler->mutex);
280         simap_increase(usage, "handler upcalls",  handler->n_upcalls);
281         ovs_mutex_unlock(&handler->mutex);
282     }
283 }
284
285 /* Destroys and deallocates 'upcall'. */
286 static void
287 upcall_destroy(struct upcall *upcall)
288 {
289     if (upcall) {
290         ofpbuf_uninit(&upcall->upcall_buf);
291         free(upcall);
292     }
293 }
294
295 /* Retrieves the next batch of processed flow misses for 'udpif' to install.
296  * The caller is responsible for destroying it with flow_miss_batch_destroy().
297  */
298 struct flow_miss_batch *
299 flow_miss_batch_next(struct udpif *udpif)
300 {
301     int i;
302
303     for (i = 0; i < 50; i++) {
304         struct flow_miss_batch *next;
305         struct list *next_node;
306
307         next_node = guarded_list_pop_front(&udpif->fmbs);
308         if (!next_node) {
309             break;
310         }
311
312         next = CONTAINER_OF(next_node, struct flow_miss_batch, list_node);
313         if (next->reval_seq == seq_read(udpif->reval_seq)) {
314             return next;
315         }
316
317         flow_miss_batch_destroy(next);
318     }
319
320     return NULL;
321 }
322
323 /* Destroys and deallocates 'fmb'. */
324 void
325 flow_miss_batch_destroy(struct flow_miss_batch *fmb)
326 {
327     struct flow_miss *miss, *next;
328     struct upcall *upcall, *next_upcall;
329
330     if (!fmb) {
331         return;
332     }
333
334     HMAP_FOR_EACH_SAFE (miss, next, hmap_node, &fmb->misses) {
335         hmap_remove(&fmb->misses, &miss->hmap_node);
336         miss_destroy(miss);
337     }
338
339     LIST_FOR_EACH_SAFE (upcall, next_upcall, list_node, &fmb->upcalls) {
340         list_remove(&upcall->list_node);
341         upcall_destroy(upcall);
342     }
343
344     hmap_destroy(&fmb->misses);
345     free(fmb);
346 }
347
348 /* Retrieves the next drop key which ofproto-dpif needs to process.  The caller
349  * is responsible for destroying it with drop_key_destroy(). */
350 struct drop_key *
351 drop_key_next(struct udpif *udpif)
352 {
353     struct list *next = guarded_list_pop_front(&udpif->drop_keys);
354     return next ? CONTAINER_OF(next, struct drop_key, list_node) : NULL;
355 }
356
357 /* Destroys and deallocates 'drop_key'. */
358 void
359 drop_key_destroy(struct drop_key *drop_key)
360 {
361     if (drop_key) {
362         free(drop_key->key);
363         free(drop_key);
364     }
365 }
366
367 /* Clears all drop keys waiting to be processed by drop_key_next(). */
368 void
369 udpif_drop_key_clear(struct udpif *udpif)
370 {
371     struct drop_key *drop_key, *next;
372     struct list list;
373
374     guarded_list_pop_all(&udpif->drop_keys, &list);
375     LIST_FOR_EACH_SAFE (drop_key, next, list_node, &list) {
376         list_remove(&drop_key->list_node);
377         drop_key_destroy(drop_key);
378     }
379 }
380 \f
381 /* The dispatcher thread is responsible for receiving upcalls from the kernel,
382  * assigning them to a upcall_handler thread. */
383 static void *
384 udpif_dispatcher(void *arg)
385 {
386     struct udpif *udpif = arg;
387
388     set_subprogram_name("dispatcher");
389     while (!latch_is_set(&udpif->exit_latch)) {
390         recv_upcalls(udpif);
391         dpif_recv_wait(udpif->dpif);
392         latch_wait(&udpif->exit_latch);
393         poll_block();
394     }
395
396     return NULL;
397 }
398
399 /* The miss handler thread is responsible for processing miss upcalls retrieved
400  * by the dispatcher thread.  Once finished it passes the processed miss
401  * upcalls to ofproto-dpif where they're installed in the datapath. */
402 static void *
403 udpif_upcall_handler(void *arg)
404 {
405     struct handler *handler = arg;
406
407     set_subprogram_name("upcall_%u", ovsthread_id_self());
408     for (;;) {
409         struct list misses = LIST_INITIALIZER(&misses);
410         size_t i;
411
412         ovs_mutex_lock(&handler->mutex);
413
414         if (latch_is_set(&handler->udpif->exit_latch)) {
415             ovs_mutex_unlock(&handler->mutex);
416             return NULL;
417         }
418
419         if (!handler->n_upcalls) {
420             ovs_mutex_cond_wait(&handler->wake_cond, &handler->mutex);
421         }
422
423         for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
424             if (handler->n_upcalls) {
425                 handler->n_upcalls--;
426                 list_push_back(&misses, list_pop_front(&handler->upcalls));
427             } else {
428                 break;
429             }
430         }
431         ovs_mutex_unlock(&handler->mutex);
432
433         handle_upcalls(handler->udpif, &misses);
434
435         coverage_clear();
436     }
437 }
438 \f
439 static void
440 miss_destroy(struct flow_miss *miss)
441 {
442     xlate_out_uninit(&miss->xout);
443 }
444
445 static enum upcall_type
446 classify_upcall(const struct upcall *upcall)
447 {
448     const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
449     union user_action_cookie cookie;
450     size_t userdata_len;
451
452     /* First look at the upcall type. */
453     switch (dpif_upcall->type) {
454     case DPIF_UC_ACTION:
455         break;
456
457     case DPIF_UC_MISS:
458         return MISS_UPCALL;
459
460     case DPIF_N_UC_TYPES:
461     default:
462         VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
463                      dpif_upcall->type);
464         return BAD_UPCALL;
465     }
466
467     /* "action" upcalls need a closer look. */
468     if (!dpif_upcall->userdata) {
469         VLOG_WARN_RL(&rl, "action upcall missing cookie");
470         return BAD_UPCALL;
471     }
472     userdata_len = nl_attr_get_size(dpif_upcall->userdata);
473     if (userdata_len < sizeof cookie.type
474         || userdata_len > sizeof cookie) {
475         VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
476                      userdata_len);
477         return BAD_UPCALL;
478     }
479     memset(&cookie, 0, sizeof cookie);
480     memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
481     if (userdata_len == sizeof cookie.sflow
482         && cookie.type == USER_ACTION_COOKIE_SFLOW) {
483         return SFLOW_UPCALL;
484     } else if (userdata_len == sizeof cookie.slow_path
485                && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
486         return MISS_UPCALL;
487     } else if (userdata_len == sizeof cookie.flow_sample
488                && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
489         return FLOW_SAMPLE_UPCALL;
490     } else if (userdata_len == sizeof cookie.ipfix
491                && cookie.type == USER_ACTION_COOKIE_IPFIX) {
492         return IPFIX_UPCALL;
493     } else {
494         VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
495                      " and size %"PRIuSIZE, cookie.type, userdata_len);
496         return BAD_UPCALL;
497     }
498 }
499
500 static void
501 recv_upcalls(struct udpif *udpif)
502 {
503     int n;
504
505     for (;;) {
506         uint32_t hash = udpif->secret;
507         struct handler *handler;
508         struct upcall *upcall;
509         size_t n_bytes, left;
510         struct nlattr *nla;
511         int error;
512
513         upcall = xmalloc(sizeof *upcall);
514         ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
515                         sizeof upcall->upcall_stub);
516         error = dpif_recv(udpif->dpif, &upcall->dpif_upcall,
517                           &upcall->upcall_buf);
518         if (error) {
519             upcall_destroy(upcall);
520             break;
521         }
522
523         n_bytes = 0;
524         NL_ATTR_FOR_EACH (nla, left, upcall->dpif_upcall.key,
525                           upcall->dpif_upcall.key_len) {
526             enum ovs_key_attr type = nl_attr_type(nla);
527             if (type == OVS_KEY_ATTR_IN_PORT
528                 || type == OVS_KEY_ATTR_TCP
529                 || type == OVS_KEY_ATTR_UDP) {
530                 if (nl_attr_get_size(nla) == 4) {
531                     hash = mhash_add(hash, nl_attr_get_u32(nla));
532                     n_bytes += 4;
533                 } else {
534                     VLOG_WARN_RL(&rl,
535                                  "Netlink attribute with incorrect size.");
536                 }
537             }
538         }
539         hash =  mhash_finish(hash, n_bytes);
540
541         handler = &udpif->handlers[hash % udpif->n_handlers];
542
543         ovs_mutex_lock(&handler->mutex);
544         if (handler->n_upcalls < MAX_QUEUE_LENGTH) {
545             list_push_back(&handler->upcalls, &upcall->list_node);
546             if (handler->n_upcalls == 0) {
547                 handler->need_signal = true;
548             }
549             handler->n_upcalls++;
550             if (handler->need_signal &&
551                 handler->n_upcalls >= FLOW_MISS_MAX_BATCH) {
552                 handler->need_signal = false;
553                 xpthread_cond_signal(&handler->wake_cond);
554             }
555             ovs_mutex_unlock(&handler->mutex);
556             if (!VLOG_DROP_DBG(&rl)) {
557                 struct ds ds = DS_EMPTY_INITIALIZER;
558
559                 odp_flow_key_format(upcall->dpif_upcall.key,
560                                     upcall->dpif_upcall.key_len,
561                                     &ds);
562                 VLOG_DBG("dispatcher: enqueue (%s)", ds_cstr(&ds));
563                 ds_destroy(&ds);
564             }
565         } else {
566             ovs_mutex_unlock(&handler->mutex);
567             COVERAGE_INC(upcall_queue_overflow);
568             upcall_destroy(upcall);
569         }
570     }
571
572     for (n = 0; n < udpif->n_handlers; ++n) {
573         struct handler *handler = &udpif->handlers[n];
574
575         if (handler->need_signal) {
576             handler->need_signal = false;
577             ovs_mutex_lock(&handler->mutex);
578             xpthread_cond_signal(&handler->wake_cond);
579             ovs_mutex_unlock(&handler->mutex);
580         }
581     }
582 }
583
584 static struct flow_miss *
585 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
586                const struct flow *flow, uint32_t hash)
587 {
588     struct flow_miss *miss;
589
590     HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
591         if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
592             return miss;
593         }
594     }
595
596     return NULL;
597 }
598
599 static void
600 handle_upcalls(struct udpif *udpif, struct list *upcalls)
601 {
602     struct dpif_op *opsp[FLOW_MISS_MAX_BATCH];
603     struct dpif_op ops[FLOW_MISS_MAX_BATCH];
604     struct upcall *upcall, *next;
605     struct flow_miss_batch *fmb;
606     size_t n_misses, n_ops, i;
607     struct flow_miss *miss;
608     enum upcall_type type;
609     bool fail_open;
610
611     /* Extract the flow from each upcall.  Construct in fmb->misses a hash
612      * table that maps each unique flow to a 'struct flow_miss'.
613      *
614      * Most commonly there is a single packet per flow_miss, but there are
615      * several reasons why there might be more than one, e.g.:
616      *
617      *   - The dpif packet interface does not support TSO (or UFO, etc.), so a
618      *     large packet sent to userspace is split into a sequence of smaller
619      *     ones.
620      *
621      *   - A stream of quickly arriving packets in an established "slow-pathed"
622      *     flow.
623      *
624      *   - Rarely, a stream of quickly arriving packets in a flow not yet
625      *     established.  (This is rare because most protocols do not send
626      *     multiple back-to-back packets before receiving a reply from the
627      *     other end of the connection, which gives OVS a chance to set up a
628      *     datapath flow.)
629      */
630     fmb = xmalloc(sizeof *fmb);
631     fmb->reval_seq = seq_read(udpif->reval_seq);
632     hmap_init(&fmb->misses);
633     list_init(&fmb->upcalls);
634     n_misses = 0;
635     LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
636         struct dpif_upcall *dupcall = &upcall->dpif_upcall;
637         struct ofpbuf *packet = dupcall->packet;
638         struct flow_miss *miss = &fmb->miss_buf[n_misses];
639         struct flow_miss *existing_miss;
640         struct ofproto_dpif *ofproto;
641         struct dpif_sflow *sflow;
642         struct dpif_ipfix *ipfix;
643         odp_port_t odp_in_port;
644         struct flow flow;
645         int error;
646
647         error = xlate_receive(udpif->backer, packet, dupcall->key,
648                               dupcall->key_len, &flow, &miss->key_fitness,
649                               &ofproto, &ipfix, &sflow, NULL, &odp_in_port);
650         if (error) {
651             if (error == ENODEV) {
652                 struct drop_key *drop_key;
653
654                 /* Received packet on datapath port for which we couldn't
655                  * associate an ofproto.  This can happen if a port is removed
656                  * while traffic is being received.  Print a rate-limited
657                  * message in case it happens frequently.  Install a drop flow
658                  * so that future packets of the flow are inexpensively dropped
659                  * in the kernel. */
660                 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
661                              "port %"PRIu32, odp_in_port);
662
663                 drop_key = xmalloc(sizeof *drop_key);
664                 drop_key->key = xmemdup(dupcall->key, dupcall->key_len);
665                 drop_key->key_len = dupcall->key_len;
666
667                 if (guarded_list_push_back(&udpif->drop_keys,
668                                            &drop_key->list_node,
669                                            MAX_QUEUE_LENGTH)) {
670                     seq_change(udpif->wait_seq);
671                 } else {
672                     COVERAGE_INC(drop_queue_overflow);
673                     drop_key_destroy(drop_key);
674                 }
675             }
676             list_remove(&upcall->list_node);
677             upcall_destroy(upcall);
678             continue;
679         }
680
681         type = classify_upcall(upcall);
682         if (type == MISS_UPCALL) {
683             uint32_t hash;
684
685             flow_extract(packet, flow.skb_priority, flow.pkt_mark,
686                          &flow.tunnel, &flow.in_port, &miss->flow);
687
688             hash = flow_hash(&miss->flow, 0);
689             existing_miss = flow_miss_find(&fmb->misses, ofproto, &miss->flow,
690                                            hash);
691             if (!existing_miss) {
692                 hmap_insert(&fmb->misses, &miss->hmap_node, hash);
693                 miss->ofproto = ofproto;
694                 miss->key = dupcall->key;
695                 miss->key_len = dupcall->key_len;
696                 miss->upcall_type = dupcall->type;
697                 miss->stats.n_packets = 0;
698                 miss->stats.n_bytes = 0;
699                 miss->stats.used = time_msec();
700                 miss->stats.tcp_flags = 0;
701
702                 n_misses++;
703             } else {
704                 miss = existing_miss;
705             }
706             miss->stats.tcp_flags |= packet_get_tcp_flags(packet, &miss->flow);
707             miss->stats.n_bytes += packet->size;
708             miss->stats.n_packets++;
709
710             upcall->flow_miss = miss;
711             continue;
712         }
713
714         switch (type) {
715         case SFLOW_UPCALL:
716             if (sflow) {
717                 union user_action_cookie cookie;
718
719                 memset(&cookie, 0, sizeof cookie);
720                 memcpy(&cookie, nl_attr_get(dupcall->userdata),
721                        sizeof cookie.sflow);
722                 dpif_sflow_received(sflow, dupcall->packet, &flow, odp_in_port,
723                                     &cookie);
724             }
725             break;
726         case IPFIX_UPCALL:
727             if (ipfix) {
728                 dpif_ipfix_bridge_sample(ipfix, dupcall->packet, &flow);
729             }
730             break;
731         case FLOW_SAMPLE_UPCALL:
732             if (ipfix) {
733                 union user_action_cookie cookie;
734
735                 memset(&cookie, 0, sizeof cookie);
736                 memcpy(&cookie, nl_attr_get(dupcall->userdata),
737                        sizeof cookie.flow_sample);
738
739                 /* The flow reflects exactly the contents of the packet.
740                  * Sample the packet using it. */
741                 dpif_ipfix_flow_sample(ipfix, dupcall->packet, &flow,
742                                        cookie.flow_sample.collector_set_id,
743                                        cookie.flow_sample.probability,
744                                        cookie.flow_sample.obs_domain_id,
745                                        cookie.flow_sample.obs_point_id);
746             }
747             break;
748         case BAD_UPCALL:
749             break;
750         case MISS_UPCALL:
751             NOT_REACHED();
752         }
753
754         dpif_ipfix_unref(ipfix);
755         dpif_sflow_unref(sflow);
756
757         list_remove(&upcall->list_node);
758         upcall_destroy(upcall);
759     }
760
761     /* Initialize each 'struct flow_miss's ->xout.
762      *
763      * We do this per-flow_miss rather than per-packet because, most commonly,
764      * all the packets in a flow can use the same translation.
765      *
766      * We can't do this in the previous loop because we need the TCP flags for
767      * all the packets in each miss. */
768     fail_open = false;
769     HMAP_FOR_EACH (miss, hmap_node, &fmb->misses) {
770         struct xlate_in xin;
771
772         xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL,
773                       miss->stats.tcp_flags, NULL);
774         xin.may_learn = true;
775         xin.resubmit_stats = &miss->stats;
776         xlate_actions(&xin, &miss->xout);
777         fail_open = fail_open || miss->xout.fail_open;
778     }
779
780     /* Now handle the packets individually in order of arrival.  In the common
781      * case each packet of a miss can share the same actions, but slow-pathed
782      * packets need to be translated individually:
783      *
784      *   - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
785      *     processes received packets for these protocols.
786      *
787      *   - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
788      *     controller.
789      *
790      * The loop fills 'ops' with an array of operations to execute in the
791      * datapath. */
792     n_ops = 0;
793     LIST_FOR_EACH (upcall, list_node, upcalls) {
794         struct flow_miss *miss = upcall->flow_miss;
795         struct ofpbuf *packet = upcall->dpif_upcall.packet;
796
797         if (miss->xout.slow) {
798             struct xlate_in xin;
799
800             xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL, 0, packet);
801             xlate_actions_for_side_effects(&xin);
802         }
803
804         if (miss->xout.odp_actions.size) {
805             struct dpif_op *op;
806
807             if (miss->flow.in_port.ofp_port
808                 != vsp_realdev_to_vlandev(miss->ofproto,
809                                           miss->flow.in_port.ofp_port,
810                                           miss->flow.vlan_tci)) {
811                 /* This packet was received on a VLAN splinter port.  We
812                  * added a VLAN to the packet to make the packet resemble
813                  * the flow, but the actions were composed assuming that
814                  * the packet contained no VLAN.  So, we must remove the
815                  * VLAN header from the packet before trying to execute the
816                  * actions. */
817                 eth_pop_vlan(packet);
818             }
819
820             op = &ops[n_ops++];
821             op->type = DPIF_OP_EXECUTE;
822             op->u.execute.key = miss->key;
823             op->u.execute.key_len = miss->key_len;
824             op->u.execute.packet = packet;
825             op->u.execute.actions = miss->xout.odp_actions.data;
826             op->u.execute.actions_len = miss->xout.odp_actions.size;
827             op->u.execute.needs_help = (miss->xout.slow & SLOW_ACTION) != 0;
828         }
829     }
830
831     /* Execute batch. */
832     for (i = 0; i < n_ops; i++) {
833         opsp[i] = &ops[i];
834     }
835     dpif_operate(udpif->dpif, opsp, n_ops);
836
837     /* Special case for fail-open mode.
838      *
839      * If we are in fail-open mode, but we are connected to a controller too,
840      * then we should send the packet up to the controller in the hope that it
841      * will try to set up a flow and thereby allow us to exit fail-open.
842      *
843      * See the top-level comment in fail-open.c for more information. */
844     if (fail_open) {
845         LIST_FOR_EACH (upcall, list_node, upcalls) {
846             struct flow_miss *miss = upcall->flow_miss;
847             struct ofpbuf *packet = upcall->dpif_upcall.packet;
848             struct ofproto_packet_in *pin;
849
850             pin = xmalloc(sizeof *pin);
851             pin->up.packet = xmemdup(packet->data, packet->size);
852             pin->up.packet_len = packet->size;
853             pin->up.reason = OFPR_NO_MATCH;
854             pin->up.table_id = 0;
855             pin->up.cookie = OVS_BE64_MAX;
856             flow_get_metadata(&miss->flow, &pin->up.fmd);
857             pin->send_len = 0; /* Not used for flow table misses. */
858             pin->generated_by_table_miss = false;
859             ofproto_dpif_send_packet_in(miss->ofproto, pin);
860         }
861     }
862
863     list_move(&fmb->upcalls, upcalls);
864
865     if (fmb->reval_seq != seq_read(udpif->reval_seq)) {
866         COVERAGE_INC(fmb_queue_revalidated);
867         flow_miss_batch_destroy(fmb);
868     } else if (!guarded_list_push_back(&udpif->fmbs, &fmb->list_node,
869                                        MAX_QUEUE_LENGTH)) {
870         COVERAGE_INC(fmb_queue_overflow);
871         flow_miss_batch_destroy(fmb);
872     } else {
873         seq_change(udpif->wait_seq);
874     }
875 }