Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Nov 19, 2023
1 parent 418ebae commit 387fdaa
Show file tree
Hide file tree
Showing 23 changed files with 380 additions and 366 deletions.
28 changes: 14 additions & 14 deletions src/cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <rte_memory.h>
#include <rte_eal.h>
#include <rte_atomic.h>
#include <rte_malloc.h>
#include <rte_branch_prediction.h>
#include <rte_launch.h>
#include <rte_per_lcore.h>
Expand All @@ -38,6 +37,7 @@
#include "dhcpd/dhcp_codec.h"
#include "init.h"
#include "vrg.h"
#include "utils.h"

extern struct rte_ring *rte_ring;
typedef struct cli_to_main_msg {
Expand Down Expand Up @@ -176,9 +176,9 @@ static void cmd_quit_parsed(__attribute__((unused)) void *parsed_result,
__attribute__((unused)) struct cmdline *cl,
__attribute__((unused)) void *data)
{
tVRG_MBX *mail = (tVRG_MBX *)rte_malloc(NULL, sizeof(tVRG_MBX), 0);
tVRG_MBX *mail = vrg_malloc(tVRG_MBX, sizeof(tVRG_MBX), 0);
if (mail == NULL) {
cmdline_printf(cl, "cmd_quit_parsed failed: rte_malloc failed: %s\n", rte_strerror(rte_errno));
cmdline_printf(cl, "cmd_quit_parsed failed: vrg_malloc failed: %s\n", rte_strerror(rte_errno));
return;
}
cli_to_main_msg_t *msg = (cli_to_main_msg_t *)mail->refp;
Expand Down Expand Up @@ -248,9 +248,9 @@ static void cmd_connect_parsed( void *parsed_result,
__attribute__((unused)) void *data)
{
struct cmd_connect_result *res = parsed_result;
tVRG_MBX *mail = (tVRG_MBX *)rte_malloc(NULL, sizeof(tVRG_MBX), 0);
tVRG_MBX *mail = vrg_malloc(tVRG_MBX, sizeof(tVRG_MBX), 0);
if (mail == NULL) {
cmdline_printf(cl, "cmd_connect_parsed failed: rte_malloc failed: %s\n", rte_strerror(rte_errno));
cmdline_printf(cl, "cmd_connect_parsed failed: vrg_malloc failed: %s\n", rte_strerror(rte_errno));
return;
}
cli_to_main_msg_t *msg = (cli_to_main_msg_t *)mail->refp;
Expand All @@ -266,14 +266,14 @@ static void cmd_connect_parsed( void *parsed_result,
msg->user_id = strtoul(res->user_id, NULL, 10);
if (msg->user_id <= 0) {
printf("Wrong user id\nvRG> ");
rte_free(mail);
vrg_mfree(mail);
return;
}
}

if (msg->user_id > vrg_ccb->user_count) {
printf("Too large user id\nvRG> ");
rte_free(mail);
vrg_mfree(mail);
return;
}

Expand Down Expand Up @@ -312,9 +312,9 @@ static void cmd_dhcp_parsed( void *parsed_result,
__attribute__((unused)) void *data)
{
struct cmd_dhcp_result *res = parsed_result;
tVRG_MBX *mail = (tVRG_MBX *)rte_malloc(NULL, sizeof(tVRG_MBX), 0);
tVRG_MBX *mail = vrg_malloc(tVRG_MBX, sizeof(tVRG_MBX), 0);
if (mail == NULL) {
cmdline_printf(cl, "cmd_dhcp_parsed failed: rte_malloc failed: %s\n", rte_strerror(rte_errno));
cmdline_printf(cl, "cmd_dhcp_parsed failed: vrg_malloc failed: %s\n", rte_strerror(rte_errno));
return;
}
cli_to_main_msg_t *msg = (cli_to_main_msg_t *)mail->refp;
Expand All @@ -325,7 +325,7 @@ static void cmd_dhcp_parsed( void *parsed_result,
msg->type = CLI_DHCP_STOP;
else {
printf("Wrong dhcp cmd\nvRG> ");
rte_free(mail);
vrg_mfree(mail);
return;
}

Expand All @@ -335,14 +335,14 @@ static void cmd_dhcp_parsed( void *parsed_result,
msg->user_id = strtoul(res->user_id, NULL, 10);
if (msg->user_id <= 0) {
printf("Wrong user id\nvRG> ");
rte_free(mail);
vrg_mfree(mail);
return;
}
}

if (msg->user_id > vrg_ccb->user_count) {
printf("Too large user id\nvRG> ");
rte_free(mail);
vrg_mfree(mail);
return;
}

Expand Down Expand Up @@ -382,9 +382,9 @@ cmdline_parse_ctx_t ctx[] = {
NULL,
};

STATUS init_cli(VRG_t *ccb)
STATUS init_cli(void *ccb)
{
vrg_ccb = ccb;
vrg_ccb = (VRG_t *)ccb;
vrg_ccb->cl = cmdline_stdin_new(ctx, "vRG> ");
if (vrg_ccb->cl == NULL)
return ERROR;
Expand Down
2 changes: 1 addition & 1 deletion src/cmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
#include "vrg.h"

extern cmdline_parse_ctx_t ctx[];
STATUS init_cli(VRG_t *vrg_ccb);
STATUS init_cli(void *vrg_ccb);

#endif
4 changes: 2 additions & 2 deletions src/dbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void LOGGER(U8 level, char *filename, int line_num, FILE *log_fp, void *ccb, voi
}
}

void dbg_init(VRG_t *ccb)
void dbg_init(void *ccb)
{
vrg_ccb = ccb;
vrg_ccb = (VRG_t *)ccb;
}
10 changes: 5 additions & 5 deletions src/dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#ifndef _DBG_H_
#define _DBG_H_

#include "vrg.h"
#include <common.h>

#define LOGDBG 1U
#define LOGINFO 2U
Expand All @@ -22,11 +22,11 @@ char *DHCP_state2str(U16 state);
/* log level, logfile fp, log msg */
#define VRG_LOG(lvl, fp, ccb, ccb2str, ...) LOGGER(LOG ## lvl, __FILE__, __LINE__, fp, ccb, ccb2str, __VA_ARGS__)

extern void LOGGER(U8 level, char *filename, int line_num, FILE *log_fp, void *ccb, void (*ccb2str)(void *, char *), const char *fmt,...);
void LOGGER(U8 level, char *filename, int line_num, FILE *log_fp, void *ccb, void (*ccb2str)(void *, char *), const char *fmt,...);
char *loglvl2str(U8 level);
U8 logstr2lvl(const char *log_str);
extern void PPPLOGMSG(void *ccb, char *buf);
extern void DHCPLOGMSG(void *ccb, char *buf);
void dbg_init(VRG_t *ccb);
void PPPLOGMSG(void *ccb, char *buf);
void DHCPLOGMSG(void *ccb, char *buf);
void dbg_init(void *ccb);

#endif
18 changes: 8 additions & 10 deletions src/dhcpd/dhcp_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "../vrg.h"
#include "dhcp_fsm.h"

extern struct lcore_map lcore;

extern STATUS build_dhcp_offer(dhcp_ccb_t *dhcp_ccb);
extern STATUS build_dhcp_ack(dhcp_ccb_t *dhcp_ccb);
extern STATUS build_dhcp_nak(dhcp_ccb_t *dhcp_ccb);
Expand All @@ -17,7 +15,7 @@ STATUS A_send_dhcp_nak(struct rte_timer *tim, dhcp_ccb_t *dhcp_ccb);
STATUS A_wait_lease_timer(struct rte_timer *tim, dhcp_ccb_t *dhcp_ccb);
STATUS A_release(struct rte_timer *tim, dhcp_ccb_t *dhcp_ccb);

static FILE *log_fp;
static VRG_t *vrg_ccb;

tDHCP_STATE_TBL dhcp_fsm_tbl[9] = {
/*//////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -62,9 +60,9 @@ STATUS dhcp_fsm(dhcp_ccb_t *dhcp_ccb, U16 event)
for(i=0; dhcp_fsm_tbl[i].state!=S_DHCP_INVLD; i++)
if (dhcp_fsm_tbl[i].state == dhcp_ccb->lan_user_info[dhcp_ccb->cur_lan_user_index].state)
break;
VRG_LOG(INFO, log_fp, (U8 *)dhcp_ccb, DHCPLOGMSG, "Current state is %s\n", DHCP_state2str(dhcp_fsm_tbl[i].state));
VRG_LOG(INFO, vrg_ccb->fp, (U8 *)dhcp_ccb, DHCPLOGMSG, "Current state is %s\n", DHCP_state2str(dhcp_fsm_tbl[i].state));
if (dhcp_fsm_tbl[i].state == S_DHCP_INVLD) {
VRG_LOG(INFO, log_fp, (U8 *)dhcp_ccb, DHCPLOGMSG, "Error! unknown state(%d) specified for the event(%d)\n",
VRG_LOG(INFO, vrg_ccb->fp, (U8 *)dhcp_ccb, DHCPLOGMSG, "Error! unknown state(%d) specified for the event(%d)\n",
dhcp_ccb->lan_user_info[dhcp_ccb->cur_lan_user_index].state, event);
return FALSE;
}
Expand All @@ -78,7 +76,7 @@ STATUS dhcp_fsm(dhcp_ccb_t *dhcp_ccb, U16 event)
break;

if (dhcp_fsm_tbl[i].state != dhcp_ccb->lan_user_info[dhcp_ccb->cur_lan_user_index].state) { /* search until meet the next state */
VRG_LOG(INFO, log_fp, (U8 *)dhcp_ccb, DHCPLOGMSG, "error! invalid event(%d) in state(%s)\n",
VRG_LOG(INFO, vrg_ccb->fp, (U8 *)dhcp_ccb, DHCPLOGMSG, "error! invalid event(%d) in state(%s)\n",
event, DHCP_state2str(dhcp_ccb->lan_user_info[dhcp_ccb->cur_lan_user_index].state));
return FALSE;
}
Expand All @@ -87,7 +85,7 @@ STATUS dhcp_fsm(dhcp_ccb_t *dhcp_ccb, U16 event)
if (dhcp_ccb->lan_user_info[dhcp_ccb->cur_lan_user_index].state != dhcp_fsm_tbl[i].next_state) {
strcpy(str1,DHCP_state2str(dhcp_ccb->lan_user_info[dhcp_ccb->cur_lan_user_index].state));
strcpy(str2,DHCP_state2str(dhcp_fsm_tbl[i].next_state));
VRG_LOG(INFO, log_fp, (U8 *)dhcp_ccb, DHCPLOGMSG, "dhcp state changed from %s to %s\n",str1,str2);
VRG_LOG(INFO, vrg_ccb->fp, (U8 *)dhcp_ccb, DHCPLOGMSG, "dhcp state changed from %s to %s\n",str1,str2);
dhcp_ccb->lan_user_info[dhcp_ccb->cur_lan_user_index].state = dhcp_fsm_tbl[i].next_state;
}

Expand Down Expand Up @@ -116,7 +114,7 @@ void request_timer(struct rte_timer *tim, dhcp_ccb_t *dhcp_ccb)
STATUS A_wait_request_timer(struct rte_timer *tim, dhcp_ccb_t *dhcp_ccb)
{
rte_timer_stop(tim);
rte_timer_reset(tim, 5 * rte_get_timer_hz(), SINGLE, lcore.timer_thread, (rte_timer_cb_t)request_timer, dhcp_ccb);
rte_timer_reset(tim, 5 * rte_get_timer_hz(), SINGLE, vrg_ccb->lcore.timer_thread, (rte_timer_cb_t)request_timer, dhcp_ccb);
return TRUE;
}

Expand Down Expand Up @@ -146,7 +144,7 @@ STATUS A_wait_lease_timer(struct rte_timer *tim, dhcp_ccb_t *dhcp_ccb)
dhcp_ccb_t dhcp_ccb_for_timer = *dhcp_ccb;

rte_timer_stop(tim);
rte_timer_reset(tim, LEASE_TIMEOUT * rte_get_timer_hz(), SINGLE, lcore.timer_thread, (rte_timer_cb_t)lease_timer, &dhcp_ccb_for_timer);
rte_timer_reset(tim, LEASE_TIMEOUT * rte_get_timer_hz(), SINGLE, vrg_ccb->lcore.timer_thread, (rte_timer_cb_t)lease_timer, &dhcp_ccb_for_timer);

return TRUE;
}
Expand All @@ -159,5 +157,5 @@ STATUS A_release(struct rte_timer *tim, dhcp_ccb_t *dhcp_ccb)

void dhcp_fsm_init(VRG_t *ccb)
{
log_fp = ccb->fp;
vrg_ccb = ccb;
}
9 changes: 3 additions & 6 deletions src/dhcpd/dhcpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
#include <rte_byteorder.h>
#include <rte_ether.h>
#include <rte_ip.h>
#include <rte_malloc.h>
#include <sys/mman.h>
#include "../vrg.h"
#include "dhcp_fsm.h"

extern struct lcore_map lcore;

extern STATUS dhcp_fsm(dhcp_ccb_t *dhcp_ccb, U16 event);
void release_lan_user(dhcp_ccb_t *dhcp_ccb);

struct rte_ether_addr zero_mac;
static VRG_t *vrg_ccb;

STATUS dhcp_init(VRG_t *ccb)
STATUS dhcp_init(void *ccb)
{
vrg_ccb = ccb;
vrg_ccb = (VRG_t *)ccb;
vrg_ccb->dhcp_ccb = mmap(NULL, sizeof(dhcp_ccb_t)*vrg_ccb->user_count, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
if (vrg_ccb->dhcp_ccb == MAP_FAILED) {
perror("map mem");
Expand Down Expand Up @@ -80,7 +77,7 @@ int dhcpd(struct rte_mbuf *single_pkt, struct rte_ether_hdr *eth_hdr, vlan_heade
}
/* If no more packet from the host, clear all information in dhcp_ccb */
rte_timer_stop(&dhcp_ccb[user_index].lan_user_info[lan_user_index].timer);
rte_timer_reset(&dhcp_ccb[user_index].lan_user_info[lan_user_index].timer, LEASE_TIMEOUT * 2 * rte_get_timer_hz(), SINGLE, lcore.timer_thread, (rte_timer_cb_t)release_lan_user, &dhcp_ccb[user_index]);
rte_timer_reset(&dhcp_ccb[user_index].lan_user_info[lan_user_index].timer, LEASE_TIMEOUT * 2 * rte_get_timer_hz(), SINGLE, vrg_ccb->lcore.timer_thread, (rte_timer_cb_t)release_lan_user, &dhcp_ccb[user_index]);

event = dhcp_decode(&dhcp_ccb[user_index], eth_hdr, vlan_header, ip_hdr, udp_hdr);
if (event < 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/dhcpd/dhcpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
#include "../vrg.h"

int dhcpd(struct rte_mbuf *single_pkt, struct rte_ether_hdr *eth_hdr, vlan_header_t *vlan_header, struct rte_ipv4_hdr *ip_hdr, struct rte_udp_hdr *udp_hdr, U16 user_index);
STATUS dhcp_init(VRG_t *ccb);
STATUS dhcp_init(void *ccb);

#endif
25 changes: 5 additions & 20 deletions src/dp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <inttypes.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_malloc.h>
#include <rte_cycles.h>
#include <rte_lcore.h>
#include <rte_mbuf.h>
Expand All @@ -27,6 +26,7 @@
#include "dbg.h"
#include "dp.h"
#include "trace.h"
#include "utils.h"

#define RX_RING_SIZE 128

Expand Down Expand Up @@ -57,8 +57,6 @@ static struct rte_eth_conf port_conf_default = {
.intr_conf = {
.lsc = 1, /**< link status interrupt feature enabled */ },
};
extern STATUS PPP_FSM(struct rte_timer *ppp, PPP_INFO_t *s_ppp_ccb, U16 event);
int control_plane_dequeue(tVRG_MBX **mail);
static int lsi_event_callback(U16 port_id, enum rte_eth_event_type type, void *param);

int PORT_INIT(VRG_t *vrg_ccb, U16 port)
Expand Down Expand Up @@ -127,15 +125,15 @@ int wan_recvd(void *arg)
struct rte_mbuf *pkt[BURST_SIZE];
U16 ori_port_id, nb_rx;
ppp_payload_t *ppp_payload;
tVRG_MBX *mail = rte_malloc(NULL, sizeof(tVRG_MBX)*32, 65536);
tVRG_MBX *mail = vrg_malloc(tVRG_MBX, sizeof(tVRG_MBX)*32, 65536);
int i;
U32 icmp_new_cksum;
char *cur;
U16 user_index;
VRG_t *vrg_ccb = (VRG_t *)arg;

if (mail == NULL) {
VRG_LOG(ERR, vrg_ccb->fp, NULL, NULL, "wan_recvd failed: rte_malloc failed: %s\n", rte_strerror(rte_errno));
VRG_LOG(ERR, vrg_ccb->fp, NULL, NULL, "wan_recvd failed: vrg_malloc failed: %s\n", rte_strerror(rte_errno));
return -1;
}

Expand Down Expand Up @@ -368,19 +366,6 @@ int downlink(void *arg)
return 0;
}

int control_plane_dequeue(tVRG_MBX **mail)
{
U16 burst_size;

for(;;) {
burst_size = rte_ring_dequeue_burst(rte_ring,(void **)mail,BURST_SIZE,NULL);
if (likely(burst_size == 0))
continue;
break;
}
return burst_size;
}

int uplink(void *arg)
{
struct rte_mbuf *pkt[BURST_SIZE], *single_pkt;
Expand Down Expand Up @@ -770,10 +755,10 @@ void drv_xmit(VRG_t *vrg_ccb, U8 *mu, U16 mulen)
static int lsi_event_callback(U16 port_id, enum rte_eth_event_type type, void *param)
{
struct rte_eth_link link;
tVRG_MBX *mail = (tVRG_MBX *)rte_malloc(NULL, sizeof(tVRG_MBX), 2048);
tVRG_MBX *mail = vrg_malloc(tVRG_MBX, sizeof(tVRG_MBX), 2048);

if (mail == NULL) {
VRG_LOG(ERR, NULL, NULL, NULL, "lsi_event_callback failed: rte_malloc failed: %s\n", rte_strerror(rte_errno));
VRG_LOG(ERR, NULL, NULL, NULL, "lsi_event_callback failed: vrg_malloc failed: %s\n", rte_strerror(rte_errno));
return -1;
}

Expand Down
1 change: 0 additions & 1 deletion src/dp.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ int downlink(void *arg);
int gateway(void *arg);
int lan_recvd(void *arg);
int PORT_INIT(VRG_t *vrg_ccb, U16 port);
extern int control_plane_dequeue(tVRG_MBX **mail);

#endif
Loading

0 comments on commit 387fdaa

Please sign in to comment.