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