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