Skip to content

Commit

Permalink
pppoe: VTY code refactor
Browse files Browse the repository at this point in the history
Create a dedicated vty command. Make pppoe configuration global and no specific to VRF so that
same PPPoE instance can be referenced to multiple VRF.
  • Loading branch information
acassen committed Apr 2, 2024
1 parent 2d94ef0 commit 976c2a1
Show file tree
Hide file tree
Showing 10 changed files with 697 additions and 553 deletions.
1 change: 1 addition & 0 deletions lib/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ typedef enum _node_type {

PDN_NODE, /* PDN daemon commands. */
IP_VRF_NODE, /* IP VRF commands. */
PPPOE_NODE, /* PPPoE commands. */
APN_NODE, /* APN commands. */
GTP_SWITCH_NODE, /* GTP Switch commands. */
GTP_ROUTER_NODE, /* GTP Router commands. */
Expand Down
3 changes: 2 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ OBJS = main.o gtp_data.o gtp_vty.o gtp_if.o gtp_server.o \
gtp_xdp_iptnl.o gtp_xdp_ppp.o gtp_msg.o gtp_vrf.o gtp_vrf_vty.o \
gtp_switch.o gtp_switch_vty.o gtp_switch_hdl.o gtp_switch_hdl_v1.o \
gtp_switch_hdl_v2.o gtp_router.o gtp_router_vty.o gtp_router_hdl.o \
gtp_pppoe.o gtp_pppoe_session.o gtp_pppoe_proto.o gtp_ppp.o
gtp_pppoe.o gtp_pppoe_session.o gtp_pppoe_proto.o gtp_pppoe_vty.o \
gtp_ppp.o

HEADERS = $(OBJS:.o=.h)

Expand Down
97 changes: 46 additions & 51 deletions src/gtp_pppoe.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,25 @@ pthread_mutex_t gtp_pppoe_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
* PPPoE utilities
*/
gtp_pppoe_t *
gtp_pppoe_get_by_name(const char *name)
{
gtp_pppoe_t *pppoe;

pthread_mutex_lock(&gtp_pppoe_mutex);
list_for_each_entry(pppoe, &daemon_data->pppoe, next) {
if (!strncmp(pppoe->name, name, GTP_NAME_MAX_LEN)) {
pppoe->refcnt++;
pthread_mutex_unlock(&gtp_pppoe_mutex);
return pppoe;
}
}
pthread_mutex_unlock(&gtp_pppoe_mutex);
return NULL;
}

static gtp_pppoe_t *
gtp_pppoe_get(const unsigned int ifindex)
gtp_pppoe_get_by_ifindex(const unsigned int ifindex)
{
gtp_pppoe_t *pppoe;

Expand Down Expand Up @@ -387,47 +404,6 @@ gtp_pppoe_timer_destroy(gtp_pppoe_t *pppoe)
}


/*
* VTY display
*/
static int
gtp_pppoe_worker_vty(vty_t *vty, gtp_pppoe_worker_t *w)
{
vty_out(vty, " #%.2d: rx_packets:%ld rx_bytes:%ld tx_packets:%ld tx_bytes:%ld%s"
, w->id, w->rx_packets, w->rx_bytes, w->tx_packets, w->tx_bytes
, VTY_NEWLINE);
return 0;
}

static int
gtp_pppoe_workers_vty(vty_t *vty, const char *desc, gtp_pppoe_worker_t *w, int count)
{
int i;

vty_out(vty, " %s:%s", desc, VTY_NEWLINE);
for (i = 0; i < count; i++)
gtp_pppoe_worker_vty(vty, &w[i]);

return 0;
}

int
gtp_pppoe_vty(vty_t *vty, gtp_pppoe_t *pppoe)
{
if (!pppoe)
return -1;

vty_out(vty, " PPPoE: interface %s (ifindex:%d) sessions:%d%s"
, pppoe->ifname, pppoe->ifindex, pppoe->session_count
, VTY_NEWLINE);
gtp_pppoe_workers_vty(vty, "Discovery channel"
, pppoe->worker_disc, pppoe->thread_cnt);
gtp_pppoe_workers_vty(vty, "Session channel"
, pppoe->worker_ses, pppoe->thread_cnt);
return 0;
}


/*
* PPPoE service init
*/
Expand Down Expand Up @@ -455,24 +431,43 @@ gtp_pppoe_start(gtp_pppoe_t *pppoe)
return 0;
}

int
gtp_pppoe_interface_init(gtp_pppoe_t *pppoe, const char *ifname)
{
unsigned int ifindex = if_nametoindex(ifname);

if (!ifindex) {
errno = EINVAL;
return -1;
}

if (gtp_pppoe_get_by_ifindex(ifindex)) {
errno = EEXIST;
return -1;
}

strlcpy(pppoe->ifname, ifname, GTP_NAME_MAX_LEN);
pppoe->ifindex = ifindex;
return 0;
}

gtp_pppoe_t *
gtp_pppoe_init(const char *ifname)
gtp_pppoe_init(const char *name)
{
gtp_pppoe_t *pppoe = NULL;
unsigned int ifindex = if_nametoindex(ifname);

if (!ifindex)
pppoe = gtp_pppoe_get_by_name(name);
if (pppoe) {
errno = EEXIST;
return NULL;

pppoe = gtp_pppoe_get(ifindex);
if (pppoe)
return pppoe;
}

PMALLOC(pppoe);
if (!pppoe)
return NULL;
strlcpy(pppoe->name, name, GTP_NAME_MAX_LEN);
pppoe->seed = time(NULL);
srand(pppoe->seed);
strlcpy(pppoe->ifname, ifname, GTP_NAME_MAX_LEN);
pppoe->ifindex = ifindex;
pkt_queue_init(&pppoe->pkt_q);
gtp_htab_init(&pppoe->session_tab, CONN_HASHTAB_SIZE);
gtp_htab_init(&pppoe->unique_tab, CONN_HASHTAB_SIZE);
Expand Down
Loading

0 comments on commit 976c2a1

Please sign in to comment.