mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
net_util: Add helper for generating a random local MAC
We must ensure our MAC addresses do not conflict with a global one. Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate libc;
|
||||
extern crate rand;
|
||||
extern crate serde;
|
||||
|
||||
extern crate net_gen;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the THIRD-PARTY file.
|
||||
|
||||
use rand::Rng;
|
||||
use std::result::Result;
|
||||
|
||||
use serde::de::{Deserialize, Deserializer, Error};
|
||||
@@ -72,6 +73,18 @@ impl MacAddr {
|
||||
b[0], b[1], b[2], b[3], b[4], b[5]
|
||||
)
|
||||
}
|
||||
|
||||
pub fn local_random() -> MacAddr {
|
||||
// Generate a fully random MAC
|
||||
let mut random_bytes = rand::thread_rng().gen::<[u8; MAC_ADDR_LEN]>();
|
||||
|
||||
// Set the first byte to make the OUI a locally administered OUI
|
||||
random_bytes[0] = 0x2e;
|
||||
|
||||
MacAddr {
|
||||
bytes: random_bytes,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for MacAddr {
|
||||
|
||||
Reference in New Issue
Block a user