* [PATCH v2 net-next 1/3] netem: add prng attribute to netem_sched_data
2023-08-15 9:23 [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events Francois Michel
@ 2023-08-15 9:23 ` Francois Michel
2023-08-15 9:23 ` [PATCH v2 net-next 2/3] netem: use a seeded PRNG for generating random losses Francois Michel
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Francois Michel @ 2023-08-15 9:23 UTC (permalink / raw)
Cc: Francois Michel, Jamal Hadi Salim, Cong Wang, Jiri Pirko,
Stephen Hemminger, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, netdev, linux-kernel
From: François Michel <francois.michel@uclouvain.be>
Add prng attribute to struct netem_sched_data and
allows setting the seed of the PRNG through netlink
using the new TCA_NETEM_PRNG_SEED attribute.
The PRNG attribute is not actually used yet.
Signed-off-by: François Michel <francois.michel@uclouvain.be>
---
include/uapi/linux/pkt_sched.h | 1 +
net/sched/sch_netem.c | 16 ++++++++++++++++
2 files changed, 17 insertions(+)
diff --git a/include/uapi/linux/pkt_sched.h b/include/uapi/linux/pkt_sched.h
index 00f6ff0aff1f..3f85ae578056 100644
--- a/include/uapi/linux/pkt_sched.h
+++ b/include/uapi/linux/pkt_sched.h
@@ -603,6 +603,7 @@ enum {
TCA_NETEM_JITTER64,
TCA_NETEM_SLOT,
TCA_NETEM_SLOT_DIST,
+ TCA_NETEM_PRNG_SEED,
__TCA_NETEM_MAX,
};
diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
index 38d9aa0cd30e..621c6acfd644 100644
--- a/net/sched/sch_netem.c
+++ b/net/sched/sch_netem.c
@@ -105,6 +105,11 @@ struct netem_sched_data {
u32 rho;
} delay_cor, loss_cor, dup_cor, reorder_cor, corrupt_cor;
+ struct prng {
+ u64 seed;
+ struct rnd_state prng_state;
+ } prng;
+
struct disttable *delay_dist;
enum {
@@ -922,6 +927,7 @@ static const struct nla_policy netem_policy[TCA_NETEM_MAX + 1] = {
[TCA_NETEM_LATENCY64] = { .type = NLA_S64 },
[TCA_NETEM_JITTER64] = { .type = NLA_S64 },
[TCA_NETEM_SLOT] = { .len = sizeof(struct tc_netem_slot) },
+ [TCA_NETEM_PRNG_SEED] = { .type = NLA_U64 },
};
static int parse_attr(struct nlattr *tb[], int maxtype, struct nlattr *nla,
@@ -1040,6 +1046,12 @@ static int netem_change(struct Qdisc *sch, struct nlattr *opt,
/* capping jitter to the range acceptable by tabledist() */
q->jitter = min_t(s64, abs(q->jitter), INT_MAX);
+ if (tb[TCA_NETEM_PRNG_SEED])
+ q->prng.seed = nla_get_u64(tb[TCA_NETEM_PRNG_SEED]);
+ else
+ q->prng.seed = get_random_u64();
+ prandom_seed_state(&q->prng.prng_state, q->prng.seed);
+
unlock:
sch_tree_unlock(sch);
@@ -1203,6 +1215,10 @@ static int netem_dump(struct Qdisc *sch, struct sk_buff *skb)
goto nla_put_failure;
}
+ if (nla_put_u64_64bit(skb, TCA_NETEM_PRNG_SEED, q->prng.seed,
+ TCA_NETEM_PAD))
+ goto nla_put_failure;
+
return nla_nest_end(skb, nla);
nla_put_failure:
--
2.41.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 net-next 2/3] netem: use a seeded PRNG for generating random losses
2023-08-15 9:23 [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events Francois Michel
2023-08-15 9:23 ` [PATCH v2 net-next 1/3] netem: add prng attribute to netem_sched_data Francois Michel
@ 2023-08-15 9:23 ` Francois Michel
2023-08-15 9:23 ` [PATCH v2 net-next 3/3] netem: use seeded PRNG for correlated loss events Francois Michel
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Francois Michel @ 2023-08-15 9:23 UTC (permalink / raw)
Cc: Francois Michel, Jamal Hadi Salim, Cong Wang, Jiri Pirko,
Stephen Hemminger, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, netdev, linux-kernel
From: François Michel <francois.michel@uclouvain.be>
Use prandom_u32_state() instead of get_random_u32() to generate
the random loss events of netem. The state of the prng is part
of the prng attribute of struct netem_sched_data.
Signed-off-by: François Michel <francois.michel@uclouvain.be>
---
net/sched/sch_netem.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
index 621c6acfd644..8b54b1005a10 100644
--- a/net/sched/sch_netem.c
+++ b/net/sched/sch_netem.c
@@ -206,7 +206,7 @@ static u32 get_crandom(struct crndstate *state)
static bool loss_4state(struct netem_sched_data *q)
{
struct clgstate *clg = &q->clg;
- u32 rnd = get_random_u32();
+ u32 rnd = prandom_u32_state(&q->prng.prng_state);
/*
* Makes a comparison between rnd and the transition
@@ -271,18 +271,19 @@ static bool loss_4state(struct netem_sched_data *q)
static bool loss_gilb_ell(struct netem_sched_data *q)
{
struct clgstate *clg = &q->clg;
+ struct rnd_state *s = &q->prng.prng_state;
switch (clg->state) {
case GOOD_STATE:
- if (get_random_u32() < clg->a1)
+ if (prandom_u32_state(s) < clg->a1)
clg->state = BAD_STATE;
- if (get_random_u32() < clg->a4)
+ if (prandom_u32_state(s) < clg->a4)
return true;
break;
case BAD_STATE:
- if (get_random_u32() < clg->a2)
+ if (prandom_u32_state(s) < clg->a2)
clg->state = GOOD_STATE;
- if (get_random_u32() > clg->a3)
+ if (prandom_u32_state(s) > clg->a3)
return true;
}
--
2.41.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v2 net-next 3/3] netem: use seeded PRNG for correlated loss events
2023-08-15 9:23 [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events Francois Michel
2023-08-15 9:23 ` [PATCH v2 net-next 1/3] netem: add prng attribute to netem_sched_data Francois Michel
2023-08-15 9:23 ` [PATCH v2 net-next 2/3] netem: use a seeded PRNG for generating random losses Francois Michel
@ 2023-08-15 9:23 ` Francois Michel
2023-08-16 9:36 ` [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events Simon Horman
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Francois Michel @ 2023-08-15 9:23 UTC (permalink / raw)
Cc: Francois Michel, Jamal Hadi Salim, Cong Wang, Jiri Pirko,
Stephen Hemminger, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, netdev, linux-kernel
From: François Michel <francois.michel@uclouvain.be>
Use prandom_u32_state() instead of get_random_u32() to generate
the correlated loss events of netem.
Signed-off-by: François Michel <francois.michel@uclouvain.be>
---
net/sched/sch_netem.c | 22 ++++++++++++----------
1 file changed, 12 insertions(+), 10 deletions(-)
diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
index 8b54b1005a10..4ad39a4a3cf5 100644
--- a/net/sched/sch_netem.c
+++ b/net/sched/sch_netem.c
@@ -184,15 +184,16 @@ static void init_crandom(struct crndstate *state, unsigned long rho)
* Next number depends on last value.
* rho is scaled to avoid floating point.
*/
-static u32 get_crandom(struct crndstate *state)
+static u32 get_crandom(struct crndstate *state, struct prng *p)
{
u64 value, rho;
unsigned long answer;
+ struct rnd_state *s = &p->prng_state;
if (!state || state->rho == 0) /* no correlation */
- return get_random_u32();
+ return prandom_u32_state(s);
- value = get_random_u32();
+ value = prandom_u32_state(s);
rho = (u64)state->rho + 1;
answer = (value * ((1ull<<32) - rho) + state->last * rho) >> 32;
state->last = answer;
@@ -295,7 +296,7 @@ static bool loss_event(struct netem_sched_data *q)
switch (q->loss_model) {
case CLG_RANDOM:
/* Random packet drop 0 => none, ~0 => all */
- return q->loss && q->loss >= get_crandom(&q->loss_cor);
+ return q->loss && q->loss >= get_crandom(&q->loss_cor, &q->prng);
case CLG_4_STATES:
/* 4state loss model algorithm (used also for GI model)
@@ -324,6 +325,7 @@ static bool loss_event(struct netem_sched_data *q)
*/
static s64 tabledist(s64 mu, s32 sigma,
struct crndstate *state,
+ struct prng *prng,
const struct disttable *dist)
{
s64 x;
@@ -333,7 +335,7 @@ static s64 tabledist(s64 mu, s32 sigma,
if (sigma == 0)
return mu;
- rnd = get_crandom(state);
+ rnd = get_crandom(state, prng);
/* default uniform distribution */
if (dist == NULL)
@@ -455,7 +457,7 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
skb->prev = NULL;
/* Random duplication */
- if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor))
+ if (q->duplicate && q->duplicate >= get_crandom(&q->dup_cor, &q->prng))
++count;
/* Drop packet? */
@@ -498,7 +500,7 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
* If packet is going to be hardware checksummed, then
* do it now in software before we mangle it.
*/
- if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor)) {
+ if (q->corrupt && q->corrupt >= get_crandom(&q->corrupt_cor, &q->prng)) {
if (skb_is_gso(skb)) {
skb = netem_segment(skb, sch, to_free);
if (!skb)
@@ -536,12 +538,12 @@ static int netem_enqueue(struct sk_buff *skb, struct Qdisc *sch,
cb = netem_skb_cb(skb);
if (q->gap == 0 || /* not doing reordering */
q->counter < q->gap - 1 || /* inside last reordering gap */
- q->reorder < get_crandom(&q->reorder_cor)) {
+ q->reorder < get_crandom(&q->reorder_cor, &q->prng)) {
u64 now;
s64 delay;
delay = tabledist(q->latency, q->jitter,
- &q->delay_cor, q->delay_dist);
+ &q->delay_cor, &q->prng, q->delay_dist);
now = ktime_get_ns();
@@ -645,7 +647,7 @@ static void get_slot_next(struct netem_sched_data *q, u64 now)
else
next_delay = tabledist(q->slot_config.dist_delay,
(s32)(q->slot_config.dist_jitter),
- NULL, q->slot_dist);
+ NULL, &q->prng, q->slot_dist);
q->slot.slot_next = now + next_delay;
q->slot.packets_left = q->slot_config.max_packets;
--
2.41.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events
2023-08-15 9:23 [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events Francois Michel
` (2 preceding siblings ...)
2023-08-15 9:23 ` [PATCH v2 net-next 3/3] netem: use seeded PRNG for correlated loss events Francois Michel
@ 2023-08-16 9:36 ` Simon Horman
2023-08-16 15:02 ` Stephen Hemminger
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Simon Horman @ 2023-08-16 9:36 UTC (permalink / raw)
To: Francois Michel
Cc: Jamal Hadi Salim, Cong Wang, Jiri Pirko, Stephen Hemminger,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
netdev, linux-kernel
On Tue, Aug 15, 2023 at 11:23:37AM +0200, Francois Michel wrote:
> From: François Michel <francois.michel@uclouvain.be>
>
> In order to reproduce bugs or performance evaluation of
> network protocols and applications, it is useful to have
> reproducible test suites and tools. This patch adds
> a way to specify a PRNG seed through the
> TCA_NETEM_PRNG_SEED attribute for generating netem
> loss and corruption events. Initializing the qdisc
> with the same seed leads to the exact same loss
> and corruption patterns. If no seed is explicitly
> specified, the qdisc generates a random seed using
> get_random_u64().
>
> This patch can be and has been tested using tc from
> the following iproute2-next fork:
> https://github.com/francoismichel/iproute2-next
>
> For instance, setting the seed 42424242 on the loopback
> with a loss rate of 10% will systematically drop the 5th,
> 12th and 24th packet when sending 25 packets.
>
> v1 -> v2: Address comments and directly use
> prandom_u32_state() instead of get_random_u32() for
> generating loss and corruption events. Generates a random
> seed using get_random_u64() if none was provided explicitly.
>
> François Michel (3):
> netem: add prng attribute to netem_sched_data
> netem: use a seeded PRNG for generating random losses
> netem: use seeded PRNG for correlated loss events
>
> include/uapi/linux/pkt_sched.h | 1 +
> net/sched/sch_netem.c | 49 +++++++++++++++++++++++-----------
> 2 files changed, 35 insertions(+), 15 deletions(-)
For series,
Reviewed-by: Simon Horman <horms@kernel.org>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events
2023-08-15 9:23 [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events Francois Michel
` (3 preceding siblings ...)
2023-08-16 9:36 ` [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events Simon Horman
@ 2023-08-16 15:02 ` Stephen Hemminger
2023-08-18 2:30 ` patchwork-bot+netdevbpf
2023-08-19 0:17 ` Stephen Hemminger
6 siblings, 0 replies; 8+ messages in thread
From: Stephen Hemminger @ 2023-08-16 15:02 UTC (permalink / raw)
To: Francois Michel
Cc: Jamal Hadi Salim, Cong Wang, Jiri Pirko, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-kernel
On Tue, 15 Aug 2023 11:23:37 +0200
Francois Michel <francois.michel@uclouvain.be> wrote:
> From: François Michel <francois.michel@uclouvain.be>
>
> In order to reproduce bugs or performance evaluation of
> network protocols and applications, it is useful to have
> reproducible test suites and tools. This patch adds
> a way to specify a PRNG seed through the
> TCA_NETEM_PRNG_SEED attribute for generating netem
> loss and corruption events. Initializing the qdisc
> with the same seed leads to the exact same loss
> and corruption patterns. If no seed is explicitly
> specified, the qdisc generates a random seed using
> get_random_u64().
>
> This patch can be and has been tested using tc from
> the following iproute2-next fork:
> https://github.com/francoismichel/iproute2-next
>
> For instance, setting the seed 42424242 on the loopback
> with a loss rate of 10% will systematically drop the 5th,
> 12th and 24th packet when sending 25 packets.
>
> v1 -> v2: Address comments and directly use
> prandom_u32_state() instead of get_random_u32() for
> generating loss and corruption events. Generates a random
> seed using get_random_u64() if none was provided explicitly.
For series.
Acked-by: Stephen Hemminger <stephen@networkplumber.org>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events
2023-08-15 9:23 [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events Francois Michel
` (4 preceding siblings ...)
2023-08-16 15:02 ` Stephen Hemminger
@ 2023-08-18 2:30 ` patchwork-bot+netdevbpf
2023-08-19 0:17 ` Stephen Hemminger
6 siblings, 0 replies; 8+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-08-18 2:30 UTC (permalink / raw)
To: Francois Michel
Cc: jhs, xiyou.wangcong, jiri, stephen, davem, edumazet, kuba, pabeni,
netdev, linux-kernel
Hello:
This series was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Tue, 15 Aug 2023 11:23:37 +0200 you wrote:
> From: François Michel <francois.michel@uclouvain.be>
>
> In order to reproduce bugs or performance evaluation of
> network protocols and applications, it is useful to have
> reproducible test suites and tools. This patch adds
> a way to specify a PRNG seed through the
> TCA_NETEM_PRNG_SEED attribute for generating netem
> loss and corruption events. Initializing the qdisc
> with the same seed leads to the exact same loss
> and corruption patterns. If no seed is explicitly
> specified, the qdisc generates a random seed using
> get_random_u64().
>
> [...]
Here is the summary with links:
- [v2,net-next,1/3] netem: add prng attribute to netem_sched_data
https://git.kernel.org/netdev/net-next/c/4072d97ddc44
- [v2,net-next,2/3] netem: use a seeded PRNG for generating random losses
https://git.kernel.org/netdev/net-next/c/9c87b2aeccf1
- [v2,net-next,3/3] netem: use seeded PRNG for correlated loss events
https://git.kernel.org/netdev/net-next/c/3cad70bc74ef
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events
2023-08-15 9:23 [PATCH v2 net-next 0/3] netem: use a seeded PRNG for loss and corruption events Francois Michel
` (5 preceding siblings ...)
2023-08-18 2:30 ` patchwork-bot+netdevbpf
@ 2023-08-19 0:17 ` Stephen Hemminger
6 siblings, 0 replies; 8+ messages in thread
From: Stephen Hemminger @ 2023-08-19 0:17 UTC (permalink / raw)
To: Francois Michel
Cc: Jamal Hadi Salim, Cong Wang, Jiri Pirko, David S. Miller,
Eric Dumazet, Jakub Kicinski, Paolo Abeni, netdev, linux-kernel
On Tue, 15 Aug 2023 11:23:37 +0200
Francois Michel <francois.michel@uclouvain.be> wrote:
> From: François Michel <francois.michel@uclouvain.be>
>
> In order to reproduce bugs or performance evaluation of
> network protocols and applications, it is useful to have
> reproducible test suites and tools. This patch adds
> a way to specify a PRNG seed through the
> TCA_NETEM_PRNG_SEED attribute for generating netem
> loss and corruption events. Initializing the qdisc
> with the same seed leads to the exact same loss
> and corruption patterns. If no seed is explicitly
> specified, the qdisc generates a random seed using
> get_random_u64().
>
> This patch can be and has been tested using tc from
> the following iproute2-next fork:
> https://github.com/francoismichel/iproute2-next
>
> For instance, setting the seed 42424242 on the loopback
> with a loss rate of 10% will systematically drop the 5th,
> 12th and 24th packet when sending 25 packets.
>
> v1 -> v2: Address comments and directly use
> prandom_u32_state() instead of get_random_u32() for
> generating loss and corruption events. Generates a random
> seed using get_random_u64() if none was provided explicitly.
>
> François Michel (3):
> netem: add prng attribute to netem_sched_data
> netem: use a seeded PRNG for generating random losses
> netem: use seeded PRNG for correlated loss events
>
> include/uapi/linux/pkt_sched.h | 1 +
> net/sched/sch_netem.c | 49 +++++++++++++++++++++++-----------
> 2 files changed, 35 insertions(+), 15 deletions(-)
>
>
> base-commit: f614a29d6ca6962139b0eb36b985e3dda80258a6
Would you please send an iproute2 patch now for iproute-next
^ permalink raw reply [flat|nested] 8+ messages in thread