Avoid printf type modifiers not supported by MSVC C runtime library.
[cascardo/ovs.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     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_%u", ovsthread_id_self());
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         coverage_clear();
426     }
427 }
428 \f
429 static void
430 miss_destroy(struct flow_miss *miss)
431 {
432     xlate_out_uninit(&miss->xout);
433 }
434
435 static enum upcall_type
436 classify_upcall(const struct upcall *upcall)
437 {
438     const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
439     union user_action_cookie cookie;
440     size_t userdata_len;
441
442     /* First look at the upcall type. */
443     switch (dpif_upcall->type) {
444     case DPIF_UC_ACTION:
445         break;
446
447     case DPIF_UC_MISS:
448         return MISS_UPCALL;
449
450     case DPIF_N_UC_TYPES:
451     default:
452         VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
453                      dpif_upcall->type);
454         return BAD_UPCALL;
455     }
456
457     /* "action" upcalls need a closer look. */
458     if (!dpif_upcall->userdata) {
459         VLOG_WARN_RL(&rl, "action upcall missing cookie");
460         return BAD_UPCALL;
461     }
462     userdata_len = nl_attr_get_size(dpif_upcall->userdata);
463     if (userdata_len < sizeof cookie.type
464         || userdata_len > sizeof cookie) {
465         VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %"PRIuSIZE,
466                      userdata_len);
467         return BAD_UPCALL;
468     }
469     memset(&cookie, 0, sizeof cookie);
470     memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
471     if (userdata_len == sizeof cookie.sflow
472         && cookie.type == USER_ACTION_COOKIE_SFLOW) {
473         return SFLOW_UPCALL;
474     } else if (userdata_len == sizeof cookie.slow_path
475                && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
476         return MISS_UPCALL;
477     } else if (userdata_len == sizeof cookie.flow_sample
478                && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
479         return FLOW_SAMPLE_UPCALL;
480     } else if (userdata_len == sizeof cookie.ipfix
481                && cookie.type == USER_ACTION_COOKIE_IPFIX) {
482         return IPFIX_UPCALL;
483     } else {
484         VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
485                      " and size %"PRIuSIZE, cookie.type, userdata_len);
486         return BAD_UPCALL;
487     }
488 }
489
490 static void
491 recv_upcalls(struct udpif *udpif)
492 {
493     int n;
494
495     for (;;) {
496         uint32_t hash = udpif->secret;
497         struct handler *handler;
498         struct upcall *upcall;
499         size_t n_bytes, left;
500         struct nlattr *nla;
501         int error;
502
503         upcall = xmalloc(sizeof *upcall);
504         ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
505                         sizeof upcall->upcall_stub);
506         error = dpif_recv(udpif->dpif, &upcall->dpif_upcall,
507                           &upcall->upcall_buf);
508         if (error) {
509             upcall_destroy(upcall);
510             break;
511         }
512
513         n_bytes = 0;
514         NL_ATTR_FOR_EACH (nla, left, upcall->dpif_upcall.key,
515                           upcall->dpif_upcall.key_len) {
516             enum ovs_key_attr type = nl_attr_type(nla);
517             if (type == OVS_KEY_ATTR_IN_PORT
518                 || type == OVS_KEY_ATTR_TCP
519                 || type == OVS_KEY_ATTR_UDP) {
520                 if (nl_attr_get_size(nla) == 4) {
521                     hash = mhash_add(hash, nl_attr_get_u32(nla));
522                     n_bytes += 4;
523                 } else {
524                     VLOG_WARN_RL(&rl,
525                                  "Netlink attribute with incorrect size.");
526                 }
527             }
528         }
529         hash =  mhash_finish(hash, n_bytes);
530
531         handler = &udpif->handlers[hash % udpif->n_handlers];
532
533         ovs_mutex_lock(&handler->mutex);
534         if (handler->n_upcalls < MAX_QUEUE_LENGTH) {
535             list_push_back(&handler->upcalls, &upcall->list_node);
536             if (handler->n_upcalls == 0) {
537                 handler->need_signal = true;
538             }
539             handler->n_upcalls++;
540             if (handler->need_signal &&
541                 handler->n_upcalls >= FLOW_MISS_MAX_BATCH) {
542                 handler->need_signal = false;
543                 xpthread_cond_signal(&handler->wake_cond);
544             }
545             ovs_mutex_unlock(&handler->mutex);
546             if (!VLOG_DROP_DBG(&rl)) {
547                 struct ds ds = DS_EMPTY_INITIALIZER;
548
549                 odp_flow_key_format(upcall->dpif_upcall.key,
550                                     upcall->dpif_upcall.key_len,
551                                     &ds);
552                 VLOG_DBG("dispatcher: enqueue (%s)", ds_cstr(&ds));
553                 ds_destroy(&ds);
554             }
555         } else {
556             ovs_mutex_unlock(&handler->mutex);
557             COVERAGE_INC(upcall_queue_overflow);
558             upcall_destroy(upcall);
559         }
560     }
561
562     for (n = 0; n < udpif->n_handlers; ++n) {
563         struct handler *handler = &udpif->handlers[n];
564
565         if (handler->need_signal) {
566             handler->need_signal = false;
567             ovs_mutex_lock(&handler->mutex);
568             xpthread_cond_signal(&handler->wake_cond);
569             ovs_mutex_unlock(&handler->mutex);
570         }
571     }
572 }
573
574 static struct flow_miss *
575 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
576                const struct flow *flow, uint32_t hash)
577 {
578     struct flow_miss *miss;
579
580     HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
581         if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
582             return miss;
583         }
584     }
585
586     return NULL;
587 }
588
589 static void
590 handle_upcalls(struct udpif *udpif, struct list *upcalls)
591 {
592     struct dpif_op *opsp[FLOW_MISS_MAX_BATCH];
593     struct dpif_op ops[FLOW_MISS_MAX_BATCH];
594     struct upcall *upcall, *next;
595     struct flow_miss_batch *fmb;
596     size_t n_misses, n_ops, i;
597     struct flow_miss *miss;
598     unsigned int reval_seq;
599     enum upcall_type type;
600     bool fail_open;
601
602     /* Extract the flow from each upcall.  Construct in fmb->misses a hash
603      * table that maps each unique flow to a 'struct flow_miss'.
604      *
605      * Most commonly there is a single packet per flow_miss, but there are
606      * several reasons why there might be more than one, e.g.:
607      *
608      *   - The dpif packet interface does not support TSO (or UFO, etc.), so a
609      *     large packet sent to userspace is split into a sequence of smaller
610      *     ones.
611      *
612      *   - A stream of quickly arriving packets in an established "slow-pathed"
613      *     flow.
614      *
615      *   - Rarely, a stream of quickly arriving packets in a flow not yet
616      *     established.  (This is rare because most protocols do not send
617      *     multiple back-to-back packets before receiving a reply from the
618      *     other end of the connection, which gives OVS a chance to set up a
619      *     datapath flow.)
620      */
621     fmb = xmalloc(sizeof *fmb);
622     atomic_read(&udpif->reval_seq, &fmb->reval_seq);
623     hmap_init(&fmb->misses);
624     list_init(&fmb->upcalls);
625     n_misses = 0;
626     LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
627         struct dpif_upcall *dupcall = &upcall->dpif_upcall;
628         struct ofpbuf *packet = dupcall->packet;
629         struct flow_miss *miss = &fmb->miss_buf[n_misses];
630         struct flow_miss *existing_miss;
631         struct ofproto_dpif *ofproto;
632         struct dpif_sflow *sflow;
633         struct dpif_ipfix *ipfix;
634         odp_port_t odp_in_port;
635         struct flow flow;
636         int error;
637
638         error = xlate_receive(udpif->backer, packet, dupcall->key,
639                               dupcall->key_len, &flow, &miss->key_fitness,
640                               &ofproto, &odp_in_port);
641         if (error) {
642             if (error == ENODEV) {
643                 struct drop_key *drop_key;
644
645                 /* Received packet on datapath port for which we couldn't
646                  * associate an ofproto.  This can happen if a port is removed
647                  * while traffic is being received.  Print a rate-limited
648                  * message in case it happens frequently.  Install a drop flow
649                  * so that future packets of the flow are inexpensively dropped
650                  * in the kernel. */
651                 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
652                              "port %"PRIu32, odp_in_port);
653
654                 drop_key = xmalloc(sizeof *drop_key);
655                 drop_key->key = xmemdup(dupcall->key, dupcall->key_len);
656                 drop_key->key_len = dupcall->key_len;
657
658                 if (guarded_list_push_back(&udpif->drop_keys,
659                                            &drop_key->list_node,
660                                            MAX_QUEUE_LENGTH)) {
661                     seq_change(udpif->wait_seq);
662                 } else {
663                     COVERAGE_INC(drop_queue_overflow);
664                     drop_key_destroy(drop_key);
665                 }
666             }
667             list_remove(&upcall->list_node);
668             upcall_destroy(upcall);
669             continue;
670         }
671
672         type = classify_upcall(upcall);
673         if (type == MISS_UPCALL) {
674             uint32_t hash;
675
676             flow_extract(packet, flow.skb_priority, flow.pkt_mark,
677                          &flow.tunnel, &flow.in_port, &miss->flow);
678
679             hash = flow_hash(&miss->flow, 0);
680             existing_miss = flow_miss_find(&fmb->misses, ofproto, &miss->flow,
681                                            hash);
682             if (!existing_miss) {
683                 hmap_insert(&fmb->misses, &miss->hmap_node, hash);
684                 miss->ofproto = ofproto;
685                 miss->key = dupcall->key;
686                 miss->key_len = dupcall->key_len;
687                 miss->upcall_type = dupcall->type;
688                 miss->stats.n_packets = 0;
689                 miss->stats.n_bytes = 0;
690                 miss->stats.used = time_msec();
691                 miss->stats.tcp_flags = 0;
692
693                 n_misses++;
694             } else {
695                 miss = existing_miss;
696             }
697             miss->stats.tcp_flags |= packet_get_tcp_flags(packet, &miss->flow);
698             miss->stats.n_bytes += packet->size;
699             miss->stats.n_packets++;
700
701             upcall->flow_miss = miss;
702             continue;
703         }
704
705         switch (type) {
706         case SFLOW_UPCALL:
707             sflow = xlate_get_sflow(ofproto);
708             if (sflow) {
709                 union user_action_cookie cookie;
710
711                 memset(&cookie, 0, sizeof cookie);
712                 memcpy(&cookie, nl_attr_get(dupcall->userdata),
713                        sizeof cookie.sflow);
714                 dpif_sflow_received(sflow, dupcall->packet, &flow, odp_in_port,
715                                     &cookie);
716                 dpif_sflow_unref(sflow);
717             }
718             break;
719         case IPFIX_UPCALL:
720             ipfix = xlate_get_ipfix(ofproto);
721             if (ipfix) {
722                 dpif_ipfix_bridge_sample(ipfix, dupcall->packet, &flow);
723                 dpif_ipfix_unref(ipfix);
724             }
725             break;
726         case FLOW_SAMPLE_UPCALL:
727             ipfix = xlate_get_ipfix(ofproto);
728             if (ipfix) {
729                 union user_action_cookie cookie;
730
731                 memset(&cookie, 0, sizeof cookie);
732                 memcpy(&cookie, nl_attr_get(dupcall->userdata),
733                        sizeof cookie.flow_sample);
734
735                 /* The flow reflects exactly the contents of the packet.
736                  * Sample the packet using it. */
737                 dpif_ipfix_flow_sample(ipfix, dupcall->packet, &flow,
738                                        cookie.flow_sample.collector_set_id,
739                                        cookie.flow_sample.probability,
740                                        cookie.flow_sample.obs_domain_id,
741                                        cookie.flow_sample.obs_point_id);
742                 dpif_ipfix_unref(ipfix);
743             }
744             break;
745         case BAD_UPCALL:
746             break;
747         case MISS_UPCALL:
748             NOT_REACHED();
749         }
750
751         list_remove(&upcall->list_node);
752         upcall_destroy(upcall);
753     }
754
755     /* Initialize each 'struct flow_miss's ->xout.
756      *
757      * We do this per-flow_miss rather than per-packet because, most commonly,
758      * all the packets in a flow can use the same translation.
759      *
760      * We can't do this in the previous loop because we need the TCP flags for
761      * all the packets in each miss. */
762     fail_open = false;
763     HMAP_FOR_EACH (miss, hmap_node, &fmb->misses) {
764         struct xlate_in xin;
765
766         xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL,
767                       miss->stats.tcp_flags, NULL);
768         xin.may_learn = true;
769         xin.resubmit_stats = &miss->stats;
770         xlate_actions(&xin, &miss->xout);
771         fail_open = fail_open || miss->xout.fail_open;
772     }
773
774     /* Now handle the packets individually in order of arrival.  In the common
775      * case each packet of a miss can share the same actions, but slow-pathed
776      * packets need to be translated individually:
777      *
778      *   - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
779      *     processes received packets for these protocols.
780      *
781      *   - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
782      *     controller.
783      *
784      * The loop fills 'ops' with an array of operations to execute in the
785      * datapath. */
786     n_ops = 0;
787     LIST_FOR_EACH (upcall, list_node, upcalls) {
788         struct flow_miss *miss = upcall->flow_miss;
789         struct ofpbuf *packet = upcall->dpif_upcall.packet;
790
791         if (miss->xout.slow) {
792             struct xlate_in xin;
793
794             xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL, 0, packet);
795             xlate_actions_for_side_effects(&xin);
796         }
797
798         if (miss->xout.odp_actions.size) {
799             struct dpif_op *op;
800
801             if (miss->flow.in_port.ofp_port
802                 != vsp_realdev_to_vlandev(miss->ofproto,
803                                           miss->flow.in_port.ofp_port,
804                                           miss->flow.vlan_tci)) {
805                 /* This packet was received on a VLAN splinter port.  We
806                  * added a VLAN to the packet to make the packet resemble
807                  * the flow, but the actions were composed assuming that
808                  * the packet contained no VLAN.  So, we must remove the
809                  * VLAN header from the packet before trying to execute the
810                  * actions. */
811                 eth_pop_vlan(packet);
812             }
813
814             op = &ops[n_ops++];
815             op->type = DPIF_OP_EXECUTE;
816             op->u.execute.key = miss->key;
817             op->u.execute.key_len = miss->key_len;
818             op->u.execute.packet = packet;
819             op->u.execute.actions = miss->xout.odp_actions.data;
820             op->u.execute.actions_len = miss->xout.odp_actions.size;
821             op->u.execute.needs_help = (miss->xout.slow & SLOW_ACTION) != 0;
822         }
823     }
824
825     /* Execute batch. */
826     for (i = 0; i < n_ops; i++) {
827         opsp[i] = &ops[i];
828     }
829     dpif_operate(udpif->dpif, opsp, n_ops);
830
831     /* Special case for fail-open mode.
832      *
833      * If we are in fail-open mode, but we are connected to a controller too,
834      * then we should send the packet up to the controller in the hope that it
835      * will try to set up a flow and thereby allow us to exit fail-open.
836      *
837      * See the top-level comment in fail-open.c for more information. */
838     if (fail_open) {
839         LIST_FOR_EACH (upcall, list_node, upcalls) {
840             struct flow_miss *miss = upcall->flow_miss;
841             struct ofpbuf *packet = upcall->dpif_upcall.packet;
842             struct ofproto_packet_in *pin;
843
844             pin = xmalloc(sizeof *pin);
845             pin->up.packet = xmemdup(packet->data, packet->size);
846             pin->up.packet_len = packet->size;
847             pin->up.reason = OFPR_NO_MATCH;
848             pin->up.table_id = 0;
849             pin->up.cookie = OVS_BE64_MAX;
850             flow_get_metadata(&miss->flow, &pin->up.fmd);
851             pin->send_len = 0; /* Not used for flow table misses. */
852             pin->generated_by_table_miss = false;
853             ofproto_dpif_send_packet_in(miss->ofproto, pin);
854         }
855     }
856
857     list_move(&fmb->upcalls, upcalls);
858
859     atomic_read(&udpif->reval_seq, &reval_seq);
860     if (reval_seq != fmb->reval_seq) {
861         COVERAGE_INC(fmb_queue_revalidated);
862         flow_miss_batch_destroy(fmb);
863     } else if (!guarded_list_push_back(&udpif->fmbs, &fmb->list_node,
864                                        MAX_QUEUE_LENGTH)) {
865         COVERAGE_INC(fmb_queue_overflow);
866         flow_miss_batch_destroy(fmb);
867     } else {
868         seq_change(udpif->wait_seq);
869     }
870 }