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