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