Skip to content

Commit

Permalink
soreuseport: pass skb to secondary UDP socket lookup
Browse files Browse the repository at this point in the history
This socket-lookup path did not pass along the skb in question
in my original BPF-based socket selection patch.  The skb in the
udpN_lib_lookup2 path can be used for BPF-based socket selection just
like it is in the 'traditional' udpN_lib_lookup path.

udpN_lib_lookup2 kicks in when there are greater than 10 sockets in
the same hlist slot.  Coincidentally, I chose 10 sockets per
reuseport group in my functional test, so the lookup2 path was not
excersised. This adds an additional set of tests with 20 sockets.

Fixes: 538950a ("soreuseport: setsockopt SO_ATTACH_REUSEPORT_[CE]BPF")
Fixes: 3ca8e40 ("soreuseport: BPF selection functional test")
Suggested-by: Eric Dumazet <[email protected]>
Signed-off-by: Craig Gallek <[email protected]>
Acked-by: Eric Dumazet <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
kraigatgoog authored and davem330 committed Jan 6, 2016
1 parent f0138e2 commit 1134158
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
10 changes: 6 additions & 4 deletions net/ipv4/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ static u32 udp_ehashfn(const struct net *net, const __be32 laddr,
static struct sock *udp4_lib_lookup2(struct net *net,
__be32 saddr, __be16 sport,
__be32 daddr, unsigned int hnum, int dif,
struct udp_hslot *hslot2, unsigned int slot2)
struct udp_hslot *hslot2, unsigned int slot2,
struct sk_buff *skb)
{
struct sock *sk, *result;
struct hlist_nulls_node *node;
Expand All @@ -514,7 +515,8 @@ static struct sock *udp4_lib_lookup2(struct net *net,
struct sock *sk2;
hash = udp_ehashfn(net, daddr, hnum,
saddr, sport);
sk2 = reuseport_select_sock(sk, hash, NULL, 0);
sk2 = reuseport_select_sock(sk, hash, skb,
sizeof(struct udphdr));
if (sk2) {
result = sk2;
goto found;
Expand Down Expand Up @@ -573,7 +575,7 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,

result = udp4_lib_lookup2(net, saddr, sport,
daddr, hnum, dif,
hslot2, slot2);
hslot2, slot2, skb);
if (!result) {
hash2 = udp4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
slot2 = hash2 & udptable->mask;
Expand All @@ -583,7 +585,7 @@ struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,

result = udp4_lib_lookup2(net, saddr, sport,
htonl(INADDR_ANY), hnum, dif,
hslot2, slot2);
hslot2, slot2, skb);
}
rcu_read_unlock();
return result;
Expand Down
10 changes: 6 additions & 4 deletions net/ipv6/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ static inline int compute_score2(struct sock *sk, struct net *net,
static struct sock *udp6_lib_lookup2(struct net *net,
const struct in6_addr *saddr, __be16 sport,
const struct in6_addr *daddr, unsigned int hnum, int dif,
struct udp_hslot *hslot2, unsigned int slot2)
struct udp_hslot *hslot2, unsigned int slot2,
struct sk_buff *skb)
{
struct sock *sk, *result;
struct hlist_nulls_node *node;
Expand All @@ -272,7 +273,8 @@ static struct sock *udp6_lib_lookup2(struct net *net,
struct sock *sk2;
hash = udp6_ehashfn(net, daddr, hnum,
saddr, sport);
sk2 = reuseport_select_sock(sk, hash, NULL, 0);
sk2 = reuseport_select_sock(sk, hash, skb,
sizeof(struct udphdr));
if (sk2) {
result = sk2;
goto found;
Expand Down Expand Up @@ -331,7 +333,7 @@ struct sock *__udp6_lib_lookup(struct net *net,

result = udp6_lib_lookup2(net, saddr, sport,
daddr, hnum, dif,
hslot2, slot2);
hslot2, slot2, skb);
if (!result) {
hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
slot2 = hash2 & udptable->mask;
Expand All @@ -341,7 +343,7 @@ struct sock *__udp6_lib_lookup(struct net *net,

result = udp6_lib_lookup2(net, saddr, sport,
&in6addr_any, hnum, dif,
hslot2, slot2);
hslot2, slot2, skb);
}
rcu_read_unlock();
return result;
Expand Down
47 changes: 47 additions & 0 deletions tools/testing/selftests/net/reuseport_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ static void attach_ebpf(int fd, uint16_t mod)
if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF, &bpf_fd,
sizeof(bpf_fd)))
error(1, errno, "failed to set SO_ATTACH_REUSEPORT_EBPF");

close(bpf_fd);
}

static void attach_cbpf(int fd, uint16_t mod)
Expand Down Expand Up @@ -396,20 +398,37 @@ static void test_filter_without_bind(void)
int main(void)
{
fprintf(stderr, "---- IPv4 UDP ----\n");
/* NOTE: UDP socket lookups traverse a different code path when there
* are > 10 sockets in a group. Run the bpf test through both paths.
*/
test_reuseport_ebpf((struct test_params) {
.recv_family = AF_INET,
.send_family = AF_INET,
.protocol = SOCK_DGRAM,
.recv_socks = 10,
.recv_port = 8000,
.send_port_min = 9000});
test_reuseport_ebpf((struct test_params) {
.recv_family = AF_INET,
.send_family = AF_INET,
.protocol = SOCK_DGRAM,
.recv_socks = 20,
.recv_port = 8000,
.send_port_min = 9000});
test_reuseport_cbpf((struct test_params) {
.recv_family = AF_INET,
.send_family = AF_INET,
.protocol = SOCK_DGRAM,
.recv_socks = 10,
.recv_port = 8001,
.send_port_min = 9020});
test_reuseport_cbpf((struct test_params) {
.recv_family = AF_INET,
.send_family = AF_INET,
.protocol = SOCK_DGRAM,
.recv_socks = 20,
.recv_port = 8001,
.send_port_min = 9020});
test_extra_filter((struct test_params) {
.recv_family = AF_INET,
.protocol = SOCK_DGRAM,
Expand All @@ -427,13 +446,27 @@ int main(void)
.recv_socks = 10,
.recv_port = 8003,
.send_port_min = 9040});
test_reuseport_ebpf((struct test_params) {
.recv_family = AF_INET6,
.send_family = AF_INET6,
.protocol = SOCK_DGRAM,
.recv_socks = 20,
.recv_port = 8003,
.send_port_min = 9040});
test_reuseport_cbpf((struct test_params) {
.recv_family = AF_INET6,
.send_family = AF_INET6,
.protocol = SOCK_DGRAM,
.recv_socks = 10,
.recv_port = 8004,
.send_port_min = 9060});
test_reuseport_cbpf((struct test_params) {
.recv_family = AF_INET6,
.send_family = AF_INET6,
.protocol = SOCK_DGRAM,
.recv_socks = 20,
.recv_port = 8004,
.send_port_min = 9060});
test_extra_filter((struct test_params) {
.recv_family = AF_INET6,
.protocol = SOCK_DGRAM,
Expand All @@ -444,6 +477,13 @@ int main(void)
.recv_port = 8009});

fprintf(stderr, "---- IPv6 UDP w/ mapped IPv4 ----\n");
test_reuseport_ebpf((struct test_params) {
.recv_family = AF_INET6,
.send_family = AF_INET,
.protocol = SOCK_DGRAM,
.recv_socks = 20,
.recv_port = 8006,
.send_port_min = 9080});
test_reuseport_ebpf((struct test_params) {
.recv_family = AF_INET6,
.send_family = AF_INET,
Expand All @@ -458,6 +498,13 @@ int main(void)
.recv_socks = 10,
.recv_port = 8007,
.send_port_min = 9100});
test_reuseport_cbpf((struct test_params) {
.recv_family = AF_INET6,
.send_family = AF_INET,
.protocol = SOCK_DGRAM,
.recv_socks = 20,
.recv_port = 8007,
.send_port_min = 9100});


test_filter_without_bind();
Expand Down

0 comments on commit 1134158

Please sign in to comment.