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