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