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