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