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