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