From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 2E53B4411D; Fri, 31 May 2024 17:49:45 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 0019742DFE; Fri, 31 May 2024 17:48:04 +0200 (CEST) Received: from egress-ip11b.ess.de.barracuda.com (egress-ip11b.ess.de.barracuda.com [18.185.115.215]) by mails.dpdk.org (Postfix) with ESMTP id 028E442D9A for ; Fri, 31 May 2024 17:47:50 +0200 (CEST) Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-db5eur01lp2051.outbound.protection.outlook.com [104.47.2.51]) by mx-outbound20-91.eu-central-1b.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 31 May 2024 15:47:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B6Y8VlG0mT3Mf0UmyWvbW7GkqgUOjiLE5PeV9GWCqMBx+U/Pwwb70Rb8pN45A2Tx8i6W0l82RjLun6D7DHUakhQG7tQZ0R37ZLDDTTAQr+s8ZPDyKvIioNPfSaY0pLkG/pwJp8FTSiJT+swc3snFOW4kq5WIFSwzDyTvbdscEC4tSBHJdSpoltrYhcNrUQR8gLWdMQyevCuc/hRToN92H88MyqvoVBX4zZwfz+bJ4sWJOkWZxitdGLJ1JBgKUFTOaH2WEsBG6xCpoexmEWMwlkGFW+4Hswj3TLNyO+DaSoV4tgC6Ku+jLIad2YYuthVXgt2zqhy7EaxkB7fagwiGHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PeTmuYEoVBNyOo/fC3XLTNzftQg3uN2pGulAGHbCVTQ=; b=jj2TK1ao1kQ/9Zzf8mHo8dkXOXZk/hGeYaWacBnd0eKRiElcsOfJEo2R/E/1/2J3bAn+gxHzO/yBpq9fW0nQoBjtxuYo6o7kldyzu6xhPVHOEg7fPx+CIMTUVmKU/ZkZWeRdkD6hpbB+G+sG7t1eAwyAMm4GlhemDC84jk3Uf47oGi5juV7C40tNL1Xs8zTAGUdU04WTy0RzcFc7hFOOK9qQ41mbazXuSunzJ4qHlPTfT2EEWguD4JVBPdg9/UuJ5moPdWNTkN5aWsVAsLSXqtCHOH0LsRehORQBvjSuUFkxRrwAuGrLY98ojax9kRm1aKLNyFiK3gtRhQVGuBj+pg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=fail (sender ip is 178.72.21.4) smtp.rcpttodomain=dpdk.org smtp.mailfrom=napatech.com; dmarc=fail (p=reject sp=reject pct=100) action=oreject header.from=napatech.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=napatech.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PeTmuYEoVBNyOo/fC3XLTNzftQg3uN2pGulAGHbCVTQ=; b=VuhSX4JAtt9QBYxDNFMZTGg4CKm+PvUcX64mVHppmYO47K0lJ5wlPXC7URuc3wnfF1ZtV+qYIRhtk9qCGCxfO5agcXhBUX9cep8vhF0Ga1d9PktWHMho2nI+klYZQ1o88KBL9GAl69y2Z0kocqG4jLwA+2MDOB9cTWpZV9+OFk0= Received: from AS4P251CA0027.EURP251.PROD.OUTLOOK.COM (2603:10a6:20b:5d3::13) by AS8P190MB1349.EURP190.PROD.OUTLOOK.COM (2603:10a6:20b:2ee::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Fri, 31 May 2024 15:47:43 +0000 Received: from AMS1EPF00000047.eurprd04.prod.outlook.com (2603:10a6:20b:5d3:cafe::d9) by AS4P251CA0027.outlook.office365.com (2603:10a6:20b:5d3::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.22 via Frontend Transport; Fri, 31 May 2024 15:47:43 +0000 X-MS-Exchange-Authentication-Results: spf=fail (sender IP is 178.72.21.4) smtp.mailfrom=napatech.com; dkim=none (message not signed) header.d=none;dmarc=fail action=oreject header.from=napatech.com; Received-SPF: Fail (protection.outlook.com: domain of napatech.com does not designate 178.72.21.4 as permitted sender) receiver=protection.outlook.com; client-ip=178.72.21.4; helo=localhost.localdomain; Received: from localhost.localdomain (178.72.21.4) by AMS1EPF00000047.mail.protection.outlook.com (10.167.16.135) with Microsoft SMTP Server id 15.20.7633.15 via Frontend Transport; Fri, 31 May 2024 15:47:43 +0000 From: Serhii Iliushyk To: dev@dpdk.org Cc: mko-plv@napatech.com, ckm@napatech.com, andrew.rybchenko@oktetlabs.ru, ferruh.yigit@amd.com Subject: [PATCH v2 10/17] net/ntnic: add Logs and utilities implementation Date: Fri, 31 May 2024 17:47:14 +0200 Message-ID: <20240531154731.1856874-10-sil-plv@napatech.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240531154731.1856874-1-sil-plv@napatech.com> References: <20240530144929.4127931-1-sil-plv@napatech.com> <20240531154731.1856874-1-sil-plv@napatech.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AMS1EPF00000047:EE_|AS8P190MB1349:EE_ Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: 309cbbb7-8841-441b-3dff-08dc818902a6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|82310400017|1800799015|36860700004|376005; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?KUhFX7Za6Zog92IqEIgM8sq5QLqHU1eweLt5MpxV0EWBGpkJkxCVam1dTETx?= =?us-ascii?Q?I6hK5iDP8PmgnMixA53oKFlcoKYJBiD6Hp44nFr7mMw68/wiYdrtn1OJo5uP?= =?us-ascii?Q?0LaC0ZYxbDQr1r/ctGetjaBx2tDvREX+kP4Ou5U37zmP5jML7H8fP9CX/Y5+?= =?us-ascii?Q?OBJH58cCxF28DT38+MHriqeEWGwtk9/cOd12UqSjAJObBtB3Rmk5r1pk3ARq?= =?us-ascii?Q?MqF/D2VghuikPGjO8FPq7EKJaFy7XL9e9TcKyh+JxAN2/nUlWZeUmEVCNKcH?= =?us-ascii?Q?n2cHmPCa+wIdKMNAe7UL9+o6Gacp1dYjb8jnPTG9YaK/HSYK/sAGL6ItaRw1?= =?us-ascii?Q?wzMi5/FkEhEIKQsfcUQccnG329HSeFMrYmO+HU4Rlc7JMX+TZDG8lLyKTm8o?= =?us-ascii?Q?hevX8A8BJ49ZvhYPaNJBXz6s7mYq8wW6h7BzPGGLW3rm1kBQdFx+yMRPfeaH?= =?us-ascii?Q?MliZUu8NHyl8DSZqiy21TAKPeNMEegfQBqbGAeCYGKLNyppVaJlF+UOLQwf9?= =?us-ascii?Q?GpJ9vhdwpwmZ/EyXdLGzG7iXAsH1+SKcawEC16pe6O2XCdvBk7Eu/XdzvYzS?= =?us-ascii?Q?rIZ2unoa7lvNO63R7EASTaqIUTB0BSc5Y6Nhn6VhArCy15fmL39R7eonjvDs?= =?us-ascii?Q?QYlsVncjnyBqlTFCBIjsP86KW5hAQGSKt0/hhvk+YGAI+bVWlGIXS5E6CqPw?= =?us-ascii?Q?lqOoOjxiIqCASEunY9qa34euK6NAa/3EEYpDI9cM6dTqL8hbeuj6IdsS94vR?= =?us-ascii?Q?4XhrEyHNXDOJDwSCi9gqZrUllvWO8KUNxH5kyeZ3AJBv18RC1mQktbrRZcik?= =?us-ascii?Q?+XkMBa1N5FrYe07ig1Qx8a+kVmF5w0CRDMHz1psUriWAWrf9/N9MeguUTeh1?= =?us-ascii?Q?SRGO/jtw71yXadaBXTDwTLT6jCNeJ0tUW4E5a+KSCe7/2BFSG8i3oWj2rk6e?= =?us-ascii?Q?VchK32mYDXabOvmKAkZDZETe0PsQ4YMTEsYw6Yrn0yL6BesACzzSkDiQZDJJ?= =?us-ascii?Q?c2ezkl4nBJpp7mtYEJ1aRWfYc4zFd1TFw+dmckDlci10pC632xaKNIeI/onn?= =?us-ascii?Q?E1iE9pKtcUU10W67ACVLNScTau/1B0FeDCMD8dGY3sVHkM5usMCiT3NU2Isc?= =?us-ascii?Q?Kz5MBvhMTB+vIN5v9KNL0U3eLTswJXzJ/70mYnq2MW6B++wPiqlbpn+ym7wL?= =?us-ascii?Q?9OvuTTbtHEIzAGXcucLoBy8zjCybnUIDR/x9TsAy6NRkODO1sDYh+IIDx4Or?= =?us-ascii?Q?RQp1EqF/llwPKQvK71UqWwI03EaclU+Ik7UficEtDTfgc/EUiU1qHdR/K/h4?= =?us-ascii?Q?RL6Wp1M2tO6+y/U1Xi2DbzDAPyfAbRMB7/seKvzizzYP7Tc3Vdshv4XdEA/K?= =?us-ascii?Q?Tc9mPH8=3D?= X-Forefront-Antispam-Report: CIP:178.72.21.4; CTRY:DK; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:localhost.localdomain; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230031)(82310400017)(1800799015)(36860700004)(376005); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: ladYmeFAzLKDSfBwpFUHLzBl2wW0T4NDDvK4f+RxAy4FLhQ6ht7bBMmvn9sER44dsCf6wi7V6oJN1YV3cVUMNGi9ulJnLq3x/Ii38Z2LIcQZEokrjLw2Xwa/QKwi6j+hoEkb94WmT5BKv9gi5P6vNc6DOdadJXduHORtvxvvGMay2JjN1SgjOD/NTzoZHG+HsmPqiq0rRDT5vcZR4hzvUlRXwj1XQf4kh+4INLZnoB+8gVrDWWmMRYIdE2zTlRQD0y3snHXgg1HOC9v2ImPNAxOY4LWirlWd9AVsxqLua5CsK32LZxumyu/nBZwNAv+/nM7UUFRBvGf2mR1sFrpbsqvejZbH/zRV0i8uD5XmBKVFnosPR1us7WNdh5lNIkigWUeDFcpUL3nJGrKA6aoZ+6FGYxvUde3iWQvqQc4cNSnP9fLdMHCYXcfkN1vD/4GlCOqzsskzdZTTVqP9YB3GgJk2d39IykbU04mzbRKvlPXjWqDV0sx4PMlAFRU/unMgE+PzQfeH0D2UlR83wwvI0oLfdzQPBiNfbQdWaduWZE9isiBhcJM1Y89hMoLEP11jkomHwWO69Lbb3Bd5hGVUFCPoVLcnU7N0Xy3Igzr2/AE5MskfjmxgHylx8r0LeKaLzr13Gm/Zp11xK+HkQGBTRQ== X-OriginatorOrg: napatech.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 May 2024 15:47:43.3625 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 309cbbb7-8841-441b-3dff-08dc818902a6 X-MS-Exchange-CrossTenant-Id: c4540d0b-728a-4233-9da5-9ea30c7ec3ed X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=c4540d0b-728a-4233-9da5-9ea30c7ec3ed; Ip=[178.72.21.4]; Helo=[localhost.localdomain] X-MS-Exchange-CrossTenant-AuthSource: AMS1EPF00000047.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8P190MB1349 X-BESS-ID: 1717170465-305211-12713-33311-2 X-BESS-VER: 2019.1_20240530.1612 X-BESS-Apparent-Source-IP: 104.47.2.51 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVoZGRkbGQGYGUNQ00Swl1czIMM 3I0CzJxNLQKDHV2DTZIDHJxDg50cQgWak2FgD5+u+mQgAAAA== X-BESS-Outbound-Spam-Score: 0.50 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.256627 [from cloudscan9-65.eu-central-1a.ess.aws.cudaops.com] Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.50 BSF_RULE7568M META: Custom Rule 7568M 0.00 BSF_BESS_OUTBOUND META: BESS Outbound X-BESS-Outbound-Spam-Status: SCORE=0.50 using account:ESS113687 scores of KILL_LEVEL=7.0 tests=BSF_RULE7568M, BSF_BESS_OUTBOUND X-BESS-BRTS-Status: 1 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Add ntnic logging API and utilities. Signed-off-by: Serhii Iliushyk --- v2: * Fixed unused header * Fixed error on -Wformat-nonliteral --- drivers/net/ntnic/ntlog/include/ntlog.h | 162 +++++++++++++++++++++ drivers/net/ntnic/ntlog/ntlog.c | 111 ++++++++++++++ drivers/net/ntnic/ntutil/include/nt_util.h | 51 +++++++ drivers/net/ntnic/ntutil/nt_util.c | 97 ++++++++++++ 4 files changed, 421 insertions(+) create mode 100644 drivers/net/ntnic/ntlog/include/ntlog.h create mode 100644 drivers/net/ntnic/ntlog/ntlog.c create mode 100644 drivers/net/ntnic/ntutil/include/nt_util.h create mode 100644 drivers/net/ntnic/ntutil/nt_util.c diff --git a/drivers/net/ntnic/ntlog/include/ntlog.h b/drivers/net/ntnic/ntlog/include/ntlog.h new file mode 100644 index 0000000000..75fc08d4e1 --- /dev/null +++ b/drivers/net/ntnic/ntlog/include/ntlog.h @@ -0,0 +1,162 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Napatech A/S + */ + +#ifndef NTOSS_SYSTEM_NTLOG_H +#define NTOSS_SYSTEM_NTLOG_H + +#include +#include + +#ifndef NT_LOG_MODULE_PREFIX + +/* DPDK modules */ +#define NT_LOG_MODULE_EAL 0 +#define NT_LOG_MODULE_MALLOC 1 +#define NT_LOG_MODULE_RING 2 +#define NT_LOG_MODULE_MEMPOOL 3 +#define NT_LOG_MODULE_TIMER 4 +#define NT_LOG_MODULE_PMD 5 +#define NT_LOG_MODULE_HASH 6 +#define NT_LOG_MODULE_LPM 7 +#define NT_LOG_MODULE_KNI 8 +#define NT_LOG_MODULE_ACL 9 +#define NT_LOG_MODULE_POWER 10 +#define NT_LOG_MODULE_METER 11 +#define NT_LOG_MODULE_SCHED 12 +#define NT_LOG_MODULE_PORT 13 +#define NT_LOG_MODULE_TABLE 14 +#define NT_LOG_MODULE_PIPELINE 15 +#define NT_LOG_MODULE_MBUF 16 +#define NT_LOG_MODULE_CRYPTODEV 17 +#define NT_LOG_MODULE_EFD 18 +#define NT_LOG_MODULE_EVENTDEV 19 +#define NT_LOG_MODULE_GSO 20 +#define NT_LOG_MODULE_USER1 24 +#define NT_LOG_MODULE_USER2 25 +#define NT_LOG_MODULE_USER3 26 +#define NT_LOG_MODULE_USER4 27 +#define NT_LOG_MODULE_USER5 28 +#define NT_LOG_MODULE_USER6 29 +#define NT_LOG_MODULE_USER7 30 +#define NT_LOG_MODULE_USER8 31 + +/* NT modules */ +#define NT_LOG_MODULE_GENERAL 10000 /* Should always be a first (smallest) */ +#define NT_LOG_MODULE_NTHW 10001 +#define NT_LOG_MODULE_FILTER 10002 +#define NT_LOG_MODULE_DRV 10003 +#define NT_LOG_MODULE_VDPA 10004 +#define NT_LOG_MODULE_FPGA 10005 +#define NT_LOG_MODULE_NTCONNECT 10006 +#define NT_LOG_MODULE_ETHDEV 10007 +#define NT_LOG_MODULE_SENSOR 10008 +#define NT_LOG_MODULE_END 10009 /* Mark for the range end of NT_LOG */ + +#define NT_LOG_MODULE_COUNT (NT_LOG_MODULE_END - NT_LOG_MODULE_GENERAL) +#define NT_LOG_MODULE_INDEX(module) ((module) - (NT_LOG_MODULE_GENERAL)) +#define NT_LOG_MODULE_PREFIX(type) NT_LOG_MODULE_##type + +#endif + +#ifndef NT_LOG_ENABLE +#define NT_LOG_ENABLE 1 +#endif + +#if defined NT_LOG_ENABLE && NT_LOG_ENABLE > 0 +#ifndef NT_LOG_ENABLE_ERR +#define NT_LOG_ENABLE_ERR 1 +#endif +#ifndef NT_LOG_ENABLE_WRN +#define NT_LOG_ENABLE_WRN 1 +#endif +#ifndef NT_LOG_ENABLE_INF +#define NT_LOG_ENABLE_INF 1 +#endif +#ifndef NT_LOG_ENABLE_DBG +#define NT_LOG_ENABLE_DBG 1 +#endif +#ifndef NT_LOG_ENABLE_DB1 +#define NT_LOG_ENABLE_DB1 0 +#endif +#ifndef NT_LOG_ENABLE_DB2 +#define NT_LOG_ENABLE_DB2 0 +#endif +#endif + +#if defined NT_LOG_ENABLE_ERR && NT_LOG_ENABLE_ERR > 0 +#define NT_LOG_NT_LOG_ERR(...) nt_log(__VA_ARGS__) +#else +#define NT_LOG_NT_LOG_ERR(...) +#endif + +#if defined NT_LOG_ENABLE_WRN && NT_LOG_ENABLE_WRN > 0 +#define NT_LOG_NT_LOG_WRN(...) nt_log(__VA_ARGS__) +#else +#define NT_LOG_NT_LOG_WRN(...) +#endif + +#if defined NT_LOG_ENABLE_INF && NT_LOG_ENABLE_INF > 0 +#define NT_LOG_NT_LOG_INF(...) nt_log(__VA_ARGS__) +#else +#define NT_LOG_NT_LOG_INF(...) +#endif + +#if defined NT_LOG_ENABLE_DBG && NT_LOG_ENABLE_DBG > 0 +#define NT_LOG_NT_LOG_DBG(...) nt_log(__VA_ARGS__) +#else +#define NT_LOG_NT_LOG_DBG(...) +#endif + +#if defined NT_LOG_ENABLE_DB1 && NT_LOG_ENABLE_DB1 > 0 +#define NT_LOG_NT_LOG_DB1(...) nt_log(__VA_ARGS__) +#else +#define NT_LOG_NT_LOG_DB1(...) +#endif + +#if defined NT_LOG_ENABLE_DB2 && NT_LOG_ENABLE_DB2 > 0 +#define NT_LOG_NT_LOG_DB2(...) nt_log(__VA_ARGS__) +#else +#define NT_LOG_NT_LOG_DB2(...) +#endif + +#define NT_LOG(level, module, ...) \ + NT_LOG_NT_LOG_##level(NT_LOG_##level, NT_LOG_MODULE_PREFIX(module), \ + #module ": " #level ": " __VA_ARGS__) + +enum nt_log_level { + NT_LOG_ERR = 0x001, + NT_LOG_WRN = 0x002, + NT_LOG_INF = 0x004, + NT_LOG_DBG = 0x008, + NT_LOG_DB1 = 0x010, + NT_LOG_DB2 = 0x020, +}; + +struct nt_log_impl { + int (*init)(void); + int (*log)(enum nt_log_level level, uint32_t module, const char *format, va_list args); + int (*is_debug)(uint32_t module); +}; + +int nt_log_init(struct nt_log_impl *impl); + +int nt_log(enum nt_log_level level, uint32_t module, const char *format, ...); + +/* Returns 1 if RTE_DEBUG, 0 if lower log level, -1 if incorrect module */ +int nt_log_is_debug(uint32_t module); + +/* + * nt log helper functions + * to create a string for NT_LOG usage to output a one-liner log + * to use when one single function call to NT_LOG is not optimal - that is + * you do not know the number of parameters at programming time or it is variable + */ +char *ntlog_helper_str_alloc(const char *sinit); + +void ntlog_helper_str_add(char *s, const char *format, ...); + +void ntlog_helper_str_free(char *s); + +#endif /* NTOSS_SYSTEM_NTLOG_H */ diff --git a/drivers/net/ntnic/ntlog/ntlog.c b/drivers/net/ntnic/ntlog/ntlog.c new file mode 100644 index 0000000000..ee2d0ddba9 --- /dev/null +++ b/drivers/net/ntnic/ntlog/ntlog.c @@ -0,0 +1,111 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Napatech A/S + */ + +#include "ntlog.h" + +#include +#include +#include +#include +#include +#include + +#include + +#define NTLOG_HELPER_STR_SIZE_MAX (1024) + +static struct nt_log_impl *user_impl; + +int nt_log_init(struct nt_log_impl *impl) +{ + user_impl = impl; + return user_impl->init(); +} + +static char *last_trailing_eol(char *s) +{ + int i = strlen(s) - 1; + + /* Skip spaces */ + while (i > 0 && s[i] == ' ') + --i; + + if (s[i] != '\n') + return NULL; + + /* + * Find the last trailing EOL "hello_world\n\n\n" + * ^ + */ + while (i > 1 && s[i] == '\n' && s[i - 1] == '\n') + --i; + + return &s[i]; +} + +/* Always terminates the NT_LOG statement with a !!!single!!! EOL. */ +int nt_log(enum nt_log_level level, uint32_t module, const char *format, ...) +{ + int rv = -1; + va_list args; + + if (user_impl == NULL) + return rv; + + char *actual_format = ntlog_helper_str_alloc(format); + char *eol = last_trailing_eol(actual_format); + + if (!eol) /* If log line is not terminated with '\n' we add it. */ + strncat(actual_format, "\n", NTLOG_HELPER_STR_SIZE_MAX - strlen(actual_format)); + + else /* If multiple trailing EOLs, then keep just one of them. */ + *(eol + 1) = '\0'; + + va_start(args, format); + rv = user_impl->log(level, module, actual_format, args); + va_end(args); + + ntlog_helper_str_free(actual_format); + return rv; +} + +int nt_log_is_debug(uint32_t module) +{ + return user_impl->is_debug(module); +} + +char *ntlog_helper_str_alloc(const char *sinit) +{ + char *s = malloc(NTLOG_HELPER_STR_SIZE_MAX); + + if (!s) + return NULL; + + if (sinit) + snprintf(s, NTLOG_HELPER_STR_SIZE_MAX, "%s", sinit); + + else + s[0] = '\0'; + + return s; +} + +__rte_format_printf(2, 0) +void ntlog_helper_str_add(char *s, const char *format, ...) +{ + if (!s) + return; + + va_list args; + va_start(args, format); + int len = strlen(s); + vsnprintf(&s[len], (NTLOG_HELPER_STR_SIZE_MAX - 1 - len), format, args); + va_end(args); +} + +void ntlog_helper_str_free(char *s) +{ + free(s); +} diff --git a/drivers/net/ntnic/ntutil/include/nt_util.h b/drivers/net/ntnic/ntutil/include/nt_util.h new file mode 100644 index 0000000000..0f27698a84 --- /dev/null +++ b/drivers/net/ntnic/ntutil/include/nt_util.h @@ -0,0 +1,51 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Napatech A/S + */ + +#ifndef NTOSS_SYSTEM_NT_UTIL_H +#define NTOSS_SYSTEM_NT_UTIL_H + +#include + +/* + * Windows size in seconds for measuring FLM load + * and Port load. + * The windows size must max be 3 min in order to + * prevent overflow. + */ +#define FLM_LOAD_WINDOWS_SIZE 2ULL +#define PORT_LOAD_WINDOWS_SIZE 2ULL + +/* Total max VDPA ports */ +#define MAX_VDPA_PORTS 128UL + +#define PCIIDENT_TO_DOMAIN(pci_ident) ((uint16_t)(((unsigned int)(pci_ident) >> 16) & 0xFFFFU)) +#define PCIIDENT_TO_BUSNR(pci_ident) ((uint8_t)(((unsigned int)(pci_ident) >> 8) & 0xFFU)) +#define PCIIDENT_TO_DEVNR(pci_ident) ((uint8_t)(((unsigned int)(pci_ident) >> 3) & 0x1FU)) +#define PCIIDENT_TO_FUNCNR(pci_ident) ((uint8_t)(((unsigned int)(pci_ident) >> 0) & 0x7U)) +#define PCIIDENT_PRINT_STR "%04x:%02x:%02x.%x" +#define BDF_TO_PCIIDENT(dom, bus, dev, fnc) (((dom) << 16) | ((bus) << 8) | ((dev) << 3) | (fnc)) + +uint64_t nt_os_get_time_monotonic_counter(void); +void nt_os_wait_usec(int val); + +uint64_t nt_util_align_size(uint64_t size); + +struct nt_dma_s { + uint64_t iova; + uint64_t addr; + uint64_t size; +}; + +struct nt_dma_s *nt_dma_alloc(uint64_t size, uint64_t align, int numa); +void nt_dma_free(struct nt_dma_s *vfio_addr); + +struct nt_util_vfio_impl { + int (*vfio_dma_map)(int vf_num, void *virt_addr, uint64_t *iova_addr, uint64_t size); + int (*vfio_dma_unmap)(int vf_num, void *virt_addr, uint64_t iova_addr, uint64_t size); +}; + +void nt_util_vfio_init(struct nt_util_vfio_impl *impl); + +#endif /* NTOSS_SYSTEM_NT_UTIL_H */ diff --git a/drivers/net/ntnic/ntutil/nt_util.c b/drivers/net/ntnic/ntutil/nt_util.c new file mode 100644 index 0000000000..1d91b12224 --- /dev/null +++ b/drivers/net/ntnic/ntutil/nt_util.c @@ -0,0 +1,97 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Napatech A/S + */ + +#include +#include +#include +#include +#include + +#include +#include "rte_malloc.h" + +#include "ntlog.h" +#include "nt_util.h" + +static struct nt_util_vfio_impl vfio_cb; + +/* uses usleep which schedules out the calling thread */ +void nt_os_wait_usec(int val) +{ + rte_delay_us_sleep(val); +} + +uint64_t nt_os_get_time_monotonic_counter(void) +{ + return rte_get_timer_cycles(); +} + +/* Allocation size matching minimum alignment of specified size */ +uint64_t nt_util_align_size(uint64_t size) +{ + return 1 << rte_log2_u64(size); +} + +void nt_util_vfio_init(struct nt_util_vfio_impl *impl) +{ + vfio_cb = *impl; +} + +struct nt_dma_s *nt_dma_alloc(uint64_t size, uint64_t align, int numa) +{ + int res; + struct nt_dma_s *vfio_addr; + + vfio_addr = rte_malloc(NULL, sizeof(struct nt_dma_s), 0); + + if (!vfio_addr) { + NT_LOG(ERR, GENERAL, "VFIO rte_malloc failed\n"); + return NULL; + } + + void *addr = rte_malloc_socket(NULL, size, align, numa); + + if (!addr) { + rte_free(vfio_addr); + NT_LOG(ERR, GENERAL, "VFIO rte_malloc_socket failed\n"); + return NULL; + } + + res = vfio_cb.vfio_dma_map(0, addr, &vfio_addr->iova, nt_util_align_size(size)); + + if (res != 0) { + rte_free(addr); + rte_free(vfio_addr); + NT_LOG(ERR, GENERAL, "VFIO nt_dma_map failed\n"); + return NULL; + } + + vfio_addr->addr = (uint64_t)addr; + vfio_addr->size = nt_util_align_size(size); + + NT_LOG(DBG, GENERAL, + "VFIO DMA alloc addr=%" PRIX64 ", iova=%" PRIX64 ", size=%u, align=0x%X\n", + vfio_addr->addr, vfio_addr->iova, vfio_addr->size, align); + + return vfio_addr; +} + +void nt_dma_free(struct nt_dma_s *vfio_addr) +{ + NT_LOG(DBG, GENERAL, "VFIO DMA free addr=%" PRIX64 ", iova=%" PRIX64 ", size=%u\n", + vfio_addr->addr, vfio_addr->iova, vfio_addr->size); + + int res = vfio_cb.vfio_dma_unmap(0, (void *)vfio_addr->addr, vfio_addr->iova, + vfio_addr->size); + + if (res != 0) { + NT_LOG(WRN, GENERAL, + "VFIO DMA free FAILED addr=%" PRIX64 ", iova=%" PRIX64 ", size=%u\n", + vfio_addr->addr, vfio_addr->iova, vfio_addr->size); + } + + rte_free((void *)(vfio_addr->addr)); + rte_free(vfio_addr); +} -- 2.44.0