generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathudp_buffer_tuner.bpf.c
233 lines (211 loc) · 6.78 KB
/
udp_buffer_tuner.bpf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
/*
* Copyright (c) 2025, Oracle and/or its affiliates.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include <bpftune/bpftune.bpf.h>
#include "udp_buffer_tuner.h"
#include <bpftune/corr.h>
bool under_memory_pressure = false;
bool near_memory_pressure = false;
bool near_memory_exhaustion = false;
/* set from userspace */
int kernel_page_size;
int kernel_page_shift;
int sk_mem_quantum;
int sk_mem_quantum_shift;
unsigned long long nr_free_buffer_pages;
long rmem_max, rmem_default;
struct bpftune_sample udp_fail_rcv_sample = { };
static __always_inline bool udp_nearly_out_of_memory(struct sock *sk,
struct bpftune_event *event)
{
long limit_sk_mem_quantum[3] = { };
long allocated;
long mem[3] = { }, mem_new[3] = { };
struct proto *prot = BPFTUNE_CORE_READ(sk, sk_prot);
atomic_long_t *memory_allocated = BPFTUNE_CORE_READ(prot, memory_allocated);
long *sysctl_mem = BPFTUNE_CORE_READ(prot, sysctl_mem);
__u8 shift_left = 0, shift_right = 0;
struct net *net;
int i;
if (!sk || !prot || !memory_allocated)
return false;
net = BPFTUNE_CORE_READ(sk, sk_net.net);
if (!net)
return 0;
allocated = BPFTUNE_CORE_READ(memory_allocated, counter);
if (!allocated)
return false;
if (bpf_probe_read_kernel(mem, sizeof(mem), sysctl_mem))
return false;
if (!mem[0] || !mem[1] || !mem[2])
return false;
if (kernel_page_shift >= sk_mem_quantum_shift) {
shift_left = kernel_page_shift - sk_mem_quantum_shift;
if (shift_left >= 32)
return false;
} else {
shift_right = sk_mem_quantum_shift - kernel_page_shift;
if (shift_right >= 32)
return false;
}
for (i = 0; i < 3; i++) {
limit_sk_mem_quantum[i] = mem[i];
if (shift_left)
limit_sk_mem_quantum[i] <<= shift_left;
if (shift_right)
limit_sk_mem_quantum[i] >>= shift_right;
if (limit_sk_mem_quantum[i] <= 0)
return false;
}
if (NEARLY_FULL(allocated, limit_sk_mem_quantum[2])) {
/* approaching memory exhaustion event; dial down mem/rmem
* buffer limits to limit per-socket costs.
*/
near_memory_exhaustion = true;
near_memory_pressure = true;
if (mem[0] < nr_free_buffer_pages >> 4)
mem_new[0] = BPFTUNE_GROW_BY_DELTA(mem[0]);
if (mem[1] < nr_free_buffer_pages >> 3)
mem_new[1] = BPFTUNE_GROW_BY_DELTA(mem[1]);
mem_new[2] = min(nr_free_buffer_pages >> 2,
BPFTUNE_GROW_BY_DELTA(mem[2]));
/* if we still have room to grow mem exhaustion limit, do that,
* otherwise shrink rmem.
*/
if (mem_new[2] <= (nr_free_buffer_pages >> 2)) {
send_sk_sysctl_event(sk, UDP_MEM_EXHAUSTION,
UDP_BUFFER_UDP_MEM, mem, mem_new,
event);
return true;
}
if (!net)
return true;
mem[0] = rmem_max;
mem_new[0] = BPFTUNE_SHRINK_BY_DELTA(mem[0]);
if (mem_new[0] > UDP_BUFFER_MIN)
send_sk_sysctl_event(sk, UDP_BUFFER_DECREASE,
UDP_BUFFER_NET_CORE_RMEM_MAX,
mem, mem_new, event);
mem[0] = rmem_default;
mem_new[0] = BPFTUNE_SHRINK_BY_DELTA(mem[0]);
if (mem_new[0] > UDP_BUFFER_MIN)
send_sk_sysctl_event(sk, UDP_BUFFER_DECREASE,
UDP_BUFFER_NET_CORE_RMEM_MAX,
mem, mem_new, event);
return true;
} else if (NEARLY_FULL(allocated, limit_sk_mem_quantum[1])) {
/* approaching memory pressure event. If min/memory pressure
* are less than ~8%,~12% of memory), bump them up too.
* Mem exhaustion maxes out at 25% of memory.
*/
if (!mem[0] || !mem[1] || !mem[2])
return false;
mem_new[0] = mem[0];
mem_new[1] = mem[1];
if (mem[0] < nr_free_buffer_pages >> 4)
mem_new[0] = BPFTUNE_GROW_BY_DELTA(mem[0]);
if (mem[1] < nr_free_buffer_pages >> 3)
mem_new[1] = BPFTUNE_GROW_BY_DELTA(mem[1]);
mem_new[2] = min(nr_free_buffer_pages >> 2,
BPFTUNE_GROW_BY_DELTA(mem[2]));
send_sk_sysctl_event(sk, UDP_MEM_PRESSURE,
UDP_BUFFER_UDP_MEM, mem, mem_new,
event);
near_memory_pressure = true;
return true;
}
near_memory_exhaustion = false;
near_memory_pressure = false;
return false;
}
static __always_inline int udp_fail_rcv(int ret, struct sock *sk)
{
struct bpftune_event event = { 0 };
long rmem[3], rmem_new[3];
int rcvbuf;
int id;
if (!sk)
return 0;
switch (ret) {
case -ENOBUFS:
if (udp_nearly_out_of_memory(sk, &event))
return 0;
return 0;
case -ENOMEM:
/* even if rmem_max is set to a high value, the socket in
* question may have an sk_rcvbuf value that is lower;
* in such cases, the losses incurred should not inform
* an rmem_max increase. However if the rcvbuf size is
* approximate to rmem_default, increase that. Note that
* if these increases succeeds, the same socket cannot drive
* future increases since the relevant rmem_* value is
* now out of range of the static rcvbuf value.
*/
rcvbuf = BPFTUNE_CORE_READ(sk, sk_rcvbuf);
if (BPFTUNE_WITHIN_BITSHIFT(rcvbuf, rmem_max, 2)) {
rmem[0] = rmem_max;
rmem_new[0] = BPFTUNE_GROW_BY_DELTA(rmem_max);
id = UDP_BUFFER_NET_CORE_RMEM_MAX;
} else if (BPFTUNE_WITHIN_BITSHIFT(rcvbuf, rmem_default, 2)) {
__u8 sk_userlocks = 0;
rmem[0] = rmem_default;
rmem_new[0] = BPFTUNE_GROW_BY_DELTA(rmem_default);
id = UDP_BUFFER_NET_CORE_RMEM_DEFAULT;
/* sk_userlocks is a bitfield prior to 6.9 */
if (LINUX_KERNEL_VERSION < KERNEL_VERSION(6, 9, 0)) {
#ifndef BPFTUNE_LEGACY
#ifdef BPF_CORE_READ_BITFIELD
sk_userlocks = BPF_CORE_READ_BITFIELD(sk, sk_userlocks);
#else
sk_userlocks = 0;
#endif
#endif
} else {
sk_userlocks = BPFTUNE_CORE_READ(sk, sk_userlocks);
}
/* buffer locked; ignore since rmem_default updates
* will not help; rmem_max updates will since they
* increase the max value specifiable via setsockopt.
*/
if (sk_userlocks & SOCK_RCVBUF_LOCK)
return 0;
} else {
return 0;
}
if (rmem_new[0] >= UDP_BUFFER_MAX)
return 0;
send_sk_sysctl_event(sk, UDP_BUFFER_INCREASE, id,
rmem, rmem_new, &event);
return 0;
default:
return 0;
}
}
#ifdef BPFTUNE_LEGACY
SEC("raw_tracepoint/udp_fail_queue_rcv_skb")
#else
SEC("tp_btf/udp_fail_queue_rcv_skb")
#endif
int BPF_PROG(bpftune_udp_fail_rcv, int ret, struct sock *sk)
{
if (ret == 0)
return 0;
/* only sample subset of events to reduce overhead. */
bpftune_sample(udp_fail_rcv_sample);
return udp_fail_rcv(ret, sk);
}