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