forked from SoftRoCE/librxe-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rxe_cfg
executable file
·706 lines (599 loc) · 14.9 KB
/
rxe_cfg
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
#!/usr/bin/perl
# * Copyright (c) 2009-2011 Mellanox Technologies Ltd. All rights reserved.
# * Copyright (c) 2009-2011 System Fabric Works, Inc. All rights reserved.
# *
# * This software is available to you under a choice of one of two
# * licenses. You may choose to be licensed under the terms of the GNU
# * General Public License (GPL) Version 2, available from the file
# * COPYING in the main directory of this source tree, or the
# * OpenIB.org BSD license below:
# *
# * Redistribution and use in source and binary forms, with or
# * without modification, are permitted provided that the following
# * conditions are met:
# *
# * - Redistributions of source code must retain the above
# * copyright notice, this list of conditions and the following
# * disclaimer.
# *
# * - Redistributions in binary form must reproduce the above
# * copyright notice, this list of conditions and the following
# * disclaimer in the documentation and/or other materials
# * provided with the distribution.
# *
# * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# * SOFTWARE.
#
use warnings;
use strict;
use Getopt::Long;
use Switch;
my $z = "2>/dev/null";
my $help = 0;
my $no_persist = 0;
my $debug = 0;
my $force = 0;
my $linkonly = 0;
my $parms = "/sys/module/rdma_rxe/parameters";
my $modprobe_opt = "";
my $modprobe_checked = "0";
my $persistence_path = "/var/rxe";
my $persistence_file = "${persistence_path}/rxe";
my $num_persistent = 0;
my $sys = "/sys/module/rdma_rxe/parameters";
my %rxe_names;
my @rxe_array;
my %eth_names;
my @eth_list;
my %eth_driver;
my %link_state;
my %link_speed;
my %eth_mtu;
my %ipv4_addr;
my %rxe_mtu;
my @persistence_array;
my %persistence_hash;
my @mlx4_port;
my @mlx4_ether;
my @roce_list;
#get mapping between rxe and eth devices
sub get_names {
return if (!(-e "/sys/class/infiniband"));
my @list;
my $rxe;
my $eth;
my $i = 0;
@list = `ls /sys/class/infiniband | grep rxe $z`;
foreach $rxe (@list) {
chomp($rxe);
$eth = `cat /sys/class/infiniband/$rxe/parent $z`;
chomp($eth);
if (($eth =~ /[\w]+[\d]/)
&& ($rxe =~ /rxe[0123456789]/)) {
# hash ethername to rxename
$rxe_names{$eth} = $rxe;
$rxe_array[$i++] = $rxe;
# hash rxename to ethername
$eth_names{$rxe} = $eth;
}
}
}
# get list of Mellanox RoCE ports
sub get_mlx4_list {
return if (!(-e "/sys/class/infiniband"));
my @list;
my $mlx4;
my @ports;
my $port;
my $link;
my $i = 0;
@list = `ls /sys/class/infiniband | grep mlx4_ $z`;
foreach $mlx4 (@list) {
chomp($mlx4);
chdir("/sys/class/infiniband/$mlx4/ports");
@ports = `ls $z`;
foreach $port (@ports) {
chomp($port);
$link= `cat $port/link_layer $z`;
chomp($link);
if ($link =~ "Ethernet") {
$roce_list[$i++] = "$mlx4:$port";
}
}
}
}
#collect per device information
sub get_dev_info {
my @list;
my @fields;
my @lines;
my $line;
my $eth;
my $drv;
my $np;
my $i = 0;
my $j = 0;
get_mlx4_list();
my @my_eth_list = ();
my @my_eth_devs = `ls /sys/class/net`;
foreach my $my_eth_dev (@my_eth_devs) {
chomp($my_eth_dev);
my $my_dev_type = `cat /sys/class/net/${my_eth_dev}/type`;
if ($my_dev_type == "1") {
push(@my_eth_list, "$my_eth_dev");
}
}
@list = @my_eth_list;
foreach $eth (@list) {
chomp($eth);
$eth_list[$i++] = $eth;
@lines = `ethtool -i $eth $z`;
foreach $line (@lines) {
chomp($line);
@fields = split(/\s+/, $line);
chomp($fields[0]);
if ($fields[0] =~ /driver:/) {
$drv = $fields[1];
$eth_driver{$eth} = $drv;
if ($drv =~ /mlx4_en/ && scalar(@roce_list) > 0 ) {
$eth_names{$roce_list[$j++]} = $eth;
}
}
}
# get link status
$link_state{$eth} = "";
$link_speed{$eth} = "";
@lines = `ethtool $eth $z`;
foreach $line (@lines) {
chomp($line);
@fields = split(/:/, $line);
if (defined($fields[1])) {
$fields[1] =~ s/^\s+//g;
if ($fields[0] =~ "Link detected") {
$link_state{$eth} = $fields[1];
}
}
elsif ($line =~ "10000baseT") {
$link_speed{$eth} = "10GigE";
}
}
$ipv4_addr{$eth} = " ";
$eth_mtu{$eth} = "";
@lines = `ifconfig $eth $z`;
foreach $line (@lines) {
# get IP address
if ($line =~ /inet addr/) {
$line =~ s/^\s+inet addr://g;
@fields = split(/\s+/, $line);
$ipv4_addr{$eth} = $fields[0];
}
# get ethernet mtu
if ($line =~ /MTU:/) {
$line =~ s/^.*MTU://g;
@fields = split(/\s+/, $line);
$eth_mtu{$eth} = $fields[0];
}
}
}
# get rxe mtu
foreach my $rxe (@rxe_array) {
@lines = `ibv_devinfo -d $rxe $z`;
foreach $line (@lines) {
if ($line =~ "active_mtu") {
$line =~ s/^\s+active_mtu:\s+//g;
chomp($line);
$rxe_mtu{$rxe} = $line;
}
}
}
}
# return string or the string "###" if string is all whitespace
sub set_field {
my $fld = $_[0];
if (defined($fld) && $fld =~ /\S/) {
return $fld;
} else {
return "###";
}
}
# format status output into fixed width columns
sub status_print {
my @fields;
my $field;
my @flen = ();
my $num_fields = 0;
my $i;
my $j;
my $pad;
my $line;
# one pass to size the columns
foreach $line (@_) {
@fields = split(/\s+/, $line);
$i = 0;
foreach $field (@fields) {
if (!defined($flen[$i])) {
$flen[$i] = length($field);
}
else {
$flen[$i] = max($flen[$i], length($field));
}
$i++;
}
if ($i > $num_fields) {
$num_fields = $i;
}
}
# one pass to print
foreach $line (@_) {
print " ";
@fields = split(/\s+/, $line);
for ($i = 0; $i < $num_fields; $i++) {
if (defined($fields[$i])) {
$pad = $flen[$i] - length($fields[$i]) + 2;
}
else {
$pad = $flen[$i] + 2;
}
if (defined($fields[$i]) && ($fields[$i] ne "###")) {
print "$fields[$i]";
}
else {
print " ";
}
for ($j = 0; $j < $pad; $j++) {
print " ";
}
}
print "\n";
}
}
# check driver load status
sub check_module_status {
if (-e $sys) {
return 0;
} else {
return 1;
}
}
# print driver load status and ethertype for rdma_rxe and rdma_rxe_net
sub show_module_status {
my $rxe_loaded = 1;
if (!(-e $sys)) {
$rxe_loaded = 0;
}
if ($rxe_loaded) {
return 0;
}
elsif (!$rxe_loaded) {
print "rxe modules not loaded\n";
return 1;
}
else {
print "Configuration does not make sense\n";
return 1;
}
}
# print rxe status
sub do_status {
my $instance = $_[0];
my $ln = 0;
my @outp;
my $rxe;
my $rmtu;
get_names();
get_dev_info();
show_module_status();
$outp[$ln++] = "Name\tLink\tDriver\t\tSpeed\tNMTU\tIPv4_addr\tRDEV\tRMTU";
foreach my $eth (@eth_list) {
# handle case where rxe_drivers are not loaded
if (defined($rxe_names{$eth})) {
$rxe = $rxe_names{$eth};
$rmtu = $rxe_mtu{$rxe};
}
else {
$rxe = "";
$rmtu = "";
}
if ((!defined($instance)
&& (($linkonly == 0) || ($link_state{$eth} =~ "yes")))
|| (defined($instance) && ($rxe =~ "$instance"))) {
$outp[$ln] = set_field("$eth");
$outp[$ln] .= "\t";
$outp[$ln] .= set_field("$link_state{$eth}");
$outp[$ln] .= "\t";
$outp[$ln] .= set_field("$eth_driver{$eth}");
$outp[$ln] .= "\t";
$outp[$ln] .= set_field("$link_speed{$eth}");
$outp[$ln] .= "\t";
$outp[$ln] .= set_field("$eth_mtu{$eth}");
$outp[$ln] .= "\t";
$outp[$ln] .= set_field("$ipv4_addr{$eth}");
$outp[$ln] .= "\t";
$outp[$ln] .= set_field("$rxe");
$outp[$ln] .= "\t";
$outp[$ln] .= set_field("$rmtu");
$ln++;
}
}
status_print(@outp);
}
# read file containing list of ethernet devices into a list
sub populate_persistence {
my @lines = `cat $persistence_file $z`;
my $line;
my $i = 0;
foreach $line (@lines) {
chomp($line);
$line =~ s/^\s+//g;
if ($line =~ /[\w]+[\d]/) {
# in case we add fields later
my ($eth, $cruft) = split(/\s+/, $line, 2);
if ($eth =~ /^[\w]+[\d]/) {
$persistence_array[$i] = $eth;
$persistence_hash{$eth} = $i++;
}
}
}
$num_persistent = $i;
}
# print out list of ethernet devices to file
sub commit_persistent {
my $i;
my $eth;
open(PF, ">$persistence_file");
for ($i = 0; $i < $num_persistent; $i++) {
$eth = $persistence_array[$i];
if ($eth =~ /[\w]+[\d]/) {
print(PF "$persistence_array[$i]\n");
}
}
close(PF);
}
sub delete_persistent {
my $eth = $_[0];
if (defined($persistence_hash{$eth})) {
$persistence_array[$persistence_hash{$eth}] = "";
}
}
sub add_persistent {
my $eth = $_[0];
# Is this one already in the persistence list?
if (!defined($persistence_hash{$eth})) {
$persistence_array[$num_persistent] = $eth;
$persistence_hash{$eth} = $num_persistent;
$num_persistent++;
}
}
# add new rxe device to eth if not already up
sub rxe_add {
my $eth = $_[0];
if (!($eth =~ /[\w]+[\d]/)) {
print "eth_name ($eth) looks bogus\n";
return;
}
if (!defined($rxe_names{$eth})) {
system("echo '$eth' > $parms/add");
}
if (!$no_persist) {
add_persistent($eth);
commit_persistent();
}
}
sub rxe_remove {
my $arg2 = $_[0];
my $rxe;
my $eth;
print "remove $arg2\n" if ($debug > 0);
if ($arg2 =~ /[\w]+[\d]/) {
$eth = $arg2;
$rxe = $rxe_names{$eth};
}
elsif ($arg2 =~ /rxe[0123456789]/) {
$rxe = $arg2;
$eth = $eth_names{$rxe};
}
elsif ($arg2 eq "all") {
$rxe = "all";
}
if (($rxe eq "all") || ($rxe =~ /^rxe[0123456789]/)) {
my $cmd = "echo '$rxe' > $parms/remove";
#print "$cmd\n";
system($cmd);
if (!$no_persist) {
if ($rxe eq "all") {
unlink($persistence_file);
}
elsif ($eth =~/[\w]+[\d]/) {
delete_persistent($eth);
commit_persistent();
}
else {
print "Warning: Unable to resolve ethname; "
. "instance may persist on restart\n";
}
}
}
else {
print "rxe instance $rxe not found\n";
}
}
sub get_devinfo {
my $rxe = $_[0];
my $cmd = "ibv_devinfo -d $rxe";
return `$cmd $z`;
}
# allow unsupported modules to load in SLES11 if allowed
sub modprobe {
my $module = $_[0];
my $opts = $_[1];
my @lines;
my $line;
if ($modprobe_checked == "0") {
@lines = `modprobe -c`;
foreach $line (@lines) {
if ($line =~ /^allow_unsupported_modules *0/) {
$modprobe_opt = " --allow-unsupported-modules ";
last;
}
}
$modprobe_checked = "1";
}
if (!defined($opts)) {
$opts = "";
}
system("modprobe $modprobe_opt $module $opts");
}
# bring up rxe
sub do_start {
my $proto_str = "";
system("mkdir -p /var/rxe");
system("touch $persistence_file");
modprobe("ib_core");
modprobe("ib_uverbs");
modprobe("rdma_ucm");
modprobe("rdma_rxe");
populate_persistence();
foreach my $eth (@persistence_array) {
rxe_add($eth);
}
get_names();
foreach my $rxe (@rxe_array) {
my $stat = get_devinfo($rxe);
if ($stat =~ "PORT_DOWN") {
my $cmd = "ifconfig $eth_names{$rxe} up";
system($cmd);
}
}
}
# check if argument is an integer
sub is_integer {
defined $_[0] && $_[0] =~ /^[+-]?\d+$/;
}
# remove all rxe devices and unload drivers
sub do_stop {
my $rxe;
foreach $rxe (@rxe_array) {
system("echo '$rxe' > $sys/remove");
}
if (-e $sys) {
system("rmmod rdma_rxe");
}
if (-e $sys) {
print "unable to unload drivers, reboot required\n";
}
}
sub do_debug {
my $arg2 = $_[0];
my $debugfile = "$parms/debug";
chomp($arg2);
#print "debug $arg2\n";
#system("echo 'debug $arg2' > $proc");
if (!(-e "$debugfile")) {
print "Error: debug is compiled out of this rxe driver\n";
return;
}
switch ($arg2) {
case "on" { system("echo '31' > $debugfile"); }
case "off" { system("echo '0' > $debugfile"); }
case "0" { system("echo '0' > $debugfile"); }
case "" { }
elsif ($arg2 ge "0" && $arg2 le "31") {
system("echo '$arg2' > $debugfile");
}
else {
print "unrecognized debug cmd ($arg2)\n";
}
}
my $current = `cat $debugfile $z`;
chomp($current);
if ($current > 0) {
print "Debug is ON ($current)\n";
}
elsif ($current == 0) {
print "Debug is OFF\n";
}
else {
print "Unrecognized debug value\n";
}
}
sub max {
my $a = $_[0];
my $b = $_[1];
return $a if ($a > $b);
return $b;
}
# show usage for rxe_cfg
sub usage {
print " Usage:\n";
print " rxe_cfg [options] start|stop|status|persistent\n";
print " rxe_cfg debug on|off|<num>\n";
print " rxe_cfg [-n] add <ndev>\n";
print " rxe_cfg [-n] remove <ndev>|<rdev>\n";
print "\n";
print " <ndev> = network device e.g. eth3\n";
print " <rdev> = rdma device e.g. rxe1\n";
print "\n";
print " Options:\n";
print " -h: print this usage information\n";
print " -n: do not make the configuration action persistent\n";
print " -v: print additional debug output\n";
print " -l: show status for interfaces with link up\n";
print " -p <num>: (start command only) - set ethertype\n";
}
sub main {
GetOptions(
"-h" => \$help,
"--help" => \$help,
"-n" => \$no_persist,
"-v:+" => \$debug,
"-f" => \$force,
"-l" => \$linkonly,
);
my $arg1 = $ARGV[0];
my $arg2 = $ARGV[1];
my $arg3 = $ARGV[2];
# status is the default
if (!defined($arg1) || ($arg1 =~ /status/)) {
do_status($arg2);
exit;
}
if ($help) {
usage();
exit;
}
# stuff that does not require modules to be loaded
switch($arg1) {
case "help" { usage(); exit; }
case "start" { do_start(); do_status(); exit; }
case "persistent" { system("cat $persistence_file"); exit; }
}
# can't do much else, bail if modules aren't loaded
if (check_module_status()) {
exit;
}
# create persistence file if necessary
`mkdir -p $persistence_path $z`;
if (!(-e $persistence_file)) {
`touch $persistence_file $z`;
}
# Get full context of the configuration
populate_persistence();
get_names();
get_dev_info();
# Stuff that requires the rdma_rxe module to be loaded
switch($arg1) {
case "stop" { do_stop(); exit; }
case "debug" { do_debug($arg2); exit; }
case "add" { rxe_add($arg2); exit; }
case "remove" { rxe_remove($arg2); exit; }
case "help" { usage(); exit; }
}
}
main();
exit;