unbound: services/cache/rrset.c File Reference

unbound 0.1

Macros | Functions
rrset.c File Reference

This file contains the rrset cache. More...

`#include "config.h"`
`#include "services/cache/rrset.h"`
`#include "sldns/rrdef.h"`
`#include "util/storage/slabhash.h"`
`#include "util/config_file.h"`
`#include "util/data/packed_rrset.h"`
`#include "util/data/msgreply.h"`
`#include "util/data/msgparse.h"`
`#include "util/data/dname.h"`
`#include "util/regional.h"`
`#include "util/alloc.h"`
`#include "util/net_help.h"`

## Macros
#define | DNAME_TTL0_GRACE_SECONDS 1
| Grace period in seconds for TTL=0 DNAME rrsets (RFC 2308: do not cache).

## Functions
void | rrset_markdel (void *key)
| mark rrset to be deleted, set id=0
struct rrset_cache * | rrset_cache_create (struct config_file *cfg, struct alloc_cache *alloc)
| Create rrset cache.
void | rrset_cache_delete (struct rrset_cache *r)
| Delete rrset cache.
struct rrset_cache * | rrset_cache_adjust (struct rrset_cache *r, struct config_file *cfg, struct alloc_cache *alloc)
| Adjust settings of the cache to settings from the config file.
void | rrset_cache_touch (struct rrset_cache *r, struct ub_packed_rrset_key *key, hashvalue_type hash, rrset_id_type id)
| Touch rrset, with given pointer and id.
static int | need_to_update_rrset (void *nd, void *cd, time_t timenow, int equal, int ns)
| see if rrset needs to be updated in the cache
static void | rrset_update_id (struct rrset_ref *ref, struct alloc_cache *alloc)
| Update RRSet special key ID.
int | rrset_cache_update (struct rrset_cache *r, struct rrset_ref *ref, struct alloc_cache *alloc, time_t timenow)
| Update an rrset in the rrset cache.
void | rrset_cache_update_wildcard (struct rrset_cache *rrset_cache, struct ub_packed_rrset_key *rrset, uint8_t *ce, size_t ce_len, struct alloc_cache *alloc, time_t timenow)
| Update or add an rrset in the rrset cache using a wildcard dname.
struct ub_packed_rrset_key * | rrset_cache_lookup (struct rrset_cache *r, uint8_t *qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, uint32_t flags, time_t timenow, int wr)
| Lookup rrset.
int | rrset_array_lock (struct rrset_ref *ref, size_t count, time_t timenow)
| Obtain readlock on a (sorted) list of rrset references.
void | rrset_array_unlock (struct rrset_ref *ref, size_t count)
| Unlock array (sorted) of rrset references.
void | rrset_array_unlock_touch (struct rrset_cache *r, struct regional *scratch, struct rrset_ref *ref, size_t count)
| Unlock array (sorted) of rrset references and at the same time touch LRU on the rrsets.
void | rrset_update_sec_status (struct rrset_cache *r, struct ub_packed_rrset_key *rrset, time_t now)
| Update security status of an rrset.
void | rrset_check_sec_status (struct rrset_cache *r, struct ub_packed_rrset_key *rrset, time_t now)
| Looks up security status of an rrset.
void | rrset_cache_remove_above (struct rrset_cache *r, uint8_t **qname, size_t *qnamelen, uint16_t searchtype, uint16_t qclass, time_t now, uint8_t *qnametop, size_t qnametoplen)
| Removes rrsets above the qname, returns upper qname.
int | rrset_cache_expired_above (struct rrset_cache *r, uint8_t **qname, size_t *qnamelen, uint16_t searchtype, uint16_t qclass, time_t now, uint8_t *qnametop, size_t qnametoplen)
| Sees if an rrset is expired above the qname, returns upper qname.
void | rrset_cache_remove (struct rrset_cache *r, uint8_t *nm, size_t nmlen, uint16_t type, uint16_t dclass, uint32_t flags)
| Remove an rrset from the cache, by name and type and flags.

## Detailed Description

This file contains the rrset cache.

## Macro Definition Documentation

## ◆ DNAME_TTL0_GRACE_SECONDS

#define DNAME_TTL0_GRACE_SECONDS 1

Grace period in seconds for TTL=0 DNAME rrsets (RFC 2308: do not cache).

Allows synthesis from cache within this window to reduce recursion load.

Referenced by rrset_cache_lookup().

## Function Documentation

## ◆ rrset_cache_create()

struct rrset_cache * rrset_cache_create | ( | struct config_file * | cfg,
|  | struct alloc_cache * | alloc )

Create rrset cache.

Parameters

  cfg | config settings or NULL for defaults.
  alloc | initial default rrset key allocation.

Returns
  : NULL on error.

References HASH_DEFAULT_MAXMEM, HASH_DEFAULT_SLABS, HASH_DEFAULT_STARTARRAY, config_file::rrset_cache_size, config_file::rrset_cache_slabs, rrset_markdel(), slabhash_create(), slabhash_setmarkdel(), rrset_cache::table, ub_rrset_compare(), ub_rrset_key_delete(), and ub_rrset_sizefunc().

Referenced by rrset_cache_adjust().

## ◆ rrset_cache_delete()

void rrset_cache_delete | ( | struct rrset_cache * | r | ) |

Delete rrset cache.

Parameters

  r | rrset cache to delete.

References slabhash_delete(), and rrset_cache::table.

Referenced by daemon_delete(), rrset_cache_adjust(), and ub_ctx_delete().

## ◆ rrset_cache_adjust()

struct rrset_cache * rrset_cache_adjust | ( | struct rrset_cache * | r,
|  | struct config_file * | cfg,
|  | struct alloc_cache * | alloc )

Adjust settings of the cache to settings from the config file.

May purge the cache. May recreate the cache. There may be no threading or use by other threads.

Parameters

  r | rrset cache to adjust (like realloc).
  cfg | config settings or NULL for defaults.
  alloc | initial default rrset key allocation.

Returns
  0 on error, or new rrset cache pointer on success.

References rrset_cache_create(), rrset_cache_delete(), config_file::rrset_cache_size, config_file::rrset_cache_slabs, slabhash_is_size(), and rrset_cache::table.

Referenced by context_finalize(), and daemon_apply_cfg().

## ◆ rrset_cache_touch()

void rrset_cache_touch | ( | struct rrset_cache * | r,
|  | struct ub_packed_rrset_key * | key,
|  | hashvalue_type | hash,
|  | rrset_id_type | id )

Touch rrset, with given pointer and id.

Caller may not hold a lock on ANY rrset, this could give deadlock.

This routine is faster than a hashtable lookup: o no bin_lock is acquired. o no walk through the bin-overflow-list. o no comparison of the entry key to find it.

Parameters

  r | rrset cache.
  key | rrset key. Marked recently used (if it was not deleted before the lock is acquired, in that case nothing happens).
  hash | hash value of the item. Please read it from the key when you have it locked. Used to find slab from slabhash.
  id | used to check that the item is unchanged and not deleted.

References ub_packed_rrset_key::entry, lruhash_entry::hash, ub_packed_rrset_key::id, lruhash::lock, lruhash_entry::lock, lru_touch(), slabhash_gettable(), and rrset_cache::table.

Referenced by rrset_array_unlock_touch().

## ◆ rrset_cache_update()

int rrset_cache_update | ( | struct rrset_cache * | r,
|  | struct rrset_ref * | ref,
|  | struct alloc_cache * | alloc,
|  | time_t | timenow )

Update an rrset in the rrset cache.

Stores the information for later use. Will lookup if the rrset is in the cache and perform an update if necessary. If the item was present, and superior, references are returned to that. The passed item is then deallocated with rrset_parsedelete.

A superior rrset is: o rrset with better trust value. o same trust value, different rdata, newly passed rrset is inserted. If rdata is the same, TTL in the cache is updated.

Parameters

  r | the rrset cache.
  ref | reference (ptr and id) to the rrset. Pass reference setup for the new rrset. The reference may be changed if the cached rrset is superior. Before calling the rrset is presumed newly allocated and changeable. After calling you do not hold a lock, and the rrset is inserted in the hashtable so you need a lock to change it.
  alloc | how to allocate (and deallocate) the special rrset key.
  timenow | current time (to see if ttl in cache is expired).

Returns
  : true if the passed reference is updated, false if it is unchanged. 0: reference unchanged, inserted in cache. 1: reference updated, item is inserted in cache. 2: reference updated, item in cache is considered superior. also the rdata is equal (but other parameters in cache are superior).

References lruhash_entry::data, packed_rrset_key::dname, ub_packed_rrset_key::entry, lruhash_entry::hash, rrset_ref::id, ub_packed_rrset_key::id, lruhash_entry::key, rrset_ref::key, LDNS_RR_TYPE_DNAME, LDNS_RR_TYPE_NS, lruhash_entry::lock, log_assert, need_to_update_rrset(), ub_packed_rrset_key::rk, rrset_update_id(), rrsetdata_equal(), slabhash_insert(), slabhash_lookup(), rrset_cache::table, packed_rrset_key::type, and ub_packed_rrset_parsedelete().

Referenced by dns_cache_store(), iter_store_parentside_rrset(), move_into_cache(), rrset_cache_update_wildcard(), store_rrset(), and store_rrsets().

## ◆ rrset_cache_update_wildcard()

void rrset_cache_update_wildcard | ( | struct rrset_cache * | rrset_cache,
|  | struct ub_packed_rrset_key * | rrset,
|  | uint8_t * | ce,
|  | size_t | ce_len,
|  | struct alloc_cache * | alloc,
|  | time_t | timenow )

Update or add an rrset in the rrset cache using a wildcard dname.

Generates wildcard dname by prepending the wildcard label to the closest encloser. Will lookup if the rrset is in the cache and perform an update if necessary.

Parameters

  rrset_cache | the rrset cache.
  rrset | which rrset to cache as wildcard. This rrset is left untouched.
  ce | the closest encloser, will be uses to generate the wildcard dname.
  ce_len | the closest encloser length.
  alloc | how to allocate (and deallocate) the special rrset key.
  timenow | current time (to see if ttl in cache is expired).

References alloc_special_release(), packed_rrset_key::dname, packed_rrset_key::dname_len, ub_packed_rrset_key::entry, lruhash_entry::hash, rrset_ref::id, ub_packed_rrset_key::id, rrset_ref::key, LDNS_MAX_DOMAINLEN, log_err(), memdup(), packed_rrset_copy_alloc(), ub_packed_rrset_key::rk, rrset_cache_update(), and rrset_key_hash().

Referenced by validate_positive_response().

## ◆ rrset_cache_lookup()

struct ub_packed_rrset_key * rrset_cache_lookup | ( | struct rrset_cache * | r,
|  | uint8_t * | qname,
|  | size_t | qnamelen,
|  | uint16_t | qtype,
|  | uint16_t | qclass,
|  | uint32_t | flags,
|  | time_t | timenow,
|  | int | wr )

Lookup rrset.

You obtain read/write lock. You must unlock before lookup anything of else.

Parameters

  r | the rrset cache.
  qname | name of rrset to lookup.
  qnamelen | length of name of rrset to lookup.
  qtype | type of rrset to lookup (host order).
  qclass | class of rrset to lookup (host order).
  flags | rrset flags, or 0.
  timenow | used to compare with TTL.
  wr | set true to get writelock.

Returns
  packed rrset key pointer. Remember to unlock the key.entry.lock. or NULL if could not be found or it was timed out.

References lruhash_entry::data, packed_rrset_key::dname, packed_rrset_key::dname_len, DNAME_TTL0_GRACE_SECONDS, ub_packed_rrset_key::entry, packed_rrset_key::flags, lruhash_entry::hash, lruhash_entry::key, LDNS_RR_TYPE_DNAME, lruhash_entry::lock, PACKED_RRSET_UPSTREAM_0TTL, ub_packed_rrset_key::rk, packed_rrset_key::rrset_class, rrset_key_hash(), slabhash_lookup(), rrset_cache::table, packed_rrset_data::ttl, packed_rrset_data::ttl_add, TTL_IS_EXPIRED, and packed_rrset_key::type.

Referenced by add_soa(), cache_fill_missing(), dns_cache_lookup(), fill_any(), find_add_addrs(), find_add_ds(), find_closest_of_type(), grab_nsec(), iter_lookup_parent_glue_from_cache(), iter_lookup_parent_NS_from_cache(), load_ref(), rrset_cache_expired_above(), rrset_cache_remove_above(), val_find_DS(), and val_neg_getmsg().

## ◆ rrset_array_lock()

int rrset_array_lock | ( | struct rrset_ref * | ref,
|  | size_t | count,
|  | time_t | timenow )

Obtain readlock on a (sorted) list of rrset references.

Checks TTLs and IDs of the rrsets and rollbacks locking if not Ok.

Parameters

  ref | array of rrset references (key pointer and ID value). duplicate references are allowed and handled.
  count | size of array.
  timenow | used to compare with TTL.

Returns
  true on success, false on a failure, which can be that some RRsets have timed out, or that they do not exist any more, the RRsets have been purged from the cache. If true, you hold readlocks on all the ref items.

References packed_rrset_data::count, lruhash_entry::data, ub_packed_rrset_key::entry, rrset_ref::id, rrset_ref::key, rrset_array_unlock(), packed_rrset_data::ttl, and TTL_IS_EXPIRED.

Referenced by answer_from_cache(), dump_msg(), invalidateQueryInCache(), and tomsg().

## ◆ rrset_array_unlock()

void rrset_array_unlock | ( | struct rrset_ref * | ref,
|  | size_t | count )

Unlock array (sorted) of rrset references.

Parameters

  ref | array of rrset references (key pointer and ID value). duplicate references are allowed and handled.
  count | size of array.

References packed_rrset_data::count.

Referenced by dump_msg(), invalidateQueryInCache(), rrset_array_lock(), and tomsg().

## ◆ rrset_array_unlock_touch()

void rrset_array_unlock_touch | ( | struct rrset_cache * | r,
|  | struct regional * | scratch,
|  | struct rrset_ref * | ref,
|  | size_t | count )

Unlock array (sorted) of rrset references and at the same time touch LRU on the rrsets.

It needs the scratch region for temporary storage as it uses the initial locks to obtain hash values.

Parameters

  r | the rrset cache. In this cache LRU is updated.
  scratch | region for temporary storage of hash values. if memory allocation fails, the lru touch fails silently, but locks are released. memory errors are logged.
  ref | array of rrset references (key pointer and ID value). duplicate references are allowed and handled.
  count | size of array.

References packed_rrset_data::count, log_warn(), regional_alloc(), RR_COUNT_MAX, and rrset_cache_touch().

Referenced by answer_from_cache(), and tomsg().

## ◆ rrset_update_sec_status()

void rrset_update_sec_status | ( | struct rrset_cache * | r,
|  | struct ub_packed_rrset_key * | rrset,
|  | time_t | now )

Update security status of an rrset.

Looks up the rrset. If found, checks if rdata is equal. If so, it will update the security, trust and rrset-ttl values. The values are only updated if security is increased (towards secure).

Parameters

  r | the rrset cache.
  rrset | which rrset to attempt to update. This rrset is left untouched. The rrset in the cache is updated in-place.
  now | current time.

References packed_rrset_data::count, lruhash_entry::data, ub_packed_rrset_key::entry, lruhash_entry::hash, LDNS_RR_TYPE_NS, lruhash_entry::lock, ub_packed_rrset_key::rk, packed_rrset_data::rr_ttl, rrset_key_hash(), rrsetdata_equal(), packed_rrset_data::rrsig_count, sec_status_bogus, packed_rrset_data::security, slabhash_lookup(), rrset_cache::table, packed_rrset_data::trust, packed_rrset_data::ttl, packed_rrset_data::ttl_add, and packed_rrset_key::type.

Referenced by list_is_secure(), nsec_verify_rrset(), val_mark_indeterminate(), and val_mark_insecure().

## ◆ rrset_check_sec_status()

void rrset_check_sec_status | ( | struct rrset_cache * | r,
|  | struct ub_packed_rrset_key * | rrset,
|  | time_t | now )

Looks up security status of an rrset.

Looks up the rrset. If found, checks if rdata is equal, and entry did not expire. If so, it will update the security, trust and rrset-ttl values.

Parameters

  r | the rrset cache.
  rrset | This rrset may change security status due to the cache. But its status will only improve, towards secure.
  now | current time.

References packed_rrset_data::count, lruhash_entry::data, ub_packed_rrset_key::entry, lruhash_entry::hash, lruhash_entry::lock, ub_packed_rrset_key::rk, packed_rrset_data::rr_ttl, rrset_key_hash(), rrsetdata_equal(), packed_rrset_data::rrsig_count, sec_status_bogus, packed_rrset_data::security, slabhash_lookup(), rrset_cache::table, packed_rrset_data::trust, and packed_rrset_data::ttl.

Referenced by list_is_secure(), and nsec_verify_rrset().

## ◆ rrset_cache_remove_above()

void rrset_cache_remove_above | ( | struct rrset_cache * | r,
|  | uint8_t ** | qname,
|  | size_t * | qnamelen,
|  | uint16_t | searchtype,
|  | uint16_t | qclass,
|  | time_t | now,
|  | uint8_t * | qnametop,
|  | size_t | qnametoplen )

Removes rrsets above the qname, returns upper qname.

Parameters

  r | the rrset cache.
  qname | the start qname, also used as the output.
  qnamelen | length of qname, updated when it returns.
  searchtype | qtype to search for.
  qclass | qclass to search for.
  now | current time.
  qnametop | the top qname to stop removal (it is not removed).
  qnametoplen | length of qnametop.

References lruhash_entry::data, ub_packed_rrset_key::entry, lruhash_entry::lock, log_nametypeclass(), query_dname_compare(), rrset_cache_lookup(), rrset_cache_remove(), packed_rrset_data::ttl, VERB_ALGO, and verbosity.

Referenced by processQueryResponse().

## ◆ rrset_cache_expired_above()

int rrset_cache_expired_above | ( | struct rrset_cache * | r,
|  | uint8_t ** | qname,
|  | size_t * | qnamelen,
|  | uint16_t | searchtype,
|  | uint16_t | qclass,
|  | time_t | now,
|  | uint8_t * | qnametop,
|  | size_t | qnametoplen )

Sees if an rrset is expired above the qname, returns upper qname.

Parameters

  r | the rrset cache.
  qname | the start qname, also used as the output.
  qnamelen | length of qname, updated when it returns.
  searchtype | qtype to search for.
  qclass | qclass to search for.
  now | current time.
  qnametop | the top qname, don't look farther than that.
  qnametoplen | length of qnametop.

Returns
  true if there is an expired rrset above, false otherwise.

References lruhash_entry::data, ub_packed_rrset_key::entry, lruhash_entry::lock, log_nametypeclass(), query_dname_compare(), rrset_cache_lookup(), packed_rrset_data::ttl, TTL_IS_EXPIRED, and VERB_ALGO.

Referenced by find_closest_of_type().

## ◆ rrset_cache_remove()

void rrset_cache_remove | ( | struct rrset_cache * | r,
|  | uint8_t * | nm,
|  | size_t | nmlen,
|  | uint16_t | type,
|  | uint16_t | dclass,
|  | uint32_t | flags )

Remove an rrset from the cache, by name and type and flags.

Parameters

  r | rrset cache
  nm | name of rrset
  nmlen | length of name
  type | type of rrset
  dclass | class of rrset, host order
  flags | flags of rrset, host order

References packed_rrset_key::dname, packed_rrset_key::dname_len, ub_packed_rrset_key::entry, packed_rrset_key::flags, lruhash_entry::hash, lruhash_entry::key, ub_packed_rrset_key::rk, packed_rrset_key::rrset_class, rrset_key_hash(), slabhash_remove(), rrset_cache::table, and packed_rrset_key::type.

Referenced by dns64_adjust_a(), do_cache_remove(), probe_anchor(), and rrset_cache_remove_above().