blob: 58c13c287636ec7e38750429f231d9f8201eca19 [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 Corasd85de682018-11-29 17:02:29 -080025static int
Florin Corasc4c4cf52019-08-24 18:17:34 -070026vcl_segment_is_not_mounted (vcl_worker_t * wrk, u64 segment_handle)
Florin Coras54693d22018-07-17 10:46:29 -070027{
Florin Corasc4c4cf52019-08-24 18:17:34 -070028 u32 segment_index;
Florin Coras54693d22018-07-17 10:46:29 -070029
Florin Corasd85de682018-11-29 17:02:29 -080030 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
Florin Coras5f45e012019-01-23 09:21:30 -080031 return 0;
Florin Coras54693d22018-07-17 10:46:29 -070032
Florin Corasc4c4cf52019-08-24 18:17:34 -070033 segment_index = vcl_segment_table_lookup (segment_handle);
34 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
35 return 0;
36
Florin Corasd85de682018-11-29 17:02:29 -080037 return 1;
Florin Coras54693d22018-07-17 10:46:29 -070038}
39
Florin Coras30e79c22019-01-02 19:31:22 -080040static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070041vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080042{
43 svm_msg_q_msg_t *msg;
44 u32 n_msgs;
45 int i;
46
Florin Corase003a1b2019-06-05 10:47:16 -070047 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
Florin Coras30e79c22019-01-02 19:31:22 -080048 for (i = 0; i < n_msgs; i++)
49 {
50 vec_add2 (wrk->mq_msg_vector, msg, 1);
51 svm_msg_q_sub_w_lock (mq, msg);
52 }
53 return n_msgs;
54}
55
Florin Coras697faea2018-06-27 17:10:49 -070056const char *
Florin Coras288eaab2019-02-03 15:26:14 -080057vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040058{
59 char *st;
60
61 switch (state)
62 {
Florin Corasc127d5a2020-10-14 16:35:58 -070063 case VCL_STATE_CLOSED:
Florin Coras54140622020-02-04 19:04:34 +000064 st = "STATE_CLOSED";
Dave Wallace543852a2017-08-03 02:11:34 -040065 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070066 case VCL_STATE_LISTEN:
Dave Wallace543852a2017-08-03 02:11:34 -040067 st = "STATE_LISTEN";
68 break;
Florin Corasdfffdd72020-10-15 10:54:47 -070069 case VCL_STATE_READY:
70 st = "STATE_READY";
Dave Wallace543852a2017-08-03 02:11:34 -040071 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070072 case VCL_STATE_VPP_CLOSING:
Florin Coras3c7d4f92018-12-14 11:28:43 -080073 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050074 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070075 case VCL_STATE_DISCONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040076 st = "STATE_DISCONNECT";
77 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070078 case VCL_STATE_DETACHED:
Florin Corasadcfb152020-02-12 08:50:29 +000079 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040080 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070081 case VCL_STATE_UPDATED:
Florin Coras2d675d72019-01-28 15:54:27 -080082 st = "STATE_UPDATED";
83 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070084 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -080085 st = "STATE_LISTEN_NO_MQ";
86 break;
Dave Wallace543852a2017-08-03 02:11:34 -040087 default:
88 st = "UNKNOWN_STATE";
89 break;
90 }
91
92 return st;
93}
94
Dave Wallace543852a2017-08-03 02:11:34 -040095u8 *
96format_ip4_address (u8 * s, va_list * args)
97{
98 u8 *a = va_arg (*args, u8 *);
99 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
100}
101
102u8 *
103format_ip6_address (u8 * s, va_list * args)
104{
105 ip6_address_t *a = va_arg (*args, ip6_address_t *);
106 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
107
108 i_max_n_zero = ARRAY_LEN (a->as_u16);
109 max_n_zeros = 0;
110 i_first_zero = i_max_n_zero;
111 n_zeros = 0;
112 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
113 {
114 u32 is_zero = a->as_u16[i] == 0;
115 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
116 {
117 i_first_zero = i;
118 n_zeros = 0;
119 }
120 n_zeros += is_zero;
121 if ((!is_zero && n_zeros > max_n_zeros)
122 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
123 {
124 i_max_n_zero = i_first_zero;
125 max_n_zeros = n_zeros;
126 i_first_zero = ARRAY_LEN (a->as_u16);
127 n_zeros = 0;
128 }
129 }
130
131 last_double_colon = 0;
132 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
133 {
134 if (i == i_max_n_zero && max_n_zeros > 1)
135 {
136 s = format (s, "::");
137 i += max_n_zeros - 1;
138 last_double_colon = 1;
139 }
140 else
141 {
142 s = format (s, "%s%x",
143 (last_double_colon || i == 0) ? "" : ":",
144 clib_net_to_host_u16 (a->as_u16[i]));
145 last_double_colon = 0;
146 }
147 }
148
149 return s;
150}
151
152/* Format an IP46 address. */
153u8 *
154format_ip46_address (u8 * s, va_list * args)
155{
156 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
157 ip46_type_t type = va_arg (*args, ip46_type_t);
158 int is_ip4 = 1;
159
160 switch (type)
161 {
162 case IP46_TYPE_ANY:
163 is_ip4 = ip46_address_is_ip4 (ip46);
164 break;
165 case IP46_TYPE_IP4:
166 is_ip4 = 1;
167 break;
168 case IP46_TYPE_IP6:
169 is_ip4 = 0;
170 break;
171 }
172
173 return is_ip4 ?
174 format (s, "%U", format_ip4_address, &ip46->ip4) :
175 format (s, "%U", format_ip6_address, &ip46->ip6);
176}
177
Florin Coras697faea2018-06-27 17:10:49 -0700178/*
179 * VPPCOM Utility Functions
180 */
181
Florin Coras458089b2019-08-21 16:20:44 -0700182static void
183vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
184{
185 app_session_evt_t _app_evt, *app_evt = &_app_evt;
186 session_listen_msg_t *mp;
187 svm_msg_q_t *mq;
188
189 mq = vcl_worker_ctrl_mq (wrk);
190 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
191 mp = (session_listen_msg_t *) app_evt->evt->data;
192 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700193 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700194 mp->context = s->session_index;
195 mp->wrk_index = wrk->vpp_wrk_index;
196 mp->is_ip4 = s->transport.is_ip4;
197 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
198 mp->port = s->transport.lcl_port;
199 mp->proto = s->session_type;
Florin Coras1e966172020-05-16 18:18:14 +0000200 if (s->flags & VCL_SESSION_F_CONNECTED)
201 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700202 app_send_ctrl_evt_to_vpp (mq, app_evt);
203}
204
205static void
206vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
207{
208 app_session_evt_t _app_evt, *app_evt = &_app_evt;
209 session_connect_msg_t *mp;
210 svm_msg_q_t *mq;
211
212 mq = vcl_worker_ctrl_mq (wrk);
213 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
214 mp = (session_connect_msg_t *) app_evt->evt->data;
215 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700216 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700217 mp->context = s->session_index;
218 mp->wrk_index = wrk->vpp_wrk_index;
219 mp->is_ip4 = s->transport.is_ip4;
220 mp->parent_handle = s->parent_handle;
221 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700222 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700223 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000224 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700225 mp->proto = s->session_type;
Florin Coras0a1e1832020-03-29 18:54:04 +0000226 if (s->flags & VCL_SESSION_F_CONNECTED)
227 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700228 app_send_ctrl_evt_to_vpp (mq, app_evt);
229}
230
231void
232vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
233{
234 app_session_evt_t _app_evt, *app_evt = &_app_evt;
235 session_unlisten_msg_t *mp;
236 svm_msg_q_t *mq;
237
238 mq = vcl_worker_ctrl_mq (wrk);
239 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
240 mp = (session_unlisten_msg_t *) app_evt->evt->data;
241 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700242 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700243 mp->wrk_index = wrk->vpp_wrk_index;
244 mp->handle = s->vpp_handle;
245 mp->context = wrk->wrk_index;
246 app_send_ctrl_evt_to_vpp (mq, app_evt);
247}
248
249static void
250vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
251{
252 app_session_evt_t _app_evt, *app_evt = &_app_evt;
253 session_disconnect_msg_t *mp;
254 svm_msg_q_t *mq;
255
256 /* Send to thread that owns the session */
257 mq = s->vpp_evt_q;
258 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
259 mp = (session_disconnect_msg_t *) app_evt->evt->data;
260 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700261 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700262 mp->handle = s->vpp_handle;
263 app_send_ctrl_evt_to_vpp (mq, app_evt);
264}
265
266static void
267vcl_send_app_detach (vcl_worker_t * wrk)
268{
269 app_session_evt_t _app_evt, *app_evt = &_app_evt;
270 session_app_detach_msg_t *mp;
271 svm_msg_q_t *mq;
272
273 mq = vcl_worker_ctrl_mq (wrk);
274 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
275 mp = (session_app_detach_msg_t *) app_evt->evt->data;
276 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700277 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700278 app_send_ctrl_evt_to_vpp (mq, app_evt);
279}
Florin Coras697faea2018-06-27 17:10:49 -0700280
Florin Coras54693d22018-07-17 10:46:29 -0700281static void
282vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
283 session_handle_t handle, int retval)
284{
285 app_session_evt_t _app_evt, *app_evt = &_app_evt;
286 session_accepted_reply_msg_t *rmp;
287 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
288 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
289 rmp->handle = handle;
290 rmp->context = context;
291 rmp->retval = retval;
292 app_send_ctrl_evt_to_vpp (mq, app_evt);
293}
294
Florin Coras99368312018-08-02 10:45:44 -0700295static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800296vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
297 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700298{
299 app_session_evt_t _app_evt, *app_evt = &_app_evt;
300 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800301 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700302 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
303 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800304 rmp->handle = s->vpp_handle;
305 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700306 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800307 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700308}
309
Florin Corasc9fbd662018-08-24 12:59:56 -0700310static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800311vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
312 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700313{
314 app_session_evt_t _app_evt, *app_evt = &_app_evt;
315 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800316 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
317 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700318 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800319 rmp->handle = s->vpp_handle;
320 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700321 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800322 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700323}
324
Florin Coras30e79c22019-01-02 19:31:22 -0800325void
326vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
327 u32 wrk_index)
328{
329 app_session_evt_t _app_evt, *app_evt = &_app_evt;
330 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800331
Florin Coras52dd29f2020-11-18 19:02:17 -0800332 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
333 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800334 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700335 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800336 mp->handle = s->vpp_handle;
337 mp->req_wrk_index = wrk->vpp_wrk_index;
338 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800339 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800340}
341
hanlina3a48962020-07-13 11:09:15 +0800342int
Florin Coras40c07ce2020-07-16 20:46:17 -0700343vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
344{
345 app_session_evt_t _app_evt, *app_evt = &_app_evt;
346 session_app_wrk_rpc_msg_t *mp;
347 vcl_worker_t *dst_wrk, *wrk;
348 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800349 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700350
351 if (data_len > sizeof (mp->data))
352 goto done;
353
354 clib_spinlock_lock (&vcm->workers_lock);
355
356 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
357 if (!dst_wrk)
358 goto done;
359
360 wrk = vcl_worker_get_current ();
361 mq = vcl_worker_ctrl_mq (wrk);
362 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
363 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700364 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700365 mp->wrk_index = dst_wrk->vpp_wrk_index;
366 clib_memcpy (mp->data, data, data_len);
367 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800368 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700369
370done:
371 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800372 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700373}
374
Florin Coras54693d22018-07-17 10:46:29 -0700375static u32
Florin Coras00cca802019-06-06 09:38:44 -0700376vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
377 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700378{
379 vcl_session_t *session, *listen_session;
380 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700381 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700382
Florin Coras134a9962018-08-28 11:32:04 -0700383 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700384
Florin Coras00cca802019-06-06 09:38:44 -0700385 listen_session = vcl_session_get (wrk, ls_index);
386 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700387 {
Florin Coras00cca802019-06-06 09:38:44 -0700388 VDBG (0, "ERROR: listener handle %lu does not match session %u",
389 mp->listener_handle, ls_index);
390 goto error;
391 }
392
Florin Corasc4c4cf52019-08-24 18:17:34 -0700393 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Coras00cca802019-06-06 09:38:44 -0700394 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700395 VDBG (0, "ERROR: segment for session %u is not mounted!",
Florin Coras00cca802019-06-06 09:38:44 -0700396 session->session_index);
397 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700398 }
399
Florin Coras54693d22018-07-17 10:46:29 -0700400 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
401 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Coras653e43f2019-03-04 10:56:23 -0800402 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
403 svm_msg_q_t *);
404 rx_fifo->client_session_index = session->session_index;
405 tx_fifo->client_session_index = session->session_index;
406 rx_fifo->client_thread_index = vcl_get_worker_index ();
407 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras54693d22018-07-17 10:46:29 -0700408
409 session->vpp_handle = mp->handle;
Florin Coras54693d22018-07-17 10:46:29 -0700410 session->rx_fifo = rx_fifo;
411 session->tx_fifo = tx_fifo;
412
Florin Corasdfffdd72020-10-15 10:54:47 -0700413 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700414 session->transport.rmt_port = mp->rmt.port;
415 session->transport.is_ip4 = mp->rmt.is_ip4;
416 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500417 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700418
Florin Coras134a9962018-08-28 11:32:04 -0700419 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700420 session->transport.lcl_port = listen_session->transport.lcl_port;
421 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700422 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200423 session->is_dgram = vcl_proto_is_dgram (session->session_type);
424 session->listener_index = listen_session->session_index;
425 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700426
Florin Coras5e062572019-03-14 19:07:51 -0700427 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
428 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700429 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
430 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
431 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700432 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
433
Florin Coras00cca802019-06-06 09:38:44 -0700434 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
435 session->vpp_handle, 0);
436
Florin Coras134a9962018-08-28 11:32:04 -0700437 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700438
439error:
440 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
441 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
442 VNET_API_ERROR_INVALID_ARGUMENT);
443 vcl_session_free (wrk, session);
444 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700445}
446
447static u32
Florin Coras134a9962018-08-28 11:32:04 -0700448vcl_session_connected_handler (vcl_worker_t * wrk,
449 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700450{
Florin Coras54693d22018-07-17 10:46:29 -0700451 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700452 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800453 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700454
455 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700456 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700457 if (!session)
458 {
Florin Coras5e062572019-03-14 19:07:51 -0700459 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
460 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700461 return VCL_INVALID_SESSION_INDEX;
462 }
Florin Coras54693d22018-07-17 10:46:29 -0700463 if (mp->retval)
464 {
Florin Coras5e062572019-03-14 19:07:51 -0700465 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700466 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700467 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700468 session->vpp_handle = mp->handle;
469 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700470 }
471
Florin Corasdbc9c592019-09-25 16:37:43 -0700472 session->vpp_handle = mp->handle;
473 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
474 svm_msg_q_t *);
Florin Coras460dce62018-07-27 05:45:06 -0700475 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
476 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700477 if (vcl_segment_is_not_mounted (wrk, mp->segment_handle))
Florin Corasd85de682018-11-29 17:02:29 -0800478 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700479 VDBG (0, "segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800480 session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700481 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700482 vcl_send_session_disconnect (wrk, session);
483 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800484 }
485
Florin Coras460dce62018-07-27 05:45:06 -0700486 rx_fifo->client_session_index = session_index;
487 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700488 rx_fifo->client_thread_index = vcl_get_worker_index ();
489 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700490
Florin Coras653e43f2019-03-04 10:56:23 -0800491 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700492 {
Florin Coras653e43f2019-03-04 10:56:23 -0800493 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
494 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700495 if (vcl_segment_is_not_mounted (wrk, mp->ct_segment_handle))
Florin Coras653e43f2019-03-04 10:56:23 -0800496 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700497 VDBG (0, "ct segment for session %u is not mounted!",
Florin Coras653e43f2019-03-04 10:56:23 -0800498 session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700499 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700500 vcl_send_session_disconnect (wrk, session);
501 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800502 }
Florin Coras99368312018-08-02 10:45:44 -0700503 }
Florin Coras54693d22018-07-17 10:46:29 -0700504
Florin Coras54693d22018-07-17 10:46:29 -0700505 session->rx_fifo = rx_fifo;
506 session->tx_fifo = tx_fifo;
Florin Coras09d18c22019-04-24 11:10:02 -0700507 session->transport.is_ip4 = mp->lcl.is_ip4;
508 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500509 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700510 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700511
512 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700513 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700514 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700515 vcl_send_session_disconnect (wrk, session);
516 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700517 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700518
519 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800520 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700521
Florin Coras5e062572019-03-14 19:07:51 -0700522 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
523 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700524 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700525
Florin Coras54693d22018-07-17 10:46:29 -0700526 return session_index;
527}
528
Florin Coras3c7d4f92018-12-14 11:28:43 -0800529static int
530vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
531{
532 vcl_session_msg_t *accepted_msg;
533 int i;
534
535 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
536 {
537 accepted_msg = &session->accept_evts_fifo[i];
538 if (accepted_msg->accepted_msg.handle == handle)
539 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800540 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800541 return 1;
542 }
543 }
544 return 0;
545}
546
Florin Corasc9fbd662018-08-24 12:59:56 -0700547static u32
Florin Coras134a9962018-08-28 11:32:04 -0700548vcl_session_reset_handler (vcl_worker_t * wrk,
549 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700550{
551 vcl_session_t *session;
552 u32 sid;
553
Florin Coras134a9962018-08-28 11:32:04 -0700554 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
555 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700556 if (!session)
557 {
558 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
559 return VCL_INVALID_SESSION_INDEX;
560 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800561
562 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700563 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800564 {
565
566 if (!vcl_flag_accepted_session (session, reset_msg->handle,
567 VCL_ACCEPTED_F_RESET))
568 VDBG (0, "session was not accepted!");
569 return VCL_INVALID_SESSION_INDEX;
570 }
571
Florin Corasc127d5a2020-10-14 16:35:58 -0700572 if (session->session_state != VCL_STATE_CLOSED)
573 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800574 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700575 return sid;
576}
577
Florin Coras60116992018-08-27 09:52:18 -0700578static u32
Florin Coras134a9962018-08-28 11:32:04 -0700579vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700580{
581 vcl_session_t *session;
582 u32 sid = mp->context;
583
Florin Coras134a9962018-08-28 11:32:04 -0700584 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700585 if (mp->retval)
586 {
Florin Coras5e062572019-03-14 19:07:51 -0700587 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700588 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700589 if (session)
590 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700591 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700592 session->vpp_handle = mp->handle;
593 return sid;
594 }
595 else
596 {
Florin Coras5e062572019-03-14 19:07:51 -0700597 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
598 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700599 return VCL_INVALID_SESSION_INDEX;
600 }
601 }
602
603 session->vpp_handle = mp->handle;
604 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500605 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
606 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700607 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700608 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700609 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800610 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
Florin Coras5f45e012019-01-23 09:21:30 -0800611
Florin Coras6e3c1f82020-01-15 01:30:46 +0000612 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700613 {
614 svm_fifo_t *rx_fifo, *tx_fifo;
615 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
616 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
617 rx_fifo->client_session_index = sid;
618 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
619 tx_fifo->client_session_index = sid;
620 session->rx_fifo = rx_fifo;
621 session->tx_fifo = tx_fifo;
622 }
623
Florin Coras05ecfcc2018-12-12 18:19:39 -0800624 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700625 return sid;
626}
627
Florin Corasdfae9f92019-02-20 19:48:31 -0800628static void
629vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
630{
631 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
632 vcl_session_t *s;
633
634 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000635 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800636 {
637 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
638 return;
639 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700640 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000641 {
642 /* Connected udp listener */
643 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700644 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000645 return;
646
647 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
648 return;
649 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800650
651 if (mp->retval)
652 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700653 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800654
655 if (mp->context != wrk->wrk_index)
656 VDBG (0, "wrong context");
657
658 vcl_session_table_del_vpp_handle (wrk, mp->handle);
659 vcl_session_free (wrk, s);
660}
661
Florin Coras68b7e582020-01-21 18:33:23 -0800662static void
663vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
664{
665 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
666 vcl_session_t *s;
667
668 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
669 if (!s)
670 {
671 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
672 return;
673 }
674
Florin Coras57660d92020-04-04 22:45:34 +0000675 s->vpp_handle = mp->new_handle;
Florin Coras68b7e582020-01-21 18:33:23 -0800676 s->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
677
Florin Coras68b7e582020-01-21 18:33:23 -0800678 vcl_session_table_del_vpp_handle (wrk, mp->handle);
679 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
680
681 /* Generate new tx event if we have outstanding data */
682 if (svm_fifo_has_event (s->tx_fifo))
683 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
684 SESSION_IO_EVT_TX, SVM_Q_WAIT);
685
Florin Coras57660d92020-04-04 22:45:34 +0000686 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800687 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800688}
689
Florin Coras3c7d4f92018-12-14 11:28:43 -0800690static vcl_session_t *
691vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
692{
693 vcl_session_msg_t *vcl_msg;
694 vcl_session_t *session;
695
696 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
697 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800698 VWRN ("session overlap handle %lu state %u!", msg->handle,
699 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800700
701 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
702 if (!session)
703 {
704 VERR ("couldn't find listen session: listener handle %llx",
705 msg->listener_handle);
706 return 0;
707 }
708
709 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000710 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800711 vcl_msg->accepted_msg = *msg;
712 /* Session handle points to listener until fully accepted by app */
713 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
714
715 return session;
716}
717
718static vcl_session_t *
719vcl_session_disconnected_handler (vcl_worker_t * wrk,
720 session_disconnected_msg_t * msg)
721{
722 vcl_session_t *session;
723
724 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
725 if (!session)
726 {
727 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
728 return 0;
729 }
730
Florin Corasadcfb152020-02-12 08:50:29 +0000731 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700732 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000733 return 0;
734
Florin Coras3c7d4f92018-12-14 11:28:43 -0800735 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700736 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800737 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800738 if (!vcl_flag_accepted_session (session, msg->handle,
739 VCL_ACCEPTED_F_CLOSED))
740 VDBG (0, "session was not accepted!");
741 return 0;
742 }
743
Florin Corasadcfb152020-02-12 08:50:29 +0000744 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700745 if (session->session_state != VCL_STATE_DISCONNECT)
746 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000747
Florin Coras3c7d4f92018-12-14 11:28:43 -0800748 return session;
749}
750
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700751static int
752vppcom_session_disconnect (u32 session_handle)
753{
754 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700755 vcl_session_t *session, *listen_session;
756 vcl_session_state_t state;
757 u64 vpp_handle;
758
759 session = vcl_session_get_w_handle (wrk, session_handle);
760 if (!session)
761 return VPPCOM_EBADFD;
762
763 vpp_handle = session->vpp_handle;
764 state = session->session_state;
765
766 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
767 vpp_handle, state, vppcom_session_state_str (state));
768
769 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
770 {
771 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
772 return VPPCOM_EBADFD;
773 }
774
775 if (state == VCL_STATE_VPP_CLOSING)
776 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800777 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700778 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
779 session->session_index, vpp_handle);
780 }
781 else
782 {
783 /* Session doesn't have an event queue yet. Probably a non-blocking
784 * connect. Wait for the reply */
785 if (PREDICT_FALSE (!session->vpp_evt_q))
786 return VPPCOM_OK;
787
788 VDBG (1, "session %u [0x%llx]: sending disconnect...",
789 session->session_index, vpp_handle);
790 vcl_send_session_disconnect (wrk, session);
791 }
792
793 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
794 {
795 listen_session = vcl_session_get (wrk, session->listener_index);
796 listen_session->n_accepted_sessions--;
797 }
798
799 return VPPCOM_OK;
800}
801
Florin Coras30e79c22019-01-02 19:31:22 -0800802static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700803vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
804{
805 session_cleanup_msg_t *msg;
806 vcl_session_t *session;
807
808 msg = (session_cleanup_msg_t *) data;
809 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
810 if (!session)
811 {
812 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
813 return;
814 }
815
Florin Coras36d49392020-04-24 23:00:11 +0000816 if (msg->type == SESSION_CLEANUP_TRANSPORT)
817 {
818 /* Transport was cleaned up before we confirmed close. Probably the
819 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700820 * Confirm close to make sure everything is cleaned up.
821 * Move to undetermined state to ensure that the session is not
822 * removed before both vpp and the app cleanup.
823 * - If the app closes first, the session is moved to CLOSED state
824 * and the session cleanup notification from vpp removes the
825 * session.
826 * - If vpp cleans up the session first, the session is moved to
827 * DETACHED state lower and subsequently the close from the app
828 * frees the session
829 */
830 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700831 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700832 vppcom_session_disconnect (vcl_session_handle (session));
833 session->session_state = VCL_STATE_UPDATED;
834 }
835 else if (session->session_state == VCL_STATE_DISCONNECT)
836 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800837 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700838 session->session_state = VCL_STATE_UPDATED;
839 }
Florin Coras36d49392020-04-24 23:00:11 +0000840 return;
841 }
842
Florin Coras9ace36d2019-10-28 13:14:17 -0700843 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000844 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700845 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000846 {
847 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700848 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000849 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
850 return;
851 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700852 vcl_session_free (wrk, session);
853}
854
855static void
Florin Coras30e79c22019-01-02 19:31:22 -0800856vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
857{
858 session_req_worker_update_msg_t *msg;
859 vcl_session_t *s;
860
861 msg = (session_req_worker_update_msg_t *) data;
862 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
863 if (!s)
864 return;
865
866 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
867}
868
869static void
870vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
871{
872 session_worker_update_reply_msg_t *msg;
873 vcl_session_t *s;
874
875 msg = (session_worker_update_reply_msg_t *) data;
876 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
877 if (!s)
878 {
879 VDBG (0, "unknown handle 0x%llx", msg->handle);
880 return;
881 }
Florin Corasc4c4cf52019-08-24 18:17:34 -0700882 if (vcl_segment_is_not_mounted (wrk, msg->segment_handle))
Florin Coras30e79c22019-01-02 19:31:22 -0800883 {
Florin Corasc4c4cf52019-08-24 18:17:34 -0700884 clib_warning ("segment for session %u is not mounted!",
Florin Coras30e79c22019-01-02 19:31:22 -0800885 s->session_index);
886 return;
887 }
Florin Coras30e79c22019-01-02 19:31:22 -0800888
Florin Coras5f45e012019-01-23 09:21:30 -0800889 if (s->rx_fifo)
890 {
891 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
892 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
893 s->rx_fifo->client_session_index = s->session_index;
894 s->tx_fifo->client_session_index = s->session_index;
895 s->rx_fifo->client_thread_index = wrk->wrk_index;
896 s->tx_fifo->client_thread_index = wrk->wrk_index;
897 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700898 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800899
Florin Coras30e79c22019-01-02 19:31:22 -0800900 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
901 s->vpp_handle, wrk->wrk_index);
902}
903
Florin Coras935ce752020-09-08 22:43:47 -0700904static int
905vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
906{
907
908 if (vcm->cfg.vpp_app_socket_api)
909 return vcl_sapi_recv_fds (wrk, fds, n_fds);
910
911 return vcl_bapi_recv_fds (wrk, fds, n_fds);
912}
913
Florin Corasc4c4cf52019-08-24 18:17:34 -0700914static void
915vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
916{
917 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
918 session_app_add_segment_msg_t *msg;
919 u64 segment_handle;
920 int fd = -1;
921
922 msg = (session_app_add_segment_msg_t *) data;
923
924 if (msg->fd_flags)
925 {
Florin Coras935ce752020-09-08 22:43:47 -0700926 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700927 seg_type = SSVM_SEGMENT_MEMFD;
928 }
929
930 segment_handle = msg->segment_handle;
931 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
932 {
933 clib_warning ("invalid segment handle");
934 return;
935 }
936
937 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
938 seg_type, fd))
939 {
940 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
941 return;
942 }
943
944 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
945 msg->segment_size);
946}
947
948static void
949vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
950{
951 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
952 vcl_segment_detach (msg->segment_handle);
953 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
954}
955
Florin Coras40c07ce2020-07-16 20:46:17 -0700956static void
957vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
958{
959 if (!vcm->wrk_rpc_fn)
960 return;
961
hanlina3a48962020-07-13 11:09:15 +0800962 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700963}
964
Florin Coras86f04502018-09-12 16:08:01 -0700965static int
966vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700967{
Florin Coras54693d22018-07-17 10:46:29 -0700968 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700969 session_connected_msg_t *connected_msg;
970 session_reset_msg_t *reset_msg;
971 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -0700972 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -0700973 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -0700974
975 switch (e->event_type)
976 {
Florin Coras653e43f2019-03-04 10:56:23 -0800977 case SESSION_IO_EVT_RX:
978 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -0700979 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -0700980 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -0800981 break;
Florin Coras86f04502018-09-12 16:08:01 -0700982 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700983 break;
Florin Corasb242d312020-10-26 15:35:40 -0700984 case SESSION_CTRL_EVT_BOUND:
985 /* We can only wait for only one listen so not postponed */
986 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
987 break;
Florin Coras54693d22018-07-17 10:46:29 -0700988 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -0700989 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
990 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
991 {
992 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
993 *ecpy = *e;
994 ecpy->postponed = 1;
995 ecpy->session_index = s->session_index;
996 }
Florin Coras54693d22018-07-17 10:46:29 -0700997 break;
998 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -0700999 connected_msg = (session_connected_msg_t *) e->data;
1000 sid = vcl_session_connected_handler (wrk, connected_msg);
1001 if (!(s = vcl_session_get (wrk, sid)))
1002 break;
1003 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1004 {
1005 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
1006 *ecpy = *e;
1007 ecpy->postponed = 1;
1008 ecpy->session_index = s->session_index;
1009 }
Florin Coras54693d22018-07-17 10:46:29 -07001010 break;
1011 case SESSION_CTRL_EVT_DISCONNECTED:
1012 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07001013 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
1014 break;
1015 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1016 {
1017 vec_add1 (wrk->unhandled_evts_vector, *e);
1018 break;
1019 }
1020 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -08001021 break;
Florin Corasc127d5a2020-10-14 16:35:58 -07001022 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
1023 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -07001024 break;
1025 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -07001026 reset_msg = (session_reset_msg_t *) e->data;
1027 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
1028 break;
1029 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
1030 {
1031 vec_add1 (wrk->unhandled_evts_vector, *e);
1032 break;
1033 }
Florin Coras134a9962018-08-28 11:32:04 -07001034 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001035 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001036 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1037 vcl_session_unlisten_reply_handler (wrk, e->data);
1038 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001039 case SESSION_CTRL_EVT_MIGRATED:
1040 vcl_session_migrated_handler (wrk, e->data);
1041 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001042 case SESSION_CTRL_EVT_CLEANUP:
1043 vcl_session_cleanup_handler (wrk, e->data);
1044 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001045 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1046 vcl_session_req_worker_update_handler (wrk, e->data);
1047 break;
1048 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1049 vcl_session_worker_update_reply_handler (wrk, e->data);
1050 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001051 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1052 vcl_session_app_add_segment_handler (wrk, e->data);
1053 break;
1054 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1055 vcl_session_app_del_segment_handler (wrk, e->data);
1056 break;
hanlina3a48962020-07-13 11:09:15 +08001057 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001058 vcl_worker_rpc_handler (wrk, e->data);
1059 break;
Florin Coras54693d22018-07-17 10:46:29 -07001060 default:
1061 clib_warning ("unhandled %u", e->event_type);
1062 }
1063 return VPPCOM_OK;
1064}
1065
Florin Coras30e79c22019-01-02 19:31:22 -08001066static int
Florin Coras697faea2018-06-27 17:10:49 -07001067vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001068 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001069 f64 wait_for_time)
1070{
Florin Coras134a9962018-08-28 11:32:04 -07001071 vcl_worker_t *wrk = vcl_worker_get_current ();
1072 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001073 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001074 svm_msg_q_msg_t msg;
1075 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001076
Florin Coras697faea2018-06-27 17:10:49 -07001077 do
Dave Wallace543852a2017-08-03 02:11:34 -04001078 {
Florin Coras134a9962018-08-28 11:32:04 -07001079 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001080 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001081 {
Florin Coras070453d2018-08-24 17:04:27 -07001082 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001083 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001084 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001085 {
Florin Coras697faea2018-06-27 17:10:49 -07001086 return VPPCOM_OK;
1087 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001088 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001089 {
Florin Coras697faea2018-06-27 17:10:49 -07001090 return VPPCOM_ECONNREFUSED;
1091 }
Florin Coras54693d22018-07-17 10:46:29 -07001092
Florin Coras134a9962018-08-28 11:32:04 -07001093 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001094 {
1095 usleep (100);
1096 continue;
1097 }
Florin Coras134a9962018-08-28 11:32:04 -07001098 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001099 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001100 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001101 }
Florin Coras134a9962018-08-28 11:32:04 -07001102 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001103
Florin Coras05ecfcc2018-12-12 18:19:39 -08001104 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001105 vppcom_session_state_str (state));
1106 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001107
Florin Coras697faea2018-06-27 17:10:49 -07001108 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001109}
1110
Florin Coras30e79c22019-01-02 19:31:22 -08001111static void
1112vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1113{
Florin Coras288eaab2019-02-03 15:26:14 -08001114 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001115 vcl_session_t *s;
1116 u32 *sip;
1117
1118 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1119 return;
1120
1121 vec_foreach (sip, wrk->pending_session_wrk_updates)
1122 {
1123 s = vcl_session_get (wrk, *sip);
1124 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1125 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001126 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1127 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001128 s->session_state = state;
1129 }
1130 vec_reset_length (wrk->pending_session_wrk_updates);
1131}
1132
Florin Corasf9240dc2019-01-15 08:03:17 -08001133void
Florin Coras30e79c22019-01-02 19:31:22 -08001134vcl_flush_mq_events (void)
1135{
1136 vcl_worker_t *wrk = vcl_worker_get_current ();
1137 svm_msg_q_msg_t *msg;
1138 session_event_t *e;
1139 svm_msg_q_t *mq;
1140 int i;
1141
1142 mq = wrk->app_event_queue;
1143 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -07001144 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001145 svm_msg_q_unlock (mq);
1146
1147 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1148 {
1149 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1150 e = svm_msg_q_msg_data (mq, msg);
1151 vcl_handle_mq_event (wrk, e);
1152 svm_msg_q_free_msg (mq, msg);
1153 }
1154 vec_reset_length (wrk->mq_msg_vector);
1155 vcl_handle_pending_wrk_updates (wrk);
1156}
1157
Florin Coras697faea2018-06-27 17:10:49 -07001158static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001159vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001160{
Florin Coras134a9962018-08-28 11:32:04 -07001161 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001162 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001163 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001164 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001165
Florin Corasab2f6db2018-08-31 14:31:41 -07001166 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001167 if (!session)
1168 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001169
Florin Corasaaff5ee2019-07-08 13:00:00 -07001170 /* Flush pending accept events, if any */
1171 while (clib_fifo_elts (session->accept_evts_fifo))
1172 {
1173 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1174 accepted_msg = &evt->accepted_msg;
1175 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1176 vcl_send_session_accepted_reply (session->vpp_evt_q,
1177 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001178 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001179 }
1180 clib_fifo_free (session->accept_evts_fifo);
1181
Florin Coras458089b2019-08-21 16:20:44 -07001182 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001183
Florin Coras5e062572019-03-14 19:07:51 -07001184 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001185 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001186 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001187
1188 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001189 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001190
Florin Coras070453d2018-08-24 17:04:27 -07001191 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001192}
1193
Florin Coras940f78f2018-11-30 12:11:20 -08001194/**
1195 * Handle app exit
1196 *
1197 * Notify vpp of the disconnect and mark the worker as free. If we're the
1198 * last worker, do a full cleanup otherwise, since we're probably a forked
1199 * child, avoid syscalls as much as possible. We might've lost privileges.
1200 */
1201void
1202vppcom_app_exit (void)
1203{
1204 if (!pool_elts (vcm->workers))
1205 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001206 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1207 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001208 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001209}
1210
Florin Coras935ce752020-09-08 22:43:47 -07001211static int
1212vcl_api_attach (void)
1213{
1214 if (vcm->cfg.vpp_app_socket_api)
1215 return vcl_sapi_attach ();
1216
1217 return vcl_bapi_attach ();
1218}
1219
1220static void
1221vcl_api_detach (vcl_worker_t * wrk)
1222{
1223 vcl_send_app_detach (wrk);
1224
1225 if (vcm->cfg.vpp_app_socket_api)
1226 return vcl_sapi_detach (wrk);
1227
1228 return vcl_bapi_disconnect_from_vpp ();
1229}
1230
Dave Wallace543852a2017-08-03 02:11:34 -04001231/*
1232 * VPPCOM Public API functions
1233 */
1234int
Florin Coras66ec4672020-06-15 07:59:40 -07001235vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001236{
Dave Wallace543852a2017-08-03 02:11:34 -04001237 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001238 int rv;
1239
Florin Coras47c40e22018-11-26 17:01:36 -08001240 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001241 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001242 VDBG (1, "already initialized");
1243 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001244 }
1245
Florin Coras47c40e22018-11-26 17:01:36 -08001246 vcm->is_init = 1;
1247 vppcom_cfg (&vcm->cfg);
1248 vcl_cfg = &vcm->cfg;
1249
1250 vcm->main_cpu = pthread_self ();
1251 vcm->main_pid = getpid ();
1252 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001253 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1254 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001255 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1256 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001257 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001258 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001259 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001260
1261 /* Allocate default worker */
1262 vcl_worker_alloc_and_init ();
1263
Florin Coras935ce752020-09-08 22:43:47 -07001264 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001265 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001266
1267 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001268 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001269
1270 return VPPCOM_OK;
1271}
1272
1273void
1274vppcom_app_destroy (void)
1275{
Florin Corasdc0ded72020-04-30 02:59:55 +00001276 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001277 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001278
Florin Coras940f78f2018-11-30 12:11:20 -08001279 if (!pool_elts (vcm->workers))
1280 return;
1281
Florin Coras0d427d82018-06-27 03:24:07 -07001282 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001283
Florin Corasdc0ded72020-04-30 02:59:55 +00001284 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001285
Florin Corasce815de2020-04-16 18:47:27 +00001286 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001287 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001288 if (current_wrk != wrk)
1289 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001290 }
Florin Corasce815de2020-04-16 18:47:27 +00001291 /* *INDENT-ON* */
1292
Florin Coras935ce752020-09-08 22:43:47 -07001293 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001294 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1295
Florin Coras0d427d82018-06-27 03:24:07 -07001296 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001297
1298 /*
1299 * Free the heap and fix vcm
1300 */
1301 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001302 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001303
1304 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001305 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001306}
1307
1308int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001309vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001310{
Florin Coras134a9962018-08-28 11:32:04 -07001311 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001312 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001313
Florin Coras134a9962018-08-28 11:32:04 -07001314 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001315
Florin Coras7e12d942018-06-27 14:32:43 -07001316 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001317 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001318 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001319 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001320
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001321 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001322 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001323
Florin Coras7e12d942018-06-27 14:32:43 -07001324 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1325 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001326
Florin Coras5e062572019-03-14 19:07:51 -07001327 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001328
Florin Coras134a9962018-08-28 11:32:04 -07001329 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001330}
1331
1332int
Florin Corasc127d5a2020-10-14 16:35:58 -07001333vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001334 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001335{
Florin Coras134a9962018-08-28 11:32:04 -07001336 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001337
Florin Coras6c3b2182020-10-19 18:36:48 -07001338 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001339
Florin Coras6c3b2182020-10-19 18:36:48 -07001340 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001341 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001342 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001343 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001344 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001345 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001346 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001347 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001348 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1349 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001350 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001351 }
Florin Corase4306b62020-10-28 12:51:10 -07001352 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001353 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001354
Florin Coras6c3b2182020-10-19 18:36:48 -07001355 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001356 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001357 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001358 if (rv < 0)
1359 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001360 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1361 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001362 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001363
Florin Coras9ace36d2019-10-28 13:14:17 -07001364 if (!do_disconnect)
1365 {
1366 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001367 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001368 goto cleanup;
1369 }
1370
Florin Coras6c3b2182020-10-19 18:36:48 -07001371 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001372 {
1373 rv = vppcom_session_unbind (sh);
1374 if (PREDICT_FALSE (rv < 0))
1375 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001376 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001377 vppcom_retval_str (rv));
1378 return rv;
1379 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001380 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001381 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001382 {
1383 rv = vppcom_session_disconnect (sh);
1384 if (PREDICT_FALSE (rv < 0))
1385 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001386 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001387 rv, vppcom_retval_str (rv));
1388 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001389 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001390 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001391 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001392 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001393 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001394 {
1395 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001396 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001397 goto free_session;
1398 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001399
Florin Corasc127d5a2020-10-14 16:35:58 -07001400 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001401
Florin Coras9ace36d2019-10-28 13:14:17 -07001402 /* Session is removed only after vpp confirms the disconnect */
1403 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001404
Florin Corasf9240dc2019-01-15 08:03:17 -08001405cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001406 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001407free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001408 vcl_session_free (wrk, s);
1409 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001410
Dave Wallace543852a2017-08-03 02:11:34 -04001411 return rv;
1412}
1413
1414int
Florin Corasf9240dc2019-01-15 08:03:17 -08001415vppcom_session_close (uint32_t session_handle)
1416{
1417 vcl_worker_t *wrk = vcl_worker_get_current ();
1418 vcl_session_t *session;
1419
1420 session = vcl_session_get_w_handle (wrk, session_handle);
1421 if (!session)
1422 return VPPCOM_EBADFD;
1423 return vcl_session_cleanup (wrk, session, session_handle,
1424 1 /* do_disconnect */ );
1425}
1426
1427int
Florin Coras134a9962018-08-28 11:32:04 -07001428vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001429{
Florin Coras134a9962018-08-28 11:32:04 -07001430 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001431 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001432
1433 if (!ep || !ep->ip)
1434 return VPPCOM_EINVAL;
1435
Florin Coras134a9962018-08-28 11:32:04 -07001436 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001437 if (!session)
1438 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001439
Florin Coras6c3b2182020-10-19 18:36:48 -07001440 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001441 {
Florin Coras5e062572019-03-14 19:07:51 -07001442 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1443 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001444 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001445 }
1446
Florin Coras7e12d942018-06-27 14:32:43 -07001447 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001448 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001449 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1450 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001451 else
Dave Barach178cf492018-11-13 16:34:13 -05001452 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1453 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001454 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001455
Florin Coras5e062572019-03-14 19:07:51 -07001456 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1457 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001458 session->transport.is_ip4 ? "IPv4" : "IPv6",
1459 format_ip46_address, &session->transport.lcl_ip,
1460 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1461 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001462 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001463 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001464
1465 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001466 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001467
Florin Coras070453d2018-08-24 17:04:27 -07001468 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001469}
1470
1471int
Florin Coras134a9962018-08-28 11:32:04 -07001472vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001473{
Florin Coras134a9962018-08-28 11:32:04 -07001474 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001475 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001476 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001477 int rv;
1478
Florin Coras134a9962018-08-28 11:32:04 -07001479 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001480 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001481 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001482
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001483 if (q_len == 0 || q_len == ~0)
1484 q_len = vcm->cfg.listen_queue_size;
1485
Dave Wallaceee45d412017-11-24 21:44:06 -05001486 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001487 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001488 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001489 VDBG (0, "session %u [0x%llx]: already in listen state!",
1490 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001491 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001492 }
1493
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001494 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001495
Florin Coras070453d2018-08-24 17:04:27 -07001496 /*
1497 * Send listen request to vpp and wait for reply
1498 */
Florin Coras458089b2019-08-21 16:20:44 -07001499 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001500 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001501 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001502 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001503
Florin Coras070453d2018-08-24 17:04:27 -07001504 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001505 {
Florin Coras134a9962018-08-28 11:32:04 -07001506 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001507 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1508 listen_sh, listen_session->vpp_handle, rv,
1509 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001510 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001511 }
1512
Florin Coras070453d2018-08-24 17:04:27 -07001513 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001514}
1515
Florin Coras134a9962018-08-28 11:32:04 -07001516static int
Florin Coras5e062572019-03-14 19:07:51 -07001517validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001518{
Florin Coras6c3b2182020-10-19 18:36:48 -07001519 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001520 {
Florin Coras5e062572019-03-14 19:07:51 -07001521 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1522 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001523 return VPPCOM_EBADFD;
1524 }
1525
Florin Corasc127d5a2020-10-14 16:35:58 -07001526 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001527 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001528 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001529 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001530 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001531 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001532 return VPPCOM_EBADFD;
1533 }
1534 return VPPCOM_OK;
1535}
1536
1537int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001538vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1539{
1540 if (!strcmp (proto_str, "TCP"))
1541 *proto = VPPCOM_PROTO_TCP;
1542 else if (!strcmp (proto_str, "tcp"))
1543 *proto = VPPCOM_PROTO_TCP;
1544 else if (!strcmp (proto_str, "UDP"))
1545 *proto = VPPCOM_PROTO_UDP;
1546 else if (!strcmp (proto_str, "udp"))
1547 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001548 else if (!strcmp (proto_str, "TLS"))
1549 *proto = VPPCOM_PROTO_TLS;
1550 else if (!strcmp (proto_str, "tls"))
1551 *proto = VPPCOM_PROTO_TLS;
1552 else if (!strcmp (proto_str, "QUIC"))
1553 *proto = VPPCOM_PROTO_QUIC;
1554 else if (!strcmp (proto_str, "quic"))
1555 *proto = VPPCOM_PROTO_QUIC;
1556 else
1557 return 1;
1558 return 0;
1559}
1560
1561int
Florin Coras134a9962018-08-28 11:32:04 -07001562vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001563 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001564{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001565 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001566 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001567 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001568 vcl_session_t *listen_session = 0;
1569 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001570 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001571 svm_msg_q_msg_t msg;
1572 session_event_t *e;
1573 u8 is_nonblocking;
1574 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001575
Florin Coras134a9962018-08-28 11:32:04 -07001576 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001577 if (!listen_session)
1578 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001579
Florin Coras134a9962018-08-28 11:32:04 -07001580 listen_session_index = listen_session->session_index;
1581 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001582 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001583
Florin Coras54693d22018-07-17 10:46:29 -07001584 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001585 {
Florin Coras54693d22018-07-17 10:46:29 -07001586 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001587 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001588 accepted_msg = evt->accepted_msg;
1589 goto handle;
1590 }
1591
Florin Corasac422d62020-10-19 20:51:36 -07001592 is_nonblocking = vcl_session_has_attr (listen_session,
1593 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001594 while (1)
1595 {
Carl Smith592a9092019-11-12 14:57:37 +13001596 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1597 return VPPCOM_EAGAIN;
1598
Florin Coras134a9962018-08-28 11:32:04 -07001599 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001600 return VPPCOM_EAGAIN;
1601
Florin Coras134a9962018-08-28 11:32:04 -07001602 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001603 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001604 {
wanghanlin96453fd2019-12-16 19:14:39 +08001605 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001606 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001607 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001608 }
Dave Barach178cf492018-11-13 16:34:13 -05001609 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001610 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001611 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001612 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001613
Florin Coras54693d22018-07-17 10:46:29 -07001614handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001615
Florin Coras00cca802019-06-06 09:38:44 -07001616 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1617 listen_session_index);
1618 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1619 return VPPCOM_ECONNABORTED;
1620
Florin Coras134a9962018-08-28 11:32:04 -07001621 listen_session = vcl_session_get (wrk, listen_session_index);
1622 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001623
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001624 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001625 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001626
Florin Coras5e062572019-03-14 19:07:51 -07001627 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1628 " flags %d, is_nonblocking %u", listen_session->session_index,
1629 listen_session->vpp_handle, client_session_index,
1630 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001631 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001632
Dave Wallace048b1d62018-01-03 22:24:41 -05001633 if (ep)
1634 {
Florin Coras7e12d942018-06-27 14:32:43 -07001635 ep->is_ip4 = client_session->transport.is_ip4;
1636 ep->port = client_session->transport.rmt_port;
1637 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001638 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1639 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001640 else
Dave Barach178cf492018-11-13 16:34:13 -05001641 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1642 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001643 }
Dave Wallace60caa062017-11-10 17:07:13 -05001644
Florin Coras05ecfcc2018-12-12 18:19:39 -08001645 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001646 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001647 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001648 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001649 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001650 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001651 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001652 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001653 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001654 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1655 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001656
Florin Coras3c7d4f92018-12-14 11:28:43 -08001657 /*
1658 * Session might have been closed already
1659 */
1660 if (accept_flags)
1661 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001662 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001663 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001664 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001665 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001666 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001667 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001668}
1669
1670int
Florin Coras134a9962018-08-28 11:32:04 -07001671vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001672{
Florin Coras134a9962018-08-28 11:32:04 -07001673 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001674 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001675 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001676 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001677
Florin Coras134a9962018-08-28 11:32:04 -07001678 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001679 if (!session)
1680 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001681 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001682
Florin Coras6c3b2182020-10-19 18:36:48 -07001683 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001684 {
Florin Coras5e062572019-03-14 19:07:51 -07001685 VDBG (0, "ERROR: cannot connect epoll session %u!",
1686 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001687 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001688 }
1689
Florin Corasc127d5a2020-10-14 16:35:58 -07001690 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001691 {
Florin Coras7baeb712019-01-04 17:05:43 -08001692 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001693 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001694 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001695 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001696 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001697 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001698 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001699 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001700 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001701 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001702 }
1703
Florin Coras0a1e1832020-03-29 18:54:04 +00001704 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001705 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001706 {
1707 if (session->session_type != VPPCOM_PROTO_UDP)
1708 return VPPCOM_EINVAL;
1709 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001710 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001711 }
1712
Florin Coras7e12d942018-06-27 14:32:43 -07001713 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001714 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001715 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001716 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001717 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001718
Florin Coras0a1e1832020-03-29 18:54:04 +00001719 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001720 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001721 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001722 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001723 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001724 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001725 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001726 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001727 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001728
Florin Coras458089b2019-08-21 16:20:44 -07001729 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001730
Florin Corasac422d62020-10-19 20:51:36 -07001731 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001732 {
fanyfa49d59d2020-10-13 17:07:16 +08001733 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001734 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001735 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001736 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001737 return VPPCOM_EINPROGRESS;
1738 }
Florin Coras57c88932019-08-29 12:03:17 -07001739
1740 /*
1741 * Wait for reply from vpp if blocking
1742 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001743 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001744 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001745
Florin Coras134a9962018-08-28 11:32:04 -07001746 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001747 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1748 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001749
Dave Wallace4878cbe2017-11-21 03:45:09 -05001750 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001751}
1752
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001753int
1754vppcom_session_stream_connect (uint32_t session_handle,
1755 uint32_t parent_session_handle)
1756{
1757 vcl_worker_t *wrk = vcl_worker_get_current ();
1758 vcl_session_t *session, *parent_session;
1759 u32 session_index, parent_session_index;
1760 int rv;
1761
1762 session = vcl_session_get_w_handle (wrk, session_handle);
1763 if (!session)
1764 return VPPCOM_EBADFD;
1765 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1766 if (!parent_session)
1767 return VPPCOM_EBADFD;
1768
1769 session_index = session->session_index;
1770 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001771 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001772 {
1773 VDBG (0, "ERROR: cannot connect epoll session %u!",
1774 session->session_index);
1775 return VPPCOM_EBADFD;
1776 }
1777
Florin Corasc127d5a2020-10-14 16:35:58 -07001778 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001779 {
1780 VDBG (0, "session handle %u [0x%llx]: session already "
1781 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1782 session_handle, session->vpp_handle,
1783 parent_session_handle, parent_session->vpp_handle,
1784 vppcom_proto_str (session->session_type), session->session_state,
1785 vppcom_session_state_str (session->session_state));
1786 return VPPCOM_OK;
1787 }
1788
1789 /* Connect to quic session specifics */
1790 session->transport.is_ip4 = parent_session->transport.is_ip4;
1791 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1792 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001793 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001794
1795 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1796 session_handle, parent_session_handle, parent_session->vpp_handle);
1797
1798 /*
1799 * Send connect request and wait for reply from vpp
1800 */
Florin Coras458089b2019-08-21 16:20:44 -07001801 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001802 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001803 vcm->cfg.session_timeout);
1804
1805 session->listener_index = parent_session_index;
1806 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001807 if (parent_session)
1808 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001809
1810 session = vcl_session_get (wrk, session_index);
1811 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1812 session->vpp_handle, rv ? "failed" : "succeeded");
1813
1814 return rv;
1815}
1816
Florin Coras54693d22018-07-17 10:46:29 -07001817static u8
1818vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1819{
Florin Corasc0737e92019-03-04 14:19:39 -08001820 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001821}
1822
Steven58f464e2017-10-25 12:33:12 -07001823static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001824vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001825 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001826{
Florin Coras134a9962018-08-28 11:32:04 -07001827 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001828 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001829 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001830 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001831 svm_msg_q_msg_t msg;
1832 session_event_t *e;
1833 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001834 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001835
Florin Coras070453d2018-08-24 17:04:27 -07001836 if (PREDICT_FALSE (!buf))
1837 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001838
Florin Coras134a9962018-08-28 11:32:04 -07001839 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001840 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001841 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001842
Florin Coras6d0106e2019-01-29 20:11:58 -08001843 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001844 {
Florin Coras5e062572019-03-14 19:07:51 -07001845 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001846 s->session_index, s->vpp_handle, s->session_state,
1847 vppcom_session_state_str (s->session_state));
1848 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001849 }
1850
Florin Corasac422d62020-10-19 20:51:36 -07001851 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001852 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001853 mq = wrk->app_event_queue;
1854 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001855 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001856
Sirshak Das28aa5392019-02-05 01:33:33 -06001857 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001858 {
Florin Coras54693d22018-07-17 10:46:29 -07001859 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001860 {
Yu Pingb2955352019-12-04 06:49:04 +08001861 if (vcl_session_is_closing (s))
1862 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001863 if (is_ct)
1864 svm_fifo_unset_event (s->rx_fifo);
1865 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001866 return VPPCOM_EWOULDBLOCK;
1867 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001868 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001869 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001870 if (vcl_session_is_closing (s))
1871 return vcl_session_closing_error (s);
1872
Florin Corasd6894562020-10-28 00:37:15 -07001873 if (is_ct)
1874 svm_fifo_unset_event (s->rx_fifo);
1875 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001876 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001877 if (svm_msg_q_is_empty (mq))
1878 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001879
Florin Coras54693d22018-07-17 10:46:29 -07001880 svm_msg_q_sub_w_lock (mq, &msg);
1881 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001882 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001883 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001884 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001885 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001886 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001887 }
Florin Coras54693d22018-07-17 10:46:29 -07001888
Florin Coras4a2c7942020-09-10 12:27:14 -07001889read_again:
1890
Florin Coras460dce62018-07-27 05:45:06 -07001891 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001892 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001893 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001894 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1895
1896 ASSERT (rv >= 0);
1897 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001898
Sirshak Das28aa5392019-02-05 01:33:33 -06001899 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001900 {
Florin Corasd6894562020-10-28 00:37:15 -07001901 if (is_ct)
1902 svm_fifo_unset_event (s->rx_fifo);
1903 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07001904 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07001905 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07001906 {
Florin Corasc34118b2020-08-12 18:58:25 -07001907 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1908 e->event_type = SESSION_IO_EVT_RX;
1909 e->session_index = s->session_index;
1910 }
1911 }
Florin Coras54fe32c2020-11-25 20:26:32 -08001912 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07001913 {
1914 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08001915 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07001916 buf += rv;
1917 n -= rv;
1918 goto read_again;
1919 }
Florin Coras41c9e042018-09-11 00:10:41 -07001920
Florin Coras17ec5772020-08-12 20:46:29 -07001921 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07001922 {
Florin Coras17ec5772020-08-12 20:46:29 -07001923 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001924 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1925 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07001926 }
1927
Florin Coras5e062572019-03-14 19:07:51 -07001928 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1929 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001930
Florin Coras54693d22018-07-17 10:46:29 -07001931 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001932}
1933
Steven58f464e2017-10-25 12:33:12 -07001934int
Florin Coras134a9962018-08-28 11:32:04 -07001935vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001936{
Florin Coras134a9962018-08-28 11:32:04 -07001937 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001938}
1939
1940static int
Florin Coras134a9962018-08-28 11:32:04 -07001941vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001942{
Florin Coras134a9962018-08-28 11:32:04 -07001943 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001944}
1945
Florin Coras2cba8532018-09-11 16:33:36 -07001946int
1947vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07001948 vppcom_data_segment_t * ds, uint32_t n_segments,
1949 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001950{
1951 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001952 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001953 vcl_session_t *s = 0;
1954 svm_fifo_t *rx_fifo;
1955 svm_msg_q_msg_t msg;
1956 session_event_t *e;
1957 svm_msg_q_t *mq;
1958 u8 is_ct;
1959
1960 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001961 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07001962 return VPPCOM_EBADFD;
1963
Florin Coras6d0106e2019-01-29 20:11:58 -08001964 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1965 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001966
Florin Corasac422d62020-10-19 20:51:36 -07001967 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07001968 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07001969 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07001970 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001971 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07001972
Sirshak Das28aa5392019-02-05 01:33:33 -06001973 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001974 {
1975 if (is_nonblocking)
1976 {
Florin Coras62877022020-10-28 21:22:04 -07001977 if (is_ct)
1978 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001979 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001980 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001981 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001982 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001983 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001984 if (vcl_session_is_closing (s))
1985 return vcl_session_closing_error (s);
1986
Florin Coras62877022020-10-28 21:22:04 -07001987 if (is_ct)
1988 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001989 svm_fifo_unset_event (rx_fifo);
1990 svm_msg_q_lock (mq);
1991 if (svm_msg_q_is_empty (mq))
1992 svm_msg_q_wait (mq);
1993
1994 svm_msg_q_sub_w_lock (mq, &msg);
1995 e = svm_msg_q_msg_data (mq, &msg);
1996 svm_msg_q_unlock (mq);
1997 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001998 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001999 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07002000 }
2001 }
2002
Florin Corasd1cc38d2020-10-11 11:05:04 -07002003 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
2004 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
2005 if (n_read < 0)
2006 return VPPCOM_EAGAIN;
2007
2008 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
2009 {
Florin Coras62877022020-10-28 21:22:04 -07002010 if (is_ct)
2011 svm_fifo_unset_event (s->rx_fifo);
2012 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002013 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07002014 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07002015 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07002016 {
2017 session_event_t *e;
2018 vec_add2 (wrk->unhandled_evts_vector, e, 1);
2019 e->event_type = SESSION_IO_EVT_RX;
2020 e->session_index = s->session_index;
2021 }
2022 }
2023
2024 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07002025 return n_read;
2026}
2027
2028void
Florin Corasd68faf82020-09-25 15:18:13 -07002029vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002030{
2031 vcl_worker_t *wrk = vcl_worker_get_current ();
2032 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002033 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002034
2035 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002036 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002037 return;
2038
Florin Coras62877022020-10-28 21:22:04 -07002039 is_ct = vcl_session_is_ct (s);
2040 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002041
2042 ASSERT (s->rx_bytes_pending < n_bytes);
2043 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002044}
2045
Florin Coras54693d22018-07-17 10:46:29 -07002046static u8
2047vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
2048{
Florin Corasc0737e92019-03-04 14:19:39 -08002049 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04002050}
2051
Florin Coras7a2abce2020-04-05 19:25:44 +00002052always_inline u8
2053vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002054{
Florin Coras7a2abce2020-04-05 19:25:44 +00002055 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2056 if (is_dgram)
2057 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2058 else
2059 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002060}
2061
2062always_inline int
2063vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2064 size_t n, u8 is_flush, u8 is_dgram)
2065{
Florin Coras6d0106e2019-01-29 20:11:58 -08002066 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002067 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07002068 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08002069 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002070 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07002071 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002072 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002073
jiangxiaomingff31ac62019-11-21 23:34:56 +08002074 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002075 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002076
Florin Coras6c3b2182020-10-19 18:36:48 -07002077 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002078 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002079 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2080 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002081 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002082 }
2083
Florin Coras6d0106e2019-01-29 20:11:58 -08002084 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002085 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002086 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2087 s->session_index, s->vpp_handle, s->session_state,
2088 vppcom_session_state_str (s->session_state));
2089 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002090 }
2091
Florin Coras0e88e852018-09-17 22:09:02 -07002092 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002093 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002094 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002095
Florin Coras653e43f2019-03-04 10:56:23 -08002096 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002097 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002098 {
Florin Coras54693d22018-07-17 10:46:29 -07002099 if (is_nonblocking)
2100 {
Florin Coras070453d2018-08-24 17:04:27 -07002101 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002102 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002103 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002104 {
Florin Coras2d379d82019-06-28 12:45:12 -07002105 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002106 if (vcl_session_is_closing (s))
2107 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07002108 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07002109 if (svm_msg_q_is_empty (mq))
2110 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07002111
Florin Coras54693d22018-07-17 10:46:29 -07002112 svm_msg_q_sub_w_lock (mq, &msg);
2113 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07002114 svm_msg_q_unlock (mq);
2115
Florin Coras0e88e852018-09-17 22:09:02 -07002116 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07002117 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07002118 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07002119 }
Dave Wallace543852a2017-08-03 02:11:34 -04002120 }
Dave Wallace543852a2017-08-03 02:11:34 -04002121
Florin Coras653e43f2019-03-04 10:56:23 -08002122 et = SESSION_IO_EVT_TX;
2123 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002124 et = SESSION_IO_EVT_TX_FLUSH;
2125
Florin Coras7a2abce2020-04-05 19:25:44 +00002126 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002127 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002128 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002129 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002130 else
2131 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002132 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002133
Florin Coras14ed6df2019-03-06 21:13:42 -08002134 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08002135 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
2136 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002137
Florin Coras56230092020-09-02 20:52:58 -07002138 /* The underlying fifo segment can run out of memory */
2139 if (PREDICT_FALSE (n_write < 0))
2140 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002141
Florin Coras6d0106e2019-01-29 20:11:58 -08002142 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2143 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002144
Florin Coras54693d22018-07-17 10:46:29 -07002145 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002146}
2147
Florin Coras42ceddb2018-12-12 10:56:01 -08002148int
2149vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2150{
Florin Coras7a2abce2020-04-05 19:25:44 +00002151 vcl_worker_t *wrk = vcl_worker_get_current ();
2152 vcl_session_t *s;
2153
2154 s = vcl_session_get_w_handle (wrk, session_handle);
2155 if (PREDICT_FALSE (!s))
2156 return VPPCOM_EBADFD;
2157
2158 return vppcom_session_write_inline (wrk, s, buf, n,
2159 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002160}
2161
Florin Corasb0f662f2018-12-27 14:51:46 -08002162int
2163vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2164{
Florin Coras7a2abce2020-04-05 19:25:44 +00002165 vcl_worker_t *wrk = vcl_worker_get_current ();
2166 vcl_session_t *s;
2167
2168 s = vcl_session_get_w_handle (wrk, session_handle);
2169 if (PREDICT_FALSE (!s))
2170 return VPPCOM_EBADFD;
2171
2172 return vppcom_session_write_inline (wrk, s, buf, n,
2173 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002174}
2175
Florin Corasc0737e92019-03-04 14:19:39 -08002176#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002177if (PREDICT_FALSE (!_s->rx_fifo)) \
2178 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002179if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2180 { \
2181 if (!vcl_session_is_ct (_s)) \
2182 { \
2183 svm_fifo_unset_event (_s->rx_fifo); \
2184 if (svm_fifo_is_empty (_s->rx_fifo)) \
2185 break; \
2186 } \
2187 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2188 { \
Florin Coras2f630182020-08-13 00:17:51 -07002189 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002190 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2191 break; \
2192 } \
2193 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002194
Florin Coras86f04502018-09-12 16:08:01 -07002195static void
2196vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2197 unsigned long n_bits, unsigned long *read_map,
2198 unsigned long *write_map,
2199 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002200{
2201 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002202 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002203 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002204 u32 sid;
2205
2206 switch (e->event_type)
2207 {
Florin Corasc0737e92019-03-04 14:19:39 -08002208 case SESSION_IO_EVT_RX:
2209 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002210 s = vcl_session_get (wrk, sid);
2211 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002212 break;
Florin Corasb242d312020-10-26 15:35:40 -07002213 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002214 if (sid < n_bits && read_map)
2215 {
David Johnsond9818dd2018-12-14 14:53:41 -05002216 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002217 *bits_set += 1;
2218 }
2219 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002220 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002221 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002222 s = vcl_session_get (wrk, sid);
2223 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002224 break;
2225 if (sid < n_bits && write_map)
2226 {
David Johnsond9818dd2018-12-14 14:53:41 -05002227 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002228 *bits_set += 1;
2229 }
2230 break;
Florin Coras86f04502018-09-12 16:08:01 -07002231 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002232 if (!e->postponed)
2233 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2234 else
2235 s = vcl_session_get (wrk, e->session_index);
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 && read_map)
2240 {
David Johnsond9818dd2018-12-14 14:53:41 -05002241 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002242 *bits_set += 1;
2243 }
2244 break;
2245 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002246 if (!e->postponed)
2247 {
2248 connected_msg = (session_connected_msg_t *) e->data;
2249 sid = vcl_session_connected_handler (wrk, connected_msg);
2250 }
2251 else
2252 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002253 if (sid == VCL_INVALID_SESSION_INDEX)
2254 break;
2255 if (sid < n_bits && write_map)
2256 {
2257 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2258 *bits_set += 1;
2259 }
Florin Coras86f04502018-09-12 16:08:01 -07002260 break;
2261 case SESSION_CTRL_EVT_DISCONNECTED:
2262 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002263 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2264 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002265 break;
Florin Corasb242d312020-10-26 15:35:40 -07002266 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002267 if (sid < n_bits && except_map)
2268 {
David Johnsond9818dd2018-12-14 14:53:41 -05002269 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002270 *bits_set += 1;
2271 }
2272 break;
2273 case SESSION_CTRL_EVT_RESET:
2274 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2275 if (sid < n_bits && except_map)
2276 {
David Johnsond9818dd2018-12-14 14:53:41 -05002277 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002278 *bits_set += 1;
2279 }
2280 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002281 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2282 vcl_session_unlisten_reply_handler (wrk, e->data);
2283 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002284 case SESSION_CTRL_EVT_MIGRATED:
2285 vcl_session_migrated_handler (wrk, e->data);
2286 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002287 case SESSION_CTRL_EVT_CLEANUP:
2288 vcl_session_cleanup_handler (wrk, e->data);
2289 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002290 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2291 vcl_session_worker_update_reply_handler (wrk, e->data);
2292 break;
2293 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2294 vcl_session_req_worker_update_handler (wrk, e->data);
2295 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002296 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2297 vcl_session_app_add_segment_handler (wrk, e->data);
2298 break;
2299 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2300 vcl_session_app_del_segment_handler (wrk, e->data);
2301 break;
hanlina3a48962020-07-13 11:09:15 +08002302 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002303 vcl_worker_rpc_handler (wrk, e->data);
2304 break;
Florin Coras86f04502018-09-12 16:08:01 -07002305 default:
2306 clib_warning ("unhandled: %u", e->event_type);
2307 break;
2308 }
2309}
2310
2311static int
2312vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2313 unsigned long n_bits, unsigned long *read_map,
2314 unsigned long *write_map, unsigned long *except_map,
2315 double time_to_wait, u32 * bits_set)
2316{
Florin Coras99368312018-08-02 10:45:44 -07002317 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002318 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002319 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002320
2321 svm_msg_q_lock (mq);
2322 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002323 {
Florin Coras54693d22018-07-17 10:46:29 -07002324 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002325 {
Florin Coras54693d22018-07-17 10:46:29 -07002326 svm_msg_q_unlock (mq);
2327 return 0;
2328 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002329
Florin Coras54693d22018-07-17 10:46:29 -07002330 if (!time_to_wait)
2331 {
2332 svm_msg_q_unlock (mq);
2333 return 0;
2334 }
2335 else if (time_to_wait < 0)
2336 {
2337 svm_msg_q_wait (mq);
2338 }
2339 else
2340 {
2341 if (svm_msg_q_timedwait (mq, time_to_wait))
2342 {
2343 svm_msg_q_unlock (mq);
2344 return 0;
2345 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002346 }
2347 }
Florin Corase003a1b2019-06-05 10:47:16 -07002348 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002349 svm_msg_q_unlock (mq);
2350
Florin Coras134a9962018-08-28 11:32:04 -07002351 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002352 {
Florin Coras134a9962018-08-28 11:32:04 -07002353 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002354 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002355 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2356 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002357 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002358 }
Florin Coras134a9962018-08-28 11:32:04 -07002359 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002360 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002361 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002362}
2363
Florin Coras99368312018-08-02 10:45:44 -07002364static int
Florin Coras294afe22019-01-07 17:49:17 -08002365vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2366 vcl_si_set * read_map, vcl_si_set * write_map,
2367 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002368 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002369{
Florin Coras14ed6df2019-03-06 21:13:42 -08002370 double wait = 0, start = 0;
2371
2372 if (!*bits_set)
2373 {
2374 wait = time_to_wait;
2375 start = clib_time_now (&wrk->clib_time);
2376 }
2377
2378 do
2379 {
2380 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2381 write_map, except_map, wait, bits_set);
2382 if (*bits_set)
2383 return *bits_set;
2384 if (wait == -1)
2385 continue;
2386
2387 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2388 }
2389 while (wait > 0);
2390
2391 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002392}
2393
2394static int
Florin Coras294afe22019-01-07 17:49:17 -08002395vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2396 vcl_si_set * read_map, vcl_si_set * write_map,
2397 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002398 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002399{
2400 vcl_mq_evt_conn_t *mqc;
2401 int __clib_unused n_read;
2402 int n_mq_evts, i;
2403 u64 buf;
2404
Florin Coras134a9962018-08-28 11:32:04 -07002405 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2406 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2407 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002408 for (i = 0; i < n_mq_evts; i++)
2409 {
Florin Coras134a9962018-08-28 11:32:04 -07002410 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002411 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002412 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002413 except_map, 0, bits_set);
2414 }
2415
2416 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2417}
2418
Dave Wallace543852a2017-08-03 02:11:34 -04002419int
Florin Coras294afe22019-01-07 17:49:17 -08002420vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2421 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002422{
Florin Coras54693d22018-07-17 10:46:29 -07002423 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002424 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002425 vcl_session_t *session = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002426 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002427
Dave Wallace7876d392017-10-19 03:53:57 -04002428 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002429 {
Florin Coras134a9962018-08-28 11:32:04 -07002430 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002431 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002432 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2433 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002434 }
Dave Wallace7876d392017-10-19 03:53:57 -04002435 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002436 {
Florin Coras134a9962018-08-28 11:32:04 -07002437 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002438 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002439 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2440 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002441 }
Dave Wallace7876d392017-10-19 03:53:57 -04002442 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002443 {
Florin Coras134a9962018-08-28 11:32:04 -07002444 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002445 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002446 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2447 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002448 }
2449
Florin Coras54693d22018-07-17 10:46:29 -07002450 if (!n_bits)
2451 return 0;
2452
2453 if (!write_map)
2454 goto check_rd;
2455
2456 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +01002457 clib_bitmap_foreach (sid, wrk->wr_bitmap) {
Florin Coras134a9962018-08-28 11:32:04 -07002458 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002459 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002460 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2461 bits_set++;
2462 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002463 }
2464
Florin Coras5e6222a2020-04-24 17:09:25 +00002465 if (vcl_session_write_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002466 {
David Johnsond9818dd2018-12-14 14:53:41 -05002467 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002468 bits_set++;
2469 }
Florin Coras294afe22019-01-07 17:49:17 -08002470 else
Florin Coras2d379d82019-06-28 12:45:12 -07002471 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Damjan Marionf0ca1e82020-12-13 23:26:56 +01002472 }
Florin Coras54693d22018-07-17 10:46:29 -07002473
2474check_rd:
2475 if (!read_map)
2476 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002477
Damjan Marionf0ca1e82020-12-13 23:26:56 +01002478 clib_bitmap_foreach (sid, wrk->rd_bitmap) {
Florin Coras134a9962018-08-28 11:32:04 -07002479 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002480 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002481 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2482 bits_set++;
2483 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002484 }
2485
Florin Coras5e6222a2020-04-24 17:09:25 +00002486 if (vcl_session_read_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002487 {
David Johnsond9818dd2018-12-14 14:53:41 -05002488 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002489 bits_set++;
2490 }
Damjan Marionf0ca1e82020-12-13 23:26:56 +01002491 }
Florin Coras54693d22018-07-17 10:46:29 -07002492 /* *INDENT-ON* */
2493
2494check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002495
Florin Coras86f04502018-09-12 16:08:01 -07002496 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2497 {
2498 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2499 read_map, write_map, except_map, &bits_set);
2500 }
2501 vec_reset_length (wrk->unhandled_evts_vector);
2502
Florin Coras99368312018-08-02 10:45:44 -07002503 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002504 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002505 time_to_wait, &bits_set);
2506 else
Florin Coras134a9962018-08-28 11:32:04 -07002507 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002508 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002509
Dave Wallace543852a2017-08-03 02:11:34 -04002510 return (bits_set);
2511}
2512
Dave Wallacef7f809c2017-10-03 01:48:42 -04002513static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002514vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002515{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002516 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002517 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002518 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002519
Florin Corasc0737e92019-03-04 14:19:39 -08002520 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002521 return;
2522
Florin Coras6c3b2182020-10-19 18:36:48 -07002523 s = vcl_session_get_w_handle (wrk, vep_handle);
2524 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002525 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002526 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002527 goto done;
2528 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002529 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002530 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002531 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002532 goto done;
2533 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002534 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002535 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002536 "{\n"
2537 " is_vep = %u\n"
2538 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002539 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002540 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2541 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002542
Florin Coras7e5e62b2019-07-30 14:08:23 -07002543 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002544 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002545 s = vcl_session_get_w_handle (wrk, sh);
2546 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002547 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002548 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002549 goto done;
2550 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002551 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002552 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002553 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002554 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002555 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002556 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002557 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002558 goto done;
2559 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002560 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002561 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2562 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002563 sh, s->vep.vep_sh, vep_handle);
2564 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002565 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002566 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002567 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002568 " next_sh = 0x%x (%u)\n"
2569 " prev_sh = 0x%x (%u)\n"
2570 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002571 " ev.events = 0x%x\n"
2572 " ev.data.u64 = 0x%llx\n"
2573 " et_mask = 0x%x\n"
2574 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002575 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002576 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2577 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002578 }
2579 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002580
2581done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002582 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002583}
2584
2585int
2586vppcom_epoll_create (void)
2587{
Florin Coras134a9962018-08-28 11:32:04 -07002588 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002589 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002590
Florin Coras134a9962018-08-28 11:32:04 -07002591 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002592
Florin Coras6c3b2182020-10-19 18:36:48 -07002593 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002594 vep_session->vep.vep_sh = ~0;
2595 vep_session->vep.next_sh = ~0;
2596 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002597 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002598
Florin Corasa7a1a222018-12-30 17:11:31 -08002599 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2600 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002601
Florin Corasab2f6db2018-08-31 14:31:41 -07002602 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002603}
2604
2605int
Florin Coras134a9962018-08-28 11:32:04 -07002606vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002607 struct epoll_event *event)
2608{
Florin Coras134a9962018-08-28 11:32:04 -07002609 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002610 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002611 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002612 vcl_session_t *s;
2613 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002614
Florin Coras134a9962018-08-28 11:32:04 -07002615 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002616 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002617 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002618 return VPPCOM_EINVAL;
2619 }
2620
Florin Coras134a9962018-08-28 11:32:04 -07002621 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002622 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002623 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002624 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002625 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002626 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002627 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002628 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002629 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002630 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002631 }
2632
Florin Coras134a9962018-08-28 11:32:04 -07002633 ASSERT (vep_session->vep.vep_sh == ~0);
2634 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002635
Florin Coras17ec5772020-08-12 20:46:29 -07002636 s = vcl_session_get_w_handle (wrk, session_handle);
2637 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002638 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002639 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002640 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002641 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002642 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002643 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002644 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002645 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002646 }
2647
2648 switch (op)
2649 {
2650 case EPOLL_CTL_ADD:
2651 if (PREDICT_FALSE (!event))
2652 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002653 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002654 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002655 }
Florin Coras134a9962018-08-28 11:32:04 -07002656 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002657 {
Florin Coras7e12d942018-06-27 14:32:43 -07002658 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002659 next_session = vcl_session_get_w_handle (wrk,
2660 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002661 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002662 {
Florin Coras5e062572019-03-14 19:07:51 -07002663 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002664 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002665 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002666 }
Florin Coras134a9962018-08-28 11:32:04 -07002667 ASSERT (next_session->vep.prev_sh == vep_handle);
2668 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002669 }
Florin Coras17ec5772020-08-12 20:46:29 -07002670 s->vep.next_sh = vep_session->vep.next_sh;
2671 s->vep.prev_sh = vep_handle;
2672 s->vep.vep_sh = vep_handle;
2673 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2674 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002675 s->flags &= ~VCL_SESSION_F_IS_VEP;
2676 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002677 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002678
Florin Coras17ec5772020-08-12 20:46:29 -07002679 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2680 if (txf && (event->events & EPOLLOUT))
2681 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002682
Florin Coras6e3c1f82020-01-15 01:30:46 +00002683 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002684 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002685 {
2686 session_event_t e = { 0 };
2687 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002688 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002689 vec_add1 (wrk->unhandled_evts_vector, e);
2690 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002691 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002692 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002693 {
2694 session_event_t e = { 0 };
2695 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002696 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002697 vec_add1 (wrk->unhandled_evts_vector, e);
2698 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002699 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2700 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002701 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002702 break;
2703
2704 case EPOLL_CTL_MOD:
2705 if (PREDICT_FALSE (!event))
2706 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002707 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002708 rv = VPPCOM_EINVAL;
2709 goto done;
2710 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002711 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002712 {
Florin Coras5e062572019-03-14 19:07:51 -07002713 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002714 rv = VPPCOM_EINVAL;
2715 goto done;
2716 }
Florin Coras17ec5772020-08-12 20:46:29 -07002717 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002718 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002719 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002720 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002721 rv = VPPCOM_EINVAL;
2722 goto done;
2723 }
hanlin475c9d72019-12-26 11:44:28 +08002724
2725 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2726 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002727 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002728 {
2729 session_event_t e = { 0 };
2730 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002731 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002732 vec_add1 (wrk->unhandled_evts_vector, e);
2733 }
Florin Coras17ec5772020-08-12 20:46:29 -07002734 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2735 s->vep.ev = *event;
2736 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2737 if (event->events & EPOLLOUT)
2738 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2739 else
2740 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasa7a1a222018-12-30 17:11:31 -08002741 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2742 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002743 break;
2744
2745 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002746 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002747 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002748 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002749 rv = VPPCOM_EINVAL;
2750 goto done;
2751 }
Florin Coras17ec5772020-08-12 20:46:29 -07002752 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002753 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002754 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002755 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002756 rv = VPPCOM_EINVAL;
2757 goto done;
2758 }
2759
Florin Coras17ec5772020-08-12 20:46:29 -07002760 if (s->vep.prev_sh == vep_handle)
2761 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002762 else
2763 {
Florin Coras7e12d942018-06-27 14:32:43 -07002764 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002765 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002766 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002767 {
Florin Coras5e062572019-03-14 19:07:51 -07002768 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002769 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002770 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002771 }
Florin Coras134a9962018-08-28 11:32:04 -07002772 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002773 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002774 }
Florin Coras17ec5772020-08-12 20:46:29 -07002775 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776 {
Florin Coras7e12d942018-06-27 14:32:43 -07002777 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002778 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002779 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002780 {
Florin Coras5e062572019-03-14 19:07:51 -07002781 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002782 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002783 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002784 }
Florin Coras134a9962018-08-28 11:32:04 -07002785 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002786 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002787 }
2788
Florin Coras17ec5772020-08-12 20:46:29 -07002789 memset (&s->vep, 0, sizeof (s->vep));
2790 s->vep.next_sh = ~0;
2791 s->vep.prev_sh = ~0;
2792 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002793 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002794
Florin Coras17ec5772020-08-12 20:46:29 -07002795 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2796 if (txf)
2797 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002798
Florin Coras5e062572019-03-14 19:07:51 -07002799 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002800 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002801 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002802 break;
2803
2804 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002805 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002806 rv = VPPCOM_EINVAL;
2807 }
2808
Florin Coras134a9962018-08-28 11:32:04 -07002809 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002810
2811done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002812 return rv;
2813}
2814
Florin Coras86f04502018-09-12 16:08:01 -07002815static inline void
2816vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2817 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002818{
2819 session_disconnected_msg_t *disconnected_msg;
2820 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002821 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002822 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002823 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002824 u8 add_event = 0;
2825
2826 switch (e->event_type)
2827 {
Florin Coras653e43f2019-03-04 10:56:23 -08002828 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002829 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002830 s = vcl_session_get (wrk, sid);
2831 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002832 break;
Florin Corasb242d312020-10-26 15:35:40 -07002833 vcl_fifo_rx_evt_valid_or_break (s);
2834 session_events = s->vep.ev.events;
2835 if (!(EPOLLIN & s->vep.ev.events)
2836 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002837 break;
2838 add_event = 1;
2839 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002840 session_evt_data = s->vep.ev.data.u64;
2841 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002842 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002843 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002844 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002845 s = vcl_session_get (wrk, sid);
2846 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002847 break;
Florin Corasb242d312020-10-26 15:35:40 -07002848 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002849 if (!(EPOLLOUT & session_events))
2850 break;
2851 add_event = 1;
2852 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002853 session_evt_data = s->vep.ev.data.u64;
2854 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2855 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002856 break;
Florin Coras86f04502018-09-12 16:08:01 -07002857 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002858 if (!e->postponed)
2859 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2860 else
2861 s = vcl_session_get (wrk, e->session_index);
2862 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002863 break;
Florin Corasb242d312020-10-26 15:35:40 -07002864 session_events = s->vep.ev.events;
2865 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002866 if (!(EPOLLIN & session_events))
2867 break;
Florin Coras86f04502018-09-12 16:08:01 -07002868 add_event = 1;
2869 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002870 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002871 break;
2872 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002873 if (!e->postponed)
2874 {
2875 connected_msg = (session_connected_msg_t *) e->data;
2876 sid = vcl_session_connected_handler (wrk, connected_msg);
2877 }
2878 else
2879 sid = e->session_index;
2880 s = vcl_session_get (wrk, sid);
2881 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002882 break;
Florin Corasb242d312020-10-26 15:35:40 -07002883 session_events = s->vep.ev.events;
2884 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002885 if (!(EPOLLOUT & session_events))
2886 break;
2887 add_event = 1;
2888 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002889 session_evt_data = s->vep.ev.data.u64;
2890 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002891 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002892 break;
2893 case SESSION_CTRL_EVT_DISCONNECTED:
2894 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002895 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2896 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002897 break;
Florin Corasb242d312020-10-26 15:35:40 -07002898 sid = s->session_index;
2899 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002900 add_event = 1;
2901 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002902 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002903 break;
2904 case SESSION_CTRL_EVT_RESET:
2905 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002906 s = vcl_session_get (wrk, sid);
2907 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002908 break;
Florin Corasb242d312020-10-26 15:35:40 -07002909 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002910 add_event = 1;
2911 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002912 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002913 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002914 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2915 vcl_session_unlisten_reply_handler (wrk, e->data);
2916 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002917 case SESSION_CTRL_EVT_MIGRATED:
2918 vcl_session_migrated_handler (wrk, e->data);
2919 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002920 case SESSION_CTRL_EVT_CLEANUP:
2921 vcl_session_cleanup_handler (wrk, e->data);
2922 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002923 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2924 vcl_session_req_worker_update_handler (wrk, e->data);
2925 break;
2926 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2927 vcl_session_worker_update_reply_handler (wrk, e->data);
2928 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002929 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2930 vcl_session_app_add_segment_handler (wrk, e->data);
2931 break;
2932 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2933 vcl_session_app_del_segment_handler (wrk, e->data);
2934 break;
hanlina3a48962020-07-13 11:09:15 +08002935 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002936 vcl_worker_rpc_handler (wrk, e->data);
2937 break;
Florin Coras86f04502018-09-12 16:08:01 -07002938 default:
2939 VDBG (0, "unhandled: %u", e->event_type);
2940 break;
2941 }
2942
2943 if (add_event)
2944 {
2945 events[*num_ev].data.u64 = session_evt_data;
2946 if (EPOLLONESHOT & session_events)
2947 {
Florin Corasb242d312020-10-26 15:35:40 -07002948 s = vcl_session_get (wrk, sid);
2949 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002950 }
2951 *num_ev += 1;
2952 }
2953}
2954
2955static int
2956vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2957 struct epoll_event *events, u32 maxevents,
2958 double wait_for_time, u32 * num_ev)
2959{
Florin Coras99368312018-08-02 10:45:44 -07002960 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002961 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002962 int i;
2963
Florin Coras539663c2018-09-28 14:59:37 -07002964 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2965 goto handle_dequeued;
2966
Florin Coras54693d22018-07-17 10:46:29 -07002967 svm_msg_q_lock (mq);
2968 if (svm_msg_q_is_empty (mq))
2969 {
2970 if (!wait_for_time)
2971 {
2972 svm_msg_q_unlock (mq);
2973 return 0;
2974 }
2975 else if (wait_for_time < 0)
2976 {
2977 svm_msg_q_wait (mq);
2978 }
2979 else
2980 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002981 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002982 {
2983 svm_msg_q_unlock (mq);
2984 return 0;
2985 }
2986 }
2987 }
Florin Corase003a1b2019-06-05 10:47:16 -07002988 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08002989 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002990 svm_msg_q_unlock (mq);
2991
Florin Coras539663c2018-09-28 14:59:37 -07002992handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002993 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002994 {
Florin Coras134a9962018-08-28 11:32:04 -07002995 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002996 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08002997 if (*num_ev < maxevents)
2998 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2999 else
3000 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07003001 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07003002 }
Florin Corasaa27eb92018-10-13 12:20:01 -07003003 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08003004 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07003005 return *num_ev;
3006}
3007
Florin Coras99368312018-08-02 10:45:44 -07003008static int
Florin Coras134a9962018-08-28 11:32:04 -07003009vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003010 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003011{
Florin Corase003a1b2019-06-05 10:47:16 -07003012 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003013
3014 if (!n_evts)
3015 {
3016 wait = wait_for_time;
3017 start = clib_time_now (&wrk->clib_time);
3018 }
3019
3020 do
3021 {
3022 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
3023 wait, &n_evts);
3024 if (n_evts)
3025 return n_evts;
3026 if (wait == -1)
3027 continue;
3028
Florin Corase003a1b2019-06-05 10:47:16 -07003029 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07003030 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07003031 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003032 }
3033 while (wait > 0);
3034
3035 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003036}
3037
3038static int
Florin Coras134a9962018-08-28 11:32:04 -07003039vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003040 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003041{
3042 vcl_mq_evt_conn_t *mqc;
3043 int __clib_unused n_read;
3044 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07003045 u64 buf;
3046
Florin Coras134a9962018-08-28 11:32:04 -07003047 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07003048again:
Florin Coras134a9962018-08-28 11:32:04 -07003049 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
3050 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07003051 for (i = 0; i < n_mq_evts; i++)
3052 {
Florin Coras134a9962018-08-28 11:32:04 -07003053 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07003054 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07003055 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07003056 }
Florin Corasa4878ed2018-10-12 13:09:36 -07003057 if (!n_evts && n_mq_evts > 0)
3058 goto again;
Florin Coras99368312018-08-02 10:45:44 -07003059
3060 return (int) n_evts;
3061}
3062
Dave Wallacef7f809c2017-10-03 01:48:42 -04003063int
Florin Coras134a9962018-08-28 11:32:04 -07003064vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003065 int maxevents, double wait_for_time)
3066{
Florin Coras134a9962018-08-28 11:32:04 -07003067 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003068 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003069 u32 n_evts = 0;
3070 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003071
3072 if (PREDICT_FALSE (maxevents <= 0))
3073 {
Florin Coras5e062572019-03-14 19:07:51 -07003074 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003075 return VPPCOM_EINVAL;
3076 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003077
Florin Coras134a9962018-08-28 11:32:04 -07003078 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003079 if (!vep_session)
3080 return VPPCOM_EBADFD;
3081
Florin Coras6c3b2182020-10-19 18:36:48 -07003082 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003083 {
Florin Coras5e062572019-03-14 19:07:51 -07003084 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003085 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003086 }
Florin Coras54693d22018-07-17 10:46:29 -07003087
3088 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003089
Florin Coras86f04502018-09-12 16:08:01 -07003090 if (vec_len (wrk->unhandled_evts_vector))
3091 {
3092 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3093 {
3094 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3095 events, &n_evts);
3096 if (n_evts == maxevents)
3097 {
Florin Corase003a1b2019-06-05 10:47:16 -07003098 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3099 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003100 }
3101 }
Florin Corase003a1b2019-06-05 10:47:16 -07003102 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003103 }
3104
3105 if (vcm->cfg.use_mq_eventfd)
3106 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3107 wait_for_time);
3108
3109 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3110 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003111}
3112
Dave Wallace35830af2017-10-09 01:43:42 -04003113int
Florin Coras134a9962018-08-28 11:32:04 -07003114vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003115 void *buffer, uint32_t * buflen)
3116{
Florin Coras134a9962018-08-28 11:32:04 -07003117 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003118 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04003119 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08003120 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003121 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003122
Florin Coras134a9962018-08-28 11:32:04 -07003123 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003124 if (!session)
3125 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003126
Dave Wallace35830af2017-10-09 01:43:42 -04003127 switch (op)
3128 {
3129 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003130 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003131 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3132 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003133 break;
3134
Dave Wallace227867f2017-11-13 21:21:53 -05003135 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003136 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003137 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3138 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003139 break;
3140
3141 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003142 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003143 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003144 *flags =
3145 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003146 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003147 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003148 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003149 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3150 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003151 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003152 }
3153 else
3154 rv = VPPCOM_EINVAL;
3155 break;
3156
3157 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003158 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003159 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003160 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003161 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003162 else
Florin Corasac422d62020-10-19 20:51:36 -07003163 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003164
Florin Coras5e062572019-03-14 19:07:51 -07003165 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3166 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003167 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003168 }
3169 else
3170 rv = VPPCOM_EINVAL;
3171 break;
3172
3173 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003174 if (PREDICT_TRUE (buffer && buflen &&
3175 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003176 {
Florin Coras7e12d942018-06-27 14:32:43 -07003177 ep->is_ip4 = session->transport.is_ip4;
3178 ep->port = session->transport.rmt_port;
3179 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003180 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3181 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003182 else
Dave Barach178cf492018-11-13 16:34:13 -05003183 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3184 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003185 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003186 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3187 "addr = %U, port %u", session_handle, ep->is_ip4,
3188 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003189 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3190 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003191 }
3192 else
3193 rv = VPPCOM_EINVAL;
3194 break;
3195
3196 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003197 if (PREDICT_TRUE (buffer && buflen &&
3198 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003199 {
Florin Coras7e12d942018-06-27 14:32:43 -07003200 ep->is_ip4 = session->transport.is_ip4;
3201 ep->port = session->transport.lcl_port;
3202 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003203 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3204 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003205 else
Dave Barach178cf492018-11-13 16:34:13 -05003206 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3207 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003208 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003209 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3210 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003211 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003212 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3213 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003214 }
3215 else
3216 rv = VPPCOM_EINVAL;
3217 break;
Stevenb5a11602017-10-11 09:59:30 -07003218
Florin Corasef7cbf62019-10-17 09:56:27 -07003219 case VPPCOM_ATTR_SET_LCL_ADDR:
3220 if (PREDICT_TRUE (buffer && buflen &&
3221 (*buflen >= sizeof (*ep)) && ep->ip))
3222 {
3223 session->transport.is_ip4 = ep->is_ip4;
3224 session->transport.lcl_port = ep->port;
3225 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3226 *buflen = sizeof (*ep);
3227 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3228 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3229 &session->transport.lcl_ip,
3230 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3231 clib_net_to_host_u16 (ep->port));
3232 }
3233 else
3234 rv = VPPCOM_EINVAL;
3235 break;
3236
Dave Wallace048b1d62018-01-03 22:24:41 -05003237 case VPPCOM_ATTR_GET_LIBC_EPFD:
3238 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003239 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003240 break;
3241
3242 case VPPCOM_ATTR_SET_LIBC_EPFD:
3243 if (PREDICT_TRUE (buffer && buflen &&
3244 (*buflen == sizeof (session->libc_epfd))))
3245 {
3246 session->libc_epfd = *(int *) buffer;
3247 *buflen = sizeof (session->libc_epfd);
3248
Florin Coras5e062572019-03-14 19:07:51 -07003249 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3250 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003251 }
3252 else
3253 rv = VPPCOM_EINVAL;
3254 break;
3255
3256 case VPPCOM_ATTR_GET_PROTOCOL:
3257 if (buffer && buflen && (*buflen >= sizeof (int)))
3258 {
Florin Coras7e12d942018-06-27 14:32:43 -07003259 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003260 *buflen = sizeof (int);
3261
Florin Coras5e062572019-03-14 19:07:51 -07003262 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3263 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003264 }
3265 else
3266 rv = VPPCOM_EINVAL;
3267 break;
3268
3269 case VPPCOM_ATTR_GET_LISTEN:
3270 if (buffer && buflen && (*buflen >= sizeof (int)))
3271 {
Florin Corasac422d62020-10-19 20:51:36 -07003272 *(int *) buffer = vcl_session_has_attr (session,
3273 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003274 *buflen = sizeof (int);
3275
Florin Coras5e062572019-03-14 19:07:51 -07003276 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3277 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003278 }
3279 else
3280 rv = VPPCOM_EINVAL;
3281 break;
3282
3283 case VPPCOM_ATTR_GET_ERROR:
3284 if (buffer && buflen && (*buflen >= sizeof (int)))
3285 {
3286 *(int *) buffer = 0;
3287 *buflen = sizeof (int);
3288
Florin Coras5e062572019-03-14 19:07:51 -07003289 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3290 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003291 }
3292 else
3293 rv = VPPCOM_EINVAL;
3294 break;
3295
3296 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3297 if (buffer && buflen && (*buflen >= sizeof (u32)))
3298 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003299
3300 /* VPP-TBD */
3301 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003302 session->tx_fifo ?
3303 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003304 vcm->cfg.tx_fifo_size);
3305 *buflen = sizeof (u32);
3306
Florin Coras5e062572019-03-14 19:07:51 -07003307 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3308 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3309 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003310 }
3311 else
3312 rv = VPPCOM_EINVAL;
3313 break;
3314
3315 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3316 if (buffer && buflen && (*buflen == sizeof (u32)))
3317 {
3318 /* VPP-TBD */
3319 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003320 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3321 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3322 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003323 }
3324 else
3325 rv = VPPCOM_EINVAL;
3326 break;
3327
3328 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3329 if (buffer && buflen && (*buflen >= sizeof (u32)))
3330 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003331
3332 /* VPP-TBD */
3333 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003334 session->rx_fifo ?
3335 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003336 vcm->cfg.rx_fifo_size);
3337 *buflen = sizeof (u32);
3338
Florin Coras5e062572019-03-14 19:07:51 -07003339 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3340 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003341 }
3342 else
3343 rv = VPPCOM_EINVAL;
3344 break;
3345
3346 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3347 if (buffer && buflen && (*buflen == sizeof (u32)))
3348 {
3349 /* VPP-TBD */
3350 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003351 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3352 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3353 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003354 }
3355 else
3356 rv = VPPCOM_EINVAL;
3357 break;
3358
3359 case VPPCOM_ATTR_GET_REUSEADDR:
3360 if (buffer && buflen && (*buflen >= sizeof (int)))
3361 {
3362 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003363 *(int *) buffer = vcl_session_has_attr (session,
3364 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003365 *buflen = sizeof (int);
3366
Florin Coras5e062572019-03-14 19:07:51 -07003367 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3368 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003369 }
3370 else
3371 rv = VPPCOM_EINVAL;
3372 break;
3373
Stevenb5a11602017-10-11 09:59:30 -07003374 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003375 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003376 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003377 {
3378 /* VPP-TBD */
3379 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003380 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003381 else
Florin Corasac422d62020-10-19 20:51:36 -07003382 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003383
Florin Coras5e062572019-03-14 19:07:51 -07003384 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003385 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003386 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003387 }
3388 else
3389 rv = VPPCOM_EINVAL;
3390 break;
3391
3392 case VPPCOM_ATTR_GET_REUSEPORT:
3393 if (buffer && buflen && (*buflen >= sizeof (int)))
3394 {
3395 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003396 *(int *) buffer = vcl_session_has_attr (session,
3397 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003398 *buflen = sizeof (int);
3399
Florin Coras5e062572019-03-14 19:07:51 -07003400 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3401 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003402 }
3403 else
3404 rv = VPPCOM_EINVAL;
3405 break;
3406
3407 case VPPCOM_ATTR_SET_REUSEPORT:
3408 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003409 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003410 {
3411 /* VPP-TBD */
3412 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003413 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003414 else
Florin Corasac422d62020-10-19 20:51:36 -07003415 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003416
Florin Coras5e062572019-03-14 19:07:51 -07003417 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003418 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003419 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003420 }
3421 else
3422 rv = VPPCOM_EINVAL;
3423 break;
3424
3425 case VPPCOM_ATTR_GET_BROADCAST:
3426 if (buffer && buflen && (*buflen >= sizeof (int)))
3427 {
3428 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003429 *(int *) buffer = vcl_session_has_attr (session,
3430 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003431 *buflen = sizeof (int);
3432
Florin Coras5e062572019-03-14 19:07:51 -07003433 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3434 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003435 }
3436 else
3437 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003438 break;
3439
3440 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003441 if (buffer && buflen && (*buflen == sizeof (int)))
3442 {
3443 /* VPP-TBD */
3444 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003445 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003446 else
Florin Corasac422d62020-10-19 20:51:36 -07003447 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003448
Florin Coras5e062572019-03-14 19:07:51 -07003449 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003450 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003451 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003452 }
3453 else
3454 rv = VPPCOM_EINVAL;
3455 break;
3456
3457 case VPPCOM_ATTR_GET_V6ONLY:
3458 if (buffer && buflen && (*buflen >= sizeof (int)))
3459 {
3460 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003461 *(int *) buffer = vcl_session_has_attr (session,
3462 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003463 *buflen = sizeof (int);
3464
Florin Coras5e062572019-03-14 19:07:51 -07003465 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3466 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003467 }
3468 else
3469 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003470 break;
3471
3472 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003473 if (buffer && buflen && (*buflen == sizeof (int)))
3474 {
3475 /* VPP-TBD */
3476 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003477 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003478 else
Florin Corasac422d62020-10-19 20:51:36 -07003479 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003480
Florin Coras5e062572019-03-14 19:07:51 -07003481 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003482 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003483 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003484 }
3485 else
3486 rv = VPPCOM_EINVAL;
3487 break;
3488
3489 case VPPCOM_ATTR_GET_KEEPALIVE:
3490 if (buffer && buflen && (*buflen >= sizeof (int)))
3491 {
3492 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003493 *(int *) buffer = vcl_session_has_attr (session,
3494 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003495 *buflen = sizeof (int);
3496
Florin Coras5e062572019-03-14 19:07:51 -07003497 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3498 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003499 }
3500 else
3501 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003502 break;
Stevenbccd3392017-10-12 20:42:21 -07003503
3504 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003505 if (buffer && buflen && (*buflen == sizeof (int)))
3506 {
3507 /* VPP-TBD */
3508 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003509 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003510 else
Florin Corasac422d62020-10-19 20:51:36 -07003511 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003512
Florin Coras5e062572019-03-14 19:07:51 -07003513 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003514 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003515 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003516 }
3517 else
3518 rv = VPPCOM_EINVAL;
3519 break;
3520
3521 case VPPCOM_ATTR_GET_TCP_NODELAY:
3522 if (buffer && buflen && (*buflen >= sizeof (int)))
3523 {
3524 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003525 *(int *) buffer = vcl_session_has_attr (session,
3526 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003527 *buflen = sizeof (int);
3528
Florin Coras5e062572019-03-14 19:07:51 -07003529 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3530 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003531 }
3532 else
3533 rv = VPPCOM_EINVAL;
3534 break;
3535
3536 case VPPCOM_ATTR_SET_TCP_NODELAY:
3537 if (buffer && buflen && (*buflen == sizeof (int)))
3538 {
3539 /* VPP-TBD */
3540 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003541 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003542 else
Florin Corasac422d62020-10-19 20:51:36 -07003543 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003544
Florin Coras5e062572019-03-14 19:07:51 -07003545 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003546 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003547 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003548 }
3549 else
3550 rv = VPPCOM_EINVAL;
3551 break;
3552
3553 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3554 if (buffer && buflen && (*buflen >= sizeof (int)))
3555 {
3556 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003557 *(int *) buffer = vcl_session_has_attr (session,
3558 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003559 *buflen = sizeof (int);
3560
Florin Coras5e062572019-03-14 19:07:51 -07003561 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3562 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003563 }
3564 else
3565 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003566 break;
3567
3568 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003569 if (buffer && buflen && (*buflen == sizeof (int)))
3570 {
3571 /* VPP-TBD */
3572 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003573 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003574 else
Florin Corasac422d62020-10-19 20:51:36 -07003575 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003576
Florin Coras5e062572019-03-14 19:07:51 -07003577 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003578 vcl_session_has_attr (session,
3579 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003580 }
3581 else
3582 rv = VPPCOM_EINVAL;
3583 break;
3584
3585 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3586 if (buffer && buflen && (*buflen >= sizeof (int)))
3587 {
3588 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003589 *(int *) buffer = vcl_session_has_attr (session,
3590 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003591 *buflen = sizeof (int);
3592
Florin Coras5e062572019-03-14 19:07:51 -07003593 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3594 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003595 }
3596 else
3597 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003598 break;
3599
3600 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003601 if (buffer && buflen && (*buflen == sizeof (int)))
3602 {
3603 /* VPP-TBD */
3604 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003605 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003606 else
Florin Corasac422d62020-10-19 20:51:36 -07003607 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003608
Florin Coras5e062572019-03-14 19:07:51 -07003609 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003610 vcl_session_has_attr (session,
3611 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003612 }
3613 else
3614 rv = VPPCOM_EINVAL;
3615 break;
3616
3617 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3618 if (buffer && buflen && (*buflen >= sizeof (u32)))
3619 {
3620 /* VPP-TBD */
3621 *(u32 *) buffer = session->user_mss;
3622 *buflen = sizeof (int);
3623
Florin Coras5e062572019-03-14 19:07:51 -07003624 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3625 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003626 }
3627 else
3628 rv = VPPCOM_EINVAL;
3629 break;
3630
3631 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3632 if (buffer && buflen && (*buflen == sizeof (u32)))
3633 {
3634 /* VPP-TBD */
3635 session->user_mss = *(u32 *) buffer;
3636
Florin Coras5e062572019-03-14 19:07:51 -07003637 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3638 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003639 }
3640 else
3641 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003642 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003643
Florin Coras7baeb712019-01-04 17:05:43 -08003644 case VPPCOM_ATTR_SET_SHUT:
3645 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003646 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003647 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003648 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003649 break;
3650
3651 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003652 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003653 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003654 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003655 tmp_flags |= 2;
3656 if (tmp_flags == 1)
3657 *(int *) buffer = SHUT_RD;
3658 else if (tmp_flags == 2)
3659 *(int *) buffer = SHUT_WR;
3660 else if (tmp_flags == 3)
3661 *(int *) buffer = SHUT_RDWR;
3662 *buflen = sizeof (int);
3663 break;
Florin Coras1e966172020-05-16 18:18:14 +00003664
3665 case VPPCOM_ATTR_SET_CONNECTED:
3666 session->flags |= VCL_SESSION_F_CONNECTED;
3667 break;
3668
Dave Wallacee22aa742017-10-20 12:30:38 -04003669 default:
3670 rv = VPPCOM_EINVAL;
3671 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003672 }
3673
Dave Wallace35830af2017-10-09 01:43:42 -04003674 return rv;
3675}
3676
Stevenac1f96d2017-10-24 16:03:58 -07003677int
Florin Coras134a9962018-08-28 11:32:04 -07003678vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003679 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3680{
Florin Coras134a9962018-08-28 11:32:04 -07003681 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003682 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003683 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003684
3685 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003686 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003687 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003688 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003689 else
3690 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003691 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003692 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003693 }
3694
Florin Coras0a1e1832020-03-29 18:54:04 +00003695 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003696 {
Florin Coras5da10c42020-04-01 04:31:21 +00003697 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003698 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003699 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3700 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003701 else
Dave Barach178cf492018-11-13 16:34:13 -05003702 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3703 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003704 ep->is_ip4 = session->transport.is_ip4;
3705 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003706 }
Florin Coras460dce62018-07-27 05:45:06 -07003707
Stevenac1f96d2017-10-24 16:03:58 -07003708 return rv;
3709}
3710
3711int
Florin Coras134a9962018-08-28 11:32:04 -07003712vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003713 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3714{
Florin Coras7a2abce2020-04-05 19:25:44 +00003715 vcl_worker_t *wrk = vcl_worker_get_current ();
3716 vcl_session_t *s;
3717
3718 s = vcl_session_get_w_handle (wrk, session_handle);
3719 if (!s)
3720 return VPPCOM_EBADFD;
3721
Dave Wallace617dffa2017-10-26 14:47:06 -04003722 if (!buffer)
3723 return VPPCOM_EINVAL;
3724
3725 if (ep)
3726 {
Florin Coras5824cc52020-10-20 18:44:41 -07003727 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003728 return VPPCOM_EINVAL;
3729
3730 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003731 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003732 {
Florin Coras5824cc52020-10-20 18:44:41 -07003733 u32 session_index = s->session_index;
3734 f64 timeout = vcm->cfg.session_timeout;
3735 int rv;
3736
Florin Coras5da10c42020-04-01 04:31:21 +00003737 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003738 rv = vppcom_wait_for_session_state_change (session_index,
3739 VCL_STATE_READY,
3740 timeout);
3741 if (rv < 0)
3742 return rv;
3743 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003744 }
Florin Coras5824cc52020-10-20 18:44:41 -07003745
3746 s->transport.is_ip4 = ep->is_ip4;
3747 s->transport.rmt_port = ep->port;
3748 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003749 }
3750
3751 if (flags)
3752 {
3753 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003754 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003755 }
3756
Florin Coras7a2abce2020-04-05 19:25:44 +00003757 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3758 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003759}
3760
Dave Wallace048b1d62018-01-03 22:24:41 -05003761int
3762vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3763{
Florin Coras134a9962018-08-28 11:32:04 -07003764 vcl_worker_t *wrk = vcl_worker_get_current ();
3765 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003766 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003767 svm_msg_q_msg_t msg;
3768 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003769 int rv, num_ev = 0;
3770
Florin Coras5e062572019-03-14 19:07:51 -07003771 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003772
3773 if (!vp)
3774 return VPPCOM_EFAULT;
3775
3776 do
3777 {
Florin Coras7e12d942018-06-27 14:32:43 -07003778 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003779
Florin Coras6917b942018-11-13 22:44:54 -08003780 /* Dequeue all events and drop all unhandled io events */
3781 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3782 {
3783 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3784 vcl_handle_mq_event (wrk, e);
3785 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3786 }
3787 vec_reset_length (wrk->unhandled_evts_vector);
3788
Dave Wallace048b1d62018-01-03 22:24:41 -05003789 for (i = 0; i < n_sids; i++)
3790 {
Florin Coras7baeb712019-01-04 17:05:43 -08003791 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003792 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003793 {
3794 vp[i].revents = POLLHUP;
3795 num_ev++;
3796 continue;
3797 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003798
Florin Coras6917b942018-11-13 22:44:54 -08003799 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003800
3801 if (POLLIN & vp[i].events)
3802 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003803 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003804 if (rv > 0)
3805 {
Florin Coras6917b942018-11-13 22:44:54 -08003806 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003807 num_ev++;
3808 }
3809 else if (rv < 0)
3810 {
3811 switch (rv)
3812 {
3813 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003814 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003815 break;
3816
3817 default:
Florin Coras6917b942018-11-13 22:44:54 -08003818 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003819 break;
3820 }
3821 num_ev++;
3822 }
3823 }
3824
3825 if (POLLOUT & vp[i].events)
3826 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003827 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003828 if (rv > 0)
3829 {
Florin Coras6917b942018-11-13 22:44:54 -08003830 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003831 num_ev++;
3832 }
3833 else if (rv < 0)
3834 {
3835 switch (rv)
3836 {
3837 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003838 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003839 break;
3840
3841 default:
Florin Coras6917b942018-11-13 22:44:54 -08003842 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003843 break;
3844 }
3845 num_ev++;
3846 }
3847 }
3848
Dave Wallace7e607a72018-06-18 18:41:32 -04003849 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003850 {
Florin Coras6917b942018-11-13 22:44:54 -08003851 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003852 num_ev++;
3853 }
3854 }
3855 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003856 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003857 }
3858 while ((num_ev == 0) && keep_trying);
3859
Dave Wallace048b1d62018-01-03 22:24:41 -05003860 return num_ev;
3861}
3862
Florin Coras99368312018-08-02 10:45:44 -07003863int
3864vppcom_mq_epoll_fd (void)
3865{
Florin Coras134a9962018-08-28 11:32:04 -07003866 vcl_worker_t *wrk = vcl_worker_get_current ();
3867 return wrk->mqs_epfd;
3868}
3869
3870int
Florin Coras30e79c22019-01-02 19:31:22 -08003871vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003872{
3873 return session_handle & 0xFFFFFF;
3874}
3875
3876int
Florin Coras30e79c22019-01-02 19:31:22 -08003877vppcom_session_worker (vcl_session_handle_t session_handle)
3878{
3879 return session_handle >> 24;
3880}
3881
3882int
Florin Coras134a9962018-08-28 11:32:04 -07003883vppcom_worker_register (void)
3884{
Florin Coras47c40e22018-11-26 17:01:36 -08003885 if (!vcl_worker_alloc_and_init ())
3886 return VPPCOM_EEXIST;
3887
Florin Coras47c40e22018-11-26 17:01:36 -08003888 if (vcl_worker_register_with_vpp ())
3889 return VPPCOM_EEXIST;
3890
3891 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003892}
3893
Florin Coras369db832019-07-08 12:34:45 -07003894void
3895vppcom_worker_unregister (void)
3896{
3897 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3898 vcl_set_worker_index (~0);
3899}
3900
Pivo6017ff02020-05-04 17:57:33 +02003901void
3902vppcom_worker_index_set (int index)
3903{
3904 vcl_set_worker_index (index);
3905}
3906
Florin Corasdfe4cf42018-11-28 22:13:45 -08003907int
3908vppcom_worker_index (void)
3909{
3910 return vcl_get_worker_index ();
3911}
3912
Florin Corase0982e52019-01-25 13:19:56 -08003913int
3914vppcom_worker_mqs_epfd (void)
3915{
3916 vcl_worker_t *wrk = vcl_worker_get_current ();
3917 if (!vcm->cfg.use_mq_eventfd)
3918 return -1;
3919 return wrk->mqs_epfd;
3920}
3921
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003922int
3923vppcom_session_is_connectable_listener (uint32_t session_handle)
3924{
3925 vcl_session_t *session;
3926 vcl_worker_t *wrk = vcl_worker_get_current ();
3927 session = vcl_session_get_w_handle (wrk, session_handle);
3928 if (!session)
3929 return VPPCOM_EBADFD;
3930 return vcl_session_is_connectable_listener (wrk, session);
3931}
3932
3933int
3934vppcom_session_listener (uint32_t session_handle)
3935{
3936 vcl_worker_t *wrk = vcl_worker_get_current ();
3937 vcl_session_t *listen_session, *session;
3938 session = vcl_session_get_w_handle (wrk, session_handle);
3939 if (!session)
3940 return VPPCOM_EBADFD;
3941 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3942 return VPPCOM_EBADFD;
3943 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3944 if (!listen_session)
3945 return VPPCOM_EBADFD;
3946 return vcl_session_handle (listen_session);
3947}
3948
3949int
3950vppcom_session_n_accepted (uint32_t session_handle)
3951{
3952 vcl_worker_t *wrk = vcl_worker_get_current ();
3953 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3954 if (!session)
3955 return VPPCOM_EBADFD;
3956 return session->n_accepted_sessions;
3957}
3958
Florin Coras66ec4672020-06-15 07:59:40 -07003959const char *
3960vppcom_proto_str (vppcom_proto_t proto)
3961{
3962 char const *proto_str;
3963
3964 switch (proto)
3965 {
3966 case VPPCOM_PROTO_TCP:
3967 proto_str = "TCP";
3968 break;
3969 case VPPCOM_PROTO_UDP:
3970 proto_str = "UDP";
3971 break;
3972 case VPPCOM_PROTO_TLS:
3973 proto_str = "TLS";
3974 break;
3975 case VPPCOM_PROTO_QUIC:
3976 proto_str = "QUIC";
3977 break;
3978 default:
3979 proto_str = "UNKNOWN";
3980 break;
3981 }
3982 return proto_str;
3983}
3984
3985const char *
3986vppcom_retval_str (int retval)
3987{
3988 char const *st;
3989
3990 switch (retval)
3991 {
3992 case VPPCOM_OK:
3993 st = "VPPCOM_OK";
3994 break;
3995
3996 case VPPCOM_EAGAIN:
3997 st = "VPPCOM_EAGAIN";
3998 break;
3999
4000 case VPPCOM_EFAULT:
4001 st = "VPPCOM_EFAULT";
4002 break;
4003
4004 case VPPCOM_ENOMEM:
4005 st = "VPPCOM_ENOMEM";
4006 break;
4007
4008 case VPPCOM_EINVAL:
4009 st = "VPPCOM_EINVAL";
4010 break;
4011
4012 case VPPCOM_EBADFD:
4013 st = "VPPCOM_EBADFD";
4014 break;
4015
4016 case VPPCOM_EAFNOSUPPORT:
4017 st = "VPPCOM_EAFNOSUPPORT";
4018 break;
4019
4020 case VPPCOM_ECONNABORTED:
4021 st = "VPPCOM_ECONNABORTED";
4022 break;
4023
4024 case VPPCOM_ECONNRESET:
4025 st = "VPPCOM_ECONNRESET";
4026 break;
4027
4028 case VPPCOM_ENOTCONN:
4029 st = "VPPCOM_ENOTCONN";
4030 break;
4031
4032 case VPPCOM_ECONNREFUSED:
4033 st = "VPPCOM_ECONNREFUSED";
4034 break;
4035
4036 case VPPCOM_ETIMEDOUT:
4037 st = "VPPCOM_ETIMEDOUT";
4038 break;
4039
4040 default:
4041 st = "UNKNOWN_STATE";
4042 break;
4043 }
4044
4045 return st;
4046}
4047
Dave Wallacee22aa742017-10-20 12:30:38 -04004048/*
4049 * fd.io coding-style-patch-verification: ON
4050 *
4051 * Local Variables:
4052 * eval: (c-set-style "gnu")
4053 * End:
4054 */