From patchwork Mon Jul 22 19:04:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739030 Received: from mail-oo1-f50.google.com (mail-oo1-f50.google.com [209.85.161.50]) (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 388FB18030 for ; Mon, 22 Jul 2024 19:04:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675088; cv=none; b=Q0E/28OeFYWm28igDO74fRfqTfd4wpzwAsPA+nXugxfH6OA4N8YpiooURafnCRJgY2Z6hIn6BoYrTIoxD69hFdFypY3VWRzZO4VE9faVrG2ze2SFGM/RKzcxTUo3ZnpZqK8Kgc7YcdSK+AyXhJIYC7XMM+mgEqoBMnyt5gylGzQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675088; c=relaxed/simple; bh=Z4jFBkz/sqQaJ0jaV769Swf3yzEUugsksMGiRoZ8hsM=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=IrOcBkLj2I3LTrc9I6MpIKO1Na2F6hIFIMHY+4pJnlq79+pv/Lp7eAFSnqFGA/duudEMWDSiG6JX12HBXuaUz9tmxm05ihNlutwMtzqwEEKQIStSZQNDTA1Cdj0/fDftLPj8mQX7+ZUUj3Xe4Pw5ijMdyfy/nTLZX9HuUN+vsno= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TwQ7Dqwc; arc=none smtp.client-ip=209.85.161.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TwQ7Dqwc" Received: by mail-oo1-f50.google.com with SMTP id 006d021491bc7-5d5846f797aso404525eaf.0 for ; Mon, 22 Jul 2024 12:04:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675086; x=1722279886; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=/3JMGqD0j4AtdgIadpyC8Tft9BzzxUUNEYDEsvlR9Zk=; b=TwQ7DqwcfFl9eSMOGfg3dXDto/FOywYzvP7w1kciNyuHgA4eWEfHJpQQCB6luGj2yk MrVUHpRl4y2xiaxeFMY8q/dp+2wjz5jnzRXHiC28whGOeUdV5RVihFNYmqJuMwmHsCyo LZ6WOSK6e6EYY0AUJhcYOw1B1HfFBs3w1y1+hcNFXM/T1EpwYFrVvRAXA5g5YkEEsHzG QOiCzgfVnxAL0VWqYqlldQcSF93+3htC2Om9K9mReFEkt+RKJVrbVnd1rkJVhAjYHKdj lm8NrzV3L76S7hHeIG2LKwalMWYSOEytBtbbnFGqwC/p46gZdVhZ360LgrMe5EdEC0NY ffGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675086; x=1722279886; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/3JMGqD0j4AtdgIadpyC8Tft9BzzxUUNEYDEsvlR9Zk=; b=MZDY9Wiw/8oTn3hMrBljHscwUlWzJSGPdJs9VzfRuxXGDlgn9QjGiIL4lzMn8POsvf 1sq8mlVvwpI45QWoXOshGk8LhKjW7zfZ2aL6CJtjDyS780Da2gGrsaojkBAr9LvoPsZY yB4XgIjCM0wOO264pePX6Kr3X9aKdHJhQsIC2WV6Dd3fz7vkiLvkmxWrzlLw6hqaa/Ha TWiabpWcg6NvNHLuEaLXC7xedL8/LQhSXzZlklFnVJbYrEqdW92m2iAEUTlnbwhK0sT8 NvVPfa8iPkmNxTk/q8hT0ZuTZXOaKfMoY+AL8P9ea+20Zdxsy9zbIU3WDiqyIEQ9yXtG CjYQ== X-Gm-Message-State: AOJu0YxN02fk9L2arOR64CAJgNspEhBKTkb+dWR1ErhrTUwAqjHapK3i AgDjgtOX1CGMyVlMss4/1ElaZQWnVSqcKmbYD+QzmIeODNajilxjgu66Rw== X-Google-Smtp-Source: AGHT+IGLzyflDBK6IPdKxvnFh75CSvRbUIPT5XHXdfGOxCfvWTMDpqE8PqlMXyCF4Ait6jtxBw0SbA== X-Received: by 2002:a05:6820:4c04:b0:5cc:a794:9ea7 with SMTP id 006d021491bc7-5d58b807b66mr1175897eaf.7.1721675086064; Mon, 22 Jul 2024 12:04:46 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:45 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 01/12] useful: Add utility to find the next power of two Date: Mon, 22 Jul 2024 14:04:18 -0500 Message-ID: <20240722190443.43196-1-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 --- ell/useful.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ell/useful.h b/ell/useful.h index 7bb924bcab8c..504ea089cb21 100644 --- a/ell/useful.h +++ b/ell/useful.h @@ -52,6 +52,12 @@ static inline unsigned char bit_field(const unsigned char oct, return (oct >> start) & mask; } +/* Must be called with n >= 2 and n <= ULONG_MAX / 2 + 1 */ +static inline unsigned long roundup_pow_of_two(unsigned long n) +{ + return 1UL << (sizeof(unsigned long) * 8 - __builtin_clzl(n - 1)); +} + #define DIV_ROUND_CLOSEST(x, divisor) \ ({ \ typeof(divisor) _d = (divisor); \ From patchwork Mon Jul 22 19:04:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739031 Received: from mail-oo1-f47.google.com (mail-oo1-f47.google.com [209.85.161.47]) (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 DA3331805E for ; Mon, 22 Jul 2024 19:04:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675089; cv=none; b=sBAiEOcd6EQK1ALQR/oQ82hKrR4kvi05fKWNdER0RzRthF+cMW5z3YIDy7xQpJsI1GiLuH4a4IJO8pV4Za/kz2MmHiDjQ2xJ8jjIWmwoqV6oBUDkmF/sZDpuRFTIq0oZnXXHkaYG5d2JgR4PugisokJ9IQvUTVJzu3SyvG5J16o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675089; c=relaxed/simple; bh=w4qbSYHp8xXVUkTEEC7x7Il2I0oHSu9WJc0SR+O1zcA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AnMRfArJBLA6hc5XBEDxNRQddwh20BdQgHjNYz5kTdivMCrgH6eDGEu7/lqw3YIup9N5CGbTNG1Df298R+7xufjVaGb3gC8TjFlu0pqxgtQYL3lqVQTw+b0ibaIxxo3bUSFrrkuedjoDfKo9U4UixGa5FGekmDf9p8OIYIRxbW4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=h+hT2X++; arc=none smtp.client-ip=209.85.161.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="h+hT2X++" Received: by mail-oo1-f47.google.com with SMTP id 006d021491bc7-5ce739c2650so2490365eaf.1 for ; Mon, 22 Jul 2024 12:04:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675087; x=1722279887; darn=lists.linux.dev; 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=nnVqIdf7tJti7Wkfi1ZE3c57AQfTI8UkuY4bieQ5yQU=; b=h+hT2X++7+os0Gx/G6BanvZeggqh4HKeYfTKp6O//RcjFjCsOleE33btCYHI6+wP1g s/CdwOFsh1cAr2Zpr3H4v37v4mF9e/Jo2CAV1lSR12UZJnsXqTQL2hbF4/11hAlAOYaS IRvfNgpPqsDfyhGW074Ar3pKQnY3LWTI/X9/VRO+joGP7c/Yr6nqZWkqsFQVu1+QUHOG NOTKwp47jTVQexZtQhtQOLFQNeIkIJ44h0aD/Pc0SDiAQgMtvhe4cwBwBCmDRT7YP+l9 cBnQCFI0TPN4lLT5X8XGDZ83h6BfOyKatZpn2cUQUNNGPUfnZueQB49cbltRk1gA8faV LRNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675087; x=1722279887; 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=nnVqIdf7tJti7Wkfi1ZE3c57AQfTI8UkuY4bieQ5yQU=; b=CTzsZI395sJpECmFEBkr/F3zSAF67E8IaBAvHVRW26rKSlD288f+Q1Qfb/O0TZNVml pQgkWhEzsU3kxsZbBxE2H+fwJTqUh4DK5OGMAJeJ3AEZF0Hfp8i88yholuN85EYgRKdt 31aO6PpDGONG/r/tmgd+Y6SAkpYAoFGDVD2iuKz4NpP+xBpM9dSlWI9joIcjoUWeh02u ssG36vkr1E6myxUbgdGzrpR1PVDPwKbm+v2c9B2hxfaQnhdZzYaUvP6KzXwxeBH+Rvn1 MsMK6octhKJ8fTN43CfugrGHv6noZOHR4m9I1UNHeYRsy9kMBrh7y5FRwBHZDO4LEA9j NJIg== X-Gm-Message-State: AOJu0YwY4tkoxJiMrD+XZlduHIl/8PtOQThx3C/lFK/u7ecH+eDlaZEM KGjblHYrtlM4GObe8h1/v9+wMjHik7QcOwRkG93isYjj8+x85hvHdTmUBA== X-Google-Smtp-Source: AGHT+IH62IRr/s4gWFiYoGXquf2mtMAezq954rINimCIzXwigph51thkCNQcG3WqvVtGywaDpbtuOw== X-Received: by 2002:a05:6820:2002:b0:5ce:e851:7356 with SMTP id 006d021491bc7-5d564e1f3a7mr10208961eaf.1.1721675086882; Mon, 22 Jul 2024 12:04:46 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:46 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 02/12] unit: Add unit test for roundup_pow_of_two Date: Mon, 22 Jul 2024 14:04:19 -0500 Message-ID: <20240722190443.43196-2-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 --- unit/test-util.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/unit/test-util.c b/unit/test-util.c index b178b86cf489..341c790c3731 100644 --- a/unit/test-util.c +++ b/unit/test-util.c @@ -15,6 +15,7 @@ #include #include +#include static void test_hexstring(const void *test_data) { @@ -270,6 +271,23 @@ static void test_clear_bits(const void *test_data) assert(l_memeqzero(bitmap, sizeof(bitmap))); } +static void test_roundup_pow_of_two(const void *test_data) +{ + assert(roundup_pow_of_two(2UL) == 2UL); + assert(roundup_pow_of_two(3UL) == 4UL); + assert(roundup_pow_of_two(5UL) == 8UL); + assert(roundup_pow_of_two(15UL) == 16UL); + assert(roundup_pow_of_two(25UL) == 32UL); + assert(roundup_pow_of_two(1025) == 2048UL); + + if (sizeof(unsigned long) > 4) { + unsigned long l = UINT_MAX; + + l += 1; + assert(roundup_pow_of_two(UINT_MAX) == l); + } +} + int main(int argc, char *argv[]) { l_test_init(&argc, &argv); @@ -293,5 +311,7 @@ int main(int argc, char *argv[]) l_test_add("L_BITS_SET", test_set_bits, NULL); l_test_add("L_BITS_CLEAR", test_clear_bits, NULL); + l_test_add("roundup_pow_of_two", test_roundup_pow_of_two, NULL); + return l_test_run(); } From patchwork Mon Jul 22 19:04:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739032 Received: from mail-oo1-f48.google.com (mail-oo1-f48.google.com [209.85.161.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 AD07B1863F for ; Mon, 22 Jul 2024 19:04:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675090; cv=none; b=euiZ/PGLQmlRgaefnhHIYtFpT/M7zqGcvgrmORNLVkX8euIwcpGSw/MYmYILSTMBovWg/aBDFoBVN4WekvWEfQv2aSnb6TSvW05BpfAPrViNWTQXBU8YKkyH8qOAuQPvm4p5ZejkinfMd1AkOxqF3uAL2hi+Xm79F4pijRAPyyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675090; c=relaxed/simple; bh=Hy0R84CVcG3Sw49FW9f8blctks4owv6XZ3EAohmXsNw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WJZQW6GrVivhJISOKyADSdED6ekSNQXHvsbR+4ZS35l91+M+XeI3DuY4uDSBFMk62d8oq/oyCvtx1HwPpnEKBrwv+eRGwr2GuvxCo6jEn2N5Dc/XtOZb7iUhzDevjLKESz4r1zJQEzZmkfCJI5FFH7NCy8r44gVFRhIpaRo4az0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=j8Qt8MSS; arc=none smtp.client-ip=209.85.161.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="j8Qt8MSS" Received: by mail-oo1-f48.google.com with SMTP id 006d021491bc7-5c669a0b5d1so2770234eaf.3 for ; Mon, 22 Jul 2024 12:04:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675087; x=1722279887; darn=lists.linux.dev; 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=Mo0eLWlEhhHrwQmkhuENMv7vw2sYPCeYuxDPg6p8iNQ=; b=j8Qt8MSSm7JA03bl5rT1gQPx0hSdBDUwuRRRp8EPJ6xqZGgjVt0Mr1M6UjlM6+AHIk pbkQHJFrd+p6J/fF7LYwrRPBJKiG91kpVFBysc2iWTIGV1QoPGRpV3iqvAgO580DMXh4 B69ltLokGZVWDz/I/Z3lcdvCBCXIr450J2S7oClqzTKQusf6nyqTVB9OIRW9sCIekJnp SikvCCjvTllT+rxLCPa+6BxWa+ktup6TObEm5thTopChEV1xN/g5Rlyu748uIyonn5/H cAzDRNsfH5rIHE7d1HkIpBT4DZ6yM5DPOEsjwjjMxMDitqhUR3LcJY3B+B1Qc4Lxs+Oe ZUXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675087; x=1722279887; 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=Mo0eLWlEhhHrwQmkhuENMv7vw2sYPCeYuxDPg6p8iNQ=; b=Zk4lB6ttUtCq6XP4O3i6bxE8MPck+QNBpwdgnXrezskTQX4Qus85BX6F8yjkuuULO3 Tr2alrFeTyps4rbz/fHHQB8Y9pMQ9rrRtpD2x03lo+ZIxV2MIXUlN9NS//EnwvlszJcJ IqgozK0aA7OvWVacojsAf+nmDr/IXu8Qf4urXQQZQoiXkqKb+OmGESWBvdQ8bgKgsG22 2adiVbofvNGGHl2w6+i6h+fmJhB1MPwcRLGdHY+ExQLIK31c5T2Dkvea6Fb+uOEoXSmf KoBSOJkBIBZFKweeijWvgQ6/91puSgjDHbAmv65gKihB+jccMDR+nJi6wIlZdchttwRL FvzA== X-Gm-Message-State: AOJu0YwKkzXBCXMJoh2lCAHQMxmuWesdDe5u/h0vbaWAcuzfZ1VhEBTC Ca/3ub+N7QVa2PwNwKRqf4bj74hK9NMKbx6aFRMjBlW7/gwCPyjFiuJXLg== X-Google-Smtp-Source: AGHT+IGv5TTZEaryYuR89kWjuEEK9VO+owhUDSVwW249REtOat4j652lZW0WAxvjPrW/N+U632jSUQ== X-Received: by 2002:a4a:e84b:0:b0:5c6:9293:ed8a with SMTP id 006d021491bc7-5d564fd61abmr10363671eaf.6.1721675087665; Mon, 22 Jul 2024 12:04:47 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:47 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 03/12] util: Introduce l_util_pagesize Date: Mon, 22 Jul 2024 14:04:20 -0500 Message-ID: <20240722190443.43196-3-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 --- ell/ell.sym | 1 + ell/util.c | 12 ++++++++++++ ell/util.h | 2 ++ 3 files changed, 15 insertions(+) diff --git a/ell/ell.sym b/ell/ell.sym index 9fcc3aad9a2f..bc0c6e10ebca 100644 --- a/ell/ell.sym +++ b/ell/ell.sym @@ -23,6 +23,7 @@ global: l_util_hexdumpv; l_util_debug; l_util_get_debugfs_path; + l_util_pagesize; l_memeq; l_secure_memeq; l_safe_atox8; diff --git a/ell/util.c b/ell/util.c index 549e253eefc0..45ec6c8110bc 100644 --- a/ell/util.c +++ b/ell/util.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "utf8.h" #include "util.h" @@ -812,3 +813,14 @@ LIB_EXPORT int l_safe_atox32(const char *s, uint32_t *out_x) return safe_atou(s, 16, out_x); } + +LIB_EXPORT size_t l_util_pagesize(void) +{ + static size_t page_size = 0; + + if (likely(page_size > 0)) + return page_size; + + page_size = sysconf(_SC_PAGESIZE); + return page_size; +} diff --git a/ell/util.h b/ell/util.h index f4fc4d1143e3..c56f182292fa 100644 --- a/ell/util.h +++ b/ell/util.h @@ -515,6 +515,8 @@ int l_safe_atox32(const char *s, uint32_t *out_u); int l_safe_atox16(const char *s, uint16_t *out_u); int l_safe_atox8(const char *s, uint8_t *out_u); +size_t l_util_pagesize(void); + #ifdef __cplusplus } #endif From patchwork Mon Jul 22 19:04:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739033 Received: from mail-oo1-f46.google.com (mail-oo1-f46.google.com [209.85.161.46]) (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 5851C18030 for ; Mon, 22 Jul 2024 19:04:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675090; cv=none; b=I+m3uNEC13C1NEKP1wLXoT8ullD6n2nVwOGvsFqmKbQ84q4KFehJGwq1O6C8O/P7FlkZ621Ml93kuD5uwQuUTodxPnCD5m6aD2JXn5l5KIqJ5bAfc3VaKoKFxThPPrDZK1lZqI8MxuemCjtNeHR7dYbEQOsqPSUUj3maJ9OI0lo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675090; c=relaxed/simple; bh=fhUP7ABoW8ejvLCDkAdomuT+lyblZbyUI42C+ewG4Dc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C4Ya8jDRd/p7GIe1FDnwEtE9aYG24nIpSYTWtHyojRI4Th8L7X9wingqJTKELfP35cszx56JfeVJjDOdeDLbHJf+hBIBCbKreXzwRD01LhnbknmZYRlC45urAAa2tQxc6qFDO5yQOonaMEFuWw1mokMh1qIvL46ceavnfQxa4gg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=a27zGchY; arc=none smtp.client-ip=209.85.161.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="a27zGchY" Received: by mail-oo1-f46.google.com with SMTP id 006d021491bc7-5c669a0b5d1so2770251eaf.3 for ; Mon, 22 Jul 2024 12:04:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675088; x=1722279888; darn=lists.linux.dev; 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=HjzJFItQFqoBMF56u5bFyJ7TaXwSWytDi/66uEjOp6w=; b=a27zGchYkVgG/niqaNaYWEn18EYYwDtZu8YXSHVu1W7GOlKgJn2N02jj6+WWgvu/gk MsMzFDa2lmiX+xcLPCU0160W97RJF0DabHMzOgyBrbTnWW0zKNxn/GrLVvqYX5qyAeNO /Bjv+rkXa6SI8OZd/6B3+Azt+JPWKetp/iG5c5UJKiWXo+CXUE6v45GjqaUXJTV11IpO HqWx/En9Rnilds6mcfH1g92sHlkIDlZR11e10B6fhxfepWzg0ljatJ8Q97U0drhWsp9R RtocsQunX3JWAhX/86Z+DTTgCAoNYxXD2lX46fIWKfbyOWuIpmeHUXlG3IyN/Q0ojeIp wzRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675088; x=1722279888; 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=HjzJFItQFqoBMF56u5bFyJ7TaXwSWytDi/66uEjOp6w=; b=jcvKmAba6thXN9zF7Hrxwk4VEObHywTkl96lHajdsCszlsBlDLWsEhqh+UJvcLCQHR uIKJ+P/xjsV10Y5OfVDkc1ZB627qJuWIGVXZrC/N0s+OWDOEXosBSR7S1YVX2sE6aLyN N9GVOZWs6kWuZKSuY9hrLi0MGZT0gAl7WF3GtZSanZl/0IR57kRdkDV2eoxJfZeeikor 5Tdy6WcPvnU5iIL93CGzGvJYoahG7dOJsihPYa5z130irNmu9De4yXnmOeyIn07IIu/O dv/Nk99uJ8A3i7v/Okb2V314KpSk74OykWD/HcMplJcKlaWptmweKh2GWbH7r5C8g8l0 cORw== X-Gm-Message-State: AOJu0YzrI/lCd8mpVxkyP3KGGn7itfg+KEMcq94jIk7xIVoQvGyOBNk/ YUs/i8l6egSseMYFSBasOpENJpj2E7DwNxmSHRMDk24dYuMYyLQXbrfy1g== X-Google-Smtp-Source: AGHT+IHmYl29pZHHAIUkAGGh0txu0aryDZel2K2jRPxR/dM6gBxD4gW98dleZXhQ5WMyw/BC+6Bg4w== X-Received: by 2002:a05:6820:2014:b0:5b9:e7db:1cf8 with SMTP id 006d021491bc7-5d564f82ed7mr11736438eaf.4.1721675088382; Mon, 22 Jul 2024 12:04:48 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:48 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 04/12] string: Use conservative allocation growth strategy Date: Mon, 22 Jul 2024 14:04:21 -0500 Message-ID: <20240722190443.43196-4-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Instead of always growing exponentially, only request enough pages to hold the new string. It is unlikely that ell based applications will be processing large strings, so be more conservative with memory use. --- ell/string.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/ell/string.c b/ell/string.c index a6d359d20cc9..e8e45beacb92 100644 --- a/ell/string.c +++ b/ell/string.c @@ -34,25 +34,18 @@ struct l_string { char *str; }; -static inline size_t next_power(size_t len) -{ - size_t n = 1; - - if (len > SIZE_MAX / 2) - return SIZE_MAX; - - while (n < len) - n = n << 1; - - return n; -} - static void grow_string(struct l_string *str, size_t extra) { if (str->len + extra < str->max) return; - str->max = next_power(str->len + extra + 1); + str->max = str->len + extra + 1; + + if (str->max < l_util_pagesize()) + str->max = roundup_pow_of_two(str->max); + else + str->max = align_len(str->max, l_util_pagesize()); + str->str = l_realloc(str->str, str->max); } From patchwork Mon Jul 22 19:04:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739034 Received: from mail-oo1-f53.google.com (mail-oo1-f53.google.com [209.85.161.53]) (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 3F98A16EB6E for ; Mon, 22 Jul 2024 19:04:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675091; cv=none; b=semeozHRMbkp1LUGu2c147COdulZNdRRUy6Wd/0NcUZklFTRmAvSXbNPV8H2SYwIZSSa/g30qC33SRH/VeGneNEI5evL2MEIUgWTS2OmSH4gITVxOMQ/DkKEM4yYdDUiKSu0fkAEokr4k0ZYoEyzTmY008uXz0UxyN++f3f/l30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675091; c=relaxed/simple; bh=zoACB15UJymFC/3yTgf9oeIzU3HaO3afBeov9YD7y3w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=owG6LqkuyWTgkT+zbL0HmmlwnYyWL1QO3+5GevAcPszGN4HagFleCKAOUJAbbDK/qjXXv9zwTrE0F1eD9eji1NdjquG3i2VBalrQ08ag7I5LziLn+ZD3ymAH1EDvl5xBJBgqm+N4LnlHZMmNGBxLos3l/CCgQWKkIWfV+KhQIDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=AH7EAN96; arc=none smtp.client-ip=209.85.161.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="AH7EAN96" Received: by mail-oo1-f53.google.com with SMTP id 006d021491bc7-5d4fb707895so2549339eaf.0 for ; Mon, 22 Jul 2024 12:04:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675089; x=1722279889; darn=lists.linux.dev; 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=jiRv008JRANyR7s7yqGbHvOaQRAx716GauA0f+wPekI=; b=AH7EAN96J0JtQozEa9KWDd6l1d08on/PnEGoX97BWRcoFWJHfTtImLqTGtBLfpIZch dAQX6Lbtk6O1cBNQvUF6gDOH56sS+fgXd07AjcBJDMYv6YoYIMxzzzSCUCva7CVDw6OV TbLVIwKG535BWn+1VXLIPDq7XOyGDTmmuugXNj/zqCOSP8QMXi9y0AjTzuk0mmgt8+UO SHkFMnE2JRts5xY0K+tyo9As+DtK4NXuuy5dDcHLsRw3U0RiUBxuieF5j5dqHEdBCjsa TLfIvpU4m1xag/dARER+csVL2MI6jyiFzlDAn6DgL7CYHOvL/Wxc5vr+CD1CCPJ23eHZ YXWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675089; x=1722279889; 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=jiRv008JRANyR7s7yqGbHvOaQRAx716GauA0f+wPekI=; b=XhxNqPa+6/BLQrr8gpNhwF5fMHjkPRFthVzgP0f371xeYRvARqDQXzNlj/owGTLRJm gMoTcb/hwy3pyO98J837GIdY9IMmXKxbaUFJ/qWiZkGBWKmCxjRlY+hc+UNzSZeI5em/ aHcS60AB5DgvnIovJT2agXi+V/NVZA61k3tpFeGUdG5uAlTguBeKPN2PkwKpyEoT3Lyi wS01YMpH1aqBQv/vKrn09it9TANVmaiNN3ZwmohilGQePyucsMO5LjnvVThsieQTSBgH ID4AXBG2MAPJbXcstApSw9Egj/KbjyK5XCHwIKcq18598CxPQcYon67pUpBspMJ6DZyp PkNQ== X-Gm-Message-State: AOJu0YxAN/HZqRqjOQyggS1UvR1VqCdBH7mlLksUPAQOUiRSE2/hil8L ANr+d5ocK2F52CzU3djZLVg02q8MmaYYYOer7+UGrgiYzUY6DM6tedi3Rg== X-Google-Smtp-Source: AGHT+IHVdBccHKfqB4JZfvOd1UeCVORIJhILU6ARhbO5ZQ+7ImTm22gj25Bd1BMtevvUJ11DuNkwSg== X-Received: by 2002:a05:6820:c8b:b0:5c4:144b:1ff9 with SMTP id 006d021491bc7-5d564faa2ddmr10906446eaf.5.1721675089134; Mon, 22 Jul 2024 12:04:49 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:48 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 05/12] unit: Refactor genl-msg tests Date: Mon, 22 Jul 2024 14:04:22 -0500 Message-ID: <20240722190443.43196-5-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Both parse|build_set_station and parse|build_set_rekey_offload tests declare precanned information as static const variables in the individual test functions. Declare this precanned information inside a test structure for easier maintenance and future reusability. --- unit/test-genl-msg.c | 124 ++++++++++++++++++++++++++----------------- 1 file changed, 74 insertions(+), 50 deletions(-) diff --git a/unit/test-genl-msg.c b/unit/test-genl-msg.c index 0ae92aefebc9..e6432f4362ef 100644 --- a/unit/test-genl-msg.c +++ b/unit/test-genl-msg.c @@ -31,10 +31,25 @@ static unsigned char set_station_request[] = { 0x02, 0x00, 0x00, 0x00, }; +struct set_station_test { + uint8_t mac[6]; + uint32_t ifindex; + uint32_t flags[2]; + uint32_t seq; + uint32_t pid; +}; + +static const struct set_station_test set_station = { + .mac = { 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 }, + .ifindex = 3, + .flags = { 2, 2 }, + .seq = 0x550d538b, + .pid = 3604, +}; + static void parse_set_station(const void *data) { - static const unsigned char mac[6] = - { 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 }; + const struct set_station_test *test = data; struct nlmsghdr *nlmsg; struct l_genl_msg *msg; struct l_genl_attr attr; @@ -56,13 +71,13 @@ static void parse_set_station(const void *data) assert(l_genl_attr_next(&attr, &type, &len, &payload)); assert(type == 3); assert(len == 4); - assert(*((unsigned int *) payload) == 3); + assert(*((unsigned int *) payload) == test->ifindex); /* MAC Address 24:A2:E1:EC:17:04 */ assert(l_genl_attr_next(&attr, &type, &len, &payload)); assert(type == 6); - assert(len == 6); - assert(!memcmp(payload, mac, 6)); + assert(len == sizeof(test->mac)); + assert(!memcmp(payload, test->mac, sizeof(test->mac))); /* Station Flags 2: len 8 * Mask: 0x00000002 @@ -72,19 +87,16 @@ static void parse_set_station(const void *data) */ assert(l_genl_attr_next(&attr, &type, &len, &payload)); assert(type == 67); - assert(len == 8); - assert(((unsigned int *) payload)[0] == 2); - assert(((unsigned int *) payload)[1] == 2); + assert(len == sizeof(test->flags)); + assert(((unsigned int *) payload)[0] == test->flags[0]); + assert(((unsigned int *) payload)[1] == test->flags[1]); l_genl_msg_unref(msg); } static void build_set_station(const void *data) { - static uint32_t index = 3; - static const unsigned char mac[6] = - { 0x24, 0xa2, 0xe1, 0xec, 0x17, 0x04 }; - static uint32_t flags[] = { 2, 2 }; + const struct set_station_test *test = data; struct l_genl_msg *msg; const void *raw; size_t size; @@ -92,15 +104,19 @@ static void build_set_station(const void *data) msg = l_genl_msg_new_sized(18, 512); assert(msg); - assert(l_genl_msg_append_attr(msg, 3, 4, &index)); - assert(l_genl_msg_append_attr(msg, 6, 6, mac)); - assert(l_genl_msg_append_attr(msg, 67, 8, flags)); + assert(l_genl_msg_append_attr(msg, 3, + sizeof(test->ifindex), &test->ifindex)); + assert(l_genl_msg_append_attr(msg, 6, sizeof(test->mac), test->mac)); + assert(l_genl_msg_append_attr(msg, 67, + sizeof(test->flags), test->flags)); - raw = l_genl_msg_to_data(msg, 0x17, 0x05, 0x550d538b, 3604, &size); + raw = l_genl_msg_to_data(msg, 0x17, NLM_F_REQUEST | NLM_F_ACK, + test->seq, test->pid, &size); if (do_print) { l_util_hexdump(false, raw, size, do_debug, "[MSG] "); - l_util_hexdump(true, set_station_request, size, + l_util_hexdump(true, set_station_request, + sizeof(set_station_request), do_debug, "[MSG] "); } @@ -120,16 +136,29 @@ static const unsigned char set_rekey_offload_request[] = { 0x0c, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, }; +struct set_rekey_offload_test { + uint32_t seq; + uint32_t pid; + uint32_t ifindex; + uint8_t kek[16]; + uint8_t kck[16]; + uint8_t replay_counter[8]; +}; + +static const struct set_rekey_offload_test rekey_offload = { + .seq = 0x53e1a359, + .pid = 0xe74002ba, + .kek = { 0x2f, 0x82, 0xbb, 0x0d, 0x93, 0x56, 0x60, 0x4b, + 0xb1, 0x55, 0x1c, 0x85, 0xc0, 0xeb, 0x32, 0x8b }, + .kck = { 0x43, 0x25, 0xcf, 0x08, 0x0b, 0x92, 0xa7, 0x2d, + 0x86, 0xdc, 0x43, 0x21, 0xd6, 0x0c, 0x12, 0x03 }, + .replay_counter = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }, + .ifindex = 3, +}; + static void parse_set_rekey_offload(const void *data) { - static const unsigned char kek[] = { - 0x2f, 0x82, 0xbb, 0x0d, 0x93, 0x56, 0x60, 0x4b, - 0xb1, 0x55, 0x1c, 0x85, 0xc0, 0xeb, 0x32, 0x8b }; - static const unsigned char kck[] = { - 0x43, 0x25, 0xcf, 0x08, 0x0b, 0x92, 0xa7, 0x2d, - 0x86, 0xdc, 0x43, 0x21, 0xd6, 0x0c, 0x12, 0x03 }; - static const unsigned char replay_counter[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }; + const struct set_rekey_offload_test *test = data; struct nlmsghdr *nlmsg; struct l_genl_msg *msg; struct l_genl_attr attr; @@ -172,33 +201,25 @@ static void parse_set_rekey_offload(const void *data) assert(l_genl_attr_next(&nested, &type, &len, &payload)); assert(type == 1); - assert(len == 16); - assert(!memcmp(payload, kek, 16)); + assert(len == sizeof(test->kek)); + assert(!memcmp(payload, test->kek, sizeof(test->kek))); assert(l_genl_attr_next(&nested, &type, &len, &payload)); assert(type == 2); - assert(len == 16); - assert(!memcmp(payload, kck, 16)); + assert(len == sizeof(test->kck)); + assert(!memcmp(payload, test->kck, sizeof(test->kck))); assert(l_genl_attr_next(&nested, &type, &len, &payload)); assert(type == 3); - assert(len == 8); - assert(!memcmp(payload, replay_counter, 8)); + assert(len == sizeof(test->replay_counter)); + assert(!memcmp(payload, test->replay_counter, len)); l_genl_msg_unref(msg); } static void build_set_rekey_offload(const void *data) { - static uint32_t index = 3; - static const unsigned char kek[] = { - 0x2f, 0x82, 0xbb, 0x0d, 0x93, 0x56, 0x60, 0x4b, - 0xb1, 0x55, 0x1c, 0x85, 0xc0, 0xeb, 0x32, 0x8b }; - static const unsigned char kck[] = { - 0x43, 0x25, 0xcf, 0x08, 0x0b, 0x92, 0xa7, 0x2d, - 0x86, 0xdc, 0x43, 0x21, 0xd6, 0x0c, 0x12, 0x03 }; - static const unsigned char replay_counter[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }; + const struct set_rekey_offload_test *test = data; struct l_genl_msg *msg; const void *raw; size_t size; @@ -206,16 +227,19 @@ static void build_set_rekey_offload(const void *data) msg = l_genl_msg_new_sized(79, 0); assert(msg); - assert(l_genl_msg_append_attr(msg, 3, 4, &index)); + assert(l_genl_msg_append_attr(msg, 3, + sizeof(test->ifindex), &test->ifindex)); assert(l_genl_msg_enter_nested(msg, 122)); - assert(l_genl_msg_append_attr(msg, 1, 16, kek)); - assert(l_genl_msg_append_attr(msg, 2, 16, kck)); - assert(l_genl_msg_append_attr(msg, 3, 8, replay_counter)); + assert(l_genl_msg_append_attr(msg, 1, sizeof(test->kek), test->kek)); + assert(l_genl_msg_append_attr(msg, 2, sizeof(test->kck), test->kck)); + assert(l_genl_msg_append_attr(msg, 3, sizeof(test->replay_counter), + test->replay_counter)); assert(l_genl_msg_leave_nested(msg)); - raw = l_genl_msg_to_data(msg, 0x1b, 0x05, 0x53e1a359, 0xe74002ba, - &size); + raw = l_genl_msg_to_data(msg, 0x1b, NLM_F_REQUEST | NLM_F_ACK, + test->seq, test->pid, &size); + if (do_print) { l_util_hexdump(false, raw, size, do_debug, "[MSG] "); l_util_hexdump(true, set_rekey_offload_request, size, @@ -412,13 +436,13 @@ int main(int argc, char *argv[]) if (!little_endian) goto done; - l_test_add("Parse Set Station Request", parse_set_station, NULL); + l_test_add("Parse Set Station Request", parse_set_station, &set_station); l_test_add("Parse Set Rekey Offload Request", - parse_set_rekey_offload, NULL); + parse_set_rekey_offload, &rekey_offload); - l_test_add("Build Set Station Request", build_set_station, NULL); + l_test_add("Build Set Station Request", build_set_station, &set_station); l_test_add("Build Set Rekey Offload Request", - build_set_rekey_offload, NULL); + build_set_rekey_offload, &rekey_offload); l_test_add("libnl-generated Example with Nesting", parse_libnl_nested, NULL); From patchwork Mon Jul 22 19:04:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739035 Received: from mail-oo1-f41.google.com (mail-oo1-f41.google.com [209.85.161.41]) (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 E290518030 for ; Mon, 22 Jul 2024 19:04:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675092; cv=none; b=UGHKN9+OKxGiUeoh3EV6Ixo5drlnS1MqKSCiAkGQjp3brhaJ7A+BylXyRYadlO+TueTPSz3Rmi1OVMhpwODV0h88g4JRbzeF4MbNzLEdkYyArDQJm1uCAckPfYiWpDVBiCrKEg5hFC7D1ivB9Ex+tJwn27EDy6I4wouduzKTETU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675092; c=relaxed/simple; bh=g6mxt8O/vg7Spt4vp6gcKEqncw4JqwAGdTuRecP6aLw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S8UfLHFCApD2/ArV40OjjcfSCk90cEY4j3aAjW3BANfBBNmV44Y6K0VUxRYHeX5qjRwWwgvY0vy9mz2vznNKkH3KuKRlAK/KOy1/LV4cA3asEon6daaTIJPVx6ULTmMdRf92vwgFp7SCFHvyWs7pFEdYFj3q/LO5rdcP+eik6tg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=K8HFA66j; arc=none smtp.client-ip=209.85.161.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="K8HFA66j" Received: by mail-oo1-f41.google.com with SMTP id 006d021491bc7-5c667b28c82so2153908eaf.1 for ; Mon, 22 Jul 2024 12:04:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675090; x=1722279890; darn=lists.linux.dev; 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=uegv3AweMDG0BkxOh+/tA+bEATdYXMdlok3Fo1thj0A=; b=K8HFA66j8pZHfaI+CMPIb2B8/JG0a0s1xaOS8Z8xL6IQtvAamtDDEDHwOYHEDVQ7nr 0FvY1c3e2F+UUV0oVQVPgJZnoZaaB/uxP9/L1uZB67KTILd9BQoV2ykeCipdIrrp9DJ0 rJv+JFLmxfKb9Qg99NGy7iLjObygOOmrHFq34F52iYlTIbjjcpf95tf9HCT44Sdrz2mN xj/Y3dZ4oVjmB40MY7HKie7Fzh61DBsZXVCwp8nY4qFnGddr/nDYB6eEoAVjS0h+zRgS 3fD3XxRJE/z3fsXbp4eSVrgMVm+KswdliFCJ+3nRkHUTZJg3PkzcxOb7bH0fXKgyxpIy L5Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675090; x=1722279890; 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=uegv3AweMDG0BkxOh+/tA+bEATdYXMdlok3Fo1thj0A=; b=nmf1aZR+bhe92yNmwMxSw4aqSurN00LDP7+nbJTa9/hJi0SMeBk64j6P+ALtVF67DQ U4BzYAFb7OEJVPxblSqFwEkWTD6VKdYnwmyz154eSanedLH0mkSMkgLm+iXtDsZ7QGJc elf9S412161IkvFJOvRd/pftQjgChaHE5Vq22KAKx3VG7yw71Zw9OiW3zvQHdC1Q0GMi 2FOFsxDmjySQxcAnIopnEeGU2lnb8qh3PoOYHOwvw/S8R8F9T4LxIn1SaA7jzVv3g1RR Ky2oNzx7RHr0sxn7yEsf8DPwVqv88fDkPFYe2tpnmhJByKpljZl4pxKKFHchlHJjqpxa OwQg== X-Gm-Message-State: AOJu0YzMQdqBBsjIccl2QqMMwuWgsjsXU3hZVDYWHvHA5wKTwZEA9ql3 wa7oxneAq6FAl2M8mTDXtRd4zwnWZlLWnflZYeT7C7PTkpgpygnuxpx+tg== X-Google-Smtp-Source: AGHT+IF6faZYeYXv5TZpObZvj9Xn2yPe/GfkIxV5dRnW6mBs4Gz7vWNmW/6luqvWd9z3iXzeV5Aw2Q== X-Received: by 2002:a05:6820:199b:b0:5c4:79a5:c535 with SMTP id 006d021491bc7-5d564fd4baemr4937194eaf.2.1721675089946; Mon, 22 Jul 2024 12:04:49 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:49 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 06/12] netlink: Add l_netlink_message class Date: Mon, 22 Jul 2024 14:04:23 -0500 Message-ID: <20240722190443.43196-6-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Initial implementation of the l_netlink_message class. For now, only basic message building is supported. Both RTNL (rtnetlink) and Generic Netlink (genetlink) messages can be created by including the appropriate header using l_netlink_message_add_header. --- ell/ell.sym | 6 ++ ell/netlink-private.h | 11 +++ ell/netlink.c | 174 ++++++++++++++++++++++++++++++++++++++++++ ell/netlink.h | 12 +++ 4 files changed, 203 insertions(+) diff --git a/ell/ell.sym b/ell/ell.sym index bc0c6e10ebca..247786d5ee98 100644 --- a/ell/ell.sym +++ b/ell/ell.sym @@ -434,6 +434,12 @@ global: l_netlink_register; l_netlink_unregister; l_netlink_set_debug; + l_netlink_message_new; + l_netlink_message_new_sized; + l_netlink_message_ref; + l_netlink_message_unref; + l_netlink_message_append; + l_netlink_message_add_header; /* path */ l_basename; l_path_find; diff --git a/ell/netlink-private.h b/ell/netlink-private.h index fdb584e62652..d04af749b9fb 100644 --- a/ell/netlink-private.h +++ b/ell/netlink-private.h @@ -31,6 +31,17 @@ enum nlmsgerr_attrs { #define NLM_F_ACK_TLVS 0x200 #endif +struct l_netlink_message { + int ref_count; + uint32_t size; + union { /* The actual data */ + struct nlmsghdr *hdr; + void *data; + }; +}; + bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg, const char **out_error_msg, uint32_t *out_error_offset); +int netlink_message_reserve_header(struct l_netlink_message *message, + size_t header_len, void **out_header); diff --git a/ell/netlink.c b/ell/netlink.c index 2e23c8903509..39c5bc335e19 100644 --- a/ell/netlink.c +++ b/ell/netlink.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "useful.h" #include "hashmap.h" @@ -687,3 +688,176 @@ bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg, return true; } + +static int message_grow(struct l_netlink_message *message, uint32_t needed) +{ + uint32_t grow_to; + + if (message->size - message->hdr->nlmsg_len >= needed) + return 0; + + /* + * Kernel places a practical limit on the size of messages it will + * accept, at least without tweaking SNDBUF. There's no (known) reason + * to send very large messages, so limit accordingly + */ + grow_to = message->hdr->nlmsg_len + needed; + if (grow_to > (1U << 20)) + return -EMSGSIZE; + + if (grow_to < l_util_pagesize()) + grow_to = roundup_pow_of_two(grow_to); + else + grow_to = align_len(grow_to, l_util_pagesize()); + + message->data = l_realloc(message->data, grow_to); + message->size = grow_to; + + return 0; +} + +static inline void *message_tail(struct l_netlink_message *message) +{ + return message->data + NLMSG_ALIGN(message->hdr->nlmsg_len); +} + +static int add_attribute(struct l_netlink_message *message, + uint16_t type, size_t len, + void **out_dest) +{ + struct nlattr *attr = message_tail(message); + int offset = message->hdr->nlmsg_len; + + attr->nla_type = type; + attr->nla_len = NLA_HDRLEN + len; + + if (len) { + void *dest = message_tail(message) + NLA_HDRLEN; + + memset(dest + len, 0, NLA_ALIGN(len) - len); + + if (out_dest) + *out_dest = dest; + } + + message->hdr->nlmsg_len += NLA_HDRLEN + NLA_ALIGN(len); + + return offset; +} + +LIB_EXPORT struct l_netlink_message *l_netlink_message_new_sized(uint16_t type, + uint16_t flags, size_t initial_len) +{ + struct l_netlink_message *message; + + message = l_new(struct l_netlink_message, 1); + + message->size = initial_len + NLMSG_HDRLEN; + message->hdr = l_realloc(NULL, message->size); + memset(message->hdr, 0, NLMSG_HDRLEN); + + message->hdr->nlmsg_len = NLMSG_HDRLEN; + message->hdr->nlmsg_type = type; + message->hdr->nlmsg_flags = flags; + /* seq and pid will be filled on send */ + message->hdr->nlmsg_pid = 0; + + return l_netlink_message_ref(message); + +} + +LIB_EXPORT struct l_netlink_message *l_netlink_message_new(uint16_t type, + uint16_t flags) +{ + return l_netlink_message_new_sized(type, flags, 256 - NLMSG_HDRLEN); +} + +LIB_EXPORT struct l_netlink_message *l_netlink_message_ref( + struct l_netlink_message *message) +{ + if (unlikely(!message)) + return NULL; + + __atomic_fetch_add(&message->ref_count, 1, __ATOMIC_SEQ_CST); + + return message; +} + +LIB_EXPORT void l_netlink_message_unref(struct l_netlink_message *message) +{ + if (unlikely(!message)) + return; + + if (__atomic_sub_fetch(&message->ref_count, 1, __ATOMIC_SEQ_CST)) + return; + + l_free(message->hdr); + l_free(message); +} + +LIB_EXPORT int l_netlink_message_append(struct l_netlink_message *message, + uint16_t type, + const void *data, size_t len) +{ + void *dest; + int r; + + if (unlikely(!message)) + return -EINVAL; + + if (len > USHRT_MAX - NLA_HDRLEN) + return -ERANGE; + + r = message_grow(message, NLA_HDRLEN + NLA_ALIGN(len)); + if (r < 0) + return r; + + r = add_attribute(message, type, len, &dest); + if (r < 0) + return r; + + memcpy(dest, data, len); + + return 0; +} + +int netlink_message_reserve_header(struct l_netlink_message *message, + size_t len, void **out_header) +{ + int r; + + if (message->hdr->nlmsg_len != NLMSG_HDRLEN) + return -EBADE; + + if (len > USHRT_MAX) + return -ERANGE; + + r = message_grow(message, NLA_ALIGN(len)); + if (r < 0) + return r; + + if (out_header) + *out_header = message_tail(message); + + memset(message_tail(message) + len, 0, NLA_ALIGN(len) - len); + message->hdr->nlmsg_len += NLA_ALIGN(len); + return 0; +} + +LIB_EXPORT int l_netlink_message_add_header(struct l_netlink_message *message, + const void *header, + size_t len) +{ + int r; + void *dest; + + if (unlikely(!message || !len)) + return -EINVAL; + + r = netlink_message_reserve_header(message, len, &dest); + if (r < 0) + return r; + + memcpy(dest, header, len); + return 0; +} diff --git a/ell/netlink.h b/ell/netlink.h index 3f3dc50e14be..70a55d652560 100644 --- a/ell/netlink.h +++ b/ell/netlink.h @@ -44,6 +44,18 @@ bool l_netlink_set_debug(struct l_netlink *netlink, l_netlink_debug_func_t function, void *user_data, l_netlink_destroy_func_t destroy); +struct l_netlink_message *l_netlink_message_new(uint16_t type, uint16_t flags); +struct l_netlink_message *l_netlink_message_new_sized(uint16_t type, + uint16_t flags, + size_t initial_size); +struct l_netlink_message *l_netlink_message_ref( + struct l_netlink_message *message); +void l_netlink_message_unref(struct l_netlink_message *message); +int l_netlink_message_append(struct l_netlink_message *message, uint16_t type, + const void *data, size_t len); +int l_netlink_message_add_header(struct l_netlink_message *message, + const void *header, size_t len); + #ifdef __cplusplus } #endif From patchwork Mon Jul 22 19:04:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739036 Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.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 BD79716EC00 for ; Mon, 22 Jul 2024 19:04:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675093; cv=none; b=mBPB+U21KeHqqkNf0i2OOqnMMh3qIAK7cVrhY3Wn1cZjpeF6A97JFYAvuXAIWFBRlCEtV74SYeYqRftpHc6IBKW8OGy+4CHA3ONj0Lt9fvBuA9Rse0bOsk+F6Oj9HPkOjXLkBiBca6BJEE7ykepk9avsqCG5JIH59Of5cxRVjnY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675093; c=relaxed/simple; bh=JTOKlvnHXKQP7X0qSl5f+r4NTRUFOVQuadEzm1D2zVg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fJl4bnNFvsIjVRJ5rllJusuO9IB2YOO5k8onsQ6cM6+hvuidDMEg8/3pC0vlsRe3ILSlwJKW2p7JG0yqqQ0WmJVLfts65YcyfEvGNvPx7XR7MYqaP7gugZ6vi3O6vW/wuKObiUSrAGPAV24FlGiUPuRgF4cRev3kDGJotQDlWb4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Av3oAjF6; arc=none smtp.client-ip=209.85.210.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Av3oAjF6" Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-704473c0698so1922251a34.3 for ; Mon, 22 Jul 2024 12:04:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675091; x=1722279891; darn=lists.linux.dev; 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=6Ar9oa9Y7D9VTC3vGfK+ZeqCQMwFCA81nm2GNev4SuE=; b=Av3oAjF6NKmnnPL89BjnydIZo33o8PY3DobIGu1N8L0e8hNgO1vzQwo6VWKigWth7x oPfcqIOgAAFhLYT05VpbMGUu82+W7282Brm7Wx52HbctHnpdtmnj8ZzTNm6lT08FegHt UTHRpUF85U5UmL6GZUehfkI3M5t5OLMy8mAuroBhd8ulnp/4icxyzuiCyk+jnPpqZqU4 JwQ2aCfA7mjC+KQ8NSwnTTfa2rZieuA1DTGhQX+AQcbKHc4T4LRQY1lihgtuOydFz/kT 4Xsh2iq/ARy+1G//3IcxhKCwawHG+bjlPZ5crrxMWCEcfrwgDiXBZBR8sMkeXDh2OjW6 7IFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675091; x=1722279891; 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=6Ar9oa9Y7D9VTC3vGfK+ZeqCQMwFCA81nm2GNev4SuE=; b=IuRo71B9lW1nJLB5N7YWEn7jepR79ts1F6ztMrJUCoZ3S49FkVv4BVzGpbMwgd4Vw7 /nnYRvf+gCN2EVSXjiE+KkqQY7HP1Np2WrTEZtOqXG+u2nksFDpjrxxwcFRvsnvfHMpE k7OhDCksqi+y1CkNfRI2ktcDkXEtHgIpi8jzIHnZ2U2O1hSIZ1GZi4R/pmtX194jylSt B+kA/v0wVSHdq0/FfNJwmoWjajcDx3/OHt6Z03eX2seCy0icllbVVN4dqSp0cYfbW8gW Gox9IA9BmNE9yCF5PDLOOtciMjGPQt5tAud8Rs8U3fTzwd+2QHPY+yN9oeNRELQvOvD0 xGtQ== X-Gm-Message-State: AOJu0YxVvP/rDFZ0JGSXk5Ecjr6J8ZpHQ/h0nwh5Tm0vVWA+zfSG3g4b mn6QjPTp1ZgWB2wcYovedbuYm6j3xSPxIPeitVMqdPLVQDbLOZbrYGZMbA== X-Google-Smtp-Source: AGHT+IEqm0khNybxLCHAXP2u62aDbSJjZTD72H1mQPuzCzWesBJMaMwYLMzyXbKmcqSOESd5GxzDKg== X-Received: by 2002:a05:6830:621a:b0:708:e969:e0d9 with SMTP id 46e09a7af769-709009258edmr7395081a34.17.1721675090598; Mon, 22 Jul 2024 12:04:50 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:50 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 07/12] netlink: support appending using scatter / gather Date: Mon, 22 Jul 2024 14:04:24 -0500 Message-ID: <20240722190443.43196-7-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This version builds the attribute using scatter / gather semantics (iovs) in order to minimize the number of copies required to build the message. --- ell/ell.sym | 1 + ell/netlink.c | 34 ++++++++++++++++++++++++++++++++++ ell/netlink.h | 3 +++ 3 files changed, 38 insertions(+) diff --git a/ell/ell.sym b/ell/ell.sym index 247786d5ee98..b99b6e552eaa 100644 --- a/ell/ell.sym +++ b/ell/ell.sym @@ -439,6 +439,7 @@ global: l_netlink_message_ref; l_netlink_message_unref; l_netlink_message_append; + l_netlink_message_appendv; l_netlink_message_add_header; /* path */ l_basename; diff --git a/ell/netlink.c b/ell/netlink.c index 39c5bc335e19..d7294a95f20d 100644 --- a/ell/netlink.c +++ b/ell/netlink.c @@ -821,6 +821,40 @@ LIB_EXPORT int l_netlink_message_append(struct l_netlink_message *message, return 0; } +LIB_EXPORT int l_netlink_message_appendv(struct l_netlink_message *message, + uint16_t type, + const struct iovec *iov, size_t iov_len) +{ + size_t len = 0; + void *dest; + size_t i; + int r; + + if (unlikely(!message)) + return -EINVAL; + + for (i = 0; i < iov_len; i++) + len += iov[i].iov_len; + + if (len > USHRT_MAX - NLA_HDRLEN) + return -ERANGE; + + r = message_grow(message, NLA_HDRLEN + NLA_ALIGN(len)); + if (r < 0) + return r; + + r = add_attribute(message, type, len, &dest); + if (r < 0) + return r; + + for (i = 0, len = 0; i < iov_len; i++, iov++) { + memcpy(dest + len, iov->iov_base, iov->iov_len); + len += iov->iov_len; + } + + return 0; +} + int netlink_message_reserve_header(struct l_netlink_message *message, size_t len, void **out_header) { diff --git a/ell/netlink.h b/ell/netlink.h index 70a55d652560..4d1c4761b3bb 100644 --- a/ell/netlink.h +++ b/ell/netlink.h @@ -53,6 +53,9 @@ struct l_netlink_message *l_netlink_message_ref( void l_netlink_message_unref(struct l_netlink_message *message); int l_netlink_message_append(struct l_netlink_message *message, uint16_t type, const void *data, size_t len); +int l_netlink_message_appendv(struct l_netlink_message *message, + uint16_t type, + const struct iovec *iov, size_t iov_len); int l_netlink_message_add_header(struct l_netlink_message *message, const void *header, size_t len); From patchwork Mon Jul 22 19:04:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739037 Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (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 98D2116EC1E for ; Mon, 22 Jul 2024 19:04:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675094; cv=none; b=FlzWAKOy58+KmjpTXixYOkBnA3ZB0LsDVunBn5s8bOieEQbYmf4xwLmMYhbeODHchJxJC7HVOFelADVKxKxVD0FJCBr4zYw6KYBEs0jwPt/RC+oIxhBXcLIQEAIbX62yJVE85xn6Eu2I2YTS8+NAV2e4oj8vD8qp8TNeZHrEg/s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675094; c=relaxed/simple; bh=pP2vi1bgFOJY4S+vn73KcTZhn4Jvg/Q/82otUHpYyoA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WCQcGx5w0qNaATwttUHvA6fC+PSMhJHH/jICVioOmjmq5JnPKm7Gqoj6XD42dJEo4AnRpesk20DZwLs2YFDoLYMl5vW74kRqocfYo5G/YZ/SdGpLpvAuaQ8pRNMPA0Q9hJf7dPQ70+XVCYHVK5RHEbPc85NjQ45qjtaM3jtJrxs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=E9g7CBRJ; arc=none smtp.client-ip=209.85.161.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="E9g7CBRJ" Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-5c7b6c6a581so2108164eaf.0 for ; Mon, 22 Jul 2024 12:04:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675091; x=1722279891; darn=lists.linux.dev; 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=uTI90/cK/NxkdJK+sulTQBjvT37WA/2Of7VzJNu9jec=; b=E9g7CBRJDiZ4x5C1cniCsToA9c9XHt2faB/wP1mR3paKzP9qh1vH4HzjpOwapoZAW6 5HXlmhz61KcX6hMJ1bB+rzpyL8zoPYcnpK7WBKPiYDPw3lFewIySiSivuDi+VUKP91a5 /lur1yY/dYrwU/IzOTW6/3Nj48+QoPUfycnvxZ91MfjNV+kNXHt3Rz5cGMWWQfEiifcQ ERfJ4oFrRBDvj2Ndk+3i3VkdgBno5kfvloKZGZbvxhOXNXBT3U6crN12EHnCTbBN/QfE xHEhxTl+bVDGE2D/nnyUtZdoHLw/RcKNcGuxnJOC+MMvq3+0z4vdYyt788xVHh5whe/J Yv1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675091; x=1722279891; 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=uTI90/cK/NxkdJK+sulTQBjvT37WA/2Of7VzJNu9jec=; b=YkRqb+3ALlNexaI/pOrfsfy5Wg9GVdcrkGypzDBrhaXOp/28+D6kNI5fbTy1IEsqEu ZJjalTr08BWphA/xeHdd+wonDLl707iM5PtZQ3xWxQ0Bwu4FnXenCoLsY54Uwz7UiIy0 N902Tdw6XampN9aGCJ4PbHOkP5fgxHW/e2cCgqbTf7fAx8mMHU/8k/IiA/FBoNnZ+cyW naNp7n5mWB+SJ6hH9ASErYYvlsIgFBihHcfsmU4zzM4cGOlXnA46u4oKwrsRepgKAePK Cs5Tt78gWvAPc7wnzWShYZDVhSGnjron3pi6H4e49IpN4fg4ArcUel3eg3NB7XxDkEvW xvWg== X-Gm-Message-State: AOJu0YzUIwiLdjcNFTB95jUTUKmw2twAIXC+J6KlKf6CR45qIB9IMxNK BaAN20p4ejFtOOPTgRXXIjIDkyZCZkTqBO6l+UG/2s+Mak14vTQErBtyOQ== X-Google-Smtp-Source: AGHT+IFCRzFBZZAGXFa82Mp6t989FmD4c8oXUBpgapfPKI4DftSZhwQIbRBfje2ydC4GIASNJlQu+A== X-Received: by 2002:a05:6820:1e07:b0:5c6:9320:2df6 with SMTP id 006d021491bc7-5d58b866f09mr1149744eaf.7.1721675091397; Mon, 22 Jul 2024 12:04:51 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:51 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 08/12] netlink: Support adding nested attributes to messages Date: Mon, 22 Jul 2024 14:04:25 -0500 Message-ID: <20240722190443.43196-8-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 --- ell/ell.sym | 2 ++ ell/netlink-private.h | 2 ++ ell/netlink.c | 54 +++++++++++++++++++++++++++++++++++++++++++ ell/netlink.h | 3 +++ 4 files changed, 61 insertions(+) diff --git a/ell/ell.sym b/ell/ell.sym index b99b6e552eaa..0dad73f5546e 100644 --- a/ell/ell.sym +++ b/ell/ell.sym @@ -441,6 +441,8 @@ global: l_netlink_message_append; l_netlink_message_appendv; l_netlink_message_add_header; + l_netlink_message_enter_nested; + l_netlink_message_leave_nested; /* path */ l_basename; l_path_find; diff --git a/ell/netlink-private.h b/ell/netlink-private.h index d04af749b9fb..db4317786a8f 100644 --- a/ell/netlink-private.h +++ b/ell/netlink-private.h @@ -38,6 +38,8 @@ struct l_netlink_message { struct nlmsghdr *hdr; void *data; }; + uint32_t nest_offset[4]; + uint8_t nest_level; }; bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg, diff --git a/ell/netlink.c b/ell/netlink.c index d7294a95f20d..f73b876b4ef0 100644 --- a/ell/netlink.c +++ b/ell/netlink.c @@ -727,6 +727,15 @@ static int add_attribute(struct l_netlink_message *message, { struct nlattr *attr = message_tail(message); int offset = message->hdr->nlmsg_len; + int i; + + for (i = 0; i < message->nest_level; i++) { + uint32_t nested_len = offset + NLA_HDRLEN + NLA_ALIGN(len) - + message->nest_offset[i]; + + if (nested_len > USHRT_MAX) + return -ERANGE; + } attr->nla_type = type; attr->nla_len = NLA_HDRLEN + len; @@ -895,3 +904,48 @@ LIB_EXPORT int l_netlink_message_add_header(struct l_netlink_message *message, memcpy(dest, header, len); return 0; } + +LIB_EXPORT int l_netlink_message_enter_nested(struct l_netlink_message *message, + uint16_t type) +{ + int r; + + if (unlikely(!message)) + return -EINVAL; + + if (unlikely(message->nest_level == L_ARRAY_SIZE(message->nest_offset))) + return -EOVERFLOW; + + r = message_grow(message, NLA_HDRLEN); + if (r < 0) + return r; + + r = add_attribute(message, type | NLA_F_NESTED, 0, NULL); + if (r < 0) + return false; + + message->nest_offset[message->nest_level] = r; + message->nest_level += 1; + + return 0; +} + +LIB_EXPORT int l_netlink_message_leave_nested(struct l_netlink_message *message) +{ + struct nlattr *nla; + uint32_t offset; + + if (unlikely(!message)) + return -EINVAL; + + if (unlikely(message->nest_level == 0)) + return -EOVERFLOW; + + message->nest_level -= 1; + offset = message->nest_offset[message->nest_level]; + + nla = message->data + offset; + nla->nla_len = message->hdr->nlmsg_len - offset; + + return 0; +} diff --git a/ell/netlink.h b/ell/netlink.h index 4d1c4761b3bb..593cddf1aee8 100644 --- a/ell/netlink.h +++ b/ell/netlink.h @@ -58,6 +58,9 @@ int l_netlink_message_appendv(struct l_netlink_message *message, const struct iovec *iov, size_t iov_len); int l_netlink_message_add_header(struct l_netlink_message *message, const void *header, size_t len); +int l_netlink_message_enter_nested(struct l_netlink_message *message, + uint16_t type); +int l_netlink_message_leave_nested(struct l_netlink_message *message); #ifdef __cplusplus } From patchwork Mon Jul 22 19:04:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739038 Received: from mail-oi1-f172.google.com (mail-oi1-f172.google.com [209.85.167.172]) (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 464A318637 for ; Mon, 22 Jul 2024 19:04:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675094; cv=none; b=tTw5ukdaq03CfvigAZcpfiu49wEuDWFtl1G42eXbmOs90deF6Xaye4f3V7IFdlDE6oo7PEkbxTcKiTFfu6BeW7dpN/QIbh3q+iFqNAQV7889nusUSIK34cV5PhjV7nOoIB4Ia2VJy9qB+4q7xLjlb7pW04O6H3bNiBEZ72s63JQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675094; c=relaxed/simple; bh=mvxvRCkpeY/8mm4UHwjWK1nhilAomrMCnKrpmabG2P8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qM5ZXGal9x9T/IAGsAG5/KYtF9yLKYre4bw426Onf5cJoCgkXL7N43SSWW/sRpOoNBB3jPukbl2E/GryRqG7i9DbnHdXhrhuERsDjCQKA30E3sx5E5MSbFC2qaBcpX2bikjGR5JiU6BHEO0x9R/ERB0PyeoPKHfHo+xd/mExHgw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=j789Svhk; arc=none smtp.client-ip=209.85.167.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="j789Svhk" Received: by mail-oi1-f172.google.com with SMTP id 5614622812f47-3daf0e73a59so801034b6e.1 for ; Mon, 22 Jul 2024 12:04:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675092; x=1722279892; darn=lists.linux.dev; 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=vdx1MTQkyWlY4DSqjiy9jL2ROlqXPrp4F3hAgRBBBPw=; b=j789SvhkAIyp/0e8fICqSuza3Mcw4gzeQkX1lSuGOsdDQv7QxXV1jvT8Bybs9m0ATR zHdZ42UD2hdo9Y3W/ma8cZADFaL8UrAq8h2QiDurEdIm1cKOFs3weiutduFzX3o4MfWC YovzHWemQL/ogTrU6xzqll4chWFv7isWirBb4NuqwJ1tjrXpSVVzORBcqFaN7dBNc7Wp a9tZ9ni5l0S7K5tT3FOplyxiCGMI7ZVzh270HYFbQaFKMnRqxrZz0Vy8IpNWsTXfmGo3 jPYqviIyhWzxSpH+Fhz+1+Vlcx3r0C+GuhWWNEcaQaLx4UQFAQcLJlhnfqBM5UUDFXzb 725A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675092; x=1722279892; 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=vdx1MTQkyWlY4DSqjiy9jL2ROlqXPrp4F3hAgRBBBPw=; b=EO29lj4kh1gL4KfP8ldzsb3ueF7sy9lTI2DEwaIr7cqmrbT4bREJuNd8F/M2LPyo9Y K1uhQBPSYKpnTX4AEpLOxjf4i9LBLEoMRP7/ysm+02/lHSG0VH6MdkVGZGTyyVVsJl+0 p/NlQqUiDYinJvtsckndO4wGtzYqDi5qh7MFtbQJ2p3AoXKcbkEKU3HoyCAraYauJxW4 P8IEOa5jQNekzMFNZEgOSBq83AX3Frtpdn2UymAi9BdTu1ETvLHmVzFq2LBimuzm9IOA uEOs9sI457sQuNPB7UGFJ+Xs7FoWD6XbSXLKklRUzQFu1svW8UjAnQrHTVJyM/p9WT4w m74A== X-Gm-Message-State: AOJu0YzNaK48HEqyzaK0WJFQjlDWqxxLJnN4v66JTozTprZC9XHaKC6p UTXweHCTLQHxo25YhbFWCTr/Zj8PwYRfjTIk7znKbRJ/fcsPFnN5UhQOeg== X-Google-Smtp-Source: AGHT+IE8n175O0mq8MjSFisIHLJzOwwIGkgg/G9Wl2mlGcO+kgDI2llf9QN4a1u3zkVYAcA+9QQtpQ== X-Received: by 2002:a05:6808:2387:b0:3d9:2104:47e6 with SMTP id 5614622812f47-3dad73dc129mr7565684b6e.13.1721675092352; Mon, 22 Jul 2024 12:04:52 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:52 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 09/12] netlink: Add additional helpers Date: Mon, 22 Jul 2024 14:04:26 -0500 Message-ID: <20240722190443.43196-9-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Helpers around l_netlink_message_append for use with common data types. --- ell/netlink.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/ell/netlink.h b/ell/netlink.h index 593cddf1aee8..e7962b9932dc 100644 --- a/ell/netlink.h +++ b/ell/netlink.h @@ -62,6 +62,61 @@ int l_netlink_message_enter_nested(struct l_netlink_message *message, uint16_t type); int l_netlink_message_leave_nested(struct l_netlink_message *message); +static inline int l_netlink_message_append_u8(struct l_netlink_message *message, + uint16_t type, uint8_t u8) +{ + return l_netlink_message_append(message, type, &u8, sizeof(uint8_t)); +} + +static inline int l_netlink_message_append_u16(struct l_netlink_message *message, + uint16_t type, uint16_t u16) +{ + return l_netlink_message_append(message, type, &u16, sizeof(uint16_t)); +} + +static inline int l_netlink_message_append_u32(struct l_netlink_message *message, + uint16_t type, uint32_t u32) +{ + return l_netlink_message_append(message, type, &u32, sizeof(uint32_t)); +} + +static inline int l_netlink_message_append_u64(struct l_netlink_message *message, + uint16_t type, uint64_t u64) +{ + return l_netlink_message_append(message, type, &u64, sizeof(uint64_t)); +} + +static inline int l_netlink_message_append_s8(struct l_netlink_message *message, + uint16_t type, int8_t s8) +{ + return l_netlink_message_append(message, type, &s8, sizeof(int8_t)); +} + +static inline int l_netlink_message_append_s16(struct l_netlink_message *message, + uint16_t type, int16_t s16) +{ + return l_netlink_message_append(message, type, &s16, sizeof(int16_t)); +} + +static inline int l_netlink_message_append_s32(struct l_netlink_message *message, + uint16_t type, int32_t s32) +{ + return l_netlink_message_append(message, type, &s32, sizeof(int32_t)); +} + +static inline int l_netlink_message_append_s64(struct l_netlink_message *message, + uint16_t type, int64_t s64) +{ + return l_netlink_message_append(message, type, &s64, sizeof(int64_t)); +} + +static inline int l_netlink_message_append_mac(struct l_netlink_message *message, + uint16_t type, + const uint8_t mac[static 6]) +{ + return l_netlink_message_append(message, type, mac, 6); +} + #ifdef __cplusplus } #endif From patchwork Mon Jul 22 19:04:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739039 Received: from mail-oo1-f42.google.com (mail-oo1-f42.google.com [209.85.161.42]) (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 297152032A for ; Mon, 22 Jul 2024 19:04:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675095; cv=none; b=FwFPC0d52xpp56Q8TWldxWJ4hI9H5u40xT3VsCjPrJ5pYs3BMeDT8Caou3rxvuF98llKhqza3/YJwix6GwnBLAwnRqZDhR2GSKs4+wVsV3jsioOJHgR4PXIT6P2LeQiYO/SqVueGBg43cYXgAf1Wl0aV1xiIb73hk1rnEErJGaI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675095; c=relaxed/simple; bh=85kKHV44JaXoI4q7w84xKPeWEE/wt9uU8oEgxhgYL2I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HiZ1OWZurrDnvDC9oHxzMExNpQhmvZJHJLoOMB8L8Qrf9gbxAOgKpGqiE8iH4zFTT1pdJp5bJPuKPvLH9AROALROiObmw3G3Qd3jyxV3ruHBucPxnRj8jKfpMkY4yGVTwsKo3JvNEfr7fkkbMyepONMcqvwKgQzDlA9xkY/v4is= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NdLy/yxG; arc=none smtp.client-ip=209.85.161.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NdLy/yxG" Received: by mail-oo1-f42.google.com with SMTP id 006d021491bc7-5d569902626so1171069eaf.2 for ; Mon, 22 Jul 2024 12:04:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675093; x=1722279893; darn=lists.linux.dev; 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=DyyfFvvgXQKHvzqcKXjAA0jfXa5GRyrMHP/lNXZeszY=; b=NdLy/yxGyWW7JI0UqK8C2bOSUEmo7khOgmOYvy88NSKr5HZOelYNZCZPwKIgiagF3n cdm/MfxqGM5LGqd1iyp7Vl25Iq5QqonVx/3vG5vl/7iD9ODaQBoiLQ/im5EIIRUNbODa aOQdGlYUuY3M/nzvN+TYGdQfJxIM68HZn/5WuZnh2E5F8BF7LsUYoRR2Cra8+5RW3hcR JGV7YVZfl88eh/N9FgrzTerSKpAL9DaUBc/zXNZlhdiGRhSc7k5J+BOqPQsCMKqpzmI2 HjXbvqp2BbLb6Jd4CJthB/PLmThN5/S8Ah6fr8Ltoaq9CVoGjbIOMWN2UUnHvnX2OhMI ZZNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675093; x=1722279893; 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=DyyfFvvgXQKHvzqcKXjAA0jfXa5GRyrMHP/lNXZeszY=; b=kYQ9J3hQVJi+99X9G6ZYqJxvEm3Ezl5yt6nAhdaKAZSh2gc2N2s3v+mVfZtbRjSrWy UK9LhkKoRVUzs4N0mqpZ8lu2xarTsQvc7OpU9ndY7DTIFLHuh6KLvi4DlvBKtRExh8se gdzyGTpfMSt1eO4OSUZ7RKB8GBat+UGsgEF6Di2l4x3njdX7tk7WJ6izqhyZdLUHxjYR Qlyqx0UlyJmyC+KfRTsgCSmUyFUn6CLxVV3RvwEY+As3y1KxIkaQu1V1GxgfhSFnfT1J 2A+l7DCcbcH4JgSfpmSvOYMKdVRGakGUZ4LG3yp73ErITeKpsZZ7wDtR/d6QKNmpzfff 4M4Q== X-Gm-Message-State: AOJu0Yxr74vnRdSRbKKGGMVLOm1SpBQUtKjE8Xl8EpoJopeHCkXpcGuZ nyBYzWbBlCK5t3esrk057pXQ51x17vbPXusBLqs8emgH8JqVIR32TAxEYw== X-Google-Smtp-Source: AGHT+IEB3rbfpOIiXNYvbRaM7anY7INGakDnbPgZ7SiTZs/hG3Iscx4dJ6wvWbCxMO6urUlAYt7qzg== X-Received: by 2002:a05:6820:1e01:b0:5c4:2497:c92d with SMTP id 006d021491bc7-5d564e497femr10406308eaf.2.1721675093128; Mon, 22 Jul 2024 12:04:53 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:52 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 10/12] unit: Add netlink message builder / parser tests Date: Mon, 22 Jul 2024 14:04:27 -0500 Message-ID: <20240722190443.43196-10-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 These tests are similar to l_genl_msg based tests, but use l_netlink_message APIs directly. --- unit/test-genl-msg.c | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/unit/test-genl-msg.c b/unit/test-genl-msg.c index e6432f4362ef..3d996f4e242a 100644 --- a/unit/test-genl-msg.c +++ b/unit/test-genl-msg.c @@ -14,6 +14,8 @@ #include #include +#include "ell/netlink-private.h" + static bool do_print = false; static void do_debug(const char *str, void *user_data) @@ -126,6 +128,38 @@ static void build_set_station(const void *data) l_genl_msg_unref(msg); } +static void build_set_station_netlink(const void *data) +{ + const struct set_station_test *test = data; + struct l_netlink_message *m; + struct genlmsghdr genlhdr = { .cmd = 18, .version = 0, .reserved = 0 }; + + m = l_netlink_message_new(0x17, NLM_F_REQUEST | NLM_F_ACK); + assert(m); + + assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr))); + assert(!l_netlink_message_append_u32(m, 3, test->ifindex)); + assert(!l_netlink_message_append_mac(m, 6, test->mac)); + assert(!l_netlink_message_append(m, 67, + test->flags, sizeof(test->flags))); + + m->hdr->nlmsg_seq = test->seq; + m->hdr->nlmsg_pid = test->pid; + + if (do_print) { + l_util_hexdump(false, m->data, m->hdr->nlmsg_len, + do_debug, "[MSG] "); + l_util_hexdump(true, set_station_request, + sizeof(set_station_request), do_debug, "[MSG] "); + } + + assert(m->hdr->nlmsg_len == sizeof(set_station_request)); + assert(!memcmp(m->data, set_station_request, + sizeof(set_station_request))); + + l_netlink_message_unref(m); +} + static const unsigned char set_rekey_offload_request[] = { 0x54, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x05, 0x00, 0x59, 0xa3, 0xe1, 0x53, 0xba, 0x02, 0x40, 0xe7, 0x4f, 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0x00, @@ -252,6 +286,42 @@ static void build_set_rekey_offload(const void *data) l_genl_msg_unref(msg); } +static void build_set_rekey_offload_netlink(const void *data) +{ + const struct set_rekey_offload_test *test = data; + struct l_netlink_message *m; + struct genlmsghdr genlhdr = { .cmd = 79, .version = 0, .reserved = 0 }; + + m = l_netlink_message_new(0x1b, NLM_F_REQUEST | NLM_F_ACK); + assert(m); + + assert(!l_netlink_message_add_header(m, &genlhdr, sizeof(genlhdr))); + assert(!l_netlink_message_append_u32(m, 3, test->ifindex)); + assert(!l_netlink_message_enter_nested(m, 122)); + assert(!l_netlink_message_append(m, 1, test->kek, sizeof(test->kek))); + assert(!l_netlink_message_append(m, 2, test->kck, sizeof(test->kck))); + assert(!l_netlink_message_append(m, 3, test->replay_counter, + sizeof(test->replay_counter))); + assert(!l_netlink_message_leave_nested(m)); + + m->hdr->nlmsg_seq = test->seq; + m->hdr->nlmsg_pid = test->pid; + + if (do_print) { + l_util_hexdump(false, m->data, m->hdr->nlmsg_len, + do_debug, "[MSG] "); + l_util_hexdump(true, set_rekey_offload_request, + sizeof(set_rekey_offload_request), + do_debug, "[MSG] "); + } + + assert(m->hdr->nlmsg_len == sizeof(set_rekey_offload_request)); + assert(!memcmp(m->data, set_rekey_offload_request, + sizeof(set_rekey_offload_request))); + + l_netlink_message_unref(m); +} + /* * This example is generated by libnl: msg = nlmsg_alloc(); @@ -441,8 +511,12 @@ int main(int argc, char *argv[]) parse_set_rekey_offload, &rekey_offload); l_test_add("Build Set Station Request", build_set_station, &set_station); + l_test_add("Build Set Station Request (Netlink)", + build_set_station_netlink, &set_station); l_test_add("Build Set Rekey Offload Request", build_set_rekey_offload, &rekey_offload); + l_test_add("Build Set Rekey Offload Request (Netlink)", + build_set_rekey_offload_netlink, &rekey_offload); l_test_add("libnl-generated Example with Nesting", parse_libnl_nested, NULL); From patchwork Mon Jul 22 19:04:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739040 Received: from mail-oo1-f54.google.com (mail-oo1-f54.google.com [209.85.161.54]) (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 E7F7F18637 for ; Mon, 22 Jul 2024 19:04:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675096; cv=none; b=n4RIFt5a8c6159nZRApVPGvY6zNrAgPihUuddVvEXhx+Hoj0fwVpN0xR0roDlhX9QZ0pwP+Oa9cQsU8ZUpq8gLfVf7QoE0E2MPpcS0KPHr0ikvoNvPbN71x7NVMAohZ0eGZkFLz6xzO71Bvt6OPBhMrad2h1/CC4AJwzf1HP+cQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675096; c=relaxed/simple; bh=Hn1CNurE9BtxPUuHaG8+6p0mH5LbXv5yJ2AEzO1LVA4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XwdoXCZfBYjsGp6JDFNA03QzwslhTkzh+P0sSCcMAURI4r7tNsD3AvHg6fc+73BYnoJqyLXxmrvlAJQTaw5JuNRwpBxAmgPiNg6UPKjNQcAJCP/S4t8N/LjVpq8O4bJOTmurIEzwvNHl4tJBl/5o55kfDgMzGFm0VQc6cthY0fI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RKz+LFp8; arc=none smtp.client-ip=209.85.161.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RKz+LFp8" Received: by mail-oo1-f54.google.com with SMTP id 006d021491bc7-5d569bee00aso1248059eaf.1 for ; Mon, 22 Jul 2024 12:04:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675094; x=1722279894; darn=lists.linux.dev; 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=FnIA1vupo76R42JBHhMrMF2WBhjdzyZwSHusD5SML1s=; b=RKz+LFp8V1oxuE+2eJr2JNfJQlSoXFHYwuK6uLrXb1nLcsOrPe53I2ob3KlmHg3+id Hta0Xtm2spOkLOmx2u7MIj3SkPd1Vz0qsw6DmOt/YsrzclQ/WYE5cY1VfgVO6yjZ7YuU AYqrgeHRXENMJN2xqwelcINJBWKFpsE3P+xhi9bGnTgloVzDcuak7Pru76jBswHANROf IZwrh3s80h0nJRPE7ylPhrUHBWRK6e3Lk3CqXMS1gT85sI3D1RcFHFgkxNDUKfIUOY5e WWg928wk/CNz5PTXafgv6HcH/pjHnlte1wVfAR8JiC+6HuryZ2GtCTgxxdOeMmZBgdyI LeKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675094; x=1722279894; 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=FnIA1vupo76R42JBHhMrMF2WBhjdzyZwSHusD5SML1s=; b=fYpxopgtCK2CCGvph6SeGIkiqY9zLjWwokckQK0p/8PMHtw5s8kIJZi4BuLQPWf5Ft 3SAxtTgBzL2/eWLS61yiJ3sNi248u7CoI2NwbCF1jnlyLmOE+ha7xuD/RXA6mj5zxwst yo84Kg/gjkh+gdCFwktyFlpEA41wqQDiEEKLSjdPMrxLy1tJGUEHtWWbNirRptROatrO DFppC7rR9iVofWW9Sa0j7cbYoG19GrDYlnSRbyXjqOkLQ8Ag4O7+Bn/5DEFtcZzkH1ps Rwo0ZIy3OUC+AMTWsGdOPfdIHJDDnxATUApM7cOtk7DO1e+qL8SSYFfRJvRars3uCvNP wkHA== X-Gm-Message-State: AOJu0YwuG4S0LH35I7F2H+F8ClbOu6OoSHtIWSpr5u8QcmtvbgOJ8xWH Ks7Zl6sNoX38/zz4ZVbncf/x6b/q//iitrsZloG+HW0ITbq9XqGqfMIYiw== X-Google-Smtp-Source: AGHT+IFFYPy3iU9vq/j0ZslcpOwZdI4eUfUgE3Nh2mYjfh2apz8UgpnchHwdj/CyD82EkTbA7ggBpw== X-Received: by 2002:a05:6820:210c:b0:5c7:b679:4503 with SMTP id 006d021491bc7-5d564cfc023mr11127482eaf.0.1721675093791; Mon, 22 Jul 2024 12:04:53 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:53 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 11/12] netlink: add netlink_message_from_nlmsg Date: Mon, 22 Jul 2024 14:04:28 -0500 Message-ID: <20240722190443.43196-11-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This is a low level utility to create a l_netlink_message from raw data represented by struct nlmsghdr. Typically used on the RX path when reading from the socket, or by unit tests. --- ell/netlink-private.h | 2 ++ ell/netlink.c | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/ell/netlink-private.h b/ell/netlink-private.h index db4317786a8f..b114f62f6bf3 100644 --- a/ell/netlink-private.h +++ b/ell/netlink-private.h @@ -47,3 +47,5 @@ bool netlink_parse_ext_ack_error(const struct nlmsghdr *nlmsg, uint32_t *out_error_offset); int netlink_message_reserve_header(struct l_netlink_message *message, size_t header_len, void **out_header); +struct l_netlink_message *netlink_message_from_nlmsg( + const struct nlmsghdr *nlmsg); diff --git a/ell/netlink.c b/ell/netlink.c index f73b876b4ef0..024740b62fc3 100644 --- a/ell/netlink.c +++ b/ell/netlink.c @@ -781,6 +781,17 @@ LIB_EXPORT struct l_netlink_message *l_netlink_message_new(uint16_t type, return l_netlink_message_new_sized(type, flags, 256 - NLMSG_HDRLEN); } +struct l_netlink_message *netlink_message_from_nlmsg( + const struct nlmsghdr *nlmsg) +{ + struct l_netlink_message *message = l_new(struct l_netlink_message, 1); + + message->hdr = l_memdup(nlmsg, nlmsg->nlmsg_len); + message->size = nlmsg->nlmsg_len; + + return l_netlink_message_ref(message); +} + LIB_EXPORT struct l_netlink_message *l_netlink_message_ref( struct l_netlink_message *message) { From patchwork Mon Jul 22 19:04:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Denis Kenzior X-Patchwork-Id: 13739041 Received: from mail-oo1-f42.google.com (mail-oo1-f42.google.com [209.85.161.42]) (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 A89C416F0CE for ; Mon, 22 Jul 2024 19:04:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675097; cv=none; b=DbPVjnVIkcupmFD1DNJFeVO8qqIMktYUI7T3pP/uI19w75n6yojsfn0IOIE3ZJj/lovJ3hISMQaRvH7JDIw/EdYoCL/0hqdOIjbmQDdNO8Ik8dZaSKs/TDL1nwxZN16QT5KhhQujT4sSLehLhXeblD+gYulYEQA91OV1PkqyBTU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721675097; c=relaxed/simple; bh=jHZKLbVmRn9+/y5X+scseqa94IU1ZJGOyk8nYd6bOL4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fe18dKGNDByjtm32fk93AuWLtwLg52sk5T/OiDnwR8J5GzCeUrfFnGFrEz26IQPGuGLcDoKkCOYu8VxIKLkz6X9Gg2V5Bwu3aAtHwiffqk1fYilIF1+0wmSa4TyBZbFG87sgzJK6TW/ebY13HmSdc4AjoCkWzdTJ8cppK7Nb/fA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CRIpAuxU; arc=none smtp.client-ip=209.85.161.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CRIpAuxU" Received: by mail-oo1-f42.google.com with SMTP id 006d021491bc7-5d5846f799dso457727eaf.3 for ; Mon, 22 Jul 2024 12:04:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721675094; x=1722279894; darn=lists.linux.dev; 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=aGNxvHmkTR/bsMHw5Vr0kOh6lxHjm/NM/yldEGmEh+M=; b=CRIpAuxU94lizDjthSfDC04KrYWDnTh3NQP437ukt8BdXNzZ6MADeInCmsalLN6Crj peqlA8SHtMhU3m3oZoT5pEzRgMsHJB8WJ4/GV+ZQ9QBueg9SjtB1HxkacE+EsJUkJnbW kBpdy4J+wiPOcHbeITuARE+elmlGTIt8B2H+JAUVNFWjo2dprlo7g9K+G7Si2elYIGAU issOlxm2Q9O63BZ7nBovTqZOiOrjVHjVFDfsA+lJszl4GQziClv8qTggarT/W0ZEK1rF 7gnE6aruqJ0gE6Ia1msMXJ84z9QKVIN1pvcNhvy+Ax39PTM9s7L+CW6aFTaVwrTwP/xC cy8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721675094; x=1722279894; 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=aGNxvHmkTR/bsMHw5Vr0kOh6lxHjm/NM/yldEGmEh+M=; b=rePIdOgpwc6KjL4OJ62y1MUduCU/FMhw+TxajW3wSBUa7i6e+Pz8aPio/rFvTMUHg2 sWVK5XUCT+Msaeq94Jiar1vIxyhjqsPdPdm5I7Dk6ZuXBwlh+Z7SQIPN1/TaiLwMhiFI bM9VOFTE7mBDzrvuXdOhaLe8sWaObQnyyVjPnH/+FD56e3207Ky1dUOehsWsDkbtBxKq nVnpo1FV0dmPkCzIlOBYccgh6bkIfNpjhbRv/4silosqgRKGTZRCyKvOppBCZ+g+Vyg8 qk7gBZr6JVUIubZD6KxIDWJjROlljtOnKuAOAVwnTzPXORq2Slu/CkbYvCgODYtGOd5f 8Oqg== X-Gm-Message-State: AOJu0Yy6pNAnK1Y6kK3ffyh9yox9BaWdPgkUQikdGPXmotSjSi5frNjN Cjqw+TFQ1J4hFqZKEHod4RQ1BAOLz6tPyWEBWgiqy4ZQGv6vjLCiWJNGIQ== X-Google-Smtp-Source: AGHT+IEfraIw5eXiReCCG/HcqChupPVIRg2GCR42ORTHaYSo/X3U13sV4LmqQYaMgo+im1oo0gEOBg== X-Received: by 2002:a4a:ee83:0:b0:5c4:5cbc:b1b5 with SMTP id 006d021491bc7-5d564cfc26dmr10377239eaf.0.1721675094554; Mon, 22 Jul 2024 12:04:54 -0700 (PDT) Received: from localhost.localdomain (syn-070-114-247-242.res.spectrum.com. [70.114.247.242]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5d55aaee1fasm1411186eaf.42.2024.07.22.12.04.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jul 2024 12:04:54 -0700 (PDT) From: Denis Kenzior To: ell@lists.linux.dev Cc: Denis Kenzior Subject: [PATCH 12/12] genl: Use l_netlink_message as implementation for l_genl_msg Date: Mon, 22 Jul 2024 14:04:29 -0500 Message-ID: <20240722190443.43196-12-denkenz@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240722190443.43196-1-denkenz@gmail.com> References: <20240722190443.43196-1-denkenz@gmail.com> Precedence: bulk X-Mailing-List: ell@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This switches the underlying l_genl_msg implementation to l_netlink_message. --- ell/genl.c | 146 ++++++++++------------------------------------------- 1 file changed, 28 insertions(+), 118 deletions(-) diff --git a/ell/genl.c b/ell/genl.c index d2c6ea15dfa9..5a8dd371a970 100644 --- a/ell/genl.c +++ b/ell/genl.c @@ -19,20 +19,15 @@ #include "queue.h" #include "io.h" #include "private.h" +#include "netlink.h" #include "netlink-private.h" #include "notifylist.h" #include "genl.h" -#define MAX_NESTING_LEVEL 4 #define GENL_DEBUG(fmt, args...) \ l_util_debug(genl->debug_callback, genl->debug_data, "%s:%i " fmt, \ __func__, __LINE__, ## args) -struct nest_info { - uint16_t type; - uint16_t offset; -}; - struct unicast_watch { struct l_notifylist_entry super; char name[GENL_NAMSIZ]; @@ -89,11 +84,7 @@ struct l_genl_msg { char *error_msg; uint8_t cmd; uint8_t version; - void *data; - uint32_t size; - uint32_t len; - struct nest_info nests[MAX_NESTING_LEVEL]; - uint8_t nesting_level; + struct l_netlink_message *nlm; }; struct genl_request { @@ -714,44 +705,6 @@ static bool match_request_hid(const void *a, const void *b) return request->handle_id == id; } -static struct l_genl_msg *msg_alloc(uint8_t cmd, uint8_t version, uint32_t size) -{ - struct l_genl_msg *msg; - - msg = l_new(struct l_genl_msg, 1); - - msg->cmd = cmd; - msg->version = version; - - msg->len = NLMSG_HDRLEN + GENL_HDRLEN; - msg->size = msg->len + NLMSG_ALIGN(size); - - msg->data = l_realloc(NULL, msg->size); - memset(msg->data, 0, msg->size); - msg->nesting_level = 0; - - return l_genl_msg_ref(msg); -} - -static bool msg_grow(struct l_genl_msg *msg, uint32_t needed) -{ - uint32_t grow_by; - - if (msg->size >= msg->len + needed) - return true; - - grow_by = msg->len + needed - msg->size; - - if (grow_by < 32) - grow_by = 128; - - msg->data = l_realloc(msg->data, msg->size + grow_by); - memset(msg->data + msg->size, 0, grow_by); - msg->size += grow_by; - - return true; -} - static struct l_genl_msg *msg_create(const struct nlmsghdr *nlmsg) { struct l_genl_msg *msg; @@ -771,13 +724,10 @@ static struct l_genl_msg *msg_create(const struct nlmsghdr *nlmsg) goto done; } - msg->data = l_memdup(nlmsg, nlmsg->nlmsg_len); + msg->nlm = netlink_message_from_nlmsg(nlmsg); - msg->len = nlmsg->nlmsg_len; - msg->size = nlmsg->nlmsg_len; - - if (msg->len >= GENL_HDRLEN) { - struct genlmsghdr *genlmsg = msg->data + NLMSG_HDRLEN; + if (nlmsg->nlmsg_len >= NLMSG_LENGTH(GENL_HDRLEN)) { + struct genlmsghdr *genlmsg = msg->nlm->data + NLMSG_HDRLEN; msg->cmd = genlmsg->cmd; msg->version = genlmsg->version; @@ -794,23 +744,23 @@ static const void *msg_as_bytes(struct l_genl_msg *msg, uint16_t type, struct nlmsghdr *nlmsg; struct genlmsghdr *genlmsg; - nlmsg = msg->data; + nlmsg = msg->nlm->data; - nlmsg->nlmsg_len = msg->len; nlmsg->nlmsg_type = type; nlmsg->nlmsg_flags = flags; nlmsg->nlmsg_seq = seq; nlmsg->nlmsg_pid = pid; - genlmsg = msg->data + NLMSG_HDRLEN; + genlmsg = msg->nlm->data + NLMSG_HDRLEN; genlmsg->cmd = msg->cmd; genlmsg->version = msg->version; + genlmsg->reserved = 0; if (out_size) - *out_size = msg->len; + *out_size = nlmsg->nlmsg_len; - return msg->data; + return msg->nlm->data; } static void write_watch_destroy(void *user_data) @@ -842,7 +792,7 @@ static bool can_write_data(struct l_io *io, void *user_data) return false; } - l_util_hexdump(false, request->msg->data, bytes_written, + l_util_hexdump(false, data, bytes_written, genl->debug_callback, genl->debug_data); l_queue_push_tail(genl->pending_list, request); @@ -1472,7 +1422,14 @@ LIB_EXPORT struct l_genl_msg *l_genl_msg_new(uint8_t cmd) LIB_EXPORT struct l_genl_msg *l_genl_msg_new_sized(uint8_t cmd, uint32_t size) { - return msg_alloc(cmd, 0x00, size); + struct l_genl_msg *msg = l_new(struct l_genl_msg, 1); + + msg->cmd = cmd; + msg->nlm = l_netlink_message_new_sized(0, 0, size + GENL_HDRLEN); + netlink_message_reserve_header(msg->nlm, + sizeof(struct genlmsghdr), NULL); + + return l_genl_msg_ref(msg); } LIB_EXPORT struct l_genl_msg *l_genl_msg_new_from_data(const void *data, @@ -1516,7 +1473,7 @@ LIB_EXPORT void l_genl_msg_unref(struct l_genl_msg *msg) return; l_free(msg->error_msg); - l_free(msg->data); + l_netlink_message_unref(msg->nlm); l_free(msg); } @@ -1555,23 +1512,12 @@ LIB_EXPORT const char *l_genl_msg_get_extended_error(struct l_genl_msg *msg) LIB_EXPORT bool l_genl_msg_append_attr(struct l_genl_msg *msg, uint16_t type, uint16_t len, const void *data) { - struct nlattr *nla; - if (unlikely(!msg)) return false; - if (!msg_grow(msg, NLA_HDRLEN + NLA_ALIGN(len))) + if (l_netlink_message_append(msg->nlm, type, data, len) < 0) return false; - nla = msg->data + msg->len; - nla->nla_len = NLA_HDRLEN + len; - nla->nla_type = type; - - if (len) - memcpy(msg->data + msg->len + NLA_HDRLEN, data, len); - - msg->len += NLA_HDRLEN + NLA_ALIGN(len); - return true; } @@ -1579,32 +1525,12 @@ LIB_EXPORT bool l_genl_msg_append_attrv(struct l_genl_msg *msg, uint16_t type, const struct iovec *iov, size_t iov_len) { - struct nlattr *nla; - size_t len = 0; - unsigned int i; - if (unlikely(!msg)) return false; - for (i = 0; i < iov_len; i++) - len += iov[i].iov_len; - - if (!msg_grow(msg, NLA_HDRLEN + NLA_ALIGN(len))) + if (l_netlink_message_appendv(msg->nlm, type, iov, iov_len) < 0) return false; - nla = msg->data + msg->len; - nla->nla_len = NLA_HDRLEN + len; - nla->nla_type = type; - - msg->len += NLA_HDRLEN; - - for (i = 0; i < iov_len; i++, iov++) { - memcpy(msg->data + msg->len, iov->iov_base, iov->iov_len); - msg->len += iov->iov_len; - } - - msg->len += NLA_ALIGN(len) - len; - return true; } @@ -1613,37 +1539,20 @@ LIB_EXPORT bool l_genl_msg_enter_nested(struct l_genl_msg *msg, uint16_t type) if (unlikely(!msg)) return false; - if (unlikely(msg->nesting_level == MAX_NESTING_LEVEL)) - return false; - - if (!msg_grow(msg, NLA_HDRLEN)) + if (l_netlink_message_enter_nested(msg->nlm, type) < 0) return false; - msg->nests[msg->nesting_level].type = type | NLA_F_NESTED; - msg->nests[msg->nesting_level].offset = msg->len; - msg->nesting_level += 1; - - msg->len += NLA_HDRLEN; - return true; } LIB_EXPORT bool l_genl_msg_leave_nested(struct l_genl_msg *msg) { - struct nlattr *nla; - if (unlikely(!msg)) return false; - if (unlikely(msg->nesting_level == 0)) + if (l_netlink_message_leave_nested(msg->nlm) < 0) return false; - nla = msg->data + msg->nests[msg->nesting_level - 1].offset; - nla->nla_type = msg->nests[msg->nesting_level - 1].type; - nla->nla_len = msg->len - msg->nests[msg->nesting_level - 1].offset; - - msg->nesting_level -= 1; - return true; } @@ -1656,11 +1565,12 @@ LIB_EXPORT bool l_genl_attr_init(struct l_genl_attr *attr, if (unlikely(!attr) || unlikely(!msg)) return false; - if (!msg->data || msg->len < NLMSG_HDRLEN + GENL_HDRLEN) + if (!msg->nlm || + msg->nlm->hdr->nlmsg_len < NLMSG_HDRLEN + GENL_HDRLEN) return false; - nla = msg->data + NLMSG_HDRLEN + GENL_HDRLEN; - len = msg->len - NLMSG_HDRLEN - GENL_HDRLEN; + nla = msg->nlm->data + NLMSG_HDRLEN + GENL_HDRLEN; + len = msg->nlm->hdr->nlmsg_len - NLMSG_HDRLEN - GENL_HDRLEN; if (!NLA_OK(nla, len)) return false;