blob: 43ecf2b07e94516883722fba2937d246f5b3a197 [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;
69 case TRANSPORT_PROTO_API_UDPC:
70 return TRANSPORT_PROTO_UDPC;
71 case TRANSPORT_PROTO_API_QUIC:
72 return TRANSPORT_PROTO_QUIC;
73 default:
74 return TRANSPORT_PROTO_NONE;
75 }
76}
77
78static vl_api_transport_proto_t
79api_session_transport_proto_encode (const transport_proto_t tp)
80{
81 switch (tp)
82 {
83 case TRANSPORT_PROTO_TCP:
84 return TRANSPORT_PROTO_API_TCP;
85 case TRANSPORT_PROTO_UDP:
86 return TRANSPORT_PROTO_API_UDP;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +010087 case TRANSPORT_PROTO_TLS:
88 return TRANSPORT_PROTO_API_TLS;
89 case TRANSPORT_PROTO_UDPC:
90 return TRANSPORT_PROTO_API_UDPC;
91 case TRANSPORT_PROTO_QUIC:
92 return TRANSPORT_PROTO_API_QUIC;
93 default:
94 return TRANSPORT_PROTO_API_NONE;
95 }
96}
97
Dave Barach68b0fb02017-02-28 15:15:56 -050098static int
Florin Coras99368312018-08-02 10:45:44 -070099session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
Florin Corasb384b542018-01-15 01:08:33 -0800100{
101 clib_error_t *error;
102 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
Florin Coras00e01d32019-10-21 16:07:46 -0700103 return SESSION_E_BAPI_NO_FD;
Florin Coras99368312018-08-02 10:45:44 -0700104 error = vl_api_send_fd_msg (reg, fds, n_fds);
Florin Corasb384b542018-01-15 01:08:33 -0800105 if (error)
106 {
107 clib_error_report (error);
Florin Coras00e01d32019-10-21 16:07:46 -0700108 return SESSION_E_BAPI_SEND_FD;
Florin Corasb384b542018-01-15 01:08:33 -0800109 }
110 return 0;
111}
112
Florin Coras99368312018-08-02 10:45:44 -0700113static int
Florin Coras83ea6692018-10-12 16:55:14 -0700114mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
115{
116 int rv;
117 u8 try = 0;
118 while (try < 100)
119 {
120 rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
121 SESSION_MQ_CTRL_EVT_RING,
122 SVM_Q_NOWAIT, msg);
123 if (!rv)
124 return 0;
125 try++;
Florin Corase2ea1932018-12-17 23:08:14 -0800126 usleep (1);
Florin Coras83ea6692018-10-12 16:55:14 -0700127 }
Florin Corasdc2e2512018-12-03 17:47:26 -0800128 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700129 return -1;
130}
131
132static int
Florin Coras288eaab2019-02-03 15:26:14 -0800133mq_send_session_accepted_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700134{
Florin Coras15531972018-08-12 23:50:53 -0700135 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700136 svm_msg_q_msg_t _msg, *msg = &_msg;
137 svm_msg_q_t *vpp_queue, *app_mq;
Florin Coras288eaab2019-02-03 15:26:14 -0800138 session_t *listener;
Florin Coras52207f12018-07-12 14:48:06 -0700139 session_accepted_msg_t *mp;
140 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700141 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700142
Florin Coras15531972018-08-12 23:50:53 -0700143 app = application_get (app_wrk->app_index);
144 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700145 if (mq_try_lock_and_alloc_msg (app_mq, msg))
Florin Coras00e01d32019-10-21 16:07:46 -0700146 return SESSION_E_MQ_MSG_ALLOC;
Florin Coras52207f12018-07-12 14:48:06 -0700147
148 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400149 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700150 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
151 mp = (session_accepted_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800152 clib_memset (mp, 0, sizeof (*mp));
Florin Corasab2f6db2018-08-31 14:31:41 -0700153 mp->context = app->app_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800154 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
155 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800156 mp->segment_handle = session_segment_handle (s);
Nathan Skrzypczakc00f4802019-12-03 16:25:11 +0100157 mp->flags = s->flags;
Florin Coras52207f12018-07-12 14:48:06 -0700158
159 if (session_has_transport (s))
160 {
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200161 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras87d66332019-06-11 12:31:31 -0700162 mp->listener_handle = app_listen_session_handle (listener);
Florin Coras52207f12018-07-12 14:48:06 -0700163 if (application_is_proxy (app))
164 {
165 listener =
Florin Coras15531972018-08-12 23:50:53 -0700166 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
167 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700168 if (listener)
169 mp->listener_handle = listen_session_get_handle (listener);
170 }
Florin Coras31c99552019-03-01 13:00:58 -0800171 vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700172 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
173 mp->handle = session_handle (s);
Aloys Augustincdb71702019-04-08 17:54:39 +0200174
Florin Coras09d18c22019-04-24 11:10:02 -0700175 session_get_endpoint (s, &mp->rmt, 0 /* is_lcl */ );
Florin Coras52207f12018-07-12 14:48:06 -0700176 }
177 else
178 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800179 ct_connection_t *ct;
Florin Coras99368312018-08-02 10:45:44 -0700180
Florin Coras2b81e3c2019-02-27 07:55:46 -0800181 ct = (ct_connection_t *) session_get_transport (s);
Nathan Skrzypczak2f0f96b2019-06-13 10:14:28 +0200182 listener = listen_session_get_from_handle (s->listener_handle);
Florin Coras87d66332019-06-11 12:31:31 -0700183 mp->listener_handle = app_listen_session_handle (listener);
Florin Coras09d18c22019-04-24 11:10:02 -0700184 mp->rmt.is_ip4 = session_type_is_ip4 (listener->session_type);
185 mp->rmt.port = ct->c_rmt_port;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800186 mp->handle = session_handle (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800187 vpp_queue = session_main_get_vpp_event_queue (0);
Florin Coras54693d22018-07-17 10:46:29 -0700188 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Coras52207f12018-07-12 14:48:06 -0700189 }
Florin Coras537b17e2018-09-28 10:35:45 -0700190 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700191
192 return 0;
193}
194
Florin Coras72b04282019-01-14 17:23:11 -0800195static inline void
196mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
197 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700198{
Florin Coras52207f12018-07-12 14:48:06 -0700199 svm_msg_q_msg_t _msg, *msg = &_msg;
200 session_disconnected_msg_t *mp;
201 svm_msg_q_t *app_mq;
202 session_event_t *evt;
203
Florin Coras15531972018-08-12 23:50:53 -0700204 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700205 if (mq_try_lock_and_alloc_msg (app_mq, msg))
206 return;
Florin Coras52207f12018-07-12 14:48:06 -0700207 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400208 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800209 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700210 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800211 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800212 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700213 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700214}
215
Florin Coras72b04282019-01-14 17:23:11 -0800216static inline void
217mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
218 svm_fifo_t * f, session_evt_type_t evt_type)
219{
220 app_worker_t *app_wrk;
221 application_t *app;
222 int i;
223
224 app = application_get (app_index);
225 if (!app)
226 return;
227
228 for (i = 0; i < f->n_subscribers; i++)
229 {
230 if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
231 continue;
232 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
233 }
234}
235
236static void
Florin Coras288eaab2019-02-03 15:26:14 -0800237mq_send_session_disconnected_cb (session_t * s)
Florin Coras72b04282019-01-14 17:23:11 -0800238{
239 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
240 session_handle_t sh = session_handle (s);
241
242 mq_send_session_close_evt (app_wrk, session_handle (s),
243 SESSION_CTRL_EVT_DISCONNECTED);
244
Florin Coras288eaab2019-02-03 15:26:14 -0800245 if (svm_fifo_n_subscribers (s->rx_fifo))
246 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800247 SESSION_CTRL_EVT_DISCONNECTED);
248}
249
Florin Coras52207f12018-07-12 14:48:06 -0700250static void
Florin Coras288eaab2019-02-03 15:26:14 -0800251mq_send_session_reset_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700252{
Florin Coras72b04282019-01-14 17:23:11 -0800253 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
254 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700255
Florin Coras72b04282019-01-14 17:23:11 -0800256 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
257
Florin Coras288eaab2019-02-03 15:26:14 -0800258 if (svm_fifo_n_subscribers (s->rx_fifo))
259 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800260 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700261}
262
Florin Coras458089b2019-08-21 16:20:44 -0700263int
Florin Coras15531972018-08-12 23:50:53 -0700264mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras00e01d32019-10-21 16:07:46 -0700265 session_t * s, session_error_t err)
Florin Coras52207f12018-07-12 14:48:06 -0700266{
267 svm_msg_q_msg_t _msg, *msg = &_msg;
268 session_connected_msg_t *mp;
269 svm_msg_q_t *vpp_mq, *app_mq;
270 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700271 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700272 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700273
Florin Coras15531972018-08-12 23:50:53 -0700274 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700275 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700276 if (!app_mq)
277 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800278 clib_warning ("app %u with api index: %u not attached",
279 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700280 return -1;
281 }
282
Florin Coras83ea6692018-10-12 16:55:14 -0700283 if (mq_try_lock_and_alloc_msg (app_mq, msg))
Florin Coras00e01d32019-10-21 16:07:46 -0700284 return SESSION_E_MQ_MSG_ALLOC;
Florin Corasdc2e2512018-12-03 17:47:26 -0800285
Florin Coras52207f12018-07-12 14:48:06 -0700286 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400287 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700288 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
289 mp = (session_connected_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800290 clib_memset (mp, 0, sizeof (*mp));
Florin Coras52207f12018-07-12 14:48:06 -0700291 mp->context = api_context;
292
Florin Coras00e01d32019-10-21 16:07:46 -0700293 if (err)
Florin Coras52207f12018-07-12 14:48:06 -0700294 goto done;
295
296 if (session_has_transport (s))
297 {
298 tc = session_get_transport (s);
299 if (!tc)
300 {
Florin Coras00e01d32019-10-21 16:07:46 -0700301 clib_warning ("failed to retrieve transport!");
302 err = SESSION_E_REFUSED;
Florin Coras52207f12018-07-12 14:48:06 -0700303 goto done;
304 }
305
Florin Coras31c99552019-03-01 13:00:58 -0800306 vpp_mq = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700307 mp->handle = session_handle (s);
308 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Aloys Augustincdb71702019-04-08 17:54:39 +0200309
Florin Coras09d18c22019-04-24 11:10:02 -0700310 session_get_endpoint (s, &mp->lcl, 1 /* is_lcl */ );
Aloys Augustincdb71702019-04-08 17:54:39 +0200311
Florin Coras288eaab2019-02-03 15:26:14 -0800312 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
313 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800314 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700315 }
316 else
317 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800318 ct_connection_t *cct;
319 session_t *ss;
Florin Coras99368312018-08-02 10:45:44 -0700320
Florin Coras2b81e3c2019-02-27 07:55:46 -0800321 cct = (ct_connection_t *) session_get_transport (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800322 mp->handle = session_handle (s);
Florin Coras09d18c22019-04-24 11:10:02 -0700323 mp->lcl.port = cct->c_lcl_port;
324 mp->lcl.is_ip4 = cct->c_is_ip4;
Florin Coras653e43f2019-03-04 10:56:23 -0800325 vpp_mq = session_main_get_vpp_event_queue (0);
Florin Coras54693d22018-07-17 10:46:29 -0700326 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras653e43f2019-03-04 10:56:23 -0800327 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
328 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
329 mp->segment_handle = session_segment_handle (s);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800330 ss = ct_session_get_peer (s);
Florin Coras653e43f2019-03-04 10:56:23 -0800331 mp->ct_rx_fifo = pointer_to_uword (ss->tx_fifo);
332 mp->ct_tx_fifo = pointer_to_uword (ss->rx_fifo);
333 mp->ct_segment_handle = session_segment_handle (ss);
Florin Coras52207f12018-07-12 14:48:06 -0700334 }
335
336done:
Florin Coras00e01d32019-10-21 16:07:46 -0700337 mp->retval = err;
Florin Coras52207f12018-07-12 14:48:06 -0700338
Florin Coras537b17e2018-09-28 10:35:45 -0700339 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700340 return 0;
341}
342
Florin Coras458089b2019-08-21 16:20:44 -0700343int
Florin Coras60116992018-08-27 09:52:18 -0700344mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
345 session_handle_t handle, int rv)
346{
347 svm_msg_q_msg_t _msg, *msg = &_msg;
348 svm_msg_q_t *app_mq, *vpp_evt_q;
Yu Ping0b819152019-05-07 02:24:30 +0800349 transport_endpoint_t tep;
Florin Coras60116992018-08-27 09:52:18 -0700350 session_bound_msg_t *mp;
351 app_worker_t *app_wrk;
352 session_event_t *evt;
Florin Corasc9940fc2019-02-05 20:55:11 -0800353 app_listener_t *al;
354 session_t *ls = 0;
Florin Coras60116992018-08-27 09:52:18 -0700355 app_wrk = app_worker_get (app_wrk_index);
Florin Coras60116992018-08-27 09:52:18 -0700356 app_mq = app_wrk->event_queue;
357 if (!app_mq)
358 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800359 clib_warning ("app %u with api index: %u not attached",
360 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700361 return -1;
362 }
363
Florin Coras83ea6692018-10-12 16:55:14 -0700364 if (mq_try_lock_and_alloc_msg (app_mq, msg))
Florin Coras00e01d32019-10-21 16:07:46 -0700365 return SESSION_E_MQ_MSG_ALLOC;
Florin Coras83ea6692018-10-12 16:55:14 -0700366
Florin Coras60116992018-08-27 09:52:18 -0700367 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400368 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700369 evt->event_type = SESSION_CTRL_EVT_BOUND;
370 mp = (session_bound_msg_t *) evt->data;
371 mp->context = api_context;
372
373 if (rv)
374 goto done;
375
376 mp->handle = handle;
Florin Corasd4295e62019-02-22 13:11:38 -0800377 al = app_listener_get_w_handle (handle);
378 if (al->session_index != SESSION_INVALID_INDEX)
379 ls = app_listener_get_session (al);
Florin Coras60116992018-08-27 09:52:18 -0700380 else
Florin Corasd4295e62019-02-22 13:11:38 -0800381 ls = app_listener_get_local_session (al);
Yu Ping0b819152019-05-07 02:24:30 +0800382
383 session_get_endpoint (ls, &tep, 1 /* is_lcl */ );
384 mp->lcl_port = tep.port;
385 mp->lcl_is_ip4 = tep.is_ip4;
386 clib_memcpy_fast (mp->lcl_ip, &tep.ip, sizeof (tep.ip));
Florin Coras60116992018-08-27 09:52:18 -0700387
Florin Coras31c99552019-03-01 13:00:58 -0800388 vpp_evt_q = session_main_get_vpp_event_queue (0);
Florin Coras5f45e012019-01-23 09:21:30 -0800389 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
390
Florin Coras2b81e3c2019-02-27 07:55:46 -0800391 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras60116992018-08-27 09:52:18 -0700392 {
Florin Coras288eaab2019-02-03 15:26:14 -0800393 mp->rx_fifo = pointer_to_uword (ls->rx_fifo);
394 mp->tx_fifo = pointer_to_uword (ls->tx_fifo);
Florin Coras60116992018-08-27 09:52:18 -0700395 }
396
397done:
398 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700399 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700400 return 0;
401}
402
Florin Coras458089b2019-08-21 16:20:44 -0700403void
404mq_send_unlisten_reply (app_worker_t * app_wrk, session_handle_t sh,
405 u32 context, int rv)
406{
407 svm_msg_q_msg_t _msg, *msg = &_msg;
408 session_unlisten_reply_msg_t *ump;
409 svm_msg_q_t *app_mq;
410 session_event_t *evt;
411
412 app_mq = app_wrk->event_queue;
413 if (mq_try_lock_and_alloc_msg (app_mq, msg))
414 return;
415
416 evt = svm_msg_q_msg_data (app_mq, msg);
417 clib_memset (evt, 0, sizeof (*evt));
418 evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY;
419 ump = (session_unlisten_reply_msg_t *) evt->data;
420 ump->context = context;
421 ump->handle = sh;
422 ump->retval = rv;
423 svm_msg_q_add_and_unlock (app_mq, msg);
424}
425
Florin Coras49568af2019-07-31 16:46:24 -0700426static void
427mq_send_session_migrate_cb (session_t * s, session_handle_t new_sh)
428{
Florin Coras68b7e582020-01-21 18:33:23 -0800429 svm_msg_q_msg_t _msg, *msg = &_msg;
430 session_migrated_msg_t *mp;
431 svm_msg_q_t *vpp_evt_q;
432 app_worker_t *app_wrk;
433 session_event_t *evt;
434 svm_msg_q_t *app_mq;
435
436 app_wrk = app_worker_get (s->app_wrk_index);
437 app_mq = app_wrk->event_queue;
438 if (mq_try_lock_and_alloc_msg (app_mq, msg))
439 return;
440
441 evt = svm_msg_q_msg_data (app_mq, msg);
442 clib_memset (evt, 0, sizeof (*evt));
443 evt->event_type = SESSION_CTRL_EVT_MIGRATED;
444 mp = (session_migrated_msg_t *) evt->data;
445 mp->handle = session_handle (s);
446 mp->new_handle = new_sh;
447 mp->vpp_thread_index = session_thread_from_handle (new_sh);
448 vpp_evt_q = session_main_get_vpp_event_queue (mp->vpp_thread_index);
449 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
450 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras49568af2019-07-31 16:46:24 -0700451}
452
Florin Corasc4c4cf52019-08-24 18:17:34 -0700453static int
454mq_send_add_segment_cb (u32 app_wrk_index, u64 segment_handle)
455{
456 int fds[SESSION_N_FD_TYPE], n_fds = 0;
457 svm_msg_q_msg_t _msg, *msg = &_msg;
458 session_app_add_segment_msg_t *mp;
459 vl_api_registration_t *reg;
460 app_worker_t *app_wrk;
461 session_event_t *evt;
462 svm_msg_q_t *app_mq;
463 fifo_segment_t *fs;
464 ssvm_private_t *sp;
465 u8 fd_flags = 0;
466
467 app_wrk = app_worker_get (app_wrk_index);
468
469 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
470 if (!reg)
471 {
472 clib_warning ("no api registration for client: %u",
473 app_wrk->api_client_index);
474 return -1;
475 }
476
477 fs = segment_manager_get_segment_w_handle (segment_handle);
478 sp = &fs->ssvm;
479 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
480 {
481 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
482 {
483 clib_warning ("can't send memfd fd");
484 return -1;
485 }
486
487 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
488 fds[n_fds] = sp->fd;
489 n_fds += 1;
490 }
491
492 app_mq = app_wrk->event_queue;
493 if (mq_try_lock_and_alloc_msg (app_mq, msg))
494 return -1;
495
496 if (n_fds)
497 session_send_fds (reg, fds, n_fds);
498
499 evt = svm_msg_q_msg_data (app_mq, msg);
500 clib_memset (evt, 0, sizeof (*evt));
501 evt->event_type = SESSION_CTRL_EVT_APP_ADD_SEGMENT;
502 mp = (session_app_add_segment_msg_t *) evt->data;
503 clib_memset (mp, 0, sizeof (*mp));
504 mp->segment_size = sp->ssvm_size;
505 mp->fd_flags = fd_flags;
506 mp->segment_handle = segment_handle;
507 strncpy ((char *) mp->segment_name, (char *) sp->name,
508 sizeof (mp->segment_name) - 1);
509
510 svm_msg_q_add_and_unlock (app_mq, msg);
511
512 return 0;
513}
514
515static int
516mq_send_del_segment_cb (u32 app_wrk_index, u64 segment_handle)
517{
518 svm_msg_q_msg_t _msg, *msg = &_msg;
519 session_app_del_segment_msg_t *mp;
520 vl_api_registration_t *reg;
521 app_worker_t *app_wrk;
522 session_event_t *evt;
523 svm_msg_q_t *app_mq;
524
525 app_wrk = app_worker_get (app_wrk_index);
526 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
527 if (!reg)
528 {
529 clib_warning ("no registration: %u", app_wrk->api_client_index);
530 return -1;
531 }
532
533 app_mq = app_wrk->event_queue;
534 if (mq_try_lock_and_alloc_msg (app_mq, msg))
535 return -1;
536
537 evt = svm_msg_q_msg_data (app_mq, msg);
538 clib_memset (evt, 0, sizeof (*evt));
539 evt->event_type = SESSION_CTRL_EVT_APP_DEL_SEGMENT;
540 mp = (session_app_del_segment_msg_t *) evt->data;
541 clib_memset (mp, 0, sizeof (*mp));
542 mp->segment_handle = segment_handle;
543 svm_msg_q_add_and_unlock (app_mq, msg);
544
545 return 0;
546}
547
Florin Coras9ace36d2019-10-28 13:14:17 -0700548static void
549mq_send_session_cleanup_cb (session_t * s, session_cleanup_ntf_t ntf)
550{
551 svm_msg_q_msg_t _msg, *msg = &_msg;
552 session_cleanup_msg_t *mp;
553 svm_msg_q_t *app_mq;
554 session_event_t *evt;
555 app_worker_t *app_wrk;
556
557 /* Only propagate session cleanup notification */
558 if (ntf == SESSION_CLEANUP_TRANSPORT)
559 return;
560
561 app_wrk = app_worker_get_if_valid (s->app_wrk_index);
562 if (!app_wrk)
563 return;
564
565 app_mq = app_wrk->event_queue;
566 if (mq_try_lock_and_alloc_msg (app_mq, msg))
567 return;
568
569 evt = svm_msg_q_msg_data (app_mq, msg);
570 clib_memset (evt, 0, sizeof (*evt));
571 evt->event_type = SESSION_CTRL_EVT_CLEANUP;
572 mp = (session_cleanup_msg_t *) evt->data;
573 mp->handle = session_handle (s);
574 svm_msg_q_add_and_unlock (app_mq, msg);
575}
576
Florin Corasc4c4cf52019-08-24 18:17:34 -0700577static session_cb_vft_t session_mq_cb_vft = {
578 .session_accept_callback = mq_send_session_accepted_cb,
579 .session_disconnect_callback = mq_send_session_disconnected_cb,
580 .session_connected_callback = mq_send_session_connected_cb,
581 .session_reset_callback = mq_send_session_reset_cb,
582 .session_migrate_callback = mq_send_session_migrate_cb,
Florin Coras9ace36d2019-10-28 13:14:17 -0700583 .session_cleanup_callback = mq_send_session_cleanup_cb,
Florin Corasc4c4cf52019-08-24 18:17:34 -0700584 .add_segment_callback = mq_send_add_segment_cb,
585 .del_segment_callback = mq_send_del_segment_cb,
586};
587
Dave Barach68b0fb02017-02-28 15:15:56 -0500588static void
Florin Corase04c2992017-03-01 08:17:34 -0800589vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
590{
591 vl_api_session_enable_disable_reply_t *rmp;
592 vlib_main_t *vm = vlib_get_main ();
593 int rv = 0;
594
595 vnet_session_enable_disable (vm, mp->is_enable);
596 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
597}
598
Dave Barach68b0fb02017-02-28 15:15:56 -0500599static void
Florin Coras458089b2019-08-21 16:20:44 -0700600vl_api_app_attach_t_handler (vl_api_app_attach_t * mp)
601{
602 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
603 vl_api_app_attach_reply_t *rmp;
604 ssvm_private_t *segp, *evt_q_segment;
605 vnet_app_attach_args_t _a, *a = &_a;
606 u8 fd_flags = 0, ctrl_thread;
607 vl_api_registration_t *reg;
608 svm_msg_q_t *ctrl_mq;
609
610 reg = vl_api_client_index_to_registration (mp->client_index);
611 if (!reg)
612 return;
613
614 if (session_main_is_enabled () == 0)
615 {
616 rv = VNET_API_ERROR_FEATURE_DISABLED;
617 goto done;
618 }
619
620 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
621 sizeof (mp->options),
622 "Out of options, fix api message definition");
623
624 clib_memset (a, 0, sizeof (*a));
625 a->api_client_index = mp->client_index;
626 a->options = mp->options;
627 a->session_cb_vft = &session_mq_cb_vft;
Florin Coras458089b2019-08-21 16:20:44 -0700628
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100629 a->namespace_id = vl_api_from_api_to_new_vec (&mp->namespace_id);
Florin Coras458089b2019-08-21 16:20:44 -0700630
631 if ((rv = vnet_application_attach (a)))
632 {
633 clib_warning ("attach returned: %d", rv);
634 vec_free (a->namespace_id);
635 goto done;
636 }
637 vec_free (a->namespace_id);
638
639 /* Send event queues segment */
640 if ((evt_q_segment = session_main_get_evt_q_segment ()))
641 {
642 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
643 fds[n_fds] = evt_q_segment->fd;
644 n_fds += 1;
645 }
646 /* Send fifo segment fd if needed */
647 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
648 {
649 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
650 fds[n_fds] = a->segment->fd;
651 n_fds += 1;
652 }
653 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
654 {
655 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
656 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
657 n_fds += 1;
658 }
659
660done:
Florin Coras458089b2019-08-21 16:20:44 -0700661 /* *INDENT-OFF* */
662 REPLY_MACRO2 (VL_API_APP_ATTACH_REPLY, ({
663 if (!rv)
664 {
Vratko Polak4f599852019-11-08 10:32:39 +0100665 ctrl_thread = vlib_num_workers () ? 1 : 0;
Nathan Skrzypczak9d3e1b42019-11-07 10:29:24 +0100666 ctrl_mq = session_main_get_vpp_event_queue (ctrl_thread);
Florin Coras458089b2019-08-21 16:20:44 -0700667 segp = a->segment;
668 rmp->app_index = clib_host_to_net_u32 (a->app_index);
669 rmp->app_mq = pointer_to_uword (a->app_evt_q);
670 rmp->vpp_ctrl_mq = pointer_to_uword (ctrl_mq);
671 rmp->vpp_ctrl_mq_thread = ctrl_thread;
672 rmp->n_fds = n_fds;
673 rmp->fd_flags = fd_flags;
674 if (vec_len (segp->name))
675 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100676 vl_api_vec_to_api_string (segp->name, &rmp->segment_name);
Florin Coras458089b2019-08-21 16:20:44 -0700677 }
678 rmp->segment_size = segp->ssvm_size;
679 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
680 }
681 }));
682 /* *INDENT-ON* */
683
684 if (n_fds)
685 session_send_fds (reg, fds, n_fds);
686}
687
Florin Corascea194d2017-10-02 00:18:51 -0700688static void
Florin Coras15531972018-08-12 23:50:53 -0700689vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
690{
691 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
692 vl_api_app_worker_add_del_reply_t *rmp;
693 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -0700694 application_t *app;
695 u8 fd_flags = 0;
696
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200697 if (session_main_is_enabled () == 0)
Florin Coras15531972018-08-12 23:50:53 -0700698 {
699 rv = VNET_API_ERROR_FEATURE_DISABLED;
700 goto done;
701 }
702
703 reg = vl_api_client_index_to_registration (mp->client_index);
704 if (!reg)
705 return;
706
Florin Corasc1f5a432018-11-20 11:31:26 -0800707 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -0700708 if (!app)
709 {
710 rv = VNET_API_ERROR_INVALID_VALUE;
711 goto done;
712 }
713
714 vnet_app_worker_add_del_args_t args = {
715 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -0800716 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -0800717 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -0700718 .is_add = mp->is_add
719 };
Florin Corasc1a42652019-02-08 18:27:29 -0800720 rv = vnet_app_worker_add_del (&args);
721 if (rv)
Florin Coras15531972018-08-12 23:50:53 -0700722 {
Florin Corasc1a42652019-02-08 18:27:29 -0800723 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -0700724 goto done;
725 }
726
Florin Coras14598772018-09-04 19:47:52 -0700727 if (!mp->is_add)
728 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -0700729
Florin Coras15531972018-08-12 23:50:53 -0700730 /* Send fifo segment fd if needed */
731 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
732 {
733 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
734 fds[n_fds] = args.segment->fd;
735 n_fds += 1;
736 }
737 if (application_segment_manager_properties (app)->use_mq_eventfd)
738 {
739 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
740 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
741 n_fds += 1;
742 }
743
744 /* *INDENT-OFF* */
745done:
746 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
747 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -0800748 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -0800749 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -0700750 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -0700751 {
Florin Coras15531972018-08-12 23:50:53 -0700752 if (vec_len (args.segment->name))
753 {
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100754 vl_api_vec_to_api_string (args.segment->name, &rmp->segment_name);
Florin Coras15531972018-08-12 23:50:53 -0700755 }
756 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
757 rmp->n_fds = n_fds;
758 rmp->fd_flags = fd_flags;
759 }
760 }));
761 /* *INDENT-ON* */
762
763 if (n_fds)
764 session_send_fds (reg, fds, n_fds);
765}
766
767static void
Florin Coras888d9f02020-04-02 23:00:13 +0000768vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
769{
770 vl_api_application_detach_reply_t *rmp;
771 int rv = VNET_API_ERROR_INVALID_VALUE_2;
772 vnet_app_detach_args_t _a, *a = &_a;
773 application_t *app;
774
775 if (session_main_is_enabled () == 0)
776 {
777 rv = VNET_API_ERROR_FEATURE_DISABLED;
778 goto done;
779 }
780
781 app = application_lookup (mp->client_index);
782 if (app)
783 {
784 a->app_index = app->app_index;
785 a->api_client_index = mp->client_index;
786 rv = vnet_application_detach (a);
787 }
788
789done:
790 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
791}
792
793static void
Florin Corascea194d2017-10-02 00:18:51 -0700794vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
795{
796 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -0800797 u32 appns_index = 0;
798 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700799 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +0200800 if (session_main_is_enabled () == 0)
Florin Corascea194d2017-10-02 00:18:51 -0700801 {
802 rv = VNET_API_ERROR_FEATURE_DISABLED;
803 goto done;
804 }
805
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100806 ns_id = vl_api_from_api_to_new_vec (&mp->namespace_id);
Florin Corascea194d2017-10-02 00:18:51 -0700807
Florin Corascea194d2017-10-02 00:18:51 -0700808 vnet_app_namespace_add_del_args_t args = {
809 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -0700810 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -0700811 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
812 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
813 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
814 .is_add = 1
815 };
Florin Corasc1a42652019-02-08 18:27:29 -0800816 rv = vnet_app_namespace_add_del (&args);
817 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -0800818 {
819 appns_index = app_namespace_index_from_id (ns_id);
820 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
821 {
822 clib_warning ("app ns lookup failed");
823 rv = VNET_API_ERROR_UNSPECIFIED;
824 }
825 }
Florin Corascea194d2017-10-02 00:18:51 -0700826 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -0800827
828 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -0700829done:
Florin Coras6e8c6672017-11-10 09:03:54 -0800830 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
831 if (!rv)
832 rmp->appns_index = clib_host_to_net_u32 (appns_index);
833 }));
834 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -0700835}
836
Florin Coras1c710452017-10-17 00:03:13 -0700837static void
838vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
839{
840 vl_api_session_rule_add_del_reply_t *rmp;
841 session_rule_add_del_args_t args;
842 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -0700843 int rv = 0;
844
Dave Barachb7b92992018-10-17 10:38:51 -0400845 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -0700846
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100847 ip_prefix_decode (&mp->lcl, &table_args->lcl);
848 ip_prefix_decode (&mp->rmt, &table_args->rmt);
849
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100850 table_args->lcl_port = mp->lcl_port;
851 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -0700852 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
853 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -0800854 mp->tag[sizeof (mp->tag) - 1] = 0;
855 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700856 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
857 args.scope = mp->scope;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100858 args.transport_proto =
859 api_session_transport_proto_decode (&mp->transport_proto) ==
860 TRANSPORT_PROTO_UDP ? 1 : 0;
Florin Coras1c710452017-10-17 00:03:13 -0700861
Florin Corasc1a42652019-02-08 18:27:29 -0800862 rv = vnet_session_rule_add_del (&args);
863 if (rv)
864 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -0800865 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -0700866 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
867}
868
Florin Coras6c36f532017-11-03 18:32:34 -0700869static void
870send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -0800871 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800872 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700873{
874 vl_api_session_rules_details_t *rmp = 0;
875 session_mask_or_match_4_t *match =
876 (session_mask_or_match_4_t *) & rule->match;
877 session_mask_or_match_4_t *mask =
878 (session_mask_or_match_4_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100879 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -0700880
881 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400882 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -0700883 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
884 rmp->context = context;
885
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100886 clib_memset (&lcl, 0, sizeof (lcl));
887 clib_memset (&rmt, 0, sizeof (rmt));
888 ip_set (&lcl.fp_addr, &match->lcl_ip, 1);
889 ip_set (&rmt.fp_addr, &match->rmt_ip, 1);
890 lcl.fp_len = ip4_mask_to_preflen (&mask->lcl_ip);
891 rmt.fp_len = ip4_mask_to_preflen (&mask->rmt_ip);
892
893 ip_prefix_encode (&lcl, &rmp->lcl);
894 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100895 rmp->lcl_port = match->lcl_port;
896 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700897 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
898 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100899 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
900 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -0700901 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800902 if (tag)
903 {
Dave Barach178cf492018-11-13 16:34:13 -0500904 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -0800905 rmp->tag[vec_len (tag)] = 0;
906 }
Florin Coras6c36f532017-11-03 18:32:34 -0700907
Florin Coras6c4dae22018-01-09 06:39:23 -0800908 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700909}
910
911static void
Florin Corasc97a7392017-11-05 23:07:07 -0800912send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
913 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800914 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700915{
916 vl_api_session_rules_details_t *rmp = 0;
917 session_mask_or_match_6_t *match =
918 (session_mask_or_match_6_t *) & rule->match;
919 session_mask_or_match_6_t *mask =
920 (session_mask_or_match_6_t *) & rule->mask;
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100921 fib_prefix_t lcl, rmt;
Florin Coras6c36f532017-11-03 18:32:34 -0700922
923 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -0400924 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -0700925 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
926 rmp->context = context;
927
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100928 clib_memset (&lcl, 0, sizeof (lcl));
929 clib_memset (&rmt, 0, sizeof (rmt));
930 ip_set (&lcl.fp_addr, &match->lcl_ip, 0);
931 ip_set (&rmt.fp_addr, &match->rmt_ip, 0);
932 lcl.fp_len = ip6_mask_to_preflen (&mask->lcl_ip);
933 rmt.fp_len = ip6_mask_to_preflen (&mask->rmt_ip);
934
935 ip_prefix_encode (&lcl, &rmp->lcl);
936 ip_prefix_encode (&rmt, &rmp->rmt);
Milan Lenco8b9a5d12017-11-24 17:12:33 +0100937 rmp->lcl_port = match->lcl_port;
938 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -0700939 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800940 rmp->scope =
Jakub Grajciarb4e5e502020-01-31 09:35:29 +0100941 is_local ? SESSION_RULE_SCOPE_API_LOCAL : SESSION_RULE_SCOPE_API_GLOBAL;
942 rmp->transport_proto = api_session_transport_proto_encode (transport_proto);
Florin Coras6c36f532017-11-03 18:32:34 -0700943 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -0800944 if (tag)
945 {
Dave Barach178cf492018-11-13 16:34:13 -0500946 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -0800947 rmp->tag[vec_len (tag)] = 0;
948 }
Florin Coras6c36f532017-11-03 18:32:34 -0700949
Florin Coras6c4dae22018-01-09 06:39:23 -0800950 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -0700951}
952
953static void
954send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -0800955 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -0800956 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -0700957{
958 mma_rule_16_t *rule16;
959 mma_rule_40_t *rule40;
960 mma_rules_table_16_t *srt16;
961 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -0800962 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -0700963
Florin Corasc97a7392017-11-05 23:07:07 -0800964 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -0700965 {
Florin Corasc97a7392017-11-05 23:07:07 -0800966 u8 *tag = 0;
967 /* *INDENT-OFF* */
968 srt16 = &srt->session_rules_tables_16;
969 pool_foreach (rule16, srt16->rules, ({
970 ri = mma_rules_table_rule_index_16 (srt16, rule16);
971 tag = session_rules_table_rule_tag (srt, ri, 1);
972 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800973 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -0800974 }));
975 /* *INDENT-ON* */
976 }
977 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
978 {
979 u8 *tag = 0;
980 /* *INDENT-OFF* */
981 srt40 = &srt->session_rules_tables_40;
982 pool_foreach (rule40, srt40->rules, ({
983 ri = mma_rules_table_rule_index_40 (srt40, rule40);
984 tag = session_rules_table_rule_tag (srt, ri, 1);
985 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -0800986 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -0800987 }));
988 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -0700989 }
990}
991
992static void
993vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
994{
Florin Coras6c4dae22018-01-09 06:39:23 -0800995 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -0700996 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -0800997 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -0700998
Florin Coras6c4dae22018-01-09 06:39:23 -0800999 reg = vl_api_client_index_to_registration (mp->client_index);
1000 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001001 return;
1002
1003 /* *INDENT-OFF* */
1004 session_table_foreach (st, ({
Florin Coras07063b82020-03-13 04:44:51 +00001005 for (tp = 0; tp < TRANSPORT_N_PROTOS; tp++)
Florin Corasc97a7392017-11-05 23:07:07 -08001006 {
1007 send_session_rules_table_details (&st->session_rules[tp],
1008 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001009 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001010 mp->context);
1011 }
Florin Coras6c36f532017-11-03 18:32:34 -07001012 }));
1013 /* *INDENT-ON* */
1014}
1015
Florin Coras371ca502018-02-21 12:07:41 -08001016static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001017vl_api_app_add_cert_key_pair_t_handler (vl_api_app_add_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001018{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001019 vl_api_app_add_cert_key_pair_reply_t *rmp;
1020 vnet_app_add_cert_key_pair_args_t _a, *a = &_a;
1021 u32 certkey_len, key_len, cert_len;
Florin Coras371ca502018-02-21 12:07:41 -08001022 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001023 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001024 {
1025 rv = VNET_API_ERROR_FEATURE_DISABLED;
1026 goto done;
1027 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001028
Florin Coras371ca502018-02-21 12:07:41 -08001029 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001030 if (cert_len > 10000)
1031 {
1032 rv = VNET_API_ERROR_INVALID_VALUE;
1033 goto done;
1034 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001035
1036 certkey_len = clib_net_to_host_u16 (mp->certkey_len);
1037 if (certkey_len < cert_len)
Florin Coras371ca502018-02-21 12:07:41 -08001038 {
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001039 rv = VNET_API_ERROR_INVALID_VALUE;
1040 goto done;
Florin Coras371ca502018-02-21 12:07:41 -08001041 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001042
1043 key_len = certkey_len - cert_len;
1044 if (key_len > 10000)
1045 {
1046 rv = VNET_API_ERROR_INVALID_VALUE;
1047 goto done;
1048 }
1049
1050 clib_memset (a, 0, sizeof (*a));
1051 vec_validate (a->cert, cert_len);
1052 vec_validate (a->key, key_len);
1053 clib_memcpy_fast (a->cert, mp->certkey, cert_len);
1054 clib_memcpy_fast (a->key, mp->certkey + cert_len, key_len);
1055 rv = vnet_app_add_cert_key_pair (a);
Florin Coras371ca502018-02-21 12:07:41 -08001056 vec_free (a->cert);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001057 vec_free (a->key);
1058
Florin Coras371ca502018-02-21 12:07:41 -08001059done:
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001060 /* *INDENT-OFF* */
1061 REPLY_MACRO2 (VL_API_APP_ADD_CERT_KEY_PAIR_REPLY, ({
1062 if (!rv)
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001063 rmp->index = clib_host_to_net_u32 (a->index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001064 }));
1065 /* *INDENT-ON* */
Florin Coras371ca502018-02-21 12:07:41 -08001066}
1067
1068static void
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001069vl_api_app_del_cert_key_pair_t_handler (vl_api_app_del_cert_key_pair_t * mp)
Florin Coras371ca502018-02-21 12:07:41 -08001070{
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001071 vl_api_app_del_cert_key_pair_reply_t *rmp;
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001072 u32 ckpair_index;
Florin Coras371ca502018-02-21 12:07:41 -08001073 int rv = 0;
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001074 if (session_main_is_enabled () == 0)
1075 {
1076 rv = VNET_API_ERROR_FEATURE_DISABLED;
1077 goto done;
1078 }
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001079 ckpair_index = clib_net_to_host_u32 (mp->index);
1080 rv = vnet_app_del_cert_key_pair (ckpair_index);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001081
1082done:
Nathan Skrzypczak45ec9f42019-11-06 14:47:40 +01001083 REPLY_MACRO (VL_API_APP_DEL_CERT_KEY_PAIR_REPLY);
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001084}
1085
1086/* ### WILL BE DEPRECATED POST 20.01 ### */
1087static void
1088vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1089 mp)
1090{
1091 vl_api_application_tls_cert_add_reply_t *rmp;
1092 app_cert_key_pair_t *ckpair;
1093 application_t *app;
1094 u32 cert_len;
1095 int rv = 0;
1096 if (session_main_is_enabled () == 0)
Florin Coras371ca502018-02-21 12:07:41 -08001097 {
1098 rv = VNET_API_ERROR_FEATURE_DISABLED;
1099 goto done;
1100 }
Florin Corase3e2f072018-03-04 07:24:30 -08001101 if (!(app = application_lookup (mp->client_index)))
1102 {
1103 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1104 goto done;
1105 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001106 cert_len = clib_net_to_host_u16 (mp->cert_len);
1107 if (cert_len > 10000)
1108 {
1109 rv = VNET_API_ERROR_INVALID_VALUE;
1110 goto done;
1111 }
1112 ckpair = app_cert_key_pair_get_default ();
1113 vec_validate (ckpair->cert, cert_len);
1114 clib_memcpy_fast (ckpair->cert, mp->cert, cert_len);
1115
1116done:
1117 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1118}
1119
1120/* ### WILL BE DEPRECATED POST 20.01 ### */
1121static void
1122vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1123 mp)
1124{
1125 vl_api_application_tls_key_add_reply_t *rmp;
1126 app_cert_key_pair_t *ckpair;
1127 application_t *app;
1128 u32 key_len;
1129 int rv = 0;
1130 if (session_main_is_enabled () == 0)
1131 {
1132 rv = VNET_API_ERROR_FEATURE_DISABLED;
1133 goto done;
1134 }
1135 if (!(app = application_lookup (mp->client_index)))
1136 {
1137 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1138 goto done;
1139 }
Florin Coras371ca502018-02-21 12:07:41 -08001140 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001141 if (key_len > 10000)
1142 {
1143 rv = VNET_API_ERROR_INVALID_VALUE;
1144 goto done;
1145 }
Nathan Skrzypczak79f89532019-09-13 11:08:13 +02001146 ckpair = app_cert_key_pair_get_default ();
1147 vec_validate (ckpair->key, key_len);
1148 clib_memcpy_fast (ckpair->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001149done:
1150 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1151}
1152
Florin Coras6cf30ad2017-04-04 23:08:23 -07001153static clib_error_t *
1154application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001155{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001156 application_t *app = application_lookup (client_index);
1157 vnet_app_detach_args_t _a, *a = &_a;
1158 if (app)
1159 {
Florin Coras15531972018-08-12 23:50:53 -07001160 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001161 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001162 vnet_application_detach (a);
1163 }
1164 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001165}
1166
Florin Coras6cf30ad2017-04-04 23:08:23 -07001167VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001168
1169#define vl_msg_name_crc_list
1170#include <vnet/vnet_all_api_h.h>
1171#undef vl_msg_name_crc_list
1172
1173static void
1174setup_message_id_table (api_main_t * am)
1175{
1176#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1177 foreach_vl_msg_name_crc_session;
1178#undef _
1179}
1180
1181/*
1182 * session_api_hookup
1183 * Add uri's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -05001184 * vlib has already mapped shared memory and
Dave Barach68b0fb02017-02-28 15:15:56 -05001185 * added the client registration handlers.
1186 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1187 */
1188static clib_error_t *
1189session_api_hookup (vlib_main_t * vm)
1190{
Dave Barach39d69112019-11-27 11:42:13 -05001191 api_main_t *am = vlibapi_get_main ();
Dave Barach68b0fb02017-02-28 15:15:56 -05001192
1193#define _(N,n) \
1194 vl_msg_api_set_handlers(VL_API_##N, #n, \
1195 vl_api_##n##_t_handler, \
1196 vl_noop_handler, \
1197 vl_api_##n##_t_endian, \
1198 vl_api_##n##_t_print, \
1199 sizeof(vl_api_##n##_t), 1);
1200 foreach_session_api_msg;
1201#undef _
1202
1203 /*
Dave Barach68b0fb02017-02-28 15:15:56 -05001204 * Set up the (msg_name, crc, message-id) table
1205 */
1206 setup_message_id_table (am);
1207
1208 return 0;
1209}
1210
1211VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001212
Dave Barach68b0fb02017-02-28 15:15:56 -05001213/*
1214 * fd.io coding-style-patch-verification: ON
1215 *
1216 * Local Variables:
1217 * eval: (c-set-style "gnu")
1218 * End:
1219 */