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 7453544111; Thu, 30 May 2024 16:50:55 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 139E1427DC; Thu, 30 May 2024 16:50:07 +0200 (CEST) Received: from egress-ip42b.ess.de.barracuda.com (egress-ip42b.ess.de.barracuda.com [18.185.115.246]) by mails.dpdk.org (Postfix) with ESMTP id 464BB410F2 for ; Thu, 30 May 2024 16:50:01 +0200 (CEST) Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-db5eur01lp2050.outbound.protection.outlook.com [104.47.2.50]) by mx-outbound18-255.eu-central-1b.ess.aws.cudaops.com (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 30 May 2024 14:50:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qb7j/jMx/2PvoG6ilRk4yqhh2cixUCpEWWPc+Qu+oJ2LiIH+JROV/Jss6pCD175BkbUvBpl14Qj33efR9/DeMBeuhak8W4kF5F0Yir0nn6Z0f6qZMguijHVYqJkpsJCs98REgG+ymF1av1GzXhCrddmtx8QHS1gRUtIE+z0FzrEIvohZGLCXrE6/2MjngefBBp9aGVv3v1ae6NrXbg/dZ7K8+aoluOBC/VVz45lHXuDnoDSxqvF99yft0zKFHHzyI77NG4Dg24SkdNThJxQfJ6K+KnsdC2ebImL0MM4P3CKfoppY3gO8cj/vg4KMk8gRy5Hz8R7p0YXlThOyZUzzwg== 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=LxIsmAjxKtt7CKfq0THC593HDTUa7J5iyHiCTpGVkNA=; b=NR/JRY1+NK0tL+saj3RYOZWlj9tJ/PQtr/pW1Zhz0i1VUtqfRRgfBF15wrrfiQYXs08tjlIPcLHrC6v3eeSHCYmDJDwkPqirIXysoyi/UvbJOaEVDTapkqqC9DLV2lW3CSnyy8E8WRtsud58rOEHR4lrQIWuGB0jPKStc3GfCoK4lenHHw5M3GWbhmcKRpbRBXezBja9G05+Dh3bqvOVSboVKgrunsenylsFLvCjebwNIOPQ0wi3iqLdGxCnYGNv+8nQUZvcCWOvPAvBcEkMTIo21ZjBjkzJ0Z4RoZQ4yMY2ML+okqhitsthv7kQqjhHlgfLpMBbe9LiCnh3jUg2wQ== 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=LxIsmAjxKtt7CKfq0THC593HDTUa7J5iyHiCTpGVkNA=; b=RjCIRlBlf/efMek99mjB/cdqcwHVkpli8SkNmMVxEImH8xKm25n9Hr1n4aRV4cmqtPa1qOB8QaytJS+WYTlo3hDCaNNNPYiLy3O4jawpg4MYVQKt8YiWeDHVsa+p0fv4OYuhWds+4hcEoABU/yOneqS6fkNw57b5LaG4NTiT2Xc= Received: from DU6P191CA0037.EURP191.PROD.OUTLOOK.COM (2603:10a6:10:53f::16) by AM0P190MB0737.EURP190.PROD.OUTLOOK.COM (2603:10a6:208:195::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7611.30; Thu, 30 May 2024 14:49:58 +0000 Received: from DB1PEPF000509EB.eurprd03.prod.outlook.com (2603:10a6:10:53f:cafe::5f) by DU6P191CA0037.outlook.office365.com (2603:10a6:10:53f::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21 via Frontend Transport; Thu, 30 May 2024 14:49:58 +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 DB1PEPF000509EB.mail.protection.outlook.com (10.167.242.69) with Microsoft SMTP Server id 15.20.7633.15 via Frontend Transport; Thu, 30 May 2024 14:49:58 +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 v1 10/17] net/ntnic: add Logs and utilities implementation Date: Thu, 30 May 2024 16:49:07 +0200 Message-ID: <20240530144929.4127931-10-sil-plv@napatech.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240530144929.4127931-1-sil-plv@napatech.com> References: <20240530144929.4127931-1-sil-plv@napatech.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DB1PEPF000509EB:EE_|AM0P190MB0737:EE_ Content-Type: text/plain X-MS-Office365-Filtering-Correlation-Id: 35e64c98-1945-4a59-70a0-08dc80b7c6fe X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230031|376005|1800799015|36860700004|82310400017; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?RCJLbLjVKd/Dxuvval+5/CG8pTtuzMotag3hdEKaAiJRwcyMydbf2tsA7Pve?= =?us-ascii?Q?1ICdlSAZlB032n8Jv8HKkVGzLoGMrs8y4wmmtU+/ruMBvPTs0/JHGsGZpOT1?= =?us-ascii?Q?BCBivvJEkbnHvk96gWW3SP9833JIcGRNCDnOlOKrvXy+NOr5uLPvBn7LY28p?= =?us-ascii?Q?vRHWpXBQIe1/Q8JNPMzndahN65rzgCdY6c0lE6eFZMseeoClJapzeAQJb7o1?= =?us-ascii?Q?0mC1quzCYUKYUZ4gdgGd5TDcxs0RJ5R6NC50+5/wH8NvimwJSFTkjlM/QJiT?= =?us-ascii?Q?xiEoAxGDofM5ZCklgDTJLez4ZgRTEQiSua4pzuZJ5hwCvR0Ip4Ey6688we6C?= =?us-ascii?Q?dpBOCUfxHaTsM2gzP6YY980Yx3cITalNgFjt5o1hIdayVMX2YVrll3j9L2Ou?= =?us-ascii?Q?KZcW7EZQ+UUIKY//3D+PdgE7MS4OLWNkn1YtNJ37M6zSbITOb2OEfB/PESod?= =?us-ascii?Q?5kCm4qHGBfkJ4Aartxq685vlZGSgogOprSbOqfouCIAMMta+ayEbpo1Qq31v?= =?us-ascii?Q?yOFP6QgBaGY8BZyEbJpZYLWhS5rGmCLdCoaFidXRJkyqRyhKOdb1ELskOdB8?= =?us-ascii?Q?KTIiVF8Tkwr6wzjfri24IFwre0eJH0e460aNVKV63iyccsZWbTe//wLtjsSo?= =?us-ascii?Q?kV+K145Tup57NrEI0Z5ITlhvaVaRKRp3M4OJBYn51FvOsi0kdzfwpTp0rY2y?= =?us-ascii?Q?QgVZPKecQuzUnXHI4Dr3Fdk10sODMKWrGFFRvvdlR9MO6OD7VNitEAZPrQTa?= =?us-ascii?Q?hPik4z/jSF5mQ28Ae38G1Ah1xPCYckAij2k1vm/k438nWddX0gf7mpM1jMWm?= =?us-ascii?Q?VvImItCE6Cf8l2dgYc90Ewkem2hj0DVZf36vy7epbCQJ8TU06965vwdHoJ3p?= =?us-ascii?Q?dNwe+UxZSgfRfeiVmNy4RyJRzme+SDBjp8eco6+P+nhslv/K5ccaCC1j2T8q?= =?us-ascii?Q?Gb2Yr2w6vUOW+MeBHymxCyTHPh+7ptwOBIo/V8+KqXxdm0FglMRUMzmfY92D?= =?us-ascii?Q?4l8fwnXa8JYkOQHje5hQbWdCOKB8wNgyujxl4h4OCZ3NioGdsUoX/V9yjff4?= =?us-ascii?Q?O9iD7u5i8DAhjQbizT9N6D7b6IsowpVSNB8m2sGxW3Qj9UTFF5wks6zSO7tB?= =?us-ascii?Q?Sc30jQPfZxNvy42OSLwQ+T0GdBh2nKfxVw09bvVykiSjJvelLiwHLHE6CBvf?= =?us-ascii?Q?JwV29krTdpo5qcszc6zDcxUvkh0hy+YyEkyNbo6F2isNfcG6WxSlV43MbzBr?= =?us-ascii?Q?/G0goX91LKIllCL1RnrlCiMC52nbSi3EHLHLCo4uSDHOO/pzJCDN9Nelmhfg?= =?us-ascii?Q?niMU/XlJG9JmmvKmFyN+WIcBx4Fm2ab/oUDhD8w3igR487Ls+POp946f3YyL?= =?us-ascii?Q?+7yZFC8=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)(376005)(1800799015)(36860700004)(82310400017); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: TlSWLPrFztTs/RvyPrNbPIPwog7jDqvrJ96c8Iym+0WxYTNweyhQA1hqXktksrv41k+waO964ZWZuRVjA03bwKxGTlWW11tRjL47L524Z/4QtsQiV9LiPgAQXdVsjMbaByDeGY1dEOcWXaT3ejnxBBtJ7GseJw3Fj5by6NWgWj5au27dI9vkZ8feP+cQKefqBXLkBrx/miLvBgIszWqAGQo7WhnlnHj/sT0aC+CoPLo5WeAoAKdPa66ms0M3OhstAa6Bi4b9nhW5yrhfkyL+cZw1FM9Ox1XULHkqs5WJWMG5N3lVgAy7D9EL/mqas94qV0lntQwtBfYWmgNgjnyE94X47uSJTmS59L0OcfCvWFFQUJa7qffSYKLFENbK7WptfGI+BoDJpNR8kAISFJgZqF8D1s8NlpgdxD99yZSBatHcjqN62i1zHyzJnxdTx36PNm9eQmMejOxL6TrTOXPQ+ap1DimKZjyhgVgje/wVrNPUj/s/DCUGh7pGtOrvUACUAGtCOCqfNSqwmYH5Te0GDgB8uXRtU8kdH0cxoHdEqv/1J3Z9084W2p4lYLcmA3wujV2XPp9f6oHI8VxrBZaq+isWGAqf4XHmtgkZD3GpF5rSKHlSl/KmfBDnfzJFiuCLTGL0Cg/dGniXbsENB5/12Q== X-OriginatorOrg: napatech.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 May 2024 14:49:58.3202 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 35e64c98-1945-4a59-70a0-08dc80b7c6fe 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: DB1PEPF000509EB.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0P190MB0737 X-BESS-ID: 1717080600-304863-12850-22679-1 X-BESS-VER: 2019.1_20240429.2309 X-BESS-Apparent-Source-IP: 104.47.2.50 X-BESS-Parts: H4sIAAAAAAACA4uuVkqtKFGyUioBkjpK+cVKVoZGhoaWQGYGUDTFyDTVxNwoNc Ui2cDI1NQy1SzZIsnILCnF1MzYyCQ5Wak2FgAP/9b5QgAAAA== X-BESS-Outbound-Spam-Score: 0.00 X-BESS-Outbound-Spam-Report: Code version 3.2, rules version 3.2.2.256603 [from cloudscan22-78.eu-central-1b.ess.aws.cudaops.com] Rule breakdown below pts rule name description ---- ---------------------- -------------------------------- 0.00 BSF_BESS_OUTBOUND META: BESS Outbound X-BESS-Outbound-Spam-Status: SCORE=0.00 using account:ESS113687 scores of KILL_LEVEL=7.0 tests=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 --- drivers/net/ntnic/ntlog/include/ntlog.h | 162 +++++++++++++++++++++ drivers/net/ntnic/ntlog/ntlog.c | 108 ++++++++++++++ drivers/net/ntnic/ntutil/include/nt_util.h | 51 +++++++ drivers/net/ntnic/ntutil/nt_util.c | 98 +++++++++++++ 4 files changed, 419 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..a4f104efad --- /dev/null +++ b/drivers/net/ntnic/ntlog/ntlog.c @@ -0,0 +1,108 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Napatech A/S + */ + +#include "ntlog.h" + +#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; +} + +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..e4b18c1a7e --- /dev/null +++ b/drivers/net/ntnic/ntutil/nt_util.c @@ -0,0 +1,98 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2023 Napatech A/S + */ + +#include +#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