Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manager: add daemon server for privileged controls #582

Merged
merged 10 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ APP_BUILD_DIR=${WORKSPACE}/build/app
TEST_BUILD_DIR=${WORKSPACE}/build/tests
PLUGINS_BUILD_DIR=${WORKSPACE}/build/plugins
LD_PRELOAD_BUILD_DIR=${WORKSPACE}/build/ld_preload
MANAGER_BUILD_DIR=${WORKSPACE}/build/manager

# build lib
meson setup "${LIB_BUILD_DIR}" -Dbuildtype="$buildtype" -Ddisable_pcapng="$disable_pcapng" -Denable_asan="$enable_asan" -Denable_tap="$enable_tap"
Expand All @@ -85,17 +86,13 @@ popd
# build app
pushd app/
meson setup "${APP_BUILD_DIR}" -Dbuildtype="$buildtype" -Denable_asan="$enable_asan"
popd
pushd "${APP_BUILD_DIR}"
ninja
meson compile -C "${APP_BUILD_DIR}"
popd

# build tests
pushd tests/
meson setup "${TEST_BUILD_DIR}" -Dbuildtype="$buildtype" -Denable_asan="$enable_asan"
popd
pushd "${TEST_BUILD_DIR}"
ninja
meson compile -C "${TEST_BUILD_DIR}"
popd

# build plugins
Expand Down Expand Up @@ -124,4 +121,12 @@ else
sudo ninja install
fi
popd
fi

# build mtl_manager
if [ "$OS" != "Windows_NT" ]; then
pushd manager/
meson setup "${MANAGER_BUILD_DIR}" -Dbuildtype="$buildtype" -Denable_asan="$enable_asan"
meson compile -C "${MANAGER_BUILD_DIR}"
popd
fi
1 change: 1 addition & 0 deletions lib/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sources = files(
'mt_stat.c',
'mt_rtcp.c',
'mt_flow.c',
'mt_instance.c',
)

if is_windows
Expand Down
152 changes: 152 additions & 0 deletions lib/src/mt_instance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2023 Intel Corporation
*/

#include "mt_instance.h"

#ifndef WINDOWSENV

#include <sys/un.h>

#include "../manager/mtl_mproto.h"
#include "mt_log.h"

int mt_instance_put_lcore(struct mtl_main_impl* impl, unsigned int lcore_id) {
int ret;
int sock = impl->instance_fd;

mtl_message_t msg;
msg.header.magic = htonl(MTL_MANAGER_MAGIC);
msg.header.type = htonl(MTL_MSG_TYPE_PUT_LCORE);
msg.body.lcore_msg.lcore = htons(lcore_id);
msg.header.body_len = sizeof(msg.body.lcore_msg);

ret = send(sock, &msg, sizeof(msg), 0);
if (ret < 0) {
err("%s, send message fail\n", __func__);
return ret;
}

return 0;
}

int mt_instance_get_lcore(struct mtl_main_impl* impl, unsigned int lcore_id) {
int ret;
int sock = impl->instance_fd;

mtl_message_t msg;
msg.header.magic = htonl(MTL_MANAGER_MAGIC);
msg.header.type = htonl(MTL_MSG_TYPE_GET_LCORE);
msg.body.lcore_msg.lcore = htons(lcore_id);
msg.header.body_len = htonl(sizeof(mtl_lcore_message_t));

ret = send(sock, &msg, sizeof(mtl_message_t), 0);
if (ret < 0) {
err("%s, send message fail\n", __func__);
return ret;
}

ret = recv(sock, &msg, sizeof(mtl_message_t), 0);

if (ret < 0 || ntohl(msg.header.magic) != MTL_MANAGER_MAGIC ||
ntohl(msg.header.type) != MTL_MSG_TYPE_RESPONSE) {
err("%s, recv response fail\n", __func__);
return -EIO;
}

int response = msg.body.response_msg.response;

return -response;
ricmli marked this conversation as resolved.
Show resolved Hide resolved
}

int mt_instance_init(struct mtl_main_impl* impl) {
impl->instance_fd = -1;
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
err("%s, create socket fail %d\n", __func__, sock);
return sock;
}

struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, MTL_MANAGER_SOCK_PATH, sizeof(addr.sun_path) - 1);
int ret = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
if (ret < 0) {
err("%s, connect to manager fail\n", __func__);
close(sock);
return ret;
}

struct mt_user_info* u_info = &impl->u_info;

mtl_message_t msg;
msg.header.magic = htonl(MTL_MANAGER_MAGIC);
msg.header.type = htonl(MTL_MSG_TYPE_REGISTER);
msg.header.body_len = sizeof(mtl_register_message_t);

mtl_register_message_t* reg_msg = &msg.body.register_msg;
reg_msg->pid = htonl(u_info->pid);
reg_msg->uid = htonl(getuid());
strncpy(reg_msg->hostname, u_info->hostname, sizeof(reg_msg->hostname));

ret = send(sock, &msg, sizeof(msg), 0);
if (ret < 0) {
err("%s, send message fail\n", __func__);
close(sock);
return ret;
}

ret = recv(sock, &msg, sizeof(msg), 0);
if (ret < 0 || ntohl(msg.header.magic) != MTL_MANAGER_MAGIC ||
ntohl(msg.header.type) != MTL_MSG_TYPE_RESPONSE) {
err("%s, recv response fail\n", __func__);
close(sock);
return ret;
}

int response = msg.body.response_msg.response;
if (response != 0) {
err("%s, register fail\n", __func__);
close(sock);
return -EIO;
}

impl->instance_fd = sock;

info("%s, succ\n", __func__);

return 0;
}

int mt_instance_uinit(struct mtl_main_impl* impl) {
int sock = impl->instance_fd;
if (sock <= 0) return -EIO;

return close(sock);
}

#else /* not supported on Windows */

int mt_instance_init(struct mtl_main_impl* impl) {
impl->instance_fd = -1;
return -ENOTSUP;
}

int mt_instance_uinit(struct mtl_main_impl* impl) {
MTL_MAY_UNUSED(impl);
return -ENOTSUP;
}

int mt_instance_get_lcore(struct mtl_main_impl* impl, unsigned int lcore_id) {
MTL_MAY_UNUSED(impl);
MTL_MAY_UNUSED(lcore_id);
return -ENOTSUP;
}

int mt_instance_put_lcore(struct mtl_main_impl* impl, unsigned int lcore_id) {
MTL_MAY_UNUSED(impl);
MTL_MAY_UNUSED(lcore_id);
return -ENOTSUP;
}

#endif
16 changes: 16 additions & 0 deletions lib/src/mt_instance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2023 Intel Corporation
*/

#ifndef _MT_LIB_INSTANCE_HEAD_H_
#define _MT_LIB_INSTANCE_HEAD_H_

#include "mt_main.h"

int mt_instance_init(struct mtl_main_impl* impl);
int mt_instance_uinit(struct mtl_main_impl* impl);

int mt_instance_get_lcore(struct mtl_main_impl* impl, unsigned int lcore_id);
int mt_instance_put_lcore(struct mtl_main_impl* impl, unsigned int lcore_id);

#endif
5 changes: 5 additions & 0 deletions lib/src/mt_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "mt_dhcp.h"
#include "mt_dma.h"
#include "mt_flow.h"
#include "mt_instance.h"
#include "mt_log.h"
#include "mt_mcast.h"
#include "mt_ptp.h"
Expand Down Expand Up @@ -475,6 +476,8 @@ mtl_handle mtl_init(struct mtl_init_params* p) {
impl->privileged = true;
#endif

mt_instance_init(impl);

rte_memcpy(&impl->user_para, p, sizeof(*p));
impl->var_para.sch_default_sleep_us = 1 * US_PER_MS; /* default 1ms */
/* use sleep zero if sleep us is smaller than this thresh */
Expand Down Expand Up @@ -630,6 +633,8 @@ int mtl_uninit(mtl_handle mt) {

mt_stat_uinit(impl);

mt_instance_uinit(impl);

mt_rte_free(impl);

mt_dev_uinit(p);
Expand Down
7 changes: 7 additions & 0 deletions lib/src/mt_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,9 @@ struct mtl_main_impl {

int arp_timeout_ms;
bool privileged; /* if app running with root privilege */

/* connect to mtl manager */
int instance_fd;
};

static inline struct mtl_init_params* mt_get_user_params(struct mtl_main_impl* impl) {
Expand All @@ -1207,6 +1210,10 @@ static inline bool mt_is_privileged(struct mtl_main_impl* impl) {
return impl->privileged;
}

static inline bool mt_is_manager_connected(struct mtl_main_impl* impl) {
return impl->instance_fd > 0;
}

static inline struct mt_interface* mt_if(struct mtl_main_impl* impl, enum mtl_port port) {
return &impl->inf[port];
}
Expand Down
Loading