qosify: add support for keeping stats
[project/qosify.git] / loader.c
index 539aae4713dccab7f3e4c22ce80d1e28173612cc..64797f0092c27abfe3714457b8be54ff2587eb45 100644 (file)
--- a/loader.c
+++ b/loader.c
 
 #include "qosify.h"
 
+static struct {
+       const char *suffix;
+       uint32_t flags;
+       int fd;
+} bpf_progs[] = {
+       { "egress_eth",  0 },
+       { "egress_ip",   QOSIFY_IP_ONLY },
+       { "ingress_eth", QOSIFY_INGRESS },
+       { "ingress_ip",  QOSIFY_INGRESS | QOSIFY_IP_ONLY },
+};
+
 static int qosify_bpf_pr(enum libbpf_print_level level, const char *format,
                     va_list args)
 {
@@ -30,7 +41,7 @@ static void qosify_fill_rodata(struct bpf_object *obj, uint32_t flags)
 {
        struct bpf_map *map = NULL;
 
-       while ((map = bpf_map__next(map, obj)) != NULL) {
+       while ((map = bpf_object__next_map(obj, map)) != NULL) {
                if (!strstr(bpf_map__name(map), ".rodata"))
                        continue;
 
@@ -38,25 +49,34 @@ static void qosify_fill_rodata(struct bpf_object *obj, uint32_t flags)
        }
 }
 
+const char *qosify_get_program(uint32_t flags, int *fd)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(bpf_progs); i++) {
+               if (bpf_progs[i].flags != flags)
+                       continue;
+
+               *fd = bpf_progs[i].fd;
+               return bpf_progs[i].suffix;
+       }
+
+       return NULL;
+}
+
+
 static int
-qosify_create_program(const char *suffix, uint32_t flags, bool *force_init)
+qosify_create_program(int idx)
 {
        DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
                .pin_root_path = CLASSIFY_DATA_PATH,
        );
        struct bpf_program *prog;
        struct bpf_object *obj;
-       struct stat st;
        char path[256];
        int err;
 
-       snprintf(path, sizeof(path), CLASSIFY_PIN_PATH "_" "%s", suffix);
-       if (!*force_init) {
-               if (stat(path, &st) == 0)
-                       return 0;
-
-               *force_init = true;
-       }
+       snprintf(path, sizeof(path), CLASSIFY_PIN_PATH "_" "%s", bpf_progs[idx].suffix);
 
        obj = bpf_object__open_file(CLASSIFY_PROG_PATH, &opts);
        err = libbpf_get_error(obj);
@@ -65,7 +85,7 @@ qosify_create_program(const char *suffix, uint32_t flags, bool *force_init)
                return -1;
        }
 
-       prog = bpf_object__find_program_by_title(obj, "classifier");
+       prog = bpf_object__find_program_by_name(obj, "classify");
        if (!prog) {
                fprintf(stderr, "Can't find classifier prog\n");
                return -1;
@@ -73,7 +93,7 @@ qosify_create_program(const char *suffix, uint32_t flags, bool *force_init)
 
        bpf_program__set_type(prog, BPF_PROG_TYPE_SCHED_CLS);
 
-       qosify_fill_rodata(obj, flags);
+       qosify_fill_rodata(obj, bpf_progs[idx].flags);
 
        err = bpf_object__load(obj);
        if (err) {
@@ -88,41 +108,37 @@ qosify_create_program(const char *suffix, uint32_t flags, bool *force_init)
        if (err) {
                fprintf(stderr, "Failed to pin program to %s: %s\n",
                        path, strerror(-err));
+               return -1;
        }
 
        bpf_object__close(obj);
 
+       err = bpf_obj_get(path);
+       if (err < 0) {
+               fprintf(stderr, "Failed to load pinned program %s: %s\n",
+                       path, strerror(errno));
+       }
+       bpf_progs[idx].fd = err;
+
        return 0;
 }
 
-int qosify_loader_init(bool force_init)
+int qosify_loader_init(void)
 {
-       static const struct {
-               const char *suffix;
-               uint32_t flags;
-       } progs[] = {
-               { "egress_eth", 0 },
-               { "egress_ip", QOSIFY_IP_ONLY },
-               { "ingress_eth", QOSIFY_INGRESS },
-               { "ingress_ip", QOSIFY_INGRESS | QOSIFY_IP_ONLY },
-       };
        glob_t g;
        int i;
 
-       if (force_init &&
-           glob(CLASSIFY_DATA_PATH "/*", 0, NULL, &g) == 0) {
+       if (glob(CLASSIFY_DATA_PATH "/*", 0, NULL, &g) == 0) {
                for (i = 0; i < g.gl_pathc; i++)
                        unlink(g.gl_pathv[i]);
        }
 
-
        libbpf_set_print(qosify_bpf_pr);
 
        qosify_init_env();
 
-       for (i = 0; i < ARRAY_SIZE(progs); i++) {
-               if (qosify_create_program(progs[i].suffix, progs[i].flags,
-                                     &force_init))
+       for (i = 0; i < ARRAY_SIZE(bpf_progs); i++) {
+               if (qosify_create_program(i))
                        return -1;
        }