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