wdp-xflow: Always report an up-to-date port list in wx_port_list().
[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, 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, node, &xf->flow_table) {
549         xf_netdev_free_flow(xf, flow);
550     }
551 }
552
553 static int
554 xfif_netdev_flow_flush(struct xfif *xfif)
555 {
556     struct xf_netdev *xf = get_xf_netdev(xfif);
557     xf_netdev_flow_flush(xf);
558     return 0;
559 }
560
561 static int
562 xfif_netdev_port_list(const struct xfif *xfif, struct xflow_port *ports, int n)
563 {
564     struct xf_netdev *xf = get_xf_netdev(xfif);
565     struct xf_netdev_port *port;
566     int i;
567
568     i = 0;
569     LIST_FOR_EACH (port, node, &xf->port_list) {
570         struct xflow_port *xflow_port = &ports[i];
571         if (i >= n) {
572             break;
573         }
574         answer_port_query(port, xflow_port);
575         i++;
576     }
577     return xf->n_ports;
578 }
579
580 static int
581 xfif_netdev_port_poll(const struct xfif *xfif_, char **devnamep OVS_UNUSED)
582 {
583     struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
584     if (xfif->xf_serial != xfif->xf->serial) {
585         xfif->xf_serial = xfif->xf->serial;
586         return ENOBUFS;
587     } else {
588         return EAGAIN;
589     }
590 }
591
592 static void
593 xfif_netdev_port_poll_wait(const struct xfif *xfif_)
594 {
595     struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
596     if (xfif->xf_serial != xfif->xf->serial) {
597         poll_immediate_wake();
598     }
599 }
600
601 static int
602 get_port_group(const struct xfif *xfif, int group_no,
603                struct xflow_port_group **groupp)
604 {
605     struct xf_netdev *xf = get_xf_netdev(xfif);
606
607     if (group_no >= 0 && group_no < N_GROUPS) {
608         *groupp = &xf->groups[group_no];
609         return 0;
610     } else {
611         *groupp = NULL;
612         return EINVAL;
613     }
614 }
615
616 static int
617 xfif_netdev_port_group_get(const struct xfif *xfif, int group_no,
618                            uint16_t ports[], int n)
619 {
620     struct xflow_port_group *group;
621     int error;
622
623     if (n < 0) {
624         return -EINVAL;
625     }
626
627     error = get_port_group(xfif, group_no, &group);
628     if (!error) {
629         memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
630         return group->n_ports;
631     } else {
632         return -error;
633     }
634 }
635
636 static int
637 xfif_netdev_port_group_set(struct xfif *xfif, int group_no,
638                            const uint16_t ports[], int n)
639 {
640     struct xflow_port_group *group;
641     int error;
642
643     if (n < 0 || n > MAX_PORTS) {
644         return EINVAL;
645     }
646
647     error = get_port_group(xfif, group_no, &group);
648     if (!error) {
649         free(group->ports);
650         group->ports = xmemdup(ports, n * sizeof *group->ports);
651         group->n_ports = n;
652         group->group = group_no;
653     }
654     return error;
655 }
656
657 static struct xf_netdev_flow *
658 xf_netdev_lookup_flow(const struct xf_netdev *xf,
659                       const struct xflow_key *key)
660 {
661     struct xf_netdev_flow *flow;
662
663     HMAP_FOR_EACH_WITH_HASH (flow, node,
664                              xflow_key_hash(key, 0), &xf->flow_table) {
665         if (xflow_key_equal(&flow->key, key)) {
666             return flow;
667         }
668     }
669     return NULL;
670 }
671
672 static void
673 answer_flow_query(struct xf_netdev_flow *flow, uint32_t query_flags,
674                   struct xflow_flow *xflow_flow)
675 {
676     if (flow) {
677         xflow_flow->key = flow->key;
678         xflow_flow->stats.n_packets = flow->packet_count;
679         xflow_flow->stats.n_bytes = flow->byte_count;
680         xflow_flow->stats.used_sec = flow->used.tv_sec;
681         xflow_flow->stats.used_nsec = flow->used.tv_nsec;
682         xflow_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
683         xflow_flow->stats.reserved = 0;
684         xflow_flow->stats.error = 0;
685         if (xflow_flow->n_actions > 0) {
686             unsigned int n = MIN(xflow_flow->n_actions, flow->n_actions);
687             memcpy(xflow_flow->actions, flow->actions,
688                    n * sizeof *xflow_flow->actions);
689             xflow_flow->n_actions = flow->n_actions;
690         }
691
692         if (query_flags & XFLOWFF_ZERO_TCP_FLAGS) {
693             flow->tcp_ctl = 0;
694         }
695
696     } else {
697         xflow_flow->stats.error = ENOENT;
698     }
699 }
700
701 static int
702 xfif_netdev_flow_get(const struct xfif *xfif, struct xflow_flow flows[], int n)
703 {
704     struct xf_netdev *xf = get_xf_netdev(xfif);
705     int i;
706
707     for (i = 0; i < n; i++) {
708         struct xflow_flow *xflow_flow = &flows[i];
709         answer_flow_query(xf_netdev_lookup_flow(xf, &xflow_flow->key),
710                           xflow_flow->flags, xflow_flow);
711     }
712     return 0;
713 }
714
715 static int
716 xfif_netdev_validate_actions(const union xflow_action *actions, int n_actions,
717                              bool *mutates)
718 {
719     unsigned int i;
720
721     *mutates = false;
722     for (i = 0; i < n_actions; i++) {
723         const union xflow_action *a = &actions[i];
724         switch (a->type) {
725         case XFLOWAT_OUTPUT:
726             if (a->output.port >= MAX_PORTS) {
727                 return EINVAL;
728             }
729             break;
730
731         case XFLOWAT_OUTPUT_GROUP:
732             *mutates = true;
733             if (a->output_group.group >= N_GROUPS) {
734                 return EINVAL;
735             }
736             break;
737
738         case XFLOWAT_CONTROLLER:
739             break;
740
741         case XFLOWAT_SET_DL_TCI:
742             *mutates = true;
743             if (a->dl_tci.mask != htons(VLAN_VID_MASK)
744                 && a->dl_tci.mask != htons(VLAN_PCP_MASK)
745                 && a->dl_tci.mask != htons(VLAN_VID_MASK | VLAN_PCP_MASK)) {
746                 return EINVAL;
747             }
748             if (a->dl_tci.tci & ~a->dl_tci.mask){
749                 return EINVAL;
750             }
751             break;
752
753         case XFLOWAT_SET_NW_TOS:
754             *mutates = true;
755             if (a->nw_tos.nw_tos & IP_ECN_MASK) {
756                 return EINVAL;
757             }
758             break;
759
760         case XFLOWAT_STRIP_VLAN:
761         case XFLOWAT_SET_DL_SRC:
762         case XFLOWAT_SET_DL_DST:
763         case XFLOWAT_SET_NW_SRC:
764         case XFLOWAT_SET_NW_DST:
765         case XFLOWAT_SET_TP_SRC:
766         case XFLOWAT_SET_TP_DST:
767             *mutates = true;
768             break;
769
770         default:
771             return EOPNOTSUPP;
772         }
773     }
774     return 0;
775 }
776
777 static int
778 set_flow_actions(struct xf_netdev_flow *flow, struct xflow_flow *xflow_flow)
779 {
780     size_t n_bytes;
781     bool mutates;
782     int error;
783
784     if (xflow_flow->n_actions >= 4096 / sizeof *xflow_flow->actions) {
785         return EINVAL;
786     }
787     error = xfif_netdev_validate_actions(xflow_flow->actions,
788                                          xflow_flow->n_actions, &mutates);
789     if (error) {
790         return error;
791     }
792
793     n_bytes = xflow_flow->n_actions * sizeof *flow->actions;
794     flow->actions = xrealloc(flow->actions, n_bytes);
795     flow->n_actions = xflow_flow->n_actions;
796     memcpy(flow->actions, xflow_flow->actions, n_bytes);
797     return 0;
798 }
799
800 static int
801 add_flow(struct xfif *xfif, struct xflow_flow *xflow_flow)
802 {
803     struct xf_netdev *xf = get_xf_netdev(xfif);
804     struct xf_netdev_flow *flow;
805     int error;
806
807     flow = xzalloc(sizeof *flow);
808     flow->key = xflow_flow->key;
809
810     error = set_flow_actions(flow, xflow_flow);
811     if (error) {
812         free(flow);
813         return error;
814     }
815
816     hmap_insert(&xf->flow_table, &flow->node,
817                 xflow_key_hash(&flow->key, 0));
818     return 0;
819 }
820
821 static void
822 clear_stats(struct xf_netdev_flow *flow)
823 {
824     flow->used.tv_sec = 0;
825     flow->used.tv_nsec = 0;
826     flow->packet_count = 0;
827     flow->byte_count = 0;
828     flow->tcp_ctl = 0;
829 }
830
831 static int
832 xfif_netdev_flow_put(struct xfif *xfif, struct xflow_flow_put *put)
833 {
834     struct xf_netdev *xf = get_xf_netdev(xfif);
835     struct xf_netdev_flow *flow;
836
837     flow = xf_netdev_lookup_flow(xf, &put->flow.key);
838     if (!flow) {
839         if (put->flags & XFLOWPF_CREATE) {
840             if (hmap_count(&xf->flow_table) < MAX_FLOWS) {
841                 return add_flow(xfif, &put->flow);
842             } else {
843                 return EFBIG;
844             }
845         } else {
846             return ENOENT;
847         }
848     } else {
849         if (put->flags & XFLOWPF_MODIFY) {
850             int error = set_flow_actions(flow, &put->flow);
851             if (!error && put->flags & XFLOWPF_ZERO_STATS) {
852                 clear_stats(flow);
853             }
854             return error;
855         } else {
856             return EEXIST;
857         }
858     }
859 }
860
861
862 static int
863 xfif_netdev_flow_del(struct xfif *xfif, struct xflow_flow *xflow_flow)
864 {
865     struct xf_netdev *xf = get_xf_netdev(xfif);
866     struct xf_netdev_flow *flow;
867
868     flow = xf_netdev_lookup_flow(xf, &xflow_flow->key);
869     if (flow) {
870         answer_flow_query(flow, 0, xflow_flow);
871         xf_netdev_free_flow(xf, flow);
872         return 0;
873     } else {
874         return ENOENT;
875     }
876 }
877
878 static int
879 xfif_netdev_flow_list(const struct xfif *xfif, struct xflow_flow flows[], int n)
880 {
881     struct xf_netdev *xf = get_xf_netdev(xfif);
882     struct xf_netdev_flow *flow;
883     int i;
884
885     i = 0;
886     HMAP_FOR_EACH (flow, node, &xf->flow_table) {
887         if (i >= n) {
888             break;
889         }
890         answer_flow_query(flow, 0, &flows[i++]);
891     }
892     return hmap_count(&xf->flow_table);
893 }
894
895 static int
896 xfif_netdev_execute(struct xfif *xfif, uint16_t in_port,
897                     const union xflow_action actions[], int n_actions,
898                     const struct ofpbuf *packet)
899 {
900     struct xf_netdev *xf = get_xf_netdev(xfif);
901     struct ofpbuf copy;
902     bool mutates;
903     struct xflow_key key;
904     flow_t flow;
905     int error;
906
907     if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
908         return EINVAL;
909     }
910
911     error = xfif_netdev_validate_actions(actions, n_actions, &mutates);
912     if (error) {
913         return error;
914     }
915
916     if (mutates) {
917         /* We need a deep copy of 'packet' since we're going to modify its
918          * data. */
919         ofpbuf_init(&copy, XF_NETDEV_HEADROOM + packet->size);
920         copy.data = (char*)copy.base + XF_NETDEV_HEADROOM;
921         ofpbuf_put(&copy, packet->data, packet->size);
922     } else {
923         /* We still need a shallow copy of 'packet', even though we won't
924          * modify its data, because flow_extract() modifies packet->l2, etc.
925          * We could probably get away with modifying those but it's more polite
926          * if we don't. */
927         copy = *packet;
928     }
929     flow_extract(&copy, 0, in_port, &flow);
930     xflow_key_from_flow(&key, &flow);
931     error = xf_netdev_execute_actions(xf, &copy, &key, actions, n_actions);
932     if (mutates) {
933         ofpbuf_uninit(&copy);
934     }
935     return error;
936 }
937
938 static int
939 xfif_netdev_recv_get_mask(const struct xfif *xfif, int *listen_mask)
940 {
941     struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
942     *listen_mask = xfif_netdev->listen_mask;
943     return 0;
944 }
945
946 static int
947 xfif_netdev_recv_set_mask(struct xfif *xfif, int listen_mask)
948 {
949     struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
950     if (!(listen_mask & ~XFLOWL_ALL)) {
951         xfif_netdev->listen_mask = listen_mask;
952         return 0;
953     } else {
954         return EINVAL;
955     }
956 }
957
958 static struct ovs_queue *
959 find_nonempty_queue(struct xfif *xfif)
960 {
961     struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
962     struct xf_netdev *xf = get_xf_netdev(xfif);
963     int mask = xfif_netdev->listen_mask;
964     int i;
965
966     for (i = 0; i < N_QUEUES; i++) {
967         struct ovs_queue *q = &xf->queues[i];
968         if (q->n && mask & (1u << i)) {
969             return q;
970         }
971     }
972     return NULL;
973 }
974
975 static int
976 xfif_netdev_recv(struct xfif *xfif, struct ofpbuf **bufp)
977 {
978     struct ovs_queue *q = find_nonempty_queue(xfif);
979     if (q) {
980         *bufp = queue_pop_head(q);
981         return 0;
982     } else {
983         return EAGAIN;
984     }
985 }
986
987 static void
988 xfif_netdev_recv_wait(struct xfif *xfif)
989 {
990     struct ovs_queue *q = find_nonempty_queue(xfif);
991     if (q) {
992         poll_immediate_wake();
993     } else {
994         /* No messages ready to be received, and xf_wait() will ensure that we
995          * wake up to queue new messages, so there is nothing to do. */
996     }
997 }
998 \f
999 static void
1000 xf_netdev_flow_used(struct xf_netdev_flow *flow,
1001                     const struct xflow_key *key,
1002                     const struct ofpbuf *packet)
1003 {
1004     time_timespec(&flow->used);
1005     flow->packet_count++;
1006     flow->byte_count += packet->size;
1007     if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
1008         struct tcp_header *th = packet->l4;
1009         flow->tcp_ctl |= th->tcp_ctl;
1010     }
1011 }
1012
1013 static void
1014 xf_netdev_port_input(struct xf_netdev *xf, struct xf_netdev_port *port,
1015                      struct ofpbuf *packet)
1016 {
1017     struct xf_netdev_flow *flow;
1018     struct xflow_key key;
1019     flow_t f;
1020
1021     if (packet->size < ETH_HEADER_LEN) {
1022         return;
1023     }
1024     if (flow_extract(packet, 0, port->port_no, &f) && xf->drop_frags) {
1025         xf->n_frags++;
1026         return;
1027     }
1028     xflow_key_from_flow(&key, &f);
1029
1030     flow = xf_netdev_lookup_flow(xf, &key);
1031     if (flow) {
1032         xf_netdev_flow_used(flow, &key, packet);
1033         xf_netdev_execute_actions(xf, packet, &key,
1034                                   flow->actions, flow->n_actions);
1035         xf->n_hit++;
1036     } else {
1037         xf->n_missed++;
1038         xf_netdev_output_control(xf, packet, _XFLOWL_MISS_NR, port->port_no, 0);
1039     }
1040 }
1041
1042 static void
1043 xf_netdev_run(void)
1044 {
1045     struct ofpbuf packet;
1046     struct xf_netdev *xf;
1047
1048     ofpbuf_init(&packet, XF_NETDEV_HEADROOM + max_mtu);
1049     LIST_FOR_EACH (xf, node, &xf_netdev_list) {
1050         struct xf_netdev_port *port;
1051
1052         LIST_FOR_EACH (port, node, &xf->port_list) {
1053             int error;
1054
1055             /* Reset packet contents. */
1056             packet.data = (char*)packet.base + XF_NETDEV_HEADROOM;
1057             packet.size = 0;
1058
1059             error = netdev_recv(port->netdev, &packet);
1060             if (!error) {
1061                 xf_netdev_port_input(xf, port, &packet);
1062             } else if (error != EAGAIN) {
1063                 struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1064                 VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
1065                             netdev_get_name(port->netdev), strerror(error));
1066             }
1067         }
1068     }
1069     ofpbuf_uninit(&packet);
1070 }
1071
1072 static void
1073 xf_netdev_wait(void)
1074 {
1075     struct xf_netdev *xf;
1076
1077     LIST_FOR_EACH (xf, node, &xf_netdev_list) {
1078         struct xf_netdev_port *port;
1079         LIST_FOR_EACH (port, node, &xf->port_list) {
1080             netdev_recv_wait(port->netdev);
1081         }
1082     }
1083 }
1084
1085
1086 /* Modify or add a 802.1Q header in 'packet' according to 'a'. */
1087 static void
1088 xf_netdev_set_dl_tci(struct ofpbuf *packet,
1089                      const struct xflow_action_dl_tci *a)
1090 {
1091     struct vlan_eth_header *veh;
1092     struct eth_header *eh;
1093
1094     eh = packet->l2;
1095     if (packet->size >= sizeof(struct vlan_eth_header)
1096         && eh->eth_type == htons(ETH_TYPE_VLAN)) {
1097         veh = packet->l2;
1098         veh->veth_tci = (veh->veth_tci & ~a->mask) | a->tci;
1099     } else {
1100         /* Insert new 802.1Q header. */
1101         struct vlan_eth_header tmp;
1102         memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
1103         memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
1104         tmp.veth_type = htons(ETH_TYPE_VLAN);
1105         tmp.veth_tci = htons(a->tci);
1106         tmp.veth_next_type = eh->eth_type;
1107
1108         veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
1109         memcpy(veh, &tmp, sizeof tmp);
1110         packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
1111     }
1112 }
1113
1114 static void
1115 xf_netdev_strip_vlan(struct ofpbuf *packet, struct xflow_key *key)
1116 {
1117     struct vlan_eth_header *veh = packet->l2;
1118     if (packet->size >= sizeof *veh
1119         && veh->veth_type == htons(ETH_TYPE_VLAN)) {
1120         struct eth_header tmp;
1121
1122         memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
1123         memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
1124         tmp.eth_type = veh->veth_next_type;
1125
1126         packet->size -= VLAN_HEADER_LEN;
1127         packet->data = (char*)packet->data + VLAN_HEADER_LEN;
1128         packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
1129         memcpy(packet->data, &tmp, sizeof tmp);
1130
1131         key->dl_tci = htons(0);
1132     }
1133 }
1134
1135 static void
1136 xf_netdev_set_dl_src(struct ofpbuf *packet,
1137                      const uint8_t dl_addr[ETH_ADDR_LEN])
1138 {
1139     struct eth_header *eh = packet->l2;
1140     memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
1141 }
1142
1143 static void
1144 xf_netdev_set_dl_dst(struct ofpbuf *packet,
1145                      const uint8_t dl_addr[ETH_ADDR_LEN])
1146 {
1147     struct eth_header *eh = packet->l2;
1148     memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
1149 }
1150
1151 static bool
1152 is_ip(const struct ofpbuf *packet, const struct xflow_key *key)
1153 {
1154     return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
1155 }
1156
1157 static void
1158 xf_netdev_set_nw_addr(struct ofpbuf *packet, const struct xflow_key *key,
1159                       const struct xflow_action_nw_addr *a)
1160 {
1161     if (is_ip(packet, key)) {
1162         struct ip_header *nh = packet->l3;
1163         uint32_t *field;
1164
1165         field = a->type == XFLOWAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
1166         if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
1167             struct tcp_header *th = packet->l4;
1168             th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
1169         } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
1170             struct udp_header *uh = packet->l4;
1171             if (uh->udp_csum) {
1172                 uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
1173                 if (!uh->udp_csum) {
1174                     uh->udp_csum = 0xffff;
1175                 }
1176             }
1177         }
1178         nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
1179         *field = a->nw_addr;
1180     }
1181 }
1182
1183 static void
1184 xf_netdev_set_nw_tos(struct ofpbuf *packet, const struct xflow_key *key,
1185                      const struct xflow_action_nw_tos *a)
1186 {
1187     if (is_ip(packet, key)) {
1188         struct ip_header *nh = packet->l3;
1189         uint8_t *field = &nh->ip_tos;
1190
1191         /* Set the DSCP bits and preserve the ECN bits. */
1192         uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
1193
1194         nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
1195                 htons((uint16_t)a->nw_tos));
1196         *field = new;
1197     }
1198 }
1199
1200 static void
1201 xf_netdev_set_tp_port(struct ofpbuf *packet, const struct xflow_key *key,
1202                       const struct xflow_action_tp_port *a)
1203 {
1204     if (is_ip(packet, key)) {
1205         uint16_t *field;
1206         if (key->nw_proto == IPPROTO_TCP && packet->l7) {
1207             struct tcp_header *th = packet->l4;
1208             field = a->type == XFLOWAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
1209             th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
1210             *field = a->tp_port;
1211         } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
1212             struct udp_header *uh = packet->l4;
1213             field = a->type == XFLOWAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
1214             uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
1215             *field = a->tp_port;
1216         } else {
1217             return;
1218         }
1219     }
1220 }
1221
1222 static void
1223 xf_netdev_output_port(struct xf_netdev *xf, struct ofpbuf *packet,
1224                       uint16_t out_port)
1225 {
1226     struct xf_netdev_port *p = xf->ports[out_port];
1227     if (p) {
1228         netdev_send(p->netdev, packet);
1229     }
1230 }
1231
1232 static void
1233 xf_netdev_output_group(struct xf_netdev *xf, uint16_t group, uint16_t in_port,
1234                        struct ofpbuf *packet)
1235 {
1236     struct xflow_port_group *g = &xf->groups[group];
1237     int i;
1238
1239     for (i = 0; i < g->n_ports; i++) {
1240         uint16_t out_port = g->ports[i];
1241         if (out_port != in_port) {
1242             xf_netdev_output_port(xf, packet, out_port);
1243         }
1244     }
1245 }
1246
1247 static int
1248 xf_netdev_output_control(struct xf_netdev *xf, const struct ofpbuf *packet,
1249                          int queue_no, int port_no, uint32_t arg)
1250 {
1251     struct ovs_queue *q = &xf->queues[queue_no];
1252     struct xflow_msg *header;
1253     struct ofpbuf *msg;
1254     size_t msg_size;
1255
1256     if (q->n >= MAX_QUEUE_LEN) {
1257         xf->n_lost++;
1258         return ENOBUFS;
1259     }
1260
1261     msg_size = sizeof *header + packet->size;
1262     msg = ofpbuf_new(msg_size + XFIF_RECV_MSG_PADDING);
1263     header = ofpbuf_put_uninit(msg, sizeof *header);
1264     ofpbuf_reserve(msg, XFIF_RECV_MSG_PADDING);
1265     header->type = queue_no;
1266     header->length = msg_size;
1267     header->port = port_no;
1268     header->arg = arg;
1269     ofpbuf_put(msg, packet->data, packet->size);
1270     queue_push_tail(q, msg);
1271
1272     return 0;
1273 }
1274
1275 /* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
1276  * screwy or truncated header fields or one whose inner and outer Ethernet
1277  * address differ. */
1278 static bool
1279 xf_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct xflow_key *key)
1280 {
1281     struct arp_eth_header *arp;
1282     struct eth_header *eth;
1283     ptrdiff_t l3_size;
1284
1285     if (key->dl_type != htons(ETH_TYPE_ARP)) {
1286         return false;
1287     }
1288
1289     l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
1290     if (l3_size < sizeof(struct arp_eth_header)) {
1291         return true;
1292     }
1293
1294     eth = packet->l2;
1295     arp = packet->l3;
1296     return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
1297             || arp->ar_pro != htons(ARP_PRO_IP)
1298             || arp->ar_hln != ETH_HEADER_LEN
1299             || arp->ar_pln != 4
1300             || !eth_addr_equals(arp->ar_sha, eth->eth_src));
1301 }
1302
1303 static int
1304 xf_netdev_execute_actions(struct xf_netdev *xf,
1305                           struct ofpbuf *packet, struct xflow_key *key,
1306                           const union xflow_action *actions, int n_actions)
1307 {
1308     int i;
1309     for (i = 0; i < n_actions; i++) {
1310         const union xflow_action *a = &actions[i];
1311
1312         switch (a->type) {
1313         case XFLOWAT_OUTPUT:
1314             xf_netdev_output_port(xf, packet, a->output.port);
1315             break;
1316
1317         case XFLOWAT_OUTPUT_GROUP:
1318             xf_netdev_output_group(xf, a->output_group.group, key->in_port,
1319                                    packet);
1320             break;
1321
1322         case XFLOWAT_CONTROLLER:
1323             xf_netdev_output_control(xf, packet, _XFLOWL_ACTION_NR,
1324                                      key->in_port, a->controller.arg);
1325             break;
1326
1327         case XFLOWAT_SET_DL_TCI:
1328             xf_netdev_set_dl_tci(packet, &a->dl_tci);
1329             break;
1330
1331         case XFLOWAT_STRIP_VLAN:
1332             xf_netdev_strip_vlan(packet, key);
1333             break;
1334
1335         case XFLOWAT_SET_DL_SRC:
1336             xf_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
1337             break;
1338
1339         case XFLOWAT_SET_DL_DST:
1340             xf_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
1341             break;
1342
1343         case XFLOWAT_SET_NW_SRC:
1344         case XFLOWAT_SET_NW_DST:
1345             xf_netdev_set_nw_addr(packet, key, &a->nw_addr);
1346             break;
1347
1348         case XFLOWAT_SET_NW_TOS:
1349             xf_netdev_set_nw_tos(packet, key, &a->nw_tos);
1350             break;
1351
1352         case XFLOWAT_SET_TP_SRC:
1353         case XFLOWAT_SET_TP_DST:
1354             xf_netdev_set_tp_port(packet, key, &a->tp_port);
1355             break;
1356
1357         case XFLOWAT_DROP_SPOOFED_ARP:
1358             if (xf_netdev_is_spoofed_arp(packet, key)) {
1359                 return 0;
1360             }
1361         }
1362     }
1363     return 0;
1364 }
1365
1366 const struct xfif_class xfif_netdev_class = {
1367     "netdev",
1368     xf_netdev_run,
1369     xf_netdev_wait,
1370     NULL,                       /* enumerate */
1371     xfif_netdev_open,
1372     xfif_netdev_close,
1373     NULL,                       /* get_all_names */
1374     xfif_netdev_destroy,
1375     xfif_netdev_get_stats,
1376     xfif_netdev_get_drop_frags,
1377     xfif_netdev_set_drop_frags,
1378     xfif_netdev_port_add,
1379     xfif_netdev_port_del,
1380     xfif_netdev_port_query_by_number,
1381     xfif_netdev_port_query_by_name,
1382     xfif_netdev_port_list,
1383     xfif_netdev_port_poll,
1384     xfif_netdev_port_poll_wait,
1385     xfif_netdev_port_group_get,
1386     xfif_netdev_port_group_set,
1387     xfif_netdev_flow_get,
1388     xfif_netdev_flow_put,
1389     xfif_netdev_flow_del,
1390     xfif_netdev_flow_flush,
1391     xfif_netdev_flow_list,
1392     xfif_netdev_execute,
1393     xfif_netdev_recv_get_mask,
1394     xfif_netdev_recv_set_mask,
1395     NULL,                       /* get_sflow_probability */
1396     NULL,                       /* set_sflow_probability */
1397     NULL,                       /* queue_to_priority */
1398     xfif_netdev_recv,
1399     xfif_netdev_recv_wait,
1400 };