netdev: Change netdev_get_mtu() to return an error code.
[sliver-openvswitch.git] / lib / dpif-netdev.c
1 /*
2  * Copyright (c) 2009 Nicira Networks.
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 <assert.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <net/if.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/ethtool.h>
28 #include <linux/sockios.h>
29 #include <netinet/in.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/ioctl.h>
33 #include <sys/stat.h>
34 #include <sys/sysmacros.h>
35 #include <unistd.h>
36
37 #include "csum.h"
38 #include "dpif-provider.h"
39 #include "flow.h"
40 #include "hmap.h"
41 #include "list.h"
42 #include "netdev.h"
43 #include "odp-util.h"
44 #include "ofp-print.h"
45 #include "ofpbuf.h"
46 #include "packets.h"
47 #include "poll-loop.h"
48 #include "queue.h"
49 #include "timeval.h"
50 #include "util.h"
51
52 #include "vlog.h"
53 #define THIS_MODULE VLM_dpif_netdev
54
55 /* Configuration parameters. */
56 enum { N_QUEUES = 2 };          /* Number of queues for dpif_recv(). */
57 enum { MAX_QUEUE_LEN = 100 };   /* Maximum number of packets per queue. */
58 enum { N_GROUPS = 16 };         /* Number of port groups. */
59 enum { MAX_PORTS = 256 };       /* Maximum number of ports. */
60 enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
61
62 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
63  * headers to be aligned on a 4-byte boundary.  */
64 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
65
66 /* Datapath based on the network device interface from netdev.h. */
67 struct dp_netdev {
68     struct list node;
69     int dp_idx;
70     int open_cnt;
71     bool deleted;
72
73     bool drop_frags;            /* Drop all IP fragments, if true. */
74     struct ovs_queue queues[N_QUEUES]; /* Messages queued for dpif_recv(). */
75     struct hmap flow_table;     /* Flow table. */
76     struct odp_port_group groups[N_GROUPS];
77
78     /* Statistics. */
79     long long int n_frags;      /* Number of dropped IP fragments. */
80     long long int n_hit;        /* Number of flow table matches. */
81     long long int n_missed;     /* Number of flow table misses. */
82     long long int n_lost;       /* Number of misses not passed to client. */
83
84     /* Ports. */
85     int n_ports;
86     struct dp_netdev_port *ports[MAX_PORTS];
87     struct list port_list;
88     unsigned int serial;
89 };
90
91 /* A port in a netdev-based datapath. */
92 struct dp_netdev_port {
93     int port_no;                /* Index into dp_netdev's 'ports'. */
94     struct list node;           /* Element in dp_netdev's 'port_list'. */
95     struct netdev *netdev;
96     bool internal;              /* Internal port (as ODP_PORT_INTERNAL)? */
97 };
98
99 /* A flow in dp_netdev's 'flow_table'. */
100 struct dp_netdev_flow {
101     struct hmap_node node;      /* Element in dp_netdev's 'flow_table'. */
102     flow_t key;
103
104     /* Statistics. */
105         struct timeval used;        /* Last used time, in milliseconds. */
106         long long int packet_count; /* Number of packets matched. */
107         long long int byte_count;   /* Number of bytes matched. */
108         uint8_t ip_tos;             /* IP TOS value. */
109         uint16_t tcp_ctl;           /* Bitwise-OR of seen tcp_ctl values. */
110
111     /* Actions. */
112     union odp_action *actions;
113     unsigned int n_actions;
114 };
115
116 /* Interface to netdev-based datapath. */
117 struct dpif_netdev {
118     struct dpif dpif;
119     struct dp_netdev *dp;
120     int listen_mask;
121     unsigned int dp_serial;
122 };
123
124 /* All netdev-based datapaths. */
125 static struct dp_netdev *dp_netdevs[256];
126 struct list dp_netdev_list = LIST_INITIALIZER(&dp_netdev_list);
127 enum { N_DP_NETDEVS = ARRAY_SIZE(dp_netdevs) };
128
129 /* Maximum port MTU seen so far. */
130 static int max_mtu = ETH_PAYLOAD_MAX;
131
132 static int get_port_by_number(struct dp_netdev *, uint16_t port_no,
133                               struct dp_netdev_port **portp);
134 static int get_port_by_name(struct dp_netdev *, const char *devname,
135                             struct dp_netdev_port **portp);
136 static void dp_netdev_free(struct dp_netdev *);
137 static void dp_netdev_flow_flush(struct dp_netdev *);
138 static int do_add_port(struct dp_netdev *, const char *devname, uint16_t flags,
139                        uint16_t port_no);
140 static int do_del_port(struct dp_netdev *, uint16_t port_no);
141 static int dp_netdev_output_control(struct dp_netdev *, const struct ofpbuf *,
142                                     int queue_no, int port_no, uint32_t arg);
143 static int dp_netdev_execute_actions(struct dp_netdev *,
144                                      struct ofpbuf *, flow_t *,
145                                      const union odp_action *, int n);
146
147 static struct dpif_netdev *
148 dpif_netdev_cast(const struct dpif *dpif)
149 {
150     dpif_assert_class(dpif, &dpif_netdev_class);
151     return CONTAINER_OF(dpif, struct dpif_netdev, dpif);
152 }
153
154 static struct dp_netdev *
155 get_dp_netdev(const struct dpif *dpif)
156 {
157     return dpif_netdev_cast(dpif)->dp;
158 }
159
160 static int
161 name_to_dp_idx(const char *name)
162 {
163     if (!strncmp(name, "dp", 2) && isdigit(name[2])) {
164         int dp_idx = atoi(name + 2);
165         if (dp_idx >= 0 && dp_idx < N_DP_NETDEVS) {
166             return dp_idx;
167         }
168     }
169     return -1;
170 }
171
172 static struct dp_netdev *
173 find_dp_netdev(const char *name)
174 {
175     int dp_idx;
176     size_t i;
177
178     dp_idx = name_to_dp_idx(name);
179     if (dp_idx >= 0) {
180         return dp_netdevs[dp_idx];
181     }
182
183     for (i = 0; i < N_DP_NETDEVS; i++) {
184         struct dp_netdev *dp = dp_netdevs[i];
185         if (dp) {
186             struct dp_netdev_port *port;
187             if (!get_port_by_name(dp, name, &port)) {
188                 return dp;
189             }
190         }
191     }
192     return NULL;
193 }
194
195 static struct dpif *
196 create_dpif_netdev(struct dp_netdev *dp)
197 {
198     struct dpif_netdev *dpif;
199     char *dpname;
200
201     dp->open_cnt++;
202
203     dpname = xasprintf("netdev:dp%d", dp->dp_idx);
204     dpif = xmalloc(sizeof *dpif);
205     dpif_init(&dpif->dpif, &dpif_netdev_class, dpname, dp->dp_idx, dp->dp_idx);
206     dpif->dp = dp;
207     dpif->listen_mask = 0;
208     dpif->dp_serial = dp->serial;
209     free(dpname);
210
211     return &dpif->dpif;
212 }
213
214 static int
215 create_dp_netdev(const char *name, int dp_idx, struct dpif **dpifp)
216 {
217     struct dp_netdev *dp;
218     int error;
219     int i;
220
221     if (dp_netdevs[dp_idx]) {
222         return EBUSY;
223     }
224
225     /* Create datapath. */
226     dp_netdevs[dp_idx] = dp = xcalloc(1, sizeof *dp);
227     list_push_back(&dp_netdev_list, &dp->node);
228     dp->dp_idx = dp_idx;
229     dp->open_cnt = 0;
230     dp->drop_frags = false;
231     for (i = 0; i < N_QUEUES; i++) {
232         queue_init(&dp->queues[i]);
233     }
234     hmap_init(&dp->flow_table);
235     for (i = 0; i < N_GROUPS; i++) {
236         dp->groups[i].ports = NULL;
237         dp->groups[i].n_ports = 0;
238         dp->groups[i].group = i;
239     }
240     list_init(&dp->port_list);
241     error = do_add_port(dp, name, ODP_PORT_INTERNAL, ODPP_LOCAL);
242     if (error) {
243         dp_netdev_free(dp);
244         return error;
245     }
246
247     *dpifp = create_dpif_netdev(dp);
248     return 0;
249 }
250
251 static int
252 dpif_netdev_open(const char *name UNUSED, char *suffix, bool create,
253                  struct dpif **dpifp)
254 {
255     if (create) {
256         if (find_dp_netdev(suffix)) {
257             return EEXIST;
258         } else {
259             int dp_idx = name_to_dp_idx(suffix);
260             if (dp_idx >= 0) {
261                 return create_dp_netdev(suffix, dp_idx, dpifp);
262             } else {
263                 /* Scan for unused dp_idx number. */
264                 for (dp_idx = 0; dp_idx < N_DP_NETDEVS; dp_idx++) {
265                     int error = create_dp_netdev(suffix, dp_idx, dpifp);
266                     if (error != EBUSY) {
267                         return error;
268                     }
269                 }
270
271                 /* All datapath numbers in use. */
272                 return ENOBUFS;
273             }
274         }
275     } else {
276         struct dp_netdev *dp = find_dp_netdev(suffix);
277         if (dp) {
278             *dpifp = create_dpif_netdev(dp);
279             return 0;
280         } else {
281             return ENODEV;
282         }
283     }
284 }
285
286 static void
287 dp_netdev_free(struct dp_netdev *dp)
288 {
289     int i;
290
291     dp_netdev_flow_flush(dp);
292     while (dp->n_ports > 0) {
293         struct dp_netdev_port *port = CONTAINER_OF(
294             dp->port_list.next, struct dp_netdev_port, node);
295         do_del_port(dp, port->port_no);
296     }
297     for (i = 0; i < N_QUEUES; i++) {
298         queue_destroy(&dp->queues[i]);
299     }
300     hmap_destroy(&dp->flow_table);
301     for (i = 0; i < N_GROUPS; i++) {
302         free(dp->groups[i].ports);
303     }
304     dp_netdevs[dp->dp_idx] = NULL;
305     list_remove(&dp->node);
306     free(dp);
307 }
308
309 static void
310 dpif_netdev_close(struct dpif *dpif)
311 {
312     struct dp_netdev *dp = get_dp_netdev(dpif);
313     assert(dp->open_cnt > 0);
314     if (--dp->open_cnt == 0 && dp->deleted) {
315         dp_netdev_free(dp);
316     }
317     free(dpif);
318 }
319
320 static int
321 dpif_netdev_delete(struct dpif *dpif)
322 {
323     struct dp_netdev *dp = get_dp_netdev(dpif);
324     dp->deleted = true;
325     return 0;
326 }
327
328 static int
329 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
330 {
331     struct dp_netdev *dp = get_dp_netdev(dpif);
332     memset(stats, 0, sizeof *stats);
333     stats->n_flows = hmap_count(&dp->flow_table);
334     stats->cur_capacity = hmap_capacity(&dp->flow_table);
335     stats->max_capacity = MAX_FLOWS;
336     stats->n_ports = dp->n_ports;
337     stats->max_ports = MAX_PORTS;
338     stats->max_groups = N_GROUPS;
339     stats->n_frags = dp->n_frags;
340     stats->n_hit = dp->n_hit;
341     stats->n_missed = dp->n_missed;
342     stats->n_lost = dp->n_lost;
343     stats->max_miss_queue = MAX_QUEUE_LEN;
344     stats->max_action_queue = MAX_QUEUE_LEN;
345     return 0;
346 }
347
348 static int
349 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
350 {
351     struct dp_netdev *dp = get_dp_netdev(dpif);
352     *drop_fragsp = dp->drop_frags;
353     return 0;
354 }
355
356 static int
357 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
358 {
359     struct dp_netdev *dp = get_dp_netdev(dpif);
360     dp->drop_frags = drop_frags;
361     return 0;
362 }
363
364 static int
365 do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
366             uint16_t port_no)
367 {
368     bool internal = (flags & ODP_PORT_INTERNAL) != 0;
369     struct dp_netdev_port *port;
370     struct netdev *netdev;
371     int mtu;
372     int error;
373
374     /* XXX reject devices already in some dp_netdev. */
375
376     /* Open and validate network device. */
377     if (!internal) {
378         error = netdev_open(devname, NETDEV_ETH_TYPE_ANY, &netdev);
379     } else {
380         error = netdev_open_tap(devname, &netdev);
381     }
382     if (error) {
383         return error;
384     }
385     /* XXX reject loopback devices */
386     /* XXX reject non-Ethernet devices */
387
388     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
389     if (error) {
390         netdev_close(netdev);
391         return error;
392     }
393
394     port = xmalloc(sizeof *port);
395     port->port_no = port_no;
396     port->netdev = netdev;
397     port->internal = internal;
398
399     netdev_get_mtu(netdev, &mtu);
400     if (mtu > max_mtu) {
401         max_mtu = mtu;
402     }
403
404     list_push_back(&dp->port_list, &port->node);
405     dp->ports[port_no] = port;
406     dp->n_ports++;
407     dp->serial++;
408
409     return 0;
410 }
411
412 static int
413 dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
414                      uint16_t *port_nop)
415 {
416     struct dp_netdev *dp = get_dp_netdev(dpif);
417     int port_no;
418
419     for (port_no = 0; port_no < MAX_PORTS; port_no++) {
420         if (!dp->ports[port_no]) {
421             *port_nop = port_no;
422             return do_add_port(dp, devname, flags, port_no);
423         }
424     }
425     return EXFULL;
426 }
427
428 static int
429 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
430 {
431     struct dp_netdev *dp = get_dp_netdev(dpif);
432     return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
433 }
434
435 static bool
436 is_valid_port_number(uint16_t port_no)
437 {
438     return port_no < MAX_PORTS;
439 }
440
441 static int
442 get_port_by_number(struct dp_netdev *dp,
443                    uint16_t port_no, struct dp_netdev_port **portp)
444 {
445     if (!is_valid_port_number(port_no)) {
446         *portp = NULL;
447         return EINVAL;
448     } else {
449         *portp = dp->ports[port_no];
450         return *portp ? 0 : ENOENT;
451     }
452 }
453
454 static int
455 get_port_by_name(struct dp_netdev *dp,
456                  const char *devname, struct dp_netdev_port **portp)
457 {
458     struct dp_netdev_port *port;
459
460     LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
461         if (!strcmp(netdev_get_name(port->netdev), devname)) {
462             *portp = port;
463             return 0;
464         }
465     }
466     return ENOENT;
467 }
468
469 static int
470 do_del_port(struct dp_netdev *dp, uint16_t port_no)
471 {
472     struct dp_netdev_port *port;
473     int error;
474
475     error = get_port_by_number(dp, port_no, &port);
476     if (error) {
477         return error;
478     }
479
480     list_remove(&port->node);
481     dp->ports[port->port_no] = NULL;
482     dp->n_ports--;
483     dp->serial++;
484
485     netdev_close(port->netdev);
486     free(port);
487
488     return 0;
489 }
490
491 static void
492 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
493 {
494     memset(odp_port, 0, sizeof *odp_port);
495     ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
496                 sizeof odp_port->devname);
497     odp_port->port = port->port_no;
498     odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0;
499 }
500
501 static int
502 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
503                                  struct odp_port *odp_port)
504 {
505     struct dp_netdev *dp = get_dp_netdev(dpif);
506     struct dp_netdev_port *port;
507     int error;
508
509     error = get_port_by_number(dp, port_no, &port);
510     if (!error) {
511         answer_port_query(port, odp_port);
512     }
513     return error;
514 }
515
516 static int
517 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
518                                struct odp_port *odp_port)
519 {
520     struct dp_netdev *dp = get_dp_netdev(dpif);
521     struct dp_netdev_port *port;
522     int error;
523
524     error = get_port_by_name(dp, devname, &port);
525     if (!error) {
526         answer_port_query(port, odp_port);
527     }
528     return error;
529 }
530
531 static void
532 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
533 {
534     hmap_remove(&dp->flow_table, &flow->node);
535     free(flow->actions);
536     free(flow);
537 }
538
539 static void
540 dp_netdev_flow_flush(struct dp_netdev *dp)
541 {
542     struct dp_netdev_flow *flow, *next;
543
544     HMAP_FOR_EACH_SAFE (flow, next, struct dp_netdev_flow, node,
545                         &dp->flow_table) {
546         dp_netdev_free_flow(dp, flow);
547     }
548 }
549
550 static int
551 dpif_netdev_flow_flush(struct dpif *dpif)
552 {
553     struct dp_netdev *dp = get_dp_netdev(dpif);
554     dp_netdev_flow_flush(dp);
555     return 0;
556 }
557
558 static int
559 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
560 {
561     struct dp_netdev *dp = get_dp_netdev(dpif);
562     struct dp_netdev_port *port;
563     int i;
564
565     i = 0;
566     LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
567         struct odp_port *odp_port = &ports[i];
568         if (i >= n) {
569             break;
570         }
571         answer_port_query(port, odp_port);
572         i++;
573     }
574     return dp->n_ports;
575 }
576
577 static int
578 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep UNUSED)
579 {
580     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
581     if (dpif->dp_serial != dpif->dp->serial) {
582         dpif->dp_serial = dpif->dp->serial;
583         return ENOBUFS;
584     } else {
585         return EAGAIN;
586     }
587 }
588
589 static void
590 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
591 {
592     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
593     if (dpif->dp_serial != dpif->dp->serial) {
594         poll_immediate_wake();
595     }
596 }
597
598 static int
599 get_port_group(const struct dpif *dpif, int group_no,
600                struct odp_port_group **groupp)
601 {
602     struct dp_netdev *dp = get_dp_netdev(dpif);
603
604     if (group_no >= 0 && group_no < N_GROUPS) {
605         *groupp = &dp->groups[group_no];
606         return 0;
607     } else {
608         *groupp = NULL;
609         return EINVAL;
610     }
611 }
612
613 static int
614 dpif_netdev_port_group_get(const struct dpif *dpif, int group_no,
615                            uint16_t ports[], int n)
616 {
617     struct odp_port_group *group;
618     int error;
619
620     if (n < 0) {
621         return -EINVAL;
622     }
623
624     error = get_port_group(dpif, group_no, &group);
625     if (!error) {
626         memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
627         return group->n_ports;
628     } else {
629         return -error;
630     }
631 }
632
633 static int
634 dpif_netdev_port_group_set(struct dpif *dpif, int group_no,
635                            const uint16_t ports[], int n)
636 {
637     struct odp_port_group *group;
638     int error;
639
640     if (n < 0 || n > MAX_PORTS) {
641         return EINVAL;
642     }
643
644     error = get_port_group(dpif, group_no, &group);
645     if (!error) {
646         free(group->ports);
647         group->ports = xmemdup(ports, n * sizeof *group->ports);
648         group->n_ports = n;
649         group->group = group_no;
650     }
651     return error;
652 }
653
654 static struct dp_netdev_flow *
655 dp_netdev_lookup_flow(const struct dp_netdev *dp, const flow_t *key)
656 {
657     struct dp_netdev_flow *flow;
658
659     assert(key->reserved == 0);
660     HMAP_FOR_EACH_WITH_HASH (flow, struct dp_netdev_flow, node,
661                              flow_hash(key, 0), &dp->flow_table) {
662         if (flow_equal(&flow->key, key)) {
663             return flow;
664         }
665     }
666     return NULL;
667 }
668
669 static void
670 answer_flow_query(const struct dp_netdev_flow *flow,
671                   struct odp_flow *odp_flow)
672 {
673     if (flow) {
674         odp_flow->key = flow->key;
675         odp_flow->stats.n_packets = flow->packet_count;
676         odp_flow->stats.n_bytes = flow->byte_count;
677         odp_flow->stats.used_sec = flow->used.tv_sec;
678         odp_flow->stats.used_nsec = flow->used.tv_usec * 1000;
679         odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
680         odp_flow->stats.ip_tos = flow->ip_tos;
681         odp_flow->stats.error = 0;
682         if (odp_flow->n_actions > 0) {
683             unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
684             memcpy(odp_flow->actions, flow->actions,
685                    n * sizeof *odp_flow->actions);
686             odp_flow->n_actions = flow->n_actions;
687         }
688     } else {
689         odp_flow->stats.error = ENOENT;
690     }
691 }
692
693 static int
694 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
695 {
696     struct dp_netdev *dp = get_dp_netdev(dpif);
697     int i;
698
699     for (i = 0; i < n; i++) {
700         struct odp_flow *odp_flow = &flows[i];
701         answer_flow_query(dp_netdev_lookup_flow(dp, &odp_flow->key), odp_flow);
702     }
703     return 0;
704 }
705
706 static int
707 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
708                              bool *mutates)
709 {
710         unsigned int i;
711
712     *mutates = false;
713         for (i = 0; i < n_actions; i++) {
714                 const union odp_action *a = &actions[i];
715                 switch (a->type) {
716                 case ODPAT_OUTPUT:
717                         if (a->output.port >= MAX_PORTS) {
718                                 return EINVAL;
719             }
720                         break;
721
722                 case ODPAT_OUTPUT_GROUP:
723             *mutates = true;
724                         if (a->output_group.group >= N_GROUPS) {
725                                 return EINVAL;
726             }
727                         break;
728
729         case ODPAT_CONTROLLER:
730             break;
731
732                 case ODPAT_SET_VLAN_VID:
733             *mutates = true;
734                         if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK)) {
735                                 return EINVAL;
736             }
737                         break;
738
739                 case ODPAT_SET_VLAN_PCP:
740             *mutates = true;
741                         if (a->vlan_pcp.vlan_pcp & ~VLAN_PCP_MASK) {
742                                 return EINVAL;
743             }
744                         break;
745
746         case ODPAT_STRIP_VLAN:
747         case ODPAT_SET_DL_SRC:
748         case ODPAT_SET_DL_DST:
749         case ODPAT_SET_NW_SRC:
750         case ODPAT_SET_NW_DST:
751         case ODPAT_SET_TP_SRC:
752         case ODPAT_SET_TP_DST:
753             *mutates = true;
754             break;
755
756                 default:
757             return EOPNOTSUPP;
758                 }
759         }
760         return 0;
761 }
762
763 static int
764 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
765 {
766     size_t n_bytes;
767     bool mutates;
768     int error;
769
770     if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
771         return EINVAL;
772     }
773     error = dpif_netdev_validate_actions(odp_flow->actions,
774                                          odp_flow->n_actions, &mutates);
775     if (error) {
776         return error;
777     }
778
779     n_bytes = odp_flow->n_actions * sizeof *flow->actions;
780     flow->actions = xrealloc(flow->actions, n_bytes);
781     flow->n_actions = odp_flow->n_actions;
782     memcpy(flow->actions, odp_flow->actions, n_bytes);
783     return 0;
784 }
785
786 static int
787 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
788 {
789     struct dp_netdev *dp = get_dp_netdev(dpif);
790     struct dp_netdev_flow *flow;
791     int error;
792
793     flow = xcalloc(1, sizeof *flow);
794     flow->key = odp_flow->key;
795     flow->key.reserved = 0;
796
797     error = set_flow_actions(flow, odp_flow);
798     if (error) {
799         free(flow);
800         return error;
801     }
802
803     hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
804     return 0;
805 }
806
807 static void
808 clear_stats(struct dp_netdev_flow *flow)
809 {
810     flow->used.tv_sec = 0;
811     flow->used.tv_usec = 0;
812     flow->packet_count = 0;
813     flow->byte_count = 0;
814     flow->ip_tos = 0;
815     flow->tcp_ctl = 0;
816 }
817
818 static int
819 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
820 {
821     struct dp_netdev *dp = get_dp_netdev(dpif);
822     struct dp_netdev_flow *flow;
823
824     flow = dp_netdev_lookup_flow(dp, &put->flow.key);
825     if (!flow) {
826         if (put->flags & ODPPF_CREATE) {
827             if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
828                 return add_flow(dpif, &put->flow);
829             } else {
830                 return EXFULL;
831             }
832         } else {
833             return ENOENT;
834         }
835     } else {
836         if (put->flags & ODPPF_MODIFY) {
837             int error = set_flow_actions(flow, &put->flow);
838             if (!error && put->flags & ODPPF_ZERO_STATS) {
839                 clear_stats(flow);
840             }
841             return error;
842         } else {
843             return EEXIST;
844         }
845     }
846 }
847
848
849 static int
850 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
851 {
852     struct dp_netdev *dp = get_dp_netdev(dpif);
853     struct dp_netdev_flow *flow;
854
855     flow = dp_netdev_lookup_flow(dp, &odp_flow->key);
856     if (flow) {
857         answer_flow_query(flow, odp_flow);
858         dp_netdev_free_flow(dp, flow);
859         return 0;
860     } else {
861         return ENOENT;
862     }
863 }
864
865 static int
866 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
867 {
868     struct dp_netdev *dp = get_dp_netdev(dpif);
869     struct dp_netdev_flow *flow;
870     int i;
871
872     i = 0;
873     HMAP_FOR_EACH (flow, struct dp_netdev_flow, node, &dp->flow_table) {
874         if (i >= n) {
875             break;
876         }
877         answer_flow_query(flow, &flows[i++]);
878     }
879     return hmap_count(&dp->flow_table);
880 }
881
882 static int
883 dpif_netdev_execute(struct dpif *dpif, uint16_t in_port,
884                     const union odp_action actions[], int n_actions,
885                     const struct ofpbuf *packet)
886 {
887     struct dp_netdev *dp = get_dp_netdev(dpif);
888     struct ofpbuf copy;
889     bool mutates;
890     flow_t flow;
891     int error;
892
893     if (packet->size < ETH_HLEN || packet->size > UINT16_MAX) {
894         return EINVAL;
895     }
896
897     error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
898     if (error) {
899         return error;
900     }
901
902     if (mutates) {
903         /* We need a deep copy of 'packet' since we're going to modify its
904          * data. */
905         ofpbuf_init(&copy, DP_NETDEV_HEADROOM + packet->size);
906         copy.data = (char*)copy.base + DP_NETDEV_HEADROOM;
907         ofpbuf_put(&copy, packet->data, packet->size);
908     } else {
909         /* We still need a shallow copy of 'packet', even though we won't
910          * modify its data, because flow_extract() modifies packet->l2, etc.
911          * We could probably get away with modifying those but it's more polite
912          * if we don't. */
913         copy = *packet;
914     }
915     flow_extract(&copy, in_port, &flow);
916     error = dp_netdev_execute_actions(dp, &copy, &flow, actions, n_actions);
917     if (mutates) {
918         ofpbuf_uninit(&copy);
919     }
920     return error;
921 }
922
923 static int
924 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
925 {
926     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
927     *listen_mask = dpif_netdev->listen_mask;
928     return 0;
929 }
930
931 static int
932 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
933 {
934     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
935     if (!(listen_mask & ~ODPL_ALL)) {
936         dpif_netdev->listen_mask = listen_mask;
937         return 0;
938     } else {
939         return EINVAL;
940     }
941 }
942
943 static struct ovs_queue *
944 find_nonempty_queue(struct dpif *dpif)
945 {
946     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
947     struct dp_netdev *dp = get_dp_netdev(dpif);
948     int mask = dpif_netdev->listen_mask;
949     int i;
950
951     for (i = 0; i < N_QUEUES; i++) {
952         struct ovs_queue *q = &dp->queues[i];
953         if (q->n && mask & (1u << i)) {
954             return q;
955         }
956     }
957     return NULL;
958 }
959
960 static int
961 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
962 {
963     struct ovs_queue *q = find_nonempty_queue(dpif);
964     if (q) {
965         *bufp = queue_pop_head(q);
966         return 0;
967     } else {
968         return EAGAIN;
969     }
970 }
971
972 static void
973 dpif_netdev_recv_wait(struct dpif *dpif)
974 {
975     struct ovs_queue *q = find_nonempty_queue(dpif);
976     if (q) {
977         poll_immediate_wake();
978     } else {
979         /* No messages ready to be received, and dp_wait() will ensure that we
980          * wake up to queue new messages, so there is nothing to do. */
981     }
982 }
983 \f
984 static void
985 dp_netdev_flow_used(struct dp_netdev_flow *flow, const flow_t *key,
986                     const struct ofpbuf *packet)
987 {
988     time_timeval(&flow->used);
989     flow->packet_count++;
990     flow->byte_count += packet->size;
991     if (key->dl_type == htons(ETH_P_IP)) {
992         struct ip_header *nh = packet->l3;
993         flow->ip_tos = nh->ip_tos;
994
995         if (key->nw_proto == IPPROTO_TCP) {
996             struct tcp_header *th = packet->l4;
997             flow->tcp_ctl |= th->tcp_ctl;
998         }
999     }
1000 }
1001
1002 static void
1003 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1004                      struct ofpbuf *packet)
1005 {
1006     struct dp_netdev_flow *flow;
1007     flow_t key;
1008
1009     if (flow_extract(packet, port->port_no, &key) && dp->drop_frags) {
1010         dp->n_frags++;
1011         return;
1012     }
1013
1014     flow = dp_netdev_lookup_flow(dp, &key);
1015     if (flow) {
1016         dp_netdev_flow_used(flow, &key, packet);
1017         dp_netdev_execute_actions(dp, packet, &key,
1018                                   flow->actions, flow->n_actions);
1019         dp->n_hit++;
1020     } else {
1021         dp->n_missed++;
1022         dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
1023     }
1024 }
1025
1026 static void
1027 dp_netdev_run(void)
1028 {
1029     struct ofpbuf packet;
1030     struct dp_netdev *dp;
1031
1032     ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu);
1033     LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1034         struct dp_netdev_port *port;
1035
1036         LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1037             int error;
1038
1039             /* Reset packet contents. */
1040             packet.data = (char*)packet.base + DP_NETDEV_HEADROOM;
1041             packet.size = 0;
1042
1043             error = netdev_recv(port->netdev, &packet);
1044             if (!error) {
1045                 dp_netdev_port_input(dp, port, &packet);
1046             } else if (error != EAGAIN) {
1047                 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1048                 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1049                             netdev_get_name(port->netdev), strerror(error));
1050             }
1051         }
1052     }
1053     ofpbuf_uninit(&packet);
1054 }
1055
1056 static void
1057 dp_netdev_wait(void)
1058 {
1059     struct dp_netdev *dp;
1060
1061     LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1062         struct dp_netdev_port *port;
1063         LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1064             netdev_recv_wait(port->netdev);
1065         }
1066     }
1067 }
1068
1069 static void
1070 dp_netdev_modify_vlan_tci(struct ofpbuf *packet, flow_t *key,
1071                           uint16_t tci, uint16_t mask)
1072 {
1073     struct vlan_eth_header *veh;
1074
1075     if (key->dl_vlan != htons(ODP_VLAN_NONE)) {
1076         /* Modify 'mask' bits, but maintain other TCI bits. */
1077         veh = packet->l2;
1078         veh->veth_tci &= ~htons(mask);
1079         veh->veth_tci |= htons(tci);
1080     } else {
1081         /* Insert new 802.1Q header. */
1082         struct eth_header *eh = packet->l2;
1083         struct vlan_eth_header tmp;
1084         memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1085         memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1086         tmp.veth_type = htons(ETH_TYPE_VLAN);
1087         tmp.veth_tci = htons(tci);
1088         tmp.veth_next_type = eh->eth_type;
1089
1090         veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1091         memcpy(veh, &tmp, sizeof tmp);
1092         packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1093     }
1094
1095     key->dl_vlan = veh->veth_tci & htons(VLAN_VID_MASK);
1096 }
1097
1098 static void
1099 dp_netdev_strip_vlan(struct ofpbuf *packet, flow_t *key)
1100 {
1101     struct vlan_eth_header *veh = packet->l2;
1102     if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1103         struct eth_header tmp;
1104
1105         memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1106         memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1107         tmp.eth_type = veh->veth_next_type;
1108
1109         packet->size -= VLAN_HEADER_LEN;
1110         packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1111         packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1112         memcpy(packet->data, &tmp, sizeof tmp);
1113
1114         key->dl_vlan = htons(ODP_VLAN_NONE);
1115     }
1116 }
1117
1118 static void
1119 dp_netdev_set_dl_src(struct ofpbuf *packet,
1120                      const uint8_t dl_addr[ETH_ADDR_LEN])
1121 {
1122     struct eth_header *eh = packet->l2;
1123     memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1124 }
1125
1126 static void
1127 dp_netdev_set_dl_dst(struct ofpbuf *packet,
1128                      const uint8_t dl_addr[ETH_ADDR_LEN])
1129 {
1130     struct eth_header *eh = packet->l2;
1131     memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1132 }
1133
1134 static void
1135 dp_netdev_set_nw_addr(struct ofpbuf *packet, flow_t *key,
1136                       const struct odp_action_nw_addr *a)
1137 {
1138     if (key->dl_type == htons(ETH_TYPE_IP)) {
1139         struct ip_header *nh = packet->l3;
1140         uint32_t *field;
1141
1142         field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1143         if (key->nw_proto == IP_TYPE_TCP) {
1144             struct tcp_header *th = packet->l4;
1145             th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1146         } else if (key->nw_proto == IP_TYPE_UDP) {
1147             struct udp_header *uh = packet->l4;
1148             if (uh->udp_csum) {
1149                 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1150                 if (!uh->udp_csum) {
1151                     uh->udp_csum = 0xffff;
1152                 }
1153             }
1154         }
1155         nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1156         *field = a->nw_addr;
1157     }
1158 }
1159
1160 static void
1161 dp_netdev_set_tp_port(struct ofpbuf *packet, flow_t *key,
1162                       const struct odp_action_tp_port *a)
1163 {
1164         if (key->dl_type == htons(ETH_P_IP)) {
1165         uint16_t *field;
1166         if (key->nw_proto == IPPROTO_TCP) {
1167             struct tcp_header *th = packet->l4;
1168             field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1169             th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1170             *field = a->tp_port;
1171         } else if (key->nw_proto == IPPROTO_UDP) {
1172             struct udp_header *uh = packet->l4;
1173             field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1174             uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1175             *field = a->tp_port;
1176         }
1177     }
1178 }
1179
1180 static void
1181 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1182                       uint16_t out_port)
1183 {
1184         struct dp_netdev_port *p = dp->ports[out_port];
1185     if (p) {
1186         netdev_send(p->netdev, packet);
1187     }
1188 }
1189
1190 static void
1191 dp_netdev_output_group(struct dp_netdev *dp, uint16_t group, uint16_t in_port,
1192                        struct ofpbuf *packet)
1193 {
1194         struct odp_port_group *g = &dp->groups[group];
1195         int i;
1196
1197         for (i = 0; i < g->n_ports; i++) {
1198         uint16_t out_port = g->ports[i];
1199         if (out_port != in_port) {
1200             dp_netdev_output_port(dp, packet, out_port);
1201         }
1202         }
1203 }
1204
1205 static int
1206 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1207                          int queue_no, int port_no, uint32_t arg)
1208 {
1209     struct ovs_queue *q = &dp->queues[queue_no];
1210     struct odp_msg *header;
1211     struct ofpbuf *msg;
1212     size_t msg_size;
1213
1214     if (q->n >= MAX_QUEUE_LEN) {
1215         dp->n_lost++;
1216         return ENOBUFS;
1217     }
1218
1219     msg_size = sizeof *header + packet->size;
1220     msg = ofpbuf_new(msg_size);
1221     header = ofpbuf_put_uninit(msg, sizeof *header);
1222     header->type = queue_no;
1223     header->length = msg_size;
1224     header->port = port_no;
1225     header->arg = arg;
1226     ofpbuf_put(msg, packet->data, packet->size);
1227     queue_push_tail(q, msg);
1228
1229     return 0;
1230 }
1231
1232 static int
1233 dp_netdev_execute_actions(struct dp_netdev *dp,
1234                           struct ofpbuf *packet, flow_t *key,
1235                           const union odp_action *actions, int n_actions)
1236 {
1237     int i;
1238     for (i = 0; i < n_actions; i++) {
1239         const union odp_action *a = &actions[i];
1240
1241                 switch (a->type) {
1242                 case ODPAT_OUTPUT:
1243             dp_netdev_output_port(dp, packet, a->output.port);
1244                         break;
1245
1246                 case ODPAT_OUTPUT_GROUP:
1247                         dp_netdev_output_group(dp, a->output_group.group, key->in_port,
1248                                    packet);
1249                         break;
1250
1251                 case ODPAT_CONTROLLER:
1252             dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1253                                      key->in_port, a->controller.arg);
1254                         break;
1255
1256                 case ODPAT_SET_VLAN_VID:
1257                         dp_netdev_modify_vlan_tci(packet, key, ntohs(a->vlan_vid.vlan_vid),
1258                                       VLAN_VID_MASK);
1259             break;
1260
1261                 case ODPAT_SET_VLAN_PCP:
1262                         dp_netdev_modify_vlan_tci(packet, key, a->vlan_pcp.vlan_pcp << 13,
1263                                       VLAN_PCP_MASK);
1264             break;
1265
1266                 case ODPAT_STRIP_VLAN:
1267                         dp_netdev_strip_vlan(packet, key);
1268                         break;
1269
1270                 case ODPAT_SET_DL_SRC:
1271             dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1272                         break;
1273
1274                 case ODPAT_SET_DL_DST:
1275             dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1276                         break;
1277
1278                 case ODPAT_SET_NW_SRC:
1279                 case ODPAT_SET_NW_DST:
1280                         dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1281                         break;
1282
1283                 case ODPAT_SET_TP_SRC:
1284                 case ODPAT_SET_TP_DST:
1285                         dp_netdev_set_tp_port(packet, key, &a->tp_port);
1286                         break;
1287                 }
1288         }
1289     return 0;
1290 }
1291
1292 const struct dpif_class dpif_netdev_class = {
1293     "netdev",
1294     "netdev",
1295     dp_netdev_run,
1296     dp_netdev_wait,
1297     NULL,                       /* enumerate */
1298     dpif_netdev_open,
1299     dpif_netdev_close,
1300     NULL,                       /* get_all_names */
1301     dpif_netdev_delete,
1302     dpif_netdev_get_stats,
1303     dpif_netdev_get_drop_frags,
1304     dpif_netdev_set_drop_frags,
1305     dpif_netdev_port_add,
1306     dpif_netdev_port_del,
1307     dpif_netdev_port_query_by_number,
1308     dpif_netdev_port_query_by_name,
1309     dpif_netdev_port_list,
1310     dpif_netdev_port_poll,
1311     dpif_netdev_port_poll_wait,
1312     dpif_netdev_port_group_get,
1313     dpif_netdev_port_group_set,
1314     dpif_netdev_flow_get,
1315     dpif_netdev_flow_put,
1316     dpif_netdev_flow_del,
1317     dpif_netdev_flow_flush,
1318     dpif_netdev_flow_list,
1319     dpif_netdev_execute,
1320     dpif_netdev_recv_get_mask,
1321     dpif_netdev_recv_set_mask,
1322     dpif_netdev_recv,
1323     dpif_netdev_recv_wait,
1324 };