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