1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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. */
16 #include "ofproto-dpif-upcall.h"
23 #include "dynamic-string.h"
25 #include "fail-open.h"
31 #include "ofproto-dpif.h"
33 #include "poll-loop.h"
36 #define MAX_QUEUE_LENGTH 512
38 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_upcall);
40 COVERAGE_DEFINE(upcall_queue_overflow);
41 COVERAGE_DEFINE(drop_queue_overflow);
42 COVERAGE_DEFINE(miss_queue_overflow);
43 COVERAGE_DEFINE(fmb_queue_overflow);
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. */
49 struct udpif *udpif; /* Parent udpif. */
50 pthread_t thread; /* Thread ID. */
52 struct ovs_mutex mutex; /* Mutex guarding the following. */
54 /* Atomic queue of unprocessed miss upcalls. */
55 struct list upcalls OVS_GUARDED;
56 size_t n_upcalls OVS_GUARDED;
58 size_t n_new_upcalls; /* Only changed by the dispatcher. */
60 pthread_cond_t wake_cond; /* Wakes 'thread' while holding
64 /* An upcall handler for ofproto_dpif.
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. */
72 struct dpif *dpif; /* Datapath handle. */
73 struct dpif_backer *backer; /* Opaque dpif_backer pointer. */
75 uint32_t secret; /* Random seed for upcall hash. */
77 pthread_t dispatcher; /* Dispatcher thread ID. */
79 struct handler *handlers; /* Miss handlers. */
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;
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;
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;
97 /* Number of times udpif_revalidate() has been called. */
98 atomic_uint reval_seq;
100 struct seq *wait_seq;
103 struct latch exit_latch; /* Tells child threads to exit. */
106 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
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 *);
115 udpif_create(struct dpif_backer *backer, struct dpif *dpif)
117 struct udpif *udpif = xzalloc(sizeof *udpif);
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);
136 udpif_destroy(struct udpif *udpif)
138 struct flow_miss_batch *fmb;
139 struct drop_key *drop_key;
140 struct upcall *upcall;
142 udpif_recv_set(udpif, 0, false);
144 while ((drop_key = drop_key_next(udpif))) {
145 drop_key_destroy(drop_key);
148 while ((upcall = upcall_next(udpif))) {
149 upcall_destroy(upcall);
152 while ((fmb = flow_miss_batch_next(udpif))) {
153 flow_miss_batch_destroy(fmb);
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);
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. */
168 udpif_recv_set(struct udpif *udpif, size_t n_handlers, bool enable)
170 n_handlers = enable ? n_handlers : 0;
171 n_handlers = MIN(n_handlers, 64);
173 /* Stop the old threads (if any). */
174 if (udpif->handlers && udpif->n_handlers != n_handlers) {
177 latch_set(&udpif->exit_latch);
179 /* Wake the handlers so they can exit. */
180 for (i = 0; i < udpif->n_handlers; i++) {
181 struct handler *handler = &udpif->handlers[i];
183 ovs_mutex_lock(&handler->mutex);
184 xpthread_cond_signal(&handler->wake_cond);
185 ovs_mutex_unlock(&handler->mutex);
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;
193 xpthread_join(handler->thread, NULL);
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);
200 ovs_mutex_unlock(&handler->mutex);
201 ovs_mutex_destroy(&handler->mutex);
203 xpthread_cond_destroy(&handler->wake_cond);
205 latch_poll(&udpif->exit_latch);
207 free(udpif->handlers);
208 udpif->handlers = NULL;
209 udpif->n_handlers = 0;
212 /* Start new threads (if necessary). */
213 if (!udpif->handlers && n_handlers) {
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];
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);
227 xpthread_create(&udpif->dispatcher, NULL, udpif_dispatcher, udpif);
232 udpif_run(struct udpif *udpif)
234 udpif->last_seq = seq_read(udpif->wait_seq);
238 udpif_wait(struct udpif *udpif)
240 ovs_mutex_lock(&udpif->drop_key_mutex);
241 if (udpif->n_drop_keys) {
242 poll_immediate_wake();
244 ovs_mutex_unlock(&udpif->drop_key_mutex);
246 ovs_mutex_lock(&udpif->upcall_mutex);
247 if (udpif->n_upcalls) {
248 poll_immediate_wake();
250 ovs_mutex_unlock(&udpif->upcall_mutex);
252 ovs_mutex_lock(&udpif->fmb_mutex);
254 poll_immediate_wake();
256 ovs_mutex_unlock(&udpif->fmb_mutex);
258 seq_wait(udpif->wait_seq, udpif->last_seq);
261 /* Notifies 'udpif' that something changed which may render previous
262 * xlate_actions() results invalid. */
264 udpif_revalidate(struct udpif *udpif)
266 struct flow_miss_batch *fmb, *next_fmb;
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);
281 ovs_mutex_unlock(&udpif->fmb_mutex);
282 udpif_drop_key_clear(udpif);
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(). */
289 upcall_next(struct udpif *udpif)
291 struct upcall *next = NULL;
293 ovs_mutex_lock(&udpif->upcall_mutex);
294 if (udpif->n_upcalls) {
296 next = CONTAINER_OF(list_pop_front(&udpif->upcalls), struct upcall,
299 ovs_mutex_unlock(&udpif->upcall_mutex);
303 /* Destroys and deallocates 'upcall'. */
305 upcall_destroy(struct upcall *upcall)
308 ofpbuf_uninit(&upcall->upcall_buf);
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().
316 struct flow_miss_batch *
317 flow_miss_batch_next(struct udpif *udpif)
319 struct flow_miss_batch *next = NULL;
321 ovs_mutex_lock(&udpif->fmb_mutex);
324 next = CONTAINER_OF(list_pop_front(&udpif->fmbs),
325 struct flow_miss_batch, list_node);
327 ovs_mutex_unlock(&udpif->fmb_mutex);
331 /* Destroys and deallocates 'fmb'. */
333 flow_miss_batch_destroy(struct flow_miss_batch *fmb)
335 struct flow_miss *miss, *next;
341 HMAP_FOR_EACH_SAFE (miss, next, hmap_node, &fmb->misses) {
342 hmap_remove(&fmb->misses, &miss->hmap_node);
346 hmap_destroy(&fmb->misses);
350 /* Retreives the next drop key which ofproto-dpif needs to process. The caller
351 * is responsible for destroying it with drop_key_destroy(). */
353 drop_key_next(struct udpif *udpif)
355 struct drop_key *next = NULL;
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,
363 ovs_mutex_unlock(&udpif->drop_key_mutex);
367 /* Destorys and deallocates 'drop_key'. */
369 drop_key_destroy(struct drop_key *drop_key)
377 /* Clears all drop keys waiting to be processed by drop_key_next(). */
379 udpif_drop_key_clear(struct udpif *udpif)
381 struct drop_key *drop_key, *next;
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--;
389 ovs_mutex_unlock(&udpif->drop_key_mutex);
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. */
396 udpif_dispatcher(void *arg)
398 struct udpif *udpif = arg;
400 set_subprogram_name("dispatcher");
401 while (!latch_is_set(&udpif->exit_latch)) {
403 dpif_recv_wait(udpif->dpif);
404 latch_wait(&udpif->exit_latch);
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. */
415 udpif_miss_handler(void *arg)
417 struct list misses = LIST_INITIALIZER(&misses);
418 struct handler *handler = arg;
420 set_subprogram_name("miss_handler");
424 ovs_mutex_lock(&handler->mutex);
426 if (latch_is_set(&handler->udpif->exit_latch)) {
427 ovs_mutex_unlock(&handler->mutex);
431 if (!handler->n_upcalls) {
432 ovs_mutex_cond_wait(&handler->wake_cond, &handler->mutex);
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));
443 ovs_mutex_unlock(&handler->mutex);
445 handle_miss_upcalls(handler->udpif, &misses);
450 miss_destroy(struct flow_miss *miss)
452 struct upcall *upcall, *next;
454 LIST_FOR_EACH_SAFE (upcall, next, list_node, &miss->upcalls) {
455 list_remove(&upcall->list_node);
456 upcall_destroy(upcall);
458 xlate_out_uninit(&miss->xout);
461 static enum upcall_type
462 classify_upcall(const struct upcall *upcall)
464 const struct dpif_upcall *dpif_upcall = &upcall->dpif_upcall;
465 union user_action_cookie cookie;
468 /* First look at the upcall type. */
469 switch (dpif_upcall->type) {
476 case DPIF_N_UC_TYPES:
478 VLOG_WARN_RL(&rl, "upcall has unexpected type %"PRIu32,
483 /* "action" upcalls need a closer look. */
484 if (!dpif_upcall->userdata) {
485 VLOG_WARN_RL(&rl, "action upcall missing cookie");
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",
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) {
500 } else if (userdata_len == sizeof cookie.slow_path
501 && cookie.type == USER_ACTION_COOKIE_SLOW_PATH) {
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) {
510 VLOG_WARN_RL(&rl, "invalid user cookie of type %"PRIu16
511 " and size %zu", cookie.type, userdata_len);
517 recv_upcalls(struct udpif *udpif)
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;
525 struct upcall *upcall;
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);
534 upcall_destroy(upcall);
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;
545 size_t n_bytes, left;
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);
558 VLOG_WARN("Netlink attribute with incorrect size.");
562 hash = mhash_finish(hash, n_bytes);
564 handler = &udpif->handlers[hash % udpif->n_handlers];
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;
571 if (handler->n_new_upcalls >= FLOW_MISS_MAX_BATCH) {
572 xpthread_cond_signal(&handler->wake_cond);
574 ovs_mutex_unlock(&handler->mutex);
575 if (!VLOG_DROP_DBG(&rl)) {
576 struct ds ds = DS_EMPTY_INITIALIZER;
578 odp_flow_key_format(upcall->dpif_upcall.key,
579 upcall->dpif_upcall.key_len,
581 VLOG_DBG("dispatcher: miss enqueue (%s)", ds_cstr(&ds));
585 ovs_mutex_unlock(&handler->mutex);
586 COVERAGE_INC(miss_queue_overflow);
587 upcall_destroy(upcall);
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);
596 if (n_udpif_new_upcalls >= FLOW_MISS_MAX_BATCH) {
597 seq_change(udpif->wait_seq);
600 ovs_mutex_unlock(&udpif->upcall_mutex);
601 COVERAGE_INC(upcall_queue_overflow);
602 upcall_destroy(upcall);
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);
615 if (n_udpif_new_upcalls) {
616 seq_change(udpif->wait_seq);
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)
624 struct flow_miss *miss;
626 HMAP_FOR_EACH_WITH_HASH (miss, hmap_node, hash, todo) {
627 if (miss->ofproto == ofproto && flow_equal(&miss->flow, flow)) {
635 /* Executes flow miss 'miss'. May add any required datapath operations
636 * to 'ops', incrementing '*n_ops' for each new op. */
638 execute_flow_miss(struct flow_miss *miss, struct dpif_op *ops, size_t *n_ops)
640 struct ofproto_dpif *ofproto = miss->ofproto;
641 struct flow_wildcards wc;
642 struct rule_dpif *rule;
643 struct ofpbuf *packet;
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++;
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,
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);
664 if (rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
665 LIST_FOR_EACH (packet, list_node, &miss->packets) {
666 struct ofputil_packet_in *pin;
668 /* Extra-special case for fail-open mode.
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.
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;
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);
689 if (miss->xout.slow) {
690 LIST_FOR_EACH (packet, list_node, &miss->packets) {
693 xlate_in_init(&xin, miss->ofproto, &miss->flow, rule, 0, packet);
694 xlate_actions_for_side_effects(&xin);
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;
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
714 eth_pop_vlan(packet);
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;
730 handle_miss_upcalls(struct udpif *udpif, struct list *upcalls)
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;
740 atomic_read(&udpif->reval_seq, &old_reval_seq);
742 /* Construct the to-do list.
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);
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;
760 error = xlate_receive(udpif->backer, dupcall->packet, dupcall->key,
761 dupcall->key_len, &flow, &miss->key_fitness,
762 &ofproto, &odp_in_port);
764 if (error == ENODEV) {
765 struct drop_key *drop_key;
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
773 VLOG_INFO_RL(&rl, "received packet on unassociated datapath port "
774 "%"PRIu32, odp_in_port);
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;
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);
787 ovs_mutex_unlock(&udpif->drop_key_mutex);
788 COVERAGE_INC(drop_queue_overflow);
789 drop_key_destroy(drop_key);
796 flow_extract(dupcall->packet, flow.skb_priority, flow.pkt_mark,
797 &flow.tunnel, &flow.in_port, &miss->flow);
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);
813 miss = existing_miss;
815 list_push_back(&miss->packets, &dupcall->packet->list_node);
817 list_remove(&upcall->list_node);
818 list_push_back(&miss->upcalls, &upcall->list_node);
821 LIST_FOR_EACH_SAFE (upcall, next, list_node, upcalls) {
822 list_remove(&upcall->list_node);
823 upcall_destroy(upcall);
826 /* Process each element in the to-do list, constructing the set of
827 * operations to batch. */
829 HMAP_FOR_EACH (miss, hmap_node, &fmb->misses) {
830 execute_flow_miss(miss, ops, &n_ops);
832 ovs_assert(n_ops <= ARRAY_SIZE(ops));
835 for (i = 0; i < n_ops; i++) {
838 dpif_operate(udpif->dpif, opsp, n_ops);
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) {
849 list_push_back(&udpif->fmbs, &fmb->list_node);
850 ovs_mutex_unlock(&udpif->fmb_mutex);
851 seq_change(udpif->wait_seq);
853 COVERAGE_INC(fmb_queue_overflow);
854 ovs_mutex_unlock(&udpif->fmb_mutex);
855 flow_miss_batch_destroy(fmb);