blob: cf8bb72d4515a74c92255239c5fd98cba11b7323 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
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 <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Coras30e79c22019-01-02 19:31:22 -080025static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070026vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080027{
28 svm_msg_q_msg_t *msg;
29 u32 n_msgs;
30 int i;
31
Florin Corase003a1b2019-06-05 10:47:16 -070032 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
Florin Coras30e79c22019-01-02 19:31:22 -080033 for (i = 0; i < n_msgs; i++)
34 {
35 vec_add2 (wrk->mq_msg_vector, msg, 1);
36 svm_msg_q_sub_w_lock (mq, msg);
37 }
38 return n_msgs;
39}
40
Florin Coras697faea2018-06-27 17:10:49 -070041const char *
Florin Coras288eaab2019-02-03 15:26:14 -080042vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040043{
44 char *st;
45
46 switch (state)
47 {
Florin Corasc127d5a2020-10-14 16:35:58 -070048 case VCL_STATE_CLOSED:
Florin Coras54140622020-02-04 19:04:34 +000049 st = "STATE_CLOSED";
Dave Wallace543852a2017-08-03 02:11:34 -040050 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070051 case VCL_STATE_LISTEN:
Dave Wallace543852a2017-08-03 02:11:34 -040052 st = "STATE_LISTEN";
53 break;
Florin Corasdfffdd72020-10-15 10:54:47 -070054 case VCL_STATE_READY:
55 st = "STATE_READY";
Dave Wallace543852a2017-08-03 02:11:34 -040056 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070057 case VCL_STATE_VPP_CLOSING:
Florin Coras3c7d4f92018-12-14 11:28:43 -080058 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050059 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070060 case VCL_STATE_DISCONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040061 st = "STATE_DISCONNECT";
62 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070063 case VCL_STATE_DETACHED:
Florin Corasadcfb152020-02-12 08:50:29 +000064 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040065 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070066 case VCL_STATE_UPDATED:
Florin Coras2d675d72019-01-28 15:54:27 -080067 st = "STATE_UPDATED";
68 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070069 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -080070 st = "STATE_LISTEN_NO_MQ";
71 break;
Dave Wallace543852a2017-08-03 02:11:34 -040072 default:
73 st = "UNKNOWN_STATE";
74 break;
75 }
76
77 return st;
78}
79
Dave Wallace543852a2017-08-03 02:11:34 -040080u8 *
81format_ip4_address (u8 * s, va_list * args)
82{
83 u8 *a = va_arg (*args, u8 *);
84 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
85}
86
87u8 *
88format_ip6_address (u8 * s, va_list * args)
89{
90 ip6_address_t *a = va_arg (*args, ip6_address_t *);
91 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
92
93 i_max_n_zero = ARRAY_LEN (a->as_u16);
94 max_n_zeros = 0;
95 i_first_zero = i_max_n_zero;
96 n_zeros = 0;
97 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
98 {
99 u32 is_zero = a->as_u16[i] == 0;
100 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
101 {
102 i_first_zero = i;
103 n_zeros = 0;
104 }
105 n_zeros += is_zero;
106 if ((!is_zero && n_zeros > max_n_zeros)
107 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
108 {
109 i_max_n_zero = i_first_zero;
110 max_n_zeros = n_zeros;
111 i_first_zero = ARRAY_LEN (a->as_u16);
112 n_zeros = 0;
113 }
114 }
115
116 last_double_colon = 0;
117 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
118 {
119 if (i == i_max_n_zero && max_n_zeros > 1)
120 {
121 s = format (s, "::");
122 i += max_n_zeros - 1;
123 last_double_colon = 1;
124 }
125 else
126 {
127 s = format (s, "%s%x",
128 (last_double_colon || i == 0) ? "" : ":",
129 clib_net_to_host_u16 (a->as_u16[i]));
130 last_double_colon = 0;
131 }
132 }
133
134 return s;
135}
136
137/* Format an IP46 address. */
138u8 *
139format_ip46_address (u8 * s, va_list * args)
140{
141 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
142 ip46_type_t type = va_arg (*args, ip46_type_t);
143 int is_ip4 = 1;
144
145 switch (type)
146 {
147 case IP46_TYPE_ANY:
148 is_ip4 = ip46_address_is_ip4 (ip46);
149 break;
150 case IP46_TYPE_IP4:
151 is_ip4 = 1;
152 break;
153 case IP46_TYPE_IP6:
154 is_ip4 = 0;
155 break;
156 }
157
158 return is_ip4 ?
159 format (s, "%U", format_ip4_address, &ip46->ip4) :
160 format (s, "%U", format_ip6_address, &ip46->ip6);
161}
162
Florin Coras697faea2018-06-27 17:10:49 -0700163/*
164 * VPPCOM Utility Functions
165 */
166
Florin Coras458089b2019-08-21 16:20:44 -0700167static void
168vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
169{
170 app_session_evt_t _app_evt, *app_evt = &_app_evt;
171 session_listen_msg_t *mp;
172 svm_msg_q_t *mq;
173
174 mq = vcl_worker_ctrl_mq (wrk);
175 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
176 mp = (session_listen_msg_t *) app_evt->evt->data;
177 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700178 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700179 mp->context = s->session_index;
180 mp->wrk_index = wrk->vpp_wrk_index;
181 mp->is_ip4 = s->transport.is_ip4;
182 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
183 mp->port = s->transport.lcl_port;
184 mp->proto = s->session_type;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800185 mp->ckpair_index = s->ckpair_index;
Florin Coras6a6555a2021-01-28 11:39:27 -0800186 mp->vrf = s->vrf;
Florin Coras1e966172020-05-16 18:18:14 +0000187 if (s->flags & VCL_SESSION_F_CONNECTED)
188 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700189 app_send_ctrl_evt_to_vpp (mq, app_evt);
190}
191
192static void
193vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
194{
195 app_session_evt_t _app_evt, *app_evt = &_app_evt;
196 session_connect_msg_t *mp;
197 svm_msg_q_t *mq;
198
199 mq = vcl_worker_ctrl_mq (wrk);
200 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
201 mp = (session_connect_msg_t *) app_evt->evt->data;
202 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700203 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700204 mp->context = s->session_index;
205 mp->wrk_index = wrk->vpp_wrk_index;
206 mp->is_ip4 = s->transport.is_ip4;
207 mp->parent_handle = s->parent_handle;
208 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700209 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700210 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000211 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700212 mp->proto = s->session_type;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800213 mp->ckpair_index = s->ckpair_index;
Florin Coras6a6555a2021-01-28 11:39:27 -0800214 mp->vrf = s->vrf;
Florin Coras0a1e1832020-03-29 18:54:04 +0000215 if (s->flags & VCL_SESSION_F_CONNECTED)
216 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700217 app_send_ctrl_evt_to_vpp (mq, app_evt);
218}
219
220void
221vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
222{
223 app_session_evt_t _app_evt, *app_evt = &_app_evt;
224 session_unlisten_msg_t *mp;
225 svm_msg_q_t *mq;
226
227 mq = vcl_worker_ctrl_mq (wrk);
228 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
229 mp = (session_unlisten_msg_t *) app_evt->evt->data;
230 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700231 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700232 mp->wrk_index = wrk->vpp_wrk_index;
233 mp->handle = s->vpp_handle;
234 mp->context = wrk->wrk_index;
235 app_send_ctrl_evt_to_vpp (mq, app_evt);
236}
237
238static void
239vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
240{
241 app_session_evt_t _app_evt, *app_evt = &_app_evt;
242 session_disconnect_msg_t *mp;
243 svm_msg_q_t *mq;
244
245 /* Send to thread that owns the session */
246 mq = s->vpp_evt_q;
247 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
248 mp = (session_disconnect_msg_t *) app_evt->evt->data;
249 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700250 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700251 mp->handle = s->vpp_handle;
252 app_send_ctrl_evt_to_vpp (mq, app_evt);
253}
254
255static void
256vcl_send_app_detach (vcl_worker_t * wrk)
257{
258 app_session_evt_t _app_evt, *app_evt = &_app_evt;
259 session_app_detach_msg_t *mp;
260 svm_msg_q_t *mq;
261
262 mq = vcl_worker_ctrl_mq (wrk);
263 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
264 mp = (session_app_detach_msg_t *) app_evt->evt->data;
265 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700266 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700267 app_send_ctrl_evt_to_vpp (mq, app_evt);
268}
Florin Coras697faea2018-06-27 17:10:49 -0700269
Florin Coras54693d22018-07-17 10:46:29 -0700270static void
271vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
272 session_handle_t handle, int retval)
273{
274 app_session_evt_t _app_evt, *app_evt = &_app_evt;
275 session_accepted_reply_msg_t *rmp;
276 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
277 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
278 rmp->handle = handle;
279 rmp->context = context;
280 rmp->retval = retval;
281 app_send_ctrl_evt_to_vpp (mq, app_evt);
282}
283
Florin Coras99368312018-08-02 10:45:44 -0700284static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800285vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
286 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700287{
288 app_session_evt_t _app_evt, *app_evt = &_app_evt;
289 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800290 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700291 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
292 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800293 rmp->handle = s->vpp_handle;
294 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700295 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800296 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700297}
298
Florin Corasc9fbd662018-08-24 12:59:56 -0700299static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800300vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
301 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700302{
303 app_session_evt_t _app_evt, *app_evt = &_app_evt;
304 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800305 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
306 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700307 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800308 rmp->handle = s->vpp_handle;
309 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700310 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800311 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700312}
313
Florin Coras30e79c22019-01-02 19:31:22 -0800314void
315vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
316 u32 wrk_index)
317{
318 app_session_evt_t _app_evt, *app_evt = &_app_evt;
319 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800320
Florin Coras52dd29f2020-11-18 19:02:17 -0800321 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
322 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800323 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700324 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800325 mp->handle = s->vpp_handle;
326 mp->req_wrk_index = wrk->vpp_wrk_index;
327 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800328 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800329}
330
hanlina3a48962020-07-13 11:09:15 +0800331int
Florin Coras40c07ce2020-07-16 20:46:17 -0700332vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
333{
334 app_session_evt_t _app_evt, *app_evt = &_app_evt;
335 session_app_wrk_rpc_msg_t *mp;
336 vcl_worker_t *dst_wrk, *wrk;
337 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800338 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700339
340 if (data_len > sizeof (mp->data))
341 goto done;
342
343 clib_spinlock_lock (&vcm->workers_lock);
344
345 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
346 if (!dst_wrk)
347 goto done;
348
349 wrk = vcl_worker_get_current ();
350 mq = vcl_worker_ctrl_mq (wrk);
351 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
352 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700353 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700354 mp->wrk_index = dst_wrk->vpp_wrk_index;
355 clib_memcpy (mp->data, data, data_len);
356 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800357 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700358
359done:
360 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800361 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700362}
363
Florin Coras54693d22018-07-17 10:46:29 -0700364static u32
Florin Coras00cca802019-06-06 09:38:44 -0700365vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
366 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700367{
368 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700369 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700370
Florin Coras134a9962018-08-28 11:32:04 -0700371 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700372
Florin Coras00cca802019-06-06 09:38:44 -0700373 listen_session = vcl_session_get (wrk, ls_index);
374 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700375 {
Florin Coras00cca802019-06-06 09:38:44 -0700376 VDBG (0, "ERROR: listener handle %lu does not match session %u",
377 mp->listener_handle, ls_index);
378 goto error;
379 }
380
Florin Corasc547e912020-12-08 17:50:45 -0800381 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800382 mp->server_tx_fifo,
383 mp->vpp_event_queue_address, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700384 {
Florin Corasc547e912020-12-08 17:50:45 -0800385 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Coras00cca802019-06-06 09:38:44 -0700386 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700387 }
388
Florin Coras54693d22018-07-17 10:46:29 -0700389 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700390 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700391 session->transport.rmt_port = mp->rmt.port;
392 session->transport.is_ip4 = mp->rmt.is_ip4;
393 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500394 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700395
Florin Coras134a9962018-08-28 11:32:04 -0700396 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700397 session->transport.lcl_port = listen_session->transport.lcl_port;
398 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700399 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200400 session->is_dgram = vcl_proto_is_dgram (session->session_type);
401 session->listener_index = listen_session->session_index;
402 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700403
Florin Coras5e062572019-03-14 19:07:51 -0700404 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
405 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700406 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
407 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
408 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700409 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
410
Florin Coras00cca802019-06-06 09:38:44 -0700411 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
412 session->vpp_handle, 0);
413
Florin Coras134a9962018-08-28 11:32:04 -0700414 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700415
416error:
Florin Corasb4624182020-12-11 13:58:12 -0800417 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
418 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700419 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
420 VNET_API_ERROR_INVALID_ARGUMENT);
421 vcl_session_free (wrk, session);
422 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700423}
424
425static u32
Florin Coras134a9962018-08-28 11:32:04 -0700426vcl_session_connected_handler (vcl_worker_t * wrk,
427 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700428{
Florin Coras99368312018-08-02 10:45:44 -0700429 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800430 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700431
432 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700433 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700434 if (!session)
435 {
Florin Coras5e062572019-03-14 19:07:51 -0700436 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
437 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700438 return VCL_INVALID_SESSION_INDEX;
439 }
Florin Coras54693d22018-07-17 10:46:29 -0700440 if (mp->retval)
441 {
Florin Coras5e062572019-03-14 19:07:51 -0700442 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700443 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700444 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700445 session->vpp_handle = mp->handle;
446 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700447 }
448
Florin Corasdbc9c592019-09-25 16:37:43 -0700449 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800450
451 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800452 mp->server_tx_fifo,
453 mp->vpp_event_queue_address, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800454 {
Florin Corasc547e912020-12-08 17:50:45 -0800455 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700456 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700457 vcl_send_session_disconnect (wrk, session);
458 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800459 }
460
Florin Coras653e43f2019-03-04 10:56:23 -0800461 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700462 {
Florin Corasc547e912020-12-08 17:50:45 -0800463 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800464 mp->ct_tx_fifo, (uword) ~0, 1, session))
Florin Coras653e43f2019-03-04 10:56:23 -0800465 {
Florin Corasc547e912020-12-08 17:50:45 -0800466 VDBG (0, "failed to attach ct fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700467 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700468 vcl_send_session_disconnect (wrk, session);
469 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800470 }
Florin Coras99368312018-08-02 10:45:44 -0700471 }
Florin Coras54693d22018-07-17 10:46:29 -0700472
Florin Coras09d18c22019-04-24 11:10:02 -0700473 session->transport.is_ip4 = mp->lcl.is_ip4;
474 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500475 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700476 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700477
478 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700479 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700480 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700481 vcl_send_session_disconnect (wrk, session);
482 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700483 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700484
485 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800486 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700487
Florin Coras5e062572019-03-14 19:07:51 -0700488 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
489 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700490 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700491
Florin Coras54693d22018-07-17 10:46:29 -0700492 return session_index;
493}
494
Florin Coras3c7d4f92018-12-14 11:28:43 -0800495static int
496vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
497{
498 vcl_session_msg_t *accepted_msg;
499 int i;
500
501 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
502 {
503 accepted_msg = &session->accept_evts_fifo[i];
504 if (accepted_msg->accepted_msg.handle == handle)
505 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800506 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800507 return 1;
508 }
509 }
510 return 0;
511}
512
Florin Corasc9fbd662018-08-24 12:59:56 -0700513static u32
Florin Coras134a9962018-08-28 11:32:04 -0700514vcl_session_reset_handler (vcl_worker_t * wrk,
515 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700516{
517 vcl_session_t *session;
518 u32 sid;
519
Florin Coras134a9962018-08-28 11:32:04 -0700520 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
521 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700522 if (!session)
523 {
524 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
525 return VCL_INVALID_SESSION_INDEX;
526 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800527
528 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700529 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800530 {
531
532 if (!vcl_flag_accepted_session (session, reset_msg->handle,
533 VCL_ACCEPTED_F_RESET))
534 VDBG (0, "session was not accepted!");
535 return VCL_INVALID_SESSION_INDEX;
536 }
537
Florin Corasc127d5a2020-10-14 16:35:58 -0700538 if (session->session_state != VCL_STATE_CLOSED)
539 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800540 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700541 return sid;
542}
543
Florin Coras60116992018-08-27 09:52:18 -0700544static u32
Florin Coras134a9962018-08-28 11:32:04 -0700545vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700546{
547 vcl_session_t *session;
548 u32 sid = mp->context;
549
Florin Coras134a9962018-08-28 11:32:04 -0700550 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700551 if (mp->retval)
552 {
Florin Coras5e062572019-03-14 19:07:51 -0700553 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700554 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700555 if (session)
556 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700557 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700558 session->vpp_handle = mp->handle;
559 return sid;
560 }
561 else
562 {
Florin Coras5e062572019-03-14 19:07:51 -0700563 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
564 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700565 return VCL_INVALID_SESSION_INDEX;
566 }
567 }
568
569 session->vpp_handle = mp->handle;
570 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500571 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
572 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700573 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700574 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700575 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800576
Florin Coras6e3c1f82020-01-15 01:30:46 +0000577 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700578 {
Florin Corasc547e912020-12-08 17:50:45 -0800579 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800580 mp->tx_fifo, mp->vpp_evt_q, 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800581 {
582 VDBG (0, "failed to attach fifos for %u", session->session_index);
583 session->session_state = VCL_STATE_DETACHED;
584 return VCL_INVALID_SESSION_INDEX;
585 }
Florin Coras60116992018-08-27 09:52:18 -0700586 }
587
Florin Coras05ecfcc2018-12-12 18:19:39 -0800588 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700589 return sid;
590}
591
Florin Corasdfae9f92019-02-20 19:48:31 -0800592static void
593vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
594{
595 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
596 vcl_session_t *s;
597
598 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000599 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800600 {
601 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
602 return;
603 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700604 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000605 {
606 /* Connected udp listener */
607 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700608 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000609 return;
610
611 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
612 return;
613 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800614
615 if (mp->retval)
616 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700617 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800618
619 if (mp->context != wrk->wrk_index)
620 VDBG (0, "wrong context");
621
622 vcl_session_table_del_vpp_handle (wrk, mp->handle);
623 vcl_session_free (wrk, s);
624}
625
Florin Coras68b7e582020-01-21 18:33:23 -0800626static void
627vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
628{
629 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
630 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800631 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800632
633 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
634 if (!s)
635 {
636 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
637 return;
638 }
639
Florin Corasc547e912020-12-08 17:50:45 -0800640 fs_index = vcl_segment_table_lookup (mp->segment_handle);
641 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
642 {
643 VDBG (0, "segment for session %u is not mounted!", s->session_index);
644 s->session_state = VCL_STATE_DETACHED;
645 return;
646 }
647
Florin Coras57660d92020-04-04 22:45:34 +0000648 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800649
650 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
651 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800652
Florin Coras68b7e582020-01-21 18:33:23 -0800653 vcl_session_table_del_vpp_handle (wrk, mp->handle);
654 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
655
656 /* Generate new tx event if we have outstanding data */
657 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800658 app_send_io_evt_to_vpp (s->vpp_evt_q,
659 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800660 SESSION_IO_EVT_TX, SVM_Q_WAIT);
661
Florin Coras57660d92020-04-04 22:45:34 +0000662 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800663 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800664}
665
Florin Coras3c7d4f92018-12-14 11:28:43 -0800666static vcl_session_t *
667vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
668{
669 vcl_session_msg_t *vcl_msg;
670 vcl_session_t *session;
671
672 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
673 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800674 VWRN ("session overlap handle %lu state %u!", msg->handle,
675 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800676
677 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
678 if (!session)
679 {
680 VERR ("couldn't find listen session: listener handle %llx",
681 msg->listener_handle);
682 return 0;
683 }
684
685 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000686 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800687 vcl_msg->accepted_msg = *msg;
688 /* Session handle points to listener until fully accepted by app */
689 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
690
691 return session;
692}
693
694static vcl_session_t *
695vcl_session_disconnected_handler (vcl_worker_t * wrk,
696 session_disconnected_msg_t * msg)
697{
698 vcl_session_t *session;
699
700 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
701 if (!session)
702 {
703 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
704 return 0;
705 }
706
Florin Corasadcfb152020-02-12 08:50:29 +0000707 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700708 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000709 return 0;
710
Florin Coras3c7d4f92018-12-14 11:28:43 -0800711 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700712 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800713 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800714 if (!vcl_flag_accepted_session (session, msg->handle,
715 VCL_ACCEPTED_F_CLOSED))
716 VDBG (0, "session was not accepted!");
717 return 0;
718 }
719
Florin Corasadcfb152020-02-12 08:50:29 +0000720 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700721 if (session->session_state != VCL_STATE_DISCONNECT)
722 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000723
Florin Coras3c7d4f92018-12-14 11:28:43 -0800724 return session;
725}
726
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700727static int
728vppcom_session_disconnect (u32 session_handle)
729{
730 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700731 vcl_session_t *session, *listen_session;
732 vcl_session_state_t state;
733 u64 vpp_handle;
734
735 session = vcl_session_get_w_handle (wrk, session_handle);
736 if (!session)
737 return VPPCOM_EBADFD;
738
739 vpp_handle = session->vpp_handle;
740 state = session->session_state;
741
742 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
743 vpp_handle, state, vppcom_session_state_str (state));
744
745 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
746 {
747 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
748 return VPPCOM_EBADFD;
749 }
750
751 if (state == VCL_STATE_VPP_CLOSING)
752 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800753 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700754 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
755 session->session_index, vpp_handle);
756 }
757 else
758 {
759 /* Session doesn't have an event queue yet. Probably a non-blocking
760 * connect. Wait for the reply */
761 if (PREDICT_FALSE (!session->vpp_evt_q))
762 return VPPCOM_OK;
763
764 VDBG (1, "session %u [0x%llx]: sending disconnect...",
765 session->session_index, vpp_handle);
766 vcl_send_session_disconnect (wrk, session);
767 }
768
769 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
770 {
771 listen_session = vcl_session_get (wrk, session->listener_index);
772 listen_session->n_accepted_sessions--;
773 }
774
775 return VPPCOM_OK;
776}
777
Florin Coras30e79c22019-01-02 19:31:22 -0800778static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700779vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
780{
781 session_cleanup_msg_t *msg;
782 vcl_session_t *session;
783
784 msg = (session_cleanup_msg_t *) data;
785 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
786 if (!session)
787 {
788 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
789 return;
790 }
791
Florin Coras36d49392020-04-24 23:00:11 +0000792 if (msg->type == SESSION_CLEANUP_TRANSPORT)
793 {
794 /* Transport was cleaned up before we confirmed close. Probably the
795 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700796 * Confirm close to make sure everything is cleaned up.
797 * Move to undetermined state to ensure that the session is not
798 * removed before both vpp and the app cleanup.
799 * - If the app closes first, the session is moved to CLOSED state
800 * and the session cleanup notification from vpp removes the
801 * session.
802 * - If vpp cleans up the session first, the session is moved to
803 * DETACHED state lower and subsequently the close from the app
804 * frees the session
805 */
806 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700807 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700808 vppcom_session_disconnect (vcl_session_handle (session));
809 session->session_state = VCL_STATE_UPDATED;
810 }
811 else if (session->session_state == VCL_STATE_DISCONNECT)
812 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800813 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700814 session->session_state = VCL_STATE_UPDATED;
815 }
Florin Coras36d49392020-04-24 23:00:11 +0000816 return;
817 }
818
Florin Coras9ace36d2019-10-28 13:14:17 -0700819 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000820 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700821 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000822 {
823 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700824 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000825 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
826 return;
827 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700828 vcl_session_free (wrk, session);
829}
830
831static void
Florin Coras30e79c22019-01-02 19:31:22 -0800832vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
833{
834 session_req_worker_update_msg_t *msg;
835 vcl_session_t *s;
836
837 msg = (session_req_worker_update_msg_t *) data;
838 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
839 if (!s)
840 return;
841
842 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
843}
844
845static void
846vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
847{
848 session_worker_update_reply_msg_t *msg;
849 vcl_session_t *s;
850
851 msg = (session_worker_update_reply_msg_t *) data;
852 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
853 if (!s)
854 {
855 VDBG (0, "unknown handle 0x%llx", msg->handle);
856 return;
857 }
Florin Coras30e79c22019-01-02 19:31:22 -0800858
Florin Coras5f45e012019-01-23 09:21:30 -0800859 if (s->rx_fifo)
860 {
Florin Corasc547e912020-12-08 17:50:45 -0800861 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800862 msg->tx_fifo, (uword) ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800863 {
864 VDBG (0, "failed to attach fifos for %u", s->session_index);
865 return;
866 }
Florin Coras5f45e012019-01-23 09:21:30 -0800867 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700868 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800869
Florin Coras30e79c22019-01-02 19:31:22 -0800870 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
871 s->vpp_handle, wrk->wrk_index);
872}
873
Florin Coras935ce752020-09-08 22:43:47 -0700874static int
875vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
876{
877
878 if (vcm->cfg.vpp_app_socket_api)
879 return vcl_sapi_recv_fds (wrk, fds, n_fds);
880
881 return vcl_bapi_recv_fds (wrk, fds, n_fds);
882}
883
Florin Corasc4c4cf52019-08-24 18:17:34 -0700884static void
885vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
886{
887 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
888 session_app_add_segment_msg_t *msg;
889 u64 segment_handle;
890 int fd = -1;
891
892 msg = (session_app_add_segment_msg_t *) data;
893
894 if (msg->fd_flags)
895 {
Florin Coras935ce752020-09-08 22:43:47 -0700896 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700897 seg_type = SSVM_SEGMENT_MEMFD;
898 }
899
900 segment_handle = msg->segment_handle;
901 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
902 {
903 clib_warning ("invalid segment handle");
904 return;
905 }
906
907 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
908 seg_type, fd))
909 {
910 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
911 return;
912 }
913
914 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
915 msg->segment_size);
916}
917
918static void
919vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
920{
921 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
922 vcl_segment_detach (msg->segment_handle);
923 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
924}
925
Florin Coras40c07ce2020-07-16 20:46:17 -0700926static void
927vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
928{
929 if (!vcm->wrk_rpc_fn)
930 return;
931
hanlina3a48962020-07-13 11:09:15 +0800932 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700933}
934
Florin Coras86f04502018-09-12 16:08:01 -0700935static int
936vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700937{
Florin Coras54693d22018-07-17 10:46:29 -0700938 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700939 session_connected_msg_t *connected_msg;
940 session_reset_msg_t *reset_msg;
941 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -0700942 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -0700943 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -0700944
945 switch (e->event_type)
946 {
Florin Coras653e43f2019-03-04 10:56:23 -0800947 case SESSION_IO_EVT_RX:
948 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -0700949 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -0700950 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -0800951 break;
Florin Coras86f04502018-09-12 16:08:01 -0700952 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700953 break;
Florin Corasb242d312020-10-26 15:35:40 -0700954 case SESSION_CTRL_EVT_BOUND:
955 /* We can only wait for only one listen so not postponed */
956 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
957 break;
Florin Coras54693d22018-07-17 10:46:29 -0700958 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -0700959 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
960 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
961 {
962 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
963 *ecpy = *e;
964 ecpy->postponed = 1;
965 ecpy->session_index = s->session_index;
966 }
Florin Coras54693d22018-07-17 10:46:29 -0700967 break;
968 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -0700969 connected_msg = (session_connected_msg_t *) e->data;
970 sid = vcl_session_connected_handler (wrk, connected_msg);
971 if (!(s = vcl_session_get (wrk, sid)))
972 break;
973 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
974 {
975 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
976 *ecpy = *e;
977 ecpy->postponed = 1;
978 ecpy->session_index = s->session_index;
979 }
Florin Coras54693d22018-07-17 10:46:29 -0700980 break;
981 case SESSION_CTRL_EVT_DISCONNECTED:
982 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -0700983 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
984 break;
985 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
986 {
987 vec_add1 (wrk->unhandled_evts_vector, *e);
988 break;
989 }
990 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -0800991 break;
Florin Corasc127d5a2020-10-14 16:35:58 -0700992 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
993 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700994 break;
995 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -0700996 reset_msg = (session_reset_msg_t *) e->data;
997 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
998 break;
999 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1000 {
1001 vec_add1 (wrk->unhandled_evts_vector, *e);
1002 break;
1003 }
Florin Coras134a9962018-08-28 11:32:04 -07001004 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001005 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001006 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1007 vcl_session_unlisten_reply_handler (wrk, e->data);
1008 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001009 case SESSION_CTRL_EVT_MIGRATED:
1010 vcl_session_migrated_handler (wrk, e->data);
1011 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001012 case SESSION_CTRL_EVT_CLEANUP:
1013 vcl_session_cleanup_handler (wrk, e->data);
1014 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001015 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1016 vcl_session_req_worker_update_handler (wrk, e->data);
1017 break;
1018 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1019 vcl_session_worker_update_reply_handler (wrk, e->data);
1020 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001021 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1022 vcl_session_app_add_segment_handler (wrk, e->data);
1023 break;
1024 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1025 vcl_session_app_del_segment_handler (wrk, e->data);
1026 break;
hanlina3a48962020-07-13 11:09:15 +08001027 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001028 vcl_worker_rpc_handler (wrk, e->data);
1029 break;
Florin Coras54693d22018-07-17 10:46:29 -07001030 default:
1031 clib_warning ("unhandled %u", e->event_type);
1032 }
1033 return VPPCOM_OK;
1034}
1035
Florin Coras30e79c22019-01-02 19:31:22 -08001036static int
Florin Coras697faea2018-06-27 17:10:49 -07001037vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001038 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001039 f64 wait_for_time)
1040{
Florin Coras134a9962018-08-28 11:32:04 -07001041 vcl_worker_t *wrk = vcl_worker_get_current ();
1042 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001043 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001044 svm_msg_q_msg_t msg;
1045 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001046
Florin Coras697faea2018-06-27 17:10:49 -07001047 do
Dave Wallace543852a2017-08-03 02:11:34 -04001048 {
Florin Coras134a9962018-08-28 11:32:04 -07001049 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001050 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001051 {
Florin Coras070453d2018-08-24 17:04:27 -07001052 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001053 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001054 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001055 {
Florin Coras697faea2018-06-27 17:10:49 -07001056 return VPPCOM_OK;
1057 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001058 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001059 {
Florin Coras697faea2018-06-27 17:10:49 -07001060 return VPPCOM_ECONNREFUSED;
1061 }
Florin Coras54693d22018-07-17 10:46:29 -07001062
Florin Coras134a9962018-08-28 11:32:04 -07001063 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001064 {
1065 usleep (100);
1066 continue;
1067 }
Florin Coras134a9962018-08-28 11:32:04 -07001068 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001069 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001070 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001071 }
Florin Coras134a9962018-08-28 11:32:04 -07001072 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001073
Florin Coras05ecfcc2018-12-12 18:19:39 -08001074 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001075 vppcom_session_state_str (state));
1076 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001077
Florin Coras697faea2018-06-27 17:10:49 -07001078 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001079}
1080
Florin Coras30e79c22019-01-02 19:31:22 -08001081static void
1082vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1083{
Florin Coras288eaab2019-02-03 15:26:14 -08001084 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001085 vcl_session_t *s;
1086 u32 *sip;
1087
1088 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1089 return;
1090
1091 vec_foreach (sip, wrk->pending_session_wrk_updates)
1092 {
1093 s = vcl_session_get (wrk, *sip);
1094 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1095 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001096 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1097 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001098 s->session_state = state;
1099 }
1100 vec_reset_length (wrk->pending_session_wrk_updates);
1101}
1102
Florin Corasf9240dc2019-01-15 08:03:17 -08001103void
Florin Coras30e79c22019-01-02 19:31:22 -08001104vcl_flush_mq_events (void)
1105{
1106 vcl_worker_t *wrk = vcl_worker_get_current ();
1107 svm_msg_q_msg_t *msg;
1108 session_event_t *e;
1109 svm_msg_q_t *mq;
1110 int i;
1111
1112 mq = wrk->app_event_queue;
1113 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -07001114 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001115 svm_msg_q_unlock (mq);
1116
1117 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1118 {
1119 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1120 e = svm_msg_q_msg_data (mq, msg);
1121 vcl_handle_mq_event (wrk, e);
1122 svm_msg_q_free_msg (mq, msg);
1123 }
1124 vec_reset_length (wrk->mq_msg_vector);
1125 vcl_handle_pending_wrk_updates (wrk);
1126}
1127
Florin Coras697faea2018-06-27 17:10:49 -07001128static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001129vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001130{
Florin Coras134a9962018-08-28 11:32:04 -07001131 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001132 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001133 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001134 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001135
Florin Corasab2f6db2018-08-31 14:31:41 -07001136 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001137 if (!session)
1138 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001139
Florin Corasaaff5ee2019-07-08 13:00:00 -07001140 /* Flush pending accept events, if any */
1141 while (clib_fifo_elts (session->accept_evts_fifo))
1142 {
1143 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1144 accepted_msg = &evt->accepted_msg;
1145 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1146 vcl_send_session_accepted_reply (session->vpp_evt_q,
1147 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001148 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001149 }
1150 clib_fifo_free (session->accept_evts_fifo);
1151
Florin Coras458089b2019-08-21 16:20:44 -07001152 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001153
Florin Coras5e062572019-03-14 19:07:51 -07001154 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001155 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001156 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001157
1158 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001159 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001160
Florin Coras070453d2018-08-24 17:04:27 -07001161 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001162}
1163
Florin Coras940f78f2018-11-30 12:11:20 -08001164/**
1165 * Handle app exit
1166 *
1167 * Notify vpp of the disconnect and mark the worker as free. If we're the
1168 * last worker, do a full cleanup otherwise, since we're probably a forked
1169 * child, avoid syscalls as much as possible. We might've lost privileges.
1170 */
1171void
1172vppcom_app_exit (void)
1173{
1174 if (!pool_elts (vcm->workers))
1175 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001176 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1177 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001178 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001179}
1180
Florin Coras935ce752020-09-08 22:43:47 -07001181static int
1182vcl_api_attach (void)
1183{
1184 if (vcm->cfg.vpp_app_socket_api)
1185 return vcl_sapi_attach ();
1186
1187 return vcl_bapi_attach ();
1188}
1189
1190static void
1191vcl_api_detach (vcl_worker_t * wrk)
1192{
1193 vcl_send_app_detach (wrk);
1194
1195 if (vcm->cfg.vpp_app_socket_api)
1196 return vcl_sapi_detach (wrk);
1197
1198 return vcl_bapi_disconnect_from_vpp ();
1199}
1200
Dave Wallace543852a2017-08-03 02:11:34 -04001201/*
1202 * VPPCOM Public API functions
1203 */
1204int
Florin Coras66ec4672020-06-15 07:59:40 -07001205vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001206{
Dave Wallace543852a2017-08-03 02:11:34 -04001207 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001208 int rv;
1209
Florin Coras47c40e22018-11-26 17:01:36 -08001210 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001211 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001212 VDBG (1, "already initialized");
1213 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001214 }
1215
Florin Coras47c40e22018-11-26 17:01:36 -08001216 vcm->is_init = 1;
1217 vppcom_cfg (&vcm->cfg);
1218 vcl_cfg = &vcm->cfg;
1219
1220 vcm->main_cpu = pthread_self ();
1221 vcm->main_pid = getpid ();
1222 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001223 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1224 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001225 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1226 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001227 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001228 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001229 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001230
1231 /* Allocate default worker */
1232 vcl_worker_alloc_and_init ();
1233
Florin Coras935ce752020-09-08 22:43:47 -07001234 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001235 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001236
1237 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001238 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001239
1240 return VPPCOM_OK;
1241}
1242
1243void
1244vppcom_app_destroy (void)
1245{
Florin Corasdc0ded72020-04-30 02:59:55 +00001246 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001247 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001248
Florin Coras940f78f2018-11-30 12:11:20 -08001249 if (!pool_elts (vcm->workers))
1250 return;
1251
Florin Coras0d427d82018-06-27 03:24:07 -07001252 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001253
Florin Corasdc0ded72020-04-30 02:59:55 +00001254 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001255
Florin Corasce815de2020-04-16 18:47:27 +00001256 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001257 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001258 if (current_wrk != wrk)
1259 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001260 }
Florin Corasce815de2020-04-16 18:47:27 +00001261 /* *INDENT-ON* */
1262
Florin Coras935ce752020-09-08 22:43:47 -07001263 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001264 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1265
Florin Coras0d427d82018-06-27 03:24:07 -07001266 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001267
1268 /*
1269 * Free the heap and fix vcm
1270 */
1271 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001272 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001273
1274 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001275 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001276}
1277
1278int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001279vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001280{
Florin Coras134a9962018-08-28 11:32:04 -07001281 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001282 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001283
Florin Coras134a9962018-08-28 11:32:04 -07001284 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001285
Florin Coras7e12d942018-06-27 14:32:43 -07001286 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001287 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001288 session->vpp_handle = ~0;
Florin Corasa5a9efd2021-01-05 17:03:29 -08001289 session->ckpair_index = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001290 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001291
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001292 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001293 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001294
Florin Coras7e12d942018-06-27 14:32:43 -07001295 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1296 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001297
Florin Coras5e062572019-03-14 19:07:51 -07001298 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001299
Florin Coras134a9962018-08-28 11:32:04 -07001300 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001301}
1302
1303int
Florin Corasc127d5a2020-10-14 16:35:58 -07001304vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001305 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001306{
Florin Coras134a9962018-08-28 11:32:04 -07001307 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001308
Florin Coras6c3b2182020-10-19 18:36:48 -07001309 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001310
Florin Coras6c3b2182020-10-19 18:36:48 -07001311 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001312 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001313 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001314 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001315 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001316 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001317 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001318 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001319 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1320 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001321 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001322 }
Florin Corase4306b62020-10-28 12:51:10 -07001323 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001324 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001325
Florin Coras6c3b2182020-10-19 18:36:48 -07001326 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001327 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001328 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001329 if (rv < 0)
1330 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001331 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1332 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001333 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001334
Florin Coras9ace36d2019-10-28 13:14:17 -07001335 if (!do_disconnect)
1336 {
1337 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001338 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001339 goto cleanup;
1340 }
1341
Florin Coras6c3b2182020-10-19 18:36:48 -07001342 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001343 {
1344 rv = vppcom_session_unbind (sh);
1345 if (PREDICT_FALSE (rv < 0))
1346 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001347 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001348 vppcom_retval_str (rv));
1349 return rv;
1350 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001351 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001352 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001353 {
1354 rv = vppcom_session_disconnect (sh);
1355 if (PREDICT_FALSE (rv < 0))
1356 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001357 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001358 rv, vppcom_retval_str (rv));
1359 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001360 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001361 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001362 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001363 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001364 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001365 {
1366 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001367 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001368 goto free_session;
1369 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001370
Florin Corasc127d5a2020-10-14 16:35:58 -07001371 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001372
Florin Coras9ace36d2019-10-28 13:14:17 -07001373 /* Session is removed only after vpp confirms the disconnect */
1374 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001375
Florin Corasf9240dc2019-01-15 08:03:17 -08001376cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001377 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001378free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001379 vcl_session_free (wrk, s);
1380 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001381
Dave Wallace543852a2017-08-03 02:11:34 -04001382 return rv;
1383}
1384
1385int
Florin Corasf9240dc2019-01-15 08:03:17 -08001386vppcom_session_close (uint32_t session_handle)
1387{
1388 vcl_worker_t *wrk = vcl_worker_get_current ();
1389 vcl_session_t *session;
1390
1391 session = vcl_session_get_w_handle (wrk, session_handle);
1392 if (!session)
1393 return VPPCOM_EBADFD;
1394 return vcl_session_cleanup (wrk, session, session_handle,
1395 1 /* do_disconnect */ );
1396}
1397
1398int
Florin Coras134a9962018-08-28 11:32:04 -07001399vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001400{
Florin Coras134a9962018-08-28 11:32:04 -07001401 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001402 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001403
1404 if (!ep || !ep->ip)
1405 return VPPCOM_EINVAL;
1406
Florin Coras134a9962018-08-28 11:32:04 -07001407 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001408 if (!session)
1409 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001410
Florin Coras6c3b2182020-10-19 18:36:48 -07001411 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001412 {
Florin Coras5e062572019-03-14 19:07:51 -07001413 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1414 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001415 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001416 }
1417
Florin Coras7e12d942018-06-27 14:32:43 -07001418 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001419 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001420 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1421 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001422 else
Dave Barach178cf492018-11-13 16:34:13 -05001423 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1424 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001425 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001426
Florin Coras5e062572019-03-14 19:07:51 -07001427 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1428 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001429 session->transport.is_ip4 ? "IPv4" : "IPv6",
1430 format_ip46_address, &session->transport.lcl_ip,
1431 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1432 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001433 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001434 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001435
1436 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001437 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001438
Florin Coras070453d2018-08-24 17:04:27 -07001439 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001440}
1441
1442int
Florin Coras134a9962018-08-28 11:32:04 -07001443vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001444{
Florin Coras134a9962018-08-28 11:32:04 -07001445 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001446 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001447 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001448 int rv;
1449
Florin Coras134a9962018-08-28 11:32:04 -07001450 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001451 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001452 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001453
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001454 if (q_len == 0 || q_len == ~0)
1455 q_len = vcm->cfg.listen_queue_size;
1456
Dave Wallaceee45d412017-11-24 21:44:06 -05001457 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001458 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001459 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001460 VDBG (0, "session %u [0x%llx]: already in listen state!",
1461 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001462 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001463 }
1464
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001465 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001466
Florin Coras070453d2018-08-24 17:04:27 -07001467 /*
1468 * Send listen request to vpp and wait for reply
1469 */
Florin Coras458089b2019-08-21 16:20:44 -07001470 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001471 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001472 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001473 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001474
Florin Coras070453d2018-08-24 17:04:27 -07001475 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001476 {
Florin Coras134a9962018-08-28 11:32:04 -07001477 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001478 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1479 listen_sh, listen_session->vpp_handle, rv,
1480 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001481 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001482 }
1483
Florin Coras070453d2018-08-24 17:04:27 -07001484 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001485}
1486
Florin Coras134a9962018-08-28 11:32:04 -07001487static int
Florin Coras5e062572019-03-14 19:07:51 -07001488validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001489{
Florin Coras6c3b2182020-10-19 18:36:48 -07001490 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001491 {
Florin Coras5e062572019-03-14 19:07:51 -07001492 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1493 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001494 return VPPCOM_EBADFD;
1495 }
1496
Florin Corasc127d5a2020-10-14 16:35:58 -07001497 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001498 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001499 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001500 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001501 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001502 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001503 return VPPCOM_EBADFD;
1504 }
1505 return VPPCOM_OK;
1506}
1507
1508int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001509vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1510{
1511 if (!strcmp (proto_str, "TCP"))
1512 *proto = VPPCOM_PROTO_TCP;
1513 else if (!strcmp (proto_str, "tcp"))
1514 *proto = VPPCOM_PROTO_TCP;
1515 else if (!strcmp (proto_str, "UDP"))
1516 *proto = VPPCOM_PROTO_UDP;
1517 else if (!strcmp (proto_str, "udp"))
1518 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001519 else if (!strcmp (proto_str, "TLS"))
1520 *proto = VPPCOM_PROTO_TLS;
1521 else if (!strcmp (proto_str, "tls"))
1522 *proto = VPPCOM_PROTO_TLS;
1523 else if (!strcmp (proto_str, "QUIC"))
1524 *proto = VPPCOM_PROTO_QUIC;
1525 else if (!strcmp (proto_str, "quic"))
1526 *proto = VPPCOM_PROTO_QUIC;
1527 else
1528 return 1;
1529 return 0;
1530}
1531
1532int
Florin Coras134a9962018-08-28 11:32:04 -07001533vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001534 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001535{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001536 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001537 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001538 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001539 vcl_session_t *listen_session = 0;
1540 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001541 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001542 svm_msg_q_msg_t msg;
1543 session_event_t *e;
1544 u8 is_nonblocking;
1545 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001546
Florin Coras134a9962018-08-28 11:32:04 -07001547 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001548 if (!listen_session)
1549 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001550
Florin Coras134a9962018-08-28 11:32:04 -07001551 listen_session_index = listen_session->session_index;
1552 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001553 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001554
Florin Coras54693d22018-07-17 10:46:29 -07001555 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001556 {
Florin Coras54693d22018-07-17 10:46:29 -07001557 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001558 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001559 accepted_msg = evt->accepted_msg;
1560 goto handle;
1561 }
1562
Florin Corasac422d62020-10-19 20:51:36 -07001563 is_nonblocking = vcl_session_has_attr (listen_session,
1564 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001565 while (1)
1566 {
Carl Smith592a9092019-11-12 14:57:37 +13001567 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1568 return VPPCOM_EAGAIN;
1569
Florin Coras134a9962018-08-28 11:32:04 -07001570 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001571 return VPPCOM_EAGAIN;
1572
Florin Coras134a9962018-08-28 11:32:04 -07001573 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001574 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001575 {
wanghanlin96453fd2019-12-16 19:14:39 +08001576 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001577 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001578 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001579 }
Dave Barach178cf492018-11-13 16:34:13 -05001580 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001581 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001582 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001583 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001584
Florin Coras54693d22018-07-17 10:46:29 -07001585handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001586
Florin Coras00cca802019-06-06 09:38:44 -07001587 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1588 listen_session_index);
1589 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1590 return VPPCOM_ECONNABORTED;
1591
Florin Coras134a9962018-08-28 11:32:04 -07001592 listen_session = vcl_session_get (wrk, listen_session_index);
1593 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001594
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001595 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001596 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001597
Florin Coras5e062572019-03-14 19:07:51 -07001598 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1599 " flags %d, is_nonblocking %u", listen_session->session_index,
1600 listen_session->vpp_handle, client_session_index,
1601 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001602 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001603
Dave Wallace048b1d62018-01-03 22:24:41 -05001604 if (ep)
1605 {
Florin Coras7e12d942018-06-27 14:32:43 -07001606 ep->is_ip4 = client_session->transport.is_ip4;
1607 ep->port = client_session->transport.rmt_port;
1608 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001609 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1610 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001611 else
Dave Barach178cf492018-11-13 16:34:13 -05001612 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1613 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001614 }
Dave Wallace60caa062017-11-10 17:07:13 -05001615
Florin Coras05ecfcc2018-12-12 18:19:39 -08001616 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001617 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001618 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001619 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001620 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001621 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001622 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001623 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001624 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001625 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1626 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001627
Florin Coras3c7d4f92018-12-14 11:28:43 -08001628 /*
1629 * Session might have been closed already
1630 */
1631 if (accept_flags)
1632 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001633 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001634 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001635 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001636 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001637 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001638 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001639}
1640
1641int
Florin Coras134a9962018-08-28 11:32:04 -07001642vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001643{
Florin Coras134a9962018-08-28 11:32:04 -07001644 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001645 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001646 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001647 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001648
Florin Coras134a9962018-08-28 11:32:04 -07001649 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001650 if (!session)
1651 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001652 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001653
Florin Coras6c3b2182020-10-19 18:36:48 -07001654 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001655 {
Florin Coras5e062572019-03-14 19:07:51 -07001656 VDBG (0, "ERROR: cannot connect epoll session %u!",
1657 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001658 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001659 }
1660
Florin Corasc127d5a2020-10-14 16:35:58 -07001661 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001662 {
Florin Coras7baeb712019-01-04 17:05:43 -08001663 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001664 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001665 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001666 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001667 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001668 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001669 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001670 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001671 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001672 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001673 }
1674
Florin Coras0a1e1832020-03-29 18:54:04 +00001675 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001676 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001677 {
1678 if (session->session_type != VPPCOM_PROTO_UDP)
1679 return VPPCOM_EINVAL;
1680 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001681 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001682 }
1683
Florin Coras7e12d942018-06-27 14:32:43 -07001684 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001685 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001686 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001687 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001688 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001689
Florin Coras0a1e1832020-03-29 18:54:04 +00001690 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001691 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001692 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001693 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001694 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001695 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001696 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001697 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001698 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001699
Florin Coras458089b2019-08-21 16:20:44 -07001700 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001701
Florin Corasac422d62020-10-19 20:51:36 -07001702 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001703 {
fanyfa49d59d2020-10-13 17:07:16 +08001704 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001705 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001706 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001707 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001708 return VPPCOM_EINPROGRESS;
1709 }
Florin Coras57c88932019-08-29 12:03:17 -07001710
1711 /*
1712 * Wait for reply from vpp if blocking
1713 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001714 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001715 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001716
Florin Coras134a9962018-08-28 11:32:04 -07001717 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001718 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1719 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001720
Dave Wallace4878cbe2017-11-21 03:45:09 -05001721 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001722}
1723
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001724int
1725vppcom_session_stream_connect (uint32_t session_handle,
1726 uint32_t parent_session_handle)
1727{
1728 vcl_worker_t *wrk = vcl_worker_get_current ();
1729 vcl_session_t *session, *parent_session;
1730 u32 session_index, parent_session_index;
1731 int rv;
1732
1733 session = vcl_session_get_w_handle (wrk, session_handle);
1734 if (!session)
1735 return VPPCOM_EBADFD;
1736 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1737 if (!parent_session)
1738 return VPPCOM_EBADFD;
1739
1740 session_index = session->session_index;
1741 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001742 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001743 {
1744 VDBG (0, "ERROR: cannot connect epoll session %u!",
1745 session->session_index);
1746 return VPPCOM_EBADFD;
1747 }
1748
Florin Corasc127d5a2020-10-14 16:35:58 -07001749 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001750 {
1751 VDBG (0, "session handle %u [0x%llx]: session already "
1752 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1753 session_handle, session->vpp_handle,
1754 parent_session_handle, parent_session->vpp_handle,
1755 vppcom_proto_str (session->session_type), session->session_state,
1756 vppcom_session_state_str (session->session_state));
1757 return VPPCOM_OK;
1758 }
1759
1760 /* Connect to quic session specifics */
1761 session->transport.is_ip4 = parent_session->transport.is_ip4;
1762 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1763 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001764 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001765
1766 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1767 session_handle, parent_session_handle, parent_session->vpp_handle);
1768
1769 /*
1770 * Send connect request and wait for reply from vpp
1771 */
Florin Coras458089b2019-08-21 16:20:44 -07001772 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001773 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001774 vcm->cfg.session_timeout);
1775
1776 session->listener_index = parent_session_index;
1777 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001778 if (parent_session)
1779 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001780
1781 session = vcl_session_get (wrk, session_index);
1782 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1783 session->vpp_handle, rv ? "failed" : "succeeded");
1784
1785 return rv;
1786}
1787
Florin Coras54693d22018-07-17 10:46:29 -07001788static u8
1789vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1790{
Florin Corasc0737e92019-03-04 14:19:39 -08001791 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001792}
1793
Steven58f464e2017-10-25 12:33:12 -07001794static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001795vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001796 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001797{
Florin Coras134a9962018-08-28 11:32:04 -07001798 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001799 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001800 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001801 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001802 svm_msg_q_msg_t msg;
1803 session_event_t *e;
1804 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001805 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001806
Florin Coras070453d2018-08-24 17:04:27 -07001807 if (PREDICT_FALSE (!buf))
1808 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001809
Florin Coras134a9962018-08-28 11:32:04 -07001810 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001811 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001812 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001813
Florin Coras6d0106e2019-01-29 20:11:58 -08001814 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001815 {
Florin Coras5e062572019-03-14 19:07:51 -07001816 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001817 s->session_index, s->vpp_handle, s->session_state,
1818 vppcom_session_state_str (s->session_state));
1819 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001820 }
1821
Florin Corasac422d62020-10-19 20:51:36 -07001822 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001823 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001824 mq = wrk->app_event_queue;
1825 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001826 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001827
Sirshak Das28aa5392019-02-05 01:33:33 -06001828 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001829 {
Florin Coras54693d22018-07-17 10:46:29 -07001830 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001831 {
Yu Pingb2955352019-12-04 06:49:04 +08001832 if (vcl_session_is_closing (s))
1833 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001834 if (is_ct)
1835 svm_fifo_unset_event (s->rx_fifo);
1836 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001837 return VPPCOM_EWOULDBLOCK;
1838 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001839 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001840 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001841 if (vcl_session_is_closing (s))
1842 return vcl_session_closing_error (s);
1843
Florin Corasd6894562020-10-28 00:37:15 -07001844 if (is_ct)
1845 svm_fifo_unset_event (s->rx_fifo);
1846 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001847 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001848 if (svm_msg_q_is_empty (mq))
1849 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001850
Florin Coras54693d22018-07-17 10:46:29 -07001851 svm_msg_q_sub_w_lock (mq, &msg);
1852 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001853 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001854 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001855 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001856 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001857 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001858 }
Florin Coras54693d22018-07-17 10:46:29 -07001859
Florin Coras4a2c7942020-09-10 12:27:14 -07001860read_again:
1861
Florin Coras460dce62018-07-27 05:45:06 -07001862 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001863 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001864 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001865 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1866
1867 ASSERT (rv >= 0);
1868 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001869
Sirshak Das28aa5392019-02-05 01:33:33 -06001870 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001871 {
Florin Corasd6894562020-10-28 00:37:15 -07001872 if (is_ct)
1873 svm_fifo_unset_event (s->rx_fifo);
1874 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07001875 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07001876 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07001877 {
Florin Corasc34118b2020-08-12 18:58:25 -07001878 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1879 e->event_type = SESSION_IO_EVT_RX;
1880 e->session_index = s->session_index;
1881 }
1882 }
Florin Coras54fe32c2020-11-25 20:26:32 -08001883 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07001884 {
1885 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08001886 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07001887 buf += rv;
1888 n -= rv;
1889 goto read_again;
1890 }
Florin Coras41c9e042018-09-11 00:10:41 -07001891
Florin Coras17ec5772020-08-12 20:46:29 -07001892 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07001893 {
Florin Coras17ec5772020-08-12 20:46:29 -07001894 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08001895 app_send_io_evt_to_vpp (s->vpp_evt_q,
1896 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07001897 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07001898 }
1899
Florin Coras5e062572019-03-14 19:07:51 -07001900 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1901 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001902
Florin Coras54693d22018-07-17 10:46:29 -07001903 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001904}
1905
Steven58f464e2017-10-25 12:33:12 -07001906int
Florin Coras134a9962018-08-28 11:32:04 -07001907vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001908{
Florin Coras134a9962018-08-28 11:32:04 -07001909 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001910}
1911
1912static int
Florin Coras134a9962018-08-28 11:32:04 -07001913vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001914{
Florin Coras134a9962018-08-28 11:32:04 -07001915 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001916}
1917
Florin Coras2cba8532018-09-11 16:33:36 -07001918int
1919vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07001920 vppcom_data_segment_t * ds, uint32_t n_segments,
1921 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001922{
1923 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001924 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001925 vcl_session_t *s = 0;
1926 svm_fifo_t *rx_fifo;
1927 svm_msg_q_msg_t msg;
1928 session_event_t *e;
1929 svm_msg_q_t *mq;
1930 u8 is_ct;
1931
1932 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001933 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07001934 return VPPCOM_EBADFD;
1935
Florin Coras6d0106e2019-01-29 20:11:58 -08001936 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1937 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001938
Florin Corasac422d62020-10-19 20:51:36 -07001939 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07001940 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07001941 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07001942 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001943 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07001944
Sirshak Das28aa5392019-02-05 01:33:33 -06001945 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001946 {
1947 if (is_nonblocking)
1948 {
Florin Coras62877022020-10-28 21:22:04 -07001949 if (is_ct)
1950 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001951 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001952 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001953 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001954 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001955 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001956 if (vcl_session_is_closing (s))
1957 return vcl_session_closing_error (s);
1958
Florin Coras62877022020-10-28 21:22:04 -07001959 if (is_ct)
1960 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001961 svm_fifo_unset_event (rx_fifo);
1962 svm_msg_q_lock (mq);
1963 if (svm_msg_q_is_empty (mq))
1964 svm_msg_q_wait (mq);
1965
1966 svm_msg_q_sub_w_lock (mq, &msg);
1967 e = svm_msg_q_msg_data (mq, &msg);
1968 svm_msg_q_unlock (mq);
1969 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001970 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001971 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001972 }
1973 }
1974
Florin Corasd1cc38d2020-10-11 11:05:04 -07001975 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
1976 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
1977 if (n_read < 0)
1978 return VPPCOM_EAGAIN;
1979
1980 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
1981 {
Florin Coras62877022020-10-28 21:22:04 -07001982 if (is_ct)
1983 svm_fifo_unset_event (s->rx_fifo);
1984 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07001985 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07001986 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07001987 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07001988 {
1989 session_event_t *e;
1990 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1991 e->event_type = SESSION_IO_EVT_RX;
1992 e->session_index = s->session_index;
1993 }
1994 }
1995
1996 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07001997 return n_read;
1998}
1999
2000void
Florin Corasd68faf82020-09-25 15:18:13 -07002001vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002002{
2003 vcl_worker_t *wrk = vcl_worker_get_current ();
2004 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002005 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002006
2007 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002008 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002009 return;
2010
Florin Coras62877022020-10-28 21:22:04 -07002011 is_ct = vcl_session_is_ct (s);
2012 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002013
2014 ASSERT (s->rx_bytes_pending < n_bytes);
2015 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002016}
2017
Florin Coras54693d22018-07-17 10:46:29 -07002018static u8
2019vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
2020{
Florin Corasc0737e92019-03-04 14:19:39 -08002021 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04002022}
2023
Florin Coras7a2abce2020-04-05 19:25:44 +00002024always_inline u8
2025vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002026{
Florin Coras7a2abce2020-04-05 19:25:44 +00002027 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2028 if (is_dgram)
2029 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2030 else
2031 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002032}
2033
2034always_inline int
2035vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2036 size_t n, u8 is_flush, u8 is_dgram)
2037{
Florin Coras6d0106e2019-01-29 20:11:58 -08002038 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002039 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07002040 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08002041 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002042 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07002043 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002044 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002045
jiangxiaomingff31ac62019-11-21 23:34:56 +08002046 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002047 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002048
Florin Coras6c3b2182020-10-19 18:36:48 -07002049 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002050 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002051 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2052 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002053 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002054 }
2055
Florin Coras6d0106e2019-01-29 20:11:58 -08002056 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002057 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002058 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2059 s->session_index, s->vpp_handle, s->session_state,
2060 vppcom_session_state_str (s->session_state));
2061 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002062 }
2063
Florin Coras0e88e852018-09-17 22:09:02 -07002064 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002065 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002066 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002067
Florin Coras653e43f2019-03-04 10:56:23 -08002068 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002069 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002070 {
Florin Coras54693d22018-07-17 10:46:29 -07002071 if (is_nonblocking)
2072 {
Florin Coras070453d2018-08-24 17:04:27 -07002073 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002074 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002075 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002076 {
Florin Coras2d379d82019-06-28 12:45:12 -07002077 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002078 if (vcl_session_is_closing (s))
2079 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07002080 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07002081 if (svm_msg_q_is_empty (mq))
2082 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07002083
Florin Coras54693d22018-07-17 10:46:29 -07002084 svm_msg_q_sub_w_lock (mq, &msg);
2085 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07002086 svm_msg_q_unlock (mq);
2087
Florin Coras0e88e852018-09-17 22:09:02 -07002088 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07002089 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07002090 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07002091 }
Dave Wallace543852a2017-08-03 02:11:34 -04002092 }
Dave Wallace543852a2017-08-03 02:11:34 -04002093
Florin Coras653e43f2019-03-04 10:56:23 -08002094 et = SESSION_IO_EVT_TX;
2095 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002096 et = SESSION_IO_EVT_TX_FLUSH;
2097
Florin Coras7a2abce2020-04-05 19:25:44 +00002098 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002099 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002100 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002101 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002102 else
2103 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002104 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002105
Florin Coras14ed6df2019-03-06 21:13:42 -08002106 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002107 app_send_io_evt_to_vpp (
2108 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002109
Florin Coras56230092020-09-02 20:52:58 -07002110 /* The underlying fifo segment can run out of memory */
2111 if (PREDICT_FALSE (n_write < 0))
2112 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002113
Florin Coras6d0106e2019-01-29 20:11:58 -08002114 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2115 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002116
Florin Coras54693d22018-07-17 10:46:29 -07002117 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002118}
2119
Florin Coras42ceddb2018-12-12 10:56:01 -08002120int
2121vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2122{
Florin Coras7a2abce2020-04-05 19:25:44 +00002123 vcl_worker_t *wrk = vcl_worker_get_current ();
2124 vcl_session_t *s;
2125
2126 s = vcl_session_get_w_handle (wrk, session_handle);
2127 if (PREDICT_FALSE (!s))
2128 return VPPCOM_EBADFD;
2129
2130 return vppcom_session_write_inline (wrk, s, buf, n,
2131 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002132}
2133
Florin Corasb0f662f2018-12-27 14:51:46 -08002134int
2135vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2136{
Florin Coras7a2abce2020-04-05 19:25:44 +00002137 vcl_worker_t *wrk = vcl_worker_get_current ();
2138 vcl_session_t *s;
2139
2140 s = vcl_session_get_w_handle (wrk, session_handle);
2141 if (PREDICT_FALSE (!s))
2142 return VPPCOM_EBADFD;
2143
2144 return vppcom_session_write_inline (wrk, s, buf, n,
2145 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002146}
2147
Florin Corasc0737e92019-03-04 14:19:39 -08002148#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002149if (PREDICT_FALSE (!_s->rx_fifo)) \
2150 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002151if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2152 { \
2153 if (!vcl_session_is_ct (_s)) \
2154 { \
2155 svm_fifo_unset_event (_s->rx_fifo); \
2156 if (svm_fifo_is_empty (_s->rx_fifo)) \
2157 break; \
2158 } \
2159 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2160 { \
Florin Coras2f630182020-08-13 00:17:51 -07002161 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002162 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2163 break; \
2164 } \
2165 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002166
Florin Coras86f04502018-09-12 16:08:01 -07002167static void
2168vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2169 unsigned long n_bits, unsigned long *read_map,
2170 unsigned long *write_map,
2171 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002172{
2173 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002174 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002175 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002176 u32 sid;
2177
2178 switch (e->event_type)
2179 {
Florin Corasc0737e92019-03-04 14:19:39 -08002180 case SESSION_IO_EVT_RX:
2181 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002182 s = vcl_session_get (wrk, sid);
2183 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002184 break;
Florin Corasb242d312020-10-26 15:35:40 -07002185 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002186 if (sid < n_bits && read_map)
2187 {
David Johnsond9818dd2018-12-14 14:53:41 -05002188 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002189 *bits_set += 1;
2190 }
2191 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002192 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002193 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002194 s = vcl_session_get (wrk, sid);
2195 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002196 break;
2197 if (sid < n_bits && write_map)
2198 {
David Johnsond9818dd2018-12-14 14:53:41 -05002199 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002200 *bits_set += 1;
2201 }
2202 break;
Florin Coras86f04502018-09-12 16:08:01 -07002203 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002204 if (!e->postponed)
2205 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2206 else
2207 s = vcl_session_get (wrk, e->session_index);
2208 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002209 break;
Florin Corasb242d312020-10-26 15:35:40 -07002210 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002211 if (sid < n_bits && read_map)
2212 {
David Johnsond9818dd2018-12-14 14:53:41 -05002213 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002214 *bits_set += 1;
2215 }
2216 break;
2217 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002218 if (!e->postponed)
2219 {
2220 connected_msg = (session_connected_msg_t *) e->data;
2221 sid = vcl_session_connected_handler (wrk, connected_msg);
2222 }
2223 else
2224 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002225 if (sid == VCL_INVALID_SESSION_INDEX)
2226 break;
2227 if (sid < n_bits && write_map)
2228 {
2229 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2230 *bits_set += 1;
2231 }
Florin Coras86f04502018-09-12 16:08:01 -07002232 break;
2233 case SESSION_CTRL_EVT_DISCONNECTED:
2234 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002235 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2236 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002237 break;
Florin Corasb242d312020-10-26 15:35:40 -07002238 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002239 if (sid < n_bits && except_map)
2240 {
David Johnsond9818dd2018-12-14 14:53:41 -05002241 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002242 *bits_set += 1;
2243 }
2244 break;
2245 case SESSION_CTRL_EVT_RESET:
2246 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2247 if (sid < n_bits && except_map)
2248 {
David Johnsond9818dd2018-12-14 14:53:41 -05002249 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002250 *bits_set += 1;
2251 }
2252 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002253 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2254 vcl_session_unlisten_reply_handler (wrk, e->data);
2255 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002256 case SESSION_CTRL_EVT_MIGRATED:
2257 vcl_session_migrated_handler (wrk, e->data);
2258 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002259 case SESSION_CTRL_EVT_CLEANUP:
2260 vcl_session_cleanup_handler (wrk, e->data);
2261 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002262 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2263 vcl_session_worker_update_reply_handler (wrk, e->data);
2264 break;
2265 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2266 vcl_session_req_worker_update_handler (wrk, e->data);
2267 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002268 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2269 vcl_session_app_add_segment_handler (wrk, e->data);
2270 break;
2271 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2272 vcl_session_app_del_segment_handler (wrk, e->data);
2273 break;
hanlina3a48962020-07-13 11:09:15 +08002274 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002275 vcl_worker_rpc_handler (wrk, e->data);
2276 break;
Florin Coras86f04502018-09-12 16:08:01 -07002277 default:
2278 clib_warning ("unhandled: %u", e->event_type);
2279 break;
2280 }
2281}
2282
2283static int
2284vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2285 unsigned long n_bits, unsigned long *read_map,
2286 unsigned long *write_map, unsigned long *except_map,
2287 double time_to_wait, u32 * bits_set)
2288{
Florin Coras99368312018-08-02 10:45:44 -07002289 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002290 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002291 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002292
2293 svm_msg_q_lock (mq);
2294 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002295 {
Florin Coras54693d22018-07-17 10:46:29 -07002296 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002297 {
Florin Coras54693d22018-07-17 10:46:29 -07002298 svm_msg_q_unlock (mq);
2299 return 0;
2300 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002301
Florin Coras54693d22018-07-17 10:46:29 -07002302 if (!time_to_wait)
2303 {
2304 svm_msg_q_unlock (mq);
2305 return 0;
2306 }
2307 else if (time_to_wait < 0)
2308 {
2309 svm_msg_q_wait (mq);
2310 }
2311 else
2312 {
2313 if (svm_msg_q_timedwait (mq, time_to_wait))
2314 {
2315 svm_msg_q_unlock (mq);
2316 return 0;
2317 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002318 }
2319 }
Florin Corase003a1b2019-06-05 10:47:16 -07002320 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002321 svm_msg_q_unlock (mq);
2322
Florin Coras134a9962018-08-28 11:32:04 -07002323 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002324 {
Florin Coras134a9962018-08-28 11:32:04 -07002325 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002326 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002327 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2328 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002329 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002330 }
Florin Coras134a9962018-08-28 11:32:04 -07002331 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002332 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002333 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002334}
2335
Florin Coras99368312018-08-02 10:45:44 -07002336static int
Florin Coras294afe22019-01-07 17:49:17 -08002337vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2338 vcl_si_set * read_map, vcl_si_set * write_map,
2339 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002340 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002341{
Florin Coras14ed6df2019-03-06 21:13:42 -08002342 double wait = 0, start = 0;
2343
2344 if (!*bits_set)
2345 {
2346 wait = time_to_wait;
2347 start = clib_time_now (&wrk->clib_time);
2348 }
2349
2350 do
2351 {
2352 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2353 write_map, except_map, wait, bits_set);
2354 if (*bits_set)
2355 return *bits_set;
2356 if (wait == -1)
2357 continue;
2358
2359 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2360 }
2361 while (wait > 0);
2362
2363 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002364}
2365
2366static int
Florin Coras294afe22019-01-07 17:49:17 -08002367vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2368 vcl_si_set * read_map, vcl_si_set * write_map,
2369 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002370 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002371{
2372 vcl_mq_evt_conn_t *mqc;
2373 int __clib_unused n_read;
2374 int n_mq_evts, i;
2375 u64 buf;
2376
Florin Coras134a9962018-08-28 11:32:04 -07002377 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2378 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2379 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002380 for (i = 0; i < n_mq_evts; i++)
2381 {
Florin Coras134a9962018-08-28 11:32:04 -07002382 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002383 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002384 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002385 except_map, 0, bits_set);
2386 }
2387
2388 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2389}
2390
Dave Wallace543852a2017-08-03 02:11:34 -04002391int
Florin Coras294afe22019-01-07 17:49:17 -08002392vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2393 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002394{
Florin Coras54693d22018-07-17 10:46:29 -07002395 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002396 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002397 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002398 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002399
Dave Wallace7876d392017-10-19 03:53:57 -04002400 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002401 {
Florin Coras134a9962018-08-28 11:32:04 -07002402 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002403 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002404 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2405 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002406 }
Dave Wallace7876d392017-10-19 03:53:57 -04002407 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002408 {
Florin Coras134a9962018-08-28 11:32:04 -07002409 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002410 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002411 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2412 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002413 }
Dave Wallace7876d392017-10-19 03:53:57 -04002414 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002415 {
Florin Coras134a9962018-08-28 11:32:04 -07002416 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002417 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002418 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2419 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002420 }
2421
Florin Coras54693d22018-07-17 10:46:29 -07002422 if (!n_bits)
2423 return 0;
2424
2425 if (!write_map)
2426 goto check_rd;
2427
Florin Coras096a1d52021-01-27 15:33:51 -08002428 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2429 {
2430 if (!(s = vcl_session_get (wrk, sid)))
2431 {
2432 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2433 bits_set++;
2434 continue;
2435 }
Florin Coras54693d22018-07-17 10:46:29 -07002436
Florin Coras096a1d52021-01-27 15:33:51 -08002437 if (vcl_session_write_ready (s))
2438 {
2439 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2440 bits_set++;
2441 }
2442 else
2443 {
2444 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2445 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2446 }
2447 }
Florin Coras54693d22018-07-17 10:46:29 -07002448
2449check_rd:
2450 if (!read_map)
2451 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002452
Florin Coras096a1d52021-01-27 15:33:51 -08002453 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2454 {
2455 if (!(s = vcl_session_get (wrk, sid)))
2456 {
2457 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2458 bits_set++;
2459 continue;
2460 }
Florin Coras54693d22018-07-17 10:46:29 -07002461
Florin Coras096a1d52021-01-27 15:33:51 -08002462 if (vcl_session_read_ready (s))
2463 {
2464 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2465 bits_set++;
2466 }
2467 }
Florin Coras54693d22018-07-17 10:46:29 -07002468
2469check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002470
Florin Coras86f04502018-09-12 16:08:01 -07002471 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2472 {
2473 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2474 read_map, write_map, except_map, &bits_set);
2475 }
2476 vec_reset_length (wrk->unhandled_evts_vector);
2477
Florin Coras99368312018-08-02 10:45:44 -07002478 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002479 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002480 time_to_wait, &bits_set);
2481 else
Florin Coras134a9962018-08-28 11:32:04 -07002482 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002483 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002484
Dave Wallace543852a2017-08-03 02:11:34 -04002485 return (bits_set);
2486}
2487
Dave Wallacef7f809c2017-10-03 01:48:42 -04002488static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002489vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002490{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002491 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002492 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002493 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002494
Florin Corasc0737e92019-03-04 14:19:39 -08002495 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002496 return;
2497
Florin Coras6c3b2182020-10-19 18:36:48 -07002498 s = vcl_session_get_w_handle (wrk, vep_handle);
2499 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002500 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002501 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002502 goto done;
2503 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002504 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002505 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002506 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002507 goto done;
2508 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002509 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002510 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002511 "{\n"
2512 " is_vep = %u\n"
2513 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002514 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002515 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2516 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002517
Florin Coras7e5e62b2019-07-30 14:08:23 -07002518 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002519 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002520 s = vcl_session_get_w_handle (wrk, sh);
2521 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002522 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002523 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002524 goto done;
2525 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002526 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002527 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002528 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002529 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002530 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002531 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002532 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002533 goto done;
2534 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002535 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002536 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2537 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002538 sh, s->vep.vep_sh, vep_handle);
2539 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002540 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002541 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002542 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002543 " next_sh = 0x%x (%u)\n"
2544 " prev_sh = 0x%x (%u)\n"
2545 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002546 " ev.events = 0x%x\n"
2547 " ev.data.u64 = 0x%llx\n"
2548 " et_mask = 0x%x\n"
2549 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002550 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002551 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2552 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002553 }
2554 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002555
2556done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002557 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002558}
2559
2560int
2561vppcom_epoll_create (void)
2562{
Florin Coras134a9962018-08-28 11:32:04 -07002563 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002564 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002565
Florin Coras134a9962018-08-28 11:32:04 -07002566 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002567
Florin Coras6c3b2182020-10-19 18:36:48 -07002568 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002569 vep_session->vep.vep_sh = ~0;
2570 vep_session->vep.next_sh = ~0;
2571 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002572 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002573
Florin Corasa7a1a222018-12-30 17:11:31 -08002574 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2575 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002576
Florin Corasab2f6db2018-08-31 14:31:41 -07002577 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002578}
2579
2580int
Florin Coras134a9962018-08-28 11:32:04 -07002581vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002582 struct epoll_event *event)
2583{
Florin Coras134a9962018-08-28 11:32:04 -07002584 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002585 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002586 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002587 vcl_session_t *s;
2588 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002589
Florin Coras134a9962018-08-28 11:32:04 -07002590 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002591 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002592 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002593 return VPPCOM_EINVAL;
2594 }
2595
Florin Coras134a9962018-08-28 11:32:04 -07002596 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002597 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002598 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002599 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002600 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002601 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002602 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002603 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002604 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002605 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002606 }
2607
Florin Coras134a9962018-08-28 11:32:04 -07002608 ASSERT (vep_session->vep.vep_sh == ~0);
2609 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002610
Florin Coras17ec5772020-08-12 20:46:29 -07002611 s = vcl_session_get_w_handle (wrk, session_handle);
2612 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002613 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002614 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002615 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002616 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002617 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002618 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002619 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002620 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002621 }
2622
2623 switch (op)
2624 {
2625 case EPOLL_CTL_ADD:
2626 if (PREDICT_FALSE (!event))
2627 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002628 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002629 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002630 }
Florin Coras134a9962018-08-28 11:32:04 -07002631 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002632 {
Florin Coras7e12d942018-06-27 14:32:43 -07002633 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002634 next_session = vcl_session_get_w_handle (wrk,
2635 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002636 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002637 {
Florin Coras5e062572019-03-14 19:07:51 -07002638 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002639 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002640 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002641 }
Florin Coras134a9962018-08-28 11:32:04 -07002642 ASSERT (next_session->vep.prev_sh == vep_handle);
2643 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002644 }
Florin Coras17ec5772020-08-12 20:46:29 -07002645 s->vep.next_sh = vep_session->vep.next_sh;
2646 s->vep.prev_sh = vep_handle;
2647 s->vep.vep_sh = vep_handle;
2648 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2649 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002650 s->flags &= ~VCL_SESSION_F_IS_VEP;
2651 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002652 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002653
Florin Coras17ec5772020-08-12 20:46:29 -07002654 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2655 if (txf && (event->events & EPOLLOUT))
2656 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002657
Florin Coras6e3c1f82020-01-15 01:30:46 +00002658 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002659 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002660 {
2661 session_event_t e = { 0 };
2662 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002663 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002664 vec_add1 (wrk->unhandled_evts_vector, e);
2665 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002666 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002667 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002668 {
2669 session_event_t e = { 0 };
2670 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002671 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002672 vec_add1 (wrk->unhandled_evts_vector, e);
2673 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002674 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2675 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002676 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002677 break;
2678
2679 case EPOLL_CTL_MOD:
2680 if (PREDICT_FALSE (!event))
2681 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002682 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002683 rv = VPPCOM_EINVAL;
2684 goto done;
2685 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002686 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002687 {
Florin Coras5e062572019-03-14 19:07:51 -07002688 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002689 rv = VPPCOM_EINVAL;
2690 goto done;
2691 }
Florin Coras17ec5772020-08-12 20:46:29 -07002692 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002693 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002694 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002695 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002696 rv = VPPCOM_EINVAL;
2697 goto done;
2698 }
hanlin475c9d72019-12-26 11:44:28 +08002699
2700 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2701 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002702 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002703 {
2704 session_event_t e = { 0 };
2705 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002706 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002707 vec_add1 (wrk->unhandled_evts_vector, e);
2708 }
Florin Coras17ec5772020-08-12 20:46:29 -07002709 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2710 s->vep.ev = *event;
2711 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2712 if (event->events & EPOLLOUT)
2713 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2714 else
2715 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasa7a1a222018-12-30 17:11:31 -08002716 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2717 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002718 break;
2719
2720 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002721 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002722 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002723 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002724 rv = VPPCOM_EINVAL;
2725 goto done;
2726 }
Florin Coras17ec5772020-08-12 20:46:29 -07002727 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002728 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002729 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002730 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002731 rv = VPPCOM_EINVAL;
2732 goto done;
2733 }
2734
Florin Coras17ec5772020-08-12 20:46:29 -07002735 if (s->vep.prev_sh == vep_handle)
2736 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002737 else
2738 {
Florin Coras7e12d942018-06-27 14:32:43 -07002739 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002740 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002741 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002742 {
Florin Coras5e062572019-03-14 19:07:51 -07002743 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002744 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002745 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002746 }
Florin Coras134a9962018-08-28 11:32:04 -07002747 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002748 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002749 }
Florin Coras17ec5772020-08-12 20:46:29 -07002750 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002751 {
Florin Coras7e12d942018-06-27 14:32:43 -07002752 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002753 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002754 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002755 {
Florin Coras5e062572019-03-14 19:07:51 -07002756 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002757 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002758 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002759 }
Florin Coras134a9962018-08-28 11:32:04 -07002760 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002761 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002762 }
2763
Florin Coras17ec5772020-08-12 20:46:29 -07002764 memset (&s->vep, 0, sizeof (s->vep));
2765 s->vep.next_sh = ~0;
2766 s->vep.prev_sh = ~0;
2767 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002768 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002769
Florin Coras17ec5772020-08-12 20:46:29 -07002770 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2771 if (txf)
2772 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002773
Florin Coras5e062572019-03-14 19:07:51 -07002774 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002775 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002776 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002777 break;
2778
2779 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002780 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002781 rv = VPPCOM_EINVAL;
2782 }
2783
Florin Coras134a9962018-08-28 11:32:04 -07002784 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002785
2786done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002787 return rv;
2788}
2789
Florin Coras86f04502018-09-12 16:08:01 -07002790static inline void
2791vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2792 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002793{
2794 session_disconnected_msg_t *disconnected_msg;
2795 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002796 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002797 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002798 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002799 u8 add_event = 0;
2800
2801 switch (e->event_type)
2802 {
Florin Coras653e43f2019-03-04 10:56:23 -08002803 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002804 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002805 s = vcl_session_get (wrk, sid);
2806 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002807 break;
Florin Corasb242d312020-10-26 15:35:40 -07002808 vcl_fifo_rx_evt_valid_or_break (s);
2809 session_events = s->vep.ev.events;
2810 if (!(EPOLLIN & s->vep.ev.events)
2811 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002812 break;
2813 add_event = 1;
2814 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002815 session_evt_data = s->vep.ev.data.u64;
2816 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002817 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002818 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002819 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002820 s = vcl_session_get (wrk, sid);
2821 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002822 break;
Florin Corasb242d312020-10-26 15:35:40 -07002823 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002824 if (!(EPOLLOUT & session_events))
2825 break;
2826 add_event = 1;
2827 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002828 session_evt_data = s->vep.ev.data.u64;
2829 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2830 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002831 break;
Florin Coras86f04502018-09-12 16:08:01 -07002832 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002833 if (!e->postponed)
2834 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2835 else
2836 s = vcl_session_get (wrk, e->session_index);
2837 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002838 break;
Florin Corasb242d312020-10-26 15:35:40 -07002839 session_events = s->vep.ev.events;
2840 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002841 if (!(EPOLLIN & session_events))
2842 break;
Florin Coras86f04502018-09-12 16:08:01 -07002843 add_event = 1;
2844 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002845 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002846 break;
2847 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002848 if (!e->postponed)
2849 {
2850 connected_msg = (session_connected_msg_t *) e->data;
2851 sid = vcl_session_connected_handler (wrk, connected_msg);
2852 }
2853 else
2854 sid = e->session_index;
2855 s = vcl_session_get (wrk, sid);
2856 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002857 break;
Florin Corasb242d312020-10-26 15:35:40 -07002858 session_events = s->vep.ev.events;
2859 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002860 if (!(EPOLLOUT & session_events))
2861 break;
2862 add_event = 1;
2863 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002864 session_evt_data = s->vep.ev.data.u64;
2865 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002866 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002867 break;
2868 case SESSION_CTRL_EVT_DISCONNECTED:
2869 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002870 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2871 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002872 break;
Florin Corasb242d312020-10-26 15:35:40 -07002873 sid = s->session_index;
2874 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002875 add_event = 1;
2876 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002877 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002878 break;
2879 case SESSION_CTRL_EVT_RESET:
2880 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002881 s = vcl_session_get (wrk, sid);
2882 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002883 break;
Florin Corasb242d312020-10-26 15:35:40 -07002884 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002885 add_event = 1;
2886 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002887 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002888 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002889 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2890 vcl_session_unlisten_reply_handler (wrk, e->data);
2891 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002892 case SESSION_CTRL_EVT_MIGRATED:
2893 vcl_session_migrated_handler (wrk, e->data);
2894 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002895 case SESSION_CTRL_EVT_CLEANUP:
2896 vcl_session_cleanup_handler (wrk, e->data);
2897 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002898 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2899 vcl_session_req_worker_update_handler (wrk, e->data);
2900 break;
2901 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2902 vcl_session_worker_update_reply_handler (wrk, e->data);
2903 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002904 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2905 vcl_session_app_add_segment_handler (wrk, e->data);
2906 break;
2907 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2908 vcl_session_app_del_segment_handler (wrk, e->data);
2909 break;
hanlina3a48962020-07-13 11:09:15 +08002910 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002911 vcl_worker_rpc_handler (wrk, e->data);
2912 break;
Florin Coras86f04502018-09-12 16:08:01 -07002913 default:
2914 VDBG (0, "unhandled: %u", e->event_type);
2915 break;
2916 }
2917
2918 if (add_event)
2919 {
2920 events[*num_ev].data.u64 = session_evt_data;
2921 if (EPOLLONESHOT & session_events)
2922 {
Florin Corasb242d312020-10-26 15:35:40 -07002923 s = vcl_session_get (wrk, sid);
2924 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002925 }
2926 *num_ev += 1;
2927 }
2928}
2929
2930static int
2931vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2932 struct epoll_event *events, u32 maxevents,
2933 double wait_for_time, u32 * num_ev)
2934{
Florin Coras99368312018-08-02 10:45:44 -07002935 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002936 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002937 int i;
2938
Florin Coras539663c2018-09-28 14:59:37 -07002939 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2940 goto handle_dequeued;
2941
Florin Coras54693d22018-07-17 10:46:29 -07002942 svm_msg_q_lock (mq);
2943 if (svm_msg_q_is_empty (mq))
2944 {
2945 if (!wait_for_time)
2946 {
2947 svm_msg_q_unlock (mq);
2948 return 0;
2949 }
2950 else if (wait_for_time < 0)
2951 {
2952 svm_msg_q_wait (mq);
2953 }
2954 else
2955 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002956 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002957 {
2958 svm_msg_q_unlock (mq);
2959 return 0;
2960 }
2961 }
2962 }
Florin Corase003a1b2019-06-05 10:47:16 -07002963 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08002964 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002965 svm_msg_q_unlock (mq);
2966
Florin Coras539663c2018-09-28 14:59:37 -07002967handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002968 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002969 {
Florin Coras134a9962018-08-28 11:32:04 -07002970 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002971 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08002972 if (*num_ev < maxevents)
2973 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2974 else
2975 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07002976 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002977 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002978 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002979 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002980 return *num_ev;
2981}
2982
Florin Coras99368312018-08-02 10:45:44 -07002983static int
Florin Coras134a9962018-08-28 11:32:04 -07002984vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002985 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002986{
Florin Corase003a1b2019-06-05 10:47:16 -07002987 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002988
2989 if (!n_evts)
2990 {
2991 wait = wait_for_time;
2992 start = clib_time_now (&wrk->clib_time);
2993 }
2994
2995 do
2996 {
2997 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2998 wait, &n_evts);
2999 if (n_evts)
3000 return n_evts;
3001 if (wait == -1)
3002 continue;
3003
Florin Corase003a1b2019-06-05 10:47:16 -07003004 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07003005 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07003006 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003007 }
3008 while (wait > 0);
3009
3010 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003011}
3012
3013static int
Florin Coras134a9962018-08-28 11:32:04 -07003014vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003015 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003016{
Florin Corasb13ba132021-01-27 16:05:24 -08003017 double wait = 0, start = 0, now;
Florin Coras99368312018-08-02 10:45:44 -07003018 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003019 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003020 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07003021 u64 buf;
3022
Florin Coras134a9962018-08-28 11:32:04 -07003023 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003024 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003025 {
Florin Corasb13ba132021-01-27 16:05:24 -08003026 wait = wait_for_time;
3027 start = clib_time_now (&wrk->clib_time);
Florin Coras99368312018-08-02 10:45:44 -07003028 }
3029
Florin Corasb13ba132021-01-27 16:05:24 -08003030 do
3031 {
3032 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
3033 vec_len (wrk->mq_events), wait);
3034 if (n_mq_evts < 0)
3035 {
3036 VDBG (0, "epoll_wait error %u", errno);
3037 return n_evts;
3038 }
3039
3040 for (i = 0; i < n_mq_evts; i++)
3041 {
3042 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3043 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3044 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3045 &n_evts);
3046 }
3047
3048 if (n_evts)
3049 return n_evts;
3050 if (wait == -1)
3051 continue;
3052
3053 now = clib_time_now (&wrk->clib_time);
3054 wait -= (now - start) * 1e3;
3055 start = now;
3056 }
3057 while (wait > 0);
3058
3059 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003060}
3061
Dave Wallacef7f809c2017-10-03 01:48:42 -04003062int
Florin Coras134a9962018-08-28 11:32:04 -07003063vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003064 int maxevents, double wait_for_time)
3065{
Florin Coras134a9962018-08-28 11:32:04 -07003066 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003067 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003068 u32 n_evts = 0;
3069 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003070
3071 if (PREDICT_FALSE (maxevents <= 0))
3072 {
Florin Coras5e062572019-03-14 19:07:51 -07003073 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003074 return VPPCOM_EINVAL;
3075 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003076
Florin Coras134a9962018-08-28 11:32:04 -07003077 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003078 if (!vep_session)
3079 return VPPCOM_EBADFD;
3080
Florin Coras6c3b2182020-10-19 18:36:48 -07003081 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003082 {
Florin Coras5e062572019-03-14 19:07:51 -07003083 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003084 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003085 }
Florin Coras54693d22018-07-17 10:46:29 -07003086
3087 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003088
Florin Coras86f04502018-09-12 16:08:01 -07003089 if (vec_len (wrk->unhandled_evts_vector))
3090 {
3091 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3092 {
3093 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3094 events, &n_evts);
3095 if (n_evts == maxevents)
3096 {
Florin Corase003a1b2019-06-05 10:47:16 -07003097 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3098 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003099 }
3100 }
Florin Corase003a1b2019-06-05 10:47:16 -07003101 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003102 }
3103
3104 if (vcm->cfg.use_mq_eventfd)
3105 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3106 wait_for_time);
3107
3108 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3109 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003110}
3111
Dave Wallace35830af2017-10-09 01:43:42 -04003112int
Florin Coras134a9962018-08-28 11:32:04 -07003113vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003114 void *buffer, uint32_t * buflen)
3115{
Florin Coras134a9962018-08-28 11:32:04 -07003116 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08003117 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003118 vppcom_endpt_t *ep = buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003119 vcl_session_t *session;
3120 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003121
Florin Coras134a9962018-08-28 11:32:04 -07003122 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003123 if (!session)
3124 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003125
Dave Wallace35830af2017-10-09 01:43:42 -04003126 switch (op)
3127 {
3128 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003129 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003130 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3131 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003132 break;
3133
Dave Wallace227867f2017-11-13 21:21:53 -05003134 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003135 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003136 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3137 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003138 break;
3139
3140 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003141 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003142 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003143 *flags =
3144 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003145 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003146 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003147 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003148 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3149 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003150 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003151 }
3152 else
3153 rv = VPPCOM_EINVAL;
3154 break;
3155
3156 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003157 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003158 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003159 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003160 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003161 else
Florin Corasac422d62020-10-19 20:51:36 -07003162 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003163
Florin Coras5e062572019-03-14 19:07:51 -07003164 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3165 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003166 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003167 }
3168 else
3169 rv = VPPCOM_EINVAL;
3170 break;
3171
3172 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003173 if (PREDICT_TRUE (buffer && buflen &&
3174 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003175 {
Florin Coras7e12d942018-06-27 14:32:43 -07003176 ep->is_ip4 = session->transport.is_ip4;
3177 ep->port = session->transport.rmt_port;
3178 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003179 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3180 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003181 else
Dave Barach178cf492018-11-13 16:34:13 -05003182 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3183 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003184 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003185 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3186 "addr = %U, port %u", session_handle, ep->is_ip4,
3187 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003188 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3189 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003190 }
3191 else
3192 rv = VPPCOM_EINVAL;
3193 break;
3194
3195 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003196 if (PREDICT_TRUE (buffer && buflen &&
3197 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003198 {
Florin Coras7e12d942018-06-27 14:32:43 -07003199 ep->is_ip4 = session->transport.is_ip4;
3200 ep->port = session->transport.lcl_port;
3201 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003202 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3203 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003204 else
Dave Barach178cf492018-11-13 16:34:13 -05003205 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3206 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003207 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003208 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3209 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003210 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003211 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3212 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003213 }
3214 else
3215 rv = VPPCOM_EINVAL;
3216 break;
Stevenb5a11602017-10-11 09:59:30 -07003217
Florin Corasef7cbf62019-10-17 09:56:27 -07003218 case VPPCOM_ATTR_SET_LCL_ADDR:
3219 if (PREDICT_TRUE (buffer && buflen &&
3220 (*buflen >= sizeof (*ep)) && ep->ip))
3221 {
3222 session->transport.is_ip4 = ep->is_ip4;
3223 session->transport.lcl_port = ep->port;
3224 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3225 *buflen = sizeof (*ep);
3226 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3227 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3228 &session->transport.lcl_ip,
3229 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3230 clib_net_to_host_u16 (ep->port));
3231 }
3232 else
3233 rv = VPPCOM_EINVAL;
3234 break;
3235
Dave Wallace048b1d62018-01-03 22:24:41 -05003236 case VPPCOM_ATTR_GET_LIBC_EPFD:
3237 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003238 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003239 break;
3240
3241 case VPPCOM_ATTR_SET_LIBC_EPFD:
3242 if (PREDICT_TRUE (buffer && buflen &&
3243 (*buflen == sizeof (session->libc_epfd))))
3244 {
3245 session->libc_epfd = *(int *) buffer;
3246 *buflen = sizeof (session->libc_epfd);
3247
Florin Coras5e062572019-03-14 19:07:51 -07003248 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3249 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003250 }
3251 else
3252 rv = VPPCOM_EINVAL;
3253 break;
3254
3255 case VPPCOM_ATTR_GET_PROTOCOL:
3256 if (buffer && buflen && (*buflen >= sizeof (int)))
3257 {
Florin Coras7e12d942018-06-27 14:32:43 -07003258 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003259 *buflen = sizeof (int);
3260
Florin Coras5e062572019-03-14 19:07:51 -07003261 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3262 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003263 }
3264 else
3265 rv = VPPCOM_EINVAL;
3266 break;
3267
3268 case VPPCOM_ATTR_GET_LISTEN:
3269 if (buffer && buflen && (*buflen >= sizeof (int)))
3270 {
Florin Corasac422d62020-10-19 20:51:36 -07003271 *(int *) buffer = vcl_session_has_attr (session,
3272 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003273 *buflen = sizeof (int);
3274
Florin Coras5e062572019-03-14 19:07:51 -07003275 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3276 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003277 }
3278 else
3279 rv = VPPCOM_EINVAL;
3280 break;
3281
3282 case VPPCOM_ATTR_GET_ERROR:
3283 if (buffer && buflen && (*buflen >= sizeof (int)))
3284 {
3285 *(int *) buffer = 0;
3286 *buflen = sizeof (int);
3287
Florin Coras5e062572019-03-14 19:07:51 -07003288 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3289 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003290 }
3291 else
3292 rv = VPPCOM_EINVAL;
3293 break;
3294
3295 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3296 if (buffer && buflen && (*buflen >= sizeof (u32)))
3297 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003298
3299 /* VPP-TBD */
3300 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003301 session->tx_fifo ?
3302 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003303 vcm->cfg.tx_fifo_size);
3304 *buflen = sizeof (u32);
3305
Florin Coras5e062572019-03-14 19:07:51 -07003306 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3307 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3308 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003309 }
3310 else
3311 rv = VPPCOM_EINVAL;
3312 break;
3313
3314 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3315 if (buffer && buflen && (*buflen == sizeof (u32)))
3316 {
3317 /* VPP-TBD */
3318 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003319 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3320 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3321 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003322 }
3323 else
3324 rv = VPPCOM_EINVAL;
3325 break;
3326
3327 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3328 if (buffer && buflen && (*buflen >= sizeof (u32)))
3329 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003330
3331 /* VPP-TBD */
3332 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003333 session->rx_fifo ?
3334 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003335 vcm->cfg.rx_fifo_size);
3336 *buflen = sizeof (u32);
3337
Florin Coras5e062572019-03-14 19:07:51 -07003338 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3339 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003340 }
3341 else
3342 rv = VPPCOM_EINVAL;
3343 break;
3344
3345 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3346 if (buffer && buflen && (*buflen == sizeof (u32)))
3347 {
3348 /* VPP-TBD */
3349 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003350 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3351 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3352 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003353 }
3354 else
3355 rv = VPPCOM_EINVAL;
3356 break;
3357
3358 case VPPCOM_ATTR_GET_REUSEADDR:
3359 if (buffer && buflen && (*buflen >= sizeof (int)))
3360 {
3361 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003362 *(int *) buffer = vcl_session_has_attr (session,
3363 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003364 *buflen = sizeof (int);
3365
Florin Coras5e062572019-03-14 19:07:51 -07003366 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3367 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003368 }
3369 else
3370 rv = VPPCOM_EINVAL;
3371 break;
3372
Stevenb5a11602017-10-11 09:59:30 -07003373 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003374 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003375 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003376 {
3377 /* VPP-TBD */
3378 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003379 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003380 else
Florin Corasac422d62020-10-19 20:51:36 -07003381 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003382
Florin Coras5e062572019-03-14 19:07:51 -07003383 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003384 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003385 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003386 }
3387 else
3388 rv = VPPCOM_EINVAL;
3389 break;
3390
3391 case VPPCOM_ATTR_GET_REUSEPORT:
3392 if (buffer && buflen && (*buflen >= sizeof (int)))
3393 {
3394 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003395 *(int *) buffer = vcl_session_has_attr (session,
3396 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003397 *buflen = sizeof (int);
3398
Florin Coras5e062572019-03-14 19:07:51 -07003399 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3400 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003401 }
3402 else
3403 rv = VPPCOM_EINVAL;
3404 break;
3405
3406 case VPPCOM_ATTR_SET_REUSEPORT:
3407 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003408 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003409 {
3410 /* VPP-TBD */
3411 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003412 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003413 else
Florin Corasac422d62020-10-19 20:51:36 -07003414 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003415
Florin Coras5e062572019-03-14 19:07:51 -07003416 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003417 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003418 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003419 }
3420 else
3421 rv = VPPCOM_EINVAL;
3422 break;
3423
3424 case VPPCOM_ATTR_GET_BROADCAST:
3425 if (buffer && buflen && (*buflen >= sizeof (int)))
3426 {
3427 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003428 *(int *) buffer = vcl_session_has_attr (session,
3429 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003430 *buflen = sizeof (int);
3431
Florin Coras5e062572019-03-14 19:07:51 -07003432 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3433 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003434 }
3435 else
3436 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003437 break;
3438
3439 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003440 if (buffer && buflen && (*buflen == sizeof (int)))
3441 {
3442 /* VPP-TBD */
3443 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003444 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003445 else
Florin Corasac422d62020-10-19 20:51:36 -07003446 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003447
Florin Coras5e062572019-03-14 19:07:51 -07003448 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003449 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003450 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003451 }
3452 else
3453 rv = VPPCOM_EINVAL;
3454 break;
3455
3456 case VPPCOM_ATTR_GET_V6ONLY:
3457 if (buffer && buflen && (*buflen >= sizeof (int)))
3458 {
3459 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003460 *(int *) buffer = vcl_session_has_attr (session,
3461 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003462 *buflen = sizeof (int);
3463
Florin Coras5e062572019-03-14 19:07:51 -07003464 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3465 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003466 }
3467 else
3468 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003469 break;
3470
3471 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003472 if (buffer && buflen && (*buflen == sizeof (int)))
3473 {
3474 /* VPP-TBD */
3475 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003476 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003477 else
Florin Corasac422d62020-10-19 20:51:36 -07003478 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003479
Florin Coras5e062572019-03-14 19:07:51 -07003480 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003481 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003482 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003483 }
3484 else
3485 rv = VPPCOM_EINVAL;
3486 break;
3487
3488 case VPPCOM_ATTR_GET_KEEPALIVE:
3489 if (buffer && buflen && (*buflen >= sizeof (int)))
3490 {
3491 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003492 *(int *) buffer = vcl_session_has_attr (session,
3493 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003494 *buflen = sizeof (int);
3495
Florin Coras5e062572019-03-14 19:07:51 -07003496 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3497 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003498 }
3499 else
3500 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003501 break;
Stevenbccd3392017-10-12 20:42:21 -07003502
3503 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003504 if (buffer && buflen && (*buflen == sizeof (int)))
3505 {
3506 /* VPP-TBD */
3507 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003508 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003509 else
Florin Corasac422d62020-10-19 20:51:36 -07003510 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003511
Florin Coras5e062572019-03-14 19:07:51 -07003512 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003513 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003514 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003515 }
3516 else
3517 rv = VPPCOM_EINVAL;
3518 break;
3519
3520 case VPPCOM_ATTR_GET_TCP_NODELAY:
3521 if (buffer && buflen && (*buflen >= sizeof (int)))
3522 {
3523 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003524 *(int *) buffer = vcl_session_has_attr (session,
3525 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003526 *buflen = sizeof (int);
3527
Florin Coras5e062572019-03-14 19:07:51 -07003528 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3529 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003530 }
3531 else
3532 rv = VPPCOM_EINVAL;
3533 break;
3534
3535 case VPPCOM_ATTR_SET_TCP_NODELAY:
3536 if (buffer && buflen && (*buflen == sizeof (int)))
3537 {
3538 /* VPP-TBD */
3539 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003540 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003541 else
Florin Corasac422d62020-10-19 20:51:36 -07003542 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003543
Florin Coras5e062572019-03-14 19:07:51 -07003544 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003545 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003546 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003547 }
3548 else
3549 rv = VPPCOM_EINVAL;
3550 break;
3551
3552 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3553 if (buffer && buflen && (*buflen >= sizeof (int)))
3554 {
3555 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003556 *(int *) buffer = vcl_session_has_attr (session,
3557 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003558 *buflen = sizeof (int);
3559
Florin Coras5e062572019-03-14 19:07:51 -07003560 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3561 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003562 }
3563 else
3564 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003565 break;
3566
3567 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003568 if (buffer && buflen && (*buflen == sizeof (int)))
3569 {
3570 /* VPP-TBD */
3571 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003572 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003573 else
Florin Corasac422d62020-10-19 20:51:36 -07003574 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003575
Florin Coras5e062572019-03-14 19:07:51 -07003576 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003577 vcl_session_has_attr (session,
3578 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003579 }
3580 else
3581 rv = VPPCOM_EINVAL;
3582 break;
3583
3584 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3585 if (buffer && buflen && (*buflen >= sizeof (int)))
3586 {
3587 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003588 *(int *) buffer = vcl_session_has_attr (session,
3589 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003590 *buflen = sizeof (int);
3591
Florin Coras5e062572019-03-14 19:07:51 -07003592 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3593 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003594 }
3595 else
3596 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003597 break;
3598
3599 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003600 if (buffer && buflen && (*buflen == sizeof (int)))
3601 {
3602 /* VPP-TBD */
3603 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003604 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003605 else
Florin Corasac422d62020-10-19 20:51:36 -07003606 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003607
Florin Coras5e062572019-03-14 19:07:51 -07003608 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003609 vcl_session_has_attr (session,
3610 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003611 }
3612 else
3613 rv = VPPCOM_EINVAL;
3614 break;
3615
3616 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3617 if (buffer && buflen && (*buflen >= sizeof (u32)))
3618 {
3619 /* VPP-TBD */
3620 *(u32 *) buffer = session->user_mss;
3621 *buflen = sizeof (int);
3622
Florin Coras5e062572019-03-14 19:07:51 -07003623 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3624 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003625 }
3626 else
3627 rv = VPPCOM_EINVAL;
3628 break;
3629
3630 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3631 if (buffer && buflen && (*buflen == sizeof (u32)))
3632 {
3633 /* VPP-TBD */
3634 session->user_mss = *(u32 *) buffer;
3635
Florin Coras5e062572019-03-14 19:07:51 -07003636 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3637 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003638 }
3639 else
3640 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003641 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003642
Florin Coras7baeb712019-01-04 17:05:43 -08003643 case VPPCOM_ATTR_SET_SHUT:
3644 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003645 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003646 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003647 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003648 break;
3649
3650 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003651 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003652 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003653 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003654 tmp_flags |= 2;
3655 if (tmp_flags == 1)
3656 *(int *) buffer = SHUT_RD;
3657 else if (tmp_flags == 2)
3658 *(int *) buffer = SHUT_WR;
3659 else if (tmp_flags == 3)
3660 *(int *) buffer = SHUT_RDWR;
3661 *buflen = sizeof (int);
3662 break;
Florin Coras1e966172020-05-16 18:18:14 +00003663
3664 case VPPCOM_ATTR_SET_CONNECTED:
3665 session->flags |= VCL_SESSION_F_CONNECTED;
3666 break;
3667
Florin Corasa5a9efd2021-01-05 17:03:29 -08003668 case VPPCOM_ATTR_SET_CKPAIR:
3669 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3670 !vcl_session_has_crypto (session))
3671 {
3672 rv = VPPCOM_EINVAL;
3673 break;
3674 }
3675 session->ckpair_index = *(uint32_t *) buffer;
3676 break;
3677
Florin Coras6a6555a2021-01-28 11:39:27 -08003678 case VPPCOM_ATTR_SET_VRF:
3679 if (!(buffer && buflen && (*buflen == sizeof (u32))))
3680 {
3681 rv = VPPCOM_EINVAL;
3682 break;
3683 }
3684 session->vrf = *(u32 *) buffer;
3685 break;
3686
3687 case VPPCOM_ATTR_GET_VRF:
3688 if (!(buffer && buflen && (*buflen >= sizeof (u32))))
3689 {
3690 rv = VPPCOM_EINVAL;
3691 break;
3692 }
3693 *(u32 *) buffer = session->vrf;
3694 *buflen = sizeof (u32);
3695 break;
3696
Dave Wallacee22aa742017-10-20 12:30:38 -04003697 default:
3698 rv = VPPCOM_EINVAL;
3699 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003700 }
3701
Dave Wallace35830af2017-10-09 01:43:42 -04003702 return rv;
3703}
3704
Stevenac1f96d2017-10-24 16:03:58 -07003705int
Florin Coras134a9962018-08-28 11:32:04 -07003706vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003707 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3708{
Florin Coras134a9962018-08-28 11:32:04 -07003709 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003710 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003711 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003712
3713 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003714 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003715 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003716 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003717 else
3718 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003719 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003720 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003721 }
3722
Florin Coras0a1e1832020-03-29 18:54:04 +00003723 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003724 {
Florin Coras5da10c42020-04-01 04:31:21 +00003725 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003726 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003727 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3728 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003729 else
Dave Barach178cf492018-11-13 16:34:13 -05003730 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3731 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003732 ep->is_ip4 = session->transport.is_ip4;
3733 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003734 }
Florin Coras460dce62018-07-27 05:45:06 -07003735
Stevenac1f96d2017-10-24 16:03:58 -07003736 return rv;
3737}
3738
3739int
Florin Coras134a9962018-08-28 11:32:04 -07003740vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003741 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3742{
Florin Coras7a2abce2020-04-05 19:25:44 +00003743 vcl_worker_t *wrk = vcl_worker_get_current ();
3744 vcl_session_t *s;
3745
3746 s = vcl_session_get_w_handle (wrk, session_handle);
3747 if (!s)
3748 return VPPCOM_EBADFD;
3749
Dave Wallace617dffa2017-10-26 14:47:06 -04003750 if (!buffer)
3751 return VPPCOM_EINVAL;
3752
3753 if (ep)
3754 {
Florin Coras5824cc52020-10-20 18:44:41 -07003755 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003756 return VPPCOM_EINVAL;
3757
3758 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003759 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003760 {
Florin Coras5824cc52020-10-20 18:44:41 -07003761 u32 session_index = s->session_index;
3762 f64 timeout = vcm->cfg.session_timeout;
3763 int rv;
3764
Florin Coras5da10c42020-04-01 04:31:21 +00003765 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003766 rv = vppcom_wait_for_session_state_change (session_index,
3767 VCL_STATE_READY,
3768 timeout);
3769 if (rv < 0)
3770 return rv;
3771 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003772 }
Florin Coras5824cc52020-10-20 18:44:41 -07003773
3774 s->transport.is_ip4 = ep->is_ip4;
3775 s->transport.rmt_port = ep->port;
3776 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003777 }
3778
3779 if (flags)
3780 {
3781 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003782 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003783 }
3784
Florin Coras7a2abce2020-04-05 19:25:44 +00003785 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3786 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003787}
3788
Dave Wallace048b1d62018-01-03 22:24:41 -05003789int
3790vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3791{
Florin Coras134a9962018-08-28 11:32:04 -07003792 vcl_worker_t *wrk = vcl_worker_get_current ();
3793 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003794 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003795 svm_msg_q_msg_t msg;
3796 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003797 int rv, num_ev = 0;
3798
Florin Coras5e062572019-03-14 19:07:51 -07003799 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003800
3801 if (!vp)
3802 return VPPCOM_EFAULT;
3803
3804 do
3805 {
Florin Coras7e12d942018-06-27 14:32:43 -07003806 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003807
Florin Coras6917b942018-11-13 22:44:54 -08003808 /* Dequeue all events and drop all unhandled io events */
3809 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3810 {
3811 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3812 vcl_handle_mq_event (wrk, e);
3813 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3814 }
3815 vec_reset_length (wrk->unhandled_evts_vector);
3816
Dave Wallace048b1d62018-01-03 22:24:41 -05003817 for (i = 0; i < n_sids; i++)
3818 {
Florin Coras7baeb712019-01-04 17:05:43 -08003819 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003820 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003821 {
3822 vp[i].revents = POLLHUP;
3823 num_ev++;
3824 continue;
3825 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003826
Florin Coras6917b942018-11-13 22:44:54 -08003827 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003828
3829 if (POLLIN & vp[i].events)
3830 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003831 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003832 if (rv > 0)
3833 {
Florin Coras6917b942018-11-13 22:44:54 -08003834 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003835 num_ev++;
3836 }
3837 else if (rv < 0)
3838 {
3839 switch (rv)
3840 {
3841 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003842 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003843 break;
3844
3845 default:
Florin Coras6917b942018-11-13 22:44:54 -08003846 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003847 break;
3848 }
3849 num_ev++;
3850 }
3851 }
3852
3853 if (POLLOUT & vp[i].events)
3854 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003855 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003856 if (rv > 0)
3857 {
Florin Coras6917b942018-11-13 22:44:54 -08003858 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003859 num_ev++;
3860 }
3861 else if (rv < 0)
3862 {
3863 switch (rv)
3864 {
3865 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003866 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003867 break;
3868
3869 default:
Florin Coras6917b942018-11-13 22:44:54 -08003870 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003871 break;
3872 }
3873 num_ev++;
3874 }
3875 }
3876
Dave Wallace7e607a72018-06-18 18:41:32 -04003877 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003878 {
Florin Coras6917b942018-11-13 22:44:54 -08003879 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003880 num_ev++;
3881 }
3882 }
3883 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003884 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003885 }
3886 while ((num_ev == 0) && keep_trying);
3887
Dave Wallace048b1d62018-01-03 22:24:41 -05003888 return num_ev;
3889}
3890
Florin Coras99368312018-08-02 10:45:44 -07003891int
3892vppcom_mq_epoll_fd (void)
3893{
Florin Coras134a9962018-08-28 11:32:04 -07003894 vcl_worker_t *wrk = vcl_worker_get_current ();
3895 return wrk->mqs_epfd;
3896}
3897
3898int
Florin Coras30e79c22019-01-02 19:31:22 -08003899vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003900{
3901 return session_handle & 0xFFFFFF;
3902}
3903
3904int
Florin Coras30e79c22019-01-02 19:31:22 -08003905vppcom_session_worker (vcl_session_handle_t session_handle)
3906{
3907 return session_handle >> 24;
3908}
3909
3910int
Florin Coras134a9962018-08-28 11:32:04 -07003911vppcom_worker_register (void)
3912{
Florin Coras47c40e22018-11-26 17:01:36 -08003913 if (!vcl_worker_alloc_and_init ())
3914 return VPPCOM_EEXIST;
3915
Florin Coras47c40e22018-11-26 17:01:36 -08003916 if (vcl_worker_register_with_vpp ())
3917 return VPPCOM_EEXIST;
3918
3919 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003920}
3921
Florin Coras369db832019-07-08 12:34:45 -07003922void
3923vppcom_worker_unregister (void)
3924{
3925 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3926 vcl_set_worker_index (~0);
3927}
3928
Pivo6017ff02020-05-04 17:57:33 +02003929void
3930vppcom_worker_index_set (int index)
3931{
3932 vcl_set_worker_index (index);
3933}
3934
Florin Corasdfe4cf42018-11-28 22:13:45 -08003935int
3936vppcom_worker_index (void)
3937{
3938 return vcl_get_worker_index ();
3939}
3940
Florin Corase0982e52019-01-25 13:19:56 -08003941int
3942vppcom_worker_mqs_epfd (void)
3943{
3944 vcl_worker_t *wrk = vcl_worker_get_current ();
3945 if (!vcm->cfg.use_mq_eventfd)
3946 return -1;
3947 return wrk->mqs_epfd;
3948}
3949
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003950int
3951vppcom_session_is_connectable_listener (uint32_t session_handle)
3952{
3953 vcl_session_t *session;
3954 vcl_worker_t *wrk = vcl_worker_get_current ();
3955 session = vcl_session_get_w_handle (wrk, session_handle);
3956 if (!session)
3957 return VPPCOM_EBADFD;
3958 return vcl_session_is_connectable_listener (wrk, session);
3959}
3960
3961int
3962vppcom_session_listener (uint32_t session_handle)
3963{
3964 vcl_worker_t *wrk = vcl_worker_get_current ();
3965 vcl_session_t *listen_session, *session;
3966 session = vcl_session_get_w_handle (wrk, session_handle);
3967 if (!session)
3968 return VPPCOM_EBADFD;
3969 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3970 return VPPCOM_EBADFD;
3971 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3972 if (!listen_session)
3973 return VPPCOM_EBADFD;
3974 return vcl_session_handle (listen_session);
3975}
3976
3977int
3978vppcom_session_n_accepted (uint32_t session_handle)
3979{
3980 vcl_worker_t *wrk = vcl_worker_get_current ();
3981 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3982 if (!session)
3983 return VPPCOM_EBADFD;
3984 return session->n_accepted_sessions;
3985}
3986
Florin Coras66ec4672020-06-15 07:59:40 -07003987const char *
3988vppcom_proto_str (vppcom_proto_t proto)
3989{
3990 char const *proto_str;
3991
3992 switch (proto)
3993 {
3994 case VPPCOM_PROTO_TCP:
3995 proto_str = "TCP";
3996 break;
3997 case VPPCOM_PROTO_UDP:
3998 proto_str = "UDP";
3999 break;
4000 case VPPCOM_PROTO_TLS:
4001 proto_str = "TLS";
4002 break;
4003 case VPPCOM_PROTO_QUIC:
4004 proto_str = "QUIC";
4005 break;
4006 default:
4007 proto_str = "UNKNOWN";
4008 break;
4009 }
4010 return proto_str;
4011}
4012
4013const char *
4014vppcom_retval_str (int retval)
4015{
4016 char const *st;
4017
4018 switch (retval)
4019 {
4020 case VPPCOM_OK:
4021 st = "VPPCOM_OK";
4022 break;
4023
4024 case VPPCOM_EAGAIN:
4025 st = "VPPCOM_EAGAIN";
4026 break;
4027
4028 case VPPCOM_EFAULT:
4029 st = "VPPCOM_EFAULT";
4030 break;
4031
4032 case VPPCOM_ENOMEM:
4033 st = "VPPCOM_ENOMEM";
4034 break;
4035
4036 case VPPCOM_EINVAL:
4037 st = "VPPCOM_EINVAL";
4038 break;
4039
4040 case VPPCOM_EBADFD:
4041 st = "VPPCOM_EBADFD";
4042 break;
4043
4044 case VPPCOM_EAFNOSUPPORT:
4045 st = "VPPCOM_EAFNOSUPPORT";
4046 break;
4047
4048 case VPPCOM_ECONNABORTED:
4049 st = "VPPCOM_ECONNABORTED";
4050 break;
4051
4052 case VPPCOM_ECONNRESET:
4053 st = "VPPCOM_ECONNRESET";
4054 break;
4055
4056 case VPPCOM_ENOTCONN:
4057 st = "VPPCOM_ENOTCONN";
4058 break;
4059
4060 case VPPCOM_ECONNREFUSED:
4061 st = "VPPCOM_ECONNREFUSED";
4062 break;
4063
4064 case VPPCOM_ETIMEDOUT:
4065 st = "VPPCOM_ETIMEDOUT";
4066 break;
4067
4068 default:
4069 st = "UNKNOWN_STATE";
4070 break;
4071 }
4072
4073 return st;
4074}
4075
Florin Corasa5a9efd2021-01-05 17:03:29 -08004076int
4077vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4078{
4079 if (vcm->cfg.vpp_app_socket_api)
4080 {
4081 clib_warning ("not supported");
4082 return VPPCOM_EINVAL;
4083 }
4084 return vcl_bapi_add_cert_key_pair (ckpair);
4085}
4086
4087int
4088vppcom_del_cert_key_pair (uint32_t ckpair_index)
4089{
4090 if (vcm->cfg.vpp_app_socket_api)
4091 {
4092 clib_warning ("not supported");
4093 return VPPCOM_EINVAL;
4094 }
4095 return vcl_bapi_del_cert_key_pair (ckpair_index);
4096}
4097
Dave Wallacee22aa742017-10-20 12:30:38 -04004098/*
4099 * fd.io coding-style-patch-verification: ON
4100 *
4101 * Local Variables:
4102 * eval: (c-set-style "gnu")
4103 * End:
4104 */