FD.io VPP  v19.08.2-294-g37e99c22d
Vector Packet Processing
resolver_process.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <dns/dns.h>
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19 
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 
23 /* define message IDs */
24 #include <dns/dns_msg_enum.h>
25 
26 #define vl_typedefs /* define message structures */
27 #include <dns/dns_all_api_h.h>
28 #undef vl_typedefs
29 
30 #define vl_endianfun /* define message structures */
31 #include <dns/dns_all_api_h.h>
32 #undef vl_endianfun
33 
34 /* instantiate all the print functions we know about */
35 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
36 #define vl_printfun
37 #include <dns/dns_all_api_h.h>
38 #undef vl_printfun
39 
41 
42 int
43 vnet_dns_response_to_reply (u8 * response,
45  u32 * min_ttlp);
46 int
47 vnet_dns_response_to_name (u8 * response,
49  u32 * min_ttlp);
50 
51 static void
52 resolve_event (dns_main_t * dm, f64 now, u8 * reply)
53 {
54  vlib_main_t *vm = dm->vlib_main;
56  dns_header_t *d;
57  u32 pool_index;
59  u32 min_ttl;
60  u16 flags;
61  u16 rcode;
62  int i;
63  int entry_was_valid;
64  int remove_count;
65  int rv = 0;
66 
67  d = (dns_header_t *) reply;
68  flags = clib_net_to_host_u16 (d->flags);
69  rcode = flags & DNS_RCODE_MASK;
70 
71  /* $$$ u16 limits cache to 65K entries, fix later multiple dst ports */
72  pool_index = clib_net_to_host_u16 (d->id);
73  dns_cache_lock (dm, 10);
74 
75  if (pool_is_free_index (dm->entries, pool_index))
76  {
77  vec_free (reply);
78  if (0)
79  clib_warning ("pool index %d is free", pool_index);
81  DNS46_REPLY_ERROR_NO_ELT, 1);
82  dns_cache_unlock (dm);
83  return;
84  }
85 
86  ep = pool_elt_at_index (dm->entries, pool_index);
87 
88  if (ep->dns_response)
89  vec_free (ep->dns_response);
90 
91  /* Handle [sic] recursion AKA CNAME indirection */
92  rv = vnet_dns_cname_indirection_nolock (dm, pool_index, reply);
93 
94  /* CNAME found, further resolution pending, we're done here */
95  if (rv > 0)
96  {
97  dns_cache_unlock (dm);
98  return;
99  }
100  /* Server backfire: refused to answer, or sent zero replies */
101  if (rv < 0)
102  {
103  /* Try a different server */
104  if (ep->server_af /* ip6 */ )
105  {
106  if (0)
107  clib_warning ("Server %U failed to resolve '%s'",
109  dm->ip6_name_servers + ep->server_rotor, ep->name);
110  /* Any more servers to try? */
111  if (ep->server_fails > 1 || vec_len (dm->ip6_name_servers) <= 1)
112  {
113  /* No, tell the client to go away */
114  goto reply;
115  }
116  ep->retry_count = 0;
117  ep->server_rotor++;
118  ep->server_fails++;
119  if (ep->server_rotor >= vec_len (dm->ip6_name_servers))
120  ep->server_rotor = 0;
121  if (0)
122  clib_warning ("Try server %U", format_ip6_address,
123  dm->ip6_name_servers + ep->server_rotor);
125  (dm, ep, dm->ip6_name_servers + ep->server_rotor);
126  }
127  else
128  {
129  if (0)
130  clib_warning ("Server %U failed to resolve '%s'",
132  dm->ip4_name_servers + ep->server_rotor, ep->name);
133 
134  if (ep->server_fails > 1 || vec_len (dm->ip4_name_servers) <= 1)
135  {
136  /* No, tell the client to go away */
137  goto reply;
138  }
139  ep->retry_count = 0;
140  ep->server_rotor++;
141  ep->server_fails++;
142  if (ep->server_rotor >= vec_len (dm->ip4_name_servers))
143  ep->server_rotor = 0;
144  if (0)
145  clib_warning ("Try server %U", format_ip4_address,
146  dm->ip4_name_servers + ep->server_rotor);
148  (dm, ep, dm->ip4_name_servers + ep->server_rotor);
149  }
150  dns_cache_unlock (dm);
151  return;
152  }
153 
154 reply:
155  /* Save the response */
156  ep->dns_response = reply;
157 
158  /*
159  * Pick a sensible default cache entry expiration time.
160  * We don't play the 10-second timeout game.
161  */
162  ep->expiration_time = now + 600.0;
163 
164  if (0)
165  clib_warning ("resolving '%s', was %s valid",
166  ep->name, (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ?
167  "already" : "not");
168  /*
169  * The world is a mess. A single DNS request sent to e.g. 8.8.8.8
170  * may yield multiple, subtly different responses - all with the same
171  * DNS protocol-level ID.
172  *
173  * Last response wins in terms of what ends up in the cache.
174  * First response wins in terms of the response sent to the client.
175  */
176 
177  /* Strong hint that we may not find a pending resolution entry */
178  entry_was_valid = (ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) ? 1 : 0;
179 
180  if (vec_len (ep->dns_response))
182 
183  /* Most likely, send 1 message */
184  for (i = 0; i < vec_len (ep->pending_requests); i++)
185  {
186  vl_api_registration_t *regp;
187 
188  pr = vec_elt_at_index (ep->pending_requests, i);
189 
190  switch (pr->request_type)
191  {
193  {
196  if (regp == 0)
197  continue;
198 
199  rmp = vl_msg_api_alloc (sizeof (*rmp));
200  rmp->_vl_msg_id =
201  clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY
202  + dm->msg_id_base);
203  rmp->context = pr->client_context;
204  min_ttl = ~0;
205  rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
206  if (min_ttl != ~0)
207  ep->expiration_time = now + min_ttl;
208  rmp->retval = clib_host_to_net_u32 (rv);
209  vl_api_send_msg (regp, (u8 *) rmp);
210  }
211  break;
212 
214  {
216 
218  if (regp == 0)
219  continue;
220 
221  rmp = vl_msg_api_alloc (sizeof (*rmp));
222  rmp->_vl_msg_id =
223  clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY
224  + dm->msg_id_base);
225  rmp->context = pr->client_context;
226  min_ttl = ~0;
227  rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
228  if (min_ttl != ~0)
229  ep->expiration_time = now + min_ttl;
230  rmp->retval = clib_host_to_net_u32 (rv);
231  vl_api_send_msg (regp, (u8 *) rmp);
232  }
233  break;
234 
237  if (pr->is_ip6)
238  vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ );
239  else
240  vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ );
241  break;
242  default:
243  clib_warning ("request type %d unknown", pr->request_type);
244  break;
245  }
246  }
248 
249  remove_count = 0;
250  for (i = 0; i < vec_len (dm->unresolved_entries); i++)
251  {
252  if (dm->unresolved_entries[i] == pool_index)
253  {
254  vec_delete (dm->unresolved_entries, 1, i);
255  remove_count++;
256  i--;
257  }
258  }
259  /* See multiple response comment above... */
260  if (remove_count == 0)
261  {
262  u32 error_code = entry_was_valid ? DNS46_REPLY_ERROR_MULTIPLE_REPLY :
263  DNS46_REPLY_ERROR_NO_UNRESOLVED_ENTRY;
264 
265  vlib_node_increment_counter (vm, dns46_reply_node.index, error_code, 1);
266  dns_cache_unlock (dm);
267  return;
268  }
269 
270  /* Deal with bogus names, server issues, etc. */
271  switch (rcode)
272  {
273  default:
274  case DNS_RCODE_NO_ERROR:
275  break;
276 
279  case DNS_RCODE_REFUSED:
280  if (ep->server_af == 0)
281  clib_warning ("name server %U can't resolve '%s'",
283  dm->ip4_name_servers + ep->server_rotor, ep->name);
284  else
285  clib_warning ("name server %U can't resolve '%s'",
287  dm->ip6_name_servers + ep->server_rotor, ep->name);
288  /* FALLTHROUGH */
291  /* remove trash from the cache... */
293  break;
294  }
295 
296 
297  dns_cache_unlock (dm);
298  return;
299 }
300 
301 static void
303 {
304  int i;
305  dns_cache_entry_t *ep;
306 
307  for (i = 0; i < vec_len (dm->unresolved_entries); i++)
308  {
309  dns_cache_lock (dm, 11);
310  ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
311 
312  ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
313  vnet_send_dns_request (dm, ep);
314  dns_cache_unlock (dm);
315  }
316 }
317 
318 static uword
321 {
322  dns_main_t *dm = &dns_main;
323  f64 now;
324  f64 timeout = 1000.0;
325  uword *event_data = 0;
326  uword event_type;
327  int i;
328 
329  while (1)
330  {
332 
333  now = vlib_time_now (vm);
334 
335  event_type = vlib_process_get_events (vm, (uword **) & event_data);
336 
337  switch (event_type)
338  {
339  /* Send one of these when a resolution is pending */
341  timeout = 2.0;
342  break;
343 
345  for (i = 0; i < vec_len (event_data); i++)
346  resolve_event (dm, now, (u8 *) event_data[i]);
347  break;
348 
349  case ~0: /* timeout */
350  retry_scan (dm, now);
351  break;
352  }
353  vec_reset_length (event_data);
354 
355  /* No work? Back to slow timeout mode... */
356  if (vec_len (dm->unresolved_entries) == 0)
357  timeout = 1000.0;
358  }
359  return 0; /* or not */
360 }
361 
362 void
364 {
365  /* Already created the resolver process? */
366  if (dm->resolver_process_node_index > 0)
367  return;
368 
369  /* No, create it now and make a note of the node index */
371  (dm->vlib_main, "dns-resolver-process",
372  dns_resolver_process, 16 /* log2_n_stack_bytes */ );
373 }
374 
375 /*
376  * fd.io coding-style-patch-verification: ON
377  *
378  * Local Variables:
379  * eval: (c-set-style "gnu")
380  * End:
381  */
Definition: dns.h:50
u32 flags
Definition: vhost_user.h:141
ip6_address_t * ip6_name_servers
Definition: dns.h:112
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:673
#define DNS_CACHE_ENTRY_FLAG_VALID
we have Actual Data
Definition: dns.h:81
vlib_node_registration_t dns46_reply_node
(constructor) VLIB_REGISTER_NODE (dns46_reply_node)
Definition: reply_node.c:42
int vnet_dns_cname_indirection_nolock(dns_main_t *dm, u32 ep_index, u8 *reply)
Handle cname indirection.
Definition: dns.c:986
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
#define DNS_RCODE_REFUSED
Definition: dns_packet.h:40
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
#define DNS_RCODE_NO_ERROR
Definition: dns_packet.h:35
u8 * dns_response
Cached dns response.
Definition: dns.h:75
int vnet_dns_delete_entry_by_index_nolock(dns_main_t *dm, u32 index)
Definition: dns.c:682
int retry_count
Retry parameters.
Definition: dns.h:68
for(i=1;i<=collision_buckets;i++)
int i
static void retry_scan(dns_main_t *dm, f64 now)
static void resolve_event(dns_main_t *dm, f64 now, u8 *reply)
u32 client_context
Definition: dns.h:33
void * vl_msg_api_alloc(int nbytes)
void vnet_dns_create_resolver_process(dns_main_t *dm)
#define DNS_RCODE_NOT_IMPLEMENTED
Definition: dns_packet.h:39
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
vlib_main_t * vlib_main
Definition: dns.h:126
format_function_t format_ip4_address
Definition: format.h:75
dns_main_t dns_main
Definition: dns.c:61
void vnet_send_dns_request(dns_main_t *dm, dns_cache_entry_t *ep)
Definition: dns.c:558
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:516
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u32 * unresolved_entries
Pool indices of unresolved entries.
Definition: dns.h:97
#define DNS_RCODE_MASK
Definition: dns_packet.h:34
unsigned int u32
Definition: types.h:88
void vnet_send_dns4_reply(dns_main_t *dm, dns_pending_request_t *pr, dns_cache_entry_t *ep, vlib_buffer_t *b0)
Definition: dns.c:2777
u32 resolver_process_node_index
resolver process node index
Definition: dns.h:115
dns_pending_request_t * pending_requests
Clients / peers awaiting responses.
Definition: dns.h:78
ip4_address_t * ip4_name_servers
upstream name servers, e.g.
Definition: dns.h:111
u32 vlib_process_create(vlib_main_t *vm, char *name, vlib_node_function_t *f, u32 log2_n_stack_bytes)
Create a vlib process.
Definition: node.c:743
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
u8 * name
The name in "normal human being" notation, e.g.
Definition: dns.h:56
#define DNS_RCODE_SERVER_FAILURE
Definition: dns_packet.h:37
unsigned short u16
Definition: types.h:57
DNS ip->name resolution reply.
Definition: dns.api:98
int server_rotor
Definition: dns.h:69
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1150
An API client registration, only in vpp/vlib.
Definition: api_common.h:46
format_function_t format_ip6_address
Definition: format.h:93
vlib_main_t * vm
Definition: buffer.c:323
static void dns_cache_unlock(dns_main_t *dm)
Definition: dns.h:214
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
int vnet_dns_response_to_name(u8 *response, vl_api_dns_resolve_ip_reply_t *rmp, u32 *min_ttlp)
Definition: dns.c:1322
#define clib_warning(format, args...)
Definition: error.h:59
u16 msg_id_base
message-ID base
Definition: dns.h:123
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
#define ASSERT(truth)
volatile u8 flags
flags
Definition: dns.h:53
#define DNS_RCODE_FORMAT_ERROR
Definition: dns_packet.h:36
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:784
#define DNS_RESOLVER_EVENT_RESOLVED
Definition: dns.h:87
static void dns_cache_lock(dns_main_t *dm, int tag)
Definition: dns.h:202
static uword dns_resolver_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
#define DNS_RCODE_NAME_ERROR
Definition: dns_packet.h:38
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void vnet_send_dns6_reply(dns_main_t *dm, dns_pending_request_t *pr, dns_cache_entry_t *ep, vlib_buffer_t *b0)
Definition: dns.c:2769
void vnet_dns_send_dns6_request(dns_main_t *dm, dns_cache_entry_t *ep, ip6_address_t *server)
Definition: dns.c:359
u64 uword
Definition: types.h:112
Definition: dns.h:91
int server_fails
Definition: dns.h:71
int vnet_dns_response_to_reply(u8 *response, vl_api_dns_resolve_name_reply_t *rmp, u32 *min_ttlp)
Definition: dns.c:1181
void vnet_dns_send_dns4_request(dns_main_t *dm, dns_cache_entry_t *ep, ip4_address_t *server)
Definition: dns.c:240
int server_af
Definition: dns.h:70
f64 expiration_time
Expiration time.
Definition: dns.h:62
DNS name resolution reply.
Definition: dns.api:68
#define DNS_RESOLVER_EVENT_PENDING
Definition: dns.h:88
dns_cache_entry_t * entries
Pool of cache entries.
Definition: dns.h:94