From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
Shannon Zhao <shannon.zhao@linaro.org>,
Stefan Hajnoczi <stefanha@redhat.com>,
Shannon Zhao <zhaoshenglong@huawei.com>
Subject: [Qemu-devel] [PULL 17/17] net/net: Record usage status of mac address
Date: Wed, 27 May 2015 11:03:08 +0100 [thread overview]
Message-ID: <1432720988-20200-18-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1432720988-20200-1-git-send-email-stefanha@redhat.com>
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>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
net/net.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 55 insertions(+), 4 deletions(-)
diff --git a/net/net.c b/net/net.c
index 0fe5b8b..db6be12 100644
--- a/net/net.c
+++ b/net/net.c
@@ -167,19 +167,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);
}
/**
@@ -374,6 +423,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.4.1
next prev parent reply other threads:[~2015-05-27 11:04 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-27 10:02 [Qemu-devel] [PULL 00/17] Net patches Stefan Hajnoczi
2015-05-27 10:02 ` [Qemu-devel] [PULL 01/17] net: Change help text to list -netdev instead of -net by default Stefan Hajnoczi
2015-05-27 10:02 ` [Qemu-devel] [PULL 02/17] net: Improve error message for -net hubport a bit Stefan Hajnoczi
2015-05-27 10:02 ` [Qemu-devel] [PULL 03/17] net: Permit incremental conversion of init functions to Error Stefan Hajnoczi
2015-05-27 10:02 ` [Qemu-devel] [PULL 04/17] net: Improve -net nic error reporting Stefan Hajnoczi
2015-05-27 10:02 ` [Qemu-devel] [PULL 05/17] net/dump: Improve -net/host_net_add dump " Stefan Hajnoczi
2015-05-27 10:02 ` [Qemu-devel] [PULL 06/17] tap: net_tap_fd_init() can't fail, drop dead error handling Stefan Hajnoczi
2015-05-27 10:02 ` [Qemu-devel] [PULL 07/17] tap: Improve -netdev/netdev_add/-net/... bridge error reporting Stefan Hajnoczi
2015-05-27 10:02 ` [Qemu-devel] [PULL 08/17] tap: Convert tap_set_sndbuf() to Error Stefan Hajnoczi
2015-05-27 10:03 ` [Qemu-devel] [PULL 09/17] tap: Convert net_init_tap_one() " Stefan Hajnoczi
2015-05-27 10:03 ` [Qemu-devel] [PULL 10/17] tap: Convert launch_script() " Stefan Hajnoczi
2015-05-27 10:03 ` [Qemu-devel] [PULL 11/17] tap: Permit incremental conversion of tap_open() " Stefan Hajnoczi
2015-05-27 10:03 ` [Qemu-devel] [PULL 12/17] tap-linux: Convert " Stefan Hajnoczi
2015-05-27 10:03 ` [Qemu-devel] [PULL 13/17] tap-bsd: " Stefan Hajnoczi
2015-05-27 10:03 ` [Qemu-devel] [PULL 14/17] tap-solaris: " Stefan Hajnoczi
2015-05-27 10:03 ` [Qemu-devel] [PULL 15/17] tap: Finish conversion of " Stefan Hajnoczi
2015-05-27 10:03 ` [Qemu-devel] [PULL 16/17] tap: Improve -netdev/netdev_add/-net/... tap error reporting Stefan Hajnoczi
2015-05-27 10:03 ` Stefan Hajnoczi [this message]
2015-05-28 11:26 ` [Qemu-devel] [PULL 00/17] Net patches Peter Maydell
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=1432720988-20200-18-git-send-email-stefanha@redhat.com \
--to=stefanha@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=shannon.zhao@linaro.org \
--cc=zhaoshenglong@huawei.com \
/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;
as well as URLs for NNTP newsgroup(s).