1#!/usr/bin/perl
2
3# For now, this just tests the addition and removal of 1000 VLAN interfaces on eth0
4
5# Arguments:
6#  graph  Generate a graph.
7#  clean  Remove interfaces.
8
9use strict;
10
11$| = 1;
12
13if ($ARGV[0] eq "graph") {
14  my $dev_cnt = 0;
15  my $user = 0;
16  my $system = 0;
17  my $real = 0;
18  my $prog = "";
19
20  open(IPNH, ">/tmp/ip_rpt_no_hash.txt") || die("Can't open /tmp/ip_rpt_no_hash.txt\n");
21  open(IFCFGNH, ">/tmp/ifconfig_rpt_no_hash.txt") || die("Can't open /tmp/ifconfig_rpt_no_hash.txt\n");
22  open(IP, ">/tmp/ip_rpt.txt") || die("Can't open /tmp/ip_rpt.txt\n");
23  open(IFCFG, ">/tmp/ifconfig_rpt.txt") || die("Can't open /tmp/ifconfig_rpt.txt\n");
24
25  my $hash = $ARGV[1];
26  my $no_hash = $ARGV[2];
27
28  open(IF, "$no_hash");
29  while (<IF>) {
30    my $ln = $_;
31    chomp($ln);
32
33    #print "LINE: -:$ln:-\n";
34
35    if ($ln =~ /Doing ip addr show for (\S+)/) {
36      $dev_cnt = $1;
37      $prog = "ip";
38    }
39    elsif ($ln =~ /Doing ifconfig -a for (\S+)/) {
40      $dev_cnt = $1;
41      $prog = "ifconfig";
42    }
43    elsif ($ln =~ /^real (\S+)/) {
44      $real = $1;
45    }
46    elsif ($ln =~ /^user (\S+)/) {
47      $user = $1;
48    }
49    elsif ($ln =~ /^sys (\S+)/) {
50      $system = $1;
51      #print "prog: $prog  $dev_cnt\t$user\t$system\t$real\n";
52      if ($prog eq "ip") {
53	print IPNH "$dev_cnt\t$user\t$system\t$real\n";
54      }
55      else {
56	print IFCFGNH "$dev_cnt\t$user\t$system\t$real\n";
57      }
58    }
59    else {
60      #print "INFO:  Didn't match anything -:$ln:-\n";
61    }
62  }
63
64  close(IPNH);
65  close(IFCFGNH);
66  close(IF);
67
68  open(IF, "$hash");
69  while (<IF>) {
70    my $ln = $_;
71    chomp($ln);
72
73    #print "LINE: -:$ln:-\n";
74
75    if ($ln =~ /Doing ip addr show for (\S+)/) {
76      $dev_cnt = $1;
77      $prog = "ip";
78    }
79    elsif ($ln =~ /Doing ifconfig -a for (\S+)/) {
80      $dev_cnt = $1;
81      $prog = "ifconfig";
82    }
83    elsif ($ln =~ /^real (\S+)/) {
84      $real = $1;
85    }
86    elsif ($ln =~ /^user (\S+)/) {
87      $user = $1;
88    }
89    elsif ($ln =~ /^sys (\S+)/) {
90      $system = $1;
91      #print "prog: $prog  $dev_cnt\t$user\t$system\t$real\n";
92      if ($prog eq "ip") {
93	print IP "$dev_cnt\t$user\t$system\t$real\n";
94      }
95      else {
96	print IFCFG "$dev_cnt\t$user\t$system\t$real\n";
97      }
98    }
99    else {
100      #print "INFO:  Didn't match anything -:$ln:-\n";
101    }
102  }
103
104  close(IP);
105  close(IFCFG);
106
107  my $plot_cmd = "set title \"ip addr show V/S ifconfig -a\"
108set terminal png color
109set output \"ip_addr_show.png\"
110set size 1,2
111set xlabel \"Interface Count\"
112set ylabel \"Seconds\"
113set grid
114plot \'/tmp/ip_rpt.txt\' using 1:3 title \"ip_system\" with lines, \\
115     \'/tmp/ip_rpt.txt\' using 1:2 title \"ip_user\" with lines, \\
116     \'/tmp/ifconfig_rpt.txt\' using 1:3 title \"ifconfig_system\" with lines, \\
117     \'/tmp/ifconfig_rpt.txt\' using 1:2 title \"ifconfig_user\" with lines, \\
118     \'/tmp/ip_rpt_no_hash.txt\' using 1:3 title \"ip_system_no_hash\" with lines, \\
119     \'/tmp/ip_rpt_no_hash.txt\' using 1:2 title \"ip_user_no_hash\" with lines, \\
120     \'/tmp/ifconfig_rpt_no_hash.txt\' using 1:3 title \"ifconfig_system_no_hash\" with lines, \\
121     \'/tmp/ifconfig_rpt_no_hash.txt\' using 1:2 title \"ifconfig_user_no_hash\" with lines";
122  print "Plotting with cmd -:$plot_cmd:-\n";
123
124  open(GP, "| gnuplot") or die ("Can't open gnuplot pipe(2).\n");
125  print GP "$plot_cmd";
126  close(GP);
127
128  exit(0);
129}
130
131my $num_if = 4000;
132
133`/usr/local/bin/vconfig set_name_type VLAN_PLUS_VID_NO_PAD`;
134
135my $d = 5;
136my $c = 5;
137
138if ($ARGV[0] ne "clean") {
139
140  my $i;
141  print "Adding VLAN interfaces 1 through $num_if\n";
142
143  print "Turnning off /sbin/hotplug";
144  `echo  > /proc/sys/kernel/hotplug`;
145
146  my $p = time();
147  for ($i = 1; $i<=$num_if; $i++) {
148    `/usr/local/bin/vconfig add eth0 $i`;
149    #`ip address flush dev vlan$i`;
150    `ip address add 192.168.$c.$c/24 dev vlan$i`;
151    `ip link set dev vlan$i up`;
152
153    if (($i <= 4000) && (($i % 50) == 0)) {
154      #print "Doing ifconfig -a for $i devices.\n";
155      #`time -p ifconfig -a > /tmp/vlan_test_ifconfig_a_$i.txt`;
156      print "Doing ip addr show for $i devices.\n";
157      `time -p ip addr show > /tmp/vlan_test_ip_addr_$i.txt`;
158    }
159
160    $d++;
161    if ($d > 250) {
162      $d = 5;
163      $c++;
164    }
165  }
166  my $n = time();
167  my $diff = $n - $p;
168
169  print "Done adding $num_if VLAN interfaces in $diff seconds.\n";
170
171  sleep 2;
172}
173
174print "Removing VLAN interfaces 1 through $num_if\n";
175$d = 5;
176$c = 5;
177my $p = time();
178my $i;
179for ($i = 1; $i<=$num_if; $i++) {
180  `/usr/local/bin/vconfig rem vlan$i`;
181
182  $d++;
183  if ($d > 250) {
184    $d = 5;
185    $c++;
186  }
187}
188my $n = time();
189my $diff = $n - $p;
190print "Done deleting $num_if VLAN interfaces in $diff seconds.\n";
191
192sleep 2;
193
194
195if ($ARGV[0] ne "clean") {
196
197  my $tmp = $num_if / 4;
198  print "\nGoing to add and remove 2 interfaces $tmp times.\n";
199  $p = time();
200
201
202  for ($i = 1; $i<=$tmp; $i++) {
203    `/usr/local/bin/vconfig add eth0 1`;
204    `ifconfig vlan1 192.168.200.200`;
205    `ifconfig vlan1 up`;
206    `ifconfig vlan1 down`;
207
208    `/usr/local/bin/vconfig add eth0 2`;
209    `ifconfig vlan2 192.168.202.202`;
210    `ifconfig vlan2 up`;
211    `ifconfig vlan2 down`;
212
213    `/usr/local/bin/vconfig rem vlan2`;
214    `/usr/local/bin/vconfig rem vlan1`;
215  }
216  $n = time();
217  $diff = $n - $p;
218  print "Done adding/removing 2 VLAN interfaces $tmp times in $diff seconds.\n";
219}
220
221print "Re-installing /sbin/hotplug";
222`echo /sbin/hotplug > /proc/sys/kernel/hotplug`;
223