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