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