blob: ff11bcb690a9db4f7f2af73527c84ad020025bc1 [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>
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010024#include <vnet/ip/ip_types_api.h>
25
Filip Tehlar0046e972021-06-26 22:12:08 +000026#include <vnet/format_fns.h>
27#include <vnet/session/session.api_enum.h>
28#include <vnet/session/session.api_types.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050029
Filip Tehlar0046e972021-06-26 22:12:08 +000030#define REPLY_MSG_ID_BASE session_main.msg_id_base
Dave Barach68b0fb02017-02-28 15:15:56 -050031#include <vlibapi/api_helper_macros.h>
32
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010033static transport_proto_t
34api_session_transport_proto_decode (const vl_api_transport_proto_t * api_tp)
35{
36 switch (*api_tp)
37 {
38 case TRANSPORT_PROTO_API_TCP:
39 return TRANSPORT_PROTO_TCP;
40 case TRANSPORT_PROTO_API_UDP:
41 return TRANSPORT_PROTO_UDP;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010042 case TRANSPORT_PROTO_API_TLS:
43 return TRANSPORT_PROTO_TLS;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010044 case TRANSPORT_PROTO_API_QUIC:
45 return TRANSPORT_PROTO_QUIC;
46 default:
47 return TRANSPORT_PROTO_NONE;
48 }
49}
50
51static vl_api_transport_proto_t
52api_session_transport_proto_encode (const transport_proto_t tp)
53{
54 switch (tp)
55 {
56 case TRANSPORT_PROTO_TCP:
57 return TRANSPORT_PROTO_API_TCP;
58 case TRANSPORT_PROTO_UDP:
59 return TRANSPORT_PROTO_API_UDP;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010060 case TRANSPORT_PROTO_TLS:
61 return TRANSPORT_PROTO_API_TLS;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010062 case TRANSPORT_PROTO_QUIC:
63 return TRANSPORT_PROTO_API_QUIC;
64 default:
65 return TRANSPORT_PROTO_API_NONE;
66 }
67}
68
Dave Barach68b0fb02017-02-28 15:15:56 -050069static int
Florin Coras99368312018-08-02 10:45:44 -070070session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
Florin Corasb384b542018-01-15 01:08:33 -080071{
72 clib_error_t *error;
73 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
Florin Coras00e01d32019-10-21 16:07:46 -070074 return SESSION_E_BAPI_NO_FD;
Florin Coras99368312018-08-02 10:45:44 -070075 error = vl_api_send_fd_msg (reg, fds, n_fds);
Florin Corasb384b542018-01-15 01:08:33 -080076 if (error)
77 {
78 clib_error_report (error);
Florin Coras00e01d32019-10-21 16:07:46 -070079 return SESSION_E_BAPI_SEND_FD;
Florin Corasb384b542018-01-15 01:08:33 -080080 }
81 return 0;
82}
83
Florin Coras99368312018-08-02 10:45:44 -070084static int
Florin Coras288eaab2019-02-03 15:26:14 -080085mq_send_session_accepted_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -070086{
Florin Coras15531972018-08-12 23:50:53 -070087 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras09bf91a2021-02-23 08:44:13 -080088 session_accepted_msg_t m = { 0 };
Florin Corasb4624182020-12-11 13:58:12 -080089 fifo_segment_t *eq_seg;
Florin Coras288eaab2019-02-03 15:26:14 -080090 session_t *listener;
Florin Coras15531972018-08-12 23:50:53 -070091 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -070092
Florin Coras15531972018-08-12 23:50:53 -070093 app = application_get (app_wrk->app_index);
Florin Coras52207f12018-07-12 14:48:06 -070094
Florin Coras09bf91a2021-02-23 08:44:13 -080095 m.context = app->app_index;
96 m.server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
97 m.server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
98 m.segment_handle = session_segment_handle (s);
99 m.flags = s->flags;
Florin Coras52207f12018-07-12 14:48:06 -0700100
Florin Coras41d5f542021-01-15 13:49:33 -0800101 eq_seg = application_get_rx_mqs_segment (app);
Florin Corasb4624182020-12-11 13:58:12 -0800102
Florin Coras52207f12018-07-12 14:48:06 -0700103 if (session_has_transport (s))
104 {
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200105 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras09bf91a2021-02-23 08:44:13 -0800106 m.listener_handle = app_listen_session_handle (listener);
Florin Coras52207f12018-07-12 14:48:06 -0700107 if (application_is_proxy (app))
108 {
109 listener =
Florin Coras15531972018-08-12 23:50:53 -0700110 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
111 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700112 if (listener)
Florin Coras09bf91a2021-02-23 08:44:13 -0800113 m.listener_handle = listen_session_get_handle (listener);
Florin Coras52207f12018-07-12 14:48:06 -0700114 }
Florin Coras09bf91a2021-02-23 08:44:13 -0800115 m.vpp_event_queue_address =
Florin Corasb4624182020-12-11 13:58:12 -0800116 fifo_segment_msg_q_offset (eq_seg, s->thread_index);
Florin Coras09bf91a2021-02-23 08:44:13 -0800117 m.mq_index = s->thread_index;
118 m.handle = session_handle (s);
Aloys Augustincdb71702019-04-08 17:54:39 +0200119
Florin Coras09bf91a2021-02-23 08:44:13 -0800120 session_get_endpoint (s, &m.rmt, 0 /* is_lcl */);
Florin Coras67c90a32021-03-09 18:36:06 -0800121 session_get_endpoint (s, &m.lcl, 1 /* is_lcl */);
Florin Coras52207f12018-07-12 14:48:06 -0700122 }
123 else
124 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800125 ct_connection_t *ct;
Florin Coras99368312018-08-02 10:45:44 -0700126
Florin Coras2b81e3c2019-02-27 07:55:46 -0800127 ct = (ct_connection_t *) session_get_transport (s);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200128 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras09bf91a2021-02-23 08:44:13 -0800129 m.listener_handle = app_listen_session_handle (listener);
130 m.rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
131 m.rmt.port = ct->c_rmt_port;
Florin Coras67c90a32021-03-09 18:36:06 -0800132 m.lcl.port = ct->c_lcl_port;
Florin Coras09bf91a2021-02-23 08:44:13 -0800133 m.handle = session_handle (s);
134 m.vpp_event_queue_address =
Florin Corasb4624182020-12-11 13:58:12 -0800135 fifo_segment_msg_q_offset (eq_seg, s->thread_index);
Florin Coras09bf91a2021-02-23 08:44:13 -0800136 m.mq_index = s->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700137 }
Florin Coras09bf91a2021-02-23 08:44:13 -0800138
qinyangaf9b7152023-06-27 01:11:53 -0700139 if (application_original_dst_is_enabled (app))
140 {
141 session_get_original_dst (&m.lcl, &m.rmt,
142 session_get_transport_proto (s),
143 &m.original_dst_ip4, &m.original_dst_port);
144 }
145
Florin Coras20c24232021-11-22 21:19:01 -0800146 app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_ACCEPTED, &m, sizeof (m));
Florin Coras52207f12018-07-12 14:48:06 -0700147
148 return 0;
149}
150
Florin Coras72b04282019-01-14 17:23:11 -0800151static inline void
152mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
153 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700154{
Florin Coras20c24232021-11-22 21:19:01 -0800155 session_disconnected_msg_t m = { 0 };
Florin Coras52207f12018-07-12 14:48:06 -0700156
Florin Coras20c24232021-11-22 21:19:01 -0800157 m.handle = sh;
158 m.context = app_wrk->api_client_index;
159
160 app_wrk_send_ctrl_evt (app_wrk, evt_type, &m, sizeof (m));
Florin Coras52207f12018-07-12 14:48:06 -0700161}
162
Florin Coras72b04282019-01-14 17:23:11 -0800163static inline void
164mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
165 svm_fifo_t * f, session_evt_type_t evt_type)
166{
167 app_worker_t *app_wrk;
168 application_t *app;
169 int i;
170
171 app = application_get (app_index);
172 if (!app)
173 return;
174
Florin Corasc547e912020-12-08 17:50:45 -0800175 for (i = 0; i < f->shr->n_subscribers; i++)
Florin Coras72b04282019-01-14 17:23:11 -0800176 {
Florin Corasc547e912020-12-08 17:50:45 -0800177 if (!(app_wrk = application_get_worker (app, f->shr->subscribers[i])))
Florin Coras72b04282019-01-14 17:23:11 -0800178 continue;
179 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
180 }
181}
182
183static void
Florin Coras288eaab2019-02-03 15:26:14 -0800184mq_send_session_disconnected_cb (session_t * s)
Florin Coras72b04282019-01-14 17:23:11 -0800185{
186 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
187 session_handle_t sh = session_handle (s);
188
189 mq_send_session_close_evt (app_wrk, session_handle (s),
190 SESSION_CTRL_EVT_DISCONNECTED);
191
Florin Coras288eaab2019-02-03 15:26:14 -0800192 if (svm_fifo_n_subscribers (s->rx_fifo))
193 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800194 SESSION_CTRL_EVT_DISCONNECTED);
195}
196
Florin Coras52207f12018-07-12 14:48:06 -0700197static void
Florin Coras288eaab2019-02-03 15:26:14 -0800198mq_send_session_reset_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700199{
Florin Coras72b04282019-01-14 17:23:11 -0800200 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
201 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700202
Florin Coras72b04282019-01-14 17:23:11 -0800203 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
204
Florin Coras288eaab2019-02-03 15:26:14 -0800205 if (svm_fifo_n_subscribers (s->rx_fifo))
206 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800207 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700208}
209
Florin Coras458089b2019-08-21 16:20:44 -0700210int
Florin Coras15531972018-08-12 23:50:53 -0700211mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras00e01d32019-10-21 16:07:46 -0700212 session_t * s, session_error_t err)
Florin Coras52207f12018-07-12 14:48:06 -0700213{
Florin Coras09bf91a2021-02-23 08:44:13 -0800214 session_connected_msg_t m = { 0 };
Florin Coras52207f12018-07-12 14:48:06 -0700215 transport_connection_t *tc;
Florin Corasb4624182020-12-11 13:58:12 -0800216 fifo_segment_t *eq_seg;
Florin Coras15531972018-08-12 23:50:53 -0700217 app_worker_t *app_wrk;
Florin Coras41d5f542021-01-15 13:49:33 -0800218 application_t *app;
219
220 app_wrk = app_worker_get (app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700221
Florin Coras09bf91a2021-02-23 08:44:13 -0800222 m.context = api_context;
223 m.retval = err;
Florin Coras52207f12018-07-12 14:48:06 -0700224
Florin Coras00e01d32019-10-21 16:07:46 -0700225 if (err)
Florin Coras09bf91a2021-02-23 08:44:13 -0800226 goto snd_msg;
Florin Coras52207f12018-07-12 14:48:06 -0700227
Florin Coras41d5f542021-01-15 13:49:33 -0800228 app = application_get (app_wrk->app_index);
229 eq_seg = application_get_rx_mqs_segment (app);
Florin Corasb4624182020-12-11 13:58:12 -0800230
Florin Coras52207f12018-07-12 14:48:06 -0700231 if (session_has_transport (s))
232 {
233 tc = session_get_transport (s);
234 if (!tc)
235 {
Florin Coras00e01d32019-10-21 16:07:46 -0700236 clib_warning ("failed to retrieve transport!");
Florin Coras09bf91a2021-02-23 08:44:13 -0800237 m.retval = SESSION_E_REFUSED;
238 goto snd_msg;
Florin Coras52207f12018-07-12 14:48:06 -0700239 }
240
Florin Coras09bf91a2021-02-23 08:44:13 -0800241 m.handle = session_handle (s);
242 m.vpp_event_queue_address =
Florin Corasb4624182020-12-11 13:58:12 -0800243 fifo_segment_msg_q_offset (eq_seg, s->thread_index);
Aloys Augustincdb71702019-04-08 17:54:39 +0200244
Florin Coras09bf91a2021-02-23 08:44:13 -0800245 session_get_endpoint (s, &m.lcl, 1 /* is_lcl */);
Aloys Augustincdb71702019-04-08 17:54:39 +0200246
Florin Coras09bf91a2021-02-23 08:44:13 -0800247 m.server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
248 m.server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
249 m.segment_handle = session_segment_handle (s);
Florin Corasf6e284b2021-07-21 18:17:20 -0700250 m.mq_index = s->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700251 }
252 else
253 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800254 ct_connection_t *cct;
255 session_t *ss;
Florin Coras99368312018-08-02 10:45:44 -0700256
Florin Coras2b81e3c2019-02-27 07:55:46 -0800257 cct = (ct_connection_t *) session_get_transport (s);
Florin Coras09bf91a2021-02-23 08:44:13 -0800258 m.handle = session_handle (s);
259 m.lcl.port = cct->c_lcl_port;
260 m.lcl.is_ip4 = cct->c_is_ip4;
261 m.vpp_event_queue_address =
Florin Corasb4624182020-12-11 13:58:12 -0800262 fifo_segment_msg_q_offset (eq_seg, s->thread_index);
Florin Coras09bf91a2021-02-23 08:44:13 -0800263 m.server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
264 m.server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
265 m.segment_handle = session_segment_handle (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800266 ss = ct_session_get_peer (s);
Florin Coras09bf91a2021-02-23 08:44:13 -0800267 m.ct_rx_fifo = fifo_segment_fifo_offset (ss->tx_fifo);
268 m.ct_tx_fifo = fifo_segment_fifo_offset (ss->rx_fifo);
269 m.ct_segment_handle = session_segment_handle (ss);
Florin Corasf6e284b2021-07-21 18:17:20 -0700270 m.mq_index = s->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700271 }
272
Florin Coras893bc972021-04-23 08:58:57 -0700273 /* Setup client session index in advance, in case data arrives
274 * before the app processes message and updates it */
275 s->rx_fifo->shr->client_session_index = api_context;
276 s->tx_fifo->shr->client_session_index = api_context;
277
Florin Coras09bf91a2021-02-23 08:44:13 -0800278snd_msg:
279
Florin Coras20c24232021-11-22 21:19:01 -0800280 app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_CONNECTED, &m, sizeof (m));
Florin Coras09bf91a2021-02-23 08:44:13 -0800281
Florin Coras52207f12018-07-12 14:48:06 -0700282 return 0;
283}
284
Florin Coras458089b2019-08-21 16:20:44 -0700285int
Florin Coras60116992018-08-27 09:52:18 -0700286mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
287 session_handle_t handle, int rv)
288{
Florin Coras09bf91a2021-02-23 08:44:13 -0800289 session_bound_msg_t m = { 0 };
Yu Ping0b819152019-05-07 02:24:30 +0800290 transport_endpoint_t tep;
Florin Corasb4624182020-12-11 13:58:12 -0800291 fifo_segment_t *eq_seg;
Florin Coras60116992018-08-27 09:52:18 -0700292 app_worker_t *app_wrk;
Florin Coras41d5f542021-01-15 13:49:33 -0800293 application_t *app;
Florin Corasc9940fc2019-02-05 20:55:11 -0800294 app_listener_t *al;
295 session_t *ls = 0;
Florin Coras09bf91a2021-02-23 08:44:13 -0800296
Florin Coras41d5f542021-01-15 13:49:33 -0800297 app_wrk = app_worker_get (app_wrk_index);
298
Florin Coras09bf91a2021-02-23 08:44:13 -0800299 m.context = api_context;
300 m.retval = rv;
301
302 if (rv)
303 goto snd_msg;
304
305 m.handle = handle;
306 al = app_listener_get_w_handle (handle);
307 if (al->session_index != SESSION_INVALID_INDEX)
308 ls = app_listener_get_session (al);
309 else
310 ls = app_listener_get_local_session (al);
311
312 session_get_endpoint (ls, &tep, 1 /* is_lcl */);
313 m.lcl_port = tep.port;
314 m.lcl_is_ip4 = tep.is_ip4;
315 clib_memcpy_fast (m.lcl_ip, &tep.ip, sizeof (tep.ip));
Florin Coras41d5f542021-01-15 13:49:33 -0800316 app = application_get (app_wrk->app_index);
317 eq_seg = application_get_rx_mqs_segment (app);
Florin Coras09bf91a2021-02-23 08:44:13 -0800318 m.vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, ls->thread_index);
Florin Corasf6e284b2021-07-21 18:17:20 -0700319 m.mq_index = ls->thread_index;
Florin Coras09bf91a2021-02-23 08:44:13 -0800320
Florin Coras8694fbc2021-03-10 00:21:02 -0800321 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL &&
322 ls->rx_fifo)
Florin Coras09bf91a2021-02-23 08:44:13 -0800323 {
Florin Corasaeb7c1c2023-03-10 10:22:21 -0800324 m.mq_index = transport_cl_thread ();
325 m.vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, m.mq_index);
Florin Coras09bf91a2021-02-23 08:44:13 -0800326 m.rx_fifo = fifo_segment_fifo_offset (ls->rx_fifo);
327 m.tx_fifo = fifo_segment_fifo_offset (ls->tx_fifo);
328 m.segment_handle = session_segment_handle (ls);
329 }
330
331snd_msg:
332
Florin Coras20c24232021-11-22 21:19:01 -0800333 app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_BOUND, &m, sizeof (m));
Florin Coras60116992018-08-27 09:52:18 -0700334
Florin Coras60116992018-08-27 09:52:18 -0700335 return 0;
336}
337
Florin Coras458089b2019-08-21 16:20:44 -0700338void
339mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
340 u32 context, int rv)
341{
Florin Coras20c24232021-11-22 21:19:01 -0800342 session_unlisten_reply_msg_t m = { 0 };
Florin Coras458089b2019-08-21 16:20:44 -0700343
Florin Coras20c24232021-11-22 21:19:01 -0800344 m.context = context;
345 m.handle = sh;
346 m.retval = rv;
347 app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_UNLISTEN_REPLY, &m,
348 sizeof (m));
Florin Coras458089b2019-08-21 16:20:44 -0700349}
350
Florin Coras49568af2019-07-31 16:46:24 -0700351static void
352mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh)
353{
Florin Coras09bf91a2021-02-23 08:44:13 -0800354 session_migrated_msg_t m = { 0 };
Florin Corasb4624182020-12-11 13:58:12 -0800355 fifo_segment_t *eq_seg;
Florin Coras68b7e582020-01-21 18:33:23 -0800356 app_worker_t *app_wrk;
Florin Coras41d5f542021-01-15 13:49:33 -0800357 application_t *app;
Florin Corasb4624182020-12-11 13:58:12 -0800358 u32 thread_index;
359
360 thread_index = session_thread_from_handle (new_sh);
Florin Coras41d5f542021-01-15 13:49:33 -0800361 app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras41d5f542021-01-15 13:49:33 -0800362 app = application_get (app_wrk->app_index);
363 eq_seg = application_get_rx_mqs_segment (app);
Florin Coras68b7e582020-01-21 18:33:23 -0800364
Florin Coras09bf91a2021-02-23 08:44:13 -0800365 m.handle = session_handle (s);
366 m.new_handle = new_sh;
367 m.vpp_thread_index = thread_index;
368 m.vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, thread_index);
369 m.segment_handle = SESSION_INVALID_HANDLE;
370
Florin Coras20c24232021-11-22 21:19:01 -0800371 app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_MIGRATED, &m, sizeof (m));
Florin Coras49568af2019-07-31 16:46:24 -0700372}
373
Florin Corasc4c4cf52019-08-24 18:17:34 -0700374static int
375mq_send_add_segment_cb (u32 app_wrk_index, u64 segment_handle)
376{
Florin Coras20c24232021-11-22 21:19:01 -0800377 session_app_add_segment_msg_t m = { 0 };
Florin Corasc4c4cf52019-08-24 18:17:34 -0700378 vl_api_registration_t *reg;
379 app_worker_t *app_wrk;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700380 fifo_segment_t *fs;
381 ssvm_private_t *sp;
382 u8 fd_flags = 0;
383
384 app_wrk = app_worker_get (app_wrk_index);
385
386 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
387 if (!reg)
388 {
389 clib_warning ("no api registration for client: %u",
390 app_wrk->api_client_index);
391 return -1;
392 }
393
394 fs = segment_manager_get_segment_w_handle (segment_handle);
395 sp = &fs->ssvm;
396 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
397 {
398 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
399 {
400 clib_warning ("can't send memfd fd");
401 return -1;
402 }
403
404 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700405 }
406
Florin Coras20c24232021-11-22 21:19:01 -0800407 m.segment_size = sp->ssvm_size;
408 m.fd_flags = fd_flags;
409 m.segment_handle = segment_handle;
410 strncpy ((char *) m.segment_name, (char *) sp->name,
411 sizeof (m.segment_name) - 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700412
Florin Coras20c24232021-11-22 21:19:01 -0800413 app_wrk_send_ctrl_evt_fd (app_wrk, SESSION_CTRL_EVT_APP_ADD_SEGMENT, &m,
414 sizeof (m), sp->fd);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700415
416 return 0;
417}
418
419static int
420mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle)
421{
Florin Coras20c24232021-11-22 21:19:01 -0800422 session_app_del_segment_msg_t m = { 0 };
Florin Corasc4c4cf52019-08-24 18:17:34 -0700423 vl_api_registration_t *reg;
424 app_worker_t *app_wrk;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700425
426 app_wrk = app_worker_get (app_wrk_index);
427 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
428 if (!reg)
429 {
430 clib_warning ("no registration: %u", app_wrk->api_client_index);
431 return -1;
432 }
433
Florin Coras20c24232021-11-22 21:19:01 -0800434 m.segment_handle = segment_handle;
Florin Corasc4c4cf52019-08-24 18:17:34 -0700435
Florin Coras20c24232021-11-22 21:19:01 -0800436 app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_APP_DEL_SEGMENT, &m,
437 sizeof (m));
Florin Corasc4c4cf52019-08-24 18:17:34 -0700438
439 return 0;
440}
441
Florin Coras9ace36d2019-10-28 13:14:17 -0700442static void
443mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf)
444{
Florin Coras20c24232021-11-22 21:19:01 -0800445 session_cleanup_msg_t m = { 0 };
Florin Coras9ace36d2019-10-28 13:14:17 -0700446 app_worker_t *app_wrk;
447
Florin Coras36d49392020-04-24 23:00:11 +0000448 /* Propagate transport cleanup notifications only if app didn't close */
449 if (ntf == SESSION_CLEANUP_TRANSPORT
450 && s->session_state != SESSION_STATE_TRANSPORT_DELETED)
Florin Coras9ace36d2019-10-28 13:14:17 -0700451 return;
452
453 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
454 if (!app_wrk)
455 return;
456
Florin Coras20c24232021-11-22 21:19:01 -0800457 m.handle = session_handle (s);
458 m.type = ntf;
Florin Coras9ace36d2019-10-28 13:14:17 -0700459
Florin Coras20c24232021-11-22 21:19:01 -0800460 app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_CLEANUP, &m, sizeof (m));
Florin Coras9ace36d2019-10-28 13:14:17 -0700461}
462
Florin Corasc4c4cf52019-08-24 18:17:34 -0700463static session_cb_vft_t session_mq_cb_vft = {
464 .session_accept_callback = mq_send_session_accepted_cb,
465 .session_disconnect_callback = mq_send_session_disconnected_cb,
466 .session_connected_callback = mq_send_session_connected_cb,
467 .session_reset_callback = mq_send_session_reset_cb,
468 .session_migrate_callback = mq_send_session_migrate_cb,
Florin Coras9ace36d2019-10-28 13:14:17 -0700469 .session_cleanup_callback = mq_send_session_cleanup_cb,
Florin Corasc4c4cf52019-08-24 18:17:34 -0700470 .add_segment_callback = mq_send_add_segment_cb,
471 .del_segment_callback = mq_send_del_segment_cb,
472};
473
Dave Barach68b0fb02017-02-28 15:15:56 -0500474static void
Florin Corase04c2992017-03-01 08:17:34 -0800475vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
476{
477 vl_api_session_enable_disable_reply_t *rmp;
478 vlib_main_t *vm = vlib_get_main ();
479 int rv = 0;
480
481 vnet_session_enable_disable (vm, mp->is_enable);
482 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
483}
484
Dave Barach68b0fb02017-02-28 15:15:56 -0500485static void
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +0200486vl_api_session_sapi_enable_disable_t_handler (
487 vl_api_session_sapi_enable_disable_t *mp)
488{
489 vl_api_session_sapi_enable_disable_reply_t *rmp;
490 int rv = 0;
491
492 rv = appns_sapi_enable_disable (mp->is_enable);
493 REPLY_MACRO (VL_API_SESSION_SAPI_ENABLE_DISABLE_REPLY);
494}
495
496static void
Florin Coras458089b2019-08-21 16:20:44 -0700497vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
498{
Florin Coras41d5f542021-01-15 13:49:33 -0800499 int rv = 0, *fds = 0, n_fds = 0, n_workers, i;
500 fifo_segment_t *segp, *rx_mqs_seg = 0;
Florin Coras458089b2019-08-21 16:20:44 -0700501 vnet_app_attach_args_t _a, *a = &_a;
Florin Coras41d5f542021-01-15 13:49:33 -0800502 vl_api_app_attach_reply_t *rmp;
Florin Coras458089b2019-08-21 16:20:44 -0700503 u8 fd_flags = 0, ctrl_thread;
504 vl_api_registration_t *reg;
Florin Coras41d5f542021-01-15 13:49:33 -0800505 svm_msg_q_t *rx_mq;
506 application_t *app;
Florin Coras458089b2019-08-21 16:20:44 -0700507
508 reg = vl_api_client_index_to_registration (mp->client_index);
509 if (!reg)
510 return;
511
Florin Coras41d5f542021-01-15 13:49:33 -0800512 n_workers = vlib_num_workers ();
Florin Coras61ae0562020-09-02 19:10:28 -0700513 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras458089b2019-08-21 16:20:44 -0700514 {
515 rv = VNET_API_ERROR_FEATURE_DISABLED;
516 goto done;
517 }
Florin Coras6c10ab22020-11-08 16:50:39 -0800518 /* Only support binary api with socket transport */
519 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
520 {
521 rv = VNET_API_ERROR_APP_UNSUPPORTED_CFG;
522 goto done;
523 }
Florin Coras458089b2019-08-21 16:20:44 -0700524
525 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
526 sizeof (mp->options),
527 "Out of options, fix api message definition");
528
529 clib_memset (a, 0, sizeof (*a));
530 a->api_client_index = mp->client_index;
531 a->options = mp->options;
532 a->session_cb_vft = &session_mq_cb_vft;
Dave Barach77841402020-04-29 17:04:10 -0400533 a->namespace_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
Florin Coras458089b2019-08-21 16:20:44 -0700534
535 if ((rv = vnet_application_attach (a)))
536 {
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200537 clib_warning ("attach returned: %U", format_session_error, rv);
538 rv = VNET_API_ERROR_UNSPECIFIED;
Florin Coras458089b2019-08-21 16:20:44 -0700539 vec_free (a->namespace_id);
540 goto done;
541 }
542 vec_free (a->namespace_id);
543
Florin Coras41d5f542021-01-15 13:49:33 -0800544 vec_validate (fds, 3 /* segs + tx evtfd */ + n_workers);
545
546 /* Send rx mqs segment */
547 app = application_get (a->app_index);
548 rx_mqs_seg = application_get_rx_mqs_segment (app);
549
550 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
551 fds[n_fds] = rx_mqs_seg->ssvm.fd;
552 n_fds += 1;
553
Florin Coras458089b2019-08-21 16:20:44 -0700554 /* Send fifo segment fd if needed */
555 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
556 {
557 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
558 fds[n_fds] = a->segment->fd;
559 n_fds += 1;
560 }
561 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
562 {
563 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
Florin Coras86f12322021-01-22 15:05:14 -0800564 fds[n_fds] = svm_msg_q_get_eventfd (a->app_evt_q);
Florin Coras458089b2019-08-21 16:20:44 -0700565 n_fds += 1;
566 }
567
Florin Coras41d5f542021-01-15 13:49:33 -0800568 if (application_use_private_rx_mqs ())
569 {
570 fd_flags |= SESSION_FD_F_VPP_MQ_EVENTFD;
571 for (i = 0; i < n_workers + 1; i++)
572 {
573 rx_mq = application_rx_mq_get (app, i);
574 fds[n_fds] = svm_msg_q_get_eventfd (rx_mq);
575 n_fds += 1;
576 }
577 }
578
Florin Coras458089b2019-08-21 16:20:44 -0700579done:
Florin Coras458089b2019-08-21 16:20:44 -0700580 /* *INDENT-OFF* */
Klement Sekera0eb83f42021-12-02 16:36:34 +0000581 REPLY_MACRO3 (
582 VL_API_APP_ATTACH_REPLY,
583 ((!rv) ? vec_len (((fifo_segment_t *) a->segment)->ssvm.name) : 0), ({
584 if (!rv)
585 {
586 ctrl_thread = n_workers ? 1 : 0;
587 segp = (fifo_segment_t *) a->segment;
588 rmp->app_index = clib_host_to_net_u32 (a->app_index);
589 rmp->app_mq = fifo_segment_msg_q_offset (segp, 0);
590 rmp->vpp_ctrl_mq =
591 fifo_segment_msg_q_offset (rx_mqs_seg, ctrl_thread);
592 rmp->vpp_ctrl_mq_thread = ctrl_thread;
593 rmp->n_fds = n_fds;
594 rmp->fd_flags = fd_flags;
595 if (vec_len (segp->ssvm.name))
596 {
597 vl_api_vec_to_api_string (segp->ssvm.name, &rmp->segment_name);
598 }
599 rmp->segment_size = segp->ssvm.ssvm_size;
600 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
601 }
602 }));
Florin Coras458089b2019-08-21 16:20:44 -0700603 /* *INDENT-ON* */
604
605 if (n_fds)
606 session_send_fds (reg, fds, n_fds);
Florin Coras41d5f542021-01-15 13:49:33 -0800607 vec_free (fds);
Florin Coras458089b2019-08-21 16:20:44 -0700608}
609
Florin Corascea194d2017-10-02 00:18:51 -0700610static void
Florin Coras15531972018-08-12 23:50:53 -0700611vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
612{
613 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
614 vl_api_app_worker_add_del_reply_t *rmp;
615 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -0700616 application_t *app;
617 u8 fd_flags = 0;
618
Florin Coras61ae0562020-09-02 19:10:28 -0700619 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras15531972018-08-12 23:50:53 -0700620 {
621 rv = VNET_API_ERROR_FEATURE_DISABLED;
622 goto done;
623 }
624
625 reg = vl_api_client_index_to_registration (mp->client_index);
626 if (!reg)
627 return;
628
Florin Corasc1f5a432018-11-20 11:31:26 -0800629 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -0700630 if (!app)
631 {
632 rv = VNET_API_ERROR_INVALID_VALUE;
633 goto done;
634 }
635
636 vnet_app_worker_add_del_args_t args = {
637 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -0800638 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -0800639 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -0700640 .is_add = mp->is_add
641 };
Florin Corasc1a42652019-02-08 18:27:29 -0800642 rv = vnet_app_worker_add_del (&args);
643 if (rv)
Florin Coras15531972018-08-12 23:50:53 -0700644 {
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200645 clib_warning ("app worker add/del returned: %U", format_session_error,
646 rv);
647 rv = VNET_API_ERROR_UNSPECIFIED;
Florin Coras15531972018-08-12 23:50:53 -0700648 goto done;
649 }
650
Florin Coras14598772018-09-04 19:47:52 -0700651 if (!mp->is_add)
652 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -0700653
Florin Coras15531972018-08-12 23:50:53 -0700654 /* Send fifo segment fd if needed */
655 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
656 {
657 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
658 fds[n_fds] = args.segment->fd;
659 n_fds += 1;
660 }
661 if (application_segment_manager_properties (app)->use_mq_eventfd)
662 {
663 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
Florin Coras86f12322021-01-22 15:05:14 -0800664 fds[n_fds] = svm_msg_q_get_eventfd (args.evt_q);
Florin Coras15531972018-08-12 23:50:53 -0700665 n_fds += 1;
666 }
667
668 /* *INDENT-OFF* */
669done:
Klement Sekera0eb83f42021-12-02 16:36:34 +0000670 REPLY_MACRO3 (
671 VL_API_APP_WORKER_ADD_DEL_REPLY,
672 ((!rv && mp->is_add) ? vec_len (args.segment->name) : 0), ({
673 rmp->is_add = mp->is_add;
674 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
675 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
676 if (!rv && mp->is_add)
677 {
678 rmp->app_event_queue_address =
679 fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0);
680 rmp->n_fds = n_fds;
681 rmp->fd_flags = fd_flags;
682 if (vec_len (args.segment->name))
683 {
684 vl_api_vec_to_api_string (args.segment->name,
685 &rmp->segment_name);
686 }
687 }
688 }));
Florin Coras15531972018-08-12 23:50:53 -0700689 /* *INDENT-ON* */
690
691 if (n_fds)
692 session_send_fds (reg, fds, n_fds);
693}
694
695static void
Florin Coras888d9f02020-04-02 23:00:13 +0000696vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
697{
698 vl_api_application_detach_reply_t *rmp;
699 int rv = VNET_API_ERROR_INVALID_VALUE_2;
700 vnet_app_detach_args_t _a, *a = &_a;
701 application_t *app;
702
Florin Coras61ae0562020-09-02 19:10:28 -0700703 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras888d9f02020-04-02 23:00:13 +0000704 {
705 rv = VNET_API_ERROR_FEATURE_DISABLED;
706 goto done;
707 }
708
709 app = application_lookup (mp->client_index);
710 if (app)
711 {
712 a->app_index = app->app_index;
713 a->api_client_index = mp->client_index;
714 rv = vnet_application_detach (a);
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200715 if (rv)
716 {
717 clib_warning ("vnet_application_detach: %U", format_session_error,
718 rv);
719 rv = VNET_API_ERROR_UNSPECIFIED;
720 }
Florin Coras888d9f02020-04-02 23:00:13 +0000721 }
722
723done:
724 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
725}
726
727static void
Florin Corascea194d2017-10-02 00:18:51 -0700728vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
729{
730 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -0800731 u32 appns_index = 0;
732 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700733 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200734 if (session_main_is_enabled () == 0)
Florin Corascea194d2017-10-02 00:18:51 -0700735 {
736 rv = VNET_API_ERROR_FEATURE_DISABLED;
737 goto done;
738 }
739
Dave Barach77841402020-04-29 17:04:10 -0400740 ns_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
Florin Corascea194d2017-10-02 00:18:51 -0700741
Florin Corascea194d2017-10-02 00:18:51 -0700742 vnet_app_namespace_add_del_args_t args = {
743 .ns_id = ns_id,
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200744 .sock_name = 0,
Florin Coras9a9adb22017-10-26 08:16:59 -0700745 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -0700746 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
747 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
748 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
749 .is_add = 1
750 };
Florin Corasc1a42652019-02-08 18:27:29 -0800751 rv = vnet_app_namespace_add_del (&args);
752 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -0800753 {
754 appns_index = app_namespace_index_from_id (ns_id);
755 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
756 {
757 clib_warning ("app ns lookup failed");
758 rv = VNET_API_ERROR_UNSPECIFIED;
759 }
760 }
Florin Corascea194d2017-10-02 00:18:51 -0700761 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -0800762
763 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -0700764done:
Florin Coras6e8c6672017-11-10 09:03:54 -0800765 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
766 if (!rv)
767 rmp->appns_index = clib_host_to_net_u32 (appns_index);
768 }));
769 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -0700770}
771
Florin Coras1c710452017-10-17 00:03:13 -0700772static void
Florin Coras7cb471a2021-07-23 08:39:26 -0700773vl_api_app_namespace_add_del_v2_t_handler (
774 vl_api_app_namespace_add_del_v2_t *mp)
775{
776 vl_api_app_namespace_add_del_v2_reply_t *rmp;
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +0200777 u8 *ns_id = 0;
Florin Coras7cb471a2021-07-23 08:39:26 -0700778 u32 appns_index = 0;
779 int rv = 0;
780
781 if (session_main_is_enabled () == 0)
782 {
783 rv = VNET_API_ERROR_FEATURE_DISABLED;
784 goto done;
785 }
786
787 mp->namespace_id[sizeof (mp->namespace_id) - 1] = 0;
Florin Coras7cb471a2021-07-23 08:39:26 -0700788 ns_id = format (0, "%s", &mp->namespace_id);
Florin Coras7cb471a2021-07-23 08:39:26 -0700789
790 vnet_app_namespace_add_del_args_t args = {
791 .ns_id = ns_id,
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200792 .sock_name = 0,
Florin Coras7cb471a2021-07-23 08:39:26 -0700793 .secret = clib_net_to_host_u64 (mp->secret),
794 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
795 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
796 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
797 .is_add = 1
798 };
799 rv = vnet_app_namespace_add_del (&args);
800 if (!rv)
801 {
802 appns_index = app_namespace_index_from_id (ns_id);
803 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
804 {
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200805 clib_warning ("app ns lookup failed id:%s", ns_id);
Florin Coras7cb471a2021-07-23 08:39:26 -0700806 rv = VNET_API_ERROR_UNSPECIFIED;
807 }
808 }
809 vec_free (ns_id);
Florin Coras7cb471a2021-07-23 08:39:26 -0700810
811done:
812 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_V2_REPLY, ({
813 if (!rv)
814 rmp->appns_index = clib_host_to_net_u32 (appns_index);
815 }));
816}
817
818static void
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +0200819vl_api_app_namespace_add_del_v4_t_handler (
820 vl_api_app_namespace_add_del_v4_t *mp)
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200821{
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +0200822 vl_api_app_namespace_add_del_v4_reply_t *rmp;
823 u8 *ns_id = 0, *sock_name = 0;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200824 u32 appns_index = 0;
825 int rv = 0;
826 if (session_main_is_enabled () == 0)
827 {
828 rv = VNET_API_ERROR_FEATURE_DISABLED;
829 goto done;
830 }
831 mp->namespace_id[sizeof (mp->namespace_id) - 1] = 0;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200832 ns_id = format (0, "%s", &mp->namespace_id);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200833 sock_name = vl_api_from_api_to_new_vec (mp, &mp->sock_name);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200834 vnet_app_namespace_add_del_args_t args = {
835 .ns_id = ns_id,
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200836 .sock_name = sock_name,
837 .secret = clib_net_to_host_u64 (mp->secret),
838 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
839 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
840 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
841 .is_add = mp->is_add,
842 };
843 rv = vnet_app_namespace_add_del (&args);
844 if (!rv && mp->is_add)
845 {
846 appns_index = app_namespace_index_from_id (ns_id);
847 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
848 {
849 clib_warning ("app ns lookup failed id:%s", ns_id);
850 rv = VNET_API_ERROR_UNSPECIFIED;
851 }
852 }
853 vec_free (ns_id);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200854 vec_free (sock_name);
855done:
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +0200856 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_V4_REPLY, ({
857 if (!rv)
858 rmp->appns_index = clib_host_to_net_u32 (appns_index);
859 }));
860}
861
862static void
863vl_api_app_namespace_add_del_v3_t_handler (
864 vl_api_app_namespace_add_del_v3_t *mp)
865{
866 vl_api_app_namespace_add_del_v3_reply_t *rmp;
867 u8 *ns_id = 0, *sock_name = 0, *api_sock_name = 0;
868 u32 appns_index = 0;
869 int rv = 0;
870 if (session_main_is_enabled () == 0)
871 {
872 rv = VNET_API_ERROR_FEATURE_DISABLED;
873 goto done;
874 }
875 mp->namespace_id[sizeof (mp->namespace_id) - 1] = 0;
876 ns_id = format (0, "%s", &mp->namespace_id);
877 api_sock_name = vl_api_from_api_to_new_vec (mp, &mp->sock_name);
878 mp->netns[sizeof (mp->netns) - 1] = 0;
879 if (strlen ((char *) mp->netns) != 0)
880 {
881 sock_name =
882 format (0, "abstract:%v,netns_name=%s", api_sock_name, &mp->netns);
883 }
884 else
885 {
886 sock_name = api_sock_name;
887 api_sock_name = 0; // for vec_free
888 }
889
890 vnet_app_namespace_add_del_args_t args = {
891 .ns_id = ns_id,
892 .sock_name = sock_name,
893 .secret = clib_net_to_host_u64 (mp->secret),
894 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
895 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
896 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
897 .is_add = mp->is_add,
898 };
899 rv = vnet_app_namespace_add_del (&args);
900 if (!rv && mp->is_add)
901 {
902 appns_index = app_namespace_index_from_id (ns_id);
903 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
904 {
905 clib_warning ("app ns lookup failed id:%s", ns_id);
906 rv = VNET_API_ERROR_UNSPECIFIED;
907 }
908 }
909 vec_free (ns_id);
910 vec_free (sock_name);
911 vec_free (api_sock_name);
912done:
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200913 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_V3_REPLY, ({
914 if (!rv)
915 rmp->appns_index = clib_host_to_net_u32 (appns_index);
916 }));
917}
918
919static void
Florin Coras1c710452017-10-17 00:03:13 -0700920vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
921{
922 vl_api_session_rule_add_del_reply_t *rmp;
923 session_rule_add_del_args_t args;
924 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -0700925 int rv = 0;
926
Dave Barachb7b92992018-10-17 10:38:51 -0400927 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -0700928
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100929 ip_prefix_decode (&mp->lcl, &table_args->lcl);
930 ip_prefix_decode (&mp->rmt, &table_args->rmt);
931
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100932 table_args->lcl_port = mp->lcl_port;
933 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -0700934 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
935 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -0800936 mp->tag[sizeof (mp->tag) - 1] = 0;
937 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700938 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
939 args.scope = mp->scope;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100940 args.transport_proto =
941 api_session_transport_proto_decode (&mp->transport_proto) ==
942 TRANSPORT_PROTO_UDP ? 1 : 0;
Florin Coras1c710452017-10-17 00:03:13 -0700943
Florin Corasc1a42652019-02-08 18:27:29 -0800944 rv = vnet_session_rule_add_del (&args);
945 if (rv)
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200946 {
947 clib_warning ("rule add del returned: %U", format_session_error, rv);
948 rv = VNET_API_ERROR_UNSPECIFIED;
949 }
Florin Corasc97a7392017-11-05 23:07:07 -0800950 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700951 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
952}
953
Florin Coras6c36f532017-11-03 18:32:34 -0700954static void
955send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -0800956 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800957 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700958{
959 vl_api_session_rules_details_t *rmp = 0;
960 session_mask_or_match_4_t *match =
961 (session_mask_or_match_4_t *) & rule->match;
962 session_mask_or_match_4_t *mask =
963 (session_mask_or_match_4_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100964 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -0700965
966 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400967 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar0046e972021-06-26 22:12:08 +0000968 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SESSION_RULES_DETAILS);
Florin Coras6c36f532017-11-03 18:32:34 -0700969 rmp->context = context;
970
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100971 clib_memset (&lcl, 0, sizeof (lcl));
972 clib_memset (&rmt, 0, sizeof (rmt));
973 ip_set (&lcl.fp_addr, &match->lcl_ip, 1);
974 ip_set (&rmt.fp_addr, &match->rmt_ip, 1);
975 lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip);
976 rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip);
977
978 ip_prefix_encode (&lcl, &rmp->lcl);
979 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100980 rmp->lcl_port = match->lcl_port;
981 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700982 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
983 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100984 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
985 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -0700986 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800987 if (tag)
988 {
Dave Barach178cf492018-11-13 16:34:13 -0500989 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -0800990 rmp->tag[vec_len (tag)] = 0;
991 }
Florin Coras6c36f532017-11-03 18:32:34 -0700992
Florin Coras6c4dae22018-01-09 06:39:23 -0800993 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700994}
995
996static void
Florin Corasc97a7392017-11-05 23:07:07 -0800997send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
998 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800999 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001000{
1001 vl_api_session_rules_details_t *rmp = 0;
1002 session_mask_or_match_6_t *match =
1003 (session_mask_or_match_6_t *) & rule->match;
1004 session_mask_or_match_6_t *mask =
1005 (session_mask_or_match_6_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001006 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -07001007
1008 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001009 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar0046e972021-06-26 22:12:08 +00001010 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SESSION_RULES_DETAILS);
Florin Coras6c36f532017-11-03 18:32:34 -07001011 rmp->context = context;
1012
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001013 clib_memset (&lcl, 0, sizeof (lcl));
1014 clib_memset (&rmt, 0, sizeof (rmt));
1015 ip_set (&lcl.fp_addr, &match->lcl_ip, 0);
1016 ip_set (&rmt.fp_addr, &match->rmt_ip, 0);
1017 lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip);
1018 rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip);
1019
1020 ip_prefix_encode (&lcl, &rmp->lcl);
1021 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001022 rmp->lcl_port = match->lcl_port;
1023 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001024 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001025 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +01001026 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
1027 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -07001028 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001029 if (tag)
1030 {
Dave Barach178cf492018-11-13 16:34:13 -05001031 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001032 rmp->tag[vec_len (tag)] = 0;
1033 }
Florin Coras6c36f532017-11-03 18:32:34 -07001034
Florin Coras6c4dae22018-01-09 06:39:23 -08001035 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001036}
1037
1038static void
1039send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001040 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001041 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001042{
1043 mma_rule_16_t *rule16;
1044 mma_rule_40_t *rule40;
1045 mma_rules_table_16_t *srt16;
1046 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001047 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001048
Florin Corasc97a7392017-11-05 23:07:07 -08001049 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001050 {
Florin Corasc97a7392017-11-05 23:07:07 -08001051 u8 *tag = 0;
1052 /* *INDENT-OFF* */
1053 srt16 = &srt->session_rules_tables_16;
Damjan Marionb2c31b62020-12-13 21:47:40 +01001054 pool_foreach (rule16, srt16->rules) {
Florin Corasc97a7392017-11-05 23:07:07 -08001055 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1056 tag = session_rules_table_rule_tag (srt, ri, 1);
1057 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001058 reg, context);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001059 }
Florin Corasc97a7392017-11-05 23:07:07 -08001060 /* *INDENT-ON* */
1061 }
1062 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1063 {
1064 u8 *tag = 0;
1065 /* *INDENT-OFF* */
1066 srt40 = &srt->session_rules_tables_40;
Damjan Marionb2c31b62020-12-13 21:47:40 +01001067 pool_foreach (rule40, srt40->rules) {
Florin Corasc97a7392017-11-05 23:07:07 -08001068 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1069 tag = session_rules_table_rule_tag (srt, ri, 1);
1070 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001071 reg, context);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001072 }
Florin Corasc97a7392017-11-05 23:07:07 -08001073 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001074 }
1075}
1076
1077static void
Neale Ranns2b202bc2020-09-21 08:17:51 +00001078vl_api_session_rules_dump_t_handler (vl_api_session_rules_dump_t * mp)
Florin Coras6c36f532017-11-03 18:32:34 -07001079{
Florin Coras6c4dae22018-01-09 06:39:23 -08001080 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001081 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001082 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001083
Florin Coras6c4dae22018-01-09 06:39:23 -08001084 reg = vl_api_client_index_to_registration (mp->client_index);
1085 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001086 return;
1087
1088 /* *INDENT-OFF* */
1089 session_table_foreach (st, ({
Florin Coras07063b82020-03-13 04:44:51 +00001090 for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++)
Florin Corasc97a7392017-11-05 23:07:07 -08001091 {
1092 send_session_rules_table_details (&st->session_rules[tp],
1093 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001094 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001095 mp->context);
1096 }
Florin Coras6c36f532017-11-03 18:32:34 -07001097 }));
1098 /* *INDENT-ON* */
1099}
1100
Florin Coras371ca502018-02-21 12:07:41 -08001101static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001102vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001103{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001104 vl_api_app_add_cert_key_pair_reply_t *rmp;
1105 vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1106 u32 certkey_len, key_len, cert_len;
Florin Coras371ca502018-02-21 12:07:41 -08001107 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001108 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001109 {
1110 rv = VNET_API_ERROR_FEATURE_DISABLED;
1111 goto done;
1112 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001113
Florin Coras371ca502018-02-21 12:07:41 -08001114 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001115 if (cert_len > 10000)
1116 {
1117 rv = VNET_API_ERROR_INVALID_VALUE;
1118 goto done;
1119 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001120
1121 certkey_len = clib_net_to_host_u16 (mp->certkey_len);
1122 if (certkey_len < cert_len)
Florin Coras371ca502018-02-21 12:07:41 -08001123 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001124 rv = VNET_API_ERROR_INVALID_VALUE;
1125 goto done;
Florin Coras371ca502018-02-21 12:07:41 -08001126 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001127
1128 key_len = certkey_len - cert_len;
1129 if (key_len > 10000)
1130 {
1131 rv = VNET_API_ERROR_INVALID_VALUE;
1132 goto done;
1133 }
1134
1135 clib_memset (a, 0, sizeof (*a));
Florin Corasa5a9efd2021-01-05 17:03:29 -08001136 a->cert = mp->certkey;
1137 a->key = mp->certkey + cert_len;
1138 a->cert_len = cert_len;
1139 a->key_len = key_len;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001140 rv = vnet_app_add_cert_key_pair (a);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001141
Florin Coras371ca502018-02-21 12:07:41 -08001142done:
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001143 /* *INDENT-OFF* */
1144 REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
1145 if (!rv)
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001146 rmp->index = clib_host_to_net_u32 (a->index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001147 }));
1148 /* *INDENT-ON* */
Florin Coras371ca502018-02-21 12:07:41 -08001149}
1150
1151static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001152vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001153{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001154 vl_api_app_del_cert_key_pair_reply_t *rmp;
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001155 u32 ckpair_index;
Florin Coras371ca502018-02-21 12:07:41 -08001156 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001157 if (session_main_is_enabled () == 0)
1158 {
1159 rv = VNET_API_ERROR_FEATURE_DISABLED;
1160 goto done;
1161 }
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001162 ckpair_index = clib_net_to_host_u32 (mp->index);
1163 rv = vnet_app_del_cert_key_pair (ckpair_index);
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001164 if (rv)
1165 {
1166 clib_warning ("vnet_app_del_cert_key_pair: %U", format_session_error,
1167 rv);
1168 rv = VNET_API_ERROR_UNSPECIFIED;
1169 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001170
1171done:
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001172 REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001173}
1174
Florin Coras6cf30ad2017-04-04 23:08:23 -07001175static clib_error_t *
1176application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001177{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001178 application_t *app = application_lookup (client_index);
1179 vnet_app_detach_args_t _a, *a = &_a;
1180 if (app)
1181 {
Florin Coras15531972018-08-12 23:50:53 -07001182 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001183 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001184 vnet_application_detach (a);
1185 }
1186 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001187}
1188
Florin Coras6cf30ad2017-04-04 23:08:23 -07001189VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001190
Dave Barach68b0fb02017-02-28 15:15:56 -05001191/*
Florin Coras61ae0562020-09-02 19:10:28 -07001192 * Socket api functions
1193 */
1194
Florin Coras61ae0562020-09-02 19:10:28 -07001195static int
1196mq_send_add_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1197{
Florin Coras20c24232021-11-22 21:19:01 -08001198 session_app_add_segment_msg_t m = { 0 };
Florin Coras61ae0562020-09-02 19:10:28 -07001199 app_worker_t *app_wrk;
Florin Coras61ae0562020-09-02 19:10:28 -07001200 fifo_segment_t *fs;
1201 ssvm_private_t *sp;
1202 u8 fd_flags = 0;
1203
1204 app_wrk = app_worker_get (app_wrk_index);
1205
1206 fs = segment_manager_get_segment_w_handle (segment_handle);
1207 sp = &fs->ssvm;
1208 ASSERT (ssvm_type (sp) == SSVM_SEGMENT_MEMFD);
1209
1210 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
Florin Coras61ae0562020-09-02 19:10:28 -07001211
Florin Coras20c24232021-11-22 21:19:01 -08001212 m.segment_size = sp->ssvm_size;
1213 m.fd_flags = fd_flags;
1214 m.segment_handle = segment_handle;
1215 strncpy ((char *) m.segment_name, (char *) sp->name,
1216 sizeof (m.segment_name) - 1);
Florin Coras61ae0562020-09-02 19:10:28 -07001217
Florin Coras20c24232021-11-22 21:19:01 -08001218 app_wrk_send_ctrl_evt_fd (app_wrk, SESSION_CTRL_EVT_APP_ADD_SEGMENT, &m,
1219 sizeof (m), sp->fd);
Florin Coras61ae0562020-09-02 19:10:28 -07001220
1221 return 0;
1222}
1223
1224static int
1225mq_send_del_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1226{
Florin Coras20c24232021-11-22 21:19:01 -08001227 session_app_del_segment_msg_t m = { 0 };
Florin Coras61ae0562020-09-02 19:10:28 -07001228 app_worker_t *app_wrk;
Florin Coras61ae0562020-09-02 19:10:28 -07001229
1230 app_wrk = app_worker_get (app_wrk_index);
1231
Florin Coras20c24232021-11-22 21:19:01 -08001232 m.segment_handle = segment_handle;
Florin Coras61ae0562020-09-02 19:10:28 -07001233
Florin Coras20c24232021-11-22 21:19:01 -08001234 app_wrk_send_ctrl_evt (app_wrk, SESSION_CTRL_EVT_APP_DEL_SEGMENT, &m,
1235 sizeof (m));
Florin Coras61ae0562020-09-02 19:10:28 -07001236
1237 return 0;
1238}
1239
1240static session_cb_vft_t session_mq_sapi_cb_vft = {
1241 .session_accept_callback = mq_send_session_accepted_cb,
1242 .session_disconnect_callback = mq_send_session_disconnected_cb,
1243 .session_connected_callback = mq_send_session_connected_cb,
1244 .session_reset_callback = mq_send_session_reset_cb,
1245 .session_migrate_callback = mq_send_session_migrate_cb,
1246 .session_cleanup_callback = mq_send_session_cleanup_cb,
1247 .add_segment_callback = mq_send_add_segment_sapi_cb,
1248 .del_segment_callback = mq_send_del_segment_sapi_cb,
1249};
1250
1251static void
1252session_api_attach_handler (app_namespace_t * app_ns, clib_socket_t * cs,
1253 app_sapi_attach_msg_t * mp)
1254{
Florin Coras41d5f542021-01-15 13:49:33 -08001255 int rv = 0, *fds = 0, n_fds = 0, i, n_workers;
Florin Coras61ae0562020-09-02 19:10:28 -07001256 vnet_app_attach_args_t _a, *a = &_a;
1257 app_sapi_attach_reply_msg_t *rmp;
Florin Coras61ae0562020-09-02 19:10:28 -07001258 u8 fd_flags = 0, ctrl_thread;
1259 app_ns_api_handle_t *handle;
Florin Coras41d5f542021-01-15 13:49:33 -08001260 fifo_segment_t *rx_mqs_seg;
Florin Coras61ae0562020-09-02 19:10:28 -07001261 app_sapi_msg_t msg = { 0 };
1262 app_worker_t *app_wrk;
Florin Coras61ae0562020-09-02 19:10:28 -07001263 application_t *app;
Florin Coras41d5f542021-01-15 13:49:33 -08001264 svm_msg_q_t *rx_mq;
Florin Coras61ae0562020-09-02 19:10:28 -07001265
1266 /* Make sure name is null terminated */
1267 mp->name[63] = 0;
1268
1269 clib_memset (a, 0, sizeof (*a));
1270 a->api_client_index = appns_sapi_socket_handle (app_ns, cs);
1271 a->name = format (0, "%s", (char *) mp->name);
1272 a->options = mp->options;
1273 a->session_cb_vft = &session_mq_sapi_cb_vft;
1274 a->use_sock_api = 1;
1275 a->options[APP_OPTIONS_NAMESPACE] = app_namespace_index (app_ns);
1276
1277 if ((rv = vnet_application_attach (a)))
1278 {
1279 clib_warning ("attach returned: %d", rv);
1280 goto done;
1281 }
1282
Florin Coras41d5f542021-01-15 13:49:33 -08001283 n_workers = vlib_num_workers ();
1284 vec_validate (fds, 3 /* segs + tx evtfd */ + n_workers);
1285
Florin Coras61ae0562020-09-02 19:10:28 -07001286 /* Send event queues segment */
Florin Coras41d5f542021-01-15 13:49:33 -08001287 app = application_get (a->app_index);
1288 rx_mqs_seg = application_get_rx_mqs_segment (app);
1289
1290 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
1291 fds[n_fds] = rx_mqs_seg->ssvm.fd;
1292 n_fds += 1;
1293
Florin Coras61ae0562020-09-02 19:10:28 -07001294 /* Send fifo segment fd if needed */
1295 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
1296 {
1297 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1298 fds[n_fds] = a->segment->fd;
1299 n_fds += 1;
1300 }
1301 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
1302 {
1303 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
Florin Coras86f12322021-01-22 15:05:14 -08001304 fds[n_fds] = svm_msg_q_get_eventfd (a->app_evt_q);
Florin Coras61ae0562020-09-02 19:10:28 -07001305 n_fds += 1;
1306 }
1307
Florin Coras41d5f542021-01-15 13:49:33 -08001308 if (application_use_private_rx_mqs ())
1309 {
1310 fd_flags |= SESSION_FD_F_VPP_MQ_EVENTFD;
1311 for (i = 0; i < n_workers + 1; i++)
1312 {
1313 rx_mq = application_rx_mq_get (app, i);
1314 fds[n_fds] = svm_msg_q_get_eventfd (rx_mq);
1315 n_fds += 1;
1316 }
1317 }
1318
Florin Coras61ae0562020-09-02 19:10:28 -07001319done:
1320
1321 msg.type = APP_SAPI_MSG_TYPE_ATTACH_REPLY;
1322 rmp = &msg.attach_reply;
1323 rmp->retval = rv;
1324 if (!rv)
1325 {
Florin Coras41d5f542021-01-15 13:49:33 -08001326 ctrl_thread = n_workers ? 1 : 0;
Florin Coras61ae0562020-09-02 19:10:28 -07001327 rmp->app_index = a->app_index;
Florin Corasb4624182020-12-11 13:58:12 -08001328 rmp->app_mq =
1329 fifo_segment_msg_q_offset ((fifo_segment_t *) a->segment, 0);
Florin Coras41d5f542021-01-15 13:49:33 -08001330 rmp->vpp_ctrl_mq = fifo_segment_msg_q_offset (rx_mqs_seg, ctrl_thread);
Florin Coras61ae0562020-09-02 19:10:28 -07001331 rmp->vpp_ctrl_mq_thread = ctrl_thread;
1332 rmp->n_fds = n_fds;
1333 rmp->fd_flags = fd_flags;
1334 /* No segment name and size since we only support memfds
1335 * in this configuration */
1336 rmp->segment_handle = a->segment_handle;
1337 rmp->api_client_handle = a->api_client_index;
1338
1339 /* Update app index for socket */
1340 handle = (app_ns_api_handle_t *) & cs->private_data;
Florin Coras61ae0562020-09-02 19:10:28 -07001341 app_wrk = application_get_worker (app, 0);
1342 handle->aah_app_wrk_index = app_wrk->wrk_index;
1343 }
1344
1345 clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1346 vec_free (a->name);
Florin Coras41d5f542021-01-15 13:49:33 -08001347 vec_free (fds);
Florin Coras61ae0562020-09-02 19:10:28 -07001348}
1349
Florin Coras98078ab2021-08-12 18:12:09 -07001350void
Florin Coras61ae0562020-09-02 19:10:28 -07001351sapi_socket_close_w_handle (u32 api_handle)
1352{
1353 app_namespace_t *app_ns = app_namespace_get (api_handle >> 16);
1354 u16 sock_index = api_handle & 0xffff;
1355 app_ns_api_handle_t *handle;
1356 clib_socket_t *cs;
1357 clib_file_t *cf;
1358
1359 cs = appns_sapi_get_socket (app_ns, sock_index);
1360 if (!cs)
1361 return;
1362
1363 handle = (app_ns_api_handle_t *) & cs->private_data;
1364 cf = clib_file_get (&file_main, handle->aah_file_index);
1365 clib_file_del (&file_main, cf);
1366
1367 clib_socket_close (cs);
1368 appns_sapi_free_socket (app_ns, cs);
1369}
1370
1371static void
1372sapi_add_del_worker_handler (app_namespace_t * app_ns,
1373 clib_socket_t * cs,
1374 app_sapi_worker_add_del_msg_t * mp)
1375{
1376 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1377 app_sapi_worker_add_del_reply_msg_t *rmp;
1378 app_ns_api_handle_t *handle;
1379 app_sapi_msg_t msg = { 0 };
1380 app_worker_t *app_wrk;
Florin Corascc7c88e2020-09-15 15:56:51 -07001381 u32 sapi_handle = -1;
Florin Coras61ae0562020-09-02 19:10:28 -07001382 application_t *app;
Florin Coras61ae0562020-09-02 19:10:28 -07001383 u8 fd_flags = 0;
1384
1385 app = application_get_if_valid (mp->app_index);
1386 if (!app)
1387 {
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001388 rv = SESSION_E_INVALID;
Florin Coras61ae0562020-09-02 19:10:28 -07001389 goto done;
1390 }
1391
1392 sapi_handle = appns_sapi_socket_handle (app_ns, cs);
1393
1394 vnet_app_worker_add_del_args_t args = {
1395 .app_index = app->app_index,
1396 .wrk_map_index = mp->wrk_index,
1397 .api_client_index = sapi_handle,
1398 .is_add = mp->is_add
1399 };
1400 rv = vnet_app_worker_add_del (&args);
1401 if (rv)
1402 {
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001403 clib_warning ("app worker add/del returned: %U", format_session_error,
1404 rv);
Florin Coras61ae0562020-09-02 19:10:28 -07001405 goto done;
1406 }
1407
1408 if (!mp->is_add)
Florin Coras98078ab2021-08-12 18:12:09 -07001409 goto done;
Florin Coras61ae0562020-09-02 19:10:28 -07001410
1411 /* Send fifo segment fd if needed */
1412 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1413 {
1414 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1415 fds[n_fds] = args.segment->fd;
1416 n_fds += 1;
1417 }
1418 if (application_segment_manager_properties (app)->use_mq_eventfd)
1419 {
1420 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
Florin Coras86f12322021-01-22 15:05:14 -08001421 fds[n_fds] = svm_msg_q_get_eventfd (args.evt_q);
Florin Coras61ae0562020-09-02 19:10:28 -07001422 n_fds += 1;
1423 }
1424
1425done:
1426
1427 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY;
1428 rmp = &msg.worker_add_del_reply;
1429 rmp->retval = rv;
1430 rmp->is_add = mp->is_add;
Florin Corascc7c88e2020-09-15 15:56:51 -07001431 rmp->api_client_handle = sapi_handle;
Florin Coras61ae0562020-09-02 19:10:28 -07001432 rmp->wrk_index = args.wrk_map_index;
1433 rmp->segment_handle = args.segment_handle;
1434 if (!rv && mp->is_add)
1435 {
1436 /* No segment name and size. This supports only memfds */
Florin Corasb4624182020-12-11 13:58:12 -08001437 rmp->app_event_queue_address =
1438 fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0);
Florin Coras61ae0562020-09-02 19:10:28 -07001439 rmp->n_fds = n_fds;
1440 rmp->fd_flags = fd_flags;
1441
1442 /* Update app index for socket */
1443 handle = (app_ns_api_handle_t *) & cs->private_data;
1444 app_wrk = application_get_worker (app, args.wrk_map_index);
1445 handle->aah_app_wrk_index = app_wrk->wrk_index;
1446 }
1447
1448 clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1449}
1450
Filip Tehlar447e51d2022-02-18 08:49:43 +00001451/* This is a workaround for the case when session layer starts reading
1452 * the socket before the client actualy sends the data
1453 */
1454static clib_error_t *
1455sapi_socket_receive_wait (clib_socket_t *cs, u8 *msg, u32 msg_len)
1456{
1457 clib_error_t *err;
1458 int n_tries = 5;
1459
1460 while (1)
1461 {
1462 err = clib_socket_recvmsg (cs, msg, msg_len, 0, 0);
1463 if (!err)
1464 break;
1465
1466 if (!n_tries)
1467 return err;
1468
1469 n_tries--;
1470 usleep (1);
1471 }
1472
1473 return err;
1474}
1475
Florin Coras61ae0562020-09-02 19:10:28 -07001476static void
Florin Corase191d762021-08-11 14:55:49 -07001477sapi_add_del_cert_key_handler (app_namespace_t *app_ns, clib_socket_t *cs,
1478 app_sapi_cert_key_add_del_msg_t *mp)
1479{
1480 vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1481 app_sapi_cert_key_add_del_reply_msg_t *rmp;
1482 app_sapi_msg_t msg = { 0 };
1483 int rv = 0;
1484
1485 if (mp->is_add)
1486 {
1487 const u32 max_certkey_len = 2e4, max_cert_len = 1e4, max_key_len = 1e4;
1488 clib_error_t *err;
1489 u8 *certkey = 0;
1490 u32 key_len;
1491
1492 if (mp->certkey_len > max_certkey_len)
1493 {
1494 rv = SESSION_E_INVALID;
1495 goto send_reply;
1496 }
1497
1498 vec_validate (certkey, mp->certkey_len - 1);
Filip Tehlar447e51d2022-02-18 08:49:43 +00001499
1500 err = sapi_socket_receive_wait (cs, certkey, mp->certkey_len);
Florin Corase191d762021-08-11 14:55:49 -07001501 if (err)
1502 {
1503 clib_error_report (err);
Florin Corase191d762021-08-11 14:55:49 -07001504 rv = SESSION_E_INVALID;
1505 goto send_reply;
1506 }
1507
1508 if (mp->cert_len > max_cert_len)
1509 {
1510 rv = SESSION_E_INVALID;
1511 goto send_reply;
1512 }
1513
1514 if (mp->certkey_len < mp->cert_len)
1515 {
1516 rv = SESSION_E_INVALID;
1517 goto send_reply;
1518 }
1519
1520 key_len = mp->certkey_len - mp->cert_len;
1521 if (key_len > max_key_len)
1522 {
1523 rv = SESSION_E_INVALID;
1524 goto send_reply;
1525 }
1526
1527 clib_memset (a, 0, sizeof (*a));
1528 a->cert = certkey;
1529 a->key = certkey + mp->cert_len;
1530 a->cert_len = mp->cert_len;
1531 a->key_len = key_len;
1532 rv = vnet_app_add_cert_key_pair (a);
1533
1534 vec_free (certkey);
1535 }
1536 else
1537 {
1538 rv = vnet_app_del_cert_key_pair (mp->index);
1539 }
1540
1541send_reply:
1542
1543 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY_REPLY;
1544 rmp = &msg.cert_key_add_del_reply;
1545 rmp->retval = rv;
1546 rmp->context = mp->context;
1547 if (!rv && mp->is_add)
1548 rmp->index = a->index;
1549
1550 clib_socket_sendmsg (cs, &msg, sizeof (msg), 0, 0);
1551}
1552
1553static void
Florin Coras61ae0562020-09-02 19:10:28 -07001554sapi_socket_detach (app_namespace_t * app_ns, clib_socket_t * cs)
1555{
Florin Coras61ae0562020-09-02 19:10:28 -07001556 app_ns_api_handle_t *handle;
1557 app_worker_t *app_wrk;
1558 u32 api_client_handle;
1559
1560 api_client_handle = appns_sapi_socket_handle (app_ns, cs);
Florin Coras61ae0562020-09-02 19:10:28 -07001561
Florin Corasf99a7d62020-09-14 10:29:29 -07001562 /* Cleanup everything because app worker closed socket or crashed */
Florin Coras61ae0562020-09-02 19:10:28 -07001563 handle = (app_ns_api_handle_t *) & cs->private_data;
Filip Tehlar9c32f052022-03-11 11:12:56 +00001564 app_wrk = app_worker_get_if_valid (handle->aah_app_wrk_index);
1565 if (!app_wrk)
1566 return;
Florin Corasf99a7d62020-09-14 10:29:29 -07001567
1568 vnet_app_worker_add_del_args_t args = {
1569 .app_index = app_wrk->app_index,
1570 .wrk_map_index = app_wrk->wrk_map_index,
1571 .api_client_index = api_client_handle,
1572 .is_add = 0
1573 };
1574 /* Send rpc to main thread for worker barrier */
1575 vlib_rpc_call_main_thread (vnet_app_worker_add_del, (u8 *) & args,
1576 sizeof (args));
Florin Coras61ae0562020-09-02 19:10:28 -07001577}
1578
1579static clib_error_t *
1580sapi_sock_read_ready (clib_file_t * cf)
1581{
1582 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
Florin Coras7360e3d2020-09-18 16:15:47 -07001583 vlib_main_t *vm = vlib_get_main ();
Florin Coras61ae0562020-09-02 19:10:28 -07001584 app_sapi_msg_t msg = { 0 };
1585 app_namespace_t *app_ns;
1586 clib_error_t *err = 0;
1587 clib_socket_t *cs;
1588
1589 app_ns = app_namespace_get (handle->aah_app_ns_index);
1590 cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
1591 if (!cs)
1592 goto error;
1593
1594 err = clib_socket_recvmsg (cs, &msg, sizeof (msg), 0, 0);
1595 if (err)
1596 {
1597 clib_error_free (err);
1598 sapi_socket_detach (app_ns, cs);
1599 goto error;
1600 }
1601
1602 handle = (app_ns_api_handle_t *) & cs->private_data;
1603
Florin Coras7360e3d2020-09-18 16:15:47 -07001604 vlib_worker_thread_barrier_sync (vm);
1605
Florin Coras61ae0562020-09-02 19:10:28 -07001606 switch (msg.type)
1607 {
1608 case APP_SAPI_MSG_TYPE_ATTACH:
1609 session_api_attach_handler (app_ns, cs, &msg.attach);
1610 break;
1611 case APP_SAPI_MSG_TYPE_ADD_DEL_WORKER:
1612 sapi_add_del_worker_handler (app_ns, cs, &msg.worker_add_del);
1613 break;
Florin Corase191d762021-08-11 14:55:49 -07001614 case APP_SAPI_MSG_TYPE_ADD_DEL_CERT_KEY:
1615 sapi_add_del_cert_key_handler (app_ns, cs, &msg.cert_key_add_del);
1616 break;
Florin Coras61ae0562020-09-02 19:10:28 -07001617 default:
1618 clib_warning ("app wrk %u unknown message type: %u",
1619 handle->aah_app_wrk_index, msg.type);
1620 break;
1621 }
1622
Florin Coras7360e3d2020-09-18 16:15:47 -07001623 vlib_worker_thread_barrier_release (vm);
1624
Florin Coras61ae0562020-09-02 19:10:28 -07001625error:
1626 return 0;
1627}
1628
1629static clib_error_t *
1630sapi_sock_write_ready (clib_file_t * cf)
1631{
1632 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1633 clib_warning ("called for app ns %u", handle->aah_app_ns_index);
1634 return 0;
1635}
1636
1637static clib_error_t *
1638sapi_sock_error (clib_file_t * cf)
1639{
1640 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1641 app_namespace_t *app_ns;
1642 clib_socket_t *cs;
1643
1644 app_ns = app_namespace_get (handle->aah_app_ns_index);
1645 cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
1646 if (!cs)
1647 return 0;
1648
1649 sapi_socket_detach (app_ns, cs);
1650 return 0;
1651}
1652
1653static clib_error_t *
1654sapi_sock_accept_ready (clib_file_t * scf)
1655{
1656 app_ns_api_handle_t handle = *(app_ns_api_handle_t *) & scf->private_data;
1657 app_namespace_t *app_ns;
1658 clib_file_t cf = { 0 };
1659 clib_error_t *err = 0;
1660 clib_socket_t *ccs, *scs;
1661
1662 /* Listener files point to namespace */
1663 app_ns = app_namespace_get (handle.aah_app_ns_index);
1664
1665 /*
1666 * Initialize client socket
1667 */
1668 ccs = appns_sapi_alloc_socket (app_ns);
1669
1670 /* Grab server socket after client is initialized */
1671 scs = appns_sapi_get_socket (app_ns, handle.aah_sock_index);
1672 if (!scs)
1673 goto error;
1674
1675 err = clib_socket_accept (scs, ccs);
1676 if (err)
1677 {
1678 clib_error_report (err);
1679 goto error;
1680 }
1681
1682 cf.read_function = sapi_sock_read_ready;
1683 cf.write_function = sapi_sock_write_ready;
1684 cf.error_function = sapi_sock_error;
1685 cf.file_descriptor = ccs->fd;
1686 /* File points to app namespace and socket */
1687 handle.aah_sock_index = appns_sapi_socket_index (app_ns, ccs);
Florin Coras6b6c10b2020-09-24 11:58:28 -07001688 cf.private_data = handle.as_u64;
Florin Coras61ae0562020-09-02 19:10:28 -07001689 cf.description = format (0, "app sock conn fd: %d", ccs->fd);
1690
1691 /* Poll until we get an attach message. Socket points to file and
1692 * application that owns the socket */
1693 handle.aah_app_wrk_index = APP_INVALID_INDEX;
1694 handle.aah_file_index = clib_file_add (&file_main, &cf);
Florin Coras6b6c10b2020-09-24 11:58:28 -07001695 ccs->private_data = handle.as_u64;
Florin Coras61ae0562020-09-02 19:10:28 -07001696
1697 return err;
1698
1699error:
1700 appns_sapi_free_socket (app_ns, ccs);
1701 return err;
1702}
1703
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +02001704void
1705appns_sapi_del_ns_socket (app_namespace_t *app_ns)
1706{
1707 app_ns_api_handle_t *handle;
1708 clib_socket_t *cs;
1709
1710 pool_foreach (cs, app_ns->app_sockets)
1711 {
1712 handle = (app_ns_api_handle_t *) &cs->private_data;
1713 clib_file_del_by_index (&file_main, handle->aah_file_index);
1714
1715 clib_socket_close (cs);
1716 clib_socket_free (cs);
1717 }
1718 pool_free (app_ns->app_sockets);
1719}
1720
Florin Coras61ae0562020-09-02 19:10:28 -07001721int
1722appns_sapi_add_ns_socket (app_namespace_t * app_ns)
1723{
1724 char *subdir = "/app_ns_sockets/";
1725 app_ns_api_handle_t *handle;
1726 clib_file_t cf = { 0 };
1727 struct stat file_stat;
1728 clib_error_t *err;
1729 clib_socket_t *cs;
Benoît Ganne0bb670e2021-09-09 12:00:00 +02001730 char dir[4096];
Florin Coras61ae0562020-09-02 19:10:28 -07001731
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +02001732 snprintf (dir, sizeof (dir), "%s%s", vlib_unix_get_runtime_dir (), subdir);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001733
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +02001734 if (!app_ns->sock_name)
1735 app_ns->sock_name = format (0, "%s%v%c", dir, app_ns->ns_id, 0);
Florin Coras61ae0562020-09-02 19:10:28 -07001736
1737 /*
1738 * Create and initialize socket to listen on
1739 */
1740 cs = appns_sapi_alloc_socket (app_ns);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +02001741 cs->config = (char *) vec_dup (app_ns->sock_name);
Florin Coras61ae0562020-09-02 19:10:28 -07001742 cs->flags = CLIB_SOCKET_F_IS_SERVER |
1743 CLIB_SOCKET_F_ALLOW_GROUP_WRITE |
1744 CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED;
1745
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +02001746 if (clib_socket_prefix_get_type (cs->config) == CLIB_SOCKET_TYPE_UNIX)
1747 {
1748 err = vlib_unix_recursive_mkdir ((char *) dir);
1749 if (err)
1750 {
1751 clib_error_report (err);
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001752 return SESSION_E_SYSCALL;
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +02001753 }
1754 }
1755
1756 if ((err = clib_socket_init (cs)))
Florin Coras61ae0562020-09-02 19:10:28 -07001757 {
1758 clib_error_report (err);
Benoît Ganne0bb670e2021-09-09 12:00:00 +02001759 return -1;
Florin Coras61ae0562020-09-02 19:10:28 -07001760 }
1761
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +02001762 if (clib_socket_prefix_get_type (cs->config) == CLIB_SOCKET_TYPE_UNIX &&
1763 stat ((char *) app_ns->sock_name, &file_stat) == -1)
Benoît Ganne0bb670e2021-09-09 12:00:00 +02001764 return -1;
Florin Coras61ae0562020-09-02 19:10:28 -07001765
1766 /*
1767 * Start polling it
1768 */
1769 cf.read_function = sapi_sock_accept_ready;
1770 cf.file_descriptor = cs->fd;
1771 /* File points to namespace */
1772 handle = (app_ns_api_handle_t *) & cf.private_data;
1773 handle->aah_app_ns_index = app_namespace_index (app_ns);
1774 handle->aah_sock_index = appns_sapi_socket_index (app_ns, cs);
1775 cf.description = format (0, "app sock listener: %s", app_ns->sock_name);
1776
1777 /* Socket points to clib file index */
1778 handle = (app_ns_api_handle_t *) & cs->private_data;
1779 handle->aah_file_index = clib_file_add (&file_main, &cf);
1780 handle->aah_app_wrk_index = APP_INVALID_INDEX;
1781
Benoît Ganne0bb670e2021-09-09 12:00:00 +02001782 return 0;
Florin Coras61ae0562020-09-02 19:10:28 -07001783}
1784
Filip Tehlar0046e972021-06-26 22:12:08 +00001785#include <vnet/session/session.api.c>
1786static clib_error_t *
1787session_api_hookup (vlib_main_t *vm)
1788{
1789 /*
1790 * Set up the (msg_name, crc, message-id) table
1791 */
1792 REPLY_MSG_ID_BASE = setup_message_id_table ();
1793
1794 return 0;
1795}
1796
1797VLIB_API_INIT_FUNCTION (session_api_hookup);
1798
Florin Coras61ae0562020-09-02 19:10:28 -07001799/*
Dave Barach68b0fb02017-02-28 15:15:56 -05001800 * fd.io coding-style-patch-verification: ON
1801 *
1802 * Local Variables:
1803 * eval: (c-set-style "gnu")
1804 * End:
1805 */