blob: 88deafe13e5a3e2f699c136f217dc73cd4046ec5 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2015-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * 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>
Florin Corasba7d8f52019-02-22 13:11:38 -080020#include <vnet/session/application_local.h>
Florin Coras1c710452017-10-17 00:03:13 -070021#include <vnet/session/session_rules_table.h>
Florin Coras6c36f532017-11-03 18:32:34 -070022#include <vnet/session/session_table.h>
Florin Corasc9940fc2019-02-05 20:55:11 -080023#include <vnet/session/session.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010025#include <vnet/ip/ip_types_api.h>
26
Dave Barach68b0fb02017-02-28 15:15:56 -050027#include <vnet/vnet_msg_enum.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050028
29#define vl_typedefs /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_typedefs
32
33#define vl_endianfun /* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39#define vl_printfun
40#include <vnet/vnet_all_api_h.h>
41#undef vl_printfun
42
43#include <vlibapi/api_helper_macros.h>
44
45#define foreach_session_api_msg \
46_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070047_(APPLICATION_ATTACH, application_attach) \
Florin Coras458089b2019-08-21 16:20:44 -070048_(APP_ATTACH, app_attach) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070049_(APPLICATION_DETACH, application_detach) \
Dave Barach68b0fb02017-02-28 15:15:56 -050050_(BIND_URI, bind_uri) \
51_(UNBIND_URI, unbind_uri) \
52_(CONNECT_URI, connect_uri) \
53_(DISCONNECT_SESSION, disconnect_session) \
54_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
Florin Corascea194d2017-10-02 00:18:51 -070055_(BIND_SOCK, bind_sock) \
Dave Barach68b0fb02017-02-28 15:15:56 -050056_(UNBIND_SOCK, unbind_sock) \
57_(CONNECT_SOCK, connect_sock) \
Florin Corase04c2992017-03-01 08:17:34 -080058_(SESSION_ENABLE_DISABLE, session_enable_disable) \
Florin Corascea194d2017-10-02 00:18:51 -070059_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
Florin Coras1c710452017-10-17 00:03:13 -070060_(SESSION_RULE_ADD_DEL, session_rule_add_del) \
Florin Coras6c36f532017-11-03 18:32:34 -070061_(SESSION_RULES_DUMP, session_rules_dump) \
Florin Coras371ca502018-02-21 12:07:41 -080062_(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \
63_(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \
Nathan Skrzypczak79f89532019-09-13 11:08:13 +020064_(APP_ADD_CERT_KEY_PAIR, app_add_cert_key_pair) \
65_(APP_DEL_CERT_KEY_PAIR, app_del_cert_key_pair) \
Florin Coras15531972018-08-12 23:50:53 -070066_(APP_WORKER_ADD_DEL, app_worker_add_del) \
Florin Corase04c2992017-03-01 08:17:34 -080067
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010068static transport_proto_t
69api_session_transport_proto_decode (const vl_api_transport_proto_t * api_tp)
70{
71 switch (*api_tp)
72 {
73 case TRANSPORT_PROTO_API_TCP:
74 return TRANSPORT_PROTO_TCP;
75 case TRANSPORT_PROTO_API_UDP:
76 return TRANSPORT_PROTO_UDP;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010077 case TRANSPORT_PROTO_API_TLS:
78 return TRANSPORT_PROTO_TLS;
79 case TRANSPORT_PROTO_API_UDPC:
80 return TRANSPORT_PROTO_UDPC;
81 case TRANSPORT_PROTO_API_QUIC:
82 return TRANSPORT_PROTO_QUIC;
83 default:
84 return TRANSPORT_PROTO_NONE;
85 }
86}
87
88static vl_api_transport_proto_t
89api_session_transport_proto_encode (const transport_proto_t tp)
90{
91 switch (tp)
92 {
93 case TRANSPORT_PROTO_TCP:
94 return TRANSPORT_PROTO_API_TCP;
95 case TRANSPORT_PROTO_UDP:
96 return TRANSPORT_PROTO_API_UDP;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010097 case TRANSPORT_PROTO_TLS:
98 return TRANSPORT_PROTO_API_TLS;
99 case TRANSPORT_PROTO_UDPC:
100 return TRANSPORT_PROTO_API_UDPC;
101 case TRANSPORT_PROTO_QUIC:
102 return TRANSPORT_PROTO_API_QUIC;
103 default:
104 return TRANSPORT_PROTO_API_NONE;
105 }
106}
107
Dave Barach68b0fb02017-02-28 15:15:56 -0500108static int
Florin Coras99368312018-08-02 10:45:44 -0700109session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
Florin Corasb384b542018-01-15 01:08:33 -0800110{
111 clib_error_t *error;
112 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
113 {
114 clib_warning ("can't send memfd fd");
115 return -1;
116 }
Florin Coras99368312018-08-02 10:45:44 -0700117 error = vl_api_send_fd_msg (reg, fds, n_fds);
Florin Corasb384b542018-01-15 01:08:33 -0800118 if (error)
119 {
120 clib_error_report (error);
121 return -1;
122 }
123 return 0;
124}
125
Florin Corasc4c4cf52019-08-24 18:17:34 -0700126/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Corasb384b542018-01-15 01:08:33 -0800127static int
Florin Corasfa76a762018-11-29 12:40:10 -0800128send_add_segment_callback (u32 api_client_index, u64 segment_handle)
Dave Barach68b0fb02017-02-28 15:15:56 -0500129{
Florin Coras99368312018-08-02 10:45:44 -0700130 int fds[SESSION_N_FD_TYPE], n_fds = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500131 vl_api_map_another_segment_t *mp;
Florin Corasb384b542018-01-15 01:08:33 -0800132 vl_api_registration_t *reg;
Florin Coras88001c62019-04-24 14:44:46 -0700133 fifo_segment_t *fs;
Florin Corasfa76a762018-11-29 12:40:10 -0800134 ssvm_private_t *sp;
Florin Coras99368312018-08-02 10:45:44 -0700135 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500136
Florin Corasb384b542018-01-15 01:08:33 -0800137 reg = vl_mem_api_client_index_to_registration (api_client_index);
138 if (!reg)
139 {
Florin Corasfa76a762018-11-29 12:40:10 -0800140 clib_warning ("no api registration for client: %u", api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800141 return -1;
142 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500143
Florin Corasfa76a762018-11-29 12:40:10 -0800144 fs = segment_manager_get_segment_w_handle (segment_handle);
145 sp = &fs->ssvm;
Florin Coras99368312018-08-02 10:45:44 -0700146 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
Florin Corasb384b542018-01-15 01:08:33 -0800147 {
Florin Coras99368312018-08-02 10:45:44 -0700148 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
149 {
150 clib_warning ("can't send memfd fd");
151 return -1;
152 }
153
154 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
155 fds[n_fds] = sp->fd;
156 n_fds += 1;
Florin Corasb384b542018-01-15 01:08:33 -0800157 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500158
Florin Coras99368312018-08-02 10:45:44 -0700159 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400160 clib_memset (mp, 0, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500161 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
Florin Corasb384b542018-01-15 01:08:33 -0800162 mp->segment_size = sp->ssvm_size;
Florin Coras99368312018-08-02 10:45:44 -0700163 mp->fd_flags = fd_flags;
Florin Corasfa76a762018-11-29 12:40:10 -0800164 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasb384b542018-01-15 01:08:33 -0800165 strncpy ((char *) mp->segment_name, (char *) sp->name,
Dave Barach68b0fb02017-02-28 15:15:56 -0500166 sizeof (mp->segment_name) - 1);
167
Florin Corasb384b542018-01-15 01:08:33 -0800168 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
169
Florin Coras99368312018-08-02 10:45:44 -0700170 if (n_fds)
171 return session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500172
173 return 0;
174}
175
Florin Corasc4c4cf52019-08-24 18:17:34 -0700176/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -0500177static int
Florin Corasfa76a762018-11-29 12:40:10 -0800178send_del_segment_callback (u32 api_client_index, u64 segment_handle)
Florin Corasf8f516a2018-02-08 15:10:09 -0800179{
180 vl_api_unmap_segment_t *mp;
181 vl_api_registration_t *reg;
182
183 reg = vl_mem_api_client_index_to_registration (api_client_index);
184 if (!reg)
185 {
186 clib_warning ("no registration: %u", api_client_index);
187 return -1;
188 }
189
Florin Coras99368312018-08-02 10:45:44 -0700190 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400191 clib_memset (mp, 0, sizeof (*mp));
Florin Corasf8f516a2018-02-08 15:10:09 -0800192 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
Florin Corasfa76a762018-11-29 12:40:10 -0800193 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasf8f516a2018-02-08 15:10:09 -0800194 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
195
Florin Coras99368312018-08-02 10:45:44 -0700196 return 0;
197}
198
199static int
Florin Coras83ea6692018-10-12 16:55:14 -0700200mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
201{
202 int rv;
203 u8 try = 0;
204 while (try < 100)
205 {
206 rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
207 SESSION_MQ_CTRL_EVT_RING,
208 SVM_Q_NOWAIT, msg);
209 if (!rv)
210 return 0;
211 try++;
Florin Corase2ea1932018-12-17 23:08:14 -0800212 usleep (1);
Florin Coras83ea6692018-10-12 16:55:14 -0700213 }
Florin Corasdc2e2512018-12-03 17:47:26 -0800214 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700215 return -1;
216}
217
218static int
Florin Coras288eaab2019-02-03 15:26:14 -0800219mq_send_session_accepted_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700220{
Florin Coras15531972018-08-12 23:50:53 -0700221 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700222 svm_msg_q_msg_t _msg, *msg = &_msg;
223 svm_msg_q_t *vpp_queue, *app_mq;
Florin Coras288eaab2019-02-03 15:26:14 -0800224 session_t *listener;
Florin Coras52207f12018-07-12 14:48:06 -0700225 session_accepted_msg_t *mp;
226 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700227 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700228
Florin Coras15531972018-08-12 23:50:53 -0700229 app = application_get (app_wrk->app_index);
230 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700231 if (mq_try_lock_and_alloc_msg (app_mq, msg))
232 return -1;
Florin Coras52207f12018-07-12 14:48:06 -0700233
234 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400235 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700236 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
237 mp = (session_accepted_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800238 clib_memset (mp, 0, sizeof (*mp));
Florin Corasab2f6db2018-08-31 14:31:41 -0700239 mp->context = app->app_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800240 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
241 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800242 mp->segment_handle = session_segment_handle (s);
Nathan Skrzypczakc00f4802019-12-03 16:25:11 +0100243 mp->flags = s->flags;
Florin Coras52207f12018-07-12 14:48:06 -0700244
245 if (session_has_transport (s))
246 {
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200247 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras87d66332019-06-11 12:31:31 -0700248 mp->listener_handle = app_listen_session_handle (listener);
Florin Coras52207f12018-07-12 14:48:06 -0700249 if (application_is_proxy (app))
250 {
251 listener =
Florin Coras15531972018-08-12 23:50:53 -0700252 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
253 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700254 if (listener)
255 mp->listener_handle = listen_session_get_handle (listener);
256 }
Florin Coras31c99552019-03-01 13:00:58 -0800257 vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700258 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
259 mp->handle = session_handle (s);
Aloys Augustincdb71702019-04-08 17:54:39 +0200260
Florin Coras09d18c22019-04-24 11:10:02 -0700261 session_get_endpoint (s, &mp->rmt, 0 /* is_lcl */ );
Florin Coras52207f12018-07-12 14:48:06 -0700262 }
263 else
264 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800265 ct_connection_t *ct;
Florin Coras99368312018-08-02 10:45:44 -0700266
Florin Coras2b81e3c2019-02-27 07:55:46 -0800267 ct = (ct_connection_t *) session_get_transport (s);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200268 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras87d66332019-06-11 12:31:31 -0700269 mp->listener_handle = app_listen_session_handle (listener);
Florin Coras09d18c22019-04-24 11:10:02 -0700270 mp->rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
271 mp->rmt.port = ct->c_rmt_port;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800272 mp->handle = session_handle (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800273 vpp_queue = session_main_get_vpp_event_queue (0);
Florin Coras54693d22018-07-17 10:46:29 -0700274 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Coras52207f12018-07-12 14:48:06 -0700275 }
Florin Coras537b17e2018-09-28 10:35:45 -0700276 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700277
278 return 0;
279}
280
Florin Coras72b04282019-01-14 17:23:11 -0800281static inline void
282mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
283 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700284{
Florin Coras52207f12018-07-12 14:48:06 -0700285 svm_msg_q_msg_t _msg, *msg = &_msg;
286 session_disconnected_msg_t *mp;
287 svm_msg_q_t *app_mq;
288 session_event_t *evt;
289
Florin Coras15531972018-08-12 23:50:53 -0700290 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700291 if (mq_try_lock_and_alloc_msg (app_mq, msg))
292 return;
Florin Coras52207f12018-07-12 14:48:06 -0700293 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400294 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800295 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700296 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800297 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800298 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700299 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700300}
301
Florin Coras72b04282019-01-14 17:23:11 -0800302static inline void
303mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
304 svm_fifo_t * f, session_evt_type_t evt_type)
305{
306 app_worker_t *app_wrk;
307 application_t *app;
308 int i;
309
310 app = application_get (app_index);
311 if (!app)
312 return;
313
314 for (i = 0; i < f->n_subscribers; i++)
315 {
316 if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
317 continue;
318 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
319 }
320}
321
322static void
Florin Coras288eaab2019-02-03 15:26:14 -0800323mq_send_session_disconnected_cb (session_t * s)
Florin Coras72b04282019-01-14 17:23:11 -0800324{
325 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
326 session_handle_t sh = session_handle (s);
327
328 mq_send_session_close_evt (app_wrk, session_handle (s),
329 SESSION_CTRL_EVT_DISCONNECTED);
330
Florin Coras288eaab2019-02-03 15:26:14 -0800331 if (svm_fifo_n_subscribers (s->rx_fifo))
332 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800333 SESSION_CTRL_EVT_DISCONNECTED);
334}
335
Florin Coras52207f12018-07-12 14:48:06 -0700336static void
Florin Coras288eaab2019-02-03 15:26:14 -0800337mq_send_session_reset_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700338{
Florin Coras72b04282019-01-14 17:23:11 -0800339 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
340 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700341
Florin Coras72b04282019-01-14 17:23:11 -0800342 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
343
Florin Coras288eaab2019-02-03 15:26:14 -0800344 if (svm_fifo_n_subscribers (s->rx_fifo))
345 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800346 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700347}
348
Florin Coras458089b2019-08-21 16:20:44 -0700349int
Florin Coras15531972018-08-12 23:50:53 -0700350mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800351 session_t * s, u8 is_fail)
Florin Coras52207f12018-07-12 14:48:06 -0700352{
353 svm_msg_q_msg_t _msg, *msg = &_msg;
354 session_connected_msg_t *mp;
355 svm_msg_q_t *vpp_mq, *app_mq;
356 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700357 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700358 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700359
Florin Coras15531972018-08-12 23:50:53 -0700360 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700361 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700362 if (!app_mq)
363 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800364 clib_warning ("app %u with api index: %u not attached",
365 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700366 return -1;
367 }
368
Florin Coras83ea6692018-10-12 16:55:14 -0700369 if (mq_try_lock_and_alloc_msg (app_mq, msg))
370 return -1;
Florin Corasdc2e2512018-12-03 17:47:26 -0800371
Florin Coras52207f12018-07-12 14:48:06 -0700372 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400373 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700374 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
375 mp = (session_connected_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800376 clib_memset (mp, 0, sizeof (*mp));
Florin Coras52207f12018-07-12 14:48:06 -0700377 mp->context = api_context;
378
379 if (is_fail)
380 goto done;
381
382 if (session_has_transport (s))
383 {
384 tc = session_get_transport (s);
385 if (!tc)
386 {
387 is_fail = 1;
388 goto done;
389 }
390
Florin Coras31c99552019-03-01 13:00:58 -0800391 vpp_mq = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700392 mp->handle = session_handle (s);
393 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Aloys Augustincdb71702019-04-08 17:54:39 +0200394
Florin Coras09d18c22019-04-24 11:10:02 -0700395 session_get_endpoint (s, &mp->lcl, 1 /* is_lcl */ );
Aloys Augustincdb71702019-04-08 17:54:39 +0200396
Florin Coras288eaab2019-02-03 15:26:14 -0800397 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
398 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800399 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700400 }
401 else
402 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800403 ct_connection_t *cct;
404 session_t *ss;
Florin Coras99368312018-08-02 10:45:44 -0700405
Florin Coras2b81e3c2019-02-27 07:55:46 -0800406 cct = (ct_connection_t *) session_get_transport (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800407 mp->handle = session_handle (s);
Florin Coras09d18c22019-04-24 11:10:02 -0700408 mp->lcl.port = cct->c_lcl_port;
409 mp->lcl.is_ip4 = cct->c_is_ip4;
Florin Coras653e43f2019-03-04 10:56:23 -0800410 vpp_mq = session_main_get_vpp_event_queue (0);
Florin Coras54693d22018-07-17 10:46:29 -0700411 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras653e43f2019-03-04 10:56:23 -0800412 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
413 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
414 mp->segment_handle = session_segment_handle (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800415 ss = ct_session_get_peer (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800416 mp->ct_rx_fifo = pointer_to_uword (ss->tx_fifo);
417 mp->ct_tx_fifo = pointer_to_uword (ss->rx_fifo);
418 mp->ct_segment_handle = session_segment_handle (ss);
Florin Coras52207f12018-07-12 14:48:06 -0700419 }
420
421done:
422 mp->retval = is_fail ?
423 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
424
Florin Coras537b17e2018-09-28 10:35:45 -0700425 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700426 return 0;
427}
428
Florin Coras458089b2019-08-21 16:20:44 -0700429int
Florin Coras60116992018-08-27 09:52:18 -0700430mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
431 session_handle_t handle, int rv)
432{
433 svm_msg_q_msg_t _msg, *msg = &_msg;
434 svm_msg_q_t *app_mq, *vpp_evt_q;
Yu Ping0b819152019-05-07 02:24:30 +0800435 transport_endpoint_t tep;
Florin Coras60116992018-08-27 09:52:18 -0700436 session_bound_msg_t *mp;
437 app_worker_t *app_wrk;
438 session_event_t *evt;
Florin Corasc9940fc2019-02-05 20:55:11 -0800439 app_listener_t *al;
440 session_t *ls = 0;
Florin Coras60116992018-08-27 09:52:18 -0700441 app_wrk = app_worker_get (app_wrk_index);
Florin Coras60116992018-08-27 09:52:18 -0700442 app_mq = app_wrk->event_queue;
443 if (!app_mq)
444 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800445 clib_warning ("app %u with api index: %u not attached",
446 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700447 return -1;
448 }
449
Florin Coras83ea6692018-10-12 16:55:14 -0700450 if (mq_try_lock_and_alloc_msg (app_mq, msg))
451 return -1;
452
Florin Coras60116992018-08-27 09:52:18 -0700453 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400454 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700455 evt->event_type = SESSION_CTRL_EVT_BOUND;
456 mp = (session_bound_msg_t *) evt->data;
457 mp->context = api_context;
458
459 if (rv)
460 goto done;
461
462 mp->handle = handle;
Florin Corasd4295e62019-02-22 13:11:38 -0800463 al = app_listener_get_w_handle (handle);
464 if (al->session_index != SESSION_INVALID_INDEX)
465 ls = app_listener_get_session (al);
Florin Coras60116992018-08-27 09:52:18 -0700466 else
Florin Corasd4295e62019-02-22 13:11:38 -0800467 ls = app_listener_get_local_session (al);
Yu Ping0b819152019-05-07 02:24:30 +0800468
469 session_get_endpoint (ls, &tep, 1 /* is_lcl */ );
470 mp->lcl_port = tep.port;
471 mp->lcl_is_ip4 = tep.is_ip4;
472 clib_memcpy_fast (mp->lcl_ip, &tep.ip, sizeof (tep.ip));
Florin Coras60116992018-08-27 09:52:18 -0700473
Florin Coras31c99552019-03-01 13:00:58 -0800474 vpp_evt_q = session_main_get_vpp_event_queue (0);
Florin Coras5f45e012019-01-23 09:21:30 -0800475 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
476
Florin Coras2b81e3c2019-02-27 07:55:46 -0800477 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras60116992018-08-27 09:52:18 -0700478 {
Florin Coras288eaab2019-02-03 15:26:14 -0800479 mp->rx_fifo = pointer_to_uword (ls->rx_fifo);
480 mp->tx_fifo = pointer_to_uword (ls->tx_fifo);
Florin Coras60116992018-08-27 09:52:18 -0700481 }
482
483done:
484 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700485 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700486 return 0;
487}
488
Florin Coras458089b2019-08-21 16:20:44 -0700489void
490mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
491 u32 context, int rv)
492{
493 svm_msg_q_msg_t _msg, *msg = &_msg;
494 session_unlisten_reply_msg_t *ump;
495 svm_msg_q_t *app_mq;
496 session_event_t *evt;
497
498 app_mq = app_wrk->event_queue;
499 if (mq_try_lock_and_alloc_msg (app_mq, msg))
500 return;
501
502 evt = svm_msg_q_msg_data (app_mq, msg);
503 clib_memset (evt, 0, sizeof (*evt));
504 evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY;
505 ump = (session_unlisten_reply_msg_t *) evt->data;
506 ump->context = context;
507 ump->handle = sh;
508 ump->retval = rv;
509 svm_msg_q_add_and_unlock (app_mq, msg);
510}
511
Florin Coras49568af2019-07-31 16:46:24 -0700512static void
513mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh)
514{
Florin Coras68b7e582020-01-21 18:33:23 -0800515 svm_msg_q_msg_t _msg, *msg = &_msg;
516 session_migrated_msg_t *mp;
517 svm_msg_q_t *vpp_evt_q;
518 app_worker_t *app_wrk;
519 session_event_t *evt;
520 svm_msg_q_t *app_mq;
521
522 app_wrk = app_worker_get (s->app_wrk_index);
523 app_mq = app_wrk->event_queue;
524 if (mq_try_lock_and_alloc_msg (app_mq, msg))
525 return;
526
527 evt = svm_msg_q_msg_data (app_mq, msg);
528 clib_memset (evt, 0, sizeof (*evt));
529 evt->event_type = SESSION_CTRL_EVT_MIGRATED;
530 mp = (session_migrated_msg_t *) evt->data;
531 mp->handle = session_handle (s);
532 mp->new_handle = new_sh;
533 mp->vpp_thread_index = session_thread_from_handle (new_sh);
534 vpp_evt_q = session_main_get_vpp_event_queue (mp->vpp_thread_index);
535 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
536 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras49568af2019-07-31 16:46:24 -0700537}
538
Florin Corasc4c4cf52019-08-24 18:17:34 -0700539static int
540mq_send_add_segment_cb (u32 app_wrk_index, u64 segment_handle)
541{
542 int fds[SESSION_N_FD_TYPE], n_fds = 0;
543 svm_msg_q_msg_t _msg, *msg = &_msg;
544 session_app_add_segment_msg_t *mp;
545 vl_api_registration_t *reg;
546 app_worker_t *app_wrk;
547 session_event_t *evt;
548 svm_msg_q_t *app_mq;
549 fifo_segment_t *fs;
550 ssvm_private_t *sp;
551 u8 fd_flags = 0;
552
553 app_wrk = app_worker_get (app_wrk_index);
554
555 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
556 if (!reg)
557 {
558 clib_warning ("no api registration for client: %u",
559 app_wrk->api_client_index);
560 return -1;
561 }
562
563 fs = segment_manager_get_segment_w_handle (segment_handle);
564 sp = &fs->ssvm;
565 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
566 {
567 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
568 {
569 clib_warning ("can't send memfd fd");
570 return -1;
571 }
572
573 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
574 fds[n_fds] = sp->fd;
575 n_fds += 1;
576 }
577
578 app_mq = app_wrk->event_queue;
579 if (mq_try_lock_and_alloc_msg (app_mq, msg))
580 return -1;
581
582 if (n_fds)
583 session_send_fds (reg, fds, n_fds);
584
585 evt = svm_msg_q_msg_data (app_mq, msg);
586 clib_memset (evt, 0, sizeof (*evt));
587 evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT;
588 mp = (session_app_add_segment_msg_t *) evt->data;
589 clib_memset (mp, 0, sizeof (*mp));
590 mp->segment_size = sp->ssvm_size;
591 mp->fd_flags = fd_flags;
592 mp->segment_handle = segment_handle;
593 strncpy ((char *) mp->segment_name, (char *) sp->name,
594 sizeof (mp->segment_name) - 1);
595
596 svm_msg_q_add_and_unlock (app_mq, msg);
597
598 return 0;
599}
600
601static int
602mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle)
603{
604 svm_msg_q_msg_t _msg, *msg = &_msg;
605 session_app_del_segment_msg_t *mp;
606 vl_api_registration_t *reg;
607 app_worker_t *app_wrk;
608 session_event_t *evt;
609 svm_msg_q_t *app_mq;
610
611 app_wrk = app_worker_get (app_wrk_index);
612 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
613 if (!reg)
614 {
615 clib_warning ("no registration: %u", app_wrk->api_client_index);
616 return -1;
617 }
618
619 app_mq = app_wrk->event_queue;
620 if (mq_try_lock_and_alloc_msg (app_mq, msg))
621 return -1;
622
623 evt = svm_msg_q_msg_data (app_mq, msg);
624 clib_memset (evt, 0, sizeof (*evt));
625 evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT;
626 mp = (session_app_del_segment_msg_t *) evt->data;
627 clib_memset (mp, 0, sizeof (*mp));
628 mp->segment_handle = segment_handle;
629 svm_msg_q_add_and_unlock (app_mq, msg);
630
631 return 0;
632}
633
Florin Coras9ace36d2019-10-28 13:14:17 -0700634static void
635mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf)
636{
637 svm_msg_q_msg_t _msg, *msg = &_msg;
638 session_cleanup_msg_t *mp;
639 svm_msg_q_t *app_mq;
640 session_event_t *evt;
641 app_worker_t *app_wrk;
642
643 /* Only propagate session cleanup notification */
644 if (ntf == SESSION_CLEANUP_TRANSPORT)
645 return;
646
647 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
648 if (!app_wrk)
649 return;
650
651 app_mq = app_wrk->event_queue;
652 if (mq_try_lock_and_alloc_msg (app_mq, msg))
653 return;
654
655 evt = svm_msg_q_msg_data (app_mq, msg);
656 clib_memset (evt, 0, sizeof (*evt));
657 evt->event_type = SESSION_CTRL_EVT_CLEANUP;
658 mp = (session_cleanup_msg_t *) evt->data;
659 mp->handle = session_handle (s);
660 svm_msg_q_add_and_unlock (app_mq, msg);
661}
662
Florin Corasc4c4cf52019-08-24 18:17:34 -0700663/* ### WILL BE DEPRECATED POST 20.01 ### */
664static session_cb_vft_t session_mq_cb_vft_old = {
Florin Coras52207f12018-07-12 14:48:06 -0700665 .session_accept_callback = mq_send_session_accepted_cb,
666 .session_disconnect_callback = mq_send_session_disconnected_cb,
667 .session_connected_callback = mq_send_session_connected_cb,
668 .session_reset_callback = mq_send_session_reset_cb,
Florin Coras49568af2019-07-31 16:46:24 -0700669 .session_migrate_callback = mq_send_session_migrate_cb,
Florin Coras52207f12018-07-12 14:48:06 -0700670 .add_segment_callback = send_add_segment_callback,
671 .del_segment_callback = send_del_segment_callback,
672};
673
Florin Corasc4c4cf52019-08-24 18:17:34 -0700674static session_cb_vft_t session_mq_cb_vft = {
675 .session_accept_callback = mq_send_session_accepted_cb,
676 .session_disconnect_callback = mq_send_session_disconnected_cb,
677 .session_connected_callback = mq_send_session_connected_cb,
678 .session_reset_callback = mq_send_session_reset_cb,
679 .session_migrate_callback = mq_send_session_migrate_cb,
Florin Coras9ace36d2019-10-28 13:14:17 -0700680 .session_cleanup_callback = mq_send_session_cleanup_cb,
Florin Corasc4c4cf52019-08-24 18:17:34 -0700681 .add_segment_callback = mq_send_add_segment_cb,
682 .del_segment_callback = mq_send_del_segment_cb,
683};
684
Dave Barach68b0fb02017-02-28 15:15:56 -0500685static void
Florin Corase04c2992017-03-01 08:17:34 -0800686vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
687{
688 vl_api_session_enable_disable_reply_t *rmp;
689 vlib_main_t *vm = vlib_get_main ();
690 int rv = 0;
691
692 vnet_session_enable_disable (vm, mp->is_enable);
693 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
694}
695
Florin Coras458089b2019-08-21 16:20:44 -0700696/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Corase04c2992017-03-01 08:17:34 -0800697static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700698vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500699{
Florin Coras99368312018-08-02 10:45:44 -0700700 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700701 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800702 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700703 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800704 vl_api_registration_t *reg;
Florin Coras99368312018-08-02 10:45:44 -0700705 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500706
Florin Corasfc804d92018-01-26 01:27:01 -0800707 reg = vl_api_client_index_to_registration (mp->client_index);
708 if (!reg)
709 return;
710
Florin Coras31c99552019-03-01 13:00:58 -0800711 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700712 {
713 rv = VNET_API_ERROR_FEATURE_DISABLED;
714 goto done;
715 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500716
Florin Corasff6e7692017-12-11 04:59:01 -0800717 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700718 sizeof (mp->options),
719 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500720
Dave Barachb7b92992018-10-17 10:38:51 -0400721 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500722 a->api_client_index = mp->client_index;
723 a->options = mp->options;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700724 a->session_cb_vft = &session_mq_cb_vft_old;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100725 a->namespace_id =
726 (u8 *) vl_api_from_api_to_new_c_string (&mp->namespace_id);
Florin Corascea194d2017-10-02 00:18:51 -0700727
Florin Corasc1a42652019-02-08 18:27:29 -0800728 if ((rv = vnet_application_attach (a)))
Florin Corascea194d2017-10-02 00:18:51 -0700729 {
Florin Corasc1a42652019-02-08 18:27:29 -0800730 clib_warning ("attach returned: %d", rv);
Florin Coras99368312018-08-02 10:45:44 -0700731 vec_free (a->namespace_id);
732 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700733 }
734 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500735
Florin Coras99368312018-08-02 10:45:44 -0700736 /* Send event queues segment */
Florin Coras31c99552019-03-01 13:00:58 -0800737 if ((evt_q_segment = session_main_get_evt_q_segment ()))
Florin Coras99368312018-08-02 10:45:44 -0700738 {
739 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
740 fds[n_fds] = evt_q_segment->fd;
741 n_fds += 1;
742 }
743 /* Send fifo segment fd if needed */
744 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
745 {
746 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
747 fds[n_fds] = a->segment->fd;
748 n_fds += 1;
749 }
750 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
751 {
752 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
753 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
754 n_fds += 1;
755 }
756
Florin Coras6cf30ad2017-04-04 23:08:23 -0700757done:
Florin Corasa5464812017-04-19 13:00:05 -0700758
Dave Barach68b0fb02017-02-28 15:15:56 -0500759 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700760 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500761 if (!rv)
762 {
Florin Corasb384b542018-01-15 01:08:33 -0800763 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800764 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100765 vl_api_vec_to_api_string (segp->name, &rmp->segment_name);
Florin Corasb384b542018-01-15 01:08:33 -0800766 rmp->segment_size = segp->ssvm_size;
Florin Coras99368312018-08-02 10:45:44 -0700767 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
768 rmp->n_fds = n_fds;
769 rmp->fd_flags = fd_flags;
Florin Corasd85de682018-11-29 17:02:29 -0800770 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500771 }
772 }));
773 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800774
Florin Coras99368312018-08-02 10:45:44 -0700775 if (n_fds)
776 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500777}
778
779static void
Florin Coras458089b2019-08-21 16:20:44 -0700780vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
781{
782 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
783 vl_api_app_attach_reply_t *rmp;
784 ssvm_private_t *segp, *evt_q_segment;
785 vnet_app_attach_args_t _a, *a = &_a;
786 u8 fd_flags = 0, ctrl_thread;
787 vl_api_registration_t *reg;
788 svm_msg_q_t *ctrl_mq;
789
790 reg = vl_api_client_index_to_registration (mp->client_index);
791 if (!reg)
792 return;
793
794 if (session_main_is_enabled () == 0)
795 {
796 rv = VNET_API_ERROR_FEATURE_DISABLED;
797 goto done;
798 }
799
800 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
801 sizeof (mp->options),
802 "Out of options, fix api message definition");
803
804 clib_memset (a, 0, sizeof (*a));
805 a->api_client_index = mp->client_index;
806 a->options = mp->options;
807 a->session_cb_vft = &session_mq_cb_vft;
Florin Coras458089b2019-08-21 16:20:44 -0700808
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100809 a->namespace_id = vl_api_from_api_to_new_vec (&mp->namespace_id);
Florin Coras458089b2019-08-21 16:20:44 -0700810
811 if ((rv = vnet_application_attach (a)))
812 {
813 clib_warning ("attach returned: %d", rv);
814 vec_free (a->namespace_id);
815 goto done;
816 }
817 vec_free (a->namespace_id);
818
819 /* Send event queues segment */
820 if ((evt_q_segment = session_main_get_evt_q_segment ()))
821 {
822 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
823 fds[n_fds] = evt_q_segment->fd;
824 n_fds += 1;
825 }
826 /* Send fifo segment fd if needed */
827 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
828 {
829 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
830 fds[n_fds] = a->segment->fd;
831 n_fds += 1;
832 }
833 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
834 {
835 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
836 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
837 n_fds += 1;
838 }
839
840done:
Florin Coras458089b2019-08-21 16:20:44 -0700841 /* *INDENT-OFF* */
842 REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({
843 if (!rv)
844 {
Vratko Polak4f599852019-11-08 10:32:39 +0100845 ctrl_thread = vlib_num_workers () ? 1 : 0;
Nathan Skrzypczak9d3e1b42019-11-07 10:29:24 +0100846 ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
Florin Coras458089b2019-08-21 16:20:44 -0700847 segp = a->segment;
848 rmp->app_index = clib_host_to_net_u32 (a->app_index);
849 rmp->app_mq = pointer_to_uword (a->app_evt_q);
850 rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
851 rmp->vpp_ctrl_mq_thread = ctrl_thread;
852 rmp->n_fds = n_fds;
853 rmp->fd_flags = fd_flags;
854 if (vec_len (segp->name))
855 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100856 vl_api_vec_to_api_string (segp->name, &rmp->segment_name);
Florin Coras458089b2019-08-21 16:20:44 -0700857 }
858 rmp->segment_size = segp->ssvm_size;
859 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
860 }
861 }));
862 /* *INDENT-ON* */
863
864 if (n_fds)
865 session_send_fds (reg, fds, n_fds);
866}
867
868/* ### WILL BE DEPRECATED POST 20.01 ### */
869static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700870vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
871{
872 vl_api_application_detach_reply_t *rmp;
873 int rv = VNET_API_ERROR_INVALID_VALUE_2;
874 vnet_app_detach_args_t _a, *a = &_a;
875 application_t *app;
876
Florin Coras31c99552019-03-01 13:00:58 -0800877 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700878 {
879 rv = VNET_API_ERROR_FEATURE_DISABLED;
880 goto done;
881 }
882
883 app = application_lookup (mp->client_index);
884 if (app)
885 {
Florin Coras15531972018-08-12 23:50:53 -0700886 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800887 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700888 rv = vnet_application_detach (a);
889 }
890
891done:
892 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
893}
894
Florin Coras458089b2019-08-21 16:20:44 -0700895/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700896static void
897vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
898{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700899 vl_api_bind_uri_reply_t *rmp;
Florin Coras64424012019-03-02 10:47:47 -0800900 vnet_listen_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700901 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -0700902 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700903 int rv;
904
Florin Coras31c99552019-03-01 13:00:58 -0800905 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700906 {
907 rv = VNET_API_ERROR_FEATURE_DISABLED;
908 goto done;
909 }
910
911 app = application_lookup (mp->client_index);
912 if (app)
913 {
Dave Barachb7b92992018-10-17 10:38:51 -0400914 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700915 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700916 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700917 rv = vnet_bind_uri (a);
918 }
919 else
920 {
921 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
922 }
923
924done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700925
Florin Coras64424012019-03-02 10:47:47 -0800926 REPLY_MACRO (VL_API_BIND_URI_REPLY);
Florin Coras60116992018-08-27 09:52:18 -0700927
Florin Coras64424012019-03-02 10:47:47 -0800928 if (app)
Florin Coras60116992018-08-27 09:52:18 -0700929 {
930 app_wrk = application_get_worker (app, 0);
931 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
932 rv);
933 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700934}
935
Florin Coras458089b2019-08-21 16:20:44 -0700936/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700937static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500938vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
939{
940 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700941 application_t *app;
Florin Corasc1a42652019-02-08 18:27:29 -0800942 vnet_unlisten_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500943 int rv;
944
Florin Coras31c99552019-03-01 13:00:58 -0800945 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700946 {
947 rv = VNET_API_ERROR_FEATURE_DISABLED;
948 goto done;
949 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500950
Florin Coras6cf30ad2017-04-04 23:08:23 -0700951 app = application_lookup (mp->client_index);
952 if (app)
953 {
954 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700955 a->app_index = app->app_index;
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +0200956 a->wrk_map_index = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700957 rv = vnet_unbind_uri (a);
958 }
959 else
960 {
961 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
962 }
963
964done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500965 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
966}
967
Florin Coras458089b2019-08-21 16:20:44 -0700968/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Corasf03a59a2017-06-09 21:07:32 -0700969static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500970vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
971{
Florin Coras64424012019-03-02 10:47:47 -0800972 vl_api_connect_uri_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500973 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700974 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700975 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500976
Florin Coras31c99552019-03-01 13:00:58 -0800977 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700978 {
979 rv = VNET_API_ERROR_FEATURE_DISABLED;
980 goto done;
981 }
Florin Corase04c2992017-03-01 08:17:34 -0800982
Florin Coras6cf30ad2017-04-04 23:08:23 -0700983 app = application_lookup (mp->client_index);
984 if (app)
985 {
Dave Barachb7b92992018-10-17 10:38:51 -0400986 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700987 a->uri = (char *) mp->uri;
988 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -0700989 a->app_index = app->app_index;
Florin Corasc1a42652019-02-08 18:27:29 -0800990 if ((rv = vnet_connect_uri (a)))
991 clib_warning ("connect_uri returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700992 }
993 else
994 {
995 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
996 }
Florin Corase04c2992017-03-01 08:17:34 -0800997
Florin Coras3cbc04b2017-10-02 00:18:51 -0700998 /*
999 * Don't reply to stream (tcp) connects. The reply will come once
1000 * the connection is established. In case of the redirects, the reply
1001 * will come from the server app.
1002 */
Florin Coras8f89dd02018-03-05 16:53:07 -08001003 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001004 return;
1005
Florin Coras6cf30ad2017-04-04 23:08:23 -07001006done:
Florin Coras64424012019-03-02 10:47:47 -08001007 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -05001008}
1009
Florin Coras458089b2019-08-21 16:20:44 -07001010/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001011static void
1012vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
1013{
1014 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001015 vnet_disconnect_args_t _a, *a = &_a;
1016 application_t *app;
1017 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001018
Florin Coras31c99552019-03-01 13:00:58 -08001019 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001020 {
1021 rv = VNET_API_ERROR_FEATURE_DISABLED;
1022 goto done;
1023 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001024
Florin Coras6cf30ad2017-04-04 23:08:23 -07001025 app = application_lookup (mp->client_index);
1026 if (app)
1027 {
1028 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001029 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001030 rv = vnet_disconnect_session (a);
1031 }
1032 else
1033 {
1034 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1035 }
1036
1037done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001038 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001039}
1040
Florin Coras458089b2019-08-21 16:20:44 -07001041/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001042static void
1043vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1044 mp)
1045{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001046 vnet_disconnect_args_t _a, *a = &_a;
1047 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001048
1049 /* Client objected to disconnecting the session, log and continue */
1050 if (mp->retval)
1051 {
1052 clib_warning ("client retval %d", mp->retval);
1053 return;
1054 }
1055
1056 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001057 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001058 if (app)
1059 {
1060 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001061 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001062 vnet_disconnect_session (a);
1063 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001064}
1065
Florin Corasc4c4cf52019-08-24 18:17:34 -07001066/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001067static void
Dave Barach68b0fb02017-02-28 15:15:56 -05001068vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1069 * mp)
1070{
1071 clib_warning ("not implemented");
1072}
1073
Florin Coras458089b2019-08-21 16:20:44 -07001074/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001075static void
1076vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1077{
Florin Corasc9940fc2019-02-05 20:55:11 -08001078 vnet_listen_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -05001079 vl_api_bind_sock_reply_t *rmp;
Florin Coraseb2945c2017-11-22 10:39:09 -08001080 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001081 app_worker_t *app_wrk;
Florin Corasc9940fc2019-02-05 20:55:11 -08001082 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001083
Florin Coras31c99552019-03-01 13:00:58 -08001084 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001085 {
1086 rv = VNET_API_ERROR_FEATURE_DISABLED;
1087 goto done;
1088 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001089
Florin Coras6cf30ad2017-04-04 23:08:23 -07001090 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001091 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001092 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001093 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1094 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001095 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001096
Dave Barachb7b92992018-10-17 10:38:51 -04001097 clib_memset (a, 0, sizeof (*a));
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001098 ip_address_decode (&mp->ip, &a->sep.ip);
1099 a->sep.is_ip4 = ip46_address_is_ip4 (&a->sep.ip);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001100 a->sep.port = mp->port;
1101 a->sep.fib_index = mp->vrf;
1102 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001103 a->sep.transport_proto = api_session_transport_proto_decode (&mp->proto);
Florin Coras15531972018-08-12 23:50:53 -07001104 a->app_index = app->app_index;
1105 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001106
Florin Corasc1a42652019-02-08 18:27:29 -08001107 if ((rv = vnet_listen (a)))
1108 clib_warning ("listen returned: %d", rv);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001109
Florin Coras6cf30ad2017-04-04 23:08:23 -07001110done:
Florin Coras64424012019-03-02 10:47:47 -08001111 /* Actual reply sent only over mq */
1112 REPLY_MACRO (VL_API_BIND_SOCK_REPLY);
Florin Coras60116992018-08-27 09:52:18 -07001113
Florin Coras64424012019-03-02 10:47:47 -08001114 if (app)
Florin Coras60116992018-08-27 09:52:18 -07001115 {
1116 app_wrk = application_get_worker (app, mp->wrk_index);
1117 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1118 rv);
1119 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001120}
1121
Florin Coras458089b2019-08-21 16:20:44 -07001122/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001123static void
1124vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1125{
1126 vl_api_unbind_sock_reply_t *rmp;
Florin Corasc1a42652019-02-08 18:27:29 -08001127 vnet_unlisten_args_t _a, *a = &_a;
Florin Corasdfae9f92019-02-20 19:48:31 -08001128 app_worker_t *app_wrk;
1129 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001130 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001131
Florin Coras31c99552019-03-01 13:00:58 -08001132 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001133 {
1134 rv = VNET_API_ERROR_FEATURE_DISABLED;
1135 goto done;
1136 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001137
Florin Coras6cf30ad2017-04-04 23:08:23 -07001138 app = application_lookup (mp->client_index);
1139 if (app)
1140 {
Florin Coras15531972018-08-12 23:50:53 -07001141 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001142 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001143 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001144 if ((rv = vnet_unlisten (a)))
1145 clib_warning ("unlisten returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001146 }
Florin Coraseef61bb2019-08-27 15:13:35 -07001147 else
1148 {
1149 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1150 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001151
Florin Coras6cf30ad2017-04-04 23:08:23 -07001152done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001153 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
Florin Corasdfae9f92019-02-20 19:48:31 -08001154
Florin Coraseef61bb2019-08-27 15:13:35 -07001155 if (!app)
1156 return;
1157
Florin Corasdfae9f92019-02-20 19:48:31 -08001158 app_wrk = application_get_worker (app, a->wrk_map_index);
1159 if (!app_wrk)
1160 return;
1161
Florin Coras458089b2019-08-21 16:20:44 -07001162 mq_send_unlisten_reply (app_wrk, mp->handle, mp->context, rv);
Dave Barach68b0fb02017-02-28 15:15:56 -05001163}
1164
Florin Coras458089b2019-08-21 16:20:44 -07001165/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001166static void
1167vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1168{
Florin Coras64424012019-03-02 10:47:47 -08001169 vl_api_connect_sock_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001170 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001171 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001172 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001173
Florin Coras31c99552019-03-01 13:00:58 -08001174 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001175 {
1176 rv = VNET_API_ERROR_FEATURE_DISABLED;
1177 goto done;
1178 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001179
Florin Coras6cf30ad2017-04-04 23:08:23 -07001180 app = application_lookup (mp->client_index);
1181 if (app)
1182 {
Florin Corase86a8ed2018-01-05 03:20:25 -08001183 svm_queue_t *client_q;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001184
Dave Barachb7b92992018-10-17 10:38:51 -04001185 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001186 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1187 mp->client_queue_address = pointer_to_uword (client_q);
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001188 ip_address_decode (&mp->ip, &a->sep.ip);
1189 a->sep.is_ip4 = ip46_address_is_ip4 (&a->sep.ip);
Florin Corascea194d2017-10-02 00:18:51 -07001190 a->sep.port = mp->port;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001191 a->sep.transport_proto =
1192 api_session_transport_proto_decode (&mp->proto);
Florin Coras5665ced2018-10-25 18:03:45 -07001193 a->sep.peer.fib_index = mp->vrf;
1194 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001195 a->sep_ext.parent_handle = mp->parent_handle;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001196 a->sep_ext.hostname =
1197 (u8 *) vl_api_from_api_to_new_c_string (&mp->hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001198 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001199 a->app_index = app->app_index;
1200 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001201 if ((rv = vnet_connect (a)))
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +02001202 clib_warning ("connect returned: %U", format_vnet_api_errno, rv);
Florin Coras15531972018-08-12 23:50:53 -07001203 vec_free (a->sep_ext.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001204 }
1205 else
1206 {
1207 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1208 }
Florin Corase04c2992017-03-01 08:17:34 -08001209
Florin Coras8f89dd02018-03-05 16:53:07 -08001210 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001211 return;
1212
1213 /* Got some error, relay it */
1214
Florin Coras6cf30ad2017-04-04 23:08:23 -07001215done:
Florin Coras64424012019-03-02 10:47:47 -08001216 REPLY_MACRO (VL_API_CONNECT_SOCK_REPLY);
Florin Corasab2f6db2018-08-31 14:31:41 -07001217
Florin Coras64424012019-03-02 10:47:47 -08001218 if (app)
Florin Corasab2f6db2018-08-31 14:31:41 -07001219 {
1220 app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1221 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1222 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001223}
1224
Florin Corascea194d2017-10-02 00:18:51 -07001225static void
Florin Coras15531972018-08-12 23:50:53 -07001226vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1227{
1228 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1229 vl_api_app_worker_add_del_reply_t *rmp;
1230 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -07001231 application_t *app;
1232 u8 fd_flags = 0;
1233
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001234 if (session_main_is_enabled () == 0)
Florin Coras15531972018-08-12 23:50:53 -07001235 {
1236 rv = VNET_API_ERROR_FEATURE_DISABLED;
1237 goto done;
1238 }
1239
1240 reg = vl_api_client_index_to_registration (mp->client_index);
1241 if (!reg)
1242 return;
1243
Florin Corasc1f5a432018-11-20 11:31:26 -08001244 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001245 if (!app)
1246 {
1247 rv = VNET_API_ERROR_INVALID_VALUE;
1248 goto done;
1249 }
1250
1251 vnet_app_worker_add_del_args_t args = {
1252 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001253 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001254 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001255 .is_add = mp->is_add
1256 };
Florin Corasc1a42652019-02-08 18:27:29 -08001257 rv = vnet_app_worker_add_del (&args);
1258 if (rv)
Florin Coras15531972018-08-12 23:50:53 -07001259 {
Florin Corasc1a42652019-02-08 18:27:29 -08001260 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -07001261 goto done;
1262 }
1263
Florin Coras14598772018-09-04 19:47:52 -07001264 if (!mp->is_add)
1265 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001266
Florin Coras15531972018-08-12 23:50:53 -07001267 /* Send fifo segment fd if needed */
1268 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1269 {
1270 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1271 fds[n_fds] = args.segment->fd;
1272 n_fds += 1;
1273 }
1274 if (application_segment_manager_properties (app)->use_mq_eventfd)
1275 {
1276 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1277 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1278 n_fds += 1;
1279 }
1280
1281 /* *INDENT-OFF* */
1282done:
1283 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1284 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001285 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001286 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001287 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001288 {
Florin Coras15531972018-08-12 23:50:53 -07001289 if (vec_len (args.segment->name))
1290 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001291 vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name);
Florin Coras15531972018-08-12 23:50:53 -07001292 }
1293 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1294 rmp->n_fds = n_fds;
1295 rmp->fd_flags = fd_flags;
1296 }
1297 }));
1298 /* *INDENT-ON* */
1299
1300 if (n_fds)
1301 session_send_fds (reg, fds, n_fds);
1302}
1303
1304static void
Florin Corascea194d2017-10-02 00:18:51 -07001305vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1306{
1307 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -08001308 u32 appns_index = 0;
1309 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001310 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001311 if (session_main_is_enabled () == 0)
Florin Corascea194d2017-10-02 00:18:51 -07001312 {
1313 rv = VNET_API_ERROR_FEATURE_DISABLED;
1314 goto done;
1315 }
1316
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001317 ns_id = vl_api_from_api_to_new_vec (&mp->namespace_id);
Florin Corascea194d2017-10-02 00:18:51 -07001318
Florin Corascea194d2017-10-02 00:18:51 -07001319 vnet_app_namespace_add_del_args_t args = {
1320 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001321 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001322 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1323 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1324 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1325 .is_add = 1
1326 };
Florin Corasc1a42652019-02-08 18:27:29 -08001327 rv = vnet_app_namespace_add_del (&args);
1328 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -08001329 {
1330 appns_index = app_namespace_index_from_id (ns_id);
1331 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1332 {
1333 clib_warning ("app ns lookup failed");
1334 rv = VNET_API_ERROR_UNSPECIFIED;
1335 }
1336 }
Florin Corascea194d2017-10-02 00:18:51 -07001337 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -08001338
1339 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -07001340done:
Florin Coras6e8c6672017-11-10 09:03:54 -08001341 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1342 if (!rv)
1343 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1344 }));
1345 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -07001346}
1347
Florin Coras1c710452017-10-17 00:03:13 -07001348static void
1349vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1350{
1351 vl_api_session_rule_add_del_reply_t *rmp;
1352 session_rule_add_del_args_t args;
1353 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -07001354 int rv = 0;
1355
Dave Barachb7b92992018-10-17 10:38:51 -04001356 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001357
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001358 ip_prefix_decode (&mp->lcl, &table_args->lcl);
1359 ip_prefix_decode (&mp->rmt, &table_args->rmt);
1360
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001361 table_args->lcl_port = mp->lcl_port;
1362 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001363 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1364 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001365 mp->tag[sizeof (mp->tag) - 1] = 0;
1366 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001367 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1368 args.scope = mp->scope;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001369 args.transport_proto =
1370 api_session_transport_proto_decode (&mp->transport_proto) ==
1371 TRANSPORT_PROTO_UDP ? 1 : 0;
Florin Coras1c710452017-10-17 00:03:13 -07001372
Florin Corasc1a42652019-02-08 18:27:29 -08001373 rv = vnet_session_rule_add_del (&args);
1374 if (rv)
1375 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -08001376 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001377 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1378}
1379
Florin Coras6c36f532017-11-03 18:32:34 -07001380static void
1381send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001382 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001383 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001384{
1385 vl_api_session_rules_details_t *rmp = 0;
1386 session_mask_or_match_4_t *match =
1387 (session_mask_or_match_4_t *) & rule->match;
1388 session_mask_or_match_4_t *mask =
1389 (session_mask_or_match_4_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001390 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -07001391
1392 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001393 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001394 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1395 rmp->context = context;
1396
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001397 clib_memset (&lcl, 0, sizeof (lcl));
1398 clib_memset (&rmt, 0, sizeof (rmt));
1399 ip_set (&lcl.fp_addr, &match->lcl_ip, 1);
1400 ip_set (&rmt.fp_addr, &match->rmt_ip, 1);
1401 lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip);
1402 rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip);
1403
1404 ip_prefix_encode (&lcl, &rmp->lcl);
1405 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001406 rmp->lcl_port = match->lcl_port;
1407 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001408 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1409 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001410 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
1411 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -07001412 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001413 if (tag)
1414 {
Dave Barach178cf492018-11-13 16:34:13 -05001415 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001416 rmp->tag[vec_len (tag)] = 0;
1417 }
Florin Coras6c36f532017-11-03 18:32:34 -07001418
Florin Coras6c4dae22018-01-09 06:39:23 -08001419 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001420}
1421
1422static void
Florin Corasc97a7392017-11-05 23:07:07 -08001423send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1424 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001425 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001426{
1427 vl_api_session_rules_details_t *rmp = 0;
1428 session_mask_or_match_6_t *match =
1429 (session_mask_or_match_6_t *) & rule->match;
1430 session_mask_or_match_6_t *mask =
1431 (session_mask_or_match_6_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001432 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -07001433
1434 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001435 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001436 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1437 rmp->context = context;
1438
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001439 clib_memset (&lcl, 0, sizeof (lcl));
1440 clib_memset (&rmt, 0, sizeof (rmt));
1441 ip_set (&lcl.fp_addr, &match->lcl_ip, 0);
1442 ip_set (&rmt.fp_addr, &match->rmt_ip, 0);
1443 lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip);
1444 rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip);
1445
1446 ip_prefix_encode (&lcl, &rmp->lcl);
1447 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001448 rmp->lcl_port = match->lcl_port;
1449 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001450 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001451 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001452 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
1453 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -07001454 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001455 if (tag)
1456 {
Dave Barach178cf492018-11-13 16:34:13 -05001457 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001458 rmp->tag[vec_len (tag)] = 0;
1459 }
Florin Coras6c36f532017-11-03 18:32:34 -07001460
Florin Coras6c4dae22018-01-09 06:39:23 -08001461 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001462}
1463
1464static void
1465send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001466 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001467 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001468{
1469 mma_rule_16_t *rule16;
1470 mma_rule_40_t *rule40;
1471 mma_rules_table_16_t *srt16;
1472 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001473 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001474
Florin Corasc97a7392017-11-05 23:07:07 -08001475 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001476 {
Florin Corasc97a7392017-11-05 23:07:07 -08001477 u8 *tag = 0;
1478 /* *INDENT-OFF* */
1479 srt16 = &srt->session_rules_tables_16;
1480 pool_foreach (rule16, srt16->rules, ({
1481 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1482 tag = session_rules_table_rule_tag (srt, ri, 1);
1483 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001484 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001485 }));
1486 /* *INDENT-ON* */
1487 }
1488 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1489 {
1490 u8 *tag = 0;
1491 /* *INDENT-OFF* */
1492 srt40 = &srt->session_rules_tables_40;
1493 pool_foreach (rule40, srt40->rules, ({
1494 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1495 tag = session_rules_table_rule_tag (srt, ri, 1);
1496 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001497 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001498 }));
1499 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001500 }
1501}
1502
1503static void
1504vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1505{
Florin Coras6c4dae22018-01-09 06:39:23 -08001506 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001507 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001508 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001509
Florin Coras6c4dae22018-01-09 06:39:23 -08001510 reg = vl_api_client_index_to_registration (mp->client_index);
1511 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001512 return;
1513
1514 /* *INDENT-OFF* */
1515 session_table_foreach (st, ({
Florin Coras07063b82020-03-13 04:44:51 +00001516 for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++)
Florin Corasc97a7392017-11-05 23:07:07 -08001517 {
1518 send_session_rules_table_details (&st->session_rules[tp],
1519 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001520 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001521 mp->context);
1522 }
Florin Coras6c36f532017-11-03 18:32:34 -07001523 }));
1524 /* *INDENT-ON* */
1525}
1526
Florin Coras371ca502018-02-21 12:07:41 -08001527static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001528vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001529{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001530 vl_api_app_add_cert_key_pair_reply_t *rmp;
1531 vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1532 u32 certkey_len, key_len, cert_len;
Florin Coras371ca502018-02-21 12:07:41 -08001533 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001534 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001535 {
1536 rv = VNET_API_ERROR_FEATURE_DISABLED;
1537 goto done;
1538 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001539
Florin Coras371ca502018-02-21 12:07:41 -08001540 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001541 if (cert_len > 10000)
1542 {
1543 rv = VNET_API_ERROR_INVALID_VALUE;
1544 goto done;
1545 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001546
1547 certkey_len = clib_net_to_host_u16 (mp->certkey_len);
1548 if (certkey_len < cert_len)
Florin Coras371ca502018-02-21 12:07:41 -08001549 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001550 rv = VNET_API_ERROR_INVALID_VALUE;
1551 goto done;
Florin Coras371ca502018-02-21 12:07:41 -08001552 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001553
1554 key_len = certkey_len - cert_len;
1555 if (key_len > 10000)
1556 {
1557 rv = VNET_API_ERROR_INVALID_VALUE;
1558 goto done;
1559 }
1560
1561 clib_memset (a, 0, sizeof (*a));
1562 vec_validate (a->cert, cert_len);
1563 vec_validate (a->key, key_len);
1564 clib_memcpy_fast (a->cert, mp->certkey, cert_len);
1565 clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len);
1566 rv = vnet_app_add_cert_key_pair (a);
Florin Coras371ca502018-02-21 12:07:41 -08001567 vec_free (a->cert);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001568 vec_free (a->key);
1569
Florin Coras371ca502018-02-21 12:07:41 -08001570done:
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001571 /* *INDENT-OFF* */
1572 REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
1573 if (!rv)
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001574 rmp->index = clib_host_to_net_u32 (a->index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001575 }));
1576 /* *INDENT-ON* */
Florin Coras371ca502018-02-21 12:07:41 -08001577}
1578
1579static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001580vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001581{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001582 vl_api_app_del_cert_key_pair_reply_t *rmp;
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001583 u32 ckpair_index;
Florin Coras371ca502018-02-21 12:07:41 -08001584 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001585 if (session_main_is_enabled () == 0)
1586 {
1587 rv = VNET_API_ERROR_FEATURE_DISABLED;
1588 goto done;
1589 }
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001590 ckpair_index = clib_net_to_host_u32 (mp->index);
1591 rv = vnet_app_del_cert_key_pair (ckpair_index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001592
1593done:
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001594 REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001595}
1596
1597/* ### WILL BE DEPRECATED POST 20.01 ### */
1598static void
1599vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1600 mp)
1601{
1602 vl_api_application_tls_cert_add_reply_t *rmp;
1603 app_cert_key_pair_t *ckpair;
1604 application_t *app;
1605 u32 cert_len;
1606 int rv = 0;
1607 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001608 {
1609 rv = VNET_API_ERROR_FEATURE_DISABLED;
1610 goto done;
1611 }
Florin Corase3e2f072018-03-04 07:24:30 -08001612 if (!(app = application_lookup (mp->client_index)))
1613 {
1614 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1615 goto done;
1616 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001617 cert_len = clib_net_to_host_u16 (mp->cert_len);
1618 if (cert_len > 10000)
1619 {
1620 rv = VNET_API_ERROR_INVALID_VALUE;
1621 goto done;
1622 }
1623 ckpair = app_cert_key_pair_get_default ();
1624 vec_validate (ckpair->cert, cert_len);
1625 clib_memcpy_fast (ckpair->cert, mp->cert, cert_len);
1626
1627done:
1628 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1629}
1630
1631/* ### WILL BE DEPRECATED POST 20.01 ### */
1632static void
1633vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1634 mp)
1635{
1636 vl_api_application_tls_key_add_reply_t *rmp;
1637 app_cert_key_pair_t *ckpair;
1638 application_t *app;
1639 u32 key_len;
1640 int rv = 0;
1641 if (session_main_is_enabled () == 0)
1642 {
1643 rv = VNET_API_ERROR_FEATURE_DISABLED;
1644 goto done;
1645 }
1646 if (!(app = application_lookup (mp->client_index)))
1647 {
1648 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1649 goto done;
1650 }
Florin Coras371ca502018-02-21 12:07:41 -08001651 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001652 if (key_len > 10000)
1653 {
1654 rv = VNET_API_ERROR_INVALID_VALUE;
1655 goto done;
1656 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001657 ckpair = app_cert_key_pair_get_default ();
1658 vec_validate (ckpair->key, key_len);
1659 clib_memcpy_fast (ckpair->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001660done:
1661 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1662}
1663
Florin Coras6cf30ad2017-04-04 23:08:23 -07001664static clib_error_t *
1665application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001666{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001667 application_t *app = application_lookup (client_index);
1668 vnet_app_detach_args_t _a, *a = &_a;
1669 if (app)
1670 {
Florin Coras15531972018-08-12 23:50:53 -07001671 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001672 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001673 vnet_application_detach (a);
1674 }
1675 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001676}
1677
Florin Coras6cf30ad2017-04-04 23:08:23 -07001678VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001679
1680#define vl_msg_name_crc_list
1681#include <vnet/vnet_all_api_h.h>
1682#undef vl_msg_name_crc_list
1683
1684static void
1685setup_message_id_table (api_main_t * am)
1686{
1687#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1688 foreach_vl_msg_name_crc_session;
1689#undef _
1690}
1691
1692/*
1693 * session_api_hookup
1694 * Add uri's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -05001695 * vlib has already mapped shared memory and
Dave Barach68b0fb02017-02-28 15:15:56 -05001696 * added the client registration handlers.
1697 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1698 */
1699static clib_error_t *
1700session_api_hookup (vlib_main_t * vm)
1701{
Dave Barach39d69112019-11-27 11:42:13 -05001702 api_main_t *am = vlibapi_get_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001703
1704#define _(N,n) \
1705 vl_msg_api_set_handlers(VL_API_##N, #n, \
1706 vl_api_##n##_t_handler, \
1707 vl_noop_handler, \
1708 vl_api_##n##_t_endian, \
1709 vl_api_##n##_t_print, \
1710 sizeof(vl_api_##n##_t), 1);
1711 foreach_session_api_msg;
1712#undef _
1713
1714 /*
1715 * Messages which bounce off the data-plane to
1716 * an API client. Simply tells the message handling infra not
1717 * to free the message.
1718 *
1719 * Bounced message handlers MUST NOT block the data plane
1720 */
1721 am->message_bounce[VL_API_CONNECT_URI] = 1;
1722 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1723
1724 /*
1725 * Set up the (msg_name, crc, message-id) table
1726 */
1727 setup_message_id_table (am);
1728
1729 return 0;
1730}
1731
1732VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001733
Dave Barach68b0fb02017-02-28 15:15:56 -05001734/*
1735 * fd.io coding-style-patch-verification: ON
1736 *
1737 * Local Variables:
1738 * eval: (c-set-style "gnu")
1739 * End:
1740 */