From patchwork Thu Feb 22 13:43:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13567352 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (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 B6D5A12FB02 for ; Thu, 22 Feb 2024 13:43:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708609441; cv=none; b=qc6Qi7WNmIgY0TswwK5c+83BSH00s+U15vbk7LcDvuzI6YaNSxxPARf3Qz9Wym+gquO1MJgKnwijoHDhpFqC/vUUs8fH0LB75E2lFcxnS5f4Qk99lri0fo9fYX+9naw10WzqmtYUKRiNUbyGOI+lCBTnn0O31t3vZDFHiS1gEkQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708609441; c=relaxed/simple; bh=ODfFoL+MWBN1dx66G2oN8UVXEmbt7bcvqwfrnmJQkDY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L92gUgJn85tEk5ejAUpyRTq1iUYksk250a80OP9NpBuNoTQThqGy+L3nRiyf3ocj7+SOKZouO5cMZ9Zx3tZjp2rx9perV9MVbF642IN0bH0o0CZ93A1NjWxSKT4HiQsv2UTZ3lssUKvJWfsmqakUZRhJ7B1aPKML3r1JUGepIes= 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=P4Y1xuiL; arc=none smtp.client-ip=209.85.221.44 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="P4Y1xuiL" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-33d146737e6so5239256f8f.0 for ; Thu, 22 Feb 2024 05:43:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1708609438; x=1709214238; 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=/23d48srVTCVecddwtQXX/AMBM+Sc7WTVbaCUJYThpY=; b=P4Y1xuiLH7/yPXnd6eg/B6DEBMiko6YbZNhkCYSjI5YDTrhjQ3m7HFVKSVkzsm7BKy Eh98tv8Y8f5moNB3cT5NhXzbGfIiYtFq8CyVNboKxVSsOv4E6OPzQeQePE4mhPeHh2k2 VXfWhp4OdZub+w8vYJlAC0Qog7FDWOZ6weS1ARGu6oWt91Vm+kOUvG1JWdfQ6vtpjsPL a6ASVaa84RqgAx2wgjU5gr7BMz/iHl7sdQHjN/ySus1G3rjwBHDKAy0AROqDftfINBvt Us9O4ussCleI/0QaIwVkCR/xNfcoAdmm8sXbYoNOAVRg4uJst+duV5r8gcRPf7AMnj1f TNog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708609438; x=1709214238; 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=/23d48srVTCVecddwtQXX/AMBM+Sc7WTVbaCUJYThpY=; b=oSpeSzXCgSsuyWWl+zhVG0sN+g2uv8O64uJzzviofkIPd3tLyARmMYpHKsFpLoOFyS ztbRVtmLnGqIcf7GG8esuvFtfLlWSPxKo9fShF9otogZISjkJXmuVuZnkSieROLVyuUU sCnJQyEPdo/eJ6Txv8C3m/6QXBMRKHijbFI/ugqaYuglPiup13FbAAZf2p2DwQfTpoZH EwxcMNYfmzr/OmrT+RMxN8BcKbqLES6zEpFV+cRl8S7qQRxdFdeLWC+ZTHEoYNgftD3h ZMKBoJn5INDWGkYFKXuLeLGrn2GGVXqItJBPKBl8tw/YqURxc+tcr2whJieHkEKRQ+1j 4Mkw== X-Gm-Message-State: AOJu0YzmiuA8JKYdF1tXNcu+s6l4HbCuDunB8iBhYh8IKNyrTLIEHZMr hzzUMxl+C5O/ClYU1hyGpCtt+URZ5ySkdVELWxENbpLh5Hdo1zf0oa/Cr9lI/KGAnnnL4L8tRIq b X-Google-Smtp-Source: AGHT+IFkcjpDOj8k+YpdNMnvZQ0qgVglPP1VLthnBbHX4MzocvZ09b7LSDyw6W54FVf9ftYLefmcVg== X-Received: by 2002:adf:e705:0:b0:33d:855b:78f9 with SMTP id c5-20020adfe705000000b0033d855b78f9mr2548600wrm.41.1708609437997; Thu, 22 Feb 2024 05:43:57 -0800 (PST) Received: from localhost ([193.47.165.251]) by smtp.gmail.com with ESMTPSA id bw4-20020a0560001f8400b0033d297c9118sm18540293wrb.24.2024.02.22.05.43.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 05:43:57 -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 v3 1/3] tools: ynl: allow user to specify flag attr with bool values Date: Thu, 22 Feb 2024 14:43:49 +0100 Message-ID: <20240222134351.224704-2-jiri@resnulli.us> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240222134351.224704-1-jiri@resnulli.us> References: <20240222134351.224704-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 Reviewed-by: Donald Hunter --- v2->V3: - add comment v1->v2: - accept other values than "False" --- tools/net/ynl/lib/ynl.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py index f45ee5f29bed..1c5c7662dc9a 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -459,6 +459,9 @@ class YnlFamily(SpecFamily): attr_payload += self._add_attr(attr['nested-attributes'], subname, subvalue, sub_attrs) elif attr["type"] == 'flag': + if not value: + # If value is absent or false then skip attribute creation. + return b'' attr_payload = b'' elif attr["type"] == 'string': attr_payload = str(value).encode('ascii') + b'\x00' From patchwork Thu Feb 22 13:43:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13567353 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 B094012FB02 for ; Thu, 22 Feb 2024 13:44:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708609445; cv=none; b=ZbzvOwEMTnxDcGFvO/jNYEsbiFdPMFajI5VEab9Eg5eq5DDQv9Mg8SqHjcLmObJ3xcJ3MEWsh7JzlReWlpowXclPWHo73E7SBr9saPFnXR+RW1OIzfJGvdTdvhdkqRaT641VGc8j1bN53QQLV2JRXmdpRcEpaOtOa6eZ3zI72jo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708609445; c=relaxed/simple; bh=0lDaAsR4W7Z0DjQ+oq/bkBeHfxUAlWrt6e8jEuJOfOk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MiZdyCiJy+CotDuHCphS9mr6zvUf64+iz7GjjD67NI3y/QqAhU1puRwCamo9JCliH/C9OsYvCxjdTwVS5q9DavHEodQiFn2jBHvmcAtEKMTkRpxBU30glpN9y2bp3V0L2VRJ7nYu2rdArj0zClWSIR4br/K6T8q4wYkHfWH1bpE= 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=2ekzz9dE; arc=none smtp.client-ip=209.85.221.52 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="2ekzz9dE" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-33d36736d4eso3578632f8f.1 for ; Thu, 22 Feb 2024 05:44:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1708609442; x=1709214242; 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=jMPthOOurzrYeChkIv/lThMImRodm5q1y0tKDgpysO4=; b=2ekzz9dE2BDNiQmhPbmkLIXsL/CIiZluur3abIMUZLMt+wgWMI4TU/42HlnZ3X68iE AyaTxIwrkhlSA/hQlZ+CpCNHC+8U3iZHxh5rDigjrZyBGwN3oE7VwwkNkJFk6SQFPUX+ olUBYC+Ag7sBSxxofDNhuZ47kuRKsXztoTS5lRyfJjzOggXF/4WVRFwIyJnqX0wJgEj2 GscLlaQOkD9B8P/mX4YhNOsEHPsobCt8F+WhS+SukpDCbsh036A+g/jeP67HFnPIa/9v WBo8xlzHSAfwpaD4bhnTY5zsEk712z1QjoqFXzpDwPKc8rwKGKv/VT0tBVdbBvPk9thi odIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708609442; x=1709214242; 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=jMPthOOurzrYeChkIv/lThMImRodm5q1y0tKDgpysO4=; b=WxRWLpC5CFQNiQogW62tbobq/sy3J/NjPw39H97ygejHLKrMkZhjvt5vlxSJxKPoSd 0Xyk+VuzzrIGPTeIZJ18i/LrqW4WkEIE2y1M9ou+YDroz4omp8110A9jrx4rA0PJNdX+ c72foui1Bqseg0tqsxZghg4QB9FdVoXo1A59RctH7YFp3UNNkqOVgkdeXsWgSCuTfeXc Ne3dJqHemCy0OA/3kiWzrLhLMIxYX2mYccwgqJu7PIhz2gdkFqvfeODtHzIEf44lZXhI K341fzKNEZv9ckz/WWZEB27AZTwVhngwi6UyWffQALCGw+fQZfyAfXN9nTAyDYwGzUoP CIEw== X-Gm-Message-State: AOJu0YwLm+FglX8OhXDPkAMbilUtIin1UPYKNupH9N0RNpE25MOGmyha zSUqjFPfJzMZ9DmawkorGhN/WGSQYDayAsKrFLIu4EYchhAvqZvK1PnLMNRTh7fh29ey0EEW1MX 8 X-Google-Smtp-Source: AGHT+IE5NMFF0Tu52dlkyPKiOo75py/opnnosZCFIKrq2R1pSTNZOY8iHYnM5C2KFEziUu5ivVrSwQ== X-Received: by 2002:adf:f647:0:b0:33d:7e42:e3de with SMTP id x7-20020adff647000000b0033d7e42e3demr3063321wrp.16.1708609442136; Thu, 22 Feb 2024 05:44:02 -0800 (PST) Received: from localhost ([193.47.165.251]) by smtp.gmail.com with ESMTPSA id t17-20020adfe111000000b0033d56d43fbesm12032849wrz.115.2024.02.22.05.44.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 05:44:00 -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 v3 2/3] tools: ynl: process all scalar types encoding in single elif statement Date: Thu, 22 Feb 2024 14:43:50 +0100 Message-ID: <20240222134351.224704-3-jiri@resnulli.us> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240222134351.224704-1-jiri@resnulli.us> References: <20240222134351.224704-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 Reviewed-by: Jacob Keller --- 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 1c5c7662dc9a..e459a130170b 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -474,14 +474,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 Thu Feb 22 13:43:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Pirko X-Patchwork-Id: 13567354 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 B89A513B296 for ; Thu, 22 Feb 2024 13:44:07 +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=1708609449; cv=none; b=qGmKoSJrFWf+9gDhCxosYPgEfSVxJwvAEDRoUvjSZBKeR5QNzSDguHpnd+ZSpRIDtvdFIAgkHRKUveQ3Sc6SINF4NZii8ndrY6YCI0IVVIAXvh1CTKwOaqxw0qO37C+rinQSVO4/vLnTbB2rqqTJlBBa2n+SkaLvxNOyUEgjzBY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708609449; c=relaxed/simple; bh=TEVRe+sUpzzN+HsUjVxeytZ+wm7epXxye4BPtOPzHLc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=udOMeQGJu6AWZr2wl1HM4WXzKnl52cZAPHABadO0fVLWsrpot44qsVlui1/gxeLxQB5WLS8sCVDO2+Qyc+XE6gyB09ITPxiyq2QRIFzOs2P+mWMnXc/DKQVcmg5r+e/6Mr+J7AJgqeDGht3kYYBTemIHlULBc4+MDh8eBnqoFtE= 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=AI1TcbPA; 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="AI1TcbPA" Received: by mail-lj1-f173.google.com with SMTP id 38308e7fff4ca-2d208be133bso87400261fa.2 for ; Thu, 22 Feb 2024 05:44:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=resnulli-us.20230601.gappssmtp.com; s=20230601; t=1708609446; x=1709214246; 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=iYXsYJGiQfqijxYyQOyyM1nVM3Rpld49x3qHPu0qp+M=; b=AI1TcbPAODH9Xv8KIKRg9aAEu//ctFneyK3QlZaIriJ7oNdF/kb/IV930BRDEHzWdh x45rMkghCdnGXPCvCfUytlqams/xaQoaxwheYHsSNwx2UWjzGEsEfs6zqoLkoBPJLgjx 6wQJvmx18YJFQmBxwyJdjvKIHiWnAiNstHDzVTzxU1f7D2FltXTLxUt++uqrA3CpmLCG OOA4GsAJAnyyq5aKvpMWYc4jAtjlgLQh2ymcRe1fsGLwEzOn34WitU78igoPzciS5C7a zilZelyDVwhP6QMflh0MaSwKzKF5Y6s+6o5KwOxudxJBBVTm51mowkvC29ugF8hVPORI fgaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708609446; x=1709214246; 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=iYXsYJGiQfqijxYyQOyyM1nVM3Rpld49x3qHPu0qp+M=; b=NROeE5xTCPPDPLFdzH1x+No8mMAOU4Qvxp5vG/xmGbHHVIkYJ+LXojGszv5s5ej3Eh bmJLW2AXOZAjVi5Ryvi4p1sdlcA0SVRfCw171CleEIdFpMC4vDw2fSZLPhgPKs1Dr4t+ iP6R/sGZWrby5ZuXqyBeFkLCa6osKMXWsBBO0/24S9QRIDehW/82L70c/1lgkGEmZ4tC uiPdTuIg0yXhliKGRhn17vFRa4RdcWVvcQVtLrlf2aeM4V8bmTS1wQBkpPTsLjzXvTWJ KR2pzWz1Sr42pQO9lyjOoqLIyLb+5mbtrUImgC+L6xMZEahQA4baA2NOBMm6wT0WBtBW HE0g== X-Gm-Message-State: AOJu0Yyuay6d9d/e0xfuEVllYMxfyII9ETJxx7bdVTbCNFiVu2P+m26P 2IxsWfiRPrc0lvsFPKxlQKVRBlV3oeFwKCSWtlh9Z731uVM3WDqF+MQ1dci+Am9zBkx6gLrsuPJ Z X-Google-Smtp-Source: AGHT+IEuFGxllMTJf+x3UmOmWeMA85z9Gsk7lEv5E6LHdi6Ca9JQ1kWolxDfkYIoN6v5wEggogsQnw== X-Received: by 2002:a2e:3807:0:b0:2d2:6568:eb6c with SMTP id f7-20020a2e3807000000b002d26568eb6cmr595632lja.30.1708609445731; Thu, 22 Feb 2024 05:44:05 -0800 (PST) Received: from localhost ([193.47.165.251]) by smtp.gmail.com with ESMTPSA id m16-20020a05600c3b1000b0041262ec5f0esm15648264wms.1.2024.02.22.05.44.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Feb 2024 05:44:05 -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 v3 3/3] tools: ynl: allow user to pass enum string instead of scalar value Date: Thu, 22 Feb 2024 14:43:51 +0100 Message-ID: <20240222134351.224704-4-jiri@resnulli.us> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240222134351.224704-1-jiri@resnulli.us> References: <20240222134351.224704-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 --- v2->v3: - s/_encode_enum/_get_scalar/ back - push out enum related code from _get_scalar() to new _encode_enum() function v1->v2: - s/_get_scalar/_encode_enum/ - accept flat name not in a list --- tools/net/ynl/lib/ynl.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py index e459a130170b..ac55aa5a3083 100644 --- a/tools/net/ynl/lib/ynl.py +++ b/tools/net/ynl/lib/ynl.py @@ -438,6 +438,26 @@ class YnlFamily(SpecFamily): self.sock.setsockopt(Netlink.SOL_NETLINK, Netlink.NETLINK_ADD_MEMBERSHIP, mcast_id) + def _encode_enum(self, attr_spec, value): + 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 _get_scalar(self, attr_spec, value): + try: + return int(value) + except (ValueError, TypeError) as e: + if 'enum' not in attr_spec: + raise e + return self._encode_enum(attr_spec, value); + def _add_attr(self, space, name, value, search_attrs): try: attr = self.attr_sets[space][name] @@ -475,7 +495,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._get_scalar(attr, value) if attr.is_auto_scalar: attr_type = attr["type"][0] + ('32' if scalar.bit_length() <= 32 else '64') else: @@ -483,7 +503,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._get_scalar(attr, value["value"]) + scalar_selector = self._get_scalar(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''