Skip to content

Commit 7b1b2b6

Browse files
amorenozkuba-moo
authored andcommitted
net: psample: allow using rate as probability
Although not explicitly documented in the psample module itself, the definition of PSAMPLE_ATTR_SAMPLE_RATE seems inherited from act_sample. Quoting tc-sample(8): "RATE of 100 will lead to an average of one sampled packet out of every 100 observed." With this semantics, the rates that we can express with an unsigned 32-bits number are very unevenly distributed and concentrated towards "sampling few packets". For example, we can express a probability of 2.32E-8% but we cannot express anything between 100% and 50%. For sampling applications that are capable of sampling a decent amount of packets, this sampling rate semantics is not very useful. Add a new flag to the uAPI that indicates that the sampling rate is expressed in scaled probability, this is: - 0 is 0% probability, no packets get sampled. - U32_MAX is 100% probability, all packets get sampled. Reviewed-by: Aaron Conole <[email protected]> Acked-by: Eelco Chaudron <[email protected]> Reviewed-by: Ido Schimmel <[email protected]> Signed-off-by: Adrian Moreno <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent c35d86a commit 7b1b2b6

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

include/net/psample.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ struct psample_metadata {
2424
u8 out_tc_valid:1,
2525
out_tc_occ_valid:1,
2626
latency_valid:1,
27-
unused:5;
27+
rate_as_probability:1,
28+
unused:4;
2829
const u8 *user_cookie;
2930
u32 user_cookie_len;
3031
};

include/uapi/linux/psample.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ enum {
88
PSAMPLE_ATTR_ORIGSIZE,
99
PSAMPLE_ATTR_SAMPLE_GROUP,
1010
PSAMPLE_ATTR_GROUP_SEQ,
11-
PSAMPLE_ATTR_SAMPLE_RATE,
11+
PSAMPLE_ATTR_SAMPLE_RATE, /* u32, ratio between observed and
12+
* sampled packets or scaled probability
13+
* if PSAMPLE_ATTR_SAMPLE_PROBABILITY
14+
* is set.
15+
*/
1216
PSAMPLE_ATTR_DATA,
1317
PSAMPLE_ATTR_GROUP_REFCOUNT,
1418
PSAMPLE_ATTR_TUNNEL,
@@ -20,6 +24,10 @@ enum {
2024
PSAMPLE_ATTR_TIMESTAMP, /* u64, nanoseconds */
2125
PSAMPLE_ATTR_PROTO, /* u16 */
2226
PSAMPLE_ATTR_USER_COOKIE, /* binary, user provided data */
27+
PSAMPLE_ATTR_SAMPLE_PROBABILITY,/* no argument, interpret rate in
28+
* PSAMPLE_ATTR_SAMPLE_RATE as a
29+
* probability scaled 0 - U32_MAX.
30+
*/
2331

2432
__PSAMPLE_ATTR_MAX
2533
};

net/psample/psample.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
497497
md->user_cookie))
498498
goto error;
499499

500+
if (md->rate_as_probability)
501+
nla_put_flag(skb, PSAMPLE_ATTR_SAMPLE_PROBABILITY);
502+
500503
genlmsg_end(nl_skb, data);
501504
genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0,
502505
PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC);

0 commit comments

Comments
 (0)