2019-05-19 15:08:20 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2005-04-17 02:20:36 +04:00
/*
* net / sched / cls_tcindex . c Packet classifier for skb - > tc_index
*
* Written 1998 , 1999 by Werner Almesberger , EPFL ICA
*/
# include <linux/module.h>
# include <linux/types.h>
# include <linux/kernel.h>
# include <linux/skbuff.h>
# include <linux/errno.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2020-03-28 22:12:59 +03:00
# include <linux/refcount.h>
2005-04-17 02:20:36 +04:00
# include <net/act_api.h>
2007-03-26 10:06:12 +04:00
# include <net/netlink.h>
2005-04-17 02:20:36 +04:00
# include <net/pkt_cls.h>
2017-10-13 15:01:03 +03:00
# include <net/sch_generic.h>
2022-12-06 16:55:13 +03:00
# include <net/tc_wrapper.h>
2005-04-17 02:20:36 +04:00
/*
* Passing parameters to the root seems to be done more awkwardly than really
* necessary . At least , u32 doesn ' t seem to use such dirty hacks . To be
* verified . FIXME .
*/
# define PERFECT_HASH_THRESHOLD 64 /* use perfect hash if not bigger */
# define DEFAULT_HASH_SIZE 64 /* optimized for diffserv */
2020-03-28 22:12:59 +03:00
struct tcindex_data ;
2005-04-17 02:20:36 +04:00
struct tcindex_filter_result {
struct tcf_exts exts ;
struct tcf_result res ;
2020-03-28 22:12:59 +03:00
struct tcindex_data * p ;
2018-05-24 01:26:53 +03:00
struct rcu_work rwork ;
2005-04-17 02:20:36 +04:00
} ;
struct tcindex_filter {
u16 key ;
struct tcindex_filter_result result ;
2014-09-13 07:08:20 +04:00
struct tcindex_filter __rcu * next ;
2018-05-24 01:26:53 +03:00
struct rcu_work rwork ;
2005-04-17 02:20:36 +04:00
} ;
struct tcindex_data {
struct tcindex_filter_result * perfect ; /* perfect hash; NULL if none */
2014-09-13 07:08:20 +04:00
struct tcindex_filter __rcu * * h ; /* imperfect hash; */
struct tcf_proto * tp ;
2005-04-17 02:20:36 +04:00
u16 mask ; /* AND key with mask */
2014-09-13 07:08:20 +04:00
u32 shift ; /* shift ANDed key to the right */
u32 hash ; /* hash table size; 0 if undefined */
u32 alloc_hash ; /* allocated size */
u32 fall_through ; /* 0: only classify if explicit match */
2020-03-28 22:12:59 +03:00
refcount_t refcnt ; /* a temporary refcnt for perfect hash */
2019-02-16 21:58:26 +03:00
struct rcu_work rwork ;
2005-04-17 02:20:36 +04:00
} ;
2016-09-18 15:45:33 +03:00
static inline int tcindex_filter_is_set ( struct tcindex_filter_result * r )
2005-04-17 02:20:36 +04:00
{
2017-08-04 15:29:00 +03:00
return tcf_exts_has_actions ( & r - > exts ) | | r - > res . classid ;
2005-04-17 02:20:36 +04:00
}
2020-03-28 22:12:59 +03:00
static void tcindex_data_get ( struct tcindex_data * p )
{
refcount_inc ( & p - > refcnt ) ;
}
static void tcindex_data_put ( struct tcindex_data * p )
{
if ( refcount_dec_and_test ( & p - > refcnt ) ) {
kfree ( p - > perfect ) ;
kfree ( p - > h ) ;
kfree ( p ) ;
}
}
2016-09-18 15:45:33 +03:00
static struct tcindex_filter_result * tcindex_lookup ( struct tcindex_data * p ,
u16 key )
2005-04-17 02:20:36 +04:00
{
2014-09-13 07:08:20 +04:00
if ( p - > perfect ) {
struct tcindex_filter_result * f = p - > perfect + key ;
return tcindex_filter_is_set ( f ) ? f : NULL ;
} else if ( p - > h ) {
struct tcindex_filter __rcu * * fp ;
struct tcindex_filter * f ;
2005-04-17 02:20:36 +04:00
2014-09-13 07:08:20 +04:00
fp = & p - > h [ key % p - > hash ] ;
for ( f = rcu_dereference_bh_rtnl ( * fp ) ;
f ;
fp = & f - > next , f = rcu_dereference_bh_rtnl ( * fp ) )
2005-04-17 02:20:36 +04:00
if ( f - > key = = key )
return & f - > result ;
}
return NULL ;
}
2022-12-06 16:55:13 +03:00
TC_INDIRECT_SCOPE int tcindex_classify ( struct sk_buff * skb ,
const struct tcf_proto * tp ,
struct tcf_result * res )
2005-04-17 02:20:36 +04:00
{
2014-09-16 01:06:48 +04:00
struct tcindex_data * p = rcu_dereference_bh ( tp - > root ) ;
2005-04-17 02:20:36 +04:00
struct tcindex_filter_result * f ;
int key = ( skb - > tc_index & p - > mask ) > > p - > shift ;
2008-01-21 13:26:41 +03:00
pr_debug ( " tcindex_classify(skb %p,tp %p,res %p),p %p \n " ,
skb , tp , res , p ) ;
2005-04-17 02:20:36 +04:00
f = tcindex_lookup ( p , key ) ;
if ( ! f ) {
2017-10-13 15:01:03 +03:00
struct Qdisc * q = tcf_block_q ( tp - > chain - > block ) ;
2005-04-17 02:20:36 +04:00
if ( ! p - > fall_through )
return - 1 ;
2017-10-13 15:01:03 +03:00
res - > classid = TC_H_MAKE ( TC_H_MAJ ( q - > handle ) , key ) ;
2005-04-17 02:20:36 +04:00
res - > class = 0 ;
2008-01-21 13:26:41 +03:00
pr_debug ( " alg 0x%x \n " , res - > classid ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
}
* res = f - > res ;
2008-01-21 13:26:41 +03:00
pr_debug ( " map 0x%x \n " , res - > classid ) ;
2005-04-17 02:20:36 +04:00
return tcf_exts_exec ( skb , & f - > exts , res ) ;
}
2017-08-05 07:31:43 +03:00
static void * tcindex_get ( struct tcf_proto * tp , u32 handle )
2005-04-17 02:20:36 +04:00
{
2014-09-13 07:08:20 +04:00
struct tcindex_data * p = rtnl_dereference ( tp - > root ) ;
2005-04-17 02:20:36 +04:00
struct tcindex_filter_result * r ;
2008-01-21 13:26:41 +03:00
pr_debug ( " tcindex_get(tp %p,handle 0x%08x) \n " , tp , handle ) ;
2005-04-17 02:20:36 +04:00
if ( p - > perfect & & handle > = p - > alloc_hash )
2017-08-05 07:31:43 +03:00
return NULL ;
2005-04-17 02:20:36 +04:00
r = tcindex_lookup ( p , handle ) ;
2017-08-05 07:31:43 +03:00
return r & & tcindex_filter_is_set ( r ) ? r : NULL ;
2005-04-17 02:20:36 +04:00
}
static int tcindex_init ( struct tcf_proto * tp )
{
struct tcindex_data * p ;
2008-01-21 13:26:41 +03:00
pr_debug ( " tcindex_init(tp %p) \n " , tp ) ;
p = kzalloc ( sizeof ( struct tcindex_data ) , GFP_KERNEL ) ;
2005-04-17 02:20:36 +04:00
if ( ! p )
return - ENOMEM ;
p - > mask = 0xffff ;
p - > hash = DEFAULT_HASH_SIZE ;
p - > fall_through = 1 ;
2020-04-03 06:58:51 +03:00
refcount_set ( & p - > refcnt , 1 ) ; /* Paired with tcindex_destroy_work() */
2005-04-17 02:20:36 +04:00
2014-09-13 07:08:20 +04:00
rcu_assign_pointer ( tp - > root , p ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
}
2017-11-07 00:47:29 +03:00
static void __tcindex_destroy_rexts ( struct tcindex_filter_result * r )
{
tcf_exts_destroy ( & r - > exts ) ;
tcf_exts_put_net ( & r - > exts ) ;
2020-03-28 22:12:59 +03:00
tcindex_data_put ( r - > p ) ;
2017-11-07 00:47:29 +03:00
}
2017-10-27 04:24:39 +03:00
static void tcindex_destroy_rexts_work ( struct work_struct * work )
{
struct tcindex_filter_result * r ;
2018-05-24 01:26:53 +03:00
r = container_of ( to_rcu_work ( work ) ,
struct tcindex_filter_result ,
rwork ) ;
2017-10-27 04:24:39 +03:00
rtnl_lock ( ) ;
2017-11-07 00:47:29 +03:00
__tcindex_destroy_rexts ( r ) ;
2017-10-27 04:24:39 +03:00
rtnl_unlock ( ) ;
}
2017-11-07 00:47:29 +03:00
static void __tcindex_destroy_fexts ( struct tcindex_filter * f )
{
tcf_exts_destroy ( & f - > result . exts ) ;
tcf_exts_put_net ( & f - > result . exts ) ;
kfree ( f ) ;
}
2017-10-27 04:24:39 +03:00
static void tcindex_destroy_fexts_work ( struct work_struct * work )
{
2018-05-24 01:26:53 +03:00
struct tcindex_filter * f = container_of ( to_rcu_work ( work ) ,
struct tcindex_filter ,
rwork ) ;
2017-10-27 04:24:39 +03:00
rtnl_lock ( ) ;
2017-11-07 00:47:29 +03:00
__tcindex_destroy_fexts ( f ) ;
2017-10-27 04:24:39 +03:00
rtnl_unlock ( ) ;
2015-08-26 06:06:33 +03:00
}
2018-01-18 19:20:53 +03:00
static int tcindex_delete ( struct tcf_proto * tp , void * arg , bool * last ,
2019-02-11 11:55:45 +03:00
bool rtnl_held , struct netlink_ext_ack * extack )
2005-04-17 02:20:36 +04:00
{
2014-09-13 07:08:20 +04:00
struct tcindex_data * p = rtnl_dereference ( tp - > root ) ;
2017-08-05 07:31:43 +03:00
struct tcindex_filter_result * r = arg ;
2014-09-13 07:08:20 +04:00
struct tcindex_filter __rcu * * walk ;
2005-04-17 02:20:36 +04:00
struct tcindex_filter * f = NULL ;
2017-08-05 07:31:43 +03:00
pr_debug ( " tcindex_delete(tp %p,arg %p),p %p \n " , tp , arg , p ) ;
2005-04-17 02:20:36 +04:00
if ( p - > perfect ) {
if ( ! r - > res . class )
return - ENOENT ;
} else {
int i ;
2014-09-13 07:08:20 +04:00
for ( i = 0 ; i < p - > hash ; i + + ) {
walk = p - > h + i ;
for ( f = rtnl_dereference ( * walk ) ; f ;
walk = & f - > next , f = rtnl_dereference ( * walk ) ) {
if ( & f - > result = = r )
2005-04-17 02:20:36 +04:00
goto found ;
2014-09-13 07:08:20 +04:00
}
}
2005-04-17 02:20:36 +04:00
return - ENOENT ;
found :
2014-09-13 07:08:20 +04:00
rcu_assign_pointer ( * walk , rtnl_dereference ( f - > next ) ) ;
2005-04-17 02:20:36 +04:00
}
tcf_unbind_filter ( tp , & r - > res ) ;
2015-08-26 06:06:33 +03:00
/* all classifiers are required to call tcf_exts_destroy() after rcu
* grace period , since converted - to - rcu actions are relying on that
* in cleanup ( ) callback
*/
2017-11-07 00:47:29 +03:00
if ( f ) {
if ( tcf_exts_get_net ( & f - > result . exts ) )
2018-05-24 01:26:53 +03:00
tcf_queue_work ( & f - > rwork , tcindex_destroy_fexts_work ) ;
2017-11-07 00:47:29 +03:00
else
__tcindex_destroy_fexts ( f ) ;
} else {
2020-03-28 22:12:59 +03:00
tcindex_data_get ( p ) ;
2017-11-07 00:47:29 +03:00
if ( tcf_exts_get_net ( & r - > exts ) )
2018-05-24 01:26:53 +03:00
tcf_queue_work ( & r - > rwork , tcindex_destroy_rexts_work ) ;
2017-11-07 00:47:29 +03:00
else
__tcindex_destroy_rexts ( r ) ;
}
2017-04-20 00:21:21 +03:00
* last = false ;
2005-04-17 02:20:36 +04:00
return 0 ;
}
2019-02-16 21:58:26 +03:00
static void tcindex_destroy_work ( struct work_struct * work )
2014-09-13 07:08:20 +04:00
{
2019-02-16 21:58:26 +03:00
struct tcindex_data * p = container_of ( to_rcu_work ( work ) ,
struct tcindex_data ,
rwork ) ;
2014-09-13 07:08:20 +04:00
2020-03-28 22:12:59 +03:00
tcindex_data_put ( p ) ;
2005-04-17 02:20:36 +04:00
}
static inline int
valid_perfect_hash ( struct tcindex_data * p )
{
return p - > hash > ( p - > mask > > p - > shift ) ;
}
2008-01-24 07:36:12 +03:00
static const struct nla_policy tcindex_policy [ TCA_TCINDEX_MAX + 1 ] = {
[ TCA_TCINDEX_HASH ] = { . type = NLA_U32 } ,
[ TCA_TCINDEX_MASK ] = { . type = NLA_U16 } ,
[ TCA_TCINDEX_SHIFT ] = { . type = NLA_U32 } ,
[ TCA_TCINDEX_FALL_THROUGH ] = { . type = NLA_U32 } ,
[ TCA_TCINDEX_CLASSID ] = { . type = NLA_U32 } ,
} ;
2019-02-21 08:37:42 +03:00
static int tcindex_filter_result_init ( struct tcindex_filter_result * r ,
2020-03-28 22:12:59 +03:00
struct tcindex_data * p ,
2019-02-21 08:37:42 +03:00
struct net * net )
net_sched: fix an oops in tcindex filter
Kelly reported the following crash:
IP: [<ffffffff817a993d>] tcf_action_exec+0x46/0x90
PGD 3009067 PUD 300c067 PMD 11ff30067 PTE 800000011634b060
Oops: 0000 [#1] SMP DEBUG_PAGEALLOC
CPU: 1 PID: 639 Comm: dhclient Not tainted 3.15.0-rc4+ #342
Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
task: ffff8801169ecd00 ti: ffff8800d21b8000 task.ti: ffff8800d21b8000
RIP: 0010:[<ffffffff817a993d>] [<ffffffff817a993d>] tcf_action_exec+0x46/0x90
RSP: 0018:ffff8800d21b9b90 EFLAGS: 00010283
RAX: 00000000ffffffff RBX: ffff88011634b8e8 RCX: ffff8800cf7133d8
RDX: ffff88011634b900 RSI: ffff8800cf7133e0 RDI: ffff8800d210f840
RBP: ffff8800d21b9bb0 R08: ffffffff8287bf60 R09: 0000000000000001
R10: ffff8800d2b22b24 R11: 0000000000000001 R12: ffff8800d210f840
R13: ffff8800d21b9c50 R14: ffff8800cf7133e0 R15: ffff8800cad433d8
FS: 00007f49723e1840(0000) GS:ffff88011a800000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: ffff88011634b8f0 CR3: 00000000ce469000 CR4: 00000000000006e0
Stack:
ffff8800d2170188 ffff8800d210f840 ffff8800d2171b90 0000000000000000
ffff8800d21b9be8 ffffffff817c55bb ffff8800d21b9c50 ffff8800d2171b90
ffff8800d210f840 ffff8800d21b0300 ffff8800d21b9c50 ffff8800d21b9c18
Call Trace:
[<ffffffff817c55bb>] tcindex_classify+0x88/0x9b
[<ffffffff817a7f7d>] tc_classify_compat+0x3e/0x7b
[<ffffffff817a7fdf>] tc_classify+0x25/0x9f
[<ffffffff817b0e68>] htb_enqueue+0x55/0x27a
[<ffffffff817b6c2e>] dsmark_enqueue+0x165/0x1a4
[<ffffffff81775642>] __dev_queue_xmit+0x35e/0x536
[<ffffffff8177582a>] dev_queue_xmit+0x10/0x12
[<ffffffff818f8ecd>] packet_sendmsg+0xb26/0xb9a
[<ffffffff810b1507>] ? __lock_acquire+0x3ae/0xdf3
[<ffffffff8175cf08>] __sock_sendmsg_nosec+0x25/0x27
[<ffffffff8175d916>] sock_aio_write+0xd0/0xe7
[<ffffffff8117d6b8>] do_sync_write+0x59/0x78
[<ffffffff8117d84d>] vfs_write+0xb5/0x10a
[<ffffffff8117d96a>] SyS_write+0x49/0x7f
[<ffffffff8198e212>] system_call_fastpath+0x16/0x1b
This is because we memcpy struct tcindex_filter_result which contains
struct tcf_exts, obviously struct list_head can not be simply copied.
This is a regression introduced by commit 33be627159913b094bb578
(net_sched: act: use standard struct list_head).
It's not very easy to fix it as the code is a mess:
if (old_r)
memcpy(&cr, r, sizeof(cr));
else {
memset(&cr, 0, sizeof(cr));
tcf_exts_init(&cr.exts, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
}
...
tcf_exts_change(tp, &cr.exts, &e);
...
memcpy(r, &cr, sizeof(cr));
the above code should equal to:
tcindex_filter_result_init(&cr);
if (old_r)
cr.res = r->res;
...
if (old_r)
tcf_exts_change(tp, &r->exts, &e);
else
tcf_exts_change(tp, &cr.exts, &e);
...
r->res = cr.res;
after this change, since there is no need to copy struct tcf_exts.
And it also fixes other places zero'ing struct's contains struct tcf_exts.
Fixes: commit 33be627159913b0 (net_sched: act: use standard struct list_head)
Reported-by: Kelly Anderson <kelly@xilka.com>
Tested-by: Kelly Anderson <kelly@xilka.com>
Cc: David S. Miller <davem@davemloft.net>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-05-19 23:15:49 +04:00
{
memset ( r , 0 , sizeof ( * r ) ) ;
2020-03-28 22:12:59 +03:00
r - > p = p ;
2019-02-21 08:37:42 +03:00
return tcf_exts_init ( & r - > exts , net , TCA_TCINDEX_ACT ,
TCA_TCINDEX_POLICE ) ;
net_sched: fix an oops in tcindex filter
Kelly reported the following crash:
IP: [<ffffffff817a993d>] tcf_action_exec+0x46/0x90
PGD 3009067 PUD 300c067 PMD 11ff30067 PTE 800000011634b060
Oops: 0000 [#1] SMP DEBUG_PAGEALLOC
CPU: 1 PID: 639 Comm: dhclient Not tainted 3.15.0-rc4+ #342
Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
task: ffff8801169ecd00 ti: ffff8800d21b8000 task.ti: ffff8800d21b8000
RIP: 0010:[<ffffffff817a993d>] [<ffffffff817a993d>] tcf_action_exec+0x46/0x90
RSP: 0018:ffff8800d21b9b90 EFLAGS: 00010283
RAX: 00000000ffffffff RBX: ffff88011634b8e8 RCX: ffff8800cf7133d8
RDX: ffff88011634b900 RSI: ffff8800cf7133e0 RDI: ffff8800d210f840
RBP: ffff8800d21b9bb0 R08: ffffffff8287bf60 R09: 0000000000000001
R10: ffff8800d2b22b24 R11: 0000000000000001 R12: ffff8800d210f840
R13: ffff8800d21b9c50 R14: ffff8800cf7133e0 R15: ffff8800cad433d8
FS: 00007f49723e1840(0000) GS:ffff88011a800000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: ffff88011634b8f0 CR3: 00000000ce469000 CR4: 00000000000006e0
Stack:
ffff8800d2170188 ffff8800d210f840 ffff8800d2171b90 0000000000000000
ffff8800d21b9be8 ffffffff817c55bb ffff8800d21b9c50 ffff8800d2171b90
ffff8800d210f840 ffff8800d21b0300 ffff8800d21b9c50 ffff8800d21b9c18
Call Trace:
[<ffffffff817c55bb>] tcindex_classify+0x88/0x9b
[<ffffffff817a7f7d>] tc_classify_compat+0x3e/0x7b
[<ffffffff817a7fdf>] tc_classify+0x25/0x9f
[<ffffffff817b0e68>] htb_enqueue+0x55/0x27a
[<ffffffff817b6c2e>] dsmark_enqueue+0x165/0x1a4
[<ffffffff81775642>] __dev_queue_xmit+0x35e/0x536
[<ffffffff8177582a>] dev_queue_xmit+0x10/0x12
[<ffffffff818f8ecd>] packet_sendmsg+0xb26/0xb9a
[<ffffffff810b1507>] ? __lock_acquire+0x3ae/0xdf3
[<ffffffff8175cf08>] __sock_sendmsg_nosec+0x25/0x27
[<ffffffff8175d916>] sock_aio_write+0xd0/0xe7
[<ffffffff8117d6b8>] do_sync_write+0x59/0x78
[<ffffffff8117d84d>] vfs_write+0xb5/0x10a
[<ffffffff8117d96a>] SyS_write+0x49/0x7f
[<ffffffff8198e212>] system_call_fastpath+0x16/0x1b
This is because we memcpy struct tcindex_filter_result which contains
struct tcf_exts, obviously struct list_head can not be simply copied.
This is a regression introduced by commit 33be627159913b094bb578
(net_sched: act: use standard struct list_head).
It's not very easy to fix it as the code is a mess:
if (old_r)
memcpy(&cr, r, sizeof(cr));
else {
memset(&cr, 0, sizeof(cr));
tcf_exts_init(&cr.exts, TCA_TCINDEX_ACT, TCA_TCINDEX_POLICE);
}
...
tcf_exts_change(tp, &cr.exts, &e);
...
memcpy(r, &cr, sizeof(cr));
the above code should equal to:
tcindex_filter_result_init(&cr);
if (old_r)
cr.res = r->res;
...
if (old_r)
tcf_exts_change(tp, &r->exts, &e);
else
tcf_exts_change(tp, &cr.exts, &e);
...
r->res = cr.res;
after this change, since there is no need to copy struct tcf_exts.
And it also fixes other places zero'ing struct's contains struct tcf_exts.
Fixes: commit 33be627159913b0 (net_sched: act: use standard struct list_head)
Reported-by: Kelly Anderson <kelly@xilka.com>
Tested-by: Kelly Anderson <kelly@xilka.com>
Cc: David S. Miller <davem@davemloft.net>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-05-19 23:15:49 +04:00
}
2021-07-17 14:29:33 +03:00
static void tcindex_free_perfect_hash ( struct tcindex_data * cp ) ;
2019-02-16 21:58:26 +03:00
static void tcindex_partial_destroy_work ( struct work_struct * work )
2014-09-13 07:08:20 +04:00
{
2019-02-16 21:58:26 +03:00
struct tcindex_data * p = container_of ( to_rcu_work ( work ) ,
struct tcindex_data ,
rwork ) ;
2014-09-13 07:08:20 +04:00
2020-03-12 08:42:27 +03:00
rtnl_lock ( ) ;
2021-07-17 14:29:33 +03:00
if ( p - > perfect )
tcindex_free_perfect_hash ( p ) ;
2014-09-13 07:08:20 +04:00
kfree ( p ) ;
2020-03-12 08:42:27 +03:00
rtnl_unlock ( ) ;
2014-09-13 07:08:20 +04:00
}
2016-08-19 22:36:54 +03:00
static void tcindex_free_perfect_hash ( struct tcindex_data * cp )
{
int i ;
for ( i = 0 ; i < cp - > hash ; i + + )
tcf_exts_destroy ( & cp - > perfect [ i ] . exts ) ;
kfree ( cp - > perfect ) ;
}
2019-02-12 00:06:15 +03:00
static int tcindex_alloc_perfect_hash ( struct net * net , struct tcindex_data * cp )
2016-08-19 22:36:54 +03:00
{
int i , err = 0 ;
cp - > perfect = kcalloc ( cp - > hash , sizeof ( struct tcindex_filter_result ) ,
2021-06-25 23:23:48 +03:00
GFP_KERNEL | __GFP_NOWARN ) ;
2016-08-19 22:36:54 +03:00
if ( ! cp - > perfect )
return - ENOMEM ;
for ( i = 0 ; i < cp - > hash ; i + + ) {
2019-02-21 08:37:42 +03:00
err = tcf_exts_init ( & cp - > perfect [ i ] . exts , net ,
2016-08-19 22:36:54 +03:00
TCA_TCINDEX_ACT , TCA_TCINDEX_POLICE ) ;
if ( err < 0 )
goto errout ;
2020-03-28 22:12:59 +03:00
cp - > perfect [ i ] . p = cp ;
2016-08-19 22:36:54 +03:00
}
return 0 ;
errout :
tcindex_free_perfect_hash ( cp ) ;
return err ;
}
2005-04-17 02:20:36 +04:00
static int
2013-01-14 09:15:39 +04:00
tcindex_set_parms ( struct net * net , struct tcf_proto * tp , unsigned long base ,
u32 handle , struct tcindex_data * p ,
struct tcindex_filter_result * r , struct nlattr * * tb ,
2021-07-30 02:12:14 +03:00
struct nlattr * est , u32 flags , struct netlink_ext_ack * extack )
2005-04-17 02:20:36 +04:00
{
struct tcindex_filter_result new_filter_result , * old_r = r ;
2016-08-19 22:36:54 +03:00
struct tcindex_data * cp = NULL , * oldp ;
2005-04-17 02:20:36 +04:00
struct tcindex_filter * f = NULL ; /* make gcc behave */
2019-02-12 00:06:16 +03:00
struct tcf_result cr = { } ;
2016-08-19 22:36:54 +03:00
int err , balloc = 0 ;
2005-04-17 02:20:36 +04:00
struct tcf_exts e ;
2019-02-21 08:37:42 +03:00
err = tcf_exts_init ( & e , net , TCA_TCINDEX_ACT , TCA_TCINDEX_POLICE ) ;
2005-04-17 02:20:36 +04:00
if ( err < 0 )
return err ;
2021-07-30 02:12:14 +03:00
err = tcf_exts_validate ( net , tp , tb , est , & e , flags , extack ) ;
2016-08-19 22:36:54 +03:00
if ( err < 0 )
goto errout ;
2007-02-09 17:25:16 +03:00
2014-09-25 23:06:04 +04:00
err = - ENOMEM ;
2014-09-13 07:08:20 +04:00
/* tcindex_data attributes must look atomic to classifier/lookup so
* allocate new tcindex data and RCU assign it onto root . Keeping
* perfect hash and hash pointers from old data .
*/
2014-09-16 01:06:46 +04:00
cp = kzalloc ( sizeof ( * cp ) , GFP_KERNEL ) ;
2014-09-25 23:06:04 +04:00
if ( ! cp )
2014-09-16 03:43:42 +04:00
goto errout ;
2014-09-13 07:08:20 +04:00
cp - > mask = p - > mask ;
cp - > shift = p - > shift ;
cp - > hash = p - > hash ;
cp - > alloc_hash = p - > alloc_hash ;
cp - > fall_through = p - > fall_through ;
cp - > tp = tp ;
2020-03-28 22:12:59 +03:00
refcount_set ( & cp - > refcnt , 1 ) ; /* Paired with tcindex_destroy_work() */
2014-09-13 07:08:20 +04:00
2020-02-03 08:14:35 +03:00
if ( tb [ TCA_TCINDEX_HASH ] )
cp - > hash = nla_get_u32 ( tb [ TCA_TCINDEX_HASH ] ) ;
if ( tb [ TCA_TCINDEX_MASK ] )
cp - > mask = nla_get_u16 ( tb [ TCA_TCINDEX_MASK ] ) ;
2021-01-14 21:52:29 +03:00
if ( tb [ TCA_TCINDEX_SHIFT ] ) {
2020-02-03 08:14:35 +03:00
cp - > shift = nla_get_u32 ( tb [ TCA_TCINDEX_SHIFT ] ) ;
2021-01-14 21:52:29 +03:00
if ( cp - > shift > 16 ) {
err = - EINVAL ;
goto errout ;
}
}
2020-02-03 08:14:35 +03:00
if ( ! cp - > hash ) {
/* Hash not specified, use perfect hash if the upper limit
* of the hashing index is below the threshold .
*/
if ( ( cp - > mask > > cp - > shift ) < PERFECT_HASH_THRESHOLD )
cp - > hash = ( cp - > mask > > cp - > shift ) + 1 ;
else
cp - > hash = DEFAULT_HASH_SIZE ;
}
2014-09-13 07:08:20 +04:00
if ( p - > perfect ) {
2014-10-01 03:07:23 +04:00
int i ;
2019-02-12 00:06:15 +03:00
if ( tcindex_alloc_perfect_hash ( net , cp ) < 0 )
2014-09-13 07:08:20 +04:00
goto errout ;
2020-03-12 08:42:28 +03:00
cp - > alloc_hash = cp - > hash ;
2020-02-03 08:14:35 +03:00
for ( i = 0 ; i < min ( cp - > hash , p - > hash ) ; i + + )
2016-08-19 22:36:54 +03:00
cp - > perfect [ i ] . res = p - > perfect [ i ] . res ;
2014-09-16 03:43:42 +04:00
balloc = 1 ;
2014-09-13 07:08:20 +04:00
}
cp - > h = p - > h ;
2020-03-28 22:12:59 +03:00
err = tcindex_filter_result_init ( & new_filter_result , cp , net ) ;
2016-08-19 22:36:54 +03:00
if ( err < 0 )
2020-02-04 22:10:12 +03:00
goto errout_alloc ;
2005-04-17 02:20:36 +04:00
if ( old_r )
2019-02-12 00:06:16 +03:00
cr = r - > res ;
2005-04-17 02:20:36 +04:00
err = - EBUSY ;
2014-09-13 07:08:20 +04:00
2005-04-17 02:20:36 +04:00
/* Hash already allocated, make sure that we still meet the
* requirements for the allocated hash .
*/
2014-09-13 07:08:20 +04:00
if ( cp - > perfect ) {
if ( ! valid_perfect_hash ( cp ) | |
cp - > hash > cp - > alloc_hash )
2014-09-16 03:43:42 +04:00
goto errout_alloc ;
2014-09-13 07:08:20 +04:00
} else if ( cp - > h & & cp - > hash ! = cp - > alloc_hash ) {
2014-09-16 03:43:42 +04:00
goto errout_alloc ;
2014-09-13 07:08:20 +04:00
}
2005-04-17 02:20:36 +04:00
err = - EINVAL ;
2008-01-24 07:36:12 +03:00
if ( tb [ TCA_TCINDEX_FALL_THROUGH ] )
2014-09-13 07:08:20 +04:00
cp - > fall_through = nla_get_u32 ( tb [ TCA_TCINDEX_FALL_THROUGH ] ) ;
2005-04-17 02:20:36 +04:00
2014-09-25 23:06:05 +04:00
if ( ! cp - > perfect & & ! cp - > h )
2014-09-13 07:08:20 +04:00
cp - > alloc_hash = cp - > hash ;
2005-04-17 02:20:36 +04:00
/* Note: this could be as restrictive as if (handle & ~(mask >> shift))
* but then , we ' d fail handles that may become valid after some future
* mask change . While this is extremely unlikely to ever matter ,
* the check below is safer ( and also more backwards - compatible ) .
*/
2014-09-13 07:08:20 +04:00
if ( cp - > perfect | | valid_perfect_hash ( cp ) )
if ( handle > = cp - > alloc_hash )
2014-09-16 03:43:42 +04:00
goto errout_alloc ;
2005-04-17 02:20:36 +04:00
err = - ENOMEM ;
2014-09-13 07:08:20 +04:00
if ( ! cp - > perfect & & ! cp - > h ) {
if ( valid_perfect_hash ( cp ) ) {
2019-02-12 00:06:15 +03:00
if ( tcindex_alloc_perfect_hash ( net , cp ) < 0 )
2014-09-16 03:43:42 +04:00
goto errout_alloc ;
2005-04-17 02:20:36 +04:00
balloc = 1 ;
} else {
2014-09-13 07:08:20 +04:00
struct tcindex_filter __rcu * * hash ;
hash = kcalloc ( cp - > hash ,
sizeof ( struct tcindex_filter * ) ,
GFP_KERNEL ) ;
if ( ! hash )
2014-09-16 03:43:42 +04:00
goto errout_alloc ;
2014-09-13 07:08:20 +04:00
cp - > h = hash ;
2005-04-17 02:20:36 +04:00
balloc = 2 ;
}
}
2014-09-13 07:08:20 +04:00
if ( cp - > perfect )
r = cp - > perfect + handle ;
2005-04-17 02:20:36 +04:00
else
2014-09-13 07:08:20 +04:00
r = tcindex_lookup ( cp , handle ) ? : & new_filter_result ;
2005-04-17 02:20:36 +04:00
if ( r = = & new_filter_result ) {
2006-07-22 01:51:30 +04:00
f = kzalloc ( sizeof ( * f ) , GFP_KERNEL ) ;
2005-04-17 02:20:36 +04:00
if ( ! f )
goto errout_alloc ;
2014-10-01 03:07:23 +04:00
f - > key = handle ;
f - > next = NULL ;
2020-03-28 22:12:59 +03:00
err = tcindex_filter_result_init ( & f - > result , cp , net ) ;
2016-08-19 22:36:54 +03:00
if ( err < 0 ) {
kfree ( f ) ;
goto errout_alloc ;
}
2007-02-09 17:25:16 +03:00
}
2005-04-17 02:20:36 +04:00
2008-01-23 09:11:33 +03:00
if ( tb [ TCA_TCINDEX_CLASSID ] ) {
2019-02-12 00:06:16 +03:00
cr . classid = nla_get_u32 ( tb [ TCA_TCINDEX_CLASSID ] ) ;
tcf_bind_filter ( tp , & cr , base ) ;
2007-02-09 17:25:16 +03:00
}
2005-04-17 02:20:36 +04:00
2016-08-19 22:36:54 +03:00
if ( old_r & & old_r ! = r ) {
2020-03-28 22:12:59 +03:00
err = tcindex_filter_result_init ( old_r , cp , net ) ;
2016-08-19 22:36:54 +03:00
if ( err < 0 ) {
kfree ( f ) ;
goto errout_alloc ;
}
}
2005-04-17 02:20:36 +04:00
2014-09-13 07:08:20 +04:00
oldp = p ;
2019-02-12 00:06:16 +03:00
r - > res = cr ;
2018-08-13 13:44:03 +03:00
tcf_exts_change ( & r - > exts , & e ) ;
2014-09-13 07:08:20 +04:00
rcu_assign_pointer ( tp - > root , cp ) ;
2005-04-17 02:20:36 +04:00
if ( r = = & new_filter_result ) {
2014-09-13 07:08:20 +04:00
struct tcindex_filter * nfp ;
struct tcindex_filter __rcu * * fp ;
2005-04-17 02:20:36 +04:00
2018-08-13 13:44:04 +03:00
f - > result . res = r - > res ;
2017-08-04 15:29:15 +03:00
tcf_exts_change ( & f - > result . exts , & r - > exts ) ;
2014-09-13 07:08:20 +04:00
2014-09-16 03:43:43 +04:00
fp = cp - > h + ( handle % cp - > hash ) ;
2014-09-13 07:08:20 +04:00
for ( nfp = rtnl_dereference ( * fp ) ;
nfp ;
fp = & nfp - > next , nfp = rtnl_dereference ( * fp ) )
; /* nothing */
rcu_assign_pointer ( * fp , f ) ;
2019-02-12 00:06:16 +03:00
} else {
tcf_exts_destroy ( & new_filter_result . exts ) ;
2007-02-09 17:25:16 +03:00
}
2005-04-17 02:20:36 +04:00
2014-09-13 07:08:20 +04:00
if ( oldp )
2019-02-16 21:58:26 +03:00
tcf_queue_work ( & oldp - > rwork , tcindex_partial_destroy_work ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
errout_alloc :
if ( balloc = = 1 )
2016-08-19 22:36:54 +03:00
tcindex_free_perfect_hash ( cp ) ;
2005-04-17 02:20:36 +04:00
else if ( balloc = = 2 )
2014-09-13 07:08:20 +04:00
kfree ( cp - > h ) ;
2016-08-19 22:36:54 +03:00
tcf_exts_destroy ( & new_filter_result . exts ) ;
2005-04-17 02:20:36 +04:00
errout :
2014-09-13 07:08:20 +04:00
kfree ( cp ) ;
2014-09-25 21:26:37 +04:00
tcf_exts_destroy ( & e ) ;
2005-04-17 02:20:36 +04:00
return err ;
}
static int
2013-01-14 09:15:39 +04:00
tcindex_change ( struct net * net , struct sk_buff * in_skb ,
2012-05-25 23:42:45 +04:00
struct tcf_proto * tp , unsigned long base , u32 handle ,
2021-07-30 02:12:14 +03:00
struct nlattr * * tca , void * * arg , u32 flags ,
struct netlink_ext_ack * extack )
2005-04-17 02:20:36 +04:00
{
2008-01-23 09:11:33 +03:00
struct nlattr * opt = tca [ TCA_OPTIONS ] ;
struct nlattr * tb [ TCA_TCINDEX_MAX + 1 ] ;
2014-09-13 07:08:20 +04:00
struct tcindex_data * p = rtnl_dereference ( tp - > root ) ;
2017-08-05 07:31:43 +03:00
struct tcindex_filter_result * r = * arg ;
2008-01-24 07:33:32 +03:00
int err ;
2005-04-17 02:20:36 +04:00
2008-01-21 13:26:41 +03:00
pr_debug ( " tcindex_change(tp %p,handle 0x%08x,tca %p,arg %p),opt %p, "
2017-08-05 07:31:43 +03:00
" p %p,r %p,*arg %p \n " ,
2020-06-22 05:24:30 +03:00
tp , handle , tca , arg , opt , p , r , * arg ) ;
2005-04-17 02:20:36 +04:00
if ( ! opt )
return 0 ;
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 15:07:28 +03:00
err = nla_parse_nested_deprecated ( tb , TCA_TCINDEX_MAX , opt ,
tcindex_policy , NULL ) ;
2008-01-24 07:33:32 +03:00
if ( err < 0 )
return err ;
2005-04-17 02:20:36 +04:00
2013-01-14 09:15:39 +04:00
return tcindex_set_parms ( net , tp , base , handle , p , r , tb ,
2021-07-30 02:12:14 +03:00
tca [ TCA_RATE ] , flags , extack ) ;
2005-04-17 02:20:36 +04:00
}
2019-02-11 11:55:45 +03:00
static void tcindex_walk ( struct tcf_proto * tp , struct tcf_walker * walker ,
bool rtnl_held )
2005-04-17 02:20:36 +04:00
{
2014-09-13 07:08:20 +04:00
struct tcindex_data * p = rtnl_dereference ( tp - > root ) ;
2008-01-21 13:26:41 +03:00
struct tcindex_filter * f , * next ;
2005-04-17 02:20:36 +04:00
int i ;
2008-01-21 13:26:41 +03:00
pr_debug ( " tcindex_walk(tp %p,walker %p),p %p \n " , tp , walker , p ) ;
2005-04-17 02:20:36 +04:00
if ( p - > perfect ) {
for ( i = 0 ; i < p - > hash ; i + + ) {
if ( ! p - > perfect [ i ] . res . class )
continue ;
2022-09-16 05:02:44 +03:00
if ( ! tc_cls_stats_dump ( tp , walker , p - > perfect + i ) )
return ;
2005-04-17 02:20:36 +04:00
}
}
if ( ! p - > h )
return ;
for ( i = 0 ; i < p - > hash ; i + + ) {
2014-09-13 07:08:20 +04:00
for ( f = rtnl_dereference ( p - > h [ i ] ) ; f ; f = next ) {
next = rtnl_dereference ( f - > next ) ;
2022-09-16 05:02:44 +03:00
if ( ! tc_cls_stats_dump ( tp , walker , & f - > result ) )
return ;
2005-04-17 02:20:36 +04:00
}
}
}
2019-02-11 11:55:45 +03:00
static void tcindex_destroy ( struct tcf_proto * tp , bool rtnl_held ,
2018-01-24 23:54:13 +03:00
struct netlink_ext_ack * extack )
2005-04-17 02:20:36 +04:00
{
2014-09-13 07:08:20 +04:00
struct tcindex_data * p = rtnl_dereference ( tp - > root ) ;
2019-02-16 21:58:27 +03:00
int i ;
2005-04-17 02:20:36 +04:00
2008-01-21 13:26:41 +03:00
pr_debug ( " tcindex_destroy(tp %p),p %p \n " , tp , p ) ;
2019-02-16 21:58:27 +03:00
if ( p - > perfect ) {
for ( i = 0 ; i < p - > hash ; i + + ) {
struct tcindex_filter_result * r = p - > perfect + i ;
2020-03-28 22:12:59 +03:00
/* tcf_queue_work() does not guarantee the ordering we
* want , so we have to take this refcnt temporarily to
* ensure ' p ' is freed after all tcindex_filter_result
* here . Imperfect hash does not need this , because it
* uses linked lists rather than an array .
*/
tcindex_data_get ( p ) ;
2019-02-16 21:58:27 +03:00
tcf_unbind_filter ( tp , & r - > res ) ;
if ( tcf_exts_get_net ( & r - > exts ) )
tcf_queue_work ( & r - > rwork ,
tcindex_destroy_rexts_work ) ;
else
__tcindex_destroy_rexts ( r ) ;
}
}
for ( i = 0 ; p - > h & & i < p - > hash ; i + + ) {
struct tcindex_filter * f , * next ;
bool last ;
for ( f = rtnl_dereference ( p - > h [ i ] ) ; f ; f = next ) {
next = rtnl_dereference ( f - > next ) ;
tcindex_delete ( tp , & f - > result , & last , rtnl_held , NULL ) ;
}
}
2014-09-13 07:08:20 +04:00
2019-02-16 21:58:26 +03:00
tcf_queue_work ( & p - > rwork , tcindex_destroy_work ) ;
2005-04-17 02:20:36 +04:00
}
2017-08-05 07:31:43 +03:00
static int tcindex_dump ( struct net * net , struct tcf_proto * tp , void * fh ,
2019-02-11 11:55:45 +03:00
struct sk_buff * skb , struct tcmsg * t , bool rtnl_held )
2005-04-17 02:20:36 +04:00
{
2014-09-13 07:08:20 +04:00
struct tcindex_data * p = rtnl_dereference ( tp - > root ) ;
2017-08-05 07:31:43 +03:00
struct tcindex_filter_result * r = fh ;
2008-01-24 07:34:11 +03:00
struct nlattr * nest ;
2005-04-17 02:20:36 +04:00
2017-08-05 07:31:43 +03:00
pr_debug ( " tcindex_dump(tp %p,fh %p,skb %p,t %p),p %p,r %p \n " ,
2014-12-10 00:23:29 +03:00
tp , fh , skb , t , p , r ) ;
2008-01-21 13:26:41 +03:00
pr_debug ( " p->perfect %p p->h %p \n " , p - > perfect , p - > h ) ;
2008-01-24 07:34:11 +03:00
2019-04-26 12:13:06 +03:00
nest = nla_nest_start_noflag ( skb , TCA_OPTIONS ) ;
2008-01-24 07:34:11 +03:00
if ( nest = = NULL )
goto nla_put_failure ;
2005-04-17 02:20:36 +04:00
if ( ! fh ) {
t - > tcm_handle = ~ 0 ; /* whatever ... */
2012-03-29 13:11:39 +04:00
if ( nla_put_u32 ( skb , TCA_TCINDEX_HASH , p - > hash ) | |
nla_put_u16 ( skb , TCA_TCINDEX_MASK , p - > mask ) | |
nla_put_u32 ( skb , TCA_TCINDEX_SHIFT , p - > shift ) | |
nla_put_u32 ( skb , TCA_TCINDEX_FALL_THROUGH , p - > fall_through ) )
goto nla_put_failure ;
2008-01-24 07:34:11 +03:00
nla_nest_end ( skb , nest ) ;
2005-04-17 02:20:36 +04:00
} else {
if ( p - > perfect ) {
2014-09-13 07:08:20 +04:00
t - > tcm_handle = r - p - > perfect ;
2005-04-17 02:20:36 +04:00
} else {
struct tcindex_filter * f ;
2014-09-13 07:08:20 +04:00
struct tcindex_filter __rcu * * fp ;
2005-04-17 02:20:36 +04:00
int i ;
t - > tcm_handle = 0 ;
for ( i = 0 ; ! t - > tcm_handle & & i < p - > hash ; i + + ) {
2014-09-13 07:08:20 +04:00
fp = & p - > h [ i ] ;
for ( f = rtnl_dereference ( * fp ) ;
! t - > tcm_handle & & f ;
fp = & f - > next , f = rtnl_dereference ( * fp ) ) {
2005-04-17 02:20:36 +04:00
if ( & f - > result = = r )
t - > tcm_handle = f - > key ;
}
}
}
2008-01-21 13:26:41 +03:00
pr_debug ( " handle = %d \n " , t - > tcm_handle ) ;
2012-03-29 13:11:39 +04:00
if ( r - > res . class & &
nla_put_u32 ( skb , TCA_TCINDEX_CLASSID , r - > res . classid ) )
goto nla_put_failure ;
2005-04-17 02:20:36 +04:00
2013-12-16 08:15:07 +04:00
if ( tcf_exts_dump ( skb , & r - > exts ) < 0 )
2008-01-23 09:11:33 +03:00
goto nla_put_failure ;
2008-01-24 07:34:11 +03:00
nla_nest_end ( skb , nest ) ;
2005-04-17 02:20:36 +04:00
2013-12-16 08:15:07 +04:00
if ( tcf_exts_dump_stats ( skb , & r - > exts ) < 0 )
2008-01-23 09:11:33 +03:00
goto nla_put_failure ;
2005-04-17 02:20:36 +04:00
}
2007-02-09 17:25:16 +03:00
2005-04-17 02:20:36 +04:00
return skb - > len ;
2008-01-23 09:11:33 +03:00
nla_put_failure :
2014-12-10 00:23:29 +03:00
nla_nest_cancel ( skb , nest ) ;
2005-04-17 02:20:36 +04:00
return - 1 ;
}
2020-01-24 03:26:18 +03:00
static void tcindex_bind_class ( void * fh , u32 classid , unsigned long cl ,
void * q , unsigned long base )
net_sched: add reverse binding for tc class
TC filters when used as classifiers are bound to TC classes.
However, there is a hidden difference when adding them in different
orders:
1. If we add tc classes before its filters, everything is fine.
Logically, the classes exist before we specify their ID's in
filters, it is easy to bind them together, just as in the current
code base.
2. If we add tc filters before the tc classes they bind, we have to
do dynamic lookup in fast path. What's worse, this happens all
the time not just once, because on fast path tcf_result is passed
on stack, there is no way to propagate back to the one in tc filters.
This hidden difference hurts performance silently if we have many tc
classes in hierarchy.
This patch intends to close this gap by doing the reverse binding when
we create a new class, in this case we can actually search all the
filters in its parent, match and fixup by classid. And because
tcf_result is specific to each type of tc filter, we have to introduce
a new ops for each filter to tell how to bind the class.
Note, we still can NOT totally get rid of those class lookup in
->enqueue() because cgroup and flow filters have no way to determine
the classid at setup time, they still have to go through dynamic lookup.
Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-08-31 00:30:36 +03:00
{
struct tcindex_filter_result * r = fh ;
2022-09-27 15:48:55 +03:00
tc_cls_bind_class ( classid , cl , q , & r - > res , base ) ;
net_sched: add reverse binding for tc class
TC filters when used as classifiers are bound to TC classes.
However, there is a hidden difference when adding them in different
orders:
1. If we add tc classes before its filters, everything is fine.
Logically, the classes exist before we specify their ID's in
filters, it is easy to bind them together, just as in the current
code base.
2. If we add tc filters before the tc classes they bind, we have to
do dynamic lookup in fast path. What's worse, this happens all
the time not just once, because on fast path tcf_result is passed
on stack, there is no way to propagate back to the one in tc filters.
This hidden difference hurts performance silently if we have many tc
classes in hierarchy.
This patch intends to close this gap by doing the reverse binding when
we create a new class, in this case we can actually search all the
filters in its parent, match and fixup by classid. And because
tcf_result is specific to each type of tc filter, we have to introduce
a new ops for each filter to tell how to bind the class.
Note, we still can NOT totally get rid of those class lookup in
->enqueue() because cgroup and flow filters have no way to determine
the classid at setup time, they still have to go through dynamic lookup.
Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-08-31 00:30:36 +03:00
}
2008-01-23 09:10:42 +03:00
static struct tcf_proto_ops cls_tcindex_ops __read_mostly = {
2005-04-17 02:20:36 +04:00
. kind = " tcindex " ,
. classify = tcindex_classify ,
. init = tcindex_init ,
. destroy = tcindex_destroy ,
. get = tcindex_get ,
. change = tcindex_change ,
. delete = tcindex_delete ,
. walk = tcindex_walk ,
. dump = tcindex_dump ,
net_sched: add reverse binding for tc class
TC filters when used as classifiers are bound to TC classes.
However, there is a hidden difference when adding them in different
orders:
1. If we add tc classes before its filters, everything is fine.
Logically, the classes exist before we specify their ID's in
filters, it is easy to bind them together, just as in the current
code base.
2. If we add tc filters before the tc classes they bind, we have to
do dynamic lookup in fast path. What's worse, this happens all
the time not just once, because on fast path tcf_result is passed
on stack, there is no way to propagate back to the one in tc filters.
This hidden difference hurts performance silently if we have many tc
classes in hierarchy.
This patch intends to close this gap by doing the reverse binding when
we create a new class, in this case we can actually search all the
filters in its parent, match and fixup by classid. And because
tcf_result is specific to each type of tc filter, we have to introduce
a new ops for each filter to tell how to bind the class.
Note, we still can NOT totally get rid of those class lookup in
->enqueue() because cgroup and flow filters have no way to determine
the classid at setup time, they still have to go through dynamic lookup.
Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-08-31 00:30:36 +03:00
. bind_class = tcindex_bind_class ,
2005-04-17 02:20:36 +04:00
. owner = THIS_MODULE ,
} ;
static int __init init_tcindex ( void )
{
return register_tcf_proto_ops ( & cls_tcindex_ops ) ;
}
2007-02-09 17:25:16 +03:00
static void __exit exit_tcindex ( void )
2005-04-17 02:20:36 +04:00
{
unregister_tcf_proto_ops ( & cls_tcindex_ops ) ;
}
module_init ( init_tcindex )
module_exit ( exit_tcindex )
MODULE_LICENSE ( " GPL " ) ;