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