blob: 2decd9b8a2e7ee5051c8b1493158f48df13550a5 [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++;
Florin Corase2ea1932018-12-17 23:08:14 -0800411 usleep (1);
Florin Coras83ea6692018-10-12 16:55:14 -0700412 }
Florin Corasdc2e2512018-12-03 17:47:26 -0800413 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700414 return -1;
415}
416
417static int
Florin Coras52207f12018-07-12 14:48:06 -0700418mq_send_session_accepted_cb (stream_session_t * s)
419{
Florin Coras15531972018-08-12 23:50:53 -0700420 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700421 svm_msg_q_msg_t _msg, *msg = &_msg;
422 svm_msg_q_t *vpp_queue, *app_mq;
423 transport_proto_vft_t *tp_vft;
424 transport_connection_t *tc;
425 stream_session_t *listener;
426 session_accepted_msg_t *mp;
427 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700428 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700429
Florin Coras15531972018-08-12 23:50:53 -0700430 app = application_get (app_wrk->app_index);
431 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700432 if (mq_try_lock_and_alloc_msg (app_mq, msg))
433 return -1;
Florin Coras52207f12018-07-12 14:48:06 -0700434
435 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400436 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700437 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
438 mp = (session_accepted_msg_t *) evt->data;
Florin Corasab2f6db2018-08-31 14:31:41 -0700439 mp->context = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700440 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
441 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800442 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700443
444 if (session_has_transport (s))
445 {
446 listener = listen_session_get (s->listener_index);
447 mp->listener_handle = listen_session_get_handle (listener);
448 if (application_is_proxy (app))
449 {
450 listener =
Florin Coras15531972018-08-12 23:50:53 -0700451 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
452 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700453 if (listener)
454 mp->listener_handle = listen_session_get_handle (listener);
455 }
456 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
457 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
458 mp->handle = session_handle (s);
459 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
460 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
461 mp->port = tc->rmt_port;
462 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500463 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700464 }
465 else
466 {
467 local_session_t *ls = (local_session_t *) s;
468 local_session_t *ll;
Florin Coras99368312018-08-02 10:45:44 -0700469 u8 main_thread = vlib_num_workers ()? 1 : 0;
470
Florin Corasc1f5a432018-11-20 11:31:26 -0800471 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700472 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700473 ls->server_evt_q,
474 ls->client_evt_q);
475
Florin Coras52207f12018-07-12 14:48:06 -0700476 if (application_local_session_listener_has_transport (ls))
477 {
478 listener = listen_session_get (ls->listener_index);
479 mp->listener_handle = listen_session_get_handle (listener);
480 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
481 }
482 else
483 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700484 ll = application_get_local_listen_session (app, ls->listener_index);
Florin Coras52207f12018-07-12 14:48:06 -0700485 if (ll->transport_listener_index != ~0)
486 {
487 listener = listen_session_get (ll->transport_listener_index);
488 mp->listener_handle = listen_session_get_handle (listener);
489 }
490 else
491 {
492 mp->listener_handle = application_local_session_handle (ll);
493 }
494 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
495 }
496 mp->handle = application_local_session_handle (ls);
497 mp->port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700498 vpp_queue = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700499 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
500 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700501 mp->server_event_queue_address = ls->server_evt_q;
502 }
Florin Coras537b17e2018-09-28 10:35:45 -0700503 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700504
505 return 0;
506}
507
Florin Coras72b04282019-01-14 17:23:11 -0800508static inline void
509mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
510 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700511{
Florin Coras52207f12018-07-12 14:48:06 -0700512 svm_msg_q_msg_t _msg, *msg = &_msg;
513 session_disconnected_msg_t *mp;
514 svm_msg_q_t *app_mq;
515 session_event_t *evt;
516
Florin Coras15531972018-08-12 23:50:53 -0700517 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700518 if (mq_try_lock_and_alloc_msg (app_mq, msg))
519 return;
Florin Coras52207f12018-07-12 14:48:06 -0700520 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400521 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800522 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700523 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800524 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800525 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700526 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700527}
528
Florin Coras72b04282019-01-14 17:23:11 -0800529static inline void
530mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
531 svm_fifo_t * f, session_evt_type_t evt_type)
532{
533 app_worker_t *app_wrk;
534 application_t *app;
535 int i;
536
537 app = application_get (app_index);
538 if (!app)
539 return;
540
541 for (i = 0; i < f->n_subscribers; i++)
542 {
543 if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
544 continue;
545 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
546 }
547}
548
549static void
550mq_send_session_disconnected_cb (stream_session_t * s)
551{
552 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
553 session_handle_t sh = session_handle (s);
554
555 mq_send_session_close_evt (app_wrk, session_handle (s),
556 SESSION_CTRL_EVT_DISCONNECTED);
557
558 if (svm_fifo_n_subscribers (s->server_rx_fifo))
559 mq_notify_close_subscribers (app_wrk->app_index, sh, s->server_rx_fifo,
560 SESSION_CTRL_EVT_DISCONNECTED);
561}
562
Florin Corasaa27eb92018-10-13 12:20:01 -0700563void
564mq_send_local_session_disconnected_cb (u32 app_wrk_index,
565 local_session_t * ls)
566{
567 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
Florin Coras72b04282019-01-14 17:23:11 -0800568 session_handle_t sh = application_local_session_handle (ls);
Florin Corasaa27eb92018-10-13 12:20:01 -0700569
Florin Coras72b04282019-01-14 17:23:11 -0800570 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
571
572 if (svm_fifo_n_subscribers (ls->server_rx_fifo))
573 mq_notify_close_subscribers (app_wrk->app_index, sh, ls->server_rx_fifo,
574 SESSION_CTRL_EVT_DISCONNECTED);
Florin Corasaa27eb92018-10-13 12:20:01 -0700575}
576
Florin Coras52207f12018-07-12 14:48:06 -0700577static void
578mq_send_session_reset_cb (stream_session_t * s)
579{
Florin Coras72b04282019-01-14 17:23:11 -0800580 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
581 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700582
Florin Coras72b04282019-01-14 17:23:11 -0800583 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
584
585 if (svm_fifo_n_subscribers (s->server_rx_fifo))
586 mq_notify_close_subscribers (app_wrk->app_index, sh, s->server_rx_fifo,
587 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700588}
589
590static int
Florin Coras15531972018-08-12 23:50:53 -0700591mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras52207f12018-07-12 14:48:06 -0700592 stream_session_t * s, u8 is_fail)
593{
594 svm_msg_q_msg_t _msg, *msg = &_msg;
595 session_connected_msg_t *mp;
596 svm_msg_q_t *vpp_mq, *app_mq;
597 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700598 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700599 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700600
Florin Coras15531972018-08-12 23:50:53 -0700601 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700602 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700603 if (!app_mq)
604 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800605 clib_warning ("app %u with api index: %u not attached",
606 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700607 return -1;
608 }
609
Florin Coras83ea6692018-10-12 16:55:14 -0700610 if (mq_try_lock_and_alloc_msg (app_mq, msg))
611 return -1;
Florin Corasdc2e2512018-12-03 17:47:26 -0800612
Florin Coras52207f12018-07-12 14:48:06 -0700613 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400614 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700615 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
616 mp = (session_connected_msg_t *) evt->data;
617 mp->context = api_context;
618
619 if (is_fail)
620 goto done;
621
Florin Corasdc2e2512018-12-03 17:47:26 -0800622 mp->segment_handle = session_segment_handle (s);
623
Florin Coras52207f12018-07-12 14:48:06 -0700624 if (session_has_transport (s))
625 {
626 tc = session_get_transport (s);
627 if (!tc)
628 {
629 is_fail = 1;
630 goto done;
631 }
632
633 vpp_mq = session_manager_get_vpp_event_queue (s->thread_index);
634 mp->handle = session_handle (s);
635 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Dave Barach178cf492018-11-13 16:34:13 -0500636 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700637 mp->is_ip4 = tc->is_ip4;
638 mp->lcl_port = tc->lcl_port;
639 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
640 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
641 }
642 else
643 {
644 local_session_t *ls = (local_session_t *) s;
Florin Coras99368312018-08-02 10:45:44 -0700645 u8 main_thread = vlib_num_workers ()? 1 : 0;
646
Florin Corasc1f5a432018-11-20 11:31:26 -0800647 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700648 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700649 ls->client_evt_q,
650 ls->server_evt_q);
651
Florin Coras52207f12018-07-12 14:48:06 -0700652 mp->handle = application_local_session_handle (ls);
653 mp->lcl_port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700654 vpp_mq = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700655 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras52207f12018-07-12 14:48:06 -0700656 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700657 mp->server_event_queue_address = ls->server_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700658 mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
659 mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
660 }
661
662done:
663 mp->retval = is_fail ?
664 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
665
Florin Coras537b17e2018-09-28 10:35:45 -0700666 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700667 return 0;
668}
669
Florin Coras60116992018-08-27 09:52:18 -0700670static int
671mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
672 session_handle_t handle, int rv)
673{
674 svm_msg_q_msg_t _msg, *msg = &_msg;
675 svm_msg_q_t *app_mq, *vpp_evt_q;
676 transport_connection_t *tc;
677 stream_session_t *ls = 0;
678 session_bound_msg_t *mp;
679 app_worker_t *app_wrk;
680 session_event_t *evt;
681 application_t *app;
682
683 app_wrk = app_worker_get (app_wrk_index);
684 app = application_get (app_wrk->app_index);
685 app_mq = app_wrk->event_queue;
686 if (!app_mq)
687 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800688 clib_warning ("app %u with api index: %u not attached",
689 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700690 return -1;
691 }
692
Florin Coras83ea6692018-10-12 16:55:14 -0700693 if (mq_try_lock_and_alloc_msg (app_mq, msg))
694 return -1;
695
Florin Coras60116992018-08-27 09:52:18 -0700696 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400697 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700698 evt->event_type = SESSION_CTRL_EVT_BOUND;
699 mp = (session_bound_msg_t *) evt->data;
700 mp->context = api_context;
701
702 if (rv)
703 goto done;
704
705 mp->handle = handle;
706 if (application_has_global_scope (app))
707 {
708 ls = listen_session_get_from_handle (handle);
709 tc = listen_session_get_transport (ls);
710 mp->lcl_port = tc->lcl_port;
711 mp->lcl_is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500712 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras60116992018-08-27 09:52:18 -0700713 }
714 else
715 {
716 local_session_t *local;
Florin Corasab2f6db2018-08-31 14:31:41 -0700717 local = application_get_local_listener_w_handle (handle);
Florin Coras60116992018-08-27 09:52:18 -0700718 mp->lcl_port = local->port;
719 mp->lcl_is_ip4 = session_type_is_ip4 (local->session_type);
720 }
721
722 if (ls && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
723 {
724 mp->rx_fifo = pointer_to_uword (ls->server_rx_fifo);
725 mp->tx_fifo = pointer_to_uword (ls->server_tx_fifo);
726 vpp_evt_q = session_manager_get_vpp_event_queue (0);
727 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
728 }
729
730done:
731 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700732 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700733 return 0;
734}
735
Florin Coras52207f12018-07-12 14:48:06 -0700736static session_cb_vft_t session_mq_cb_vft = {
737 .session_accept_callback = mq_send_session_accepted_cb,
738 .session_disconnect_callback = mq_send_session_disconnected_cb,
739 .session_connected_callback = mq_send_session_connected_cb,
740 .session_reset_callback = mq_send_session_reset_cb,
741 .add_segment_callback = send_add_segment_callback,
742 .del_segment_callback = send_del_segment_callback,
743};
744
Dave Barach68b0fb02017-02-28 15:15:56 -0500745static void
Florin Corase04c2992017-03-01 08:17:34 -0800746vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
747{
748 vl_api_session_enable_disable_reply_t *rmp;
749 vlib_main_t *vm = vlib_get_main ();
750 int rv = 0;
751
752 vnet_session_enable_disable (vm, mp->is_enable);
753 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
754}
755
756static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700757vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500758{
Florin Coras99368312018-08-02 10:45:44 -0700759 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700760 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800761 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700762 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800763 vl_api_registration_t *reg;
Florin Corascea194d2017-10-02 00:18:51 -0700764 clib_error_t *error = 0;
Florin Coras99368312018-08-02 10:45:44 -0700765 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500766
Florin Corasfc804d92018-01-26 01:27:01 -0800767 reg = vl_api_client_index_to_registration (mp->client_index);
768 if (!reg)
769 return;
770
Florin Coras6cf30ad2017-04-04 23:08:23 -0700771 if (session_manager_is_enabled () == 0)
772 {
773 rv = VNET_API_ERROR_FEATURE_DISABLED;
774 goto done;
775 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500776
Florin Corasff6e7692017-12-11 04:59:01 -0800777 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700778 sizeof (mp->options),
779 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500780
Dave Barachb7b92992018-10-17 10:38:51 -0400781 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500782 a->api_client_index = mp->client_index;
783 a->options = mp->options;
Florin Coras52207f12018-07-12 14:48:06 -0700784
785 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
786 a->session_cb_vft = &session_mq_cb_vft;
787 else
788 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500789
Florin Corascea194d2017-10-02 00:18:51 -0700790 if (mp->namespace_id_len > 64)
791 {
792 rv = VNET_API_ERROR_INVALID_VALUE;
793 goto done;
794 }
795
796 if (mp->namespace_id_len)
797 {
Dave Wallace8af20542017-10-26 03:29:30 -0400798 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500799 clib_memcpy_fast (a->namespace_id, mp->namespace_id,
800 mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -0700801 }
802
803 if ((error = vnet_application_attach (a)))
804 {
805 rv = clib_error_get_code (error);
806 clib_error_report (error);
Florin Coras99368312018-08-02 10:45:44 -0700807 vec_free (a->namespace_id);
808 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700809 }
810 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500811
Florin Coras99368312018-08-02 10:45:44 -0700812 /* Send event queues segment */
813 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
814 {
815 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
816 fds[n_fds] = evt_q_segment->fd;
817 n_fds += 1;
818 }
819 /* Send fifo segment fd if needed */
820 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
821 {
822 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
823 fds[n_fds] = a->segment->fd;
824 n_fds += 1;
825 }
826 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
827 {
828 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
829 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
830 n_fds += 1;
831 }
832
Florin Coras6cf30ad2017-04-04 23:08:23 -0700833done:
Florin Corasa5464812017-04-19 13:00:05 -0700834
Dave Barach68b0fb02017-02-28 15:15:56 -0500835 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700836 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500837 if (!rv)
838 {
Florin Corasb384b542018-01-15 01:08:33 -0800839 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800840 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500841 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800842 rmp->segment_size = segp->ssvm_size;
843 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500844 {
Florin Corasb384b542018-01-15 01:08:33 -0800845 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
846 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500847 }
Florin Coras99368312018-08-02 10:45:44 -0700848 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
849 rmp->n_fds = n_fds;
850 rmp->fd_flags = fd_flags;
Florin Corasd85de682018-11-29 17:02:29 -0800851 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500852 }
853 }));
854 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800855
Florin Coras99368312018-08-02 10:45:44 -0700856 if (n_fds)
857 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500858}
859
860static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700861vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
862{
863 vl_api_application_detach_reply_t *rmp;
864 int rv = VNET_API_ERROR_INVALID_VALUE_2;
865 vnet_app_detach_args_t _a, *a = &_a;
866 application_t *app;
867
868 if (session_manager_is_enabled () == 0)
869 {
870 rv = VNET_API_ERROR_FEATURE_DISABLED;
871 goto done;
872 }
873
874 app = application_lookup (mp->client_index);
875 if (app)
876 {
Florin Coras15531972018-08-12 23:50:53 -0700877 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800878 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700879 rv = vnet_application_detach (a);
880 }
881
882done:
883 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
884}
885
886static void
887vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
888{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700889 transport_connection_t *tc = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700890 vnet_bind_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700891 vl_api_bind_uri_reply_t *rmp;
892 stream_session_t *s;
893 application_t *app = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700894 svm_msg_q_t *vpp_evt_q;
Florin Coras60116992018-08-27 09:52:18 -0700895 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700896 int rv;
897
898 if (session_manager_is_enabled () == 0)
899 {
900 rv = VNET_API_ERROR_FEATURE_DISABLED;
901 goto done;
902 }
903
904 app = application_lookup (mp->client_index);
905 if (app)
906 {
Dave Barachb7b92992018-10-17 10:38:51 -0400907 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700908 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700909 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700910 rv = vnet_bind_uri (a);
911 }
912 else
913 {
914 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
915 }
916
917done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700918
919 /* *INDENT-OFF* */
920 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
921 if (!rv)
922 {
923 rmp->handle = a->handle;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700924 if (app && application_has_global_scope (app))
925 {
926 s = listen_session_get_from_handle (a->handle);
927 tc = listen_session_get_transport (s);
Florin Coras6c354942018-04-18 00:05:21 -0700928 rmp->lcl_is_ip4 = tc->is_ip4;
929 rmp->lcl_port = tc->lcl_port;
Dave Barach178cf492018-11-13 16:34:13 -0500930 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700931 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
932 {
933 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
934 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
935 vpp_evt_q = session_manager_get_vpp_event_queue (0);
936 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
937 }
938 }
939 }
940 }));
941 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -0700942
943 /* If app uses mq for control messages, send an mq message as well */
944 if (app && application_use_mq_for_ctrl (app))
945 {
946 app_wrk = application_get_worker (app, 0);
947 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
948 rv);
949 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700950}
951
952static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500953vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
954{
955 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700956 application_t *app;
957 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500958 int rv;
959
Florin Coras6cf30ad2017-04-04 23:08:23 -0700960 if (session_manager_is_enabled () == 0)
961 {
962 rv = VNET_API_ERROR_FEATURE_DISABLED;
963 goto done;
964 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500965
Florin Coras6cf30ad2017-04-04 23:08:23 -0700966 app = application_lookup (mp->client_index);
967 if (app)
968 {
969 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700970 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700971 rv = vnet_unbind_uri (a);
972 }
973 else
974 {
975 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
976 }
977
978done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500979 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
980}
981
Florin Corasf03a59a2017-06-09 21:07:32 -0700982static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500983vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
984{
Dave Wallace33e002b2017-09-06 01:20:02 -0400985 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500986 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700987 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700988 clib_error_t *error = 0;
989 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500990
Florin Coras6cf30ad2017-04-04 23:08:23 -0700991 if (session_manager_is_enabled () == 0)
992 {
993 rv = VNET_API_ERROR_FEATURE_DISABLED;
994 goto done;
995 }
Florin Corase04c2992017-03-01 08:17:34 -0800996
Florin Coras6cf30ad2017-04-04 23:08:23 -0700997 app = application_lookup (mp->client_index);
998 if (app)
999 {
Dave Barachb7b92992018-10-17 10:38:51 -04001000 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -07001001 a->uri = (char *) mp->uri;
1002 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001003 a->app_index = app->app_index;
Florin Corascea194d2017-10-02 00:18:51 -07001004 if ((error = vnet_connect_uri (a)))
1005 {
1006 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -08001007 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -07001008 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001009 }
1010 else
1011 {
1012 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1013 }
Florin Corase04c2992017-03-01 08:17:34 -08001014
Florin Coras3cbc04b2017-10-02 00:18:51 -07001015 /*
1016 * Don't reply to stream (tcp) connects. The reply will come once
1017 * the connection is established. In case of the redirects, the reply
1018 * will come from the server app.
1019 */
Florin Coras8f89dd02018-03-05 16:53:07 -08001020 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001021 return;
1022
Florin Coras6cf30ad2017-04-04 23:08:23 -07001023done:
Florin Corase04c2992017-03-01 08:17:34 -08001024 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -04001025 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -08001026 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001027}
1028
1029static void
1030vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
1031{
1032 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001033 vnet_disconnect_args_t _a, *a = &_a;
1034 application_t *app;
1035 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001036
Florin Coras6cf30ad2017-04-04 23:08:23 -07001037 if (session_manager_is_enabled () == 0)
1038 {
1039 rv = VNET_API_ERROR_FEATURE_DISABLED;
1040 goto done;
1041 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001042
Florin Coras6cf30ad2017-04-04 23:08:23 -07001043 app = application_lookup (mp->client_index);
1044 if (app)
1045 {
1046 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001047 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001048 rv = vnet_disconnect_session (a);
1049 }
1050 else
1051 {
1052 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1053 }
1054
1055done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001056 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001057}
1058
1059static void
1060vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1061 mp)
1062{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001063 vnet_disconnect_args_t _a, *a = &_a;
1064 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001065
1066 /* Client objected to disconnecting the session, log and continue */
1067 if (mp->retval)
1068 {
1069 clib_warning ("client retval %d", mp->retval);
1070 return;
1071 }
1072
1073 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001074 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001075 if (app)
1076 {
1077 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001078 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001079 vnet_disconnect_session (a);
1080 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001081}
1082
1083static void
1084vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
1085{
Florin Coras4af830c2018-12-04 09:21:36 -08001086 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras15531972018-08-12 23:50:53 -07001087 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001088 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001089 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001090 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05001091
Ondrej Fabry6bd197e2018-08-15 08:46:46 +02001092 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001093 if (!app)
1094 return;
1095
Florin Corascea194d2017-10-02 00:18:51 -07001096 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001097 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -07001098 if (!s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001099 {
1100 clib_warning ("Invalid session!");
1101 return;
1102 }
1103
Florin Coras15531972018-08-12 23:50:53 -07001104 app_wrk = app_worker_get (s->app_wrk_index);
1105 if (app_wrk->app_index != app->app_index)
1106 {
1107 clib_warning ("app %u does not own handle 0x%lx", app->app_index,
1108 mp->handle);
1109 return;
1110 }
1111
Dave Barach68b0fb02017-02-28 15:15:56 -05001112 /* Client objected to resetting the session, log and continue */
1113 if (mp->retval)
1114 {
1115 clib_warning ("client retval %d", mp->retval);
1116 return;
1117 }
1118
Dave Barach68b0fb02017-02-28 15:15:56 -05001119 /* This comes as a response to a reset, transport only waiting for
1120 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -08001121 a->handle = mp->handle;
1122 a->app_index = app->app_index;
1123 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -05001124}
1125
1126static void
1127vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
1128{
Florin Corasf8f516a2018-02-08 15:10:09 -08001129 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
1130 local_session_t *ls;
Dave Barach68b0fb02017-02-28 15:15:56 -05001131 stream_session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001132
Florin Corasa5464812017-04-19 13:00:05 -07001133 /* Server isn't interested, kill the session */
1134 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -05001135 {
Florin Corasa5464812017-04-19 13:00:05 -07001136 a->app_index = mp->context;
1137 a->handle = mp->handle;
1138 vnet_disconnect_session (a);
Florin Corasf8f516a2018-02-08 15:10:09 -08001139 return;
1140 }
1141
1142 if (session_handle_is_local (mp->handle))
1143 {
1144 ls = application_get_local_session_from_handle (mp->handle);
Florin Coras15531972018-08-12 23:50:53 -07001145 if (!ls || ls->app_wrk_index != mp->context)
Florin Corasf8f516a2018-02-08 15:10:09 -08001146 {
1147 clib_warning ("server %u doesn't own local handle %llu",
1148 mp->context, mp->handle);
1149 return;
1150 }
1151 if (application_local_session_connect_notify (ls))
1152 return;
1153 ls->session_state = SESSION_STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001154 }
Florin Corasa5464812017-04-19 13:00:05 -07001155 else
1156 {
Florin Corasf8f516a2018-02-08 15:10:09 -08001157 s = session_get_from_handle_if_valid (mp->handle);
Florin Corasa5464812017-04-19 13:00:05 -07001158 if (!s)
1159 {
1160 clib_warning ("session doesn't exist");
1161 return;
1162 }
Florin Coras15531972018-08-12 23:50:53 -07001163 if (s->app_wrk_index != mp->context)
Florin Corasa5464812017-04-19 13:00:05 -07001164 {
1165 clib_warning ("app doesn't own session");
1166 return;
1167 }
Florin Corasa5464812017-04-19 13:00:05 -07001168 s->session_state = SESSION_STATE_READY;
1169 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001170}
1171
1172static void
1173vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1174 * mp)
1175{
1176 clib_warning ("not implemented");
1177}
1178
1179static void
1180vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1181{
1182 vl_api_bind_sock_reply_t *rmp;
1183 vnet_bind_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -07001184 int rv = 0;
1185 clib_error_t *error;
Florin Coraseb2945c2017-11-22 10:39:09 -08001186 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001187 app_worker_t *app_wrk;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001188 stream_session_t *s;
1189 transport_connection_t *tc = 0;
1190 ip46_address_t *ip46;
Florin Coras3c2fed52018-07-04 04:15:05 -07001191 svm_msg_q_t *vpp_evt_q;
Dave Barach68b0fb02017-02-28 15:15:56 -05001192
Florin Coras6cf30ad2017-04-04 23:08:23 -07001193 if (session_manager_is_enabled () == 0)
1194 {
1195 rv = VNET_API_ERROR_FEATURE_DISABLED;
1196 goto done;
1197 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001198
Florin Coras6cf30ad2017-04-04 23:08:23 -07001199 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001200 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001201 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001202 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1203 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001204 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001205
1206 ip46 = (ip46_address_t *) mp->ip;
Dave Barachb7b92992018-10-17 10:38:51 -04001207 clib_memset (a, 0, sizeof (*a));
Florin Corasdcf55ce2017-11-16 15:32:50 -08001208 a->sep.is_ip4 = mp->is_ip4;
1209 a->sep.ip = *ip46;
1210 a->sep.port = mp->port;
1211 a->sep.fib_index = mp->vrf;
1212 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1213 a->sep.transport_proto = mp->proto;
Florin Coras15531972018-08-12 23:50:53 -07001214 a->app_index = app->app_index;
1215 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001216
1217 if ((error = vnet_bind (a)))
1218 {
1219 rv = clib_error_get_code (error);
1220 clib_error_report (error);
1221 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001222
Florin Coras6cf30ad2017-04-04 23:08:23 -07001223done:
Florin Corascea194d2017-10-02 00:18:51 -07001224 /* *INDENT-OFF* */
1225 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1226 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -08001227 {
1228 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -08001229 rmp->lcl_port = mp->port;
Keith Burns (alagalah)60ae80f2018-03-15 10:04:20 -07001230 rmp->lcl_is_ip4 = mp->is_ip4;
Dave Wallace97b1a272017-12-18 13:51:59 -05001231 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -08001232 {
1233 s = listen_session_get_from_handle (a->handle);
1234 tc = listen_session_get_transport (s);
Dave Barach178cf492018-11-13 16:34:13 -05001235 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001236 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1237 {
1238 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
1239 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
1240 vpp_evt_q = session_manager_get_vpp_event_queue (0);
1241 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1242 }
Florin Coraseb2945c2017-11-22 10:39:09 -08001243 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001244 }
Florin Corascea194d2017-10-02 00:18:51 -07001245 }));
1246 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -07001247
1248 /* If app uses mq for control messages, send an mq message as well */
1249 if (app && application_use_mq_for_ctrl (app))
1250 {
1251 app_wrk = application_get_worker (app, mp->wrk_index);
1252 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1253 rv);
1254 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001255}
1256
1257static void
1258vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1259{
1260 vl_api_unbind_sock_reply_t *rmp;
1261 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001262 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -07001263 clib_error_t *error;
1264 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001265
Florin Coras6cf30ad2017-04-04 23:08:23 -07001266 if (session_manager_is_enabled () == 0)
1267 {
1268 rv = VNET_API_ERROR_FEATURE_DISABLED;
1269 goto done;
1270 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001271
Florin Coras6cf30ad2017-04-04 23:08:23 -07001272 app = application_lookup (mp->client_index);
1273 if (app)
1274 {
Florin Coras15531972018-08-12 23:50:53 -07001275 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001276 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001277 a->wrk_map_index = mp->wrk_index;
Florin Corascea194d2017-10-02 00:18:51 -07001278 if ((error = vnet_unbind (a)))
1279 {
1280 rv = clib_error_get_code (error);
1281 clib_error_report (error);
1282 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001283 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001284
Florin Coras6cf30ad2017-04-04 23:08:23 -07001285done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001286 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
1287}
1288
1289static void
1290vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1291{
Dave Wallace33e002b2017-09-06 01:20:02 -04001292 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001293 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001294 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001295 clib_error_t *error = 0;
1296 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001297
Florin Coras6cf30ad2017-04-04 23:08:23 -07001298 if (session_manager_is_enabled () == 0)
1299 {
1300 rv = VNET_API_ERROR_FEATURE_DISABLED;
1301 goto done;
1302 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001303
Florin Coras6cf30ad2017-04-04 23:08:23 -07001304 app = application_lookup (mp->client_index);
1305 if (app)
1306 {
Florin Corase86a8ed2018-01-05 03:20:25 -08001307 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -04001308 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001309
Dave Barachb7b92992018-10-17 10:38:51 -04001310 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001311 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1312 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -07001313 a->sep.is_ip4 = mp->is_ip4;
1314 a->sep.ip = *ip46;
1315 a->sep.port = mp->port;
1316 a->sep.transport_proto = mp->proto;
Florin Coras5665ced2018-10-25 18:03:45 -07001317 a->sep.peer.fib_index = mp->vrf;
1318 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras8f89dd02018-03-05 16:53:07 -08001319 if (mp->hostname_len)
1320 {
Florin Coras15531972018-08-12 23:50:53 -07001321 vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001322 clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
1323 mp->hostname_len);
Florin Coras8f89dd02018-03-05 16:53:07 -08001324 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001325 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001326 a->app_index = app->app_index;
1327 a->wrk_map_index = mp->wrk_index;
Florin Corascea194d2017-10-02 00:18:51 -07001328 if ((error = vnet_connect (a)))
1329 {
1330 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -08001331 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -07001332 }
Florin Coras15531972018-08-12 23:50:53 -07001333 vec_free (a->sep_ext.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001334 }
1335 else
1336 {
1337 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1338 }
Florin Corase04c2992017-03-01 08:17:34 -08001339
Florin Coras8f89dd02018-03-05 16:53:07 -08001340 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001341 return;
1342
1343 /* Got some error, relay it */
1344
Florin Coras6cf30ad2017-04-04 23:08:23 -07001345done:
Dave Wallace33e002b2017-09-06 01:20:02 -04001346 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corasab2f6db2018-08-31 14:31:41 -07001347
1348 if (app && application_use_mq_for_ctrl (app))
1349 {
1350 app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1351 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1352 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001353}
1354
Florin Corascea194d2017-10-02 00:18:51 -07001355static void
Florin Coras15531972018-08-12 23:50:53 -07001356vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1357{
1358 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1359 vl_api_app_worker_add_del_reply_t *rmp;
1360 vl_api_registration_t *reg;
1361 clib_error_t *error = 0;
1362 application_t *app;
1363 u8 fd_flags = 0;
1364
1365 if (!session_manager_is_enabled ())
1366 {
1367 rv = VNET_API_ERROR_FEATURE_DISABLED;
1368 goto done;
1369 }
1370
1371 reg = vl_api_client_index_to_registration (mp->client_index);
1372 if (!reg)
1373 return;
1374
Florin Corasc1f5a432018-11-20 11:31:26 -08001375 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001376 if (!app)
1377 {
1378 rv = VNET_API_ERROR_INVALID_VALUE;
1379 goto done;
1380 }
1381
1382 vnet_app_worker_add_del_args_t args = {
1383 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001384 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001385 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001386 .is_add = mp->is_add
1387 };
1388 error = vnet_app_worker_add_del (&args);
1389 if (error)
1390 {
1391 rv = clib_error_get_code (error);
1392 clib_error_report (error);
1393 goto done;
1394 }
1395
Florin Coras14598772018-09-04 19:47:52 -07001396 if (!mp->is_add)
1397 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001398
Florin Coras15531972018-08-12 23:50:53 -07001399 /* Send fifo segment fd if needed */
1400 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1401 {
1402 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1403 fds[n_fds] = args.segment->fd;
1404 n_fds += 1;
1405 }
1406 if (application_segment_manager_properties (app)->use_mq_eventfd)
1407 {
1408 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1409 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1410 n_fds += 1;
1411 }
1412
1413 /* *INDENT-OFF* */
1414done:
1415 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1416 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001417 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001418 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001419 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001420 {
Florin Coras15531972018-08-12 23:50:53 -07001421 if (vec_len (args.segment->name))
1422 {
1423 memcpy (rmp->segment_name, args.segment->name,
1424 vec_len (args.segment->name));
1425 rmp->segment_name_length = vec_len (args.segment->name);
1426 }
1427 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1428 rmp->n_fds = n_fds;
1429 rmp->fd_flags = fd_flags;
1430 }
1431 }));
1432 /* *INDENT-ON* */
1433
1434 if (n_fds)
1435 session_send_fds (reg, fds, n_fds);
1436}
1437
1438static void
Florin Corascea194d2017-10-02 00:18:51 -07001439vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1440{
1441 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Corascea194d2017-10-02 00:18:51 -07001442 clib_error_t *error = 0;
Florin Coras6e8c6672017-11-10 09:03:54 -08001443 u32 appns_index = 0;
1444 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001445 int rv = 0;
1446 if (!session_manager_is_enabled ())
1447 {
1448 rv = VNET_API_ERROR_FEATURE_DISABLED;
1449 goto done;
1450 }
1451
1452 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1453 {
1454 rv = VNET_API_ERROR_INVALID_VALUE;
1455 goto done;
1456 }
1457
1458 vec_validate (ns_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001459 clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -07001460 vnet_app_namespace_add_del_args_t args = {
1461 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001462 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001463 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1464 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1465 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1466 .is_add = 1
1467 };
1468 error = vnet_app_namespace_add_del (&args);
1469 if (error)
1470 {
1471 rv = clib_error_get_code (error);
1472 clib_error_report (error);
1473 }
Florin Coras6e8c6672017-11-10 09:03:54 -08001474 else
1475 {
1476 appns_index = app_namespace_index_from_id (ns_id);
1477 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1478 {
1479 clib_warning ("app ns lookup failed");
1480 rv = VNET_API_ERROR_UNSPECIFIED;
1481 }
1482 }
Florin Corascea194d2017-10-02 00:18:51 -07001483 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -08001484
1485 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -07001486done:
Florin Coras6e8c6672017-11-10 09:03:54 -08001487 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1488 if (!rv)
1489 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1490 }));
1491 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -07001492}
1493
Florin Coras1c710452017-10-17 00:03:13 -07001494static void
1495vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1496{
1497 vl_api_session_rule_add_del_reply_t *rmp;
1498 session_rule_add_del_args_t args;
1499 session_rule_table_add_del_args_t *table_args = &args.table_args;
1500 clib_error_t *error;
1501 u8 fib_proto;
1502 int rv = 0;
1503
Dave Barachb7b92992018-10-17 10:38:51 -04001504 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001505 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1506
1507 table_args->lcl.fp_len = mp->lcl_plen;
1508 table_args->lcl.fp_proto = fib_proto;
1509 table_args->rmt.fp_len = mp->rmt_plen;
1510 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001511 table_args->lcl_port = mp->lcl_port;
1512 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001513 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1514 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001515 mp->tag[sizeof (mp->tag) - 1] = 0;
1516 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001517 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1518 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -08001519 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -07001520
Dave Barachb7b92992018-10-17 10:38:51 -04001521 clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1522 clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
Florin Coras1c710452017-10-17 00:03:13 -07001523 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1524 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
1525 error = vnet_session_rule_add_del (&args);
1526 if (error)
1527 {
1528 rv = clib_error_get_code (error);
1529 clib_error_report (error);
1530 }
Florin Corasc97a7392017-11-05 23:07:07 -08001531 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001532 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1533}
1534
Florin Coras6c36f532017-11-03 18:32:34 -07001535static void
1536send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001537 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001538 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001539{
1540 vl_api_session_rules_details_t *rmp = 0;
1541 session_mask_or_match_4_t *match =
1542 (session_mask_or_match_4_t *) & rule->match;
1543 session_mask_or_match_4_t *mask =
1544 (session_mask_or_match_4_t *) & rule->mask;
1545
1546 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001547 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001548 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1549 rmp->context = context;
1550
1551 rmp->is_ip4 = 1;
Dave Barach178cf492018-11-13 16:34:13 -05001552 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1553 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001554 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1555 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001556 rmp->lcl_port = match->lcl_port;
1557 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001558 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1559 rmp->scope =
1560 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1561 rmp->transport_proto = transport_proto;
1562 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001563 if (tag)
1564 {
Dave Barach178cf492018-11-13 16:34:13 -05001565 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001566 rmp->tag[vec_len (tag)] = 0;
1567 }
Florin Coras6c36f532017-11-03 18:32:34 -07001568
Florin Coras6c4dae22018-01-09 06:39:23 -08001569 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001570}
1571
1572static void
Florin Corasc97a7392017-11-05 23:07:07 -08001573send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1574 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001575 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001576{
1577 vl_api_session_rules_details_t *rmp = 0;
1578 session_mask_or_match_6_t *match =
1579 (session_mask_or_match_6_t *) & rule->match;
1580 session_mask_or_match_6_t *mask =
1581 (session_mask_or_match_6_t *) & rule->mask;
1582
1583 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001584 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001585 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1586 rmp->context = context;
1587
1588 rmp->is_ip4 = 0;
Dave Barach178cf492018-11-13 16:34:13 -05001589 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1590 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001591 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1592 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001593 rmp->lcl_port = match->lcl_port;
1594 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001595 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001596 rmp->scope =
1597 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -07001598 rmp->transport_proto = transport_proto;
1599 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001600 if (tag)
1601 {
Dave Barach178cf492018-11-13 16:34:13 -05001602 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001603 rmp->tag[vec_len (tag)] = 0;
1604 }
Florin Coras6c36f532017-11-03 18:32:34 -07001605
Florin Coras6c4dae22018-01-09 06:39:23 -08001606 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001607}
1608
1609static void
1610send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001611 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001612 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001613{
1614 mma_rule_16_t *rule16;
1615 mma_rule_40_t *rule40;
1616 mma_rules_table_16_t *srt16;
1617 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001618 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001619
Florin Corasc97a7392017-11-05 23:07:07 -08001620 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001621 {
Florin Corasc97a7392017-11-05 23:07:07 -08001622 u8 *tag = 0;
1623 /* *INDENT-OFF* */
1624 srt16 = &srt->session_rules_tables_16;
1625 pool_foreach (rule16, srt16->rules, ({
1626 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1627 tag = session_rules_table_rule_tag (srt, ri, 1);
1628 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001629 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001630 }));
1631 /* *INDENT-ON* */
1632 }
1633 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1634 {
1635 u8 *tag = 0;
1636 /* *INDENT-OFF* */
1637 srt40 = &srt->session_rules_tables_40;
1638 pool_foreach (rule40, srt40->rules, ({
1639 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1640 tag = session_rules_table_rule_tag (srt, ri, 1);
1641 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001642 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001643 }));
1644 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001645 }
1646}
1647
1648static void
1649vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1650{
Florin Coras6c4dae22018-01-09 06:39:23 -08001651 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001652 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001653 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001654
Florin Coras6c4dae22018-01-09 06:39:23 -08001655 reg = vl_api_client_index_to_registration (mp->client_index);
1656 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001657 return;
1658
1659 /* *INDENT-OFF* */
1660 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001661 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1662 {
1663 send_session_rules_table_details (&st->session_rules[tp],
1664 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001665 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001666 mp->context);
1667 }
Florin Coras6c36f532017-11-03 18:32:34 -07001668 }));
1669 /* *INDENT-ON* */
1670}
1671
Florin Coras371ca502018-02-21 12:07:41 -08001672static void
1673vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1674 mp)
1675{
1676 vl_api_app_namespace_add_del_reply_t *rmp;
1677 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1678 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001679 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001680 u32 cert_len;
1681 int rv = 0;
1682 if (!session_manager_is_enabled ())
1683 {
1684 rv = VNET_API_ERROR_FEATURE_DISABLED;
1685 goto done;
1686 }
Florin Corase3e2f072018-03-04 07:24:30 -08001687 if (!(app = application_lookup (mp->client_index)))
1688 {
1689 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1690 goto done;
1691 }
Dave Barachb7b92992018-10-17 10:38:51 -04001692 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001693 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001694 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001695 if (cert_len > 10000)
1696 {
1697 rv = VNET_API_ERROR_INVALID_VALUE;
1698 goto done;
1699 }
Florin Coras371ca502018-02-21 12:07:41 -08001700 vec_validate (a->cert, cert_len);
Dave Barach178cf492018-11-13 16:34:13 -05001701 clib_memcpy_fast (a->cert, mp->cert, cert_len);
Florin Coras371ca502018-02-21 12:07:41 -08001702 if ((error = vnet_app_add_tls_cert (a)))
1703 {
1704 rv = clib_error_get_code (error);
1705 clib_error_report (error);
1706 }
1707 vec_free (a->cert);
1708done:
1709 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1710}
1711
1712static void
1713vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1714 mp)
1715{
1716 vl_api_app_namespace_add_del_reply_t *rmp;
1717 vnet_app_add_tls_key_args_t _a, *a = &_a;
1718 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001719 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001720 u32 key_len;
1721 int rv = 0;
1722 if (!session_manager_is_enabled ())
1723 {
1724 rv = VNET_API_ERROR_FEATURE_DISABLED;
1725 goto done;
1726 }
Florin Corase3e2f072018-03-04 07:24:30 -08001727 if (!(app = application_lookup (mp->client_index)))
1728 {
1729 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1730 goto done;
1731 }
Dave Barachb7b92992018-10-17 10:38:51 -04001732 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001733 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001734 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001735 if (key_len > 10000)
1736 {
1737 rv = VNET_API_ERROR_INVALID_VALUE;
1738 goto done;
1739 }
Florin Coras371ca502018-02-21 12:07:41 -08001740 vec_validate (a->key, key_len);
Dave Barach178cf492018-11-13 16:34:13 -05001741 clib_memcpy_fast (a->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001742 if ((error = vnet_app_add_tls_key (a)))
1743 {
1744 rv = clib_error_get_code (error);
1745 clib_error_report (error);
1746 }
1747 vec_free (a->key);
1748done:
1749 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1750}
1751
Florin Coras6cf30ad2017-04-04 23:08:23 -07001752static clib_error_t *
1753application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001754{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001755 application_t *app = application_lookup (client_index);
1756 vnet_app_detach_args_t _a, *a = &_a;
1757 if (app)
1758 {
Florin Coras15531972018-08-12 23:50:53 -07001759 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001760 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001761 vnet_application_detach (a);
1762 }
1763 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001764}
1765
Florin Coras6cf30ad2017-04-04 23:08:23 -07001766VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001767
1768#define vl_msg_name_crc_list
1769#include <vnet/vnet_all_api_h.h>
1770#undef vl_msg_name_crc_list
1771
1772static void
1773setup_message_id_table (api_main_t * am)
1774{
1775#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1776 foreach_vl_msg_name_crc_session;
1777#undef _
1778}
1779
1780/*
1781 * session_api_hookup
1782 * Add uri's API message handlers to the table.
1783 * vlib has alread mapped shared memory and
1784 * added the client registration handlers.
1785 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1786 */
1787static clib_error_t *
1788session_api_hookup (vlib_main_t * vm)
1789{
1790 api_main_t *am = &api_main;
1791
1792#define _(N,n) \
1793 vl_msg_api_set_handlers(VL_API_##N, #n, \
1794 vl_api_##n##_t_handler, \
1795 vl_noop_handler, \
1796 vl_api_##n##_t_endian, \
1797 vl_api_##n##_t_print, \
1798 sizeof(vl_api_##n##_t), 1);
1799 foreach_session_api_msg;
1800#undef _
1801
1802 /*
1803 * Messages which bounce off the data-plane to
1804 * an API client. Simply tells the message handling infra not
1805 * to free the message.
1806 *
1807 * Bounced message handlers MUST NOT block the data plane
1808 */
1809 am->message_bounce[VL_API_CONNECT_URI] = 1;
1810 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1811
1812 /*
1813 * Set up the (msg_name, crc, message-id) table
1814 */
1815 setup_message_id_table (am);
1816
1817 return 0;
1818}
1819
1820VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001821
Dave Barach68b0fb02017-02-28 15:15:56 -05001822/*
1823 * fd.io coding-style-patch-verification: ON
1824 *
1825 * Local Variables:
1826 * eval: (c-set-style "gnu")
1827 * End:
1828 */