2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "dpif-linux.h"
27 #include <linux/types.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
36 #include "dpif-provider.h"
38 #include "netdev-vport.h"
39 #include "netlink-socket.h"
43 #include "openvswitch/tunnel.h"
45 #include "poll-loop.h"
46 #include "rtnetlink.h"
47 #include "rtnetlink-link.h"
50 #include "unaligned.h"
54 VLOG_DEFINE_THIS_MODULE(dpif_linux
);
56 enum { LRU_MAX_PORTS
= 1024 };
57 enum { LRU_MASK
= LRU_MAX_PORTS
- 1};
58 BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS
));
60 struct dpif_linux_dp
{
61 /* Generic Netlink header. */
64 /* struct odp_header. */
68 const char *name
; /* ODP_DP_ATTR_NAME. */
69 struct odp_stats stats
; /* ODP_DP_ATTR_STATS. */
70 enum odp_frag_handling ipv4_frags
; /* ODP_DP_ATTR_IPV4_FRAGS. */
71 const uint32_t *sampling
; /* ODP_DP_ATTR_SAMPLING. */
72 uint32_t mcgroups
[DPIF_N_UC_TYPES
]; /* ODP_DP_ATTR_MCGROUPS. */
75 static void dpif_linux_dp_init(struct dpif_linux_dp
*);
76 static int dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp
*,
77 const struct ofpbuf
*);
78 static void dpif_linux_dp_dump_start(struct nl_dump
*);
79 static int dpif_linux_dp_transact(const struct dpif_linux_dp
*request
,
80 struct dpif_linux_dp
*reply
,
81 struct ofpbuf
**bufp
);
82 static int dpif_linux_dp_get(const struct dpif
*, struct dpif_linux_dp
*reply
,
83 struct ofpbuf
**bufp
);
85 struct dpif_linux_flow
{
86 /* Generic Netlink header. */
89 /* struct odp_header. */
90 unsigned int nlmsg_flags
;
95 * The 'stats' and 'used' members point to 64-bit data that might only be
96 * aligned on 32-bit boundaries, so get_unaligned_u64() should be used to
97 * access their values.
99 * If 'actions' is nonnull then ODP_FLOW_ATTR_ACTIONS will be included in
100 * the Netlink version of the command, even if actions_len is zero. */
101 const struct nlattr
*key
; /* ODP_FLOW_ATTR_KEY. */
103 const struct nlattr
*actions
; /* ODP_FLOW_ATTR_ACTIONS. */
105 const struct odp_flow_stats
*stats
; /* ODP_FLOW_ATTR_STATS. */
106 const uint8_t *tcp_flags
; /* ODP_FLOW_ATTR_TCP_FLAGS. */
107 const uint64_t *used
; /* ODP_FLOW_ATTR_USED. */
108 bool clear
; /* ODP_FLOW_ATTR_CLEAR. */
111 static void dpif_linux_flow_init(struct dpif_linux_flow
*);
112 static int dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow
*,
113 const struct ofpbuf
*);
114 static void dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow
*,
116 static int dpif_linux_flow_transact(const struct dpif_linux_flow
*request
,
117 struct dpif_linux_flow
*reply
,
118 struct ofpbuf
**bufp
);
119 static void dpif_linux_flow_get_stats(const struct dpif_linux_flow
*,
120 struct dpif_flow_stats
*);
122 /* Datapath interface for the openvswitch Linux kernel module. */
127 /* Multicast group messages. */
128 struct nl_sock
*mc_sock
;
129 uint32_t mcgroups
[DPIF_N_UC_TYPES
];
130 unsigned int listen_mask
;
132 /* Change notification. */
133 struct sset changed_ports
; /* Ports that have changed. */
134 struct rtnetlink_notifier port_notifier
;
137 /* Queue of unused ports. */
138 unsigned long *lru_bitmap
;
139 uint16_t lru_ports
[LRU_MAX_PORTS
];
144 static struct vlog_rate_limit error_rl
= VLOG_RATE_LIMIT_INIT(9999, 5);
146 /* Generic Netlink family numbers for ODP. */
147 static int odp_datapath_family
;
148 static int odp_vport_family
;
149 static int odp_flow_family
;
150 static int odp_packet_family
;
152 /* Generic Netlink socket. */
153 static struct nl_sock
*genl_sock
;
155 static int dpif_linux_init(void);
156 static int open_dpif(const struct dpif_linux_dp
*, struct dpif
**);
157 static void dpif_linux_port_changed(const struct rtnetlink_link_change
*,
160 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport
*,
162 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport
*,
163 const struct ofpbuf
*);
165 static struct dpif_linux
*
166 dpif_linux_cast(const struct dpif
*dpif
)
168 dpif_assert_class(dpif
, &dpif_linux_class
);
169 return CONTAINER_OF(dpif
, struct dpif_linux
, dpif
);
173 dpif_linux_push_port(struct dpif_linux
*dp
, uint16_t port
)
175 if (port
< LRU_MAX_PORTS
&& !bitmap_is_set(dp
->lru_bitmap
, port
)) {
176 bitmap_set1(dp
->lru_bitmap
, port
);
177 dp
->lru_ports
[dp
->lru_head
++ & LRU_MASK
] = port
;
182 dpif_linux_pop_port(struct dpif_linux
*dp
)
186 if (dp
->lru_head
== dp
->lru_tail
) {
190 port
= dp
->lru_ports
[dp
->lru_tail
++ & LRU_MASK
];
191 bitmap_set0(dp
->lru_bitmap
, port
);
196 dpif_linux_enumerate(struct sset
*all_dps
)
202 error
= dpif_linux_init();
207 dpif_linux_dp_dump_start(&dump
);
208 while (nl_dump_next(&dump
, &msg
)) {
209 struct dpif_linux_dp dp
;
211 if (!dpif_linux_dp_from_ofpbuf(&dp
, &msg
)) {
212 sset_add(all_dps
, dp
.name
);
215 return nl_dump_done(&dump
);
219 dpif_linux_open(const struct dpif_class
*class OVS_UNUSED
, const char *name
,
220 bool create
, struct dpif
**dpifp
)
222 struct dpif_linux_dp dp_request
, dp
;
226 error
= dpif_linux_init();
231 /* Create or look up datapath. */
232 dpif_linux_dp_init(&dp_request
);
233 dp_request
.cmd
= create
? ODP_DP_CMD_NEW
: ODP_DP_CMD_GET
;
234 dp_request
.name
= name
;
235 error
= dpif_linux_dp_transact(&dp_request
, &dp
, &buf
);
239 error
= open_dpif(&dp
, dpifp
);
246 open_dpif(const struct dpif_linux_dp
*dp
, struct dpif
**dpifp
)
248 struct dpif_linux
*dpif
;
252 dpif
= xmalloc(sizeof *dpif
);
253 error
= rtnetlink_link_notifier_register(&dpif
->port_notifier
,
254 dpif_linux_port_changed
, dpif
);
259 dpif_init(&dpif
->dpif
, &dpif_linux_class
, dp
->name
,
260 dp
->dp_ifindex
, dp
->dp_ifindex
);
262 dpif
->mc_sock
= NULL
;
263 for (i
= 0; i
< DPIF_N_UC_TYPES
; i
++) {
264 dpif
->mcgroups
[i
] = dp
->mcgroups
[i
];
266 dpif
->listen_mask
= 0;
267 dpif
->dp_ifindex
= dp
->dp_ifindex
;
268 sset_init(&dpif
->changed_ports
);
269 dpif
->change_error
= false;
270 *dpifp
= &dpif
->dpif
;
272 dpif
->lru_head
= dpif
->lru_tail
= 0;
273 dpif
->lru_bitmap
= bitmap_allocate(LRU_MAX_PORTS
);
274 bitmap_set1(dpif
->lru_bitmap
, ODPP_LOCAL
);
275 for (i
= 1; i
< LRU_MAX_PORTS
; i
++) {
276 dpif_linux_push_port(dpif
, i
);
286 dpif_linux_close(struct dpif
*dpif_
)
288 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
289 rtnetlink_link_notifier_unregister(&dpif
->port_notifier
);
290 sset_destroy(&dpif
->changed_ports
);
291 free(dpif
->lru_bitmap
);
296 dpif_linux_destroy(struct dpif
*dpif_
)
298 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
299 struct dpif_linux_dp dp
;
301 dpif_linux_dp_init(&dp
);
302 dp
.cmd
= ODP_DP_CMD_DEL
;
303 dp
.dp_ifindex
= dpif
->dp_ifindex
;
304 return dpif_linux_dp_transact(&dp
, NULL
, NULL
);
308 dpif_linux_get_stats(const struct dpif
*dpif_
, struct odp_stats
*stats
)
310 struct dpif_linux_dp dp
;
314 error
= dpif_linux_dp_get(dpif_
, &dp
, &buf
);
323 dpif_linux_get_drop_frags(const struct dpif
*dpif_
, bool *drop_fragsp
)
325 struct dpif_linux_dp dp
;
329 error
= dpif_linux_dp_get(dpif_
, &dp
, &buf
);
331 *drop_fragsp
= dp
.ipv4_frags
== ODP_DP_FRAG_DROP
;
338 dpif_linux_set_drop_frags(struct dpif
*dpif_
, bool drop_frags
)
340 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
341 struct dpif_linux_dp dp
;
343 dpif_linux_dp_init(&dp
);
344 dp
.cmd
= ODP_DP_CMD_SET
;
345 dp
.dp_ifindex
= dpif
->dp_ifindex
;
346 dp
.ipv4_frags
= drop_frags
? ODP_DP_FRAG_DROP
: ODP_DP_FRAG_ZERO
;
347 return dpif_linux_dp_transact(&dp
, NULL
, NULL
);
351 dpif_linux_port_add(struct dpif
*dpif_
, struct netdev
*netdev
,
354 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
355 const char *name
= netdev_get_name(netdev
);
356 const char *type
= netdev_get_type(netdev
);
357 struct dpif_linux_vport request
, reply
;
358 const struct ofpbuf
*options
;
362 dpif_linux_vport_init(&request
);
363 request
.cmd
= ODP_VPORT_CMD_NEW
;
364 request
.dp_ifindex
= dpif
->dp_ifindex
;
365 request
.type
= netdev_vport_get_vport_type(netdev
);
366 if (request
.type
== ODP_VPORT_TYPE_UNSPEC
) {
367 VLOG_WARN_RL(&error_rl
, "%s: cannot create port `%s' because it has "
368 "unsupported type `%s'",
369 dpif_name(dpif_
), name
, type
);
374 options
= netdev_vport_get_options(netdev
);
375 if (options
&& options
->size
) {
376 request
.options
= options
->data
;
377 request
.options_len
= options
->size
;
380 /* Loop until we find a port that isn't used. */
382 request
.port_no
= dpif_linux_pop_port(dpif
);
383 error
= dpif_linux_vport_transact(&request
, &reply
, &buf
);
386 *port_nop
= reply
.port_no
;
389 } while (request
.port_no
!= UINT32_MAX
390 && (error
== EBUSY
|| error
== EFBIG
));
396 dpif_linux_port_del(struct dpif
*dpif_
, uint16_t port_no
)
398 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
399 struct dpif_linux_vport vport
;
402 dpif_linux_vport_init(&vport
);
403 vport
.cmd
= ODP_VPORT_CMD_DEL
;
404 vport
.dp_ifindex
= dpif
->dp_ifindex
;
405 vport
.port_no
= port_no
;
406 error
= dpif_linux_vport_transact(&vport
, NULL
, NULL
);
409 dpif_linux_push_port(dpif
, port_no
);
415 dpif_linux_port_query__(const struct dpif
*dpif
, uint32_t port_no
,
416 const char *port_name
, struct dpif_port
*dpif_port
)
418 struct dpif_linux_vport request
;
419 struct dpif_linux_vport reply
;
423 dpif_linux_vport_init(&request
);
424 request
.cmd
= ODP_VPORT_CMD_GET
;
425 request
.dp_ifindex
= dpif_linux_cast(dpif
)->dp_ifindex
;
426 request
.port_no
= port_no
;
427 request
.name
= port_name
;
429 error
= dpif_linux_vport_transact(&request
, &reply
, &buf
);
431 dpif_port
->name
= xstrdup(reply
.name
);
432 dpif_port
->type
= xstrdup(netdev_vport_get_netdev_type(&reply
));
433 dpif_port
->port_no
= reply
.port_no
;
440 dpif_linux_port_query_by_number(const struct dpif
*dpif
, uint16_t port_no
,
441 struct dpif_port
*dpif_port
)
443 return dpif_linux_port_query__(dpif
, port_no
, NULL
, dpif_port
);
447 dpif_linux_port_query_by_name(const struct dpif
*dpif
, const char *devname
,
448 struct dpif_port
*dpif_port
)
450 return dpif_linux_port_query__(dpif
, 0, devname
, dpif_port
);
454 dpif_linux_get_max_ports(const struct dpif
*dpif OVS_UNUSED
)
456 /* If the datapath increases its range of supported ports, then it should
457 * start reporting that. */
462 dpif_linux_flow_flush(struct dpif
*dpif_
)
464 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
465 struct dpif_linux_flow flow
;
467 dpif_linux_flow_init(&flow
);
468 flow
.cmd
= ODP_FLOW_CMD_DEL
;
469 flow
.dp_ifindex
= dpif
->dp_ifindex
;
470 return dpif_linux_flow_transact(&flow
, NULL
, NULL
);
473 struct dpif_linux_port_state
{
478 dpif_linux_port_dump_start(const struct dpif
*dpif_
, void **statep
)
480 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
481 struct dpif_linux_port_state
*state
;
482 struct dpif_linux_vport request
;
485 *statep
= state
= xmalloc(sizeof *state
);
487 dpif_linux_vport_init(&request
);
488 request
.cmd
= ODP_DP_CMD_GET
;
489 request
.dp_ifindex
= dpif
->dp_ifindex
;
491 buf
= ofpbuf_new(1024);
492 dpif_linux_vport_to_ofpbuf(&request
, buf
);
493 nl_dump_start(&state
->dump
, genl_sock
, buf
);
500 dpif_linux_port_dump_next(const struct dpif
*dpif OVS_UNUSED
, void *state_
,
501 struct dpif_port
*dpif_port
)
503 struct dpif_linux_port_state
*state
= state_
;
504 struct dpif_linux_vport vport
;
508 if (!nl_dump_next(&state
->dump
, &buf
)) {
512 error
= dpif_linux_vport_from_ofpbuf(&vport
, &buf
);
517 dpif_port
->name
= (char *) vport
.name
;
518 dpif_port
->type
= (char *) netdev_vport_get_netdev_type(&vport
);
519 dpif_port
->port_no
= vport
.port_no
;
524 dpif_linux_port_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
526 struct dpif_linux_port_state
*state
= state_
;
527 int error
= nl_dump_done(&state
->dump
);
533 dpif_linux_port_poll(const struct dpif
*dpif_
, char **devnamep
)
535 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
537 if (dpif
->change_error
) {
538 dpif
->change_error
= false;
539 sset_clear(&dpif
->changed_ports
);
541 } else if (!sset_is_empty(&dpif
->changed_ports
)) {
542 *devnamep
= sset_pop(&dpif
->changed_ports
);
550 dpif_linux_port_poll_wait(const struct dpif
*dpif_
)
552 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
553 if (!sset_is_empty(&dpif
->changed_ports
) || dpif
->change_error
) {
554 poll_immediate_wake();
556 rtnetlink_link_notifier_wait();
561 dpif_linux_flow_get__(const struct dpif
*dpif_
,
562 const struct nlattr
*key
, size_t key_len
,
563 struct dpif_linux_flow
*reply
, struct ofpbuf
**bufp
)
565 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
566 struct dpif_linux_flow request
;
568 dpif_linux_flow_init(&request
);
569 request
.cmd
= ODP_FLOW_CMD_GET
;
570 request
.dp_ifindex
= dpif
->dp_ifindex
;
572 request
.key_len
= key_len
;
573 return dpif_linux_flow_transact(&request
, reply
, bufp
);
577 dpif_linux_flow_get(const struct dpif
*dpif_
,
578 const struct nlattr
*key
, size_t key_len
,
579 struct ofpbuf
**actionsp
, struct dpif_flow_stats
*stats
)
581 struct dpif_linux_flow reply
;
585 error
= dpif_linux_flow_get__(dpif_
, key
, key_len
, &reply
, &buf
);
588 dpif_linux_flow_get_stats(&reply
, stats
);
591 buf
->data
= (void *) reply
.actions
;
592 buf
->size
= reply
.actions_len
;
602 dpif_linux_flow_put(struct dpif
*dpif_
, enum dpif_flow_put_flags flags
,
603 const struct nlattr
*key
, size_t key_len
,
604 const struct nlattr
*actions
, size_t actions_len
,
605 struct dpif_flow_stats
*stats
)
607 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
608 struct dpif_linux_flow request
, reply
;
609 struct nlattr dummy_action
;
613 dpif_linux_flow_init(&request
);
614 request
.cmd
= flags
& DPIF_FP_CREATE
? ODP_FLOW_CMD_NEW
: ODP_FLOW_CMD_SET
;
615 request
.dp_ifindex
= dpif
->dp_ifindex
;
617 request
.key_len
= key_len
;
618 /* Ensure that ODP_FLOW_ATTR_ACTIONS will always be included. */
619 request
.actions
= actions
? actions
: &dummy_action
;
620 request
.actions_len
= actions_len
;
621 if (flags
& DPIF_FP_ZERO_STATS
) {
622 request
.clear
= true;
624 request
.nlmsg_flags
= flags
& DPIF_FP_MODIFY
? 0 : NLM_F_CREATE
;
625 error
= dpif_linux_flow_transact(&request
,
626 stats
? &reply
: NULL
,
627 stats
? &buf
: NULL
);
628 if (!error
&& stats
) {
629 dpif_linux_flow_get_stats(&reply
, stats
);
636 dpif_linux_flow_del(struct dpif
*dpif_
,
637 const struct nlattr
*key
, size_t key_len
,
638 struct dpif_flow_stats
*stats
)
640 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
641 struct dpif_linux_flow request
, reply
;
645 dpif_linux_flow_init(&request
);
646 request
.cmd
= ODP_FLOW_CMD_DEL
;
647 request
.dp_ifindex
= dpif
->dp_ifindex
;
649 request
.key_len
= key_len
;
650 error
= dpif_linux_flow_transact(&request
,
651 stats
? &reply
: NULL
,
652 stats
? &buf
: NULL
);
653 if (!error
&& stats
) {
654 dpif_linux_flow_get_stats(&reply
, stats
);
660 struct dpif_linux_flow_state
{
662 struct dpif_linux_flow flow
;
663 struct dpif_flow_stats stats
;
668 dpif_linux_flow_dump_start(const struct dpif
*dpif_
, void **statep
)
670 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
671 struct dpif_linux_flow_state
*state
;
672 struct dpif_linux_flow request
;
675 *statep
= state
= xmalloc(sizeof *state
);
677 dpif_linux_flow_init(&request
);
678 request
.cmd
= ODP_DP_CMD_GET
;
679 request
.dp_ifindex
= dpif
->dp_ifindex
;
681 buf
= ofpbuf_new(1024);
682 dpif_linux_flow_to_ofpbuf(&request
, buf
);
683 nl_dump_start(&state
->dump
, genl_sock
, buf
);
692 dpif_linux_flow_dump_next(const struct dpif
*dpif_ OVS_UNUSED
, void *state_
,
693 const struct nlattr
**key
, size_t *key_len
,
694 const struct nlattr
**actions
, size_t *actions_len
,
695 const struct dpif_flow_stats
**stats
)
697 struct dpif_linux_flow_state
*state
= state_
;
702 ofpbuf_delete(state
->buf
);
705 if (!nl_dump_next(&state
->dump
, &buf
)) {
709 error
= dpif_linux_flow_from_ofpbuf(&state
->flow
, &buf
);
714 if (actions
&& !state
->flow
.actions
) {
715 error
= dpif_linux_flow_get__(dpif_
, state
->flow
.key
,
717 &state
->flow
, &state
->buf
);
718 if (error
== ENOENT
) {
719 VLOG_DBG("dumped flow disappeared on get");
721 VLOG_WARN("error fetching dumped flow: %s", strerror(error
));
727 *actions
= state
->flow
.actions
;
728 *actions_len
= state
->flow
.actions_len
;
731 *key
= state
->flow
.key
;
732 *key_len
= state
->flow
.key_len
;
735 dpif_linux_flow_get_stats(&state
->flow
, &state
->stats
);
736 *stats
= &state
->stats
;
742 dpif_linux_flow_dump_done(const struct dpif
*dpif OVS_UNUSED
, void *state_
)
744 struct dpif_linux_flow_state
*state
= state_
;
745 int error
= nl_dump_done(&state
->dump
);
746 ofpbuf_delete(state
->buf
);
752 dpif_linux_execute(struct dpif
*dpif_
,
753 const struct nlattr
*actions
, size_t actions_len
,
754 const struct ofpbuf
*packet
)
756 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
757 struct odp_header
*execute
;
761 buf
= ofpbuf_new(128 + actions_len
+ packet
->size
);
763 nl_msg_put_genlmsghdr(buf
, 0, odp_packet_family
, NLM_F_REQUEST
,
764 ODP_PACKET_CMD_EXECUTE
, 1);
766 execute
= ofpbuf_put_uninit(buf
, sizeof *execute
);
767 execute
->dp_ifindex
= dpif
->dp_ifindex
;
769 nl_msg_put_unspec(buf
, ODP_PACKET_ATTR_PACKET
, packet
->data
, packet
->size
);
770 nl_msg_put_unspec(buf
, ODP_PACKET_ATTR_ACTIONS
, actions
, actions_len
);
772 error
= nl_sock_transact(genl_sock
, buf
, NULL
);
778 dpif_linux_recv_get_mask(const struct dpif
*dpif_
, int *listen_mask
)
780 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
781 *listen_mask
= dpif
->listen_mask
;
786 dpif_linux_recv_set_mask(struct dpif
*dpif_
, int listen_mask
)
788 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
792 if (listen_mask
== dpif
->listen_mask
) {
794 } else if (!listen_mask
) {
795 nl_sock_destroy(dpif
->mc_sock
);
796 dpif
->mc_sock
= NULL
;
797 dpif
->listen_mask
= 0;
799 } else if (!dpif
->mc_sock
) {
800 error
= nl_sock_create(NETLINK_GENERIC
, &dpif
->mc_sock
);
806 /* Unsubscribe from old groups. */
807 for (i
= 0; i
< DPIF_N_UC_TYPES
; i
++) {
808 if (dpif
->listen_mask
& (1u << i
)) {
809 nl_sock_leave_mcgroup(dpif
->mc_sock
, dpif
->mcgroups
[i
]);
813 /* Update listen_mask. */
814 dpif
->listen_mask
= listen_mask
;
816 /* Subscribe to new groups. */
818 for (i
= 0; i
< DPIF_N_UC_TYPES
; i
++) {
819 if (dpif
->listen_mask
& (1u << i
)) {
822 retval
= nl_sock_join_mcgroup(dpif
->mc_sock
, dpif
->mcgroups
[i
]);
832 dpif_linux_get_sflow_probability(const struct dpif
*dpif_
,
833 uint32_t *probability
)
835 struct dpif_linux_dp dp
;
839 error
= dpif_linux_dp_get(dpif_
, &dp
, &buf
);
841 *probability
= dp
.sampling
? *dp
.sampling
: 0;
848 dpif_linux_set_sflow_probability(struct dpif
*dpif_
, uint32_t probability
)
850 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
851 struct dpif_linux_dp dp
;
853 dpif_linux_dp_init(&dp
);
854 dp
.cmd
= ODP_DP_CMD_SET
;
855 dp
.dp_ifindex
= dpif
->dp_ifindex
;
856 dp
.sampling
= &probability
;
857 return dpif_linux_dp_transact(&dp
, NULL
, NULL
);
861 dpif_linux_queue_to_priority(const struct dpif
*dpif OVS_UNUSED
,
862 uint32_t queue_id
, uint32_t *priority
)
864 if (queue_id
< 0xf000) {
865 *priority
= TC_H_MAKE(1 << 16, queue_id
+ 1);
873 parse_odp_packet(struct ofpbuf
*buf
, struct dpif_upcall
*upcall
,
876 static const struct nl_policy odp_packet_policy
[] = {
877 /* Always present. */
878 [ODP_PACKET_ATTR_PACKET
] = { .type
= NL_A_UNSPEC
,
879 .min_len
= ETH_HEADER_LEN
},
880 [ODP_PACKET_ATTR_KEY
] = { .type
= NL_A_NESTED
},
882 /* ODP_PACKET_CMD_ACTION only. */
883 [ODP_PACKET_ATTR_USERDATA
] = { .type
= NL_A_U64
, .optional
= true },
885 /* ODP_PACKET_CMD_SAMPLE only. */
886 [ODP_PACKET_ATTR_SAMPLE_POOL
] = { .type
= NL_A_U32
, .optional
= true },
887 [ODP_PACKET_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
890 struct odp_header
*odp_header
;
891 struct nlattr
*a
[ARRAY_SIZE(odp_packet_policy
)];
892 struct nlmsghdr
*nlmsg
;
893 struct genlmsghdr
*genl
;
897 ofpbuf_use_const(&b
, buf
->data
, buf
->size
);
899 nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
900 genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
901 odp_header
= ofpbuf_try_pull(&b
, sizeof *odp_header
);
902 if (!nlmsg
|| !genl
|| !odp_header
903 || nlmsg
->nlmsg_type
!= odp_packet_family
904 || !nl_policy_parse(&b
, 0, odp_packet_policy
, a
,
905 ARRAY_SIZE(odp_packet_policy
))) {
909 type
= (genl
->cmd
== ODP_PACKET_CMD_MISS
? DPIF_UC_MISS
910 : genl
->cmd
== ODP_PACKET_CMD_ACTION
? DPIF_UC_ACTION
911 : genl
->cmd
== ODP_PACKET_CMD_SAMPLE
? DPIF_UC_SAMPLE
917 memset(upcall
, 0, sizeof *upcall
);
919 upcall
->packet
= buf
;
920 upcall
->packet
->data
= (void *) nl_attr_get(a
[ODP_PACKET_ATTR_PACKET
]);
921 upcall
->packet
->size
= nl_attr_get_size(a
[ODP_PACKET_ATTR_PACKET
]);
922 upcall
->key
= (void *) nl_attr_get(a
[ODP_PACKET_ATTR_KEY
]);
923 upcall
->key_len
= nl_attr_get_size(a
[ODP_PACKET_ATTR_KEY
]);
924 upcall
->userdata
= (a
[ODP_PACKET_ATTR_USERDATA
]
925 ? nl_attr_get_u64(a
[ODP_PACKET_ATTR_USERDATA
])
927 upcall
->sample_pool
= (a
[ODP_PACKET_ATTR_SAMPLE_POOL
]
928 ? nl_attr_get_u32(a
[ODP_PACKET_ATTR_SAMPLE_POOL
])
930 if (a
[ODP_PACKET_ATTR_ACTIONS
]) {
931 upcall
->actions
= (void *) nl_attr_get(a
[ODP_PACKET_ATTR_ACTIONS
]);
932 upcall
->actions_len
= nl_attr_get_size(a
[ODP_PACKET_ATTR_ACTIONS
]);
935 *dp_ifindex
= odp_header
->dp_ifindex
;
941 dpif_linux_recv(struct dpif
*dpif_
, struct dpif_upcall
*upcall
)
943 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
948 if (!dpif
->mc_sock
) {
952 for (i
= 0; i
< 50; i
++) {
955 error
= nl_sock_recv(dpif
->mc_sock
, &buf
, false);
960 error
= parse_odp_packet(buf
, upcall
, &dp_ifindex
);
962 && dp_ifindex
== dpif
->dp_ifindex
963 && dpif
->listen_mask
& (1u << upcall
->type
)) {
977 dpif_linux_recv_wait(struct dpif
*dpif_
)
979 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
981 nl_sock_wait(dpif
->mc_sock
, POLLIN
);
986 dpif_linux_recv_purge(struct dpif
*dpif_
)
988 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
991 nl_sock_drain(dpif
->mc_sock
);
995 const struct dpif_class dpif_linux_class
= {
997 dpif_linux_enumerate
,
1003 dpif_linux_get_stats
,
1004 dpif_linux_get_drop_frags
,
1005 dpif_linux_set_drop_frags
,
1006 dpif_linux_port_add
,
1007 dpif_linux_port_del
,
1008 dpif_linux_port_query_by_number
,
1009 dpif_linux_port_query_by_name
,
1010 dpif_linux_get_max_ports
,
1011 dpif_linux_port_dump_start
,
1012 dpif_linux_port_dump_next
,
1013 dpif_linux_port_dump_done
,
1014 dpif_linux_port_poll
,
1015 dpif_linux_port_poll_wait
,
1016 dpif_linux_flow_get
,
1017 dpif_linux_flow_put
,
1018 dpif_linux_flow_del
,
1019 dpif_linux_flow_flush
,
1020 dpif_linux_flow_dump_start
,
1021 dpif_linux_flow_dump_next
,
1022 dpif_linux_flow_dump_done
,
1024 dpif_linux_recv_get_mask
,
1025 dpif_linux_recv_set_mask
,
1026 dpif_linux_get_sflow_probability
,
1027 dpif_linux_set_sflow_probability
,
1028 dpif_linux_queue_to_priority
,
1030 dpif_linux_recv_wait
,
1031 dpif_linux_recv_purge
,
1035 dpif_linux_init(void)
1037 static int error
= -1;
1040 error
= nl_lookup_genl_family(ODP_DATAPATH_FAMILY
,
1041 &odp_datapath_family
);
1043 VLOG_ERR("Generic Netlink family '%s' does not exist. "
1044 "The Open vSwitch kernel module is probably not loaded.",
1045 ODP_DATAPATH_FAMILY
);
1048 error
= nl_lookup_genl_family(ODP_VPORT_FAMILY
, &odp_vport_family
);
1051 error
= nl_lookup_genl_family(ODP_FLOW_FAMILY
, &odp_flow_family
);
1054 error
= nl_lookup_genl_family(ODP_PACKET_FAMILY
,
1055 &odp_packet_family
);
1058 error
= nl_sock_create(NETLINK_GENERIC
, &genl_sock
);
1066 dpif_linux_is_internal_device(const char *name
)
1068 struct dpif_linux_vport reply
;
1072 error
= dpif_linux_vport_get(name
, &reply
, &buf
);
1075 } else if (error
!= ENODEV
&& error
!= ENOENT
) {
1076 VLOG_WARN_RL(&error_rl
, "%s: vport query failed (%s)",
1077 name
, strerror(error
));
1080 return reply
.type
== ODP_VPORT_TYPE_INTERNAL
;
1084 dpif_linux_vport_send(int dp_ifindex
, uint32_t port_no
,
1085 const void *data
, size_t size
)
1087 struct odp_header
*execute
;
1092 buf
= ofpbuf_new(128 + size
);
1094 nl_msg_put_genlmsghdr(buf
, 0, odp_packet_family
, NLM_F_REQUEST
,
1095 ODP_PACKET_CMD_EXECUTE
, 1);
1097 execute
= ofpbuf_put_uninit(buf
, sizeof *execute
);
1098 execute
->dp_ifindex
= dp_ifindex
;
1100 nl_msg_put_unspec(buf
, ODP_PACKET_ATTR_PACKET
, data
, size
);
1102 actions_ofs
= nl_msg_start_nested(buf
, ODP_PACKET_ATTR_ACTIONS
);
1103 nl_msg_put_u32(buf
, ODP_ACTION_ATTR_OUTPUT
, port_no
);
1104 nl_msg_end_nested(buf
, actions_ofs
);
1106 error
= nl_sock_transact(genl_sock
, buf
, NULL
);
1112 dpif_linux_port_changed(const struct rtnetlink_link_change
*change
,
1115 struct dpif_linux
*dpif
= dpif_
;
1118 if (change
->master_ifindex
== dpif
->dp_ifindex
1119 && (change
->nlmsg_type
== RTM_NEWLINK
1120 || change
->nlmsg_type
== RTM_DELLINK
))
1122 /* Our datapath changed, either adding a new port or deleting an
1124 sset_add(&dpif
->changed_ports
, change
->ifname
);
1127 dpif
->change_error
= true;
1131 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1132 * by Netlink attributes, into 'vport'. Returns 0 if successful, otherwise a
1133 * positive errno value.
1135 * 'vport' will contain pointers into 'buf', so the caller should not free
1136 * 'buf' while 'vport' is still in use. */
1138 dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport
*vport
,
1139 const struct ofpbuf
*buf
)
1141 static const struct nl_policy odp_vport_policy
[] = {
1142 [ODP_VPORT_ATTR_PORT_NO
] = { .type
= NL_A_U32
},
1143 [ODP_VPORT_ATTR_TYPE
] = { .type
= NL_A_U32
},
1144 [ODP_VPORT_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
1145 [ODP_VPORT_ATTR_STATS
] = { .type
= NL_A_UNSPEC
,
1146 .min_len
= sizeof(struct rtnl_link_stats64
),
1147 .max_len
= sizeof(struct rtnl_link_stats64
),
1149 [ODP_VPORT_ATTR_ADDRESS
] = { .type
= NL_A_UNSPEC
,
1150 .min_len
= ETH_ADDR_LEN
,
1151 .max_len
= ETH_ADDR_LEN
,
1153 [ODP_VPORT_ATTR_MTU
] = { .type
= NL_A_U32
, .optional
= true },
1154 [ODP_VPORT_ATTR_OPTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
1155 [ODP_VPORT_ATTR_IFINDEX
] = { .type
= NL_A_U32
, .optional
= true },
1156 [ODP_VPORT_ATTR_IFLINK
] = { .type
= NL_A_U32
, .optional
= true },
1159 struct nlattr
*a
[ARRAY_SIZE(odp_vport_policy
)];
1160 struct odp_header
*odp_header
;
1161 struct nlmsghdr
*nlmsg
;
1162 struct genlmsghdr
*genl
;
1165 dpif_linux_vport_init(vport
);
1167 ofpbuf_use_const(&b
, buf
->data
, buf
->size
);
1168 nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
1169 genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
1170 odp_header
= ofpbuf_try_pull(&b
, sizeof *odp_header
);
1171 if (!nlmsg
|| !genl
|| !odp_header
1172 || nlmsg
->nlmsg_type
!= odp_vport_family
1173 || !nl_policy_parse(&b
, 0, odp_vport_policy
, a
,
1174 ARRAY_SIZE(odp_vport_policy
))) {
1178 vport
->cmd
= genl
->cmd
;
1179 vport
->dp_ifindex
= odp_header
->dp_ifindex
;
1180 vport
->port_no
= nl_attr_get_u32(a
[ODP_VPORT_ATTR_PORT_NO
]);
1181 vport
->type
= nl_attr_get_u32(a
[ODP_VPORT_ATTR_TYPE
]);
1182 vport
->name
= nl_attr_get_string(a
[ODP_VPORT_ATTR_NAME
]);
1183 if (a
[ODP_VPORT_ATTR_STATS
]) {
1184 vport
->stats
= nl_attr_get(a
[ODP_VPORT_ATTR_STATS
]);
1186 if (a
[ODP_VPORT_ATTR_ADDRESS
]) {
1187 vport
->address
= nl_attr_get(a
[ODP_VPORT_ATTR_ADDRESS
]);
1189 if (a
[ODP_VPORT_ATTR_MTU
]) {
1190 vport
->mtu
= nl_attr_get_u32(a
[ODP_VPORT_ATTR_MTU
]);
1192 vport
->mtu
= INT_MAX
;
1194 if (a
[ODP_VPORT_ATTR_OPTIONS
]) {
1195 vport
->options
= nl_attr_get(a
[ODP_VPORT_ATTR_OPTIONS
]);
1196 vport
->options_len
= nl_attr_get_size(a
[ODP_VPORT_ATTR_OPTIONS
]);
1198 if (a
[ODP_VPORT_ATTR_IFINDEX
]) {
1199 vport
->ifindex
= nl_attr_get_u32(a
[ODP_VPORT_ATTR_IFINDEX
]);
1201 if (a
[ODP_VPORT_ATTR_IFLINK
]) {
1202 vport
->iflink
= nl_attr_get_u32(a
[ODP_VPORT_ATTR_IFLINK
]);
1207 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1208 * followed by Netlink attributes corresponding to 'vport'. */
1210 dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport
*vport
,
1213 struct odp_header
*odp_header
;
1215 nl_msg_put_genlmsghdr(buf
, 0, odp_vport_family
, NLM_F_REQUEST
| NLM_F_ECHO
,
1218 odp_header
= ofpbuf_put_uninit(buf
, sizeof *odp_header
);
1219 odp_header
->dp_ifindex
= vport
->dp_ifindex
;
1221 if (vport
->port_no
!= UINT32_MAX
) {
1222 nl_msg_put_u32(buf
, ODP_VPORT_ATTR_PORT_NO
, vport
->port_no
);
1225 if (vport
->type
!= ODP_VPORT_TYPE_UNSPEC
) {
1226 nl_msg_put_u32(buf
, ODP_VPORT_ATTR_TYPE
, vport
->type
);
1230 nl_msg_put_string(buf
, ODP_VPORT_ATTR_NAME
, vport
->name
);
1234 nl_msg_put_unspec(buf
, ODP_VPORT_ATTR_STATS
,
1235 vport
->stats
, sizeof *vport
->stats
);
1238 if (vport
->address
) {
1239 nl_msg_put_unspec(buf
, ODP_VPORT_ATTR_ADDRESS
,
1240 vport
->address
, ETH_ADDR_LEN
);
1243 if (vport
->mtu
&& vport
->mtu
!= INT_MAX
) {
1244 nl_msg_put_u32(buf
, ODP_VPORT_ATTR_MTU
, vport
->mtu
);
1247 if (vport
->options
) {
1248 nl_msg_put_nested(buf
, ODP_VPORT_ATTR_OPTIONS
,
1249 vport
->options
, vport
->options_len
);
1252 if (vport
->ifindex
) {
1253 nl_msg_put_u32(buf
, ODP_VPORT_ATTR_IFINDEX
, vport
->ifindex
);
1256 if (vport
->iflink
) {
1257 nl_msg_put_u32(buf
, ODP_VPORT_ATTR_IFLINK
, vport
->iflink
);
1261 /* Clears 'vport' to "empty" values. */
1263 dpif_linux_vport_init(struct dpif_linux_vport
*vport
)
1265 memset(vport
, 0, sizeof *vport
);
1266 vport
->port_no
= UINT32_MAX
;
1269 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1270 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1271 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1272 * result of the command is expected to be an odp_vport also, which is decoded
1273 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1274 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1276 dpif_linux_vport_transact(const struct dpif_linux_vport
*request
,
1277 struct dpif_linux_vport
*reply
,
1278 struct ofpbuf
**bufp
)
1280 struct ofpbuf
*request_buf
;
1283 assert((reply
!= NULL
) == (bufp
!= NULL
));
1285 error
= dpif_linux_init();
1289 dpif_linux_vport_init(reply
);
1294 request_buf
= ofpbuf_new(1024);
1295 dpif_linux_vport_to_ofpbuf(request
, request_buf
);
1296 error
= nl_sock_transact(genl_sock
, request_buf
, bufp
);
1297 ofpbuf_delete(request_buf
);
1301 error
= dpif_linux_vport_from_ofpbuf(reply
, *bufp
);
1304 dpif_linux_vport_init(reply
);
1305 ofpbuf_delete(*bufp
);
1312 /* Obtains information about the kernel vport named 'name' and stores it into
1313 * '*reply' and '*bufp'. The caller must free '*bufp' when the reply is no
1314 * longer needed ('reply' will contain pointers into '*bufp'). */
1316 dpif_linux_vport_get(const char *name
, struct dpif_linux_vport
*reply
,
1317 struct ofpbuf
**bufp
)
1319 struct dpif_linux_vport request
;
1321 dpif_linux_vport_init(&request
);
1322 request
.cmd
= ODP_VPORT_CMD_GET
;
1323 request
.name
= name
;
1325 return dpif_linux_vport_transact(&request
, reply
, bufp
);
1328 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1329 * by Netlink attributes, into 'dp'. Returns 0 if successful, otherwise a
1330 * positive errno value.
1332 * 'dp' will contain pointers into 'buf', so the caller should not free 'buf'
1333 * while 'dp' is still in use. */
1335 dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp
*dp
, const struct ofpbuf
*buf
)
1337 static const struct nl_policy odp_datapath_policy
[] = {
1338 [ODP_DP_ATTR_NAME
] = { .type
= NL_A_STRING
, .max_len
= IFNAMSIZ
},
1339 [ODP_DP_ATTR_STATS
] = { .type
= NL_A_UNSPEC
,
1340 .min_len
= sizeof(struct odp_stats
),
1341 .max_len
= sizeof(struct odp_stats
),
1343 [ODP_DP_ATTR_IPV4_FRAGS
] = { .type
= NL_A_U32
, .optional
= true },
1344 [ODP_DP_ATTR_SAMPLING
] = { .type
= NL_A_U32
, .optional
= true },
1345 [ODP_DP_ATTR_MCGROUPS
] = { .type
= NL_A_NESTED
, .optional
= true },
1348 struct nlattr
*a
[ARRAY_SIZE(odp_datapath_policy
)];
1349 struct odp_header
*odp_header
;
1350 struct nlmsghdr
*nlmsg
;
1351 struct genlmsghdr
*genl
;
1354 dpif_linux_dp_init(dp
);
1356 ofpbuf_use_const(&b
, buf
->data
, buf
->size
);
1357 nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
1358 genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
1359 odp_header
= ofpbuf_try_pull(&b
, sizeof *odp_header
);
1360 if (!nlmsg
|| !genl
|| !odp_header
1361 || nlmsg
->nlmsg_type
!= odp_datapath_family
1362 || !nl_policy_parse(&b
, 0, odp_datapath_policy
, a
,
1363 ARRAY_SIZE(odp_datapath_policy
))) {
1367 dp
->cmd
= genl
->cmd
;
1368 dp
->dp_ifindex
= odp_header
->dp_ifindex
;
1369 dp
->name
= nl_attr_get_string(a
[ODP_DP_ATTR_NAME
]);
1370 if (a
[ODP_DP_ATTR_STATS
]) {
1371 /* Can't use structure assignment because Netlink doesn't ensure
1372 * sufficient alignment for 64-bit members. */
1373 memcpy(&dp
->stats
, nl_attr_get(a
[ODP_DP_ATTR_STATS
]),
1376 if (a
[ODP_DP_ATTR_IPV4_FRAGS
]) {
1377 dp
->ipv4_frags
= nl_attr_get_u32(a
[ODP_DP_ATTR_IPV4_FRAGS
]);
1379 if (a
[ODP_DP_ATTR_SAMPLING
]) {
1380 dp
->sampling
= nl_attr_get(a
[ODP_DP_ATTR_SAMPLING
]);
1383 if (a
[ODP_DP_ATTR_MCGROUPS
]) {
1384 static const struct nl_policy odp_mcgroup_policy
[] = {
1385 [ODP_PACKET_CMD_MISS
] = { .type
= NL_A_U32
, .optional
= true },
1386 [ODP_PACKET_CMD_ACTION
] = { .type
= NL_A_U32
, .optional
= true },
1387 [ODP_PACKET_CMD_SAMPLE
] = { .type
= NL_A_U32
, .optional
= true },
1390 struct nlattr
*mcgroups
[ARRAY_SIZE(odp_mcgroup_policy
)];
1392 if (!nl_parse_nested(a
[ODP_DP_ATTR_MCGROUPS
], odp_mcgroup_policy
,
1393 mcgroups
, ARRAY_SIZE(odp_mcgroup_policy
))) {
1397 if (mcgroups
[ODP_PACKET_CMD_MISS
]) {
1398 dp
->mcgroups
[DPIF_UC_MISS
]
1399 = nl_attr_get_u32(mcgroups
[ODP_PACKET_CMD_MISS
]);
1401 if (mcgroups
[ODP_PACKET_CMD_ACTION
]) {
1402 dp
->mcgroups
[DPIF_UC_ACTION
]
1403 = nl_attr_get_u32(mcgroups
[ODP_PACKET_CMD_ACTION
]);
1405 if (mcgroups
[ODP_PACKET_CMD_SAMPLE
]) {
1406 dp
->mcgroups
[DPIF_UC_SAMPLE
]
1407 = nl_attr_get_u32(mcgroups
[ODP_PACKET_CMD_SAMPLE
]);
1414 /* Appends to 'buf' the Generic Netlink message described by 'dp'. */
1416 dpif_linux_dp_to_ofpbuf(const struct dpif_linux_dp
*dp
, struct ofpbuf
*buf
)
1418 struct odp_header
*odp_header
;
1420 nl_msg_put_genlmsghdr(buf
, 0, odp_datapath_family
,
1421 NLM_F_REQUEST
| NLM_F_ECHO
, dp
->cmd
, 1);
1423 odp_header
= ofpbuf_put_uninit(buf
, sizeof *odp_header
);
1424 odp_header
->dp_ifindex
= dp
->dp_ifindex
;
1427 nl_msg_put_string(buf
, ODP_DP_ATTR_NAME
, dp
->name
);
1430 /* Skip ODP_DP_ATTR_STATS since we never have a reason to serialize it. */
1432 if (dp
->ipv4_frags
) {
1433 nl_msg_put_u32(buf
, ODP_DP_ATTR_IPV4_FRAGS
, dp
->ipv4_frags
);
1437 nl_msg_put_u32(buf
, ODP_DP_ATTR_SAMPLING
, *dp
->sampling
);
1441 /* Clears 'dp' to "empty" values. */
1443 dpif_linux_dp_init(struct dpif_linux_dp
*dp
)
1445 memset(dp
, 0, sizeof *dp
);
1449 dpif_linux_dp_dump_start(struct nl_dump
*dump
)
1451 struct dpif_linux_dp request
;
1454 dpif_linux_dp_init(&request
);
1455 request
.cmd
= ODP_DP_CMD_GET
;
1457 buf
= ofpbuf_new(1024);
1458 dpif_linux_dp_to_ofpbuf(&request
, buf
);
1459 nl_dump_start(dump
, genl_sock
, buf
);
1463 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1464 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1465 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1466 * result of the command is expected to be of the same form, which is decoded
1467 * and stored in '*reply' and '*bufp'. The caller must free '*bufp' when the
1468 * reply is no longer needed ('reply' will contain pointers into '*bufp'). */
1470 dpif_linux_dp_transact(const struct dpif_linux_dp
*request
,
1471 struct dpif_linux_dp
*reply
, struct ofpbuf
**bufp
)
1473 struct ofpbuf
*request_buf
;
1476 assert((reply
!= NULL
) == (bufp
!= NULL
));
1478 request_buf
= ofpbuf_new(1024);
1479 dpif_linux_dp_to_ofpbuf(request
, request_buf
);
1480 error
= nl_sock_transact(genl_sock
, request_buf
, bufp
);
1481 ofpbuf_delete(request_buf
);
1485 error
= dpif_linux_dp_from_ofpbuf(reply
, *bufp
);
1488 dpif_linux_dp_init(reply
);
1489 ofpbuf_delete(*bufp
);
1496 /* Obtains information about 'dpif_' and stores it into '*reply' and '*bufp'.
1497 * The caller must free '*bufp' when the reply is no longer needed ('reply'
1498 * will contain pointers into '*bufp'). */
1500 dpif_linux_dp_get(const struct dpif
*dpif_
, struct dpif_linux_dp
*reply
,
1501 struct ofpbuf
**bufp
)
1503 struct dpif_linux
*dpif
= dpif_linux_cast(dpif_
);
1504 struct dpif_linux_dp request
;
1506 dpif_linux_dp_init(&request
);
1507 request
.cmd
= ODP_DP_CMD_GET
;
1508 request
.dp_ifindex
= dpif
->dp_ifindex
;
1510 return dpif_linux_dp_transact(&request
, reply
, bufp
);
1513 /* Parses the contents of 'buf', which contains a "struct odp_header" followed
1514 * by Netlink attributes, into 'flow'. Returns 0 if successful, otherwise a
1515 * positive errno value.
1517 * 'flow' will contain pointers into 'buf', so the caller should not free 'buf'
1518 * while 'flow' is still in use. */
1520 dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow
*flow
,
1521 const struct ofpbuf
*buf
)
1523 static const struct nl_policy odp_flow_policy
[] = {
1524 [ODP_FLOW_ATTR_KEY
] = { .type
= NL_A_NESTED
},
1525 [ODP_FLOW_ATTR_ACTIONS
] = { .type
= NL_A_NESTED
, .optional
= true },
1526 [ODP_FLOW_ATTR_STATS
] = { .type
= NL_A_UNSPEC
,
1527 .min_len
= sizeof(struct odp_flow_stats
),
1528 .max_len
= sizeof(struct odp_flow_stats
),
1530 [ODP_FLOW_ATTR_TCP_FLAGS
] = { .type
= NL_A_U8
, .optional
= true },
1531 [ODP_FLOW_ATTR_USED
] = { .type
= NL_A_U64
, .optional
= true },
1532 /* The kernel never uses ODP_FLOW_ATTR_CLEAR. */
1535 struct nlattr
*a
[ARRAY_SIZE(odp_flow_policy
)];
1536 struct odp_header
*odp_header
;
1537 struct nlmsghdr
*nlmsg
;
1538 struct genlmsghdr
*genl
;
1541 dpif_linux_flow_init(flow
);
1543 ofpbuf_use_const(&b
, buf
->data
, buf
->size
);
1544 nlmsg
= ofpbuf_try_pull(&b
, sizeof *nlmsg
);
1545 genl
= ofpbuf_try_pull(&b
, sizeof *genl
);
1546 odp_header
= ofpbuf_try_pull(&b
, sizeof *odp_header
);
1547 if (!nlmsg
|| !genl
|| !odp_header
1548 || nlmsg
->nlmsg_type
!= odp_flow_family
1549 || !nl_policy_parse(&b
, 0, odp_flow_policy
, a
,
1550 ARRAY_SIZE(odp_flow_policy
))) {
1554 flow
->nlmsg_flags
= nlmsg
->nlmsg_flags
;
1555 flow
->dp_ifindex
= odp_header
->dp_ifindex
;
1556 flow
->key
= nl_attr_get(a
[ODP_FLOW_ATTR_KEY
]);
1557 flow
->key_len
= nl_attr_get_size(a
[ODP_FLOW_ATTR_KEY
]);
1558 if (a
[ODP_FLOW_ATTR_ACTIONS
]) {
1559 flow
->actions
= nl_attr_get(a
[ODP_FLOW_ATTR_ACTIONS
]);
1560 flow
->actions_len
= nl_attr_get_size(a
[ODP_FLOW_ATTR_ACTIONS
]);
1562 if (a
[ODP_FLOW_ATTR_STATS
]) {
1563 flow
->stats
= nl_attr_get(a
[ODP_FLOW_ATTR_STATS
]);
1565 if (a
[ODP_FLOW_ATTR_TCP_FLAGS
]) {
1566 flow
->tcp_flags
= nl_attr_get(a
[ODP_FLOW_ATTR_TCP_FLAGS
]);
1568 if (a
[ODP_FLOW_ATTR_USED
]) {
1569 flow
->used
= nl_attr_get(a
[ODP_FLOW_ATTR_USED
]);
1574 /* Appends to 'buf' (which must initially be empty) a "struct odp_header"
1575 * followed by Netlink attributes corresponding to 'flow'. */
1577 dpif_linux_flow_to_ofpbuf(const struct dpif_linux_flow
*flow
,
1580 struct odp_header
*odp_header
;
1582 nl_msg_put_genlmsghdr(buf
, 0, odp_flow_family
,
1583 NLM_F_REQUEST
| NLM_F_ECHO
| flow
->nlmsg_flags
,
1586 odp_header
= ofpbuf_put_uninit(buf
, sizeof *odp_header
);
1587 odp_header
->dp_ifindex
= flow
->dp_ifindex
;
1589 if (flow
->key_len
) {
1590 nl_msg_put_unspec(buf
, ODP_FLOW_ATTR_KEY
, flow
->key
, flow
->key_len
);
1593 if (flow
->actions
|| flow
->actions_len
) {
1594 nl_msg_put_unspec(buf
, ODP_FLOW_ATTR_ACTIONS
,
1595 flow
->actions
, flow
->actions_len
);
1598 /* We never need to send these to the kernel. */
1599 assert(!flow
->stats
);
1600 assert(!flow
->tcp_flags
);
1601 assert(!flow
->used
);
1604 nl_msg_put_flag(buf
, ODP_FLOW_ATTR_CLEAR
);
1608 /* Clears 'flow' to "empty" values. */
1610 dpif_linux_flow_init(struct dpif_linux_flow
*flow
)
1612 memset(flow
, 0, sizeof *flow
);
1615 /* Executes 'request' in the kernel datapath. If the command fails, returns a
1616 * positive errno value. Otherwise, if 'reply' and 'bufp' are null, returns 0
1617 * without doing anything else. If 'reply' and 'bufp' are nonnull, then the
1618 * result of the command is expected to be a flow also, which is decoded and
1619 * stored in '*reply' and '*bufp'. The caller must free '*bufp' when the reply
1620 * is no longer needed ('reply' will contain pointers into '*bufp'). */
1622 dpif_linux_flow_transact(const struct dpif_linux_flow
*request
,
1623 struct dpif_linux_flow
*reply
, struct ofpbuf
**bufp
)
1625 struct ofpbuf
*request_buf
;
1628 assert((reply
!= NULL
) == (bufp
!= NULL
));
1630 request_buf
= ofpbuf_new(1024);
1631 dpif_linux_flow_to_ofpbuf(request
, request_buf
);
1632 error
= nl_sock_transact(genl_sock
, request_buf
, bufp
);
1633 ofpbuf_delete(request_buf
);
1637 error
= dpif_linux_flow_from_ofpbuf(reply
, *bufp
);
1640 dpif_linux_flow_init(reply
);
1641 ofpbuf_delete(*bufp
);
1649 dpif_linux_flow_get_stats(const struct dpif_linux_flow
*flow
,
1650 struct dpif_flow_stats
*stats
)
1653 stats
->n_packets
= get_unaligned_u64(&flow
->stats
->n_packets
);
1654 stats
->n_bytes
= get_unaligned_u64(&flow
->stats
->n_bytes
);
1656 stats
->n_packets
= 0;
1659 stats
->used
= flow
->used
? get_unaligned_u64(flow
->used
) : 0;
1660 stats
->tcp_flags
= flow
->tcp_flags
? *flow
->tcp_flags
: 0;