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