forked from vtl/ethblk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initiator.c
2569 lines (2223 loc) · 67.4 KB
/
initiator.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/hdreg.h>
#include <linux/idr.h>
#include <linux/init.h>
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/version.h>
#include "ethblk.h"
#include "initiator.h"
#include "network.h"
#define FIX_Q_USAGE_COUNTER
static bool initiator_running = false;
static int lat_stat = 0;
module_param(lat_stat, int, 0644);
MODULE_PARM_DESC(lat_stat, "Enable per-disk/target latency stats");
static int num_hw_queues = 0;
module_param(num_hw_queues, int, 0644);
MODULE_PARM_DESC(num_hw_queues,
"Number of hardware queues (num_online_cpus by default)");
unsigned int eth_p_type = 0x88aa; // FIXME
module_param(eth_p_type, int, 0644);
MODULE_PARM_DESC(eth_p_type, "Ethernet packet type");
static int disk_major = 153;
module_param(disk_major, int, 0644);
MODULE_PARM_DESC(disk_major, "Disk major number (153 by default)");
static int lat_hist_buckets = 64;
static int lat_hist_start = 1000;
static int lat_hist_bucket_size = 1000;
static int lat_hist_bucket_grow_factor = 100;
static int queue_depth = 128;
module_param(queue_depth, int, 0644);
MODULE_PARM_DESC(queue_depth, "Initiator disk queue_depth (128 by default)");
#define CMD_TAG_MASK 0x7fff
#define TAINT_SCORN (1 > (queue_depth / 4) ? 1 : (queue_depth / 4))
#define TAINT_RELAX 10000
static DEFINE_MUTEX(ethblk_initiator_disks_lock);
static struct list_head ethblk_initiator_disks;
static struct ethblk_initiator_tgt *
ethblk_initiator_disk_add_target(struct ethblk_initiator_disk *d,
unsigned char *addr, struct net_device *nd,
bool l3);
static int ethblk_initiator_disk_remove_target(struct ethblk_initiator_tgt *t);
static void ethblk_initiator_disk_send_id(struct ethblk_initiator_disk *d);
#define NET_STAT_ADD(t, var, val) \
do { \
if (t) { \
if (t->d->net_stat_enabled) { \
struct ethblk_initiator_net_stat *dstat = \
this_cpu_ptr(t->d->stat); \
dstat->_##var += val; \
if (t->net_stat_enabled) { \
struct ethblk_initiator_net_stat *ts = \
this_cpu_ptr(t->stat); \
ts->_##var += val; \
} \
} \
} \
} while (0)
#define NET_STAT_INC(t, var) NET_STAT_ADD(t, 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 kobject ethblk_sysfs_initiator_kobj;
#define N_DEVS ((1U << MINORBITS) / ETHBLK_PARTITIONS)
static DEFINE_IDA(ethblk_used_minors);
static long ethblk_initiator_alloc_minor(void)
__must_hold(ðblk_initiator_disks_lock)
{
int ret;
ret = ida_simple_get(ðblk_used_minors, 0, N_DEVS, GFP_KERNEL);
if (ret < 0)
return ret;
return ret * ETHBLK_PARTITIONS;
}
static void ethblk_initiator_free_minor(long minor)
__must_hold(ðblk_initiator_disks_lock)
{
minor /= ETHBLK_PARTITIONS;
WARN_ON(minor >= N_DEVS);
ida_simple_remove(ðblk_used_minors, minor);
}
static void
ethblk_initiator_cmd_dump_to_string(struct ethblk_initiator_cmd *cmd, char *ptr,
int n)
{
struct request *req = blk_mq_rq_from_pdu(cmd);
char *req_name;
int ret;
bool has_lba = false;
switch (req_op(req)) {
case REQ_OP_READ:
req_name = "READ";
has_lba = true;
break;
case REQ_OP_WRITE:
req_name = "WRITE";
has_lba = true;
break;
case REQ_OP_DRV_IN:
req_name = "PRIVATE";
break;
default:
req_name = "???";
break;
}
ret = snprintf(ptr, n,
"cmd[%d] %p req %p t %p hctx_idx %d gen_id %lu "
"retries %d disk %s op %d (%s)",
cmd->id, cmd, req, cmd->t, cmd->hctx_idx, cmd->gen_id,
cmd->retries, cmd->d->name, req_op(req), req_name);
if (has_lba) {
snprintf(ptr + ret, n - ret, " lba %ld len %u", blk_rq_pos(req),
blk_rq_bytes(req));
}
}
#define dynamic_pr_debug1(descriptor, fmt, ...) \
do { \
__dynamic_pr_debug(descriptor, pr_fmt(fmt), ##__VA_ARGS__); \
} while (0)
#define DEBUG_INI_CMD(level, cmd, fmt, arg...) \
do { \
int pid = task_pid_nr(current); \
int cpu = smp_processor_id(); \
char *__buf__##__line__ = &log_buf[cpu * LOG_ENTRY_SIZE]; \
char __attribute__((unused)) _debug, _err, _info; \
if (&_##level == &_debug) { \
DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
if (DYNAMIC_DEBUG_BRANCH(descriptor)) { \
ethblk_initiator_cmd_dump_to_string( \
cmd, __buf__##__line__, \
LOG_ENTRY_SIZE - 1); \
dynamic_pr_debug1(&descriptor, \
"%s[%s pid:%d cpu:%d] " fmt \
": %s\n", \
__func__, current->comm, \
pid, cpu, ##arg, \
__buf__##__line__); \
} \
} else { \
ethblk_initiator_cmd_dump_to_string( \
cmd, __buf__##__line__, LOG_ENTRY_SIZE - 1); \
pr_##level("%s[%s pid:%d cpu:%d] " fmt ": %s\n", \
__func__, current->comm, pid, cpu, ##arg, \
__buf__##__line__); \
} \
} while (0)
static int ethblk_initiator_net_stat_prepare_latency_hist(
struct ethblk_initiator_net_stat *stat, unsigned lat_hist_buckets,
unsigned lat_hist_start, unsigned lat_hist_bucket_size,
unsigned lat_hist_bucket_grow_factor)
{
uint64_t p, b;
int i;
if (lat_hist_buckets == 0 || lat_hist_buckets > LAT_BUCKETS)
return -EINVAL;
if (lat_hist_start == 0)
return -EINVAL;
if (lat_hist_bucket_size == 0)
return -EINVAL;
if (lat_hist_bucket_grow_factor < 100 ||
lat_hist_bucket_grow_factor > 200)
return -EINVAL;
stat->lat_hist_buckets = lat_hist_buckets;
stat->lat_hist_start = lat_hist_start;
stat->lat_hist_bucket_size = lat_hist_bucket_size;
stat->lat_hist_bucket_grow_factor = lat_hist_bucket_grow_factor;
b = stat->lat_hist_bucket_size;
for (i = 0, p = stat->lat_hist_start; i < stat->lat_hist_buckets; i++) {
stat->_lat.hist_idx[i] = p;
p += b;
b *= stat->lat_hist_bucket_grow_factor;
do_div(b, 100);
}
return 0;
}
static void
ethblk_initiator_net_stat_clear(struct ethblk_initiator_net_stat __percpu *stat)
{
int cpu;
for_each_possible_cpu (cpu) {
memset(&per_cpu_ptr(stat, cpu)->_cnt, 0, sizeof(stat->_cnt));
memset(&per_cpu_ptr(stat, cpu)->_lat, 0, sizeof(stat->_lat));
}
}
static void ethblk_initiator_disk_tgt_stat_clear(struct ethblk_initiator_tgt *t)
{
dprintk(info, "t %s %p\n", t->name, t);
ethblk_initiator_net_stat_clear(t->stat);
}
static int ethblk_initiator_disk_tgt_stat_hist_init(
struct ethblk_initiator_tgt *t, int lat_hist_buckets,
int lat_hist_start, int lat_hist_bucket_size,
int lat_hist_bucket_grow_factor)
{
int cpu;
dprintk(info, "%s %u %u %u %u\n", t->name, lat_hist_buckets,
lat_hist_start, lat_hist_bucket_size,
lat_hist_bucket_grow_factor);
for_each_possible_cpu (cpu) {
if (ethblk_initiator_net_stat_prepare_latency_hist(
per_cpu_ptr(t->stat, cpu), lat_hist_buckets,
lat_hist_start, lat_hist_bucket_size,
lat_hist_bucket_grow_factor))
return -EINVAL;
}
return 0;
}
static void ethblk_initiator_disk_stat_clear(struct ethblk_initiator_disk *d)
{
struct ethblk_initiator_tgt *t;
struct ethblk_initiator_tgt_array *ta;
int i;
dprintk(info, "%s\n", d->name);
ethblk_initiator_net_stat_clear(d->stat);
rcu_read_lock();
ta = rcu_dereference(d->targets);
for (i = 0; i < ta->nr; i++) {
t = ta->tgts[i];
ethblk_initiator_disk_tgt_stat_clear(t);
}
rcu_read_unlock();
}
static int ethblk_initiator_disk_stat_hist_init(struct ethblk_initiator_disk *d,
int lat_hist_buckets,
int lat_hist_start,
int lat_hist_bucket_size,
int lat_hist_bucket_grow_factor)
{
struct ethblk_initiator_tgt *t;
struct ethblk_initiator_tgt_array *ta;
int i, cpu, ret;
dprintk(info, "%s %u %u %u %u\n", d->name, lat_hist_buckets,
lat_hist_start, lat_hist_bucket_size,
lat_hist_bucket_grow_factor);
for_each_possible_cpu (cpu) {
ret = ethblk_initiator_net_stat_prepare_latency_hist(
per_cpu_ptr(d->stat, cpu), lat_hist_buckets,
lat_hist_start, lat_hist_bucket_size,
lat_hist_bucket_grow_factor);
if (ret)
return ret;
}
rcu_read_lock();
ta = rcu_dereference(d->targets);
for (i = 0; i < ta->nr; i++) {
t = ta->tgts[i];
t->net_stat_enabled = d->net_stat_enabled;
t->lat_stat_enabled = d->lat_stat_enabled;
ret = ethblk_initiator_disk_tgt_stat_hist_init(
t, lat_hist_buckets, lat_hist_start,
lat_hist_bucket_size, lat_hist_bucket_grow_factor);
if (ret)
goto out;
}
ret = 0;
out:
rcu_read_unlock();
return ret;
}
static void ethblk_initiator_disk_stat_free(struct ethblk_initiator_disk *d)
{
if (d->stat)
free_percpu(d->stat);
}
static int ethblk_initiator_disk_stat_init(struct ethblk_initiator_disk *d)
{
d->stat = alloc_percpu(struct ethblk_initiator_net_stat);
if (!d->stat) {
dprintk(err, "can't alloc net stat\n");
goto err;
}
ethblk_initiator_disk_stat_clear(d);
ethblk_initiator_disk_stat_hist_init(d, lat_hist_buckets,
lat_hist_start,
lat_hist_bucket_size,
lat_hist_bucket_grow_factor);
return 0;
err:
ethblk_initiator_disk_stat_free(d);
return -ENOMEM;
}
static void ethblk_initiator_tgt_stat_free(struct ethblk_initiator_tgt *t)
{
if (t->stat)
free_percpu(t->stat);
}
static int ethblk_initiator_tgt_stat_init(struct ethblk_initiator_tgt *t)
{
int cpu;
t->stat = alloc_percpu(struct ethblk_initiator_net_stat);
if (!t->stat) {
dprintk(err, "can't alloc net stat\n");
goto err;
}
for_each_possible_cpu (cpu) {
struct ethblk_initiator_net_stat *stat =
per_cpu_ptr(t->stat, cpu);
memset(stat, 0, sizeof(struct ethblk_initiator_net_stat));
ethblk_initiator_net_stat_prepare_latency_hist(
stat, lat_hist_buckets, lat_hist_start,
lat_hist_bucket_size, lat_hist_bucket_grow_factor);
}
return 0;
err:
ethblk_initiator_tgt_stat_free(t);
return -ENOMEM;
}
static void ethblk_initiator_cmd_stat_account(struct ethblk_initiator_cmd *cmd)
{
struct request *req = blk_mq_rq_from_pdu(cmd);
struct ethblk_initiator_net_stat *stat;
unsigned long lat;
int i;
if (!cmd->t)
return;
if (cmd->status == BLK_STS_OK) {
NET_STAT_INC(cmd->t, cnt.rx_count);
if (rq_data_dir(req) == READ)
NET_STAT_ADD(cmd->t, cnt.rx_bytes, blk_rq_bytes(req));
} else {
NET_STAT_INC(cmd->t, cnt.err_count);
}
if (!cmd->t->lat_stat_enabled)
return;
stat = this_cpu_ptr(cmd->t->stat);
lat = cmd->time_completed - cmd->time_queued;
for (i = 0; i < stat->lat_hist_buckets; i++) {
if (lat <= stat->_lat.hist_idx[i])
break;
}
if (i >= stat->lat_hist_buckets)
i = stat->lat_hist_buckets - 1;
switch (req_op(req)) {
case REQ_OP_READ:
NET_STAT_ADD(cmd->t, lat.read, lat);
NET_STAT_INC(cmd->t, lat.hist_read[i]);
break;
case REQ_OP_WRITE:
NET_STAT_ADD(cmd->t, lat.write, lat);
NET_STAT_INC(cmd->t, lat.hist_write[i]);
break;
default:
DEBUG_INI_CMD(err, cmd, "unknown req op %d\n", req_op(req));
break;
}
}
static int
ethblk_initiator_net_stat_dump(char *buf, int len,
struct ethblk_initiator_net_stat __percpu *stat)
{
int i, ret = 0;
unsigned long long tmp;
ret = snprintf(buf, len,
"rx-count %llu\ntx-count %llu\nrx-bytes %llu\n"
"tx-bytes %llu\ntx-dropped %llu\nerr-count %llu\n"
"tx-retry-count %llu\nrx-late-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.tx_dropped),
NET_STAT_GET(stat, cnt.err_count),
NET_STAT_GET(stat, cnt.tx_retry_count),
NET_STAT_GET(stat, cnt.rx_late_count));
ret += snprintf(buf + ret, len - ret, "rlat-total %llu\n",
NET_STAT_GET(stat, lat.read));
ret += snprintf(buf + ret, len - ret, "wlat-total %llu\n",
NET_STAT_GET(stat, lat.write));
ret += snprintf(
buf + ret, len - ret, "rlat-avg %llu\n",
(tmp = NET_STAT_GET(stat, lat.read),
do_div(tmp, max(1ULL, NET_STAT_GET(stat, cnt.tx_count))),
tmp));
ret += snprintf(
buf + ret, len - ret, "wlat-avg %llu\n",
(tmp = NET_STAT_GET(stat, lat.write),
do_div(tmp, max(1ULL, NET_STAT_GET(stat, cnt.tx_count))),
tmp));
for (i = 0; i < this_cpu_ptr(stat)->lat_hist_buckets - 1; i++) {
ret += snprintf(buf + ret, PAGE_SIZE - ret,
"[%d] < %llu ns = r:%llu w:%llu\n", i,
this_cpu_ptr(stat)->_lat.hist_idx[i],
NET_STAT_GET(stat, lat.hist_read[i]),
NET_STAT_GET(stat, lat.hist_write[i]));
}
ret += snprintf(buf + ret, len - ret, "[%d] rest = r:%llu w:%llu\n", i,
NET_STAT_GET(stat, lat.hist_read[i]),
NET_STAT_GET(stat, lat.hist_write[i]));
return ret;
}
static ssize_t ethblk_initiator_disk_stat_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
struct ethblk_initiator_disk *d =
container_of(kobj, struct ethblk_initiator_disk, kobj);
int ret;
ret = ethblk_initiator_net_stat_dump(buf, PAGE_SIZE, d->stat);
return min((int)PAGE_SIZE, ret);
}
static ssize_t ethblk_initiator_disk_stat_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
struct ethblk_initiator_disk *d =
container_of(kobj, struct ethblk_initiator_disk, kobj);
unsigned param[4];
int ret;
ret = sscanf(buf, "%u %u %u %u\n", ¶m[0], ¶m[1], ¶m[2],
¶m[3]);
dprintk(info, "got %d params\n", ret);
switch (ret) {
case 0: /* clear counters */
ethblk_initiator_disk_stat_clear(d);
break;
case 2:
d->lat_stat_enabled = param[1];
case 1:
d->net_stat_enabled = param[0];
break;
case 4: /* set new histogram */
d->lat_stat_enabled = true;
d->net_stat_enabled = true;
ethblk_initiator_disk_stat_clear(d);
ret = ethblk_initiator_disk_stat_hist_init(
d, param[0], param[1], param[2], param[3]);
if (ret)
return ret;
break;
default:
dprintk(err, "can't parse %d params (%s)\n", ret, buf);
return -EINVAL;
}
return count;
}
static struct ethblk_initiator_tgt *
ethblk_initiator_disk_cmd_get_next_tgt(struct ethblk_initiator_cmd *cmd);
static void
ethblk_initiator_disk_remove_all_targets(struct ethblk_initiator_disk *d);
static inline void ethblk_initiator_put_disk(struct ethblk_initiator_disk *d);
struct ethblk_initiator_put_disk_struct {
struct work_struct w;
struct ethblk_initiator_disk *d;
};
static void ethblk_initiator_put_disk_work(struct work_struct *w)
{
struct ethblk_initiator_put_disk_struct *pds =
container_of(w, struct ethblk_initiator_put_disk_struct, w);
ethblk_initiator_put_disk(pds->d);
kfree(pds);
}
static void ethblk_initiator_put_disk_delayed(struct ethblk_initiator_disk *d)
{
struct ethblk_initiator_put_disk_struct *pds;
pds = kmalloc(sizeof(struct ethblk_initiator_put_disk_struct),
GFP_KERNEL);
if (!pds) {
dprintk(err, "Can't allocate memory for delayed put_disk work. "
"Trying to put disk inline (may hang)...\n");
ethblk_initiator_put_disk(d);
} else {
INIT_WORK(&pds->w, ethblk_initiator_put_disk_work);
pds->d = d;
schedule_work(&pds->w);
}
}
static ssize_t
ethblk_initiator_disk_disconnect_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
struct ethblk_initiator_disk *d =
container_of(kobj, struct ethblk_initiator_disk, kobj);
dprintk(info, "disconnecting disk %s %p\n", d->name, d);
ethblk_initiator_disk_remove_all_targets(d);
ethblk_initiator_put_disk_delayed(d);
return count;
}
static struct kobj_attribute ethblk_initiator_disk_disconnect_attr =
__ATTR(disconnect, 0220, NULL, ethblk_initiator_disk_disconnect_store);
static struct kobj_attribute ethblk_initiator_disk_stat_attr =
__ATTR(stat, 0660, ethblk_initiator_disk_stat_show,
ethblk_initiator_disk_stat_store);
static struct attribute *ethblk_initiator_disk_attrs[] = {
ðblk_initiator_disk_disconnect_attr.attr,
ðblk_initiator_disk_stat_attr.attr, NULL
};
static struct attribute_group ethblk_initiator_disk_group = {
.attrs = ethblk_initiator_disk_attrs,
};
static struct kobj_type ethblk_initiator_disk_kobj_type = {
.sysfs_ops = &kobj_sysfs_ops,
};
static ssize_t ethblk_initiator_disk_tgt_stat_show(struct kobject *kobj,
struct kobj_attribute *attr,
char *buf)
{
struct ethblk_initiator_tgt *t =
container_of(kobj, struct ethblk_initiator_tgt, kobj);
int ret;
ret = ethblk_initiator_net_stat_dump(buf, PAGE_SIZE, t->stat);
return min((int)PAGE_SIZE, ret);
}
static ssize_t ethblk_initiator_disk_tgt_stat_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf,
size_t count)
{
struct ethblk_initiator_tgt *t =
container_of(kobj, struct ethblk_initiator_tgt, kobj);
unsigned param[4];
int ret;
ret = sscanf(buf, "%u %u %u %u\n", ¶m[0], ¶m[1], ¶m[2],
¶m[3]);
dprintk(info, "got %d params\n", ret);
switch (ret) {
case 0: /* clear counters */
ethblk_initiator_disk_tgt_stat_clear(t);
break;
case 2:
t->lat_stat_enabled = param[1];
case 1:
t->net_stat_enabled = param[0];
break;
case 4: /* set new histogram */
ret = ethblk_initiator_disk_tgt_stat_hist_init(
t, param[0], param[1], param[2], param[3]);
if (ret)
return ret;
t->lat_stat_enabled = true;
t->net_stat_enabled = true;
break;
default:
dprintk(err, "can't parse %d params (%s)\n", ret, buf);
return -EINVAL;
}
return count;
}
static ssize_t
ethblk_initiator_disk_tgt_disconnect_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf, size_t count)
{
struct ethblk_initiator_tgt *t =
container_of(kobj, struct ethblk_initiator_tgt, kobj);
int ret;
dprintk(info, "disk %s disconnecting tgt %s\n", t->d->name, t->name);
ret = ethblk_initiator_disk_remove_target(t);
return ret ? ret : count;
}
static ssize_t ethblk_initiator_disk_tgts_add_store(struct kobject *kobj,
struct kobj_attribute *attr,
const char *buf,
size_t count)
{
struct ethblk_initiator_disk *d =
container_of(kobj, struct ethblk_initiator_disk, tgts_kobj);
struct ethblk_initiator_tgt *t;
struct net_device *nd;
unsigned char mac[ETH_ALEN];
int ip[4];
char iface[IFNAMSIZ];
char s[ETH_ALEN * 3 + 1];
int ret;
bool l3 = false;
int i;
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) /* L2 target */
goto create;
ret = sscanf(buf, "%u.%u.%u.%u %s", &ip[0], &ip[1], &ip[2], &ip[3],
iface);
if (ret != 5) {
dprintk(err, "can't parse (%d): %s\n", ret, buf);
return -EINVAL;
}
for (i = 0; i < 4; i++)
mac[i] = (unsigned char)ip[i];
l3 = true;
create:
nd = dev_get_by_name(&init_net, iface);
if (!nd) {
dprintk(err, "no network interface %s\n", iface);
return -EINVAL;
}
t = ethblk_initiator_disk_add_target(d, mac, nd, l3);
if (!t) {
dprintk(err, "disk %s can't add target %s_%s (see logs)\n",
d->name, iface, s);
return -EINVAL;
}
ethblk_initiator_disk_send_id(d);
return count;
}
static struct kobj_attribute ethblk_initiator_disk_tgts_add_attr =
__ATTR(add, 0220, NULL, ethblk_initiator_disk_tgts_add_store);
static struct attribute *ethblk_initiator_disk_tgts_attrs[] = {
ðblk_initiator_disk_tgts_add_attr.attr, NULL
};
static struct attribute_group ethblk_initiator_disk_tgts_group = {
.attrs = ethblk_initiator_disk_tgts_attrs,
};
static struct kobj_type ethblk_initiator_disk_tgts_kobj_type = {
.sysfs_ops = &kobj_sysfs_ops,
};
static struct kobj_attribute ethblk_initiator_disk_tgt_stat_attr =
__ATTR(stat, 0660, ethblk_initiator_disk_tgt_stat_show,
ethblk_initiator_disk_tgt_stat_store);
static struct kobj_attribute ethblk_initiator_disk_tgt_disconnect_attr = __ATTR(
disconnect, 0220, NULL, ethblk_initiator_disk_tgt_disconnect_store);
static struct attribute *ethblk_initiator_disk_tgt_attrs[] = {
ðblk_initiator_disk_tgt_stat_attr.attr,
ðblk_initiator_disk_tgt_disconnect_attr.attr, NULL
};
static struct attribute_group ethblk_initiator_disk_tgt_group = {
.attrs = ethblk_initiator_disk_tgt_attrs,
};
static struct kobj_type ethblk_initiator_disk_tgt_kobj_type = {
.sysfs_ops = &kobj_sysfs_ops,
};
static void ethblk_initiator_tgt_free(struct percpu_ref *ref);
static void ethblk_initiator_disk_free(struct kref *ref);
static inline void ethblk_initiator_get_tgt(struct ethblk_initiator_tgt *t)
{
percpu_ref_get(&t->ref);
}
static inline void ethblk_initiator_put_tgt(struct ethblk_initiator_tgt *t)
{
percpu_ref_put(&t->ref);
}
static inline void ethblk_initiator_get_disk(struct ethblk_initiator_disk *d)
{
kref_get(&d->ref);
}
static inline void ethblk_initiator_put_disk(struct ethblk_initiator_disk *d)
{
kref_put(&d->ref, ethblk_initiator_disk_free);
}
static void ethblk_initiator_disk_set_capacity_work(struct work_struct *w)
{
struct ethblk_initiator_disk *d =
container_of(w, struct ethblk_initiator_disk, cap_work);
struct block_device *bd = bdget_disk(d->gd, 0);
if (bd) {
loff_t size = (loff_t)get_capacity(d->gd) << 9;
dprintk(info, "disk %s new size is %lld bytes\n", d->name,
size);
inode_lock(bd->bd_inode);
i_size_write(bd->bd_inode, size);
inode_unlock(bd->bd_inode);
bdput(bd);
}
}
static void ethblk_initiator_disk_free(struct kref *ref)
{
struct ethblk_initiator_disk *d =
container_of(ref, struct ethblk_initiator_disk, ref);
dprintk(info, "freeing disk %s %p\n", d->name, d);
ethblk_initiator_disk_remove_all_targets(d);
mutex_lock(ðblk_initiator_disks_lock);
sysfs_remove_group(&d->kobj, ðblk_initiator_disk_group);
kobject_del(&d->kobj);
blk_mq_stop_hw_queues(d->queue);
flush_work(&d->cap_work);
ethblk_initiator_free_minor(d->gd->first_minor);
del_gendisk(d->gd);
blk_cleanup_queue(d->queue);
blk_mq_free_tag_set(&d->tag_set);
#ifdef FIX_Q_USAGE_COUNTER
/*
Deal with blk-mq IO complete/timeout race...
Switch q->usage_counter to atomic, fix it, switch back to percpu
*/
{
unsigned long __percpu *percpu_count =
(unsigned long __percpu *)(d->queue->q_usage_counter
.percpu_count_ptr &
~__PERCPU_REF_ATOMIC_DEAD);
unsigned long count = 0;
int cpu;
for_each_possible_cpu (cpu)
count += *per_cpu_ptr(percpu_count, cpu);
if (count != 0) {
dprintk(err,
"disk %s fixing q_usage_counter "
"(percpu %ld, atomic %ld)\n",
d->name,
atomic_long_read(
&d->queue->q_usage_counter.count),
(long)count);
percpu_ref_switch_to_atomic_sync(
&d->queue->q_usage_counter);
atomic_long_set(&d->queue->q_usage_counter.count, 0);
percpu_ref_switch_to_percpu(&d->queue->q_usage_counter);
}
}
#endif
put_disk(d->gd);
list_del_rcu(&d->list);
sysfs_remove_group(&d->tgts_kobj, ðblk_initiator_disk_tgts_group);
kobject_del(&d->tgts_kobj);
kfree(d->cmd);
kfree(d->ctx);
mutex_unlock(ðblk_initiator_disks_lock);
ethblk_initiator_disk_stat_free(d);
complete(&d->destroy_completion);
kfree_rcu(d, rcu);
dprintk(info, "disk %p eda%d freed\n", d, d->drv_id);
}
static int ethblk_blk_open(struct block_device *bdev, fmode_t mode)
{
struct ethblk_initiator_disk *d = bdev->bd_disk->private_data;
dprintk(info, "disk %s opened by %s\n", d->name, current->comm);
ethblk_initiator_get_disk(d);
return 0;
}
static void ethblk_blk_release(struct gendisk *disk, fmode_t mode)
{
struct ethblk_initiator_disk *d = disk->private_data;
dprintk(info, "disk %s closed by %s\n", d->name, current->comm);
ethblk_initiator_put_disk_delayed(d);
}
static int ethblk_blk_ioctl(struct block_device *bdev, fmode_t mode,
unsigned int cmd, unsigned long arg)
{
struct ethblk_initiator_disk *d;
if (!arg)
return -EINVAL;
d = bdev->bd_disk->private_data;
switch (cmd) {
case HDIO_GET_IDENTITY:
if (!copy_to_user((void __user *)arg, &d->uuid,
sizeof(d->uuid)))
return 0;
return -EFAULT;
case BLKFLSBUF:
return 0;
default:
break;
}
return -ENOTTY;
}
static int ethblk_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
{
// FIXME
/* struct ethblk_initiator_disk *d = bdev->bd_disk->private_data; */
/* geo->cylinders = d->geo.cylinders; */
/* geo->heads = d->geo.heads; */
/* geo->sectors = d->geo.sectors; */
return 0;
}
static const struct block_device_operations ethblk_bdops = {
.open = ethblk_blk_open,
.release = ethblk_blk_release,
.ioctl = ethblk_blk_ioctl,
.getgeo = ethblk_blk_getgeo,
.owner = THIS_MODULE,
};
static void
ethblk_initiator_cmd_fill_skb_headers(struct ethblk_initiator_cmd *cmd,
struct sk_buff *skb)
{
if (cmd->l3) {
struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
struct iphdr *ip = (struct iphdr *)(eth + 1);
struct udphdr *udp = (struct udphdr *)(ip + 1);
/* make space for eth, ip and udp headers (ethblk hdr follows) */
skb_put(skb, sizeof(struct ethhdr) + sizeof(struct iphdr) +
sizeof(struct udphdr));
skb->protocol = htons(ETH_P_IP);
eth->h_proto = htons(ETH_P_IP);
ip->version = 4;
ip->ihl = 5;
ip->ttl = 255;
ip->protocol = IPPROTO_UDP;
ip->frag_off = htons(IP_DF);
ip->id = 0;
/* NOTE to fool Mellanox packet steering we have to
* use semi-random source ports */
// FIXME make tunable
udp->source = htons(eth_p_type + cmd->hctx_idx);
udp->dest = htons(eth_p_type);// + cmd->hctx_idx);
} else {
skb->protocol = htons(eth_p_type);
}
}
static void
ethblk_initiator_cmd_finalize_skb_headers(struct ethblk_initiator_cmd *cmd,
struct sk_buff *skb)
{
if (cmd->l3) {
struct ethblk_hdr *h = &cmd->ethblk_hdr;
struct ethhdr *eth = eth_hdr(skb);
struct iphdr *ip = (struct iphdr *)(eth + 1);
struct udphdr *udp = (struct udphdr *)(ip + 1);
ether_addr_copy(eth->h_source, h->src);
ip->saddr = cmd->t->local_ip;
ip->daddr = cmd->t->dest_ip;
if (!cmd->t->has_router_mac) {
if (ethblk_network_route_l3(cmd->t->nd,
ip->daddr, ip->saddr,
cmd->t->router_mac) == 0)
cmd->t->has_router_mac = true;
}
if (cmd->t->has_router_mac) {
ether_addr_copy(eth->h_dest, cmd->t->router_mac);
} else {
ether_addr_copy(eth->h_dest, h->dst);
}
ip->tot_len = htons(skb->len - sizeof(struct ethhdr) +
(skb->data_len));
udp->len = htons(ntohs(ip->tot_len) - sizeof(struct iphdr));
udp->check = 0;
ip_send_check(ip);
}
}
static void ethblk_initiator_cmd_hdr_init(struct ethblk_initiator_cmd *cmd)
{
struct ethblk_initiator_disk *d = cmd->d;
struct ethblk_initiator_tgt *t = cmd->t;
struct ethblk_hdr *h = &cmd->ethblk_hdr;
unsigned int gen_id = cmd->gen_id & CMD_TAG_MASK;
unsigned int host_tag = (gen_id << 16) | cmd->id;
DEBUG_INI_CMD(debug, cmd, "host_tag = %d", host_tag);
ether_addr_copy(h->src, t->nd->dev_addr);
ether_addr_copy(h->dst, t->mac);
h->type = cpu_to_be16(eth_p_type);
h->version = ETHBLK_PROTO_VERSION;
h->status = 0;
h->response = 0;
h->drv_id = cpu_to_be16(d->drv_id);
h->tag = cpu_to_be32(host_tag);
}