| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <assert.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <stdio.h> |
| 17 |
|
| 18 |
#include <isc/buffer.h> |
| 19 |
#include <isc/loop.h> |
| 20 |
#include <isc/magic.h> |
| 21 |
#include <isc/refcount.h> |
| 22 |
#include <isc/rwlock.h> |
| 23 |
#include <isc/urcu.h> |
| 24 |
#include <isc/util.h> |
| 25 |
|
| 26 |
#include <dns/fixedname.h> |
| 27 |
#include <dns/name.h> |
| 28 |
#include <dns/qp.h> |
| 29 |
#include <dns/types.h> |
| 30 |
|
| 31 |
#include "qp_p.h" |
| 32 |
|
| 33 |
#include <tests/qp.h> |
| 34 |
|
| 35 |
/*********************************************************************** |
| 36 |
* |
| 37 |
* key reverse conversions |
| 38 |
*/ |
| 39 |
|
| 40 |
uint8_t |
| 41 |
qp_test_bittoascii(dns_qpshift_t bit) { |
| 42 |
uint8_t byte = dns_qp_byte_for_bit[bit]; |
| 43 |
if (bit == SHIFT_NOBYTE) { |
| 44 |
return '.'; |
| 45 |
} else if (qp_common_character(byte)) { |
| 46 |
return byte; |
| 47 |
} else if (byte < '-') { |
| 48 |
return '#'; |
| 49 |
} else if (byte < '_') { |
| 50 |
return '@'; |
| 51 |
} else { |
| 52 |
return '~' - SHIFT_OFFSET + bit; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
const char * |
| 57 |
qp_test_keytoascii(dns_qpkey_t key, size_t len) { |
| 58 |
for (size_t offset = 0; offset < len; offset++) { |
| 59 |
key[offset] = qp_test_bittoascii(key[offset]); |
| 60 |
} |
| 61 |
key[len] = '\0'; |
| 62 |
return (const char *)key; |
| 63 |
} |
| 64 |
|
| 65 |
/*********************************************************************** |
| 66 |
* |
| 67 |
* trie properties |
| 68 |
*/ |
| 69 |
|
| 70 |
static size_t |
| 71 |
getheight(dns_qp_t *qp, dns_qpnode_t *n) { |
| 72 |
if (node_tag(n) == LEAF_TAG) { |
| 73 |
return 0; |
| 74 |
} |
| 75 |
size_t max_height = 0; |
| 76 |
dns_qpnode_t *twigs = branch_twigs(qp, n); |
| 77 |
dns_qpweight_t size = branch_twigs_size(n); |
| 78 |
for (dns_qpweight_t pos = 0; pos < size; pos++) { |
| 79 |
size_t height = getheight(qp, &twigs[pos]); |
| 80 |
max_height = ISC_MAX(max_height, height); |
| 81 |
} |
| 82 |
return max_height + 1; |
| 83 |
} |
| 84 |
|
| 85 |
size_t |
| 86 |
qp_test_getheight(dns_qp_t *qp) { |
| 87 |
dns_qpnode_t *root = get_root(qp); |
| 88 |
return root == NULL ? 0 : getheight(qp, root); |
| 89 |
} |
| 90 |
|
| 91 |
static size_t |
| 92 |
maxkeylen(dns_qp_t *qp, dns_qpnode_t *n) { |
| 93 |
if (node_tag(n) == LEAF_TAG) { |
| 94 |
dns_qpkey_t key; |
| 95 |
return leaf_qpkey(qp, n, key); |
| 96 |
} |
| 97 |
size_t max_len = 0; |
| 98 |
dns_qpnode_t *twigs = branch_twigs(qp, n); |
| 99 |
dns_qpweight_t size = branch_twigs_size(n); |
| 100 |
for (dns_qpweight_t pos = 0; pos < size; pos++) { |
| 101 |
size_t len = maxkeylen(qp, &twigs[pos]); |
| 102 |
max_len = ISC_MAX(max_len, len); |
| 103 |
} |
| 104 |
return max_len; |
| 105 |
} |
| 106 |
|
| 107 |
size_t |
| 108 |
qp_test_maxkeylen(dns_qp_t *qp) { |
| 109 |
dns_qpnode_t *root = get_root(qp); |
| 110 |
return root == NULL ? 0 : maxkeylen(qp, root); |
| 111 |
} |
| 112 |
|
| 113 |
/*********************************************************************** |
| 114 |
* |
| 115 |
* dump to stdout |
| 116 |
*/ |
| 117 |
|
| 118 |
static void |
| 119 |
dumpread(dns_qpreadable_t qpr, const char *type, const char *tail) { |
| 120 |
dns_qpreader_t *qp = dns_qpreader(qpr); |
| 121 |
printf("%s %p root %u %u:%u base %p methods %p%s", type, qp, |
| 122 |
qp->root_ref, ref_chunk(qp->root_ref), ref_cell(qp->root_ref), |
| 123 |
qp->base, qp->methods, tail); |
| 124 |
} |
| 125 |
|
| 126 |
static void |
| 127 |
dumpqp(dns_qp_t *qp, const char *type) { |
| 128 |
dumpread(qp, type, " mctx "); |
| 129 |
printf("%p\n", qp->mctx); |
| 130 |
printf("%s %p usage %p chunk_max %u bump %u fender %u\n", type, qp, |
| 131 |
qp->usage, qp->chunk_max, qp->bump, qp->fender); |
| 132 |
printf("%s %p leaf %u live %u used %u free %u hold %u\n", type, qp, |
| 133 |
qp->leaf_count, qp->used_count - qp->free_count, qp->used_count, |
| 134 |
qp->free_count, qp->hold_count); |
| 135 |
printf("%s %p compact_all=%d transaction_mode=%d write_protect=%d\n", |
| 136 |
type, qp, qp->compact_all, qp->transaction_mode, |
| 137 |
qp->write_protect); |
| 138 |
} |
| 139 |
|
| 140 |
void |
| 141 |
qp_test_dumpread(dns_qpreadable_t qp) { |
| 142 |
dumpread(qp, "qpread", "\n"); |
| 143 |
fflush(stdout); |
| 144 |
} |
| 145 |
|
| 146 |
void |
| 147 |
qp_test_dumpsnap(dns_qpsnap_t *qp) { |
| 148 |
dumpread(qp, "qpsnap", " whence "); |
| 149 |
printf("%p\n", qp->whence); |
| 150 |
fflush(stdout); |
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
qp_test_dumpqp(dns_qp_t *qp) { |
| 155 |
dumpqp(qp, "qp"); |
| 156 |
fflush(stdout); |
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
qp_test_dumpmulti(dns_qpmulti_t *multi) { |
| 161 |
dns_qpreader_t qpr; |
| 162 |
dns_qpnode_t *reader = rcu_dereference(multi->reader); |
| 163 |
dns_qpmulti_t *whence = unpack_reader(&qpr, reader); |
| 164 |
dumpqp(&multi->writer, "qpmulti->writer"); |
| 165 |
printf("qpmulti->reader %p root_ref %u %u:%u base %p\n", reader, |
| 166 |
qpr.root_ref, ref_chunk(qpr.root_ref), ref_cell(qpr.root_ref), |
| 167 |
qpr.base); |
| 168 |
printf("qpmulti->reader %p whence %p\n", reader, whence); |
| 169 |
unsigned int snapshots = 0; |
| 170 |
for (dns_qpsnap_t *snap = ISC_LIST_HEAD(multi->snapshots); // |
| 171 |
snap != NULL; snap = ISC_LIST_NEXT(snap, link), snapshots++) |
| 172 |
{ |
| 173 |
} |
| 174 |
printf("qpmulti %p snapshots %u\n", multi, snapshots); |
| 175 |
fflush(stdout); |
| 176 |
} |
| 177 |
|
| 178 |
void |
| 179 |
qp_test_dumpchunks(dns_qp_t *qp) { |
| 180 |
dns_qpcell_t used = 0; |
| 181 |
dns_qpcell_t free = 0; |
| 182 |
dumpqp(qp, "qp"); |
| 183 |
for (dns_qpchunk_t c = 0; c < qp->chunk_max; c++) { |
| 184 |
printf("qp %p chunk %u base %p " |
| 185 |
"used %u free %u immutable %u discounted %u\n", |
| 186 |
qp, c, qp->base->ptr[c], qp->usage[c].used, |
| 187 |
qp->usage[c].free, qp->usage[c].immutable, |
| 188 |
qp->usage[c].discounted); |
| 189 |
used += qp->usage[c].used; |
| 190 |
free += qp->usage[c].free; |
| 191 |
} |
| 192 |
printf("qp %p total used %u free %u\n", qp, used, free); |
| 193 |
fflush(stdout); |
| 194 |
} |
| 195 |
|
| 196 |
void |
| 197 |
qp_test_dumptrie(dns_qpreadable_t qpr) { |
| 198 |
dns_qpreader_t *qp = dns_qpreader(qpr); |
| 199 |
struct { |
| 200 |
dns_qpref_t ref; |
| 201 |
dns_qpshift_t max, pos; |
| 202 |
} stack[512]; |
| 203 |
size_t sp = 0; |
| 204 |
dns_qpcell_t leaf_count = 0; |
| 205 |
|
| 206 |
/* |
| 207 |
* fake up a sentinel stack entry corresponding to the root |
| 208 |
* node; the ref is deliberately out of bounds, and pos == max |
| 209 |
* so we will immediately stop scanning it |
| 210 |
*/ |
| 211 |
stack[sp].ref = INVALID_REF; |
| 212 |
stack[sp].max = 0; |
| 213 |
stack[sp].pos = 0; |
| 214 |
|
| 215 |
dns_qpnode_t *n = get_root(qp); |
| 216 |
if (n == NULL) { |
| 217 |
printf("%p EMPTY\n", n); |
| 218 |
fflush(stdout); |
| 219 |
return; |
| 220 |
} else { |
| 221 |
printf("%p ROOT qp %p base %p\n", n, qp, qp->base); |
| 222 |
} |
| 223 |
|
| 224 |
for (;;) { |
| 225 |
if (is_branch(n)) { |
| 226 |
dns_qpref_t ref = branch_twigs_ref(n); |
| 227 |
dns_qpweight_t max = branch_twigs_size(n); |
| 228 |
dns_qpnode_t *twigs = ref_ptr(qp, ref); |
| 229 |
|
| 230 |
/* brief list of twigs */ |
| 231 |
dns_qpkey_t bits; |
| 232 |
size_t len = 0; |
| 233 |
for (dns_qpshift_t bit = SHIFT_NOBYTE; |
| 234 |
bit < SHIFT_OFFSET; bit++) |
| 235 |
{ |
| 236 |
if (branch_has_twig(n, bit)) { |
| 237 |
bits[len++] = bit; |
| 238 |
} |
| 239 |
} |
| 240 |
assert(len == max); |
| 241 |
qp_test_keytoascii(bits, len); |
| 242 |
printf("%*s%p BRANCH %p %u %u:%u %zu %s\n", (int)sp * 2, |
| 243 |
"", n, twigs, ref, ref_chunk(ref), ref_cell(ref), |
| 244 |
branch_key_offset(n), bits); |
| 245 |
|
| 246 |
++sp; |
| 247 |
stack[sp].ref = ref; |
| 248 |
stack[sp].max = max; |
| 249 |
stack[sp].pos = 0; |
| 250 |
} else { |
| 251 |
dns_qpkey_t key; |
| 252 |
qp_test_keytoascii(key, leaf_qpkey(qp, n, key)); |
| 253 |
printf("%*s%p LEAF %p %d %s\n", (int)sp * 2, "", n, |
| 254 |
leaf_pval(n), leaf_ival(n), key); |
| 255 |
leaf_count++; |
| 256 |
} |
| 257 |
|
| 258 |
while (stack[sp].pos == stack[sp].max) { |
| 259 |
if (sp == 0) { |
| 260 |
printf("LEAVES %d\n", leaf_count); |
| 261 |
fflush(stdout); |
| 262 |
return; |
| 263 |
} |
| 264 |
--sp; |
| 265 |
} |
| 266 |
|
| 267 |
stack[sp].pos++; |
| 268 |
fprintf(stderr, "pos %d/%d, ref+%d\n", stack[sp].pos, |
| 269 |
stack[sp].max, stack[sp].pos - 1); |
| 270 |
n = ref_ptr(qp, stack[sp].ref) + stack[sp].pos - 1; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
static void |
| 275 |
dumpdot_name(dns_qpnode_t *n) { |
| 276 |
if (n == NULL) { |
| 277 |
printf("empty"); |
| 278 |
} else if (is_branch(n)) { |
| 279 |
dns_qpref_t ref = branch_twigs_ref(n); |
| 280 |
printf("c%dn%d", ref_chunk(ref), ref_cell(ref)); |
| 281 |
} else { |
| 282 |
printf("v%p", leaf_pval(n)); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
static void |
| 287 |
dumpdot_twig(dns_qp_t *qp, dns_qpnode_t *n) { |
| 288 |
if (n == NULL) { |
| 289 |
printf("empty [shape=oval, label=\"\\N EMPTY\"];\n"); |
| 290 |
} else if (is_branch(n)) { |
| 291 |
dumpdot_name(n); |
| 292 |
printf(" [shape=record, label=\"{ \\N\\noff %zu | ", |
| 293 |
branch_key_offset(n)); |
| 294 |
char sep = '{'; |
| 295 |
for (dns_qpshift_t bit = SHIFT_NOBYTE; bit < SHIFT_OFFSET; |
| 296 |
bit++) |
| 297 |
{ |
| 298 |
if (branch_has_twig(n, bit)) { |
| 299 |
printf("%c <t%d> %c ", sep, |
| 300 |
branch_twig_pos(n, bit), |
| 301 |
qp_test_bittoascii(bit)); |
| 302 |
sep = '|'; |
| 303 |
} |
| 304 |
} |
| 305 |
printf("}}\"];\n"); |
| 306 |
|
| 307 |
dns_qpnode_t *twigs = branch_twigs(qp, n); |
| 308 |
dns_qpweight_t size = branch_twigs_size(n); |
| 309 |
|
| 310 |
for (dns_qpweight_t pos = 0; pos < size; pos++) { |
| 311 |
dumpdot_name(n); |
| 312 |
printf(":t%d:e -> ", pos); |
| 313 |
dumpdot_name(&twigs[pos]); |
| 314 |
printf(":w;\n"); |
| 315 |
} |
| 316 |
|
| 317 |
for (dns_qpweight_t pos = 0; pos < size; pos++) { |
| 318 |
dumpdot_twig(qp, &twigs[pos]); |
| 319 |
} |
| 320 |
|
| 321 |
} else { |
| 322 |
dns_qpkey_t key; |
| 323 |
const char *str; |
| 324 |
str = qp_test_keytoascii(key, leaf_qpkey(qp, n, key)); |
| 325 |
printf("v%p [shape=oval, label=\"\\N ival %d\\n%s\"];\n", |
| 326 |
leaf_pval(n), leaf_ival(n), str); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
void |
| 331 |
qp_test_dumpdot(dns_qp_t *qp) { |
| 332 |
REQUIRE(QP_VALID(qp)); |
| 333 |
dns_qpnode_t *n = get_root(qp); |
| 334 |
printf("strict digraph {\nrankdir = \"LR\"; ranksep = 1.0;\n"); |
| 335 |
printf("ROOT [shape=point]; ROOT -> "); |
| 336 |
dumpdot_name(n); |
| 337 |
printf(":w;\n"); |
| 338 |
dumpdot_twig(qp, n); |
| 339 |
printf("}\n"); |
| 340 |
} |
| 341 |
|
| 342 |
void |
| 343 |
qp_test_printkey(const dns_qpkey_t key, size_t keylen) { |
| 344 |
dns_fixedname_t fn; |
| 345 |
dns_name_t *n = dns_fixedname_initname(&fn); |
| 346 |
char txt[DNS_NAME_FORMATSIZE]; |
| 347 |
|
| 348 |
dns_qpkey_toname(key, keylen, n); |
| 349 |
dns_name_format(n, txt, sizeof(txt)); |
| 350 |
printf("%s%s\n", txt, dns_name_isabsolute(n) ? "." : ""); |
| 351 |
} |
| 352 |
|
| 353 |
/**********************************************************************/ |