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