Merge branch 'mainstream'
[sliver-openvswitch.git] / lib / dpif-netdev.c
1 /*
2  * Copyright (c) 2009, 2010, 2011, 2012, 2013 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 "csum.h"
35 #include "dpif.h"
36 #include "dpif-provider.h"
37 #include "dummy.h"
38 #include "dynamic-string.h"
39 #include "flow.h"
40 #include "hmap.h"
41 #include "list.h"
42 #include "netdev.h"
43 #include "netdev-vport.h"
44 #include "netlink.h"
45 #include "odp-execute.h"
46 #include "odp-util.h"
47 #include "ofp-print.h"
48 #include "ofpbuf.h"
49 #include "packets.h"
50 #include "poll-loop.h"
51 #include "random.h"
52 #include "seq.h"
53 #include "shash.h"
54 #include "sset.h"
55 #include "timeval.h"
56 #include "unixctl.h"
57 #include "util.h"
58 #include "vlog.h"
59
60 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
61
62 /* Configuration parameters. */
63 enum { MAX_PORTS = 256 };       /* Maximum number of ports. */
64 enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
65
66 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
67  * headers to be aligned on a 4-byte boundary.  */
68 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
69
70 /* Queues. */
71 enum { N_QUEUES = 2 };          /* Number of queues for dpif_recv(). */
72 enum { MAX_QUEUE_LEN = 128 };   /* Maximum number of packets per queue. */
73 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
74 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
75
76 struct dp_netdev_upcall {
77     struct dpif_upcall upcall;  /* Queued upcall information. */
78     struct ofpbuf buf;          /* ofpbuf instance for upcall.packet. */
79 };
80
81 struct dp_netdev_queue {
82     struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN];
83     unsigned int head, tail;
84 };
85
86 /* Datapath based on the network device interface from netdev.h. */
87 struct dp_netdev {
88     const struct dpif_class *class;
89     char *name;
90     int open_cnt;
91     bool destroyed;
92     int max_mtu;                /* Maximum MTU of any port added so far. */
93
94     struct dp_netdev_queue queues[N_QUEUES];
95     struct hmap flow_table;     /* Flow table. */
96     struct seq *queue_seq;      /* Incremented whenever a packet is queued. */
97
98     /* Statistics. */
99     long long int n_hit;        /* Number of flow table matches. */
100     long long int n_missed;     /* Number of flow table misses. */
101     long long int n_lost;       /* Number of misses not passed to client. */
102
103     /* Ports. */
104     struct dp_netdev_port *ports[MAX_PORTS];
105     struct list port_list;
106     struct seq *port_seq;       /* Incremented whenever a port changes. */
107 };
108
109 /* A port in a netdev-based datapath. */
110 struct dp_netdev_port {
111     odp_port_t port_no;         /* Index into dp_netdev's 'ports'. */
112     struct list node;           /* Element in dp_netdev's 'port_list'. */
113     struct netdev *netdev;
114     struct netdev_saved_flags *sf;
115     struct netdev_rx *rx;
116     char *type;                 /* Port type as requested by user. */
117 };
118
119 /* A flow in dp_netdev's 'flow_table'. */
120 struct dp_netdev_flow {
121     struct hmap_node node;      /* Element in dp_netdev's 'flow_table'. */
122     struct flow key;
123
124     /* Statistics. */
125     long long int used;         /* Last used time, in monotonic msecs. */
126     long long int packet_count; /* Number of packets matched. */
127     long long int byte_count;   /* Number of bytes matched. */
128     uint16_t tcp_flags;         /* Bitwise-OR of seen tcp_flags values. */
129
130     /* Actions. */
131     struct nlattr *actions;
132     size_t actions_len;
133 };
134
135 /* Interface to netdev-based datapath. */
136 struct dpif_netdev {
137     struct dpif dpif;
138     struct dp_netdev *dp;
139     uint64_t last_port_seq;
140 };
141
142 /* All netdev-based datapaths. */
143 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
144
145 /* Global lock for all data. */
146 static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
147
148 static int get_port_by_number(struct dp_netdev *, odp_port_t port_no,
149                               struct dp_netdev_port **portp);
150 static int get_port_by_name(struct dp_netdev *, const char *devname,
151                             struct dp_netdev_port **portp);
152 static void dp_netdev_free(struct dp_netdev *);
153 static void dp_netdev_flow_flush(struct dp_netdev *);
154 static int do_add_port(struct dp_netdev *, const char *devname,
155                        const char *type, odp_port_t port_no);
156 static int do_del_port(struct dp_netdev *, odp_port_t port_no);
157 static int dpif_netdev_open(const struct dpif_class *, const char *name,
158                             bool create, struct dpif **);
159 static int dp_netdev_output_userspace(struct dp_netdev *, const struct ofpbuf *,
160                                     int queue_no, const struct flow *,
161                                     const struct nlattr *userdata);
162 static void dp_netdev_execute_actions(struct dp_netdev *,
163                                       struct ofpbuf *, struct flow *,
164                                       const struct nlattr *actions,
165                                       size_t actions_len);
166 static void dp_netdev_port_input(struct dp_netdev *dp,
167                                  struct dp_netdev_port *port,
168                                  struct ofpbuf *packet, uint32_t skb_priority,
169                                  uint32_t pkt_mark, const struct flow_tnl *tnl);
170
171 static struct dpif_netdev *
172 dpif_netdev_cast(const struct dpif *dpif)
173 {
174     ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
175     return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
176 }
177
178 static struct dp_netdev *
179 get_dp_netdev(const struct dpif *dpif)
180 {
181     return dpif_netdev_cast(dpif)->dp;
182 }
183
184 static int
185 dpif_netdev_enumerate(struct sset *all_dps)
186 {
187     struct shash_node *node;
188
189     ovs_mutex_lock(&dp_netdev_mutex);
190     SHASH_FOR_EACH(node, &dp_netdevs) {
191         sset_add(all_dps, node->name);
192     }
193     ovs_mutex_unlock(&dp_netdev_mutex);
194
195     return 0;
196 }
197
198 static bool
199 dpif_netdev_class_is_dummy(const struct dpif_class *class)
200 {
201     return class != &dpif_netdev_class;
202 }
203
204 static bool
205 dpif_netdev_class_is_planetlab(const struct dpif_class *class)
206 {
207     return class == &dpif_planetlab_class;
208 }
209
210 static const char *
211 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
212 {
213     return strcmp(type, "internal") ? type
214                   : dpif_netdev_class_is_planetlab(class) ? "pltap"
215                   : dpif_netdev_class_is_dummy(class) ? "dummy"
216                   : "tap";
217 }
218
219 static struct dpif *
220 create_dpif_netdev(struct dp_netdev *dp)
221 {
222     uint16_t netflow_id = hash_string(dp->name, 0);
223     struct dpif_netdev *dpif;
224
225     dp->open_cnt++;
226
227     dpif = xmalloc(sizeof *dpif);
228     dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
229     dpif->dp = dp;
230     dpif->last_port_seq = seq_read(dp->port_seq);
231
232     return &dpif->dpif;
233 }
234
235 /* Choose an unused, non-zero port number and return it on success.
236  * Return ODPP_NONE on failure. */
237 static odp_port_t
238 choose_port(struct dp_netdev *dp, const char *name)
239 {
240     uint32_t port_no;
241
242     if (dp->class != &dpif_netdev_class && 
243         dp->class != &dpif_planetlab_class) {
244         const char *p;
245         int start_no = 0;
246
247         /* If the port name begins with "br", start the number search at
248          * 100 to make writing tests easier. */
249         if (!strncmp(name, "br", 2)) {
250             start_no = 100;
251         }
252
253         /* If the port name contains a number, try to assign that port number.
254          * This can make writing unit tests easier because port numbers are
255          * predictable. */
256         for (p = name; *p != '\0'; p++) {
257             if (isdigit((unsigned char) *p)) {
258                 port_no = start_no + strtol(p, NULL, 10);
259                 if (port_no > 0 && port_no < MAX_PORTS
260                     && !dp->ports[port_no]) {
261                     return u32_to_odp(port_no);
262                 }
263                 break;
264             }
265         }
266     }
267
268     for (port_no = 1; port_no < MAX_PORTS; port_no++) {
269         if (!dp->ports[port_no]) {
270             return u32_to_odp(port_no);
271         }
272     }
273
274     return ODPP_NONE;
275 }
276
277 static int
278 create_dp_netdev(const char *name, const struct dpif_class *class,
279                  struct dp_netdev **dpp)
280 {
281     struct dp_netdev *dp;
282     int error;
283     int i;
284
285     dp = xzalloc(sizeof *dp);
286     dp->class = class;
287     dp->name = xstrdup(name);
288     dp->open_cnt = 0;
289     dp->max_mtu = ETH_PAYLOAD_MAX;
290     for (i = 0; i < N_QUEUES; i++) {
291         dp->queues[i].head = dp->queues[i].tail = 0;
292     }
293     dp->queue_seq = seq_create();
294     hmap_init(&dp->flow_table);
295     list_init(&dp->port_list);
296     dp->port_seq = seq_create();
297
298     error = do_add_port(dp, name, "internal", ODPP_LOCAL);
299     if (error) {
300         dp_netdev_free(dp);
301         return error;
302     }
303
304     shash_add(&dp_netdevs, name, dp);
305
306     *dpp = dp;
307     return 0;
308 }
309
310 static int
311 dpif_netdev_open(const struct dpif_class *class, const char *name,
312                  bool create, struct dpif **dpifp)
313 {
314     struct dp_netdev *dp;
315     int error;
316
317     ovs_mutex_lock(&dp_netdev_mutex);
318     dp = shash_find_data(&dp_netdevs, name);
319     if (!dp) {
320         error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
321     } else {
322         error = (dp->class != class ? EINVAL
323                  : create ? EEXIST
324                  : 0);
325     }
326     if (!error) {
327         *dpifp = create_dpif_netdev(dp);
328     }
329     ovs_mutex_unlock(&dp_netdev_mutex);
330
331     return error;
332 }
333
334 static void
335 dp_netdev_purge_queues(struct dp_netdev *dp)
336 {
337     int i;
338
339     for (i = 0; i < N_QUEUES; i++) {
340         struct dp_netdev_queue *q = &dp->queues[i];
341
342         while (q->tail != q->head) {
343             struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
344             ofpbuf_uninit(&u->buf);
345         }
346     }
347 }
348
349 static void
350 dp_netdev_free(struct dp_netdev *dp)
351 {
352     struct dp_netdev_port *port, *next;
353
354     dp_netdev_flow_flush(dp);
355     LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
356         do_del_port(dp, port->port_no);
357     }
358     dp_netdev_purge_queues(dp);
359     seq_destroy(dp->queue_seq);
360     hmap_destroy(&dp->flow_table);
361     seq_destroy(dp->port_seq);
362     free(dp->name);
363     free(dp);
364 }
365
366 static void
367 dpif_netdev_close(struct dpif *dpif)
368 {
369     struct dp_netdev *dp = get_dp_netdev(dpif);
370
371     ovs_mutex_lock(&dp_netdev_mutex);
372
373     ovs_assert(dp->open_cnt > 0);
374     if (--dp->open_cnt == 0 && dp->destroyed) {
375         shash_find_and_delete(&dp_netdevs, dp->name);
376         dp_netdev_free(dp);
377     }
378     free(dpif);
379
380     ovs_mutex_unlock(&dp_netdev_mutex);
381 }
382
383 static int
384 dpif_netdev_destroy(struct dpif *dpif)
385 {
386     struct dp_netdev *dp = get_dp_netdev(dpif);
387
388     ovs_mutex_lock(&dp_netdev_mutex);
389     dp->destroyed = true;
390     ovs_mutex_unlock(&dp_netdev_mutex);
391
392     return 0;
393 }
394
395 static int
396 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
397 {
398     struct dp_netdev *dp = get_dp_netdev(dpif);
399
400     ovs_mutex_lock(&dp_netdev_mutex);
401     stats->n_flows = hmap_count(&dp->flow_table);
402     stats->n_hit = dp->n_hit;
403     stats->n_missed = dp->n_missed;
404     stats->n_lost = dp->n_lost;
405     stats->n_masks = UINT64_MAX;
406     stats->n_mask_hit = UINT64_MAX;
407     ovs_mutex_unlock(&dp_netdev_mutex);
408
409     return 0;
410 }
411
412 static int
413 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
414             odp_port_t port_no)
415 {
416     struct netdev_saved_flags *sf;
417     struct dp_netdev_port *port;
418     struct netdev *netdev;
419     struct netdev_rx *rx;
420     enum netdev_flags flags;
421     const char *open_type;
422     int mtu;
423     int error;
424
425     /* XXX reject devices already in some dp_netdev. */
426
427     /* Open and validate network device. */
428     open_type = dpif_netdev_port_open_type(dp->class, type);
429     error = netdev_open(devname, open_type, &netdev);
430     if (error) {
431         return error;
432     }
433     /* XXX reject non-Ethernet devices */
434
435     netdev_get_flags(netdev, &flags);
436     if (flags & NETDEV_LOOPBACK) {
437         VLOG_ERR("%s: cannot add a loopback device", devname);
438         netdev_close(netdev);
439         return EINVAL;
440     }
441
442     error = netdev_rx_open(netdev, &rx);
443     if (error
444         && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
445         VLOG_ERR("%s: cannot receive packets on this network device (%s)",
446                  devname, ovs_strerror(errno));
447         netdev_close(netdev);
448         return error;
449     }
450
451     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
452     if (error) {
453         netdev_rx_close(rx);
454         netdev_close(netdev);
455         return error;
456     }
457
458     port = xmalloc(sizeof *port);
459     port->port_no = port_no;
460     port->netdev = netdev;
461     port->sf = sf;
462     port->rx = rx;
463     port->type = xstrdup(type);
464
465     error = netdev_get_mtu(netdev, &mtu);
466     if (!error && mtu > dp->max_mtu) {
467         dp->max_mtu = mtu;
468     }
469
470     list_push_back(&dp->port_list, &port->node);
471     dp->ports[odp_to_u32(port_no)] = port;
472     seq_change(dp->port_seq);
473
474     return 0;
475 }
476
477 static int
478 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
479                      odp_port_t *port_nop)
480 {
481     struct dp_netdev *dp = get_dp_netdev(dpif);
482     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
483     const char *dpif_port;
484     odp_port_t port_no;
485     int error;
486
487     ovs_mutex_lock(&dp_netdev_mutex);
488     dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
489     if (*port_nop != ODPP_NONE) {
490         uint32_t port_idx = odp_to_u32(*port_nop);
491         if (port_idx >= MAX_PORTS) {
492             error = EFBIG;
493         } else if (dp->ports[port_idx]) {
494             error = EBUSY;
495         } else {
496             error = 0;
497             port_no = *port_nop;
498         }
499     } else {
500         port_no = choose_port(dp, dpif_port);
501         error = port_no == ODPP_NONE ? EFBIG : 0;
502     }
503     if (!error) {
504         *port_nop = port_no;
505         error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
506     }
507     ovs_mutex_unlock(&dp_netdev_mutex);
508
509     return error;
510 }
511
512 static int
513 dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
514 {
515     struct dp_netdev *dp = get_dp_netdev(dpif);
516     int error;
517
518     ovs_mutex_lock(&dp_netdev_mutex);
519     error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
520     ovs_mutex_unlock(&dp_netdev_mutex);
521
522     return error;
523 }
524
525 static bool
526 is_valid_port_number(odp_port_t port_no)
527 {
528     return odp_to_u32(port_no) < MAX_PORTS;
529 }
530
531 static int
532 get_port_by_number(struct dp_netdev *dp,
533                    odp_port_t port_no, struct dp_netdev_port **portp)
534 {
535     if (!is_valid_port_number(port_no)) {
536         *portp = NULL;
537         return EINVAL;
538     } else {
539         *portp = dp->ports[odp_to_u32(port_no)];
540         return *portp ? 0 : ENOENT;
541     }
542 }
543
544 static int
545 get_port_by_name(struct dp_netdev *dp,
546                  const char *devname, struct dp_netdev_port **portp)
547 {
548     struct dp_netdev_port *port;
549
550     LIST_FOR_EACH (port, node, &dp->port_list) {
551         if (!strcmp(netdev_get_name(port->netdev), devname)) {
552             *portp = port;
553             return 0;
554         }
555     }
556     return ENOENT;
557 }
558
559 static int
560 do_del_port(struct dp_netdev *dp, odp_port_t port_no)
561 {
562     struct dp_netdev_port *port;
563     int error;
564
565     error = get_port_by_number(dp, port_no, &port);
566     if (error) {
567         return error;
568     }
569
570     list_remove(&port->node);
571     dp->ports[odp_to_u32(port_no)] = NULL;
572     seq_change(dp->port_seq);
573
574     netdev_close(port->netdev);
575     netdev_restore_flags(port->sf);
576     netdev_rx_close(port->rx);
577     free(port->type);
578     free(port);
579
580     return 0;
581 }
582
583 static void
584 answer_port_query(const struct dp_netdev_port *port,
585                   struct dpif_port *dpif_port)
586 {
587     dpif_port->name = xstrdup(netdev_get_name(port->netdev));
588     dpif_port->type = xstrdup(port->type);
589     dpif_port->port_no = port->port_no;
590 }
591
592 static int
593 dpif_netdev_port_query_by_number(const struct dpif *dpif, odp_port_t port_no,
594                                  struct dpif_port *dpif_port)
595 {
596     struct dp_netdev *dp = get_dp_netdev(dpif);
597     struct dp_netdev_port *port;
598     int error;
599
600     ovs_mutex_lock(&dp_netdev_mutex);
601     error = get_port_by_number(dp, port_no, &port);
602     if (!error && dpif_port) {
603         answer_port_query(port, dpif_port);
604     }
605     ovs_mutex_unlock(&dp_netdev_mutex);
606
607     return error;
608 }
609
610 static int
611 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
612                                struct dpif_port *dpif_port)
613 {
614     struct dp_netdev *dp = get_dp_netdev(dpif);
615     struct dp_netdev_port *port;
616     int error;
617
618     ovs_mutex_lock(&dp_netdev_mutex);
619     error = get_port_by_name(dp, devname, &port);
620     if (!error && dpif_port) {
621         answer_port_query(port, dpif_port);
622     }
623     ovs_mutex_unlock(&dp_netdev_mutex);
624
625     return error;
626 }
627
628 static uint32_t
629 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
630 {
631     return MAX_PORTS;
632 }
633
634 static void
635 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *netdev_flow)
636 {
637     hmap_remove(&dp->flow_table, &netdev_flow->node);
638     free(netdev_flow->actions);
639     free(netdev_flow);
640 }
641
642 static void
643 dp_netdev_flow_flush(struct dp_netdev *dp)
644 {
645     struct dp_netdev_flow *netdev_flow, *next;
646
647     HMAP_FOR_EACH_SAFE (netdev_flow, next, node, &dp->flow_table) {
648         dp_netdev_free_flow(dp, netdev_flow);
649     }
650 }
651
652 static int
653 dpif_netdev_flow_flush(struct dpif *dpif)
654 {
655     struct dp_netdev *dp = get_dp_netdev(dpif);
656
657     ovs_mutex_lock(&dp_netdev_mutex);
658     dp_netdev_flow_flush(dp);
659     ovs_mutex_unlock(&dp_netdev_mutex);
660
661     return 0;
662 }
663
664 struct dp_netdev_port_state {
665     odp_port_t port_no;
666     char *name;
667 };
668
669 static int
670 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
671 {
672     *statep = xzalloc(sizeof(struct dp_netdev_port_state));
673     return 0;
674 }
675
676 static int
677 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
678                            struct dpif_port *dpif_port)
679 {
680     struct dp_netdev_port_state *state = state_;
681     struct dp_netdev *dp = get_dp_netdev(dpif);
682     uint32_t port_idx;
683
684     ovs_mutex_lock(&dp_netdev_mutex);
685     for (port_idx = odp_to_u32(state->port_no);
686          port_idx < MAX_PORTS; port_idx++) {
687         struct dp_netdev_port *port = dp->ports[port_idx];
688         if (port) {
689             free(state->name);
690             state->name = xstrdup(netdev_get_name(port->netdev));
691             dpif_port->name = state->name;
692             dpif_port->type = port->type;
693             dpif_port->port_no = port->port_no;
694             state->port_no = u32_to_odp(port_idx + 1);
695             ovs_mutex_unlock(&dp_netdev_mutex);
696
697             return 0;
698         }
699     }
700     ovs_mutex_unlock(&dp_netdev_mutex);
701
702     return EOF;
703 }
704
705 static int
706 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
707 {
708     struct dp_netdev_port_state *state = state_;
709     free(state->name);
710     free(state);
711     return 0;
712 }
713
714 static int
715 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
716 {
717     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
718     uint64_t new_port_seq;
719     int error;
720
721     ovs_mutex_lock(&dp_netdev_mutex);
722     new_port_seq = seq_read(dpif->dp->port_seq);
723     if (dpif->last_port_seq != new_port_seq) {
724         dpif->last_port_seq = new_port_seq;
725         error = ENOBUFS;
726     } else {
727         error = EAGAIN;
728     }
729     ovs_mutex_unlock(&dp_netdev_mutex);
730
731     return error;
732 }
733
734 static void
735 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
736 {
737     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
738
739     ovs_mutex_lock(&dp_netdev_mutex);
740     seq_wait(dpif->dp->port_seq, dpif->last_port_seq);
741     ovs_mutex_unlock(&dp_netdev_mutex);
742 }
743
744 static struct dp_netdev_flow *
745 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
746 {
747     struct dp_netdev_flow *netdev_flow;
748
749     HMAP_FOR_EACH_WITH_HASH (netdev_flow, node, flow_hash(key, 0),
750                              &dp->flow_table) {
751         if (flow_equal(&netdev_flow->key, key)) {
752             return netdev_flow;
753         }
754     }
755     return NULL;
756 }
757
758 static void
759 get_dpif_flow_stats(struct dp_netdev_flow *netdev_flow,
760                     struct dpif_flow_stats *stats)
761 {
762     stats->n_packets = netdev_flow->packet_count;
763     stats->n_bytes = netdev_flow->byte_count;
764     stats->used = netdev_flow->used;
765     stats->tcp_flags = netdev_flow->tcp_flags;
766 }
767
768 static int
769 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
770                               struct flow *flow)
771 {
772     odp_port_t in_port;
773
774     if (odp_flow_key_to_flow(key, key_len, flow) != ODP_FIT_PERFECT) {
775         /* This should not happen: it indicates that odp_flow_key_from_flow()
776          * and odp_flow_key_to_flow() disagree on the acceptable form of a
777          * flow.  Log the problem as an error, with enough details to enable
778          * debugging. */
779         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
780
781         if (!VLOG_DROP_ERR(&rl)) {
782             struct ds s;
783
784             ds_init(&s);
785             odp_flow_key_format(key, key_len, &s);
786             VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
787             ds_destroy(&s);
788         }
789
790         return EINVAL;
791     }
792
793     in_port = flow->in_port.odp_port;
794     if (!is_valid_port_number(in_port) && in_port != ODPP_NONE) {
795         return EINVAL;
796     }
797
798     return 0;
799 }
800
801 static int
802 dpif_netdev_flow_get(const struct dpif *dpif,
803                      const struct nlattr *nl_key, size_t nl_key_len,
804                      struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
805 {
806     struct dp_netdev *dp = get_dp_netdev(dpif);
807     struct dp_netdev_flow *netdev_flow;
808     struct flow key;
809     int error;
810
811     error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
812     if (error) {
813         return error;
814     }
815
816     ovs_mutex_lock(&dp_netdev_mutex);
817     netdev_flow = dp_netdev_lookup_flow(dp, &key);
818     if (netdev_flow) {
819         if (stats) {
820             get_dpif_flow_stats(netdev_flow, stats);
821         }
822         if (actionsp) {
823             *actionsp = ofpbuf_clone_data(netdev_flow->actions,
824                                           netdev_flow->actions_len);
825         }
826     } else {
827         error = ENOENT;
828     }
829     ovs_mutex_unlock(&dp_netdev_mutex);
830
831     return error;
832 }
833
834 static int
835 set_flow_actions(struct dp_netdev_flow *netdev_flow,
836                  const struct nlattr *actions, size_t actions_len)
837 {
838     netdev_flow->actions = xrealloc(netdev_flow->actions, actions_len);
839     netdev_flow->actions_len = actions_len;
840     memcpy(netdev_flow->actions, actions, actions_len);
841     return 0;
842 }
843
844 static int
845 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key,
846                    const struct nlattr *actions, size_t actions_len)
847 {
848     struct dp_netdev_flow *netdev_flow;
849     int error;
850
851     netdev_flow = xzalloc(sizeof *netdev_flow);
852     netdev_flow->key = *key;
853
854     error = set_flow_actions(netdev_flow, actions, actions_len);
855     if (error) {
856         free(netdev_flow);
857         return error;
858     }
859
860     hmap_insert(&dp->flow_table, &netdev_flow->node,
861                 flow_hash(&netdev_flow->key, 0));
862     return 0;
863 }
864
865 static void
866 clear_stats(struct dp_netdev_flow *netdev_flow)
867 {
868     netdev_flow->used = 0;
869     netdev_flow->packet_count = 0;
870     netdev_flow->byte_count = 0;
871     netdev_flow->tcp_flags = 0;
872 }
873
874 static int
875 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
876 {
877     struct dp_netdev *dp = get_dp_netdev(dpif);
878     struct dp_netdev_flow *netdev_flow;
879     struct flow key;
880     int error;
881
882     error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
883     if (error) {
884         return error;
885     }
886
887     ovs_mutex_lock(&dp_netdev_mutex);
888     netdev_flow = dp_netdev_lookup_flow(dp, &key);
889     if (!netdev_flow) {
890         if (put->flags & DPIF_FP_CREATE) {
891             if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
892                 if (put->stats) {
893                     memset(put->stats, 0, sizeof *put->stats);
894                 }
895                 error = dp_netdev_flow_add(dp, &key, put->actions,
896                                            put->actions_len);
897             } else {
898                 error = EFBIG;
899             }
900         } else {
901             error = ENOENT;
902         }
903     } else {
904         if (put->flags & DPIF_FP_MODIFY) {
905             error = set_flow_actions(netdev_flow, put->actions,
906                                      put->actions_len);
907             if (!error) {
908                 if (put->stats) {
909                     get_dpif_flow_stats(netdev_flow, put->stats);
910                 }
911                 if (put->flags & DPIF_FP_ZERO_STATS) {
912                     clear_stats(netdev_flow);
913                 }
914             }
915         } else {
916             error = EEXIST;
917         }
918     }
919     ovs_mutex_unlock(&dp_netdev_mutex);
920
921     return error;
922 }
923
924 static int
925 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
926 {
927     struct dp_netdev *dp = get_dp_netdev(dpif);
928     struct dp_netdev_flow *netdev_flow;
929     struct flow key;
930     int error;
931
932     error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
933     if (error) {
934         return error;
935     }
936
937     ovs_mutex_lock(&dp_netdev_mutex);
938     netdev_flow = dp_netdev_lookup_flow(dp, &key);
939     if (netdev_flow) {
940         if (del->stats) {
941             get_dpif_flow_stats(netdev_flow, del->stats);
942         }
943         dp_netdev_free_flow(dp, netdev_flow);
944     } else {
945         error = ENOENT;
946     }
947     ovs_mutex_unlock(&dp_netdev_mutex);
948
949     return error;
950 }
951
952 struct dp_netdev_flow_state {
953     uint32_t bucket;
954     uint32_t offset;
955     struct nlattr *actions;
956     struct odputil_keybuf keybuf;
957     struct dpif_flow_stats stats;
958 };
959
960 static int
961 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
962 {
963     struct dp_netdev_flow_state *state;
964
965     *statep = state = xmalloc(sizeof *state);
966     state->bucket = 0;
967     state->offset = 0;
968     state->actions = NULL;
969     return 0;
970 }
971
972 static int
973 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
974                            const struct nlattr **key, size_t *key_len,
975                            const struct nlattr **mask, size_t *mask_len,
976                            const struct nlattr **actions, size_t *actions_len,
977                            const struct dpif_flow_stats **stats)
978 {
979     struct dp_netdev_flow_state *state = state_;
980     struct dp_netdev *dp = get_dp_netdev(dpif);
981     struct dp_netdev_flow *netdev_flow;
982     struct hmap_node *node;
983
984     ovs_mutex_lock(&dp_netdev_mutex);
985     node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
986     if (!node) {
987         ovs_mutex_unlock(&dp_netdev_mutex);
988         return EOF;
989     }
990
991     netdev_flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
992
993     if (key) {
994         struct ofpbuf buf;
995
996         ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
997         odp_flow_key_from_flow(&buf, &netdev_flow->key,
998                                netdev_flow->key.in_port.odp_port);
999
1000         *key = buf.data;
1001         *key_len = buf.size;
1002     }
1003
1004     if (mask) {
1005         *mask = NULL;
1006         *mask_len = 0;
1007     }
1008
1009     if (actions) {
1010         free(state->actions);
1011         state->actions = xmemdup(netdev_flow->actions,
1012                          netdev_flow->actions_len);
1013
1014         *actions = state->actions;
1015         *actions_len = netdev_flow->actions_len;
1016     }
1017
1018     if (stats) {
1019         get_dpif_flow_stats(netdev_flow, &state->stats);
1020         *stats = &state->stats;
1021     }
1022
1023     ovs_mutex_unlock(&dp_netdev_mutex);
1024     return 0;
1025 }
1026
1027 static int
1028 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
1029 {
1030     struct dp_netdev_flow_state *state = state_;
1031
1032     free(state->actions);
1033     free(state);
1034     return 0;
1035 }
1036
1037 static int
1038 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
1039 {
1040     struct dp_netdev *dp = get_dp_netdev(dpif);
1041     struct ofpbuf copy;
1042     struct flow key;
1043     int error;
1044
1045     if (execute->packet->size < ETH_HEADER_LEN ||
1046         execute->packet->size > UINT16_MAX) {
1047         return EINVAL;
1048     }
1049
1050     /* Make a deep copy of 'packet', because we might modify its data. */
1051     ofpbuf_init(&copy, DP_NETDEV_HEADROOM + execute->packet->size);
1052     ofpbuf_reserve(&copy, DP_NETDEV_HEADROOM);
1053     ofpbuf_put(&copy, execute->packet->data, execute->packet->size);
1054
1055     flow_extract(&copy, 0, 0, NULL, NULL, &key);
1056     error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
1057                                           &key);
1058     if (!error) {
1059         ovs_mutex_lock(&dp_netdev_mutex);
1060         dp_netdev_execute_actions(dp, &copy, &key,
1061                                   execute->actions, execute->actions_len);
1062         ovs_mutex_unlock(&dp_netdev_mutex);
1063     }
1064
1065     ofpbuf_uninit(&copy);
1066     return error;
1067 }
1068
1069 static int
1070 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
1071 {
1072     return 0;
1073 }
1074
1075 static int
1076 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1077                               uint32_t queue_id, uint32_t *priority)
1078 {
1079     *priority = queue_id;
1080     return 0;
1081 }
1082
1083 static struct dp_netdev_queue *
1084 find_nonempty_queue(struct dpif *dpif)
1085 {
1086     struct dp_netdev *dp = get_dp_netdev(dpif);
1087     int i;
1088
1089     for (i = 0; i < N_QUEUES; i++) {
1090         struct dp_netdev_queue *q = &dp->queues[i];
1091         if (q->head != q->tail) {
1092             return q;
1093         }
1094     }
1095     return NULL;
1096 }
1097
1098 static int
1099 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1100                  struct ofpbuf *buf)
1101 {
1102     struct dp_netdev_queue *q;
1103     int error;
1104
1105     ovs_mutex_lock(&dp_netdev_mutex);
1106     q = find_nonempty_queue(dpif);
1107     if (q) {
1108         struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1109
1110         *upcall = u->upcall;
1111         upcall->packet = buf;
1112
1113         ofpbuf_uninit(buf);
1114         *buf = u->buf;
1115
1116         error = 0;
1117     } else {
1118         error = EAGAIN;
1119     }
1120     ovs_mutex_unlock(&dp_netdev_mutex);
1121
1122     return error;
1123 }
1124
1125 static void
1126 dpif_netdev_recv_wait(struct dpif *dpif)
1127 {
1128     struct dp_netdev *dp = get_dp_netdev(dpif);
1129     uint64_t seq;
1130
1131     ovs_mutex_lock(&dp_netdev_mutex);
1132     seq = seq_read(dp->queue_seq);
1133     if (find_nonempty_queue(dpif)) {
1134         poll_immediate_wake();
1135     } else {
1136         seq_wait(dp->queue_seq, seq);
1137     }
1138     ovs_mutex_unlock(&dp_netdev_mutex);
1139 }
1140
1141 static void
1142 dpif_netdev_recv_purge(struct dpif *dpif)
1143 {
1144     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1145     ovs_mutex_lock(&dp_netdev_mutex);
1146     dp_netdev_purge_queues(dpif_netdev->dp);
1147     ovs_mutex_unlock(&dp_netdev_mutex);
1148 }
1149 \f
1150 static void
1151 dp_netdev_flow_used(struct dp_netdev_flow *netdev_flow,
1152                     const struct ofpbuf *packet)
1153 {
1154     netdev_flow->used = time_msec();
1155     netdev_flow->packet_count++;
1156     netdev_flow->byte_count += packet->size;
1157     netdev_flow->tcp_flags |= packet_get_tcp_flags(packet, &netdev_flow->key);
1158 }
1159
1160 static void
1161 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1162                      struct ofpbuf *packet, uint32_t skb_priority,
1163                      uint32_t pkt_mark, const struct flow_tnl *tnl)
1164 {
1165     struct dp_netdev_flow *netdev_flow;
1166     struct flow key;
1167     union flow_in_port in_port_;
1168
1169     if (packet->size < ETH_HEADER_LEN) {
1170         return;
1171     }
1172     in_port_.odp_port = port->port_no;
1173     flow_extract(packet, skb_priority, pkt_mark, tnl, &in_port_, &key);
1174     netdev_flow = dp_netdev_lookup_flow(dp, &key);
1175     if (netdev_flow) {
1176         dp_netdev_flow_used(netdev_flow, packet);
1177         dp_netdev_execute_actions(dp, packet, &key,
1178                                   netdev_flow->actions,
1179                                   netdev_flow->actions_len);
1180         dp->n_hit++;
1181     } else {
1182         dp->n_missed++;
1183         dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1184     }
1185 }
1186
1187 static void
1188 dpif_netdev_run(struct dpif *dpif)
1189 {
1190     struct dp_netdev_port *port;
1191     struct dp_netdev *dp;
1192     struct ofpbuf packet;
1193
1194     ovs_mutex_lock(&dp_netdev_mutex);
1195     dp = get_dp_netdev(dpif);
1196     ofpbuf_init(&packet,
1197                 DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + dp->max_mtu);
1198
1199     LIST_FOR_EACH (port, node, &dp->port_list) {
1200         int error;
1201
1202         /* Reset packet contents. */
1203         ofpbuf_clear(&packet);
1204         ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1205
1206         error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1207         if (!error) {
1208             dp_netdev_port_input(dp, port, &packet, 0, 0, NULL);
1209         } else if (error != EAGAIN && error != EOPNOTSUPP) {
1210             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1211
1212             VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1213                         netdev_get_name(port->netdev), ovs_strerror(error));
1214         }
1215     }
1216     ofpbuf_uninit(&packet);
1217     ovs_mutex_unlock(&dp_netdev_mutex);
1218 }
1219
1220 static void
1221 dpif_netdev_wait(struct dpif *dpif)
1222 {
1223     struct dp_netdev_port *port;
1224
1225     /* There is a race here, if thread A calls dpif_netdev_wait(dpif) and
1226      * thread B calls dpif_port_add(dpif) or dpif_port_remove(dpif) before
1227      * A makes it to poll_block().
1228      *
1229      * But I think it doesn't matter:
1230      *
1231      *     - In the dpif_port_add() case, A will not wake up when a packet
1232      *       arrives on the new port, but this would also happen if the
1233      *       ordering were reversed.
1234      *
1235      *     - In the dpif_port_remove() case, A might wake up spuriously, but
1236      *       that is harmless. */
1237
1238     ovs_mutex_lock(&dp_netdev_mutex);
1239     LIST_FOR_EACH (port, node, &get_dp_netdev(dpif)->port_list) {
1240         if (port->rx) {
1241             netdev_rx_wait(port->rx);
1242         }
1243     }
1244     ovs_mutex_unlock(&dp_netdev_mutex);
1245 }
1246
1247 static void
1248 dp_netdev_output_port(void *dp_, struct ofpbuf *packet,
1249                       const struct flow *flow OVS_UNUSED, odp_port_t out_port)
1250 {
1251     struct dp_netdev *dp = dp_;
1252     struct dp_netdev_port *p = dp->ports[odp_to_u32(out_port)];
1253     if (p) {
1254         netdev_send(p->netdev, packet);
1255     }
1256 }
1257
1258 static int
1259 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1260                            int queue_no, const struct flow *flow,
1261                            const struct nlattr *userdata)
1262 {
1263     struct dp_netdev_queue *q = &dp->queues[queue_no];
1264     if (q->head - q->tail < MAX_QUEUE_LEN) {
1265         struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1266         struct dpif_upcall *upcall = &u->upcall;
1267         struct ofpbuf *buf = &u->buf;
1268         size_t buf_size;
1269
1270         upcall->type = queue_no;
1271
1272         /* Allocate buffer big enough for everything. */
1273         buf_size = ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size;
1274         if (userdata) {
1275             buf_size += NLA_ALIGN(userdata->nla_len);
1276         }
1277         ofpbuf_init(buf, buf_size);
1278
1279         /* Put ODP flow. */
1280         odp_flow_key_from_flow(buf, flow, flow->in_port.odp_port);
1281         upcall->key = buf->data;
1282         upcall->key_len = buf->size;
1283
1284         /* Put userdata. */
1285         if (userdata) {
1286             upcall->userdata = ofpbuf_put(buf, userdata,
1287                                           NLA_ALIGN(userdata->nla_len));
1288         }
1289
1290         /* Put packet.
1291          *
1292          * We adjust 'data' and 'size' in 'buf' so that only the packet itself
1293          * is visible in 'upcall->packet'.  The ODP flow and (if present)
1294          * userdata become part of the headroom. */
1295         ofpbuf_put_zeros(buf, 2);
1296         buf->data = ofpbuf_put(buf, packet->data, packet->size);
1297         buf->size = packet->size;
1298         upcall->packet = buf;
1299
1300         seq_change(dp->queue_seq);
1301
1302         return 0;
1303     } else {
1304         dp->n_lost++;
1305         return ENOBUFS;
1306     }
1307 }
1308
1309 static void
1310 dp_netdev_action_userspace(void *dp, struct ofpbuf *packet,
1311                            const struct flow *key,
1312                            const struct nlattr *a)
1313 {
1314     const struct nlattr *userdata;
1315
1316     userdata = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
1317     dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1318 }
1319
1320 static void
1321 dp_netdev_execute_actions(struct dp_netdev *dp,
1322                           struct ofpbuf *packet, struct flow *key,
1323                           const struct nlattr *actions,
1324                           size_t actions_len)
1325 {
1326     odp_execute_actions(dp, packet, key, actions, actions_len,
1327                         dp_netdev_output_port, dp_netdev_action_userspace);
1328 }
1329
1330 #define DPIF_NETDEV_CLASS_FUNCTIONS                     \
1331     dpif_netdev_enumerate,                              \
1332     dpif_netdev_port_open_type,                         \
1333     dpif_netdev_open,                                   \
1334     dpif_netdev_close,                                  \
1335     dpif_netdev_destroy,                                \
1336     dpif_netdev_run,                                    \
1337     dpif_netdev_wait,                                   \
1338     dpif_netdev_get_stats,                              \
1339     dpif_netdev_port_add,                               \
1340     dpif_netdev_port_del,                               \
1341     dpif_netdev_port_query_by_number,                   \
1342     dpif_netdev_port_query_by_name,                     \
1343     dpif_netdev_get_max_ports,                          \
1344     NULL,                       /* port_get_pid */      \
1345     dpif_netdev_port_dump_start,                        \
1346     dpif_netdev_port_dump_next,                         \
1347     dpif_netdev_port_dump_done,                         \
1348     dpif_netdev_port_poll,                              \
1349     dpif_netdev_port_poll_wait,                         \
1350     dpif_netdev_flow_get,                               \
1351     dpif_netdev_flow_put,                               \
1352     dpif_netdev_flow_del,                               \
1353     dpif_netdev_flow_flush,                             \
1354     dpif_netdev_flow_dump_start,                        \
1355     dpif_netdev_flow_dump_next,                         \
1356     dpif_netdev_flow_dump_done,                         \
1357     dpif_netdev_execute,                                \
1358     NULL,                       /* operate */           \
1359     dpif_netdev_recv_set,                               \
1360     dpif_netdev_queue_to_priority,                      \
1361     dpif_netdev_recv,                                   \
1362     dpif_netdev_recv_wait,                              \
1363     dpif_netdev_recv_purge,                             \
1364
1365 const struct dpif_class dpif_netdev_class = {
1366     "netdev",
1367     DPIF_NETDEV_CLASS_FUNCTIONS
1368 };
1369
1370 const struct dpif_class dpif_planetlab_class = {
1371     "planetlab",
1372     DPIF_NETDEV_CLASS_FUNCTIONS
1373 };
1374
1375 static void
1376 dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
1377                               const char *argv[], void *aux OVS_UNUSED)
1378 {
1379     struct dp_netdev_port *port;
1380     struct dp_netdev *dp;
1381     int port_no;
1382
1383     dp = shash_find_data(&dp_netdevs, argv[1]);
1384     if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
1385         unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
1386         return;
1387     }
1388
1389     if (get_port_by_name(dp, argv[2], &port)) {
1390         unixctl_command_reply_error(conn, "unknown port");
1391         return;
1392     }
1393
1394     port_no = atoi(argv[3]);
1395     if (port_no <= 0 || port_no >= MAX_PORTS) {
1396         unixctl_command_reply_error(conn, "bad port number");
1397         return;
1398     }
1399     if (dp->ports[port_no]) {
1400         unixctl_command_reply_error(conn, "port number already in use");
1401         return;
1402     }
1403     dp->ports[odp_to_u32(port->port_no)] = NULL;
1404     dp->ports[port_no] = port;
1405     port->port_no = u32_to_odp(port_no);
1406     seq_change(dp->port_seq);
1407     unixctl_command_reply(conn, NULL);
1408 }
1409
1410 static void
1411 dpif_dummy_register__(const char *type)
1412 {
1413     struct dpif_class *class;
1414
1415     class = xmalloc(sizeof *class);
1416     *class = dpif_netdev_class;
1417     class->type = xstrdup(type);
1418     dp_register_provider(class);
1419 }
1420
1421 void
1422 dpif_dummy_register(bool override)
1423 {
1424     if (override) {
1425         struct sset types;
1426         const char *type;
1427
1428         sset_init(&types);
1429         dp_enumerate_types(&types);
1430         SSET_FOR_EACH (type, &types) {
1431             if (!dp_unregister_provider(type)) {
1432                 dpif_dummy_register__(type);
1433             }
1434         }
1435         sset_destroy(&types);
1436     }
1437
1438     dpif_dummy_register__("dummy");
1439
1440     unixctl_command_register("dpif-dummy/change-port-number",
1441                              "DP PORT NEW-NUMBER",
1442                              3, 3, dpif_dummy_change_port_number, NULL);
1443 }
1444