datapath: Drop port information from odp_stats.
[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_purge_queues(struct dp_netdev *dp)
250 {
251     int i;
252
253     for (i = 0; i < N_QUEUES; i++) {
254         struct dp_netdev_queue *q = &dp->queues[i];
255
256         while (q->tail != q->head) {
257             struct dpif_upcall *upcall = q->upcalls[q->tail++ & QUEUE_MASK];
258
259             ofpbuf_delete(upcall->packet);
260             free(upcall);
261         }
262     }
263 }
264
265 static void
266 dp_netdev_free(struct dp_netdev *dp)
267 {
268     dp_netdev_flow_flush(dp);
269     while (dp->n_ports > 0) {
270         struct dp_netdev_port *port = CONTAINER_OF(
271             dp->port_list.next, struct dp_netdev_port, node);
272         do_del_port(dp, port->port_no);
273     }
274     dp_netdev_purge_queues(dp);
275     hmap_destroy(&dp->flow_table);
276     free(dp->name);
277     free(dp);
278 }
279
280 static void
281 dpif_netdev_close(struct dpif *dpif)
282 {
283     struct dp_netdev *dp = get_dp_netdev(dpif);
284     assert(dp->open_cnt > 0);
285     if (--dp->open_cnt == 0 && dp->destroyed) {
286         shash_find_and_delete(&dp_netdevs, dp->name);
287         dp_netdev_free(dp);
288     }
289     free(dpif);
290 }
291
292 static int
293 dpif_netdev_destroy(struct dpif *dpif)
294 {
295     struct dp_netdev *dp = get_dp_netdev(dpif);
296     dp->destroyed = true;
297     return 0;
298 }
299
300 static int
301 dpif_netdev_get_stats(const struct dpif *dpif, struct odp_stats *stats)
302 {
303     struct dp_netdev *dp = get_dp_netdev(dpif);
304     memset(stats, 0, sizeof *stats);
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     return 0;
310 }
311
312 static int
313 dpif_netdev_get_drop_frags(const struct dpif *dpif, bool *drop_fragsp)
314 {
315     struct dp_netdev *dp = get_dp_netdev(dpif);
316     *drop_fragsp = dp->drop_frags;
317     return 0;
318 }
319
320 static int
321 dpif_netdev_set_drop_frags(struct dpif *dpif, bool drop_frags)
322 {
323     struct dp_netdev *dp = get_dp_netdev(dpif);
324     dp->drop_frags = drop_frags;
325     return 0;
326 }
327
328 static int
329 do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
330             uint16_t port_no)
331 {
332     struct dp_netdev_port *port;
333     struct netdev_options netdev_options;
334     struct netdev *netdev;
335     bool internal;
336     int mtu;
337     int error;
338
339     /* XXX reject devices already in some dp_netdev. */
340     if (type[0] == '\0' || !strcmp(type, "system")) {
341         internal = false;
342     } else if (!strcmp(type, "internal")) {
343         internal = true;
344     } else {
345         VLOG_WARN("%s: unsupported port type %s", devname, type);
346         return EINVAL;
347     }
348
349     /* Open and validate network device. */
350     memset(&netdev_options, 0, sizeof netdev_options);
351     netdev_options.name = devname;
352     netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
353     if (dp->class == &dpif_dummy_class) {
354         netdev_options.type = "dummy";
355     } else if (internal) {
356         netdev_options.type = "tap";
357     }
358
359     error = netdev_open(&netdev_options, &netdev);
360     if (error) {
361         return error;
362     }
363     /* XXX reject loopback devices */
364     /* XXX reject non-Ethernet devices */
365
366     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
367     if (error) {
368         netdev_close(netdev);
369         return error;
370     }
371
372     port = xmalloc(sizeof *port);
373     port->port_no = port_no;
374     port->netdev = netdev;
375     port->internal = internal;
376
377     netdev_get_mtu(netdev, &mtu);
378     if (mtu > max_mtu) {
379         max_mtu = mtu;
380     }
381
382     list_push_back(&dp->port_list, &port->node);
383     dp->ports[port_no] = port;
384     dp->n_ports++;
385     dp->serial++;
386
387     return 0;
388 }
389
390 static int
391 dpif_netdev_port_add(struct dpif *dpif, struct netdev *netdev,
392                      uint16_t *port_nop)
393 {
394     struct dp_netdev *dp = get_dp_netdev(dpif);
395     int port_no;
396
397     for (port_no = 0; port_no < MAX_PORTS; port_no++) {
398         if (!dp->ports[port_no]) {
399             *port_nop = port_no;
400             return do_add_port(dp, netdev_get_name(netdev),
401                                netdev_get_type(netdev), port_no);
402         }
403     }
404     return EFBIG;
405 }
406
407 static int
408 dpif_netdev_port_del(struct dpif *dpif, uint16_t port_no)
409 {
410     struct dp_netdev *dp = get_dp_netdev(dpif);
411     return port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
412 }
413
414 static bool
415 is_valid_port_number(uint16_t port_no)
416 {
417     return port_no < MAX_PORTS;
418 }
419
420 static int
421 get_port_by_number(struct dp_netdev *dp,
422                    uint16_t port_no, struct dp_netdev_port **portp)
423 {
424     if (!is_valid_port_number(port_no)) {
425         *portp = NULL;
426         return EINVAL;
427     } else {
428         *portp = dp->ports[port_no];
429         return *portp ? 0 : ENOENT;
430     }
431 }
432
433 static int
434 get_port_by_name(struct dp_netdev *dp,
435                  const char *devname, struct dp_netdev_port **portp)
436 {
437     struct dp_netdev_port *port;
438
439     LIST_FOR_EACH (port, node, &dp->port_list) {
440         if (!strcmp(netdev_get_name(port->netdev), devname)) {
441             *portp = port;
442             return 0;
443         }
444     }
445     return ENOENT;
446 }
447
448 static int
449 do_del_port(struct dp_netdev *dp, uint16_t port_no)
450 {
451     struct dp_netdev_port *port;
452     char *name;
453     int error;
454
455     error = get_port_by_number(dp, port_no, &port);
456     if (error) {
457         return error;
458     }
459
460     list_remove(&port->node);
461     dp->ports[port->port_no] = NULL;
462     dp->n_ports--;
463     dp->serial++;
464
465     name = xstrdup(netdev_get_name(port->netdev));
466     netdev_close(port->netdev);
467
468     free(name);
469     free(port);
470
471     return 0;
472 }
473
474 static void
475 answer_port_query(const struct dp_netdev_port *port,
476                   struct dpif_port *dpif_port)
477 {
478     dpif_port->name = xstrdup(netdev_get_name(port->netdev));
479     dpif_port->type = xstrdup(port->internal ? "internal" : "system");
480     dpif_port->port_no = port->port_no;
481 }
482
483 static int
484 dpif_netdev_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
485                                  struct dpif_port *dpif_port)
486 {
487     struct dp_netdev *dp = get_dp_netdev(dpif);
488     struct dp_netdev_port *port;
489     int error;
490
491     error = get_port_by_number(dp, port_no, &port);
492     if (!error) {
493         answer_port_query(port, dpif_port);
494     }
495     return error;
496 }
497
498 static int
499 dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname,
500                                struct dpif_port *dpif_port)
501 {
502     struct dp_netdev *dp = get_dp_netdev(dpif);
503     struct dp_netdev_port *port;
504     int error;
505
506     error = get_port_by_name(dp, devname, &port);
507     if (!error) {
508         answer_port_query(port, dpif_port);
509     }
510     return error;
511 }
512
513 static int
514 dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
515 {
516     return MAX_PORTS;
517 }
518
519 static void
520 dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
521 {
522     hmap_remove(&dp->flow_table, &flow->node);
523     free(flow->actions);
524     free(flow);
525 }
526
527 static void
528 dp_netdev_flow_flush(struct dp_netdev *dp)
529 {
530     struct dp_netdev_flow *flow, *next;
531
532     HMAP_FOR_EACH_SAFE (flow, next, node, &dp->flow_table) {
533         dp_netdev_free_flow(dp, flow);
534     }
535 }
536
537 static int
538 dpif_netdev_flow_flush(struct dpif *dpif)
539 {
540     struct dp_netdev *dp = get_dp_netdev(dpif);
541     dp_netdev_flow_flush(dp);
542     return 0;
543 }
544
545 struct dp_netdev_port_state {
546     uint32_t port_no;
547     char *name;
548 };
549
550 static int
551 dpif_netdev_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
552 {
553     *statep = xzalloc(sizeof(struct dp_netdev_port_state));
554     return 0;
555 }
556
557 static int
558 dpif_netdev_port_dump_next(const struct dpif *dpif, void *state_,
559                            struct dpif_port *dpif_port)
560 {
561     struct dp_netdev_port_state *state = state_;
562     struct dp_netdev *dp = get_dp_netdev(dpif);
563     uint32_t port_no;
564
565     for (port_no = state->port_no; port_no < MAX_PORTS; port_no++) {
566         struct dp_netdev_port *port = dp->ports[port_no];
567         if (port) {
568             free(state->name);
569             state->name = xstrdup(netdev_get_name(port->netdev));
570             dpif_port->name = state->name;
571             dpif_port->type = port->internal ? "internal" : "system";
572             dpif_port->port_no = port->port_no;
573             state->port_no = port_no + 1;
574             return 0;
575         }
576     }
577     return EOF;
578 }
579
580 static int
581 dpif_netdev_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
582 {
583     struct dp_netdev_port_state *state = state_;
584     free(state->name);
585     free(state);
586     return 0;
587 }
588
589 static int
590 dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
591 {
592     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
593     if (dpif->dp_serial != dpif->dp->serial) {
594         dpif->dp_serial = dpif->dp->serial;
595         return ENOBUFS;
596     } else {
597         return EAGAIN;
598     }
599 }
600
601 static void
602 dpif_netdev_port_poll_wait(const struct dpif *dpif_)
603 {
604     struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
605     if (dpif->dp_serial != dpif->dp->serial) {
606         poll_immediate_wake();
607     }
608 }
609
610 static struct dp_netdev_flow *
611 dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
612 {
613     struct dp_netdev_flow *flow;
614
615     HMAP_FOR_EACH_WITH_HASH (flow, node, flow_hash(key, 0), &dp->flow_table) {
616         if (flow_equal(&flow->key, key)) {
617             return flow;
618         }
619     }
620     return NULL;
621 }
622
623 /* The caller must fill in odp_flow->key itself. */
624 static void
625 answer_flow_query(struct dp_netdev_flow *flow, uint32_t query_flags,
626                   struct odp_flow *odp_flow)
627 {
628     if (flow) {
629         odp_flow->stats.n_packets = flow->packet_count;
630         odp_flow->stats.n_bytes = flow->byte_count;
631         odp_flow->stats.used_sec = flow->used.tv_sec;
632         odp_flow->stats.used_nsec = flow->used.tv_nsec;
633         odp_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
634         odp_flow->stats.reserved = 0;
635         odp_flow->stats.error = 0;
636         if (odp_flow->actions_len > 0) {
637             memcpy(odp_flow->actions, flow->actions,
638                    MIN(odp_flow->actions_len, flow->actions_len));
639             odp_flow->actions_len = flow->actions_len;
640         }
641
642         if (query_flags & ODPFF_ZERO_TCP_FLAGS) {
643             flow->tcp_ctl = 0;
644         }
645
646     } else {
647         odp_flow->stats.error = ENOENT;
648     }
649 }
650
651 static int
652 dpif_netdev_flow_from_nlattrs(const struct nlattr *key, uint32_t key_len,
653                               struct flow *flow)
654 {
655     if (odp_flow_key_to_flow(key, key_len, flow)) {
656         /* This should not happen: it indicates that odp_flow_key_from_flow()
657          * and odp_flow_key_to_flow() disagree on the acceptable form of a
658          * flow.  Log the problem as an error, with enough details to enable
659          * debugging. */
660         static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
661
662         if (!VLOG_DROP_ERR(&rl)) {
663             struct ds s;
664
665             ds_init(&s);
666             odp_flow_key_format(key, key_len, &s);
667             VLOG_ERR("internal error parsing flow key %s", ds_cstr(&s));
668             ds_destroy(&s);
669         }
670
671         return EINVAL;
672     }
673
674     return 0;
675 }
676
677 static int
678 dpif_netdev_flow_get(const struct dpif *dpif, struct odp_flow flows[], int n)
679 {
680     struct dp_netdev *dp = get_dp_netdev(dpif);
681     int i;
682
683     for (i = 0; i < n; i++) {
684         struct odp_flow *odp_flow = &flows[i];
685         struct flow key;
686         int error;
687
688         error = dpif_netdev_flow_from_nlattrs(odp_flow->key, odp_flow->key_len,
689                                               &key);
690         if (error) {
691             return error;
692         }
693
694         answer_flow_query(dp_netdev_lookup_flow(dp, &key),
695                           odp_flow->flags, odp_flow);
696     }
697     return 0;
698 }
699
700 static int
701 dpif_netdev_validate_actions(const struct nlattr *actions,
702                              size_t actions_len, bool *mutates)
703 {
704     const struct nlattr *a;
705     unsigned int left;
706
707     *mutates = false;
708     NL_ATTR_FOR_EACH (a, left, actions, actions_len) {
709         uint16_t type = nl_attr_type(a);
710         int len = odp_action_len(type);
711
712         if (len != nl_attr_get_size(a)) {
713             return EINVAL;
714         }
715
716         switch (type) {
717         case ODPAT_OUTPUT:
718             if (nl_attr_get_u32(a) >= MAX_PORTS) {
719                 return EINVAL;
720             }
721             break;
722
723         case ODPAT_CONTROLLER:
724         case ODPAT_DROP_SPOOFED_ARP:
725             break;
726
727         case ODPAT_SET_DL_TCI:
728             *mutates = true;
729             if (nl_attr_get_be16(a) & htons(VLAN_CFI)) {
730                 return EINVAL;
731             }
732             break;
733
734         case ODPAT_SET_NW_TOS:
735             *mutates = true;
736             if (nl_attr_get_u8(a) & IP_ECN_MASK) {
737                 return EINVAL;
738             }
739             break;
740
741         case ODPAT_STRIP_VLAN:
742         case ODPAT_SET_DL_SRC:
743         case ODPAT_SET_DL_DST:
744         case ODPAT_SET_NW_SRC:
745         case ODPAT_SET_NW_DST:
746         case ODPAT_SET_TP_SRC:
747         case ODPAT_SET_TP_DST:
748             *mutates = true;
749             break;
750
751         case ODPAT_SET_TUNNEL:
752         case ODPAT_SET_PRIORITY:
753         case ODPAT_POP_PRIORITY:
754         default:
755             return EOPNOTSUPP;
756         }
757     }
758     return 0;
759 }
760
761 static int
762 set_flow_actions(struct dp_netdev_flow *flow, struct odp_flow *odp_flow)
763 {
764     bool mutates;
765     int error;
766
767     error = dpif_netdev_validate_actions(odp_flow->actions,
768                                          odp_flow->actions_len, &mutates);
769     if (error) {
770         return error;
771     }
772
773     flow->actions = xrealloc(flow->actions, odp_flow->actions_len);
774     flow->actions_len = odp_flow->actions_len;
775     memcpy(flow->actions, odp_flow->actions, odp_flow->actions_len);
776     return 0;
777 }
778
779 static int
780 add_flow(struct dpif *dpif, const struct flow *key, struct odp_flow *odp_flow)
781 {
782     struct dp_netdev *dp = get_dp_netdev(dpif);
783     struct dp_netdev_flow *flow;
784     int error;
785
786     flow = xzalloc(sizeof *flow);
787     flow->key = *key;
788
789     error = set_flow_actions(flow, odp_flow);
790     if (error) {
791         free(flow);
792         return error;
793     }
794
795     hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
796     return 0;
797 }
798
799 static void
800 clear_stats(struct dp_netdev_flow *flow)
801 {
802     flow->used.tv_sec = 0;
803     flow->used.tv_nsec = 0;
804     flow->packet_count = 0;
805     flow->byte_count = 0;
806     flow->tcp_ctl = 0;
807 }
808
809 static int
810 dpif_netdev_flow_put(struct dpif *dpif, struct odp_flow_put *put)
811 {
812     struct dp_netdev *dp = get_dp_netdev(dpif);
813     struct dp_netdev_flow *flow;
814     struct flow key;
815     int error;
816
817     error = dpif_netdev_flow_from_nlattrs(put->flow.key, put->flow.key_len,
818                                           &key);
819     if (error) {
820         return error;
821     }
822
823     flow = dp_netdev_lookup_flow(dp, &key);
824     if (!flow) {
825         if (put->flags & ODPPF_CREATE) {
826             if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
827                 return add_flow(dpif, &key, &put->flow);
828             } else {
829                 return EFBIG;
830             }
831         } else {
832             return ENOENT;
833         }
834     } else {
835         if (put->flags & ODPPF_MODIFY) {
836             int error = set_flow_actions(flow, &put->flow);
837             if (!error && put->flags & ODPPF_ZERO_STATS) {
838                 clear_stats(flow);
839             }
840             return error;
841         } else {
842             return EEXIST;
843         }
844     }
845 }
846
847
848 static int
849 dpif_netdev_flow_del(struct dpif *dpif, struct odp_flow *odp_flow)
850 {
851     struct dp_netdev *dp = get_dp_netdev(dpif);
852     struct dp_netdev_flow *flow;
853     struct flow key;
854     int error;
855
856     error = dpif_netdev_flow_from_nlattrs(odp_flow->key, odp_flow->key_len,
857                                           &key);
858     if (error) {
859         return error;
860     }
861
862     flow = dp_netdev_lookup_flow(dp, &key);
863     if (flow) {
864         answer_flow_query(flow, 0, odp_flow);
865         dp_netdev_free_flow(dp, flow);
866         return 0;
867     } else {
868         return ENOENT;
869     }
870 }
871
872 struct dp_netdev_flow_state {
873     uint32_t bucket;
874     uint32_t offset;
875 };
876
877 static int
878 dpif_netdev_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
879 {
880     *statep = xzalloc(sizeof(struct dp_netdev_flow_state));
881     return 0;
882 }
883
884 static int
885 dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
886                            struct odp_flow *odp_flow)
887 {
888     struct dp_netdev_flow_state *state = state_;
889     struct dp_netdev *dp = get_dp_netdev(dpif);
890     struct dp_netdev_flow *flow;
891     struct hmap_node *node;
892     struct ofpbuf key;
893
894     node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
895     if (!node) {
896         return EOF;
897     }
898
899     flow = CONTAINER_OF(node, struct dp_netdev_flow, node);
900
901     ofpbuf_use_stack(&key, odp_flow->key, odp_flow->key_len);
902     odp_flow_key_from_flow(&key, &flow->key);
903     odp_flow->key_len = key.size;
904     ofpbuf_uninit(&key);
905
906     answer_flow_query(flow, 0, odp_flow);
907
908     return 0;
909 }
910
911 static int
912 dpif_netdev_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state)
913 {
914     free(state);
915     return 0;
916 }
917
918 static int
919 dpif_netdev_execute(struct dpif *dpif,
920                     const struct nlattr *actions, size_t actions_len,
921                     const struct ofpbuf *packet)
922 {
923     struct dp_netdev *dp = get_dp_netdev(dpif);
924     struct ofpbuf copy;
925     bool mutates;
926     struct flow key;
927     int error;
928
929     if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
930         return EINVAL;
931     }
932
933     error = dpif_netdev_validate_actions(actions, actions_len, &mutates);
934     if (error) {
935         return error;
936     }
937
938     if (mutates) {
939         /* We need a deep copy of 'packet' since we're going to modify its
940          * data. */
941         ofpbuf_init(&copy, DP_NETDEV_HEADROOM + packet->size);
942         ofpbuf_reserve(&copy, DP_NETDEV_HEADROOM);
943         ofpbuf_put(&copy, packet->data, packet->size);
944     } else {
945         /* We still need a shallow copy of 'packet', even though we won't
946          * modify its data, because flow_extract() modifies packet->l2, etc.
947          * We could probably get away with modifying those but it's more polite
948          * if we don't. */
949         copy = *packet;
950     }
951     flow_extract(&copy, 0, -1, &key);
952     error = dp_netdev_execute_actions(dp, &copy, &key, actions, actions_len);
953     if (mutates) {
954         ofpbuf_uninit(&copy);
955     }
956     return error;
957 }
958
959 static int
960 dpif_netdev_recv_get_mask(const struct dpif *dpif, int *listen_mask)
961 {
962     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
963     *listen_mask = dpif_netdev->listen_mask;
964     return 0;
965 }
966
967 static int
968 dpif_netdev_recv_set_mask(struct dpif *dpif, int listen_mask)
969 {
970     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
971     if (!(listen_mask & ~ODPL_ALL)) {
972         dpif_netdev->listen_mask = listen_mask;
973         return 0;
974     } else {
975         return EINVAL;
976     }
977 }
978
979 static struct dp_netdev_queue *
980 find_nonempty_queue(struct dpif *dpif)
981 {
982     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
983     struct dp_netdev *dp = get_dp_netdev(dpif);
984     int mask = dpif_netdev->listen_mask;
985     int i;
986
987     for (i = 0; i < N_QUEUES; i++) {
988         struct dp_netdev_queue *q = &dp->queues[i];
989         if (q->head != q->tail && mask & (1u << i)) {
990             return q;
991         }
992     }
993     return NULL;
994 }
995
996 static int
997 dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall)
998 {
999     struct dp_netdev_queue *q = find_nonempty_queue(dpif);
1000     if (q) {
1001         struct dpif_upcall *u = q->upcalls[q->tail++ & QUEUE_MASK];
1002         *upcall = *u;
1003         free(u);
1004
1005         return 0;
1006     } else {
1007         return EAGAIN;
1008     }
1009 }
1010
1011 static void
1012 dpif_netdev_recv_wait(struct dpif *dpif)
1013 {
1014     if (find_nonempty_queue(dpif)) {
1015         poll_immediate_wake();
1016     } else {
1017         /* No messages ready to be received, and dp_wait() will ensure that we
1018          * wake up to queue new messages, so there is nothing to do. */
1019     }
1020 }
1021
1022 static void
1023 dpif_netdev_recv_purge(struct dpif *dpif)
1024 {
1025     struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
1026     dp_netdev_purge_queues(dpif_netdev->dp);
1027 }
1028 \f
1029 static void
1030 dp_netdev_flow_used(struct dp_netdev_flow *flow, struct flow *key,
1031                     const struct ofpbuf *packet)
1032 {
1033     time_timespec(&flow->used);
1034     flow->packet_count++;
1035     flow->byte_count += packet->size;
1036     if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
1037         struct tcp_header *th = packet->l4;
1038         flow->tcp_ctl |= th->tcp_ctl;
1039     }
1040 }
1041
1042 static void
1043 dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
1044                      struct ofpbuf *packet)
1045 {
1046     struct dp_netdev_flow *flow;
1047     struct flow key;
1048
1049     if (packet->size < ETH_HEADER_LEN) {
1050         return;
1051     }
1052     if (flow_extract(packet, 0, port->port_no, &key) && dp->drop_frags) {
1053         dp->n_frags++;
1054         return;
1055     }
1056
1057     flow = dp_netdev_lookup_flow(dp, &key);
1058     if (flow) {
1059         dp_netdev_flow_used(flow, &key, packet);
1060         dp_netdev_execute_actions(dp, packet, &key,
1061                                   flow->actions, flow->actions_len);
1062         dp->n_hit++;
1063     } else {
1064         dp->n_missed++;
1065         dp_netdev_output_control(dp, packet, _ODPL_MISS_NR, &key, 0);
1066     }
1067 }
1068
1069 static void
1070 dp_netdev_run(void)
1071 {
1072     struct shash_node *node;
1073     struct ofpbuf packet;
1074
1075     ofpbuf_init(&packet, DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + max_mtu);
1076     SHASH_FOR_EACH (node, &dp_netdevs) {
1077         struct dp_netdev *dp = node->data;
1078         struct dp_netdev_port *port;
1079
1080         LIST_FOR_EACH (port, node, &dp->port_list) {
1081             int error;
1082
1083             /* Reset packet contents. */
1084             ofpbuf_clear(&packet);
1085             ofpbuf_reserve(&packet, DP_NETDEV_HEADROOM);
1086
1087             error = netdev_recv(port->netdev, &packet);
1088             if (!error) {
1089                 dp_netdev_port_input(dp, port, &packet);
1090             } else if (error != EAGAIN && error != EOPNOTSUPP) {
1091                 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1092                 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1093                             netdev_get_name(port->netdev), strerror(error));
1094             }
1095         }
1096     }
1097     ofpbuf_uninit(&packet);
1098 }
1099
1100 static void
1101 dp_netdev_wait(void)
1102 {
1103     struct shash_node *node;
1104
1105     SHASH_FOR_EACH (node, &dp_netdevs) {
1106         struct dp_netdev *dp = node->data;
1107         struct dp_netdev_port *port;
1108
1109         LIST_FOR_EACH (port, node, &dp->port_list) {
1110             netdev_recv_wait(port->netdev);
1111         }
1112     }
1113 }
1114
1115
1116 /* Modify the TCI field of 'packet'.  If a VLAN tag is present, its TCI field
1117  * is replaced by 'tci'.  If a VLAN tag is not present, one is added with the
1118  * TCI field set to 'tci'.
1119  */
1120 static void
1121 dp_netdev_set_dl_tci(struct ofpbuf *packet, uint16_t tci)
1122 {
1123     struct vlan_eth_header *veh;
1124     struct eth_header *eh;
1125
1126     eh = packet->l2;
1127     if (packet->size >= sizeof(struct vlan_eth_header)
1128         && eh->eth_type == htons(ETH_TYPE_VLAN)) {
1129         veh = packet->l2;
1130         veh->veth_tci = tci;
1131     } else {
1132         /* Insert new 802.1Q header. */
1133         struct vlan_eth_header tmp;
1134         memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1135         memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1136         tmp.veth_type = htons(ETH_TYPE_VLAN);
1137         tmp.veth_tci = tci;
1138         tmp.veth_next_type = eh->eth_type;
1139
1140         veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1141         memcpy(veh, &tmp, sizeof tmp);
1142         packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1143     }
1144 }
1145
1146 static void
1147 dp_netdev_strip_vlan(struct ofpbuf *packet)
1148 {
1149     struct vlan_eth_header *veh = packet->l2;
1150     if (packet->size >= sizeof *veh
1151         && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1152         struct eth_header tmp;
1153
1154         memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1155         memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1156         tmp.eth_type = veh->veth_next_type;
1157
1158         ofpbuf_pull(packet, VLAN_HEADER_LEN);
1159         packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1160         memcpy(packet->data, &tmp, sizeof tmp);
1161     }
1162 }
1163
1164 static void
1165 dp_netdev_set_dl_src(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1166 {
1167     struct eth_header *eh = packet->l2;
1168     memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1169 }
1170
1171 static void
1172 dp_netdev_set_dl_dst(struct ofpbuf *packet, const uint8_t dl_addr[ETH_ADDR_LEN])
1173 {
1174     struct eth_header *eh = packet->l2;
1175     memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1176 }
1177
1178 static bool
1179 is_ip(const struct ofpbuf *packet, const struct flow *key)
1180 {
1181     return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1182 }
1183
1184 static void
1185 dp_netdev_set_nw_addr(struct ofpbuf *packet, const struct flow *key,
1186                       const struct nlattr *a)
1187 {
1188     if (is_ip(packet, key)) {
1189         struct ip_header *nh = packet->l3;
1190         ovs_be32 ip = nl_attr_get_be32(a);
1191         uint16_t type = nl_attr_type(a);
1192         uint32_t *field;
1193
1194         field = type == ODPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1195         if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1196             struct tcp_header *th = packet->l4;
1197             th->tcp_csum = recalc_csum32(th->tcp_csum, *field, ip);
1198         } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1199             struct udp_header *uh = packet->l4;
1200             if (uh->udp_csum) {
1201                 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, ip);
1202                 if (!uh->udp_csum) {
1203                     uh->udp_csum = 0xffff;
1204                 }
1205             }
1206         }
1207         nh->ip_csum = recalc_csum32(nh->ip_csum, *field, ip);
1208         *field = ip;
1209     }
1210 }
1211
1212 static void
1213 dp_netdev_set_nw_tos(struct ofpbuf *packet, const struct flow *key,
1214                      uint8_t nw_tos)
1215 {
1216     if (is_ip(packet, key)) {
1217         struct ip_header *nh = packet->l3;
1218         uint8_t *field = &nh->ip_tos;
1219
1220         /* Set the DSCP bits and preserve the ECN bits. */
1221         uint8_t new = nw_tos | (nh->ip_tos & IP_ECN_MASK);
1222
1223         nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1224                 htons((uint16_t) new));
1225         *field = new;
1226     }
1227 }
1228
1229 static void
1230 dp_netdev_set_tp_port(struct ofpbuf *packet, const struct flow *key,
1231                       const struct nlattr *a)
1232 {
1233         if (is_ip(packet, key)) {
1234         uint16_t type = nl_attr_type(a);
1235         ovs_be16 port = nl_attr_get_be16(a);
1236         uint16_t *field;
1237
1238         if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1239             struct tcp_header *th = packet->l4;
1240             field = type == ODPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1241             th->tcp_csum = recalc_csum16(th->tcp_csum, *field, port);
1242             *field = port;
1243         } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1244             struct udp_header *uh = packet->l4;
1245             field = type == ODPAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1246             uh->udp_csum = recalc_csum16(uh->udp_csum, *field, port);
1247             *field = port;
1248         } else {
1249             return;
1250         }
1251     }
1252 }
1253
1254 static void
1255 dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
1256                       uint16_t out_port)
1257 {
1258     struct dp_netdev_port *p = dp->ports[out_port];
1259     if (p) {
1260         netdev_send(p->netdev, packet);
1261     }
1262 }
1263
1264 static int
1265 dp_netdev_output_control(struct dp_netdev *dp, const struct ofpbuf *packet,
1266                          int queue_no, const struct flow *flow, uint64_t arg)
1267 {
1268     struct dp_netdev_queue *q = &dp->queues[queue_no];
1269     struct dpif_upcall *upcall;
1270     struct ofpbuf *buf;
1271     size_t key_len;
1272
1273     if (q->head - q->tail >= MAX_QUEUE_LEN) {
1274         dp->n_lost++;
1275         return ENOBUFS;
1276     }
1277
1278     buf = ofpbuf_new(ODPUTIL_FLOW_KEY_BYTES + 2 + packet->size);
1279     odp_flow_key_from_flow(buf, flow);
1280     key_len = buf->size;
1281     ofpbuf_pull(buf, key_len);
1282     ofpbuf_reserve(buf, 2);
1283     ofpbuf_put(buf, packet->data, packet->size);
1284
1285     upcall = xzalloc(sizeof *upcall);
1286     upcall->type = queue_no;
1287     upcall->packet = buf;
1288     upcall->key = buf->base;
1289     upcall->key_len = key_len;
1290     upcall->userdata = arg;
1291
1292     q->upcalls[++q->head & QUEUE_MASK] = upcall;
1293
1294     return 0;
1295 }
1296
1297 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1298  * screwy or truncated header fields or one whose inner and outer Ethernet
1299  * address differ. */
1300 static bool
1301 dp_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct flow *key)
1302 {
1303     struct arp_eth_header *arp;
1304     struct eth_header *eth;
1305     ptrdiff_t l3_size;
1306
1307     if (key->dl_type != htons(ETH_TYPE_ARP)) {
1308         return false;
1309     }
1310
1311     l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1312     if (l3_size < sizeof(struct arp_eth_header)) {
1313         return true;
1314     }
1315
1316     eth = packet->l2;
1317     arp = packet->l3;
1318     return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1319             || arp->ar_pro != htons(ARP_PRO_IP)
1320             || arp->ar_hln != ETH_HEADER_LEN
1321             || arp->ar_pln != 4
1322             || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1323 }
1324
1325 static int
1326 dp_netdev_execute_actions(struct dp_netdev *dp,
1327                           struct ofpbuf *packet, struct flow *key,
1328                           const struct nlattr *actions,
1329                           size_t actions_len)
1330 {
1331     const struct nlattr *a;
1332     unsigned int left;
1333
1334     NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
1335         switch (nl_attr_type(a)) {
1336         case ODPAT_OUTPUT:
1337             dp_netdev_output_port(dp, packet, nl_attr_get_u32(a));
1338             break;
1339
1340         case ODPAT_CONTROLLER:
1341             dp_netdev_output_control(dp, packet, _ODPL_ACTION_NR,
1342                                      key, nl_attr_get_u64(a));
1343             break;
1344
1345         case ODPAT_SET_DL_TCI:
1346             dp_netdev_set_dl_tci(packet, nl_attr_get_be16(a));
1347             break;
1348
1349         case ODPAT_STRIP_VLAN:
1350             dp_netdev_strip_vlan(packet);
1351             break;
1352
1353         case ODPAT_SET_DL_SRC:
1354             dp_netdev_set_dl_src(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1355             break;
1356
1357         case ODPAT_SET_DL_DST:
1358             dp_netdev_set_dl_dst(packet, nl_attr_get_unspec(a, ETH_ADDR_LEN));
1359             break;
1360
1361         case ODPAT_SET_NW_SRC:
1362         case ODPAT_SET_NW_DST:
1363             dp_netdev_set_nw_addr(packet, key, a);
1364             break;
1365
1366         case ODPAT_SET_NW_TOS:
1367             dp_netdev_set_nw_tos(packet, key, nl_attr_get_u8(a));
1368             break;
1369
1370         case ODPAT_SET_TP_SRC:
1371         case ODPAT_SET_TP_DST:
1372             dp_netdev_set_tp_port(packet, key, a);
1373             break;
1374
1375         case ODPAT_DROP_SPOOFED_ARP:
1376             if (dp_netdev_is_spoofed_arp(packet, key)) {
1377                 return 0;
1378             }
1379         }
1380     }
1381     return 0;
1382 }
1383
1384 const struct dpif_class dpif_netdev_class = {
1385     "netdev",
1386     dp_netdev_run,
1387     dp_netdev_wait,
1388     NULL,                       /* enumerate */
1389     dpif_netdev_open,
1390     dpif_netdev_close,
1391     NULL,                       /* get_all_names */
1392     dpif_netdev_destroy,
1393     dpif_netdev_get_stats,
1394     dpif_netdev_get_drop_frags,
1395     dpif_netdev_set_drop_frags,
1396     dpif_netdev_port_add,
1397     dpif_netdev_port_del,
1398     dpif_netdev_port_query_by_number,
1399     dpif_netdev_port_query_by_name,
1400     dpif_netdev_get_max_ports,
1401     dpif_netdev_port_dump_start,
1402     dpif_netdev_port_dump_next,
1403     dpif_netdev_port_dump_done,
1404     dpif_netdev_port_poll,
1405     dpif_netdev_port_poll_wait,
1406     dpif_netdev_flow_get,
1407     dpif_netdev_flow_put,
1408     dpif_netdev_flow_del,
1409     dpif_netdev_flow_flush,
1410     dpif_netdev_flow_dump_start,
1411     dpif_netdev_flow_dump_next,
1412     dpif_netdev_flow_dump_done,
1413     dpif_netdev_execute,
1414     dpif_netdev_recv_get_mask,
1415     dpif_netdev_recv_set_mask,
1416     NULL,                       /* get_sflow_probability */
1417     NULL,                       /* set_sflow_probability */
1418     NULL,                       /* queue_to_priority */
1419     dpif_netdev_recv,
1420     dpif_netdev_recv_wait,
1421     dpif_netdev_recv_purge,
1422 };
1423
1424 void
1425 dpif_dummy_register(void)
1426 {
1427     if (!dpif_dummy_class.type) {
1428         dpif_dummy_class = dpif_netdev_class;
1429         dpif_dummy_class.type = "dummy";
1430         dp_register_provider(&dpif_dummy_class);
1431     }
1432 }