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