Skip to content

Commit 3a1c395

Browse files
tannerlovegregkh
authored andcommitted
net/packet: make packet_fanout.arr size configurable up to 64K
[ Upstream commit 9c661b0 ] One use case of PACKET_FANOUT is lockless reception with one socket per CPU. 256 is a practical limit on increasingly many machines. Increase PACKET_FANOUT_MAX to 64K. Expand setsockopt PACKET_FANOUT to take an extra argument max_num_members. Also explicitly define a fanout_args struct, instead of implicitly casting to an integer. This documents the API and simplifies the control flow. If max_num_members is not specified or is set to 0, then 256 is used, same as before. Signed-off-by: Tanner Love <tannerlove@google.com> Signed-off-by: Willem de Bruijn <willemb@google.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent d10c516 commit 3a1c395

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

‎include/uapi/linux/if_packet.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef __LINUX_IF_PACKET_H
33
#define __LINUX_IF_PACKET_H
44

5+
#include <asm/byteorder.h>
56
#include <linux/types.h>
67

78
struct sockaddr_pkt {
@@ -296,6 +297,17 @@ struct packet_mreq {
296297
unsigned char mr_address[8];
297298
};
298299

300+
struct fanout_args {
301+
#if defined(__LITTLE_ENDIAN_BITFIELD)
302+
__u16 id;
303+
__u16 type_flags;
304+
#else
305+
__u16 type_flags;
306+
__u16 id;
307+
#endif
308+
__u32 max_num_members;
309+
};
310+
299311
#define PACKET_MR_MULTICAST 0
300312
#define PACKET_MR_PROMISC 1
301313
#define PACKET_MR_ALLMULTI 2

‎net/packet/af_packet.c‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,13 +1636,15 @@ static bool fanout_find_new_id(struct sock *sk, u16 *new_id)
16361636
return false;
16371637
}
16381638

1639-
static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
1639+
static int fanout_add(struct sock *sk, struct fanout_args *args)
16401640
{
16411641
struct packet_rollover *rollover = NULL;
16421642
struct packet_sock *po = pkt_sk(sk);
1643+
u16 type_flags = args->type_flags;
16431644
struct packet_fanout *f, *match;
16441645
u8 type = type_flags & 0xff;
16451646
u8 flags = type_flags >> 8;
1647+
u16 id = args->id;
16461648
int err;
16471649

16481650
switch (type) {
@@ -1700,11 +1702,21 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
17001702
}
17011703
}
17021704
err = -EINVAL;
1703-
if (match && match->flags != flags)
1704-
goto out;
1705-
if (!match) {
1705+
if (match) {
1706+
if (match->flags != flags)
1707+
goto out;
1708+
if (args->max_num_members &&
1709+
args->max_num_members != match->max_num_members)
1710+
goto out;
1711+
} else {
1712+
if (args->max_num_members > PACKET_FANOUT_MAX)
1713+
goto out;
1714+
if (!args->max_num_members)
1715+
/* legacy PACKET_FANOUT_MAX */
1716+
args->max_num_members = 256;
17061717
err = -ENOMEM;
1707-
match = kzalloc(sizeof(*match), GFP_KERNEL);
1718+
match = kvzalloc(struct_size(match, arr, args->max_num_members),
1719+
GFP_KERNEL);
17081720
if (!match)
17091721
goto out;
17101722
write_pnet(&match->net, sock_net(sk));
@@ -1720,6 +1732,7 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
17201732
match->prot_hook.func = packet_rcv_fanout;
17211733
match->prot_hook.af_packet_priv = match;
17221734
match->prot_hook.id_match = match_fanout_group;
1735+
match->max_num_members = args->max_num_members;
17231736
list_add(&match->list, &fanout_list);
17241737
}
17251738
err = -EINVAL;
@@ -1730,7 +1743,7 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
17301743
match->prot_hook.type == po->prot_hook.type &&
17311744
match->prot_hook.dev == po->prot_hook.dev) {
17321745
err = -ENOSPC;
1733-
if (refcount_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
1746+
if (refcount_read(&match->sk_ref) < match->max_num_members) {
17341747
__dev_remove_pack(&po->prot_hook);
17351748
po->fanout = match;
17361749
po->rollover = rollover;
@@ -1744,7 +1757,7 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
17441757

17451758
if (err && !refcount_read(&match->sk_ref)) {
17461759
list_del(&match->list);
1747-
kfree(match);
1760+
kvfree(match);
17481761
}
17491762

17501763
out:
@@ -3075,7 +3088,7 @@ static int packet_release(struct socket *sock)
30753088
kfree(po->rollover);
30763089
if (f) {
30773090
fanout_release_data(f);
3078-
kfree(f);
3091+
kvfree(f);
30793092
}
30803093
/*
30813094
* Now the socket is dead. No more input will appear.
@@ -3866,14 +3879,14 @@ packet_setsockopt(struct socket *sock, int level, int optname, sockptr_t optval,
38663879
}
38673880
case PACKET_FANOUT:
38683881
{
3869-
int val;
3882+
struct fanout_args args = { 0 };
38703883

3871-
if (optlen != sizeof(val))
3884+
if (optlen != sizeof(int) && optlen != sizeof(args))
38723885
return -EINVAL;
3873-
if (copy_from_sockptr(&val, optval, sizeof(val)))
3886+
if (copy_from_sockptr(&args, optval, optlen))
38743887
return -EFAULT;
38753888

3876-
return fanout_add(sk, val & 0xffff, val >> 16);
3889+
return fanout_add(sk, &args);
38773890
}
38783891
case PACKET_FANOUT_DATA:
38793892
{

‎net/packet/internal.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ struct packet_ring_buffer {
7777
};
7878

7979
extern struct mutex fanout_mutex;
80-
#define PACKET_FANOUT_MAX 256
80+
#define PACKET_FANOUT_MAX (1 << 16)
8181

8282
struct packet_fanout {
8383
possible_net_t net;
8484
unsigned int num_members;
85+
u32 max_num_members;
8586
u16 id;
8687
u8 type;
8788
u8 flags;
@@ -90,10 +91,10 @@ struct packet_fanout {
9091
struct bpf_prog __rcu *bpf_prog;
9192
};
9293
struct list_head list;
93-
struct sock *arr[PACKET_FANOUT_MAX];
9494
spinlock_t lock;
9595
refcount_t sk_ref;
9696
struct packet_type prot_hook ____cacheline_aligned_in_smp;
97+
struct sock *arr[];
9798
};
9899

99100
struct packet_rollover {

0 commit comments

Comments
 (0)