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