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