-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better error handling in utils, and reduce logging in GetDevices (#340)
* Change to use Go Dial api for ssh, for better error messages, when ACC to IMC connection is down. Make utils apis more go based. Per redhat JIRA: Also reduce excessive logging in GetDevices(which gets called periodically by DPU). Fix debug erorr messages, if ssh fails from ACC to IMC, We will throw error message in RunCmdOnImc
- Loading branch information
1 parent
56622a7
commit bc3f554
Showing
6 changed files
with
86 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,13 +26,15 @@ import ( | |
|
||
"github.com/intel/ipu-opi-plugins/ipu-plugin/pkg/types" | ||
log "github.com/sirupsen/logrus" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
const ( | ||
vsiToVportOffset = 16 | ||
pbPythonEnvVar = "PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python" | ||
hostToImcIpAddr = "100.0.0.100" | ||
accToImcIpAddr = "192.168.0.1" | ||
imcAddress = "192.168.0.1:22" | ||
) | ||
|
||
var execCommand = exec.Command | ||
|
@@ -119,48 +121,95 @@ func ExecuteScript(script string) (string, error) { | |
} | ||
|
||
func ImcQueryfindVsiGivenMacAddr(mode string, mac string) (string, error) { | ||
var ipAddr string | ||
if mode == types.HostMode { | ||
ipAddr = hostToImcIpAddr | ||
} else if mode == types.IpuMode { | ||
ipAddr = accToImcIpAddr | ||
if (mode == types.HostMode) || (mode != types.IpuMode) { | ||
log.Errorf("ImcQueryfindVsiGivenMacAddr: invalid mode-%v. access from host to IMC, not supported", mode) | ||
return "", fmt.Errorf("ImcQueryfindVsiGivenMacAddr: invalid mode-%v. access from host to IMC, not supported", mode) | ||
} | ||
|
||
runCommand := fmt.Sprintf(`ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@"%s" "/usr/bin/cli_client -cq" \ | ||
| awk '{if(($17 == "%s")) {print $8}}'`, ipAddr, mac) | ||
commands := fmt.Sprintf(`set -o pipefail && cli_client -cq | awk '{if(($17 == "%s")) {print $8}}'`, mac) | ||
outputBytes, err := RunCmdOnImc(commands) | ||
|
||
output, err := ExecuteScript(runCommand) | ||
output = strings.TrimSpace(string(output)) | ||
|
||
if err != nil || output == "" { | ||
log.Errorf("unable to reach IMC %v or null output->%v", err, output) | ||
return "", fmt.Errorf("unable to reach IMC %v or null output->%v", err, output) | ||
//Handle case where command ran without error, but empty output, due to config issue. | ||
if (err != nil) || (len(outputBytes) == 0) { | ||
log.Errorf("ImcQueryfindVsiGivenMacAddr: Error %v, from RunCmdOnImc (OR) empty (output)-%v", err, len(outputBytes)) | ||
return "", fmt.Errorf("ImcQueryfindVsiGivenMacAddr: Error %v, from RunCmdOnImc (OR) empty (output)-%v", err, len(outputBytes)) | ||
} | ||
return output, nil | ||
|
||
outputStr := strings.TrimSpace(string(outputBytes)) | ||
log.Infof("ImcQueryfindVsiGivenMacAddr: %s, len(output)-%v", outputStr, len(outputStr)) | ||
|
||
return outputStr, err | ||
} | ||
|
||
func GetVfMacList() ([]string, error) { | ||
// reach out to the IMC to get the mac addresses of the VFs | ||
output, err := ExecuteScript(`ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 [email protected] "/usr/bin/cli_client -cq" \ | ||
| awk '{if(($4 == "0x0") && ($6 == "yes")) {print $17}}'`) | ||
func RunCmdOnImc(cmd string) ([]byte, error) { | ||
|
||
config := &ssh.ClientConfig{ | ||
User: "root", | ||
Auth: []ssh.AuthMethod{ | ||
ssh.Password(""), | ||
}, | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), | ||
} | ||
// Connect to the remote server. | ||
client, err := ssh.Dial("tcp", imcAddress, config) | ||
if err != nil { | ||
log.Errorf("failed to dial remote server(%s): %s", imcAddress, err) | ||
return nil, fmt.Errorf("failed to dial remote server(%s): %s", imcAddress, err) | ||
} | ||
defer client.Close() | ||
|
||
// Start a session. | ||
session, err := client.NewSession() | ||
if err != nil { | ||
log.Errorf("failed to create ssh session: %s", err) | ||
return nil, fmt.Errorf("failed to create ssh session: %s", err) | ||
} | ||
defer session.Close() | ||
|
||
// Run a command on the remote server and capture the output. | ||
outputBytes, err := session.CombinedOutput(cmd) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to reach the IMC %v", err) | ||
log.Errorf("cmd error: %s", err) | ||
return nil, fmt.Errorf("cmd error: %s", err) | ||
} | ||
|
||
return strings.Split(strings.TrimSpace(output), "\n"), nil | ||
return outputBytes, nil | ||
|
||
} | ||
|
||
func GetAccApfMacList() ([]string, error) { | ||
// reach out to the IMC to get the mac addresses of the VFs | ||
output, err := ExecuteScript(`ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 [email protected] "/usr/bin/cli_client -cq" \ | ||
| awk '{if(($2 == "0x4") && ($4 == "0x4")) {print $17}}'`) | ||
commands := `set -o pipefail && cli_client -cq | awk '{if(($2 == "0x4") && ($4 == "0x4")) {print $17}}'` | ||
outputBytes, err := RunCmdOnImc(commands) | ||
|
||
var outputStr []string | ||
//Handle case where command ran without error, but empty output, due to config issue. | ||
if (err != nil) || (len(outputBytes) == 0) { | ||
log.Errorf("GetAccApfMacList: Error %v, from RunCmdOnImc (OR) empty (output)-%v", err, len(outputBytes)) | ||
return outputStr, fmt.Errorf("GetAccApfMacList: Error %v, from RunCmdOnImc (OR) empty (output)-%v", err, len(outputBytes)) | ||
} | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("unable to reach the IMC %v", err) | ||
outputStr = strings.Split(strings.TrimSpace(string(outputBytes)), "\n") | ||
log.Infof("GetAccApfMacList: %s, len(output)-%v", outputStr, len(outputStr)) | ||
|
||
return outputStr, err | ||
} | ||
|
||
func GetVfMacList() ([]string, error) { | ||
// reach out to the IMC to get the mac addresses of the VFs | ||
commands := `set -o pipefail && cli_client -cq | awk '{if(($4 == "0x0") && ($6 == "yes")) {print $17}}'` | ||
outputBytes, err := RunCmdOnImc(commands) | ||
|
||
var outputStr []string | ||
//Handle case where command ran without error, but empty output, due to config issue. | ||
if (err != nil) || (len(outputBytes) == 0) { | ||
log.Errorf("GetVfMacList: Error %v, from RunCmdOnImc (OR) empty (output)-%v", err, len(outputBytes)) | ||
return outputStr, fmt.Errorf("GetVfMacList: Error %v, from RunCmdOnImc (OR) empty (output)-%v", err, len(outputBytes)) | ||
} | ||
|
||
return strings.Split(strings.TrimSpace(output), "\n"), nil | ||
outputStr = strings.Split(strings.TrimSpace(string(outputBytes)), "\n") | ||
log.Infof("GetVfMacList: %s, len(output)-%v", outputStr, len(outputStr)) | ||
|
||
return outputStr, err | ||
} | ||
|
||
// Taken from the IPDK k8s-infra-offload project instead of including the full project as a module | ||
|