Skip to content

Commit 77f65eb

Browse files
wdebruijdavem330
authored andcommitted
packet: packet fanout rollover during socket overload
Changes: v3->v2: rebase (no other changes) passes selftest v2->v1: read f->num_members only once fix bug: test rollover mode + flag Minimize packet drop in a fanout group. If one socket is full, roll over packets to another from the group. Maintain flow affinity during normal load using an rxhash fanout policy, while dispersing unexpected traffic storms that hit a single cpu, such as spoofed-source DoS flows. Rollover breaks affinity for flows arriving at saturated sockets during those conditions. The patch adds a fanout policy ROLLOVER that rotates between sockets, filling each socket before moving to the next. It also adds a fanout flag ROLLOVER. If passed along with any other fanout policy, the primary policy is applied until the chosen socket is full. Then, rollover selects another socket, to delay packet drop until the entire system is saturated. Probing sockets is not free. Selecting the last used socket, as rollover does, is a greedy approach that maximizes chance of success, at the cost of extreme load imbalance. In practice, with sufficiently long queues to absorb bursts, sockets are drained in parallel and load balance looks uniform in `top`. To avoid contention, scales counters with number of sockets and accesses them lockfree. Values are bounds checked to ensure correctness. Tested using an application with 9 threads pinned to CPUs, one socket per thread and sufficient busywork per packet operation to limits each thread to handling 32 Kpps. When sent 500 Kpps single UDP stream packets, a FANOUT_CPU setup processes 32 Kpps in total without this patch, 270 Kpps with the patch. Tested with read() and with a packet ring (V1). Also, passes psock_fanout.c unit test added to selftests. Signed-off-by: Willem de Bruijn <willemb@google.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b0aa73b commit 77f65eb

7 files changed

Lines changed: 451 additions & 24 deletions

File tree

‎include/uapi/linux/if_packet.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ struct sockaddr_ll {
5555
#define PACKET_FANOUT_HASH 0
5656
#define PACKET_FANOUT_LB 1
5757
#define PACKET_FANOUT_CPU 2
58+
#define PACKET_FANOUT_ROLLOVER 3
59+
#define PACKET_FANOUT_FLAG_ROLLOVER 0x1000
5860
#define PACKET_FANOUT_FLAG_DEFRAG 0x8000
5961

6062
struct tpacket_stats {

‎net/packet/af_packet.c‎

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
181181

182182
struct packet_sock;
183183
static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
184+
static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
185+
struct packet_type *pt, struct net_device *orig_dev);
184186

185187
static void *packet_previous_frame(struct packet_sock *po,
186188
struct packet_ring_buffer *rb,
@@ -973,11 +975,11 @@ static void *packet_current_rx_frame(struct packet_sock *po,
973975

974976
static void *prb_lookup_block(struct packet_sock *po,
975977
struct packet_ring_buffer *rb,
976-
unsigned int previous,
978+
unsigned int idx,
977979
int status)
978980
{
979981
struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
980-
struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, previous);
982+
struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
981983

982984
if (status != BLOCK_STATUS(pbd))
983985
return NULL;
@@ -1041,6 +1043,29 @@ static void packet_increment_head(struct packet_ring_buffer *buff)
10411043
buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
10421044
}
10431045

1046+
static bool packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1047+
{
1048+
struct sock *sk = &po->sk;
1049+
bool has_room;
1050+
1051+
if (po->prot_hook.func != tpacket_rcv)
1052+
return (atomic_read(&sk->sk_rmem_alloc) + skb->truesize)
1053+
<= sk->sk_rcvbuf;
1054+
1055+
spin_lock(&sk->sk_receive_queue.lock);
1056+
if (po->tp_version == TPACKET_V3)
1057+
has_room = prb_lookup_block(po, &po->rx_ring,
1058+
po->rx_ring.prb_bdqc.kactive_blk_num,
1059+
TP_STATUS_KERNEL);
1060+
else
1061+
has_room = packet_lookup_frame(po, &po->rx_ring,
1062+
po->rx_ring.head,
1063+
TP_STATUS_KERNEL);
1064+
spin_unlock(&sk->sk_receive_queue.lock);
1065+
1066+
return has_room;
1067+
}
1068+
10441069
static void packet_sock_destruct(struct sock *sk)
10451070
{
10461071
skb_queue_purge(&sk->sk_error_queue);
@@ -1066,31 +1091,57 @@ static int fanout_rr_next(struct packet_fanout *f, unsigned int num)
10661091
return x;
10671092
}
10681093

1069-
static struct sock *fanout_demux_hash(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
1094+
static unsigned int fanout_demux_hash(struct packet_fanout *f,
1095+
struct sk_buff *skb,
1096+
unsigned int num)
10701097
{
1071-
u32 idx, hash = skb->rxhash;
1072-
1073-
idx = ((u64)hash * num) >> 32;
1074-
1075-
return f->arr[idx];
1098+
return (((u64)skb->rxhash) * num) >> 32;
10761099
}
10771100

1078-
static struct sock *fanout_demux_lb(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
1101+
static unsigned int fanout_demux_lb(struct packet_fanout *f,
1102+
struct sk_buff *skb,
1103+
unsigned int num)
10791104
{
10801105
int cur, old;
10811106

10821107
cur = atomic_read(&f->rr_cur);
10831108
while ((old = atomic_cmpxchg(&f->rr_cur, cur,
10841109
fanout_rr_next(f, num))) != cur)
10851110
cur = old;
1086-
return f->arr[cur];
1111+
return cur;
1112+
}
1113+
1114+
static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1115+
struct sk_buff *skb,
1116+
unsigned int num)
1117+
{
1118+
return smp_processor_id() % num;
10871119
}
10881120

1089-
static struct sock *fanout_demux_cpu(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
1121+
static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1122+
struct sk_buff *skb,
1123+
unsigned int idx, unsigned int skip,
1124+
unsigned int num)
10901125
{
1091-
unsigned int cpu = smp_processor_id();
1126+
unsigned int i, j;
10921127

1093-
return f->arr[cpu % num];
1128+
i = j = min_t(int, f->next[idx], num - 1);
1129+
do {
1130+
if (i != skip && packet_rcv_has_room(pkt_sk(f->arr[i]), skb)) {
1131+
if (i != j)
1132+
f->next[idx] = i;
1133+
return i;
1134+
}
1135+
if (++i == num)
1136+
i = 0;
1137+
} while (i != j);
1138+
1139+
return idx;
1140+
}
1141+
1142+
static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1143+
{
1144+
return f->flags & (flag >> 8);
10941145
}
10951146

10961147
static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
@@ -1099,7 +1150,7 @@ static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
10991150
struct packet_fanout *f = pt->af_packet_priv;
11001151
unsigned int num = f->num_members;
11011152
struct packet_sock *po;
1102-
struct sock *sk;
1153+
unsigned int idx;
11031154

11041155
if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
11051156
!num) {
@@ -1110,23 +1161,31 @@ static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
11101161
switch (f->type) {
11111162
case PACKET_FANOUT_HASH:
11121163
default:
1113-
if (f->defrag) {
1164+
if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
11141165
skb = ip_check_defrag(skb, IP_DEFRAG_AF_PACKET);
11151166
if (!skb)
11161167
return 0;
11171168
}
11181169
skb_get_rxhash(skb);
1119-
sk = fanout_demux_hash(f, skb, num);
1170+
idx = fanout_demux_hash(f, skb, num);
11201171
break;
11211172
case PACKET_FANOUT_LB:
1122-
sk = fanout_demux_lb(f, skb, num);
1173+
idx = fanout_demux_lb(f, skb, num);
11231174
break;
11241175
case PACKET_FANOUT_CPU:
1125-
sk = fanout_demux_cpu(f, skb, num);
1176+
idx = fanout_demux_cpu(f, skb, num);
1177+
break;
1178+
case PACKET_FANOUT_ROLLOVER:
1179+
idx = fanout_demux_rollover(f, skb, 0, (unsigned int) -1, num);
11261180
break;
11271181
}
11281182

1129-
po = pkt_sk(sk);
1183+
po = pkt_sk(f->arr[idx]);
1184+
if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER) &&
1185+
unlikely(!packet_rcv_has_room(po, skb))) {
1186+
idx = fanout_demux_rollover(f, skb, idx, idx, num);
1187+
po = pkt_sk(f->arr[idx]);
1188+
}
11301189

11311190
return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
11321191
}
@@ -1175,10 +1234,13 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
11751234
struct packet_sock *po = pkt_sk(sk);
11761235
struct packet_fanout *f, *match;
11771236
u8 type = type_flags & 0xff;
1178-
u8 defrag = (type_flags & PACKET_FANOUT_FLAG_DEFRAG) ? 1 : 0;
1237+
u8 flags = type_flags >> 8;
11791238
int err;
11801239

11811240
switch (type) {
1241+
case PACKET_FANOUT_ROLLOVER:
1242+
if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1243+
return -EINVAL;
11821244
case PACKET_FANOUT_HASH:
11831245
case PACKET_FANOUT_LB:
11841246
case PACKET_FANOUT_CPU:
@@ -1203,7 +1265,7 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
12031265
}
12041266
}
12051267
err = -EINVAL;
1206-
if (match && match->defrag != defrag)
1268+
if (match && match->flags != flags)
12071269
goto out;
12081270
if (!match) {
12091271
err = -ENOMEM;
@@ -1213,7 +1275,7 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
12131275
write_pnet(&match->net, sock_net(sk));
12141276
match->id = id;
12151277
match->type = type;
1216-
match->defrag = defrag;
1278+
match->flags = flags;
12171279
atomic_set(&match->rr_cur, 0);
12181280
INIT_LIST_HEAD(&match->list);
12191281
spin_lock_init(&match->lock);
@@ -3240,7 +3302,8 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,
32403302
case PACKET_FANOUT:
32413303
val = (po->fanout ?
32423304
((u32)po->fanout->id |
3243-
((u32)po->fanout->type << 16)) :
3305+
((u32)po->fanout->type << 16) |
3306+
((u32)po->fanout->flags << 24)) :
32443307
0);
32453308
break;
32463309
case PACKET_TX_HAS_OFF:

‎net/packet/internal.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ struct packet_fanout {
7777
unsigned int num_members;
7878
u16 id;
7979
u8 type;
80-
u8 defrag;
80+
u8 flags;
8181
atomic_t rr_cur;
8282
struct list_head list;
8383
struct sock *arr[PACKET_FANOUT_MAX];
84+
int next[PACKET_FANOUT_MAX];
8485
spinlock_t lock;
8586
atomic_t sk_ref;
8687
struct packet_type prot_hook ____cacheline_aligned_in_smp;

‎tools/testing/selftests/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ TARGETS += cpu-hotplug
66
TARGETS += memory-hotplug
77
TARGETS += efivarfs
88
TARGETS += net-socket
9+
TARGETS += net-afpacket
910

1011
all:
1112
for TARGET in $(TARGETS); do \
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Makefile for net-socket selftests
2+
3+
CC = $(CROSS_COMPILE)gcc
4+
CFLAGS = -Wall
5+
6+
CFLAGS += -I../../../../usr/include/
7+
8+
AF_PACKET_PROGS = psock_fanout
9+
10+
all: $(AF_PACKET_PROGS)
11+
%: %.c
12+
$(CC) $(CFLAGS) -o $@ $^
13+
14+
run_tests: all
15+
@/bin/sh ./run_afpackettests || echo "afpackettests: [FAIL]"
16+
17+
clean:
18+
$(RM) $(AF_PACKET_PROGS)

0 commit comments

Comments
 (0)