blob: 988d3e26138a40b88599da6003b65e1de7c7e5a1 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2015-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/vnet.h>
17#include <vlibmemory/api.h>
18#include <vnet/session/application.h>
Florin Coras1c710452017-10-17 00:03:13 -070019#include <vnet/session/application_interface.h>
Florin Corasba7d8f52019-02-22 13:11:38 -080020#include <vnet/session/application_local.h>
Florin Coras1c710452017-10-17 00:03:13 -070021#include <vnet/session/session_rules_table.h>
Florin Coras6c36f532017-11-03 18:32:34 -070022#include <vnet/session/session_table.h>
Florin Corasc9940fc2019-02-05 20:55:11 -080023#include <vnet/session/session.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010025#include <vnet/ip/ip_types_api.h>
26
Dave Barach68b0fb02017-02-28 15:15:56 -050027#include <vnet/vnet_msg_enum.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050028
29#define vl_typedefs /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_typedefs
32
33#define vl_endianfun /* define message structures */
34#include <vnet/vnet_all_api_h.h>
35#undef vl_endianfun
36
37/* instantiate all the print functions we know about */
38#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39#define vl_printfun
40#include <vnet/vnet_all_api_h.h>
41#undef vl_printfun
42
43#include <vlibapi/api_helper_macros.h>
44
45#define foreach_session_api_msg \
Florin Coras458089b2019-08-21 16:20:44 -070046_(APP_ATTACH, app_attach) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070047_(APPLICATION_DETACH, application_detach) \
Florin Corase04c2992017-03-01 08:17:34 -080048_(SESSION_ENABLE_DISABLE, session_enable_disable) \
Florin Corascea194d2017-10-02 00:18:51 -070049_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
Florin Coras1c710452017-10-17 00:03:13 -070050_(SESSION_RULE_ADD_DEL, session_rule_add_del) \
Florin Coras6c36f532017-11-03 18:32:34 -070051_(SESSION_RULES_DUMP, session_rules_dump) \
Florin Coras371ca502018-02-21 12:07:41 -080052_(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \
53_(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \
Nathan Skrzypczak79f89532019-09-13 11:08:13 +020054_(APP_ADD_CERT_KEY_PAIR, app_add_cert_key_pair) \
55_(APP_DEL_CERT_KEY_PAIR, app_del_cert_key_pair) \
Florin Coras15531972018-08-12 23:50:53 -070056_(APP_WORKER_ADD_DEL, app_worker_add_del) \
Florin Corase04c2992017-03-01 08:17:34 -080057
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010058static transport_proto_t
59api_session_transport_proto_decode (const vl_api_transport_proto_t * api_tp)
60{
61 switch (*api_tp)
62 {
63 case TRANSPORT_PROTO_API_TCP:
64 return TRANSPORT_PROTO_TCP;
65 case TRANSPORT_PROTO_API_UDP:
66 return TRANSPORT_PROTO_UDP;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010067 case TRANSPORT_PROTO_API_TLS:
68 return TRANSPORT_PROTO_TLS;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010069 case TRANSPORT_PROTO_API_QUIC:
70 return TRANSPORT_PROTO_QUIC;
71 default:
72 return TRANSPORT_PROTO_NONE;
73 }
74}
75
76static vl_api_transport_proto_t
77api_session_transport_proto_encode (const transport_proto_t tp)
78{
79 switch (tp)
80 {
81 case TRANSPORT_PROTO_TCP:
82 return TRANSPORT_PROTO_API_TCP;
83 case TRANSPORT_PROTO_UDP:
84 return TRANSPORT_PROTO_API_UDP;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010085 case TRANSPORT_PROTO_TLS:
86 return TRANSPORT_PROTO_API_TLS;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010087 case TRANSPORT_PROTO_QUIC:
88 return TRANSPORT_PROTO_API_QUIC;
89 default:
90 return TRANSPORT_PROTO_API_NONE;
91 }
92}
93
Dave Barach68b0fb02017-02-28 15:15:56 -050094static int
Florin Coras99368312018-08-02 10:45:44 -070095session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
Florin Corasb384b542018-01-15 01:08:33 -080096{
97 clib_error_t *error;
98 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
Florin Coras00e01d32019-10-21 16:07:46 -070099 return SESSION_E_BAPI_NO_FD;
Florin Coras99368312018-08-02 10:45:44 -0700100 error = vl_api_send_fd_msg (reg, fds, n_fds);
Florin Corasb384b542018-01-15 01:08:33 -0800101 if (error)
102 {
103 clib_error_report (error);
Florin Coras00e01d32019-10-21 16:07:46 -0700104 return SESSION_E_BAPI_SEND_FD;
Florin Corasb384b542018-01-15 01:08:33 -0800105 }
106 return 0;
107}
108
Florin Coras99368312018-08-02 10:45:44 -0700109static int
Florin Coras83ea6692018-10-12 16:55:14 -0700110mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
111{
112 int rv;
113 u8 try = 0;
114 while (try < 100)
115 {
116 rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
117 SESSION_MQ_CTRL_EVT_RING,
118 SVM_Q_NOWAIT, msg);
119 if (!rv)
120 return 0;
121 try++;
Florin Corase2ea1932018-12-17 23:08:14 -0800122 usleep (1);
Florin Coras83ea6692018-10-12 16:55:14 -0700123 }
Florin Corasdc2e2512018-12-03 17:47:26 -0800124 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700125 return -1;
126}
127
128static int
Florin Coras288eaab2019-02-03 15:26:14 -0800129mq_send_session_accepted_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700130{
Florin Coras15531972018-08-12 23:50:53 -0700131 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700132 svm_msg_q_msg_t _msg, *msg = &_msg;
133 svm_msg_q_t *vpp_queue, *app_mq;
Florin Coras288eaab2019-02-03 15:26:14 -0800134 session_t *listener;
Florin Coras52207f12018-07-12 14:48:06 -0700135 session_accepted_msg_t *mp;
136 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700137 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700138
Florin Coras15531972018-08-12 23:50:53 -0700139 app = application_get (app_wrk->app_index);
140 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700141 if (mq_try_lock_and_alloc_msg (app_mq, msg))
Florin Coras00e01d32019-10-21 16:07:46 -0700142 return SESSION_E_MQ_MSG_ALLOC;
Florin Coras52207f12018-07-12 14:48:06 -0700143
144 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400145 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700146 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
147 mp = (session_accepted_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800148 clib_memset (mp, 0, sizeof (*mp));
Florin Corasab2f6db2018-08-31 14:31:41 -0700149 mp->context = app->app_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800150 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
151 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800152 mp->segment_handle = session_segment_handle (s);
Nathan Skrzypczakc00f4802019-12-03 16:25:11 +0100153 mp->flags = s->flags;
Florin Coras52207f12018-07-12 14:48:06 -0700154
155 if (session_has_transport (s))
156 {
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200157 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras87d66332019-06-11 12:31:31 -0700158 mp->listener_handle = app_listen_session_handle (listener);
Florin Coras52207f12018-07-12 14:48:06 -0700159 if (application_is_proxy (app))
160 {
161 listener =
Florin Coras15531972018-08-12 23:50:53 -0700162 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
163 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700164 if (listener)
165 mp->listener_handle = listen_session_get_handle (listener);
166 }
Florin Coras31c99552019-03-01 13:00:58 -0800167 vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700168 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
169 mp->handle = session_handle (s);
Aloys Augustincdb71702019-04-08 17:54:39 +0200170
Florin Coras09d18c22019-04-24 11:10:02 -0700171 session_get_endpoint (s, &mp->rmt, 0 /* is_lcl */ );
Florin Coras52207f12018-07-12 14:48:06 -0700172 }
173 else
174 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800175 ct_connection_t *ct;
Florin Coras99368312018-08-02 10:45:44 -0700176
Florin Coras2b81e3c2019-02-27 07:55:46 -0800177 ct = (ct_connection_t *) session_get_transport (s);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200178 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras87d66332019-06-11 12:31:31 -0700179 mp->listener_handle = app_listen_session_handle (listener);
Florin Coras09d18c22019-04-24 11:10:02 -0700180 mp->rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
181 mp->rmt.port = ct->c_rmt_port;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800182 mp->handle = session_handle (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800183 vpp_queue = session_main_get_vpp_event_queue (0);
Florin Coras54693d22018-07-17 10:46:29 -0700184 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Coras52207f12018-07-12 14:48:06 -0700185 }
Florin Coras537b17e2018-09-28 10:35:45 -0700186 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700187
188 return 0;
189}
190
Florin Coras72b04282019-01-14 17:23:11 -0800191static inline void
192mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
193 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700194{
Florin Coras52207f12018-07-12 14:48:06 -0700195 svm_msg_q_msg_t _msg, *msg = &_msg;
196 session_disconnected_msg_t *mp;
197 svm_msg_q_t *app_mq;
198 session_event_t *evt;
199
Florin Coras15531972018-08-12 23:50:53 -0700200 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700201 if (mq_try_lock_and_alloc_msg (app_mq, msg))
202 return;
Florin Coras52207f12018-07-12 14:48:06 -0700203 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400204 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800205 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700206 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800207 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800208 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700209 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700210}
211
Florin Coras72b04282019-01-14 17:23:11 -0800212static inline void
213mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
214 svm_fifo_t * f, session_evt_type_t evt_type)
215{
216 app_worker_t *app_wrk;
217 application_t *app;
218 int i;
219
220 app = application_get (app_index);
221 if (!app)
222 return;
223
224 for (i = 0; i < f->n_subscribers; i++)
225 {
226 if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
227 continue;
228 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
229 }
230}
231
232static void
Florin Coras288eaab2019-02-03 15:26:14 -0800233mq_send_session_disconnected_cb (session_t * s)
Florin Coras72b04282019-01-14 17:23:11 -0800234{
235 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
236 session_handle_t sh = session_handle (s);
237
238 mq_send_session_close_evt (app_wrk, session_handle (s),
239 SESSION_CTRL_EVT_DISCONNECTED);
240
Florin Coras288eaab2019-02-03 15:26:14 -0800241 if (svm_fifo_n_subscribers (s->rx_fifo))
242 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800243 SESSION_CTRL_EVT_DISCONNECTED);
244}
245
Florin Coras52207f12018-07-12 14:48:06 -0700246static void
Florin Coras288eaab2019-02-03 15:26:14 -0800247mq_send_session_reset_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700248{
Florin Coras72b04282019-01-14 17:23:11 -0800249 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
250 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700251
Florin Coras72b04282019-01-14 17:23:11 -0800252 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
253
Florin Coras288eaab2019-02-03 15:26:14 -0800254 if (svm_fifo_n_subscribers (s->rx_fifo))
255 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800256 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700257}
258
Florin Coras458089b2019-08-21 16:20:44 -0700259int
Florin Coras15531972018-08-12 23:50:53 -0700260mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras00e01d32019-10-21 16:07:46 -0700261 session_t * s, session_error_t err)
Florin Coras52207f12018-07-12 14:48:06 -0700262{
263 svm_msg_q_msg_t _msg, *msg = &_msg;
264 session_connected_msg_t *mp;
265 svm_msg_q_t *vpp_mq, *app_mq;
266 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700267 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700268 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700269
Florin Coras15531972018-08-12 23:50:53 -0700270 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700271 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700272 if (!app_mq)
273 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800274 clib_warning ("app %u with api index: %u not attached",
275 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700276 return -1;
277 }
278
Florin Coras83ea6692018-10-12 16:55:14 -0700279 if (mq_try_lock_and_alloc_msg (app_mq, msg))
Florin Coras00e01d32019-10-21 16:07:46 -0700280 return SESSION_E_MQ_MSG_ALLOC;
Florin Corasdc2e2512018-12-03 17:47:26 -0800281
Florin Coras52207f12018-07-12 14:48:06 -0700282 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400283 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700284 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
285 mp = (session_connected_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800286 clib_memset (mp, 0, sizeof (*mp));
Florin Coras52207f12018-07-12 14:48:06 -0700287 mp->context = api_context;
288
Florin Coras00e01d32019-10-21 16:07:46 -0700289 if (err)
Florin Coras52207f12018-07-12 14:48:06 -0700290 goto done;
291
292 if (session_has_transport (s))
293 {
294 tc = session_get_transport (s);
295 if (!tc)
296 {
Florin Coras00e01d32019-10-21 16:07:46 -0700297 clib_warning ("failed to retrieve transport!");
298 err = SESSION_E_REFUSED;
Florin Coras52207f12018-07-12 14:48:06 -0700299 goto done;
300 }
301
Florin Coras31c99552019-03-01 13:00:58 -0800302 vpp_mq = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700303 mp->handle = session_handle (s);
304 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Aloys Augustincdb71702019-04-08 17:54:39 +0200305
Florin Coras09d18c22019-04-24 11:10:02 -0700306 session_get_endpoint (s, &mp->lcl, 1 /* is_lcl */ );
Aloys Augustincdb71702019-04-08 17:54:39 +0200307
Florin Coras288eaab2019-02-03 15:26:14 -0800308 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
309 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800310 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700311 }
312 else
313 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800314 ct_connection_t *cct;
315 session_t *ss;
Florin Coras99368312018-08-02 10:45:44 -0700316
Florin Coras2b81e3c2019-02-27 07:55:46 -0800317 cct = (ct_connection_t *) session_get_transport (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800318 mp->handle = session_handle (s);
Florin Coras09d18c22019-04-24 11:10:02 -0700319 mp->lcl.port = cct->c_lcl_port;
320 mp->lcl.is_ip4 = cct->c_is_ip4;
Florin Coras653e43f2019-03-04 10:56:23 -0800321 vpp_mq = session_main_get_vpp_event_queue (0);
Florin Coras54693d22018-07-17 10:46:29 -0700322 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras653e43f2019-03-04 10:56:23 -0800323 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
324 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
325 mp->segment_handle = session_segment_handle (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800326 ss = ct_session_get_peer (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800327 mp->ct_rx_fifo = pointer_to_uword (ss->tx_fifo);
328 mp->ct_tx_fifo = pointer_to_uword (ss->rx_fifo);
329 mp->ct_segment_handle = session_segment_handle (ss);
Florin Coras52207f12018-07-12 14:48:06 -0700330 }
331
332done:
Florin Coras00e01d32019-10-21 16:07:46 -0700333 mp->retval = err;
Florin Coras52207f12018-07-12 14:48:06 -0700334
Florin Coras537b17e2018-09-28 10:35:45 -0700335 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700336 return 0;
337}
338
Florin Coras458089b2019-08-21 16:20:44 -0700339int
Florin Coras60116992018-08-27 09:52:18 -0700340mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
341 session_handle_t handle, int rv)
342{
343 svm_msg_q_msg_t _msg, *msg = &_msg;
344 svm_msg_q_t *app_mq, *vpp_evt_q;
Yu Ping0b819152019-05-07 02:24:30 +0800345 transport_endpoint_t tep;
Florin Coras60116992018-08-27 09:52:18 -0700346 session_bound_msg_t *mp;
347 app_worker_t *app_wrk;
348 session_event_t *evt;
Florin Corasc9940fc2019-02-05 20:55:11 -0800349 app_listener_t *al;
350 session_t *ls = 0;
Florin Coras60116992018-08-27 09:52:18 -0700351 app_wrk = app_worker_get (app_wrk_index);
Florin Coras60116992018-08-27 09:52:18 -0700352 app_mq = app_wrk->event_queue;
353 if (!app_mq)
354 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800355 clib_warning ("app %u with api index: %u not attached",
356 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700357 return -1;
358 }
359
Florin Coras83ea6692018-10-12 16:55:14 -0700360 if (mq_try_lock_and_alloc_msg (app_mq, msg))
Florin Coras00e01d32019-10-21 16:07:46 -0700361 return SESSION_E_MQ_MSG_ALLOC;
Florin Coras83ea6692018-10-12 16:55:14 -0700362
Florin Coras60116992018-08-27 09:52:18 -0700363 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400364 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700365 evt->event_type = SESSION_CTRL_EVT_BOUND;
366 mp = (session_bound_msg_t *) evt->data;
367 mp->context = api_context;
368
369 if (rv)
370 goto done;
371
372 mp->handle = handle;
Florin Corasd4295e62019-02-22 13:11:38 -0800373 al = app_listener_get_w_handle (handle);
374 if (al->session_index != SESSION_INVALID_INDEX)
375 ls = app_listener_get_session (al);
Florin Coras60116992018-08-27 09:52:18 -0700376 else
Florin Corasd4295e62019-02-22 13:11:38 -0800377 ls = app_listener_get_local_session (al);
Yu Ping0b819152019-05-07 02:24:30 +0800378
379 session_get_endpoint (ls, &tep, 1 /* is_lcl */ );
380 mp->lcl_port = tep.port;
381 mp->lcl_is_ip4 = tep.is_ip4;
382 clib_memcpy_fast (mp->lcl_ip, &tep.ip, sizeof (tep.ip));
Florin Coras60116992018-08-27 09:52:18 -0700383
Florin Coras31c99552019-03-01 13:00:58 -0800384 vpp_evt_q = session_main_get_vpp_event_queue (0);
Florin Coras5f45e012019-01-23 09:21:30 -0800385 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
386
Florin Coras2b81e3c2019-02-27 07:55:46 -0800387 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras60116992018-08-27 09:52:18 -0700388 {
Florin Coras288eaab2019-02-03 15:26:14 -0800389 mp->rx_fifo = pointer_to_uword (ls->rx_fifo);
390 mp->tx_fifo = pointer_to_uword (ls->tx_fifo);
Florin Coras60116992018-08-27 09:52:18 -0700391 }
392
393done:
394 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700395 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700396 return 0;
397}
398
Florin Coras458089b2019-08-21 16:20:44 -0700399void
400mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
401 u32 context, int rv)
402{
403 svm_msg_q_msg_t _msg, *msg = &_msg;
404 session_unlisten_reply_msg_t *ump;
405 svm_msg_q_t *app_mq;
406 session_event_t *evt;
407
408 app_mq = app_wrk->event_queue;
409 if (mq_try_lock_and_alloc_msg (app_mq, msg))
410 return;
411
412 evt = svm_msg_q_msg_data (app_mq, msg);
413 clib_memset (evt, 0, sizeof (*evt));
414 evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY;
415 ump = (session_unlisten_reply_msg_t *) evt->data;
416 ump->context = context;
417 ump->handle = sh;
418 ump->retval = rv;
419 svm_msg_q_add_and_unlock (app_mq, msg);
420}
421
Florin Coras49568af2019-07-31 16:46:24 -0700422static void
423mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh)
424{
Florin Coras68b7e582020-01-21 18:33:23 -0800425 svm_msg_q_msg_t _msg, *msg = &_msg;
426 session_migrated_msg_t *mp;
427 svm_msg_q_t *vpp_evt_q;
428 app_worker_t *app_wrk;
429 session_event_t *evt;
430 svm_msg_q_t *app_mq;
431
432 app_wrk = app_worker_get (s->app_wrk_index);
433 app_mq = app_wrk->event_queue;
434 if (mq_try_lock_and_alloc_msg (app_mq, msg))
435 return;
436
437 evt = svm_msg_q_msg_data (app_mq, msg);
438 clib_memset (evt, 0, sizeof (*evt));
439 evt->event_type = SESSION_CTRL_EVT_MIGRATED;
440 mp = (session_migrated_msg_t *) evt->data;
441 mp->handle = session_handle (s);
442 mp->new_handle = new_sh;
443 mp->vpp_thread_index = session_thread_from_handle (new_sh);
444 vpp_evt_q = session_main_get_vpp_event_queue (mp->vpp_thread_index);
445 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
446 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras49568af2019-07-31 16:46:24 -0700447}
448
Florin Corasc4c4cf52019-08-24 18:17:34 -0700449static int
450mq_send_add_segment_cb (u32 app_wrk_index, u64 segment_handle)
451{
452 int fds[SESSION_N_FD_TYPE], n_fds = 0;
453 svm_msg_q_msg_t _msg, *msg = &_msg;
454 session_app_add_segment_msg_t *mp;
455 vl_api_registration_t *reg;
456 app_worker_t *app_wrk;
457 session_event_t *evt;
458 svm_msg_q_t *app_mq;
459 fifo_segment_t *fs;
460 ssvm_private_t *sp;
461 u8 fd_flags = 0;
462
463 app_wrk = app_worker_get (app_wrk_index);
464
465 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
466 if (!reg)
467 {
468 clib_warning ("no api registration for client: %u",
469 app_wrk->api_client_index);
470 return -1;
471 }
472
473 fs = segment_manager_get_segment_w_handle (segment_handle);
474 sp = &fs->ssvm;
475 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
476 {
477 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
478 {
479 clib_warning ("can't send memfd fd");
480 return -1;
481 }
482
483 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
484 fds[n_fds] = sp->fd;
485 n_fds += 1;
486 }
487
488 app_mq = app_wrk->event_queue;
489 if (mq_try_lock_and_alloc_msg (app_mq, msg))
490 return -1;
491
492 if (n_fds)
493 session_send_fds (reg, fds, n_fds);
494
495 evt = svm_msg_q_msg_data (app_mq, msg);
496 clib_memset (evt, 0, sizeof (*evt));
497 evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT;
498 mp = (session_app_add_segment_msg_t *) evt->data;
499 clib_memset (mp, 0, sizeof (*mp));
500 mp->segment_size = sp->ssvm_size;
501 mp->fd_flags = fd_flags;
502 mp->segment_handle = segment_handle;
503 strncpy ((char *) mp->segment_name, (char *) sp->name,
504 sizeof (mp->segment_name) - 1);
505
506 svm_msg_q_add_and_unlock (app_mq, msg);
507
508 return 0;
509}
510
511static int
512mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle)
513{
514 svm_msg_q_msg_t _msg, *msg = &_msg;
515 session_app_del_segment_msg_t *mp;
516 vl_api_registration_t *reg;
517 app_worker_t *app_wrk;
518 session_event_t *evt;
519 svm_msg_q_t *app_mq;
520
521 app_wrk = app_worker_get (app_wrk_index);
522 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
523 if (!reg)
524 {
525 clib_warning ("no registration: %u", app_wrk->api_client_index);
526 return -1;
527 }
528
529 app_mq = app_wrk->event_queue;
530 if (mq_try_lock_and_alloc_msg (app_mq, msg))
531 return -1;
532
533 evt = svm_msg_q_msg_data (app_mq, msg);
534 clib_memset (evt, 0, sizeof (*evt));
535 evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT;
536 mp = (session_app_del_segment_msg_t *) evt->data;
537 clib_memset (mp, 0, sizeof (*mp));
538 mp->segment_handle = segment_handle;
539 svm_msg_q_add_and_unlock (app_mq, msg);
540
541 return 0;
542}
543
Florin Coras9ace36d2019-10-28 13:14:17 -0700544static void
545mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf)
546{
547 svm_msg_q_msg_t _msg, *msg = &_msg;
548 session_cleanup_msg_t *mp;
549 svm_msg_q_t *app_mq;
550 session_event_t *evt;
551 app_worker_t *app_wrk;
552
Florin Coras36d49392020-04-24 23:00:11 +0000553 /* Propagate transport cleanup notifications only if app didn't close */
554 if (ntf == SESSION_CLEANUP_TRANSPORT
555 && s->session_state != SESSION_STATE_TRANSPORT_DELETED)
Florin Coras9ace36d2019-10-28 13:14:17 -0700556 return;
557
558 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
559 if (!app_wrk)
560 return;
561
562 app_mq = app_wrk->event_queue;
563 if (mq_try_lock_and_alloc_msg (app_mq, msg))
564 return;
565
566 evt = svm_msg_q_msg_data (app_mq, msg);
567 clib_memset (evt, 0, sizeof (*evt));
568 evt->event_type = SESSION_CTRL_EVT_CLEANUP;
569 mp = (session_cleanup_msg_t *) evt->data;
570 mp->handle = session_handle (s);
Florin Coras36d49392020-04-24 23:00:11 +0000571 mp->type = ntf;
Florin Coras9ace36d2019-10-28 13:14:17 -0700572 svm_msg_q_add_and_unlock (app_mq, msg);
573}
574
Florin Corasc4c4cf52019-08-24 18:17:34 -0700575static session_cb_vft_t session_mq_cb_vft = {
576 .session_accept_callback = mq_send_session_accepted_cb,
577 .session_disconnect_callback = mq_send_session_disconnected_cb,
578 .session_connected_callback = mq_send_session_connected_cb,
579 .session_reset_callback = mq_send_session_reset_cb,
580 .session_migrate_callback = mq_send_session_migrate_cb,
Florin Coras9ace36d2019-10-28 13:14:17 -0700581 .session_cleanup_callback = mq_send_session_cleanup_cb,
Florin Corasc4c4cf52019-08-24 18:17:34 -0700582 .add_segment_callback = mq_send_add_segment_cb,
583 .del_segment_callback = mq_send_del_segment_cb,
584};
585
Dave Barach68b0fb02017-02-28 15:15:56 -0500586static void
Florin Corase04c2992017-03-01 08:17:34 -0800587vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
588{
589 vl_api_session_enable_disable_reply_t *rmp;
590 vlib_main_t *vm = vlib_get_main ();
591 int rv = 0;
592
593 vnet_session_enable_disable (vm, mp->is_enable);
594 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
595}
596
Dave Barach68b0fb02017-02-28 15:15:56 -0500597static void
Florin Coras458089b2019-08-21 16:20:44 -0700598vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
599{
600 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
601 vl_api_app_attach_reply_t *rmp;
602 ssvm_private_t *segp, *evt_q_segment;
603 vnet_app_attach_args_t _a, *a = &_a;
604 u8 fd_flags = 0, ctrl_thread;
605 vl_api_registration_t *reg;
606 svm_msg_q_t *ctrl_mq;
607
608 reg = vl_api_client_index_to_registration (mp->client_index);
609 if (!reg)
610 return;
611
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 }
617
618 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
619 sizeof (mp->options),
620 "Out of options, fix api message definition");
621
622 clib_memset (a, 0, sizeof (*a));
623 a->api_client_index = mp->client_index;
624 a->options = mp->options;
625 a->session_cb_vft = &session_mq_cb_vft;
Florin Coras458089b2019-08-21 16:20:44 -0700626
Dave Barach77841402020-04-29 17:04:10 -0400627 a->namespace_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
Florin Coras458089b2019-08-21 16:20:44 -0700628
629 if ((rv = vnet_application_attach (a)))
630 {
631 clib_warning ("attach returned: %d", rv);
632 vec_free (a->namespace_id);
633 goto done;
634 }
635 vec_free (a->namespace_id);
636
637 /* Send event queues segment */
638 if ((evt_q_segment = session_main_get_evt_q_segment ()))
639 {
640 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
641 fds[n_fds] = evt_q_segment->fd;
642 n_fds += 1;
643 }
644 /* Send fifo segment fd if needed */
645 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
646 {
647 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
648 fds[n_fds] = a->segment->fd;
649 n_fds += 1;
650 }
651 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
652 {
653 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
654 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
655 n_fds += 1;
656 }
657
658done:
Florin Coras458089b2019-08-21 16:20:44 -0700659 /* *INDENT-OFF* */
660 REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({
661 if (!rv)
662 {
Vratko Polak4f599852019-11-08 10:32:39 +0100663 ctrl_thread = vlib_num_workers () ? 1 : 0;
Nathan Skrzypczak9d3e1b42019-11-07 10:29:24 +0100664 ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
Florin Coras458089b2019-08-21 16:20:44 -0700665 segp = a->segment;
666 rmp->app_index = clib_host_to_net_u32 (a->app_index);
667 rmp->app_mq = pointer_to_uword (a->app_evt_q);
668 rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
669 rmp->vpp_ctrl_mq_thread = ctrl_thread;
670 rmp->n_fds = n_fds;
671 rmp->fd_flags = fd_flags;
672 if (vec_len (segp->name))
673 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100674 vl_api_vec_to_api_string (segp->name, &rmp->segment_name);
Florin Coras458089b2019-08-21 16:20:44 -0700675 }
676 rmp->segment_size = segp->ssvm_size;
677 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
678 }
679 }));
680 /* *INDENT-ON* */
681
682 if (n_fds)
683 session_send_fds (reg, fds, n_fds);
684}
685
Florin Corascea194d2017-10-02 00:18:51 -0700686static void
Florin Coras15531972018-08-12 23:50:53 -0700687vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
688{
689 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
690 vl_api_app_worker_add_del_reply_t *rmp;
691 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -0700692 application_t *app;
693 u8 fd_flags = 0;
694
Florin Coras61ae0562020-09-02 19:10:28 -0700695 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras15531972018-08-12 23:50:53 -0700696 {
697 rv = VNET_API_ERROR_FEATURE_DISABLED;
698 goto done;
699 }
700
701 reg = vl_api_client_index_to_registration (mp->client_index);
702 if (!reg)
703 return;
704
Florin Corasc1f5a432018-11-20 11:31:26 -0800705 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -0700706 if (!app)
707 {
708 rv = VNET_API_ERROR_INVALID_VALUE;
709 goto done;
710 }
711
712 vnet_app_worker_add_del_args_t args = {
713 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -0800714 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -0800715 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -0700716 .is_add = mp->is_add
717 };
Florin Corasc1a42652019-02-08 18:27:29 -0800718 rv = vnet_app_worker_add_del (&args);
719 if (rv)
Florin Coras15531972018-08-12 23:50:53 -0700720 {
Florin Corasc1a42652019-02-08 18:27:29 -0800721 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -0700722 goto done;
723 }
724
Florin Coras14598772018-09-04 19:47:52 -0700725 if (!mp->is_add)
726 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -0700727
Florin Coras15531972018-08-12 23:50:53 -0700728 /* Send fifo segment fd if needed */
729 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
730 {
731 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
732 fds[n_fds] = args.segment->fd;
733 n_fds += 1;
734 }
735 if (application_segment_manager_properties (app)->use_mq_eventfd)
736 {
737 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
738 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
739 n_fds += 1;
740 }
741
742 /* *INDENT-OFF* */
743done:
744 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
745 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -0800746 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -0800747 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -0700748 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -0700749 {
Florin Coras15531972018-08-12 23:50:53 -0700750 if (vec_len (args.segment->name))
751 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100752 vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name);
Florin Coras15531972018-08-12 23:50:53 -0700753 }
754 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
755 rmp->n_fds = n_fds;
756 rmp->fd_flags = fd_flags;
757 }
758 }));
759 /* *INDENT-ON* */
760
761 if (n_fds)
762 session_send_fds (reg, fds, n_fds);
763}
764
765static void
Florin Coras888d9f02020-04-02 23:00:13 +0000766vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
767{
768 vl_api_application_detach_reply_t *rmp;
769 int rv = VNET_API_ERROR_INVALID_VALUE_2;
770 vnet_app_detach_args_t _a, *a = &_a;
771 application_t *app;
772
Florin Coras61ae0562020-09-02 19:10:28 -0700773 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras888d9f02020-04-02 23:00:13 +0000774 {
775 rv = VNET_API_ERROR_FEATURE_DISABLED;
776 goto done;
777 }
778
779 app = application_lookup (mp->client_index);
780 if (app)
781 {
782 a->app_index = app->app_index;
783 a->api_client_index = mp->client_index;
784 rv = vnet_application_detach (a);
785 }
786
787done:
788 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
789}
790
791static void
Florin Corascea194d2017-10-02 00:18:51 -0700792vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
793{
794 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -0800795 u32 appns_index = 0;
796 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700797 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200798 if (session_main_is_enabled () == 0)
Florin Corascea194d2017-10-02 00:18:51 -0700799 {
800 rv = VNET_API_ERROR_FEATURE_DISABLED;
801 goto done;
802 }
803
Dave Barach77841402020-04-29 17:04:10 -0400804 ns_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
Florin Corascea194d2017-10-02 00:18:51 -0700805
Florin Corascea194d2017-10-02 00:18:51 -0700806 vnet_app_namespace_add_del_args_t args = {
807 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -0700808 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -0700809 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
810 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
811 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
812 .is_add = 1
813 };
Florin Corasc1a42652019-02-08 18:27:29 -0800814 rv = vnet_app_namespace_add_del (&args);
815 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -0800816 {
817 appns_index = app_namespace_index_from_id (ns_id);
818 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
819 {
820 clib_warning ("app ns lookup failed");
821 rv = VNET_API_ERROR_UNSPECIFIED;
822 }
823 }
Florin Corascea194d2017-10-02 00:18:51 -0700824 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -0800825
826 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -0700827done:
Florin Coras6e8c6672017-11-10 09:03:54 -0800828 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
829 if (!rv)
830 rmp->appns_index = clib_host_to_net_u32 (appns_index);
831 }));
832 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -0700833}
834
Florin Coras1c710452017-10-17 00:03:13 -0700835static void
836vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
837{
838 vl_api_session_rule_add_del_reply_t *rmp;
839 session_rule_add_del_args_t args;
840 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -0700841 int rv = 0;
842
Dave Barachb7b92992018-10-17 10:38:51 -0400843 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -0700844
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100845 ip_prefix_decode (&mp->lcl, &table_args->lcl);
846 ip_prefix_decode (&mp->rmt, &table_args->rmt);
847
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100848 table_args->lcl_port = mp->lcl_port;
849 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -0700850 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
851 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -0800852 mp->tag[sizeof (mp->tag) - 1] = 0;
853 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700854 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
855 args.scope = mp->scope;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100856 args.transport_proto =
857 api_session_transport_proto_decode (&mp->transport_proto) ==
858 TRANSPORT_PROTO_UDP ? 1 : 0;
Florin Coras1c710452017-10-17 00:03:13 -0700859
Florin Corasc1a42652019-02-08 18:27:29 -0800860 rv = vnet_session_rule_add_del (&args);
861 if (rv)
862 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -0800863 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700864 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
865}
866
Florin Coras6c36f532017-11-03 18:32:34 -0700867static void
868send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -0800869 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800870 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700871{
872 vl_api_session_rules_details_t *rmp = 0;
873 session_mask_or_match_4_t *match =
874 (session_mask_or_match_4_t *) & rule->match;
875 session_mask_or_match_4_t *mask =
876 (session_mask_or_match_4_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100877 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -0700878
879 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400880 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -0700881 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
882 rmp->context = context;
883
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100884 clib_memset (&lcl, 0, sizeof (lcl));
885 clib_memset (&rmt, 0, sizeof (rmt));
886 ip_set (&lcl.fp_addr, &match->lcl_ip, 1);
887 ip_set (&rmt.fp_addr, &match->rmt_ip, 1);
888 lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip);
889 rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip);
890
891 ip_prefix_encode (&lcl, &rmp->lcl);
892 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100893 rmp->lcl_port = match->lcl_port;
894 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700895 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
896 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100897 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
898 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -0700899 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800900 if (tag)
901 {
Dave Barach178cf492018-11-13 16:34:13 -0500902 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -0800903 rmp->tag[vec_len (tag)] = 0;
904 }
Florin Coras6c36f532017-11-03 18:32:34 -0700905
Florin Coras6c4dae22018-01-09 06:39:23 -0800906 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700907}
908
909static void
Florin Corasc97a7392017-11-05 23:07:07 -0800910send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
911 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800912 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700913{
914 vl_api_session_rules_details_t *rmp = 0;
915 session_mask_or_match_6_t *match =
916 (session_mask_or_match_6_t *) & rule->match;
917 session_mask_or_match_6_t *mask =
918 (session_mask_or_match_6_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100919 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -0700920
921 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400922 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -0700923 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
924 rmp->context = context;
925
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100926 clib_memset (&lcl, 0, sizeof (lcl));
927 clib_memset (&rmt, 0, sizeof (rmt));
928 ip_set (&lcl.fp_addr, &match->lcl_ip, 0);
929 ip_set (&rmt.fp_addr, &match->rmt_ip, 0);
930 lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip);
931 rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip);
932
933 ip_prefix_encode (&lcl, &rmp->lcl);
934 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100935 rmp->lcl_port = match->lcl_port;
936 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700937 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800938 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100939 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
940 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -0700941 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800942 if (tag)
943 {
Dave Barach178cf492018-11-13 16:34:13 -0500944 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -0800945 rmp->tag[vec_len (tag)] = 0;
946 }
Florin Coras6c36f532017-11-03 18:32:34 -0700947
Florin Coras6c4dae22018-01-09 06:39:23 -0800948 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700949}
950
951static void
952send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -0800953 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800954 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700955{
956 mma_rule_16_t *rule16;
957 mma_rule_40_t *rule40;
958 mma_rules_table_16_t *srt16;
959 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -0800960 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -0700961
Florin Corasc97a7392017-11-05 23:07:07 -0800962 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -0700963 {
Florin Corasc97a7392017-11-05 23:07:07 -0800964 u8 *tag = 0;
965 /* *INDENT-OFF* */
966 srt16 = &srt->session_rules_tables_16;
967 pool_foreach (rule16, srt16->rules, ({
968 ri = mma_rules_table_rule_index_16 (srt16, rule16);
969 tag = session_rules_table_rule_tag (srt, ri, 1);
970 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800971 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -0800972 }));
973 /* *INDENT-ON* */
974 }
975 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
976 {
977 u8 *tag = 0;
978 /* *INDENT-OFF* */
979 srt40 = &srt->session_rules_tables_40;
980 pool_foreach (rule40, srt40->rules, ({
981 ri = mma_rules_table_rule_index_40 (srt40, rule40);
982 tag = session_rules_table_rule_tag (srt, ri, 1);
983 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800984 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -0800985 }));
986 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -0700987 }
988}
989
990static void
991vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
992{
Florin Coras6c4dae22018-01-09 06:39:23 -0800993 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -0700994 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -0800995 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -0700996
Florin Coras6c4dae22018-01-09 06:39:23 -0800997 reg = vl_api_client_index_to_registration (mp->client_index);
998 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -0700999 return;
1000
1001 /* *INDENT-OFF* */
1002 session_table_foreach (st, ({
Florin Coras07063b82020-03-13 04:44:51 +00001003 for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++)
Florin Corasc97a7392017-11-05 23:07:07 -08001004 {
1005 send_session_rules_table_details (&st->session_rules[tp],
1006 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001007 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001008 mp->context);
1009 }
Florin Coras6c36f532017-11-03 18:32:34 -07001010 }));
1011 /* *INDENT-ON* */
1012}
1013
Florin Coras371ca502018-02-21 12:07:41 -08001014static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001015vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001016{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001017 vl_api_app_add_cert_key_pair_reply_t *rmp;
1018 vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1019 u32 certkey_len, key_len, cert_len;
Florin Coras371ca502018-02-21 12:07:41 -08001020 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001021 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001022 {
1023 rv = VNET_API_ERROR_FEATURE_DISABLED;
1024 goto done;
1025 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001026
Florin Coras371ca502018-02-21 12:07:41 -08001027 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001028 if (cert_len > 10000)
1029 {
1030 rv = VNET_API_ERROR_INVALID_VALUE;
1031 goto done;
1032 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001033
1034 certkey_len = clib_net_to_host_u16 (mp->certkey_len);
1035 if (certkey_len < cert_len)
Florin Coras371ca502018-02-21 12:07:41 -08001036 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001037 rv = VNET_API_ERROR_INVALID_VALUE;
1038 goto done;
Florin Coras371ca502018-02-21 12:07:41 -08001039 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001040
1041 key_len = certkey_len - cert_len;
1042 if (key_len > 10000)
1043 {
1044 rv = VNET_API_ERROR_INVALID_VALUE;
1045 goto done;
1046 }
1047
1048 clib_memset (a, 0, sizeof (*a));
1049 vec_validate (a->cert, cert_len);
1050 vec_validate (a->key, key_len);
1051 clib_memcpy_fast (a->cert, mp->certkey, cert_len);
1052 clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len);
1053 rv = vnet_app_add_cert_key_pair (a);
Florin Coras371ca502018-02-21 12:07:41 -08001054 vec_free (a->cert);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001055 vec_free (a->key);
1056
Florin Coras371ca502018-02-21 12:07:41 -08001057done:
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001058 /* *INDENT-OFF* */
1059 REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
1060 if (!rv)
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001061 rmp->index = clib_host_to_net_u32 (a->index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001062 }));
1063 /* *INDENT-ON* */
Florin Coras371ca502018-02-21 12:07:41 -08001064}
1065
1066static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001067vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001068{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001069 vl_api_app_del_cert_key_pair_reply_t *rmp;
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001070 u32 ckpair_index;
Florin Coras371ca502018-02-21 12:07:41 -08001071 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001072 if (session_main_is_enabled () == 0)
1073 {
1074 rv = VNET_API_ERROR_FEATURE_DISABLED;
1075 goto done;
1076 }
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001077 ckpair_index = clib_net_to_host_u32 (mp->index);
1078 rv = vnet_app_del_cert_key_pair (ckpair_index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001079
1080done:
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001081 REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001082}
1083
1084/* ### WILL BE DEPRECATED POST 20.01 ### */
1085static void
1086vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1087 mp)
1088{
1089 vl_api_application_tls_cert_add_reply_t *rmp;
1090 app_cert_key_pair_t *ckpair;
1091 application_t *app;
1092 u32 cert_len;
1093 int rv = 0;
1094 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001095 {
1096 rv = VNET_API_ERROR_FEATURE_DISABLED;
1097 goto done;
1098 }
Florin Corase3e2f072018-03-04 07:24:30 -08001099 if (!(app = application_lookup (mp->client_index)))
1100 {
1101 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1102 goto done;
1103 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001104 cert_len = clib_net_to_host_u16 (mp->cert_len);
1105 if (cert_len > 10000)
1106 {
1107 rv = VNET_API_ERROR_INVALID_VALUE;
1108 goto done;
1109 }
1110 ckpair = app_cert_key_pair_get_default ();
1111 vec_validate (ckpair->cert, cert_len);
1112 clib_memcpy_fast (ckpair->cert, mp->cert, cert_len);
1113
1114done:
1115 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1116}
1117
1118/* ### WILL BE DEPRECATED POST 20.01 ### */
1119static void
1120vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1121 mp)
1122{
1123 vl_api_application_tls_key_add_reply_t *rmp;
1124 app_cert_key_pair_t *ckpair;
1125 application_t *app;
1126 u32 key_len;
1127 int rv = 0;
1128 if (session_main_is_enabled () == 0)
1129 {
1130 rv = VNET_API_ERROR_FEATURE_DISABLED;
1131 goto done;
1132 }
1133 if (!(app = application_lookup (mp->client_index)))
1134 {
1135 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1136 goto done;
1137 }
Florin Coras371ca502018-02-21 12:07:41 -08001138 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001139 if (key_len > 10000)
1140 {
1141 rv = VNET_API_ERROR_INVALID_VALUE;
1142 goto done;
1143 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001144 ckpair = app_cert_key_pair_get_default ();
1145 vec_validate (ckpair->key, key_len);
1146 clib_memcpy_fast (ckpair->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001147done:
1148 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1149}
1150
Florin Coras6cf30ad2017-04-04 23:08:23 -07001151static clib_error_t *
1152application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001153{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001154 application_t *app = application_lookup (client_index);
1155 vnet_app_detach_args_t _a, *a = &_a;
1156 if (app)
1157 {
Florin Coras15531972018-08-12 23:50:53 -07001158 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001159 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001160 vnet_application_detach (a);
1161 }
1162 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001163}
1164
Florin Coras6cf30ad2017-04-04 23:08:23 -07001165VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001166
1167#define vl_msg_name_crc_list
1168#include <vnet/vnet_all_api_h.h>
1169#undef vl_msg_name_crc_list
1170
1171static void
1172setup_message_id_table (api_main_t * am)
1173{
1174#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1175 foreach_vl_msg_name_crc_session;
1176#undef _
1177}
1178
1179/*
1180 * session_api_hookup
1181 * Add uri's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -05001182 * vlib has already mapped shared memory and
Dave Barach68b0fb02017-02-28 15:15:56 -05001183 * added the client registration handlers.
1184 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1185 */
1186static clib_error_t *
1187session_api_hookup (vlib_main_t * vm)
1188{
Dave Barach39d69112019-11-27 11:42:13 -05001189 api_main_t *am = vlibapi_get_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001190
1191#define _(N,n) \
1192 vl_msg_api_set_handlers(VL_API_##N, #n, \
1193 vl_api_##n##_t_handler, \
1194 vl_noop_handler, \
1195 vl_api_##n##_t_endian, \
1196 vl_api_##n##_t_print, \
1197 sizeof(vl_api_##n##_t), 1);
1198 foreach_session_api_msg;
1199#undef _
1200
1201 /*
Dave Barach68b0fb02017-02-28 15:15:56 -05001202 * Set up the (msg_name, crc, message-id) table
1203 */
1204 setup_message_id_table (am);
1205
1206 return 0;
1207}
1208
1209VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001210
Dave Barach68b0fb02017-02-28 15:15:56 -05001211/*
Florin Coras61ae0562020-09-02 19:10:28 -07001212 * Socket api functions
1213 */
1214
1215static void
1216sapi_send_fds (app_worker_t * app_wrk, int *fds, int n_fds)
1217{
1218 app_sapi_msg_t smsg = { 0 };
1219 app_namespace_t *app_ns;
1220 application_t *app;
1221 clib_socket_t *cs;
1222 u32 cs_index;
1223
1224 app = application_get (app_wrk->app_index);
1225 app_ns = app_namespace_get (app->ns_index);
1226 cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index);
1227 cs = appns_sapi_get_socket (app_ns, cs_index);
1228
1229 /* There's no payload for the message only the type */
1230 smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS;
1231 clib_socket_sendmsg (cs, &smsg, sizeof (smsg), fds, n_fds);
1232}
1233
1234static int
1235mq_send_add_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1236{
1237 int fds[SESSION_N_FD_TYPE], n_fds = 0;
1238 svm_msg_q_msg_t _msg, *msg = &_msg;
1239 session_app_add_segment_msg_t *mp;
1240 app_worker_t *app_wrk;
1241 session_event_t *evt;
1242 svm_msg_q_t *app_mq;
1243 fifo_segment_t *fs;
1244 ssvm_private_t *sp;
1245 u8 fd_flags = 0;
1246
1247 app_wrk = app_worker_get (app_wrk_index);
1248
1249 fs = segment_manager_get_segment_w_handle (segment_handle);
1250 sp = &fs->ssvm;
1251 ASSERT (ssvm_type (sp) == SSVM_SEGMENT_MEMFD);
1252
1253 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1254 fds[n_fds] = sp->fd;
1255 n_fds += 1;
1256
1257 app_mq = app_wrk->event_queue;
1258 if (mq_try_lock_and_alloc_msg (app_mq, msg))
1259 return -1;
1260
1261 /*
1262 * Send the fd over api socket
1263 */
1264 sapi_send_fds (app_wrk, fds, n_fds);
1265
1266 /*
1267 * Send the actual message over mq
1268 */
1269 evt = svm_msg_q_msg_data (app_mq, msg);
1270 clib_memset (evt, 0, sizeof (*evt));
1271 evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT;
1272 mp = (session_app_add_segment_msg_t *) evt->data;
1273 clib_memset (mp, 0, sizeof (*mp));
1274 mp->segment_size = sp->ssvm_size;
1275 mp->fd_flags = fd_flags;
1276 mp->segment_handle = segment_handle;
1277 strncpy ((char *) mp->segment_name, (char *) sp->name,
1278 sizeof (mp->segment_name) - 1);
1279
1280 svm_msg_q_add_and_unlock (app_mq, msg);
1281
1282 return 0;
1283}
1284
1285static int
1286mq_send_del_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1287{
1288 svm_msg_q_msg_t _msg, *msg = &_msg;
1289 session_app_del_segment_msg_t *mp;
1290 app_worker_t *app_wrk;
1291 session_event_t *evt;
1292 svm_msg_q_t *app_mq;
1293
1294 app_wrk = app_worker_get (app_wrk_index);
1295
1296 app_mq = app_wrk->event_queue;
1297 if (mq_try_lock_and_alloc_msg (app_mq, msg))
1298 return -1;
1299
1300 evt = svm_msg_q_msg_data (app_mq, msg);
1301 clib_memset (evt, 0, sizeof (*evt));
1302 evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT;
1303 mp = (session_app_del_segment_msg_t *) evt->data;
1304 clib_memset (mp, 0, sizeof (*mp));
1305 mp->segment_handle = segment_handle;
1306 svm_msg_q_add_and_unlock (app_mq, msg);
1307
1308 return 0;
1309}
1310
1311static session_cb_vft_t session_mq_sapi_cb_vft = {
1312 .session_accept_callback = mq_send_session_accepted_cb,
1313 .session_disconnect_callback = mq_send_session_disconnected_cb,
1314 .session_connected_callback = mq_send_session_connected_cb,
1315 .session_reset_callback = mq_send_session_reset_cb,
1316 .session_migrate_callback = mq_send_session_migrate_cb,
1317 .session_cleanup_callback = mq_send_session_cleanup_cb,
1318 .add_segment_callback = mq_send_add_segment_sapi_cb,
1319 .del_segment_callback = mq_send_del_segment_sapi_cb,
1320};
1321
1322static void
1323session_api_attach_handler (app_namespace_t * app_ns, clib_socket_t * cs,
1324 app_sapi_attach_msg_t * mp)
1325{
1326 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1327 vnet_app_attach_args_t _a, *a = &_a;
1328 app_sapi_attach_reply_msg_t *rmp;
1329 ssvm_private_t *evt_q_segment;
1330 u8 fd_flags = 0, ctrl_thread;
1331 app_ns_api_handle_t *handle;
1332 app_sapi_msg_t msg = { 0 };
1333 app_worker_t *app_wrk;
1334 svm_msg_q_t *ctrl_mq;
1335 application_t *app;
1336
1337 /* Make sure name is null terminated */
1338 mp->name[63] = 0;
1339
1340 clib_memset (a, 0, sizeof (*a));
1341 a->api_client_index = appns_sapi_socket_handle (app_ns, cs);
1342 a->name = format (0, "%s", (char *) mp->name);
1343 a->options = mp->options;
1344 a->session_cb_vft = &session_mq_sapi_cb_vft;
1345 a->use_sock_api = 1;
1346 a->options[APP_OPTIONS_NAMESPACE] = app_namespace_index (app_ns);
1347
1348 if ((rv = vnet_application_attach (a)))
1349 {
1350 clib_warning ("attach returned: %d", rv);
1351 goto done;
1352 }
1353
1354 /* Send event queues segment */
1355 if ((evt_q_segment = session_main_get_evt_q_segment ()))
1356 {
1357 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
1358 fds[n_fds] = evt_q_segment->fd;
1359 n_fds += 1;
1360 }
1361 /* Send fifo segment fd if needed */
1362 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
1363 {
1364 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1365 fds[n_fds] = a->segment->fd;
1366 n_fds += 1;
1367 }
1368 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
1369 {
1370 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1371 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
1372 n_fds += 1;
1373 }
1374
1375done:
1376
1377 msg.type = APP_SAPI_MSG_TYPE_ATTACH_REPLY;
1378 rmp = &msg.attach_reply;
1379 rmp->retval = rv;
1380 if (!rv)
1381 {
1382 ctrl_thread = vlib_num_workers ()? 1 : 0;
1383 ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
1384 rmp->app_index = a->app_index;
1385 rmp->app_mq = pointer_to_uword (a->app_evt_q);
1386 rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
1387 rmp->vpp_ctrl_mq_thread = ctrl_thread;
1388 rmp->n_fds = n_fds;
1389 rmp->fd_flags = fd_flags;
1390 /* No segment name and size since we only support memfds
1391 * in this configuration */
1392 rmp->segment_handle = a->segment_handle;
1393 rmp->api_client_handle = a->api_client_index;
1394
1395 /* Update app index for socket */
1396 handle = (app_ns_api_handle_t *) & cs->private_data;
1397 app = application_get (a->app_index);
1398 app_wrk = application_get_worker (app, 0);
1399 handle->aah_app_wrk_index = app_wrk->wrk_index;
1400 }
1401
1402 clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1403 vec_free (a->name);
1404}
1405
1406static void
1407sapi_socket_close_w_handle (u32 api_handle)
1408{
1409 app_namespace_t *app_ns = app_namespace_get (api_handle >> 16);
1410 u16 sock_index = api_handle & 0xffff;
1411 app_ns_api_handle_t *handle;
1412 clib_socket_t *cs;
1413 clib_file_t *cf;
1414
1415 cs = appns_sapi_get_socket (app_ns, sock_index);
1416 if (!cs)
1417 return;
1418
1419 handle = (app_ns_api_handle_t *) & cs->private_data;
1420 cf = clib_file_get (&file_main, handle->aah_file_index);
1421 clib_file_del (&file_main, cf);
1422
1423 clib_socket_close (cs);
1424 appns_sapi_free_socket (app_ns, cs);
1425}
1426
1427static void
1428sapi_add_del_worker_handler (app_namespace_t * app_ns,
1429 clib_socket_t * cs,
1430 app_sapi_worker_add_del_msg_t * mp)
1431{
1432 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1433 app_sapi_worker_add_del_reply_msg_t *rmp;
1434 app_ns_api_handle_t *handle;
1435 app_sapi_msg_t msg = { 0 };
1436 app_worker_t *app_wrk;
1437 application_t *app;
1438 u32 sapi_handle;
1439 u8 fd_flags = 0;
1440
1441 app = application_get_if_valid (mp->app_index);
1442 if (!app)
1443 {
1444 rv = VNET_API_ERROR_INVALID_VALUE;
1445 goto done;
1446 }
1447
1448 sapi_handle = appns_sapi_socket_handle (app_ns, cs);
1449
1450 vnet_app_worker_add_del_args_t args = {
1451 .app_index = app->app_index,
1452 .wrk_map_index = mp->wrk_index,
1453 .api_client_index = sapi_handle,
1454 .is_add = mp->is_add
1455 };
1456 rv = vnet_app_worker_add_del (&args);
1457 if (rv)
1458 {
1459 clib_warning ("app worker add/del returned: %d", rv);
1460 goto done;
1461 }
1462
1463 if (!mp->is_add)
1464 {
1465 sapi_socket_close_w_handle (sapi_handle);
1466 goto done;
1467 }
1468
1469 /* Send fifo segment fd if needed */
1470 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1471 {
1472 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1473 fds[n_fds] = args.segment->fd;
1474 n_fds += 1;
1475 }
1476 if (application_segment_manager_properties (app)->use_mq_eventfd)
1477 {
1478 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1479 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1480 n_fds += 1;
1481 }
1482
1483done:
1484
1485 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY;
1486 rmp = &msg.worker_add_del_reply;
1487 rmp->retval = rv;
1488 rmp->is_add = mp->is_add;
1489 rmp->wrk_index = args.wrk_map_index;
1490 rmp->segment_handle = args.segment_handle;
1491 if (!rv && mp->is_add)
1492 {
1493 /* No segment name and size. This supports only memfds */
1494 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1495 rmp->n_fds = n_fds;
1496 rmp->fd_flags = fd_flags;
1497
1498 /* Update app index for socket */
1499 handle = (app_ns_api_handle_t *) & cs->private_data;
1500 app_wrk = application_get_worker (app, args.wrk_map_index);
1501 handle->aah_app_wrk_index = app_wrk->wrk_index;
1502 }
1503
1504 clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1505}
1506
1507static void
1508sapi_socket_detach (app_namespace_t * app_ns, clib_socket_t * cs)
1509{
Florin Coras61ae0562020-09-02 19:10:28 -07001510 app_ns_api_handle_t *handle;
1511 app_worker_t *app_wrk;
1512 u32 api_client_handle;
1513
1514 api_client_handle = appns_sapi_socket_handle (app_ns, cs);
1515 sapi_socket_close_w_handle (api_client_handle);
1516
Florin Corasf99a7d62020-09-14 10:29:29 -07001517 /* Cleanup everything because app worker closed socket or crashed */
Florin Coras61ae0562020-09-02 19:10:28 -07001518 handle = (app_ns_api_handle_t *) & cs->private_data;
1519 app_wrk = app_worker_get (handle->aah_app_wrk_index);
Florin Corasf99a7d62020-09-14 10:29:29 -07001520
1521 vnet_app_worker_add_del_args_t args = {
1522 .app_index = app_wrk->app_index,
1523 .wrk_map_index = app_wrk->wrk_map_index,
1524 .api_client_index = api_client_handle,
1525 .is_add = 0
1526 };
1527 /* Send rpc to main thread for worker barrier */
1528 vlib_rpc_call_main_thread (vnet_app_worker_add_del, (u8 *) & args,
1529 sizeof (args));
Florin Coras61ae0562020-09-02 19:10:28 -07001530}
1531
1532static clib_error_t *
1533sapi_sock_read_ready (clib_file_t * cf)
1534{
1535 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1536 app_sapi_msg_t msg = { 0 };
1537 app_namespace_t *app_ns;
1538 clib_error_t *err = 0;
1539 clib_socket_t *cs;
1540
1541 app_ns = app_namespace_get (handle->aah_app_ns_index);
1542 cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
1543 if (!cs)
1544 goto error;
1545
1546 err = clib_socket_recvmsg (cs, &msg, sizeof (msg), 0, 0);
1547 if (err)
1548 {
1549 clib_error_free (err);
1550 sapi_socket_detach (app_ns, cs);
1551 goto error;
1552 }
1553
1554 handle = (app_ns_api_handle_t *) & cs->private_data;
1555
1556 switch (msg.type)
1557 {
1558 case APP_SAPI_MSG_TYPE_ATTACH:
1559 session_api_attach_handler (app_ns, cs, &msg.attach);
1560 break;
1561 case APP_SAPI_MSG_TYPE_ADD_DEL_WORKER:
1562 sapi_add_del_worker_handler (app_ns, cs, &msg.worker_add_del);
1563 break;
1564 default:
1565 clib_warning ("app wrk %u unknown message type: %u",
1566 handle->aah_app_wrk_index, msg.type);
1567 break;
1568 }
1569
1570error:
1571 return 0;
1572}
1573
1574static clib_error_t *
1575sapi_sock_write_ready (clib_file_t * cf)
1576{
1577 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1578 clib_warning ("called for app ns %u", handle->aah_app_ns_index);
1579 return 0;
1580}
1581
1582static clib_error_t *
1583sapi_sock_error (clib_file_t * cf)
1584{
1585 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1586 app_namespace_t *app_ns;
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 return 0;
1593
1594 sapi_socket_detach (app_ns, cs);
1595 return 0;
1596}
1597
1598static clib_error_t *
1599sapi_sock_accept_ready (clib_file_t * scf)
1600{
1601 app_ns_api_handle_t handle = *(app_ns_api_handle_t *) & scf->private_data;
1602 app_namespace_t *app_ns;
1603 clib_file_t cf = { 0 };
1604 clib_error_t *err = 0;
1605 clib_socket_t *ccs, *scs;
1606
1607 /* Listener files point to namespace */
1608 app_ns = app_namespace_get (handle.aah_app_ns_index);
1609
1610 /*
1611 * Initialize client socket
1612 */
1613 ccs = appns_sapi_alloc_socket (app_ns);
1614
1615 /* Grab server socket after client is initialized */
1616 scs = appns_sapi_get_socket (app_ns, handle.aah_sock_index);
1617 if (!scs)
1618 goto error;
1619
1620 err = clib_socket_accept (scs, ccs);
1621 if (err)
1622 {
1623 clib_error_report (err);
1624 goto error;
1625 }
1626
1627 cf.read_function = sapi_sock_read_ready;
1628 cf.write_function = sapi_sock_write_ready;
1629 cf.error_function = sapi_sock_error;
1630 cf.file_descriptor = ccs->fd;
1631 /* File points to app namespace and socket */
1632 handle.aah_sock_index = appns_sapi_socket_index (app_ns, ccs);
1633 cf.private_data = handle.as_uword;
1634 cf.description = format (0, "app sock conn fd: %d", ccs->fd);
1635
1636 /* Poll until we get an attach message. Socket points to file and
1637 * application that owns the socket */
1638 handle.aah_app_wrk_index = APP_INVALID_INDEX;
1639 handle.aah_file_index = clib_file_add (&file_main, &cf);
1640 ccs->private_data = handle.as_uword;
1641
1642 return err;
1643
1644error:
1645 appns_sapi_free_socket (app_ns, ccs);
1646 return err;
1647}
1648
1649int
1650appns_sapi_add_ns_socket (app_namespace_t * app_ns)
1651{
1652 char *subdir = "/app_ns_sockets/";
1653 app_ns_api_handle_t *handle;
1654 clib_file_t cf = { 0 };
1655 struct stat file_stat;
1656 clib_error_t *err;
1657 clib_socket_t *cs;
1658 u8 *dir = 0;
1659 int rv = 0;
1660
1661 vec_add (dir, vlib_unix_get_runtime_dir (),
1662 strlen (vlib_unix_get_runtime_dir ()));
1663 vec_add (dir, (u8 *) subdir, strlen (subdir));
1664
1665 err = vlib_unix_recursive_mkdir ((char *) dir);
1666 if (err)
1667 {
1668 clib_error_report (err);
1669 rv = -1;
1670 goto error;
1671 }
1672
1673 app_ns->sock_name = format (0, "%v%v%c", dir, app_ns->ns_id, 0);
1674
1675 /*
1676 * Create and initialize socket to listen on
1677 */
1678 cs = appns_sapi_alloc_socket (app_ns);
1679 cs->config = (char *) app_ns->sock_name;
1680 cs->flags = CLIB_SOCKET_F_IS_SERVER |
1681 CLIB_SOCKET_F_ALLOW_GROUP_WRITE |
1682 CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED;
1683
1684 if ((err = clib_socket_init (cs)))
1685 {
1686 clib_error_report (err);
1687 rv = -1;
1688 goto error;
1689 }
1690
1691 if (stat ((char *) app_ns->sock_name, &file_stat) == -1)
1692 {
1693 rv = -1;
1694 goto error;
1695 }
1696
1697 /*
1698 * Start polling it
1699 */
1700 cf.read_function = sapi_sock_accept_ready;
1701 cf.file_descriptor = cs->fd;
1702 /* File points to namespace */
1703 handle = (app_ns_api_handle_t *) & cf.private_data;
1704 handle->aah_app_ns_index = app_namespace_index (app_ns);
1705 handle->aah_sock_index = appns_sapi_socket_index (app_ns, cs);
1706 cf.description = format (0, "app sock listener: %s", app_ns->sock_name);
1707
1708 /* Socket points to clib file index */
1709 handle = (app_ns_api_handle_t *) & cs->private_data;
1710 handle->aah_file_index = clib_file_add (&file_main, &cf);
1711 handle->aah_app_wrk_index = APP_INVALID_INDEX;
1712
1713error:
1714 vec_free (dir);
1715 return rv;
1716}
1717
1718/*
Dave Barach68b0fb02017-02-28 15:15:56 -05001719 * fd.io coding-style-patch-verification: ON
1720 *
1721 * Local Variables:
1722 * eval: (c-set-style "gnu")
1723 * End:
1724 */