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