snmpit_arista.pm.in 27.8 KB
Newer Older
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
#!/usr/bin/perl -w

#
# EMULAB-LGPL
# Copyright (c) 2012 University of Utah and the Flux Group.
# All rights reserved.
#

#
# snmpit module for Arista networks switches
#

package snmpit_arista;
use strict;

$| = 1; # Turn off line buffering on output

use English;
use SNMP;
use snmpit_lib;

use libtestbed;
use Lan;
use Port;
use Data::Dumper;
26
27
use RPC::XML;
use RPC::XML::Client;
28

29
30
# Configure variables:
my $RPCPORT  = "@ARISTA_SWITCH_XMLRPCPORT@";
31

Weibin Sun's avatar
Weibin Sun committed
32
33
34
35
36

my $STATUS_UP = 1;
my $STATUS_DOWN = 2;
my $SNMP_NO_INSTANCE = "NOSUCHINSTNCE";

37
38
39
40
41
42
43
44
45
46
#
# Port status and control.
#
my $PORT_ADMIN_STATUS     = "ifAdminStatus";
my %cmdOIDs =
    (
     "enable"  => [$PORT_ADMIN_STATUS, $STATUS_UP, "INTEGER"],
     "disable" => [$PORT_ADMIN_STATUS, $STATUS_DOWN, "INTEGER"],
    );
my $PORT_OPER_STATUS      = "ifOperStatus";
47
48
my $PORT_SPEED            = "ifHighSpeed";

49
# See the explaination of VLAN ID prefix in snmpit_arista_switch_daemon.
50
51
52
53
54
55
56
my $VLAN_ID_PREFIX = "EV_";


# Port format
#
# 1. Port used by SNMP OID: (slot*1000)+port
# 2. Port class instance.
57
58
# 3. Port used by SysDB on Arista switch, "EthernetX/Y", X: module/layer/level/slot, Y: port
#
59
60
# NOTE: 1. switch side port module # starts from 1, not 0
#       2. the wires table in DB must also take care of the format rules above.
61
62
63
#
my $PORT_FORMAT_IFINDEX   = 1;
my $PORT_FORMAT_PORT      = 2;
64
my $PORT_FORMAT_SYSDB     = 3;
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

#
# Creates a new object. 
#
# usage: new($classname,$devicename,$debuglevel,$community)
#        returns a new object, blessed into the snmpit_arista class.
#
sub new($$$;$) {

    # The next two lines are some voodoo taken from perltoot(1)
    my $proto = shift;
    my $class = ref($proto) || $proto;

    my $name = shift;
    my $debugLevel = shift;
    my $community = shift;  # actually the password for telnet

    #
    # Create the actual object
    #
    my $self = {};

    #
    # Set the defaults for this object
    # 
    if (defined($debugLevel)) {
        $self->{DEBUG} = $debugLevel;
    } else {
        $self->{DEBUG} = 0;
    }
    $self->{BLOCK} = 1;
    $self->{CONFIRM} = 1;
    $self->{NAME} = $name;

    #
    # Get config options from the database
    #
    my $options = getDeviceOptions($self->{NAME});
    if (!$options) {
        warn "ERROR: Getting switch options for $self->{NAME}\n";
        return undef;
    }

    $self->{MIN_VLAN}         = $options->{'min_vlan'};
    $self->{MAX_VLAN}         = $options->{'max_vlan'};

     if ($community) { # Allow this to over-ride the default
        $self->{COMMUNITY}    = $community;
    } else {
        $self->{COMMUNITY}    = $options->{'snmp_community'};
    }
 
    # other global variables
    $self->{DOALLPORTS} = 0;
    $self->{SKIPIGMP} = 1;

    # Max # of ports, must be a multiple of 8.
    $self->{MAX_NUM_PORTS} = 144;

    if ($self->{DEBUG}) {
        print "snmpit_arista initializing $self->{NAME}, " .
            "debug level $self->{DEBUG}\n" ;   
    }

    #
    # Set up SNMP module variables, and connect to the device
    #
    $SNMP::debugging = ($self->{DEBUG} - 2) if $self->{DEBUG} > 2;

    # Placeholder for SNMP session object
    $self->{SESS} = 0;
136
137
138

    # Placeholder for XML-RPC client
    $self->{CLT} = 0;
139
140
141
142
143
    
    # Make it a class object
    bless($self, $class);

    # Create SNMP session
144
    if (!$self->initSNMPSession()) {
145
146
147
	return undef;
    }

148
149
150
151
152
    # Create XML-RPC session client
    if (!$self->initRPCSession()) {
	return undef; 
    }

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
    #
    # Sometimes the SNMP session gets created when there is no connectivity
    # to the device so let's try something simple
    #
    my $test_case = $self->get1("sysObjectID", 0);
    if (!defined($test_case)) {
	warn "WARNING: Unable to retrieve via SNMP from $self->{NAME}\n";
	return undef;
    }

    return $self;
}

#
# Sometime after an SNMP op failure, following SNMP ops will also fail
# , we simply create a new one to overcome such annoying problem.
#
170
sub initSNMPSession($) {
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    my $self = shift;

    $self->{SESS} = new SNMP::Session(DestHost => $self->{NAME},Version => "2c",
				      Timeout => 4000000, Retries=> 12,
				      Community => $self->{COMMUNITY});

    if (!$self->{SESS}) {
	#
	# Bomb out if the session could not be established
	#
	warn "WARNING: Unable to connect via SNMP to $self->{NAME}\n";
	return 0;
    }

    return 1;
}

188
189
190
191
192
193
#
# Initialize XML-RPC client object
#
sub initRPCSession($) {
    my $self = shift;

Weibin Sun's avatar
Weibin Sun committed
194
    $self->{CLT} = RPC::XML::Client->new("http://".$self->{NAME}.":$RPCPORT");
195
196
197
198
199
200
201
202
    if (!$self->{CLT}) {
	warn "WARNING: Unable to connect to XML RPC server at $self->{NAME}\n";
	return 0;
    }

    return 1;
}

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#
# XML RPC Call helper.
#
# usage: callRPC($self, $id, @args)
#        return remote method return value on success.
#        return undef and print error on failure.
#
sub callRPC {
    my ($self, $id, @args) = @_;

    my $resp = $self->{CLT}->send_request(@args);
    if (ref($resp)) {
	return $resp->value;
    } else {
	warn "WARNING: $id XML-RPC failed, error: ".$resp->string."\n";
    }

    return undef;
}


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
sub sysdb2Ifindex($$) {
    my ($self, $sbport) = @_;

    if ($sbport =~ /^Ethernet(\d+)\/(\d+)$/) {
	return $1*1000 + $2;
    }

    return undef;
}

sub sysdb2PortInstance($$) {
    my ($self, $sbport) = @_;

    if ($sbport =~ /^Ethernet(\d+)\/(\d+)$/) {
	return Port->LookupByStringForced(
	    Port->Tokens2TripleString(
		$self->{NAME},
		$1,
		$2));
    }

    return undef;
}

248
249

#
250
# Converting port formats.
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
#
sub convertPortFormat($$@) {
    my $self = shift;
    my $output = shift;
    my @ports = @_;

    #
    # Avoid warnings by exiting if no ports given
    # 
    if (!@ports) {
	return ();
    }

    #
    # We determine the type by sampling the first port given
    #
    my $sample = $ports[0];
    if (!defined($sample)) {
	warn "convertPortFormat: Given a bad list of ports\n";
	return undef;
    }

    my $input = undef;
    if (Port->isPort($sample)) {
	$input = $PORT_FORMAT_PORT;
276
277
    } elsif ($sample =~ /^Ethernet/) {
	$input = $PORT_FORMAT_SYSDB;
278
279
280
281
282
283
284
285
286
287
288
289
290
    } else {
	$input = $PORT_FORMAT_IFINDEX;
    }
    
    #
    # It's possible the ports are already in the right format
    #
    if ($input == $output) {
	return @ports;
    }

    if ($input == $PORT_FORMAT_PORT) {
	my @swports = map $_->getEndByNode($self->{NAME}), @ports;
291
292

	if ($output == $PORT_FORMAT_IFINDEX) {
Weibin Sun's avatar
Weibin Sun committed
293
	    my @ifports = map int(int($_->card())*1000 +int($_->port())), @swports;
294
295
296
297
298
	    return @ifports;
	} else {
	    my @sysdbports = map "Ethernet".$_->card()."/".$_->port(), @swports;
	    return @sysdbports
	}
299
    } elsif ($input == $PORT_FORMAT_IFINDEX) {
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
	if ($output == $PORT_FORMAT_PORT) {
	    my @swports = map Port->LookupByStringForced(
		Port->Tokens2TripleString(
		    $self->{NAME},
		    int($_/1000),
		    $_ %1000)), @ports;
	    return @swports;
	} else {
	    my @sysdbports = map "Ethernet".int($_ /1000)."/".int($_ %1000), @ports;
	    return @sysdbports;
	}
	
    } else { # input is $PORT_FORMAT_SYSDB
	if ($output == $PORT_FORMAT_IFINDEX) {
	    my @ifports = map $self->sysdb2Ifindex($_), @ports;
	    return @ifports;
	} else {
	    my @swports = map $self->sysdb2PortInstance($_), @ports;
	    return @swports
	}	
    }
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

    #
    # Some combination we don't know how to handle
    #
    warn "convertPortFormat: Bad input/output combination ($input/$output)\n";
    return undef;    
}

# SNMP helpers from snmpit_hp

sub hammer($$$;$) {
    my ($self, $closure, $id, $retries) = @_;

    if (!defined($retries)) { $retries = 12; }
    for my $i (1 .. $retries) {
	my $result = $closure->();
	if (defined($result) || ($retries == 1)) { return $result; }
	warn $id . " ... will try again\n";
	sleep 1;
    }
    warn  $id . " .. giving up\n";
    return undef;
}

# SNMP shorthand

sub get1($$$) {
    my ($self, $obj, $instance) = @_;
    my $id = $self->{NAME} . "::get1($obj.$instance)";
    my $closure = sub () {
	my $RetVal = snmpitGet($self->{SESS}, [$obj, $instance], 1);
	if (!defined($RetVal)) { sleep 4;}
	return $RetVal;
    };
    my $RetVal = $self->hammer($closure, $id, 40);
    if (!defined($RetVal)) {
	warn "$id failed - $snmpit_lib::snmpitErrorString\n";
    }

    #
    # Accoding to my testing on Extreme switch, no-instance will
    # still return a string "NOSUCHINSTANCE";
    #
    if ($SNMP_NO_INSTANCE eq $RetVal) {
	return undef;
    }
    return $RetVal;
}

sub walk($$) {
    my ($self, $oid) = @_;

    return $self->{SESS}->bulkwalk(0, 32, [[$oid]]);
}

sub set($$;$$) {
    my ($self, $varbind, $id, $retries) = @_;
    if (!defined($id)) { $id = $self->{NAME} . ":set "; }
    if (!defined($retries)) { $retries = 2; }
    my $sess = $self->{SESS};
    my $closure = sub () {
	my $RetVal = $sess->set($varbind);
	my $status = $RetVal;
	if (!defined($RetVal)) {
	    $status = "(undefined)";
	    if ($sess->{ErrorNum}) {
		my $bad = "$id had error number " . $sess->{ErrorNum} .
			  " and had error string " . $sess->{ErrorStr} . "\n";
		print $bad;
	    }
	}
	return $RetVal;
    };
    my $RetVal = $self->hammer($closure, $id, $retries);
    return $RetVal;
}

sub strtrim($$) {
    my ($self, $str) = @_;

    $str =~ s/^\s+|\s+$//g;
    return $str;
}

sub iidCatHelper($$) {
    my $self = shift;
    my $iid = shift;

    if (length($self->strtrim($iid)) > 0) {
	return ".".$self->strtrim($iid);
    }

    return "";
}

sub isExpVlanName($$) {
    my ($self, $vid) = @_;

    if ($vid =~ /^$VLAN_ID_PREFIX/ ) {
	return 1;
    }

    return 0;
}


############# Standard snmpit driver interface APIs:################

#
# Set a variable associated with a port. The commands to execute are given
# in the cmdOIs hash above
#
# usage: portControl($self, $command, @ports)
#	 returns 0 on success.
#	 returns number of failed ports on failure.
#	 returns -1 if the operation is unsupported
#
sub portControl ($$@) {
    my $self = shift;
    my $cmd = shift;
    my @ports = @_;

    my $errors = 0;
    
    $self->debug("portControl: $cmd -> (".Port->toStrings(@ports).")\n");

    my @ifports = $self->convertPortFormat($PORT_FORMAT_IFINDEX, @ports);

    if (defined $cmdOIDs{$cmd}) {
	my $oid = $cmdOIDs{$cmd};
	foreach my $ifport (@ifports) {
	    my $retval = $self->set(
		[[$oid->[0], $ifport, $oid->[1], $oid->[2]]],
		$self->{NAME}."::portControl");
	    if (!defined($retval)) {
		$errors++;
	    }
	}
    } else {
	#
	# Command not supported
	#
	$self->debug("Unsupported port control command '$cmd' ignored.\n");
    }

    return $errors;
}

# 
# Check to see if the given 802.1Q VLAN tag exists on the switch
#
# usage: vlanNumberExists($self, $vlan_number)
#        returns 1 if the VLAN exists, 0 otherwise
#
sub vlanNumberExists($$) {
    my ($self, $vlan_number) = @_;
    my $id = $self->{NAME}."::vlanNumberExists($vlan_number)";

    $self->debug($id."\n");

481
482
483
    my $resp = $self->callRPC($id, 'vlanTagExist', $vlan_number);
    if ($resp) {
	return 1;
484
    }
485
    
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
    $self->debug($id." VLAN #$vlan_number not exists.\n");

    return 0;
}

#
# Given VLAN indentifiers from the database, finds the 802.1Q VLAN
# number for them. If not VLAN id is given, returns mappings for the entire
# switch.
# 
# usage: findVlans($self, @vlan_ids)
#        returns a hash mapping VLAN ids to 802.1Q VLAN numbers
#        any VLANs not found have NULL VLAN numbers
#
sub findVlans($@) {
    my $self = shift;
    my @vlan_ids = @_;
    my $id = $self->{NAME} . "::findVlans";
    $self->debug("$id\n");

506
507
508
    my $resp = $self->callRPC($id, 'getVlanName2TagMappings', \@vlan_ids);
    if ($resp) {
	my %mps = %{$resp};
Weibin Sun's avatar
Weibin Sun committed
509
	$self->debug("$id RPC results: ".Dumper(\%mps));
510
	return %mps;
511
    } 
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
    
    return ();
}

#
# Given a VLAN identifier from the database, find the 802.1Q VLAN
# number that is assigned to that VLAN. Retries several times (to account
# for propagation delays) unless the $no_retry option is given.
#
# usage: findVlan($self, $vlan_id,$no_retry)
#        returns the VLAN number for the given vlan_id if it exists
#        returns undef if the VLAN id is not found
#
sub findVlan($$;$) {
    my $self = shift;
    my $vlan_id = shift;
    my $no_retry = shift; # ignored here
    my $id = $self->{NAME} . ":findVlan";

    $self->debug("$id ( $vlan_id )\n",2);
532
533
534
535
536
537

    my %mps = $self->findVlans($vlan_id);
    if (exists($mps{$vlan_id})) {
	return $mps{$vlan_id};
    }
    
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
    return undef;
}

#   
# Create a VLAN on this switch, with the given identifier (which comes from
# the database) and given 802.1Q tag number.
#
# usage: createVlan($self, $vlan_id, $vlan_number)
#        returns the new VLAN number on success
#        returns 0 on failure
#
sub createVlan($$$) {
    my $self = shift;
    my $vlan_id = shift;
    my $vlan_number = shift;
    my $id = $self->{NAME} . ":createVlan";

    if (!defined($vlan_number)) {
	warn "$id called without supplying vlan_number";
	return 0;
    }
    my $check_number = $self->findVlan($vlan_id,1);
    if (defined($check_number)) {
	if ($check_number != $vlan_number) {
562
563
564
	    warn "  ERROR: $id: Not creating $vlan_id because it already ".
	         "exists with name $check_number\n";
            return 0;
565
566
567
568
569
	}
    }
    
    $self->debug("createVlan: name $vlan_id number $vlan_number \n");

570
571
572
    print "  Creating VLAN $vlan_id as VLAN #$vlan_number on " .
	"$self->{NAME} ...\n";

573
    $self->lock();
574
    my $resp = $self->callRPC($id, 'createVlan', $vlan_id, $vlan_number);
575
576
    $self->unlock();
    
Weibin Sun's avatar
Weibin Sun committed
577
    if (!defined($resp) || $resp ne "1") {
578
579
	warn "  WARNING: $id Creating VLAN $vlan_id as VLAN #$vlan_number on ".
	    "$self->{NAME} failed.".(defined($resp)?" error: $resp":"")."\n";
580
581
    }
    
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
    return $vlan_number;
}

#
# Put the given ports in the given VLAN. The VLAN is given as an 802.1Q 
# tag number.
#
# usage: setPortVlan($self, $vlan_number, @ports)
#	 returns 0 on sucess.
#	 returns the number of failed ports on failure.
#
sub setPortVlan($$@) {
    my $self = shift;
    my $vlan_number = shift;
    my @ports = @_;
    my $errors = 0;

    my $id = $self->{NAME} . "::setPortVlan($vlan_number)";
    $self->debug($id."\n");

602
603
604
605
606
    if (! $self->callRPC('vlanTagExist', $id, $vlan_number)) {
	warn "VLAN $vlan_number does not exist on $self->{NAME}\n";
	return 1;
    }

607
608
    return 0 unless(@ports);

609
    my @swports = $self->convertPortFormat($PORT_FORMAT_SYSDB, @ports);
610

611
    my $resp = $self->callRPC($id, 'setPortVlan', $vlan_number, \@swports);
612
    
613
614
615
616
    if (defined($resp)) {
	$errors = $resp;
    } else {
	$errors = scalar(@ports);
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
    }

    return $errors;
}


#
# Remove the given ports from the given VLAN. The VLAN is given as an 802.1Q 
# tag number.
#
# usage: delPortVlan($self, $vlan_number, @ports)
#	 returns 0 on sucess.
#	 returns the number of failed ports on failure.
#
sub delPortVlan($$@) {
    my $self = shift;
    my $vlan_number = shift;
    my @ports = @_;
    my $errors = 0;

    my $id = $self->{NAME}."::delPortVlan($vlan_number)";
638
    $self->debug($id."\n");
639

640
641
642
643
    if (! $self->callRPC($id, 'vlanTagExist', $vlan_number)) {
	warn "VLAN $vlan_number does not exist on $self->{NAME}\n";
	return 1;
    }
644
645
646
    
    return 0 unless(@ports);

647
648
649
    my @swports = $self->convertPortFormat($PORT_FORMAT_SYSDB, @ports);

    my $resp = $self->callRPC($id, 'removePortsFromVlan', $vlan_number, \@swports);
650
    
651
652
653
654
655
656
    if (defined($resp)) {
	$errors = $resp;
    } else {
	$errors = scalar(@ports);
    }

657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
    return $errors;
}

#
# Disables all ports in the given VLANS. Each VLAN is given as a VLAN
# 802.1Q tag value.
#
# usage: removePortsFromVlan(self,@vlan)
#	 returns 0 on sucess.
#	 returns the number of failed ports on failure.
#
sub removePortsFromVlan($@) {
    my $self = shift;
    my @vlan_numbers = @_;
    my $errors = 0;
    my $id = $self->{NAME} . "::removePortsFromVlan";

    $self->debug($id."\n");
    
    foreach my $vlan_number (@vlan_numbers) {
677
678
679
680
681
682
683
684
685
686
687
688
	 if (! $self->callRPC($id, 'vlanTagExist', $vlan_number)) {
	     warn "  VLAN $vlan_number does not exist on $self->{NAME}\n";
	     $errors++;
	 }
    
	 my $resp = $self->callRPC($id, 'removePortsFromVlan', $vlan_number, []);
    
	 if (defined($resp)) {
	     $errors += $resp;
	 } else {
	     $errors++;
	 }
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
    }

    return $errors;
}

#
# Removes and disables some ports in a given VLAN.
# The VLAN is given as a VLAN 802.1Q tag value.
#
# usage: removeSomePortsFromVlan(self,vlan,@ports)
#	 returns 0 on sucess.
#	 returns the number of failed ports on failure.
#
sub removeSomePortsFromVlan($$@) {
    my ($self, $vlan_number, @ports) = @_;
    my $id = $self->{NAME} . "::removeSomePortsFromVlan";

706
    $self->debug($id."\n");
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
    return $self->delPortVlan($vlan_number, @ports);
}

#
# Remove the given VLANs from this switch. Removes all ports from the VLAN,
# The VLAN is given as a VLAN identifier from the database.
#
# usage: removeVlan(self,int vlan)
#	 returns 1 on success
#	 returns 0 on failure
#
sub removeVlan($@) {
    my $self = shift;
    my @vlan_numbers = @_;
    my $errors = 0;
    my $id = $self->{NAME} . "::removeVlan";
    
    foreach my $vlan_number (@vlan_numbers) {
725
726
	$self->removePortsFromVlan($vlan_number);
	my $resp = $self->callRPC($id, 'removeVlan',$vlan_number);
Weibin Sun's avatar
Weibin Sun committed
727
	if (!defined($resp) || $resp ne "1") {
728
729
730
731
	    warn "$id remove VLAN #$vlan_number failed on $self->{NAME}."
		.(defined($resp)?" Error: $resp":"")."\n";
	    $errors++;
	}
732
733
734
735
736
737
738
739
740
741
742
    }
   
    return ($errors == 0) ? 1 : 0;
}

#
# Update Field, we don't support.
# This is ususally used for set port property, for Arista switch,
# we only support enable and disable, both can be done inside portControl().
#
sub UpdateField($$$@) {
743
    warn "ERROR: snmpit_arista does not support UpdateField().\n";
744
745
746
747
748
749
750
751
752
    return 0;
}

#
# Determine if a VLAN has any members 
# (Used by stack->switchesWithPortsInVlan())
#
sub vlanHasPorts($$) {
    my ($self, $vlan_number) = @_;
Weibin Sun's avatar
Weibin Sun committed
753
    my $id = $self->{NAME}."::vlanHasPorts($vlan_number)";
754

755
756
757
    if ($self->callRPC($id, 'vlanHasPorts', $vlan_number)) {
	return 1;
    }
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
    
    return 0;
}    

#
# List all VLANs on the device
#
# usage: listVlans($self)
# see snmpit_cisco_stack.pm for a description of return value format
#
sub listVlans($) {
    my $self = shift;
    my $id = $self->{NAME} . "::listVlans()";
    my @list = ();

    $self->debug($id,1);

775
776
777
778
779
780
    my $resp = $self->callRPC($id, 'listVlans');
    if ($resp) {
	my @vlans = @$resp;
	foreach my $vlan (@vlans) {
	    my @ports = $self->convertPortFormat($PORT_FORMAT_PORT, @{$vlan->[2]});
	    push @list, [$vlan->[0], $vlan->[1], \@ports];		
Weibin Sun's avatar
Weibin Sun committed
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
    
    return @list;
}

#
# List all ports on the device
#
# Specially for Arista switch: All ports are duplex, auto-negotiated.
#
# usage: listPorts($self)
# see snmpit_cisco_stack.pm for a description of return value format
#
sub listPorts($) {
    my $self = shift;
    
    my @rv = ();
    my %Able = ();
    my %Link = ();
    my %speed =();

    my $id = $self->{NAME}."::listPorts";

    my $ifTable = [$PORT_ADMIN_STATUS, 0];
    my ($varname, $modport, $ifIndex, $portIndex, $status, $portname);
    $self->{SESS}->getnext($ifTable);
    do {
	($varname,$ifIndex,$status) = @{$ifTable};

	# Make sure this port is wired up and connecting to a node.
	if (defined(Port->GetOtherEndByTriple($self->{NAME},
813
814
					      int($ifIndex /1000),
					      int($ifIndex % 1000)))) {
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
	    $self->debug("$varname $ifIndex $status\n");
	    if ($varname =~ /$PORT_ADMIN_STATUS/) { 
		$Able{$ifIndex} =
		    ($status =~/up/ || "$status" eq "$STATUS_UP")  ? "yes" : "no";
	    }
	}	
	$self->{SESS}->getnext($ifTable);
    } while ( $varname =~ /^$PORT_ADMIN_STATUS/) ;

    foreach $ifIndex (keys %Able) {
	$status = $self->get1($PORT_OPER_STATUS, $ifIndex);
	if (defined($status)) {
	    $Link{$ifIndex} =
		($status =~/up/ || "$status" eq "$STATUS_UP") ? "yes" : "no";
	}

	$status = $self->get1($PORT_SPEED, $ifIndex);
	if (defined($status)) {
	    $speed{$ifIndex} = "$status"."Mbs";
	}
    }

    foreach $ifIndex (keys %Able) {
	my $port = Port->GetOtherEndByTriple($self->{NAME},
839
840
					     int($ifIndex /1000),
					     int($ifIndex % 1000));
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
	if (!defined($port)) {
	    $self->debug("$id : $ifIndex not connected, skipping\n");
	    next;			 
	}
	push @rv, [$port, $Able{$ifIndex}, $Link{$ifIndex},
		   (exists($speed{$ifIndex})? $speed{$ifIndex}: ""),
		   "duplex"];
    }    
    
    return @rv;
}

# 
# Get statistics for ports on the switch
#
# usage: getStats($self)
# see snmpit_cisco_stack.pm for a description of return value format
#
sub getStats() {
    my $self = shift;

    #
    # Walk the tree for the VLAN members
    #
    my $vars = new SNMP::VarList(['ifInOctets'],['ifInUcastPkts'],
				 ['ifInNUcastPkts'],['ifInDiscards'],
				 ['ifInErrors'],['ifInUnknownProtos'],
				 ['ifOutOctets'],['ifOutUcastPkts'],
				 ['ifOutNUcastPkts'],['ifOutDiscards'],
				 ['ifOutErrors'],['ifOutQLen']);
    my @stats = $self->{SESS}->bulkwalk(0,32,$vars);

    my %allports = ();
    
    #
    # We need to flip the two-dimentional array we got from bulkwalk on
    # its side, and convert ifindexes into Port instance
    #
    my $i = 0;
    my %stats;
    foreach my $array (@stats) {
	while (@$array) {
	    my ($name,$ifindex,$value) = @{shift @$array};

	    # Make sure this port is wired up and connecting to a node.
	    if (defined(Port->GetOtherEndByTriple($self->{NAME},
887
888
						  int($ifindex / 1000),
						  int($ifindex % 1000)))) {
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
		my ($po) = $self->convertPortFormat($PORT_FORMAT_PORT, $ifindex);
		if (!$po) { next; } # Skip if we don't know about it
		my $port = $po->getOtherEndPort()->toTripleString();

		#
		# in case a switch port without wire, but convertPortFromString should work
		# because the cache in Port class already saves the instance ...
		#
		$allports{$port} = $po;
		
		${$stats{$port}}[$i] = $value;
	    }
	}
	$i++;
    }

    return map [$allports{$_}, @{$stats{$_}}], sort {tbsort($a,$b)} keys %stats;
}

#
# Used to flush FDB entries easily
#
# usage: resetVlanIfOnTrunk(self, modport, vlan)
#
sub resetVlanIfOnTrunk($$$) {
    my ($self, $modport, $vlan) = @_;

916
917
918
919
920
921
    #
    # MAYBE-TODO: check like snmpit_hp?
    #

    $self->setVlansOnTrunk($modport, 0, $vlan);
    $self->setVlansOnTrunk($modport, 1, $vlan);
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944

    return 0;
}

#
# Get the ifindex for an EtherChannel (trunk given as a list of ports)
#
# usage: getChannelIfIndex(self, ports)
#        Returns: undef if more than one port is given, and no channel is found
#           an ifindex if a channel is found and/or only one port is given
#
# N.B. by Sklower - cisco's use this to put vlans on multiwire trunks;
# it gets called from _stack.pm
#
# HP's also require a different ifindex for putting a vlan on a multiwire
# trunk from the individual ifindex from any constituent port.
#
# although Rob Ricci's vision is that this would only get called when putting
# vlans on multi-wire interswitch trunks and the check would happen in
# _stack, it is 1.) possible to use snmpit -i Switch <mod>/<port> to do
# maintenance functions of vlans and so you should check for each port
# any way, and 2.) the check is cheap and can be done in convertPortFormat.
#
945
# NOTE: NOT-SUPPORTED We mostly use XML-RPC rather than SNMP.
946
947
948
949
950
#
sub getChannelIfIndex($@) {
    my $self = shift;
    my @ports = @_;

951
    warn "ERROR: " . $self->{NAME} . "::getChannelIfIndex: not supported.\n";
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969

    return undef;
}


#
# Enable, or disable,  port on a trunk
#
# usage: setVlansOnTrunk(self, modport, value, vlan_numbers)
#        modport: module.port of the trunk to operate on
#        value: 0 to disallow the VLAN on the trunk, 1 to allow it
#	 vlan_numbers: An array of 802.1Q VLAN numbers to operate on
#        Returns 1 on success, 0 otherwise
#
sub setVlansOnTrunk($$$$) {
    my ($self, $modport, $value, @vlan_numbers) = @_;
    my $id = $self->{NAME} . "::setVlansOnTrunk";

Weibin Sun's avatar
Weibin Sun committed
970
    # NOTE: so-called $modport must be a Port instance!
Weibin Sun's avatar
Weibin Sun committed
971
    my $swport = $self->convertPortFormat($PORT_FORMAT_SYSDB, $modport);
Weibin Sun's avatar
Weibin Sun committed
972
973
974
975
976
977
978
    my $resp = $self->callRPC($id, 'setAllowedVlansOnTrunkedPort', $swport,
			      ($value?1:0), \@vlan_numbers);
    if ($resp) {
	return 1;
    } else {
	return 0;
    }
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
}

#
# Enable trunking on a port
#
# usage: enablePortTrunking2(self, modport, nativevlan, equaltrunking[, drop])
#        modport: module.port of the trunk to operate on
#        nativevlan: VLAN number of the native VLAN for this trunk
#	 equaltrunk: don't do dual mode; tag PVID also.
#	 exclude: need to choose something other than this, which actually
#        is the current PVID for this port.
#        Returns 1 on success, 0 otherwise
#
sub enablePortTrunking2($$$$) {
    my ($self,$port,$native_vlan,$equaltrunking) = @_;
    my $id = $self->{NAME} .
		"::enablePortTrunking($port,$native_vlan,$equaltrunking)";

Weibin Sun's avatar
Weibin Sun committed
997
998
999
1000
    my $swport = $self->convertPortFormat($PORT_FORMAT_SYSDB, $port);
    my $resp = $self->callRPC($id, 'enablePortTrunking',
			      $swport, $native_vlan, ($equaltrunking?1:0));
    if ($resp) {
For faster browsing, not all history is shown. View entire blame