[CRIU] Re: [PATCH 6/6] protobuf: Convert sk_packet_entry to PB engine

Cyrill Gorcunov gorcunov at openvz.org
Tue Jul 17 08:10:04 EDT 2012


On Tue, Jul 17, 2012 at 02:21:27PM +0400, Cyrill Gorcunov wrote:
> On Tue, Jul 17, 2012 at 02:18:26PM +0400, Pavel Emelyanov wrote:
> > >>
> > >> options dump/restore -- should be separate patch
> > > 
> > > How? They are part of sk-unix.proto.
> > 
> > Helpers and .proto file in one patch users in 2 others.
> 
> Unused functions lead us to the state where gcc complains on them.
> Anyway, thanks for comments, Pavel, i'll try to address all complains.

Pavel, could you please check if these two patches looks good for you?

	Cyrill
-------------- next part --------------
>From 295a85fe8455c68658180e95d978dceb691e227d Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Tue, 17 Jul 2012 15:32:47 +0400
Subject: [PATCH 1/2] protobuf: Convert sk_opts_entry to PB format

This patch prepares the ground for further patches.
sk_opts_entry get converted to PB format but not
yet used anywhere in code. Also a few additional
pb_ helpers are added.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/sockets.h      |   10 ++++++
 protobuf/Makefile      |    1 +
 protobuf/sk-opts.proto |    7 ++++
 sockets.c              |   76 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 94 insertions(+), 0 deletions(-)
 create mode 100644 protobuf/sk-opts.proto

diff --git a/include/sockets.h b/include/sockets.h
index 9979a76..50d143e 100644
--- a/include/sockets.h
+++ b/include/sockets.h
@@ -7,6 +7,9 @@
 
 #include "types.h"
 
+#include "protobuf.h"
+#include "../protobuf/sk-opts.pb-c.h"
+
 struct fdinfo_list_entry;
 struct sk_opts_entry;
 struct cr_options;
@@ -22,11 +25,18 @@ struct socket_desc {
 	int			already_dumped;
 };
 
+extern void free_socket_opts(SkOptsEntry *soe);
+extern SkOptsEntry *alloc_socket_opts(void);
+
 extern int dump_socket(struct fd_parms *p, int lfd, const struct cr_fdset *cr_fdset);
 extern int dump_socket_opts(int sk, struct sk_opts_entry *soe);
 extern int restore_socket_opts(int sk, struct sk_opts_entry *soe);
 extern void show_socket_opts(struct sk_opts_entry *soe);
 
+extern int pb_restore_socket_opts(int sk, SkOptsEntry *soe);
+extern int pb_dump_socket_opts(int sk, SkOptsEntry *soe);
+extern void pb_show_socket_opts(SkOptsEntry *soe);
+
 extern int sk_collect_one(int ino, int family, struct socket_desc *d);
 extern int collect_sockets(void);
 extern int collect_inet_sockets(void);
diff --git a/protobuf/Makefile b/protobuf/Makefile
index 4b7c520..216bcb7 100644
--- a/protobuf/Makefile
+++ b/protobuf/Makefile
@@ -36,6 +36,7 @@ PROTO_FILES	+= tcp-stream.proto
 PROTO_FILES	+= sk-packet.proto
 PROTO_FILES	+= mnt.proto
 PROTO_FILES	+= pipe-data.proto
+PROTO_FILES	+= sk-opts.proto
 
 HDRS	:= $(patsubst %.proto,%.pb-c.h,$(PROTO_FILES))
 SRCS	:= $(patsubst %.proto,%.pb-c.c,$(PROTO_FILES))
diff --git a/protobuf/sk-opts.proto b/protobuf/sk-opts.proto
new file mode 100644
index 0000000..b2da568
--- /dev/null
+++ b/protobuf/sk-opts.proto
@@ -0,0 +1,7 @@
+message sk_opts_entry {
+	required uint32		so_sndbuf	= 1;
+	required uint32		so_rcvbuf	= 2;
+
+	repeated uint64		so_snd_tmo	= 3;
+	repeated uint64		so_rcv_tmo	= 4;
+}
diff --git a/sockets.c b/sockets.c
index d802759..9401d88 100644
--- a/sockets.c
+++ b/sockets.c
@@ -63,6 +63,22 @@ static int do_restore_opt(int sk, int name, void *val, int len)
 
 #define restore_opt(s, n, f)	do_restore_opt(s, n, f, sizeof(*f))
 
+int pb_restore_socket_opts(int sk, SkOptsEntry *soe)
+{
+	int ret = 0;
+
+	if (soe->n_so_snd_tmo != 2 || soe->n_so_rcv_tmo != 2)
+		return -1;
+
+	ret |= restore_opt(sk, SO_SNDBUFFORCE, &soe->so_sndbuf);
+	ret |= restore_opt(sk, SO_RCVBUFFORCE, &soe->so_rcvbuf);
+
+	ret |= do_restore_opt(sk, SO_SNDTIMEO, soe->so_snd_tmo, pb_repeated_size(soe, so_snd_tmo));
+	ret |= do_restore_opt(sk, SO_RCVTIMEO, soe->so_rcv_tmo, pb_repeated_size(soe, so_rcv_tmo));
+
+	return ret;
+}
+
 int restore_socket_opts(int sk, struct sk_opts_entry *soe)
 {
 	int ret = 0;
@@ -107,6 +123,51 @@ int dump_socket_opts(int sk, struct sk_opts_entry *soe)
 	return ret;
 }
 
+void free_socket_opts(SkOptsEntry *soe)
+{
+	if (soe) {
+		xfree(soe->so_snd_tmo);
+		xfree(soe->so_rcv_tmo);
+		xfree(soe);
+	}
+}
+
+SkOptsEntry *alloc_socket_opts(void)
+{
+	SkOptsEntry *soe = xmalloc(sizeof(*soe));
+	if (soe) {
+		sk_opts_entry__init(soe);
+
+		soe->n_so_snd_tmo = 2;
+		soe->n_so_rcv_tmo = 2;
+
+		soe->so_snd_tmo = xmalloc(pb_repeated_size(soe, so_snd_tmo));
+		soe->so_rcv_tmo = xmalloc(pb_repeated_size(soe, so_rcv_tmo));
+
+		if (!soe->so_snd_tmo || !soe->so_rcv_tmo) {
+			free_socket_opts(soe);
+			return NULL;
+		}
+	}
+	return soe;
+}
+
+int pb_dump_socket_opts(int sk, SkOptsEntry *soe)
+{
+	int ret = 0;
+
+	if (soe->n_so_snd_tmo != 2 || soe->n_so_rcv_tmo != 2)
+		return -1;
+
+	ret |= dump_opt(sk, SO_SNDBUF, &soe->so_sndbuf);
+	ret |= dump_opt(sk, SO_RCVBUF, &soe->so_rcvbuf);
+
+	ret |= do_dump_opt(sk, SO_SNDTIMEO, soe->so_snd_tmo, pb_repeated_size(soe, so_snd_tmo));
+	ret |= do_dump_opt(sk, SO_RCVTIMEO, soe->so_rcv_tmo, pb_repeated_size(soe, so_rcv_tmo));
+
+	return ret;
+}
+
 int dump_socket(struct fd_parms *p, int lfd, const struct cr_fdset *cr_fdset)
 {
 	int family;
@@ -385,3 +446,18 @@ void show_socket_opts(struct sk_opts_entry *soe)
 
 	pr_msg("\n");
 }
+
+void pb_show_socket_opts(SkOptsEntry *soe)
+{
+	pr_msg("\t");
+
+	pr_msg("sndbuf: %u  ", soe->so_sndbuf);
+	pr_msg("rcvbuf: %u  ", soe->so_rcvbuf);
+
+	if (soe->n_so_snd_tmo > 1)
+		sk_show_timeval("sndtmo", soe->so_snd_tmo);
+	if (soe->n_so_rcv_tmo > 1)
+		sk_show_timeval("rcvtmo", soe->so_rcv_tmo);
+
+	pr_msg("\n");
+}
-- 
1.7.7.6

-------------- next part --------------
>From 45ebc3aae3adf391bd5e666fd940fb41f037df26 Mon Sep 17 00:00:00 2001
From: Cyrill Gorcunov <gorcunov at openvz.org>
Date: Tue, 17 Jul 2012 15:52:53 +0400
Subject: [PATCH 2/2] protobuf: Convert unix_sk_entry to PB engine v2

v2:
 - Use alloc_socket_opts/free_socket_opts helpers
 - Use pb_prep_fown helper

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 include/image.h        |   16 ------
 protobuf/Makefile      |    1 +
 protobuf/sk-unix.proto |   16 ++++++
 sk-unix.c              |  136 ++++++++++++++++++++++++------------------------
 4 files changed, 85 insertions(+), 84 deletions(-)
 create mode 100644 protobuf/sk-unix.proto

diff --git a/include/image.h b/include/image.h
index 36d4f6c..71ac7db 100644
--- a/include/image.h
+++ b/include/image.h
@@ -84,22 +84,6 @@ struct sk_opts_entry {
 	u64	so_rcv_tmo[2];
 };
 
-struct unix_sk_entry {
-	u32	id;
-	u32	ino;
-	u8	type;
-	u8	state;
-	u8	namelen; /* fits UNIX_PATH_MAX */
-	u8	pad;
-	u32	flags;
-	u32	uflags;  /* own service flags */
-	u32	backlog;
-	u32	peer;
-	fown_t	fown;
-	struct sk_opts_entry opts;
-	u8	name[0];
-} __packed;
-
 struct inet_sk_entry {
 	u32	id;
 	u32	ino;
diff --git a/protobuf/Makefile b/protobuf/Makefile
index 216bcb7..3e8516e 100644
--- a/protobuf/Makefile
+++ b/protobuf/Makefile
@@ -37,6 +37,7 @@ PROTO_FILES	+= sk-packet.proto
 PROTO_FILES	+= mnt.proto
 PROTO_FILES	+= pipe-data.proto
 PROTO_FILES	+= sk-opts.proto
+PROTO_FILES	+= sk-unix.proto
 
 HDRS	:= $(patsubst %.proto,%.pb-c.h,$(PROTO_FILES))
 SRCS	:= $(patsubst %.proto,%.pb-c.c,$(PROTO_FILES))
diff --git a/protobuf/sk-unix.proto b/protobuf/sk-unix.proto
new file mode 100644
index 0000000..46a9fa9
--- /dev/null
+++ b/protobuf/sk-unix.proto
@@ -0,0 +1,16 @@
+import "fown.proto";
+import "sk-opts.proto";
+
+message unix_sk_entry {
+	required uint32			id		=  1;
+	required uint32			ino		=  2;
+	required uint32			type		=  3;
+	required uint32			state		=  4;
+	required uint32			flags		=  5;
+	required uint32			uflags		=  6;
+	required uint32			backlog		=  7;
+	required uint32			peer		=  8;
+	required fown_entry		fown		=  9;
+	required sk_opts_entry		opts		= 10;
+	required bytes			name		= 11;
+}
diff --git a/sk-unix.c b/sk-unix.c
index 9273293..706af5c 100644
--- a/sk-unix.c
+++ b/sk-unix.c
@@ -20,7 +20,8 @@
 #include "sockets.h"
 #include "sk-queue.h"
 
-static char buf[4096];
+#include "protobuf.h"
+#include "protobuf/sk-unix.pb-c.h"
 
 struct unix_sk_desc {
 	struct socket_desc	sd;
@@ -72,10 +73,10 @@ static void show_one_unix(char *act, const struct unix_sk_desc *sk)
 	}
 }
 
-static void show_one_unix_img(const char *act, const struct unix_sk_entry *e)
+static void show_one_unix_img(const char *act, const UnixSkEntry *e)
 {
 	pr_info("\t%s: id 0x%x ino 0x%x peer 0x%x type %d state %d name %d bytes\n",
-		act, e->id, e->ino, e->peer, e->type, e->state, e->namelen);
+		act, e->id, e->ino, e->peer, e->type, e->state, (int)e->name.len);
 }
 
 static int can_dump_unix_sk(const struct unix_sk_desc *sk)
@@ -109,7 +110,9 @@ static int can_dump_unix_sk(const struct unix_sk_desc *sk)
 static int dump_one_unix_fd(int lfd, u32 id, const struct fd_parms *p)
 {
 	struct unix_sk_desc *sk;
-	struct unix_sk_entry ue;
+	UnixSkEntry ue = UNIX_SK_ENTRY__INIT;
+	FownEntry fown = FOWN_ENTRY__INIT;
+	SkOptsEntry *skopts = NULL;
 
 	sk = (struct unix_sk_desc *)lookup_socket(p->stat.st_ino);
 	if (!sk)
@@ -120,15 +123,24 @@ static int dump_one_unix_fd(int lfd, u32 id, const struct fd_parms *p)
 
 	BUG_ON(sk->sd.already_dumped);
 
+	skopts = alloc_socket_opts();
+	if (!skopts)
+		goto err;
+
+	pb_prep_fown(&fown, &p->fown);
+
+	ue.name.len	= (size_t)sk->namelen;
+	ue.name.data	= (void *)sk->name;
+
 	ue.id		= id;
 	ue.ino		= sk->sd.ino;
 	ue.type		= sk->type;
 	ue.state	= sk->state;
-	ue.namelen	= sk->namelen;
 	ue.flags	= p->flags;
 	ue.backlog	= sk->wqlen;
 	ue.peer		= sk->peer_ino;
-	ue.fown		= p->fown;
+	ue.fown		= &fown;
+	ue.opts		= skopts;
 	ue.uflags	= 0;
 
 	if (ue.peer) {
@@ -190,12 +202,10 @@ static int dump_one_unix_fd(int lfd, u32 id, const struct fd_parms *p)
 				ue.ino, ue.peer);
 	}
 
-	if (dump_socket_opts(lfd, &ue.opts))
+	if (pb_dump_socket_opts(lfd, skopts))
 		goto err;
 
-	if (write_img(fdset_fd(glob_fdset, CR_FD_UNIXSK), &ue))
-		goto err;
-	if (write_img_buf(fdset_fd(glob_fdset, CR_FD_UNIXSK), sk->name, ue.namelen))
+	if (pb_write(fdset_fd(glob_fdset, CR_FD_UNIXSK), &ue, unix_sk_entry))
 		goto err;
 
 	if (sk->rqlen != 0 && !(sk->type == SOCK_STREAM &&
@@ -209,9 +219,13 @@ static int dump_one_unix_fd(int lfd, u32 id, const struct fd_parms *p)
 
 	list_del_init(&sk->list);
 	sk->sd.already_dumped = 1;
+
+	free_socket_opts(skopts);
+
 	return 0;
 
 err:
+	free_socket_opts(skopts);
 	return -1;
 }
 
@@ -376,7 +390,9 @@ int fix_external_unix_sockets(void)
 	pr_debug("Dumping external sockets\n");
 
 	list_for_each_entry(sk, &unix_sockets, list) {
-		struct unix_sk_entry e = { };
+		UnixSkEntry e = UNIX_SK_ENTRY__INIT;
+		FownEntry fown = FOWN_ENTRY__INIT;
+		SkOptsEntry skopts = SK_OPTS_ENTRY__INIT;
 
 		BUG_ON(sk->sd.already_dumped);
 
@@ -394,16 +410,16 @@ int fix_external_unix_sockets(void)
 		e.ino		= sk->sd.ino;
 		e.type		= SOCK_DGRAM;
 		e.state		= TCP_LISTEN;
-		e.namelen	= sk->namelen;
+		e.name.data	= (void *)sk->name;
+		e.name.len	= (size_t)sk->namelen;
 		e.uflags	= USK_EXTERN;
 		e.peer		= 0;
+		e.fown		= &fown;
+		e.opts		= &skopts;
 
 		show_one_unix("Dumping extern", sk);
 
-		if (write_img(fdset_fd(glob_fdset, CR_FD_UNIXSK), &e))
-			goto err;
-		if (write_img_buf(fdset_fd(glob_fdset, CR_FD_UNIXSK),
-					sk->name, e.namelen))
+		if (pb_write(fdset_fd(glob_fdset, CR_FD_UNIXSK), &e, unix_sk_entry))
 			goto err;
 
 		show_one_unix_img("Dumped extern", &e);
@@ -415,7 +431,7 @@ err:
 }
 
 struct unix_sk_info {
-	struct unix_sk_entry *ue;
+	UnixSkEntry *ue;
 	struct list_head list;
 	char *name;
 	unsigned flags;
@@ -440,35 +456,32 @@ static struct unix_sk_info *find_unix_sk_by_ino(int ino)
 
 void show_unixsk(int fd, struct cr_options *o)
 {
-	struct unix_sk_entry ue;
+	UnixSkEntry *ue;
 	int ret = 0;
 
 	pr_img_head(CR_FD_UNIXSK);
 
 	while (1) {
-		ret = read_img_eof(fd, &ue);
+		ret = pb_read_eof(fd, &ue, unix_sk_entry);
 		if (ret <= 0)
 			goto out;
 
 		pr_msg("id %#x ino %#x type %s state %s namelen %4d backlog %4d peer %#x flags %#x uflags %#x",
-			ue.id, ue.ino, sktype2s(ue.type), skstate2s(ue.state),
-			ue.namelen, ue.backlog, ue.peer, ue.flags, ue.uflags);
-
-		if (ue.namelen) {
-			BUG_ON(ue.namelen > sizeof(buf));
-			ret = read_img_buf(fd, buf, ue.namelen);
-			if (ret < 0) {
-				pr_info("\n");
-				goto out;
-			}
-			if (!buf[0])
-				buf[0] = '@';
-			pr_msg(" --> %s\n", buf);
+			ue->id, ue->ino, sktype2s(ue->type), skstate2s(ue->state),
+			(int)ue->name.len, ue->backlog, ue->peer, ue->flags, ue->uflags);
+
+		if (ue->name.len) {
+			if (!ue->name.data[0])
+				ue->name.data[0] = '@';
+			pr_msg(" --> %s\n", ue->name.data);
 		} else
 			pr_msg("\n");
-		pr_msg("\t"), show_fown_cont(&ue.fown), pr_msg("\n");
+		pb_show_fown_cont(ue->fown);
+		pr_msg("\n");
 
-		show_socket_opts(&ue.opts);
+		if (ue->opts)
+			pb_show_socket_opts(ue->opts);
+		unix_sk_entry__free_unpacked(ue, NULL);
 	}
 out:
 	pr_img_tail(CR_FD_UNIXSK);
@@ -516,11 +529,11 @@ int run_unix_connections(void)
 
 		memset(&addr, 0, sizeof(addr));
 		addr.sun_family = AF_UNIX;
-		memcpy(&addr.sun_path, peer->name, peer->ue->namelen);
+		memcpy(&addr.sun_path, peer->name, peer->ue->name.len);
 try_again:
 		if (connect(fle->fe->fd, (struct sockaddr *)&addr,
 					sizeof(addr.sun_family) +
-					peer->ue->namelen) < 0) {
+					peer->ue->name.len) < 0) {
 			if (attempts) {
 				usleep(1000);
 				attempts--;
@@ -534,10 +547,10 @@ try_again:
 		if (restore_sk_queue(fle->fe->fd, peer->ue->id))
 			return -1;
 
-		if (rst_file_params(fle->fe->fd, &ui->ue->fown, ui->ue->flags))
+		if (pb_rst_file_params(fle->fe->fd, ui->ue->fown, ui->ue->flags))
 			return -1;
 
-		if (restore_socket_opts(fle->fe->fd, &ui->ue->opts))
+		if (pb_restore_socket_opts(fle->fe->fd, ui->ue->opts))
 			return -1;
 
 		cj = cj->next;
@@ -562,10 +575,10 @@ static int bind_unix_sk(int sk, struct unix_sk_info *ui)
 
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_UNIX;
-	memcpy(&addr.sun_path, ui->name, ui->ue->namelen);
+	memcpy(&addr.sun_path, ui->name, ui->ue->name.len);
 
 	if (bind(sk, (struct sockaddr *)&addr,
-				sizeof(addr.sun_family) + ui->ue->namelen)) {
+				sizeof(addr.sun_family) + ui->ue->name.len)) {
 		pr_perror("Can't bind socket");
 		return -1;
 	}
@@ -604,7 +617,7 @@ static int open_unixsk_pair_master(struct unix_sk_info *ui)
 	if (bind_unix_sk(sk[0], ui))
 		return -1;
 
-	if (rst_file_params(sk[0], &ui->ue->fown, ui->ue->flags))
+	if (pb_rst_file_params(sk[0], ui->ue->fown, ui->ue->flags))
 		return -1;
 
 	tsk = socket(PF_UNIX, SOCK_DGRAM, 0);
@@ -645,10 +658,10 @@ static int open_unixsk_pair_slave(struct unix_sk_info *ui)
 	if (bind_unix_sk(sk, ui))
 		return -1;
 
-	if (rst_file_params(sk, &ui->ue->fown, ui->ue->flags))
+	if (pb_rst_file_params(sk, ui->ue->fown, ui->ue->flags))
 		return -1;
 
-	if (restore_socket_opts(sk, &ui->ue->opts))
+	if (pb_restore_socket_opts(sk, ui->ue->opts))
 		return -1;
 
 	return sk;
@@ -677,10 +690,10 @@ static int open_unixsk_standalone(struct unix_sk_info *ui)
 			return -1;
 		}
 
-		if (rst_file_params(sk, &ui->ue->fown, ui->ue->flags))
+		if (pb_rst_file_params(sk, ui->ue->fown, ui->ue->flags))
 			return -1;
 
-		if (restore_socket_opts(sk, &ui->ue->opts))
+		if (pb_restore_socket_opts(sk, ui->ue->opts))
 			return -1;
 
 	} else if (ui->peer) {
@@ -713,6 +726,7 @@ static struct file_desc_ops unix_desc_ops = {
 
 int collect_unix_sockets(void)
 {
+	struct unix_sk_info *ui = NULL;
 	int fd, ret;
 
 	pr_info("Reading unix sockets in\n");
@@ -726,38 +740,24 @@ int collect_unix_sockets(void)
 	}
 
 	while (1) {
-		struct unix_sk_info *ui;
-
-		ui = xmalloc(sizeof(*ui));
 		ret = -1;
+		ui = xmalloc(sizeof(*ui));
 		if (ui == NULL)
 			break;
 
-		ui->ue = xmalloc(sizeof(*ui->ue));
-		if (ui->ue == NULL)
-			break;
-
-		ret = read_img_eof(fd, ui->ue);
-		if (ret <= 0) {
-			xfree(ui);
+		ret = pb_read_eof(fd, &ui->ue, unix_sk_entry);
+		if (ret <= 0)
 			break;
-		}
 
-		if (ui->ue->namelen) {
+		if (ui->ue->name.len) {
 			ret = -1;
 
-			if (!ui->ue->namelen || ui->ue->namelen >= UNIX_PATH_MAX) {
-				pr_err("Bad unix name len %d\n", ui->ue->namelen);
+			if (ui->ue->name.len >= UNIX_PATH_MAX) {
+				pr_err("Bad unix name len %d\n", (int)ui->ue->name.len);
 				break;
 			}
 
-			ui->name = xmalloc(ui->ue->namelen);
-			if (ui->name == NULL)
-				break;
-
-			ret = read_img_buf(fd, ui->name, ui->ue->namelen);
-			if (ret < 0)
-				break;
+			ui->name = (void *)ui->ue->name.data;
 
 			/*
 			 * Make FS clean from sockets we're about to
@@ -766,8 +766,7 @@ int collect_unix_sockets(void)
 			if (ui->name[0] != '\0' &&
 			    !(ui->ue->uflags & USK_EXTERN))
 				unlink(ui->name);
-		} else
-			ui->name = NULL;
+		}
 
 		ui->peer = NULL;
 		ui->flags = 0;
@@ -776,6 +775,7 @@ int collect_unix_sockets(void)
 		list_add_tail(&ui->list, &unix_sockets);
 	}
 
+	xfree(ui);
 	close(fd);
 
 	return read_sk_queues();
-- 
1.7.7.6



More information about the CRIU mailing list