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