1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2019 Mellanox Technologies, Ltd
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include "rte_eal.h"
#include "mlx5_utils.h"
#include "mlx5.h"
/* PMD socket service for tools. */
#define MLX5_SOCKET_PATH "/var/tmp/dpdk_net_mlx5_%d"
#define MLX5_ALL_PORT_IDS 0xffff
int server_socket = -1; /* Unix socket for primary process. */
struct rte_intr_handle *server_intr_handle; /* Interrupt handler. */
/**
* Handle server pmd socket interrupts.
*/
static void
mlx5_pmd_socket_handle(void *cb __rte_unused)
{
int conn_sock;
int ret;
struct cmsghdr *cmsg = NULL;
uint32_t data[MLX5_SENDMSG_MAX / sizeof(uint32_t)];
struct rte_flow *flow_ptr = NULL;
uint8_t buf[CMSG_SPACE(sizeof(int))] = { 0 };
struct iovec io = {
.iov_base = data,
.iov_len = sizeof(data),
};
struct msghdr msg = {
.msg_iov = &io,
.msg_iovlen = 1,
.msg_control = buf,
.msg_controllen = sizeof(buf),
};
uint32_t port_id;
int fd;
FILE *file = NULL;
struct rte_eth_dev *dev;
struct rte_flow_error err;
struct mlx5_flow_dump_req *dump_req;
struct mlx5_flow_dump_ack *dump_ack;
memset(data, 0, sizeof(data));
/* Accept the connection from the client. */
conn_sock = accept(server_socket, NULL, NULL);
if (conn_sock < 0) {
DRV_LOG(WARNING, "connection failed: %s", strerror(errno));
return;
}
ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
if (ret != sizeof(struct mlx5_flow_dump_req)) {
DRV_LOG(WARNING, "wrong message received: %s",
strerror(errno));
goto error;
}
/* Receive file descriptor. */
cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg == NULL || cmsg->cmsg_type != SCM_RIGHTS ||
cmsg->cmsg_len < sizeof(int)) {
DRV_LOG(WARNING, "invalid file descriptor message");
goto error;
}
memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
file = fdopen(fd, "w");
if (!file) {
DRV_LOG(WARNING, "Failed to open file");
goto error;
}
/* Receive port number. */
if (msg.msg_iovlen != 1 || msg.msg_iov->iov_len < sizeof(uint16_t)) {
DRV_LOG(WARNING, "wrong port number message");
goto error;
}
dump_req = (struct mlx5_flow_dump_req *)msg.msg_iov->iov_base;
if (dump_req) {
port_id = dump_req->port_id;
flow_ptr = (struct rte_flow *)((uintptr_t)dump_req->flow_id);
} else {
DRV_LOG(WARNING, "Invalid message");
goto error;
}
if (port_id == MLX5_ALL_PORT_IDS) {
/* Dump all port ids */
if (flow_ptr) {
DRV_LOG(WARNING, "Flow ptr unsupported with given port id");
goto error;
}
MLX5_ETH_FOREACH_DEV(port_id, NULL) {
dev = &rte_eth_devices[port_id];
ret = mlx5_flow_dev_dump(dev, NULL, file, &err);
if (ret)
break;
}
} else {
/* Dump single port id */
if (!rte_eth_dev_is_valid_port(port_id)) {
DRV_LOG(WARNING, "Invalid port %u", port_id);
goto error;
}
/* Dump flow */
dev = &rte_eth_devices[port_id];
ret = mlx5_flow_dev_dump(dev, flow_ptr, file, &err);
}
/* Set-up the ancillary data and reply. */
msg.msg_controllen = 0;
msg.msg_control = NULL;
msg.msg_iovlen = 1;
msg.msg_iov = &io;
dump_ack = (struct mlx5_flow_dump_ack *)data;
dump_ack->rc = -ret;
io.iov_len = sizeof(struct mlx5_flow_dump_ack);
io.iov_base = dump_ack;
do {
ret = sendmsg(conn_sock, &msg, 0);
} while (ret < 0 && errno == EINTR);
if (ret < 0)
DRV_LOG(WARNING, "failed to send response %s",
strerror(errno));
error:
if (conn_sock >= 0)
close(conn_sock);
if (file)
fclose(file);
}
/**
* Initialise the socket to communicate with external tools.
*
* @return
* 0 on success, a negative value otherwise.
*/
int
mlx5_pmd_socket_init(void)
{
struct sockaddr_un sun = {
.sun_family = AF_UNIX,
};
int ret;
int flags;
MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
if (server_socket != -1)
return 0;
ret = socket(AF_UNIX, SOCK_STREAM, 0);
if (ret < 0) {
DRV_LOG(WARNING, "Failed to open mlx5 socket: %s",
strerror(errno));
goto error;
}
server_socket = ret;
flags = fcntl(server_socket, F_GETFL, 0);
if (flags == -1)
goto close;
ret = fcntl(server_socket, F_SETFL, flags | O_NONBLOCK);
if (ret < 0)
goto close;
snprintf(sun.sun_path, sizeof(sun.sun_path), MLX5_SOCKET_PATH,
getpid());
remove(sun.sun_path);
ret = bind(server_socket, (const struct sockaddr *)&sun, sizeof(sun));
if (ret < 0) {
DRV_LOG(WARNING,
"cannot bind mlx5 socket: %s", strerror(errno));
goto remove;
}
ret = listen(server_socket, 0);
if (ret < 0) {
DRV_LOG(WARNING, "cannot listen on mlx5 socket: %s",
strerror(errno));
goto remove;
}
server_intr_handle = mlx5_os_interrupt_handler_create
(RTE_INTR_INSTANCE_F_PRIVATE, false,
server_socket, mlx5_pmd_socket_handle, NULL);
if (server_intr_handle == NULL) {
DRV_LOG(WARNING, "cannot register interrupt handler for mlx5 socket: %s",
strerror(errno));
goto remove;
}
return 0;
remove:
remove(sun.sun_path);
close:
claim_zero(close(server_socket));
server_socket = -1;
error:
DRV_LOG(ERR, "Cannot initialize socket: %s", strerror(errno));
return -errno;
}
/**
* Un-Initialize the pmd socket
*/
void
mlx5_pmd_socket_uninit(void)
{
if (server_socket == -1)
return;
mlx5_os_interrupt_handler_destroy(server_intr_handle,
mlx5_pmd_socket_handle, NULL);
claim_zero(close(server_socket));
server_socket = -1;
MKSTR(path, MLX5_SOCKET_PATH, getpid());
claim_zero(remove(path));
}
|