xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Thanos Makatos <thanos.makatos@citrix.com>
To: xen-devel@lists.xensource.com
Cc: thanos.makatos@citrix.com
Subject: [PATCH 7 of 9 RFC v3] blktap3/libblktapctl: Introduce tapdisk message exchange functionality
Date: Thu, 24 Jan 2013 12:20:47 +0000	[thread overview]
Message-ID: <bb6d03ab75c3e58be246.1359030047@makatos-desktop> (raw)
In-Reply-To: <patchbomb.1359030040@makatos-desktop>

This patch introduces file conrol/tap-ctl-ipc.c, where the functionality of
talking to a tapdisk process is implemented. This file is imported from the
existing blktap2 implementation, with most changes coming from blktap2 living
in github.

---
Changed since v2:
  * Check for unexpected response in tap_ctl_send_and_receive.
  * If the message has been served but an error occurred, the error in the
    message is returned by the function in order to simplify error
    checking/handling by the caller.

diff --git a/tools/blktap2/control/tap-ctl-ipc.c b/tools/blktap3/control/tap-ctl-ipc.c
copy from tools/blktap2/control/tap-ctl-ipc.c
copy to tools/blktap3/control/tap-ctl-ipc.c
--- a/tools/blktap2/control/tap-ctl-ipc.c
+++ b/tools/blktap3/control/tap-ctl-ipc.c
@@ -30,44 +30,34 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <fcntl.h>
 #include <sys/un.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/socket.h>
+#include <assert.h>
 
 #include "tap-ctl.h"
-#include "blktap2.h"
+#include "blktap3.h"
 
 int tap_ctl_debug = 0;
 
 int
-tap_ctl_read_message(int fd, tapdisk_message_t *message, int timeout)
+tap_ctl_read_raw(int fd, void *buf, size_t size, struct timeval *timeout)
 {
 	fd_set readfds;
-	int ret, len, offset;
-	struct timeval tv, *t;
+	size_t offset = 0;
+	int ret;
 
-	t      = NULL;
-	offset = 0;
-	len    = sizeof(tapdisk_message_t);
-
-	if (timeout) {
-		tv.tv_sec  = timeout;
-		tv.tv_usec = 0;
-		t = &tv;
-	}
-
-	memset(message, 0, sizeof(tapdisk_message_t));
-
-	while (offset < len) {
+	while (offset < size) {
 		FD_ZERO(&readfds);
 		FD_SET(fd, &readfds);
 
-		ret = select(fd + 1, &readfds, NULL, NULL, t);
+		ret = select(fd + 1, &readfds, NULL, NULL, timeout);
 		if (ret == -1)
 			break;
 		else if (FD_ISSET(fd, &readfds)) {
-			ret = read(fd, message + offset, len - offset);
+			ret = read(fd, buf + offset, size - offset);
 			if (ret <= 0)
 				break;
 			offset += ret;
@@ -75,34 +65,24 @@ tap_ctl_read_message(int fd, tapdisk_mes
 			break;
 	}
 
-	if (offset != len) {
-		EPRINTF("failure reading message\n");
+	if (offset != size) {
+		EPRINTF("failure reading data %zd/%zd\n", offset, size);
 		return -EIO;
 	}
 
-	DBG("received '%s' message (uuid = %u)\n",
-	    tapdisk_message_name(message->type), message->cookie);
-
 	return 0;
 }
 
 int
-tap_ctl_write_message(int fd, tapdisk_message_t *message, int timeout)
+tap_ctl_write_message(int fd, tapdisk_message_t * message,
+					  struct timeval *timeout)
 {
 	fd_set writefds;
 	int ret, len, offset;
-	struct timeval tv, *t;
 
-	t      = NULL;
 	offset = 0;
 	len    = sizeof(tapdisk_message_t);
 
-	if (timeout) {
-		tv.tv_sec  = timeout;
-		tv.tv_usec = 0;
-		t = &tv;
-	}
-
 	DBG("sending '%s' message (uuid = %u)\n",
 	    tapdisk_message_name(message->type), message->cookie);
 
@@ -113,7 +93,7 @@ tap_ctl_write_message(int fd, tapdisk_me
 		/* we don't bother reinitializing tv. at worst, it will wait a
 		 * bit more time than expected. */
 
-		ret = select(fd + 1, NULL, &writefds, NULL, t);
+		ret = select(fd + 1, NULL, &writefds, NULL, timeout);
 		if (ret == -1)
 			break;
 		else if (FD_ISSET(fd, &writefds)) {
@@ -134,9 +114,15 @@ tap_ctl_write_message(int fd, tapdisk_me
 }
 
 int
-tap_ctl_send_and_receive(int sfd, tapdisk_message_t *message, int timeout)
+tap_ctl_send_and_receive(const int sfd, tapdisk_message_t * const message,
+						 struct timeval *timeout)
 {
 	int err;
+	td_msg_id_t msg_type;
+
+	assert(message);
+
+	msg_type = message->type;
 
 	err = tap_ctl_write_message(sfd, message, timeout);
 	if (err) {
@@ -147,12 +133,29 @@ tap_ctl_send_and_receive(int sfd, tapdis
 
 	err = tap_ctl_read_message(sfd, message, timeout);
 	if (err) {
+		/*
+		 * TODO If there are messages for which a response is not required
+		 * and failure to receive a reply is expected, this print is bogus.
+		 */
 		EPRINTF("failed to receive '%s' message\n",
 			tapdisk_message_name(message->type));
 		return err;
 	}
 
-	return 0;
+	if (tapdisk_message_is_rsp_paired(msg_type)) {
+		if (message->type - msg_type != 1) {
+			err = EINVAL;
+			EPRINTF("invalid response '%s' to message '%s'\n",
+					tapdisk_message_name(message->type),
+					tapdisk_message_name(msg_type));
+		} else if (message->u.response.error) {
+			err = message->u.response.error;
+			EPRINTF("failed to serve message '%s': %s\n",
+					tapdisk_message_name(msg_type), strerror(err));
+		}
+	}
+
+	return err;
 }
 
 char *
@@ -161,7 +164,7 @@ tap_ctl_socket_name(int id)
 	char *name;
 
 	if (asprintf(&name, "%s/%s%d",
-		     BLKTAP2_CONTROL_DIR, BLKTAP2_CONTROL_SOCKET, id) == -1)
+				 BLKTAP3_CONTROL_DIR, BLKTAP3_CONTROL_SOCKET, id) == -1)
 		return NULL;
 
 	return name;
@@ -222,7 +225,8 @@ tap_ctl_connect_id(int id, int *sfd)
 }
 
 int
-tap_ctl_connect_send_and_receive(int id, tapdisk_message_t *message, int timeout)
+tap_ctl_connect_send_and_receive(int id, tapdisk_message_t * message,
+								 struct timeval *timeout)
 {
 	int err, sfd;
 
@@ -235,3 +239,20 @@ tap_ctl_connect_send_and_receive(int id,
 	close(sfd);
 	return err;
 }
+
+int
+tap_ctl_read_message(int fd, tapdisk_message_t * message,
+					 struct timeval *timeout)
+{
+	size_t size = sizeof(tapdisk_message_t);
+	int err;
+
+	err = tap_ctl_read_raw(fd, message, size, timeout);
+	if (err)
+		return err;
+
+	DBG("received '%s' message (uuid = %u)\n",
+		tapdisk_message_name(message->type), message->cookie);
+
+	return 0;
+}

  parent reply	other threads:[~2013-01-24 12:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-24 12:20 [PATCH 0 of 9 RFC v3] blktap3: Introduce a small subset of blktap3 files Thanos Makatos
2013-01-24 12:20 ` [PATCH 1 of 9 RFC v3] blktap3: Introduce blktap3 headers Thanos Makatos
2013-01-24 12:20 ` [PATCH 2 of 9 RFC v3] blktap3/libblktapctl: Introduce tapdisk message types and structures Thanos Makatos
2013-01-24 12:20 ` [PATCH 3 of 9 RFC v3] blktap3/libblktapctl: Introduce the tapdisk control header Thanos Makatos
2013-01-24 12:20 ` [PATCH 4 of 9 RFC v3] blktap3/libblktapctl: Introduce listing running tapdisks functionality Thanos Makatos
2013-01-24 12:20 ` [PATCH 5 of 9 RFC v3] blktap3/libblktapctl: Introduce functionality used by tapback to instruct tapdisk to connect to the sring Thanos Makatos
2013-01-24 12:20 ` [PATCH 6 of 9 RFC v3] blktap3/libblktapctl: Introduce block device control information retrieval functionality Thanos Makatos
2013-01-24 12:20 ` Thanos Makatos [this message]
2013-01-24 12:20 ` [PATCH 8 of 9 RFC v3] blktap3/libblktapctl: Introduce tapdisk spawn functionality Thanos Makatos
2013-01-24 12:20 ` [PATCH 9 of 9 RFC v3] blktap3/libblktapctl: Introduce makefile that builds tapback-required libblktapctl functionality Thanos Makatos
2013-02-25 11:15 ` [PATCH 0 of 9 RFC v3] blktap3: Introduce a small subset of blktap3 files Thanos Makatos

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bb6d03ab75c3e58be246.1359030047@makatos-desktop \
    --to=thanos.makatos@citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).