[2/2] BPF map files are now dumped.

Submitted by Abhishek Vijeev on April 21, 2020, 7:27 p.m.

Details

Message ID 20200421192741.18794-1-abhishek.vijeev@gmail.com
State New
Series "Series without cover letter"
Headers show

Commit Message

Abhishek Vijeev April 21, 2020, 7:27 p.m.
Signed-off-by: Abhishek Vijeev <abhishek.vijeev@gmail.com>
---
 criu/proc_parse.c        | 81 ++++++++++++++++++++++++++++++++++++++++
 images/bpfmap-file.proto |  6 +--
 2 files changed, 84 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/criu/proc_parse.c b/criu/proc_parse.c
index 4a22700a..46d6afd6 100644
--- a/criu/proc_parse.c
+++ b/criu/proc_parse.c
@@ -1683,6 +1683,73 @@  nodata:
 	goto parse_err;
 }
 
+static int parse_bpfmap(struct bfd *f, char *str, BpfmapFileEntry *bpf)
+{
+	/*
+	 * Format is
+	 * map_type: 3
+	 * key_size: 4
+	 * value_size: 4
+	 * max_entries: 4
+	 * map_flags: 0x0
+	 * memlock: 4096
+	 * map_id: 10
+	 * frozen: 0
+	 */
+
+	if (sscanf(str, "map_type: %u", &bpf->map_type) != 1)
+		goto parse_err;
+
+	str = breadline(f);
+	if (IS_ERR_OR_NULL(str))
+		goto nodata;
+	if (sscanf(str, "key_size: %u", &bpf->key_size) != 1)
+		goto parse_err;
+
+	str = breadline(f);
+	if (IS_ERR_OR_NULL(str))
+		goto nodata;
+	if (sscanf(str, "value_size: %u", &bpf->value_size) != 1)
+		goto parse_err;
+
+	str = breadline(f);
+	if (IS_ERR_OR_NULL(str))
+		goto nodata;
+	if (sscanf(str, "max_entries: %u", &bpf->max_entries) != 1)
+		goto parse_err;
+
+	str = breadline(f);
+	if (IS_ERR_OR_NULL(str))
+		goto nodata;
+	if (sscanf(str, "map_flags: %lu", &bpf->map_flags) != 1)
+		goto parse_err;
+
+	str = breadline(f);
+	if (IS_ERR_OR_NULL(str))
+		goto nodata;
+	if (sscanf(str, "memlock: %lu", &bpf->memlock) != 1)
+		goto parse_err;
+
+	str = breadline(f);
+	if (IS_ERR_OR_NULL(str))
+		goto nodata;
+	if (sscanf(str, "map_id: %u", &bpf->map_id) != 1)
+		goto parse_err;
+
+	str = breadline(f);
+	if (IS_ERR_OR_NULL(str))
+		goto nodata;
+	if (sscanf(str, "frozen: %d", &bpf->frozen) != 1)
+		goto parse_err;
+	return 0;
+
+parse_err:
+	return -1;
+nodata:
+	pr_err("No data left in proc file while parsing bpfmap\n");
+	goto parse_err;
+}
+
 #define fdinfo_field(str, field)	!strncmp(str, field":", sizeof(field))
 
 static int parse_file_lock_buf(char *buf, struct file_lock *fl,
@@ -2015,6 +2082,20 @@  static int parse_fdinfo_pid_s(int pid, int fd, int type, void *arg)
 			entry_met = true;
 			continue;
 		}
+		if (fdinfo_field(str, "map_type")) {
+
+			BpfmapFileEntry *bpf = arg;
+
+			if (type != FD_TYPES__BPFMAP)
+				goto parse_err;
+
+			ret = parse_bpfmap(&f, str, bpf);
+			if (ret)
+				goto parse_err;
+
+			entry_met = true;
+			continue;
+		}
 	}
 
 	exit_code = 0;
diff --git a/images/bpfmap-file.proto b/images/bpfmap-file.proto
index a21c032c..9f0b8593 100644
--- a/images/bpfmap-file.proto
+++ b/images/bpfmap-file.proto
@@ -15,9 +15,9 @@  message bpfmap_file_entry {
     required uint32     max_entries = 10;
 
     optional sint32		mnt_id	= 11 [default = -1];
-    optional uint64     map_flags   = 12;
-    optional uint64     memlock = 13;
-    optional bool       frozen  = 14;
+    required uint64     map_flags   = 12;
+    required uint64     memlock = 13;
+    required bool       frozen  = 14;
 
 
 }
\ No newline at end of file

Comments

Abhishek Vijeev April 21, 2020, 7:27 p.m.
Hi,

With reference to https://github.com/checkpoint-restore/criu/issues/777 and
https://lists.openvz.org/pipermail/criu/2020-April/044957.html, I have modified the function
'parse_fdinfo_pid_s()' to parse BPF file info from the proc filesystem.

A small note: I have changed the labels of 3 fields of bpfmap-file.proto to 'required' in order
to verify whether all fields were being dumped successfully ('optional' fields were not dumped by
CRIU and hence the change). As mentioned in point (b) of my previous email, I will correctly label
all fields of the bpfmap-file.proto file with the right datatypes too, once I better understand
the nature of data stored in the proc filesystem for BPF maps.

For the example program here https://github.com/checkpoint-restore/criu/issues/777#issue-490673307,
CRIU now dumps the BPF map file information as shown below. 

Here the output of the dump 'files.img' obtained using 'crit show':

{
    "magic": "FILES", 
    "entries": [
        {
            "type": "REG", 
            "id": 1, 
            "reg": {
                "id": 1, 
                "flags": "", 
                "pos": 0, 
                "fown": {
                    "uid": 0, 
                    "euid": 0, 
                    "signum": 0, 
                    "pid_type": 0, 
                    "pid": 0
                }, 
                "name": "/home/abhishek/criu_dumps/bpf_test", 
                "size": 8472, 
                "mode": 33277
            }
        }, 
        {
            "type": "REG", 
            "id": 2, 
            "reg": {
                "id": 2, 
                "flags": "", 
                "pos": 0, 
                "fown": {
                    "uid": 0, 
                    "euid": 0, 
                    "signum": 0, 
                    "pid_type": 0, 
                    "pid": 0
                }, 
                "name": "/lib/x86_64-linux-gnu/libc-2.27.so", 
                "size": 2030544, 
                "mode": 33261
            }
        }, 
        {
            "type": "REG", 
            "id": 3, 
            "reg": {
                "id": 3, 
                "flags": "", 
                "pos": 0, 
                "fown": {
                    "uid": 0, 
                    "euid": 0, 
                    "signum": 0, 
                    "pid_type": 0, 
                    "pid": 0
                }, 
                "name": "/lib/x86_64-linux-gnu/ld-2.27.so", 
                "size": 170960, 
                "mode": 33261
            }
        }, 
        {
            "type": "REG", 
            "id": 5, 
            "reg": {
                "id": 5, 
                "flags": "O_RDWR", 
                "pos": 0, 
                "fown": {
                    "uid": 0, 
                    "euid": 0, 
                    "signum": 0, 
                    "pid_type": 0, 
                    "pid": 0
                }, 
                "name": "/dev/pts/2", 
                "mode": 8576
            }
        }, 
        {
            "type": "TTY", 
            "id": 4, 
            "tty": {
                "id": 4, 
                "tty_info_id": 4, 
                "flags": "0x2", 
                "fown": {
                    "uid": 0, 
                    "euid": 0, 
                    "signum": 0, 
                    "pid_type": 0, 
                    "pid": 0
                }, 
                "regf_id": 5
            }
        }, 
        {
            "type": "BPFMAP", 
            "id": 6, 
            "bpf": {
                "id": 6, 
                "flags": "O_RDWR", 
                "pos": 0, 
                "fown": {
                    "uid": 0, 
                    "euid": 0, 
                    "signum": 0, 
                    "pid_type": 0, 
                    "pid": 0
                }, 
                "map_type": 3, 
                "key_size": 4, 
                "value_size": 4, 
                "map_id": 13, 
                "max_entries": 4
            }
        }, 
        {
            "type": "REG", 
            "id": 7, 
            "reg": {
                "id": 7, 
                "flags": "", 
                "pos": 0, 
                "fown": {
                    "uid": 0, 
                    "euid": 0, 
                    "signum": 0, 
                    "pid_type": 0, 
                    "pid": 0
                }, 
                "name": "/home/abhishek/criu_dumps", 
                "mode": 16893
            }
        }, 
        {
            "type": "REG", 
            "id": 8, 
            "reg": {
                "id": 8, 
                "flags": "", 
                "pos": 0, 
                "fown": {
                    "uid": 0, 
                    "euid": 0, 
                    "signum": 0, 
                    "pid_type": 0, 
                    "pid": 0
                }, 
                "name": "/", 
                "mode": 16877
            }
        }
    ]
}


Here the output of the dump 'fdinfo-2.img' obtained using 'crit show':

{
    "magic": "FDINFO", 
    "entries": [
        {
            "id": 4, 
            "flags": 0, 
            "type": "TTY", 
            "fd": 0
        }, 
        {
            "id": 4, 
            "flags": 0, 
            "type": "TTY", 
            "fd": 1
        }, 
        {
            "id": 4, 
            "flags": 0, 
            "type": "TTY", 
            "fd": 2
        }, 
        {
            "id": 6, 
            "flags": 1, 
            "type": "BPFMAP", 
            "fd": 3
        }
    ]
}


I apologize for the multiple patche submissions, as I over-estimated the time required to
code up the parsing part.

I await your kind feedback. In the meanwhile, I shall start working on restoring BPF map files.

Thank you,
Abhishek Vijeev.


Abhishek Vijeev (2):
  Preliminary set of changes to support dumping BPF map files. A major
    task is yet to be completed: parsing BPF file info from the proc
    filesystem in the function parse_fdinfo_pid_s()
  BPF map files are now dumped. Also changed the labels of 3 fields of
    bpfmap-file.proto to 'required' in order to verify whether all
    fields are being dumped.

 criu/Makefile.crtools    |  1 +
 criu/bpfmap.c            | 49 ++++++++++++++++++++++++
 criu/files.c             |  5 +++
 criu/include/bpfmap.h    |  9 +++++
 criu/proc_parse.c        | 81 ++++++++++++++++++++++++++++++++++++++++
 images/Makefile          |  1 +
 images/bpfmap-file.proto | 23 ++++++++++++
 images/fdinfo.proto      |  4 ++
 8 files changed, 173 insertions(+)
 create mode 100644 criu/bpfmap.c
 create mode 100644 criu/include/bpfmap.h
 create mode 100644 images/bpfmap-file.proto
Andrei Vagin April 23, 2020, 3:25 a.m.
Hi Abhishek,

We accept github pull requests and this is the preferred way to
contribute to CRIU. For that you should push your work to your fork of
CRIU at GitHub and create a pull request.

On Wed, Apr 22, 2020 at 12:57:39AM +0530, Abhishek Vijeev wrote:
> Hi,
> 
> With reference to https://github.com/checkpoint-restore/criu/issues/777 and
> https://lists.openvz.org/pipermail/criu/2020-April/044957.html, I have modified the function
> 'parse_fdinfo_pid_s()' to parse BPF file info from the proc filesystem.
> 
> A small note: I have changed the labels of 3 fields of bpfmap-file.proto to 'required' in order
> to verify whether all fields were being dumped successfully ('optional' fields were not dumped by
> CRIU and hence the change). As mentioned in point (b) of my previous email, I will correctly label
> all fields of the bpfmap-file.proto file with the right datatypes too, once I better understand
> the nature of data stored in the proc filesystem for BPF maps.

Commit messages should be wrapped to 72 characters per line.

> 
> For the example program here https://github.com/checkpoint-restore/criu/issues/777#issue-490673307,
> CRIU now dumps the BPF map file information as shown below. 
> 
> Here the output of the dump 'files.img' obtained using 'crit show':
> 
> {
>     "magic": "FILES", 
>     "entries": [
>         {
>             "type": "REG", 
>             "id": 1, 
>             "reg": {
>                 "id": 1, 
>                 "flags": "", 
>                 "pos": 0, 
>                 "fown": {
>                     "uid": 0, 
>                     "euid": 0, 
>                     "signum": 0, 
>                     "pid_type": 0, 
>                     "pid": 0
>                 }, 
>                 "name": "/home/abhishek/criu_dumps/bpf_test", 
>                 "size": 8472, 
>                 "mode": 33277
>             }
>         }, 
>         {
>             "type": "REG", 
>             "id": 2, 
>             "reg": {
>                 "id": 2, 
>                 "flags": "", 
>                 "pos": 0, 
>                 "fown": {
>                     "uid": 0, 
>                     "euid": 0, 
>                     "signum": 0, 
>                     "pid_type": 0, 
>                     "pid": 0
>                 }, 
>                 "name": "/lib/x86_64-linux-gnu/libc-2.27.so", 
>                 "size": 2030544, 
>                 "mode": 33261
>             }
>         }, 
>         {
>             "type": "REG", 
>             "id": 3, 
>             "reg": {
>                 "id": 3, 
>                 "flags": "", 
>                 "pos": 0, 
>                 "fown": {
>                     "uid": 0, 
>                     "euid": 0, 
>                     "signum": 0, 
>                     "pid_type": 0, 
>                     "pid": 0
>                 }, 
>                 "name": "/lib/x86_64-linux-gnu/ld-2.27.so", 
>                 "size": 170960, 
>                 "mode": 33261
>             }
>         }, 
>         {
>             "type": "REG", 
>             "id": 5, 
>             "reg": {
>                 "id": 5, 
>                 "flags": "O_RDWR", 
>                 "pos": 0, 
>                 "fown": {
>                     "uid": 0, 
>                     "euid": 0, 
>                     "signum": 0, 
>                     "pid_type": 0, 
>                     "pid": 0
>                 }, 
>                 "name": "/dev/pts/2", 
>                 "mode": 8576
>             }
>         }, 
>         {
>             "type": "TTY", 
>             "id": 4, 
>             "tty": {
>                 "id": 4, 
>                 "tty_info_id": 4, 
>                 "flags": "0x2", 
>                 "fown": {
>                     "uid": 0, 
>                     "euid": 0, 
>                     "signum": 0, 
>                     "pid_type": 0, 
>                     "pid": 0
>                 }, 
>                 "regf_id": 5
>             }
>         }, 
>         {
>             "type": "BPFMAP", 
>             "id": 6, 
>             "bpf": {
>                 "id": 6, 
>                 "flags": "O_RDWR", 
>                 "pos": 0, 
>                 "fown": {
>                     "uid": 0, 
>                     "euid": 0, 
>                     "signum": 0, 
>                     "pid_type": 0, 
>                     "pid": 0
>                 }, 
>                 "map_type": 3, 
>                 "key_size": 4, 
>                 "value_size": 4, 
>                 "map_id": 13, 
>                 "max_entries": 4
>             }
>         }, 
>         {
>             "type": "REG", 
>             "id": 7, 
>             "reg": {
>                 "id": 7, 
>                 "flags": "", 
>                 "pos": 0, 
>                 "fown": {
>                     "uid": 0, 
>                     "euid": 0, 
>                     "signum": 0, 
>                     "pid_type": 0, 
>                     "pid": 0
>                 }, 
>                 "name": "/home/abhishek/criu_dumps", 
>                 "mode": 16893
>             }
>         }, 
>         {
>             "type": "REG", 
>             "id": 8, 
>             "reg": {
>                 "id": 8, 
>                 "flags": "", 
>                 "pos": 0, 
>                 "fown": {
>                     "uid": 0, 
>                     "euid": 0, 
>                     "signum": 0, 
>                     "pid_type": 0, 
>                     "pid": 0
>                 }, 
>                 "name": "/", 
>                 "mode": 16877
>             }
>         }
>     ]
> }
> 
> 
> Here the output of the dump 'fdinfo-2.img' obtained using 'crit show':
> 
> {
>     "magic": "FDINFO", 
>     "entries": [
>         {
>             "id": 4, 
>             "flags": 0, 
>             "type": "TTY", 
>             "fd": 0
>         }, 
>         {
>             "id": 4, 
>             "flags": 0, 
>             "type": "TTY", 
>             "fd": 1
>         }, 
>         {
>             "id": 4, 
>             "flags": 0, 
>             "type": "TTY", 
>             "fd": 2
>         }, 
>         {
>             "id": 6, 
>             "flags": 1, 
>             "type": "BPFMAP", 
>             "fd": 3
>         }
>     ]
> }
> 
> 
> I apologize for the multiple patche submissions, as I over-estimated the time required to
> code up the parsing part.
> 
> I await your kind feedback. In the meanwhile, I shall start working on restoring BPF map files.
> 
> Thank you,
> Abhishek Vijeev.
> 
> 
> Abhishek Vijeev (2):
>   Preliminary set of changes to support dumping BPF map files. A major
>     task is yet to be completed: parsing BPF file info from the proc
>     filesystem in the function parse_fdinfo_pid_s()
>   BPF map files are now dumped. Also changed the labels of 3 fields of
>     bpfmap-file.proto to 'required' in order to verify whether all
>     fields are being dumped.
> 
>  criu/Makefile.crtools    |  1 +
>  criu/bpfmap.c            | 49 ++++++++++++++++++++++++
>  criu/files.c             |  5 +++
>  criu/include/bpfmap.h    |  9 +++++
>  criu/proc_parse.c        | 81 ++++++++++++++++++++++++++++++++++++++++
>  images/Makefile          |  1 +
>  images/bpfmap-file.proto | 23 ++++++++++++
>  images/fdinfo.proto      |  4 ++
>  8 files changed, 173 insertions(+)
>  create mode 100644 criu/bpfmap.c
>  create mode 100644 criu/include/bpfmap.h
>  create mode 100644 images/bpfmap-file.proto
> 
> -- 
> 2.17.1
> 
> _______________________________________________
> CRIU mailing list
> CRIU@openvz.org
> https://lists.openvz.org/mailman/listinfo/criu