api: refactor vlibmemory
[vpp.git] / src / vnet / udp / builtin_server.c
1 /*
2  * Copyright (c) 2016 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 /** @file
17     udp builtin server
18 */
19
20 #include <vnet/udp/udp.h>
21 #include <vnet/session/session.h>
22 #include <vnet/session/application_interface.h>
23
24 /** per-worker built-in server copy buffers */
25 u8 **copy_buffers;
26 static int app_index = ~0;
27
28 static int
29 builtin_session_create_callback (stream_session_t * s)
30 {
31   /* Simple version: declare session ready-to-go... */
32   s->session_state = SESSION_STATE_READY;
33   return 0;
34 }
35
36 static void
37 builtin_session_disconnect_callback (stream_session_t * s)
38 {
39   stream_session_disconnect (s);
40 }
41
42 static void
43 builtin_session_reset_callback (stream_session_t * s)
44 {
45   clib_warning ("Reset session %U", format_stream_session, s, 2);
46   stream_session_cleanup (s);
47 }
48
49 static int
50 builtin_session_connected_callback (u32 app_index, u32 api_context,
51                                     stream_session_t * s, u8 is_fail)
52 {
53   clib_warning ("called...");
54   return -1;
55 }
56
57 static int
58 builtin_server_rx_callback (stream_session_t * s)
59 {
60   svm_fifo_t *rx_fifo, *tx_fifo;
61   u32 this_transfer, max_deq, max_enq;
62   int actual_transfer;
63   u8 *my_copy_buffer;
64   session_fifo_event_t evt;
65   svm_queue_t *q;
66
67   my_copy_buffer = copy_buffers[s->thread_index];
68   rx_fifo = s->server_rx_fifo;
69   tx_fifo = s->server_tx_fifo;
70
71   max_deq = svm_fifo_max_dequeue (rx_fifo);
72   max_enq = svm_fifo_max_enqueue (tx_fifo);
73   this_transfer = max_enq < max_deq ? max_enq : max_deq;
74
75   vec_validate (my_copy_buffer, this_transfer - 1);
76   _vec_len (my_copy_buffer) = this_transfer;
77
78   actual_transfer = svm_fifo_dequeue_nowait (rx_fifo, this_transfer,
79                                              my_copy_buffer);
80   ASSERT (actual_transfer == this_transfer);
81   actual_transfer = svm_fifo_enqueue_nowait (tx_fifo, this_transfer,
82                                              my_copy_buffer);
83   ASSERT (actual_transfer == this_transfer);
84
85   copy_buffers[s->thread_index] = my_copy_buffer;
86
87   if (svm_fifo_set_event (tx_fifo))
88     {
89       /* Fabricate TX event, send to ourselves */
90       evt.fifo = tx_fifo;
91       evt.event_type = FIFO_EVENT_APP_TX;
92       q = session_manager_get_vpp_event_queue (s->thread_index);
93       svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
94     }
95
96   return 0;
97 }
98
99 /* *INDENT-OFF* */
100 static session_cb_vft_t builtin_server = {
101     .session_accept_callback = builtin_session_create_callback,
102     .session_connected_callback = builtin_session_connected_callback,
103     .session_disconnect_callback = builtin_session_disconnect_callback,
104     .builtin_server_rx_callback = builtin_server_rx_callback,
105     .session_reset_callback = builtin_session_reset_callback
106 };
107 /* *INDENT-ON* */
108
109 static int
110 attach_builtin_uri_server ()
111 {
112   vnet_app_attach_args_t _a, *a = &_a;
113   u8 segment_name[128];
114   u32 segment_name_length;
115   u64 options[16];
116
117   segment_name_length = ARRAY_LEN (segment_name);
118
119   memset (a, 0, sizeof (*a));
120   memset (options, 0, sizeof (options));
121
122   a->api_client_index = ~0;
123   a->segment_name = segment_name;
124   a->segment_name_length = segment_name_length;
125   a->session_cb_vft = &builtin_server;
126
127   options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678;
128   options[APP_OPTIONS_SEGMENT_SIZE] = (2 << 30);        /*$$$$ config / arg */
129   options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
130   options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 1024;
131
132   a->options = options;
133
134   if (vnet_application_attach (a))
135     return -1;
136
137   app_index = a->app_index;
138   return 0;
139 }
140
141 static int
142 bind_builtin_uri_server (u8 * uri)
143 {
144   vnet_bind_args_t _a, *a = &_a;
145   int rv;
146
147   rv = attach_builtin_uri_server ();
148   if (rv)
149     return rv;
150
151   memset (a, 0, sizeof (*a));
152   a->uri = (char *) uri;
153   a->app_index = app_index;
154
155   rv = vnet_bind_uri (a);
156
157   return rv;
158 }
159
160 static int
161 unbind_builtin_uri_server (u8 * uri)
162 {
163   vnet_unbind_args_t _a, *a = &_a;
164
165   a->app_index = app_index;
166   a->uri = (char *) uri;
167
168   return vnet_unbind_uri (a);
169 }
170
171 static clib_error_t *
172 builtin_server_init (vlib_main_t * vm)
173 {
174   vlib_thread_main_t *vtm = vlib_get_thread_main ();
175   u32 num_threads;
176
177   num_threads = 1 /* main thread */  + vtm->n_threads;
178
179   vec_validate (copy_buffers, num_threads - 1);
180   return 0;
181 }
182
183 VLIB_INIT_FUNCTION (builtin_server_init);
184
185 static clib_error_t *
186 builtin_uri_bind_command_fn (vlib_main_t * vm,
187                              unformat_input_t * input,
188                              vlib_cli_command_t * cmd)
189 {
190   u8 *uri = 0;
191   int rv;
192
193   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
194     {
195       if (unformat (input, "uri %s", &uri))
196         ;
197       else
198         break;
199     }
200
201   if (uri == 0)
202     return clib_error_return (0, "uri to bind not specified...");
203
204   vnet_session_enable_disable (vm, 1 /* turn on UDP, etc. */ );
205
206   rv = bind_builtin_uri_server (uri);
207
208   vec_free (uri);
209
210   switch (rv)
211     {
212     case 0:
213       break;
214
215     default:
216       return clib_error_return (0, "bind_uri_server returned %d", rv);
217       break;
218     }
219
220   return 0;
221 }
222
223 /* *INDENT-OFF* */
224 VLIB_CLI_COMMAND (builtin_uri_bind_command, static) =
225 {
226   .path = "builtin uri bind",
227   .short_help = "builtin uri bind",
228   .function = builtin_uri_bind_command_fn,
229 };
230 /* *INDENT-ON* */
231
232 static clib_error_t *
233 builtin_uri_unbind_command_fn (vlib_main_t * vm,
234                                unformat_input_t * input,
235                                vlib_cli_command_t * cmd)
236 {
237   u8 *uri = 0;
238   int rv;
239
240   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
241     {
242       if (unformat (input, "uri %s", &uri))
243         ;
244       else
245         break;
246     }
247
248   if (uri == 0)
249     return clib_error_return (0, "uri to unbind not specified...");
250
251   rv = unbind_builtin_uri_server (uri);
252
253   vec_free (uri);
254
255   switch (rv)
256     {
257     case 0:
258       break;
259
260     default:
261       return clib_error_return (0, "unbind_uri_server returned %d", rv);
262       break;
263     }
264
265   return 0;
266 }
267
268 /* *INDENT-OFF* */
269 VLIB_CLI_COMMAND (builtin_uri_unbind_command, static) =
270 {
271   .path = "builtin uri unbind",
272   .short_help = "builtin uri unbind",
273   .function = builtin_uri_unbind_command_fn,
274 };
275 /* *INDENT-ON* */
276
277 /*
278  * fd.io coding-style-patch-verification: ON
279  *
280  * Local Variables:
281  * eval: (c-set-style "gnu")
282  * End:
283  */