146 lines
4.5 KiB
Diff
146 lines
4.5 KiB
Diff
From 56d36bfa97abf388bf0bfa567470810fdae2a921 Mon Sep 17 00:00:00 2001
|
|
From: kircher <majun65@huawei.com>
|
|
Date: Fri, 10 Feb 2023 14:51:43 +0800
|
|
Subject: [PATCH] bring up kni when init
|
|
|
|
---
|
|
src/lstack/core/lstack_dpdk.c | 28 ------------------
|
|
src/lstack/core/lstack_init.c | 55 ++++++++++++++++++++++++++++++++++-
|
|
2 files changed, 54 insertions(+), 29 deletions(-)
|
|
|
|
diff --git a/src/lstack/core/lstack_dpdk.c b/src/lstack/core/lstack_dpdk.c
|
|
index 560162f..5475f31 100644
|
|
--- a/src/lstack/core/lstack_dpdk.c
|
|
+++ b/src/lstack/core/lstack_dpdk.c
|
|
@@ -523,32 +523,6 @@ int32_t dpdk_ethdev_start(void)
|
|
return 0;
|
|
}
|
|
|
|
-static void set_kni_ip_mac(uint16_t port_id)
|
|
-{
|
|
- struct cfg_params *cfg = get_global_cfg_params();
|
|
-
|
|
- int32_t fd = posix_api->socket_fn(AF_INET, SOCK_DGRAM, 0);
|
|
- struct ifreq set_ifr = {0};
|
|
- struct sockaddr_in *sin = (struct sockaddr_in *)&set_ifr.ifr_addr;
|
|
-
|
|
- sin->sin_family = AF_INET;
|
|
- sin->sin_addr.s_addr = cfg->host_addr.addr;
|
|
- if (strcpy_s(set_ifr.ifr_name, sizeof(set_ifr.ifr_name), GAZELLE_KNI_NAME) != 0) {
|
|
- LSTACK_LOG(ERR, LSTACK, "strcpy_s fail \n");
|
|
- }
|
|
-
|
|
- if (posix_api->ioctl_fn(fd, SIOCSIFADDR, &set_ifr) < 0) {
|
|
- LSTACK_LOG(ERR, LSTACK, "set kni ip=%u fail\n", cfg->host_addr.addr);
|
|
- }
|
|
-
|
|
- sin->sin_addr.s_addr = cfg->netmask.addr;
|
|
- if (posix_api->ioctl_fn(fd, SIOCSIFNETMASK, &set_ifr) < 0) {
|
|
- LSTACK_LOG(ERR, LSTACK, "set kni netmask=%u fail\n", cfg->netmask.addr);
|
|
- }
|
|
-
|
|
- posix_api->close_fn(fd);
|
|
-}
|
|
-
|
|
int32_t dpdk_init_lstack_kni(void)
|
|
{
|
|
struct protocol_stack_group *stack_group = get_protocol_stack_group();
|
|
@@ -563,8 +537,6 @@ int32_t dpdk_init_lstack_kni(void)
|
|
return -1;
|
|
}
|
|
|
|
- set_kni_ip_mac(stack_group->port_id);
|
|
-
|
|
return 0;
|
|
}
|
|
|
|
diff --git a/src/lstack/core/lstack_init.c b/src/lstack/core/lstack_init.c
|
|
index 6309d1d..34b2c0d 100644
|
|
--- a/src/lstack/core/lstack_init.c
|
|
+++ b/src/lstack/core/lstack_init.c
|
|
@@ -16,13 +16,16 @@
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <semaphore.h>
|
|
-#include <sys/socket.h>
|
|
+#include <sys/ioctl.h>
|
|
#include <sys/file.h>
|
|
#include <securec.h>
|
|
#include <numa.h>
|
|
#include <pthread.h>
|
|
#include <rte_pdump.h>
|
|
#include <unistd.h>
|
|
+#include <net/if.h>
|
|
+#include <net/if_arp.h>
|
|
+#include <netinet/in.h>
|
|
|
|
#include <lwip/def.h>
|
|
#include <lwip/init.h>
|
|
@@ -264,6 +267,52 @@ static void gazelle_signal_init(void)
|
|
lstack_signal_init();
|
|
}
|
|
|
|
+static void set_kni_ip_mac() {
|
|
+ struct cfg_params *cfg = get_global_cfg_params();
|
|
+
|
|
+ int32_t fd = posix_api->socket_fn(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
+ struct ifreq set_ifr = {0};
|
|
+ struct sockaddr_in *sin = (struct sockaddr_in *)&set_ifr.ifr_addr;
|
|
+
|
|
+ sin->sin_family = AF_INET;
|
|
+ sin->sin_addr.s_addr = cfg->host_addr.addr;
|
|
+ if (strcpy_s(set_ifr.ifr_name, sizeof(set_ifr.ifr_name), GAZELLE_KNI_NAME) != 0) {
|
|
+ LSTACK_LOG(ERR, LSTACK, "strcpy_s fail \n");
|
|
+ }
|
|
+
|
|
+ if (posix_api->ioctl_fn(fd, SIOCSIFADDR, &set_ifr) < 0) {
|
|
+ LSTACK_LOG(ERR, LSTACK, "set kni ip=%u fail\n", cfg->host_addr.addr);
|
|
+ }
|
|
+
|
|
+ sin->sin_addr.s_addr = cfg->netmask.addr;
|
|
+ if (posix_api->ioctl_fn(fd, SIOCSIFNETMASK, &set_ifr) < 0) {
|
|
+ LSTACK_LOG(ERR, LSTACK, "set kni netmask=%u fail\n", cfg->netmask.addr);
|
|
+ }
|
|
+
|
|
+ set_ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
|
|
+ for (int i = 0; i < 6; i++) {
|
|
+ set_ifr.ifr_hwaddr.sa_data[i] = cfg->mac_addr[i];
|
|
+ }
|
|
+
|
|
+ if (posix_api->ioctl_fn(fd, SIOCSIFHWADDR, &set_ifr) < 0) {
|
|
+ LSTACK_LOG(ERR, LSTACK, "set kni macaddr=%hhx:%hhx:%hhx:%hhx:%hhx:%hhx fail\n",
|
|
+ cfg->mac_addr[0], cfg->mac_addr[1],
|
|
+ cfg->mac_addr[2], cfg->mac_addr[3],
|
|
+ cfg->mac_addr[4], cfg->mac_addr[5]);
|
|
+ }
|
|
+
|
|
+ if (posix_api->ioctl_fn(fd, SIOCGIFFLAGS, &set_ifr) < 0) {
|
|
+ LSTACK_LOG(ERR, LSTACK, "get kni state fail\n");
|
|
+ }
|
|
+
|
|
+ set_ifr.ifr_flags |= (IFF_RUNNING | IFF_UP);
|
|
+ if (posix_api->ioctl_fn(fd, SIOCSIFFLAGS, &set_ifr) < 0){
|
|
+ LSTACK_LOG(ERR, LSTACK, "set kni state fail\n");
|
|
+ }
|
|
+
|
|
+ posix_api->close_fn(fd);
|
|
+}
|
|
+
|
|
__attribute__((constructor)) void gazelle_network_init(void)
|
|
{
|
|
/*
|
|
@@ -347,6 +396,10 @@ __attribute__((constructor)) void gazelle_network_init(void)
|
|
LSTACK_EXIT(1, "stack thread or kernel_event thread failed\n");
|
|
}
|
|
|
|
+ if (get_global_cfg_params()->kni_switch) {
|
|
+ set_kni_ip_mac();
|
|
+ }
|
|
+
|
|
posix_api->ues_posix = 0;
|
|
LSTACK_LOG(INFO, LSTACK, "gazelle_network_init success\n");
|
|
rte_smp_mb();
|
|
--
|
|
2.23.0
|
|
|