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