-
Notifications
You must be signed in to change notification settings - Fork 370
Add mdio IPC client library #1134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
1fd8a92
1db84cf
67715d2
69f9d23
d95c2bb
70462c7
9da4562
c789f84
39f8b59
fcde33b
1937037
042b5d6
5f4e7bc
8665f8c
e9f6483
cc08e19
519cb6b
3f820d7
11d9882
f0d52f9
fce22a9
4766672
12c2718
fe907f7
4c5a6e4
c175d8a
118e10d
d34f7ff
abfd217
84b8dbd
4e18bb3
51065d7
b3aa4a2
9d1f7fe
7f7c771
e3a33a0
0afecec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| usr/bin | ||
| usr/lib |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ usr/bin/saidiscovery | |
| usr/bin/saiasiccmp | ||
| usr/bin/syncd* | ||
| syncd/scripts/* usr/bin | ||
| usr/lib/*/libMdioIpcClient.so.* | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #! /usr/bin/dh-exec | ||
| /usr/lib/${DEB_HOST_MULTIARCH}/libMdioIpcClient.so.0 /usr/lib/${DEB_HOST_MULTIARCH}/libMdioIpcClient.so |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| /* Includes */ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <stdint.h> | ||
| #include <errno.h> | ||
| #include <string.h> | ||
| #include <sys/types.h> | ||
| #include <sys/stat.h> | ||
| #include <fcntl.h> | ||
| #include <sys/socket.h> | ||
| #include <sys/un.h> | ||
| #include <unistd.h> | ||
| #include <time.h> | ||
| #include <pthread.h> | ||
| #include <mutex> | ||
|
|
||
| #include "MdioIpcClient.h" | ||
| #include "MdioIpcCommon.h" | ||
|
|
||
| #include "swss/logger.h" | ||
|
|
||
| static std::mutex ipcMutex; | ||
|
|
||
| /* Global variables */ | ||
|
|
||
| static int syncd_mdio_ipc_command(char *cmd, char *resp) | ||
| { | ||
| // SWSS_LOG_ENTER(); // disabled | ||
|
|
||
| int fd; | ||
| ssize_t ret; | ||
| size_t len; | ||
| struct sockaddr_un saddr, caddr; | ||
| static int sock = 0; | ||
| static char path[128] = { 0 }; | ||
| static time_t timeout = 0; | ||
|
|
||
| if (timeout < time(NULL)) | ||
| { | ||
| /* It might already be timed out at the server side, reconnect ... */ | ||
| if (sock > 0) | ||
| { | ||
| close(sock); | ||
| } | ||
| sock = 0; | ||
| } | ||
|
|
||
| if (strlen(path) == 0) | ||
| { | ||
| strcpy(path, SYNCD_IPC_SOCK_SYNCD); | ||
| fd = open(path, O_DIRECTORY); | ||
| if (fd < 0) | ||
| { | ||
| SWSS_LOG_INFO("Program is not run on host\n"); | ||
| strcpy(path, SYNCD_IPC_SOCK_HOST); | ||
| fd = open(path, O_DIRECTORY); | ||
| if (fd < 0) | ||
| { | ||
| SWSS_LOG_ERROR("Unable to open the directory for IPC\n"); | ||
| return errno; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (sock <= 0) | ||
| { | ||
| sock = socket(AF_UNIX, SOCK_STREAM, 0); | ||
| if (sock < 0) | ||
| { | ||
| SWSS_LOG_ERROR("socket() :%s", strerror(errno)); | ||
| return errno; | ||
| } | ||
|
|
||
| caddr.sun_family = AF_UNIX; | ||
| snprintf(caddr.sun_path, sizeof(caddr.sun_path), "%s/%s.cli.%d", path, SYNCD_IPC_SOCK_FILE, getpid()); | ||
| unlink(caddr.sun_path); | ||
| if (bind(sock, (struct sockaddr *)&caddr, sizeof(caddr)) < 0) | ||
| { | ||
| SWSS_LOG_ERROR("bind() :%s", strerror(errno)); | ||
| close(sock); | ||
| sock = 0; | ||
| return errno; | ||
| } | ||
|
|
||
| saddr.sun_family = AF_UNIX; | ||
| snprintf(saddr.sun_path, sizeof(saddr.sun_path), "%s/%s.srv", path, SYNCD_IPC_SOCK_FILE); | ||
| if (connect(sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) | ||
| { | ||
| SWSS_LOG_ERROR("connect() :%s", strerror(errno)); | ||
| close(sock); | ||
| sock = 0; | ||
| unlink(caddr.sun_path); | ||
| return errno; | ||
| } | ||
| } | ||
|
|
||
| len = strlen(cmd); | ||
| ret = send(sock, cmd, len, 0); | ||
| if (ret < (ssize_t)len) | ||
| { | ||
| SWSS_LOG_ERROR("send failed, ret=%ld, expected=%ld\n", ret, len); | ||
| close(sock); | ||
| sock = 0; | ||
| unlink(caddr.sun_path); | ||
| return -EIO; | ||
| } | ||
|
|
||
| ret = recv(sock, resp, SYNCD_IPC_BUFF_SIZE - 1, 0); | ||
| if (ret <= 0) | ||
| { | ||
| SWSS_LOG_ERROR("recv failed, ret=%ld\n", ret); | ||
| close(sock); | ||
| sock = 0; | ||
| unlink(caddr.sun_path); | ||
| return -EIO; | ||
| } | ||
|
|
||
| timeout = time(NULL) + MDIO_CLIENT_TIMEOUT; | ||
| return (int)strtol(resp, NULL, 0); | ||
| } | ||
|
|
||
|
|
||
| /* Function to read data from MDIO interface */ | ||
| extern "C" sai_status_t mdio_read(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, | ||
| uint32_t number_of_registers, uint32_t *data) | ||
| { | ||
| // SWSS_LOG_ENTER(); // disabled | ||
|
|
||
| int rc = SAI_STATUS_FAILURE; | ||
| char cmd[SYNCD_IPC_BUFF_SIZE], resp[SYNCD_IPC_BUFF_SIZE]; | ||
|
|
||
| if (number_of_registers > 1) | ||
| { | ||
| SWSS_LOG_ERROR("Multiple register reads are not supported, num_of_registers: %d\n", number_of_registers); | ||
| return SAI_STATUS_FAILURE; | ||
| } | ||
|
|
||
| ipcMutex.lock(); | ||
|
|
||
| sprintf(cmd, "mdio 0x%x 0x%x\n", mdio_addr, reg_addr); | ||
| rc = syncd_mdio_ipc_command(cmd, resp); | ||
| if (rc == 0) | ||
| { | ||
| *data = (uint32_t)strtoul(strchrnul(resp, ' ') + 1, NULL, 0); | ||
| rc = SAI_STATUS_SUCCESS; | ||
| } | ||
| else | ||
| { | ||
| SWSS_LOG_ERROR("syncd_mdio_ipc_command returns : %d\n", rc); | ||
| } | ||
|
|
||
| ipcMutex.unlock(); | ||
| return rc; | ||
| } | ||
|
|
||
| /* Function to write data to MDIO interface */ | ||
| extern "C" sai_status_t mdio_write(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, | ||
| uint32_t number_of_registers, const uint32_t *data) | ||
| { | ||
| // SWSS_LOG_ENTER(); // disabled | ||
|
|
||
| int rc = SAI_STATUS_FAILURE; | ||
| char cmd[SYNCD_IPC_BUFF_SIZE], resp[SYNCD_IPC_BUFF_SIZE]; | ||
|
|
||
| if (number_of_registers > 1) | ||
| { | ||
| SWSS_LOG_ERROR("Multiple register reads are not supported, num_of_registers: %d\n", number_of_registers); | ||
| return SAI_STATUS_FAILURE; | ||
| } | ||
|
|
||
| ipcMutex.lock(); | ||
|
|
||
| sprintf(cmd, "mdio 0x%x 0x%x 0x%x\n", mdio_addr, reg_addr, *data); | ||
| rc = syncd_mdio_ipc_command(cmd, resp); | ||
| if (rc == 0) | ||
| { | ||
| rc = SAI_STATUS_SUCCESS; | ||
| } | ||
| else | ||
| { | ||
| SWSS_LOG_ERROR("syncd_mdio_ipc_command returns : %d\n", rc); | ||
| } | ||
|
|
||
| ipcMutex.unlock(); | ||
| return rc; | ||
| } | ||
|
|
||
| /* Function to read data using clause 22 from MDIO interface */ | ||
| extern "C" sai_status_t mdio_read_cl22(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, | ||
|
||
| uint32_t number_of_registers, uint32_t *data) | ||
| { | ||
| // SWSS_LOG_ENTER(); // disabled | ||
|
|
||
| int rc = SAI_STATUS_FAILURE; | ||
| char cmd[SYNCD_IPC_BUFF_SIZE], resp[SYNCD_IPC_BUFF_SIZE]; | ||
|
|
||
| if (number_of_registers > 1) | ||
| { | ||
| SWSS_LOG_ERROR("Multiple register reads are not supported, num_of_registers: %d\n", number_of_registers); | ||
| return SAI_STATUS_FAILURE; | ||
| } | ||
|
|
||
| ipcMutex.lock(); | ||
|
|
||
| sprintf(cmd, "mdio-cl22 0x%x 0x%x\n", mdio_addr, reg_addr); | ||
| rc = syncd_mdio_ipc_command(cmd, resp); | ||
| if (rc == 0) | ||
| { | ||
| *data = (uint32_t)strtoul(strchrnul(resp, ' ') + 1, NULL, 0); | ||
| rc = SAI_STATUS_SUCCESS; | ||
| } | ||
| else | ||
| { | ||
| SWSS_LOG_ERROR("syncd_mdio_ipc_command returns : %d\n", rc); | ||
| } | ||
|
|
||
| ipcMutex.unlock(); | ||
| return rc; | ||
| } | ||
|
|
||
| /* Function to write data using clause 22 to MDIO interface */ | ||
| extern "C" sai_status_t mdio_write_cl22(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, | ||
| uint32_t number_of_registers, const uint32_t *data) | ||
| { | ||
| // SWSS_LOG_ENTER(); // disabled | ||
|
|
||
| int rc = SAI_STATUS_FAILURE; | ||
| char cmd[SYNCD_IPC_BUFF_SIZE], resp[SYNCD_IPC_BUFF_SIZE]; | ||
|
|
||
| if (number_of_registers > 1) | ||
| { | ||
| SWSS_LOG_ERROR("Multiple register reads are not supported, num_of_registers: %d\n", number_of_registers); | ||
| return SAI_STATUS_FAILURE; | ||
| } | ||
|
|
||
| ipcMutex.lock(); | ||
|
|
||
| sprintf(cmd, "mdio-cl22 0x%x 0x%x 0x%x\n", mdio_addr, reg_addr, *data); | ||
| rc = syncd_mdio_ipc_command(cmd, resp); | ||
| if (rc == 0) | ||
| { | ||
| rc = SAI_STATUS_SUCCESS; | ||
| } | ||
| else | ||
| { | ||
| SWSS_LOG_ERROR("syncd_mdio_ipc_command returns : %d\n", rc); | ||
| } | ||
|
|
||
| ipcMutex.unlock(); | ||
| return rc; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If syncd_mdio_ipc_command(cmd, resp) < 0, for debugging, should at least add log for error code. Better to translate the error code to SAI status code on
rc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add error log as the else condition statement.