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