summaryrefslogtreecommitdiffstats
path: root/src/kernel/net.c
blob: 1207d0a229f86fa06aa83ec3bdd37db2c617015a (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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
/*
 * 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/sock.h>
#include <linux/wait.h>
#include <linux/sort.h>

#include "net.h"
#include "utils.h"
#include "clientconfig.h"
#include "mq.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 dnbd3_sock_create(af,type,proto,sock) \
	sock_create_kern(&init_net, (af) == HOST_IP4 ? AF_INET : AF_INET6, \
			type, proto, sock)

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

static int __dnbd3_socket_connect(struct dnbd3_sock *sock,
		struct dnbd3_server * server);
static int dnbd3_socket_connect(struct dnbd3_sock *sock,
		struct dnbd3_server * server);
static int dnbd3_socket_disconnect(struct dnbd3_sock *sock);

static uint64_t dnbd3_average_rtt(struct dnbd3_server *server)
{
	int i, j = 0;
	uint64_t avg = 0;
	for (i = 0; i < 4; i++) {
		avg += server->rtts[i];
		j += server->rtts[i] == 0 ? 0 : 1;
	}
	if (avg == 0) {
		return RTT_UNKNOWN;
	} else {
		avg = avg / j;
		avg += server->failures * avg / 10;
		return avg;
	}
}

/*
 * 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");
		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->panic = true;
		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
 */
static 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;
	}

	mutex_lock(&sock->tx_lock);
	sock->pending = req;
	result = dnbd3_send_request(sock, req, NULL);
	mutex_unlock(&sock->tx_lock);

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
 */
static 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;
	}
	/* hold the tx_lock so no new requests are send */
	mutex_lock(&sock->tx_lock);
	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:
	mutex_unlock(&sock->tx_lock);
	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.
 */
static 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
 */
static 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
 */
static 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
 */
static 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
 */
static 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;

}


/*
 * Timer and workers
 */

/**
 * dnbd3_timer - the timer to start different workers
 * @arg: the timer_list used to get the dnbd3_device
 *
 * workers to start:
 * - panic_worker
 * - keepalive_worker for each connected socket
 * - discovery_worker
 */
static void dnbd3_timer(struct timer_list *arg)
{
	struct dnbd3_device *dev;
	unsigned long busy;
	int i;
	dev = container_of(arg, struct dnbd3_device, timer);
	queue_work(dnbd3_wq, &dev->panic_worker);
	busy = dnbd3_is_mq_busy(dev);

	if (dev->timer_count % TIMER_INTERVAL_KEEPALIVE_PACKET == 0) {
		for (i = 0; i < NUMBER_CONNECTIONS; i++) {
			if (!test_bit(i, &busy) && dnbd3_is_sock_alive(dev->socks[i])) {
				queue_work(dnbd3_wq, &dev->socks[i].keepalive_worker);
			}
		}
	}

	if (!busy) {
		/* start after 4 seconds */
		if (dev->timer_count % TIMER_INTERVAL_PROBE_NORMAL == 4) {
			queue_work(dnbd3_wq, &dev->discovery_worker);
		}


	}
	dev->timer_count++;
	dev->timer.expires = jiffies + HZ;
	add_timer(&dev->timer);
}

/**
 * dnbd3_receive_worker - receives data from a socket
 * @work: the work used to get the dndb3_sock
 *
 * receives data until the socket is closed (returns 0)
 */
static void dnbd3_receive_worker(struct work_struct *work)
{
	struct dnbd3_sock *sock;
	dnbd3_reply_t reply;
	int result;
	sock = container_of(work, struct dnbd3_sock, receive_worker);
	debug_sock(sock, "receive worker is starting");

	while(1) { // loop until socket returns 0
		result = dnbd3_receive_cmd(sock, &reply);
		if (result == -EAGAIN) {
			continue;

		} else if (result <= 0) {
			error_sock(sock, "connection to server lost %d", result);
			goto error;

		}

		switch (reply.cmd) {
		case CMD_GET_BLOCK:
			result = dnbd3_receive_cmd_get_block_mq(sock, &reply);
			if (result <= 0) {
				error_sock(sock, "receive cmd get block mq failed %d",
						result);
				goto error;
			}
			continue;
		case CMD_GET_SERVERS:
			result = dnbd3_receive_cmd_get_servers(sock, &reply);
			if (result <= 0) {
				error_sock(sock, "receive cmd get servers failed %d",
						result);
				goto error;
			}
			break;
		case CMD_LATEST_RID:
			result = dnbd3_receive_cmd_latest_rid(sock, &reply);
			if (result <= 0) {
				error_sock(sock, "receive cmd latest rid failed %d",
						result);
				goto error;
			}
			break;
		case CMD_KEEPALIVE:
			if (reply.size != 0) {
				error_sock(sock, "got keep alive packet with payload");
				goto error;
			}
			debug_sock(sock, "keep alive received");
			break;
		case CMD_SELECT_IMAGE:
			result = dnbd3_receive_cmd_select_image(sock, &reply);
			if (result <= 0) {
				error_sock(sock, "receive cmd select image failed %d",
						result);
				goto error;
			}
			break;
		default:
			warn_sock(sock, "unknown command received");
			break;
		}
error:
		if (result == 0) {
			info_sock(sock, "result is 0, socket seems to be down");
			sock->panic = true;
			break;
		} else if (result < 0) {
			/* discovery takes care of to many failures */
			sock->server->failures++;
			warn_sock(sock, "receive error happened %d, total failures %d",
 					result, sock->server->failures);
		}
		debug_sock(sock, "receive completed, waiting for next receive");
	}

	debug_sock(sock, "receive work queue is stopped");
}


/**
 * dnbd3_receive_worker - sends a keepalive
 * @work: the work used to get the dndb3_sock
 */
static void dnbd3_keepalive_worker(struct work_struct *work)
{
	struct dnbd3_sock *sock;
	sock = container_of(work, struct dnbd3_sock, keepalive_worker);
	if (sock->server != NULL && !sock->panic) {
		debug_sock(sock, "starting keepalive worker");
		dnbd3_send_request_cmd(sock, CMD_KEEPALIVE);
	}
}

/**
 * dnbd3_compare_servers - comparator for the server
 * @lhs: left hand sign
 * @rhs: right hand sign
 */
static int dnbd3_compare_servers(const void *lhs, const void *rhs) {
	uint64_t l, r;
	struct dnbd3_server *lhs_server = *((struct dnbd3_server **) lhs);
	struct dnbd3_server *rhs_server = *((struct dnbd3_server **) rhs);

	l = lhs_server->host.type != 0 ? lhs_server->avg_rtt
			: RTT_UNREACHABLE;
	r = rhs_server->host.type != 0 ? rhs_server->avg_rtt
			: RTT_UNREACHABLE;
	if (l < r) {
		return -1;
	} else if (l > r) {
		return 1;
	}
	return 0;
}

/**
 * dnbd3_sort_server - sort the alt server according to their avg rtt
 * @dev: the dndb3 device
 *
 * the returned array has to be freed with kfree
 */
static struct dnbd3_server **dnbd3_sort_server(struct dnbd3_device *dev) {
	int i;
	struct dnbd3_server **sorted_servers = kmalloc(NUMBER_SERVERS * sizeof(struct dnbd3_device *), GFP_KERNEL);
	if (!sorted_servers) {
		return NULL;
	}

	for (i = 0; i < NUMBER_SERVERS; i++) {
		sorted_servers[i] = &dev->alt_servers[i];
	}
	sort(sorted_servers, NUMBER_SERVERS, sizeof(struct dnbd3_device *),
			&dnbd3_compare_servers, NULL);

	return sorted_servers;
}

static int dnbd3_panic_connect(struct dnbd3_device *dev)
{
	struct dnbd3_server *working = NULL;
	int i;
	for (i = 0; i < NUMBER_CONNECTIONS; i++) {
		if (dnbd3_is_sock_alive(dev->socks[i])) {
			working = dev->socks[i].server;
		}
	}
	if (working == NULL) {
		for (i = 0; i < NUMBER_SERVERS; i++) {
			if (!dnbd3_socket_connect(&dev->socks[0],
					&dev->alt_servers[i])) {
				working = &dev->alt_servers[i];
			}
		}
	}
	if (working == NULL) {
		return -EIO;
	}
	for (i = 0; i < NUMBER_CONNECTIONS; i++) {
		if (dev->socks[i].server != working) {
			dnbd3_socket_connect(&dev->socks[i], working);
		}
	}
	return 0;
}

/**
 * dnbd3_adjust_connections - create a connection plan and connect
 * @dev: the dnbd3 device
 *
 * 1. sort the alt server after the avg rtt
 * 2. create a connection plan
 * 3. connect the plan
 */
static int dnbd3_adjust_connections(struct dnbd3_device *dev) {
	int i, j, fallback;
	struct dnbd3_server *plan[NUMBER_CONNECTIONS];
	struct dnbd3_server **servers = dnbd3_sort_server(dev);
//TODO don't connect to anyting bader then rtt unknown
	if (servers && servers[0]->host.type != 0) {
		plan[0] = servers[0];
		fallback = 0;
		j = 1;
		debug_dev(dev, "connection plan:");
		debug_server(dev, plan[0], "server 0 with rtt %llu:",
				plan[0]->avg_rtt);
		for (i = 1; i < NUMBER_CONNECTIONS; i++) {
			if (servers[j]->host.type != 0 &&
					servers[j]->avg_rtt < RTT_UNKNOWN) {
				if (RTT_FACTOR(plan[i - 1]->avg_rtt) >
						servers[j]->avg_rtt) {
					plan[i] = servers[j];
					j++;
				} else {
					plan[i] = plan[fallback];
					fallback++;
				}
			} else {
				plan[i] = plan[fallback];
				fallback++;
			}
			debug_server(dev, plan[i], "server %d with rtt %llu:",
					i, plan[i]->avg_rtt);
		}
		kfree(servers);
		for (i = 0; i < NUMBER_CONNECTIONS; i++) {
			if (plan[i] != dev->socks[i].server) {
				if (dnbd3_is_sock_alive(dev->socks[i])) {
					dnbd3_socket_disconnect(&dev->socks[i]);
				}
				j = dnbd3_socket_connect(&dev->socks[i], plan[i]);
				if (j) {
					return j;
				}
			}
		}
		return 0;
	} else { /* there is nothing to connect */
		if (servers) {
			kfree(servers);
		}
		return -ENONET;
	}


}

/**
 * dnbd3_panic_worker - handle panicked sockets
 * @work: the work used to get the dndb3_device
 *
 * 1. disconnect panicked socket
 * 2. reconnect to good alternative
 * 3. if no socket is connected do a panic_connect
 */
static void dnbd3_panic_worker(struct work_struct *work)
{
	struct dnbd3_device *dev;
	bool panic = false;
	int i;
	int sock_alive = 0;
	dev = container_of(work, struct dnbd3_device, panic_worker);
	for (i = 0; i < NUMBER_CONNECTIONS; i++) {
		if (dev->socks[i].panic) {
			panic = true;
			dnbd3_set_rtt_unreachable(dev->socks[i].server);
			dnbd3_socket_disconnect(&dev->socks[i]);

		} else if (dnbd3_is_sock_alive(dev->socks[i])) {
			sock_alive++;
		}
	}


	if (panic) {
		warn_dev(dev, "panicked, connections still alive %d",
				sock_alive);

		mutex_lock(&dev->device_lock);
		if (dnbd3_adjust_connections(dev)) {
			if (dnbd3_panic_connect(dev)) {
				error_dev(dev, "failed to connect to any server");
				dev->connected = false;
			}

		}
		mutex_unlock(&dev->device_lock);
	}
}

/**
 * dnbd3_meassure_rtt - meassure the rtt of a server
 * @dev: the device this server belongs to
 * @server: the server to meassure
 */
static int dnbd3_meassure_rtt(struct dnbd3_device *dev,
		struct dnbd3_server *server)
{
	struct timeval start, end;
	dnbd3_reply_t reply;
	struct request req;
	int result;
	uint64_t rtt = RTT_UNREACHABLE;
	struct dnbd3_sock sock = {
			.sock_nr = NUMBER_CONNECTIONS,
			.sock = NULL,
			.device = dev,
			.server = server
	};

	result = __dnbd3_socket_connect(&sock, server);
	if (result) {
		error_sock(&sock, "socket connect failed in rtt measurement");
		goto error;
	}
	dnbd3_connect_to_req(&req);
	result = dnbd3_send_request_cmd(&sock, CMD_SELECT_IMAGE);
	if (result <= 0) {
		error_sock(&sock, "request select image failed in rtt measurement");
		goto error;
	}

	result = dnbd3_receive_cmd(&sock, &reply);
	if (result <= 0) {
		error_sock(&sock, "receive select image failed in rtt measurement");
		goto error;

	}
	if (reply.magic != dnbd3_packet_magic || reply.cmd != CMD_SELECT_IMAGE
			|| reply.size < 4) {
		error_sock(&sock, "receive select image wrong header in rtt measurement");
		result = -EIO;
		goto error;
	}

	result = dnbd3_receive_cmd_select_image(&sock, &reply);
	if (result <= 0) {
		error_sock(&sock, "receive data select image failed in rtt measurement");
		goto error;
	}


	do_gettimeofday(&start);
	result = dnbd3_send_request_cmd(&sock, CMD_GET_BLOCK);
	if (result <= 0) {
		error_sock(&sock, "request test block failed in rtt measurement");
		goto error;
	}
	result = dnbd3_receive_cmd(&sock, &reply);
	if (reply.magic != dnbd3_packet_magic|| reply.cmd != CMD_GET_BLOCK
			|| reply.size != RTT_BLOCK_SIZE) {
		error_sock(&sock, "receive header cmd test block failed in rtt measurement");
		result = -EIO;
		goto error;
	}
	result =  dnbd3_receive_cmd_get_block_test(&sock, &reply);
	if (result <= 0) {
		error_sock(&sock, "receive test block failed in rtt measurement");
		goto error;
	}
	do_gettimeofday(&end); // end rtt measurement

	rtt = (uint64_t)((end.tv_sec - start.tv_sec) * 1000000ull
			+ (end.tv_usec - start.tv_usec));

	debug_sock(&sock, "new rrt is %llu", rtt);

error:
	sock.server->rtts[dev->discovery_count % 4] = rtt;
	sock.server->avg_rtt = dnbd3_average_rtt(sock.server);
	if (result <= 0) {
		server->failures++;
	}
	if (sock.sock) {
		kernel_sock_shutdown(sock.sock, SHUT_RDWR);
		sock.server = NULL;
		sock_release(sock.sock);
		sock.sock = NULL;
	}
	return result;
}


/**
 * dnbd3_merge_new_server - merge the new server into the alt server list
 * @dev: the device
 * @new_server: the new server list to merge
 */
static void dnbd3_merge_new_server(struct dnbd3_device *dev,
		dnbd3_server_entry_t *new_server)
{
	int i;
	struct dnbd3_server *existing_server, *free_server, *failed_server;
	existing_server = NULL;
	free_server = NULL;
	failed_server = NULL;

	/* find servers in alternative servers */
	for (i = 0; i < NUMBER_SERVERS; i++) {
		if ((new_server->host.type == dev->alt_servers[i].host.type)
				&& (new_server->host.port == dev->alt_servers[i].host.port)
				&& (0 == memcmp(new_server->host.addr,
				   dev->alt_servers[i].host.addr,
				   (new_server->host.type == HOST_IP4 ? 4 : 16)
				   ))) {

			existing_server = &dev->alt_servers[i];
		} else if (dev->alt_servers[i].host.type == 0) {
			free_server = &dev->alt_servers[i];
		} else if (dev->alt_servers[i].failures > 20) {
			failed_server = &dev->alt_servers[i];
		}
	}

	if (existing_server) {
		if (new_server->failures == 1) { /* remove is requested */
			info_server(dev, new_server,
					"remove server is requested");
			// adjust connection will remove it later
			existing_server->host.type = 0;
			dnbd3_set_rtt_unreachable(existing_server);
		}
//		existing_server->failures = 0; // reset failure count
		return;
	} else if (free_server) {
		//TODO disconnect the server if it is connected
		free_server->host = new_server->host;
	} else if (failed_server) {
		failed_server->host = new_server->host;
		free_server = failed_server;
	} else {
		/* no server found to replace */
		return;
	}
	info_server(dev, free_server, "got new alternative server");
	free_server->failures = 0;
	free_server->protocol_version = 0;
	dnbd3_set_rtt_unknown(free_server);
}


/**
 * dnbd3_discovery_worker - handle discovery
 * @work: the work used to get the dndb3_device
 *
 * 1. check if new servers are available and set them to alternative servers
 * 2. meassure the rtt for all available servers
 * 3. adjust the connections
 */
static void dnbd3_discovery_worker(struct work_struct *work)
{
	struct dnbd3_device *dev;
	int i;
	struct dnbd3_server *server;
	dnbd3_server_entry_t *new_server;
	dev = container_of(work, struct dnbd3_device, discovery_worker);


	debug_dev(dev, "starting discovery worker new server num is %d",
			dev->new_servers_num);

	if (dev->new_servers_num) {
		mutex_lock(&dev->device_lock);
		for (i = 0; i < dev->new_servers_num; i++) {
			new_server = &dev->new_servers[i];
			if (new_server->host.type != 0) {
				dnbd3_merge_new_server(dev, new_server);
			}
		}
		dev->new_servers_num = 0;
		mutex_unlock(&dev->device_lock);
	}
	// measure rtt for all alt servers
	for (i = 0; i < NUMBER_SERVERS; i++) {
		server = &dev->alt_servers[i];
		if (server->host.type) {
			if (dnbd3_meassure_rtt(dev, server) <= 0) {
				server->failures++;
				warn_server(dev, server,
						"failed to meassure rtt");
			}
		}
	}


	mutex_lock(&dev->device_lock);
	if (dnbd3_adjust_connections(dev)) {
		if (dnbd3_panic_connect(dev)) {
			error_dev(dev, "failed to connect to any server");
			dev->connected = false;
		}

	}
	mutex_unlock(&dev->device_lock);

	dev->discovery_count++;
}


/*
 * Connect and disconnect
 */


/**
 * __dnbd3_socket_connect - internal connect a socket to a server
 * @sock: the socket to connect
 * @server: the server
 */
static int __dnbd3_socket_connect(struct dnbd3_sock *sock,
		struct dnbd3_server *server)
{
	int result = 0;
	struct timeval timeout;

	if (server->host.port == 0 || server->host.type == 0) {
		error_sock(sock, "host or port not set");
		return -EIO;
	}
	if (sock->sock) {
		warn_sock(sock, "already connected");
		return -EIO;
	}

	timeout.tv_sec = SOCKET_TIMEOUT_CLIENT_DATA;
	timeout.tv_usec = 0;
	result = dnbd3_sock_create(server->host.type, SOCK_STREAM, IPPROTO_TCP,
			&sock->sock);
	if (result < 0) {
		error_sock(sock, "could not create socket");
		goto error;
	}

	kernel_setsockopt(sock->sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
			sizeof(timeout));
	kernel_setsockopt(sock->sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
			sizeof(timeout));
	sock->sock->sk->sk_allocation = GFP_NOIO;
	if (server->host.type == HOST_IP4) {
		struct sockaddr_in sin;
		memset(&sin, 0, sizeof(sin));
		sin.sin_family = AF_INET;
		memcpy(&(sin.sin_addr), server->host.addr, 4);
		sin.sin_port = server->host.port;
		result = kernel_connect(sock->sock, (struct sockaddr *)&sin,
				sizeof(sin), 0);
		if (result != 0) {
			error_sock(sock, "connection to host failed");
			goto error;
		}
	} else {
		struct sockaddr_in6 sin;
		memset(&sin, 0, sizeof(sin));
		sin.sin6_family = AF_INET6;
		memcpy(&(sin.sin6_addr), server->host.addr, 16);
		sin.sin6_port = server->host.port;
		result = kernel_connect(sock->sock, (struct sockaddr *)&sin,
				sizeof(sin), 0);
		if (result != 0){
			error_sock(sock, "connection to host failed");
			goto error;
		}
	}

	return 0;
error:
	if (sock->sock) {
		sock_release(sock->sock);
		sock->sock = NULL;
	}
	return result;
}

/**
 * dnbd3_socket_connect - connect a socket to a server
 * @sock: the socket
 * @server: the server to connect
 *
 * 1. connects the server to the socket
 * 2. select the image
 * 3. start receiver_worker and keepalive_worker
 */
static int dnbd3_socket_connect(struct dnbd3_sock *sock,
		struct dnbd3_server *server)
{
	int result = -EIO;
	dnbd3_reply_t reply;
	struct dnbd3_device *dev = sock->device;
	sock->server = server;

	debug_sock(sock, "socket connect");

	mutex_init(&sock->tx_lock);
	mutex_lock(&sock->tx_lock);
	result = __dnbd3_socket_connect(sock, server);
	if (result) {
		error_sock(sock, "connection to socket failed");
		mutex_unlock(&sock->tx_lock);
		result = -EIO;
		goto error;
	}
	mutex_unlock(&sock->tx_lock);

	sock->panic = false;

	if (!sock->sock) {
		error_sock(sock, "socket is not connected");
		server->failures++;
		result = -EIO;
		goto error;
	}


	result = dnbd3_send_request_cmd(sock, CMD_SELECT_IMAGE);
	if (result <= 0) {

		error_sock(sock, "connection to image %s failed", dev->imgname);
		result = -EIO;
		goto error;
	}
	result = dnbd3_receive_cmd(sock, &reply);
	if (result <= 0) {
		error_sock(sock, "receive cmd to image %s failed",
				dev->imgname);
		result = -EIO;
		goto error;
	}
	if (reply.magic != dnbd3_packet_magic || reply.cmd != CMD_SELECT_IMAGE
			|| reply.size < 4) {
		error_sock(sock, "receive select image wrong header %s",
				dev->imgname);
		result = -EIO;
		goto error;
	}
	result = dnbd3_receive_cmd_select_image(sock, &reply);
	if (result <= 0) {
		error_sock(sock, "receive cmd select image %s failed",
				dev->imgname);
		result = -EIO;
		goto error;
	}
	debug_sock(sock, "connected to image %s, filesize %llu", dev->imgname,
			dev->reported_size);

	// start the receiver
	INIT_WORK(&sock->receive_worker, dnbd3_receive_worker);
	queue_work(dnbd3_wq, &sock->receive_worker);

	INIT_WORK(&sock->keepalive_worker, dnbd3_keepalive_worker);

	/* request alternative servers receiver will handle this */
	if (dnbd3_send_request_cmd(sock, CMD_GET_SERVERS) <= 0) {
		error_sock(sock, "failed to get servers in discovery");
	}
	/* failure count is divided on each successful connect */
	server->failures = server->failures / 2;

	return 0;
error:
	server->failures++;
	sock->panic = true;
	if (sock->sock) {
		kernel_sock_shutdown(sock->sock, SHUT_RDWR);
		cancel_work_sync(&sock->receive_worker);
		sock_release(sock->sock);
		sock->sock = NULL;
	}
	mutex_destroy(&sock->tx_lock);
	return result;
}


/**
 * dnbd3_socket_disconnect - disconnect a socket
 * @sock: the socket to disconnect
 */
static int dnbd3_socket_disconnect(struct dnbd3_sock *sock)
{
	cancel_work_sync(&sock->keepalive_worker);

	debug_sock(sock, "socket disconnect");
	mutex_lock(&sock->tx_lock);

	/*
	 * Important sequence to shut down socket
	 * 1. kernel_sock_shutdown
	 *      socket shutdown, receiver which block ins socket receive
	 *      returns 0
	 * 2. cancel_work_sync(receiver)
	 *      wait for the receiver to finish, so the socket is not used
	 *      anymore
	 * 3. sock_release
	 *      release the socket and set to NULL
	 */
	if (sock->sock) {
		kernel_sock_shutdown(sock->sock, SHUT_RDWR);
	}
	mutex_unlock(&sock->tx_lock);
	mutex_destroy(&sock->tx_lock);

	cancel_work_sync(&sock->receive_worker);

	if (sock->sock) {
		sock_release(sock->sock);
		sock->sock = NULL;
	}

	sock->server = NULL;
	sock->panic = false;
	return 0;
}


/**
 * dnbd3_net_connect - connect device
 * @dev: the device to connect
 *
 * dnbd3_device.alt_servers[0] must be set
 */
int dnbd3_net_connect(struct dnbd3_device *dev)
{
	int result;
	debug_dev(dev, "connecting to server");

	if (dev->alt_servers[0].host.type == 0) {
		return -ENONET;
	}

	result = dnbd3_adjust_connections(dev);
	if (result) {
		error_dev(dev, "failed to connect to initial server");
		dnbd3_net_disconnect(dev);
		return -ENOENT;
	}
	dev->connected = true;

	debug_dev(dev, "connected, starting workers");
	INIT_WORK(&dev->discovery_worker, dnbd3_discovery_worker);
	INIT_WORK(&dev->panic_worker, dnbd3_panic_worker);
	timer_setup(&dev->timer, dnbd3_timer, 0);
	dev->timer.expires = jiffies + HZ;
	add_timer(&dev->timer);

	// alt_server[0] is the initial server
//	result = dnbd3_server_connect(dev, &dev->alt_servers[0]);
//	if (result) {
//		error_dev(dev, "failed to connect to initial server");
//		result = -ENOENT;
//		dev->imgname = NULL;
//		dev->socks[0].server = NULL;
//	}
	return result;
}

/**
 * dnbd3_net_disconnect - disconnect device
 * @dev: the device to disconnect
 */
int dnbd3_net_disconnect(struct dnbd3_device *dev)
{
	int i;
	int result = 0;

	del_timer_sync(&dev->timer);
	/* be sure it does not recover while disconnecting */
	cancel_work_sync(&dev->discovery_worker);
	cancel_work_sync(&dev->panic_worker);

	for (i = 0; i < NUMBER_CONNECTIONS; i++) {
		if (dev->socks[i].sock) {
			if (dnbd3_socket_disconnect(&dev->socks[i])) {
				result = -EIO;
			}
		}
	}
	dev->connected = false;
	return result;
}