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