blob: 015b29f81d2d9e57764e4d2eb3ca3a562bef9d06 [file] [log] [blame]
Dave Barach68b0fb02017-02-28 15:15:56 -05001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2015-2019 Cisco and/or its affiliates.
Dave Barach68b0fb02017-02-28 15:15:56 -05003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/vnet.h>
17#include <vlibmemory/api.h>
18#include <vnet/session/application.h>
Florin Coras1c710452017-10-17 00:03:13 -070019#include <vnet/session/application_interface.h>
20#include <vnet/session/session_rules_table.h>
Florin Coras6c36f532017-11-03 18:32:34 -070021#include <vnet/session/session_table.h>
Florin Corasc9940fc2019-02-05 20:55:11 -080022#include <vnet/session/session.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050023
24#include <vnet/vnet_msg_enum.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050025
26#define vl_typedefs /* define message structures */
27#include <vnet/vnet_all_api_h.h>
28#undef vl_typedefs
29
30#define vl_endianfun /* define message structures */
31#include <vnet/vnet_all_api_h.h>
32#undef vl_endianfun
33
34/* instantiate all the print functions we know about */
35#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
36#define vl_printfun
37#include <vnet/vnet_all_api_h.h>
38#undef vl_printfun
39
40#include <vlibapi/api_helper_macros.h>
41
42#define foreach_session_api_msg \
43_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070044_(APPLICATION_ATTACH, application_attach) \
45_(APPLICATION_DETACH, application_detach) \
Dave Barach68b0fb02017-02-28 15:15:56 -050046_(BIND_URI, bind_uri) \
47_(UNBIND_URI, unbind_uri) \
48_(CONNECT_URI, connect_uri) \
49_(DISCONNECT_SESSION, disconnect_session) \
50_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
51_(ACCEPT_SESSION_REPLY, accept_session_reply) \
52_(RESET_SESSION_REPLY, reset_session_reply) \
Florin Corascea194d2017-10-02 00:18:51 -070053_(BIND_SOCK, bind_sock) \
Dave Barach68b0fb02017-02-28 15:15:56 -050054_(UNBIND_SOCK, unbind_sock) \
55_(CONNECT_SOCK, connect_sock) \
Florin Corase04c2992017-03-01 08:17:34 -080056_(SESSION_ENABLE_DISABLE, session_enable_disable) \
Florin Corascea194d2017-10-02 00:18:51 -070057_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
Florin Coras1c710452017-10-17 00:03:13 -070058_(SESSION_RULE_ADD_DEL, session_rule_add_del) \
Florin Coras6c36f532017-11-03 18:32:34 -070059_(SESSION_RULES_DUMP, session_rules_dump) \
Florin Coras371ca502018-02-21 12:07:41 -080060_(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \
61_(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \
Florin Coras15531972018-08-12 23:50:53 -070062_(APP_WORKER_ADD_DEL, app_worker_add_del) \
Florin Corase04c2992017-03-01 08:17:34 -080063
Dave Barach68b0fb02017-02-28 15:15:56 -050064static int
Florin Coras99368312018-08-02 10:45:44 -070065session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
Florin Corasb384b542018-01-15 01:08:33 -080066{
67 clib_error_t *error;
68 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
69 {
70 clib_warning ("can't send memfd fd");
71 return -1;
72 }
Florin Coras99368312018-08-02 10:45:44 -070073 error = vl_api_send_fd_msg (reg, fds, n_fds);
Florin Corasb384b542018-01-15 01:08:33 -080074 if (error)
75 {
76 clib_error_report (error);
77 return -1;
78 }
79 return 0;
80}
81
82static int
Florin Corasfa76a762018-11-29 12:40:10 -080083send_add_segment_callback (u32 api_client_index, u64 segment_handle)
Dave Barach68b0fb02017-02-28 15:15:56 -050084{
Florin Coras99368312018-08-02 10:45:44 -070085 int fds[SESSION_N_FD_TYPE], n_fds = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050086 vl_api_map_another_segment_t *mp;
Florin Corasfa76a762018-11-29 12:40:10 -080087 svm_fifo_segment_private_t *fs;
Florin Corasb384b542018-01-15 01:08:33 -080088 vl_api_registration_t *reg;
Florin Corasfa76a762018-11-29 12:40:10 -080089 ssvm_private_t *sp;
Florin Coras99368312018-08-02 10:45:44 -070090 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050091
Florin Corasb384b542018-01-15 01:08:33 -080092 reg = vl_mem_api_client_index_to_registration (api_client_index);
93 if (!reg)
94 {
Florin Corasfa76a762018-11-29 12:40:10 -080095 clib_warning ("no api registration for client: %u", api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -080096 return -1;
97 }
Dave Barach68b0fb02017-02-28 15:15:56 -050098
Florin Corasfa76a762018-11-29 12:40:10 -080099 fs = segment_manager_get_segment_w_handle (segment_handle);
100 sp = &fs->ssvm;
Florin Coras99368312018-08-02 10:45:44 -0700101 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
Florin Corasb384b542018-01-15 01:08:33 -0800102 {
Florin Coras99368312018-08-02 10:45:44 -0700103 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
104 {
105 clib_warning ("can't send memfd fd");
106 return -1;
107 }
108
109 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
110 fds[n_fds] = sp->fd;
111 n_fds += 1;
Florin Corasb384b542018-01-15 01:08:33 -0800112 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500113
Florin Coras99368312018-08-02 10:45:44 -0700114 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400115 clib_memset (mp, 0, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500116 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
Florin Corasb384b542018-01-15 01:08:33 -0800117 mp->segment_size = sp->ssvm_size;
Florin Coras99368312018-08-02 10:45:44 -0700118 mp->fd_flags = fd_flags;
Florin Corasfa76a762018-11-29 12:40:10 -0800119 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasb384b542018-01-15 01:08:33 -0800120 strncpy ((char *) mp->segment_name, (char *) sp->name,
Dave Barach68b0fb02017-02-28 15:15:56 -0500121 sizeof (mp->segment_name) - 1);
122
Florin Corasb384b542018-01-15 01:08:33 -0800123 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
124
Florin Coras99368312018-08-02 10:45:44 -0700125 if (n_fds)
126 return session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500127
128 return 0;
129}
130
131static int
Florin Corasfa76a762018-11-29 12:40:10 -0800132send_del_segment_callback (u32 api_client_index, u64 segment_handle)
Florin Corasf8f516a2018-02-08 15:10:09 -0800133{
134 vl_api_unmap_segment_t *mp;
135 vl_api_registration_t *reg;
136
137 reg = vl_mem_api_client_index_to_registration (api_client_index);
138 if (!reg)
139 {
140 clib_warning ("no registration: %u", api_client_index);
141 return -1;
142 }
143
Florin Coras99368312018-08-02 10:45:44 -0700144 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400145 clib_memset (mp, 0, sizeof (*mp));
Florin Corasf8f516a2018-02-08 15:10:09 -0800146 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
Florin Corasfa76a762018-11-29 12:40:10 -0800147 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasf8f516a2018-02-08 15:10:09 -0800148 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
149
Florin Coras99368312018-08-02 10:45:44 -0700150 return 0;
151}
152
153static int
Florin Coras134a9962018-08-28 11:32:04 -0700154send_app_cut_through_registration_add (u32 api_client_index,
155 u32 wrk_map_index, u64 mq_addr,
Florin Coras99368312018-08-02 10:45:44 -0700156 u64 peer_mq_addr)
157{
158 vl_api_app_cut_through_registration_add_t *mp;
159 vl_api_registration_t *reg;
160 svm_msg_q_t *mq, *peer_mq;
161 int fds[2];
162
163 reg = vl_mem_api_client_index_to_registration (api_client_index);
164 if (!reg)
165 {
166 clib_warning ("no registration: %u", api_client_index);
167 return -1;
168 }
169
170 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400171 clib_memset (mp, 0, sizeof (*mp));
Florin Coras99368312018-08-02 10:45:44 -0700172 mp->_vl_msg_id =
173 clib_host_to_net_u16 (VL_API_APP_CUT_THROUGH_REGISTRATION_ADD);
174
175 mp->evt_q_address = mq_addr;
176 mp->peer_evt_q_address = peer_mq_addr;
Florin Coras134a9962018-08-28 11:32:04 -0700177 mp->wrk_index = wrk_map_index;
Florin Coras99368312018-08-02 10:45:44 -0700178
179 mq = uword_to_pointer (mq_addr, svm_msg_q_t *);
180 peer_mq = uword_to_pointer (peer_mq_addr, svm_msg_q_t *);
181
182 if (svm_msg_q_get_producer_eventfd (mq) != -1)
183 {
184 mp->fd_flags |= SESSION_FD_F_MQ_EVENTFD;
185 mp->n_fds = 2;
186 /* app will overwrite exactly the fds we pass here. So
187 * when we swap mq with peer_mq (accept vs connect) the
188 * fds will still be valid */
189 fds[0] = svm_msg_q_get_consumer_eventfd (mq);
190 fds[1] = svm_msg_q_get_producer_eventfd (peer_mq);
191 }
192
193 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
194
195 if (mp->n_fds != 0)
196 session_send_fds (reg, fds, mp->n_fds);
Florin Corasf8f516a2018-02-08 15:10:09 -0800197
198 return 0;
199}
200
201static int
Florin Coras288eaab2019-02-03 15:26:14 -0800202send_session_accept_callback (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500203{
Florin Coras15531972018-08-12 23:50:53 -0700204 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
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 Coras288eaab2019-02-03 15:26:14 -0800208 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;
Florin Corasc9940fc2019-02-05 20:55:11 -0800211 app_listener_t *al;
Dave Barach68b0fb02017-02-28 15:15:56 -0500212
Florin Coras15531972018-08-12 23:50:53 -0700213 server = application_get (server_wrk->app_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800214 reg =
215 vl_mem_api_client_index_to_registration (server_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800216 if (!reg)
217 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800218 clib_warning ("no registration: %u", server_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800219 return -1;
220 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500221
Florin Corasb384b542018-01-15 01:08:33 -0800222 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400223 clib_memset (mp, 0, sizeof (*mp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700224
Dave Barach68b0fb02017-02-28 15:15:56 -0500225 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
Florin Coras15531972018-08-12 23:50:53 -0700226 mp->context = server_wrk->wrk_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800227 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
228 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800229
230 if (session_has_transport (s))
231 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700232 listener = listen_session_get (s->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800233 al = app_listener_get (server, listener->al_index);
234 mp->listener_handle = app_listener_handle (al);
Florin Corasf8f516a2018-02-08 15:10:09 -0800235 if (application_is_proxy (server))
236 {
237 listener =
Florin Coras15531972018-08-12 23:50:53 -0700238 app_worker_first_listener (server_wrk, session_get_fib_proto (s),
239 session_get_transport_proto (s));
Florin Corasf8f516a2018-02-08 15:10:09 -0800240 if (listener)
241 mp->listener_handle = listen_session_get_handle (listener);
242 }
243 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
244 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
245 mp->handle = session_handle (s);
Florin Coras1ee78302019-02-05 15:51:15 -0800246 tc = transport_get_connection (session_get_transport_proto (s),
247 s->connection_index, s->thread_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800248 mp->port = tc->rmt_port;
249 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500250 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Corasf8f516a2018-02-08 15:10:09 -0800251 }
252 else
253 {
254 local_session_t *ls = (local_session_t *) s;
255 local_session_t *ll;
256 if (application_local_session_listener_has_transport (ls))
257 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700258 listener = listen_session_get (ls->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800259 al = app_listener_get (server, listener->al_index);
260 mp->listener_handle = app_listener_handle (al);
Florin Coras5fda7a32018-02-14 08:04:31 -0800261 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
Florin Corasf8f516a2018-02-08 15:10:09 -0800262 }
263 else
264 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700265 ll = application_get_local_listen_session (server,
Florin Corasf8f516a2018-02-08 15:10:09 -0800266 ls->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800267 al = app_listener_get (server, ll->al_index);
268 mp->listener_handle = app_listener_handle (al);
Florin Coras5fda7a32018-02-14 08:04:31 -0800269 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
Florin Corasf8f516a2018-02-08 15:10:09 -0800270 }
271 mp->handle = application_local_session_handle (ls);
272 mp->port = ls->port;
273 mp->vpp_event_queue_address = ls->client_evt_q;
274 mp->server_event_queue_address = ls->server_evt_q;
275 }
Florin Corasb384b542018-01-15 01:08:33 -0800276 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500277
278 return 0;
279}
280
281static void
Florin Coras288eaab2019-02-03 15:26:14 -0800282send_session_disconnect_callback (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500283{
Florin Coras15531972018-08-12 23:50:53 -0700284 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Corasb384b542018-01-15 01:08:33 -0800285 vl_api_disconnect_session_t *mp;
286 vl_api_registration_t *reg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500287
Florin Corasc1f5a432018-11-20 11:31:26 -0800288 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800289 if (!reg)
290 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800291 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800292 return;
293 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500294
Florin Corasb384b542018-01-15 01:08:33 -0800295 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400296 clib_memset (mp, 0, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500297 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700298 mp->handle = session_handle (s);
Florin Corasc1f5a432018-11-20 11:31:26 -0800299 mp->context = app_wrk->api_client_index;
Florin Corasb384b542018-01-15 01:08:33 -0800300 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500301}
302
Florin Corasd79b41e2017-03-04 05:37:52 -0800303static void
Florin Coras288eaab2019-02-03 15:26:14 -0800304send_session_reset_callback (session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800305{
Florin Coras15531972018-08-12 23:50:53 -0700306 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Corasb384b542018-01-15 01:08:33 -0800307 vl_api_registration_t *reg;
308 vl_api_reset_session_t *mp;
Florin Corasd79b41e2017-03-04 05:37:52 -0800309
Florin Corasc1f5a432018-11-20 11:31:26 -0800310 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800311 if (!reg)
312 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800313 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800314 return;
315 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800316
Florin Corasb384b542018-01-15 01:08:33 -0800317 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400318 clib_memset (mp, 0, sizeof (*mp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800319 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700320 mp->handle = session_handle (s);
Florin Corasb384b542018-01-15 01:08:33 -0800321 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Florin Corasd79b41e2017-03-04 05:37:52 -0800322}
323
Dave Barach0194f1a2017-05-15 10:11:39 -0400324int
Florin Coras15531972018-08-12 23:50:53 -0700325send_session_connected_callback (u32 app_wrk_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800326 session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500327{
Dave Wallace33e002b2017-09-06 01:20:02 -0400328 vl_api_connect_session_reply_t *mp;
Florin Corasade70e42017-10-14 18:56:41 -0700329 transport_connection_t *tc;
Florin Corasb384b542018-01-15 01:08:33 -0800330 vl_api_registration_t *reg;
Florin Coras3c2fed52018-07-04 04:15:05 -0700331 svm_msg_q_t *vpp_queue;
Florin Coras15531972018-08-12 23:50:53 -0700332 app_worker_t *app_wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500333
Florin Coras15531972018-08-12 23:50:53 -0700334 app_wrk = app_worker_get (app_wrk_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800335 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800336 if (!reg)
337 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800338 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800339 return -1;
340 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500341
Florin Corasb384b542018-01-15 01:08:33 -0800342 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Wallace33e002b2017-09-06 01:20:02 -0400343 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700344 mp->context = api_context;
Dave Wallace965fec92017-10-17 16:19:41 -0400345
346 if (is_fail)
347 goto done;
348
Florin Corasf8f516a2018-02-08 15:10:09 -0800349 if (session_has_transport (s))
Dave Barach68b0fb02017-02-28 15:15:56 -0500350 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800351 tc = session_get_transport (s);
352 if (!tc)
353 {
354 is_fail = 1;
355 goto done;
356 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500357
Florin Corasf8f516a2018-02-08 15:10:09 -0800358 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
359 mp->handle = session_handle (s);
360 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Dave Barach178cf492018-11-13 16:34:13 -0500361 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Corasf8f516a2018-02-08 15:10:09 -0800362 mp->is_ip4 = tc->is_ip4;
363 mp->lcl_port = tc->lcl_port;
Florin Coras288eaab2019-02-03 15:26:14 -0800364 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
365 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800366 }
367 else
368 {
369 local_session_t *ls = (local_session_t *) s;
370 mp->handle = application_local_session_handle (ls);
371 mp->lcl_port = ls->port;
372 mp->vpp_event_queue_address = ls->server_evt_q;
373 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras288eaab2019-02-03 15:26:14 -0800374 mp->server_rx_fifo = pointer_to_uword (s->tx_fifo);
375 mp->server_tx_fifo = pointer_to_uword (s->rx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800376 }
Dave Wallace965fec92017-10-17 16:19:41 -0400377
378done:
379 mp->retval = is_fail ?
380 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
Florin Corasb384b542018-01-15 01:08:33 -0800381 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500382 return 0;
383}
384
Florin Corascea194d2017-10-02 00:18:51 -0700385static session_cb_vft_t session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500386 .session_accept_callback = send_session_accept_callback,
387 .session_disconnect_callback = send_session_disconnect_callback,
388 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800389 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500390 .add_segment_callback = send_add_segment_callback,
Florin Corasf8f516a2018-02-08 15:10:09 -0800391 .del_segment_callback = send_del_segment_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500392};
393
Florin Coras52207f12018-07-12 14:48:06 -0700394static int
Florin Coras83ea6692018-10-12 16:55:14 -0700395mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
396{
397 int rv;
398 u8 try = 0;
399 while (try < 100)
400 {
401 rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
402 SESSION_MQ_CTRL_EVT_RING,
403 SVM_Q_NOWAIT, msg);
404 if (!rv)
405 return 0;
406 try++;
Florin Corase2ea1932018-12-17 23:08:14 -0800407 usleep (1);
Florin Coras83ea6692018-10-12 16:55:14 -0700408 }
Florin Corasdc2e2512018-12-03 17:47:26 -0800409 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700410 return -1;
411}
412
413static int
Florin Coras288eaab2019-02-03 15:26:14 -0800414mq_send_session_accepted_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700415{
Florin Coras15531972018-08-12 23:50:53 -0700416 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700417 svm_msg_q_msg_t _msg, *msg = &_msg;
418 svm_msg_q_t *vpp_queue, *app_mq;
Florin Coras52207f12018-07-12 14:48:06 -0700419 transport_connection_t *tc;
Florin Coras288eaab2019-02-03 15:26:14 -0800420 session_t *listener;
Florin Coras52207f12018-07-12 14:48:06 -0700421 session_accepted_msg_t *mp;
422 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700423 application_t *app;
Florin Corasc9940fc2019-02-05 20:55:11 -0800424 app_listener_t *al;
Florin Coras52207f12018-07-12 14:48:06 -0700425
Florin Coras15531972018-08-12 23:50:53 -0700426 app = application_get (app_wrk->app_index);
427 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700428 if (mq_try_lock_and_alloc_msg (app_mq, msg))
429 return -1;
Florin Coras52207f12018-07-12 14:48:06 -0700430
431 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400432 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700433 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
434 mp = (session_accepted_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800435 clib_memset (mp, 0, sizeof (*mp));
Florin Corasab2f6db2018-08-31 14:31:41 -0700436 mp->context = app->app_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800437 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
438 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800439 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700440
441 if (session_has_transport (s))
442 {
443 listener = listen_session_get (s->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800444 al = app_listener_get (app, listener->al_index);
445 mp->listener_handle = app_listener_handle (al);
Florin Coras52207f12018-07-12 14:48:06 -0700446 if (application_is_proxy (app))
447 {
448 listener =
Florin Coras15531972018-08-12 23:50:53 -0700449 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
450 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700451 if (listener)
452 mp->listener_handle = listen_session_get_handle (listener);
453 }
454 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
455 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
456 mp->handle = session_handle (s);
Florin Coras1ee78302019-02-05 15:51:15 -0800457 tc = transport_get_connection (session_get_transport_proto (s),
458 s->connection_index, s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700459 mp->port = tc->rmt_port;
460 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500461 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700462 }
463 else
464 {
465 local_session_t *ls = (local_session_t *) s;
466 local_session_t *ll;
Florin Coras99368312018-08-02 10:45:44 -0700467 u8 main_thread = vlib_num_workers ()? 1 : 0;
468
Florin Corasc1f5a432018-11-20 11:31:26 -0800469 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700470 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700471 ls->server_evt_q,
472 ls->client_evt_q);
473
Florin Coras52207f12018-07-12 14:48:06 -0700474 if (application_local_session_listener_has_transport (ls))
475 {
476 listener = listen_session_get (ls->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800477 al = app_listener_get (app, listener->al_index);
478 mp->listener_handle = app_listener_handle (al);
Florin Coras52207f12018-07-12 14:48:06 -0700479 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 Corasc9940fc2019-02-05 20:55:11 -0800484 al = app_listener_get (app, ll->al_index);
485 mp->listener_handle = app_listener_handle (al);
Florin Coras52207f12018-07-12 14:48:06 -0700486 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
487 }
488 mp->handle = application_local_session_handle (ls);
489 mp->port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700490 vpp_queue = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700491 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
492 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700493 mp->server_event_queue_address = ls->server_evt_q;
494 }
Florin Coras537b17e2018-09-28 10:35:45 -0700495 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700496
497 return 0;
498}
499
Florin Coras72b04282019-01-14 17:23:11 -0800500static inline void
501mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
502 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700503{
Florin Coras52207f12018-07-12 14:48:06 -0700504 svm_msg_q_msg_t _msg, *msg = &_msg;
505 session_disconnected_msg_t *mp;
506 svm_msg_q_t *app_mq;
507 session_event_t *evt;
508
Florin Coras15531972018-08-12 23:50:53 -0700509 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700510 if (mq_try_lock_and_alloc_msg (app_mq, msg))
511 return;
Florin Coras52207f12018-07-12 14:48:06 -0700512 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400513 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800514 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700515 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800516 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800517 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700518 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700519}
520
Florin Coras72b04282019-01-14 17:23:11 -0800521static inline void
522mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
523 svm_fifo_t * f, session_evt_type_t evt_type)
524{
525 app_worker_t *app_wrk;
526 application_t *app;
527 int i;
528
529 app = application_get (app_index);
530 if (!app)
531 return;
532
533 for (i = 0; i < f->n_subscribers; i++)
534 {
535 if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
536 continue;
537 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
538 }
539}
540
541static void
Florin Coras288eaab2019-02-03 15:26:14 -0800542mq_send_session_disconnected_cb (session_t * s)
Florin Coras72b04282019-01-14 17:23:11 -0800543{
544 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
545 session_handle_t sh = session_handle (s);
546
547 mq_send_session_close_evt (app_wrk, session_handle (s),
548 SESSION_CTRL_EVT_DISCONNECTED);
549
Florin Coras288eaab2019-02-03 15:26:14 -0800550 if (svm_fifo_n_subscribers (s->rx_fifo))
551 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800552 SESSION_CTRL_EVT_DISCONNECTED);
553}
554
Florin Corasaa27eb92018-10-13 12:20:01 -0700555void
556mq_send_local_session_disconnected_cb (u32 app_wrk_index,
557 local_session_t * ls)
558{
559 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
Florin Coras72b04282019-01-14 17:23:11 -0800560 session_handle_t sh = application_local_session_handle (ls);
Florin Corasaa27eb92018-10-13 12:20:01 -0700561
Florin Coras72b04282019-01-14 17:23:11 -0800562 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
563
Florin Coras288eaab2019-02-03 15:26:14 -0800564 if (svm_fifo_n_subscribers (ls->rx_fifo))
565 mq_notify_close_subscribers (app_wrk->app_index, sh, ls->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800566 SESSION_CTRL_EVT_DISCONNECTED);
Florin Corasaa27eb92018-10-13 12:20:01 -0700567}
568
Florin Coras52207f12018-07-12 14:48:06 -0700569static void
Florin Coras288eaab2019-02-03 15:26:14 -0800570mq_send_session_reset_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700571{
Florin Coras72b04282019-01-14 17:23:11 -0800572 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
573 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700574
Florin Coras72b04282019-01-14 17:23:11 -0800575 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
576
Florin Coras288eaab2019-02-03 15:26:14 -0800577 if (svm_fifo_n_subscribers (s->rx_fifo))
578 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800579 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700580}
581
582static int
Florin Coras15531972018-08-12 23:50:53 -0700583mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800584 session_t * s, u8 is_fail)
Florin Coras52207f12018-07-12 14:48:06 -0700585{
586 svm_msg_q_msg_t _msg, *msg = &_msg;
587 session_connected_msg_t *mp;
588 svm_msg_q_t *vpp_mq, *app_mq;
589 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700590 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700591 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700592
Florin Coras15531972018-08-12 23:50:53 -0700593 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700594 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700595 if (!app_mq)
596 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800597 clib_warning ("app %u with api index: %u not attached",
598 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700599 return -1;
600 }
601
Florin Coras83ea6692018-10-12 16:55:14 -0700602 if (mq_try_lock_and_alloc_msg (app_mq, msg))
603 return -1;
Florin Corasdc2e2512018-12-03 17:47:26 -0800604
Florin Coras52207f12018-07-12 14:48:06 -0700605 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400606 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700607 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
608 mp = (session_connected_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800609 clib_memset (mp, 0, sizeof (*mp));
Florin Coras52207f12018-07-12 14:48:06 -0700610 mp->context = api_context;
611
612 if (is_fail)
613 goto done;
614
Florin Corasdc2e2512018-12-03 17:47:26 -0800615 mp->segment_handle = session_segment_handle (s);
616
Florin Coras52207f12018-07-12 14:48:06 -0700617 if (session_has_transport (s))
618 {
619 tc = session_get_transport (s);
620 if (!tc)
621 {
622 is_fail = 1;
623 goto done;
624 }
625
626 vpp_mq = session_manager_get_vpp_event_queue (s->thread_index);
627 mp->handle = session_handle (s);
628 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Dave Barach178cf492018-11-13 16:34:13 -0500629 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700630 mp->is_ip4 = tc->is_ip4;
631 mp->lcl_port = tc->lcl_port;
Florin Coras288eaab2019-02-03 15:26:14 -0800632 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
633 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras52207f12018-07-12 14:48:06 -0700634 }
635 else
636 {
637 local_session_t *ls = (local_session_t *) s;
Florin Coras99368312018-08-02 10:45:44 -0700638 u8 main_thread = vlib_num_workers ()? 1 : 0;
639
Florin Corasc1f5a432018-11-20 11:31:26 -0800640 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700641 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700642 ls->client_evt_q,
643 ls->server_evt_q);
644
Florin Coras52207f12018-07-12 14:48:06 -0700645 mp->handle = application_local_session_handle (ls);
646 mp->lcl_port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700647 vpp_mq = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700648 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras52207f12018-07-12 14:48:06 -0700649 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700650 mp->server_event_queue_address = ls->server_evt_q;
Florin Coras288eaab2019-02-03 15:26:14 -0800651 mp->server_rx_fifo = pointer_to_uword (s->tx_fifo);
652 mp->server_tx_fifo = pointer_to_uword (s->rx_fifo);
Florin Coras52207f12018-07-12 14:48:06 -0700653 }
654
655done:
656 mp->retval = is_fail ?
657 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
658
Florin Coras537b17e2018-09-28 10:35:45 -0700659 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700660 return 0;
661}
662
Florin Coras60116992018-08-27 09:52:18 -0700663static int
664mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
665 session_handle_t handle, int rv)
666{
667 svm_msg_q_msg_t _msg, *msg = &_msg;
668 svm_msg_q_t *app_mq, *vpp_evt_q;
669 transport_connection_t *tc;
Florin Coras60116992018-08-27 09:52:18 -0700670 session_bound_msg_t *mp;
671 app_worker_t *app_wrk;
672 session_event_t *evt;
673 application_t *app;
Florin Corasc9940fc2019-02-05 20:55:11 -0800674 app_listener_t *al;
675 session_t *ls = 0;
Florin Coras60116992018-08-27 09:52:18 -0700676
677 app_wrk = app_worker_get (app_wrk_index);
678 app = application_get (app_wrk->app_index);
679 app_mq = app_wrk->event_queue;
680 if (!app_mq)
681 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800682 clib_warning ("app %u with api index: %u not attached",
683 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700684 return -1;
685 }
686
Florin Coras83ea6692018-10-12 16:55:14 -0700687 if (mq_try_lock_and_alloc_msg (app_mq, msg))
688 return -1;
689
Florin Coras60116992018-08-27 09:52:18 -0700690 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400691 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700692 evt->event_type = SESSION_CTRL_EVT_BOUND;
693 mp = (session_bound_msg_t *) evt->data;
694 mp->context = api_context;
695
696 if (rv)
697 goto done;
698
699 mp->handle = handle;
700 if (application_has_global_scope (app))
701 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800702 al = app_listener_get_w_handle (handle);
703 ls = app_listener_get_session (al);
Florin Coras60116992018-08-27 09:52:18 -0700704 tc = listen_session_get_transport (ls);
705 mp->lcl_port = tc->lcl_port;
706 mp->lcl_is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500707 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras60116992018-08-27 09:52:18 -0700708 }
709 else
710 {
711 local_session_t *local;
Florin Corasab2f6db2018-08-31 14:31:41 -0700712 local = application_get_local_listener_w_handle (handle);
Florin Coras60116992018-08-27 09:52:18 -0700713 mp->lcl_port = local->port;
714 mp->lcl_is_ip4 = session_type_is_ip4 (local->session_type);
715 }
716
Florin Coras5f45e012019-01-23 09:21:30 -0800717 vpp_evt_q = session_manager_get_vpp_event_queue (0);
718 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
719
Florin Coras60116992018-08-27 09:52:18 -0700720 if (ls && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
721 {
Florin Coras288eaab2019-02-03 15:26:14 -0800722 mp->rx_fifo = pointer_to_uword (ls->rx_fifo);
723 mp->tx_fifo = pointer_to_uword (ls->tx_fifo);
Florin Coras60116992018-08-27 09:52:18 -0700724 }
725
726done:
727 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700728 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700729 return 0;
730}
731
Florin Coras52207f12018-07-12 14:48:06 -0700732static session_cb_vft_t session_mq_cb_vft = {
733 .session_accept_callback = mq_send_session_accepted_cb,
734 .session_disconnect_callback = mq_send_session_disconnected_cb,
735 .session_connected_callback = mq_send_session_connected_cb,
736 .session_reset_callback = mq_send_session_reset_cb,
737 .add_segment_callback = send_add_segment_callback,
738 .del_segment_callback = send_del_segment_callback,
739};
740
Dave Barach68b0fb02017-02-28 15:15:56 -0500741static void
Florin Corase04c2992017-03-01 08:17:34 -0800742vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
743{
744 vl_api_session_enable_disable_reply_t *rmp;
745 vlib_main_t *vm = vlib_get_main ();
746 int rv = 0;
747
748 vnet_session_enable_disable (vm, mp->is_enable);
749 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
750}
751
752static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700753vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500754{
Florin Coras99368312018-08-02 10:45:44 -0700755 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700756 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800757 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700758 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800759 vl_api_registration_t *reg;
Florin Coras99368312018-08-02 10:45:44 -0700760 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500761
Florin Corasfc804d92018-01-26 01:27:01 -0800762 reg = vl_api_client_index_to_registration (mp->client_index);
763 if (!reg)
764 return;
765
Florin Coras6cf30ad2017-04-04 23:08:23 -0700766 if (session_manager_is_enabled () == 0)
767 {
768 rv = VNET_API_ERROR_FEATURE_DISABLED;
769 goto done;
770 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500771
Florin Corasff6e7692017-12-11 04:59:01 -0800772 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700773 sizeof (mp->options),
774 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500775
Dave Barachb7b92992018-10-17 10:38:51 -0400776 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500777 a->api_client_index = mp->client_index;
778 a->options = mp->options;
Florin Coras52207f12018-07-12 14:48:06 -0700779
780 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
781 a->session_cb_vft = &session_mq_cb_vft;
782 else
783 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500784
Florin Corascea194d2017-10-02 00:18:51 -0700785 if (mp->namespace_id_len > 64)
786 {
787 rv = VNET_API_ERROR_INVALID_VALUE;
788 goto done;
789 }
790
791 if (mp->namespace_id_len)
792 {
Dave Wallace8af20542017-10-26 03:29:30 -0400793 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500794 clib_memcpy_fast (a->namespace_id, mp->namespace_id,
795 mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -0700796 }
797
Florin Corasc1a42652019-02-08 18:27:29 -0800798 if ((rv = vnet_application_attach (a)))
Florin Corascea194d2017-10-02 00:18:51 -0700799 {
Florin Corasc1a42652019-02-08 18:27:29 -0800800 clib_warning ("attach returned: %d", rv);
Florin Coras99368312018-08-02 10:45:44 -0700801 vec_free (a->namespace_id);
802 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700803 }
804 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500805
Florin Coras99368312018-08-02 10:45:44 -0700806 /* Send event queues segment */
807 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
808 {
809 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
810 fds[n_fds] = evt_q_segment->fd;
811 n_fds += 1;
812 }
813 /* Send fifo segment fd if needed */
814 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
815 {
816 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
817 fds[n_fds] = a->segment->fd;
818 n_fds += 1;
819 }
820 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
821 {
822 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
823 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
824 n_fds += 1;
825 }
826
Florin Coras6cf30ad2017-04-04 23:08:23 -0700827done:
Florin Corasa5464812017-04-19 13:00:05 -0700828
Dave Barach68b0fb02017-02-28 15:15:56 -0500829 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700830 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500831 if (!rv)
832 {
Florin Corasb384b542018-01-15 01:08:33 -0800833 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800834 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500835 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800836 rmp->segment_size = segp->ssvm_size;
837 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500838 {
Florin Corasb384b542018-01-15 01:08:33 -0800839 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
840 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500841 }
Florin Coras99368312018-08-02 10:45:44 -0700842 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
843 rmp->n_fds = n_fds;
844 rmp->fd_flags = fd_flags;
Florin Corasd85de682018-11-29 17:02:29 -0800845 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500846 }
847 }));
848 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800849
Florin Coras99368312018-08-02 10:45:44 -0700850 if (n_fds)
851 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500852}
853
854static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700855vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
856{
857 vl_api_application_detach_reply_t *rmp;
858 int rv = VNET_API_ERROR_INVALID_VALUE_2;
859 vnet_app_detach_args_t _a, *a = &_a;
860 application_t *app;
861
862 if (session_manager_is_enabled () == 0)
863 {
864 rv = VNET_API_ERROR_FEATURE_DISABLED;
865 goto done;
866 }
867
868 app = application_lookup (mp->client_index);
869 if (app)
870 {
Florin Coras15531972018-08-12 23:50:53 -0700871 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800872 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700873 rv = vnet_application_detach (a);
874 }
875
876done:
877 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
878}
879
880static void
881vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
882{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700883 transport_connection_t *tc = 0;
Florin Corasc9940fc2019-02-05 20:55:11 -0800884 vnet_listen_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700885 vl_api_bind_uri_reply_t *rmp;
Florin Coras288eaab2019-02-03 15:26:14 -0800886 session_t *s;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700887 application_t *app = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700888 svm_msg_q_t *vpp_evt_q;
Florin Coras60116992018-08-27 09:52:18 -0700889 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700890 int rv;
891
892 if (session_manager_is_enabled () == 0)
893 {
894 rv = VNET_API_ERROR_FEATURE_DISABLED;
895 goto done;
896 }
897
898 app = application_lookup (mp->client_index);
899 if (app)
900 {
Dave Barachb7b92992018-10-17 10:38:51 -0400901 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700902 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700903 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700904 rv = vnet_bind_uri (a);
905 }
906 else
907 {
908 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
909 }
910
911done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700912
913 /* *INDENT-OFF* */
914 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
915 if (!rv)
916 {
917 rmp->handle = a->handle;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700918 if (app && application_has_global_scope (app))
919 {
920 s = listen_session_get_from_handle (a->handle);
921 tc = listen_session_get_transport (s);
Florin Coras6c354942018-04-18 00:05:21 -0700922 rmp->lcl_is_ip4 = tc->is_ip4;
923 rmp->lcl_port = tc->lcl_port;
Dave Barach178cf492018-11-13 16:34:13 -0500924 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700925 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
926 {
Florin Coras288eaab2019-02-03 15:26:14 -0800927 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
928 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700929 vpp_evt_q = session_manager_get_vpp_event_queue (0);
930 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
931 }
932 }
933 }
934 }));
935 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -0700936
937 /* If app uses mq for control messages, send an mq message as well */
938 if (app && application_use_mq_for_ctrl (app))
939 {
940 app_wrk = application_get_worker (app, 0);
941 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
942 rv);
943 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700944}
945
946static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500947vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
948{
949 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700950 application_t *app;
Florin Corasc1a42652019-02-08 18:27:29 -0800951 vnet_unlisten_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500952 int rv;
953
Florin Coras6cf30ad2017-04-04 23:08:23 -0700954 if (session_manager_is_enabled () == 0)
955 {
956 rv = VNET_API_ERROR_FEATURE_DISABLED;
957 goto done;
958 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500959
Florin Coras6cf30ad2017-04-04 23:08:23 -0700960 app = application_lookup (mp->client_index);
961 if (app)
962 {
963 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700964 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700965 rv = vnet_unbind_uri (a);
966 }
967 else
968 {
969 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
970 }
971
972done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500973 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
974}
975
Florin Corasf03a59a2017-06-09 21:07:32 -0700976static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500977vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
978{
Dave Wallace33e002b2017-09-06 01:20:02 -0400979 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500980 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700981 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700982 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500983
Florin Coras6cf30ad2017-04-04 23:08:23 -0700984 if (session_manager_is_enabled () == 0)
985 {
986 rv = VNET_API_ERROR_FEATURE_DISABLED;
987 goto done;
988 }
Florin Corase04c2992017-03-01 08:17:34 -0800989
Florin Coras6cf30ad2017-04-04 23:08:23 -0700990 app = application_lookup (mp->client_index);
991 if (app)
992 {
Dave Barachb7b92992018-10-17 10:38:51 -0400993 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700994 a->uri = (char *) mp->uri;
995 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -0700996 a->app_index = app->app_index;
Florin Corasc1a42652019-02-08 18:27:29 -0800997 if ((rv = vnet_connect_uri (a)))
998 clib_warning ("connect_uri returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700999 }
1000 else
1001 {
1002 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1003 }
Florin Corase04c2992017-03-01 08:17:34 -08001004
Florin Coras3cbc04b2017-10-02 00:18:51 -07001005 /*
1006 * Don't reply to stream (tcp) connects. The reply will come once
1007 * the connection is established. In case of the redirects, the reply
1008 * will come from the server app.
1009 */
Florin Coras8f89dd02018-03-05 16:53:07 -08001010 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001011 return;
1012
Florin Coras6cf30ad2017-04-04 23:08:23 -07001013done:
Florin Corase04c2992017-03-01 08:17:34 -08001014 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -04001015 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -08001016 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001017}
1018
1019static void
1020vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
1021{
1022 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001023 vnet_disconnect_args_t _a, *a = &_a;
1024 application_t *app;
1025 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001026
Florin Coras6cf30ad2017-04-04 23:08:23 -07001027 if (session_manager_is_enabled () == 0)
1028 {
1029 rv = VNET_API_ERROR_FEATURE_DISABLED;
1030 goto done;
1031 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001032
Florin Coras6cf30ad2017-04-04 23:08:23 -07001033 app = application_lookup (mp->client_index);
1034 if (app)
1035 {
1036 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001037 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001038 rv = vnet_disconnect_session (a);
1039 }
1040 else
1041 {
1042 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1043 }
1044
1045done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001046 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001047}
1048
1049static void
1050vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1051 mp)
1052{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001053 vnet_disconnect_args_t _a, *a = &_a;
1054 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001055
1056 /* Client objected to disconnecting the session, log and continue */
1057 if (mp->retval)
1058 {
1059 clib_warning ("client retval %d", mp->retval);
1060 return;
1061 }
1062
1063 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001064 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001065 if (app)
1066 {
1067 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001068 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001069 vnet_disconnect_session (a);
1070 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001071}
1072
1073static void
1074vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
1075{
Florin Coras4af830c2018-12-04 09:21:36 -08001076 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras15531972018-08-12 23:50:53 -07001077 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001078 application_t *app;
Florin Coras288eaab2019-02-03 15:26:14 -08001079 session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001080 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05001081
Ondrej Fabry6bd197e2018-08-15 08:46:46 +02001082 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001083 if (!app)
1084 return;
1085
Florin Corascea194d2017-10-02 00:18:51 -07001086 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001087 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -07001088 if (!s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001089 {
1090 clib_warning ("Invalid session!");
1091 return;
1092 }
1093
Florin Coras15531972018-08-12 23:50:53 -07001094 app_wrk = app_worker_get (s->app_wrk_index);
1095 if (app_wrk->app_index != app->app_index)
1096 {
1097 clib_warning ("app %u does not own handle 0x%lx", app->app_index,
1098 mp->handle);
1099 return;
1100 }
1101
Dave Barach68b0fb02017-02-28 15:15:56 -05001102 /* Client objected to resetting the session, log and continue */
1103 if (mp->retval)
1104 {
1105 clib_warning ("client retval %d", mp->retval);
1106 return;
1107 }
1108
Dave Barach68b0fb02017-02-28 15:15:56 -05001109 /* This comes as a response to a reset, transport only waiting for
1110 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -08001111 a->handle = mp->handle;
1112 a->app_index = app->app_index;
1113 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -05001114}
1115
1116static void
1117vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
1118{
Florin Corasf8f516a2018-02-08 15:10:09 -08001119 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
1120 local_session_t *ls;
Florin Coras288eaab2019-02-03 15:26:14 -08001121 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001122
Florin Corasa5464812017-04-19 13:00:05 -07001123 /* Server isn't interested, kill the session */
1124 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -05001125 {
Florin Corasa5464812017-04-19 13:00:05 -07001126 a->app_index = mp->context;
1127 a->handle = mp->handle;
1128 vnet_disconnect_session (a);
Florin Corasf8f516a2018-02-08 15:10:09 -08001129 return;
1130 }
1131
1132 if (session_handle_is_local (mp->handle))
1133 {
Florin Coras623eb562019-02-03 19:28:34 -08001134 ls = app_worker_get_local_session_from_handle (mp->handle);
Florin Coras15531972018-08-12 23:50:53 -07001135 if (!ls || ls->app_wrk_index != mp->context)
Florin Corasf8f516a2018-02-08 15:10:09 -08001136 {
1137 clib_warning ("server %u doesn't own local handle %llu",
1138 mp->context, mp->handle);
1139 return;
1140 }
Florin Coras623eb562019-02-03 19:28:34 -08001141 if (app_worker_local_session_connect_notify (ls))
Florin Corasf8f516a2018-02-08 15:10:09 -08001142 return;
1143 ls->session_state = SESSION_STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001144 }
Florin Corasa5464812017-04-19 13:00:05 -07001145 else
1146 {
Florin Corasf8f516a2018-02-08 15:10:09 -08001147 s = session_get_from_handle_if_valid (mp->handle);
Florin Corasa5464812017-04-19 13:00:05 -07001148 if (!s)
1149 {
1150 clib_warning ("session doesn't exist");
1151 return;
1152 }
Florin Coras15531972018-08-12 23:50:53 -07001153 if (s->app_wrk_index != mp->context)
Florin Corasa5464812017-04-19 13:00:05 -07001154 {
1155 clib_warning ("app doesn't own session");
1156 return;
1157 }
Florin Corasa5464812017-04-19 13:00:05 -07001158 s->session_state = SESSION_STATE_READY;
1159 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001160}
1161
1162static void
1163vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1164 * mp)
1165{
1166 clib_warning ("not implemented");
1167}
1168
1169static void
1170vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1171{
Florin Corasc9940fc2019-02-05 20:55:11 -08001172 vnet_listen_args_t _a, *a = &_a;
1173 transport_connection_t *tc = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001174 vl_api_bind_sock_reply_t *rmp;
Florin Corasc9940fc2019-02-05 20:55:11 -08001175 svm_msg_q_t *vpp_evt_q;
Florin Coraseb2945c2017-11-22 10:39:09 -08001176 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001177 app_worker_t *app_wrk;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001178 ip46_address_t *ip46;
Florin Corasc9940fc2019-02-05 20:55:11 -08001179 app_listener_t *al;
1180 session_t *s;
1181 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001182
Florin Coras6cf30ad2017-04-04 23:08:23 -07001183 if (session_manager_is_enabled () == 0)
1184 {
1185 rv = VNET_API_ERROR_FEATURE_DISABLED;
1186 goto done;
1187 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001188
Florin Coras6cf30ad2017-04-04 23:08:23 -07001189 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001190 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001191 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001192 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1193 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001194 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001195
1196 ip46 = (ip46_address_t *) mp->ip;
Dave Barachb7b92992018-10-17 10:38:51 -04001197 clib_memset (a, 0, sizeof (*a));
Florin Corasdcf55ce2017-11-16 15:32:50 -08001198 a->sep.is_ip4 = mp->is_ip4;
1199 a->sep.ip = *ip46;
1200 a->sep.port = mp->port;
1201 a->sep.fib_index = mp->vrf;
1202 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1203 a->sep.transport_proto = mp->proto;
Florin Coras15531972018-08-12 23:50:53 -07001204 a->app_index = app->app_index;
1205 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001206
Florin Corasc1a42652019-02-08 18:27:29 -08001207 if ((rv = vnet_listen (a)))
1208 clib_warning ("listen returned: %d", rv);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001209
Florin Coras6cf30ad2017-04-04 23:08:23 -07001210done:
Florin Corascea194d2017-10-02 00:18:51 -07001211 /* *INDENT-OFF* */
1212 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1213 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -08001214 {
1215 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -08001216 rmp->lcl_port = mp->port;
Keith Burns (alagalah)60ae80f2018-03-15 10:04:20 -07001217 rmp->lcl_is_ip4 = mp->is_ip4;
Dave Wallace97b1a272017-12-18 13:51:59 -05001218 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -08001219 {
Florin Corasc9940fc2019-02-05 20:55:11 -08001220 al = app_listener_get_w_handle (a->handle);
1221 s = app_listener_get_session (al);
Florin Coraseb2945c2017-11-22 10:39:09 -08001222 tc = listen_session_get_transport (s);
Dave Barach178cf492018-11-13 16:34:13 -05001223 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001224 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1225 {
Florin Coras288eaab2019-02-03 15:26:14 -08001226 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
1227 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001228 vpp_evt_q = session_manager_get_vpp_event_queue (0);
1229 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1230 }
Florin Coraseb2945c2017-11-22 10:39:09 -08001231 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001232 }
Florin Corascea194d2017-10-02 00:18:51 -07001233 }));
1234 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -07001235
1236 /* If app uses mq for control messages, send an mq message as well */
1237 if (app && application_use_mq_for_ctrl (app))
1238 {
1239 app_wrk = application_get_worker (app, mp->wrk_index);
1240 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1241 rv);
1242 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001243}
1244
1245static void
1246vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1247{
1248 vl_api_unbind_sock_reply_t *rmp;
Florin Corasc1a42652019-02-08 18:27:29 -08001249 vnet_unlisten_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001250 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -07001251 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001252
Florin Coras6cf30ad2017-04-04 23:08:23 -07001253 if (session_manager_is_enabled () == 0)
1254 {
1255 rv = VNET_API_ERROR_FEATURE_DISABLED;
1256 goto done;
1257 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001258
Florin Coras6cf30ad2017-04-04 23:08:23 -07001259 app = application_lookup (mp->client_index);
1260 if (app)
1261 {
Florin Coras15531972018-08-12 23:50:53 -07001262 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001263 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001264 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001265 if ((rv = vnet_unlisten (a)))
1266 clib_warning ("unlisten returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001267 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001268
Florin Coras6cf30ad2017-04-04 23:08:23 -07001269done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001270 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
1271}
1272
1273static void
1274vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1275{
Dave Wallace33e002b2017-09-06 01:20:02 -04001276 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001277 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001278 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001279 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001280
Florin Coras6cf30ad2017-04-04 23:08:23 -07001281 if (session_manager_is_enabled () == 0)
1282 {
1283 rv = VNET_API_ERROR_FEATURE_DISABLED;
1284 goto done;
1285 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001286
Florin Coras6cf30ad2017-04-04 23:08:23 -07001287 app = application_lookup (mp->client_index);
1288 if (app)
1289 {
Florin Corase86a8ed2018-01-05 03:20:25 -08001290 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -04001291 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001292
Dave Barachb7b92992018-10-17 10:38:51 -04001293 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001294 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1295 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -07001296 a->sep.is_ip4 = mp->is_ip4;
1297 a->sep.ip = *ip46;
1298 a->sep.port = mp->port;
1299 a->sep.transport_proto = mp->proto;
Florin Coras5665ced2018-10-25 18:03:45 -07001300 a->sep.peer.fib_index = mp->vrf;
1301 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras8f89dd02018-03-05 16:53:07 -08001302 if (mp->hostname_len)
1303 {
Florin Coras15531972018-08-12 23:50:53 -07001304 vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001305 clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
1306 mp->hostname_len);
Florin Coras8f89dd02018-03-05 16:53:07 -08001307 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001308 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001309 a->app_index = app->app_index;
1310 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001311 if ((rv = vnet_connect (a)))
1312 clib_warning ("connect returned: %u", rv);
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;
Florin Coras15531972018-08-12 23:50:53 -07001341 application_t *app;
1342 u8 fd_flags = 0;
1343
1344 if (!session_manager_is_enabled ())
1345 {
1346 rv = VNET_API_ERROR_FEATURE_DISABLED;
1347 goto done;
1348 }
1349
1350 reg = vl_api_client_index_to_registration (mp->client_index);
1351 if (!reg)
1352 return;
1353
Florin Corasc1f5a432018-11-20 11:31:26 -08001354 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001355 if (!app)
1356 {
1357 rv = VNET_API_ERROR_INVALID_VALUE;
1358 goto done;
1359 }
1360
1361 vnet_app_worker_add_del_args_t args = {
1362 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001363 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001364 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001365 .is_add = mp->is_add
1366 };
Florin Corasc1a42652019-02-08 18:27:29 -08001367 rv = vnet_app_worker_add_del (&args);
1368 if (rv)
Florin Coras15531972018-08-12 23:50:53 -07001369 {
Florin Corasc1a42652019-02-08 18:27:29 -08001370 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -07001371 goto done;
1372 }
1373
Florin Coras14598772018-09-04 19:47:52 -07001374 if (!mp->is_add)
1375 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001376
Florin Coras15531972018-08-12 23:50:53 -07001377 /* Send fifo segment fd if needed */
1378 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1379 {
1380 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1381 fds[n_fds] = args.segment->fd;
1382 n_fds += 1;
1383 }
1384 if (application_segment_manager_properties (app)->use_mq_eventfd)
1385 {
1386 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1387 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1388 n_fds += 1;
1389 }
1390
1391 /* *INDENT-OFF* */
1392done:
1393 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1394 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001395 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001396 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001397 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001398 {
Florin Coras15531972018-08-12 23:50:53 -07001399 if (vec_len (args.segment->name))
1400 {
1401 memcpy (rmp->segment_name, args.segment->name,
1402 vec_len (args.segment->name));
1403 rmp->segment_name_length = vec_len (args.segment->name);
1404 }
1405 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1406 rmp->n_fds = n_fds;
1407 rmp->fd_flags = fd_flags;
1408 }
1409 }));
1410 /* *INDENT-ON* */
1411
1412 if (n_fds)
1413 session_send_fds (reg, fds, n_fds);
1414}
1415
1416static void
Florin Corascea194d2017-10-02 00:18:51 -07001417vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1418{
1419 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -08001420 u32 appns_index = 0;
1421 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001422 int rv = 0;
1423 if (!session_manager_is_enabled ())
1424 {
1425 rv = VNET_API_ERROR_FEATURE_DISABLED;
1426 goto done;
1427 }
1428
1429 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1430 {
1431 rv = VNET_API_ERROR_INVALID_VALUE;
1432 goto done;
1433 }
1434
1435 vec_validate (ns_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001436 clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -07001437 vnet_app_namespace_add_del_args_t args = {
1438 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001439 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001440 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1441 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1442 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1443 .is_add = 1
1444 };
Florin Corasc1a42652019-02-08 18:27:29 -08001445 rv = vnet_app_namespace_add_del (&args);
1446 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -08001447 {
1448 appns_index = app_namespace_index_from_id (ns_id);
1449 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1450 {
1451 clib_warning ("app ns lookup failed");
1452 rv = VNET_API_ERROR_UNSPECIFIED;
1453 }
1454 }
Florin Corascea194d2017-10-02 00:18:51 -07001455 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -08001456
1457 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -07001458done:
Florin Coras6e8c6672017-11-10 09:03:54 -08001459 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1460 if (!rv)
1461 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1462 }));
1463 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -07001464}
1465
Florin Coras1c710452017-10-17 00:03:13 -07001466static void
1467vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1468{
1469 vl_api_session_rule_add_del_reply_t *rmp;
1470 session_rule_add_del_args_t args;
1471 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -07001472 u8 fib_proto;
1473 int rv = 0;
1474
Dave Barachb7b92992018-10-17 10:38:51 -04001475 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001476 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1477
1478 table_args->lcl.fp_len = mp->lcl_plen;
1479 table_args->lcl.fp_proto = fib_proto;
1480 table_args->rmt.fp_len = mp->rmt_plen;
1481 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001482 table_args->lcl_port = mp->lcl_port;
1483 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001484 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1485 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001486 mp->tag[sizeof (mp->tag) - 1] = 0;
1487 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001488 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1489 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -08001490 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -07001491
Dave Barachb7b92992018-10-17 10:38:51 -04001492 clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1493 clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
Florin Coras1c710452017-10-17 00:03:13 -07001494 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1495 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
Florin Corasc1a42652019-02-08 18:27:29 -08001496 rv = vnet_session_rule_add_del (&args);
1497 if (rv)
1498 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -08001499 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001500 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1501}
1502
Florin Coras6c36f532017-11-03 18:32:34 -07001503static void
1504send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001505 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001506 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001507{
1508 vl_api_session_rules_details_t *rmp = 0;
1509 session_mask_or_match_4_t *match =
1510 (session_mask_or_match_4_t *) & rule->match;
1511 session_mask_or_match_4_t *mask =
1512 (session_mask_or_match_4_t *) & rule->mask;
1513
1514 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001515 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001516 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1517 rmp->context = context;
1518
1519 rmp->is_ip4 = 1;
Dave Barach178cf492018-11-13 16:34:13 -05001520 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1521 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001522 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1523 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001524 rmp->lcl_port = match->lcl_port;
1525 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001526 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1527 rmp->scope =
1528 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1529 rmp->transport_proto = transport_proto;
1530 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001531 if (tag)
1532 {
Dave Barach178cf492018-11-13 16:34:13 -05001533 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001534 rmp->tag[vec_len (tag)] = 0;
1535 }
Florin Coras6c36f532017-11-03 18:32:34 -07001536
Florin Coras6c4dae22018-01-09 06:39:23 -08001537 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001538}
1539
1540static void
Florin Corasc97a7392017-11-05 23:07:07 -08001541send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1542 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001543 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001544{
1545 vl_api_session_rules_details_t *rmp = 0;
1546 session_mask_or_match_6_t *match =
1547 (session_mask_or_match_6_t *) & rule->match;
1548 session_mask_or_match_6_t *mask =
1549 (session_mask_or_match_6_t *) & rule->mask;
1550
1551 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001552 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001553 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1554 rmp->context = context;
1555
1556 rmp->is_ip4 = 0;
Dave Barach178cf492018-11-13 16:34:13 -05001557 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1558 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001559 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1560 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001561 rmp->lcl_port = match->lcl_port;
1562 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001563 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001564 rmp->scope =
1565 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -07001566 rmp->transport_proto = transport_proto;
1567 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001568 if (tag)
1569 {
Dave Barach178cf492018-11-13 16:34:13 -05001570 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001571 rmp->tag[vec_len (tag)] = 0;
1572 }
Florin Coras6c36f532017-11-03 18:32:34 -07001573
Florin Coras6c4dae22018-01-09 06:39:23 -08001574 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001575}
1576
1577static void
1578send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001579 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001580 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001581{
1582 mma_rule_16_t *rule16;
1583 mma_rule_40_t *rule40;
1584 mma_rules_table_16_t *srt16;
1585 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001586 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001587
Florin Corasc97a7392017-11-05 23:07:07 -08001588 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001589 {
Florin Corasc97a7392017-11-05 23:07:07 -08001590 u8 *tag = 0;
1591 /* *INDENT-OFF* */
1592 srt16 = &srt->session_rules_tables_16;
1593 pool_foreach (rule16, srt16->rules, ({
1594 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1595 tag = session_rules_table_rule_tag (srt, ri, 1);
1596 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001597 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001598 }));
1599 /* *INDENT-ON* */
1600 }
1601 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1602 {
1603 u8 *tag = 0;
1604 /* *INDENT-OFF* */
1605 srt40 = &srt->session_rules_tables_40;
1606 pool_foreach (rule40, srt40->rules, ({
1607 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1608 tag = session_rules_table_rule_tag (srt, ri, 1);
1609 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001610 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001611 }));
1612 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001613 }
1614}
1615
1616static void
1617vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1618{
Florin Coras6c4dae22018-01-09 06:39:23 -08001619 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001620 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001621 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001622
Florin Coras6c4dae22018-01-09 06:39:23 -08001623 reg = vl_api_client_index_to_registration (mp->client_index);
1624 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001625 return;
1626
1627 /* *INDENT-OFF* */
1628 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001629 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1630 {
1631 send_session_rules_table_details (&st->session_rules[tp],
1632 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001633 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001634 mp->context);
1635 }
Florin Coras6c36f532017-11-03 18:32:34 -07001636 }));
1637 /* *INDENT-ON* */
1638}
1639
Florin Coras371ca502018-02-21 12:07:41 -08001640static void
1641vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1642 mp)
1643{
1644 vl_api_app_namespace_add_del_reply_t *rmp;
1645 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1646 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001647 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001648 u32 cert_len;
1649 int rv = 0;
1650 if (!session_manager_is_enabled ())
1651 {
1652 rv = VNET_API_ERROR_FEATURE_DISABLED;
1653 goto done;
1654 }
Florin Corase3e2f072018-03-04 07:24:30 -08001655 if (!(app = application_lookup (mp->client_index)))
1656 {
1657 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1658 goto done;
1659 }
Dave Barachb7b92992018-10-17 10:38:51 -04001660 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001661 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001662 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001663 if (cert_len > 10000)
1664 {
1665 rv = VNET_API_ERROR_INVALID_VALUE;
1666 goto done;
1667 }
Florin Coras371ca502018-02-21 12:07:41 -08001668 vec_validate (a->cert, cert_len);
Dave Barach178cf492018-11-13 16:34:13 -05001669 clib_memcpy_fast (a->cert, mp->cert, cert_len);
Florin Coras371ca502018-02-21 12:07:41 -08001670 if ((error = vnet_app_add_tls_cert (a)))
1671 {
1672 rv = clib_error_get_code (error);
1673 clib_error_report (error);
1674 }
1675 vec_free (a->cert);
1676done:
1677 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1678}
1679
1680static void
1681vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1682 mp)
1683{
1684 vl_api_app_namespace_add_del_reply_t *rmp;
1685 vnet_app_add_tls_key_args_t _a, *a = &_a;
1686 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001687 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001688 u32 key_len;
1689 int rv = 0;
1690 if (!session_manager_is_enabled ())
1691 {
1692 rv = VNET_API_ERROR_FEATURE_DISABLED;
1693 goto done;
1694 }
Florin Corase3e2f072018-03-04 07:24:30 -08001695 if (!(app = application_lookup (mp->client_index)))
1696 {
1697 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1698 goto done;
1699 }
Dave Barachb7b92992018-10-17 10:38:51 -04001700 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001701 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001702 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001703 if (key_len > 10000)
1704 {
1705 rv = VNET_API_ERROR_INVALID_VALUE;
1706 goto done;
1707 }
Florin Coras371ca502018-02-21 12:07:41 -08001708 vec_validate (a->key, key_len);
Dave Barach178cf492018-11-13 16:34:13 -05001709 clib_memcpy_fast (a->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001710 if ((error = vnet_app_add_tls_key (a)))
1711 {
1712 rv = clib_error_get_code (error);
1713 clib_error_report (error);
1714 }
1715 vec_free (a->key);
1716done:
1717 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1718}
1719
Florin Coras6cf30ad2017-04-04 23:08:23 -07001720static clib_error_t *
1721application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001722{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001723 application_t *app = application_lookup (client_index);
1724 vnet_app_detach_args_t _a, *a = &_a;
1725 if (app)
1726 {
Florin Coras15531972018-08-12 23:50:53 -07001727 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001728 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001729 vnet_application_detach (a);
1730 }
1731 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001732}
1733
Florin Coras6cf30ad2017-04-04 23:08:23 -07001734VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001735
1736#define vl_msg_name_crc_list
1737#include <vnet/vnet_all_api_h.h>
1738#undef vl_msg_name_crc_list
1739
1740static void
1741setup_message_id_table (api_main_t * am)
1742{
1743#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1744 foreach_vl_msg_name_crc_session;
1745#undef _
1746}
1747
1748/*
1749 * session_api_hookup
1750 * Add uri's API message handlers to the table.
1751 * vlib has alread mapped shared memory and
1752 * added the client registration handlers.
1753 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1754 */
1755static clib_error_t *
1756session_api_hookup (vlib_main_t * vm)
1757{
1758 api_main_t *am = &api_main;
1759
1760#define _(N,n) \
1761 vl_msg_api_set_handlers(VL_API_##N, #n, \
1762 vl_api_##n##_t_handler, \
1763 vl_noop_handler, \
1764 vl_api_##n##_t_endian, \
1765 vl_api_##n##_t_print, \
1766 sizeof(vl_api_##n##_t), 1);
1767 foreach_session_api_msg;
1768#undef _
1769
1770 /*
1771 * Messages which bounce off the data-plane to
1772 * an API client. Simply tells the message handling infra not
1773 * to free the message.
1774 *
1775 * Bounced message handlers MUST NOT block the data plane
1776 */
1777 am->message_bounce[VL_API_CONNECT_URI] = 1;
1778 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1779
1780 /*
1781 * Set up the (msg_name, crc, message-id) table
1782 */
1783 setup_message_id_table (am);
1784
1785 return 0;
1786}
1787
1788VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001789
Dave Barach68b0fb02017-02-28 15:15:56 -05001790/*
1791 * fd.io coding-style-patch-verification: ON
1792 *
1793 * Local Variables:
1794 * eval: (c-set-style "gnu")
1795 * End:
1796 */