Skip to content

Commit

Permalink
add padt unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Oct 14, 2023
1 parent 29b0d99 commit bd53184
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 55 deletions.
86 changes: 41 additions & 45 deletions src/pppd/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,11 @@ STATUS check_nak_reject(U8 flag, pppoe_header_t *pppoe_header, __attribute__((un
/**
* build_padi
*
* purpose: For build PPPoE init and send.
* input: *s_ppp_ccb
* *time - PPPoE timer
* output: TRUE/FALSE
* purpose: For build PPPoE init.
* input: *buffer - pkt buffer
* *mulen - pkt length
* *s_ppp_ccb - ppp ccb
* output: SUCCESS/ERROR
*/
STATUS build_padi(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
{
Expand Down Expand Up @@ -519,10 +520,11 @@ STATUS build_padi(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
/**
* build_padr
*
* purpose: For build PPPoE request and send.
* input: *s_ppp_ccb
* *time - PPPoE timer
* output: TRUE/FALSE
* purpose: For build PPPoE request.
* input: *buffer - pkt buffer
* *mulen - pkt length
* *s_ppp_ccb - ppp ccb
* output: SUCCESS/ERROR
*/
STATUS build_padr(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
{
Expand Down Expand Up @@ -597,47 +599,36 @@ STATUS build_padr(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
/**
* build_padt
*
* purpose: For build PPPoE terminate and send.
* input: *s_ppp_ccb
* output: TRUE/FALSE
* purpose: For build PPPoE termination.
* input: *buffer - pkt buffer
* *mulen - pkt length
* *s_ppp_ccb - ppp ccb
* output:
*/
STATUS build_padt(PPP_INFO_t *s_ppp_ccb)
void build_padt(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb)
{
unsigned char buffer[MSG_BUF];
U16 mulen;
struct rte_ether_hdr eth_hdr;
vlan_header_t vlan_header;
pppoe_header_t pppoe_header;

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 = rte_cpu_to_be_16(VLAN);

vlan_header.tci_union.tci_struct.priority = 0;
vlan_header.tci_union.tci_struct.DEI = 0;
vlan_header.tci_union.tci_struct.vlan_id = s_ppp_ccb->vlan;
vlan_header.next_proto = rte_cpu_to_be_16(ETH_P_PPP_DIS);
vlan_header.tci_union.tci_value = rte_cpu_to_be_16(vlan_header.tci_union.tci_value);

pppoe_header.ver_type = VER_TYPE;
pppoe_header.code = PADT;
pppoe_header.session_id = s_ppp_ccb->session_id;
pppoe_header.length = 0;
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);

mulen = sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t);
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 = rte_cpu_to_be_16(VLAN);

rte_memcpy(buffer,&eth_hdr,sizeof(struct rte_ether_hdr));
rte_memcpy(buffer+sizeof(struct rte_ether_hdr),&vlan_header,sizeof(vlan_header_t));
rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t),&pppoe_header,sizeof(pppoe_header_t));
drv_xmit(vrg_ccb, buffer, mulen);
vlan_header->tci_union.tci_struct.priority = 0;
vlan_header->tci_union.tci_struct.DEI = 0;
vlan_header->tci_union.tci_struct.vlan_id = s_ppp_ccb->vlan;
vlan_header->next_proto = rte_cpu_to_be_16(ETH_P_PPP_DIS);
vlan_header->tci_union.tci_value = rte_cpu_to_be_16(vlan_header->tci_union.tci_value);

s_ppp_ccb->phase = PPPOE_PHASE;
s_ppp_ccb->pppoe_phase.active = FALSE;
VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %u PPPoE session closed successfully.", s_ppp_ccb->user_num);
pppoe_header->ver_type = VER_TYPE;
pppoe_header->code = PADT;
pppoe_header->session_id = s_ppp_ccb->session_id;
pppoe_header->length = 0;

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

return TRUE;
return SUCCESS;
}

/**
Expand Down Expand Up @@ -1140,23 +1131,28 @@ STATUS send_pkt(U8 encode_type, PPP_INFO_t *s_ppp_ccb)
return ERROR;
}
s_ppp_ccb->pppoe_phase.timer_counter++;
drv_xmit(vrg_ccb, buffer, mulen);
break;
case ENCODE_PADR:
if (build_padr(buffer, &mulen, s_ppp_ccb) == ERROR) {
PPP_bye(s_ppp_ccb);
return ERROR;
}
s_ppp_ccb->pppoe_phase.timer_counter++;
drv_xmit(vrg_ccb, buffer, mulen);
break;
case ENCODE_PADT:
/* code */
build_padt(buffer, &mulen, s_ppp_ccb);
drv_xmit(vrg_ccb, buffer, mulen);
s_ppp_ccb->phase = PPPOE_PHASE;
s_ppp_ccb->pppoe_phase.active = FALSE;
VRG_LOG(DBG, vrg_ccb->fp, s_ppp_ccb, PPPLOGMSG, "User %u PPPoE session closed successfully.", s_ppp_ccb->user_num);
PPP_bye(s_ppp_ccb);
break;
default:
return ERROR;
}

drv_xmit(vrg_ccb, buffer, mulen);

return SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion src/pppd/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ STATUS build_auth_response_chap(unsigned char* buffer, PPP_INFO_t *s_ppp_ccb, U1

STATUS build_padi(U8 *buffer, U16 *mulen, 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);
void build_padt(U8 *buffer, U16 *mulen, PPP_INFO_t *s_ppp_ccb);
STATUS send_pkt(U8 encode_type, PPP_INFO_t *s_ppp_ccb);

typedef enum {
Expand Down
3 changes: 1 addition & 2 deletions src/pppd/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,7 @@ STATUS A_zero_restart_count(__attribute__((unused)) struct rte_timer *tim, __att

STATUS A_send_padt(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) PPP_INFO_t *s_ppp_ccb)
{
if (build_padt(s_ppp_ccb) < 0)
return FALSE;
send_pkt(ENCODE_PADT, s_ppp_ccb);
s_ppp_ccb->phase = END_PHASE;

return TRUE;
Expand Down
10 changes: 5 additions & 5 deletions src/pppd/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,21 @@
typedef struct pppoe_header {
U8 ver_type;
U8 code;
U16 session_id;
U16 length;
U16 session_id; // network byte order
U16 length; // host byte order
} pppoe_header_t;

typedef struct pppoe_header_tag {
U16 type;
U16 length;
U16 type; // network byte order
U16 length; // host byte order
// depend on the type and length.
U8 value[0];
} pppoe_header_tag_t;

typedef struct ppp_header {
U8 code;
U8 identifier;
U16 length;
U16 length; // host byte order
}ppp_header_t;

typedef struct ppp_pap_ack_nak {
Expand Down
45 changes: 43 additions & 2 deletions unit_test/pppd/codec_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void test_build_padr() {
.next_proto = htons(ETH_P_PPP_DIS),
};
pppoe_header_t pppoe_header = {
.code = 0x07,
.code = PADO,
.ver_type = 0x11,
.session_id = htons(0x000a),
.length = htons(0x002c),
Expand Down Expand Up @@ -76,4 +76,45 @@ void test_build_padr() {
memset(buffer, 0, sizeof(buffer));
s_ppp_ccb_1.pppoe_phase.timer_counter = 10;
assert(build_padr(buffer, &mulen, &s_ppp_ccb_1) == ERROR);
}
}

void test_build_padt() {
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 = PADS,
.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},
},
.session_id = htons(0x000a),
};
char pkt_1[] = {0x74, 0x4d, 0x28, 0x8d, 0x00, 0x31, 0x9c, 0x69, 0xb4, 0x61,
0x16, 0xdd, 0x81, 0x00, 0x00, 0x02, 0x88, 0x63, 0x11, 0xa7, 0x00, 0x0a, 0x00,
0x00};

assert(build_padt(buffer, &mulen, &s_ppp_ccb_1) == SUCCESS);
assert(mulen == sizeof(pkt_1));
assert(memcmp(buffer, pkt_1, mulen) == 0);
}
1 change: 1 addition & 0 deletions unit_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ int main()
puts("====================test pppd/codec.c====================");
test_build_padi();
test_build_padr();
test_build_padt();
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 @@ -3,5 +3,6 @@

void test_build_padi();
void test_build_padr();
void test_build_padt();

#endif

0 comments on commit bd53184

Please sign in to comment.