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