blob: a1225f83e8ca448c850f8e5dab9326131464504a [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
Florin Corasb384b542018-01-15 01:08:33 -080061session_send_memfd_fd (vl_api_registration_t * reg, const ssvm_private_t * sp)
62{
63 clib_error_t *error;
64 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
65 {
66 clib_warning ("can't send memfd fd");
67 return -1;
68 }
69 error = vl_api_send_fd_msg (reg, sp->fd);
70 if (error)
71 {
72 clib_error_report (error);
73 return -1;
74 }
75 return 0;
76}
77
78static int
79send_add_segment_callback (u32 api_client_index, const ssvm_private_t * sp)
Dave Barach68b0fb02017-02-28 15:15:56 -050080{
81 vl_api_map_another_segment_t *mp;
Florin Corasb384b542018-01-15 01:08:33 -080082 vl_api_registration_t *reg;
Dave Barach68b0fb02017-02-28 15:15:56 -050083
Florin Corasb384b542018-01-15 01:08:33 -080084 reg = vl_mem_api_client_index_to_registration (api_client_index);
85 if (!reg)
86 {
87 clib_warning ("no registration: %u", api_client_index);
88 return -1;
89 }
Dave Barach68b0fb02017-02-28 15:15:56 -050090
Florin Corasb384b542018-01-15 01:08:33 -080091 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD
92 && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
93 {
94 clib_warning ("can't send memfd fd");
95 return -1;
96 }
Dave Barach68b0fb02017-02-28 15:15:56 -050097
Florin Coras7a2e1bd2017-11-30 16:07:39 -050098 mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -050099 memset (mp, 0, sizeof (*mp));
100 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
Florin Corasb384b542018-01-15 01:08:33 -0800101 mp->segment_size = sp->ssvm_size;
102 strncpy ((char *) mp->segment_name, (char *) sp->name,
Dave Barach68b0fb02017-02-28 15:15:56 -0500103 sizeof (mp->segment_name) - 1);
104
Florin Corasb384b542018-01-15 01:08:33 -0800105 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
106
107 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
108 return session_send_memfd_fd (reg, sp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500109
110 return 0;
111}
112
113static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700114send_session_accept_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500115{
Dave Barach68b0fb02017-02-28 15:15:56 -0500116 application_t *server = application_get (s->app_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700117 transport_proto_vft_t *tp_vft;
Florin Corasb384b542018-01-15 01:08:33 -0800118 vl_api_accept_session_t *mp;
119 vl_api_registration_t *reg;
120 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700121 stream_session_t *listener;
Florin Corasb384b542018-01-15 01:08:33 -0800122 svm_queue_t *vpp_queue;
Dave Barach68b0fb02017-02-28 15:15:56 -0500123
Dave Barach68b0fb02017-02-28 15:15:56 -0500124 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
Florin Corasb384b542018-01-15 01:08:33 -0800125 reg = vl_mem_api_client_index_to_registration (server->api_client_index);
126 if (!reg)
127 {
128 clib_warning ("no registration: %u", server->api_client_index);
129 return -1;
130 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500131
Florin Corasb384b542018-01-15 01:08:33 -0800132 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700133 memset (mp, 0, sizeof (*mp));
134
Dave Barach68b0fb02017-02-28 15:15:56 -0500135 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
Florin Corasa5464812017-04-19 13:00:05 -0700136 mp->context = server->index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700137 listener = listen_session_get (s->session_type, s->listener_index);
Florin Coras561af9b2017-12-09 10:19:43 -0800138 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700139 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
140 mp->listener_handle = listen_session_get_handle (listener);
Florin Corasc3ddea82017-11-27 03:12:00 -0800141
142 if (application_is_proxy (server))
143 {
144 listener =
145 application_first_listener (server,
146 transport_connection_fib_proto (tc),
147 tc->proto);
148 if (listener)
149 mp->listener_handle = listen_session_get_handle (listener);
150 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700151 mp->handle = session_handle (s);
Damjan Marion7bee80c2017-04-26 15:32:12 +0200152 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
153 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
154 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700155 mp->port = tc->rmt_port;
156 mp->is_ip4 = tc->is_ip4;
157 clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Corasb384b542018-01-15 01:08:33 -0800158 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500159
160 return 0;
161}
162
163static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700164send_session_disconnect_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500165{
Dave Barach68b0fb02017-02-28 15:15:56 -0500166 application_t *app = application_get (s->app_index);
Florin Corasb384b542018-01-15 01:08:33 -0800167 vl_api_disconnect_session_t *mp;
168 vl_api_registration_t *reg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500169
Florin Corasb384b542018-01-15 01:08:33 -0800170 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
171 if (!reg)
172 {
173 clib_warning ("no registration: %u", app->api_client_index);
174 return;
175 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500176
Florin Corasb384b542018-01-15 01:08:33 -0800177 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500178 memset (mp, 0, sizeof (*mp));
179 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700180 mp->handle = session_handle (s);
Florin Corasb384b542018-01-15 01:08:33 -0800181 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500182}
183
Florin Corasd79b41e2017-03-04 05:37:52 -0800184static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700185send_session_reset_callback (stream_session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800186{
Florin Corasd79b41e2017-03-04 05:37:52 -0800187 application_t *app = application_get (s->app_index);
Florin Corasb384b542018-01-15 01:08:33 -0800188 vl_api_registration_t *reg;
189 vl_api_reset_session_t *mp;
Florin Corasd79b41e2017-03-04 05:37:52 -0800190
Florin Corasb384b542018-01-15 01:08:33 -0800191 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
192 if (!reg)
193 {
194 clib_warning ("no registration: %u", app->api_client_index);
195 return;
196 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800197
Florin Corasb384b542018-01-15 01:08:33 -0800198 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800199 memset (mp, 0, sizeof (*mp));
200 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700201 mp->handle = session_handle (s);
Florin Corasb384b542018-01-15 01:08:33 -0800202 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Florin Corasd79b41e2017-03-04 05:37:52 -0800203}
204
Dave Barach0194f1a2017-05-15 10:11:39 -0400205int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700206send_session_connected_callback (u32 app_index, u32 api_context,
207 stream_session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500208{
Dave Wallace33e002b2017-09-06 01:20:02 -0400209 vl_api_connect_session_reply_t *mp;
Florin Corasade70e42017-10-14 18:56:41 -0700210 transport_connection_t *tc;
Florin Corasb384b542018-01-15 01:08:33 -0800211 vl_api_registration_t *reg;
212 svm_queue_t *vpp_queue;
213 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500214
Florin Coras6cf30ad2017-04-04 23:08:23 -0700215 app = application_get (app_index);
Florin Corasb384b542018-01-15 01:08:33 -0800216 reg = vl_mem_api_client_index_to_registration (app->api_client_index);
217 if (!reg)
218 {
219 clib_warning ("no registration: %u", app->api_client_index);
220 return -1;
221 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500222
Florin Corasb384b542018-01-15 01:08:33 -0800223 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Wallace33e002b2017-09-06 01:20:02 -0400224 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700225 mp->context = api_context;
Dave Wallace965fec92017-10-17 16:19:41 -0400226
227 if (is_fail)
228 goto done;
229
230 tc = session_get_transport (s);
231 if (!tc)
Dave Barach68b0fb02017-02-28 15:15:56 -0500232 {
Dave Wallace965fec92017-10-17 16:19:41 -0400233 is_fail = 1;
234 goto done;
Florin Corase04c2992017-03-01 08:17:34 -0800235 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500236
Dave Wallace965fec92017-10-17 16:19:41 -0400237 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
238 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
239 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
240 mp->handle = session_handle (s);
241 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
242 clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
243 mp->is_ip4 = tc->is_ip4;
244 mp->lcl_port = tc->lcl_port;
245
246done:
247 mp->retval = is_fail ?
248 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
Florin Corasb384b542018-01-15 01:08:33 -0800249 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500250 return 0;
251}
252
253/**
254 * Redirect a connect_uri message to the indicated server.
255 * Only sent if the server has bound the related port with
256 * URI_OPTIONS_FLAGS_USE_FIFO
257 */
258static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700259redirect_connect_callback (u32 server_api_client_index, void *mp_arg)
Dave Barach68b0fb02017-02-28 15:15:56 -0500260{
Florin Corascea194d2017-10-02 00:18:51 -0700261 vl_api_connect_sock_t *mp = mp_arg;
Florin Corase86a8ed2018-01-05 03:20:25 -0800262 svm_queue_t *server_q, *client_q;
Florin Corasad0c77f2017-11-09 18:00:15 -0800263 segment_manager_properties_t *props;
Dave Barach68b0fb02017-02-28 15:15:56 -0500264 vlib_main_t *vm = vlib_get_main ();
265 f64 timeout = vlib_time_now (vm) + 0.5;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700266 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500267 int rv = 0;
268
269 server_q = vl_api_client_index_to_input_queue (server_api_client_index);
270
271 if (!server_q)
272 {
273 rv = VNET_API_ERROR_INVALID_VALUE;
274 goto out;
275 }
276
277 client_q = vl_api_client_index_to_input_queue (mp->client_index);
278 if (!client_q)
279 {
280 rv = VNET_API_ERROR_INVALID_VALUE_2;
281 goto out;
282 }
283
284 /* Tell the server the client's API queue address, so it can reply */
Damjan Marion7bee80c2017-04-26 15:32:12 +0200285 mp->client_queue_address = pointer_to_uword (client_q);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700286 app = application_lookup (mp->client_index);
Florin Coras82b13a82017-04-25 11:58:06 -0700287 if (!app)
288 {
289 clib_warning ("no client application");
290 return -1;
291 }
292
Florin Corasad0c77f2017-11-09 18:00:15 -0800293 props = segment_manager_properties_get (app->sm_properties);
Florin Corasff6e7692017-12-11 04:59:01 -0800294 mp->options[APP_OPTIONS_RX_FIFO_SIZE] = props->rx_fifo_size;
295 mp->options[APP_OPTIONS_TX_FIFO_SIZE] = props->tx_fifo_size;
Dave Barach68b0fb02017-02-28 15:15:56 -0500296
297 /*
298 * Bounce message handlers MUST NOT block the data-plane.
299 * Spin waiting for the queue lock, but
300 */
301
302 while (vlib_time_now (vm) < timeout)
303 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800304 rv = svm_queue_add (server_q, (u8 *) & mp, 1 /*nowait */ );
Dave Barach68b0fb02017-02-28 15:15:56 -0500305 switch (rv)
306 {
307 /* correctly enqueued */
308 case 0:
Florin Corascea194d2017-10-02 00:18:51 -0700309 return VNET_API_ERROR_SESSION_REDIRECT;
Dave Barach68b0fb02017-02-28 15:15:56 -0500310
311 /* continue spinning, wait for pthread_mutex_trylock to work */
312 case -1:
313 continue;
314
315 /* queue stuffed, drop the msg */
316 case -2:
317 rv = VNET_API_ERROR_QUEUE_FULL;
318 goto out;
319 }
320 }
321out:
322 /* Dispose of the message */
323 vl_msg_api_free (mp);
324 return rv;
325}
326
Florin Corascea194d2017-10-02 00:18:51 -0700327static session_cb_vft_t session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500328 .session_accept_callback = send_session_accept_callback,
329 .session_disconnect_callback = send_session_disconnect_callback,
330 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800331 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500332 .add_segment_callback = send_add_segment_callback,
333 .redirect_connect_callback = redirect_connect_callback
334};
335
Dave Barach68b0fb02017-02-28 15:15:56 -0500336static void
Florin Corase04c2992017-03-01 08:17:34 -0800337vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
338{
339 vl_api_session_enable_disable_reply_t *rmp;
340 vlib_main_t *vm = vlib_get_main ();
341 int rv = 0;
342
343 vnet_session_enable_disable (vm, mp->is_enable);
344 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
345}
346
347static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700348vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500349{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700350 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800351 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700352 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800353 vl_api_registration_t *reg;
Florin Corascea194d2017-10-02 00:18:51 -0700354 clib_error_t *error = 0;
355 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500356
Florin Corasfc804d92018-01-26 01:27:01 -0800357 reg = vl_api_client_index_to_registration (mp->client_index);
358 if (!reg)
359 return;
360
Florin Coras6cf30ad2017-04-04 23:08:23 -0700361 if (session_manager_is_enabled () == 0)
362 {
363 rv = VNET_API_ERROR_FEATURE_DISABLED;
364 goto done;
365 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500366
Florin Corasff6e7692017-12-11 04:59:01 -0800367 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700368 sizeof (mp->options),
369 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500370
371 memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500372 a->api_client_index = mp->client_index;
373 a->options = mp->options;
Florin Corascea194d2017-10-02 00:18:51 -0700374 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500375
Florin Corascea194d2017-10-02 00:18:51 -0700376 if (mp->namespace_id_len > 64)
377 {
378 rv = VNET_API_ERROR_INVALID_VALUE;
379 goto done;
380 }
381
382 if (mp->namespace_id_len)
383 {
Dave Wallace8af20542017-10-26 03:29:30 -0400384 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Florin Corascea194d2017-10-02 00:18:51 -0700385 clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
386 }
387
388 if ((error = vnet_application_attach (a)))
389 {
390 rv = clib_error_get_code (error);
391 clib_error_report (error);
392 }
393 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500394
Florin Coras6cf30ad2017-04-04 23:08:23 -0700395done:
Florin Corasa5464812017-04-19 13:00:05 -0700396
Dave Barach68b0fb02017-02-28 15:15:56 -0500397 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700398 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500399 if (!rv)
400 {
Florin Corasb384b542018-01-15 01:08:33 -0800401 segp = a->segment;
Dave Barach68b0fb02017-02-28 15:15:56 -0500402 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800403 rmp->segment_size = segp->ssvm_size;
404 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500405 {
Florin Corasb384b542018-01-15 01:08:33 -0800406 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
407 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500408 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700409 rmp->app_event_queue_address = a->app_event_queue_address;
Dave Barach68b0fb02017-02-28 15:15:56 -0500410 }
411 }));
412 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800413
414 if (rv)
415 return;
416
Florin Corasb384b542018-01-15 01:08:33 -0800417 /* Send fifo segment fd if needed */
418 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
419 session_send_memfd_fd (reg, a->segment);
420 /* Send event queues segment */
421 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
422 session_send_memfd_fd (reg, evt_q_segment);
Dave Barach68b0fb02017-02-28 15:15:56 -0500423}
424
425static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700426vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
427{
428 vl_api_application_detach_reply_t *rmp;
429 int rv = VNET_API_ERROR_INVALID_VALUE_2;
430 vnet_app_detach_args_t _a, *a = &_a;
431 application_t *app;
432
433 if (session_manager_is_enabled () == 0)
434 {
435 rv = VNET_API_ERROR_FEATURE_DISABLED;
436 goto done;
437 }
438
439 app = application_lookup (mp->client_index);
440 if (app)
441 {
442 a->app_index = app->index;
443 rv = vnet_application_detach (a);
444 }
445
446done:
447 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
448}
449
450static void
451vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
452{
453 vl_api_bind_uri_reply_t *rmp;
454 vnet_bind_args_t _a, *a = &_a;
455 application_t *app;
456 int rv;
457
458 if (session_manager_is_enabled () == 0)
459 {
460 rv = VNET_API_ERROR_FEATURE_DISABLED;
461 goto done;
462 }
463
464 app = application_lookup (mp->client_index);
465 if (app)
466 {
467 memset (a, 0, sizeof (*a));
468 a->uri = (char *) mp->uri;
469 a->app_index = app->index;
470 rv = vnet_bind_uri (a);
471 }
472 else
473 {
474 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
475 }
476
477done:
478 REPLY_MACRO (VL_API_BIND_URI_REPLY);
479}
480
481static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500482vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
483{
484 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700485 application_t *app;
486 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500487 int rv;
488
Florin Coras6cf30ad2017-04-04 23:08:23 -0700489 if (session_manager_is_enabled () == 0)
490 {
491 rv = VNET_API_ERROR_FEATURE_DISABLED;
492 goto done;
493 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500494
Florin Coras6cf30ad2017-04-04 23:08:23 -0700495 app = application_lookup (mp->client_index);
496 if (app)
497 {
498 a->uri = (char *) mp->uri;
499 a->app_index = app->index;
500 rv = vnet_unbind_uri (a);
501 }
502 else
503 {
504 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
505 }
506
507done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500508 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
509}
510
Florin Corasf03a59a2017-06-09 21:07:32 -0700511static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500512vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
513{
Dave Wallace33e002b2017-09-06 01:20:02 -0400514 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500515 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700516 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700517 clib_error_t *error = 0;
518 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500519
Florin Coras6cf30ad2017-04-04 23:08:23 -0700520 if (session_manager_is_enabled () == 0)
521 {
522 rv = VNET_API_ERROR_FEATURE_DISABLED;
523 goto done;
524 }
Florin Corase04c2992017-03-01 08:17:34 -0800525
Florin Coras6cf30ad2017-04-04 23:08:23 -0700526 app = application_lookup (mp->client_index);
527 if (app)
528 {
529 a->uri = (char *) mp->uri;
530 a->api_context = mp->context;
531 a->app_index = app->index;
532 a->mp = mp;
Florin Corascea194d2017-10-02 00:18:51 -0700533 if ((error = vnet_connect_uri (a)))
534 {
535 rv = clib_error_get_code (error);
536 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
537 clib_error_report (error);
538 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700539 }
540 else
541 {
542 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
543 }
Florin Corase04c2992017-03-01 08:17:34 -0800544
Florin Coras3cbc04b2017-10-02 00:18:51 -0700545 /*
546 * Don't reply to stream (tcp) connects. The reply will come once
547 * the connection is established. In case of the redirects, the reply
548 * will come from the server app.
549 */
Florin Corascea194d2017-10-02 00:18:51 -0700550 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
Florin Corase04c2992017-03-01 08:17:34 -0800551 return;
552
Florin Coras6cf30ad2017-04-04 23:08:23 -0700553done:
Florin Corase04c2992017-03-01 08:17:34 -0800554 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -0400555 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800556 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500557}
558
559static void
560vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
561{
562 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700563 vnet_disconnect_args_t _a, *a = &_a;
564 application_t *app;
565 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500566
Florin Coras6cf30ad2017-04-04 23:08:23 -0700567 if (session_manager_is_enabled () == 0)
568 {
569 rv = VNET_API_ERROR_FEATURE_DISABLED;
570 goto done;
571 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500572
Florin Coras6cf30ad2017-04-04 23:08:23 -0700573 app = application_lookup (mp->client_index);
574 if (app)
575 {
576 a->handle = mp->handle;
577 a->app_index = app->index;
578 rv = vnet_disconnect_session (a);
579 }
580 else
581 {
582 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
583 }
584
585done:
Dave Wallacede5fec92017-11-11 22:41:34 -0500586 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500587}
588
589static void
590vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
591 mp)
592{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700593 vnet_disconnect_args_t _a, *a = &_a;
594 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500595
596 /* Client objected to disconnecting the session, log and continue */
597 if (mp->retval)
598 {
599 clib_warning ("client retval %d", mp->retval);
600 return;
601 }
602
603 /* Disconnect has been confirmed. Confirm close to transport */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700604 app = application_lookup (mp->client_index);
605 if (app)
606 {
607 a->handle = mp->handle;
608 a->app_index = app->index;
609 vnet_disconnect_session (a);
610 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500611}
612
613static void
614vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
615{
Florin Coras6cf30ad2017-04-04 23:08:23 -0700616 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -0500617 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700618 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -0500619
Florin Coras6cf30ad2017-04-04 23:08:23 -0700620 app = application_lookup (mp->client_index);
621 if (!app)
622 return;
623
Florin Corascea194d2017-10-02 00:18:51 -0700624 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700625 s = session_get_if_valid (index, thread_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700626 if (s == 0 || app->index != s->app_index)
Dave Barach68b0fb02017-02-28 15:15:56 -0500627 {
628 clib_warning ("Invalid session!");
629 return;
630 }
631
632 /* Client objected to resetting the session, log and continue */
633 if (mp->retval)
634 {
635 clib_warning ("client retval %d", mp->retval);
636 return;
637 }
638
Dave Barach68b0fb02017-02-28 15:15:56 -0500639 /* This comes as a response to a reset, transport only waiting for
640 * confirmation to remove connection state, no need to disconnect */
641 stream_session_cleanup (s);
642}
643
644static void
645vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
646{
647 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700648 u32 session_index, thread_index;
Florin Corasa5464812017-04-19 13:00:05 -0700649 vnet_disconnect_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500650
Florin Corasa5464812017-04-19 13:00:05 -0700651 /* Server isn't interested, kill the session */
652 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -0500653 {
Florin Corasa5464812017-04-19 13:00:05 -0700654 a->app_index = mp->context;
655 a->handle = mp->handle;
656 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -0500657 }
Florin Corasa5464812017-04-19 13:00:05 -0700658 else
659 {
Florin Corascea194d2017-10-02 00:18:51 -0700660 session_parse_handle (mp->handle, &session_index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700661 s = session_get_if_valid (session_index, thread_index);
Florin Corasa5464812017-04-19 13:00:05 -0700662 if (!s)
663 {
664 clib_warning ("session doesn't exist");
665 return;
666 }
667 if (s->app_index != mp->context)
668 {
669 clib_warning ("app doesn't own session");
670 return;
671 }
Florin Corasa5464812017-04-19 13:00:05 -0700672 s->session_state = SESSION_STATE_READY;
673 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500674}
675
676static void
677vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
678 * mp)
679{
680 clib_warning ("not implemented");
681}
682
683static void
684vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
685{
686 vl_api_bind_sock_reply_t *rmp;
687 vnet_bind_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -0700688 int rv = 0;
689 clib_error_t *error;
Florin Coraseb2945c2017-11-22 10:39:09 -0800690 application_t *app = 0;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800691 stream_session_t *s;
692 transport_connection_t *tc = 0;
693 ip46_address_t *ip46;
Dave Barach68b0fb02017-02-28 15:15:56 -0500694
Florin Coras6cf30ad2017-04-04 23:08:23 -0700695 if (session_manager_is_enabled () == 0)
696 {
697 rv = VNET_API_ERROR_FEATURE_DISABLED;
698 goto done;
699 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500700
Florin Coras6cf30ad2017-04-04 23:08:23 -0700701 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800702 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700703 {
Florin Corasdcf55ce2017-11-16 15:32:50 -0800704 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
705 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700706 }
Florin Corasdcf55ce2017-11-16 15:32:50 -0800707
708 ip46 = (ip46_address_t *) mp->ip;
709 memset (a, 0, sizeof (*a));
710 a->sep.is_ip4 = mp->is_ip4;
711 a->sep.ip = *ip46;
712 a->sep.port = mp->port;
713 a->sep.fib_index = mp->vrf;
714 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
715 a->sep.transport_proto = mp->proto;
716 a->app_index = app->index;
717
718 if ((error = vnet_bind (a)))
719 {
720 rv = clib_error_get_code (error);
721 clib_error_report (error);
722 }
Florin Corasdcf55ce2017-11-16 15:32:50 -0800723
Florin Coras6cf30ad2017-04-04 23:08:23 -0700724done:
Florin Corascea194d2017-10-02 00:18:51 -0700725 /* *INDENT-OFF* */
726 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
727 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -0800728 {
729 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -0800730 rmp->lcl_port = mp->port;
Dave Wallace97b1a272017-12-18 13:51:59 -0500731 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -0800732 {
733 s = listen_session_get_from_handle (a->handle);
734 tc = listen_session_get_transport (s);
735 rmp->lcl_is_ip4 = tc->is_ip4;
736 clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
737 }
Florin Corasdcf55ce2017-11-16 15:32:50 -0800738 }
Florin Corascea194d2017-10-02 00:18:51 -0700739 }));
740 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500741}
742
743static void
744vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
745{
746 vl_api_unbind_sock_reply_t *rmp;
747 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700748 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700749 clib_error_t *error;
750 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500751
Florin Coras6cf30ad2017-04-04 23:08:23 -0700752 if (session_manager_is_enabled () == 0)
753 {
754 rv = VNET_API_ERROR_FEATURE_DISABLED;
755 goto done;
756 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500757
Florin Coras6cf30ad2017-04-04 23:08:23 -0700758 app = application_lookup (mp->client_index);
759 if (app)
760 {
Florin Corasef24d422017-11-09 10:05:42 -0800761 a->app_index = app->index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700762 a->handle = mp->handle;
Florin Corascea194d2017-10-02 00:18:51 -0700763 if ((error = vnet_unbind (a)))
764 {
765 rv = clib_error_get_code (error);
766 clib_error_report (error);
767 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700768 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500769
Florin Coras6cf30ad2017-04-04 23:08:23 -0700770done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500771 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
772}
773
774static void
775vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
776{
Dave Wallace33e002b2017-09-06 01:20:02 -0400777 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500778 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700779 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700780 clib_error_t *error = 0;
781 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500782
Florin Coras6cf30ad2017-04-04 23:08:23 -0700783 if (session_manager_is_enabled () == 0)
784 {
785 rv = VNET_API_ERROR_FEATURE_DISABLED;
786 goto done;
787 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500788
Florin Coras6cf30ad2017-04-04 23:08:23 -0700789 app = application_lookup (mp->client_index);
790 if (app)
791 {
Florin Corase86a8ed2018-01-05 03:20:25 -0800792 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -0400793 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -0400794
795 client_q = vl_api_client_index_to_input_queue (mp->client_index);
796 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -0700797 a->sep.is_ip4 = mp->is_ip4;
798 a->sep.ip = *ip46;
799 a->sep.port = mp->port;
800 a->sep.transport_proto = mp->proto;
801 a->sep.fib_index = mp->vrf;
802 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700803 a->api_context = mp->context;
804 a->app_index = app->index;
805 a->mp = mp;
Florin Corascea194d2017-10-02 00:18:51 -0700806 if ((error = vnet_connect (a)))
807 {
808 rv = clib_error_get_code (error);
809 if (rv != VNET_API_ERROR_SESSION_REDIRECT)
810 clib_error_report (error);
811 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700812 }
813 else
814 {
815 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
816 }
Florin Corase04c2992017-03-01 08:17:34 -0800817
Florin Corascea194d2017-10-02 00:18:51 -0700818 if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
Florin Corase04c2992017-03-01 08:17:34 -0800819 return;
820
821 /* Got some error, relay it */
822
Florin Coras6cf30ad2017-04-04 23:08:23 -0700823done:
Dave Wallace33e002b2017-09-06 01:20:02 -0400824 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -0500825}
826
Florin Corascea194d2017-10-02 00:18:51 -0700827static void
828vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
829{
830 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Corascea194d2017-10-02 00:18:51 -0700831 clib_error_t *error = 0;
Florin Coras6e8c6672017-11-10 09:03:54 -0800832 u32 appns_index = 0;
833 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700834 int rv = 0;
835 if (!session_manager_is_enabled ())
836 {
837 rv = VNET_API_ERROR_FEATURE_DISABLED;
838 goto done;
839 }
840
841 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
842 {
843 rv = VNET_API_ERROR_INVALID_VALUE;
844 goto done;
845 }
846
847 vec_validate (ns_id, mp->namespace_id_len - 1);
848 clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
849 vnet_app_namespace_add_del_args_t args = {
850 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -0700851 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -0700852 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
853 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
854 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
855 .is_add = 1
856 };
857 error = vnet_app_namespace_add_del (&args);
858 if (error)
859 {
860 rv = clib_error_get_code (error);
861 clib_error_report (error);
862 }
Florin Coras6e8c6672017-11-10 09:03:54 -0800863 else
864 {
865 appns_index = app_namespace_index_from_id (ns_id);
866 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
867 {
868 clib_warning ("app ns lookup failed");
869 rv = VNET_API_ERROR_UNSPECIFIED;
870 }
871 }
Florin Corascea194d2017-10-02 00:18:51 -0700872 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -0800873
874 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -0700875done:
Florin Coras6e8c6672017-11-10 09:03:54 -0800876 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
877 if (!rv)
878 rmp->appns_index = clib_host_to_net_u32 (appns_index);
879 }));
880 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -0700881}
882
Florin Coras1c710452017-10-17 00:03:13 -0700883static void
884vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
885{
886 vl_api_session_rule_add_del_reply_t *rmp;
887 session_rule_add_del_args_t args;
888 session_rule_table_add_del_args_t *table_args = &args.table_args;
889 clib_error_t *error;
890 u8 fib_proto;
891 int rv = 0;
892
Florin Corasc97a7392017-11-05 23:07:07 -0800893 memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -0700894 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
895
896 table_args->lcl.fp_len = mp->lcl_plen;
897 table_args->lcl.fp_proto = fib_proto;
898 table_args->rmt.fp_len = mp->rmt_plen;
899 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100900 table_args->lcl_port = mp->lcl_port;
901 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -0700902 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
903 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -0800904 mp->tag[sizeof (mp->tag) - 1] = 0;
905 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700906 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
907 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -0800908 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -0700909
910 memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
911 memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
912 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
913 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
914 error = vnet_session_rule_add_del (&args);
915 if (error)
916 {
917 rv = clib_error_get_code (error);
918 clib_error_report (error);
919 }
Florin Corasc97a7392017-11-05 23:07:07 -0800920 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700921 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
922}
923
Florin Coras6c36f532017-11-03 18:32:34 -0700924static void
925send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -0800926 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800927 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700928{
929 vl_api_session_rules_details_t *rmp = 0;
930 session_mask_or_match_4_t *match =
931 (session_mask_or_match_4_t *) & rule->match;
932 session_mask_or_match_4_t *mask =
933 (session_mask_or_match_4_t *) & rule->mask;
934
935 rmp = vl_msg_api_alloc (sizeof (*rmp));
936 memset (rmp, 0, sizeof (*rmp));
937 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
938 rmp->context = context;
939
940 rmp->is_ip4 = 1;
941 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
942 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
943 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
944 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100945 rmp->lcl_port = match->lcl_port;
946 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700947 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
948 rmp->scope =
949 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
950 rmp->transport_proto = transport_proto;
951 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800952 if (tag)
953 {
954 clib_memcpy (rmp->tag, tag, vec_len (tag));
955 rmp->tag[vec_len (tag)] = 0;
956 }
Florin Coras6c36f532017-11-03 18:32:34 -0700957
Florin Coras6c4dae22018-01-09 06:39:23 -0800958 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700959}
960
961static void
Florin Corasc97a7392017-11-05 23:07:07 -0800962send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
963 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800964 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700965{
966 vl_api_session_rules_details_t *rmp = 0;
967 session_mask_or_match_6_t *match =
968 (session_mask_or_match_6_t *) & rule->match;
969 session_mask_or_match_6_t *mask =
970 (session_mask_or_match_6_t *) & rule->mask;
971
972 rmp = vl_msg_api_alloc (sizeof (*rmp));
973 memset (rmp, 0, sizeof (*rmp));
974 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
975 rmp->context = context;
976
977 rmp->is_ip4 = 0;
978 clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
979 clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
980 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
981 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100982 rmp->lcl_port = match->lcl_port;
983 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700984 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800985 rmp->scope =
986 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -0700987 rmp->transport_proto = transport_proto;
988 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800989 if (tag)
990 {
991 clib_memcpy (rmp->tag, tag, vec_len (tag));
992 rmp->tag[vec_len (tag)] = 0;
993 }
Florin Coras6c36f532017-11-03 18:32:34 -0700994
Florin Coras6c4dae22018-01-09 06:39:23 -0800995 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700996}
997
998static void
999send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001000 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001001 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001002{
1003 mma_rule_16_t *rule16;
1004 mma_rule_40_t *rule40;
1005 mma_rules_table_16_t *srt16;
1006 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001007 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001008
Florin Corasc97a7392017-11-05 23:07:07 -08001009 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001010 {
Florin Corasc97a7392017-11-05 23:07:07 -08001011 u8 *tag = 0;
1012 /* *INDENT-OFF* */
1013 srt16 = &srt->session_rules_tables_16;
1014 pool_foreach (rule16, srt16->rules, ({
1015 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1016 tag = session_rules_table_rule_tag (srt, ri, 1);
1017 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001018 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001019 }));
1020 /* *INDENT-ON* */
1021 }
1022 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1023 {
1024 u8 *tag = 0;
1025 /* *INDENT-OFF* */
1026 srt40 = &srt->session_rules_tables_40;
1027 pool_foreach (rule40, srt40->rules, ({
1028 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1029 tag = session_rules_table_rule_tag (srt, ri, 1);
1030 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001031 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001032 }));
1033 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001034 }
1035}
1036
1037static void
1038vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1039{
Florin Coras6c4dae22018-01-09 06:39:23 -08001040 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001041 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001042 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001043
Florin Coras6c4dae22018-01-09 06:39:23 -08001044 reg = vl_api_client_index_to_registration (mp->client_index);
1045 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001046 return;
1047
1048 /* *INDENT-OFF* */
1049 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001050 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1051 {
1052 send_session_rules_table_details (&st->session_rules[tp],
1053 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001054 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001055 mp->context);
1056 }
Florin Coras6c36f532017-11-03 18:32:34 -07001057 }));
1058 /* *INDENT-ON* */
1059}
1060
Florin Coras6cf30ad2017-04-04 23:08:23 -07001061static clib_error_t *
1062application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001063{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001064 application_t *app = application_lookup (client_index);
1065 vnet_app_detach_args_t _a, *a = &_a;
1066 if (app)
1067 {
1068 a->app_index = app->index;
1069 vnet_application_detach (a);
1070 }
1071 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001072}
1073
Florin Coras6cf30ad2017-04-04 23:08:23 -07001074VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001075
1076#define vl_msg_name_crc_list
1077#include <vnet/vnet_all_api_h.h>
1078#undef vl_msg_name_crc_list
1079
1080static void
1081setup_message_id_table (api_main_t * am)
1082{
1083#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1084 foreach_vl_msg_name_crc_session;
1085#undef _
1086}
1087
1088/*
1089 * session_api_hookup
1090 * Add uri's API message handlers to the table.
1091 * vlib has alread mapped shared memory and
1092 * added the client registration handlers.
1093 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1094 */
1095static clib_error_t *
1096session_api_hookup (vlib_main_t * vm)
1097{
1098 api_main_t *am = &api_main;
1099
1100#define _(N,n) \
1101 vl_msg_api_set_handlers(VL_API_##N, #n, \
1102 vl_api_##n##_t_handler, \
1103 vl_noop_handler, \
1104 vl_api_##n##_t_endian, \
1105 vl_api_##n##_t_print, \
1106 sizeof(vl_api_##n##_t), 1);
1107 foreach_session_api_msg;
1108#undef _
1109
1110 /*
1111 * Messages which bounce off the data-plane to
1112 * an API client. Simply tells the message handling infra not
1113 * to free the message.
1114 *
1115 * Bounced message handlers MUST NOT block the data plane
1116 */
1117 am->message_bounce[VL_API_CONNECT_URI] = 1;
1118 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1119
1120 /*
1121 * Set up the (msg_name, crc, message-id) table
1122 */
1123 setup_message_id_table (am);
1124
1125 return 0;
1126}
1127
1128VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001129
Dave Barach68b0fb02017-02-28 15:15:56 -05001130/*
1131 * fd.io coding-style-patch-verification: ON
1132 *
1133 * Local Variables:
1134 * eval: (c-set-style "gnu")
1135 * End:
1136 */