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