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