* [Qemu-devel] [RFC PATCH] net/net: Record usage status of mac address
@ 2015-05-21 9:44 Shannon Zhao
2015-05-26 10:08 ` Stefan Hajnoczi
0 siblings, 1 reply; 2+ messages in thread
From: Shannon Zhao @ 2015-05-21 9:44 UTC (permalink / raw)
To: qemu-devel, stefanha
Cc: hangaohuai, zhaoshenglong, peter.huangpeng, shannon.zhao
From: Shannon Zhao <shannon.zhao@linaro.org>
Currently QEMU dynamically generates mac address for the NIC which
doesn't specify the mac address. But when we hotplug a NIC without
specifying mac address, the mac address will increase for the same NIC
along with hotplug and hot-unplug, and at last it will overflow. And if
we codeplug one NIC with mac address e.g. "52:54:00:12:34:56", then
hotplug one NIC without specifying mac address and the mac address of
the hotplugged NIC is duplicate of "52:54:00:12:34:56".
This patch add a mac_table to record the usage status and free the mac
address when the NIC is unrealized.
Signed-off-by: Shannon Zhao <zhaoshenglong@huawei.com>
Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org>
---
net/net.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 55 insertions(+), 4 deletions(-)
diff --git a/net/net.c b/net/net.c
index 0be084d..7f0646e 100644
--- a/net/net.c
+++ b/net/net.c
@@ -160,19 +160,68 @@ void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
macaddr[3], macaddr[4], macaddr[5]);
}
+static int mac_table[256] = {0};
+
+static void qemu_macaddr_set_used(MACAddr *macaddr)
+{
+ int index;
+
+ for (index = 0x56; index < 0xFF; index++) {
+ if (macaddr->a[5] == index) {
+ mac_table[index]++;
+ }
+ }
+}
+
+static void qemu_macaddr_set_free(MACAddr *macaddr)
+{
+ int index;
+ static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
+
+ if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
+ return;
+ }
+ for (index = 0x56; index < 0xFF; index++) {
+ if (macaddr->a[5] == index) {
+ mac_table[index]--;
+ }
+ }
+}
+
+static int qemu_macaddr_get_free(void)
+{
+ int index;
+
+ for (index = 0x56; index < 0xFF; index++) {
+ if (mac_table[index] == 0) {
+ return index;
+ }
+ }
+
+ return -1;
+}
+
void qemu_macaddr_default_if_unset(MACAddr *macaddr)
{
- static int index = 0;
static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
+ static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
+
+ if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
+ if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
+ return;
+ } else {
+ qemu_macaddr_set_used(macaddr);
+ return;
+ }
+ }
- if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
- return;
macaddr->a[0] = 0x52;
macaddr->a[1] = 0x54;
macaddr->a[2] = 0x00;
macaddr->a[3] = 0x12;
macaddr->a[4] = 0x34;
- macaddr->a[5] = 0x56 + index++;
+ macaddr->a[5] = qemu_macaddr_get_free();
+ qemu_macaddr_set_used(macaddr);
}
/**
@@ -367,6 +416,8 @@ void qemu_del_nic(NICState *nic)
{
int i, queues = MAX(nic->conf->peers.queues, 1);
+ qemu_macaddr_set_free(&nic->conf->macaddr);
+
/* If this is a peer NIC and peer has already been deleted, free it now. */
if (nic->peer_deleted) {
for (i = 0; i < queues; i++) {
--
2.0.4
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [Qemu-devel] [RFC PATCH] net/net: Record usage status of mac address
2015-05-21 9:44 [Qemu-devel] [RFC PATCH] net/net: Record usage status of mac address Shannon Zhao
@ 2015-05-26 10:08 ` Stefan Hajnoczi
0 siblings, 0 replies; 2+ messages in thread
From: Stefan Hajnoczi @ 2015-05-26 10:08 UTC (permalink / raw)
To: Shannon Zhao
Cc: hangaohuai, shannon.zhao, qemu-devel, stefanha, peter.huangpeng
[-- Attachment #1: Type: text/plain, Size: 1073 bytes --]
On Thu, May 21, 2015 at 05:44:48PM +0800, Shannon Zhao wrote:
> From: Shannon Zhao <shannon.zhao@linaro.org>
>
> Currently QEMU dynamically generates mac address for the NIC which
> doesn't specify the mac address. But when we hotplug a NIC without
> specifying mac address, the mac address will increase for the same NIC
> along with hotplug and hot-unplug, and at last it will overflow. And if
> we codeplug one NIC with mac address e.g. "52:54:00:12:34:56", then
> hotplug one NIC without specifying mac address and the mac address of
> the hotplugged NIC is duplicate of "52:54:00:12:34:56".
>
> This patch add a mac_table to record the usage status and free the mac
> address when the NIC is unrealized.
>
> Signed-off-by: Shannon Zhao <zhaoshenglong@huawei.com>
> Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org>
> ---
> net/net.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 55 insertions(+), 4 deletions(-)
Thanks, applied to my net tree:
https://github.com/stefanha/qemu/commits/net
Stefan
[-- Attachment #2: Type: application/pgp-signature, Size: 473 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-05-26 10:08 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-21 9:44 [Qemu-devel] [RFC PATCH] net/net: Record usage status of mac address Shannon Zhao
2015-05-26 10:08 ` Stefan Hajnoczi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).