blob: c4be556f63757ba85b55ba971c12f8a078dcb285 [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 }
Florin Coras31c99552019-03-01 13:00:58 -0800244 vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800245 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 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800255 ct_connection_t *ct;
256 ct = (ct_connection_t *) session_get_transport (s);
257 listener = listen_session_get (s->listener_index);
Florin Corasd4295e62019-02-22 13:11:38 -0800258 al = app_listener_get (server, listener->al_index);
259 mp->listener_handle = app_listener_handle (al);
260 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800261 mp->handle = session_handle (s);
262 mp->port = ct->c_rmt_port;
263 mp->vpp_event_queue_address = ct->client_evt_q;
264 mp->server_event_queue_address = ct->server_evt_q;
Florin Corasf8f516a2018-02-08 15:10:09 -0800265 }
Florin Corasb384b542018-01-15 01:08:33 -0800266 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500267
268 return 0;
269}
270
271static void
Florin Coras288eaab2019-02-03 15:26:14 -0800272send_session_disconnect_callback (session_t * s)
Dave Barach68b0fb02017-02-28 15:15:56 -0500273{
Florin Coras15531972018-08-12 23:50:53 -0700274 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Corasb384b542018-01-15 01:08:33 -0800275 vl_api_disconnect_session_t *mp;
276 vl_api_registration_t *reg;
Dave Barach68b0fb02017-02-28 15:15:56 -0500277
Florin Corasc1f5a432018-11-20 11:31:26 -0800278 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800279 if (!reg)
280 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800281 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800282 return;
283 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500284
Florin Corasb384b542018-01-15 01:08:33 -0800285 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400286 clib_memset (mp, 0, sizeof (*mp));
Dave Barach68b0fb02017-02-28 15:15:56 -0500287 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700288 mp->handle = session_handle (s);
Florin Corasc1f5a432018-11-20 11:31:26 -0800289 mp->context = app_wrk->api_client_index;
Florin Corasb384b542018-01-15 01:08:33 -0800290 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500291}
292
Florin Corasd79b41e2017-03-04 05:37:52 -0800293static void
Florin Coras288eaab2019-02-03 15:26:14 -0800294send_session_reset_callback (session_t * s)
Florin Corasd79b41e2017-03-04 05:37:52 -0800295{
Florin Coras15531972018-08-12 23:50:53 -0700296 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Corasb384b542018-01-15 01:08:33 -0800297 vl_api_registration_t *reg;
298 vl_api_reset_session_t *mp;
Florin Corasd79b41e2017-03-04 05:37:52 -0800299
Florin Corasc1f5a432018-11-20 11:31:26 -0800300 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800301 if (!reg)
302 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800303 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800304 return;
305 }
Florin Corasd79b41e2017-03-04 05:37:52 -0800306
Florin Corasb384b542018-01-15 01:08:33 -0800307 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -0400308 clib_memset (mp, 0, sizeof (*mp));
Florin Corasd79b41e2017-03-04 05:37:52 -0800309 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
Florin Coras3cbc04b2017-10-02 00:18:51 -0700310 mp->handle = session_handle (s);
Florin Corasb384b542018-01-15 01:08:33 -0800311 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Florin Corasd79b41e2017-03-04 05:37:52 -0800312}
313
Dave Barach0194f1a2017-05-15 10:11:39 -0400314int
Florin Coras15531972018-08-12 23:50:53 -0700315send_session_connected_callback (u32 app_wrk_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800316 session_t * s, u8 is_fail)
Dave Barach68b0fb02017-02-28 15:15:56 -0500317{
Dave Wallace33e002b2017-09-06 01:20:02 -0400318 vl_api_connect_session_reply_t *mp;
Florin Corasade70e42017-10-14 18:56:41 -0700319 transport_connection_t *tc;
Florin Corasb384b542018-01-15 01:08:33 -0800320 vl_api_registration_t *reg;
Florin Coras3c2fed52018-07-04 04:15:05 -0700321 svm_msg_q_t *vpp_queue;
Florin Coras15531972018-08-12 23:50:53 -0700322 app_worker_t *app_wrk;
Dave Barach68b0fb02017-02-28 15:15:56 -0500323
Florin Coras15531972018-08-12 23:50:53 -0700324 app_wrk = app_worker_get (app_wrk_index);
Florin Corasc1f5a432018-11-20 11:31:26 -0800325 reg = vl_mem_api_client_index_to_registration (app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800326 if (!reg)
327 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800328 clib_warning ("no registration: %u", app_wrk->api_client_index);
Florin Corasb384b542018-01-15 01:08:33 -0800329 return -1;
330 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500331
Florin Corasb384b542018-01-15 01:08:33 -0800332 mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
Dave Wallace33e002b2017-09-06 01:20:02 -0400333 mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700334 mp->context = api_context;
Dave Wallace965fec92017-10-17 16:19:41 -0400335
336 if (is_fail)
337 goto done;
338
Florin Corasf8f516a2018-02-08 15:10:09 -0800339 if (session_has_transport (s))
Dave Barach68b0fb02017-02-28 15:15:56 -0500340 {
Florin Corasf8f516a2018-02-08 15:10:09 -0800341 tc = session_get_transport (s);
342 if (!tc)
343 {
344 is_fail = 1;
345 goto done;
346 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500347
Florin Coras31c99552019-03-01 13:00:58 -0800348 vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
Florin Corasf8f516a2018-02-08 15:10:09 -0800349 mp->handle = session_handle (s);
350 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Dave Barach178cf492018-11-13 16:34:13 -0500351 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Corasf8f516a2018-02-08 15:10:09 -0800352 mp->is_ip4 = tc->is_ip4;
353 mp->lcl_port = tc->lcl_port;
Florin Coras288eaab2019-02-03 15:26:14 -0800354 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
355 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800356 }
357 else
358 {
Florin Coras2b81e3c2019-02-27 07:55:46 -0800359 ct_connection_t *ct;
360 ct = (ct_connection_t *) session_get_transport (s);
361 mp->handle = session_handle (s);
362 mp->lcl_port = ct->c_lcl_port;
363 mp->vpp_event_queue_address = ct->server_evt_q;
364 mp->client_event_queue_address = ct->client_evt_q;
Florin Coras288eaab2019-02-03 15:26:14 -0800365 mp->server_rx_fifo = pointer_to_uword (s->tx_fifo);
366 mp->server_tx_fifo = pointer_to_uword (s->rx_fifo);
Florin Corasf8f516a2018-02-08 15:10:09 -0800367 }
Dave Wallace965fec92017-10-17 16:19:41 -0400368
369done:
370 mp->retval = is_fail ?
371 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
Florin Corasb384b542018-01-15 01:08:33 -0800372 vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
Dave Barach68b0fb02017-02-28 15:15:56 -0500373 return 0;
374}
375
Florin Corascea194d2017-10-02 00:18:51 -0700376static session_cb_vft_t session_cb_vft = {
Dave Barach68b0fb02017-02-28 15:15:56 -0500377 .session_accept_callback = send_session_accept_callback,
378 .session_disconnect_callback = send_session_disconnect_callback,
379 .session_connected_callback = send_session_connected_callback,
Florin Corasd79b41e2017-03-04 05:37:52 -0800380 .session_reset_callback = send_session_reset_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500381 .add_segment_callback = send_add_segment_callback,
Florin Corasf8f516a2018-02-08 15:10:09 -0800382 .del_segment_callback = send_del_segment_callback,
Dave Barach68b0fb02017-02-28 15:15:56 -0500383};
384
Florin Coras52207f12018-07-12 14:48:06 -0700385static int
Florin Coras83ea6692018-10-12 16:55:14 -0700386mq_try_lock_and_alloc_msg (svm_msg_q_t * app_mq, svm_msg_q_msg_t * msg)
387{
388 int rv;
389 u8 try = 0;
390 while (try < 100)
391 {
392 rv = svm_msg_q_lock_and_alloc_msg_w_ring (app_mq,
393 SESSION_MQ_CTRL_EVT_RING,
394 SVM_Q_NOWAIT, msg);
395 if (!rv)
396 return 0;
397 try++;
Florin Corase2ea1932018-12-17 23:08:14 -0800398 usleep (1);
Florin Coras83ea6692018-10-12 16:55:14 -0700399 }
Florin Corasdc2e2512018-12-03 17:47:26 -0800400 clib_warning ("failed to alloc msg");
Florin Coras83ea6692018-10-12 16:55:14 -0700401 return -1;
402}
403
404static int
Florin Coras288eaab2019-02-03 15:26:14 -0800405mq_send_session_accepted_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700406{
Florin Coras15531972018-08-12 23:50:53 -0700407 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
Florin Coras52207f12018-07-12 14:48:06 -0700408 svm_msg_q_msg_t _msg, *msg = &_msg;
409 svm_msg_q_t *vpp_queue, *app_mq;
Florin Coras52207f12018-07-12 14:48:06 -0700410 transport_connection_t *tc;
Florin Coras288eaab2019-02-03 15:26:14 -0800411 session_t *listener;
Florin Coras52207f12018-07-12 14:48:06 -0700412 session_accepted_msg_t *mp;
413 session_event_t *evt;
Florin Coras15531972018-08-12 23:50:53 -0700414 application_t *app;
Florin Corasc9940fc2019-02-05 20:55:11 -0800415 app_listener_t *al;
Florin Coras52207f12018-07-12 14:48:06 -0700416
Florin Coras15531972018-08-12 23:50:53 -0700417 app = application_get (app_wrk->app_index);
418 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700419 if (mq_try_lock_and_alloc_msg (app_mq, msg))
420 return -1;
Florin Coras52207f12018-07-12 14:48:06 -0700421
422 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400423 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700424 evt->event_type = SESSION_CTRL_EVT_ACCEPTED;
425 mp = (session_accepted_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800426 clib_memset (mp, 0, sizeof (*mp));
Florin Corasab2f6db2018-08-31 14:31:41 -0700427 mp->context = app->app_index;
Florin Coras288eaab2019-02-03 15:26:14 -0800428 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
429 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Corasfa76a762018-11-29 12:40:10 -0800430 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700431
432 if (session_has_transport (s))
433 {
434 listener = listen_session_get (s->listener_index);
Florin Corasc9940fc2019-02-05 20:55:11 -0800435 al = app_listener_get (app, listener->al_index);
436 mp->listener_handle = app_listener_handle (al);
Florin Coras52207f12018-07-12 14:48:06 -0700437 if (application_is_proxy (app))
438 {
439 listener =
Florin Coras15531972018-08-12 23:50:53 -0700440 app_worker_first_listener (app_wrk, session_get_fib_proto (s),
441 session_get_transport_proto (s));
Florin Coras52207f12018-07-12 14:48:06 -0700442 if (listener)
443 mp->listener_handle = listen_session_get_handle (listener);
444 }
Florin Coras31c99552019-03-01 13:00:58 -0800445 vpp_queue = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700446 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
447 mp->handle = session_handle (s);
Florin Coras1ee78302019-02-05 15:51:15 -0800448 tc = transport_get_connection (session_get_transport_proto (s),
449 s->connection_index, s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700450 mp->port = tc->rmt_port;
451 mp->is_ip4 = tc->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500452 clib_memcpy_fast (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700453 }
454 else
455 {
Florin Coras99368312018-08-02 10:45:44 -0700456 u8 main_thread = vlib_num_workers ()? 1 : 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800457 ct_connection_t *ct;
Florin Coras99368312018-08-02 10:45:44 -0700458
Florin Coras2b81e3c2019-02-27 07:55:46 -0800459 ct = (ct_connection_t *) session_get_transport (s);
Florin Corasc1f5a432018-11-20 11:31:26 -0800460 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700461 app_wrk->wrk_map_index,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800462 ct->server_evt_q,
463 ct->client_evt_q);
Florin Coras99368312018-08-02 10:45:44 -0700464
Florin Coras2b81e3c2019-02-27 07:55:46 -0800465 listener = listen_session_get (s->listener_index);
Florin Corasd4295e62019-02-22 13:11:38 -0800466 al = app_listener_get (app, listener->al_index);
467 mp->listener_handle = app_listener_handle (al);
468 mp->is_ip4 = session_type_is_ip4 (listener->session_type);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800469 mp->handle = session_handle (s);
470 mp->port = ct->c_rmt_port;
Florin Coras31c99552019-03-01 13:00:58 -0800471 vpp_queue = session_main_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700472 mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800473 mp->client_event_queue_address = ct->client_evt_q;
474 mp->server_event_queue_address = ct->server_evt_q;
Florin Coras52207f12018-07-12 14:48:06 -0700475 }
Florin Coras537b17e2018-09-28 10:35:45 -0700476 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700477
478 return 0;
479}
480
Florin Coras72b04282019-01-14 17:23:11 -0800481static inline void
482mq_send_session_close_evt (app_worker_t * app_wrk, session_handle_t sh,
483 session_evt_type_t evt_type)
Florin Coras52207f12018-07-12 14:48:06 -0700484{
Florin Coras52207f12018-07-12 14:48:06 -0700485 svm_msg_q_msg_t _msg, *msg = &_msg;
486 session_disconnected_msg_t *mp;
487 svm_msg_q_t *app_mq;
488 session_event_t *evt;
489
Florin Coras15531972018-08-12 23:50:53 -0700490 app_mq = app_wrk->event_queue;
Florin Coras83ea6692018-10-12 16:55:14 -0700491 if (mq_try_lock_and_alloc_msg (app_mq, msg))
492 return;
Florin Coras52207f12018-07-12 14:48:06 -0700493 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400494 clib_memset (evt, 0, sizeof (*evt));
Florin Coras72b04282019-01-14 17:23:11 -0800495 evt->event_type = evt_type;
Florin Coras52207f12018-07-12 14:48:06 -0700496 mp = (session_disconnected_msg_t *) evt->data;
Florin Coras72b04282019-01-14 17:23:11 -0800497 mp->handle = sh;
Florin Corasc1f5a432018-11-20 11:31:26 -0800498 mp->context = app_wrk->api_client_index;
Florin Coras537b17e2018-09-28 10:35:45 -0700499 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700500}
501
Florin Coras72b04282019-01-14 17:23:11 -0800502static inline void
503mq_notify_close_subscribers (u32 app_index, session_handle_t sh,
504 svm_fifo_t * f, session_evt_type_t evt_type)
505{
506 app_worker_t *app_wrk;
507 application_t *app;
508 int i;
509
510 app = application_get (app_index);
511 if (!app)
512 return;
513
514 for (i = 0; i < f->n_subscribers; i++)
515 {
516 if (!(app_wrk = application_get_worker (app, f->subscribers[i])))
517 continue;
518 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_DISCONNECTED);
519 }
520}
521
522static void
Florin Coras288eaab2019-02-03 15:26:14 -0800523mq_send_session_disconnected_cb (session_t * s)
Florin Coras72b04282019-01-14 17:23:11 -0800524{
525 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
526 session_handle_t sh = session_handle (s);
527
528 mq_send_session_close_evt (app_wrk, session_handle (s),
529 SESSION_CTRL_EVT_DISCONNECTED);
530
Florin Coras288eaab2019-02-03 15:26:14 -0800531 if (svm_fifo_n_subscribers (s->rx_fifo))
532 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800533 SESSION_CTRL_EVT_DISCONNECTED);
534}
535
Florin Coras52207f12018-07-12 14:48:06 -0700536static void
Florin Coras288eaab2019-02-03 15:26:14 -0800537mq_send_session_reset_cb (session_t * s)
Florin Coras52207f12018-07-12 14:48:06 -0700538{
Florin Coras72b04282019-01-14 17:23:11 -0800539 app_worker_t *app_wrk = app_worker_get (s->app_wrk_index);
540 session_handle_t sh = session_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700541
Florin Coras72b04282019-01-14 17:23:11 -0800542 mq_send_session_close_evt (app_wrk, sh, SESSION_CTRL_EVT_RESET);
543
Florin Coras288eaab2019-02-03 15:26:14 -0800544 if (svm_fifo_n_subscribers (s->rx_fifo))
545 mq_notify_close_subscribers (app_wrk->app_index, sh, s->rx_fifo,
Florin Coras72b04282019-01-14 17:23:11 -0800546 SESSION_CTRL_EVT_RESET);
Florin Coras52207f12018-07-12 14:48:06 -0700547}
548
549static int
Florin Coras15531972018-08-12 23:50:53 -0700550mq_send_session_connected_cb (u32 app_wrk_index, u32 api_context,
Florin Coras288eaab2019-02-03 15:26:14 -0800551 session_t * s, u8 is_fail)
Florin Coras52207f12018-07-12 14:48:06 -0700552{
553 svm_msg_q_msg_t _msg, *msg = &_msg;
554 session_connected_msg_t *mp;
555 svm_msg_q_t *vpp_mq, *app_mq;
556 transport_connection_t *tc;
Florin Coras15531972018-08-12 23:50:53 -0700557 app_worker_t *app_wrk;
Florin Coras52207f12018-07-12 14:48:06 -0700558 session_event_t *evt;
Florin Coras52207f12018-07-12 14:48:06 -0700559
Florin Coras15531972018-08-12 23:50:53 -0700560 app_wrk = app_worker_get (app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -0700561 app_mq = app_wrk->event_queue;
Florin Coras52207f12018-07-12 14:48:06 -0700562 if (!app_mq)
563 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800564 clib_warning ("app %u with api index: %u not attached",
565 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras52207f12018-07-12 14:48:06 -0700566 return -1;
567 }
568
Florin Coras83ea6692018-10-12 16:55:14 -0700569 if (mq_try_lock_and_alloc_msg (app_mq, msg))
570 return -1;
Florin Corasdc2e2512018-12-03 17:47:26 -0800571
Florin Coras52207f12018-07-12 14:48:06 -0700572 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400573 clib_memset (evt, 0, sizeof (*evt));
Florin Coras52207f12018-07-12 14:48:06 -0700574 evt->event_type = SESSION_CTRL_EVT_CONNECTED;
575 mp = (session_connected_msg_t *) evt->data;
Florin Coras87c65702019-01-26 14:33:26 -0800576 clib_memset (mp, 0, sizeof (*mp));
Florin Coras52207f12018-07-12 14:48:06 -0700577 mp->context = api_context;
578
579 if (is_fail)
580 goto done;
581
582 if (session_has_transport (s))
583 {
584 tc = session_get_transport (s);
585 if (!tc)
586 {
587 is_fail = 1;
588 goto done;
589 }
590
Florin Coras31c99552019-03-01 13:00:58 -0800591 vpp_mq = session_main_get_vpp_event_queue (s->thread_index);
Florin Coras52207f12018-07-12 14:48:06 -0700592 mp->handle = session_handle (s);
593 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Dave Barach178cf492018-11-13 16:34:13 -0500594 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras52207f12018-07-12 14:48:06 -0700595 mp->is_ip4 = tc->is_ip4;
596 mp->lcl_port = tc->lcl_port;
Florin Coras288eaab2019-02-03 15:26:14 -0800597 mp->server_rx_fifo = pointer_to_uword (s->rx_fifo);
598 mp->server_tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800599 mp->segment_handle = session_segment_handle (s);
Florin Coras52207f12018-07-12 14:48:06 -0700600 }
601 else
602 {
Florin Coras99368312018-08-02 10:45:44 -0700603 u8 main_thread = vlib_num_workers ()? 1 : 0;
Florin Coras2b81e3c2019-02-27 07:55:46 -0800604 ct_connection_t *cct;
605 session_t *ss;
Florin Coras99368312018-08-02 10:45:44 -0700606
Florin Coras2b81e3c2019-02-27 07:55:46 -0800607 cct = (ct_connection_t *) session_get_transport (s);
Florin Corasc1f5a432018-11-20 11:31:26 -0800608 send_app_cut_through_registration_add (app_wrk->api_client_index,
Florin Coras134a9962018-08-28 11:32:04 -0700609 app_wrk->wrk_map_index,
Florin Coras2b81e3c2019-02-27 07:55:46 -0800610 cct->client_evt_q,
611 cct->server_evt_q);
Florin Coras99368312018-08-02 10:45:44 -0700612
Florin Coras2b81e3c2019-02-27 07:55:46 -0800613 mp->handle = session_handle (s);
614 mp->lcl_port = cct->c_lcl_port;
Florin Coras31c99552019-03-01 13:00:58 -0800615 vpp_mq = session_main_get_vpp_event_queue (main_thread);
Florin Coras54693d22018-07-17 10:46:29 -0700616 mp->vpp_event_queue_address = pointer_to_uword (vpp_mq);
Florin Coras2b81e3c2019-02-27 07:55:46 -0800617 mp->client_event_queue_address = cct->client_evt_q;
618 mp->server_event_queue_address = cct->server_evt_q;
619 ss = ct_session_get_peer (s);
620 mp->server_rx_fifo = pointer_to_uword (ss->tx_fifo);
621 mp->server_tx_fifo = pointer_to_uword (ss->rx_fifo);
622 mp->segment_handle = session_segment_handle (ss);
Florin Coras52207f12018-07-12 14:48:06 -0700623 }
624
625done:
626 mp->retval = is_fail ?
627 clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
628
Florin Coras537b17e2018-09-28 10:35:45 -0700629 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras52207f12018-07-12 14:48:06 -0700630 return 0;
631}
632
Florin Coras60116992018-08-27 09:52:18 -0700633static int
634mq_send_session_bound_cb (u32 app_wrk_index, u32 api_context,
635 session_handle_t handle, int rv)
636{
637 svm_msg_q_msg_t _msg, *msg = &_msg;
638 svm_msg_q_t *app_mq, *vpp_evt_q;
639 transport_connection_t *tc;
Florin Coras60116992018-08-27 09:52:18 -0700640 session_bound_msg_t *mp;
641 app_worker_t *app_wrk;
642 session_event_t *evt;
Florin Corasc9940fc2019-02-05 20:55:11 -0800643 app_listener_t *al;
644 session_t *ls = 0;
Florin Coras60116992018-08-27 09:52:18 -0700645
646 app_wrk = app_worker_get (app_wrk_index);
Florin Coras60116992018-08-27 09:52:18 -0700647 app_mq = app_wrk->event_queue;
648 if (!app_mq)
649 {
Florin Corasc1f5a432018-11-20 11:31:26 -0800650 clib_warning ("app %u with api index: %u not attached",
651 app_wrk->app_index, app_wrk->api_client_index);
Florin Coras60116992018-08-27 09:52:18 -0700652 return -1;
653 }
654
Florin Coras83ea6692018-10-12 16:55:14 -0700655 if (mq_try_lock_and_alloc_msg (app_mq, msg))
656 return -1;
657
Florin Coras60116992018-08-27 09:52:18 -0700658 evt = svm_msg_q_msg_data (app_mq, msg);
Dave Barachb7b92992018-10-17 10:38:51 -0400659 clib_memset (evt, 0, sizeof (*evt));
Florin Coras60116992018-08-27 09:52:18 -0700660 evt->event_type = SESSION_CTRL_EVT_BOUND;
661 mp = (session_bound_msg_t *) evt->data;
662 mp->context = api_context;
663
664 if (rv)
665 goto done;
666
667 mp->handle = handle;
Florin Corasd4295e62019-02-22 13:11:38 -0800668 al = app_listener_get_w_handle (handle);
669 if (al->session_index != SESSION_INVALID_INDEX)
670 ls = app_listener_get_session (al);
Florin Coras60116992018-08-27 09:52:18 -0700671 else
Florin Corasd4295e62019-02-22 13:11:38 -0800672 ls = app_listener_get_local_session (al);
673 tc = listen_session_get_transport (ls);
674 mp->lcl_port = tc->lcl_port;
675 mp->lcl_is_ip4 = tc->is_ip4;
676 clib_memcpy_fast (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras60116992018-08-27 09:52:18 -0700677
Florin Coras31c99552019-03-01 13:00:58 -0800678 vpp_evt_q = session_main_get_vpp_event_queue (0);
Florin Coras5f45e012019-01-23 09:21:30 -0800679 mp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
680
Florin Coras2b81e3c2019-02-27 07:55:46 -0800681 if (session_transport_service_type (ls) == TRANSPORT_SERVICE_CL)
Florin Coras60116992018-08-27 09:52:18 -0700682 {
Florin Coras288eaab2019-02-03 15:26:14 -0800683 mp->rx_fifo = pointer_to_uword (ls->rx_fifo);
684 mp->tx_fifo = pointer_to_uword (ls->tx_fifo);
Florin Coras60116992018-08-27 09:52:18 -0700685 }
686
687done:
688 mp->retval = rv;
Florin Coras537b17e2018-09-28 10:35:45 -0700689 svm_msg_q_add_and_unlock (app_mq, msg);
Florin Coras60116992018-08-27 09:52:18 -0700690 return 0;
691}
692
Florin Coras52207f12018-07-12 14:48:06 -0700693static session_cb_vft_t session_mq_cb_vft = {
694 .session_accept_callback = mq_send_session_accepted_cb,
695 .session_disconnect_callback = mq_send_session_disconnected_cb,
696 .session_connected_callback = mq_send_session_connected_cb,
697 .session_reset_callback = mq_send_session_reset_cb,
698 .add_segment_callback = send_add_segment_callback,
699 .del_segment_callback = send_del_segment_callback,
700};
701
Dave Barach68b0fb02017-02-28 15:15:56 -0500702static void
Florin Corase04c2992017-03-01 08:17:34 -0800703vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
704{
705 vl_api_session_enable_disable_reply_t *rmp;
706 vlib_main_t *vm = vlib_get_main ();
707 int rv = 0;
708
709 vnet_session_enable_disable (vm, mp->is_enable);
710 REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
711}
712
713static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700714vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
Dave Barach68b0fb02017-02-28 15:15:56 -0500715{
Florin Coras99368312018-08-02 10:45:44 -0700716 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700717 vl_api_application_attach_reply_t *rmp;
Florin Corasb384b542018-01-15 01:08:33 -0800718 ssvm_private_t *segp, *evt_q_segment;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700719 vnet_app_attach_args_t _a, *a = &_a;
Florin Corasb384b542018-01-15 01:08:33 -0800720 vl_api_registration_t *reg;
Florin Coras99368312018-08-02 10:45:44 -0700721 u8 fd_flags = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500722
Florin Corasfc804d92018-01-26 01:27:01 -0800723 reg = vl_api_client_index_to_registration (mp->client_index);
724 if (!reg)
725 return;
726
Florin Coras31c99552019-03-01 13:00:58 -0800727 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700728 {
729 rv = VNET_API_ERROR_FEATURE_DISABLED;
730 goto done;
731 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500732
Florin Corasff6e7692017-12-11 04:59:01 -0800733 STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
Florin Coras6cf30ad2017-04-04 23:08:23 -0700734 sizeof (mp->options),
735 "Out of options, fix api message definition");
Dave Barach68b0fb02017-02-28 15:15:56 -0500736
Dave Barachb7b92992018-10-17 10:38:51 -0400737 clib_memset (a, 0, sizeof (*a));
Dave Barach68b0fb02017-02-28 15:15:56 -0500738 a->api_client_index = mp->client_index;
739 a->options = mp->options;
Florin Coras52207f12018-07-12 14:48:06 -0700740
741 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS)
742 a->session_cb_vft = &session_mq_cb_vft;
743 else
744 a->session_cb_vft = &session_cb_vft;
Dave Barach68b0fb02017-02-28 15:15:56 -0500745
Florin Corascea194d2017-10-02 00:18:51 -0700746 if (mp->namespace_id_len > 64)
747 {
748 rv = VNET_API_ERROR_INVALID_VALUE;
749 goto done;
750 }
751
752 if (mp->namespace_id_len)
753 {
Dave Wallace8af20542017-10-26 03:29:30 -0400754 vec_validate (a->namespace_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -0500755 clib_memcpy_fast (a->namespace_id, mp->namespace_id,
756 mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -0700757 }
758
Florin Corasc1a42652019-02-08 18:27:29 -0800759 if ((rv = vnet_application_attach (a)))
Florin Corascea194d2017-10-02 00:18:51 -0700760 {
Florin Corasc1a42652019-02-08 18:27:29 -0800761 clib_warning ("attach returned: %d", rv);
Florin Coras99368312018-08-02 10:45:44 -0700762 vec_free (a->namespace_id);
763 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700764 }
765 vec_free (a->namespace_id);
Dave Barach68b0fb02017-02-28 15:15:56 -0500766
Florin Coras99368312018-08-02 10:45:44 -0700767 /* Send event queues segment */
Florin Coras31c99552019-03-01 13:00:58 -0800768 if ((evt_q_segment = session_main_get_evt_q_segment ()))
Florin Coras99368312018-08-02 10:45:44 -0700769 {
770 fd_flags |= SESSION_FD_F_VPP_MQ_SEGMENT;
771 fds[n_fds] = evt_q_segment->fd;
772 n_fds += 1;
773 }
774 /* Send fifo segment fd if needed */
775 if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
776 {
777 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
778 fds[n_fds] = a->segment->fd;
779 n_fds += 1;
780 }
781 if (a->options[APP_OPTIONS_FLAGS] & APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD)
782 {
783 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
784 fds[n_fds] = svm_msg_q_get_producer_eventfd (a->app_evt_q);
785 n_fds += 1;
786 }
787
Florin Coras6cf30ad2017-04-04 23:08:23 -0700788done:
Florin Corasa5464812017-04-19 13:00:05 -0700789
Dave Barach68b0fb02017-02-28 15:15:56 -0500790 /* *INDENT-OFF* */
Florin Coras6cf30ad2017-04-04 23:08:23 -0700791 REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
Dave Barach68b0fb02017-02-28 15:15:56 -0500792 if (!rv)
793 {
Florin Corasb384b542018-01-15 01:08:33 -0800794 segp = a->segment;
Florin Corasc1f5a432018-11-20 11:31:26 -0800795 rmp->app_index = clib_host_to_net_u32 (a->app_index);
Dave Barach68b0fb02017-02-28 15:15:56 -0500796 rmp->segment_name_length = 0;
Florin Corasb384b542018-01-15 01:08:33 -0800797 rmp->segment_size = segp->ssvm_size;
798 if (vec_len (segp->name))
Dave Barach68b0fb02017-02-28 15:15:56 -0500799 {
Florin Corasb384b542018-01-15 01:08:33 -0800800 memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
801 rmp->segment_name_length = vec_len (segp->name);
Dave Barach68b0fb02017-02-28 15:15:56 -0500802 }
Florin Coras99368312018-08-02 10:45:44 -0700803 rmp->app_event_queue_address = pointer_to_uword (a->app_evt_q);
804 rmp->n_fds = n_fds;
805 rmp->fd_flags = fd_flags;
Florin Corasd85de682018-11-29 17:02:29 -0800806 rmp->segment_handle = clib_host_to_net_u64 (a->segment_handle);
Dave Barach68b0fb02017-02-28 15:15:56 -0500807 }
808 }));
809 /* *INDENT-ON* */
Florin Corasb384b542018-01-15 01:08:33 -0800810
Florin Coras99368312018-08-02 10:45:44 -0700811 if (n_fds)
812 session_send_fds (reg, fds, n_fds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500813}
814
815static void
Florin Coras6cf30ad2017-04-04 23:08:23 -0700816vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
817{
818 vl_api_application_detach_reply_t *rmp;
819 int rv = VNET_API_ERROR_INVALID_VALUE_2;
820 vnet_app_detach_args_t _a, *a = &_a;
821 application_t *app;
822
Florin Coras31c99552019-03-01 13:00:58 -0800823 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700824 {
825 rv = VNET_API_ERROR_FEATURE_DISABLED;
826 goto done;
827 }
828
829 app = application_lookup (mp->client_index);
830 if (app)
831 {
Florin Coras15531972018-08-12 23:50:53 -0700832 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800833 a->api_client_index = mp->client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700834 rv = vnet_application_detach (a);
835 }
836
837done:
838 REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
839}
840
841static void
842vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
843{
Florin Coras7fb0fe12018-04-09 09:24:52 -0700844 transport_connection_t *tc = 0;
Florin Corasc9940fc2019-02-05 20:55:11 -0800845 vnet_listen_args_t _a, *a = &_a;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700846 vl_api_bind_uri_reply_t *rmp;
Florin Coras288eaab2019-02-03 15:26:14 -0800847 session_t *s;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700848 application_t *app = 0;
Florin Coras3c2fed52018-07-04 04:15:05 -0700849 svm_msg_q_t *vpp_evt_q;
Florin Coras60116992018-08-27 09:52:18 -0700850 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700851 int rv;
852
Florin Coras31c99552019-03-01 13:00:58 -0800853 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700854 {
855 rv = VNET_API_ERROR_FEATURE_DISABLED;
856 goto done;
857 }
858
859 app = application_lookup (mp->client_index);
860 if (app)
861 {
Dave Barachb7b92992018-10-17 10:38:51 -0400862 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700863 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700864 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700865 rv = vnet_bind_uri (a);
866 }
867 else
868 {
869 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
870 }
871
872done:
Florin Coras7fb0fe12018-04-09 09:24:52 -0700873
874 /* *INDENT-OFF* */
875 REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({
876 if (!rv)
877 {
878 rmp->handle = a->handle;
Florin Coras7fb0fe12018-04-09 09:24:52 -0700879 if (app && application_has_global_scope (app))
880 {
Nathan Skrzypczakbb65ba12019-02-14 13:24:43 +0100881 app_listener_t* al = app_listener_get_w_handle(a->handle);
882 s = app_listener_get_session(al);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700883 tc = listen_session_get_transport (s);
Florin Coras6c354942018-04-18 00:05:21 -0700884 rmp->lcl_is_ip4 = tc->is_ip4;
885 rmp->lcl_port = tc->lcl_port;
Dave Barach178cf492018-11-13 16:34:13 -0500886 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -0700887 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
888 {
Florin Coras288eaab2019-02-03 15:26:14 -0800889 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
890 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras31c99552019-03-01 13:00:58 -0800891 vpp_evt_q = session_main_get_vpp_event_queue (0);
Florin Coras7fb0fe12018-04-09 09:24:52 -0700892 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
893 }
894 }
895 }
896 }));
897 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -0700898
899 /* If app uses mq for control messages, send an mq message as well */
900 if (app && application_use_mq_for_ctrl (app))
901 {
902 app_wrk = application_get_worker (app, 0);
903 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
904 rv);
905 }
Florin Coras6cf30ad2017-04-04 23:08:23 -0700906}
907
908static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500909vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
910{
911 vl_api_unbind_uri_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700912 application_t *app;
Florin Corasc1a42652019-02-08 18:27:29 -0800913 vnet_unlisten_args_t _a, *a = &_a;
Dave Barach68b0fb02017-02-28 15:15:56 -0500914 int rv;
915
Florin Coras31c99552019-03-01 13:00:58 -0800916 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700917 {
918 rv = VNET_API_ERROR_FEATURE_DISABLED;
919 goto done;
920 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500921
Florin Coras6cf30ad2017-04-04 23:08:23 -0700922 app = application_lookup (mp->client_index);
923 if (app)
924 {
925 a->uri = (char *) mp->uri;
Florin Coras15531972018-08-12 23:50:53 -0700926 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700927 rv = vnet_unbind_uri (a);
928 }
929 else
930 {
931 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
932 }
933
934done:
Dave Barach68b0fb02017-02-28 15:15:56 -0500935 REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
936}
937
Florin Corasf03a59a2017-06-09 21:07:32 -0700938static void
Dave Barach68b0fb02017-02-28 15:15:56 -0500939vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
940{
Dave Wallace33e002b2017-09-06 01:20:02 -0400941 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -0500942 vnet_connect_args_t _a, *a = &_a;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700943 application_t *app;
Florin Corascea194d2017-10-02 00:18:51 -0700944 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500945
Florin Coras31c99552019-03-01 13:00:58 -0800946 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700947 {
948 rv = VNET_API_ERROR_FEATURE_DISABLED;
949 goto done;
950 }
Florin Corase04c2992017-03-01 08:17:34 -0800951
Florin Coras6cf30ad2017-04-04 23:08:23 -0700952 app = application_lookup (mp->client_index);
953 if (app)
954 {
Dave Barachb7b92992018-10-17 10:38:51 -0400955 clib_memset (a, 0, sizeof (*a));
Florin Coras6cf30ad2017-04-04 23:08:23 -0700956 a->uri = (char *) mp->uri;
957 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -0700958 a->app_index = app->app_index;
Florin Corasc1a42652019-02-08 18:27:29 -0800959 if ((rv = vnet_connect_uri (a)))
960 clib_warning ("connect_uri returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -0700961 }
962 else
963 {
964 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
965 }
Florin Corase04c2992017-03-01 08:17:34 -0800966
Florin Coras3cbc04b2017-10-02 00:18:51 -0700967 /*
968 * Don't reply to stream (tcp) connects. The reply will come once
969 * the connection is established. In case of the redirects, the reply
970 * will come from the server app.
971 */
Florin Coras8f89dd02018-03-05 16:53:07 -0800972 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -0800973 return;
974
Florin Coras6cf30ad2017-04-04 23:08:23 -0700975done:
Florin Corase04c2992017-03-01 08:17:34 -0800976 /* *INDENT-OFF* */
Dave Wallace33e002b2017-09-06 01:20:02 -0400977 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corase04c2992017-03-01 08:17:34 -0800978 /* *INDENT-ON* */
Dave Barach68b0fb02017-02-28 15:15:56 -0500979}
980
981static void
982vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
983{
984 vl_api_disconnect_session_reply_t *rmp;
Florin Coras6cf30ad2017-04-04 23:08:23 -0700985 vnet_disconnect_args_t _a, *a = &_a;
986 application_t *app;
987 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -0500988
Florin Coras31c99552019-03-01 13:00:58 -0800989 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -0700990 {
991 rv = VNET_API_ERROR_FEATURE_DISABLED;
992 goto done;
993 }
Dave Barach68b0fb02017-02-28 15:15:56 -0500994
Florin Coras6cf30ad2017-04-04 23:08:23 -0700995 app = application_lookup (mp->client_index);
996 if (app)
997 {
998 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -0700999 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001000 rv = vnet_disconnect_session (a);
1001 }
1002 else
1003 {
1004 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1005 }
1006
1007done:
Dave Wallacede5fec92017-11-11 22:41:34 -05001008 REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
Dave Barach68b0fb02017-02-28 15:15:56 -05001009}
1010
1011static void
1012vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
1013 mp)
1014{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001015 vnet_disconnect_args_t _a, *a = &_a;
1016 application_t *app;
Dave Barach68b0fb02017-02-28 15:15:56 -05001017
1018 /* Client objected to disconnecting the session, log and continue */
1019 if (mp->retval)
1020 {
1021 clib_warning ("client retval %d", mp->retval);
1022 return;
1023 }
1024
1025 /* Disconnect has been confirmed. Confirm close to transport */
Florin Corasf8f516a2018-02-08 15:10:09 -08001026 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001027 if (app)
1028 {
1029 a->handle = mp->handle;
Florin Coras15531972018-08-12 23:50:53 -07001030 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001031 vnet_disconnect_session (a);
1032 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001033}
1034
1035static void
1036vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
1037{
Florin Coras4af830c2018-12-04 09:21:36 -08001038 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras15531972018-08-12 23:50:53 -07001039 app_worker_t *app_wrk;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001040 application_t *app;
Florin Coras288eaab2019-02-03 15:26:14 -08001041 session_t *s;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001042 u32 index, thread_index;
Dave Barach68b0fb02017-02-28 15:15:56 -05001043
Ondrej Fabry6bd197e2018-08-15 08:46:46 +02001044 app = application_lookup (mp->context);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001045 if (!app)
1046 return;
1047
Florin Corascea194d2017-10-02 00:18:51 -07001048 session_parse_handle (mp->handle, &index, &thread_index);
Florin Coras3cbc04b2017-10-02 00:18:51 -07001049 s = session_get_if_valid (index, thread_index);
Florin Coras15531972018-08-12 23:50:53 -07001050 if (!s)
Dave Barach68b0fb02017-02-28 15:15:56 -05001051 {
1052 clib_warning ("Invalid session!");
1053 return;
1054 }
1055
Florin Coras15531972018-08-12 23:50:53 -07001056 app_wrk = app_worker_get (s->app_wrk_index);
1057 if (app_wrk->app_index != app->app_index)
1058 {
1059 clib_warning ("app %u does not own handle 0x%lx", app->app_index,
1060 mp->handle);
1061 return;
1062 }
1063
Dave Barach68b0fb02017-02-28 15:15:56 -05001064 /* Client objected to resetting the session, log and continue */
1065 if (mp->retval)
1066 {
1067 clib_warning ("client retval %d", mp->retval);
1068 return;
1069 }
1070
Dave Barach68b0fb02017-02-28 15:15:56 -05001071 /* This comes as a response to a reset, transport only waiting for
1072 * confirmation to remove connection state, no need to disconnect */
Florin Coras4af830c2018-12-04 09:21:36 -08001073 a->handle = mp->handle;
1074 a->app_index = app->app_index;
1075 vnet_disconnect_session (a);
Dave Barach68b0fb02017-02-28 15:15:56 -05001076}
1077
1078static void
1079vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
1080{
Florin Corasf8f516a2018-02-08 15:10:09 -08001081 vnet_disconnect_args_t _a = { 0 }, *a = &_a;
Florin Coras288eaab2019-02-03 15:26:14 -08001082 session_t *s;
Dave Barach68b0fb02017-02-28 15:15:56 -05001083
Florin Corasa5464812017-04-19 13:00:05 -07001084 /* Server isn't interested, kill the session */
1085 if (mp->retval)
Dave Barach68b0fb02017-02-28 15:15:56 -05001086 {
Florin Corasa5464812017-04-19 13:00:05 -07001087 a->app_index = mp->context;
1088 a->handle = mp->handle;
1089 vnet_disconnect_session (a);
Florin Corasf8f516a2018-02-08 15:10:09 -08001090 return;
1091 }
1092
Florin Coras2b81e3c2019-02-27 07:55:46 -08001093 s = session_get_from_handle_if_valid (mp->handle);
1094 if (!s)
Florin Corasf8f516a2018-02-08 15:10:09 -08001095 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001096 clib_warning ("session doesn't exist");
1097 return;
Dave Barach68b0fb02017-02-28 15:15:56 -05001098 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001099 if (s->app_wrk_index != mp->context)
Florin Corasa5464812017-04-19 13:00:05 -07001100 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001101 clib_warning ("app doesn't own session");
1102 return;
Florin Corasa5464812017-04-19 13:00:05 -07001103 }
Florin Coras2b81e3c2019-02-27 07:55:46 -08001104 s->session_state = SESSION_STATE_READY;
1105
1106 if (!session_has_transport (s))
1107 ct_session_connect_notify (s);
Dave Barach68b0fb02017-02-28 15:15:56 -05001108}
1109
1110static void
1111vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
1112 * mp)
1113{
1114 clib_warning ("not implemented");
1115}
1116
1117static void
1118vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
1119{
Florin Corasc9940fc2019-02-05 20:55:11 -08001120 vnet_listen_args_t _a, *a = &_a;
1121 transport_connection_t *tc = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001122 vl_api_bind_sock_reply_t *rmp;
Florin Corasc9940fc2019-02-05 20:55:11 -08001123 svm_msg_q_t *vpp_evt_q;
Florin Coraseb2945c2017-11-22 10:39:09 -08001124 application_t *app = 0;
Florin Coras60116992018-08-27 09:52:18 -07001125 app_worker_t *app_wrk;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001126 ip46_address_t *ip46;
Florin Corasc9940fc2019-02-05 20:55:11 -08001127 app_listener_t *al;
1128 session_t *s;
1129 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001130
Florin Coras31c99552019-03-01 13:00:58 -08001131 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001132 {
1133 rv = VNET_API_ERROR_FEATURE_DISABLED;
1134 goto done;
1135 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001136
Florin Coras6cf30ad2017-04-04 23:08:23 -07001137 app = application_lookup (mp->client_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001138 if (!app)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001139 {
Florin Corasdcf55ce2017-11-16 15:32:50 -08001140 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1141 goto done;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001142 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001143
1144 ip46 = (ip46_address_t *) mp->ip;
Dave Barachb7b92992018-10-17 10:38:51 -04001145 clib_memset (a, 0, sizeof (*a));
Florin Corasdcf55ce2017-11-16 15:32:50 -08001146 a->sep.is_ip4 = mp->is_ip4;
1147 a->sep.ip = *ip46;
1148 a->sep.port = mp->port;
1149 a->sep.fib_index = mp->vrf;
1150 a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
1151 a->sep.transport_proto = mp->proto;
Florin Coras15531972018-08-12 23:50:53 -07001152 a->app_index = app->app_index;
1153 a->wrk_map_index = mp->wrk_index;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001154
Florin Corasc1a42652019-02-08 18:27:29 -08001155 if ((rv = vnet_listen (a)))
1156 clib_warning ("listen returned: %d", rv);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001157
Florin Coras6cf30ad2017-04-04 23:08:23 -07001158done:
Florin Corascea194d2017-10-02 00:18:51 -07001159 /* *INDENT-OFF* */
1160 REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
1161 if (!rv)
Florin Corasdcf55ce2017-11-16 15:32:50 -08001162 {
1163 rmp->handle = a->handle;
Florin Coraseb2945c2017-11-22 10:39:09 -08001164 rmp->lcl_port = mp->port;
Keith Burns (alagalah)60ae80f2018-03-15 10:04:20 -07001165 rmp->lcl_is_ip4 = mp->is_ip4;
Dave Wallace97b1a272017-12-18 13:51:59 -05001166 if (app && application_has_global_scope (app))
Florin Coraseb2945c2017-11-22 10:39:09 -08001167 {
Florin Corasc9940fc2019-02-05 20:55:11 -08001168 al = app_listener_get_w_handle (a->handle);
1169 s = app_listener_get_session (al);
Florin Coraseb2945c2017-11-22 10:39:09 -08001170 tc = listen_session_get_transport (s);
Dave Barach178cf492018-11-13 16:34:13 -05001171 clib_memcpy_fast (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
Florin Coras7fb0fe12018-04-09 09:24:52 -07001172 if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL)
1173 {
Florin Coras288eaab2019-02-03 15:26:14 -08001174 rmp->rx_fifo = pointer_to_uword (s->rx_fifo);
1175 rmp->tx_fifo = pointer_to_uword (s->tx_fifo);
Florin Coras31c99552019-03-01 13:00:58 -08001176 vpp_evt_q = session_main_get_vpp_event_queue (0);
Florin Coras7fb0fe12018-04-09 09:24:52 -07001177 rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q);
1178 }
Florin Coraseb2945c2017-11-22 10:39:09 -08001179 }
Florin Corasdcf55ce2017-11-16 15:32:50 -08001180 }
Florin Corascea194d2017-10-02 00:18:51 -07001181 }));
1182 /* *INDENT-ON* */
Florin Coras60116992018-08-27 09:52:18 -07001183
1184 /* If app uses mq for control messages, send an mq message as well */
1185 if (app && application_use_mq_for_ctrl (app))
1186 {
1187 app_wrk = application_get_worker (app, mp->wrk_index);
1188 mq_send_session_bound_cb (app_wrk->wrk_index, mp->context, a->handle,
1189 rv);
1190 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001191}
1192
1193static void
1194vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
1195{
1196 vl_api_unbind_sock_reply_t *rmp;
Florin Corasc1a42652019-02-08 18:27:29 -08001197 vnet_unlisten_args_t _a, *a = &_a;
Florin Corasdfae9f92019-02-20 19:48:31 -08001198 app_worker_t *app_wrk;
1199 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001200 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001201
Florin Coras31c99552019-03-01 13:00:58 -08001202 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001203 {
1204 rv = VNET_API_ERROR_FEATURE_DISABLED;
1205 goto done;
1206 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001207
Florin Coras6cf30ad2017-04-04 23:08:23 -07001208 app = application_lookup (mp->client_index);
1209 if (app)
1210 {
Florin Coras15531972018-08-12 23:50:53 -07001211 a->app_index = app->app_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001212 a->handle = mp->handle;
Florin Corasab2f6db2018-08-31 14:31:41 -07001213 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001214 if ((rv = vnet_unlisten (a)))
1215 clib_warning ("unlisten returned: %d", rv);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001216 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001217
Florin Coras6cf30ad2017-04-04 23:08:23 -07001218done:
Dave Barach68b0fb02017-02-28 15:15:56 -05001219 REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
Florin Corasdfae9f92019-02-20 19:48:31 -08001220
1221 /*
1222 * Send reply over msg queue
1223 */
1224 svm_msg_q_msg_t _msg, *msg = &_msg;
1225 session_unlisten_reply_msg_t *ump;
1226 svm_msg_q_t *app_mq;
1227 session_event_t *evt;
1228
1229 if (!app)
1230 return;
1231
1232 app_wrk = application_get_worker (app, a->wrk_map_index);
1233 if (!app_wrk)
1234 return;
1235
1236 app_mq = app_wrk->event_queue;
1237 if (mq_try_lock_and_alloc_msg (app_mq, msg))
1238 return;
1239
1240 evt = svm_msg_q_msg_data (app_mq, msg);
1241 clib_memset (evt, 0, sizeof (*evt));
1242 evt->event_type = SESSION_CTRL_EVT_UNLISTEN_REPLY;
1243 ump = (session_unlisten_reply_msg_t *) evt->data;
1244 ump->context = mp->context;
1245 ump->handle = mp->handle;
1246 ump->retval = rv;
1247 svm_msg_q_add_and_unlock (app_mq, msg);
Dave Barach68b0fb02017-02-28 15:15:56 -05001248}
1249
1250static void
1251vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1252{
Dave Wallace33e002b2017-09-06 01:20:02 -04001253 vl_api_connect_session_reply_t *rmp;
Dave Barach68b0fb02017-02-28 15:15:56 -05001254 vnet_connect_args_t _a, *a = &_a;
Florin Corasab2f6db2018-08-31 14:31:41 -07001255 application_t *app = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001256 int rv = 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001257
Florin Coras31c99552019-03-01 13:00:58 -08001258 if (session_main_is_enabled () == 0)
Florin Coras6cf30ad2017-04-04 23:08:23 -07001259 {
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 Corase86a8ed2018-01-05 03:20:25 -08001267 svm_queue_t *client_q;
Dave Wallace33e002b2017-09-06 01:20:02 -04001268 ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001269
Dave Barachb7b92992018-10-17 10:38:51 -04001270 clib_memset (a, 0, sizeof (*a));
Dave Wallaceb2d5ff32017-06-14 12:38:28 -04001271 client_q = vl_api_client_index_to_input_queue (mp->client_index);
1272 mp->client_queue_address = pointer_to_uword (client_q);
Florin Corascea194d2017-10-02 00:18:51 -07001273 a->sep.is_ip4 = mp->is_ip4;
1274 a->sep.ip = *ip46;
1275 a->sep.port = mp->port;
1276 a->sep.transport_proto = mp->proto;
Florin Coras5665ced2018-10-25 18:03:45 -07001277 a->sep.peer.fib_index = mp->vrf;
1278 a->sep.peer.sw_if_index = ENDPOINT_INVALID_INDEX;
Florin Coras8f89dd02018-03-05 16:53:07 -08001279 if (mp->hostname_len)
1280 {
Florin Coras15531972018-08-12 23:50:53 -07001281 vec_validate (a->sep_ext.hostname, mp->hostname_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001282 clib_memcpy_fast (a->sep_ext.hostname, mp->hostname,
1283 mp->hostname_len);
Florin Coras8f89dd02018-03-05 16:53:07 -08001284 }
Florin Coras6cf30ad2017-04-04 23:08:23 -07001285 a->api_context = mp->context;
Florin Coras15531972018-08-12 23:50:53 -07001286 a->app_index = app->app_index;
1287 a->wrk_map_index = mp->wrk_index;
Florin Corasc1a42652019-02-08 18:27:29 -08001288 if ((rv = vnet_connect (a)))
1289 clib_warning ("connect returned: %u", rv);
Florin Coras15531972018-08-12 23:50:53 -07001290 vec_free (a->sep_ext.hostname);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001291 }
1292 else
1293 {
1294 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1295 }
Florin Corase04c2992017-03-01 08:17:34 -08001296
Florin Coras8f89dd02018-03-05 16:53:07 -08001297 if (rv == 0)
Florin Corase04c2992017-03-01 08:17:34 -08001298 return;
1299
1300 /* Got some error, relay it */
1301
Florin Coras6cf30ad2017-04-04 23:08:23 -07001302done:
Dave Wallace33e002b2017-09-06 01:20:02 -04001303 REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
Florin Corasab2f6db2018-08-31 14:31:41 -07001304
1305 if (app && application_use_mq_for_ctrl (app))
1306 {
1307 app_worker_t *app_wrk = application_get_worker (app, mp->wrk_index);
1308 mq_send_session_connected_cb (app_wrk->wrk_index, mp->context, 0, 1);
1309 }
Dave Barach68b0fb02017-02-28 15:15:56 -05001310}
1311
Florin Corascea194d2017-10-02 00:18:51 -07001312static void
Florin Coras15531972018-08-12 23:50:53 -07001313vl_api_app_worker_add_del_t_handler (vl_api_app_worker_add_del_t * mp)
1314{
1315 int rv = 0, fds[SESSION_N_FD_TYPE], n_fds = 0;
1316 vl_api_app_worker_add_del_reply_t *rmp;
1317 vl_api_registration_t *reg;
Florin Coras15531972018-08-12 23:50:53 -07001318 application_t *app;
1319 u8 fd_flags = 0;
1320
Florin Coras31c99552019-03-01 13:00:58 -08001321 if (!session_main_is_enabled ())
Florin Coras15531972018-08-12 23:50:53 -07001322 {
1323 rv = VNET_API_ERROR_FEATURE_DISABLED;
1324 goto done;
1325 }
1326
1327 reg = vl_api_client_index_to_registration (mp->client_index);
1328 if (!reg)
1329 return;
1330
Florin Corasc1f5a432018-11-20 11:31:26 -08001331 app = application_get_if_valid (clib_net_to_host_u32 (mp->app_index));
Florin Coras15531972018-08-12 23:50:53 -07001332 if (!app)
1333 {
1334 rv = VNET_API_ERROR_INVALID_VALUE;
1335 goto done;
1336 }
1337
1338 vnet_app_worker_add_del_args_t args = {
1339 .app_index = app->app_index,
Florin Coras349f8ca2018-11-20 16:52:49 -08001340 .wrk_map_index = clib_net_to_host_u32 (mp->wrk_index),
Florin Corasc1f5a432018-11-20 11:31:26 -08001341 .api_client_index = mp->client_index,
Florin Coras15531972018-08-12 23:50:53 -07001342 .is_add = mp->is_add
1343 };
Florin Corasc1a42652019-02-08 18:27:29 -08001344 rv = vnet_app_worker_add_del (&args);
1345 if (rv)
Florin Coras15531972018-08-12 23:50:53 -07001346 {
Florin Corasc1a42652019-02-08 18:27:29 -08001347 clib_warning ("app worker add/del returned: %d", rv);
Florin Coras15531972018-08-12 23:50:53 -07001348 goto done;
1349 }
1350
Florin Coras14598772018-09-04 19:47:52 -07001351 if (!mp->is_add)
1352 goto done;
Florin Corasc3638fe2018-08-24 13:58:49 -07001353
Florin Coras15531972018-08-12 23:50:53 -07001354 /* Send fifo segment fd if needed */
1355 if (ssvm_type (args.segment) == SSVM_SEGMENT_MEMFD)
1356 {
1357 fd_flags |= SESSION_FD_F_MEMFD_SEGMENT;
1358 fds[n_fds] = args.segment->fd;
1359 n_fds += 1;
1360 }
1361 if (application_segment_manager_properties (app)->use_mq_eventfd)
1362 {
1363 fd_flags |= SESSION_FD_F_MQ_EVENTFD;
1364 fds[n_fds] = svm_msg_q_get_producer_eventfd (args.evt_q);
1365 n_fds += 1;
1366 }
1367
1368 /* *INDENT-OFF* */
1369done:
1370 REPLY_MACRO2 (VL_API_APP_WORKER_ADD_DEL_REPLY, ({
1371 rmp->is_add = mp->is_add;
Florin Coras349f8ca2018-11-20 16:52:49 -08001372 rmp->wrk_index = clib_host_to_net_u32 (args.wrk_map_index);
Florin Corasfa76a762018-11-29 12:40:10 -08001373 rmp->segment_handle = clib_host_to_net_u64 (args.segment_handle);
Florin Coras14598772018-09-04 19:47:52 -07001374 if (!rv && mp->is_add)
Florin Coras15531972018-08-12 23:50:53 -07001375 {
Florin Coras15531972018-08-12 23:50:53 -07001376 if (vec_len (args.segment->name))
1377 {
1378 memcpy (rmp->segment_name, args.segment->name,
1379 vec_len (args.segment->name));
1380 rmp->segment_name_length = vec_len (args.segment->name);
1381 }
1382 rmp->app_event_queue_address = pointer_to_uword (args.evt_q);
1383 rmp->n_fds = n_fds;
1384 rmp->fd_flags = fd_flags;
1385 }
1386 }));
1387 /* *INDENT-ON* */
1388
1389 if (n_fds)
1390 session_send_fds (reg, fds, n_fds);
1391}
1392
1393static void
Florin Corascea194d2017-10-02 00:18:51 -07001394vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
1395{
1396 vl_api_app_namespace_add_del_reply_t *rmp;
Florin Coras6e8c6672017-11-10 09:03:54 -08001397 u32 appns_index = 0;
1398 u8 *ns_id = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001399 int rv = 0;
Florin Coras31c99552019-03-01 13:00:58 -08001400 if (!session_main_is_enabled ())
Florin Corascea194d2017-10-02 00:18:51 -07001401 {
1402 rv = VNET_API_ERROR_FEATURE_DISABLED;
1403 goto done;
1404 }
1405
1406 if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
1407 {
1408 rv = VNET_API_ERROR_INVALID_VALUE;
1409 goto done;
1410 }
1411
1412 vec_validate (ns_id, mp->namespace_id_len - 1);
Dave Barach178cf492018-11-13 16:34:13 -05001413 clib_memcpy_fast (ns_id, mp->namespace_id, mp->namespace_id_len);
Florin Corascea194d2017-10-02 00:18:51 -07001414 vnet_app_namespace_add_del_args_t args = {
1415 .ns_id = ns_id,
Florin Coras9a9adb22017-10-26 08:16:59 -07001416 .secret = clib_net_to_host_u64 (mp->secret),
Florin Corascea194d2017-10-02 00:18:51 -07001417 .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
1418 .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
1419 .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
1420 .is_add = 1
1421 };
Florin Corasc1a42652019-02-08 18:27:29 -08001422 rv = vnet_app_namespace_add_del (&args);
1423 if (!rv)
Florin Coras6e8c6672017-11-10 09:03:54 -08001424 {
1425 appns_index = app_namespace_index_from_id (ns_id);
1426 if (appns_index == APP_NAMESPACE_INVALID_INDEX)
1427 {
1428 clib_warning ("app ns lookup failed");
1429 rv = VNET_API_ERROR_UNSPECIFIED;
1430 }
1431 }
Florin Corascea194d2017-10-02 00:18:51 -07001432 vec_free (ns_id);
Florin Coras6e8c6672017-11-10 09:03:54 -08001433
1434 /* *INDENT-OFF* */
Florin Corascea194d2017-10-02 00:18:51 -07001435done:
Florin Coras6e8c6672017-11-10 09:03:54 -08001436 REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
1437 if (!rv)
1438 rmp->appns_index = clib_host_to_net_u32 (appns_index);
1439 }));
1440 /* *INDENT-ON* */
Florin Corascea194d2017-10-02 00:18:51 -07001441}
1442
Florin Coras1c710452017-10-17 00:03:13 -07001443static void
1444vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
1445{
1446 vl_api_session_rule_add_del_reply_t *rmp;
1447 session_rule_add_del_args_t args;
1448 session_rule_table_add_del_args_t *table_args = &args.table_args;
Florin Coras1c710452017-10-17 00:03:13 -07001449 u8 fib_proto;
1450 int rv = 0;
1451
Dave Barachb7b92992018-10-17 10:38:51 -04001452 clib_memset (&args, 0, sizeof (args));
Florin Coras1c710452017-10-17 00:03:13 -07001453 fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1454
1455 table_args->lcl.fp_len = mp->lcl_plen;
1456 table_args->lcl.fp_proto = fib_proto;
1457 table_args->rmt.fp_len = mp->rmt_plen;
1458 table_args->rmt.fp_proto = fib_proto;
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001459 table_args->lcl_port = mp->lcl_port;
1460 table_args->rmt_port = mp->rmt_port;
Florin Coras1c710452017-10-17 00:03:13 -07001461 table_args->action_index = clib_net_to_host_u32 (mp->action_index);
1462 table_args->is_add = mp->is_add;
Florin Corasc97a7392017-11-05 23:07:07 -08001463 mp->tag[sizeof (mp->tag) - 1] = 0;
1464 table_args->tag = format (0, "%s", mp->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001465 args.appns_index = clib_net_to_host_u32 (mp->appns_index);
1466 args.scope = mp->scope;
Florin Coras42324ad2017-11-18 18:45:20 -08001467 args.transport_proto = mp->transport_proto;
Florin Coras1c710452017-10-17 00:03:13 -07001468
Dave Barachb7b92992018-10-17 10:38:51 -04001469 clib_memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
1470 clib_memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
Florin Coras1c710452017-10-17 00:03:13 -07001471 ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
1472 ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
Florin Corasc1a42652019-02-08 18:27:29 -08001473 rv = vnet_session_rule_add_del (&args);
1474 if (rv)
1475 clib_warning ("rule add del returned: %d", rv);
Florin Corasc97a7392017-11-05 23:07:07 -08001476 vec_free (table_args->tag);
Florin Coras1c710452017-10-17 00:03:13 -07001477 REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
1478}
1479
Florin Coras6c36f532017-11-03 18:32:34 -07001480static void
1481send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
Florin Corasc97a7392017-11-05 23:07:07 -08001482 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001483 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001484{
1485 vl_api_session_rules_details_t *rmp = 0;
1486 session_mask_or_match_4_t *match =
1487 (session_mask_or_match_4_t *) & rule->match;
1488 session_mask_or_match_4_t *mask =
1489 (session_mask_or_match_4_t *) & rule->mask;
1490
1491 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001492 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001493 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1494 rmp->context = context;
1495
1496 rmp->is_ip4 = 1;
Dave Barach178cf492018-11-13 16:34:13 -05001497 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1498 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001499 rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
1500 rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001501 rmp->lcl_port = match->lcl_port;
1502 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001503 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1504 rmp->scope =
1505 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1506 rmp->transport_proto = transport_proto;
1507 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001508 if (tag)
1509 {
Dave Barach178cf492018-11-13 16:34:13 -05001510 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001511 rmp->tag[vec_len (tag)] = 0;
1512 }
Florin Coras6c36f532017-11-03 18:32:34 -07001513
Florin Coras6c4dae22018-01-09 06:39:23 -08001514 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001515}
1516
1517static void
Florin Corasc97a7392017-11-05 23:07:07 -08001518send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1519 u8 transport_proto, u32 appns_index, u8 * tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001520 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001521{
1522 vl_api_session_rules_details_t *rmp = 0;
1523 session_mask_or_match_6_t *match =
1524 (session_mask_or_match_6_t *) & rule->match;
1525 session_mask_or_match_6_t *mask =
1526 (session_mask_or_match_6_t *) & rule->mask;
1527
1528 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -04001529 clib_memset (rmp, 0, sizeof (*rmp));
Florin Coras6c36f532017-11-03 18:32:34 -07001530 rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1531 rmp->context = context;
1532
1533 rmp->is_ip4 = 0;
Dave Barach178cf492018-11-13 16:34:13 -05001534 clib_memcpy_fast (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1535 clib_memcpy_fast (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
Florin Coras6c36f532017-11-03 18:32:34 -07001536 rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1537 rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
Milan Lenco8b9a5d12017-11-24 17:12:33 +01001538 rmp->lcl_port = match->lcl_port;
1539 rmp->rmt_port = match->rmt_port;
Florin Coras6c36f532017-11-03 18:32:34 -07001540 rmp->action_index = clib_host_to_net_u32 (rule->action_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001541 rmp->scope =
1542 is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
Florin Coras6c36f532017-11-03 18:32:34 -07001543 rmp->transport_proto = transport_proto;
1544 rmp->appns_index = clib_host_to_net_u32 (appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001545 if (tag)
1546 {
Dave Barach178cf492018-11-13 16:34:13 -05001547 clib_memcpy_fast (rmp->tag, tag, vec_len (tag));
Florin Corasc97a7392017-11-05 23:07:07 -08001548 rmp->tag[vec_len (tag)] = 0;
1549 }
Florin Coras6c36f532017-11-03 18:32:34 -07001550
Florin Coras6c4dae22018-01-09 06:39:23 -08001551 vl_api_send_msg (reg, (u8 *) rmp);
Florin Coras6c36f532017-11-03 18:32:34 -07001552}
1553
1554static void
1555send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
Florin Corasc97a7392017-11-05 23:07:07 -08001556 u8 tp, u8 is_local, u32 appns_index,
Florin Coras6c4dae22018-01-09 06:39:23 -08001557 vl_api_registration_t * reg, u32 context)
Florin Coras6c36f532017-11-03 18:32:34 -07001558{
1559 mma_rule_16_t *rule16;
1560 mma_rule_40_t *rule40;
1561 mma_rules_table_16_t *srt16;
1562 mma_rules_table_40_t *srt40;
Florin Corasc97a7392017-11-05 23:07:07 -08001563 u32 ri;
Florin Coras6c36f532017-11-03 18:32:34 -07001564
Florin Corasc97a7392017-11-05 23:07:07 -08001565 if (is_local || fib_proto == FIB_PROTOCOL_IP4)
Florin Coras6c36f532017-11-03 18:32:34 -07001566 {
Florin Corasc97a7392017-11-05 23:07:07 -08001567 u8 *tag = 0;
1568 /* *INDENT-OFF* */
1569 srt16 = &srt->session_rules_tables_16;
1570 pool_foreach (rule16, srt16->rules, ({
1571 ri = mma_rules_table_rule_index_16 (srt16, rule16);
1572 tag = session_rules_table_rule_tag (srt, ri, 1);
1573 send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001574 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001575 }));
1576 /* *INDENT-ON* */
1577 }
1578 if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1579 {
1580 u8 *tag = 0;
1581 /* *INDENT-OFF* */
1582 srt40 = &srt->session_rules_tables_40;
1583 pool_foreach (rule40, srt40->rules, ({
1584 ri = mma_rules_table_rule_index_40 (srt40, rule40);
1585 tag = session_rules_table_rule_tag (srt, ri, 1);
1586 send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
Florin Coras6c4dae22018-01-09 06:39:23 -08001587 reg, context);
Florin Corasc97a7392017-11-05 23:07:07 -08001588 }));
1589 /* *INDENT-ON* */
Florin Coras6c36f532017-11-03 18:32:34 -07001590 }
1591}
1592
1593static void
1594vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1595{
Florin Coras6c4dae22018-01-09 06:39:23 -08001596 vl_api_registration_t *reg;
Florin Coras6c36f532017-11-03 18:32:34 -07001597 session_table_t *st;
Florin Corasc97a7392017-11-05 23:07:07 -08001598 u8 tp;
Florin Coras6c36f532017-11-03 18:32:34 -07001599
Florin Coras6c4dae22018-01-09 06:39:23 -08001600 reg = vl_api_client_index_to_registration (mp->client_index);
1601 if (!reg)
Florin Coras6c36f532017-11-03 18:32:34 -07001602 return;
1603
1604 /* *INDENT-OFF* */
1605 session_table_foreach (st, ({
Florin Corasc97a7392017-11-05 23:07:07 -08001606 for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1607 {
1608 send_session_rules_table_details (&st->session_rules[tp],
1609 st->active_fib_proto, tp,
Florin Coras6c4dae22018-01-09 06:39:23 -08001610 st->is_local, st->appns_index, reg,
Florin Corasc97a7392017-11-05 23:07:07 -08001611 mp->context);
1612 }
Florin Coras6c36f532017-11-03 18:32:34 -07001613 }));
1614 /* *INDENT-ON* */
1615}
1616
Florin Coras371ca502018-02-21 12:07:41 -08001617static void
1618vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1619 mp)
1620{
1621 vl_api_app_namespace_add_del_reply_t *rmp;
1622 vnet_app_add_tls_cert_args_t _a, *a = &_a;
1623 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001624 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001625 u32 cert_len;
1626 int rv = 0;
Florin Coras31c99552019-03-01 13:00:58 -08001627 if (!session_main_is_enabled ())
Florin Coras371ca502018-02-21 12:07:41 -08001628 {
1629 rv = VNET_API_ERROR_FEATURE_DISABLED;
1630 goto done;
1631 }
Florin Corase3e2f072018-03-04 07:24:30 -08001632 if (!(app = application_lookup (mp->client_index)))
1633 {
1634 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1635 goto done;
1636 }
Dave Barachb7b92992018-10-17 10:38:51 -04001637 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001638 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001639 cert_len = clib_net_to_host_u16 (mp->cert_len);
Florin Coras5090c572018-03-18 08:22:17 -07001640 if (cert_len > 10000)
1641 {
1642 rv = VNET_API_ERROR_INVALID_VALUE;
1643 goto done;
1644 }
Florin Coras371ca502018-02-21 12:07:41 -08001645 vec_validate (a->cert, cert_len);
Dave Barach178cf492018-11-13 16:34:13 -05001646 clib_memcpy_fast (a->cert, mp->cert, cert_len);
Florin Coras371ca502018-02-21 12:07:41 -08001647 if ((error = vnet_app_add_tls_cert (a)))
1648 {
1649 rv = clib_error_get_code (error);
1650 clib_error_report (error);
1651 }
1652 vec_free (a->cert);
1653done:
1654 REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1655}
1656
1657static void
1658vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1659 mp)
1660{
1661 vl_api_app_namespace_add_del_reply_t *rmp;
1662 vnet_app_add_tls_key_args_t _a, *a = &_a;
1663 clib_error_t *error;
Florin Corase3e2f072018-03-04 07:24:30 -08001664 application_t *app;
Florin Coras371ca502018-02-21 12:07:41 -08001665 u32 key_len;
1666 int rv = 0;
Florin Coras31c99552019-03-01 13:00:58 -08001667 if (!session_main_is_enabled ())
Florin Coras371ca502018-02-21 12:07:41 -08001668 {
1669 rv = VNET_API_ERROR_FEATURE_DISABLED;
1670 goto done;
1671 }
Florin Corase3e2f072018-03-04 07:24:30 -08001672 if (!(app = application_lookup (mp->client_index)))
1673 {
1674 rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
1675 goto done;
1676 }
Dave Barachb7b92992018-10-17 10:38:51 -04001677 clib_memset (a, 0, sizeof (*a));
Florin Coras15531972018-08-12 23:50:53 -07001678 a->app_index = app->app_index;
Florin Coras371ca502018-02-21 12:07:41 -08001679 key_len = clib_net_to_host_u16 (mp->key_len);
Florin Coras5090c572018-03-18 08:22:17 -07001680 if (key_len > 10000)
1681 {
1682 rv = VNET_API_ERROR_INVALID_VALUE;
1683 goto done;
1684 }
Florin Coras371ca502018-02-21 12:07:41 -08001685 vec_validate (a->key, key_len);
Dave Barach178cf492018-11-13 16:34:13 -05001686 clib_memcpy_fast (a->key, mp->key, key_len);
Florin Coras371ca502018-02-21 12:07:41 -08001687 if ((error = vnet_app_add_tls_key (a)))
1688 {
1689 rv = clib_error_get_code (error);
1690 clib_error_report (error);
1691 }
1692 vec_free (a->key);
1693done:
1694 REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1695}
1696
Florin Coras6cf30ad2017-04-04 23:08:23 -07001697static clib_error_t *
1698application_reaper_cb (u32 client_index)
Dave Barach68b0fb02017-02-28 15:15:56 -05001699{
Florin Coras6cf30ad2017-04-04 23:08:23 -07001700 application_t *app = application_lookup (client_index);
1701 vnet_app_detach_args_t _a, *a = &_a;
1702 if (app)
1703 {
Florin Coras15531972018-08-12 23:50:53 -07001704 a->app_index = app->app_index;
Florin Coras053a0e42018-11-13 15:52:38 -08001705 a->api_client_index = client_index;
Florin Coras6cf30ad2017-04-04 23:08:23 -07001706 vnet_application_detach (a);
1707 }
1708 return 0;
Dave Barach68b0fb02017-02-28 15:15:56 -05001709}
1710
Florin Coras6cf30ad2017-04-04 23:08:23 -07001711VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
Dave Barach68b0fb02017-02-28 15:15:56 -05001712
1713#define vl_msg_name_crc_list
1714#include <vnet/vnet_all_api_h.h>
1715#undef vl_msg_name_crc_list
1716
1717static void
1718setup_message_id_table (api_main_t * am)
1719{
1720#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1721 foreach_vl_msg_name_crc_session;
1722#undef _
1723}
1724
1725/*
1726 * session_api_hookup
1727 * Add uri's API message handlers to the table.
1728 * vlib has alread mapped shared memory and
1729 * added the client registration handlers.
1730 * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1731 */
1732static clib_error_t *
1733session_api_hookup (vlib_main_t * vm)
1734{
1735 api_main_t *am = &api_main;
1736
1737#define _(N,n) \
1738 vl_msg_api_set_handlers(VL_API_##N, #n, \
1739 vl_api_##n##_t_handler, \
1740 vl_noop_handler, \
1741 vl_api_##n##_t_endian, \
1742 vl_api_##n##_t_print, \
1743 sizeof(vl_api_##n##_t), 1);
1744 foreach_session_api_msg;
1745#undef _
1746
1747 /*
1748 * Messages which bounce off the data-plane to
1749 * an API client. Simply tells the message handling infra not
1750 * to free the message.
1751 *
1752 * Bounced message handlers MUST NOT block the data plane
1753 */
1754 am->message_bounce[VL_API_CONNECT_URI] = 1;
1755 am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1756
1757 /*
1758 * Set up the (msg_name, crc, message-id) table
1759 */
1760 setup_message_id_table (am);
1761
1762 return 0;
1763}
1764
1765VLIB_API_INIT_FUNCTION (session_api_hookup);
Florin Coras6cf30ad2017-04-04 23:08:23 -07001766
Dave Barach68b0fb02017-02-28 15:15:56 -05001767/*
1768 * fd.io coding-style-patch-verification: ON
1769 *
1770 * Local Variables:
1771 * eval: (c-set-style "gnu")
1772 * End:
1773 */