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