FD.io VPP  v19.04-6-g6f05f72
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 <vnet/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 #include <vnet/vnet_msg_enum.h>
24 
25 #define vl_typedefs /* define message structures */
26 #include <vnet/vnet_all_api_h.h>
27 #undef vl_typedefs
28 
29 #define vl_endianfun /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_endianfun
32 
33 /* instantiate all the print functions we know about */
34 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35 #define vl_printfun
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_printfun
38 
40 
42 
43 extern int
44 vnet_dns_response_to_reply (u8 * response,
46  u32 * min_ttlp);
47 extern int
48 vnet_dns_response_to_name (u8 * response,
50  u32 * min_ttlp);
51 
52 static void
53 resolve_event (dns_main_t * dm, f64 now, u8 * reply)
54 {
55  vlib_main_t *vm = dm->vlib_main;
57  dns_header_t *d;
58  u32 pool_index;
60  u32 min_ttl;
61  u16 flags;
62  u16 rcode;
63  int i;
64  int rv = 0;
65 
66  d = (dns_header_t *) reply;
67  flags = clib_net_to_host_u16 (d->flags);
68  rcode = flags & DNS_RCODE_MASK;
69 
70  /* $$$ u16 limits cache to 65K entries, fix later multiple dst ports */
71  pool_index = clib_net_to_host_u16 (d->id);
72  dns_cache_lock (dm);
73 
74  if (pool_is_free_index (dm->entries, pool_index))
75  {
76  vec_free (reply);
78  DNS46_REPLY_ERROR_NO_ELT, 1);
79  dns_cache_unlock (dm);
80  return;
81  }
82 
83  ep = pool_elt_at_index (dm->entries, pool_index);
84 
85  if (ep->dns_response)
86  vec_free (ep->dns_response);
87 
88  /* Handle [sic] recursion AKA CNAME indirection */
89  rv = vnet_dns_cname_indirection_nolock (dm, pool_index, reply);
90 
91  /* CNAME found, further resolution pending, we're done here */
92  if (rv > 0)
93  {
94  dns_cache_unlock (dm);
95  return;
96  }
97  /* Server backfire: refused to answer, or sent zero replies */
98  if (rv < 0)
99  {
100  /* Try a different server */
101  if (ep->server_af /* ip6 */ )
102  {
103  if (0)
104  clib_warning ("Server %U failed to resolve '%s'",
106  dm->ip6_name_servers + ep->server_rotor, ep->name);
107  /* Any more servers to try? */
108  if (ep->server_fails > 1 || vec_len (dm->ip6_name_servers) <= 1)
109  {
110  /* No, tell the client to go away */
111  goto reply;
112  }
113  ep->retry_count = 0;
114  ep->server_rotor++;
115  ep->server_fails++;
116  if (ep->server_rotor >= vec_len (dm->ip6_name_servers))
117  ep->server_rotor = 0;
118  if (0)
119  clib_warning ("Try server %U", format_ip6_address,
120  dm->ip6_name_servers + ep->server_rotor);
122  (dm, ep, dm->ip6_name_servers + ep->server_rotor);
123  }
124  else
125  {
126  if (0)
127  clib_warning ("Server %U failed to resolve '%s'",
129  dm->ip4_name_servers + ep->server_rotor, ep->name);
130 
131  if (ep->server_fails > 1 || vec_len (dm->ip4_name_servers) <= 1)
132  {
133  /* No, tell the client to go away */
134  goto reply;
135  }
136  ep->retry_count = 0;
137  ep->server_rotor++;
138  ep->server_fails++;
139  if (ep->server_rotor >= vec_len (dm->ip4_name_servers))
140  ep->server_rotor = 0;
141  if (0)
142  clib_warning ("Try server %U", format_ip4_address,
143  dm->ip4_name_servers + ep->server_rotor);
145  (dm, ep, dm->ip4_name_servers + ep->server_rotor);
146  }
147  dns_cache_unlock (dm);
148  return;
149  }
150 
151 reply:
152  /* Save the response */
153  ep->dns_response = reply;
154  /* Pick some sensible default. */
155  ep->expiration_time = now + 600.0;
156  if (vec_len (ep->dns_response))
158 
159  /* Most likely, send 1 message */
160  for (i = 0; i < vec_len (ep->pending_requests); i++)
161  {
162  vl_api_registration_t *regp;
163 
164  pr = vec_elt_at_index (ep->pending_requests, i);
165 
166  switch (pr->request_type)
167  {
169  {
172  if (regp == 0)
173  continue;
174 
175  rmp = vl_msg_api_alloc (sizeof (*rmp));
176  rmp->_vl_msg_id =
177  clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY);
178  rmp->context = pr->client_context;
179  min_ttl = ~0;
180  rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
181  if (min_ttl != ~0)
182  ep->expiration_time = now + min_ttl;
183  rmp->retval = clib_host_to_net_u32 (rv);
184  vl_api_send_msg (regp, (u8 *) rmp);
185  }
186  break;
187 
189  {
191 
193  if (regp == 0)
194  continue;
195 
196  rmp = vl_msg_api_alloc (sizeof (*rmp));
197  rmp->_vl_msg_id =
198  clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY);
199  rmp->context = pr->client_context;
200  min_ttl = ~0;
201  rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
202  if (min_ttl != ~0)
203  ep->expiration_time = now + min_ttl;
204  rmp->retval = clib_host_to_net_u32 (rv);
205  vl_api_send_msg (regp, (u8 *) rmp);
206  }
207  break;
208 
211  if (pr->is_ip6)
212  vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ );
213  else
214  vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ );
215  break;
216  default:
217  clib_warning ("request type %d unknown", pr->request_type);
218  break;
219  }
220  }
222 
223  for (i = 0; i < vec_len (dm->unresolved_entries); i++)
224  {
225  if (dm->unresolved_entries[i] == pool_index)
226  {
227  vec_delete (dm->unresolved_entries, 1, i);
228  goto found;
229  }
230  }
231  clib_warning ("pool index %d AWOL from unresolved vector", pool_index);
232 
233 found:
234  /* Deal with bogus names, server issues, etc. */
235  switch (rcode)
236  {
237  default:
238  case DNS_RCODE_NO_ERROR:
239  break;
240 
243  case DNS_RCODE_REFUSED:
244  if (ep->server_af == 0)
245  clib_warning ("name server %U backfire",
247  dm->ip4_name_servers + ep->server_rotor);
248  else
249  clib_warning ("name server %U backfire",
251  dm->ip6_name_servers + ep->server_rotor);
252  /* FALLTHROUGH */
255  /* remove trash from the cache... */
257  break;
258  }
259 
260  dns_cache_unlock (dm);
261  return;
262 }
263 
264 static void
266 {
267  int i;
268  dns_cache_entry_t *ep;
269 
270  for (i = 0; i < vec_len (dm->unresolved_entries); i++)
271  {
272  dns_cache_lock (dm);
273  ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
274 
275  ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
276  vnet_send_dns_request (dm, ep);
277  dns_cache_unlock (dm);
278  }
279 }
280 
281 static uword
284 {
285  dns_main_t *dm = &dns_main;
286  f64 now;
287  f64 timeout = 1000.0;
288  uword *event_data = 0;
289  uword event_type;
290  int i;
291 
292  while (1)
293  {
295 
296  now = vlib_time_now (vm);
297 
298  event_type = vlib_process_get_events (vm, (uword **) & event_data);
299 
300  switch (event_type)
301  {
302  /* Send one of these when a resolution is pending */
304  timeout = 2.0;
305  break;
306 
308  for (i = 0; i < vec_len (event_data); i++)
309  resolve_event (dm, now, (u8 *) event_data[i]);
310  break;
311 
312  case ~0: /* timeout */
313  retry_scan (dm, now);
314  break;
315  }
316  vec_reset_length (event_data);
317 
318  /* No work? Back to slow timeout mode... */
319  if (vec_len (dm->unresolved_entries) == 0)
320  timeout = 1000.0;
321  }
322  return 0; /* or not */
323 }
324 
325 /* *INDENT-OFF* */
327 {
328  .function = dns_resolver_process,
329  .type = VLIB_NODE_TYPE_PROCESS,
330  .name = "dns-resolver-process",
331 };
332 /* *INDENT-ON* */
333 
334 
335 /*
336  * fd.io coding-style-patch-verification: ON
337  *
338  * Local Variables:
339  * eval: (c-set-style "gnu")
340  * End:
341  */
Definition: dns.h:48
u32 flags
Definition: vhost_user.h:115
ip6_address_t * ip6_name_servers
Definition: dns.h:109
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:703
#define DNS_CACHE_ENTRY_FLAG_VALID
we have Actual Data
Definition: dns.h:79
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:955
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
#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:34
#define DNS_RCODE_NO_ERROR
Definition: dns_packet.h:35
u8 * dns_response
Cached dns response.
Definition: dns.h:73
int vnet_dns_delete_entry_by_index_nolock(dns_main_t *dm, u32 index)
Definition: dns.c:656
int retry_count
Retry parameters.
Definition: dns.h:66
int i
static void retry_scan(dns_main_t *dm, f64 now)
vlib_node_registration_t dns_resolver_node
(constructor) VLIB_REGISTER_NODE (dns_resolver_node)
static void resolve_event(dns_main_t *dm, f64 now, u8 *reply)
u32 client_context
Definition: dns.h:31
void * vl_msg_api_alloc(int nbytes)
#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:117
format_function_t format_ip4_address
Definition: format.h:75
dns_main_t dns_main
Definition: dns.c:42
void vnet_send_dns_request(dns_main_t *dm, dns_cache_entry_t *ep)
Definition: dns.c:533
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:546
#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:95
#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:2737
dns_pending_request_t * pending_requests
Clients / peers awaiting responses.
Definition: dns.h:76
ip4_address_t * ip4_name_servers
upstream name servers, e.g.
Definition: dns.h:108
#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:54
#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:67
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1180
An API client registration, only in vpp/vlib.
Definition: api_common.h:45
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
format_function_t format_ip6_address
Definition: format.h:93
vlib_main_t * vm
Definition: buffer.c:312
static void dns_cache_unlock(dns_main_t *dm)
Definition: dns.h:199
#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:1290
#define clib_warning(format, args...)
Definition: error.h:59
static void dns_cache_lock(dns_main_t *dm)
Definition: dns.h:189
#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:56
#define ASSERT(truth)
volatile u8 flags
flags
Definition: dns.h:51
#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:85
struct _vlib_node_registration vlib_node_registration_t
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:2729
void vnet_dns_send_dns6_request(dns_main_t *dm, dns_cache_entry_t *ep, ip6_address_t *server)
Definition: dns.c:335
u64 uword
Definition: types.h:112
Definition: dns.h:89
int server_fails
Definition: dns.h:69
int vnet_dns_response_to_reply(u8 *response, vl_api_dns_resolve_name_reply_t *rmp, u32 *min_ttlp)
Definition: dns.c:1149
void vnet_dns_send_dns4_request(dns_main_t *dm, dns_cache_entry_t *ep, ip4_address_t *server)
Definition: dns.c:219
int server_af
Definition: dns.h:68
f64 expiration_time
Expiration time.
Definition: dns.h:60
DNS name resolution reply.
Definition: dns.api:68
#define DNS_RESOLVER_EVENT_PENDING
Definition: dns.h:86
dns_cache_entry_t * entries
Pool of cache entries.
Definition: dns.h:92