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