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