Skip to content

Commit

Permalink
add padi unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
w180112 committed Oct 8, 2023
1 parent c23d9f5 commit 2e78d50
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
######################################
CC = gcc
INCLUDE =
CFLAGS = $(INCLUDE) -Wall -g $(shell pkg-config --cflags libdpdk) -O3 -DALLOW_EXPERIMENTAL_API
CFLAGS = $(INCLUDE) -Wall -g $(shell pkg-config --cflags libdpdk) -O3 -DALLOW_EXPERIMENTAL_API -D_TEST_MODE

LDFLAGS = $(shell pkg-config --static --libs libdpdk) -lutils -lconfig

Expand All @@ -34,7 +34,7 @@ $(TARGET): $(OBJ)
install:
cp $(TARGET) /usr/local/bin/$(TARGET)

test:
test: $(TARGET)
${MAKE} -C $(TESTDIR)
./$(TESTDIR)/$(TESTBIN)

Expand Down
13 changes: 13 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include <common.h>
#include "vrg.h"

int main(int argc, char **argv)
{
if (argc < 5) {
puts("Too less parameter.");
puts("Type vrg <eal_options>");
return ERROR;
}
return vrg_start(argc, argv);
}
82 changes: 51 additions & 31 deletions src/pppd/codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,48 +463,40 @@ STATUS check_nak_reject(U8 flag, pppoe_header_t *pppoe_header, __attribute__((un
* *time - PPPoE timer
* output: TRUE/FALSE
*/
STATUS build_padi(__attribute__((unused)) struct rte_timer *tim, PPP_INFO_t *s_ppp_ccb)
STATUS build_padi(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;
pppoe_header_tag_t pppoe_header_tag;
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 PADI", s_ppp_ccb->user_num);
PPP_bye(s_ppp_ccb);
}
for(int i=0; i<6; i++) {
eth_hdr.src_addr.addr_bytes[i] = vrg_ccb->nic_info.hsi_wan_src_mac.addr_bytes[i];
eth_hdr.dst_addr.addr_bytes[i] = 0xff;
return FALSE;
}
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);
for(int i=0; i<RTE_ETHER_ADDR_LEN; i++) {
eth_hdr->src_addr.addr_bytes[i] = vrg_ccb->nic_info.hsi_wan_src_mac.addr_bytes[i];
eth_hdr->dst_addr.addr_bytes[i] = 0xff;
}
eth_hdr->ether_type = rte_cpu_to_be_16(VLAN);

pppoe_header.ver_type = VER_TYPE;
pppoe_header.code = PADI;
pppoe_header.session_id = 0;
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_tag.type = rte_cpu_to_be_16(SERVICE_NAME); //padi tag type (service name)
pppoe_header_tag.length = 0;
pppoe_header->ver_type = VER_TYPE;
pppoe_header->code = PADI;
pppoe_header->session_id = 0;

pppoe_header.length = rte_cpu_to_be_16(sizeof(pppoe_header_tag_t));
pppoe_header_tag->type = rte_cpu_to_be_16(SERVICE_NAME); //padi tag type (service name)
pppoe_header_tag->length = 0;

mulen = sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t) + sizeof(pppoe_header_tag_t);
pppoe_header->length = rte_cpu_to_be_16(sizeof(pppoe_header_tag_t));

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));
rte_memcpy(buffer+sizeof(struct rte_ether_hdr)+sizeof(vlan_header_t)+sizeof(pppoe_header_t),&pppoe_header_tag,sizeof(pppoe_header_tag_t));
drv_xmit(vrg_ccb, buffer, mulen);
s_ppp_ccb->pppoe_phase.timer_counter++;
*mulen = sizeof(struct rte_ether_hdr) + sizeof(vlan_header_t) + sizeof(pppoe_header_t) + sizeof(pppoe_header_tag_t);

return TRUE;
}
Expand Down Expand Up @@ -1116,6 +1108,34 @@ STATUS build_auth_response_chap(U8 *buffer, PPP_INFO_t *s_ppp_ccb, U16 *mulen, p
return TRUE;
}

STATUS send_pkt(U8 encode_type, PPP_INFO_t *s_ppp_ccb)
{
U8 buffer[MSG_BUF];
U16 mulen;

switch (encode_type) {
case ENCODE_PADI:
if (build_padi(buffer, &mulen, s_ppp_ccb) == FALSE) {
PPP_bye(s_ppp_ccb);
return ERROR;
}
s_ppp_ccb->pppoe_phase.timer_counter++;
break;
case ENCODE_PADR:
/* code */
break;
case ENCODE_PADT:
/* code */
break;
default:
return ERROR;
}

drv_xmit(vrg_ccb, buffer, mulen);

return SUCCESS;
}

void codec_init(VRG_t *ccb)
{
vrg_ccb = ccb;
Expand Down
9 changes: 8 additions & 1 deletion src/pppd/codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ STATUS check_nak_reject(U8 flag, pppoe_header_t *pppoe_header, ppp_payload_t *pp
STATUS check_ipcp_nak_rej(U8 flag, pppoe_header_t *pppoe_header, ppp_payload_t *ppp_payload, ppp_header_t *ppp_hdr, ppp_options_t *ppp_options, U16 total_lcp_length);
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(__attribute__((unused)) struct rte_timer *tim, PPP_INFO_t *s_ppp_ccb);
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_padt(PPP_INFO_t *s_ppp_ccb);
STATUS send_pkt(U8 encode_type, PPP_INFO_t *s_ppp_ccb);

typedef enum {
ENCODE_PADI,
ENCODE_PADR,
ENCODE_PADT,
}PPP_CODE_TYPE_t;

#endif
5 changes: 5 additions & 0 deletions src/pppd/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,11 @@ STATUS A_create_close_to_lower_layer(__attribute__((unused)) struct rte_timer *t
return TRUE;
}

STATUS A_padi_timer_func(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) PPP_INFO_t *s_ppp_ccb)
{
return send_pkt(ENCODE_PADI, s_ppp_ccb);
}

void fsm_init(VRG_t *ccb)
{
vrg_ccb = ccb;
Expand Down
2 changes: 2 additions & 0 deletions src/pppd/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ typedef enum {
MT_ppp_peer,
} PPP_MAIL_TYPE;

STATUS A_padi_timer_func(__attribute__((unused)) struct rte_timer *tim, __attribute__((unused)) PPP_INFO_t *s_ppp_ccb);

/*======================= external ==========================*/
#ifdef __cplusplus
extern "C" {
Expand Down
15 changes: 5 additions & 10 deletions src/vrg.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ rte_atomic16_t cp_recv_cums;
struct lcore_map lcore;
VRG_t vrg_ccb;

int main(int argc, char **argv)
int vrg_start(int argc, char **argv)
{
if (argc < 5) {
puts("Too less parameter.");
puts("Type vrg <eal_options>");
return ERROR;
}
int ret = rte_eal_init(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE, "rte initlize fail.\n");
Expand Down Expand Up @@ -230,11 +225,11 @@ int vrg_loop(VRG_t *vrg_ccb)
vrg_ccb->ppp_ccb[j].phase = PPPOE_PHASE;
vrg_ccb->ppp_ccb[j].pppoe_phase.max_retransmit = MAX_RETRAN;
vrg_ccb->ppp_ccb[j].pppoe_phase.timer_counter = 0;
if (build_padi(&(vrg_ccb->ppp_ccb[j].pppoe),&(vrg_ccb->ppp_ccb[j])) == FALSE)
if (send_pkt(ENCODE_PADI, &(vrg_ccb->ppp_ccb[j])) == ERROR)
PPP_bye(&(vrg_ccb->ppp_ccb[j]));
/* set ppp starting boolean flag to TRUE */
rte_atomic16_set(&vrg_ccb->ppp_ccb[j].ppp_bool, 1);
rte_timer_reset(&(vrg_ccb->ppp_ccb[j].pppoe),rte_get_timer_hz(),PERIODICAL,lcore.timer_thread,(rte_timer_cb_t)build_padi,&(vrg_ccb->ppp_ccb[j]));
rte_timer_reset(&(vrg_ccb->ppp_ccb[j].pppoe), rte_get_timer_hz(), PERIODICAL, lcore.timer_thread, (rte_timer_cb_t)A_padi_timer_func, &(vrg_ccb->ppp_ccb[j]));
}
}
else {
Expand All @@ -246,11 +241,11 @@ int vrg_loop(VRG_t *vrg_ccb)
vrg_ccb->ppp_ccb[user_id-1].phase = PPPOE_PHASE;
vrg_ccb->ppp_ccb[user_id-1].pppoe_phase.max_retransmit = MAX_RETRAN;
vrg_ccb->ppp_ccb[user_id-1].pppoe_phase.timer_counter = 0;
if (build_padi(&(vrg_ccb->ppp_ccb[user_id-1].pppoe), &(vrg_ccb->ppp_ccb[user_id-1])) == FALSE)
if (send_pkt(ENCODE_PADI, &(vrg_ccb->ppp_ccb[user_id-1])) == ERROR)
PPP_bye(&(vrg_ccb->ppp_ccb[user_id-1]));
/* set ppp starting boolean flag to TRUE */
rte_atomic16_set(&vrg_ccb->ppp_ccb[user_id-1].ppp_bool, 1);
rte_timer_reset(&(vrg_ccb->ppp_ccb[user_id-1].pppoe),rte_get_timer_hz(),PERIODICAL,lcore.timer_thread,(rte_timer_cb_t)build_padi,&(vrg_ccb->ppp_ccb[user_id-1]));
rte_timer_reset(&(vrg_ccb->ppp_ccb[user_id-1].pppoe), rte_get_timer_hz(), PERIODICAL, lcore.timer_thread, (rte_timer_cb_t)A_padi_timer_func, &(vrg_ccb->ppp_ccb[user_id-1]));
}
break;
case CLI_DHCP_START:
Expand Down
2 changes: 2 additions & 0 deletions src/vrg.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ typedef struct {
int len;
} tVRG_MBX;

int vrg_start(int argc, char **argv);

#endif
2 changes: 1 addition & 1 deletion unit_test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LDFLAGS = $(shell pkg-config --static --libs libdpdk) -lutils -lconfig
TARGET = unit-tester
SRC = $(wildcard *.c) $(wildcard pppd/*.c) $(wildcard dhcpd/*.c)

OBJ = $(SRC:.c=.o)
OBJ = $(SRC:.c=.o) $(wildcard ../src/pppd/*.o) $(wildcard ../src/dhcpd/*.o) $(filter-out ../src/main.o, $(wildcard ../src/*.o))

ifneq ($(shell pkg-config --exists libdpdk && echo 0),0)
$(error "no installation of DPDK found")
Expand Down
45 changes: 44 additions & 1 deletion unit_test/pppd/codec_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
#include <assert.h>
#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);
}

void test_build_padi() {
assert(1 == 1);
U8 buffer[80];
U16 mulen;

init_ccb();

PPP_INFO_t s_ppp_ccb_1 = {
.pppoe_phase = {
.timer_counter = 0,
.max_retransmit = 10,
},
.user_num = 1,
.vlan = 2,
};
char 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(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);
}

0 comments on commit 2e78d50

Please sign in to comment.