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