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