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