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