TCP/session improvements
[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       /* Program self-tap to retry */
131       if (svm_fifo_set_event (rx_fifo))
132         {
133           evt.fifo = rx_fifo;
134           evt.event_type = FIFO_EVENT_BUILTIN_RX;
135           evt.event_id = 0;
136           unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
137                                         (u8 *) & evt,
138                                         0 /* do wait for mutex */ );
139         }
140
141       return 0;
142     }
143
144   svm_fifo_unset_event (rx_fifo);
145
146   vec_validate (bsm->rx_buf, max_transfer - 1);
147   _vec_len (bsm->rx_buf) = max_transfer;
148
149   actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, 0, max_transfer,
150                                              bsm->rx_buf);
151   ASSERT (actual_transfer == max_transfer);
152
153 //  test_bytes (bsm, actual_transfer);
154
155   /*
156    * Echo back
157    */
158
159   n_written =
160     svm_fifo_enqueue_nowait (tx_fifo, 0, actual_transfer, bsm->rx_buf);
161   ASSERT (n_written == max_transfer);
162
163   if (svm_fifo_set_event (tx_fifo))
164     {
165       /* Fabricate TX event, send to vpp */
166       evt.fifo = tx_fifo;
167       evt.event_type = FIFO_EVENT_SERVER_TX;
168       evt.event_id = serial_number++;
169
170       unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index],
171                                     (u8 *) & evt, 0 /* do wait for mutex */ );
172     }
173
174   return 0;
175 }
176
177 static session_cb_vft_t builtin_session_cb_vft = {
178   .session_accept_callback = builtin_session_accept_callback,
179   .session_disconnect_callback = builtin_session_disconnect_callback,
180   .session_connected_callback = builtin_session_connected_callback,
181   .add_segment_callback = builtin_add_segment_callback,
182   .redirect_connect_callback = builtin_redirect_connect_callback,
183   .builtin_server_rx_callback = builtin_server_rx_callback,
184   .session_reset_callback = builtin_session_reset_callback
185 };
186
187 static int
188 server_create (vlib_main_t * vm)
189 {
190   vnet_bind_args_t _a, *a = &_a;
191   u64 options[SESSION_OPTIONS_N_OPTIONS];
192   char segment_name[128];
193   u32 num_threads;
194   vlib_thread_main_t *vtm = vlib_get_thread_main ();
195
196   num_threads = 1 /* main thread */  + vtm->n_threads;
197   vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
198
199   memset (a, 0, sizeof (*a));
200   memset (options, 0, sizeof (options));
201
202   a->uri = "tcp://0.0.0.0/1234";
203   a->api_client_index = ~0;
204   a->session_cb_vft = &builtin_session_cb_vft;
205   a->options = options;
206   a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 128 << 20;
207   a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 64 << 10;
208   a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 64 << 10;
209   a->segment_name = segment_name;
210   a->segment_name_length = ARRAY_LEN (segment_name);
211
212   return vnet_bind_uri (a);
213 }
214
215 static clib_error_t *
216 server_create_command_fn (vlib_main_t * vm,
217                           unformat_input_t * input, vlib_cli_command_t * cmd)
218 {
219   int rv;
220 #if 0
221   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
222     {
223       if (unformat (input, "whatever %d", &whatever))
224         ;
225       else
226         return clib_error_return (0, "unknown input `%U'",
227                                   format_unformat_error, input);
228     }
229 #endif
230
231   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
232   rv = server_create (vm);
233   switch (rv)
234     {
235     case 0:
236       break;
237     default:
238       return clib_error_return (0, "server_create returned %d", rv);
239     }
240   return 0;
241 }
242
243 /* *INDENT-OFF* */
244 VLIB_CLI_COMMAND (server_create_command, static) =
245 {
246   .path = "test server",
247   .short_help = "test server",
248   .function = server_create_command_fn,
249 };
250 /* *INDENT-ON* */
251
252 /*
253 * fd.io coding-style-patch-verification: ON
254 *
255 * Local Variables:
256 * eval: (c-set-style "gnu")
257 * End:
258 */