Skip to content

Commit 47dceb8

Browse files
wdebruijdavem330
authored andcommitted
packet: add classic BPF fanout mode
Add fanout mode PACKET_FANOUT_CBPF that accepts a classic BPF program to select a socket. This avoids having to keep adding special case fanout modes. One example use case is application layer load balancing. The QUIC protocol, for instance, encodes a connection ID in UDP payload. Also add socket option SOL_PACKET/PACKET_FANOUT_DATA that updates data associated with the socket group. Fanout mode PACKET_FANOUT_CBPF is the only user so far. Signed-off-by: Willem de Bruijn <willemb@google.com> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a1c234f commit 47dceb8

3 files changed

Lines changed: 104 additions & 2 deletions

File tree

‎include/uapi/linux/if_packet.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ struct sockaddr_ll {
5555
#define PACKET_TX_HAS_OFF 19
5656
#define PACKET_QDISC_BYPASS 20
5757
#define PACKET_ROLLOVER_STATS 21
58+
#define PACKET_FANOUT_DATA 22
5859

5960
#define PACKET_FANOUT_HASH 0
6061
#define PACKET_FANOUT_LB 1
6162
#define PACKET_FANOUT_CPU 2
6263
#define PACKET_FANOUT_ROLLOVER 3
6364
#define PACKET_FANOUT_RND 4
6465
#define PACKET_FANOUT_QM 5
66+
#define PACKET_FANOUT_CBPF 6
6567
#define PACKET_FANOUT_FLAG_ROLLOVER 0x1000
6668
#define PACKET_FANOUT_FLAG_DEFRAG 0x8000
6769

‎net/packet/af_packet.c‎

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#ifdef CONFIG_INET
9393
#include <net/inet_common.h>
9494
#endif
95+
#include <linux/bpf.h>
9596

9697
#include "internal.h"
9798

@@ -1410,6 +1411,22 @@ static unsigned int fanout_demux_qm(struct packet_fanout *f,
14101411
return skb_get_queue_mapping(skb) % num;
14111412
}
14121413

1414+
static unsigned int fanout_demux_bpf(struct packet_fanout *f,
1415+
struct sk_buff *skb,
1416+
unsigned int num)
1417+
{
1418+
struct bpf_prog *prog;
1419+
unsigned int ret = 0;
1420+
1421+
rcu_read_lock();
1422+
prog = rcu_dereference(f->bpf_prog);
1423+
if (prog)
1424+
ret = BPF_PROG_RUN(prog, skb) % num;
1425+
rcu_read_unlock();
1426+
1427+
return ret;
1428+
}
1429+
14131430
static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
14141431
{
14151432
return f->flags & (flag >> 8);
@@ -1454,6 +1471,9 @@ static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
14541471
case PACKET_FANOUT_ROLLOVER:
14551472
idx = fanout_demux_rollover(f, skb, 0, false, num);
14561473
break;
1474+
case PACKET_FANOUT_CBPF:
1475+
idx = fanout_demux_bpf(f, skb, num);
1476+
break;
14571477
}
14581478

14591479
if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER))
@@ -1502,6 +1522,74 @@ static bool match_fanout_group(struct packet_type *ptype, struct sock *sk)
15021522
return false;
15031523
}
15041524

1525+
static void fanout_init_data(struct packet_fanout *f)
1526+
{
1527+
switch (f->type) {
1528+
case PACKET_FANOUT_LB:
1529+
atomic_set(&f->rr_cur, 0);
1530+
break;
1531+
case PACKET_FANOUT_CBPF:
1532+
RCU_INIT_POINTER(f->bpf_prog, NULL);
1533+
break;
1534+
}
1535+
}
1536+
1537+
static void __fanout_set_data_bpf(struct packet_fanout *f, struct bpf_prog *new)
1538+
{
1539+
struct bpf_prog *old;
1540+
1541+
spin_lock(&f->lock);
1542+
old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock));
1543+
rcu_assign_pointer(f->bpf_prog, new);
1544+
spin_unlock(&f->lock);
1545+
1546+
if (old) {
1547+
synchronize_net();
1548+
bpf_prog_destroy(old);
1549+
}
1550+
}
1551+
1552+
static int fanout_set_data_cbpf(struct packet_sock *po, char __user *data,
1553+
unsigned int len)
1554+
{
1555+
struct bpf_prog *new;
1556+
struct sock_fprog fprog;
1557+
int ret;
1558+
1559+
if (sock_flag(&po->sk, SOCK_FILTER_LOCKED))
1560+
return -EPERM;
1561+
if (len != sizeof(fprog))
1562+
return -EINVAL;
1563+
if (copy_from_user(&fprog, data, len))
1564+
return -EFAULT;
1565+
1566+
ret = bpf_prog_create_from_user(&new, &fprog, NULL);
1567+
if (ret)
1568+
return ret;
1569+
1570+
__fanout_set_data_bpf(po->fanout, new);
1571+
return 0;
1572+
}
1573+
1574+
static int fanout_set_data(struct packet_sock *po, char __user *data,
1575+
unsigned int len)
1576+
{
1577+
switch (po->fanout->type) {
1578+
case PACKET_FANOUT_CBPF:
1579+
return fanout_set_data_cbpf(po, data, len);
1580+
default:
1581+
return -EINVAL;
1582+
};
1583+
}
1584+
1585+
static void fanout_release_data(struct packet_fanout *f)
1586+
{
1587+
switch (f->type) {
1588+
case PACKET_FANOUT_CBPF:
1589+
__fanout_set_data_bpf(f, NULL);
1590+
};
1591+
}
1592+
15051593
static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
15061594
{
15071595
struct packet_sock *po = pkt_sk(sk);
@@ -1519,6 +1607,7 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
15191607
case PACKET_FANOUT_CPU:
15201608
case PACKET_FANOUT_RND:
15211609
case PACKET_FANOUT_QM:
1610+
case PACKET_FANOUT_CBPF:
15221611
break;
15231612
default:
15241613
return -EINVAL;
@@ -1561,10 +1650,10 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
15611650
match->id = id;
15621651
match->type = type;
15631652
match->flags = flags;
1564-
atomic_set(&match->rr_cur, 0);
15651653
INIT_LIST_HEAD(&match->list);
15661654
spin_lock_init(&match->lock);
15671655
atomic_set(&match->sk_ref, 0);
1656+
fanout_init_data(match);
15681657
match->prot_hook.type = po->prot_hook.type;
15691658
match->prot_hook.dev = po->prot_hook.dev;
15701659
match->prot_hook.func = packet_rcv_fanout;
@@ -1610,6 +1699,7 @@ static void fanout_release(struct sock *sk)
16101699
if (atomic_dec_and_test(&f->sk_ref)) {
16111700
list_del(&f->list);
16121701
dev_remove_pack(&f->prot_hook);
1702+
fanout_release_data(f);
16131703
kfree(f);
16141704
}
16151705
mutex_unlock(&fanout_mutex);
@@ -3529,6 +3619,13 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv
35293619

35303620
return fanout_add(sk, val & 0xffff, val >> 16);
35313621
}
3622+
case PACKET_FANOUT_DATA:
3623+
{
3624+
if (!po->fanout)
3625+
return -EINVAL;
3626+
3627+
return fanout_set_data(po, optval, optlen);
3628+
}
35323629
case PACKET_TX_HAS_OFF:
35333630
{
35343631
unsigned int val;

‎net/packet/internal.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ struct packet_fanout {
7979
u16 id;
8080
u8 type;
8181
u8 flags;
82-
atomic_t rr_cur;
82+
union {
83+
atomic_t rr_cur;
84+
struct bpf_prog __rcu *bpf_prog;
85+
};
8386
struct list_head list;
8487
struct sock *arr[PACKET_FANOUT_MAX];
8588
spinlock_t lock;

0 commit comments

Comments
 (0)