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