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