From 81fc2ed5bdd5e2e07c13ecc0ff75b2527601c596 Mon Sep 17 00:00:00 2001 From: the Date: Sun, 26 Nov 2023 14:42:02 +0800 Subject: [PATCH] add configs --- boot.sh | 4 ++++ config.cfg | 3 +++ src/config.c | 21 ++++++++++++++++---- src/config.h | 7 ++++++- src/pppd/nat.h | 6 +++--- src/pppd/pppd.c | 6 +++--- src/pppd/pppd.h | 1 - src/unix.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ src/unix.h | 8 ++++++++ src/vrg.c | 19 +++++++++++------- src/vrg.h | 3 ++- 11 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 src/unix.c create mode 100644 src/unix.h diff --git a/boot.sh b/boot.sh index 6bf44f4..f6a7691 100755 --- a/boot.sh +++ b/boot.sh @@ -34,3 +34,7 @@ popd pushd $path make && make install popd +mkdir -p /var/log/vrg +mkdir -p /var/run/vrg +mkdir -p /etc/vrg +cp config.cfg /etc/vrg/config.cfg diff --git a/config.cfg b/config.cfg index 1ade0bc..5b76fd0 100644 --- a/config.cfg +++ b/config.cfg @@ -9,9 +9,12 @@ BaseVlan = 2; # vRG loglvl Loglvl = "DBG"; +LogPath = "/var/log/vrg/vrg.log"; # non-vlan mode NonVlanMode = 0; # vRG default gateway ip DefaultGateway = "192.168.2.1"; + +DefaultUnixSocket = "/var/run/vrg/vrg.sock"; \ No newline at end of file diff --git a/src/config.c b/src/config.c index 64fc5e5..3caede9 100644 --- a/src/config.c +++ b/src/config.c @@ -2,16 +2,17 @@ #include #include "vrg.h" #include "dbg.h" +#include "config.h" -STATUS parse_config(const char *config_path, VRG_t *vrg_ccb) +STATUS parse_config(const char *config_path, VRG_t *vrg_ccb, struct vrg_config *vrg_cfg) { config_t cfg; int user_count, base_vlan, non_vlan_mode; - const char *loglvl, *default_gateway; + const char *loglvl, *default_gateway, *unix_sock_path, *log_path; config_init(&cfg); if (!config_read_file(&cfg, config_path)) { - VRG_LOG(INFO, vrg_ccb->fp, NULL, NULL, "read config file %s content error: %s:%d - %s", + fprintf(stderr, "read config file %s content error: %s:%d - %s\n", config_path, config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return ERROR; @@ -26,10 +27,17 @@ STATUS parse_config(const char *config_path, VRG_t *vrg_ccb) loglvl = "DBG"; vrg_ccb->loglvl = logstr2lvl(loglvl); if (vrg_ccb->loglvl == 0) { - VRG_LOG(INFO, vrg_ccb->fp, NULL, NULL, "log level error"); + fprintf(stderr, "log level error\n"); config_destroy(&cfg); return ERROR; } + if (config_lookup_string(&cfg, "LogPath", &log_path) == CONFIG_FALSE) { + log_path = "/var/log/vrg/vrg.log"; + printf("log path not found, use default path: %s\n", log_path); + } + strncpy(vrg_cfg->log_path, log_path, sizeof(vrg_cfg->log_path) - 1); + vrg_cfg->log_path[sizeof(vrg_cfg->log_path) - 1] = '\0'; + if (config_lookup_int(&cfg, "NonVlanMode", &non_vlan_mode) == CONFIG_FALSE) non_vlan_mode = 0; vrg_ccb->non_vlan_mode = non_vlan_mode; @@ -37,6 +45,11 @@ STATUS parse_config(const char *config_path, VRG_t *vrg_ccb) if (config_lookup_string(&cfg, "DefaultGateway", &default_gateway) == CONFIG_FALSE) default_gateway = "192.168.2.1"; vrg_ccb->lan_ip = rte_cpu_to_be_32(inet_addr(default_gateway)); + + if (config_lookup_string(&cfg, "DefaultUnixSocket", &unix_sock_path) == CONFIG_FALSE) + unix_sock_path = "/var/run/vrg/vrg.sock"; + strncpy(vrg_cfg->unix_sock_path, unix_sock_path, sizeof(vrg_cfg->unix_sock_path) - 1); + vrg_cfg->unix_sock_path[sizeof(vrg_cfg->unix_sock_path) - 1] = '\0'; config_destroy(&cfg); diff --git a/src/config.h b/src/config.h index 81139cb..2547e03 100644 --- a/src/config.h +++ b/src/config.h @@ -9,6 +9,11 @@ #include "vrg.h" -extern STATUS parse_config(const char *config_path, VRG_t *vrg_ccb); +struct vrg_config { + char unix_sock_path[256]; + char log_path[256]; +}; + +STATUS parse_config(const char *config_path, VRG_t *vrg_ccb, struct vrg_config *vrg_cfg); #endif diff --git a/src/pppd/nat.h b/src/pppd/nat.h index 542c37b..40684c6 100644 --- a/src/pppd/nat.h +++ b/src/pppd/nat.h @@ -1,3 +1,6 @@ +#ifndef _NAT_H_ +#define _NAT_H_ + #include #include #include @@ -20,9 +23,6 @@ #include #include "pppd.h" -#ifndef _NAT_H_ -#define _NAT_H_ - void nat_rule_timer(__attribute__((unused)) struct rte_timer *tim, PPP_INFO_t *s_ppp_ccb); static inline void nat_icmp_learning(struct rte_ether_hdr *eth_hdr, struct rte_ipv4_hdr *ip_hdr, struct rte_icmp_hdr *icmphdr, U32 *new_port_id, addr_table_t addr_table[]) diff --git a/src/pppd/pppd.c b/src/pppd/pppd.c index fffaa39..dec68f3 100644 --- a/src/pppd/pppd.c +++ b/src/pppd/pppd.c @@ -34,10 +34,10 @@ #include "../vrg.h" #include "../utils.h" -U32 ppp_interval; -static VRG_t *vrg_ccb; +U32 ppp_interval; +static VRG_t *vrg_ccb; -extern STATUS PPP_FSM(struct rte_timer *ppp, PPP_INFO_t *ppp_ccb, U16 event); +extern STATUS PPP_FSM(struct rte_timer *ppp, PPP_INFO_t *ppp_ccb, U16 event); /** * @brief pppoe connection closing processing function diff --git a/src/pppd/pppd.h b/src/pppd/pppd.h index 847dca8..4161109 100644 --- a/src/pppd/pppd.h +++ b/src/pppd/pppd.h @@ -58,7 +58,6 @@ typedef struct { rte_atomic16_t ppp_bool; /* boolean flag for accept ppp packets at data plane */ rte_atomic16_t dp_start_bool; /* hsi data plane starting boolean flag */ BOOL ppp_processing; /* boolean flag for checking ppp is disconnecting */ - //FILE *fp; /* log file pointer */ addr_table_t addr_table[TOTAL_SOCK_PORT]; /* hsi nat addr table */ struct rte_timer pppoe; /* pppoe timer */ struct rte_timer ppp; /* ppp timer */ diff --git a/src/unix.c b/src/unix.c new file mode 100644 index 0000000..fdbc0c7 --- /dev/null +++ b/src/unix.c @@ -0,0 +1,52 @@ +#include +#include +#include "vrg.h" +#include "dbg.h" + +STATUS init_unix_sock(VRG_t *vrg_ccb) +{ + remove(vrg_ccb->unix_sock_path); + + int server_socket = socket(AF_UNIX, SOCK_STREAM, 0); + if (server_socket == -1) { + VRG_LOG(ERR, vrg_ccb->fp, NULL, NULL, "Error! Cannot create unix socket: %s", strerror(errno)); + return ERROR; + } + + struct sockaddr_un server_addr; + int server_len = sizeof(server_addr); + struct sockaddr_un client_addr; + unsigned int client_len = sizeof(client_addr); + + server_addr.sun_family = AF_UNIX; + strcpy(server_addr.sun_path, vrg_ccb->unix_sock_path); + + bind(server_socket, (struct sockaddr *) &server_addr, server_len); + + listen(server_socket, 1); + VRG_LOG(INFO, vrg_ccb->fp, NULL, NULL, "Unix socket server is listening"); + + for(;;) { + int client_socket = accept(server_socket, (struct sockaddr *) &client_addr, &client_len); + if (client_socket == -1) { + VRG_LOG(ERR, vrg_ccb->fp, NULL, NULL, "Error! Cannot accept unix socket client: %s", strerror(errno)); + continue; + } + for(;;) { + char buf[1024]; + int len = recv(client_socket, buf, 1024, 0); + if (len == -1) { + VRG_LOG(ERR, vrg_ccb->fp, NULL, NULL, "Error! Cannot receive data from unix socket client: %s", strerror(errno)); + break; + } + if (len == 0) { + VRG_LOG(WARN, vrg_ccb->fp, NULL, NULL, "Unix socket client disconnected"); + break; + } + + send(client_socket, buf, len, 0); + } + } + + return SUCCESS; +} \ No newline at end of file diff --git a/src/unix.h b/src/unix.h new file mode 100644 index 0000000..9c8826f --- /dev/null +++ b/src/unix.h @@ -0,0 +1,8 @@ +#ifndef _UNIX_H_ +#define _UNIX_H_ + +#include + +STATUS init_unix_sock(VRG_t *vrg_ccb); + +#endif \ No newline at end of file diff --git a/src/vrg.c b/src/vrg.c index 002a2ef..240723e 100644 --- a/src/vrg.c +++ b/src/vrg.c @@ -28,6 +28,7 @@ #include "config.h" #include "timer.h" #include "utils.h" +#include "unix.h" #define BURST_SIZE 32 @@ -164,13 +165,15 @@ int vrg_loop(VRG_t *vrg_ccb) int control_plane(VRG_t *vrg_ccb) { if (vrg_loop(vrg_ccb) == ERROR) - return ERROR; + return -1; return 0; } int northbound(VRG_t *vrg_ccb) { - while(1); // place holder + if (init_unix_sock(vrg_ccb) == ERROR) + return -1; + return 0; } @@ -180,10 +183,6 @@ int vrg_start(int argc, char **argv) if (ret < 0) rte_exit(EXIT_FAILURE, "rte initlize fail.\n"); - vrg_ccb.fp = fopen("./vrg.log","w+"); - if (vrg_ccb.fp) - rte_openlog_stream(vrg_ccb.fp); - dbg_init((void *)&vrg_ccb); if (rte_lcore_count() < 8) rte_exit(EXIT_FAILURE, "We need at least 8 cores.\n"); if (rte_eth_dev_count_avail() < 2) @@ -196,8 +195,10 @@ int vrg_start(int argc, char **argv) if (rte_eth_dev_socket_id(1) > 0 && rte_eth_dev_socket_id(1) != (int)rte_lcore_to_socket_id(vrg_ccb.lcore.wan_thread)) printf("WARNING, WAN port is on remote NUMA node to polling thread.\n\tPerformance will not be optimal.\n"); + dbg_init((void *)&vrg_ccb); /* Read network config */ - if (parse_config("./config.cfg", &vrg_ccb) != SUCCESS) { + struct vrg_config vrg_cfg; + if (parse_config("/etc/vrg/config.cfg", &vrg_ccb, &vrg_cfg) != SUCCESS) { printf("parse config file error\n"); return -1; } @@ -206,6 +207,10 @@ int vrg_start(int argc, char **argv) vrg_ccb.user_count = 1; vrg_ccb.base_vlan = 2; } + vrg_ccb.unix_sock_path = vrg_cfg.unix_sock_path; + vrg_ccb.fp = fopen(vrg_cfg.log_path, "w+"); + if (vrg_ccb.fp) + rte_openlog_stream(vrg_ccb.fp); if (vrg_ccb.user_count < 1 || vrg_ccb.base_vlan < 2) rte_exit(EXIT_FAILURE, "vRG system configuration failed.\n"); diff --git a/src/vrg.h b/src/vrg.h index 790646f..92d6d43 100644 --- a/src/vrg.h +++ b/src/vrg.h @@ -42,7 +42,8 @@ typedef struct { volatile BOOL quit_flag; /* vRG quit flag */ U32 lan_ip; /* vRG LAN side ip */ struct lcore_map lcore; /* lcore map */ - FILE *fp; + char *unix_sock_path;/* vRG unix socket file path */ + FILE *fp; /* vRG log file pointer */ struct cmdline *cl; struct nic_info nic_info; PPP_INFO_t *ppp_ccb; /* pppoe control block */