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