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