Skip to content

Commit

Permalink
criu: implement helper functions and structures to restore BPF maps
Browse files Browse the repository at this point in the history
    Updated bpfmap.c to define:

    a) structures and structure variables needed to hold information
    about BPF map files that are being restored

        * struct bpfmap_file_info - A new structure that is used to
        store information about the BPF map file currently being
        restored

        * struct file_desc_ops bpfmap_desc_ops - A structure variable
        that specifies the function handler for opening BPF map files

        * struct collect_image_info bpfmap_cinfo - A structure variable
        that specifies the function handler for collecting BPF map
        files from checkpointed images

    b) functions

        * collect_one_bpfmap() - handler for collecting information
        about BPF map files from the checkpointed image and adding it
        to the process' list of file descriptors that must be restored

        * bpfmap_open() - creates a new BPF map file with the same
        parameters as the checkpointed image and sets it to the same
        file descriptor number used by the process before it was
        checkpointed

    This commit provides the infrastructure needed by other parts of
    CRIU to restore a process' BPF map files

Signed-off-by: Abhishek Vijeev <[email protected]>
  • Loading branch information
abhishekvijeev committed May 23, 2020
1 parent c02f595 commit 8b421fe
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
79 changes: 79 additions & 0 deletions criu/bpfmap.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <linux/bpf.h>

#include "common/compiler.h"
#include "imgset.h"
Expand All @@ -11,6 +12,11 @@
#include "protobuf.h"
#include "bpfmap-file.pb-c.h"

struct bpfmap_file_info {
BpfmapFileEntry *bpfe;
struct file_desc d;
};

int is_bpfmap_link(char *link)
{
return is_anon_link_type(link, "bpf-map");
Expand Down Expand Up @@ -45,4 +51,77 @@ static int dump_one_bpfmap(int lfd, u32 id, const struct fd_parms *p)
const struct fdtype_ops bpfmap_dump_ops = {
.type = FD_TYPES__BPFMAP,
.dump = dump_one_bpfmap,
};

static inline int sys_bpf(enum bpf_cmd cmd, union bpf_attr *attr,
unsigned int size)
{
return syscall(__NR_bpf, cmd, attr, size);
}

static int bpf_create_map(enum bpf_map_type map_type,
unsigned int key_size,
unsigned int value_size,
unsigned int max_entries)
{
union bpf_attr attr = {
.map_type = map_type,
.key_size = key_size,
.value_size = value_size,
.max_entries = max_entries
};

return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
}

static int bpfmap_open(struct file_desc *d, int *new_fd)
{
struct bpfmap_file_info *info;
BpfmapFileEntry *bpfe;
int tmp;

info = container_of(d, struct bpfmap_file_info, d);
bpfe = info->bpfe;

tmp = bpf_create_map(bpfe->map_type, bpfe->key_size,
bpfe->value_size, bpfe->max_entries);
if (tmp < 0) {
pr_perror("Can't create bpfmap %#08x",
bpfe->id);
return -1;
}

if (rst_file_params(tmp, bpfe->fown, bpfe->flags)) {
pr_perror("Can't restore params on bpfmap %#08x",
bpfe->id);
goto err_close;
}

*new_fd = tmp;
return 0;

err_close:
close(tmp);
return -1;
}

static struct file_desc_ops bpfmap_desc_ops = {
.type = FD_TYPES__BPFMAP,
.open = bpfmap_open,
};

static int collect_one_bpfmap(void *obj, ProtobufCMessage *msg, struct cr_img *i)
{
struct bpfmap_file_info *info = obj;

info->bpfe = pb_msg(msg, BpfmapFileEntry);
pr_info_bpfmap("Collected ", info->bpfe);
return file_desc_add(&info->d, info->bpfe->id, &bpfmap_desc_ops);
}

struct collect_image_info bpfmap_cinfo = {
.fd_type = CR_FD_BPFMAP_FILE,
.pb_type = PB_BPFMAP_FILE,
.priv_size = sizeof(struct bpfmap_file_info),
.collect = collect_one_bpfmap,
};
1 change: 1 addition & 0 deletions criu/include/bpfmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

extern int is_bpfmap_link(char *link);
extern const struct fdtype_ops bpfmap_dump_ops;
extern struct collect_image_info bpfmap_cinfo;

#endif /* __CR_BPFMAP_H__ */

0 comments on commit 8b421fe

Please sign in to comment.