[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20121004.171211.1135905847490517665.davem@davemloft.net>
Date: Thu, 04 Oct 2012 17:12:11 -0400 (EDT)
From: David Miller <davem@...emloft.net>
To: stephen.hemminger@...tta.com
Cc: dada1@...mosbay.com, netdev@...r.kernel.org
Subject: Re: [PATCH] udp: port starting location not random
From: Stephen Hemminger <stephen.hemminger@...tta.com>
Date: Thu, 04 Oct 2012 14:05:26 -0700 (PDT)
>> From: Stephen Hemminger <shemminger@...tta.com>
>> Date: Thu, 4 Oct 2012 13:56:56 -0700
>>
>> > - first = (((u64)rand * remaining) >> 32) + low;
>> > + first = rand % remaining + low;
>>
>> I really don't get it, either a modulus via multiplcation
>> works or it doesn't. We have this construct all over the
>> place.
>>
>>
>
> Try out of kernel:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <stdint.h>
>
> int main(int ac, char **av) {
> int low, high, remaining;
> unsigned int rand;
> unsigned short first;
>
> low = atoi(av[1]);
> high = atoi(av[2]);
> remaining = (high - low) + 1;
> rand = atoi(av[3]);
>
> first = (((uint64_t)rand * remaining) >> 32) + low;
>
> printf("%d %d %u => %u\n", low, high, rand, first);
> return 0;
> }
>
> $ port-test 32768 61000 $RANDOM
> 32768 61000 7027 => 32768
It just shows that we need to shift "remaining" up into the top N
bits instead of the bottom N bits.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Powered by blists - more mailing lists