7171#ifdef CONFIG_XDP_LUA
7272#include <lua.h>
7373#include <lauxlib.h>
74+ #include <lstate.h>
7475#include <lualib.h>
7576#include <luadata.h>
7677#endif /* CONFIG_XDP_LUA */
160161static DEFINE_SPINLOCK (ptype_lock );
161162static DEFINE_SPINLOCK (offload_lock );
162163#ifdef CONFIG_XDP_LUA
163- static DEFINE_PER_CPU (spinlock_t , lua_state_lock ) ;
164+ DEFINE_PER_CPU (struct xdplua_create_work , luaworks );
164165#endif
165166struct list_head ptype_base [PTYPE_HASH_SIZE ] __read_mostly ;
166167struct list_head ptype_all __read_mostly ; /* Taps */
@@ -4382,11 +4383,21 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
43824383 rxqueue = netif_get_rxqueue (skb );
43834384 xdp -> rxq = & rxqueue -> xdp_rxq ;
43844385#ifdef CONFIG_XDP_LUA
4386+ struct xdplua_create_work * lw = this_cpu_ptr (& luaworks );
4387+
43854388 xdp -> skb = skb ;
4389+ xdp -> L = lw -> L ;
43864390#endif /* CONFIG_XDP_LUA */
43874391
43884392 act = bpf_prog_run_xdp (xdp_prog , xdp );
43894393
4394+ #ifdef CONFIG_XDP_LUA
4395+ if (lw -> init ) {
4396+ lw -> init = false;
4397+ spin_unlock (& lw -> lock );
4398+ }
4399+ #endif
4400+
43904401 /* check if bpf_xdp_adjust_head was used */
43914402 off = xdp -> data - orig_data ;
43924403 if (off ) {
@@ -5197,24 +5208,30 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
51975208}
51985209
51995210#ifdef CONFIG_XDP_LUA
5200- DEFINE_PER_CPU (struct xdplua , xdplua_per_cpu );
52015211
5202- int generic_xdp_lua_install_prog ( char * lua_prog )
5203- {
5204- struct xdplua * sc ;
5205- int i ;
5212+ static void per_cpu_xdp_lua_install ( struct work_struct * w ) {
5213+ int this_cpu = smp_processor_id ();
5214+ struct xdplua_create_work * lw =
5215+ container_of ( w , struct xdplua_create_work , work ) ;
52065216
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- }
5217+ spin_lock_bh (& lw -> lock );
5218+ if (luaL_dostring (lw -> L , lw -> lua_script )) {
5219+ pr_err (KERN_INFO "error: %s\nOn cpu: %d\n" ,
5220+ lua_tostring (lw -> L , -1 ), this_cpu );
5221+ }
5222+ spin_unlock_bh (& lw -> lock );
5223+ }
5224+
5225+ int generic_xdp_lua_install_prog (char * lua_script )
5226+ {
5227+ int cpu ;
5228+ struct xdplua_create_work * lw ;
52165229
5217- spin_unlock_bh (sc -> lock );
5230+ for_each_possible_cpu (cpu ) {
5231+ lw = per_cpu_ptr (& luaworks , cpu );
5232+ lw -> lua_script = lua_script ;
5233+ INIT_WORK (& lw -> work , per_cpu_xdp_lua_install );
5234+ schedule_work_on (cpu , & lw -> work );
52185235 }
52195236 return 0 ;
52205237}
@@ -9862,7 +9879,9 @@ static int __init net_dev_init(void)
98629879 for_each_possible_cpu (i ) {
98639880 struct work_struct * flush = per_cpu_ptr (& flush_works , i );
98649881 struct softnet_data * sd = & per_cpu (softnet_data , i );
9865- struct xdplua * xdplua = per_cpu_ptr (& xdplua_per_cpu , i );
9882+ #ifdef CONFIG_XDP_LUA
9883+ struct xdplua_create_work * lw = per_cpu_ptr (& luaworks , i );
9884+ #endif
98669885
98679886 INIT_WORK (flush , flush_backlog );
98689887
@@ -9883,16 +9902,15 @@ static int __init net_dev_init(void)
98839902 sd -> backlog .poll = process_backlog ;
98849903 sd -> backlog .weight = weight_p ;
98859904#ifdef CONFIG_XDP_LUA
9886- xdplua -> L = luaL_newstate ();
9887- if (!xdplua -> L ) {
9888- kfree (xdplua );
9905+ lw -> L = luaL_newstate ();
9906+ WARN_ON (!lw -> L )
9907+
9908+ if (!lw -> L )
98899909 continue ;
9890- }
98919910
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 );
9911+ luaL_openlibs (lw -> L );
9912+ luaL_requiref (lw -> L , "data" , luaopen_data , 1 );
9913+ lua_pop (lw -> L , 1 );
98969914#endif /* CONFIG_XDP_LUA */
98979915 }
98989916
0 commit comments