2018-01-29 13:11:30 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
* Copyright 2016 6WIND S.A.
|
2018-03-20 19:20:35 +00:00
|
|
|
* Copyright 2016 Mellanox Technologies, Ltd
|
2017-10-06 15:45:49 +00:00
|
|
|
*/
|
2018-01-29 13:11:30 +00:00
|
|
|
|
2017-10-06 15:45:49 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#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 "mlx5.h"
|
|
|
|
#include "mlx5_utils.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise the socket to communicate with the secondary process
|
|
|
|
*
|
2018-03-05 12:21:04 +00:00
|
|
|
* @param[in] dev
|
|
|
|
* Pointer to Ethernet device.
|
2017-10-06 15:45:49 +00:00
|
|
|
*
|
|
|
|
* @return
|
2018-03-05 12:21:06 +00:00
|
|
|
* 0 on success, a negative errno value otherwise and rte_errno is set.
|
2017-10-06 15:45:49 +00:00
|
|
|
*/
|
|
|
|
int
|
2018-03-05 12:21:04 +00:00
|
|
|
mlx5_socket_init(struct rte_eth_dev *dev)
|
2017-10-06 15:45:49 +00:00
|
|
|
{
|
2018-03-05 12:21:04 +00:00
|
|
|
struct priv *priv = dev->data->dev_private;
|
2017-10-06 15:45:49 +00:00
|
|
|
struct sockaddr_un sun = {
|
|
|
|
.sun_family = AF_UNIX,
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialise the socket to communicate with the secondary
|
|
|
|
* process.
|
|
|
|
*/
|
|
|
|
ret = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (ret < 0) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = errno;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u secondary process not supported: %s",
|
|
|
|
dev->data->port_id, strerror(errno));
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
priv->primary_socket = ret;
|
|
|
|
flags = fcntl(priv->primary_socket, F_GETFL, 0);
|
2018-03-05 12:21:06 +00:00
|
|
|
if (flags == -1) {
|
|
|
|
rte_errno = errno;
|
|
|
|
goto error;
|
|
|
|
}
|
2017-10-06 15:45:49 +00:00
|
|
|
ret = fcntl(priv->primary_socket, F_SETFL, flags | O_NONBLOCK);
|
2018-03-05 12:21:06 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
rte_errno = errno;
|
|
|
|
goto error;
|
|
|
|
}
|
2017-10-06 15:45:49 +00:00
|
|
|
snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
|
|
|
|
MLX5_DRIVER_NAME, priv->primary_socket);
|
2018-03-16 15:22:27 +00:00
|
|
|
remove(sun.sun_path);
|
2017-10-06 15:45:49 +00:00
|
|
|
ret = bind(priv->primary_socket, (const struct sockaddr *)&sun,
|
|
|
|
sizeof(sun));
|
|
|
|
if (ret < 0) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = errno;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING,
|
|
|
|
"port %u cannot bind socket, secondary process not"
|
|
|
|
" supported: %s",
|
|
|
|
dev->data->port_id, strerror(errno));
|
2017-10-06 15:45:49 +00:00
|
|
|
goto close;
|
|
|
|
}
|
|
|
|
ret = listen(priv->primary_socket, 0);
|
|
|
|
if (ret < 0) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = errno;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u secondary process not supported: %s",
|
|
|
|
dev->data->port_id, strerror(errno));
|
2017-10-06 15:45:49 +00:00
|
|
|
goto close;
|
|
|
|
}
|
2018-03-05 12:21:06 +00:00
|
|
|
return 0;
|
2017-10-06 15:45:49 +00:00
|
|
|
close:
|
|
|
|
remove(sun.sun_path);
|
2018-03-05 12:21:06 +00:00
|
|
|
error:
|
2017-10-06 15:45:49 +00:00
|
|
|
claim_zero(close(priv->primary_socket));
|
|
|
|
priv->primary_socket = 0;
|
2018-03-05 12:21:06 +00:00
|
|
|
return -rte_errno;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Un-Initialise the socket to communicate with the secondary process
|
|
|
|
*
|
2018-03-05 12:21:04 +00:00
|
|
|
* @param[in] dev
|
2017-10-06 15:45:49 +00:00
|
|
|
*/
|
2018-03-05 12:21:05 +00:00
|
|
|
void
|
2018-03-05 12:21:04 +00:00
|
|
|
mlx5_socket_uninit(struct rte_eth_dev *dev)
|
2017-10-06 15:45:49 +00:00
|
|
|
{
|
2018-03-05 12:21:04 +00:00
|
|
|
struct priv *priv = dev->data->dev_private;
|
|
|
|
|
2017-10-06 15:45:49 +00:00
|
|
|
MKSTR(path, "/var/tmp/%s_%d", MLX5_DRIVER_NAME, priv->primary_socket);
|
|
|
|
claim_zero(close(priv->primary_socket));
|
|
|
|
priv->primary_socket = 0;
|
|
|
|
claim_zero(remove(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle socket interrupts.
|
|
|
|
*
|
2018-03-05 12:21:04 +00:00
|
|
|
* @param dev
|
|
|
|
* Pointer to Ethernet device.
|
2017-10-06 15:45:49 +00:00
|
|
|
*/
|
|
|
|
void
|
2018-03-05 12:21:04 +00:00
|
|
|
mlx5_socket_handle(struct rte_eth_dev *dev)
|
2017-10-06 15:45:49 +00:00
|
|
|
{
|
2018-03-05 12:21:04 +00:00
|
|
|
struct priv *priv = dev->data->dev_private;
|
2017-10-06 15:45:49 +00:00
|
|
|
int conn_sock;
|
|
|
|
int ret = 0;
|
|
|
|
struct cmsghdr *cmsg = NULL;
|
|
|
|
struct ucred *cred = NULL;
|
|
|
|
char buf[CMSG_SPACE(sizeof(struct ucred))] = { 0 };
|
|
|
|
char vbuf[1024] = { 0 };
|
|
|
|
struct iovec io = {
|
|
|
|
.iov_base = vbuf,
|
|
|
|
.iov_len = sizeof(*vbuf),
|
|
|
|
};
|
|
|
|
struct msghdr msg = {
|
|
|
|
.msg_iov = &io,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_control = buf,
|
|
|
|
.msg_controllen = sizeof(buf),
|
|
|
|
};
|
|
|
|
int *fd;
|
|
|
|
|
|
|
|
/* Accept the connection from the client. */
|
|
|
|
conn_sock = accept(priv->primary_socket, NULL, NULL);
|
|
|
|
if (conn_sock < 0) {
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u connection failed: %s",
|
|
|
|
dev->data->port_id, strerror(errno));
|
2017-10-06 15:45:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ret = setsockopt(conn_sock, SOL_SOCKET, SO_PASSCRED, &(int){1},
|
|
|
|
sizeof(int));
|
|
|
|
if (ret < 0) {
|
2018-03-05 12:21:06 +00:00
|
|
|
ret = errno;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u cannot change socket options: %s",
|
|
|
|
dev->data->port_id, strerror(rte_errno));
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
ret = recvmsg(conn_sock, &msg, MSG_WAITALL);
|
|
|
|
if (ret < 0) {
|
2018-03-05 12:21:06 +00:00
|
|
|
ret = errno;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u received an empty message: %s",
|
|
|
|
dev->data->port_id, strerror(rte_errno));
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
/* Expect to receive credentials only. */
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
if (cmsg == NULL) {
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u no message", dev->data->port_id);
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
if ((cmsg->cmsg_type == SCM_CREDENTIALS) &&
|
|
|
|
(cmsg->cmsg_len >= sizeof(*cred))) {
|
|
|
|
cred = (struct ucred *)CMSG_DATA(cmsg);
|
|
|
|
assert(cred != NULL);
|
|
|
|
}
|
|
|
|
cmsg = CMSG_NXTHDR(&msg, cmsg);
|
|
|
|
if (cmsg != NULL) {
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u message wrongly formatted",
|
|
|
|
dev->data->port_id);
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
/* Make sure all the ancillary data was received and valid. */
|
|
|
|
if ((cred == NULL) || (cred->uid != getuid()) ||
|
|
|
|
(cred->gid != getgid())) {
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u wrong credentials",
|
|
|
|
dev->data->port_id);
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
/* Set-up the ancillary data. */
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
assert(cmsg != NULL);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(priv->ctx->cmd_fd));
|
|
|
|
fd = (int *)CMSG_DATA(cmsg);
|
|
|
|
*fd = priv->ctx->cmd_fd;
|
|
|
|
ret = sendmsg(conn_sock, &msg, 0);
|
|
|
|
if (ret < 0)
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u cannot send response",
|
|
|
|
dev->data->port_id);
|
2018-03-05 12:21:06 +00:00
|
|
|
error:
|
2017-10-06 15:45:49 +00:00
|
|
|
close(conn_sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to the primary process.
|
|
|
|
*
|
2018-03-05 12:21:04 +00:00
|
|
|
* @param[in] dev
|
|
|
|
* Pointer to Ethernet structure.
|
2017-10-06 15:45:49 +00:00
|
|
|
*
|
|
|
|
* @return
|
2018-03-05 12:21:06 +00:00
|
|
|
* fd on success, negative errno value otherwise and rte_errno is set.
|
2017-10-06 15:45:49 +00:00
|
|
|
*/
|
|
|
|
int
|
2018-03-05 12:21:04 +00:00
|
|
|
mlx5_socket_connect(struct rte_eth_dev *dev)
|
2017-10-06 15:45:49 +00:00
|
|
|
{
|
2018-03-05 12:21:04 +00:00
|
|
|
struct priv *priv = dev->data->dev_private;
|
2017-10-06 15:45:49 +00:00
|
|
|
struct sockaddr_un sun = {
|
|
|
|
.sun_family = AF_UNIX,
|
|
|
|
};
|
2018-03-05 12:21:06 +00:00
|
|
|
int socket_fd = -1;
|
2017-10-06 15:45:49 +00:00
|
|
|
int *fd = NULL;
|
|
|
|
int ret;
|
|
|
|
struct ucred *cred;
|
|
|
|
char buf[CMSG_SPACE(sizeof(*cred))] = { 0 };
|
|
|
|
char vbuf[1024] = { 0 };
|
|
|
|
struct iovec io = {
|
|
|
|
.iov_base = vbuf,
|
|
|
|
.iov_len = sizeof(*vbuf),
|
|
|
|
};
|
|
|
|
struct msghdr msg = {
|
|
|
|
.msg_control = buf,
|
|
|
|
.msg_controllen = sizeof(buf),
|
|
|
|
.msg_iov = &io,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
};
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
|
|
|
ret = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (ret < 0) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = errno;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u cannot connect to primary",
|
|
|
|
dev->data->port_id);
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
socket_fd = ret;
|
|
|
|
snprintf(sun.sun_path, sizeof(sun.sun_path), "/var/tmp/%s_%d",
|
|
|
|
MLX5_DRIVER_NAME, priv->primary_socket);
|
|
|
|
ret = connect(socket_fd, (const struct sockaddr *)&sun, sizeof(sun));
|
|
|
|
if (ret < 0) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = errno;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u cannot connect to primary",
|
|
|
|
dev->data->port_id);
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
if (cmsg == NULL) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = EINVAL;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(DEBUG, "port %u cannot get first message",
|
|
|
|
dev->data->port_id);
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_CREDENTIALS;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(*cred));
|
|
|
|
cred = (struct ucred *)CMSG_DATA(cmsg);
|
|
|
|
if (cred == NULL) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = EINVAL;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(DEBUG, "port %u no credentials received",
|
|
|
|
dev->data->port_id);
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
cred->pid = getpid();
|
|
|
|
cred->uid = getuid();
|
|
|
|
cred->gid = getgid();
|
|
|
|
ret = sendmsg(socket_fd, &msg, MSG_DONTWAIT);
|
|
|
|
if (ret < 0) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = errno;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING,
|
|
|
|
"port %u cannot send credentials to primary: %s",
|
|
|
|
dev->data->port_id, strerror(errno));
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
ret = recvmsg(socket_fd, &msg, MSG_WAITALL);
|
|
|
|
if (ret <= 0) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = errno;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u no message from primary: %s",
|
|
|
|
dev->data->port_id, strerror(errno));
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
if (cmsg == NULL) {
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = EINVAL;
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u no file descriptor received",
|
|
|
|
dev->data->port_id);
|
2018-03-05 12:21:06 +00:00
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
fd = (int *)CMSG_DATA(cmsg);
|
2018-03-05 12:21:06 +00:00
|
|
|
if (*fd < 0) {
|
2018-03-13 09:23:56 +00:00
|
|
|
DRV_LOG(WARNING, "port %u no file descriptor received: %s",
|
|
|
|
dev->data->port_id, strerror(errno));
|
2018-03-05 12:21:06 +00:00
|
|
|
rte_errno = *fd;
|
|
|
|
goto error;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|
|
|
|
ret = *fd;
|
|
|
|
close(socket_fd);
|
2018-05-03 07:59:36 +00:00
|
|
|
return ret;
|
2018-03-05 12:21:06 +00:00
|
|
|
error:
|
|
|
|
if (socket_fd != -1)
|
|
|
|
close(socket_fd);
|
|
|
|
return -rte_errno;
|
2017-10-06 15:45:49 +00:00
|
|
|
}
|