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