Skip to content

Commit 6a09815

Browse files
LorenzoBianconiAlexei Starovoitov
authored andcommitted
samples/bpf: xdp_redirect_cpu: Set MAX_CPUS according to NR_CPUS
xdp_redirect_cpu is currently failing in bpf_prog_load_xattr() allocating cpu_map map if CONFIG_NR_CPUS is less than 64 since cpu_map_alloc() requires max_entries to be less than NR_CPUS. Set cpu_map max_entries according to NR_CPUS in xdp_redirect_cpu_kern.c and get currently running cpus in xdp_redirect_cpu_user.c Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/374472755001c260158c4e4b22f193bdd3c56fb7.1589300442.git.lorenzo@kernel.org
1 parent 8f4605a commit 6a09815

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎samples/bpf/xdp_redirect_cpu_kern.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <bpf/bpf_helpers.h>
1616
#include "hash_func01.h"
1717

18-
#define MAX_CPUS 64 /* WARNING - sync with _user.c */
18+
#define MAX_CPUS NR_CPUS
1919

2020
/* Special map type that can XDP_REDIRECT frames to another CPU */
2121
struct {

‎samples/bpf/xdp_redirect_cpu_user.c‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static const char *__doc__ =
1313
#include <unistd.h>
1414
#include <locale.h>
1515
#include <sys/resource.h>
16+
#include <sys/sysinfo.h>
1617
#include <getopt.h>
1718
#include <net/if.h>
1819
#include <time.h>
@@ -24,8 +25,6 @@ static const char *__doc__ =
2425
#include <arpa/inet.h>
2526
#include <linux/if_link.h>
2627

27-
#define MAX_CPUS 64 /* WARNING - sync with _kern.c */
28-
2928
/* How many xdp_progs are defined in _kern.c */
3029
#define MAX_PROG 6
3130

@@ -40,6 +39,7 @@ static char *ifname;
4039
static __u32 prog_id;
4140

4241
static __u32 xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
42+
static int n_cpus;
4343
static int cpu_map_fd;
4444
static int rx_cnt_map_fd;
4545
static int redirect_err_cnt_map_fd;
@@ -170,7 +170,7 @@ struct stats_record {
170170
struct record redir_err;
171171
struct record kthread;
172172
struct record exception;
173-
struct record enq[MAX_CPUS];
173+
struct record enq[];
174174
};
175175

176176
static bool map_collect_percpu(int fd, __u32 key, struct record *rec)
@@ -225,10 +225,11 @@ static struct datarec *alloc_record_per_cpu(void)
225225
static struct stats_record *alloc_stats_record(void)
226226
{
227227
struct stats_record *rec;
228-
int i;
228+
int i, size;
229229

230-
rec = malloc(sizeof(*rec));
231-
memset(rec, 0, sizeof(*rec));
230+
size = sizeof(*rec) + n_cpus * sizeof(struct record);
231+
rec = malloc(size);
232+
memset(rec, 0, size);
232233
if (!rec) {
233234
fprintf(stderr, "Mem alloc error\n");
234235
exit(EXIT_FAIL_MEM);
@@ -237,7 +238,7 @@ static struct stats_record *alloc_stats_record(void)
237238
rec->redir_err.cpu = alloc_record_per_cpu();
238239
rec->kthread.cpu = alloc_record_per_cpu();
239240
rec->exception.cpu = alloc_record_per_cpu();
240-
for (i = 0; i < MAX_CPUS; i++)
241+
for (i = 0; i < n_cpus; i++)
241242
rec->enq[i].cpu = alloc_record_per_cpu();
242243

243244
return rec;
@@ -247,7 +248,7 @@ static void free_stats_record(struct stats_record *r)
247248
{
248249
int i;
249250

250-
for (i = 0; i < MAX_CPUS; i++)
251+
for (i = 0; i < n_cpus; i++)
251252
free(r->enq[i].cpu);
252253
free(r->exception.cpu);
253254
free(r->kthread.cpu);
@@ -350,7 +351,7 @@ static void stats_print(struct stats_record *stats_rec,
350351
}
351352

352353
/* cpumap enqueue stats */
353-
for (to_cpu = 0; to_cpu < MAX_CPUS; to_cpu++) {
354+
for (to_cpu = 0; to_cpu < n_cpus; to_cpu++) {
354355
char *fmt = "%-15s %3d:%-3d %'-14.0f %'-11.0f %'-10.2f %s\n";
355356
char *fm2 = "%-15s %3s:%-3d %'-14.0f %'-11.0f %'-10.2f %s\n";
356357
char *errstr = "";
@@ -475,7 +476,7 @@ static void stats_collect(struct stats_record *rec)
475476
map_collect_percpu(fd, 1, &rec->redir_err);
476477

477478
fd = cpumap_enqueue_cnt_map_fd;
478-
for (i = 0; i < MAX_CPUS; i++)
479+
for (i = 0; i < n_cpus; i++)
479480
map_collect_percpu(fd, i, &rec->enq[i]);
480481

481482
fd = cpumap_kthread_cnt_map_fd;
@@ -549,10 +550,10 @@ static int create_cpu_entry(__u32 cpu, __u32 queue_size,
549550
*/
550551
static void mark_cpus_unavailable(void)
551552
{
552-
__u32 invalid_cpu = MAX_CPUS;
553+
__u32 invalid_cpu = n_cpus;
553554
int ret, i;
554555

555-
for (i = 0; i < MAX_CPUS; i++) {
556+
for (i = 0; i < n_cpus; i++) {
556557
ret = bpf_map_update_elem(cpus_available_map_fd, &i,
557558
&invalid_cpu, 0);
558559
if (ret) {
@@ -688,6 +689,8 @@ int main(int argc, char **argv)
688689
int prog_fd;
689690
__u32 qsize;
690691

692+
n_cpus = get_nprocs_conf();
693+
691694
/* Notice: choosing he queue size is very important with the
692695
* ixgbe driver, because it's driver page recycling trick is
693696
* dependend on pages being returned quickly. The number of
@@ -757,7 +760,7 @@ int main(int argc, char **argv)
757760
case 'c':
758761
/* Add multiple CPUs */
759762
add_cpu = strtoul(optarg, NULL, 0);
760-
if (add_cpu >= MAX_CPUS) {
763+
if (add_cpu >= n_cpus) {
761764
fprintf(stderr,
762765
"--cpu nr too large for cpumap err(%d):%s\n",
763766
errno, strerror(errno));

0 commit comments

Comments
 (0)