Skip to content

Commit

Permalink
add config nak and reject unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Nov 1, 2023
1 parent f503234 commit 46ebbc6
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ install:

test: $(TARGET)
${MAKE} -C $(TESTDIR)
./$(TESTDIR)/$(TESTBIN)
ulimit -s 16384 && ./$(TESTDIR)/$(TESTBIN)

######################################
# Clean
Expand Down
1 change: 0 additions & 1 deletion src/cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <rte_malloc.h>
#include <rte_branch_prediction.h>
#include <rte_launch.h>
#include <rte_log.h>
#include <rte_per_lcore.h>
#include <rte_lcore.h>
#include <rte_ring.h>
Expand Down
4 changes: 3 additions & 1 deletion src/dbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "dhcpd/dhcp_fsm.h"
#include "dbg.h"

#define DBG_VRG_MSG_LEN 2048
#define DBG_VRG_MSG_LEN 256
#define LOGGER_BUF_LEN 1024

char *PPP_state2str(U16 state);
Expand Down Expand Up @@ -148,6 +148,8 @@ void LOGGER(U8 level, char *filename, int line_num, FILE *log_fp, void *ccb, voi
char buf[LOGGER_BUF_LEN], protocol_buf[LOGGER_BUF_LEN-100], msg[DBG_VRG_MSG_LEN];

protocol_buf[0] = '\0';
msg[0] = 0;
buf[0] = 0;

//user offer level must > system requirement
if (vrg_ccb->loglvl > level)
Expand Down
32 changes: 15 additions & 17 deletions src/pppd/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <rte_ether.h>
#include <rte_malloc.h>
#include <rte_memcpy.h>
#include <rte_log.h>
#include <rte_byteorder.h>
#include <cmdline_socket.h>
#include "../protocol.h"
Expand Down Expand Up @@ -795,30 +794,29 @@ void build_config_ack(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
* output: TRUE/FALSE
* return: packet buffer
*/
STATUS build_config_nak_rej(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen)
void build_config_nak_rej(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;
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);

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(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);

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,ntohs(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);

rte_memcpy(ppp_options, s_ppp_ccb->ppp_phase[s_ppp_ccb->cp].ppp_options, rte_be_to_cpu_16(ppp_hdr->length) - sizeof(ppp_header_t));

VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %" PRIu16 " config nak/rej 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 @@ -24,7 +24,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);
extern STATUS build_config_nak_rej(unsigned char *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen);
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);
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);
Expand Down
3 changes: 1 addition & 2 deletions src/pppd/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,7 @@ STATUS A_send_config_nak_rej(__attribute__((unused)) struct rte_timer *tim, __at
unsigned char buffer[MSG_BUF];
U16 mulen;

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

return TRUE;
Expand Down
1 change: 0 additions & 1 deletion src/pppd/pppd.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <rte_timer.h>
#include <rte_malloc.h>
#include <rte_ether.h>
#include <rte_log.h>
#include <cmdline_rdline.h>
#include <cmdline_parse.h>
#include <cmdline_parse_string.h>
Expand Down
1 change: 0 additions & 1 deletion src/vrg.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <rte_timer.h>
#include <rte_malloc.h>
#include <rte_ether.h>
#include <rte_log.h>
#include <cmdline_rdline.h>
#include <cmdline_parse.h>
#include <cmdline_parse_string.h>
Expand Down
187 changes: 185 additions & 2 deletions unit_test/pppd/codec_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,195 @@ void test_build_terminate_request() {
/* 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);
assert(memcmp(buffer, pkt_lcp, 26/* only memcmp to ipcp field */) == 0);
ppp_header_t *test_ppp_hdr = (ppp_header_t *)(buffer + 26);
assert(test_ppp_hdr->code == TERMIN_REQUEST);
assert(test_ppp_hdr->length == htons(0x0004));

/* 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);
assert(memcmp(buffer, pkt_ipcp, 26/* only memcmp to ipcp field */) == 0);
test_ppp_hdr = (ppp_header_t *)(buffer + 26);
assert(test_ppp_hdr->code == TERMIN_REQUEST);
assert(test_ppp_hdr->length == htons(0x0004));
}

void test_build_config_nak_rej()
{
U8 buffer[80] = {0};
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(0x000a),
},
.ppp_payload = &(ppp_payload_t) {
.ppp_protocol = htons(LCP_PROTOCOL),
},
.ppp_hdr = &(ppp_header_t) {
.code = CONFIG_REJECT,
.identifier = 0x01,
.length = htons(0x0008),
},
.ppp_options = (ppp_options_t *)(U8 []){
0x03, 0x04, 0xc0, 0x23
}, // CHAP
},{
.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_REJECT,
.identifier = 0x01,
.length = htons(0x000a),
},
.ppp_options = (ppp_options_t *)(U8 []){
0x83, 0x06, 0x00, 0x00, 0x00, 0x00
}, // Secondary DNS
},},
.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,
};
PPP_INFO_t s_ppp_ccb_2 = {
.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(0x000a),
},
.ppp_payload = &(ppp_payload_t) {
.ppp_protocol = htons(LCP_PROTOCOL),
},
.ppp_hdr = &(ppp_header_t) {
.code = CONFIG_NAK,
.identifier = 0x01,
.length = htons(0x0008),
},
.ppp_options = (ppp_options_t *)(U8 []){
0x01, 0x04, 0x05, 0xd0
}, // MRU
},{
.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(0x0010),
},
.ppp_payload = &(ppp_payload_t) {
.ppp_protocol = htons(IPCP_PROTOCOL),
},
.ppp_hdr = &(ppp_header_t) {
.code = CONFIG_NAK,
.identifier = 0x01,
.length = htons(0x0010),
},
.ppp_options = (ppp_options_t *)(U8 []){
0xc0, 0xa8, 0xc8, 0xfe, 0x81, 0x06, 0xc0, 0xa8, 0x0a, 0x01
}, // IP and Primary DNS
},},
.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_1[] = {/* 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, 0x0a, /* ppp protocol */0xc0, 0x21, /* ppp hdr*/
0x04, 0x01, 0x00, 0x08, /* ppp option */0x03, 0x04, 0xc0, 0x23};
char pkt_ipcp_1[] = {/* 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*/
0x04, 0x01, 0x00, 0x0a, /* ppp option */0x83, 0x06, 0x00, 0x00, 0x00, 0x00};
char pkt_lcp_2[] = {/* 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, 0x0a, /* ppp protocol */0xc0, 0x21, /* ppp hdr*/
0x03, 0x01, 0x00, 0x08, /* ppp option */0x01, 0x04, 0x05, 0xd0};
char pkt_ipcp_2[] = {/* 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, 0x10, /* ppp protocol */0x80, 0x21, /* ppp hdr*/
0x03, 0x01, 0x00, 0x10, /* ppp option */0xc0, 0xa8, 0xc8, 0xfe, 0x81, 0x06, 0xc0,
0xa8, 0x0a, 0x01};

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

/* test IPCP */
s_ppp_ccb_1.cp = 1;
memset(buffer, 0, sizeof(buffer));
build_config_nak_rej(buffer, &mulen, &s_ppp_ccb_1);
assert(mulen == sizeof(pkt_ipcp_1));
assert(memcmp(buffer, pkt_ipcp_1, sizeof(pkt_ipcp_1)) == 0);

s_ppp_ccb_2.cp = 0;
memset(buffer, 0, sizeof(buffer));
build_config_nak_rej(buffer, &mulen, &s_ppp_ccb_2);
assert(mulen == sizeof(pkt_lcp_2));
assert(memcmp(buffer, pkt_lcp_2, sizeof(pkt_lcp_2)) == 0);

s_ppp_ccb_2.cp = 1;
memset(buffer, 0, sizeof(buffer));
build_config_nak_rej(buffer, &mulen, &s_ppp_ccb_2);
assert(mulen == sizeof(pkt_ipcp_2));
assert(memcmp(buffer, pkt_ipcp_2, sizeof(pkt_ipcp_2)) == 0);
}
3 changes: 3 additions & 0 deletions unit_test/test.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <common.h>
#include <sys/resource.h>
#include "../src/vrg.h"
#include "../src/pppd/codec.h"
#include "../src/dbg.h"
Expand Down Expand Up @@ -34,6 +35,8 @@ int main()
test_build_padt();
test_build_config_request();
test_build_config_ack();
test_build_config_nak_rej();
test_build_terminate_request();
puts("ok!");

puts("\nall test successfully");
Expand Down
2 changes: 2 additions & 0 deletions unit_test/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ void test_build_padr();
void test_build_padt();
void test_build_config_request();
void test_build_config_ack();
void test_build_terminate_request();
void test_build_config_nak_rej();

#endif

0 comments on commit 46ebbc6

Please sign in to comment.