[SLOF] [PATCH v2 4/7] net-snk: Seed the pseudo-random number generator

Thomas Huth thuth at redhat.com
Tue Dec 22 21:08:40 AEDT 2015


Use the MAC address and the current timebase value to seed the
pseudo-random number generator - this will hopefully give use
enough pseudo-randomness so that two guests that are booting in
parallel won't use the same rand() numbers.

Signed-off-by: Thomas Huth <thuth at redhat.com>
Reviewed-by: Stefan Berger <stefanb at linux.vnet.ibm.com>
---
 clients/net-snk/app/netapps/netboot.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/clients/net-snk/app/netapps/netboot.c b/clients/net-snk/app/netapps/netboot.c
index 5b7cefa..74fc235 100644
--- a/clients/net-snk/app/netapps/netboot.c
+++ b/clients/net-snk/app/netapps/netboot.c
@@ -367,6 +367,18 @@ int dhcp(char *ret_buffer, filename_ip_t * fn_ip, unsigned int retries, int flag
 	return rc;
 }
 
+/**
+ * Seed the random number generator with our mac and current timestamp
+ */
+static void seed_rng(uint8_t mac[])
+{
+	unsigned int seed;
+
+	asm volatile("mftbl %0" : "=r"(seed));
+	seed ^= (mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5];
+	srand(seed);
+}
+
 int
 netboot(int argc, char *argv[])
 {
@@ -437,6 +449,8 @@ netboot(int argc, char *argv[])
 	// init ethernet layer
 	set_mac_address(own_mac);
 
+	seed_rng(own_mac);
+
 	if (argc > 6) {
 		parse_args(argv[6], &obp_tftp_args);
 		if(obp_tftp_args.bootp_retries - rc < DEFAULT_BOOT_RETRIES)
-- 
1.8.3.1



More information about the SLOF mailing list