From patchwork Wed May 27 10:03:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 49011 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f69.google.com (mail-wg0-f69.google.com [74.125.82.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id EC83D202A3 for ; Wed, 27 May 2015 11:04:35 +0000 (UTC) Received: by wgwl7 with SMTP id l7sf1856248wgw.0 for ; Wed, 27 May 2015 04:04:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:date :message-id:in-reply-to:references: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=TXsXaLMt56pT9LLIzKI9rPNIr9xHEFojM5PoCus/45I=; b=RIxeLwrQqW11L1rTGDrvU/NeAf3Qffqa++TJJFUAQZQgGF0HBYJoyEBMZm4BxtMFwT cx0qekpsydB5YGhiSrhUlKaoLqkGKi/O9Dnc8BjARgwNc4GXuH0CMf72Tu5KS9dfawRO C4nGBOGgkaxxEIOQpsAR6GnxO4g5zojlAbj5ygsi0l2bQ6jzQwk0XZT0lLLW7xvVMQho z2QKtlWCj1a6Z4suyxj6A7goCO+PMbnNrF3FtLRumxefQPC0cQD6Ur/ONfZaBqLxTaIN GlFNQ5VZZMAY4+aty/uAsgwHhBY+TaXynJKspD/N6M711Gwy5yyEyPbqfcaZjH1rngZD OwNA== X-Gm-Message-State: ALoCoQkoOcJBd/cBvGctPlJbwre0FlIAKH98DrhqNKES/JpRYjtyfEz2eyCn0x4EKFs3ThLJ89C3 X-Received: by 10.194.5.229 with SMTP id v5mr31597210wjv.0.1432724675288; Wed, 27 May 2015 04:04:35 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.5.131 with SMTP id s3ls27777las.65.gmail; Wed, 27 May 2015 04:04:35 -0700 (PDT) X-Received: by 10.152.120.106 with SMTP id lb10mr26894616lab.92.1432724675129; Wed, 27 May 2015 04:04:35 -0700 (PDT) Received: from mail-la0-f43.google.com (mail-la0-f43.google.com. [209.85.215.43]) by mx.google.com with ESMTPS id wz10si13364843lbb.138.2015.05.27.04.04.35 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 May 2015 04:04:35 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.43 as permitted sender) client-ip=209.85.215.43; Received: by laat2 with SMTP id t2so4516070laa.1 for ; Wed, 27 May 2015 04:04:35 -0700 (PDT) X-Received: by 10.152.36.161 with SMTP id r1mr27335030laj.88.1432724674997; Wed, 27 May 2015 04:04:34 -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 hn6csp275485lbb; Wed, 27 May 2015 04:04:33 -0700 (PDT) X-Received: by 10.55.19.82 with SMTP id d79mr12470865qkh.21.1432724673206; Wed, 27 May 2015 04:04:33 -0700 (PDT) Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id a83si1807439qkh.109.2015.05.27.04.04.32 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 27 May 2015 04:04:33 -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]:53104 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YxZ8R-0001qg-67 for patch@linaro.org; Wed, 27 May 2015 07:04:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48422) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YxZ7v-0001Z1-Ou for qemu-devel@nongnu.org; Wed, 27 May 2015 07:04:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YxZ7s-0005ek-6m for qemu-devel@nongnu.org; Wed, 27 May 2015 07:03:59 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54650) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YxZ7s-0005eT-0K for qemu-devel@nongnu.org; Wed, 27 May 2015 07:03:56 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t4RA3mOR026041 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 27 May 2015 06:03:48 -0400 Received: from localhost (ovpn-112-78.ams2.redhat.com [10.36.112.78]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t4RA3lDr012584; Wed, 27 May 2015 06:03:47 -0400 From: Stefan Hajnoczi To: Date: Wed, 27 May 2015 11:03:08 +0100 Message-Id: <1432720988-20200-18-git-send-email-stefanha@redhat.com> In-Reply-To: <1432720988-20200-1-git-send-email-stefanha@redhat.com> References: <1432720988-20200-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Peter Maydell , Shannon Zhao , Stefan Hajnoczi , Shannon Zhao Subject: [Qemu-devel] [PULL 17/17] 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: stefanha@redhat.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.43 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 Signed-off-by: Stefan Hajnoczi --- 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++) {