From nobody Sun Feb 8 22:21:19 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; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1685705343; cv=none; d=zohomail.com; s=zohoarc; b=T6uj1M879myL73nEBaXXUi1etiRVjA4E/+NenAWLOaZWFz6SCeEvK90V1i4guzAQWwMv4Fj2BSRtahnFfmZw5GrldEbUM+Eavtr0+iHylC4tM5jLuwUDeQ+Rp34tCijGsYq4zF+w5glubOmUCb480Ej6jvmC6n0ypGqi4s1l8dg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685705343; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=fj2jIsdD6S7vq/hAU36SEKBaz47wC3H7C8YqXue7UFM=; b=nWKl80HwpmDpioGlorEdquDY966yJwaxmLZM3E60JzUQHk/DIqcO1wM6YZC2xi4Rz1ZZadpKIdNnwZINp3tMYChPSOMk3EcjgWgzQ/dgNQfEowrjGUNky+wgoEp2AFM2phA3NzjFJ9gNS0/08dv97WeMvPkegX5PA1ITxbncEtg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) 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 1685705343963283.7293792750396; Fri, 2 Jun 2023 04:29:03 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-183-Sp5nFiPyP7CZW022Q8s22A-1; Fri, 02 Jun 2023 07:28:43 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 03CB63C1392F; Fri, 2 Jun 2023 11:28:39 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id E58AE40CFD45; Fri, 2 Jun 2023 11:28:38 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id DB93519465A0; Fri, 2 Jun 2023 11:28:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id C3E0F1946F04 for ; Fri, 2 Jun 2023 11:28:37 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id B499F9E68; Fri, 2 Jun 2023 11:28:37 +0000 (UTC) Received: from speedmetal.lan (unknown [10.45.242.14]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3AEA68162 for ; Fri, 2 Jun 2023 11:28:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685705343; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=fj2jIsdD6S7vq/hAU36SEKBaz47wC3H7C8YqXue7UFM=; b=W/JZS2Puh3Y9rOxHMoYZCm1G6wjB5dbRTsv86+vQzSbcGNKXsVRuh2jlrIofab/rParp9J ic8deECzyrz1xWwfvNRmiIlkyO7AM981cLH6dPmzS/fX2QMl9FrwdAfFBxcoAat3pc2sH4 /Wz02R5ObF0zKbECyET9mYnbvNfo7ds= X-MC-Unique: Sp5nFiPyP7CZW022Q8s22A-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 13/15] virNWFilterRuleDefDetailsFormat: Refactor formatter Date: Fri, 2 Jun 2023 13:28:23 +0200 Message-Id: <9a34f818ef956cde04751c027dee186097678ace.1685705273.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1685705344370100001 Content-Type: text/plain; charset="utf-8" Format the rule attributes in two passes, first for positive 'match' and second pass for negative. This removes the crazy logic for switching between match modes inside the formatter. The refactor makes it also more clear in which cases we actually do format something. Signed-off-by: Peter Krempa --- src/conf/nwfilter_conf.c | 232 +++++++++----------- tests/nwfilterxml2xmlout/quirks-invalid.xml | 2 +- 2 files changed, 107 insertions(+), 127 deletions(-) diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index e6f7c0f8b7..f96ae707f9 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -2701,146 +2701,119 @@ static void virNWFilterRuleDefDetailsFormat(virBuffer *buf, const char *type, const virXMLAttr2Struct *att, - virNWFilterRuleDef *def) + virNWFilterRuleDef *def, + bool negative, + bool *hasAttrs) { - size_t i =3D 0, j; - bool typeShown =3D false; - bool neverShown =3D true; - bool asHex; - enum match { - MATCH_NONE =3D 0, - MATCH_YES, - MATCH_NO - } matchShown =3D MATCH_NONE; - nwItemDesc *item; + g_auto(virBuffer) attrBuf =3D VIR_BUFFER_INITIALIZER; + bool present =3D false; + size_t i; + size_t j; - while (att[i].name) { - virNWFilterEntryItemFlags flags; + if (negative) + virBufferAddLit(&attrBuf, " match=3D'no'"); + + for (i =3D 0; att[i].name; i++) { + nwItemDesc *item; VIR_WARNINGS_NO_CAST_ALIGN item =3D (nwItemDesc *)((char *)def + att[i].dataIdx); VIR_WARNINGS_RESET - flags =3D item->flags; - if ((flags & NWFILTER_ENTRY_ITEM_FLAG_EXISTS)) { - if (!typeShown) { - virBufferAsprintf(buf, "<%s", type); - typeShown =3D true; - neverShown =3D false; - } + if (!(item->flags & NWFILTER_ENTRY_ITEM_FLAG_EXISTS)) + continue; - if ((flags & NWFILTER_ENTRY_ITEM_FLAG_IS_NEG)) { - if (matchShown =3D=3D MATCH_NONE) { - virBufferAddLit(buf, " match=3D'no'"); - matchShown =3D MATCH_NO; - } else if (matchShown =3D=3D MATCH_YES) { - virBufferAddLit(buf, "/>\n"); - typeShown =3D false; - matchShown =3D MATCH_NONE; - continue; - } - } else { - if (matchShown =3D=3D MATCH_NO) { - virBufferAddLit(buf, "/>\n"); - typeShown =3D false; - matchShown =3D MATCH_NONE; - continue; - } - matchShown =3D MATCH_YES; + if (negative !=3D !!(item->flags & NWFILTER_ENTRY_ITEM_FLAG_IS_NEG= )) + continue; + + present =3D true; + *hasAttrs =3D true; + + virBufferAsprintf(&attrBuf, " %s=3D'", att[i].name); + + if (att[i].formatter && !(item->flags & NWFILTER_ENTRY_ITEM_FLAG_H= AS_VAR)) { + if (!att[i].formatter(&attrBuf, def, item)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("formatter for %1$s %2$s reported error"), + type, att[i].name); + return; } + } else if ((item->flags & NWFILTER_ENTRY_ITEM_FLAG_HAS_VAR)) { + virBufferAddChar(&attrBuf, '$'); + virNWFilterVarAccessPrint(item->varAccess, &attrBuf); + } else { - virBufferAsprintf(buf, " %s=3D'", - att[i].name); - if (att[i].formatter && !(flags & NWFILTER_ENTRY_ITEM_FLAG_HAS= _VAR)) { - if (!att[i].formatter(buf, def, item)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("formatter for %1$s %2$s reported error= "), - type, - att[i].name); - return; - } - } else if ((flags & NWFILTER_ENTRY_ITEM_FLAG_HAS_VAR)) { - virBufferAddChar(buf, '$'); - virNWFilterVarAccessPrint(item->varAccess, buf); - } else { - asHex =3D false; - - switch (item->datatype) { - - case DATATYPE_UINT8_HEX: - asHex =3D true; - G_GNUC_FALLTHROUGH; - case DATATYPE_IPMASK: - case DATATYPE_IPV6MASK: - /* display all masks in CIDR format */ - case DATATYPE_UINT8: - virBufferAsprintf(buf, asHex ? "0x%x" : "%d", - item->u.u8); - break; - - case DATATYPE_UINT16_HEX: - asHex =3D true; - G_GNUC_FALLTHROUGH; - case DATATYPE_UINT16: - virBufferAsprintf(buf, asHex ? "0x%x" : "%d", - item->u.u16); - break; - - case DATATYPE_UINT32_HEX: - asHex =3D true; - G_GNUC_FALLTHROUGH; - case DATATYPE_UINT32: - virBufferAsprintf(buf, asHex ? "0x%x" : "%u", - item->u.u32); - break; - - case DATATYPE_IPADDR: - case DATATYPE_IPV6ADDR: - virNWIPAddressFormat(buf, - &item->u.ipaddr); - break; - - case DATATYPE_MACMASK: - case DATATYPE_MACADDR: - for (j =3D 0; j < 6; j++) - virBufferAsprintf(buf, "%02x%s", - item->u.macaddr.addr[j], - (j < 5) ? ":" : ""); - break; - - case DATATYPE_STRINGCOPY: - virBufferEscapeString(buf, "%s", item->u.string); - break; - - case DATATYPE_BOOLEAN: - if (item->u.boolean) - virBufferAddLit(buf, "true"); - else - virBufferAddLit(buf, "false"); - break; - - case DATATYPE_IPSETNAME: - case DATATYPE_IPSETFLAGS: - case DATATYPE_STRING: - case DATATYPE_LAST: - default: - virBufferAsprintf(buf, - "UNSUPPORTED DATATYPE 0x%02x\n", - att[i].datatype); - } + switch (item->datatype) { + + case DATATYPE_UINT8_HEX: + virBufferAsprintf(&attrBuf, "0x%x", item->u.u8); + break; + + case DATATYPE_IPMASK: + case DATATYPE_IPV6MASK: + /* display all masks in CIDR format */ + case DATATYPE_UINT8: + virBufferAsprintf(&attrBuf, "%d", item->u.u8); + break; + + case DATATYPE_UINT16_HEX: + virBufferAsprintf(&attrBuf, "0x%x", item->u.u16); + break; + + case DATATYPE_UINT16: + virBufferAsprintf(&attrBuf, "%d", item->u.u16); + break; + + case DATATYPE_UINT32_HEX: + virBufferAsprintf(&attrBuf, "0x%x", item->u.u32); + break; + + case DATATYPE_UINT32: + virBufferAsprintf(&attrBuf, "%u", item->u.u32); + break; + + case DATATYPE_IPADDR: + case DATATYPE_IPV6ADDR: + virNWIPAddressFormat(&attrBuf, &item->u.ipaddr); + break; + + case DATATYPE_MACMASK: + case DATATYPE_MACADDR: + for (j =3D 0; j < 6; j++) + virBufferAsprintf(&attrBuf, "%02x%s", + item->u.macaddr.addr[j], + (j < 5) ? ":" : ""); + break; + + case DATATYPE_STRINGCOPY: + virBufferEscapeString(&attrBuf, "%s", item->u.string); + break; + + case DATATYPE_BOOLEAN: + if (item->u.boolean) + virBufferAddLit(&attrBuf, "true"); + else + virBufferAddLit(&attrBuf, "false"); + break; + + case DATATYPE_IPSETNAME: + case DATATYPE_IPSETFLAGS: + case DATATYPE_STRING: + case DATATYPE_LAST: + default: + virBufferAsprintf(&attrBuf, + "UNSUPPORTED DATATYPE 0x%02x\n", + att[i].datatype); } - virBufferAddLit(buf, "'"); } - i++; + + virBufferAddLit(&attrBuf, "'"); } - if (typeShown) - virBufferAddLit(buf, "/>\n"); - if (neverShown) - virBufferAsprintf(buf, - "<%s/>\n", type); + if (!present) + return; - return; + virXMLFormatElement(buf, type, &attrBuf, NULL); } @@ -2861,10 +2834,17 @@ virNWFilterRuleDefFormat(virBuffer *buf, virBufferAddLit(&attrBuf, " statematch=3D'false'"); for (i =3D 0; virAttr[i].id; i++) { + bool hasAttrs =3D false; + if (virAttr[i].prtclType !=3D def->prtclType) continue; - virNWFilterRuleDefDetailsFormat(&childBuf, virAttr[i].id, virAttr[= i].att, def); + virNWFilterRuleDefDetailsFormat(&childBuf, virAttr[i].id, virAttr[= i].att, def, false, &hasAttrs); + virNWFilterRuleDefDetailsFormat(&childBuf, virAttr[i].id, virAttr[= i].att, def, true, &hasAttrs); + + if (!hasAttrs) + virBufferAsprintf(&childBuf, "<%s/>\n", virAttr[i].id); + break; } diff --git a/tests/nwfilterxml2xmlout/quirks-invalid.xml b/tests/nwfilterxm= l2xmlout/quirks-invalid.xml index f244d45e08..5159eaf21d 100644 --- a/tests/nwfilterxml2xmlout/quirks-invalid.xml +++ b/tests/nwfilterxml2xmlout/quirks-invalid.xml @@ -1,7 +1,7 @@ 01a992d2-f8c8-7c27-f69b-ab0a9d377379 - + --=20 2.40.1