summaryrefslogtreecommitdiffstats
path: root/src/kernel/net.c
blob: ac8e81d5877eb08f0f7c122917d4490295615790 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * This file is part of the Distributed Network Block Device 3
 *
 * Copyright(c) 2011-2012 Johann Latocha <johann@latocha.de>
 *
 * This file may be licensed under the terms of of the
 * GNU General Public License Version 2 (the ``GPL'').
 *
 * Software distributed under the License is distributed
 * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the GPL for the specific language
 * governing rights and limitations.
 *
 * You should have received a copy of the GPL along with this
 * program. If not, go to http://www.gnu.org/licenses/gpl.html
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "net.h"
#include "utils.h"

void dnbd3_net_connect(void)
{
	struct sockaddr_in sin;
	struct msghdr msg;
	struct kvec iov;
	struct dnbd3_request dnbd3_request;
	struct dnbd3_reply dnbd3_reply;
	struct task_struct *thread_send;
	struct task_struct *thread_receive;

	if (!_host || !_port || !_image_id)
	{
		printk("ERROR: Host or port not set.");
		return;
	}

	// initialize socket
	if (sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &_sock) < 0)
	{
		printk("ERROR: dnbd3 couldn't create socket.\n");
		return;
	}
	_sock->sk->sk_allocation = GFP_NOIO;
	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = inet_addr(_host);
	sin.sin_port = htons(simple_strtol(_port, NULL, 10));
	if (kernel_connect(_sock, (struct sockaddr *) &sin, sizeof(sin), 0) < 0)
	{
		printk("ERROR: dnbd3 couldn't connect to given host.\n");
		return;
	}

	// prepare message and send request
	dnbd3_request.cmd = CMD_GET_SIZE;
	strcpy(dnbd3_request.image_id, _image_id);
	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE

	iov.iov_base = &dnbd3_request;
	iov.iov_len = sizeof(dnbd3_request);
	kernel_sendmsg(_sock, &msg, &iov, 1, sizeof(dnbd3_request));

	// receive replay
	iov.iov_base = &dnbd3_reply;
	iov.iov_len = sizeof(dnbd3_reply);
	kernel_recvmsg(_sock, &msg, &iov, 1, sizeof(dnbd3_reply), msg.msg_flags);

	// set filesize
	if (dnbd3_reply.filesize <= 0)
	{
		printk("ERROR: File size returned by server is < 0.\n");
		return;
	}

	printk("INFO: dnbd3 filesize: %llu\n", dnbd3_reply.filesize);
	set_capacity(disk, dnbd3_reply.filesize >> 9); /* 512 Byte blocks */

	// start sending thread
	thread_send = kthread_create(dnbd3_net_send, NULL, "none");
	wake_up_process(thread_send);

	// start receiving thread
	thread_receive = kthread_create(dnbd3_net_receive, NULL, "none");
	wake_up_process(thread_receive);
}

int dnbd3_net_send(void *data)
{
	struct dnbd3_request dnbd3_request;
	struct request *blk_request;
	struct msghdr msg;
	struct kvec iov;

	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE

	set_user_nice(current, -20);

	while (!kthread_should_stop() || !list_empty(&_request_queue_send))
	{
		wait_event_interruptible(_process_queue_send,
				kthread_should_stop() || !list_empty(&_request_queue_send));

		if (list_empty(&_request_queue_send))
			continue;

		// extract block request
		spin_lock_irq(&dnbd3_lock);
		blk_request = list_entry(_request_queue_send.next, struct request, queuelist);
		list_del_init(&blk_request->queuelist);
		spin_unlock_irq(&dnbd3_lock);

		// prepare net request
		dnbd3_request.cmd = CMD_GET_BLOCK;
		dnbd3_request.offset = blk_rq_pos(blk_request) << 9; // *512
		dnbd3_request.size = blk_rq_bytes(blk_request); // blk_rq_bytes() Returns bytes left to complete in the entire request
		memcpy(dnbd3_request.handle, &blk_request, sizeof(blk_request));
		iov.iov_base = &dnbd3_request;
		iov.iov_len = sizeof(dnbd3_request);

		// send net request
		if (kernel_sendmsg(_sock, &msg, &iov, 1, sizeof(dnbd3_request)) <= 0)
			printk("ERROR: kernel_sendmsg\n");

		spin_lock_irq(&dnbd3_lock);
		list_add_tail(&blk_request->queuelist, &_request_queue_receive);
		spin_unlock_irq(&dnbd3_lock);
		wake_up(&_process_queue_receive);
	}
	return 0;
}

int dnbd3_net_receive(void *data)
{
	struct dnbd3_reply dnbd3_reply;
	struct request *blk_request;
	struct msghdr msg;
	struct kvec iov;
	struct req_iterator iter;
	struct bio_vec *bvec;
	unsigned long flags;
	sigset_t blocked, oldset;
	struct request *tmp_request, *received_request;
	void *kaddr;
	unsigned int size;

	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = MSG_WAITALL | MSG_NOSIGNAL; // No SIGPIPE

	set_user_nice(current, -20);

	while (!kthread_should_stop() || !list_empty(&_request_queue_receive))
	{
		wait_event_interruptible(_process_queue_receive,
				kthread_should_stop() || !list_empty(&_request_queue_receive));

		// receive net replay
		iov.iov_base = &dnbd3_reply;
		iov.iov_len = sizeof(dnbd3_reply);
		kernel_recvmsg(_sock, &msg, &iov, 1, sizeof(dnbd3_reply),
				msg.msg_flags);

		// search for replied request in queue
		received_request = *(struct request **) dnbd3_reply.handle;
		spin_lock_irq(&dnbd3_lock);
		list_for_each_entry_safe(blk_request, tmp_request,
				&_request_queue_receive, queuelist)
		{
			if (blk_request != received_request)
				continue;

			list_del_init(&blk_request->queuelist);
			break;
		}
		spin_unlock_irq(&dnbd3_lock);

		// receive data and answer to block layer
		rq_for_each_segment(bvec, blk_request, iter)
			{
				siginitsetinv(&blocked, sigmask(SIGKILL));
				sigprocmask(SIG_SETMASK, &blocked, &oldset);

				kaddr = kmap(bvec->bv_page) + bvec->bv_offset;
				size = bvec->bv_len;
				iov.iov_base = kaddr;
				iov.iov_len = size;
				kernel_recvmsg(_sock, &msg, &iov, 1, size, msg.msg_flags);
				kunmap(bvec->bv_page);

				sigprocmask(SIG_SETMASK, &oldset, NULL);
			}

		spin_lock_irqsave(&dnbd3_lock, flags);
		__blk_end_request_all(blk_request, 0);
		spin_unlock_irqrestore(&dnbd3_lock, flags);
	}

	return 0;
}