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