Skip to content

Commit

Permalink
add configs
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Nov 26, 2023
1 parent f76e46c commit 81fc2ed
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 20 deletions.
4 changes: 4 additions & 0 deletions boot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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";
21 changes: 17 additions & 4 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
#include <libconfig.h>
#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;
Expand All @@ -26,17 +27,29 @@ 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;

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);

Expand Down
7 changes: 6 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions src/pppd/nat.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef _NAT_H_
#define _NAT_H_

#include <stdint.h>
#include <inttypes.h>
#include <rte_eal.h>
Expand All @@ -20,9 +23,6 @@
#include <rte_memcpy.h>
#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[])
Expand Down
6 changes: 3 additions & 3 deletions src/pppd/pppd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/pppd/pppd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
52 changes: 52 additions & 0 deletions src/unix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <common.h>
#include <sys/un.h>
#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;
}
8 changes: 8 additions & 0 deletions src/unix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _UNIX_H_
#define _UNIX_H_

#include <common.h>

STATUS init_unix_sock(VRG_t *vrg_ccb);

#endif
19 changes: 12 additions & 7 deletions src/vrg.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "config.h"
#include "timer.h"
#include "utils.h"
#include "unix.h"

#define BURST_SIZE 32

Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
Expand All @@ -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;
}
Expand All @@ -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");
Expand Down
3 changes: 2 additions & 1 deletion src/vrg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 81fc2ed

Please sign in to comment.