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