blob: 713d70b490b5f5d9b75491b7a2656ded9fb50fe5 [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 Coras87c65702019-01-26 14:33:26 -0800439 clib_memset (mp, 0, sizeof (*mp));
Florin Corasab2f6db2018-08-31 14:31:41 -0700440 mp->context = app->app_index;
Florin Coras52207f12018-07-12 14:48:06 -0700441 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
442 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800443 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700444
445 if (session_has_transport (s))
446 {
447 listener = listen_session_get (s->listener_index);
448 mp->listener_handle = listen_session_get_handle (listener);
449 if (application_is_proxy (app))
450 {
451 listener =
Florin Coras15531972018-08-12 23:50:53 -0700452 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
453 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700454 if (listener)
455 mp->listener_handle = listen_session_get_handle (listener);
456 }
457 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
458 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
459 mp->handle = session_handle (s);
460 tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
461 tc = tp_vft->get_connection (s->connection_index, s->thread_index);
462 mp->port = tc->rmt_port;
463 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500464 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700465 }
466 else
467 {
468 local_session_t *ls = (local_session_t *) s;
469 local_session_t *ll;
Florin Coras99368312018-08-02 10:45:44 -0700470 u8 main_thread = vlib_num_workers ()? 1 : 0;
471
Florin Corasc1f5a432018-11-20 11:31:26 -0800472 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700473 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700474 ls->server_evt_q,
475 ls->client_evt_q);
476
Florin Coras52207f12018-07-12 14:48:06 -0700477 if (application_local_session_listener_has_transport (ls))
478 {
479 listener = listen_session_get (ls->listener_index);
480 mp->listener_handle = listen_session_get_handle (listener);
481 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
482 }
483 else
484 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700485 ll = application_get_local_listen_session (app, ls->listener_index);
Florin Coras52207f12018-07-12 14:48:06 -0700486 if (ll->transport_listener_index != ~0)
487 {
488 listener = listen_session_get (ll->transport_listener_index);
489 mp->listener_handle = listen_session_get_handle (listener);
490 }
491 else
492 {
493 mp->listener_handle = application_local_session_handle (ll);
494 }
495 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
496 }
497 mp->handle = application_local_session_handle (ls);
498 mp->port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700499 vpp_queue = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700500 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
501 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700502 mp->server_event_queue_address = ls->server_evt_q;
503 }
Florin Coras537b17e2018-09-28 10:35:45 -0700504 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700505
506 return 0;
507}
508
Florin Coras72b04282019-01-14 17:23:11 -0800509static inline void
510mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
511 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700512{
Florin Coras52207f12018-07-12 14:48:06 -0700513 svm_msg_q_msg_t _msg, *msg = &_msg;
514 session_disconnected_msg_t *mp;
515 svm_msg_q_t *app_mq;
516 session_event_t *evt;
517
Florin Coras15531972018-08-12 23:50:53 -0700518 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700519 if (mq_try_lock_and_alloc_msg (app_mq, msg))
520 return;
Florin Coras52207f12018-07-12 14:48:06 -0700521 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400522 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800523 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700524 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800525 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800526 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700527 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700528}
529
Florin Coras72b04282019-01-14 17:23:11 -0800530static inline void
531mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
532 svm_fifo_t * f, session_evt_type_t evt_type)
533{
534 app_worker_t *app_wrk;
535 application_t *app;
536 int i;
537
538 app = application_get (app_index);
539 if (!app)
540 return;
541
542 for (i = 0; i < f->n_subscribers; i++)
543 {
544 if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
545 continue;
546 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
547 }
548}
549
550static void
551mq_send_session_disconnected_cb (stream_session_t * s)
552{
553 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
554 session_handle_t sh = session_handle (s);
555
556 mq_send_session_close_evt (app_wrk, session_handle (s),
557 SESSION_CTRL_EVT_DISCONNECTED);
558
559 if (svm_fifo_n_subscribers (s->server_rx_fifo))
560 mq_notify_close_subscribers (app_wrk->app_index, sh, s->server_rx_fifo,
561 SESSION_CTRL_EVT_DISCONNECTED);
562}
563
Florin Corasaa27eb92018-10-13 12:20:01 -0700564void
565mq_send_local_session_disconnected_cb (u32 app_wrk_index,
566 local_session_t * ls)
567{
568 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
Florin Coras72b04282019-01-14 17:23:11 -0800569 session_handle_t sh = application_local_session_handle (ls);
Florin Corasaa27eb92018-10-13 12:20:01 -0700570
Florin Coras72b04282019-01-14 17:23:11 -0800571 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
572
573 if (svm_fifo_n_subscribers (ls->server_rx_fifo))
574 mq_notify_close_subscribers (app_wrk->app_index, sh, ls->server_rx_fifo,
575 SESSION_CTRL_EVT_DISCONNECTED);
Florin Corasaa27eb92018-10-13 12:20:01 -0700576}
577
Florin Coras52207f12018-07-12 14:48:06 -0700578static void
579mq_send_session_reset_cb (stream_session_t * s)
580{
Florin Coras72b04282019-01-14 17:23:11 -0800581 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
582 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700583
Florin Coras72b04282019-01-14 17:23:11 -0800584 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
585
586 if (svm_fifo_n_subscribers (s->server_rx_fifo))
587 mq_notify_close_subscribers (app_wrk->app_index, sh, s->server_rx_fifo,
588 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700589}
590
591static int
Florin Coras15531972018-08-12 23:50:53 -0700592mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras52207f12018-07-12 14:48:06 -0700593 stream_session_t * s, u8 is_fail)
594{
595 svm_msg_q_msg_t _msg, *msg = &_msg;
596 session_connected_msg_t *mp;
597 svm_msg_q_t *vpp_mq, *app_mq;
598 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700599 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700600 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700601
Florin Coras15531972018-08-12 23:50:53 -0700602 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700603 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700604 if (!app_mq)
605 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800606 clib_warning ("app %u with api index: %u not attached",
607 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700608 return -1;
609 }
610
Florin Coras83ea6692018-10-12 16:55:14 -0700611 if (mq_try_lock_and_alloc_msg (app_mq, msg))
612 return -1;
Florin Corasdc2e2512018-12-03 17:47:26 -0800613
Florin Coras52207f12018-07-12 14:48:06 -0700614 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400615 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700616 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
617 mp = (session_connected_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800618 clib_memset (mp, 0, sizeof (*mp));
Florin Coras52207f12018-07-12 14:48:06 -0700619 mp->context = api_context;
620
621 if (is_fail)
622 goto done;
623
Florin Corasdc2e2512018-12-03 17:47:26 -0800624 mp->segment_handle = session_segment_handle (s);
625
Florin Coras52207f12018-07-12 14:48:06 -0700626 if (session_has_transport (s))
627 {
628 tc = session_get_transport (s);
629 if (!tc)
630 {
631 is_fail = 1;
632 goto done;
633 }
634
635 vpp_mq = session_manager_get_vpp_event_queue (s->thread_index);
636 mp->handle = session_handle (s);
637 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Dave Barach178cf492018-11-13 16:34:13 -0500638 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700639 mp->is_ip4 = tc->is_ip4;
640 mp->lcl_port = tc->lcl_port;
641 mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
642 mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
643 }
644 else
645 {
646 local_session_t *ls = (local_session_t *) s;
Florin Coras99368312018-08-02 10:45:44 -0700647 u8 main_thread = vlib_num_workers ()? 1 : 0;
648
Florin Corasc1f5a432018-11-20 11:31:26 -0800649 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700650 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700651 ls->client_evt_q,
652 ls->server_evt_q);
653
Florin Coras52207f12018-07-12 14:48:06 -0700654 mp->handle = application_local_session_handle (ls);
655 mp->lcl_port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700656 vpp_mq = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700657 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras52207f12018-07-12 14:48:06 -0700658 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700659 mp->server_event_queue_address = ls->server_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700660 mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
661 mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
662 }
663
664done:
665 mp->retval = is_fail ?
666 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
667
Florin Coras537b17e2018-09-28 10:35:45 -0700668 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700669 return 0;
670}
671
Florin Coras60116992018-08-27 09:52:18 -0700672static int
673mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
674 session_handle_t handle, int rv)
675{
676 svm_msg_q_msg_t _msg, *msg = &_msg;
677 svm_msg_q_t *app_mq, *vpp_evt_q;
678 transport_connection_t *tc;
679 stream_session_t *ls = 0;
680 session_bound_msg_t *mp;
681 app_worker_t *app_wrk;
682 session_event_t *evt;
683 application_t *app;
684
685 app_wrk = app_worker_get (app_wrk_index);
686 app = application_get (app_wrk->app_index);
687 app_mq = app_wrk->event_queue;
688 if (!app_mq)
689 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800690 clib_warning ("app %u with api index: %u not attached",
691 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700692 return -1;
693 }
694
Florin Coras83ea6692018-10-12 16:55:14 -0700695 if (mq_try_lock_and_alloc_msg (app_mq, msg))
696 return -1;
697
Florin Coras60116992018-08-27 09:52:18 -0700698 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400699 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700700 evt->event_type = SESSION_CTRL_EVT_BOUND;
701 mp = (session_bound_msg_t *) evt->data;
702 mp->context = api_context;
703
704 if (rv)
705 goto done;
706
707 mp->handle = handle;
708 if (application_has_global_scope (app))
709 {
710 ls = listen_session_get_from_handle (handle);
711 tc = listen_session_get_transport (ls);
712 mp->lcl_port = tc->lcl_port;
713 mp->lcl_is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500714 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras60116992018-08-27 09:52:18 -0700715 }
716 else
717 {
718 local_session_t *local;
Florin Corasab2f6db2018-08-31 14:31:41 -0700719 local = application_get_local_listener_w_handle (handle);
Florin Coras60116992018-08-27 09:52:18 -0700720 mp->lcl_port = local->port;
721 mp->lcl_is_ip4 = session_type_is_ip4 (local->session_type);
722 }
723
Florin Coras5f45e012019-01-23 09:21:30 -0800724 vpp_evt_q = session_manager_get_vpp_event_queue (0);
725 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
726
Florin Coras60116992018-08-27 09:52:18 -0700727 if (ls && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
728 {
729 mp->rx_fifo = pointer_to_uword (ls->server_rx_fifo);
730 mp->tx_fifo = pointer_to_uword (ls->server_tx_fifo);
Florin Coras60116992018-08-27 09:52:18 -0700731 }
732
733done:
734 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700735 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700736 return 0;
737}
738
Florin Coras52207f12018-07-12 14:48:06 -0700739static session_cb_vft_t session_mq_cb_vft = {
740 .session_accept_callback = mq_send_session_accepted_cb,
741 .session_disconnect_callback = mq_send_session_disconnected_cb,
742 .session_connected_callback = mq_send_session_connected_cb,
743 .session_reset_callback = mq_send_session_reset_cb,
744 .add_segment_callback = send_add_segment_callback,
745 .del_segment_callback = send_del_segment_callback,
746};
747
Dave Barach68b0fb02017-02-28 15:15:56 -0500748static void
Florin Corase04c2992017-03-01 08:17:34 -0800749vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
750{
751 vl_api_session_enable_disable_reply_t *rmp;
752 vlib_main_t *vm = vlib_get_main ();
753 int rv = 0;
754
755 vnet_session_enable_disable (vm, mp->is_enable);
756 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
757}
758
759static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700760vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500761{
Florin Coras99368312018-08-02 10:45:44 -0700762 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700763 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800764 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700765 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800766 vl_api_registration_t *reg;
Florin Corascea194d2017-10-02 00:18:51 -0700767 clib_error_t *error = 0;
Florin Coras99368312018-08-02 10:45:44 -0700768 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500769
Florin Corasfc804d92018-01-26 01:27:01 -0800770 reg = vl_api_client_index_to_registration (mp->client_index);
771 if (!reg)
772 return;
773
Florin Coras6cf30ad2017-04-04 23:08:23 -0700774 if (session_manager_is_enabled () == 0)
775 {
776 rv = VNET_API_ERROR_FEATURE_DISABLED;
777 goto done;
778 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500779
Florin Corasff6e7692017-12-11 04:59:01 -0800780 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700781 sizeof (mp->options),
782 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500783
Dave Barachb7b92992018-10-17 10:38:51 -0400784 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500785 a->api_client_index = mp->client_index;
786 a->options = mp->options;
Florin Coras52207f12018-07-12 14:48:06 -0700787
788 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
789 a->session_cb_vft = &session_mq_cb_vft;
790 else
791 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500792
Florin Corascea194d2017-10-02 00:18:51 -0700793 if (mp->namespace_id_len > 64)
794 {
795 rv = VNET_API_ERROR_INVALID_VALUE;
796 goto done;
797 }
798
799 if (mp->namespace_id_len)
800 {
Dave Wallace8af20542017-10-26 03:29:30 -0400801 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500802 clib_memcpy_fast (a->namespace_id, mp->namespace_id,
803 mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -0700804 }
805
806 if ((error = vnet_application_attach (a)))
807 {
808 rv = clib_error_get_code (error);
809 clib_error_report (error);
Florin Coras99368312018-08-02 10:45:44 -0700810 vec_free (a->namespace_id);
811 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700812 }
813 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500814
Florin Coras99368312018-08-02 10:45:44 -0700815 /* Send event queues segment */
816 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
817 {
818 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
819 fds[n_fds] = evt_q_segment->fd;
820 n_fds += 1;
821 }
822 /* Send fifo segment fd if needed */
823 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
824 {
825 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
826 fds[n_fds] = a->segment->fd;
827 n_fds += 1;
828 }
829 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
830 {
831 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
832 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
833 n_fds += 1;
834 }
835
Florin Coras6cf30ad2017-04-04 23:08:23 -0700836done:
Florin Corasa5464812017-04-19 13:00:05 -0700837
Dave Barach68b0fb02017-02-28 15:15:56 -0500838 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700839 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500840 if (!rv)
841 {
Florin Corasb384b542018-01-15 01:08:33 -0800842 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800843 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500844 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800845 rmp->segment_size = segp->ssvm_size;
846 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500847 {
Florin Corasb384b542018-01-15 01:08:33 -0800848 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
849 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500850 }
Florin Coras99368312018-08-02 10:45:44 -0700851 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
852 rmp->n_fds = n_fds;
853 rmp->fd_flags = fd_flags;
Florin Corasd85de682018-11-29 17:02:29 -0800854 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500855 }
856 }));
857 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800858
Florin Coras99368312018-08-02 10:45:44 -0700859 if (n_fds)
860 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500861}
862
863static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700864vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
865{
866 vl_api_application_detach_reply_t *rmp;
867 int rv = VNET_API_ERROR_INVALID_VALUE_2;
868 vnet_app_detach_args_t _a, *a = &_a;
869 application_t *app;
870
871 if (session_manager_is_enabled () == 0)
872 {
873 rv = VNET_API_ERROR_FEATURE_DISABLED;
874 goto done;
875 }
876
877 app = application_lookup (mp->client_index);
878 if (app)
879 {
Florin Coras15531972018-08-12 23:50:53 -0700880 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800881 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700882 rv = vnet_application_detach (a);
883 }
884
885done:
886 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
887}
888
889static void
890vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
891{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700892 transport_connection_t *tc = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700893 vnet_bind_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700894 vl_api_bind_uri_reply_t *rmp;
895 stream_session_t *s;
896 application_t *app = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700897 svm_msg_q_t *vpp_evt_q;
Florin Coras60116992018-08-27 09:52:18 -0700898 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700899 int rv;
900
901 if (session_manager_is_enabled () == 0)
902 {
903 rv = VNET_API_ERROR_FEATURE_DISABLED;
904 goto done;
905 }
906
907 app = application_lookup (mp->client_index);
908 if (app)
909 {
Dave Barachb7b92992018-10-17 10:38:51 -0400910 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700911 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700912 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700913 rv = vnet_bind_uri (a);
914 }
915 else
916 {
917 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
918 }
919
920done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700921
922 /* *INDENT-OFF* */
923 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
924 if (!rv)
925 {
926 rmp->handle = a->handle;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700927 if (app && application_has_global_scope (app))
928 {
929 s = listen_session_get_from_handle (a->handle);
930 tc = listen_session_get_transport (s);
Florin Coras6c354942018-04-18 00:05:21 -0700931 rmp->lcl_is_ip4 = tc->is_ip4;
932 rmp->lcl_port = tc->lcl_port;
Dave Barach178cf492018-11-13 16:34:13 -0500933 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700934 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
935 {
936 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
937 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
938 vpp_evt_q = session_manager_get_vpp_event_queue (0);
939 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
940 }
941 }
942 }
943 }));
944 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -0700945
946 /* If app uses mq for control messages, send an mq message as well */
947 if (app && application_use_mq_for_ctrl (app))
948 {
949 app_wrk = application_get_worker (app, 0);
950 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
951 rv);
952 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700953}
954
955static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500956vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
957{
958 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700959 application_t *app;
960 vnet_unbind_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500961 int rv;
962
Florin Coras6cf30ad2017-04-04 23:08:23 -0700963 if (session_manager_is_enabled () == 0)
964 {
965 rv = VNET_API_ERROR_FEATURE_DISABLED;
966 goto done;
967 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500968
Florin Coras6cf30ad2017-04-04 23:08:23 -0700969 app = application_lookup (mp->client_index);
970 if (app)
971 {
972 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700973 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700974 rv = vnet_unbind_uri (a);
975 }
976 else
977 {
978 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
979 }
980
981done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500982 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
983}
984
Florin Corasf03a59a2017-06-09 21:07:32 -0700985static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500986vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
987{
Dave Wallace33e002b2017-09-06 01:20:02 -0400988 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500989 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700990 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700991 clib_error_t *error = 0;
992 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500993
Florin Coras6cf30ad2017-04-04 23:08:23 -0700994 if (session_manager_is_enabled () == 0)
995 {
996 rv = VNET_API_ERROR_FEATURE_DISABLED;
997 goto done;
998 }
Florin Corase04c2992017-03-01 08:17:34 -0800999
Florin Coras6cf30ad2017-04-04 23:08:23 -07001000 app = application_lookup (mp->client_index);
1001 if (app)
1002 {
Dave Barachb7b92992018-10-17 10:38:51 -04001003 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -07001004 a->uri = (char *) mp->uri;
1005 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001006 a->app_index = app->app_index;
Florin Corascea194d2017-10-02 00:18:51 -07001007 if ((error = vnet_connect_uri (a)))
1008 {
1009 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -08001010 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -07001011 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001012 }
1013 else
1014 {
1015 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1016 }
Florin Corase04c2992017-03-01 08:17:34 -08001017
Florin Coras3cbc04b2017-10-02 00:18:51 -07001018 /*
1019 * Don't reply to stream (tcp) connects. The reply will come once
1020 * the connection is established. In case of the redirects, the reply
1021 * will come from the server app.
1022 */
Florin Coras8f89dd02018-03-05 16:53:07 -08001023 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001024 return;
1025
Florin Coras6cf30ad2017-04-04 23:08:23 -07001026done:
Florin Corase04c2992017-03-01 08:17:34 -08001027 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -04001028 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -08001029 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001030}
1031
1032static void
1033vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
1034{
1035 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001036 vnet_disconnect_args_t _a, *a = &_a;
1037 application_t *app;
1038 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001039
Florin Coras6cf30ad2017-04-04 23:08:23 -07001040 if (session_manager_is_enabled () == 0)
1041 {
1042 rv = VNET_API_ERROR_FEATURE_DISABLED;
1043 goto done;
1044 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001045
Florin Coras6cf30ad2017-04-04 23:08:23 -07001046 app = application_lookup (mp->client_index);
1047 if (app)
1048 {
1049 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001050 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001051 rv = vnet_disconnect_session (a);
1052 }
1053 else
1054 {
1055 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1056 }
1057
1058done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001059 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001060}
1061
1062static void
1063vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1064 mp)
1065{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001066 vnet_disconnect_args_t _a, *a = &_a;
1067 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001068
1069 /* Client objected to disconnecting the session, log and continue */
1070 if (mp->retval)
1071 {
1072 clib_warning ("client retval %d", mp->retval);
1073 return;
1074 }
1075
1076 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001077 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001078 if (app)
1079 {
1080 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001081 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001082 vnet_disconnect_session (a);
1083 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001084}
1085
1086static void
1087vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
1088{
Florin Coras4af830c2018-12-04 09:21:36 -08001089 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras15531972018-08-12 23:50:53 -07001090 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001091 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001092 stream_session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001093 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05001094
Ondrej Fabry6bd197e2018-08-15 08:46:46 +02001095 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001096 if (!app)
1097 return;
1098
Florin Corascea194d2017-10-02 00:18:51 -07001099 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001100 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -07001101 if (!s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001102 {
1103 clib_warning ("Invalid session!");
1104 return;
1105 }
1106
Florin Coras15531972018-08-12 23:50:53 -07001107 app_wrk = app_worker_get (s->app_wrk_index);
1108 if (app_wrk->app_index != app->app_index)
1109 {
1110 clib_warning ("app %u does not own handle 0x%lx", app->app_index,
1111 mp->handle);
1112 return;
1113 }
1114
Dave Barach68b0fb02017-02-28 15:15:56 -05001115 /* Client objected to resetting the session, log and continue */
1116 if (mp->retval)
1117 {
1118 clib_warning ("client retval %d", mp->retval);
1119 return;
1120 }
1121
Dave Barach68b0fb02017-02-28 15:15:56 -05001122 /* This comes as a response to a reset, transport only waiting for
1123 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -08001124 a->handle = mp->handle;
1125 a->app_index = app->app_index;
1126 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -05001127}
1128
1129static void
1130vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
1131{
Florin Corasf8f516a2018-02-08 15:10:09 -08001132 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
1133 local_session_t *ls;
Dave Barach68b0fb02017-02-28 15:15:56 -05001134 stream_session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001135
Florin Corasa5464812017-04-19 13:00:05 -07001136 /* Server isn't interested, kill the session */
1137 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -05001138 {
Florin Corasa5464812017-04-19 13:00:05 -07001139 a->app_index = mp->context;
1140 a->handle = mp->handle;
1141 vnet_disconnect_session (a);
Florin Corasf8f516a2018-02-08 15:10:09 -08001142 return;
1143 }
1144
1145 if (session_handle_is_local (mp->handle))
1146 {
1147 ls = application_get_local_session_from_handle (mp->handle);
Florin Coras15531972018-08-12 23:50:53 -07001148 if (!ls || ls->app_wrk_index != mp->context)
Florin Corasf8f516a2018-02-08 15:10:09 -08001149 {
1150 clib_warning ("server %u doesn't own local handle %llu",
1151 mp->context, mp->handle);
1152 return;
1153 }
1154 if (application_local_session_connect_notify (ls))
1155 return;
1156 ls->session_state = SESSION_STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001157 }
Florin Corasa5464812017-04-19 13:00:05 -07001158 else
1159 {
Florin Corasf8f516a2018-02-08 15:10:09 -08001160 s = session_get_from_handle_if_valid (mp->handle);
Florin Corasa5464812017-04-19 13:00:05 -07001161 if (!s)
1162 {
1163 clib_warning ("session doesn't exist");
1164 return;
1165 }
Florin Coras15531972018-08-12 23:50:53 -07001166 if (s->app_wrk_index != mp->context)
Florin Corasa5464812017-04-19 13:00:05 -07001167 {
1168 clib_warning ("app doesn't own session");
1169 return;
1170 }
Florin Corasa5464812017-04-19 13:00:05 -07001171 s->session_state = SESSION_STATE_READY;
1172 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001173}
1174
1175static void
1176vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1177 * mp)
1178{
1179 clib_warning ("not implemented");
1180}
1181
1182static void
1183vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1184{
1185 vl_api_bind_sock_reply_t *rmp;
1186 vnet_bind_args_t _a, *a = &_a;
Florin Corascea194d2017-10-02 00:18:51 -07001187 int rv = 0;
1188 clib_error_t *error;
Florin Coraseb2945c2017-11-22 10:39:09 -08001189 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001190 app_worker_t *app_wrk;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001191 stream_session_t *s;
1192 transport_connection_t *tc = 0;
1193 ip46_address_t *ip46;
Florin Coras3c2fed52018-07-04 04:15:05 -07001194 svm_msg_q_t *vpp_evt_q;
Dave Barach68b0fb02017-02-28 15:15:56 -05001195
Florin Coras6cf30ad2017-04-04 23:08:23 -07001196 if (session_manager_is_enabled () == 0)
1197 {
1198 rv = VNET_API_ERROR_FEATURE_DISABLED;
1199 goto done;
1200 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001201
Florin Coras6cf30ad2017-04-04 23:08:23 -07001202 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001203 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001204 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001205 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1206 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001207 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001208
1209 ip46 = (ip46_address_t *) mp->ip;
Dave Barachb7b92992018-10-17 10:38:51 -04001210 clib_memset (a, 0, sizeof (*a));
Florin Corasdcf55ce2017-11-16 15:32:50 -08001211 a->sep.is_ip4 = mp->is_ip4;
1212 a->sep.ip = *ip46;
1213 a->sep.port = mp->port;
1214 a->sep.fib_index = mp->vrf;
1215 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1216 a->sep.transport_proto = mp->proto;
Florin Coras15531972018-08-12 23:50:53 -07001217 a->app_index = app->app_index;
1218 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001219
1220 if ((error = vnet_bind (a)))
1221 {
1222 rv = clib_error_get_code (error);
1223 clib_error_report (error);
1224 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001225
Florin Coras6cf30ad2017-04-04 23:08:23 -07001226done:
Florin Corascea194d2017-10-02 00:18:51 -07001227 /* *INDENT-OFF* */
1228 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1229 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -08001230 {
1231 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -08001232 rmp->lcl_port = mp->port;
Keith Burns (alagalah)60ae80f2018-03-15 10:04:20 -07001233 rmp->lcl_is_ip4 = mp->is_ip4;
Dave Wallace97b1a272017-12-18 13:51:59 -05001234 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -08001235 {
1236 s = listen_session_get_from_handle (a->handle);
1237 tc = listen_session_get_transport (s);
Dave Barach178cf492018-11-13 16:34:13 -05001238 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001239 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1240 {
1241 rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo);
1242 rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo);
1243 vpp_evt_q = session_manager_get_vpp_event_queue (0);
1244 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1245 }
Florin Coraseb2945c2017-11-22 10:39:09 -08001246 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001247 }
Florin Corascea194d2017-10-02 00:18:51 -07001248 }));
1249 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -07001250
1251 /* If app uses mq for control messages, send an mq message as well */
1252 if (app && application_use_mq_for_ctrl (app))
1253 {
1254 app_wrk = application_get_worker (app, mp->wrk_index);
1255 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1256 rv);
1257 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001258}
1259
1260static void
1261vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1262{
1263 vl_api_unbind_sock_reply_t *rmp;
1264 vnet_unbind_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001265 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -07001266 clib_error_t *error;
1267 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001268
Florin Coras6cf30ad2017-04-04 23:08:23 -07001269 if (session_manager_is_enabled () == 0)
1270 {
1271 rv = VNET_API_ERROR_FEATURE_DISABLED;
1272 goto done;
1273 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001274
Florin Coras6cf30ad2017-04-04 23:08:23 -07001275 app = application_lookup (mp->client_index);
1276 if (app)
1277 {
Florin Coras15531972018-08-12 23:50:53 -07001278 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001279 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001280 a->wrk_map_index = mp->wrk_index;
Florin Corascea194d2017-10-02 00:18:51 -07001281 if ((error = vnet_unbind (a)))
1282 {
1283 rv = clib_error_get_code (error);
1284 clib_error_report (error);
1285 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001286 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001287
Florin Coras6cf30ad2017-04-04 23:08:23 -07001288done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001289 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
1290}
1291
1292static void
1293vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1294{
Dave Wallace33e002b2017-09-06 01:20:02 -04001295 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001296 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001297 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001298 clib_error_t *error = 0;
1299 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001300
Florin Coras6cf30ad2017-04-04 23:08:23 -07001301 if (session_manager_is_enabled () == 0)
1302 {
1303 rv = VNET_API_ERROR_FEATURE_DISABLED;
1304 goto done;
1305 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001306
Florin Coras6cf30ad2017-04-04 23:08:23 -07001307 app = application_lookup (mp->client_index);
1308 if (app)
1309 {
Florin Corase86a8ed2018-01-05 03:20:25 -08001310 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -04001311 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001312
Dave Barachb7b92992018-10-17 10:38:51 -04001313 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001314 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1315 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -07001316 a->sep.is_ip4 = mp->is_ip4;
1317 a->sep.ip = *ip46;
1318 a->sep.port = mp->port;
1319 a->sep.transport_proto = mp->proto;
Florin Coras5665ced2018-10-25 18:03:45 -07001320 a->sep.peer.fib_index = mp->vrf;
1321 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras8f89dd02018-03-05 16:53:07 -08001322 if (mp->hostname_len)
1323 {
Florin Coras15531972018-08-12 23:50:53 -07001324 vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001325 clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
1326 mp->hostname_len);
Florin Coras8f89dd02018-03-05 16:53:07 -08001327 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001328 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001329 a->app_index = app->app_index;
1330 a->wrk_map_index = mp->wrk_index;
Florin Corascea194d2017-10-02 00:18:51 -07001331 if ((error = vnet_connect (a)))
1332 {
1333 rv = clib_error_get_code (error);
Florin Coras8f89dd02018-03-05 16:53:07 -08001334 clib_error_report (error);
Florin Corascea194d2017-10-02 00:18:51 -07001335 }
Florin Coras15531972018-08-12 23:50:53 -07001336 vec_free (a->sep_ext.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001337 }
1338 else
1339 {
1340 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1341 }
Florin Corase04c2992017-03-01 08:17:34 -08001342
Florin Coras8f89dd02018-03-05 16:53:07 -08001343 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001344 return;
1345
1346 /* Got some error, relay it */
1347
Florin Coras6cf30ad2017-04-04 23:08:23 -07001348done:
Dave Wallace33e002b2017-09-06 01:20:02 -04001349 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corasab2f6db2018-08-31 14:31:41 -07001350
1351 if (app && application_use_mq_for_ctrl (app))
1352 {
1353 app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1354 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1355 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001356}
1357
Florin Corascea194d2017-10-02 00:18:51 -07001358static void
Florin Coras15531972018-08-12 23:50:53 -07001359vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1360{
1361 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1362 vl_api_app_worker_add_del_reply_t *rmp;
1363 vl_api_registration_t *reg;
1364 clib_error_t *error = 0;
1365 application_t *app;
1366 u8 fd_flags = 0;
1367
1368 if (!session_manager_is_enabled ())
1369 {
1370 rv = VNET_API_ERROR_FEATURE_DISABLED;
1371 goto done;
1372 }
1373
1374 reg = vl_api_client_index_to_registration (mp->client_index);
1375 if (!reg)
1376 return;
1377
Florin Corasc1f5a432018-11-20 11:31:26 -08001378 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001379 if (!app)
1380 {
1381 rv = VNET_API_ERROR_INVALID_VALUE;
1382 goto done;
1383 }
1384
1385 vnet_app_worker_add_del_args_t args = {
1386 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001387 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001388 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001389 .is_add = mp->is_add
1390 };
1391 error = vnet_app_worker_add_del (&args);
1392 if (error)
1393 {
1394 rv = clib_error_get_code (error);
1395 clib_error_report (error);
1396 goto done;
1397 }
1398
Florin Coras14598772018-09-04 19:47:52 -07001399 if (!mp->is_add)
1400 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001401
Florin Coras15531972018-08-12 23:50:53 -07001402 /* Send fifo segment fd if needed */
1403 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1404 {
1405 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1406 fds[n_fds] = args.segment->fd;
1407 n_fds += 1;
1408 }
1409 if (application_segment_manager_properties (app)->use_mq_eventfd)
1410 {
1411 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1412 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1413 n_fds += 1;
1414 }
1415
1416 /* *INDENT-OFF* */
1417done:
1418 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1419 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001420 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001421 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001422 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001423 {
Florin Coras15531972018-08-12 23:50:53 -07001424 if (vec_len (args.segment->name))
1425 {
1426 memcpy (rmp->segment_name, args.segment->name,
1427 vec_len (args.segment->name));
1428 rmp->segment_name_length = vec_len (args.segment->name);
1429 }
1430 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1431 rmp->n_fds = n_fds;
1432 rmp->fd_flags = fd_flags;
1433 }
1434 }));
1435 /* *INDENT-ON* */
1436
1437 if (n_fds)
1438 session_send_fds (reg, fds, n_fds);
1439}
1440
1441static void
Florin Corascea194d2017-10-02 00:18:51 -07001442vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1443{
1444 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Corascea194d2017-10-02 00:18:51 -07001445 clib_error_t *error = 0;
Florin Coras6e8c6672017-11-10 09:03:54 -08001446 u32 appns_index = 0;
1447 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001448 int rv = 0;
1449 if (!session_manager_is_enabled ())
1450 {
1451 rv = VNET_API_ERROR_FEATURE_DISABLED;
1452 goto done;
1453 }
1454
1455 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1456 {
1457 rv = VNET_API_ERROR_INVALID_VALUE;
1458 goto done;
1459 }
1460
1461 vec_validate (ns_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001462 clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -07001463 vnet_app_namespace_add_del_args_t args = {
1464 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001465 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001466 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1467 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1468 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1469 .is_add = 1
1470 };
1471 error = vnet_app_namespace_add_del (&args);
1472 if (error)
1473 {
1474 rv = clib_error_get_code (error);
1475 clib_error_report (error);
1476 }
Florin Coras6e8c6672017-11-10 09:03:54 -08001477 else
1478 {
1479 appns_index = app_namespace_index_from_id (ns_id);
1480 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1481 {
1482 clib_warning ("app ns lookup failed");
1483 rv = VNET_API_ERROR_UNSPECIFIED;
1484 }
1485 }
Florin Corascea194d2017-10-02 00:18:51 -07001486 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -08001487
1488 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -07001489done:
Florin Coras6e8c6672017-11-10 09:03:54 -08001490 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1491 if (!rv)
1492 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1493 }));
1494 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -07001495}
1496
Florin Coras1c710452017-10-17 00:03:13 -07001497static void
1498vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1499{
1500 vl_api_session_rule_add_del_reply_t *rmp;
1501 session_rule_add_del_args_t args;
1502 session_rule_table_add_del_args_t *table_args = &args.table_args;
1503 clib_error_t *error;
1504 u8 fib_proto;
1505 int rv = 0;
1506
Dave Barachb7b92992018-10-17 10:38:51 -04001507 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001508 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1509
1510 table_args->lcl.fp_len = mp->lcl_plen;
1511 table_args->lcl.fp_proto = fib_proto;
1512 table_args->rmt.fp_len = mp->rmt_plen;
1513 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001514 table_args->lcl_port = mp->lcl_port;
1515 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001516 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1517 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001518 mp->tag[sizeof (mp->tag) - 1] = 0;
1519 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001520 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1521 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -08001522 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -07001523
Dave Barachb7b92992018-10-17 10:38:51 -04001524 clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1525 clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
Florin Coras1c710452017-10-17 00:03:13 -07001526 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1527 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
1528 error = vnet_session_rule_add_del (&args);
1529 if (error)
1530 {
1531 rv = clib_error_get_code (error);
1532 clib_error_report (error);
1533 }
Florin Corasc97a7392017-11-05 23:07:07 -08001534 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001535 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1536}
1537
Florin Coras6c36f532017-11-03 18:32:34 -07001538static void
1539send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001540 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001541 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001542{
1543 vl_api_session_rules_details_t *rmp = 0;
1544 session_mask_or_match_4_t *match =
1545 (session_mask_or_match_4_t *) & rule->match;
1546 session_mask_or_match_4_t *mask =
1547 (session_mask_or_match_4_t *) & rule->mask;
1548
1549 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001550 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001551 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1552 rmp->context = context;
1553
1554 rmp->is_ip4 = 1;
Dave Barach178cf492018-11-13 16:34:13 -05001555 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1556 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001557 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1558 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001559 rmp->lcl_port = match->lcl_port;
1560 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001561 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1562 rmp->scope =
1563 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1564 rmp->transport_proto = transport_proto;
1565 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001566 if (tag)
1567 {
Dave Barach178cf492018-11-13 16:34:13 -05001568 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001569 rmp->tag[vec_len (tag)] = 0;
1570 }
Florin Coras6c36f532017-11-03 18:32:34 -07001571
Florin Coras6c4dae22018-01-09 06:39:23 -08001572 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001573}
1574
1575static void
Florin Corasc97a7392017-11-05 23:07:07 -08001576send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1577 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001578 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001579{
1580 vl_api_session_rules_details_t *rmp = 0;
1581 session_mask_or_match_6_t *match =
1582 (session_mask_or_match_6_t *) & rule->match;
1583 session_mask_or_match_6_t *mask =
1584 (session_mask_or_match_6_t *) & rule->mask;
1585
1586 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001587 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001588 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1589 rmp->context = context;
1590
1591 rmp->is_ip4 = 0;
Dave Barach178cf492018-11-13 16:34:13 -05001592 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1593 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001594 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1595 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001596 rmp->lcl_port = match->lcl_port;
1597 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001598 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001599 rmp->scope =
1600 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -07001601 rmp->transport_proto = transport_proto;
1602 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001603 if (tag)
1604 {
Dave Barach178cf492018-11-13 16:34:13 -05001605 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001606 rmp->tag[vec_len (tag)] = 0;
1607 }
Florin Coras6c36f532017-11-03 18:32:34 -07001608
Florin Coras6c4dae22018-01-09 06:39:23 -08001609 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001610}
1611
1612static void
1613send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001614 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001615 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001616{
1617 mma_rule_16_t *rule16;
1618 mma_rule_40_t *rule40;
1619 mma_rules_table_16_t *srt16;
1620 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001621 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001622
Florin Corasc97a7392017-11-05 23:07:07 -08001623 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001624 {
Florin Corasc97a7392017-11-05 23:07:07 -08001625 u8 *tag = 0;
1626 /* *INDENT-OFF* */
1627 srt16 = &srt->session_rules_tables_16;
1628 pool_foreach (rule16, srt16->rules, ({
1629 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1630 tag = session_rules_table_rule_tag (srt, ri, 1);
1631 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001632 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001633 }));
1634 /* *INDENT-ON* */
1635 }
1636 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1637 {
1638 u8 *tag = 0;
1639 /* *INDENT-OFF* */
1640 srt40 = &srt->session_rules_tables_40;
1641 pool_foreach (rule40, srt40->rules, ({
1642 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1643 tag = session_rules_table_rule_tag (srt, ri, 1);
1644 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001645 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001646 }));
1647 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001648 }
1649}
1650
1651static void
1652vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1653{
Florin Coras6c4dae22018-01-09 06:39:23 -08001654 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001655 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001656 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001657
Florin Coras6c4dae22018-01-09 06:39:23 -08001658 reg = vl_api_client_index_to_registration (mp->client_index);
1659 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001660 return;
1661
1662 /* *INDENT-OFF* */
1663 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001664 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1665 {
1666 send_session_rules_table_details (&st->session_rules[tp],
1667 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001668 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001669 mp->context);
1670 }
Florin Coras6c36f532017-11-03 18:32:34 -07001671 }));
1672 /* *INDENT-ON* */
1673}
1674
Florin Coras371ca502018-02-21 12:07:41 -08001675static void
1676vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1677 mp)
1678{
1679 vl_api_app_namespace_add_del_reply_t *rmp;
1680 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1681 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001682 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001683 u32 cert_len;
1684 int rv = 0;
1685 if (!session_manager_is_enabled ())
1686 {
1687 rv = VNET_API_ERROR_FEATURE_DISABLED;
1688 goto done;
1689 }
Florin Corase3e2f072018-03-04 07:24:30 -08001690 if (!(app = application_lookup (mp->client_index)))
1691 {
1692 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1693 goto done;
1694 }
Dave Barachb7b92992018-10-17 10:38:51 -04001695 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001696 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001697 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001698 if (cert_len > 10000)
1699 {
1700 rv = VNET_API_ERROR_INVALID_VALUE;
1701 goto done;
1702 }
Florin Coras371ca502018-02-21 12:07:41 -08001703 vec_validate (a->cert, cert_len);
Dave Barach178cf492018-11-13 16:34:13 -05001704 clib_memcpy_fast (a->cert, mp->cert, cert_len);
Florin Coras371ca502018-02-21 12:07:41 -08001705 if ((error = vnet_app_add_tls_cert (a)))
1706 {
1707 rv = clib_error_get_code (error);
1708 clib_error_report (error);
1709 }
1710 vec_free (a->cert);
1711done:
1712 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1713}
1714
1715static void
1716vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1717 mp)
1718{
1719 vl_api_app_namespace_add_del_reply_t *rmp;
1720 vnet_app_add_tls_key_args_t _a, *a = &_a;
1721 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001722 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001723 u32 key_len;
1724 int rv = 0;
1725 if (!session_manager_is_enabled ())
1726 {
1727 rv = VNET_API_ERROR_FEATURE_DISABLED;
1728 goto done;
1729 }
Florin Corase3e2f072018-03-04 07:24:30 -08001730 if (!(app = application_lookup (mp->client_index)))
1731 {
1732 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1733 goto done;
1734 }
Dave Barachb7b92992018-10-17 10:38:51 -04001735 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001736 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001737 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001738 if (key_len > 10000)
1739 {
1740 rv = VNET_API_ERROR_INVALID_VALUE;
1741 goto done;
1742 }
Florin Coras371ca502018-02-21 12:07:41 -08001743 vec_validate (a->key, key_len);
Dave Barach178cf492018-11-13 16:34:13 -05001744 clib_memcpy_fast (a->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001745 if ((error = vnet_app_add_tls_key (a)))
1746 {
1747 rv = clib_error_get_code (error);
1748 clib_error_report (error);
1749 }
1750 vec_free (a->key);
1751done:
1752 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1753}
1754
Florin Coras6cf30ad2017-04-04 23:08:23 -07001755static clib_error_t *
1756application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001757{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001758 application_t *app = application_lookup (client_index);
1759 vnet_app_detach_args_t _a, *a = &_a;
1760 if (app)
1761 {
Florin Coras15531972018-08-12 23:50:53 -07001762 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001763 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001764 vnet_application_detach (a);
1765 }
1766 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001767}
1768
Florin Coras6cf30ad2017-04-04 23:08:23 -07001769VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001770
1771#define vl_msg_name_crc_list
1772#include <vnet/vnet_all_api_h.h>
1773#undef vl_msg_name_crc_list
1774
1775static void
1776setup_message_id_table (api_main_t * am)
1777{
1778#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1779 foreach_vl_msg_name_crc_session;
1780#undef _
1781}
1782
1783/*
1784 * session_api_hookup
1785 * Add uri's API message handlers to the table.
1786 * vlib has alread mapped shared memory and
1787 * added the client registration handlers.
1788 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1789 */
1790static clib_error_t *
1791session_api_hookup (vlib_main_t * vm)
1792{
1793 api_main_t *am = &api_main;
1794
1795#define _(N,n) \
1796 vl_msg_api_set_handlers(VL_API_##N, #n, \
1797 vl_api_##n##_t_handler, \
1798 vl_noop_handler, \
1799 vl_api_##n##_t_endian, \
1800 vl_api_##n##_t_print, \
1801 sizeof(vl_api_##n##_t), 1);
1802 foreach_session_api_msg;
1803#undef _
1804
1805 /*
1806 * Messages which bounce off the data-plane to
1807 * an API client. Simply tells the message handling infra not
1808 * to free the message.
1809 *
1810 * Bounced message handlers MUST NOT block the data plane
1811 */
1812 am->message_bounce[VL_API_CONNECT_URI] = 1;
1813 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1814
1815 /*
1816 * Set up the (msg_name, crc, message-id) table
1817 */
1818 setup_message_id_table (am);
1819
1820 return 0;
1821}
1822
1823VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001824
Dave Barach68b0fb02017-02-28 15:15:56 -05001825/*
1826 * fd.io coding-style-patch-verification: ON
1827 *
1828 * Local Variables:
1829 * eval: (c-set-style "gnu")
1830 * End:
1831 */