dns.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. Kamailio and DNS Overview
  2. ---------------------------
  3. The DNS subsystem in Kamailio can either directly use libresolv and a combination
  4. of the locally configured DNS server, /etc/hosts and the local Network
  5. Information Service (NIS/YP a.s.o) or cache the query results (both positive
  6. and negative) and look first in its internal cache.
  7. When its internal DNS cache is enabled, Kamailio can also use DNS failover: if
  8. one destination resolves to multiple addresses Kamailio can try all of them until
  9. it finds one to which it can successfully send the packet or it exhausts all
  10. of them. Kamailio (The tm module to be more precise) uses the DNS failover also
  11. when the destination host doesn't send any reply to a forwarded invite within the
  12. SIP timeout interval (whose value can be configured using the tm fr_timer
  13. parameter).
  14. When SRV based load balancing is enabled Kamailio can even do DNS based load
  15. balancing (see RFC2782 and the dns_srv_lb option below). This is using the
  16. weight value in the DNS SRV record.
  17. DNS Cache and Failover Drawbacks
  18. --------------------------------
  19. Using the DNS cache and the DNS failover has also some drawbacks:
  20. 1. Only the locally configured DNS server (usually in /etc/resolv.conf) is
  21. used for the requests (/etc/hosts and the local Network Information Service
  22. are ignored).
  23. Workaround: disable the DNS cache (use_dns_cache=off or compile without -DUSE_DNS_CACHE).
  24. 2. The DNS cache uses extra memory
  25. Workaround: disable the DNS cache.
  26. 3. The DNS failover introduces a very small performance penalty
  27. Workaround: disable the DNS failover (use_dns_failover=off).
  28. 4. The DNS failover increases the memory usage (the internal structures
  29. used to represent the transaction are bigger when the DNS failover support is
  30. compiled).
  31. Workaround: compile without DNS failover support (-DUSE_DNS_FAILOVER).
  32. Turning it off from the config file is not enough in this case (the extra
  33. memory will still be used).
  34. On the other hand using the DNS cache saves lots of DNS queries and makes
  35. DNS based failover and DNS based load balancing possible. If the destination
  36. blacklist is enabled, Kamailio can do failover even if forwarding in stateless
  37. mode.
  38. In the ideal case with the DNS cache enabled Kamailio will do only one query for
  39. a NAPTR (if enabled) or SRV lookup and then it will use the results for the
  40. record's TTL (for example if all the resulting records have 1 minute TTL,
  41. the server won't make another query for this domain for 1 minute). Even negative
  42. answers will be cached.
  43. Without the DNS cache, each NAPTR or SRV lookup will result in at least 2
  44. queries. These queries will happen every time, for each message (even if
  45. all of them go to the same domain).
  46. DNS Resolver Options
  47. --------------------
  48. The DNS resolver options control how Kamailio will interact with the external
  49. DNS servers. These options (with the dns_try_ipv6 exception) are passed to
  50. libresolv and are used each time a DNS request is made.
  51. The default values are system specific and generally depend on the
  52. /etc/resolv.conf content. For servers doing a lot of DNS requests it is
  53. highly recommended to change the default values in the Kamailio config file
  54. (even if using Kamailio's internal DNS cache).
  55. dns_try_ipv6 = on | off - if on and Kamailio listens on at least one ipv6 socket,
  56. ipv6 (AAAA) lookups will be performed if the ipv4 (A) lookups fail.
  57. If off only ipv4 (A) lookups will be used.
  58. Default: on if Kamailio is compiled with ipv6 support.
  59. dns_try_naptr = on | off - if on Kamailio will first try a NAPTR lookup for
  60. destinations that don't have the protocol or port specified and
  61. are not simple ip addresses (as described in RFC 3263). This will
  62. introduce a slight performance penalty and will probably cause extra
  63. DNS lookups. For example a lookup for a non-existing domain will
  64. produce one extra query: NAPTR(domain), SRV(_sip._udp.domain)
  65. and A/AAAA(domain).
  66. If the result of a query contains several NAPTR records, Kamailio will select
  67. among them according to the RFC2915 and Kamailio preference towards a
  68. specific protocol (see dns_udp_pref, dns_tcp_pref and dns_tls_pref
  69. below). For an RFC3263 compliant configuration (choose the remote side
  70. preferred protocol if supported), set dns_udp_pref, dns_tcp_pref and
  71. dns_tls_pref to the same value (>=0), e.g. 0.
  72. Default: off
  73. dns_udp_pref = number - udp protocol preference when doing NAPTR lookups.
  74. This option works together with dns_tcp_pref, dns_tls_pref and
  75. dns_sctp_pref. If all this options have the same positive value and more
  76. NAPTR records are available, Kamailio will select the NAPTR record preferred
  77. by the remote side (according to RFC2915). If the values are positive
  78. but different, Kamailio will select the NAPTR record whose protocol it
  79. prefers the most (the protocol with the highest dns_<proto>_pref
  80. number). If there are several NAPTR records with the same preferred
  81. protocol, Kamailio will select among them based on their order and preference
  82. (see RFC2915).
  83. To completely disable selecting a specific protocol, use a negative
  84. number. For example dns_tcp_pref=-1 will completely disable selection
  85. of tcp NAPTR records, even if this will result in the NAPTR lookup
  86. failure. Note: if a protocol is disabled in Kamailio (e.g. tls_disable=1)
  87. the corresponding NAPTR records selection will be also disabled,
  88. irrespective of the dns_<proto>_preference value.
  89. Default: dns_udp_pref=30, dns_tcp_pref=20, dns_tls_pref=10 and
  90. dns_sctp_pref=20.
  91. (prefer udp, but if no udp NAPTR record found or no SRV-resolvable
  92. udp NAPTR record found use tcp or sctp records and if this fails too
  93. use tls)
  94. dns_tcp_pref = number (see dns_udp_pref above)
  95. dns_tls_pref = number (see dns_udp_pref above)
  96. dns_sctp_pref = number (see dns_udp_pref above)
  97. dns_retr_time = time - time in s before retrying a DNS request.
  98. Default: system specific, depends also on the/etc/resolv.conf content
  99. (usually 5 s).
  100. dns_retr_no = no. - number of DNS retransmissions before giving up.
  101. Default: see above (usually 4)
  102. dns_servers_no = no. - how many DNS servers from the ones defined in
  103. /etc/resolv.conf will be used. Default: all of them.
  104. dns_use_search_list= yes/no - if no, the search list in /etc/resolv.conf
  105. will be ignored (=> fewer lookups => gives up faster).
  106. Default: yes.
  107. HINT: even if you don't have a search list defined, setting this option
  108. to "no" will still be "faster", because an empty search list is in
  109. fact search "" (so even if the search list is empty/missing there will
  110. still be 2 DNS queries, eg. foo+'.' and foo+""+'.')
  111. dns_search_full_match = yes/no - controls the check of the name part
  112. which is found in the answer expanding the searched name before
  113. the answer is treated as correct and "link" (fake CNAME record)
  114. between the short name (query) and long name (answer) is created
  115. which is then stored in dns_cache and reused for next queries.
  116. If set to no - no additional check is done.
  117. If set to yes - the additional part is checked against the search list.
  118. The maximum time a DNS request can take (before failing) is:
  119. (dns_retr_time*dns_retr_no) * (search_list_domains) If dns_try_ipv6 is yes,
  120. mutliply it again by 2.
  121. The option combination that produces the "fastest" DNS resolver config
  122. (the "faster" in the sense that it gives up the quickest) is:
  123. dns_try_ipv6=no
  124. dns_retr_time=1
  125. dns_retr_no=1
  126. dns_servers_no=1
  127. dns_use_search_list=no
  128. The recommended DNS configuration is to have a "close" DNS caching recursive
  129. server configured in /etc/resolv.conf, set the DNS resolver options in Kamailio's
  130. config as in the above example and enable the DNS cache (in Kamailio).
  131. Pay particular attention to dns_servers_no and dns_use_search_list. It's a
  132. good idea to make sure you don't need / use the search list or more then one
  133. DNS server (to avoid unnecessary extra lookups).
  134. DNS Resolver Compile Options
  135. ----------------------------
  136. USE_NAPTR - if defined the naptr lookup support will be compiled in.
  137. NAPTR support still has to be enabled from Kamailio's config file (it's
  138. off by default).
  139. RESOLVE_DBG - if defined, the resolver will be very verbose: it will log
  140. a lot of debugging information at L_DBG level.
  141. NAPTR_DBG - if defined the NAPTR related resolver functions will be very
  142. verbose.
  143. DNS Cache and Failover Config Variables
  144. ---------------------------------------
  145. use_dns_cache = on | off - if off the DNS cache won't be used (all dns
  146. lookups will result into a DNS request). When on all the DNS request
  147. results will be cached.
  148. WARNING: when enabled /etc/hosts will be completely bypassed, all the dns
  149. request will go directly to the system configured (resolv.conf) dns
  150. server.
  151. Default: on.
  152. use_dns_failover = on |off - if on and sending a request fails (due to not
  153. being allowed from an onsend_route, send failure, blacklisted destination
  154. or, when using tm, invite timeout), and the destination resolves to
  155. multiple ip addresses and/or multiple SRV records, the send will be
  156. re-tried using the next ip/record. In tm's case a new branch will be
  157. created for each new send attempt.
  158. Default: off.
  159. Depends on use_dns_cache being on. If tm is used along with DNS failover is
  160. recommended to also turn on dst_blacklist.
  161. dns_srv_lb = on | off or
  162. dns_srv_loadbalancing = on | off - if on instead of doing simple DNS
  163. failover (like above), Kamailio will load balance requests to different srv
  164. records of the same priority based on the SRV records weights (like
  165. described in RFC2782). For a destination which has different priorities
  166. for all its SRV records, this option will be equivalent with simple
  167. DNS failover.
  168. Note: this option requires having DNS failover enabled (see
  169. use_dns_failover above).
  170. Default: off.
  171. dns_try_ipv6 = on | off - shared with the resolver (see resolver
  172. description).
  173. dns_try_naptr = on | off - shared with the resolver (see resolver
  174. description).
  175. dns_udp_pref = number - shared with the resolver (see resolver
  176. description).
  177. dns_tcp_pref = number - shared with the resolver (see resolver
  178. description).
  179. dns_tls_pref = number - shared with the resolver (see resolver
  180. description).
  181. dns_cache_flags = DNS cache specific resolver flags, used for overriding
  182. the default behaviour (low level).
  183. Possible values:
  184. 1 - ipv4 only: only DNS A requests are performed, even if Kamailio listens
  185. also on ipv6 addresses.
  186. 2 - ipv6 only: only DNS AAAA requests are performed. Ignored if
  187. dns_try_ipv6 is off or Kamailio doesn't listen on any ipv6
  188. address.
  189. 4 - prefer ipv6: try first to resolve a host name to an ipv6 address
  190. (DNS AAAA request) and only if this fails try an ipv4
  191. address (DNS A request).
  192. By default the ipv4 addresses are preferred.
  193. Default: 0
  194. dns_cache_negative_ttl = time to live for negative results ("not found") in
  195. seconds. Use 0 to disable.
  196. Default: 60 s.
  197. dns_cache_min_ttl = minimum accepted time to live for a record, in seconds.
  198. If a record has a lower ttl, its value will be discarded and
  199. dns_cache_min_ttl will be used instead.
  200. Default: 0
  201. dns_cache_max_ttl = maximum accepted time to live for a record, in seconds.
  202. If a record has a higher ttl, its value will be discarded and
  203. dns_cache_max_ttl will be used instead.
  204. Default: MAXINT
  205. dns_cache_mem = maximum memory used for the DNS cache in Kb.
  206. Default: 500 Kb
  207. dns_cache_gc_interval = how often (in s) the DNS cache will be garbage
  208. collected.
  209. Default: 120 s.
  210. dns_cache_del_nonexp = yes | no or
  211. dns_cache_delete_nonexpired = yes | no - allow deletion of non-expired
  212. records from the cache when there is no more space left for new
  213. ones. The last-recently used entries are deleted first.
  214. Default: no
  215. dns_cache_init = on | off - if off, the DNS cache is not initialized
  216. at startup and cannot be enabled runtime, that saves some memory.
  217. Default: on
  218. DNS Cache Compile Options
  219. -------------------------
  220. USE_DNS_CACHE - if defined the DNS cache support will be compiled in
  221. (default). If not needed/wanted the dns_cache can be disabled from the
  222. Kamailio's config file. The only advantages for not compiling the DNS cache
  223. support is a slight decrease of the executable size and an extremely
  224. small performance increase (1 less comparison per DNS request).
  225. USE_DNS_FAILOVER - if defined the DNS failover support will be compiled in.
  226. (default). Compiling the DNS failover support has a few disadvantages,
  227. see the "Drawbacks" section.
  228. DNS_SRV_LB - if defined (default) support for load balancing using
  229. SRV records weights (as described in RFC2782) will be compiled in.
  230. Note however that it still must be enabled from the Kamailio config, it's
  231. disabled by default (see the dns_srv_lb config option).
  232. USE_NAPTR - (shared with the resolver) if defined NAPTR support will
  233. be compiled in (default). Note that even if compiled, NAPTR support
  234. must be enabled also from the Kamailio config (see the dns_try_naptr option).
  235. NAPTR_CACHE_ALL_ARS - if defined all the additional records in a NAPTR
  236. answer will be cached. Normally Kamailio would cache only "related" records
  237. (records that are directly referred), but for answers with lots of
  238. A/AAAA records it might happen that not all of the SRV records will fit
  239. in the AR section. In this case, without this compile option Kamailio will
  240. not cache the un-referred A/AAAA records. BY default this option is
  241. disabled.
  242. CACHE_RELEVANT_RECS_ONLY - if defined (default), records in the AR section
  243. of an answer will be cached only if they are "related" to the query.
  244. For example if the query is for a SRV record, A & AAAA records in the
  245. AR section will be cached only if there are SRV records pointing to
  246. them. This avoids adding possible garbage to the cache.
  247. If this option is not defined (experimental), everything in the AR
  248. section will be added to the cache.
  249. DNS_CACHE_DEBUG - if defined the DNS cache will be very verbose (it will
  250. log lots of messages at the L_DBG levell).
  251. Note: To remove a compile options, edit Kamailio's Makefile.defs and remove it
  252. from DEFS list. To add a compile options add it to the make command line,
  253. e.g.: make proper; make all extra_defs=-DUSE_DNS_FAILOVER
  254. or for a permanent solution, edit Makefile.defs and add it to DEFS
  255. (don't foget to prefix it with -D). Some options require editing
  256. dns_cache.c or resolve.[ch] (just grep after them).