Message ID | 20240108011131.83295-1-shaozhengchao@huawei.com (mailing list archive) |
---|---|
State | Rejected |
Delegated to: | Netdev Maintainers |
Headers | show |
Series | [net-next] fib: rules: use memcmp to simplify code in rule_exists | expand |
On 1/7/24 6:11 PM, Zhengchao Shao wrote: > In the fib_rule structure, the member variables 'pref' to 'oifname' are > consecutive. In addition, the newly generated rule uses kzalloc to > allocate memory, and all allocated memory is initialized to 0. Therefore, > the comparison of two fib_rule structures from 'pref' to 'oifname' can be > simplified into the comparison of continuous memory. > > Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com> > --- > net/core/fib_rules.c | 18 +++++------------- > 1 file changed, 5 insertions(+), 13 deletions(-) > This is control path, and I think the readability of the existing code is better.
On Mon, Jan 8, 2024 at 2:01 AM Zhengchao Shao <shaozhengchao@huawei.com> wrote: > > In the fib_rule structure, the member variables 'pref' to 'oifname' are > consecutive. This could be changed in the future. This is error prone for no gain (as David said, this is control path) In addition, the newly generated rule uses kzalloc to > allocate memory, and all allocated memory is initialized to 0. Therefore, > the comparison of two fib_rule structures from 'pref' to 'oifname' can be > simplified into the comparison of continuous memory. >
diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c index a50a0bde8db1..e6def052e7d3 100644 --- a/net/core/fib_rules.c +++ b/net/core/fib_rules.c @@ -686,6 +686,10 @@ static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh, struct nlattr **tb, struct fib_rule *rule) { struct fib_rule *r; + size_t off_len; + + off_len = offsetof(struct fib_rule, uid_range) - + offsetof(struct fib_rule, pref); list_for_each_entry(r, &ops->rules_list, list) { if (r->action != rule->action) @@ -694,24 +698,12 @@ static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh, if (r->table != rule->table) continue; - if (r->pref != rule->pref) - continue; - - if (memcmp(r->iifname, rule->iifname, IFNAMSIZ)) - continue; - - if (memcmp(r->oifname, rule->oifname, IFNAMSIZ)) + if (memcmp(&r->pref, &rule->pref, off_len)) continue; if (r->mark != rule->mark) continue; - if (r->suppress_ifgroup != rule->suppress_ifgroup) - continue; - - if (r->suppress_prefixlen != rule->suppress_prefixlen) - continue; - if (r->mark_mask != rule->mark_mask) continue;
In the fib_rule structure, the member variables 'pref' to 'oifname' are consecutive. In addition, the newly generated rule uses kzalloc to allocate memory, and all allocated memory is initialized to 0. Therefore, the comparison of two fib_rule structures from 'pref' to 'oifname' can be simplified into the comparison of continuous memory. Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com> --- net/core/fib_rules.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)