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