Skip to content

Commit

Permalink
add terminate ack unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Nov 10, 2023
1 parent 46ebbc6 commit 9f12c19
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 26 deletions.
49 changes: 27 additions & 22 deletions src/pppd/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,37 +859,42 @@ STATUS build_echo_reply(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen
/**
* build_terminate_ack
*
* purpose: For build PPP terminate ack, either in NCP or LCP phase.
* input: *buffer - packet buffer,
* *s_ppp_ccb,
* *mulen - packet length
* output: TRUE/FALSE
* return: packet buffer
* @brief
* For build PPP terminate ack, either in NCP or LCP phase.
* @param buffer
* The buffer to be processed by the codec.
* @param s_ppp_ccb
* The ppp ccb.
* @param len
* The length of the buffer.
* @return
* void
*/
STATUS build_terminate_ack(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen)
void build_terminate_ack(unsigned char* 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;

ppp_hdr->code = TERMIN_ACK;
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);
eth_hdr->ether_type = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].eth_hdr->ether_type;

*mulen = ntohs(pppoe_header->length) + sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t);
*vlan_header = *(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].vlan_header);
*pppoe_header = *(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].pppoe_header);
*ppp_payload = *(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_payload);
*ppp_hdr = *(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_hdr);

memset(buffer,0,MSG_BUF);
rte_memcpy(buffer,eth_hdr,sizeof(struct rte_ether_hdr));
rte_memcpy(buffer+14,vlan_header,sizeof(vlan_header_t));
rte_memcpy(buffer+14+sizeof(vlan_header_t),pppoe_header,sizeof(pppoe_header_t));
rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t),ppp_payload,sizeof(ppp_payload_t));
rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t),ppp_hdr,sizeof(ppp_header_t));
ppp_hdr->code = TERMIN_ACK;
ppp_hdr->length = rte_cpu_to_be_16(sizeof(ppp_header_t));

pppoe_header->length = rte_cpu_to_be_16(sizeof(ppp_header_t) + sizeof(ppp_payload_t));

*mulen = rte_be_to_cpu_16(pppoe_header->length) + sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t);

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

/**
Expand Down
2 changes: 1 addition & 1 deletion src/pppd/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extern void DECODE_OBJID(U8 *vp, U8 vlen, U32 *oids, U8 *oids_len);
void build_config_request(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb);
void build_config_ack(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb);
void build_config_nak_rej(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb);
extern STATUS build_terminate_ack(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
void build_terminate_ack(unsigned char *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb);
extern STATUS build_code_reject(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);
Expand Down
5 changes: 2 additions & 3 deletions src/pppd/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,13 +830,12 @@ STATUS A_send_terminate_request(struct rte_timer *tim, PPP_INFO_t *s_ppp_ccb)
return TRUE;
}

STATUS A_send_terminate_ack(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) PPP_INFO_t *s_ppp_ccb)
STATUS A_send_terminate_ack(__attribute__((unused)) struct rte_timer *tim, PPP_INFO_t *s_ppp_ccb)
{
unsigned char buffer[MSG_BUF];
U16 mulen;

if (build_terminate_ack(buffer,s_ppp_ccb,&mulen) < 0)
return FALSE;
build_terminate_ack(buffer, s_ppp_ccb, &mulen);
drv_xmit(vrg_ccb, buffer, mulen);

return TRUE;
Expand Down
95 changes: 95 additions & 0 deletions unit_test/pppd/codec_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,98 @@ void test_build_config_nak_rej()
assert(mulen == sizeof(pkt_ipcp_2));
assert(memcmp(buffer, pkt_ipcp_2, sizeof(pkt_ipcp_2)) == 0);
}

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

PPP_INFO_t s_ppp_ccb_1 = {
.ppp_phase = {{
.timer_counter = 0,
.max_retransmit = 10,
.eth_hdr = &(struct rte_ether_hdr) {
.ether_type = htons(VLAN),
},
.vlan_header = &(vlan_header_t) {
.tci_union.tci_value = htons(0002),
.next_proto = htons(ETH_P_PPP_SES),
},
.pppoe_header = &(pppoe_header_t) {
.code = 0,
.ver_type = 0x11,
.session_id = htons(0x000a),
.length = htons(0x0014),
},
.ppp_payload = &(ppp_payload_t) {
.ppp_protocol = htons(LCP_PROTOCOL),
},
.ppp_hdr = &(ppp_header_t) {
.code = TERMIN_REQUEST,
.identifier = 0x01,
.length = htons(0x0012),
},
/* this field is not used, we juts leave this here to make sure it won't
be inserted into terminate ack packet */
.ppp_options = (ppp_options_t *)(U8 []){
0x03, 0x04, 0xc0, 0x23, 0x01, 0x04, 0x05, 0xd0, 0x05, 0x06, 0x01, 0x02, 0x03, 0x04
}, // MRU, AUTH, MAGIC NUMBER
},{
.timer_counter = 0,
.max_retransmit = 10,
.eth_hdr = &(struct rte_ether_hdr) {
.ether_type = htons(VLAN),
},
.vlan_header = &(vlan_header_t) {
.tci_union.tci_value = htons(0002),
.next_proto = htons(ETH_P_PPP_SES),
},
.pppoe_header = &(pppoe_header_t) {
.code = 0,
.ver_type = 0x11,
.session_id = htons(0x000a),
.length = htons(0x000c),
},
.ppp_payload = &(ppp_payload_t) {
.ppp_protocol = htons(IPCP_PROTOCOL),
},
.ppp_hdr = &(ppp_header_t) {
.code = CONFIG_REQUEST,
.identifier = 0x01,
.length = htons(0x000a),
},
/* this field is not used, we juts leave this here to make sure it won't
be inserted into terminate ack packet */
.ppp_options = (ppp_options_t *)(U8 []){
0x03, 0x06, 0xc0, 0xa8, 0xc8, 0x01
}, // IP_ADDRESS
},},
.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*/
0x06, 0x01, 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*/
0x06, 0x01, 0x00, 0x04};

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

memset(buffer, 0, sizeof(buffer));
/* test IPCP */
s_ppp_ccb_1.cp = 1;
build_terminate_ack(buffer, &mulen, &s_ppp_ccb_1);
assert(mulen == sizeof(pkt_ipcp));
assert(memcmp(buffer, pkt_ipcp, sizeof(pkt_ipcp)) == 0);
}
1 change: 1 addition & 0 deletions unit_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ int main()
test_build_config_ack();
test_build_config_nak_rej();
test_build_terminate_request();
test_build_terminate_ack();
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 @@ -8,5 +8,6 @@ void test_build_config_request();
void test_build_config_ack();
void test_build_terminate_request();
void test_build_config_nak_rej();
void test_build_terminate_ack();

#endif

0 comments on commit 9f12c19

Please sign in to comment.