summaryrefslogtreecommitdiffstats
path: root/src/kernel/net-txrx.c
blob: 4ca3fa54b02c3d61047bef02db99b07cc647824b (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 * This file is part of the Distributed Network Block Device 3
 *
 * Copyright(c) 2019 Frederic Robra <frederic@robra.org>
 * Parts copyright 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-txrx.h"
#include "utils.h"
#include "clientconfig.h"
#include "mq.h"
#include <linux/signal.h>

#include <net/sock.h>



#define DNBD3_REQ_OP_SPECIAL REQ_OP_DRV_IN
#define DNBD3_REQ_OP_CONNECT REQ_OP_DRV_OUT

#define dnbd3_cmd_to_op_special(req, cmd) \
	(req)->cmd_flags = DNBD3_REQ_OP_SPECIAL | ((cmd) << REQ_FLAG_BITS)

#define dnbd3_op_special_to_cmd(req) \
	((req)->cmd_flags >> REQ_FLAG_BITS)

#define dnbd3_connect_to_req(req) \
	(req)->cmd_flags = DNBD3_REQ_OP_CONNECT \
			| ((CMD_SELECT_IMAGE) << REQ_FLAG_BITS)

#define dnbd3_test_block_to_req(req) \
	do { \
		(req)->cmd_flags = REQ_OP_READ; \
		(req)->__data_len = RTT_BLOCK_SIZE; \
		(req)->__sector = 0; \
	} while (0)


#define REQUEST_TIMEOUT \
	(HZ * SOCKET_TIMEOUT_CLIENT_DATA)


#define dnbd3_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)



/*
 * Methods for request and receive commands
 */


/**
 * dnbd3_to_handle - convert tag and cookie to handle
 * @tag: the tag to convert
 * @cookie: the cookie to convert
 */
static inline uint64_t dnbd3_to_handle(uint32_t tag, uint32_t cookie) {
	return ((uint64_t) tag << 32) | cookie;
}

/**
 * dnbd3_tag_from_handle - get tag from handle
 * @handle: the handle
 */
static inline uint32_t dnbd3_tag_from_handle(uint64_t handle) {
	return (uint32_t)(handle >> 32);
}

/**
 * dnbd3_cookie_from_handle - get cookie from handle
 * @handle: the handle
 */
static inline uint32_t dnbd3_cookie_from_handle(uint64_t handle) {
	return (uint32_t) handle;
}

/**
 * dnbd3_send_request - send a request
 * @sock: the socket where the request is send
 * @req: the request to send
 * @cmd: optional - the dnbd3_cmd from mq
 *
 * the tx_lock of the socket must be held
 */
int dnbd3_send_request(struct dnbd3_sock *sock, struct request *req,
		struct dnbd3_cmd *cmd)
{
	dnbd3_request_t request;
	struct msghdr msg;
	struct kvec iov[2];
	size_t iov_num = 1;
	size_t lng;
	int result;
	uint32_t tag;
	uint64_t handle;
	serialized_buffer_t payload_buffer;
	sock->pending = req;
	dnbd3_init_msghdr(msg);

	request.magic = dnbd3_packet_magic;

	switch (req_op(req)) {
	case REQ_OP_READ:
//		debug_sock(sock, "request operation read %p", req);
		request.cmd = CMD_GET_BLOCK;
		request.offset = blk_rq_pos(req) << 9; // * 512
		request.size = blk_rq_bytes(req);
		break;
	case DNBD3_REQ_OP_SPECIAL:
		debug_sock(sock, "request operation special");
		request.cmd = dnbd3_op_special_to_cmd(req);
		request.size = 0;
		break;
	case DNBD3_REQ_OP_CONNECT:
		debug_sock(sock, "request operation connect to %s",
				sock->device->imgname);
		request.cmd = CMD_SELECT_IMAGE;
		serializer_reset_write(&payload_buffer);
		serializer_put_uint16(&payload_buffer, PROTOCOL_VERSION);
		serializer_put_string(&payload_buffer, sock->device->imgname);
		serializer_put_uint16(&payload_buffer, sock->device->rid);
		serializer_put_uint8(&payload_buffer, 0); // is_server = false
		iov[1].iov_base = &payload_buffer;
		request.size = serializer_get_written_length(&payload_buffer);
		iov[1].iov_len = request.size;
		iov_num = 2;
		break;
	default:
		return -EIO;
	}
	sock->cookie++;
	if (cmd != NULL) {
		cmd->cookie = sock->cookie;
		tag = blk_mq_unique_tag(req);
		handle = dnbd3_to_handle(tag, sock->cookie);
	} else {
		handle = sock->cookie;
	}
	memcpy(&request.handle, &handle, sizeof(handle));

	fixup_request(request);
	iov[0].iov_base = &request;
	iov[0].iov_len = sizeof(request);
	lng = iov_num == 1 ? iov[0].iov_len : iov[0].iov_len + iov[1].iov_len;
	result = kernel_sendmsg(sock->sock, &msg, iov, iov_num, lng);
	if (result != lng) {
		error_sock(sock, "connection to server lost");
		if (cmd) {
			dnbd3_requeue_cmd(cmd);
		}
		sock->server->failures++;
		goto error;
	}

error:
	sock->pending = NULL;
	return result;
}


/**
 * dnbd3_send_request_cmd - send a dndb3 cmd
 * @sock: the socket where the request is send
 * @dnbd3_cmd: the dnbd3 cmd to send
 *
 * the tx_lock of the socket must be held
 */
int dnbd3_send_request_cmd(struct dnbd3_sock *sock, uint16_t dnbd3_cmd)
{
	int result;
	struct request *req = kmalloc(sizeof(struct request), GFP_KERNEL);
	if (!req) {
		error_sock(sock, "kmalloc failed");
		result = -EIO;
		goto error;
	}

	switch (dnbd3_cmd) {
	case CMD_KEEPALIVE:
	case CMD_GET_SERVERS:
		dnbd3_cmd_to_op_special(req, dnbd3_cmd);
		break;
	case CMD_SELECT_IMAGE:
		dnbd3_connect_to_req(req);
		break;
	case CMD_GET_BLOCK:
		dnbd3_test_block_to_req(req);
		break;
	default:
		warn_sock(sock, "unsupported command %d", dnbd3_cmd);
		result = -EINVAL;
		goto error;
	}

	if (unlikely(!sock->sock)) {
		result = -EIO;
		goto error;
	}
	sock->pending = req;
	result = dnbd3_send_request(sock, req, NULL);

error:
	if (req) {
		kfree(req);
	}
	return result;
}

/**
 * dnbd3_receive_cmd - receive a command
 * @sock: the socket where the request is received
 * @reply: an unused reply will be filled with the reply of the server
 *
 * this method should be called directly after the dnbd3_send_request_ method
 */
int dnbd3_receive_cmd(struct dnbd3_sock *sock, dnbd3_reply_t *reply)
{
	int result;
	struct msghdr msg;
	struct kvec iov;
	dnbd3_init_msghdr(msg);
	iov.iov_base = reply;
	iov.iov_len = sizeof(dnbd3_reply_t);
	result = kernel_recvmsg(sock->sock, &msg, &iov, 1, iov.iov_len,
			msg.msg_flags);
	if (result <= 0) {
		return result;
	}
	fixup_reply(dnbd3_reply);

	if (reply->magic != dnbd3_packet_magic) {
		error_sock(sock, "receive cmd wrong magic packet");
		return -EIO;
	}

	if (reply->cmd == 0) {
		error_sock(sock, "receive command was 0");
		return -EIO;
	}
	return result;
}

static int dnbd3_clear_socket(struct dnbd3_sock *sock, dnbd3_reply_t *reply,
		int remaining)
{
	int result = 0;
	char *buf;
	struct kvec iov;
	struct msghdr msg;
	dnbd3_init_msghdr(msg);
	warn_sock(sock, "clearing socket %d bytes", remaining);
	buf = kmalloc(RTT_BLOCK_SIZE, GFP_KERNEL);
	if (!buf) {
		error_sock(sock, "kmalloc failed");
		return -EIO;
	}
	iov.iov_base = buf;
	iov.iov_len = RTT_BLOCK_SIZE;
	while (remaining > 0) {
		result = kernel_recvmsg(sock->sock, &msg, &iov, 1, iov.iov_len,
				msg.msg_flags);
		if (result <= 0) {
			goto error;
		}
		remaining -= result;
	}

	debug_sock(sock, "cleared socket");
error:
	if (buf) {
		kfree(buf);
	}
	return result;
}

/**
 * dnbd3_receive_cmd_get_block_mq - receive a block for mq
 * @sock: the socket where the request is received
 * @reply: the reply initialized by dnbd3_receive_cmd
 *
 * this method should be called directly after the dnbd3_receive_cmd method
 *
 * this method copies the data to user space according to the request which is
 * encoded in the handle by the send request method and decoded here.
 */
int dnbd3_receive_cmd_get_block_mq(struct dnbd3_sock *sock,
		dnbd3_reply_t *reply)
{
	struct dnbd3_cmd *cmd;
	struct msghdr msg;
	struct request *req = NULL;
	struct kvec iov;
	struct req_iterator iter;
	struct bio_vec bvec_inst;
	struct dnbd3_device *dev = sock->device;
	struct bio_vec *bvec = &bvec_inst;
	sigset_t blocked, oldset;
	void *kaddr;
	uint32_t tag, cookie;
	uint16_t hwq;
	uint32_t remaining = reply->size;
	int result = 0;
	uint64_t handle;
	dnbd3_init_msghdr(msg);

	memcpy(&handle, &reply->handle, sizeof(handle));
	cookie = dnbd3_cookie_from_handle(handle);
	tag = dnbd3_tag_from_handle(handle);

	hwq = blk_mq_unique_tag_to_hwq(tag);
	if (hwq < dev->tag_set.nr_hw_queues) {
		req = blk_mq_tag_to_rq(dev->tag_set.tags[hwq],
				blk_mq_unique_tag_to_tag(tag));
	}
	if (!req || !blk_mq_request_started(req)) {
		error_sock(sock, "unexpected reply (%d) %p", tag, req);
		if (req) {
			debug_sock(sock, "requeue request");
			dnbd3_requeue_cmd(blk_mq_rq_to_pdu(req));
		}
		dnbd3_clear_socket(sock, reply, remaining);
		return -EIO;
	}
	cmd = blk_mq_rq_to_pdu(req);

	mutex_lock(&cmd->lock);
	if (cmd->cookie != cookie) {
		error_sock(sock, "double reply on req %p, cookie %u, handle cookie %u",
				req, cmd->cookie, cookie);
		mutex_unlock(&cmd->lock);
		dnbd3_clear_socket(sock, reply, remaining);
		return -EIO;
	}

	rq_for_each_segment(bvec_inst, req, iter) {
		siginitsetinv(&blocked, sigmask(SIGKILL));
		sigprocmask(SIG_SETMASK, &blocked, &oldset);

		kaddr = kmap(bvec->bv_page) + bvec->bv_offset;
		iov.iov_base = kaddr;
		iov.iov_len = bvec->bv_len;
		result = kernel_recvmsg(sock->sock, &msg, &iov, 1, bvec->bv_len,
				msg.msg_flags);
		remaining -= result;
		if (result != bvec->bv_len) {
			kunmap(bvec->bv_page);
			sigprocmask(SIG_SETMASK, &oldset, NULL );
			error_sock(sock, "could not receive from net to block layer");
			dnbd3_requeue_cmd(cmd);
			mutex_unlock(&cmd->lock);
			if (result >= 0) {
				dnbd3_clear_socket(sock, reply, remaining);
				return -EIO;
			} else {
				return result;
			}
		}
		kunmap(bvec->bv_page);

		sigprocmask(SIG_SETMASK, &oldset, NULL );
	}
	mutex_unlock(&cmd->lock);
	dnbd3_end_cmd(cmd, 0);
	return result;
}



/**
 * dnbd3_receive_cmd_get_block_test - receive a test block
 * @sock: the socket where the request is received
 * @reply: the reply initialized by dnbd3_receive_cmd
 *
 * this method should be called directly after the dnbd3_receive_cmd method
 *
 * the received data is just thrown away
 */
int dnbd3_receive_cmd_get_block_test(struct dnbd3_sock *sock,
		dnbd3_reply_t *reply)
{
	struct msghdr msg;
	struct kvec iov;
	int result = 0;
	char *buf = kmalloc(reply->size, GFP_KERNEL);
	if (!buf) {
		error_sock(sock, "kmalloc failed");
		goto error;
	}

	dnbd3_init_msghdr(msg);
	iov.iov_base = buf;
	iov.iov_len = reply->size;
	result = kernel_recvmsg(sock->sock, &msg, &iov, 1, reply->size,
			msg.msg_flags);
	if (result != RTT_BLOCK_SIZE) {
		error_sock(sock, "receive test block failed");
		goto error;
	}

error:
	if (buf) {
		kfree(buf);
	}
	return result;
}

/**
 * dnbd3_receive_cmd_get_servers - receive new servers
 * @sock: the socket where the request is received
 * @reply: the reply initialized by dnbd3_receive_cmd
 *
 * this method should be called directly after the dnbd3_receive_cmd method
 *
 * the new servers are copied to dnbd3_device.new_servers and
 * dnbd3_device.new_server_num is set accordingly
 */
int dnbd3_receive_cmd_get_servers(struct dnbd3_sock *sock, dnbd3_reply_t *reply)
{
	struct msghdr msg;
	struct kvec iov;
	struct dnbd3_device *dev = sock->device;
	int result = 1;
	int count, remaining;
	dnbd3_init_msghdr(msg);

	debug_sock(sock, "get servers received");
	mutex_lock(&dev->device_lock);
	if (!dev->use_server_provided_alts) {
		remaining = reply->size;
		goto consume_payload;
	}
	dev->new_servers_num = 0;
	count = MIN(NUMBER_SERVERS, reply->size / sizeof(dnbd3_server_entry_t));

	if (count != 0) {
		iov.iov_base = dev->new_servers;
		iov.iov_len = count * sizeof(dnbd3_server_entry_t);
		result = kernel_recvmsg(sock->sock, &msg, &iov, 1, iov.iov_len,
				msg.msg_flags);
		if (result <= 0) {
			error_sock(sock, "failed to receive get servers %d",
					result);
			mutex_unlock(&dev->device_lock);
			return result;
		} else if (result != (count * sizeof(dnbd3_server_entry_t))) {
			error_sock(sock, "failed to get servers");
			mutex_unlock(&dev->device_lock);
			return -EIO;
		}
		dev->new_servers_num = count;
	}
	/*
	 * if there were more servers than accepted, remove the remaining data
	 * from the socket buffer
	 * abuse the reply struct as the receive buffer
	 */
	remaining = reply->size - (count * sizeof(dnbd3_server_entry_t));
consume_payload:
	while (remaining > 0) {
		count = MIN(sizeof(dnbd3_reply_t), remaining);
		iov.iov_base = reply;
		iov.iov_len = count;
		result = kernel_recvmsg(sock->sock, &msg, &iov, 1, count,
				msg.msg_flags);
		if (result <= 0) {
			error_sock(sock, "failed to receive payload from get servers");
			mutex_unlock(&dev->device_lock);
			return result;
		}
		remaining -= result;
	}
	mutex_unlock(&dev->device_lock);
	return result;
}

/**
 * dnbd3_receive_cmd_latest_rid - receive latest rid
 * @sock: the socket where the request is received
 * @reply: the reply initialized by dnbd3_receive_cmd
 *
 * this method should be called directly after the dnbd3_receive_cmd method
 *
 * dnbd3_device.update_available is set if a new RID is received
 */
int dnbd3_receive_cmd_latest_rid(struct dnbd3_sock *sock, dnbd3_reply_t *reply)
{
	struct kvec iov;
	uint16_t rid;
	int result;
	struct msghdr msg;
	struct dnbd3_device *dev = sock->device;
	dnbd3_init_msghdr(msg);
	debug_sock(sock, "latest rid received");

	if (reply->size != 2) {
		error_sock(sock, "failed to get latest rid, wrong size");
		return -EIO;
	}
	iov.iov_base = &rid;
	iov.iov_len = sizeof(rid);
	result = kernel_recvmsg(sock->sock, &msg, &iov, 1, iov.iov_len,
			msg.msg_flags);
	if (result <= 0) {
		error_sock(sock, "failed to receive latest rid");
		return result;
	}
	rid = net_order_16(rid);
	debug_sock(sock, "latest rid of %s is %d (currently using %d)",
			dev->imgname, (int)rid, (int)dev->rid);
	dev->update_available = (rid > dev->rid ? true : false);
	return result;
}


/**
 * dnbd3_receive_cmd_latest_rid - select the image
 * @sock: the socket where the request is received
 * @reply: the reply initialized by dnbd3_receive_cmd
 *
 * this method should be called directly after the dnbd3_receive_cmd method
 *
 * if this is the first connection the image name, file size and rid will be set
 * if this is a further connection image name, file size and rid will be checked
 */
int dnbd3_receive_cmd_select_image(struct dnbd3_sock *sock,
		dnbd3_reply_t *reply)
{
	struct kvec iov;
	uint16_t rid;
	char *name;
	int result;
	struct msghdr msg;
	serialized_buffer_t payload_buffer;
	uint64_t reported_size;
	struct dnbd3_device *dev = sock->device;
	dnbd3_init_msghdr(msg);
	debug_sock(sock, "select image received");
	iov.iov_base = &payload_buffer;
	iov.iov_len = reply->size;
	result = kernel_recvmsg(sock->sock, &msg, &iov, 1, iov.iov_len,
			msg.msg_flags);
	if (result <= 0) {
		error_sock(sock, "failed to receive select image %d", result);
		return result;
	} else if (result != reply->size) {
		error_sock(sock, "could not read CMD_SELECT_IMAGE payload on handshake, size is %d and should be %d",
				result, reply->size);
		return -EIO;
	}

	/* handle/check reply payload */
	serializer_reset_read(&payload_buffer, reply->size);
	sock->server->protocol_version = serializer_get_uint16(&payload_buffer);
	if (sock->server->protocol_version < MIN_SUPPORTED_SERVER) {
		error_sock(sock, "server version is lower than min supported version");
		return -EIO;
	}

	//TODO compare RID

	name = serializer_get_string(&payload_buffer);
	rid = serializer_get_uint16(&payload_buffer);
	if (dev->rid != rid && strcmp(name, dev->imgname) != 0) {
		error_sock(sock, "server offers image '%s', requested '%s'",
				name, dev->imgname);
		return -EIO;
	}

	reported_size = serializer_get_uint64(&payload_buffer);
	if (!dev->reported_size) {
		if (reported_size < 4096) {
			error_sock(sock, "reported size by server is < 4096");
			return -EIO;
		}
		dev->reported_size = reported_size;
		set_capacity(dev->disk, dev->reported_size >> 9); /* 512 Byte */
	} else if (dev->reported_size != reported_size) {
		error_sock(sock, "reported size by server is %llu but should be %llu",
				reported_size, dev->reported_size);
		return -EIO;
	}
	return result;

}