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