]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Set num_workers default value earlier
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 31 May 2023 19:15:34 +0000 (15:15 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 31 May 2023 19:15:39 +0000 (15:15 -0400)
src/lib/server/cf_parse.c
src/lib/server/cf_parse.h
src/lib/server/main_config.c

index 30294265d45f35ca86c0c3058967dce876370bff..1be1629a09181260289964c0721975ecb29d5d5d 100644 (file)
@@ -298,13 +298,14 @@ finish:
  * The pair created by this function should fed to #cf_pair_parse for parsing.
  *
  * @param[out] out     Where to write the CONF_PAIR we created with the default value.
+ * @param[in] parent   being populated.
  * @param[in] cs       to parent the CONF_PAIR from.
  * @param[in] rule     to use to create the default.
  * @return
  *     - 0 on success.
  *     - -1 on failure.
  */
-static int cf_pair_default(CONF_PAIR **out, CONF_SECTION *cs, CONF_PARSER const *rule)
+static int cf_pair_default(CONF_PAIR **out, void *parent, CONF_SECTION *cs, CONF_PARSER const *rule)
 
 {
        int             lineno = 0;
@@ -338,7 +339,7 @@ static int cf_pair_default(CONF_PAIR **out, CONF_SECTION *cs, CONF_PARSER const
         *      Use the dynamic default function if set
         */
        if (rule->dflt_func) {
-               if (rule->dflt_func(out, cs, dflt_quote, rule) < 0) {
+               if (rule->dflt_func(out, parent, cs, dflt_quote, rule) < 0) {
                        cf_log_perr(cs, "Failed producing default for \"%s\"", rule->name);
                        return -1;
                }
@@ -437,7 +438,7 @@ static int CC_HINT(nonnull(4,5)) cf_pair_parse_internal(TALLOC_CTX *ctx, void *o
                                return 1;
                        }
 
-                       if (cf_pair_default(&dflt_cp, cs, rule) < 0) return -1;
+                       if (cf_pair_default(&dflt_cp, base, cs, rule) < 0) return -1;
                        count = cf_pair_count(cs, rule->name);  /* Dynamic functions can add multiple defaults */
                        if (!count) {
                                if (fr_rule_not_empty(rule)) {
@@ -535,7 +536,7 @@ static int CC_HINT(nonnull(4,5)) cf_pair_parse_internal(TALLOC_CTX *ctx, void *o
                                return 1;
                        }
 
-                       if (cf_pair_default(&dflt_cp, cs, rule) < 0) return -1;
+                       if (cf_pair_default(&dflt_cp, base, cs, rule) < 0) return -1;
                        cp = dflt_cp;
                        if (!cp) {
                                if (fr_rule_not_empty(rule)) {
index 1581ec230afff076b734680dcd9bcabd0f5a1238..1c0fabb3ce136f07f89f136a31bd9b5e8a60f5fd 100644 (file)
@@ -445,6 +445,7 @@ typedef int (*cf_parse_t)(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *c
 /** Callback for producing dynamic defaults from 3rd party libraries
  *
  * @param[out] out     Where to write default conf pair.
+ * @param[in] parent   being populated.
  * @param[in] cs       to allocate pair in.
  * @param[in] quote    to use when allocing the pair.  Provided as a convenience.
  * @param[in] rule     to produce default for.
@@ -452,7 +453,7 @@ typedef int (*cf_parse_t)(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *c
  *     - 0 on success.
  *     - -1 on failure.
  */
-typedef int (*cf_dflt_t)(CONF_PAIR **out, CONF_SECTION *cs, fr_token_t quote, CONF_PARSER const *rule);
+typedef int (*cf_dflt_t)(CONF_PAIR **out, void *parent, CONF_SECTION *cs, fr_token_t quote, CONF_PARSER const *rule);
 
 /** Defines a #CONF_PAIR to C data type mapping
  *
index f46db3ac6c453cae0c11a76bdde8e8d9f855cd01..7ab4be48c957cec44becfc37c7dd4bc5f676d402 100644 (file)
  * @copyright 2002,2006-2007 The FreeRADIUS server project
  * @copyright 2002 Alan DeKok (aland@freeradius.org)
  */
-#include "lib/unlang/xlat.h"
+
 RCSID("$Id$")
 
 #include <freeradius-devel/server/cf_file.h>
+#include <freeradius-devel/server/cf_util.h>
 #include <freeradius-devel/server/client.h>
 #include <freeradius-devel/server/cond.h>
 #include <freeradius-devel/server/dependency.h>
@@ -37,6 +38,8 @@ RCSID("$Id$")
 #include <freeradius-devel/server/util.h>
 #include <freeradius-devel/server/virtual_servers.h>
 
+#include <freeradius-devel/unlang/xlat.h>
+
 #include <freeradius-devel/util/conf.h>
 #include <freeradius-devel/util/debug.h>
 #include <freeradius-devel/util/dict.h>
@@ -44,6 +47,7 @@ RCSID("$Id$")
 #include <freeradius-devel/util/hw.h>
 #include <freeradius-devel/util/perm.h>
 #include <freeradius-devel/util/sem.h>
+#include <freeradius-devel/util/token.h>
 
 #include <freeradius-devel/unlang/xlat_func.h>
 
@@ -85,6 +89,8 @@ static int hostname_lookups_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF
 
 static int num_networks_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, CONF_PARSER const *rule);
 static int num_workers_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, CONF_PARSER const *rule);
+static int num_workers_dflt(CONF_PAIR **out, void *parent, CONF_SECTION *cs, fr_token_t quote, CONF_PARSER const *rule);
+
 static int lib_dir_on_read(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, CONF_PARSER const *rule);
 
 static int talloc_pool_size_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, CONF_PARSER const *rule);
@@ -165,7 +171,7 @@ static const CONF_PARSER thread_config[] = {
        { FR_CONF_OFFSET("num_networks", FR_TYPE_UINT32, main_config_t, max_networks), .dflt = STRINGIFY(1),
          .func = num_networks_parse },
        { FR_CONF_OFFSET("num_workers", FR_TYPE_UINT32, main_config_t, max_workers), .dflt = STRINGIFY(0),
-         .func = num_workers_parse },
+         .func = num_workers_parse, .dflt_func = num_workers_dflt },
 
        { FR_CONF_OFFSET("stats_interval", FR_TYPE_TIME_DELTA | FR_TYPE_HIDDEN, main_config_t, stats_interval), },
 
@@ -417,61 +423,64 @@ static int num_networks_parse(TALLOC_CTX *ctx, void *out, void *parent,
        return 0;
 }
 
-static int num_workers_parse(TALLOC_CTX *ctx, void *out, void *parent,
-                            CONF_ITEM *ci, CONF_PARSER const *rule)
+static int num_workers_parse(TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, CONF_PARSER const *rule)
 {
        int             ret;
        uint32_t        value;
-       main_config_t   *conf = parent;
 
        if ((ret = cf_pair_parse_value(ctx, out, parent, ci, rule)) < 0) return ret;
 
        memcpy(&value, out, sizeof(value));
 
+       fr_assert_msg(value != 0, "Number of workers should never be zero, verify num_workers_dflt called");
+
        /*
         *      If no value is specified, try and
         *      discover it automatically.
         */
-       if (value == 0) {
-               char *strvalue;
+       FR_INTEGER_BOUND_CHECK("thread.num_workers", value, >=, 1);
+       FR_INTEGER_BOUND_CHECK("thread.num_workers", value, <=, 128);
 
-               value = fr_hw_num_cores_active();
+       memcpy(out, &value, sizeof(value));
 
-               /*
-                *      If we've got more than four times
-                *      the number of cores as we have
-                *      networks, then set the number of
-                *      workers to the number of cores
-                *      minus networks.
-                *
-                *      This ensures at a least a 4:1
-                *      ratio of workers to networks,
-                *      which seems like a sensible ratio.
-                */
-               if ((conf->max_networks * 4) < value) {
-                       value -= conf->max_networks;
-               }
+       return 0;
+}
+
+static int num_workers_dflt(CONF_PAIR **out, void *parent, CONF_SECTION *cs, fr_token_t quote, CONF_PARSER const *rule)
+{
+       char            *strvalue;
+       uint32_t        value;
+       main_config_t   *conf = parent;
 
-               strvalue = talloc_asprintf(ci, "%u", value);
-               (void) cf_pair_replace(cf_item_to_section(cf_parent(ci)), cf_item_to_pair(ci), strvalue);
-               talloc_free(strvalue);
+       value = fr_hw_num_cores_active();
 
-               /*
-                *      Otherwise just create as many
-                *      workers as we have cores.
-                */
-               cf_log_info(ci, "Setting thread.workers = %u", value);
-       } else {
-               FR_INTEGER_BOUND_CHECK("thread.num_workers", value, >=, 1);
-               FR_INTEGER_BOUND_CHECK("thread.num_workers", value, <=, 128);
+       /*
+        *      If we've got more than four times
+        *      the number of cores as we have
+        *      networks, then set the number of
+        *      workers to the number of cores
+        *      minus networks.
+        *
+        *      This ensures at a least a 4:1
+        *      ratio of workers to networks,
+        *      which seems like a sensible ratio.
+        */
+       if ((conf->max_networks * 4) < value) {
+               value -= conf->max_networks;
        }
+       strvalue = talloc_asprintf(NULL, "%u", value);
+       *out = cf_pair_alloc(cs, rule->name, strvalue, T_OP_EQ, T_BARE_WORD, quote);
+       talloc_free(strvalue);
 
-       memcpy(out, &value, sizeof(value));
+       /*
+        *      Otherwise just create as many
+        *      workers as we have cores.
+        */
+       cf_log_info(cs, "Dynamically determined thread.workers = %u", value);
 
        return 0;
 }
 
-
 static int xlat_config_escape(UNUSED request_t *request, fr_value_box_t *vb, UNUSED void *uctx)
 {
        static char const       disallowed[] = "%{}\\'\"`";