blob: eb5b2c36f99f357c59d09144bb38ad65d3a2f259 [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;
77 case TRANSPORT_PROTO_API_SCTP:
78 return TRANSPORT_PROTO_SCTP;
79 case TRANSPORT_PROTO_API_TLS:
80 return TRANSPORT_PROTO_TLS;
81 case TRANSPORT_PROTO_API_UDPC:
82 return TRANSPORT_PROTO_UDPC;
83 case TRANSPORT_PROTO_API_QUIC:
84 return TRANSPORT_PROTO_QUIC;
85 default:
86 return TRANSPORT_PROTO_NONE;
87 }
88}
89
90static vl_api_transport_proto_t
91api_session_transport_proto_encode (const transport_proto_t tp)
92{
93 switch (tp)
94 {
95 case TRANSPORT_PROTO_TCP:
96 return TRANSPORT_PROTO_API_TCP;
97 case TRANSPORT_PROTO_UDP:
98 return TRANSPORT_PROTO_API_UDP;
99 case TRANSPORT_PROTO_SCTP:
100 return TRANSPORT_PROTO_API_SCTP;
101 case TRANSPORT_PROTO_TLS:
102 return TRANSPORT_PROTO_API_TLS;
103 case TRANSPORT_PROTO_UDPC:
104 return TRANSPORT_PROTO_API_UDPC;
105 case TRANSPORT_PROTO_QUIC:
106 return TRANSPORT_PROTO_API_QUIC;
107 default:
108 return TRANSPORT_PROTO_API_NONE;
109 }
110}
111
Dave Barach68b0fb02017-02-28 15:15:56 -0500112static int
Florin Coras99368312018-08-02 10:45:44 -0700113session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
Florin Corasb384b542018-01-15 01:08:33 -0800114{
115 clib_error_t *error;
116 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
117 {
118 clib_warning ("can't send memfd fd");
119 return -1;
120 }
Florin Coras99368312018-08-02 10:45:44 -0700121 error = vl_api_send_fd_msg (reg, fds, n_fds);
Florin Corasb384b542018-01-15 01:08:33 -0800122 if (error)
123 {
124 clib_error_report (error);
125 return -1;
126 }
127 return 0;
128}
129
Florin Corasc4c4cf52019-08-24 18:17:34 -0700130/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Corasb384b542018-01-15 01:08:33 -0800131static int
Florin Corasfa76a762018-11-29 12:40:10 -0800132send_add_segment_callback (u32 api_client_index, u64 segment_handle)
Dave Barach68b0fb02017-02-28 15:15:56 -0500133{
Florin Coras99368312018-08-02 10:45:44 -0700134 int fds[SESSION_N_FD_TYPE], n_fds = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500135 vl_api_map_another_segment_t *mp;
Florin Corasb384b542018-01-15 01:08:33 -0800136 vl_api_registration_t *reg;
Florin Coras88001c62019-04-24 14:44:46 -0700137 fifo_segment_t *fs;
Florin Corasfa76a762018-11-29 12:40:10 -0800138 ssvm_private_t *sp;
Florin Coras99368312018-08-02 10:45:44 -0700139 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500140
Florin Corasb384b542018-01-15 01:08:33 -0800141 reg = vl_mem_api_client_index_to_registration (api_client_index);
142 if (!reg)
143 {
Florin Corasfa76a762018-11-29 12:40:10 -0800144 clib_warning ("no api registration for client: %u", api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800145 return -1;
146 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500147
Florin Corasfa76a762018-11-29 12:40:10 -0800148 fs = segment_manager_get_segment_w_handle (segment_handle);
149 sp = &fs->ssvm;
Florin Coras99368312018-08-02 10:45:44 -0700150 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
Florin Corasb384b542018-01-15 01:08:33 -0800151 {
Florin Coras99368312018-08-02 10:45:44 -0700152 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
153 {
154 clib_warning ("can't send memfd fd");
155 return -1;
156 }
157
158 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
159 fds[n_fds] = sp->fd;
160 n_fds += 1;
Florin Corasb384b542018-01-15 01:08:33 -0800161 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500162
Florin Coras99368312018-08-02 10:45:44 -0700163 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400164 clib_memset (mp, 0, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500165 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
Florin Corasb384b542018-01-15 01:08:33 -0800166 mp->segment_size = sp->ssvm_size;
Florin Coras99368312018-08-02 10:45:44 -0700167 mp->fd_flags = fd_flags;
Florin Corasfa76a762018-11-29 12:40:10 -0800168 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasb384b542018-01-15 01:08:33 -0800169 strncpy ((char *) mp->segment_name, (char *) sp->name,
Dave Barach68b0fb02017-02-28 15:15:56 -0500170 sizeof (mp->segment_name) - 1);
171
Florin Corasb384b542018-01-15 01:08:33 -0800172 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
173
Florin Coras99368312018-08-02 10:45:44 -0700174 if (n_fds)
175 return session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500176
177 return 0;
178}
179
Florin Corasc4c4cf52019-08-24 18:17:34 -0700180/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -0500181static int
Florin Corasfa76a762018-11-29 12:40:10 -0800182send_del_segment_callback (u32 api_client_index, u64 segment_handle)
Florin Corasf8f516a2018-02-08 15:10:09 -0800183{
184 vl_api_unmap_segment_t *mp;
185 vl_api_registration_t *reg;
186
187 reg = vl_mem_api_client_index_to_registration (api_client_index);
188 if (!reg)
189 {
190 clib_warning ("no registration: %u", api_client_index);
191 return -1;
192 }
193
Florin Coras99368312018-08-02 10:45:44 -0700194 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400195 clib_memset (mp, 0, sizeof (*mp));
Florin Corasf8f516a2018-02-08 15:10:09 -0800196 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
Florin Corasfa76a762018-11-29 12:40:10 -0800197 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasf8f516a2018-02-08 15:10:09 -0800198 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
199
Florin Coras99368312018-08-02 10:45:44 -0700200 return 0;
201}
202
203static int
Florin Coras83ea6692018-10-12 16:55:14 -0700204mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
205{
206 int rv;
207 u8 try = 0;
208 while (try < 100)
209 {
210 rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
211 SESSION_MQ_CTRL_EVT_RING,
212 SVM_Q_NOWAIT, msg);
213 if (!rv)
214 return 0;
215 try++;
Florin Corase2ea1932018-12-17 23:08:14 -0800216 usleep (1);
Florin Coras83ea6692018-10-12 16:55:14 -0700217 }
Florin Corasdc2e2512018-12-03 17:47:26 -0800218 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700219 return -1;
220}
221
222static int
Florin Coras288eaab2019-02-03 15:26:14 -0800223mq_send_session_accepted_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700224{
Florin Coras15531972018-08-12 23:50:53 -0700225 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700226 svm_msg_q_msg_t _msg, *msg = &_msg;
227 svm_msg_q_t *vpp_queue, *app_mq;
Florin Coras288eaab2019-02-03 15:26:14 -0800228 session_t *listener;
Florin Coras52207f12018-07-12 14:48:06 -0700229 session_accepted_msg_t *mp;
230 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700231 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700232
Florin Coras15531972018-08-12 23:50:53 -0700233 app = application_get (app_wrk->app_index);
234 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700235 if (mq_try_lock_and_alloc_msg (app_mq, msg))
236 return -1;
Florin Coras52207f12018-07-12 14:48:06 -0700237
238 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400239 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700240 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
241 mp = (session_accepted_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800242 clib_memset (mp, 0, sizeof (*mp));
Florin Corasab2f6db2018-08-31 14:31:41 -0700243 mp->context = app->app_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800244 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
245 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800246 mp->segment_handle = session_segment_handle (s);
Nathan Skrzypczakc00f4802019-12-03 16:25:11 +0100247 mp->flags = s->flags;
Florin Coras52207f12018-07-12 14:48:06 -0700248
249 if (session_has_transport (s))
250 {
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200251 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras87d66332019-06-11 12:31:31 -0700252 mp->listener_handle = app_listen_session_handle (listener);
Florin Coras52207f12018-07-12 14:48:06 -0700253 if (application_is_proxy (app))
254 {
255 listener =
Florin Coras15531972018-08-12 23:50:53 -0700256 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
257 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700258 if (listener)
259 mp->listener_handle = listen_session_get_handle (listener);
260 }
Florin Coras31c99552019-03-01 13:00:58 -0800261 vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700262 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
263 mp->handle = session_handle (s);
Aloys Augustincdb71702019-04-08 17:54:39 +0200264
Florin Coras09d18c22019-04-24 11:10:02 -0700265 session_get_endpoint (s, &mp->rmt, 0 /* is_lcl */ );
Florin Coras52207f12018-07-12 14:48:06 -0700266 }
267 else
268 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800269 ct_connection_t *ct;
Florin Coras99368312018-08-02 10:45:44 -0700270
Florin Coras2b81e3c2019-02-27 07:55:46 -0800271 ct = (ct_connection_t *) session_get_transport (s);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200272 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras87d66332019-06-11 12:31:31 -0700273 mp->listener_handle = app_listen_session_handle (listener);
Florin Coras09d18c22019-04-24 11:10:02 -0700274 mp->rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
275 mp->rmt.port = ct->c_rmt_port;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800276 mp->handle = session_handle (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800277 vpp_queue = session_main_get_vpp_event_queue (0);
Florin Coras54693d22018-07-17 10:46:29 -0700278 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Coras52207f12018-07-12 14:48:06 -0700279 }
Florin Coras537b17e2018-09-28 10:35:45 -0700280 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700281
282 return 0;
283}
284
Florin Coras72b04282019-01-14 17:23:11 -0800285static inline void
286mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
287 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700288{
Florin Coras52207f12018-07-12 14:48:06 -0700289 svm_msg_q_msg_t _msg, *msg = &_msg;
290 session_disconnected_msg_t *mp;
291 svm_msg_q_t *app_mq;
292 session_event_t *evt;
293
Florin Coras15531972018-08-12 23:50:53 -0700294 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700295 if (mq_try_lock_and_alloc_msg (app_mq, msg))
296 return;
Florin Coras52207f12018-07-12 14:48:06 -0700297 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400298 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800299 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700300 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800301 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800302 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700303 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700304}
305
Florin Coras72b04282019-01-14 17:23:11 -0800306static inline void
307mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
308 svm_fifo_t * f, session_evt_type_t evt_type)
309{
310 app_worker_t *app_wrk;
311 application_t *app;
312 int i;
313
314 app = application_get (app_index);
315 if (!app)
316 return;
317
318 for (i = 0; i < f->n_subscribers; i++)
319 {
320 if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
321 continue;
322 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
323 }
324}
325
326static void
Florin Coras288eaab2019-02-03 15:26:14 -0800327mq_send_session_disconnected_cb (session_t * s)
Florin Coras72b04282019-01-14 17:23:11 -0800328{
329 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
330 session_handle_t sh = session_handle (s);
331
332 mq_send_session_close_evt (app_wrk, session_handle (s),
333 SESSION_CTRL_EVT_DISCONNECTED);
334
Florin Coras288eaab2019-02-03 15:26:14 -0800335 if (svm_fifo_n_subscribers (s->rx_fifo))
336 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800337 SESSION_CTRL_EVT_DISCONNECTED);
338}
339
Florin Coras52207f12018-07-12 14:48:06 -0700340static void
Florin Coras288eaab2019-02-03 15:26:14 -0800341mq_send_session_reset_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700342{
Florin Coras72b04282019-01-14 17:23:11 -0800343 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
344 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700345
Florin Coras72b04282019-01-14 17:23:11 -0800346 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
347
Florin Coras288eaab2019-02-03 15:26:14 -0800348 if (svm_fifo_n_subscribers (s->rx_fifo))
349 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800350 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700351}
352
Florin Coras458089b2019-08-21 16:20:44 -0700353int
Florin Coras15531972018-08-12 23:50:53 -0700354mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800355 session_t * s, u8 is_fail)
Florin Coras52207f12018-07-12 14:48:06 -0700356{
357 svm_msg_q_msg_t _msg, *msg = &_msg;
358 session_connected_msg_t *mp;
359 svm_msg_q_t *vpp_mq, *app_mq;
360 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700361 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700362 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700363
Florin Coras15531972018-08-12 23:50:53 -0700364 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700365 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700366 if (!app_mq)
367 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800368 clib_warning ("app %u with api index: %u not attached",
369 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700370 return -1;
371 }
372
Florin Coras83ea6692018-10-12 16:55:14 -0700373 if (mq_try_lock_and_alloc_msg (app_mq, msg))
374 return -1;
Florin Corasdc2e2512018-12-03 17:47:26 -0800375
Florin Coras52207f12018-07-12 14:48:06 -0700376 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400377 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700378 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
379 mp = (session_connected_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800380 clib_memset (mp, 0, sizeof (*mp));
Florin Coras52207f12018-07-12 14:48:06 -0700381 mp->context = api_context;
382
383 if (is_fail)
384 goto done;
385
386 if (session_has_transport (s))
387 {
388 tc = session_get_transport (s);
389 if (!tc)
390 {
391 is_fail = 1;
392 goto done;
393 }
394
Florin Coras31c99552019-03-01 13:00:58 -0800395 vpp_mq = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700396 mp->handle = session_handle (s);
397 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Aloys Augustincdb71702019-04-08 17:54:39 +0200398
Florin Coras09d18c22019-04-24 11:10:02 -0700399 session_get_endpoint (s, &mp->lcl, 1 /* is_lcl */ );
Aloys Augustincdb71702019-04-08 17:54:39 +0200400
Florin Coras288eaab2019-02-03 15:26:14 -0800401 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
402 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800403 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700404 }
405 else
406 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800407 ct_connection_t *cct;
408 session_t *ss;
Florin Coras99368312018-08-02 10:45:44 -0700409
Florin Coras2b81e3c2019-02-27 07:55:46 -0800410 cct = (ct_connection_t *) session_get_transport (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800411 mp->handle = session_handle (s);
Florin Coras09d18c22019-04-24 11:10:02 -0700412 mp->lcl.port = cct->c_lcl_port;
413 mp->lcl.is_ip4 = cct->c_is_ip4;
Florin Coras653e43f2019-03-04 10:56:23 -0800414 vpp_mq = session_main_get_vpp_event_queue (0);
Florin Coras54693d22018-07-17 10:46:29 -0700415 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras653e43f2019-03-04 10:56:23 -0800416 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
417 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
418 mp->segment_handle = session_segment_handle (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800419 ss = ct_session_get_peer (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800420 mp->ct_rx_fifo = pointer_to_uword (ss->tx_fifo);
421 mp->ct_tx_fifo = pointer_to_uword (ss->rx_fifo);
422 mp->ct_segment_handle = session_segment_handle (ss);
Florin Coras52207f12018-07-12 14:48:06 -0700423 }
424
425done:
426 mp->retval = is_fail ?
427 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
428
Florin Coras537b17e2018-09-28 10:35:45 -0700429 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700430 return 0;
431}
432
Florin Coras458089b2019-08-21 16:20:44 -0700433int
Florin Coras60116992018-08-27 09:52:18 -0700434mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
435 session_handle_t handle, int rv)
436{
437 svm_msg_q_msg_t _msg, *msg = &_msg;
438 svm_msg_q_t *app_mq, *vpp_evt_q;
Yu Ping0b819152019-05-07 02:24:30 +0800439 transport_endpoint_t tep;
Florin Coras60116992018-08-27 09:52:18 -0700440 session_bound_msg_t *mp;
441 app_worker_t *app_wrk;
442 session_event_t *evt;
Florin Corasc9940fc2019-02-05 20:55:11 -0800443 app_listener_t *al;
444 session_t *ls = 0;
Florin Coras60116992018-08-27 09:52:18 -0700445 app_wrk = app_worker_get (app_wrk_index);
Florin Coras60116992018-08-27 09:52:18 -0700446 app_mq = app_wrk->event_queue;
447 if (!app_mq)
448 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800449 clib_warning ("app %u with api index: %u not attached",
450 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700451 return -1;
452 }
453
Florin Coras83ea6692018-10-12 16:55:14 -0700454 if (mq_try_lock_and_alloc_msg (app_mq, msg))
455 return -1;
456
Florin Coras60116992018-08-27 09:52:18 -0700457 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400458 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700459 evt->event_type = SESSION_CTRL_EVT_BOUND;
460 mp = (session_bound_msg_t *) evt->data;
461 mp->context = api_context;
462
463 if (rv)
464 goto done;
465
466 mp->handle = handle;
Florin Corasd4295e62019-02-22 13:11:38 -0800467 al = app_listener_get_w_handle (handle);
468 if (al->session_index != SESSION_INVALID_INDEX)
469 ls = app_listener_get_session (al);
Florin Coras60116992018-08-27 09:52:18 -0700470 else
Florin Corasd4295e62019-02-22 13:11:38 -0800471 ls = app_listener_get_local_session (al);
Yu Ping0b819152019-05-07 02:24:30 +0800472
473 session_get_endpoint (ls, &tep, 1 /* is_lcl */ );
474 mp->lcl_port = tep.port;
475 mp->lcl_is_ip4 = tep.is_ip4;
476 clib_memcpy_fast (mp->lcl_ip, &tep.ip, sizeof (tep.ip));
Florin Coras60116992018-08-27 09:52:18 -0700477
Florin Coras31c99552019-03-01 13:00:58 -0800478 vpp_evt_q = session_main_get_vpp_event_queue (0);
Florin Coras5f45e012019-01-23 09:21:30 -0800479 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
480
Florin Coras2b81e3c2019-02-27 07:55:46 -0800481 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras60116992018-08-27 09:52:18 -0700482 {
Florin Coras288eaab2019-02-03 15:26:14 -0800483 mp->rx_fifo = pointer_to_uword (ls->rx_fifo);
484 mp->tx_fifo = pointer_to_uword (ls->tx_fifo);
Florin Coras60116992018-08-27 09:52:18 -0700485 }
486
487done:
488 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700489 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700490 return 0;
491}
492
Florin Coras458089b2019-08-21 16:20:44 -0700493void
494mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
495 u32 context, int rv)
496{
497 svm_msg_q_msg_t _msg, *msg = &_msg;
498 session_unlisten_reply_msg_t *ump;
499 svm_msg_q_t *app_mq;
500 session_event_t *evt;
501
502 app_mq = app_wrk->event_queue;
503 if (mq_try_lock_and_alloc_msg (app_mq, msg))
504 return;
505
506 evt = svm_msg_q_msg_data (app_mq, msg);
507 clib_memset (evt, 0, sizeof (*evt));
508 evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY;
509 ump = (session_unlisten_reply_msg_t *) evt->data;
510 ump->context = context;
511 ump->handle = sh;
512 ump->retval = rv;
513 svm_msg_q_add_and_unlock (app_mq, msg);
514}
515
Florin Coras49568af2019-07-31 16:46:24 -0700516static void
517mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh)
518{
Florin Coras68b7e582020-01-21 18:33:23 -0800519 svm_msg_q_msg_t _msg, *msg = &_msg;
520 session_migrated_msg_t *mp;
521 svm_msg_q_t *vpp_evt_q;
522 app_worker_t *app_wrk;
523 session_event_t *evt;
524 svm_msg_q_t *app_mq;
525
526 app_wrk = app_worker_get (s->app_wrk_index);
527 app_mq = app_wrk->event_queue;
528 if (mq_try_lock_and_alloc_msg (app_mq, msg))
529 return;
530
531 evt = svm_msg_q_msg_data (app_mq, msg);
532 clib_memset (evt, 0, sizeof (*evt));
533 evt->event_type = SESSION_CTRL_EVT_MIGRATED;
534 mp = (session_migrated_msg_t *) evt->data;
535 mp->handle = session_handle (s);
536 mp->new_handle = new_sh;
537 mp->vpp_thread_index = session_thread_from_handle (new_sh);
538 vpp_evt_q = session_main_get_vpp_event_queue (mp->vpp_thread_index);
539 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
540 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras49568af2019-07-31 16:46:24 -0700541}
542
Florin Corasc4c4cf52019-08-24 18:17:34 -0700543static int
544mq_send_add_segment_cb (u32 app_wrk_index, u64 segment_handle)
545{
546 int fds[SESSION_N_FD_TYPE], n_fds = 0;
547 svm_msg_q_msg_t _msg, *msg = &_msg;
548 session_app_add_segment_msg_t *mp;
549 vl_api_registration_t *reg;
550 app_worker_t *app_wrk;
551 session_event_t *evt;
552 svm_msg_q_t *app_mq;
553 fifo_segment_t *fs;
554 ssvm_private_t *sp;
555 u8 fd_flags = 0;
556
557 app_wrk = app_worker_get (app_wrk_index);
558
559 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
560 if (!reg)
561 {
562 clib_warning ("no api registration for client: %u",
563 app_wrk->api_client_index);
564 return -1;
565 }
566
567 fs = segment_manager_get_segment_w_handle (segment_handle);
568 sp = &fs->ssvm;
569 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
570 {
571 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
572 {
573 clib_warning ("can't send memfd fd");
574 return -1;
575 }
576
577 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
578 fds[n_fds] = sp->fd;
579 n_fds += 1;
580 }
581
582 app_mq = app_wrk->event_queue;
583 if (mq_try_lock_and_alloc_msg (app_mq, msg))
584 return -1;
585
586 if (n_fds)
587 session_send_fds (reg, fds, n_fds);
588
589 evt = svm_msg_q_msg_data (app_mq, msg);
590 clib_memset (evt, 0, sizeof (*evt));
591 evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT;
592 mp = (session_app_add_segment_msg_t *) evt->data;
593 clib_memset (mp, 0, sizeof (*mp));
594 mp->segment_size = sp->ssvm_size;
595 mp->fd_flags = fd_flags;
596 mp->segment_handle = segment_handle;
597 strncpy ((char *) mp->segment_name, (char *) sp->name,
598 sizeof (mp->segment_name) - 1);
599
600 svm_msg_q_add_and_unlock (app_mq, msg);
601
602 return 0;
603}
604
605static int
606mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle)
607{
608 svm_msg_q_msg_t _msg, *msg = &_msg;
609 session_app_del_segment_msg_t *mp;
610 vl_api_registration_t *reg;
611 app_worker_t *app_wrk;
612 session_event_t *evt;
613 svm_msg_q_t *app_mq;
614
615 app_wrk = app_worker_get (app_wrk_index);
616 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
617 if (!reg)
618 {
619 clib_warning ("no registration: %u", app_wrk->api_client_index);
620 return -1;
621 }
622
623 app_mq = app_wrk->event_queue;
624 if (mq_try_lock_and_alloc_msg (app_mq, msg))
625 return -1;
626
627 evt = svm_msg_q_msg_data (app_mq, msg);
628 clib_memset (evt, 0, sizeof (*evt));
629 evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT;
630 mp = (session_app_del_segment_msg_t *) evt->data;
631 clib_memset (mp, 0, sizeof (*mp));
632 mp->segment_handle = segment_handle;
633 svm_msg_q_add_and_unlock (app_mq, msg);
634
635 return 0;
636}
637
Florin Coras9ace36d2019-10-28 13:14:17 -0700638static void
639mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf)
640{
641 svm_msg_q_msg_t _msg, *msg = &_msg;
642 session_cleanup_msg_t *mp;
643 svm_msg_q_t *app_mq;
644 session_event_t *evt;
645 app_worker_t *app_wrk;
646
647 /* Only propagate session cleanup notification */
648 if (ntf == SESSION_CLEANUP_TRANSPORT)
649 return;
650
651 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
652 if (!app_wrk)
653 return;
654
655 app_mq = app_wrk->event_queue;
656 if (mq_try_lock_and_alloc_msg (app_mq, msg))
657 return;
658
659 evt = svm_msg_q_msg_data (app_mq, msg);
660 clib_memset (evt, 0, sizeof (*evt));
661 evt->event_type = SESSION_CTRL_EVT_CLEANUP;
662 mp = (session_cleanup_msg_t *) evt->data;
663 mp->handle = session_handle (s);
664 svm_msg_q_add_and_unlock (app_mq, msg);
665}
666
Florin Corasc4c4cf52019-08-24 18:17:34 -0700667/* ### WILL BE DEPRECATED POST 20.01 ### */
668static session_cb_vft_t session_mq_cb_vft_old = {
Florin Coras52207f12018-07-12 14:48:06 -0700669 .session_accept_callback = mq_send_session_accepted_cb,
670 .session_disconnect_callback = mq_send_session_disconnected_cb,
671 .session_connected_callback = mq_send_session_connected_cb,
672 .session_reset_callback = mq_send_session_reset_cb,
Florin Coras49568af2019-07-31 16:46:24 -0700673 .session_migrate_callback = mq_send_session_migrate_cb,
Florin Coras52207f12018-07-12 14:48:06 -0700674 .add_segment_callback = send_add_segment_callback,
675 .del_segment_callback = send_del_segment_callback,
676};
677
Florin Corasc4c4cf52019-08-24 18:17:34 -0700678static session_cb_vft_t session_mq_cb_vft = {
679 .session_accept_callback = mq_send_session_accepted_cb,
680 .session_disconnect_callback = mq_send_session_disconnected_cb,
681 .session_connected_callback = mq_send_session_connected_cb,
682 .session_reset_callback = mq_send_session_reset_cb,
683 .session_migrate_callback = mq_send_session_migrate_cb,
Florin Coras9ace36d2019-10-28 13:14:17 -0700684 .session_cleanup_callback = mq_send_session_cleanup_cb,
Florin Corasc4c4cf52019-08-24 18:17:34 -0700685 .add_segment_callback = mq_send_add_segment_cb,
686 .del_segment_callback = mq_send_del_segment_cb,
687};
688
Dave Barach68b0fb02017-02-28 15:15:56 -0500689static void
Florin Corase04c2992017-03-01 08:17:34 -0800690vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
691{
692 vl_api_session_enable_disable_reply_t *rmp;
693 vlib_main_t *vm = vlib_get_main ();
694 int rv = 0;
695
696 vnet_session_enable_disable (vm, mp->is_enable);
697 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
698}
699
Florin Coras458089b2019-08-21 16:20:44 -0700700/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Corase04c2992017-03-01 08:17:34 -0800701static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700702vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500703{
Florin Coras99368312018-08-02 10:45:44 -0700704 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700705 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800706 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700707 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800708 vl_api_registration_t *reg;
Florin Coras99368312018-08-02 10:45:44 -0700709 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500710
Florin Corasfc804d92018-01-26 01:27:01 -0800711 reg = vl_api_client_index_to_registration (mp->client_index);
712 if (!reg)
713 return;
714
Florin Coras31c99552019-03-01 13:00:58 -0800715 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700716 {
717 rv = VNET_API_ERROR_FEATURE_DISABLED;
718 goto done;
719 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500720
Florin Corasff6e7692017-12-11 04:59:01 -0800721 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700722 sizeof (mp->options),
723 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500724
Dave Barachb7b92992018-10-17 10:38:51 -0400725 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500726 a->api_client_index = mp->client_index;
727 a->options = mp->options;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700728 a->session_cb_vft = &session_mq_cb_vft_old;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100729 a->namespace_id =
730 (u8 *) vl_api_from_api_to_new_c_string (&mp->namespace_id);
Florin Corascea194d2017-10-02 00:18:51 -0700731
Florin Corasc1a42652019-02-08 18:27:29 -0800732 if ((rv = vnet_application_attach (a)))
Florin Corascea194d2017-10-02 00:18:51 -0700733 {
Florin Corasc1a42652019-02-08 18:27:29 -0800734 clib_warning ("attach returned: %d", rv);
Florin Coras99368312018-08-02 10:45:44 -0700735 vec_free (a->namespace_id);
736 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700737 }
738 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500739
Florin Coras99368312018-08-02 10:45:44 -0700740 /* Send event queues segment */
Florin Coras31c99552019-03-01 13:00:58 -0800741 if ((evt_q_segment = session_main_get_evt_q_segment ()))
Florin Coras99368312018-08-02 10:45:44 -0700742 {
743 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
744 fds[n_fds] = evt_q_segment->fd;
745 n_fds += 1;
746 }
747 /* Send fifo segment fd if needed */
748 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
749 {
750 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
751 fds[n_fds] = a->segment->fd;
752 n_fds += 1;
753 }
754 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
755 {
756 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
757 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
758 n_fds += 1;
759 }
760
Florin Coras6cf30ad2017-04-04 23:08:23 -0700761done:
Florin Corasa5464812017-04-19 13:00:05 -0700762
Dave Barach68b0fb02017-02-28 15:15:56 -0500763 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700764 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500765 if (!rv)
766 {
Florin Corasb384b542018-01-15 01:08:33 -0800767 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800768 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100769 vl_api_vec_to_api_string (segp->name, &rmp->segment_name);
Florin Corasb384b542018-01-15 01:08:33 -0800770 rmp->segment_size = segp->ssvm_size;
Florin Coras99368312018-08-02 10:45:44 -0700771 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
772 rmp->n_fds = n_fds;
773 rmp->fd_flags = fd_flags;
Florin Corasd85de682018-11-29 17:02:29 -0800774 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500775 }
776 }));
777 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800778
Florin Coras99368312018-08-02 10:45:44 -0700779 if (n_fds)
780 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500781}
782
783static void
Florin Coras458089b2019-08-21 16:20:44 -0700784vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
785{
786 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
787 vl_api_app_attach_reply_t *rmp;
788 ssvm_private_t *segp, *evt_q_segment;
789 vnet_app_attach_args_t _a, *a = &_a;
790 u8 fd_flags = 0, ctrl_thread;
791 vl_api_registration_t *reg;
792 svm_msg_q_t *ctrl_mq;
793
794 reg = vl_api_client_index_to_registration (mp->client_index);
795 if (!reg)
796 return;
797
798 if (session_main_is_enabled () == 0)
799 {
800 rv = VNET_API_ERROR_FEATURE_DISABLED;
801 goto done;
802 }
803
804 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
805 sizeof (mp->options),
806 "Out of options, fix api message definition");
807
808 clib_memset (a, 0, sizeof (*a));
809 a->api_client_index = mp->client_index;
810 a->options = mp->options;
811 a->session_cb_vft = &session_mq_cb_vft;
Florin Coras458089b2019-08-21 16:20:44 -0700812
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100813 a->namespace_id = vl_api_from_api_to_new_vec (&mp->namespace_id);
Florin Coras458089b2019-08-21 16:20:44 -0700814
815 if ((rv = vnet_application_attach (a)))
816 {
817 clib_warning ("attach returned: %d", rv);
818 vec_free (a->namespace_id);
819 goto done;
820 }
821 vec_free (a->namespace_id);
822
823 /* Send event queues segment */
824 if ((evt_q_segment = session_main_get_evt_q_segment ()))
825 {
826 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
827 fds[n_fds] = evt_q_segment->fd;
828 n_fds += 1;
829 }
830 /* Send fifo segment fd if needed */
831 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
832 {
833 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
834 fds[n_fds] = a->segment->fd;
835 n_fds += 1;
836 }
837 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
838 {
839 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
840 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
841 n_fds += 1;
842 }
843
844done:
Florin Coras458089b2019-08-21 16:20:44 -0700845 /* *INDENT-OFF* */
846 REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({
847 if (!rv)
848 {
Vratko Polak4f599852019-11-08 10:32:39 +0100849 ctrl_thread = vlib_num_workers () ? 1 : 0;
Nathan Skrzypczak9d3e1b42019-11-07 10:29:24 +0100850 ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
Florin Coras458089b2019-08-21 16:20:44 -0700851 segp = a->segment;
852 rmp->app_index = clib_host_to_net_u32 (a->app_index);
853 rmp->app_mq = pointer_to_uword (a->app_evt_q);
854 rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
855 rmp->vpp_ctrl_mq_thread = ctrl_thread;
856 rmp->n_fds = n_fds;
857 rmp->fd_flags = fd_flags;
858 if (vec_len (segp->name))
859 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100860 vl_api_vec_to_api_string (segp->name, &rmp->segment_name);
Florin Coras458089b2019-08-21 16:20:44 -0700861 }
862 rmp->segment_size = segp->ssvm_size;
863 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
864 }
865 }));
866 /* *INDENT-ON* */
867
868 if (n_fds)
869 session_send_fds (reg, fds, n_fds);
870}
871
872/* ### WILL BE DEPRECATED POST 20.01 ### */
873static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700874vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
875{
876 vl_api_application_detach_reply_t *rmp;
877 int rv = VNET_API_ERROR_INVALID_VALUE_2;
878 vnet_app_detach_args_t _a, *a = &_a;
879 application_t *app;
880
Florin Coras31c99552019-03-01 13:00:58 -0800881 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700882 {
883 rv = VNET_API_ERROR_FEATURE_DISABLED;
884 goto done;
885 }
886
887 app = application_lookup (mp->client_index);
888 if (app)
889 {
Florin Coras15531972018-08-12 23:50:53 -0700890 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800891 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700892 rv = vnet_application_detach (a);
893 }
894
895done:
896 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
897}
898
Florin Coras458089b2019-08-21 16:20:44 -0700899/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700900static void
901vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
902{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700903 vl_api_bind_uri_reply_t *rmp;
Florin Coras64424012019-03-02 10:47:47 -0800904 vnet_listen_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700905 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -0700906 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700907 int rv;
908
Florin Coras31c99552019-03-01 13:00:58 -0800909 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700910 {
911 rv = VNET_API_ERROR_FEATURE_DISABLED;
912 goto done;
913 }
914
915 app = application_lookup (mp->client_index);
916 if (app)
917 {
Dave Barachb7b92992018-10-17 10:38:51 -0400918 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700919 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700920 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700921 rv = vnet_bind_uri (a);
922 }
923 else
924 {
925 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
926 }
927
928done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700929
Florin Coras64424012019-03-02 10:47:47 -0800930 REPLY_MACRO (VL_API_BIND_URI_REPLY);
Florin Coras60116992018-08-27 09:52:18 -0700931
Florin Coras64424012019-03-02 10:47:47 -0800932 if (app)
Florin Coras60116992018-08-27 09:52:18 -0700933 {
934 app_wrk = application_get_worker (app, 0);
935 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
936 rv);
937 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700938}
939
Florin Coras458089b2019-08-21 16:20:44 -0700940/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700941static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500942vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
943{
944 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700945 application_t *app;
Florin Corasc1a42652019-02-08 18:27:29 -0800946 vnet_unlisten_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500947 int rv;
948
Florin Coras31c99552019-03-01 13:00:58 -0800949 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700950 {
951 rv = VNET_API_ERROR_FEATURE_DISABLED;
952 goto done;
953 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500954
Florin Coras6cf30ad2017-04-04 23:08:23 -0700955 app = application_lookup (mp->client_index);
956 if (app)
957 {
958 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700959 a->app_index = app->app_index;
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +0200960 a->wrk_map_index = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700961 rv = vnet_unbind_uri (a);
962 }
963 else
964 {
965 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
966 }
967
968done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500969 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
970}
971
Florin Coras458089b2019-08-21 16:20:44 -0700972/* ### WILL BE DEPRECATED POST 20.01 ### */
Florin Corasf03a59a2017-06-09 21:07:32 -0700973static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500974vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
975{
Florin Coras64424012019-03-02 10:47:47 -0800976 vl_api_connect_uri_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500977 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700978 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700979 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500980
Florin Coras31c99552019-03-01 13:00:58 -0800981 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700982 {
983 rv = VNET_API_ERROR_FEATURE_DISABLED;
984 goto done;
985 }
Florin Corase04c2992017-03-01 08:17:34 -0800986
Florin Coras6cf30ad2017-04-04 23:08:23 -0700987 app = application_lookup (mp->client_index);
988 if (app)
989 {
Dave Barachb7b92992018-10-17 10:38:51 -0400990 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700991 a->uri = (char *) mp->uri;
992 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -0700993 a->app_index = app->app_index;
Florin Corasc1a42652019-02-08 18:27:29 -0800994 if ((rv = vnet_connect_uri (a)))
995 clib_warning ("connect_uri returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700996 }
997 else
998 {
999 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1000 }
Florin Corase04c2992017-03-01 08:17:34 -08001001
Florin Coras3cbc04b2017-10-02 00:18:51 -07001002 /*
1003 * Don't reply to stream (tcp) connects. The reply will come once
1004 * the connection is established. In case of the redirects, the reply
1005 * will come from the server app.
1006 */
Florin Coras8f89dd02018-03-05 16:53:07 -08001007 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001008 return;
1009
Florin Coras6cf30ad2017-04-04 23:08:23 -07001010done:
Florin Coras64424012019-03-02 10:47:47 -08001011 REPLY_MACRO (VL_API_CONNECT_URI_REPLY);
Dave Barach68b0fb02017-02-28 15:15:56 -05001012}
1013
Florin Coras458089b2019-08-21 16:20:44 -07001014/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001015static void
1016vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
1017{
1018 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001019 vnet_disconnect_args_t _a, *a = &_a;
1020 application_t *app;
1021 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001022
Florin Coras31c99552019-03-01 13:00:58 -08001023 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001024 {
1025 rv = VNET_API_ERROR_FEATURE_DISABLED;
1026 goto done;
1027 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001028
Florin Coras6cf30ad2017-04-04 23:08:23 -07001029 app = application_lookup (mp->client_index);
1030 if (app)
1031 {
1032 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001033 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001034 rv = vnet_disconnect_session (a);
1035 }
1036 else
1037 {
1038 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1039 }
1040
1041done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001042 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001043}
1044
Florin Coras458089b2019-08-21 16:20:44 -07001045/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001046static void
1047vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1048 mp)
1049{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001050 vnet_disconnect_args_t _a, *a = &_a;
1051 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001052
1053 /* Client objected to disconnecting the session, log and continue */
1054 if (mp->retval)
1055 {
1056 clib_warning ("client retval %d", mp->retval);
1057 return;
1058 }
1059
1060 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001061 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001062 if (app)
1063 {
1064 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001065 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001066 vnet_disconnect_session (a);
1067 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001068}
1069
Florin Corasc4c4cf52019-08-24 18:17:34 -07001070/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001071static void
Dave Barach68b0fb02017-02-28 15:15:56 -05001072vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1073 * mp)
1074{
1075 clib_warning ("not implemented");
1076}
1077
Florin Coras458089b2019-08-21 16:20:44 -07001078/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001079static void
1080vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1081{
Florin Corasc9940fc2019-02-05 20:55:11 -08001082 vnet_listen_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -05001083 vl_api_bind_sock_reply_t *rmp;
Florin Coraseb2945c2017-11-22 10:39:09 -08001084 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001085 app_worker_t *app_wrk;
Florin Corasc9940fc2019-02-05 20:55:11 -08001086 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001087
Florin Coras31c99552019-03-01 13:00:58 -08001088 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001089 {
1090 rv = VNET_API_ERROR_FEATURE_DISABLED;
1091 goto done;
1092 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001093
Florin Coras6cf30ad2017-04-04 23:08:23 -07001094 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001095 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001096 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001097 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1098 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001099 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001100
Dave Barachb7b92992018-10-17 10:38:51 -04001101 clib_memset (a, 0, sizeof (*a));
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001102 ip_address_decode (&mp->ip, &a->sep.ip);
1103 a->sep.is_ip4 = ip46_address_is_ip4 (&a->sep.ip);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001104 a->sep.port = mp->port;
1105 a->sep.fib_index = mp->vrf;
1106 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001107 a->sep.transport_proto = api_session_transport_proto_decode (&mp->proto);
Florin Coras15531972018-08-12 23:50:53 -07001108 a->app_index = app->app_index;
1109 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001110
Florin Corasc1a42652019-02-08 18:27:29 -08001111 if ((rv = vnet_listen (a)))
1112 clib_warning ("listen returned: %d", rv);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001113
Florin Coras6cf30ad2017-04-04 23:08:23 -07001114done:
Florin Coras64424012019-03-02 10:47:47 -08001115 /* Actual reply sent only over mq */
1116 REPLY_MACRO (VL_API_BIND_SOCK_REPLY);
Florin Coras60116992018-08-27 09:52:18 -07001117
Florin Coras64424012019-03-02 10:47:47 -08001118 if (app)
Florin Coras60116992018-08-27 09:52:18 -07001119 {
1120 app_wrk = application_get_worker (app, mp->wrk_index);
1121 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1122 rv);
1123 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001124}
1125
Florin Coras458089b2019-08-21 16:20:44 -07001126/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001127static void
1128vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1129{
1130 vl_api_unbind_sock_reply_t *rmp;
Florin Corasc1a42652019-02-08 18:27:29 -08001131 vnet_unlisten_args_t _a, *a = &_a;
Florin Corasdfae9f92019-02-20 19:48:31 -08001132 app_worker_t *app_wrk;
1133 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001134 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001135
Florin Coras31c99552019-03-01 13:00:58 -08001136 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001137 {
1138 rv = VNET_API_ERROR_FEATURE_DISABLED;
1139 goto done;
1140 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001141
Florin Coras6cf30ad2017-04-04 23:08:23 -07001142 app = application_lookup (mp->client_index);
1143 if (app)
1144 {
Florin Coras15531972018-08-12 23:50:53 -07001145 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001146 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001147 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001148 if ((rv = vnet_unlisten (a)))
1149 clib_warning ("unlisten returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001150 }
Florin Coraseef61bb2019-08-27 15:13:35 -07001151 else
1152 {
1153 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1154 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001155
Florin Coras6cf30ad2017-04-04 23:08:23 -07001156done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001157 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
Florin Corasdfae9f92019-02-20 19:48:31 -08001158
Florin Coraseef61bb2019-08-27 15:13:35 -07001159 if (!app)
1160 return;
1161
Florin Corasdfae9f92019-02-20 19:48:31 -08001162 app_wrk = application_get_worker (app, a->wrk_map_index);
1163 if (!app_wrk)
1164 return;
1165
Florin Coras458089b2019-08-21 16:20:44 -07001166 mq_send_unlisten_reply (app_wrk, mp->handle, mp->context, rv);
Dave Barach68b0fb02017-02-28 15:15:56 -05001167}
1168
Florin Coras458089b2019-08-21 16:20:44 -07001169/* ### WILL BE DEPRECATED POST 20.01 ### */
Dave Barach68b0fb02017-02-28 15:15:56 -05001170static void
1171vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1172{
Florin Coras64424012019-03-02 10:47:47 -08001173 vl_api_connect_sock_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001174 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001175 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001176 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001177
Florin Coras31c99552019-03-01 13:00:58 -08001178 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001179 {
1180 rv = VNET_API_ERROR_FEATURE_DISABLED;
1181 goto done;
1182 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001183
Florin Coras6cf30ad2017-04-04 23:08:23 -07001184 app = application_lookup (mp->client_index);
1185 if (app)
1186 {
Florin Corase86a8ed2018-01-05 03:20:25 -08001187 svm_queue_t *client_q;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001188
Dave Barachb7b92992018-10-17 10:38:51 -04001189 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001190 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1191 mp->client_queue_address = pointer_to_uword (client_q);
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001192 ip_address_decode (&mp->ip, &a->sep.ip);
1193 a->sep.is_ip4 = ip46_address_is_ip4 (&a->sep.ip);
Florin Corascea194d2017-10-02 00:18:51 -07001194 a->sep.port = mp->port;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001195 a->sep.transport_proto =
1196 api_session_transport_proto_decode (&mp->proto);
Florin Coras5665ced2018-10-25 18:03:45 -07001197 a->sep.peer.fib_index = mp->vrf;
1198 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001199 a->sep_ext.parent_handle = mp->parent_handle;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001200 a->sep_ext.hostname =
1201 (u8 *) vl_api_from_api_to_new_c_string (&mp->hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001202 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001203 a->app_index = app->app_index;
1204 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001205 if ((rv = vnet_connect (a)))
Nathan Skrzypczakba65ca42019-05-16 16:35:40 +02001206 clib_warning ("connect returned: %U", format_vnet_api_errno, rv);
Florin Coras15531972018-08-12 23:50:53 -07001207 vec_free (a->sep_ext.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001208 }
1209 else
1210 {
1211 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1212 }
Florin Corase04c2992017-03-01 08:17:34 -08001213
Florin Coras8f89dd02018-03-05 16:53:07 -08001214 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001215 return;
1216
1217 /* Got some error, relay it */
1218
Florin Coras6cf30ad2017-04-04 23:08:23 -07001219done:
Florin Coras64424012019-03-02 10:47:47 -08001220 REPLY_MACRO (VL_API_CONNECT_SOCK_REPLY);
Florin Corasab2f6db2018-08-31 14:31:41 -07001221
Florin Coras64424012019-03-02 10:47:47 -08001222 if (app)
Florin Corasab2f6db2018-08-31 14:31:41 -07001223 {
1224 app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1225 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1226 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001227}
1228
Florin Corascea194d2017-10-02 00:18:51 -07001229static void
Florin Coras15531972018-08-12 23:50:53 -07001230vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1231{
1232 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1233 vl_api_app_worker_add_del_reply_t *rmp;
1234 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -07001235 application_t *app;
1236 u8 fd_flags = 0;
1237
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001238 if (session_main_is_enabled () == 0)
Florin Coras15531972018-08-12 23:50:53 -07001239 {
1240 rv = VNET_API_ERROR_FEATURE_DISABLED;
1241 goto done;
1242 }
1243
1244 reg = vl_api_client_index_to_registration (mp->client_index);
1245 if (!reg)
1246 return;
1247
Florin Corasc1f5a432018-11-20 11:31:26 -08001248 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001249 if (!app)
1250 {
1251 rv = VNET_API_ERROR_INVALID_VALUE;
1252 goto done;
1253 }
1254
1255 vnet_app_worker_add_del_args_t args = {
1256 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001257 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001258 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001259 .is_add = mp->is_add
1260 };
Florin Corasc1a42652019-02-08 18:27:29 -08001261 rv = vnet_app_worker_add_del (&args);
1262 if (rv)
Florin Coras15531972018-08-12 23:50:53 -07001263 {
Florin Corasc1a42652019-02-08 18:27:29 -08001264 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -07001265 goto done;
1266 }
1267
Florin Coras14598772018-09-04 19:47:52 -07001268 if (!mp->is_add)
1269 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001270
Florin Coras15531972018-08-12 23:50:53 -07001271 /* Send fifo segment fd if needed */
1272 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1273 {
1274 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1275 fds[n_fds] = args.segment->fd;
1276 n_fds += 1;
1277 }
1278 if (application_segment_manager_properties (app)->use_mq_eventfd)
1279 {
1280 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1281 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1282 n_fds += 1;
1283 }
1284
1285 /* *INDENT-OFF* */
1286done:
1287 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1288 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001289 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001290 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001291 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001292 {
Florin Coras15531972018-08-12 23:50:53 -07001293 if (vec_len (args.segment->name))
1294 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001295 vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name);
Florin Coras15531972018-08-12 23:50:53 -07001296 }
1297 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1298 rmp->n_fds = n_fds;
1299 rmp->fd_flags = fd_flags;
1300 }
1301 }));
1302 /* *INDENT-ON* */
1303
1304 if (n_fds)
1305 session_send_fds (reg, fds, n_fds);
1306}
1307
1308static void
Florin Corascea194d2017-10-02 00:18:51 -07001309vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1310{
1311 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -08001312 u32 appns_index = 0;
1313 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001314 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001315 if (session_main_is_enabled () == 0)
Florin Corascea194d2017-10-02 00:18:51 -07001316 {
1317 rv = VNET_API_ERROR_FEATURE_DISABLED;
1318 goto done;
1319 }
1320
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001321 ns_id = vl_api_from_api_to_new_vec (&mp->namespace_id);
Florin Corascea194d2017-10-02 00:18:51 -07001322
Florin Corascea194d2017-10-02 00:18:51 -07001323 vnet_app_namespace_add_del_args_t args = {
1324 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001325 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001326 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1327 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1328 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1329 .is_add = 1
1330 };
Florin Corasc1a42652019-02-08 18:27:29 -08001331 rv = vnet_app_namespace_add_del (&args);
1332 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -08001333 {
1334 appns_index = app_namespace_index_from_id (ns_id);
1335 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1336 {
1337 clib_warning ("app ns lookup failed");
1338 rv = VNET_API_ERROR_UNSPECIFIED;
1339 }
1340 }
Florin Corascea194d2017-10-02 00:18:51 -07001341 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -08001342
1343 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -07001344done:
Florin Coras6e8c6672017-11-10 09:03:54 -08001345 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1346 if (!rv)
1347 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1348 }));
1349 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -07001350}
1351
Florin Coras1c710452017-10-17 00:03:13 -07001352static void
1353vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1354{
1355 vl_api_session_rule_add_del_reply_t *rmp;
1356 session_rule_add_del_args_t args;
1357 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -07001358 int rv = 0;
1359
Dave Barachb7b92992018-10-17 10:38:51 -04001360 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001361
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001362 ip_prefix_decode (&mp->lcl, &table_args->lcl);
1363 ip_prefix_decode (&mp->rmt, &table_args->rmt);
1364
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001365 table_args->lcl_port = mp->lcl_port;
1366 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001367 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1368 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001369 mp->tag[sizeof (mp->tag) - 1] = 0;
1370 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001371 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1372 args.scope = mp->scope;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001373 args.transport_proto =
1374 api_session_transport_proto_decode (&mp->transport_proto) ==
1375 TRANSPORT_PROTO_UDP ? 1 : 0;
Florin Coras1c710452017-10-17 00:03:13 -07001376
Florin Corasc1a42652019-02-08 18:27:29 -08001377 rv = vnet_session_rule_add_del (&args);
1378 if (rv)
1379 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -08001380 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001381 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1382}
1383
Florin Coras6c36f532017-11-03 18:32:34 -07001384static void
1385send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001386 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001387 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001388{
1389 vl_api_session_rules_details_t *rmp = 0;
1390 session_mask_or_match_4_t *match =
1391 (session_mask_or_match_4_t *) & rule->match;
1392 session_mask_or_match_4_t *mask =
1393 (session_mask_or_match_4_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001394 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -07001395
1396 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001397 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001398 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1399 rmp->context = context;
1400
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001401 clib_memset (&lcl, 0, sizeof (lcl));
1402 clib_memset (&rmt, 0, sizeof (rmt));
1403 ip_set (&lcl.fp_addr, &match->lcl_ip, 1);
1404 ip_set (&rmt.fp_addr, &match->rmt_ip, 1);
1405 lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip);
1406 rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip);
1407
1408 ip_prefix_encode (&lcl, &rmp->lcl);
1409 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001410 rmp->lcl_port = match->lcl_port;
1411 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001412 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1413 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001414 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
1415 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -07001416 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001417 if (tag)
1418 {
Dave Barach178cf492018-11-13 16:34:13 -05001419 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001420 rmp->tag[vec_len (tag)] = 0;
1421 }
Florin Coras6c36f532017-11-03 18:32:34 -07001422
Florin Coras6c4dae22018-01-09 06:39:23 -08001423 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001424}
1425
1426static void
Florin Corasc97a7392017-11-05 23:07:07 -08001427send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1428 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001429 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001430{
1431 vl_api_session_rules_details_t *rmp = 0;
1432 session_mask_or_match_6_t *match =
1433 (session_mask_or_match_6_t *) & rule->match;
1434 session_mask_or_match_6_t *mask =
1435 (session_mask_or_match_6_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001436 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -07001437
1438 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001439 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001440 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1441 rmp->context = context;
1442
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001443 clib_memset (&lcl, 0, sizeof (lcl));
1444 clib_memset (&rmt, 0, sizeof (rmt));
1445 ip_set (&lcl.fp_addr, &match->lcl_ip, 0);
1446 ip_set (&rmt.fp_addr, &match->rmt_ip, 0);
1447 lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip);
1448 rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip);
1449
1450 ip_prefix_encode (&lcl, &rmp->lcl);
1451 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001452 rmp->lcl_port = match->lcl_port;
1453 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001454 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001455 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001456 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
1457 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -07001458 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001459 if (tag)
1460 {
Dave Barach178cf492018-11-13 16:34:13 -05001461 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001462 rmp->tag[vec_len (tag)] = 0;
1463 }
Florin Coras6c36f532017-11-03 18:32:34 -07001464
Florin Coras6c4dae22018-01-09 06:39:23 -08001465 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001466}
1467
1468static void
1469send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001470 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001471 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001472{
1473 mma_rule_16_t *rule16;
1474 mma_rule_40_t *rule40;
1475 mma_rules_table_16_t *srt16;
1476 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001477 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001478
Florin Corasc97a7392017-11-05 23:07:07 -08001479 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001480 {
Florin Corasc97a7392017-11-05 23:07:07 -08001481 u8 *tag = 0;
1482 /* *INDENT-OFF* */
1483 srt16 = &srt->session_rules_tables_16;
1484 pool_foreach (rule16, srt16->rules, ({
1485 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1486 tag = session_rules_table_rule_tag (srt, ri, 1);
1487 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001488 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001489 }));
1490 /* *INDENT-ON* */
1491 }
1492 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1493 {
1494 u8 *tag = 0;
1495 /* *INDENT-OFF* */
1496 srt40 = &srt->session_rules_tables_40;
1497 pool_foreach (rule40, srt40->rules, ({
1498 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1499 tag = session_rules_table_rule_tag (srt, ri, 1);
1500 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001501 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001502 }));
1503 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001504 }
1505}
1506
1507static void
1508vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1509{
Florin Coras6c4dae22018-01-09 06:39:23 -08001510 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001511 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001512 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001513
Florin Coras6c4dae22018-01-09 06:39:23 -08001514 reg = vl_api_client_index_to_registration (mp->client_index);
1515 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001516 return;
1517
1518 /* *INDENT-OFF* */
1519 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001520 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1521 {
1522 send_session_rules_table_details (&st->session_rules[tp],
1523 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001524 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001525 mp->context);
1526 }
Florin Coras6c36f532017-11-03 18:32:34 -07001527 }));
1528 /* *INDENT-ON* */
1529}
1530
Florin Coras371ca502018-02-21 12:07:41 -08001531static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001532vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001533{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001534 vl_api_app_add_cert_key_pair_reply_t *rmp;
1535 vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1536 u32 certkey_len, key_len, cert_len;
Florin Coras371ca502018-02-21 12:07:41 -08001537 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001538 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001539 {
1540 rv = VNET_API_ERROR_FEATURE_DISABLED;
1541 goto done;
1542 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001543
Florin Coras371ca502018-02-21 12:07:41 -08001544 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001545 if (cert_len > 10000)
1546 {
1547 rv = VNET_API_ERROR_INVALID_VALUE;
1548 goto done;
1549 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001550
1551 certkey_len = clib_net_to_host_u16 (mp->certkey_len);
1552 if (certkey_len < cert_len)
Florin Coras371ca502018-02-21 12:07:41 -08001553 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001554 rv = VNET_API_ERROR_INVALID_VALUE;
1555 goto done;
Florin Coras371ca502018-02-21 12:07:41 -08001556 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001557
1558 key_len = certkey_len - cert_len;
1559 if (key_len > 10000)
1560 {
1561 rv = VNET_API_ERROR_INVALID_VALUE;
1562 goto done;
1563 }
1564
1565 clib_memset (a, 0, sizeof (*a));
1566 vec_validate (a->cert, cert_len);
1567 vec_validate (a->key, key_len);
1568 clib_memcpy_fast (a->cert, mp->certkey, cert_len);
1569 clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len);
1570 rv = vnet_app_add_cert_key_pair (a);
Florin Coras371ca502018-02-21 12:07:41 -08001571 vec_free (a->cert);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001572 vec_free (a->key);
1573
Florin Coras371ca502018-02-21 12:07:41 -08001574done:
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001575 /* *INDENT-OFF* */
1576 REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
1577 if (!rv)
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001578 rmp->index = clib_host_to_net_u32 (a->index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001579 }));
1580 /* *INDENT-ON* */
Florin Coras371ca502018-02-21 12:07:41 -08001581}
1582
1583static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001584vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001585{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001586 vl_api_app_del_cert_key_pair_reply_t *rmp;
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001587 u32 ckpair_index;
Florin Coras371ca502018-02-21 12:07:41 -08001588 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001589 if (session_main_is_enabled () == 0)
1590 {
1591 rv = VNET_API_ERROR_FEATURE_DISABLED;
1592 goto done;
1593 }
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001594 ckpair_index = clib_net_to_host_u32 (mp->index);
1595 rv = vnet_app_del_cert_key_pair (ckpair_index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001596
1597done:
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001598 REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001599}
1600
1601/* ### WILL BE DEPRECATED POST 20.01 ### */
1602static void
1603vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1604 mp)
1605{
1606 vl_api_application_tls_cert_add_reply_t *rmp;
1607 app_cert_key_pair_t *ckpair;
1608 application_t *app;
1609 u32 cert_len;
1610 int rv = 0;
1611 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001612 {
1613 rv = VNET_API_ERROR_FEATURE_DISABLED;
1614 goto done;
1615 }
Florin Corase3e2f072018-03-04 07:24:30 -08001616 if (!(app = application_lookup (mp->client_index)))
1617 {
1618 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1619 goto done;
1620 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001621 cert_len = clib_net_to_host_u16 (mp->cert_len);
1622 if (cert_len > 10000)
1623 {
1624 rv = VNET_API_ERROR_INVALID_VALUE;
1625 goto done;
1626 }
1627 ckpair = app_cert_key_pair_get_default ();
1628 vec_validate (ckpair->cert, cert_len);
1629 clib_memcpy_fast (ckpair->cert, mp->cert, cert_len);
1630
1631done:
1632 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1633}
1634
1635/* ### WILL BE DEPRECATED POST 20.01 ### */
1636static void
1637vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1638 mp)
1639{
1640 vl_api_application_tls_key_add_reply_t *rmp;
1641 app_cert_key_pair_t *ckpair;
1642 application_t *app;
1643 u32 key_len;
1644 int rv = 0;
1645 if (session_main_is_enabled () == 0)
1646 {
1647 rv = VNET_API_ERROR_FEATURE_DISABLED;
1648 goto done;
1649 }
1650 if (!(app = application_lookup (mp->client_index)))
1651 {
1652 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1653 goto done;
1654 }
Florin Coras371ca502018-02-21 12:07:41 -08001655 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001656 if (key_len > 10000)
1657 {
1658 rv = VNET_API_ERROR_INVALID_VALUE;
1659 goto done;
1660 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001661 ckpair = app_cert_key_pair_get_default ();
1662 vec_validate (ckpair->key, key_len);
1663 clib_memcpy_fast (ckpair->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001664done:
1665 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1666}
1667
Florin Coras6cf30ad2017-04-04 23:08:23 -07001668static clib_error_t *
1669application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001670{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001671 application_t *app = application_lookup (client_index);
1672 vnet_app_detach_args_t _a, *a = &_a;
1673 if (app)
1674 {
Florin Coras15531972018-08-12 23:50:53 -07001675 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001676 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001677 vnet_application_detach (a);
1678 }
1679 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001680}
1681
Florin Coras6cf30ad2017-04-04 23:08:23 -07001682VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001683
1684#define vl_msg_name_crc_list
1685#include <vnet/vnet_all_api_h.h>
1686#undef vl_msg_name_crc_list
1687
1688static void
1689setup_message_id_table (api_main_t * am)
1690{
1691#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1692 foreach_vl_msg_name_crc_session;
1693#undef _
1694}
1695
1696/*
1697 * session_api_hookup
1698 * Add uri's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -05001699 * vlib has already mapped shared memory and
Dave Barach68b0fb02017-02-28 15:15:56 -05001700 * added the client registration handlers.
1701 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1702 */
1703static clib_error_t *
1704session_api_hookup (vlib_main_t * vm)
1705{
Dave Barach39d69112019-11-27 11:42:13 -05001706 api_main_t *am = vlibapi_get_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001707
1708#define _(N,n) \
1709 vl_msg_api_set_handlers(VL_API_##N, #n, \
1710 vl_api_##n##_t_handler, \
1711 vl_noop_handler, \
1712 vl_api_##n##_t_endian, \
1713 vl_api_##n##_t_print, \
1714 sizeof(vl_api_##n##_t), 1);
1715 foreach_session_api_msg;
1716#undef _
1717
1718 /*
1719 * Messages which bounce off the data-plane to
1720 * an API client. Simply tells the message handling infra not
1721 * to free the message.
1722 *
1723 * Bounced message handlers MUST NOT block the data plane
1724 */
1725 am->message_bounce[VL_API_CONNECT_URI] = 1;
1726 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1727
1728 /*
1729 * Set up the (msg_name, crc, message-id) table
1730 */
1731 setup_message_id_table (am);
1732
1733 return 0;
1734}
1735
1736VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001737
Dave Barach68b0fb02017-02-28 15:15:56 -05001738/*
1739 * fd.io coding-style-patch-verification: ON
1740 *
1741 * Local Variables:
1742 * eval: (c-set-style "gnu")
1743 * End:
1744 */