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