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