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