blob: f96274ae3f3c4425dd244f2094906206ddd2549c [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 Coras70c35f32019-02-25 15:17:28 -0800713 app_listener_t *al;
714 al = app_listener_get_w_handle (handle);
715 local = application_get_local_listen_session (app, al->local_index);
Florin Coras60116992018-08-27 09:52:18 -0700716 mp->lcl_port = local->port;
717 mp->lcl_is_ip4 = session_type_is_ip4 (local->session_type);
718 }
719
Florin Coras5f45e012019-01-23 09:21:30 -0800720 vpp_evt_q = session_manager_get_vpp_event_queue (0);
721 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
722
Florin Coras60116992018-08-27 09:52:18 -0700723 if (ls && session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
724 {
Florin Coras288eaab2019-02-03 15:26:14 -0800725 mp->rx_fifo = pointer_to_uword (ls->rx_fifo);
726 mp->tx_fifo = pointer_to_uword (ls->tx_fifo);
Florin Coras60116992018-08-27 09:52:18 -0700727 }
728
729done:
730 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700731 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700732 return 0;
733}
734
Florin Coras52207f12018-07-12 14:48:06 -0700735static session_cb_vft_t session_mq_cb_vft = {
736 .session_accept_callback = mq_send_session_accepted_cb,
737 .session_disconnect_callback = mq_send_session_disconnected_cb,
738 .session_connected_callback = mq_send_session_connected_cb,
739 .session_reset_callback = mq_send_session_reset_cb,
740 .add_segment_callback = send_add_segment_callback,
741 .del_segment_callback = send_del_segment_callback,
742};
743
Dave Barach68b0fb02017-02-28 15:15:56 -0500744static void
Florin Corase04c2992017-03-01 08:17:34 -0800745vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
746{
747 vl_api_session_enable_disable_reply_t *rmp;
748 vlib_main_t *vm = vlib_get_main ();
749 int rv = 0;
750
751 vnet_session_enable_disable (vm, mp->is_enable);
752 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
753}
754
755static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700756vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500757{
Florin Coras99368312018-08-02 10:45:44 -0700758 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700759 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800760 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700761 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800762 vl_api_registration_t *reg;
Florin Coras99368312018-08-02 10:45:44 -0700763 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500764
Florin Corasfc804d92018-01-26 01:27:01 -0800765 reg = vl_api_client_index_to_registration (mp->client_index);
766 if (!reg)
767 return;
768
Florin Coras6cf30ad2017-04-04 23:08:23 -0700769 if (session_manager_is_enabled () == 0)
770 {
771 rv = VNET_API_ERROR_FEATURE_DISABLED;
772 goto done;
773 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500774
Florin Corasff6e7692017-12-11 04:59:01 -0800775 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700776 sizeof (mp->options),
777 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500778
Dave Barachb7b92992018-10-17 10:38:51 -0400779 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500780 a->api_client_index = mp->client_index;
781 a->options = mp->options;
Florin Coras52207f12018-07-12 14:48:06 -0700782
783 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
784 a->session_cb_vft = &session_mq_cb_vft;
785 else
786 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500787
Florin Corascea194d2017-10-02 00:18:51 -0700788 if (mp->namespace_id_len > 64)
789 {
790 rv = VNET_API_ERROR_INVALID_VALUE;
791 goto done;
792 }
793
794 if (mp->namespace_id_len)
795 {
Dave Wallace8af20542017-10-26 03:29:30 -0400796 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500797 clib_memcpy_fast (a->namespace_id, mp->namespace_id,
798 mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -0700799 }
800
Florin Corasc1a42652019-02-08 18:27:29 -0800801 if ((rv = vnet_application_attach (a)))
Florin Corascea194d2017-10-02 00:18:51 -0700802 {
Florin Corasc1a42652019-02-08 18:27:29 -0800803 clib_warning ("attach returned: %d", rv);
Florin Coras99368312018-08-02 10:45:44 -0700804 vec_free (a->namespace_id);
805 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700806 }
807 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500808
Florin Coras99368312018-08-02 10:45:44 -0700809 /* Send event queues segment */
810 if ((evt_q_segment = session_manager_get_evt_q_segment ()))
811 {
812 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
813 fds[n_fds] = evt_q_segment->fd;
814 n_fds += 1;
815 }
816 /* Send fifo segment fd if needed */
817 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
818 {
819 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
820 fds[n_fds] = a->segment->fd;
821 n_fds += 1;
822 }
823 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
824 {
825 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
826 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
827 n_fds += 1;
828 }
829
Florin Coras6cf30ad2017-04-04 23:08:23 -0700830done:
Florin Corasa5464812017-04-19 13:00:05 -0700831
Dave Barach68b0fb02017-02-28 15:15:56 -0500832 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700833 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500834 if (!rv)
835 {
Florin Corasb384b542018-01-15 01:08:33 -0800836 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800837 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500838 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800839 rmp->segment_size = segp->ssvm_size;
840 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500841 {
Florin Corasb384b542018-01-15 01:08:33 -0800842 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
843 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500844 }
Florin Coras99368312018-08-02 10:45:44 -0700845 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
846 rmp->n_fds = n_fds;
847 rmp->fd_flags = fd_flags;
Florin Corasd85de682018-11-29 17:02:29 -0800848 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500849 }
850 }));
851 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800852
Florin Coras99368312018-08-02 10:45:44 -0700853 if (n_fds)
854 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500855}
856
857static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700858vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
859{
860 vl_api_application_detach_reply_t *rmp;
861 int rv = VNET_API_ERROR_INVALID_VALUE_2;
862 vnet_app_detach_args_t _a, *a = &_a;
863 application_t *app;
864
865 if (session_manager_is_enabled () == 0)
866 {
867 rv = VNET_API_ERROR_FEATURE_DISABLED;
868 goto done;
869 }
870
871 app = application_lookup (mp->client_index);
872 if (app)
873 {
Florin Coras15531972018-08-12 23:50:53 -0700874 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800875 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700876 rv = vnet_application_detach (a);
877 }
878
879done:
880 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
881}
882
883static void
884vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
885{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700886 transport_connection_t *tc = 0;
Florin Corasc9940fc2019-02-05 20:55:11 -0800887 vnet_listen_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700888 vl_api_bind_uri_reply_t *rmp;
Florin Coras288eaab2019-02-03 15:26:14 -0800889 session_t *s;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700890 application_t *app = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700891 svm_msg_q_t *vpp_evt_q;
Florin Coras60116992018-08-27 09:52:18 -0700892 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700893 int rv;
894
895 if (session_manager_is_enabled () == 0)
896 {
897 rv = VNET_API_ERROR_FEATURE_DISABLED;
898 goto done;
899 }
900
901 app = application_lookup (mp->client_index);
902 if (app)
903 {
Dave Barachb7b92992018-10-17 10:38:51 -0400904 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700905 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700906 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700907 rv = vnet_bind_uri (a);
908 }
909 else
910 {
911 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
912 }
913
914done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700915
916 /* *INDENT-OFF* */
917 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
918 if (!rv)
919 {
920 rmp->handle = a->handle;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700921 if (app && application_has_global_scope (app))
922 {
Nathan Skrzypczakbb65ba12019-02-14 13:24:43 +0100923 app_listener_t* al = app_listener_get_w_handle(a->handle);
924 s = app_listener_get_session(al);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700925 tc = listen_session_get_transport (s);
Florin Coras6c354942018-04-18 00:05:21 -0700926 rmp->lcl_is_ip4 = tc->is_ip4;
927 rmp->lcl_port = tc->lcl_port;
Dave Barach178cf492018-11-13 16:34:13 -0500928 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700929 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
930 {
Florin Coras288eaab2019-02-03 15:26:14 -0800931 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
932 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700933 vpp_evt_q = session_manager_get_vpp_event_queue (0);
934 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
935 }
936 }
937 }
938 }));
939 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -0700940
941 /* If app uses mq for control messages, send an mq message as well */
942 if (app && application_use_mq_for_ctrl (app))
943 {
944 app_wrk = application_get_worker (app, 0);
945 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
946 rv);
947 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700948}
949
950static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500951vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
952{
953 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700954 application_t *app;
Florin Corasc1a42652019-02-08 18:27:29 -0800955 vnet_unlisten_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500956 int rv;
957
Florin Coras6cf30ad2017-04-04 23:08:23 -0700958 if (session_manager_is_enabled () == 0)
959 {
960 rv = VNET_API_ERROR_FEATURE_DISABLED;
961 goto done;
962 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500963
Florin Coras6cf30ad2017-04-04 23:08:23 -0700964 app = application_lookup (mp->client_index);
965 if (app)
966 {
967 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700968 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700969 rv = vnet_unbind_uri (a);
970 }
971 else
972 {
973 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
974 }
975
976done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500977 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
978}
979
Florin Corasf03a59a2017-06-09 21:07:32 -0700980static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500981vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
982{
Dave Wallace33e002b2017-09-06 01:20:02 -0400983 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500984 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700985 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700986 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500987
Florin Coras6cf30ad2017-04-04 23:08:23 -0700988 if (session_manager_is_enabled () == 0)
989 {
990 rv = VNET_API_ERROR_FEATURE_DISABLED;
991 goto done;
992 }
Florin Corase04c2992017-03-01 08:17:34 -0800993
Florin Coras6cf30ad2017-04-04 23:08:23 -0700994 app = application_lookup (mp->client_index);
995 if (app)
996 {
Dave Barachb7b92992018-10-17 10:38:51 -0400997 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700998 a->uri = (char *) mp->uri;
999 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001000 a->app_index = app->app_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001001 if ((rv = vnet_connect_uri (a)))
1002 clib_warning ("connect_uri returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001003 }
1004 else
1005 {
1006 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1007 }
Florin Corase04c2992017-03-01 08:17:34 -08001008
Florin Coras3cbc04b2017-10-02 00:18:51 -07001009 /*
1010 * Don't reply to stream (tcp) connects. The reply will come once
1011 * the connection is established. In case of the redirects, the reply
1012 * will come from the server app.
1013 */
Florin Coras8f89dd02018-03-05 16:53:07 -08001014 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001015 return;
1016
Florin Coras6cf30ad2017-04-04 23:08:23 -07001017done:
Florin Corase04c2992017-03-01 08:17:34 -08001018 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -04001019 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -08001020 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -05001021}
1022
1023static void
1024vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
1025{
1026 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001027 vnet_disconnect_args_t _a, *a = &_a;
1028 application_t *app;
1029 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001030
Florin Coras6cf30ad2017-04-04 23:08:23 -07001031 if (session_manager_is_enabled () == 0)
1032 {
1033 rv = VNET_API_ERROR_FEATURE_DISABLED;
1034 goto done;
1035 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001036
Florin Coras6cf30ad2017-04-04 23:08:23 -07001037 app = application_lookup (mp->client_index);
1038 if (app)
1039 {
1040 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001041 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001042 rv = vnet_disconnect_session (a);
1043 }
1044 else
1045 {
1046 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1047 }
1048
1049done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001050 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001051}
1052
1053static void
1054vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1055 mp)
1056{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001057 vnet_disconnect_args_t _a, *a = &_a;
1058 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001059
1060 /* Client objected to disconnecting the session, log and continue */
1061 if (mp->retval)
1062 {
1063 clib_warning ("client retval %d", mp->retval);
1064 return;
1065 }
1066
1067 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001068 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001069 if (app)
1070 {
1071 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001072 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001073 vnet_disconnect_session (a);
1074 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001075}
1076
1077static void
1078vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
1079{
Florin Coras4af830c2018-12-04 09:21:36 -08001080 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras15531972018-08-12 23:50:53 -07001081 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001082 application_t *app;
Florin Coras288eaab2019-02-03 15:26:14 -08001083 session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001084 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05001085
Ondrej Fabry6bd197e2018-08-15 08:46:46 +02001086 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001087 if (!app)
1088 return;
1089
Florin Corascea194d2017-10-02 00:18:51 -07001090 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001091 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -07001092 if (!s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001093 {
1094 clib_warning ("Invalid session!");
1095 return;
1096 }
1097
Florin Coras15531972018-08-12 23:50:53 -07001098 app_wrk = app_worker_get (s->app_wrk_index);
1099 if (app_wrk->app_index != app->app_index)
1100 {
1101 clib_warning ("app %u does not own handle 0x%lx", app->app_index,
1102 mp->handle);
1103 return;
1104 }
1105
Dave Barach68b0fb02017-02-28 15:15:56 -05001106 /* Client objected to resetting the session, log and continue */
1107 if (mp->retval)
1108 {
1109 clib_warning ("client retval %d", mp->retval);
1110 return;
1111 }
1112
Dave Barach68b0fb02017-02-28 15:15:56 -05001113 /* This comes as a response to a reset, transport only waiting for
1114 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -08001115 a->handle = mp->handle;
1116 a->app_index = app->app_index;
1117 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -05001118}
1119
1120static void
1121vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
1122{
Florin Corasf8f516a2018-02-08 15:10:09 -08001123 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
1124 local_session_t *ls;
Florin Coras288eaab2019-02-03 15:26:14 -08001125 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001126
Florin Corasa5464812017-04-19 13:00:05 -07001127 /* Server isn't interested, kill the session */
1128 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -05001129 {
Florin Corasa5464812017-04-19 13:00:05 -07001130 a->app_index = mp->context;
1131 a->handle = mp->handle;
1132 vnet_disconnect_session (a);
Florin Corasf8f516a2018-02-08 15:10:09 -08001133 return;
1134 }
1135
1136 if (session_handle_is_local (mp->handle))
1137 {
Florin Coras623eb562019-02-03 19:28:34 -08001138 ls = app_worker_get_local_session_from_handle (mp->handle);
Florin Coras15531972018-08-12 23:50:53 -07001139 if (!ls || ls->app_wrk_index != mp->context)
Florin Corasf8f516a2018-02-08 15:10:09 -08001140 {
1141 clib_warning ("server %u doesn't own local handle %llu",
1142 mp->context, mp->handle);
1143 return;
1144 }
Florin Coras623eb562019-02-03 19:28:34 -08001145 if (app_worker_local_session_connect_notify (ls))
Florin Corasf8f516a2018-02-08 15:10:09 -08001146 return;
1147 ls->session_state = SESSION_STATE_READY;
Dave Barach68b0fb02017-02-28 15:15:56 -05001148 }
Florin Corasa5464812017-04-19 13:00:05 -07001149 else
1150 {
Florin Corasf8f516a2018-02-08 15:10:09 -08001151 s = session_get_from_handle_if_valid (mp->handle);
Florin Corasa5464812017-04-19 13:00:05 -07001152 if (!s)
1153 {
1154 clib_warning ("session doesn't exist");
1155 return;
1156 }
Florin Coras15531972018-08-12 23:50:53 -07001157 if (s->app_wrk_index != mp->context)
Florin Corasa5464812017-04-19 13:00:05 -07001158 {
1159 clib_warning ("app doesn't own session");
1160 return;
1161 }
Florin Corasa5464812017-04-19 13:00:05 -07001162 s->session_state = SESSION_STATE_READY;
1163 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001164}
1165
1166static void
1167vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1168 * mp)
1169{
1170 clib_warning ("not implemented");
1171}
1172
1173static void
1174vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1175{
Florin Corasc9940fc2019-02-05 20:55:11 -08001176 vnet_listen_args_t _a, *a = &_a;
1177 transport_connection_t *tc = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001178 vl_api_bind_sock_reply_t *rmp;
Florin Corasc9940fc2019-02-05 20:55:11 -08001179 svm_msg_q_t *vpp_evt_q;
Florin Coraseb2945c2017-11-22 10:39:09 -08001180 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001181 app_worker_t *app_wrk;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001182 ip46_address_t *ip46;
Florin Corasc9940fc2019-02-05 20:55:11 -08001183 app_listener_t *al;
1184 session_t *s;
1185 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001186
Florin Coras6cf30ad2017-04-04 23:08:23 -07001187 if (session_manager_is_enabled () == 0)
1188 {
1189 rv = VNET_API_ERROR_FEATURE_DISABLED;
1190 goto done;
1191 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001192
Florin Coras6cf30ad2017-04-04 23:08:23 -07001193 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001194 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001195 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001196 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1197 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001198 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001199
1200 ip46 = (ip46_address_t *) mp->ip;
Dave Barachb7b92992018-10-17 10:38:51 -04001201 clib_memset (a, 0, sizeof (*a));
Florin Corasdcf55ce2017-11-16 15:32:50 -08001202 a->sep.is_ip4 = mp->is_ip4;
1203 a->sep.ip = *ip46;
1204 a->sep.port = mp->port;
1205 a->sep.fib_index = mp->vrf;
1206 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1207 a->sep.transport_proto = mp->proto;
Florin Coras15531972018-08-12 23:50:53 -07001208 a->app_index = app->app_index;
1209 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001210
Florin Corasc1a42652019-02-08 18:27:29 -08001211 if ((rv = vnet_listen (a)))
1212 clib_warning ("listen returned: %d", rv);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001213
Florin Coras6cf30ad2017-04-04 23:08:23 -07001214done:
Florin Corascea194d2017-10-02 00:18:51 -07001215 /* *INDENT-OFF* */
1216 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1217 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -08001218 {
1219 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -08001220 rmp->lcl_port = mp->port;
Keith Burns (alagalah)60ae80f2018-03-15 10:04:20 -07001221 rmp->lcl_is_ip4 = mp->is_ip4;
Dave Wallace97b1a272017-12-18 13:51:59 -05001222 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -08001223 {
Florin Corasc9940fc2019-02-05 20:55:11 -08001224 al = app_listener_get_w_handle (a->handle);
1225 s = app_listener_get_session (al);
Florin Coraseb2945c2017-11-22 10:39:09 -08001226 tc = listen_session_get_transport (s);
Dave Barach178cf492018-11-13 16:34:13 -05001227 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001228 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1229 {
Florin Coras288eaab2019-02-03 15:26:14 -08001230 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
1231 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001232 vpp_evt_q = session_manager_get_vpp_event_queue (0);
1233 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1234 }
Florin Coraseb2945c2017-11-22 10:39:09 -08001235 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001236 }
Florin Corascea194d2017-10-02 00:18:51 -07001237 }));
1238 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -07001239
1240 /* If app uses mq for control messages, send an mq message as well */
1241 if (app && application_use_mq_for_ctrl (app))
1242 {
1243 app_wrk = application_get_worker (app, mp->wrk_index);
1244 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1245 rv);
1246 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001247}
1248
1249static void
1250vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1251{
1252 vl_api_unbind_sock_reply_t *rmp;
Florin Corasc1a42652019-02-08 18:27:29 -08001253 vnet_unlisten_args_t _a, *a = &_a;
Florin Corasdfae9f92019-02-20 19:48:31 -08001254 app_worker_t *app_wrk;
1255 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001256 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001257
Florin Coras6cf30ad2017-04-04 23:08:23 -07001258 if (session_manager_is_enabled () == 0)
1259 {
1260 rv = VNET_API_ERROR_FEATURE_DISABLED;
1261 goto done;
1262 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001263
Florin Coras6cf30ad2017-04-04 23:08:23 -07001264 app = application_lookup (mp->client_index);
1265 if (app)
1266 {
Florin Coras15531972018-08-12 23:50:53 -07001267 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001268 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001269 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001270 if ((rv = vnet_unlisten (a)))
1271 clib_warning ("unlisten returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001272 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001273
Florin Coras6cf30ad2017-04-04 23:08:23 -07001274done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001275 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
Florin Corasdfae9f92019-02-20 19:48:31 -08001276
1277 /*
1278 * Send reply over msg queue
1279 */
1280 svm_msg_q_msg_t _msg, *msg = &_msg;
1281 session_unlisten_reply_msg_t *ump;
1282 svm_msg_q_t *app_mq;
1283 session_event_t *evt;
1284
1285 if (!app)
1286 return;
1287
1288 app_wrk = application_get_worker (app, a->wrk_map_index);
1289 if (!app_wrk)
1290 return;
1291
1292 app_mq = app_wrk->event_queue;
1293 if (mq_try_lock_and_alloc_msg (app_mq, msg))
1294 return;
1295
1296 evt = svm_msg_q_msg_data (app_mq, msg);
1297 clib_memset (evt, 0, sizeof (*evt));
1298 evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY;
1299 ump = (session_unlisten_reply_msg_t *) evt->data;
1300 ump->context = mp->context;
1301 ump->handle = mp->handle;
1302 ump->retval = rv;
1303 svm_msg_q_add_and_unlock (app_mq, msg);
Dave Barach68b0fb02017-02-28 15:15:56 -05001304}
1305
1306static void
1307vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1308{
Dave Wallace33e002b2017-09-06 01:20:02 -04001309 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001310 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001311 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001312 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001313
Florin Coras6cf30ad2017-04-04 23:08:23 -07001314 if (session_manager_is_enabled () == 0)
1315 {
1316 rv = VNET_API_ERROR_FEATURE_DISABLED;
1317 goto done;
1318 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001319
Florin Coras6cf30ad2017-04-04 23:08:23 -07001320 app = application_lookup (mp->client_index);
1321 if (app)
1322 {
Florin Corase86a8ed2018-01-05 03:20:25 -08001323 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -04001324 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001325
Dave Barachb7b92992018-10-17 10:38:51 -04001326 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001327 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1328 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -07001329 a->sep.is_ip4 = mp->is_ip4;
1330 a->sep.ip = *ip46;
1331 a->sep.port = mp->port;
1332 a->sep.transport_proto = mp->proto;
Florin Coras5665ced2018-10-25 18:03:45 -07001333 a->sep.peer.fib_index = mp->vrf;
1334 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras8f89dd02018-03-05 16:53:07 -08001335 if (mp->hostname_len)
1336 {
Florin Coras15531972018-08-12 23:50:53 -07001337 vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001338 clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
1339 mp->hostname_len);
Florin Coras8f89dd02018-03-05 16:53:07 -08001340 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001341 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001342 a->app_index = app->app_index;
1343 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001344 if ((rv = vnet_connect (a)))
1345 clib_warning ("connect returned: %u", rv);
Florin Coras15531972018-08-12 23:50:53 -07001346 vec_free (a->sep_ext.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001347 }
1348 else
1349 {
1350 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1351 }
Florin Corase04c2992017-03-01 08:17:34 -08001352
Florin Coras8f89dd02018-03-05 16:53:07 -08001353 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001354 return;
1355
1356 /* Got some error, relay it */
1357
Florin Coras6cf30ad2017-04-04 23:08:23 -07001358done:
Dave Wallace33e002b2017-09-06 01:20:02 -04001359 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corasab2f6db2018-08-31 14:31:41 -07001360
1361 if (app && application_use_mq_for_ctrl (app))
1362 {
1363 app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1364 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1365 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001366}
1367
Florin Corascea194d2017-10-02 00:18:51 -07001368static void
Florin Coras15531972018-08-12 23:50:53 -07001369vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1370{
1371 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1372 vl_api_app_worker_add_del_reply_t *rmp;
1373 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -07001374 application_t *app;
1375 u8 fd_flags = 0;
1376
1377 if (!session_manager_is_enabled ())
1378 {
1379 rv = VNET_API_ERROR_FEATURE_DISABLED;
1380 goto done;
1381 }
1382
1383 reg = vl_api_client_index_to_registration (mp->client_index);
1384 if (!reg)
1385 return;
1386
Florin Corasc1f5a432018-11-20 11:31:26 -08001387 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001388 if (!app)
1389 {
1390 rv = VNET_API_ERROR_INVALID_VALUE;
1391 goto done;
1392 }
1393
1394 vnet_app_worker_add_del_args_t args = {
1395 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001396 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001397 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001398 .is_add = mp->is_add
1399 };
Florin Corasc1a42652019-02-08 18:27:29 -08001400 rv = vnet_app_worker_add_del (&args);
1401 if (rv)
Florin Coras15531972018-08-12 23:50:53 -07001402 {
Florin Corasc1a42652019-02-08 18:27:29 -08001403 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -07001404 goto done;
1405 }
1406
Florin Coras14598772018-09-04 19:47:52 -07001407 if (!mp->is_add)
1408 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001409
Florin Coras15531972018-08-12 23:50:53 -07001410 /* Send fifo segment fd if needed */
1411 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1412 {
1413 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1414 fds[n_fds] = args.segment->fd;
1415 n_fds += 1;
1416 }
1417 if (application_segment_manager_properties (app)->use_mq_eventfd)
1418 {
1419 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1420 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1421 n_fds += 1;
1422 }
1423
1424 /* *INDENT-OFF* */
1425done:
1426 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1427 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001428 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001429 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001430 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001431 {
Florin Coras15531972018-08-12 23:50:53 -07001432 if (vec_len (args.segment->name))
1433 {
1434 memcpy (rmp->segment_name, args.segment->name,
1435 vec_len (args.segment->name));
1436 rmp->segment_name_length = vec_len (args.segment->name);
1437 }
1438 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1439 rmp->n_fds = n_fds;
1440 rmp->fd_flags = fd_flags;
1441 }
1442 }));
1443 /* *INDENT-ON* */
1444
1445 if (n_fds)
1446 session_send_fds (reg, fds, n_fds);
1447}
1448
1449static void
Florin Corascea194d2017-10-02 00:18:51 -07001450vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1451{
1452 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -08001453 u32 appns_index = 0;
1454 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001455 int rv = 0;
1456 if (!session_manager_is_enabled ())
1457 {
1458 rv = VNET_API_ERROR_FEATURE_DISABLED;
1459 goto done;
1460 }
1461
1462 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1463 {
1464 rv = VNET_API_ERROR_INVALID_VALUE;
1465 goto done;
1466 }
1467
1468 vec_validate (ns_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001469 clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -07001470 vnet_app_namespace_add_del_args_t args = {
1471 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001472 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001473 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1474 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1475 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1476 .is_add = 1
1477 };
Florin Corasc1a42652019-02-08 18:27:29 -08001478 rv = vnet_app_namespace_add_del (&args);
1479 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -08001480 {
1481 appns_index = app_namespace_index_from_id (ns_id);
1482 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1483 {
1484 clib_warning ("app ns lookup failed");
1485 rv = VNET_API_ERROR_UNSPECIFIED;
1486 }
1487 }
Florin Corascea194d2017-10-02 00:18:51 -07001488 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -08001489
1490 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -07001491done:
Florin Coras6e8c6672017-11-10 09:03:54 -08001492 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1493 if (!rv)
1494 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1495 }));
1496 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -07001497}
1498
Florin Coras1c710452017-10-17 00:03:13 -07001499static void
1500vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1501{
1502 vl_api_session_rule_add_del_reply_t *rmp;
1503 session_rule_add_del_args_t args;
1504 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -07001505 u8 fib_proto;
1506 int rv = 0;
1507
Dave Barachb7b92992018-10-17 10:38:51 -04001508 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001509 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1510
1511 table_args->lcl.fp_len = mp->lcl_plen;
1512 table_args->lcl.fp_proto = fib_proto;
1513 table_args->rmt.fp_len = mp->rmt_plen;
1514 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001515 table_args->lcl_port = mp->lcl_port;
1516 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001517 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1518 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001519 mp->tag[sizeof (mp->tag) - 1] = 0;
1520 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001521 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1522 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -08001523 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -07001524
Dave Barachb7b92992018-10-17 10:38:51 -04001525 clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1526 clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
Florin Coras1c710452017-10-17 00:03:13 -07001527 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1528 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
Florin Corasc1a42652019-02-08 18:27:29 -08001529 rv = vnet_session_rule_add_del (&args);
1530 if (rv)
1531 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -08001532 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001533 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1534}
1535
Florin Coras6c36f532017-11-03 18:32:34 -07001536static void
1537send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001538 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001539 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001540{
1541 vl_api_session_rules_details_t *rmp = 0;
1542 session_mask_or_match_4_t *match =
1543 (session_mask_or_match_4_t *) & rule->match;
1544 session_mask_or_match_4_t *mask =
1545 (session_mask_or_match_4_t *) & rule->mask;
1546
1547 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001548 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001549 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1550 rmp->context = context;
1551
1552 rmp->is_ip4 = 1;
Dave Barach178cf492018-11-13 16:34:13 -05001553 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1554 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001555 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1556 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001557 rmp->lcl_port = match->lcl_port;
1558 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001559 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1560 rmp->scope =
1561 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1562 rmp->transport_proto = transport_proto;
1563 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001564 if (tag)
1565 {
Dave Barach178cf492018-11-13 16:34:13 -05001566 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001567 rmp->tag[vec_len (tag)] = 0;
1568 }
Florin Coras6c36f532017-11-03 18:32:34 -07001569
Florin Coras6c4dae22018-01-09 06:39:23 -08001570 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001571}
1572
1573static void
Florin Corasc97a7392017-11-05 23:07:07 -08001574send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1575 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001576 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001577{
1578 vl_api_session_rules_details_t *rmp = 0;
1579 session_mask_or_match_6_t *match =
1580 (session_mask_or_match_6_t *) & rule->match;
1581 session_mask_or_match_6_t *mask =
1582 (session_mask_or_match_6_t *) & rule->mask;
1583
1584 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001585 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001586 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1587 rmp->context = context;
1588
1589 rmp->is_ip4 = 0;
Dave Barach178cf492018-11-13 16:34:13 -05001590 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1591 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001592 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1593 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001594 rmp->lcl_port = match->lcl_port;
1595 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001596 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001597 rmp->scope =
1598 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -07001599 rmp->transport_proto = transport_proto;
1600 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001601 if (tag)
1602 {
Dave Barach178cf492018-11-13 16:34:13 -05001603 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001604 rmp->tag[vec_len (tag)] = 0;
1605 }
Florin Coras6c36f532017-11-03 18:32:34 -07001606
Florin Coras6c4dae22018-01-09 06:39:23 -08001607 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001608}
1609
1610static void
1611send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001612 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001613 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001614{
1615 mma_rule_16_t *rule16;
1616 mma_rule_40_t *rule40;
1617 mma_rules_table_16_t *srt16;
1618 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001619 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001620
Florin Corasc97a7392017-11-05 23:07:07 -08001621 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001622 {
Florin Corasc97a7392017-11-05 23:07:07 -08001623 u8 *tag = 0;
1624 /* *INDENT-OFF* */
1625 srt16 = &srt->session_rules_tables_16;
1626 pool_foreach (rule16, srt16->rules, ({
1627 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1628 tag = session_rules_table_rule_tag (srt, ri, 1);
1629 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001630 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001631 }));
1632 /* *INDENT-ON* */
1633 }
1634 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1635 {
1636 u8 *tag = 0;
1637 /* *INDENT-OFF* */
1638 srt40 = &srt->session_rules_tables_40;
1639 pool_foreach (rule40, srt40->rules, ({
1640 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1641 tag = session_rules_table_rule_tag (srt, ri, 1);
1642 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001643 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001644 }));
1645 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001646 }
1647}
1648
1649static void
1650vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1651{
Florin Coras6c4dae22018-01-09 06:39:23 -08001652 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001653 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001654 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001655
Florin Coras6c4dae22018-01-09 06:39:23 -08001656 reg = vl_api_client_index_to_registration (mp->client_index);
1657 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001658 return;
1659
1660 /* *INDENT-OFF* */
1661 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001662 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1663 {
1664 send_session_rules_table_details (&st->session_rules[tp],
1665 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001666 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001667 mp->context);
1668 }
Florin Coras6c36f532017-11-03 18:32:34 -07001669 }));
1670 /* *INDENT-ON* */
1671}
1672
Florin Coras371ca502018-02-21 12:07:41 -08001673static void
1674vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1675 mp)
1676{
1677 vl_api_app_namespace_add_del_reply_t *rmp;
1678 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1679 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001680 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001681 u32 cert_len;
1682 int rv = 0;
1683 if (!session_manager_is_enabled ())
1684 {
1685 rv = VNET_API_ERROR_FEATURE_DISABLED;
1686 goto done;
1687 }
Florin Corase3e2f072018-03-04 07:24:30 -08001688 if (!(app = application_lookup (mp->client_index)))
1689 {
1690 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1691 goto done;
1692 }
Dave Barachb7b92992018-10-17 10:38:51 -04001693 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001694 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001695 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001696 if (cert_len > 10000)
1697 {
1698 rv = VNET_API_ERROR_INVALID_VALUE;
1699 goto done;
1700 }
Florin Coras371ca502018-02-21 12:07:41 -08001701 vec_validate (a->cert, cert_len);
Dave Barach178cf492018-11-13 16:34:13 -05001702 clib_memcpy_fast (a->cert, mp->cert, cert_len);
Florin Coras371ca502018-02-21 12:07:41 -08001703 if ((error = vnet_app_add_tls_cert (a)))
1704 {
1705 rv = clib_error_get_code (error);
1706 clib_error_report (error);
1707 }
1708 vec_free (a->cert);
1709done:
1710 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1711}
1712
1713static void
1714vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1715 mp)
1716{
1717 vl_api_app_namespace_add_del_reply_t *rmp;
1718 vnet_app_add_tls_key_args_t _a, *a = &_a;
1719 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001720 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001721 u32 key_len;
1722 int rv = 0;
1723 if (!session_manager_is_enabled ())
1724 {
1725 rv = VNET_API_ERROR_FEATURE_DISABLED;
1726 goto done;
1727 }
Florin Corase3e2f072018-03-04 07:24:30 -08001728 if (!(app = application_lookup (mp->client_index)))
1729 {
1730 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1731 goto done;
1732 }
Dave Barachb7b92992018-10-17 10:38:51 -04001733 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001734 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001735 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001736 if (key_len > 10000)
1737 {
1738 rv = VNET_API_ERROR_INVALID_VALUE;
1739 goto done;
1740 }
Florin Coras371ca502018-02-21 12:07:41 -08001741 vec_validate (a->key, key_len);
Dave Barach178cf492018-11-13 16:34:13 -05001742 clib_memcpy_fast (a->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001743 if ((error = vnet_app_add_tls_key (a)))
1744 {
1745 rv = clib_error_get_code (error);
1746 clib_error_report (error);
1747 }
1748 vec_free (a->key);
1749done:
1750 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1751}
1752
Florin Coras6cf30ad2017-04-04 23:08:23 -07001753static clib_error_t *
1754application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001755{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001756 application_t *app = application_lookup (client_index);
1757 vnet_app_detach_args_t _a, *a = &_a;
1758 if (app)
1759 {
Florin Coras15531972018-08-12 23:50:53 -07001760 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001761 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001762 vnet_application_detach (a);
1763 }
1764 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001765}
1766
Florin Coras6cf30ad2017-04-04 23:08:23 -07001767VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001768
1769#define vl_msg_name_crc_list
1770#include <vnet/vnet_all_api_h.h>
1771#undef vl_msg_name_crc_list
1772
1773static void
1774setup_message_id_table (api_main_t * am)
1775{
1776#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1777 foreach_vl_msg_name_crc_session;
1778#undef _
1779}
1780
1781/*
1782 * session_api_hookup
1783 * Add uri's API message handlers to the table.
1784 * vlib has alread mapped shared memory and
1785 * added the client registration handlers.
1786 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1787 */
1788static clib_error_t *
1789session_api_hookup (vlib_main_t * vm)
1790{
1791 api_main_t *am = &api_main;
1792
1793#define _(N,n) \
1794 vl_msg_api_set_handlers(VL_API_##N, #n, \
1795 vl_api_##n##_t_handler, \
1796 vl_noop_handler, \
1797 vl_api_##n##_t_endian, \
1798 vl_api_##n##_t_print, \
1799 sizeof(vl_api_##n##_t), 1);
1800 foreach_session_api_msg;
1801#undef _
1802
1803 /*
1804 * Messages which bounce off the data-plane to
1805 * an API client. Simply tells the message handling infra not
1806 * to free the message.
1807 *
1808 * Bounced message handlers MUST NOT block the data plane
1809 */
1810 am->message_bounce[VL_API_CONNECT_URI] = 1;
1811 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1812
1813 /*
1814 * Set up the (msg_name, crc, message-id) table
1815 */
1816 setup_message_id_table (am);
1817
1818 return 0;
1819}
1820
1821VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001822
Dave Barach68b0fb02017-02-28 15:15:56 -05001823/*
1824 * fd.io coding-style-patch-verification: ON
1825 *
1826 * Local Variables:
1827 * eval: (c-set-style "gnu")
1828 * End:
1829 */