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
, bool *force_init
)
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
;
53 snprintf(path
, sizeof(path
), CLASSIFY_PIN_PATH
"_" "%s", suffix
);
55 if (stat(path
, &st
) == 0)
61 obj
= bpf_object__open_file(CLASSIFY_PROG_PATH
, &opts
);
62 err
= libbpf_get_error(obj
);
64 perror("bpf_object__open_file");
68 prog
= bpf_object__find_program_by_title(obj
, "classifier");
70 fprintf(stderr
, "Can't find classifier prog\n");
74 bpf_program__set_type(prog
, BPF_PROG_TYPE_SCHED_CLS
);
76 qosify_fill_rodata(obj
, flags
);
78 err
= bpf_object__load(obj
);
80 perror("bpf_object__load");
84 libbpf_set_print(NULL
);
87 err
= bpf_program__pin(prog
, path
);
89 fprintf(stderr
, "Failed to pin program to %s: %s\n",
90 path
, strerror(-err
));
93 bpf_object__close(obj
);
98 int qosify_loader_init(bool force_init
)
100 static const struct {
105 { "egress_ip", QOSIFY_IP_ONLY
},
106 { "ingress_eth", QOSIFY_INGRESS
},
107 { "ingress_ip", QOSIFY_INGRESS
| QOSIFY_IP_ONLY
},
113 glob(CLASSIFY_DATA_PATH
"/*", 0, NULL
, &g
) == 0) {
114 for (i
= 0; i
< g
.gl_pathc
; i
++)
115 unlink(g
.gl_pathv
[i
]);
119 libbpf_set_print(qosify_bpf_pr
);
123 for (i
= 0; i
< ARRAY_SIZE(progs
); i
++) {
124 if (qosify_create_program(progs
[i
].suffix
, progs
[i
].flags
,