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