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