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