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