From patchwork Thu May 21 09:44:48 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shannon Zhao X-Patchwork-Id: 48854 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f70.google.com (mail-wg0-f70.google.com [74.125.82.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 7591C2121F for ; Thu, 21 May 2015 09:49:32 +0000 (UTC) Received: by wgwl7 with SMTP id l7sf25166398wgw.0 for ; Thu, 21 May 2015 02:49:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id :mime-version:content-type:cc:subject:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:list-subscribe :errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=3V0k+kge0wW4XXLhxWreeNSSMN6aXarrZQvVAp4V1V4=; b=emA6Z53n0nXZqYDu4xkE7NAInOmw8o8GiLGS0+MgWN5NvHaGgOXeX8WO+krg0W0Jp8 m6RuJdRyJ3MMmo9GIeKvfXP2SWG8KefQuFTJNCk4sFwRZo0WN4xcsVfDBrxMx1JJt4Tj Zo/yC49jZR7qX1YZrUB+be9zqROaCN/tyRNDb40QPytiZ8mO0tL9E0bvk4J82+IVRi03 InAEA+L5y72tGbzUOOOIIt+7V8IinBmsPsypBafryo58EmrzUgtooUxBS/5RC3RjHIfj KNSfnvzfwOtLeASwD5D/L7AG5NkGYMed/qr9UbeQJkZ0wPSx7A5EsLZI8po4Td1s2xHo tzEA== X-Gm-Message-State: ALoCoQn0eCaFihziQS/LmPmRrwDUubaZ3cYytemG7EvwPj3NBCM4BbuKArfOuj8XPSdTgvcxvoPr X-Received: by 10.180.105.226 with SMTP id gp2mr22662581wib.1.1432201771791; Thu, 21 May 2015 02:49:31 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.116.38 with SMTP id jt6ls348840lab.101.gmail; Thu, 21 May 2015 02:49:31 -0700 (PDT) X-Received: by 10.112.130.129 with SMTP id oe1mr1435593lbb.37.1432201771660; Thu, 21 May 2015 02:49:31 -0700 (PDT) Received: from mail-lb0-f175.google.com (mail-lb0-f175.google.com. [209.85.217.175]) by mx.google.com with ESMTPS id ez8si12941714lbc.64.2015.05.21.02.49.31 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 May 2015 02:49:31 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.175 as permitted sender) client-ip=209.85.217.175; Received: by lbbuc2 with SMTP id uc2so13993404lbb.2 for ; Thu, 21 May 2015 02:49:31 -0700 (PDT) X-Received: by 10.113.10.134 with SMTP id ea6mr1452357lbd.29.1432201771534; Thu, 21 May 2015 02:49:31 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp443987lbb; Thu, 21 May 2015 02:49:30 -0700 (PDT) X-Received: by 10.55.20.141 with SMTP id 13mr3971981qku.30.1432201770241; Thu, 21 May 2015 02:49:30 -0700 (PDT) Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id i68si4734631qgf.23.2015.05.21.02.49.29 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 21 May 2015 02:49:30 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Received: from localhost ([::1]:56286 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YvN6X-0003dz-Em for patch@linaro.org; Thu, 21 May 2015 05:49:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49141) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YvN5q-0002tc-Op for qemu-devel@nongnu.org; Thu, 21 May 2015 05:48:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YvN5k-0006oY-Sk for qemu-devel@nongnu.org; Thu, 21 May 2015 05:48:46 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:19354) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YvN5k-0006nx-8J for qemu-devel@nongnu.org; Thu, 21 May 2015 05:48:40 -0400 Received: from 172.24.2.119 (EHLO szxeml430-hub.china.huawei.com) ([172.24.2.119]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CLS02801; Thu, 21 May 2015 17:45:20 +0800 (CST) Received: from HGHY1Z002260041.china.huawei.com (10.177.16.142) by szxeml430-hub.china.huawei.com (10.82.67.185) with Microsoft SMTP Server id 14.3.158.1; Thu, 21 May 2015 17:45:07 +0800 From: Shannon Zhao To: , Date: Thu, 21 May 2015 17:44:48 +0800 Message-ID: <1432201488-3832-1-git-send-email-zhaoshenglong@huawei.com> X-Mailer: git-send-email 1.9.0.msysgit.0 MIME-Version: 1.0 X-Originating-IP: [10.177.16.142] X-CFilter-Loop: Reflected X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 119.145.14.65 Cc: hangaohuai@huawei.com, zhaoshenglong@huawei.com, peter.huangpeng@huawei.com, shannon.zhao@linaro.org Subject: [Qemu-devel] [RFC PATCH] net/net: Record usage status of mac address X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: patch@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.175 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 From: Shannon Zhao 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 Signed-off-by: Shannon Zhao --- 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++) {