8bd2f3605e608adee833cc4383f94b630f865f61
[vpp.git] / src / vnet / tcp / builtin_server.c
1 /*
2 * Copyright (c) 2015-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/vnet.h>
17 #include <vlibmemory/api.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/application_interface.h>
20
21 /* define message IDs */
22 #include <vpp/api/vpe_msg_enum.h>
23
24 /* define message structures */
25 #define vl_typedefs
26 #include <vpp/api/vpe_all_api_h.h>
27 #undef vl_typedefs
28
29 /* define generated endian-swappers */
30 #define vl_endianfun
31 #include <vpp/api/vpe_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 <vpp/api/vpe_all_api_h.h>
38 #undef vl_printfun
39
40 typedef struct
41 {
42   /* Per-thread RX buffer */
43   u8 **rx_buf;
44   unix_shared_memory_queue_t **vpp_queue;
45   u64 byte_index;
46
47   /* Sever's event queue */
48   unix_shared_memory_queue_t *vl_input_queue;
49
50   /* API client handle */
51   u32 my_client_index;
52
53   u32 app_index;
54
55   /* process node index for evnt scheduling */
56   u32 node_index;
57   vlib_main_t *vlib_main;
58 } builtin_server_main_t;
59
60 builtin_server_main_t builtin_server_main;
61
62 int
63 builtin_session_accept_callback (stream_session_t * s)
64 {
65   builtin_server_main_t *bsm = &builtin_server_main;
66
67   bsm->vpp_queue[s->thread_index] =
68     session_manager_get_vpp_event_queue (s->thread_index);
69   s->session_state = SESSION_STATE_READY;
70   bsm->byte_index = 0;
71   return 0;
72 }
73
74 void
75 builtin_session_disconnect_callback (stream_session_t * s)
76 {
77   builtin_server_main_t *bsm = &builtin_server_main;
78   vnet_disconnect_args_t _a, *a = &_a;
79
80   a->handle = stream_session_handle (s);
81   a->app_index = bsm->app_index;
82   vnet_disconnect_session (a);
83 }
84
85 void
86 builtin_session_reset_callback (stream_session_t * s)
87 {
88   clib_warning ("called.. ");
89
90   stream_session_cleanup (s);
91 }
92
93
94 int
95 builtin_session_connected_callback (u32 app_index, u32 api_context,
96                                     stream_session_t * s, u8 is_fail)
97 {
98   clib_warning ("called...");
99   return -1;
100 }
101
102 int
103 builtin_add_segment_callback (u32 client_index,
104                               const u8 * seg_name, u32 seg_size)
105 {
106   clib_warning ("called...");
107   return -1;
108 }
109
110 int
111 builtin_redirect_connect_callback (u32 client_index, void *mp)
112 {
113   clib_warning ("called...");
114   return -1;
115 }
116
117 void
118 test_bytes (builtin_server_main_t * bsm, int actual_transfer)
119 {
120   int i;
121   u32 my_thread_id = vlib_get_thread_index ();
122
123   for (i = 0; i < actual_transfer; i++)
124     {
125       if (bsm->rx_buf[my_thread_id][i] != ((bsm->byte_index + i) & 0xff))
126         {
127           clib_warning ("at %lld expected %d got %d", bsm->byte_index + i,
128                         (bsm->byte_index + i) & 0xff,
129                         bsm->rx_buf[my_thread_id][i]);
130         }
131     }
132   bsm->byte_index += actual_transfer;
133 }
134
135 int
136 builtin_server_rx_callback (stream_session_t * s)
137 {
138   u32 n_written, max_dequeue, max_enqueue, max_transfer;
139   int actual_transfer;
140   svm_fifo_t *tx_fifo, *rx_fifo;
141   builtin_server_main_t *bsm = &builtin_server_main;
142   session_fifo_event_t evt;
143   static int serial_number = 0;
144   u32 my_thread_id = vlib_get_thread_index ();
145
146   tx_fifo = s->server_tx_fifo;
147   rx_fifo = s->server_rx_fifo;
148
149   max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
150   max_enqueue = svm_fifo_max_enqueue (s->server_tx_fifo);
151
152   if (PREDICT_FALSE (max_dequeue == 0))
153     return 0;
154
155   /* Number of bytes we're going to copy */
156   max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue;
157
158   /* No space in tx fifo */
159   if (PREDICT_FALSE (max_transfer == 0))
160     {
161       /* XXX timeout for session that are stuck */
162
163     rx_event:
164       /* Program self-tap to retry */
165       if (svm_fifo_set_event (rx_fifo))
166         {
167           evt.fifo = rx_fifo;
168           evt.event_type = FIFO_EVENT_BUILTIN_RX;
169           evt.event_id = 0;
170           unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
171                                         (u8 *) & evt,
172                                         0 /* do wait for mutex */ );
173         }
174
175       return 0;
176     }
177
178   vec_validate (bsm->rx_buf, my_thread_id);
179   vec_validate (bsm->rx_buf[my_thread_id], max_transfer - 1);
180   _vec_len (bsm->rx_buf[my_thread_id]) = max_transfer;
181
182   actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, max_transfer,
183                                              bsm->rx_buf[my_thread_id]);
184   ASSERT (actual_transfer == max_transfer);
185
186 //  test_bytes (bsm, actual_transfer);
187
188   /*
189    * Echo back
190    */
191
192   n_written = svm_fifo_enqueue_nowait (tx_fifo, actual_transfer,
193                                        bsm->rx_buf[my_thread_id]);
194
195   if (n_written != max_transfer)
196     clib_warning ("short trout!");
197
198   if (svm_fifo_set_event (tx_fifo))
199     {
200       /* Fabricate TX event, send to vpp */
201       evt.fifo = tx_fifo;
202       evt.event_type = FIFO_EVENT_APP_TX;
203       evt.event_id = serial_number++;
204
205       unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
206                                     (u8 *) & evt, 0 /* do wait for mutex */ );
207     }
208
209   if (PREDICT_FALSE (max_enqueue < max_dequeue))
210     goto rx_event;
211
212   return 0;
213 }
214
215 static session_cb_vft_t builtin_session_cb_vft = {
216   .session_accept_callback = builtin_session_accept_callback,
217   .session_disconnect_callback = builtin_session_disconnect_callback,
218   .session_connected_callback = builtin_session_connected_callback,
219   .add_segment_callback = builtin_add_segment_callback,
220   .redirect_connect_callback = builtin_redirect_connect_callback,
221   .builtin_server_rx_callback = builtin_server_rx_callback,
222   .session_reset_callback = builtin_session_reset_callback
223 };
224
225 /* Abuse VPP's input queue */
226 static int
227 create_api_loopback (vlib_main_t * vm)
228 {
229   builtin_server_main_t *bsm = &builtin_server_main;
230   vl_api_memclnt_create_t _m, *mp = &_m;
231   extern void vl_api_memclnt_create_t_handler (vl_api_memclnt_create_t *);
232   api_main_t *am = &api_main;
233   vl_shmem_hdr_t *shmem_hdr;
234   uword *event_data = 0, event_type;
235   int resolved = 0;
236
237   /*
238    * Create a "loopback" API client connection
239    * Don't do things like this unless you know what you're doing...
240    */
241
242   shmem_hdr = am->shmem_hdr;
243   bsm->vl_input_queue = shmem_hdr->vl_input_queue;
244   memset (mp, 0, sizeof (*mp));
245   mp->_vl_msg_id = VL_API_MEMCLNT_CREATE;
246   mp->context = 0xFEEDFACE;
247   mp->input_queue = pointer_to_uword (bsm->vl_input_queue);
248   strncpy ((char *) mp->name, "tcp_test_server", sizeof (mp->name) - 1);
249
250   vl_api_memclnt_create_t_handler (mp);
251
252   /* Wait for reply */
253   bsm->node_index = vlib_get_current_process (vm)->node_runtime.node_index;
254   vlib_process_wait_for_event_or_clock (vm, 1.0);
255   event_type = vlib_process_get_events (vm, &event_data);
256   switch (event_type)
257     {
258     case 1:
259       resolved = 1;
260       break;
261     case ~0:
262       /* timed out */
263       break;
264     default:
265       clib_warning ("unknown event_type %d", event_type);
266     }
267   if (!resolved)
268     return -1;
269
270   return 0;
271 }
272
273 static int
274 server_attach ()
275 {
276   builtin_server_main_t *bsm = &builtin_server_main;
277   u8 segment_name[128];
278   u64 options[SESSION_OPTIONS_N_OPTIONS];
279   vnet_app_attach_args_t _a, *a = &_a;
280
281   memset (a, 0, sizeof (*a));
282   memset (options, 0, sizeof (options));
283
284   a->api_client_index = bsm->my_client_index;
285   a->session_cb_vft = &builtin_session_cb_vft;
286   a->options = options;
287   a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 512 << 20;
288   a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 64 << 10;
289   a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 64 << 10;
290   a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
291   a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 8192;
292   a->segment_name = segment_name;
293   a->segment_name_length = ARRAY_LEN (segment_name);
294
295   if (vnet_application_attach (a))
296     {
297       clib_warning ("failed to attach server");
298       return -1;
299     }
300   bsm->app_index = a->app_index;
301   return 0;
302 }
303
304 static int
305 server_listen ()
306 {
307   builtin_server_main_t *bsm = &builtin_server_main;
308   vnet_bind_args_t _a, *a = &_a;
309   memset (a, 0, sizeof (*a));
310   a->app_index = bsm->app_index;
311   a->uri = "tcp://0.0.0.0/1234";
312   return vnet_bind_uri (a);
313 }
314
315 static int
316 server_create (vlib_main_t * vm)
317 {
318   builtin_server_main_t *bsm = &builtin_server_main;
319   u32 num_threads;
320   vlib_thread_main_t *vtm = vlib_get_thread_main ();
321
322   if (bsm->my_client_index == (u32) ~ 0)
323     {
324       if (create_api_loopback (vm))
325         return -1;
326     }
327
328   num_threads = 1 /* main thread */  + vtm->n_threads;
329   vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
330
331   if (server_attach ())
332     {
333       clib_warning ("failed to attach server");
334       return -1;
335     }
336   if (server_listen ())
337     {
338       clib_warning ("failed to start listening");
339       return -1;
340     }
341   return 0;
342 }
343
344 /* Get our api client index */
345 static void
346 vl_api_memclnt_create_reply_t_handler (vl_api_memclnt_create_reply_t * mp)
347 {
348   vlib_main_t *vm = vlib_get_main ();
349   builtin_server_main_t *bsm = &builtin_server_main;
350   bsm->my_client_index = mp->index;
351   vlib_process_signal_event (vm, bsm->node_index, 1 /* evt */ ,
352                              0 /* data */ );
353 }
354
355 #define foreach_tcp_builtin_server_api_msg                      \
356 _(MEMCLNT_CREATE_REPLY, memclnt_create_reply)                   \
357
358 static clib_error_t *
359 tcp_builtin_server_api_hookup (vlib_main_t * vm)
360 {
361   vl_msg_api_msg_config_t _c, *c = &_c;
362
363   /* Hook up client-side static APIs to our handlers */
364 #define _(N,n) do {                                             \
365     c->id = VL_API_##N;                                         \
366     c->name = #n;                                               \
367     c->handler = vl_api_##n##_t_handler;                        \
368     c->cleanup = vl_noop_handler;                               \
369     c->endian = vl_api_##n##_t_endian;                          \
370     c->print = vl_api_##n##_t_print;                            \
371     c->size = sizeof(vl_api_##n##_t);                           \
372     c->traced = 1; /* trace, so these msgs print */             \
373     c->replay = 0; /* don't replay client create/delete msgs */ \
374     c->message_bounce = 0; /* don't bounce this message */      \
375     vl_msg_api_config(c);} while (0);
376
377   foreach_tcp_builtin_server_api_msg;
378 #undef _
379
380   return 0;
381 }
382
383 static clib_error_t *
384 server_create_command_fn (vlib_main_t * vm,
385                           unformat_input_t * input, vlib_cli_command_t * cmd)
386 {
387   int rv;
388 #if 0
389   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
390     {
391       if (unformat (input, "whatever %d", &whatever))
392         ;
393       else
394         return clib_error_return (0, "unknown input `%U'",
395                                   format_unformat_error, input);
396     }
397 #endif
398
399   tcp_builtin_server_api_hookup (vm);
400   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
401   rv = server_create (vm);
402   switch (rv)
403     {
404     case 0:
405       break;
406     default:
407       return clib_error_return (0, "server_create returned %d", rv);
408     }
409   return 0;
410 }
411
412 /* *INDENT-OFF* */
413 VLIB_CLI_COMMAND (server_create_command, static) =
414 {
415   .path = "test tcp server",
416   .short_help = "test tcp server",
417   .function = server_create_command_fn,
418 };
419 /* *INDENT-ON* */
420
421 clib_error_t *
422 builtin_tcp_server_main_init (vlib_main_t * vm)
423 {
424   builtin_server_main_t *bsm = &builtin_server_main;
425   bsm->my_client_index = ~0;
426   return 0;
427 }
428
429 VLIB_INIT_FUNCTION (builtin_tcp_server_main_init);
430
431 /*
432 * fd.io coding-style-patch-verification: ON
433 *
434 * Local Variables:
435 * eval: (c-set-style "gnu")
436 * End:
437 */