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