Skip to content

Commit bfeab4b

Browse files
T8010: Refactoring VPP hooks private data
Using the session's private data list instead of a dedicated field. Signed-off-by: Andrii Melnychenko <a.melnychenko@vyos.io>
1 parent e451115 commit bfeab4b

6 files changed

Lines changed: 52 additions & 29 deletions

File tree

accel-pppd/include/ap_session.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ struct ap_session
130130

131131
#ifdef HAVE_SESSION_HOOKS
132132
struct ap_session_hooks_t *hooks;
133-
void *hooks_priv_data; /* private data for hooks */
134133
#endif /* HAVE_SESSION_HOOKS */
135134
};
136135

accel-pppd/session.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ void __export ap_session_init(struct ap_session *ses)
8686

8787
#ifdef HAVE_SESSION_HOOKS
8888
ses->hooks = NULL;
89-
ses->hooks_priv_data = NULL;
9089
#endif /* HAVE_SESSION_HOOKS */
9190

9291
ses->vrf_name = NULL;

accel-pppd/session_hooks/vyos_vpp/vpphooks.c

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,44 @@
2626
/* NOTE: function from ctrl/pppoe plugin! */
2727
void pppoe_get_session_mac_and_sid(struct ap_session *ses, uint8_t **mac, uint16_t *sid);
2828

29+
static void *pd_key;
30+
31+
struct vpphook_private_data_t *vpphook_get_pd(struct ap_session *ses)
32+
{
33+
struct ap_private *pd;
34+
35+
list_for_each_entry(pd, &ses->pd_list, entry) {
36+
if (pd->key == &pd_key)
37+
return container_of(pd, struct vpphook_private_data_t, pd);
38+
}
39+
40+
return NULL;
41+
}
42+
2943
int vpphook_session_hook_init(struct ap_session *ses)
3044
{
31-
ses->hooks_priv_data = _malloc(sizeof(struct vpphook_private_data_t));
32-
memset(ses->hooks_priv_data, 0, sizeof(struct vpphook_private_data_t));
45+
struct vpphook_private_data_t *pd = _malloc(sizeof(struct vpphook_private_data_t));
46+
if (pd == NULL)
47+
return -1;
48+
49+
memset(pd, 0, sizeof(struct vpphook_private_data_t));
50+
pd->pd.key = &pd_key;
3351

34-
INIT_LIST_HEAD(&VPPHOOK_GET_PRIV(ses)->vpp_routes);
52+
INIT_LIST_HEAD(&pd->vpp_routes);
3553

36-
return ses->hooks_priv_data == NULL ? -1 : 0; /* return 0 is ok */
54+
list_add_tail(&pd->pd.entry, &ses->pd_list);
55+
56+
return 0; /* return 0 is ok */
3757
}
3858

3959
void vpphook_session_hook_deinit(struct ap_session *ses)
4060
{
41-
_free(ses->hooks_priv_data);
42-
ses->hooks_priv_data = NULL;
61+
struct vpphook_private_data_t *pd = vpphook_get_pd(ses);
62+
if (pd == NULL)
63+
return;
64+
65+
list_del(&pd->pd.entry);
66+
_free(pd);
4367
}
4468

4569
int vpphook_pppoe_create_vpp_session_interface(struct ap_session *ses)
@@ -56,7 +80,7 @@ int vpphook_pppoe_create_vpp_session_interface(struct ap_session *ses)
5680
return ret;
5781
}
5882

59-
VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index = ifindex;
83+
vpphook_get_pd(ses)->vpp_sw_if_index = ifindex;
6084
vpppoe_dump_interface_name(ifindex, ses->ifname, AP_IFNAME_LEN);
6185

6286
if (ses->ipv4) {
@@ -92,73 +116,73 @@ int vpphook_pppoe_terminate(struct ap_session *ses, int hard)
92116

93117
int vpphook_ipaddr_add(struct ap_session *ses, int ifindex, in_addr_t addr, int mask)
94118
{
95-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
119+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
96120
/* setup route instead */
97121
return vpp_iproute_add_del(ses, 1, sw_ifindex, 0, addr, 0, 0, mask, 0);
98122
}
99123

100124
int vpphook_ipaddr_add_peer(struct ap_session *ses, int ifindex, in_addr_t addr, in_addr_t peer_addr)
101125
{
102-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
126+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
103127
/* setup route instead */
104128
return vpp_iproute_add_del(ses, 1, sw_ifindex, 0, peer_addr, 0, 0, 32, 0);
105129
}
106130

107131
int vpphook_ipaddr_del(struct ap_session *ses, int ifindex, in_addr_t addr, int mask)
108132
{
109-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
133+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
110134
/* remove route instead */
111135
return vpp_iproute_add_del(ses, 0, sw_ifindex, 0, addr, 0, 0, mask, 0);
112136
}
113137

114138
int vpphook_ipaddr_del_peer(struct ap_session *ses, int ifindex, in_addr_t addr, in_addr_t peer)
115139
{
116-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
140+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
117141
/* remove route instead */
118142
return vpp_iproute_add_del(ses, 0, sw_ifindex, 0, peer, 0, 0, 32, 0);
119143
}
120144

121145
int vpphook_iproute_add(struct ap_session *ses, int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw, int proto, int mask, uint32_t prio, const char *vrf_name)
122146
{
123-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
147+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
124148
return vpp_iproute_add_del(ses, 1, sw_ifindex, src, dst, gw, proto, mask, prio);
125149
}
126150

127151
int vpphook_iproute_del(struct ap_session *ses, int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw, int proto, int mask, uint32_t prio, const char *vrf_name)
128152
{
129-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
153+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
130154
return vpp_iproute_add_del(ses, 0, sw_ifindex, src, dst, gw, proto, mask, prio);
131155
}
132156

133157
int vpphook_ip6route_add(struct ap_session *ses, int ifindex, const struct in6_addr *dst, int pref_len, const struct in6_addr *gw, int proto, uint32_t prio, const char *vrf_name)
134158
{
135-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
159+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
136160
return vpp_ip6route_add_del(ses, 1, sw_ifindex, dst, pref_len, gw, proto, prio);
137161
}
138162

139163
int vpphook_ip6route_del(struct ap_session *ses, int ifindex, const struct in6_addr *dst, int pref_len, const struct in6_addr *gw, int proto, uint32_t prio, const char *vrf_name)
140164
{
141-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
165+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
142166
return vpp_ip6route_add_del(ses, 0, sw_ifindex, dst, pref_len, gw, proto, prio);
143167
}
144168

145169
int vpphook_ip6addr_add(struct ap_session *ses, int ifindex, struct in6_addr *addr, int prefix_len)
146170
{
147-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
171+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
148172
/* setup route instead */
149173
return vpp_ip6route_add_del(ses, 1, sw_ifindex, addr, prefix_len, NULL, 0, 0);
150174
}
151175

152176
int vpphook_ip6addr_add_peer(struct ap_session *ses, int ifindex, struct in6_addr *addr, struct in6_addr *peer_addr)
153177
{
154-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
178+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
155179
/* setup route instead */
156180
return vpp_ip6route_add_del(ses, 1, sw_ifindex, peer_addr, 128, NULL, 0, 0);
157181
}
158182

159183
int vpphook_ip6addr_del(struct ap_session *ses, int ifindex, struct in6_addr *addr, int prefix_len)
160184
{
161-
uint32_t sw_ifindex = VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index;
185+
uint32_t sw_ifindex = vpphook_get_pd(ses)->vpp_sw_if_index;
162186
/* remove route instead */
163187
return vpp_ip6route_add_del(ses, 0, sw_ifindex, addr, prefix_len, NULL, 0, 0);
164188
}

accel-pppd/session_hooks/vyos_vpp/vpphooks.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "list.h"
1515

1616
struct vpphook_private_data_t {
17+
struct ap_private pd;
1718
uint32_t vpp_sw_if_index;
1819
struct list_head vpp_routes;
1920
uint32_t vpppolicer_down;
@@ -22,6 +23,6 @@ struct vpphook_private_data_t {
2223
uint64_t vpppolicer_up_burst;
2324
};
2425

25-
#define VPPHOOK_GET_PRIV(ses) ((struct vpphook_private_data_t *)(ses)->hooks_priv_data)
26+
struct vpphook_private_data_t *vpphook_get_pd(struct ap_session *ses);
2627

2728
#endif /* VPPHOOKS_H */

accel-pppd/session_hooks/vyos_vpp/vppiputils.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct vpp_route_t {
3838
static void vpp_iproute_save(struct ap_session *ses, in_addr_t dst, int mask)
3939
{
4040
struct vpp_route_t *saved_route = (struct vpp_route_t *)malloc(sizeof(*saved_route));
41-
struct vpphook_private_data_t *vpphook_data = VPPHOOK_GET_PRIV(ses);
41+
struct vpphook_private_data_t *vpphook_data = vpphook_get_pd(ses);
4242
saved_route->af = AF_INET;
4343
memcpy(&saved_route->un.ip4.dst, &dst, sizeof(dst));
4444
saved_route->un.ip4.mask = mask;
@@ -48,7 +48,7 @@ static void vpp_iproute_save(struct ap_session *ses, in_addr_t dst, int mask)
4848
static void vpp_iproute_del_saved(struct ap_session *ses, in_addr_t dst, int mask)
4949
{
5050
struct list_head *pos, *n;
51-
struct vpphook_private_data_t *vpphook_data = VPPHOOK_GET_PRIV(ses);
51+
struct vpphook_private_data_t *vpphook_data = vpphook_get_pd(ses);
5252

5353
list_for_each_safe(pos, n, &vpphook_data->vpp_routes) {
5454
struct vpp_route_t *saved_route = list_entry(pos, typeof(*saved_route), entry);
@@ -63,7 +63,7 @@ static void vpp_iproute_del_saved(struct ap_session *ses, in_addr_t dst, int mas
6363
static void vpp_ip6route_save(struct ap_session *ses, const struct in6_addr *dst, int pref_len)
6464
{
6565
struct vpp_route_t *saved_route = (struct vpp_route_t *)malloc(sizeof(*saved_route));
66-
struct vpphook_private_data_t *vpphook_data = VPPHOOK_GET_PRIV(ses);
66+
struct vpphook_private_data_t *vpphook_data = vpphook_get_pd(ses);
6767
saved_route->af = AF_INET6;
6868
memcpy(&saved_route->un.ip6.dst, dst, sizeof(*dst));
6969
saved_route->un.ip6.preffix = pref_len;
@@ -73,7 +73,7 @@ static void vpp_ip6route_save(struct ap_session *ses, const struct in6_addr *dst
7373
static void vpp_ip6route_del_saved(struct ap_session *ses, const struct in6_addr *dst, int pref_len)
7474
{
7575
struct list_head *pos, *n;
76-
struct vpphook_private_data_t *vpphook_data = VPPHOOK_GET_PRIV(ses);
76+
struct vpphook_private_data_t *vpphook_data = vpphook_get_pd(ses);
7777

7878
list_for_each_safe(pos, n, &vpphook_data->vpp_routes) {
7979
struct vpp_route_t *saved_route = list_entry(pos, typeof(*saved_route), entry);
@@ -187,7 +187,7 @@ int vpp_ip6route_add_del(struct ap_session *ses, int is_add, int ifindex, const
187187
int vpp_iproute_flush(struct ap_session *ses)
188188
{
189189
struct list_head *pos, *n;
190-
struct vpphook_private_data_t *vpphook_data = VPPHOOK_GET_PRIV(ses);
190+
struct vpphook_private_data_t *vpphook_data = vpphook_get_pd(ses);
191191

192192
list_for_each_safe(pos, n, &vpphook_data->vpp_routes) {
193193
struct vpp_route_t *saved_route = list_entry(pos, typeof(*saved_route), entry);

accel-pppd/session_hooks/vyos_vpp/vpppolicer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ static int vpppolicer_add_del(int is_add, const char *policer_name, uint32_t cir
167167

168168
static void vpppolicer_generate_name(char *name, size_t size, struct ap_session *ses, uint32_t rate, uint64_t burst, int is_up)
169169
{
170-
snprintf(name, size, "vyos_%d_%d_%ld_%s", VPPHOOK_GET_PRIV(ses)->vpp_sw_if_index, rate, burst, is_up ? "up" : "down");
170+
snprintf(name, size, "vyos_%d_%d_%ld_%s", vpphook_get_pd(ses)->vpp_sw_if_index, rate, burst, is_up ? "up" : "down");
171171
}
172172

173173
int vpppolicer_install_limiter(struct ap_session *ses, int down_speed, int down_burst, int up_speed, int up_burst)
174174
{
175175
int ret = 0;
176176
char policer_input[POLICER_NAME_MAX_LEN] = {};
177177
char policer_output[POLICER_NAME_MAX_LEN] = {};
178-
struct vpphook_private_data_t *vpphook_data = VPPHOOK_GET_PRIV(ses);
178+
struct vpphook_private_data_t *vpphook_data = vpphook_get_pd(ses);
179179

180180
/* convert rate to kbits/s and burst to bits */
181181
down_speed = down_speed * 8 / 1000;
@@ -232,7 +232,7 @@ int vpppolicer_remove_limiter(struct ap_session *ses)
232232
int ret = 0;
233233
char policer_input[64] = {};
234234
char policer_output[64] = {};
235-
struct vpphook_private_data_t *vpphook_data = VPPHOOK_GET_PRIV(ses);
235+
struct vpphook_private_data_t *vpphook_data = vpphook_get_pd(ses);
236236

237237
if (vpphook_data->vpppolicer_down) {
238238
vpppolicer_generate_name(policer_output, POLICER_NAME_MAX_LEN - 1, ses, vpphook_data->vpppolicer_down, vpphook_data->vpppolicer_down_burst, 0);

0 commit comments

Comments
 (0)