xref: /openbmc/openbmc-test-automation/lib/bmc_network_utils.robot (revision 3982974c06feee77be9f9874b7e4dac3e736f659)
1*** Settings ***
2Resource                ../lib/utils.robot
3Resource                ../lib/connection_client.robot
4Resource                ../lib/boot_utils.robot
5Library                 ../lib/gen_misc.py
6Library                 ../lib/utils.py
7
8*** Variables ***
9# MAC input from user.
10${MAC_ADDRESS}          ${EMPTY}
11
12
13*** Keywords ***
14
15Check And Reset MAC
16    [Documentation]  Update BMC with user input MAC address.
17    [Arguments]  ${mac_address}=${MAC_ADDRESS}
18
19    # Description of argument(s):
20    # mac_address  The mac address (e.g. 00:01:6c:80:02:28).
21
22    Should Not Be Empty  ${mac_address}
23    Open Connection And Log In
24    ${bmc_mac_addr}=  Execute Command On BMC  cat /sys/class/net/eth0/address
25    Run Keyword If  '${mac_address.lower()}' != '${bmc_mac_addr.lower()}'
26    ...  Set MAC Address
27
28
29Set MAC Address
30    [Documentation]  Update eth0 with input MAC address.
31    [Arguments]  ${mac_address}=${MAC_ADDRESS}
32
33    # Description of argument(s):
34    # mac_address  The mac address (e.g. 00:01:6c:80:02:28).
35
36    Write  fw_setenv ethaddr ${mac_address}
37    OBMC Reboot (off)
38
39    # Take SSH session post BMC reboot.
40    Open Connection And Log In
41    ${bmc_mac_addr}=  Execute Command On BMC  cat /sys/class/net/eth0/address
42    Should Be Equal  ${bmc_mac_addr}  ${mac_address}  ignore_case=True
43
44
45Get BMC IP Info
46    [Documentation]  Get system IP address and prefix length.
47
48    Open Connection And Login
49
50    # Get system IP address and prefix length details using "ip addr"
51    # Sample Output of "ip addr":
52    # 1: eth0: <BROADCAST,MULTIAST> mtu 1500 qdisc mq state UP qlen 1000
53    #     link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
54    #     inet xx.xx.xx.xx/24 brd xx.xx.xx.xx scope global eth0
55
56    ${cmd_output}=  Execute Command On BMC  /sbin/ip addr | grep eth0
57
58    # Get line having IP address details.
59    ${lines}=  Get Lines Containing String  ${cmd_output}  inet
60
61    # List IP address details.
62    @{ip_components}=  Split To Lines  ${lines}
63
64    @{ip_data}=  Create List
65
66    # Get all IP addresses and prefix lengths on system.
67    :FOR  ${ip_component}  IN  @{ip_components}
68    \  @{if_info}=  Split String  ${ip_component}
69    \  ${ip_n_prefix}=  Get From List  ${if_info}  1
70    \  Append To List  ${ip_data}  ${ip_n_prefix}
71
72    [Return]  ${ip_data}
73
74Get BMC Route Info
75    [Documentation]  Get system route info.
76
77    Open Connection And Login
78
79    # Sample output of "ip route":
80    # default via xx.xx.xx.x dev eth0
81    # xx.xx.xx.0/23 dev eth0  src xx.xx.xx.xx
82    # xx.xx.xx.0/24 dev eth0  src xx.xx.xx.xx
83
84    ${cmd_output}=  Execute Command On BMC  /sbin/ip route
85
86    [Return]  ${cmd_output}
87
88# TODO: openbmc/openbmc-test-automation#1331
89Get BMC MAC Address
90    [Documentation]  Get system MAC address.
91
92    Open Connection And Login
93
94    # Sample output of "ip addr | grep ether":
95    # link/ether xx.xx.xx.xx.xx.xx brd ff:ff:ff:ff:ff:ff
96
97    ${cmd_output}=  Execute Command On BMC  /sbin/ip addr | grep ether
98
99    # Split the line and return MAC address.
100    # Split list data:
101    # link/ether | xx:xx:xx:xx:xx:xx | brd | ff:ff:ff:ff:ff:ff
102
103    @{words}=  Split String  ${cmd_output}
104
105    [Return]  ${words[1]}
106
107
108Get BMC MAC Address List
109    [Documentation]  Get system MAC address
110
111    # Sample output of "ip addr | grep ether":
112    # link/ether xx.xx.xx.xx.xx.xx brd ff:ff:ff:ff:ff:ff
113
114    ${cmd_output}=  BMC Execute Command  /sbin/ip addr | grep ether
115
116    # Split the line and return MAC address.
117    # Split list data:
118    # link/ether | xx:xx:xx:xx:xx:xx | brd | ff:ff:ff:ff:ff:ff
119    # link/ether | xx:xx:xx:xx:xx:xx | brd | ff:ff:ff:ff:ff:ff
120
121    ${mac_list}=  Create List
122    @{lines}=  Split To Lines  ${cmd_output[0]}
123    :FOR  ${line}  IN  @{lines}
124    \  @{words}=  Split String  ${line}
125    \   Append To List  ${mac_list}  ${words[1]}
126
127    [Return]  ${mac_list}
128
129Get BMC Hostname
130    [Documentation]  Get BMC hostname.
131
132    # Sample output of  "hostnamectl":
133    #   Static hostname: xxyyxxyyxx
134    #         Icon name: computer
135    #        Machine ID: 6939927dc0db409ea09289d5b56eef08
136    #           Boot ID: bb806955fd904d47b6aa4bc7c34df482
137    #  Operating System: Phosphor OpenBMC (xxx xx xx) v1.xx.x-xx
138    #            Kernel: Linux 4.10.17-d6ae40dc4c4dff3265cc254d404ed6b03fcc2206
139    #      Architecture: arm
140
141    ${output}=  Execute Command on BMC  hostnamectl | grep hostname
142
143    [Return]  ${output}
144
145Get List Of IP Address Via REST
146    [Documentation]  Get list of IP address via REST.
147    [Arguments]  @{ip_uri_list}
148
149    # Description of argument(s):
150    # ip_uri_list  List of IP objects.
151    # Example:
152    #   "data": [
153    #     "/xyz/openbmc_project/network/eth0/ipv4/e9767624",
154    #     "/xyz/openbmc_project/network/eth0/ipv4/31f4ce8b"
155    #   ],
156
157    ${ip_list}=  Create List
158
159    : FOR  ${ip_uri}  IN  @{ip_uri_list}
160    \  ${ip_addr}=  Read Attribute  ${ip_uri}  Address
161    \  Append To List  ${ip_list}  ${ip_addr}
162
163    [Return]  @{ip_list}
164
165Delete IP And Object
166    [Documentation]  Delete IP and object.
167    [Arguments]  ${ip_addr}  @{ip_uri_list}
168
169    # Description of argument(s):
170    # ip_addr      IP address to be deleted.
171    # ip_uri_list  List of IP object URIs.
172
173    # Find IP object having this IP address.
174
175    : FOR  ${ip_uri}  IN  @{ip_uri_list}
176    \  ${ip_addr1}=  Read Attribute  ${ip_uri}  Address
177    \  Run Keyword If  '${ip_addr}' == '${ip_addr1}'  Exit For Loop
178
179    # If the given IP address is not configured, return.
180    # Otherwise, delete the IP and object.
181
182    Run Keyword And Return If  '${ip_addr}' != '${ip_addr1}'
183    ...  Pass Execution  IP address to be deleted is not configured.
184
185    Run Keyword And Ignore Error  OpenBMC Delete Request  ${ip_uri}
186
187    # After any modification on network interface, BMC restarts network
188    # module, wait until it is reachable.
189
190    Wait For Host To Ping  ${OPENBMC_HOST}  ${NETWORK_RETRY_TIME}
191    ...  ${NETWORK_TIMEOUT}
192
193    # Verify whether deleted IP address is removed from BMC system.
194
195    ${ip_data}=  Get BMC IP Info
196    Should Not Contain Match  ${ip_data}  ${ip_addr}*
197    ...  msg=IP address not deleted.
198
199Get First Non Pingable IP From Subnet
200    [Documentation]  Find first non-pingable IP from the subnet and return it.
201    [Arguments]  ${host}=${OPENBMC_HOST}
202
203    # Description of argument(s):
204    # host  Any valid host name or IP address
205    #       (e.g. "machine1" or "9.xx.xx.31").
206
207    # Non-pingable IP is unused IP address in the subnet.
208    ${host_name}  ${ip_addr}=  Get Host Name IP
209
210    # Split IP address into network part and host part.
211    # IP address will have 4 octets xx.xx.xx.xx.
212    # Sample output after split:
213    # split_ip  [xx.xx.xx, xx]
214
215    ${split_ip}=  Split String From Right  ${ip_addr}  .  1
216    # First element in list is Network part.
217    ${network_part}=  Get From List  ${split_ip}  0
218
219    : FOR  ${octet4}  IN RANGE  1  255
220    \  ${new_ip}=  Catenate  ${network_part}.${octet4}
221    \  ${status}=  Run Keyword And Return Status  Ping Host  ${new_ip}
222    # If IP is non-pingable, return it.
223    \  Return From Keyword If  '${status}' == 'False'  ${new_ip}
224
225    Fail  msg=No non-pingable IP could be found in subnet ${network_part}.
226
227
228Validate MAC On BMC
229    [Documentation]  Validate MAC on BMC.
230    [Arguments]  ${mac_addr}
231
232    # Description of argument(s):
233    # mac_addr  MAC address of the BMC.
234
235    ${system_mac}=  Get BMC MAC Address
236
237    ${status}=  Compare MAC Address  ${system_mac}  ${mac_addr}
238    Should Be True  ${status}
239    ...  msg=MAC address ${system_mac} does not match ${mac_addr}.
240
241
242Run Build Net
243    [Documentation]  Run build_net to preconfigure the ethernet interfaces.
244
245    OS Execute Command  build_net help y y
246    # Run pingum to chech if the "build_net" was run correctly done.
247    ${output}  ${stderr}  ${rc}=  OS Execute Command  pingum
248    Should Contain  ${output}  All networks ping Ok