|
| 1 | +/* Includes */ |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <stdint.h> |
| 5 | +#include <errno.h> |
| 6 | +#include <string.h> |
| 7 | +#include <sys/types.h> |
| 8 | +#include <sys/stat.h> |
| 9 | +#include <fcntl.h> |
| 10 | +#include <sys/socket.h> |
| 11 | +#include <sys/un.h> |
| 12 | +#include <unistd.h> |
| 13 | +#include <time.h> |
| 14 | +#include <pthread.h> |
| 15 | +#include <mutex> |
| 16 | + |
| 17 | +#include "MdioIpcClient.h" |
| 18 | +#include "MdioIpcCommon.h" |
| 19 | + |
| 20 | +#include "swss/logger.h" |
| 21 | + |
| 22 | +static std::mutex ipcMutex; |
| 23 | + |
| 24 | +/* Global variables */ |
| 25 | + |
| 26 | +static int syncd_mdio_ipc_command(char *cmd, char *resp) |
| 27 | +{ |
| 28 | + // SWSS_LOG_ENTER(); // disabled |
| 29 | + |
| 30 | + int fd; |
| 31 | + ssize_t ret; |
| 32 | + size_t len; |
| 33 | + struct sockaddr_un saddr, caddr; |
| 34 | + static int sock = 0; |
| 35 | + static char path[128] = { 0 }; |
| 36 | + static time_t timeout = 0; |
| 37 | + |
| 38 | + if (timeout < time(NULL)) |
| 39 | + { |
| 40 | + /* It might already be timed out at the server side, reconnect ... */ |
| 41 | + if (sock > 0) |
| 42 | + { |
| 43 | + close(sock); |
| 44 | + } |
| 45 | + sock = 0; |
| 46 | + } |
| 47 | + |
| 48 | + if (strlen(path) == 0) |
| 49 | + { |
| 50 | + strcpy(path, SYNCD_IPC_SOCK_SYNCD); |
| 51 | + fd = open(path, O_DIRECTORY); |
| 52 | + if (fd < 0) |
| 53 | + { |
| 54 | + SWSS_LOG_INFO("Program is not run on host\n"); |
| 55 | + strcpy(path, SYNCD_IPC_SOCK_HOST); |
| 56 | + fd = open(path, O_DIRECTORY); |
| 57 | + if (fd < 0) |
| 58 | + { |
| 59 | + SWSS_LOG_ERROR("Unable to open the directory for IPC\n"); |
| 60 | + return errno; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (sock <= 0) |
| 66 | + { |
| 67 | + sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 68 | + if (sock < 0) |
| 69 | + { |
| 70 | + SWSS_LOG_ERROR("socket() :%s", strerror(errno)); |
| 71 | + return errno; |
| 72 | + } |
| 73 | + |
| 74 | + caddr.sun_family = AF_UNIX; |
| 75 | + snprintf(caddr.sun_path, sizeof(caddr.sun_path), "%s/%s.cli.%d", path, SYNCD_IPC_SOCK_FILE, getpid()); |
| 76 | + unlink(caddr.sun_path); |
| 77 | + if (bind(sock, (struct sockaddr *)&caddr, sizeof(caddr)) < 0) |
| 78 | + { |
| 79 | + SWSS_LOG_ERROR("bind() :%s", strerror(errno)); |
| 80 | + close(sock); |
| 81 | + sock = 0; |
| 82 | + return errno; |
| 83 | + } |
| 84 | + |
| 85 | + saddr.sun_family = AF_UNIX; |
| 86 | + snprintf(saddr.sun_path, sizeof(saddr.sun_path), "%s/%s.srv", path, SYNCD_IPC_SOCK_FILE); |
| 87 | + if (connect(sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) |
| 88 | + { |
| 89 | + SWSS_LOG_ERROR("connect() :%s", strerror(errno)); |
| 90 | + close(sock); |
| 91 | + sock = 0; |
| 92 | + unlink(caddr.sun_path); |
| 93 | + return errno; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + len = strlen(cmd); |
| 98 | + ret = send(sock, cmd, len, 0); |
| 99 | + if (ret < (ssize_t)len) |
| 100 | + { |
| 101 | + SWSS_LOG_ERROR("send failed, ret=%ld, expected=%ld\n", ret, len); |
| 102 | + close(sock); |
| 103 | + sock = 0; |
| 104 | + unlink(caddr.sun_path); |
| 105 | + return -EIO; |
| 106 | + } |
| 107 | + |
| 108 | + ret = recv(sock, resp, SYNCD_IPC_BUFF_SIZE - 1, 0); |
| 109 | + if (ret <= 0) |
| 110 | + { |
| 111 | + SWSS_LOG_ERROR("recv failed, ret=%ld\n", ret); |
| 112 | + close(sock); |
| 113 | + sock = 0; |
| 114 | + unlink(caddr.sun_path); |
| 115 | + return -EIO; |
| 116 | + } |
| 117 | + |
| 118 | + timeout = time(NULL) + MDIO_CLIENT_TIMEOUT; |
| 119 | + return (int)strtol(resp, NULL, 0); |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +/* Function to read data from MDIO interface */ |
| 124 | +sai_status_t mdio_read(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, |
| 125 | + uint32_t number_of_registers, uint32_t *data) |
| 126 | +{ |
| 127 | + // SWSS_LOG_ENTER(); // disabled |
| 128 | + |
| 129 | + int rc = SAI_STATUS_FAILURE; |
| 130 | + char cmd[SYNCD_IPC_BUFF_SIZE], resp[SYNCD_IPC_BUFF_SIZE]; |
| 131 | + |
| 132 | + if (number_of_registers > 1) |
| 133 | + { |
| 134 | + SWSS_LOG_ERROR("Multiple register reads are not supported, num_of_registers: %d\n", number_of_registers); |
| 135 | + return SAI_STATUS_FAILURE; |
| 136 | + } |
| 137 | + |
| 138 | + ipcMutex.lock(); |
| 139 | + |
| 140 | + sprintf(cmd, "mdio 0x%x 0x%x\n", mdio_addr, reg_addr); |
| 141 | + rc = syncd_mdio_ipc_command(cmd, resp); |
| 142 | + if (rc == 0) |
| 143 | + { |
| 144 | + *data = (uint32_t)strtoul(strchrnul(resp, ' ') + 1, NULL, 0); |
| 145 | + rc = SAI_STATUS_SUCCESS; |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + SWSS_LOG_ERROR("syncd_mdio_ipc_command returns : %d\n", rc); |
| 150 | + } |
| 151 | + |
| 152 | + ipcMutex.unlock(); |
| 153 | + return rc; |
| 154 | +} |
| 155 | + |
| 156 | +/* Function to write data to MDIO interface */ |
| 157 | +sai_status_t mdio_write(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, |
| 158 | + uint32_t number_of_registers, const uint32_t *data) |
| 159 | +{ |
| 160 | + // SWSS_LOG_ENTER(); // disabled |
| 161 | + |
| 162 | + int rc = SAI_STATUS_FAILURE; |
| 163 | + char cmd[SYNCD_IPC_BUFF_SIZE], resp[SYNCD_IPC_BUFF_SIZE]; |
| 164 | + |
| 165 | + if (number_of_registers > 1) |
| 166 | + { |
| 167 | + SWSS_LOG_ERROR("Multiple register reads are not supported, num_of_registers: %d\n", number_of_registers); |
| 168 | + return SAI_STATUS_FAILURE; |
| 169 | + } |
| 170 | + |
| 171 | + ipcMutex.lock(); |
| 172 | + |
| 173 | + sprintf(cmd, "mdio 0x%x 0x%x 0x%x\n", mdio_addr, reg_addr, *data); |
| 174 | + rc = syncd_mdio_ipc_command(cmd, resp); |
| 175 | + if (rc == 0) |
| 176 | + { |
| 177 | + rc = SAI_STATUS_SUCCESS; |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + SWSS_LOG_ERROR("syncd_mdio_ipc_command returns : %d\n", rc); |
| 182 | + } |
| 183 | + |
| 184 | + ipcMutex.unlock(); |
| 185 | + return rc; |
| 186 | +} |
| 187 | + |
| 188 | +/* Function to read data using clause 22 from MDIO interface */ |
| 189 | +sai_status_t mdio_read_cl22(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, |
| 190 | + uint32_t number_of_registers, uint32_t *data) |
| 191 | +{ |
| 192 | + // SWSS_LOG_ENTER(); // disabled |
| 193 | + |
| 194 | + int rc = SAI_STATUS_FAILURE; |
| 195 | + char cmd[SYNCD_IPC_BUFF_SIZE], resp[SYNCD_IPC_BUFF_SIZE]; |
| 196 | + |
| 197 | + if (number_of_registers > 1) |
| 198 | + { |
| 199 | + SWSS_LOG_ERROR("Multiple register reads are not supported, num_of_registers: %d\n", number_of_registers); |
| 200 | + return SAI_STATUS_FAILURE; |
| 201 | + } |
| 202 | + |
| 203 | + ipcMutex.lock(); |
| 204 | + |
| 205 | + sprintf(cmd, "mdio-cl22 0x%x 0x%x\n", mdio_addr, reg_addr); |
| 206 | + rc = syncd_mdio_ipc_command(cmd, resp); |
| 207 | + if (rc == 0) |
| 208 | + { |
| 209 | + *data = (uint32_t)strtoul(strchrnul(resp, ' ') + 1, NULL, 0); |
| 210 | + rc = SAI_STATUS_SUCCESS; |
| 211 | + } |
| 212 | + else |
| 213 | + { |
| 214 | + SWSS_LOG_ERROR("syncd_mdio_ipc_command returns : %d\n", rc); |
| 215 | + } |
| 216 | + |
| 217 | + ipcMutex.unlock(); |
| 218 | + return rc; |
| 219 | +} |
| 220 | + |
| 221 | +/* Function to write data using clause 22 to MDIO interface */ |
| 222 | +sai_status_t mdio_write_cl22(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, |
| 223 | + uint32_t number_of_registers, const uint32_t *data) |
| 224 | +{ |
| 225 | + // SWSS_LOG_ENTER(); // disabled |
| 226 | + |
| 227 | + int rc = SAI_STATUS_FAILURE; |
| 228 | + char cmd[SYNCD_IPC_BUFF_SIZE], resp[SYNCD_IPC_BUFF_SIZE]; |
| 229 | + |
| 230 | + if (number_of_registers > 1) |
| 231 | + { |
| 232 | + SWSS_LOG_ERROR("Multiple register reads are not supported, num_of_registers: %d\n", number_of_registers); |
| 233 | + return SAI_STATUS_FAILURE; |
| 234 | + } |
| 235 | + |
| 236 | + ipcMutex.lock(); |
| 237 | + |
| 238 | + sprintf(cmd, "mdio-cl22 0x%x 0x%x 0x%x\n", mdio_addr, reg_addr, *data); |
| 239 | + rc = syncd_mdio_ipc_command(cmd, resp); |
| 240 | + if (rc == 0) |
| 241 | + { |
| 242 | + rc = SAI_STATUS_SUCCESS; |
| 243 | + } |
| 244 | + else |
| 245 | + { |
| 246 | + SWSS_LOG_ERROR("syncd_mdio_ipc_command returns : %d\n", rc); |
| 247 | + } |
| 248 | + |
| 249 | + ipcMutex.unlock(); |
| 250 | + return rc; |
| 251 | +} |
0 commit comments