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