ovs-dpctl: Add mega flow support
[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 "csum.h"
35 #include "dpif.h"
36 #include "dpif-provider.h"
37 #include "dummy.h"
38 #include "dynamic-string.h"
39 #include "flow.h"
40 #include "hmap.h"
41 #include "list.h"
42 #include "netdev.h"
43 #include "netdev-vport.h"
44 #include "netlink.h"
45 #include "odp-execute.h"
46 #include "odp-util.h"
47 #include "ofp-print.h"
48 #include "ofpbuf.h"
49 #include "packets.h"
50 #include "poll-loop.h"
51 #include "random.h"
52 #include "shash.h"
53 #include "sset.h"
54 #include "timeval.h"
55 #include "util.h"
56 #include "vlog.h"
57
58 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
59
60 /* Configuration parameters. */
61 enum { MAX_PORTS = 256 };       /* Maximum number of ports. */
62 enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
63
64 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
65  * headers to be aligned on a 4-byte boundary.  */
66 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
67
68 /* Queues. */
69 enum { N_QUEUES = 2 };          /* Number of queues for dpif_recv(). */
70 enum { MAX_QUEUE_LEN = 128 };   /* Maximum number of packets per queue. */
71 enum { QUEUE_MASK = MAX_QUEUE_LEN - 1 };
72 BUILD_ASSERT_DECL(IS_POW2(MAX_QUEUE_LEN));
73
74 struct dp_netdev_upcall {
75     struct dpif_upcall upcall;  /* Queued upcall information. */
76     struct ofpbuf buf;          /* ofpbuf instance for upcall.packet. */
77 };
78
79 struct dp_netdev_queue {
80     struct dp_netdev_upcall upcalls[MAX_QUEUE_LEN];
81     unsigned int head, tail;
82 };
83
84 /* Datapath based on the network device interface from netdev.h. */
85 struct dp_netdev {
86     const struct dpif_class *class;
87     char *name;
88     int open_cnt;
89     bool destroyed;
90
91     struct dp_netdev_queue queues[N_QUEUES];
92     struct hmap flow_table;     /* Flow table. */
93
94     /* Statistics. */
95     long long int n_hit;        /* Number of flow table matches. */
96     long long int n_missed;     /* Number of flow table misses. */
97     long long int n_lost;       /* Number of misses not passed to client. */
98
99     /* Ports. */
100     struct dp_netdev_port *ports[MAX_PORTS];
101     struct list port_list;
102     unsigned int serial;
103 };
104
105 /* A port in a netdev-based datapath. */
106 struct dp_netdev_port {
107     int port_no;                /* Index into dp_netdev's 'ports'. */
108     struct list node;           /* Element in dp_netdev's 'port_list'. */
109     struct netdev *netdev;
110     struct netdev_saved_flags *sf;
111     struct netdev_rx *rx;
112     char *type;                 /* Port type as requested by user. */
113 };
114
115 /* A flow in dp_netdev's 'flow_table'. */
116 struct dp_netdev_flow {
117     struct hmap_node node;      /* Element in dp_netdev's 'flow_table'. */
118     struct flow key;
119
120     /* Statistics. */
121     long long int used;         /* Last used time, in monotonic msecs. */
122     long long int packet_count; /* Number of packets matched. */
123     long long int byte_count;   /* Number of bytes matched. */
124     uint8_t tcp_flags;          /* Bitwise-OR of seen tcp_flags values. */
125
126     /* Actions. */
127     struct nlattr *actions;
128     size_t actions_len;
129 };
130
131 /* Interface to netdev-based datapath. */
132 struct dpif_netdev {
133     struct dpif dpif;
134     struct dp_netdev *dp;
135     unsigned int dp_serial;
136 };
137
138 /* All netdev-based datapaths. */
139 static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
140
141 /* Maximum port MTU seen so far. */
142 static int max_mtu = ETH_PAYLOAD_MAX;
143
144 static int get_port_by_number(struct dp_netdev *, uint32_t port_no,
145                               struct dp_netdev_port **portp);
146 static int get_port_by_name(struct dp_netdev *, const char *devname,
147                             struct dp_netdev_port **portp);
148 static void dp_netdev_free(struct dp_netdev *);
149 static void dp_netdev_flow_flush(struct dp_netdev *);
150 static int do_add_port(struct dp_netdev *, const char *devname,
151                        const char *type, uint32_t port_no);
152 static int do_del_port(struct dp_netdev *, uint32_t port_no);
153 static int dpif_netdev_open(const struct dpif_class *, const char *name,
154                             bool create, struct dpif **);
155 static int dp_netdev_output_userspace(struct dp_netdev *, const struct ofpbuf *,
156                                     int queue_no, const struct flow *,
157                                     const struct nlattr *userdata);
158 static void dp_netdev_execute_actions(struct dp_netdev *,
159                                       struct ofpbuf *, struct flow *,
160                                       const struct nlattr *actions,
161                                       size_t actions_len);
162 static void dp_netdev_port_input(struct dp_netdev *dp,
163                                  struct dp_netdev_port *port,
164                                  struct ofpbuf *packet, uint32_t skb_priority,
165                                  uint32_t skb_mark, const struct flow_tnl *tnl);
166
167 static struct dpif_netdev *
168 dpif_netdev_cast(const struct dpif *dpif)
169 {
170     ovs_assert(dpif->dpif_class->open == dpif_netdev_open);
171     return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
172 }
173
174 static struct dp_netdev *
175 get_dp_netdev(const struct dpif *dpif)
176 {
177     return dpif_netdev_cast(dpif)->dp;
178 }
179
180 static int
181 dpif_netdev_enumerate(struct sset *all_dps)
182 {
183     struct shash_node *node;
184
185     SHASH_FOR_EACH(node, &dp_netdevs) {
186         sset_add(all_dps, node->name);
187     }
188     return 0;
189 }
190
191 static bool
192 dpif_netdev_class_is_dummy(const struct dpif_class *class)
193 {
194     return class != &dpif_netdev_class;
195 }
196
197 static const char *
198 dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
199 {
200     return strcmp(type, "internal") ? type
201                   : dpif_netdev_class_is_dummy(class) ? "dummy"
202                   : "tap";
203 }
204
205 static struct dpif *
206 create_dpif_netdev(struct dp_netdev *dp)
207 {
208     uint16_t netflow_id = hash_string(dp->name, 0);
209     struct dpif_netdev *dpif;
210
211     dp->open_cnt++;
212
213     dpif = xmalloc(sizeof *dpif);
214     dpif_init(&dpif->dpif, dp->class, dp->name, netflow_id >> 8, netflow_id);
215     dpif->dp = dp;
216     dpif->dp_serial = dp->serial;
217
218     return &dpif->dpif;
219 }
220
221 static int
222 choose_port(struct dp_netdev *dp, const char *name)
223 {
224     int port_no;
225
226     if (dp->class != &dpif_netdev_class) {
227         const char *p;
228         int start_no = 0;
229
230         /* If the port name begins with "br", start the number search at
231          * 100 to make writing tests easier. */
232         if (!strncmp(name, "br", 2)) {
233             start_no = 100;
234         }
235
236         /* If the port name contains a number, try to assign that port number.
237          * This can make writing unit tests easier because port numbers are
238          * predictable. */
239         for (p = name; *p != '\0'; p++) {
240             if (isdigit((unsigned char) *p)) {
241                 port_no = start_no + strtol(p, NULL, 10);
242                 if (port_no > 0 && port_no < MAX_PORTS
243                     && !dp->ports[port_no]) {
244                     return port_no;
245                 }
246                 break;
247             }
248         }
249     }
250
251     for (port_no = 1; port_no < MAX_PORTS; port_no++) {
252         if (!dp->ports[port_no]) {
253             return port_no;
254         }
255     }
256
257     return -1;
258 }
259
260 static int
261 create_dp_netdev(const char *name, const struct dpif_class *class,
262                  struct dp_netdev **dpp)
263 {
264     struct dp_netdev *dp;
265     int error;
266     int i;
267
268     dp = xzalloc(sizeof *dp);
269     dp->class = class;
270     dp->name = xstrdup(name);
271     dp->open_cnt = 0;
272     for (i = 0; i < N_QUEUES; i++) {
273         dp->queues[i].head = dp->queues[i].tail = 0;
274     }
275     hmap_init(&dp->flow_table);
276     list_init(&dp->port_list);
277
278     error = do_add_port(dp, name, "internal", OVSP_LOCAL);
279     if (error) {
280         dp_netdev_free(dp);
281         return error;
282     }
283
284     shash_add(&dp_netdevs, name, dp);
285
286     *dpp = dp;
287     return 0;
288 }
289
290 static int
291 dpif_netdev_open(const struct dpif_class *class, const char *name,
292                  bool create, struct dpif **dpifp)
293 {
294     struct dp_netdev *dp;
295
296     dp = shash_find_data(&dp_netdevs, name);
297     if (!dp) {
298         if (!create) {
299             return ENODEV;
300         } else {
301             int error = create_dp_netdev(name, class, &dp);
302             if (error) {
303                 return error;
304             }
305             ovs_assert(dp != NULL);
306         }
307     } else {
308         if (dp->class != class) {
309             return EINVAL;
310         } else if (create) {
311             return EEXIST;
312         }
313     }
314
315     *dpifp = create_dpif_netdev(dp);
316     return 0;
317 }
318
319 static void
320 dp_netdev_purge_queues(struct dp_netdev *dp)
321 {
322     int i;
323
324     for (i = 0; i < N_QUEUES; i++) {
325         struct dp_netdev_queue *q = &dp->queues[i];
326
327         while (q->tail != q->head) {
328             struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
329             ofpbuf_uninit(&u->buf);
330         }
331     }
332 }
333
334 static void
335 dp_netdev_free(struct dp_netdev *dp)
336 {
337     struct dp_netdev_port *port, *next;
338
339     dp_netdev_flow_flush(dp);
340     LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
341         do_del_port(dp, port->port_no);
342     }
343     dp_netdev_purge_queues(dp);
344     hmap_destroy(&dp->flow_table);
345     free(dp->name);
346     free(dp);
347 }
348
349 static void
350 dpif_netdev_close(struct dpif *dpif)
351 {
352     struct dp_netdev *dp = get_dp_netdev(dpif);
353     ovs_assert(dp->open_cnt > 0);
354     if (--dp->open_cnt == 0 && dp->destroyed) {
355         shash_find_and_delete(&dp_netdevs, dp->name);
356         dp_netdev_free(dp);
357     }
358     free(dpif);
359 }
360
361 static int
362 dpif_netdev_destroy(struct dpif *dpif)
363 {
364     struct dp_netdev *dp = get_dp_netdev(dpif);
365     dp->destroyed = true;
366     return 0;
367 }
368
369 static int
370 dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
371 {
372     struct dp_netdev *dp = get_dp_netdev(dpif);
373     stats->n_flows = hmap_count(&dp->flow_table);
374     stats->n_hit = dp->n_hit;
375     stats->n_missed = dp->n_missed;
376     stats->n_lost = dp->n_lost;
377     return 0;
378 }
379
380 static int
381 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
382             uint32_t port_no)
383 {
384     struct netdev_saved_flags *sf;
385     struct dp_netdev_port *port;
386     struct netdev *netdev;
387     struct netdev_rx *rx;
388     const char *open_type;
389     int mtu;
390     int error;
391
392     /* XXX reject devices already in some dp_netdev. */
393
394     /* Open and validate network device. */
395     open_type = dpif_netdev_port_open_type(dp->class, type);
396     error = netdev_open(devname, open_type, &netdev);
397     if (error) {
398         return error;
399     }
400     /* XXX reject loopback devices */
401     /* XXX reject non-Ethernet devices */
402
403     error = netdev_rx_open(netdev, &rx);
404     if (error
405         && !(error == EOPNOTSUPP && dpif_netdev_class_is_dummy(dp->class))) {
406         VLOG_ERR("%s: cannot receive packets on this network device (%s)",
407                  devname, strerror(errno));
408         netdev_close(netdev);
409         return error;
410     }
411
412     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, &sf);
413     if (error) {
414         netdev_rx_close(rx);
415         netdev_close(netdev);
416         return error;
417     }
418
419     port = xmalloc(sizeof *port);
420     port->port_no = port_no;
421     port->netdev = netdev;
422     port->sf = sf;
423     port->rx = rx;
424     port->type = xstrdup(type);
425
426     error = netdev_get_mtu(netdev, &mtu);
427     if (!error && mtu > max_mtu) {
428         max_mtu = mtu;
429     }
430
431     list_push_back(&dp->port_list, &port->node);
432     dp->ports[port_no] = port;
433     dp->serial++;
434
435     return 0;
436 }
437
438 static int
439 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
440                      uint32_t *port_nop)
441 {
442     struct dp_netdev *dp = get_dp_netdev(dpif);
443     char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
444     const char *dpif_port;
445     int port_no;
446
447     dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
448     if (*port_nop != UINT32_MAX) {
449         if (*port_nop >= MAX_PORTS) {
450             return EFBIG;
451         } else if (dp->ports[*port_nop]) {
452             return EBUSY;
453         }
454         port_no = *port_nop;
455     } else {
456         port_no = choose_port(dp, dpif_port);
457     }
458     if (port_no >= 0) {
459         *port_nop = port_no;
460         return do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
461     }
462     return EFBIG;
463 }
464
465 static int
466 dpif_netdev_port_del(struct dpif *dpif, uint32_t port_no)
467 {
468     struct dp_netdev *dp = get_dp_netdev(dpif);
469     return port_no == OVSP_LOCAL ? EINVAL : do_del_port(dp, port_no);
470 }
471
472 static bool
473 is_valid_port_number(uint32_t port_no)
474 {
475     return port_no < MAX_PORTS;
476 }
477
478 static int
479 get_port_by_number(struct dp_netdev *dp,
480                    uint32_t port_no, struct dp_netdev_port **portp)
481 {
482     if (!is_valid_port_number(port_no)) {
483         *portp = NULL;
484         return EINVAL;
485     } else {
486         *portp = dp->ports[port_no];
487         return *portp ? 0 : ENOENT;
488     }
489 }
490
491 static int
492 get_port_by_name(struct dp_netdev *dp,
493                  const char *devname, struct dp_netdev_port **portp)
494 {
495     struct dp_netdev_port *port;
496
497     LIST_FOR_EACH (port, node, &dp->port_list) {
498         if (!strcmp(netdev_get_name(port->netdev), devname)) {
499             *portp = port;
500             return 0;
501         }
502     }
503     return ENOENT;
504 }
505
506 static int
507 do_del_port(struct dp_netdev *dp, uint32_t port_no)
508 {
509     struct dp_netdev_port *port;
510     int error;
511
512     error = get_port_by_number(dp, port_no, &port);
513     if (error) {
514         return error;
515     }
516
517     list_remove(&port->node);
518     dp->ports[port->port_no] = NULL;
519     dp->serial++;
520
521     netdev_close(port->netdev);
522     netdev_restore_flags(port->sf);
523     netdev_rx_close(port->rx);
524     free(port->type);
525     free(port);
526
527     return 0;
528 }
529
530 static void
531 answer_port_query(const struct dp_netdev_port *port,
532                   struct dpif_port *dpif_port)
533 {
534     dpif_port->name = xstrdup(netdev_get_name(port->netdev));
535     dpif_port->type = xstrdup(port->type);
536     dpif_port->port_no = port->port_no;
537 }
538
539 static int
540 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
541                                  struct dpif_port *dpif_port)
542 {
543     struct dp_netdev *dp = get_dp_netdev(dpif);
544     struct dp_netdev_port *port;
545     int error;
546
547     error = get_port_by_number(dp, port_no, &port);
548     if (!error && dpif_port) {
549         answer_port_query(port, dpif_port);
550     }
551     return error;
552 }
553
554 static int
555 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
556                                struct dpif_port *dpif_port)
557 {
558     struct dp_netdev *dp = get_dp_netdev(dpif);
559     struct dp_netdev_port *port;
560     int error;
561
562     error = get_port_by_name(dp, devname, &port);
563     if (!error && dpif_port) {
564         answer_port_query(port, dpif_port);
565     }
566     return error;
567 }
568
569 static int
570 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
571 {
572     return MAX_PORTS;
573 }
574
575 static void
576 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
577 {
578     hmap_remove(&dp->flow_table, &flow->node);
579     free(flow->actions);
580     free(flow);
581 }
582
583 static void
584 dp_netdev_flow_flush(struct dp_netdev *dp)
585 {
586     struct dp_netdev_flow *flow, *next;
587
588     HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
589         dp_netdev_free_flow(dp, flow);
590     }
591 }
592
593 static int
594 dpif_netdev_flow_flush(struct dpif *dpif)
595 {
596     struct dp_netdev *dp = get_dp_netdev(dpif);
597     dp_netdev_flow_flush(dp);
598     return 0;
599 }
600
601 struct dp_netdev_port_state {
602     uint32_t port_no;
603     char *name;
604 };
605
606 static int
607 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
608 {
609     *statep = xzalloc(sizeof(struct dp_netdev_port_state));
610     return 0;
611 }
612
613 static int
614 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
615                            struct dpif_port *dpif_port)
616 {
617     struct dp_netdev_port_state *state = state_;
618     struct dp_netdev *dp = get_dp_netdev(dpif);
619     uint32_t port_no;
620
621     for (port_no = state->port_no; port_no < MAX_PORTS; port_no++) {
622         struct dp_netdev_port *port = dp->ports[port_no];
623         if (port) {
624             free(state->name);
625             state->name = xstrdup(netdev_get_name(port->netdev));
626             dpif_port->name = state->name;
627             dpif_port->type = port->type;
628             dpif_port->port_no = port->port_no;
629             state->port_no = port_no + 1;
630             return 0;
631         }
632     }
633     return EOF;
634 }
635
636 static int
637 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
638 {
639     struct dp_netdev_port_state *state = state_;
640     free(state->name);
641     free(state);
642     return 0;
643 }
644
645 static int
646 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
647 {
648     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
649     if (dpif->dp_serial != dpif->dp->serial) {
650         dpif->dp_serial = dpif->dp->serial;
651         return ENOBUFS;
652     } else {
653         return EAGAIN;
654     }
655 }
656
657 static void
658 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
659 {
660     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
661     if (dpif->dp_serial != dpif->dp->serial) {
662         poll_immediate_wake();
663     }
664 }
665
666 static struct dp_netdev_flow *
667 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
668 {
669     struct dp_netdev_flow *flow;
670
671     HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
672         if (flow_equal(&flow->key, key)) {
673             return flow;
674         }
675     }
676     return NULL;
677 }
678
679 static void
680 get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
681 {
682     stats->n_packets = flow->packet_count;
683     stats->n_bytes = flow->byte_count;
684     stats->used = flow->used;
685     stats->tcp_flags = flow->tcp_flags;
686 }
687
688 static int
689 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
690                               struct flow *flow)
691 {
692     if (odp_flow_key_to_flow(key, key_len, flow) != ODP_FIT_PERFECT) {
693         /* This should not happen: it indicates that odp_flow_key_from_flow()
694          * and odp_flow_key_to_flow() disagree on the acceptable form of a
695          * flow.  Log the problem as an error, with enough details to enable
696          * debugging. */
697         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
698
699         if (!VLOG_DROP_ERR(&rl)) {
700             struct ds s;
701
702             ds_init(&s);
703             odp_flow_key_format(key, key_len, &s);
704             VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
705             ds_destroy(&s);
706         }
707
708         return EINVAL;
709     }
710
711     if (flow->in_port < OFPP_MAX
712         ? flow->in_port >= MAX_PORTS
713         : flow->in_port != OFPP_LOCAL && flow->in_port != OFPP_NONE) {
714         return EINVAL;
715     }
716
717     return 0;
718 }
719
720 static int
721 dpif_netdev_flow_get(const struct dpif *dpif,
722                      const struct nlattr *nl_key, size_t nl_key_len,
723                      struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
724 {
725     struct dp_netdev *dp = get_dp_netdev(dpif);
726     struct dp_netdev_flow *flow;
727     struct flow key;
728     int error;
729
730     error = dpif_netdev_flow_from_nlattrs(nl_key, nl_key_len, &key);
731     if (error) {
732         return error;
733     }
734
735     flow = dp_netdev_lookup_flow(dp, &key);
736     if (!flow) {
737         return ENOENT;
738     }
739
740     if (stats) {
741         get_dpif_flow_stats(flow, stats);
742     }
743     if (actionsp) {
744         *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
745     }
746     return 0;
747 }
748
749 static int
750 set_flow_actions(struct dp_netdev_flow *flow,
751                  const struct nlattr *actions, size_t actions_len)
752 {
753     flow->actions = xrealloc(flow->actions, actions_len);
754     flow->actions_len = actions_len;
755     memcpy(flow->actions, actions, actions_len);
756     return 0;
757 }
758
759 static int
760 dp_netdev_flow_add(struct dp_netdev *dp, const struct flow *key,
761                    const struct nlattr *actions, size_t actions_len)
762 {
763     struct dp_netdev_flow *flow;
764     int error;
765
766     flow = xzalloc(sizeof *flow);
767     flow->key = *key;
768
769     error = set_flow_actions(flow, actions, actions_len);
770     if (error) {
771         free(flow);
772         return error;
773     }
774
775     hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
776     return 0;
777 }
778
779 static void
780 clear_stats(struct dp_netdev_flow *flow)
781 {
782     flow->used = 0;
783     flow->packet_count = 0;
784     flow->byte_count = 0;
785     flow->tcp_flags = 0;
786 }
787
788 static int
789 dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
790 {
791     struct dp_netdev *dp = get_dp_netdev(dpif);
792     struct dp_netdev_flow *flow;
793     struct flow key;
794     int error;
795
796     error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
797     if (error) {
798         return error;
799     }
800
801     flow = dp_netdev_lookup_flow(dp, &key);
802     if (!flow) {
803         if (put->flags & DPIF_FP_CREATE) {
804             if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
805                 if (put->stats) {
806                     memset(put->stats, 0, sizeof *put->stats);
807                 }
808                 return dp_netdev_flow_add(dp, &key, put->actions,
809                                           put->actions_len);
810             } else {
811                 return EFBIG;
812             }
813         } else {
814             return ENOENT;
815         }
816     } else {
817         if (put->flags & DPIF_FP_MODIFY) {
818             int error = set_flow_actions(flow, put->actions, put->actions_len);
819             if (!error) {
820                 if (put->stats) {
821                     get_dpif_flow_stats(flow, put->stats);
822                 }
823                 if (put->flags & DPIF_FP_ZERO_STATS) {
824                     clear_stats(flow);
825                 }
826             }
827             return error;
828         } else {
829             return EEXIST;
830         }
831     }
832 }
833
834 static int
835 dpif_netdev_flow_del(struct dpif *dpif, const struct dpif_flow_del *del)
836 {
837     struct dp_netdev *dp = get_dp_netdev(dpif);
838     struct dp_netdev_flow *flow;
839     struct flow key;
840     int error;
841
842     error = dpif_netdev_flow_from_nlattrs(del->key, del->key_len, &key);
843     if (error) {
844         return error;
845     }
846
847     flow = dp_netdev_lookup_flow(dp, &key);
848     if (flow) {
849         if (del->stats) {
850             get_dpif_flow_stats(flow, del->stats);
851         }
852         dp_netdev_free_flow(dp, flow);
853         return 0;
854     } else {
855         return ENOENT;
856     }
857 }
858
859 struct dp_netdev_flow_state {
860     uint32_t bucket;
861     uint32_t offset;
862     struct nlattr *actions;
863     struct odputil_keybuf keybuf;
864     struct dpif_flow_stats stats;
865 };
866
867 static int
868 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
869 {
870     struct dp_netdev_flow_state *state;
871
872     *statep = state = xmalloc(sizeof *state);
873     state->bucket = 0;
874     state->offset = 0;
875     state->actions = NULL;
876     return 0;
877 }
878
879 static int
880 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
881                            const struct nlattr **key, size_t *key_len,
882                            const struct nlattr **mask, size_t *mask_len,
883                            const struct nlattr **actions, size_t *actions_len,
884                            const struct dpif_flow_stats **stats)
885 {
886     struct dp_netdev_flow_state *state = state_;
887     struct dp_netdev *dp = get_dp_netdev(dpif);
888     struct dp_netdev_flow *flow;
889     struct hmap_node *node;
890
891     node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
892     if (!node) {
893         return EOF;
894     }
895
896     flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
897
898     if (key) {
899         struct ofpbuf buf;
900
901         ofpbuf_use_stack(&buf, &state->keybuf, sizeof state->keybuf);
902         odp_flow_key_from_flow(&buf, &flow->key, flow->key.in_port);
903
904         *key = buf.data;
905         *key_len = buf.size;
906     }
907
908     if (mask) {
909         *mask = NULL;
910         *mask_len = 0;
911     }
912
913     if (actions) {
914         free(state->actions);
915         state->actions = xmemdup(flow->actions, flow->actions_len);
916
917         *actions = state->actions;
918         *actions_len = flow->actions_len;
919     }
920
921     if (stats) {
922         get_dpif_flow_stats(flow, &state->stats);
923         *stats = &state->stats;
924     }
925
926     return 0;
927 }
928
929 static int
930 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
931 {
932     struct dp_netdev_flow_state *state = state_;
933
934     free(state->actions);
935     free(state);
936     return 0;
937 }
938
939 static int
940 dpif_netdev_execute(struct dpif *dpif, const struct dpif_execute *execute)
941 {
942     struct dp_netdev *dp = get_dp_netdev(dpif);
943     struct ofpbuf copy;
944     struct flow key;
945     int error;
946
947     if (execute->packet->size < ETH_HEADER_LEN ||
948         execute->packet->size > UINT16_MAX) {
949         return EINVAL;
950     }
951
952     /* Make a deep copy of 'packet', because we might modify its data. */
953     ofpbuf_init(&copy, DP_NETDEV_HEADROOM + execute->packet->size);
954     ofpbuf_reserve(&copy, DP_NETDEV_HEADROOM);
955     ofpbuf_put(&copy, execute->packet->data, execute->packet->size);
956
957     flow_extract(&copy, 0, 0, NULL, -1, &key);
958     error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
959                                           &key);
960     if (!error) {
961         dp_netdev_execute_actions(dp, &copy, &key,
962                                   execute->actions, execute->actions_len);
963     }
964
965     ofpbuf_uninit(&copy);
966     return error;
967 }
968
969 static int
970 dpif_netdev_recv_set(struct dpif *dpif OVS_UNUSED, bool enable OVS_UNUSED)
971 {
972     return 0;
973 }
974
975 static int
976 dpif_netdev_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
977                               uint32_t queue_id, uint32_t *priority)
978 {
979     *priority = queue_id;
980     return 0;
981 }
982
983 static struct dp_netdev_queue *
984 find_nonempty_queue(struct dpif *dpif)
985 {
986     struct dp_netdev *dp = get_dp_netdev(dpif);
987     int i;
988
989     for (i = 0; i < N_QUEUES; i++) {
990         struct dp_netdev_queue *q = &dp->queues[i];
991         if (q->head != q->tail) {
992             return q;
993         }
994     }
995     return NULL;
996 }
997
998 static int
999 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
1000                  struct ofpbuf *buf)
1001 {
1002     struct dp_netdev_queue *q = find_nonempty_queue(dpif);
1003     if (q) {
1004         struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
1005
1006         *upcall = u->upcall;
1007         upcall->packet = buf;
1008
1009         ofpbuf_uninit(buf);
1010         *buf = u->buf;
1011
1012         return 0;
1013     } else {
1014         return EAGAIN;
1015     }
1016 }
1017
1018 static void
1019 dpif_netdev_recv_wait(struct dpif *dpif)
1020 {
1021     if (find_nonempty_queue(dpif)) {
1022         poll_immediate_wake();
1023     } else {
1024         /* No messages ready to be received, and dp_wait() will ensure that we
1025          * wake up to queue new messages, so there is nothing to do. */
1026     }
1027 }
1028
1029 static void
1030 dpif_netdev_recv_purge(struct dpif *dpif)
1031 {
1032     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1033     dp_netdev_purge_queues(dpif_netdev->dp);
1034 }
1035 \f
1036 static void
1037 dp_netdev_flow_used(struct dp_netdev_flow *flow, const struct ofpbuf *packet)
1038 {
1039     flow->used = time_msec();
1040     flow->packet_count++;
1041     flow->byte_count += packet->size;
1042     flow->tcp_flags |= packet_get_tcp_flags(packet, &flow->key);
1043 }
1044
1045 static void
1046 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1047                      struct ofpbuf *packet, uint32_t skb_priority,
1048                      uint32_t skb_mark, const struct flow_tnl *tnl)
1049 {
1050     struct dp_netdev_flow *flow;
1051     struct flow key;
1052
1053     if (packet->size < ETH_HEADER_LEN) {
1054         return;
1055     }
1056     flow_extract(packet, skb_priority, skb_mark, tnl, port->port_no, &key);
1057     flow = dp_netdev_lookup_flow(dp, &key);
1058     if (flow) {
1059         dp_netdev_flow_used(flow, packet);
1060         dp_netdev_execute_actions(dp, packet, &key,
1061                                   flow->actions, flow->actions_len);
1062         dp->n_hit++;
1063     } else {
1064         dp->n_missed++;
1065         dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, NULL);
1066     }
1067 }
1068
1069 static void
1070 dpif_netdev_run(struct dpif *dpif)
1071 {
1072     struct dp_netdev *dp = get_dp_netdev(dpif);
1073     struct dp_netdev_port *port;
1074     struct ofpbuf packet;
1075
1076     ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1077
1078     LIST_FOR_EACH (port, node, &dp->port_list) {
1079         int error;
1080
1081         /* Reset packet contents. */
1082         ofpbuf_clear(&packet);
1083         ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1084
1085         error = port->rx ? netdev_rx_recv(port->rx, &packet) : EOPNOTSUPP;
1086         if (!error) {
1087             dp_netdev_port_input(dp, port, &packet, 0, 0, NULL);
1088         } else if (error != EAGAIN && error != EOPNOTSUPP) {
1089             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1090
1091             VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1092                         netdev_get_name(port->netdev), strerror(error));
1093         }
1094     }
1095     ofpbuf_uninit(&packet);
1096 }
1097
1098 static void
1099 dpif_netdev_wait(struct dpif *dpif)
1100 {
1101     struct dp_netdev *dp = get_dp_netdev(dpif);
1102     struct dp_netdev_port *port;
1103
1104     LIST_FOR_EACH (port, node, &dp->port_list) {
1105         if (port->rx) {
1106             netdev_rx_wait(port->rx);
1107         }
1108     }
1109 }
1110
1111 static void
1112 dp_netdev_output_port(void *dp_, struct ofpbuf *packet, uint32_t out_port)
1113 {
1114     struct dp_netdev *dp = dp_;
1115     struct dp_netdev_port *p = dp->ports[out_port];
1116     if (p) {
1117         netdev_send(p->netdev, packet);
1118     }
1119 }
1120
1121 static int
1122 dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
1123                            int queue_no, const struct flow *flow,
1124                            const struct nlattr *userdata)
1125 {
1126     struct dp_netdev_queue *q = &dp->queues[queue_no];
1127     if (q->head - q->tail < MAX_QUEUE_LEN) {
1128         struct dp_netdev_upcall *u = &q->upcalls[q->head++ & QUEUE_MASK];
1129         struct dpif_upcall *upcall = &u->upcall;
1130         struct ofpbuf *buf = &u->buf;
1131         size_t buf_size;
1132
1133         upcall->type = queue_no;
1134
1135         /* Allocate buffer big enough for everything. */
1136         buf_size = ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size;
1137         if (userdata) {
1138             buf_size += NLA_ALIGN(userdata->nla_len);
1139         }
1140         ofpbuf_init(buf, buf_size);
1141
1142         /* Put ODP flow. */
1143         odp_flow_key_from_flow(buf, flow, flow->in_port);
1144         upcall->key = buf->data;
1145         upcall->key_len = buf->size;
1146
1147         /* Put userdata. */
1148         if (userdata) {
1149             upcall->userdata = ofpbuf_put(buf, userdata,
1150                                           NLA_ALIGN(userdata->nla_len));
1151         }
1152
1153         /* Put packet.
1154          *
1155          * We adjust 'data' and 'size' in 'buf' so that only the packet itself
1156          * is visible in 'upcall->packet'.  The ODP flow and (if present)
1157          * userdata become part of the headroom. */
1158         ofpbuf_put_zeros(buf, 2);
1159         buf->data = ofpbuf_put(buf, packet->data, packet->size);
1160         buf->size = packet->size;
1161         upcall->packet = buf;
1162
1163         return 0;
1164     } else {
1165         dp->n_lost++;
1166         return ENOBUFS;
1167     }
1168 }
1169
1170 static void
1171 dp_netdev_action_userspace(void *dp, struct ofpbuf *packet,
1172                            const struct flow *key,
1173                            const struct nlattr *userdata)
1174 {
1175     dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
1176 }
1177
1178 static void
1179 dp_netdev_execute_actions(struct dp_netdev *dp,
1180                           struct ofpbuf *packet, struct flow *key,
1181                           const struct nlattr *actions,
1182                           size_t actions_len)
1183 {
1184     odp_execute_actions(dp, packet, key, actions, actions_len,
1185                         dp_netdev_output_port, dp_netdev_action_userspace);
1186 }
1187
1188 const struct dpif_class dpif_netdev_class = {
1189     "netdev",
1190     dpif_netdev_enumerate,
1191     dpif_netdev_port_open_type,
1192     dpif_netdev_open,
1193     dpif_netdev_close,
1194     dpif_netdev_destroy,
1195     dpif_netdev_run,
1196     dpif_netdev_wait,
1197     dpif_netdev_get_stats,
1198     dpif_netdev_port_add,
1199     dpif_netdev_port_del,
1200     dpif_netdev_port_query_by_number,
1201     dpif_netdev_port_query_by_name,
1202     dpif_netdev_get_max_ports,
1203     NULL,                       /* port_get_pid */
1204     dpif_netdev_port_dump_start,
1205     dpif_netdev_port_dump_next,
1206     dpif_netdev_port_dump_done,
1207     dpif_netdev_port_poll,
1208     dpif_netdev_port_poll_wait,
1209     dpif_netdev_flow_get,
1210     dpif_netdev_flow_put,
1211     dpif_netdev_flow_del,
1212     dpif_netdev_flow_flush,
1213     dpif_netdev_flow_dump_start,
1214     dpif_netdev_flow_dump_next,
1215     dpif_netdev_flow_dump_done,
1216     dpif_netdev_execute,
1217     NULL,                       /* operate */
1218     dpif_netdev_recv_set,
1219     dpif_netdev_queue_to_priority,
1220     dpif_netdev_recv,
1221     dpif_netdev_recv_wait,
1222     dpif_netdev_recv_purge,
1223 };
1224
1225 static void
1226 dpif_dummy_register__(const char *type)
1227 {
1228     struct dpif_class *class;
1229
1230     class = xmalloc(sizeof *class);
1231     *class = dpif_netdev_class;
1232     class->type = xstrdup(type);
1233     dp_register_provider(class);
1234 }
1235
1236 void
1237 dpif_dummy_register(bool override)
1238 {
1239     if (override) {
1240         struct sset types;
1241         const char *type;
1242
1243         sset_init(&types);
1244         dp_enumerate_types(&types);
1245         SSET_FOR_EACH (type, &types) {
1246             if (!dp_unregister_provider(type)) {
1247                 dpif_dummy_register__(type);
1248             }
1249         }
1250         sset_destroy(&types);
1251     }
1252
1253     dpif_dummy_register__("dummy");
1254 }