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