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