dpif-linux: Close channel Netlink sockets when a port number gets recycled.
[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 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 ? put->actions : &dummy_action;
810     request->actions_len = put->actions_len;
811     if (put->flags & DPIF_FP_ZERO_STATS) {
812         request->clear = true;
813     }
814     request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
815 }
816
817 static int
818 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
819 {
820     struct dpif_linux_flow request, reply;
821     struct ofpbuf *buf;
822     int error;
823
824     dpif_linux_init_flow_put(dpif_, put, &request);
825     error = dpif_linux_flow_transact(&request,
826                                      put->stats ? &reply : NULL,
827                                      put->stats ? &buf : NULL);
828     if (!error && put->stats) {
829         dpif_linux_flow_get_stats(&reply, put->stats);
830         ofpbuf_delete(buf);
831     }
832     return error;
833 }
834
835 static void
836 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
837                          struct dpif_linux_flow *request)
838 {
839     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
840
841     dpif_linux_flow_init(request);
842     request->cmd = OVS_FLOW_CMD_DEL;
843     request->dp_ifindex = dpif->dp_ifindex;
844     request->key = del->key;
845     request->key_len = del->key_len;
846 }
847
848 static int
849 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
850 {
851     struct dpif_linux_flow request, reply;
852     struct ofpbuf *buf;
853     int error;
854
855     dpif_linux_init_flow_del(dpif_, del, &request);
856     error = dpif_linux_flow_transact(&request,
857                                      del->stats ? &reply : NULL,
858                                      del->stats ? &buf : NULL);
859     if (!error && del->stats) {
860         dpif_linux_flow_get_stats(&reply, del->stats);
861         ofpbuf_delete(buf);
862     }
863     return error;
864 }
865
866 struct dpif_linux_flow_state {
867     struct nl_dump dump;
868     struct dpif_linux_flow flow;
869     struct dpif_flow_stats stats;
870     struct ofpbuf *buf;
871 };
872
873 static int
874 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
875 {
876     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
877     struct dpif_linux_flow_state *state;
878     struct dpif_linux_flow request;
879     struct ofpbuf *buf;
880
881     *statep = state = xmalloc(sizeof *state);
882
883     dpif_linux_flow_init(&request);
884     request.cmd = OVS_DP_CMD_GET;
885     request.dp_ifindex = dpif->dp_ifindex;
886
887     buf = ofpbuf_new(1024);
888     dpif_linux_flow_to_ofpbuf(&request, buf);
889     nl_dump_start(&state->dump, genl_sock, buf);
890     ofpbuf_delete(buf);
891
892     state->buf = NULL;
893
894     return 0;
895 }
896
897 static int
898 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
899                           const struct nlattr **key, size_t *key_len,
900                           const struct nlattr **actions, size_t *actions_len,
901                           const struct dpif_flow_stats **stats)
902 {
903     struct dpif_linux_flow_state *state = state_;
904     struct ofpbuf buf;
905     int error;
906
907     do {
908         ofpbuf_delete(state->buf);
909         state->buf = NULL;
910
911         if (!nl_dump_next(&state->dump, &buf)) {
912             return EOF;
913         }
914
915         error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
916         if (error) {
917             return error;
918         }
919
920         if (actions && !state->flow.actions) {
921             error = dpif_linux_flow_get__(dpif_, state->flow.key,
922                                           state->flow.key_len,
923                                           &state->flow, &state->buf);
924             if (error == ENOENT) {
925                 VLOG_DBG("dumped flow disappeared on get");
926             } else if (error) {
927                 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
928             }
929         }
930     } while (error);
931
932     if (actions) {
933         *actions = state->flow.actions;
934         *actions_len = state->flow.actions_len;
935     }
936     if (key) {
937         *key = state->flow.key;
938         *key_len = state->flow.key_len;
939     }
940     if (stats) {
941         dpif_linux_flow_get_stats(&state->flow, &state->stats);
942         *stats = &state->stats;
943     }
944     return error;
945 }
946
947 static int
948 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
949 {
950     struct dpif_linux_flow_state *state = state_;
951     int error = nl_dump_done(&state->dump);
952     ofpbuf_delete(state->buf);
953     free(state);
954     return error;
955 }
956
957 static void
958 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
959                           struct ofpbuf *buf)
960 {
961     struct ovs_header *k_exec;
962
963     ofpbuf_prealloc_tailroom(buf, (64
964                                    + d_exec->packet->size
965                                    + d_exec->key_len
966                                    + d_exec->actions_len));
967
968     nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
969                           OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
970
971     k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
972     k_exec->dp_ifindex = dp_ifindex;
973
974     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
975                       d_exec->packet->data, d_exec->packet->size);
976     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
977     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
978                       d_exec->actions, d_exec->actions_len);
979 }
980
981 static int
982 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
983 {
984     uint64_t request_stub[1024 / 8];
985     struct ofpbuf request;
986     int error;
987
988     ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
989     dpif_linux_encode_execute(dp_ifindex, execute, &request);
990     error = nl_sock_transact(genl_sock, &request, NULL);
991     ofpbuf_uninit(&request);
992
993     return error;
994 }
995
996 static int
997 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
998 {
999     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1000
1001     return dpif_linux_execute__(dpif->dp_ifindex, execute);
1002 }
1003
1004 #define MAX_OPS 50
1005
1006 static void
1007 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
1008 {
1009     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1010
1011     struct op_auxdata {
1012         struct nl_transaction txn;
1013
1014         struct ofpbuf request;
1015         uint64_t request_stub[1024 / 8];
1016
1017         struct ofpbuf reply;
1018         uint64_t reply_stub[1024 / 8];
1019     } auxes[MAX_OPS];
1020
1021     struct nl_transaction *txnsp[MAX_OPS];
1022     size_t i;
1023
1024     ovs_assert(n_ops <= MAX_OPS);
1025     for (i = 0; i < n_ops; i++) {
1026         struct op_auxdata *aux = &auxes[i];
1027         struct dpif_op *op = ops[i];
1028         struct dpif_flow_put *put;
1029         struct dpif_flow_del *del;
1030         struct dpif_execute *execute;
1031         struct dpif_linux_flow flow;
1032
1033         ofpbuf_use_stub(&aux->request,
1034                         aux->request_stub, sizeof aux->request_stub);
1035         aux->txn.request = &aux->request;
1036
1037         ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1038         aux->txn.reply = NULL;
1039
1040         switch (op->type) {
1041         case DPIF_OP_FLOW_PUT:
1042             put = &op->u.flow_put;
1043             dpif_linux_init_flow_put(dpif_, put, &flow);
1044             if (put->stats) {
1045                 flow.nlmsg_flags |= NLM_F_ECHO;
1046                 aux->txn.reply = &aux->reply;
1047             }
1048             dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1049             break;
1050
1051         case DPIF_OP_FLOW_DEL:
1052             del = &op->u.flow_del;
1053             dpif_linux_init_flow_del(dpif_, del, &flow);
1054             if (del->stats) {
1055                 flow.nlmsg_flags |= NLM_F_ECHO;
1056                 aux->txn.reply = &aux->reply;
1057             }
1058             dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1059             break;
1060
1061         case DPIF_OP_EXECUTE:
1062             execute = &op->u.execute;
1063             dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1064                                       &aux->request);
1065             break;
1066
1067         default:
1068             NOT_REACHED();
1069         }
1070     }
1071
1072     for (i = 0; i < n_ops; i++) {
1073         txnsp[i] = &auxes[i].txn;
1074     }
1075     nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1076
1077     for (i = 0; i < n_ops; i++) {
1078         struct op_auxdata *aux = &auxes[i];
1079         struct nl_transaction *txn = &auxes[i].txn;
1080         struct dpif_op *op = ops[i];
1081         struct dpif_flow_put *put;
1082         struct dpif_flow_del *del;
1083
1084         op->error = txn->error;
1085
1086         switch (op->type) {
1087         case DPIF_OP_FLOW_PUT:
1088             put = &op->u.flow_put;
1089             if (put->stats) {
1090                 if (!op->error) {
1091                     struct dpif_linux_flow reply;
1092
1093                     op->error = dpif_linux_flow_from_ofpbuf(&reply,
1094                                                             txn->reply);
1095                     if (!op->error) {
1096                         dpif_linux_flow_get_stats(&reply, put->stats);
1097                     }
1098                 }
1099
1100                 if (op->error) {
1101                     memset(put->stats, 0, sizeof *put->stats);
1102                 }
1103             }
1104             break;
1105
1106         case DPIF_OP_FLOW_DEL:
1107             del = &op->u.flow_del;
1108             if (del->stats) {
1109                 if (!op->error) {
1110                     struct dpif_linux_flow reply;
1111
1112                     op->error = dpif_linux_flow_from_ofpbuf(&reply,
1113                                                             txn->reply);
1114                     if (!op->error) {
1115                         dpif_linux_flow_get_stats(&reply, del->stats);
1116                     }
1117                 }
1118
1119                 if (op->error) {
1120                     memset(del->stats, 0, sizeof *del->stats);
1121                 }
1122             }
1123             break;
1124
1125         case DPIF_OP_EXECUTE:
1126             break;
1127
1128         default:
1129             NOT_REACHED();
1130         }
1131
1132         ofpbuf_uninit(&aux->request);
1133         ofpbuf_uninit(&aux->reply);
1134     }
1135 }
1136
1137 static void
1138 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1139 {
1140     while (n_ops > 0) {
1141         size_t chunk = MIN(n_ops, MAX_OPS);
1142         dpif_linux_operate__(dpif, ops, chunk);
1143         ops += chunk;
1144         n_ops -= chunk;
1145     }
1146 }
1147
1148 static int
1149 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1150 {
1151     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1152
1153     if ((dpif->epoll_fd >= 0) == enable) {
1154         return 0;
1155     }
1156
1157     if (!enable) {
1158         destroy_channels(dpif);
1159     } else {
1160         struct dpif_port_dump port_dump;
1161         struct dpif_port port;
1162
1163         dpif->epoll_fd = epoll_create(10);
1164         if (dpif->epoll_fd < 0) {
1165             return errno;
1166         }
1167
1168         DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1169             struct dpif_linux_vport vport_request;
1170             struct nl_sock *sock;
1171             uint32_t upcall_pid;
1172             int error;
1173
1174             error = nl_sock_create(NETLINK_GENERIC, &sock);
1175             if (error) {
1176                 return error;
1177             }
1178
1179             upcall_pid = nl_sock_pid(sock);
1180
1181             dpif_linux_vport_init(&vport_request);
1182             vport_request.cmd = OVS_VPORT_CMD_SET;
1183             vport_request.dp_ifindex = dpif->dp_ifindex;
1184             vport_request.port_no = port.port_no;
1185             vport_request.upcall_pid = &upcall_pid;
1186             error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1187             if (!error) {
1188                 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1189                          dpif_name(&dpif->dpif), vport_request.port_no,
1190                          upcall_pid);
1191             } else {
1192                 VLOG_WARN_RL(&error_rl,
1193                              "%s: failed to set upcall pid on port: %s",
1194                              dpif_name(&dpif->dpif), strerror(error));
1195                 nl_sock_destroy(sock);
1196
1197                 if (error == ENODEV || error == ENOENT) {
1198                     /* This device isn't there, but keep trying the others. */
1199                     continue;
1200                 } else {
1201                     return error;
1202                 }
1203             }
1204
1205             error = add_channel(dpif, port.port_no, sock);
1206             if (error) {
1207                 VLOG_INFO("%s: could not add channel for port %s",
1208                           dpif_name(dpif_), port.name);
1209                 nl_sock_destroy(sock);
1210                 return error;
1211             }
1212         }
1213     }
1214
1215     return 0;
1216 }
1217
1218 static int
1219 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1220                              uint32_t queue_id, uint32_t *priority)
1221 {
1222     if (queue_id < 0xf000) {
1223         *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1224         return 0;
1225     } else {
1226         return EINVAL;
1227     }
1228 }
1229
1230 static int
1231 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1232                  int *dp_ifindex)
1233 {
1234     static const struct nl_policy ovs_packet_policy[] = {
1235         /* Always present. */
1236         [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1237                                      .min_len = ETH_HEADER_LEN },
1238         [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1239
1240         /* OVS_PACKET_CMD_ACTION only. */
1241         [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_UNSPEC, .optional = true },
1242     };
1243
1244     struct ovs_header *ovs_header;
1245     struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1246     struct nlmsghdr *nlmsg;
1247     struct genlmsghdr *genl;
1248     struct ofpbuf b;
1249     int type;
1250
1251     ofpbuf_use_const(&b, buf->data, buf->size);
1252
1253     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1254     genl = ofpbuf_try_pull(&b, sizeof *genl);
1255     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1256     if (!nlmsg || !genl || !ovs_header
1257         || nlmsg->nlmsg_type != ovs_packet_family
1258         || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1259                             ARRAY_SIZE(ovs_packet_policy))) {
1260         return EINVAL;
1261     }
1262
1263     type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1264             : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1265             : -1);
1266     if (type < 0) {
1267         return EINVAL;
1268     }
1269
1270     memset(upcall, 0, sizeof *upcall);
1271     upcall->type = type;
1272     upcall->packet = buf;
1273     upcall->packet->data = CONST_CAST(struct nlattr *,
1274                                       nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1275     upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1276     upcall->key = CONST_CAST(struct nlattr *,
1277                              nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1278     upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1279     upcall->userdata = a[OVS_PACKET_ATTR_USERDATA];
1280     *dp_ifindex = ovs_header->dp_ifindex;
1281
1282     return 0;
1283 }
1284
1285 static int
1286 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1287                 struct ofpbuf *buf)
1288 {
1289     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1290     int read_tries = 0;
1291
1292     if (dpif->epoll_fd < 0) {
1293        return EAGAIN;
1294     }
1295
1296     if (dpif->event_offset >= dpif->n_events) {
1297         int retval;
1298
1299         dpif->event_offset = dpif->n_events = 0;
1300
1301         do {
1302             retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1303                                 dpif->uc_array_size, 0);
1304         } while (retval < 0 && errno == EINTR);
1305         if (retval < 0) {
1306             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1307             VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1308         } else if (retval > 0) {
1309             dpif->n_events = retval;
1310         }
1311     }
1312
1313     while (dpif->event_offset < dpif->n_events) {
1314         int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1315         struct dpif_channel *ch = &dpif->channels[idx];
1316
1317         dpif->event_offset++;
1318
1319         for (;;) {
1320             int dp_ifindex;
1321             int error;
1322
1323             if (++read_tries > 50) {
1324                 return EAGAIN;
1325             }
1326
1327             error = nl_sock_recv(ch->sock, buf, false);
1328             if (error == ENOBUFS) {
1329                 /* ENOBUFS typically means that we've received so many
1330                  * packets that the buffer overflowed.  Try again
1331                  * immediately because there's almost certainly a packet
1332                  * waiting for us. */
1333                 report_loss(dpif_, ch);
1334                 continue;
1335             }
1336
1337             ch->last_poll = time_msec();
1338             if (error) {
1339                 if (error == EAGAIN) {
1340                     break;
1341                 }
1342                 return error;
1343             }
1344
1345             error = parse_odp_packet(buf, upcall, &dp_ifindex);
1346             if (!error && dp_ifindex == dpif->dp_ifindex) {
1347                 return 0;
1348             } else if (error) {
1349                 return error;
1350             }
1351         }
1352     }
1353
1354     return EAGAIN;
1355 }
1356
1357 static void
1358 dpif_linux_recv_wait(struct dpif *dpif_)
1359 {
1360     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1361
1362     if (dpif->epoll_fd < 0) {
1363        return;
1364     }
1365
1366     poll_fd_wait(dpif->epoll_fd, POLLIN);
1367 }
1368
1369 static void
1370 dpif_linux_recv_purge(struct dpif *dpif_)
1371 {
1372     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1373     struct dpif_channel *ch;
1374
1375     if (dpif->epoll_fd < 0) {
1376        return;
1377     }
1378
1379     for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1380         if (ch->sock) {
1381             nl_sock_drain(ch->sock);
1382         }
1383     }
1384 }
1385
1386 const struct dpif_class dpif_linux_class = {
1387     "system",
1388     dpif_linux_enumerate,
1389     NULL,
1390     dpif_linux_open,
1391     dpif_linux_close,
1392     dpif_linux_destroy,
1393     dpif_linux_run,
1394     dpif_linux_wait,
1395     dpif_linux_get_stats,
1396     dpif_linux_port_add,
1397     dpif_linux_port_del,
1398     dpif_linux_port_query_by_number,
1399     dpif_linux_port_query_by_name,
1400     dpif_linux_get_max_ports,
1401     dpif_linux_port_get_pid,
1402     dpif_linux_port_dump_start,
1403     dpif_linux_port_dump_next,
1404     dpif_linux_port_dump_done,
1405     dpif_linux_port_poll,
1406     dpif_linux_port_poll_wait,
1407     dpif_linux_flow_get,
1408     dpif_linux_flow_put,
1409     dpif_linux_flow_del,
1410     dpif_linux_flow_flush,
1411     dpif_linux_flow_dump_start,
1412     dpif_linux_flow_dump_next,
1413     dpif_linux_flow_dump_done,
1414     dpif_linux_execute,
1415     dpif_linux_operate,
1416     dpif_linux_recv_set,
1417     dpif_linux_queue_to_priority,
1418     dpif_linux_recv,
1419     dpif_linux_recv_wait,
1420     dpif_linux_recv_purge,
1421 };
1422 \f
1423 static int
1424 dpif_linux_init(void)
1425 {
1426     static int error = -1;
1427
1428     if (error < 0) {
1429         unsigned int ovs_vport_mcgroup;
1430
1431         error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1432                                       &ovs_datapath_family);
1433         if (error) {
1434             VLOG_ERR("Generic Netlink family '%s' does not exist. "
1435                      "The Open vSwitch kernel module is probably not loaded.",
1436                      OVS_DATAPATH_FAMILY);
1437         }
1438         if (!error) {
1439             error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1440         }
1441         if (!error) {
1442             error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1443         }
1444         if (!error) {
1445             error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1446                                           &ovs_packet_family);
1447         }
1448         if (!error) {
1449             error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1450         }
1451         if (!error) {
1452             error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1453                                            &ovs_vport_mcgroup,
1454                                            OVS_VPORT_MCGROUP_FALLBACK_ID);
1455         }
1456         if (!error) {
1457             static struct dpif_linux_vport vport;
1458             nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1459                              dpif_linux_nln_parse, &vport);
1460         }
1461     }
1462
1463     return error;
1464 }
1465
1466 bool
1467 dpif_linux_is_internal_device(const char *name)
1468 {
1469     struct dpif_linux_vport reply;
1470     struct ofpbuf *buf;
1471     int error;
1472
1473     error = dpif_linux_vport_get(name, &reply, &buf);
1474     if (!error) {
1475         ofpbuf_delete(buf);
1476     } else if (error != ENODEV && error != ENOENT) {
1477         VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1478                      name, strerror(error));
1479     }
1480
1481     return reply.type == OVS_VPORT_TYPE_INTERNAL;
1482 }
1483
1484 static bool
1485 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1486 {
1487     struct dpif_linux_vport *vport = vport_;
1488     return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1489 }
1490
1491 static void
1492 dpif_linux_port_changed(const void *vport_, void *dpif_)
1493 {
1494     const struct dpif_linux_vport *vport = vport_;
1495     struct dpif_linux *dpif = dpif_;
1496
1497     if (vport) {
1498         if (vport->dp_ifindex == dpif->dp_ifindex
1499             && (vport->cmd == OVS_VPORT_CMD_NEW
1500                 || vport->cmd == OVS_VPORT_CMD_DEL
1501                 || vport->cmd == OVS_VPORT_CMD_SET)) {
1502             VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1503                      dpif->dpif.full_name, vport->name, vport->cmd);
1504             sset_add(&dpif->changed_ports, vport->name);
1505         }
1506     } else {
1507         dpif->change_error = true;
1508     }
1509 }
1510 \f
1511 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1512  * by Netlink attributes, into 'vport'.  Returns 0 if successful, otherwise a
1513  * positive errno value.
1514  *
1515  * 'vport' will contain pointers into 'buf', so the caller should not free
1516  * 'buf' while 'vport' is still in use. */
1517 static int
1518 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1519                              const struct ofpbuf *buf)
1520 {
1521     static const struct nl_policy ovs_vport_policy[] = {
1522         [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1523         [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1524         [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1525         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1526         [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1527                                    .optional = true },
1528         [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1529     };
1530
1531     struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1532     struct ovs_header *ovs_header;
1533     struct nlmsghdr *nlmsg;
1534     struct genlmsghdr *genl;
1535     struct ofpbuf b;
1536
1537     dpif_linux_vport_init(vport);
1538
1539     ofpbuf_use_const(&b, buf->data, buf->size);
1540     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1541     genl = ofpbuf_try_pull(&b, sizeof *genl);
1542     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1543     if (!nlmsg || !genl || !ovs_header
1544         || nlmsg->nlmsg_type != ovs_vport_family
1545         || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1546                             ARRAY_SIZE(ovs_vport_policy))) {
1547         return EINVAL;
1548     }
1549
1550     vport->cmd = genl->cmd;
1551     vport->dp_ifindex = ovs_header->dp_ifindex;
1552     vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1553     vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1554     vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1555     if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1556         vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1557     }
1558     if (a[OVS_VPORT_ATTR_STATS]) {
1559         vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1560     }
1561     if (a[OVS_VPORT_ATTR_OPTIONS]) {
1562         vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1563         vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1564     }
1565     return 0;
1566 }
1567
1568 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1569  * followed by Netlink attributes corresponding to 'vport'. */
1570 static void
1571 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1572                            struct ofpbuf *buf)
1573 {
1574     struct ovs_header *ovs_header;
1575
1576     nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1577                           vport->cmd, OVS_VPORT_VERSION);
1578
1579     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1580     ovs_header->dp_ifindex = vport->dp_ifindex;
1581
1582     if (vport->port_no != UINT32_MAX) {
1583         nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1584     }
1585
1586     if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1587         nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1588     }
1589
1590     if (vport->name) {
1591         nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1592     }
1593
1594     if (vport->upcall_pid) {
1595         nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1596     }
1597
1598     if (vport->stats) {
1599         nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1600                           vport->stats, sizeof *vport->stats);
1601     }
1602
1603     if (vport->options) {
1604         nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1605                           vport->options, vport->options_len);
1606     }
1607 }
1608
1609 /* Clears 'vport' to "empty" values. */
1610 void
1611 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1612 {
1613     memset(vport, 0, sizeof *vport);
1614     vport->port_no = UINT32_MAX;
1615 }
1616
1617 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1618  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1619  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1620  * result of the command is expected to be an ovs_vport also, which is decoded
1621  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1622  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1623 int
1624 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1625                           struct dpif_linux_vport *reply,
1626                           struct ofpbuf **bufp)
1627 {
1628     struct ofpbuf *request_buf;
1629     int error;
1630
1631     ovs_assert((reply != NULL) == (bufp != NULL));
1632
1633     error = dpif_linux_init();
1634     if (error) {
1635         if (reply) {
1636             *bufp = NULL;
1637             dpif_linux_vport_init(reply);
1638         }
1639         return error;
1640     }
1641
1642     request_buf = ofpbuf_new(1024);
1643     dpif_linux_vport_to_ofpbuf(request, request_buf);
1644     error = nl_sock_transact(genl_sock, request_buf, bufp);
1645     ofpbuf_delete(request_buf);
1646
1647     if (reply) {
1648         if (!error) {
1649             error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1650         }
1651         if (error) {
1652             dpif_linux_vport_init(reply);
1653             ofpbuf_delete(*bufp);
1654             *bufp = NULL;
1655         }
1656     }
1657     return error;
1658 }
1659
1660 /* Obtains information about the kernel vport named 'name' and stores it into
1661  * '*reply' and '*bufp'.  The caller must free '*bufp' when the reply is no
1662  * longer needed ('reply' will contain pointers into '*bufp').  */
1663 int
1664 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1665                      struct ofpbuf **bufp)
1666 {
1667     struct dpif_linux_vport request;
1668
1669     dpif_linux_vport_init(&request);
1670     request.cmd = OVS_VPORT_CMD_GET;
1671     request.name = name;
1672
1673     return dpif_linux_vport_transact(&request, reply, bufp);
1674 }
1675 \f
1676 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1677  * by Netlink attributes, into 'dp'.  Returns 0 if successful, otherwise a
1678  * positive errno value.
1679  *
1680  * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1681  * while 'dp' is still in use. */
1682 static int
1683 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1684 {
1685     static const struct nl_policy ovs_datapath_policy[] = {
1686         [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1687         [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1688                                 .optional = true },
1689     };
1690
1691     struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1692     struct ovs_header *ovs_header;
1693     struct nlmsghdr *nlmsg;
1694     struct genlmsghdr *genl;
1695     struct ofpbuf b;
1696
1697     dpif_linux_dp_init(dp);
1698
1699     ofpbuf_use_const(&b, buf->data, buf->size);
1700     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1701     genl = ofpbuf_try_pull(&b, sizeof *genl);
1702     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1703     if (!nlmsg || !genl || !ovs_header
1704         || nlmsg->nlmsg_type != ovs_datapath_family
1705         || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1706                             ARRAY_SIZE(ovs_datapath_policy))) {
1707         return EINVAL;
1708     }
1709
1710     dp->cmd = genl->cmd;
1711     dp->dp_ifindex = ovs_header->dp_ifindex;
1712     dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1713     if (a[OVS_DP_ATTR_STATS]) {
1714         /* Can't use structure assignment because Netlink doesn't ensure
1715          * sufficient alignment for 64-bit members. */
1716         memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1717                sizeof dp->stats);
1718     }
1719
1720     return 0;
1721 }
1722
1723 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1724 static void
1725 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1726 {
1727     struct ovs_header *ovs_header;
1728
1729     nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1730                           NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1731                           OVS_DATAPATH_VERSION);
1732
1733     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1734     ovs_header->dp_ifindex = dp->dp_ifindex;
1735
1736     if (dp->name) {
1737         nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1738     }
1739
1740     if (dp->upcall_pid) {
1741         nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1742     }
1743
1744     /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1745 }
1746
1747 /* Clears 'dp' to "empty" values. */
1748 static void
1749 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1750 {
1751     memset(dp, 0, sizeof *dp);
1752 }
1753
1754 static void
1755 dpif_linux_dp_dump_start(struct nl_dump *dump)
1756 {
1757     struct dpif_linux_dp request;
1758     struct ofpbuf *buf;
1759
1760     dpif_linux_dp_init(&request);
1761     request.cmd = OVS_DP_CMD_GET;
1762
1763     buf = ofpbuf_new(1024);
1764     dpif_linux_dp_to_ofpbuf(&request, buf);
1765     nl_dump_start(dump, genl_sock, buf);
1766     ofpbuf_delete(buf);
1767 }
1768
1769 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1770  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1771  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1772  * result of the command is expected to be of the same form, which is decoded
1773  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1774  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1775 static int
1776 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1777                        struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1778 {
1779     struct ofpbuf *request_buf;
1780     int error;
1781
1782     ovs_assert((reply != NULL) == (bufp != NULL));
1783
1784     request_buf = ofpbuf_new(1024);
1785     dpif_linux_dp_to_ofpbuf(request, request_buf);
1786     error = nl_sock_transact(genl_sock, request_buf, bufp);
1787     ofpbuf_delete(request_buf);
1788
1789     if (reply) {
1790         if (!error) {
1791             error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1792         }
1793         if (error) {
1794             dpif_linux_dp_init(reply);
1795             ofpbuf_delete(*bufp);
1796             *bufp = NULL;
1797         }
1798     }
1799     return error;
1800 }
1801
1802 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1803  * The caller must free '*bufp' when the reply is no longer needed ('reply'
1804  * will contain pointers into '*bufp').  */
1805 static int
1806 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1807                   struct ofpbuf **bufp)
1808 {
1809     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1810     struct dpif_linux_dp request;
1811
1812     dpif_linux_dp_init(&request);
1813     request.cmd = OVS_DP_CMD_GET;
1814     request.dp_ifindex = dpif->dp_ifindex;
1815
1816     return dpif_linux_dp_transact(&request, reply, bufp);
1817 }
1818 \f
1819 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1820  * by Netlink attributes, into 'flow'.  Returns 0 if successful, otherwise a
1821  * positive errno value.
1822  *
1823  * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1824  * while 'flow' is still in use. */
1825 static int
1826 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1827                             const struct ofpbuf *buf)
1828 {
1829     static const struct nl_policy ovs_flow_policy[] = {
1830         [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1831         [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1832         [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1833                                   .optional = true },
1834         [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1835         [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1836         /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1837     };
1838
1839     struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1840     struct ovs_header *ovs_header;
1841     struct nlmsghdr *nlmsg;
1842     struct genlmsghdr *genl;
1843     struct ofpbuf b;
1844
1845     dpif_linux_flow_init(flow);
1846
1847     ofpbuf_use_const(&b, buf->data, buf->size);
1848     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1849     genl = ofpbuf_try_pull(&b, sizeof *genl);
1850     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1851     if (!nlmsg || !genl || !ovs_header
1852         || nlmsg->nlmsg_type != ovs_flow_family
1853         || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1854                             ARRAY_SIZE(ovs_flow_policy))) {
1855         return EINVAL;
1856     }
1857
1858     flow->nlmsg_flags = nlmsg->nlmsg_flags;
1859     flow->dp_ifindex = ovs_header->dp_ifindex;
1860     flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1861     flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1862     if (a[OVS_FLOW_ATTR_ACTIONS]) {
1863         flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1864         flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1865     }
1866     if (a[OVS_FLOW_ATTR_STATS]) {
1867         flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1868     }
1869     if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1870         flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1871     }
1872     if (a[OVS_FLOW_ATTR_USED]) {
1873         flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1874     }
1875     return 0;
1876 }
1877
1878 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1879  * followed by Netlink attributes corresponding to 'flow'. */
1880 static void
1881 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1882                           struct ofpbuf *buf)
1883 {
1884     struct ovs_header *ovs_header;
1885
1886     nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1887                           NLM_F_REQUEST | flow->nlmsg_flags,
1888                           flow->cmd, OVS_FLOW_VERSION);
1889
1890     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1891     ovs_header->dp_ifindex = flow->dp_ifindex;
1892
1893     if (flow->key_len) {
1894         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1895     }
1896
1897     if (flow->actions || flow->actions_len) {
1898         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1899                           flow->actions, flow->actions_len);
1900     }
1901
1902     /* We never need to send these to the kernel. */
1903     ovs_assert(!flow->stats);
1904     ovs_assert(!flow->tcp_flags);
1905     ovs_assert(!flow->used);
1906
1907     if (flow->clear) {
1908         nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1909     }
1910 }
1911
1912 /* Clears 'flow' to "empty" values. */
1913 static void
1914 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1915 {
1916     memset(flow, 0, sizeof *flow);
1917 }
1918
1919 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1920  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1921  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1922  * result of the command is expected to be a flow also, which is decoded and
1923  * stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the reply
1924  * is no longer needed ('reply' will contain pointers into '*bufp'). */
1925 static int
1926 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1927                          struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1928 {
1929     struct ofpbuf *request_buf;
1930     int error;
1931
1932     ovs_assert((reply != NULL) == (bufp != NULL));
1933
1934     if (reply) {
1935         request->nlmsg_flags |= NLM_F_ECHO;
1936     }
1937
1938     request_buf = ofpbuf_new(1024);
1939     dpif_linux_flow_to_ofpbuf(request, request_buf);
1940     error = nl_sock_transact(genl_sock, request_buf, bufp);
1941     ofpbuf_delete(request_buf);
1942
1943     if (reply) {
1944         if (!error) {
1945             error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1946         }
1947         if (error) {
1948             dpif_linux_flow_init(reply);
1949             ofpbuf_delete(*bufp);
1950             *bufp = NULL;
1951         }
1952     }
1953     return error;
1954 }
1955
1956 static void
1957 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1958                           struct dpif_flow_stats *stats)
1959 {
1960     if (flow->stats) {
1961         stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1962         stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1963     } else {
1964         stats->n_packets = 0;
1965         stats->n_bytes = 0;
1966     }
1967     stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1968     stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1969 }
1970 \f
1971 /* Logs information about a packet that was recently lost in 'ch' (in
1972  * 'dpif_'). */
1973 static void
1974 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1975 {
1976     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1977     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1978     struct ds s;
1979
1980     if (VLOG_DROP_WARN(&rl)) {
1981         return;
1982     }
1983
1984     ds_init(&s);
1985     if (ch->last_poll != LLONG_MIN) {
1986         ds_put_format(&s, " (last polled %lld ms ago)",
1987                       time_msec() - ch->last_poll);
1988     }
1989
1990     VLOG_WARN("%s: lost packet on channel %td%s",
1991               dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));
1992     ds_destroy(&s);
1993 }