From nobody Mon Dec 15 12:02:08 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1534230079934483.7012249985265; Tue, 14 Aug 2018 00:01:19 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5A1EC30001E5; Tue, 14 Aug 2018 07:01:17 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 76BD770A18; Tue, 14 Aug 2018 07:01:16 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id DFD9718037EF; Tue, 14 Aug 2018 07:01:15 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w7E71E0G004968 for ; Tue, 14 Aug 2018 03:01:14 -0400 Received: by smtp.corp.redhat.com (Postfix) id A925B10694D7; Tue, 14 Aug 2018 07:01:14 +0000 (UTC) Received: from mx1.redhat.com (ext-mx19.extmail.prod.ext.phx2.redhat.com [10.5.110.48]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9F68210694E1 for ; Tue, 14 Aug 2018 07:01:14 +0000 (UTC) Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D9A4F307D861 for ; Tue, 14 Aug 2018 07:01:07 +0000 (UTC) Received: from localhost.localdomain ([58.56.27.130]) by mail.gmx.com (mrgmx101 [212.227.17.174]) with ESMTPSA (Nemesis) id 0MUDXS-1fOQ0w2Zes-00R0Wo; Tue, 14 Aug 2018 09:01:06 +0200 From: Shi Lei To: libvir-list@redhat.com Date: Tue, 14 Aug 2018 15:00:50 +0800 Message-Id: <1534230052-10359-2-git-send-email-shilei.massclouds@gmx.com> In-Reply-To: <1534230052-10359-1-git-send-email-shilei.massclouds@gmx.com> References: <1534230052-10359-1-git-send-email-shilei.massclouds@gmx.com> X-Provags-ID: V03:K1:pN/VexoGPG7IArNLIuA15a3R+oEPrW4H6qKcrlYRrrQg48Pjp7F DEIp3C7jz187zVtcxTEjPMqot3BzYMRKH3S/BZ88SzvHHnVVKCLHiD26vt88DrHHh64Qgpt 6xRDwOK33Xls7AWdLHXlj8y8tDAMxTgF8AdFm344oYAJS6a1dP/R0HC3JHa1Wb55G7kxfCl nAwc6LzCIuirOCDVqNczg== X-UI-Out-Filterresults: notjunk:1;V01:K0:+Kn03a+VXWw=:SBj2j9biKppXXcSiFCpjEl T55YIq5+daX44j+pMhMLOxN6WRmdcFTlhDsNjG1bqKJ0EFdUtg1OvNrA6d+bTHILS91qLT4Q+ rk4k8tzZAtQ0PyXJ/Ra9s/TODLJp3NQqft5YL3dMUCjbetPz4EVx/ProZgk09bIIjDNr2aTZq +UJqvehH70XPNZeCK5IpQl2KgEIsI2GlZezjIsmJrgjCT2XIaqMCleRISYrC6jHWJ49zJWEzM wJkMT+FAihRj4cUq9DTZhkFcaKRl6Q7X6LDHQ6UPqSe5kDkeyLUxh/hJLyRNWWJ6kexJnzouA 4Twh1TlOQwIUnO/WTy6/Hssdz8Hmhv+7GFWctZkpuXQeIm4Er+9lFwnPIJjcxGIvEfVM1CmZq A9MSJPFq+xavEyzDXE2Ro/oShSpRDqcbzpaHcT2dZNWVfXlg6eSdv/dolw9Ot1ufpAXZr9NHD dEXZVZE7vqrMvz/syyb3ry6EWgnBbf14VYwGZsX6sVpI957l8xZAPsulqo83OW/UiTHmqVAQf dtULVYX1K2ofMDXXkIUJt8LjL1VSsNBgr0OSo8D9BNFSRg7WmupO+OGBaL7qZPaL/Y6vf6yNv P9HKD8DKChQzkndeRJiIGjACF4ZofeMIUMvYQSM1O+l1wjpspEuKZ2gY4261aTDBS7tSMptA8 5q1gdfflNO0ihoUxArkIjJYS7a/UlDvE3A6f9mADiczzMMonqWqrFNzuh1VDHUyEIj9TZmsFf Zqx5BoiX6CIW36WU9EK1hhXIxw7WgmIA/ixAtdRfp4gt59V6LRqQZwKd0Pc= X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Tue, 14 Aug 2018 07:01:08 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Tue, 14 Aug 2018 07:01:08 +0000 (UTC) for IP:'212.227.17.20' DOMAIN:'mout.gmx.net' HELO:'mout.gmx.net' FROM:'shilei.massclouds@gmx.com' RCPT:'' X-RedHat-Spam-Score: -0.7 (FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS) 212.227.17.20 mout.gmx.net 212.227.17.20 mout.gmx.net X-Scanned-By: MIMEDefang 2.84 on 10.5.110.48 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Cc: Shi Lei Subject: [libvirt] [PATCHv2 1/3] add functions: load 8021q module, create/destroy vlan-dev X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Tue, 14 Aug 2018 07:01:18 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Shi Lei --- configure.ac | 6 ++ src/libvirt_private.syms | 4 + src/util/virnetdev.c | 203 +++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virnetdev.h | 18 +++++ 4 files changed, 231 insertions(+) diff --git a/configure.ac b/configure.ac index da940e3..5f2ea12 100644 --- a/configure.ac +++ b/configure.ac @@ -784,6 +784,12 @@ AM_CONDITIONAL([WITH_NODE_DEVICES], [test "$with_noded= ev" =3D "yes"]) dnl GET_VLAN_VID_CMD is required for virNetDevGetVLanID AC_CHECK_DECLS([GET_VLAN_VID_CMD], [], [], [[#include ]]) =20 +dnl ADD_VLAN_CMD is required for virNetDevCreateVLanDev +AC_CHECK_DECLS([ADD_VLAN_CMD], [], [], [[#include ]]) + +dnl DEL_VLAN_CMD is required for virNetDevDestroyVLanDev +AC_CHECK_DECLS([DEL_VLAN_CMD], [], [], [[#include ]]) + # Check for Linux vs. BSD ifreq members AC_CHECK_MEMBERS([struct ifreq.ifr_newname, struct ifreq.ifr_ifindex, diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ca4a192..f41fa43 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2267,7 +2267,9 @@ virModuleLoad; =20 # util/virnetdev.h virNetDevAddMulti; +virNetDevCreateVLanDev; virNetDevDelMulti; +virNetDevDestroyVLanDev; virNetDevExists; virNetDevFeatureTypeFromString; virNetDevFeatureTypeToString; @@ -2288,10 +2290,12 @@ virNetDevGetRxFilter; virNetDevGetVirtualFunctionIndex; virNetDevGetVirtualFunctionInfo; virNetDevGetVirtualFunctions; +virNetDevGetVLanDevName; virNetDevGetVLanID; virNetDevIfStateTypeFromString; virNetDevIfStateTypeToString; virNetDevIsVirtualFunction; +virNetDevLoad8021Q; virNetDevPFGetVF; virNetDevReadNetConfig; virNetDevRunEthernetScript; diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 8eac419..5425a41 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -43,6 +43,7 @@ # include # include # define VIR_NETDEV_FAMILY AF_UNIX +# include "virkmod.h" #elif defined(HAVE_STRUCT_IFREQ) && defined(AF_LOCAL) # define VIR_NETDEV_FAMILY AF_LOCAL #else @@ -1054,6 +1055,208 @@ int virNetDevGetVLanID(const char *ifname ATTRIBUTE= _UNUSED, #endif /* ! SIOCGIFVLAN */ =20 =20 +#if defined(HAVE_STRUCT_IFREQ) + +# define MODULE_8021Q "8021q" +# define PROC_NET_VLAN_CONFIG "/proc/net/vlan/config" + +static int +controlVlanDev(unsigned int cmd, + const char *ifname, + unsigned int vlanid, + const char *vdname) +{ + int fd; + struct vlan_ioctl_args if_request; + + if (!ifname || strlen(ifname) =3D=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Interface name not provided")); + return -1; + } + + memset(&if_request, 0, sizeof(struct vlan_ioctl_args)); + if_request.cmd =3D cmd; + if (cmd =3D=3D ADD_VLAN_CMD) { + /* for vlan_ioctl_args.devices1[24] */ + if (strlen(ifname) > 24) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Interface name '%s' too long"), + ifname); + return -1; + } + strcpy(if_request.device1, ifname); + if_request.u.VID =3D vlanid; + } else if (cmd =3D=3D DEL_VLAN_CMD) { + if (!vdname) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("vlan-device name not provided")); + return -1; + } + /* for vlan_ioctl_args.devices1[24] */ + if (strlen(vdname) > 24) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("vlan-device name '%s' too long"), + vdname); + return -1; + } + strcpy(if_request.device1, vdname); + } else { + virReportSystemError(ENOSYS, + _("unsupported command option: %d"), + cmd); + return -1; + } + + if ((fd =3D socket(AF_INET, SOCK_STREAM, 0)) < 0) { + virReportSystemError(errno, "%s", + _("unable to open VLAN control socket")); + return -1; + } + + if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) { + virReportSystemError(errno, "%s", + _("control VLAN device error")); + VIR_FORCE_CLOSE(fd); + return -1; + } + + VIR_FORCE_CLOSE(fd); + return 0; +} + + +/** + * virNetDevGetVLanDevName: + * @ifname: name of interface vlan-device depends on + * @vlanid: VLAN ID + * @vdname: used to return the name of vlan-device + * + * Get the name of the vlan-device + * + * Returns 0 on success, -1 on failure + */ +int +virNetDevGetVLanDevName(const char *ifname, unsigned int vlanid, char **vd= name) +{ + if (!ifname || strlen(ifname) =3D=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Interface name not provided")); + return -1; + } + if (virAsprintf(vdname, "%s.%d", ifname, vlanid) < 0) + return -1; + return 0; +} + + +/** + * virNetDevLoad8021Q: + * + * Load 8021q module (since kernel v2.6) + * + * Returns 0 on success, -1 on failure + */ +int +virNetDevLoad8021Q(void) +{ + if (!virFileExists(PROC_NET_VLAN_CONFIG)) { + VIR_AUTOFREE(char *) errbuf =3D NULL; + if ((errbuf =3D virKModLoad(MODULE_8021Q, false))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Failed to load 8021q module")); + return -1; + } + if (!virFileExists(PROC_NET_VLAN_CONFIG)) { + virReportError(VIR_ERR_NO_SUPPORT, "%s", + _("cannot load 8021q module")); + return -1; + } + } + return 0; +} + + +/** + * virNetDevCreateVLanDev: + * @ifname: name of interface we will create vlan-device on + * @vlanid: VLAN ID + * @vdname: used to return the name of vlan-device + * + * Create vlan-device which based on 8021q module. + * + * Returns 0 on success, -1 on failure + */ +int +virNetDevCreateVLanDev(const char *ifname, unsigned int vlanid, char **vdn= ame) +{ + if (controlVlanDev(ADD_VLAN_CMD, ifname, vlanid, NULL) < 0) + return -1; + return virNetDevGetVLanDevName(ifname, vlanid, vdname); +} + + +/** + * virNetDevDestroyVLanDev: + * @ifname: name of interface vlan-device depends on + * @vlanid: VLAN ID + * @vdname: the name of vlan-device + * + * Destroy vlan-device whick has created by virNetDevCreateVLanDev. + */ +void +virNetDevDestroyVLanDev(const char *ifname, + unsigned int vlanid, + const char *vdname) +{ + ignore_value(controlVlanDev(DEL_VLAN_CMD, ifname, vlanid, vdname)); +} + +#else /* !HAVE_STRUCT_IFREQ */ + +int +virNetDevGetVLanDevName(const char *ifname ATTRIBUTE_UNUSED, + unsigned int vlanid ATTRIBUTE_UNUSED, + char **vdname ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENOSYS, "%s", + _("Unable to get vlan-dev name on this platform")= ); + return -1; +} + + +int +virNetDevLoad8021Q(void) +{ + virReportSystemError(ENOSYS, "%s", + _("Unable to load 8021q module on this platform")= ); + return -1; +} + + +int +virNetDevCreateVLanDev(const char *ifname ATTRIBUTE_UNUSED, + unsigned int vlanid ATTRIBUTE_UNUSED, + char **vdname ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENOSYS, "%s", + _("Unable to create vlan-dev on this platform")); + return -1; +} + + +void +virNetDevDestroyVLanDev(const char *ifname ATTRIBUTE_UNUSED, + unsigned int vlanid ATTRIBUTE_UNUSED, + const char *vdname ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENOSYS, "%s", + _("Unable to destroy vlan-dev on this platform")); +} + +#endif /* HAVE_STRUCT_IFREQ */ + + /** * virNetDevValidateConfig: * @ifname: Name of the interface diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h index 8860ea1..d1fb58e 100644 --- a/src/util/virnetdev.h +++ b/src/util/virnetdev.h @@ -207,6 +207,24 @@ int virNetDevGetIndex(const char *ifname, int *ifindex) int virNetDevGetVLanID(const char *ifname, int *vlanid) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; =20 +int virNetDevGetVLanDevName(const char *ifname, + unsigned int vlanid, + char **vdname) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK; + +int virNetDevLoad8021Q(void) + ATTRIBUTE_RETURN_CHECK; + +int virNetDevCreateVLanDev(const char *ifname, + unsigned int vlanid, + char **vdname) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK; + +void virNetDevDestroyVLanDev(const char *ifname, + unsigned int vlanid, + const char *vdname) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3); + int virNetDevGetMaster(const char *ifname, char **master) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK; =20 --=20 2.7.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list