netdev-vport: Build on all platforms.
[sliver-openvswitch.git] / lib / dpif-linux.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include "dpif-linux.h"
20
21 #include <ctype.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <net/if.h>
26 #include <linux/types.h>
27 #include <linux/pkt_sched.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/sockios.h>
30 #include <poll.h>
31 #include <stdlib.h>
32 #include <strings.h>
33 #include <sys/epoll.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 #include "bitmap.h"
38 #include "dpif-provider.h"
39 #include "dynamic-string.h"
40 #include "flow.h"
41 #include "netdev.h"
42 #include "netdev-linux.h"
43 #include "netdev-vport.h"
44 #include "netlink-notifier.h"
45 #include "netlink-socket.h"
46 #include "netlink.h"
47 #include "odp-util.h"
48 #include "ofpbuf.h"
49 #include "openvswitch/datapath-compat.h"
50 #include "openvswitch/tunnel.h"
51 #include "packets.h"
52 #include "poll-loop.h"
53 #include "random.h"
54 #include "shash.h"
55 #include "sset.h"
56 #include "timeval.h"
57 #include "unaligned.h"
58 #include "util.h"
59 #include "vlog.h"
60
61 VLOG_DEFINE_THIS_MODULE(dpif_linux);
62 enum { MAX_PORTS = USHRT_MAX };
63
64 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
65  * missing if we have old headers. */
66 #define ETH_FLAG_LRO      (1 << 15)    /* LRO is enabled */
67
68 struct dpif_linux_dp {
69     /* Generic Netlink header. */
70     uint8_t cmd;
71
72     /* struct ovs_header. */
73     int dp_ifindex;
74
75     /* Attributes. */
76     const char *name;                  /* OVS_DP_ATTR_NAME. */
77     const uint32_t *upcall_pid;        /* OVS_DP_UPCALL_PID. */
78     struct ovs_dp_stats stats;         /* OVS_DP_ATTR_STATS. */
79 };
80
81 static void dpif_linux_dp_init(struct dpif_linux_dp *);
82 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
83                                      const struct ofpbuf *);
84 static void dpif_linux_dp_dump_start(struct nl_dump *);
85 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
86                                   struct dpif_linux_dp *reply,
87                                   struct ofpbuf **bufp);
88 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
89                              struct ofpbuf **bufp);
90
91 struct dpif_linux_flow {
92     /* Generic Netlink header. */
93     uint8_t cmd;
94
95     /* struct ovs_header. */
96     unsigned int nlmsg_flags;
97     int dp_ifindex;
98
99     /* Attributes.
100      *
101      * The 'stats' member points to 64-bit data that might only be aligned on
102      * 32-bit boundaries, so get_unaligned_u64() should be used to access its
103      * values.
104      *
105      * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
106      * the Netlink version of the command, even if actions_len is zero. */
107     const struct nlattr *key;           /* OVS_FLOW_ATTR_KEY. */
108     size_t key_len;
109     const struct nlattr *actions;       /* OVS_FLOW_ATTR_ACTIONS. */
110     size_t actions_len;
111     const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
112     const uint8_t *tcp_flags;           /* OVS_FLOW_ATTR_TCP_FLAGS. */
113     const ovs_32aligned_u64 *used;      /* OVS_FLOW_ATTR_USED. */
114     bool clear;                         /* OVS_FLOW_ATTR_CLEAR. */
115 };
116
117 static void dpif_linux_flow_init(struct dpif_linux_flow *);
118 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
119                                        const struct ofpbuf *);
120 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
121                                       struct ofpbuf *);
122 static int dpif_linux_flow_transact(struct dpif_linux_flow *request,
123                                     struct dpif_linux_flow *reply,
124                                     struct ofpbuf **bufp);
125 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
126                                       struct dpif_flow_stats *);
127
128 /* One of the dpif channels between the kernel and userspace. */
129 struct dpif_channel {
130     struct nl_sock *sock;       /* Netlink socket. */
131     long long int last_poll;    /* Last time this channel was polled. */
132 };
133
134 static void report_loss(struct dpif *, struct dpif_channel *);
135
136 /* Datapath interface for the openvswitch Linux kernel module. */
137 struct dpif_linux {
138     struct dpif dpif;
139     int dp_ifindex;
140
141     /* Upcall messages. */
142     int uc_array_size;          /* Size of 'channels' and 'epoll_events'. */
143     struct dpif_channel *channels;
144     struct epoll_event *epoll_events;
145     int epoll_fd;               /* epoll fd that includes channel socks. */
146     int n_events;               /* Num events returned by epoll_wait(). */
147     int event_offset;           /* Offset into 'epoll_events'. */
148
149     /* Change notification. */
150     struct sset changed_ports;  /* Ports that have changed. */
151     struct nln_notifier *port_notifier;
152     bool change_error;
153 };
154
155 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
156
157 /* Generic Netlink family numbers for OVS. */
158 static int ovs_datapath_family;
159 static int ovs_vport_family;
160 static int ovs_flow_family;
161 static int ovs_packet_family;
162
163 /* Generic Netlink socket. */
164 static struct nl_sock *genl_sock;
165 static struct nln *nln = NULL;
166
167 static int dpif_linux_init(void);
168 static void open_dpif(const struct dpif_linux_dp *, struct dpif **);
169 static bool dpif_linux_nln_parse(struct ofpbuf *, void *);
170 static void dpif_linux_port_changed(const void *vport, void *dpif);
171 static uint32_t dpif_linux_port_get_pid(const struct dpif *, uint32_t port_no);
172
173 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
174                                        struct ofpbuf *);
175 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
176                                         const struct ofpbuf *);
177
178 static struct dpif_linux *
179 dpif_linux_cast(const struct dpif *dpif)
180 {
181     dpif_assert_class(dpif, &dpif_linux_class);
182     return CONTAINER_OF(dpif, struct dpif_linux, dpif);
183 }
184
185 static int
186 dpif_linux_enumerate(struct sset *all_dps)
187 {
188     struct nl_dump dump;
189     struct ofpbuf msg;
190     int error;
191
192     error = dpif_linux_init();
193     if (error) {
194         return error;
195     }
196
197     dpif_linux_dp_dump_start(&dump);
198     while (nl_dump_next(&dump, &msg)) {
199         struct dpif_linux_dp dp;
200
201         if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
202             sset_add(all_dps, dp.name);
203         }
204     }
205     return nl_dump_done(&dump);
206 }
207
208 static int
209 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
210                 bool create, struct dpif **dpifp)
211 {
212     struct dpif_linux_dp dp_request, dp;
213     struct ofpbuf *buf;
214     uint32_t upcall_pid;
215     int error;
216
217     error = dpif_linux_init();
218     if (error) {
219         return error;
220     }
221
222     /* Create or look up datapath. */
223     dpif_linux_dp_init(&dp_request);
224     if (create) {
225         dp_request.cmd = OVS_DP_CMD_NEW;
226         upcall_pid = 0;
227         dp_request.upcall_pid = &upcall_pid;
228     } else {
229         dp_request.cmd = OVS_DP_CMD_GET;
230     }
231     dp_request.name = name;
232     error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
233     if (error) {
234         return error;
235     }
236
237     open_dpif(&dp, dpifp);
238     ofpbuf_delete(buf);
239     return 0;
240 }
241
242 static void
243 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
244 {
245     struct dpif_linux *dpif;
246
247     dpif = xzalloc(sizeof *dpif);
248     dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
249                                               dpif);
250     dpif->epoll_fd = -1;
251
252     dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
253               dp->dp_ifindex, dp->dp_ifindex);
254
255     dpif->dp_ifindex = dp->dp_ifindex;
256     sset_init(&dpif->changed_ports);
257     *dpifp = &dpif->dpif;
258 }
259
260 static void
261 destroy_channels(struct dpif_linux *dpif)
262 {
263     int i;
264
265     if (dpif->epoll_fd < 0) {
266         return;
267     }
268
269     for (i = 0; i < dpif->uc_array_size; i++ ) {
270         struct dpif_linux_vport vport_request;
271         struct dpif_channel *ch = &dpif->channels[i];
272         uint32_t upcall_pid = 0;
273
274         if (!ch->sock) {
275             continue;
276         }
277
278         /* Turn off upcalls. */
279         dpif_linux_vport_init(&vport_request);
280         vport_request.cmd = OVS_VPORT_CMD_SET;
281         vport_request.dp_ifindex = dpif->dp_ifindex;
282         vport_request.port_no = i;
283         vport_request.upcall_pid = &upcall_pid;
284         dpif_linux_vport_transact(&vport_request, NULL, NULL);
285
286         nl_sock_destroy(ch->sock);
287     }
288
289     free(dpif->channels);
290     dpif->channels = NULL;
291     dpif->uc_array_size = 0;
292
293     free(dpif->epoll_events);
294     dpif->epoll_events = NULL;
295     dpif->n_events = dpif->event_offset = 0;
296
297     close(dpif->epoll_fd);
298     dpif->epoll_fd = -1;
299 }
300
301 static int
302 add_channel(struct dpif_linux *dpif, uint32_t port_no, struct nl_sock *sock)
303 {
304     struct epoll_event event;
305
306     if (dpif->epoll_fd < 0) {
307         return 0;
308     }
309
310     /* We assume that the datapath densely chooses port numbers, which
311      * can therefore be used as an index into an array of channels. */
312     if (port_no >= dpif->uc_array_size) {
313         int new_size = port_no + 1;
314         int i;
315
316         if (new_size > 65535) {
317             VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
318                          dpif_name(&dpif->dpif), port_no);
319             return EFBIG;
320         }
321
322         dpif->channels = xrealloc(dpif->channels,
323                                   new_size * sizeof *dpif->channels);
324         for (i = dpif->uc_array_size; i < new_size; i++) {
325             dpif->channels[i].sock = NULL;
326         }
327
328         dpif->epoll_events = xrealloc(dpif->epoll_events,
329                                       new_size * sizeof *dpif->epoll_events);
330         dpif->uc_array_size = new_size;
331     }
332
333     memset(&event, 0, sizeof event);
334     event.events = EPOLLIN;
335     event.data.u32 = port_no;
336     if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
337                   &event) < 0) {
338         return errno;
339     }
340
341     dpif->channels[port_no].sock = sock;
342     dpif->channels[port_no].last_poll = LLONG_MIN;
343
344     return 0;
345 }
346
347 static void
348 del_channel(struct dpif_linux *dpif, uint32_t port_no)
349 {
350     struct dpif_channel *ch;
351
352     if (dpif->epoll_fd < 0 || port_no >= dpif->uc_array_size) {
353         return;
354     }
355
356     ch = &dpif->channels[port_no];
357     if (!ch->sock) {
358         return;
359     }
360
361     epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
362
363     nl_sock_destroy(ch->sock);
364     ch->sock = NULL;
365 }
366
367 static void
368 dpif_linux_close(struct dpif *dpif_)
369 {
370     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
371
372     nln_notifier_destroy(dpif->port_notifier);
373     destroy_channels(dpif);
374     sset_destroy(&dpif->changed_ports);
375     free(dpif);
376 }
377
378 static int
379 dpif_linux_destroy(struct dpif *dpif_)
380 {
381     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
382     struct dpif_linux_dp dp;
383
384     dpif_linux_dp_init(&dp);
385     dp.cmd = OVS_DP_CMD_DEL;
386     dp.dp_ifindex = dpif->dp_ifindex;
387     return dpif_linux_dp_transact(&dp, NULL, NULL);
388 }
389
390 static void
391 dpif_linux_run(struct dpif *dpif_ OVS_UNUSED)
392 {
393     if (nln) {
394         nln_run(nln);
395     }
396 }
397
398 static void
399 dpif_linux_wait(struct dpif *dpif OVS_UNUSED)
400 {
401     if (nln) {
402         nln_wait(nln);
403     }
404 }
405
406 static int
407 dpif_linux_get_stats(const struct dpif *dpif_, struct dpif_dp_stats *stats)
408 {
409     struct dpif_linux_dp dp;
410     struct ofpbuf *buf;
411     int error;
412
413     error = dpif_linux_dp_get(dpif_, &dp, &buf);
414     if (!error) {
415         stats->n_hit    = dp.stats.n_hit;
416         stats->n_missed = dp.stats.n_missed;
417         stats->n_lost   = dp.stats.n_lost;
418         stats->n_flows  = dp.stats.n_flows;
419         ofpbuf_delete(buf);
420     }
421     return error;
422 }
423
424 static const char *
425 get_vport_type(const struct dpif_linux_vport *vport)
426 {
427     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
428
429     switch (vport->type) {
430     case OVS_VPORT_TYPE_NETDEV:
431         return "system";
432
433     case OVS_VPORT_TYPE_INTERNAL:
434         return "internal";
435
436     case OVS_VPORT_TYPE_GRE:
437         return "gre";
438
439     case OVS_VPORT_TYPE_GRE64:
440         return "gre64";
441
442     case OVS_VPORT_TYPE_CAPWAP:
443         return "capwap";
444
445     case OVS_VPORT_TYPE_VXLAN:
446         return "vxlan";
447
448     case OVS_VPORT_TYPE_UNSPEC:
449     case __OVS_VPORT_TYPE_MAX:
450         break;
451     }
452
453     VLOG_WARN_RL(&rl, "dp%d: port `%s' has unsupported type %u",
454                  vport->dp_ifindex, vport->name, (unsigned int) vport->type);
455     return "unknown";
456 }
457
458 static enum ovs_vport_type
459 netdev_to_ovs_vport_type(const struct netdev *netdev)
460 {
461     const char *type = netdev_get_type(netdev);
462
463     if (!strcmp(type, "tap") || !strcmp(type, "system")) {
464         return OVS_VPORT_TYPE_NETDEV;
465     } else if (!strcmp(type, "internal")) {
466         return OVS_VPORT_TYPE_INTERNAL;
467     } else if (strstr(type, "gre64")) {
468         return OVS_VPORT_TYPE_GRE64;
469     } else if (strstr(type, "gre")) {
470         return OVS_VPORT_TYPE_GRE;
471     } else if (!strcmp(type, "capwap")) {
472         return OVS_VPORT_TYPE_CAPWAP;
473     } else if (!strcmp(type, "vxlan")) {
474         return OVS_VPORT_TYPE_VXLAN;
475     } else {
476         return OVS_VPORT_TYPE_UNSPEC;
477     }
478 }
479
480 static int
481 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
482                     uint32_t *port_nop)
483 {
484     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
485     const char *name = netdev_vport_get_dpif_port(netdev);
486     const char *type = netdev_get_type(netdev);
487     struct dpif_linux_vport request, reply;
488     struct nl_sock *sock = NULL;
489     uint32_t upcall_pid;
490     struct ofpbuf *buf;
491     int error;
492
493     if (dpif->epoll_fd >= 0) {
494         error = nl_sock_create(NETLINK_GENERIC, &sock);
495         if (error) {
496             return error;
497         }
498     }
499
500     dpif_linux_vport_init(&request);
501     request.cmd = OVS_VPORT_CMD_NEW;
502     request.dp_ifindex = dpif->dp_ifindex;
503     request.type = netdev_to_ovs_vport_type(netdev);
504     if (request.type == OVS_VPORT_TYPE_UNSPEC) {
505         VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
506                      "unsupported type `%s'",
507                      dpif_name(dpif_), name, type);
508         nl_sock_destroy(sock);
509         return EINVAL;
510     }
511     request.name = name;
512
513     if (request.type == OVS_VPORT_TYPE_NETDEV) {
514         netdev_linux_ethtool_set_flag(netdev, ETH_FLAG_LRO, "LRO", false);
515     }
516
517     request.port_no = *port_nop;
518     upcall_pid = sock ? nl_sock_pid(sock) : 0;
519     request.upcall_pid = &upcall_pid;
520
521     error = dpif_linux_vport_transact(&request, &reply, &buf);
522     if (!error) {
523         *port_nop = reply.port_no;
524         VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
525                  dpif_name(dpif_), reply.port_no, upcall_pid);
526     } else {
527         if (error == EBUSY && *port_nop != UINT32_MAX) {
528             VLOG_INFO("%s: requested port %"PRIu32" is in use",
529                       dpif_name(dpif_), *port_nop);
530         }
531         nl_sock_destroy(sock);
532         ofpbuf_delete(buf);
533         return error;
534     }
535     ofpbuf_delete(buf);
536
537     if (sock) {
538         error = add_channel(dpif, *port_nop, sock);
539         if (error) {
540             VLOG_INFO("%s: could not add channel for port %s",
541                       dpif_name(dpif_), name);
542
543             /* Delete the port. */
544             dpif_linux_vport_init(&request);
545             request.cmd = OVS_VPORT_CMD_DEL;
546             request.dp_ifindex = dpif->dp_ifindex;
547             request.port_no = *port_nop;
548             dpif_linux_vport_transact(&request, NULL, NULL);
549
550             nl_sock_destroy(sock);
551             return error;
552         }
553     }
554
555     return 0;
556 }
557
558 static int
559 dpif_linux_port_del(struct dpif *dpif_, uint32_t port_no)
560 {
561     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
562     struct dpif_linux_vport vport;
563     int error;
564
565     dpif_linux_vport_init(&vport);
566     vport.cmd = OVS_VPORT_CMD_DEL;
567     vport.dp_ifindex = dpif->dp_ifindex;
568     vport.port_no = port_no;
569     error = dpif_linux_vport_transact(&vport, NULL, NULL);
570
571     del_channel(dpif, port_no);
572
573     return error;
574 }
575
576 static int
577 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
578                         const char *port_name, struct dpif_port *dpif_port)
579 {
580     struct dpif_linux_vport request;
581     struct dpif_linux_vport reply;
582     struct ofpbuf *buf;
583     int error;
584
585     dpif_linux_vport_init(&request);
586     request.cmd = OVS_VPORT_CMD_GET;
587     request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
588     request.port_no = port_no;
589     request.name = port_name;
590
591     error = dpif_linux_vport_transact(&request, &reply, &buf);
592     if (!error) {
593         if (reply.dp_ifindex != request.dp_ifindex) {
594             /* A query by name reported that 'port_name' is in some datapath
595              * other than 'dpif', but the caller wants to know about 'dpif'. */
596             error = ENODEV;
597         } else if (dpif_port) {
598             dpif_port->name = xstrdup(reply.name);
599             dpif_port->type = xstrdup(get_vport_type(&reply));
600             dpif_port->port_no = reply.port_no;
601         }
602         ofpbuf_delete(buf);
603     }
604     return error;
605 }
606
607 static int
608 dpif_linux_port_query_by_number(const struct dpif *dpif, uint32_t port_no,
609                                 struct dpif_port *dpif_port)
610 {
611     return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
612 }
613
614 static int
615 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
616                               struct dpif_port *dpif_port)
617 {
618     return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
619 }
620
621 static int
622 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
623 {
624     return MAX_PORTS;
625 }
626
627 static uint32_t
628 dpif_linux_port_get_pid(const struct dpif *dpif_, uint32_t port_no)
629 {
630     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
631
632     if (dpif->epoll_fd < 0) {
633         return 0;
634     } else {
635         /* The UINT32_MAX "reserved" port number uses the "ovs-system"'s
636          * channel, since it is not heavily loaded. */
637         int idx = (port_no >= dpif->uc_array_size) ? 0 : port_no;
638         return nl_sock_pid(dpif->channels[idx].sock);
639     }
640 }
641
642 static int
643 dpif_linux_flow_flush(struct dpif *dpif_)
644 {
645     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
646     struct dpif_linux_flow flow;
647
648     dpif_linux_flow_init(&flow);
649     flow.cmd = OVS_FLOW_CMD_DEL;
650     flow.dp_ifindex = dpif->dp_ifindex;
651     return dpif_linux_flow_transact(&flow, NULL, NULL);
652 }
653
654 struct dpif_linux_port_state {
655     struct nl_dump dump;
656 };
657
658 static int
659 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
660 {
661     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
662     struct dpif_linux_port_state *state;
663     struct dpif_linux_vport request;
664     struct ofpbuf *buf;
665
666     *statep = state = xmalloc(sizeof *state);
667
668     dpif_linux_vport_init(&request);
669     request.cmd = OVS_DP_CMD_GET;
670     request.dp_ifindex = dpif->dp_ifindex;
671
672     buf = ofpbuf_new(1024);
673     dpif_linux_vport_to_ofpbuf(&request, buf);
674     nl_dump_start(&state->dump, genl_sock, buf);
675     ofpbuf_delete(buf);
676
677     return 0;
678 }
679
680 static int
681 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
682                           struct dpif_port *dpif_port)
683 {
684     struct dpif_linux_port_state *state = state_;
685     struct dpif_linux_vport vport;
686     struct ofpbuf buf;
687     int error;
688
689     if (!nl_dump_next(&state->dump, &buf)) {
690         return EOF;
691     }
692
693     error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
694     if (error) {
695         return error;
696     }
697
698     dpif_port->name = CONST_CAST(char *, vport.name);
699     dpif_port->type = CONST_CAST(char *, get_vport_type(&vport));
700     dpif_port->port_no = vport.port_no;
701     return 0;
702 }
703
704 static int
705 dpif_linux_port_dump_done(const struct dpif *dpif_ OVS_UNUSED, void *state_)
706 {
707     struct dpif_linux_port_state *state = state_;
708     int error = nl_dump_done(&state->dump);
709
710     free(state);
711     return error;
712 }
713
714 static int
715 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
716 {
717     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
718
719     if (dpif->change_error) {
720         dpif->change_error = false;
721         sset_clear(&dpif->changed_ports);
722         return ENOBUFS;
723     } else if (!sset_is_empty(&dpif->changed_ports)) {
724         *devnamep = sset_pop(&dpif->changed_ports);
725         return 0;
726     } else {
727         return EAGAIN;
728     }
729 }
730
731 static void
732 dpif_linux_port_poll_wait(const struct dpif *dpif_)
733 {
734     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
735     if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
736         poll_immediate_wake();
737     }
738 }
739
740 static int
741 dpif_linux_flow_get__(const struct dpif *dpif_,
742                       const struct nlattr *key, size_t key_len,
743                       struct dpif_linux_flow *reply, struct ofpbuf **bufp)
744 {
745     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
746     struct dpif_linux_flow request;
747
748     dpif_linux_flow_init(&request);
749     request.cmd = OVS_FLOW_CMD_GET;
750     request.dp_ifindex = dpif->dp_ifindex;
751     request.key = key;
752     request.key_len = key_len;
753     return dpif_linux_flow_transact(&request, reply, bufp);
754 }
755
756 static int
757 dpif_linux_flow_get(const struct dpif *dpif_,
758                     const struct nlattr *key, size_t key_len,
759                     struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
760 {
761     struct dpif_linux_flow reply;
762     struct ofpbuf *buf;
763     int error;
764
765     error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
766     if (!error) {
767         if (stats) {
768             dpif_linux_flow_get_stats(&reply, stats);
769         }
770         if (actionsp) {
771             buf->data = CONST_CAST(struct nlattr *, reply.actions);
772             buf->size = reply.actions_len;
773             *actionsp = buf;
774         } else {
775             ofpbuf_delete(buf);
776         }
777     }
778     return error;
779 }
780
781 static void
782 dpif_linux_init_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put,
783                          struct dpif_linux_flow *request)
784 {
785     static struct nlattr dummy_action;
786
787     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
788
789     dpif_linux_flow_init(request);
790     request->cmd = (put->flags & DPIF_FP_CREATE
791                     ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET);
792     request->dp_ifindex = dpif->dp_ifindex;
793     request->key = put->key;
794     request->key_len = put->key_len;
795     /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
796     request->actions = put->actions ? put->actions : &dummy_action;
797     request->actions_len = put->actions_len;
798     if (put->flags & DPIF_FP_ZERO_STATS) {
799         request->clear = true;
800     }
801     request->nlmsg_flags = put->flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
802 }
803
804 static int
805 dpif_linux_flow_put(struct dpif *dpif_, const struct dpif_flow_put *put)
806 {
807     struct dpif_linux_flow request, reply;
808     struct ofpbuf *buf;
809     int error;
810
811     dpif_linux_init_flow_put(dpif_, put, &request);
812     error = dpif_linux_flow_transact(&request,
813                                      put->stats ? &reply : NULL,
814                                      put->stats ? &buf : NULL);
815     if (!error && put->stats) {
816         dpif_linux_flow_get_stats(&reply, put->stats);
817         ofpbuf_delete(buf);
818     }
819     return error;
820 }
821
822 static void
823 dpif_linux_init_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del,
824                          struct dpif_linux_flow *request)
825 {
826     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
827
828     dpif_linux_flow_init(request);
829     request->cmd = OVS_FLOW_CMD_DEL;
830     request->dp_ifindex = dpif->dp_ifindex;
831     request->key = del->key;
832     request->key_len = del->key_len;
833 }
834
835 static int
836 dpif_linux_flow_del(struct dpif *dpif_, const struct dpif_flow_del *del)
837 {
838     struct dpif_linux_flow request, reply;
839     struct ofpbuf *buf;
840     int error;
841
842     dpif_linux_init_flow_del(dpif_, del, &request);
843     error = dpif_linux_flow_transact(&request,
844                                      del->stats ? &reply : NULL,
845                                      del->stats ? &buf : NULL);
846     if (!error && del->stats) {
847         dpif_linux_flow_get_stats(&reply, del->stats);
848         ofpbuf_delete(buf);
849     }
850     return error;
851 }
852
853 struct dpif_linux_flow_state {
854     struct nl_dump dump;
855     struct dpif_linux_flow flow;
856     struct dpif_flow_stats stats;
857     struct ofpbuf *buf;
858 };
859
860 static int
861 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
862 {
863     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
864     struct dpif_linux_flow_state *state;
865     struct dpif_linux_flow request;
866     struct ofpbuf *buf;
867
868     *statep = state = xmalloc(sizeof *state);
869
870     dpif_linux_flow_init(&request);
871     request.cmd = OVS_DP_CMD_GET;
872     request.dp_ifindex = dpif->dp_ifindex;
873
874     buf = ofpbuf_new(1024);
875     dpif_linux_flow_to_ofpbuf(&request, buf);
876     nl_dump_start(&state->dump, genl_sock, buf);
877     ofpbuf_delete(buf);
878
879     state->buf = NULL;
880
881     return 0;
882 }
883
884 static int
885 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
886                           const struct nlattr **key, size_t *key_len,
887                           const struct nlattr **actions, size_t *actions_len,
888                           const struct dpif_flow_stats **stats)
889 {
890     struct dpif_linux_flow_state *state = state_;
891     struct ofpbuf buf;
892     int error;
893
894     do {
895         ofpbuf_delete(state->buf);
896         state->buf = NULL;
897
898         if (!nl_dump_next(&state->dump, &buf)) {
899             return EOF;
900         }
901
902         error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
903         if (error) {
904             return error;
905         }
906
907         if (actions && !state->flow.actions) {
908             error = dpif_linux_flow_get__(dpif_, state->flow.key,
909                                           state->flow.key_len,
910                                           &state->flow, &state->buf);
911             if (error == ENOENT) {
912                 VLOG_DBG("dumped flow disappeared on get");
913             } else if (error) {
914                 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
915             }
916         }
917     } while (error);
918
919     if (actions) {
920         *actions = state->flow.actions;
921         *actions_len = state->flow.actions_len;
922     }
923     if (key) {
924         *key = state->flow.key;
925         *key_len = state->flow.key_len;
926     }
927     if (stats) {
928         dpif_linux_flow_get_stats(&state->flow, &state->stats);
929         *stats = &state->stats;
930     }
931     return error;
932 }
933
934 static int
935 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
936 {
937     struct dpif_linux_flow_state *state = state_;
938     int error = nl_dump_done(&state->dump);
939     ofpbuf_delete(state->buf);
940     free(state);
941     return error;
942 }
943
944 static void
945 dpif_linux_encode_execute(int dp_ifindex, const struct dpif_execute *d_exec,
946                           struct ofpbuf *buf)
947 {
948     struct ovs_header *k_exec;
949
950     ofpbuf_prealloc_tailroom(buf, (64
951                                    + d_exec->packet->size
952                                    + d_exec->key_len
953                                    + d_exec->actions_len));
954
955     nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
956                           OVS_PACKET_CMD_EXECUTE, OVS_PACKET_VERSION);
957
958     k_exec = ofpbuf_put_uninit(buf, sizeof *k_exec);
959     k_exec->dp_ifindex = dp_ifindex;
960
961     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET,
962                       d_exec->packet->data, d_exec->packet->size);
963     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, d_exec->key, d_exec->key_len);
964     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS,
965                       d_exec->actions, d_exec->actions_len);
966 }
967
968 static int
969 dpif_linux_execute__(int dp_ifindex, const struct dpif_execute *execute)
970 {
971     uint64_t request_stub[1024 / 8];
972     struct ofpbuf request;
973     int error;
974
975     ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
976     dpif_linux_encode_execute(dp_ifindex, execute, &request);
977     error = nl_sock_transact(genl_sock, &request, NULL);
978     ofpbuf_uninit(&request);
979
980     return error;
981 }
982
983 static int
984 dpif_linux_execute(struct dpif *dpif_, const struct dpif_execute *execute)
985 {
986     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
987
988     return dpif_linux_execute__(dpif->dp_ifindex, execute);
989 }
990
991 #define MAX_OPS 50
992
993 static void
994 dpif_linux_operate__(struct dpif *dpif_, struct dpif_op **ops, size_t n_ops)
995 {
996     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
997
998     struct op_auxdata {
999         struct nl_transaction txn;
1000
1001         struct ofpbuf request;
1002         uint64_t request_stub[1024 / 8];
1003
1004         struct ofpbuf reply;
1005         uint64_t reply_stub[1024 / 8];
1006     } auxes[MAX_OPS];
1007
1008     struct nl_transaction *txnsp[MAX_OPS];
1009     size_t i;
1010
1011     ovs_assert(n_ops <= MAX_OPS);
1012     for (i = 0; i < n_ops; i++) {
1013         struct op_auxdata *aux = &auxes[i];
1014         struct dpif_op *op = ops[i];
1015         struct dpif_flow_put *put;
1016         struct dpif_flow_del *del;
1017         struct dpif_execute *execute;
1018         struct dpif_linux_flow flow;
1019
1020         ofpbuf_use_stub(&aux->request,
1021                         aux->request_stub, sizeof aux->request_stub);
1022         aux->txn.request = &aux->request;
1023
1024         ofpbuf_use_stub(&aux->reply, aux->reply_stub, sizeof aux->reply_stub);
1025         aux->txn.reply = NULL;
1026
1027         switch (op->type) {
1028         case DPIF_OP_FLOW_PUT:
1029             put = &op->u.flow_put;
1030             dpif_linux_init_flow_put(dpif_, put, &flow);
1031             if (put->stats) {
1032                 flow.nlmsg_flags |= NLM_F_ECHO;
1033                 aux->txn.reply = &aux->reply;
1034             }
1035             dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1036             break;
1037
1038         case DPIF_OP_FLOW_DEL:
1039             del = &op->u.flow_del;
1040             dpif_linux_init_flow_del(dpif_, del, &flow);
1041             if (del->stats) {
1042                 flow.nlmsg_flags |= NLM_F_ECHO;
1043                 aux->txn.reply = &aux->reply;
1044             }
1045             dpif_linux_flow_to_ofpbuf(&flow, &aux->request);
1046             break;
1047
1048         case DPIF_OP_EXECUTE:
1049             execute = &op->u.execute;
1050             dpif_linux_encode_execute(dpif->dp_ifindex, execute,
1051                                       &aux->request);
1052             break;
1053
1054         default:
1055             NOT_REACHED();
1056         }
1057     }
1058
1059     for (i = 0; i < n_ops; i++) {
1060         txnsp[i] = &auxes[i].txn;
1061     }
1062     nl_sock_transact_multiple(genl_sock, txnsp, n_ops);
1063
1064     for (i = 0; i < n_ops; i++) {
1065         struct op_auxdata *aux = &auxes[i];
1066         struct nl_transaction *txn = &auxes[i].txn;
1067         struct dpif_op *op = ops[i];
1068         struct dpif_flow_put *put;
1069         struct dpif_flow_del *del;
1070
1071         op->error = txn->error;
1072
1073         switch (op->type) {
1074         case DPIF_OP_FLOW_PUT:
1075             put = &op->u.flow_put;
1076             if (put->stats) {
1077                 if (!op->error) {
1078                     struct dpif_linux_flow reply;
1079
1080                     op->error = dpif_linux_flow_from_ofpbuf(&reply,
1081                                                             txn->reply);
1082                     if (!op->error) {
1083                         dpif_linux_flow_get_stats(&reply, put->stats);
1084                     }
1085                 }
1086
1087                 if (op->error) {
1088                     memset(put->stats, 0, sizeof *put->stats);
1089                 }
1090             }
1091             break;
1092
1093         case DPIF_OP_FLOW_DEL:
1094             del = &op->u.flow_del;
1095             if (del->stats) {
1096                 if (!op->error) {
1097                     struct dpif_linux_flow reply;
1098
1099                     op->error = dpif_linux_flow_from_ofpbuf(&reply,
1100                                                             txn->reply);
1101                     if (!op->error) {
1102                         dpif_linux_flow_get_stats(&reply, del->stats);
1103                     }
1104                 }
1105
1106                 if (op->error) {
1107                     memset(del->stats, 0, sizeof *del->stats);
1108                 }
1109             }
1110             break;
1111
1112         case DPIF_OP_EXECUTE:
1113             break;
1114
1115         default:
1116             NOT_REACHED();
1117         }
1118
1119         ofpbuf_uninit(&aux->request);
1120         ofpbuf_uninit(&aux->reply);
1121     }
1122 }
1123
1124 static void
1125 dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
1126 {
1127     while (n_ops > 0) {
1128         size_t chunk = MIN(n_ops, MAX_OPS);
1129         dpif_linux_operate__(dpif, ops, chunk);
1130         ops += chunk;
1131         n_ops -= chunk;
1132     }
1133 }
1134
1135 static int
1136 dpif_linux_recv_set(struct dpif *dpif_, bool enable)
1137 {
1138     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1139
1140     if ((dpif->epoll_fd >= 0) == enable) {
1141         return 0;
1142     }
1143
1144     if (!enable) {
1145         destroy_channels(dpif);
1146     } else {
1147         struct dpif_port_dump port_dump;
1148         struct dpif_port port;
1149
1150         dpif->epoll_fd = epoll_create(10);
1151         if (dpif->epoll_fd < 0) {
1152             return errno;
1153         }
1154
1155         DPIF_PORT_FOR_EACH (&port, &port_dump, &dpif->dpif) {
1156             struct dpif_linux_vport vport_request;
1157             struct nl_sock *sock;
1158             uint32_t upcall_pid;
1159             int error;
1160
1161             error = nl_sock_create(NETLINK_GENERIC, &sock);
1162             if (error) {
1163                 return error;
1164             }
1165
1166             upcall_pid = nl_sock_pid(sock);
1167
1168             dpif_linux_vport_init(&vport_request);
1169             vport_request.cmd = OVS_VPORT_CMD_SET;
1170             vport_request.dp_ifindex = dpif->dp_ifindex;
1171             vport_request.port_no = port.port_no;
1172             vport_request.upcall_pid = &upcall_pid;
1173             error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
1174             if (!error) {
1175                 VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
1176                          dpif_name(&dpif->dpif), vport_request.port_no,
1177                          upcall_pid);
1178             } else {
1179                 VLOG_WARN_RL(&error_rl,
1180                              "%s: failed to set upcall pid on port: %s",
1181                              dpif_name(&dpif->dpif), strerror(error));
1182                 nl_sock_destroy(sock);
1183
1184                 if (error == ENODEV || error == ENOENT) {
1185                     /* This device isn't there, but keep trying the others. */
1186                     continue;
1187                 } else {
1188                     return error;
1189                 }
1190             }
1191
1192             error = add_channel(dpif, port.port_no, sock);
1193             if (error) {
1194                 VLOG_INFO("%s: could not add channel for port %s",
1195                           dpif_name(dpif_), port.name);
1196                 nl_sock_destroy(sock);
1197                 return error;
1198             }
1199         }
1200     }
1201
1202     return 0;
1203 }
1204
1205 static int
1206 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
1207                              uint32_t queue_id, uint32_t *priority)
1208 {
1209     if (queue_id < 0xf000) {
1210         *priority = TC_H_MAKE(1 << 16, queue_id + 1);
1211         return 0;
1212     } else {
1213         return EINVAL;
1214     }
1215 }
1216
1217 static int
1218 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
1219                  int *dp_ifindex)
1220 {
1221     static const struct nl_policy ovs_packet_policy[] = {
1222         /* Always present. */
1223         [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
1224                                      .min_len = ETH_HEADER_LEN },
1225         [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
1226
1227         /* OVS_PACKET_CMD_ACTION only. */
1228         [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
1229     };
1230
1231     struct ovs_header *ovs_header;
1232     struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
1233     struct nlmsghdr *nlmsg;
1234     struct genlmsghdr *genl;
1235     struct ofpbuf b;
1236     int type;
1237
1238     ofpbuf_use_const(&b, buf->data, buf->size);
1239
1240     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1241     genl = ofpbuf_try_pull(&b, sizeof *genl);
1242     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1243     if (!nlmsg || !genl || !ovs_header
1244         || nlmsg->nlmsg_type != ovs_packet_family
1245         || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
1246                             ARRAY_SIZE(ovs_packet_policy))) {
1247         return EINVAL;
1248     }
1249
1250     type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
1251             : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
1252             : -1);
1253     if (type < 0) {
1254         return EINVAL;
1255     }
1256
1257     memset(upcall, 0, sizeof *upcall);
1258     upcall->type = type;
1259     upcall->packet = buf;
1260     upcall->packet->data = CONST_CAST(struct nlattr *,
1261                                       nl_attr_get(a[OVS_PACKET_ATTR_PACKET]));
1262     upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
1263     upcall->key = CONST_CAST(struct nlattr *,
1264                              nl_attr_get(a[OVS_PACKET_ATTR_KEY]));
1265     upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
1266     upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
1267                         ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
1268                         : 0);
1269     *dp_ifindex = ovs_header->dp_ifindex;
1270
1271     return 0;
1272 }
1273
1274 static int
1275 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall,
1276                 struct ofpbuf *buf)
1277 {
1278     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1279     int read_tries = 0;
1280
1281     if (dpif->epoll_fd < 0) {
1282        return EAGAIN;
1283     }
1284
1285     if (dpif->event_offset >= dpif->n_events) {
1286         int retval;
1287
1288         dpif->event_offset = dpif->n_events = 0;
1289
1290         do {
1291             retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
1292                                 dpif->uc_array_size, 0);
1293         } while (retval < 0 && errno == EINTR);
1294         if (retval < 0) {
1295             static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
1296             VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
1297         } else if (retval > 0) {
1298             dpif->n_events = retval;
1299         }
1300     }
1301
1302     while (dpif->event_offset < dpif->n_events) {
1303         int idx = dpif->epoll_events[dpif->event_offset].data.u32;
1304         struct dpif_channel *ch = &dpif->channels[idx];
1305
1306         dpif->event_offset++;
1307
1308         for (;;) {
1309             int dp_ifindex;
1310             int error;
1311
1312             if (++read_tries > 50) {
1313                 return EAGAIN;
1314             }
1315
1316             error = nl_sock_recv(ch->sock, buf, false);
1317             if (error == ENOBUFS) {
1318                 /* ENOBUFS typically means that we've received so many
1319                  * packets that the buffer overflowed.  Try again
1320                  * immediately because there's almost certainly a packet
1321                  * waiting for us. */
1322                 report_loss(dpif_, ch);
1323                 continue;
1324             }
1325
1326             ch->last_poll = time_msec();
1327             if (error) {
1328                 if (error == EAGAIN) {
1329                     break;
1330                 }
1331                 return error;
1332             }
1333
1334             error = parse_odp_packet(buf, upcall, &dp_ifindex);
1335             if (!error && dp_ifindex == dpif->dp_ifindex) {
1336                 return 0;
1337             } else if (error) {
1338                 return error;
1339             }
1340         }
1341     }
1342
1343     return EAGAIN;
1344 }
1345
1346 static void
1347 dpif_linux_recv_wait(struct dpif *dpif_)
1348 {
1349     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1350
1351     if (dpif->epoll_fd < 0) {
1352        return;
1353     }
1354
1355     poll_fd_wait(dpif->epoll_fd, POLLIN);
1356 }
1357
1358 static void
1359 dpif_linux_recv_purge(struct dpif *dpif_)
1360 {
1361     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1362     struct dpif_channel *ch;
1363
1364     if (dpif->epoll_fd < 0) {
1365        return;
1366     }
1367
1368     for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size]; ch++) {
1369         if (ch->sock) {
1370             nl_sock_drain(ch->sock);
1371         }
1372     }
1373 }
1374
1375 const struct dpif_class dpif_linux_class = {
1376     "system",
1377     dpif_linux_enumerate,
1378     NULL,
1379     dpif_linux_open,
1380     dpif_linux_close,
1381     dpif_linux_destroy,
1382     dpif_linux_run,
1383     dpif_linux_wait,
1384     dpif_linux_get_stats,
1385     dpif_linux_port_add,
1386     dpif_linux_port_del,
1387     dpif_linux_port_query_by_number,
1388     dpif_linux_port_query_by_name,
1389     dpif_linux_get_max_ports,
1390     dpif_linux_port_get_pid,
1391     dpif_linux_port_dump_start,
1392     dpif_linux_port_dump_next,
1393     dpif_linux_port_dump_done,
1394     dpif_linux_port_poll,
1395     dpif_linux_port_poll_wait,
1396     dpif_linux_flow_get,
1397     dpif_linux_flow_put,
1398     dpif_linux_flow_del,
1399     dpif_linux_flow_flush,
1400     dpif_linux_flow_dump_start,
1401     dpif_linux_flow_dump_next,
1402     dpif_linux_flow_dump_done,
1403     dpif_linux_execute,
1404     dpif_linux_operate,
1405     dpif_linux_recv_set,
1406     dpif_linux_queue_to_priority,
1407     dpif_linux_recv,
1408     dpif_linux_recv_wait,
1409     dpif_linux_recv_purge,
1410 };
1411 \f
1412 static int
1413 dpif_linux_init(void)
1414 {
1415     static int error = -1;
1416
1417     if (error < 0) {
1418         unsigned int ovs_vport_mcgroup;
1419
1420         error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1421                                       &ovs_datapath_family);
1422         if (error) {
1423             VLOG_ERR("Generic Netlink family '%s' does not exist. "
1424                      "The Open vSwitch kernel module is probably not loaded.",
1425                      OVS_DATAPATH_FAMILY);
1426         }
1427         if (!error) {
1428             error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1429         }
1430         if (!error) {
1431             error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1432         }
1433         if (!error) {
1434             error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1435                                           &ovs_packet_family);
1436         }
1437         if (!error) {
1438             error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1439         }
1440         if (!error) {
1441             error = nl_lookup_genl_mcgroup(OVS_VPORT_FAMILY, OVS_VPORT_MCGROUP,
1442                                            &ovs_vport_mcgroup,
1443                                            OVS_VPORT_MCGROUP_FALLBACK_ID);
1444         }
1445         if (!error) {
1446             static struct dpif_linux_vport vport;
1447             nln = nln_create(NETLINK_GENERIC, ovs_vport_mcgroup,
1448                              dpif_linux_nln_parse, &vport);
1449         }
1450     }
1451
1452     return error;
1453 }
1454
1455 bool
1456 dpif_linux_is_internal_device(const char *name)
1457 {
1458     struct dpif_linux_vport reply;
1459     struct ofpbuf *buf;
1460     int error;
1461
1462     error = dpif_linux_vport_get(name, &reply, &buf);
1463     if (!error) {
1464         ofpbuf_delete(buf);
1465     } else if (error != ENODEV && error != ENOENT) {
1466         VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1467                      name, strerror(error));
1468     }
1469
1470     return reply.type == OVS_VPORT_TYPE_INTERNAL;
1471 }
1472
1473 static bool
1474 dpif_linux_nln_parse(struct ofpbuf *buf, void *vport_)
1475 {
1476     struct dpif_linux_vport *vport = vport_;
1477     return dpif_linux_vport_from_ofpbuf(vport, buf) == 0;
1478 }
1479
1480 static void
1481 dpif_linux_port_changed(const void *vport_, void *dpif_)
1482 {
1483     const struct dpif_linux_vport *vport = vport_;
1484     struct dpif_linux *dpif = dpif_;
1485
1486     if (vport) {
1487         if (vport->dp_ifindex == dpif->dp_ifindex
1488             && (vport->cmd == OVS_VPORT_CMD_NEW
1489                 || vport->cmd == OVS_VPORT_CMD_DEL
1490                 || vport->cmd == OVS_VPORT_CMD_SET)) {
1491             VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
1492                      dpif->dpif.full_name, vport->name, vport->cmd);
1493             sset_add(&dpif->changed_ports, vport->name);
1494         }
1495     } else {
1496         dpif->change_error = true;
1497     }
1498 }
1499 \f
1500 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1501  * by Netlink attributes, into 'vport'.  Returns 0 if successful, otherwise a
1502  * positive errno value.
1503  *
1504  * 'vport' will contain pointers into 'buf', so the caller should not free
1505  * 'buf' while 'vport' is still in use. */
1506 static int
1507 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1508                              const struct ofpbuf *buf)
1509 {
1510     static const struct nl_policy ovs_vport_policy[] = {
1511         [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1512         [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1513         [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1514         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
1515         [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
1516                                    .optional = true },
1517         [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1518     };
1519
1520     struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1521     struct ovs_header *ovs_header;
1522     struct nlmsghdr *nlmsg;
1523     struct genlmsghdr *genl;
1524     struct ofpbuf b;
1525
1526     dpif_linux_vport_init(vport);
1527
1528     ofpbuf_use_const(&b, buf->data, buf->size);
1529     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1530     genl = ofpbuf_try_pull(&b, sizeof *genl);
1531     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1532     if (!nlmsg || !genl || !ovs_header
1533         || nlmsg->nlmsg_type != ovs_vport_family
1534         || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1535                             ARRAY_SIZE(ovs_vport_policy))) {
1536         return EINVAL;
1537     }
1538
1539     vport->cmd = genl->cmd;
1540     vport->dp_ifindex = ovs_header->dp_ifindex;
1541     vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1542     vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1543     vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1544     if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1545         vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PID]);
1546     }
1547     if (a[OVS_VPORT_ATTR_STATS]) {
1548         vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1549     }
1550     if (a[OVS_VPORT_ATTR_OPTIONS]) {
1551         vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1552         vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1553     }
1554     return 0;
1555 }
1556
1557 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1558  * followed by Netlink attributes corresponding to 'vport'. */
1559 static void
1560 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1561                            struct ofpbuf *buf)
1562 {
1563     struct ovs_header *ovs_header;
1564
1565     nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1566                           vport->cmd, OVS_VPORT_VERSION);
1567
1568     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1569     ovs_header->dp_ifindex = vport->dp_ifindex;
1570
1571     if (vport->port_no != UINT32_MAX) {
1572         nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1573     }
1574
1575     if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1576         nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1577     }
1578
1579     if (vport->name) {
1580         nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1581     }
1582
1583     if (vport->upcall_pid) {
1584         nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PID, *vport->upcall_pid);
1585     }
1586
1587     if (vport->stats) {
1588         nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1589                           vport->stats, sizeof *vport->stats);
1590     }
1591
1592     if (vport->options) {
1593         nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1594                           vport->options, vport->options_len);
1595     }
1596 }
1597
1598 /* Clears 'vport' to "empty" values. */
1599 void
1600 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1601 {
1602     memset(vport, 0, sizeof *vport);
1603     vport->port_no = UINT32_MAX;
1604 }
1605
1606 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1607  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1608  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1609  * result of the command is expected to be an ovs_vport also, which is decoded
1610  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1611  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1612 int
1613 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1614                           struct dpif_linux_vport *reply,
1615                           struct ofpbuf **bufp)
1616 {
1617     struct ofpbuf *request_buf;
1618     int error;
1619
1620     ovs_assert((reply != NULL) == (bufp != NULL));
1621
1622     error = dpif_linux_init();
1623     if (error) {
1624         if (reply) {
1625             *bufp = NULL;
1626             dpif_linux_vport_init(reply);
1627         }
1628         return error;
1629     }
1630
1631     request_buf = ofpbuf_new(1024);
1632     dpif_linux_vport_to_ofpbuf(request, request_buf);
1633     error = nl_sock_transact(genl_sock, request_buf, bufp);
1634     ofpbuf_delete(request_buf);
1635
1636     if (reply) {
1637         if (!error) {
1638             error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1639         }
1640         if (error) {
1641             dpif_linux_vport_init(reply);
1642             ofpbuf_delete(*bufp);
1643             *bufp = NULL;
1644         }
1645     }
1646     return error;
1647 }
1648
1649 /* Obtains information about the kernel vport named 'name' and stores it into
1650  * '*reply' and '*bufp'.  The caller must free '*bufp' when the reply is no
1651  * longer needed ('reply' will contain pointers into '*bufp').  */
1652 int
1653 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1654                      struct ofpbuf **bufp)
1655 {
1656     struct dpif_linux_vport request;
1657
1658     dpif_linux_vport_init(&request);
1659     request.cmd = OVS_VPORT_CMD_GET;
1660     request.name = name;
1661
1662     return dpif_linux_vport_transact(&request, reply, bufp);
1663 }
1664 \f
1665 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1666  * by Netlink attributes, into 'dp'.  Returns 0 if successful, otherwise a
1667  * positive errno value.
1668  *
1669  * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1670  * while 'dp' is still in use. */
1671 static int
1672 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1673 {
1674     static const struct nl_policy ovs_datapath_policy[] = {
1675         [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1676         [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
1677                                 .optional = true },
1678     };
1679
1680     struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1681     struct ovs_header *ovs_header;
1682     struct nlmsghdr *nlmsg;
1683     struct genlmsghdr *genl;
1684     struct ofpbuf b;
1685
1686     dpif_linux_dp_init(dp);
1687
1688     ofpbuf_use_const(&b, buf->data, buf->size);
1689     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1690     genl = ofpbuf_try_pull(&b, sizeof *genl);
1691     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1692     if (!nlmsg || !genl || !ovs_header
1693         || nlmsg->nlmsg_type != ovs_datapath_family
1694         || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1695                             ARRAY_SIZE(ovs_datapath_policy))) {
1696         return EINVAL;
1697     }
1698
1699     dp->cmd = genl->cmd;
1700     dp->dp_ifindex = ovs_header->dp_ifindex;
1701     dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1702     if (a[OVS_DP_ATTR_STATS]) {
1703         /* Can't use structure assignment because Netlink doesn't ensure
1704          * sufficient alignment for 64-bit members. */
1705         memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1706                sizeof dp->stats);
1707     }
1708
1709     return 0;
1710 }
1711
1712 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1713 static void
1714 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1715 {
1716     struct ovs_header *ovs_header;
1717
1718     nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1719                           NLM_F_REQUEST | NLM_F_ECHO, dp->cmd,
1720                           OVS_DATAPATH_VERSION);
1721
1722     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1723     ovs_header->dp_ifindex = dp->dp_ifindex;
1724
1725     if (dp->name) {
1726         nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1727     }
1728
1729     if (dp->upcall_pid) {
1730         nl_msg_put_u32(buf, OVS_DP_ATTR_UPCALL_PID, *dp->upcall_pid);
1731     }
1732
1733     /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1734 }
1735
1736 /* Clears 'dp' to "empty" values. */
1737 static void
1738 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1739 {
1740     memset(dp, 0, sizeof *dp);
1741 }
1742
1743 static void
1744 dpif_linux_dp_dump_start(struct nl_dump *dump)
1745 {
1746     struct dpif_linux_dp request;
1747     struct ofpbuf *buf;
1748
1749     dpif_linux_dp_init(&request);
1750     request.cmd = OVS_DP_CMD_GET;
1751
1752     buf = ofpbuf_new(1024);
1753     dpif_linux_dp_to_ofpbuf(&request, buf);
1754     nl_dump_start(dump, genl_sock, buf);
1755     ofpbuf_delete(buf);
1756 }
1757
1758 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1759  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1760  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1761  * result of the command is expected to be of the same form, which is decoded
1762  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1763  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1764 static int
1765 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1766                        struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1767 {
1768     struct ofpbuf *request_buf;
1769     int error;
1770
1771     ovs_assert((reply != NULL) == (bufp != NULL));
1772
1773     request_buf = ofpbuf_new(1024);
1774     dpif_linux_dp_to_ofpbuf(request, request_buf);
1775     error = nl_sock_transact(genl_sock, request_buf, bufp);
1776     ofpbuf_delete(request_buf);
1777
1778     if (reply) {
1779         if (!error) {
1780             error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1781         }
1782         if (error) {
1783             dpif_linux_dp_init(reply);
1784             ofpbuf_delete(*bufp);
1785             *bufp = NULL;
1786         }
1787     }
1788     return error;
1789 }
1790
1791 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1792  * The caller must free '*bufp' when the reply is no longer needed ('reply'
1793  * will contain pointers into '*bufp').  */
1794 static int
1795 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1796                   struct ofpbuf **bufp)
1797 {
1798     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1799     struct dpif_linux_dp request;
1800
1801     dpif_linux_dp_init(&request);
1802     request.cmd = OVS_DP_CMD_GET;
1803     request.dp_ifindex = dpif->dp_ifindex;
1804
1805     return dpif_linux_dp_transact(&request, reply, bufp);
1806 }
1807 \f
1808 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1809  * by Netlink attributes, into 'flow'.  Returns 0 if successful, otherwise a
1810  * positive errno value.
1811  *
1812  * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1813  * while 'flow' is still in use. */
1814 static int
1815 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1816                             const struct ofpbuf *buf)
1817 {
1818     static const struct nl_policy ovs_flow_policy[] = {
1819         [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1820         [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1821         [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
1822                                   .optional = true },
1823         [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1824         [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1825         /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1826     };
1827
1828     struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1829     struct ovs_header *ovs_header;
1830     struct nlmsghdr *nlmsg;
1831     struct genlmsghdr *genl;
1832     struct ofpbuf b;
1833
1834     dpif_linux_flow_init(flow);
1835
1836     ofpbuf_use_const(&b, buf->data, buf->size);
1837     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1838     genl = ofpbuf_try_pull(&b, sizeof *genl);
1839     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1840     if (!nlmsg || !genl || !ovs_header
1841         || nlmsg->nlmsg_type != ovs_flow_family
1842         || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1843                             ARRAY_SIZE(ovs_flow_policy))) {
1844         return EINVAL;
1845     }
1846
1847     flow->nlmsg_flags = nlmsg->nlmsg_flags;
1848     flow->dp_ifindex = ovs_header->dp_ifindex;
1849     flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1850     flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1851     if (a[OVS_FLOW_ATTR_ACTIONS]) {
1852         flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1853         flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1854     }
1855     if (a[OVS_FLOW_ATTR_STATS]) {
1856         flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1857     }
1858     if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1859         flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1860     }
1861     if (a[OVS_FLOW_ATTR_USED]) {
1862         flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1863     }
1864     return 0;
1865 }
1866
1867 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1868  * followed by Netlink attributes corresponding to 'flow'. */
1869 static void
1870 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1871                           struct ofpbuf *buf)
1872 {
1873     struct ovs_header *ovs_header;
1874
1875     nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1876                           NLM_F_REQUEST | flow->nlmsg_flags,
1877                           flow->cmd, OVS_FLOW_VERSION);
1878
1879     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1880     ovs_header->dp_ifindex = flow->dp_ifindex;
1881
1882     if (flow->key_len) {
1883         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1884     }
1885
1886     if (flow->actions || flow->actions_len) {
1887         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1888                           flow->actions, flow->actions_len);
1889     }
1890
1891     /* We never need to send these to the kernel. */
1892     ovs_assert(!flow->stats);
1893     ovs_assert(!flow->tcp_flags);
1894     ovs_assert(!flow->used);
1895
1896     if (flow->clear) {
1897         nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1898     }
1899 }
1900
1901 /* Clears 'flow' to "empty" values. */
1902 static void
1903 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1904 {
1905     memset(flow, 0, sizeof *flow);
1906 }
1907
1908 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1909  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1910  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1911  * result of the command is expected to be a flow also, which is decoded and
1912  * stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the reply
1913  * is no longer needed ('reply' will contain pointers into '*bufp'). */
1914 static int
1915 dpif_linux_flow_transact(struct dpif_linux_flow *request,
1916                          struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1917 {
1918     struct ofpbuf *request_buf;
1919     int error;
1920
1921     ovs_assert((reply != NULL) == (bufp != NULL));
1922
1923     if (reply) {
1924         request->nlmsg_flags |= NLM_F_ECHO;
1925     }
1926
1927     request_buf = ofpbuf_new(1024);
1928     dpif_linux_flow_to_ofpbuf(request, request_buf);
1929     error = nl_sock_transact(genl_sock, request_buf, bufp);
1930     ofpbuf_delete(request_buf);
1931
1932     if (reply) {
1933         if (!error) {
1934             error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1935         }
1936         if (error) {
1937             dpif_linux_flow_init(reply);
1938             ofpbuf_delete(*bufp);
1939             *bufp = NULL;
1940         }
1941     }
1942     return error;
1943 }
1944
1945 static void
1946 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1947                           struct dpif_flow_stats *stats)
1948 {
1949     if (flow->stats) {
1950         stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1951         stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1952     } else {
1953         stats->n_packets = 0;
1954         stats->n_bytes = 0;
1955     }
1956     stats->used = flow->used ? get_32aligned_u64(flow->used) : 0;
1957     stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1958 }
1959 \f
1960 /* Logs information about a packet that was recently lost in 'ch' (in
1961  * 'dpif_'). */
1962 static void
1963 report_loss(struct dpif *dpif_, struct dpif_channel *ch)
1964 {
1965     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1966     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
1967     struct ds s;
1968
1969     if (VLOG_DROP_WARN(&rl)) {
1970         return;
1971     }
1972
1973     ds_init(&s);
1974     if (ch->last_poll != LLONG_MIN) {
1975         ds_put_format(&s, " (last polled %lld ms ago)",
1976                       time_msec() - ch->last_poll);
1977     }
1978
1979     VLOG_WARN("%s: lost packet on channel %td%s",
1980               dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));
1981     ds_destroy(&s);
1982 }