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