From 29b0d998aee860b8da9858f0b16e725774738e7f Mon Sep 17 00:00:00 2001 From: the Date: Fri, 13 Oct 2023 01:28:33 +0800 Subject: [PATCH] add padr unit test --- src/pppd/codec.c | 73 ++++++++++++++++++++--------------- src/pppd/codec.h | 2 +- src/pppd/fsm.c | 5 +++ src/pppd/fsm.h | 1 + src/pppd/pppd.c | 4 +- unit_test/pppd/codec_test.c | 77 +++++++++++++++++++++++++------------ unit_test/test.c | 25 +++++++++++- unit_test/test.h | 1 + 8 files changed, 128 insertions(+), 60 deletions(-) diff --git a/src/pppd/codec.c b/src/pppd/codec.c index d24d301..1070cc6 100644 --- a/src/pppd/codec.c +++ b/src/pppd/codec.c @@ -487,7 +487,7 @@ STATUS build_padi(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb) if (s_ppp_ccb->pppoe_phase.timer_counter >= s_ppp_ccb->pppoe_phase.max_retransmit) { VRG_LOG(ERR, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " timeout when sending PADI", s_ppp_ccb->user_num); - return FALSE; + return ERROR; } for(int i=0; ipppoe_phase.timer_counter >= s_ppp_ccb->pppoe_phase.max_retransmit) { VRG_LOG(ERR, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 "timeout when sending PADR", s_ppp_ccb->user_num); - PPP_bye(s_ppp_ccb); + return ERROR; } - if (s_ppp_ccb->pppoe_phase.timer_counter > 0) - goto send; + rte_ether_addr_copy(&vrg_ccb->nic_info.hsi_wan_src_mac, &s_ppp_ccb->pppoe_phase.eth_hdr->src_addr); rte_ether_addr_copy(&s_ppp_ccb->PPP_dst_mac, &s_ppp_ccb->pppoe_phase.eth_hdr->dst_addr); s_ppp_ccb->pppoe_phase.pppoe_header->code = PADR; U32 total_tag_length = 0; - for(pppoe_header_tag_t *cur = tmp_pppoe_header_tag;;) { - cur->type = s_ppp_ccb->pppoe_phase.pppoe_header_tag->type; - cur->length = s_ppp_ccb->pppoe_phase.pppoe_header_tag->length; - switch(ntohs(s_ppp_ccb->pppoe_phase.pppoe_header_tag->type)) { + pppoe_header_tag_t *cur = s_ppp_ccb->pppoe_phase.pppoe_header_tag; + pppoe_header_tag->length = 0; + pppoe_header_tag->type = rte_cpu_to_be_16(SERVICE_NAME); + pppoe_header_tag += 1; + total_tag_length += sizeof(pppoe_header_tag_t); + for(;;) { + pppoe_header_tag->type = cur->type; + pppoe_header_tag->length = cur->length; + U16 tag_len = ntohs(cur->length); + switch(ntohs(cur->type)) { case END_OF_LIST: break; case SERVICE_NAME: break; case AC_NAME: /* We dont need to add ac-name tag to PADR. */ - s_ppp_ccb->pppoe_phase.pppoe_header_tag = (pppoe_header_tag_t *)((char *)(s_ppp_ccb->pppoe_phase.pppoe_header_tag) + 4 + ntohs(s_ppp_ccb->pppoe_phase.pppoe_header_tag->length)); + cur = (pppoe_header_tag_t *)((char *)cur + sizeof(pppoe_header_tag_t) + tag_len); continue; case HOST_UNIQ: case AC_COOKIE: case RELAY_ID: - if (cur->length != 0) - rte_memcpy(cur->value,s_ppp_ccb->pppoe_phase.pppoe_header_tag->value,ntohs(cur->length)); + if (cur->length != 0) { + rte_memcpy(pppoe_header_tag->value, cur->value, tag_len); + total_tag_length = tag_len + sizeof(pppoe_header_tag_t) + total_tag_length; + } break; case GENERIC_ERROR: VRG_LOG(ERR, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "PPPoE discover generic error."); @@ -565,28 +575,23 @@ STATUS build_padr(__attribute__((unused)) struct rte_timer *tim, PPP_INFO_t *s_p default: VRG_LOG(WARN, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "Unknown PPPOE tag value."); } - if (ntohs(s_ppp_ccb->pppoe_phase.pppoe_header_tag->type) == END_OF_LIST) + if (ntohs(cur->type) == END_OF_LIST) break; /* to caculate total pppoe header tags' length, we need to add tag type and tag length field in each tag scanning. */ - total_tag_length = ntohs(cur->length) + 4 + total_tag_length; /* Fetch next tag field. */ - s_ppp_ccb->pppoe_phase.pppoe_header_tag = (pppoe_header_tag_t *)((char *)(s_ppp_ccb->pppoe_phase.pppoe_header_tag) + 4 + ntohs(s_ppp_ccb->pppoe_phase.pppoe_header_tag->length)); - cur = (pppoe_header_tag_t *)((char *)cur + 4 + ntohs(cur->length)); + cur = (pppoe_header_tag_t *)((char *)cur + sizeof(pppoe_header_tag_t) + tag_len); + pppoe_header_tag = (pppoe_header_tag_t *)((char *)pppoe_header_tag + sizeof(pppoe_header_tag_t) + tag_len); } s_ppp_ccb->pppoe_phase.pppoe_header->length = rte_cpu_to_be_16(total_tag_length); - mulen = sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t) + total_tag_length; + *mulen = sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t) + total_tag_length; - rte_memcpy(buffer,s_ppp_ccb->pppoe_phase.eth_hdr,sizeof(struct rte_ether_hdr)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr),s_ppp_ccb->pppoe_phase.vlan_header,sizeof(vlan_header_t)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t),s_ppp_ccb->pppoe_phase.pppoe_header,sizeof(pppoe_header_t)); - rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t),tmp_pppoe_header_tag,total_tag_length); -send: - drv_xmit(vrg_ccb, buffer, mulen); - s_ppp_ccb->pppoe_phase.timer_counter++; + *eth_hdr = *(s_ppp_ccb->pppoe_phase.eth_hdr); + *vlan_header = *(s_ppp_ccb->pppoe_phase.vlan_header); + *pppoe_header = *(s_ppp_ccb->pppoe_phase.pppoe_header); - return TRUE; + return SUCCESS; } /** @@ -1126,18 +1131,22 @@ STATUS build_auth_response_chap(U8 *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen, p STATUS send_pkt(U8 encode_type, PPP_INFO_t *s_ppp_ccb) { U8 buffer[MSG_BUF]; - U16 mulen; + U16 mulen = 0; switch (encode_type) { case ENCODE_PADI: - if (build_padi(buffer, &mulen, s_ppp_ccb) == FALSE) { + if (build_padi(buffer, &mulen, s_ppp_ccb) == ERROR) { PPP_bye(s_ppp_ccb); return ERROR; } s_ppp_ccb->pppoe_phase.timer_counter++; break; case ENCODE_PADR: - /* code */ + if (build_padr(buffer, &mulen, s_ppp_ccb) == ERROR) { + PPP_bye(s_ppp_ccb); + return ERROR; + } + s_ppp_ccb->pppoe_phase.timer_counter++; break; case ENCODE_PADT: /* code */ diff --git a/src/pppd/codec.h b/src/pppd/codec.h index 3c64bd2..0c9e07d 100644 --- a/src/pppd/codec.h +++ b/src/pppd/codec.h @@ -37,7 +37,7 @@ STATUS check_ipcp_nak_rej(U8 flag, pppoe_header_t *pppoe_header, ppp_payload_t * STATUS build_auth_response_chap(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen, ppp_chap_data_t *ppp_chap_data); STATUS build_padi(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); -STATUS build_padr(__attribute__((unused)) struct rte_timer *tim, PPP_INFO_t *s_ppp_ccb); +STATUS build_padr(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb); STATUS build_padt(PPP_INFO_t *s_ppp_ccb); STATUS send_pkt(U8 encode_type, PPP_INFO_t *s_ppp_ccb); diff --git a/src/pppd/fsm.c b/src/pppd/fsm.c index f40cb79..0c93ce7 100644 --- a/src/pppd/fsm.c +++ b/src/pppd/fsm.c @@ -923,6 +923,11 @@ STATUS A_padi_timer_func(__attribute__((unused)) struct rte_timer *tim, __attrib return send_pkt(ENCODE_PADI, s_ppp_ccb); } +STATUS A_padr_timer_func(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) PPP_INFO_t *s_ppp_ccb) +{ + return send_pkt(ENCODE_PADR, s_ppp_ccb); +} + void fsm_init(VRG_t *ccb) { vrg_ccb = ccb; diff --git a/src/pppd/fsm.h b/src/pppd/fsm.h index 2fb74e8..d90ec4d 100644 --- a/src/pppd/fsm.h +++ b/src/pppd/fsm.h @@ -63,6 +63,7 @@ typedef enum { } PPP_MAIL_TYPE; STATUS A_padi_timer_func(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) PPP_INFO_t *s_ppp_ccb); +STATUS A_padr_timer_func(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) PPP_INFO_t *s_ppp_ccb); void fsm_init(VRG_t *ccb); /*======================= external ==========================*/ diff --git a/src/pppd/pppd.c b/src/pppd/pppd.c index 6ee5be9..be33198 100644 --- a/src/pppd/pppd.c +++ b/src/pppd/pppd.c @@ -237,11 +237,11 @@ STATUS ppp_process(void *mail) ppp_ccb[session_index].pppoe_phase.timer_counter = 0; rte_timer_stop(&(ppp_ccb[session_index].pppoe)); rte_ether_addr_copy(ð_hdr.src_addr, &ppp_ccb[session_index].PPP_dst_mac); - if (build_padr(&(ppp_ccb[session_index].pppoe),&(ppp_ccb[session_index])) == FALSE) { + if (send_pkt(ENCODE_PADR, &(ppp_ccb[session_index])) == ERROR) { exit_ppp(&(ppp_ccb[session_index].pppoe), &(ppp_ccb[session_index])); return FALSE; } - rte_timer_reset(&(ppp_ccb[session_index].pppoe),rte_get_timer_hz(),PERIODICAL,lcore.timer_thread,(rte_timer_cb_t)build_padr,&ppp_ccb[session_index]); + rte_timer_reset(&(ppp_ccb[session_index].pppoe), rte_get_timer_hz(), PERIODICAL, lcore.timer_thread, (rte_timer_cb_t)A_padr_timer_func, &ppp_ccb[session_index]); return FALSE; case PADS: rte_timer_stop(&(ppp_ccb[session_index].pppoe)); diff --git a/unit_test/pppd/codec_test.c b/unit_test/pppd/codec_test.c index 52cbf0c..c9ead15 100644 --- a/unit_test/pppd/codec_test.c +++ b/unit_test/pppd/codec_test.c @@ -2,32 +2,12 @@ #include #include #include "../../src/pppd/codec.h" -#include "../../src/dbg.h" - -void init_ccb() -{ - VRG_t *ccb = malloc(sizeof(VRG_t)); - - ccb->fp = NULL, - ccb->nic_info = (struct nic_info){ - .hsi_wan_src_mac = { - .addr_bytes = {0x9c, 0x69, 0xb4, 0x61, 0x16, 0xdd}, - }, - .hsi_lan_mac = { - .addr_bytes = {0x9c, 0x69, 0xb4, 0x61, 0x16, 0xdc}, - }, - }; - ccb->loglvl = -1; - codec_init(ccb); - dbg_init(ccb); -} +#include "../../src/protocol.h" void test_build_padi() { U8 buffer[80]; U16 mulen; - init_ccb(); - PPP_INFO_t s_ppp_ccb_1 = { .pppoe_phase = { .timer_counter = 0, @@ -36,15 +16,64 @@ void test_build_padi() { .user_num = 1, .vlan = 2, }; - char pkt_1[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9c, 0x69, 0xb4, 0x61, + U8 pkt_1[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9c, 0x69, 0xb4, 0x61, 0x16, 0xdd, 0x81, 0x00, 0x00, 0x02, 0x88, 0x63, 0x11, 0x09, 0x00, 0x00, 0x00, 0x04, 0x01, 0x01, 0x00, 0x00}; - assert(build_padi(buffer, &mulen, &s_ppp_ccb_1) == TRUE); + assert(build_padi(buffer, &mulen, &s_ppp_ccb_1) == SUCCESS); + assert(mulen == sizeof(pkt_1)); + assert(memcmp(buffer, pkt_1, mulen) == 0); + + memset(buffer, 0, sizeof(buffer)); + s_ppp_ccb_1.pppoe_phase.timer_counter = 10; + assert(build_padi(buffer, &mulen, &s_ppp_ccb_1) == ERROR); +} + +void test_build_padr() { + U8 buffer[80]; + U16 mulen = 0; + struct rte_ether_hdr eth_hdr = { + .ether_type = htons(VLAN), + }; + vlan_header_t vlan_header = { + .tci_union.tci_value = htons(0002), + .next_proto = htons(ETH_P_PPP_DIS), + }; + pppoe_header_t pppoe_header = { + .code = 0x07, + .ver_type = 0x11, + .session_id = htons(0x000a), + .length = htons(0x002c), + }; + + PPP_INFO_t s_ppp_ccb_1 = { + .pppoe_phase = { + .timer_counter = 0, + .max_retransmit = 10, + .eth_hdr = ð_hdr, + .vlan_header = &vlan_header, + .pppoe_header = &pppoe_header, + }, + .user_num = 1, + .vlan = 2, + .PPP_dst_mac = (struct rte_ether_addr){ + .addr_bytes = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31}, + }, + }; + U8 pppoe_header_tag[44] = {0x01, 0x03, 0x00, 0x04, 0xdb, 0xce, 0x00, 0x00, + 0x01, 0x01, 0x00, 0x00, 0x01, 0x02, 0x00, 0x08, 0x4d, 0x69, 0x6b, 0x72, + 0x6f, 0x54, 0x69, 0x6b, 0x01, 0x01, 0x00, 0x10, 0x76, 0x72, 0x67, 0x5f, + 0x73, 0x65, 0x72, 0x76, 0x65, 0x72}; + s_ppp_ccb_1.pppoe_phase.pppoe_header_tag = (pppoe_header_tag_t *)pppoe_header_tag; + char pkt_1[] = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4, 0x61, + 0x16, 0xdd, 0x81, 0x00, 0x00, 0x02, 0x88, 0x63, 0x11, 0x19, 0x00, 0x0a, 0x00, + 0x0c, 0x01, 0x01, 0x00, 0x00, 0x01, 0x03, 0x00, 0x04, 0xdb, 0xce, 0x00, 0x00}; + + assert(build_padr(buffer, &mulen, &s_ppp_ccb_1) == SUCCESS); assert(mulen == sizeof(pkt_1)); assert(memcmp(buffer, pkt_1, mulen) == 0); memset(buffer, 0, sizeof(buffer)); s_ppp_ccb_1.pppoe_phase.timer_counter = 10; - assert(build_padi(buffer, &mulen, &s_ppp_ccb_1) == FALSE); + assert(build_padr(buffer, &mulen, &s_ppp_ccb_1) == ERROR); } \ No newline at end of file diff --git a/unit_test/test.c b/unit_test/test.c index 3225e19..1cda12a 100644 --- a/unit_test/test.c +++ b/unit_test/test.c @@ -1,13 +1,36 @@ #include +#include "../src/vrg.h" +#include "../src/pppd/codec.h" +#include "../src/dbg.h" #include "test.h" +void init_ccb() +{ + VRG_t *ccb = malloc(sizeof(VRG_t)); + + ccb->fp = NULL, + ccb->nic_info = (struct nic_info){ + .hsi_wan_src_mac = { + .addr_bytes = {0x9c, 0x69, 0xb4, 0x61, 0x16, 0xdd}, + }, + .hsi_lan_mac = { + .addr_bytes = {0x9c, 0x69, 0xb4, 0x61, 0x16, 0xdc}, + }, + }; + ccb->loglvl = -1; + codec_init(ccb); + dbg_init(ccb); +} + int main() { signal(SIGCHLD, SIG_IGN); puts("====================start unit tests====================\n"); - puts("====================test codec.c===================="); + init_ccb(); + puts("====================test pppd/codec.c===================="); test_build_padi(); + test_build_padr(); puts("ok!"); puts("\nall test successfully"); diff --git a/unit_test/test.h b/unit_test/test.h index 5ed15a0..38ca75e 100644 --- a/unit_test/test.h +++ b/unit_test/test.h @@ -2,5 +2,6 @@ #define _TEST_H_ void test_build_padi(); +void test_build_padr(); #endif \ No newline at end of file