Merge "master" into "wdp".
[sliver-openvswitch.git] / lib / xfif-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 "xfif.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 "flow.h"
36 #include "hmap.h"
37 #include "list.h"
38 #include "netdev.h"
39 #include "xflow-util.h"
40 #include "ofp-print.h"
41 #include "ofpbuf.h"
42 #include "packets.h"
43 #include "poll-loop.h"
44 #include "queue.h"
45 #include "timeval.h"
46 #include "util.h"
47 #include "vlog.h"
48 #include "xfif-provider.h"
49
50 VLOG_DEFINE_THIS_MODULE(xfif_netdev)
51
52 /* Configuration parameters. */
53 enum { N_QUEUES = 2 };          /* Number of queues for xfif_recv(). */
54 enum { MAX_QUEUE_LEN = 100 };   /* Maximum number of packets per queue. */
55 enum { N_GROUPS = 16 };         /* Number of port groups. */
56 enum { MAX_PORTS = 256 };       /* Maximum number of ports. */
57 enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
58
59 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
60  * headers to be aligned on a 4-byte boundary.  */
61 enum { XF_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
62
63 /* Datapath based on the network device interface from netdev.h. */
64 struct xf_netdev {
65     struct list node;
66     int xf_idx;
67     int open_cnt;
68     bool destroyed;
69
70     bool drop_frags;            /* Drop all IP fragments, if true. */
71     struct ovs_queue queues[N_QUEUES]; /* Messages queued for xfif_recv(). */
72     struct hmap flow_table;     /* Flow table. */
73     struct xflow_port_group groups[N_GROUPS];
74
75     /* Statistics. */
76     long long int n_frags;      /* Number of dropped IP fragments. */
77     long long int n_hit;        /* Number of flow table matches. */
78     long long int n_missed;     /* Number of flow table misses. */
79     long long int n_lost;       /* Number of misses not passed to client. */
80
81     /* Ports. */
82     int n_ports;
83     struct xf_netdev_port *ports[MAX_PORTS];
84     struct list port_list;
85     unsigned int serial;
86 };
87
88 /* A port in a netdev-based datapath. */
89 struct xf_netdev_port {
90     int port_no;                /* Index into xf_netdev's 'ports'. */
91     struct list node;           /* Element in xf_netdev's 'port_list'. */
92     struct netdev *netdev;
93     bool internal;              /* Internal port (as XFLOW_PORT_INTERNAL)? */
94 };
95
96 /* A flow in xf_netdev's 'flow_table'. */
97 struct xf_netdev_flow {
98     struct hmap_node node;      /* Element in xf_netdev's 'flow_table'. */
99     struct xflow_key key;
100
101     /* Statistics. */
102     struct timespec used;       /* Last used time. */
103     long long int packet_count; /* Number of packets matched. */
104     long long int byte_count;   /* Number of bytes matched. */
105     uint16_t tcp_ctl;           /* Bitwise-OR of seen tcp_ctl values. */
106
107     /* Actions. */
108     union xflow_action *actions;
109     unsigned int n_actions;
110 };
111
112 /* Interface to netdev-based datapath. */
113 struct xfif_netdev {
114     struct xfif xfif;
115     struct xf_netdev *xf;
116     int listen_mask;
117     unsigned int xf_serial;
118 };
119
120 /* All netdev-based datapaths. */
121 static struct xf_netdev *xf_netdevs[256];
122 struct list xf_netdev_list = LIST_INITIALIZER(&xf_netdev_list);
123 enum { N_XF_NETDEVS = ARRAY_SIZE(xf_netdevs) };
124
125 /* Maximum port MTU seen so far. */
126 static int max_mtu = ETH_PAYLOAD_MAX;
127
128 static int get_port_by_number(struct xf_netdev *, uint16_t port_no,
129                               struct xf_netdev_port **portp);
130 static int get_port_by_name(struct xf_netdev *, const char *devname,
131                             struct xf_netdev_port **portp);
132 static void xf_netdev_free(struct xf_netdev *);
133 static void xf_netdev_flow_flush(struct xf_netdev *);
134 static int do_add_port(struct xf_netdev *, const char *devname, uint16_t flags,
135                        uint16_t port_no);
136 static int do_del_port(struct xf_netdev *, uint16_t port_no);
137 static int xf_netdev_output_control(struct xf_netdev *, const struct ofpbuf *,
138                                     int queue_no, int port_no, uint32_t arg);
139 static int xf_netdev_execute_actions(struct xf_netdev *,
140                                      struct ofpbuf *, struct xflow_key *,
141                                      const union xflow_action *, int n);
142
143 static struct xfif_netdev *
144 xfif_netdev_cast(const struct xfif *xfif)
145 {
146     xfif_assert_class(xfif, &xfif_netdev_class);
147     return CONTAINER_OF(xfif, struct xfif_netdev, xfif);
148 }
149
150 static struct xf_netdev *
151 get_xf_netdev(const struct xfif *xfif)
152 {
153     return xfif_netdev_cast(xfif)->xf;
154 }
155
156 static int
157 name_to_xf_idx(const char *name)
158 {
159     if (!strncmp(name, "xf", 2) && isdigit((unsigned char)name[2])) {
160         int xf_idx = atoi(name + 2);
161         if (xf_idx >= 0 && xf_idx < N_XF_NETDEVS) {
162             return xf_idx;
163         }
164     }
165     return -1;
166 }
167
168 static struct xf_netdev *
169 find_xf_netdev(const char *name)
170 {
171     int xf_idx;
172     size_t i;
173
174     xf_idx = name_to_xf_idx(name);
175     if (xf_idx >= 0) {
176         return xf_netdevs[xf_idx];
177     }
178
179     for (i = 0; i < N_XF_NETDEVS; i++) {
180         struct xf_netdev *xf = xf_netdevs[i];
181         if (xf) {
182             struct xf_netdev_port *port;
183             if (!get_port_by_name(xf, name, &port)) {
184                 return xf;
185             }
186         }
187     }
188     return NULL;
189 }
190
191 static struct xfif *
192 create_xfif_netdev(struct xf_netdev *xf)
193 {
194     struct xfif_netdev *xfif;
195     char *xfname;
196
197     xf->open_cnt++;
198
199     xfname = xasprintf("xf%d", xf->xf_idx);
200     xfif = xmalloc(sizeof *xfif);
201     xfif_init(&xfif->xfif, &xfif_netdev_class, xfname, xf->xf_idx, xf->xf_idx);
202     xfif->xf = xf;
203     xfif->listen_mask = 0;
204     xfif->xf_serial = xf->serial;
205     free(xfname);
206
207     return &xfif->xfif;
208 }
209
210 static int
211 create_xf_netdev(const char *name, int xf_idx, struct xfif **xfifp)
212 {
213     struct xf_netdev *xf;
214     int error;
215     int i;
216
217     if (xf_netdevs[xf_idx]) {
218         return EBUSY;
219     }
220
221     /* Create datapath. */
222     xf_netdevs[xf_idx] = xf = xzalloc(sizeof *xf);
223     list_push_back(&xf_netdev_list, &xf->node);
224     xf->xf_idx = xf_idx;
225     xf->open_cnt = 0;
226     xf->drop_frags = false;
227     for (i = 0; i < N_QUEUES; i++) {
228         queue_init(&xf->queues[i]);
229     }
230     hmap_init(&xf->flow_table);
231     for (i = 0; i < N_GROUPS; i++) {
232         xf->groups[i].ports = NULL;
233         xf->groups[i].n_ports = 0;
234         xf->groups[i].group = i;
235     }
236     list_init(&xf->port_list);
237     error = do_add_port(xf, name, XFLOW_PORT_INTERNAL, XFLOWP_LOCAL);
238     if (error) {
239         xf_netdev_free(xf);
240         return ENODEV;
241     }
242
243     *xfifp = create_xfif_netdev(xf);
244     return 0;
245 }
246
247 static int
248 xfif_netdev_open(const char *name, const char *type OVS_UNUSED, bool create,
249                  struct xfif **xfifp)
250 {
251     if (create) {
252         if (find_xf_netdev(name)) {
253             return EEXIST;
254         } else {
255             int xf_idx = name_to_xf_idx(name);
256             if (xf_idx >= 0) {
257                 return create_xf_netdev(name, xf_idx, xfifp);
258             } else {
259                 /* Scan for unused xf_idx number. */
260                 for (xf_idx = 0; xf_idx < N_XF_NETDEVS; xf_idx++) {
261                     int error = create_xf_netdev(name, xf_idx, xfifp);
262                     if (error != EBUSY) {
263                         return error;
264                     }
265                 }
266
267                 /* All datapath numbers in use. */
268                 return ENOBUFS;
269             }
270         }
271     } else {
272         struct xf_netdev *xf = find_xf_netdev(name);
273         if (xf) {
274             *xfifp = create_xfif_netdev(xf);
275             return 0;
276         } else {
277             return ENODEV;
278         }
279     }
280 }
281
282 static void
283 xf_netdev_free(struct xf_netdev *xf)
284 {
285     int i;
286
287     xf_netdev_flow_flush(xf);
288     while (xf->n_ports > 0) {
289         struct xf_netdev_port *port = CONTAINER_OF(
290             xf->port_list.next, struct xf_netdev_port, node);
291         do_del_port(xf, port->port_no);
292     }
293     for (i = 0; i < N_QUEUES; i++) {
294         queue_destroy(&xf->queues[i]);
295     }
296     hmap_destroy(&xf->flow_table);
297     for (i = 0; i < N_GROUPS; i++) {
298         free(xf->groups[i].ports);
299     }
300     xf_netdevs[xf->xf_idx] = NULL;
301     list_remove(&xf->node);
302     free(xf);
303 }
304
305 static void
306 xfif_netdev_close(struct xfif *xfif)
307 {
308     struct xf_netdev *xf = get_xf_netdev(xfif);
309     assert(xf->open_cnt > 0);
310     if (--xf->open_cnt == 0 && xf->destroyed) {
311         xf_netdev_free(xf);
312     }
313     free(xfif);
314 }
315
316 static int
317 xfif_netdev_destroy(struct xfif *xfif)
318 {
319     struct xf_netdev *xf = get_xf_netdev(xfif);
320     xf->destroyed = true;
321     return 0;
322 }
323
324 static int
325 xfif_netdev_get_stats(const struct xfif *xfif, struct xflow_stats *stats)
326 {
327     struct xf_netdev *xf = get_xf_netdev(xfif);
328     memset(stats, 0, sizeof *stats);
329     stats->n_flows = hmap_count(&xf->flow_table);
330     stats->cur_capacity = hmap_capacity(&xf->flow_table);
331     stats->max_capacity = MAX_FLOWS;
332     stats->n_ports = xf->n_ports;
333     stats->max_ports = MAX_PORTS;
334     stats->max_groups = N_GROUPS;
335     stats->n_frags = xf->n_frags;
336     stats->n_hit = xf->n_hit;
337     stats->n_missed = xf->n_missed;
338     stats->n_lost = xf->n_lost;
339     stats->max_miss_queue = MAX_QUEUE_LEN;
340     stats->max_action_queue = MAX_QUEUE_LEN;
341     return 0;
342 }
343
344 static int
345 xfif_netdev_get_drop_frags(const struct xfif *xfif, bool *drop_fragsp)
346 {
347     struct xf_netdev *xf = get_xf_netdev(xfif);
348     *drop_fragsp = xf->drop_frags;
349     return 0;
350 }
351
352 static int
353 xfif_netdev_set_drop_frags(struct xfif *xfif, bool drop_frags)
354 {
355     struct xf_netdev *xf = get_xf_netdev(xfif);
356     xf->drop_frags = drop_frags;
357     return 0;
358 }
359
360 static int
361 do_add_port(struct xf_netdev *xf, const char *devname, uint16_t flags,
362             uint16_t port_no)
363 {
364     bool internal = (flags & XFLOW_PORT_INTERNAL) != 0;
365     struct xf_netdev_port *port;
366     struct netdev_options netdev_options;
367     struct netdev *netdev;
368     int mtu;
369     int error;
370
371     /* XXX reject devices already in some xf_netdev. */
372
373     /* Open and validate network device. */
374     memset(&netdev_options, 0, sizeof netdev_options);
375     netdev_options.name = devname;
376     netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
377     if (internal) {
378         netdev_options.type = "tap";
379     }
380
381     error = netdev_open(&netdev_options, &netdev);
382     if (error) {
383         return error;
384     }
385     /* XXX reject loopback devices */
386     /* XXX reject non-Ethernet devices */
387
388     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
389     if (error) {
390         netdev_close(netdev);
391         return error;
392     }
393
394     port = xmalloc(sizeof *port);
395     port->port_no = port_no;
396     port->netdev = netdev;
397     port->internal = internal;
398
399     netdev_get_mtu(netdev, &mtu);
400     if (mtu > max_mtu) {
401         max_mtu = mtu;
402     }
403
404     list_push_back(&xf->port_list, &port->node);
405     xf->ports[port_no] = port;
406     xf->n_ports++;
407     xf->serial++;
408
409     return 0;
410 }
411
412 static int
413 xfif_netdev_port_add(struct xfif *xfif, const char *devname, uint16_t flags,
414                      uint16_t *port_nop)
415 {
416     struct xf_netdev *xf = get_xf_netdev(xfif);
417     int port_no;
418
419     for (port_no = 0; port_no < MAX_PORTS; port_no++) {
420         if (!xf->ports[port_no]) {
421             *port_nop = port_no;
422             return do_add_port(xf, devname, flags, port_no);
423         }
424     }
425     return EFBIG;
426 }
427
428 static int
429 xfif_netdev_port_del(struct xfif *xfif, uint16_t port_no)
430 {
431     struct xf_netdev *xf = get_xf_netdev(xfif);
432     return port_no == XFLOWP_LOCAL ? EINVAL : do_del_port(xf, port_no);
433 }
434
435 static bool
436 is_valid_port_number(uint16_t port_no)
437 {
438     return port_no < MAX_PORTS;
439 }
440
441 static int
442 get_port_by_number(struct xf_netdev *xf,
443                    uint16_t port_no, struct xf_netdev_port **portp)
444 {
445     if (!is_valid_port_number(port_no)) {
446         *portp = NULL;
447         return EINVAL;
448     } else {
449         *portp = xf->ports[port_no];
450         return *portp ? 0 : ENOENT;
451     }
452 }
453
454 static int
455 get_port_by_name(struct xf_netdev *xf,
456                  const char *devname, struct xf_netdev_port **portp)
457 {
458     struct xf_netdev_port *port;
459
460     LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
461         if (!strcmp(netdev_get_name(port->netdev), devname)) {
462             *portp = port;
463             return 0;
464         }
465     }
466     return ENOENT;
467 }
468
469 static int
470 do_del_port(struct xf_netdev *xf, uint16_t port_no)
471 {
472     struct xf_netdev_port *port;
473     char *name;
474     int error;
475
476     error = get_port_by_number(xf, port_no, &port);
477     if (error) {
478         return error;
479     }
480
481     list_remove(&port->node);
482     xf->ports[port->port_no] = NULL;
483     xf->n_ports--;
484     xf->serial++;
485
486     name = xstrdup(netdev_get_name(port->netdev));
487     netdev_close(port->netdev);
488
489     free(name);
490     free(port);
491
492     return 0;
493 }
494
495 static void
496 answer_port_query(const struct xf_netdev_port *port, struct xflow_port *xflow_port)
497 {
498     memset(xflow_port, 0, sizeof *xflow_port);
499     ovs_strlcpy(xflow_port->devname, netdev_get_name(port->netdev),
500                 sizeof xflow_port->devname);
501     xflow_port->port = port->port_no;
502     xflow_port->flags = port->internal ? XFLOW_PORT_INTERNAL : 0;
503 }
504
505 static int
506 xfif_netdev_port_query_by_number(const struct xfif *xfif, uint16_t port_no,
507                                  struct xflow_port *xflow_port)
508 {
509     struct xf_netdev *xf = get_xf_netdev(xfif);
510     struct xf_netdev_port *port;
511     int error;
512
513     error = get_port_by_number(xf, port_no, &port);
514     if (!error) {
515         answer_port_query(port, xflow_port);
516     }
517     return error;
518 }
519
520 static int
521 xfif_netdev_port_query_by_name(const struct xfif *xfif, const char *devname,
522                                struct xflow_port *xflow_port)
523 {
524     struct xf_netdev *xf = get_xf_netdev(xfif);
525     struct xf_netdev_port *port;
526     int error;
527
528     error = get_port_by_name(xf, devname, &port);
529     if (!error) {
530         answer_port_query(port, xflow_port);
531     }
532     return error;
533 }
534
535 static void
536 xf_netdev_free_flow(struct xf_netdev *xf, struct xf_netdev_flow *flow)
537 {
538     hmap_remove(&xf->flow_table, &flow->node);
539     free(flow->actions);
540     free(flow);
541 }
542
543 static void
544 xf_netdev_flow_flush(struct xf_netdev *xf)
545 {
546     struct xf_netdev_flow *flow, *next;
547
548     HMAP_FOR_EACH_SAFE (flow, next, struct xf_netdev_flow, node,
549                         &xf->flow_table) {
550         xf_netdev_free_flow(xf, flow);
551     }
552 }
553
554 static int
555 xfif_netdev_flow_flush(struct xfif *xfif)
556 {
557     struct xf_netdev *xf = get_xf_netdev(xfif);
558     xf_netdev_flow_flush(xf);
559     return 0;
560 }
561
562 static int
563 xfif_netdev_port_list(const struct xfif *xfif, struct xflow_port *ports, int n)
564 {
565     struct xf_netdev *xf = get_xf_netdev(xfif);
566     struct xf_netdev_port *port;
567     int i;
568
569     i = 0;
570     LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
571         struct xflow_port *xflow_port = &ports[i];
572         if (i >= n) {
573             break;
574         }
575         answer_port_query(port, xflow_port);
576         i++;
577     }
578     return xf->n_ports;
579 }
580
581 static int
582 xfif_netdev_port_poll(const struct xfif *xfif_, char **devnamep OVS_UNUSED)
583 {
584     struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
585     if (xfif->xf_serial != xfif->xf->serial) {
586         xfif->xf_serial = xfif->xf->serial;
587         return ENOBUFS;
588     } else {
589         return EAGAIN;
590     }
591 }
592
593 static void
594 xfif_netdev_port_poll_wait(const struct xfif *xfif_)
595 {
596     struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
597     if (xfif->xf_serial != xfif->xf->serial) {
598         poll_immediate_wake();
599     }
600 }
601
602 static int
603 get_port_group(const struct xfif *xfif, int group_no,
604                struct xflow_port_group **groupp)
605 {
606     struct xf_netdev *xf = get_xf_netdev(xfif);
607
608     if (group_no >= 0 && group_no < N_GROUPS) {
609         *groupp = &xf->groups[group_no];
610         return 0;
611     } else {
612         *groupp = NULL;
613         return EINVAL;
614     }
615 }
616
617 static int
618 xfif_netdev_port_group_get(const struct xfif *xfif, int group_no,
619                            uint16_t ports[], int n)
620 {
621     struct xflow_port_group *group;
622     int error;
623
624     if (n < 0) {
625         return -EINVAL;
626     }
627
628     error = get_port_group(xfif, group_no, &group);
629     if (!error) {
630         memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
631         return group->n_ports;
632     } else {
633         return -error;
634     }
635 }
636
637 static int
638 xfif_netdev_port_group_set(struct xfif *xfif, int group_no,
639                            const uint16_t ports[], int n)
640 {
641     struct xflow_port_group *group;
642     int error;
643
644     if (n < 0 || n > MAX_PORTS) {
645         return EINVAL;
646     }
647
648     error = get_port_group(xfif, group_no, &group);
649     if (!error) {
650         free(group->ports);
651         group->ports = xmemdup(ports, n * sizeof *group->ports);
652         group->n_ports = n;
653         group->group = group_no;
654     }
655     return error;
656 }
657
658 static struct xf_netdev_flow *
659 xf_netdev_lookup_flow(const struct xf_netdev *xf,
660                       const struct xflow_key *key)
661 {
662     struct xf_netdev_flow *flow;
663
664     HMAP_FOR_EACH_WITH_HASH (flow, struct xf_netdev_flow, node,
665                              xflow_key_hash(key, 0), &xf->flow_table) {
666         if (xflow_key_equal(&flow->key, key)) {
667             return flow;
668         }
669     }
670     return NULL;
671 }
672
673 static void
674 answer_flow_query(struct xf_netdev_flow *flow, uint32_t query_flags,
675                   struct xflow_flow *xflow_flow)
676 {
677     if (flow) {
678         xflow_flow->key = flow->key;
679         xflow_flow->stats.n_packets = flow->packet_count;
680         xflow_flow->stats.n_bytes = flow->byte_count;
681         xflow_flow->stats.used_sec = flow->used.tv_sec;
682         xflow_flow->stats.used_nsec = flow->used.tv_nsec;
683         xflow_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
684         xflow_flow->stats.reserved = 0;
685         xflow_flow->stats.error = 0;
686         if (xflow_flow->n_actions > 0) {
687             unsigned int n = MIN(xflow_flow->n_actions, flow->n_actions);
688             memcpy(xflow_flow->actions, flow->actions,
689                    n * sizeof *xflow_flow->actions);
690             xflow_flow->n_actions = flow->n_actions;
691         }
692
693         if (query_flags & XFLOWFF_ZERO_TCP_FLAGS) {
694             flow->tcp_ctl = 0;
695         }
696
697     } else {
698         xflow_flow->stats.error = ENOENT;
699     }
700 }
701
702 static int
703 xfif_netdev_flow_get(const struct xfif *xfif, struct xflow_flow flows[], int n)
704 {
705     struct xf_netdev *xf = get_xf_netdev(xfif);
706     int i;
707
708     for (i = 0; i < n; i++) {
709         struct xflow_flow *xflow_flow = &flows[i];
710         answer_flow_query(xf_netdev_lookup_flow(xf, &xflow_flow->key),
711                           xflow_flow->flags, xflow_flow);
712     }
713     return 0;
714 }
715
716 static int
717 xfif_netdev_validate_actions(const union xflow_action *actions, int n_actions,
718                              bool *mutates)
719 {
720     unsigned int i;
721
722     *mutates = false;
723     for (i = 0; i < n_actions; i++) {
724         const union xflow_action *a = &actions[i];
725         switch (a->type) {
726         case XFLOWAT_OUTPUT:
727             if (a->output.port >= MAX_PORTS) {
728                 return EINVAL;
729             }
730             break;
731
732         case XFLOWAT_OUTPUT_GROUP:
733             *mutates = true;
734             if (a->output_group.group >= N_GROUPS) {
735                 return EINVAL;
736             }
737             break;
738
739         case XFLOWAT_CONTROLLER:
740             break;
741
742         case XFLOWAT_SET_DL_TCI:
743             *mutates = true;
744             if (a->dl_tci.mask != htons(VLAN_VID_MASK)
745                 && a->dl_tci.mask != htons(VLAN_PCP_MASK)
746                 && a->dl_tci.mask != htons(VLAN_VID_MASK | VLAN_PCP_MASK)) {
747                 return EINVAL;
748             }
749             if (a->dl_tci.tci & ~a->dl_tci.mask){
750                 return EINVAL;
751             }
752             break;
753
754         case XFLOWAT_SET_NW_TOS:
755             *mutates = true;
756             if (a->nw_tos.nw_tos & IP_ECN_MASK) {
757                 return EINVAL;
758             }
759             break;
760
761         case XFLOWAT_STRIP_VLAN:
762         case XFLOWAT_SET_DL_SRC:
763         case XFLOWAT_SET_DL_DST:
764         case XFLOWAT_SET_NW_SRC:
765         case XFLOWAT_SET_NW_DST:
766         case XFLOWAT_SET_TP_SRC:
767         case XFLOWAT_SET_TP_DST:
768             *mutates = true;
769             break;
770
771         default:
772             return EOPNOTSUPP;
773         }
774     }
775     return 0;
776 }
777
778 static int
779 set_flow_actions(struct xf_netdev_flow *flow, struct xflow_flow *xflow_flow)
780 {
781     size_t n_bytes;
782     bool mutates;
783     int error;
784
785     if (xflow_flow->n_actions >= 4096 / sizeof *xflow_flow->actions) {
786         return EINVAL;
787     }
788     error = xfif_netdev_validate_actions(xflow_flow->actions,
789                                          xflow_flow->n_actions, &mutates);
790     if (error) {
791         return error;
792     }
793
794     n_bytes = xflow_flow->n_actions * sizeof *flow->actions;
795     flow->actions = xrealloc(flow->actions, n_bytes);
796     flow->n_actions = xflow_flow->n_actions;
797     memcpy(flow->actions, xflow_flow->actions, n_bytes);
798     return 0;
799 }
800
801 static int
802 add_flow(struct xfif *xfif, struct xflow_flow *xflow_flow)
803 {
804     struct xf_netdev *xf = get_xf_netdev(xfif);
805     struct xf_netdev_flow *flow;
806     int error;
807
808     flow = xzalloc(sizeof *flow);
809     flow->key = xflow_flow->key;
810
811     error = set_flow_actions(flow, xflow_flow);
812     if (error) {
813         free(flow);
814         return error;
815     }
816
817     hmap_insert(&xf->flow_table, &flow->node,
818                 xflow_key_hash(&flow->key, 0));
819     return 0;
820 }
821
822 static void
823 clear_stats(struct xf_netdev_flow *flow)
824 {
825     flow->used.tv_sec = 0;
826     flow->used.tv_nsec = 0;
827     flow->packet_count = 0;
828     flow->byte_count = 0;
829     flow->tcp_ctl = 0;
830 }
831
832 static int
833 xfif_netdev_flow_put(struct xfif *xfif, struct xflow_flow_put *put)
834 {
835     struct xf_netdev *xf = get_xf_netdev(xfif);
836     struct xf_netdev_flow *flow;
837
838     flow = xf_netdev_lookup_flow(xf, &put->flow.key);
839     if (!flow) {
840         if (put->flags & XFLOWPF_CREATE) {
841             if (hmap_count(&xf->flow_table) < MAX_FLOWS) {
842                 return add_flow(xfif, &put->flow);
843             } else {
844                 return EFBIG;
845             }
846         } else {
847             return ENOENT;
848         }
849     } else {
850         if (put->flags & XFLOWPF_MODIFY) {
851             int error = set_flow_actions(flow, &put->flow);
852             if (!error && put->flags & XFLOWPF_ZERO_STATS) {
853                 clear_stats(flow);
854             }
855             return error;
856         } else {
857             return EEXIST;
858         }
859     }
860 }
861
862
863 static int
864 xfif_netdev_flow_del(struct xfif *xfif, struct xflow_flow *xflow_flow)
865 {
866     struct xf_netdev *xf = get_xf_netdev(xfif);
867     struct xf_netdev_flow *flow;
868
869     flow = xf_netdev_lookup_flow(xf, &xflow_flow->key);
870     if (flow) {
871         answer_flow_query(flow, 0, xflow_flow);
872         xf_netdev_free_flow(xf, flow);
873         return 0;
874     } else {
875         return ENOENT;
876     }
877 }
878
879 static int
880 xfif_netdev_flow_list(const struct xfif *xfif, struct xflow_flow flows[], int n)
881 {
882     struct xf_netdev *xf = get_xf_netdev(xfif);
883     struct xf_netdev_flow *flow;
884     int i;
885
886     i = 0;
887     HMAP_FOR_EACH (flow, struct xf_netdev_flow, node, &xf->flow_table) {
888         if (i >= n) {
889             break;
890         }
891         answer_flow_query(flow, 0, &flows[i++]);
892     }
893     return hmap_count(&xf->flow_table);
894 }
895
896 static int
897 xfif_netdev_execute(struct xfif *xfif, uint16_t in_port,
898                     const union xflow_action actions[], int n_actions,
899                     const struct ofpbuf *packet)
900 {
901     struct xf_netdev *xf = get_xf_netdev(xfif);
902     struct ofpbuf copy;
903     bool mutates;
904     struct xflow_key key;
905     flow_t flow;
906     int error;
907
908     if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
909         return EINVAL;
910     }
911
912     error = xfif_netdev_validate_actions(actions, n_actions, &mutates);
913     if (error) {
914         return error;
915     }
916
917     if (mutates) {
918         /* We need a deep copy of 'packet' since we're going to modify its
919          * data. */
920         ofpbuf_init(&copy, XF_NETDEV_HEADROOM + packet->size);
921         copy.data = (char*)copy.base + XF_NETDEV_HEADROOM;
922         ofpbuf_put(&copy, packet->data, packet->size);
923     } else {
924         /* We still need a shallow copy of 'packet', even though we won't
925          * modify its data, because flow_extract() modifies packet->l2, etc.
926          * We could probably get away with modifying those but it's more polite
927          * if we don't. */
928         copy = *packet;
929     }
930     flow_extract(&copy, 0, in_port, &flow);
931     xflow_key_from_flow(&key, &flow);
932     error = xf_netdev_execute_actions(xf, &copy, &key, actions, n_actions);
933     if (mutates) {
934         ofpbuf_uninit(&copy);
935     }
936     return error;
937 }
938
939 static int
940 xfif_netdev_recv_get_mask(const struct xfif *xfif, int *listen_mask)
941 {
942     struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
943     *listen_mask = xfif_netdev->listen_mask;
944     return 0;
945 }
946
947 static int
948 xfif_netdev_recv_set_mask(struct xfif *xfif, int listen_mask)
949 {
950     struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
951     if (!(listen_mask & ~XFLOWL_ALL)) {
952         xfif_netdev->listen_mask = listen_mask;
953         return 0;
954     } else {
955         return EINVAL;
956     }
957 }
958
959 static struct ovs_queue *
960 find_nonempty_queue(struct xfif *xfif)
961 {
962     struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
963     struct xf_netdev *xf = get_xf_netdev(xfif);
964     int mask = xfif_netdev->listen_mask;
965     int i;
966
967     for (i = 0; i < N_QUEUES; i++) {
968         struct ovs_queue *q = &xf->queues[i];
969         if (q->n && mask & (1u << i)) {
970             return q;
971         }
972     }
973     return NULL;
974 }
975
976 static int
977 xfif_netdev_recv(struct xfif *xfif, struct ofpbuf **bufp)
978 {
979     struct ovs_queue *q = find_nonempty_queue(xfif);
980     if (q) {
981         *bufp = queue_pop_head(q);
982         return 0;
983     } else {
984         return EAGAIN;
985     }
986 }
987
988 static void
989 xfif_netdev_recv_wait(struct xfif *xfif)
990 {
991     struct ovs_queue *q = find_nonempty_queue(xfif);
992     if (q) {
993         poll_immediate_wake();
994     } else {
995         /* No messages ready to be received, and xf_wait() will ensure that we
996          * wake up to queue new messages, so there is nothing to do. */
997     }
998 }
999 \f
1000 static void
1001 xf_netdev_flow_used(struct xf_netdev_flow *flow,
1002                     const struct xflow_key *key,
1003                     const struct ofpbuf *packet)
1004 {
1005     time_timespec(&flow->used);
1006     flow->packet_count++;
1007     flow->byte_count += packet->size;
1008     if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
1009         struct tcp_header *th = packet->l4;
1010         flow->tcp_ctl |= th->tcp_ctl;
1011     }
1012 }
1013
1014 static void
1015 xf_netdev_port_input(struct xf_netdev *xf, struct xf_netdev_port *port,
1016                      struct ofpbuf *packet)
1017 {
1018     struct xf_netdev_flow *flow;
1019     struct xflow_key key;
1020     flow_t f;
1021
1022     if (flow_extract(packet, 0, port->port_no, &f) && xf->drop_frags) {
1023         xf->n_frags++;
1024         return;
1025     }
1026     xflow_key_from_flow(&key, &f);
1027
1028     flow = xf_netdev_lookup_flow(xf, &key);
1029     if (flow) {
1030         xf_netdev_flow_used(flow, &key, packet);
1031         xf_netdev_execute_actions(xf, packet, &key,
1032                                   flow->actions, flow->n_actions);
1033         xf->n_hit++;
1034     } else {
1035         xf->n_missed++;
1036         xf_netdev_output_control(xf, packet, _XFLOWL_MISS_NR, port->port_no, 0);
1037     }
1038 }
1039
1040 static void
1041 xf_netdev_run(void)
1042 {
1043     struct ofpbuf packet;
1044     struct xf_netdev *xf;
1045
1046     ofpbuf_init(&packet, XF_NETDEV_HEADROOM + max_mtu);
1047     LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1048         struct xf_netdev_port *port;
1049
1050         LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1051             int error;
1052
1053             /* Reset packet contents. */
1054             packet.data = (char*)packet.base + XF_NETDEV_HEADROOM;
1055             packet.size = 0;
1056
1057             error = netdev_recv(port->netdev, &packet);
1058             if (!error) {
1059                 xf_netdev_port_input(xf, port, &packet);
1060             } else if (error != EAGAIN) {
1061                 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1062                 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1063                             netdev_get_name(port->netdev), strerror(error));
1064             }
1065         }
1066     }
1067     ofpbuf_uninit(&packet);
1068 }
1069
1070 static void
1071 xf_netdev_wait(void)
1072 {
1073     struct xf_netdev *xf;
1074
1075     LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1076         struct xf_netdev_port *port;
1077         LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1078             netdev_recv_wait(port->netdev);
1079         }
1080     }
1081 }
1082
1083
1084 /* Modify or add a 802.1Q header in 'packet' according to 'a'. */
1085 static void
1086 xf_netdev_set_dl_tci(struct ofpbuf *packet, struct xflow_key *key,
1087                      const struct xflow_action_dl_tci *a)
1088 {
1089     struct vlan_eth_header *veh;
1090
1091     if (key->dl_tci) {
1092         veh = packet->l2;
1093         veh->veth_tci = (veh->veth_tci & ~a->mask) | a->tci;
1094     } else {
1095         /* Insert new 802.1Q header. */
1096         struct eth_header *eh = packet->l2;
1097         struct vlan_eth_header tmp;
1098         memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1099         memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1100         tmp.veth_type = htons(ETH_TYPE_VLAN);
1101         tmp.veth_tci = htons(a->tci);
1102         tmp.veth_next_type = eh->eth_type;
1103
1104         veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1105         memcpy(veh, &tmp, sizeof tmp);
1106         packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1107     }
1108
1109     key->dl_tci = veh->veth_tci | htons(XFLOW_TCI_PRESENT);
1110 }
1111
1112 static void
1113 xf_netdev_strip_vlan(struct ofpbuf *packet, struct xflow_key *key)
1114 {
1115     struct vlan_eth_header *veh = packet->l2;
1116     if (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         packet->size -= VLAN_HEADER_LEN;
1124         packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1125         packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1126         memcpy(packet->data, &tmp, sizeof tmp);
1127
1128         key->dl_tci = htons(0);
1129     }
1130 }
1131
1132 static void
1133 xf_netdev_set_dl_src(struct ofpbuf *packet,
1134                      const uint8_t dl_addr[ETH_ADDR_LEN])
1135 {
1136     struct eth_header *eh = packet->l2;
1137     memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1138 }
1139
1140 static void
1141 xf_netdev_set_dl_dst(struct ofpbuf *packet,
1142                      const uint8_t dl_addr[ETH_ADDR_LEN])
1143 {
1144     struct eth_header *eh = packet->l2;
1145     memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1146 }
1147
1148 static void
1149 xf_netdev_set_nw_addr(struct ofpbuf *packet, const struct xflow_key *key,
1150                       const struct xflow_action_nw_addr *a)
1151 {
1152     if (key->dl_type == htons(ETH_TYPE_IP)) {
1153         struct ip_header *nh = packet->l3;
1154         uint32_t *field;
1155
1156         field = a->type == XFLOWAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1157         if (key->nw_proto == IP_TYPE_TCP) {
1158             struct tcp_header *th = packet->l4;
1159             th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1160         } else if (key->nw_proto == IP_TYPE_UDP) {
1161             struct udp_header *uh = packet->l4;
1162             if (uh->udp_csum) {
1163                 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1164                 if (!uh->udp_csum) {
1165                     uh->udp_csum = 0xffff;
1166                 }
1167             }
1168         }
1169         nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1170         *field = a->nw_addr;
1171     }
1172 }
1173
1174 static void
1175 xf_netdev_set_nw_tos(struct ofpbuf *packet, const struct xflow_key *key,
1176                      const struct xflow_action_nw_tos *a)
1177 {
1178     if (key->dl_type == htons(ETH_TYPE_IP)) {
1179         struct ip_header *nh = packet->l3;
1180         uint8_t *field = &nh->ip_tos;
1181
1182         /* Set the DSCP bits and preserve the ECN bits. */
1183         uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1184
1185         nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1186                 htons((uint16_t)a->nw_tos));
1187         *field = new;
1188     }
1189 }
1190
1191 static void
1192 xf_netdev_set_tp_port(struct ofpbuf *packet, const struct xflow_key *key,
1193                       const struct xflow_action_tp_port *a)
1194 {
1195     if (key->dl_type == htons(ETH_TYPE_IP)) {
1196         uint16_t *field;
1197         if (key->nw_proto == IPPROTO_TCP) {
1198             struct tcp_header *th = packet->l4;
1199             field = a->type == XFLOWAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1200             th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1201             *field = a->tp_port;
1202         } else if (key->nw_proto == IPPROTO_UDP) {
1203             struct udp_header *uh = packet->l4;
1204             field = a->type == XFLOWAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1205             uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1206             *field = a->tp_port;
1207         } else {
1208             return;
1209         }
1210     }
1211 }
1212
1213 static void
1214 xf_netdev_output_port(struct xf_netdev *xf, struct ofpbuf *packet,
1215                       uint16_t out_port)
1216 {
1217     struct xf_netdev_port *p = xf->ports[out_port];
1218     if (p) {
1219         netdev_send(p->netdev, packet);
1220     }
1221 }
1222
1223 static void
1224 xf_netdev_output_group(struct xf_netdev *xf, uint16_t group, uint16_t in_port,
1225                        struct ofpbuf *packet)
1226 {
1227     struct xflow_port_group *g = &xf->groups[group];
1228     int i;
1229
1230     for (i = 0; i < g->n_ports; i++) {
1231         uint16_t out_port = g->ports[i];
1232         if (out_port != in_port) {
1233             xf_netdev_output_port(xf, packet, out_port);
1234         }
1235     }
1236 }
1237
1238 static int
1239 xf_netdev_output_control(struct xf_netdev *xf, const struct ofpbuf *packet,
1240                          int queue_no, int port_no, uint32_t arg)
1241 {
1242     struct ovs_queue *q = &xf->queues[queue_no];
1243     struct xflow_msg *header;
1244     struct ofpbuf *msg;
1245     size_t msg_size;
1246
1247     if (q->n >= MAX_QUEUE_LEN) {
1248         xf->n_lost++;
1249         return ENOBUFS;
1250     }
1251
1252     msg_size = sizeof *header + packet->size;
1253     msg = ofpbuf_new(msg_size + XFIF_RECV_MSG_PADDING);
1254     header = ofpbuf_put_uninit(msg, sizeof *header);
1255     ofpbuf_reserve(msg, XFIF_RECV_MSG_PADDING);
1256     header->type = queue_no;
1257     header->length = msg_size;
1258     header->port = port_no;
1259     header->arg = arg;
1260     ofpbuf_put(msg, packet->data, packet->size);
1261     queue_push_tail(q, msg);
1262
1263     return 0;
1264 }
1265
1266 static int
1267 xf_netdev_execute_actions(struct xf_netdev *xf,
1268                           struct ofpbuf *packet, struct xflow_key *key,
1269                           const union xflow_action *actions, int n_actions)
1270 {
1271     int i;
1272     for (i = 0; i < n_actions; i++) {
1273         const union xflow_action *a = &actions[i];
1274
1275         switch (a->type) {
1276         case XFLOWAT_OUTPUT:
1277             xf_netdev_output_port(xf, packet, a->output.port);
1278             break;
1279
1280         case XFLOWAT_OUTPUT_GROUP:
1281             xf_netdev_output_group(xf, a->output_group.group, key->in_port,
1282                                    packet);
1283             break;
1284
1285         case XFLOWAT_CONTROLLER:
1286             xf_netdev_output_control(xf, packet, _XFLOWL_ACTION_NR,
1287                                      key->in_port, a->controller.arg);
1288             break;
1289
1290         case XFLOWAT_SET_DL_TCI:
1291             xf_netdev_set_dl_tci(packet, key, &a->dl_tci);
1292             break;
1293
1294         case XFLOWAT_STRIP_VLAN:
1295             xf_netdev_strip_vlan(packet, key);
1296             break;
1297
1298         case XFLOWAT_SET_DL_SRC:
1299             xf_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1300             break;
1301
1302         case XFLOWAT_SET_DL_DST:
1303             xf_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1304             break;
1305
1306         case XFLOWAT_SET_NW_SRC:
1307         case XFLOWAT_SET_NW_DST:
1308             xf_netdev_set_nw_addr(packet, key, &a->nw_addr);
1309             break;
1310
1311         case XFLOWAT_SET_NW_TOS:
1312             xf_netdev_set_nw_tos(packet, key, &a->nw_tos);
1313             break;
1314
1315         case XFLOWAT_SET_TP_SRC:
1316         case XFLOWAT_SET_TP_DST:
1317             xf_netdev_set_tp_port(packet, key, &a->tp_port);
1318             break;
1319         }
1320     }
1321     return 0;
1322 }
1323
1324 const struct xfif_class xfif_netdev_class = {
1325     "netdev",
1326     xf_netdev_run,
1327     xf_netdev_wait,
1328     NULL,                       /* enumerate */
1329     xfif_netdev_open,
1330     xfif_netdev_close,
1331     NULL,                       /* get_all_names */
1332     xfif_netdev_destroy,
1333     xfif_netdev_get_stats,
1334     xfif_netdev_get_drop_frags,
1335     xfif_netdev_set_drop_frags,
1336     xfif_netdev_port_add,
1337     xfif_netdev_port_del,
1338     xfif_netdev_port_query_by_number,
1339     xfif_netdev_port_query_by_name,
1340     xfif_netdev_port_list,
1341     xfif_netdev_port_poll,
1342     xfif_netdev_port_poll_wait,
1343     xfif_netdev_port_group_get,
1344     xfif_netdev_port_group_set,
1345     xfif_netdev_flow_get,
1346     xfif_netdev_flow_put,
1347     xfif_netdev_flow_del,
1348     xfif_netdev_flow_flush,
1349     xfif_netdev_flow_list,
1350     xfif_netdev_execute,
1351     xfif_netdev_recv_get_mask,
1352     xfif_netdev_recv_set_mask,
1353     NULL,                       /* get_sflow_probability */
1354     NULL,                       /* set_sflow_probability */
1355     NULL,                       /* queue_to_priority */
1356     xfif_netdev_recv,
1357     xfif_netdev_recv_wait,
1358 };