From f5032342cf4726f0c4a5d7bbef6ac14da30cca9f Mon Sep 17 00:00:00 2001 From: the Date: Tue, 31 Oct 2023 18:47:48 +0800 Subject: [PATCH] add ppp terminate request unit test --- Makefile | 2 +- src/dbg.c | 11 +++++--- src/ethtool.c | 3 +-- src/pppd/codec.c | 22 ++++++---------- src/pppd/codec.h | 2 +- src/pppd/fsm.c | 5 ++-- src/pppd/pppd.c | 34 ++++++++++++++++++++++++ src/pppd/pppd.h | 3 +++ src/protocol.h | 1 + src/vrg.c | 52 +++++-------------------------------- src/vrg.h | 2 +- unit_test/pppd/codec_test.c | 43 ++++++++++++++++++++++++++++++ 12 files changed, 109 insertions(+), 71 deletions(-) diff --git a/Makefile b/Makefile index 22a3719..399d6fb 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/dbg.c b/src/dbg.c index 0d7fbd9..acad660 100644 --- a/src/dbg.c +++ b/src/dbg.c @@ -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); @@ -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)); } } @@ -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; @@ -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); diff --git a/src/ethtool.c b/src/ethtool.c index 84d39d4..d86e626 100644 --- a/src/ethtool.c +++ b/src/ethtool.c @@ -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); diff --git a/src/pppd/codec.c b/src/pppd/codec.c index 91d8517..e70eef5 100644 --- a/src/pppd/codec.c +++ b/src/pppd/codec.c @@ -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; } @@ -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, ð_hdr->src_addr); rte_ether_addr_copy(&s_ppp_ccb->PPP_dst_mac, ð_hdr->dst_addr); @@ -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) diff --git a/src/pppd/codec.h b/src/pppd/codec.h index 0bfaf2f..49b89dd 100644 --- a/src/pppd/codec.h +++ b/src/pppd/codec.h @@ -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); diff --git a/src/pppd/fsm.c b/src/pppd/fsm.c index 7a89cce..017459a 100644 --- a/src/pppd/fsm.c +++ b/src/pppd/fsm.c @@ -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; @@ -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--; diff --git a/src/pppd/pppd.c b/src/pppd/pppd.c index cc2acd2..aadde60 100644 --- a/src/pppd/pppd.c +++ b/src/pppd/pppd.c @@ -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)); @@ -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); diff --git a/src/pppd/pppd.h b/src/pppd/pppd.h index a338f3c..2e919bd 100644 --- a/src/pppd/pppd.h +++ b/src/pppd/pppd.h @@ -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 */ @@ -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); diff --git a/src/protocol.h b/src/protocol.h index 7eeb120..c99b296 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -4,6 +4,7 @@ #include #define ETH_MTU 1500 +#define ETH_JUMBO 9000 #define ETH_P_PPP_DIS 0x8863 #define ETH_P_PPP_SES 0x8864 diff --git a/src/vrg.c b/src/vrg.c index 24206a0..afc19e6 100644 --- a/src/vrg.c +++ b/src/vrg.c @@ -192,61 +192,23 @@ int vrg_loop(VRG_t *vrg_ccb) case CLI_DISCONNECT: if (user_id == 0) { for(int j=0; juser_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; juser_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: diff --git a/src/vrg.h b/src/vrg.h index 4269220..9835cdf 100644 --- a/src/vrg.h +++ b/src/vrg.h @@ -59,7 +59,7 @@ struct lcore_map { */ typedef struct { U16 type; - U8 refp[ETH_MTU]; + U8 refp[ETH_JUMBO]; int len; } tVRG_MBX; diff --git a/unit_test/pppd/codec_test.c b/unit_test/pppd/codec_test.c index a429ad9..53503dc 100644 --- a/unit_test/pppd/codec_test.c +++ b/unit_test/pppd/codec_test.c @@ -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); +}