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