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