summaryrefslogtreecommitdiffstats
path: root/src/kernel
diff options
context:
space:
mode:
authorFrederic Robra2019-07-02 14:38:11 +0200
committerFrederic Robra2019-07-02 14:38:11 +0200
commit8aa12e9063201e0704f7ad3a34fb4bf8614a075d (patch)
treef6875d934c2ccb3c59232182c48cba6c209bf1c5 /src/kernel
parentadded mq (diff)
downloaddnbd3-ng-8aa12e9063201e0704f7ad3a34fb4bf8614a075d.tar.gz
dnbd3-ng-8aa12e9063201e0704f7ad3a34fb4bf8614a075d.tar.xz
dnbd3-ng-8aa12e9063201e0704f7ad3a34fb4bf8614a075d.zip
added code to connect to server
Diffstat (limited to 'src/kernel')
-rw-r--r--src/kernel/block.c230
-rw-r--r--src/kernel/block.h5
-rw-r--r--src/kernel/core.c3
-rw-r--r--src/kernel/dnbd3.h5
-rw-r--r--src/kernel/mq.c4
-rw-r--r--src/kernel/serialize_kmod.c5
6 files changed, 248 insertions, 4 deletions
diff --git a/src/kernel/block.c b/src/kernel/block.c
index bd2f8d7..4eb52c5 100644
--- a/src/kernel/block.c
+++ b/src/kernel/block.c
@@ -5,11 +5,21 @@
* Author: fred
*/
+#include <net/sock.h>
#include "dnbd3.h"
#include "block.h"
+#include "clientconfig.h"
+#define dnbd3_sock_create(af,type,proto,sock) sock_create_kern(&init_net, (af) == HOST_IP4 ? AF_INET : AF_INET6, type, proto, sock)
+#define init_msghdr(h) do { \
+ h.msg_name = NULL; \
+ h.msg_namelen = 0; \
+ h.msg_control = NULL; \
+ h.msg_controllen = 0; \
+ h.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; \
+ } while (0)
static int dnbd3_open(struct block_device *bdev, fmode_t mode)
{
@@ -34,16 +44,236 @@ void dnbd3_blk_fail_all_requests(dnbd3_device_t *dev)
}
+
int dnbd3_net_connect(dnbd3_device_t *dev)
{
+ struct request *req1 = NULL;
+ struct timeval timeout;
+
printk(KERN_DEBUG "dnbd3: net connect device %i\n", dev->minor);
+
+ if (dev->disconnecting) {
+ printk(KERN_DEBUG "dnbd3: device still disconnecting\n");
+ while (dev->disconnecting)
+ schedule();
+ }
+ if (dev->request) {
+ printk(KERN_DEBUG "dnbd3: device still in request\n");
+ while (dev->disconnecting)
+ schedule();
+ }
+
+ timeout.tv_sec = SOCKET_TIMEOUT_CLIENT_DATA;
+ timeout.tv_usec = 0;
+
+ // do some checks before connecting
+
+ req1 = kmalloc(sizeof(*req1), GFP_ATOMIC );
+ if (!req1) {
+ printk(KERN_ERR "dnbd3: kmalloc failed\n");
+ goto error;
+ }
+ if (dev->cur_server.host.port == 0 || dev->cur_server.host.type == 0 || dev->imgname == NULL ) {
+ printk(KERN_ERR "dnbd3: host, port or image name not set\n");
+ goto error;
+ }
+ if (dev->sock) {
+ printk(KERN_WARNING "dnbd3: already connected\n");
+ goto error;
+ }
+
+ if (dev->cur_server.host.type != HOST_IP4 && dev->cur_server.host.type != HOST_IP6) {
+ printk(KERN_ERR "dnbd3: unknown address type %d\n", (int)dev->cur_server.host.type);
+ goto error;
+ }
+
+ if (dev->better_sock == NULL ) {
+ // no established connection yet from discovery thread, start new one
+ dnbd3_request_t dnbd3_request;
+ dnbd3_reply_t dnbd3_reply;
+ struct msghdr msg;
+ struct kvec iov[2];
+ uint16_t rid;
+ char *name;
+ int mlen;
+ init_msghdr(msg);
+
+ if (dnbd3_sock_create(dev->cur_server.host.type, SOCK_STREAM, IPPROTO_TCP, &dev->sock) < 0) {
+ printk(KERN_ERR "dnbd3: could not create socket\n");
+ goto error;
+ }
+
+ kernel_setsockopt(dev->sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
+ kernel_setsockopt(dev->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
+ dev->sock->sk->sk_allocation = GFP_NOIO;
+ if (dev->cur_server.host.type == HOST_IP4) {
+ struct sockaddr_in sin;
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ memcpy(&(sin.sin_addr), dev->cur_server.host.addr, 4);
+ sin.sin_port = dev->cur_server.host.port;
+ if (kernel_connect(dev->sock, (struct sockaddr *)&sin, sizeof(sin), 0) != 0) {
+ printk(KERN_ERR "dnbd3: connection to host failed (ipv4)\n");
+ goto error;
+ }
+ } else {
+ struct sockaddr_in6 sin;
+ memset(&sin, 0, sizeof(sin));
+ sin.sin6_family = AF_INET6;
+ memcpy(&(sin.sin6_addr), dev->cur_server.host.addr, 16);
+ sin.sin6_port = dev->cur_server.host.port;
+ if (kernel_connect(dev->sock, (struct sockaddr *)&sin, sizeof(sin), 0) != 0){
+ printk(KERN_ERR "dnbd3: connection to host failed (ipv6)\n");
+ goto error;
+ }
+ }
+ // Request filesize
+ dnbd3_request.magic = dnbd3_packet_magic;
+ dnbd3_request.cmd = CMD_SELECT_IMAGE;
+ iov[0].iov_base = &dnbd3_request;
+ iov[0].iov_len = sizeof(dnbd3_request);
+ serializer_reset_write(&dev->payload_buffer);
+ serializer_put_uint16(&dev->payload_buffer, PROTOCOL_VERSION);
+ serializer_put_string(&dev->payload_buffer, dev->imgname);
+ serializer_put_uint16(&dev->payload_buffer, dev->rid);
+ serializer_put_uint8(&dev->payload_buffer, 0); // is_server = false
+ iov[1].iov_base = &dev->payload_buffer;
+ dnbd3_request.size = iov[1].iov_len = serializer_get_written_length(&dev->payload_buffer);
+ fixup_request(dnbd3_request);
+ mlen = sizeof(dnbd3_request) + iov[1].iov_len;
+ if (kernel_sendmsg(dev->sock, &msg, iov, 2, mlen) != mlen) {
+ printk(KERN_ERR "dnbd3: could not send CMD_SIZE_REQUEST\n");
+ goto error;
+ }
+ // receive reply header
+ iov[0].iov_base = &dnbd3_reply;
+ iov[0].iov_len = sizeof(dnbd3_reply);
+ if (kernel_recvmsg(dev->sock, &msg, iov, 1, sizeof(dnbd3_reply), msg.msg_flags) != sizeof(dnbd3_reply)) {
+ printk(KERN_ERR "dnbd3: received corrupted reply header after CMD_SIZE_REQUEST\n");
+ goto error;
+ }
+
+ // check reply header
+ fixup_reply(dnbd3_reply);
+ if (dnbd3_reply.cmd != CMD_SELECT_IMAGE ||
+ dnbd3_reply.size < 3 ||
+ dnbd3_reply.size > MAX_PAYLOAD ||
+ dnbd3_reply.magic != dnbd3_packet_magic) {
+ printk(KERN_ERR "dnbd3: received invalid reply to CMD_SIZE_REQUEST image does not exist on server\n");
+ goto error;
+ }
+
+ // receive reply payload
+ iov[0].iov_base = &dev->payload_buffer;
+ iov[0].iov_len = dnbd3_reply.size;
+ if (kernel_recvmsg(dev->sock, &msg, iov, 1, dnbd3_reply.size, msg.msg_flags) != dnbd3_reply.size) {
+ printk(KERN_ERR "dnbd3: could not read CMD_SELECT_IMAGE payload on handshake\n");
+ goto error;
+ }
+
+ // handle/check reply payload
+ serializer_reset_read(&dev->payload_buffer, dnbd3_reply.size);
+ dev->cur_server.protocol_version = serializer_get_uint16(&dev->payload_buffer);
+ if (dev->cur_server.protocol_version < MIN_SUPPORTED_SERVER) {
+ printk(KERN_ERR "dnbd3: server version is lower than min supported version\n");
+ goto error;
+ }
+
+ name = serializer_get_string(&dev->payload_buffer);
+ if (dev->rid != 0 && strcmp(name, dev->imgname) != 0) {
+ printk(KERN_ERR "dnbd3: server offers image '%s', requested '%s'\n", name, dev->imgname);
+ goto error;
+ }
+
+ if (strlen(dev->imgname) < strlen(name)) {
+ dev->imgname = krealloc(dev->imgname, strlen(name) + 1, GFP_ATOMIC );
+ if (dev->imgname == NULL ) {
+ printk(KERN_ERR "dnbd3: reallocating buffer for new image name failed\n");
+ goto error;
+ }
+ }
+ strcpy(dev->imgname, name);
+ rid = serializer_get_uint16(&dev->payload_buffer);
+ if (dev->rid != 0 && dev->rid != rid) {
+ printk(KERN_ERR "dnbd3: server provides rid %d, requested was %d\n", (int)rid, (int)dev->rid);
+ goto error;
+ }
+
+ dev->rid = rid;
+ dev->reported_size = serializer_get_uint64(&dev->payload_buffer);
+ if (dev->reported_size < 4096) {
+ printk(KERN_ERR "dnbd3: reported size by server is < 4096\n");
+ goto error;
+ }
+
+ // store image information
+ set_capacity(dev->disk, dev->reported_size >> 9); /* 512 Byte blocks */
+ printk(KERN_DEBUG "dnbd3: connected to image %s, filesize %llu\n", dev->imgname, dev->reported_size);
+ dev->update_available = 0;
+ } else { // Switching server, connection is already established and size request was executed
+
+ printk(KERN_DEBUG "dnbd3: changing server\n");
+ dev->sock = dev->better_sock;
+ dev->better_sock = NULL;
+ kernel_setsockopt(dev->sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
+ kernel_setsockopt(dev->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
+ }
+
+ dev->panic = 0;
+ dev->panic_count = 0;
+
+ // Enqueue request to request_queue_send for a fresh list of alt servers
+ //TODO refresh alt server list
+
+ // add heartbeat timer
+ //TODO needed anymore=
+// dev->heartbeat_count = 0;
+// timer_setup(&dev->hb_timer, dnbd3_net_heartbeat, 0);
+// dev->hb_timer.expires = jiffies + HZ;
+// add_timer(&dev->hb_timer);
return 0;
+error:
+ if (dev->sock) {
+ sock_release(dev->sock);
+ dev->sock = NULL;
+ }
+ dev->cur_server.host.type = 0;
+ dev->cur_server.host.port = 0;
+ if (req1) {
+ kfree(req1);
+ }
+ return -1;
}
int dnbd3_net_disconnect(dnbd3_device_t *dev)
{
printk(KERN_DEBUG "dnbd3: net disconnect device %i\n", dev->minor);
+ if (dev->disconnecting) {
+ return 0;
+ }
+
+ dev->disconnecting = 1;
+
+ // clear heartbeat timer
+// del_timer(&dev->hb_timer);
+
+ dev->discover = 0;
+
+ if (dev->sock) {
+ kernel_sock_shutdown(dev->sock, SHUT_RDWR);
+ }
+
+ // clear socket
+ if (dev->sock) {
+ sock_release(dev->sock);
+ dev->sock = NULL;
+ }
+ dev->cur_server.host.type = 0;
+ dev->cur_server.host.port = 0;
+
+ dev->disconnecting = 0;
+
return 0;
}
diff --git a/src/kernel/block.h b/src/kernel/block.h
index 5493a00..af99108 100644
--- a/src/kernel/block.h
+++ b/src/kernel/block.h
@@ -8,6 +8,11 @@
#ifndef SRC_KERNEL_BLOCK_H_
#define SRC_KERNEL_BLOCK_H_
+
+int dnbd3_net_connect(dnbd3_device_t *dev);
+
+int dnbd3_net_disconnect(dnbd3_device_t *dev);
+
extern struct block_device_operations dnbd3_fops;
diff --git a/src/kernel/core.c b/src/kernel/core.c
index 7725a1d..3b30a11 100644
--- a/src/kernel/core.c
+++ b/src/kernel/core.c
@@ -427,6 +427,9 @@ static void dnbd3_dev_remove(struct dnbd3_device_t *dnbd3)
del_gendisk(disk);
blk_cleanup_queue(q);
blk_mq_free_tag_set(&dnbd3->tag_set);
+ if (dnbd3->sock) {
+ dnbd3_net_disconnect(dnbd3);
+ }
disk->private_data = NULL;
put_disk(disk);
}
diff --git a/src/kernel/dnbd3.h b/src/kernel/dnbd3.h
index b8b0f59..3fb625a 100644
--- a/src/kernel/dnbd3.h
+++ b/src/kernel/dnbd3.h
@@ -28,6 +28,7 @@
#define KERNEL_MODULE
#include "types.h"
+#include "serialize.h"
typedef struct
{
@@ -58,11 +59,11 @@ typedef struct dnbd3_device_t {
struct socket *sock;
dnbd3_server_t cur_server, initial_server;
uint64_t cur_rtt;
-// serialized_buffer_t payload_buffer;
+ serialized_buffer_t payload_buffer;
dnbd3_server_t alt_servers[NUMBER_SERVERS]; // array of alt servers
int new_servers_num; // number of new alt servers that are waiting to be copied to above array
dnbd3_server_entry_t new_servers[NUMBER_SERVERS]; // pending new alt servers
- uint8_t discover, panic, disconnecting, update_available, panic_count;
+ uint8_t discover, panic, disconnecting, update_available, panic_count, request;
uint8_t use_server_provided_alts;
uint16_t rid;
uint32_t heartbeat_count;
diff --git a/src/kernel/mq.c b/src/kernel/mq.c
index d402ecf..ee60574 100644
--- a/src/kernel/mq.c
+++ b/src/kernel/mq.c
@@ -16,7 +16,7 @@ static int dnbd3_handle_cmd(struct dnbd3_cmd *cmd, int index)
struct request *req = blk_mq_rq_from_pdu(cmd);
struct dnbd3_device_t *dev = cmd->dnbd3;
struct dnbd3_sock *nsock;
- int ret;
+ int ret = -1;
printk(KERN_DEBUG "dnbd3: handle command %i device %i\n", cmd->cmd_cookie, dev->minor);
@@ -46,7 +46,7 @@ static blk_status_t dnbd3_queue_rq(struct blk_mq_hw_ctx *hctx, const struct blk_
ret = BLK_STS_OK;
mutex_unlock(&cmd->lock);
- return -10;
+ return ret;
}
static void dnbd3_complete_rq(struct request *req)
diff --git a/src/kernel/serialize_kmod.c b/src/kernel/serialize_kmod.c
new file mode 100644
index 0000000..50746df
--- /dev/null
+++ b/src/kernel/serialize_kmod.c
@@ -0,0 +1,5 @@
+#include <linux/kernel.h>
+#include <linux/string.h>
+
+#define KERNEL_MODULE
+#include "serialize.c"