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