dpif-linux: Avoid segfault on netdev_get_stats() without kernel module.
[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 };
476
477 static int
478 dpif_linux_port_dump_start(const struct dpif *dpif_, void **statep)
479 {
480     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
481     struct dpif_linux_port_state *state;
482     struct dpif_linux_vport request;
483     struct ofpbuf *buf;
484
485     *statep = state = xmalloc(sizeof *state);
486
487     dpif_linux_vport_init(&request);
488     request.cmd = ODP_DP_CMD_GET;
489     request.dp_ifindex = dpif->dp_ifindex;
490
491     buf = ofpbuf_new(1024);
492     dpif_linux_vport_to_ofpbuf(&request, buf);
493     nl_dump_start(&state->dump, genl_sock, buf);
494     ofpbuf_delete(buf);
495
496     return 0;
497 }
498
499 static int
500 dpif_linux_port_dump_next(const struct dpif *dpif OVS_UNUSED, void *state_,
501                           struct dpif_port *dpif_port)
502 {
503     struct dpif_linux_port_state *state = state_;
504     struct dpif_linux_vport vport;
505     struct ofpbuf buf;
506     int error;
507
508     if (!nl_dump_next(&state->dump, &buf)) {
509         return EOF;
510     }
511
512     error = dpif_linux_vport_from_ofpbuf(&vport, &buf);
513     if (error) {
514         return error;
515     }
516
517     dpif_port->name = (char *) vport.name;
518     dpif_port->type = (char *) netdev_vport_get_netdev_type(&vport);
519     dpif_port->port_no = vport.port_no;
520     return 0;
521 }
522
523 static int
524 dpif_linux_port_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
525 {
526     struct dpif_linux_port_state *state = state_;
527     int error = nl_dump_done(&state->dump);
528     free(state);
529     return error;
530 }
531
532 static int
533 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
534 {
535     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
536
537     if (dpif->change_error) {
538         dpif->change_error = false;
539         sset_clear(&dpif->changed_ports);
540         return ENOBUFS;
541     } else if (!sset_is_empty(&dpif->changed_ports)) {
542         *devnamep = sset_pop(&dpif->changed_ports);
543         return 0;
544     } else {
545         return EAGAIN;
546     }
547 }
548
549 static void
550 dpif_linux_port_poll_wait(const struct dpif *dpif_)
551 {
552     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
553     if (!sset_is_empty(&dpif->changed_ports) || dpif->change_error) {
554         poll_immediate_wake();
555     } else {
556         rtnetlink_link_notifier_wait();
557     }
558 }
559
560 static int
561 dpif_linux_flow_get__(const struct dpif *dpif_,
562                       const struct nlattr *key, size_t key_len,
563                       struct dpif_linux_flow *reply, struct ofpbuf **bufp)
564 {
565     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
566     struct dpif_linux_flow request;
567
568     dpif_linux_flow_init(&request);
569     request.cmd = ODP_FLOW_CMD_GET;
570     request.dp_ifindex = dpif->dp_ifindex;
571     request.key = key;
572     request.key_len = key_len;
573     return dpif_linux_flow_transact(&request, reply, bufp);
574 }
575
576 static int
577 dpif_linux_flow_get(const struct dpif *dpif_,
578                     const struct nlattr *key, size_t key_len,
579                     struct ofpbuf **actionsp, struct dpif_flow_stats *stats)
580 {
581     struct dpif_linux_flow reply;
582     struct ofpbuf *buf;
583     int error;
584
585     error = dpif_linux_flow_get__(dpif_, key, key_len, &reply, &buf);
586     if (!error) {
587         if (stats) {
588             dpif_linux_flow_get_stats(&reply, stats);
589         }
590         if (actionsp) {
591             buf->data = (void *) reply.actions;
592             buf->size = reply.actions_len;
593             *actionsp = buf;
594         } else {
595             ofpbuf_delete(buf);
596         }
597     }
598     return error;
599 }
600
601 static int
602 dpif_linux_flow_put(struct dpif *dpif_, enum dpif_flow_put_flags flags,
603                     const struct nlattr *key, size_t key_len,
604                     const struct nlattr *actions, size_t actions_len,
605                     struct dpif_flow_stats *stats)
606 {
607     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
608     struct dpif_linux_flow request, reply;
609     struct nlattr dummy_action;
610     struct ofpbuf *buf;
611     int error;
612
613     dpif_linux_flow_init(&request);
614     request.cmd = flags & DPIF_FP_CREATE ? ODP_FLOW_CMD_NEW : ODP_FLOW_CMD_SET;
615     request.dp_ifindex = dpif->dp_ifindex;
616     request.key = key;
617     request.key_len = key_len;
618     /* Ensure that ODP_FLOW_ATTR_ACTIONS will always be included. */
619     request.actions = actions ? actions : &dummy_action;
620     request.actions_len = actions_len;
621     if (flags & DPIF_FP_ZERO_STATS) {
622         request.clear = true;
623     }
624     request.nlmsg_flags = flags & DPIF_FP_MODIFY ? 0 : NLM_F_CREATE;
625     error = dpif_linux_flow_transact(&request,
626                                      stats ? &reply : NULL,
627                                      stats ? &buf : NULL);
628     if (!error && stats) {
629         dpif_linux_flow_get_stats(&reply, stats);
630         ofpbuf_delete(buf);
631     }
632     return error;
633 }
634
635 static int
636 dpif_linux_flow_del(struct dpif *dpif_,
637                     const struct nlattr *key, size_t key_len,
638                     struct dpif_flow_stats *stats)
639 {
640     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
641     struct dpif_linux_flow request, reply;
642     struct ofpbuf *buf;
643     int error;
644
645     dpif_linux_flow_init(&request);
646     request.cmd = ODP_FLOW_CMD_DEL;
647     request.dp_ifindex = dpif->dp_ifindex;
648     request.key = key;
649     request.key_len = key_len;
650     error = dpif_linux_flow_transact(&request,
651                                      stats ? &reply : NULL,
652                                      stats ? &buf : NULL);
653     if (!error && stats) {
654         dpif_linux_flow_get_stats(&reply, stats);
655         ofpbuf_delete(buf);
656     }
657     return error;
658 }
659
660 struct dpif_linux_flow_state {
661     struct nl_dump dump;
662     struct dpif_linux_flow flow;
663     struct dpif_flow_stats stats;
664     struct ofpbuf *buf;
665 };
666
667 static int
668 dpif_linux_flow_dump_start(const struct dpif *dpif_, void **statep)
669 {
670     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
671     struct dpif_linux_flow_state *state;
672     struct dpif_linux_flow request;
673     struct ofpbuf *buf;
674
675     *statep = state = xmalloc(sizeof *state);
676
677     dpif_linux_flow_init(&request);
678     request.cmd = ODP_DP_CMD_GET;
679     request.dp_ifindex = dpif->dp_ifindex;
680
681     buf = ofpbuf_new(1024);
682     dpif_linux_flow_to_ofpbuf(&request, buf);
683     nl_dump_start(&state->dump, genl_sock, buf);
684     ofpbuf_delete(buf);
685
686     state->buf = NULL;
687
688     return 0;
689 }
690
691 static int
692 dpif_linux_flow_dump_next(const struct dpif *dpif_ OVS_UNUSED, void *state_,
693                           const struct nlattr **key, size_t *key_len,
694                           const struct nlattr **actions, size_t *actions_len,
695                           const struct dpif_flow_stats **stats)
696 {
697     struct dpif_linux_flow_state *state = state_;
698     struct ofpbuf buf;
699     int error;
700
701     do {
702         ofpbuf_delete(state->buf);
703         state->buf = NULL;
704
705         if (!nl_dump_next(&state->dump, &buf)) {
706             return EOF;
707         }
708
709         error = dpif_linux_flow_from_ofpbuf(&state->flow, &buf);
710         if (error) {
711             return error;
712         }
713
714         if (actions && !state->flow.actions) {
715             error = dpif_linux_flow_get__(dpif_, state->flow.key,
716                                           state->flow.key_len,
717                                           &state->flow, &state->buf);
718             if (error == ENOENT) {
719                 VLOG_DBG("dumped flow disappeared on get");
720             } else if (error) {
721                 VLOG_WARN("error fetching dumped flow: %s", strerror(error));
722             }
723         }
724     } while (error);
725
726     if (actions) {
727         *actions = state->flow.actions;
728         *actions_len = state->flow.actions_len;
729     }
730     if (key) {
731         *key = state->flow.key;
732         *key_len = state->flow.key_len;
733     }
734     if (stats) {
735         dpif_linux_flow_get_stats(&state->flow, &state->stats);
736         *stats = &state->stats;
737     }
738     return error;
739 }
740
741 static int
742 dpif_linux_flow_dump_done(const struct dpif *dpif OVS_UNUSED, void *state_)
743 {
744     struct dpif_linux_flow_state *state = state_;
745     int error = nl_dump_done(&state->dump);
746     ofpbuf_delete(state->buf);
747     free(state);
748     return error;
749 }
750
751 static int
752 dpif_linux_execute(struct dpif *dpif_,
753                    const struct nlattr *actions, size_t actions_len,
754                    const struct ofpbuf *packet)
755 {
756     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
757     struct odp_header *execute;
758     struct ofpbuf *buf;
759     int error;
760
761     buf = ofpbuf_new(128 + actions_len + packet->size);
762
763     nl_msg_put_genlmsghdr(buf, 0, odp_packet_family, NLM_F_REQUEST,
764                           ODP_PACKET_CMD_EXECUTE, 1);
765
766     execute = ofpbuf_put_uninit(buf, sizeof *execute);
767     execute->dp_ifindex = dpif->dp_ifindex;
768
769     nl_msg_put_unspec(buf, ODP_PACKET_ATTR_PACKET, packet->data, packet->size);
770     nl_msg_put_unspec(buf, ODP_PACKET_ATTR_ACTIONS, actions, actions_len);
771
772     error = nl_sock_transact(genl_sock, buf, NULL);
773     ofpbuf_delete(buf);
774     return error;
775 }
776
777 static int
778 dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
779 {
780     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
781     *listen_mask = dpif->listen_mask;
782     return 0;
783 }
784
785 static int
786 dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
787 {
788     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
789     int error;
790     int i;
791
792     if (listen_mask == dpif->listen_mask) {
793         return 0;
794     } else if (!listen_mask) {
795         nl_sock_destroy(dpif->mc_sock);
796         dpif->mc_sock = NULL;
797         dpif->listen_mask = 0;
798         return 0;
799     } else if (!dpif->mc_sock) {
800         error = nl_sock_create(NETLINK_GENERIC, &dpif->mc_sock);
801         if (error) {
802             return error;
803         }
804     }
805
806     /* Unsubscribe from old groups. */
807     for (i = 0; i < DPIF_N_UC_TYPES; i++) {
808         if (dpif->listen_mask & (1u << i)) {
809             nl_sock_leave_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
810         }
811     }
812
813     /* Update listen_mask. */
814     dpif->listen_mask = listen_mask;
815
816     /* Subscribe to new groups. */
817     error = 0;
818     for (i = 0; i < DPIF_N_UC_TYPES; i++) {
819         if (dpif->listen_mask & (1u << i)) {
820             int retval;
821
822             retval = nl_sock_join_mcgroup(dpif->mc_sock, dpif->mcgroups[i]);
823             if (retval) {
824                 error = retval;
825             }
826         }
827     }
828     return error;
829 }
830
831 static int
832 dpif_linux_get_sflow_probability(const struct dpif *dpif_,
833                                  uint32_t *probability)
834 {
835     struct dpif_linux_dp dp;
836     struct ofpbuf *buf;
837     int error;
838
839     error = dpif_linux_dp_get(dpif_, &dp, &buf);
840     if (!error) {
841         *probability = dp.sampling ? *dp.sampling : 0;
842         ofpbuf_delete(buf);
843     }
844     return error;
845 }
846
847 static int
848 dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
849 {
850     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
851     struct dpif_linux_dp dp;
852
853     dpif_linux_dp_init(&dp);
854     dp.cmd = ODP_DP_CMD_SET;
855     dp.dp_ifindex = dpif->dp_ifindex;
856     dp.sampling = &probability;
857     return dpif_linux_dp_transact(&dp, NULL, NULL);
858 }
859
860 static int
861 dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
862                              uint32_t queue_id, uint32_t *priority)
863 {
864     if (queue_id < 0xf000) {
865         *priority = TC_H_MAKE(1 << 16, queue_id + 1);
866         return 0;
867     } else {
868         return EINVAL;
869     }
870 }
871
872 static int
873 parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
874                  int *dp_ifindex)
875 {
876     static const struct nl_policy odp_packet_policy[] = {
877         /* Always present. */
878         [ODP_PACKET_ATTR_PACKET] = { .type = NL_A_UNSPEC,
879                                      .min_len = ETH_HEADER_LEN },
880         [ODP_PACKET_ATTR_KEY] = { .type = NL_A_NESTED },
881
882         /* ODP_PACKET_CMD_ACTION only. */
883         [ODP_PACKET_ATTR_USERDATA] = { .type = NL_A_U64, .optional = true },
884
885         /* ODP_PACKET_CMD_SAMPLE only. */
886         [ODP_PACKET_ATTR_SAMPLE_POOL] = { .type = NL_A_U32, .optional = true },
887         [ODP_PACKET_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
888     };
889
890     struct odp_header *odp_header;
891     struct nlattr *a[ARRAY_SIZE(odp_packet_policy)];
892     struct nlmsghdr *nlmsg;
893     struct genlmsghdr *genl;
894     struct ofpbuf b;
895     int type;
896
897     ofpbuf_use_const(&b, buf->data, buf->size);
898
899     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
900     genl = ofpbuf_try_pull(&b, sizeof *genl);
901     odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
902     if (!nlmsg || !genl || !odp_header
903         || nlmsg->nlmsg_type != odp_packet_family
904         || !nl_policy_parse(&b, 0, odp_packet_policy, a,
905                             ARRAY_SIZE(odp_packet_policy))) {
906         return EINVAL;
907     }
908
909     type = (genl->cmd == ODP_PACKET_CMD_MISS ? DPIF_UC_MISS
910             : genl->cmd == ODP_PACKET_CMD_ACTION ? DPIF_UC_ACTION
911             : genl->cmd == ODP_PACKET_CMD_SAMPLE ? DPIF_UC_SAMPLE
912             : -1);
913     if (type < 0) {
914         return EINVAL;
915     }
916
917     memset(upcall, 0, sizeof *upcall);
918     upcall->type = type;
919     upcall->packet = buf;
920     upcall->packet->data = (void *) nl_attr_get(a[ODP_PACKET_ATTR_PACKET]);
921     upcall->packet->size = nl_attr_get_size(a[ODP_PACKET_ATTR_PACKET]);
922     upcall->key = (void *) nl_attr_get(a[ODP_PACKET_ATTR_KEY]);
923     upcall->key_len = nl_attr_get_size(a[ODP_PACKET_ATTR_KEY]);
924     upcall->userdata = (a[ODP_PACKET_ATTR_USERDATA]
925                         ? nl_attr_get_u64(a[ODP_PACKET_ATTR_USERDATA])
926                         : 0);
927     upcall->sample_pool = (a[ODP_PACKET_ATTR_SAMPLE_POOL]
928                         ? nl_attr_get_u32(a[ODP_PACKET_ATTR_SAMPLE_POOL])
929                            : 0);
930     if (a[ODP_PACKET_ATTR_ACTIONS]) {
931         upcall->actions = (void *) nl_attr_get(a[ODP_PACKET_ATTR_ACTIONS]);
932         upcall->actions_len = nl_attr_get_size(a[ODP_PACKET_ATTR_ACTIONS]);
933     }
934
935     *dp_ifindex = odp_header->dp_ifindex;
936
937     return 0;
938 }
939
940 static int
941 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
942 {
943     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
944     struct ofpbuf *buf;
945     int error;
946     int i;
947
948     if (!dpif->mc_sock) {
949         return EAGAIN;
950     }
951
952     for (i = 0; i < 50; i++) {
953         int dp_ifindex;
954
955         error = nl_sock_recv(dpif->mc_sock, &buf, false);
956         if (error) {
957             return error;
958         }
959
960         error = parse_odp_packet(buf, upcall, &dp_ifindex);
961         if (!error
962             && dp_ifindex == dpif->dp_ifindex
963             && dpif->listen_mask & (1u << upcall->type)) {
964             return 0;
965         }
966
967         ofpbuf_delete(buf);
968         if (error) {
969             return error;
970         }
971     }
972
973     return EAGAIN;
974 }
975
976 static void
977 dpif_linux_recv_wait(struct dpif *dpif_)
978 {
979     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
980     if (dpif->mc_sock) {
981         nl_sock_wait(dpif->mc_sock, POLLIN);
982     }
983 }
984
985 static void
986 dpif_linux_recv_purge(struct dpif *dpif_)
987 {
988     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
989
990     if (dpif->mc_sock) {
991         nl_sock_drain(dpif->mc_sock);
992     }
993 }
994
995 const struct dpif_class dpif_linux_class = {
996     "system",
997     NULL,                       /* run */
998     NULL,                       /* wait */
999     dpif_linux_enumerate,
1000     dpif_linux_open,
1001     dpif_linux_close,
1002     dpif_linux_destroy,
1003     dpif_linux_get_stats,
1004     dpif_linux_get_drop_frags,
1005     dpif_linux_set_drop_frags,
1006     dpif_linux_port_add,
1007     dpif_linux_port_del,
1008     dpif_linux_port_query_by_number,
1009     dpif_linux_port_query_by_name,
1010     dpif_linux_get_max_ports,
1011     dpif_linux_port_dump_start,
1012     dpif_linux_port_dump_next,
1013     dpif_linux_port_dump_done,
1014     dpif_linux_port_poll,
1015     dpif_linux_port_poll_wait,
1016     dpif_linux_flow_get,
1017     dpif_linux_flow_put,
1018     dpif_linux_flow_del,
1019     dpif_linux_flow_flush,
1020     dpif_linux_flow_dump_start,
1021     dpif_linux_flow_dump_next,
1022     dpif_linux_flow_dump_done,
1023     dpif_linux_execute,
1024     dpif_linux_recv_get_mask,
1025     dpif_linux_recv_set_mask,
1026     dpif_linux_get_sflow_probability,
1027     dpif_linux_set_sflow_probability,
1028     dpif_linux_queue_to_priority,
1029     dpif_linux_recv,
1030     dpif_linux_recv_wait,
1031     dpif_linux_recv_purge,
1032 };
1033 \f
1034 static int
1035 dpif_linux_init(void)
1036 {
1037     static int error = -1;
1038
1039     if (error < 0) {
1040         error = nl_lookup_genl_family(ODP_DATAPATH_FAMILY,
1041                                       &odp_datapath_family);
1042         if (error) {
1043             VLOG_ERR("Generic Netlink family '%s' does not exist. "
1044                      "The Open vSwitch kernel module is probably not loaded.",
1045                      ODP_DATAPATH_FAMILY);
1046         }
1047         if (!error) {
1048             error = nl_lookup_genl_family(ODP_VPORT_FAMILY, &odp_vport_family);
1049         }
1050         if (!error) {
1051             error = nl_lookup_genl_family(ODP_FLOW_FAMILY, &odp_flow_family);
1052         }
1053         if (!error) {
1054             error = nl_lookup_genl_family(ODP_PACKET_FAMILY,
1055                                           &odp_packet_family);
1056         }
1057         if (!error) {
1058             error = nl_sock_create(NETLINK_GENERIC, &genl_sock);
1059         }
1060     }
1061
1062     return error;
1063 }
1064
1065 bool
1066 dpif_linux_is_internal_device(const char *name)
1067 {
1068     struct dpif_linux_vport reply;
1069     struct ofpbuf *buf;
1070     int error;
1071
1072     error = dpif_linux_vport_get(name, &reply, &buf);
1073     if (!error) {
1074         ofpbuf_delete(buf);
1075     } else if (error != ENODEV) {
1076         VLOG_WARN_RL(&error_rl, "%s: vport query failed (%s)",
1077                      name, strerror(error));
1078     }
1079
1080     return reply.type == ODP_VPORT_TYPE_INTERNAL;
1081 }
1082
1083 static void
1084 dpif_linux_port_changed(const struct rtnetlink_link_change *change,
1085                         void *dpif_)
1086 {
1087     struct dpif_linux *dpif = dpif_;
1088
1089     if (change) {
1090         if (change->master_ifindex == dpif->dp_ifindex
1091             && (change->nlmsg_type == RTM_NEWLINK
1092                 || change->nlmsg_type == RTM_DELLINK))
1093         {
1094             /* Our datapath changed, either adding a new port or deleting an
1095              * existing one. */
1096             sset_add(&dpif->changed_ports, change->ifname);
1097         }
1098     } else {
1099         dpif->change_error = true;
1100     }
1101 }
1102 \f
1103 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1104  * by Netlink attributes, into 'vport'.  Returns 0 if successful, otherwise a
1105  * positive errno value.
1106  *
1107  * 'vport' will contain pointers into 'buf', so the caller should not free
1108  * 'buf' while 'vport' is still in use. */
1109 static int
1110 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
1111                              const struct ofpbuf *buf)
1112 {
1113     static const struct nl_policy odp_vport_policy[] = {
1114         [ODP_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
1115         [ODP_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
1116         [ODP_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1117         [ODP_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
1118                                    .min_len = sizeof(struct rtnl_link_stats64),
1119                                    .max_len = sizeof(struct rtnl_link_stats64),
1120                                    .optional = true },
1121         [ODP_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
1122                                      .min_len = ETH_ADDR_LEN,
1123                                      .max_len = ETH_ADDR_LEN,
1124                                      .optional = true },
1125         [ODP_VPORT_ATTR_MTU] = { .type = NL_A_U32, .optional = true },
1126         [ODP_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
1127         [ODP_VPORT_ATTR_IFINDEX] = { .type = NL_A_U32, .optional = true },
1128         [ODP_VPORT_ATTR_IFLINK] = { .type = NL_A_U32, .optional = true },
1129     };
1130
1131     struct nlattr *a[ARRAY_SIZE(odp_vport_policy)];
1132     struct odp_header *odp_header;
1133     struct nlmsghdr *nlmsg;
1134     struct genlmsghdr *genl;
1135     struct ofpbuf b;
1136
1137     dpif_linux_vport_init(vport);
1138
1139     ofpbuf_use_const(&b, buf->data, buf->size);
1140     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1141     genl = ofpbuf_try_pull(&b, sizeof *genl);
1142     odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1143     if (!nlmsg || !genl || !odp_header
1144         || nlmsg->nlmsg_type != odp_vport_family
1145         || !nl_policy_parse(&b, 0, odp_vport_policy, a,
1146                             ARRAY_SIZE(odp_vport_policy))) {
1147         return EINVAL;
1148     }
1149
1150     vport->cmd = genl->cmd;
1151     vport->dp_ifindex = odp_header->dp_ifindex;
1152     vport->port_no = nl_attr_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1153     vport->type = nl_attr_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1154     vport->name = nl_attr_get_string(a[ODP_VPORT_ATTR_NAME]);
1155     if (a[ODP_VPORT_ATTR_STATS]) {
1156         vport->stats = nl_attr_get(a[ODP_VPORT_ATTR_STATS]);
1157     }
1158     if (a[ODP_VPORT_ATTR_ADDRESS]) {
1159         vport->address = nl_attr_get(a[ODP_VPORT_ATTR_ADDRESS]);
1160     }
1161     if (a[ODP_VPORT_ATTR_MTU]) {
1162         vport->mtu = nl_attr_get_u32(a[ODP_VPORT_ATTR_MTU]);
1163     } else {
1164         vport->mtu = INT_MAX;
1165     }
1166     if (a[ODP_VPORT_ATTR_OPTIONS]) {
1167         vport->options = nl_attr_get(a[ODP_VPORT_ATTR_OPTIONS]);
1168         vport->options_len = nl_attr_get_size(a[ODP_VPORT_ATTR_OPTIONS]);
1169     }
1170     if (a[ODP_VPORT_ATTR_IFINDEX]) {
1171         vport->ifindex = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFINDEX]);
1172     }
1173     if (a[ODP_VPORT_ATTR_IFLINK]) {
1174         vport->iflink = nl_attr_get_u32(a[ODP_VPORT_ATTR_IFLINK]);
1175     }
1176     return 0;
1177 }
1178
1179 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1180  * followed by Netlink attributes corresponding to 'vport'. */
1181 static void
1182 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
1183                            struct ofpbuf *buf)
1184 {
1185     struct odp_header *odp_header;
1186
1187     nl_msg_put_genlmsghdr(buf, 0, odp_vport_family, NLM_F_REQUEST | NLM_F_ECHO,
1188                           vport->cmd, 1);
1189
1190     odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1191     odp_header->dp_ifindex = vport->dp_ifindex;
1192
1193     if (vport->port_no != UINT32_MAX) {
1194         nl_msg_put_u32(buf, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1195     }
1196
1197     if (vport->type != ODP_VPORT_TYPE_UNSPEC) {
1198         nl_msg_put_u32(buf, ODP_VPORT_ATTR_TYPE, vport->type);
1199     }
1200
1201     if (vport->name) {
1202         nl_msg_put_string(buf, ODP_VPORT_ATTR_NAME, vport->name);
1203     }
1204
1205     if (vport->stats) {
1206         nl_msg_put_unspec(buf, ODP_VPORT_ATTR_STATS,
1207                           vport->stats, sizeof *vport->stats);
1208     }
1209
1210     if (vport->address) {
1211         nl_msg_put_unspec(buf, ODP_VPORT_ATTR_ADDRESS,
1212                           vport->address, ETH_ADDR_LEN);
1213     }
1214
1215     if (vport->mtu && vport->mtu != INT_MAX) {
1216         nl_msg_put_u32(buf, ODP_VPORT_ATTR_MTU, vport->mtu);
1217     }
1218
1219     if (vport->options) {
1220         nl_msg_put_nested(buf, ODP_VPORT_ATTR_OPTIONS,
1221                           vport->options, vport->options_len);
1222     }
1223
1224     if (vport->ifindex) {
1225         nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFINDEX, vport->ifindex);
1226     }
1227
1228     if (vport->iflink) {
1229         nl_msg_put_u32(buf, ODP_VPORT_ATTR_IFLINK, vport->iflink);
1230     }
1231 }
1232
1233 /* Clears 'vport' to "empty" values. */
1234 void
1235 dpif_linux_vport_init(struct dpif_linux_vport *vport)
1236 {
1237     memset(vport, 0, sizeof *vport);
1238     vport->port_no = UINT32_MAX;
1239 }
1240
1241 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1242  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1243  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1244  * result of the command is expected to be an odp_vport also, which is decoded
1245  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1246  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1247 int
1248 dpif_linux_vport_transact(const struct dpif_linux_vport *request,
1249                           struct dpif_linux_vport *reply,
1250                           struct ofpbuf **bufp)
1251 {
1252     struct ofpbuf *request_buf;
1253     int error;
1254
1255     assert((reply != NULL) == (bufp != NULL));
1256
1257     error = dpif_linux_init();
1258     if (error) {
1259         if (reply) {
1260             *bufp = NULL;
1261             dpif_linux_vport_init(reply);
1262         }
1263         return error;
1264     }
1265
1266     request_buf = ofpbuf_new(1024);
1267     dpif_linux_vport_to_ofpbuf(request, request_buf);
1268     error = nl_sock_transact(genl_sock, request_buf, bufp);
1269     ofpbuf_delete(request_buf);
1270
1271     if (reply) {
1272         if (!error) {
1273             error = dpif_linux_vport_from_ofpbuf(reply, *bufp);
1274         }
1275         if (error) {
1276             dpif_linux_vport_init(reply);
1277             ofpbuf_delete(*bufp);
1278             *bufp = NULL;
1279         }
1280     }
1281     return error;
1282 }
1283
1284 /* Obtains information about the kernel vport named 'name' and stores it into
1285  * '*reply' and '*bufp'.  The caller must free '*bufp' when the reply is no
1286  * longer needed ('reply' will contain pointers into '*bufp').  */
1287 int
1288 dpif_linux_vport_get(const char *name, struct dpif_linux_vport *reply,
1289                      struct ofpbuf **bufp)
1290 {
1291     struct dpif_linux_vport request;
1292
1293     dpif_linux_vport_init(&request);
1294     request.cmd = ODP_VPORT_CMD_GET;
1295     request.name = name;
1296
1297     return dpif_linux_vport_transact(&request, reply, bufp);
1298 }
1299 \f
1300 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1301  * by Netlink attributes, into 'dp'.  Returns 0 if successful, otherwise a
1302  * positive errno value.
1303  *
1304  * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1305  * while 'dp' is still in use. */
1306 static int
1307 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
1308 {
1309     static const struct nl_policy odp_datapath_policy[] = {
1310         [ODP_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
1311         [ODP_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
1312                                 .min_len = sizeof(struct odp_stats),
1313                                 .max_len = sizeof(struct odp_stats),
1314                                 .optional = true },
1315         [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NL_A_U32, .optional = true },
1316         [ODP_DP_ATTR_SAMPLING] = { .type = NL_A_U32, .optional = true },
1317         [ODP_DP_ATTR_MCGROUPS] = { .type = NL_A_NESTED, .optional = true },
1318     };
1319
1320     struct nlattr *a[ARRAY_SIZE(odp_datapath_policy)];
1321     struct odp_header *odp_header;
1322     struct nlmsghdr *nlmsg;
1323     struct genlmsghdr *genl;
1324     struct ofpbuf b;
1325
1326     dpif_linux_dp_init(dp);
1327
1328     ofpbuf_use_const(&b, buf->data, buf->size);
1329     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1330     genl = ofpbuf_try_pull(&b, sizeof *genl);
1331     odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1332     if (!nlmsg || !genl || !odp_header
1333         || nlmsg->nlmsg_type != odp_datapath_family
1334         || !nl_policy_parse(&b, 0, odp_datapath_policy, a,
1335                             ARRAY_SIZE(odp_datapath_policy))) {
1336         return EINVAL;
1337     }
1338
1339     dp->cmd = genl->cmd;
1340     dp->dp_ifindex = odp_header->dp_ifindex;
1341     dp->name = nl_attr_get_string(a[ODP_DP_ATTR_NAME]);
1342     if (a[ODP_DP_ATTR_STATS]) {
1343         /* Can't use structure assignment because Netlink doesn't ensure
1344          * sufficient alignment for 64-bit members. */
1345         memcpy(&dp->stats, nl_attr_get(a[ODP_DP_ATTR_STATS]),
1346                sizeof dp->stats);
1347     }
1348     if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1349         dp->ipv4_frags = nl_attr_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1350     }
1351     if (a[ODP_DP_ATTR_SAMPLING]) {
1352         dp->sampling = nl_attr_get(a[ODP_DP_ATTR_SAMPLING]);
1353     }
1354
1355     if (a[ODP_DP_ATTR_MCGROUPS]) {
1356         static const struct nl_policy odp_mcgroup_policy[] = {
1357             [ODP_PACKET_CMD_MISS] = { .type = NL_A_U32, .optional = true },
1358             [ODP_PACKET_CMD_ACTION] = { .type = NL_A_U32, .optional = true },
1359             [ODP_PACKET_CMD_SAMPLE] = { .type = NL_A_U32, .optional = true },
1360         };
1361
1362         struct nlattr *mcgroups[ARRAY_SIZE(odp_mcgroup_policy)];
1363
1364         if (!nl_parse_nested(a[ODP_DP_ATTR_MCGROUPS], odp_mcgroup_policy,
1365                              mcgroups, ARRAY_SIZE(odp_mcgroup_policy))) {
1366             return EINVAL;
1367         }
1368
1369         if (mcgroups[ODP_PACKET_CMD_MISS]) {
1370             dp->mcgroups[DPIF_UC_MISS]
1371                 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_MISS]);
1372         }
1373         if (mcgroups[ODP_PACKET_CMD_ACTION]) {
1374             dp->mcgroups[DPIF_UC_ACTION]
1375                 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_ACTION]);
1376         }
1377         if (mcgroups[ODP_PACKET_CMD_SAMPLE]) {
1378             dp->mcgroups[DPIF_UC_SAMPLE]
1379                 = nl_attr_get_u32(mcgroups[ODP_PACKET_CMD_SAMPLE]);
1380         }
1381     }
1382
1383     return 0;
1384 }
1385
1386 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1387 static void
1388 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp *dp, struct ofpbuf *buf)
1389 {
1390     struct odp_header *odp_header;
1391
1392     nl_msg_put_genlmsghdr(buf, 0, odp_datapath_family,
1393                           NLM_F_REQUEST | NLM_F_ECHO, dp->cmd, 1);
1394
1395     odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1396     odp_header->dp_ifindex = dp->dp_ifindex;
1397
1398     if (dp->name) {
1399         nl_msg_put_string(buf, ODP_DP_ATTR_NAME, dp->name);
1400     }
1401
1402     /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1403
1404     if (dp->ipv4_frags) {
1405         nl_msg_put_u32(buf, ODP_DP_ATTR_IPV4_FRAGS, dp->ipv4_frags);
1406     }
1407
1408     if (dp->sampling) {
1409         nl_msg_put_u32(buf, ODP_DP_ATTR_SAMPLING, *dp->sampling);
1410     }
1411 }
1412
1413 /* Clears 'dp' to "empty" values. */
1414 void
1415 dpif_linux_dp_init(struct dpif_linux_dp *dp)
1416 {
1417     memset(dp, 0, sizeof *dp);
1418 }
1419
1420 static void
1421 dpif_linux_dp_dump_start(struct nl_dump *dump)
1422 {
1423     struct dpif_linux_dp request;
1424     struct ofpbuf *buf;
1425
1426     dpif_linux_dp_init(&request);
1427     request.cmd = ODP_DP_CMD_GET;
1428
1429     buf = ofpbuf_new(1024);
1430     dpif_linux_dp_to_ofpbuf(&request, buf);
1431     nl_dump_start(dump, genl_sock, buf);
1432     ofpbuf_delete(buf);
1433 }
1434
1435 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1436  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1437  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1438  * result of the command is expected to be of the same form, which is decoded
1439  * and stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the
1440  * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1441 int
1442 dpif_linux_dp_transact(const struct dpif_linux_dp *request,
1443                        struct dpif_linux_dp *reply, struct ofpbuf **bufp)
1444 {
1445     struct ofpbuf *request_buf;
1446     int error;
1447
1448     assert((reply != NULL) == (bufp != NULL));
1449
1450     request_buf = ofpbuf_new(1024);
1451     dpif_linux_dp_to_ofpbuf(request, request_buf);
1452     error = nl_sock_transact(genl_sock, request_buf, bufp);
1453     ofpbuf_delete(request_buf);
1454
1455     if (reply) {
1456         if (!error) {
1457             error = dpif_linux_dp_from_ofpbuf(reply, *bufp);
1458         }
1459         if (error) {
1460             dpif_linux_dp_init(reply);
1461             ofpbuf_delete(*bufp);
1462             *bufp = NULL;
1463         }
1464     }
1465     return error;
1466 }
1467
1468 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1469  * The caller must free '*bufp' when the reply is no longer needed ('reply'
1470  * will contain pointers into '*bufp').  */
1471 int
1472 dpif_linux_dp_get(const struct dpif *dpif_, struct dpif_linux_dp *reply,
1473                   struct ofpbuf **bufp)
1474 {
1475     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
1476     struct dpif_linux_dp request;
1477
1478     dpif_linux_dp_init(&request);
1479     request.cmd = ODP_DP_CMD_GET;
1480     request.dp_ifindex = dpif->dp_ifindex;
1481
1482     return dpif_linux_dp_transact(&request, reply, bufp);
1483 }
1484 \f
1485 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1486  * by Netlink attributes, into 'flow'.  Returns 0 if successful, otherwise a
1487  * positive errno value.
1488  *
1489  * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1490  * while 'flow' is still in use. */
1491 static int
1492 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
1493                             const struct ofpbuf *buf)
1494 {
1495     static const struct nl_policy odp_flow_policy[] = {
1496         [ODP_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
1497         [ODP_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
1498         [ODP_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
1499                                   .min_len = sizeof(struct odp_flow_stats),
1500                                   .max_len = sizeof(struct odp_flow_stats),
1501                                   .optional = true },
1502         [ODP_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
1503         [ODP_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },
1504         /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1505     };
1506
1507     struct nlattr *a[ARRAY_SIZE(odp_flow_policy)];
1508     struct odp_header *odp_header;
1509     struct nlmsghdr *nlmsg;
1510     struct genlmsghdr *genl;
1511     struct ofpbuf b;
1512
1513     dpif_linux_flow_init(flow);
1514
1515     ofpbuf_use_const(&b, buf->data, buf->size);
1516     nlmsg = ofpbuf_try_pull(&b, sizeof *nlmsg);
1517     genl = ofpbuf_try_pull(&b, sizeof *genl);
1518     odp_header = ofpbuf_try_pull(&b, sizeof *odp_header);
1519     if (!nlmsg || !genl || !odp_header
1520         || nlmsg->nlmsg_type != odp_flow_family
1521         || !nl_policy_parse(&b, 0, odp_flow_policy, a,
1522                             ARRAY_SIZE(odp_flow_policy))) {
1523         return EINVAL;
1524     }
1525
1526     flow->nlmsg_flags = nlmsg->nlmsg_flags;
1527     flow->dp_ifindex = odp_header->dp_ifindex;
1528     flow->key = nl_attr_get(a[ODP_FLOW_ATTR_KEY]);
1529     flow->key_len = nl_attr_get_size(a[ODP_FLOW_ATTR_KEY]);
1530     if (a[ODP_FLOW_ATTR_ACTIONS]) {
1531         flow->actions = nl_attr_get(a[ODP_FLOW_ATTR_ACTIONS]);
1532         flow->actions_len = nl_attr_get_size(a[ODP_FLOW_ATTR_ACTIONS]);
1533     }
1534     if (a[ODP_FLOW_ATTR_STATS]) {
1535         flow->stats = nl_attr_get(a[ODP_FLOW_ATTR_STATS]);
1536     }
1537     if (a[ODP_FLOW_ATTR_TCP_FLAGS]) {
1538         flow->tcp_flags = nl_attr_get(a[ODP_FLOW_ATTR_TCP_FLAGS]);
1539     }
1540     if (a[ODP_FLOW_ATTR_USED]) {
1541         flow->used = nl_attr_get(a[ODP_FLOW_ATTR_USED]);
1542     }
1543     return 0;
1544 }
1545
1546 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1547  * followed by Netlink attributes corresponding to 'flow'. */
1548 static void
1549 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow *flow,
1550                           struct ofpbuf *buf)
1551 {
1552     struct odp_header *odp_header;
1553
1554     nl_msg_put_genlmsghdr(buf, 0, odp_flow_family,
1555                           NLM_F_REQUEST | NLM_F_ECHO | flow->nlmsg_flags,
1556                           flow->cmd, 1);
1557
1558     odp_header = ofpbuf_put_uninit(buf, sizeof *odp_header);
1559     odp_header->dp_ifindex = flow->dp_ifindex;
1560
1561     if (flow->key_len) {
1562         nl_msg_put_unspec(buf, ODP_FLOW_ATTR_KEY, flow->key, flow->key_len);
1563     }
1564
1565     if (flow->actions || flow->actions_len) {
1566         nl_msg_put_unspec(buf, ODP_FLOW_ATTR_ACTIONS,
1567                           flow->actions, flow->actions_len);
1568     }
1569
1570     /* We never need to send these to the kernel. */
1571     assert(!flow->stats);
1572     assert(!flow->tcp_flags);
1573     assert(!flow->used);
1574
1575     if (flow->clear) {
1576         nl_msg_put_flag(buf, ODP_FLOW_ATTR_CLEAR);
1577     }
1578 }
1579
1580 /* Clears 'flow' to "empty" values. */
1581 void
1582 dpif_linux_flow_init(struct dpif_linux_flow *flow)
1583 {
1584     memset(flow, 0, sizeof *flow);
1585 }
1586
1587 /* Executes 'request' in the kernel datapath.  If the command fails, returns a
1588  * positive errno value.  Otherwise, if 'reply' and 'bufp' are null, returns 0
1589  * without doing anything else.  If 'reply' and 'bufp' are nonnull, then the
1590  * result of the command is expected to be a flow also, which is decoded and
1591  * stored in '*reply' and '*bufp'.  The caller must free '*bufp' when the reply
1592  * is no longer needed ('reply' will contain pointers into '*bufp'). */
1593 int
1594 dpif_linux_flow_transact(const struct dpif_linux_flow *request,
1595                          struct dpif_linux_flow *reply, struct ofpbuf **bufp)
1596 {
1597     struct ofpbuf *request_buf;
1598     int error;
1599
1600     assert((reply != NULL) == (bufp != NULL));
1601
1602     request_buf = ofpbuf_new(1024);
1603     dpif_linux_flow_to_ofpbuf(request, request_buf);
1604     error = nl_sock_transact(genl_sock, request_buf, bufp);
1605     ofpbuf_delete(request_buf);
1606
1607     if (reply) {
1608         if (!error) {
1609             error = dpif_linux_flow_from_ofpbuf(reply, *bufp);
1610         }
1611         if (error) {
1612             dpif_linux_flow_init(reply);
1613             ofpbuf_delete(*bufp);
1614             *bufp = NULL;
1615         }
1616     }
1617     return error;
1618 }
1619
1620 static void
1621 dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
1622                           struct dpif_flow_stats *stats)
1623 {
1624     if (flow->stats) {
1625         stats->n_packets = get_unaligned_u64(&flow->stats->n_packets);
1626         stats->n_bytes = get_unaligned_u64(&flow->stats->n_bytes);
1627     } else {
1628         stats->n_packets = 0;
1629         stats->n_bytes = 0;
1630     }
1631     stats->used = flow->used ? get_unaligned_u64(flow->used) : 0;
1632     stats->tcp_flags = flow->tcp_flags ? *flow->tcp_flags : 0;
1633 }
1634