blob: 578078745896a1a894e8076e4a43f3dbd2a91320 [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>
Florin Corasba7d8f52019-02-22 13:11:38 -080020#include <vnet/session/application_local.h>
Florin Coras1c710452017-10-17 00:03:13 -070021#include <vnet/session/session_rules_table.h>
Florin Coras6c36f532017-11-03 18:32:34 -070022#include <vnet/session/session_table.h>
Florin Corasc9940fc2019-02-05 20:55:11 -080023#include <vnet/session/session.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050024
25#include <vnet/vnet_msg_enum.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050026
27#define vl_typedefs /* define message structures */
28#include <vnet/vnet_all_api_h.h>
29#undef vl_typedefs
30
31#define vl_endianfun /* define message structures */
32#include <vnet/vnet_all_api_h.h>
33#undef vl_endianfun
34
35/* instantiate all the print functions we know about */
36#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37#define vl_printfun
38#include <vnet/vnet_all_api_h.h>
39#undef vl_printfun
40
41#include <vlibapi/api_helper_macros.h>
42
43#define foreach_session_api_msg \
44_(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply) \
Florin Coras6cf30ad2017-04-04 23:08:23 -070045_(APPLICATION_ATTACH, application_attach) \
46_(APPLICATION_DETACH, application_detach) \
Dave Barach68b0fb02017-02-28 15:15:56 -050047_(BIND_URI, bind_uri) \
48_(UNBIND_URI, unbind_uri) \
49_(CONNECT_URI, connect_uri) \
50_(DISCONNECT_SESSION, disconnect_session) \
51_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
52_(ACCEPT_SESSION_REPLY, accept_session_reply) \
53_(RESET_SESSION_REPLY, reset_session_reply) \
Florin Corascea194d2017-10-02 00:18:51 -070054_(BIND_SOCK, bind_sock) \
Dave Barach68b0fb02017-02-28 15:15:56 -050055_(UNBIND_SOCK, unbind_sock) \
56_(CONNECT_SOCK, connect_sock) \
Florin Corase04c2992017-03-01 08:17:34 -080057_(SESSION_ENABLE_DISABLE, session_enable_disable) \
Florin Corascea194d2017-10-02 00:18:51 -070058_(APP_NAMESPACE_ADD_DEL, app_namespace_add_del) \
Florin Coras1c710452017-10-17 00:03:13 -070059_(SESSION_RULE_ADD_DEL, session_rule_add_del) \
Florin Coras6c36f532017-11-03 18:32:34 -070060_(SESSION_RULES_DUMP, session_rules_dump) \
Florin Coras371ca502018-02-21 12:07:41 -080061_(APPLICATION_TLS_CERT_ADD, application_tls_cert_add) \
62_(APPLICATION_TLS_KEY_ADD, application_tls_key_add) \
Florin Coras15531972018-08-12 23:50:53 -070063_(APP_WORKER_ADD_DEL, app_worker_add_del) \
Florin Corase04c2992017-03-01 08:17:34 -080064
Dave Barach68b0fb02017-02-28 15:15:56 -050065static int
Florin Coras99368312018-08-02 10:45:44 -070066session_send_fds (vl_api_registration_t * reg, int fds[], int n_fds)
Florin Corasb384b542018-01-15 01:08:33 -080067{
68 clib_error_t *error;
69 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
70 {
71 clib_warning ("can't send memfd fd");
72 return -1;
73 }
Florin Coras99368312018-08-02 10:45:44 -070074 error = vl_api_send_fd_msg (reg, fds, n_fds);
Florin Corasb384b542018-01-15 01:08:33 -080075 if (error)
76 {
77 clib_error_report (error);
78 return -1;
79 }
80 return 0;
81}
82
83static int
Florin Corasfa76a762018-11-29 12:40:10 -080084send_add_segment_callback (u32 api_client_index, u64 segment_handle)
Dave Barach68b0fb02017-02-28 15:15:56 -050085{
Florin Coras99368312018-08-02 10:45:44 -070086 int fds[SESSION_N_FD_TYPE], n_fds = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050087 vl_api_map_another_segment_t *mp;
Florin Corasfa76a762018-11-29 12:40:10 -080088 svm_fifo_segment_private_t *fs;
Florin Corasb384b542018-01-15 01:08:33 -080089 vl_api_registration_t *reg;
Florin Corasfa76a762018-11-29 12:40:10 -080090 ssvm_private_t *sp;
Florin Coras99368312018-08-02 10:45:44 -070091 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -050092
Florin Corasb384b542018-01-15 01:08:33 -080093 reg = vl_mem_api_client_index_to_registration (api_client_index);
94 if (!reg)
95 {
Florin Corasfa76a762018-11-29 12:40:10 -080096 clib_warning ("no api registration for client: %u", api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -080097 return -1;
98 }
Dave Barach68b0fb02017-02-28 15:15:56 -050099
Florin Corasfa76a762018-11-29 12:40:10 -0800100 fs = segment_manager_get_segment_w_handle (segment_handle);
101 sp = &fs->ssvm;
Florin Coras99368312018-08-02 10:45:44 -0700102 if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
Florin Corasb384b542018-01-15 01:08:33 -0800103 {
Florin Coras99368312018-08-02 10:45:44 -0700104 if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
105 {
106 clib_warning ("can't send memfd fd");
107 return -1;
108 }
109
110 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
111 fds[n_fds] = sp->fd;
112 n_fds += 1;
Florin Corasb384b542018-01-15 01:08:33 -0800113 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500114
Florin Coras99368312018-08-02 10:45:44 -0700115 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400116 clib_memset (mp, 0, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500117 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
Florin Corasb384b542018-01-15 01:08:33 -0800118 mp->segment_size = sp->ssvm_size;
Florin Coras99368312018-08-02 10:45:44 -0700119 mp->fd_flags = fd_flags;
Florin Corasfa76a762018-11-29 12:40:10 -0800120 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasb384b542018-01-15 01:08:33 -0800121 strncpy ((char *) mp->segment_name, (char *) sp->name,
Dave Barach68b0fb02017-02-28 15:15:56 -0500122 sizeof (mp->segment_name) - 1);
123
Florin Corasb384b542018-01-15 01:08:33 -0800124 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
125
Florin Coras99368312018-08-02 10:45:44 -0700126 if (n_fds)
127 return session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500128
129 return 0;
130}
131
132static int
Florin Corasfa76a762018-11-29 12:40:10 -0800133send_del_segment_callback (u32 api_client_index, u64 segment_handle)
Florin Corasf8f516a2018-02-08 15:10:09 -0800134{
135 vl_api_unmap_segment_t *mp;
136 vl_api_registration_t *reg;
137
138 reg = vl_mem_api_client_index_to_registration (api_client_index);
139 if (!reg)
140 {
141 clib_warning ("no registration: %u", api_client_index);
142 return -1;
143 }
144
Florin Coras99368312018-08-02 10:45:44 -0700145 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400146 clib_memset (mp, 0, sizeof (*mp));
Florin Corasf8f516a2018-02-08 15:10:09 -0800147 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
Florin Corasfa76a762018-11-29 12:40:10 -0800148 mp->segment_handle = clib_host_to_net_u64 (segment_handle);
Florin Corasf8f516a2018-02-08 15:10:09 -0800149 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
150
Florin Coras99368312018-08-02 10:45:44 -0700151 return 0;
152}
153
154static int
Florin Coras134a9962018-08-28 11:32:04 -0700155send_app_cut_through_registration_add (u32 api_client_index,
156 u32 wrk_map_index, u64 mq_addr,
Florin Coras99368312018-08-02 10:45:44 -0700157 u64 peer_mq_addr)
158{
159 vl_api_app_cut_through_registration_add_t *mp;
160 vl_api_registration_t *reg;
161 svm_msg_q_t *mq, *peer_mq;
162 int fds[2];
163
164 reg = vl_mem_api_client_index_to_registration (api_client_index);
165 if (!reg)
166 {
167 clib_warning ("no registration: %u", api_client_index);
168 return -1;
169 }
170
171 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400172 clib_memset (mp, 0, sizeof (*mp));
Florin Coras99368312018-08-02 10:45:44 -0700173 mp->_vl_msg_id =
174 clib_host_to_net_u16 (VL_API_APP_CUT_THROUGH_REGISTRATION_ADD);
175
176 mp->evt_q_address = mq_addr;
177 mp->peer_evt_q_address = peer_mq_addr;
Florin Coras134a9962018-08-28 11:32:04 -0700178 mp->wrk_index = wrk_map_index;
Florin Coras99368312018-08-02 10:45:44 -0700179
180 mq = uword_to_pointer (mq_addr, svm_msg_q_t *);
181 peer_mq = uword_to_pointer (peer_mq_addr, svm_msg_q_t *);
182
183 if (svm_msg_q_get_producer_eventfd (mq) != -1)
184 {
185 mp->fd_flags |= SESSION_FD_F_MQ_EVENTFD;
186 mp->n_fds = 2;
187 /* app will overwrite exactly the fds we pass here. So
188 * when we swap mq with peer_mq (accept vs connect) the
189 * fds will still be valid */
190 fds[0] = svm_msg_q_get_consumer_eventfd (mq);
191 fds[1] = svm_msg_q_get_producer_eventfd (peer_mq);
192 }
193
194 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
195
196 if (mp->n_fds != 0)
197 session_send_fds (reg, fds, mp->n_fds);
Florin Corasf8f516a2018-02-08 15:10:09 -0800198
199 return 0;
200}
201
202static int
Florin Coras288eaab2019-02-03 15:26:14 -0800203send_session_accept_callback (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500204{
Florin Coras15531972018-08-12 23:50:53 -0700205 app_worker_t *server_wrk = app_worker_get (s->app_wrk_index);
Florin Corasb384b542018-01-15 01:08:33 -0800206 vl_api_accept_session_t *mp;
207 vl_api_registration_t *reg;
208 transport_connection_t *tc;
Florin Coras288eaab2019-02-03 15:26:14 -0800209 session_t *listener;
Florin Coras3c2fed52018-07-04 04:15:05 -0700210 svm_msg_q_t *vpp_queue;
Florin Coras15531972018-08-12 23:50:53 -0700211 application_t *server;
Florin Corasc9940fc2019-02-05 20:55:11 -0800212 app_listener_t *al;
Dave Barach68b0fb02017-02-28 15:15:56 -0500213
Florin Coras15531972018-08-12 23:50:53 -0700214 server = application_get (server_wrk->app_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800215 reg =
216 vl_mem_api_client_index_to_registration (server_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800217 if (!reg)
218 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800219 clib_warning ("no registration: %u", server_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800220 return -1;
221 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500222
Florin Corasb384b542018-01-15 01:08:33 -0800223 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400224 clib_memset (mp, 0, sizeof (*mp));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700225
Dave Barach68b0fb02017-02-28 15:15:56 -0500226 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
Florin Coras15531972018-08-12 23:50:53 -0700227 mp->context = server_wrk->wrk_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800228 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
229 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800230
231 if (session_has_transport (s))
232 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700233 listener = listen_session_get (s->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800234 al = app_listener_get (server, listener->al_index);
235 mp->listener_handle = app_listener_handle (al);
Florin Corasf8f516a2018-02-08 15:10:09 -0800236 if (application_is_proxy (server))
237 {
238 listener =
Florin Coras15531972018-08-12 23:50:53 -0700239 app_worker_first_listener (server_wrk, session_get_fib_proto (s),
240 session_get_transport_proto (s));
Florin Corasf8f516a2018-02-08 15:10:09 -0800241 if (listener)
242 mp->listener_handle = listen_session_get_handle (listener);
243 }
244 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
245 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
246 mp->handle = session_handle (s);
Florin Coras1ee78302019-02-05 15:51:15 -0800247 tc = transport_get_connection (session_get_transport_proto (s),
248 s->connection_index, s->thread_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800249 mp->port = tc->rmt_port;
250 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500251 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Corasf8f516a2018-02-08 15:10:09 -0800252 }
253 else
254 {
255 local_session_t *ls = (local_session_t *) s;
256 local_session_t *ll;
257 if (application_local_session_listener_has_transport (ls))
258 {
Florin Coras5c9083d2018-04-13 06:39:07 -0700259 listener = listen_session_get (ls->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800260 al = app_listener_get (server, listener->al_index);
261 mp->listener_handle = app_listener_handle (al);
Florin Coras5fda7a32018-02-14 08:04:31 -0800262 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
Florin Corasf8f516a2018-02-08 15:10:09 -0800263 }
264 else
265 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700266 ll = application_get_local_listen_session (server,
Florin Corasf8f516a2018-02-08 15:10:09 -0800267 ls->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800268 al = app_listener_get (server, ll->al_index);
269 mp->listener_handle = app_listener_handle (al);
Florin Coras5fda7a32018-02-14 08:04:31 -0800270 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
Florin Corasf8f516a2018-02-08 15:10:09 -0800271 }
272 mp->handle = application_local_session_handle (ls);
273 mp->port = ls->port;
274 mp->vpp_event_queue_address = ls->client_evt_q;
275 mp->server_event_queue_address = ls->server_evt_q;
276 }
Florin Corasb384b542018-01-15 01:08:33 -0800277 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500278
279 return 0;
280}
281
282static void
Florin Coras288eaab2019-02-03 15:26:14 -0800283send_session_disconnect_callback (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500284{
Florin Coras15531972018-08-12 23:50:53 -0700285 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Corasb384b542018-01-15 01:08:33 -0800286 vl_api_disconnect_session_t *mp;
287 vl_api_registration_t *reg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500288
Florin Corasc1f5a432018-11-20 11:31:26 -0800289 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800290 if (!reg)
291 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800292 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800293 return;
294 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500295
Florin Corasb384b542018-01-15 01:08:33 -0800296 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400297 clib_memset (mp, 0, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500298 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700299 mp->handle = session_handle (s);
Florin Corasc1f5a432018-11-20 11:31:26 -0800300 mp->context = app_wrk->api_client_index;
Florin Corasb384b542018-01-15 01:08:33 -0800301 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500302}
303
Florin Corasd79b41e2017-03-04 05:37:52 -0800304static void
Florin Coras288eaab2019-02-03 15:26:14 -0800305send_session_reset_callback (session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800306{
Florin Coras15531972018-08-12 23:50:53 -0700307 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Corasb384b542018-01-15 01:08:33 -0800308 vl_api_registration_t *reg;
309 vl_api_reset_session_t *mp;
Florin Corasd79b41e2017-03-04 05:37:52 -0800310
Florin Corasc1f5a432018-11-20 11:31:26 -0800311 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800312 if (!reg)
313 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800314 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800315 return;
316 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800317
Florin Corasb384b542018-01-15 01:08:33 -0800318 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400319 clib_memset (mp, 0, sizeof (*mp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800320 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700321 mp->handle = session_handle (s);
Florin Corasb384b542018-01-15 01:08:33 -0800322 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Florin Corasd79b41e2017-03-04 05:37:52 -0800323}
324
Dave Barach0194f1a2017-05-15 10:11:39 -0400325int
Florin Coras15531972018-08-12 23:50:53 -0700326send_session_connected_callback (u32 app_wrk_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800327 session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500328{
Dave Wallace33e002b2017-09-06 01:20:02 -0400329 vl_api_connect_session_reply_t *mp;
Florin Corasade70e42017-10-14 18:56:41 -0700330 transport_connection_t *tc;
Florin Corasb384b542018-01-15 01:08:33 -0800331 vl_api_registration_t *reg;
Florin Coras3c2fed52018-07-04 04:15:05 -0700332 svm_msg_q_t *vpp_queue;
Florin Coras15531972018-08-12 23:50:53 -0700333 app_worker_t *app_wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500334
Florin Coras15531972018-08-12 23:50:53 -0700335 app_wrk = app_worker_get (app_wrk_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800336 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800337 if (!reg)
338 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800339 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800340 return -1;
341 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500342
Florin Corasb384b542018-01-15 01:08:33 -0800343 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Wallace33e002b2017-09-06 01:20:02 -0400344 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700345 mp->context = api_context;
Dave Wallace965fec92017-10-17 16:19:41 -0400346
347 if (is_fail)
348 goto done;
349
Florin Corasf8f516a2018-02-08 15:10:09 -0800350 if (session_has_transport (s))
Dave Barach68b0fb02017-02-28 15:15:56 -0500351 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800352 tc = session_get_transport (s);
353 if (!tc)
354 {
355 is_fail = 1;
356 goto done;
357 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500358
Florin Corasf8f516a2018-02-08 15:10:09 -0800359 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
360 mp->handle = session_handle (s);
361 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Dave Barach178cf492018-11-13 16:34:13 -0500362 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Corasf8f516a2018-02-08 15:10:09 -0800363 mp->is_ip4 = tc->is_ip4;
364 mp->lcl_port = tc->lcl_port;
Florin Coras288eaab2019-02-03 15:26:14 -0800365 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
366 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800367 }
368 else
369 {
370 local_session_t *ls = (local_session_t *) s;
371 mp->handle = application_local_session_handle (ls);
372 mp->lcl_port = ls->port;
373 mp->vpp_event_queue_address = ls->server_evt_q;
374 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras288eaab2019-02-03 15:26:14 -0800375 mp->server_rx_fifo = pointer_to_uword (s->tx_fifo);
376 mp->server_tx_fifo = pointer_to_uword (s->rx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800377 }
Dave Wallace965fec92017-10-17 16:19:41 -0400378
379done:
380 mp->retval = is_fail ?
381 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
Florin Corasb384b542018-01-15 01:08:33 -0800382 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500383 return 0;
384}
385
Florin Corascea194d2017-10-02 00:18:51 -0700386static session_cb_vft_t session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500387 .session_accept_callback = send_session_accept_callback,
388 .session_disconnect_callback = send_session_disconnect_callback,
389 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800390 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500391 .add_segment_callback = send_add_segment_callback,
Florin Corasf8f516a2018-02-08 15:10:09 -0800392 .del_segment_callback = send_del_segment_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500393};
394
Florin Coras52207f12018-07-12 14:48:06 -0700395static int
Florin Coras83ea6692018-10-12 16:55:14 -0700396mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
397{
398 int rv;
399 u8 try = 0;
400 while (try < 100)
401 {
402 rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
403 SESSION_MQ_CTRL_EVT_RING,
404 SVM_Q_NOWAIT, msg);
405 if (!rv)
406 return 0;
407 try++;
Florin Corase2ea1932018-12-17 23:08:14 -0800408 usleep (1);
Florin Coras83ea6692018-10-12 16:55:14 -0700409 }
Florin Corasdc2e2512018-12-03 17:47:26 -0800410 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700411 return -1;
412}
413
414static int
Florin Coras288eaab2019-02-03 15:26:14 -0800415mq_send_session_accepted_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700416{
Florin Coras15531972018-08-12 23:50:53 -0700417 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700418 svm_msg_q_msg_t _msg, *msg = &_msg;
419 svm_msg_q_t *vpp_queue, *app_mq;
Florin Coras52207f12018-07-12 14:48:06 -0700420 transport_connection_t *tc;
Florin Coras288eaab2019-02-03 15:26:14 -0800421 session_t *listener;
Florin Coras52207f12018-07-12 14:48:06 -0700422 session_accepted_msg_t *mp;
423 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700424 application_t *app;
Florin Corasc9940fc2019-02-05 20:55:11 -0800425 app_listener_t *al;
Florin Coras52207f12018-07-12 14:48:06 -0700426
Florin Coras15531972018-08-12 23:50:53 -0700427 app = application_get (app_wrk->app_index);
428 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700429 if (mq_try_lock_and_alloc_msg (app_mq, msg))
430 return -1;
Florin Coras52207f12018-07-12 14:48:06 -0700431
432 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400433 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700434 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
435 mp = (session_accepted_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800436 clib_memset (mp, 0, sizeof (*mp));
Florin Corasab2f6db2018-08-31 14:31:41 -0700437 mp->context = app->app_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800438 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
439 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800440 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700441
442 if (session_has_transport (s))
443 {
444 listener = listen_session_get (s->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800445 al = app_listener_get (app, listener->al_index);
446 mp->listener_handle = app_listener_handle (al);
Florin Coras52207f12018-07-12 14:48:06 -0700447 if (application_is_proxy (app))
448 {
449 listener =
Florin Coras15531972018-08-12 23:50:53 -0700450 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
451 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700452 if (listener)
453 mp->listener_handle = listen_session_get_handle (listener);
454 }
455 vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
456 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
457 mp->handle = session_handle (s);
Florin Coras1ee78302019-02-05 15:51:15 -0800458 tc = transport_get_connection (session_get_transport_proto (s),
459 s->connection_index, s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700460 mp->port = tc->rmt_port;
461 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500462 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700463 }
464 else
465 {
466 local_session_t *ls = (local_session_t *) s;
467 local_session_t *ll;
Florin Coras99368312018-08-02 10:45:44 -0700468 u8 main_thread = vlib_num_workers ()? 1 : 0;
469
Florin Corasc1f5a432018-11-20 11:31:26 -0800470 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700471 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700472 ls->server_evt_q,
473 ls->client_evt_q);
474
Florin Coras52207f12018-07-12 14:48:06 -0700475 if (application_local_session_listener_has_transport (ls))
476 {
477 listener = listen_session_get (ls->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800478 al = app_listener_get (app, listener->al_index);
479 mp->listener_handle = app_listener_handle (al);
Florin Coras52207f12018-07-12 14:48:06 -0700480 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
481 }
482 else
483 {
Florin Corasab2f6db2018-08-31 14:31:41 -0700484 ll = application_get_local_listen_session (app, ls->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800485 al = app_listener_get (app, ll->al_index);
486 mp->listener_handle = app_listener_handle (al);
Florin Coras52207f12018-07-12 14:48:06 -0700487 mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
488 }
489 mp->handle = application_local_session_handle (ls);
490 mp->port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700491 vpp_queue = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700492 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
493 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700494 mp->server_event_queue_address = ls->server_evt_q;
495 }
Florin Coras537b17e2018-09-28 10:35:45 -0700496 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700497
498 return 0;
499}
500
Florin Coras72b04282019-01-14 17:23:11 -0800501static inline void
502mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
503 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700504{
Florin Coras52207f12018-07-12 14:48:06 -0700505 svm_msg_q_msg_t _msg, *msg = &_msg;
506 session_disconnected_msg_t *mp;
507 svm_msg_q_t *app_mq;
508 session_event_t *evt;
509
Florin Coras15531972018-08-12 23:50:53 -0700510 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700511 if (mq_try_lock_and_alloc_msg (app_mq, msg))
512 return;
Florin Coras52207f12018-07-12 14:48:06 -0700513 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400514 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800515 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700516 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800517 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800518 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700519 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700520}
521
Florin Coras72b04282019-01-14 17:23:11 -0800522static inline void
523mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
524 svm_fifo_t * f, session_evt_type_t evt_type)
525{
526 app_worker_t *app_wrk;
527 application_t *app;
528 int i;
529
530 app = application_get (app_index);
531 if (!app)
532 return;
533
534 for (i = 0; i < f->n_subscribers; i++)
535 {
536 if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
537 continue;
538 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
539 }
540}
541
542static void
Florin Coras288eaab2019-02-03 15:26:14 -0800543mq_send_session_disconnected_cb (session_t * s)
Florin Coras72b04282019-01-14 17:23:11 -0800544{
545 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
546 session_handle_t sh = session_handle (s);
547
548 mq_send_session_close_evt (app_wrk, session_handle (s),
549 SESSION_CTRL_EVT_DISCONNECTED);
550
Florin Coras288eaab2019-02-03 15:26:14 -0800551 if (svm_fifo_n_subscribers (s->rx_fifo))
552 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800553 SESSION_CTRL_EVT_DISCONNECTED);
554}
555
Florin Corasaa27eb92018-10-13 12:20:01 -0700556void
557mq_send_local_session_disconnected_cb (u32 app_wrk_index,
558 local_session_t * ls)
559{
560 app_worker_t *app_wrk = app_worker_get (app_wrk_index);
Florin Coras72b04282019-01-14 17:23:11 -0800561 session_handle_t sh = application_local_session_handle (ls);
Florin Corasaa27eb92018-10-13 12:20:01 -0700562
Florin Coras72b04282019-01-14 17:23:11 -0800563 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
564
Florin Coras288eaab2019-02-03 15:26:14 -0800565 if (svm_fifo_n_subscribers (ls->rx_fifo))
566 mq_notify_close_subscribers (app_wrk->app_index, sh, ls->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800567 SESSION_CTRL_EVT_DISCONNECTED);
Florin Corasaa27eb92018-10-13 12:20:01 -0700568}
569
Florin Coras52207f12018-07-12 14:48:06 -0700570static void
Florin Coras288eaab2019-02-03 15:26:14 -0800571mq_send_session_reset_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700572{
Florin Coras72b04282019-01-14 17:23:11 -0800573 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
574 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700575
Florin Coras72b04282019-01-14 17:23:11 -0800576 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
577
Florin Coras288eaab2019-02-03 15:26:14 -0800578 if (svm_fifo_n_subscribers (s->rx_fifo))
579 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800580 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700581}
582
583static int
Florin Coras15531972018-08-12 23:50:53 -0700584mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800585 session_t * s, u8 is_fail)
Florin Coras52207f12018-07-12 14:48:06 -0700586{
587 svm_msg_q_msg_t _msg, *msg = &_msg;
588 session_connected_msg_t *mp;
589 svm_msg_q_t *vpp_mq, *app_mq;
590 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700591 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700592 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700593
Florin Coras15531972018-08-12 23:50:53 -0700594 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700595 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700596 if (!app_mq)
597 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800598 clib_warning ("app %u with api index: %u not attached",
599 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700600 return -1;
601 }
602
Florin Coras83ea6692018-10-12 16:55:14 -0700603 if (mq_try_lock_and_alloc_msg (app_mq, msg))
604 return -1;
Florin Corasdc2e2512018-12-03 17:47:26 -0800605
Florin Coras52207f12018-07-12 14:48:06 -0700606 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400607 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700608 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
609 mp = (session_connected_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800610 clib_memset (mp, 0, sizeof (*mp));
Florin Coras52207f12018-07-12 14:48:06 -0700611 mp->context = api_context;
612
613 if (is_fail)
614 goto done;
615
Florin Corasdc2e2512018-12-03 17:47:26 -0800616 mp->segment_handle = session_segment_handle (s);
617
Florin Coras52207f12018-07-12 14:48:06 -0700618 if (session_has_transport (s))
619 {
620 tc = session_get_transport (s);
621 if (!tc)
622 {
623 is_fail = 1;
624 goto done;
625 }
626
627 vpp_mq = session_manager_get_vpp_event_queue (s->thread_index);
628 mp->handle = session_handle (s);
629 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Dave Barach178cf492018-11-13 16:34:13 -0500630 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700631 mp->is_ip4 = tc->is_ip4;
632 mp->lcl_port = tc->lcl_port;
Florin Coras288eaab2019-02-03 15:26:14 -0800633 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
634 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras52207f12018-07-12 14:48:06 -0700635 }
636 else
637 {
638 local_session_t *ls = (local_session_t *) s;
Florin Coras99368312018-08-02 10:45:44 -0700639 u8 main_thread = vlib_num_workers ()? 1 : 0;
640
Florin Corasc1f5a432018-11-20 11:31:26 -0800641 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700642 app_wrk->wrk_map_index,
Florin Coras99368312018-08-02 10:45:44 -0700643 ls->client_evt_q,
644 ls->server_evt_q);
645
Florin Coras52207f12018-07-12 14:48:06 -0700646 mp->handle = application_local_session_handle (ls);
647 mp->lcl_port = ls->port;
Florin Coras99368312018-08-02 10:45:44 -0700648 vpp_mq = session_manager_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700649 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras52207f12018-07-12 14:48:06 -0700650 mp->client_event_queue_address = ls->client_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700651 mp->server_event_queue_address = ls->server_evt_q;
Florin Coras288eaab2019-02-03 15:26:14 -0800652 mp->server_rx_fifo = pointer_to_uword (s->tx_fifo);
653 mp->server_tx_fifo = pointer_to_uword (s->rx_fifo);
Florin Coras52207f12018-07-12 14:48:06 -0700654 }
655
656done:
657 mp->retval = is_fail ?
658 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
659
Florin Coras537b17e2018-09-28 10:35:45 -0700660 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700661 return 0;
662}
663
Florin Coras60116992018-08-27 09:52:18 -0700664static int
665mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
666 session_handle_t handle, int rv)
667{
668 svm_msg_q_msg_t _msg, *msg = &_msg;
669 svm_msg_q_t *app_mq, *vpp_evt_q;
670 transport_connection_t *tc;
Florin Coras60116992018-08-27 09:52:18 -0700671 session_bound_msg_t *mp;
672 app_worker_t *app_wrk;
673 session_event_t *evt;
674 application_t *app;
Florin Corasc9940fc2019-02-05 20:55:11 -0800675 app_listener_t *al;
676 session_t *ls = 0;
Florin Coras60116992018-08-27 09:52:18 -0700677
678 app_wrk = app_worker_get (app_wrk_index);
679 app = application_get (app_wrk->app_index);
680 app_mq = app_wrk->event_queue;
681 if (!app_mq)
682 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800683 clib_warning ("app %u with api index: %u not attached",
684 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700685 return -1;
686 }
687
Florin Coras83ea6692018-10-12 16:55:14 -0700688 if (mq_try_lock_and_alloc_msg (app_mq, msg))
689 return -1;
690
Florin Coras60116992018-08-27 09:52:18 -0700691 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400692 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700693 evt->event_type = SESSION_CTRL_EVT_BOUND;
694 mp = (session_bound_msg_t *) evt->data;
695 mp->context = api_context;
696
697 if (rv)
698 goto done;
699
700 mp->handle = handle;
701 if (application_has_global_scope (app))
702 {
Florin Corasc9940fc2019-02-05 20:55:11 -0800703 al = app_listener_get_w_handle (handle);
704 ls = app_listener_get_session (al);
Florin Coras60116992018-08-27 09:52:18 -0700705 tc = listen_session_get_transport (ls);
706 mp->lcl_port = tc->lcl_port;
707 mp->lcl_is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500708 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras60116992018-08-27 09:52:18 -0700709 }
710 else
711 {
712 local_session_t *local;
Florin Corasab2f6db2018-08-31 14:31:41 -0700713 local = application_get_local_listener_w_handle (handle);
Florin Coras60116992018-08-27 09:52:18 -0700714 mp->lcl_port = local->port;
715 mp->lcl_is_ip4 = session_type_is_ip4 (local->session_type);
716 }
717
Florin Coras5f45e012019-01-23 09:21:30 -0800718 vpp_evt_q = session_manager_get_vpp_event_queue (0);
719 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
720
Florin Coras60116992018-08-27 09:52:18 -0700721 if (ls && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
722 {
Florin Coras288eaab2019-02-03 15:26:14 -0800723 mp->rx_fifo = pointer_to_uword (ls->rx_fifo);
724 mp->tx_fifo = pointer_to_uword (ls->tx_fifo);
Florin Coras60116992018-08-27 09:52:18 -0700725 }
726
727done:
728 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700729 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700730 return 0;
731}
732
Florin Coras52207f12018-07-12 14:48:06 -0700733static session_cb_vft_t session_mq_cb_vft = {
734 .session_accept_callback = mq_send_session_accepted_cb,
735 .session_disconnect_callback = mq_send_session_disconnected_cb,
736 .session_connected_callback = mq_send_session_connected_cb,
737 .session_reset_callback = mq_send_session_reset_cb,
738 .add_segment_callback = send_add_segment_callback,
739 .del_segment_callback = send_del_segment_callback,
740};
741
Dave Barach68b0fb02017-02-28 15:15:56 -0500742static void
Florin Corase04c2992017-03-01 08:17:34 -0800743vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
744{
745 vl_api_session_enable_disable_reply_t *rmp;
746 vlib_main_t *vm = vlib_get_main ();
747 int rv = 0;
748
749 vnet_session_enable_disable (vm, mp->is_enable);
750 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
751}
752
753static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700754vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500755{
Florin Coras99368312018-08-02 10:45:44 -0700756 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700757 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800758 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700759 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800760 vl_api_registration_t *reg;
Florin Coras99368312018-08-02 10:45:44 -0700761 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500762
Florin Corasfc804d92018-01-26 01:27:01 -0800763 reg = vl_api_client_index_to_registration (mp->client_index);
764 if (!reg)
765 return;
766
Florin Coras6cf30ad2017-04-04 23:08:23 -0700767 if (session_manager_is_enabled () == 0)
768 {
769 rv = VNET_API_ERROR_FEATURE_DISABLED;
770 goto done;
771 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500772
Florin Corasff6e7692017-12-11 04:59:01 -0800773 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700774 sizeof (mp->options),
775 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500776
Dave Barachb7b92992018-10-17 10:38:51 -0400777 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500778 a->api_client_index = mp->client_index;
779 a->options = mp->options;
Florin Coras52207f12018-07-12 14:48:06 -0700780
781 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
782 a->session_cb_vft = &session_mq_cb_vft;
783 else
784 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500785
Florin Corascea194d2017-10-02 00:18:51 -0700786 if (mp->namespace_id_len > 64)
787 {
788 rv = VNET_API_ERROR_INVALID_VALUE;
789 goto done;
790 }
791
792 if (mp->namespace_id_len)
793 {
Dave Wallace8af20542017-10-26 03:29:30 -0400794 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500795 clib_memcpy_fast (a->namespace_id, mp->namespace_id,
796 mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -0700797 }
798
Florin Corasc1a42652019-02-08 18:27:29 -0800799 if ((rv = vnet_application_attach (a)))
Florin Corascea194d2017-10-02 00:18:51 -0700800 {
Florin Corasc1a42652019-02-08 18:27:29 -0800801 clib_warning ("attach returned: %d", rv);
Florin Coras99368312018-08-02 10:45:44 -0700802 vec_free (a->namespace_id);
803 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700804 }
805 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500806
Florin Coras99368312018-08-02 10:45:44 -0700807 /* Send event queues segment */
808 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
809 {
810 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
811 fds[n_fds] = evt_q_segment->fd;
812 n_fds += 1;
813 }
814 /* Send fifo segment fd if needed */
815 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
816 {
817 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
818 fds[n_fds] = a->segment->fd;
819 n_fds += 1;
820 }
821 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
822 {
823 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
824 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
825 n_fds += 1;
826 }
827
Florin Coras6cf30ad2017-04-04 23:08:23 -0700828done:
Florin Corasa5464812017-04-19 13:00:05 -0700829
Dave Barach68b0fb02017-02-28 15:15:56 -0500830 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700831 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500832 if (!rv)
833 {
Florin Corasb384b542018-01-15 01:08:33 -0800834 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800835 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500836 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800837 rmp->segment_size = segp->ssvm_size;
838 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500839 {
Florin Corasb384b542018-01-15 01:08:33 -0800840 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
841 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500842 }
Florin Coras99368312018-08-02 10:45:44 -0700843 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
844 rmp->n_fds = n_fds;
845 rmp->fd_flags = fd_flags;
Florin Corasd85de682018-11-29 17:02:29 -0800846 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500847 }
848 }));
849 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800850
Florin Coras99368312018-08-02 10:45:44 -0700851 if (n_fds)
852 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500853}
854
855static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700856vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
857{
858 vl_api_application_detach_reply_t *rmp;
859 int rv = VNET_API_ERROR_INVALID_VALUE_2;
860 vnet_app_detach_args_t _a, *a = &_a;
861 application_t *app;
862
863 if (session_manager_is_enabled () == 0)
864 {
865 rv = VNET_API_ERROR_FEATURE_DISABLED;
866 goto done;
867 }
868
869 app = application_lookup (mp->client_index);
870 if (app)
871 {
Florin Coras15531972018-08-12 23:50:53 -0700872 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800873 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700874 rv = vnet_application_detach (a);
875 }
876
877done:
878 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
879}
880
881static void
882vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
883{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700884 transport_connection_t *tc = 0;
Florin Corasc9940fc2019-02-05 20:55:11 -0800885 vnet_listen_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700886 vl_api_bind_uri_reply_t *rmp;
Florin Coras288eaab2019-02-03 15:26:14 -0800887 session_t *s;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700888 application_t *app = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700889 svm_msg_q_t *vpp_evt_q;
Florin Coras60116992018-08-27 09:52:18 -0700890 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700891 int rv;
892
893 if (session_manager_is_enabled () == 0)
894 {
895 rv = VNET_API_ERROR_FEATURE_DISABLED;
896 goto done;
897 }
898
899 app = application_lookup (mp->client_index);
900 if (app)
901 {
Dave Barachb7b92992018-10-17 10:38:51 -0400902 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700903 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700904 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700905 rv = vnet_bind_uri (a);
906 }
907 else
908 {
909 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
910 }
911
912done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700913
914 /* *INDENT-OFF* */
915 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
916 if (!rv)
917 {
918 rmp->handle = a->handle;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700919 if (app && application_has_global_scope (app))
920 {
Nathan Skrzypczakbb65ba12019-02-14 13:24:43 +0100921 app_listener_t* al = app_listener_get_w_handle(a->handle);
922 s = app_listener_get_session(al);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700923 tc = listen_session_get_transport (s);
Florin Coras6c354942018-04-18 00:05:21 -0700924 rmp->lcl_is_ip4 = tc->is_ip4;
925 rmp->lcl_port = tc->lcl_port;
Dave Barach178cf492018-11-13 16:34:13 -0500926 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700927 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
928 {
Florin Coras288eaab2019-02-03 15:26:14 -0800929 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
930 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700931 vpp_evt_q = session_manager_get_vpp_event_queue (0);
932 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
933 }
934 }
935 }
936 }));
937 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -0700938
939 /* If app uses mq for control messages, send an mq message as well */
940 if (app && application_use_mq_for_ctrl (app))
941 {
942 app_wrk = application_get_worker (app, 0);
943 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
944 rv);
945 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700946}
947
948static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500949vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
950{
951 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700952 application_t *app;
Florin Corasc1a42652019-02-08 18:27:29 -0800953 vnet_unlisten_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500954 int rv;
955
Florin Coras6cf30ad2017-04-04 23:08:23 -0700956 if (session_manager_is_enabled () == 0)
957 {
958 rv = VNET_API_ERROR_FEATURE_DISABLED;
959 goto done;
960 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500961
Florin Coras6cf30ad2017-04-04 23:08:23 -0700962 app = application_lookup (mp->client_index);
963 if (app)
964 {
965 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700966 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700967 rv = vnet_unbind_uri (a);
968 }
969 else
970 {
971 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
972 }
973
974done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500975 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
976}
977
Florin Corasf03a59a2017-06-09 21:07:32 -0700978static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500979vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
980{
Dave Wallace33e002b2017-09-06 01:20:02 -0400981 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500982 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700983 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700984 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500985
Florin Coras6cf30ad2017-04-04 23:08:23 -0700986 if (session_manager_is_enabled () == 0)
987 {
988 rv = VNET_API_ERROR_FEATURE_DISABLED;
989 goto done;
990 }
Florin Corase04c2992017-03-01 08:17:34 -0800991
Florin Coras6cf30ad2017-04-04 23:08:23 -0700992 app = application_lookup (mp->client_index);
993 if (app)
994 {
Dave Barachb7b92992018-10-17 10:38:51 -0400995 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700996 a->uri = (char *) mp->uri;
997 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -0700998 a->app_index = app->app_index;
Florin Corasc1a42652019-02-08 18:27:29 -0800999 if ((rv = vnet_connect_uri (a)))
1000 clib_warning ("connect_uri returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001001 }
1002 else
1003 {
1004 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1005 }
Florin Corase04c2992017-03-01 08:17:34 -08001006
Florin Coras3cbc04b2017-10-02 00:18:51 -07001007 /*
1008 * Don't reply to stream (tcp) connects. The reply will come once
1009 * the connection is established. In case of the redirects, the reply
1010 * will come from the server app.
1011 */
Florin Coras8f89dd02018-03-05 16:53:07 -08001012 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001013 return;
1014
Florin Coras6cf30ad2017-04-04 23:08:23 -07001015done:
Florin Corase04c2992017-03-01 08:17:34 -08001016 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -04001017 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -08001018 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001019}
1020
1021static void
1022vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
1023{
1024 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001025 vnet_disconnect_args_t _a, *a = &_a;
1026 application_t *app;
1027 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001028
Florin Coras6cf30ad2017-04-04 23:08:23 -07001029 if (session_manager_is_enabled () == 0)
1030 {
1031 rv = VNET_API_ERROR_FEATURE_DISABLED;
1032 goto done;
1033 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001034
Florin Coras6cf30ad2017-04-04 23:08:23 -07001035 app = application_lookup (mp->client_index);
1036 if (app)
1037 {
1038 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001039 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001040 rv = vnet_disconnect_session (a);
1041 }
1042 else
1043 {
1044 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1045 }
1046
1047done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001048 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001049}
1050
1051static void
1052vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1053 mp)
1054{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001055 vnet_disconnect_args_t _a, *a = &_a;
1056 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001057
1058 /* Client objected to disconnecting the session, log and continue */
1059 if (mp->retval)
1060 {
1061 clib_warning ("client retval %d", mp->retval);
1062 return;
1063 }
1064
1065 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001066 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001067 if (app)
1068 {
1069 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001070 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001071 vnet_disconnect_session (a);
1072 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001073}
1074
1075static void
1076vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
1077{
Florin Coras4af830c2018-12-04 09:21:36 -08001078 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras15531972018-08-12 23:50:53 -07001079 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001080 application_t *app;
Florin Coras288eaab2019-02-03 15:26:14 -08001081 session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001082 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05001083
Ondrej Fabry6bd197e2018-08-15 08:46:46 +02001084 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001085 if (!app)
1086 return;
1087
Florin Corascea194d2017-10-02 00:18:51 -07001088 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001089 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -07001090 if (!s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001091 {
1092 clib_warning ("Invalid session!");
1093 return;
1094 }
1095
Florin Coras15531972018-08-12 23:50:53 -07001096 app_wrk = app_worker_get (s->app_wrk_index);
1097 if (app_wrk->app_index != app->app_index)
1098 {
1099 clib_warning ("app %u does not own handle 0x%lx", app->app_index,
1100 mp->handle);
1101 return;
1102 }
1103
Dave Barach68b0fb02017-02-28 15:15:56 -05001104 /* Client objected to resetting the session, log and continue */
1105 if (mp->retval)
1106 {
1107 clib_warning ("client retval %d", mp->retval);
1108 return;
1109 }
1110
Dave Barach68b0fb02017-02-28 15:15:56 -05001111 /* This comes as a response to a reset, transport only waiting for
1112 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -08001113 a->handle = mp->handle;
1114 a->app_index = app->app_index;
1115 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -05001116}
1117
1118static void
1119vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
1120{
Florin Corasf8f516a2018-02-08 15:10:09 -08001121 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
1122 local_session_t *ls;
Florin Coras288eaab2019-02-03 15:26:14 -08001123 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001124
Florin Corasa5464812017-04-19 13:00:05 -07001125 /* Server isn't interested, kill the session */
1126 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -05001127 {
Florin Corasa5464812017-04-19 13:00:05 -07001128 a->app_index = mp->context;
1129 a->handle = mp->handle;
1130 vnet_disconnect_session (a);
Florin Corasf8f516a2018-02-08 15:10:09 -08001131 return;
1132 }
1133
1134 if (session_handle_is_local (mp->handle))
1135 {
Florin Coras623eb562019-02-03 19:28:34 -08001136 ls = app_worker_get_local_session_from_handle (mp->handle);
Florin Coras15531972018-08-12 23:50:53 -07001137 if (!ls || ls->app_wrk_index != mp->context)
Florin Corasf8f516a2018-02-08 15:10:09 -08001138 {
1139 clib_warning ("server %u doesn't own local handle %llu",
1140 mp->context, mp->handle);
1141 return;
1142 }
Florin Coras623eb562019-02-03 19:28:34 -08001143 if (app_worker_local_session_connect_notify (ls))
Florin Corasf8f516a2018-02-08 15:10:09 -08001144 return;
1145 ls->session_state = SESSION_STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001146 }
Florin Corasa5464812017-04-19 13:00:05 -07001147 else
1148 {
Florin Corasf8f516a2018-02-08 15:10:09 -08001149 s = session_get_from_handle_if_valid (mp->handle);
Florin Corasa5464812017-04-19 13:00:05 -07001150 if (!s)
1151 {
1152 clib_warning ("session doesn't exist");
1153 return;
1154 }
Florin Coras15531972018-08-12 23:50:53 -07001155 if (s->app_wrk_index != mp->context)
Florin Corasa5464812017-04-19 13:00:05 -07001156 {
1157 clib_warning ("app doesn't own session");
1158 return;
1159 }
Florin Corasa5464812017-04-19 13:00:05 -07001160 s->session_state = SESSION_STATE_READY;
1161 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001162}
1163
1164static void
1165vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1166 * mp)
1167{
1168 clib_warning ("not implemented");
1169}
1170
1171static void
1172vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1173{
Florin Corasc9940fc2019-02-05 20:55:11 -08001174 vnet_listen_args_t _a, *a = &_a;
1175 transport_connection_t *tc = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001176 vl_api_bind_sock_reply_t *rmp;
Florin Corasc9940fc2019-02-05 20:55:11 -08001177 svm_msg_q_t *vpp_evt_q;
Florin Coraseb2945c2017-11-22 10:39:09 -08001178 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001179 app_worker_t *app_wrk;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001180 ip46_address_t *ip46;
Florin Corasc9940fc2019-02-05 20:55:11 -08001181 app_listener_t *al;
1182 session_t *s;
1183 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001184
Florin Coras6cf30ad2017-04-04 23:08:23 -07001185 if (session_manager_is_enabled () == 0)
1186 {
1187 rv = VNET_API_ERROR_FEATURE_DISABLED;
1188 goto done;
1189 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001190
Florin Coras6cf30ad2017-04-04 23:08:23 -07001191 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001192 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001193 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001194 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1195 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001196 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001197
1198 ip46 = (ip46_address_t *) mp->ip;
Dave Barachb7b92992018-10-17 10:38:51 -04001199 clib_memset (a, 0, sizeof (*a));
Florin Corasdcf55ce2017-11-16 15:32:50 -08001200 a->sep.is_ip4 = mp->is_ip4;
1201 a->sep.ip = *ip46;
1202 a->sep.port = mp->port;
1203 a->sep.fib_index = mp->vrf;
1204 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1205 a->sep.transport_proto = mp->proto;
Florin Coras15531972018-08-12 23:50:53 -07001206 a->app_index = app->app_index;
1207 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001208
Florin Corasc1a42652019-02-08 18:27:29 -08001209 if ((rv = vnet_listen (a)))
1210 clib_warning ("listen returned: %d", rv);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001211
Florin Coras6cf30ad2017-04-04 23:08:23 -07001212done:
Florin Corascea194d2017-10-02 00:18:51 -07001213 /* *INDENT-OFF* */
1214 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1215 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -08001216 {
1217 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -08001218 rmp->lcl_port = mp->port;
Keith Burns (alagalah)60ae80f2018-03-15 10:04:20 -07001219 rmp->lcl_is_ip4 = mp->is_ip4;
Dave Wallace97b1a272017-12-18 13:51:59 -05001220 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -08001221 {
Florin Corasc9940fc2019-02-05 20:55:11 -08001222 al = app_listener_get_w_handle (a->handle);
1223 s = app_listener_get_session (al);
Florin Coraseb2945c2017-11-22 10:39:09 -08001224 tc = listen_session_get_transport (s);
Dave Barach178cf492018-11-13 16:34:13 -05001225 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001226 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1227 {
Florin Coras288eaab2019-02-03 15:26:14 -08001228 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
1229 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001230 vpp_evt_q = session_manager_get_vpp_event_queue (0);
1231 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1232 }
Florin Coraseb2945c2017-11-22 10:39:09 -08001233 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001234 }
Florin Corascea194d2017-10-02 00:18:51 -07001235 }));
1236 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -07001237
1238 /* If app uses mq for control messages, send an mq message as well */
1239 if (app && application_use_mq_for_ctrl (app))
1240 {
1241 app_wrk = application_get_worker (app, mp->wrk_index);
1242 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1243 rv);
1244 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001245}
1246
1247static void
1248vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1249{
1250 vl_api_unbind_sock_reply_t *rmp;
Florin Corasc1a42652019-02-08 18:27:29 -08001251 vnet_unlisten_args_t _a, *a = &_a;
Florin Corasdfae9f92019-02-20 19:48:31 -08001252 app_worker_t *app_wrk;
1253 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001254 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001255
Florin Coras6cf30ad2017-04-04 23:08:23 -07001256 if (session_manager_is_enabled () == 0)
1257 {
1258 rv = VNET_API_ERROR_FEATURE_DISABLED;
1259 goto done;
1260 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001261
Florin Coras6cf30ad2017-04-04 23:08:23 -07001262 app = application_lookup (mp->client_index);
1263 if (app)
1264 {
Florin Coras15531972018-08-12 23:50:53 -07001265 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001266 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001267 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001268 if ((rv = vnet_unlisten (a)))
1269 clib_warning ("unlisten returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001270 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001271
Florin Coras6cf30ad2017-04-04 23:08:23 -07001272done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001273 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
Florin Corasdfae9f92019-02-20 19:48:31 -08001274
1275 /*
1276 * Send reply over msg queue
1277 */
1278 svm_msg_q_msg_t _msg, *msg = &_msg;
1279 session_unlisten_reply_msg_t *ump;
1280 svm_msg_q_t *app_mq;
1281 session_event_t *evt;
1282
1283 if (!app)
1284 return;
1285
1286 app_wrk = application_get_worker (app, a->wrk_map_index);
1287 if (!app_wrk)
1288 return;
1289
1290 app_mq = app_wrk->event_queue;
1291 if (mq_try_lock_and_alloc_msg (app_mq, msg))
1292 return;
1293
1294 evt = svm_msg_q_msg_data (app_mq, msg);
1295 clib_memset (evt, 0, sizeof (*evt));
1296 evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY;
1297 ump = (session_unlisten_reply_msg_t *) evt->data;
1298 ump->context = mp->context;
1299 ump->handle = mp->handle;
1300 ump->retval = rv;
1301 svm_msg_q_add_and_unlock (app_mq, msg);
Dave Barach68b0fb02017-02-28 15:15:56 -05001302}
1303
1304static void
1305vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1306{
Dave Wallace33e002b2017-09-06 01:20:02 -04001307 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001308 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001309 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001310 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001311
Florin Coras6cf30ad2017-04-04 23:08:23 -07001312 if (session_manager_is_enabled () == 0)
1313 {
1314 rv = VNET_API_ERROR_FEATURE_DISABLED;
1315 goto done;
1316 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001317
Florin Coras6cf30ad2017-04-04 23:08:23 -07001318 app = application_lookup (mp->client_index);
1319 if (app)
1320 {
Florin Corase86a8ed2018-01-05 03:20:25 -08001321 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -04001322 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001323
Dave Barachb7b92992018-10-17 10:38:51 -04001324 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001325 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1326 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -07001327 a->sep.is_ip4 = mp->is_ip4;
1328 a->sep.ip = *ip46;
1329 a->sep.port = mp->port;
1330 a->sep.transport_proto = mp->proto;
Florin Coras5665ced2018-10-25 18:03:45 -07001331 a->sep.peer.fib_index = mp->vrf;
1332 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras8f89dd02018-03-05 16:53:07 -08001333 if (mp->hostname_len)
1334 {
Florin Coras15531972018-08-12 23:50:53 -07001335 vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001336 clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
1337 mp->hostname_len);
Florin Coras8f89dd02018-03-05 16:53:07 -08001338 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001339 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001340 a->app_index = app->app_index;
1341 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001342 if ((rv = vnet_connect (a)))
1343 clib_warning ("connect returned: %u", rv);
Florin Coras15531972018-08-12 23:50:53 -07001344 vec_free (a->sep_ext.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001345 }
1346 else
1347 {
1348 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1349 }
Florin Corase04c2992017-03-01 08:17:34 -08001350
Florin Coras8f89dd02018-03-05 16:53:07 -08001351 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001352 return;
1353
1354 /* Got some error, relay it */
1355
Florin Coras6cf30ad2017-04-04 23:08:23 -07001356done:
Dave Wallace33e002b2017-09-06 01:20:02 -04001357 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corasab2f6db2018-08-31 14:31:41 -07001358
1359 if (app && application_use_mq_for_ctrl (app))
1360 {
1361 app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1362 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1363 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001364}
1365
Florin Corascea194d2017-10-02 00:18:51 -07001366static void
Florin Coras15531972018-08-12 23:50:53 -07001367vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1368{
1369 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1370 vl_api_app_worker_add_del_reply_t *rmp;
1371 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -07001372 application_t *app;
1373 u8 fd_flags = 0;
1374
1375 if (!session_manager_is_enabled ())
1376 {
1377 rv = VNET_API_ERROR_FEATURE_DISABLED;
1378 goto done;
1379 }
1380
1381 reg = vl_api_client_index_to_registration (mp->client_index);
1382 if (!reg)
1383 return;
1384
Florin Corasc1f5a432018-11-20 11:31:26 -08001385 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001386 if (!app)
1387 {
1388 rv = VNET_API_ERROR_INVALID_VALUE;
1389 goto done;
1390 }
1391
1392 vnet_app_worker_add_del_args_t args = {
1393 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001394 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001395 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001396 .is_add = mp->is_add
1397 };
Florin Corasc1a42652019-02-08 18:27:29 -08001398 rv = vnet_app_worker_add_del (&args);
1399 if (rv)
Florin Coras15531972018-08-12 23:50:53 -07001400 {
Florin Corasc1a42652019-02-08 18:27:29 -08001401 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -07001402 goto done;
1403 }
1404
Florin Coras14598772018-09-04 19:47:52 -07001405 if (!mp->is_add)
1406 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001407
Florin Coras15531972018-08-12 23:50:53 -07001408 /* Send fifo segment fd if needed */
1409 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1410 {
1411 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1412 fds[n_fds] = args.segment->fd;
1413 n_fds += 1;
1414 }
1415 if (application_segment_manager_properties (app)->use_mq_eventfd)
1416 {
1417 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1418 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1419 n_fds += 1;
1420 }
1421
1422 /* *INDENT-OFF* */
1423done:
1424 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1425 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001426 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001427 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001428 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001429 {
Florin Coras15531972018-08-12 23:50:53 -07001430 if (vec_len (args.segment->name))
1431 {
1432 memcpy (rmp->segment_name, args.segment->name,
1433 vec_len (args.segment->name));
1434 rmp->segment_name_length = vec_len (args.segment->name);
1435 }
1436 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1437 rmp->n_fds = n_fds;
1438 rmp->fd_flags = fd_flags;
1439 }
1440 }));
1441 /* *INDENT-ON* */
1442
1443 if (n_fds)
1444 session_send_fds (reg, fds, n_fds);
1445}
1446
1447static void
Florin Corascea194d2017-10-02 00:18:51 -07001448vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1449{
1450 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -08001451 u32 appns_index = 0;
1452 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001453 int rv = 0;
1454 if (!session_manager_is_enabled ())
1455 {
1456 rv = VNET_API_ERROR_FEATURE_DISABLED;
1457 goto done;
1458 }
1459
1460 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1461 {
1462 rv = VNET_API_ERROR_INVALID_VALUE;
1463 goto done;
1464 }
1465
1466 vec_validate (ns_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001467 clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -07001468 vnet_app_namespace_add_del_args_t args = {
1469 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001470 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001471 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1472 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1473 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1474 .is_add = 1
1475 };
Florin Corasc1a42652019-02-08 18:27:29 -08001476 rv = vnet_app_namespace_add_del (&args);
1477 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -08001478 {
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;
Florin Coras1c710452017-10-17 00:03:13 -07001503 u8 fib_proto;
1504 int rv = 0;
1505
Dave Barachb7b92992018-10-17 10:38:51 -04001506 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001507 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1508
1509 table_args->lcl.fp_len = mp->lcl_plen;
1510 table_args->lcl.fp_proto = fib_proto;
1511 table_args->rmt.fp_len = mp->rmt_plen;
1512 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001513 table_args->lcl_port = mp->lcl_port;
1514 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001515 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1516 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001517 mp->tag[sizeof (mp->tag) - 1] = 0;
1518 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001519 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1520 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -08001521 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -07001522
Dave Barachb7b92992018-10-17 10:38:51 -04001523 clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1524 clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
Florin Coras1c710452017-10-17 00:03:13 -07001525 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1526 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
Florin Corasc1a42652019-02-08 18:27:29 -08001527 rv = vnet_session_rule_add_del (&args);
1528 if (rv)
1529 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -08001530 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001531 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1532}
1533
Florin Coras6c36f532017-11-03 18:32:34 -07001534static void
1535send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001536 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001537 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001538{
1539 vl_api_session_rules_details_t *rmp = 0;
1540 session_mask_or_match_4_t *match =
1541 (session_mask_or_match_4_t *) & rule->match;
1542 session_mask_or_match_4_t *mask =
1543 (session_mask_or_match_4_t *) & rule->mask;
1544
1545 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001546 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001547 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1548 rmp->context = context;
1549
1550 rmp->is_ip4 = 1;
Dave Barach178cf492018-11-13 16:34:13 -05001551 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1552 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001553 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1554 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001555 rmp->lcl_port = match->lcl_port;
1556 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001557 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1558 rmp->scope =
1559 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1560 rmp->transport_proto = transport_proto;
1561 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001562 if (tag)
1563 {
Dave Barach178cf492018-11-13 16:34:13 -05001564 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001565 rmp->tag[vec_len (tag)] = 0;
1566 }
Florin Coras6c36f532017-11-03 18:32:34 -07001567
Florin Coras6c4dae22018-01-09 06:39:23 -08001568 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001569}
1570
1571static void
Florin Corasc97a7392017-11-05 23:07:07 -08001572send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1573 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001574 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001575{
1576 vl_api_session_rules_details_t *rmp = 0;
1577 session_mask_or_match_6_t *match =
1578 (session_mask_or_match_6_t *) & rule->match;
1579 session_mask_or_match_6_t *mask =
1580 (session_mask_or_match_6_t *) & rule->mask;
1581
1582 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001583 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001584 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1585 rmp->context = context;
1586
1587 rmp->is_ip4 = 0;
Dave Barach178cf492018-11-13 16:34:13 -05001588 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1589 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001590 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1591 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001592 rmp->lcl_port = match->lcl_port;
1593 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001594 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001595 rmp->scope =
1596 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -07001597 rmp->transport_proto = transport_proto;
1598 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001599 if (tag)
1600 {
Dave Barach178cf492018-11-13 16:34:13 -05001601 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001602 rmp->tag[vec_len (tag)] = 0;
1603 }
Florin Coras6c36f532017-11-03 18:32:34 -07001604
Florin Coras6c4dae22018-01-09 06:39:23 -08001605 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001606}
1607
1608static void
1609send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001610 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001611 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001612{
1613 mma_rule_16_t *rule16;
1614 mma_rule_40_t *rule40;
1615 mma_rules_table_16_t *srt16;
1616 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001617 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001618
Florin Corasc97a7392017-11-05 23:07:07 -08001619 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001620 {
Florin Corasc97a7392017-11-05 23:07:07 -08001621 u8 *tag = 0;
1622 /* *INDENT-OFF* */
1623 srt16 = &srt->session_rules_tables_16;
1624 pool_foreach (rule16, srt16->rules, ({
1625 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1626 tag = session_rules_table_rule_tag (srt, ri, 1);
1627 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001628 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001629 }));
1630 /* *INDENT-ON* */
1631 }
1632 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1633 {
1634 u8 *tag = 0;
1635 /* *INDENT-OFF* */
1636 srt40 = &srt->session_rules_tables_40;
1637 pool_foreach (rule40, srt40->rules, ({
1638 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1639 tag = session_rules_table_rule_tag (srt, ri, 1);
1640 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001641 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001642 }));
1643 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001644 }
1645}
1646
1647static void
1648vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1649{
Florin Coras6c4dae22018-01-09 06:39:23 -08001650 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001651 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001652 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001653
Florin Coras6c4dae22018-01-09 06:39:23 -08001654 reg = vl_api_client_index_to_registration (mp->client_index);
1655 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001656 return;
1657
1658 /* *INDENT-OFF* */
1659 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001660 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1661 {
1662 send_session_rules_table_details (&st->session_rules[tp],
1663 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001664 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001665 mp->context);
1666 }
Florin Coras6c36f532017-11-03 18:32:34 -07001667 }));
1668 /* *INDENT-ON* */
1669}
1670
Florin Coras371ca502018-02-21 12:07:41 -08001671static void
1672vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1673 mp)
1674{
1675 vl_api_app_namespace_add_del_reply_t *rmp;
1676 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1677 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001678 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001679 u32 cert_len;
1680 int rv = 0;
1681 if (!session_manager_is_enabled ())
1682 {
1683 rv = VNET_API_ERROR_FEATURE_DISABLED;
1684 goto done;
1685 }
Florin Corase3e2f072018-03-04 07:24:30 -08001686 if (!(app = application_lookup (mp->client_index)))
1687 {
1688 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1689 goto done;
1690 }
Dave Barachb7b92992018-10-17 10:38:51 -04001691 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001692 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001693 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001694 if (cert_len > 10000)
1695 {
1696 rv = VNET_API_ERROR_INVALID_VALUE;
1697 goto done;
1698 }
Florin Coras371ca502018-02-21 12:07:41 -08001699 vec_validate (a->cert, cert_len);
Dave Barach178cf492018-11-13 16:34:13 -05001700 clib_memcpy_fast (a->cert, mp->cert, cert_len);
Florin Coras371ca502018-02-21 12:07:41 -08001701 if ((error = vnet_app_add_tls_cert (a)))
1702 {
1703 rv = clib_error_get_code (error);
1704 clib_error_report (error);
1705 }
1706 vec_free (a->cert);
1707done:
1708 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1709}
1710
1711static void
1712vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1713 mp)
1714{
1715 vl_api_app_namespace_add_del_reply_t *rmp;
1716 vnet_app_add_tls_key_args_t _a, *a = &_a;
1717 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001718 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001719 u32 key_len;
1720 int rv = 0;
1721 if (!session_manager_is_enabled ())
1722 {
1723 rv = VNET_API_ERROR_FEATURE_DISABLED;
1724 goto done;
1725 }
Florin Corase3e2f072018-03-04 07:24:30 -08001726 if (!(app = application_lookup (mp->client_index)))
1727 {
1728 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1729 goto done;
1730 }
Dave Barachb7b92992018-10-17 10:38:51 -04001731 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001732 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001733 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001734 if (key_len > 10000)
1735 {
1736 rv = VNET_API_ERROR_INVALID_VALUE;
1737 goto done;
1738 }
Florin Coras371ca502018-02-21 12:07:41 -08001739 vec_validate (a->key, key_len);
Dave Barach178cf492018-11-13 16:34:13 -05001740 clib_memcpy_fast (a->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001741 if ((error = vnet_app_add_tls_key (a)))
1742 {
1743 rv = clib_error_get_code (error);
1744 clib_error_report (error);
1745 }
1746 vec_free (a->key);
1747done:
1748 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1749}
1750
Florin Coras6cf30ad2017-04-04 23:08:23 -07001751static clib_error_t *
1752application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001753{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001754 application_t *app = application_lookup (client_index);
1755 vnet_app_detach_args_t _a, *a = &_a;
1756 if (app)
1757 {
Florin Coras15531972018-08-12 23:50:53 -07001758 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001759 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001760 vnet_application_detach (a);
1761 }
1762 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001763}
1764
Florin Coras6cf30ad2017-04-04 23:08:23 -07001765VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001766
1767#define vl_msg_name_crc_list
1768#include <vnet/vnet_all_api_h.h>
1769#undef vl_msg_name_crc_list
1770
1771static void
1772setup_message_id_table (api_main_t * am)
1773{
1774#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1775 foreach_vl_msg_name_crc_session;
1776#undef _
1777}
1778
1779/*
1780 * session_api_hookup
1781 * Add uri's API message handlers to the table.
1782 * vlib has alread mapped shared memory and
1783 * added the client registration handlers.
1784 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1785 */
1786static clib_error_t *
1787session_api_hookup (vlib_main_t * vm)
1788{
1789 api_main_t *am = &api_main;
1790
1791#define _(N,n) \
1792 vl_msg_api_set_handlers(VL_API_##N, #n, \
1793 vl_api_##n##_t_handler, \
1794 vl_noop_handler, \
1795 vl_api_##n##_t_endian, \
1796 vl_api_##n##_t_print, \
1797 sizeof(vl_api_##n##_t), 1);
1798 foreach_session_api_msg;
1799#undef _
1800
1801 /*
1802 * Messages which bounce off the data-plane to
1803 * an API client. Simply tells the message handling infra not
1804 * to free the message.
1805 *
1806 * Bounced message handlers MUST NOT block the data plane
1807 */
1808 am->message_bounce[VL_API_CONNECT_URI] = 1;
1809 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1810
1811 /*
1812 * Set up the (msg_name, crc, message-id) table
1813 */
1814 setup_message_id_table (am);
1815
1816 return 0;
1817}
1818
1819VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001820
Dave Barach68b0fb02017-02-28 15:15:56 -05001821/*
1822 * fd.io coding-style-patch-verification: ON
1823 *
1824 * Local Variables:
1825 * eval: (c-set-style "gnu")
1826 * End:
1827 */