Skip to content

Commit

Permalink
tools/ebpf: remove xsk_map_fd send function (#610)
Browse files Browse the repository at this point in the history
This function is integrated to MTL Manager.

---------

Signed-off-by: Ric Li <[email protected]>
  • Loading branch information
ricmli authored Nov 29, 2023
1 parent ee6fc20 commit e02ec97
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 274 deletions.
111 changes: 9 additions & 102 deletions lib/src/dev/mt_af_xdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,6 @@ struct mt_xdp_priv {
bool has_ctrl;
};

static int xdp_connect_control_sock() {
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock <= 0) return -1;
struct sockaddr_un server;
server.sun_family = AF_UNIX;
snprintf(server.sun_path, sizeof(server.sun_path), MTL_XDP_CTRL_SOCK_PATH);

if (connect(sock, (struct sockaddr*)&server, sizeof(struct sockaddr_un)) < 0) {
close(sock);
return -1;
}

return sock;
}

static int xdp_queue_tx_max_rate(struct mt_xdp_priv* xdp, struct mt_xdp_queue* xq,
uint32_t rate_kbps) {
enum mtl_port port = xq->port;
Expand Down Expand Up @@ -461,64 +446,17 @@ static int xdp_socket_update_xskmap(struct mtl_main_impl* impl, struct mt_xdp_qu
enum mtl_port port = xq->port;
uint16_t q = xq->q;
int ret;
int xsks_map_fd = -1;

if (mt_is_manager_connected(impl)) {
xsks_map_fd = mt_instance_request_xsks_map_fd(impl, if_nametoindex(ifname));
} else {
int sock = xdp_connect_control_sock();
if (sock < 0) {
err("%s(%d,%u), unix socket create fail, %s\n", __func__, port, q, strerror(errno));
return errno;
}

char command[64];
snprintf(command, sizeof(command), "imtl:if:%s:get_xsk_map", ifname);

send(sock, command, sizeof(command), 0);

char cms[CMSG_SPACE(sizeof(int))];
struct cmsghdr* cmsg;
struct msghdr msg;
struct iovec iov;
int value;
int len;

iov.iov_base = &value;
iov.iov_len = sizeof(int);

memset(&msg, 0, sizeof(msg));
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = (caddr_t)cms;
msg.msg_controllen = sizeof(cms);

len = recvmsg(sock, &msg, 0);

close(sock);

if (len <= 0) {
err("%s(%d,%u), recvmsg wrong length %d\n", __func__, port, q, len);
return -EINVAL;
}

cmsg = CMSG_FIRSTHDR(&msg);
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS ||
cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
err("%s(%d,%u), invalid cmsg for map fd\n", __func__, port, q);
return -EINVAL;
}
xsks_map_fd = *(int*)CMSG_DATA(cmsg);
}
int xsks_map_fd = mt_instance_request_xsks_map_fd(impl, if_nametoindex(ifname));

if (xsks_map_fd < 0) {
err("%s(%d,%u), get xsks_map_fd fail, %s\n", __func__, port, q, strerror(errno));
return errno;
err("%s(%d,%u), get xsks_map_fd fail\n", __func__, port, q);
return -EIO;
}

ret = xsk_socket__update_xskmap(xq->socket, xsks_map_fd);
if (ret) {
err("%s(%d,%u), get xsks_map_fd fail, %d\n", __func__, port, q, ret);
err("%s(%d,%u), update xsks_map fail, %d\n", __func__, port, q, ret);
return ret;
}

Expand Down Expand Up @@ -547,7 +485,7 @@ static int xdp_socket_init(struct mt_xdp_priv* xdp, struct mt_xdp_queue* xq) {
&cfg);
if (ret < 0) {
if (ret == -EPERM) {
err("%s(%d,%u), please run with control server or root user\n", __func__, port, q);
err("%s(%d,%u), please run with mtl manager or root user\n", __func__, port, q);
return ret;
}
warn("%s(%d,%u), xsk create with zero copy fail %d(%s), try copy mode\n", __func__,
Expand All @@ -557,8 +495,7 @@ static int xdp_socket_init(struct mt_xdp_priv* xdp, struct mt_xdp_queue* xq) {
&xq->tx_prod, &cfg);
if (ret < 0) {
if (ret == -EPERM) {
err("%s(%d,%u), please run with control server or root user\n", __func__, port,
q);
err("%s(%d,%u), please run with mtl manager or root user\n", __func__, port, q);
}
err("%s(%d,%u), xsk create fail %d(%s)\n", __func__, port, q, ret, strerror(ret));
return ret;
Expand Down Expand Up @@ -876,18 +813,6 @@ int mt_dev_xdp_init(struct mt_interface* inf) {

xdp_parse_drv_name(xdp);

if (!mt_is_privileged(impl)) {
int ctrl_sock = xdp_connect_control_sock();
if (ctrl_sock > 0) {
char buf[10];
snprintf(buf, sizeof(buf), "imtl:ping");
send(ctrl_sock, buf, sizeof(buf), 0);
recv(ctrl_sock, buf, sizeof(buf), 0);
if (strncmp(buf, "pong", 4) == 0) xdp->has_ctrl = true;
close(ctrl_sock);
}
}

if (mt_is_manager_connected(impl)) xdp->has_ctrl = true;

xdp_parse_combined_info(xdp);
Expand Down Expand Up @@ -1059,27 +984,9 @@ uint16_t mt_tx_xdp_burst(struct mt_tx_xdp_entry* entry, struct rte_mbuf** tx_pkt
return xdp_tx(entry->parent, entry->xq, tx_pkts, nb_pkts);
}

static int xdp_socket_update_dp(struct mtl_main_impl* impl, const char* if_name,
uint16_t dp, bool add) {
if (mt_is_manager_connected(impl))
return mt_instance_update_udp_dp_filter(impl, if_nametoindex(if_name), dp, add);

int sock = xdp_connect_control_sock();
if (sock < 0) {
err("%s, unix socket create fail, %s\n", __func__, strerror(errno));
return errno;
}

char command[64];
if (add)
snprintf(command, sizeof(command), "imtl:if:%s:dp_add_filter:%u", if_name, dp);
else
snprintf(command, sizeof(command), "imtl:if:%s:dp_del_filter:%u", if_name, dp);

send(sock, command, sizeof(command), 0);

close(sock);
return 0;
static inline int xdp_socket_update_dp(struct mtl_main_impl* impl, const char* if_name,
uint16_t dp, bool add) {
return mt_instance_update_udp_dp_filter(impl, if_nametoindex(if_name), dp, add);
}

struct mt_rx_xdp_entry* mt_rx_xdp_get(struct mtl_main_impl* impl, enum mtl_port port,
Expand Down
2 changes: 0 additions & 2 deletions lib/src/dev/mt_af_xdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include "../mt_main.h"

#define MTL_XDP_CTRL_SOCK_PATH "/var/run/et_xdp.sock"

struct mt_tx_xdp_get_args {
bool queue_match;
uint16_t queue_id;
Expand Down
29 changes: 8 additions & 21 deletions manager/mtl_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,42 +190,29 @@ std::shared_ptr<mtl_interface> mtl_instance::get_interface(const unsigned int if

int mtl_instance::handle_message_request_map_fd(
mtl_request_map_fd_message_t* request_map_fd_msg) {
int fd = -1;
unsigned int ifindex = ntohl(request_map_fd_msg->ifindex);
auto interface = get_interface(ifindex);
if (interface != nullptr) fd = interface->get_xsks_map_fd();

struct msghdr msg;
struct msghdr msg = {0};
struct iovec iov[1];
struct cmsghdr* cmsg = NULL;
char ctrl_buf[CMSG_SPACE(sizeof(int))];
char data[1];
char control[CMSG_SPACE(sizeof(int))] = {0};
char data[1] = {' '};

memset(&msg, 0, sizeof(struct msghdr));
memset(ctrl_buf, 0, CMSG_SPACE(sizeof(int)));

data[0] = ' ';
iov[0].iov_base = data;
iov[0].iov_len = sizeof(data);

msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_controllen = CMSG_SPACE(sizeof(int));
msg.msg_control = ctrl_buf;
msg.msg_control = control;

cmsg = CMSG_FIRSTHDR(&msg);
struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));

if (interface == nullptr) {
log(log_level::ERROR, "Failed to get interface " + std::to_string(ifindex));
*((int*)CMSG_DATA(cmsg)) = -1;
sendmsg(conn_fd, &msg, 0);
return -1;
}

*((int*)CMSG_DATA(cmsg)) = interface->get_xsks_map_fd();
*((int*)CMSG_DATA(cmsg)) = fd;

return sendmsg(conn_fd, &msg, 0);
}
Expand Down
3 changes: 2 additions & 1 deletion manager/mtl_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ int main() {
goto out;
}

logger::log(log_level::INFO, "MTL Manager is running. Press Ctrl+C to stop it.");
logger::log(log_level::INFO,
"MTL Manager is running. Press Ctrl+C or use SIGINT to stop it.");

while (is_running) {
struct epoll_event events[clients.size() + 2];
Expand Down
8 changes: 1 addition & 7 deletions tools/ebpf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ fentry: a simple program to trace udp_send_skb calls, requires kernel > 5.5.
sudo ./et --prog fentry [--print]
```

xdp: a privileged program to load afxdp bpf program and send the xsk_map_fd to other processes by socket.

```bash
sudo ./et --prog xdp --ifname ens785f0
```

load a custom program for xdp:
xdp: a privileged program to load custom xdp bpf program:

```bash
sudo ./et --prog xdp --ifname ens785f0,ens785f1 --xdp_path xsk.xdp.o
Expand Down
Loading

0 comments on commit e02ec97

Please sign in to comment.