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