diff mbox series

[net-next,09/12] nexthop: Strongly-type context of rtm_dump_nexthop()

Message ID 540e18781f82a0f0b0482b10e7180a57369e8388.1611836479.git.petrm@nvidia.com
State New
Headers show
Series nexthop: Preparations for resilient next-hop groups | expand

Commit Message

Petr Machata Jan. 28, 2021, 12:49 p.m. UTC
The dump operations need to keep state from one invocation to another. A
scratch area is dedicated for this purpose in the passed-in argument, cb,
namely via two aliased arrays, struct netlink_callback.args and .ctx.

Dumping of buckets will end up having to iterate over next hops as well,
and it would be nice to be able to reuse the iteration logic with the NH
dumper. The fact that the logic currently relies on fixed index to the
.args array, and the indices would have to be coordinated between the two
dumpers, makes this somewhat awkward.

To make the access patters clearer, introduce a helper struct with a NH
index, and instead of using the .args array directly, use it through this
structure.

Signed-off-by: Petr Machata <petrm@nvidia.com>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
---
 net/ipv4/nexthop.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

Comments

David Ahern Jan. 29, 2021, 3:18 a.m. UTC | #1
On 1/28/21 5:49 AM, Petr Machata wrote:
> The dump operations need to keep state from one invocation to another. A

> scratch area is dedicated for this purpose in the passed-in argument, cb,

> namely via two aliased arrays, struct netlink_callback.args and .ctx.

> 

> Dumping of buckets will end up having to iterate over next hops as well,

> and it would be nice to be able to reuse the iteration logic with the NH

> dumper. The fact that the logic currently relies on fixed index to the

> .args array, and the indices would have to be coordinated between the two

> dumpers, makes this somewhat awkward.

> 

> To make the access patters clearer, introduce a helper struct with a NH

> index, and instead of using the .args array directly, use it through this

> structure.

> 

> Signed-off-by: Petr Machata <petrm@nvidia.com>

> Reviewed-by: Ido Schimmel <idosch@nvidia.com>

> ---

>  net/ipv4/nexthop.c | 18 ++++++++++++++++--

>  1 file changed, 16 insertions(+), 2 deletions(-)

> 


Reviewed-by: David Ahern <dsahern@kernel.org>
diff mbox series

Patch

diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
index 1c4f10fe3b4e..7ae197efa5a9 100644
--- a/net/ipv4/nexthop.c
+++ b/net/ipv4/nexthop.c
@@ -2066,9 +2066,23 @@  static int nh_valid_dump_req(const struct nlmsghdr *nlh,
 	return __nh_valid_dump_req(nlh, tb, filter, cb->extack);
 }
 
+struct rtm_dump_nh_ctx {
+	u32 idx;
+};
+
+static struct rtm_dump_nh_ctx *
+rtm_dump_nh_ctx(struct netlink_callback *cb)
+{
+	struct rtm_dump_nh_ctx *ctx = (void *)cb->ctx;
+
+	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
+	return ctx;
+}
+
 /* rtnl */
 static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
 {
+	struct rtm_dump_nh_ctx *ctx = rtm_dump_nh_ctx(cb);
 	struct nhmsg *nhm = nlmsg_data(cb->nlh);
 	struct net *net = sock_net(skb->sk);
 	struct rb_root *root = &net->nexthop.rb_root;
@@ -2081,7 +2095,7 @@  static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
 	if (err < 0)
 		return err;
 
-	s_idx = cb->args[0];
+	s_idx = ctx->idx;
 	for (node = rb_first(root); node; node = rb_next(node)) {
 		struct nexthop *nh;
 
@@ -2108,7 +2122,7 @@  static int rtm_dump_nexthop(struct sk_buff *skb, struct netlink_callback *cb)
 out:
 	err = skb->len;
 out_err:
-	cb->args[0] = idx;
+	ctx->idx = idx;
 	cb->seq = net->nexthop.seq;
 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));