FD.io VPP  v18.01.2-1-g9b554f3
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  if (vnet_dns_cname_indirection_nolock (dm, pool_index, reply))
90  {
91  dns_cache_unlock (dm);
92  return;
93  }
94 
95  /* Save the response */
96  ep->dns_response = reply;
97  /* Pick some sensible default. */
98  ep->expiration_time = now + 600.0;
99  if (vec_len (ep->dns_response))
101 
102  /* Most likely, send 1 message */
103  for (i = 0; i < vec_len (ep->pending_requests); i++)
104  {
105  vl_api_registration_t *regp;
106 
107  pr = vec_elt_at_index (ep->pending_requests, i);
108 
109  switch (pr->request_type)
110  {
112  {
115  if (regp == 0)
116  continue;
117 
118  rmp = vl_msg_api_alloc (sizeof (*rmp));
119  rmp->_vl_msg_id =
120  clib_host_to_net_u16 (VL_API_DNS_RESOLVE_NAME_REPLY);
121  rmp->context = pr->client_context;
122  min_ttl = ~0;
123  rv = vnet_dns_response_to_reply (ep->dns_response, rmp, &min_ttl);
124  if (min_ttl != ~0)
125  ep->expiration_time = now + min_ttl;
126  rmp->retval = clib_host_to_net_u32 (rv);
127  vl_msg_api_send (regp, (u8 *) rmp);
128  }
129  break;
130 
132  {
134 
136  if (regp == 0)
137  continue;
138 
139  rmp = vl_msg_api_alloc (sizeof (*rmp));
140  rmp->_vl_msg_id =
141  clib_host_to_net_u16 (VL_API_DNS_RESOLVE_IP_REPLY);
142  rmp->context = pr->client_context;
143  min_ttl = ~0;
144  rv = vnet_dns_response_to_name (ep->dns_response, rmp, &min_ttl);
145  if (min_ttl != ~0)
146  ep->expiration_time = now + min_ttl;
147  rmp->retval = clib_host_to_net_u32 (rv);
148  vl_msg_api_send (regp, (u8 *) rmp);
149  }
150  break;
151 
154  if (pr->is_ip6)
155  vnet_send_dns6_reply (dm, pr, ep, 0 /* allocate a buffer */ );
156  else
157  vnet_send_dns4_reply (dm, pr, ep, 0 /* allocate a buffer */ );
158  break;
159  default:
160  clib_warning ("request type %d unknown", pr->request_type);
161  break;
162  }
163  }
165 
166  for (i = 0; i < vec_len (dm->unresolved_entries); i++)
167  {
168  if (dm->unresolved_entries[i] == pool_index)
169  {
170  vec_delete (dm->unresolved_entries, 1, i);
171  goto found;
172  }
173  }
174  clib_warning ("pool index %d AWOL from unresolved vector", pool_index);
175 
176 found:
177  /* Deal with bogus names, server issues, etc. */
178  switch (rcode)
179  {
180  default:
181  case DNS_RCODE_NO_ERROR:
182  break;
183 
186  case DNS_RCODE_REFUSED:
187  if (ep->server_af == 0)
188  clib_warning ("name server %U backfire",
190  dm->ip4_name_servers + ep->server_rotor);
191  else
192  clib_warning ("name server %U backfire",
194  dm->ip6_name_servers + ep->server_rotor);
195  /* FALLTHROUGH */
198  /* remove trash from the cache... */
200  break;
201  }
202 
203  dns_cache_unlock (dm);
204  return;
205 }
206 
207 static void
209 {
210  int i;
211  dns_cache_entry_t *ep;
212 
213  for (i = 0; i < vec_len (dm->unresolved_entries); i++)
214  {
215  dns_cache_lock (dm);
216  ep = pool_elt_at_index (dm->entries, dm->unresolved_entries[i]);
217 
218  ASSERT ((ep->flags & DNS_CACHE_ENTRY_FLAG_VALID) == 0);
219  vnet_send_dns_request (dm, ep);
220  dns_cache_unlock (dm);
221  }
222 }
223 
224 static uword
227 {
228  dns_main_t *dm = &dns_main;
229  f64 now;
230  f64 timeout = 1000.0;
231  uword *event_data = 0;
232  uword event_type;
233  int i;
234 
235  while (1)
236  {
238 
239  now = vlib_time_now (vm);
240 
241  event_type = vlib_process_get_events (vm, (uword **) & event_data);
242 
243  switch (event_type)
244  {
245  /* Send one of these when a resolution is pending */
247  timeout = 2.0;
248  break;
249 
251  for (i = 0; i < vec_len (event_data); i++)
252  resolve_event (dm, now, (u8 *) event_data[i]);
253  break;
254 
255  case ~0: /* timeout */
256  retry_scan (dm, now);
257  break;
258  }
259  vec_reset_length (event_data);
260 
261  /* No work? Back to slow timeout mode... */
262  if (vec_len (dm->unresolved_entries) == 0)
263  timeout = 1000.0;
264  }
265  return 0; /* or not */
266 }
267 
268 /* *INDENT-OFF* */
270 {
271  .function = dns_resolver_process,
272  .type = VLIB_NODE_TYPE_PROCESS,
273  .name = "dns-resolver-process",
274 };
275 /* *INDENT-ON* */
276 
277 
278 /*
279  * fd.io coding-style-patch-verification: ON
280  *
281  * Local Variables:
282  * eval: (c-set-style "gnu")
283  * End:
284  */
Definition: dns.h:48
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:337
ip6_address_t * ip6_name_servers
Definition: dns.h:105
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:699
#define DNS_CACHE_ENTRY_FLAG_VALID
we have Actual Data
Definition: dns.h:78
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:919
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:224
#define DNS_RCODE_REFUSED
Definition: dns_packet.h:40
#define DNS_RCODE_NO_ERROR
Definition: dns_packet.h:35
u8 * dns_response
Cached dns response.
Definition: dns.h:72
int vnet_dns_delete_entry_by_index_nolock(dns_main_t *dm, u32 index)
Definition: dns.c:620
struct _vlib_node_registration vlib_node_registration_t
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
#define DNS_RCODE_NOT_IMPLEMENTED
Definition: dns_packet.h:39
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
vlib_main_t * vlib_main
Definition: dns.h:113
format_function_t format_ip4_address
Definition: format.h:79
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:515
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:542
#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:94
#define DNS_RCODE_MASK
Definition: dns_packet.h:34
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:2652
dns_pending_request_t * pending_requests
Clients / peers awaiting responses.
Definition: dns.h:75
void * vl_msg_api_alloc(int nbytes)
ip4_address_t * ip4_name_servers
upstream name servers, e.g.
Definition: dns.h:104
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:459
#define DNS_RCODE_SERVER_FAILURE
Definition: dns_packet.h:37
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:1158
An API client registration, only in vpp/vlib.
Definition: api_common.h:44
format_function_t format_ip6_address
Definition: format.h:95
vlib_main_t * vm
Definition: buffer.c:283
static void dns_cache_unlock(dns_main_t *dm)
Definition: dns.h:188
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
int vnet_dns_response_to_name(u8 *response, vl_api_dns_resolve_ip_reply_t *rmp, u32 *min_ttlp)
Definition: dns.c:1210
#define clib_warning(format, args...)
Definition: error.h:59
static void dns_cache_lock(dns_main_t *dm)
Definition: dns.h:178
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:268
#define ASSERT(truth)
volatile u8 flags
flags
Definition: dns.h:51
unsigned int u32
Definition: types.h:88
#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:781
#define DNS_RESOLVER_EVENT_RESOLVED
Definition: dns.h:84
u64 uword
Definition: types.h:112
unsigned short u16
Definition: types.h:57
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)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
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:2644
Definition: dns.h:88
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
int vnet_dns_response_to_reply(u8 *response, vl_api_dns_resolve_name_reply_t *rmp, u32 *min_ttlp)
Definition: dns.c:1069
vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
int server_af
Definition: dns.h:68
f64 expiration_time
Expiration time.
Definition: dns.h:60
u32 flags
Definition: vhost-user.h:77
DNS name resolution reply.
Definition: dns.api:68
void vl_msg_api_send(vl_api_registration_t *rp, u8 *elem)
Definition: memory_vlib.c:102
#define DNS_RESOLVER_EVENT_PENDING
Definition: dns.h:85
dns_cache_entry_t * entries
Pool of cache entries.
Definition: dns.h:91