From patchwork Wed Feb 21 15:54:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13565792 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 D75DA81730 for ; Wed, 21 Feb 2024 15:54:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708530869; cv=none; b=Fgt/Sooi/qo6QAUaabn8XvJaaJH6ko1WJKFduQIhV9ZAuLlypQORvQpnB1KwQCePrF8lhtKjz8kjB53d+4CMCjA1seHWtEll1KkWRObQpvnWrWk3sdMhNxZlisIS2PFV63ZWCL/e4gH80/DrcdLxAESRXn1EuAJuUmrC0d8FdBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708530869; c=relaxed/simple; bh=z4dV5sMV14/n4ROoaWN7EHCC9FKFvm0O8lvWXYfFGC0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WGL9dXh0FLoaLAt2BvWs/vW8vH+p+fg9PV8HwANuS4eM6BsQZeuqnOB043Cr1ozkZ+mLenToKVV2z/Pgs0MBH4qiIx8t/EFD3IOdU5EdcXYqVX+nFuN4Q5j0tfrffPNeplGCj/7EMTliwlGg8Yd/hV2CmnA06SSKKicqOMsZacI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us; spf=none smtp.mailfrom=resnulli.us; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b=U2ydPz8f; arc=none smtp.client-ip=209.85.128.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="U2ydPz8f" Received: by mail-wm1-f48.google.com with SMTP id 5b1f17b1804b1-412698ac6f9so17916135e9.0 for ; Wed, 21 Feb 2024 07:54:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1708530861; x=1709135661; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Igz3Pm9IcLbKinmSkAsWb32QhBDY2If+O3AZdxzSZ90=; b=U2ydPz8fYPNXqddctig4pg7nAo3DncZwszFrSldFzWD2dLBbkvgv5DVklC+mR2I18k NhGcdfcQNfkhc83jeY5lngQkvdp26Jq1Gyiy3hkjh4RtaYsSEa5/Yx6hTI0hX/eO8WHc inb4s+6U7ZXZ/Q6ATxN+EnjLpR3BcvU9IlFb1q04nJZLPlBk6O/AR5cXMMLbgXgaGD5E cE5r6rKH2gB3FENKWPb4vM8kxxrfNGi6Um1mR+Bo7aYqnn7xczEPAWPY1aTWT+jez5Bo d7s09AxXlVAnRpRbzo5Vosqufzw1tKedzAXSxN3s/Ymr+9WZhudkMKQfg114sUJ6SK2D 5qRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708530861; x=1709135661; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Igz3Pm9IcLbKinmSkAsWb32QhBDY2If+O3AZdxzSZ90=; b=iktpxLdedF79MeT3Mu224i9NnqI0q1hRei2hUlyVxEHYkiF68zX2JGfiZExhuiSgA2 x3K5VD0N/s2nOFbd+RsZ2K+QfwrUVU828d7IUj6eosKB8yD3TMPnAXUu9Y0MkfcgoHir 0I/eC+chGNlzj5m7XkXmF5Na6fa6HmnJWfp/+CgBMBXoCirPikIuzux923WmwQLoGNC+ 8zmqodpXZPalB2Xs89u5VgeSIyYlPM9bdM1ijXMOwh86ONe83y1+Tj1RD78bQgJonaLf c3QPnBDk2Pj7y5X4ng8xtiue7R/8xG7CP/6ewh+6N5lDBd7Vlz4GbD9pg1I1rmepb66u XGHQ== X-Gm-Message-State: AOJu0YxilaB88D0VIRS2nQxc19A4WQdZc2Z8hp3FOqtKjk0M3j5j8v4o OJcKzck37j5+jaVlcuUz6Sfo7CPcJTQNAzPGw7pb5aRxBIPNAggV854WwMS31RNUxWns2jzSFTq k X-Google-Smtp-Source: AGHT+IFs1lOHVLamzh8hZ6S0WRRREXlRQPIE5BYgc2No7bVlk1H3WMeJqMe9Gr+mGn9iCCbbdqjbrg== X-Received: by 2002:a05:600c:444a:b0:412:73f5:3aa5 with SMTP id v10-20020a05600c444a00b0041273f53aa5mr2623606wmn.16.1708530861707; Wed, 21 Feb 2024 07:54:21 -0800 (PST) Received: from localhost ([193.47.165.251]) by smtp.gmail.com with ESMTPSA id b7-20020a05600c11c700b00411c3c2fc55sm18115234wmi.45.2024.02.21.07.54.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 07:54:21 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, swarupkotikalapudi@gmail.com, donald.hunter@gmail.com, sdf@google.com, lorenzo@kernel.org, alessandromarcolini99@gmail.com Subject: [patch net-next v2 1/3] tools: ynl: allow user to specify flag attr with bool values Date: Wed, 21 Feb 2024 16:54:13 +0100 Message-ID: <20240221155415.158174-2-jiri@resnulli.us> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240221155415.158174-1-jiri@resnulli.us> References: <20240221155415.158174-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko The flag attr presence in Netlink message indicates value "true", if it is missing in the message it means "false". Allow user to specify attrname with value "true"/"false" in json for flag attrs, treat "false" value properly. Signed-off-by: Jiri Pirko --- v1->v2: - accept other values than "False" --- tools/net/ynl/lib/ynl.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py index f45ee5f29bed..4a44840bab68 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -459,6 +459,8 @@ class YnlFamily(SpecFamily): attr_payload += self._add_attr(attr['nested-attributes'], subname, subvalue, sub_attrs) elif attr["type"] == 'flag': + if not value: + return b'' attr_payload = b'' elif attr["type"] == 'string': attr_payload = str(value).encode('ascii') + b'\x00' From patchwork Wed Feb 21 15:54:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13565794 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) (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 E13F178B70 for ; Wed, 21 Feb 2024 15:54:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708530870; cv=none; b=UHd3iP4tMHDqEEZrZ9i15hsNNoFM7BHjzrJ+l6AXQebF/xGVk6nnkOeyItLPj2wRPsNADeWubt41O+PRWi97rE16mJgZZRcTbA7cpbb18vR1jcioe8bYk26+vTgIJsAOIbS/u0nNZEbftdYnjlvQQyw/XZ8fDwxEqFUxzn9doTc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708530870; c=relaxed/simple; bh=ZAijF8H8mV+HKyhjaM5pp0WYBFh+lTDCmsHsPYS6rK0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MkvdXYpq1WOgmH0q9RC+NTu30v0aIflwh7EiyRMs1kjFUBXpg2jWsiQS/vNuZZVAnlP++Q+Fg5Jv77dE7sEe2A9up/k+BjXb2vbkBsvBu9rzOXofzuJFXWSqVvDiToyLjlragHieF5+r44F20/186QFrmiJi1+j3XQOeVf3Yimc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us; spf=none smtp.mailfrom=resnulli.us; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b=10gV8Hpi; arc=none smtp.client-ip=209.85.208.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="10gV8Hpi" Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-2d220e39907so75391001fa.1 for ; Wed, 21 Feb 2024 07:54:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1708530865; x=1709135665; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WHVvHPCPXKuXh5TNyV3Uph7jNYGzOOiHwYhKYNT3XBk=; b=10gV8Hpi0nqk/bHCPt95I16kEQo39Wh8WSgvnRnGEvhqFtjCTcJJQpOEnL6svJyPMZ OvgTjfQs8+hRHV4/VX8jYqHJjp+iMbJMr9w46b2flrpOaEIxEboAnDYfqwNySyShPAci 8CFCxwZ3d2sfFvvrirVLZRsChFviuJugn5Y5SS/I57iTimGvHXferAmyLcKXoV8JXKPU ExfCoO+Y5XLfIIzhQqss+Gul8MKfvVALVILC0gn6BUH+IBx/O2lVeWbCp6b6kp81ULL/ sjBd1yOFXQmQqkyJKOuuxnWBtu8UGjXlPZAJHEshuIAw2cDhunpuvy9v/Osd7HQsK7kB 2m3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708530865; x=1709135665; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WHVvHPCPXKuXh5TNyV3Uph7jNYGzOOiHwYhKYNT3XBk=; b=IA2Rk1Xj+XZK0zpa1E7RdmB0nfQVMtSNllcBwxah3OTPWe42PNGN+kA/ctQUgMECii EDTtfOwuqYTeBp2NkUjIg4j8wc/jmBdvefo/qXL0CNL8g4cbsY4H1ExjRodluHKeugZY kt2vTEJ+ae07NNcuoIbZTnWEPsqnCKJKwTLOr/MLSVxH9M36rfaxk7amj05LoHdoI9ia jvNngt/1cR958XBnBwctFONKQu7VUnAWIEMLpvt7KksHvfjRdgqxMcmjE7ehmuYPCCFI fAUHZrLNuCI8/YW80c2uya40pEuoBY3Fy6GDaE77TF/yRs+NEAJUCK+LdDGQRzwuoEN9 Gung== X-Gm-Message-State: AOJu0YzTRwa6vz+Ph00F/xWsdw7gEic6S6hoLJVOB+dKDf6NeFy10Vxb aFV+DCZnjychuFWqiADWmh7QogqW6Ew7QrjTJ+oi+YSykp9ejMMhjjOzyiQx/ZwMPkNjRIO9nen I X-Google-Smtp-Source: AGHT+IFFYKz2olECThYSHAYNm/n6LjDCFrNdJTdwunp3Fr6S9Wv8OknO2njthp7mkh6/OSh+wueyJw== X-Received: by 2002:a05:651c:2125:b0:2d2:5cca:cf6f with SMTP id a37-20020a05651c212500b002d25ccacf6fmr308408ljq.18.1708530865337; Wed, 21 Feb 2024 07:54:25 -0800 (PST) Received: from localhost ([193.47.165.251]) by smtp.gmail.com with ESMTPSA id u26-20020a05600c00da00b00410c7912c6esm2894465wmm.14.2024.02.21.07.54.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 07:54:24 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, swarupkotikalapudi@gmail.com, donald.hunter@gmail.com, sdf@google.com, lorenzo@kernel.org, alessandromarcolini99@gmail.com Subject: [patch net-next v2 2/3] tools: ynl: process all scalar types encoding in single elif statement Date: Wed, 21 Feb 2024 16:54:14 +0100 Message-ID: <20240221155415.158174-3-jiri@resnulli.us> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240221155415.158174-1-jiri@resnulli.us> References: <20240221155415.158174-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko As a preparation to handle enums for scalar values, unify the processing of all scalar types in a single elif statement. Signed-off-by: Jiri Pirko Reviewed-by: Donald Hunter --- tools/net/ynl/lib/ynl.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py index 4a44840bab68..38244aff1ec7 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -473,14 +473,14 @@ class YnlFamily(SpecFamily): attr_payload = self._encode_struct(attr.struct_name, value) else: raise Exception(f'Unknown type for binary attribute, value: {value}') - elif attr.is_auto_scalar: + elif attr['type'] in NlAttr.type_formats or attr.is_auto_scalar: scalar = int(value) - real_type = attr["type"][0] + ('32' if scalar.bit_length() <= 32 else '64') - format = NlAttr.get_format(real_type, attr.byte_order) - attr_payload = format.pack(int(value)) - elif attr['type'] in NlAttr.type_formats: - format = NlAttr.get_format(attr['type'], attr.byte_order) - attr_payload = format.pack(int(value)) + if attr.is_auto_scalar: + attr_type = attr["type"][0] + ('32' if scalar.bit_length() <= 32 else '64') + else: + attr_type = attr["type"] + format = NlAttr.get_format(attr_type, attr.byte_order) + attr_payload = format.pack(scalar) elif attr['type'] in "bitfield32": attr_payload = struct.pack("II", int(value["value"]), int(value["selector"])) elif attr['type'] == 'sub-message': From patchwork Wed Feb 21 15:54:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13565795 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (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 0BCA481740 for ; Wed, 21 Feb 2024 15:54:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708530872; cv=none; b=WGC9pQFX0WTfaQH5qCntCkO1uDzneXGj0uRBBV7ZXLpIGsUy9wA0+9odf9i4VPNu65WcLReQgQn2aQw/MMetA2eHlgdTDDabCJmw1VOzaU3xcTheu4xHSQ3j8EnO02BK1UN8uVaYfatgf2xJ/5Ws1LaD+77R1Cz1fkxxShU26I4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708530872; c=relaxed/simple; bh=CK2Zn7IT1YhyI394J2tFidsf2bcKR/w+ZOUToE4nD+A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JpV68aIGpNIMkBhsaCWBd/dztbnZ0xXhPyIMMGu+Xg2he4mCNq/dha1Gu83tIsnDulFYXVbxt19d9AzZXAjA9AHwb+DjwDvT2McmNOpT45JS9tmXWjztXemL8jZyf+AbnGDWnj/sGhllJdEFkdGZMww6vyUdJrHGUFbtXdRaRWU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us; spf=none smtp.mailfrom=resnulli.us; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b=p3rAnV5j; arc=none smtp.client-ip=209.85.221.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=resnulli.us Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=resnulli.us Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=resnulli-us.20230601.gappssmtp.com header.i=@resnulli-us.20230601.gappssmtp.com header.b="p3rAnV5j" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-33d4c11bd34so2621423f8f.3 for ; Wed, 21 Feb 2024 07:54:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1708530869; x=1709135669; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QgSKmQ6GQ4Auw6c9me4dvApmwDymjR34/yKN41/IEuk=; b=p3rAnV5jeLu29umGiwDOSUY4yEaqXn4dIGrE9RToCH+gJOknQ5EEfbgV+jdUU3fHRN Wa1yqzc1nGdD8vW++wa4boYfNnBbBTpO+WgVnp97soV2YT2VtA5f3IvBFrvcH/IocASq g4wNbbfgRvxTxuOTLZ93I7KE0Y+W4v//KcbBn0CaKBYijgTXgQKtnzwjGCib59NB+I+K xNQEyGQhKs3mS795S0MsJv+XgR1FeZl5DBtFIWXHWQuxFw/tGkrOTPSRNwXBJ9nW2Mw6 f9GcTp7Jxwy78D3aNFS0IjoeBJTHNJ/hLDQDbagolrZx9i71D+SSB9msPKLeCBs37r23 DN3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708530869; x=1709135669; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QgSKmQ6GQ4Auw6c9me4dvApmwDymjR34/yKN41/IEuk=; b=woJ3lhK2hPYdXHgGD0GnPNxJm+End953IpaIHetQyjSOloxT4ZWqsXqE82JcyuyKos D48+Y1Bo8wqzXml0zImsv8CcX5+tF0pq2whyj2cKvC4D9SBUNhwdqJBY4WCHJrj/PYxQ sUuPHs8XkunB3Xvd8x9UUtgyjHodbZkM2uFef+Wl+HePerXaUSR+TIZWLXfYdZdXOXaH 3z0uuD+2jMhe7iNBarIpa8+cOxY4Pz79kCm33LE9VHmxzGD3D7Ca22WplDfDhIwocRgQ PyHKGUrW+yH86cGxkCxHcYlyJYsrEPDF2sITKbATCJSd1jaYE9IJPe9+6QtBMkMqhHBw 9C0Q== X-Gm-Message-State: AOJu0YyOrjuI3Qod3a5zCIPSZ+1p4x11jAwUaw5CE0976dhawrP0uu2m BE9zTb/qkFLYlL4L0wRgp17hAeuXlAFZVPyd9gGqlWJe1k9qutMtzi2FTlT9ue6JRo3UwLSuHZz R X-Google-Smtp-Source: AGHT+IFoQYAZ0KHv9nNx69XV4lZlsycYvobBx+7HiqFb8cysWfby4JF79Of2Pa/3RuYxKg0G9GuvbQ== X-Received: by 2002:a5d:64ab:0:b0:33d:7606:808b with SMTP id m11-20020a5d64ab000000b0033d7606808bmr2897720wrp.68.1708530869185; Wed, 21 Feb 2024 07:54:29 -0800 (PST) Received: from localhost ([193.47.165.251]) by smtp.gmail.com with ESMTPSA id by1-20020a056000098100b0033d873f08d4sm368410wrb.98.2024.02.21.07.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 07:54:28 -0800 (PST) From: Jiri Pirko To: netdev@vger.kernel.org Cc: kuba@kernel.org, pabeni@redhat.com, davem@davemloft.net, edumazet@google.com, jacob.e.keller@intel.com, swarupkotikalapudi@gmail.com, donald.hunter@gmail.com, sdf@google.com, lorenzo@kernel.org, alessandromarcolini99@gmail.com Subject: [patch net-next v2 3/3] tools: ynl: allow user to pass enum string instead of scalar value Date: Wed, 21 Feb 2024 16:54:15 +0100 Message-ID: <20240221155415.158174-4-jiri@resnulli.us> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240221155415.158174-1-jiri@resnulli.us> References: <20240221155415.158174-1-jiri@resnulli.us> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org From: Jiri Pirko During decoding of messages coming from kernel, attribute values are converted to enum names in case the attribute type is enum of bitfield32. However, when user constructs json message, he has to pass plain scalar values. See "state" "selector" and "value" attributes in following examples: $ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/dpll.yaml --do pin-set --json '{"id": 0, "parent-device": {"parent-id": 0, "state": 1}}' $ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/devlink.yaml --do port-set --json '{"bus-name": "pci", "dev-name": "0000:08:00.1", "port-index": 98304, "port-function": {"caps": {"selector": 1, "value": 1 }}}' Allow user to pass strings containing enum names, convert them to scalar values to be encoded into Netlink message: $ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/dpll.yaml --do pin-set --json '{"id": 0, "parent-device": {"parent-id": 0, "state": "connected"}}' $ sudo ./tools/net/ynl/cli.py --spec Documentation/netlink/specs/devlink.yaml --do port-set --json '{"bus-name": "pci", "dev-name": "0000:08:00.1", "port-index": 98304, "port-function": {"caps": {"selector": ["roce-bit"], "value": ["roce-bit"] }}}' Signed-off-by: Jiri Pirko Reviewed-by: Donald Hunter --- v1->v2: - s/_get_scalar/_encode_enum/ - accept flat name not in a list --- tools/net/ynl/lib/ynl.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py index 38244aff1ec7..14ae30db984a 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -438,6 +438,23 @@ class YnlFamily(SpecFamily): self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP, mcast_id) + def _encode_enum(self, attr_spec, value): + try: + return int(value) + except (ValueError, TypeError) as e: + if 'enum' not in attr_spec: + raise e + enum = self.consts[attr_spec['enum']] + if enum.type == 'flags' or attr_spec.get('enum-as-flags', False): + scalar = 0 + if isinstance(value, str): + value = [value] + for single_value in value: + scalar += enum.entries[single_value].user_value(as_flags = True) + return scalar + else: + return enum.entries[value].user_value() + def _add_attr(self, space, name, value, search_attrs): try: attr = self.attr_sets[space][name] @@ -474,7 +491,7 @@ class YnlFamily(SpecFamily): else: raise Exception(f'Unknown type for binary attribute, value: {value}') elif attr['type'] in NlAttr.type_formats or attr.is_auto_scalar: - scalar = int(value) + scalar = self._encode_enum(attr, value) if attr.is_auto_scalar: attr_type = attr["type"][0] + ('32' if scalar.bit_length() <= 32 else '64') else: @@ -482,7 +499,9 @@ class YnlFamily(SpecFamily): format = NlAttr.get_format(attr_type, attr.byte_order) attr_payload = format.pack(scalar) elif attr['type'] in "bitfield32": - attr_payload = struct.pack("II", int(value["value"]), int(value["selector"])) + scalar_value = self._encode_enum(attr, value["value"]) + scalar_selector = self._encode_enum(attr, value["selector"]) + attr_payload = struct.pack("II", scalar_value, scalar_selector) elif attr['type'] == 'sub-message': msg_format = self._resolve_selector(attr, search_attrs) attr_payload = b''