Skip to content

Commit

Permalink
add padr unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Oct 12, 2023
1 parent 1ee2c70 commit 29b0d99
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 60 deletions.
73 changes: 41 additions & 32 deletions src/pppd/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; i<RTE_ETHER_ADDR_LEN; i++) {
Expand All @@ -513,7 +513,7 @@ STATUS build_padi(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)

*mulen = sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t) + sizeof(pppoe_header_tag_t);

return TRUE;
return SUCCESS;
}

/**
Expand All @@ -524,69 +524,74 @@ STATUS build_padi(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
* *time - PPPoE timer
* output: TRUE/FALSE
*/
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)
{
static unsigned char buffer[MSG_BUF];
static U16 mulen;
pppoe_header_tag_t *tmp_pppoe_header_tag = (pppoe_header_tag_t *)((pppoe_header_t *)((vlan_header_t *)((struct rte_ether_hdr *)buffer + 1) + 1) + 1);
//static unsigned char buffer[MSG_BUF];
//static U16 mulen;
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);
pppoe_header_tag_t *pppoe_header_tag = (pppoe_header_tag_t *)(pppoe_header + 1);

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 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.");
return FALSE;
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;
}

/**
Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/pppd/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions src/pppd/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/pppd/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==========================*/
Expand Down
4 changes: 2 additions & 2 deletions src/pppd/pppd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(&eth_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));
Expand Down
77 changes: 53 additions & 24 deletions unit_test/pppd/codec_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,12 @@
#include <stdlib.h>
#include <common.h>
#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,
Expand All @@ -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 = &eth_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);
}
25 changes: 24 additions & 1 deletion unit_test/test.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
#include <common.h>
#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");
Expand Down
1 change: 1 addition & 0 deletions unit_test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#define _TEST_H_

void test_build_padi();
void test_build_padr();

#endif

0 comments on commit 29b0d99

Please sign in to comment.