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