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