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