From nobody Mon Feb 9 16:01:00 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=126.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1624873787727370.16192524214375; Mon, 28 Jun 2021 02:49:47 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-28-e9skOBHiPuutIWoOIwODRA-1; Mon, 28 Jun 2021 05:49:44 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9F4B1106B7DE; Mon, 28 Jun 2021 09:49:39 +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 76FC41A26A; Mon, 28 Jun 2021 09:49:39 +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 112AA1809CB4; Mon, 28 Jun 2021 09:49:39 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 15S9nDms008646 for ; Mon, 28 Jun 2021 05:49:13 -0400 Received: by smtp.corp.redhat.com (Postfix) id 523B24E3C4; Mon, 28 Jun 2021 09:49:13 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4C7234C834 for ; Mon, 28 Jun 2021 09:49:09 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CD1F689C7DD for ; Mon, 28 Jun 2021 09:49:09 +0000 (UTC) Received: from m15112.mail.126.com (m15112.mail.126.com [220.181.15.112]) by relay.mimecast.com with ESMTP id us-mta-401-TSEYFPONOg2PhIWswvETcA-1; Mon, 28 Jun 2021 05:49:05 -0400 Received: from zhangjl0200.home.langchao.com (unknown [58.56.96.28]) by smtp2 (Coremail) with SMTP id DMmowAB3fVXik9lg0G6wCg--.64150S4; Mon, 28 Jun 2021 17:18:40 +0800 (CST) X-MC-Unique: e9skOBHiPuutIWoOIwODRA-1 X-MC-Unique: TSEYFPONOg2PhIWswvETcA-1 From: zhangjl02 To: libvir-list@redhat.com Subject: [PATCH 2/4] qemu: interface: add virNetDevOpenvswitchInterfaceSetQos and virNetDevOpenvswitchInterfaceClearQos Date: Mon, 28 Jun 2021 17:18:21 +0800 Message-Id: <20210628091823.2345-3-jx8zjs@126.com> In-Reply-To: <20210628091823.2345-1-jx8zjs@126.com> References: <20210628091823.2345-1-jx8zjs@126.com> MIME-Version: 1.0 X-CM-TRANSID: DMmowAB3fVXik9lg0G6wCg--.64150S4 X-Coremail-Antispam: 1Uf129KBjvAXoW3Cw4DGr17GF43Cr13ZrW7XFb_yoW8JF13Ao W3Gr47Kr1DW34UuanYyan7G3yUAr15KrsxWr4Y9ayDJry8Gr9YvrykAan8AFsaqFW8Ar17 WF1SgrW3GFsxCw1fn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUTFApUUUUU X-Originating-IP: [58.56.96.28] X-CM-SenderInfo: dm0y6ybv6rjloofrz/1tbinxG-SFpEB0CGDwAAsp X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com X-Mailman-Approved-At: Mon, 28 Jun 2021 05:49:29 -0400 Cc: zhangjl02 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: zhangjl02 Introduce qos setting and cleaning method. Use ovs command to set qos parameters on specific interface of qemu virtual machine. When an ovs port is created, we add 'ifname' to external-ids. When setting qos on an ovs port, query its qos and queue. If found, change qos on queried queue and qos, otherwise create new queue and qos. When cleaning qos, query and clean queues and qos in ovs table record by 'ifname' and 'vmid'. --- src/libvirt_private.syms | 2 + src/util/virnetdevopenvswitch.c | 269 ++++++++++++++++++++++++++++++++ src/util/virnetdevopenvswitch.h | 11 ++ 3 files changed, 282 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 68e4b6aab8..775f6706b3 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2805,8 +2805,10 @@ virNetDevMidonetUnbindPort; virNetDevOpenvswitchAddPort; virNetDevOpenvswitchGetMigrateData; virNetDevOpenvswitchGetVhostuserIfname; +virNetDevOpenvswitchInterfaceClearQos; virNetDevOpenvswitchInterfaceGetMaster; virNetDevOpenvswitchInterfaceParseStats; +virNetDevOpenvswitchInterfaceSetQos; virNetDevOpenvswitchInterfaceStats; virNetDevOpenvswitchMaybeUnescapeReply; virNetDevOpenvswitchRemovePort; diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitc= h.c index eac68d9556..f27b74f35f 100644 --- a/src/util/virnetdevopenvswitch.c +++ b/src/util/virnetdevopenvswitch.c @@ -30,6 +30,7 @@ #include "virlog.h" #include "virjson.h" #include "virfile.h" +#include "virutil.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE =20 @@ -140,6 +141,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, con= st char *ifname, g_autofree char *ifaceid_ex_id =3D NULL; g_autofree char *profile_ex_id =3D NULL; g_autofree char *vmid_ex_id =3D NULL; + g_autofree char *ifname_ex_id =3D NULL; =20 virMacAddrFormat(macaddr, macaddrstr); virUUIDFormat(ovsport->interfaceID, ifuuidstr); @@ -149,6 +151,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, con= st char *ifname, macaddrstr); ifaceid_ex_id =3D g_strdup_printf("external-ids:iface-id=3D\"%s\"", if= uuidstr); vmid_ex_id =3D g_strdup_printf("external-ids:vm-id=3D\"%s\"", vmuuidst= r); + ifname_ex_id =3D g_strdup_printf("external-ids:ifname=3D\"%s\"", ifnam= e); if (ovsport->profileID[0] !=3D '\0') { profile_ex_id =3D g_strdup_printf("external-ids:port-profile=3D\"%= s\"", ovsport->profileID); @@ -174,6 +177,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, con= st char *ifname, "--", "set", "Interface", ifname, ifaceid_ex_= id, "--", "set", "Interface", ifname, vmid_ex_id, "--", "set", "Interface", ifname, profile_ex_= id, + "--", "set", "Interface", ifname, ifname_ex_i= d, "--", "set", "Interface", ifname, "external-ids:iface-status=3Dactive", NULL); @@ -614,3 +618,268 @@ int virNetDevOpenvswitchUpdateVlan(const char *ifname, =20 return 0; } + + +/** + * virNetDevOpenvswitchInterfaceSetQos: + * @ifname: on which interface + * @bandwidth: rates to set (may be NULL) + * @swapped: true if IN/OUT should be set contrariwise + * + * Update qos configuration of an OVS port. + * + * If @swapped is set, the IN part of @bandwidth is set on + * @ifname's TX, and vice versa. If it is not set, IN is set on + * RX and OUT on TX. This is because for some types of interfaces + * domain and the host live on the same side of the interface (so + * domain's RX/TX is host's RX/TX), and for some it's swapped + * (domain's RX/TX is hosts's TX/RX). + * + * Return 0 on success, -1 otherwise. + */ +int virNetDevOpenvswitchInterfaceSetQos(const char *ifname, + const virNetDevBandwidth *bandwidt= h, + const unsigned char *vmid, + bool swapped) +{ + int ret =3D -1; + char vmuuidstr[VIR_UUID_STRING_BUFLEN]; + virNetDevBandwidthRate *rx =3D NULL; /* From domain POV */ + virNetDevBandwidthRate *tx =3D NULL; /* From domain POV */ + virCommand *cmd =3D NULL; + char *vmid_ex_id =3D NULL; + char *ifname_ex_id =3D NULL; + char *average =3D NULL; + char *peak =3D NULL; + char *burst =3D NULL; + char *qos_uuid =3D NULL; + char *queue_uuid =3D NULL; + char **lines =3D NULL; + + if (!bandwidth) { + /* nothing to be enabled */ + ret =3D 0; + goto cleanup; + } + + if (geteuid() !=3D 0) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("Network bandwidth tuning is not available" + " in session mode")); + return -1; + } + + if (!ifname) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("Unable to set bandwidth for interface because " + "device name is unknown")); + return -1; + } + + if (swapped) { + rx =3D bandwidth->out; + tx =3D bandwidth->in; + } else { + rx =3D bandwidth->in; + tx =3D bandwidth->out; + } + if(!bandwidth->out && !bandwidth->in) { + ret =3D virNetDevOpenvswitchInterfaceClearQos(ifname, vmid); + // virNetDevBandwidthClear(iframe); + } + if (tx && tx->average) { + average =3D g_strdup_printf("%llu", tx->average * 8192); + if (tx->burst) + burst =3D g_strdup_printf("%llu", tx->burst * 8192); + if (tx->peak) + peak =3D g_strdup_printf("%llu", tx->peak * 8192); + + // find queue + cmd =3D virNetDevOpenvswitchCreateCmd(); + virUUIDFormat(vmid, vmuuidstr); + vmid_ex_id =3D g_strdup_printf("external-ids:vm-id=3D\"%s\"", vmuu= idstr); + ifname_ex_id =3D g_strdup_printf("external-ids:ifname=3D\"%s\"", i= fname); + virCommandAddArgList(cmd, "--no-heading", "--columns=3D_uuid", "fi= nd", "queue", vmid_ex_id, ifname_ex_id,NULL); + virCommandSetOutputBuffer(cmd, &queue_uuid); + if (virCommandRun(cmd, NULL) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find queue on port %s"), ifname); + } + + // find qos + cmd =3D virNetDevOpenvswitchCreateCmd(); + virCommandAddArgList(cmd, "--no-heading", "--columns=3D_uuid", "fi= nd", "qos", vmid_ex_id, ifname_ex_id,NULL); + virCommandSetOutputBuffer(cmd, &qos_uuid); + if (virCommandRun(cmd, NULL) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find qos on port %s"), ifname); + } + + // create qos and set + cmd =3D virNetDevOpenvswitchCreateCmd(); + if (queue_uuid && *queue_uuid) { + lines =3D g_strsplit(queue_uuid, "\n", 0); + virCommandAddArgList(cmd, "set", "queue", lines[0], NULL); + }else{ + virCommandAddArgList(cmd, "set", "port", ifname, "qos=3D@qos1"= , vmid_ex_id, ifname_ex_id, + "--", "--id=3D@qos1", "create", "qos", "t= ype=3Dlinux-htb", NULL); + virCommandAddArgFormat(cmd, "other_config:min-rate=3D%s", aver= age); + if(burst){ + virCommandAddArgFormat(cmd, "other_config:burst=3D%s", bur= st); + } + if(peak){ + virCommandAddArgFormat(cmd, "other_config:max-rate=3D%s", = peak); + } + virCommandAddArgList(cmd, "queues:0=3D@queue0", vmid_ex_id, if= name_ex_id, + "--", "--id=3D@queue0", "create", "queue"= , NULL); + } + virCommandAddArgFormat(cmd, "other_config:min-rate=3D%s", average); + if(burst){ + virCommandAddArgFormat(cmd, "other_config:burst=3D%s", burst); + } + if(peak){ + virCommandAddArgFormat(cmd, "other_config:max-rate=3D%s", peak= ); + } + virCommandAddArgList(cmd, vmid_ex_id, ifname_ex_id, NULL); + if (virCommandRun(cmd, NULL) < 0) { + if(*queue_uuid){ + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to set queue configuration on por= t %s"), ifname); + } + else + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to create and set qos configurati= on on port %s"), ifname); + goto cleanup; + } + + if(qos_uuid && *qos_uuid){ + cmd =3D virNetDevOpenvswitchCreateCmd(); + lines =3D g_strsplit(qos_uuid, "\n", 0); + virCommandAddArgList(cmd, "set", "qos", lines[0], NULL); + virCommandAddArgFormat(cmd, "other_config:min-rate=3D%s", aver= age); + if(burst){ + virCommandAddArgFormat(cmd, "other_config:burst=3D%s", bur= st); + } + if(peak){ + virCommandAddArgFormat(cmd, "other_config:max-rate=3D%s", = peak); + } + virCommandAddArgList(cmd, vmid_ex_id, ifname_ex_id, NULL); + if (virCommandRun(cmd, NULL) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to set qos configuration on port = %s"), ifname); + goto cleanup; + } + } + + // ret =3D virNetDevBandwidthSet(ifname,bandwidth,false,swapped); + } + + if (rx) { + average =3D g_strdup_printf("%llu", rx->average * 8); + if (rx->burst) + burst =3D g_strdup_printf("%llu", rx->burst * 8); + cmd =3D virNetDevOpenvswitchCreateCmd(); + virCommandAddArgList(cmd, "set", "Interface", ifname, NULL); + virCommandAddArgFormat(cmd, "ingress_policing_rate=3D%s", average); + if (burst) + virCommandAddArgFormat(cmd, "ingress_policing_burst=3D%s", bur= st); + + if (virCommandRun(cmd, NULL) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to set vlan configuration on port %s"= ), ifname); + goto cleanup; + } + } + + return 0; + + cleanup: + virCommandFree(cmd); + VIR_FREE(average); + VIR_FREE(peak); + VIR_FREE(burst); + VIR_FREE(qos_uuid); + VIR_FREE(queue_uuid); + VIR_FREE(vmid_ex_id); + VIR_FREE(ifname_ex_id); + VIR_FREE(lines); + return ret; +} + +int virNetDevOpenvswitchInterfaceClearQos(const char *ifname, + const unsigned char *vmid){ + char vmuuidstr[VIR_UUID_STRING_BUFLEN]; + virCommand *cmd =3D NULL; + char *vmid_ex_id =3D NULL; + char *qos_uuid =3D NULL; + char *queue_uuid =3D NULL; + char **lines =3D NULL; + size_t i; + + // find qos + cmd =3D virNetDevOpenvswitchCreateCmd(); + virUUIDFormat(vmid, vmuuidstr); + vmid_ex_id =3D g_strdup_printf("external-ids:vm-id=3D\"%s\"", vmuuidst= r); + virCommandAddArgList(cmd, "--no-heading", "--columns=3D_uuid", "find",= "qos", vmid_ex_id, NULL); + virCommandSetOutputBuffer(cmd, &qos_uuid); + if (virCommandRun(cmd, NULL) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find qos on port %s"), ifname); + } + + // find queue + cmd =3D virNetDevOpenvswitchCreateCmd(); + vmid_ex_id =3D g_strdup_printf("external-ids:vm-id=3D\"%s\"", vmuuidst= r); + virCommandAddArgList(cmd, "--no-heading", "--columns=3D_uuid", "find",= "queue", vmid_ex_id, NULL); + virCommandSetOutputBuffer(cmd, &queue_uuid); + if (virCommandRun(cmd, NULL) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to find queue on port %s"), ifname); + } + + if (qos_uuid && *qos_uuid) { + lines =3D g_strsplit(qos_uuid, "\n", 0); + // destroy qos + for (i =3D 0; lines[i] !=3D NULL; i++) { + const char *line =3D lines[i]; + if (!*line) { + continue; + } + cmd =3D virNetDevOpenvswitchCreateCmd(); + virCommandAddArgList(cmd, "remove", "port", ifname, "qos", lin= e, NULL); + if (virCommandRun(cmd, NULL) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to remove port qos on port %s"), = ifname); + } + cmd =3D virNetDevOpenvswitchCreateCmd(); + virCommandAddArgList(cmd, "destroy", "qos", line, NULL); + if (virCommandRun(cmd, NULL) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to destroy qos on port %s"), ifna= me); + } + } + } + // destroy queue + if (queue_uuid && *queue_uuid) { + lines =3D g_strsplit(queue_uuid, "\n", 0); + for (i =3D 0; lines[i] !=3D NULL; i++) { + const char *line =3D lines[i]; + if (!*line) { + continue; + } + cmd =3D virNetDevOpenvswitchCreateCmd(); + virCommandAddArgList(cmd, "destroy", "queue", line, NULL); + if (virCommandRun(cmd, NULL) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to destroy queue on port %s"), if= name); + } + } + } + + virCommandFree(cmd); + VIR_FREE(qos_uuid); + VIR_FREE(queue_uuid); + VIR_FREE(vmid_ex_id); + VIR_FREE(lines); + return 0; +} \ No newline at end of file diff --git a/src/util/virnetdevopenvswitch.h b/src/util/virnetdevopenvswitc= h.h index 7525376855..2dcd1aec6b 100644 --- a/src/util/virnetdevopenvswitch.h +++ b/src/util/virnetdevopenvswitch.h @@ -21,6 +21,7 @@ #pragma once =20 #include "internal.h" +#include "virnetdevbandwidth.h" #include "virnetdevvportprofile.h" #include "virnetdevvlan.h" =20 @@ -69,3 +70,13 @@ int virNetDevOpenvswitchGetVhostuserIfname(const char *p= ath, int virNetDevOpenvswitchUpdateVlan(const char *ifname, const virNetDevVlan *virtVlan) ATTRIBUTE_NONNULL(1) G_GNUC_WARN_UNUSED_RESULT; + +int virNetDevOpenvswitchInterfaceSetQos(const char *ifname, + const virNetDevBandwidth *bandwidt= h, + const unsigned char *vmid, + bool swapped) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) G_GNUC_WARN_UNUSED_RESULT; + +int virNetDevOpenvswitchInterfaceClearQos(const char *ifname, + const unsigned char *vmid) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) G_GNUC_WARN_UNUSED_RESULT; --=20 2.30.2.windows.1