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