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