VPP-659 TCP 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   vlib_main_t *vlib_main;
26 } builtin_server_main_t;
27
28 builtin_server_main_t builtin_server_main;
29
30
31 int
32 builtin_session_accept_callback (stream_session_t * s)
33 {
34   builtin_server_main_t *bsm = &builtin_server_main;
35   clib_warning ("called...");
36
37   bsm->vpp_queue[s->thread_index] =
38     session_manager_get_vpp_event_queue (s->thread_index);
39   s->session_state = SESSION_STATE_READY;
40   return 0;
41 }
42
43 void
44 builtin_session_disconnect_callback (stream_session_t * s)
45 {
46   clib_warning ("called...");
47
48   vnet_disconnect_session (s->session_index, s->thread_index);
49 }
50
51 void
52 builtin_session_reset_callback (stream_session_t * s)
53 {
54   clib_warning ("called.. ");
55
56   stream_session_cleanup (s);
57 }
58
59
60 int
61 builtin_session_connected_callback (u32 client_index,
62                                     stream_session_t * s, u8 is_fail)
63 {
64   clib_warning ("called...");
65   return -1;
66 }
67
68 int
69 builtin_add_segment_callback (u32 client_index,
70                               const u8 * seg_name, u32 seg_size)
71 {
72   clib_warning ("called...");
73   return -1;
74 }
75
76 int
77 builtin_redirect_connect_callback (u32 client_index, void *mp)
78 {
79   clib_warning ("called...");
80   return -1;
81 }
82
83 int
84 builtin_server_rx_callback (stream_session_t * s, session_fifo_event_t * e)
85 {
86   int n_written, bytes, total_copy_bytes;
87   int n_read;
88   svm_fifo_t *tx_fifo;
89   builtin_server_main_t *bsm = &builtin_server_main;
90   session_fifo_event_t evt;
91   static int serial_number = 0;
92
93   bytes = e->enqueue_length;
94   if (PREDICT_FALSE (bytes <= 0))
95     {
96       clib_warning ("bizarre rx callback: bytes %d", bytes);
97       return 0;
98     }
99
100   tx_fifo = s->server_tx_fifo;
101
102   /* Number of bytes we're going to copy */
103   total_copy_bytes = (bytes < (tx_fifo->nitems - tx_fifo->cursize)) ? bytes :
104     tx_fifo->nitems - tx_fifo->cursize;
105
106   if (PREDICT_FALSE (total_copy_bytes <= 0))
107     {
108       clib_warning ("no space in tx fifo, event had %d bytes", bytes);
109       return 0;
110     }
111
112   vec_validate (bsm->rx_buf, total_copy_bytes - 1);
113   _vec_len (bsm->rx_buf) = total_copy_bytes;
114
115   n_read = svm_fifo_dequeue_nowait (s->server_rx_fifo, 0, total_copy_bytes,
116                                     bsm->rx_buf);
117   ASSERT (n_read == total_copy_bytes);
118
119   /*
120    * Echo back
121    */
122
123   n_written = svm_fifo_enqueue_nowait (tx_fifo, 0, n_read, bsm->rx_buf);
124   ASSERT (n_written == total_copy_bytes);
125
126   /* Fabricate TX event, send to vpp */
127   evt.fifo = tx_fifo;
128   evt.event_type = FIFO_EVENT_SERVER_TX;
129   evt.enqueue_length = total_copy_bytes;
130   evt.event_id = serial_number++;
131
132   unix_shared_memory_queue_add (bsm->vpp_queue[s->thread_index], (u8 *) & evt,
133                                 0 /* do wait for mutex */ );
134
135   return 0;
136 }
137
138 static session_cb_vft_t builtin_session_cb_vft = {
139   .session_accept_callback = builtin_session_accept_callback,
140   .session_disconnect_callback = builtin_session_disconnect_callback,
141   .session_connected_callback = builtin_session_connected_callback,
142   .add_segment_callback = builtin_add_segment_callback,
143   .redirect_connect_callback = builtin_redirect_connect_callback,
144   .builtin_server_rx_callback = builtin_server_rx_callback,
145   .session_reset_callback = builtin_session_reset_callback
146 };
147
148 static int
149 server_create (vlib_main_t * vm)
150 {
151   vnet_bind_args_t _a, *a = &_a;
152   u64 options[SESSION_OPTIONS_N_OPTIONS];
153   char segment_name[128];
154   u32 num_threads;
155   vlib_thread_main_t *vtm = vlib_get_thread_main ();
156
157   num_threads = 1 /* main thread */  + vtm->n_threads;
158   vec_validate (builtin_server_main.vpp_queue, num_threads - 1);
159
160   memset (a, 0, sizeof (*a));
161   memset (options, 0, sizeof (options));
162
163   a->uri = "tcp://0.0.0.0/80";
164   a->api_client_index = ~0;
165   a->session_cb_vft = &builtin_session_cb_vft;
166   a->options = options;
167   a->options[SESSION_OPTIONS_SEGMENT_SIZE] = 256 << 10;
168   a->options[SESSION_OPTIONS_RX_FIFO_SIZE] = 64 << 10;
169   a->options[SESSION_OPTIONS_TX_FIFO_SIZE] = 64 << 10;
170   a->segment_name = segment_name;
171   a->segment_name_length = ARRAY_LEN (segment_name);
172
173   return vnet_bind_uri (a);
174 }
175
176 static clib_error_t *
177 server_create_command_fn (vlib_main_t * vm,
178                           unformat_input_t * input, vlib_cli_command_t * cmd)
179 {
180   int rv;
181 #if 0
182   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
183     {
184       if (unformat (input, "whatever %d", &whatever))
185         ;
186       else
187         return clib_error_return (0, "unknown input `%U'",
188                                   format_unformat_error, input);
189     }
190 #endif
191
192   vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ );
193   rv = server_create (vm);
194   switch (rv)
195     {
196     case 0:
197       break;
198     default:
199       return clib_error_return (0, "server_create returned %d", rv);
200     }
201   return 0;
202 }
203
204 /* *INDENT-OFF* */
205 VLIB_CLI_COMMAND (server_create_command, static) =
206 {
207   .path = "test server",
208   .short_help = "test server",
209   .function = server_create_command_fn,
210 };
211 /* *INDENT-ON* */
212
213 /*
214 * fd.io coding-style-patch-verification: ON
215 *
216 * Local Variables:
217 * eval: (c-set-style "gnu")
218 * End:
219 */