ofproto-dpif-upcall: Fix a memory leak.
[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 /* Retrieves 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 /* Retrieves 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     struct upcall *upcall, *next_upcall;
313
314     if (!fmb) {
315         return;
316     }
317
318     HMAP_FOR_EACH_SAFE (miss, next, hmap_node, &fmb->misses) {
319         hmap_remove(&fmb->misses, &miss->hmap_node);
320         miss_destroy(miss);
321     }
322
323     LIST_FOR_EACH_SAFE (upcall, next_upcall, list_node, &fmb->upcalls) {
324         list_remove(&upcall->list_node);
325         upcall_destroy(upcall);
326     }
327
328     hmap_destroy(&fmb->misses);
329     free(fmb);
330 }
331
332 /* Retrieves the next drop key which ofproto-dpif needs to process.  The caller
333  * is responsible for destroying it with drop_key_destroy(). */
334 struct drop_key *
335 drop_key_next(struct udpif *udpif)
336 {
337     struct list *next = guarded_list_pop_front(&udpif->drop_keys);
338     return next ? CONTAINER_OF(next, struct drop_key, list_node) : NULL;
339 }
340
341 /* Destroys and deallocates 'drop_key'. */
342 void
343 drop_key_destroy(struct drop_key *drop_key)
344 {
345     if (drop_key) {
346         free(drop_key->key);
347         free(drop_key);
348     }
349 }
350
351 /* Clears all drop keys waiting to be processed by drop_key_next(). */
352 void
353 udpif_drop_key_clear(struct udpif *udpif)
354 {
355     struct drop_key *drop_key, *next;
356     struct list list;
357
358     guarded_list_pop_all(&udpif->drop_keys, &list);
359     LIST_FOR_EACH_SAFE (drop_key, next, list_node, &list) {
360         list_remove(&drop_key->list_node);
361         drop_key_destroy(drop_key);
362     }
363 }
364 \f
365 /* The dispatcher thread is responsible for receving upcalls from the kernel,
366  * assigning the miss upcalls to a miss_handler thread, and assigning the more
367  * complex ones to ofproto-dpif directly. */
368 static void *
369 udpif_dispatcher(void *arg)
370 {
371     struct udpif *udpif = arg;
372
373     set_subprogram_name("dispatcher");
374     while (!latch_is_set(&udpif->exit_latch)) {
375         recv_upcalls(udpif);
376         dpif_recv_wait(udpif->dpif);
377         latch_wait(&udpif->exit_latch);
378         poll_block();
379     }
380
381     return NULL;
382 }
383
384 /* The miss handler thread is responsible for processing miss upcalls retrieved
385  * by the dispatcher thread.  Once finished it passes the processed miss
386  * upcalls to ofproto-dpif where they're installed in the datapath. */
387 static void *
388 udpif_miss_handler(void *arg)
389 {
390     struct handler *handler = arg;
391
392     set_subprogram_name("miss_handler");
393     for (;;) {
394         struct list misses = LIST_INITIALIZER(&misses);
395         size_t i;
396
397         ovs_mutex_lock(&handler->mutex);
398
399         if (latch_is_set(&handler->udpif->exit_latch)) {
400             ovs_mutex_unlock(&handler->mutex);
401             return NULL;
402         }
403
404         if (!handler->n_upcalls) {
405             ovs_mutex_cond_wait(&handler->wake_cond, &handler->mutex);
406         }
407
408         for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
409             if (handler->n_upcalls) {
410                 handler->n_upcalls--;
411                 list_push_back(&misses, list_pop_front(&handler->upcalls));
412             } else {
413                 break;
414             }
415         }
416         ovs_mutex_unlock(&handler->mutex);
417
418         handle_miss_upcalls(handler->udpif, &misses);
419     }
420 }
421 \f
422 static void
423 miss_destroy(struct flow_miss *miss)
424 {
425     xlate_out_uninit(&miss->xout);
426 }
427
428 static enum upcall_type
429 classify_upcall(const struct upcall *upcall)
430 {
431     const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
432     union user_action_cookie cookie;
433     size_t userdata_len;
434
435     /* First look at the upcall type. */
436     switch (dpif_upcall->type) {
437     case DPIF_UC_ACTION:
438         break;
439
440     case DPIF_UC_MISS:
441         return MISS_UPCALL;
442
443     case DPIF_N_UC_TYPES:
444     default:
445         VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
446                      dpif_upcall->type);
447         return BAD_UPCALL;
448     }
449
450     /* "action" upcalls need a closer look. */
451     if (!dpif_upcall->userdata) {
452         VLOG_WARN_RL(&rl, "action upcall missing cookie");
453         return BAD_UPCALL;
454     }
455     userdata_len = nl_attr_get_size(dpif_upcall->userdata);
456     if (userdata_len < sizeof cookie.type
457         || userdata_len > sizeof cookie) {
458         VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %zu",
459                      userdata_len);
460         return BAD_UPCALL;
461     }
462     memset(&cookie, 0, sizeof cookie);
463     memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
464     if (userdata_len == sizeof cookie.sflow
465         && cookie.type == USER_ACTION_COOKIE_SFLOW) {
466         return SFLOW_UPCALL;
467     } else if (userdata_len == sizeof cookie.slow_path
468                && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
469         return MISS_UPCALL;
470     } else if (userdata_len == sizeof cookie.flow_sample
471                && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
472         return FLOW_SAMPLE_UPCALL;
473     } else if (userdata_len == sizeof cookie.ipfix
474                && cookie.type == USER_ACTION_COOKIE_IPFIX) {
475         return IPFIX_UPCALL;
476     } else {
477         VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
478                      " and size %zu", cookie.type, userdata_len);
479         return BAD_UPCALL;
480     }
481 }
482
483 static void
484 recv_upcalls(struct udpif *udpif)
485 {
486     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
487     size_t n_udpif_new_upcalls = 0;
488     struct handler *handler;
489     int n;
490
491     for (;;) {
492         struct upcall *upcall;
493         int error;
494
495         upcall = xmalloc(sizeof *upcall);
496         ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
497                         sizeof upcall->upcall_stub);
498         error = dpif_recv(udpif->dpif, &upcall->dpif_upcall,
499                           &upcall->upcall_buf);
500         if (error) {
501             upcall_destroy(upcall);
502             break;
503         }
504
505         upcall->type = classify_upcall(upcall);
506         if (upcall->type == BAD_UPCALL) {
507             upcall_destroy(upcall);
508         } else if (upcall->type == MISS_UPCALL) {
509             struct dpif_upcall *dupcall = &upcall->dpif_upcall;
510             uint32_t hash = udpif->secret;
511             struct nlattr *nla;
512             size_t n_bytes, left;
513
514             n_bytes = 0;
515             NL_ATTR_FOR_EACH (nla, left, dupcall->key, dupcall->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                         ovs_be32 attr = nl_attr_get_be32(nla);
522                         hash = mhash_add(hash, (OVS_FORCE uint32_t) attr);
523                         n_bytes += 4;
524                     } else {
525                         VLOG_WARN("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                 handler->n_new_upcalls = ++handler->n_upcalls;
537
538                 if (handler->n_new_upcalls >= FLOW_MISS_MAX_BATCH) {
539                     xpthread_cond_signal(&handler->wake_cond);
540                 }
541                 ovs_mutex_unlock(&handler->mutex);
542                 if (!VLOG_DROP_DBG(&rl)) {
543                     struct ds ds = DS_EMPTY_INITIALIZER;
544
545                     odp_flow_key_format(upcall->dpif_upcall.key,
546                                         upcall->dpif_upcall.key_len,
547                                         &ds);
548                     VLOG_DBG("dispatcher: miss enqueue (%s)", ds_cstr(&ds));
549                     ds_destroy(&ds);
550                 }
551             } else {
552                 ovs_mutex_unlock(&handler->mutex);
553                 COVERAGE_INC(miss_queue_overflow);
554                 upcall_destroy(upcall);
555             }
556         } else {
557             size_t len;
558
559             len = guarded_list_push_back(&udpif->upcalls, &upcall->list_node,
560                                          MAX_QUEUE_LENGTH);
561             if (len > 0) {
562                 n_udpif_new_upcalls = len;
563                 if (n_udpif_new_upcalls >= FLOW_MISS_MAX_BATCH) {
564                     seq_change(udpif->wait_seq);
565                 }
566             } else {
567                 COVERAGE_INC(upcall_queue_overflow);
568                 upcall_destroy(upcall);
569             }
570         }
571     }
572     for (n = 0; n < udpif->n_handlers; ++n) {
573         handler = &udpif->handlers[n];
574         if (handler->n_new_upcalls) {
575             handler->n_new_upcalls = 0;
576             ovs_mutex_lock(&handler->mutex);
577             xpthread_cond_signal(&handler->wake_cond);
578             ovs_mutex_unlock(&handler->mutex);
579         }
580     }
581     if (n_udpif_new_upcalls) {
582         seq_change(udpif->wait_seq);
583     }
584 }
585
586 static struct flow_miss *
587 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
588                const struct flow *flow, uint32_t hash)
589 {
590     struct flow_miss *miss;
591
592     HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
593         if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
594             return miss;
595         }
596     }
597
598     return NULL;
599 }
600
601 static void
602 handle_miss_upcalls(struct udpif *udpif, struct list *upcalls)
603 {
604     struct dpif_op *opsp[FLOW_MISS_MAX_BATCH];
605     struct dpif_op ops[FLOW_MISS_MAX_BATCH];
606     struct upcall *upcall, *next;
607     struct flow_miss_batch *fmb;
608     size_t n_misses, n_ops, i;
609     struct flow_miss *miss;
610     unsigned int reval_seq;
611     bool fail_open;
612
613     /* Extract the flow from each upcall.  Construct in fmb->misses a hash
614      * table that maps each unique flow to a 'struct flow_miss'.
615      *
616      * Most commonly there is a single packet per flow_miss, but there are
617      * several reasons why there might be more than one, e.g.:
618      *
619      *   - The dpif packet interface does not support TSO (or UFO, etc.), so a
620      *     large packet sent to userspace is split into a sequence of smaller
621      *     ones.
622      *
623      *   - A stream of quickly arriving packets in an established "slow-pathed"
624      *     flow.
625      *
626      *   - Rarely, a stream of quickly arriving packets in a flow not yet
627      *     established.  (This is rare because most protocols do not send
628      *     multiple back-to-back packets before receiving a reply from the
629      *     other end of the connection, which gives OVS a chance to set up a
630      *     datapath flow.)
631      */
632     fmb = xmalloc(sizeof *fmb);
633     atomic_read(&udpif->reval_seq, &fmb->reval_seq);
634     hmap_init(&fmb->misses);
635     list_init(&fmb->upcalls);
636     n_misses = 0;
637     LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
638         struct dpif_upcall *dupcall = &upcall->dpif_upcall;
639         struct ofpbuf *packet = dupcall->packet;
640         struct flow_miss *miss = &fmb->miss_buf[n_misses];
641         struct flow_miss *existing_miss;
642         struct ofproto_dpif *ofproto;
643         odp_port_t odp_in_port;
644         struct flow flow;
645         int error;
646
647         error = xlate_receive(udpif->backer, packet, dupcall->key,
648                               dupcall->key_len, &flow, &miss->key_fitness,
649                               &ofproto, &odp_in_port);
650
651         if (!error) {
652             uint32_t hash;
653
654             flow_extract(packet, flow.skb_priority, flow.pkt_mark,
655                          &flow.tunnel, &flow.in_port, &miss->flow);
656
657             hash = flow_hash(&miss->flow, 0);
658             existing_miss = flow_miss_find(&fmb->misses, ofproto, &miss->flow,
659                                            hash);
660             if (!existing_miss) {
661                 hmap_insert(&fmb->misses, &miss->hmap_node, hash);
662                 miss->ofproto = ofproto;
663                 miss->key = dupcall->key;
664                 miss->key_len = dupcall->key_len;
665                 miss->upcall_type = dupcall->type;
666                 miss->stats.n_packets = 0;
667                 miss->stats.n_bytes = 0;
668                 miss->stats.used = time_msec();
669                 miss->stats.tcp_flags = 0;
670
671                 n_misses++;
672             } else {
673                 miss = existing_miss;
674             }
675             miss->stats.tcp_flags |= packet_get_tcp_flags(packet, &miss->flow);
676             miss->stats.n_bytes += packet->size;
677             miss->stats.n_packets++;
678
679             upcall->flow_miss = miss;
680         } else {
681             if (error == ENODEV) {
682                 struct drop_key *drop_key;
683
684                 /* Received packet on datapath port for which we couldn't
685                  * associate an ofproto.  This can happen if a port is removed
686                  * while traffic is being received.  Print a rate-limited
687                  * message in case it happens frequently.  Install a drop flow
688                  * so that future packets of the flow are inexpensively dropped
689                  * in the kernel. */
690                 VLOG_INFO_RL(&rl, "received packet on unassociated datapath "
691                              "port %"PRIu32, odp_in_port);
692
693                 drop_key = xmalloc(sizeof *drop_key);
694                 drop_key->key = xmemdup(dupcall->key, dupcall->key_len);
695                 drop_key->key_len = dupcall->key_len;
696
697                 if (guarded_list_push_back(&udpif->drop_keys,
698                                            &drop_key->list_node,
699                                            MAX_QUEUE_LENGTH)) {
700                     seq_change(udpif->wait_seq);
701                 } else {
702                     COVERAGE_INC(drop_queue_overflow);
703                     drop_key_destroy(drop_key);
704                 }
705             }
706             list_remove(&upcall->list_node);
707             upcall_destroy(upcall);
708         }
709     }
710
711     /* Initialize each 'struct flow_miss's ->xout.
712      *
713      * We do this per-flow_miss rather than per-packet because, most commonly,
714      * all the packets in a flow can use the same translation.
715      *
716      * We can't do this in the previous loop because we need the TCP flags for
717      * all the packets in each miss. */
718     fail_open = false;
719     HMAP_FOR_EACH (miss, hmap_node, &fmb->misses) {
720         struct flow_wildcards wc;
721         struct rule_dpif *rule;
722         struct xlate_in xin;
723
724         flow_wildcards_init_catchall(&wc);
725         rule_dpif_lookup(miss->ofproto, &miss->flow, &wc, &rule);
726         if (rule_dpif_fail_open(rule)) {
727             fail_open = true;
728         }
729         rule_dpif_credit_stats(rule, &miss->stats);
730         xlate_in_init(&xin, miss->ofproto, &miss->flow, rule,
731                       miss->stats.tcp_flags, NULL);
732         xin.may_learn = true;
733         xin.resubmit_stats = &miss->stats;
734         xlate_actions(&xin, &miss->xout);
735         flow_wildcards_or(&miss->xout.wc, &miss->xout.wc, &wc);
736         rule_dpif_unref(rule);
737     }
738
739     /* Now handle the packets individually in order of arrival.  In the common
740      * case each packet of a miss can share the same actions, but slow-pathed
741      * packets need to be translated individually:
742      *
743      *   - For SLOW_CFM, SLOW_LACP, SLOW_STP, and SLOW_BFD, translation is what
744      *     processes received packets for these protocols.
745      *
746      *   - For SLOW_CONTROLLER, translation sends the packet to the OpenFlow
747      *     controller.
748      *
749      * The loop fills 'ops' with an array of operations to execute in the
750      * datapath. */
751     n_ops = 0;
752     LIST_FOR_EACH (upcall, list_node, upcalls) {
753         struct flow_miss *miss = upcall->flow_miss;
754         struct ofpbuf *packet = upcall->dpif_upcall.packet;
755
756         if (miss->xout.slow) {
757             struct rule_dpif *rule;
758             struct xlate_in xin;
759
760             rule_dpif_lookup(miss->ofproto, &miss->flow, NULL, &rule);
761             xlate_in_init(&xin, miss->ofproto, &miss->flow, rule, 0, packet);
762             xlate_actions_for_side_effects(&xin);
763             rule_dpif_unref(rule);
764         }
765
766         if (miss->xout.odp_actions.size) {
767             struct dpif_op *op;
768
769             if (miss->flow.in_port.ofp_port
770                 != vsp_realdev_to_vlandev(miss->ofproto,
771                                           miss->flow.in_port.ofp_port,
772                                           miss->flow.vlan_tci)) {
773                 /* This packet was received on a VLAN splinter port.  We
774                  * added a VLAN to the packet to make the packet resemble
775                  * the flow, but the actions were composed assuming that
776                  * the packet contained no VLAN.  So, we must remove the
777                  * VLAN header from the packet before trying to execute the
778                  * actions. */
779                 eth_pop_vlan(packet);
780             }
781
782             op = &ops[n_ops++];
783             op->type = DPIF_OP_EXECUTE;
784             op->u.execute.key = miss->key;
785             op->u.execute.key_len = miss->key_len;
786             op->u.execute.packet = packet;
787             op->u.execute.actions = miss->xout.odp_actions.data;
788             op->u.execute.actions_len = miss->xout.odp_actions.size;
789         }
790     }
791
792     /* Execute batch. */
793     for (i = 0; i < n_ops; i++) {
794         opsp[i] = &ops[i];
795     }
796     dpif_operate(udpif->dpif, opsp, n_ops);
797
798     /* Special case for fail-open mode.
799      *
800      * If we are in fail-open mode, but we are connected to a controller too,
801      * then we should send the packet up to the controller in the hope that it
802      * will try to set up a flow and thereby allow us to exit fail-open.
803      *
804      * See the top-level comment in fail-open.c for more information. */
805     if (fail_open) {
806         LIST_FOR_EACH (upcall, list_node, upcalls) {
807             struct flow_miss *miss = upcall->flow_miss;
808             struct ofpbuf *packet = upcall->dpif_upcall.packet;
809             struct ofputil_packet_in *pin;
810
811             pin = xmalloc(sizeof *pin);
812             pin->packet = xmemdup(packet->data, packet->size);
813             pin->packet_len = packet->size;
814             pin->reason = OFPR_NO_MATCH;
815             pin->controller_id = 0;
816             pin->table_id = 0;
817             pin->cookie = 0;
818             pin->send_len = 0; /* Not used for flow table misses. */
819             flow_get_metadata(&miss->flow, &pin->fmd);
820             ofproto_dpif_send_packet_in(miss->ofproto, pin);
821         }
822     }
823
824     list_move(&fmb->upcalls, upcalls);
825
826     atomic_read(&udpif->reval_seq, &reval_seq);
827     if (reval_seq != fmb->reval_seq) {
828         COVERAGE_INC(fmb_queue_revalidated);
829         flow_miss_batch_destroy(fmb);
830     } else if (!guarded_list_push_back(&udpif->fmbs, &fmb->list_node,
831                                        MAX_QUEUE_LENGTH)) {
832         COVERAGE_INC(fmb_queue_overflow);
833         flow_miss_batch_destroy(fmb);
834     } else {
835         seq_change(udpif->wait_seq);
836     }
837 }