datapath: Use "OVS_*" as opposed to "ODP_*" for user<->kernel interactions.
[openvswitch] / lib / dpif-linux.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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 <assert.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <net/if.h>
27 #include <linux/types.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
31 #include <stdlib.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 "netdev.h"
40 #include "netdev-linux.h"
41 #include "netdev-vport.h"
42 #include "netlink-socket.h"
43 #include "netlink.h"
44 #include "odp-util.h"
45 #include "ofpbuf.h"
46 #include "openvswitch/tunnel.h"
47 #include "packets.h"
48 #include "poll-loop.h"
49 #include "rtnetlink.h"
50 #include "rtnetlink-link.h"
51 #include "shash.h"
52 #include "sset.h"
53 #include "unaligned.h"
54 #include "util.h"
55 #include "vlog.h"
56
57 VLOG_DEFINE_THIS_MODULE(dpif_linux);
58
59 enum { LRU_MAX_PORTS = 1024 };
60 enum { LRU_MASK = LRU_MAX_PORTS - 1};
61 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
62
63 struct dpif_linux_dp {
64     /* Generic Netlink header. */
65     uint8_t cmd;
66
67     /* struct ovs_header. */
68     int dp_ifindex;
69
70     /* Attributes. */
71     const char *name;                  /* OVS_DP_ATTR_NAME. */
72     struct ovs_dp_stats stats;         /* OVS_DP_ATTR_STATS. */
73     enum ovs_frag_handling ipv4_frags; /* OVS_DP_ATTR_IPV4_FRAGS. */
74     const uint32_t *sampling;          /* OVS_DP_ATTR_SAMPLING. */
75     uint32_t mcgroups[DPIF_N_UC_TYPES]; /* OVS_DP_ATTR_MCGROUPS. */
76 };
77
78 static void dpif_linux_dp_init(struct dpif_linux_dp *);
79 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *,
80                                      const struct ofpbuf *);
81 static void dpif_linux_dp_dump_start(struct nl_dump *);
82 static int dpif_linux_dp_transact(const struct dpif_linux_dp *request,
83                                   struct dpif_linux_dp *reply,
84                                   struct ofpbuf **bufp);
85 static int dpif_linux_dp_get(const struct dpif *, struct dpif_linux_dp *reply,
86                              struct ofpbuf **bufp);
87
88 struct dpif_linux_flow {
89     /* Generic Netlink header. */
90     uint8_t cmd;
91
92     /* struct ovs_header. */
93     unsigned int nlmsg_flags;
94     int dp_ifindex;
95
96     /* Attributes.
97      *
98      * The 'stats' and 'used' members point to 64-bit data that might only be
99      * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
100      * access their values.
101      *
102      * If 'actions' is nonnull then OVS_FLOW_ATTR_ACTIONS will be included in
103      * the Netlink version of the command, even if actions_len is zero. */
104     const struct nlattr *key;           /* OVS_FLOW_ATTR_KEY. */
105     size_t key_len;
106     const struct nlattr *actions;       /* OVS_FLOW_ATTR_ACTIONS. */
107     size_t actions_len;
108     const struct ovs_flow_stats *stats; /* OVS_FLOW_ATTR_STATS. */
109     const uint8_t *tcp_flags;           /* OVS_FLOW_ATTR_TCP_FLAGS. */
110     const uint64_t *used;               /* OVS_FLOW_ATTR_USED. */
111     bool clear;                         /* OVS_FLOW_ATTR_CLEAR. */
112 };
113
114 static void dpif_linux_flow_init(struct dpif_linux_flow *);
115 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *,
116                                        const struct ofpbuf *);
117 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *,
118                                       struct ofpbuf *);
119 static int dpif_linux_flow_transact(const struct dpif_linux_flow *request,
120                                     struct dpif_linux_flow *reply,
121                                     struct ofpbuf **bufp);
122 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow *,
123                                       struct dpif_flow_stats *);
124
125 /* Datapath interface for the openvswitch Linux kernel module. */
126 struct dpif_linux {
127     struct dpif dpif;
128     int dp_ifindex;
129
130     /* Multicast group messages. */
131     struct nl_sock *mc_sock;
132     uint32_t mcgroups[DPIF_N_UC_TYPES];
133     unsigned int listen_mask;
134
135     /* Change notification. */
136     struct sset changed_ports;  /* Ports that have changed. */
137     struct rtnetlink_notifier port_notifier;
138     bool change_error;
139
140     /* Queue of unused ports. */
141     unsigned long *lru_bitmap;
142     uint16_t lru_ports[LRU_MAX_PORTS];
143     size_t lru_head;
144     size_t lru_tail;
145 };
146
147 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
148
149 /* Generic Netlink family numbers for OVS. */
150 static int ovs_datapath_family;
151 static int ovs_vport_family;
152 static int ovs_flow_family;
153 static int ovs_packet_family;
154
155 /* Generic Netlink socket. */
156 static struct nl_sock *genl_sock;
157
158 static int dpif_linux_init(void);
159 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
160 static void dpif_linux_port_changed(const struct rtnetlink_link_change *,
161                                     void *dpif);
162
163 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
164                                        struct ofpbuf *);
165 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
166                                         const struct ofpbuf *);
167
168 static struct dpif_linux *
169 dpif_linux_cast(const struct dpif *dpif)
170 {
171     dpif_assert_class(dpif, &dpif_linux_class);
172     return CONTAINER_OF(dpif, struct dpif_linux, dpif);
173 }
174
175 static void
176 dpif_linux_push_port(struct dpif_linux *dp, uint16_t port)
177 {
178     if (port < LRU_MAX_PORTS && !bitmap_is_set(dp->lru_bitmap, port)) {
179         bitmap_set1(dp->lru_bitmap, port);
180         dp->lru_ports[dp->lru_head++ & LRU_MASK] = port;
181     }
182 }
183
184 static uint32_t
185 dpif_linux_pop_port(struct dpif_linux *dp)
186 {
187     uint16_t port;
188
189     if (dp->lru_head == dp->lru_tail) {
190         return UINT32_MAX;
191     }
192
193     port = dp->lru_ports[dp->lru_tail++ & LRU_MASK];
194     bitmap_set0(dp->lru_bitmap, port);
195     return port;
196 }
197
198 static int
199 dpif_linux_enumerate(struct sset *all_dps)
200 {
201     struct nl_dump dump;
202     struct ofpbuf msg;
203     int error;
204
205     error = dpif_linux_init();
206     if (error) {
207         return error;
208     }
209
210     dpif_linux_dp_dump_start(&dump);
211     while (nl_dump_next(&dump, &msg)) {
212         struct dpif_linux_dp dp;
213
214         if (!dpif_linux_dp_from_ofpbuf(&dp, &msg)) {
215             sset_add(all_dps, dp.name);
216         }
217     }
218     return nl_dump_done(&dump);
219 }
220
221 static int
222 dpif_linux_open(const struct dpif_class *class OVS_UNUSED, const char *name,
223                 bool create, struct dpif **dpifp)
224 {
225     struct dpif_linux_dp dp_request, dp;
226     struct ofpbuf *buf;
227     int error;
228
229     error = dpif_linux_init();
230     if (error) {
231         return error;
232     }
233
234     /* Create or look up datapath. */
235     dpif_linux_dp_init(&dp_request);
236     dp_request.cmd = create ? OVS_DP_CMD_NEW : OVS_DP_CMD_GET;
237     dp_request.name = name;
238     error = dpif_linux_dp_transact(&dp_request, &dp, &buf);
239     if (error) {
240         return error;
241     }
242     error = open_dpif(&dp, dpifp);
243     ofpbuf_delete(buf);
244
245     return error;
246 }
247
248 static int
249 open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
250 {
251     struct dpif_linux *dpif;
252     int error;
253     int i;
254
255     dpif = xmalloc(sizeof *dpif);
256     error = rtnetlink_link_notifier_register(&dpif->port_notifier,
257                                              dpif_linux_port_changed, dpif);
258     if (error) {
259         goto error_free;
260     }
261
262     dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
263               dp->dp_ifindex, dp->dp_ifindex);
264
265     dpif->mc_sock = NULL;
266     for (i = 0; i < DPIF_N_UC_TYPES; i++) {
267         dpif->mcgroups[i] = dp->mcgroups[i];
268     }
269     dpif->listen_mask = 0;
270     dpif->dp_ifindex = dp->dp_ifindex;
271     sset_init(&dpif->changed_ports);
272     dpif->change_error = false;
273     *dpifp = &dpif->dpif;
274
275     dpif->lru_head = dpif->lru_tail = 0;
276     dpif->lru_bitmap = bitmap_allocate(LRU_MAX_PORTS);
277     bitmap_set1(dpif->lru_bitmap, OVSP_LOCAL);
278     for (i = 1; i < LRU_MAX_PORTS; i++) {
279         dpif_linux_push_port(dpif, i);
280     }
281     return 0;
282
283 error_free:
284     free(dpif);
285     return error;
286 }
287
288 static void
289 dpif_linux_close(struct dpif *dpif_)
290 {
291     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
292     nl_sock_destroy(dpif->mc_sock);
293     rtnetlink_link_notifier_unregister(&dpif->port_notifier);
294     sset_destroy(&dpif->changed_ports);
295     free(dpif->lru_bitmap);
296     free(dpif);
297 }
298
299 static int
300 dpif_linux_destroy(struct dpif *dpif_)
301 {
302     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
303     struct dpif_linux_dp dp;
304
305     dpif_linux_dp_init(&dp);
306     dp.cmd = OVS_DP_CMD_DEL;
307     dp.dp_ifindex = dpif->dp_ifindex;
308     return dpif_linux_dp_transact(&dp, NULL, NULL);
309 }
310
311 static int
312 dpif_linux_get_stats(const struct dpif *dpif_, struct ovs_dp_stats *stats)
313 {
314     struct dpif_linux_dp dp;
315     struct ofpbuf *buf;
316     int error;
317
318     error = dpif_linux_dp_get(dpif_, &dp, &buf);
319     if (!error) {
320         *stats = dp.stats;
321         ofpbuf_delete(buf);
322     }
323     return error;
324 }
325
326 static int
327 dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
328 {
329     struct dpif_linux_dp dp;
330     struct ofpbuf *buf;
331     int error;
332
333     error = dpif_linux_dp_get(dpif_, &dp, &buf);
334     if (!error) {
335         *drop_fragsp = dp.ipv4_frags == OVS_DP_FRAG_DROP;
336         ofpbuf_delete(buf);
337     }
338     return error;
339 }
340
341 static int
342 dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
343 {
344     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
345     struct dpif_linux_dp dp;
346
347     dpif_linux_dp_init(&dp);
348     dp.cmd = OVS_DP_CMD_SET;
349     dp.dp_ifindex = dpif->dp_ifindex;
350     dp.ipv4_frags = drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO;
351     return dpif_linux_dp_transact(&dp, NULL, NULL);
352 }
353
354 static int
355 dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
356                     uint16_t *port_nop)
357 {
358     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
359     const char *name = netdev_get_name(netdev);
360     const char *type = netdev_get_type(netdev);
361     struct dpif_linux_vport request, reply;
362     const struct ofpbuf *options;
363     struct ofpbuf *buf;
364     int error;
365
366     dpif_linux_vport_init(&request);
367     request.cmd = OVS_VPORT_CMD_NEW;
368     request.dp_ifindex = dpif->dp_ifindex;
369     request.type = netdev_vport_get_vport_type(netdev);
370     if (request.type == OVS_VPORT_TYPE_UNSPEC) {
371         VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
372                      "unsupported type `%s'",
373                      dpif_name(dpif_), name, type);
374         return EINVAL;
375     }
376     request.name = name;
377
378     options = netdev_vport_get_options(netdev);
379     if (options && options->size) {
380         request.options = options->data;
381         request.options_len = options->size;
382     }
383
384     /* Loop until we find a port that isn't used. */
385     do {
386         request.port_no = dpif_linux_pop_port(dpif);
387         error = dpif_linux_vport_transact(&request, &reply, &buf);
388
389         if (!error) {
390             *port_nop = reply.port_no;
391         }
392         ofpbuf_delete(buf);
393     } while (request.port_no != UINT32_MAX
394              && (error == EBUSY || error == EFBIG));
395
396     return error;
397 }
398
399 static int
400 dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
401 {
402     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
403     struct dpif_linux_vport vport;
404     int error;
405
406     dpif_linux_vport_init(&vport);
407     vport.cmd = OVS_VPORT_CMD_DEL;
408     vport.dp_ifindex = dpif->dp_ifindex;
409     vport.port_no = port_no;
410     error = dpif_linux_vport_transact(&vport, NULL, NULL);
411
412     if (!error) {
413         dpif_linux_push_port(dpif, port_no);
414     }
415     return error;
416 }
417
418 static int
419 dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
420                         const char *port_name, struct dpif_port *dpif_port)
421 {
422     struct dpif_linux_vport request;
423     struct dpif_linux_vport reply;
424     struct ofpbuf *buf;
425     int error;
426
427     dpif_linux_vport_init(&request);
428     request.cmd = OVS_VPORT_CMD_GET;
429     request.dp_ifindex = dpif_linux_cast(dpif)->dp_ifindex;
430     request.port_no = port_no;
431     request.name = port_name;
432
433     error = dpif_linux_vport_transact(&request, &reply, &buf);
434     if (!error) {
435         dpif_port->name = xstrdup(reply.name);
436         dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
437         dpif_port->port_no = reply.port_no;
438         if (reply.stats) {
439             netdev_stats_from_rtnl_link_stats64(&dpif_port->stats,
440                                                 reply.stats);
441         } else {
442             memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
443         }
444         ofpbuf_delete(buf);
445     }
446     return error;
447 }
448
449 static int
450 dpif_linux_port_query_by_number(const struct dpif *dpif, uint16_t port_no,
451                                 struct dpif_port *dpif_port)
452 {
453     return dpif_linux_port_query__(dpif, port_no, NULL, dpif_port);
454 }
455
456 static int
457 dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
458                               struct dpif_port *dpif_port)
459 {
460     return dpif_linux_port_query__(dpif, 0, devname, dpif_port);
461 }
462
463 static int
464 dpif_linux_get_max_ports(const struct dpif *dpif OVS_UNUSED)
465 {
466     /* If the datapath increases its range of supported ports, then it should
467      * start reporting that. */
468     return 1024;
469 }
470
471 static int
472 dpif_linux_flow_flush(struct dpif *dpif_)
473 {
474     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
475     struct dpif_linux_flow flow;
476
477     dpif_linux_flow_init(&flow);
478     flow.cmd = OVS_FLOW_CMD_DEL;
479     flow.dp_ifindex = dpif->dp_ifindex;
480     return dpif_linux_flow_transact(&flow, NULL, NULL);
481 }
482
483 struct dpif_linux_port_state {
484     struct nl_dump dump;
485     unsigned long *port_bitmap; /* Ports in the datapath. */
486     bool complete;              /* Dump completed without error. */
487 };
488
489 static int
490 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
491 {
492     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
493     struct dpif_linux_port_state *state;
494     struct dpif_linux_vport request;
495     struct ofpbuf *buf;
496
497     *statep = state = xmalloc(sizeof *state);
498     state->port_bitmap = bitmap_allocate(LRU_MAX_PORTS);
499     state->complete = false;
500
501     dpif_linux_vport_init(&request);
502     request.cmd = OVS_DP_CMD_GET;
503     request.dp_ifindex = dpif->dp_ifindex;
504
505     buf = ofpbuf_new(1024);
506     dpif_linux_vport_to_ofpbuf(&request, buf);
507     nl_dump_start(&state->dump, genl_sock, buf);
508     ofpbuf_delete(buf);
509
510     return 0;
511 }
512
513 static int
514 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
515                           struct dpif_port *dpif_port)
516 {
517     struct dpif_linux_port_state *state = state_;
518     struct dpif_linux_vport vport;
519     struct ofpbuf buf;
520     int error;
521
522     if (!nl_dump_next(&state->dump, &buf)) {
523         state->complete = true;
524         return EOF;
525     }
526
527     error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
528     if (error) {
529         return error;
530     }
531
532     if (vport.port_no < LRU_MAX_PORTS) {
533         bitmap_set1(state->port_bitmap, vport.port_no);
534     }
535
536     dpif_port->name = (char *) vport.name;
537     dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
538     dpif_port->port_no = vport.port_no;
539     if (vport.stats) {
540         netdev_stats_from_rtnl_link_stats64(&dpif_port->stats, vport.stats);
541     } else {
542         memset(&dpif_port->stats, 0xff, sizeof dpif_port->stats);
543     }
544     return 0;
545 }
546
547 static int
548 dpif_linux_port_dump_done(const struct dpif *dpif_, void *state_)
549 {
550     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
551     struct dpif_linux_port_state *state = state_;
552     int error = nl_dump_done(&state->dump);
553
554     if (state->complete) {
555         uint16_t i;
556
557         for (i = 0; i < LRU_MAX_PORTS; i++) {
558             if (!bitmap_is_set(state->port_bitmap, i)) {
559                 dpif_linux_push_port(dpif, i);
560             }
561         }
562     }
563
564     free(state->port_bitmap);
565     free(state);
566     return error;
567 }
568
569 static int
570 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
571 {
572     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
573
574     if (dpif->change_error) {
575         dpif->change_error = false;
576         sset_clear(&dpif->changed_ports);
577         return ENOBUFS;
578     } else if (!sset_is_empty(&dpif->changed_ports)) {
579         *devnamep = sset_pop(&dpif->changed_ports);
580         return 0;
581     } else {
582         return EAGAIN;
583     }
584 }
585
586 static void
587 dpif_linux_port_poll_wait(const struct dpif *dpif_)
588 {
589     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
590     if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
591         poll_immediate_wake();
592     } else {
593         rtnetlink_link_notifier_wait();
594     }
595 }
596
597 static int
598 dpif_linux_flow_get__(const struct dpif *dpif_,
599                       const struct nlattr *key, size_t key_len,
600                       struct dpif_linux_flow *reply, struct ofpbuf **bufp)
601 {
602     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
603     struct dpif_linux_flow request;
604
605     dpif_linux_flow_init(&request);
606     request.cmd = OVS_FLOW_CMD_GET;
607     request.dp_ifindex = dpif->dp_ifindex;
608     request.key = key;
609     request.key_len = key_len;
610     return dpif_linux_flow_transact(&request, reply, bufp);
611 }
612
613 static int
614 dpif_linux_flow_get(const struct dpif *dpif_,
615                     const struct nlattr *key, size_t key_len,
616                     struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
617 {
618     struct dpif_linux_flow reply;
619     struct ofpbuf *buf;
620     int error;
621
622     error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
623     if (!error) {
624         if (stats) {
625             dpif_linux_flow_get_stats(&reply, stats);
626         }
627         if (actionsp) {
628             buf->data = (void *) reply.actions;
629             buf->size = reply.actions_len;
630             *actionsp = buf;
631         } else {
632             ofpbuf_delete(buf);
633         }
634     }
635     return error;
636 }
637
638 static int
639 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
640                     const struct nlattr *key, size_t key_len,
641                     const struct nlattr *actions, size_t actions_len,
642                     struct dpif_flow_stats *stats)
643 {
644     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
645     struct dpif_linux_flow request, reply;
646     struct nlattr dummy_action;
647     struct ofpbuf *buf;
648     int error;
649
650     dpif_linux_flow_init(&request);
651     request.cmd = flags & DPIF_FP_CREATE ? OVS_FLOW_CMD_NEW : OVS_FLOW_CMD_SET;
652     request.dp_ifindex = dpif->dp_ifindex;
653     request.key = key;
654     request.key_len = key_len;
655     /* Ensure that OVS_FLOW_ATTR_ACTIONS will always be included. */
656     request.actions = actions ? actions : &dummy_action;
657     request.actions_len = actions_len;
658     if (flags & DPIF_FP_ZERO_STATS) {
659         request.clear = true;
660     }
661     request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
662     error = dpif_linux_flow_transact(&request,
663                                      stats ? &reply : NULL,
664                                      stats ? &buf : NULL);
665     if (!error && stats) {
666         dpif_linux_flow_get_stats(&reply, stats);
667         ofpbuf_delete(buf);
668     }
669     return error;
670 }
671
672 static int
673 dpif_linux_flow_del(struct dpif *dpif_,
674                     const struct nlattr *key, size_t key_len,
675                     struct dpif_flow_stats *stats)
676 {
677     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
678     struct dpif_linux_flow request, reply;
679     struct ofpbuf *buf;
680     int error;
681
682     dpif_linux_flow_init(&request);
683     request.cmd = OVS_FLOW_CMD_DEL;
684     request.dp_ifindex = dpif->dp_ifindex;
685     request.key = key;
686     request.key_len = key_len;
687     error = dpif_linux_flow_transact(&request,
688                                      stats ? &reply : NULL,
689                                      stats ? &buf : NULL);
690     if (!error && stats) {
691         dpif_linux_flow_get_stats(&reply, stats);
692         ofpbuf_delete(buf);
693     }
694     return error;
695 }
696
697 struct dpif_linux_flow_state {
698     struct nl_dump dump;
699     struct dpif_linux_flow flow;
700     struct dpif_flow_stats stats;
701     struct ofpbuf *buf;
702 };
703
704 static int
705 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
706 {
707     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
708     struct dpif_linux_flow_state *state;
709     struct dpif_linux_flow request;
710     struct ofpbuf *buf;
711
712     *statep = state = xmalloc(sizeof *state);
713
714     dpif_linux_flow_init(&request);
715     request.cmd = OVS_DP_CMD_GET;
716     request.dp_ifindex = dpif->dp_ifindex;
717
718     buf = ofpbuf_new(1024);
719     dpif_linux_flow_to_ofpbuf(&request, buf);
720     nl_dump_start(&state->dump, genl_sock, buf);
721     ofpbuf_delete(buf);
722
723     state->buf = NULL;
724
725     return 0;
726 }
727
728 static int
729 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
730                           const struct nlattr **key, size_t *key_len,
731                           const struct nlattr **actions, size_t *actions_len,
732                           const struct dpif_flow_stats **stats)
733 {
734     struct dpif_linux_flow_state *state = state_;
735     struct ofpbuf buf;
736     int error;
737
738     do {
739         ofpbuf_delete(state->buf);
740         state->buf = NULL;
741
742         if (!nl_dump_next(&state->dump, &buf)) {
743             return EOF;
744         }
745
746         error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
747         if (error) {
748             return error;
749         }
750
751         if (actions && !state->flow.actions) {
752             error = dpif_linux_flow_get__(dpif_, state->flow.key,
753                                           state->flow.key_len,
754                                           &state->flow, &state->buf);
755             if (error == ENOENT) {
756                 VLOG_DBG("dumped flow disappeared on get");
757             } else if (error) {
758                 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
759             }
760         }
761     } while (error);
762
763     if (actions) {
764         *actions = state->flow.actions;
765         *actions_len = state->flow.actions_len;
766     }
767     if (key) {
768         *key = state->flow.key;
769         *key_len = state->flow.key_len;
770     }
771     if (stats) {
772         dpif_linux_flow_get_stats(&state->flow, &state->stats);
773         *stats = &state->stats;
774     }
775     return error;
776 }
777
778 static int
779 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
780 {
781     struct dpif_linux_flow_state *state = state_;
782     int error = nl_dump_done(&state->dump);
783     ofpbuf_delete(state->buf);
784     free(state);
785     return error;
786 }
787
788 static int
789 dpif_linux_execute__(int dp_ifindex,
790                      const struct nlattr *key, size_t key_len,
791                      const struct nlattr *actions, size_t actions_len,
792                      const struct ofpbuf *packet)
793 {
794     struct ovs_header *execute;
795     struct ofpbuf *buf;
796     int error;
797
798     buf = ofpbuf_new(128 + actions_len + packet->size);
799
800     nl_msg_put_genlmsghdr(buf, 0, ovs_packet_family, NLM_F_REQUEST,
801                           OVS_PACKET_CMD_EXECUTE, 1);
802
803     execute = ofpbuf_put_uninit(buf, sizeof *execute);
804     execute->dp_ifindex = dp_ifindex;
805
806     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_PACKET, packet->data, packet->size);
807     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_KEY, key, key_len);
808     nl_msg_put_unspec(buf, OVS_PACKET_ATTR_ACTIONS, actions, actions_len);
809
810     error = nl_sock_transact(genl_sock, buf, NULL);
811     ofpbuf_delete(buf);
812     return error;
813 }
814
815 static int
816 dpif_linux_execute(struct dpif *dpif_,
817                    const struct nlattr *key, size_t key_len,
818                    const struct nlattr *actions, size_t actions_len,
819                    const struct ofpbuf *packet)
820 {
821     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
822
823     return dpif_linux_execute__(dpif->dp_ifindex, key, key_len,
824                                 actions, actions_len, packet);
825 }
826
827 static int
828 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
829 {
830     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
831     *listen_mask = dpif->listen_mask;
832     return 0;
833 }
834
835 static int
836 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
837 {
838     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
839     int error;
840     int i;
841
842     if (listen_mask == dpif->listen_mask) {
843         return 0;
844     } else if (!listen_mask) {
845         nl_sock_destroy(dpif->mc_sock);
846         dpif->mc_sock = NULL;
847         dpif->listen_mask = 0;
848         return 0;
849     } else if (!dpif->mc_sock) {
850         error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
851         if (error) {
852             return error;
853         }
854     }
855
856     /* Unsubscribe from old groups. */
857     for (i = 0; i < DPIF_N_UC_TYPES; i++) {
858         if (dpif->listen_mask & (1u << i)) {
859             nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
860         }
861     }
862
863     /* Update listen_mask. */
864     dpif->listen_mask = listen_mask;
865
866     /* Subscribe to new groups. */
867     error = 0;
868     for (i = 0; i < DPIF_N_UC_TYPES; i++) {
869         if (dpif->listen_mask & (1u << i)) {
870             int retval;
871
872             retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
873             if (retval) {
874                 error = retval;
875             }
876         }
877     }
878     return error;
879 }
880
881 static int
882 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
883                                  uint32_t *probability)
884 {
885     struct dpif_linux_dp dp;
886     struct ofpbuf *buf;
887     int error;
888
889     error = dpif_linux_dp_get(dpif_, &dp, &buf);
890     if (!error) {
891         *probability = dp.sampling ? *dp.sampling : 0;
892         ofpbuf_delete(buf);
893     }
894     return error;
895 }
896
897 static int
898 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
899 {
900     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
901     struct dpif_linux_dp dp;
902
903     dpif_linux_dp_init(&dp);
904     dp.cmd = OVS_DP_CMD_SET;
905     dp.dp_ifindex = dpif->dp_ifindex;
906     dp.sampling = &probability;
907     return dpif_linux_dp_transact(&dp, NULL, NULL);
908 }
909
910 static int
911 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
912                              uint32_t queue_id, uint32_t *priority)
913 {
914     if (queue_id < 0xf000) {
915         *priority = TC_H_MAKE(1 << 16, queue_id + 1);
916         return 0;
917     } else {
918         return EINVAL;
919     }
920 }
921
922 static int
923 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
924                  int *dp_ifindex)
925 {
926     static const struct nl_policy ovs_packet_policy[] = {
927         /* Always present. */
928         [OVS_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
929                                      .min_len = ETH_HEADER_LEN },
930         [OVS_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
931
932         /* OVS_PACKET_CMD_ACTION only. */
933         [OVS_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
934
935         /* OVS_PACKET_CMD_SAMPLE only. */
936         [OVS_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
937         [OVS_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
938     };
939
940     struct ovs_header *ovs_header;
941     struct nlattr *a[ARRAY_SIZE(ovs_packet_policy)];
942     struct nlmsghdr *nlmsg;
943     struct genlmsghdr *genl;
944     struct ofpbuf b;
945     int type;
946
947     ofpbuf_use_const(&b, buf->data, buf->size);
948
949     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
950     genl = ofpbuf_try_pull(&b, sizeof *genl);
951     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
952     if (!nlmsg || !genl || !ovs_header
953         || nlmsg->nlmsg_type != ovs_packet_family
954         || !nl_policy_parse(&b, 0, ovs_packet_policy, a,
955                             ARRAY_SIZE(ovs_packet_policy))) {
956         return EINVAL;
957     }
958
959     type = (genl->cmd == OVS_PACKET_CMD_MISS ? DPIF_UC_MISS
960             : genl->cmd == OVS_PACKET_CMD_ACTION ? DPIF_UC_ACTION
961             : genl->cmd == OVS_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
962             : -1);
963     if (type < 0) {
964         return EINVAL;
965     }
966
967     memset(upcall, 0, sizeof *upcall);
968     upcall->type = type;
969     upcall->packet = buf;
970     upcall->packet->data = (void *) nl_attr_get(a[OVS_PACKET_ATTR_PACKET]);
971     upcall->packet->size = nl_attr_get_size(a[OVS_PACKET_ATTR_PACKET]);
972     upcall->key = (void *) nl_attr_get(a[OVS_PACKET_ATTR_KEY]);
973     upcall->key_len = nl_attr_get_size(a[OVS_PACKET_ATTR_KEY]);
974     upcall->userdata = (a[OVS_PACKET_ATTR_USERDATA]
975                         ? nl_attr_get_u64(a[OVS_PACKET_ATTR_USERDATA])
976                         : 0);
977     upcall->sample_pool = (a[OVS_PACKET_ATTR_SAMPLE_POOL]
978                         ? nl_attr_get_u32(a[OVS_PACKET_ATTR_SAMPLE_POOL])
979                            : 0);
980     if (a[OVS_PACKET_ATTR_ACTIONS]) {
981         upcall->actions = (void *) nl_attr_get(a[OVS_PACKET_ATTR_ACTIONS]);
982         upcall->actions_len = nl_attr_get_size(a[OVS_PACKET_ATTR_ACTIONS]);
983     }
984
985     *dp_ifindex = ovs_header->dp_ifindex;
986
987     return 0;
988 }
989
990 static int
991 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
992 {
993     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
994     struct ofpbuf *buf;
995     int error;
996     int i;
997
998     if (!dpif->mc_sock) {
999         return EAGAIN;
1000     }
1001
1002     for (i = 0; i < 50; i++) {
1003         int dp_ifindex;
1004
1005         error = nl_sock_recv(dpif->mc_sock, &buf, false);
1006         if (error) {
1007             return error;
1008         }
1009
1010         error = parse_odp_packet(buf, upcall, &dp_ifindex);
1011         if (!error
1012             && dp_ifindex == dpif->dp_ifindex
1013             && dpif->listen_mask & (1u << upcall->type)) {
1014             return 0;
1015         }
1016
1017         ofpbuf_delete(buf);
1018         if (error) {
1019             return error;
1020         }
1021     }
1022
1023     return EAGAIN;
1024 }
1025
1026 static void
1027 dpif_linux_recv_wait(struct dpif *dpif_)
1028 {
1029     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1030     if (dpif->mc_sock) {
1031         nl_sock_wait(dpif->mc_sock, POLLIN);
1032     }
1033 }
1034
1035 static void
1036 dpif_linux_recv_purge(struct dpif *dpif_)
1037 {
1038     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1039
1040     if (dpif->mc_sock) {
1041         nl_sock_drain(dpif->mc_sock);
1042     }
1043 }
1044
1045 const struct dpif_class dpif_linux_class = {
1046     "system",
1047     dpif_linux_enumerate,
1048     dpif_linux_open,
1049     dpif_linux_close,
1050     dpif_linux_destroy,
1051     NULL,                       /* run */
1052     NULL,                       /* wait */
1053     dpif_linux_get_stats,
1054     dpif_linux_get_drop_frags,
1055     dpif_linux_set_drop_frags,
1056     dpif_linux_port_add,
1057     dpif_linux_port_del,
1058     dpif_linux_port_query_by_number,
1059     dpif_linux_port_query_by_name,
1060     dpif_linux_get_max_ports,
1061     dpif_linux_port_dump_start,
1062     dpif_linux_port_dump_next,
1063     dpif_linux_port_dump_done,
1064     dpif_linux_port_poll,
1065     dpif_linux_port_poll_wait,
1066     dpif_linux_flow_get,
1067     dpif_linux_flow_put,
1068     dpif_linux_flow_del,
1069     dpif_linux_flow_flush,
1070     dpif_linux_flow_dump_start,
1071     dpif_linux_flow_dump_next,
1072     dpif_linux_flow_dump_done,
1073     dpif_linux_execute,
1074     dpif_linux_recv_get_mask,
1075     dpif_linux_recv_set_mask,
1076     dpif_linux_get_sflow_probability,
1077     dpif_linux_set_sflow_probability,
1078     dpif_linux_queue_to_priority,
1079     dpif_linux_recv,
1080     dpif_linux_recv_wait,
1081     dpif_linux_recv_purge,
1082 };
1083 \f
1084 static int
1085 dpif_linux_init(void)
1086 {
1087     static int error = -1;
1088
1089     if (error < 0) {
1090         error = nl_lookup_genl_family(OVS_DATAPATH_FAMILY,
1091                                       &ovs_datapath_family);
1092         if (error) {
1093             VLOG_ERR("Generic Netlink family '%s' does not exist. "
1094                      "The Open vSwitch kernel module is probably not loaded.",
1095                      OVS_DATAPATH_FAMILY);
1096         }
1097         if (!error) {
1098             error = nl_lookup_genl_family(OVS_VPORT_FAMILY, &ovs_vport_family);
1099         }
1100         if (!error) {
1101             error = nl_lookup_genl_family(OVS_FLOW_FAMILY, &ovs_flow_family);
1102         }
1103         if (!error) {
1104             error = nl_lookup_genl_family(OVS_PACKET_FAMILY,
1105                                           &ovs_packet_family);
1106         }
1107         if (!error) {
1108             error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1109         }
1110     }
1111
1112     return error;
1113 }
1114
1115 bool
1116 dpif_linux_is_internal_device(const char *name)
1117 {
1118     struct dpif_linux_vport reply;
1119     struct ofpbuf *buf;
1120     int error;
1121
1122     error = dpif_linux_vport_get(name, &reply, &buf);
1123     if (!error) {
1124         ofpbuf_delete(buf);
1125     } else if (error != ENODEV && error != ENOENT) {
1126         VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1127                      name, strerror(error));
1128     }
1129
1130     return reply.type == OVS_VPORT_TYPE_INTERNAL;
1131 }
1132
1133 int
1134 dpif_linux_vport_send(int dp_ifindex, uint32_t port_no,
1135                       const void *data, size_t size)
1136 {
1137     struct ofpbuf actions, key, packet;
1138     struct odputil_keybuf keybuf;
1139     struct flow flow;
1140     uint64_t action;
1141
1142     ofpbuf_use_const(&packet, data, size);
1143     flow_extract(&packet, htonll(0), 0, &flow);
1144
1145     ofpbuf_use_stack(&key, &keybuf, sizeof keybuf);
1146     odp_flow_key_from_flow(&key, &flow);
1147
1148     ofpbuf_use_stack(&actions, &action, sizeof action);
1149     nl_msg_put_u32(&actions, OVS_ACTION_ATTR_OUTPUT, port_no);
1150
1151     return dpif_linux_execute__(dp_ifindex, key.data, key.size,
1152                                 actions.data, actions.size, &packet);
1153 }
1154
1155 static void
1156 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1157                         void *dpif_)
1158 {
1159     struct dpif_linux *dpif = dpif_;
1160
1161     if (change) {
1162         if (change->master_ifindex == dpif->dp_ifindex
1163             && (change->nlmsg_type == RTM_NEWLINK
1164                 || change->nlmsg_type == RTM_DELLINK))
1165         {
1166             /* Our datapath changed, either adding a new port or deleting an
1167              * existing one. */
1168             sset_add(&dpif->changed_ports, change->ifname);
1169         }
1170     } else {
1171         dpif->change_error = true;
1172     }
1173 }
1174 \f
1175 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1176  * by Netlink attributes, into 'vport'.  Returns 0 if successful, otherwise a
1177  * positive errno value.
1178  *
1179  * 'vport' will contain pointers into 'buf', so the caller should not free
1180  * 'buf' while 'vport' is still in use. */
1181 static int
1182 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1183                              const struct ofpbuf *buf)
1184 {
1185     static const struct nl_policy ovs_vport_policy[] = {
1186         [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1187         [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1188         [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1189         [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1190                                    .min_len = sizeof(struct rtnl_link_stats64),
1191                                    .max_len = sizeof(struct rtnl_link_stats64),
1192                                    .optional = true },
1193         [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1194                                      .min_len = ETH_ADDR_LEN,
1195                                      .max_len = ETH_ADDR_LEN,
1196                                      .optional = true },
1197         [OVS_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1198         [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1199         [OVS_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1200         [OVS_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1201     };
1202
1203     struct nlattr *a[ARRAY_SIZE(ovs_vport_policy)];
1204     struct ovs_header *ovs_header;
1205     struct nlmsghdr *nlmsg;
1206     struct genlmsghdr *genl;
1207     struct ofpbuf b;
1208
1209     dpif_linux_vport_init(vport);
1210
1211     ofpbuf_use_const(&b, buf->data, buf->size);
1212     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1213     genl = ofpbuf_try_pull(&b, sizeof *genl);
1214     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1215     if (!nlmsg || !genl || !ovs_header
1216         || nlmsg->nlmsg_type != ovs_vport_family
1217         || !nl_policy_parse(&b, 0, ovs_vport_policy, a,
1218                             ARRAY_SIZE(ovs_vport_policy))) {
1219         return EINVAL;
1220     }
1221
1222     vport->cmd = genl->cmd;
1223     vport->dp_ifindex = ovs_header->dp_ifindex;
1224     vport->port_no = nl_attr_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1225     vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1226     vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
1227     if (a[OVS_VPORT_ATTR_STATS]) {
1228         vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
1229     }
1230     if (a[OVS_VPORT_ATTR_ADDRESS]) {
1231         vport->address = nl_attr_get(a[OVS_VPORT_ATTR_ADDRESS]);
1232     }
1233     if (a[OVS_VPORT_ATTR_MTU]) {
1234         vport->mtu = nl_attr_get_u32(a[OVS_VPORT_ATTR_MTU]);
1235     } else {
1236         vport->mtu = INT_MAX;
1237     }
1238     if (a[OVS_VPORT_ATTR_OPTIONS]) {
1239         vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
1240         vport->options_len = nl_attr_get_size(a[OVS_VPORT_ATTR_OPTIONS]);
1241     }
1242     if (a[OVS_VPORT_ATTR_IFINDEX]) {
1243         vport->ifindex = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFINDEX]);
1244     }
1245     if (a[OVS_VPORT_ATTR_IFLINK]) {
1246         vport->iflink = nl_attr_get_u32(a[OVS_VPORT_ATTR_IFLINK]);
1247     }
1248     return 0;
1249 }
1250
1251 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1252  * followed by Netlink attributes corresponding to 'vport'. */
1253 static void
1254 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1255                            struct ofpbuf *buf)
1256 {
1257     struct ovs_header *ovs_header;
1258
1259     nl_msg_put_genlmsghdr(buf, 0, ovs_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1260                           vport->cmd, 1);
1261
1262     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1263     ovs_header->dp_ifindex = vport->dp_ifindex;
1264
1265     if (vport->port_no != UINT32_MAX) {
1266         nl_msg_put_u32(buf, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1267     }
1268
1269     if (vport->type != OVS_VPORT_TYPE_UNSPEC) {
1270         nl_msg_put_u32(buf, OVS_VPORT_ATTR_TYPE, vport->type);
1271     }
1272
1273     if (vport->name) {
1274         nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
1275     }
1276
1277     if (vport->stats) {
1278         nl_msg_put_unspec(buf, OVS_VPORT_ATTR_STATS,
1279                           vport->stats, sizeof *vport->stats);
1280     }
1281
1282     if (vport->address) {
1283         nl_msg_put_unspec(buf, OVS_VPORT_ATTR_ADDRESS,
1284                           vport->address, ETH_ADDR_LEN);
1285     }
1286
1287     if (vport->mtu && vport->mtu != INT_MAX) {
1288         nl_msg_put_u32(buf, OVS_VPORT_ATTR_MTU, vport->mtu);
1289     }
1290
1291     if (vport->options) {
1292         nl_msg_put_nested(buf, OVS_VPORT_ATTR_OPTIONS,
1293                           vport->options, vport->options_len);
1294     }
1295
1296     if (vport->ifindex) {
1297         nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFINDEX, vport->ifindex);
1298     }
1299
1300     if (vport->iflink) {
1301         nl_msg_put_u32(buf, OVS_VPORT_ATTR_IFLINK, vport->iflink);
1302     }
1303 }
1304
1305 /* Clears 'vport' to "empty" values. */
1306 void
1307 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1308 {
1309     memset(vport, 0, sizeof *vport);
1310     vport->port_no = UINT32_MAX;
1311 }
1312
1313 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1314  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1315  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1316  * result of the command is expected to be an ovs_vport also, which is decoded
1317  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1318  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1319 int
1320 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1321                           struct dpif_linux_vport *reply,
1322                           struct ofpbuf **bufp)
1323 {
1324     struct ofpbuf *request_buf;
1325     int error;
1326
1327     assert((reply != NULL) == (bufp != NULL));
1328
1329     error = dpif_linux_init();
1330     if (error) {
1331         if (reply) {
1332             *bufp = NULL;
1333             dpif_linux_vport_init(reply);
1334         }
1335         return error;
1336     }
1337
1338     request_buf = ofpbuf_new(1024);
1339     dpif_linux_vport_to_ofpbuf(request, request_buf);
1340     error = nl_sock_transact(genl_sock, request_buf, bufp);
1341     ofpbuf_delete(request_buf);
1342
1343     if (reply) {
1344         if (!error) {
1345             error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1346         }
1347         if (error) {
1348             dpif_linux_vport_init(reply);
1349             ofpbuf_delete(*bufp);
1350             *bufp = NULL;
1351         }
1352     }
1353     return error;
1354 }
1355
1356 /* Obtains information about the kernel vport named 'name' and stores it into
1357  * '*reply' and '*bufp'.  The caller must free '*bufp' when the reply is no
1358  * longer needed ('reply' will contain pointers into '*bufp').  */
1359 int
1360 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1361                      struct ofpbuf **bufp)
1362 {
1363     struct dpif_linux_vport request;
1364
1365     dpif_linux_vport_init(&request);
1366     request.cmd = OVS_VPORT_CMD_GET;
1367     request.name = name;
1368
1369     return dpif_linux_vport_transact(&request, reply, bufp);
1370 }
1371 \f
1372 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1373  * by Netlink attributes, into 'dp'.  Returns 0 if successful, otherwise a
1374  * positive errno value.
1375  *
1376  * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1377  * while 'dp' is still in use. */
1378 static int
1379 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1380 {
1381     static const struct nl_policy ovs_datapath_policy[] = {
1382         [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1383         [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1384                                 .min_len = sizeof(struct ovs_dp_stats),
1385                                 .max_len = sizeof(struct ovs_dp_stats),
1386                                 .optional = true },
1387         [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1388         [OVS_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1389         [OVS_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1390     };
1391
1392     struct nlattr *a[ARRAY_SIZE(ovs_datapath_policy)];
1393     struct ovs_header *ovs_header;
1394     struct nlmsghdr *nlmsg;
1395     struct genlmsghdr *genl;
1396     struct ofpbuf b;
1397
1398     dpif_linux_dp_init(dp);
1399
1400     ofpbuf_use_const(&b, buf->data, buf->size);
1401     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1402     genl = ofpbuf_try_pull(&b, sizeof *genl);
1403     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1404     if (!nlmsg || !genl || !ovs_header
1405         || nlmsg->nlmsg_type != ovs_datapath_family
1406         || !nl_policy_parse(&b, 0, ovs_datapath_policy, a,
1407                             ARRAY_SIZE(ovs_datapath_policy))) {
1408         return EINVAL;
1409     }
1410
1411     dp->cmd = genl->cmd;
1412     dp->dp_ifindex = ovs_header->dp_ifindex;
1413     dp->name = nl_attr_get_string(a[OVS_DP_ATTR_NAME]);
1414     if (a[OVS_DP_ATTR_STATS]) {
1415         /* Can't use structure assignment because Netlink doesn't ensure
1416          * sufficient alignment for 64-bit members. */
1417         memcpy(&dp->stats, nl_attr_get(a[OVS_DP_ATTR_STATS]),
1418                sizeof dp->stats);
1419     }
1420     if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1421         dp->ipv4_frags = nl_attr_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
1422     }
1423     if (a[OVS_DP_ATTR_SAMPLING]) {
1424         dp->sampling = nl_attr_get(a[OVS_DP_ATTR_SAMPLING]);
1425     }
1426
1427     if (a[OVS_DP_ATTR_MCGROUPS]) {
1428         static const struct nl_policy ovs_mcgroup_policy[] = {
1429             [OVS_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1430             [OVS_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1431             [OVS_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1432         };
1433
1434         struct nlattr *mcgroups[ARRAY_SIZE(ovs_mcgroup_policy)];
1435
1436         if (!nl_parse_nested(a[OVS_DP_ATTR_MCGROUPS], ovs_mcgroup_policy,
1437                              mcgroups, ARRAY_SIZE(ovs_mcgroup_policy))) {
1438             return EINVAL;
1439         }
1440
1441         if (mcgroups[OVS_PACKET_CMD_MISS]) {
1442             dp->mcgroups[DPIF_UC_MISS]
1443                 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_MISS]);
1444         }
1445         if (mcgroups[OVS_PACKET_CMD_ACTION]) {
1446             dp->mcgroups[DPIF_UC_ACTION]
1447                 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_ACTION]);
1448         }
1449         if (mcgroups[OVS_PACKET_CMD_SAMPLE]) {
1450             dp->mcgroups[DPIF_UC_SAMPLE]
1451                 = nl_attr_get_u32(mcgroups[OVS_PACKET_CMD_SAMPLE]);
1452         }
1453     }
1454
1455     return 0;
1456 }
1457
1458 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1459 static void
1460 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1461 {
1462     struct ovs_header *ovs_header;
1463
1464     nl_msg_put_genlmsghdr(buf, 0, ovs_datapath_family,
1465                           NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1466
1467     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1468     ovs_header->dp_ifindex = dp->dp_ifindex;
1469
1470     if (dp->name) {
1471         nl_msg_put_string(buf, OVS_DP_ATTR_NAME, dp->name);
1472     }
1473
1474     /* Skip OVS_DP_ATTR_STATS since we never have a reason to serialize it. */
1475
1476     if (dp->ipv4_frags) {
1477         nl_msg_put_u32(buf, OVS_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1478     }
1479
1480     if (dp->sampling) {
1481         nl_msg_put_u32(buf, OVS_DP_ATTR_SAMPLING, *dp->sampling);
1482     }
1483 }
1484
1485 /* Clears 'dp' to "empty" values. */
1486 static void
1487 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1488 {
1489     memset(dp, 0, sizeof *dp);
1490 }
1491
1492 static void
1493 dpif_linux_dp_dump_start(struct nl_dump *dump)
1494 {
1495     struct dpif_linux_dp request;
1496     struct ofpbuf *buf;
1497
1498     dpif_linux_dp_init(&request);
1499     request.cmd = OVS_DP_CMD_GET;
1500
1501     buf = ofpbuf_new(1024);
1502     dpif_linux_dp_to_ofpbuf(&request, buf);
1503     nl_dump_start(dump, genl_sock, buf);
1504     ofpbuf_delete(buf);
1505 }
1506
1507 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1508  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1509  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1510  * result of the command is expected to be of the same form, which is decoded
1511  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1512  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1513 static int
1514 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1515                        struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1516 {
1517     struct ofpbuf *request_buf;
1518     int error;
1519
1520     assert((reply != NULL) == (bufp != NULL));
1521
1522     request_buf = ofpbuf_new(1024);
1523     dpif_linux_dp_to_ofpbuf(request, request_buf);
1524     error = nl_sock_transact(genl_sock, request_buf, bufp);
1525     ofpbuf_delete(request_buf);
1526
1527     if (reply) {
1528         if (!error) {
1529             error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1530         }
1531         if (error) {
1532             dpif_linux_dp_init(reply);
1533             ofpbuf_delete(*bufp);
1534             *bufp = NULL;
1535         }
1536     }
1537     return error;
1538 }
1539
1540 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1541  * The caller must free '*bufp' when the reply is no longer needed ('reply'
1542  * will contain pointers into '*bufp').  */
1543 static int
1544 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1545                   struct ofpbuf **bufp)
1546 {
1547     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1548     struct dpif_linux_dp request;
1549
1550     dpif_linux_dp_init(&request);
1551     request.cmd = OVS_DP_CMD_GET;
1552     request.dp_ifindex = dpif->dp_ifindex;
1553
1554     return dpif_linux_dp_transact(&request, reply, bufp);
1555 }
1556 \f
1557 /* Parses the contents of 'buf', which contains a "struct ovs_header" followed
1558  * by Netlink attributes, into 'flow'.  Returns 0 if successful, otherwise a
1559  * positive errno value.
1560  *
1561  * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1562  * while 'flow' is still in use. */
1563 static int
1564 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1565                             const struct ofpbuf *buf)
1566 {
1567     static const struct nl_policy ovs_flow_policy[] = {
1568         [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1569         [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1570         [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1571                                   .min_len = sizeof(struct ovs_flow_stats),
1572                                   .max_len = sizeof(struct ovs_flow_stats),
1573                                   .optional = true },
1574         [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1575         [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1576         /* The kernel never uses OVS_FLOW_ATTR_CLEAR. */
1577     };
1578
1579     struct nlattr *a[ARRAY_SIZE(ovs_flow_policy)];
1580     struct ovs_header *ovs_header;
1581     struct nlmsghdr *nlmsg;
1582     struct genlmsghdr *genl;
1583     struct ofpbuf b;
1584
1585     dpif_linux_flow_init(flow);
1586
1587     ofpbuf_use_const(&b, buf->data, buf->size);
1588     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1589     genl = ofpbuf_try_pull(&b, sizeof *genl);
1590     ovs_header = ofpbuf_try_pull(&b, sizeof *ovs_header);
1591     if (!nlmsg || !genl || !ovs_header
1592         || nlmsg->nlmsg_type != ovs_flow_family
1593         || !nl_policy_parse(&b, 0, ovs_flow_policy, a,
1594                             ARRAY_SIZE(ovs_flow_policy))) {
1595         return EINVAL;
1596     }
1597
1598     flow->nlmsg_flags = nlmsg->nlmsg_flags;
1599     flow->dp_ifindex = ovs_header->dp_ifindex;
1600     flow->key = nl_attr_get(a[OVS_FLOW_ATTR_KEY]);
1601     flow->key_len = nl_attr_get_size(a[OVS_FLOW_ATTR_KEY]);
1602     if (a[OVS_FLOW_ATTR_ACTIONS]) {
1603         flow->actions = nl_attr_get(a[OVS_FLOW_ATTR_ACTIONS]);
1604         flow->actions_len = nl_attr_get_size(a[OVS_FLOW_ATTR_ACTIONS]);
1605     }
1606     if (a[OVS_FLOW_ATTR_STATS]) {
1607         flow->stats = nl_attr_get(a[OVS_FLOW_ATTR_STATS]);
1608     }
1609     if (a[OVS_FLOW_ATTR_TCP_FLAGS]) {
1610         flow->tcp_flags = nl_attr_get(a[OVS_FLOW_ATTR_TCP_FLAGS]);
1611     }
1612     if (a[OVS_FLOW_ATTR_USED]) {
1613         flow->used = nl_attr_get(a[OVS_FLOW_ATTR_USED]);
1614     }
1615     return 0;
1616 }
1617
1618 /* Appends to 'buf' (which must initially be empty) a "struct ovs_header"
1619  * followed by Netlink attributes corresponding to 'flow'. */
1620 static void
1621 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1622                           struct ofpbuf *buf)
1623 {
1624     struct ovs_header *ovs_header;
1625
1626     nl_msg_put_genlmsghdr(buf, 0, ovs_flow_family,
1627                           NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1628                           flow->cmd, 1);
1629
1630     ovs_header = ofpbuf_put_uninit(buf, sizeof *ovs_header);
1631     ovs_header->dp_ifindex = flow->dp_ifindex;
1632
1633     if (flow->key_len) {
1634         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_KEY, flow->key, flow->key_len);
1635     }
1636
1637     if (flow->actions || flow->actions_len) {
1638         nl_msg_put_unspec(buf, OVS_FLOW_ATTR_ACTIONS,
1639                           flow->actions, flow->actions_len);
1640     }
1641
1642     /* We never need to send these to the kernel. */
1643     assert(!flow->stats);
1644     assert(!flow->tcp_flags);
1645     assert(!flow->used);
1646
1647     if (flow->clear) {
1648         nl_msg_put_flag(buf, OVS_FLOW_ATTR_CLEAR);
1649     }
1650 }
1651
1652 /* Clears 'flow' to "empty" values. */
1653 static void
1654 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1655 {
1656     memset(flow, 0, sizeof *flow);
1657 }
1658
1659 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1660  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1661  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1662  * result of the command is expected to be a flow also, which is decoded and
1663  * stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the reply
1664  * is no longer needed ('reply' will contain pointers into '*bufp'). */
1665 static int
1666 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1667                          struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1668 {
1669     struct ofpbuf *request_buf;
1670     int error;
1671
1672     assert((reply != NULL) == (bufp != NULL));
1673
1674     request_buf = ofpbuf_new(1024);
1675     dpif_linux_flow_to_ofpbuf(request, request_buf);
1676     error = nl_sock_transact(genl_sock, request_buf, bufp);
1677     ofpbuf_delete(request_buf);
1678
1679     if (reply) {
1680         if (!error) {
1681             error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1682         }
1683         if (error) {
1684             dpif_linux_flow_init(reply);
1685             ofpbuf_delete(*bufp);
1686             *bufp = NULL;
1687         }
1688     }
1689     return error;
1690 }
1691
1692 static void
1693 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1694                           struct dpif_flow_stats *stats)
1695 {
1696     if (flow->stats) {
1697         stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1698         stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1699     } else {
1700         stats->n_packets = 0;
1701         stats->n_bytes = 0;
1702     }
1703     stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1704     stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1705 }
1706