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