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 */
@@ -4337,6 +4338,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
43374338 bool orig_bcast ;
43384339 int hlen , off ;
43394340 u32 mac_len ;
4341+ #ifdef CONFIG_XDP_LUA
4342+ struct xdplua_create_work * lw ;
4343+ #endif /* CONFIG_XDP_LUA */
43404344
43414345 /* Reinjected packets coming from act_mirred or similar should
43424346 * not get XDP generic processing.
@@ -4382,11 +4386,21 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
43824386 rxqueue = netif_get_rxqueue (skb );
43834387 xdp -> rxq = & rxqueue -> xdp_rxq ;
43844388#ifdef CONFIG_XDP_LUA
4389+ lw = this_cpu_ptr (& luaworks );
4390+
43854391 xdp -> skb = skb ;
4392+ xdp -> L = lw -> L ;
43864393#endif /* CONFIG_XDP_LUA */
43874394
43884395 act = bpf_prog_run_xdp (xdp_prog , xdp );
43894396
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+
43904404 /* check if bpf_xdp_adjust_head was used */
43914405 off = xdp -> data - orig_data ;
43924406 if (off ) {
@@ -5197,24 +5211,30 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
51975211}
51985212
51995213#ifdef CONFIG_XDP_LUA
5200- DEFINE_PER_CPU (struct xdplua , xdplua_per_cpu );
52015214
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 ) ;
52065219
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 ;
52165232
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 );
52185238 }
52195239 return 0 ;
52205240}
@@ -9862,7 +9882,9 @@ static int __init net_dev_init(void)
98629882 for_each_possible_cpu (i ) {
98639883 struct work_struct * flush = per_cpu_ptr (& flush_works , i );
98649884 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
98669888
98679889 INIT_WORK (flush , flush_backlog );
98689890
@@ -9883,16 +9905,15 @@ static int __init net_dev_init(void)
98839905 sd -> backlog .poll = process_backlog ;
98849906 sd -> backlog .weight = weight_p ;
98859907#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 )
98899912 continue ;
9890- }
98919913
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 );
98969917#endif /* CONFIG_XDP_LUA */
98979918 }
98989919
0 commit comments