]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
modules/hints,lib/rules: rework TTL defaults
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 23 Aug 2023 09:58:39 +0000 (11:58 +0200)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Tue, 12 Sep 2023 10:12:55 +0000 (12:12 +0200)
It was a bit wide, with 5s and 3h, and distinction between
these two "groups" of rules was a bit random wrt. TTL choice.
Now: 5m for user's rules and 1h for RFC-default rules.

I found it relatively hard to choose defaults, but at least for
user-supplied rules it's trivial to configure a different default.

lib/rules/api.c
lib/rules/api.h
lib/rules/defaults.c
lib/rules/impl.h
lib/rules/zonefile.c
modules/hints/hints.c

index 8ff809d2c46e121a8debe36232a4cfd6a5ef6492..e2992c012dc2c4e7364902a595e22dc7085410d6 100644 (file)
 
 struct kr_rules *the_rules = NULL;
 
-const uint32_t KR_RULE_TTL_DEFAULT = RULE_TTL_DEFAULT;
+/* The default TTL value is a compromise and probably of little practical impact.
+ * - answering from local rules should be quite cheap,
+ *   so very high values are not expected to bring any improvements
+ * - on the other hand, rules are not expected to change very dynamically
+ */
+const uint32_t KR_RULE_TTL_DEFAULT = 300;
 
 /* DB key-space summary
 
@@ -405,7 +410,7 @@ int rule_local_data_answer(struct kr_query *qry, knot_pkt_t *pkt)
                                return RET_CONT_CACHE;
                        }
                        // The other types optionally specify TTL.
-                       uint32_t ttl = RULE_TTL_DEFAULT;
+                       uint32_t ttl = KR_RULE_TTL_DEFAULT;
                        if (val.len >= sizeof(ttl)) // allow omitting -> can't kr_assert
                                deserialize_fails_assert(&val, &ttl);
                        if (kr_fails_assert(val.len == 0)) {
@@ -793,7 +798,7 @@ int kr_rule_local_subtree(const knot_dname_t *apex, enum kr_rule_sub_t type,
                .data = NULL,
                .len = sizeof(tags) + sizeof(ztype),
        };
-       const bool has_ttl = ttl != RULE_TTL_DEFAULT;
+       const bool has_ttl = ttl != KR_RULE_TTL_DEFAULT;
        if (has_ttl)
                val.len += sizeof(ttl);
        int ret = ruledb_op(write, &key, &val, 1);
index 44a4f3b89d542552b8c9897778652bfb6296c881..7998560f31565463a8fd72d87d167313c11c11a2 100644 (file)
@@ -58,6 +58,7 @@ int kr_view_select_action(const struct kr_request *req, knot_db_val_t *selected)
 
 /** Default TTL for answers from local data rules.
  *
+ * This applies to rules defined by the user, not the default rules.
  * Some types of rules save space when using this default.
  * This definition exists mainly for usage from lua.
  */
index bd50b5f61ccbf37eaf0bdedf5d565d486dcffb99..bd21fa1093b3a4c63a08bc422ece8bfd907dd6fe 100644 (file)
        if ((ret) < 0) { kr_assert(false); return kr_error((ret)); } \
 } while (false)
 
+/** RFC-defined local zones should be quite static,
+ * so we use a higher TTL separate from KR_RULE_TTL_DEFAULT. */
+#define TTL ((uint32_t)3600)
+
 int rules_defaults_insert(void)
 {
        static const char * names[] = {
@@ -137,7 +141,7 @@ int rules_defaults_insert(void)
                const knot_dname_t *dname =
                        knot_dname_from_str(name_buf, names[i], sizeof(name_buf));
                int ret = kr_rule_local_subtree(dname, KR_RULE_SUB_EMPTY,
-                                               RULE_TTL_DEFAULT, KR_RULE_TAGS_ALL);
+                                               TTL, KR_RULE_TAGS_ALL);
                CHECK_RET(ret);
                /* The double conversion is perhaps a bit wasteful, but it should be rare. */
                /* LATER: add extra info with explanation?  policy module had an ADDITIONAL
@@ -149,12 +153,12 @@ int rules_defaults_insert(void)
        knot_dname_t localhost_dname[] = "\x09localhost\0";
        { // forward localhost
                int ret = kr_rule_local_subtree(localhost_dname, KR_RULE_SUB_REDIRECT,
-                                               RULE_TTL_DEFAULT, KR_RULE_TAGS_ALL);
+                                               TTL, KR_RULE_TAGS_ALL);
                CHECK_RET(ret);
 
                knot_rrset_t rr = {
                        .owner = localhost_dname,
-                       .ttl = RULE_TTL_DEFAULT,
+                       .ttl = TTL,
                        .rclass = KNOT_CLASS_IN,
                        .rrs = { 0 },
                        .additional = NULL,
@@ -183,7 +187,7 @@ int rules_defaults_insert(void)
        { // reverse localhost; LATER: the situation isn't ideal with NXDOMAIN + some exact matches
                knot_rrset_t rr = {
                        .owner = localhost_dname,
-                       .ttl = RULE_TTL_DEFAULT,
+                       .ttl = TTL,
                        .type = KNOT_RRTYPE_PTR,
                        .rclass = KNOT_CLASS_IN,
                        .rrs = { 0 },
index 1ff78140a81d98128ce26d8bbf038dbe6f5d80c0..0d7de513ad981a6ded10c46c20557b24f21d4188 100644 (file)
@@ -11,8 +11,6 @@
 #undef VERBOSE_MSG
 #define VERBOSE_MSG(qry, ...) kr_log_q((qry), RULES,  ## __VA_ARGS__)
 
-#define RULE_TTL_DEFAULT ((uint32_t)10800)
-
 /** Insert all the default rules. in ./defaults.c */
 int rules_defaults_insert(void);
 
index da53675f1684782e9da7707a650e940bbaa3687d..d29ae35e2bbda263ee63edba28fd11a02a138fa4 100644 (file)
@@ -215,7 +215,7 @@ int kr_rule_zonefile(const struct kr_rule_zonefile_config *c)
        zs_scanner_t s_storage, *s = &s_storage;
        /* zs_init(), zs_set_input_file(), zs_set_processing() returns -1 in case of error,
         * so don't print error code as it meaningless. */
-       uint32_t ttl = c->ttl ? c->ttl : RULE_TTL_DEFAULT; // 0 would be nonsense
+       uint32_t ttl = c->ttl ? c->ttl : KR_RULE_TTL_DEFAULT; // 0 would be nonsense
        int ret = zs_init(s, NULL, KNOT_CLASS_IN, ttl);
        if (ret) {
                kr_log_error(RULES, "error initializing zone scanner instance, error: %i (%s)\n",
index c422bce0bf1d8cea7be450b858c2f3b4ead1a9bc..eaefaae4cc538cab0dc72a613b5bcf42bda5e127 100644 (file)
@@ -33,7 +33,6 @@ struct hints_data {
        bool use_nodata; /**< See hint_use_nodata() description, exposed via lua. */
        uint32_t ttl;    /**< TTL used for the hints, exposed via lua. */
 };
-static const uint32_t HINTS_TTL_DEFAULT = 5;
 
 /** Useful for returning from module properties. */
 static char * bool2jsonstr(bool val)
@@ -299,7 +298,7 @@ int hints_init(struct kr_module *module)
        if (!data)
                return kr_error(ENOMEM);
        data->use_nodata = true;
-       data->ttl = HINTS_TTL_DEFAULT;
+       data->ttl = KR_RULE_TTL_DEFAULT;
        module->data = data;
 
        return kr_ok();