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