From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 90FF742D8D;
	Thu, 29 Jun 2023 17:59:22 +0200 (CEST)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 3FCB341104;
	Thu, 29 Jun 2023 17:59:20 +0200 (CEST)
Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com
 [209.85.215.174])
 by mails.dpdk.org (Postfix) with ESMTP id 36CD8406B7
 for <dev@dpdk.org>; Thu, 29 Jun 2023 17:59:17 +0200 (CEST)
Received: by mail-pg1-f174.google.com with SMTP id
 41be03b00d2f7-55b1238a013so598096a12.3
 for <dev@dpdk.org>; Thu, 29 Jun 2023 08:59:17 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=networkplumber-org.20221208.gappssmtp.com; s=20221208; t=1688054356;
 x=1690646356; 
 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=qOUhFNDBJzTW/ylgqhJjNMUgMVnNVNu77BRkvSIbP6o=;
 b=3ABgOLbhE0PisNCZcIxifS/mQEbk0SwkQUyXeBnhUFr8P9eilNWzMHUDcmpama+fZI
 rRiA4RS5COfbwcvyz88y6hWCpjDIFODjFD8w3OMqheGa1irOOjxNFQ4tIp77MZN/rSPI
 meUeSs3Dlj1WPDT8Bu6FZ3uIZ26f1KzzsOnOqXDdBcXtLYgRpquA7pcoANNQ9J3HYoHw
 cJ+7jK+cROy7aSO/5A6HrzN7CNvW3s+9oIQK0UhHGHJotdt3r4HhFq7XdB1oyKAWjQWq
 yHST2C8B31Q5GjFd5C6RIGvSyc4rBMpqmclp6ZhkPR7+V99fPSXqGPy2cmwcGYvMnVQF
 B8vA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20221208; t=1688054356; x=1690646356;
 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=qOUhFNDBJzTW/ylgqhJjNMUgMVnNVNu77BRkvSIbP6o=;
 b=bZlypOI2hQaKTxTod621lUh247xedP/sPJJeCGyEZ+FCGANFNxmFK/2rxjPUVVxhfK
 z9kBYjRypUtfO1ApBchLBG9xTzFMTvqmCuBR1L3ghXeKjHbMgl71ZSUEuh4VHmYWSBAb
 dVYpDu0hdabdXPKODszLqRRt4hAgnDAi9QnGWi0ncpIzgIgYfJ7qIParIX89Io3YqxIH
 3Baj34bkH6hKPdxrPS695iiwiP7Bw9xYZpK96orpFxAulZiBpHfUZhg4vv1J7Oz4pHI5
 6gj5AAD2clQv/q4SpXbkjC7tewVvEkVaCZhVcsOpCPs5v/wxf6o/XsQ59KnYPAztvClX
 Z0Cg==
X-Gm-Message-State: AC+VfDwgodJYPvUn5+8L7xpL2NkIDyMgiQ6CxjuPEa4MZ/y9DpW8NsaC
 bM1Lnb83tH6xotseVgWexx3pZkwNJzyhL/8oFsuhQA==
X-Google-Smtp-Source: ACHHUZ7RR+6WVLs0cnswTZV6Wr49e7AXKvn3D13gPt6F7SfwoNgJykG7NW6E15c43FQBIqr86FAtlA==
X-Received: by 2002:a05:6a20:72a5:b0:122:92d0:452a with SMTP id
 o37-20020a056a2072a500b0012292d0452amr52349pzk.37.1688054356094; 
 Thu, 29 Jun 2023 08:59:16 -0700 (PDT)
Received: from hermes.local (204-195-120-218.wavecable.com. [204.195.120.218])
 by smtp.gmail.com with ESMTPSA id
 c6-20020a63ea06000000b0055b553157a5sm471716pgi.71.2023.06.29.08.59.15
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 29 Jun 2023 08:59:15 -0700 (PDT)
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>,
 Bruce Richardson <bruce.richardson@intel.com>,
 Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>,
 Narcisa Ana Maria Vasile <navasile@linux.microsoft.com>,
 Dmitry Malloy <dmitrym@microsoft.com>,
 Pallavi Kadam <pallavi.kadam@intel.com>
Subject: [PATCH v6 1/6] eal: unify logging code
Date: Thu, 29 Jun 2023 08:58:38 -0700
Message-Id: <20230629155858.75668-2-stephen@networkplumber.org>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20230629155858.75668-1-stephen@networkplumber.org>
References: <20200814173441.23086-1-stephen@networkplumber.org>
 <20230629155858.75668-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 <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=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.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>

make log_parse common to windows
---
 .../freebsd_gsg/freebsd_eal_parameters.rst    | 27 +++++++++
 lib/eal/common/eal_common_log.c               | 46 ++++++++++++++++
 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                         | 35 ------------
 9 files changed, 108 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 <syslog facility>``
+
+    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..7e2f010fa371 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 <getopt.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stdarg.h>
@@ -16,6 +17,8 @@
 #include <rte_per_lcore.h>
 
 #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,49 @@ 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;
+	char * const old_optarg = optarg;
+#ifdef RTE_EXEC_ENV_FREEBSD
+	const int old_optreset = optreset;
+	optreset = 1;
+#endif
+
+	optind = 1;
+
+	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;
+#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 <rte_log.h>
 
 #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..240d5b8ad3e4 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)
-- 
2.39.2