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