From: Josh Wu <josh.wu@atmel.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH] tools: gen_eth_addr: remove getpid() operation for the random seed
Date: Wed, 16 Sep 2015 17:12:44 +0800 [thread overview]
Message-ID: <55F9328C.7080903@atmel.com> (raw)
In-Reply-To: <55F92707.4030405@gmail.com>
Hi, Andreas
On 9/16/2015 4:23 PM, Andreas Bie?mann wrote:
> Hi Josh,
>
> On 09/16/2015 05:18 AM, Josh Wu wrote:
>> As 'time(0) | getpid()' sometimes get same value. That depends on the
>> value of getpid().
>> So that is not a expected behavior. We expect different value for the
>> seed when when run it in many times.
> I don't think your change made it better. Here is a snippet from a run
> of time(NULL) and getpid():
>
> ---8<---
> time: 1442389450; pid: 11632; time | pid: 1442397690;
> time: 1442389450; pid: 11633; time | pid: 1442397691;
> time: 1442389450; pid: 11634; time | pid: 1442397690;
> time: 1442389450; pid: 11635; time | pid: 1442397691;
> time: 1442389450; pid: 11636; time | pid: 1442397694;
> time: 1442389450; pid: 11637; time | pid: 1442397695;
> time: 1442389450; pid: 11638; time | pid: 1442397694;
> time: 1442389450; pid: 11639; time | pid: 1442397695;
> time: 1442389450; pid: 11640; time | pid: 1442397690;
> time: 1442389450; pid: 11641; time | pid: 1442397691;
> time: 1442389450; pid: 11642; time | pid: 1442397690;
> time: 1442389450; pid: 11643; time | pid: 1442397691;
> --->8---
>
> While time(NULL) is stable, getpid() is incrementing by one. As you may
> expect the OR'ed value is oscillating and the values almost the same. So
> calling gen_eth_addr three times within the same second you will get two
> time the same MAC.
>
>> So this patch remove the getpid(), just use the time(0) as the seed.
> So let's see the effect of your change ...
>
> The output of gen_eth_addr at the current ToT:
>
> % RUN=0; while [ $RUN -lt 10000 ]; do
> /tmp/build-uboot-test/tools/gen_eth_addr; RUN=$(($RUN+1)); done | sort |
> uniq | wc -l
> 254
>
> With your change applied:
>
> % RUN=0; while [ $RUN -lt 10000 ]; do
> /tmp/build-uboot-test/tools/gen_eth_addr; RUN=$(($RUN+1)); done | sort |
> uniq | wc -l
> 10
>
> Another approach would be to change the algorithm (OR the values) here.
> A short test showed that using XOR could be a solution:
>
> ---8<---
> time: 1442389450; pid: 11632; time ^ pid: 1442394298;
> time: 1442389450; pid: 11633; time ^ pid: 1442394299;
> time: 1442389450; pid: 11634; time ^ pid: 1442394296;
> time: 1442389450; pid: 11635; time ^ pid: 1442394297;
> time: 1442389450; pid: 11636; time ^ pid: 1442394302;
> time: 1442389450; pid: 11637; time ^ pid: 1442394303;
> time: 1442389450; pid: 11638; time ^ pid: 1442394300;
> time: 1442389450; pid: 11639; time ^ pid: 1442394301;
> time: 1442389450; pid: 11640; time ^ pid: 1442394290;
> time: 1442389450; pid: 11641; time ^ pid: 1442394291;
> time: 1442389450; pid: 11642; time ^ pid: 1442394288;
> time: 1442389450; pid: 11643; time ^ pid: 1442394289;
> --->8---
>
> It is the same input but none of the outputs is the same value.
>
> The XOR approach applied to gen_eth_addr:
>
> % RUN=0; while [ $RUN -lt 10000 ]; do
> /tmp/build-uboot-test/tools/gen_eth_addr; RUN=$(($RUN+1)); done | sort |
> uniq | wc -l
> 9988
How about using ADD ?
When I change to 'time + (pid << 8)', the result is better. And the pid
change will has more impact in time range.
? tools time RUN=0; while [ $RUN -lt 10000 ]; do
./gen_eth_addr_add_shift 1; RUN=$(($RUN+1)); done | sort | uniq | wc -l
10000
The disadvantage is it might cost more time.
Best Regards,
Josh Wu
> Andreas
>
>> Signed-off-by: Josh Wu <josh.wu@atmel.com>
>> ---
>>
>> tools/gen_eth_addr.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/tools/gen_eth_addr.c b/tools/gen_eth_addr.c
>> index bf9d935..53b023a 100644
>> --- a/tools/gen_eth_addr.c
>> +++ b/tools/gen_eth_addr.c
>> @@ -15,7 +15,7 @@ main(int argc, char *argv[])
>> {
>> unsigned long ethaddr_low, ethaddr_high;
>>
>> - srand(time(0) | getpid());
>> + srand(time(0));
>>
>> /*
>> * setting the 2nd LSB in the most significant byte of
>>
next prev parent reply other threads:[~2015-09-16 9:12 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-16 3:18 [U-Boot] [PATCH] tools: gen_eth_addr: remove getpid() operation for the random seed Josh Wu
2015-09-16 6:37 ` Wolfgang Denk
2015-09-16 7:08 ` Josh Wu
2015-09-16 7:27 ` Josh Wu
2015-09-16 9:15 ` Wolfgang Denk
2015-09-16 9:53 ` Josh Wu
2015-09-16 8:23 ` Andreas Bießmann
2015-09-16 9:12 ` Josh Wu [this message]
2015-09-16 9:26 ` Andreas Bießmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=55F9328C.7080903@atmel.com \
--to=josh.wu@atmel.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.