blob: f15d2f6e8302162d41b80851ac42fb14e52ef755 [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>
Florin Coras6c36f532017-11-03 18:32:34 -070021#include <vnet/session/session_table.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022
23#include <vnet/vnet_msg_enum.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
25#define vl_typedefs /* define message structures */
26#include <vnet/vnet_all_api_h.h>
27#undef vl_typedefs
28
29#define vl_endianfun /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_endianfun
32
33/* instantiate all the print functions we know about */
34#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35#define vl_printfun
36#include <vnet/vnet_all_api_h.h>
37#undef vl_printfun
38
39#include <vlibapi/api_helper_macros.h>
40
41#define foreach_session_api_msg \
42_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070043_(APPLICATION_ATTACH, application_attach) \
44_(APPLICATION_DETACH, application_detach) \
Dave Barach68b0fb02017-02-28 15:15:56 -050045_(BIND_URI, bind_uri) \
46_(UNBIND_URI, unbind_uri) \
47_(CONNECT_URI, connect_uri) \
48_(DISCONNECT_SESSION, disconnect_session) \
49_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
50_(ACCEPT_SESSION_REPLY, accept_session_reply) \
51_(RESET_SESSION_REPLY, reset_session_reply) \
Florin Corascea194d2017-10-02 00:18:51 -070052_(BIND_SOCK, bind_sock) \
Dave Barach68b0fb02017-02-28 15:15:56 -050053_(UNBIND_SOCK, unbind_sock) \
54_(CONNECT_SOCK, connect_sock) \
Florin Corase04c2992017-03-01 08:17:34 -080055_(SESSION_ENABLE_DISABLE, session_enable_disable) \
Florin Corascea194d2017-10-02 00:18:51 -070056_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
Florin Coras1c710452017-10-17 00:03:13 -070057_(SESSION_RULE_ADD_DEL, session_rule_add_del) \
Florin Coras6c36f532017-11-03 18:32:34 -070058_(SESSION_RULES_DUMP, session_rules_dump) \
Florin Corase04c2992017-03-01 08:17:34 -080059
Dave Barach68b0fb02017-02-28 15:15:56 -050060static int
61send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
62 u32 segment_size)
63{
64 vl_api_map_another_segment_t *mp;
65 unix_shared_memory_queue_t *q;
66
67 q = vl_api_client_index_to_input_queue (api_client_index);
68
69 if (!q)
70 return -1;
71
72 mp = vl_msg_api_alloc (sizeof (*mp));
73 memset (mp, 0, sizeof (*mp));
74 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
75 mp->segment_size = segment_size;
76 strncpy ((char *) mp->segment_name, (char *) segment_name,
77 sizeof (mp->segment_name) - 1);
78
79 vl_msg_api_send_shmem (q, (u8 *) & mp);
80
81 return 0;
82}
83
84static int
Florin Coras6cf30ad2017-04-04 23:08:23 -070085send_session_accept_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -050086{
87 vl_api_accept_session_t *mp;
88 unix_shared_memory_queue_t *q, *vpp_queue;
89 application_t *server = application_get (s->app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -070090 transport_connection_t *tc;
91 transport_proto_vft_t *tp_vft;
92 stream_session_t *listener;
Dave Barach68b0fb02017-02-28 15:15:56 -050093
94 q = vl_api_client_index_to_input_queue (server->api_client_index);
95 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
96
97 if (!q)
98 return -1;
99
100 mp = vl_msg_api_alloc (sizeof (*mp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700101 memset (mp, 0, sizeof (*mp));
102
Dave Barach68b0fb02017-02-28 15:15:56 -0500103 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
Florin Corasa5464812017-04-19 13:00:05 -0700104 mp->context = server->index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700105 listener = listen_session_get (s->session_type, s->listener_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700106 tp_vft = transport_protocol_get_vft (s->session_type);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700107 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
108 mp->listener_handle = listen_session_get_handle (listener);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700109 mp->handle = session_handle (s);
Damjan Marion7bee80c2017-04-26 15:32:12 +0200110 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
111 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
112 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700113 mp->port = tc->rmt_port;
114 mp->is_ip4 = tc->is_ip4;
115 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Dave Barach68b0fb02017-02-28 15:15:56 -0500116 vl_msg_api_send_shmem (q, (u8 *) & mp);
117
118 return 0;
119}
120
121static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700122send_session_disconnect_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500123{
124 vl_api_disconnect_session_t *mp;
125 unix_shared_memory_queue_t *q;
126 application_t *app = application_get (s->app_index);
127
128 q = vl_api_client_index_to_input_queue (app->api_client_index);
129
130 if (!q)
131 return;
132
133 mp = vl_msg_api_alloc (sizeof (*mp));
134 memset (mp, 0, sizeof (*mp));
135 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700136 mp->handle = session_handle (s);
Dave Barach68b0fb02017-02-28 15:15:56 -0500137 vl_msg_api_send_shmem (q, (u8 *) & mp);
138}
139
Florin Corasd79b41e2017-03-04 05:37:52 -0800140static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700141send_session_reset_callback (stream_session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800142{
143 vl_api_reset_session_t *mp;
144 unix_shared_memory_queue_t *q;
145 application_t *app = application_get (s->app_index);
146
147 q = vl_api_client_index_to_input_queue (app->api_client_index);
148
149 if (!q)
150 return;
151
152 mp = vl_msg_api_alloc (sizeof (*mp));
153 memset (mp, 0, sizeof (*mp));
154 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700155 mp->handle = session_handle (s);
Florin Corasd79b41e2017-03-04 05:37:52 -0800156 vl_msg_api_send_shmem (q, (u8 *) & mp);
157}
158
Dave Barach0194f1a2017-05-15 10:11:39 -0400159int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700160send_session_connected_callback (u32 app_index, u32 api_context,
161 stream_session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500162{
Dave Wallace33e002b2017-09-06 01:20:02 -0400163 vl_api_connect_session_reply_t *mp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500164 unix_shared_memory_queue_t *q;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700165 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500166 unix_shared_memory_queue_t *vpp_queue;
Florin Corasade70e42017-10-14 18:56:41 -0700167 transport_connection_t *tc;
Dave Barach68b0fb02017-02-28 15:15:56 -0500168
Florin Coras6cf30ad2017-04-04 23:08:23 -0700169 app = application_get (app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500170 q = vl_api_client_index_to_input_queue (app->api_client_index);
171
172 if (!q)
173 return -1;
174
175 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Wallace33e002b2017-09-06 01:20:02 -0400176 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700177 mp->context = api_context;
Dave Wallace965fec92017-10-17 16:19:41 -0400178
179 if (is_fail)
180 goto done;
181
182 tc = session_get_transport (s);
183 if (!tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500184 {
Dave Wallace965fec92017-10-17 16:19:41 -0400185 is_fail = 1;
186 goto done;
Florin Corase04c2992017-03-01 08:17:34 -0800187 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500188
Dave Wallace965fec92017-10-17 16:19:41 -0400189 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
190 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
191 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
192 mp->handle = session_handle (s);
193 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
194 clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
195 mp->is_ip4 = tc->is_ip4;
196 mp->lcl_port = tc->lcl_port;
197
198done:
199 mp->retval = is_fail ?
200 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500201 vl_msg_api_send_shmem (q, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500202 return 0;
203}
204
205/**
206 * Redirect a connect_uri message to the indicated server.
207 * Only sent if the server has bound the related port with
208 * URI_OPTIONS_FLAGS_USE_FIFO
209 */
210static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700211redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500212{
Florin Corascea194d2017-10-02 00:18:51 -0700213 vl_api_connect_sock_t *mp = mp_arg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500214 unix_shared_memory_queue_t *server_q, *client_q;
Florin Corasad0c77f2017-11-09 18:00:15 -0800215 segment_manager_properties_t *props;
Dave Barach68b0fb02017-02-28 15:15:56 -0500216 vlib_main_t *vm = vlib_get_main ();
217 f64 timeout = vlib_time_now (vm) + 0.5;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700218 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500219 int rv = 0;
220
221 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
222
223 if (!server_q)
224 {
225 rv = VNET_API_ERROR_INVALID_VALUE;
226 goto out;
227 }
228
229 client_q = vl_api_client_index_to_input_queue (mp->client_index);
230 if (!client_q)
231 {
232 rv = VNET_API_ERROR_INVALID_VALUE_2;
233 goto out;
234 }
235
236 /* Tell the server the client's API queue address, so it can reply */
Damjan Marion7bee80c2017-04-26 15:32:12 +0200237 mp->client_queue_address = pointer_to_uword (client_q);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700238 app = application_lookup (mp->client_index);
Florin Coras82b13a82017-04-25 11:58:06 -0700239 if (!app)
240 {
241 clib_warning ("no client application");
242 return -1;
243 }
244
Florin Corasad0c77f2017-11-09 18:00:15 -0800245 props = segment_manager_properties_get (app->sm_properties);
246 mp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = props->rx_fifo_size;
247 mp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = props->tx_fifo_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500248
249 /*
250 * Bounce message handlers MUST NOT block the data-plane.
251 * Spin waiting for the queue lock, but
252 */
253
254 while (vlib_time_now (vm) < timeout)
255 {
256 rv =
257 unix_shared_memory_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
258 switch (rv)
259 {
260 /* correctly enqueued */
261 case 0:
Florin Corascea194d2017-10-02 00:18:51 -0700262 return VNET_API_ERROR_SESSION_REDIRECT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500263
264 /* continue spinning, wait for pthread_mutex_trylock to work */
265 case -1:
266 continue;
267
268 /* queue stuffed, drop the msg */
269 case -2:
270 rv = VNET_API_ERROR_QUEUE_FULL;
271 goto out;
272 }
273 }
274out:
275 /* Dispose of the message */
276 vl_msg_api_free (mp);
277 return rv;
278}
279
Florin Corascea194d2017-10-02 00:18:51 -0700280static session_cb_vft_t session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500281 .session_accept_callback = send_session_accept_callback,
282 .session_disconnect_callback = send_session_disconnect_callback,
283 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800284 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500285 .add_segment_callback = send_add_segment_callback,
286 .redirect_connect_callback = redirect_connect_callback
287};
288
Dave Barach68b0fb02017-02-28 15:15:56 -0500289static void
Florin Corase04c2992017-03-01 08:17:34 -0800290vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
291{
292 vl_api_session_enable_disable_reply_t *rmp;
293 vlib_main_t *vm = vlib_get_main ();
294 int rv = 0;
295
296 vnet_session_enable_disable (vm, mp->is_enable);
297 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
298}
299
300static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700301vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500302{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700303 vl_api_application_attach_reply_t *rmp;
304 vnet_app_attach_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -0700305 clib_error_t *error = 0;
306 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500307
Florin Coras6cf30ad2017-04-04 23:08:23 -0700308 if (session_manager_is_enabled () == 0)
309 {
310 rv = VNET_API_ERROR_FEATURE_DISABLED;
311 goto done;
312 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500313
Florin Coras6cf30ad2017-04-04 23:08:23 -0700314 STATIC_ASSERT (sizeof (u64) * SESSION_OPTIONS_N_OPTIONS <=
315 sizeof (mp->options),
316 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500317
318 memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500319 a->api_client_index = mp->client_index;
320 a->options = mp->options;
Florin Corascea194d2017-10-02 00:18:51 -0700321 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500322
Florin Corascea194d2017-10-02 00:18:51 -0700323 if (mp->namespace_id_len > 64)
324 {
325 rv = VNET_API_ERROR_INVALID_VALUE;
326 goto done;
327 }
328
329 if (mp->namespace_id_len)
330 {
Dave Wallace8af20542017-10-26 03:29:30 -0400331 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Florin Corascea194d2017-10-02 00:18:51 -0700332 clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
333 }
334
335 if ((error = vnet_application_attach (a)))
336 {
337 rv = clib_error_get_code (error);
338 clib_error_report (error);
339 }
340 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500341
Florin Coras6cf30ad2017-04-04 23:08:23 -0700342done:
Florin Corasa5464812017-04-19 13:00:05 -0700343
Dave Barach68b0fb02017-02-28 15:15:56 -0500344 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700345 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500346 if (!rv)
347 {
348 rmp->segment_name_length = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700349 rmp->segment_size = a->segment_size;
350 if (a->segment_name_length)
Dave Barach68b0fb02017-02-28 15:15:56 -0500351 {
Florin Coras6cf30ad2017-04-04 23:08:23 -0700352 memcpy (rmp->segment_name, a->segment_name,
353 a->segment_name_length);
354 rmp->segment_name_length = a->segment_name_length;
Dave Barach68b0fb02017-02-28 15:15:56 -0500355 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700356 rmp->app_event_queue_address = a->app_event_queue_address;
Dave Barach68b0fb02017-02-28 15:15:56 -0500357 }
358 }));
359 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500360}
361
362static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700363vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
364{
365 vl_api_application_detach_reply_t *rmp;
366 int rv = VNET_API_ERROR_INVALID_VALUE_2;
367 vnet_app_detach_args_t _a, *a = &_a;
368 application_t *app;
369
370 if (session_manager_is_enabled () == 0)
371 {
372 rv = VNET_API_ERROR_FEATURE_DISABLED;
373 goto done;
374 }
375
376 app = application_lookup (mp->client_index);
377 if (app)
378 {
379 a->app_index = app->index;
380 rv = vnet_application_detach (a);
381 }
382
383done:
384 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
385}
386
387static void
388vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
389{
390 vl_api_bind_uri_reply_t *rmp;
391 vnet_bind_args_t _a, *a = &_a;
392 application_t *app;
393 int rv;
394
395 if (session_manager_is_enabled () == 0)
396 {
397 rv = VNET_API_ERROR_FEATURE_DISABLED;
398 goto done;
399 }
400
401 app = application_lookup (mp->client_index);
402 if (app)
403 {
404 memset (a, 0, sizeof (*a));
405 a->uri = (char *) mp->uri;
406 a->app_index = app->index;
407 rv = vnet_bind_uri (a);
408 }
409 else
410 {
411 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
412 }
413
414done:
415 REPLY_MACRO (VL_API_BIND_URI_REPLY);
416}
417
418static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500419vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
420{
421 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700422 application_t *app;
423 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500424 int rv;
425
Florin Coras6cf30ad2017-04-04 23:08:23 -0700426 if (session_manager_is_enabled () == 0)
427 {
428 rv = VNET_API_ERROR_FEATURE_DISABLED;
429 goto done;
430 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500431
Florin Coras6cf30ad2017-04-04 23:08:23 -0700432 app = application_lookup (mp->client_index);
433 if (app)
434 {
435 a->uri = (char *) mp->uri;
436 a->app_index = app->index;
437 rv = vnet_unbind_uri (a);
438 }
439 else
440 {
441 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
442 }
443
444done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500445 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
446}
447
Florin Corasf03a59a2017-06-09 21:07:32 -0700448static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500449vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
450{
Dave Wallace33e002b2017-09-06 01:20:02 -0400451 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500452 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700453 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700454 clib_error_t *error = 0;
455 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500456
Florin Coras6cf30ad2017-04-04 23:08:23 -0700457 if (session_manager_is_enabled () == 0)
458 {
459 rv = VNET_API_ERROR_FEATURE_DISABLED;
460 goto done;
461 }
Florin Corase04c2992017-03-01 08:17:34 -0800462
Florin Coras6cf30ad2017-04-04 23:08:23 -0700463 app = application_lookup (mp->client_index);
464 if (app)
465 {
466 a->uri = (char *) mp->uri;
467 a->api_context = mp->context;
468 a->app_index = app->index;
469 a->mp = mp;
Florin Corascea194d2017-10-02 00:18:51 -0700470 if ((error = vnet_connect_uri (a)))
471 {
472 rv = clib_error_get_code (error);
473 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
474 clib_error_report (error);
475 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700476 }
477 else
478 {
479 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
480 }
Florin Corase04c2992017-03-01 08:17:34 -0800481
Florin Coras3cbc04b2017-10-02 00:18:51 -0700482 /*
483 * Don't reply to stream (tcp) connects. The reply will come once
484 * the connection is established. In case of the redirects, the reply
485 * will come from the server app.
486 */
Florin Corascea194d2017-10-02 00:18:51 -0700487 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
Florin Corase04c2992017-03-01 08:17:34 -0800488 return;
489
Florin Coras6cf30ad2017-04-04 23:08:23 -0700490done:
Florin Corase04c2992017-03-01 08:17:34 -0800491 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -0400492 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800493 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500494}
495
496static void
497vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
498{
499 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700500 vnet_disconnect_args_t _a, *a = &_a;
501 application_t *app;
502 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500503
Florin Coras6cf30ad2017-04-04 23:08:23 -0700504 if (session_manager_is_enabled () == 0)
505 {
506 rv = VNET_API_ERROR_FEATURE_DISABLED;
507 goto done;
508 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500509
Florin Coras6cf30ad2017-04-04 23:08:23 -0700510 app = application_lookup (mp->client_index);
511 if (app)
512 {
513 a->handle = mp->handle;
514 a->app_index = app->index;
515 rv = vnet_disconnect_session (a);
516 }
517 else
518 {
519 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
520 }
521
522done:
Dave Wallacede5fec92017-11-11 22:41:34 -0500523 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500524}
525
526static void
527vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
528 mp)
529{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700530 vnet_disconnect_args_t _a, *a = &_a;
531 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500532
533 /* Client objected to disconnecting the session, log and continue */
534 if (mp->retval)
535 {
536 clib_warning ("client retval %d", mp->retval);
537 return;
538 }
539
540 /* Disconnect has been confirmed. Confirm close to transport */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700541 app = application_lookup (mp->client_index);
542 if (app)
543 {
544 a->handle = mp->handle;
545 a->app_index = app->index;
546 vnet_disconnect_session (a);
547 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500548}
549
550static void
551vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
552{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700553 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500554 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700555 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500556
Florin Coras6cf30ad2017-04-04 23:08:23 -0700557 app = application_lookup (mp->client_index);
558 if (!app)
559 return;
560
Florin Corascea194d2017-10-02 00:18:51 -0700561 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700562 s = session_get_if_valid (index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700563 if (s == 0 || app->index != s->app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500564 {
565 clib_warning ("Invalid session!");
566 return;
567 }
568
569 /* Client objected to resetting the session, log and continue */
570 if (mp->retval)
571 {
572 clib_warning ("client retval %d", mp->retval);
573 return;
574 }
575
Dave Barach68b0fb02017-02-28 15:15:56 -0500576 /* This comes as a response to a reset, transport only waiting for
577 * confirmation to remove connection state, no need to disconnect */
578 stream_session_cleanup (s);
579}
580
581static void
582vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
583{
584 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700585 u32 session_index, thread_index;
Florin Corasa5464812017-04-19 13:00:05 -0700586 vnet_disconnect_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500587
Florin Corasa5464812017-04-19 13:00:05 -0700588 /* Server isn't interested, kill the session */
589 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500590 {
Florin Corasa5464812017-04-19 13:00:05 -0700591 a->app_index = mp->context;
592 a->handle = mp->handle;
593 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500594 }
Florin Corasa5464812017-04-19 13:00:05 -0700595 else
596 {
Florin Corascea194d2017-10-02 00:18:51 -0700597 session_parse_handle (mp->handle, &session_index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700598 s = session_get_if_valid (session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700599 if (!s)
600 {
601 clib_warning ("session doesn't exist");
602 return;
603 }
604 if (s->app_index != mp->context)
605 {
606 clib_warning ("app doesn't own session");
607 return;
608 }
Florin Corasa5464812017-04-19 13:00:05 -0700609 s->session_state = SESSION_STATE_READY;
610 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500611}
612
613static void
614vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
615 * mp)
616{
617 clib_warning ("not implemented");
618}
619
620static void
621vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
622{
623 vl_api_bind_sock_reply_t *rmp;
624 vnet_bind_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -0700625 int rv = 0;
626 clib_error_t *error;
Florin Coraseb2945c2017-11-22 10:39:09 -0800627 application_t *app = 0;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800628 stream_session_t *s;
629 transport_connection_t *tc = 0;
630 ip46_address_t *ip46;
Dave Barach68b0fb02017-02-28 15:15:56 -0500631
Florin Coras6cf30ad2017-04-04 23:08:23 -0700632 if (session_manager_is_enabled () == 0)
633 {
634 rv = VNET_API_ERROR_FEATURE_DISABLED;
635 goto done;
636 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500637
Florin Coras6cf30ad2017-04-04 23:08:23 -0700638 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800639 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700640 {
Florin Corasdcf55ce2017-11-16 15:32:50 -0800641 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
642 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700643 }
Florin Corasdcf55ce2017-11-16 15:32:50 -0800644
645 ip46 = (ip46_address_t *) mp->ip;
646 memset (a, 0, sizeof (*a));
647 a->sep.is_ip4 = mp->is_ip4;
648 a->sep.ip = *ip46;
649 a->sep.port = mp->port;
650 a->sep.fib_index = mp->vrf;
651 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
652 a->sep.transport_proto = mp->proto;
653 a->app_index = app->index;
654
655 if ((error = vnet_bind (a)))
656 {
657 rv = clib_error_get_code (error);
658 clib_error_report (error);
659 }
Florin Corasdcf55ce2017-11-16 15:32:50 -0800660
Florin Coras6cf30ad2017-04-04 23:08:23 -0700661done:
Florin Corascea194d2017-10-02 00:18:51 -0700662 /* *INDENT-OFF* */
663 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
664 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -0800665 {
666 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -0800667 rmp->lcl_port = mp->port;
668 if (application_has_global_scope (app))
669 {
670 s = listen_session_get_from_handle (a->handle);
671 tc = listen_session_get_transport (s);
672 rmp->lcl_is_ip4 = tc->is_ip4;
673 clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
674 }
Florin Corasdcf55ce2017-11-16 15:32:50 -0800675 }
Florin Corascea194d2017-10-02 00:18:51 -0700676 }));
677 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500678}
679
680static void
681vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
682{
683 vl_api_unbind_sock_reply_t *rmp;
684 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700685 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700686 clib_error_t *error;
687 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500688
Florin Coras6cf30ad2017-04-04 23:08:23 -0700689 if (session_manager_is_enabled () == 0)
690 {
691 rv = VNET_API_ERROR_FEATURE_DISABLED;
692 goto done;
693 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500694
Florin Coras6cf30ad2017-04-04 23:08:23 -0700695 app = application_lookup (mp->client_index);
696 if (app)
697 {
Florin Corasef24d422017-11-09 10:05:42 -0800698 a->app_index = app->index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700699 a->handle = mp->handle;
Florin Corascea194d2017-10-02 00:18:51 -0700700 if ((error = vnet_unbind (a)))
701 {
702 rv = clib_error_get_code (error);
703 clib_error_report (error);
704 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700705 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500706
Florin Coras6cf30ad2017-04-04 23:08:23 -0700707done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500708 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
709}
710
711static void
712vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
713{
Dave Wallace33e002b2017-09-06 01:20:02 -0400714 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500715 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700716 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700717 clib_error_t *error = 0;
718 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500719
Florin Coras6cf30ad2017-04-04 23:08:23 -0700720 if (session_manager_is_enabled () == 0)
721 {
722 rv = VNET_API_ERROR_FEATURE_DISABLED;
723 goto done;
724 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500725
Florin Coras6cf30ad2017-04-04 23:08:23 -0700726 app = application_lookup (mp->client_index);
727 if (app)
728 {
Dave Wallaceb2d5ff32017-06-14 12:38:28 -0400729 unix_shared_memory_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -0400730 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -0400731
732 client_q = vl_api_client_index_to_input_queue (mp->client_index);
733 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -0700734 a->sep.is_ip4 = mp->is_ip4;
735 a->sep.ip = *ip46;
736 a->sep.port = mp->port;
737 a->sep.transport_proto = mp->proto;
738 a->sep.fib_index = mp->vrf;
739 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700740 a->api_context = mp->context;
741 a->app_index = app->index;
742 a->mp = mp;
Florin Corascea194d2017-10-02 00:18:51 -0700743 if ((error = vnet_connect (a)))
744 {
745 rv = clib_error_get_code (error);
746 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
747 clib_error_report (error);
748 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700749 }
750 else
751 {
752 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
753 }
Florin Corase04c2992017-03-01 08:17:34 -0800754
Florin Corascea194d2017-10-02 00:18:51 -0700755 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
Florin Corase04c2992017-03-01 08:17:34 -0800756 return;
757
758 /* Got some error, relay it */
759
Florin Coras6cf30ad2017-04-04 23:08:23 -0700760done:
Dave Wallace33e002b2017-09-06 01:20:02 -0400761 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500762}
763
Florin Corascea194d2017-10-02 00:18:51 -0700764static void
765vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
766{
767 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Corascea194d2017-10-02 00:18:51 -0700768 clib_error_t *error = 0;
Florin Coras6e8c6672017-11-10 09:03:54 -0800769 u32 appns_index = 0;
770 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700771 int rv = 0;
772 if (!session_manager_is_enabled ())
773 {
774 rv = VNET_API_ERROR_FEATURE_DISABLED;
775 goto done;
776 }
777
778 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
779 {
780 rv = VNET_API_ERROR_INVALID_VALUE;
781 goto done;
782 }
783
784 vec_validate (ns_id, mp->namespace_id_len - 1);
785 clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
786 vnet_app_namespace_add_del_args_t args = {
787 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -0700788 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -0700789 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
790 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
791 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
792 .is_add = 1
793 };
794 error = vnet_app_namespace_add_del (&args);
795 if (error)
796 {
797 rv = clib_error_get_code (error);
798 clib_error_report (error);
799 }
Florin Coras6e8c6672017-11-10 09:03:54 -0800800 else
801 {
802 appns_index = app_namespace_index_from_id (ns_id);
803 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
804 {
805 clib_warning ("app ns lookup failed");
806 rv = VNET_API_ERROR_UNSPECIFIED;
807 }
808 }
Florin Corascea194d2017-10-02 00:18:51 -0700809 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -0800810
811 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -0700812done:
Florin Coras6e8c6672017-11-10 09:03:54 -0800813 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
814 if (!rv)
815 rmp->appns_index = clib_host_to_net_u32 (appns_index);
816 }));
817 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -0700818}
819
Florin Coras1c710452017-10-17 00:03:13 -0700820static void
821vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
822{
823 vl_api_session_rule_add_del_reply_t *rmp;
824 session_rule_add_del_args_t args;
825 session_rule_table_add_del_args_t *table_args = &args.table_args;
826 clib_error_t *error;
827 u8 fib_proto;
828 int rv = 0;
829
Florin Corasc97a7392017-11-05 23:07:07 -0800830 memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -0700831 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
832
833 table_args->lcl.fp_len = mp->lcl_plen;
834 table_args->lcl.fp_proto = fib_proto;
835 table_args->rmt.fp_len = mp->rmt_plen;
836 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100837 table_args->lcl_port = mp->lcl_port;
838 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -0700839 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
840 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -0800841 mp->tag[sizeof (mp->tag) - 1] = 0;
842 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700843 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
844 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -0800845 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -0700846
847 memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
848 memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
849 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
850 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
851 error = vnet_session_rule_add_del (&args);
852 if (error)
853 {
854 rv = clib_error_get_code (error);
855 clib_error_report (error);
856 }
Florin Corasc97a7392017-11-05 23:07:07 -0800857 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700858 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
859}
860
Florin Coras6c36f532017-11-03 18:32:34 -0700861static void
862send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -0800863 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c36f532017-11-03 18:32:34 -0700864 unix_shared_memory_queue_t * q, u32 context)
865{
866 vl_api_session_rules_details_t *rmp = 0;
867 session_mask_or_match_4_t *match =
868 (session_mask_or_match_4_t *) & rule->match;
869 session_mask_or_match_4_t *mask =
870 (session_mask_or_match_4_t *) & rule->mask;
871
872 rmp = vl_msg_api_alloc (sizeof (*rmp));
873 memset (rmp, 0, sizeof (*rmp));
874 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
875 rmp->context = context;
876
877 rmp->is_ip4 = 1;
878 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
879 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
880 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
881 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100882 rmp->lcl_port = match->lcl_port;
883 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700884 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
885 rmp->scope =
886 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
887 rmp->transport_proto = transport_proto;
888 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800889 if (tag)
890 {
891 clib_memcpy (rmp->tag, tag, vec_len (tag));
892 rmp->tag[vec_len (tag)] = 0;
893 }
Florin Coras6c36f532017-11-03 18:32:34 -0700894
895 vl_msg_api_send_shmem (q, (u8 *) & rmp);
896}
897
898static void
Florin Corasc97a7392017-11-05 23:07:07 -0800899send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
900 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c36f532017-11-03 18:32:34 -0700901 unix_shared_memory_queue_t * q, u32 context)
902{
903 vl_api_session_rules_details_t *rmp = 0;
904 session_mask_or_match_6_t *match =
905 (session_mask_or_match_6_t *) & rule->match;
906 session_mask_or_match_6_t *mask =
907 (session_mask_or_match_6_t *) & rule->mask;
908
909 rmp = vl_msg_api_alloc (sizeof (*rmp));
910 memset (rmp, 0, sizeof (*rmp));
911 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
912 rmp->context = context;
913
914 rmp->is_ip4 = 0;
915 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
916 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
917 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
918 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100919 rmp->lcl_port = match->lcl_port;
920 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700921 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800922 rmp->scope =
923 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -0700924 rmp->transport_proto = transport_proto;
925 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800926 if (tag)
927 {
928 clib_memcpy (rmp->tag, tag, vec_len (tag));
929 rmp->tag[vec_len (tag)] = 0;
930 }
Florin Coras6c36f532017-11-03 18:32:34 -0700931
932 vl_msg_api_send_shmem (q, (u8 *) & rmp);
933}
934
935static void
936send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -0800937 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c36f532017-11-03 18:32:34 -0700938 unix_shared_memory_queue_t * q, u32 context)
939{
940 mma_rule_16_t *rule16;
941 mma_rule_40_t *rule40;
942 mma_rules_table_16_t *srt16;
943 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -0800944 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -0700945
Florin Corasc97a7392017-11-05 23:07:07 -0800946 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -0700947 {
Florin Corasc97a7392017-11-05 23:07:07 -0800948 u8 *tag = 0;
949 /* *INDENT-OFF* */
950 srt16 = &srt->session_rules_tables_16;
951 pool_foreach (rule16, srt16->rules, ({
952 ri = mma_rules_table_rule_index_16 (srt16, rule16);
953 tag = session_rules_table_rule_tag (srt, ri, 1);
954 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
955 q, context);
956 }));
957 /* *INDENT-ON* */
958 }
959 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
960 {
961 u8 *tag = 0;
962 /* *INDENT-OFF* */
963 srt40 = &srt->session_rules_tables_40;
964 pool_foreach (rule40, srt40->rules, ({
965 ri = mma_rules_table_rule_index_40 (srt40, rule40);
966 tag = session_rules_table_rule_tag (srt, ri, 1);
967 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
968 q, context);
969 }));
970 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -0700971 }
972}
973
974static void
975vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
976{
977 unix_shared_memory_queue_t *q = NULL;
978 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -0800979 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -0700980
981 q = vl_api_client_index_to_input_queue (mp->client_index);
982 if (q == 0)
983 return;
984
985 /* *INDENT-OFF* */
986 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -0800987 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
988 {
989 send_session_rules_table_details (&st->session_rules[tp],
990 st->active_fib_proto, tp,
991 st->is_local, st->appns_index, q,
992 mp->context);
993 }
Florin Coras6c36f532017-11-03 18:32:34 -0700994 }));
995 /* *INDENT-ON* */
996}
997
Florin Coras6cf30ad2017-04-04 23:08:23 -0700998static clib_error_t *
999application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001000{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001001 application_t *app = application_lookup (client_index);
1002 vnet_app_detach_args_t _a, *a = &_a;
1003 if (app)
1004 {
1005 a->app_index = app->index;
1006 vnet_application_detach (a);
1007 }
1008 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001009}
1010
Florin Coras6cf30ad2017-04-04 23:08:23 -07001011VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001012
1013#define vl_msg_name_crc_list
1014#include <vnet/vnet_all_api_h.h>
1015#undef vl_msg_name_crc_list
1016
1017static void
1018setup_message_id_table (api_main_t * am)
1019{
1020#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1021 foreach_vl_msg_name_crc_session;
1022#undef _
1023}
1024
1025/*
1026 * session_api_hookup
1027 * Add uri's API message handlers to the table.
1028 * vlib has alread mapped shared memory and
1029 * added the client registration handlers.
1030 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1031 */
1032static clib_error_t *
1033session_api_hookup (vlib_main_t * vm)
1034{
1035 api_main_t *am = &api_main;
1036
1037#define _(N,n) \
1038 vl_msg_api_set_handlers(VL_API_##N, #n, \
1039 vl_api_##n##_t_handler, \
1040 vl_noop_handler, \
1041 vl_api_##n##_t_endian, \
1042 vl_api_##n##_t_print, \
1043 sizeof(vl_api_##n##_t), 1);
1044 foreach_session_api_msg;
1045#undef _
1046
1047 /*
1048 * Messages which bounce off the data-plane to
1049 * an API client. Simply tells the message handling infra not
1050 * to free the message.
1051 *
1052 * Bounced message handlers MUST NOT block the data plane
1053 */
1054 am->message_bounce[VL_API_CONNECT_URI] = 1;
1055 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1056
1057 /*
1058 * Set up the (msg_name, crc, message-id) table
1059 */
1060 setup_message_id_table (am);
1061
1062 return 0;
1063}
1064
1065VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001066
Dave Barach68b0fb02017-02-28 15:15:56 -05001067/*
1068 * fd.io coding-style-patch-verification: ON
1069 *
1070 * Local Variables:
1071 * eval: (c-set-style "gnu")
1072 * End:
1073 */