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