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