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