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