rhel: Add Patch Port support to initscripts
[sliver-openvswitch.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.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 "csum.h"
36 #include "dpif.h"
37 #include "dpif-provider.h"
38 #include "dummy.h"
39 #include "dynamic-string.h"
40 #include "flow.h"
41 #include "hmap.h"
42 #include "latch.h"
43 #include "list.h"
44 #include "meta-flow.h"
45 #include "netdev.h"
46 #include "netdev-dpdk.h"
47 #include "netdev-vport.h"
48 #include "netlink.h"
49 #include "odp-execute.h"
50 #include "odp-util.h"
51 #include "ofp-print.h"
52 #include "ofpbuf.h"
53 #include "ovs-rcu.h"
54 #include "packets.h"
55 #include "poll-loop.h"
56 #include "random.h"
57 #include "seq.h"
58 #include "shash.h"
59 #include "sset.h"
60 #include "timeval.h"
61 #include "unixctl.h"
62 #include "util.h"
63 #include "vlog.h"
64
65 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
66
67 /* By default, choose a priority in the middle. */
68 #define NETDEV_RULE_PRIORITY 0x8000
69
70 #define NR_THREADS 1
71
72 /* Configuration parameters. */
73 enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
74
75 /* Queues. */
76 enum { MAX_QUEUE_LEN = 128 };   /* Maximum number of packets per queue. */
77 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
78 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
79
80 /* Protects against changes to 'dp_netdevs'. */
81 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
82
83 /* Contains all 'struct dp_netdev's. */
84 static struct shash dp_netdevs OVS_GUARDED_BY(dp_netdev_mutex)
85     = SHASH_INITIALIZER(&dp_netdevs);
86
87 struct dp_netdev_upcall {
88     struct dpif_upcall upcall;  /* Queued upcall information. */
89     struct ofpbuf buf;          /* ofpbuf instance for upcall.packet. */
90 };
91
92 /* A queue passing packets from a struct dp_netdev to its clients (handlers).
93  *
94  *
95  * Thread-safety
96  * =============
97  *
98  * Any access at all requires the owning 'dp_netdev''s queue_rwlock and
99  * its own mutex. */
100 struct dp_netdev_queue {
101     struct ovs_mutex mutex;
102     struct seq *seq;      /* Incremented whenever a packet is queued. */
103     struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN] OVS_GUARDED;
104     unsigned int head OVS_GUARDED;
105     unsigned int tail OVS_GUARDED;
106 };
107
108 /* Datapath based on the network device interface from netdev.h.
109  *
110  *
111  * Thread-safety
112  * =============
113  *
114  * Some members, marked 'const', are immutable.  Accessing other members
115  * requires synchronization, as noted in more detail below.
116  *
117  * Acquisition order is, from outermost to innermost:
118  *
119  *    dp_netdev_mutex (global)
120  *    port_rwlock
121  *    flow_mutex
122  *    cls.rwlock
123  *    queue_rwlock
124  */
125 struct dp_netdev {
126     const struct dpif_class *const class;
127     const char *const name;
128     struct ovs_refcount ref_cnt;
129     atomic_flag destroyed;
130
131     /* Flows.
132      *
133      * Readers of 'cls' and 'flow_table' must take a 'cls->rwlock' read lock.
134      *
135      * Writers of 'cls' and 'flow_table' must take the 'flow_mutex' and then
136      * the 'cls->rwlock' write lock.  (The outer 'flow_mutex' allows writers to
137      * atomically perform multiple operations on 'cls' and 'flow_table'.)
138      */
139     struct ovs_mutex flow_mutex;
140     struct classifier cls;      /* Classifier.  Protected by cls.rwlock. */
141     struct hmap flow_table OVS_GUARDED; /* Flow table. */
142
143     /* Queues.
144      *
145      * 'queue_rwlock' protects the modification of 'handler_queues' and
146      * 'n_handlers'.  The queue elements are protected by its
147      * 'handler_queues''s mutex. */
148     struct fat_rwlock queue_rwlock;
149     struct dp_netdev_queue *handler_queues;
150     uint32_t n_handlers;
151
152     /* Statistics.
153      *
154      * ovsthread_stats is internally synchronized. */
155     struct ovsthread_stats stats; /* Contains 'struct dp_netdev_stats *'. */
156
157     /* Ports.
158      *
159      * Any lookup into 'ports' or any access to the dp_netdev_ports found
160      * through 'ports' requires taking 'port_rwlock'. */
161     struct ovs_rwlock port_rwlock;
162     struct hmap ports OVS_GUARDED;
163     struct seq *port_seq;       /* Incremented whenever a port changes. */
164
165     /* Forwarding threads. */
166     struct latch exit_latch;
167     struct pmd_thread *pmd_threads;
168     size_t n_pmd_threads;
169     int pmd_count;
170 };
171
172 static struct dp_netdev_port *dp_netdev_lookup_port(const struct dp_netdev *dp,
173                                                     odp_port_t)
174     OVS_REQ_RDLOCK(dp->port_rwlock);
175
176 enum dp_stat_type {
177     DP_STAT_HIT,                /* Packets that matched in the flow table. */
178     DP_STAT_MISS,               /* Packets that did not match. */
179     DP_STAT_LOST,               /* Packets not passed up to the client. */
180     DP_N_STATS
181 };
182
183 /* Contained by struct dp_netdev's 'stats' member.  */
184 struct dp_netdev_stats {
185     struct ovs_mutex mutex;          /* Protects 'n'. */
186
187     /* Indexed by DP_STAT_*, protected by 'mutex'. */
188     unsigned long long int n[DP_N_STATS] OVS_GUARDED;
189 };
190
191
192 /* A port in a netdev-based datapath. */
193 struct dp_netdev_port {
194     struct hmap_node node;      /* Node in dp_netdev's 'ports'. */
195     odp_port_t port_no;
196     struct netdev *netdev;
197     struct netdev_saved_flags *sf;
198     struct netdev_rxq **rxq;
199     struct ovs_refcount ref_cnt;
200     char *type;                 /* Port type as requested by user. */
201 };
202
203 /* A flow in dp_netdev's 'flow_table'.
204  *
205  *
206  * Thread-safety
207  * =============
208  *
209  * Except near the beginning or ending of its lifespan, rule 'rule' belongs to
210  * its dp_netdev's classifier.  The text below calls this classifier 'cls'.
211  *
212  * Motivation
213  * ----------
214  *
215  * The thread safety rules described here for "struct dp_netdev_flow" are
216  * motivated by two goals:
217  *
218  *    - Prevent threads that read members of "struct dp_netdev_flow" from
219  *      reading bad data due to changes by some thread concurrently modifying
220  *      those members.
221  *
222  *    - Prevent two threads making changes to members of a given "struct
223  *      dp_netdev_flow" from interfering with each other.
224  *
225  *
226  * Rules
227  * -----
228  *
229  * A flow 'flow' may be accessed without a risk of being freed by code that
230  * holds a read-lock or write-lock on 'cls->rwlock' or that owns a reference to
231  * 'flow->ref_cnt' (or both).  Code that needs to hold onto a flow for a while
232  * should take 'cls->rwlock', find the flow it needs, increment 'flow->ref_cnt'
233  * with dpif_netdev_flow_ref(), and drop 'cls->rwlock'.
234  *
235  * 'flow->ref_cnt' protects 'flow' from being freed.  It doesn't protect the
236  * flow from being deleted from 'cls' (that's 'cls->rwlock') and it doesn't
237  * protect members of 'flow' from modification (that's 'flow->mutex').
238  *
239  * 'flow->mutex' protects the members of 'flow' from modification.  It doesn't
240  * protect the flow from being deleted from 'cls' (that's 'cls->rwlock') and it
241  * doesn't prevent the flow from being freed (that's 'flow->ref_cnt').
242  *
243  * Some members, marked 'const', are immutable.  Accessing other members
244  * requires synchronization, as noted in more detail below.
245  */
246 struct dp_netdev_flow {
247     /* Packet classification. */
248     const struct cls_rule cr;   /* In owning dp_netdev's 'cls'. */
249
250     /* Hash table index by unmasked flow. */
251     const struct hmap_node node; /* In owning dp_netdev's 'flow_table'. */
252     const struct flow flow;      /* The flow that created this entry. */
253
254     /* Protects members marked OVS_GUARDED.
255      *
256      * Acquire after datapath's flow_mutex. */
257     struct ovs_mutex mutex OVS_ACQ_AFTER(dp_netdev_mutex);
258
259     /* Statistics.
260      *
261      * Reading or writing these members requires 'mutex'. */
262     struct ovsthread_stats stats; /* Contains "struct dp_netdev_flow_stats". */
263
264     /* Actions.
265      *
266      * Reading 'actions' requires 'mutex'.
267      * Writing 'actions' requires 'mutex' and (to allow for transactions) the
268      * datapath's flow_mutex. */
269     OVSRCU_TYPE(struct dp_netdev_actions *) actions;
270 };
271
272 static void dp_netdev_flow_free(struct dp_netdev_flow *);
273
274 /* Contained by struct dp_netdev_flow's 'stats' member.  */
275 struct dp_netdev_flow_stats {
276     struct ovs_mutex mutex;         /* Guards all the other members. */
277
278     long long int used OVS_GUARDED; /* Last used time, in monotonic msecs. */
279     long long int packet_count OVS_GUARDED; /* Number of packets matched. */
280     long long int byte_count OVS_GUARDED;   /* Number of bytes matched. */
281     uint16_t tcp_flags OVS_GUARDED; /* Bitwise-OR of seen tcp_flags values. */
282 };
283
284 /* A set of datapath actions within a "struct dp_netdev_flow".
285  *
286  *
287  * Thread-safety
288  * =============
289  *
290  * A struct dp_netdev_actions 'actions' may be accessed without a risk of being
291  * freed by code that holds a read-lock or write-lock on 'flow->mutex' (where
292  * 'flow' is the dp_netdev_flow for which 'flow->actions == actions') or that
293  * owns a reference to 'actions->ref_cnt' (or both). */
294 struct dp_netdev_actions {
295     /* These members are immutable: they do not change during the struct's
296      * lifetime.  */
297     struct nlattr *actions;     /* Sequence of OVS_ACTION_ATTR_* attributes. */
298     unsigned int size;          /* Size of 'actions', in bytes. */
299 };
300
301 struct dp_netdev_actions *dp_netdev_actions_create(const struct nlattr *,
302                                                    size_t);
303 struct dp_netdev_actions *dp_netdev_flow_get_actions(
304     const struct dp_netdev_flow *);
305 static void dp_netdev_actions_free(struct dp_netdev_actions *);
306
307 /* PMD: Poll modes drivers.  PMD accesses devices via polling to eliminate
308  * the performance overhead of interrupt processing.  Therefore netdev can
309  * not implement rx-wait for these devices.  dpif-netdev needs to poll
310  * these device to check for recv buffer.  pmd-thread does polling for
311  * devices assigned to itself thread.
312  *
313  * DPDK used PMD for accessing NIC.
314  *
315  * A thread that receives packets from PMD ports, looks them up in the flow
316  * table, and executes the actions it finds.
317  **/
318 struct pmd_thread {
319     struct dp_netdev *dp;
320     pthread_t thread;
321     int id;
322     atomic_uint change_seq;
323     char *name;
324 };
325
326 /* Interface to netdev-based datapath. */
327 struct dpif_netdev {
328     struct dpif dpif;
329     struct dp_netdev *dp;
330     uint64_t last_port_seq;
331 };
332
333 static int get_port_by_number(struct dp_netdev *dp, odp_port_t port_no,
334                               struct dp_netdev_port **portp)
335     OVS_REQ_RDLOCK(dp->port_rwlock);
336 static int get_port_by_name(struct dp_netdev *dp, const char *devname,
337                             struct dp_netdev_port **portp)
338     OVS_REQ_RDLOCK(dp->port_rwlock);
339 static void dp_netdev_free(struct dp_netdev *)
340     OVS_REQUIRES(dp_netdev_mutex);
341 static void dp_netdev_flow_flush(struct dp_netdev *);
342 static int do_add_port(struct dp_netdev *dp, const char *devname,
343                        const char *type, odp_port_t port_no)
344     OVS_REQ_WRLOCK(dp->port_rwlock);
345 static int do_del_port(struct dp_netdev *dp, odp_port_t port_no)
346     OVS_REQ_WRLOCK(dp->port_rwlock);
347 static void dp_netdev_destroy_all_queues(struct dp_netdev *dp)
348     OVS_REQ_WRLOCK(dp->queue_rwlock);
349 static int dpif_netdev_open(const struct dpif_class *, const char *name,
350                             bool create, struct dpif **);
351 static int dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *,
352                                       int queue_no, int type,
353                                       const struct flow *,
354                                       const struct nlattr *userdata);
355 static void dp_netdev_execute_actions(struct dp_netdev *dp,
356                                       const struct flow *, struct ofpbuf *, bool may_steal,
357                                       struct pkt_metadata *,
358                                       const struct nlattr *actions,
359                                       size_t actions_len);
360 static void dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
361                                  struct pkt_metadata *);
362
363 static void dp_netdev_set_pmd_threads(struct dp_netdev *, int n);
364
365 static struct dpif_netdev *
366 dpif_netdev_cast(const struct dpif *dpif)
367 {
368     ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
369     return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
370 }
371
372 static struct dp_netdev *
373 get_dp_netdev(const struct dpif *dpif)
374 {
375     return dpif_netdev_cast(dpif)->dp;
376 }
377
378 static int
379 dpif_netdev_enumerate(struct sset *all_dps)
380 {
381     struct shash_node *node;
382
383     ovs_mutex_lock(&dp_netdev_mutex);
384     SHASH_FOR_EACH(node, &dp_netdevs) {
385         sset_add(all_dps, node->name);
386     }
387     ovs_mutex_unlock(&dp_netdev_mutex);
388
389     return 0;
390 }
391
392 static bool
393 dpif_netdev_class_is_dummy(const struct dpif_class *class)
394 {
395     return class != &dpif_netdev_class;
396 }
397
398 static const char *
399 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
400 {
401     return strcmp(type, "internal") ? type
402                   : dpif_netdev_class_is_dummy(class) ? "dummy"
403                   : "tap";
404 }
405
406 static struct dpif *
407 create_dpif_netdev(struct dp_netdev *dp)
408 {
409     uint16_t netflow_id = hash_string(dp->name, 0);
410     struct dpif_netdev *dpif;
411
412     ovs_refcount_ref(&dp->ref_cnt);
413
414     dpif = xmalloc(sizeof *dpif);
415     dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
416     dpif->dp = dp;
417     dpif->last_port_seq = seq_read(dp->port_seq);
418
419     return &dpif->dpif;
420 }
421
422 /* Choose an unused, non-zero port number and return it on success.
423  * Return ODPP_NONE on failure. */
424 static odp_port_t
425 choose_port(struct dp_netdev *dp, const char *name)
426     OVS_REQ_RDLOCK(dp->port_rwlock)
427 {
428     uint32_t port_no;
429
430     if (dp->class != &dpif_netdev_class) {
431         const char *p;
432         int start_no = 0;
433
434         /* If the port name begins with "br", start the number search at
435          * 100 to make writing tests easier. */
436         if (!strncmp(name, "br", 2)) {
437             start_no = 100;
438         }
439
440         /* If the port name contains a number, try to assign that port number.
441          * This can make writing unit tests easier because port numbers are
442          * predictable. */
443         for (p = name; *p != '\0'; p++) {
444             if (isdigit((unsigned char) *p)) {
445                 port_no = start_no + strtol(p, NULL, 10);
446                 if (port_no > 0 && port_no != odp_to_u32(ODPP_NONE)
447                     && !dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
448                     return u32_to_odp(port_no);
449                 }
450                 break;
451             }
452         }
453     }
454
455     for (port_no = 1; port_no <= UINT16_MAX; port_no++) {
456         if (!dp_netdev_lookup_port(dp, u32_to_odp(port_no))) {
457             return u32_to_odp(port_no);
458         }
459     }
460
461     return ODPP_NONE;
462 }
463
464 static int
465 create_dp_netdev(const char *name, const struct dpif_class *class,
466                  struct dp_netdev **dpp)
467     OVS_REQUIRES(dp_netdev_mutex)
468 {
469     struct dp_netdev *dp;
470     int error;
471
472     dp = xzalloc(sizeof *dp);
473     shash_add(&dp_netdevs, name, dp);
474
475     *CONST_CAST(const struct dpif_class **, &dp->class) = class;
476     *CONST_CAST(const char **, &dp->name) = xstrdup(name);
477     ovs_refcount_init(&dp->ref_cnt);
478     atomic_flag_clear(&dp->destroyed);
479
480     ovs_mutex_init(&dp->flow_mutex);
481     classifier_init(&dp->cls, NULL);
482     hmap_init(&dp->flow_table);
483
484     fat_rwlock_init(&dp->queue_rwlock);
485
486     ovsthread_stats_init(&dp->stats);
487
488     ovs_rwlock_init(&dp->port_rwlock);
489     hmap_init(&dp->ports);
490     dp->port_seq = seq_create();
491     latch_init(&dp->exit_latch);
492
493     ovs_rwlock_wrlock(&dp->port_rwlock);
494     error = do_add_port(dp, name, "internal", ODPP_LOCAL);
495     ovs_rwlock_unlock(&dp->port_rwlock);
496     if (error) {
497         dp_netdev_free(dp);
498         return error;
499     }
500
501     *dpp = dp;
502     return 0;
503 }
504
505 static int
506 dpif_netdev_open(const struct dpif_class *class, const char *name,
507                  bool create, struct dpif **dpifp)
508 {
509     struct dp_netdev *dp;
510     int error;
511
512     ovs_mutex_lock(&dp_netdev_mutex);
513     dp = shash_find_data(&dp_netdevs, name);
514     if (!dp) {
515         error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
516     } else {
517         error = (dp->class != class ? EINVAL
518                  : create ? EEXIST
519                  : 0);
520     }
521     if (!error) {
522         *dpifp = create_dpif_netdev(dp);
523     }
524     ovs_mutex_unlock(&dp_netdev_mutex);
525
526     return error;
527 }
528
529 static void
530 dp_netdev_purge_queues(struct dp_netdev *dp)
531     OVS_REQ_WRLOCK(dp->queue_rwlock)
532 {
533     int i;
534
535     for (i = 0; i < dp->n_handlers; i++) {
536         struct dp_netdev_queue *q = &dp->handler_queues[i];
537
538         ovs_mutex_lock(&q->mutex);
539         while (q->tail != q->head) {
540             struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
541             ofpbuf_uninit(&u->upcall.packet);
542             ofpbuf_uninit(&u->buf);
543         }
544         ovs_mutex_unlock(&q->mutex);
545     }
546 }
547
548 /* Requires dp_netdev_mutex so that we can't get a new reference to 'dp'
549  * through the 'dp_netdevs' shash while freeing 'dp'. */
550 static void
551 dp_netdev_free(struct dp_netdev *dp)
552     OVS_REQUIRES(dp_netdev_mutex)
553 {
554     struct dp_netdev_port *port, *next;
555     struct dp_netdev_stats *bucket;
556     int i;
557
558     shash_find_and_delete(&dp_netdevs, dp->name);
559
560     dp_netdev_set_pmd_threads(dp, 0);
561     free(dp->pmd_threads);
562
563     dp_netdev_flow_flush(dp);
564     ovs_rwlock_wrlock(&dp->port_rwlock);
565     HMAP_FOR_EACH_SAFE (port, next, node, &dp->ports) {
566         do_del_port(dp, port->port_no);
567     }
568     ovs_rwlock_unlock(&dp->port_rwlock);
569
570     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
571         ovs_mutex_destroy(&bucket->mutex);
572         free_cacheline(bucket);
573     }
574     ovsthread_stats_destroy(&dp->stats);
575
576     fat_rwlock_wrlock(&dp->queue_rwlock);
577     dp_netdev_destroy_all_queues(dp);
578     fat_rwlock_unlock(&dp->queue_rwlock);
579
580     fat_rwlock_destroy(&dp->queue_rwlock);
581
582     classifier_destroy(&dp->cls);
583     hmap_destroy(&dp->flow_table);
584     ovs_mutex_destroy(&dp->flow_mutex);
585     seq_destroy(dp->port_seq);
586     hmap_destroy(&dp->ports);
587     latch_destroy(&dp->exit_latch);
588     free(CONST_CAST(char *, dp->name));
589     free(dp);
590 }
591
592 static void
593 dp_netdev_unref(struct dp_netdev *dp)
594 {
595     if (dp) {
596         /* Take dp_netdev_mutex so that, if dp->ref_cnt falls to zero, we can't
597          * get a new reference to 'dp' through the 'dp_netdevs' shash. */
598         ovs_mutex_lock(&dp_netdev_mutex);
599         if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
600             dp_netdev_free(dp);
601         }
602         ovs_mutex_unlock(&dp_netdev_mutex);
603     }
604 }
605
606 static void
607 dpif_netdev_close(struct dpif *dpif)
608 {
609     struct dp_netdev *dp = get_dp_netdev(dpif);
610
611     dp_netdev_unref(dp);
612     free(dpif);
613 }
614
615 static int
616 dpif_netdev_destroy(struct dpif *dpif)
617 {
618     struct dp_netdev *dp = get_dp_netdev(dpif);
619
620     if (!atomic_flag_test_and_set(&dp->destroyed)) {
621         if (ovs_refcount_unref(&dp->ref_cnt) == 1) {
622             /* Can't happen: 'dpif' still owns a reference to 'dp'. */
623             OVS_NOT_REACHED();
624         }
625     }
626
627     return 0;
628 }
629
630 static int
631 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
632 {
633     struct dp_netdev *dp = get_dp_netdev(dpif);
634     struct dp_netdev_stats *bucket;
635     size_t i;
636
637     fat_rwlock_rdlock(&dp->cls.rwlock);
638     stats->n_flows = hmap_count(&dp->flow_table);
639     fat_rwlock_unlock(&dp->cls.rwlock);
640
641     stats->n_hit = stats->n_missed = stats->n_lost = 0;
642     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &dp->stats) {
643         ovs_mutex_lock(&bucket->mutex);
644         stats->n_hit += bucket->n[DP_STAT_HIT];
645         stats->n_missed += bucket->n[DP_STAT_MISS];
646         stats->n_lost += bucket->n[DP_STAT_LOST];
647         ovs_mutex_unlock(&bucket->mutex);
648     }
649     stats->n_masks = UINT32_MAX;
650     stats->n_mask_hit = UINT64_MAX;
651
652     return 0;
653 }
654
655 static void
656 dp_netdev_reload_pmd_threads(struct dp_netdev *dp)
657 {
658     int i;
659
660     for (i = 0; i < dp->n_pmd_threads; i++) {
661         struct pmd_thread *f = &dp->pmd_threads[i];
662         int id;
663
664         atomic_add(&f->change_seq, 1, &id);
665    }
666 }
667
668 static int
669 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
670             odp_port_t port_no)
671     OVS_REQ_WRLOCK(dp->port_rwlock)
672 {
673     struct netdev_saved_flags *sf;
674     struct dp_netdev_port *port;
675     struct netdev *netdev;
676     enum netdev_flags flags;
677     const char *open_type;
678     int error;
679     int i;
680
681     /* XXX reject devices already in some dp_netdev. */
682
683     /* Open and validate network device. */
684     open_type = dpif_netdev_port_open_type(dp->class, type);
685     error = netdev_open(devname, open_type, &netdev);
686     if (error) {
687         return error;
688     }
689     /* XXX reject non-Ethernet devices */
690
691     netdev_get_flags(netdev, &flags);
692     if (flags & NETDEV_LOOPBACK) {
693         VLOG_ERR("%s: cannot add a loopback device", devname);
694         netdev_close(netdev);
695         return EINVAL;
696     }
697
698     port = xzalloc(sizeof *port);
699     port->port_no = port_no;
700     port->netdev = netdev;
701     port->rxq = xmalloc(sizeof *port->rxq * netdev_n_rxq(netdev));
702     port->type = xstrdup(type);
703     for (i = 0; i < netdev_n_rxq(netdev); i++) {
704         error = netdev_rxq_open(netdev, &port->rxq[i], i);
705         if (error
706             && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
707             VLOG_ERR("%s: cannot receive packets on this network device (%s)",
708                      devname, ovs_strerror(errno));
709             netdev_close(netdev);
710             return error;
711         }
712     }
713
714     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
715     if (error) {
716         for (i = 0; i < netdev_n_rxq(netdev); i++) {
717             netdev_rxq_close(port->rxq[i]);
718         }
719         netdev_close(netdev);
720         free(port->rxq);
721         free(port);
722         return error;
723     }
724     port->sf = sf;
725
726     if (netdev_is_pmd(netdev)) {
727         dp->pmd_count++;
728         dp_netdev_set_pmd_threads(dp, NR_THREADS);
729         dp_netdev_reload_pmd_threads(dp);
730     }
731     ovs_refcount_init(&port->ref_cnt);
732
733     hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
734     seq_change(dp->port_seq);
735
736     return 0;
737 }
738
739 static int
740 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
741                      odp_port_t *port_nop)
742 {
743     struct dp_netdev *dp = get_dp_netdev(dpif);
744     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
745     const char *dpif_port;
746     odp_port_t port_no;
747     int error;
748
749     ovs_rwlock_wrlock(&dp->port_rwlock);
750     dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
751     if (*port_nop != ODPP_NONE) {
752         port_no = *port_nop;
753         error = dp_netdev_lookup_port(dp, *port_nop) ? EBUSY : 0;
754     } else {
755         port_no = choose_port(dp, dpif_port);
756         error = port_no == ODPP_NONE ? EFBIG : 0;
757     }
758     if (!error) {
759         *port_nop = port_no;
760         error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
761     }
762     ovs_rwlock_unlock(&dp->port_rwlock);
763
764     return error;
765 }
766
767 static int
768 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
769 {
770     struct dp_netdev *dp = get_dp_netdev(dpif);
771     int error;
772
773     ovs_rwlock_wrlock(&dp->port_rwlock);
774     error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
775     ovs_rwlock_unlock(&dp->port_rwlock);
776
777     return error;
778 }
779
780 static bool
781 is_valid_port_number(odp_port_t port_no)
782 {
783     return port_no != ODPP_NONE;
784 }
785
786 static struct dp_netdev_port *
787 dp_netdev_lookup_port(const struct dp_netdev *dp, odp_port_t port_no)
788     OVS_REQ_RDLOCK(dp->port_rwlock)
789 {
790     struct dp_netdev_port *port;
791
792     HMAP_FOR_EACH_IN_BUCKET (port, node, hash_int(odp_to_u32(port_no), 0),
793                              &dp->ports) {
794         if (port->port_no == port_no) {
795             return port;
796         }
797     }
798     return NULL;
799 }
800
801 static int
802 get_port_by_number(struct dp_netdev *dp,
803                    odp_port_t port_no, struct dp_netdev_port **portp)
804     OVS_REQ_RDLOCK(dp->port_rwlock)
805 {
806     if (!is_valid_port_number(port_no)) {
807         *portp = NULL;
808         return EINVAL;
809     } else {
810         *portp = dp_netdev_lookup_port(dp, port_no);
811         return *portp ? 0 : ENOENT;
812     }
813 }
814
815 static void
816 port_ref(struct dp_netdev_port *port)
817 {
818     if (port) {
819         ovs_refcount_ref(&port->ref_cnt);
820     }
821 }
822
823 static void
824 port_unref(struct dp_netdev_port *port)
825 {
826     if (port && ovs_refcount_unref(&port->ref_cnt) == 1) {
827         int i;
828
829         netdev_close(port->netdev);
830         netdev_restore_flags(port->sf);
831
832         for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
833             netdev_rxq_close(port->rxq[i]);
834         }
835         free(port->type);
836         free(port);
837     }
838 }
839
840 static int
841 get_port_by_name(struct dp_netdev *dp,
842                  const char *devname, struct dp_netdev_port **portp)
843     OVS_REQ_RDLOCK(dp->port_rwlock)
844 {
845     struct dp_netdev_port *port;
846
847     HMAP_FOR_EACH (port, node, &dp->ports) {
848         if (!strcmp(netdev_get_name(port->netdev), devname)) {
849             *portp = port;
850             return 0;
851         }
852     }
853     return ENOENT;
854 }
855
856 static int
857 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
858     OVS_REQ_WRLOCK(dp->port_rwlock)
859 {
860     struct dp_netdev_port *port;
861     int error;
862
863     error = get_port_by_number(dp, port_no, &port);
864     if (error) {
865         return error;
866     }
867
868     hmap_remove(&dp->ports, &port->node);
869     seq_change(dp->port_seq);
870     if (netdev_is_pmd(port->netdev)) {
871         dp_netdev_reload_pmd_threads(dp);
872     }
873
874     port_unref(port);
875     return 0;
876 }
877
878 static void
879 answer_port_query(const struct dp_netdev_port *port,
880                   struct dpif_port *dpif_port)
881 {
882     dpif_port->name = xstrdup(netdev_get_name(port->netdev));
883     dpif_port->type = xstrdup(port->type);
884     dpif_port->port_no = port->port_no;
885 }
886
887 static int
888 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
889                                  struct dpif_port *dpif_port)
890 {
891     struct dp_netdev *dp = get_dp_netdev(dpif);
892     struct dp_netdev_port *port;
893     int error;
894
895     ovs_rwlock_rdlock(&dp->port_rwlock);
896     error = get_port_by_number(dp, port_no, &port);
897     if (!error && dpif_port) {
898         answer_port_query(port, dpif_port);
899     }
900     ovs_rwlock_unlock(&dp->port_rwlock);
901
902     return error;
903 }
904
905 static int
906 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
907                                struct dpif_port *dpif_port)
908 {
909     struct dp_netdev *dp = get_dp_netdev(dpif);
910     struct dp_netdev_port *port;
911     int error;
912
913     ovs_rwlock_rdlock(&dp->port_rwlock);
914     error = get_port_by_name(dp, devname, &port);
915     if (!error && dpif_port) {
916         answer_port_query(port, dpif_port);
917     }
918     ovs_rwlock_unlock(&dp->port_rwlock);
919
920     return error;
921 }
922
923 static void
924 dp_netdev_flow_free(struct dp_netdev_flow *flow)
925 {
926     struct dp_netdev_flow_stats *bucket;
927     size_t i;
928
929     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &flow->stats) {
930         ovs_mutex_destroy(&bucket->mutex);
931         free_cacheline(bucket);
932     }
933     ovsthread_stats_destroy(&flow->stats);
934
935     cls_rule_destroy(CONST_CAST(struct cls_rule *, &flow->cr));
936     dp_netdev_actions_free(dp_netdev_flow_get_actions(flow));
937     ovs_mutex_destroy(&flow->mutex);
938     free(flow);
939 }
940
941 static void
942 dp_netdev_remove_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
943     OVS_REQ_WRLOCK(dp->cls.rwlock)
944     OVS_REQUIRES(dp->flow_mutex)
945 {
946     struct cls_rule *cr = CONST_CAST(struct cls_rule *, &flow->cr);
947     struct hmap_node *node = CONST_CAST(struct hmap_node *, &flow->node);
948
949     classifier_remove(&dp->cls, cr);
950     hmap_remove(&dp->flow_table, node);
951     ovsrcu_postpone(dp_netdev_flow_free, flow);
952 }
953
954 static void
955 dp_netdev_flow_flush(struct dp_netdev *dp)
956 {
957     struct dp_netdev_flow *netdev_flow, *next;
958
959     ovs_mutex_lock(&dp->flow_mutex);
960     fat_rwlock_wrlock(&dp->cls.rwlock);
961     HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
962         dp_netdev_remove_flow(dp, netdev_flow);
963     }
964     fat_rwlock_unlock(&dp->cls.rwlock);
965     ovs_mutex_unlock(&dp->flow_mutex);
966 }
967
968 static int
969 dpif_netdev_flow_flush(struct dpif *dpif)
970 {
971     struct dp_netdev *dp = get_dp_netdev(dpif);
972
973     dp_netdev_flow_flush(dp);
974     return 0;
975 }
976
977 struct dp_netdev_port_state {
978     uint32_t bucket;
979     uint32_t offset;
980     char *name;
981 };
982
983 static int
984 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
985 {
986     *statep = xzalloc(sizeof(struct dp_netdev_port_state));
987     return 0;
988 }
989
990 static int
991 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
992                            struct dpif_port *dpif_port)
993 {
994     struct dp_netdev_port_state *state = state_;
995     struct dp_netdev *dp = get_dp_netdev(dpif);
996     struct hmap_node *node;
997     int retval;
998
999     ovs_rwlock_rdlock(&dp->port_rwlock);
1000     node = hmap_at_position(&dp->ports, &state->bucket, &state->offset);
1001     if (node) {
1002         struct dp_netdev_port *port;
1003
1004         port = CONTAINER_OF(node, struct dp_netdev_port, node);
1005
1006         free(state->name);
1007         state->name = xstrdup(netdev_get_name(port->netdev));
1008         dpif_port->name = state->name;
1009         dpif_port->type = port->type;
1010         dpif_port->port_no = port->port_no;
1011
1012         retval = 0;
1013     } else {
1014         retval = EOF;
1015     }
1016     ovs_rwlock_unlock(&dp->port_rwlock);
1017
1018     return retval;
1019 }
1020
1021 static int
1022 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1023 {
1024     struct dp_netdev_port_state *state = state_;
1025     free(state->name);
1026     free(state);
1027     return 0;
1028 }
1029
1030 static int
1031 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
1032 {
1033     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1034     uint64_t new_port_seq;
1035     int error;
1036
1037     new_port_seq = seq_read(dpif->dp->port_seq);
1038     if (dpif->last_port_seq != new_port_seq) {
1039         dpif->last_port_seq = new_port_seq;
1040         error = ENOBUFS;
1041     } else {
1042         error = EAGAIN;
1043     }
1044
1045     return error;
1046 }
1047
1048 static void
1049 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
1050 {
1051     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
1052
1053     seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
1054 }
1055
1056 static struct dp_netdev_flow *
1057 dp_netdev_flow_cast(const struct cls_rule *cr)
1058 {
1059     return cr ? CONTAINER_OF(cr, struct dp_netdev_flow, cr) : NULL;
1060 }
1061
1062 static struct dp_netdev_flow *
1063 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *flow)
1064     OVS_EXCLUDED(dp->cls.rwlock)
1065 {
1066     struct dp_netdev_flow *netdev_flow;
1067
1068     fat_rwlock_rdlock(&dp->cls.rwlock);
1069     netdev_flow = dp_netdev_flow_cast(classifier_lookup(&dp->cls, flow, NULL));
1070     fat_rwlock_unlock(&dp->cls.rwlock);
1071
1072     return netdev_flow;
1073 }
1074
1075 static struct dp_netdev_flow *
1076 dp_netdev_find_flow(const struct dp_netdev *dp, const struct flow *flow)
1077     OVS_REQ_RDLOCK(dp->cls.rwlock)
1078 {
1079     struct dp_netdev_flow *netdev_flow;
1080
1081     HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(flow, 0),
1082                              &dp->flow_table) {
1083         if (flow_equal(&netdev_flow->flow, flow)) {
1084             return netdev_flow;
1085         }
1086     }
1087
1088     return NULL;
1089 }
1090
1091 static void
1092 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
1093                     struct dpif_flow_stats *stats)
1094 {
1095     struct dp_netdev_flow_stats *bucket;
1096     size_t i;
1097
1098     memset(stats, 0, sizeof *stats);
1099     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1100         ovs_mutex_lock(&bucket->mutex);
1101         stats->n_packets += bucket->packet_count;
1102         stats->n_bytes += bucket->byte_count;
1103         stats->used = MAX(stats->used, bucket->used);
1104         stats->tcp_flags |= bucket->tcp_flags;
1105         ovs_mutex_unlock(&bucket->mutex);
1106     }
1107 }
1108
1109 static int
1110 dpif_netdev_mask_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1111                               const struct nlattr *mask_key,
1112                               uint32_t mask_key_len, const struct flow *flow,
1113                               struct flow *mask)
1114 {
1115     if (mask_key_len) {
1116         enum odp_key_fitness fitness;
1117
1118         fitness = odp_flow_key_to_mask(mask_key, mask_key_len, mask, flow);
1119         if (fitness) {
1120             /* This should not happen: it indicates that
1121              * odp_flow_key_from_mask() and odp_flow_key_to_mask()
1122              * disagree on the acceptable form of a mask.  Log the problem
1123              * as an error, with enough details to enable debugging. */
1124             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1125
1126             if (!VLOG_DROP_ERR(&rl)) {
1127                 struct ds s;
1128
1129                 ds_init(&s);
1130                 odp_flow_format(key, key_len, mask_key, mask_key_len, NULL, &s,
1131                                 true);
1132                 VLOG_ERR("internal error parsing flow mask %s (%s)",
1133                          ds_cstr(&s), odp_key_fitness_to_string(fitness));
1134                 ds_destroy(&s);
1135             }
1136
1137             return EINVAL;
1138         }
1139         /* Force unwildcard the in_port. */
1140         mask->in_port.odp_port = u32_to_odp(UINT32_MAX);
1141     } else {
1142         enum mf_field_id id;
1143         /* No mask key, unwildcard everything except fields whose
1144          * prerequisities are not met. */
1145         memset(mask, 0x0, sizeof *mask);
1146
1147         for (id = 0; id < MFF_N_IDS; ++id) {
1148             /* Skip registers and metadata. */
1149             if (!(id >= MFF_REG0 && id < MFF_REG0 + FLOW_N_REGS)
1150                 && id != MFF_METADATA) {
1151                 const struct mf_field *mf = mf_from_id(id);
1152                 if (mf_are_prereqs_ok(mf, flow)) {
1153                     mf_mask_field(mf, mask);
1154                 }
1155             }
1156         }
1157     }
1158
1159     return 0;
1160 }
1161
1162 static int
1163 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
1164                               struct flow *flow)
1165 {
1166     odp_port_t in_port;
1167
1168     if (odp_flow_key_to_flow(key, key_len, flow)) {
1169         /* This should not happen: it indicates that odp_flow_key_from_flow()
1170          * and odp_flow_key_to_flow() disagree on the acceptable form of a
1171          * flow.  Log the problem as an error, with enough details to enable
1172          * debugging. */
1173         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1174
1175         if (!VLOG_DROP_ERR(&rl)) {
1176             struct ds s;
1177
1178             ds_init(&s);
1179             odp_flow_format(key, key_len, NULL, 0, NULL, &s, true);
1180             VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
1181             ds_destroy(&s);
1182         }
1183
1184         return EINVAL;
1185     }
1186
1187     in_port = flow->in_port.odp_port;
1188     if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
1189         return EINVAL;
1190     }
1191
1192     return 0;
1193 }
1194
1195 static int
1196 dpif_netdev_flow_get(const struct dpif *dpif,
1197                      const struct nlattr *nl_key, size_t nl_key_len,
1198                      struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
1199 {
1200     struct dp_netdev *dp = get_dp_netdev(dpif);
1201     struct dp_netdev_flow *netdev_flow;
1202     struct flow key;
1203     int error;
1204
1205     error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
1206     if (error) {
1207         return error;
1208     }
1209
1210     fat_rwlock_rdlock(&dp->cls.rwlock);
1211     netdev_flow = dp_netdev_find_flow(dp, &key);
1212     fat_rwlock_unlock(&dp->cls.rwlock);
1213
1214     if (netdev_flow) {
1215         if (stats) {
1216             get_dpif_flow_stats(netdev_flow, stats);
1217         }
1218
1219         if (actionsp) {
1220             struct dp_netdev_actions *actions;
1221
1222             actions = dp_netdev_flow_get_actions(netdev_flow);
1223             *actionsp = ofpbuf_clone_data(actions->actions, actions->size);
1224         }
1225      } else {
1226         error = ENOENT;
1227     }
1228
1229     return error;
1230 }
1231
1232 static int
1233 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *flow,
1234                    const struct flow_wildcards *wc,
1235                    const struct nlattr *actions,
1236                    size_t actions_len)
1237     OVS_REQUIRES(dp->flow_mutex)
1238 {
1239     struct dp_netdev_flow *netdev_flow;
1240     struct match match;
1241
1242     netdev_flow = xzalloc(sizeof *netdev_flow);
1243     *CONST_CAST(struct flow *, &netdev_flow->flow) = *flow;
1244
1245     ovs_mutex_init(&netdev_flow->mutex);
1246
1247     ovsthread_stats_init(&netdev_flow->stats);
1248
1249     ovsrcu_set(&netdev_flow->actions,
1250                dp_netdev_actions_create(actions, actions_len));
1251
1252     match_init(&match, flow, wc);
1253     cls_rule_init(CONST_CAST(struct cls_rule *, &netdev_flow->cr),
1254                   &match, NETDEV_RULE_PRIORITY);
1255     fat_rwlock_wrlock(&dp->cls.rwlock);
1256     classifier_insert(&dp->cls,
1257                       CONST_CAST(struct cls_rule *, &netdev_flow->cr));
1258     hmap_insert(&dp->flow_table,
1259                 CONST_CAST(struct hmap_node *, &netdev_flow->node),
1260                 flow_hash(flow, 0));
1261     fat_rwlock_unlock(&dp->cls.rwlock);
1262
1263     return 0;
1264 }
1265
1266 static void
1267 clear_stats(struct dp_netdev_flow *netdev_flow)
1268 {
1269     struct dp_netdev_flow_stats *bucket;
1270     size_t i;
1271
1272     OVSTHREAD_STATS_FOR_EACH_BUCKET (bucket, i, &netdev_flow->stats) {
1273         ovs_mutex_lock(&bucket->mutex);
1274         bucket->used = 0;
1275         bucket->packet_count = 0;
1276         bucket->byte_count = 0;
1277         bucket->tcp_flags = 0;
1278         ovs_mutex_unlock(&bucket->mutex);
1279     }
1280 }
1281
1282 static int
1283 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
1284 {
1285     struct dp_netdev *dp = get_dp_netdev(dpif);
1286     struct dp_netdev_flow *netdev_flow;
1287     struct flow flow;
1288     struct flow_wildcards wc;
1289     int error;
1290
1291     error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &flow);
1292     if (error) {
1293         return error;
1294     }
1295     error = dpif_netdev_mask_from_nlattrs(put->key, put->key_len,
1296                                           put->mask, put->mask_len,
1297                                           &flow, &wc.masks);
1298     if (error) {
1299         return error;
1300     }
1301
1302     ovs_mutex_lock(&dp->flow_mutex);
1303     netdev_flow = dp_netdev_lookup_flow(dp, &flow);
1304     if (!netdev_flow) {
1305         if (put->flags & DPIF_FP_CREATE) {
1306             if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
1307                 if (put->stats) {
1308                     memset(put->stats, 0, sizeof *put->stats);
1309                 }
1310                 error = dp_netdev_flow_add(dp, &flow, &wc, put->actions,
1311                                            put->actions_len);
1312             } else {
1313                 error = EFBIG;
1314             }
1315         } else {
1316             error = ENOENT;
1317         }
1318     } else {
1319         if (put->flags & DPIF_FP_MODIFY
1320             && flow_equal(&flow, &netdev_flow->flow)) {
1321             struct dp_netdev_actions *new_actions;
1322             struct dp_netdev_actions *old_actions;
1323
1324             new_actions = dp_netdev_actions_create(put->actions,
1325                                                    put->actions_len);
1326
1327             old_actions = dp_netdev_flow_get_actions(netdev_flow);
1328             ovsrcu_set(&netdev_flow->actions, new_actions);
1329
1330             if (put->stats) {
1331                 get_dpif_flow_stats(netdev_flow, put->stats);
1332             }
1333             if (put->flags & DPIF_FP_ZERO_STATS) {
1334                 clear_stats(netdev_flow);
1335             }
1336
1337             ovsrcu_postpone(dp_netdev_actions_free, old_actions);
1338         } else if (put->flags & DPIF_FP_CREATE) {
1339             error = EEXIST;
1340         } else {
1341             /* Overlapping flow. */
1342             error = EINVAL;
1343         }
1344     }
1345     ovs_mutex_unlock(&dp->flow_mutex);
1346
1347     return error;
1348 }
1349
1350 static int
1351 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
1352 {
1353     struct dp_netdev *dp = get_dp_netdev(dpif);
1354     struct dp_netdev_flow *netdev_flow;
1355     struct flow key;
1356     int error;
1357
1358     error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
1359     if (error) {
1360         return error;
1361     }
1362
1363     ovs_mutex_lock(&dp->flow_mutex);
1364     fat_rwlock_wrlock(&dp->cls.rwlock);
1365     netdev_flow = dp_netdev_find_flow(dp, &key);
1366     if (netdev_flow) {
1367         if (del->stats) {
1368             get_dpif_flow_stats(netdev_flow, del->stats);
1369         }
1370         dp_netdev_remove_flow(dp, netdev_flow);
1371     } else {
1372         error = ENOENT;
1373     }
1374     fat_rwlock_unlock(&dp->cls.rwlock);
1375     ovs_mutex_unlock(&dp->flow_mutex);
1376
1377     return error;
1378 }
1379
1380 struct dp_netdev_flow_state {
1381     struct dp_netdev_actions *actions;
1382     struct odputil_keybuf keybuf;
1383     struct odputil_keybuf maskbuf;
1384     struct dpif_flow_stats stats;
1385 };
1386
1387 struct dp_netdev_flow_iter {
1388     uint32_t bucket;
1389     uint32_t offset;
1390     int status;
1391     struct ovs_mutex mutex;
1392 };
1393
1394 static void
1395 dpif_netdev_flow_dump_state_init(void **statep)
1396 {
1397     struct dp_netdev_flow_state *state;
1398
1399     *statep = state = xmalloc(sizeof *state);
1400     state->actions = NULL;
1401 }
1402
1403 static void
1404 dpif_netdev_flow_dump_state_uninit(void *state_)
1405 {
1406     struct dp_netdev_flow_state *state = state_;
1407
1408     free(state);
1409 }
1410
1411 static int
1412 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **iterp)
1413 {
1414     struct dp_netdev_flow_iter *iter;
1415
1416     *iterp = iter = xmalloc(sizeof *iter);
1417     iter->bucket = 0;
1418     iter->offset = 0;
1419     iter->status = 0;
1420     ovs_mutex_init(&iter->mutex);
1421     return 0;
1422 }
1423
1424 /* XXX the caller must use 'actions' without quiescing */
1425 static int
1426 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *iter_, void *state_,
1427                            const struct nlattr **key, size_t *key_len,
1428                            const struct nlattr **mask, size_t *mask_len,
1429                            const struct nlattr **actions, size_t *actions_len,
1430                            const struct dpif_flow_stats **stats)
1431 {
1432     struct dp_netdev_flow_iter *iter = iter_;
1433     struct dp_netdev_flow_state *state = state_;
1434     struct dp_netdev *dp = get_dp_netdev(dpif);
1435     struct dp_netdev_flow *netdev_flow;
1436     int error;
1437
1438     ovs_mutex_lock(&iter->mutex);
1439     error = iter->status;
1440     if (!error) {
1441         struct hmap_node *node;
1442
1443         fat_rwlock_rdlock(&dp->cls.rwlock);
1444         node = hmap_at_position(&dp->flow_table, &iter->bucket, &iter->offset);
1445         if (node) {
1446             netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
1447         }
1448         fat_rwlock_unlock(&dp->cls.rwlock);
1449         if (!node) {
1450             iter->status = error = EOF;
1451         }
1452     }
1453     ovs_mutex_unlock(&iter->mutex);
1454     if (error) {
1455         return error;
1456     }
1457
1458     if (key) {
1459         struct ofpbuf buf;
1460
1461         ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
1462         odp_flow_key_from_flow(&buf, &netdev_flow->flow,
1463                                netdev_flow->flow.in_port.odp_port);
1464
1465         *key = buf.data;
1466         *key_len = buf.size;
1467     }
1468
1469     if (key && mask) {
1470         struct ofpbuf buf;
1471         struct flow_wildcards wc;
1472
1473         ofpbuf_use_stack(&buf, &state->maskbuf, sizeof state->maskbuf);
1474         minimask_expand(&netdev_flow->cr.match.mask, &wc);
1475         odp_flow_key_from_mask(&buf, &wc.masks, &netdev_flow->flow,
1476                                odp_to_u32(wc.masks.in_port.odp_port),
1477                                SIZE_MAX);
1478
1479         *mask = buf.data;
1480         *mask_len = buf.size;
1481     }
1482
1483     if (actions || stats) {
1484         state->actions = NULL;
1485
1486         if (actions) {
1487             state->actions = dp_netdev_flow_get_actions(netdev_flow);
1488             *actions = state->actions->actions;
1489             *actions_len = state->actions->size;
1490         }
1491
1492         if (stats) {
1493             get_dpif_flow_stats(netdev_flow, &state->stats);
1494             *stats = &state->stats;
1495         }
1496     }
1497
1498     return 0;
1499 }
1500
1501 static int
1502 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *iter_)
1503 {
1504     struct dp_netdev_flow_iter *iter = iter_;
1505
1506     ovs_mutex_destroy(&iter->mutex);
1507     free(iter);
1508     return 0;
1509 }
1510
1511 static int
1512 dpif_netdev_execute(struct dpif *dpif, struct dpif_execute *execute)
1513 {
1514     struct dp_netdev *dp = get_dp_netdev(dpif);
1515     struct pkt_metadata *md = &execute->md;
1516     struct flow key;
1517
1518     if (execute->packet->size < ETH_HEADER_LEN ||
1519         execute->packet->size > UINT16_MAX) {
1520         return EINVAL;
1521     }
1522
1523     /* Extract flow key. */
1524     flow_extract(execute->packet, md, &key);
1525
1526     ovs_rwlock_rdlock(&dp->port_rwlock);
1527     dp_netdev_execute_actions(dp, &key, execute->packet, false, md,
1528                               execute->actions, execute->actions_len);
1529     ovs_rwlock_unlock(&dp->port_rwlock);
1530
1531     return 0;
1532 }
1533
1534 static void
1535 dp_netdev_destroy_all_queues(struct dp_netdev *dp)
1536     OVS_REQ_WRLOCK(dp->queue_rwlock)
1537 {
1538     size_t i;
1539
1540     dp_netdev_purge_queues(dp);
1541
1542     for (i = 0; i < dp->n_handlers; i++) {
1543         struct dp_netdev_queue *q = &dp->handler_queues[i];
1544
1545         ovs_mutex_destroy(&q->mutex);
1546         seq_destroy(q->seq);
1547     }
1548     free(dp->handler_queues);
1549     dp->handler_queues = NULL;
1550     dp->n_handlers = 0;
1551 }
1552
1553 static void
1554 dp_netdev_refresh_queues(struct dp_netdev *dp, uint32_t n_handlers)
1555     OVS_REQ_WRLOCK(dp->queue_rwlock)
1556 {
1557     if (dp->n_handlers != n_handlers) {
1558         size_t i;
1559
1560         dp_netdev_destroy_all_queues(dp);
1561
1562         dp->n_handlers = n_handlers;
1563         dp->handler_queues = xzalloc(n_handlers * sizeof *dp->handler_queues);
1564
1565         for (i = 0; i < n_handlers; i++) {
1566             struct dp_netdev_queue *q = &dp->handler_queues[i];
1567
1568             ovs_mutex_init(&q->mutex);
1569             q->seq = seq_create();
1570         }
1571     }
1572 }
1573
1574 static int
1575 dpif_netdev_recv_set(struct dpif *dpif, bool enable)
1576 {
1577     struct dp_netdev *dp = get_dp_netdev(dpif);
1578
1579     if ((dp->handler_queues != NULL) == enable) {
1580         return 0;
1581     }
1582
1583     fat_rwlock_wrlock(&dp->queue_rwlock);
1584     if (!enable) {
1585         dp_netdev_destroy_all_queues(dp);
1586     } else {
1587         dp_netdev_refresh_queues(dp, 1);
1588     }
1589     fat_rwlock_unlock(&dp->queue_rwlock);
1590
1591     return 0;
1592 }
1593
1594 static int
1595 dpif_netdev_handlers_set(struct dpif *dpif, uint32_t n_handlers)
1596 {
1597     struct dp_netdev *dp = get_dp_netdev(dpif);
1598
1599     fat_rwlock_wrlock(&dp->queue_rwlock);
1600     if (dp->handler_queues) {
1601         dp_netdev_refresh_queues(dp, n_handlers);
1602     }
1603     fat_rwlock_unlock(&dp->queue_rwlock);
1604
1605     return 0;
1606 }
1607
1608 static int
1609 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1610                               uint32_t queue_id, uint32_t *priority)
1611 {
1612     *priority = queue_id;
1613     return 0;
1614 }
1615
1616 static bool
1617 dp_netdev_recv_check(const struct dp_netdev *dp, const uint32_t handler_id)
1618     OVS_REQ_RDLOCK(dp->queue_rwlock)
1619 {
1620     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1621
1622     if (!dp->handler_queues) {
1623         VLOG_WARN_RL(&rl, "receiving upcall disabled");
1624         return false;
1625     }
1626
1627     if (handler_id >= dp->n_handlers) {
1628         VLOG_WARN_RL(&rl, "handler index out of bound");
1629         return false;
1630     }
1631
1632     return true;
1633 }
1634
1635 static int
1636 dpif_netdev_recv(struct dpif *dpif, uint32_t handler_id,
1637                  struct dpif_upcall *upcall, struct ofpbuf *buf)
1638 {
1639     struct dp_netdev *dp = get_dp_netdev(dpif);
1640     struct dp_netdev_queue *q;
1641     int error = 0;
1642
1643     fat_rwlock_rdlock(&dp->queue_rwlock);
1644
1645     if (!dp_netdev_recv_check(dp, handler_id)) {
1646         error = EAGAIN;
1647         goto out;
1648     }
1649
1650     q = &dp->handler_queues[handler_id];
1651     ovs_mutex_lock(&q->mutex);
1652     if (q->head != q->tail) {
1653         struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1654
1655         *upcall = u->upcall;
1656
1657         ofpbuf_uninit(buf);
1658         *buf = u->buf;
1659     } else {
1660         error = EAGAIN;
1661     }
1662     ovs_mutex_unlock(&q->mutex);
1663
1664 out:
1665     fat_rwlock_unlock(&dp->queue_rwlock);
1666
1667     return error;
1668 }
1669
1670 static void
1671 dpif_netdev_recv_wait(struct dpif *dpif, uint32_t handler_id)
1672 {
1673     struct dp_netdev *dp = get_dp_netdev(dpif);
1674     struct dp_netdev_queue *q;
1675     uint64_t seq;
1676
1677     fat_rwlock_rdlock(&dp->queue_rwlock);
1678
1679     if (!dp_netdev_recv_check(dp, handler_id)) {
1680         goto out;
1681     }
1682
1683     q = &dp->handler_queues[handler_id];
1684     ovs_mutex_lock(&q->mutex);
1685     seq = seq_read(q->seq);
1686     if (q->head != q->tail) {
1687         poll_immediate_wake();
1688     } else {
1689         seq_wait(q->seq, seq);
1690     }
1691
1692     ovs_mutex_unlock(&q->mutex);
1693
1694 out:
1695     fat_rwlock_unlock(&dp->queue_rwlock);
1696 }
1697
1698 static void
1699 dpif_netdev_recv_purge(struct dpif *dpif)
1700 {
1701     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1702
1703     fat_rwlock_wrlock(&dpif_netdev->dp->queue_rwlock);
1704     dp_netdev_purge_queues(dpif_netdev->dp);
1705     fat_rwlock_unlock(&dpif_netdev->dp->queue_rwlock);
1706 }
1707 \f
1708 /* Creates and returns a new 'struct dp_netdev_actions', with a reference count
1709  * of 1, whose actions are a copy of from the 'ofpacts_len' bytes of
1710  * 'ofpacts'. */
1711 struct dp_netdev_actions *
1712 dp_netdev_actions_create(const struct nlattr *actions, size_t size)
1713 {
1714     struct dp_netdev_actions *netdev_actions;
1715
1716     netdev_actions = xmalloc(sizeof *netdev_actions);
1717     netdev_actions->actions = xmemdup(actions, size);
1718     netdev_actions->size = size;
1719
1720     return netdev_actions;
1721 }
1722
1723 struct dp_netdev_actions *
1724 dp_netdev_flow_get_actions(const struct dp_netdev_flow *flow)
1725 {
1726     return ovsrcu_get(struct dp_netdev_actions *, &flow->actions);
1727 }
1728
1729 static void
1730 dp_netdev_actions_free(struct dp_netdev_actions *actions)
1731 {
1732     free(actions->actions);
1733     free(actions);
1734 }
1735 \f
1736
1737 static void
1738 dp_netdev_process_rxq_port(struct dp_netdev *dp,
1739                           struct dp_netdev_port *port,
1740                           struct netdev_rxq *rxq)
1741 {
1742     struct ofpbuf *packet[NETDEV_MAX_RX_BATCH];
1743     int error, c;
1744
1745     error = netdev_rxq_recv(rxq, packet, &c);
1746     if (!error) {
1747         struct pkt_metadata md = PKT_METADATA_INITIALIZER(port->port_no);
1748         int i;
1749
1750         for (i = 0; i < c; i++) {
1751             dp_netdev_port_input(dp, packet[i], &md);
1752         }
1753     } else if (error != EAGAIN && error != EOPNOTSUPP) {
1754         static struct vlog_rate_limit rl
1755             = VLOG_RATE_LIMIT_INIT(1, 5);
1756
1757         VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1758                     netdev_get_name(port->netdev),
1759                     ovs_strerror(error));
1760     }
1761 }
1762
1763 static void
1764 dpif_netdev_run(struct dpif *dpif)
1765 {
1766     struct dp_netdev_port *port;
1767     struct dp_netdev *dp = get_dp_netdev(dpif);
1768
1769     ovs_rwlock_rdlock(&dp->port_rwlock);
1770
1771     HMAP_FOR_EACH (port, node, &dp->ports) {
1772         if (!netdev_is_pmd(port->netdev)) {
1773             int i;
1774
1775             for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1776                 dp_netdev_process_rxq_port(dp, port, port->rxq[i]);
1777             }
1778         }
1779     }
1780
1781     ovs_rwlock_unlock(&dp->port_rwlock);
1782 }
1783
1784 static void
1785 dpif_netdev_wait(struct dpif *dpif)
1786 {
1787     struct dp_netdev_port *port;
1788     struct dp_netdev *dp = get_dp_netdev(dpif);
1789
1790     ovs_rwlock_rdlock(&dp->port_rwlock);
1791
1792     HMAP_FOR_EACH (port, node, &dp->ports) {
1793         if (!netdev_is_pmd(port->netdev)) {
1794             int i;
1795
1796             for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1797                 netdev_rxq_wait(port->rxq[i]);
1798             }
1799         }
1800     }
1801     ovs_rwlock_unlock(&dp->port_rwlock);
1802 }
1803
1804 struct rxq_poll {
1805     struct dp_netdev_port *port;
1806     struct netdev_rxq *rx;
1807 };
1808
1809 static int
1810 pmd_load_queues(struct pmd_thread *f,
1811                 struct rxq_poll **ppoll_list, int poll_cnt)
1812 {
1813     struct dp_netdev *dp = f->dp;
1814     struct rxq_poll *poll_list = *ppoll_list;
1815     struct dp_netdev_port *port;
1816     int id = f->id;
1817     int index;
1818     int i;
1819
1820     /* Simple scheduler for netdev rx polling. */
1821     ovs_rwlock_rdlock(&dp->port_rwlock);
1822     for (i = 0; i < poll_cnt; i++) {
1823          port_unref(poll_list[i].port);
1824     }
1825
1826     poll_cnt = 0;
1827     index = 0;
1828
1829     HMAP_FOR_EACH (port, node, &f->dp->ports) {
1830         if (netdev_is_pmd(port->netdev)) {
1831             int i;
1832
1833             for (i = 0; i < netdev_n_rxq(port->netdev); i++) {
1834                 if ((index % dp->n_pmd_threads) == id) {
1835                     poll_list = xrealloc(poll_list, sizeof *poll_list * (poll_cnt + 1));
1836
1837                     port_ref(port);
1838                     poll_list[poll_cnt].port = port;
1839                     poll_list[poll_cnt].rx = port->rxq[i];
1840                     poll_cnt++;
1841                 }
1842                 index++;
1843             }
1844         }
1845     }
1846
1847     ovs_rwlock_unlock(&dp->port_rwlock);
1848     *ppoll_list = poll_list;
1849     return poll_cnt;
1850 }
1851
1852 static void *
1853 pmd_thread_main(void *f_)
1854 {
1855     struct pmd_thread *f = f_;
1856     struct dp_netdev *dp = f->dp;
1857     unsigned int lc = 0;
1858     struct rxq_poll *poll_list;
1859     unsigned int port_seq;
1860     int poll_cnt;
1861     int i;
1862
1863     f->name = xasprintf("pmd_%u", ovsthread_id_self());
1864     set_subprogram_name("%s", f->name);
1865     poll_cnt = 0;
1866     poll_list = NULL;
1867
1868     pmd_thread_setaffinity_cpu(f->id);
1869 reload:
1870     poll_cnt = pmd_load_queues(f, &poll_list, poll_cnt);
1871     atomic_read(&f->change_seq, &port_seq);
1872
1873     for (;;) {
1874         unsigned int c_port_seq;
1875         int i;
1876
1877         for (i = 0; i < poll_cnt; i++) {
1878             dp_netdev_process_rxq_port(dp,  poll_list[i].port, poll_list[i].rx);
1879         }
1880
1881         if (lc++ > 1024) {
1882             ovsrcu_quiesce();
1883
1884             /* TODO: need completely userspace based signaling method.
1885              * to keep this thread entirely in userspace.
1886              * For now using atomic counter. */
1887             lc = 0;
1888             atomic_read_explicit(&f->change_seq, &c_port_seq, memory_order_consume);
1889             if (c_port_seq != port_seq) {
1890                 break;
1891             }
1892         }
1893     }
1894
1895     if (!latch_is_set(&f->dp->exit_latch)){
1896         goto reload;
1897     }
1898
1899     for (i = 0; i < poll_cnt; i++) {
1900          port_unref(poll_list[i].port);
1901     }
1902
1903     free(poll_list);
1904     free(f->name);
1905     return NULL;
1906 }
1907
1908 static void
1909 dp_netdev_set_pmd_threads(struct dp_netdev *dp, int n)
1910 {
1911     int i;
1912
1913     if (n == dp->n_pmd_threads) {
1914         return;
1915     }
1916
1917     /* Stop existing threads. */
1918     latch_set(&dp->exit_latch);
1919     dp_netdev_reload_pmd_threads(dp);
1920     for (i = 0; i < dp->n_pmd_threads; i++) {
1921         struct pmd_thread *f = &dp->pmd_threads[i];
1922
1923         xpthread_join(f->thread, NULL);
1924     }
1925     latch_poll(&dp->exit_latch);
1926     free(dp->pmd_threads);
1927
1928     /* Start new threads. */
1929     dp->pmd_threads = xmalloc(n * sizeof *dp->pmd_threads);
1930     dp->n_pmd_threads = n;
1931
1932     for (i = 0; i < n; i++) {
1933         struct pmd_thread *f = &dp->pmd_threads[i];
1934
1935         f->dp = dp;
1936         f->id = i;
1937         atomic_store(&f->change_seq, 1);
1938
1939         /* Each thread will distribute all devices rx-queues among
1940          * themselves. */
1941         xpthread_create(&f->thread, NULL, pmd_thread_main, f);
1942     }
1943 }
1944
1945 \f
1946 static void *
1947 dp_netdev_flow_stats_new_cb(void)
1948 {
1949     struct dp_netdev_flow_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1950     ovs_mutex_init(&bucket->mutex);
1951     return bucket;
1952 }
1953
1954 static void
1955 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1956                     const struct ofpbuf *packet,
1957                     const struct flow *key)
1958 {
1959     uint16_t tcp_flags = ntohs(key->tcp_flags);
1960     long long int now = time_msec();
1961     struct dp_netdev_flow_stats *bucket;
1962
1963     bucket = ovsthread_stats_bucket_get(&netdev_flow->stats,
1964                                         dp_netdev_flow_stats_new_cb);
1965
1966     ovs_mutex_lock(&bucket->mutex);
1967     bucket->used = MAX(now, bucket->used);
1968     bucket->packet_count++;
1969     bucket->byte_count += packet->size;
1970     bucket->tcp_flags |= tcp_flags;
1971     ovs_mutex_unlock(&bucket->mutex);
1972 }
1973
1974 static void *
1975 dp_netdev_stats_new_cb(void)
1976 {
1977     struct dp_netdev_stats *bucket = xzalloc_cacheline(sizeof *bucket);
1978     ovs_mutex_init(&bucket->mutex);
1979     return bucket;
1980 }
1981
1982 static void
1983 dp_netdev_count_packet(struct dp_netdev *dp, enum dp_stat_type type)
1984 {
1985     struct dp_netdev_stats *bucket;
1986
1987     bucket = ovsthread_stats_bucket_get(&dp->stats, dp_netdev_stats_new_cb);
1988     ovs_mutex_lock(&bucket->mutex);
1989     bucket->n[type]++;
1990     ovs_mutex_unlock(&bucket->mutex);
1991 }
1992
1993 static void
1994 dp_netdev_port_input(struct dp_netdev *dp, struct ofpbuf *packet,
1995                      struct pkt_metadata *md)
1996 {
1997     struct dp_netdev_flow *netdev_flow;
1998     struct flow key;
1999
2000     if (packet->size < ETH_HEADER_LEN) {
2001         ofpbuf_delete(packet);
2002         return;
2003     }
2004     flow_extract(packet, md, &key);
2005     netdev_flow = dp_netdev_lookup_flow(dp, &key);
2006     if (netdev_flow) {
2007         struct dp_netdev_actions *actions;
2008
2009         dp_netdev_flow_used(netdev_flow, packet, &key);
2010
2011         actions = dp_netdev_flow_get_actions(netdev_flow);
2012         dp_netdev_execute_actions(dp, &key, packet, true, md,
2013                                   actions->actions, actions->size);
2014         dp_netdev_count_packet(dp, DP_STAT_HIT);
2015     } else if (dp->handler_queues) {
2016         dp_netdev_count_packet(dp, DP_STAT_MISS);
2017         dp_netdev_output_userspace(dp, packet,
2018                                    flow_hash_5tuple(&key, 0) % dp->n_handlers,
2019                                    DPIF_UC_MISS, &key, NULL);
2020         ofpbuf_delete(packet);
2021     }
2022 }
2023
2024 static int
2025 dp_netdev_output_userspace(struct dp_netdev *dp, struct ofpbuf *packet,
2026                            int queue_no, int type, const struct flow *flow,
2027                            const struct nlattr *userdata)
2028 {
2029     struct dp_netdev_queue *q;
2030     int error;
2031
2032     fat_rwlock_rdlock(&dp->queue_rwlock);
2033     q = &dp->handler_queues[queue_no];
2034     ovs_mutex_lock(&q->mutex);
2035     if (q->head - q->tail < MAX_QUEUE_LEN) {
2036         struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
2037         struct dpif_upcall *upcall = &u->upcall;
2038         struct ofpbuf *buf = &u->buf;
2039         size_t buf_size;
2040
2041         upcall->type = type;
2042
2043         /* Allocate buffer big enough for everything. */
2044         buf_size = ODPUTIL_FLOW_KEY_BYTES;
2045         if (userdata) {
2046             buf_size += NLA_ALIGN(userdata->nla_len);
2047         }
2048         buf_size += packet->size;
2049         ofpbuf_init(buf, buf_size);
2050
2051         /* Put ODP flow. */
2052         odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
2053         upcall->key = buf->data;
2054         upcall->key_len = buf->size;
2055
2056         /* Put userdata. */
2057         if (userdata) {
2058             upcall->userdata = ofpbuf_put(buf, userdata,
2059                                           NLA_ALIGN(userdata->nla_len));
2060         }
2061
2062         upcall->packet.data = ofpbuf_put(buf, packet->data, packet->size);
2063         upcall->packet.size = packet->size;
2064
2065         seq_change(q->seq);
2066
2067         error = 0;
2068     } else {
2069         dp_netdev_count_packet(dp, DP_STAT_LOST);
2070         error = ENOBUFS;
2071     }
2072     ovs_mutex_unlock(&q->mutex);
2073     fat_rwlock_unlock(&dp->queue_rwlock);
2074
2075     return error;
2076 }
2077
2078 struct dp_netdev_execute_aux {
2079     struct dp_netdev *dp;
2080     const struct flow *key;
2081 };
2082
2083 static void
2084 dp_execute_cb(void *aux_, struct ofpbuf *packet,
2085               struct pkt_metadata *md,
2086               const struct nlattr *a, bool may_steal)
2087     OVS_NO_THREAD_SAFETY_ANALYSIS
2088 {
2089     struct dp_netdev_execute_aux *aux = aux_;
2090     int type = nl_attr_type(a);
2091     struct dp_netdev_port *p;
2092
2093     switch ((enum ovs_action_attr)type) {
2094     case OVS_ACTION_ATTR_OUTPUT:
2095         p = dp_netdev_lookup_port(aux->dp, u32_to_odp(nl_attr_get_u32(a)));
2096         if (p) {
2097             netdev_send(p->netdev, packet, may_steal);
2098         }
2099         break;
2100
2101     case OVS_ACTION_ATTR_USERSPACE: {
2102         const struct nlattr *userdata;
2103
2104         userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
2105
2106         dp_netdev_output_userspace(aux->dp, packet,
2107                                    flow_hash_5tuple(aux->key, 0)
2108                                        % aux->dp->n_handlers,
2109                                    DPIF_UC_ACTION, aux->key,
2110                                    userdata);
2111
2112         if (may_steal) {
2113             ofpbuf_delete(packet);
2114         }
2115         break;
2116     }
2117
2118     case OVS_ACTION_ATTR_RECIRC: {
2119         const struct ovs_action_recirc *act;
2120
2121         act = nl_attr_get(a);
2122         md->recirc_id = act->recirc_id;
2123         md->dp_hash = 0;
2124
2125         if (act->hash_alg == OVS_RECIRC_HASH_ALG_L4) {
2126             struct flow flow;
2127
2128             flow_extract(packet, md, &flow);
2129             md->dp_hash = flow_hash_symmetric_l4(&flow, act->hash_bias);
2130         }
2131
2132         dp_netdev_port_input(aux->dp, packet, md);
2133         break;
2134     }
2135
2136     case OVS_ACTION_ATTR_PUSH_VLAN:
2137     case OVS_ACTION_ATTR_POP_VLAN:
2138     case OVS_ACTION_ATTR_PUSH_MPLS:
2139     case OVS_ACTION_ATTR_POP_MPLS:
2140     case OVS_ACTION_ATTR_SET:
2141     case OVS_ACTION_ATTR_SAMPLE:
2142     case OVS_ACTION_ATTR_UNSPEC:
2143     case __OVS_ACTION_ATTR_MAX:
2144         OVS_NOT_REACHED();
2145     }
2146
2147 }
2148
2149 static void
2150 dp_netdev_execute_actions(struct dp_netdev *dp, const struct flow *key,
2151                           struct ofpbuf *packet, bool may_steal,
2152                           struct pkt_metadata *md,
2153                           const struct nlattr *actions, size_t actions_len)
2154 {
2155     struct dp_netdev_execute_aux aux = {dp, key};
2156
2157     odp_execute_actions(&aux, packet, may_steal, md,
2158                         actions, actions_len, dp_execute_cb);
2159 }
2160
2161 const struct dpif_class dpif_netdev_class = {
2162     "netdev",
2163     dpif_netdev_enumerate,
2164     dpif_netdev_port_open_type,
2165     dpif_netdev_open,
2166     dpif_netdev_close,
2167     dpif_netdev_destroy,
2168     dpif_netdev_run,
2169     dpif_netdev_wait,
2170     dpif_netdev_get_stats,
2171     dpif_netdev_port_add,
2172     dpif_netdev_port_del,
2173     dpif_netdev_port_query_by_number,
2174     dpif_netdev_port_query_by_name,
2175     NULL,                       /* port_get_pid */
2176     dpif_netdev_port_dump_start,
2177     dpif_netdev_port_dump_next,
2178     dpif_netdev_port_dump_done,
2179     dpif_netdev_port_poll,
2180     dpif_netdev_port_poll_wait,
2181     dpif_netdev_flow_get,
2182     dpif_netdev_flow_put,
2183     dpif_netdev_flow_del,
2184     dpif_netdev_flow_flush,
2185     dpif_netdev_flow_dump_state_init,
2186     dpif_netdev_flow_dump_start,
2187     dpif_netdev_flow_dump_next,
2188     NULL,
2189     dpif_netdev_flow_dump_done,
2190     dpif_netdev_flow_dump_state_uninit,
2191     dpif_netdev_execute,
2192     NULL,                       /* operate */
2193     dpif_netdev_recv_set,
2194     dpif_netdev_handlers_set,
2195     dpif_netdev_queue_to_priority,
2196     dpif_netdev_recv,
2197     dpif_netdev_recv_wait,
2198     dpif_netdev_recv_purge,
2199 };
2200
2201 static void
2202 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
2203                               const char *argv[], void *aux OVS_UNUSED)
2204 {
2205     struct dp_netdev_port *port;
2206     struct dp_netdev *dp;
2207     odp_port_t port_no;
2208
2209     ovs_mutex_lock(&dp_netdev_mutex);
2210     dp = shash_find_data(&dp_netdevs, argv[1]);
2211     if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
2212         ovs_mutex_unlock(&dp_netdev_mutex);
2213         unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
2214         return;
2215     }
2216     ovs_refcount_ref(&dp->ref_cnt);
2217     ovs_mutex_unlock(&dp_netdev_mutex);
2218
2219     ovs_rwlock_wrlock(&dp->port_rwlock);
2220     if (get_port_by_name(dp, argv[2], &port)) {
2221         unixctl_command_reply_error(conn, "unknown port");
2222         goto exit;
2223     }
2224
2225     port_no = u32_to_odp(atoi(argv[3]));
2226     if (!port_no || port_no == ODPP_NONE) {
2227         unixctl_command_reply_error(conn, "bad port number");
2228         goto exit;
2229     }
2230     if (dp_netdev_lookup_port(dp, port_no)) {
2231         unixctl_command_reply_error(conn, "port number already in use");
2232         goto exit;
2233     }
2234     hmap_remove(&dp->ports, &port->node);
2235     port->port_no = port_no;
2236     hmap_insert(&dp->ports, &port->node, hash_int(odp_to_u32(port_no), 0));
2237     seq_change(dp->port_seq);
2238     unixctl_command_reply(conn, NULL);
2239
2240 exit:
2241     ovs_rwlock_unlock(&dp->port_rwlock);
2242     dp_netdev_unref(dp);
2243 }
2244
2245 static void
2246 dpif_dummy_register__(const char *type)
2247 {
2248     struct dpif_class *class;
2249
2250     class = xmalloc(sizeof *class);
2251     *class = dpif_netdev_class;
2252     class->type = xstrdup(type);
2253     dp_register_provider(class);
2254 }
2255
2256 void
2257 dpif_dummy_register(bool override)
2258 {
2259     if (override) {
2260         struct sset types;
2261         const char *type;
2262
2263         sset_init(&types);
2264         dp_enumerate_types(&types);
2265         SSET_FOR_EACH (type, &types) {
2266             if (!dp_unregister_provider(type)) {
2267                 dpif_dummy_register__(type);
2268             }
2269         }
2270         sset_destroy(&types);
2271     }
2272
2273     dpif_dummy_register__("dummy");
2274
2275     unixctl_command_register("dpif-dummy/change-port-number",
2276                              "DP PORT NEW-NUMBER",
2277                              3, 3, dpif_dummy_change_port_number, NULL);
2278 }