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