1*** Settings ***
2Documentation   This module is for IPMI client for copying ipmitool to
3...             openbmc box and execute ipmitool IPMI standard
4...             command. IPMI raw command will use dbus-send command
5Resource        ../lib/resource.txt
6Resource        ../lib/connection_client.robot
7Resource        ../lib/utils.robot
8Resource        ../lib/state_manager.robot
9
10Library         String
11
12*** Variables ***
13${dbusHostIpmicmd1}=   dbus-send --system  ${OPENBMC_BASE_URI}HostIpmi/1
14${dbusHostIpmiCmdReceivedMsg}=   ${OPENBMC_BASE_DBUS}.HostIpmi.ReceivedMessage
15${netfnByte}=          ${EMPTY}
16${cmdByte}=            ${EMPTY}
17${arrayByte}=          array:byte:
18${IPMI_EXT_CMD}=       ipmitool -I lanplus -C 3
19${IPMI_USER_OPTIONS}   ${EMPTY}
20${IPMI_INBAND_CMD}=    ipmitool -C 3
21${HOST}=               -H
22${RAW}=                raw
23
24*** Keywords ***
25
26Run IPMI Command
27    [Arguments]  ${args}
28    ${resp}=  Run Keyword If  '${IPMI_COMMAND}' == 'External'
29    ...  Run External IPMI RAW Command  ${args}
30    ...  ELSE IF  '${IPMI_COMMAND}' == 'Inband'
31    ...  Run Inband IPMI RAW Command  ${args}
32    ...  ELSE IF  '${IPMI_COMMAND}' == 'Dbus'
33    ...  Run Dbus IPMI RAW Command  ${args}
34    ...  ELSE  Fail  msg=Invalid IPMI Command type provided : ${IPMI_COMMAND}
35    [Return]  ${resp}
36
37Run IPMI Standard Command
38    [Arguments]  ${args}
39    ${resp}=  Run Keyword If  '${IPMI_COMMAND}' == 'External'
40    ...  Run External IPMI Standard Command  ${args}
41    ...  ELSE IF  '${IPMI_COMMAND}' == 'Inband'
42    ...  Run Inband IPMI Standard Command  ${args}
43    ...  ELSE IF  '${IPMI_COMMAND}' == 'Dbus'
44    ...  Run Dbus IPMI Standard Command  ${args}
45    ...  ELSE  Fail  msg=Invalid IPMI Command type provided : ${IPMI_COMMAND}
46
47    [Return]  ${resp}
48
49Run Dbus IPMI RAW Command
50    [Arguments]    ${args}
51    ${valueinBytes}=   Byte Conversion  ${args}
52    ${cmd}=   Catenate   ${dbushostipmicmd1} ${dbusHostIpmiCmdReceivedMsg}
53    ${cmd}=   Catenate   ${cmd} ${valueinBytes}
54    ${output}   ${stderr}=  Execute Command  ${cmd}  return_stderr=True
55    Should Be Empty      ${stderr}
56    set test variable    ${OUTPUT}     "${output}"
57
58Run Dbus IPMI Standard Command
59    [Arguments]    ${args}
60    Copy ipmitool
61    ${stdout}    ${stderr}    ${output}=  Execute Command
62    ...    /tmp/ipmitool -I dbus ${args}    return_stdout=True
63    ...    return_stderr= True    return_rc=True
64    Should Be Equal    ${output}    ${0}    msg=${stderr}
65    [Return]    ${stdout}
66
67Run Inband IPMI Raw Command
68    [Documentation]  Run Inband IPMI raw command.
69    [Arguments]  ${args}  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
70    ...          ${os_password}=${OS_PASSWORD}
71
72    # Desription of arguments:
73    # ${args}  parameters to IPMI command.
74    # ${os_host} IP address of the OS Host.
75    # ${os_username}  OS Host Login user name.
76    # ${os_password}  OS Host Login passwrd.
77
78    Login To OS Host  ${os_host}  ${os_username}  ${os_password}
79    Check If IPMI Tool Exist
80
81    ${inband_raw_cmd}=  Catenate  ${IPMI_INBAND_CMD}  ${RAW}  ${args}
82    ${stdout}  ${stderr}=  Execute Command  ${inband_raw_cmd}  return_stderr=True
83    Should Be Empty  ${stderr}  msg=${stdout}
84    [Return]  ${stdout}
85
86Run Inband IPMI Standard Command
87    [Documentation]  Run Inband IPMI standard command.
88    [Arguments]  ${args}  ${os_host}=${OS_HOST}  ${os_username}=${OS_USERNAME}
89    ...          ${os_password}=${OS_PASSWORD}
90
91    # Desription of arguments:
92    # ${args}  parameters to IPMI command.
93    # ${os_host} IP address of the OS Host.
94    # ${os_username}  OS Host Login user name.
95    # ${os_password}  OS Host Login passwrd.
96
97    Login To OS Host  ${os_host}  ${os_username}  ${os_password}
98    Check If IPMI Tool Exist
99
100    ${inband_std_cmd}=  Catenate  ${IPMI_INBAND_CMD}  ${args}
101    ${stdout}  ${stderr}=  Execute Command  ${inband_std_cmd}  return_stderr=True
102    Should Be Empty  ${stderr}  msg=${stdout}
103    [Return]  ${stdout}
104
105Run External IPMI RAW Command
106    [Arguments]    ${args}
107    ${ipmi_raw_cmd}=   Catenate  SEPARATOR=
108    ...    ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}
109    ...    ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}${RAW}${SPACE}${args}
110    ${rc}    ${output}=    Run and Return RC and Output    ${ipmi_raw_cmd}
111    Should Be Equal    ${rc}    ${0}    msg=${output}
112    [Return]    ${output}
113
114Run External IPMI Standard Command
115    [Arguments]    ${args}
116
117    ${ipmi_cmd}=  Catenate  SEPARATOR=
118    ...  ${IPMI_EXT_CMD} ${IPMI_USER_OPTIONS} -P${SPACE}${IPMI_PASSWORD}
119    ...  ${SPACE}${HOST}${SPACE}${OPENBMC_HOST}${SPACE}${args}
120    ${rc}  ${output}=  Run And Return RC and Output  ${ipmi_cmd}
121    Should Be Equal  ${rc}  ${0}  msg=${output}
122    [Return]  ${output}
123
124Check If IPMI Tool Exist
125    [Documentation]  Check if IPMI Tool installed or not.
126    ${output}=  Execute Command  which ipmitool
127    Should Not Be Empty  ${output}  msg=ipmitool not installed.
128
129
130Activate SOL Via IPMI
131    [Documentation]  Start SOL using IPMI and route output to a file.
132    [Arguments]  ${file_path}=/tmp/sol_${OPENBMC_HOST}
133    # Description of argument(s):
134    # file_path  The file path on the local machine (vs OBMC) to collect SOL
135    #            output. By default SOL output is collected at
136    #            /tmp/sol_<BMC_IP> else user input location.
137
138    ${ipmi_cmd}=  Catenate  SEPARATOR=
139    ...  ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}
140    ...  ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}sol activate
141
142    Start Process  ${ipmi_cmd}  shell=True  stdout=${file_path}
143    ...  alias=sol_proc
144
145
146Deactivate SOL Via IPMI
147    [Documentation]  Stop SOL using IPMI and return SOL output.
148    [Arguments]  ${file_path}=/tmp/sol_${OPENBMC_HOST}
149    # Description of argument(s):
150    # file_path  The file path on the local machine to copy SOL output
151    #            collected by above "Activate SOL Via IPMI" keyword.
152    #            By default it copies log from /tmp/sol_<BMC_IP>.
153
154    ${ipmi_cmd}=  Catenate  SEPARATOR=
155    ...  ${IPMI_EXT_CMD} -P${SPACE}${IPMI_PASSWORD}${SPACE}
156    ...  ${HOST}${SPACE}${OPENBMC_HOST}${SPACE}sol deactivate
157
158    ${rc}  ${output}=  Run and Return RC and Output  ${ipmi_cmd}
159    Run Keyword If  ${rc} > 0  Run Keywords
160    ...  Terminate Process  sol_proc  AND  Return From Keyword  ${output}
161
162    ${rc}  ${output}=  Run and Return RC and Output  cat ${file_path}
163    Should Be Equal  ${rc}  ${0}  msg=${output}
164
165    # Logging SOL output for debug purpose.
166    Log  ${output}
167
168    [Return]  ${output}
169
170
171Byte Conversion
172    [Documentation]   Byte Conversion method receives IPMI RAW commands as
173    ...               argument in string format.
174    ...               Sample argument is as follows
175    ...               "0x04 0x30 9 0x01 0x00 0x35 0x00 0x00 0x00 0x00 0x00
176    ...               0x00"
177    ...               IPMI RAW command format is as follows
178    ...               <netfn Byte> <cmd Byte> <Data Bytes..>
179    ...               This method converts IPMI command format into
180    ...               dbus command format  as follows
181    ...               <byte:seq-id> <byte:netfn> <byte:lun> <byte:cmd>
182    ...               <array:byte:data>
183    ...               Sample dbus  Host IPMI Received Message argument
184    ...               byte:0x00 byte:0x04 byte:0x00 byte:0x30
185    ...               array:byte:9,0x01,0x00,0x35,0x00,0x00,0x00,0x00,0x00,0x00
186    [Arguments]     ${args}
187    ${argLength}=   Get Length  ${args}
188    Set Global Variable  ${arrayByte}   array:byte:
189    @{listargs}=   Split String  ${args}
190    ${index}=   Set Variable   ${0}
191    :FOR   ${word}   in   @{listargs}
192    \    Run Keyword if   ${index} == 0   Set NetFn Byte  ${word}
193    \    Run Keyword if   ${index} == 1   Set Cmd Byte    ${word}
194    \    Run Keyword if   ${index} > 1    Set Array Byte  ${word}
195    \    ${index}=    Set Variable    ${index + 1}
196    ${length}=   Get Length  ${arrayByte}
197    ${length}=   Evaluate  ${length} - 1
198    ${arrayByteLocal}=  Get Substring  ${arrayByte}  0   ${length}
199    Set Global Variable  ${arrayByte}   ${arrayByteLocal}
200    ${valueinBytesWithArray}=   Catenate  byte:0x00   ${netfnByte}  byte:0x00
201    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray}  ${cmdByte}
202    ${valueinBytesWithArray}=   Catenate  ${valueinBytesWithArray} ${arrayByte}
203    ${valueinBytesWithoutArray}=   Catenate  byte:0x00 ${netfnByte}  byte:0x00
204    ${valueinBytesWithoutArray}=   Catenate  ${valueinBytesWithoutArray} ${cmdByte}
205#   To Check scenario for smaller IPMI raw commands with only 2 arguments
206#   instead of usual 12 arguments.
207#   Sample small IPMI raw command: Run IPMI command 0x06 0x36
208#   If IPMI raw argument length is only 9 then return value in bytes without
209#   array population.
210#   Equivalent dbus-send argument for smaller IPMI raw command:
211#   byte:0x00 byte:0x06 byte:0x00 byte:0x36
212    Run Keyword if   ${argLength} == 9     Return from Keyword    ${valueinBytesWithoutArray}
213    [Return]    ${valueinBytesWithArray}
214
215
216Set NetFn Byte
217   [Arguments]    ${word}
218   ${netfnByteLocal}=  Catenate   byte:${word}
219   Set Global Variable  ${netfnByte}  ${netfnByteLocal}
220
221Set Cmd Byte
222   [Arguments]    ${word}
223   ${cmdByteLocal}=  Catenate   byte:${word}
224   Set Global Variable  ${cmdByte}  ${cmdByteLocal}
225
226Set Array Byte
227   [Arguments]    ${word}
228   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByte}  ${word}
229   ${arrayByteLocal}=   Catenate   SEPARATOR=  ${arrayByteLocal}   ,
230   Set Global Variable  ${arrayByte}   ${arrayByteLocal}
231
232Copy ipmitool
233    OperatingSystem.File Should Exist   tools/ipmitool      msg=The ipmitool program could not be found in the tools directory. It is not part of the automation code by default. You must manually copy or link the correct openbmc version of the tool in to the tools directory in order to run this test suite.
234
235    Import Library      SCPLibrary      WITH NAME       scp
236    scp.Open connection     ${OPENBMC_HOST}     username=${OPENBMC_USERNAME}      password=${OPENBMC_PASSWORD}
237    scp.Put File    tools/ipmitool   /tmp
238    SSHLibrary.Open Connection     ${OPENBMC_HOST}
239    Login   ${OPENBMC_USERNAME}    ${OPENBMC_PASSWORD}
240    Execute Command     chmod +x /tmp/ipmitool
241
242Initiate Host Boot Via External IPMI
243    [Documentation]  Initiate host power on using external IPMI.
244    [Arguments]  ${wait}=${1}
245    # Description of argument(s):
246    # wait  Indicates that this keyword should wait for host running state.
247
248    ${output}=  Run External IPMI Standard Command  chassis power on
249    Should Not Contain  ${output}  Error
250
251    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
252    Wait Until Keyword Succeeds  10 min  10 sec  Is Host Running
253
254Initiate Host PowerOff Via External IPMI
255    [Documentation]  Initiate host power off using external IPMI.
256    [Arguments]  ${wait}=${1}
257    # Description of argument(s):
258    # wait  Indicates that this keyword should wait for host off state.
259
260    ${output}=  Run External IPMI Standard Command  chassis power off
261    Should Not Contain  ${output}  Error
262
263    Run Keyword If  '${wait}' == '${0}'  Return From Keyword
264    Wait Until Keyword Succeeds  3 min  10 sec  Is Host Off
265
266Get Host State Via External IPMI
267    [Documentation]  Returns host state using external IPMI.
268
269    ${output}=  Run External IPMI Standard Command  chassis power status
270    Should Not Contain  ${output}  Error
271    ${output}=  Fetch From Right  ${output}  ${SPACE}
272
273    [Return]  ${output}
274