Cleanup isdigit() warnings.
[openvswitch] / 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((unsigned char)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         char *tapname = xasprintf("tap:%s", devname);
381         error = netdev_open(tapname, NETDEV_ETH_TYPE_ANY, &netdev);
382         free(tapname);
383     }
384     if (error) {
385         return error;
386     }
387     /* XXX reject loopback devices */
388     /* XXX reject non-Ethernet devices */
389
390     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
391     if (error) {
392         netdev_close(netdev);
393         return error;
394     }
395
396     port = xmalloc(sizeof *port);
397     port->port_no = port_no;
398     port->netdev = netdev;
399     port->internal = internal;
400
401     netdev_get_mtu(netdev, &mtu);
402     if (mtu > max_mtu) {
403         max_mtu = mtu;
404     }
405
406     list_push_back(&dp->port_list, &port->node);
407     dp->ports[port_no] = port;
408     dp->n_ports++;
409     dp->serial++;
410
411     return 0;
412 }
413
414 static int
415 dpif_netdev_port_add(struct dpif *dpif, const char *devname, uint16_t flags,
416                      uint16_t *port_nop)
417 {
418     struct dp_netdev *dp = get_dp_netdev(dpif);
419     int port_no;
420
421     for (port_no = 0; port_no < MAX_PORTS; port_no++) {
422         if (!dp->ports[port_no]) {
423             *port_nop = port_no;
424             return do_add_port(dp, devname, flags, port_no);
425         }
426     }
427     return EXFULL;
428 }
429
430 static int
431 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
432 {
433     struct dp_netdev *dp = get_dp_netdev(dpif);
434     return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
435 }
436
437 static bool
438 is_valid_port_number(uint16_t port_no)
439 {
440     return port_no < MAX_PORTS;
441 }
442
443 static int
444 get_port_by_number(struct dp_netdev *dp,
445                    uint16_t port_no, struct dp_netdev_port **portp)
446 {
447     if (!is_valid_port_number(port_no)) {
448         *portp = NULL;
449         return EINVAL;
450     } else {
451         *portp = dp->ports[port_no];
452         return *portp ? 0 : ENOENT;
453     }
454 }
455
456 static int
457 get_port_by_name(struct dp_netdev *dp,
458                  const char *devname, struct dp_netdev_port **portp)
459 {
460     struct dp_netdev_port *port;
461
462     LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
463         if (!strcmp(netdev_get_name(port->netdev), devname)) {
464             *portp = port;
465             return 0;
466         }
467     }
468     return ENOENT;
469 }
470
471 static int
472 do_del_port(struct dp_netdev *dp, uint16_t port_no)
473 {
474     struct dp_netdev_port *port;
475     int error;
476
477     error = get_port_by_number(dp, port_no, &port);
478     if (error) {
479         return error;
480     }
481
482     list_remove(&port->node);
483     dp->ports[port->port_no] = NULL;
484     dp->n_ports--;
485     dp->serial++;
486
487     netdev_close(port->netdev);
488     free(port);
489
490     return 0;
491 }
492
493 static void
494 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
495 {
496     memset(odp_port, 0, sizeof *odp_port);
497     ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
498                 sizeof odp_port->devname);
499     odp_port->port = port->port_no;
500     odp_port->flags = port->internal ? ODP_PORT_INTERNAL : 0;
501 }
502
503 static int
504 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
505                                  struct odp_port *odp_port)
506 {
507     struct dp_netdev *dp = get_dp_netdev(dpif);
508     struct dp_netdev_port *port;
509     int error;
510
511     error = get_port_by_number(dp, port_no, &port);
512     if (!error) {
513         answer_port_query(port, odp_port);
514     }
515     return error;
516 }
517
518 static int
519 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
520                                struct odp_port *odp_port)
521 {
522     struct dp_netdev *dp = get_dp_netdev(dpif);
523     struct dp_netdev_port *port;
524     int error;
525
526     error = get_port_by_name(dp, devname, &port);
527     if (!error) {
528         answer_port_query(port, odp_port);
529     }
530     return error;
531 }
532
533 static void
534 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
535 {
536     hmap_remove(&dp->flow_table, &flow->node);
537     free(flow->actions);
538     free(flow);
539 }
540
541 static void
542 dp_netdev_flow_flush(struct dp_netdev *dp)
543 {
544     struct dp_netdev_flow *flow, *next;
545
546     HMAP_FOR_EACH_SAFE (flow, next, struct dp_netdev_flow, node,
547                         &dp->flow_table) {
548         dp_netdev_free_flow(dp, flow);
549     }
550 }
551
552 static int
553 dpif_netdev_flow_flush(struct dpif *dpif)
554 {
555     struct dp_netdev *dp = get_dp_netdev(dpif);
556     dp_netdev_flow_flush(dp);
557     return 0;
558 }
559
560 static int
561 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
562 {
563     struct dp_netdev *dp = get_dp_netdev(dpif);
564     struct dp_netdev_port *port;
565     int i;
566
567     i = 0;
568     LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
569         struct odp_port *odp_port = &ports[i];
570         if (i >= n) {
571             break;
572         }
573         answer_port_query(port, odp_port);
574         i++;
575     }
576     return dp->n_ports;
577 }
578
579 static int
580 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep UNUSED)
581 {
582     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
583     if (dpif->dp_serial != dpif->dp->serial) {
584         dpif->dp_serial = dpif->dp->serial;
585         return ENOBUFS;
586     } else {
587         return EAGAIN;
588     }
589 }
590
591 static void
592 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
593 {
594     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
595     if (dpif->dp_serial != dpif->dp->serial) {
596         poll_immediate_wake();
597     }
598 }
599
600 static int
601 get_port_group(const struct dpif *dpif, int group_no,
602                struct odp_port_group **groupp)
603 {
604     struct dp_netdev *dp = get_dp_netdev(dpif);
605
606     if (group_no >= 0 && group_no < N_GROUPS) {
607         *groupp = &dp->groups[group_no];
608         return 0;
609     } else {
610         *groupp = NULL;
611         return EINVAL;
612     }
613 }
614
615 static int
616 dpif_netdev_port_group_get(const struct dpif *dpif, int group_no,
617                            uint16_t ports[], int n)
618 {
619     struct odp_port_group *group;
620     int error;
621
622     if (n < 0) {
623         return -EINVAL;
624     }
625
626     error = get_port_group(dpif, group_no, &group);
627     if (!error) {
628         memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
629         return group->n_ports;
630     } else {
631         return -error;
632     }
633 }
634
635 static int
636 dpif_netdev_port_group_set(struct dpif *dpif, int group_no,
637                            const uint16_t ports[], int n)
638 {
639     struct odp_port_group *group;
640     int error;
641
642     if (n < 0 || n > MAX_PORTS) {
643         return EINVAL;
644     }
645
646     error = get_port_group(dpif, group_no, &group);
647     if (!error) {
648         free(group->ports);
649         group->ports = xmemdup(ports, n * sizeof *group->ports);
650         group->n_ports = n;
651         group->group = group_no;
652     }
653     return error;
654 }
655
656 static struct dp_netdev_flow *
657 dp_netdev_lookup_flow(const struct dp_netdev *dp, const flow_t *key)
658 {
659     struct dp_netdev_flow *flow;
660
661     assert(key->reserved == 0);
662     HMAP_FOR_EACH_WITH_HASH (flow, struct dp_netdev_flow, node,
663                              flow_hash(key, 0), &dp->flow_table) {
664         if (flow_equal(&flow->key, key)) {
665             return flow;
666         }
667     }
668     return NULL;
669 }
670
671 static void
672 answer_flow_query(const struct dp_netdev_flow *flow,
673                   struct odp_flow *odp_flow)
674 {
675     if (flow) {
676         odp_flow->key = flow->key;
677         odp_flow->stats.n_packets = flow->packet_count;
678         odp_flow->stats.n_bytes = flow->byte_count;
679         odp_flow->stats.used_sec = flow->used.tv_sec;
680         odp_flow->stats.used_nsec = flow->used.tv_usec * 1000;
681         odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
682         odp_flow->stats.ip_tos = flow->ip_tos;
683         odp_flow->stats.error = 0;
684         if (odp_flow->n_actions > 0) {
685             unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
686             memcpy(odp_flow->actions, flow->actions,
687                    n * sizeof *odp_flow->actions);
688             odp_flow->n_actions = flow->n_actions;
689         }
690     } else {
691         odp_flow->stats.error = ENOENT;
692     }
693 }
694
695 static int
696 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
697 {
698     struct dp_netdev *dp = get_dp_netdev(dpif);
699     int i;
700
701     for (i = 0; i < n; i++) {
702         struct odp_flow *odp_flow = &flows[i];
703         answer_flow_query(dp_netdev_lookup_flow(dp, &odp_flow->key), odp_flow);
704     }
705     return 0;
706 }
707
708 static int
709 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
710                              bool *mutates)
711 {
712         unsigned int i;
713
714     *mutates = false;
715         for (i = 0; i < n_actions; i++) {
716                 const union odp_action *a = &actions[i];
717                 switch (a->type) {
718                 case ODPAT_OUTPUT:
719                         if (a->output.port >= MAX_PORTS) {
720                                 return EINVAL;
721             }
722                         break;
723
724                 case ODPAT_OUTPUT_GROUP:
725             *mutates = true;
726                         if (a->output_group.group >= N_GROUPS) {
727                                 return EINVAL;
728             }
729                         break;
730
731         case ODPAT_CONTROLLER:
732             break;
733
734                 case ODPAT_SET_VLAN_VID:
735             *mutates = true;
736                         if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK)) {
737                                 return EINVAL;
738             }
739                         break;
740
741                 case ODPAT_SET_VLAN_PCP:
742             *mutates = true;
743                         if (a->vlan_pcp.vlan_pcp & ~VLAN_PCP_MASK) {
744                                 return EINVAL;
745             }
746                         break;
747
748         case ODPAT_STRIP_VLAN:
749         case ODPAT_SET_DL_SRC:
750         case ODPAT_SET_DL_DST:
751         case ODPAT_SET_NW_SRC:
752         case ODPAT_SET_NW_DST:
753         case ODPAT_SET_TP_SRC:
754         case ODPAT_SET_TP_DST:
755             *mutates = true;
756             break;
757
758                 default:
759             return EOPNOTSUPP;
760                 }
761         }
762         return 0;
763 }
764
765 static int
766 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
767 {
768     size_t n_bytes;
769     bool mutates;
770     int error;
771
772     if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
773         return EINVAL;
774     }
775     error = dpif_netdev_validate_actions(odp_flow->actions,
776                                          odp_flow->n_actions, &mutates);
777     if (error) {
778         return error;
779     }
780
781     n_bytes = odp_flow->n_actions * sizeof *flow->actions;
782     flow->actions = xrealloc(flow->actions, n_bytes);
783     flow->n_actions = odp_flow->n_actions;
784     memcpy(flow->actions, odp_flow->actions, n_bytes);
785     return 0;
786 }
787
788 static int
789 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
790 {
791     struct dp_netdev *dp = get_dp_netdev(dpif);
792     struct dp_netdev_flow *flow;
793     int error;
794
795     flow = xcalloc(1, sizeof *flow);
796     flow->key = odp_flow->key;
797     flow->key.reserved = 0;
798
799     error = set_flow_actions(flow, odp_flow);
800     if (error) {
801         free(flow);
802         return error;
803     }
804
805     hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
806     return 0;
807 }
808
809 static void
810 clear_stats(struct dp_netdev_flow *flow)
811 {
812     flow->used.tv_sec = 0;
813     flow->used.tv_usec = 0;
814     flow->packet_count = 0;
815     flow->byte_count = 0;
816     flow->ip_tos = 0;
817     flow->tcp_ctl = 0;
818 }
819
820 static int
821 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
822 {
823     struct dp_netdev *dp = get_dp_netdev(dpif);
824     struct dp_netdev_flow *flow;
825
826     flow = dp_netdev_lookup_flow(dp, &put->flow.key);
827     if (!flow) {
828         if (put->flags & ODPPF_CREATE) {
829             if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
830                 return add_flow(dpif, &put->flow);
831             } else {
832                 return EXFULL;
833             }
834         } else {
835             return ENOENT;
836         }
837     } else {
838         if (put->flags & ODPPF_MODIFY) {
839             int error = set_flow_actions(flow, &put->flow);
840             if (!error && put->flags & ODPPF_ZERO_STATS) {
841                 clear_stats(flow);
842             }
843             return error;
844         } else {
845             return EEXIST;
846         }
847     }
848 }
849
850
851 static int
852 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
853 {
854     struct dp_netdev *dp = get_dp_netdev(dpif);
855     struct dp_netdev_flow *flow;
856
857     flow = dp_netdev_lookup_flow(dp, &odp_flow->key);
858     if (flow) {
859         answer_flow_query(flow, odp_flow);
860         dp_netdev_free_flow(dp, flow);
861         return 0;
862     } else {
863         return ENOENT;
864     }
865 }
866
867 static int
868 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
869 {
870     struct dp_netdev *dp = get_dp_netdev(dpif);
871     struct dp_netdev_flow *flow;
872     int i;
873
874     i = 0;
875     HMAP_FOR_EACH (flow, struct dp_netdev_flow, node, &dp->flow_table) {
876         if (i >= n) {
877             break;
878         }
879         answer_flow_query(flow, &flows[i++]);
880     }
881     return hmap_count(&dp->flow_table);
882 }
883
884 static int
885 dpif_netdev_execute(struct dpif *dpif, uint16_t in_port,
886                     const union odp_action actions[], int n_actions,
887                     const struct ofpbuf *packet)
888 {
889     struct dp_netdev *dp = get_dp_netdev(dpif);
890     struct ofpbuf copy;
891     bool mutates;
892     flow_t flow;
893     int error;
894
895     if (packet->size < ETH_HLEN || packet->size > UINT16_MAX) {
896         return EINVAL;
897     }
898
899     error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
900     if (error) {
901         return error;
902     }
903
904     if (mutates) {
905         /* We need a deep copy of 'packet' since we're going to modify its
906          * data. */
907         ofpbuf_init(&copy, DP_NETDEV_HEADROOM + packet->size);
908         copy.data = (char*)copy.base + DP_NETDEV_HEADROOM;
909         ofpbuf_put(&copy, packet->data, packet->size);
910     } else {
911         /* We still need a shallow copy of 'packet', even though we won't
912          * modify its data, because flow_extract() modifies packet->l2, etc.
913          * We could probably get away with modifying those but it's more polite
914          * if we don't. */
915         copy = *packet;
916     }
917     flow_extract(&copy, in_port, &flow);
918     error = dp_netdev_execute_actions(dp, &copy, &flow, actions, n_actions);
919     if (mutates) {
920         ofpbuf_uninit(&copy);
921     }
922     return error;
923 }
924
925 static int
926 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
927 {
928     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
929     *listen_mask = dpif_netdev->listen_mask;
930     return 0;
931 }
932
933 static int
934 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
935 {
936     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
937     if (!(listen_mask & ~ODPL_ALL)) {
938         dpif_netdev->listen_mask = listen_mask;
939         return 0;
940     } else {
941         return EINVAL;
942     }
943 }
944
945 static struct ovs_queue *
946 find_nonempty_queue(struct dpif *dpif)
947 {
948     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
949     struct dp_netdev *dp = get_dp_netdev(dpif);
950     int mask = dpif_netdev->listen_mask;
951     int i;
952
953     for (i = 0; i < N_QUEUES; i++) {
954         struct ovs_queue *q = &dp->queues[i];
955         if (q->n && mask & (1u << i)) {
956             return q;
957         }
958     }
959     return NULL;
960 }
961
962 static int
963 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
964 {
965     struct ovs_queue *q = find_nonempty_queue(dpif);
966     if (q) {
967         *bufp = queue_pop_head(q);
968         return 0;
969     } else {
970         return EAGAIN;
971     }
972 }
973
974 static void
975 dpif_netdev_recv_wait(struct dpif *dpif)
976 {
977     struct ovs_queue *q = find_nonempty_queue(dpif);
978     if (q) {
979         poll_immediate_wake();
980     } else {
981         /* No messages ready to be received, and dp_wait() will ensure that we
982          * wake up to queue new messages, so there is nothing to do. */
983     }
984 }
985 \f
986 static void
987 dp_netdev_flow_used(struct dp_netdev_flow *flow, const flow_t *key,
988                     const struct ofpbuf *packet)
989 {
990     time_timeval(&flow->used);
991     flow->packet_count++;
992     flow->byte_count += packet->size;
993     if (key->dl_type == htons(ETH_P_IP)) {
994         struct ip_header *nh = packet->l3;
995         flow->ip_tos = nh->ip_tos;
996
997         if (key->nw_proto == IPPROTO_TCP) {
998             struct tcp_header *th = packet->l4;
999             flow->tcp_ctl |= th->tcp_ctl;
1000         }
1001     }
1002 }
1003
1004 static void
1005 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1006                      struct ofpbuf *packet)
1007 {
1008     struct dp_netdev_flow *flow;
1009     flow_t key;
1010
1011     if (flow_extract(packet, port->port_no, &key) && dp->drop_frags) {
1012         dp->n_frags++;
1013         return;
1014     }
1015
1016     flow = dp_netdev_lookup_flow(dp, &key);
1017     if (flow) {
1018         dp_netdev_flow_used(flow, &key, packet);
1019         dp_netdev_execute_actions(dp, packet, &key,
1020                                   flow->actions, flow->n_actions);
1021         dp->n_hit++;
1022     } else {
1023         dp->n_missed++;
1024         dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
1025     }
1026 }
1027
1028 static void
1029 dp_netdev_run(void)
1030 {
1031     struct ofpbuf packet;
1032     struct dp_netdev *dp;
1033
1034     ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu);
1035     LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1036         struct dp_netdev_port *port;
1037
1038         LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1039             int error;
1040
1041             /* Reset packet contents. */
1042             packet.data = (char*)packet.base + DP_NETDEV_HEADROOM;
1043             packet.size = 0;
1044
1045             error = netdev_recv(port->netdev, &packet);
1046             if (!error) {
1047                 dp_netdev_port_input(dp, port, &packet);
1048             } else if (error != EAGAIN) {
1049                 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1050                 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1051                             netdev_get_name(port->netdev), strerror(error));
1052             }
1053         }
1054     }
1055     ofpbuf_uninit(&packet);
1056 }
1057
1058 static void
1059 dp_netdev_wait(void)
1060 {
1061     struct dp_netdev *dp;
1062
1063     LIST_FOR_EACH (dp, struct dp_netdev, node, &dp_netdev_list) {
1064         struct dp_netdev_port *port;
1065         LIST_FOR_EACH (port, struct dp_netdev_port, node, &dp->port_list) {
1066             netdev_recv_wait(port->netdev);
1067         }
1068     }
1069 }
1070
1071 static void
1072 dp_netdev_modify_vlan_tci(struct ofpbuf *packet, flow_t *key,
1073                           uint16_t tci, uint16_t mask)
1074 {
1075     struct vlan_eth_header *veh;
1076
1077     if (key->dl_vlan != htons(ODP_VLAN_NONE)) {
1078         /* Modify 'mask' bits, but maintain other TCI bits. */
1079         veh = packet->l2;
1080         veh->veth_tci &= ~htons(mask);
1081         veh->veth_tci |= htons(tci);
1082     } else {
1083         /* Insert new 802.1Q header. */
1084         struct eth_header *eh = packet->l2;
1085         struct vlan_eth_header tmp;
1086         memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1087         memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1088         tmp.veth_type = htons(ETH_TYPE_VLAN);
1089         tmp.veth_tci = htons(tci);
1090         tmp.veth_next_type = eh->eth_type;
1091
1092         veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1093         memcpy(veh, &tmp, sizeof tmp);
1094         packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1095     }
1096
1097     key->dl_vlan = veh->veth_tci & htons(VLAN_VID_MASK);
1098 }
1099
1100 static void
1101 dp_netdev_strip_vlan(struct ofpbuf *packet, flow_t *key)
1102 {
1103     struct vlan_eth_header *veh = packet->l2;
1104     if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1105         struct eth_header tmp;
1106
1107         memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1108         memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1109         tmp.eth_type = veh->veth_next_type;
1110
1111         packet->size -= VLAN_HEADER_LEN;
1112         packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1113         packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1114         memcpy(packet->data, &tmp, sizeof tmp);
1115
1116         key->dl_vlan = htons(ODP_VLAN_NONE);
1117     }
1118 }
1119
1120 static void
1121 dp_netdev_set_dl_src(struct ofpbuf *packet,
1122                      const uint8_t dl_addr[ETH_ADDR_LEN])
1123 {
1124     struct eth_header *eh = packet->l2;
1125     memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1126 }
1127
1128 static void
1129 dp_netdev_set_dl_dst(struct ofpbuf *packet,
1130                      const uint8_t dl_addr[ETH_ADDR_LEN])
1131 {
1132     struct eth_header *eh = packet->l2;
1133     memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1134 }
1135
1136 static void
1137 dp_netdev_set_nw_addr(struct ofpbuf *packet, flow_t *key,
1138                       const struct odp_action_nw_addr *a)
1139 {
1140     if (key->dl_type == htons(ETH_TYPE_IP)) {
1141         struct ip_header *nh = packet->l3;
1142         uint32_t *field;
1143
1144         field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1145         if (key->nw_proto == IP_TYPE_TCP) {
1146             struct tcp_header *th = packet->l4;
1147             th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1148         } else if (key->nw_proto == IP_TYPE_UDP) {
1149             struct udp_header *uh = packet->l4;
1150             if (uh->udp_csum) {
1151                 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1152                 if (!uh->udp_csum) {
1153                     uh->udp_csum = 0xffff;
1154                 }
1155             }
1156         }
1157         nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1158         *field = a->nw_addr;
1159     }
1160 }
1161
1162 static void
1163 dp_netdev_set_tp_port(struct ofpbuf *packet, flow_t *key,
1164                       const struct odp_action_tp_port *a)
1165 {
1166         if (key->dl_type == htons(ETH_P_IP)) {
1167         uint16_t *field;
1168         if (key->nw_proto == IPPROTO_TCP) {
1169             struct tcp_header *th = packet->l4;
1170             field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1171             th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1172             *field = a->tp_port;
1173         } else if (key->nw_proto == IPPROTO_UDP) {
1174             struct udp_header *uh = packet->l4;
1175             field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1176             uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1177             *field = a->tp_port;
1178         }
1179     }
1180 }
1181
1182 static void
1183 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1184                       uint16_t out_port)
1185 {
1186         struct dp_netdev_port *p = dp->ports[out_port];
1187     if (p) {
1188         netdev_send(p->netdev, packet);
1189     }
1190 }
1191
1192 static void
1193 dp_netdev_output_group(struct dp_netdev *dp, uint16_t group, uint16_t in_port,
1194                        struct ofpbuf *packet)
1195 {
1196         struct odp_port_group *g = &dp->groups[group];
1197         int i;
1198
1199         for (i = 0; i < g->n_ports; i++) {
1200         uint16_t out_port = g->ports[i];
1201         if (out_port != in_port) {
1202             dp_netdev_output_port(dp, packet, out_port);
1203         }
1204         }
1205 }
1206
1207 static int
1208 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1209                          int queue_no, int port_no, uint32_t arg)
1210 {
1211     struct ovs_queue *q = &dp->queues[queue_no];
1212     struct odp_msg *header;
1213     struct ofpbuf *msg;
1214     size_t msg_size;
1215
1216     if (q->n >= MAX_QUEUE_LEN) {
1217         dp->n_lost++;
1218         return ENOBUFS;
1219     }
1220
1221     msg_size = sizeof *header + packet->size;
1222     msg = ofpbuf_new(msg_size);
1223     header = ofpbuf_put_uninit(msg, sizeof *header);
1224     header->type = queue_no;
1225     header->length = msg_size;
1226     header->port = port_no;
1227     header->arg = arg;
1228     ofpbuf_put(msg, packet->data, packet->size);
1229     queue_push_tail(q, msg);
1230
1231     return 0;
1232 }
1233
1234 static int
1235 dp_netdev_execute_actions(struct dp_netdev *dp,
1236                           struct ofpbuf *packet, flow_t *key,
1237                           const union odp_action *actions, int n_actions)
1238 {
1239     int i;
1240     for (i = 0; i < n_actions; i++) {
1241         const union odp_action *a = &actions[i];
1242
1243                 switch (a->type) {
1244                 case ODPAT_OUTPUT:
1245             dp_netdev_output_port(dp, packet, a->output.port);
1246                         break;
1247
1248                 case ODPAT_OUTPUT_GROUP:
1249                         dp_netdev_output_group(dp, a->output_group.group, key->in_port,
1250                                    packet);
1251                         break;
1252
1253                 case ODPAT_CONTROLLER:
1254             dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1255                                      key->in_port, a->controller.arg);
1256                         break;
1257
1258                 case ODPAT_SET_VLAN_VID:
1259                         dp_netdev_modify_vlan_tci(packet, key, ntohs(a->vlan_vid.vlan_vid),
1260                                       VLAN_VID_MASK);
1261             break;
1262
1263                 case ODPAT_SET_VLAN_PCP:
1264                         dp_netdev_modify_vlan_tci(packet, key, a->vlan_pcp.vlan_pcp << 13,
1265                                       VLAN_PCP_MASK);
1266             break;
1267
1268                 case ODPAT_STRIP_VLAN:
1269                         dp_netdev_strip_vlan(packet, key);
1270                         break;
1271
1272                 case ODPAT_SET_DL_SRC:
1273             dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1274                         break;
1275
1276                 case ODPAT_SET_DL_DST:
1277             dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1278                         break;
1279
1280                 case ODPAT_SET_NW_SRC:
1281                 case ODPAT_SET_NW_DST:
1282                         dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1283                         break;
1284
1285                 case ODPAT_SET_TP_SRC:
1286                 case ODPAT_SET_TP_DST:
1287                         dp_netdev_set_tp_port(packet, key, &a->tp_port);
1288                         break;
1289                 }
1290         }
1291     return 0;
1292 }
1293
1294 const struct dpif_class dpif_netdev_class = {
1295     "netdev",
1296     "netdev",
1297     dp_netdev_run,
1298     dp_netdev_wait,
1299     NULL,                       /* enumerate */
1300     dpif_netdev_open,
1301     dpif_netdev_close,
1302     NULL,                       /* get_all_names */
1303     dpif_netdev_delete,
1304     dpif_netdev_get_stats,
1305     dpif_netdev_get_drop_frags,
1306     dpif_netdev_set_drop_frags,
1307     dpif_netdev_port_add,
1308     dpif_netdev_port_del,
1309     dpif_netdev_port_query_by_number,
1310     dpif_netdev_port_query_by_name,
1311     dpif_netdev_port_list,
1312     dpif_netdev_port_poll,
1313     dpif_netdev_port_poll_wait,
1314     dpif_netdev_port_group_get,
1315     dpif_netdev_port_group_set,
1316     dpif_netdev_flow_get,
1317     dpif_netdev_flow_put,
1318     dpif_netdev_flow_del,
1319     dpif_netdev_flow_flush,
1320     dpif_netdev_flow_list,
1321     dpif_netdev_execute,
1322     dpif_netdev_recv_get_mask,
1323     dpif_netdev_recv_set_mask,
1324     dpif_netdev_recv,
1325     dpif_netdev_recv_wait,
1326 };