7ec7e8d4962a69f064ff8304d46ec334b3634468
[cascardo/ovs.git] / ofproto / ofproto-dpif-upcall.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 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 /* Waits for all ongoing upcall translations to complete.  This ensures that
220  * there are no transient references to any removed ofprotos (or other
221  * objects).  In particular, this should be called after an ofproto is removed
222  * (e.g. via xlate_remove_ofproto()) but before it is destroyed. */
223 void
224 udpif_synchronize(struct udpif *udpif)
225 {
226     /* This is stronger than necessary.  It would be sufficient to ensure
227      * (somehow) that each handler and revalidator thread had passed through
228      * its main loop once. */
229     size_t n_handlers = udpif->n_handlers;
230     if (n_handlers) {
231         udpif_recv_set(udpif, 0, false);
232         udpif_recv_set(udpif, n_handlers, true);
233     }
234 }
235
236 void
237 udpif_wait(struct udpif *udpif)
238 {
239     uint64_t seq = seq_read(udpif->wait_seq);
240     if (!guarded_list_is_empty(&udpif->drop_keys) ||
241         !guarded_list_is_empty(&udpif->upcalls) ||
242         !guarded_list_is_empty(&udpif->fmbs)) {
243         poll_immediate_wake();
244     } else {
245         seq_wait(udpif->wait_seq, seq);
246     }
247 }
248
249 /* Notifies 'udpif' that something changed which may render previous
250  * xlate_actions() results invalid. */
251 void
252 udpif_revalidate(struct udpif *udpif)
253 {
254     struct flow_miss_batch *fmb, *next_fmb;
255     unsigned int junk;
256     struct list fmbs;
257
258     /* Since we remove each miss on revalidation, their statistics won't be
259      * accounted to the appropriate 'facet's in the upper layer.  In most
260      * cases, this is alright because we've already pushed the stats to the
261      * relevant rules.  However, NetFlow requires absolute packet counts on
262      * 'facet's which could now be incorrect. */
263     atomic_add(&udpif->reval_seq, 1, &junk);
264
265     guarded_list_pop_all(&udpif->fmbs, &fmbs);
266     LIST_FOR_EACH_SAFE (fmb, next_fmb, list_node, &fmbs) {
267         list_remove(&fmb->list_node);
268         flow_miss_batch_destroy(fmb);
269     }
270
271     udpif_drop_key_clear(udpif);
272 }
273
274 /* Retreives the next upcall which ofproto-dpif is responsible for handling.
275  * The caller is responsible for destroying the returned upcall with
276  * upcall_destroy(). */
277 struct upcall *
278 upcall_next(struct udpif *udpif)
279 {
280     struct list *next = guarded_list_pop_front(&udpif->upcalls);
281     return next ? CONTAINER_OF(next, struct upcall, list_node) : NULL;
282 }
283
284 /* Destroys and deallocates 'upcall'. */
285 void
286 upcall_destroy(struct upcall *upcall)
287 {
288     if (upcall) {
289         ofpbuf_uninit(&upcall->upcall_buf);
290         free(upcall);
291     }
292 }
293
294 /* Retreives the next batch of processed flow misses for 'udpif' to install.
295  * The caller is responsible for destroying it with flow_miss_batch_destroy().
296  */
297 struct flow_miss_batch *
298 flow_miss_batch_next(struct udpif *udpif)
299 {
300     int i;
301
302     for (i = 0; i < 50; i++) {
303         struct flow_miss_batch *next;
304         unsigned int reval_seq;
305         struct list *next_node;
306
307         next_node = guarded_list_pop_front(&udpif->fmbs);
308         if (!next_node) {
309             break;
310         }
311
312         next = CONTAINER_OF(next_node, struct flow_miss_batch, list_node);
313         atomic_read(&udpif->reval_seq, &reval_seq);
314         if (next->reval_seq == reval_seq) {
315             return next;
316         }
317
318         flow_miss_batch_destroy(next);
319     }
320
321     return NULL;
322 }
323
324 /* Destroys and deallocates 'fmb'. */
325 void
326 flow_miss_batch_destroy(struct flow_miss_batch *fmb)
327 {
328     struct flow_miss *miss, *next;
329
330     if (!fmb) {
331         return;
332     }
333
334     HMAP_FOR_EACH_SAFE (miss, next, hmap_node, &fmb->misses) {
335         hmap_remove(&fmb->misses, &miss->hmap_node);
336         miss_destroy(miss);
337     }
338
339     hmap_destroy(&fmb->misses);
340     free(fmb);
341 }
342
343 /* Retreives the next drop key which ofproto-dpif needs to process.  The caller
344  * is responsible for destroying it with drop_key_destroy(). */
345 struct drop_key *
346 drop_key_next(struct udpif *udpif)
347 {
348     struct list *next = guarded_list_pop_front(&udpif->drop_keys);
349     return next ? CONTAINER_OF(next, struct drop_key, list_node) : NULL;
350 }
351
352 /* Destorys and deallocates 'drop_key'. */
353 void
354 drop_key_destroy(struct drop_key *drop_key)
355 {
356     if (drop_key) {
357         free(drop_key->key);
358         free(drop_key);
359     }
360 }
361
362 /* Clears all drop keys waiting to be processed by drop_key_next(). */
363 void
364 udpif_drop_key_clear(struct udpif *udpif)
365 {
366     struct drop_key *drop_key, *next;
367     struct list list;
368
369     guarded_list_pop_all(&udpif->drop_keys, &list);
370     LIST_FOR_EACH_SAFE (drop_key, next, list_node, &list) {
371         list_remove(&drop_key->list_node);
372         drop_key_destroy(drop_key);
373     }
374 }
375 \f
376 /* The dispatcher thread is responsible for receving upcalls from the kernel,
377  * assigning the miss upcalls to a miss_handler thread, and assigning the more
378  * complex ones to ofproto-dpif directly. */
379 static void *
380 udpif_dispatcher(void *arg)
381 {
382     struct udpif *udpif = arg;
383
384     set_subprogram_name("dispatcher");
385     while (!latch_is_set(&udpif->exit_latch)) {
386         recv_upcalls(udpif);
387         dpif_recv_wait(udpif->dpif);
388         latch_wait(&udpif->exit_latch);
389         poll_block();
390     }
391
392     return NULL;
393 }
394
395 /* The miss handler thread is responsible for processing miss upcalls retreived
396  * by the dispatcher thread.  Once finished it passes the processed miss
397  * upcalls to ofproto-dpif where they're installed in the datapath. */
398 static void *
399 udpif_miss_handler(void *arg)
400 {
401     struct list misses = LIST_INITIALIZER(&misses);
402     struct handler *handler = arg;
403
404     set_subprogram_name("miss_handler");
405     for (;;) {
406         size_t i;
407
408         ovs_mutex_lock(&handler->mutex);
409
410         if (latch_is_set(&handler->udpif->exit_latch)) {
411             ovs_mutex_unlock(&handler->mutex);
412             return NULL;
413         }
414
415         if (!handler->n_upcalls) {
416             ovs_mutex_cond_wait(&handler->wake_cond, &handler->mutex);
417         }
418
419         for (i = 0; i < FLOW_MISS_MAX_BATCH; i++) {
420             if (handler->n_upcalls) {
421                 handler->n_upcalls--;
422                 list_push_back(&misses, list_pop_front(&handler->upcalls));
423             } else {
424                 break;
425             }
426         }
427         ovs_mutex_unlock(&handler->mutex);
428
429         handle_miss_upcalls(handler->udpif, &misses);
430     }
431 }
432 \f
433 static void
434 miss_destroy(struct flow_miss *miss)
435 {
436     struct upcall *upcall, *next;
437
438     LIST_FOR_EACH_SAFE (upcall, next, list_node, &miss->upcalls) {
439         list_remove(&upcall->list_node);
440         upcall_destroy(upcall);
441     }
442     xlate_out_uninit(&miss->xout);
443 }
444
445 static enum upcall_type
446 classify_upcall(const struct upcall *upcall)
447 {
448     const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
449     union user_action_cookie cookie;
450     size_t userdata_len;
451
452     /* First look at the upcall type. */
453     switch (dpif_upcall->type) {
454     case DPIF_UC_ACTION:
455         break;
456
457     case DPIF_UC_MISS:
458         return MISS_UPCALL;
459
460     case DPIF_N_UC_TYPES:
461     default:
462         VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
463                      dpif_upcall->type);
464         return BAD_UPCALL;
465     }
466
467     /* "action" upcalls need a closer look. */
468     if (!dpif_upcall->userdata) {
469         VLOG_WARN_RL(&rl, "action upcall missing cookie");
470         return BAD_UPCALL;
471     }
472     userdata_len = nl_attr_get_size(dpif_upcall->userdata);
473     if (userdata_len < sizeof cookie.type
474         || userdata_len > sizeof cookie) {
475         VLOG_WARN_RL(&rl, "action upcall cookie has unexpected size %zu",
476                      userdata_len);
477         return BAD_UPCALL;
478     }
479     memset(&cookie, 0, sizeof cookie);
480     memcpy(&cookie, nl_attr_get(dpif_upcall->userdata), userdata_len);
481     if (userdata_len == MAX(8, sizeof cookie.sflow)
482         && cookie.type == USER_ACTION_COOKIE_SFLOW) {
483         return SFLOW_UPCALL;
484     } else if (userdata_len == MAX(8, sizeof cookie.slow_path)
485                && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
486         return MISS_UPCALL;
487     } else if (userdata_len == MAX(8, sizeof cookie.flow_sample)
488                && cookie.type == USER_ACTION_COOKIE_FLOW_SAMPLE) {
489         return FLOW_SAMPLE_UPCALL;
490     } else if (userdata_len == MAX(8, sizeof cookie.ipfix)
491                && cookie.type == USER_ACTION_COOKIE_IPFIX) {
492         return IPFIX_UPCALL;
493     } else {
494         VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
495                      " and size %zu", cookie.type, userdata_len);
496         return BAD_UPCALL;
497     }
498 }
499
500 static void
501 recv_upcalls(struct udpif *udpif)
502 {
503     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
504     size_t n_udpif_new_upcalls = 0;
505     struct handler *handler;
506     int n;
507
508     for (;;) {
509         struct upcall *upcall;
510         int error;
511
512         upcall = xmalloc(sizeof *upcall);
513         ofpbuf_use_stub(&upcall->upcall_buf, upcall->upcall_stub,
514                         sizeof upcall->upcall_stub);
515         error = dpif_recv(udpif->dpif, &upcall->dpif_upcall,
516                           &upcall->upcall_buf);
517         if (error) {
518             upcall_destroy(upcall);
519             break;
520         }
521
522         upcall->type = classify_upcall(upcall);
523         if (upcall->type == BAD_UPCALL) {
524             upcall_destroy(upcall);
525         } else if (upcall->type == MISS_UPCALL) {
526             struct dpif_upcall *dupcall = &upcall->dpif_upcall;
527             uint32_t hash = udpif->secret;
528             struct nlattr *nla;
529             size_t n_bytes, left;
530
531             n_bytes = 0;
532             NL_ATTR_FOR_EACH (nla, left, dupcall->key, dupcall->key_len) {
533                 enum ovs_key_attr type = nl_attr_type(nla);
534                 if (type == OVS_KEY_ATTR_IN_PORT
535                     || type == OVS_KEY_ATTR_TCP
536                     || type == OVS_KEY_ATTR_UDP) {
537                     if (nl_attr_get_size(nla) == 4) {
538                         ovs_be32 attr = nl_attr_get_be32(nla);
539                         hash = mhash_add(hash, (OVS_FORCE uint32_t) attr);
540                         n_bytes += 4;
541                     } else {
542                         VLOG_WARN("Netlink attribute with incorrect size.");
543                     }
544                 }
545             }
546             hash =  mhash_finish(hash, n_bytes);
547
548             handler = &udpif->handlers[hash % udpif->n_handlers];
549
550             ovs_mutex_lock(&handler->mutex);
551             if (handler->n_upcalls < MAX_QUEUE_LENGTH) {
552                 list_push_back(&handler->upcalls, &upcall->list_node);
553                 handler->n_new_upcalls = ++handler->n_upcalls;
554
555                 if (handler->n_new_upcalls >= FLOW_MISS_MAX_BATCH) {
556                     xpthread_cond_signal(&handler->wake_cond);
557                 }
558                 ovs_mutex_unlock(&handler->mutex);
559                 if (!VLOG_DROP_DBG(&rl)) {
560                     struct ds ds = DS_EMPTY_INITIALIZER;
561
562                     odp_flow_key_format(upcall->dpif_upcall.key,
563                                         upcall->dpif_upcall.key_len,
564                                         &ds);
565                     VLOG_DBG("dispatcher: miss enqueue (%s)", ds_cstr(&ds));
566                     ds_destroy(&ds);
567                 }
568             } else {
569                 ovs_mutex_unlock(&handler->mutex);
570                 COVERAGE_INC(miss_queue_overflow);
571                 upcall_destroy(upcall);
572             }
573         } else {
574             size_t len;
575
576             len = guarded_list_push_back(&udpif->upcalls, &upcall->list_node,
577                                          MAX_QUEUE_LENGTH);
578             if (len > 0) {
579                 n_udpif_new_upcalls = len;
580                 if (n_udpif_new_upcalls >= FLOW_MISS_MAX_BATCH) {
581                     seq_change(udpif->wait_seq);
582                 }
583             } else {
584                 COVERAGE_INC(upcall_queue_overflow);
585                 upcall_destroy(upcall);
586             }
587         }
588     }
589     for (n = 0; n < udpif->n_handlers; ++n) {
590         handler = &udpif->handlers[n];
591         if (handler->n_new_upcalls) {
592             handler->n_new_upcalls = 0;
593             ovs_mutex_lock(&handler->mutex);
594             xpthread_cond_signal(&handler->wake_cond);
595             ovs_mutex_unlock(&handler->mutex);
596         }
597     }
598     if (n_udpif_new_upcalls) {
599         seq_change(udpif->wait_seq);
600     }
601 }
602
603 static struct flow_miss *
604 flow_miss_find(struct hmap *todo, const struct ofproto_dpif *ofproto,
605                const struct flow *flow, uint32_t hash)
606 {
607     struct flow_miss *miss;
608
609     HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
610         if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
611             return miss;
612         }
613     }
614
615     return NULL;
616 }
617
618 /* Executes flow miss 'miss'.  May add any required datapath operations
619  * to 'ops', incrementing '*n_ops' for each new op. */
620 static void
621 execute_flow_miss(struct flow_miss *miss, struct dpif_op *ops, size_t *n_ops)
622 {
623     struct ofproto_dpif *ofproto = miss->ofproto;
624     struct ofpbuf *packet;
625     struct xlate_in xin;
626
627     memset(&miss->stats, 0, sizeof miss->stats);
628     miss->stats.used = time_msec();
629     LIST_FOR_EACH (packet, list_node, &miss->packets) {
630         miss->stats.tcp_flags |= packet_get_tcp_flags(packet, &miss->flow);
631         miss->stats.n_bytes += packet->size;
632         miss->stats.n_packets++;
633     }
634
635     xlate_in_init(&xin, ofproto, &miss->flow, NULL, miss->stats.tcp_flags,
636                   NULL);
637     xin.may_learn = true;
638     xin.resubmit_stats = &miss->stats;
639     xlate_actions(&xin, &miss->xout);
640
641     if (miss->xout.fail_open) {
642         LIST_FOR_EACH (packet, list_node, &miss->packets) {
643             struct ofputil_packet_in *pin;
644
645             /* Extra-special case for fail-open mode.
646              *
647              * We are in fail-open mode and the packet matched the fail-open
648              * rule, but we are connected to a controller too.  We should send
649              * the packet up to the controller in the hope that it will try to
650              * set up a flow and thereby allow us to exit fail-open.
651              *
652              * See the top-level comment in fail-open.c for more information. */
653             pin = xmalloc(sizeof(*pin));
654             pin->packet = xmemdup(packet->data, packet->size);
655             pin->packet_len = packet->size;
656             pin->reason = OFPR_NO_MATCH;
657             pin->controller_id = 0;
658             pin->table_id = 0;
659             pin->cookie = 0;
660             pin->send_len = 0; /* Not used for flow table misses. */
661             flow_get_metadata(&miss->flow, &pin->fmd);
662             ofproto_dpif_send_packet_in(ofproto, pin);
663         }
664     }
665
666     if (miss->xout.slow) {
667         LIST_FOR_EACH (packet, list_node, &miss->packets) {
668             struct xlate_in xin;
669
670             xlate_in_init(&xin, miss->ofproto, &miss->flow, NULL, 0, packet);
671             xlate_actions_for_side_effects(&xin);
672         }
673     }
674
675     if (miss->xout.odp_actions.size) {
676         LIST_FOR_EACH (packet, list_node, &miss->packets) {
677             struct dpif_op *op = &ops[*n_ops];
678             struct dpif_execute *execute = &op->u.execute;
679
680             if (miss->flow.in_port.ofp_port
681                 != vsp_realdev_to_vlandev(miss->ofproto,
682                                           miss->flow.in_port.ofp_port,
683                                           miss->flow.vlan_tci)) {
684                 /* This packet was received on a VLAN splinter port.  We
685                  * added a VLAN to the packet to make the packet resemble
686                  * the flow, but the actions were composed assuming that
687                  * the packet contained no VLAN.  So, we must remove the
688                  * VLAN header from the packet before trying to execute the
689                  * actions. */
690                 eth_pop_vlan(packet);
691             }
692
693             op->type = DPIF_OP_EXECUTE;
694             execute->key = miss->key;
695             execute->key_len = miss->key_len;
696             execute->packet = packet;
697             execute->actions = miss->xout.odp_actions.data;
698             execute->actions_len = miss->xout.odp_actions.size;
699
700             (*n_ops)++;
701         }
702     }
703 }
704
705 static void
706 handle_miss_upcalls(struct udpif *udpif, struct list *upcalls)
707 {
708     struct dpif_op *opsp[FLOW_MISS_MAX_BATCH];
709     struct dpif_op ops[FLOW_MISS_MAX_BATCH];
710     struct upcall *upcall, *next;
711     struct flow_miss_batch *fmb;
712     size_t n_upcalls, n_ops, i;
713     struct flow_miss *miss;
714     unsigned int reval_seq;
715
716     /* Construct the to-do list.
717      *
718      * This just amounts to extracting the flow from each packet and sticking
719      * the packets that have the same flow in the same "flow_miss" structure so
720      * that we can process them together. */
721     fmb = xmalloc(sizeof *fmb);
722     atomic_read(&udpif->reval_seq, &fmb->reval_seq);
723     hmap_init(&fmb->misses);
724     n_upcalls = 0;
725     LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
726         struct dpif_upcall *dupcall = &upcall->dpif_upcall;
727         struct flow_miss *miss = &fmb->miss_buf[n_upcalls];
728         struct flow_miss *existing_miss;
729         struct ofproto_dpif *ofproto;
730         odp_port_t odp_in_port;
731         struct flow flow;
732         uint32_t hash;
733         int error;
734
735         error = xlate_receive(udpif->backer, dupcall->packet, dupcall->key,
736                               dupcall->key_len, &flow, &miss->key_fitness,
737                               &ofproto, &odp_in_port);
738
739         if (error == ENODEV) {
740             struct drop_key *drop_key;
741
742             /* Received packet on datapath port for which we couldn't
743              * associate an ofproto.  This can happen if a port is removed
744              * while traffic is being received.  Print a rate-limited message
745              * in case it happens frequently.  Install a drop flow so
746              * that future packets of the flow are inexpensively dropped
747              * in the kernel. */
748             VLOG_INFO_RL(&rl, "received packet on unassociated datapath port "
749                               "%"PRIu32, odp_in_port);
750
751             drop_key = xmalloc(sizeof *drop_key);
752             drop_key->key = xmemdup(dupcall->key, dupcall->key_len);
753             drop_key->key_len = dupcall->key_len;
754
755             if (guarded_list_push_back(&udpif->drop_keys, &drop_key->list_node,
756                                        MAX_QUEUE_LENGTH)) {
757                 seq_change(udpif->wait_seq);
758             } else {
759                 COVERAGE_INC(drop_queue_overflow);
760                 drop_key_destroy(drop_key);
761             }
762             continue;
763         } else if (error) {
764             continue;
765         }
766
767         flow_extract(dupcall->packet, flow.skb_priority, flow.pkt_mark,
768                      &flow.tunnel, &flow.in_port, &miss->flow);
769
770         /* Add other packets to a to-do list. */
771         hash = flow_hash(&miss->flow, 0);
772         existing_miss = flow_miss_find(&fmb->misses, ofproto, &miss->flow, hash);
773         if (!existing_miss) {
774             hmap_insert(&fmb->misses, &miss->hmap_node, hash);
775             miss->ofproto = ofproto;
776             miss->key = dupcall->key;
777             miss->key_len = dupcall->key_len;
778             miss->upcall_type = dupcall->type;
779             list_init(&miss->packets);
780             list_init(&miss->upcalls);
781
782             n_upcalls++;
783         } else {
784             miss = existing_miss;
785         }
786         list_push_back(&miss->packets, &dupcall->packet->list_node);
787
788         list_remove(&upcall->list_node);
789         list_push_back(&miss->upcalls, &upcall->list_node);
790     }
791
792     LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
793         list_remove(&upcall->list_node);
794         upcall_destroy(upcall);
795     }
796
797     /* Process each element in the to-do list, constructing the set of
798      * operations to batch. */
799     n_ops = 0;
800     HMAP_FOR_EACH (miss, hmap_node, &fmb->misses) {
801         execute_flow_miss(miss, ops, &n_ops);
802     }
803     ovs_assert(n_ops <= ARRAY_SIZE(ops));
804
805     /* Execute batch. */
806     for (i = 0; i < n_ops; i++) {
807         opsp[i] = &ops[i];
808     }
809     dpif_operate(udpif->dpif, opsp, n_ops);
810
811     atomic_read(&udpif->reval_seq, &reval_seq);
812     if (reval_seq != fmb->reval_seq) {
813         COVERAGE_INC(fmb_queue_revalidated);
814         flow_miss_batch_destroy(fmb);
815     } else if (!guarded_list_push_back(&udpif->fmbs, &fmb->list_node,
816                                        MAX_QUEUE_LENGTH)) {
817         COVERAGE_INC(fmb_queue_overflow);
818         flow_miss_batch_destroy(fmb);
819     } else {
820         seq_change(udpif->wait_seq);
821     }
822 }