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