From patchwork Fri Jun 28 03:38:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Anibal Limon X-Patchwork-Id: 167999 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp3157099ilk; Thu, 27 Jun 2019 20:38:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqy4bjtmj+91TZVGljdcH9o/fvNYLMuKS/IlEC2Nb9LQ4k7b4rO7IhGlT9JbtaAMxrMqVC5B X-Received: by 2002:a17:902:d887:: with SMTP id b7mr8806859plz.28.1561693086565; Thu, 27 Jun 2019 20:38:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561693086; cv=none; d=google.com; s=arc-20160816; b=WXpDUfQstNxqCzHPbfTm6giW8ne3LvPCPWXYKBbVN4yeNo4jgLDkxBVJoroEr6aWqR OPTjUfRdbkXdlTac5/3Ms2778k7e+5pjOJDoT3TY99wwkopu64tUEej7amh5gZz99UIT dqSJb0rdbRFuKIGy9//K2ws1z4PmS1VEdVojztkwXrifmGApp7Y5Jp9Z8ei5Z6WZoaEL jHpyrU3GP6e2gzsiTkjYISoILUOYowZkKSqzIPlFmfx0m7xMMWK8pkfrud/c/c5Eh3pQ yD5NaT7ks4C2mPnnB2VApxyjlkH5d6QjKKnINTDBMssOuXHMOcCwvYvJpvOY8hxyq9mo KYfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:delivered-to; bh=nEMM14ufRBuU6ahvW0AU9hZ7slpMLSftNOVd0tvFJeM=; b=MM0qUKaEsPmcw2DdU9cofREKQJeT4LJ8tRovKTZyz0bYk2DWyRrTPSwKDyfUe8CtVj Pd0p2GqgjhobIiFr/21DD18Q6quDxm/CKQys7hrEpZHajsfTD0jVm1ATlMzs+6UdUp71 jBlq+f/dvM/kAGe3UikEIfiMF/ry1JlXq5GougdKVAEHdU1S2jlC6/O0r1iavpsp4zec We9XRGBFdxVYpcm2XPyVA2Uu77jBr+wduVhms2nbswLNuBIkVXi8HO47sivKyMBUstTS JvSJ3e2prAsyieFrrWTCmNwjMfu3JDeid0gFQZi9gSFoQEK3tcrJb5iJ0rU+EZqRn9AR 0KGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=g2zncgl6; spf=pass (google.com: best guess record for domain of openembedded-core-bounces@lists.openembedded.org designates 140.211.169.62 as permitted sender) smtp.mailfrom=openembedded-core-bounces@lists.openembedded.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail.openembedded.org (mail.openembedded.org. [140.211.169.62]) by mx.google.com with ESMTP id e96si935478plb.123.2019.06.27.20.38.06; Thu, 27 Jun 2019 20:38:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of openembedded-core-bounces@lists.openembedded.org designates 140.211.169.62 as permitted sender) client-ip=140.211.169.62; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=g2zncgl6; spf=pass (google.com: best guess record for domain of openembedded-core-bounces@lists.openembedded.org designates 140.211.169.62 as permitted sender) smtp.mailfrom=openembedded-core-bounces@lists.openembedded.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from ec2-34-214-78-129.us-west-2.compute.amazonaws.com (localhost [127.0.0.1]) by mail.openembedded.org (Postfix) with ESMTP id 459D57EA5E; Fri, 28 Jun 2019 03:38:03 +0000 (UTC) X-Original-To: openembedded-core@lists.openembedded.org Delivered-To: openembedded-core@lists.openembedded.org Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by mail.openembedded.org (Postfix) with ESMTP id 9A22D7EA5F for ; Fri, 28 Jun 2019 03:37:56 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id bi6so2411013plb.12 for ; Thu, 27 Jun 2019 20:37:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wAkmMDly0bZ8LiPueFeslg8jzXm3MB4NJxjNlm+XwoE=; b=g2zncgl6CY1mEA32gTBYoBs1siJyLD4JRrhQ+B1iIgSCdBobEAU08q4qx+ZF5eAeO4 ya4+Y6ab2TtsSX6NzP2qkT02RuMuiM27PggNaZN1LZrRFRaUoAT3U22Hl/yCSiYOcUX/ FibZf7mPA7qdGy50xFrDyIzIlP8/xP492aEYMzIFjebu4y9yMYz9M2ZLg+UgfceHh3KG UJzwwdKCyibQc3IPMy1qH2zjzI6fvpbZPNYOshYn164/LMbC82CayPk1aP5TGn6N8NfW Q8Myr20hzVr4ehPLBnKZJC4B4d4EtryCVthVFCK5jyCSuWAaQe/elcMexyjewZDtXP3I bFuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wAkmMDly0bZ8LiPueFeslg8jzXm3MB4NJxjNlm+XwoE=; b=TtdGWZhpT1QcemXEu7oofNlC9RE7F29nUZGc8bvS3McHDGWyIrN7Jw0q2kF3Gz0dOB s51Edd2GwoC6ZRgFipxJnxetS8e6CnmjgNU8Wh6FLwzJXfTnTWdByEjk/bC4RF+vxL/E kNthsuNcN0Pdq9PnboK6iw2Sq8WMJm3pv1zgSxJ6E59rf3b/p2H1UVpaNl6BuRc7aLNM Oob4XuhKV3s3mp73y8FVX7gqqGU3gZNwFE8oJXm4i1fLvrD4lz5Sq9FSTSDLjDE6vyB6 6SNmSKhcPP/vBOOBABAY3Fj/Z3Ta/CMU+H9Z9PUD7pj9KvsriOtovPlP1Zc0R/3/92cv RD0g== X-Gm-Message-State: APjAAAWPsvqTbFqlglDMBIA2x1KEjlLtwN1qN2t3gNEW4S3Hid3dLZe4 tjsuEu9fl7MnyACcgg8Ijp9WIn0aP0JCVlno X-Received: by 2002:a17:902:bf08:: with SMTP id bi8mr9052756plb.189.1561693077572; Thu, 27 Jun 2019 20:37:57 -0700 (PDT) Received: from blackbox.Home ([200.68.140.5]) by smtp.gmail.com with ESMTPSA id e184sm566027pfa.169.2019.06.27.20.37.55 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 27 Jun 2019 20:37:57 -0700 (PDT) From: =?utf-8?b?QW7DrWJhbCBMaW3Ds24=?= To: openembedded-core@lists.openembedded.org Date: Thu, 27 Jun 2019 22:38:29 -0500 Message-Id: <20190628033830.27713-2-anibal.limon@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190628033830.27713-1-anibal.limon@linaro.org> References: <20190628033830.27713-1-anibal.limon@linaro.org> MIME-Version: 1.0 Subject: [OE-core] [PATCH 2/3] runqemu: Add support for multiple tap devices X-BeenThere: openembedded-core@lists.openembedded.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: Patches and discussions about the oe-core layer List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: openembedded-core-bounces@lists.openembedded.org Errors-To: openembedded-core-bounces@lists.openembedded.org Add the ability to set more than one tap devices into the same qemu instance, The code was modified to detect multiple @TAP@ and @MAC@ in the QA_TAP_OPT and QA_NETWORK_DEVICE respectively, it handles the attach/creation of multiple tap devices and stores into a list for lock/unlock. Configure the first interface because Kernel IP Configuration only takes care of the first device. This patch was tested using qemux86, kvm-vhost and NFS rootfs. Example of the configuration: QB_TAP_OPT = "-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no \ -netdev tap,id=net1,ifname=@TAP@,script=no,downscript=no" QB_NETWORK_DEVICE = "-device virtio-net-pci,netdev=net0,mac=@MAC@ \ -device virtio-net-pci,netdev=net1,mac=@MAC@" Signed-off-by: Aníbal Limón --- scripts/runqemu | 134 +++++++++++++++++++++++++++--------------------- 1 file changed, 76 insertions(+), 58 deletions(-) diff --git a/scripts/runqemu b/scripts/runqemu index 8a54c9ef9a..e1a78c8910 100755 --- a/scripts/runqemu +++ b/scripts/runqemu @@ -1004,64 +1004,81 @@ class BaseConfig(object): except FileExistsError: pass - cmd = (ip, 'link') - logger.debug('Running %s...' % str(cmd)) - ip_link = subprocess.check_output(cmd).decode('utf-8') - # Matches line like: 6: tap0: - possibles = re.findall('^[0-9]+: +(tap[0-9]+): <.*', ip_link, re.M) - tap = "" - for p in possibles: - lockfile = os.path.join(lockdir, p) - if os.path.exists('%s.skip' % lockfile): - logger.info('Found %s.skip, skipping %s' % (lockfile, p)) - continue - lock = lockfile + '.lock' - if self.acquire_lock(lock, error=False): - tap = p - logger.info("Using preconfigured tap device %s" % tap) - logger.info("If this is not intended, touch %s.skip to make runqemu skip %s." %(lockfile, tap)) - break - - if not tap: - if os.path.exists(nosudo_flag): - logger.error("Error: There are no available tap devices to use for networking,") - logger.error("and I see %s exists, so I am not going to try creating" % nosudo_flag) - raise RunQemuError("a new one with sudo.") - - gid = os.getgid() - uid = os.getuid() - logger.info("Setting up tap interface under sudo") - cmd = ('sudo', self.qemuifup, str(uid), str(gid), self.bindir_native) - tap = subprocess.check_output(cmd).decode('utf-8').strip() - lockfile = os.path.join(lockdir, tap) - lock = lockfile + '.lock' - self.acquire_lock(lock) - self.cleantap = True - logger.debug('Created tap: %s' % tap) - - if not tap: - logger.error("Failed to setup tap device. Run runqemu-gen-tapdevs to manually create.") - return 1 - self.tap = tap - tapnum = int(tap[3:]) - gateway = tapnum * 2 + 1 - client = gateway + 1 - if self.fstype == 'nfs': - self.setup_nfs() - netconf = "192.168.7.%s::192.168.7.%s:255.255.255.0" % (client, gateway) - logger.info("Network configuration: %s", netconf) - self.kernel_cmdline_script += " ip=%s" % netconf - mac = "%s%02x" % (self.mac_tap, client) - qb_tap_opt = self.get('QB_TAP_OPT') - if qb_tap_opt: - qemu_tap_opt = qb_tap_opt.replace('@TAP@', tap) - else: - qemu_tap_opt = "-netdev tap,id=net0,ifname=%s,script=no,downscript=no" % (self.tap) + self.taps = [] + qemu_tap_opt = self.get('QB_TAP_OPT') + if not qemu_tap_opt: + qemu_tap_opt = '-netdev tap,id=net0,ifname=@TAP@,script=no,downscript=no' if self.vhost_enabled: - qemu_tap_opt += ',vhost=on' + opts = [] + for tap_opt in qemu_tap_opt.split(): + if 'tap' in tap_opt: + tap_opt += ',vhost=on' + opts.append(tap_opt) + else: + opts.append(tap_opt) + qemu_tap_opt = ' '.join(opts) + + tap_no = qemu_tap_opt.count('@TAP@') + for tap_idx in range(tap_no): + cmd = (ip, 'link') + logger.debug('Running %s...' % str(cmd)) + ip_link = subprocess.check_output(cmd).decode('utf-8') + # Matches line like: 6: tap0: + possibles = re.findall('^[0-9]+: +(tap[0-9]+): <.*', ip_link, re.M) + tap = "" + for p in possibles: + if p in self.taps: + continue + + lockfile = os.path.join(lockdir, p) + if os.path.exists('%s.skip' % lockfile): + logger.info('Found %s.skip, skipping %s' % (lockfile, p)) + continue + lock = lockfile + '.lock' + if self.acquire_lock(lock, error=False): + tap = p + logger.info("Using preconfigured tap device %s" % tap) + logger.info("If this is not intended, touch %s.skip to make runqemu skip %s." %(lockfile, tap)) + break - self.set('NETWORK_CMD', '%s %s' % (self.network_device.replace('@MAC@', mac), qemu_tap_opt)) + if not tap: + if os.path.exists(nosudo_flag): + logger.error("Error: There are no available tap devices to use for networking,") + logger.error("and I see %s exists, so I am not going to try creating" % nosudo_flag) + raise RunQemuError("a new one with sudo.") + + gid = os.getgid() + uid = os.getuid() + logger.info("Setting up tap interface under sudo") + cmd = ('sudo', self.qemuifup, str(uid), str(gid), self.bindir_native) + tap = subprocess.check_output(cmd).decode('utf-8').strip() + lockfile = os.path.join(lockdir, tap) + lock = lockfile + '.lock' + self.acquire_lock(lock) + self.cleantap = True + logger.info('Created tap: %s' % tap) + + if not tap: + logger.error("Failed to setup tap device. Run runqemu-gen-tapdevs to manually create.") + return 1 + self.taps.append(tap) + tapnum = int(tap[3:]) + gateway = tapnum * 2 + 1 + client = gateway + 1 + + if tap_idx == 0: + netconf = "192.168.7.%s::192.168.7.%s:255.255.255.0::eth%d" % (client, gateway, tap_idx) + logger.info("Network configuration: %s", netconf) + self.kernel_cmdline_script += " ip=%s" % netconf + + mac = "%s%02x" % (self.mac_tap, client) + qemu_tap_opt = qemu_tap_opt.replace('@TAP@', tap, 1) + self.network_device = self.network_device.replace('@MAC@', mac, 1) + + self.set('NETWORK_CMD', '%s %s' % (self.network_device, qemu_tap_opt)) + if self.fstype == 'nfs': + self.setup_nfs() def setup_network(self): if self.get('QB_NET') == 'none': @@ -1287,9 +1304,10 @@ class BaseConfig(object): logger.info("Cleaning up") if self.cleantap: - cmd = ('sudo', self.qemuifdown, self.tap, self.bindir_native) - logger.debug('Running %s' % str(cmd)) - subprocess.check_call(cmd) + for tap in self.taps: + cmd = ('sudo', self.qemuifdown, tap, self.bindir_native) + logger.debug('Running %s' % str(cmd)) + subprocess.check_call(cmd) for lock in self.lock_descriptors.keys(): self.release_lock(lock)