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