Skip to content

Commit

Permalink
add ppp config ack unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Oct 31, 2023
1 parent 926bb97 commit ffc4688
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 28 deletions.
33 changes: 15 additions & 18 deletions src/pppd/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,32 +759,29 @@ void build_config_request(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
* output: TRUE/FALSE
* return: packet buffer
*/
STATUS build_config_ack(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen)
void build_config_ack(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;
ppp_options_t *ppp_options = s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_options;

ppp_hdr->code = CONFIG_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);
ppp_options_t *ppp_options = (ppp_options_t *)(ppp_hdr + 1);

*eth_hdr = *(s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].eth_hdr);
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);

*mulen = ntohs(pppoe_header->length) + sizeof(struct rte_ether_hdr) + sizeof(pppoe_header_t) + sizeof(vlan_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);
ppp_hdr->code = CONFIG_ACK;
rte_memcpy(ppp_options, s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_options, rte_cpu_to_be_16(ppp_hdr->length) - sizeof(ppp_header_t));

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));
rte_memcpy(buffer+14+sizeof(vlan_header_t)+sizeof(pppoe_header_t)+sizeof(ppp_payload_t)+sizeof(ppp_header_t),ppp_options,rte_cpu_to_be_16(ppp_hdr->length) - sizeof(ppp_header_t));
*mulen = rte_be_to_cpu_16(pppoe_header->length) + sizeof(struct rte_ether_hdr) + sizeof(pppoe_header_t) + sizeof(vlan_header_t);

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

/**
Expand Down
4 changes: 2 additions & 2 deletions src/pppd/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ extern STATUS decode_ipcp(pppoe_header_t *pppoe_header, ppp_payload_t *ppp_paylo

extern void DECODE_OBJID(U8 *vp, U8 vlen, U32 *oids, U8 *oids_len);

void build_config_request(unsigned char *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb);
extern STATUS build_config_ack(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
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);
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);
Expand Down
8 changes: 1 addition & 7 deletions src/pppd/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,6 @@ STATUS A_this_layer_up(__attribute__((unused)) struct rte_timer *tim, __attribut
s_ppp_ccb->phase = DATA_PHASE;
rte_timer_reset(&(s_ppp_ccb->nat),rte_get_timer_hz(),PERIODICAL,lcore.timer_thread,(rte_timer_cb_t)nat_rule_timer,s_ppp_ccb);
VRG_LOG(INFO, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " IPCP connection establish successfully.", s_ppp_ccb->user_num);
if (unlikely(vrg_ccb->non_vlan_mode == TRUE))
VRG_LOG(INFO, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "Now user %" PRIu16 " can start to send data via pppoe session id 0x%x.", s_ppp_ccb->user_num, rte_cpu_to_be_16(s_ppp_ccb->session_id));
else
VRG_LOG(INFO, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "Now user %" PRIu16 " can start to send data via pppoe session id 0x%x and vlan is %" PRIu16 ".", s_ppp_ccb->user_num, rte_cpu_to_be_16(s_ppp_ccb->session_id), s_ppp_ccb->vlan);
VRG_LOG(INFO, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " PPPoE client IP address is %" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 ", PPPoE server IP address is %" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 , s_ppp_ccb->user_num, *(((U8 *)&(s_ppp_ccb->hsi_ipv4))), *(((U8 *)&(s_ppp_ccb->hsi_ipv4))+1), *(((U8 *)&(s_ppp_ccb->hsi_ipv4))+2), *(((U8 *)&(s_ppp_ccb->hsi_ipv4))+3), *(((U8 *)&(s_ppp_ccb->hsi_ipv4_gw))), *(((U8 *)&(s_ppp_ccb->hsi_ipv4_gw))+1), *(((U8 *)&(s_ppp_ccb->hsi_ipv4_gw))+2), *(((U8 *)&(s_ppp_ccb->hsi_ipv4_gw))+3));
if (unlikely(vrg_ccb->non_vlan_mode == TRUE))
VRG_LOG(INFO, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "Now user %" PRIu16 " can start to send data via pppoe session id 0x%x.", s_ppp_ccb->user_num, rte_cpu_to_be_16(s_ppp_ccb->session_id));
else
Expand Down Expand Up @@ -813,8 +808,7 @@ STATUS A_send_config_ack(__attribute__((unused)) struct rte_timer *tim, __attrib
unsigned char buffer[MSG_BUF];
U16 mulen;

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

return TRUE;
Expand Down
2 changes: 1 addition & 1 deletion src/pppd/pppd.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ STATUS ppp_process(void *mail)
pppoe_header_t pppoe_header;
ppp_payload_t ppp_payload;
ppp_header_t ppp_hdr;
ppp_options_t *ppp_options = (ppp_options_t *)rte_malloc(NULL,40*sizeof(char),0);
ppp_options_t *ppp_options = (ppp_options_t *)rte_malloc(NULL, 80*sizeof(U8), 0);

#pragma GCC diagnostic push // require GCC 4.6
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
Expand Down
93 changes: 93 additions & 0 deletions unit_test/pppd/codec_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,96 @@ void test_build_config_request() {
assert(test_ppp_options->type == IP_ADDRESS);
assert(test_ppp_options->length == 0x06);
}


void test_build_config_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 = CONFIG_REQUEST,
.identifier = 0x01,
.length = htons(0x0012),
},
.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),
},
.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, 0x14, /* ppp protocol */0xc0, 0x21, /* ppp hdr*/
0x02, 0x01, 0x00, 0x12, /* ppp option */0x03, 0x04, 0xc0, 0x23, 0x01, 0x04, 0x05,
0xd0, 0x05, 0x06, 0x01, 0x02, 0x03, 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, 0x0c, /* ppp protocol */0x80, 0x21, /* ppp hdr*/
0x02, 0x01, 0x00, 0x0a, /* ppp option */0x03, 0x06, 0xc0, 0xa8, 0xc8, 0x01};

/* test LCP */
build_config_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_config_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 @@ -33,6 +33,7 @@ int main()
test_build_padr();
test_build_padt();
test_build_config_request();
test_build_config_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 @@ -5,5 +5,6 @@ void test_build_padi();
void test_build_padr();
void test_build_padt();
void test_build_config_request();
void test_build_config_ack();

#endif

0 comments on commit ffc4688

Please sign in to comment.