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 E1BDF42DDE; Thu, 6 Jul 2023 00:48:27 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 5784242D4B; Thu, 6 Jul 2023 00:48:15 +0200 (CEST) Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by mails.dpdk.org (Postfix) with ESMTP id BFC0142D50 for ; Thu, 6 Jul 2023 00:48:12 +0200 (CEST) Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-262e5e71978so33762a91.1 for ; Wed, 05 Jul 2023 15:48:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20221208.gappssmtp.com; s=20221208; t=1688597292; x=1691189292; 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=2oKzsvdvjYPQihIdA7H/3xJp7JRwySRFEKzx+/Tx6+Y=; b=G1vTiB1SviY15PMAaYfvNNolkSRvuIVgJA3XSS24ZFNaW2e4h2hebvPObNOEMTQk/Y S+ah6csEHViCgvdlpHvdZbN6u7i4pAK7r2QAzPA2+K+t6CxfIBmFvioSx6wCCJx47CSz MoMuLiA7XGETUgSrkEtpEk/7v2ThVWMnubaVvdBehN4AdIVwyUTEwV8D3I/8FuDFY8Rf wDd217oyV9M/pB6UF39P6Ekd1j5JH7vLyZsEwewOWSckyQJe3m4X83Hs/z0S06Kt4NEI m3RfB/jbcilCf88mWcpLcTN/+G/K4fX9SPP5wim7Aj2J5Pp2GwZC4BzbK1UwPuomPbvV wK6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688597292; x=1691189292; 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=2oKzsvdvjYPQihIdA7H/3xJp7JRwySRFEKzx+/Tx6+Y=; b=dOOxvyW0aiYWM3bVf8vSXxLR6aDRIecPqC/Zhy4Hs43ps67O7w+KYlI0aSk0c8Vbjp HWARLurPx3AYVbV+8xKnn+/+MzIDk0ZsNeB3r/2OQZALxH17Ve61m0AF9bTWkdP6mQYY dTzpgo96bQBu9tHgxcSUC+g4Qhd2yoYJavGabyQL7sRYA9MOi3pUaBOnJJZJjef39yPk mWvyoFwfzSF8A21ST9GRniJBCqsxNFiD3/qd+hLeCHulnxwFosDEK4HtvRnicM9BpSMF PZuolukwg3Imr56928+dGpVE/iF2BSokBf2SYq/S0LXH2OG/T84UP6sEk9LeR04GTFAF 3XZQ== X-Gm-Message-State: ABy/qLaoEamUU+5LT6Qh9VmYo1gSxax6SB2DuWhhN2OuKjtjxlxwHeNj SgRV0X+rWdf3EWQMg3PIX132a51Yuh91kVC/PEo= X-Google-Smtp-Source: APBJJlEik641ndqawo+Uqb/cF8Zcq0UcNufUmNqWSKt8SgIyE9tIR5roHm6L28Ce5g4uQsmihCLF6A== X-Received: by 2002:a17:90a:8b07:b0:263:1e6c:16f4 with SMTP id y7-20020a17090a8b0700b002631e6c16f4mr5127145pjn.20.1688597291647; Wed, 05 Jul 2023 15:48:11 -0700 (PDT) Received: from hermes.local (204-195-120-218.wavecable.com. [204.195.120.218]) by smtp.gmail.com with ESMTPSA id bb16-20020a17090b009000b002630bfd35b0sm1906044pjb.7.2023.07.05.15.48.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jul 2023 15:48:11 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Bruce Richardson , Dmitry Kozlyuk , Narcisa Ana Maria Vasile , Dmitry Malloy , Pallavi Kadam Subject: [PATCH v7 3/5] eal: unify logging code Date: Wed, 5 Jul 2023 15:48:03 -0700 Message-Id: <20230705224805.286156-4-stephen@networkplumber.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230705224805.286156-1-stephen@networkplumber.org> References: <20200814173441.23086-1-stephen@networkplumber.org> <20230705224805.286156-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 FreeBSD logging code was not using syslog and did not have the same options as Linux. Use a common set of functions for that. Pre-parsing for log level is common to all OS's. Now the pre-scan can return an error. A bad argument give to --log-level option was given the code would keep going. Use opterr to fix duplicate error message on bad option. Signed-off-by: Stephen Hemminger --- .../freebsd_gsg/freebsd_eal_parameters.rst | 27 +++++++++ lib/eal/common/eal_common_log.c | 49 +++++++++++++++++ lib/eal/common/eal_log.h | 5 ++ lib/eal/freebsd/eal.c | 55 ++++--------------- lib/eal/linux/eal.c | 46 ++-------------- lib/eal/linux/meson.build | 1 - lib/eal/{linux => unix}/eal_log.c | 11 ++++ lib/eal/unix/meson.build | 1 + lib/eal/windows/eal.c | 36 +----------- 9 files changed, 112 insertions(+), 119 deletions(-) rename lib/eal/{linux => unix}/eal_log.c (76%) diff --git a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst index fba467a2ce92..9270d9fa3bfc 100644 --- a/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst +++ b/doc/guides/freebsd_gsg/freebsd_eal_parameters.rst @@ -18,3 +18,30 @@ FreeBSD-specific EAL parameters ------------------------------- There are currently no FreeBSD-specific EAL command-line parameters available. + +Other options +~~~~~~~~~~~~~ + +* ``--syslog `` + + Set syslog facility. Valid syslog facilities are:: + + auth + cron + daemon + ftp + kern + lpr + mail + news + syslog + user + uucp + local0 + local1 + local2 + local3 + local4 + local5 + local6 + local7 diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c index bd7b188ceb4a..1b2080e8ef04 100644 --- a/lib/eal/common/eal_common_log.c +++ b/lib/eal/common/eal_common_log.c @@ -2,6 +2,7 @@ * Copyright(c) 2010-2014 Intel Corporation */ +#include #include #include #include @@ -16,6 +17,8 @@ #include #include "eal_log.h" +#include "eal_internal_cfg.h" +#include "eal_options.h" #include "eal_private.h" struct rte_log_dynamic_type { @@ -223,6 +226,52 @@ log_save_level(uint32_t priority, const char *regex, const char *pattern) return -1; } + +/* Parse the all arguments looking for --log-level */ +int +eal_log_level_parse(int argc, char * const argv[]) +{ + struct internal_config *internal_conf = eal_get_internal_configuration(); + int option_index, opt; + const int old_optind = optind; + const int old_optopt = optopt; + const int old_opterr = opterr; + char *old_optarg = optarg; +#ifdef RTE_EXEC_ENV_FREEBSD + const int old_optreset = optreset; + optreset = 1; +#endif + + optind = 1; + opterr = 0; + + while ((opt = getopt_long(argc, argv, eal_short_options, + eal_long_options, &option_index)) != EOF) { + + switch (opt) { + case OPT_LOG_LEVEL_NUM: + if (eal_parse_common_option(opt, optarg, internal_conf) < 0) + return -1; + break; + case '?': + /* getopt is not happy, stop right now */ + goto out; + default: + continue; + } + } +out: + /* restore getopt lib */ + optind = old_optind; + optopt = old_optopt; + optarg = old_optarg; + opterr = old_opterr; +#ifdef RTE_EXEC_ENV_FREEBSD + optreset = old_optreset; +#endif + return 0; +} + int eal_log_save_regexp(const char *regex, uint32_t level) { diff --git a/lib/eal/common/eal_log.h b/lib/eal/common/eal_log.h index c784fa604389..31dc489350f6 100644 --- a/lib/eal/common/eal_log.h +++ b/lib/eal/common/eal_log.h @@ -13,6 +13,11 @@ */ int eal_log_init(const char *id, int facility); +/* + * Scan command line args for log settings. + */ +int eal_log_level_parse(int argc, char * const argv[]); + /* * Determine where log data is written when no call to rte_openlog_stream. */ diff --git a/lib/eal/freebsd/eal.c b/lib/eal/freebsd/eal.c index 7008303e112a..bb9a2b1653d9 100644 --- a/lib/eal/freebsd/eal.c +++ b/lib/eal/freebsd/eal.c @@ -52,6 +52,7 @@ #include "eal_hugepages.h" #include "eal_options.h" #include "eal_memcfg.h" +#include "eal_log.h" #include "eal_trace.h" #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL) @@ -363,48 +364,6 @@ eal_get_hugepage_mem_size(void) return (size < SIZE_MAX) ? (size_t)(size) : SIZE_MAX; } -/* Parse the arguments for --log-level only */ -static void -eal_log_level_parse(int argc, char **argv) -{ - int opt; - char **argvopt; - int option_index; - const int old_optind = optind; - const int old_optopt = optopt; - const int old_optreset = optreset; - char * const old_optarg = optarg; - struct internal_config *internal_conf = - eal_get_internal_configuration(); - - argvopt = argv; - optind = 1; - optreset = 1; - - while ((opt = getopt_long(argc, argvopt, eal_short_options, - eal_long_options, &option_index)) != EOF) { - - int ret; - - /* getopt is not happy, stop right now */ - if (opt == '?') - break; - - ret = (opt == OPT_LOG_LEVEL_NUM) ? - eal_parse_common_option(opt, optarg, internal_conf) : 0; - - /* common parser is not happy */ - if (ret < 0) - break; - } - - /* restore getopt lib */ - optind = old_optind; - optopt = old_optopt; - optreset = old_optreset; - optarg = old_optarg; -} - /* Parse the argument given in the command line of the application */ static int eal_parse_args(int argc, char **argv) @@ -610,7 +569,11 @@ rte_eal_init(int argc, char **argv) eal_save_args(argc, argv); /* set log level as early as possible */ - eal_log_level_parse(argc, argv); + if (eal_log_level_parse(argc, argv) < 0) { + rte_eal_init_alert("invalid log option."); + rte_errno = EINVAL; + return -1; + } if (rte_eal_cpu_init() < 0) { rte_eal_init_alert("Cannot detect lcores."); @@ -759,6 +722,12 @@ rte_eal_init(int argc, char **argv) #endif } + if (eal_log_init(getprogname(), internal_conf->syslog_facility) < 0) { + rte_eal_init_alert("Cannot init logging."); + rte_errno = ENOMEM; + return -1; + } + /* in secondary processes, memory init may allocate additional fbarrays * not present in primary processes, so to avoid any potential issues, * initialize memzones first. diff --git a/lib/eal/linux/eal.c b/lib/eal/linux/eal.c index 145afafde234..c9475560d986 100644 --- a/lib/eal/linux/eal.c +++ b/lib/eal/linux/eal.c @@ -546,45 +546,6 @@ eal_parse_vfio_vf_token(const char *vf_token) return -1; } -/* Parse the arguments for --log-level only */ -static void -eal_log_level_parse(int argc, char **argv) -{ - int opt; - char **argvopt; - int option_index; - const int old_optind = optind; - const int old_optopt = optopt; - char * const old_optarg = optarg; - struct internal_config *internal_conf = - eal_get_internal_configuration(); - - argvopt = argv; - optind = 1; - - while ((opt = getopt_long(argc, argvopt, eal_short_options, - eal_long_options, &option_index)) != EOF) { - - int ret; - - /* getopt is not happy, stop right now */ - if (opt == '?') - break; - - ret = (opt == OPT_LOG_LEVEL_NUM) ? - eal_parse_common_option(opt, optarg, internal_conf) : 0; - - /* common parser is not happy */ - if (ret < 0) - break; - } - - /* restore getopt lib */ - optind = old_optind; - optopt = old_optopt; - optarg = old_optarg; -} - static int eal_parse_huge_worker_stack(const char *arg) { @@ -993,7 +954,12 @@ rte_eal_init(int argc, char **argv) eal_reset_internal_config(internal_conf); /* set log level as early as possible */ - eal_log_level_parse(argc, argv); + if (eal_log_level_parse(argc, argv) < 0) { + rte_eal_init_alert("invalid log option."); + rte_errno = EINVAL; + __atomic_store_n(&run_once, 0, __ATOMIC_RELAXED); + return -1; + } /* clone argv to report out later in telemetry */ eal_save_args(argc, argv); diff --git a/lib/eal/linux/meson.build b/lib/eal/linux/meson.build index 5af456db9edb..e99ebed25692 100644 --- a/lib/eal/linux/meson.build +++ b/lib/eal/linux/meson.build @@ -11,7 +11,6 @@ sources += files( 'eal_hugepage_info.c', 'eal_interrupts.c', 'eal_lcore.c', - 'eal_log.c', 'eal_memalloc.c', 'eal_memory.c', 'eal_thread.c', diff --git a/lib/eal/linux/eal_log.c b/lib/eal/unix/eal_log.c similarity index 76% rename from lib/eal/linux/eal_log.c rename to lib/eal/unix/eal_log.c index d44416fd6570..85d817c2d31e 100644 --- a/lib/eal/linux/eal_log.c +++ b/lib/eal/unix/eal_log.c @@ -9,6 +9,8 @@ #include #include "eal_log.h" +#include "eal_internal_cfg.h" +#include "eal_private.h" /* * default log function @@ -25,6 +27,15 @@ console_log_write(__rte_unused void *c, const char *buf, size_t size) /* Syslog error levels are from 0 to 7, so subtract 1 to convert */ syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf); +#ifdef RTE_EXEC_ENV_LINUX + /* Handle glibc quirk: write function should return the number of bytes + * copied from buf, or 0 on error. (The function must not return a negative value.) + * FreeBSD expects that write function behaves like write(2). + */ + if (ret < 0) + ret = 0; +#endif + return ret; } diff --git a/lib/eal/unix/meson.build b/lib/eal/unix/meson.build index cc7d67dd321d..37d07594df29 100644 --- a/lib/eal/unix/meson.build +++ b/lib/eal/unix/meson.build @@ -6,6 +6,7 @@ sources += files( 'eal_file.c', 'eal_filesystem.c', 'eal_firmware.c', + 'eal_log.c', 'eal_unix_memory.c', 'eal_unix_thread.c', 'eal_unix_timer.c', diff --git a/lib/eal/windows/eal.c b/lib/eal/windows/eal.c index 2d7a0e9ab27e..c6006c48ddef 100644 --- a/lib/eal/windows/eal.c +++ b/lib/eal/windows/eal.c @@ -96,41 +96,6 @@ eal_usage(const char *prgname) } } -/* Parse the arguments for --log-level only */ -static void -eal_log_level_parse(int argc, char **argv) -{ - int opt; - char **argvopt; - int option_index; - struct internal_config *internal_conf = - eal_get_internal_configuration(); - - argvopt = argv; - - eal_reset_internal_config(internal_conf); - - while ((opt = getopt_long(argc, argvopt, eal_short_options, - eal_long_options, &option_index)) != EOF) { - - int ret; - - /* getopt is not happy, stop right now */ - if (opt == '?') - break; - - ret = (opt == OPT_LOG_LEVEL_NUM) ? - eal_parse_common_option(opt, optarg, - internal_conf) : 0; - - /* common parser is not happy */ - if (ret < 0) - break; - } - - optind = 0; /* reset getopt lib */ -} - /* Parse the argument given in the command line of the application */ static int eal_parse_args(int argc, char **argv) @@ -143,6 +108,7 @@ eal_parse_args(int argc, char **argv) eal_get_internal_configuration(); argvopt = argv; + opterr = 1; while ((opt = getopt_long(argc, argvopt, eal_short_options, eal_long_options, &option_index)) != EOF) { -- 2.39.2