blob: 31022294be520683e1beea2cdfa369b21c378464 [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 Coras83ea6692018-10-12 16:55:14 -070085mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
86{
87 int rv;
88 u8 try = 0;
89 while (try < 100)
90 {
91 rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
92 SESSION_MQ_CTRL_EVT_RING,
93 SVM_Q_NOWAIT, msg);
94 if (!rv)
95 return 0;
96 try++;
Florin Corase2ea1932018-12-17 23:08:14 -080097 usleep (1);
Florin Coras83ea6692018-10-12 16:55:14 -070098 }
Florin Corasdc2e2512018-12-03 17:47:26 -080099 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700100 return -1;
101}
102
103static int
Florin Coras288eaab2019-02-03 15:26:14 -0800104mq_send_session_accepted_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700105{
Florin Coras15531972018-08-12 23:50:53 -0700106 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700107 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras09bf91a2021-02-23 08:44:13 -0800108 session_accepted_msg_t m = { 0 };
Florin Corasb4624182020-12-11 13:58:12 -0800109 svm_msg_q_t *app_mq;
110 fifo_segment_t *eq_seg;
Florin Coras288eaab2019-02-03 15:26:14 -0800111 session_t *listener;
Florin Coras52207f12018-07-12 14:48:06 -0700112 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700113 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700114
Florin Coras15531972018-08-12 23:50:53 -0700115 app = application_get (app_wrk->app_index);
Florin Coras52207f12018-07-12 14:48:06 -0700116
Florin Coras09bf91a2021-02-23 08:44:13 -0800117 m.context = app->app_index;
118 m.server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
119 m.server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
120 m.segment_handle = session_segment_handle (s);
121 m.flags = s->flags;
Florin Coras52207f12018-07-12 14:48:06 -0700122
Florin Coras41d5f542021-01-15 13:49:33 -0800123 eq_seg = application_get_rx_mqs_segment (app);
Florin Corasb4624182020-12-11 13:58:12 -0800124
Florin Coras52207f12018-07-12 14:48:06 -0700125 if (session_has_transport (s))
126 {
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200127 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras09bf91a2021-02-23 08:44:13 -0800128 m.listener_handle = app_listen_session_handle (listener);
Florin Coras52207f12018-07-12 14:48:06 -0700129 if (application_is_proxy (app))
130 {
131 listener =
Florin Coras15531972018-08-12 23:50:53 -0700132 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
133 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700134 if (listener)
Florin Coras09bf91a2021-02-23 08:44:13 -0800135 m.listener_handle = listen_session_get_handle (listener);
Florin Coras52207f12018-07-12 14:48:06 -0700136 }
Florin Coras09bf91a2021-02-23 08:44:13 -0800137 m.vpp_event_queue_address =
Florin Corasb4624182020-12-11 13:58:12 -0800138 fifo_segment_msg_q_offset (eq_seg, s->thread_index);
Florin Coras09bf91a2021-02-23 08:44:13 -0800139 m.mq_index = s->thread_index;
140 m.handle = session_handle (s);
Aloys Augustincdb71702019-04-08 17:54:39 +0200141
Florin Coras09bf91a2021-02-23 08:44:13 -0800142 session_get_endpoint (s, &m.rmt, 0 /* is_lcl */);
Florin Coras67c90a32021-03-09 18:36:06 -0800143 session_get_endpoint (s, &m.lcl, 1 /* is_lcl */);
Florin Coras52207f12018-07-12 14:48:06 -0700144 }
145 else
146 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800147 ct_connection_t *ct;
Florin Coras99368312018-08-02 10:45:44 -0700148
Florin Coras2b81e3c2019-02-27 07:55:46 -0800149 ct = (ct_connection_t *) session_get_transport (s);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200150 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras09bf91a2021-02-23 08:44:13 -0800151 m.listener_handle = app_listen_session_handle (listener);
152 m.rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
153 m.rmt.port = ct->c_rmt_port;
Florin Coras67c90a32021-03-09 18:36:06 -0800154 m.lcl.port = ct->c_lcl_port;
Florin Coras09bf91a2021-02-23 08:44:13 -0800155 m.handle = session_handle (s);
156 m.vpp_event_queue_address =
Florin Corasb4624182020-12-11 13:58:12 -0800157 fifo_segment_msg_q_offset (eq_seg, s->thread_index);
Florin Coras09bf91a2021-02-23 08:44:13 -0800158 m.mq_index = s->thread_index;
Florin Coras52207f12018-07-12 14:48:06 -0700159 }
Florin Coras09bf91a2021-02-23 08:44:13 -0800160
161 app_mq = app_wrk->event_queue;
162 if (mq_try_lock_and_alloc_msg (app_mq, msg))
163 return SESSION_E_MQ_MSG_ALLOC;
164
165 evt = svm_msg_q_msg_data (app_mq, msg);
166 clib_memset (evt, 0, sizeof (*evt));
167 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
168 clib_memcpy_fast (evt->data, &m, sizeof (m));
Florin Coras537b17e2018-09-28 10:35:45 -0700169 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700170
171 return 0;
172}
173
Florin Coras72b04282019-01-14 17:23:11 -0800174static inline void
175mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
176 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700177{
Florin Coras52207f12018-07-12 14:48:06 -0700178 svm_msg_q_msg_t _msg, *msg = &_msg;
179 session_disconnected_msg_t *mp;
180 svm_msg_q_t *app_mq;
181 session_event_t *evt;
182
Florin Coras15531972018-08-12 23:50:53 -0700183 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700184 if (mq_try_lock_and_alloc_msg (app_mq, msg))
185 return;
Florin Coras52207f12018-07-12 14:48:06 -0700186 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400187 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800188 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700189 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800190 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800191 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700192 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700193}
194
Florin Coras72b04282019-01-14 17:23:11 -0800195static inline void
196mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
197 svm_fifo_t * f, session_evt_type_t evt_type)
198{
199 app_worker_t *app_wrk;
200 application_t *app;
201 int i;
202
203 app = application_get (app_index);
204 if (!app)
205 return;
206
Florin Corasc547e912020-12-08 17:50:45 -0800207 for (i = 0; i < f->shr->n_subscribers; i++)
Florin Coras72b04282019-01-14 17:23:11 -0800208 {
Florin Corasc547e912020-12-08 17:50:45 -0800209 if (!(app_wrk = application_get_worker (app, f->shr->subscribers[i])))
Florin Coras72b04282019-01-14 17:23:11 -0800210 continue;
211 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
212 }
213}
214
215static void
Florin Coras288eaab2019-02-03 15:26:14 -0800216mq_send_session_disconnected_cb (session_t * s)
Florin Coras72b04282019-01-14 17:23:11 -0800217{
218 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
219 session_handle_t sh = session_handle (s);
220
221 mq_send_session_close_evt (app_wrk, session_handle (s),
222 SESSION_CTRL_EVT_DISCONNECTED);
223
Florin Coras288eaab2019-02-03 15:26:14 -0800224 if (svm_fifo_n_subscribers (s->rx_fifo))
225 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800226 SESSION_CTRL_EVT_DISCONNECTED);
227}
228
Florin Coras52207f12018-07-12 14:48:06 -0700229static void
Florin Coras288eaab2019-02-03 15:26:14 -0800230mq_send_session_reset_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700231{
Florin Coras72b04282019-01-14 17:23:11 -0800232 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
233 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700234
Florin Coras72b04282019-01-14 17:23:11 -0800235 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
236
Florin Coras288eaab2019-02-03 15:26:14 -0800237 if (svm_fifo_n_subscribers (s->rx_fifo))
238 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800239 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700240}
241
Florin Coras458089b2019-08-21 16:20:44 -0700242int
Florin Coras15531972018-08-12 23:50:53 -0700243mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras00e01d32019-10-21 16:07:46 -0700244 session_t * s, session_error_t err)
Florin Coras52207f12018-07-12 14:48:06 -0700245{
246 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras09bf91a2021-02-23 08:44:13 -0800247 session_connected_msg_t m = { 0 };
Florin Corasb4624182020-12-11 13:58:12 -0800248 svm_msg_q_t *app_mq;
Florin Coras52207f12018-07-12 14:48:06 -0700249 transport_connection_t *tc;
Florin Corasb4624182020-12-11 13:58:12 -0800250 fifo_segment_t *eq_seg;
Florin Coras15531972018-08-12 23:50:53 -0700251 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700252 session_event_t *evt;
Florin Coras41d5f542021-01-15 13:49:33 -0800253 application_t *app;
254
255 app_wrk = app_worker_get (app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700256
Florin Coras09bf91a2021-02-23 08:44:13 -0800257 m.context = api_context;
258 m.retval = err;
Florin Coras52207f12018-07-12 14:48:06 -0700259
Florin Coras00e01d32019-10-21 16:07:46 -0700260 if (err)
Florin Coras09bf91a2021-02-23 08:44:13 -0800261 goto snd_msg;
Florin Coras52207f12018-07-12 14:48:06 -0700262
Florin Coras41d5f542021-01-15 13:49:33 -0800263 app = application_get (app_wrk->app_index);
264 eq_seg = application_get_rx_mqs_segment (app);
Florin Corasb4624182020-12-11 13:58:12 -0800265
Florin Coras52207f12018-07-12 14:48:06 -0700266 if (session_has_transport (s))
267 {
268 tc = session_get_transport (s);
269 if (!tc)
270 {
Florin Coras00e01d32019-10-21 16:07:46 -0700271 clib_warning ("failed to retrieve transport!");
Florin Coras09bf91a2021-02-23 08:44:13 -0800272 m.retval = SESSION_E_REFUSED;
273 goto snd_msg;
Florin Coras52207f12018-07-12 14:48:06 -0700274 }
275
Florin Coras09bf91a2021-02-23 08:44:13 -0800276 m.handle = session_handle (s);
277 m.vpp_event_queue_address =
Florin Corasb4624182020-12-11 13:58:12 -0800278 fifo_segment_msg_q_offset (eq_seg, s->thread_index);
Aloys Augustincdb71702019-04-08 17:54:39 +0200279
Florin Coras09bf91a2021-02-23 08:44:13 -0800280 session_get_endpoint (s, &m.lcl, 1 /* is_lcl */);
Aloys Augustincdb71702019-04-08 17:54:39 +0200281
Florin Coras09bf91a2021-02-23 08:44:13 -0800282 m.server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
283 m.server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
284 m.segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700285 }
286 else
287 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800288 ct_connection_t *cct;
289 session_t *ss;
Florin Coras99368312018-08-02 10:45:44 -0700290
Florin Coras2b81e3c2019-02-27 07:55:46 -0800291 cct = (ct_connection_t *) session_get_transport (s);
Florin Coras09bf91a2021-02-23 08:44:13 -0800292 m.handle = session_handle (s);
293 m.lcl.port = cct->c_lcl_port;
294 m.lcl.is_ip4 = cct->c_is_ip4;
295 m.vpp_event_queue_address =
Florin Corasb4624182020-12-11 13:58:12 -0800296 fifo_segment_msg_q_offset (eq_seg, s->thread_index);
Florin Coras09bf91a2021-02-23 08:44:13 -0800297 m.server_rx_fifo = fifo_segment_fifo_offset (s->rx_fifo);
298 m.server_tx_fifo = fifo_segment_fifo_offset (s->tx_fifo);
299 m.segment_handle = session_segment_handle (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800300 ss = ct_session_get_peer (s);
Florin Coras09bf91a2021-02-23 08:44:13 -0800301 m.ct_rx_fifo = fifo_segment_fifo_offset (ss->tx_fifo);
302 m.ct_tx_fifo = fifo_segment_fifo_offset (ss->rx_fifo);
303 m.ct_segment_handle = session_segment_handle (ss);
Florin Coras52207f12018-07-12 14:48:06 -0700304 }
305
Florin Coras893bc972021-04-23 08:58:57 -0700306 /* Setup client session index in advance, in case data arrives
307 * before the app processes message and updates it */
308 s->rx_fifo->shr->client_session_index = api_context;
309 s->tx_fifo->shr->client_session_index = api_context;
310
Florin Coras09bf91a2021-02-23 08:44:13 -0800311snd_msg:
312
Florin Coras09bf91a2021-02-23 08:44:13 -0800313 app_mq = app_wrk->event_queue;
314
315 if (mq_try_lock_and_alloc_msg (app_mq, msg))
316 return SESSION_E_MQ_MSG_ALLOC;
317
318 evt = svm_msg_q_msg_data (app_mq, msg);
319 clib_memset (evt, 0, sizeof (*evt));
320 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
321 clib_memcpy_fast (evt->data, &m, sizeof (m));
Florin Coras52207f12018-07-12 14:48:06 -0700322
Florin Coras537b17e2018-09-28 10:35:45 -0700323 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700324 return 0;
325}
326
Florin Coras458089b2019-08-21 16:20:44 -0700327int
Florin Coras60116992018-08-27 09:52:18 -0700328mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
329 session_handle_t handle, int rv)
330{
331 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras09bf91a2021-02-23 08:44:13 -0800332 session_bound_msg_t m = { 0 };
Florin Corasb4624182020-12-11 13:58:12 -0800333 svm_msg_q_t *app_mq;
Yu Ping0b819152019-05-07 02:24:30 +0800334 transport_endpoint_t tep;
Florin Corasb4624182020-12-11 13:58:12 -0800335 fifo_segment_t *eq_seg;
Florin Coras60116992018-08-27 09:52:18 -0700336 app_worker_t *app_wrk;
337 session_event_t *evt;
Florin Coras41d5f542021-01-15 13:49:33 -0800338 application_t *app;
Florin Corasc9940fc2019-02-05 20:55:11 -0800339 app_listener_t *al;
340 session_t *ls = 0;
Florin Coras09bf91a2021-02-23 08:44:13 -0800341
Florin Coras41d5f542021-01-15 13:49:33 -0800342 app_wrk = app_worker_get (app_wrk_index);
343
Florin Coras09bf91a2021-02-23 08:44:13 -0800344 m.context = api_context;
345 m.retval = rv;
346
347 if (rv)
348 goto snd_msg;
349
350 m.handle = handle;
351 al = app_listener_get_w_handle (handle);
352 if (al->session_index != SESSION_INVALID_INDEX)
353 ls = app_listener_get_session (al);
354 else
355 ls = app_listener_get_local_session (al);
356
357 session_get_endpoint (ls, &tep, 1 /* is_lcl */);
358 m.lcl_port = tep.port;
359 m.lcl_is_ip4 = tep.is_ip4;
360 clib_memcpy_fast (m.lcl_ip, &tep.ip, sizeof (tep.ip));
Florin Coras41d5f542021-01-15 13:49:33 -0800361 app = application_get (app_wrk->app_index);
362 eq_seg = application_get_rx_mqs_segment (app);
Florin Coras09bf91a2021-02-23 08:44:13 -0800363 m.vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, ls->thread_index);
364
Florin Coras8694fbc2021-03-10 00:21:02 -0800365 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL &&
366 ls->rx_fifo)
Florin Coras09bf91a2021-02-23 08:44:13 -0800367 {
368 m.rx_fifo = fifo_segment_fifo_offset (ls->rx_fifo);
369 m.tx_fifo = fifo_segment_fifo_offset (ls->tx_fifo);
370 m.segment_handle = session_segment_handle (ls);
371 }
372
373snd_msg:
374
Florin Coras60116992018-08-27 09:52:18 -0700375 app_mq = app_wrk->event_queue;
Florin Coras60116992018-08-27 09:52:18 -0700376
Florin Coras83ea6692018-10-12 16:55:14 -0700377 if (mq_try_lock_and_alloc_msg (app_mq, msg))
Florin Coras00e01d32019-10-21 16:07:46 -0700378 return SESSION_E_MQ_MSG_ALLOC;
Florin Coras83ea6692018-10-12 16:55:14 -0700379
Florin Coras60116992018-08-27 09:52:18 -0700380 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400381 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700382 evt->event_type = SESSION_CTRL_EVT_BOUND;
Florin Coras09bf91a2021-02-23 08:44:13 -0800383 clib_memcpy_fast (evt->data, &m, sizeof (m));
Florin Coras60116992018-08-27 09:52:18 -0700384
Florin Coras537b17e2018-09-28 10:35:45 -0700385 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700386 return 0;
387}
388
Florin Coras458089b2019-08-21 16:20:44 -0700389void
390mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
391 u32 context, int rv)
392{
393 svm_msg_q_msg_t _msg, *msg = &_msg;
394 session_unlisten_reply_msg_t *ump;
395 svm_msg_q_t *app_mq;
396 session_event_t *evt;
397
398 app_mq = app_wrk->event_queue;
399 if (mq_try_lock_and_alloc_msg (app_mq, msg))
400 return;
401
402 evt = svm_msg_q_msg_data (app_mq, msg);
403 clib_memset (evt, 0, sizeof (*evt));
404 evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY;
405 ump = (session_unlisten_reply_msg_t *) evt->data;
406 ump->context = context;
407 ump->handle = sh;
408 ump->retval = rv;
409 svm_msg_q_add_and_unlock (app_mq, msg);
410}
411
Florin Coras49568af2019-07-31 16:46:24 -0700412static void
413mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh)
414{
Florin Coras68b7e582020-01-21 18:33:23 -0800415 svm_msg_q_msg_t _msg, *msg = &_msg;
Florin Coras09bf91a2021-02-23 08:44:13 -0800416 session_migrated_msg_t m = { 0 };
Florin Corasb4624182020-12-11 13:58:12 -0800417 fifo_segment_t *eq_seg;
Florin Coras68b7e582020-01-21 18:33:23 -0800418 app_worker_t *app_wrk;
419 session_event_t *evt;
420 svm_msg_q_t *app_mq;
Florin Coras41d5f542021-01-15 13:49:33 -0800421 application_t *app;
Florin Corasb4624182020-12-11 13:58:12 -0800422 u32 thread_index;
423
424 thread_index = session_thread_from_handle (new_sh);
Florin Coras41d5f542021-01-15 13:49:33 -0800425 app_wrk = app_worker_get (s->app_wrk_index);
426 app_mq = app_wrk->event_queue;
427 app = application_get (app_wrk->app_index);
428 eq_seg = application_get_rx_mqs_segment (app);
Florin Coras68b7e582020-01-21 18:33:23 -0800429
Florin Coras09bf91a2021-02-23 08:44:13 -0800430 m.handle = session_handle (s);
431 m.new_handle = new_sh;
432 m.vpp_thread_index = thread_index;
433 m.vpp_evt_q = fifo_segment_msg_q_offset (eq_seg, thread_index);
434 m.segment_handle = SESSION_INVALID_HANDLE;
435
Florin Coras68b7e582020-01-21 18:33:23 -0800436 if (mq_try_lock_and_alloc_msg (app_mq, msg))
437 return;
438
439 evt = svm_msg_q_msg_data (app_mq, msg);
440 clib_memset (evt, 0, sizeof (*evt));
441 evt->event_type = SESSION_CTRL_EVT_MIGRATED;
Florin Coras09bf91a2021-02-23 08:44:13 -0800442 clib_memcpy_fast (evt->data, &m, sizeof (m));
443
Florin Coras68b7e582020-01-21 18:33:23 -0800444 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras49568af2019-07-31 16:46:24 -0700445}
446
Florin Corasc4c4cf52019-08-24 18:17:34 -0700447static int
448mq_send_add_segment_cb (u32 app_wrk_index, u64 segment_handle)
449{
450 int fds[SESSION_N_FD_TYPE], n_fds = 0;
451 svm_msg_q_msg_t _msg, *msg = &_msg;
452 session_app_add_segment_msg_t *mp;
453 vl_api_registration_t *reg;
454 app_worker_t *app_wrk;
455 session_event_t *evt;
456 svm_msg_q_t *app_mq;
457 fifo_segment_t *fs;
458 ssvm_private_t *sp;
459 u8 fd_flags = 0;
460
461 app_wrk = app_worker_get (app_wrk_index);
462
463 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
464 if (!reg)
465 {
466 clib_warning ("no api registration for client: %u",
467 app_wrk->api_client_index);
468 return -1;
469 }
470
471 fs = segment_manager_get_segment_w_handle (segment_handle);
472 sp = &fs->ssvm;
473 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
474 {
475 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
476 {
477 clib_warning ("can't send memfd fd");
478 return -1;
479 }
480
481 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
482 fds[n_fds] = sp->fd;
483 n_fds += 1;
484 }
485
486 app_mq = app_wrk->event_queue;
487 if (mq_try_lock_and_alloc_msg (app_mq, msg))
488 return -1;
489
490 if (n_fds)
491 session_send_fds (reg, fds, n_fds);
492
493 evt = svm_msg_q_msg_data (app_mq, msg);
494 clib_memset (evt, 0, sizeof (*evt));
495 evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT;
496 mp = (session_app_add_segment_msg_t *) evt->data;
497 clib_memset (mp, 0, sizeof (*mp));
498 mp->segment_size = sp->ssvm_size;
499 mp->fd_flags = fd_flags;
500 mp->segment_handle = segment_handle;
501 strncpy ((char *) mp->segment_name, (char *) sp->name,
502 sizeof (mp->segment_name) - 1);
503
504 svm_msg_q_add_and_unlock (app_mq, msg);
505
506 return 0;
507}
508
509static int
510mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle)
511{
512 svm_msg_q_msg_t _msg, *msg = &_msg;
513 session_app_del_segment_msg_t *mp;
514 vl_api_registration_t *reg;
515 app_worker_t *app_wrk;
516 session_event_t *evt;
517 svm_msg_q_t *app_mq;
518
519 app_wrk = app_worker_get (app_wrk_index);
520 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
521 if (!reg)
522 {
523 clib_warning ("no registration: %u", app_wrk->api_client_index);
524 return -1;
525 }
526
527 app_mq = app_wrk->event_queue;
528 if (mq_try_lock_and_alloc_msg (app_mq, msg))
529 return -1;
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_APP_DEL_SEGMENT;
534 mp = (session_app_del_segment_msg_t *) evt->data;
535 clib_memset (mp, 0, sizeof (*mp));
536 mp->segment_handle = segment_handle;
537 svm_msg_q_add_and_unlock (app_mq, msg);
538
539 return 0;
540}
541
Florin Coras9ace36d2019-10-28 13:14:17 -0700542static void
543mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf)
544{
545 svm_msg_q_msg_t _msg, *msg = &_msg;
546 session_cleanup_msg_t *mp;
547 svm_msg_q_t *app_mq;
548 session_event_t *evt;
549 app_worker_t *app_wrk;
550
Florin Coras36d49392020-04-24 23:00:11 +0000551 /* Propagate transport cleanup notifications only if app didn't close */
552 if (ntf == SESSION_CLEANUP_TRANSPORT
553 && s->session_state != SESSION_STATE_TRANSPORT_DELETED)
Florin Coras9ace36d2019-10-28 13:14:17 -0700554 return;
555
556 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
557 if (!app_wrk)
558 return;
559
560 app_mq = app_wrk->event_queue;
561 if (mq_try_lock_and_alloc_msg (app_mq, msg))
562 return;
563
564 evt = svm_msg_q_msg_data (app_mq, msg);
565 clib_memset (evt, 0, sizeof (*evt));
566 evt->event_type = SESSION_CTRL_EVT_CLEANUP;
567 mp = (session_cleanup_msg_t *) evt->data;
568 mp->handle = session_handle (s);
Florin Coras36d49392020-04-24 23:00:11 +0000569 mp->type = ntf;
Florin Coras9ace36d2019-10-28 13:14:17 -0700570 svm_msg_q_add_and_unlock (app_mq, msg);
571}
572
Florin Corasc4c4cf52019-08-24 18:17:34 -0700573static session_cb_vft_t session_mq_cb_vft = {
574 .session_accept_callback = mq_send_session_accepted_cb,
575 .session_disconnect_callback = mq_send_session_disconnected_cb,
576 .session_connected_callback = mq_send_session_connected_cb,
577 .session_reset_callback = mq_send_session_reset_cb,
578 .session_migrate_callback = mq_send_session_migrate_cb,
Florin Coras9ace36d2019-10-28 13:14:17 -0700579 .session_cleanup_callback = mq_send_session_cleanup_cb,
Florin Corasc4c4cf52019-08-24 18:17:34 -0700580 .add_segment_callback = mq_send_add_segment_cb,
581 .del_segment_callback = mq_send_del_segment_cb,
582};
583
Dave Barach68b0fb02017-02-28 15:15:56 -0500584static void
Florin Corase04c2992017-03-01 08:17:34 -0800585vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
586{
587 vl_api_session_enable_disable_reply_t *rmp;
588 vlib_main_t *vm = vlib_get_main ();
589 int rv = 0;
590
591 vnet_session_enable_disable (vm, mp->is_enable);
592 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
593}
594
Dave Barach68b0fb02017-02-28 15:15:56 -0500595static void
Florin Coras458089b2019-08-21 16:20:44 -0700596vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
597{
Florin Coras41d5f542021-01-15 13:49:33 -0800598 int rv = 0, *fds = 0, n_fds = 0, n_workers, i;
599 fifo_segment_t *segp, *rx_mqs_seg = 0;
Florin Coras458089b2019-08-21 16:20:44 -0700600 vnet_app_attach_args_t _a, *a = &_a;
Florin Coras41d5f542021-01-15 13:49:33 -0800601 vl_api_app_attach_reply_t *rmp;
Florin Coras458089b2019-08-21 16:20:44 -0700602 u8 fd_flags = 0, ctrl_thread;
603 vl_api_registration_t *reg;
Florin Coras41d5f542021-01-15 13:49:33 -0800604 svm_msg_q_t *rx_mq;
605 application_t *app;
Florin Coras458089b2019-08-21 16:20:44 -0700606
607 reg = vl_api_client_index_to_registration (mp->client_index);
608 if (!reg)
609 return;
610
Florin Coras41d5f542021-01-15 13:49:33 -0800611 n_workers = vlib_num_workers ();
Florin Coras61ae0562020-09-02 19:10:28 -0700612 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras458089b2019-08-21 16:20:44 -0700613 {
614 rv = VNET_API_ERROR_FEATURE_DISABLED;
615 goto done;
616 }
Florin Coras6c10ab22020-11-08 16:50:39 -0800617 /* Only support binary api with socket transport */
618 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
619 {
620 rv = VNET_API_ERROR_APP_UNSUPPORTED_CFG;
621 goto done;
622 }
Florin Coras458089b2019-08-21 16:20:44 -0700623
624 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
625 sizeof (mp->options),
626 "Out of options, fix api message definition");
627
628 clib_memset (a, 0, sizeof (*a));
629 a->api_client_index = mp->client_index;
630 a->options = mp->options;
631 a->session_cb_vft = &session_mq_cb_vft;
Dave Barach77841402020-04-29 17:04:10 -0400632 a->namespace_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
Florin Coras458089b2019-08-21 16:20:44 -0700633
634 if ((rv = vnet_application_attach (a)))
635 {
636 clib_warning ("attach returned: %d", rv);
637 vec_free (a->namespace_id);
638 goto done;
639 }
640 vec_free (a->namespace_id);
641
Florin Coras41d5f542021-01-15 13:49:33 -0800642 vec_validate (fds, 3 /* segs + tx evtfd */ + n_workers);
643
644 /* Send rx mqs segment */
645 app = application_get (a->app_index);
646 rx_mqs_seg = application_get_rx_mqs_segment (app);
647
648 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
649 fds[n_fds] = rx_mqs_seg->ssvm.fd;
650 n_fds += 1;
651
Florin Coras458089b2019-08-21 16:20:44 -0700652 /* Send fifo segment fd if needed */
653 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
654 {
655 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
656 fds[n_fds] = a->segment->fd;
657 n_fds += 1;
658 }
659 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
660 {
661 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
Florin Coras86f12322021-01-22 15:05:14 -0800662 fds[n_fds] = svm_msg_q_get_eventfd (a->app_evt_q);
Florin Coras458089b2019-08-21 16:20:44 -0700663 n_fds += 1;
664 }
665
Florin Coras41d5f542021-01-15 13:49:33 -0800666 if (application_use_private_rx_mqs ())
667 {
668 fd_flags |= SESSION_FD_F_VPP_MQ_EVENTFD;
669 for (i = 0; i < n_workers + 1; i++)
670 {
671 rx_mq = application_rx_mq_get (app, i);
672 fds[n_fds] = svm_msg_q_get_eventfd (rx_mq);
673 n_fds += 1;
674 }
675 }
676
Florin Coras458089b2019-08-21 16:20:44 -0700677done:
Florin Coras458089b2019-08-21 16:20:44 -0700678 /* *INDENT-OFF* */
679 REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({
680 if (!rv)
681 {
Florin Coras41d5f542021-01-15 13:49:33 -0800682 ctrl_thread = n_workers ? 1 : 0;
Florin Corasb4624182020-12-11 13:58:12 -0800683 segp = (fifo_segment_t *) a->segment;
Florin Coras458089b2019-08-21 16:20:44 -0700684 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Florin Corasb4624182020-12-11 13:58:12 -0800685 rmp->app_mq = fifo_segment_msg_q_offset (segp, 0);
Florin Coras41d5f542021-01-15 13:49:33 -0800686 rmp->vpp_ctrl_mq = fifo_segment_msg_q_offset (rx_mqs_seg, ctrl_thread);
Florin Coras458089b2019-08-21 16:20:44 -0700687 rmp->vpp_ctrl_mq_thread = ctrl_thread;
688 rmp->n_fds = n_fds;
689 rmp->fd_flags = fd_flags;
Florin Corasb4624182020-12-11 13:58:12 -0800690 if (vec_len (segp->ssvm.name))
Florin Coras458089b2019-08-21 16:20:44 -0700691 {
Florin Corasb4624182020-12-11 13:58:12 -0800692 vl_api_vec_to_api_string (segp->ssvm.name, &rmp->segment_name);
Florin Coras458089b2019-08-21 16:20:44 -0700693 }
Florin Corasb4624182020-12-11 13:58:12 -0800694 rmp->segment_size = segp->ssvm.ssvm_size;
Florin Coras458089b2019-08-21 16:20:44 -0700695 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
696 }
697 }));
698 /* *INDENT-ON* */
699
700 if (n_fds)
701 session_send_fds (reg, fds, n_fds);
Florin Coras41d5f542021-01-15 13:49:33 -0800702 vec_free (fds);
Florin Coras458089b2019-08-21 16:20:44 -0700703}
704
Florin Corascea194d2017-10-02 00:18:51 -0700705static void
Florin Coras15531972018-08-12 23:50:53 -0700706vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
707{
708 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
709 vl_api_app_worker_add_del_reply_t *rmp;
710 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -0700711 application_t *app;
712 u8 fd_flags = 0;
713
Florin Coras61ae0562020-09-02 19:10:28 -0700714 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras15531972018-08-12 23:50:53 -0700715 {
716 rv = VNET_API_ERROR_FEATURE_DISABLED;
717 goto done;
718 }
719
720 reg = vl_api_client_index_to_registration (mp->client_index);
721 if (!reg)
722 return;
723
Florin Corasc1f5a432018-11-20 11:31:26 -0800724 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -0700725 if (!app)
726 {
727 rv = VNET_API_ERROR_INVALID_VALUE;
728 goto done;
729 }
730
731 vnet_app_worker_add_del_args_t args = {
732 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -0800733 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -0800734 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -0700735 .is_add = mp->is_add
736 };
Florin Corasc1a42652019-02-08 18:27:29 -0800737 rv = vnet_app_worker_add_del (&args);
738 if (rv)
Florin Coras15531972018-08-12 23:50:53 -0700739 {
Florin Corasc1a42652019-02-08 18:27:29 -0800740 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -0700741 goto done;
742 }
743
Florin Coras14598772018-09-04 19:47:52 -0700744 if (!mp->is_add)
745 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -0700746
Florin Coras15531972018-08-12 23:50:53 -0700747 /* Send fifo segment fd if needed */
748 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
749 {
750 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
751 fds[n_fds] = args.segment->fd;
752 n_fds += 1;
753 }
754 if (application_segment_manager_properties (app)->use_mq_eventfd)
755 {
756 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
Florin Coras86f12322021-01-22 15:05:14 -0800757 fds[n_fds] = svm_msg_q_get_eventfd (args.evt_q);
Florin Coras15531972018-08-12 23:50:53 -0700758 n_fds += 1;
759 }
760
761 /* *INDENT-OFF* */
762done:
763 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
764 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -0800765 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -0800766 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -0700767 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -0700768 {
Florin Corasb4624182020-12-11 13:58:12 -0800769 rmp->app_event_queue_address =
770 fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0);
771 rmp->n_fds = n_fds;
772 rmp->fd_flags = fd_flags;
Florin Coras15531972018-08-12 23:50:53 -0700773 if (vec_len (args.segment->name))
774 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100775 vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name);
Florin Coras15531972018-08-12 23:50:53 -0700776 }
Florin Coras15531972018-08-12 23:50:53 -0700777 }
778 }));
779 /* *INDENT-ON* */
780
781 if (n_fds)
782 session_send_fds (reg, fds, n_fds);
783}
784
785static void
Florin Coras888d9f02020-04-02 23:00:13 +0000786vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
787{
788 vl_api_application_detach_reply_t *rmp;
789 int rv = VNET_API_ERROR_INVALID_VALUE_2;
790 vnet_app_detach_args_t _a, *a = &_a;
791 application_t *app;
792
Florin Coras61ae0562020-09-02 19:10:28 -0700793 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras888d9f02020-04-02 23:00:13 +0000794 {
795 rv = VNET_API_ERROR_FEATURE_DISABLED;
796 goto done;
797 }
798
799 app = application_lookup (mp->client_index);
800 if (app)
801 {
802 a->app_index = app->app_index;
803 a->api_client_index = mp->client_index;
804 rv = vnet_application_detach (a);
805 }
806
807done:
808 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
809}
810
811static void
Florin Corascea194d2017-10-02 00:18:51 -0700812vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
813{
814 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -0800815 u32 appns_index = 0;
816 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700817 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200818 if (session_main_is_enabled () == 0)
Florin Corascea194d2017-10-02 00:18:51 -0700819 {
820 rv = VNET_API_ERROR_FEATURE_DISABLED;
821 goto done;
822 }
823
Dave Barach77841402020-04-29 17:04:10 -0400824 ns_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
Florin Corascea194d2017-10-02 00:18:51 -0700825
Florin Corascea194d2017-10-02 00:18:51 -0700826 vnet_app_namespace_add_del_args_t args = {
827 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -0700828 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -0700829 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
830 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
831 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
832 .is_add = 1
833 };
Florin Corasc1a42652019-02-08 18:27:29 -0800834 rv = vnet_app_namespace_add_del (&args);
835 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -0800836 {
837 appns_index = app_namespace_index_from_id (ns_id);
838 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
839 {
840 clib_warning ("app ns lookup failed");
841 rv = VNET_API_ERROR_UNSPECIFIED;
842 }
843 }
Florin Corascea194d2017-10-02 00:18:51 -0700844 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -0800845
846 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -0700847done:
Florin Coras6e8c6672017-11-10 09:03:54 -0800848 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
849 if (!rv)
850 rmp->appns_index = clib_host_to_net_u32 (appns_index);
851 }));
852 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -0700853}
854
Florin Coras1c710452017-10-17 00:03:13 -0700855static void
856vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
857{
858 vl_api_session_rule_add_del_reply_t *rmp;
859 session_rule_add_del_args_t args;
860 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -0700861 int rv = 0;
862
Dave Barachb7b92992018-10-17 10:38:51 -0400863 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -0700864
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100865 ip_prefix_decode (&mp->lcl, &table_args->lcl);
866 ip_prefix_decode (&mp->rmt, &table_args->rmt);
867
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100868 table_args->lcl_port = mp->lcl_port;
869 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -0700870 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
871 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -0800872 mp->tag[sizeof (mp->tag) - 1] = 0;
873 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700874 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
875 args.scope = mp->scope;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100876 args.transport_proto =
877 api_session_transport_proto_decode (&mp->transport_proto) ==
878 TRANSPORT_PROTO_UDP ? 1 : 0;
Florin Coras1c710452017-10-17 00:03:13 -0700879
Florin Corasc1a42652019-02-08 18:27:29 -0800880 rv = vnet_session_rule_add_del (&args);
881 if (rv)
882 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -0800883 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700884 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
885}
886
Florin Coras6c36f532017-11-03 18:32:34 -0700887static void
888send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -0800889 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800890 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700891{
892 vl_api_session_rules_details_t *rmp = 0;
893 session_mask_or_match_4_t *match =
894 (session_mask_or_match_4_t *) & rule->match;
895 session_mask_or_match_4_t *mask =
896 (session_mask_or_match_4_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100897 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -0700898
899 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400900 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar0046e972021-06-26 22:12:08 +0000901 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SESSION_RULES_DETAILS);
Florin Coras6c36f532017-11-03 18:32:34 -0700902 rmp->context = context;
903
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100904 clib_memset (&lcl, 0, sizeof (lcl));
905 clib_memset (&rmt, 0, sizeof (rmt));
906 ip_set (&lcl.fp_addr, &match->lcl_ip, 1);
907 ip_set (&rmt.fp_addr, &match->rmt_ip, 1);
908 lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip);
909 rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip);
910
911 ip_prefix_encode (&lcl, &rmp->lcl);
912 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100913 rmp->lcl_port = match->lcl_port;
914 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700915 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
916 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100917 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
918 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -0700919 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800920 if (tag)
921 {
Dave Barach178cf492018-11-13 16:34:13 -0500922 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -0800923 rmp->tag[vec_len (tag)] = 0;
924 }
Florin Coras6c36f532017-11-03 18:32:34 -0700925
Florin Coras6c4dae22018-01-09 06:39:23 -0800926 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700927}
928
929static void
Florin Corasc97a7392017-11-05 23:07:07 -0800930send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
931 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800932 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700933{
934 vl_api_session_rules_details_t *rmp = 0;
935 session_mask_or_match_6_t *match =
936 (session_mask_or_match_6_t *) & rule->match;
937 session_mask_or_match_6_t *mask =
938 (session_mask_or_match_6_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100939 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -0700940
941 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400942 clib_memset (rmp, 0, sizeof (*rmp));
Filip Tehlar0046e972021-06-26 22:12:08 +0000943 rmp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_SESSION_RULES_DETAILS);
Florin Coras6c36f532017-11-03 18:32:34 -0700944 rmp->context = context;
945
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100946 clib_memset (&lcl, 0, sizeof (lcl));
947 clib_memset (&rmt, 0, sizeof (rmt));
948 ip_set (&lcl.fp_addr, &match->lcl_ip, 0);
949 ip_set (&rmt.fp_addr, &match->rmt_ip, 0);
950 lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip);
951 rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip);
952
953 ip_prefix_encode (&lcl, &rmp->lcl);
954 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100955 rmp->lcl_port = match->lcl_port;
956 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700957 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800958 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100959 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
960 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -0700961 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800962 if (tag)
963 {
Dave Barach178cf492018-11-13 16:34:13 -0500964 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -0800965 rmp->tag[vec_len (tag)] = 0;
966 }
Florin Coras6c36f532017-11-03 18:32:34 -0700967
Florin Coras6c4dae22018-01-09 06:39:23 -0800968 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700969}
970
971static void
972send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -0800973 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800974 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700975{
976 mma_rule_16_t *rule16;
977 mma_rule_40_t *rule40;
978 mma_rules_table_16_t *srt16;
979 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -0800980 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -0700981
Florin Corasc97a7392017-11-05 23:07:07 -0800982 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -0700983 {
Florin Corasc97a7392017-11-05 23:07:07 -0800984 u8 *tag = 0;
985 /* *INDENT-OFF* */
986 srt16 = &srt->session_rules_tables_16;
Damjan Marionb2c31b62020-12-13 21:47:40 +0100987 pool_foreach (rule16, srt16->rules) {
Florin Corasc97a7392017-11-05 23:07:07 -0800988 ri = mma_rules_table_rule_index_16 (srt16, rule16);
989 tag = session_rules_table_rule_tag (srt, ri, 1);
990 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800991 reg, context);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100992 }
Florin Corasc97a7392017-11-05 23:07:07 -0800993 /* *INDENT-ON* */
994 }
995 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
996 {
997 u8 *tag = 0;
998 /* *INDENT-OFF* */
999 srt40 = &srt->session_rules_tables_40;
Damjan Marionb2c31b62020-12-13 21:47:40 +01001000 pool_foreach (rule40, srt40->rules) {
Florin Corasc97a7392017-11-05 23:07:07 -08001001 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1002 tag = session_rules_table_rule_tag (srt, ri, 1);
1003 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001004 reg, context);
Damjan Marionb2c31b62020-12-13 21:47:40 +01001005 }
Florin Corasc97a7392017-11-05 23:07:07 -08001006 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001007 }
1008}
1009
1010static void
Neale Ranns2b202bc2020-09-21 08:17:51 +00001011vl_api_session_rules_dump_t_handler (vl_api_session_rules_dump_t * mp)
Florin Coras6c36f532017-11-03 18:32:34 -07001012{
Florin Coras6c4dae22018-01-09 06:39:23 -08001013 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001014 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001015 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001016
Florin Coras6c4dae22018-01-09 06:39:23 -08001017 reg = vl_api_client_index_to_registration (mp->client_index);
1018 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001019 return;
1020
1021 /* *INDENT-OFF* */
1022 session_table_foreach (st, ({
Florin Coras07063b82020-03-13 04:44:51 +00001023 for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++)
Florin Corasc97a7392017-11-05 23:07:07 -08001024 {
1025 send_session_rules_table_details (&st->session_rules[tp],
1026 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001027 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001028 mp->context);
1029 }
Florin Coras6c36f532017-11-03 18:32:34 -07001030 }));
1031 /* *INDENT-ON* */
1032}
1033
Florin Coras371ca502018-02-21 12:07:41 -08001034static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001035vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001036{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001037 vl_api_app_add_cert_key_pair_reply_t *rmp;
1038 vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1039 u32 certkey_len, key_len, cert_len;
Florin Coras371ca502018-02-21 12:07:41 -08001040 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001041 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001042 {
1043 rv = VNET_API_ERROR_FEATURE_DISABLED;
1044 goto done;
1045 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001046
Florin Coras371ca502018-02-21 12:07:41 -08001047 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001048 if (cert_len > 10000)
1049 {
1050 rv = VNET_API_ERROR_INVALID_VALUE;
1051 goto done;
1052 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001053
1054 certkey_len = clib_net_to_host_u16 (mp->certkey_len);
1055 if (certkey_len < cert_len)
Florin Coras371ca502018-02-21 12:07:41 -08001056 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001057 rv = VNET_API_ERROR_INVALID_VALUE;
1058 goto done;
Florin Coras371ca502018-02-21 12:07:41 -08001059 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001060
1061 key_len = certkey_len - cert_len;
1062 if (key_len > 10000)
1063 {
1064 rv = VNET_API_ERROR_INVALID_VALUE;
1065 goto done;
1066 }
1067
1068 clib_memset (a, 0, sizeof (*a));
Florin Corasa5a9efd2021-01-05 17:03:29 -08001069 a->cert = mp->certkey;
1070 a->key = mp->certkey + cert_len;
1071 a->cert_len = cert_len;
1072 a->key_len = key_len;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001073 rv = vnet_app_add_cert_key_pair (a);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001074
Florin Coras371ca502018-02-21 12:07:41 -08001075done:
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001076 /* *INDENT-OFF* */
1077 REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
1078 if (!rv)
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001079 rmp->index = clib_host_to_net_u32 (a->index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001080 }));
1081 /* *INDENT-ON* */
Florin Coras371ca502018-02-21 12:07:41 -08001082}
1083
1084static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001085vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001086{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001087 vl_api_app_del_cert_key_pair_reply_t *rmp;
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001088 u32 ckpair_index;
Florin Coras371ca502018-02-21 12:07:41 -08001089 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001090 if (session_main_is_enabled () == 0)
1091 {
1092 rv = VNET_API_ERROR_FEATURE_DISABLED;
1093 goto done;
1094 }
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001095 ckpair_index = clib_net_to_host_u32 (mp->index);
1096 rv = vnet_app_del_cert_key_pair (ckpair_index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001097
1098done:
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001099 REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001100}
1101
Florin Coras6cf30ad2017-04-04 23:08:23 -07001102static clib_error_t *
1103application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001104{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001105 application_t *app = application_lookup (client_index);
1106 vnet_app_detach_args_t _a, *a = &_a;
1107 if (app)
1108 {
Florin Coras15531972018-08-12 23:50:53 -07001109 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001110 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001111 vnet_application_detach (a);
1112 }
1113 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001114}
1115
Florin Coras6cf30ad2017-04-04 23:08:23 -07001116VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001117
Dave Barach68b0fb02017-02-28 15:15:56 -05001118/*
Florin Coras61ae0562020-09-02 19:10:28 -07001119 * Socket api functions
1120 */
1121
1122static void
1123sapi_send_fds (app_worker_t * app_wrk, int *fds, int n_fds)
1124{
1125 app_sapi_msg_t smsg = { 0 };
1126 app_namespace_t *app_ns;
1127 application_t *app;
1128 clib_socket_t *cs;
1129 u32 cs_index;
1130
1131 app = application_get (app_wrk->app_index);
1132 app_ns = app_namespace_get (app->ns_index);
1133 cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index);
1134 cs = appns_sapi_get_socket (app_ns, cs_index);
Florin Coras1c0573d2020-09-23 10:31:27 -07001135 if (PREDICT_FALSE (!cs))
1136 return;
Florin Coras61ae0562020-09-02 19:10:28 -07001137
1138 /* There's no payload for the message only the type */
1139 smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS;
1140 clib_socket_sendmsg (cs, &smsg, sizeof (smsg), fds, n_fds);
1141}
1142
1143static int
1144mq_send_add_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1145{
1146 int fds[SESSION_N_FD_TYPE], n_fds = 0;
1147 svm_msg_q_msg_t _msg, *msg = &_msg;
1148 session_app_add_segment_msg_t *mp;
1149 app_worker_t *app_wrk;
1150 session_event_t *evt;
1151 svm_msg_q_t *app_mq;
1152 fifo_segment_t *fs;
1153 ssvm_private_t *sp;
1154 u8 fd_flags = 0;
1155
1156 app_wrk = app_worker_get (app_wrk_index);
1157
1158 fs = segment_manager_get_segment_w_handle (segment_handle);
1159 sp = &fs->ssvm;
1160 ASSERT (ssvm_type (sp) == SSVM_SEGMENT_MEMFD);
1161
1162 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1163 fds[n_fds] = sp->fd;
1164 n_fds += 1;
1165
1166 app_mq = app_wrk->event_queue;
1167 if (mq_try_lock_and_alloc_msg (app_mq, msg))
1168 return -1;
1169
1170 /*
1171 * Send the fd over api socket
1172 */
1173 sapi_send_fds (app_wrk, fds, n_fds);
1174
1175 /*
1176 * Send the actual message over mq
1177 */
1178 evt = svm_msg_q_msg_data (app_mq, msg);
1179 clib_memset (evt, 0, sizeof (*evt));
1180 evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT;
1181 mp = (session_app_add_segment_msg_t *) evt->data;
1182 clib_memset (mp, 0, sizeof (*mp));
1183 mp->segment_size = sp->ssvm_size;
1184 mp->fd_flags = fd_flags;
1185 mp->segment_handle = segment_handle;
1186 strncpy ((char *) mp->segment_name, (char *) sp->name,
1187 sizeof (mp->segment_name) - 1);
1188
1189 svm_msg_q_add_and_unlock (app_mq, msg);
1190
1191 return 0;
1192}
1193
1194static int
1195mq_send_del_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1196{
1197 svm_msg_q_msg_t _msg, *msg = &_msg;
1198 session_app_del_segment_msg_t *mp;
1199 app_worker_t *app_wrk;
1200 session_event_t *evt;
1201 svm_msg_q_t *app_mq;
1202
1203 app_wrk = app_worker_get (app_wrk_index);
1204
1205 app_mq = app_wrk->event_queue;
1206 if (mq_try_lock_and_alloc_msg (app_mq, msg))
1207 return -1;
1208
1209 evt = svm_msg_q_msg_data (app_mq, msg);
1210 clib_memset (evt, 0, sizeof (*evt));
1211 evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT;
1212 mp = (session_app_del_segment_msg_t *) evt->data;
1213 clib_memset (mp, 0, sizeof (*mp));
1214 mp->segment_handle = segment_handle;
1215 svm_msg_q_add_and_unlock (app_mq, msg);
1216
1217 return 0;
1218}
1219
1220static session_cb_vft_t session_mq_sapi_cb_vft = {
1221 .session_accept_callback = mq_send_session_accepted_cb,
1222 .session_disconnect_callback = mq_send_session_disconnected_cb,
1223 .session_connected_callback = mq_send_session_connected_cb,
1224 .session_reset_callback = mq_send_session_reset_cb,
1225 .session_migrate_callback = mq_send_session_migrate_cb,
1226 .session_cleanup_callback = mq_send_session_cleanup_cb,
1227 .add_segment_callback = mq_send_add_segment_sapi_cb,
1228 .del_segment_callback = mq_send_del_segment_sapi_cb,
1229};
1230
1231static void
1232session_api_attach_handler (app_namespace_t * app_ns, clib_socket_t * cs,
1233 app_sapi_attach_msg_t * mp)
1234{
Florin Coras41d5f542021-01-15 13:49:33 -08001235 int rv = 0, *fds = 0, n_fds = 0, i, n_workers;
Florin Coras61ae0562020-09-02 19:10:28 -07001236 vnet_app_attach_args_t _a, *a = &_a;
1237 app_sapi_attach_reply_msg_t *rmp;
Florin Coras61ae0562020-09-02 19:10:28 -07001238 u8 fd_flags = 0, ctrl_thread;
1239 app_ns_api_handle_t *handle;
Florin Coras41d5f542021-01-15 13:49:33 -08001240 fifo_segment_t *rx_mqs_seg;
Florin Coras61ae0562020-09-02 19:10:28 -07001241 app_sapi_msg_t msg = { 0 };
1242 app_worker_t *app_wrk;
Florin Coras61ae0562020-09-02 19:10:28 -07001243 application_t *app;
Florin Coras41d5f542021-01-15 13:49:33 -08001244 svm_msg_q_t *rx_mq;
Florin Coras61ae0562020-09-02 19:10:28 -07001245
1246 /* Make sure name is null terminated */
1247 mp->name[63] = 0;
1248
1249 clib_memset (a, 0, sizeof (*a));
1250 a->api_client_index = appns_sapi_socket_handle (app_ns, cs);
1251 a->name = format (0, "%s", (char *) mp->name);
1252 a->options = mp->options;
1253 a->session_cb_vft = &session_mq_sapi_cb_vft;
1254 a->use_sock_api = 1;
1255 a->options[APP_OPTIONS_NAMESPACE] = app_namespace_index (app_ns);
1256
1257 if ((rv = vnet_application_attach (a)))
1258 {
1259 clib_warning ("attach returned: %d", rv);
1260 goto done;
1261 }
1262
Florin Coras41d5f542021-01-15 13:49:33 -08001263 n_workers = vlib_num_workers ();
1264 vec_validate (fds, 3 /* segs + tx evtfd */ + n_workers);
1265
Florin Coras61ae0562020-09-02 19:10:28 -07001266 /* Send event queues segment */
Florin Coras41d5f542021-01-15 13:49:33 -08001267 app = application_get (a->app_index);
1268 rx_mqs_seg = application_get_rx_mqs_segment (app);
1269
1270 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
1271 fds[n_fds] = rx_mqs_seg->ssvm.fd;
1272 n_fds += 1;
1273
Florin Coras61ae0562020-09-02 19:10:28 -07001274 /* Send fifo segment fd if needed */
1275 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
1276 {
1277 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1278 fds[n_fds] = a->segment->fd;
1279 n_fds += 1;
1280 }
1281 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
1282 {
1283 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
Florin Coras86f12322021-01-22 15:05:14 -08001284 fds[n_fds] = svm_msg_q_get_eventfd (a->app_evt_q);
Florin Coras61ae0562020-09-02 19:10:28 -07001285 n_fds += 1;
1286 }
1287
Florin Coras41d5f542021-01-15 13:49:33 -08001288 if (application_use_private_rx_mqs ())
1289 {
1290 fd_flags |= SESSION_FD_F_VPP_MQ_EVENTFD;
1291 for (i = 0; i < n_workers + 1; i++)
1292 {
1293 rx_mq = application_rx_mq_get (app, i);
1294 fds[n_fds] = svm_msg_q_get_eventfd (rx_mq);
1295 n_fds += 1;
1296 }
1297 }
1298
Florin Coras61ae0562020-09-02 19:10:28 -07001299done:
1300
1301 msg.type = APP_SAPI_MSG_TYPE_ATTACH_REPLY;
1302 rmp = &msg.attach_reply;
1303 rmp->retval = rv;
1304 if (!rv)
1305 {
Florin Coras41d5f542021-01-15 13:49:33 -08001306 ctrl_thread = n_workers ? 1 : 0;
Florin Coras61ae0562020-09-02 19:10:28 -07001307 rmp->app_index = a->app_index;
Florin Corasb4624182020-12-11 13:58:12 -08001308 rmp->app_mq =
1309 fifo_segment_msg_q_offset ((fifo_segment_t *) a->segment, 0);
Florin Coras41d5f542021-01-15 13:49:33 -08001310 rmp->vpp_ctrl_mq = fifo_segment_msg_q_offset (rx_mqs_seg, ctrl_thread);
Florin Coras61ae0562020-09-02 19:10:28 -07001311 rmp->vpp_ctrl_mq_thread = ctrl_thread;
1312 rmp->n_fds = n_fds;
1313 rmp->fd_flags = fd_flags;
1314 /* No segment name and size since we only support memfds
1315 * in this configuration */
1316 rmp->segment_handle = a->segment_handle;
1317 rmp->api_client_handle = a->api_client_index;
1318
1319 /* Update app index for socket */
1320 handle = (app_ns_api_handle_t *) & cs->private_data;
Florin Coras61ae0562020-09-02 19:10:28 -07001321 app_wrk = application_get_worker (app, 0);
1322 handle->aah_app_wrk_index = app_wrk->wrk_index;
1323 }
1324
1325 clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1326 vec_free (a->name);
Florin Coras41d5f542021-01-15 13:49:33 -08001327 vec_free (fds);
Florin Coras61ae0562020-09-02 19:10:28 -07001328}
1329
1330static void
1331sapi_socket_close_w_handle (u32 api_handle)
1332{
1333 app_namespace_t *app_ns = app_namespace_get (api_handle >> 16);
1334 u16 sock_index = api_handle & 0xffff;
1335 app_ns_api_handle_t *handle;
1336 clib_socket_t *cs;
1337 clib_file_t *cf;
1338
1339 cs = appns_sapi_get_socket (app_ns, sock_index);
1340 if (!cs)
1341 return;
1342
1343 handle = (app_ns_api_handle_t *) & cs->private_data;
1344 cf = clib_file_get (&file_main, handle->aah_file_index);
1345 clib_file_del (&file_main, cf);
1346
1347 clib_socket_close (cs);
1348 appns_sapi_free_socket (app_ns, cs);
1349}
1350
1351static void
1352sapi_add_del_worker_handler (app_namespace_t * app_ns,
1353 clib_socket_t * cs,
1354 app_sapi_worker_add_del_msg_t * mp)
1355{
1356 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1357 app_sapi_worker_add_del_reply_msg_t *rmp;
1358 app_ns_api_handle_t *handle;
1359 app_sapi_msg_t msg = { 0 };
1360 app_worker_t *app_wrk;
Florin Corascc7c88e2020-09-15 15:56:51 -07001361 u32 sapi_handle = -1;
Florin Coras61ae0562020-09-02 19:10:28 -07001362 application_t *app;
Florin Coras61ae0562020-09-02 19:10:28 -07001363 u8 fd_flags = 0;
1364
1365 app = application_get_if_valid (mp->app_index);
1366 if (!app)
1367 {
1368 rv = VNET_API_ERROR_INVALID_VALUE;
1369 goto done;
1370 }
1371
1372 sapi_handle = appns_sapi_socket_handle (app_ns, cs);
1373
1374 vnet_app_worker_add_del_args_t args = {
1375 .app_index = app->app_index,
1376 .wrk_map_index = mp->wrk_index,
1377 .api_client_index = sapi_handle,
1378 .is_add = mp->is_add
1379 };
1380 rv = vnet_app_worker_add_del (&args);
1381 if (rv)
1382 {
1383 clib_warning ("app worker add/del returned: %d", rv);
1384 goto done;
1385 }
1386
1387 if (!mp->is_add)
1388 {
1389 sapi_socket_close_w_handle (sapi_handle);
1390 goto done;
1391 }
1392
1393 /* Send fifo segment fd if needed */
1394 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1395 {
1396 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1397 fds[n_fds] = args.segment->fd;
1398 n_fds += 1;
1399 }
1400 if (application_segment_manager_properties (app)->use_mq_eventfd)
1401 {
1402 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
Florin Coras86f12322021-01-22 15:05:14 -08001403 fds[n_fds] = svm_msg_q_get_eventfd (args.evt_q);
Florin Coras61ae0562020-09-02 19:10:28 -07001404 n_fds += 1;
1405 }
1406
1407done:
1408
1409 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY;
1410 rmp = &msg.worker_add_del_reply;
1411 rmp->retval = rv;
1412 rmp->is_add = mp->is_add;
Florin Corascc7c88e2020-09-15 15:56:51 -07001413 rmp->api_client_handle = sapi_handle;
Florin Coras61ae0562020-09-02 19:10:28 -07001414 rmp->wrk_index = args.wrk_map_index;
1415 rmp->segment_handle = args.segment_handle;
1416 if (!rv && mp->is_add)
1417 {
1418 /* No segment name and size. This supports only memfds */
Florin Corasb4624182020-12-11 13:58:12 -08001419 rmp->app_event_queue_address =
1420 fifo_segment_msg_q_offset ((fifo_segment_t *) args.segment, 0);
Florin Coras61ae0562020-09-02 19:10:28 -07001421 rmp->n_fds = n_fds;
1422 rmp->fd_flags = fd_flags;
1423
1424 /* Update app index for socket */
1425 handle = (app_ns_api_handle_t *) & cs->private_data;
1426 app_wrk = application_get_worker (app, args.wrk_map_index);
1427 handle->aah_app_wrk_index = app_wrk->wrk_index;
1428 }
1429
1430 clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1431}
1432
1433static void
1434sapi_socket_detach (app_namespace_t * app_ns, clib_socket_t * cs)
1435{
Florin Coras61ae0562020-09-02 19:10:28 -07001436 app_ns_api_handle_t *handle;
1437 app_worker_t *app_wrk;
1438 u32 api_client_handle;
1439
1440 api_client_handle = appns_sapi_socket_handle (app_ns, cs);
1441 sapi_socket_close_w_handle (api_client_handle);
1442
Florin Corasf99a7d62020-09-14 10:29:29 -07001443 /* Cleanup everything because app worker closed socket or crashed */
Florin Coras61ae0562020-09-02 19:10:28 -07001444 handle = (app_ns_api_handle_t *) & cs->private_data;
1445 app_wrk = app_worker_get (handle->aah_app_wrk_index);
Florin Corasf99a7d62020-09-14 10:29:29 -07001446
1447 vnet_app_worker_add_del_args_t args = {
1448 .app_index = app_wrk->app_index,
1449 .wrk_map_index = app_wrk->wrk_map_index,
1450 .api_client_index = api_client_handle,
1451 .is_add = 0
1452 };
1453 /* Send rpc to main thread for worker barrier */
1454 vlib_rpc_call_main_thread (vnet_app_worker_add_del, (u8 *) & args,
1455 sizeof (args));
Florin Coras61ae0562020-09-02 19:10:28 -07001456}
1457
1458static clib_error_t *
1459sapi_sock_read_ready (clib_file_t * cf)
1460{
1461 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
Florin Coras7360e3d2020-09-18 16:15:47 -07001462 vlib_main_t *vm = vlib_get_main ();
Florin Coras61ae0562020-09-02 19:10:28 -07001463 app_sapi_msg_t msg = { 0 };
1464 app_namespace_t *app_ns;
1465 clib_error_t *err = 0;
1466 clib_socket_t *cs;
1467
1468 app_ns = app_namespace_get (handle->aah_app_ns_index);
1469 cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
1470 if (!cs)
1471 goto error;
1472
1473 err = clib_socket_recvmsg (cs, &msg, sizeof (msg), 0, 0);
1474 if (err)
1475 {
1476 clib_error_free (err);
1477 sapi_socket_detach (app_ns, cs);
1478 goto error;
1479 }
1480
1481 handle = (app_ns_api_handle_t *) & cs->private_data;
1482
Florin Coras7360e3d2020-09-18 16:15:47 -07001483 vlib_worker_thread_barrier_sync (vm);
1484
Florin Coras61ae0562020-09-02 19:10:28 -07001485 switch (msg.type)
1486 {
1487 case APP_SAPI_MSG_TYPE_ATTACH:
1488 session_api_attach_handler (app_ns, cs, &msg.attach);
1489 break;
1490 case APP_SAPI_MSG_TYPE_ADD_DEL_WORKER:
1491 sapi_add_del_worker_handler (app_ns, cs, &msg.worker_add_del);
1492 break;
1493 default:
1494 clib_warning ("app wrk %u unknown message type: %u",
1495 handle->aah_app_wrk_index, msg.type);
1496 break;
1497 }
1498
Florin Coras7360e3d2020-09-18 16:15:47 -07001499 vlib_worker_thread_barrier_release (vm);
1500
Florin Coras61ae0562020-09-02 19:10:28 -07001501error:
1502 return 0;
1503}
1504
1505static clib_error_t *
1506sapi_sock_write_ready (clib_file_t * cf)
1507{
1508 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1509 clib_warning ("called for app ns %u", handle->aah_app_ns_index);
1510 return 0;
1511}
1512
1513static clib_error_t *
1514sapi_sock_error (clib_file_t * cf)
1515{
1516 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1517 app_namespace_t *app_ns;
1518 clib_socket_t *cs;
1519
1520 app_ns = app_namespace_get (handle->aah_app_ns_index);
1521 cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
1522 if (!cs)
1523 return 0;
1524
1525 sapi_socket_detach (app_ns, cs);
1526 return 0;
1527}
1528
1529static clib_error_t *
1530sapi_sock_accept_ready (clib_file_t * scf)
1531{
1532 app_ns_api_handle_t handle = *(app_ns_api_handle_t *) & scf->private_data;
1533 app_namespace_t *app_ns;
1534 clib_file_t cf = { 0 };
1535 clib_error_t *err = 0;
1536 clib_socket_t *ccs, *scs;
1537
1538 /* Listener files point to namespace */
1539 app_ns = app_namespace_get (handle.aah_app_ns_index);
1540
1541 /*
1542 * Initialize client socket
1543 */
1544 ccs = appns_sapi_alloc_socket (app_ns);
1545
1546 /* Grab server socket after client is initialized */
1547 scs = appns_sapi_get_socket (app_ns, handle.aah_sock_index);
1548 if (!scs)
1549 goto error;
1550
1551 err = clib_socket_accept (scs, ccs);
1552 if (err)
1553 {
1554 clib_error_report (err);
1555 goto error;
1556 }
1557
1558 cf.read_function = sapi_sock_read_ready;
1559 cf.write_function = sapi_sock_write_ready;
1560 cf.error_function = sapi_sock_error;
1561 cf.file_descriptor = ccs->fd;
1562 /* File points to app namespace and socket */
1563 handle.aah_sock_index = appns_sapi_socket_index (app_ns, ccs);
Florin Coras6b6c10b2020-09-24 11:58:28 -07001564 cf.private_data = handle.as_u64;
Florin Coras61ae0562020-09-02 19:10:28 -07001565 cf.description = format (0, "app sock conn fd: %d", ccs->fd);
1566
1567 /* Poll until we get an attach message. Socket points to file and
1568 * application that owns the socket */
1569 handle.aah_app_wrk_index = APP_INVALID_INDEX;
1570 handle.aah_file_index = clib_file_add (&file_main, &cf);
Florin Coras6b6c10b2020-09-24 11:58:28 -07001571 ccs->private_data = handle.as_u64;
Florin Coras61ae0562020-09-02 19:10:28 -07001572
1573 return err;
1574
1575error:
1576 appns_sapi_free_socket (app_ns, ccs);
1577 return err;
1578}
1579
1580int
1581appns_sapi_add_ns_socket (app_namespace_t * app_ns)
1582{
1583 char *subdir = "/app_ns_sockets/";
1584 app_ns_api_handle_t *handle;
1585 clib_file_t cf = { 0 };
1586 struct stat file_stat;
1587 clib_error_t *err;
1588 clib_socket_t *cs;
1589 u8 *dir = 0;
1590 int rv = 0;
1591
1592 vec_add (dir, vlib_unix_get_runtime_dir (),
1593 strlen (vlib_unix_get_runtime_dir ()));
1594 vec_add (dir, (u8 *) subdir, strlen (subdir));
1595
1596 err = vlib_unix_recursive_mkdir ((char *) dir);
1597 if (err)
1598 {
1599 clib_error_report (err);
1600 rv = -1;
1601 goto error;
1602 }
1603
1604 app_ns->sock_name = format (0, "%v%v%c", dir, app_ns->ns_id, 0);
1605
1606 /*
1607 * Create and initialize socket to listen on
1608 */
1609 cs = appns_sapi_alloc_socket (app_ns);
1610 cs->config = (char *) app_ns->sock_name;
1611 cs->flags = CLIB_SOCKET_F_IS_SERVER |
1612 CLIB_SOCKET_F_ALLOW_GROUP_WRITE |
1613 CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED;
1614
1615 if ((err = clib_socket_init (cs)))
1616 {
1617 clib_error_report (err);
1618 rv = -1;
1619 goto error;
1620 }
1621
1622 if (stat ((char *) app_ns->sock_name, &file_stat) == -1)
1623 {
1624 rv = -1;
1625 goto error;
1626 }
1627
1628 /*
1629 * Start polling it
1630 */
1631 cf.read_function = sapi_sock_accept_ready;
1632 cf.file_descriptor = cs->fd;
1633 /* File points to namespace */
1634 handle = (app_ns_api_handle_t *) & cf.private_data;
1635 handle->aah_app_ns_index = app_namespace_index (app_ns);
1636 handle->aah_sock_index = appns_sapi_socket_index (app_ns, cs);
1637 cf.description = format (0, "app sock listener: %s", app_ns->sock_name);
1638
1639 /* Socket points to clib file index */
1640 handle = (app_ns_api_handle_t *) & cs->private_data;
1641 handle->aah_file_index = clib_file_add (&file_main, &cf);
1642 handle->aah_app_wrk_index = APP_INVALID_INDEX;
1643
1644error:
1645 vec_free (dir);
1646 return rv;
1647}
1648
Filip Tehlar0046e972021-06-26 22:12:08 +00001649static void
1650vl_api_application_tls_cert_add_t_handler (
1651 vl_api_application_tls_cert_add_t *mp)
1652{
1653 /* deprecated */
1654}
1655
1656static void
1657vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *mp)
1658{
1659 /* deprecated */
1660}
1661
1662#include <vnet/session/session.api.c>
1663static clib_error_t *
1664session_api_hookup (vlib_main_t *vm)
1665{
1666 /*
1667 * Set up the (msg_name, crc, message-id) table
1668 */
1669 REPLY_MSG_ID_BASE = setup_message_id_table ();
1670
1671 return 0;
1672}
1673
1674VLIB_API_INIT_FUNCTION (session_api_hookup);
1675
Florin Coras61ae0562020-09-02 19:10:28 -07001676/*
Dave Barach68b0fb02017-02-28 15:15:56 -05001677 * fd.io coding-style-patch-verification: ON
1678 *
1679 * Local Variables:
1680 * eval: (c-set-style "gnu")
1681 * End:
1682 */