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