917d4bd3b9439759c413f52723f6c5b41491224d
[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 typedef struct
22 {
23   u8 *rx_buf;
24   unix_shared_memory_queue_t **vpp_queue;
25   u32 byte_index;
26   vlib_main_t *vlib_main;
27 } builtin_server_main_t;
28
29 builtin_server_main_t builtin_server_main;
30
31
32 int
33 builtin_session_accept_callback (stream_session_t * s)
34 {
35   builtin_server_main_t *bsm = &builtin_server_main;
36   clib_warning ("called...");
37
38   bsm->vpp_queue[s->thread_index] =
39     session_manager_get_vpp_event_queue (s->thread_index);
40   s->session_state = SESSION_STATE_READY;
41   bsm->byte_index = 0;
42   return 0;
43 }
44
45 void
46 builtin_session_disconnect_callback (stream_session_t * s)
47 {
48   clib_warning ("called...");
49
50   vnet_disconnect_session (s->session_index, s->thread_index);
51 }
52
53 void
54 builtin_session_reset_callback (stream_session_t * s)
55 {
56   clib_warning ("called.. ");
57
58   stream_session_cleanup (s);
59 }
60
61
62 int
63 builtin_session_connected_callback (u32 client_index,
64                                     stream_session_t * s, u8 is_fail)
65 {
66   clib_warning ("called...");
67   return -1;
68 }
69
70 int
71 builtin_add_segment_callback (u32 client_index,
72                               const u8 * seg_name, u32 seg_size)
73 {
74   clib_warning ("called...");
75   return -1;
76 }
77
78 int
79 builtin_redirect_connect_callback (u32 client_index, void *mp)
80 {
81   clib_warning ("called...");
82   return -1;
83 }
84
85 void
86 test_bytes (builtin_server_main_t * bsm, int actual_transfer)
87 {
88   int i;
89
90   for (i = 0; i < actual_transfer; i++)
91     {
92       if (bsm->rx_buf[i] != ((bsm->byte_index + i) & 0xff))
93         {
94           clib_warning ("at %d expected %d got %d", bsm->byte_index + i,
95                         (bsm->byte_index + i) & 0xff, bsm->rx_buf[i]);
96         }
97     }
98   bsm->byte_index += actual_transfer;
99 }
100
101 int
102 builtin_server_rx_callback (stream_session_t * s)
103 {
104   u32 n_written, max_dequeue, max_enqueue, max_transfer;
105   int actual_transfer;
106   svm_fifo_t *tx_fifo, *rx_fifo;
107   builtin_server_main_t *bsm = &builtin_server_main;
108   session_fifo_event_t evt;
109   static int serial_number = 0;
110
111   max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo);
112   max_enqueue = svm_fifo_max_enqueue (s->server_tx_fifo);
113
114   if (PREDICT_FALSE (max_dequeue == 0))
115     {
116       return 0;
117     }
118
119   tx_fifo = s->server_tx_fifo;
120   rx_fifo = s->server_rx_fifo;
121
122   /* Number of bytes we're going to copy */
123   max_transfer = (max_dequeue < max_enqueue) ? max_dequeue : max_enqueue;
124
125   /* No space in tx fifo */
126   if (PREDICT_FALSE (max_transfer == 0))
127     {
128       /* XXX timeout for session that are stuck */
129
130     rx_event:
131       /* Program self-tap to retry */
132       if (svm_fifo_set_event (rx_fifo))
133         {
134           evt.fifo = rx_fifo;
135           evt.event_type = FIFO_EVENT_BUILTIN_RX;
136           evt.event_id = 0;
137           unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
138                                         (u8 *) & evt,
139                                         0 /* do wait for mutex */ );
140         }
141
142       return 0;
143     }
144
145   svm_fifo_unset_event (rx_fifo);
146
147   vec_validate (bsm->rx_buf, max_transfer - 1);
148   _vec_len (bsm->rx_buf) = max_transfer;
149
150   actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, 0, max_transfer,
151                                              bsm->rx_buf);
152   ASSERT (actual_transfer == max_transfer);
153
154 //  test_bytes (bsm, actual_transfer);
155
156   /*
157    * Echo back
158    */
159
160   n_written =
161     svm_fifo_enqueue_nowait (tx_fifo, 0, actual_transfer, bsm->rx_buf);
162
163   if (n_written != max_transfer)
164     clib_warning ("short trout!");
165
166   if (svm_fifo_set_event (tx_fifo))
167     {
168       /* Fabricate TX event, send to vpp */
169       evt.fifo = tx_fifo;
170       evt.event_type = FIFO_EVENT_SERVER_TX;
171       evt.event_id = serial_number++;
172
173       unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
174                                     (u8 *) & evt, 0 /* do wait for mutex */ );
175     }
176
177   if (PREDICT_FALSE (max_enqueue < max_dequeue))
178     goto rx_event;
179
180   return 0;
181 }
182
183 static session_cb_vft_t builtin_session_cb_vft = {
184   .session_accept_callback = builtin_session_accept_callback,
185   .session_disconnect_callback = builtin_session_disconnect_callback,
186   .session_connected_callback = builtin_session_connected_callback,
187   .add_segment_callback = builtin_add_segment_callback,
188   .redirect_connect_callback = builtin_redirect_connect_callback,
189   .builtin_server_rx_callback = builtin_server_rx_callback,
190   .session_reset_callback = builtin_session_reset_callback
191 };
192
193 static int
194 server_create (vlib_main_t * vm)
195 {
196   vnet_bind_args_t _a, *a = &_a;
197   u64 options[SESSION_OPTIONS_N_OPTIONS];
198   char segment_name[128];
199   u32 num_threads;
200   vlib_thread_main_t *vtm = vlib_get_thread_main ();
201
202   num_threads = 1 /* main thread */  + vtm->n_threads;
203   vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
204
205   memset (a, 0, sizeof (*a));
206   memset (options, 0, sizeof (options));
207
208   a->uri = "tcp://0.0.0.0/1234";
209   a->api_client_index = ~0;
210   a->session_cb_vft = &builtin_session_cb_vft;
211   a->options = options;
212   a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
213   a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 1 << 16;
214   a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 1 << 16;
215   a->segment_name = segment_name;
216   a->segment_name_length = ARRAY_LEN (segment_name);
217
218   return vnet_bind_uri (a);
219 }
220
221 static clib_error_t *
222 server_create_command_fn (vlib_main_t * vm,
223                           unformat_input_t * input, vlib_cli_command_t * cmd)
224 {
225   int rv;
226 #if 0
227   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
228     {
229       if (unformat (input, "whatever %d", &whatever))
230         ;
231       else
232         return clib_error_return (0, "unknown input `%U'",
233                                   format_unformat_error, input);
234     }
235 #endif
236
237   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
238   rv = server_create (vm);
239   switch (rv)
240     {
241     case 0:
242       break;
243     default:
244       return clib_error_return (0, "server_create returned %d", rv);
245     }
246   return 0;
247 }
248
249 /* *INDENT-OFF* */
250 VLIB_CLI_COMMAND (server_create_command, static) =
251 {
252   .path = "test server",
253   .short_help = "test server",
254   .function = server_create_command_fn,
255 };
256 /* *INDENT-ON* */
257
258 /*
259 * fd.io coding-style-patch-verification: ON
260 *
261 * Local Variables:
262 * eval: (c-set-style "gnu")
263 * End:
264 */