Skip to content

Commit

Permalink
add ppp terminate request unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Oct 31, 2023
1 parent ffc4688 commit f503234
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
######################################
CC = gcc
INCLUDE =
CFLAGS = $(INCLUDE) -Wall -g $(shell pkg-config --cflags libdpdk) -O3 -DALLOW_EXPERIMENTAL_API -D_TEST_MODE
CFLAGS = $(INCLUDE) -Wall -g $(shell pkg-config --cflags libdpdk) -O3 -DALLOW_EXPERIMENTAL_API -D_TEST_MODE #-Wextra -fsanitize=address

LDFLAGS = $(shell pkg-config --static --libs libdpdk) -lutils -lconfig

Expand Down
11 changes: 7 additions & 4 deletions src/dbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "dhcpd/dhcp_fsm.h"
#include "dbg.h"

#define DBG_VRG_MSG_LEN 256
#define DBG_VRG_MSG_LEN 2048
#define LOGGER_BUF_LEN 1024

char *PPP_state2str(U16 state);
Expand Down Expand Up @@ -59,7 +59,7 @@ void PPPLOGMSG(void *ccb, char *buf)
{
PPP_INFO_t *s_ppp_ccb = (PPP_INFO_t *)ccb;
if (s_ppp_ccb) {
sprintf(buf,"pppd> Session id [%x.%s] ", rte_be_to_cpu_16(s_ppp_ccb->session_id), PPP_state2str(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state));
sprintf(buf, "pppd> Session id [%x.%s] ", rte_be_to_cpu_16(s_ppp_ccb->session_id), PPP_state2str(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].state));
}
}

Expand Down Expand Up @@ -145,8 +145,10 @@ char *DHCP_state2str(U16 state)
void LOGGER(U8 level, char *filename, int line_num, FILE *log_fp, void *ccb, void (*ccb2str)(void *, char *), const char *fmt,...)
{
va_list ap; /* points to each unnamed arg in turn */
char buf[LOGGER_BUF_LEN], protocol_buf[LOGGER_BUF_LEN-20] = {'\0'}, msg[DBG_VRG_MSG_LEN];
char buf[LOGGER_BUF_LEN], protocol_buf[LOGGER_BUF_LEN-100], msg[DBG_VRG_MSG_LEN];

protocol_buf[0] = '\0';

//user offer level must > system requirement
if (vrg_ccb->loglvl > level)
return;
Expand All @@ -156,7 +158,8 @@ void LOGGER(U8 level, char *filename, int line_num, FILE *log_fp, void *ccb, voi

if (ccb2str)
ccb2str(ccb, protocol_buf);
sprintf(buf, "vRG[%s]: %s:%d> %s", loglvl2str(level), filename, line_num, protocol_buf);

snprintf(buf, sizeof(buf)-1, "vRG[%s]: %s:%d> %s", loglvl2str(level), filename, line_num, protocol_buf);
strncat(buf, msg, sizeof(buf)-1);
va_end(ap);

Expand Down
3 changes: 1 addition & 2 deletions src/ethtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ int rte_ethtool_get_drvinfo(U16 port_id, struct ethtool_drvinfo *drvinfo)

RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -ENODEV);

/*int ret = rte_eth_dev_fw_version_get(port_id, drvinfo->fw_version,
int ret = rte_eth_dev_fw_version_get(port_id, drvinfo->fw_version,
sizeof(drvinfo->fw_version));
if (ret < 0)
printf("firmware version get error: (%s)\n", strerror(-ret));
else if (ret > 0)
printf("Insufficient fw version buffer size, "
"the minimum size should be %d\n", ret);
*/

memset(&dev_info, 0, sizeof(dev_info));
rte_eth_dev_info_get(port_id, &dev_info);
Expand Down
22 changes: 8 additions & 14 deletions src/pppd/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ STATUS PPP_decode_frame(tVRG_MBX *mail, struct rte_ether_hdr *eth_hdr, vlan_head
U16 mulen;
struct rte_timer *tim = &s_ppp_ccb->ppp;

if (mail->len > ETH_MTU){
if (mail->len > ETH_MTU) {
VRG_LOG(ERR, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "error! too large frame(%d)", mail->len);
/* TODO: store pkt buffer to log file, not just print out */
PRINT_MESSAGE(mail->refp, mail->len);
return ERROR;
}
Expand Down Expand Up @@ -903,13 +904,13 @@ STATUS build_terminate_ack(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16 *mu
* output: TRUE/FALSE
* return: packet buffer
*/
STATUS build_terminate_request(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen)
void build_terminate_request(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
{
struct rte_ether_hdr *eth_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].eth_hdr;
vlan_header_t *vlan_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].vlan_header;
pppoe_header_t *pppoe_header = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].pppoe_header;
ppp_payload_t *ppp_payload = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_payload;
ppp_header_t *ppp_hdr = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_hdr;
struct rte_ether_hdr *eth_hdr = (struct rte_ether_hdr *)buffer;
vlan_header_t *vlan_header = (vlan_header_t *)(eth_hdr + 1);
pppoe_header_t *pppoe_header = (pppoe_header_t *)(vlan_header + 1);
ppp_payload_t *ppp_payload = (ppp_payload_t *)(pppoe_header + 1);
ppp_header_t *ppp_hdr = (ppp_header_t *)(ppp_payload + 1);

rte_ether_addr_copy(&vrg_ccb->nic_info.hsi_wan_src_mac, &eth_hdr->src_addr);
rte_ether_addr_copy(&s_ppp_ccb->PPP_dst_mac, &eth_hdr->dst_addr);
Expand Down Expand Up @@ -942,15 +943,8 @@ STATUS build_terminate_request(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16
*mulen = pppoe_header->length + sizeof(struct rte_ether_hdr) + sizeof(pppoe_header_t) + sizeof(vlan_header_t);
pppoe_header->length = rte_cpu_to_be_16(pppoe_header->length);
ppp_hdr->length = rte_cpu_to_be_16(ppp_hdr->length);
memset(buffer,0,MSG_BUF);
rte_memcpy(buffer,eth_hdr,sizeof(struct rte_ether_hdr));
rte_memcpy(buffer+sizeof(struct rte_ether_hdr),vlan_header,sizeof(vlan_header_t));
rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t),pppoe_header,sizeof(pppoe_header_t));
rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t),ppp_payload,sizeof(ppp_payload_t));
rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t),ppp_hdr,sizeof(ppp_header_t));

VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " terminate request built.", s_ppp_ccb->user_num);
return TRUE;
}

STATUS build_code_reject(__attribute__((unused)) unsigned char* buffer, __attribute__((unused)) PPP_INFO_t *s_ppp_ccb, __attribute__((unused)) U16 *mulen)
Expand Down
2 changes: 1 addition & 1 deletion src/pppd/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void build_config_ack(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb);
extern STATUS build_config_nak_rej(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
extern STATUS build_terminate_ack(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
extern STATUS build_code_reject(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
extern STATUS build_terminate_request(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
void build_terminate_request(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb);
extern STATUS build_echo_reply(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
extern STATUS build_auth_request_pap(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
extern STATUS build_auth_ack_pap(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
Expand Down
5 changes: 2 additions & 3 deletions src/pppd/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ STATUS A_send_config_ack(__attribute__((unused)) struct rte_timer *tim, __attrib
return TRUE;
}

STATUS A_send_terminate_request(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) PPP_INFO_t *s_ppp_ccb)
STATUS A_send_terminate_request(struct rte_timer *tim, PPP_INFO_t *s_ppp_ccb)
{
unsigned char buffer[MSG_BUF];
U16 mulen;
Expand All @@ -824,8 +824,7 @@ STATUS A_send_terminate_request(__attribute__((unused)) struct rte_timer *tim, _
VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " terminate request timeout.\n", s_ppp_ccb->user_num);
PPP_FSM(tim,s_ppp_ccb,E_TIMEOUT_COUNTER_EXPIRED);
}
if (build_terminate_request(buffer,s_ppp_ccb,&mulen) < 0)
return FALSE;
build_terminate_request(buffer, &mulen, s_ppp_ccb);
drv_xmit(vrg_ccb, buffer, mulen);
s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].timer_counter--;

Expand Down
34 changes: 34 additions & 0 deletions src/pppd/pppd.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ STATUS pppdInit(void *ccb)
rte_atomic16_init(&ppp_ccb[i].addr_table[j].is_fill);
}
memset(ppp_ccb[i].PPP_dst_mac.addr_bytes, 0, ETH_ALEN);
//ppp_ccb[i].fp = vrg_ccb->fp;
rte_timer_init(&(ppp_ccb[i].pppoe));
rte_timer_init(&(ppp_ccb[i].ppp));
rte_timer_init(&(ppp_ccb[i].nat));
Expand All @@ -176,6 +177,39 @@ STATUS pppdInit(void *ccb)
return SUCCESS;
}

STATUS ppp_connect(PPP_INFO_t *ppp_ccb, U16 user_id)
{
if (ppp_ccb->phase > END_PHASE) {
VRG_LOG(ERR, vrg_ccb->fp, ppp_ccb, PPPLOGMSG, "Error! User %u is in a pppoe connection", user_id);
return ERROR;
}
ppp_ccb->phase = PPPOE_PHASE;
ppp_ccb->pppoe_phase.max_retransmit = MAX_RETRAN;
ppp_ccb->pppoe_phase.timer_counter = 0;
if (send_pkt(ENCODE_PADI, ppp_ccb) == ERROR)
PPP_bye(ppp_ccb);
/* set ppp starting boolean flag to TRUE */
rte_atomic16_set(&ppp_ccb->ppp_bool, 1);
rte_timer_reset(&ppp_ccb->pppoe, rte_get_timer_hz(), PERIODICAL, lcore.timer_thread, (rte_timer_cb_t)A_padi_timer_func, ppp_ccb);

return SUCCESS;
}

STATUS ppp_disconnect(PPP_INFO_t *ppp_ccb, U16 user_id)
{
if (ppp_ccb->phase == END_PHASE) {
VRG_LOG(ERR, vrg_ccb->fp, ppp_ccb, PPPLOGMSG, "Error! User %u is in init phase", user_id);
return ERROR;
}
if (ppp_ccb->ppp_processing == TRUE) {
VRG_LOG(ERR, vrg_ccb->fp, ppp_ccb, PPPLOGMSG, "Error! User %u is disconnecting pppoe connection, please wait...", user_id);
return ERROR;
}
PPP_bye(ppp_ccb);

return SUCCESS;
}

void exit_ppp(__attribute__((unused)) struct rte_timer *tim, PPP_INFO_t *ppp_ccb)
{
rte_atomic16_cmpset((U16 *)&(ppp_ccb->ppp_bool.cnt), 1, 0);
Expand Down
3 changes: 3 additions & 0 deletions src/pppd/pppd.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ 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 All @@ -67,6 +68,8 @@ extern U32 ppp_interval;
void PPP_int(void);
void exit_ppp(__attribute__((unused)) struct rte_timer *tim, PPP_INFO_t *ppp_ccb);
extern STATUS ppp_process(void *mail);
STATUS ppp_connect(PPP_INFO_t *ppp_ccb, U16 user_id);
STATUS ppp_disconnect(PPP_INFO_t *ppp_ccb, U16 user_id);
STATUS pppdInit(void *ccb);
void PPP_bye(PPP_INFO_t *ppp_ccb);

Expand Down
1 change: 1 addition & 0 deletions src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <common.h>

#define ETH_MTU 1500
#define ETH_JUMBO 9000

#define ETH_P_PPP_DIS 0x8863
#define ETH_P_PPP_SES 0x8864
Expand Down
52 changes: 7 additions & 45 deletions src/vrg.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,61 +192,23 @@ int vrg_loop(VRG_t *vrg_ccb)
case CLI_DISCONNECT:
if (user_id == 0) {
for(int j=0; j<vrg_ccb->user_count; j++) {
if (vrg_ccb->ppp_ccb[j].phase == END_PHASE) {
VRG_LOG(ERR, vrg_ccb->fp, &(vrg_ccb->ppp_ccb[j]), PPPLOGMSG, "Error! User %u is in init phase", j + 1);
if (ppp_disconnect(&(vrg_ccb->ppp_ccb[j]), j+1) != SUCCESS)
continue;
}
if (vrg_ccb->ppp_ccb[j].ppp_processing == TRUE) {
VRG_LOG(ERR, vrg_ccb->fp, &(vrg_ccb->ppp_ccb[j]), PPPLOGMSG, "Error! User %u is disconnecting pppoe connection, please wait...", j + 1);
continue;
}
PPP_bye(&vrg_ccb->ppp_ccb[j]);
}
}
else {
if (vrg_ccb->ppp_ccb[user_id-1].phase == END_PHASE) {
VRG_LOG(ERR, vrg_ccb->fp, &(vrg_ccb->ppp_ccb[user_id-1]), PPPLOGMSG, "Error! User %u is in init phase", user_id);
break;
}
if (vrg_ccb->ppp_ccb[user_id-1].ppp_processing == TRUE) {
VRG_LOG(ERR, vrg_ccb->fp, &(vrg_ccb->ppp_ccb[user_id-1]), PPPLOGMSG, "Error! User %u is disconnecting pppoe connection, please wait...", user_id);
break;
}
PPP_bye(&vrg_ccb->ppp_ccb[user_id-1]);
}
else
ppp_disconnect(&(vrg_ccb->ppp_ccb[user_id-1]), user_id);
break;
case CLI_CONNECT:
if (user_id == 0) {
for(int j=0; j<vrg_ccb->user_count; j++) {
if (vrg_ccb->ppp_ccb[j].phase > END_PHASE) {
VRG_LOG(ERR, vrg_ccb->fp, &(vrg_ccb->ppp_ccb[j]), PPPLOGMSG, "Error! User %u is in a pppoe connection", j + 1);
continue;
}
vrg_ccb->cur_user++;
vrg_ccb->ppp_ccb[j].phase = PPPOE_PHASE;
vrg_ccb->ppp_ccb[j].pppoe_phase.max_retransmit = MAX_RETRAN;
vrg_ccb->ppp_ccb[j].pppoe_phase.timer_counter = 0;
if (send_pkt(ENCODE_PADI, &(vrg_ccb->ppp_ccb[j])) == ERROR)
PPP_bye(&(vrg_ccb->ppp_ccb[j]));
/* set ppp starting boolean flag to TRUE */
rte_atomic16_set(&vrg_ccb->ppp_ccb[j].ppp_bool, 1);
rte_timer_reset(&(vrg_ccb->ppp_ccb[j].pppoe), rte_get_timer_hz(), PERIODICAL, lcore.timer_thread, (rte_timer_cb_t)A_padi_timer_func, &(vrg_ccb->ppp_ccb[j]));
if (ppp_connect(&(vrg_ccb->ppp_ccb[j]), j+1) == SUCCESS)
vrg_ccb->cur_user++;
}
}
else {
if (vrg_ccb->ppp_ccb[user_id-1].phase > END_PHASE) {
VRG_LOG(ERR, vrg_ccb->fp, &(vrg_ccb->ppp_ccb[user_id-1]), PPPLOGMSG, "Error! User %u is in a pppoe connection", user_id);
break;
}
vrg_ccb->cur_user++;
vrg_ccb->ppp_ccb[user_id-1].phase = PPPOE_PHASE;
vrg_ccb->ppp_ccb[user_id-1].pppoe_phase.max_retransmit = MAX_RETRAN;
vrg_ccb->ppp_ccb[user_id-1].pppoe_phase.timer_counter = 0;
if (send_pkt(ENCODE_PADI, &(vrg_ccb->ppp_ccb[user_id-1])) == ERROR)
PPP_bye(&(vrg_ccb->ppp_ccb[user_id-1]));
/* set ppp starting boolean flag to TRUE */
rte_atomic16_set(&vrg_ccb->ppp_ccb[user_id-1].ppp_bool, 1);
rte_timer_reset(&(vrg_ccb->ppp_ccb[user_id-1].pppoe), rte_get_timer_hz(), PERIODICAL, lcore.timer_thread, (rte_timer_cb_t)A_padi_timer_func, &(vrg_ccb->ppp_ccb[user_id-1]));
if (ppp_connect(&(vrg_ccb->ppp_ccb[user_id-1]), user_id) == SUCCESS)
vrg_ccb->cur_user++;
}
break;
case CLI_DHCP_START:
Expand Down
2 changes: 1 addition & 1 deletion src/vrg.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct lcore_map {
*/
typedef struct {
U16 type;
U8 refp[ETH_MTU];
U8 refp[ETH_JUMBO];
int len;
} tVRG_MBX;

Expand Down
43 changes: 43 additions & 0 deletions unit_test/pppd/codec_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,46 @@ void test_build_config_ack() {
assert(mulen == sizeof(pkt_ipcp));
assert(memcmp(buffer, pkt_ipcp, sizeof(pkt_ipcp)) == 0);
}

void test_build_terminate_request() {
U8 buffer[80];
U16 mulen = 0;

PPP_INFO_t s_ppp_ccb_1 = {
.ppp_phase = {{
.timer_counter = 0,
.max_retransmit = 10,
},{
.timer_counter = 0,
.max_retransmit = 10,
},},
.user_num = 1,
.vlan = 2,
.PPP_dst_mac = (struct rte_ether_addr){
.addr_bytes = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31},
},
.session_id = htons(0x000a),
.cp = 0,
};

char pkt_lcp[] = {/* mac */0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4,
0x61, 0x16, 0xdd, 0x81, 0x00, /* vlan */0x00, 0x02, 0x88, 0x64, /* pppoe hdr */
0x11, 0x00, 0x00, 0x0a, 0x00, 0x06, /* ppp protocol */0xc0, 0x21, /* ppp hdr*/
0x05, 0x03, 0x00, 0x04};
char pkt_ipcp[] = {/* mac */0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4,
0x61, 0x16, 0xdd, 0x81, 0x00, /* vlan */0x00, 0x02, 0x88, 0x64, /* pppoe hdr */
0x11, 0x00, 0x00, 0x0a, 0x00, 0x06, /* ppp protocol */0x80, 0x21, /* ppp hdr*/
0x05, 0x03, 0x00, 0x04};

/* test LCP */
build_terminate_request(buffer, &mulen, &s_ppp_ccb_1);
assert(mulen == sizeof(pkt_lcp));
assert(memcmp(buffer, pkt_lcp, sizeof(pkt_lcp)) == 0);

/* test IPCP */
memset(buffer, 0, sizeof(buffer));
s_ppp_ccb_1.cp = 1;
build_terminate_request(buffer, &mulen, &s_ppp_ccb_1);
assert(mulen == sizeof(pkt_ipcp));
assert(memcmp(buffer, pkt_ipcp, sizeof(pkt_ipcp)) == 0);
}

0 comments on commit f503234

Please sign in to comment.