All Classes Namespaces Files Functions Pages
utils.tcl
Go to the documentation of this file.
1 ##
2 # @brief Basic namespace for all Excentis Tcl extensions
3 #
4 namespace eval excentis {
5 
6 ##
7 # @brief This namespace holds all ByteBlower-related Tcl extensions, including the ByteBlower higher-layer Tcl API.
8 #
9 # These procedures are developed for higher-level usage of the ByteBlower API.
10 # They are mainly used to simplify running specific Test Scenarios similar to
11 # scenarios you know from the ByteBlower GUI.
12 #
13 namespace eval ByteBlower {
14 
15 ## \file
16 #
17 # @brief Utility module that provides <b>various ByteBlower helper methods</b> to ease Tcl development.
18 #
19 # It contains utilities such as bulk @apiref{ByteBlowerPort} generation.
20 #
21 # @copyright Excentis nv - www.excentis.com
22 #
23 package require excentis_basic
24 
25 proc Generate.Ports.On.Switch { server trunk baseMac L3Config { ports all } } {
26 
27  # Method which will generate a ByteBlowerPort on each port of the trunking
28  # interface of the server. The ports will have a different mac address,
29  # starting with the provided base mac address. The same holds for the Layer 3 protocol, which
30  # can use DHCP as as an alternative.
31  # @param server ByteBlowerServer which will be used for the ByteBlowerPorts
32  # @param trunk Interface nr of the trunk. E.g. 1
33  # @param baseMac Start mac address for the ByteBlower ports.
34  # @param L3Config which has the following format : L3Protocol { baseIp dhcp } { defaultGw 0.0.0.0 } { netmask 255.255.255.0 }
35  # @param L3Protocol IPv4 or IPv6
36  # @param baseIp Start IP adddress for the ByteBlower ports. If Dhcp is used,
37  # just type dhcp or leave blank
38  # @param defaultGw Default gateway, if dhcp is not used.
39  # @param netmask Netmask is dhcp is not used.
40  # @return list of generated ByteBlower ports.
41  ##
42 
43  # Initialize L3Config
44  set L3Protocol IPv4
45  set baseIP dhcp
46  set defaultGw 0.0.0.0
47  set netmask 255.255.255.0
48  set portList [ list ]
49  # Parse the L3Config
50  if { [llength $L3Config] > 0} {
51  set L3Protocol [ lindex $L3Config 0 ]
52  if { [llength $L3Config] > 1} {
53  set baseIP [ lindex $L3Config 1 ]
54  }
55  if { [llength $L3Config] > 2} {
56  set defaultGw [ lindex $L3Config 2 ]
57  }
58  if { [llength $L3Config] > 3} {
59  set netmask [ lindex $L3Config 3 ]
60  }
61  }
62  # We walk the interfaces on the server, and all interfaces which are on the requested
63  # trunk will be used for generating a ByteBlowerPort.
64  foreach interface [ $server Interfaces.Get ] {
65  if { [string match trunk-$trunk-* $interface ]} {
66  set portNr [ lindex [ split $interface - ] 2 ]
67  if { [string equal $ports all] || [lsearch $ports $portNr ] != -1} {
68  if { [ catch {
69  # puts "Interface $portNr found"
70  # Ok, we found a matching interface.
71  set port [ $server Port.Create $interface ]
72  # Configure the port.
73  set baseMac [ ::excentis::basic::Mac.Increment $baseMac ]
74  [ $port Layer2.EthII.Set ] Mac.Set $baseMac
75  # Configure Layer 3.
76  if { [ string equal $L3Protocol IPv4 ] } {
77  set l3 [ $port Layer3.IPv4.Set ]
78  if { [string equal $baseIP dhcp ] } {
79  [ $l3 Protocol.Dhcp.Get ] Perform
80  } else {
81  set baseIP [ ::excentis::basic::IP.Increment $baseIP ]
82  $l3 Ip.Set $baseIP
83  $l3 Gateway.Set $defaultGw
84  $l3 Netmask.Set $netmask
85  }
86  } else {
87  set l3 [ $port Layer3.IPv6.Set ]
88  if { [string equal $baseIP dhcp ] } {
89  [ $l3 Protocol.Dhcp.Get ] Perform
90  } else {
91  set baseIP [ ::excentis::basic::IPv6.Increment $baseIP ]
92  $l3 Ip.Manual.Add $baseIP
93  # The netmask is ignored.
94  $l3 Gateway.Set $defaultGw
95  }
96  }
97  lappend portList $port
98  } errorObject ]} {
99  puts "Ignoring port $portNr"
100  lappend portList "null $errorObject"
101  }
102  } else {
103  # puts "Interface $portNr NOT found ( $interface )"
104  }
105  }
106  }
107  return $portList
108 }
109 
110 proc Generate.Possible.Ports.On.Switch { server trunk baseMac L3Config { ports all } } {
111  # Method which does the same @see Generate.Ports.On.Switch, but only on ports where no exception is thrown.
112  #
113  if {[string equal $ports all ]} {
114  # Generate a start list.
115  set interfaceList [ $server Interfaces.Get ]
116  set porst [ list ]
117  foreach interface $interfaceList {
118  if { [string match trunk-$trunk-* $interface]} {
119  lappend ports [ lindex [ split $interface - ] end ]
120  }
121  }
122  }
123  set result [ list ]
124  while { [llength $ports ] > 0 && [ catch { set result [ Generate.Ports.On.Switch $server $trunk $baseMac $L3Config $ports ] } errorObject ]} {
125  # Extract the port which caused the exception, remove it from the port list, and continue.
126  set port [ lindex [ split $errorObject - ] end ]
127  if { [string is digit $port]} {
128  puts "Removing port $port ($errorObject)"
129  set ports [ lremove $ports $port ]
130  } else {
131  error $errorObject
132  }
133  }
134  return $result
135 
136 }
137 
138 }
139 
140 }