🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #560

[PATCH net-next v4 2/6] tools: ynl: convert enum selector to string for sub-message parsing

#560
From: Hangbin Liu
Date: Thu, 08 Oct 2026 08:24
102 lines
9888 bytes
From: Hangbin Liu <liuhangbin@kylinos.cn>

YNL sub-message parsing expects a string selector for strcmp(). So for
non-external enum selectors, convert the integer value to its string form
via the family's {enum}_str() helper. This enables correct decoding of
sub-messages keyed by enum values.

After the change, if there is no encap_type (e.g. previous ordering on
older kernels), or a new encap_type is missing from the spec file in
future kernel, the code will report "Sub-message key not set", the same
with string lookup fails. With the subsequent rt-route encap spec update,
the newly generated code will look like:

	if (!dst->_present.encap_type)
		return ynl_submsg_failed(yarg, "encap", "encap-type");
	encap_type_str = rt_route_encap_type_str(dst->encap_type);
	if (!encap_type_str)
		return ynl_submsg_failed(yarg, "encap", "enum-lookup-failed");
	if (rt_route_encap_data_parse(&parg, encap_type_str, attr))
		return YNL_PARSE_CB_ERROR;

Signed-off-by: Hangbin Liu <liuhangbin@kylinos.cn>
---

For sashiko:

1. For the extack error-walking path in ynl.c that doesn't handle
   enum-keyed selectors. This series doesn't modify ynl.c, it changes
   the code generator to emit the _str() conversion in generated parsing
   code. The run time error-walking path is a separate concern.

   Since rt-route encap is the first enum-keyed sub-message in the YNL
   specs, this is a new limitation rather than a regression in existing
   functionality. It can be addressed as a follow-up patch to ynl.c.

2. For the selector byte-order issue. This doesn't affect the current
   series. The encap-type selector is type: u16 with no byte-order
   specified (native order), so the raw value passed to _str() is already
   host-order. nftables is in GENS_UNSUP today, so no in-tree generated
   family hits this yet. We address this as a follow-up.
---
 tools/net/ynl/pyynl/ynl_gen_c.py | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index 15c79849c609..4aca51396a9e 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -951,13 +951,31 @@ class TypeSubMessage(TypeNest):
             sel_var = f"_sel_{sel}"
         else:
             sel_var = f"{var}->{sel}"
-        get_lines = [f'if (!{sel_var})',
-                     f'return ynl_submsg_failed(yarg, "{self.name}", "{selector}");',
-                     f"if ({self.nested_render_name}_parse(&parg, {sel_var}, attr))",
-                     "return YNL_PARSE_CB_ERROR;"]
+
+        local_vars = None
+
+        if self.selector.is_enum_val() and not self.selector.is_external():
+            enum = self.family.consts[self.selector.get_enum_name()]
+            pres_var = f"{var}->_present.{sel}"
+            parse_sel = f"{sel}_str"
+            local_vars = [f'const char *{parse_sel};']
+
+            get_lines = [
+                f'if (!{pres_var})',
+                f'return ynl_submsg_failed(yarg, "{self.name}", "{selector}");',
+                f'{parse_sel} = {enum.render_name}_str({sel_var});',
+                f'if (!{parse_sel})',
+                f'return ynl_submsg_failed(yarg, "{self.name}", "enum-lookup-failed");']
+        else:
+            parse_sel = sel_var
+            get_lines = [f'if (!{parse_sel})',
+                         f'return ynl_submsg_failed(yarg, "{self.name}", "{selector}");']
+
+        get_lines += [f"if ({self.nested_render_name}_parse(&parg, {parse_sel}, attr))",
+                      "return YNL_PARSE_CB_ERROR;"]
         init_lines = [f"parg.rsp_policy = &{self.nested_render_name}_nest;",
                       f"parg.data = &{var}->{self.c_name};"]
-        return get_lines, init_lines, None
+        return get_lines, init_lines, local_vars
 
 
 class Selector:
@@ -979,6 +997,12 @@ class Selector:
     def is_external(self):
         return self._external
 
+    def is_enum_val(self):
+        return self.get_enum_name() is not None
+
+    def get_enum_name(self):
+        return self.attr and self.attr.attr.get("enum")
+
 
 class Struct:
     def __init__(self, family, space_name, type_list=None, fixed_header=None,

-- 
2.55.0
Technical Details
Message-ID: <20261008-ynl_rt_encap-v4-2-4ed2ccf7f949@kylinos.cn>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-342816-linux-kernel-netdev=dewy.ru
References: <20261008-ynl_rt_encap-v4-0-4ed2ccf7f949@kylinos.cn>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-342816-linux-kernel-netdev=dewy.ru
From: hangbin.liu@linux.dev (Hangbin Liu)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next v4 2/6] tools: ynl: convert enum selector to
 string for sub-message parsing
Date: Thu, 08 Oct 2026 16:24:02 +0800
Organization: Usenet Network
Message-ID: <20261008-ynl_rt_encap-v4-2-4ed2ccf7f949@kylinos.cn>
References: <20261008-ynl_rt_encap-v4-0-4ed2ccf7f949@kylinos.cn>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Injection-Info: usenet.network;
	logging-data="835960"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:oDE7CcjPyURIQRMMW5iD/H1i3NI= sha256:UK1fC0HRP+kQnM/toRom1Ebfx2/BR8ZwCDYrle7ZmqI=
	sha1:M4N2qgOA6JLsC9LkpBGNHeSIK5o= sha256:9F5/D+sxsvd6MeR3BEWJpcDAG9TNf35S1fjQQskZOYU=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 8E52F10F354; Thu,  8 Oct 2026 11:29:12 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -0.75
X-Spam-Status: No, score=-0.75 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001,
	URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=linux.dev
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 4uTYGsHXZTQ4;
	Thu,  8 Oct 2026 11:29:09 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-342816-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=linux.dev
X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 62C1710F33D; Thu,  8 Oct 2026 11:29:06 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
	by tor.lore.kernel.org (Postfix) with ESMTP id A297A32494; Thu,  8 Oct 2026 08:24:37 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id C4BFA46D55C;
	Thu,  8 Oct 2026 08:24:27 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="c1nCUkSx"
X-Received: from mta1.migadu.com (out-242.mta1.migadu.com [95.215.58.242])
	(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64D2146D2C0
	for <netdev@vger.kernel.org>; Thu,  8 Oct 2026 08:24:25 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.242
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1791447867; cv=none; b=V/Ajd/3dpes03Gw1SxQ8o8nXVXuXhxytuFWayOoPHkAdZoo92GNfksMMJsowmuul9A4XOF8pkY2mwn4SZ+fAl9eCpIk2fPtMxHPRZO8Zufij29/fYayXLILBl2XlzuTp4Ji127Z/WuHykfhybUuqNLA5gNiSdnlwjaDZDGJtjbA=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1791447867; c=relaxed/simple;
	bh=ElAGVQu8HTnftSvAWHnNCFH9IJc4OyG0lDlK5Xphsl8=;
	h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References:
	 In-Reply-To:To:Cc; b=dGyaeQDTZjYtqzynl4J9mjQu0AdKOvo36EToaBUvr6u8hmxQY3FvXDMnmovrUlHFcGt38HsJj2CB3dlgxt/vRAb/tb1bLThWiZgPCtHuf/TASxyT2QsKA0FghUO8hYdnEiqP0KRXBnSE49aDDhIS8bsz4dqWs3dlFZ3DVavTvD4=
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=c1nCUkSx; arc=none smtp.client-ip=95.215.58.242
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev
X-Envelope-To: netdev@vger.kernel.org
X-DKIM-Signature: a=rsa-sha256; bh=ElAGVQu8HTnftSvAWHnNCFH9IJc4OyG0lDlK5Xphsl8=;
 c=simple/simple; d=linux.dev;
 h=from:to:subject:date:message-id:mime-version:content-type; s=key1;
 t=1791447863; v=1; x=1792052663;
 b=c1nCUkSxC1grHucRS0uVoUjPAYD04bdyLXi5F8fhmydLJyvW6GV9Ww2c0jZQOk1SWPRCpxoP
 aex/kZX9a23w5VJa3hPt3ZN2dxQOSaljJhUEFc1VRljWu9K0n3DPU3fmu3lG+jW1Mcqalh5QcfZ
 5oXjOCa6HHFJlYGi6RCej1rI=
X-Envelope-To: netdev@vger.kernel.org
X-Received: by mta10.migadu.com with ESMTPS id 4034c6b1c72ba104;
	Thu, 08 Oct 2026 08:24:23 +0000
X-Mizu-Trace-ID: 4034c6b1c72ba104
X-Migadu-Flow: FLOW_OUT
X-Precedence: bulk
X-Mailing-List: netdev@vger.kernel.org
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-In-Reply-To: <20261008-ynl_rt_encap-v4-0-4ed2ccf7f949@kylinos.cn>
X-Cc: Hangbin Liu <hangbin.liu@linux.dev>, netdev@vger.kernel.org, 
 linux-kernel@vger.kernel.org, bpf@vger.kernel.org, 
 Hangbin Liu <liuhangbin@kylinos.cn>
X-Mailer: b4 0.14.3