cbdbbabc569347001d4b7904e4716eddfb5272eb
[cascardo/ovs.git] / lib / dpif-linux.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "dpif-linux.h"
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <net/if.h>
27 #include <linux/types.h>
28 #include <linux/ethtool.h>
29 #include <linux/pkt_sched.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/sockios.h>
32 #include <stdlib.h>
33 #include <sys/ioctl.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 #include "dpif-provider.h"
38 #include "netdev.h"
39 #include "netdev-vport.h"
40 #include "netlink.h"
41 #include "odp-util.h"
42 #include "ofpbuf.h"
43 #include "openvswitch/tunnel.h"
44 #include "packets.h"
45 #include "poll-loop.h"
46 #include "rtnetlink.h"
47 #include "rtnetlink-link.h"
48 #include "shash.h"
49 #include "svec.h"
50 #include "unaligned.h"
51 #include "util.h"
52 #include "vlog.h"
53
54 VLOG_DEFINE_THIS_MODULE(dpif_linux);
55
56 struct dpif_linux_dp {
57     /* ioctl command argument. */
58     int cmd;
59
60     /* struct odp_datapath header. */
61     uint32_t dp_idx;
62
63     /* Attributes. */
64     const char *name;                  /* ODP_DP_ATTR_NAME. */
65     struct odp_stats stats;            /* ODP_DP_ATTR_STATS. */
66     enum odp_frag_handling ipv4_frags; /* ODP_DP_ATTR_IPV4_FRAGS. */
67     const uint32_t *sampling;          /* ODP_DP_ATTR_SAMPLING. */
68 };
69
70 static void dpif_linux_dp_init(struct dpif_linux_dp *);
71 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
72                                   struct dpif_linux_dp *reply,
73                                   struct ofpbuf **bufp);
74 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
75                              struct ofpbuf **bufp);
76
77 struct dpif_linux_flow {
78     /* ioctl command argument. */
79     int cmd;
80
81     /* struct odp_flow header. */
82     unsigned int nlmsg_flags;
83     uint32_t dp_idx;
84
85     /* Attributes.
86      *
87      * The 'stats', 'used', and 'state' members point to 64-bit data that might
88      * only be aligned on 32-bit boundaries, so get_unaligned_u64() should be
89      * used to access their values. */
90     const struct nlattr *key;           /* ODP_FLOW_ATTR_KEY. */
91     size_t key_len;
92     const struct nlattr *actions;       /* ODP_FLOW_ATTR_ACTIONS. */
93     size_t actions_len;
94     const struct odp_flow_stats *stats; /* ODP_FLOW_ATTR_STATS. */
95     const uint8_t *tcp_flags;           /* ODP_FLOW_ATTR_TCP_FLAGS. */
96     const uint64_t *used;               /* ODP_FLOW_ATTR_USED. */
97     bool clear;                         /* ODP_FLOW_ATTR_CLEAR. */
98     const uint64_t *state;              /* ODP_FLOW_ATTR_STATE. */
99 };
100
101 static void dpif_linux_flow_init(struct dpif_linux_flow *);
102 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
103                                     struct dpif_linux_flow *reply,
104                                     struct ofpbuf **bufp);
105 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
106                                       struct dpif_flow_stats *);
107
108 /* Datapath interface for the openvswitch Linux kernel module. */
109 struct dpif_linux {
110     struct dpif dpif;
111     int fd;
112
113     /* Used by dpif_linux_get_all_names(). */
114     char *local_ifname;
115     int minor;
116
117     /* Change notification. */
118     int local_ifindex;          /* Ifindex of local port. */
119     struct shash changed_ports;  /* Ports that have changed. */
120     struct rtnetlink_notifier port_notifier;
121     bool change_error;
122 };
123
124 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
125
126 static int do_ioctl(const struct dpif *, int cmd, const void *arg);
127 static int open_dpif(const struct dpif_linux_vport *local_vport,
128                      struct dpif **);
129 static int get_openvswitch_major(void);
130 static int open_minor(int minor, int *fdp);
131 static int make_openvswitch_device(int minor, char **fnp);
132 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
133                                     void *dpif);
134
135 static struct dpif_linux *
136 dpif_linux_cast(const struct dpif *dpif)
137 {
138     dpif_assert_class(dpif, &dpif_linux_class);
139     return CONTAINER_OF(dpif, struct dpif_linux, dpif);
140 }
141
142 static int
143 dpif_linux_enumerate(struct svec *all_dps)
144 {
145     uint32_t dp_idx;
146     int major;
147
148     /* Check that the Open vSwitch module is loaded. */
149     major = get_openvswitch_major();
150     if (major < 0) {
151         return -major;
152     }
153
154     dp_idx = 0;
155     for (;;) {
156         struct dpif_linux_dp request, reply;
157         struct ofpbuf *buf;
158         char devname[16];
159         int error;
160
161         dpif_linux_dp_init(&request);
162         request.dp_idx = dp_idx;
163         request.cmd = ODP_DP_DUMP;
164
165         error = dpif_linux_dp_transact(&request, &reply, &buf);
166         if (error) {
167             return error == ENODEV ? 0 : error;
168         }
169         ofpbuf_delete(buf);
170
171         sprintf(devname, "dp%d", reply.dp_idx);
172         svec_add(all_dps, devname);
173
174         dp_idx = reply.dp_idx + 1;
175     }
176 }
177
178 static int
179 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
180                 bool create, struct dpif **dpifp)
181 {
182     struct dpif_linux_vport request, reply;
183     struct ofpbuf *buf;
184     int minor;
185     int error;
186
187     minor = !strncmp(name, "dp", 2)
188             && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
189     if (create) {
190         struct dpif_linux_dp request, reply;
191         struct ofpbuf *buf;
192         int error;
193
194         dpif_linux_dp_init(&request);
195         request.cmd = ODP_DP_NEW;
196         request.dp_idx = minor;
197         request.name = name;
198         error = dpif_linux_dp_transact(&request, &reply, &buf);
199         if (error) {
200             return error;
201         }
202         minor = reply.dp_idx;
203         ofpbuf_delete(buf);
204     }
205
206     dpif_linux_vport_init(&request);
207     request.cmd = ODP_VPORT_GET;
208     request.port_no = ODPP_LOCAL;
209     if (minor >= 0) {
210         request.dp_idx = minor;
211     } else {
212         request.name = name;
213     }
214
215     error = dpif_linux_vport_transact(&request, &reply, &buf);
216     if (error) {
217         return error;
218     } else if (reply.port_no != ODPP_LOCAL) {
219         /* This is an Open vSwitch device but not the local port.  We
220          * intentionally support only using the name of the local port as the
221          * name of a datapath; otherwise, it would be too difficult to
222          * enumerate all the names of a datapath. */
223         error = EOPNOTSUPP;
224     } else {
225         error = open_dpif(&reply, dpifp);
226     }
227
228     ofpbuf_delete(buf);
229     return error;
230 }
231
232 static int
233 open_dpif(const struct dpif_linux_vport *local_vport, struct dpif **dpifp)
234 {
235     int dp_idx = local_vport->dp_idx;
236     struct dpif_linux *dpif;
237     char *name;
238     int error;
239     int fd;
240
241     error = open_minor(dp_idx, &fd);
242     if (error) {
243         goto error;
244     }
245
246     dpif = xmalloc(sizeof *dpif);
247     error = rtnetlink_link_notifier_register(&dpif->port_notifier,
248                                              dpif_linux_port_changed, dpif);
249     if (error) {
250         goto error_free;
251     }
252
253     name = xasprintf("dp%d", dp_idx);
254     dpif_init(&dpif->dpif, &dpif_linux_class, name, dp_idx, dp_idx);
255     free(name);
256
257     dpif->fd = fd;
258     dpif->local_ifname = xstrdup(local_vport->name);
259     dpif->local_ifindex = local_vport->ifindex;
260     dpif->minor = dp_idx;
261     shash_init(&dpif->changed_ports);
262     dpif->change_error = false;
263     *dpifp = &dpif->dpif;
264
265     return 0;
266
267 error_free:
268     free(dpif);
269     close(fd);
270 error:
271     return error;
272 }
273
274 static void
275 dpif_linux_close(struct dpif *dpif_)
276 {
277     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
278     rtnetlink_link_notifier_unregister(&dpif->port_notifier);
279     shash_destroy(&dpif->changed_ports);
280     free(dpif->local_ifname);
281     close(dpif->fd);
282     free(dpif);
283 }
284
285 static int
286 dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
287 {
288     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
289
290     svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
291     svec_add(all_names, dpif->local_ifname);
292     return 0;
293 }
294
295 static int
296 dpif_linux_destroy(struct dpif *dpif_)
297 {
298     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
299     struct dpif_linux_dp dp;
300
301     dpif_linux_dp_init(&dp);
302     dp.cmd = ODP_DP_DEL;
303     dp.dp_idx = dpif->minor;
304     return dpif_linux_dp_transact(&dp, NULL, NULL);
305 }
306
307 static int
308 dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
309 {
310     struct dpif_linux_dp dp;
311     struct ofpbuf *buf;
312     int error;
313
314     error = dpif_linux_dp_get(dpif_, &dp, &buf);
315     if (!error) {
316         *stats = dp.stats;
317         ofpbuf_delete(buf);
318     }
319     return error;
320 }
321
322 static int
323 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
324 {
325     struct dpif_linux_dp dp;
326     struct ofpbuf *buf;
327     int error;
328
329     error = dpif_linux_dp_get(dpif_, &dp, &buf);
330     if (!error) {
331         *drop_fragsp = dp.ipv4_frags == ODP_DP_FRAG_DROP;
332         ofpbuf_delete(buf);
333     }
334     return error;
335 }
336
337 static int
338 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
339 {
340     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
341     struct dpif_linux_dp dp;
342
343     dpif_linux_dp_init(&dp);
344     dp.cmd = ODP_DP_SET;
345     dp.dp_idx = dpif->minor;
346     dp.ipv4_frags = drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO;
347     return dpif_linux_dp_transact(&dp, NULL, NULL);
348 }
349
350 static int
351 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
352                     uint16_t *port_nop)
353 {
354     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
355     const char *name = netdev_get_name(netdev);
356     const char *type = netdev_get_type(netdev);
357     struct dpif_linux_vport request, reply;
358     const struct ofpbuf *options;
359     struct ofpbuf *buf;
360     int error;
361
362     dpif_linux_vport_init(&request);
363     request.cmd = ODP_VPORT_NEW;
364     request.dp_idx = dpif->minor;
365     request.type = netdev_vport_get_vport_type(netdev);
366     if (request.type == ODP_VPORT_TYPE_UNSPEC) {
367         VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
368                      "unsupported type `%s'",
369                      dpif_name(dpif_), name, type);
370         return EINVAL;
371     }
372     request.name = name;
373
374     options = netdev_vport_get_options(netdev);
375     if (options && options->size) {
376         request.options = options->data;
377         request.options_len = options->size;
378     }
379
380     error = dpif_linux_vport_transact(&request, &reply, &buf);
381     if (!error) {
382         *port_nop = reply.port_no;
383         ofpbuf_delete(buf);
384     }
385
386     return error;
387 }
388
389 static int
390 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
391 {
392     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
393     struct dpif_linux_vport vport;
394
395     dpif_linux_vport_init(&vport);
396     vport.cmd = ODP_VPORT_DEL;
397     vport.dp_idx = dpif->minor;
398     vport.port_no = port_no;
399     return dpif_linux_vport_transact(&vport, NULL, NULL);
400 }
401
402 static int
403 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
404                         const char *port_name, struct dpif_port *dpif_port)
405 {
406     struct dpif_linux_vport request;
407     struct dpif_linux_vport reply;
408     struct ofpbuf *buf;
409     int error;
410
411     dpif_linux_vport_init(&request);
412     request.cmd = ODP_VPORT_GET;
413     request.dp_idx = dpif_linux_cast(dpif)->minor;
414     request.port_no = port_no;
415     request.name = port_name;
416
417     error = dpif_linux_vport_transact(&request, &reply, &buf);
418     if (!error) {
419         dpif_port->name = xstrdup(reply.name);
420         dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
421         dpif_port->port_no = reply.port_no;
422         ofpbuf_delete(buf);
423     }
424     return error;
425 }
426
427 static int
428 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
429                                 struct dpif_port *dpif_port)
430 {
431     return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
432 }
433
434 static int
435 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
436                               struct dpif_port *dpif_port)
437 {
438     return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
439 }
440
441 static int
442 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
443 {
444     /* If the datapath increases its range of supported ports, then it should
445      * start reporting that. */
446     return 1024;
447 }
448
449 static int
450 dpif_linux_flow_flush(struct dpif *dpif_)
451 {
452     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
453     return ioctl(dpif->fd, ODP_FLOW_FLUSH, dpif->minor) ? errno : 0;
454 }
455
456 struct dpif_linux_port_state {
457     struct ofpbuf *buf;
458     uint32_t next;
459 };
460
461 static int
462 dpif_linux_port_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
463 {
464     *statep = xzalloc(sizeof(struct dpif_linux_port_state));
465     return 0;
466 }
467
468 static int
469 dpif_linux_port_dump_next(const struct dpif *dpif, void *state_,
470                           struct dpif_port *dpif_port)
471 {
472     struct dpif_linux_port_state *state = state_;
473     struct dpif_linux_vport request, reply;
474     struct ofpbuf *buf;
475     int error;
476
477     ofpbuf_delete(state->buf);
478     state->buf = NULL;
479
480     dpif_linux_vport_init(&request);
481     request.cmd = ODP_VPORT_DUMP;
482     request.dp_idx = dpif_linux_cast(dpif)->minor;
483     request.port_no = state->next;
484     error = dpif_linux_vport_transact(&request, &reply, &buf);
485     if (error) {
486         return error == ENODEV ? EOF : error;
487     } else {
488         dpif_port->name = (char *) reply.name;
489         dpif_port->type = (char *) netdev_vport_get_netdev_type(&reply);
490         dpif_port->port_no = reply.port_no;
491         state->buf = buf;
492         state->next = reply.port_no + 1;
493         return 0;
494     }
495 }
496
497 static int
498 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
499 {
500     struct dpif_linux_port_state *state = state_;
501     ofpbuf_delete(state->buf);
502     free(state);
503     return 0;
504 }
505
506 static int
507 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
508 {
509     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
510
511     if (dpif->change_error) {
512         dpif->change_error = false;
513         shash_clear(&dpif->changed_ports);
514         return ENOBUFS;
515     } else if (!shash_is_empty(&dpif->changed_ports)) {
516         struct shash_node *node = shash_first(&dpif->changed_ports);
517         *devnamep = shash_steal(&dpif->changed_ports, node);
518         return 0;
519     } else {
520         return EAGAIN;
521     }
522 }
523
524 static void
525 dpif_linux_port_poll_wait(const struct dpif *dpif_)
526 {
527     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
528     if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
529         poll_immediate_wake();
530     } else {
531         rtnetlink_link_notifier_wait();
532     }
533 }
534
535 static int
536 dpif_linux_flow_get(const struct dpif *dpif_,
537                     const struct nlattr *key, size_t key_len,
538                     struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
539 {
540     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
541     struct dpif_linux_flow request, reply;
542     struct ofpbuf *buf;
543     int error;
544
545     dpif_linux_flow_init(&request);
546     request.cmd = ODP_FLOW_GET;
547     request.dp_idx = dpif->minor;
548     request.key = key;
549     request.key_len = key_len;
550     error = dpif_linux_flow_transact(&request, &reply, &buf);
551     if (!error) {
552         if (stats) {
553             dpif_linux_flow_get_stats(&reply, stats);
554         }
555         if (actionsp) {
556             buf->data = (void *) reply.actions;
557             buf->size = reply.actions_len;
558             *actionsp = buf;
559         } else {
560             ofpbuf_delete(buf);
561         }
562     }
563     return error;
564 }
565
566 static int
567 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
568                     const struct nlattr *key, size_t key_len,
569                     const struct nlattr *actions, size_t actions_len,
570                     struct dpif_flow_stats *stats)
571 {
572     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
573     struct dpif_linux_flow request, reply;
574     struct ofpbuf *buf;
575     int error;
576
577     dpif_linux_flow_init(&request);
578     request.cmd = flags & DPIF_FP_CREATE ? ODP_FLOW_NEW : ODP_FLOW_SET;
579     request.dp_idx = dpif->minor;
580     request.key = key;
581     request.key_len = key_len;
582     request.actions = actions;
583     request.actions_len = actions_len;
584     if (flags & DPIF_FP_ZERO_STATS) {
585         request.clear = true;
586     }
587     request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
588     error = dpif_linux_flow_transact(&request,
589                                      stats ? &reply : NULL,
590                                      stats ? &buf : NULL);
591     if (!error && stats) {
592         dpif_linux_flow_get_stats(&reply, stats);
593         ofpbuf_delete(buf);
594     }
595     return error;
596 }
597
598 static int
599 dpif_linux_flow_del(struct dpif *dpif_,
600                     const struct nlattr *key, size_t key_len,
601                     struct dpif_flow_stats *stats)
602 {
603     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
604     struct dpif_linux_flow request, reply;
605     struct ofpbuf *buf;
606     int error;
607
608     dpif_linux_flow_init(&request);
609     request.cmd = ODP_FLOW_DEL;
610     request.dp_idx = dpif->minor;
611     request.key = key;
612     request.key_len = key_len;
613     error = dpif_linux_flow_transact(&request,
614                                      stats ? &reply : NULL,
615                                      stats ? &buf : NULL);
616     if (!error && stats) {
617         dpif_linux_flow_get_stats(&reply, stats);
618         ofpbuf_delete(buf);
619     }
620     return error;
621 }
622
623
624 struct dpif_linux_flow_state {
625     struct dpif_linux_flow flow;
626     struct ofpbuf *buf;
627     struct dpif_flow_stats stats;
628 };
629
630 static int
631 dpif_linux_flow_dump_start(const struct dpif *dpif OVS_UNUSED, void **statep)
632 {
633     *statep = xzalloc(sizeof(struct dpif_linux_flow_state));
634     return 0;
635 }
636
637 static int
638 dpif_linux_flow_dump_next(const struct dpif *dpif_, void *state_,
639                           const struct nlattr **key, size_t *key_len,
640                           const struct nlattr **actions, size_t *actions_len,
641                           const struct dpif_flow_stats **stats)
642 {
643     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
644     struct dpif_linux_flow_state *state = state_;
645     struct ofpbuf *old_buf = state->buf;
646     struct dpif_linux_flow request;
647     int error;
648
649     dpif_linux_flow_init(&request);
650     request.cmd = ODP_FLOW_DUMP;
651     request.dp_idx = dpif->minor;
652     request.state = state->flow.state;
653     error = dpif_linux_flow_transact(&request, &state->flow, &state->buf);
654     ofpbuf_delete(old_buf);
655
656     if (!error) {
657         if (key) {
658             *key = state->flow.key;
659             *key_len = state->flow.key_len;
660         }
661         if (actions) {
662             *actions = state->flow.actions;
663             *actions_len = state->flow.actions_len;
664         }
665         if (stats) {
666             dpif_linux_flow_get_stats(&state->flow, &state->stats);
667             *stats = &state->stats;
668         }
669     }
670     return error == ENODEV ? EOF : error;
671 }
672
673 static int
674 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
675 {
676     struct dpif_linux_flow_state *state = state_;
677
678     ofpbuf_delete(state->buf);
679     free(state);
680     return 0;
681 }
682
683 static int
684 dpif_linux_execute(struct dpif *dpif_,
685                    const struct nlattr *actions, size_t actions_len,
686                    const struct ofpbuf *packet)
687 {
688     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
689     struct odp_packet *execute;
690     struct ofpbuf *buf;
691     int error;
692
693     buf = ofpbuf_new(128 + actions_len + packet->size);
694
695     ofpbuf_reserve(buf, sizeof *execute);
696     nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, packet->data, packet->size);
697     nl_msg_put_unspec(buf, ODP_PACKET_ATTR_ACTIONS, actions, actions_len);
698
699     execute = ofpbuf_push_uninit(buf, sizeof *execute);
700     execute->dp_idx = dpif->minor;
701     execute->len = buf->size;
702
703     error = do_ioctl(dpif_, ODP_EXECUTE, buf->data);
704
705     ofpbuf_delete(buf);
706     return error;
707 }
708
709 static int
710 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
711 {
712     return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
713 }
714
715 static int
716 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
717 {
718     return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
719 }
720
721 static int
722 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
723                                  uint32_t *probability)
724 {
725     struct dpif_linux_dp dp;
726     struct ofpbuf *buf;
727     int error;
728
729     error = dpif_linux_dp_get(dpif_, &dp, &buf);
730     if (!error) {
731         *probability = dp.sampling ? *dp.sampling : 0;
732         ofpbuf_delete(buf);
733     }
734     return error;
735 }
736
737 static int
738 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
739 {
740     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
741     struct dpif_linux_dp dp;
742
743     dpif_linux_dp_init(&dp);
744     dp.cmd = ODP_DP_SET;
745     dp.dp_idx = dpif->minor;
746     dp.sampling = &probability;
747     return dpif_linux_dp_transact(&dp, NULL, NULL);
748 }
749
750 static int
751 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
752                              uint32_t queue_id, uint32_t *priority)
753 {
754     if (queue_id < 0xf000) {
755         *priority = TC_H_MAKE(1 << 16, queue_id + 1);
756         return 0;
757     } else {
758         return EINVAL;
759     }
760 }
761
762 static int
763 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall)
764 {
765     static const struct nl_policy odp_packet_policy[] = {
766         /* Always present. */
767         [ODP_PACKET_ATTR_TYPE] = { .type = NL_A_U32 },
768         [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
769                                      .min_len = ETH_HEADER_LEN },
770         [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
771
772         /* _ODPL_ACTION_NR only. */
773         [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
774
775         /* _ODPL_SFLOW_NR only. */
776         [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
777         [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
778     };
779
780     struct odp_packet *odp_packet = buf->data;
781     struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
782     uint32_t type;
783
784     if (!nl_policy_parse(buf, sizeof *odp_packet, odp_packet_policy,
785                          a, ARRAY_SIZE(odp_packet_policy))) {
786         return EINVAL;
787     }
788
789     memset(upcall, 0, sizeof *upcall);
790
791     type = nl_attr_get_u32(a[ODP_PACKET_ATTR_TYPE]);
792     upcall->type = (type == _ODPL_MISS_NR ? DPIF_UC_MISS
793                     : type == _ODPL_ACTION_NR ? DPIF_UC_ACTION
794                     : type == _ODPL_SFLOW_NR ? DPIF_UC_SAMPLE
795                     : -1);
796
797     upcall->packet = buf;
798     upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
799     upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
800     upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
801     upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
802     upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
803                         ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
804                         : 0);
805     upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
806                         ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
807                            : 0);
808     if (a[ODP_PACKET_ATTR_ACTIONS]) {
809         upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
810         upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
811     }
812
813     return 0;
814 }
815
816 static int
817 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
818 {
819     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
820     struct ofpbuf *buf;
821     int retval;
822     int error;
823
824     buf = ofpbuf_new(65536);
825     retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
826     if (retval < 0) {
827         error = errno;
828         if (error != EAGAIN) {
829             VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
830                          dpif_name(dpif_), strerror(error));
831         }
832     } else if (retval >= sizeof(struct odp_packet)) {
833         struct odp_packet *odp_packet = buf->data;
834         buf->size += retval;
835
836         if (odp_packet->len <= retval) {
837             error = parse_odp_packet(buf, upcall);
838         } else {
839             VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
840                          "from %"PRIu32" bytes to %d",
841                          dpif_name(dpif_), odp_packet->len, retval);
842             error = ERANGE;
843         }
844     } else if (!retval) {
845         VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
846         error = EPROTO;
847     } else {
848         VLOG_WARN_RL(&error_rl, "%s: discarding too-short message (%d bytes)",
849                      dpif_name(dpif_), retval);
850         error = ERANGE;
851     }
852
853     if (error) {
854         ofpbuf_delete(buf);
855     }
856     return error;
857 }
858
859 static void
860 dpif_linux_recv_wait(struct dpif *dpif_)
861 {
862     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
863     poll_fd_wait(dpif->fd, POLLIN);
864 }
865
866 static void
867 dpif_linux_recv_purge(struct dpif *dpif_)
868 {
869     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
870     int i;
871
872     /* This is somewhat bogus because it assumes that the following macros have
873      * fixed values, but it's going to go away later.  */
874 #define DP_N_QUEUES 3
875 #define DP_MAX_QUEUE_LEN 100
876     for (i = 0; i < DP_N_QUEUES * DP_MAX_QUEUE_LEN; i++) {
877         /* Reading even 1 byte discards a whole datagram and saves time. */
878         char buffer;
879
880         if (read(dpif->fd, &buffer, 1) != 1) {
881             break;
882         }
883     }
884 }
885
886 const struct dpif_class dpif_linux_class = {
887     "system",
888     NULL,
889     NULL,
890     dpif_linux_enumerate,
891     dpif_linux_open,
892     dpif_linux_close,
893     dpif_linux_get_all_names,
894     dpif_linux_destroy,
895     dpif_linux_get_stats,
896     dpif_linux_get_drop_frags,
897     dpif_linux_set_drop_frags,
898     dpif_linux_port_add,
899     dpif_linux_port_del,
900     dpif_linux_port_query_by_number,
901     dpif_linux_port_query_by_name,
902     dpif_linux_get_max_ports,
903     dpif_linux_port_dump_start,
904     dpif_linux_port_dump_next,
905     dpif_linux_port_dump_done,
906     dpif_linux_port_poll,
907     dpif_linux_port_poll_wait,
908     dpif_linux_flow_get,
909     dpif_linux_flow_put,
910     dpif_linux_flow_del,
911     dpif_linux_flow_flush,
912     dpif_linux_flow_dump_start,
913     dpif_linux_flow_dump_next,
914     dpif_linux_flow_dump_done,
915     dpif_linux_execute,
916     dpif_linux_recv_get_mask,
917     dpif_linux_recv_set_mask,
918     dpif_linux_get_sflow_probability,
919     dpif_linux_set_sflow_probability,
920     dpif_linux_queue_to_priority,
921     dpif_linux_recv,
922     dpif_linux_recv_wait,
923     dpif_linux_recv_purge,
924 };
925 \f
926 static int get_openvswitch_major(void);
927 static int get_major(const char *target);
928
929 static int
930 do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
931 {
932     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
933     return ioctl(dpif->fd, cmd, arg) ? errno : 0;
934 }
935
936 bool
937 dpif_linux_is_internal_device(const char *name)
938 {
939     struct dpif_linux_vport reply;
940     struct ofpbuf *buf;
941     int error;
942
943     error = dpif_linux_vport_get(name, &reply, &buf);
944     if (!error) {
945         ofpbuf_delete(buf);
946     } else if (error != ENODEV) {
947         VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
948                      name, strerror(error));
949     }
950
951     return reply.type == ODP_VPORT_TYPE_INTERNAL;
952 }
953
954 static int
955 make_openvswitch_device(int minor, char **fnp)
956 {
957     const char dirname[] = "/dev/net";
958     int major;
959     dev_t dev;
960     struct stat s;
961     char fn[128];
962
963     *fnp = NULL;
964
965     major = get_openvswitch_major();
966     if (major < 0) {
967         return -major;
968     }
969     dev = makedev(major, minor);
970
971     sprintf(fn, "%s/dp%d", dirname, minor);
972     if (!stat(fn, &s)) {
973         if (!S_ISCHR(s.st_mode)) {
974             VLOG_WARN_RL(&error_rl, "%s is not a character device, fixing",
975                          fn);
976         } else if (s.st_rdev != dev) {
977             VLOG_WARN_RL(&error_rl,
978                          "%s is device %u:%u but should be %u:%u, fixing",
979                          fn, major(s.st_rdev), minor(s.st_rdev),
980                          major(dev), minor(dev));
981         } else {
982             goto success;
983         }
984         if (unlink(fn)) {
985             VLOG_WARN_RL(&error_rl, "%s: unlink failed (%s)",
986                          fn, strerror(errno));
987             return errno;
988         }
989     } else if (errno == ENOENT) {
990         if (stat(dirname, &s)) {
991             if (errno == ENOENT) {
992                 if (mkdir(dirname, 0755)) {
993                     VLOG_WARN_RL(&error_rl, "%s: mkdir failed (%s)",
994                                  dirname, strerror(errno));
995                     return errno;
996                 }
997             } else {
998                 VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)",
999                              dirname, strerror(errno));
1000                 return errno;
1001             }
1002         }
1003     } else {
1004         VLOG_WARN_RL(&error_rl, "%s: stat failed (%s)", fn, strerror(errno));
1005         return errno;
1006     }
1007
1008     /* The device needs to be created. */
1009     if (mknod(fn, S_IFCHR | 0700, dev)) {
1010         VLOG_WARN_RL(&error_rl,
1011                      "%s: creating character device %u:%u failed (%s)",
1012                      fn, major(dev), minor(dev), strerror(errno));
1013         return errno;
1014     }
1015
1016 success:
1017     *fnp = xstrdup(fn);
1018     return 0;
1019 }
1020
1021 /* Return the major device number of the Open vSwitch device.  If it
1022  * cannot be determined, a negative errno is returned. */
1023 static int
1024 get_openvswitch_major(void)
1025 {
1026     static int openvswitch_major = -1;
1027     if (openvswitch_major < 0) {
1028         openvswitch_major = get_major("openvswitch");
1029     }
1030     return openvswitch_major;
1031 }
1032
1033 static int
1034 get_major(const char *target)
1035 {
1036     const char fn[] = "/proc/devices";
1037     char line[128];
1038     FILE *file;
1039     int ln;
1040
1041     file = fopen(fn, "r");
1042     if (!file) {
1043         VLOG_ERR("opening %s failed (%s)", fn, strerror(errno));
1044         return -errno;
1045     }
1046
1047     for (ln = 1; fgets(line, sizeof line, file); ln++) {
1048         char name[64];
1049         int major;
1050
1051         if (!strncmp(line, "Character", 9) || line[0] == '\0') {
1052             /* Nothing to do. */
1053         } else if (!strncmp(line, "Block", 5)) {
1054             /* We only want character devices, so skip the rest of the file. */
1055             break;
1056         } else if (sscanf(line, "%d %63s", &major, name)) {
1057             if (!strcmp(name, target)) {
1058                 fclose(file);
1059                 return major;
1060             }
1061         } else {
1062             VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
1063         }
1064     }
1065
1066     fclose(file);
1067
1068     VLOG_ERR("%s: %s major not found (is the module loaded?)", fn, target);
1069     return -ENODEV;
1070 }
1071
1072 static int
1073 open_minor(int minor, int *fdp)
1074 {
1075     int error;
1076     char *fn;
1077
1078     error = make_openvswitch_device(minor, &fn);
1079     if (error) {
1080         return error;
1081     }
1082
1083     *fdp = open(fn, O_RDONLY | O_NONBLOCK);
1084     if (*fdp < 0) {
1085         error = errno;
1086         VLOG_WARN("%s: open failed (%s)", fn, strerror(error));
1087         free(fn);
1088         return error;
1089     }
1090     free(fn);
1091     return 0;
1092 }
1093
1094 static void
1095 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1096                         void *dpif_)
1097 {
1098     struct dpif_linux *dpif = dpif_;
1099
1100     if (change) {
1101         if (change->master_ifindex == dpif->local_ifindex
1102             && (change->nlmsg_type == RTM_NEWLINK
1103                 || change->nlmsg_type == RTM_DELLINK))
1104         {
1105             /* Our datapath changed, either adding a new port or deleting an
1106              * existing one. */
1107             shash_add_once(&dpif->changed_ports, change->ifname, NULL);
1108         }
1109     } else {
1110         dpif->change_error = true;
1111     }
1112 }
1113
1114 static int
1115 get_dp0_fd(int *dp0_fdp)
1116 {
1117     static int dp0_fd = -1;
1118     if (dp0_fd < 0) {
1119         int error;
1120         int fd;
1121
1122         error = open_minor(0, &fd);
1123         if (error) {
1124             return error;
1125         }
1126         dp0_fd = fd;
1127     }
1128     *dp0_fdp = dp0_fd;
1129     return 0;
1130 }
1131 \f
1132 /* Parses the contents of 'buf', which contains a "struct odp_vport" followed
1133  * by Netlink attributes, into 'vport'.  Returns 0 if successful, otherwise a
1134  * positive errno value.
1135  *
1136  * 'vport' will contain pointers into 'buf', so the caller should not free
1137  * 'buf' while 'vport' is still in use. */
1138 static int
1139 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1140                              const struct ofpbuf *buf)
1141 {
1142     static const struct nl_policy odp_vport_policy[] = {
1143         [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1144         [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1145         [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1146         [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1147                                    .min_len = sizeof(struct rtnl_link_stats64),
1148                                    .max_len = sizeof(struct rtnl_link_stats64),
1149                                    .optional = true },
1150         [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1151                                      .min_len = ETH_ADDR_LEN,
1152                                      .max_len = ETH_ADDR_LEN,
1153                                      .optional = true },
1154         [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1155         [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1156         [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1157         [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1158     };
1159
1160     struct odp_vport *odp_vport;
1161     struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1162
1163     dpif_linux_vport_init(vport);
1164
1165     if (!nl_policy_parse(buf, sizeof *odp_vport, odp_vport_policy,
1166                          a, ARRAY_SIZE(odp_vport_policy))) {
1167         return EINVAL;
1168     }
1169     odp_vport = buf->data;
1170
1171     vport->dp_idx = odp_vport->dp_idx;
1172     vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1173     vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1174     vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1175     if (a[ODP_VPORT_ATTR_STATS]) {
1176         vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1177     }
1178     if (a[ODP_VPORT_ATTR_ADDRESS]) {
1179         vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1180     }
1181     if (a[ODP_VPORT_ATTR_MTU]) {
1182         vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1183     }
1184     if (a[ODP_VPORT_ATTR_OPTIONS]) {
1185         vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1186         vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1187     }
1188     if (a[ODP_VPORT_ATTR_IFINDEX]) {
1189         vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1190     }
1191     if (a[ODP_VPORT_ATTR_IFLINK]) {
1192         vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1193     }
1194     return 0;
1195 }
1196
1197 /* Appends to 'buf' (which must initially be empty) a "struct odp_vport"
1198  * followed by Netlink attributes corresponding to 'vport'. */
1199 static void
1200 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1201                            struct ofpbuf *buf)
1202 {
1203     struct odp_vport *odp_vport;
1204
1205     ofpbuf_reserve(buf, sizeof odp_vport);
1206
1207     if (vport->port_no != UINT32_MAX) {
1208         nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1209     }
1210
1211     if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1212         nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1213     }
1214
1215     if (vport->name) {
1216         nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1217     }
1218
1219     if (vport->stats) {
1220         nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1221                           vport->stats, sizeof *vport->stats);
1222     }
1223
1224     if (vport->address) {
1225         nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1226                           vport->address, ETH_ADDR_LEN);
1227     }
1228
1229     if (vport->mtu) {
1230         nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1231     }
1232
1233     if (vport->options) {
1234         nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1235                           vport->options, vport->options_len);
1236     }
1237
1238     if (vport->ifindex) {
1239         nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1240     }
1241
1242     if (vport->iflink) {
1243         nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1244     }
1245
1246     odp_vport = ofpbuf_push_uninit(buf, sizeof *odp_vport);
1247     odp_vport->dp_idx = vport->dp_idx;
1248     odp_vport->len = buf->size;
1249     odp_vport->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1250 }
1251
1252 /* Clears 'vport' to "empty" values. */
1253 void
1254 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1255 {
1256     memset(vport, 0, sizeof *vport);
1257     vport->dp_idx = UINT32_MAX;
1258     vport->port_no = UINT32_MAX;
1259 }
1260
1261 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1262  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1263  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1264  * result of the command is expected to be an odp_vport also, which is decoded
1265  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1266  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1267 int
1268 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1269                           struct dpif_linux_vport *reply,
1270                           struct ofpbuf **bufp)
1271 {
1272     struct ofpbuf *buf = NULL;
1273     int error;
1274     int fd;
1275
1276     assert((reply != NULL) == (bufp != NULL));
1277
1278     error = get_dp0_fd(&fd);
1279     if (error) {
1280         goto error;
1281     }
1282
1283     buf = ofpbuf_new(1024);
1284     dpif_linux_vport_to_ofpbuf(request, buf);
1285
1286     error = ioctl(fd, request->cmd, buf->data) ? errno : 0;
1287     if (error) {
1288         goto error;
1289     }
1290
1291     if (bufp) {
1292         buf->size = ((struct odp_vport *) buf->data)->len;
1293         error = dpif_linux_vport_from_ofpbuf(reply, buf);
1294         if (error) {
1295             goto error;
1296         }
1297         *bufp = buf;
1298     } else {
1299         ofpbuf_delete(buf);
1300     }
1301     return 0;
1302
1303 error:
1304     ofpbuf_delete(buf);
1305     if (bufp) {
1306         memset(reply, 0, sizeof *reply);
1307         *bufp = NULL;
1308     }
1309     return error;
1310 }
1311
1312 /* Obtains information about the kernel vport named 'name' and stores it into
1313  * '*reply' and '*bufp'.  The caller must free '*bufp' when the reply is no
1314  * longer needed ('reply' will contain pointers into '*bufp').  */
1315 int
1316 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1317                      struct ofpbuf **bufp)
1318 {
1319     struct dpif_linux_vport request;
1320
1321     dpif_linux_vport_init(&request);
1322     request.cmd = ODP_VPORT_GET;
1323     request.name = name;
1324
1325     return dpif_linux_vport_transact(&request, reply, bufp);
1326 }
1327 \f
1328 /* Parses the contents of 'buf', which contains a "struct odp_datapath"
1329  * followed by Netlink attributes, into 'dp'.  Returns 0 if successful,
1330  * otherwise a positive errno value.
1331  *
1332  * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1333  * while 'dp' is still in use. */
1334 static int
1335 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1336 {
1337     static const struct nl_policy odp_datapath_policy[] = {
1338         [ODP_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1339         [ODP_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1340                                 .min_len = sizeof(struct odp_stats),
1341                                 .max_len = sizeof(struct odp_stats),
1342                                 .optional = true },
1343         [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1344         [ODP_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1345     };
1346
1347     struct odp_datapath *odp_dp;
1348     struct nlattr *a[ARRAY_SIZE(odp_datapath_policy)];
1349
1350     dpif_linux_dp_init(dp);
1351
1352     if (!nl_policy_parse(buf, sizeof *odp_dp, odp_datapath_policy,
1353                          a, ARRAY_SIZE(odp_datapath_policy))) {
1354         return EINVAL;
1355     }
1356     odp_dp = buf->data;
1357
1358     dp->dp_idx = odp_dp->dp_idx;
1359     dp->name = nl_attr_get_string(a[ODP_DP_ATTR_NAME]);
1360     if (a[ODP_DP_ATTR_STATS]) {
1361         /* Can't use structure assignment because Netlink doesn't ensure
1362          * sufficient alignment for 64-bit members. */
1363         memcpy(&dp->stats, nl_attr_get(a[ODP_DP_ATTR_STATS]),
1364                sizeof dp->stats);
1365     }
1366     if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1367         dp->ipv4_frags = nl_attr_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1368     }
1369     if (a[ODP_DP_ATTR_SAMPLING]) {
1370         dp->sampling = nl_attr_get(a[ODP_DP_ATTR_SAMPLING]);
1371     }
1372     return 0;
1373 }
1374
1375 /* Appends to 'buf' (which must initially be empty) a "struct odp_datapath"
1376  * followed by Netlink attributes corresponding to 'dp'. */
1377 static void
1378 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1379 {
1380     struct odp_datapath *odp_dp;
1381
1382     ofpbuf_reserve(buf, sizeof odp_dp);
1383
1384     if (dp->name) {
1385         nl_msg_put_string(buf, ODP_DP_ATTR_NAME, dp->name);
1386     }
1387
1388     /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1389
1390     if (dp->ipv4_frags) {
1391         nl_msg_put_u32(buf, ODP_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1392     }
1393
1394     if (dp->sampling) {
1395         nl_msg_put_u32(buf, ODP_DP_ATTR_SAMPLING, *dp->sampling);
1396     }
1397
1398     odp_dp = ofpbuf_push_uninit(buf, sizeof *odp_dp);
1399     odp_dp->dp_idx = dp->dp_idx;
1400     odp_dp->len = buf->size;
1401     odp_dp->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1402 }
1403
1404 /* Clears 'dp' to "empty" values. */
1405 void
1406 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1407 {
1408     memset(dp, 0, sizeof *dp);
1409     dp->dp_idx = -1;
1410 }
1411
1412 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1413  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1414  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1415  * result of the command is expected to be an odp_datapath also, which is
1416  * decoded and stored in '*reply' and '*bufp'.  The caller must free '*bufp'
1417  * when the reply is no longer needed ('reply' will contain pointers into
1418  * '*bufp'). */
1419 int
1420 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1421                        struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1422 {
1423     struct ofpbuf *buf = NULL;
1424     int error;
1425     int fd;
1426
1427     assert((reply != NULL) == (bufp != NULL));
1428
1429     error = get_dp0_fd(&fd);
1430     if (error) {
1431         goto error;
1432     }
1433
1434     buf = ofpbuf_new(1024);
1435     dpif_linux_dp_to_ofpbuf(request, buf);
1436
1437     error = ioctl(fd, request->cmd, buf->data) ? errno : 0;
1438     if (error) {
1439         goto error;
1440     }
1441
1442     if (bufp) {
1443         buf->size = ((struct odp_datapath *) buf->data)->len;
1444         error = dpif_linux_dp_from_ofpbuf(reply, buf);
1445         if (error) {
1446             goto error;
1447         }
1448         *bufp = buf;
1449     } else {
1450         ofpbuf_delete(buf);
1451     }
1452     return 0;
1453
1454 error:
1455     ofpbuf_delete(buf);
1456     if (bufp) {
1457         memset(reply, 0, sizeof *reply);
1458         *bufp = NULL;
1459     }
1460     return error;
1461 }
1462
1463 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1464  * The caller must free '*bufp' when the reply is no longer needed ('reply'
1465  * will contain pointers into '*bufp').  */
1466 int
1467 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1468                   struct ofpbuf **bufp)
1469 {
1470     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1471     struct dpif_linux_dp request;
1472
1473     dpif_linux_dp_init(&request);
1474     request.cmd = ODP_DP_GET;
1475     request.dp_idx = dpif->minor;
1476
1477     return dpif_linux_dp_transact(&request, reply, bufp);
1478 }
1479 \f
1480 /* Parses the contents of 'buf', which contains a "struct odp_flow" followed by
1481  * Netlink attributes, into 'flow'.  Returns 0 if successful, otherwise a
1482  * positive errno value.
1483  *
1484  * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1485  * while 'flow' is still in use. */
1486 static int
1487 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1488                             const struct ofpbuf *buf)
1489 {
1490     static const struct nl_policy odp_flow_policy[] = {
1491         [ODP_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1492         [ODP_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1493         [ODP_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1494                                   .min_len = sizeof(struct odp_flow_stats),
1495                                   .max_len = sizeof(struct odp_flow_stats),
1496                                   .optional = true },
1497         [ODP_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1498         [ODP_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1499         /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1500         [ODP_FLOW_ATTR_STATE] = { .type = NL_A_U64, .optional = true },
1501     };
1502
1503     struct odp_flow *odp_flow;
1504     struct nlattr *a[ARRAY_SIZE(odp_flow_policy)];
1505
1506     dpif_linux_flow_init(flow);
1507
1508     if (!nl_policy_parse(buf, sizeof *odp_flow, odp_flow_policy,
1509                          a, ARRAY_SIZE(odp_flow_policy))) {
1510         return EINVAL;
1511     }
1512     odp_flow = buf->data;
1513
1514     flow->nlmsg_flags = odp_flow->nlmsg_flags;
1515     flow->dp_idx = odp_flow->dp_idx;
1516     flow->key = nl_attr_get(a[ODP_FLOW_ATTR_KEY]);
1517     flow->key_len = nl_attr_get_size(a[ODP_FLOW_ATTR_KEY]);
1518     if (a[ODP_FLOW_ATTR_ACTIONS]) {
1519         flow->actions = nl_attr_get(a[ODP_FLOW_ATTR_ACTIONS]);
1520         flow->actions_len = nl_attr_get_size(a[ODP_FLOW_ATTR_ACTIONS]);
1521     }
1522     if (a[ODP_FLOW_ATTR_STATS]) {
1523         flow->stats = nl_attr_get(a[ODP_FLOW_ATTR_STATS]);
1524     }
1525     if (a[ODP_FLOW_ATTR_TCP_FLAGS]) {
1526         flow->tcp_flags = nl_attr_get(a[ODP_FLOW_ATTR_TCP_FLAGS]);
1527     }
1528     if (a[ODP_FLOW_ATTR_STATE]) {
1529         flow->state = nl_attr_get(a[ODP_FLOW_ATTR_STATE]);
1530     }
1531     return 0;
1532 }
1533
1534 /* Appends to 'buf' (which must initially be empty) a "struct odp_flow"
1535  * followed by Netlink attributes corresponding to 'flow'. */
1536 static void
1537 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1538                           struct ofpbuf *buf)
1539 {
1540     struct odp_flow *odp_flow;
1541
1542     ofpbuf_reserve(buf, sizeof odp_flow);
1543
1544     if (flow->key_len) {
1545         nl_msg_put_unspec(buf, ODP_FLOW_ATTR_KEY, flow->key, flow->key_len);
1546     }
1547
1548     if (flow->actions_len) {
1549         nl_msg_put_unspec(buf, ODP_FLOW_ATTR_ACTIONS,
1550                           flow->actions, flow->actions_len);
1551     }
1552
1553     /* We never need to send these to the kernel. */
1554     assert(!flow->stats);
1555     assert(!flow->tcp_flags);
1556     assert(!flow->used);
1557
1558     if (flow->clear) {
1559         nl_msg_put_flag(buf, ODP_FLOW_ATTR_CLEAR);
1560     }
1561
1562     if (flow->state) {
1563         nl_msg_put_u64(buf, ODP_FLOW_ATTR_STATE,
1564                        get_unaligned_u64(flow->state));
1565     }
1566
1567     odp_flow = ofpbuf_push_uninit(buf, sizeof *odp_flow);
1568     odp_flow->nlmsg_flags = flow->nlmsg_flags;
1569     odp_flow->dp_idx = flow->dp_idx;
1570     odp_flow->len = buf->size;
1571     odp_flow->total_len = (char *) ofpbuf_end(buf) - (char *) buf->data;
1572 }
1573
1574 /* Clears 'flow' to "empty" values. */
1575 void
1576 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1577 {
1578     memset(flow, 0, sizeof *flow);
1579 }
1580
1581 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1582  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1583  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1584  * result of the command is expected to be an odp_flow also, which is decoded
1585  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1586  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1587 int
1588 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1589                          struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1590 {
1591     struct ofpbuf *buf = NULL;
1592     int error;
1593     int fd;
1594
1595     assert((reply != NULL) == (bufp != NULL));
1596
1597     error = get_dp0_fd(&fd);
1598     if (error) {
1599         goto error;
1600     }
1601
1602     buf = ofpbuf_new(1024);
1603     dpif_linux_flow_to_ofpbuf(request, buf);
1604
1605     error = ioctl(fd, request->cmd, buf->data) ? errno : 0;
1606     if (error) {
1607         goto error;
1608     }
1609
1610     if (bufp) {
1611         buf->size = ((struct odp_flow *) buf->data)->len;
1612         error = dpif_linux_flow_from_ofpbuf(reply, buf);
1613         if (error) {
1614             goto error;
1615         }
1616         *bufp = buf;
1617     } else {
1618         ofpbuf_delete(buf);
1619     }
1620     return 0;
1621
1622 error:
1623     ofpbuf_delete(buf);
1624     if (bufp) {
1625         memset(reply, 0, sizeof *reply);
1626         *bufp = NULL;
1627     }
1628     return error;
1629 }
1630
1631 static void
1632 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1633                           struct dpif_flow_stats *stats)
1634 {
1635     if (flow->stats) {
1636         stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1637         stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1638     } else {
1639         stats->n_packets = 0;
1640         stats->n_bytes = 0;
1641     }
1642     stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1643     stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1644 }
1645