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