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