blob: 90c0772493af360e3fd8122f6964939c82bfd888 [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 Coras2d0e3de2020-10-23 16:31:40 -0700183 vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
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 Coras2d0e3de2020-10-23 16:31:40 -0700321 vpp_mq = session_main_get_vpp_event_queue (s->thread_index);
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 }
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
642 /* Send event queues segment */
643 if ((evt_q_segment = session_main_get_evt_q_segment ()))
644 {
645 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
646 fds[n_fds] = evt_q_segment->fd;
647 n_fds += 1;
648 }
649 /* Send fifo segment fd if needed */
650 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
651 {
652 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
653 fds[n_fds] = a->segment->fd;
654 n_fds += 1;
655 }
656 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
657 {
658 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
659 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
660 n_fds += 1;
661 }
662
663done:
Florin Coras458089b2019-08-21 16:20:44 -0700664 /* *INDENT-OFF* */
665 REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({
666 if (!rv)
667 {
Vratko Polak4f599852019-11-08 10:32:39 +0100668 ctrl_thread = vlib_num_workers () ? 1 : 0;
Nathan Skrzypczak9d3e1b42019-11-07 10:29:24 +0100669 ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
Florin Coras458089b2019-08-21 16:20:44 -0700670 segp = a->segment;
671 rmp->app_index = clib_host_to_net_u32 (a->app_index);
672 rmp->app_mq = pointer_to_uword (a->app_evt_q);
673 rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
674 rmp->vpp_ctrl_mq_thread = ctrl_thread;
675 rmp->n_fds = n_fds;
676 rmp->fd_flags = fd_flags;
677 if (vec_len (segp->name))
678 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100679 vl_api_vec_to_api_string (segp->name, &rmp->segment_name);
Florin Coras458089b2019-08-21 16:20:44 -0700680 }
681 rmp->segment_size = segp->ssvm_size;
682 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
683 }
684 }));
685 /* *INDENT-ON* */
686
687 if (n_fds)
688 session_send_fds (reg, fds, n_fds);
689}
690
Florin Corascea194d2017-10-02 00:18:51 -0700691static void
Florin Coras15531972018-08-12 23:50:53 -0700692vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
693{
694 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
695 vl_api_app_worker_add_del_reply_t *rmp;
696 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -0700697 application_t *app;
698 u8 fd_flags = 0;
699
Florin Coras61ae0562020-09-02 19:10:28 -0700700 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras15531972018-08-12 23:50:53 -0700701 {
702 rv = VNET_API_ERROR_FEATURE_DISABLED;
703 goto done;
704 }
705
706 reg = vl_api_client_index_to_registration (mp->client_index);
707 if (!reg)
708 return;
709
Florin Corasc1f5a432018-11-20 11:31:26 -0800710 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -0700711 if (!app)
712 {
713 rv = VNET_API_ERROR_INVALID_VALUE;
714 goto done;
715 }
716
717 vnet_app_worker_add_del_args_t args = {
718 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -0800719 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -0800720 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -0700721 .is_add = mp->is_add
722 };
Florin Corasc1a42652019-02-08 18:27:29 -0800723 rv = vnet_app_worker_add_del (&args);
724 if (rv)
Florin Coras15531972018-08-12 23:50:53 -0700725 {
Florin Corasc1a42652019-02-08 18:27:29 -0800726 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -0700727 goto done;
728 }
729
Florin Coras14598772018-09-04 19:47:52 -0700730 if (!mp->is_add)
731 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -0700732
Florin Coras15531972018-08-12 23:50:53 -0700733 /* Send fifo segment fd if needed */
734 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
735 {
736 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
737 fds[n_fds] = args.segment->fd;
738 n_fds += 1;
739 }
740 if (application_segment_manager_properties (app)->use_mq_eventfd)
741 {
742 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
743 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
744 n_fds += 1;
745 }
746
747 /* *INDENT-OFF* */
748done:
749 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
750 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -0800751 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -0800752 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -0700753 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -0700754 {
Florin Coras15531972018-08-12 23:50:53 -0700755 if (vec_len (args.segment->name))
756 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100757 vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name);
Florin Coras15531972018-08-12 23:50:53 -0700758 }
759 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
760 rmp->n_fds = n_fds;
761 rmp->fd_flags = fd_flags;
762 }
763 }));
764 /* *INDENT-ON* */
765
766 if (n_fds)
767 session_send_fds (reg, fds, n_fds);
768}
769
770static void
Florin Coras888d9f02020-04-02 23:00:13 +0000771vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
772{
773 vl_api_application_detach_reply_t *rmp;
774 int rv = VNET_API_ERROR_INVALID_VALUE_2;
775 vnet_app_detach_args_t _a, *a = &_a;
776 application_t *app;
777
Florin Coras61ae0562020-09-02 19:10:28 -0700778 if (!session_main_is_enabled () || appns_sapi_enabled ())
Florin Coras888d9f02020-04-02 23:00:13 +0000779 {
780 rv = VNET_API_ERROR_FEATURE_DISABLED;
781 goto done;
782 }
783
784 app = application_lookup (mp->client_index);
785 if (app)
786 {
787 a->app_index = app->app_index;
788 a->api_client_index = mp->client_index;
789 rv = vnet_application_detach (a);
790 }
791
792done:
793 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
794}
795
796static void
Florin Corascea194d2017-10-02 00:18:51 -0700797vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
798{
799 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -0800800 u32 appns_index = 0;
801 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700802 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200803 if (session_main_is_enabled () == 0)
Florin Corascea194d2017-10-02 00:18:51 -0700804 {
805 rv = VNET_API_ERROR_FEATURE_DISABLED;
806 goto done;
807 }
808
Dave Barach77841402020-04-29 17:04:10 -0400809 ns_id = vl_api_from_api_to_new_vec (mp, &mp->namespace_id);
Florin Corascea194d2017-10-02 00:18:51 -0700810
Florin Corascea194d2017-10-02 00:18:51 -0700811 vnet_app_namespace_add_del_args_t args = {
812 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -0700813 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -0700814 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
815 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
816 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
817 .is_add = 1
818 };
Florin Corasc1a42652019-02-08 18:27:29 -0800819 rv = vnet_app_namespace_add_del (&args);
820 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -0800821 {
822 appns_index = app_namespace_index_from_id (ns_id);
823 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
824 {
825 clib_warning ("app ns lookup failed");
826 rv = VNET_API_ERROR_UNSPECIFIED;
827 }
828 }
Florin Corascea194d2017-10-02 00:18:51 -0700829 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -0800830
831 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -0700832done:
Florin Coras6e8c6672017-11-10 09:03:54 -0800833 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
834 if (!rv)
835 rmp->appns_index = clib_host_to_net_u32 (appns_index);
836 }));
837 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -0700838}
839
Florin Coras1c710452017-10-17 00:03:13 -0700840static void
841vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
842{
843 vl_api_session_rule_add_del_reply_t *rmp;
844 session_rule_add_del_args_t args;
845 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -0700846 int rv = 0;
847
Dave Barachb7b92992018-10-17 10:38:51 -0400848 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -0700849
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100850 ip_prefix_decode (&mp->lcl, &table_args->lcl);
851 ip_prefix_decode (&mp->rmt, &table_args->rmt);
852
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100853 table_args->lcl_port = mp->lcl_port;
854 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -0700855 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
856 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -0800857 mp->tag[sizeof (mp->tag) - 1] = 0;
858 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700859 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
860 args.scope = mp->scope;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100861 args.transport_proto =
862 api_session_transport_proto_decode (&mp->transport_proto) ==
863 TRANSPORT_PROTO_UDP ? 1 : 0;
Florin Coras1c710452017-10-17 00:03:13 -0700864
Florin Corasc1a42652019-02-08 18:27:29 -0800865 rv = vnet_session_rule_add_del (&args);
866 if (rv)
867 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -0800868 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700869 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
870}
871
Florin Coras6c36f532017-11-03 18:32:34 -0700872static void
873send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -0800874 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800875 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700876{
877 vl_api_session_rules_details_t *rmp = 0;
878 session_mask_or_match_4_t *match =
879 (session_mask_or_match_4_t *) & rule->match;
880 session_mask_or_match_4_t *mask =
881 (session_mask_or_match_4_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100882 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -0700883
884 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400885 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -0700886 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
887 rmp->context = context;
888
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100889 clib_memset (&lcl, 0, sizeof (lcl));
890 clib_memset (&rmt, 0, sizeof (rmt));
891 ip_set (&lcl.fp_addr, &match->lcl_ip, 1);
892 ip_set (&rmt.fp_addr, &match->rmt_ip, 1);
893 lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip);
894 rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip);
895
896 ip_prefix_encode (&lcl, &rmp->lcl);
897 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100898 rmp->lcl_port = match->lcl_port;
899 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700900 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
901 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100902 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
903 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -0700904 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800905 if (tag)
906 {
Dave Barach178cf492018-11-13 16:34:13 -0500907 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -0800908 rmp->tag[vec_len (tag)] = 0;
909 }
Florin Coras6c36f532017-11-03 18:32:34 -0700910
Florin Coras6c4dae22018-01-09 06:39:23 -0800911 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700912}
913
914static void
Florin Corasc97a7392017-11-05 23:07:07 -0800915send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
916 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800917 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700918{
919 vl_api_session_rules_details_t *rmp = 0;
920 session_mask_or_match_6_t *match =
921 (session_mask_or_match_6_t *) & rule->match;
922 session_mask_or_match_6_t *mask =
923 (session_mask_or_match_6_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100924 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -0700925
926 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400927 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -0700928 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
929 rmp->context = context;
930
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100931 clib_memset (&lcl, 0, sizeof (lcl));
932 clib_memset (&rmt, 0, sizeof (rmt));
933 ip_set (&lcl.fp_addr, &match->lcl_ip, 0);
934 ip_set (&rmt.fp_addr, &match->rmt_ip, 0);
935 lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip);
936 rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip);
937
938 ip_prefix_encode (&lcl, &rmp->lcl);
939 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100940 rmp->lcl_port = match->lcl_port;
941 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700942 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800943 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100944 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
945 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -0700946 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800947 if (tag)
948 {
Dave Barach178cf492018-11-13 16:34:13 -0500949 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -0800950 rmp->tag[vec_len (tag)] = 0;
951 }
Florin Coras6c36f532017-11-03 18:32:34 -0700952
Florin Coras6c4dae22018-01-09 06:39:23 -0800953 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700954}
955
956static void
957send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -0800958 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800959 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700960{
961 mma_rule_16_t *rule16;
962 mma_rule_40_t *rule40;
963 mma_rules_table_16_t *srt16;
964 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -0800965 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -0700966
Florin Corasc97a7392017-11-05 23:07:07 -0800967 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -0700968 {
Florin Corasc97a7392017-11-05 23:07:07 -0800969 u8 *tag = 0;
970 /* *INDENT-OFF* */
971 srt16 = &srt->session_rules_tables_16;
972 pool_foreach (rule16, srt16->rules, ({
973 ri = mma_rules_table_rule_index_16 (srt16, rule16);
974 tag = session_rules_table_rule_tag (srt, ri, 1);
975 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800976 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -0800977 }));
978 /* *INDENT-ON* */
979 }
980 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
981 {
982 u8 *tag = 0;
983 /* *INDENT-OFF* */
984 srt40 = &srt->session_rules_tables_40;
985 pool_foreach (rule40, srt40->rules, ({
986 ri = mma_rules_table_rule_index_40 (srt40, rule40);
987 tag = session_rules_table_rule_tag (srt, ri, 1);
988 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800989 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -0800990 }));
991 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -0700992 }
993}
994
995static void
Neale Ranns2b202bc2020-09-21 08:17:51 +0000996vl_api_session_rules_dump_t_handler (vl_api_session_rules_dump_t * mp)
Florin Coras6c36f532017-11-03 18:32:34 -0700997{
Florin Coras6c4dae22018-01-09 06:39:23 -0800998 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -0700999 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001000 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001001
Florin Coras6c4dae22018-01-09 06:39:23 -08001002 reg = vl_api_client_index_to_registration (mp->client_index);
1003 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001004 return;
1005
1006 /* *INDENT-OFF* */
1007 session_table_foreach (st, ({
Florin Coras07063b82020-03-13 04:44:51 +00001008 for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++)
Florin Corasc97a7392017-11-05 23:07:07 -08001009 {
1010 send_session_rules_table_details (&st->session_rules[tp],
1011 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001012 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001013 mp->context);
1014 }
Florin Coras6c36f532017-11-03 18:32:34 -07001015 }));
1016 /* *INDENT-ON* */
1017}
1018
Florin Coras371ca502018-02-21 12:07:41 -08001019static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001020vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001021{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001022 vl_api_app_add_cert_key_pair_reply_t *rmp;
1023 vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1024 u32 certkey_len, key_len, cert_len;
Florin Coras371ca502018-02-21 12:07:41 -08001025 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001026 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001027 {
1028 rv = VNET_API_ERROR_FEATURE_DISABLED;
1029 goto done;
1030 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001031
Florin Coras371ca502018-02-21 12:07:41 -08001032 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001033 if (cert_len > 10000)
1034 {
1035 rv = VNET_API_ERROR_INVALID_VALUE;
1036 goto done;
1037 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001038
1039 certkey_len = clib_net_to_host_u16 (mp->certkey_len);
1040 if (certkey_len < cert_len)
Florin Coras371ca502018-02-21 12:07:41 -08001041 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001042 rv = VNET_API_ERROR_INVALID_VALUE;
1043 goto done;
Florin Coras371ca502018-02-21 12:07:41 -08001044 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001045
1046 key_len = certkey_len - cert_len;
1047 if (key_len > 10000)
1048 {
1049 rv = VNET_API_ERROR_INVALID_VALUE;
1050 goto done;
1051 }
1052
1053 clib_memset (a, 0, sizeof (*a));
1054 vec_validate (a->cert, cert_len);
1055 vec_validate (a->key, key_len);
1056 clib_memcpy_fast (a->cert, mp->certkey, cert_len);
1057 clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len);
1058 rv = vnet_app_add_cert_key_pair (a);
Florin Coras371ca502018-02-21 12:07:41 -08001059 vec_free (a->cert);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001060 vec_free (a->key);
1061
Florin Coras371ca502018-02-21 12:07:41 -08001062done:
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001063 /* *INDENT-OFF* */
1064 REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
1065 if (!rv)
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001066 rmp->index = clib_host_to_net_u32 (a->index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001067 }));
1068 /* *INDENT-ON* */
Florin Coras371ca502018-02-21 12:07:41 -08001069}
1070
1071static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001072vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001073{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001074 vl_api_app_del_cert_key_pair_reply_t *rmp;
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001075 u32 ckpair_index;
Florin Coras371ca502018-02-21 12:07:41 -08001076 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001077 if (session_main_is_enabled () == 0)
1078 {
1079 rv = VNET_API_ERROR_FEATURE_DISABLED;
1080 goto done;
1081 }
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001082 ckpair_index = clib_net_to_host_u32 (mp->index);
1083 rv = vnet_app_del_cert_key_pair (ckpair_index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001084
1085done:
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001086 REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001087}
1088
1089/* ### WILL BE DEPRECATED POST 20.01 ### */
1090static void
1091vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1092 mp)
1093{
1094 vl_api_application_tls_cert_add_reply_t *rmp;
1095 app_cert_key_pair_t *ckpair;
1096 application_t *app;
1097 u32 cert_len;
1098 int rv = 0;
1099 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001100 {
1101 rv = VNET_API_ERROR_FEATURE_DISABLED;
1102 goto done;
1103 }
Florin Corase3e2f072018-03-04 07:24:30 -08001104 if (!(app = application_lookup (mp->client_index)))
1105 {
1106 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1107 goto done;
1108 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001109 cert_len = clib_net_to_host_u16 (mp->cert_len);
1110 if (cert_len > 10000)
1111 {
1112 rv = VNET_API_ERROR_INVALID_VALUE;
1113 goto done;
1114 }
1115 ckpair = app_cert_key_pair_get_default ();
1116 vec_validate (ckpair->cert, cert_len);
1117 clib_memcpy_fast (ckpair->cert, mp->cert, cert_len);
1118
1119done:
1120 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1121}
1122
1123/* ### WILL BE DEPRECATED POST 20.01 ### */
1124static void
1125vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1126 mp)
1127{
1128 vl_api_application_tls_key_add_reply_t *rmp;
1129 app_cert_key_pair_t *ckpair;
1130 application_t *app;
1131 u32 key_len;
1132 int rv = 0;
1133 if (session_main_is_enabled () == 0)
1134 {
1135 rv = VNET_API_ERROR_FEATURE_DISABLED;
1136 goto done;
1137 }
1138 if (!(app = application_lookup (mp->client_index)))
1139 {
1140 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1141 goto done;
1142 }
Florin Coras371ca502018-02-21 12:07:41 -08001143 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001144 if (key_len > 10000)
1145 {
1146 rv = VNET_API_ERROR_INVALID_VALUE;
1147 goto done;
1148 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001149 ckpair = app_cert_key_pair_get_default ();
1150 vec_validate (ckpair->key, key_len);
1151 clib_memcpy_fast (ckpair->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001152done:
1153 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1154}
1155
Florin Coras6cf30ad2017-04-04 23:08:23 -07001156static clib_error_t *
1157application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001158{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001159 application_t *app = application_lookup (client_index);
1160 vnet_app_detach_args_t _a, *a = &_a;
1161 if (app)
1162 {
Florin Coras15531972018-08-12 23:50:53 -07001163 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001164 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001165 vnet_application_detach (a);
1166 }
1167 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001168}
1169
Florin Coras6cf30ad2017-04-04 23:08:23 -07001170VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001171
1172#define vl_msg_name_crc_list
1173#include <vnet/vnet_all_api_h.h>
1174#undef vl_msg_name_crc_list
1175
1176static void
1177setup_message_id_table (api_main_t * am)
1178{
1179#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1180 foreach_vl_msg_name_crc_session;
1181#undef _
1182}
1183
1184/*
1185 * session_api_hookup
1186 * Add uri's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -05001187 * vlib has already mapped shared memory and
Dave Barach68b0fb02017-02-28 15:15:56 -05001188 * added the client registration handlers.
1189 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1190 */
1191static clib_error_t *
1192session_api_hookup (vlib_main_t * vm)
1193{
Dave Barach39d69112019-11-27 11:42:13 -05001194 api_main_t *am = vlibapi_get_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001195
1196#define _(N,n) \
1197 vl_msg_api_set_handlers(VL_API_##N, #n, \
1198 vl_api_##n##_t_handler, \
1199 vl_noop_handler, \
1200 vl_api_##n##_t_endian, \
1201 vl_api_##n##_t_print, \
1202 sizeof(vl_api_##n##_t), 1);
1203 foreach_session_api_msg;
1204#undef _
1205
1206 /*
Dave Barach68b0fb02017-02-28 15:15:56 -05001207 * Set up the (msg_name, crc, message-id) table
1208 */
1209 setup_message_id_table (am);
1210
1211 return 0;
1212}
1213
1214VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001215
Dave Barach68b0fb02017-02-28 15:15:56 -05001216/*
Florin Coras61ae0562020-09-02 19:10:28 -07001217 * Socket api functions
1218 */
1219
1220static void
1221sapi_send_fds (app_worker_t * app_wrk, int *fds, int n_fds)
1222{
1223 app_sapi_msg_t smsg = { 0 };
1224 app_namespace_t *app_ns;
1225 application_t *app;
1226 clib_socket_t *cs;
1227 u32 cs_index;
1228
1229 app = application_get (app_wrk->app_index);
1230 app_ns = app_namespace_get (app->ns_index);
1231 cs_index = appns_sapi_handle_sock_index (app_wrk->api_client_index);
1232 cs = appns_sapi_get_socket (app_ns, cs_index);
Florin Coras1c0573d2020-09-23 10:31:27 -07001233 if (PREDICT_FALSE (!cs))
1234 return;
Florin Coras61ae0562020-09-02 19:10:28 -07001235
1236 /* There's no payload for the message only the type */
1237 smsg.type = APP_SAPI_MSG_TYPE_SEND_FDS;
1238 clib_socket_sendmsg (cs, &smsg, sizeof (smsg), fds, n_fds);
1239}
1240
1241static int
1242mq_send_add_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1243{
1244 int fds[SESSION_N_FD_TYPE], n_fds = 0;
1245 svm_msg_q_msg_t _msg, *msg = &_msg;
1246 session_app_add_segment_msg_t *mp;
1247 app_worker_t *app_wrk;
1248 session_event_t *evt;
1249 svm_msg_q_t *app_mq;
1250 fifo_segment_t *fs;
1251 ssvm_private_t *sp;
1252 u8 fd_flags = 0;
1253
1254 app_wrk = app_worker_get (app_wrk_index);
1255
1256 fs = segment_manager_get_segment_w_handle (segment_handle);
1257 sp = &fs->ssvm;
1258 ASSERT (ssvm_type (sp) == SSVM_SEGMENT_MEMFD);
1259
1260 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1261 fds[n_fds] = sp->fd;
1262 n_fds += 1;
1263
1264 app_mq = app_wrk->event_queue;
1265 if (mq_try_lock_and_alloc_msg (app_mq, msg))
1266 return -1;
1267
1268 /*
1269 * Send the fd over api socket
1270 */
1271 sapi_send_fds (app_wrk, fds, n_fds);
1272
1273 /*
1274 * Send the actual message over mq
1275 */
1276 evt = svm_msg_q_msg_data (app_mq, msg);
1277 clib_memset (evt, 0, sizeof (*evt));
1278 evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT;
1279 mp = (session_app_add_segment_msg_t *) evt->data;
1280 clib_memset (mp, 0, sizeof (*mp));
1281 mp->segment_size = sp->ssvm_size;
1282 mp->fd_flags = fd_flags;
1283 mp->segment_handle = segment_handle;
1284 strncpy ((char *) mp->segment_name, (char *) sp->name,
1285 sizeof (mp->segment_name) - 1);
1286
1287 svm_msg_q_add_and_unlock (app_mq, msg);
1288
1289 return 0;
1290}
1291
1292static int
1293mq_send_del_segment_sapi_cb (u32 app_wrk_index, u64 segment_handle)
1294{
1295 svm_msg_q_msg_t _msg, *msg = &_msg;
1296 session_app_del_segment_msg_t *mp;
1297 app_worker_t *app_wrk;
1298 session_event_t *evt;
1299 svm_msg_q_t *app_mq;
1300
1301 app_wrk = app_worker_get (app_wrk_index);
1302
1303 app_mq = app_wrk->event_queue;
1304 if (mq_try_lock_and_alloc_msg (app_mq, msg))
1305 return -1;
1306
1307 evt = svm_msg_q_msg_data (app_mq, msg);
1308 clib_memset (evt, 0, sizeof (*evt));
1309 evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT;
1310 mp = (session_app_del_segment_msg_t *) evt->data;
1311 clib_memset (mp, 0, sizeof (*mp));
1312 mp->segment_handle = segment_handle;
1313 svm_msg_q_add_and_unlock (app_mq, msg);
1314
1315 return 0;
1316}
1317
1318static session_cb_vft_t session_mq_sapi_cb_vft = {
1319 .session_accept_callback = mq_send_session_accepted_cb,
1320 .session_disconnect_callback = mq_send_session_disconnected_cb,
1321 .session_connected_callback = mq_send_session_connected_cb,
1322 .session_reset_callback = mq_send_session_reset_cb,
1323 .session_migrate_callback = mq_send_session_migrate_cb,
1324 .session_cleanup_callback = mq_send_session_cleanup_cb,
1325 .add_segment_callback = mq_send_add_segment_sapi_cb,
1326 .del_segment_callback = mq_send_del_segment_sapi_cb,
1327};
1328
1329static void
1330session_api_attach_handler (app_namespace_t * app_ns, clib_socket_t * cs,
1331 app_sapi_attach_msg_t * mp)
1332{
1333 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1334 vnet_app_attach_args_t _a, *a = &_a;
1335 app_sapi_attach_reply_msg_t *rmp;
1336 ssvm_private_t *evt_q_segment;
1337 u8 fd_flags = 0, ctrl_thread;
1338 app_ns_api_handle_t *handle;
1339 app_sapi_msg_t msg = { 0 };
1340 app_worker_t *app_wrk;
1341 svm_msg_q_t *ctrl_mq;
1342 application_t *app;
1343
1344 /* Make sure name is null terminated */
1345 mp->name[63] = 0;
1346
1347 clib_memset (a, 0, sizeof (*a));
1348 a->api_client_index = appns_sapi_socket_handle (app_ns, cs);
1349 a->name = format (0, "%s", (char *) mp->name);
1350 a->options = mp->options;
1351 a->session_cb_vft = &session_mq_sapi_cb_vft;
1352 a->use_sock_api = 1;
1353 a->options[APP_OPTIONS_NAMESPACE] = app_namespace_index (app_ns);
1354
1355 if ((rv = vnet_application_attach (a)))
1356 {
1357 clib_warning ("attach returned: %d", rv);
1358 goto done;
1359 }
1360
1361 /* Send event queues segment */
1362 if ((evt_q_segment = session_main_get_evt_q_segment ()))
1363 {
1364 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
1365 fds[n_fds] = evt_q_segment->fd;
1366 n_fds += 1;
1367 }
1368 /* Send fifo segment fd if needed */
1369 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
1370 {
1371 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1372 fds[n_fds] = a->segment->fd;
1373 n_fds += 1;
1374 }
1375 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
1376 {
1377 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1378 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
1379 n_fds += 1;
1380 }
1381
1382done:
1383
1384 msg.type = APP_SAPI_MSG_TYPE_ATTACH_REPLY;
1385 rmp = &msg.attach_reply;
1386 rmp->retval = rv;
1387 if (!rv)
1388 {
1389 ctrl_thread = vlib_num_workers ()? 1 : 0;
1390 ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
1391 rmp->app_index = a->app_index;
1392 rmp->app_mq = pointer_to_uword (a->app_evt_q);
1393 rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
1394 rmp->vpp_ctrl_mq_thread = ctrl_thread;
1395 rmp->n_fds = n_fds;
1396 rmp->fd_flags = fd_flags;
1397 /* No segment name and size since we only support memfds
1398 * in this configuration */
1399 rmp->segment_handle = a->segment_handle;
1400 rmp->api_client_handle = a->api_client_index;
1401
1402 /* Update app index for socket */
1403 handle = (app_ns_api_handle_t *) & cs->private_data;
1404 app = application_get (a->app_index);
1405 app_wrk = application_get_worker (app, 0);
1406 handle->aah_app_wrk_index = app_wrk->wrk_index;
1407 }
1408
1409 clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1410 vec_free (a->name);
1411}
1412
1413static void
1414sapi_socket_close_w_handle (u32 api_handle)
1415{
1416 app_namespace_t *app_ns = app_namespace_get (api_handle >> 16);
1417 u16 sock_index = api_handle & 0xffff;
1418 app_ns_api_handle_t *handle;
1419 clib_socket_t *cs;
1420 clib_file_t *cf;
1421
1422 cs = appns_sapi_get_socket (app_ns, sock_index);
1423 if (!cs)
1424 return;
1425
1426 handle = (app_ns_api_handle_t *) & cs->private_data;
1427 cf = clib_file_get (&file_main, handle->aah_file_index);
1428 clib_file_del (&file_main, cf);
1429
1430 clib_socket_close (cs);
1431 appns_sapi_free_socket (app_ns, cs);
1432}
1433
1434static void
1435sapi_add_del_worker_handler (app_namespace_t * app_ns,
1436 clib_socket_t * cs,
1437 app_sapi_worker_add_del_msg_t * mp)
1438{
1439 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1440 app_sapi_worker_add_del_reply_msg_t *rmp;
1441 app_ns_api_handle_t *handle;
1442 app_sapi_msg_t msg = { 0 };
1443 app_worker_t *app_wrk;
Florin Corascc7c88e2020-09-15 15:56:51 -07001444 u32 sapi_handle = -1;
Florin Coras61ae0562020-09-02 19:10:28 -07001445 application_t *app;
Florin Coras61ae0562020-09-02 19:10:28 -07001446 u8 fd_flags = 0;
1447
1448 app = application_get_if_valid (mp->app_index);
1449 if (!app)
1450 {
1451 rv = VNET_API_ERROR_INVALID_VALUE;
1452 goto done;
1453 }
1454
1455 sapi_handle = appns_sapi_socket_handle (app_ns, cs);
1456
1457 vnet_app_worker_add_del_args_t args = {
1458 .app_index = app->app_index,
1459 .wrk_map_index = mp->wrk_index,
1460 .api_client_index = sapi_handle,
1461 .is_add = mp->is_add
1462 };
1463 rv = vnet_app_worker_add_del (&args);
1464 if (rv)
1465 {
1466 clib_warning ("app worker add/del returned: %d", rv);
1467 goto done;
1468 }
1469
1470 if (!mp->is_add)
1471 {
1472 sapi_socket_close_w_handle (sapi_handle);
1473 goto done;
1474 }
1475
1476 /* Send fifo segment fd if needed */
1477 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1478 {
1479 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1480 fds[n_fds] = args.segment->fd;
1481 n_fds += 1;
1482 }
1483 if (application_segment_manager_properties (app)->use_mq_eventfd)
1484 {
1485 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1486 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1487 n_fds += 1;
1488 }
1489
1490done:
1491
1492 msg.type = APP_SAPI_MSG_TYPE_ADD_DEL_WORKER_REPLY;
1493 rmp = &msg.worker_add_del_reply;
1494 rmp->retval = rv;
1495 rmp->is_add = mp->is_add;
Florin Corascc7c88e2020-09-15 15:56:51 -07001496 rmp->api_client_handle = sapi_handle;
Florin Coras61ae0562020-09-02 19:10:28 -07001497 rmp->wrk_index = args.wrk_map_index;
1498 rmp->segment_handle = args.segment_handle;
1499 if (!rv && mp->is_add)
1500 {
1501 /* No segment name and size. This supports only memfds */
1502 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1503 rmp->n_fds = n_fds;
1504 rmp->fd_flags = fd_flags;
1505
1506 /* Update app index for socket */
1507 handle = (app_ns_api_handle_t *) & cs->private_data;
1508 app_wrk = application_get_worker (app, args.wrk_map_index);
1509 handle->aah_app_wrk_index = app_wrk->wrk_index;
1510 }
1511
1512 clib_socket_sendmsg (cs, &msg, sizeof (msg), fds, n_fds);
1513}
1514
1515static void
1516sapi_socket_detach (app_namespace_t * app_ns, clib_socket_t * cs)
1517{
Florin Coras61ae0562020-09-02 19:10:28 -07001518 app_ns_api_handle_t *handle;
1519 app_worker_t *app_wrk;
1520 u32 api_client_handle;
1521
1522 api_client_handle = appns_sapi_socket_handle (app_ns, cs);
1523 sapi_socket_close_w_handle (api_client_handle);
1524
Florin Corasf99a7d62020-09-14 10:29:29 -07001525 /* Cleanup everything because app worker closed socket or crashed */
Florin Coras61ae0562020-09-02 19:10:28 -07001526 handle = (app_ns_api_handle_t *) & cs->private_data;
1527 app_wrk = app_worker_get (handle->aah_app_wrk_index);
Florin Corasf99a7d62020-09-14 10:29:29 -07001528
1529 vnet_app_worker_add_del_args_t args = {
1530 .app_index = app_wrk->app_index,
1531 .wrk_map_index = app_wrk->wrk_map_index,
1532 .api_client_index = api_client_handle,
1533 .is_add = 0
1534 };
1535 /* Send rpc to main thread for worker barrier */
1536 vlib_rpc_call_main_thread (vnet_app_worker_add_del, (u8 *) & args,
1537 sizeof (args));
Florin Coras61ae0562020-09-02 19:10:28 -07001538}
1539
1540static clib_error_t *
1541sapi_sock_read_ready (clib_file_t * cf)
1542{
1543 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
Florin Coras7360e3d2020-09-18 16:15:47 -07001544 vlib_main_t *vm = vlib_get_main ();
Florin Coras61ae0562020-09-02 19:10:28 -07001545 app_sapi_msg_t msg = { 0 };
1546 app_namespace_t *app_ns;
1547 clib_error_t *err = 0;
1548 clib_socket_t *cs;
1549
1550 app_ns = app_namespace_get (handle->aah_app_ns_index);
1551 cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
1552 if (!cs)
1553 goto error;
1554
1555 err = clib_socket_recvmsg (cs, &msg, sizeof (msg), 0, 0);
1556 if (err)
1557 {
1558 clib_error_free (err);
1559 sapi_socket_detach (app_ns, cs);
1560 goto error;
1561 }
1562
1563 handle = (app_ns_api_handle_t *) & cs->private_data;
1564
Florin Coras7360e3d2020-09-18 16:15:47 -07001565 vlib_worker_thread_barrier_sync (vm);
1566
Florin Coras61ae0562020-09-02 19:10:28 -07001567 switch (msg.type)
1568 {
1569 case APP_SAPI_MSG_TYPE_ATTACH:
1570 session_api_attach_handler (app_ns, cs, &msg.attach);
1571 break;
1572 case APP_SAPI_MSG_TYPE_ADD_DEL_WORKER:
1573 sapi_add_del_worker_handler (app_ns, cs, &msg.worker_add_del);
1574 break;
1575 default:
1576 clib_warning ("app wrk %u unknown message type: %u",
1577 handle->aah_app_wrk_index, msg.type);
1578 break;
1579 }
1580
Florin Coras7360e3d2020-09-18 16:15:47 -07001581 vlib_worker_thread_barrier_release (vm);
1582
Florin Coras61ae0562020-09-02 19:10:28 -07001583error:
1584 return 0;
1585}
1586
1587static clib_error_t *
1588sapi_sock_write_ready (clib_file_t * cf)
1589{
1590 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1591 clib_warning ("called for app ns %u", handle->aah_app_ns_index);
1592 return 0;
1593}
1594
1595static clib_error_t *
1596sapi_sock_error (clib_file_t * cf)
1597{
1598 app_ns_api_handle_t *handle = (app_ns_api_handle_t *) & cf->private_data;
1599 app_namespace_t *app_ns;
1600 clib_socket_t *cs;
1601
1602 app_ns = app_namespace_get (handle->aah_app_ns_index);
1603 cs = appns_sapi_get_socket (app_ns, handle->aah_sock_index);
1604 if (!cs)
1605 return 0;
1606
1607 sapi_socket_detach (app_ns, cs);
1608 return 0;
1609}
1610
1611static clib_error_t *
1612sapi_sock_accept_ready (clib_file_t * scf)
1613{
1614 app_ns_api_handle_t handle = *(app_ns_api_handle_t *) & scf->private_data;
1615 app_namespace_t *app_ns;
1616 clib_file_t cf = { 0 };
1617 clib_error_t *err = 0;
1618 clib_socket_t *ccs, *scs;
1619
1620 /* Listener files point to namespace */
1621 app_ns = app_namespace_get (handle.aah_app_ns_index);
1622
1623 /*
1624 * Initialize client socket
1625 */
1626 ccs = appns_sapi_alloc_socket (app_ns);
1627
1628 /* Grab server socket after client is initialized */
1629 scs = appns_sapi_get_socket (app_ns, handle.aah_sock_index);
1630 if (!scs)
1631 goto error;
1632
1633 err = clib_socket_accept (scs, ccs);
1634 if (err)
1635 {
1636 clib_error_report (err);
1637 goto error;
1638 }
1639
1640 cf.read_function = sapi_sock_read_ready;
1641 cf.write_function = sapi_sock_write_ready;
1642 cf.error_function = sapi_sock_error;
1643 cf.file_descriptor = ccs->fd;
1644 /* File points to app namespace and socket */
1645 handle.aah_sock_index = appns_sapi_socket_index (app_ns, ccs);
Florin Coras6b6c10b2020-09-24 11:58:28 -07001646 cf.private_data = handle.as_u64;
Florin Coras61ae0562020-09-02 19:10:28 -07001647 cf.description = format (0, "app sock conn fd: %d", ccs->fd);
1648
1649 /* Poll until we get an attach message. Socket points to file and
1650 * application that owns the socket */
1651 handle.aah_app_wrk_index = APP_INVALID_INDEX;
1652 handle.aah_file_index = clib_file_add (&file_main, &cf);
Florin Coras6b6c10b2020-09-24 11:58:28 -07001653 ccs->private_data = handle.as_u64;
Florin Coras61ae0562020-09-02 19:10:28 -07001654
1655 return err;
1656
1657error:
1658 appns_sapi_free_socket (app_ns, ccs);
1659 return err;
1660}
1661
1662int
1663appns_sapi_add_ns_socket (app_namespace_t * app_ns)
1664{
1665 char *subdir = "/app_ns_sockets/";
1666 app_ns_api_handle_t *handle;
1667 clib_file_t cf = { 0 };
1668 struct stat file_stat;
1669 clib_error_t *err;
1670 clib_socket_t *cs;
1671 u8 *dir = 0;
1672 int rv = 0;
1673
1674 vec_add (dir, vlib_unix_get_runtime_dir (),
1675 strlen (vlib_unix_get_runtime_dir ()));
1676 vec_add (dir, (u8 *) subdir, strlen (subdir));
1677
1678 err = vlib_unix_recursive_mkdir ((char *) dir);
1679 if (err)
1680 {
1681 clib_error_report (err);
1682 rv = -1;
1683 goto error;
1684 }
1685
1686 app_ns->sock_name = format (0, "%v%v%c", dir, app_ns->ns_id, 0);
1687
1688 /*
1689 * Create and initialize socket to listen on
1690 */
1691 cs = appns_sapi_alloc_socket (app_ns);
1692 cs->config = (char *) app_ns->sock_name;
1693 cs->flags = CLIB_SOCKET_F_IS_SERVER |
1694 CLIB_SOCKET_F_ALLOW_GROUP_WRITE |
1695 CLIB_SOCKET_F_SEQPACKET | CLIB_SOCKET_F_PASSCRED;
1696
1697 if ((err = clib_socket_init (cs)))
1698 {
1699 clib_error_report (err);
1700 rv = -1;
1701 goto error;
1702 }
1703
1704 if (stat ((char *) app_ns->sock_name, &file_stat) == -1)
1705 {
1706 rv = -1;
1707 goto error;
1708 }
1709
1710 /*
1711 * Start polling it
1712 */
1713 cf.read_function = sapi_sock_accept_ready;
1714 cf.file_descriptor = cs->fd;
1715 /* File points to namespace */
1716 handle = (app_ns_api_handle_t *) & cf.private_data;
1717 handle->aah_app_ns_index = app_namespace_index (app_ns);
1718 handle->aah_sock_index = appns_sapi_socket_index (app_ns, cs);
1719 cf.description = format (0, "app sock listener: %s", app_ns->sock_name);
1720
1721 /* Socket points to clib file index */
1722 handle = (app_ns_api_handle_t *) & cs->private_data;
1723 handle->aah_file_index = clib_file_add (&file_main, &cf);
1724 handle->aah_app_wrk_index = APP_INVALID_INDEX;
1725
1726error:
1727 vec_free (dir);
1728 return rv;
1729}
1730
1731/*
Dave Barach68b0fb02017-02-28 15:15:56 -05001732 * fd.io coding-style-patch-verification: ON
1733 *
1734 * Local Variables:
1735 * eval: (c-set-style "gnu")
1736 * End:
1737 */