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