[PATCH net-next 3/8] net: psample: add user cookie

From: Adrian Moreno
Date: Wed Apr 24 2024 - 09:55:45 EST


Add a user cookie to the sample metadata so that sample emitters can
provide more contextual information to samples.

If present, send the user cookie in a new attribute:
PSAMPLE_ATTR_USER_COOKIE.

Signed-off-by: Adrian Moreno <amorenoz@xxxxxxxxxx>
---
include/net/psample.h | 2 ++
include/uapi/linux/psample.h | 1 +
net/psample/psample.c | 12 +++++++++++-
3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/include/net/psample.h b/include/net/psample.h
index 0509d2d6be67..2ac71260a546 100644
--- a/include/net/psample.h
+++ b/include/net/psample.h
@@ -25,6 +25,8 @@ struct psample_metadata {
out_tc_occ_valid:1,
latency_valid:1,
unused:5;
+ const u8 *user_cookie;
+ u32 user_cookie_len;
};

struct psample_group *psample_group_get(struct net *net, u32 group_num);
diff --git a/include/uapi/linux/psample.h b/include/uapi/linux/psample.h
index 9d62983af0a4..83dc919c4023 100644
--- a/include/uapi/linux/psample.h
+++ b/include/uapi/linux/psample.h
@@ -19,6 +19,7 @@ enum {
PSAMPLE_ATTR_LATENCY, /* u64, nanoseconds */
PSAMPLE_ATTR_TIMESTAMP, /* u64, nanoseconds */
PSAMPLE_ATTR_PROTO, /* u16 */
+ PSAMPLE_ATTR_USER_COOKIE, /* binary, user provided data */

__PSAMPLE_ATTR_MAX
};
diff --git a/net/psample/psample.c b/net/psample/psample.c
index f5f77515b969..476aaad7a885 100644
--- a/net/psample/psample.c
+++ b/net/psample/psample.c
@@ -480,7 +480,8 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
nla_total_size(sizeof(u32)) + /* group_num */
nla_total_size(sizeof(u32)) + /* seq */
nla_total_size_64bit(sizeof(u64)) + /* timestamp */
- nla_total_size(sizeof(u16)); /* protocol */
+ nla_total_size(sizeof(u16)) + /* protocol */
+ nla_total_size(md->user_cookie_len); /* user_cookie */

#ifdef CONFIG_INET
tun_info = skb_tunnel_info(skb);
@@ -579,6 +580,15 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
goto error;
}
#endif
+ if (md->user_cookie && md->user_cookie_len) {
+ int nla_len = nla_total_size(md->user_cookie_len);
+ struct nlattr *nla;
+
+ nla = skb_put(nl_skb, nla_len);
+ nla->nla_type = PSAMPLE_ATTR_USER_COOKIE;
+ nla->nla_len = nla_attr_size(md->user_cookie_len);
+ memcpy(nla_data(nla), md->user_cookie, md->user_cookie_len);
+ }

genlmsg_end(nl_skb, data);
psample_nl_obj_desc_init(&desc, group->group_num);
--
2.44.0