71
71
#ifdef CONFIG_XDP_LUA
72
72
#include <lua.h>
73
73
#include <lauxlib.h>
74
+ #include <lstate.h>
74
75
#include <lualib.h>
75
76
#include <luadata.h>
76
77
#endif /* CONFIG_XDP_LUA */
160
161
static DEFINE_SPINLOCK (ptype_lock );
161
162
static DEFINE_SPINLOCK (offload_lock );
162
163
#ifdef CONFIG_XDP_LUA
163
- static DEFINE_PER_CPU (spinlock_t , lua_state_lock ) ;
164
+ DEFINE_PER_CPU (struct xdplua_create_work , luaworks );
164
165
#endif
165
166
struct list_head ptype_base [PTYPE_HASH_SIZE ] __read_mostly ;
166
167
struct list_head ptype_all __read_mostly ; /* Taps */
@@ -4337,6 +4338,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
4337
4338
bool orig_bcast ;
4338
4339
int hlen , off ;
4339
4340
u32 mac_len ;
4341
+ #ifdef CONFIG_XDP_LUA
4342
+ struct xdplua_create_work * lw ;
4343
+ #endif /* CONFIG_XDP_LUA */
4340
4344
4341
4345
/* Reinjected packets coming from act_mirred or similar should
4342
4346
* not get XDP generic processing.
@@ -4382,11 +4386,21 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
4382
4386
rxqueue = netif_get_rxqueue (skb );
4383
4387
xdp -> rxq = & rxqueue -> xdp_rxq ;
4384
4388
#ifdef CONFIG_XDP_LUA
4389
+ lw = this_cpu_ptr (& luaworks );
4390
+
4385
4391
xdp -> skb = skb ;
4392
+ xdp -> L = lw -> L ;
4386
4393
#endif /* CONFIG_XDP_LUA */
4387
4394
4388
4395
act = bpf_prog_run_xdp (xdp_prog , xdp );
4389
4396
4397
+ #ifdef CONFIG_XDP_LUA
4398
+ if (lw -> init ) {
4399
+ lw -> init = false;
4400
+ spin_unlock (& lw -> lock );
4401
+ }
4402
+ #endif /* CONFIG_XDP_LUA */
4403
+
4390
4404
/* check if bpf_xdp_adjust_head was used */
4391
4405
off = xdp -> data - orig_data ;
4392
4406
if (off ) {
@@ -5197,24 +5211,30 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
5197
5211
}
5198
5212
5199
5213
#ifdef CONFIG_XDP_LUA
5200
- DEFINE_PER_CPU (struct xdplua , xdplua_per_cpu );
5201
5214
5202
- int generic_xdp_lua_install_prog ( char * lua_prog )
5203
- {
5204
- struct xdplua * sc ;
5205
- int i ;
5215
+ static void per_cpu_xdp_lua_install ( struct work_struct * w ) {
5216
+ int this_cpu = smp_processor_id ();
5217
+ struct xdplua_create_work * lw =
5218
+ container_of ( w , struct xdplua_create_work , work ) ;
5206
5219
5207
- for_each_possible_cpu (i ) {
5208
- sc = per_cpu_ptr (& xdplua_per_cpu , i );
5209
- spin_lock_bh (sc -> lock );
5210
- if (luaL_dostring (sc -> L , lua_prog )) {
5211
- pr_err (KERN_INFO "error: %s\nOn cpu: %d\n" ,
5212
- lua_tostring (sc -> L , -1 ), i );
5213
- spin_unlock_bh (sc -> lock );
5214
- return - EINVAL ;
5215
- }
5220
+ spin_lock_bh (& lw -> lock );
5221
+ if (luaL_dostring (lw -> L , lw -> lua_script )) {
5222
+ pr_err (KERN_INFO "error: %s\nOn cpu: %d\n" ,
5223
+ lua_tostring (lw -> L , -1 ), this_cpu );
5224
+ }
5225
+ spin_unlock_bh (& lw -> lock );
5226
+ }
5227
+
5228
+ int generic_xdp_lua_install_prog (char * lua_script )
5229
+ {
5230
+ int cpu ;
5231
+ struct xdplua_create_work * lw ;
5216
5232
5217
- spin_unlock_bh (sc -> lock );
5233
+ for_each_possible_cpu (cpu ) {
5234
+ lw = per_cpu_ptr (& luaworks , cpu );
5235
+ lw -> lua_script = lua_script ;
5236
+ INIT_WORK (& lw -> work , per_cpu_xdp_lua_install );
5237
+ schedule_work_on (cpu , & lw -> work );
5218
5238
}
5219
5239
return 0 ;
5220
5240
}
@@ -9862,7 +9882,9 @@ static int __init net_dev_init(void)
9862
9882
for_each_possible_cpu (i ) {
9863
9883
struct work_struct * flush = per_cpu_ptr (& flush_works , i );
9864
9884
struct softnet_data * sd = & per_cpu (softnet_data , i );
9865
- struct xdplua * xdplua = per_cpu_ptr (& xdplua_per_cpu , i );
9885
+ #ifdef CONFIG_XDP_LUA
9886
+ struct xdplua_create_work * lw = per_cpu_ptr (& luaworks , i );
9887
+ #endif
9866
9888
9867
9889
INIT_WORK (flush , flush_backlog );
9868
9890
@@ -9883,16 +9905,15 @@ static int __init net_dev_init(void)
9883
9905
sd -> backlog .poll = process_backlog ;
9884
9906
sd -> backlog .weight = weight_p ;
9885
9907
#ifdef CONFIG_XDP_LUA
9886
- xdplua -> L = luaL_newstate ();
9887
- if (!xdplua -> L ) {
9888
- kfree (xdplua );
9908
+ lw -> L = luaL_newstate ();
9909
+ WARN_ON (!lw -> L );
9910
+
9911
+ if (!lw -> L )
9889
9912
continue ;
9890
- }
9891
9913
9892
- luaL_openlibs (xdplua -> L );
9893
- luaL_requiref (xdplua -> L , "data" , luaopen_data , 1 );
9894
- lua_pop (xdplua -> L , 1 );
9895
- xdplua -> lock = per_cpu_ptr (& lua_state_lock , i );
9914
+ luaL_openlibs (lw -> L );
9915
+ luaL_requiref (lw -> L , "data" , luaopen_data , 1 );
9916
+ lua_pop (lw -> L , 1 );
9896
9917
#endif /* CONFIG_XDP_LUA */
9897
9918
}
9898
9919
0 commit comments