1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2021 Felix Fietkau <nbd@nbd.name>
5 #include <sys/resource.h>
19 { "egress_ip", QOSIFY_IP_ONLY
},
20 { "ingress_eth", QOSIFY_INGRESS
},
21 { "ingress_ip", QOSIFY_INGRESS
| QOSIFY_IP_ONLY
},
24 static int qosify_bpf_pr(enum libbpf_print_level level
, const char *format
,
27 return vfprintf(stderr
, format
, args
);
30 static void qosify_init_env(void)
32 struct rlimit limit
= {
33 .rlim_cur
= RLIM_INFINITY
,
34 .rlim_max
= RLIM_INFINITY
,
37 setrlimit(RLIMIT_MEMLOCK
, &limit
);
40 static void qosify_fill_rodata(struct bpf_object
*obj
, uint32_t flags
)
42 struct bpf_map
*map
= NULL
;
44 while ((map
= bpf_object__next_map(obj
, map
)) != NULL
) {
45 if (!strstr(bpf_map__name(map
), ".rodata"))
48 bpf_map__set_initial_value(map
, &flags
, sizeof(flags
));
52 const char *qosify_get_program(uint32_t flags
, int *fd
)
56 for (i
= 0; i
< ARRAY_SIZE(bpf_progs
); i
++) {
57 if (bpf_progs
[i
].flags
!= flags
)
60 *fd
= bpf_progs
[i
].fd
;
61 return bpf_progs
[i
].suffix
;
69 qosify_create_program(int idx
)
71 DECLARE_LIBBPF_OPTS(bpf_object_open_opts
, opts
,
72 .pin_root_path
= CLASSIFY_DATA_PATH
,
74 struct bpf_program
*prog
;
75 struct bpf_object
*obj
;
79 snprintf(path
, sizeof(path
), CLASSIFY_PIN_PATH
"_" "%s", bpf_progs
[idx
].suffix
);
81 obj
= bpf_object__open_file(CLASSIFY_PROG_PATH
, &opts
);
82 err
= libbpf_get_error(obj
);
84 perror("bpf_object__open_file");
88 prog
= bpf_object__find_program_by_name(obj
, "classify");
90 fprintf(stderr
, "Can't find classifier prog\n");
94 bpf_program__set_type(prog
, BPF_PROG_TYPE_SCHED_CLS
);
96 qosify_fill_rodata(obj
, bpf_progs
[idx
].flags
);
98 err
= bpf_object__load(obj
);
100 perror("bpf_object__load");
104 libbpf_set_print(NULL
);
107 err
= bpf_program__pin(prog
, path
);
109 fprintf(stderr
, "Failed to pin program to %s: %s\n",
110 path
, strerror(-err
));
114 bpf_object__close(obj
);
116 err
= bpf_obj_get(path
);
118 fprintf(stderr
, "Failed to load pinned program %s: %s\n",
119 path
, strerror(errno
));
121 bpf_progs
[idx
].fd
= err
;
126 int qosify_loader_init(void)
131 if (glob(CLASSIFY_DATA_PATH
"/*", 0, NULL
, &g
) == 0) {
132 for (i
= 0; i
< g
.gl_pathc
; i
++)
133 unlink(g
.gl_pathv
[i
]);
136 libbpf_set_print(qosify_bpf_pr
);
140 for (i
= 0; i
< ARRAY_SIZE(bpf_progs
); i
++) {
141 if (qosify_create_program(i
))