pseudowire 4.58 KB
Newer Older
Alexander Gall's avatar
Alexander Gall committed
1 2 3 4 5 6 7
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Snabb::SNMP::Agent qw(%persistent_ifIndex %compound_scalar_handlers);
my %opt = ( 'check-interval' => 5,
            'mibs-dirs' => '',
8
            'shmem-dir' => '/var/lib/snabb/shmem');
Alexander Gall's avatar
Alexander Gall committed
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

sub usage() {
  print <<"EOF";
usage: $0 --ifindex=<file>
EOF
  exit(1);
}

## Mappings of PW shared memory segments to the index of the
## pwTable/cpwVcTable
my %pwIndex;
my $pwIndex = 1;

## Indexer for pwTable/cpwVcTable.  It uses the name of the segment
## that represents a row in these tables to generate the index.
sub pw_indexer($$$) {
  my ($oid, $table_oid, $segment) = @_;
  my $name = $segment->{name};
  my $index = $pwIndex{$name};
  unless (defined $index) {
    $index = $pwIndex++;
    print("Allocating PW index $index for segment $name\n");
    $pwIndex{$name} = $index;
  }
  return $oid.".".$index;
}

## Indexer for the pwEnetTable.  It has two indexes.  The first is the
## pwIndex according to the pw_indexer.  the second is the value of
## the pwEnetPwInstance object.
sub pw_enet_indexer($$$) {
  my ($oid, $table_oid, $segment) = @_;
  $oid = pw_indexer($oid, $table_oid, $segment);
  my $pwEnetPwInstance_obj = $segment->{objs}{pwEnetPwInstance};
  defined $pwEnetPwInstance_obj or
    die "pw_enet_indexer: no pwEnetPwInstance present for segment "
      ."$segment->{name}";
  tie my $pwEnetPwInstance, 'Snabb::SNMP::Tie::INTEGER', $segment,
    'pwEnetPwInstance';

  return $oid.".".$pwEnetPwInstance;
}

## Indexer for the cpwVcEnetTable.  It has two indexes.  The first is
## the cpwVcIndex according to the pw_indexer.  the second is the value
## of the cpwVcEnetPwVlan object.
sub cpw_enet_indexer($$$) {
  my ($oid, $table_oid, $segment) = @_;
  $oid = pw_indexer($oid, $table_oid, $segment);
  my $cpwVcEnetPwVlan_obj = $segment->{objs}{cpwVcEnetPwVlan};
  defined $cpwVcEnetPwVlan_obj or
    die "cpw_enet_indexer: no cpwVcEnetPwVlan present for segment "
      ."$segment->{name}";
  tie my $cpwVcEnetPwVlan, 'Snabb::SNMP::Tie::INTEGER', $segment,
    'cpwVcEnetPwVlan';

  return $oid.".".$cpwVcEnetPwVlan;
}

my %subtrees =
  ( cpwVcMIB =>
    { tables =>
      { scalars =>
	{ handlers =>
	  { cpwVcIndexNext =>
	    { compound_handler => sub { return $pwIndex; } },
	    cpwVcPerfTotalErrorPackets =>
	    { compound_handler => $compound_scalar_handlers{accumulator} },
	  },
	},

	cpwVcTable =>
	{ indexer => \&pw_indexer,
	},
      },
    },

    pwStdMIB =>
    { tables =>
      { scalars =>
	{ handlers =>
	  { pwIndexNext =>
	    { compound_handler => sub { return $pwIndex; } },
	    pwPerfTotalErrorPackets =>
	    { compound_handler => $compound_scalar_handlers{accumulator} },
	  },
	},

	pwTable =>
	{ indexer => \&pw_indexer,
	},
      },
    },

    cpwVcEnetMIB =>
    { tables =>
      { cpwVcEnetTable =>
	{ indexer => \&cpw_enet_indexer,
	  handlers =>
	  { cpwVcEnetPortIfIndex =>
	    { handler => sub {
		my ($value, $name, $segment) = @_;
		my $aux_name = "_X_$name";
		exists $segment->{objs}{$aux_name} or die
		  "$name handler: auxiliary object $aux_name does not exist";
		tie my $ifDescr, 'Snabb::SNMP::Tie::OCTETSTR', $segment,
		  $aux_name;
		my $ifIndex = $persistent_ifIndex{$ifDescr};
		defined $ifIndex or die
		  "$name handler: unknown interface $ifDescr";
		return($ifIndex);
	      }
	    },
	  },
	},
      },
    },

    pwEnetStdMIB =>
    { tables =>
      { pwEnetTable =>
	{ indexer => \&pw_enet_indexer,
	  handlers =>
	  { pwEnetPortIfIndex =>
	    { handler => sub {
		my ($value, $name, $segment) = @_;
		my $aux_name = "_X_$name";
		exists $segment->{objs}{$aux_name} or die
		  "$name handler: auxiliary object $aux_name does not exist";
		tie my $ifDescr, 'Snabb::SNMP::Tie::OCTETSTR', $segment,
		  $aux_name;
		my $ifIndex = $persistent_ifIndex{$ifDescr};
		defined $ifIndex or die
		  "$name handler: unknown interface $ifDescr";
		return($ifIndex);
	      }
	    },
	  },
	},
      },
    },

  );

GetOptions(\%opt, "check-interval=i", "mibs-dirs=s",
154
           "ifindex=s", "shmem-dir=s") or usage();
Alexander Gall's avatar
Alexander Gall committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
defined $opt{ifindex} or usage();
Snabb::SNMP::Agent::start({ name => "pseudowire",
                            subtrees => \%subtrees,
			    check_interval => $opt{'check-interval'},
                            if_index => $opt{ifindex},
                            mibs_dirs => $opt{'mibs-dirs'},
                            mibs => 'CISCO-SMI:CISCO-IETF-PW-TC-MIB'
                            .':CISCO-IETF-PW-MIB:PW-STD-MIB'
                            .':CISCO-IETF-PW-ENET-MIB:PW-ENET-STD-MIB',
                            shmem_dir => $opt{'shmem-dir'},
			  });

## Not reached

## Local Variables:
## mode: CPerl
## End: