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 <net/if.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #include "csum.h"
34 #include "flow.h"
35 #include "hmap.h"
36 #include "list.h"
37 #include "netdev.h"
38 #include "xflow-util.h"
39 #include "ofp-print.h"
40 #include "ofpbuf.h"
41 #include "packets.h"
42 #include "poll-loop.h"
43 #include "queue.h"
44 #include "timeval.h"
45 #include "util.h"
46 #include "xfif-provider.h"
47
48 #include "vlog.h"
49 #define THIS_MODULE VLM_xfif_netdev
50
51 /* Configuration parameters. */
52 enum { N_QUEUES = 2 };          /* Number of queues for xfif_recv(). */
53 enum { MAX_QUEUE_LEN = 100 };   /* Maximum number of packets per queue. */
54 enum { N_GROUPS = 16 };         /* Number of port groups. */
55 enum { MAX_PORTS = 256 };       /* Maximum number of ports. */
56 enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
57
58 /* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
59  * headers to be aligned on a 4-byte boundary.  */
60 enum { XF_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
61
62 /* Datapath based on the network device interface from netdev.h. */
63 struct xf_netdev {
64     struct list node;
65     int xf_idx;
66     int open_cnt;
67     bool destroyed;
68
69     bool drop_frags;            /* Drop all IP fragments, if true. */
70     struct ovs_queue queues[N_QUEUES]; /* Messages queued for xfif_recv(). */
71     struct hmap flow_table;     /* Flow table. */
72     struct xflow_port_group groups[N_GROUPS];
73
74     /* Statistics. */
75     long long int n_frags;      /* Number of dropped IP fragments. */
76     long long int n_hit;        /* Number of flow table matches. */
77     long long int n_missed;     /* Number of flow table misses. */
78     long long int n_lost;       /* Number of misses not passed to client. */
79
80     /* Ports. */
81     int n_ports;
82     struct xf_netdev_port *ports[MAX_PORTS];
83     struct list port_list;
84     unsigned int serial;
85 };
86
87 /* A port in a netdev-based datapath. */
88 struct xf_netdev_port {
89     int port_no;                /* Index into xf_netdev's 'ports'. */
90     struct list node;           /* Element in xf_netdev's 'port_list'. */
91     struct netdev *netdev;
92     bool internal;              /* Internal port (as XFLOW_PORT_INTERNAL)? */
93 };
94
95 /* A flow in xf_netdev's 'flow_table'. */
96 struct xf_netdev_flow {
97     struct hmap_node node;      /* Element in xf_netdev's 'flow_table'. */
98     struct xflow_key key;
99
100     /* Statistics. */
101     struct timeval used;        /* Last used time, in milliseconds. */
102     long long int packet_count; /* Number of packets matched. */
103     long long int byte_count;   /* Number of bytes matched. */
104     uint8_t ip_tos;             /* IP TOS value. */
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     netdev_options.may_create = true;
378     if (internal) {
379         netdev_options.type = "tap";
380     } else {
381         netdev_options.may_open = true;
382     }
383
384     error = netdev_open(&netdev_options, &netdev);
385     if (error) {
386         return error;
387     }
388     /* XXX reject loopback devices */
389     /* XXX reject non-Ethernet devices */
390
391     error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
392     if (error) {
393         netdev_close(netdev);
394         return error;
395     }
396
397     port = xmalloc(sizeof *port);
398     port->port_no = port_no;
399     port->netdev = netdev;
400     port->internal = internal;
401
402     netdev_get_mtu(netdev, &mtu);
403     if (mtu > max_mtu) {
404         max_mtu = mtu;
405     }
406
407     list_push_back(&xf->port_list, &port->node);
408     xf->ports[port_no] = port;
409     xf->n_ports++;
410     xf->serial++;
411
412     return 0;
413 }
414
415 static int
416 xfif_netdev_port_add(struct xfif *xfif, const char *devname, uint16_t flags,
417                      uint16_t *port_nop)
418 {
419     struct xf_netdev *xf = get_xf_netdev(xfif);
420     int port_no;
421
422     for (port_no = 0; port_no < MAX_PORTS; port_no++) {
423         if (!xf->ports[port_no]) {
424             *port_nop = port_no;
425             return do_add_port(xf, devname, flags, port_no);
426         }
427     }
428     return EFBIG;
429 }
430
431 static int
432 xfif_netdev_port_del(struct xfif *xfif, uint16_t port_no)
433 {
434     struct xf_netdev *xf = get_xf_netdev(xfif);
435     return port_no == XFLOWP_LOCAL ? EINVAL : do_del_port(xf, port_no);
436 }
437
438 static bool
439 is_valid_port_number(uint16_t port_no)
440 {
441     return port_no < MAX_PORTS;
442 }
443
444 static int
445 get_port_by_number(struct xf_netdev *xf,
446                    uint16_t port_no, struct xf_netdev_port **portp)
447 {
448     if (!is_valid_port_number(port_no)) {
449         *portp = NULL;
450         return EINVAL;
451     } else {
452         *portp = xf->ports[port_no];
453         return *portp ? 0 : ENOENT;
454     }
455 }
456
457 static int
458 get_port_by_name(struct xf_netdev *xf,
459                  const char *devname, struct xf_netdev_port **portp)
460 {
461     struct xf_netdev_port *port;
462
463     LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
464         if (!strcmp(netdev_get_name(port->netdev), devname)) {
465             *portp = port;
466             return 0;
467         }
468     }
469     return ENOENT;
470 }
471
472 static int
473 do_del_port(struct xf_netdev *xf, uint16_t port_no)
474 {
475     struct xf_netdev_port *port;
476     char *name;
477     int error;
478
479     error = get_port_by_number(xf, port_no, &port);
480     if (error) {
481         return error;
482     }
483
484     list_remove(&port->node);
485     xf->ports[port->port_no] = NULL;
486     xf->n_ports--;
487     xf->serial++;
488
489     name = xstrdup(netdev_get_name(port->netdev));
490     netdev_close(port->netdev);
491
492     free(name);
493     free(port);
494
495     return 0;
496 }
497
498 static void
499 answer_port_query(const struct xf_netdev_port *port, struct xflow_port *xflow_port)
500 {
501     memset(xflow_port, 0, sizeof *xflow_port);
502     ovs_strlcpy(xflow_port->devname, netdev_get_name(port->netdev),
503                 sizeof xflow_port->devname);
504     xflow_port->port = port->port_no;
505     xflow_port->flags = port->internal ? XFLOW_PORT_INTERNAL : 0;
506 }
507
508 static int
509 xfif_netdev_port_query_by_number(const struct xfif *xfif, uint16_t port_no,
510                                  struct xflow_port *xflow_port)
511 {
512     struct xf_netdev *xf = get_xf_netdev(xfif);
513     struct xf_netdev_port *port;
514     int error;
515
516     error = get_port_by_number(xf, port_no, &port);
517     if (!error) {
518         answer_port_query(port, xflow_port);
519     }
520     return error;
521 }
522
523 static int
524 xfif_netdev_port_query_by_name(const struct xfif *xfif, const char *devname,
525                                struct xflow_port *xflow_port)
526 {
527     struct xf_netdev *xf = get_xf_netdev(xfif);
528     struct xf_netdev_port *port;
529     int error;
530
531     error = get_port_by_name(xf, devname, &port);
532     if (!error) {
533         answer_port_query(port, xflow_port);
534     }
535     return error;
536 }
537
538 static void
539 xf_netdev_free_flow(struct xf_netdev *xf, struct xf_netdev_flow *flow)
540 {
541     hmap_remove(&xf->flow_table, &flow->node);
542     free(flow->actions);
543     free(flow);
544 }
545
546 static void
547 xf_netdev_flow_flush(struct xf_netdev *xf)
548 {
549     struct xf_netdev_flow *flow, *next;
550
551     HMAP_FOR_EACH_SAFE (flow, next, struct xf_netdev_flow, node,
552                         &xf->flow_table) {
553         xf_netdev_free_flow(xf, flow);
554     }
555 }
556
557 static int
558 xfif_netdev_flow_flush(struct xfif *xfif)
559 {
560     struct xf_netdev *xf = get_xf_netdev(xfif);
561     xf_netdev_flow_flush(xf);
562     return 0;
563 }
564
565 static int
566 xfif_netdev_port_list(const struct xfif *xfif, struct xflow_port *ports, int n)
567 {
568     struct xf_netdev *xf = get_xf_netdev(xfif);
569     struct xf_netdev_port *port;
570     int i;
571
572     i = 0;
573     LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
574         struct xflow_port *xflow_port = &ports[i];
575         if (i >= n) {
576             break;
577         }
578         answer_port_query(port, xflow_port);
579         i++;
580     }
581     return xf->n_ports;
582 }
583
584 static int
585 xfif_netdev_port_poll(const struct xfif *xfif_, char **devnamep OVS_UNUSED)
586 {
587     struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
588     if (xfif->xf_serial != xfif->xf->serial) {
589         xfif->xf_serial = xfif->xf->serial;
590         return ENOBUFS;
591     } else {
592         return EAGAIN;
593     }
594 }
595
596 static void
597 xfif_netdev_port_poll_wait(const struct xfif *xfif_)
598 {
599     struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
600     if (xfif->xf_serial != xfif->xf->serial) {
601         poll_immediate_wake();
602     }
603 }
604
605 static int
606 get_port_group(const struct xfif *xfif, int group_no,
607                struct xflow_port_group **groupp)
608 {
609     struct xf_netdev *xf = get_xf_netdev(xfif);
610
611     if (group_no >= 0 && group_no < N_GROUPS) {
612         *groupp = &xf->groups[group_no];
613         return 0;
614     } else {
615         *groupp = NULL;
616         return EINVAL;
617     }
618 }
619
620 static int
621 xfif_netdev_port_group_get(const struct xfif *xfif, int group_no,
622                            uint16_t ports[], int n)
623 {
624     struct xflow_port_group *group;
625     int error;
626
627     if (n < 0) {
628         return -EINVAL;
629     }
630
631     error = get_port_group(xfif, group_no, &group);
632     if (!error) {
633         memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
634         return group->n_ports;
635     } else {
636         return -error;
637     }
638 }
639
640 static int
641 xfif_netdev_port_group_set(struct xfif *xfif, int group_no,
642                            const uint16_t ports[], int n)
643 {
644     struct xflow_port_group *group;
645     int error;
646
647     if (n < 0 || n > MAX_PORTS) {
648         return EINVAL;
649     }
650
651     error = get_port_group(xfif, group_no, &group);
652     if (!error) {
653         free(group->ports);
654         group->ports = xmemdup(ports, n * sizeof *group->ports);
655         group->n_ports = n;
656         group->group = group_no;
657     }
658     return error;
659 }
660
661 static struct xf_netdev_flow *
662 xf_netdev_lookup_flow(const struct xf_netdev *xf,
663                       const struct xflow_key *key)
664 {
665     struct xf_netdev_flow *flow;
666
667     HMAP_FOR_EACH_WITH_HASH (flow, struct xf_netdev_flow, node,
668                              xflow_key_hash(key, 0), &xf->flow_table) {
669         if (xflow_key_equal(&flow->key, key)) {
670             return flow;
671         }
672     }
673     return NULL;
674 }
675
676 static void
677 answer_flow_query(struct xf_netdev_flow *flow, uint32_t query_flags,
678                   struct xflow_flow *xflow_flow)
679 {
680     if (flow) {
681         xflow_flow->key = flow->key;
682         xflow_flow->stats.n_packets = flow->packet_count;
683         xflow_flow->stats.n_bytes = flow->byte_count;
684         xflow_flow->stats.used_sec = flow->used.tv_sec;
685         xflow_flow->stats.used_nsec = flow->used.tv_usec * 1000;
686         xflow_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
687         xflow_flow->stats.ip_tos = flow->ip_tos;
688         xflow_flow->stats.error = 0;
689         if (xflow_flow->n_actions > 0) {
690             unsigned int n = MIN(xflow_flow->n_actions, flow->n_actions);
691             memcpy(xflow_flow->actions, flow->actions,
692                    n * sizeof *xflow_flow->actions);
693             xflow_flow->n_actions = flow->n_actions;
694         }
695
696         if (query_flags & XFLOWFF_ZERO_TCP_FLAGS) {
697             flow->tcp_ctl = 0;
698         }
699
700     } else {
701         xflow_flow->stats.error = ENOENT;
702     }
703 }
704
705 static int
706 xfif_netdev_flow_get(const struct xfif *xfif, struct xflow_flow flows[], int n)
707 {
708     struct xf_netdev *xf = get_xf_netdev(xfif);
709     int i;
710
711     for (i = 0; i < n; i++) {
712         struct xflow_flow *xflow_flow = &flows[i];
713         answer_flow_query(xf_netdev_lookup_flow(xf, &xflow_flow->key),
714                           xflow_flow->flags, xflow_flow);
715     }
716     return 0;
717 }
718
719 static int
720 xfif_netdev_validate_actions(const union xflow_action *actions, int n_actions,
721                              bool *mutates)
722 {
723     unsigned int i;
724
725     *mutates = false;
726     for (i = 0; i < n_actions; i++) {
727         const union xflow_action *a = &actions[i];
728         switch (a->type) {
729         case XFLOWAT_OUTPUT:
730             if (a->output.port >= MAX_PORTS) {
731                 return EINVAL;
732             }
733             break;
734
735         case XFLOWAT_OUTPUT_GROUP:
736             *mutates = true;
737             if (a->output_group.group >= N_GROUPS) {
738                 return EINVAL;
739             }
740             break;
741
742         case XFLOWAT_CONTROLLER:
743             break;
744
745         case XFLOWAT_SET_DL_TCI:
746             *mutates = true;
747             if (a->dl_tci.mask != htons(VLAN_VID_MASK)
748                 && a->dl_tci.mask != htons(VLAN_PCP_MASK)
749                 && a->dl_tci.mask != htons(VLAN_VID_MASK | VLAN_PCP_MASK)) {
750                 return EINVAL;
751             }
752             if (a->dl_tci.tci & ~a->dl_tci.mask){
753                 return EINVAL;
754             }
755             break;
756
757         case XFLOWAT_SET_NW_TOS:
758             *mutates = true;
759             if (a->nw_tos.nw_tos & IP_ECN_MASK) {
760                 return EINVAL;
761             }
762             break;
763
764         case XFLOWAT_STRIP_VLAN:
765         case XFLOWAT_SET_DL_SRC:
766         case XFLOWAT_SET_DL_DST:
767         case XFLOWAT_SET_NW_SRC:
768         case XFLOWAT_SET_NW_DST:
769         case XFLOWAT_SET_TP_SRC:
770         case XFLOWAT_SET_TP_DST:
771             *mutates = true;
772             break;
773
774         default:
775             return EOPNOTSUPP;
776         }
777     }
778     return 0;
779 }
780
781 static int
782 set_flow_actions(struct xf_netdev_flow *flow, struct xflow_flow *xflow_flow)
783 {
784     size_t n_bytes;
785     bool mutates;
786     int error;
787
788     if (xflow_flow->n_actions >= 4096 / sizeof *xflow_flow->actions) {
789         return EINVAL;
790     }
791     error = xfif_netdev_validate_actions(xflow_flow->actions,
792                                          xflow_flow->n_actions, &mutates);
793     if (error) {
794         return error;
795     }
796
797     n_bytes = xflow_flow->n_actions * sizeof *flow->actions;
798     flow->actions = xrealloc(flow->actions, n_bytes);
799     flow->n_actions = xflow_flow->n_actions;
800     memcpy(flow->actions, xflow_flow->actions, n_bytes);
801     return 0;
802 }
803
804 static int
805 add_flow(struct xfif *xfif, struct xflow_flow *xflow_flow)
806 {
807     struct xf_netdev *xf = get_xf_netdev(xfif);
808     struct xf_netdev_flow *flow;
809     int error;
810
811     flow = xzalloc(sizeof *flow);
812     flow->key = xflow_flow->key;
813
814     error = set_flow_actions(flow, xflow_flow);
815     if (error) {
816         free(flow);
817         return error;
818     }
819
820     hmap_insert(&xf->flow_table, &flow->node,
821                 xflow_key_hash(&flow->key, 0));
822     return 0;
823 }
824
825 static void
826 clear_stats(struct xf_netdev_flow *flow)
827 {
828     flow->used.tv_sec = 0;
829     flow->used.tv_usec = 0;
830     flow->packet_count = 0;
831     flow->byte_count = 0;
832     flow->ip_tos = 0;
833     flow->tcp_ctl = 0;
834 }
835
836 static int
837 xfif_netdev_flow_put(struct xfif *xfif, struct xflow_flow_put *put)
838 {
839     struct xf_netdev *xf = get_xf_netdev(xfif);
840     struct xf_netdev_flow *flow;
841
842     flow = xf_netdev_lookup_flow(xf, &put->flow.key);
843     if (!flow) {
844         if (put->flags & XFLOWPF_CREATE) {
845             if (hmap_count(&xf->flow_table) < MAX_FLOWS) {
846                 return add_flow(xfif, &put->flow);
847             } else {
848                 return EFBIG;
849             }
850         } else {
851             return ENOENT;
852         }
853     } else {
854         if (put->flags & XFLOWPF_MODIFY) {
855             int error = set_flow_actions(flow, &put->flow);
856             if (!error && put->flags & XFLOWPF_ZERO_STATS) {
857                 clear_stats(flow);
858             }
859             return error;
860         } else {
861             return EEXIST;
862         }
863     }
864 }
865
866
867 static int
868 xfif_netdev_flow_del(struct xfif *xfif, struct xflow_flow *xflow_flow)
869 {
870     struct xf_netdev *xf = get_xf_netdev(xfif);
871     struct xf_netdev_flow *flow;
872
873     flow = xf_netdev_lookup_flow(xf, &xflow_flow->key);
874     if (flow) {
875         answer_flow_query(flow, 0, xflow_flow);
876         xf_netdev_free_flow(xf, flow);
877         return 0;
878     } else {
879         return ENOENT;
880     }
881 }
882
883 static int
884 xfif_netdev_flow_list(const struct xfif *xfif, struct xflow_flow flows[], int n)
885 {
886     struct xf_netdev *xf = get_xf_netdev(xfif);
887     struct xf_netdev_flow *flow;
888     int i;
889
890     i = 0;
891     HMAP_FOR_EACH (flow, struct xf_netdev_flow, node, &xf->flow_table) {
892         if (i >= n) {
893             break;
894         }
895         answer_flow_query(flow, 0, &flows[i++]);
896     }
897     return hmap_count(&xf->flow_table);
898 }
899
900 static int
901 xfif_netdev_execute(struct xfif *xfif, uint16_t in_port,
902                     const union xflow_action actions[], int n_actions,
903                     const struct ofpbuf *packet)
904 {
905     struct xf_netdev *xf = get_xf_netdev(xfif);
906     struct ofpbuf copy;
907     bool mutates;
908     struct xflow_key key;
909     flow_t flow;
910     int error;
911
912     if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
913         return EINVAL;
914     }
915
916     error = xfif_netdev_validate_actions(actions, n_actions, &mutates);
917     if (error) {
918         return error;
919     }
920
921     if (mutates) {
922         /* We need a deep copy of 'packet' since we're going to modify its
923          * data. */
924         ofpbuf_init(&copy, XF_NETDEV_HEADROOM + packet->size);
925         copy.data = (char*)copy.base + XF_NETDEV_HEADROOM;
926         ofpbuf_put(&copy, packet->data, packet->size);
927     } else {
928         /* We still need a shallow copy of 'packet', even though we won't
929          * modify its data, because flow_extract() modifies packet->l2, etc.
930          * We could probably get away with modifying those but it's more polite
931          * if we don't. */
932         copy = *packet;
933     }
934     flow_extract(&copy, 0, in_port, &flow);
935     xflow_key_from_flow(&key, &flow);
936     error = xf_netdev_execute_actions(xf, &copy, &key, actions, n_actions);
937     if (mutates) {
938         ofpbuf_uninit(&copy);
939     }
940     return error;
941 }
942
943 static int
944 xfif_netdev_recv_get_mask(const struct xfif *xfif, int *listen_mask)
945 {
946     struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
947     *listen_mask = xfif_netdev->listen_mask;
948     return 0;
949 }
950
951 static int
952 xfif_netdev_recv_set_mask(struct xfif *xfif, int listen_mask)
953 {
954     struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
955     if (!(listen_mask & ~XFLOWL_ALL)) {
956         xfif_netdev->listen_mask = listen_mask;
957         return 0;
958     } else {
959         return EINVAL;
960     }
961 }
962
963 static struct ovs_queue *
964 find_nonempty_queue(struct xfif *xfif)
965 {
966     struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
967     struct xf_netdev *xf = get_xf_netdev(xfif);
968     int mask = xfif_netdev->listen_mask;
969     int i;
970
971     for (i = 0; i < N_QUEUES; i++) {
972         struct ovs_queue *q = &xf->queues[i];
973         if (q->n && mask & (1u << i)) {
974             return q;
975         }
976     }
977     return NULL;
978 }
979
980 static int
981 xfif_netdev_recv(struct xfif *xfif, struct ofpbuf **bufp)
982 {
983     struct ovs_queue *q = find_nonempty_queue(xfif);
984     if (q) {
985         *bufp = queue_pop_head(q);
986         return 0;
987     } else {
988         return EAGAIN;
989     }
990 }
991
992 static void
993 xfif_netdev_recv_wait(struct xfif *xfif)
994 {
995     struct ovs_queue *q = find_nonempty_queue(xfif);
996     if (q) {
997         poll_immediate_wake();
998     } else {
999         /* No messages ready to be received, and xf_wait() will ensure that we
1000          * wake up to queue new messages, so there is nothing to do. */
1001     }
1002 }
1003 \f
1004 static void
1005 xf_netdev_flow_used(struct xf_netdev_flow *flow,
1006                     const struct xflow_key *key,
1007                     const struct ofpbuf *packet)
1008 {
1009     time_timeval(&flow->used);
1010     flow->packet_count++;
1011     flow->byte_count += packet->size;
1012     if (key->dl_type == htons(ETH_TYPE_IP)) {
1013         struct ip_header *nh = packet->l3;
1014         flow->ip_tos = nh->ip_tos;
1015
1016         if (key->nw_proto == IPPROTO_TCP) {
1017             struct tcp_header *th = packet->l4;
1018             flow->tcp_ctl |= th->tcp_ctl;
1019         }
1020     }
1021 }
1022
1023 static void
1024 xf_netdev_port_input(struct xf_netdev *xf, struct xf_netdev_port *port,
1025                      struct ofpbuf *packet)
1026 {
1027     struct xf_netdev_flow *flow;
1028     struct xflow_key key;
1029     flow_t f;
1030
1031     if (flow_extract(packet, 0, port->port_no, &f) && xf->drop_frags) {
1032         xf->n_frags++;
1033         return;
1034     }
1035     xflow_key_from_flow(&key, &f);
1036
1037     flow = xf_netdev_lookup_flow(xf, &key);
1038     if (flow) {
1039         xf_netdev_flow_used(flow, &key, packet);
1040         xf_netdev_execute_actions(xf, packet, &key,
1041                                   flow->actions, flow->n_actions);
1042         xf->n_hit++;
1043     } else {
1044         xf->n_missed++;
1045         xf_netdev_output_control(xf, packet, _XFLOWL_MISS_NR, port->port_no, 0);
1046     }
1047 }
1048
1049 static void
1050 xf_netdev_run(void)
1051 {
1052     struct ofpbuf packet;
1053     struct xf_netdev *xf;
1054
1055     ofpbuf_init(&packet, XF_NETDEV_HEADROOM + max_mtu);
1056     LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1057         struct xf_netdev_port *port;
1058
1059         LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1060             int error;
1061
1062             /* Reset packet contents. */
1063             packet.data = (char*)packet.base + XF_NETDEV_HEADROOM;
1064             packet.size = 0;
1065
1066             error = netdev_recv(port->netdev, &packet);
1067             if (!error) {
1068                 xf_netdev_port_input(xf, port, &packet);
1069             } else if (error != EAGAIN) {
1070                 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1071                 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1072                             netdev_get_name(port->netdev), strerror(error));
1073             }
1074         }
1075     }
1076     ofpbuf_uninit(&packet);
1077 }
1078
1079 static void
1080 xf_netdev_wait(void)
1081 {
1082     struct xf_netdev *xf;
1083
1084     LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
1085         struct xf_netdev_port *port;
1086         LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
1087             netdev_recv_wait(port->netdev);
1088         }
1089     }
1090 }
1091
1092
1093 /* Modify or add a 802.1Q header in 'packet' according to 'a'. */
1094 static void
1095 xf_netdev_set_dl_tci(struct ofpbuf *packet, struct xflow_key *key,
1096                      const struct xflow_action_dl_tci *a)
1097 {
1098     struct vlan_eth_header *veh;
1099
1100     if (key->dl_tci) {
1101         veh = packet->l2;
1102         veh->veth_tci = (veh->veth_tci & ~a->mask) | a->tci;
1103     } else {
1104         /* Insert new 802.1Q header. */
1105         struct eth_header *eh = packet->l2;
1106         struct vlan_eth_header tmp;
1107         memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1108         memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1109         tmp.veth_type = htons(ETH_TYPE_VLAN);
1110         tmp.veth_tci = htons(a->tci);
1111         tmp.veth_next_type = eh->eth_type;
1112
1113         veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1114         memcpy(veh, &tmp, sizeof tmp);
1115         packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1116     }
1117
1118     key->dl_tci = veh->veth_tci | htons(XFLOW_TCI_PRESENT);
1119 }
1120
1121 static void
1122 xf_netdev_strip_vlan(struct ofpbuf *packet, struct xflow_key *key)
1123 {
1124     struct vlan_eth_header *veh = packet->l2;
1125     if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
1126         struct eth_header tmp;
1127
1128         memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1129         memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1130         tmp.eth_type = veh->veth_next_type;
1131
1132         packet->size -= VLAN_HEADER_LEN;
1133         packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1134         packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1135         memcpy(packet->data, &tmp, sizeof tmp);
1136
1137         key->dl_tci = htons(0);
1138     }
1139 }
1140
1141 static void
1142 xf_netdev_set_dl_src(struct ofpbuf *packet, struct xflow_key *key,
1143                      const uint8_t dl_addr[ETH_ADDR_LEN])
1144 {
1145     struct eth_header *eh = packet->l2;
1146     memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1147     memcpy(key->dl_src, dl_addr, sizeof key->dl_src);
1148 }
1149
1150 static void
1151 xf_netdev_set_dl_dst(struct ofpbuf *packet, struct xflow_key *key,
1152                      const uint8_t dl_addr[ETH_ADDR_LEN])
1153 {
1154     struct eth_header *eh = packet->l2;
1155     memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1156     memcpy(key->dl_dst, dl_addr, sizeof key->dl_dst);
1157 }
1158
1159 static void
1160 xf_netdev_set_nw_addr(struct ofpbuf *packet, struct xflow_key *key,
1161                       const struct xflow_action_nw_addr *a)
1162 {
1163     if (key->dl_type == htons(ETH_TYPE_IP)) {
1164         struct ip_header *nh = packet->l3;
1165         uint32_t *field;
1166
1167         field = a->type == XFLOWAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1168         if (key->nw_proto == IP_TYPE_TCP) {
1169             struct tcp_header *th = packet->l4;
1170             th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1171         } else if (key->nw_proto == IP_TYPE_UDP) {
1172             struct udp_header *uh = packet->l4;
1173             if (uh->udp_csum) {
1174                 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1175                 if (!uh->udp_csum) {
1176                     uh->udp_csum = 0xffff;
1177                 }
1178             }
1179         }
1180         nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1181         *field = a->nw_addr;
1182
1183         if (a->type == XFLOWAT_SET_NW_SRC) {
1184             key->nw_src = a->type;
1185         } else {
1186             key->nw_dst = a->type;
1187         }
1188     }
1189 }
1190
1191 static void
1192 xf_netdev_set_nw_tos(struct ofpbuf *packet, struct xflow_key *key,
1193                      const struct xflow_action_nw_tos *a)
1194 {
1195     if (key->dl_type == htons(ETH_TYPE_IP)) {
1196         struct ip_header *nh = packet->l3;
1197         uint8_t *field = &nh->ip_tos;
1198
1199         /* Set the DSCP bits and preserve the ECN bits. */
1200         uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1201
1202         nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1203                 htons((uint16_t)a->nw_tos));
1204         *field = new;
1205         key->nw_tos = a->nw_tos;
1206     }
1207 }
1208
1209 static void
1210 xf_netdev_set_tp_port(struct ofpbuf *packet, struct xflow_key *key,
1211                       const struct xflow_action_tp_port *a)
1212 {
1213     if (key->dl_type == htons(ETH_TYPE_IP)) {
1214         uint16_t *field;
1215         if (key->nw_proto == IPPROTO_TCP) {
1216             struct tcp_header *th = packet->l4;
1217             field = a->type == XFLOWAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1218             th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1219             *field = a->tp_port;
1220         } else if (key->nw_proto == IPPROTO_UDP) {
1221             struct udp_header *uh = packet->l4;
1222             field = a->type == XFLOWAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1223             uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1224             *field = a->tp_port;
1225         } else {
1226             return;
1227         }
1228
1229         if (a->type == XFLOWAT_SET_TP_SRC) {
1230             key->tp_src = a->tp_port;
1231         } else {
1232             key->tp_dst = a->tp_port;
1233         }
1234     }
1235 }
1236
1237 static void
1238 xf_netdev_output_port(struct xf_netdev *xf, struct ofpbuf *packet,
1239                       uint16_t out_port)
1240 {
1241     struct xf_netdev_port *p = xf->ports[out_port];
1242     if (p) {
1243         netdev_send(p->netdev, packet);
1244     }
1245 }
1246
1247 static void
1248 xf_netdev_output_group(struct xf_netdev *xf, uint16_t group, uint16_t in_port,
1249                        struct ofpbuf *packet)
1250 {
1251     struct xflow_port_group *g = &xf->groups[group];
1252     int i;
1253
1254     for (i = 0; i < g->n_ports; i++) {
1255         uint16_t out_port = g->ports[i];
1256         if (out_port != in_port) {
1257             xf_netdev_output_port(xf, packet, out_port);
1258         }
1259     }
1260 }
1261
1262 static int
1263 xf_netdev_output_control(struct xf_netdev *xf, const struct ofpbuf *packet,
1264                          int queue_no, int port_no, uint32_t arg)
1265 {
1266     struct ovs_queue *q = &xf->queues[queue_no];
1267     struct xflow_msg *header;
1268     struct ofpbuf *msg;
1269     size_t msg_size;
1270
1271     if (q->n >= MAX_QUEUE_LEN) {
1272         xf->n_lost++;
1273         return ENOBUFS;
1274     }
1275
1276     msg_size = sizeof *header + packet->size;
1277     msg = ofpbuf_new(msg_size + XFIF_RECV_MSG_PADDING);
1278     header = ofpbuf_put_uninit(msg, sizeof *header);
1279     ofpbuf_reserve(msg, XFIF_RECV_MSG_PADDING);
1280     header->type = queue_no;
1281     header->length = msg_size;
1282     header->port = port_no;
1283     header->arg = arg;
1284     ofpbuf_put(msg, packet->data, packet->size);
1285     queue_push_tail(q, msg);
1286
1287     return 0;
1288 }
1289
1290 static int
1291 xf_netdev_execute_actions(struct xf_netdev *xf,
1292                           struct ofpbuf *packet, struct xflow_key *key,
1293                           const union xflow_action *actions, int n_actions)
1294 {
1295     int i;
1296     for (i = 0; i < n_actions; i++) {
1297         const union xflow_action *a = &actions[i];
1298
1299         switch (a->type) {
1300         case XFLOWAT_OUTPUT:
1301             xf_netdev_output_port(xf, packet, a->output.port);
1302             break;
1303
1304         case XFLOWAT_OUTPUT_GROUP:
1305             xf_netdev_output_group(xf, a->output_group.group, key->in_port,
1306                                    packet);
1307             break;
1308
1309         case XFLOWAT_CONTROLLER:
1310             xf_netdev_output_control(xf, packet, _XFLOWL_ACTION_NR,
1311                                      key->in_port, a->controller.arg);
1312             break;
1313
1314         case XFLOWAT_SET_DL_TCI:
1315             xf_netdev_set_dl_tci(packet, key, &a->dl_tci);
1316             break;
1317
1318         case XFLOWAT_STRIP_VLAN:
1319             xf_netdev_strip_vlan(packet, key);
1320             break;
1321
1322         case XFLOWAT_SET_DL_SRC:
1323             xf_netdev_set_dl_src(packet, key, a->dl_addr.dl_addr);
1324             break;
1325
1326         case XFLOWAT_SET_DL_DST:
1327             xf_netdev_set_dl_dst(packet, key, a->dl_addr.dl_addr);
1328             break;
1329
1330         case XFLOWAT_SET_NW_SRC:
1331         case XFLOWAT_SET_NW_DST:
1332             xf_netdev_set_nw_addr(packet, key, &a->nw_addr);
1333             break;
1334
1335         case XFLOWAT_SET_NW_TOS:
1336             xf_netdev_set_nw_tos(packet, key, &a->nw_tos);
1337             break;
1338
1339         case XFLOWAT_SET_TP_SRC:
1340         case XFLOWAT_SET_TP_DST:
1341             xf_netdev_set_tp_port(packet, key, &a->tp_port);
1342             break;
1343         }
1344     }
1345     return 0;
1346 }
1347
1348 const struct xfif_class xfif_netdev_class = {
1349     "netdev",
1350     xf_netdev_run,
1351     xf_netdev_wait,
1352     NULL,                       /* enumerate */
1353     xfif_netdev_open,
1354     xfif_netdev_close,
1355     NULL,                       /* get_all_names */
1356     xfif_netdev_destroy,
1357     xfif_netdev_get_stats,
1358     xfif_netdev_get_drop_frags,
1359     xfif_netdev_set_drop_frags,
1360     xfif_netdev_port_add,
1361     xfif_netdev_port_del,
1362     xfif_netdev_port_query_by_number,
1363     xfif_netdev_port_query_by_name,
1364     xfif_netdev_port_list,
1365     xfif_netdev_port_poll,
1366     xfif_netdev_port_poll_wait,
1367     xfif_netdev_port_group_get,
1368     xfif_netdev_port_group_set,
1369     xfif_netdev_flow_get,
1370     xfif_netdev_flow_put,
1371     xfif_netdev_flow_del,
1372     xfif_netdev_flow_flush,
1373     xfif_netdev_flow_list,
1374     xfif_netdev_execute,
1375     xfif_netdev_recv_get_mask,
1376     xfif_netdev_recv_set_mask,
1377     NULL,                       /* get_sflow_probability */
1378     NULL,                       /* set_sflow_probability */
1379     xfif_netdev_recv,
1380     xfif_netdev_recv_wait,
1381 };