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