VPP-1027: DNS name resolver
[vpp.git] / src / vnet / dns / reply_node.c
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
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20
21 vlib_node_registration_t dns46_reply_node;
22
23 typedef struct
24 {
25   u32 pool_index;
26   u32 disposition;
27 } dns46_reply_trace_t;
28
29 /* packet trace format function */
30 static u8 *
31 format_dns46_reply_trace (u8 * s, va_list * args)
32 {
33   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35   dns46_reply_trace_t *t = va_arg (*args, dns46_reply_trace_t *);
36
37   s = format (s, "DNS46_REPLY: pool index %d, disposition  %d",
38               t->pool_index, t->disposition);
39   return s;
40 }
41
42 vlib_node_registration_t dns46_reply_node;
43
44 static char *dns46_reply_error_strings[] = {
45 #define _(sym,string) string,
46   foreach_dns46_reply_error
47 #undef _
48 };
49
50 typedef enum
51 {
52   DNS46_REPLY_NEXT_DROP,
53   DNS46_REPLY_N_NEXT,
54 } dns46_reply_next_t;
55
56 static uword
57 dns46_reply_node_fn (vlib_main_t * vm,
58                      vlib_node_runtime_t * node, vlib_frame_t * frame)
59 {
60   u32 n_left_from, *from, *to_next;
61   dns46_reply_next_t next_index;
62
63   from = vlib_frame_vector_args (frame);
64   n_left_from = frame->n_vectors;
65   next_index = node->cached_next_index;
66
67   while (n_left_from > 0)
68     {
69       u32 n_left_to_next;
70
71       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
72
73 #if 0
74       while (n_left_from >= 4 && n_left_to_next >= 2)
75         {
76           u32 next0 = DNS46_REPLY_NEXT_INTERFACE_OUTPUT;
77           u32 next1 = DNS46_REPLY_NEXT_INTERFACE_OUTPUT;
78           u32 sw_if_index0, sw_if_index1;
79           u8 tmp0[6], tmp1[6];
80           ethernet_header_t *en0, *en1;
81           u32 bi0, bi1;
82           vlib_buffer_t *b0, *b1;
83
84           /* Prefetch next iteration. */
85           {
86             vlib_buffer_t *p2, *p3;
87
88             p2 = vlib_get_buffer (vm, from[2]);
89             p3 = vlib_get_buffer (vm, from[3]);
90
91             vlib_prefetch_buffer_header (p2, LOAD);
92             vlib_prefetch_buffer_header (p3, LOAD);
93
94             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
95             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
96           }
97
98           /* speculatively enqueue b0 and b1 to the current next frame */
99           to_next[0] = bi0 = from[0];
100           to_next[1] = bi1 = from[1];
101           from += 2;
102           to_next += 2;
103           n_left_from -= 2;
104           n_left_to_next -= 2;
105
106           b0 = vlib_get_buffer (vm, bi0);
107           b1 = vlib_get_buffer (vm, bi1);
108
109           /* $$$$$ End of processing 2 x packets $$$$$ */
110
111           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
112             {
113               if (b0->flags & VLIB_BUFFER_IS_TRACED)
114                 {
115                   dns46_reply_trace_t *t =
116                     vlib_add_trace (vm, node, b0, sizeof (*t));
117                   t->sw_if_index = sw_if_index0;
118                   t->next_index = next0;
119                 }
120               if (b1->flags & VLIB_BUFFER_IS_TRACED)
121                 {
122                   dns46_reply_trace_t *t =
123                     vlib_add_trace (vm, node, b1, sizeof (*t));
124                   t->sw_if_index = sw_if_index1;
125                   t->next_index = next1;
126                 }
127             }
128
129           /* verify speculative enqueues, maybe switch current next frame */
130           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
131                                            to_next, n_left_to_next,
132                                            bi0, bi1, next0, next1);
133         }
134 #endif
135
136       while (n_left_from > 0 && n_left_to_next > 0)
137         {
138           u32 bi0;
139           vlib_buffer_t *b0;
140           u32 next0 = DNS46_REPLY_NEXT_DROP;
141           dns_header_t *d0;
142           u32 pool_index0;
143           u32 error0;
144           u8 *resp0 = 0;
145
146           /* speculatively enqueue b0 to the current next frame */
147           bi0 = from[0];
148           to_next[0] = bi0;
149           from += 1;
150           to_next += 1;
151           n_left_from -= 1;
152
153           n_left_to_next -= 1;
154
155           b0 = vlib_get_buffer (vm, bi0);
156           d0 = vlib_buffer_get_current (b0);
157
158           pool_index0 = clib_host_to_net_u16 (d0->id);
159
160           /* Save the reply */
161           vec_validate (resp0, vlib_buffer_length_in_chain (vm, b0) - 1);
162           clib_memcpy (resp0, d0, vlib_buffer_length_in_chain (vm, b0));
163
164           /*
165            * Deal with everything in process ctx on the main thread
166            */
167           vlib_process_signal_event_mt (vm, dns_resolver_node.index,
168                                         DNS_RESOLVER_EVENT_RESOLVED,
169                                         (uword) resp0);
170           error0 = DNS46_REPLY_ERROR_PROCESSED;
171
172           b0->error = node->errors[error0];
173
174           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
175                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
176             {
177               dns46_reply_trace_t *t =
178                 vlib_add_trace (vm, node, b0, sizeof (*t));
179               t->disposition = error0;
180               t->pool_index = pool_index0;
181             }
182
183           /* verify speculative enqueue, maybe switch current next frame */
184           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
185                                            to_next, n_left_to_next,
186                                            bi0, next0);
187         }
188
189       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
190     }
191
192   return frame->n_vectors;
193 }
194
195 VLIB_REGISTER_NODE (dns46_reply_node) =
196 {
197   .function = dns46_reply_node_fn,.name = "dns46_reply",.vector_size =
198     sizeof (u32),.format_trace = format_dns46_reply_trace,.type =
199     VLIB_NODE_TYPE_INTERNAL,.n_errors =
200     ARRAY_LEN (dns46_reply_error_strings),.error_strings =
201     dns46_reply_error_strings,.n_next_nodes = DNS46_REPLY_N_NEXT,
202     /* edit / add dispositions here */
203     .next_nodes =
204   {
205   [DNS46_REPLY_NEXT_DROP] = "error-drop",}
206 ,};
207
208 /*
209  * fd.io coding-style-patch-verification: ON
210  *
211  * Local Variables:
212  * eval: (c-set-style "gnu")
213  * End:
214  */