blob: 32ef34b09525ec1600b8a7deecfc29642cf4465f [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2015-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#include <vnet/vnet.h>
17#include <vlibmemory/api.h>
18#include <vnet/session/application.h>
Florin Coras1c710452017-10-17 00:03:13 -070019#include <vnet/session/application_interface.h>
20#include <vnet/session/session_rules_table.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050021
22#include <vnet/vnet_msg_enum.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050023
24#define vl_typedefs /* define message structures */
25#include <vnet/vnet_all_api_h.h>
26#undef vl_typedefs
27
28#define vl_endianfun /* define message structures */
29#include <vnet/vnet_all_api_h.h>
30#undef vl_endianfun
31
32/* instantiate all the print functions we know about */
33#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
34#define vl_printfun
35#include <vnet/vnet_all_api_h.h>
36#undef vl_printfun
37
38#include <vlibapi/api_helper_macros.h>
39
40#define foreach_session_api_msg \
41_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070042_(APPLICATION_ATTACH, application_attach) \
43_(APPLICATION_DETACH, application_detach) \
Dave Barach68b0fb02017-02-28 15:15:56 -050044_(BIND_URI, bind_uri) \
45_(UNBIND_URI, unbind_uri) \
46_(CONNECT_URI, connect_uri) \
47_(DISCONNECT_SESSION, disconnect_session) \
48_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
49_(ACCEPT_SESSION_REPLY, accept_session_reply) \
50_(RESET_SESSION_REPLY, reset_session_reply) \
Florin Corascea194d2017-10-02 00:18:51 -070051_(BIND_SOCK, bind_sock) \
Dave Barach68b0fb02017-02-28 15:15:56 -050052_(UNBIND_SOCK, unbind_sock) \
53_(CONNECT_SOCK, connect_sock) \
Florin Corase04c2992017-03-01 08:17:34 -080054_(SESSION_ENABLE_DISABLE, session_enable_disable) \
Florin Corascea194d2017-10-02 00:18:51 -070055_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
Florin Coras1c710452017-10-17 00:03:13 -070056_(SESSION_RULE_ADD_DEL, session_rule_add_del) \
Florin Corase04c2992017-03-01 08:17:34 -080057
Dave Barach68b0fb02017-02-28 15:15:56 -050058static int
59send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
60 u32 segment_size)
61{
62 vl_api_map_another_segment_t *mp;
63 unix_shared_memory_queue_t *q;
64
65 q = vl_api_client_index_to_input_queue (api_client_index);
66
67 if (!q)
68 return -1;
69
70 mp = vl_msg_api_alloc (sizeof (*mp));
71 memset (mp, 0, sizeof (*mp));
72 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
73 mp->segment_size = segment_size;
74 strncpy ((char *) mp->segment_name, (char *) segment_name,
75 sizeof (mp->segment_name) - 1);
76
77 vl_msg_api_send_shmem (q, (u8 *) & mp);
78
79 return 0;
80}
81
82static int
Florin Coras6cf30ad2017-04-04 23:08:23 -070083send_session_accept_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -050084{
85 vl_api_accept_session_t *mp;
86 unix_shared_memory_queue_t *q, *vpp_queue;
87 application_t *server = application_get (s->app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -070088 transport_connection_t *tc;
89 transport_proto_vft_t *tp_vft;
90 stream_session_t *listener;
Dave Barach68b0fb02017-02-28 15:15:56 -050091
92 q = vl_api_client_index_to_input_queue (server->api_client_index);
93 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
94
95 if (!q)
96 return -1;
97
98 mp = vl_msg_api_alloc (sizeof (*mp));
Florin Coras6cf30ad2017-04-04 23:08:23 -070099 memset (mp, 0, sizeof (*mp));
100
Dave Barach68b0fb02017-02-28 15:15:56 -0500101 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
Florin Corasa5464812017-04-19 13:00:05 -0700102 mp->context = server->index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700103 listener = listen_session_get (s->session_type, s->listener_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700104 tp_vft = transport_protocol_get_vft (s->session_type);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700105 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
106 mp->listener_handle = listen_session_get_handle (listener);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700107 mp->handle = session_handle (s);
Damjan Marion7bee80c2017-04-26 15:32:12 +0200108 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
109 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
110 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700111 mp->port = tc->rmt_port;
112 mp->is_ip4 = tc->is_ip4;
113 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Dave Barach68b0fb02017-02-28 15:15:56 -0500114 vl_msg_api_send_shmem (q, (u8 *) & mp);
115
116 return 0;
117}
118
119static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700120send_session_disconnect_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500121{
122 vl_api_disconnect_session_t *mp;
123 unix_shared_memory_queue_t *q;
124 application_t *app = application_get (s->app_index);
125
126 q = vl_api_client_index_to_input_queue (app->api_client_index);
127
128 if (!q)
129 return;
130
131 mp = vl_msg_api_alloc (sizeof (*mp));
132 memset (mp, 0, sizeof (*mp));
133 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700134 mp->handle = session_handle (s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500135 vl_msg_api_send_shmem (q, (u8 *) & mp);
136}
137
Florin Corasd79b41e2017-03-04 05:37:52 -0800138static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700139send_session_reset_callback (stream_session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800140{
141 vl_api_reset_session_t *mp;
142 unix_shared_memory_queue_t *q;
143 application_t *app = application_get (s->app_index);
144
145 q = vl_api_client_index_to_input_queue (app->api_client_index);
146
147 if (!q)
148 return;
149
150 mp = vl_msg_api_alloc (sizeof (*mp));
151 memset (mp, 0, sizeof (*mp));
152 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700153 mp->handle = session_handle (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800154 vl_msg_api_send_shmem (q, (u8 *) & mp);
155}
156
Dave Barach0194f1a2017-05-15 10:11:39 -0400157int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700158send_session_connected_callback (u32 app_index, u32 api_context,
159 stream_session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500160{
Dave Wallace33e002b2017-09-06 01:20:02 -0400161 vl_api_connect_session_reply_t *mp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500162 unix_shared_memory_queue_t *q;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700163 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500164 unix_shared_memory_queue_t *vpp_queue;
Florin Corasade70e42017-10-14 18:56:41 -0700165 transport_connection_t *tc;
Dave Barach68b0fb02017-02-28 15:15:56 -0500166
Florin Coras6cf30ad2017-04-04 23:08:23 -0700167 app = application_get (app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500168 q = vl_api_client_index_to_input_queue (app->api_client_index);
169
170 if (!q)
171 return -1;
172
173 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Wallace33e002b2017-09-06 01:20:02 -0400174 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700175 mp->context = api_context;
Dave Wallace965fec92017-10-17 16:19:41 -0400176
177 if (is_fail)
178 goto done;
179
180 tc = session_get_transport (s);
181 if (!tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500182 {
Dave Wallace965fec92017-10-17 16:19:41 -0400183 is_fail = 1;
184 goto done;
Florin Corase04c2992017-03-01 08:17:34 -0800185 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500186
Dave Wallace965fec92017-10-17 16:19:41 -0400187 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
188 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
189 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
190 mp->handle = session_handle (s);
191 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
192 clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
193 mp->is_ip4 = tc->is_ip4;
194 mp->lcl_port = tc->lcl_port;
195
196done:
197 mp->retval = is_fail ?
198 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500199 vl_msg_api_send_shmem (q, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500200 return 0;
201}
202
203/**
204 * Redirect a connect_uri message to the indicated server.
205 * Only sent if the server has bound the related port with
206 * URI_OPTIONS_FLAGS_USE_FIFO
207 */
208static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700209redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500210{
Florin Corascea194d2017-10-02 00:18:51 -0700211 vl_api_connect_sock_t *mp = mp_arg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500212 unix_shared_memory_queue_t *server_q, *client_q;
213 vlib_main_t *vm = vlib_get_main ();
214 f64 timeout = vlib_time_now (vm) + 0.5;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700215 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500216 int rv = 0;
217
218 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
219
220 if (!server_q)
221 {
222 rv = VNET_API_ERROR_INVALID_VALUE;
223 goto out;
224 }
225
226 client_q = vl_api_client_index_to_input_queue (mp->client_index);
227 if (!client_q)
228 {
229 rv = VNET_API_ERROR_INVALID_VALUE_2;
230 goto out;
231 }
232
233 /* Tell the server the client's API queue address, so it can reply */
Damjan Marion7bee80c2017-04-26 15:32:12 +0200234 mp->client_queue_address = pointer_to_uword (client_q);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700235 app = application_lookup (mp->client_index);
Florin Coras82b13a82017-04-25 11:58:06 -0700236 if (!app)
237 {
238 clib_warning ("no client application");
239 return -1;
240 }
241
Florin Coras6cf30ad2017-04-04 23:08:23 -0700242 mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = app->sm_properties.rx_fifo_size;
243 mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = app->sm_properties.tx_fifo_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500244
245 /*
246 * Bounce message handlers MUST NOT block the data-plane.
247 * Spin waiting for the queue lock, but
248 */
249
250 while (vlib_time_now (vm) < timeout)
251 {
252 rv =
253 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
254 switch (rv)
255 {
256 /* correctly enqueued */
257 case 0:
Florin Corascea194d2017-10-02 00:18:51 -0700258 return VNET_API_ERROR_SESSION_REDIRECT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500259
260 /* continue spinning, wait for pthread_mutex_trylock to work */
261 case -1:
262 continue;
263
264 /* queue stuffed, drop the msg */
265 case -2:
266 rv = VNET_API_ERROR_QUEUE_FULL;
267 goto out;
268 }
269 }
270out:
271 /* Dispose of the message */
272 vl_msg_api_free (mp);
273 return rv;
274}
275
Florin Corascea194d2017-10-02 00:18:51 -0700276static session_cb_vft_t session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500277 .session_accept_callback = send_session_accept_callback,
278 .session_disconnect_callback = send_session_disconnect_callback,
279 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800280 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500281 .add_segment_callback = send_add_segment_callback,
282 .redirect_connect_callback = redirect_connect_callback
283};
284
Dave Barach68b0fb02017-02-28 15:15:56 -0500285static void
Florin Corase04c2992017-03-01 08:17:34 -0800286vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
287{
288 vl_api_session_enable_disable_reply_t *rmp;
289 vlib_main_t *vm = vlib_get_main ();
290 int rv = 0;
291
292 vnet_session_enable_disable (vm, mp->is_enable);
293 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
294}
295
296static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700297vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500298{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700299 vl_api_application_attach_reply_t *rmp;
300 vnet_app_attach_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -0700301 clib_error_t *error = 0;
302 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500303
Florin Coras6cf30ad2017-04-04 23:08:23 -0700304 if (session_manager_is_enabled () == 0)
305 {
306 rv = VNET_API_ERROR_FEATURE_DISABLED;
307 goto done;
308 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500309
Florin Coras6cf30ad2017-04-04 23:08:23 -0700310 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
311 sizeof (mp->options),
312 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500313
314 memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500315 a->api_client_index = mp->client_index;
316 a->options = mp->options;
Florin Corascea194d2017-10-02 00:18:51 -0700317 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500318
Florin Corascea194d2017-10-02 00:18:51 -0700319 if (mp->namespace_id_len > 64)
320 {
321 rv = VNET_API_ERROR_INVALID_VALUE;
322 goto done;
323 }
324
325 if (mp->namespace_id_len)
326 {
Dave Wallace8af20542017-10-26 03:29:30 -0400327 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Florin Corascea194d2017-10-02 00:18:51 -0700328 clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
329 }
330
331 if ((error = vnet_application_attach (a)))
332 {
333 rv = clib_error_get_code (error);
334 clib_error_report (error);
335 }
336 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500337
Florin Coras6cf30ad2017-04-04 23:08:23 -0700338done:
Florin Corasa5464812017-04-19 13:00:05 -0700339
Dave Barach68b0fb02017-02-28 15:15:56 -0500340 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700341 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500342 if (!rv)
343 {
344 rmp->segment_name_length = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700345 rmp->segment_size = a->segment_size;
346 if (a->segment_name_length)
Dave Barach68b0fb02017-02-28 15:15:56 -0500347 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700348 memcpy (rmp->segment_name, a->segment_name,
349 a->segment_name_length);
350 rmp->segment_name_length = a->segment_name_length;
Dave Barach68b0fb02017-02-28 15:15:56 -0500351 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700352 rmp->app_event_queue_address = a->app_event_queue_address;
Dave Barach68b0fb02017-02-28 15:15:56 -0500353 }
354 }));
355 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500356}
357
358static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700359vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
360{
361 vl_api_application_detach_reply_t *rmp;
362 int rv = VNET_API_ERROR_INVALID_VALUE_2;
363 vnet_app_detach_args_t _a, *a = &_a;
364 application_t *app;
365
366 if (session_manager_is_enabled () == 0)
367 {
368 rv = VNET_API_ERROR_FEATURE_DISABLED;
369 goto done;
370 }
371
372 app = application_lookup (mp->client_index);
373 if (app)
374 {
375 a->app_index = app->index;
376 rv = vnet_application_detach (a);
377 }
378
379done:
380 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
381}
382
383static void
384vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
385{
386 vl_api_bind_uri_reply_t *rmp;
387 vnet_bind_args_t _a, *a = &_a;
388 application_t *app;
389 int rv;
390
391 if (session_manager_is_enabled () == 0)
392 {
393 rv = VNET_API_ERROR_FEATURE_DISABLED;
394 goto done;
395 }
396
397 app = application_lookup (mp->client_index);
398 if (app)
399 {
400 memset (a, 0, sizeof (*a));
401 a->uri = (char *) mp->uri;
402 a->app_index = app->index;
403 rv = vnet_bind_uri (a);
404 }
405 else
406 {
407 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
408 }
409
410done:
411 REPLY_MACRO (VL_API_BIND_URI_REPLY);
412}
413
414static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500415vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
416{
417 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700418 application_t *app;
419 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500420 int rv;
421
Florin Coras6cf30ad2017-04-04 23:08:23 -0700422 if (session_manager_is_enabled () == 0)
423 {
424 rv = VNET_API_ERROR_FEATURE_DISABLED;
425 goto done;
426 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500427
Florin Coras6cf30ad2017-04-04 23:08:23 -0700428 app = application_lookup (mp->client_index);
429 if (app)
430 {
431 a->uri = (char *) mp->uri;
432 a->app_index = app->index;
433 rv = vnet_unbind_uri (a);
434 }
435 else
436 {
437 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
438 }
439
440done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500441 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
442}
443
Florin Corasf03a59a2017-06-09 21:07:32 -0700444static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500445vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
446{
Dave Wallace33e002b2017-09-06 01:20:02 -0400447 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500448 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700449 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700450 clib_error_t *error = 0;
451 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500452
Florin Coras6cf30ad2017-04-04 23:08:23 -0700453 if (session_manager_is_enabled () == 0)
454 {
455 rv = VNET_API_ERROR_FEATURE_DISABLED;
456 goto done;
457 }
Florin Corase04c2992017-03-01 08:17:34 -0800458
Florin Coras6cf30ad2017-04-04 23:08:23 -0700459 app = application_lookup (mp->client_index);
460 if (app)
461 {
462 a->uri = (char *) mp->uri;
463 a->api_context = mp->context;
464 a->app_index = app->index;
465 a->mp = mp;
Florin Corascea194d2017-10-02 00:18:51 -0700466 if ((error = vnet_connect_uri (a)))
467 {
468 rv = clib_error_get_code (error);
469 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
470 clib_error_report (error);
471 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700472 }
473 else
474 {
475 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
476 }
Florin Corase04c2992017-03-01 08:17:34 -0800477
Florin Coras3cbc04b2017-10-02 00:18:51 -0700478 /*
479 * Don't reply to stream (tcp) connects. The reply will come once
480 * the connection is established. In case of the redirects, the reply
481 * will come from the server app.
482 */
Florin Corascea194d2017-10-02 00:18:51 -0700483 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
Florin Corase04c2992017-03-01 08:17:34 -0800484 return;
485
Florin Coras6cf30ad2017-04-04 23:08:23 -0700486done:
Florin Corase04c2992017-03-01 08:17:34 -0800487 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -0400488 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800489 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500490}
491
492static void
493vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
494{
495 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700496 vnet_disconnect_args_t _a, *a = &_a;
497 application_t *app;
498 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500499
Florin Coras6cf30ad2017-04-04 23:08:23 -0700500 if (session_manager_is_enabled () == 0)
501 {
502 rv = VNET_API_ERROR_FEATURE_DISABLED;
503 goto done;
504 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500505
Florin Coras6cf30ad2017-04-04 23:08:23 -0700506 app = application_lookup (mp->client_index);
507 if (app)
508 {
509 a->handle = mp->handle;
510 a->app_index = app->index;
511 rv = vnet_disconnect_session (a);
512 }
513 else
514 {
515 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
516 }
517
518done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500519 REPLY_MACRO (VL_API_DISCONNECT_SESSION_REPLY);
520}
521
522static void
523vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
524 mp)
525{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700526 vnet_disconnect_args_t _a, *a = &_a;
527 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500528
529 /* Client objected to disconnecting the session, log and continue */
530 if (mp->retval)
531 {
532 clib_warning ("client retval %d", mp->retval);
533 return;
534 }
535
536 /* Disconnect has been confirmed. Confirm close to transport */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700537 app = application_lookup (mp->client_index);
538 if (app)
539 {
540 a->handle = mp->handle;
541 a->app_index = app->index;
542 vnet_disconnect_session (a);
543 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500544}
545
546static void
547vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
548{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700549 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500550 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700551 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500552
Florin Coras6cf30ad2017-04-04 23:08:23 -0700553 app = application_lookup (mp->client_index);
554 if (!app)
555 return;
556
Florin Corascea194d2017-10-02 00:18:51 -0700557 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700558 s = session_get_if_valid (index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700559 if (s == 0 || app->index != s->app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500560 {
561 clib_warning ("Invalid session!");
562 return;
563 }
564
565 /* Client objected to resetting the session, log and continue */
566 if (mp->retval)
567 {
568 clib_warning ("client retval %d", mp->retval);
569 return;
570 }
571
Dave Barach68b0fb02017-02-28 15:15:56 -0500572 /* This comes as a response to a reset, transport only waiting for
573 * confirmation to remove connection state, no need to disconnect */
574 stream_session_cleanup (s);
575}
576
577static void
578vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
579{
580 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700581 u32 session_index, thread_index;
Florin Corasa5464812017-04-19 13:00:05 -0700582 vnet_disconnect_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500583
Florin Corasa5464812017-04-19 13:00:05 -0700584 /* Server isn't interested, kill the session */
585 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500586 {
Florin Corasa5464812017-04-19 13:00:05 -0700587 a->app_index = mp->context;
588 a->handle = mp->handle;
589 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500590 }
Florin Corasa5464812017-04-19 13:00:05 -0700591 else
592 {
Florin Corascea194d2017-10-02 00:18:51 -0700593 session_parse_handle (mp->handle, &session_index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700594 s = session_get_if_valid (session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700595 if (!s)
596 {
597 clib_warning ("session doesn't exist");
598 return;
599 }
600 if (s->app_index != mp->context)
601 {
602 clib_warning ("app doesn't own session");
603 return;
604 }
Florin Corasa5464812017-04-19 13:00:05 -0700605 s->session_state = SESSION_STATE_READY;
606 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500607}
608
609static void
610vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
611 * mp)
612{
613 clib_warning ("not implemented");
614}
615
616static void
617vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
618{
619 vl_api_bind_sock_reply_t *rmp;
620 vnet_bind_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -0700621 int rv = 0;
622 clib_error_t *error;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700623 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500624
Florin Coras6cf30ad2017-04-04 23:08:23 -0700625 if (session_manager_is_enabled () == 0)
626 {
627 rv = VNET_API_ERROR_FEATURE_DISABLED;
628 goto done;
629 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500630
Florin Coras6cf30ad2017-04-04 23:08:23 -0700631 app = application_lookup (mp->client_index);
632 if (app)
633 {
Dave Wallace33e002b2017-09-06 01:20:02 -0400634 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700635 memset (a, 0, sizeof (*a));
Florin Corascea194d2017-10-02 00:18:51 -0700636 a->sep.is_ip4 = mp->is_ip4;
637 a->sep.ip = *ip46;
638 a->sep.port = mp->port;
639 a->sep.fib_index = mp->vrf;
640 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras3cbc04b2017-10-02 00:18:51 -0700641 a->sep.transport_proto = mp->proto;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700642 a->app_index = app->index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500643
Florin Corascea194d2017-10-02 00:18:51 -0700644 if ((error = vnet_bind (a)))
645 {
646 rv = clib_error_get_code (error);
647 clib_error_report (error);
648 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700649 }
650done:
Florin Corascea194d2017-10-02 00:18:51 -0700651 /* *INDENT-OFF* */
652 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
653 if (!rv)
654 rmp->handle = a->handle;
655 }));
656 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500657}
658
659static void
660vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
661{
662 vl_api_unbind_sock_reply_t *rmp;
663 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700664 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700665 clib_error_t *error;
666 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500667
Florin Coras6cf30ad2017-04-04 23:08:23 -0700668 if (session_manager_is_enabled () == 0)
669 {
670 rv = VNET_API_ERROR_FEATURE_DISABLED;
671 goto done;
672 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500673
Florin Coras6cf30ad2017-04-04 23:08:23 -0700674 app = application_lookup (mp->client_index);
675 if (app)
676 {
677 a->app_index = mp->client_index;
678 a->handle = mp->handle;
Florin Corascea194d2017-10-02 00:18:51 -0700679 if ((error = vnet_unbind (a)))
680 {
681 rv = clib_error_get_code (error);
682 clib_error_report (error);
683 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700684 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500685
Florin Coras6cf30ad2017-04-04 23:08:23 -0700686done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500687 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
688}
689
690static void
691vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
692{
Dave Wallace33e002b2017-09-06 01:20:02 -0400693 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500694 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700695 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700696 clib_error_t *error = 0;
697 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500698
Florin Coras6cf30ad2017-04-04 23:08:23 -0700699 if (session_manager_is_enabled () == 0)
700 {
701 rv = VNET_API_ERROR_FEATURE_DISABLED;
702 goto done;
703 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500704
Florin Coras6cf30ad2017-04-04 23:08:23 -0700705 app = application_lookup (mp->client_index);
706 if (app)
707 {
Dave Wallaceb2d5ff32017-06-14 12:38:28 -0400708 unix_shared_memory_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -0400709 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -0400710
711 client_q = vl_api_client_index_to_input_queue (mp->client_index);
712 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -0700713 a->sep.is_ip4 = mp->is_ip4;
714 a->sep.ip = *ip46;
715 a->sep.port = mp->port;
716 a->sep.transport_proto = mp->proto;
717 a->sep.fib_index = mp->vrf;
718 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700719 a->api_context = mp->context;
720 a->app_index = app->index;
721 a->mp = mp;
Florin Corascea194d2017-10-02 00:18:51 -0700722 if ((error = vnet_connect (a)))
723 {
724 rv = clib_error_get_code (error);
725 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
726 clib_error_report (error);
727 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700728 }
729 else
730 {
731 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
732 }
Florin Corase04c2992017-03-01 08:17:34 -0800733
Florin Corascea194d2017-10-02 00:18:51 -0700734 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
Florin Corase04c2992017-03-01 08:17:34 -0800735 return;
736
737 /* Got some error, relay it */
738
Florin Coras6cf30ad2017-04-04 23:08:23 -0700739done:
Dave Wallace33e002b2017-09-06 01:20:02 -0400740 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500741}
742
Florin Corascea194d2017-10-02 00:18:51 -0700743static void
744vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
745{
746 vl_api_app_namespace_add_del_reply_t *rmp;
747 u8 *ns_id = 0;
748 clib_error_t *error = 0;
749 int rv = 0;
750 if (!session_manager_is_enabled ())
751 {
752 rv = VNET_API_ERROR_FEATURE_DISABLED;
753 goto done;
754 }
755
756 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
757 {
758 rv = VNET_API_ERROR_INVALID_VALUE;
759 goto done;
760 }
761
762 vec_validate (ns_id, mp->namespace_id_len - 1);
763 clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
764 vnet_app_namespace_add_del_args_t args = {
765 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -0700766 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -0700767 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
768 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
769 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
770 .is_add = 1
771 };
772 error = vnet_app_namespace_add_del (&args);
773 if (error)
774 {
775 rv = clib_error_get_code (error);
776 clib_error_report (error);
777 }
778 vec_free (ns_id);
779done:
780 REPLY_MACRO (VL_API_APP_NAMESPACE_ADD_DEL_REPLY);
781}
782
Florin Coras1c710452017-10-17 00:03:13 -0700783static void
784vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
785{
786 vl_api_session_rule_add_del_reply_t *rmp;
787 session_rule_add_del_args_t args;
788 session_rule_table_add_del_args_t *table_args = &args.table_args;
789 clib_error_t *error;
790 u8 fib_proto;
791 int rv = 0;
792
793 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
794
795 table_args->lcl.fp_len = mp->lcl_plen;
796 table_args->lcl.fp_proto = fib_proto;
797 table_args->rmt.fp_len = mp->rmt_plen;
798 table_args->rmt.fp_proto = fib_proto;
799 table_args->lcl_port = clib_net_to_host_u16 (mp->lcl_port);
800 table_args->rmt_port = clib_net_to_host_u16 (mp->rmt_port);
801 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
802 table_args->is_add = mp->is_add;
803 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
804 args.scope = mp->scope;
805
806 memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
807 memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
808 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
809 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
810 error = vnet_session_rule_add_del (&args);
811 if (error)
812 {
813 rv = clib_error_get_code (error);
814 clib_error_report (error);
815 }
816 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
817}
818
Florin Coras6cf30ad2017-04-04 23:08:23 -0700819static clib_error_t *
820application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500821{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700822 application_t *app = application_lookup (client_index);
823 vnet_app_detach_args_t _a, *a = &_a;
824 if (app)
825 {
826 a->app_index = app->index;
827 vnet_application_detach (a);
828 }
829 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500830}
831
Florin Coras6cf30ad2017-04-04 23:08:23 -0700832VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -0500833
834#define vl_msg_name_crc_list
835#include <vnet/vnet_all_api_h.h>
836#undef vl_msg_name_crc_list
837
838static void
839setup_message_id_table (api_main_t * am)
840{
841#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
842 foreach_vl_msg_name_crc_session;
843#undef _
844}
845
846/*
847 * session_api_hookup
848 * Add uri's API message handlers to the table.
849 * vlib has alread mapped shared memory and
850 * added the client registration handlers.
851 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
852 */
853static clib_error_t *
854session_api_hookup (vlib_main_t * vm)
855{
856 api_main_t *am = &api_main;
857
858#define _(N,n) \
859 vl_msg_api_set_handlers(VL_API_##N, #n, \
860 vl_api_##n##_t_handler, \
861 vl_noop_handler, \
862 vl_api_##n##_t_endian, \
863 vl_api_##n##_t_print, \
864 sizeof(vl_api_##n##_t), 1);
865 foreach_session_api_msg;
866#undef _
867
868 /*
869 * Messages which bounce off the data-plane to
870 * an API client. Simply tells the message handling infra not
871 * to free the message.
872 *
873 * Bounced message handlers MUST NOT block the data plane
874 */
875 am->message_bounce[VL_API_CONNECT_URI] = 1;
876 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
877
878 /*
879 * Set up the (msg_name, crc, message-id) table
880 */
881 setup_message_id_table (am);
882
883 return 0;
884}
885
886VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700887
Dave Barach68b0fb02017-02-28 15:15:56 -0500888/*
889 * fd.io coding-style-patch-verification: ON
890 *
891 * Local Variables:
892 * eval: (c-set-style "gnu")
893 * End:
894 */