1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
5 #include <sys/resource.h>
13 static int qosify_bpf_pr(enum libbpf_print_level level
, const char *format
,
16 return vfprintf(stderr
, format
, args
);
19 static void qosify_init_env(void)
21 struct rlimit limit
= {
22 .rlim_cur
= RLIM_INFINITY
,
23 .rlim_max
= RLIM_INFINITY
,
26 setrlimit(RLIMIT_MEMLOCK
, &limit
);
29 static void qosify_fill_rodata(struct bpf_object
*obj
, uint32_t flags
)
31 struct bpf_map
*map
= NULL
;
33 while ((map
= bpf_map__next(map
, obj
)) != NULL
) {
34 if (!strstr(bpf_map__name(map
), ".rodata"))
37 bpf_map__set_initial_value(map
, &flags
, sizeof(flags
));
42 qosify_create_program(const char *suffix
, uint32_t flags
)
44 DECLARE_LIBBPF_OPTS(bpf_object_open_opts
, opts
,
45 .pin_root_path
= CLASSIFY_DATA_PATH
,
47 struct bpf_program
*prog
;
48 struct bpf_object
*obj
;
52 snprintf(path
, sizeof(path
), CLASSIFY_PIN_PATH
"_" "%s", suffix
);
54 obj
= bpf_object__open_file(CLASSIFY_PROG_PATH
, &opts
);
55 err
= libbpf_get_error(obj
);
57 perror("bpf_object__open_file");
61 prog
= bpf_object__find_program_by_title(obj
, "classifier");
63 fprintf(stderr
, "Can't find classifier prog\n");
67 bpf_program__set_type(prog
, BPF_PROG_TYPE_SCHED_CLS
);
69 qosify_fill_rodata(obj
, flags
);
71 err
= bpf_object__load(obj
);
73 perror("bpf_object__load");
77 libbpf_set_print(NULL
);
80 err
= bpf_program__pin(prog
, path
);
82 fprintf(stderr
, "Failed to pin program to %s: %s\n",
83 path
, strerror(-err
));
86 bpf_object__close(obj
);
91 int qosify_loader_init(void)
98 { "egress_ip", QOSIFY_IP_ONLY
},
99 { "ingress_eth", QOSIFY_INGRESS
},
100 { "ingress_ip", QOSIFY_INGRESS
| QOSIFY_IP_ONLY
},
105 if (glob(CLASSIFY_DATA_PATH
"/*", 0, NULL
, &g
) == 0) {
106 for (i
= 0; i
< g
.gl_pathc
; i
++)
107 unlink(g
.gl_pathv
[i
]);
111 libbpf_set_print(qosify_bpf_pr
);
115 for (i
= 0; i
< ARRAY_SIZE(progs
); i
++) {
116 if (qosify_create_program(progs
[i
].suffix
, progs
[i
].flags
))