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