lib/cmap: cmap_find_batch().
[cascardo/ovs.git] / lib / dpif-netdev.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "dpif-netdev.h"
19
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <netinet/in.h>
25 #include <sys/socket.h>
26 #include <net/if.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33
34 #include "classifier.h"
35 #include "cmap.h"
36 #include "csum.h"
37 #include "dpif.h"
38 #include "dpif-provider.h"
39 #include "dummy.h"
40 #include "dynamic-string.h"
41 #include "fat-rwlock.h"
42 #include "flow.h"
43 #include "cmap.h"
44 #include "latch.h"
45 #include "list.h"
46 #include "meta-flow.h"
47 #include "netdev.h"
48 #include "netdev-dpdk.h"
49 #include "netdev-vport.h"
50 #include "netlink.h"
51 #include "odp-execute.h"
52 #include "odp-util.h"
53 #include "ofp-print.h"
54 #include "ofpbuf.h"
55 #include "ovs-numa.h"
56 #include "ovs-rcu.h"
57 #include "packet-dpif.h"
58 #include "packets.h"
59 #include "poll-loop.h"
60 #include "random.h"
61 #include "seq.h"
62 #include "shash.h"
63 #include "sset.h"
64 #include "timeval.h"
65 #include "unixctl.h"
66 #include "util.h"
67 #include "vlog.h"
68
69 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
70
71 /* By default, choose a priority in the middle. */
72 #define NETDEV_RULE_PRIORITY 0x8000
73
74 #define FLOW_DUMP_MAX_BATCH 50
75 /* Use per thread recirc_depth to prevent recirculation loop. */
76 #define MAX_RECIRC_DEPTH 5
77 DEFINE_STATIC_PER_THREAD_DATA(uint32_t, recirc_depth, 0)
78
79 /* Configuration parameters. */
80 enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
81
82 /* Protects against changes to 'dp_netdevs'. */
83 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
84
85 /* Contains all 'struct dp_netdev's. */
86 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
87     = SHASH_INITIALIZER(&dp_netdevs);
88
89 static struct vlog_rate_limit upcall_rl = VLOG_RATE_LIMIT_INIT(600, 600);
90
91 /* Stores a miniflow with inline values */
92
93 /* There are fields in the flow structure that we never use. Therefore we can
94  * save a few words of memory */
95 #define NETDEV_KEY_BUF_SIZE_U32 (FLOW_U32S                 \
96                                  - MINI_N_INLINE           \
97                                  - FLOW_U32_SIZE(regs)     \
98                                  - FLOW_U32_SIZE(metadata) \
99                                 )
100 struct netdev_flow_key {
101     struct miniflow flow;
102     uint32_t buf[NETDEV_KEY_BUF_SIZE_U32];
103 };
104
105 /* Exact match cache for frequently used flows
106  *
107  * The cache uses a 32-bit hash of the packet (which can be the RSS hash) to
108  * search its entries for a miniflow that matches exactly the miniflow of the
109  * packet. It stores the 'cls_rule'(rule) that matches the miniflow.
110  *
111  * A cache entry holds a reference to its 'dp_netdev_flow'.
112  *
113  * A miniflow with a given hash can be in one of EM_FLOW_HASH_SEGS different
114  * entries. The 32-bit hash is split into EM_FLOW_HASH_SEGS values (each of
115  * them is EM_FLOW_HASH_SHIFT bits wide and the remainder is thrown away). Each
116  * value is the index of a cache entry where the miniflow could be.
117  *
118  *
119  * Thread-safety
120  * =============
121  *
122  * Each pmd_thread has its own private exact match cache.
123  * If dp_netdev_input is not called from a pmd thread, a mutex is used.
124  */
125
126 #define EM_FLOW_HASH_SHIFT 10
127 #define EM_FLOW_HASH_ENTRIES (1u << EM_FLOW_HASH_SHIFT)
128 #define EM_FLOW_HASH_MASK (EM_FLOW_HASH_ENTRIES - 1)
129 #define EM_FLOW_HASH_SEGS 2
130
131 struct emc_entry {
132     uint32_t hash;
133     uint32_t mf_len;
134     struct netdev_flow_key mf;
135     struct dp_netdev_flow *flow;
136 };
137
138 struct emc_cache {
139     struct emc_entry entries[EM_FLOW_HASH_ENTRIES];
140 };
141
142 /* Iterate in the exact match cache through every entry that might contain a
143  * miniflow with hash 'HASH'. */
144 #define EMC_FOR_EACH_POS_WITH_HASH(EMC, CURRENT_ENTRY, HASH)                 \
145     for (uint32_t i__ = 0, srch_hash__ = (HASH);                             \
146          (CURRENT_ENTRY) = &(EMC)->entries[srch_hash__ & EM_FLOW_HASH_MASK], \
147          i__ < EM_FLOW_HASH_SEGS;                                            \
148          i__++, srch_hash__ >>= EM_FLOW_HASH_SHIFT)
149
150 /* Datapath based on the network device interface from netdev.h.
151  *
152  *
153  * Thread-safety
154  * =============
155  *
156  * Some members, marked 'const', are immutable.  Accessing other members
157  * requires synchronization, as noted in more detail below.
158  *
159  * Acquisition order is, from outermost to innermost:
160  *
161  *    dp_netdev_mutex (global)
162  *    port_mutex
163  *    flow_mutex
164  */
165 struct dp_netdev {
166     const struct dpif_class *const class;
167     const char *const name;
168     struct dpif *dpif;
169     struct ovs_refcount ref_cnt;
170     atomic_flag destroyed;
171
172     /* Flows.
173      *
174      * Writers of 'flow_table' must take the 'flow_mutex'.  Corresponding
175      * changes to 'cls' must be made while still holding the 'flow_mutex'.
176      */
177     struct ovs_mutex flow_mutex;
178     struct classifier cls;
179     struct cmap flow_table OVS_GUARDED; /* Flow table. */
180
181     /* Statistics.
182      *
183      * ovsthread_stats is internally synchronized. */
184     struct ovsthread_stats stats; /* Contains 'struct dp_netdev_stats *'. */
185
186     /* Ports.
187      *
188      * Protected by RCU.  Take the mutex to add or remove ports. */
189     struct ovs_mutex port_mutex;
190     struct cmap ports;
191     struct seq *port_seq;       /* Incremented whenever a port changes. */
192
193     /* Protects access to ofproto-dpif-upcall interface during revalidator
194      * thread synchronization. */
195     struct fat_rwlock upcall_rwlock;
196     upcall_callback *upcall_cb;  /* Callback function for executing upcalls. */
197     void *upcall_aux;
198
199     /* Stores all 'struct dp_netdev_pmd_thread's. */
200     struct cmap poll_threads;
201
202     /* Protects the access of the 'struct dp_netdev_pmd_thread'
203      * instance for non-pmd thread. */
204     struct ovs_mutex non_pmd_mutex;
205
206     /* Each pmd thread will store its pointer to
207      * 'struct dp_netdev_pmd_thread' in 'per_pmd_key'. */
208     ovsthread_key_t per_pmd_key;
209
210     /* Number of rx queues for each dpdk interface and the cpu mask
211      * for pin of pmd threads. */
212     size_t n_dpdk_rxqs;
213     char *pmd_cmask;
214 };
215
216 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
217                                                     odp_port_t);
218
219 enum dp_stat_type {
220     DP_STAT_HIT,                /* Packets that matched in the flow table. */
221     DP_STAT_MISS,               /* Packets that did not match. */
222     DP_STAT_LOST,               /* Packets not passed up to the client. */
223     DP_N_STATS
224 };
225
226 /* Contained by struct dp_netdev's 'stats' member.  */
227 struct dp_netdev_stats {
228     struct ovs_mutex mutex;          /* Protects 'n'. */
229
230     /* Indexed by DP_STAT_*, protected by 'mutex'. */
231     unsigned long long int n[DP_N_STATS] OVS_GUARDED;
232 };
233
234
235 /* A port in a netdev-based datapath. */
236 struct dp_netdev_port {
237     struct cmap_node node;      /* Node in dp_netdev's 'ports'. */
238     odp_port_t port_no;
239     struct netdev *netdev;
240     struct netdev_saved_flags *sf;
241     struct netdev_rxq **rxq;
242     struct ovs_refcount ref_cnt;
243     char *type;                 /* Port type as requested by user. */
244 };
245
246 /* A flow in dp_netdev's 'flow_table'.
247  *
248  *
249  * Thread-safety
250  * =============
251  *
252  * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
253  * its dp_netdev's classifier.  The text below calls this classifier 'cls'.
254  *
255  * Motivation
256  * ----------
257  *
258  * The thread safety rules described here for "struct dp_netdev_flow" are
259  * motivated by two goals:
260  *
261  *    - Prevent threads that read members of "struct dp_netdev_flow" from
262  *      reading bad data due to changes by some thread concurrently modifying
263  *      those members.
264  *
265  *    - Prevent two threads making changes to members of a given "struct
266  *      dp_netdev_flow" from interfering with each other.
267  *
268  *
269  * Rules
270  * -----
271  *
272  * A flow 'flow' may be accessed without a risk of being freed during an RCU
273  * grace period.  Code that needs to hold onto a flow for a while
274  * should try incrementing 'flow->ref_cnt' with dp_netdev_flow_ref().
275  *
276  * 'flow->ref_cnt' protects 'flow' from being freed.  It doesn't protect the
277  * flow from being deleted from 'cls' and it doesn't protect members of 'flow'
278  * from modification.
279  *
280  * Some members, marked 'const', are immutable.  Accessing other members
281  * requires synchronization, as noted in more detail below.
282  */
283 struct dp_netdev_flow {
284     bool dead;
285     /* Packet classification. */
286     const struct cls_rule cr;   /* In owning dp_netdev's 'cls'. */
287
288     /* Hash table index by unmasked flow. */
289     const struct cmap_node node; /* In owning dp_netdev's 'flow_table'. */
290     const struct flow flow;      /* The flow that created this entry. */
291
292     /* Number of references.
293      * The classifier owns one reference.
294      * Any thread trying to keep a rule from being freed should hold its own
295      * reference. */
296     struct ovs_refcount ref_cnt;
297
298     /* Statistics.
299      *
300      * Reading or writing these members requires 'mutex'. */
301     struct ovsthread_stats stats; /* Contains "struct dp_netdev_flow_stats". */
302
303     /* Actions. */
304     OVSRCU_TYPE(struct dp_netdev_actions *) actions;
305 };
306
307 static void dp_netdev_flow_unref(struct dp_netdev_flow *);
308 static bool dp_netdev_flow_ref(struct dp_netdev_flow *);
309
310 /* Contained by struct dp_netdev_flow's 'stats' member.  */
311 struct dp_netdev_flow_stats {
312     struct ovs_mutex mutex;         /* Guards all the other members. */
313
314     long long int used OVS_GUARDED; /* Last used time, in monotonic msecs. */
315     long long int packet_count OVS_GUARDED; /* Number of packets matched. */
316     long long int byte_count OVS_GUARDED;   /* Number of bytes matched. */
317     uint16_t tcp_flags OVS_GUARDED; /* Bitwise-OR of seen tcp_flags values. */
318 };
319
320 /* A set of datapath actions within a "struct dp_netdev_flow".
321  *
322  *
323  * Thread-safety
324  * =============
325  *
326  * A struct dp_netdev_actions 'actions' is protected with RCU. */
327 struct dp_netdev_actions {
328     /* These members are immutable: they do not change during the struct's
329      * lifetime.  */
330     struct nlattr *actions;     /* Sequence of OVS_ACTION_ATTR_* attributes. */
331     unsigned int size;          /* Size of 'actions', in bytes. */
332 };
333
334 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
335                                                    size_t);
336 struct dp_netdev_actions *dp_netdev_flow_get_actions(
337     const struct dp_netdev_flow *);
338 static void dp_netdev_actions_free(struct dp_netdev_actions *);
339
340 /* PMD: Poll modes drivers.  PMD accesses devices via polling to eliminate
341  * the performance overhead of interrupt processing.  Therefore netdev can
342  * not implement rx-wait for these devices.  dpif-netdev needs to poll
343  * these device to check for recv buffer.  pmd-thread does polling for
344  * devices assigned to itself thread.
345  *
346  * DPDK used PMD for accessing NIC.
347  *
348  * Note, instance with cpu core id NON_PMD_CORE_ID will be reserved for
349  * I/O of all non-pmd threads.  There will be no actual thread created
350  * for the instance.
351  **/
352 struct dp_netdev_pmd_thread {
353     struct dp_netdev *dp;
354     struct cmap_node node;          /* In 'dp->poll_threads'. */
355     /* Per thread exact-match cache.  Note, the instance for cpu core
356      * NON_PMD_CORE_ID can be accessed by multiple threads, and thusly
357      * need to be protected (e.g. by 'dp_netdev_mutex').  All other
358      * instances will only be accessed by its own pmd thread. */
359     struct emc_cache flow_cache;
360     struct latch exit_latch;        /* For terminating the pmd thread. */
361     atomic_uint change_seq;         /* For reloading pmd ports. */
362     pthread_t thread;
363     int index;                      /* Idx of this pmd thread among pmd*/
364                                     /* threads on same numa node. */
365     int core_id;                    /* CPU core id of this pmd thread. */
366     int numa_id;                    /* numa node id of this pmd thread. */
367 };
368
369 #define PMD_INITIAL_SEQ 1
370
371 /* Interface to netdev-based datapath. */
372 struct dpif_netdev {
373     struct dpif dpif;
374     struct dp_netdev *dp;
375     uint64_t last_port_seq;
376 };
377
378 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
379                               struct dp_netdev_port **portp);
380 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
381                             struct dp_netdev_port **portp);
382 static void dp_netdev_free(struct dp_netdev *)
383     OVS_REQUIRES(dp_netdev_mutex);
384 static void dp_netdev_flow_flush(struct dp_netdev *);
385 static int do_add_port(struct dp_netdev *dp, const char *devname,
386                        const char *type, odp_port_t port_no)
387     OVS_REQUIRES(dp->port_mutex);
388 static void do_del_port(struct dp_netdev *dp, struct dp_netdev_port *)
389     OVS_REQUIRES(dp->port_mutex);
390 static int dpif_netdev_open(const struct dpif_class *, const char *name,
391                             bool create, struct dpif **);
392 static void dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd,
393                                       struct dpif_packet **, int c,
394                                       bool may_steal, struct pkt_metadata *,
395                                       const struct nlattr *actions,
396                                       size_t actions_len);
397 static void dp_netdev_input(struct dp_netdev_pmd_thread *,
398                             struct dpif_packet **, int cnt,
399                             struct pkt_metadata *);
400 static void dp_netdev_disable_upcall(struct dp_netdev *);
401 static void dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd,
402                                     struct dp_netdev *dp, int index,
403                                     int core_id, int numa_id);
404 static void dp_netdev_set_nonpmd(struct dp_netdev *dp);
405 static struct dp_netdev_pmd_thread *dp_netdev_get_nonpmd(struct dp_netdev *dp);
406 static void dp_netdev_destroy_all_pmds(struct dp_netdev *dp);
407 static void dp_netdev_del_pmds_on_numa(struct dp_netdev *dp, int numa_id);
408 static void dp_netdev_set_pmds_on_numa(struct dp_netdev *dp, int numa_id);
409 static void dp_netdev_reset_pmd_threads(struct dp_netdev *dp);
410
411 static void emc_clear_entry(struct emc_entry *ce);
412
413 static void
414 emc_cache_init(struct emc_cache *flow_cache)
415 {
416     int i;
417
418     for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) {
419         flow_cache->entries[i].flow = NULL;
420         flow_cache->entries[i].hash = 0;
421         flow_cache->entries[i].mf_len = 0;
422         miniflow_initialize(&flow_cache->entries[i].mf.flow,
423                             flow_cache->entries[i].mf.buf);
424     }
425 }
426
427 static void
428 emc_cache_uninit(struct emc_cache *flow_cache)
429 {
430     int i;
431
432     for (i = 0; i < ARRAY_SIZE(flow_cache->entries); i++) {
433         emc_clear_entry(&flow_cache->entries[i]);
434     }
435 }
436
437 static struct dpif_netdev *
438 dpif_netdev_cast(const struct dpif *dpif)
439 {
440     ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
441     return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
442 }
443
444 static struct dp_netdev *
445 get_dp_netdev(const struct dpif *dpif)
446 {
447     return dpif_netdev_cast(dpif)->dp;
448 }
449
450 static int
451 dpif_netdev_enumerate(struct sset *all_dps,
452                       const struct dpif_class *dpif_class)
453 {
454     struct shash_node *node;
455
456     ovs_mutex_lock(&dp_netdev_mutex);
457     SHASH_FOR_EACH(node, &dp_netdevs) {
458         struct dp_netdev *dp = node->data;
459         if (dpif_class != dp->class) {
460             /* 'dp_netdevs' contains both "netdev" and "dummy" dpifs.
461              * If the class doesn't match, skip this dpif. */
462              continue;
463         }
464         sset_add(all_dps, node->name);
465     }
466     ovs_mutex_unlock(&dp_netdev_mutex);
467
468     return 0;
469 }
470
471 static bool
472 dpif_netdev_class_is_dummy(const struct dpif_class *class)
473 {
474     return class != &dpif_netdev_class;
475 }
476
477 static const char *
478 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
479 {
480     return strcmp(type, "internal") ? type
481                   : dpif_netdev_class_is_dummy(class) ? "dummy"
482                   : "tap";
483 }
484
485 static struct dpif *
486 create_dpif_netdev(struct dp_netdev *dp)
487 {
488     uint16_t netflow_id = hash_string(dp->name, 0);
489     struct dpif_netdev *dpif;
490
491     ovs_refcount_ref(&dp->ref_cnt);
492
493     dpif = xmalloc(sizeof *dpif);
494     dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
495     dpif->dp = dp;
496     dpif->last_port_seq = seq_read(dp->port_seq);
497
498     return &dpif->dpif;
499 }
500
501 /* Choose an unused, non-zero port number and return it on success.
502  * Return ODPP_NONE on failure. */
503 static odp_port_t
504 choose_port(struct dp_netdev *dp, const char *name)
505     OVS_REQUIRES(dp->port_mutex)
506 {
507     uint32_t port_no;
508
509     if (dp->class != &dpif_netdev_class) {
510         const char *p;
511         int start_no = 0;
512
513         /* If the port name begins with "br", start the number search at
514          * 100 to make writing tests easier. */
515         if (!strncmp(name, "br", 2)) {
516             start_no = 100;
517         }
518
519         /* If the port name contains a number, try to assign that port number.
520          * This can make writing unit tests easier because port numbers are
521          * predictable. */
522         for (p = name; *p != '\0'; p++) {
523             if (isdigit((unsigned char) *p)) {
524                 port_no = start_no + strtol(p, NULL, 10);
525                 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
526                     && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
527                     return u32_to_odp(port_no);
528                 }
529                 break;
530             }
531         }
532     }
533
534     for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
535         if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
536             return u32_to_odp(port_no);
537         }
538     }
539
540     return ODPP_NONE;
541 }
542
543 static int
544 create_dp_netdev(const char *name, const struct dpif_class *class,
545                  struct dp_netdev **dpp)
546     OVS_REQUIRES(dp_netdev_mutex)
547 {
548     struct dp_netdev *dp;
549     int error;
550
551     dp = xzalloc(sizeof *dp);
552     shash_add(&dp_netdevs, name, dp);
553
554     *CONST_CAST(const struct dpif_class **, &dp->class) = class;
555     *CONST_CAST(const char **, &dp->name) = xstrdup(name);
556     ovs_refcount_init(&dp->ref_cnt);
557     atomic_flag_clear(&dp->destroyed);
558
559     ovs_mutex_init(&dp->flow_mutex);
560     classifier_init(&dp->cls, NULL);
561     cmap_init(&dp->flow_table);
562
563     ovsthread_stats_init(&dp->stats);
564
565     ovs_mutex_init(&dp->port_mutex);
566     cmap_init(&dp->ports);
567     dp->port_seq = seq_create();
568     fat_rwlock_init(&dp->upcall_rwlock);
569
570     /* Disable upcalls by default. */
571     dp_netdev_disable_upcall(dp);
572     dp->upcall_aux = NULL;
573     dp->upcall_cb = NULL;
574
575     cmap_init(&dp->poll_threads);
576     ovs_mutex_init_recursive(&dp->non_pmd_mutex);
577     ovsthread_key_create(&dp->per_pmd_key, NULL);
578
579     /* Reserves the core NON_PMD_CORE_ID for all non-pmd threads. */
580     ovs_numa_try_pin_core_specific(NON_PMD_CORE_ID);
581     dp_netdev_set_nonpmd(dp);
582     dp->n_dpdk_rxqs = NR_QUEUE;
583
584     ovs_mutex_lock(&dp->port_mutex);
585     error = do_add_port(dp, name, "internal", ODPP_LOCAL);
586     ovs_mutex_unlock(&dp->port_mutex);
587     if (error) {
588         dp_netdev_free(dp);
589         return error;
590     }
591
592     *dpp = dp;
593     return 0;
594 }
595
596 static int
597 dpif_netdev_open(const struct dpif_class *class, const char *name,
598                  bool create, struct dpif **dpifp)
599 {
600     struct dp_netdev *dp;
601     int error;
602
603     ovs_mutex_lock(&dp_netdev_mutex);
604     dp = shash_find_data(&dp_netdevs, name);
605     if (!dp) {
606         error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
607     } else {
608         error = (dp->class != class ? EINVAL
609                  : create ? EEXIST
610                  : 0);
611     }
612     if (!error) {
613         *dpifp = create_dpif_netdev(dp);
614         dp->dpif = *dpifp;
615     }
616     ovs_mutex_unlock(&dp_netdev_mutex);
617
618     return error;
619 }
620
621 static void
622 dp_netdev_destroy_upcall_lock(struct dp_netdev *dp)
623     OVS_NO_THREAD_SAFETY_ANALYSIS
624 {
625     /* Check that upcalls are disabled, i.e. that the rwlock is taken */
626     ovs_assert(fat_rwlock_tryrdlock(&dp->upcall_rwlock));
627
628     /* Before freeing a lock we should release it */
629     fat_rwlock_unlock(&dp->upcall_rwlock);
630     fat_rwlock_destroy(&dp->upcall_rwlock);
631 }
632
633 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
634  * through the 'dp_netdevs' shash while freeing 'dp'. */
635 static void
636 dp_netdev_free(struct dp_netdev *dp)
637     OVS_REQUIRES(dp_netdev_mutex)
638 {
639     struct dp_netdev_port *port;
640     struct dp_netdev_stats *bucket;
641     int i;
642
643     shash_find_and_delete(&dp_netdevs, dp->name);
644
645     dp_netdev_destroy_all_pmds(dp);
646     cmap_destroy(&dp->poll_threads);
647     ovs_mutex_destroy(&dp->non_pmd_mutex);
648     ovsthread_key_delete(dp->per_pmd_key);
649
650     dp_netdev_flow_flush(dp);
651     ovs_mutex_lock(&dp->port_mutex);
652     CMAP_FOR_EACH (port, node, &dp->ports) {
653         do_del_port(dp, port);
654     }
655     ovs_mutex_unlock(&dp->port_mutex);
656
657     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
658         ovs_mutex_destroy(&bucket->mutex);
659         free_cacheline(bucket);
660     }
661     ovsthread_stats_destroy(&dp->stats);
662
663     classifier_destroy(&dp->cls);
664     cmap_destroy(&dp->flow_table);
665     ovs_mutex_destroy(&dp->flow_mutex);
666     seq_destroy(dp->port_seq);
667     cmap_destroy(&dp->ports);
668
669     /* Upcalls must be disabled at this point */
670     dp_netdev_destroy_upcall_lock(dp);
671
672     free(dp->pmd_cmask);
673     free(CONST_CAST(char *, dp->name));
674     free(dp);
675 }
676
677 static void
678 dp_netdev_unref(struct dp_netdev *dp)
679 {
680     if (dp) {
681         /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
682          * get a new reference to 'dp' through the 'dp_netdevs' shash. */
683         ovs_mutex_lock(&dp_netdev_mutex);
684         if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) {
685             dp_netdev_free(dp);
686         }
687         ovs_mutex_unlock(&dp_netdev_mutex);
688     }
689 }
690
691 static void
692 dpif_netdev_close(struct dpif *dpif)
693 {
694     struct dp_netdev *dp = get_dp_netdev(dpif);
695
696     dp_netdev_unref(dp);
697     free(dpif);
698 }
699
700 static int
701 dpif_netdev_destroy(struct dpif *dpif)
702 {
703     struct dp_netdev *dp = get_dp_netdev(dpif);
704
705     if (!atomic_flag_test_and_set(&dp->destroyed)) {
706         if (ovs_refcount_unref_relaxed(&dp->ref_cnt) == 1) {
707             /* Can't happen: 'dpif' still owns a reference to 'dp'. */
708             OVS_NOT_REACHED();
709         }
710     }
711
712     return 0;
713 }
714
715 static int
716 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
717 {
718     struct dp_netdev *dp = get_dp_netdev(dpif);
719     struct dp_netdev_stats *bucket;
720     size_t i;
721
722     stats->n_flows = cmap_count(&dp->flow_table);
723
724     stats->n_hit = stats->n_missed = stats->n_lost = 0;
725     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
726         ovs_mutex_lock(&bucket->mutex);
727         stats->n_hit += bucket->n[DP_STAT_HIT];
728         stats->n_missed += bucket->n[DP_STAT_MISS];
729         stats->n_lost += bucket->n[DP_STAT_LOST];
730         ovs_mutex_unlock(&bucket->mutex);
731     }
732     stats->n_masks = UINT32_MAX;
733     stats->n_mask_hit = UINT64_MAX;
734
735     return 0;
736 }
737
738 static void
739 dp_netdev_reload_pmd__(struct dp_netdev_pmd_thread *pmd)
740 {
741     int old_seq;
742
743     atomic_add_relaxed(&pmd->change_seq, 1, &old_seq);
744 }
745
746 /* Causes all pmd threads to reload its tx/rx devices.
747  * Must be called after adding/removing ports. */
748 static void
749 dp_netdev_reload_pmds(struct dp_netdev *dp)
750 {
751     struct dp_netdev_pmd_thread *pmd;
752
753     CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
754         dp_netdev_reload_pmd__(pmd);
755     }
756 }
757
758 static uint32_t
759 hash_port_no(odp_port_t port_no)
760 {
761     return hash_int(odp_to_u32(port_no), 0);
762 }
763
764 static int
765 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
766             odp_port_t port_no)
767     OVS_REQUIRES(dp->port_mutex)
768 {
769     struct netdev_saved_flags *sf;
770     struct dp_netdev_port *port;
771     struct netdev *netdev;
772     enum netdev_flags flags;
773     const char *open_type;
774     int error;
775     int i;
776
777     /* XXX reject devices already in some dp_netdev. */
778
779     /* Open and validate network device. */
780     open_type = dpif_netdev_port_open_type(dp->class, type);
781     error = netdev_open(devname, open_type, &netdev);
782     if (error) {
783         return error;
784     }
785     /* XXX reject non-Ethernet devices */
786
787     netdev_get_flags(netdev, &flags);
788     if (flags & NETDEV_LOOPBACK) {
789         VLOG_ERR("%s: cannot add a loopback device", devname);
790         netdev_close(netdev);
791         return EINVAL;
792     }
793
794     if (netdev_is_pmd(netdev)) {
795         int n_cores = ovs_numa_get_n_cores();
796
797         if (n_cores == OVS_CORE_UNSPEC) {
798             VLOG_ERR("%s, cannot get cpu core info", devname);
799             return ENOENT;
800         }
801         /* There can only be ovs_numa_get_n_cores() pmd threads,
802          * so creates a txq for each. */
803         error = netdev_set_multiq(netdev, n_cores, dp->n_dpdk_rxqs);
804         if (error) {
805             VLOG_ERR("%s, cannot set multiq", devname);
806             return errno;
807         }
808     }
809     port = xzalloc(sizeof *port);
810     port->port_no = port_no;
811     port->netdev = netdev;
812     port->rxq = xmalloc(sizeof *port->rxq * netdev_n_rxq(netdev));
813     port->type = xstrdup(type);
814     for (i = 0; i < netdev_n_rxq(netdev); i++) {
815         error = netdev_rxq_open(netdev, &port->rxq[i], i);
816         if (error
817             && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
818             VLOG_ERR("%s: cannot receive packets on this network device (%s)",
819                      devname, ovs_strerror(errno));
820             netdev_close(netdev);
821             free(port->type);
822             free(port->rxq);
823             free(port);
824             return error;
825         }
826     }
827
828     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
829     if (error) {
830         for (i = 0; i < netdev_n_rxq(netdev); i++) {
831             netdev_rxq_close(port->rxq[i]);
832         }
833         netdev_close(netdev);
834         free(port->type);
835         free(port->rxq);
836         free(port);
837         return error;
838     }
839     port->sf = sf;
840
841     if (netdev_is_pmd(netdev)) {
842         dp_netdev_set_pmds_on_numa(dp, netdev_get_numa_id(netdev));
843         dp_netdev_reload_pmds(dp);
844     }
845     ovs_refcount_init(&port->ref_cnt);
846
847     cmap_insert(&dp->ports, &port->node, hash_port_no(port_no));
848     seq_change(dp->port_seq);
849
850     return 0;
851 }
852
853 static int
854 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
855                      odp_port_t *port_nop)
856 {
857     struct dp_netdev *dp = get_dp_netdev(dpif);
858     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
859     const char *dpif_port;
860     odp_port_t port_no;
861     int error;
862
863     ovs_mutex_lock(&dp->port_mutex);
864     dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
865     if (*port_nop != ODPP_NONE) {
866         port_no = *port_nop;
867         error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
868     } else {
869         port_no = choose_port(dp, dpif_port);
870         error = port_no == ODPP_NONE ? EFBIG : 0;
871     }
872     if (!error) {
873         *port_nop = port_no;
874         error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
875     }
876     ovs_mutex_unlock(&dp->port_mutex);
877
878     return error;
879 }
880
881 static int
882 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
883 {
884     struct dp_netdev *dp = get_dp_netdev(dpif);
885     int error;
886
887     ovs_mutex_lock(&dp->port_mutex);
888     if (port_no == ODPP_LOCAL) {
889         error = EINVAL;
890     } else {
891         struct dp_netdev_port *port;
892
893         error = get_port_by_number(dp, port_no, &port);
894         if (!error) {
895             do_del_port(dp, port);
896         }
897     }
898     ovs_mutex_unlock(&dp->port_mutex);
899
900     return error;
901 }
902
903 static bool
904 is_valid_port_number(odp_port_t port_no)
905 {
906     return port_no != ODPP_NONE;
907 }
908
909 static struct dp_netdev_port *
910 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
911 {
912     struct dp_netdev_port *port;
913
914     CMAP_FOR_EACH_WITH_HASH (port, node, hash_port_no(port_no), &dp->ports) {
915         if (port->port_no == port_no) {
916             return port;
917         }
918     }
919     return NULL;
920 }
921
922 static int
923 get_port_by_number(struct dp_netdev *dp,
924                    odp_port_t port_no, struct dp_netdev_port **portp)
925 {
926     if (!is_valid_port_number(port_no)) {
927         *portp = NULL;
928         return EINVAL;
929     } else {
930         *portp = dp_netdev_lookup_port(dp, port_no);
931         return *portp ? 0 : ENOENT;
932     }
933 }
934
935 static void
936 port_ref(struct dp_netdev_port *port)
937 {
938     if (port) {
939         ovs_refcount_ref(&port->ref_cnt);
940     }
941 }
942
943 static bool
944 port_try_ref(struct dp_netdev_port *port)
945 {
946     if (port) {
947         return ovs_refcount_try_ref_rcu(&port->ref_cnt);
948     }
949
950     return false;
951 }
952
953 static void
954 port_destroy__(struct dp_netdev_port *port)
955 {
956     int n_rxq = netdev_n_rxq(port->netdev);
957     int i;
958
959     netdev_close(port->netdev);
960     netdev_restore_flags(port->sf);
961
962     for (i = 0; i < n_rxq; i++) {
963         netdev_rxq_close(port->rxq[i]);
964     }
965     free(port->rxq);
966     free(port->type);
967     free(port);
968 }
969
970 static void
971 port_unref(struct dp_netdev_port *port)
972 {
973     if (port && ovs_refcount_unref_relaxed(&port->ref_cnt) == 1) {
974         ovsrcu_postpone(port_destroy__, port);
975     }
976 }
977
978 static int
979 get_port_by_name(struct dp_netdev *dp,
980                  const char *devname, struct dp_netdev_port **portp)
981     OVS_REQUIRES(dp->port_mutex)
982 {
983     struct dp_netdev_port *port;
984
985     CMAP_FOR_EACH (port, node, &dp->ports) {
986         if (!strcmp(netdev_get_name(port->netdev), devname)) {
987             *portp = port;
988             return 0;
989         }
990     }
991     return ENOENT;
992 }
993
994 static int
995 get_n_pmd_threads_on_numa(struct dp_netdev *dp, int numa_id)
996 {
997     struct dp_netdev_pmd_thread *pmd;
998     int n_pmds = 0;
999
1000     CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
1001         if (pmd->numa_id == numa_id) {
1002             n_pmds++;
1003         }
1004     }
1005
1006     return n_pmds;
1007 }
1008
1009 /* Returns 'true' if there is a port with pmd netdev and the netdev
1010  * is on numa node 'numa_id'. */
1011 static bool
1012 has_pmd_port_for_numa(struct dp_netdev *dp, int numa_id)
1013 {
1014     struct dp_netdev_port *port;
1015
1016     CMAP_FOR_EACH (port, node, &dp->ports) {
1017         if (netdev_is_pmd(port->netdev)
1018             && netdev_get_numa_id(port->netdev) == numa_id) {
1019             return true;
1020         }
1021     }
1022
1023     return false;
1024 }
1025
1026
1027 static void
1028 do_del_port(struct dp_netdev *dp, struct dp_netdev_port *port)
1029     OVS_REQUIRES(dp->port_mutex)
1030 {
1031     cmap_remove(&dp->ports, &port->node, hash_odp_port(port->port_no));
1032     seq_change(dp->port_seq);
1033     if (netdev_is_pmd(port->netdev)) {
1034         int numa_id = netdev_get_numa_id(port->netdev);
1035
1036         /* If there is no netdev on the numa node, deletes the pmd threads
1037          * for that numa.  Else, just reloads the queues.  */
1038         if (!has_pmd_port_for_numa(dp, numa_id)) {
1039             dp_netdev_del_pmds_on_numa(dp, numa_id);
1040         }
1041         dp_netdev_reload_pmds(dp);
1042     }
1043
1044     port_unref(port);
1045 }
1046
1047 static void
1048 answer_port_query(const struct dp_netdev_port *port,
1049                   struct dpif_port *dpif_port)
1050 {
1051     dpif_port->name = xstrdup(netdev_get_name(port->netdev));
1052     dpif_port->type = xstrdup(port->type);
1053     dpif_port->port_no = port->port_no;
1054 }
1055
1056 static int
1057 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
1058                                  struct dpif_port *dpif_port)
1059 {
1060     struct dp_netdev *dp = get_dp_netdev(dpif);
1061     struct dp_netdev_port *port;
1062     int error;
1063
1064     error = get_port_by_number(dp, port_no, &port);
1065     if (!error && dpif_port) {
1066         answer_port_query(port, dpif_port);
1067     }
1068
1069     return error;
1070 }
1071
1072 static int
1073 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
1074                                struct dpif_port *dpif_port)
1075 {
1076     struct dp_netdev *dp = get_dp_netdev(dpif);
1077     struct dp_netdev_port *port;
1078     int error;
1079
1080     ovs_mutex_lock(&dp->port_mutex);
1081     error = get_port_by_name(dp, devname, &port);
1082     if (!error && dpif_port) {
1083         answer_port_query(port, dpif_port);
1084     }
1085     ovs_mutex_unlock(&dp->port_mutex);
1086
1087     return error;
1088 }
1089
1090 static void
1091 dp_netdev_flow_free(struct dp_netdev_flow *flow)
1092 {
1093     struct dp_netdev_flow_stats *bucket;
1094     size_t i;
1095
1096     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
1097         ovs_mutex_destroy(&bucket->mutex);
1098         free_cacheline(bucket);
1099     }
1100     ovsthread_stats_destroy(&flow->stats);
1101
1102     cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
1103     dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
1104     free(flow);
1105 }
1106
1107 static void dp_netdev_flow_unref(struct dp_netdev_flow *flow)
1108 {
1109     if (ovs_refcount_unref_relaxed(&flow->ref_cnt) == 1) {
1110         ovsrcu_postpone(dp_netdev_flow_free, flow);
1111     }
1112 }
1113
1114 static void
1115 dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
1116     OVS_REQUIRES(dp->flow_mutex)
1117 {
1118     struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
1119     struct cmap_node *node = CONST_CAST(struct cmap_node *, &flow->node);
1120
1121     classifier_remove(&dp->cls, cr);
1122     cmap_remove(&dp->flow_table, node, flow_hash(&flow->flow, 0));
1123     flow->dead = true;
1124
1125     dp_netdev_flow_unref(flow);
1126 }
1127
1128 static void
1129 dp_netdev_flow_flush(struct dp_netdev *dp)
1130 {
1131     struct dp_netdev_flow *netdev_flow;
1132
1133     ovs_mutex_lock(&dp->flow_mutex);
1134     CMAP_FOR_EACH (netdev_flow, node, &dp->flow_table) {
1135         dp_netdev_remove_flow(dp, netdev_flow);
1136     }
1137     ovs_mutex_unlock(&dp->flow_mutex);
1138 }
1139
1140 static int
1141 dpif_netdev_flow_flush(struct dpif *dpif)
1142 {
1143     struct dp_netdev *dp = get_dp_netdev(dpif);
1144
1145     dp_netdev_flow_flush(dp);
1146     return 0;
1147 }
1148
1149 struct dp_netdev_port_state {
1150     struct cmap_position position;
1151     char *name;
1152 };
1153
1154 static int
1155 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
1156 {
1157     *statep = xzalloc(sizeof(struct dp_netdev_port_state));
1158     return 0;
1159 }
1160
1161 static int
1162 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
1163                            struct dpif_port *dpif_port)
1164 {
1165     struct dp_netdev_port_state *state = state_;
1166     struct dp_netdev *dp = get_dp_netdev(dpif);
1167     struct cmap_node *node;
1168     int retval;
1169
1170     node = cmap_next_position(&dp->ports, &state->position);
1171     if (node) {
1172         struct dp_netdev_port *port;
1173
1174         port = CONTAINER_OF(node, struct dp_netdev_port, node);
1175
1176         free(state->name);
1177         state->name = xstrdup(netdev_get_name(port->netdev));
1178         dpif_port->name = state->name;
1179         dpif_port->type = port->type;
1180         dpif_port->port_no = port->port_no;
1181
1182         retval = 0;
1183     } else {
1184         retval = EOF;
1185     }
1186
1187     return retval;
1188 }
1189
1190 static int
1191 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1192 {
1193     struct dp_netdev_port_state *state = state_;
1194     free(state->name);
1195     free(state);
1196     return 0;
1197 }
1198
1199 static int
1200 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
1201 {
1202     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1203     uint64_t new_port_seq;
1204     int error;
1205
1206     new_port_seq = seq_read(dpif->dp->port_seq);
1207     if (dpif->last_port_seq != new_port_seq) {
1208         dpif->last_port_seq = new_port_seq;
1209         error = ENOBUFS;
1210     } else {
1211         error = EAGAIN;
1212     }
1213
1214     return error;
1215 }
1216
1217 static void
1218 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
1219 {
1220     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1221
1222     seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
1223 }
1224
1225 static struct dp_netdev_flow *
1226 dp_netdev_flow_cast(const struct cls_rule *cr)
1227 {
1228     return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1229 }
1230
1231 static bool dp_netdev_flow_ref(struct dp_netdev_flow *flow)
1232 {
1233     return ovs_refcount_try_ref_rcu(&flow->ref_cnt);
1234 }
1235
1236 /* netdev_flow_key utilities.
1237  *
1238  * netdev_flow_key is basically a miniflow.  We use these functions
1239  * (netdev_flow_key_clone, netdev_flow_key_equal, ...) instead of the miniflow
1240  * functions (miniflow_clone_inline, miniflow_equal, ...), because:
1241  *
1242  * - Since we are dealing exclusively with miniflows created by
1243  *   miniflow_extract(), if the map is different the miniflow is different.
1244  *   Therefore we can be faster by comparing the map and the miniflow in a
1245  *   single memcmp().
1246  * _ netdev_flow_key's miniflow has always inline values.
1247  * - These functions can be inlined by the compiler.
1248  *
1249  * The following assertions make sure that what we're doing with miniflow is
1250  * safe
1251  */
1252 BUILD_ASSERT_DECL(offsetof(struct miniflow, inline_values)
1253                   == sizeof(uint64_t));
1254 BUILD_ASSERT_DECL(offsetof(struct netdev_flow_key, flow) == 0);
1255
1256 static inline struct netdev_flow_key *
1257 miniflow_to_netdev_flow_key(const struct miniflow *mf)
1258 {
1259     return (struct netdev_flow_key *) CONST_CAST(struct miniflow *, mf);
1260 }
1261
1262 /* Given the number of bits set in the miniflow map, returns the size of the
1263  * netdev_flow key */
1264 static inline uint32_t
1265 netdev_flow_key_size(uint32_t flow_u32s)
1266 {
1267     return MINIFLOW_VALUES_SIZE(flow_u32s)
1268            + offsetof(struct miniflow, inline_values);
1269 }
1270
1271 /* Used to compare 'netdev_flow_key's (miniflows) in the exact match cache. */
1272 static inline bool
1273 netdev_flow_key_equal(const struct netdev_flow_key *a,
1274                       const struct netdev_flow_key *b,
1275                       uint32_t size)
1276 {
1277     return !memcmp(a, b, size);
1278 }
1279
1280 static inline void
1281 netdev_flow_key_clone(struct netdev_flow_key *dst,
1282                       const struct netdev_flow_key *src,
1283                       uint32_t size)
1284 {
1285     memcpy(dst, src, size);
1286 }
1287
1288 static inline bool
1289 emc_entry_alive(struct emc_entry *ce)
1290 {
1291     return ce->flow && !ce->flow->dead;
1292 }
1293
1294 static void
1295 emc_clear_entry(struct emc_entry *ce)
1296 {
1297     if (ce->flow) {
1298         dp_netdev_flow_unref(ce->flow);
1299         ce->flow = NULL;
1300     }
1301 }
1302
1303 static inline void
1304 emc_change_entry(struct emc_entry *ce, struct dp_netdev_flow *flow,
1305                  const struct netdev_flow_key *mf, uint32_t hash)
1306 {
1307     if (ce->flow != flow) {
1308         if (ce->flow) {
1309             dp_netdev_flow_unref(ce->flow);
1310         }
1311
1312         if (dp_netdev_flow_ref(flow)) {
1313             ce->flow = flow;
1314         } else {
1315             ce->flow = NULL;
1316         }
1317     }
1318     if (mf) {
1319         uint32_t mf_len = netdev_flow_key_size(count_1bits(mf->flow.map));
1320
1321         netdev_flow_key_clone(&ce->mf, mf, mf_len);
1322         ce->hash = hash;
1323         ce->mf_len = mf_len;
1324     }
1325 }
1326
1327 static inline void
1328 emc_insert(struct emc_cache *cache, const struct miniflow *mf, uint32_t hash,
1329            struct dp_netdev_flow *flow)
1330 {
1331     struct emc_entry *to_be_replaced = NULL;
1332     struct emc_entry *current_entry;
1333
1334     EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, hash) {
1335         if (current_entry->hash == hash
1336             && netdev_flow_key_equal(&current_entry->mf,
1337                                      miniflow_to_netdev_flow_key(mf),
1338                                      current_entry->mf_len)) {
1339
1340             /* We found the entry with the 'mf' miniflow */
1341             emc_change_entry(current_entry, flow, NULL, 0);
1342             return;
1343         }
1344
1345         /* Replacement policy: put the flow in an empty (not alive) entry, or
1346          * in the first entry where it can be */
1347         if (!to_be_replaced
1348             || (emc_entry_alive(to_be_replaced)
1349                 && !emc_entry_alive(current_entry))
1350             || current_entry->hash < to_be_replaced->hash) {
1351             to_be_replaced = current_entry;
1352         }
1353     }
1354     /* We didn't find the miniflow in the cache.
1355      * The 'to_be_replaced' entry is where the new flow will be stored */
1356
1357     emc_change_entry(to_be_replaced, flow, miniflow_to_netdev_flow_key(mf),
1358                      hash);
1359 }
1360
1361 static inline struct dp_netdev_flow *
1362 emc_lookup(struct emc_cache *cache, const struct miniflow *mf, uint32_t hash)
1363 {
1364     struct emc_entry *current_entry;
1365
1366     EMC_FOR_EACH_POS_WITH_HASH(cache, current_entry, hash) {
1367         if (current_entry->hash == hash && emc_entry_alive(current_entry)
1368             && netdev_flow_key_equal(&current_entry->mf,
1369                                      miniflow_to_netdev_flow_key(mf),
1370                                      current_entry->mf_len)) {
1371
1372             /* We found the entry with the 'mf' miniflow */
1373             return current_entry->flow;
1374         }
1375     }
1376
1377     return NULL;
1378 }
1379
1380 static struct dp_netdev_flow *
1381 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct miniflow *key)
1382 {
1383     struct dp_netdev_flow *netdev_flow;
1384     struct cls_rule *rule;
1385
1386     classifier_lookup_miniflow_batch(&dp->cls, &key, &rule, 1);
1387     netdev_flow = dp_netdev_flow_cast(rule);
1388
1389     return netdev_flow;
1390 }
1391
1392 static struct dp_netdev_flow *
1393 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1394 {
1395     struct dp_netdev_flow *netdev_flow;
1396
1397     CMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1398                              &dp->flow_table) {
1399         if (flow_equal(&netdev_flow->flow, flow)) {
1400             return netdev_flow;
1401         }
1402     }
1403
1404     return NULL;
1405 }
1406
1407 static void
1408 get_dpif_flow_stats(const struct dp_netdev_flow *netdev_flow,
1409                     struct dpif_flow_stats *stats)
1410 {
1411     struct dp_netdev_flow_stats *bucket;
1412     size_t i;
1413
1414     memset(stats, 0, sizeof *stats);
1415     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1416         ovs_mutex_lock(&bucket->mutex);
1417         stats->n_packets += bucket->packet_count;
1418         stats->n_bytes += bucket->byte_count;
1419         stats->used = MAX(stats->used, bucket->used);
1420         stats->tcp_flags |= bucket->tcp_flags;
1421         ovs_mutex_unlock(&bucket->mutex);
1422     }
1423 }
1424
1425 static void
1426 dp_netdev_flow_to_dpif_flow(const struct dp_netdev_flow *netdev_flow,
1427                             struct ofpbuf *buffer, struct dpif_flow *flow)
1428 {
1429     struct flow_wildcards wc;
1430     struct dp_netdev_actions *actions;
1431
1432     minimask_expand(&netdev_flow->cr.match.mask, &wc);
1433     odp_flow_key_from_mask(buffer, &wc.masks, &netdev_flow->flow,
1434                            odp_to_u32(wc.masks.in_port.odp_port),
1435                            SIZE_MAX, true);
1436     flow->mask = ofpbuf_data(buffer);
1437     flow->mask_len = ofpbuf_size(buffer);
1438
1439     actions = dp_netdev_flow_get_actions(netdev_flow);
1440     flow->actions = actions->actions;
1441     flow->actions_len = actions->size;
1442
1443     get_dpif_flow_stats(netdev_flow, &flow->stats);
1444 }
1445
1446 static int
1447 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1448                               const struct nlattr *mask_key,
1449                               uint32_t mask_key_len, const struct flow *flow,
1450                               struct flow *mask)
1451 {
1452     if (mask_key_len) {
1453         enum odp_key_fitness fitness;
1454
1455         fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1456         if (fitness) {
1457             /* This should not happen: it indicates that
1458              * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1459              * disagree on the acceptable form of a mask.  Log the problem
1460              * as an error, with enough details to enable debugging. */
1461             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1462
1463             if (!VLOG_DROP_ERR(&rl)) {
1464                 struct ds s;
1465
1466                 ds_init(&s);
1467                 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1468                                 true);
1469                 VLOG_ERR("internal error parsing flow mask %s (%s)",
1470                          ds_cstr(&s), odp_key_fitness_to_string(fitness));
1471                 ds_destroy(&s);
1472             }
1473
1474             return EINVAL;
1475         }
1476     } else {
1477         enum mf_field_id id;
1478         /* No mask key, unwildcard everything except fields whose
1479          * prerequisities are not met. */
1480         memset(mask, 0x0, sizeof *mask);
1481
1482         for (id = 0; id < MFF_N_IDS; ++id) {
1483             /* Skip registers and metadata. */
1484             if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1485                 && id != MFF_METADATA) {
1486                 const struct mf_field *mf = mf_from_id(id);
1487                 if (mf_are_prereqs_ok(mf, flow)) {
1488                     mf_mask_field(mf, mask);
1489                 }
1490             }
1491         }
1492     }
1493
1494     /* Force unwildcard the in_port.
1495      *
1496      * We need to do this even in the case where we unwildcard "everything"
1497      * above because "everything" only includes the 16-bit OpenFlow port number
1498      * mask->in_port.ofp_port, which only covers half of the 32-bit datapath
1499      * port number mask->in_port.odp_port. */
1500     mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1501
1502     return 0;
1503 }
1504
1505 static int
1506 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1507                               struct flow *flow)
1508 {
1509     odp_port_t in_port;
1510
1511     if (odp_flow_key_to_flow(key, key_len, flow)) {
1512         /* This should not happen: it indicates that odp_flow_key_from_flow()
1513          * and odp_flow_key_to_flow() disagree on the acceptable form of a
1514          * flow.  Log the problem as an error, with enough details to enable
1515          * debugging. */
1516         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1517
1518         if (!VLOG_DROP_ERR(&rl)) {
1519             struct ds s;
1520
1521             ds_init(&s);
1522             odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1523             VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1524             ds_destroy(&s);
1525         }
1526
1527         return EINVAL;
1528     }
1529
1530     in_port = flow->in_port.odp_port;
1531     if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1532         return EINVAL;
1533     }
1534
1535     return 0;
1536 }
1537
1538 static int
1539 dpif_netdev_flow_get(const struct dpif *dpif, const struct dpif_flow_get *get)
1540 {
1541     struct dp_netdev *dp = get_dp_netdev(dpif);
1542     struct dp_netdev_flow *netdev_flow;
1543     struct flow key;
1544     int error;
1545
1546     error = dpif_netdev_flow_from_nlattrs(get->key, get->key_len, &key);
1547     if (error) {
1548         return error;
1549     }
1550
1551     netdev_flow = dp_netdev_find_flow(dp, &key);
1552
1553     if (netdev_flow) {
1554         dp_netdev_flow_to_dpif_flow(netdev_flow, get->buffer, get->flow);
1555      } else {
1556         error = ENOENT;
1557     }
1558
1559     return error;
1560 }
1561
1562 static int
1563 dp_netdev_flow_add(struct dp_netdev *dp, struct match *match,
1564                    const struct nlattr *actions, size_t actions_len)
1565     OVS_REQUIRES(dp->flow_mutex)
1566 {
1567     struct dp_netdev_flow *netdev_flow;
1568
1569     netdev_flow = xzalloc(sizeof *netdev_flow);
1570     *CONST_CAST(struct flow *, &netdev_flow->flow) = match->flow;
1571
1572     ovs_refcount_init(&netdev_flow->ref_cnt);
1573
1574     ovsthread_stats_init(&netdev_flow->stats);
1575
1576     ovsrcu_set(&netdev_flow->actions,
1577                dp_netdev_actions_create(actions, actions_len));
1578
1579     cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1580                   match, NETDEV_RULE_PRIORITY);
1581     cmap_insert(&dp->flow_table,
1582                 CONST_CAST(struct cmap_node *, &netdev_flow->node),
1583                 flow_hash(&match->flow, 0));
1584     classifier_insert(&dp->cls,
1585                       CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1586
1587     if (OVS_UNLIKELY(VLOG_IS_DBG_ENABLED())) {
1588         struct ds ds = DS_EMPTY_INITIALIZER;
1589
1590         ds_put_cstr(&ds, "flow_add: ");
1591         match_format(match, &ds, OFP_DEFAULT_PRIORITY);
1592         ds_put_cstr(&ds, ", actions:");
1593         format_odp_actions(&ds, actions, actions_len);
1594
1595         VLOG_DBG_RL(&upcall_rl, "%s", ds_cstr(&ds));
1596
1597         ds_destroy(&ds);
1598     }
1599
1600     return 0;
1601 }
1602
1603 static void
1604 clear_stats(struct dp_netdev_flow *netdev_flow)
1605 {
1606     struct dp_netdev_flow_stats *bucket;
1607     size_t i;
1608
1609     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1610         ovs_mutex_lock(&bucket->mutex);
1611         bucket->used = 0;
1612         bucket->packet_count = 0;
1613         bucket->byte_count = 0;
1614         bucket->tcp_flags = 0;
1615         ovs_mutex_unlock(&bucket->mutex);
1616     }
1617 }
1618
1619 static int
1620 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1621 {
1622     struct dp_netdev *dp = get_dp_netdev(dpif);
1623     struct dp_netdev_flow *netdev_flow;
1624     struct miniflow miniflow;
1625     struct match match;
1626     int error;
1627
1628     error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &match.flow);
1629     if (error) {
1630         return error;
1631     }
1632     error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1633                                           put->mask, put->mask_len,
1634                                           &match.flow, &match.wc.masks);
1635     if (error) {
1636         return error;
1637     }
1638     miniflow_init(&miniflow, &match.flow);
1639
1640     ovs_mutex_lock(&dp->flow_mutex);
1641     netdev_flow = dp_netdev_lookup_flow(dp, &miniflow);
1642     if (!netdev_flow) {
1643         if (put->flags & DPIF_FP_CREATE) {
1644             if (cmap_count(&dp->flow_table) < MAX_FLOWS) {
1645                 if (put->stats) {
1646                     memset(put->stats, 0, sizeof *put->stats);
1647                 }
1648                 error = dp_netdev_flow_add(dp, &match, put->actions,
1649                                            put->actions_len);
1650             } else {
1651                 error = EFBIG;
1652             }
1653         } else {
1654             error = ENOENT;
1655         }
1656     } else {
1657         if (put->flags & DPIF_FP_MODIFY
1658             && flow_equal(&match.flow, &netdev_flow->flow)) {
1659             struct dp_netdev_actions *new_actions;
1660             struct dp_netdev_actions *old_actions;
1661
1662             new_actions = dp_netdev_actions_create(put->actions,
1663                                                    put->actions_len);
1664
1665             old_actions = dp_netdev_flow_get_actions(netdev_flow);
1666             ovsrcu_set(&netdev_flow->actions, new_actions);
1667
1668             if (put->stats) {
1669                 get_dpif_flow_stats(netdev_flow, put->stats);
1670             }
1671             if (put->flags & DPIF_FP_ZERO_STATS) {
1672                 clear_stats(netdev_flow);
1673             }
1674
1675             ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1676         } else if (put->flags & DPIF_FP_CREATE) {
1677             error = EEXIST;
1678         } else {
1679             /* Overlapping flow. */
1680             error = EINVAL;
1681         }
1682     }
1683     ovs_mutex_unlock(&dp->flow_mutex);
1684     miniflow_destroy(&miniflow);
1685
1686     return error;
1687 }
1688
1689 static int
1690 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1691 {
1692     struct dp_netdev *dp = get_dp_netdev(dpif);
1693     struct dp_netdev_flow *netdev_flow;
1694     struct flow key;
1695     int error;
1696
1697     error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1698     if (error) {
1699         return error;
1700     }
1701
1702     ovs_mutex_lock(&dp->flow_mutex);
1703     netdev_flow = dp_netdev_find_flow(dp, &key);
1704     if (netdev_flow) {
1705         if (del->stats) {
1706             get_dpif_flow_stats(netdev_flow, del->stats);
1707         }
1708         dp_netdev_remove_flow(dp, netdev_flow);
1709     } else {
1710         error = ENOENT;
1711     }
1712     ovs_mutex_unlock(&dp->flow_mutex);
1713
1714     return error;
1715 }
1716
1717 struct dpif_netdev_flow_dump {
1718     struct dpif_flow_dump up;
1719     struct cmap_position pos;
1720     int status;
1721     struct ovs_mutex mutex;
1722 };
1723
1724 static struct dpif_netdev_flow_dump *
1725 dpif_netdev_flow_dump_cast(struct dpif_flow_dump *dump)
1726 {
1727     return CONTAINER_OF(dump, struct dpif_netdev_flow_dump, up);
1728 }
1729
1730 static struct dpif_flow_dump *
1731 dpif_netdev_flow_dump_create(const struct dpif *dpif_)
1732 {
1733     struct dpif_netdev_flow_dump *dump;
1734
1735     dump = xmalloc(sizeof *dump);
1736     dpif_flow_dump_init(&dump->up, dpif_);
1737     memset(&dump->pos, 0, sizeof dump->pos);
1738     dump->status = 0;
1739     ovs_mutex_init(&dump->mutex);
1740
1741     return &dump->up;
1742 }
1743
1744 static int
1745 dpif_netdev_flow_dump_destroy(struct dpif_flow_dump *dump_)
1746 {
1747     struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_);
1748
1749     ovs_mutex_destroy(&dump->mutex);
1750     free(dump);
1751     return 0;
1752 }
1753
1754 struct dpif_netdev_flow_dump_thread {
1755     struct dpif_flow_dump_thread up;
1756     struct dpif_netdev_flow_dump *dump;
1757     struct odputil_keybuf keybuf[FLOW_DUMP_MAX_BATCH];
1758     struct odputil_keybuf maskbuf[FLOW_DUMP_MAX_BATCH];
1759 };
1760
1761 static struct dpif_netdev_flow_dump_thread *
1762 dpif_netdev_flow_dump_thread_cast(struct dpif_flow_dump_thread *thread)
1763 {
1764     return CONTAINER_OF(thread, struct dpif_netdev_flow_dump_thread, up);
1765 }
1766
1767 static struct dpif_flow_dump_thread *
1768 dpif_netdev_flow_dump_thread_create(struct dpif_flow_dump *dump_)
1769 {
1770     struct dpif_netdev_flow_dump *dump = dpif_netdev_flow_dump_cast(dump_);
1771     struct dpif_netdev_flow_dump_thread *thread;
1772
1773     thread = xmalloc(sizeof *thread);
1774     dpif_flow_dump_thread_init(&thread->up, &dump->up);
1775     thread->dump = dump;
1776     return &thread->up;
1777 }
1778
1779 static void
1780 dpif_netdev_flow_dump_thread_destroy(struct dpif_flow_dump_thread *thread_)
1781 {
1782     struct dpif_netdev_flow_dump_thread *thread
1783         = dpif_netdev_flow_dump_thread_cast(thread_);
1784
1785     free(thread);
1786 }
1787
1788 static int
1789 dpif_netdev_flow_dump_next(struct dpif_flow_dump_thread *thread_,
1790                            struct dpif_flow *flows, int max_flows)
1791 {
1792     struct dpif_netdev_flow_dump_thread *thread
1793         = dpif_netdev_flow_dump_thread_cast(thread_);
1794     struct dpif_netdev_flow_dump *dump = thread->dump;
1795     struct dpif_netdev *dpif = dpif_netdev_cast(thread->up.dpif);
1796     struct dp_netdev_flow *netdev_flows[FLOW_DUMP_MAX_BATCH];
1797     struct dp_netdev *dp = get_dp_netdev(&dpif->dpif);
1798     int n_flows = 0;
1799     int i;
1800
1801     ovs_mutex_lock(&dump->mutex);
1802     if (!dump->status) {
1803         for (n_flows = 0; n_flows < MIN(max_flows, FLOW_DUMP_MAX_BATCH);
1804              n_flows++) {
1805             struct cmap_node *node;
1806
1807             node = cmap_next_position(&dp->flow_table, &dump->pos);
1808             if (!node) {
1809                 dump->status = EOF;
1810                 break;
1811             }
1812             netdev_flows[n_flows] = CONTAINER_OF(node, struct dp_netdev_flow,
1813                                                  node);
1814         }
1815     }
1816     ovs_mutex_unlock(&dump->mutex);
1817
1818     for (i = 0; i < n_flows; i++) {
1819         struct odputil_keybuf *maskbuf = &thread->maskbuf[i];
1820         struct odputil_keybuf *keybuf = &thread->keybuf[i];
1821         struct dp_netdev_flow *netdev_flow = netdev_flows[i];
1822         struct dpif_flow *f = &flows[i];
1823         struct dp_netdev_actions *dp_actions;
1824         struct flow_wildcards wc;
1825         struct ofpbuf buf;
1826
1827         minimask_expand(&netdev_flow->cr.match.mask, &wc);
1828
1829         /* Key. */
1830         ofpbuf_use_stack(&buf, keybuf, sizeof *keybuf);
1831         odp_flow_key_from_flow(&buf, &netdev_flow->flow, &wc.masks,
1832                                netdev_flow->flow.in_port.odp_port, true);
1833         f->key = ofpbuf_data(&buf);
1834         f->key_len = ofpbuf_size(&buf);
1835
1836         /* Mask. */
1837         ofpbuf_use_stack(&buf, maskbuf, sizeof *maskbuf);
1838         odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1839                                odp_to_u32(wc.masks.in_port.odp_port),
1840                                SIZE_MAX, true);
1841         f->mask = ofpbuf_data(&buf);
1842         f->mask_len = ofpbuf_size(&buf);
1843
1844         /* Actions. */
1845         dp_actions = dp_netdev_flow_get_actions(netdev_flow);
1846         f->actions = dp_actions->actions;
1847         f->actions_len = dp_actions->size;
1848
1849         /* Stats. */
1850         get_dpif_flow_stats(netdev_flow, &f->stats);
1851     }
1852
1853     return n_flows;
1854 }
1855
1856 static int
1857 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1858     OVS_NO_THREAD_SAFETY_ANALYSIS
1859 {
1860     struct dp_netdev *dp = get_dp_netdev(dpif);
1861     struct dp_netdev_pmd_thread *pmd;
1862     struct dpif_packet packet, *pp;
1863     struct pkt_metadata *md = &execute->md;
1864
1865     if (ofpbuf_size(execute->packet) < ETH_HEADER_LEN ||
1866         ofpbuf_size(execute->packet) > UINT16_MAX) {
1867         return EINVAL;
1868     }
1869
1870     packet.ofpbuf = *execute->packet;
1871     pp = &packet;
1872
1873     /* Tries finding the 'pmd'.  If NULL is returned, that means
1874      * the current thread is a non-pmd thread and should use
1875      * dp_netdev_get_nonpmd(). */
1876     pmd = ovsthread_getspecific(dp->per_pmd_key);
1877     if (!pmd) {
1878         pmd = dp_netdev_get_nonpmd(dp);
1879     }
1880
1881     /* If the current thread is non-pmd thread, acquires
1882      * the 'non_pmd_mutex'. */
1883     if (pmd->core_id == NON_PMD_CORE_ID) {
1884         ovs_mutex_lock(&dp->non_pmd_mutex);
1885     }
1886     dp_netdev_execute_actions(pmd, &pp, 1, false, md, execute->actions,
1887                               execute->actions_len);
1888     if (pmd->core_id == NON_PMD_CORE_ID) {
1889         ovs_mutex_unlock(&dp->non_pmd_mutex);
1890     }
1891
1892     /* Even though may_steal is set to false, some actions could modify or
1893      * reallocate the ofpbuf memory. We need to pass those changes to the
1894      * caller */
1895     *execute->packet = packet.ofpbuf;
1896
1897     return 0;
1898 }
1899
1900 static void
1901 dpif_netdev_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1902 {
1903     size_t i;
1904
1905     for (i = 0; i < n_ops; i++) {
1906         struct dpif_op *op = ops[i];
1907
1908         switch (op->type) {
1909         case DPIF_OP_FLOW_PUT:
1910             op->error = dpif_netdev_flow_put(dpif, &op->u.flow_put);
1911             break;
1912
1913         case DPIF_OP_FLOW_DEL:
1914             op->error = dpif_netdev_flow_del(dpif, &op->u.flow_del);
1915             break;
1916
1917         case DPIF_OP_EXECUTE:
1918             op->error = dpif_netdev_execute(dpif, &op->u.execute);
1919             break;
1920
1921         case DPIF_OP_FLOW_GET:
1922             op->error = dpif_netdev_flow_get(dpif, &op->u.flow_get);
1923             break;
1924         }
1925     }
1926 }
1927
1928 /* Returns true if the configuration for rx queues or cpu mask
1929  * is changed. */
1930 static bool
1931 pmd_config_changed(const struct dp_netdev *dp, size_t rxqs, const char *cmask)
1932 {
1933     if (dp->n_dpdk_rxqs != rxqs) {
1934         return true;
1935     } else {
1936         if (dp->pmd_cmask != NULL && cmask != NULL) {
1937             return strcmp(dp->pmd_cmask, cmask);
1938         } else {
1939             return (dp->pmd_cmask != NULL || cmask != NULL);
1940         }
1941     }
1942 }
1943
1944 /* Resets pmd threads if the configuration for 'rxq's or cpu mask changes. */
1945 static int
1946 dpif_netdev_pmd_set(struct dpif *dpif, unsigned int n_rxqs, const char *cmask)
1947 {
1948     struct dp_netdev *dp = get_dp_netdev(dpif);
1949
1950     if (pmd_config_changed(dp, n_rxqs, cmask)) {
1951         struct dp_netdev_port *port;
1952
1953         dp_netdev_destroy_all_pmds(dp);
1954
1955         CMAP_FOR_EACH (port, node, &dp->ports) {
1956             if (netdev_is_pmd(port->netdev)) {
1957                 int i, err;
1958
1959                 /* Closes the existing 'rxq's. */
1960                 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1961                     netdev_rxq_close(port->rxq[i]);
1962                     port->rxq[i] = NULL;
1963                 }
1964
1965                 /* Sets the new rx queue config.  */
1966                 err = netdev_set_multiq(port->netdev, ovs_numa_get_n_cores(),
1967                                         n_rxqs);
1968                 if (err) {
1969                     VLOG_ERR("Failed to set dpdk interface %s rx_queue to:"
1970                              " %u", netdev_get_name(port->netdev),
1971                              n_rxqs);
1972                     return err;
1973                 }
1974
1975                 /* If the set_multiq() above succeeds, reopens the 'rxq's. */
1976                 port->rxq = xrealloc(port->rxq, sizeof *port->rxq
1977                                      * netdev_n_rxq(port->netdev));
1978                 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1979                     netdev_rxq_open(port->netdev, &port->rxq[i], i);
1980                 }
1981             }
1982         }
1983         dp->n_dpdk_rxqs = n_rxqs;
1984
1985         /* Reconfigures the cpu mask. */
1986         ovs_numa_set_cpu_mask(cmask);
1987         free(dp->pmd_cmask);
1988         dp->pmd_cmask = cmask ? xstrdup(cmask) : NULL;
1989
1990         /* Restores the non-pmd. */
1991         dp_netdev_set_nonpmd(dp);
1992         /* Restores all pmd threads. */
1993         dp_netdev_reset_pmd_threads(dp);
1994     }
1995
1996     return 0;
1997 }
1998
1999 static int
2000 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
2001                               uint32_t queue_id, uint32_t *priority)
2002 {
2003     *priority = queue_id;
2004     return 0;
2005 }
2006
2007 \f
2008 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
2009  * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
2010  * 'ofpacts'. */
2011 struct dp_netdev_actions *
2012 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
2013 {
2014     struct dp_netdev_actions *netdev_actions;
2015
2016     netdev_actions = xmalloc(sizeof *netdev_actions);
2017     netdev_actions->actions = xmemdup(actions, size);
2018     netdev_actions->size = size;
2019
2020     return netdev_actions;
2021 }
2022
2023 struct dp_netdev_actions *
2024 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
2025 {
2026     return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
2027 }
2028
2029 static void
2030 dp_netdev_actions_free(struct dp_netdev_actions *actions)
2031 {
2032     free(actions->actions);
2033     free(actions);
2034 }
2035 \f
2036
2037 static void
2038 dp_netdev_process_rxq_port(struct dp_netdev_pmd_thread *pmd,
2039                            struct dp_netdev_port *port,
2040                            struct netdev_rxq *rxq)
2041 {
2042     struct dpif_packet *packets[NETDEV_MAX_RX_BATCH];
2043     int error, cnt;
2044
2045     error = netdev_rxq_recv(rxq, packets, &cnt);
2046     if (!error) {
2047         struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
2048
2049         *recirc_depth_get() = 0;
2050         dp_netdev_input(pmd, packets, cnt, &md);
2051     } else if (error != EAGAIN && error != EOPNOTSUPP) {
2052         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2053
2054         VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
2055                     netdev_get_name(port->netdev), ovs_strerror(error));
2056     }
2057 }
2058
2059 static void
2060 dpif_netdev_run(struct dpif *dpif)
2061 {
2062     struct dp_netdev_port *port;
2063     struct dp_netdev *dp = get_dp_netdev(dpif);
2064     struct dp_netdev_pmd_thread *non_pmd = dp_netdev_get_nonpmd(dp);
2065
2066     ovs_mutex_lock(&dp->non_pmd_mutex);
2067     CMAP_FOR_EACH (port, node, &dp->ports) {
2068         if (!netdev_is_pmd(port->netdev)) {
2069             int i;
2070
2071             for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
2072                 dp_netdev_process_rxq_port(non_pmd, port, port->rxq[i]);
2073             }
2074         }
2075     }
2076     ovs_mutex_unlock(&dp->non_pmd_mutex);
2077 }
2078
2079 static void
2080 dpif_netdev_wait(struct dpif *dpif)
2081 {
2082     struct dp_netdev_port *port;
2083     struct dp_netdev *dp = get_dp_netdev(dpif);
2084
2085     ovs_mutex_lock(&dp_netdev_mutex);
2086     CMAP_FOR_EACH (port, node, &dp->ports) {
2087         if (!netdev_is_pmd(port->netdev)) {
2088             int i;
2089
2090             for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
2091                 netdev_rxq_wait(port->rxq[i]);
2092             }
2093         }
2094     }
2095     ovs_mutex_unlock(&dp_netdev_mutex);
2096 }
2097
2098 struct rxq_poll {
2099     struct dp_netdev_port *port;
2100     struct netdev_rxq *rx;
2101 };
2102
2103 static int
2104 pmd_load_queues(struct dp_netdev_pmd_thread *pmd,
2105                 struct rxq_poll **ppoll_list, int poll_cnt)
2106 {
2107     struct rxq_poll *poll_list = *ppoll_list;
2108     struct dp_netdev_port *port;
2109     int n_pmds_on_numa, index, i;
2110
2111     /* Simple scheduler for netdev rx polling. */
2112     for (i = 0; i < poll_cnt; i++) {
2113         port_unref(poll_list[i].port);
2114     }
2115
2116     poll_cnt = 0;
2117     n_pmds_on_numa = get_n_pmd_threads_on_numa(pmd->dp, pmd->numa_id);
2118     index = 0;
2119
2120     CMAP_FOR_EACH (port, node, &pmd->dp->ports) {
2121         /* Calls port_try_ref() to prevent the main thread
2122          * from deleting the port. */
2123         if (port_try_ref(port)) {
2124             if (netdev_is_pmd(port->netdev)
2125                 && netdev_get_numa_id(port->netdev) == pmd->numa_id) {
2126                 int i;
2127
2128                 for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
2129                     if ((index % n_pmds_on_numa) == pmd->index) {
2130                         poll_list = xrealloc(poll_list,
2131                                         sizeof *poll_list * (poll_cnt + 1));
2132
2133                         port_ref(port);
2134                         poll_list[poll_cnt].port = port;
2135                         poll_list[poll_cnt].rx = port->rxq[i];
2136                         poll_cnt++;
2137                     }
2138                     index++;
2139                 }
2140             }
2141             /* Unrefs the port_try_ref(). */
2142             port_unref(port);
2143         }
2144     }
2145
2146     *ppoll_list = poll_list;
2147     return poll_cnt;
2148 }
2149
2150 static void *
2151 pmd_thread_main(void *f_)
2152 {
2153     struct dp_netdev_pmd_thread *pmd = f_;
2154     unsigned int lc = 0;
2155     struct rxq_poll *poll_list;
2156     unsigned int port_seq = PMD_INITIAL_SEQ;
2157     int poll_cnt;
2158     int i;
2159
2160     poll_cnt = 0;
2161     poll_list = NULL;
2162
2163     /* Stores the pmd thread's 'pmd' to 'per_pmd_key'. */
2164     ovsthread_setspecific(pmd->dp->per_pmd_key, pmd);
2165     pmd_thread_setaffinity_cpu(pmd->core_id);
2166 reload:
2167     emc_cache_init(&pmd->flow_cache);
2168     poll_cnt = pmd_load_queues(pmd, &poll_list, poll_cnt);
2169
2170     for (;;) {
2171         int i;
2172
2173         for (i = 0; i < poll_cnt; i++) {
2174             dp_netdev_process_rxq_port(pmd, poll_list[i].port, poll_list[i].rx);
2175         }
2176
2177         if (lc++ > 1024) {
2178             unsigned int seq;
2179
2180             lc = 0;
2181
2182             ovsrcu_quiesce();
2183
2184             atomic_read_relaxed(&pmd->change_seq, &seq);
2185             if (seq != port_seq) {
2186                 port_seq = seq;
2187                 break;
2188             }
2189         }
2190     }
2191
2192     emc_cache_uninit(&pmd->flow_cache);
2193
2194     if (!latch_is_set(&pmd->exit_latch)){
2195         goto reload;
2196     }
2197
2198     for (i = 0; i < poll_cnt; i++) {
2199          port_unref(poll_list[i].port);
2200     }
2201
2202     free(poll_list);
2203     return NULL;
2204 }
2205
2206 static void
2207 dp_netdev_disable_upcall(struct dp_netdev *dp)
2208     OVS_ACQUIRES(dp->upcall_rwlock)
2209 {
2210     fat_rwlock_wrlock(&dp->upcall_rwlock);
2211 }
2212
2213 static void
2214 dpif_netdev_disable_upcall(struct dpif *dpif)
2215     OVS_NO_THREAD_SAFETY_ANALYSIS
2216 {
2217     struct dp_netdev *dp = get_dp_netdev(dpif);
2218     dp_netdev_disable_upcall(dp);
2219 }
2220
2221 static void
2222 dp_netdev_enable_upcall(struct dp_netdev *dp)
2223     OVS_RELEASES(dp->upcall_rwlock)
2224 {
2225     fat_rwlock_unlock(&dp->upcall_rwlock);
2226 }
2227
2228 static void
2229 dpif_netdev_enable_upcall(struct dpif *dpif)
2230     OVS_NO_THREAD_SAFETY_ANALYSIS
2231 {
2232     struct dp_netdev *dp = get_dp_netdev(dpif);
2233     dp_netdev_enable_upcall(dp);
2234 }
2235
2236 /* Returns the pointer to the dp_netdev_pmd_thread for non-pmd threads. */
2237 static struct dp_netdev_pmd_thread *
2238 dp_netdev_get_nonpmd(struct dp_netdev *dp)
2239 {
2240     struct dp_netdev_pmd_thread *pmd;
2241     const struct cmap_node *pnode;
2242
2243     pnode = cmap_find(&dp->poll_threads, hash_int(NON_PMD_CORE_ID, 0));
2244     ovs_assert(pnode);
2245     pmd = CONTAINER_OF(pnode, struct dp_netdev_pmd_thread, node);
2246
2247     return pmd;
2248 }
2249
2250 /* Sets the 'struct dp_netdev_pmd_thread' for non-pmd threads. */
2251 static void
2252 dp_netdev_set_nonpmd(struct dp_netdev *dp)
2253 {
2254     struct dp_netdev_pmd_thread *non_pmd;
2255
2256     non_pmd = xzalloc(sizeof *non_pmd);
2257     dp_netdev_configure_pmd(non_pmd, dp, 0, NON_PMD_CORE_ID,
2258                             OVS_NUMA_UNSPEC);
2259 }
2260
2261 /* Configures the 'pmd' based on the input argument. */
2262 static void
2263 dp_netdev_configure_pmd(struct dp_netdev_pmd_thread *pmd, struct dp_netdev *dp,
2264                         int index, int core_id, int numa_id)
2265 {
2266     pmd->dp = dp;
2267     pmd->index = index;
2268     pmd->core_id = core_id;
2269     pmd->numa_id = numa_id;
2270     latch_init(&pmd->exit_latch);
2271     atomic_init(&pmd->change_seq, PMD_INITIAL_SEQ);
2272     /* init the 'flow_cache' since there is no
2273      * actual thread created for NON_PMD_CORE_ID. */
2274     if (core_id == NON_PMD_CORE_ID) {
2275         emc_cache_init(&pmd->flow_cache);
2276     }
2277     cmap_insert(&dp->poll_threads, CONST_CAST(struct cmap_node *, &pmd->node),
2278                 hash_int(core_id, 0));
2279 }
2280
2281 /* Stops the pmd thread, removes it from the 'dp->poll_threads'
2282  * and destroys the struct. */
2283 static void
2284 dp_netdev_del_pmd(struct dp_netdev_pmd_thread *pmd)
2285 {
2286     /* Uninit the 'flow_cache' since there is
2287      * no actual thread uninit it. */
2288     if (pmd->core_id == NON_PMD_CORE_ID) {
2289         emc_cache_uninit(&pmd->flow_cache);
2290     } else {
2291         latch_set(&pmd->exit_latch);
2292         dp_netdev_reload_pmd__(pmd);
2293         ovs_numa_unpin_core(pmd->core_id);
2294         xpthread_join(pmd->thread, NULL);
2295     }
2296     cmap_remove(&pmd->dp->poll_threads, &pmd->node, hash_int(pmd->core_id, 0));
2297     latch_destroy(&pmd->exit_latch);
2298     free(pmd);
2299 }
2300
2301 /* Destroys all pmd threads. */
2302 static void
2303 dp_netdev_destroy_all_pmds(struct dp_netdev *dp)
2304 {
2305     struct dp_netdev_pmd_thread *pmd;
2306
2307     CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
2308         dp_netdev_del_pmd(pmd);
2309     }
2310 }
2311
2312 /* Deletes all pmd threads on numa node 'numa_id'. */
2313 static void
2314 dp_netdev_del_pmds_on_numa(struct dp_netdev *dp, int numa_id)
2315 {
2316     struct dp_netdev_pmd_thread *pmd;
2317
2318     CMAP_FOR_EACH (pmd, node, &dp->poll_threads) {
2319         if (pmd->numa_id == numa_id) {
2320             dp_netdev_del_pmd(pmd);
2321         }
2322     }
2323 }
2324
2325 /* Checks the numa node id of 'netdev' and starts pmd threads for
2326  * the numa node. */
2327 static void
2328 dp_netdev_set_pmds_on_numa(struct dp_netdev *dp, int numa_id)
2329 {
2330     int n_pmds;
2331
2332     if (!ovs_numa_numa_id_is_valid(numa_id)) {
2333         VLOG_ERR("Cannot create pmd threads due to numa id (%d)"
2334                  "invalid", numa_id);
2335         return ;
2336     }
2337
2338     n_pmds = get_n_pmd_threads_on_numa(dp, numa_id);
2339
2340     /* If there are already pmd threads created for the numa node
2341      * in which 'netdev' is on, do nothing.  Else, creates the
2342      * pmd threads for the numa node. */
2343     if (!n_pmds) {
2344         int can_have, n_unpinned, i;
2345
2346         n_unpinned = ovs_numa_get_n_unpinned_cores_on_numa(numa_id);
2347         if (!n_unpinned) {
2348             VLOG_ERR("Cannot create pmd threads due to out of unpinned "
2349                      "cores on numa node");
2350             return;
2351         }
2352
2353         /* If cpu mask is specified, uses all unpinned cores, otherwise
2354          * tries creating NR_PMD_THREADS pmd threads. */
2355         can_have = dp->pmd_cmask ? n_unpinned : MIN(n_unpinned, NR_PMD_THREADS);
2356         for (i = 0; i < can_have; i++) {
2357             struct dp_netdev_pmd_thread *pmd = xzalloc(sizeof *pmd);
2358             int core_id = ovs_numa_get_unpinned_core_on_numa(numa_id);
2359
2360             dp_netdev_configure_pmd(pmd, dp, i, core_id, numa_id);
2361             /* Each thread will distribute all devices rx-queues among
2362              * themselves. */
2363             pmd->thread = ovs_thread_create("pmd", pmd_thread_main, pmd);
2364         }
2365         VLOG_INFO("Created %d pmd threads on numa node %d", can_have, numa_id);
2366     }
2367 }
2368
2369 \f
2370 static void *
2371 dp_netdev_flow_stats_new_cb(void)
2372 {
2373     struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
2374     ovs_mutex_init(&bucket->mutex);
2375     return bucket;
2376 }
2377
2378 /* Called after pmd threads config change.  Restarts pmd threads with
2379  * new configuration. */
2380 static void
2381 dp_netdev_reset_pmd_threads(struct dp_netdev *dp)
2382 {
2383     struct dp_netdev_port *port;
2384
2385     CMAP_FOR_EACH (port, node, &dp->ports) {
2386         if (netdev_is_pmd(port->netdev)) {
2387             int numa_id = netdev_get_numa_id(port->netdev);
2388
2389             dp_netdev_set_pmds_on_numa(dp, numa_id);
2390         }
2391     }
2392 }
2393
2394 static void
2395 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
2396                     int cnt, int size,
2397                     uint16_t tcp_flags)
2398 {
2399     long long int now = time_msec();
2400     struct dp_netdev_flow_stats *bucket;
2401
2402     bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
2403                                         dp_netdev_flow_stats_new_cb);
2404
2405     ovs_mutex_lock(&bucket->mutex);
2406     bucket->used = MAX(now, bucket->used);
2407     bucket->packet_count += cnt;
2408     bucket->byte_count += size;
2409     bucket->tcp_flags |= tcp_flags;
2410     ovs_mutex_unlock(&bucket->mutex);
2411 }
2412
2413 static void *
2414 dp_netdev_stats_new_cb(void)
2415 {
2416     struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
2417     ovs_mutex_init(&bucket->mutex);
2418     return bucket;
2419 }
2420
2421 static void
2422 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type, int cnt)
2423 {
2424     struct dp_netdev_stats *bucket;
2425
2426     bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
2427     ovs_mutex_lock(&bucket->mutex);
2428     bucket->n[type] += cnt;
2429     ovs_mutex_unlock(&bucket->mutex);
2430 }
2431
2432 static int
2433 dp_netdev_upcall(struct dp_netdev *dp, struct dpif_packet *packet_,
2434                  struct flow *flow, struct flow_wildcards *wc,
2435                  enum dpif_upcall_type type, const struct nlattr *userdata,
2436                  struct ofpbuf *actions, struct ofpbuf *put_actions)
2437 {
2438     struct ofpbuf *packet = &packet_->ofpbuf;
2439
2440     if (type == DPIF_UC_MISS) {
2441         dp_netdev_count_packet(dp, DP_STAT_MISS, 1);
2442     }
2443
2444     if (OVS_UNLIKELY(!dp->upcall_cb)) {
2445         return ENODEV;
2446     }
2447
2448     if (OVS_UNLIKELY(!VLOG_DROP_DBG(&upcall_rl))) {
2449         struct ds ds = DS_EMPTY_INITIALIZER;
2450         struct ofpbuf key;
2451         char *packet_str;
2452
2453         ofpbuf_init(&key, 0);
2454         odp_flow_key_from_flow(&key, flow, &wc->masks, flow->in_port.odp_port,
2455                                true);
2456
2457         packet_str = ofp_packet_to_string(ofpbuf_data(packet),
2458                                           ofpbuf_size(packet));
2459
2460         odp_flow_key_format(ofpbuf_data(&key), ofpbuf_size(&key), &ds);
2461
2462         VLOG_DBG("%s: %s upcall:\n%s\n%s", dp->name,
2463                  dpif_upcall_type_to_string(type), ds_cstr(&ds), packet_str);
2464
2465         ofpbuf_uninit(&key);
2466         free(packet_str);
2467         ds_destroy(&ds);
2468     }
2469
2470     return dp->upcall_cb(packet, flow, type, userdata, actions, wc,
2471                          put_actions, dp->upcall_aux);
2472 }
2473
2474 static inline uint32_t
2475 dpif_netdev_packet_get_dp_hash(struct dpif_packet *packet,
2476                                const struct miniflow *mf)
2477 {
2478     uint32_t hash;
2479
2480     hash = dpif_packet_get_dp_hash(packet);
2481     if (OVS_UNLIKELY(!hash)) {
2482         hash = miniflow_hash_5tuple(mf, 0);
2483         dpif_packet_set_dp_hash(packet, hash);
2484     }
2485     return hash;
2486 }
2487
2488 struct packet_batch {
2489     unsigned int packet_count;
2490     unsigned int byte_count;
2491     uint16_t tcp_flags;
2492
2493     struct dp_netdev_flow *flow;
2494
2495     struct dpif_packet *packets[NETDEV_MAX_RX_BATCH];
2496     struct pkt_metadata md;
2497 };
2498
2499 static inline void
2500 packet_batch_update(struct packet_batch *batch, struct dpif_packet *packet,
2501                     const struct miniflow *mf)
2502 {
2503     batch->tcp_flags |= miniflow_get_tcp_flags(mf);
2504     batch->packets[batch->packet_count++] = packet;
2505     batch->byte_count += ofpbuf_size(&packet->ofpbuf);
2506 }
2507
2508 static inline void
2509 packet_batch_init(struct packet_batch *batch, struct dp_netdev_flow *flow,
2510                   struct pkt_metadata *md)
2511 {
2512     batch->flow = flow;
2513     batch->md = *md;
2514
2515     batch->packet_count = 0;
2516     batch->byte_count = 0;
2517     batch->tcp_flags = 0;
2518 }
2519
2520 static inline void
2521 packet_batch_execute(struct packet_batch *batch,
2522                      struct dp_netdev_pmd_thread *pmd)
2523 {
2524     struct dp_netdev_actions *actions;
2525     struct dp_netdev_flow *flow = batch->flow;
2526
2527     dp_netdev_flow_used(batch->flow, batch->packet_count, batch->byte_count,
2528                         batch->tcp_flags);
2529
2530     actions = dp_netdev_flow_get_actions(flow);
2531
2532     dp_netdev_execute_actions(pmd, batch->packets, batch->packet_count, true,
2533                               &batch->md, actions->actions, actions->size);
2534
2535     dp_netdev_count_packet(pmd->dp, DP_STAT_HIT, batch->packet_count);
2536 }
2537
2538 static inline bool
2539 dp_netdev_queue_batches(struct dpif_packet *pkt, struct pkt_metadata *md,
2540                         struct dp_netdev_flow *flow, const struct miniflow *mf,
2541                         struct packet_batch *batches, size_t *n_batches,
2542                         size_t max_batches)
2543 {
2544     struct packet_batch *batch = NULL;
2545     int j;
2546
2547     if (OVS_UNLIKELY(!flow)) {
2548         return false;
2549     }
2550     /* XXX: This O(n^2) algortihm makes sense if we're operating under the
2551      * assumption that the number of distinct flows (and therefore the
2552      * number of distinct batches) is quite small.  If this turns out not
2553      * to be the case, it may make sense to pre sort based on the
2554      * netdev_flow pointer.  That done we can get the appropriate batching
2555      * in O(n * log(n)) instead. */
2556     for (j = *n_batches - 1; j >= 0; j--) {
2557         if (batches[j].flow == flow) {
2558             batch = &batches[j];
2559             packet_batch_update(batch, pkt, mf);
2560             return true;
2561         }
2562     }
2563     if (OVS_UNLIKELY(*n_batches >= max_batches)) {
2564         return false;
2565     }
2566
2567     batch = &batches[(*n_batches)++];
2568     packet_batch_init(batch, flow, md);
2569     packet_batch_update(batch, pkt, mf);
2570     return true;
2571 }
2572
2573 static inline void
2574 dpif_packet_swap(struct dpif_packet **a, struct dpif_packet **b)
2575 {
2576     struct dpif_packet *tmp = *a;
2577     *a = *b;
2578     *b = tmp;
2579 }
2580
2581 /* Try to process all ('cnt') the 'packets' using only the exact match cache
2582  * 'flow_cache'. If a flow is not found for a packet 'packets[i]', or if there
2583  * is no matching batch for a packet's flow, the miniflow is copied into 'keys'
2584  * and the packet pointer is moved at the beginning of the 'packets' array.
2585  *
2586  * The function returns the number of packets that needs to be processed in the
2587  * 'packets' array (they have been moved to the beginning of the vector).
2588  */
2589 static inline size_t
2590 emc_processing(struct dp_netdev_pmd_thread *pmd, struct dpif_packet **packets,
2591                size_t cnt, struct pkt_metadata *md,
2592                struct netdev_flow_key *keys)
2593 {
2594     struct netdev_flow_key key;
2595     struct packet_batch batches[4];
2596     struct emc_cache *flow_cache = &pmd->flow_cache;
2597     size_t n_batches, i;
2598     size_t notfound_cnt = 0;
2599
2600     n_batches = 0;
2601     miniflow_initialize(&key.flow, key.buf);
2602     for (i = 0; i < cnt; i++) {
2603         struct dp_netdev_flow *flow;
2604         uint32_t hash;
2605
2606         if (OVS_UNLIKELY(ofpbuf_size(&packets[i]->ofpbuf) < ETH_HEADER_LEN)) {
2607             dpif_packet_delete(packets[i]);
2608             continue;
2609         }
2610
2611         miniflow_extract(&packets[i]->ofpbuf, md, &key.flow);
2612
2613         hash = dpif_netdev_packet_get_dp_hash(packets[i], &key.flow);
2614
2615         flow = emc_lookup(flow_cache, &key.flow, hash);
2616         if (OVS_UNLIKELY(!dp_netdev_queue_batches(packets[i], md,
2617                                                   flow,  &key.flow,
2618                                                   batches, &n_batches,
2619                                                   ARRAY_SIZE(batches)))) {
2620             if (i != notfound_cnt) {
2621                 dpif_packet_swap(&packets[i], &packets[notfound_cnt]);
2622             }
2623
2624             keys[notfound_cnt++] = key;
2625         }
2626     }
2627
2628     for (i = 0; i < n_batches; i++) {
2629         packet_batch_execute(&batches[i], pmd);
2630     }
2631
2632     return notfound_cnt;
2633 }
2634
2635 static inline void
2636 fast_path_processing(struct dp_netdev_pmd_thread *pmd,
2637                      struct dpif_packet **packets, size_t cnt,
2638                      struct pkt_metadata *md, struct netdev_flow_key *keys)
2639 {
2640 #if !defined(__CHECKER__) && !defined(_WIN32)
2641     const size_t PKT_ARRAY_SIZE = cnt;
2642 #else
2643     /* Sparse or MSVC doesn't like variable length array. */
2644     enum { PKT_ARRAY_SIZE = NETDEV_MAX_RX_BATCH };
2645 #endif
2646     struct packet_batch batches[PKT_ARRAY_SIZE];
2647     const struct miniflow *mfs[PKT_ARRAY_SIZE]; /* May NOT be NULL. */
2648     struct cls_rule *rules[PKT_ARRAY_SIZE];
2649     struct dp_netdev *dp = pmd->dp;
2650     struct emc_cache *flow_cache = &pmd->flow_cache;
2651     size_t n_batches, i;
2652     bool any_miss;
2653
2654     for (i = 0; i < cnt; i++) {
2655         mfs[i] = &keys[i].flow; /* No bad packets! */
2656     }
2657     any_miss = !classifier_lookup_miniflow_batch(&dp->cls, mfs, rules, cnt);
2658     if (OVS_UNLIKELY(any_miss) && !fat_rwlock_tryrdlock(&dp->upcall_rwlock)) {
2659         uint64_t actions_stub[512 / 8], slow_stub[512 / 8];
2660         struct ofpbuf actions, put_actions;
2661         struct match match;
2662
2663         ofpbuf_use_stub(&actions, actions_stub, sizeof actions_stub);
2664         ofpbuf_use_stub(&put_actions, slow_stub, sizeof slow_stub);
2665
2666         for (i = 0; i < cnt; i++) {
2667             const struct dp_netdev_flow *netdev_flow;
2668             struct ofpbuf *add_actions;
2669             int error;
2670
2671             if (OVS_LIKELY(rules[i] || !mfs[i])) {
2672                 continue;
2673             }
2674
2675             /* It's possible that an earlier slow path execution installed
2676              * the rule this flow needs.  In this case, it's a lot cheaper
2677              * to catch it here than execute a miss. */
2678             netdev_flow = dp_netdev_lookup_flow(dp, mfs[i]);
2679             if (netdev_flow) {
2680                 rules[i] = CONST_CAST(struct cls_rule *, &netdev_flow->cr);
2681                 continue;
2682             }
2683
2684             miniflow_expand(mfs[i], &match.flow);
2685
2686             ofpbuf_clear(&actions);
2687             ofpbuf_clear(&put_actions);
2688
2689             error = dp_netdev_upcall(dp, packets[i], &match.flow, &match.wc,
2690                                       DPIF_UC_MISS, NULL, &actions,
2691                                       &put_actions);
2692             if (OVS_UNLIKELY(error && error != ENOSPC)) {
2693                 continue;
2694             }
2695
2696             /* We can't allow the packet batching in the next loop to execute
2697              * the actions.  Otherwise, if there are any slow path actions,
2698              * we'll send the packet up twice. */
2699             dp_netdev_execute_actions(pmd, &packets[i], 1, true, md,
2700                                       ofpbuf_data(&actions),
2701                                       ofpbuf_size(&actions));
2702
2703             add_actions = ofpbuf_size(&put_actions)
2704                 ? &put_actions
2705                 : &actions;
2706
2707             ovs_mutex_lock(&dp->flow_mutex);
2708             /* XXX: There's a brief race where this flow could have already
2709              * been installed since we last did the flow lookup.  This could be
2710              * solved by moving the mutex lock outside the loop, but that's an
2711              * awful long time to be locking everyone out of making flow
2712              * installs.  If we move to a per-core classifier, it would be
2713              * reasonable. */
2714             if (OVS_LIKELY(error != ENOSPC)
2715                 && !dp_netdev_lookup_flow(dp, mfs[i])) {
2716                 dp_netdev_flow_add(dp, &match, ofpbuf_data(add_actions),
2717                                    ofpbuf_size(add_actions));
2718             }
2719             ovs_mutex_unlock(&dp->flow_mutex);
2720         }
2721
2722         ofpbuf_uninit(&actions);
2723         ofpbuf_uninit(&put_actions);
2724         fat_rwlock_unlock(&dp->upcall_rwlock);
2725     } else if (OVS_UNLIKELY(any_miss)) {
2726         int dropped_cnt = 0;
2727
2728         for (i = 0; i < cnt; i++) {
2729             if (OVS_UNLIKELY(!rules[i] && mfs[i])) {
2730                 dpif_packet_delete(packets[i]);
2731                 dropped_cnt++;
2732             }
2733         }
2734
2735         dp_netdev_count_packet(dp, DP_STAT_LOST, dropped_cnt);
2736     }
2737
2738     n_batches = 0;
2739     for (i = 0; i < cnt; i++) {
2740         struct dpif_packet *packet = packets[i];
2741         struct dp_netdev_flow *flow;
2742
2743         if (OVS_UNLIKELY(!rules[i] || !mfs[i])) {
2744             continue;
2745         }
2746
2747         flow = dp_netdev_flow_cast(rules[i]);
2748         emc_insert(flow_cache, mfs[i], dpif_packet_get_dp_hash(packet),
2749                    flow);
2750         dp_netdev_queue_batches(packet, md, flow, mfs[i], batches, &n_batches,
2751                                 ARRAY_SIZE(batches));
2752     }
2753
2754     for (i = 0; i < n_batches; i++) {
2755         packet_batch_execute(&batches[i], pmd);
2756     }
2757 }
2758
2759 static void
2760 dp_netdev_input(struct dp_netdev_pmd_thread *pmd,
2761                 struct dpif_packet **packets, int cnt, struct pkt_metadata *md)
2762 {
2763 #if !defined(__CHECKER__) && !defined(_WIN32)
2764     const size_t PKT_ARRAY_SIZE = cnt;
2765 #else
2766     /* Sparse or MSVC doesn't like variable length array. */
2767     enum { PKT_ARRAY_SIZE = NETDEV_MAX_RX_BATCH };
2768 #endif
2769     struct netdev_flow_key keys[PKT_ARRAY_SIZE];
2770     size_t newcnt;
2771
2772     newcnt = emc_processing(pmd, packets, cnt, md, keys);
2773     if (OVS_UNLIKELY(newcnt)) {
2774         fast_path_processing(pmd, packets, newcnt, md, keys);
2775     }
2776 }
2777
2778 struct dp_netdev_execute_aux {
2779     struct dp_netdev_pmd_thread *pmd;
2780 };
2781
2782 static void
2783 dpif_netdev_register_upcall_cb(struct dpif *dpif, upcall_callback *cb,
2784                                void *aux)
2785 {
2786     struct dp_netdev *dp = get_dp_netdev(dpif);
2787     dp->upcall_aux = aux;
2788     dp->upcall_cb = cb;
2789 }
2790
2791 static void
2792 dp_netdev_drop_packets(struct dpif_packet ** packets, int cnt, bool may_steal)
2793 {
2794     int i;
2795
2796     if (may_steal) {
2797         for (i = 0; i < cnt; i++) {
2798             dpif_packet_delete(packets[i]);
2799         }
2800     }
2801 }
2802
2803 static void
2804 dp_execute_cb(void *aux_, struct dpif_packet **packets, int cnt,
2805               struct pkt_metadata *md,
2806               const struct nlattr *a, bool may_steal)
2807     OVS_NO_THREAD_SAFETY_ANALYSIS
2808 {
2809     struct dp_netdev_execute_aux *aux = aux_;
2810     uint32_t *depth = recirc_depth_get();
2811     struct dp_netdev_pmd_thread *pmd= aux->pmd;
2812     struct dp_netdev *dp= pmd->dp;
2813     int type = nl_attr_type(a);
2814     struct dp_netdev_port *p;
2815     int i;
2816
2817     switch ((enum ovs_action_attr)type) {
2818     case OVS_ACTION_ATTR_OUTPUT:
2819         p = dp_netdev_lookup_port(dp, u32_to_odp(nl_attr_get_u32(a)));
2820         if (OVS_LIKELY(p)) {
2821             netdev_send(p->netdev, pmd->core_id, packets, cnt, may_steal);
2822             return;
2823         }
2824         break;
2825
2826     case OVS_ACTION_ATTR_USERSPACE:
2827         if (!fat_rwlock_tryrdlock(&dp->upcall_rwlock)) {
2828             const struct nlattr *userdata;
2829             struct ofpbuf actions;
2830             struct flow flow;
2831
2832             userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
2833             ofpbuf_init(&actions, 0);
2834
2835             for (i = 0; i < cnt; i++) {
2836                 int error;
2837
2838                 ofpbuf_clear(&actions);
2839
2840                 flow_extract(&packets[i]->ofpbuf, md, &flow);
2841                 error = dp_netdev_upcall(dp, packets[i], &flow, NULL,
2842                                          DPIF_UC_ACTION, userdata, &actions,
2843                                          NULL);
2844                 if (!error || error == ENOSPC) {
2845                     dp_netdev_execute_actions(pmd, &packets[i], 1, may_steal,
2846                                               md, ofpbuf_data(&actions),
2847                                               ofpbuf_size(&actions));
2848                 } else if (may_steal) {
2849                     dpif_packet_delete(packets[i]);
2850                 }
2851             }
2852             ofpbuf_uninit(&actions);
2853             fat_rwlock_unlock(&dp->upcall_rwlock);
2854
2855             return;
2856         }
2857         break;
2858
2859     case OVS_ACTION_ATTR_HASH: {
2860         const struct ovs_action_hash *hash_act;
2861         uint32_t hash;
2862
2863         hash_act = nl_attr_get(a);
2864
2865         for (i = 0; i < cnt; i++) {
2866
2867             if (hash_act->hash_alg == OVS_HASH_ALG_L4) {
2868                 /* Hash need not be symmetric, nor does it need to include
2869                  * L2 fields. */
2870                 hash = hash_2words(dpif_packet_get_dp_hash(packets[i]),
2871                                    hash_act->hash_basis);
2872             } else {
2873                 VLOG_WARN("Unknown hash algorithm specified "
2874                           "for the hash action.");
2875                 hash = 2;
2876             }
2877
2878             if (!hash) {
2879                 hash = 1; /* 0 is not valid */
2880             }
2881
2882             if (i == 0) {
2883                 md->dp_hash = hash;
2884             }
2885             dpif_packet_set_dp_hash(packets[i], hash);
2886         }
2887         return;
2888     }
2889
2890     case OVS_ACTION_ATTR_RECIRC:
2891         if (*depth < MAX_RECIRC_DEPTH) {
2892
2893             (*depth)++;
2894             for (i = 0; i < cnt; i++) {
2895                 struct dpif_packet *recirc_pkt;
2896                 struct pkt_metadata recirc_md = *md;
2897
2898                 recirc_pkt = (may_steal) ? packets[i]
2899                                     : dpif_packet_clone(packets[i]);
2900
2901                 recirc_md.recirc_id = nl_attr_get_u32(a);
2902
2903                 /* Hash is private to each packet */
2904                 recirc_md.dp_hash = dpif_packet_get_dp_hash(packets[i]);
2905
2906                 dp_netdev_input(pmd, &recirc_pkt, 1,
2907                                 &recirc_md);
2908             }
2909             (*depth)--;
2910
2911             return;
2912         }
2913
2914         VLOG_WARN("Packet dropped. Max recirculation depth exceeded.");
2915         break;
2916
2917     case OVS_ACTION_ATTR_PUSH_VLAN:
2918     case OVS_ACTION_ATTR_POP_VLAN:
2919     case OVS_ACTION_ATTR_PUSH_MPLS:
2920     case OVS_ACTION_ATTR_POP_MPLS:
2921     case OVS_ACTION_ATTR_SET:
2922     case OVS_ACTION_ATTR_SET_MASKED:
2923     case OVS_ACTION_ATTR_SAMPLE:
2924     case OVS_ACTION_ATTR_UNSPEC:
2925     case __OVS_ACTION_ATTR_MAX:
2926         OVS_NOT_REACHED();
2927     }
2928
2929     dp_netdev_drop_packets(packets, cnt, may_steal);
2930 }
2931
2932 static void
2933 dp_netdev_execute_actions(struct dp_netdev_pmd_thread *pmd,
2934                           struct dpif_packet **packets, int cnt,
2935                           bool may_steal, struct pkt_metadata *md,
2936                           const struct nlattr *actions, size_t actions_len)
2937 {
2938     struct dp_netdev_execute_aux aux = {pmd};
2939
2940     odp_execute_actions(&aux, packets, cnt, may_steal, md, actions,
2941                         actions_len, dp_execute_cb);
2942 }
2943
2944 const struct dpif_class dpif_netdev_class = {
2945     "netdev",
2946     dpif_netdev_enumerate,
2947     dpif_netdev_port_open_type,
2948     dpif_netdev_open,
2949     dpif_netdev_close,
2950     dpif_netdev_destroy,
2951     dpif_netdev_run,
2952     dpif_netdev_wait,
2953     dpif_netdev_get_stats,
2954     dpif_netdev_port_add,
2955     dpif_netdev_port_del,
2956     dpif_netdev_port_query_by_number,
2957     dpif_netdev_port_query_by_name,
2958     NULL,                       /* port_get_pid */
2959     dpif_netdev_port_dump_start,
2960     dpif_netdev_port_dump_next,
2961     dpif_netdev_port_dump_done,
2962     dpif_netdev_port_poll,
2963     dpif_netdev_port_poll_wait,
2964     dpif_netdev_flow_flush,
2965     dpif_netdev_flow_dump_create,
2966     dpif_netdev_flow_dump_destroy,
2967     dpif_netdev_flow_dump_thread_create,
2968     dpif_netdev_flow_dump_thread_destroy,
2969     dpif_netdev_flow_dump_next,
2970     dpif_netdev_operate,
2971     NULL,                       /* recv_set */
2972     NULL,                       /* handlers_set */
2973     dpif_netdev_pmd_set,
2974     dpif_netdev_queue_to_priority,
2975     NULL,                       /* recv */
2976     NULL,                       /* recv_wait */
2977     NULL,                       /* recv_purge */
2978     dpif_netdev_register_upcall_cb,
2979     dpif_netdev_enable_upcall,
2980     dpif_netdev_disable_upcall,
2981 };
2982
2983 static void
2984 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2985                               const char *argv[], void *aux OVS_UNUSED)
2986 {
2987     struct dp_netdev_port *old_port;
2988     struct dp_netdev_port *new_port;
2989     struct dp_netdev *dp;
2990     odp_port_t port_no;
2991
2992     ovs_mutex_lock(&dp_netdev_mutex);
2993     dp = shash_find_data(&dp_netdevs, argv[1]);
2994     if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2995         ovs_mutex_unlock(&dp_netdev_mutex);
2996         unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2997         return;
2998     }
2999     ovs_refcount_ref(&dp->ref_cnt);
3000     ovs_mutex_unlock(&dp_netdev_mutex);
3001
3002     ovs_mutex_lock(&dp->port_mutex);
3003     if (get_port_by_name(dp, argv[2], &old_port)) {
3004         unixctl_command_reply_error(conn, "unknown port");
3005         goto exit;
3006     }
3007
3008     port_no = u32_to_odp(atoi(argv[3]));
3009     if (!port_no || port_no == ODPP_NONE) {
3010         unixctl_command_reply_error(conn, "bad port number");
3011         goto exit;
3012     }
3013     if (dp_netdev_lookup_port(dp, port_no)) {
3014         unixctl_command_reply_error(conn, "port number already in use");
3015         goto exit;
3016     }
3017
3018     /* Remove old port. */
3019     cmap_remove(&dp->ports, &old_port->node, hash_port_no(old_port->port_no));
3020     ovsrcu_postpone(free, old_port);
3021
3022     /* Insert new port (cmap semantics mean we cannot re-insert 'old_port'). */
3023     new_port = xmemdup(old_port, sizeof *old_port);
3024     new_port->port_no = port_no;
3025     cmap_insert(&dp->ports, &new_port->node, hash_port_no(port_no));
3026
3027     seq_change(dp->port_seq);
3028     unixctl_command_reply(conn, NULL);
3029
3030 exit:
3031     ovs_mutex_unlock(&dp->port_mutex);
3032     dp_netdev_unref(dp);
3033 }
3034
3035 static void
3036 dpif_dummy_delete_port(struct unixctl_conn *conn, int argc OVS_UNUSED,
3037                        const char *argv[], void *aux OVS_UNUSED)
3038 {
3039     struct dp_netdev_port *port;
3040     struct dp_netdev *dp;
3041
3042     ovs_mutex_lock(&dp_netdev_mutex);
3043     dp = shash_find_data(&dp_netdevs, argv[1]);
3044     if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
3045         ovs_mutex_unlock(&dp_netdev_mutex);
3046         unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
3047         return;
3048     }
3049     ovs_refcount_ref(&dp->ref_cnt);
3050     ovs_mutex_unlock(&dp_netdev_mutex);
3051
3052     ovs_mutex_lock(&dp->port_mutex);
3053     if (get_port_by_name(dp, argv[2], &port)) {
3054         unixctl_command_reply_error(conn, "unknown port");
3055     } else if (port->port_no == ODPP_LOCAL) {
3056         unixctl_command_reply_error(conn, "can't delete local port");
3057     } else {
3058         do_del_port(dp, port);
3059         unixctl_command_reply(conn, NULL);
3060     }
3061     ovs_mutex_unlock(&dp->port_mutex);
3062
3063     dp_netdev_unref(dp);
3064 }
3065
3066 static void
3067 dpif_dummy_register__(const char *type)
3068 {
3069     struct dpif_class *class;
3070
3071     class = xmalloc(sizeof *class);
3072     *class = dpif_netdev_class;
3073     class->type = xstrdup(type);
3074     dp_register_provider(class);
3075 }
3076
3077 void
3078 dpif_dummy_register(bool override)
3079 {
3080     if (override) {
3081         struct sset types;
3082         const char *type;
3083
3084         sset_init(&types);
3085         dp_enumerate_types(&types);
3086         SSET_FOR_EACH (type, &types) {
3087             if (!dp_unregister_provider(type)) {
3088                 dpif_dummy_register__(type);
3089             }
3090         }
3091         sset_destroy(&types);
3092     }
3093
3094     dpif_dummy_register__("dummy");
3095
3096     unixctl_command_register("dpif-dummy/change-port-number",
3097                              "dp port new-number",
3098                              3, 3, dpif_dummy_change_port_number, NULL);
3099     unixctl_command_register("dpif-dummy/delete-port", "dp port",
3100                              2, 2, dpif_dummy_delete_port, NULL);
3101 }