forked from vtl/ethblk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.c
1295 lines (1114 loc) · 34.7 KB
/
target.c
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
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2019 Vitaly Mayatskikh <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2.
*
*/
#include <linux/blk-mq.h>
#include <linux/module.h>
#include <linux/version.h>
#include "ethblk.h"
#include "network.h"
#include "target.h"
#include "worker.h"
static bool target_running = false;
static struct ethblk_worker_pool *workers;
#define NET_STAT_ADD(i, var, val) \
do { \
if (i) { \
if (i->d->net_stat_enabled) { \
struct ethblk_target_disk_net_stat *dstat = \
this_cpu_ptr(i->d->stat); \
dstat->_##var += val; \
if (i->net_stat_enabled) { \
struct ethblk_target_disk_net_stat \
*is = this_cpu_ptr(i->stat); \
is->_##var += val; \
} \
} \
} \
} while (0)
#define NET_STAT_INC(i, var) NET_STAT_ADD(i, var, 1)
#define NET_STAT_GET(struc, var) \
({ \
int cpu; \
unsigned long long acc = 0; \
for_each_possible_cpu (cpu) { \
acc += (per_cpu_ptr(struc, cpu))->_##var; \
} \
acc; \
})
static struct kmem_cache *ethblk_target_cmd_cache = NULL;
static DEFINE_MUTEX(ethblk_target_disks_lock);
static struct list_head ethblk_target_disks;
static void
ethblk_target_disk_delete_initiator(struct ethblk_target_disk_ini *ini);
static void ethblk_target_disk_free_deferred(struct work_struct *w);
static struct ethblk_target_disk *ethblk_target_find_disk(unsigned short drv_id);
static void ethblk_target_disk_free(struct percpu_ref *ref);
static inline void ethblk_target_get_disk(struct ethblk_target_disk *d)
{
percpu_ref_get(&d->ref);
}
static inline void ethblk_target_put_disk(struct ethblk_target_disk *d)
{
percpu_ref_put(&d->ref);
}
static int ethblk_target_disk_net_stat_init(
struct ethblk_target_disk_net_stat __percpu **stat)
{
struct ethblk_target_disk_net_stat *st;
unsigned int i;
*stat = alloc_percpu(struct ethblk_target_disk_net_stat);
if (!*stat) {
dprintk(err, "can't alloc disk stat\n");
return -ENOMEM;
}
for_each_possible_cpu (i) {
st = per_cpu_ptr(*stat, i);
memset(st, 0, sizeof(struct ethblk_target_disk_net_stat));
}
return 0;
}
static void ethblk_target_unregister_netdevice(struct net_device *nd)
{
struct ethblk_target_disk *d, *n;
struct ethblk_target_disk_ini *ini;
/* FIXME rtnl_lock held, we are in atomic context. Oh well... */
dprintk(info, "nd %p name %s\n", nd, nd->name);
rcu_read_lock();
list_for_each_entry_safe (d, n, ðblk_target_disks, list) {
list_for_each_entry_rcu (ini, &d->initiators, list) {
if (nd == ini->nd) {
ethblk_target_disk_delete_initiator(ini);
}
}
}
rcu_read_unlock();
}
static struct ethblk_target_disk_ini *
ethblk_target_disk_initiator_lookup(struct ethblk_target_disk *d,
const char *mac, struct net_device *nd)
/* must be called under rcu lock */
{
struct ethblk_target_disk_ini *ini;
list_for_each_entry_rcu (ini, &d->initiators, list) {
if ((nd == ini->nd) && ether_addr_equal(mac, ini->mac))
goto out;
}
ini = NULL;
out:
return ini;
}
static int ethblk_target_disk_access_check(struct ethblk_target_disk *d,
const unsigned char *mac,
struct net_device *nd)
{
int ret;
rcu_read_lock();
ret = !!ethblk_target_disk_initiator_lookup(d, mac, nd);
rcu_read_unlock();
return ret;
}
static struct kobject ethblk_sysfs_target_kobj;
static int ethblk_target_disk_net_stat_dump(
char *buf, int len, struct ethblk_target_disk_net_stat __percpu *stat)
{
return snprintf(buf, len,
"rx-count %llu\ntx-count %llu\nrx-bytes %llu\n"
"tx-bytes %llu\nrx-dropped %llu\ntx-dropped %llu\n"
"err-count %llu\n",
NET_STAT_GET(stat, cnt.rx_count),
NET_STAT_GET(stat, cnt.tx_count),
NET_STAT_GET(stat, cnt.rx_bytes),
NET_STAT_GET(stat, cnt.tx_bytes),
NET_STAT_GET(stat, cnt.rx_dropped),
NET_STAT_GET(stat, cnt.tx_dropped),
NET_STAT_GET(stat, cnt.err_count));
}
static ssize_t ethblk_target_disk_ini_stat_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
struct ethblk_target_disk_ini *ini =
container_of(kobj, struct ethblk_target_disk_ini, kobj);
ssize_t ret;
ret = ethblk_target_disk_net_stat_dump(buf, PAGE_SIZE, ini->stat);
return min_t(ssize_t, (ssize_t)PAGE_SIZE, ret);
}
static struct kobj_attribute ethblk_target_disk_ini_stat_attr =
__ATTR(stat, 0440, ethblk_target_disk_ini_stat_show, NULL);
static struct attribute *ethblk_target_disk_ini_attrs[] = {
ðblk_target_disk_ini_stat_attr.attr, NULL
};
static struct attribute_group ethblk_target_disk_ini_group = {
.attrs = ethblk_target_disk_ini_attrs,
};
static struct kobj_type ethblk_target_disk_ini_kobj_type = {
.sysfs_ops = &kobj_sysfs_ops,
};
static int ethblk_target_disk_add_initiator(struct ethblk_target_disk *d,
unsigned char *mac, char *iface)
{
struct ethblk_target_disk_ini *ini;
struct net_device *nd;
int ret;
nd = dev_get_by_name(&init_net, iface);
if (!nd) {
dprintk(err, "no network interface %s\n", iface);
ret = -EINVAL;
goto err;
}
ini = kzalloc(sizeof(struct ethblk_target_disk_ini), GFP_KERNEL);
if (!ini) {
dprintk(err, "can't allocate memory for new initiator\n");
ret = -ENOMEM;
goto err_free_netdev;
}
ret = ethblk_target_disk_net_stat_init(&ini->stat);
if (ret != 0) {
dprintk(err, "can't allocate stat memory for new initiator\n");
goto err_free_ini;
}
snprintf(ini->name, sizeof(ini->name),
"%s_%02x:%02x:%02x:%02x:%02x:%02x", iface, mac[0], mac[1],
mac[2], mac[3], mac[4], mac[5]);
dprintk(debug, "disk %s creating initiator %s %p\n", d->name, ini->name,
ini);
ini->d = d;
ini->nd = nd;
ini->net_stat_enabled = d->net_stat_enabled;
ether_addr_copy(ini->mac, mac);
INIT_LIST_HEAD(&ini->list);
rcu_read_lock();
ret = !!ethblk_target_disk_initiator_lookup(d, ini->mac, nd);
rcu_read_unlock();
if (ret) {
dprintk(err, "disk %s initiator %s already exists\n", d->name,
ini->name);
ret = -EEXIST;
goto err_free_ini;
}
mutex_lock(&d->initiator_lock);
ret = kobject_init_and_add(&ini->kobj,
ðblk_target_disk_ini_kobj_type,
&d->inis_kobj, ini->name);
if (ret) {
mutex_unlock(&d->initiator_lock);
dprintk(err, "disk %s ini %s can't add kobject: %d\n", d->name,
ini->name, ret);
goto err_free_ini;
}
ret = sysfs_create_group(&ini->kobj, ðblk_target_disk_ini_group);
if (ret) {
mutex_unlock(&d->initiator_lock);
dprintk(err, "disk %s ini %s can't create sysfs: %d\n", d->name,
ini->name, ret);
goto err_free_kobject;
}
rcu_read_lock();
list_add_tail_rcu(&ini->list, &d->initiators);
rcu_read_unlock();
mutex_unlock(&d->initiator_lock);
dprintk(info, "disk %s added new initiator %s\n", d->name, ini->name);
return 0;
err_free_kobject:
kobject_del(&ini->kobj);
err_free_ini:
if (ini->stat)
free_percpu(ini->stat);
kfree(ini);
err_free_netdev:
dev_put(nd);
err:
return ret;
}
static void
ethblk_target_disk_delete_initiator(struct ethblk_target_disk_ini *ini)
{
dprintk(info, "disk %s deleting initiator %s\n", ini->d->name,
ini->name);
dev_put(ini->nd);
sysfs_remove_group(&ini->kobj, ðblk_target_disk_ini_group);
kobject_del(&ini->kobj);
list_del_rcu(&ini->list);
free_percpu(ini->stat);
kfree_rcu(ini, rcu);
dprintk(info, "disk %s initiator %s deleted\n", ini->d->name,
ini->name);
}
static int
ethblk_target_disk_find_and_delete_initiator(struct ethblk_target_disk *d,
unsigned char *mac, char *iface)
{
struct ethblk_target_disk_ini *ini;
struct net_device *nd;
int ret;
char s[ETH_ALEN * 3 + IFNAMSIZ];
snprintf(s, sizeof(s), "%s_%02x:%02x:%02x:%02x:%02x:%02x", iface,
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
dprintk(debug, "disk %s deleting initiator %s\n", d->name, s);
nd = dev_get_by_name(&init_net, iface);
if (!nd) {
dprintk(err, "no network interface %s\n", iface);
ret = -ENOENT;
goto err;
}
rcu_read_lock();
ini = ethblk_target_disk_initiator_lookup(d, mac, nd);
rcu_read_unlock();
if (!ini) {
char s[ETH_ALEN * 3];
ethblk_dump_mac(s, sizeof(s), mac);
dprintk(info, "disk %s initiator %s_%s was not found\n",
d->name, nd->name, s);
ret = -ENOENT;
goto err_nd;
}
ethblk_target_disk_delete_initiator(ini);
ret = 0;
err_nd:
dev_put(nd);
err:
return ret;
}
static void ethblk_target_disk_inis_free(struct ethblk_target_disk *d)
{
struct ethblk_target_disk_ini *ini;
mutex_lock(&d->initiator_lock);
dprintk(info, "disk %s deleting initiators\n", d->name);
list_for_each_entry_rcu (ini, &d->initiators, list) {
dprintk(info, "disk %s deleting initiator %s\n", d->name,
ini->name);
dev_put(ini->nd);
sysfs_remove_group(&ini->kobj, ðblk_target_disk_ini_group);
kobject_del(&ini->kobj);
list_del_rcu(&ini->list);
kfree_rcu(ini, rcu);
}
mutex_unlock(&d->initiator_lock);
}
static ssize_t ethblk_target_disk_backend_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
struct ethblk_target_disk *d =
container_of(kobj, struct ethblk_target_disk, kobj);
return snprintf(buf, PAGE_SIZE, "%s\n", d->backend_path);
}
static void ethblk_target_destroy_disk_deferred(struct work_struct *w)
{
struct ethblk_target_disk *d =
container_of(w, struct ethblk_target_disk, destroy_work);
dprintk(info, "disk %s\n", d->name);
percpu_ref_kill(&d->ref);
/* NOTE can't wait for completion - disk_free will be scheduled as */
/* work, and it may get added to the same work queue we currently */
/* run on */
}
static ssize_t ethblk_target_disk_destroy_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
struct ethblk_target_disk *d =
container_of(kobj, struct ethblk_target_disk, kobj);
dprintk(info, "destroying disk %s %p\n", d->name, d);
schedule_work(&d->destroy_work);
return count;
}
static ssize_t ethblk_target_disk_net_stat_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
struct ethblk_target_disk *d =
container_of(kobj, struct ethblk_target_disk, kobj);
ssize_t ret;
ret = ethblk_target_disk_net_stat_dump(buf, PAGE_SIZE, d->stat);
return min_t(ssize_t, PAGE_SIZE, ret);
}
static struct kobj_attribute ethblk_target_disk_backend_attr =
__ATTR(backend, 0440, ethblk_target_disk_backend_show, NULL);
static struct kobj_attribute ethblk_target_disk_destroy_attr =
__ATTR(destroy, 0220, NULL, ethblk_target_disk_destroy_store);
static struct kobj_attribute ethblk_target_disk_net_stat_attr =
__ATTR(stat, 0440, ethblk_target_disk_net_stat_show, NULL);
static struct attribute *ethblk_target_disk_attrs[] = {
ðblk_target_disk_backend_attr.attr,
ðblk_target_disk_destroy_attr.attr,
ðblk_target_disk_net_stat_attr.attr, NULL
};
static struct attribute_group ethblk_target_disk_group = {
.attrs = ethblk_target_disk_attrs,
};
static struct kobj_type ethblk_target_disk_kobj_type = {
.sysfs_ops = &kobj_sysfs_ops,
};
static ssize_t ethblk_target_disk_ini_add_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
struct ethblk_target_disk *d =
container_of(kobj, struct ethblk_target_disk, inis_kobj);
unsigned char mac[ETH_ALEN];
char iface[IFNAMSIZ];
char s[ETH_ALEN * 3 + 1];
int ret;
ret = sscanf(buf, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx %s",
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5],
iface);
if (ret != 7) {
dprintk(err, "can't parse (%d): %s\n", ret, buf);
return -EINVAL;
}
ret = ethblk_target_disk_add_initiator(d, mac, iface);
if (ret) {
ethblk_dump_mac(s, sizeof(s), mac);
dprintk(debug, "disk %s can't add initiator %s/%s\n", d->name,
s, iface);
count = ret;
}
return count;
}
static ssize_t ethblk_target_disk_ini_delete_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf,
size_t count)
{
struct ethblk_target_disk *d =
container_of(kobj, struct ethblk_target_disk, inis_kobj);
unsigned char mac[ETH_ALEN];
char iface[IFNAMSIZ];
char s[ETH_ALEN * 3 + 1];
int ret;
ret = sscanf(buf, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx %s",
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5],
iface);
if (ret != 7) {
dprintk(err, "can't parse (%d): %s\n", ret, buf);
return -EINVAL;
}
ret = ethblk_target_disk_find_and_delete_initiator(d, mac, iface);
if (ret) {
ethblk_dump_mac(s, sizeof(s), mac);
dprintk(debug, "disk %s can't del initiator %s_%s\n", d->name,
iface, s);
count = ret;
}
return count;
}
static struct kobj_attribute ethblk_target_disk_ini_add_attr =
__ATTR(add, 0220, NULL, ethblk_target_disk_ini_add_store);
static struct kobj_attribute ethblk_target_disk_ini_delete_attr =
__ATTR(delete, 0220, NULL, ethblk_target_disk_ini_delete_store);
static struct attribute *ethblk_target_disk_inis_attrs[] = {
ðblk_target_disk_ini_add_attr.attr,
ðblk_target_disk_ini_delete_attr.attr, NULL
};
static struct attribute_group ethblk_target_disk_inis_group = {
.attrs = ethblk_target_disk_inis_attrs,
};
static struct kobj_type ethblk_target_disk_inis_kobj_type = {
.sysfs_ops = &kobj_sysfs_ops,
};
static int ethblk_target_disk_create(unsigned short drv_id, char *path)
{
int ret = -EINVAL;
struct ethblk_target_disk *d;
struct request_queue *q;
dprintk(info, "creating disk drv_id:%d path:%s\n", drv_id, path);
/* FIXME do mutex_lock only for list_add_rcu
rest is under rcu_lock
or add empty disk struct to the list and set the flag that it is initing */
mutex_lock(ðblk_target_disks_lock);
d = ethblk_target_find_disk(drv_id);
if (d) {
dprintk(err, "disk %s already exists: %p\n", d->name, d);
ethblk_target_put_disk(d);
ret = -EEXIST;
goto out;
}
d = kzalloc(sizeof(struct ethblk_target_disk), GFP_KERNEL);
if (!d) {
dprintk(err, "can't allocate memory for disk eda%d\n", drv_id);
ret = -ENOMEM;
goto out;
}
d->drv_id = drv_id;
d->net_stat_enabled = net_stat;
INIT_LIST_HEAD(&d->list);
INIT_LIST_HEAD(&d->initiators);
INIT_WORK(&d->destroy_work, ethblk_target_destroy_disk_deferred);
INIT_WORK(&d->free_work, ethblk_target_disk_free_deferred);
ret = percpu_ref_init(&d->ref, ethblk_target_disk_free, 0, GFP_KERNEL);
if (ret) {
dprintk(err, "can't init percpu_ref: %d\n", ret);
goto out;
}
init_completion(&d->destroy_completion);
mutex_init(&d->initiator_lock);
if (ethblk_target_disk_net_stat_init(&d->stat) != 0) {
kfree(d);
d = NULL;
goto out;
}
snprintf(d->name, sizeof(d->name), "eda%d", drv_id);
d->bd = blkdev_get_by_path(path, FMODE_READ | FMODE_WRITE, NULL);
if (IS_ERR(d->bd)) {
dprintk(err, "disk %s can't open backing store %s: %ld\n",
d->name, path, PTR_ERR(d->bd));
d->bd = NULL;
goto out_err;
}
q = d->bd->bd_queue;
ret = kobject_init_and_add(&d->kobj, ðblk_target_disk_kobj_type,
ðblk_sysfs_target_kobj, d->name);
if (ret) {
dprintk(err, "disk %s can't add kobject: %d\n", d->name, ret);
goto out_err;
}
ret = sysfs_create_group(&d->kobj, ðblk_target_disk_group);
if (ret) {
dprintk(err, "disk %s can't create sysfs group: %d\n", d->name,
ret);
goto out_d_kobj;
}
ret = kobject_init_and_add(&d->inis_kobj,
ðblk_target_disk_inis_kobj_type, &d->kobj,
"initiators");
if (ret) {
dprintk(err, "disk %s can't create sysfs group: %d\n", d->name,
ret);
goto out_sysfs;
}
ret = sysfs_create_group(&d->inis_kobj, ðblk_target_disk_inis_group);
if (ret) {
dprintk(err, "disk %s can't create sysfs group for inis: %d\n",
d->name, ret);
goto out_d_inis_kobj;
}
d->backend_path = kstrdup(path, GFP_KERNEL);
list_add_tail_rcu(&d->list, ðblk_target_disks);
dprintk(info, "disk %s created\n", d->name);
ret = 0;
goto out;
out_d_inis_kobj:
kobject_del(&d->inis_kobj);
out_sysfs:
sysfs_remove_group(&d->kobj, ðblk_target_disk_group);
out_d_kobj:
kobject_del(&d->kobj);
out_err:
free_percpu(d->stat);
if (d->bd)
blkdev_put(d->bd, FMODE_READ | FMODE_WRITE);
kfree(d);
d = NULL;
out:
mutex_unlock(ðblk_target_disks_lock);
synchronize_rcu();
return ret;
};
static void ethblk_target_disk_free_deferred(struct work_struct *w)
{
struct ethblk_target_disk *d =
container_of(w, struct ethblk_target_disk, free_work);
unsigned short drv_id = d->drv_id;
dprintk(info, "freeing disk %s %p\n", d->name, d);
mutex_lock(ðblk_target_disks_lock);
sysfs_remove_group(&d->inis_kobj, ðblk_target_disk_inis_group);
kobject_del(&d->inis_kobj);
sysfs_remove_group(&d->kobj, ðblk_target_disk_group);
kobject_del(&d->kobj);
list_del_rcu(&d->list);
mutex_unlock(ðblk_target_disks_lock);
blkdev_put(d->bd, FMODE_READ | FMODE_WRITE);
ethblk_target_disk_inis_free(d);
free_percpu(d->stat);
kfree(d->backend_path);
complete(&d->destroy_completion);
kfree_rcu(d, rcu);
dprintk(info, "disk %p eda%d freed\n", d, drv_id);
}
static void ethblk_target_disk_free(struct percpu_ref *ref)
{
struct ethblk_target_disk *d =
container_of(ref, struct ethblk_target_disk, ref);
schedule_work(&d->free_work);
}
static void ethblk_target_destroy_all_disks(void)
{
struct ethblk_target_disk *d, *n;
LIST_HEAD(tmp);
mutex_lock(ðblk_target_disks_lock);
list_splice(ðblk_target_disks, &tmp);
mutex_unlock(ðblk_target_disks_lock);
list_for_each_entry_safe (d, n, &tmp, list) {
dprintk(info, "destroying %s %p\n", d->name, d);
percpu_ref_kill(&d->ref);
wait_for_completion(&d->destroy_completion);
}
}
static ssize_t ethblk_target_disk_create_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
unsigned short drv_id;
char path[256];
int ret;
ret = sscanf(buf, "%hu %s", &drv_id, path);
if (ret != 2) {
dprintk(err, "can't parse (%d): %s\n", ret, buf);
return -EINVAL;
}
ret = ethblk_target_disk_create(drv_id, path);
if (ret) {
dprintk(err, "can't create disk eda%d = %s: %d\n", drv_id,
path, ret);
count = ret;
}
return count;
}
static struct kobj_attribute ethblk_target_disk_create_attr =
__ATTR(create_disk, 0220, NULL, ethblk_target_disk_create_store);
static struct attribute *ethblk_sysfs_target_default_attrs[] = {
ðblk_target_disk_create_attr.attr, NULL
};
static struct kobj_type ethblk_sysfs_target_ktype = {
.sysfs_ops = &kobj_sysfs_ops,
.default_attrs = ethblk_sysfs_target_default_attrs,
};
static void ethblk_target_cmd_rw_complete(struct bio *);
static void ethblk_target_send_reply(unsigned long data);
static void ethblk_target_cmd_free(struct ethblk_target_cmd *cmd)
{
dprintk(debug, "cmd %p\n", cmd);
if (!cmd)
return;
if (cmd->d)
ethblk_target_put_disk(cmd->d);
if (cmd->bio)
bio_put(cmd->bio);
if (cmd->req_skb)
consume_skb(cmd->req_skb);
if (cmd->rep_skb)
consume_skb(cmd->rep_skb);
kmem_cache_free(ethblk_target_cmd_cache, cmd);
}
static void
ethblk_target_cmd_reply_fill_skb_headers(struct ethblk_target_cmd *cmd)
{
if (cmd->l3) {
struct ethhdr *eth = (struct ethhdr *)skb_mac_header(cmd->rep_skb);
struct iphdr *ip = (struct iphdr *)(eth + 1);
struct udphdr *udp = (struct udphdr *)(ip + 1);
struct ethhdr *s_eth =
(struct ethhdr *)skb_mac_header(cmd->req_skb);
struct iphdr *s_ip = (struct iphdr *)(s_eth + 1);
struct udphdr *s_udp = (struct udphdr *)(s_ip + 1);
/* make space for eth, ip and udp headers (ethblk hdr follows) */
skb_put(cmd->rep_skb, sizeof(struct ethhdr) +
sizeof(struct iphdr) +
sizeof(struct udphdr));
cmd->rep_skb->protocol = htons(ETH_P_IP);
eth->h_proto = htons(ETH_P_IP);
ip->version = 4;
ip->ihl = 5;
ip->ttl = 255;
ip->saddr = s_ip->daddr;
ip->daddr = s_ip->saddr;
ip->protocol = IPPROTO_UDP;
ip->frag_off = htons(IP_DF);
ip->id = 0;
udp->source = s_udp->dest;
udp->dest = s_udp->source;
} else {
cmd->rep_skb->protocol = htons(eth_p_type);
}
}
static void
ethblk_target_cmd_reply_finalize_skb_headers(struct ethblk_target_cmd *cmd)
{
if (cmd->l3) {
struct ethblk_hdr *rep_hdr = ethblk_network_skb_get_hdr(cmd->rep_skb);
struct ethhdr *eth = (struct ethhdr *)skb_mac_header(cmd->rep_skb);
struct iphdr *ip = (struct iphdr *)(eth + 1);
struct udphdr *udp = (struct udphdr *)(ip + 1);
ether_addr_copy(eth->h_dest, rep_hdr->dst);
ether_addr_copy(eth->h_source, rep_hdr->src);
ip->tot_len = htons(cmd->rep_skb->len - sizeof(struct ethhdr));
udp->len = htons(ntohs(ip->tot_len) - sizeof(struct iphdr));
udp->check = 0;
ip_send_check(ip);
}
}
static void ethblk_target_cmd_id(struct ethblk_target_cmd *cmd)
{
struct sk_buff *req_skb = cmd->req_skb;
struct ethblk_hdr *req_hdr = cmd->req_hdr;
struct ethblk_hdr *rep_hdr;
struct ethblk_cfg_hdr *rep_cfg_hdr;
struct sk_buff *rep_skb;
unsigned short drv_id;
struct ethblk_target_disk_net_stat *stat;
int len = ETH_ZLEN + 512;
drv_id = be16_to_cpu(req_hdr->drv_id);
cmd->d = ethblk_target_find_disk(drv_id);
if (!cmd->d) {
dprintk_ratelimit(err, "unknown drv_id %d\n", drv_id);
return;
}
dprintk(debug, "%s disk ID\n", cmd->d->name);
stat = per_cpu_ptr(cmd->d->stat, smp_processor_id());
/* FIXME this is wrong, ini may slip right after rcu unlock */
rcu_read_lock();
cmd->ini =
ethblk_target_disk_initiator_lookup(cmd->d, req_hdr->src, req_skb->dev);
rcu_read_unlock();
if (!cmd->ini) {
char s[ETH_ALEN * 3 + 1];
ethblk_dump_mac(s, sizeof(s), req_hdr->src);
dprintk_ratelimit(err,
"initiator %s_%s has no access to disk %s\n",
req_skb->dev->name, s, cmd->d->name);
/* can't use NET_STAT_INC, it needs initiator */
stat->_cnt.rx_dropped++;
goto out;
}
if (cmd->l3)
len += sizeof(struct iphdr) + sizeof(struct udphdr);
rep_skb = ethblk_network_new_skb(len);
if (!rep_skb) {
dprintk(debug, "can't allocate reply skb len %d\n", len);
NET_STAT_INC(cmd->ini, cnt.rx_dropped);
goto out;
}
cmd->rep_skb = rep_skb; /* fill/finalize_skb_headers need it*/
ethblk_target_cmd_reply_fill_skb_headers(cmd);
rep_hdr = ethblk_network_skb_get_hdr(rep_skb);
skb_put(rep_skb, sizeof(struct ethblk_hdr) + sizeof(struct ethblk_cfg_hdr));
ether_addr_copy(rep_hdr->src, req_skb->dev->dev_addr);
ether_addr_copy(rep_hdr->dst, req_hdr->src);
rep_hdr->type = cpu_to_be16(eth_p_type);
rep_hdr->version = ETHBLK_PROTO_VERSION;
rep_hdr->response = 1;
rep_hdr->status = 0;
rep_hdr->drv_id = cpu_to_be16(cmd->d->drv_id);
rep_hdr->tag = req_hdr->tag;
rep_hdr->op = req_hdr->op;
rep_hdr->lba = req_hdr->lba;
rep_hdr->num_sectors = req_hdr->num_sectors;
rep_skb->dev = req_skb->dev;
rep_cfg_hdr = (struct ethblk_cfg_hdr *)(rep_hdr + 1);
rep_cfg_hdr->q_depth = cpu_to_be16(cmd->d->bd->bd_queue->queue_depth);
rep_cfg_hdr->num_queues = cpu_to_be16(1); /* FIXME */
rep_cfg_hdr->num_sectors = cpu_to_be64(i_size_read(cmd->d->bd->bd_inode) >> 9); /* FIXME send disk size and uuid */
NET_STAT_INC(cmd->ini, cnt.rx_count);
ethblk_target_cmd_reply_finalize_skb_headers(cmd);
cmd->rep_skb = NULL; /* to avoid skb_clone */
if (ethblk_network_xmit_skb(rep_skb) == NET_XMIT_DROP)
NET_STAT_INC(cmd->ini, cnt.tx_count);
else
NET_STAT_INC(cmd->ini, cnt.tx_dropped);
out:
ethblk_target_cmd_free(cmd);
}
static void ethblk_target_cmd_rw(struct ethblk_target_cmd *cmd)
{
ssize_t rep_skb_alloc_len;
struct ethblk_hdr *req_hdr;
struct ethblk_hdr *rep_hdr;
struct sk_buff *rep_skb;
struct ethblk_target_disk *d;
unsigned long lba;
int len;
unsigned short drv_id;
struct request_queue *q;
struct bio *bio;
bool write;
struct scatterlist sgl[16];
int sgn;
struct sk_buff *req_skb = cmd->req_skb;
struct ethblk_target_disk_net_stat *stat;
struct sk_buff *bio_skb;
int offset;
req_hdr = cmd->req_hdr;
drv_id = be16_to_cpu(req_hdr->drv_id);
cmd->d = d = ethblk_target_find_disk(drv_id);
if (!d) {
dprintk_ratelimit(err, "unknown drv_id %d\n", drv_id);
goto out;
}
stat = per_cpu_ptr(d->stat, smp_processor_id());
/* FIXME this is wrong, ini may slip right after rcu unlock */
rcu_read_lock();
cmd->ini = ethblk_target_disk_initiator_lookup(d, req_hdr->src, req_skb->dev);
rcu_read_unlock();
if (!cmd->ini) {
char s[ETH_ALEN * 3 + 1];
ethblk_dump_mac(s, sizeof(s), req_hdr->src);
dprintk_ratelimit(err,
"initiator %s/%s has no access to disk %s\n",
s, req_skb->dev->name, d->name);
goto out_drop;
}
q = bdev_get_queue(d->bd);
if (IS_ERR(q)) {
dprintk(err, "disk %s can't get queue: %ld\n", d->name,
PTR_ERR(q));
goto out_drop;
}
write = (req_hdr->op == ETHBLK_OP_WRITE);
lba = be64_to_cpu(req_hdr->lba);
len = req_hdr->num_sectors << 9;
rep_skb_alloc_len = sizeof(struct ethblk_hdr);
if (cmd->l3)
rep_skb_alloc_len += sizeof(struct ethhdr) + sizeof(struct iphdr) +
sizeof(struct udphdr);
rep_skb = ethblk_network_new_skb_with_payload(rep_skb_alloc_len,
(write ? 0 : len));
if (!rep_skb) {
dprintk(err, "can't allocate %ld bytes for rep_skb\n",
rep_skb_alloc_len);
goto out_drop;
}
cmd->rep_skb = rep_skb;
ethblk_target_cmd_reply_fill_skb_headers(cmd);
rep_hdr = ethblk_network_skb_get_hdr(rep_skb);
skb_put(rep_skb, sizeof(struct ethblk_hdr));
ether_addr_copy(rep_hdr->src, req_skb->dev->dev_addr);
ether_addr_copy(rep_hdr->dst, req_hdr->src);
rep_hdr->type = cpu_to_be16(eth_p_type);
rep_hdr->version = ETHBLK_PROTO_VERSION;
rep_hdr->response = 1;
rep_hdr->status = 0;
rep_hdr->drv_id = req_hdr->drv_id;
rep_hdr->tag = req_hdr->tag;
rep_hdr->op = req_hdr->op;
rep_hdr->lba = req_hdr->lba;
rep_hdr->num_sectors = req_hdr->num_sectors;
rep_skb->dev = req_skb->dev;
dprintk(debug, "req_skb %p rep_skb %p tag %u disk %s op %s lba %ld len %d\n",
req_skb, rep_skb, be32_to_cpu(req_hdr->tag), d->name,
req_hdr->op == ETHBLK_OP_READ ? "READ" : "WRITE", lba,
len);
NET_STAT_INC(cmd->ini, cnt.rx_count);
NET_STAT_ADD(cmd->ini, cnt.rx_bytes, write ? len : 0);
if (write) {
bio_skb = req_skb;
} else {
bio_skb = rep_skb;
}
sg_init_table(sgl, ARRAY_SIZE(sgl));
offset = (unsigned char *)ethblk_network_skb_get_payload(bio_skb) -
skb_mac_header(bio_skb);
sgn = skb_to_sgvec(bio_skb, sgl, offset, len);
if (sgn < 0) {
dprintk(err, "skb %p skb_to_sgvec failed: %d\n", bio_skb, sgn);
goto out_err;
}
bio = bio_alloc(GFP_ATOMIC, sgn);
if (bio) {
int i, ret;
struct scatterlist *sg;
for_each_sg (sgl, sg, sgn, i) {
ret = bio_add_page(bio, sg_page(sg), sg->length,
sg->offset);
dprintk(debug,
"skb %p bio %p bio_add_page[%d/%d] "
"page %p length %d offset %d: %d\n",
bio_skb, bio, i, sgn, sg_page(sg), sg->length,
sg->offset, ret);
if (ret != sg->length) {
dprintk(err,
"skb %p bio %p can't add "
"page[%d/%d] %p length %d "
"offset %d: %d\n",
bio_skb, bio, i, sgn, sg_page(sg),
sg->length, sg->offset, ret);
bio_put(bio);
bio = NULL;
goto out_err;
}
}
} else {
dprintk_ratelimit(err, "disk %s can't alloc bio\n", d->name);
goto out_err;
}
/* add payload for READ reply skb */
if (!write) {
rep_skb->data_len = len;
rep_skb->len += len;
}
bio->bi_iter.bi_sector = lba;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 14, 0)
bio->bi_bdev = d->bd;
#else
bio->bi_disk = d->bd->bd_disk;
#endif
bio_set_op_attrs(bio, write ? REQ_OP_WRITE : REQ_OP_READ,
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
(queue_is_rq_based(q) ? REQ_NOWAIT : 0) |
#endif
(write ? REQ_SYNC | REQ_IDLE : 0));
bio->bi_end_io = ethblk_target_cmd_rw_complete;
bio->bi_private = cmd;
cmd->bio = bio;
submit_bio(bio);