blob: d37b3c995d06a19febb0bc80f4fee47f1abc130a [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
2 * Copyright (c) 2015-2016 Cisco and/or its affiliates.
3 * 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>
20#include <vnet/session/session_rules_table.h>
Florin Coras6c36f532017-11-03 18:32:34 -070021#include <vnet/session/session_table.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050022
23#include <vnet/vnet_msg_enum.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
25#define vl_typedefs /* define message structures */
26#include <vnet/vnet_all_api_h.h>
27#undef vl_typedefs
28
29#define vl_endianfun /* define message structures */
30#include <vnet/vnet_all_api_h.h>
31#undef vl_endianfun
32
33/* instantiate all the print functions we know about */
34#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35#define vl_printfun
36#include <vnet/vnet_all_api_h.h>
37#undef vl_printfun
38
39#include <vlibapi/api_helper_macros.h>
40
41#define foreach_session_api_msg \
42_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070043_(APPLICATION_ATTACH, application_attach) \
44_(APPLICATION_DETACH, application_detach) \
Dave Barach68b0fb02017-02-28 15:15:56 -050045_(BIND_URI, bind_uri) \
46_(UNBIND_URI, unbind_uri) \
47_(CONNECT_URI, connect_uri) \
48_(DISCONNECT_SESSION, disconnect_session) \
49_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
50_(ACCEPT_SESSION_REPLY, accept_session_reply) \
51_(RESET_SESSION_REPLY, reset_session_reply) \
Florin Corascea194d2017-10-02 00:18:51 -070052_(BIND_SOCK, bind_sock) \
Dave Barach68b0fb02017-02-28 15:15:56 -050053_(UNBIND_SOCK, unbind_sock) \
54_(CONNECT_SOCK, connect_sock) \
Florin Corase04c2992017-03-01 08:17:34 -080055_(SESSION_ENABLE_DISABLE, session_enable_disable) \
Florin Corascea194d2017-10-02 00:18:51 -070056_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
Florin Coras1c710452017-10-17 00:03:13 -070057_(SESSION_RULE_ADD_DEL, session_rule_add_del) \
Florin Coras6c36f532017-11-03 18:32:34 -070058_(SESSION_RULES_DUMP, session_rules_dump) \
Florin Coras371ca502018-02-21 12:07:41 -080059_(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \
60_(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \
Florin Coras15531972018-08-12 23:50:53 -070061_(APP_WORKER_ADD_DEL, app_worker_add_del) \
Florin Corase04c2992017-03-01 08:17:34 -080062
Dave Barach68b0fb02017-02-28 15:15:56 -050063static int
Florin Coras99368312018-08-02 10:45:44 -070064session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
Florin Corasb384b542018-01-15 01:08:33 -080065{
66 clib_error_t *error;
67 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
68 {
69 clib_warning ("can't send memfd fd");
70 return -1;
71 }
Florin Coras99368312018-08-02 10:45:44 -070072 error = vl_api_send_fd_msg (reg, fds, n_fds);
Florin Corasb384b542018-01-15 01:08:33 -080073 if (error)
74 {
75 clib_error_report (error);
76 return -1;
77 }
78 return 0;
79}
80
81static int
Florin Corasfa76a762018-11-29 12:40:10 -080082send_add_segment_callback (u32 api_client_index, u64 segment_handle)
Dave Barach68b0fb02017-02-28 15:15:56 -050083{
Florin Coras99368312018-08-02 10:45:44 -070084 int fds[SESSION_N_FD_TYPE], n_fds = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050085 vl_api_map_another_segment_t *mp;
Florin Corasfa76a762018-11-29 12:40:10 -080086 svm_fifo_segment_private_t *fs;
Florin Corasb384b542018-01-15 01:08:33 -080087 vl_api_registration_t *reg;
Florin Corasfa76a762018-11-29 12:40:10 -080088 ssvm_private_t *sp;
Florin Coras99368312018-08-02 10:45:44 -070089 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050090
Florin Corasb384b542018-01-15 01:08:33 -080091 reg = vl_mem_api_client_index_to_registration (api_client_index);
92 if (!reg)
93 {
Florin Corasfa76a762018-11-29 12:40:10 -080094 clib_warning ("no api registration for client: %u", api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -080095 return -1;
96 }
Dave Barach68b0fb02017-02-28 15:15:56 -050097
Florin Corasfa76a762018-11-29 12:40:10 -080098 fs = segment_manager_get_segment_w_handle (segment_handle);
99 sp = &fs->ssvm;
Florin Coras99368312018-08-02 10:45:44 -0700100 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
Florin Corasb384b542018-01-15 01:08:33 -0800101 {
Florin Coras99368312018-08-02 10:45:44 -0700102 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
103 {
104 clib_warning ("can't send memfd fd");
105 return -1;
106 }
107
108 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
109 fds[n_fds] = sp->fd;
110 n_fds += 1;
Florin Corasb384b542018-01-15 01:08:33 -0800111 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500112
Florin Coras99368312018-08-02 10:45:44 -0700113 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400114 clib_memset (mp, 0, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500115 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
Florin Corasb384b542018-01-15 01:08:33 -0800116 mp->segment_size = sp->ssvm_size;
Florin Coras99368312018-08-02 10:45:44 -0700117 mp->fd_flags = fd_flags;
Florin Corasfa76a762018-11-29 12:40:10 -0800118 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasb384b542018-01-15 01:08:33 -0800119 strncpy ((char *) mp->segment_name, (char *) sp->name,
Dave Barach68b0fb02017-02-28 15:15:56 -0500120 sizeof (mp->segment_name) - 1);
121
Florin Corasb384b542018-01-15 01:08:33 -0800122 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
123
Florin Coras99368312018-08-02 10:45:44 -0700124 if (n_fds)
125 return session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500126
127 return 0;
128}
129
130static int
Florin Corasfa76a762018-11-29 12:40:10 -0800131send_del_segment_callback (u32 api_client_index, u64 segment_handle)
Florin Corasf8f516a2018-02-08 15:10:09 -0800132{
133 vl_api_unmap_segment_t *mp;
134 vl_api_registration_t *reg;
135
136 reg = vl_mem_api_client_index_to_registration (api_client_index);
137 if (!reg)
138 {
139 clib_warning ("no registration: %u", api_client_index);
140 return -1;
141 }
142
Florin Coras99368312018-08-02 10:45:44 -0700143 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400144 clib_memset (mp, 0, sizeof (*mp));
Florin Corasf8f516a2018-02-08 15:10:09 -0800145 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
Florin Corasfa76a762018-11-29 12:40:10 -0800146 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasf8f516a2018-02-08 15:10:09 -0800147 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
148
Florin Coras99368312018-08-02 10:45:44 -0700149 return 0;
150}
151
152static int
Florin Coras134a9962018-08-28 11:32:04 -0700153send_app_cut_through_registration_add (u32 api_client_index,
154 u32 wrk_map_index, u64 mq_addr,
Florin Coras99368312018-08-02 10:45:44 -0700155 u64 peer_mq_addr)
156{
157 vl_api_app_cut_through_registration_add_t *mp;
158 vl_api_registration_t *reg;
159 svm_msg_q_t *mq, *peer_mq;
160 int fds[2];
161
162 reg = vl_mem_api_client_index_to_registration (api_client_index);
163 if (!reg)
164 {
165 clib_warning ("no registration: %u", api_client_index);
166 return -1;
167 }
168
169 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400170 clib_memset (mp, 0, sizeof (*mp));
Florin Coras99368312018-08-02 10:45:44 -0700171 mp->_vl_msg_id =
172 clib_host_to_net_u16 (VL_API_APP_CUT_THROUGH_REGISTRATION_ADD);
173
174 mp->evt_q_address = mq_addr;
175 mp->peer_evt_q_address = peer_mq_addr;
Florin Coras134a9962018-08-28 11:32:04 -0700176 mp->wrk_index = wrk_map_index;
Florin Coras99368312018-08-02 10:45:44 -0700177
178 mq = uword_to_pointer (mq_addr, svm_msg_q_t *);
179 peer_mq = uword_to_pointer (peer_mq_addr, svm_msg_q_t *);
180
181 if (svm_msg_q_get_producer_eventfd (mq) != -1)
182 {
183 mp->fd_flags |= SESSION_FD_F_MQ_EVENTFD;
184 mp->n_fds = 2;
185 /* app will overwrite exactly the fds we pass here. So
186 * when we swap mq with peer_mq (accept vs connect) the
187 * fds will still be valid */
188 fds[0] = svm_msg_q_get_consumer_eventfd (mq);
189 fds[1] = svm_msg_q_get_producer_eventfd (peer_mq);
190 }
191
192 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
193
194 if (mp->n_fds != 0)
195 session_send_fds (reg, fds, mp->n_fds);
Florin Corasf8f516a2018-02-08 15:10:09 -0800196
197 return 0;
198}
199
200static int
Florin Coras6cf30ad2017-04-04 23:08:23 -0700201send_session_accept_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500202{
Florin Coras15531972018-08-12 23:50:53 -0700203 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700204 transport_proto_vft_t *tp_vft;
Florin Corasb384b542018-01-15 01:08:33 -0800205 vl_api_accept_session_t *mp;
206 vl_api_registration_t *reg;
207 transport_connection_t *tc;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700208 stream_session_t *listener;
Florin Coras3c2fed52018-07-04 04:15:05 -0700209 svm_msg_q_t *vpp_queue;
Florin Coras15531972018-08-12 23:50:53 -0700210 application_t *server;
Dave Barach68b0fb02017-02-28 15:15:56 -0500211
Florin Coras15531972018-08-12 23:50:53 -0700212 server = application_get (server_wrk->app_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800213 reg =
214 vl_mem_api_client_index_to_registration (server_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800215 if (!reg)
216 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800217 clib_warning ("no registration: %u", server_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800218 return -1;
219 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500220
Florin Corasb384b542018-01-15 01:08:33 -0800221 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400222 clib_memset (mp, 0, sizeof (*mp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700223
Dave Barach68b0fb02017-02-28 15:15:56 -0500224 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
Florin Coras15531972018-08-12 23:50:53 -0700225 mp->context = server_wrk->wrk_index;
Damjan Marion7bee80c2017-04-26 15:32:12 +0200226 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
227 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800228
229 if (session_has_transport (s))
230 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700231 listener = listen_session_get (s->listener_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800232 mp->listener_handle = listen_session_get_handle (listener);
233 if (application_is_proxy (server))
234 {
235 listener =
Florin Coras15531972018-08-12 23:50:53 -0700236 app_worker_first_listener (server_wrk, session_get_fib_proto (s),
237 session_get_transport_proto (s));
Florin Corasf8f516a2018-02-08 15:10:09 -0800238 if (listener)
239 mp->listener_handle = listen_session_get_handle (listener);
240 }
241 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
242 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
243 mp->handle = session_handle (s);
244 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
245 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
246 mp->port = tc->rmt_port;
247 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500248 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Corasf8f516a2018-02-08 15:10:09 -0800249 }
250 else
251 {
252 local_session_t *ls = (local_session_t *) s;
253 local_session_t *ll;
254 if (application_local_session_listener_has_transport (ls))
255 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700256 listener = listen_session_get (ls->listener_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800257 mp->listener_handle = listen_session_get_handle (listener);
Florin Coras5fda7a32018-02-14 08:04:31 -0800258 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
Florin Corasf8f516a2018-02-08 15:10:09 -0800259 }
260 else
261 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700262 ll = application_get_local_listen_session (server,
Florin Corasf8f516a2018-02-08 15:10:09 -0800263 ls->listener_index);
Florin Coras5fda7a32018-02-14 08:04:31 -0800264 if (ll->transport_listener_index != ~0)
265 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700266 listener = listen_session_get (ll->transport_listener_index);
Florin Coras5fda7a32018-02-14 08:04:31 -0800267 mp->listener_handle = listen_session_get_handle (listener);
268 }
269 else
270 {
271 mp->listener_handle = application_local_session_handle (ll);
272 }
273 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
Florin Corasf8f516a2018-02-08 15:10:09 -0800274 }
275 mp->handle = application_local_session_handle (ls);
276 mp->port = ls->port;
277 mp->vpp_event_queue_address = ls->client_evt_q;
278 mp->server_event_queue_address = ls->server_evt_q;
279 }
Florin Corasb384b542018-01-15 01:08:33 -0800280 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500281
282 return 0;
283}
284
285static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700286send_session_disconnect_callback (stream_session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500287{
Florin Coras15531972018-08-12 23:50:53 -0700288 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Corasb384b542018-01-15 01:08:33 -0800289 vl_api_disconnect_session_t *mp;
290 vl_api_registration_t *reg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500291
Florin Corasc1f5a432018-11-20 11:31:26 -0800292 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800293 if (!reg)
294 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800295 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800296 return;
297 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500298
Florin Corasb384b542018-01-15 01:08:33 -0800299 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400300 clib_memset (mp, 0, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500301 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700302 mp->handle = session_handle (s);
Florin Corasc1f5a432018-11-20 11:31:26 -0800303 mp->context = app_wrk->api_client_index;
Florin Corasb384b542018-01-15 01:08:33 -0800304 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500305}
306
Florin Corasd79b41e2017-03-04 05:37:52 -0800307static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700308send_session_reset_callback (stream_session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800309{
Florin Coras15531972018-08-12 23:50:53 -0700310 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Corasb384b542018-01-15 01:08:33 -0800311 vl_api_registration_t *reg;
312 vl_api_reset_session_t *mp;
Florin Corasd79b41e2017-03-04 05:37:52 -0800313
Florin Corasc1f5a432018-11-20 11:31:26 -0800314 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800315 if (!reg)
316 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800317 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800318 return;
319 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800320
Florin Corasb384b542018-01-15 01:08:33 -0800321 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400322 clib_memset (mp, 0, sizeof (*mp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800323 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700324 mp->handle = session_handle (s);
Florin Corasb384b542018-01-15 01:08:33 -0800325 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Florin Corasd79b41e2017-03-04 05:37:52 -0800326}
327
Dave Barach0194f1a2017-05-15 10:11:39 -0400328int
Florin Coras15531972018-08-12 23:50:53 -0700329send_session_connected_callback (u32 app_wrk_index, u32 api_context,
Florin Coras6cf30ad2017-04-04 23:08:23 -0700330 stream_session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500331{
Dave Wallace33e002b2017-09-06 01:20:02 -0400332 vl_api_connect_session_reply_t *mp;
Florin Corasade70e42017-10-14 18:56:41 -0700333 transport_connection_t *tc;
Florin Corasb384b542018-01-15 01:08:33 -0800334 vl_api_registration_t *reg;
Florin Coras3c2fed52018-07-04 04:15:05 -0700335 svm_msg_q_t *vpp_queue;
Florin Coras15531972018-08-12 23:50:53 -0700336 app_worker_t *app_wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500337
Florin Coras15531972018-08-12 23:50:53 -0700338 app_wrk = app_worker_get (app_wrk_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800339 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800340 if (!reg)
341 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800342 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800343 return -1;
344 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500345
Florin Corasb384b542018-01-15 01:08:33 -0800346 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Wallace33e002b2017-09-06 01:20:02 -0400347 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700348 mp->context = api_context;
Dave Wallace965fec92017-10-17 16:19:41 -0400349
350 if (is_fail)
351 goto done;
352
Florin Corasf8f516a2018-02-08 15:10:09 -0800353 if (session_has_transport (s))
Dave Barach68b0fb02017-02-28 15:15:56 -0500354 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800355 tc = session_get_transport (s);
356 if (!tc)
357 {
358 is_fail = 1;
359 goto done;
360 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500361
Florin Corasf8f516a2018-02-08 15:10:09 -0800362 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
363 mp->handle = session_handle (s);
364 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Dave Barach178cf492018-11-13 16:34:13 -0500365 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Corasf8f516a2018-02-08 15:10:09 -0800366 mp->is_ip4 = tc->is_ip4;
367 mp->lcl_port = tc->lcl_port;
368 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
369 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
370 }
371 else
372 {
373 local_session_t *ls = (local_session_t *) s;
374 mp->handle = application_local_session_handle (ls);
375 mp->lcl_port = ls->port;
376 mp->vpp_event_queue_address = ls->server_evt_q;
377 mp->client_event_queue_address = ls->client_evt_q;
378 mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
379 mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
380 }
Dave Wallace965fec92017-10-17 16:19:41 -0400381
382done:
383 mp->retval = is_fail ?
384 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
Florin Corasb384b542018-01-15 01:08:33 -0800385 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500386 return 0;
387}
388
Florin Corascea194d2017-10-02 00:18:51 -0700389static session_cb_vft_t session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500390 .session_accept_callback = send_session_accept_callback,
391 .session_disconnect_callback = send_session_disconnect_callback,
392 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800393 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500394 .add_segment_callback = send_add_segment_callback,
Florin Corasf8f516a2018-02-08 15:10:09 -0800395 .del_segment_callback = send_del_segment_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500396};
397
Florin Coras52207f12018-07-12 14:48:06 -0700398static int
Florin Coras83ea6692018-10-12 16:55:14 -0700399mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
400{
401 int rv;
402 u8 try = 0;
403 while (try < 100)
404 {
405 rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
406 SESSION_MQ_CTRL_EVT_RING,
407 SVM_Q_NOWAIT, msg);
408 if (!rv)
409 return 0;
410 try++;
411 }
412 return -1;
413}
414
415static int
Florin Coras52207f12018-07-12 14:48:06 -0700416mq_send_session_accepted_cb (stream_session_t * s)
417{
Florin Coras15531972018-08-12 23:50:53 -0700418 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700419 svm_msg_q_msg_t _msg, *msg = &_msg;
420 svm_msg_q_t *vpp_queue, *app_mq;
421 transport_proto_vft_t *tp_vft;
422 transport_connection_t *tc;
423 stream_session_t *listener;
424 session_accepted_msg_t *mp;
425 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700426 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700427
Florin Coras15531972018-08-12 23:50:53 -0700428 app = application_get (app_wrk->app_index);
429 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700430 if (mq_try_lock_and_alloc_msg (app_mq, msg))
431 return -1;
Florin Coras52207f12018-07-12 14:48:06 -0700432
433 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400434 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700435 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
436 mp = (session_accepted_msg_t *) evt->data;
Florin Corasab2f6db2018-08-31 14:31:41 -0700437 mp->context = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700438 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
439 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800440 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700441
442 if (session_has_transport (s))
443 {
444 listener = listen_session_get (s->listener_index);
445 mp->listener_handle = listen_session_get_handle (listener);
446 if (application_is_proxy (app))
447 {
448 listener =
Florin Coras15531972018-08-12 23:50:53 -0700449 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
450 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700451 if (listener)
452 mp->listener_handle = listen_session_get_handle (listener);
453 }
454 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
455 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
456 mp->handle = session_handle (s);
457 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
458 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
459 mp->port = tc->rmt_port;
460 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500461 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700462 }
463 else
464 {
465 local_session_t *ls = (local_session_t *) s;
466 local_session_t *ll;
Florin Coras99368312018-08-02 10:45:44 -0700467 u8 main_thread = vlib_num_workers ()? 1 : 0;
468
Florin Corasc1f5a432018-11-20 11:31:26 -0800469 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700470 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700471 ls->server_evt_q,
472 ls->client_evt_q);
473
Florin Coras52207f12018-07-12 14:48:06 -0700474 if (application_local_session_listener_has_transport (ls))
475 {
476 listener = listen_session_get (ls->listener_index);
477 mp->listener_handle = listen_session_get_handle (listener);
478 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
479 }
480 else
481 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700482 ll = application_get_local_listen_session (app, ls->listener_index);
Florin Coras52207f12018-07-12 14:48:06 -0700483 if (ll->transport_listener_index != ~0)
484 {
485 listener = listen_session_get (ll->transport_listener_index);
486 mp->listener_handle = listen_session_get_handle (listener);
487 }
488 else
489 {
490 mp->listener_handle = application_local_session_handle (ll);
491 }
492 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
493 }
494 mp->handle = application_local_session_handle (ls);
495 mp->port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700496 vpp_queue = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700497 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
498 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700499 mp->server_event_queue_address = ls->server_evt_q;
500 }
Florin Coras537b17e2018-09-28 10:35:45 -0700501 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700502
503 return 0;
504}
505
506static void
507mq_send_session_disconnected_cb (stream_session_t * s)
508{
Florin Coras15531972018-08-12 23:50:53 -0700509 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700510 svm_msg_q_msg_t _msg, *msg = &_msg;
511 session_disconnected_msg_t *mp;
512 svm_msg_q_t *app_mq;
513 session_event_t *evt;
514
Florin Coras15531972018-08-12 23:50:53 -0700515 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700516 if (mq_try_lock_and_alloc_msg (app_mq, msg))
517 return;
Florin Coras52207f12018-07-12 14:48:06 -0700518 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400519 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700520 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
521 mp = (session_disconnected_msg_t *) evt->data;
522 mp->handle = session_handle (s);
Florin Corasc1f5a432018-11-20 11:31:26 -0800523 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700524 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700525}
526
Florin Corasaa27eb92018-10-13 12:20:01 -0700527void
528mq_send_local_session_disconnected_cb (u32 app_wrk_index,
529 local_session_t * ls)
530{
531 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
532 svm_msg_q_msg_t _msg, *msg = &_msg;
533 session_disconnected_msg_t *mp;
534 svm_msg_q_t *app_mq;
535 session_event_t *evt;
Florin Corasaa27eb92018-10-13 12:20:01 -0700536
Florin Corasaa27eb92018-10-13 12:20:01 -0700537 app_mq = app_wrk->event_queue;
538 if (mq_try_lock_and_alloc_msg (app_mq, msg))
539 return;
540 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400541 clib_memset (evt, 0, sizeof (*evt));
Florin Corasaa27eb92018-10-13 12:20:01 -0700542 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
543 mp = (session_disconnected_msg_t *) evt->data;
544 mp->handle = application_local_session_handle (ls);
Florin Corasc1f5a432018-11-20 11:31:26 -0800545 mp->context = app_wrk->api_client_index;
Florin Corasaa27eb92018-10-13 12:20:01 -0700546 svm_msg_q_add_and_unlock (app_mq, msg);
547}
548
Florin Coras52207f12018-07-12 14:48:06 -0700549static void
550mq_send_session_reset_cb (stream_session_t * s)
551{
Florin Coras15531972018-08-12 23:50:53 -0700552 app_worker_t *app = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700553 svm_msg_q_msg_t _msg, *msg = &_msg;
554 session_reset_msg_t *mp;
555 svm_msg_q_t *app_mq;
556 session_event_t *evt;
557
558 app_mq = app->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700559 if (mq_try_lock_and_alloc_msg (app_mq, msg))
560 return;
Florin Coras52207f12018-07-12 14:48:06 -0700561 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400562 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700563 evt->event_type = SESSION_CTRL_EVT_RESET;
564 mp = (session_reset_msg_t *) evt->data;
565 mp->handle = session_handle (s);
Florin Coras537b17e2018-09-28 10:35:45 -0700566 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700567}
568
569static int
Florin Coras15531972018-08-12 23:50:53 -0700570mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras52207f12018-07-12 14:48:06 -0700571 stream_session_t * s, u8 is_fail)
572{
573 svm_msg_q_msg_t _msg, *msg = &_msg;
574 session_connected_msg_t *mp;
575 svm_msg_q_t *vpp_mq, *app_mq;
576 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700577 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700578 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700579
Florin Coras15531972018-08-12 23:50:53 -0700580 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700581 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700582 if (!app_mq)
583 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800584 clib_warning ("app %u with api index: %u not attached",
585 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700586 return -1;
587 }
588
Florin Coras83ea6692018-10-12 16:55:14 -0700589 if (mq_try_lock_and_alloc_msg (app_mq, msg))
590 return -1;
Florin Coras52207f12018-07-12 14:48:06 -0700591 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400592 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700593 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
594 mp = (session_connected_msg_t *) evt->data;
595 mp->context = api_context;
Florin Corasfa76a762018-11-29 12:40:10 -0800596 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700597
598 if (is_fail)
599 goto done;
600
601 if (session_has_transport (s))
602 {
603 tc = session_get_transport (s);
604 if (!tc)
605 {
606 is_fail = 1;
607 goto done;
608 }
609
610 vpp_mq = session_manager_get_vpp_event_queue (s->thread_index);
611 mp->handle = session_handle (s);
612 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Dave Barach178cf492018-11-13 16:34:13 -0500613 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700614 mp->is_ip4 = tc->is_ip4;
615 mp->lcl_port = tc->lcl_port;
616 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
617 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
618 }
619 else
620 {
621 local_session_t *ls = (local_session_t *) s;
Florin Coras99368312018-08-02 10:45:44 -0700622 u8 main_thread = vlib_num_workers ()? 1 : 0;
623
Florin Corasc1f5a432018-11-20 11:31:26 -0800624 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700625 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700626 ls->client_evt_q,
627 ls->server_evt_q);
628
Florin Coras52207f12018-07-12 14:48:06 -0700629 mp->handle = application_local_session_handle (ls);
630 mp->lcl_port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700631 vpp_mq = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700632 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras52207f12018-07-12 14:48:06 -0700633 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700634 mp->server_event_queue_address = ls->server_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700635 mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
636 mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
637 }
638
639done:
640 mp->retval = is_fail ?
641 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
642
Florin Coras537b17e2018-09-28 10:35:45 -0700643 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700644 return 0;
645}
646
Florin Coras60116992018-08-27 09:52:18 -0700647static int
648mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
649 session_handle_t handle, int rv)
650{
651 svm_msg_q_msg_t _msg, *msg = &_msg;
652 svm_msg_q_t *app_mq, *vpp_evt_q;
653 transport_connection_t *tc;
654 stream_session_t *ls = 0;
655 session_bound_msg_t *mp;
656 app_worker_t *app_wrk;
657 session_event_t *evt;
658 application_t *app;
659
660 app_wrk = app_worker_get (app_wrk_index);
661 app = application_get (app_wrk->app_index);
662 app_mq = app_wrk->event_queue;
663 if (!app_mq)
664 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800665 clib_warning ("app %u with api index: %u not attached",
666 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700667 return -1;
668 }
669
Florin Coras83ea6692018-10-12 16:55:14 -0700670 if (mq_try_lock_and_alloc_msg (app_mq, msg))
671 return -1;
672
Florin Coras60116992018-08-27 09:52:18 -0700673 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400674 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700675 evt->event_type = SESSION_CTRL_EVT_BOUND;
676 mp = (session_bound_msg_t *) evt->data;
677 mp->context = api_context;
678
679 if (rv)
680 goto done;
681
682 mp->handle = handle;
683 if (application_has_global_scope (app))
684 {
685 ls = listen_session_get_from_handle (handle);
686 tc = listen_session_get_transport (ls);
687 mp->lcl_port = tc->lcl_port;
688 mp->lcl_is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500689 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras60116992018-08-27 09:52:18 -0700690 }
691 else
692 {
693 local_session_t *local;
Florin Corasab2f6db2018-08-31 14:31:41 -0700694 local = application_get_local_listener_w_handle (handle);
Florin Coras60116992018-08-27 09:52:18 -0700695 mp->lcl_port = local->port;
696 mp->lcl_is_ip4 = session_type_is_ip4 (local->session_type);
697 }
698
699 if (ls && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
700 {
701 mp->rx_fifo = pointer_to_uword (ls->server_rx_fifo);
702 mp->tx_fifo = pointer_to_uword (ls->server_tx_fifo);
703 vpp_evt_q = session_manager_get_vpp_event_queue (0);
704 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
705 }
706
707done:
708 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700709 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700710 return 0;
711}
712
Florin Coras52207f12018-07-12 14:48:06 -0700713static session_cb_vft_t session_mq_cb_vft = {
714 .session_accept_callback = mq_send_session_accepted_cb,
715 .session_disconnect_callback = mq_send_session_disconnected_cb,
716 .session_connected_callback = mq_send_session_connected_cb,
717 .session_reset_callback = mq_send_session_reset_cb,
718 .add_segment_callback = send_add_segment_callback,
719 .del_segment_callback = send_del_segment_callback,
720};
721
Dave Barach68b0fb02017-02-28 15:15:56 -0500722static void
Florin Corase04c2992017-03-01 08:17:34 -0800723vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
724{
725 vl_api_session_enable_disable_reply_t *rmp;
726 vlib_main_t *vm = vlib_get_main ();
727 int rv = 0;
728
729 vnet_session_enable_disable (vm, mp->is_enable);
730 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
731}
732
733static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700734vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500735{
Florin Coras99368312018-08-02 10:45:44 -0700736 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700737 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800738 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700739 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800740 vl_api_registration_t *reg;
Florin Corascea194d2017-10-02 00:18:51 -0700741 clib_error_t *error = 0;
Florin Coras99368312018-08-02 10:45:44 -0700742 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500743
Florin Corasfc804d92018-01-26 01:27:01 -0800744 reg = vl_api_client_index_to_registration (mp->client_index);
745 if (!reg)
746 return;
747
Florin Coras6cf30ad2017-04-04 23:08:23 -0700748 if (session_manager_is_enabled () == 0)
749 {
750 rv = VNET_API_ERROR_FEATURE_DISABLED;
751 goto done;
752 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500753
Florin Corasff6e7692017-12-11 04:59:01 -0800754 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700755 sizeof (mp->options),
756 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500757
Dave Barachb7b92992018-10-17 10:38:51 -0400758 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500759 a->api_client_index = mp->client_index;
760 a->options = mp->options;
Florin Coras52207f12018-07-12 14:48:06 -0700761
762 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
763 a->session_cb_vft = &session_mq_cb_vft;
764 else
765 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500766
Florin Corascea194d2017-10-02 00:18:51 -0700767 if (mp->namespace_id_len > 64)
768 {
769 rv = VNET_API_ERROR_INVALID_VALUE;
770 goto done;
771 }
772
773 if (mp->namespace_id_len)
774 {
Dave Wallace8af20542017-10-26 03:29:30 -0400775 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500776 clib_memcpy_fast (a->namespace_id, mp->namespace_id,
777 mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -0700778 }
779
780 if ((error = vnet_application_attach (a)))
781 {
782 rv = clib_error_get_code (error);
783 clib_error_report (error);
Florin Coras99368312018-08-02 10:45:44 -0700784 vec_free (a->namespace_id);
785 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700786 }
787 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500788
Florin Coras99368312018-08-02 10:45:44 -0700789 /* Send event queues segment */
790 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
791 {
792 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
793 fds[n_fds] = evt_q_segment->fd;
794 n_fds += 1;
795 }
796 /* Send fifo segment fd if needed */
797 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
798 {
799 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
800 fds[n_fds] = a->segment->fd;
801 n_fds += 1;
802 }
803 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
804 {
805 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
806 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
807 n_fds += 1;
808 }
809
Florin Coras6cf30ad2017-04-04 23:08:23 -0700810done:
Florin Corasa5464812017-04-19 13:00:05 -0700811
Dave Barach68b0fb02017-02-28 15:15:56 -0500812 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700813 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500814 if (!rv)
815 {
Florin Corasb384b542018-01-15 01:08:33 -0800816 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800817 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500818 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800819 rmp->segment_size = segp->ssvm_size;
820 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500821 {
Florin Corasb384b542018-01-15 01:08:33 -0800822 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
823 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500824 }
Florin Coras99368312018-08-02 10:45:44 -0700825 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
826 rmp->n_fds = n_fds;
827 rmp->fd_flags = fd_flags;
Dave Barach68b0fb02017-02-28 15:15:56 -0500828 }
829 }));
830 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800831
Florin Coras99368312018-08-02 10:45:44 -0700832 if (n_fds)
833 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500834}
835
836static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700837vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
838{
839 vl_api_application_detach_reply_t *rmp;
840 int rv = VNET_API_ERROR_INVALID_VALUE_2;
841 vnet_app_detach_args_t _a, *a = &_a;
842 application_t *app;
843
844 if (session_manager_is_enabled () == 0)
845 {
846 rv = VNET_API_ERROR_FEATURE_DISABLED;
847 goto done;
848 }
849
850 app = application_lookup (mp->client_index);
851 if (app)
852 {
Florin Coras15531972018-08-12 23:50:53 -0700853 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800854 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700855 rv = vnet_application_detach (a);
856 }
857
858done:
859 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
860}
861
862static void
863vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
864{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700865 transport_connection_t *tc = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700866 vnet_bind_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700867 vl_api_bind_uri_reply_t *rmp;
868 stream_session_t *s;
869 application_t *app = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700870 svm_msg_q_t *vpp_evt_q;
Florin Coras60116992018-08-27 09:52:18 -0700871 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700872 int rv;
873
874 if (session_manager_is_enabled () == 0)
875 {
876 rv = VNET_API_ERROR_FEATURE_DISABLED;
877 goto done;
878 }
879
880 app = application_lookup (mp->client_index);
881 if (app)
882 {
Dave Barachb7b92992018-10-17 10:38:51 -0400883 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700884 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700885 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700886 rv = vnet_bind_uri (a);
887 }
888 else
889 {
890 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
891 }
892
893done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700894
895 /* *INDENT-OFF* */
896 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
897 if (!rv)
898 {
899 rmp->handle = a->handle;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700900 if (app && application_has_global_scope (app))
901 {
902 s = listen_session_get_from_handle (a->handle);
903 tc = listen_session_get_transport (s);
Florin Coras6c354942018-04-18 00:05:21 -0700904 rmp->lcl_is_ip4 = tc->is_ip4;
905 rmp->lcl_port = tc->lcl_port;
Dave Barach178cf492018-11-13 16:34:13 -0500906 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700907 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
908 {
909 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
910 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
911 vpp_evt_q = session_manager_get_vpp_event_queue (0);
912 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
913 }
914 }
915 }
916 }));
917 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -0700918
919 /* If app uses mq for control messages, send an mq message as well */
920 if (app && application_use_mq_for_ctrl (app))
921 {
922 app_wrk = application_get_worker (app, 0);
923 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
924 rv);
925 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700926}
927
928static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500929vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
930{
931 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700932 application_t *app;
933 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500934 int rv;
935
Florin Coras6cf30ad2017-04-04 23:08:23 -0700936 if (session_manager_is_enabled () == 0)
937 {
938 rv = VNET_API_ERROR_FEATURE_DISABLED;
939 goto done;
940 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500941
Florin Coras6cf30ad2017-04-04 23:08:23 -0700942 app = application_lookup (mp->client_index);
943 if (app)
944 {
945 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700946 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700947 rv = vnet_unbind_uri (a);
948 }
949 else
950 {
951 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
952 }
953
954done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500955 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
956}
957
Florin Corasf03a59a2017-06-09 21:07:32 -0700958static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500959vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
960{
Dave Wallace33e002b2017-09-06 01:20:02 -0400961 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500962 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700963 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700964 clib_error_t *error = 0;
965 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500966
Florin Coras6cf30ad2017-04-04 23:08:23 -0700967 if (session_manager_is_enabled () == 0)
968 {
969 rv = VNET_API_ERROR_FEATURE_DISABLED;
970 goto done;
971 }
Florin Corase04c2992017-03-01 08:17:34 -0800972
Florin Coras6cf30ad2017-04-04 23:08:23 -0700973 app = application_lookup (mp->client_index);
974 if (app)
975 {
Dave Barachb7b92992018-10-17 10:38:51 -0400976 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700977 a->uri = (char *) mp->uri;
978 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -0700979 a->app_index = app->app_index;
Florin Corascea194d2017-10-02 00:18:51 -0700980 if ((error = vnet_connect_uri (a)))
981 {
982 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -0800983 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -0700984 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700985 }
986 else
987 {
988 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
989 }
Florin Corase04c2992017-03-01 08:17:34 -0800990
Florin Coras3cbc04b2017-10-02 00:18:51 -0700991 /*
992 * Don't reply to stream (tcp) connects. The reply will come once
993 * the connection is established. In case of the redirects, the reply
994 * will come from the server app.
995 */
Florin Coras8f89dd02018-03-05 16:53:07 -0800996 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800997 return;
998
Florin Coras6cf30ad2017-04-04 23:08:23 -0700999done:
Florin Corase04c2992017-03-01 08:17:34 -08001000 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -04001001 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -08001002 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001003}
1004
1005static void
1006vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
1007{
1008 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001009 vnet_disconnect_args_t _a, *a = &_a;
1010 application_t *app;
1011 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001012
Florin Coras6cf30ad2017-04-04 23:08:23 -07001013 if (session_manager_is_enabled () == 0)
1014 {
1015 rv = VNET_API_ERROR_FEATURE_DISABLED;
1016 goto done;
1017 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001018
Florin Coras6cf30ad2017-04-04 23:08:23 -07001019 app = application_lookup (mp->client_index);
1020 if (app)
1021 {
1022 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001023 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001024 rv = vnet_disconnect_session (a);
1025 }
1026 else
1027 {
1028 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1029 }
1030
1031done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001032 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001033}
1034
1035static void
1036vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1037 mp)
1038{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001039 vnet_disconnect_args_t _a, *a = &_a;
1040 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001041
1042 /* Client objected to disconnecting the session, log and continue */
1043 if (mp->retval)
1044 {
1045 clib_warning ("client retval %d", mp->retval);
1046 return;
1047 }
1048
1049 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001050 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001051 if (app)
1052 {
1053 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001054 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001055 vnet_disconnect_session (a);
1056 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001057}
1058
1059static void
1060vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
1061{
Florin Coras15531972018-08-12 23:50:53 -07001062 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001063 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001064 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001065 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05001066
Ondrej Fabry6bd197e2018-08-15 08:46:46 +02001067 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001068 if (!app)
1069 return;
1070
Florin Corascea194d2017-10-02 00:18:51 -07001071 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001072 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -07001073 if (!s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001074 {
1075 clib_warning ("Invalid session!");
1076 return;
1077 }
1078
Florin Coras15531972018-08-12 23:50:53 -07001079 app_wrk = app_worker_get (s->app_wrk_index);
1080 if (app_wrk->app_index != app->app_index)
1081 {
1082 clib_warning ("app %u does not own handle 0x%lx", app->app_index,
1083 mp->handle);
1084 return;
1085 }
1086
Dave Barach68b0fb02017-02-28 15:15:56 -05001087 /* Client objected to resetting the session, log and continue */
1088 if (mp->retval)
1089 {
1090 clib_warning ("client retval %d", mp->retval);
1091 return;
1092 }
1093
Dave Barach68b0fb02017-02-28 15:15:56 -05001094 /* This comes as a response to a reset, transport only waiting for
1095 * confirmation to remove connection state, no need to disconnect */
1096 stream_session_cleanup (s);
1097}
1098
1099static void
1100vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
1101{
Florin Corasf8f516a2018-02-08 15:10:09 -08001102 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
1103 local_session_t *ls;
Dave Barach68b0fb02017-02-28 15:15:56 -05001104 stream_session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001105
Florin Corasa5464812017-04-19 13:00:05 -07001106 /* Server isn't interested, kill the session */
1107 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -05001108 {
Florin Corasa5464812017-04-19 13:00:05 -07001109 a->app_index = mp->context;
1110 a->handle = mp->handle;
1111 vnet_disconnect_session (a);
Florin Corasf8f516a2018-02-08 15:10:09 -08001112 return;
1113 }
1114
1115 if (session_handle_is_local (mp->handle))
1116 {
1117 ls = application_get_local_session_from_handle (mp->handle);
Florin Coras15531972018-08-12 23:50:53 -07001118 if (!ls || ls->app_wrk_index != mp->context)
Florin Corasf8f516a2018-02-08 15:10:09 -08001119 {
1120 clib_warning ("server %u doesn't own local handle %llu",
1121 mp->context, mp->handle);
1122 return;
1123 }
1124 if (application_local_session_connect_notify (ls))
1125 return;
1126 ls->session_state = SESSION_STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001127 }
Florin Corasa5464812017-04-19 13:00:05 -07001128 else
1129 {
Florin Corasf8f516a2018-02-08 15:10:09 -08001130 s = session_get_from_handle_if_valid (mp->handle);
Florin Corasa5464812017-04-19 13:00:05 -07001131 if (!s)
1132 {
1133 clib_warning ("session doesn't exist");
1134 return;
1135 }
Florin Coras15531972018-08-12 23:50:53 -07001136 if (s->app_wrk_index != mp->context)
Florin Corasa5464812017-04-19 13:00:05 -07001137 {
1138 clib_warning ("app doesn't own session");
1139 return;
1140 }
Florin Corasa5464812017-04-19 13:00:05 -07001141 s->session_state = SESSION_STATE_READY;
1142 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001143}
1144
1145static void
1146vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1147 * mp)
1148{
1149 clib_warning ("not implemented");
1150}
1151
1152static void
1153vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1154{
1155 vl_api_bind_sock_reply_t *rmp;
1156 vnet_bind_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -07001157 int rv = 0;
1158 clib_error_t *error;
Florin Coraseb2945c2017-11-22 10:39:09 -08001159 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001160 app_worker_t *app_wrk;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001161 stream_session_t *s;
1162 transport_connection_t *tc = 0;
1163 ip46_address_t *ip46;
Florin Coras3c2fed52018-07-04 04:15:05 -07001164 svm_msg_q_t *vpp_evt_q;
Dave Barach68b0fb02017-02-28 15:15:56 -05001165
Florin Coras6cf30ad2017-04-04 23:08:23 -07001166 if (session_manager_is_enabled () == 0)
1167 {
1168 rv = VNET_API_ERROR_FEATURE_DISABLED;
1169 goto done;
1170 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001171
Florin Coras6cf30ad2017-04-04 23:08:23 -07001172 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001173 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001174 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001175 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1176 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001177 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001178
1179 ip46 = (ip46_address_t *) mp->ip;
Dave Barachb7b92992018-10-17 10:38:51 -04001180 clib_memset (a, 0, sizeof (*a));
Florin Corasdcf55ce2017-11-16 15:32:50 -08001181 a->sep.is_ip4 = mp->is_ip4;
1182 a->sep.ip = *ip46;
1183 a->sep.port = mp->port;
1184 a->sep.fib_index = mp->vrf;
1185 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1186 a->sep.transport_proto = mp->proto;
Florin Coras15531972018-08-12 23:50:53 -07001187 a->app_index = app->app_index;
1188 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001189
1190 if ((error = vnet_bind (a)))
1191 {
1192 rv = clib_error_get_code (error);
1193 clib_error_report (error);
1194 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001195
Florin Coras6cf30ad2017-04-04 23:08:23 -07001196done:
Florin Corascea194d2017-10-02 00:18:51 -07001197 /* *INDENT-OFF* */
1198 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1199 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -08001200 {
1201 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -08001202 rmp->lcl_port = mp->port;
Keith Burns (alagalah)60ae80f2018-03-15 10:04:20 -07001203 rmp->lcl_is_ip4 = mp->is_ip4;
Dave Wallace97b1a272017-12-18 13:51:59 -05001204 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -08001205 {
1206 s = listen_session_get_from_handle (a->handle);
1207 tc = listen_session_get_transport (s);
Dave Barach178cf492018-11-13 16:34:13 -05001208 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001209 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1210 {
1211 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
1212 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
1213 vpp_evt_q = session_manager_get_vpp_event_queue (0);
1214 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1215 }
Florin Coraseb2945c2017-11-22 10:39:09 -08001216 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001217 }
Florin Corascea194d2017-10-02 00:18:51 -07001218 }));
1219 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -07001220
1221 /* If app uses mq for control messages, send an mq message as well */
1222 if (app && application_use_mq_for_ctrl (app))
1223 {
1224 app_wrk = application_get_worker (app, mp->wrk_index);
1225 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1226 rv);
1227 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001228}
1229
1230static void
1231vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1232{
1233 vl_api_unbind_sock_reply_t *rmp;
1234 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001235 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -07001236 clib_error_t *error;
1237 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001238
Florin Coras6cf30ad2017-04-04 23:08:23 -07001239 if (session_manager_is_enabled () == 0)
1240 {
1241 rv = VNET_API_ERROR_FEATURE_DISABLED;
1242 goto done;
1243 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001244
Florin Coras6cf30ad2017-04-04 23:08:23 -07001245 app = application_lookup (mp->client_index);
1246 if (app)
1247 {
Florin Coras15531972018-08-12 23:50:53 -07001248 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001249 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001250 a->wrk_map_index = mp->wrk_index;
Florin Corascea194d2017-10-02 00:18:51 -07001251 if ((error = vnet_unbind (a)))
1252 {
1253 rv = clib_error_get_code (error);
1254 clib_error_report (error);
1255 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001256 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001257
Florin Coras6cf30ad2017-04-04 23:08:23 -07001258done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001259 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
1260}
1261
1262static void
1263vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1264{
Dave Wallace33e002b2017-09-06 01:20:02 -04001265 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001266 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001267 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001268 clib_error_t *error = 0;
1269 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001270
Florin Coras6cf30ad2017-04-04 23:08:23 -07001271 if (session_manager_is_enabled () == 0)
1272 {
1273 rv = VNET_API_ERROR_FEATURE_DISABLED;
1274 goto done;
1275 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001276
Florin Coras6cf30ad2017-04-04 23:08:23 -07001277 app = application_lookup (mp->client_index);
1278 if (app)
1279 {
Florin Corase86a8ed2018-01-05 03:20:25 -08001280 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -04001281 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001282
Dave Barachb7b92992018-10-17 10:38:51 -04001283 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001284 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1285 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -07001286 a->sep.is_ip4 = mp->is_ip4;
1287 a->sep.ip = *ip46;
1288 a->sep.port = mp->port;
1289 a->sep.transport_proto = mp->proto;
Florin Coras5665ced2018-10-25 18:03:45 -07001290 a->sep.peer.fib_index = mp->vrf;
1291 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras8f89dd02018-03-05 16:53:07 -08001292 if (mp->hostname_len)
1293 {
Florin Coras15531972018-08-12 23:50:53 -07001294 vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001295 clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
1296 mp->hostname_len);
Florin Coras8f89dd02018-03-05 16:53:07 -08001297 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001298 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001299 a->app_index = app->app_index;
1300 a->wrk_map_index = mp->wrk_index;
Florin Corascea194d2017-10-02 00:18:51 -07001301 if ((error = vnet_connect (a)))
1302 {
1303 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -08001304 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -07001305 }
Florin Coras15531972018-08-12 23:50:53 -07001306 vec_free (a->sep_ext.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001307 }
1308 else
1309 {
1310 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1311 }
Florin Corase04c2992017-03-01 08:17:34 -08001312
Florin Coras8f89dd02018-03-05 16:53:07 -08001313 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001314 return;
1315
1316 /* Got some error, relay it */
1317
Florin Coras6cf30ad2017-04-04 23:08:23 -07001318done:
Dave Wallace33e002b2017-09-06 01:20:02 -04001319 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corasab2f6db2018-08-31 14:31:41 -07001320
1321 if (app && application_use_mq_for_ctrl (app))
1322 {
1323 app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1324 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1325 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001326}
1327
Florin Corascea194d2017-10-02 00:18:51 -07001328static void
Florin Coras15531972018-08-12 23:50:53 -07001329vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1330{
1331 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1332 vl_api_app_worker_add_del_reply_t *rmp;
1333 vl_api_registration_t *reg;
1334 clib_error_t *error = 0;
1335 application_t *app;
1336 u8 fd_flags = 0;
1337
1338 if (!session_manager_is_enabled ())
1339 {
1340 rv = VNET_API_ERROR_FEATURE_DISABLED;
1341 goto done;
1342 }
1343
1344 reg = vl_api_client_index_to_registration (mp->client_index);
1345 if (!reg)
1346 return;
1347
Florin Corasc1f5a432018-11-20 11:31:26 -08001348 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001349 if (!app)
1350 {
1351 rv = VNET_API_ERROR_INVALID_VALUE;
1352 goto done;
1353 }
1354
1355 vnet_app_worker_add_del_args_t args = {
1356 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001357 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001358 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001359 .is_add = mp->is_add
1360 };
1361 error = vnet_app_worker_add_del (&args);
1362 if (error)
1363 {
1364 rv = clib_error_get_code (error);
1365 clib_error_report (error);
1366 goto done;
1367 }
1368
Florin Coras14598772018-09-04 19:47:52 -07001369 if (!mp->is_add)
1370 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001371
Florin Coras15531972018-08-12 23:50:53 -07001372 /* Send fifo segment fd if needed */
1373 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1374 {
1375 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1376 fds[n_fds] = args.segment->fd;
1377 n_fds += 1;
1378 }
1379 if (application_segment_manager_properties (app)->use_mq_eventfd)
1380 {
1381 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1382 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1383 n_fds += 1;
1384 }
1385
1386 /* *INDENT-OFF* */
1387done:
1388 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1389 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001390 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001391 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001392 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001393 {
Florin Coras15531972018-08-12 23:50:53 -07001394 if (vec_len (args.segment->name))
1395 {
1396 memcpy (rmp->segment_name, args.segment->name,
1397 vec_len (args.segment->name));
1398 rmp->segment_name_length = vec_len (args.segment->name);
1399 }
1400 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1401 rmp->n_fds = n_fds;
1402 rmp->fd_flags = fd_flags;
1403 }
1404 }));
1405 /* *INDENT-ON* */
1406
1407 if (n_fds)
1408 session_send_fds (reg, fds, n_fds);
1409}
1410
1411static void
Florin Corascea194d2017-10-02 00:18:51 -07001412vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1413{
1414 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Corascea194d2017-10-02 00:18:51 -07001415 clib_error_t *error = 0;
Florin Coras6e8c6672017-11-10 09:03:54 -08001416 u32 appns_index = 0;
1417 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001418 int rv = 0;
1419 if (!session_manager_is_enabled ())
1420 {
1421 rv = VNET_API_ERROR_FEATURE_DISABLED;
1422 goto done;
1423 }
1424
1425 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1426 {
1427 rv = VNET_API_ERROR_INVALID_VALUE;
1428 goto done;
1429 }
1430
1431 vec_validate (ns_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001432 clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -07001433 vnet_app_namespace_add_del_args_t args = {
1434 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001435 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001436 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1437 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1438 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1439 .is_add = 1
1440 };
1441 error = vnet_app_namespace_add_del (&args);
1442 if (error)
1443 {
1444 rv = clib_error_get_code (error);
1445 clib_error_report (error);
1446 }
Florin Coras6e8c6672017-11-10 09:03:54 -08001447 else
1448 {
1449 appns_index = app_namespace_index_from_id (ns_id);
1450 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1451 {
1452 clib_warning ("app ns lookup failed");
1453 rv = VNET_API_ERROR_UNSPECIFIED;
1454 }
1455 }
Florin Corascea194d2017-10-02 00:18:51 -07001456 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -08001457
1458 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -07001459done:
Florin Coras6e8c6672017-11-10 09:03:54 -08001460 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1461 if (!rv)
1462 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1463 }));
1464 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -07001465}
1466
Florin Coras1c710452017-10-17 00:03:13 -07001467static void
1468vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1469{
1470 vl_api_session_rule_add_del_reply_t *rmp;
1471 session_rule_add_del_args_t args;
1472 session_rule_table_add_del_args_t *table_args = &args.table_args;
1473 clib_error_t *error;
1474 u8 fib_proto;
1475 int rv = 0;
1476
Dave Barachb7b92992018-10-17 10:38:51 -04001477 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001478 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1479
1480 table_args->lcl.fp_len = mp->lcl_plen;
1481 table_args->lcl.fp_proto = fib_proto;
1482 table_args->rmt.fp_len = mp->rmt_plen;
1483 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001484 table_args->lcl_port = mp->lcl_port;
1485 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001486 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1487 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001488 mp->tag[sizeof (mp->tag) - 1] = 0;
1489 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001490 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1491 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -08001492 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -07001493
Dave Barachb7b92992018-10-17 10:38:51 -04001494 clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1495 clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
Florin Coras1c710452017-10-17 00:03:13 -07001496 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1497 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
1498 error = vnet_session_rule_add_del (&args);
1499 if (error)
1500 {
1501 rv = clib_error_get_code (error);
1502 clib_error_report (error);
1503 }
Florin Corasc97a7392017-11-05 23:07:07 -08001504 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001505 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1506}
1507
Florin Coras6c36f532017-11-03 18:32:34 -07001508static void
1509send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001510 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001511 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001512{
1513 vl_api_session_rules_details_t *rmp = 0;
1514 session_mask_or_match_4_t *match =
1515 (session_mask_or_match_4_t *) & rule->match;
1516 session_mask_or_match_4_t *mask =
1517 (session_mask_or_match_4_t *) & rule->mask;
1518
1519 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001520 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001521 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1522 rmp->context = context;
1523
1524 rmp->is_ip4 = 1;
Dave Barach178cf492018-11-13 16:34:13 -05001525 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1526 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001527 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1528 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001529 rmp->lcl_port = match->lcl_port;
1530 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001531 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1532 rmp->scope =
1533 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1534 rmp->transport_proto = transport_proto;
1535 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001536 if (tag)
1537 {
Dave Barach178cf492018-11-13 16:34:13 -05001538 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001539 rmp->tag[vec_len (tag)] = 0;
1540 }
Florin Coras6c36f532017-11-03 18:32:34 -07001541
Florin Coras6c4dae22018-01-09 06:39:23 -08001542 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001543}
1544
1545static void
Florin Corasc97a7392017-11-05 23:07:07 -08001546send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1547 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001548 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001549{
1550 vl_api_session_rules_details_t *rmp = 0;
1551 session_mask_or_match_6_t *match =
1552 (session_mask_or_match_6_t *) & rule->match;
1553 session_mask_or_match_6_t *mask =
1554 (session_mask_or_match_6_t *) & rule->mask;
1555
1556 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001557 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001558 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1559 rmp->context = context;
1560
1561 rmp->is_ip4 = 0;
Dave Barach178cf492018-11-13 16:34:13 -05001562 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1563 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001564 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1565 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001566 rmp->lcl_port = match->lcl_port;
1567 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001568 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001569 rmp->scope =
1570 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -07001571 rmp->transport_proto = transport_proto;
1572 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001573 if (tag)
1574 {
Dave Barach178cf492018-11-13 16:34:13 -05001575 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001576 rmp->tag[vec_len (tag)] = 0;
1577 }
Florin Coras6c36f532017-11-03 18:32:34 -07001578
Florin Coras6c4dae22018-01-09 06:39:23 -08001579 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001580}
1581
1582static void
1583send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001584 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001585 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001586{
1587 mma_rule_16_t *rule16;
1588 mma_rule_40_t *rule40;
1589 mma_rules_table_16_t *srt16;
1590 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001591 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001592
Florin Corasc97a7392017-11-05 23:07:07 -08001593 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001594 {
Florin Corasc97a7392017-11-05 23:07:07 -08001595 u8 *tag = 0;
1596 /* *INDENT-OFF* */
1597 srt16 = &srt->session_rules_tables_16;
1598 pool_foreach (rule16, srt16->rules, ({
1599 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1600 tag = session_rules_table_rule_tag (srt, ri, 1);
1601 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001602 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001603 }));
1604 /* *INDENT-ON* */
1605 }
1606 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1607 {
1608 u8 *tag = 0;
1609 /* *INDENT-OFF* */
1610 srt40 = &srt->session_rules_tables_40;
1611 pool_foreach (rule40, srt40->rules, ({
1612 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1613 tag = session_rules_table_rule_tag (srt, ri, 1);
1614 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001615 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001616 }));
1617 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001618 }
1619}
1620
1621static void
1622vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1623{
Florin Coras6c4dae22018-01-09 06:39:23 -08001624 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001625 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001626 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001627
Florin Coras6c4dae22018-01-09 06:39:23 -08001628 reg = vl_api_client_index_to_registration (mp->client_index);
1629 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001630 return;
1631
1632 /* *INDENT-OFF* */
1633 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001634 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1635 {
1636 send_session_rules_table_details (&st->session_rules[tp],
1637 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001638 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001639 mp->context);
1640 }
Florin Coras6c36f532017-11-03 18:32:34 -07001641 }));
1642 /* *INDENT-ON* */
1643}
1644
Florin Coras371ca502018-02-21 12:07:41 -08001645static void
1646vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1647 mp)
1648{
1649 vl_api_app_namespace_add_del_reply_t *rmp;
1650 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1651 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001652 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001653 u32 cert_len;
1654 int rv = 0;
1655 if (!session_manager_is_enabled ())
1656 {
1657 rv = VNET_API_ERROR_FEATURE_DISABLED;
1658 goto done;
1659 }
Florin Corase3e2f072018-03-04 07:24:30 -08001660 if (!(app = application_lookup (mp->client_index)))
1661 {
1662 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1663 goto done;
1664 }
Dave Barachb7b92992018-10-17 10:38:51 -04001665 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001666 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001667 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001668 if (cert_len > 10000)
1669 {
1670 rv = VNET_API_ERROR_INVALID_VALUE;
1671 goto done;
1672 }
Florin Coras371ca502018-02-21 12:07:41 -08001673 vec_validate (a->cert, cert_len);
Dave Barach178cf492018-11-13 16:34:13 -05001674 clib_memcpy_fast (a->cert, mp->cert, cert_len);
Florin Coras371ca502018-02-21 12:07:41 -08001675 if ((error = vnet_app_add_tls_cert (a)))
1676 {
1677 rv = clib_error_get_code (error);
1678 clib_error_report (error);
1679 }
1680 vec_free (a->cert);
1681done:
1682 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1683}
1684
1685static void
1686vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1687 mp)
1688{
1689 vl_api_app_namespace_add_del_reply_t *rmp;
1690 vnet_app_add_tls_key_args_t _a, *a = &_a;
1691 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001692 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001693 u32 key_len;
1694 int rv = 0;
1695 if (!session_manager_is_enabled ())
1696 {
1697 rv = VNET_API_ERROR_FEATURE_DISABLED;
1698 goto done;
1699 }
Florin Corase3e2f072018-03-04 07:24:30 -08001700 if (!(app = application_lookup (mp->client_index)))
1701 {
1702 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1703 goto done;
1704 }
Dave Barachb7b92992018-10-17 10:38:51 -04001705 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001706 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001707 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001708 if (key_len > 10000)
1709 {
1710 rv = VNET_API_ERROR_INVALID_VALUE;
1711 goto done;
1712 }
Florin Coras371ca502018-02-21 12:07:41 -08001713 vec_validate (a->key, key_len);
Dave Barach178cf492018-11-13 16:34:13 -05001714 clib_memcpy_fast (a->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001715 if ((error = vnet_app_add_tls_key (a)))
1716 {
1717 rv = clib_error_get_code (error);
1718 clib_error_report (error);
1719 }
1720 vec_free (a->key);
1721done:
1722 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1723}
1724
Florin Coras6cf30ad2017-04-04 23:08:23 -07001725static clib_error_t *
1726application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001727{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001728 application_t *app = application_lookup (client_index);
1729 vnet_app_detach_args_t _a, *a = &_a;
1730 if (app)
1731 {
Florin Coras15531972018-08-12 23:50:53 -07001732 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001733 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001734 vnet_application_detach (a);
1735 }
1736 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001737}
1738
Florin Coras6cf30ad2017-04-04 23:08:23 -07001739VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001740
1741#define vl_msg_name_crc_list
1742#include <vnet/vnet_all_api_h.h>
1743#undef vl_msg_name_crc_list
1744
1745static void
1746setup_message_id_table (api_main_t * am)
1747{
1748#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1749 foreach_vl_msg_name_crc_session;
1750#undef _
1751}
1752
1753/*
1754 * session_api_hookup
1755 * Add uri's API message handlers to the table.
1756 * vlib has alread mapped shared memory and
1757 * added the client registration handlers.
1758 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1759 */
1760static clib_error_t *
1761session_api_hookup (vlib_main_t * vm)
1762{
1763 api_main_t *am = &api_main;
1764
1765#define _(N,n) \
1766 vl_msg_api_set_handlers(VL_API_##N, #n, \
1767 vl_api_##n##_t_handler, \
1768 vl_noop_handler, \
1769 vl_api_##n##_t_endian, \
1770 vl_api_##n##_t_print, \
1771 sizeof(vl_api_##n##_t), 1);
1772 foreach_session_api_msg;
1773#undef _
1774
1775 /*
1776 * Messages which bounce off the data-plane to
1777 * an API client. Simply tells the message handling infra not
1778 * to free the message.
1779 *
1780 * Bounced message handlers MUST NOT block the data plane
1781 */
1782 am->message_bounce[VL_API_CONNECT_URI] = 1;
1783 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1784
1785 /*
1786 * Set up the (msg_name, crc, message-id) table
1787 */
1788 setup_message_id_table (am);
1789
1790 return 0;
1791}
1792
1793VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001794
Dave Barach68b0fb02017-02-28 15:15:56 -05001795/*
1796 * fd.io coding-style-patch-verification: ON
1797 *
1798 * Local Variables:
1799 * eval: (c-set-style "gnu")
1800 * End:
1801 */