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