From: "Andreas Bießmann" <andreas.devel@googlemail.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 11:26:29 +0200 [thread overview]
Message-ID: <55F935C5.8040906@gmail.com> (raw)
In-Reply-To: <55F9328C.7080903@atmel.com>
Hi Josh,
On 09/16/2015 11:12 AM, Josh Wu wrote:
> On 9/16/2015 4:23 PM, Andreas Bie?mann wrote:
>> 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
I'm fine with that too. All I'd like to say with my answer was that
adding getpid() was for some reason and removing it is worse than having
it OR'ed.
> The disadvantage is it might cost more time.
I don't think this count's here. This tool would be used to generate a
temporary MAC (or more than one) for testing purposes. So getting a
bunch of MAC's can last some time, but should provide unique ones.
Andreas
prev parent reply other threads:[~2015-09-16 9:26 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
2015-09-16 9:26 ` Andreas Bießmann [this message]
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=55F935C5.8040906@gmail.com \
--to=andreas.devel@googlemail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox