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