queue: Get rid of ovs_queue data structure.
[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 "shash.h"
48 #include "timeval.h"
49 #include "util.h"
50 #include "vlog.h"
51
52 VLOG_DEFINE_THIS_MODULE(dpif_netdev);
53
54 /* Configuration parameters. */
55 enum { N_QUEUES = 2 };          /* Number of queues for dpif_recv(). */
56 enum { MAX_QUEUE_LEN = 100 };   /* Maximum number of packets per queue. */
57 enum { MAX_PORTS = 256 };       /* Maximum number of ports. */
58 enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
59
60 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
61  * headers to be aligned on a 4-byte boundary.  */
62 enum { DP_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
63
64 /* Datapath based on the network device interface from netdev.h. */
65 struct dp_netdev {
66     const struct dpif_class *class;
67     char *name;
68     int open_cnt;
69     bool destroyed;
70
71     bool drop_frags;            /* Drop all IP fragments, if true. */
72     struct list queues[N_QUEUES]; /* Contain ofpbufs queued for dpif_recv(). */
73     size_t queue_len[N_QUEUES]; /* Number of packets in each queue. */
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? */
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,
134                        const char *type, 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         list_init(&dp->queues[i]);
191     }
192     hmap_init(&dp->flow_table);
193     list_init(&dp->port_list);
194     error = do_add_port(dp, name, "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         ofpbuf_list_delete(&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, const char *type,
311             uint16_t port_no)
312 {
313     struct dp_netdev_port *port;
314     struct netdev_options netdev_options;
315     struct netdev *netdev;
316     bool internal;
317     int mtu;
318     int error;
319
320     /* XXX reject devices already in some dp_netdev. */
321     if (type[0] == '\0' || !strcmp(type, "system")) {
322         internal = false;
323     } else if (!strcmp(type, "internal")) {
324         internal = true;
325     } else {
326         VLOG_WARN("%s: unsupported port type %s", devname, type);
327         return EINVAL;
328     }
329
330     /* Open and validate network device. */
331     memset(&netdev_options, 0, sizeof netdev_options);
332     netdev_options.name = devname;
333     netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
334     if (dp->class == &dpif_dummy_class) {
335         netdev_options.type = "dummy";
336     } else if (internal) {
337         netdev_options.type = "tap";
338     }
339
340     error = netdev_open(&netdev_options, &netdev);
341     if (error) {
342         return error;
343     }
344     /* XXX reject loopback devices */
345     /* XXX reject non-Ethernet devices */
346
347     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
348     if (error) {
349         netdev_close(netdev);
350         return error;
351     }
352
353     port = xmalloc(sizeof *port);
354     port->port_no = port_no;
355     port->netdev = netdev;
356     port->internal = internal;
357
358     netdev_get_mtu(netdev, &mtu);
359     if (mtu > max_mtu) {
360         max_mtu = mtu;
361     }
362
363     list_push_back(&dp->port_list, &port->node);
364     dp->ports[port_no] = port;
365     dp->n_ports++;
366     dp->serial++;
367
368     return 0;
369 }
370
371 static int
372 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
373                      uint16_t *port_nop)
374 {
375     struct dp_netdev *dp = get_dp_netdev(dpif);
376     int port_no;
377
378     for (port_no = 0; port_no < MAX_PORTS; port_no++) {
379         if (!dp->ports[port_no]) {
380             *port_nop = port_no;
381             return do_add_port(dp, netdev_get_name(netdev),
382                                netdev_get_type(netdev), port_no);
383         }
384     }
385     return EFBIG;
386 }
387
388 static int
389 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
390 {
391     struct dp_netdev *dp = get_dp_netdev(dpif);
392     return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
393 }
394
395 static bool
396 is_valid_port_number(uint16_t port_no)
397 {
398     return port_no < MAX_PORTS;
399 }
400
401 static int
402 get_port_by_number(struct dp_netdev *dp,
403                    uint16_t port_no, struct dp_netdev_port **portp)
404 {
405     if (!is_valid_port_number(port_no)) {
406         *portp = NULL;
407         return EINVAL;
408     } else {
409         *portp = dp->ports[port_no];
410         return *portp ? 0 : ENOENT;
411     }
412 }
413
414 static int
415 get_port_by_name(struct dp_netdev *dp,
416                  const char *devname, struct dp_netdev_port **portp)
417 {
418     struct dp_netdev_port *port;
419
420     LIST_FOR_EACH (port, node, &dp->port_list) {
421         if (!strcmp(netdev_get_name(port->netdev), devname)) {
422             *portp = port;
423             return 0;
424         }
425     }
426     return ENOENT;
427 }
428
429 static int
430 do_del_port(struct dp_netdev *dp, uint16_t port_no)
431 {
432     struct dp_netdev_port *port;
433     char *name;
434     int error;
435
436     error = get_port_by_number(dp, port_no, &port);
437     if (error) {
438         return error;
439     }
440
441     list_remove(&port->node);
442     dp->ports[port->port_no] = NULL;
443     dp->n_ports--;
444     dp->serial++;
445
446     name = xstrdup(netdev_get_name(port->netdev));
447     netdev_close(port->netdev);
448
449     free(name);
450     free(port);
451
452     return 0;
453 }
454
455 static void
456 answer_port_query(const struct dp_netdev_port *port, struct odp_port *odp_port)
457 {
458     memset(odp_port, 0, sizeof *odp_port);
459     ovs_strlcpy(odp_port->devname, netdev_get_name(port->netdev),
460                 sizeof odp_port->devname);
461     odp_port->port = port->port_no;
462     strcpy(odp_port->type, port->internal ? "internal" : "system");
463 }
464
465 static int
466 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
467                                  struct odp_port *odp_port)
468 {
469     struct dp_netdev *dp = get_dp_netdev(dpif);
470     struct dp_netdev_port *port;
471     int error;
472
473     error = get_port_by_number(dp, port_no, &port);
474     if (!error) {
475         answer_port_query(port, odp_port);
476     }
477     return error;
478 }
479
480 static int
481 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
482                                struct odp_port *odp_port)
483 {
484     struct dp_netdev *dp = get_dp_netdev(dpif);
485     struct dp_netdev_port *port;
486     int error;
487
488     error = get_port_by_name(dp, devname, &port);
489     if (!error) {
490         answer_port_query(port, odp_port);
491     }
492     return error;
493 }
494
495 static void
496 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
497 {
498     hmap_remove(&dp->flow_table, &flow->node);
499     free(flow->actions);
500     free(flow);
501 }
502
503 static void
504 dp_netdev_flow_flush(struct dp_netdev *dp)
505 {
506     struct dp_netdev_flow *flow, *next;
507
508     HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
509         dp_netdev_free_flow(dp, flow);
510     }
511 }
512
513 static int
514 dpif_netdev_flow_flush(struct dpif *dpif)
515 {
516     struct dp_netdev *dp = get_dp_netdev(dpif);
517     dp_netdev_flow_flush(dp);
518     return 0;
519 }
520
521 static int
522 dpif_netdev_port_list(const struct dpif *dpif, struct odp_port *ports, int n)
523 {
524     struct dp_netdev *dp = get_dp_netdev(dpif);
525     struct dp_netdev_port *port;
526     int i;
527
528     i = 0;
529     LIST_FOR_EACH (port, node, &dp->port_list) {
530         struct odp_port *odp_port = &ports[i];
531         if (i >= n) {
532             break;
533         }
534         answer_port_query(port, odp_port);
535         i++;
536     }
537     return dp->n_ports;
538 }
539
540 static int
541 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
542 {
543     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
544     if (dpif->dp_serial != dpif->dp->serial) {
545         dpif->dp_serial = dpif->dp->serial;
546         return ENOBUFS;
547     } else {
548         return EAGAIN;
549     }
550 }
551
552 static void
553 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
554 {
555     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
556     if (dpif->dp_serial != dpif->dp->serial) {
557         poll_immediate_wake();
558     }
559 }
560
561 static struct dp_netdev_flow *
562 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
563 {
564     struct dp_netdev_flow *flow;
565
566     HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
567         if (flow_equal(&flow->key, key)) {
568             return flow;
569         }
570     }
571     return NULL;
572 }
573
574 /* The caller must fill in odp_flow->key itself. */
575 static void
576 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
577                   struct odp_flow *odp_flow)
578 {
579     if (flow) {
580         odp_flow->stats.n_packets = flow->packet_count;
581         odp_flow->stats.n_bytes = flow->byte_count;
582         odp_flow->stats.used_sec = flow->used.tv_sec;
583         odp_flow->stats.used_nsec = flow->used.tv_nsec;
584         odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
585         odp_flow->stats.reserved = 0;
586         odp_flow->stats.error = 0;
587         if (odp_flow->n_actions > 0) {
588             unsigned int n = MIN(odp_flow->n_actions, flow->n_actions);
589             memcpy(odp_flow->actions, flow->actions,
590                    n * sizeof *odp_flow->actions);
591             odp_flow->n_actions = flow->n_actions;
592         }
593
594         if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
595             flow->tcp_ctl = 0;
596         }
597
598     } else {
599         odp_flow->stats.error = ENOENT;
600     }
601 }
602
603 static int
604 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
605 {
606     struct dp_netdev *dp = get_dp_netdev(dpif);
607     int i;
608
609     for (i = 0; i < n; i++) {
610         struct odp_flow *odp_flow = &flows[i];
611         struct flow key;
612
613         odp_flow_key_to_flow(&odp_flow->key, &key);
614         answer_flow_query(dp_netdev_lookup_flow(dp, &key),
615                           odp_flow->flags, odp_flow);
616     }
617     return 0;
618 }
619
620 static int
621 dpif_netdev_validate_actions(const union odp_action *actions, int n_actions,
622                              bool *mutates)
623 {
624     unsigned int i;
625
626     *mutates = false;
627     for (i = 0; i < n_actions; i++) {
628         const union odp_action *a = &actions[i];
629         switch (a->type) {
630         case ODPAT_OUTPUT:
631             if (a->output.port >= MAX_PORTS) {
632                 return EINVAL;
633             }
634             break;
635
636         case ODPAT_CONTROLLER:
637             break;
638
639         case ODPAT_SET_DL_TCI:
640             *mutates = true;
641             if (a->dl_tci.tci & htons(VLAN_CFI)) {
642                 return EINVAL;
643             }
644             break;
645
646         case ODPAT_SET_NW_TOS:
647             *mutates = true;
648             if (a->nw_tos.nw_tos & IP_ECN_MASK) {
649                 return EINVAL;
650             }
651             break;
652
653         case ODPAT_STRIP_VLAN:
654         case ODPAT_SET_DL_SRC:
655         case ODPAT_SET_DL_DST:
656         case ODPAT_SET_NW_SRC:
657         case ODPAT_SET_NW_DST:
658         case ODPAT_SET_TP_SRC:
659         case ODPAT_SET_TP_DST:
660             *mutates = true;
661             break;
662
663         default:
664             return EOPNOTSUPP;
665         }
666     }
667     return 0;
668 }
669
670 static int
671 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
672 {
673     size_t n_bytes;
674     bool mutates;
675     int error;
676
677     if (odp_flow->n_actions >= 4096 / sizeof *odp_flow->actions) {
678         return EINVAL;
679     }
680     error = dpif_netdev_validate_actions(odp_flow->actions,
681                                          odp_flow->n_actions, &mutates);
682     if (error) {
683         return error;
684     }
685
686     n_bytes = odp_flow->n_actions * sizeof *flow->actions;
687     flow->actions = xrealloc(flow->actions, n_bytes);
688     flow->n_actions = odp_flow->n_actions;
689     memcpy(flow->actions, odp_flow->actions, n_bytes);
690     return 0;
691 }
692
693 static int
694 add_flow(struct dpif *dpif, struct odp_flow *odp_flow)
695 {
696     struct dp_netdev *dp = get_dp_netdev(dpif);
697     struct dp_netdev_flow *flow;
698     int error;
699
700     flow = xzalloc(sizeof *flow);
701     odp_flow_key_to_flow(&odp_flow->key, &flow->key);
702
703     error = set_flow_actions(flow, odp_flow);
704     if (error) {
705         free(flow);
706         return error;
707     }
708
709     hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
710     return 0;
711 }
712
713 static void
714 clear_stats(struct dp_netdev_flow *flow)
715 {
716     flow->used.tv_sec = 0;
717     flow->used.tv_nsec = 0;
718     flow->packet_count = 0;
719     flow->byte_count = 0;
720     flow->tcp_ctl = 0;
721 }
722
723 static int
724 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
725 {
726     struct dp_netdev *dp = get_dp_netdev(dpif);
727     struct dp_netdev_flow *flow;
728     struct flow key;
729
730     odp_flow_key_to_flow(&put->flow.key, &key);
731     flow = dp_netdev_lookup_flow(dp, &key);
732     if (!flow) {
733         if (put->flags & ODPPF_CREATE) {
734             if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
735                 return add_flow(dpif, &put->flow);
736             } else {
737                 return EFBIG;
738             }
739         } else {
740             return ENOENT;
741         }
742     } else {
743         if (put->flags & ODPPF_MODIFY) {
744             int error = set_flow_actions(flow, &put->flow);
745             if (!error && put->flags & ODPPF_ZERO_STATS) {
746                 clear_stats(flow);
747             }
748             return error;
749         } else {
750             return EEXIST;
751         }
752     }
753 }
754
755
756 static int
757 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
758 {
759     struct dp_netdev *dp = get_dp_netdev(dpif);
760     struct dp_netdev_flow *flow;
761     struct flow key;
762
763     odp_flow_key_to_flow(&odp_flow->key, &key);
764     flow = dp_netdev_lookup_flow(dp, &key);
765     if (flow) {
766         answer_flow_query(flow, 0, odp_flow);
767         dp_netdev_free_flow(dp, flow);
768         return 0;
769     } else {
770         return ENOENT;
771     }
772 }
773
774 static int
775 dpif_netdev_flow_list(const struct dpif *dpif, struct odp_flow flows[], int n)
776 {
777     struct dp_netdev *dp = get_dp_netdev(dpif);
778     struct dp_netdev_flow *flow;
779     int i;
780
781     i = 0;
782     HMAP_FOR_EACH (flow, node, &dp->flow_table) {
783         if (i >= n) {
784             break;
785         }
786
787         odp_flow_key_from_flow(&flows[i].key, &flow->key);
788         answer_flow_query(flow, 0, &flows[i]);
789         i++;
790     }
791     return hmap_count(&dp->flow_table);
792 }
793
794 static int
795 dpif_netdev_execute(struct dpif *dpif,
796                     const union odp_action actions[], int n_actions,
797                     const struct ofpbuf *packet)
798 {
799     struct dp_netdev *dp = get_dp_netdev(dpif);
800     struct ofpbuf copy;
801     bool mutates;
802     struct flow key;
803     int error;
804
805     if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
806         return EINVAL;
807     }
808
809     error = dpif_netdev_validate_actions(actions, n_actions, &mutates);
810     if (error) {
811         return error;
812     }
813
814     if (mutates) {
815         /* We need a deep copy of 'packet' since we're going to modify its
816          * data. */
817         ofpbuf_init(&copy, DP_NETDEV_HEADROOM + packet->size);
818         copy.data = (char*)copy.base + DP_NETDEV_HEADROOM;
819         ofpbuf_put(&copy, packet->data, packet->size);
820     } else {
821         /* We still need a shallow copy of 'packet', even though we won't
822          * modify its data, because flow_extract() modifies packet->l2, etc.
823          * We could probably get away with modifying those but it's more polite
824          * if we don't. */
825         copy = *packet;
826     }
827     flow_extract(&copy, 0, -1, &key);
828     error = dp_netdev_execute_actions(dp, &copy, &key, actions, n_actions);
829     if (mutates) {
830         ofpbuf_uninit(&copy);
831     }
832     return error;
833 }
834
835 static int
836 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
837 {
838     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
839     *listen_mask = dpif_netdev->listen_mask;
840     return 0;
841 }
842
843 static int
844 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
845 {
846     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
847     if (!(listen_mask & ~ODPL_ALL)) {
848         dpif_netdev->listen_mask = listen_mask;
849         return 0;
850     } else {
851         return EINVAL;
852     }
853 }
854
855 static int
856 find_nonempty_queue(struct dpif *dpif)
857 {
858     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
859     struct dp_netdev *dp = get_dp_netdev(dpif);
860     int mask = dpif_netdev->listen_mask;
861     int i;
862
863     for (i = 0; i < N_QUEUES; i++) {
864         struct list *queue = &dp->queues[i];
865         if (!list_is_empty(queue) && mask & (1u << i)) {
866             return i;
867         }
868     }
869     return -1;
870 }
871
872 static int
873 dpif_netdev_recv(struct dpif *dpif, struct ofpbuf **bufp)
874 {
875     int queue_idx = find_nonempty_queue(dpif);
876     if (queue_idx >= 0) {
877         struct dp_netdev *dp = get_dp_netdev(dpif);
878
879         *bufp = ofpbuf_from_list(list_pop_front(&dp->queues[queue_idx]));
880         dp->queue_len[queue_idx]--;
881
882         return 0;
883     } else {
884         return EAGAIN;
885     }
886 }
887
888 static void
889 dpif_netdev_recv_wait(struct dpif *dpif)
890 {
891     if (find_nonempty_queue(dpif) >= 0) {
892         poll_immediate_wake();
893     } else {
894         /* No messages ready to be received, and dp_wait() will ensure that we
895          * wake up to queue new messages, so there is nothing to do. */
896     }
897 }
898 \f
899 static void
900 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
901                     const struct ofpbuf *packet)
902 {
903     time_timespec(&flow->used);
904     flow->packet_count++;
905     flow->byte_count += packet->size;
906     if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
907         struct tcp_header *th = packet->l4;
908         flow->tcp_ctl |= th->tcp_ctl;
909     }
910 }
911
912 static void
913 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
914                      struct ofpbuf *packet)
915 {
916     struct dp_netdev_flow *flow;
917     struct flow key;
918
919     if (packet->size < ETH_HEADER_LEN) {
920         return;
921     }
922     if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
923         dp->n_frags++;
924         return;
925     }
926
927     flow = dp_netdev_lookup_flow(dp, &key);
928     if (flow) {
929         dp_netdev_flow_used(flow, &key, packet);
930         dp_netdev_execute_actions(dp, packet, &key,
931                                   flow->actions, flow->n_actions);
932         dp->n_hit++;
933     } else {
934         dp->n_missed++;
935         dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, port->port_no, 0);
936     }
937 }
938
939 static void
940 dp_netdev_run(void)
941 {
942     struct shash_node *node;
943     struct ofpbuf packet;
944
945     ofpbuf_init(&packet, DP_NETDEV_HEADROOM + max_mtu);
946     SHASH_FOR_EACH (node, &dp_netdevs) {
947         struct dp_netdev *dp = node->data;
948         struct dp_netdev_port *port;
949
950         LIST_FOR_EACH (port, node, &dp->port_list) {
951             int error;
952
953             /* Reset packet contents. */
954             packet.data = (char*)packet.base + DP_NETDEV_HEADROOM;
955             packet.size = 0;
956
957             error = netdev_recv(port->netdev, &packet);
958             if (!error) {
959                 dp_netdev_port_input(dp, port, &packet);
960             } else if (error != EAGAIN && error != EOPNOTSUPP) {
961                 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
962                 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
963                             netdev_get_name(port->netdev), strerror(error));
964             }
965         }
966     }
967     ofpbuf_uninit(&packet);
968 }
969
970 static void
971 dp_netdev_wait(void)
972 {
973     struct shash_node *node;
974
975     SHASH_FOR_EACH (node, &dp_netdevs) {
976         struct dp_netdev *dp = node->data;
977         struct dp_netdev_port *port;
978
979         LIST_FOR_EACH (port, node, &dp->port_list) {
980             netdev_recv_wait(port->netdev);
981         }
982     }
983 }
984
985
986 /* Modify the TCI field of 'packet'.  If a VLAN tag is present, its TCI field
987  * is replaced by 'tci'.  If a VLAN tag is not present, one is added with the
988  * TCI field set to 'tci'.
989  */
990 static void
991 dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci)
992 {
993     struct vlan_eth_header *veh;
994     struct eth_header *eh;
995
996     eh = packet->l2;
997     if (packet->size >= sizeof(struct vlan_eth_header)
998         && eh->eth_type == htons(ETH_TYPE_VLAN)) {
999         veh = packet->l2;
1000         veh->veth_tci = tci;
1001     } else {
1002         /* Insert new 802.1Q header. */
1003         struct vlan_eth_header tmp;
1004         memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1005         memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1006         tmp.veth_type = htons(ETH_TYPE_VLAN);
1007         tmp.veth_tci = tci;
1008         tmp.veth_next_type = eh->eth_type;
1009
1010         veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1011         memcpy(veh, &tmp, sizeof tmp);
1012         packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1013     }
1014 }
1015
1016 static void
1017 dp_netdev_strip_vlan(struct ofpbuf *packet)
1018 {
1019     struct vlan_eth_header *veh = packet->l2;
1020     if (packet->size >= sizeof *veh
1021         && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1022         struct eth_header tmp;
1023
1024         memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1025         memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1026         tmp.eth_type = veh->veth_next_type;
1027
1028         packet->size -= VLAN_HEADER_LEN;
1029         packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1030         packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1031         memcpy(packet->data, &tmp, sizeof tmp);
1032     }
1033 }
1034
1035 static void
1036 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1037 {
1038     struct eth_header *eh = packet->l2;
1039     memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1040 }
1041
1042 static void
1043 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1044 {
1045     struct eth_header *eh = packet->l2;
1046     memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1047 }
1048
1049 static bool
1050 is_ip(const struct ofpbuf *packet, const struct flow *key)
1051 {
1052     return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1053 }
1054
1055 static void
1056 dp_netdev_set_nw_addr(struct ofpbuf *packet, struct flow *key,
1057                       const struct odp_action_nw_addr *a)
1058 {
1059     if (is_ip(packet, key)) {
1060         struct ip_header *nh = packet->l3;
1061         uint32_t *field;
1062
1063         field = a->type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1064         if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1065             struct tcp_header *th = packet->l4;
1066             th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1067         } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1068             struct udp_header *uh = packet->l4;
1069             if (uh->udp_csum) {
1070                 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1071                 if (!uh->udp_csum) {
1072                     uh->udp_csum = 0xffff;
1073                 }
1074             }
1075         }
1076         nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1077         *field = a->nw_addr;
1078     }
1079 }
1080
1081 static void
1082 dp_netdev_set_nw_tos(struct ofpbuf *packet, struct flow *key,
1083                      const struct odp_action_nw_tos *a)
1084 {
1085     if (is_ip(packet, key)) {
1086         struct ip_header *nh = packet->l3;
1087         uint8_t *field = &nh->ip_tos;
1088
1089         /* Set the DSCP bits and preserve the ECN bits. */
1090         uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1091
1092         nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1093                 htons((uint16_t)a->nw_tos));
1094         *field = new;
1095     }
1096 }
1097
1098 static void
1099 dp_netdev_set_tp_port(struct ofpbuf *packet, struct flow *key,
1100                       const struct odp_action_tp_port *a)
1101 {
1102         if (is_ip(packet, key)) {
1103         uint16_t *field;
1104         if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1105             struct tcp_header *th = packet->l4;
1106             field = a->type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1107             th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1108             *field = a->tp_port;
1109         } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1110             struct udp_header *uh = packet->l4;
1111             field = a->type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1112             uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1113             *field = a->tp_port;
1114         } else {
1115             return;
1116         }
1117     }
1118 }
1119
1120 static void
1121 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1122                       uint16_t out_port)
1123 {
1124     struct dp_netdev_port *p = dp->ports[out_port];
1125     if (p) {
1126         netdev_send(p->netdev, packet);
1127     }
1128 }
1129
1130 static int
1131 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1132                          int queue_no, int port_no, uint32_t arg)
1133 {
1134     struct odp_msg *header;
1135     struct ofpbuf *msg;
1136     size_t msg_size;
1137
1138     if (dp->queue_len[queue_no] >= MAX_QUEUE_LEN) {
1139         dp->n_lost++;
1140         return ENOBUFS;
1141     }
1142
1143     msg_size = sizeof *header + packet->size;
1144     msg = ofpbuf_new_with_headroom(msg_size, DPIF_RECV_MSG_PADDING);
1145     header = ofpbuf_put_uninit(msg, sizeof *header);
1146     header->type = queue_no;
1147     header->length = msg_size;
1148     header->port = port_no;
1149     header->arg = arg;
1150     ofpbuf_put(msg, packet->data, packet->size);
1151     list_push_back(&dp->queues[queue_no], &msg->list_node);
1152     dp->queue_len[queue_no]++;
1153
1154     return 0;
1155 }
1156
1157 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1158  * screwy or truncated header fields or one whose inner and outer Ethernet
1159  * address differ. */
1160 static bool
1161 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1162 {
1163     struct arp_eth_header *arp;
1164     struct eth_header *eth;
1165     ptrdiff_t l3_size;
1166
1167     if (key->dl_type != htons(ETH_TYPE_ARP)) {
1168         return false;
1169     }
1170
1171     l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1172     if (l3_size < sizeof(struct arp_eth_header)) {
1173         return true;
1174     }
1175
1176     eth = packet->l2;
1177     arp = packet->l3;
1178     return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1179             || arp->ar_pro != htons(ARP_PRO_IP)
1180             || arp->ar_hln != ETH_HEADER_LEN
1181             || arp->ar_pln != 4
1182             || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1183 }
1184
1185 static int
1186 dp_netdev_execute_actions(struct dp_netdev *dp,
1187                           struct ofpbuf *packet, struct flow *key,
1188                           const union odp_action *actions, int n_actions)
1189 {
1190     int i;
1191     for (i = 0; i < n_actions; i++) {
1192         const union odp_action *a = &actions[i];
1193
1194         switch (a->type) {
1195         case ODPAT_OUTPUT:
1196             dp_netdev_output_port(dp, packet, a->output.port);
1197             break;
1198
1199         case ODPAT_CONTROLLER:
1200             dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1201                                      key->in_port, a->controller.arg);
1202             break;
1203
1204         case ODPAT_SET_DL_TCI:
1205             dp_netdev_set_dl_tci(packet, a->dl_tci.tci);
1206             break;
1207
1208         case ODPAT_STRIP_VLAN:
1209             dp_netdev_strip_vlan(packet);
1210             break;
1211
1212         case ODPAT_SET_DL_SRC:
1213             dp_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1214             break;
1215
1216         case ODPAT_SET_DL_DST:
1217             dp_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1218             break;
1219
1220         case ODPAT_SET_NW_SRC:
1221         case ODPAT_SET_NW_DST:
1222             dp_netdev_set_nw_addr(packet, key, &a->nw_addr);
1223             break;
1224
1225         case ODPAT_SET_NW_TOS:
1226             dp_netdev_set_nw_tos(packet, key, &a->nw_tos);
1227             break;
1228
1229         case ODPAT_SET_TP_SRC:
1230         case ODPAT_SET_TP_DST:
1231             dp_netdev_set_tp_port(packet, key, &a->tp_port);
1232             break;
1233
1234         case ODPAT_DROP_SPOOFED_ARP:
1235             if (dp_netdev_is_spoofed_arp(packet, key)) {
1236                 return 0;
1237             }
1238         }
1239     }
1240     return 0;
1241 }
1242
1243 const struct dpif_class dpif_netdev_class = {
1244     "netdev",
1245     dp_netdev_run,
1246     dp_netdev_wait,
1247     NULL,                       /* enumerate */
1248     dpif_netdev_open,
1249     dpif_netdev_close,
1250     NULL,                       /* get_all_names */
1251     dpif_netdev_destroy,
1252     dpif_netdev_get_stats,
1253     dpif_netdev_get_drop_frags,
1254     dpif_netdev_set_drop_frags,
1255     dpif_netdev_port_add,
1256     dpif_netdev_port_del,
1257     dpif_netdev_port_query_by_number,
1258     dpif_netdev_port_query_by_name,
1259     dpif_netdev_port_list,
1260     dpif_netdev_port_poll,
1261     dpif_netdev_port_poll_wait,
1262     dpif_netdev_flow_get,
1263     dpif_netdev_flow_put,
1264     dpif_netdev_flow_del,
1265     dpif_netdev_flow_flush,
1266     dpif_netdev_flow_list,
1267     dpif_netdev_execute,
1268     dpif_netdev_recv_get_mask,
1269     dpif_netdev_recv_set_mask,
1270     NULL,                       /* get_sflow_probability */
1271     NULL,                       /* set_sflow_probability */
1272     NULL,                       /* queue_to_priority */
1273     dpif_netdev_recv,
1274     dpif_netdev_recv_wait,
1275 };
1276
1277 void
1278 dpif_dummy_register(void)
1279 {
1280     if (!dpif_dummy_class.type) {
1281         dpif_dummy_class = dpif_netdev_class;
1282         dpif_dummy_class.type = "dummy";
1283         dp_register_provider(&dpif_dummy_class);
1284     }
1285 }