blob: 40fc41629d410812894657c6b9ca80a374ca6f20 [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 }
Florin Corasdc2e2512018-12-03 17:47:26 -0800412 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700413 return -1;
414}
415
416static int
Florin Coras52207f12018-07-12 14:48:06 -0700417mq_send_session_accepted_cb (stream_session_t * s)
418{
Florin Coras15531972018-08-12 23:50:53 -0700419 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700420 svm_msg_q_msg_t _msg, *msg = &_msg;
421 svm_msg_q_t *vpp_queue, *app_mq;
422 transport_proto_vft_t *tp_vft;
423 transport_connection_t *tc;
424 stream_session_t *listener;
425 session_accepted_msg_t *mp;
426 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700427 application_t *app;
Florin Coras52207f12018-07-12 14:48:06 -0700428
Florin Coras15531972018-08-12 23:50:53 -0700429 app = application_get (app_wrk->app_index);
430 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700431 if (mq_try_lock_and_alloc_msg (app_mq, msg))
432 return -1;
Florin Coras52207f12018-07-12 14:48:06 -0700433
434 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400435 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700436 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
437 mp = (session_accepted_msg_t *) evt->data;
Florin Corasab2f6db2018-08-31 14:31:41 -0700438 mp->context = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700439 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
440 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800441 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700442
443 if (session_has_transport (s))
444 {
445 listener = listen_session_get (s->listener_index);
446 mp->listener_handle = listen_session_get_handle (listener);
447 if (application_is_proxy (app))
448 {
449 listener =
Florin Coras15531972018-08-12 23:50:53 -0700450 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
451 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700452 if (listener)
453 mp->listener_handle = listen_session_get_handle (listener);
454 }
455 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
456 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
457 mp->handle = session_handle (s);
458 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
459 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
460 mp->port = tc->rmt_port;
461 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500462 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700463 }
464 else
465 {
466 local_session_t *ls = (local_session_t *) s;
467 local_session_t *ll;
Florin Coras99368312018-08-02 10:45:44 -0700468 u8 main_thread = vlib_num_workers ()? 1 : 0;
469
Florin Corasc1f5a432018-11-20 11:31:26 -0800470 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700471 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700472 ls->server_evt_q,
473 ls->client_evt_q);
474
Florin Coras52207f12018-07-12 14:48:06 -0700475 if (application_local_session_listener_has_transport (ls))
476 {
477 listener = listen_session_get (ls->listener_index);
478 mp->listener_handle = listen_session_get_handle (listener);
479 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
480 }
481 else
482 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700483 ll = application_get_local_listen_session (app, ls->listener_index);
Florin Coras52207f12018-07-12 14:48:06 -0700484 if (ll->transport_listener_index != ~0)
485 {
486 listener = listen_session_get (ll->transport_listener_index);
487 mp->listener_handle = listen_session_get_handle (listener);
488 }
489 else
490 {
491 mp->listener_handle = application_local_session_handle (ll);
492 }
493 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
494 }
495 mp->handle = application_local_session_handle (ls);
496 mp->port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700497 vpp_queue = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700498 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
499 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700500 mp->server_event_queue_address = ls->server_evt_q;
501 }
Florin Coras537b17e2018-09-28 10:35:45 -0700502 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700503
504 return 0;
505}
506
507static void
508mq_send_session_disconnected_cb (stream_session_t * s)
509{
Florin Coras15531972018-08-12 23:50:53 -0700510 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700511 svm_msg_q_msg_t _msg, *msg = &_msg;
512 session_disconnected_msg_t *mp;
513 svm_msg_q_t *app_mq;
514 session_event_t *evt;
515
Florin Coras15531972018-08-12 23:50:53 -0700516 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700517 if (mq_try_lock_and_alloc_msg (app_mq, msg))
518 return;
Florin Coras52207f12018-07-12 14:48:06 -0700519 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400520 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700521 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
522 mp = (session_disconnected_msg_t *) evt->data;
523 mp->handle = session_handle (s);
Florin Corasc1f5a432018-11-20 11:31:26 -0800524 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700525 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700526}
527
Florin Corasaa27eb92018-10-13 12:20:01 -0700528void
529mq_send_local_session_disconnected_cb (u32 app_wrk_index,
530 local_session_t * ls)
531{
532 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
533 svm_msg_q_msg_t _msg, *msg = &_msg;
534 session_disconnected_msg_t *mp;
535 svm_msg_q_t *app_mq;
536 session_event_t *evt;
Florin Corasaa27eb92018-10-13 12:20:01 -0700537
Florin Corasaa27eb92018-10-13 12:20:01 -0700538 app_mq = app_wrk->event_queue;
539 if (mq_try_lock_and_alloc_msg (app_mq, msg))
540 return;
541 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400542 clib_memset (evt, 0, sizeof (*evt));
Florin Corasaa27eb92018-10-13 12:20:01 -0700543 evt->event_type = SESSION_CTRL_EVT_DISCONNECTED;
544 mp = (session_disconnected_msg_t *) evt->data;
545 mp->handle = application_local_session_handle (ls);
Florin Corasc1f5a432018-11-20 11:31:26 -0800546 mp->context = app_wrk->api_client_index;
Florin Corasaa27eb92018-10-13 12:20:01 -0700547 svm_msg_q_add_and_unlock (app_mq, msg);
548}
549
Florin Coras52207f12018-07-12 14:48:06 -0700550static void
551mq_send_session_reset_cb (stream_session_t * s)
552{
Florin Coras15531972018-08-12 23:50:53 -0700553 app_worker_t *app = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700554 svm_msg_q_msg_t _msg, *msg = &_msg;
555 session_reset_msg_t *mp;
556 svm_msg_q_t *app_mq;
557 session_event_t *evt;
558
559 app_mq = app->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700560 if (mq_try_lock_and_alloc_msg (app_mq, msg))
561 return;
Florin Coras52207f12018-07-12 14:48:06 -0700562 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400563 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700564 evt->event_type = SESSION_CTRL_EVT_RESET;
565 mp = (session_reset_msg_t *) evt->data;
566 mp->handle = session_handle (s);
Florin Coras537b17e2018-09-28 10:35:45 -0700567 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700568}
569
570static int
Florin Coras15531972018-08-12 23:50:53 -0700571mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras52207f12018-07-12 14:48:06 -0700572 stream_session_t * s, u8 is_fail)
573{
574 svm_msg_q_msg_t _msg, *msg = &_msg;
575 session_connected_msg_t *mp;
576 svm_msg_q_t *vpp_mq, *app_mq;
577 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700578 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700579 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700580
Florin Coras15531972018-08-12 23:50:53 -0700581 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700582 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700583 if (!app_mq)
584 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800585 clib_warning ("app %u with api index: %u not attached",
586 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700587 return -1;
588 }
589
Florin Coras83ea6692018-10-12 16:55:14 -0700590 if (mq_try_lock_and_alloc_msg (app_mq, msg))
591 return -1;
Florin Corasdc2e2512018-12-03 17:47:26 -0800592
Florin Coras52207f12018-07-12 14:48:06 -0700593 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400594 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700595 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
596 mp = (session_connected_msg_t *) evt->data;
597 mp->context = api_context;
598
599 if (is_fail)
600 goto done;
601
Florin Corasdc2e2512018-12-03 17:47:26 -0800602 mp->segment_handle = session_segment_handle (s);
603
Florin Coras52207f12018-07-12 14:48:06 -0700604 if (session_has_transport (s))
605 {
606 tc = session_get_transport (s);
607 if (!tc)
608 {
609 is_fail = 1;
610 goto done;
611 }
612
613 vpp_mq = session_manager_get_vpp_event_queue (s->thread_index);
614 mp->handle = session_handle (s);
615 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Dave Barach178cf492018-11-13 16:34:13 -0500616 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700617 mp->is_ip4 = tc->is_ip4;
618 mp->lcl_port = tc->lcl_port;
619 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
620 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
621 }
622 else
623 {
624 local_session_t *ls = (local_session_t *) s;
Florin Coras99368312018-08-02 10:45:44 -0700625 u8 main_thread = vlib_num_workers ()? 1 : 0;
626
Florin Corasc1f5a432018-11-20 11:31:26 -0800627 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700628 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700629 ls->client_evt_q,
630 ls->server_evt_q);
631
Florin Coras52207f12018-07-12 14:48:06 -0700632 mp->handle = application_local_session_handle (ls);
633 mp->lcl_port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700634 vpp_mq = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700635 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras52207f12018-07-12 14:48:06 -0700636 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700637 mp->server_event_queue_address = ls->server_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700638 mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
639 mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
640 }
641
642done:
643 mp->retval = is_fail ?
644 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
645
Florin Coras537b17e2018-09-28 10:35:45 -0700646 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700647 return 0;
648}
649
Florin Coras60116992018-08-27 09:52:18 -0700650static int
651mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
652 session_handle_t handle, int rv)
653{
654 svm_msg_q_msg_t _msg, *msg = &_msg;
655 svm_msg_q_t *app_mq, *vpp_evt_q;
656 transport_connection_t *tc;
657 stream_session_t *ls = 0;
658 session_bound_msg_t *mp;
659 app_worker_t *app_wrk;
660 session_event_t *evt;
661 application_t *app;
662
663 app_wrk = app_worker_get (app_wrk_index);
664 app = application_get (app_wrk->app_index);
665 app_mq = app_wrk->event_queue;
666 if (!app_mq)
667 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800668 clib_warning ("app %u with api index: %u not attached",
669 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700670 return -1;
671 }
672
Florin Coras83ea6692018-10-12 16:55:14 -0700673 if (mq_try_lock_and_alloc_msg (app_mq, msg))
674 return -1;
675
Florin Coras60116992018-08-27 09:52:18 -0700676 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400677 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700678 evt->event_type = SESSION_CTRL_EVT_BOUND;
679 mp = (session_bound_msg_t *) evt->data;
680 mp->context = api_context;
681
682 if (rv)
683 goto done;
684
685 mp->handle = handle;
686 if (application_has_global_scope (app))
687 {
688 ls = listen_session_get_from_handle (handle);
689 tc = listen_session_get_transport (ls);
690 mp->lcl_port = tc->lcl_port;
691 mp->lcl_is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500692 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras60116992018-08-27 09:52:18 -0700693 }
694 else
695 {
696 local_session_t *local;
Florin Corasab2f6db2018-08-31 14:31:41 -0700697 local = application_get_local_listener_w_handle (handle);
Florin Coras60116992018-08-27 09:52:18 -0700698 mp->lcl_port = local->port;
699 mp->lcl_is_ip4 = session_type_is_ip4 (local->session_type);
700 }
701
702 if (ls && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
703 {
704 mp->rx_fifo = pointer_to_uword (ls->server_rx_fifo);
705 mp->tx_fifo = pointer_to_uword (ls->server_tx_fifo);
706 vpp_evt_q = session_manager_get_vpp_event_queue (0);
707 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
708 }
709
710done:
711 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700712 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700713 return 0;
714}
715
Florin Coras52207f12018-07-12 14:48:06 -0700716static session_cb_vft_t session_mq_cb_vft = {
717 .session_accept_callback = mq_send_session_accepted_cb,
718 .session_disconnect_callback = mq_send_session_disconnected_cb,
719 .session_connected_callback = mq_send_session_connected_cb,
720 .session_reset_callback = mq_send_session_reset_cb,
721 .add_segment_callback = send_add_segment_callback,
722 .del_segment_callback = send_del_segment_callback,
723};
724
Dave Barach68b0fb02017-02-28 15:15:56 -0500725static void
Florin Corase04c2992017-03-01 08:17:34 -0800726vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
727{
728 vl_api_session_enable_disable_reply_t *rmp;
729 vlib_main_t *vm = vlib_get_main ();
730 int rv = 0;
731
732 vnet_session_enable_disable (vm, mp->is_enable);
733 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
734}
735
736static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700737vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500738{
Florin Coras99368312018-08-02 10:45:44 -0700739 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700740 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800741 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700742 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800743 vl_api_registration_t *reg;
Florin Corascea194d2017-10-02 00:18:51 -0700744 clib_error_t *error = 0;
Florin Coras99368312018-08-02 10:45:44 -0700745 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500746
Florin Corasfc804d92018-01-26 01:27:01 -0800747 reg = vl_api_client_index_to_registration (mp->client_index);
748 if (!reg)
749 return;
750
Florin Coras6cf30ad2017-04-04 23:08:23 -0700751 if (session_manager_is_enabled () == 0)
752 {
753 rv = VNET_API_ERROR_FEATURE_DISABLED;
754 goto done;
755 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500756
Florin Corasff6e7692017-12-11 04:59:01 -0800757 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700758 sizeof (mp->options),
759 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500760
Dave Barachb7b92992018-10-17 10:38:51 -0400761 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500762 a->api_client_index = mp->client_index;
763 a->options = mp->options;
Florin Coras52207f12018-07-12 14:48:06 -0700764
765 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
766 a->session_cb_vft = &session_mq_cb_vft;
767 else
768 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500769
Florin Corascea194d2017-10-02 00:18:51 -0700770 if (mp->namespace_id_len > 64)
771 {
772 rv = VNET_API_ERROR_INVALID_VALUE;
773 goto done;
774 }
775
776 if (mp->namespace_id_len)
777 {
Dave Wallace8af20542017-10-26 03:29:30 -0400778 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500779 clib_memcpy_fast (a->namespace_id, mp->namespace_id,
780 mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -0700781 }
782
783 if ((error = vnet_application_attach (a)))
784 {
785 rv = clib_error_get_code (error);
786 clib_error_report (error);
Florin Coras99368312018-08-02 10:45:44 -0700787 vec_free (a->namespace_id);
788 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700789 }
790 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500791
Florin Coras99368312018-08-02 10:45:44 -0700792 /* Send event queues segment */
793 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
794 {
795 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
796 fds[n_fds] = evt_q_segment->fd;
797 n_fds += 1;
798 }
799 /* Send fifo segment fd if needed */
800 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
801 {
802 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
803 fds[n_fds] = a->segment->fd;
804 n_fds += 1;
805 }
806 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
807 {
808 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
809 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
810 n_fds += 1;
811 }
812
Florin Coras6cf30ad2017-04-04 23:08:23 -0700813done:
Florin Corasa5464812017-04-19 13:00:05 -0700814
Dave Barach68b0fb02017-02-28 15:15:56 -0500815 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700816 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500817 if (!rv)
818 {
Florin Corasb384b542018-01-15 01:08:33 -0800819 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800820 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500821 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800822 rmp->segment_size = segp->ssvm_size;
823 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500824 {
Florin Corasb384b542018-01-15 01:08:33 -0800825 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
826 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500827 }
Florin Coras99368312018-08-02 10:45:44 -0700828 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
829 rmp->n_fds = n_fds;
830 rmp->fd_flags = fd_flags;
Florin Corasd85de682018-11-29 17:02:29 -0800831 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500832 }
833 }));
834 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800835
Florin Coras99368312018-08-02 10:45:44 -0700836 if (n_fds)
837 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500838}
839
840static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700841vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
842{
843 vl_api_application_detach_reply_t *rmp;
844 int rv = VNET_API_ERROR_INVALID_VALUE_2;
845 vnet_app_detach_args_t _a, *a = &_a;
846 application_t *app;
847
848 if (session_manager_is_enabled () == 0)
849 {
850 rv = VNET_API_ERROR_FEATURE_DISABLED;
851 goto done;
852 }
853
854 app = application_lookup (mp->client_index);
855 if (app)
856 {
Florin Coras15531972018-08-12 23:50:53 -0700857 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800858 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700859 rv = vnet_application_detach (a);
860 }
861
862done:
863 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
864}
865
866static void
867vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
868{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700869 transport_connection_t *tc = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700870 vnet_bind_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700871 vl_api_bind_uri_reply_t *rmp;
872 stream_session_t *s;
873 application_t *app = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700874 svm_msg_q_t *vpp_evt_q;
Florin Coras60116992018-08-27 09:52:18 -0700875 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700876 int rv;
877
878 if (session_manager_is_enabled () == 0)
879 {
880 rv = VNET_API_ERROR_FEATURE_DISABLED;
881 goto done;
882 }
883
884 app = application_lookup (mp->client_index);
885 if (app)
886 {
Dave Barachb7b92992018-10-17 10:38:51 -0400887 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700888 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700889 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700890 rv = vnet_bind_uri (a);
891 }
892 else
893 {
894 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
895 }
896
897done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700898
899 /* *INDENT-OFF* */
900 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
901 if (!rv)
902 {
903 rmp->handle = a->handle;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700904 if (app && application_has_global_scope (app))
905 {
906 s = listen_session_get_from_handle (a->handle);
907 tc = listen_session_get_transport (s);
Florin Coras6c354942018-04-18 00:05:21 -0700908 rmp->lcl_is_ip4 = tc->is_ip4;
909 rmp->lcl_port = tc->lcl_port;
Dave Barach178cf492018-11-13 16:34:13 -0500910 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700911 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
912 {
913 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
914 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
915 vpp_evt_q = session_manager_get_vpp_event_queue (0);
916 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
917 }
918 }
919 }
920 }));
921 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -0700922
923 /* If app uses mq for control messages, send an mq message as well */
924 if (app && application_use_mq_for_ctrl (app))
925 {
926 app_wrk = application_get_worker (app, 0);
927 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
928 rv);
929 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700930}
931
932static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500933vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
934{
935 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700936 application_t *app;
937 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500938 int rv;
939
Florin Coras6cf30ad2017-04-04 23:08:23 -0700940 if (session_manager_is_enabled () == 0)
941 {
942 rv = VNET_API_ERROR_FEATURE_DISABLED;
943 goto done;
944 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500945
Florin Coras6cf30ad2017-04-04 23:08:23 -0700946 app = application_lookup (mp->client_index);
947 if (app)
948 {
949 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700950 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700951 rv = vnet_unbind_uri (a);
952 }
953 else
954 {
955 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
956 }
957
958done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500959 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
960}
961
Florin Corasf03a59a2017-06-09 21:07:32 -0700962static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500963vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
964{
Dave Wallace33e002b2017-09-06 01:20:02 -0400965 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500966 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700967 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700968 clib_error_t *error = 0;
969 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500970
Florin Coras6cf30ad2017-04-04 23:08:23 -0700971 if (session_manager_is_enabled () == 0)
972 {
973 rv = VNET_API_ERROR_FEATURE_DISABLED;
974 goto done;
975 }
Florin Corase04c2992017-03-01 08:17:34 -0800976
Florin Coras6cf30ad2017-04-04 23:08:23 -0700977 app = application_lookup (mp->client_index);
978 if (app)
979 {
Dave Barachb7b92992018-10-17 10:38:51 -0400980 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700981 a->uri = (char *) mp->uri;
982 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -0700983 a->app_index = app->app_index;
Florin Corascea194d2017-10-02 00:18:51 -0700984 if ((error = vnet_connect_uri (a)))
985 {
986 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -0800987 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -0700988 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700989 }
990 else
991 {
992 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
993 }
Florin Corase04c2992017-03-01 08:17:34 -0800994
Florin Coras3cbc04b2017-10-02 00:18:51 -0700995 /*
996 * Don't reply to stream (tcp) connects. The reply will come once
997 * the connection is established. In case of the redirects, the reply
998 * will come from the server app.
999 */
Florin Coras8f89dd02018-03-05 16:53:07 -08001000 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001001 return;
1002
Florin Coras6cf30ad2017-04-04 23:08:23 -07001003done:
Florin Corase04c2992017-03-01 08:17:34 -08001004 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -04001005 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -08001006 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001007}
1008
1009static void
1010vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
1011{
1012 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001013 vnet_disconnect_args_t _a, *a = &_a;
1014 application_t *app;
1015 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001016
Florin Coras6cf30ad2017-04-04 23:08:23 -07001017 if (session_manager_is_enabled () == 0)
1018 {
1019 rv = VNET_API_ERROR_FEATURE_DISABLED;
1020 goto done;
1021 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001022
Florin Coras6cf30ad2017-04-04 23:08:23 -07001023 app = application_lookup (mp->client_index);
1024 if (app)
1025 {
1026 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001027 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001028 rv = vnet_disconnect_session (a);
1029 }
1030 else
1031 {
1032 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1033 }
1034
1035done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001036 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001037}
1038
1039static void
1040vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1041 mp)
1042{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001043 vnet_disconnect_args_t _a, *a = &_a;
1044 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001045
1046 /* Client objected to disconnecting the session, log and continue */
1047 if (mp->retval)
1048 {
1049 clib_warning ("client retval %d", mp->retval);
1050 return;
1051 }
1052
1053 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001054 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001055 if (app)
1056 {
1057 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001058 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001059 vnet_disconnect_session (a);
1060 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001061}
1062
1063static void
1064vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
1065{
Florin Coras4af830c2018-12-04 09:21:36 -08001066 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras15531972018-08-12 23:50:53 -07001067 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001068 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001069 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001070 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05001071
Ondrej Fabry6bd197e2018-08-15 08:46:46 +02001072 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001073 if (!app)
1074 return;
1075
Florin Corascea194d2017-10-02 00:18:51 -07001076 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001077 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -07001078 if (!s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001079 {
1080 clib_warning ("Invalid session!");
1081 return;
1082 }
1083
Florin Coras15531972018-08-12 23:50:53 -07001084 app_wrk = app_worker_get (s->app_wrk_index);
1085 if (app_wrk->app_index != app->app_index)
1086 {
1087 clib_warning ("app %u does not own handle 0x%lx", app->app_index,
1088 mp->handle);
1089 return;
1090 }
1091
Dave Barach68b0fb02017-02-28 15:15:56 -05001092 /* Client objected to resetting the session, log and continue */
1093 if (mp->retval)
1094 {
1095 clib_warning ("client retval %d", mp->retval);
1096 return;
1097 }
1098
Dave Barach68b0fb02017-02-28 15:15:56 -05001099 /* This comes as a response to a reset, transport only waiting for
1100 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -08001101 a->handle = mp->handle;
1102 a->app_index = app->app_index;
1103 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -05001104}
1105
1106static void
1107vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
1108{
Florin Corasf8f516a2018-02-08 15:10:09 -08001109 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
1110 local_session_t *ls;
Dave Barach68b0fb02017-02-28 15:15:56 -05001111 stream_session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001112
Florin Corasa5464812017-04-19 13:00:05 -07001113 /* Server isn't interested, kill the session */
1114 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -05001115 {
Florin Corasa5464812017-04-19 13:00:05 -07001116 a->app_index = mp->context;
1117 a->handle = mp->handle;
1118 vnet_disconnect_session (a);
Florin Corasf8f516a2018-02-08 15:10:09 -08001119 return;
1120 }
1121
1122 if (session_handle_is_local (mp->handle))
1123 {
1124 ls = application_get_local_session_from_handle (mp->handle);
Florin Coras15531972018-08-12 23:50:53 -07001125 if (!ls || ls->app_wrk_index != mp->context)
Florin Corasf8f516a2018-02-08 15:10:09 -08001126 {
1127 clib_warning ("server %u doesn't own local handle %llu",
1128 mp->context, mp->handle);
1129 return;
1130 }
1131 if (application_local_session_connect_notify (ls))
1132 return;
1133 ls->session_state = SESSION_STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001134 }
Florin Corasa5464812017-04-19 13:00:05 -07001135 else
1136 {
Florin Corasf8f516a2018-02-08 15:10:09 -08001137 s = session_get_from_handle_if_valid (mp->handle);
Florin Corasa5464812017-04-19 13:00:05 -07001138 if (!s)
1139 {
1140 clib_warning ("session doesn't exist");
1141 return;
1142 }
Florin Coras15531972018-08-12 23:50:53 -07001143 if (s->app_wrk_index != mp->context)
Florin Corasa5464812017-04-19 13:00:05 -07001144 {
1145 clib_warning ("app doesn't own session");
1146 return;
1147 }
Florin Corasa5464812017-04-19 13:00:05 -07001148 s->session_state = SESSION_STATE_READY;
1149 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001150}
1151
1152static void
1153vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1154 * mp)
1155{
1156 clib_warning ("not implemented");
1157}
1158
1159static void
1160vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1161{
1162 vl_api_bind_sock_reply_t *rmp;
1163 vnet_bind_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -07001164 int rv = 0;
1165 clib_error_t *error;
Florin Coraseb2945c2017-11-22 10:39:09 -08001166 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001167 app_worker_t *app_wrk;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001168 stream_session_t *s;
1169 transport_connection_t *tc = 0;
1170 ip46_address_t *ip46;
Florin Coras3c2fed52018-07-04 04:15:05 -07001171 svm_msg_q_t *vpp_evt_q;
Dave Barach68b0fb02017-02-28 15:15:56 -05001172
Florin Coras6cf30ad2017-04-04 23:08:23 -07001173 if (session_manager_is_enabled () == 0)
1174 {
1175 rv = VNET_API_ERROR_FEATURE_DISABLED;
1176 goto done;
1177 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001178
Florin Coras6cf30ad2017-04-04 23:08:23 -07001179 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001180 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001181 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001182 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1183 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001184 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001185
1186 ip46 = (ip46_address_t *) mp->ip;
Dave Barachb7b92992018-10-17 10:38:51 -04001187 clib_memset (a, 0, sizeof (*a));
Florin Corasdcf55ce2017-11-16 15:32:50 -08001188 a->sep.is_ip4 = mp->is_ip4;
1189 a->sep.ip = *ip46;
1190 a->sep.port = mp->port;
1191 a->sep.fib_index = mp->vrf;
1192 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1193 a->sep.transport_proto = mp->proto;
Florin Coras15531972018-08-12 23:50:53 -07001194 a->app_index = app->app_index;
1195 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001196
1197 if ((error = vnet_bind (a)))
1198 {
1199 rv = clib_error_get_code (error);
1200 clib_error_report (error);
1201 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001202
Florin Coras6cf30ad2017-04-04 23:08:23 -07001203done:
Florin Corascea194d2017-10-02 00:18:51 -07001204 /* *INDENT-OFF* */
1205 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1206 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -08001207 {
1208 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -08001209 rmp->lcl_port = mp->port;
Keith Burns (alagalah)60ae80f2018-03-15 10:04:20 -07001210 rmp->lcl_is_ip4 = mp->is_ip4;
Dave Wallace97b1a272017-12-18 13:51:59 -05001211 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -08001212 {
1213 s = listen_session_get_from_handle (a->handle);
1214 tc = listen_session_get_transport (s);
Dave Barach178cf492018-11-13 16:34:13 -05001215 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001216 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1217 {
1218 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
1219 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
1220 vpp_evt_q = session_manager_get_vpp_event_queue (0);
1221 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1222 }
Florin Coraseb2945c2017-11-22 10:39:09 -08001223 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001224 }
Florin Corascea194d2017-10-02 00:18:51 -07001225 }));
1226 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -07001227
1228 /* If app uses mq for control messages, send an mq message as well */
1229 if (app && application_use_mq_for_ctrl (app))
1230 {
1231 app_wrk = application_get_worker (app, mp->wrk_index);
1232 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1233 rv);
1234 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001235}
1236
1237static void
1238vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1239{
1240 vl_api_unbind_sock_reply_t *rmp;
1241 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001242 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -07001243 clib_error_t *error;
1244 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001245
Florin Coras6cf30ad2017-04-04 23:08:23 -07001246 if (session_manager_is_enabled () == 0)
1247 {
1248 rv = VNET_API_ERROR_FEATURE_DISABLED;
1249 goto done;
1250 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001251
Florin Coras6cf30ad2017-04-04 23:08:23 -07001252 app = application_lookup (mp->client_index);
1253 if (app)
1254 {
Florin Coras15531972018-08-12 23:50:53 -07001255 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001256 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001257 a->wrk_map_index = mp->wrk_index;
Florin Corascea194d2017-10-02 00:18:51 -07001258 if ((error = vnet_unbind (a)))
1259 {
1260 rv = clib_error_get_code (error);
1261 clib_error_report (error);
1262 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001263 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001264
Florin Coras6cf30ad2017-04-04 23:08:23 -07001265done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001266 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
1267}
1268
1269static void
1270vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1271{
Dave Wallace33e002b2017-09-06 01:20:02 -04001272 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001273 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001274 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001275 clib_error_t *error = 0;
1276 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001277
Florin Coras6cf30ad2017-04-04 23:08:23 -07001278 if (session_manager_is_enabled () == 0)
1279 {
1280 rv = VNET_API_ERROR_FEATURE_DISABLED;
1281 goto done;
1282 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001283
Florin Coras6cf30ad2017-04-04 23:08:23 -07001284 app = application_lookup (mp->client_index);
1285 if (app)
1286 {
Florin Corase86a8ed2018-01-05 03:20:25 -08001287 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -04001288 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001289
Dave Barachb7b92992018-10-17 10:38:51 -04001290 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001291 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1292 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -07001293 a->sep.is_ip4 = mp->is_ip4;
1294 a->sep.ip = *ip46;
1295 a->sep.port = mp->port;
1296 a->sep.transport_proto = mp->proto;
Florin Coras5665ced2018-10-25 18:03:45 -07001297 a->sep.peer.fib_index = mp->vrf;
1298 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras8f89dd02018-03-05 16:53:07 -08001299 if (mp->hostname_len)
1300 {
Florin Coras15531972018-08-12 23:50:53 -07001301 vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001302 clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
1303 mp->hostname_len);
Florin Coras8f89dd02018-03-05 16:53:07 -08001304 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001305 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001306 a->app_index = app->app_index;
1307 a->wrk_map_index = mp->wrk_index;
Florin Corascea194d2017-10-02 00:18:51 -07001308 if ((error = vnet_connect (a)))
1309 {
1310 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -08001311 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -07001312 }
Florin Coras15531972018-08-12 23:50:53 -07001313 vec_free (a->sep_ext.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001314 }
1315 else
1316 {
1317 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1318 }
Florin Corase04c2992017-03-01 08:17:34 -08001319
Florin Coras8f89dd02018-03-05 16:53:07 -08001320 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001321 return;
1322
1323 /* Got some error, relay it */
1324
Florin Coras6cf30ad2017-04-04 23:08:23 -07001325done:
Dave Wallace33e002b2017-09-06 01:20:02 -04001326 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corasab2f6db2018-08-31 14:31:41 -07001327
1328 if (app && application_use_mq_for_ctrl (app))
1329 {
1330 app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1331 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1332 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001333}
1334
Florin Corascea194d2017-10-02 00:18:51 -07001335static void
Florin Coras15531972018-08-12 23:50:53 -07001336vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1337{
1338 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1339 vl_api_app_worker_add_del_reply_t *rmp;
1340 vl_api_registration_t *reg;
1341 clib_error_t *error = 0;
1342 application_t *app;
1343 u8 fd_flags = 0;
1344
1345 if (!session_manager_is_enabled ())
1346 {
1347 rv = VNET_API_ERROR_FEATURE_DISABLED;
1348 goto done;
1349 }
1350
1351 reg = vl_api_client_index_to_registration (mp->client_index);
1352 if (!reg)
1353 return;
1354
Florin Corasc1f5a432018-11-20 11:31:26 -08001355 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001356 if (!app)
1357 {
1358 rv = VNET_API_ERROR_INVALID_VALUE;
1359 goto done;
1360 }
1361
1362 vnet_app_worker_add_del_args_t args = {
1363 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001364 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001365 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001366 .is_add = mp->is_add
1367 };
1368 error = vnet_app_worker_add_del (&args);
1369 if (error)
1370 {
1371 rv = clib_error_get_code (error);
1372 clib_error_report (error);
1373 goto done;
1374 }
1375
Florin Coras14598772018-09-04 19:47:52 -07001376 if (!mp->is_add)
1377 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001378
Florin Coras15531972018-08-12 23:50:53 -07001379 /* Send fifo segment fd if needed */
1380 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1381 {
1382 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1383 fds[n_fds] = args.segment->fd;
1384 n_fds += 1;
1385 }
1386 if (application_segment_manager_properties (app)->use_mq_eventfd)
1387 {
1388 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1389 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1390 n_fds += 1;
1391 }
1392
1393 /* *INDENT-OFF* */
1394done:
1395 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1396 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001397 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001398 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001399 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001400 {
Florin Coras15531972018-08-12 23:50:53 -07001401 if (vec_len (args.segment->name))
1402 {
1403 memcpy (rmp->segment_name, args.segment->name,
1404 vec_len (args.segment->name));
1405 rmp->segment_name_length = vec_len (args.segment->name);
1406 }
1407 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1408 rmp->n_fds = n_fds;
1409 rmp->fd_flags = fd_flags;
1410 }
1411 }));
1412 /* *INDENT-ON* */
1413
1414 if (n_fds)
1415 session_send_fds (reg, fds, n_fds);
1416}
1417
1418static void
Florin Corascea194d2017-10-02 00:18:51 -07001419vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1420{
1421 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Corascea194d2017-10-02 00:18:51 -07001422 clib_error_t *error = 0;
Florin Coras6e8c6672017-11-10 09:03:54 -08001423 u32 appns_index = 0;
1424 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001425 int rv = 0;
1426 if (!session_manager_is_enabled ())
1427 {
1428 rv = VNET_API_ERROR_FEATURE_DISABLED;
1429 goto done;
1430 }
1431
1432 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1433 {
1434 rv = VNET_API_ERROR_INVALID_VALUE;
1435 goto done;
1436 }
1437
1438 vec_validate (ns_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001439 clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -07001440 vnet_app_namespace_add_del_args_t args = {
1441 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001442 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001443 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1444 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1445 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1446 .is_add = 1
1447 };
1448 error = vnet_app_namespace_add_del (&args);
1449 if (error)
1450 {
1451 rv = clib_error_get_code (error);
1452 clib_error_report (error);
1453 }
Florin Coras6e8c6672017-11-10 09:03:54 -08001454 else
1455 {
1456 appns_index = app_namespace_index_from_id (ns_id);
1457 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1458 {
1459 clib_warning ("app ns lookup failed");
1460 rv = VNET_API_ERROR_UNSPECIFIED;
1461 }
1462 }
Florin Corascea194d2017-10-02 00:18:51 -07001463 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -08001464
1465 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -07001466done:
Florin Coras6e8c6672017-11-10 09:03:54 -08001467 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1468 if (!rv)
1469 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1470 }));
1471 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -07001472}
1473
Florin Coras1c710452017-10-17 00:03:13 -07001474static void
1475vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1476{
1477 vl_api_session_rule_add_del_reply_t *rmp;
1478 session_rule_add_del_args_t args;
1479 session_rule_table_add_del_args_t *table_args = &args.table_args;
1480 clib_error_t *error;
1481 u8 fib_proto;
1482 int rv = 0;
1483
Dave Barachb7b92992018-10-17 10:38:51 -04001484 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001485 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1486
1487 table_args->lcl.fp_len = mp->lcl_plen;
1488 table_args->lcl.fp_proto = fib_proto;
1489 table_args->rmt.fp_len = mp->rmt_plen;
1490 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001491 table_args->lcl_port = mp->lcl_port;
1492 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001493 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1494 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001495 mp->tag[sizeof (mp->tag) - 1] = 0;
1496 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001497 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1498 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -08001499 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -07001500
Dave Barachb7b92992018-10-17 10:38:51 -04001501 clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1502 clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
Florin Coras1c710452017-10-17 00:03:13 -07001503 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1504 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
1505 error = vnet_session_rule_add_del (&args);
1506 if (error)
1507 {
1508 rv = clib_error_get_code (error);
1509 clib_error_report (error);
1510 }
Florin Corasc97a7392017-11-05 23:07:07 -08001511 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001512 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1513}
1514
Florin Coras6c36f532017-11-03 18:32:34 -07001515static void
1516send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001517 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001518 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001519{
1520 vl_api_session_rules_details_t *rmp = 0;
1521 session_mask_or_match_4_t *match =
1522 (session_mask_or_match_4_t *) & rule->match;
1523 session_mask_or_match_4_t *mask =
1524 (session_mask_or_match_4_t *) & rule->mask;
1525
1526 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001527 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001528 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1529 rmp->context = context;
1530
1531 rmp->is_ip4 = 1;
Dave Barach178cf492018-11-13 16:34:13 -05001532 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1533 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001534 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1535 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001536 rmp->lcl_port = match->lcl_port;
1537 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001538 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1539 rmp->scope =
1540 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1541 rmp->transport_proto = transport_proto;
1542 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001543 if (tag)
1544 {
Dave Barach178cf492018-11-13 16:34:13 -05001545 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001546 rmp->tag[vec_len (tag)] = 0;
1547 }
Florin Coras6c36f532017-11-03 18:32:34 -07001548
Florin Coras6c4dae22018-01-09 06:39:23 -08001549 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001550}
1551
1552static void
Florin Corasc97a7392017-11-05 23:07:07 -08001553send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1554 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001555 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001556{
1557 vl_api_session_rules_details_t *rmp = 0;
1558 session_mask_or_match_6_t *match =
1559 (session_mask_or_match_6_t *) & rule->match;
1560 session_mask_or_match_6_t *mask =
1561 (session_mask_or_match_6_t *) & rule->mask;
1562
1563 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001564 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001565 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1566 rmp->context = context;
1567
1568 rmp->is_ip4 = 0;
Dave Barach178cf492018-11-13 16:34:13 -05001569 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1570 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001571 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1572 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001573 rmp->lcl_port = match->lcl_port;
1574 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001575 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001576 rmp->scope =
1577 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -07001578 rmp->transport_proto = transport_proto;
1579 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001580 if (tag)
1581 {
Dave Barach178cf492018-11-13 16:34:13 -05001582 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001583 rmp->tag[vec_len (tag)] = 0;
1584 }
Florin Coras6c36f532017-11-03 18:32:34 -07001585
Florin Coras6c4dae22018-01-09 06:39:23 -08001586 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001587}
1588
1589static void
1590send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001591 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001592 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001593{
1594 mma_rule_16_t *rule16;
1595 mma_rule_40_t *rule40;
1596 mma_rules_table_16_t *srt16;
1597 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001598 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001599
Florin Corasc97a7392017-11-05 23:07:07 -08001600 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001601 {
Florin Corasc97a7392017-11-05 23:07:07 -08001602 u8 *tag = 0;
1603 /* *INDENT-OFF* */
1604 srt16 = &srt->session_rules_tables_16;
1605 pool_foreach (rule16, srt16->rules, ({
1606 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1607 tag = session_rules_table_rule_tag (srt, ri, 1);
1608 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001609 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001610 }));
1611 /* *INDENT-ON* */
1612 }
1613 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1614 {
1615 u8 *tag = 0;
1616 /* *INDENT-OFF* */
1617 srt40 = &srt->session_rules_tables_40;
1618 pool_foreach (rule40, srt40->rules, ({
1619 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1620 tag = session_rules_table_rule_tag (srt, ri, 1);
1621 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001622 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001623 }));
1624 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001625 }
1626}
1627
1628static void
1629vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1630{
Florin Coras6c4dae22018-01-09 06:39:23 -08001631 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001632 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001633 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001634
Florin Coras6c4dae22018-01-09 06:39:23 -08001635 reg = vl_api_client_index_to_registration (mp->client_index);
1636 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001637 return;
1638
1639 /* *INDENT-OFF* */
1640 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001641 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1642 {
1643 send_session_rules_table_details (&st->session_rules[tp],
1644 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001645 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001646 mp->context);
1647 }
Florin Coras6c36f532017-11-03 18:32:34 -07001648 }));
1649 /* *INDENT-ON* */
1650}
1651
Florin Coras371ca502018-02-21 12:07:41 -08001652static void
1653vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1654 mp)
1655{
1656 vl_api_app_namespace_add_del_reply_t *rmp;
1657 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1658 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001659 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001660 u32 cert_len;
1661 int rv = 0;
1662 if (!session_manager_is_enabled ())
1663 {
1664 rv = VNET_API_ERROR_FEATURE_DISABLED;
1665 goto done;
1666 }
Florin Corase3e2f072018-03-04 07:24:30 -08001667 if (!(app = application_lookup (mp->client_index)))
1668 {
1669 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1670 goto done;
1671 }
Dave Barachb7b92992018-10-17 10:38:51 -04001672 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001673 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001674 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001675 if (cert_len > 10000)
1676 {
1677 rv = VNET_API_ERROR_INVALID_VALUE;
1678 goto done;
1679 }
Florin Coras371ca502018-02-21 12:07:41 -08001680 vec_validate (a->cert, cert_len);
Dave Barach178cf492018-11-13 16:34:13 -05001681 clib_memcpy_fast (a->cert, mp->cert, cert_len);
Florin Coras371ca502018-02-21 12:07:41 -08001682 if ((error = vnet_app_add_tls_cert (a)))
1683 {
1684 rv = clib_error_get_code (error);
1685 clib_error_report (error);
1686 }
1687 vec_free (a->cert);
1688done:
1689 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1690}
1691
1692static void
1693vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1694 mp)
1695{
1696 vl_api_app_namespace_add_del_reply_t *rmp;
1697 vnet_app_add_tls_key_args_t _a, *a = &_a;
1698 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001699 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001700 u32 key_len;
1701 int rv = 0;
1702 if (!session_manager_is_enabled ())
1703 {
1704 rv = VNET_API_ERROR_FEATURE_DISABLED;
1705 goto done;
1706 }
Florin Corase3e2f072018-03-04 07:24:30 -08001707 if (!(app = application_lookup (mp->client_index)))
1708 {
1709 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1710 goto done;
1711 }
Dave Barachb7b92992018-10-17 10:38:51 -04001712 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001713 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001714 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001715 if (key_len > 10000)
1716 {
1717 rv = VNET_API_ERROR_INVALID_VALUE;
1718 goto done;
1719 }
Florin Coras371ca502018-02-21 12:07:41 -08001720 vec_validate (a->key, key_len);
Dave Barach178cf492018-11-13 16:34:13 -05001721 clib_memcpy_fast (a->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001722 if ((error = vnet_app_add_tls_key (a)))
1723 {
1724 rv = clib_error_get_code (error);
1725 clib_error_report (error);
1726 }
1727 vec_free (a->key);
1728done:
1729 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1730}
1731
Florin Coras6cf30ad2017-04-04 23:08:23 -07001732static clib_error_t *
1733application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001734{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001735 application_t *app = application_lookup (client_index);
1736 vnet_app_detach_args_t _a, *a = &_a;
1737 if (app)
1738 {
Florin Coras15531972018-08-12 23:50:53 -07001739 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001740 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001741 vnet_application_detach (a);
1742 }
1743 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001744}
1745
Florin Coras6cf30ad2017-04-04 23:08:23 -07001746VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001747
1748#define vl_msg_name_crc_list
1749#include <vnet/vnet_all_api_h.h>
1750#undef vl_msg_name_crc_list
1751
1752static void
1753setup_message_id_table (api_main_t * am)
1754{
1755#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1756 foreach_vl_msg_name_crc_session;
1757#undef _
1758}
1759
1760/*
1761 * session_api_hookup
1762 * Add uri's API message handlers to the table.
1763 * vlib has alread mapped shared memory and
1764 * added the client registration handlers.
1765 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1766 */
1767static clib_error_t *
1768session_api_hookup (vlib_main_t * vm)
1769{
1770 api_main_t *am = &api_main;
1771
1772#define _(N,n) \
1773 vl_msg_api_set_handlers(VL_API_##N, #n, \
1774 vl_api_##n##_t_handler, \
1775 vl_noop_handler, \
1776 vl_api_##n##_t_endian, \
1777 vl_api_##n##_t_print, \
1778 sizeof(vl_api_##n##_t), 1);
1779 foreach_session_api_msg;
1780#undef _
1781
1782 /*
1783 * Messages which bounce off the data-plane to
1784 * an API client. Simply tells the message handling infra not
1785 * to free the message.
1786 *
1787 * Bounced message handlers MUST NOT block the data plane
1788 */
1789 am->message_bounce[VL_API_CONNECT_URI] = 1;
1790 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1791
1792 /*
1793 * Set up the (msg_name, crc, message-id) table
1794 */
1795 setup_message_id_table (am);
1796
1797 return 0;
1798}
1799
1800VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001801
Dave Barach68b0fb02017-02-28 15:15:56 -05001802/*
1803 * fd.io coding-style-patch-verification: ON
1804 *
1805 * Local Variables:
1806 * eval: (c-set-style "gnu")
1807 * End:
1808 */