Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/netinet/cc: Switch from deprecated random() to prng32() #1162

Merged

Conversation

hhartzer
Copy link
Contributor

@hhartzer
Copy link
Contributor Author

Hoping that someone with familiarity of these components can review this. I feel like it looks like an innocent enough set of changes, but I don't want to get something wrong here.

@gmshake
Copy link
Contributor

gmshake commented Apr 11, 2024

CC @tuexen

@bsdimp
Copy link
Member

bsdimp commented Apr 12, 2024

Also cc @cemeyer @amotin @markjdb

These seem fine, but it looks like the range is changing for these (the range is accounted for, but I want more eyes on it).

Comment on lines 523 to 525
p = (UINT_MAX / (1 << EXP_PREC)) * probexp[idx];
backoff = (prng32() < p);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why p is still int, not uint32_t or uint_t?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think that's a mistake. Should I do unit32_t for it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I'd say p should be unsigned for correct comparison, and it should have the same range for the probability to work. So there should be uint32_t and UINT32_MAX.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! I've adjusted p accordingly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. But why did you leave UINT_MAX in its assignment instead of UINT32_MAX I proposed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad! I think I missed that.

@@ -161,7 +162,7 @@ should_backoff(int qdly, int maxqdly, struct chd *chd_data)
{
unsigned long p, rand;

rand = random();
rand = prng32();

if (qdly < V_chd_qthresh) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wonder whether should_backoff() arguments here and below should really be signed, whether they makes sense and if so, whether it is handled right.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also am not sure why these were signed. I don't know hardly a thing about the code in question. Was just trying to take a stab at removing the deprecated random() call where possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know this code either, so it was just a thought. What you should to though is use UINT32_MAX for RANDOM_MAX (or just inline it) and uint32_t for p. rand variable I would just remove, inlining the prng32() call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I did most of what you suggested, but I found that the rand variable was referenced twice, so it seemed necessary to keep it assigned. Is that alright?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, looks like RANDOM_MAX is still defined in the file. Should I remove it or reference it instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that alright?

Sure. I've just missed the other use. Sorry.

Either use RANDOM_MAX everywhere, or remove everywhere (in the two files for consistency). It is used only in the same small function as prng32(), so it is a bit obvious, but a bit confusing being declared many screens away.

@hhartzer
Copy link
Contributor Author

I can update this but would prefer a little bit more guidance about how exactly it should be done, if that's possible.

@bsdimp
Copy link
Member

bsdimp commented May 16, 2024

@amotin I think @hhartzer needs some help with your suggestion

@hhartzer hhartzer force-pushed the sys-inet-cc-switch-from-random-to-prng32 branch from a3cc47d to bb3f867 Compare May 16, 2024 18:55
@@ -308,7 +309,7 @@ cdg_cb_init(struct cc_var *ccv, void *ptr)
cdg_data->queue_state = CDG_Q_UNKNOWN;
cdg_data->maxrtt_in_rtt = 0;
cdg_data->maxrtt_in_prevrtt = 0;
cdg_data->minrtt_in_rtt = INT_MAX;
cdg_data->minrtt_in_rtt = UINT_MAX;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minrtt_in_rtt is just int, this change makes no sense to me, same as one below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! I am not sure why I did that. Good catch.

Comment on lines 523 to 525
p = (UINT_MAX / (1 << EXP_PREC)) * probexp[idx];
backoff = (prng32() < p);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. But why did you leave UINT_MAX in its assignment instead of UINT32_MAX I proposed?

@@ -161,7 +162,7 @@ should_backoff(int qdly, int maxqdly, struct chd *chd_data)
{
unsigned long p, rand;

rand = random();
rand = prng32();

if (qdly < V_chd_qthresh) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that alright?

Sure. I've just missed the other use. Sorry.

Either use RANDOM_MAX everywhere, or remove everywhere (in the two files for consistency). It is used only in the same small function as prng32(), so it is a bit obvious, but a bit confusing being declared many screens away.

@hhartzer hhartzer force-pushed the sys-inet-cc-switch-from-random-to-prng32 branch from bb3f867 to ba1e56d Compare May 16, 2024 20:27
@@ -637,7 +639,7 @@ cdg_ack_received(struct cc_var *ccv, ccsignal_t ack_type)
}

cdg_data->minrtt_in_prevrtt = cdg_data->minrtt_in_rtt;
cdg_data->minrtt_in_rtt = INT_MAX;
cdg_data->minrtt_in_rtt = UINT_MAX;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The one below"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops!

/* Largest possible number returned by random(). */
#define RANDOM_MAX INT_MAX
/* Largest possible number returned by prng32(). */
#define RANDOM_MAX RANDOM_MAX
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool define! ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today is not my day! Thank you for being patient with me.

@hhartzer hhartzer force-pushed the sys-inet-cc-switch-from-random-to-prng32 branch from ba1e56d to f521b30 Compare May 16, 2024 20:51
Fix stdint.h file not found.

Fixes: 		4b75afe
bsdimp pushed a commit to hhartzer/freebsd-src that referenced this pull request May 23, 2024
@bsdimp bsdimp force-pushed the sys-inet-cc-switch-from-random-to-prng32 branch from f521b30 to 98b7bbf Compare May 23, 2024 21:09
@bsdimp bsdimp force-pushed the sys-inet-cc-switch-from-random-to-prng32 branch from 98b7bbf to 674956e Compare May 23, 2024 21:10
@freebsd-git freebsd-git merged commit 674956e into freebsd:main May 23, 2024
7 of 9 checks passed
bsdjhb pushed a commit to bsdjhb/cheribsd that referenced this pull request Aug 31, 2024
bsdjhb pushed a commit to bsdjhb/cheribsd that referenced this pull request Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants