diff mbox series

[net-next] fib: rules: use memcmp to simplify code in rule_exists

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

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1083 this patch: 1083
netdev/cc_maintainers success CCed 0 of 0 maintainers
netdev/build_clang success Errors and warnings before: 1109 this patch: 1109
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1110 this patch: 1110
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 35 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

shaozhengchao Jan. 8, 2024, 1:11 a.m. UTC
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(-)

Comments

David Ahern Jan. 8, 2024, 1:24 a.m. UTC | #1
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.
Eric Dumazet Jan. 8, 2024, 9:20 a.m. UTC | #2
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 mbox series

Patch

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;