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