blob: 5d3a18552696d78888bbe064a1d1f77229e02710 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2016 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 <vlib/vlib.h>
17#include <vnet/vnet.h>
18#include <vnet/pg/pg.h>
19#include <vnet/ip/ip.h>
20
21#include <vnet/udp/udp.h>
22#include <vppinfra/hash.h>
23#include <vppinfra/error.h>
24#include <vppinfra/elog.h>
25
26#include <vnet/udp/udp_packet.h>
27
28#include <vlibmemory/api.h>
29#include "../session/application_interface.h"
30
31vlib_node_registration_t udp4_uri_input_node;
32
33typedef struct
34{
35 u32 session;
36 u32 disposition;
37 u32 thread_index;
38} udp4_uri_input_trace_t;
39
40/* packet trace format function */
41static u8 *
42format_udp4_uri_input_trace (u8 * s, va_list * args)
43{
44 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
45 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
46 udp4_uri_input_trace_t *t = va_arg (*args, udp4_uri_input_trace_t *);
47
48 s = format (s, "UDP4_URI_INPUT: session %d, disposition %d, thread %d",
49 t->session, t->disposition, t->thread_index);
50 return s;
51}
52
53typedef enum
54{
55 UDP4_URI_INPUT_NEXT_DROP,
56 UDP4_URI_INPUT_N_NEXT,
57} udp4_uri_input_next_t;
58
59static char *udp4_uri_input_error_strings[] = {
60#define _(sym,string) string,
61 foreach_session_input_error
62#undef _
63};
64
65static uword
66udp4_uri_input_node_fn (vlib_main_t * vm,
67 vlib_node_runtime_t * node, vlib_frame_t * frame)
68{
69 u32 n_left_from, *from, *to_next;
70 udp4_uri_input_next_t next_index;
71 udp_uri_main_t *um = vnet_get_udp_main ();
72 session_manager_main_t *smm = vnet_get_session_manager_main ();
Damjan Marion586afd72017-04-05 19:18:20 +020073 u32 my_thread_index = vm->thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -050074 u8 my_enqueue_epoch;
75 u32 *session_indices_to_enqueue;
76 static u32 serial_number;
77 int i;
78
79 my_enqueue_epoch = ++smm->current_enqueue_epoch[my_thread_index];
80
81 from = vlib_frame_vector_args (frame);
82 n_left_from = frame->n_vectors;
83 next_index = node->cached_next_index;
84
85 while (n_left_from > 0)
86 {
87 u32 n_left_to_next;
88
89 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
90
91 while (n_left_from > 0 && n_left_to_next > 0)
92 {
93 u32 bi0;
94 vlib_buffer_t *b0;
95 u32 next0 = UDP4_URI_INPUT_NEXT_DROP;
96 u32 error0 = SESSION_ERROR_ENQUEUED;
97 udp_header_t *udp0;
98 ip4_header_t *ip0;
99 stream_session_t *s0;
100 svm_fifo_t *f0;
101 u16 udp_len0;
102 u8 *data0;
103
104 /* speculatively enqueue b0 to the current next frame */
105 bi0 = from[0];
106 to_next[0] = bi0;
107 from += 1;
108 to_next += 1;
109 n_left_from -= 1;
110 n_left_to_next -= 1;
111
112 b0 = vlib_get_buffer (vm, bi0);
113
114 /* udp_local hands us a pointer to the udp data */
115
116 data0 = vlib_buffer_get_current (b0);
117 udp0 = (udp_header_t *) (data0 - sizeof (*udp0));
118
119 /* $$$$ fixme: udp_local doesn't do ip options correctly anyhow */
120 ip0 = (ip4_header_t *) (((u8 *) udp0) - sizeof (*ip0));
121 s0 = 0;
122
123 /* lookup session */
Florin Corascea194d2017-10-02 00:18:51 -0700124 s0 = session_lookup4 (0, &ip0->dst_address, &ip0->src_address,
125 udp0->dst_port, udp0->src_port,
126 TRANSPORT_PROTO_UDP);
Dave Barach68b0fb02017-02-28 15:15:56 -0500127
128 /* no listener */
129 if (PREDICT_FALSE (s0 == 0))
130 {
131 error0 = SESSION_ERROR_NO_LISTENER;
132 goto trace0;
133 }
134
135 f0 = s0->server_rx_fifo;
136
137 /* established hit */
138 if (PREDICT_TRUE (s0->session_state == SESSION_STATE_READY))
139 {
140 udp_len0 = clib_net_to_host_u16 (udp0->length);
141
142 if (PREDICT_FALSE (udp_len0 > svm_fifo_max_enqueue (f0)))
143 {
144 error0 = SESSION_ERROR_FIFO_FULL;
145 goto trace0;
146 }
147
Florin Corasa5464812017-04-19 13:00:05 -0700148 svm_fifo_enqueue_nowait (f0, udp_len0 - sizeof (*udp0),
Dave Barach68b0fb02017-02-28 15:15:56 -0500149 (u8 *) (udp0 + 1));
150
151 b0->error = node->errors[SESSION_ERROR_ENQUEUED];
152
153 /* We need to send an RX event on this fifo */
154 if (s0->enqueue_epoch != my_enqueue_epoch)
155 {
156 s0->enqueue_epoch = my_enqueue_epoch;
157
158 vec_add1 (smm->session_indices_to_enqueue_by_thread
159 [my_thread_index],
160 s0 - smm->sessions[my_thread_index]);
161 }
162 }
163 /* listener hit */
164 else if (s0->session_state == SESSION_STATE_LISTENING)
165 {
166 udp_connection_t *us;
167 int rv;
168
169 error0 = SESSION_ERROR_NOT_READY;
170
171 /*
172 * create udp transport session
173 */
174 pool_get (um->udp_sessions[my_thread_index], us);
175
176 us->mtu = 1024; /* $$$$ policy */
177
178 us->c_lcl_ip4.as_u32 = ip0->dst_address.as_u32;
179 us->c_rmt_ip4.as_u32 = ip0->src_address.as_u32;
180 us->c_lcl_port = udp0->dst_port;
181 us->c_rmt_port = udp0->src_port;
Florin Coras68810622017-07-24 17:40:28 -0700182 us->c_transport_proto = TRANSPORT_PROTO_UDP;
Dave Barach68b0fb02017-02-28 15:15:56 -0500183 us->c_c_index = us - um->udp_sessions[my_thread_index];
184
185 /*
186 * create stream session and attach the udp session to it
187 */
188 rv = stream_session_accept (&us->connection, s0->session_index,
Dave Barach68b0fb02017-02-28 15:15:56 -0500189 1 /*notify */ );
190 if (rv)
191 error0 = rv;
192
193 }
194 else
195 {
196
197 error0 = SESSION_ERROR_NOT_READY;
198 goto trace0;
199 }
200
201 trace0:
202 b0->error = node->errors[error0];
203
204 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
205 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
206 {
207 udp4_uri_input_trace_t *t =
208 vlib_add_trace (vm, node, b0, sizeof (*t));
209
210 t->session = ~0;
211 if (s0)
212 t->session = s0 - smm->sessions[my_thread_index];
213 t->disposition = error0;
214 t->thread_index = my_thread_index;
215 }
216
217 /* verify speculative enqueue, maybe switch current next frame */
218 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
219 to_next, n_left_to_next,
220 bi0, next0);
221 }
222
223 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
224 }
225
226 /* Send enqueue events */
227
228 session_indices_to_enqueue =
229 smm->session_indices_to_enqueue_by_thread[my_thread_index];
230
231 for (i = 0; i < vec_len (session_indices_to_enqueue); i++)
232 {
233 session_fifo_event_t evt;
234 unix_shared_memory_queue_t *q;
235 stream_session_t *s0;
236 application_t *server0;
237
238 /* Get session */
239 s0 = pool_elt_at_index (smm->sessions[my_thread_index],
240 session_indices_to_enqueue[i]);
241
242 /* Get session's server */
243 server0 = application_get (s0->app_index);
244
Florin Corasd79b41e2017-03-04 05:37:52 -0800245 /* Built-in server? Deliver the goods... */
246 if (server0->cb_fns.builtin_server_rx_callback)
247 {
Florin Coras6792ec02017-03-13 03:49:51 -0700248 server0->cb_fns.builtin_server_rx_callback (s0);
Florin Corasd79b41e2017-03-04 05:37:52 -0800249 continue;
250 }
251
Florin Coras6792ec02017-03-13 03:49:51 -0700252 if (svm_fifo_set_event (s0->server_rx_fifo))
Dave Barach68b0fb02017-02-28 15:15:56 -0500253 {
Florin Coras6792ec02017-03-13 03:49:51 -0700254 /* Fabricate event */
255 evt.fifo = s0->server_rx_fifo;
Florin Corasa5464812017-04-19 13:00:05 -0700256 evt.event_type = FIFO_EVENT_APP_RX;
Florin Coras6792ec02017-03-13 03:49:51 -0700257 evt.event_id = serial_number++;
258
259 /* Add event to server's event queue */
260 q = server0->event_queue;
261
262 /* Don't block for lack of space */
263 if (PREDICT_TRUE (q->cursize < q->maxsize))
264 {
265 unix_shared_memory_queue_add (server0->event_queue,
266 (u8 *) & evt,
267 0 /* do wait for mutex */ );
268 }
269 else
270 {
271 vlib_node_increment_counter (vm, udp4_uri_input_node.index,
272 SESSION_ERROR_FIFO_FULL, 1);
273 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500274 }
Florin Coras6792ec02017-03-13 03:49:51 -0700275 /* *INDENT-OFF* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500276 if (1)
277 {
278 ELOG_TYPE_DECLARE (e) =
279 {
Florin Coras6792ec02017-03-13 03:49:51 -0700280 .format = "evt-enqueue: id %d length %d",
281 .format_args = "i4i4",};
Dave Barach68b0fb02017-02-28 15:15:56 -0500282 struct
283 {
284 u32 data[2];
285 } *ed;
286 ed = ELOG_DATA (&vlib_global_main.elog_main, e);
287 ed->data[0] = evt.event_id;
Florin Coras6792ec02017-03-13 03:49:51 -0700288 ed->data[1] = svm_fifo_max_dequeue (s0->server_rx_fifo);
Dave Barach68b0fb02017-02-28 15:15:56 -0500289 }
Florin Coras6792ec02017-03-13 03:49:51 -0700290 /* *INDENT-ON* */
291
Dave Barach68b0fb02017-02-28 15:15:56 -0500292 }
293
294 vec_reset_length (session_indices_to_enqueue);
295
296 smm->session_indices_to_enqueue_by_thread[my_thread_index] =
297 session_indices_to_enqueue;
298
299 return frame->n_vectors;
300}
301
302VLIB_REGISTER_NODE (udp4_uri_input_node) =
303{
304 .function = udp4_uri_input_node_fn,.name = "udp4-uri-input",.vector_size =
305 sizeof (u32),.format_trace = format_udp4_uri_input_trace,.type =
306 VLIB_NODE_TYPE_INTERNAL,.n_errors =
307 ARRAY_LEN (udp4_uri_input_error_strings),.error_strings =
308 udp4_uri_input_error_strings,.n_next_nodes = UDP4_URI_INPUT_N_NEXT,
309 /* edit / add dispositions here */
310 .next_nodes =
311 {
312 [UDP4_URI_INPUT_NEXT_DROP] = "error-drop",}
313,};
314
315/*
316 * fd.io coding-style-patch-verification: ON
317 *
318 * Local Variables:
319 * eval: (c-set-style "gnu")
320 * End:
321 */