blob: 184cb821e1d88ed1e219932bbfe6772c53716da5 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Coras30e79c22019-01-02 19:31:22 -080025static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070026vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080027{
28 svm_msg_q_msg_t *msg;
29 u32 n_msgs;
30 int i;
31
Florin Corase003a1b2019-06-05 10:47:16 -070032 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
Florin Coras30e79c22019-01-02 19:31:22 -080033 for (i = 0; i < n_msgs; i++)
34 {
35 vec_add2 (wrk->mq_msg_vector, msg, 1);
36 svm_msg_q_sub_w_lock (mq, msg);
37 }
38 return n_msgs;
39}
40
Florin Coras697faea2018-06-27 17:10:49 -070041const char *
Florin Coras288eaab2019-02-03 15:26:14 -080042vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040043{
44 char *st;
45
46 switch (state)
47 {
Florin Corasc127d5a2020-10-14 16:35:58 -070048 case VCL_STATE_CLOSED:
Florin Coras54140622020-02-04 19:04:34 +000049 st = "STATE_CLOSED";
Dave Wallace543852a2017-08-03 02:11:34 -040050 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070051 case VCL_STATE_LISTEN:
Dave Wallace543852a2017-08-03 02:11:34 -040052 st = "STATE_LISTEN";
53 break;
Florin Corasdfffdd72020-10-15 10:54:47 -070054 case VCL_STATE_READY:
55 st = "STATE_READY";
Dave Wallace543852a2017-08-03 02:11:34 -040056 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070057 case VCL_STATE_VPP_CLOSING:
Florin Coras3c7d4f92018-12-14 11:28:43 -080058 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050059 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070060 case VCL_STATE_DISCONNECT:
Dave Wallace543852a2017-08-03 02:11:34 -040061 st = "STATE_DISCONNECT";
62 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070063 case VCL_STATE_DETACHED:
Florin Corasadcfb152020-02-12 08:50:29 +000064 st = "STATE_DETACHED";
Dave Wallace543852a2017-08-03 02:11:34 -040065 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070066 case VCL_STATE_UPDATED:
Florin Coras2d675d72019-01-28 15:54:27 -080067 st = "STATE_UPDATED";
68 break;
Florin Corasc127d5a2020-10-14 16:35:58 -070069 case VCL_STATE_LISTEN_NO_MQ:
Florin Coras2d675d72019-01-28 15:54:27 -080070 st = "STATE_LISTEN_NO_MQ";
71 break;
Dave Wallace543852a2017-08-03 02:11:34 -040072 default:
73 st = "UNKNOWN_STATE";
74 break;
75 }
76
77 return st;
78}
79
Dave Wallace543852a2017-08-03 02:11:34 -040080u8 *
81format_ip4_address (u8 * s, va_list * args)
82{
83 u8 *a = va_arg (*args, u8 *);
84 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
85}
86
87u8 *
88format_ip6_address (u8 * s, va_list * args)
89{
90 ip6_address_t *a = va_arg (*args, ip6_address_t *);
91 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
92
93 i_max_n_zero = ARRAY_LEN (a->as_u16);
94 max_n_zeros = 0;
95 i_first_zero = i_max_n_zero;
96 n_zeros = 0;
97 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
98 {
99 u32 is_zero = a->as_u16[i] == 0;
100 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
101 {
102 i_first_zero = i;
103 n_zeros = 0;
104 }
105 n_zeros += is_zero;
106 if ((!is_zero && n_zeros > max_n_zeros)
107 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
108 {
109 i_max_n_zero = i_first_zero;
110 max_n_zeros = n_zeros;
111 i_first_zero = ARRAY_LEN (a->as_u16);
112 n_zeros = 0;
113 }
114 }
115
116 last_double_colon = 0;
117 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
118 {
119 if (i == i_max_n_zero && max_n_zeros > 1)
120 {
121 s = format (s, "::");
122 i += max_n_zeros - 1;
123 last_double_colon = 1;
124 }
125 else
126 {
127 s = format (s, "%s%x",
128 (last_double_colon || i == 0) ? "" : ":",
129 clib_net_to_host_u16 (a->as_u16[i]));
130 last_double_colon = 0;
131 }
132 }
133
134 return s;
135}
136
137/* Format an IP46 address. */
138u8 *
139format_ip46_address (u8 * s, va_list * args)
140{
141 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
142 ip46_type_t type = va_arg (*args, ip46_type_t);
143 int is_ip4 = 1;
144
145 switch (type)
146 {
147 case IP46_TYPE_ANY:
148 is_ip4 = ip46_address_is_ip4 (ip46);
149 break;
150 case IP46_TYPE_IP4:
151 is_ip4 = 1;
152 break;
153 case IP46_TYPE_IP6:
154 is_ip4 = 0;
155 break;
156 }
157
158 return is_ip4 ?
159 format (s, "%U", format_ip4_address, &ip46->ip4) :
160 format (s, "%U", format_ip6_address, &ip46->ip6);
161}
162
Florin Coras697faea2018-06-27 17:10:49 -0700163/*
164 * VPPCOM Utility Functions
165 */
166
Florin Coras458089b2019-08-21 16:20:44 -0700167static void
168vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
169{
170 app_session_evt_t _app_evt, *app_evt = &_app_evt;
171 session_listen_msg_t *mp;
172 svm_msg_q_t *mq;
173
174 mq = vcl_worker_ctrl_mq (wrk);
175 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
176 mp = (session_listen_msg_t *) app_evt->evt->data;
177 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700178 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700179 mp->context = s->session_index;
180 mp->wrk_index = wrk->vpp_wrk_index;
181 mp->is_ip4 = s->transport.is_ip4;
182 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
183 mp->port = s->transport.lcl_port;
184 mp->proto = s->session_type;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800185 mp->ckpair_index = s->ckpair_index;
Florin Coras1e966172020-05-16 18:18:14 +0000186 if (s->flags & VCL_SESSION_F_CONNECTED)
187 mp->flags = TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700188 app_send_ctrl_evt_to_vpp (mq, app_evt);
189}
190
191static void
192vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
193{
194 app_session_evt_t _app_evt, *app_evt = &_app_evt;
195 session_connect_msg_t *mp;
196 svm_msg_q_t *mq;
197
198 mq = vcl_worker_ctrl_mq (wrk);
199 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
200 mp = (session_connect_msg_t *) app_evt->evt->data;
201 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700202 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700203 mp->context = s->session_index;
204 mp->wrk_index = wrk->vpp_wrk_index;
205 mp->is_ip4 = s->transport.is_ip4;
206 mp->parent_handle = s->parent_handle;
207 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
Florin Corasef7cbf62019-10-17 09:56:27 -0700208 clib_memcpy_fast (&mp->lcl_ip, &s->transport.lcl_ip, sizeof (mp->lcl_ip));
Florin Coras458089b2019-08-21 16:20:44 -0700209 mp->port = s->transport.rmt_port;
Florin Coras0a1e1832020-03-29 18:54:04 +0000210 mp->lcl_port = s->transport.lcl_port;
Florin Coras458089b2019-08-21 16:20:44 -0700211 mp->proto = s->session_type;
Florin Corasa5a9efd2021-01-05 17:03:29 -0800212 mp->ckpair_index = s->ckpair_index;
Florin Coras0a1e1832020-03-29 18:54:04 +0000213 if (s->flags & VCL_SESSION_F_CONNECTED)
214 mp->flags |= TRANSPORT_CFG_F_CONNECTED;
Florin Coras458089b2019-08-21 16:20:44 -0700215 app_send_ctrl_evt_to_vpp (mq, app_evt);
216}
217
218void
219vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
220{
221 app_session_evt_t _app_evt, *app_evt = &_app_evt;
222 session_unlisten_msg_t *mp;
223 svm_msg_q_t *mq;
224
225 mq = vcl_worker_ctrl_mq (wrk);
226 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
227 mp = (session_unlisten_msg_t *) app_evt->evt->data;
228 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700229 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700230 mp->wrk_index = wrk->vpp_wrk_index;
231 mp->handle = s->vpp_handle;
232 mp->context = wrk->wrk_index;
233 app_send_ctrl_evt_to_vpp (mq, app_evt);
234}
235
236static void
237vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
238{
239 app_session_evt_t _app_evt, *app_evt = &_app_evt;
240 session_disconnect_msg_t *mp;
241 svm_msg_q_t *mq;
242
243 /* Send to thread that owns the session */
244 mq = s->vpp_evt_q;
245 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
246 mp = (session_disconnect_msg_t *) app_evt->evt->data;
247 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700248 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700249 mp->handle = s->vpp_handle;
250 app_send_ctrl_evt_to_vpp (mq, app_evt);
251}
252
253static void
254vcl_send_app_detach (vcl_worker_t * wrk)
255{
256 app_session_evt_t _app_evt, *app_evt = &_app_evt;
257 session_app_detach_msg_t *mp;
258 svm_msg_q_t *mq;
259
260 mq = vcl_worker_ctrl_mq (wrk);
261 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
262 mp = (session_app_detach_msg_t *) app_evt->evt->data;
263 memset (mp, 0, sizeof (*mp));
Florin Corascc7c88e2020-09-15 15:56:51 -0700264 mp->client_index = wrk->api_client_handle;
Florin Coras458089b2019-08-21 16:20:44 -0700265 app_send_ctrl_evt_to_vpp (mq, app_evt);
266}
Florin Coras697faea2018-06-27 17:10:49 -0700267
Florin Coras54693d22018-07-17 10:46:29 -0700268static void
269vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
270 session_handle_t handle, int retval)
271{
272 app_session_evt_t _app_evt, *app_evt = &_app_evt;
273 session_accepted_reply_msg_t *rmp;
274 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
275 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
276 rmp->handle = handle;
277 rmp->context = context;
278 rmp->retval = retval;
279 app_send_ctrl_evt_to_vpp (mq, app_evt);
280}
281
Florin Coras99368312018-08-02 10:45:44 -0700282static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800283vcl_send_session_disconnected_reply (vcl_worker_t * wrk, vcl_session_t * s,
284 int retval)
Florin Coras99368312018-08-02 10:45:44 -0700285{
286 app_session_evt_t _app_evt, *app_evt = &_app_evt;
287 session_disconnected_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800288 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
Florin Coras99368312018-08-02 10:45:44 -0700289 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
290 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800291 rmp->handle = s->vpp_handle;
292 rmp->context = wrk->api_client_handle;
Florin Coras99368312018-08-02 10:45:44 -0700293 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800294 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras99368312018-08-02 10:45:44 -0700295}
296
Florin Corasc9fbd662018-08-24 12:59:56 -0700297static void
Florin Coras52dd29f2020-11-18 19:02:17 -0800298vcl_send_session_reset_reply (vcl_worker_t * wrk, vcl_session_t * s,
299 int retval)
Florin Corasc9fbd662018-08-24 12:59:56 -0700300{
301 app_session_evt_t _app_evt, *app_evt = &_app_evt;
302 session_reset_reply_msg_t *rmp;
Florin Coras52dd29f2020-11-18 19:02:17 -0800303 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
304 SESSION_CTRL_EVT_RESET_REPLY);
Florin Corasc9fbd662018-08-24 12:59:56 -0700305 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
Florin Coras52dd29f2020-11-18 19:02:17 -0800306 rmp->handle = s->vpp_handle;
307 rmp->context = wrk->api_client_handle;
Florin Corasc9fbd662018-08-24 12:59:56 -0700308 rmp->retval = retval;
Florin Coras52dd29f2020-11-18 19:02:17 -0800309 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Corasc9fbd662018-08-24 12:59:56 -0700310}
311
Florin Coras30e79c22019-01-02 19:31:22 -0800312void
313vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
314 u32 wrk_index)
315{
316 app_session_evt_t _app_evt, *app_evt = &_app_evt;
317 session_worker_update_msg_t *mp;
Florin Coras30e79c22019-01-02 19:31:22 -0800318
Florin Coras52dd29f2020-11-18 19:02:17 -0800319 app_alloc_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt,
320 SESSION_CTRL_EVT_WORKER_UPDATE);
Florin Coras30e79c22019-01-02 19:31:22 -0800321 mp = (session_worker_update_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700322 mp->client_index = wrk->api_client_handle;
Florin Coras30e79c22019-01-02 19:31:22 -0800323 mp->handle = s->vpp_handle;
324 mp->req_wrk_index = wrk->vpp_wrk_index;
325 mp->wrk_index = wrk_index;
Florin Coras52dd29f2020-11-18 19:02:17 -0800326 app_send_ctrl_evt_to_vpp (s->vpp_evt_q, app_evt);
Florin Coras30e79c22019-01-02 19:31:22 -0800327}
328
hanlina3a48962020-07-13 11:09:15 +0800329int
Florin Coras40c07ce2020-07-16 20:46:17 -0700330vcl_send_worker_rpc (u32 dst_wrk_index, void *data, u32 data_len)
331{
332 app_session_evt_t _app_evt, *app_evt = &_app_evt;
333 session_app_wrk_rpc_msg_t *mp;
334 vcl_worker_t *dst_wrk, *wrk;
335 svm_msg_q_t *mq;
hanlina3a48962020-07-13 11:09:15 +0800336 int ret = -1;
Florin Coras40c07ce2020-07-16 20:46:17 -0700337
338 if (data_len > sizeof (mp->data))
339 goto done;
340
341 clib_spinlock_lock (&vcm->workers_lock);
342
343 dst_wrk = vcl_worker_get_if_valid (dst_wrk_index);
344 if (!dst_wrk)
345 goto done;
346
347 wrk = vcl_worker_get_current ();
348 mq = vcl_worker_ctrl_mq (wrk);
349 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_WRK_RPC);
350 mp = (session_app_wrk_rpc_msg_t *) app_evt->evt->data;
Florin Corascc7c88e2020-09-15 15:56:51 -0700351 mp->client_index = wrk->api_client_handle;
Florin Coras40c07ce2020-07-16 20:46:17 -0700352 mp->wrk_index = dst_wrk->vpp_wrk_index;
353 clib_memcpy (mp->data, data, data_len);
354 app_send_ctrl_evt_to_vpp (mq, app_evt);
hanlina3a48962020-07-13 11:09:15 +0800355 ret = 0;
Florin Coras40c07ce2020-07-16 20:46:17 -0700356
357done:
358 clib_spinlock_unlock (&vcm->workers_lock);
hanlina3a48962020-07-13 11:09:15 +0800359 return ret;
Florin Coras40c07ce2020-07-16 20:46:17 -0700360}
361
Florin Coras54693d22018-07-17 10:46:29 -0700362static u32
Florin Coras00cca802019-06-06 09:38:44 -0700363vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
364 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700365{
366 vcl_session_t *session, *listen_session;
Florin Coras99368312018-08-02 10:45:44 -0700367 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700368
Florin Coras134a9962018-08-28 11:32:04 -0700369 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700370
Florin Coras00cca802019-06-06 09:38:44 -0700371 listen_session = vcl_session_get (wrk, ls_index);
372 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700373 {
Florin Coras00cca802019-06-06 09:38:44 -0700374 VDBG (0, "ERROR: listener handle %lu does not match session %u",
375 mp->listener_handle, ls_index);
376 goto error;
377 }
378
Florin Corasc547e912020-12-08 17:50:45 -0800379 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800380 mp->server_tx_fifo,
381 mp->vpp_event_queue_address, 0, session))
Florin Coras00cca802019-06-06 09:38:44 -0700382 {
Florin Corasc547e912020-12-08 17:50:45 -0800383 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Coras00cca802019-06-06 09:38:44 -0700384 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700385 }
386
Florin Coras54693d22018-07-17 10:46:29 -0700387 session->vpp_handle = mp->handle;
Florin Corasdfffdd72020-10-15 10:54:47 -0700388 session->session_state = VCL_STATE_READY;
Florin Coras09d18c22019-04-24 11:10:02 -0700389 session->transport.rmt_port = mp->rmt.port;
390 session->transport.is_ip4 = mp->rmt.is_ip4;
391 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500392 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700393
Florin Coras134a9962018-08-28 11:32:04 -0700394 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700395 session->transport.lcl_port = listen_session->transport.lcl_port;
396 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700397 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200398 session->is_dgram = vcl_proto_is_dgram (session->session_type);
399 session->listener_index = listen_session->session_index;
400 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700401
Florin Coras5e062572019-03-14 19:07:51 -0700402 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
403 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700404 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
405 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
406 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700407 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
408
Florin Coras00cca802019-06-06 09:38:44 -0700409 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
410 session->vpp_handle, 0);
411
Florin Coras134a9962018-08-28 11:32:04 -0700412 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700413
414error:
Florin Corasb4624182020-12-11 13:58:12 -0800415 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0),
416 mp->vpp_event_queue_address, mp->mq_index, &evt_q);
Florin Coras00cca802019-06-06 09:38:44 -0700417 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
418 VNET_API_ERROR_INVALID_ARGUMENT);
419 vcl_session_free (wrk, session);
420 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700421}
422
423static u32
Florin Coras134a9962018-08-28 11:32:04 -0700424vcl_session_connected_handler (vcl_worker_t * wrk,
425 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700426{
Florin Coras99368312018-08-02 10:45:44 -0700427 vcl_session_t *session = 0;
Florin Coras52dd29f2020-11-18 19:02:17 -0800428 u32 session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700429
430 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700431 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700432 if (!session)
433 {
Florin Coras5e062572019-03-14 19:07:51 -0700434 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
435 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700436 return VCL_INVALID_SESSION_INDEX;
437 }
Florin Coras54693d22018-07-17 10:46:29 -0700438 if (mp->retval)
439 {
Florin Coras5e062572019-03-14 19:07:51 -0700440 VDBG (0, "ERROR: session index %u: connect failed! %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700441 session_index, format_session_error, mp->retval);
Florin Corasc127d5a2020-10-14 16:35:58 -0700442 session->session_state = VCL_STATE_DETACHED;
Florin Coras070453d2018-08-24 17:04:27 -0700443 session->vpp_handle = mp->handle;
444 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700445 }
446
Florin Corasdbc9c592019-09-25 16:37:43 -0700447 session->vpp_handle = mp->handle;
Florin Corasc547e912020-12-08 17:50:45 -0800448
449 if (vcl_segment_attach_session (mp->segment_handle, mp->server_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800450 mp->server_tx_fifo,
451 mp->vpp_event_queue_address, 0, session))
Florin Corasd85de682018-11-29 17:02:29 -0800452 {
Florin Corasc547e912020-12-08 17:50:45 -0800453 VDBG (0, "failed to attach fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700454 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700455 vcl_send_session_disconnect (wrk, session);
456 return session_index;
Florin Corasd85de682018-11-29 17:02:29 -0800457 }
458
Florin Coras653e43f2019-03-04 10:56:23 -0800459 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700460 {
Florin Corasc547e912020-12-08 17:50:45 -0800461 if (vcl_segment_attach_session (mp->ct_segment_handle, mp->ct_rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800462 mp->ct_tx_fifo, (uword) ~0, 1, session))
Florin Coras653e43f2019-03-04 10:56:23 -0800463 {
Florin Corasc547e912020-12-08 17:50:45 -0800464 VDBG (0, "failed to attach ct fifos for %u", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700465 session->session_state = VCL_STATE_DETACHED;
Florin Corasdbc9c592019-09-25 16:37:43 -0700466 vcl_send_session_disconnect (wrk, session);
467 return session_index;
Florin Coras653e43f2019-03-04 10:56:23 -0800468 }
Florin Coras99368312018-08-02 10:45:44 -0700469 }
Florin Coras54693d22018-07-17 10:46:29 -0700470
Florin Coras09d18c22019-04-24 11:10:02 -0700471 session->transport.is_ip4 = mp->lcl.is_ip4;
472 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500473 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700474 session->transport.lcl_port = mp->lcl.port;
Florin Corasa5ea8212020-08-24 21:23:51 -0700475
476 /* Application closed session before connect reply */
Florin Corasac422d62020-10-19 20:51:36 -0700477 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK)
Florin Corasc127d5a2020-10-14 16:35:58 -0700478 && session->session_state == VCL_STATE_CLOSED)
Florin Corasa5ea8212020-08-24 21:23:51 -0700479 vcl_send_session_disconnect (wrk, session);
480 else
Florin Corasdfffdd72020-10-15 10:54:47 -0700481 session->session_state = VCL_STATE_READY;
Florin Coras54693d22018-07-17 10:46:29 -0700482
483 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800484 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700485
Florin Coras5e062572019-03-14 19:07:51 -0700486 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
487 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700488 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700489
Florin Coras54693d22018-07-17 10:46:29 -0700490 return session_index;
491}
492
Florin Coras3c7d4f92018-12-14 11:28:43 -0800493static int
494vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
495{
496 vcl_session_msg_t *accepted_msg;
497 int i;
498
499 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
500 {
501 accepted_msg = &session->accept_evts_fifo[i];
502 if (accepted_msg->accepted_msg.handle == handle)
503 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800504 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800505 return 1;
506 }
507 }
508 return 0;
509}
510
Florin Corasc9fbd662018-08-24 12:59:56 -0700511static u32
Florin Coras134a9962018-08-28 11:32:04 -0700512vcl_session_reset_handler (vcl_worker_t * wrk,
513 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700514{
515 vcl_session_t *session;
516 u32 sid;
517
Florin Coras134a9962018-08-28 11:32:04 -0700518 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
519 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700520 if (!session)
521 {
522 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
523 return VCL_INVALID_SESSION_INDEX;
524 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800525
526 /* Caught a reset before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700527 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800528 {
529
530 if (!vcl_flag_accepted_session (session, reset_msg->handle,
531 VCL_ACCEPTED_F_RESET))
532 VDBG (0, "session was not accepted!");
533 return VCL_INVALID_SESSION_INDEX;
534 }
535
Florin Corasc127d5a2020-10-14 16:35:58 -0700536 if (session->session_state != VCL_STATE_CLOSED)
537 session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800538 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700539 return sid;
540}
541
Florin Coras60116992018-08-27 09:52:18 -0700542static u32
Florin Coras134a9962018-08-28 11:32:04 -0700543vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700544{
545 vcl_session_t *session;
546 u32 sid = mp->context;
547
Florin Coras134a9962018-08-28 11:32:04 -0700548 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700549 if (mp->retval)
550 {
Florin Coras5e062572019-03-14 19:07:51 -0700551 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Coras00e01d32019-10-21 16:07:46 -0700552 format_session_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700553 if (session)
554 {
Florin Corasc127d5a2020-10-14 16:35:58 -0700555 session->session_state = VCL_STATE_DETACHED;
Florin Coras60116992018-08-27 09:52:18 -0700556 session->vpp_handle = mp->handle;
557 return sid;
558 }
559 else
560 {
Florin Coras5e062572019-03-14 19:07:51 -0700561 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
562 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700563 return VCL_INVALID_SESSION_INDEX;
564 }
565 }
566
567 session->vpp_handle = mp->handle;
568 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500569 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
570 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700571 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700572 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Corasc127d5a2020-10-14 16:35:58 -0700573 session->session_state = VCL_STATE_LISTEN;
Florin Coras5f45e012019-01-23 09:21:30 -0800574
Florin Coras6e3c1f82020-01-15 01:30:46 +0000575 if (vcl_session_is_cl (session))
Florin Coras60116992018-08-27 09:52:18 -0700576 {
Florin Corasc547e912020-12-08 17:50:45 -0800577 if (vcl_segment_attach_session (mp->segment_handle, mp->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800578 mp->tx_fifo, mp->vpp_evt_q, 0, session))
Florin Corasc547e912020-12-08 17:50:45 -0800579 {
580 VDBG (0, "failed to attach fifos for %u", session->session_index);
581 session->session_state = VCL_STATE_DETACHED;
582 return VCL_INVALID_SESSION_INDEX;
583 }
Florin Coras60116992018-08-27 09:52:18 -0700584 }
585
Florin Coras05ecfcc2018-12-12 18:19:39 -0800586 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700587 return sid;
588}
589
Florin Corasdfae9f92019-02-20 19:48:31 -0800590static void
591vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
592{
593 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
594 vcl_session_t *s;
595
596 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
Florin Coras0a1e1832020-03-29 18:54:04 +0000597 if (!s)
Florin Corasdfae9f92019-02-20 19:48:31 -0800598 {
599 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
600 return;
601 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700602 if (s->session_state != VCL_STATE_DISCONNECT)
Florin Coras0a1e1832020-03-29 18:54:04 +0000603 {
604 /* Connected udp listener */
605 if (s->session_type == VPPCOM_PROTO_UDP
Florin Corasc127d5a2020-10-14 16:35:58 -0700606 && s->session_state == VCL_STATE_CLOSED)
Florin Coras0a1e1832020-03-29 18:54:04 +0000607 return;
608
609 VDBG (0, "Unlisten session in wrong state %llx", mp->handle);
610 return;
611 }
Florin Corasdfae9f92019-02-20 19:48:31 -0800612
613 if (mp->retval)
614 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
Florin Coras00e01d32019-10-21 16:07:46 -0700615 s->session_index, mp->handle, format_session_error, mp->retval);
Florin Corasdfae9f92019-02-20 19:48:31 -0800616
617 if (mp->context != wrk->wrk_index)
618 VDBG (0, "wrong context");
619
620 vcl_session_table_del_vpp_handle (wrk, mp->handle);
621 vcl_session_free (wrk, s);
622}
623
Florin Coras68b7e582020-01-21 18:33:23 -0800624static void
625vcl_session_migrated_handler (vcl_worker_t * wrk, void *data)
626{
627 session_migrated_msg_t *mp = (session_migrated_msg_t *) data;
628 vcl_session_t *s;
Florin Corasc547e912020-12-08 17:50:45 -0800629 u32 fs_index;
Florin Coras68b7e582020-01-21 18:33:23 -0800630
631 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
632 if (!s)
633 {
634 VDBG (0, "Migrated notification with wrong handle %llx", mp->handle);
635 return;
636 }
637
Florin Corasc547e912020-12-08 17:50:45 -0800638 fs_index = vcl_segment_table_lookup (mp->segment_handle);
639 if (fs_index == VCL_INVALID_SEGMENT_INDEX)
640 {
641 VDBG (0, "segment for session %u is not mounted!", s->session_index);
642 s->session_state = VCL_STATE_DETACHED;
643 return;
644 }
645
Florin Coras57660d92020-04-04 22:45:34 +0000646 s->vpp_handle = mp->new_handle;
Florin Corasb4624182020-12-11 13:58:12 -0800647
648 vcl_segment_attach_mq (vcl_vpp_worker_segment_handle (0), mp->vpp_evt_q,
649 mp->vpp_thread_index, &s->vpp_evt_q);
Florin Coras68b7e582020-01-21 18:33:23 -0800650
Florin Coras68b7e582020-01-21 18:33:23 -0800651 vcl_session_table_del_vpp_handle (wrk, mp->handle);
652 vcl_session_table_add_vpp_handle (wrk, mp->new_handle, s->session_index);
653
654 /* Generate new tx event if we have outstanding data */
655 if (svm_fifo_has_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -0800656 app_send_io_evt_to_vpp (s->vpp_evt_q,
657 s->tx_fifo->shr->master_session_index,
Florin Coras68b7e582020-01-21 18:33:23 -0800658 SESSION_IO_EVT_TX, SVM_Q_WAIT);
659
Florin Coras57660d92020-04-04 22:45:34 +0000660 VDBG (0, "Migrated 0x%lx to thread %u 0x%lx", mp->handle,
Florin Coras52dd29f2020-11-18 19:02:17 -0800661 mp->vpp_thread_index, mp->new_handle);
Florin Coras68b7e582020-01-21 18:33:23 -0800662}
663
Florin Coras3c7d4f92018-12-14 11:28:43 -0800664static vcl_session_t *
665vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
666{
667 vcl_session_msg_t *vcl_msg;
668 vcl_session_t *session;
669
670 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
671 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800672 VWRN ("session overlap handle %lu state %u!", msg->handle,
673 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800674
675 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
676 if (!session)
677 {
678 VERR ("couldn't find listen session: listener handle %llx",
679 msg->listener_handle);
680 return 0;
681 }
682
683 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
Florin Corase88845e2020-02-13 20:04:28 +0000684 vcl_msg->flags = 0;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800685 vcl_msg->accepted_msg = *msg;
686 /* Session handle points to listener until fully accepted by app */
687 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
688
689 return session;
690}
691
692static vcl_session_t *
693vcl_session_disconnected_handler (vcl_worker_t * wrk,
694 session_disconnected_msg_t * msg)
695{
696 vcl_session_t *session;
697
698 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
699 if (!session)
700 {
701 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
702 return 0;
703 }
704
Florin Corasadcfb152020-02-12 08:50:29 +0000705 /* Late disconnect notification on a session that has been closed */
Florin Corasc127d5a2020-10-14 16:35:58 -0700706 if (session->session_state == VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000707 return 0;
708
Florin Coras3c7d4f92018-12-14 11:28:43 -0800709 /* Caught a disconnect before actually accepting the session */
Florin Corasc127d5a2020-10-14 16:35:58 -0700710 if (session->session_state == VCL_STATE_LISTEN)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800711 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800712 if (!vcl_flag_accepted_session (session, msg->handle,
713 VCL_ACCEPTED_F_CLOSED))
714 VDBG (0, "session was not accepted!");
715 return 0;
716 }
717
Florin Corasadcfb152020-02-12 08:50:29 +0000718 /* If not already reset change state */
Florin Corasc127d5a2020-10-14 16:35:58 -0700719 if (session->session_state != VCL_STATE_DISCONNECT)
720 session->session_state = VCL_STATE_VPP_CLOSING;
Florin Corasadcfb152020-02-12 08:50:29 +0000721
Florin Coras3c7d4f92018-12-14 11:28:43 -0800722 return session;
723}
724
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700725static int
726vppcom_session_disconnect (u32 session_handle)
727{
728 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700729 vcl_session_t *session, *listen_session;
730 vcl_session_state_t state;
731 u64 vpp_handle;
732
733 session = vcl_session_get_w_handle (wrk, session_handle);
734 if (!session)
735 return VPPCOM_EBADFD;
736
737 vpp_handle = session->vpp_handle;
738 state = session->session_state;
739
740 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
741 vpp_handle, state, vppcom_session_state_str (state));
742
743 if (PREDICT_FALSE (state == VCL_STATE_LISTEN))
744 {
745 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
746 return VPPCOM_EBADFD;
747 }
748
749 if (state == VCL_STATE_VPP_CLOSING)
750 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800751 vcl_send_session_disconnected_reply (wrk, session, 0);
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700752 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
753 session->session_index, vpp_handle);
754 }
755 else
756 {
757 /* Session doesn't have an event queue yet. Probably a non-blocking
758 * connect. Wait for the reply */
759 if (PREDICT_FALSE (!session->vpp_evt_q))
760 return VPPCOM_OK;
761
762 VDBG (1, "session %u [0x%llx]: sending disconnect...",
763 session->session_index, vpp_handle);
764 vcl_send_session_disconnect (wrk, session);
765 }
766
767 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
768 {
769 listen_session = vcl_session_get (wrk, session->listener_index);
770 listen_session->n_accepted_sessions--;
771 }
772
773 return VPPCOM_OK;
774}
775
Florin Coras30e79c22019-01-02 19:31:22 -0800776static void
Florin Coras9ace36d2019-10-28 13:14:17 -0700777vcl_session_cleanup_handler (vcl_worker_t * wrk, void *data)
778{
779 session_cleanup_msg_t *msg;
780 vcl_session_t *session;
781
782 msg = (session_cleanup_msg_t *) data;
783 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
784 if (!session)
785 {
786 VDBG (0, "disconnect confirmed for unknown handle 0x%llx", msg->handle);
787 return;
788 }
789
Florin Coras36d49392020-04-24 23:00:11 +0000790 if (msg->type == SESSION_CLEANUP_TRANSPORT)
791 {
792 /* Transport was cleaned up before we confirmed close. Probably the
793 * app is still waiting for some data that cannot be delivered.
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700794 * Confirm close to make sure everything is cleaned up.
795 * Move to undetermined state to ensure that the session is not
796 * removed before both vpp and the app cleanup.
797 * - If the app closes first, the session is moved to CLOSED state
798 * and the session cleanup notification from vpp removes the
799 * session.
800 * - If vpp cleans up the session first, the session is moved to
801 * DETACHED state lower and subsequently the close from the app
802 * frees the session
803 */
804 if (session->session_state == VCL_STATE_VPP_CLOSING)
Florin Coras0ff7eec2020-08-03 18:55:40 -0700805 {
Florin Coras2bd8b3a2020-10-25 20:28:23 -0700806 vppcom_session_disconnect (vcl_session_handle (session));
807 session->session_state = VCL_STATE_UPDATED;
808 }
809 else if (session->session_state == VCL_STATE_DISCONNECT)
810 {
Florin Coras52dd29f2020-11-18 19:02:17 -0800811 vcl_send_session_reset_reply (wrk, session, 0);
Florin Coras0ff7eec2020-08-03 18:55:40 -0700812 session->session_state = VCL_STATE_UPDATED;
813 }
Florin Coras36d49392020-04-24 23:00:11 +0000814 return;
815 }
816
Florin Coras9ace36d2019-10-28 13:14:17 -0700817 vcl_session_table_del_vpp_handle (wrk, msg->handle);
Florin Corasadcfb152020-02-12 08:50:29 +0000818 /* Should not happen. App did not close the connection so don't free it. */
Florin Corasc127d5a2020-10-14 16:35:58 -0700819 if (session->session_state != VCL_STATE_CLOSED)
Florin Corasadcfb152020-02-12 08:50:29 +0000820 {
821 VDBG (0, "app did not close session %d", session->session_index);
Florin Corasc127d5a2020-10-14 16:35:58 -0700822 session->session_state = VCL_STATE_DETACHED;
Florin Corasadcfb152020-02-12 08:50:29 +0000823 session->vpp_handle = VCL_INVALID_SESSION_HANDLE;
824 return;
825 }
Florin Coras9ace36d2019-10-28 13:14:17 -0700826 vcl_session_free (wrk, session);
827}
828
829static void
Florin Coras30e79c22019-01-02 19:31:22 -0800830vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
831{
832 session_req_worker_update_msg_t *msg;
833 vcl_session_t *s;
834
835 msg = (session_req_worker_update_msg_t *) data;
836 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
837 if (!s)
838 return;
839
840 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
841}
842
843static void
844vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
845{
846 session_worker_update_reply_msg_t *msg;
847 vcl_session_t *s;
848
849 msg = (session_worker_update_reply_msg_t *) data;
850 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
851 if (!s)
852 {
853 VDBG (0, "unknown handle 0x%llx", msg->handle);
854 return;
855 }
Florin Coras30e79c22019-01-02 19:31:22 -0800856
Florin Coras5f45e012019-01-23 09:21:30 -0800857 if (s->rx_fifo)
858 {
Florin Corasc547e912020-12-08 17:50:45 -0800859 if (vcl_segment_attach_session (msg->segment_handle, msg->rx_fifo,
Florin Corasb4624182020-12-11 13:58:12 -0800860 msg->tx_fifo, (uword) ~0, 0, s))
Florin Corasc547e912020-12-08 17:50:45 -0800861 {
862 VDBG (0, "failed to attach fifos for %u", s->session_index);
863 return;
864 }
Florin Coras5f45e012019-01-23 09:21:30 -0800865 }
Florin Corasc127d5a2020-10-14 16:35:58 -0700866 s->session_state = VCL_STATE_UPDATED;
Florin Coras30e79c22019-01-02 19:31:22 -0800867
Florin Coras30e79c22019-01-02 19:31:22 -0800868 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
869 s->vpp_handle, wrk->wrk_index);
870}
871
Florin Coras935ce752020-09-08 22:43:47 -0700872static int
873vcl_api_recv_fd (vcl_worker_t * wrk, int *fds, int n_fds)
874{
875
876 if (vcm->cfg.vpp_app_socket_api)
877 return vcl_sapi_recv_fds (wrk, fds, n_fds);
878
879 return vcl_bapi_recv_fds (wrk, fds, n_fds);
880}
881
Florin Corasc4c4cf52019-08-24 18:17:34 -0700882static void
883vcl_session_app_add_segment_handler (vcl_worker_t * wrk, void *data)
884{
885 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
886 session_app_add_segment_msg_t *msg;
887 u64 segment_handle;
888 int fd = -1;
889
890 msg = (session_app_add_segment_msg_t *) data;
891
892 if (msg->fd_flags)
893 {
Florin Coras935ce752020-09-08 22:43:47 -0700894 vcl_api_recv_fd (wrk, &fd, 1);
Florin Corasc4c4cf52019-08-24 18:17:34 -0700895 seg_type = SSVM_SEGMENT_MEMFD;
896 }
897
898 segment_handle = msg->segment_handle;
899 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
900 {
901 clib_warning ("invalid segment handle");
902 return;
903 }
904
905 if (vcl_segment_attach (segment_handle, (char *) msg->segment_name,
906 seg_type, fd))
907 {
908 VDBG (0, "vcl_segment_attach ('%s') failed", msg->segment_name);
909 return;
910 }
911
912 VDBG (1, "mapped new segment '%s' size %d", msg->segment_name,
913 msg->segment_size);
914}
915
916static void
917vcl_session_app_del_segment_handler (vcl_worker_t * wrk, void *data)
918{
919 session_app_del_segment_msg_t *msg = (session_app_del_segment_msg_t *) data;
920 vcl_segment_detach (msg->segment_handle);
921 VDBG (1, "Unmapped segment: %d", msg->segment_handle);
922}
923
Florin Coras40c07ce2020-07-16 20:46:17 -0700924static void
925vcl_worker_rpc_handler (vcl_worker_t * wrk, void *data)
926{
927 if (!vcm->wrk_rpc_fn)
928 return;
929
hanlina3a48962020-07-13 11:09:15 +0800930 (vcm->wrk_rpc_fn) (((session_app_wrk_rpc_msg_t *) data)->data);
Florin Coras40c07ce2020-07-16 20:46:17 -0700931}
932
Florin Coras86f04502018-09-12 16:08:01 -0700933static int
934vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700935{
Florin Coras54693d22018-07-17 10:46:29 -0700936 session_disconnected_msg_t *disconnected_msg;
Florin Corasb242d312020-10-26 15:35:40 -0700937 session_connected_msg_t *connected_msg;
938 session_reset_msg_t *reset_msg;
939 session_event_t *ecpy;
Florin Corasc127d5a2020-10-14 16:35:58 -0700940 vcl_session_t *s;
Florin Corasb242d312020-10-26 15:35:40 -0700941 u32 sid;
Florin Coras54693d22018-07-17 10:46:29 -0700942
943 switch (e->event_type)
944 {
Florin Coras653e43f2019-03-04 10:56:23 -0800945 case SESSION_IO_EVT_RX:
946 case SESSION_IO_EVT_TX:
Florin Corasc127d5a2020-10-14 16:35:58 -0700947 s = vcl_session_get (wrk, e->session_index);
Florin Coras1bc919e2020-10-18 20:17:49 -0700948 if (!s || !vcl_session_is_open (s))
Florin Coras653e43f2019-03-04 10:56:23 -0800949 break;
Florin Coras86f04502018-09-12 16:08:01 -0700950 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700951 break;
Florin Corasb242d312020-10-26 15:35:40 -0700952 case SESSION_CTRL_EVT_BOUND:
953 /* We can only wait for only one listen so not postponed */
954 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
955 break;
Florin Coras54693d22018-07-17 10:46:29 -0700956 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -0700957 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
958 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
959 {
960 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
961 *ecpy = *e;
962 ecpy->postponed = 1;
963 ecpy->session_index = s->session_index;
964 }
Florin Coras54693d22018-07-17 10:46:29 -0700965 break;
966 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -0700967 connected_msg = (session_connected_msg_t *) e->data;
968 sid = vcl_session_connected_handler (wrk, connected_msg);
969 if (!(s = vcl_session_get (wrk, sid)))
970 break;
971 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
972 {
973 vec_add2 (wrk->unhandled_evts_vector, ecpy, 1);
974 *ecpy = *e;
975 ecpy->postponed = 1;
976 ecpy->session_index = s->session_index;
977 }
Florin Coras54693d22018-07-17 10:46:29 -0700978 break;
979 case SESSION_CTRL_EVT_DISCONNECTED:
980 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -0700981 if (!(s = vcl_session_get_w_vpp_handle (wrk, disconnected_msg->handle)))
982 break;
983 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
984 {
985 vec_add1 (wrk->unhandled_evts_vector, *e);
986 break;
987 }
988 if (!(s = vcl_session_disconnected_handler (wrk, disconnected_msg)))
Florin Coras3c7d4f92018-12-14 11:28:43 -0800989 break;
Florin Corasc127d5a2020-10-14 16:35:58 -0700990 VDBG (0, "disconnected session %u [0x%llx]", s->session_index,
991 s->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700992 break;
993 case SESSION_CTRL_EVT_RESET:
Florin Corasb242d312020-10-26 15:35:40 -0700994 reset_msg = (session_reset_msg_t *) e->data;
995 if (!(s = vcl_session_get_w_vpp_handle (wrk, reset_msg->handle)))
996 break;
997 if (vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
998 {
999 vec_add1 (wrk->unhandled_evts_vector, *e);
1000 break;
1001 }
Florin Coras134a9962018-08-28 11:32:04 -07001002 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -07001003 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001004 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1005 vcl_session_unlisten_reply_handler (wrk, e->data);
1006 break;
Florin Coras68b7e582020-01-21 18:33:23 -08001007 case SESSION_CTRL_EVT_MIGRATED:
1008 vcl_session_migrated_handler (wrk, e->data);
1009 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07001010 case SESSION_CTRL_EVT_CLEANUP:
1011 vcl_session_cleanup_handler (wrk, e->data);
1012 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001013 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1014 vcl_session_req_worker_update_handler (wrk, e->data);
1015 break;
1016 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1017 vcl_session_worker_update_reply_handler (wrk, e->data);
1018 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07001019 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
1020 vcl_session_app_add_segment_handler (wrk, e->data);
1021 break;
1022 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
1023 vcl_session_app_del_segment_handler (wrk, e->data);
1024 break;
hanlina3a48962020-07-13 11:09:15 +08001025 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07001026 vcl_worker_rpc_handler (wrk, e->data);
1027 break;
Florin Coras54693d22018-07-17 10:46:29 -07001028 default:
1029 clib_warning ("unhandled %u", e->event_type);
1030 }
1031 return VPPCOM_OK;
1032}
1033
Florin Coras30e79c22019-01-02 19:31:22 -08001034static int
Florin Coras697faea2018-06-27 17:10:49 -07001035vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -08001036 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -07001037 f64 wait_for_time)
1038{
Florin Coras134a9962018-08-28 11:32:04 -07001039 vcl_worker_t *wrk = vcl_worker_get_current ();
1040 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -07001041 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -07001042 svm_msg_q_msg_t msg;
1043 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -04001044
Florin Coras697faea2018-06-27 17:10:49 -07001045 do
Dave Wallace543852a2017-08-03 02:11:34 -04001046 {
Florin Coras134a9962018-08-28 11:32:04 -07001047 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001048 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -07001049 {
Florin Coras070453d2018-08-24 17:04:27 -07001050 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -07001051 }
Florin Corasdfffdd72020-10-15 10:54:47 -07001052 if (session->session_state == state)
Florin Coras697faea2018-06-27 17:10:49 -07001053 {
Florin Coras697faea2018-06-27 17:10:49 -07001054 return VPPCOM_OK;
1055 }
Florin Corasc127d5a2020-10-14 16:35:58 -07001056 if (session->session_state == VCL_STATE_DETACHED)
Florin Coras697faea2018-06-27 17:10:49 -07001057 {
Florin Coras697faea2018-06-27 17:10:49 -07001058 return VPPCOM_ECONNREFUSED;
1059 }
Florin Coras54693d22018-07-17 10:46:29 -07001060
Florin Coras134a9962018-08-28 11:32:04 -07001061 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -08001062 {
1063 usleep (100);
1064 continue;
1065 }
Florin Coras134a9962018-08-28 11:32:04 -07001066 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -07001067 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001068 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001069 }
Florin Coras134a9962018-08-28 11:32:04 -07001070 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001071
Florin Coras05ecfcc2018-12-12 18:19:39 -08001072 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -07001073 vppcom_session_state_str (state));
1074 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -04001075
Florin Coras697faea2018-06-27 17:10:49 -07001076 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001077}
1078
Florin Coras30e79c22019-01-02 19:31:22 -08001079static void
1080vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
1081{
Florin Coras288eaab2019-02-03 15:26:14 -08001082 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -08001083 vcl_session_t *s;
1084 u32 *sip;
1085
1086 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
1087 return;
1088
1089 vec_foreach (sip, wrk->pending_session_wrk_updates)
1090 {
1091 s = vcl_session_get (wrk, *sip);
1092 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
1093 state = s->session_state;
Florin Corasc127d5a2020-10-14 16:35:58 -07001094 vppcom_wait_for_session_state_change (s->session_index, VCL_STATE_UPDATED,
1095 5);
Florin Coras30e79c22019-01-02 19:31:22 -08001096 s->session_state = state;
1097 }
1098 vec_reset_length (wrk->pending_session_wrk_updates);
1099}
1100
Florin Corasf9240dc2019-01-15 08:03:17 -08001101void
Florin Coras30e79c22019-01-02 19:31:22 -08001102vcl_flush_mq_events (void)
1103{
1104 vcl_worker_t *wrk = vcl_worker_get_current ();
1105 svm_msg_q_msg_t *msg;
1106 session_event_t *e;
1107 svm_msg_q_t *mq;
1108 int i;
1109
1110 mq = wrk->app_event_queue;
1111 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -07001112 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -08001113 svm_msg_q_unlock (mq);
1114
1115 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
1116 {
1117 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
1118 e = svm_msg_q_msg_data (mq, msg);
1119 vcl_handle_mq_event (wrk, e);
1120 svm_msg_q_free_msg (mq, msg);
1121 }
1122 vec_reset_length (wrk->mq_msg_vector);
1123 vcl_handle_pending_wrk_updates (wrk);
1124}
1125
Florin Coras697faea2018-06-27 17:10:49 -07001126static int
Florin Corasab2f6db2018-08-31 14:31:41 -07001127vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001128{
Florin Coras134a9962018-08-28 11:32:04 -07001129 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -07001130 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001131 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -07001132 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -04001133
Florin Corasab2f6db2018-08-31 14:31:41 -07001134 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001135 if (!session)
1136 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001137
Florin Corasaaff5ee2019-07-08 13:00:00 -07001138 /* Flush pending accept events, if any */
1139 while (clib_fifo_elts (session->accept_evts_fifo))
1140 {
1141 clib_fifo_sub2 (session->accept_evts_fifo, evt);
1142 accepted_msg = &evt->accepted_msg;
1143 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
1144 vcl_send_session_accepted_reply (session->vpp_evt_q,
1145 accepted_msg->context,
wanghanlin785b6ea2020-12-10 19:12:22 +08001146 accepted_msg->handle, -1);
Florin Corasaaff5ee2019-07-08 13:00:00 -07001147 }
1148 clib_fifo_free (session->accept_evts_fifo);
1149
Florin Coras458089b2019-08-21 16:20:44 -07001150 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001151
Florin Coras5e062572019-03-14 19:07:51 -07001152 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -07001153 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -07001154 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -07001155
1156 session->vpp_handle = ~0;
Florin Corasc127d5a2020-10-14 16:35:58 -07001157 session->session_state = VCL_STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001158
Florin Coras070453d2018-08-24 17:04:27 -07001159 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001160}
1161
Florin Coras940f78f2018-11-30 12:11:20 -08001162/**
1163 * Handle app exit
1164 *
1165 * Notify vpp of the disconnect and mark the worker as free. If we're the
1166 * last worker, do a full cleanup otherwise, since we're probably a forked
1167 * child, avoid syscalls as much as possible. We might've lost privileges.
1168 */
1169void
1170vppcom_app_exit (void)
1171{
1172 if (!pool_elts (vcm->workers))
1173 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001174 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1175 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001176 vcl_elog_stop (vcm);
Florin Coras940f78f2018-11-30 12:11:20 -08001177}
1178
Florin Coras935ce752020-09-08 22:43:47 -07001179static int
1180vcl_api_attach (void)
1181{
1182 if (vcm->cfg.vpp_app_socket_api)
1183 return vcl_sapi_attach ();
1184
1185 return vcl_bapi_attach ();
1186}
1187
1188static void
1189vcl_api_detach (vcl_worker_t * wrk)
1190{
1191 vcl_send_app_detach (wrk);
1192
1193 if (vcm->cfg.vpp_app_socket_api)
1194 return vcl_sapi_detach (wrk);
1195
1196 return vcl_bapi_disconnect_from_vpp ();
1197}
1198
Dave Wallace543852a2017-08-03 02:11:34 -04001199/*
1200 * VPPCOM Public API functions
1201 */
1202int
Florin Coras66ec4672020-06-15 07:59:40 -07001203vppcom_app_create (const char *app_name)
Dave Wallace543852a2017-08-03 02:11:34 -04001204{
Dave Wallace543852a2017-08-03 02:11:34 -04001205 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001206 int rv;
1207
Florin Coras47c40e22018-11-26 17:01:36 -08001208 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001209 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001210 VDBG (1, "already initialized");
1211 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001212 }
1213
Florin Coras47c40e22018-11-26 17:01:36 -08001214 vcm->is_init = 1;
1215 vppcom_cfg (&vcm->cfg);
1216 vcl_cfg = &vcm->cfg;
1217
1218 vcm->main_cpu = pthread_self ();
1219 vcm->main_pid = getpid ();
1220 vcm->app_name = format (0, "%s", app_name);
Florin Coras88001c62019-04-24 14:44:46 -07001221 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1222 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001223 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1224 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001225 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001226 atexit (vppcom_app_exit);
Florin Corasb88de902020-09-08 16:47:57 -07001227 vcl_elog_init (vcm);
Florin Coras47c40e22018-11-26 17:01:36 -08001228
1229 /* Allocate default worker */
1230 vcl_worker_alloc_and_init ();
1231
Florin Coras935ce752020-09-08 22:43:47 -07001232 if ((rv = vcl_api_attach ()))
Florin Corasb88de902020-09-08 16:47:57 -07001233 return rv;
Florin Coras47c40e22018-11-26 17:01:36 -08001234
1235 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
Florin Corascc7c88e2020-09-15 15:56:51 -07001236 vcm->workers[0].api_client_handle, vcm->workers[0].api_client_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001237
1238 return VPPCOM_OK;
1239}
1240
1241void
1242vppcom_app_destroy (void)
1243{
Florin Corasdc0ded72020-04-30 02:59:55 +00001244 vcl_worker_t *wrk, *current_wrk;
Damjan Marion4537c302020-09-28 19:03:37 +02001245 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001246
Florin Coras940f78f2018-11-30 12:11:20 -08001247 if (!pool_elts (vcm->workers))
1248 return;
1249
Florin Coras0d427d82018-06-27 03:24:07 -07001250 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001251
Florin Corasdc0ded72020-04-30 02:59:55 +00001252 current_wrk = vcl_worker_get_current ();
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001253
Florin Corasce815de2020-04-16 18:47:27 +00001254 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01001255 pool_foreach (wrk, vcm->workers) {
Florin Corasdc0ded72020-04-30 02:59:55 +00001256 if (current_wrk != wrk)
1257 vcl_worker_cleanup (wrk, 0 /* notify vpp */ );
Damjan Marionb2c31b62020-12-13 21:47:40 +01001258 }
Florin Corasce815de2020-04-16 18:47:27 +00001259 /* *INDENT-ON* */
1260
Florin Coras935ce752020-09-08 22:43:47 -07001261 vcl_api_detach (current_wrk);
Florin Corasdc0ded72020-04-30 02:59:55 +00001262 vcl_worker_cleanup (current_wrk, 0 /* notify vpp */ );
1263
Florin Coras0d427d82018-06-27 03:24:07 -07001264 vcl_elog_stop (vcm);
Florin Corasce815de2020-04-16 18:47:27 +00001265
1266 /*
1267 * Free the heap and fix vcm
1268 */
1269 heap = clib_mem_get_heap ();
Damjan Marion4537c302020-09-28 19:03:37 +02001270 munmap (clib_mem_get_heap_base (heap), clib_mem_get_heap_size (heap));
Florin Corasce815de2020-04-16 18:47:27 +00001271
1272 vcm = &_vppcom_main;
Florin Coras69d97252020-05-11 17:19:31 +00001273 vcm->is_init = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001274}
1275
1276int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001277vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001278{
Florin Coras134a9962018-08-28 11:32:04 -07001279 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001280 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001281
Florin Coras134a9962018-08-28 11:32:04 -07001282 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001283
Florin Coras7e12d942018-06-27 14:32:43 -07001284 session->session_type = proto;
Florin Corasc127d5a2020-10-14 16:35:58 -07001285 session->session_state = VCL_STATE_CLOSED;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001286 session->vpp_handle = ~0;
Florin Corasa5a9efd2021-01-05 17:03:29 -08001287 session->ckpair_index = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001288 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001289
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001290 if (is_nonblocking)
Florin Corasac422d62020-10-19 20:51:36 -07001291 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001292
Florin Coras7e12d942018-06-27 14:32:43 -07001293 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1294 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001295
Florin Coras5e062572019-03-14 19:07:51 -07001296 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001297
Florin Coras134a9962018-08-28 11:32:04 -07001298 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001299}
1300
1301int
Florin Corasc127d5a2020-10-14 16:35:58 -07001302vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * s,
Florin Corasf9240dc2019-01-15 08:03:17 -08001303 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001304{
Florin Coras134a9962018-08-28 11:32:04 -07001305 int rv = VPPCOM_OK;
Florin Coras47c40e22018-11-26 17:01:36 -08001306
Florin Coras6c3b2182020-10-19 18:36:48 -07001307 VDBG (1, "session %u [0x%llx] closing", s->session_index, s->vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001308
Florin Coras6c3b2182020-10-19 18:36:48 -07001309 if (s->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace543852a2017-08-03 02:11:34 -04001310 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001311 u32 next_sh = s->vep.next_sh;
Florin Coras134a9962018-08-28 11:32:04 -07001312 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001313 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001314 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001315 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001316 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras6c3b2182020-10-19 18:36:48 -07001317 " failed! rv %d (%s)", s->vpp_handle, next_sh,
1318 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Florin Corasc127d5a2020-10-14 16:35:58 -07001319 next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001320 }
Florin Corase4306b62020-10-28 12:51:10 -07001321 goto free_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001322 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001323
Florin Coras6c3b2182020-10-19 18:36:48 -07001324 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001325 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001326 rv = vppcom_epoll_ctl (s->vep.vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001327 if (rv < 0)
1328 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
Florin Coras6c3b2182020-10-19 18:36:48 -07001329 "failed! rv %d (%s)", s->session_index, s->vpp_handle,
1330 s->vep.vep_sh, rv, vppcom_retval_str (rv));
Dave Wallace19481612017-09-15 18:47:44 -04001331 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001332
Florin Coras9ace36d2019-10-28 13:14:17 -07001333 if (!do_disconnect)
1334 {
1335 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Coras6c3b2182020-10-19 18:36:48 -07001336 s->session_index, s->vpp_handle);
Florin Coras9ace36d2019-10-28 13:14:17 -07001337 goto cleanup;
1338 }
1339
Florin Coras6c3b2182020-10-19 18:36:48 -07001340 if (s->session_state == VCL_STATE_LISTEN)
Florin Coras9ace36d2019-10-28 13:14:17 -07001341 {
1342 rv = vppcom_session_unbind (sh);
1343 if (PREDICT_FALSE (rv < 0))
1344 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
Florin Coras6c3b2182020-10-19 18:36:48 -07001345 "rv %d (%s)", s->session_index, s->vpp_handle, rv,
Florin Coras9ace36d2019-10-28 13:14:17 -07001346 vppcom_retval_str (rv));
1347 return rv;
1348 }
Florin Coras1bc919e2020-10-18 20:17:49 -07001349 else if (vcl_session_is_ready (s)
Florin Corasc127d5a2020-10-14 16:35:58 -07001350 || (vcl_session_is_connectable_listener (wrk, s)))
Florin Coras9ace36d2019-10-28 13:14:17 -07001351 {
1352 rv = vppcom_session_disconnect (sh);
1353 if (PREDICT_FALSE (rv < 0))
1354 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
Florin Coras6c3b2182020-10-19 18:36:48 -07001355 " rv %d (%s)", s->session_index, s->vpp_handle,
Florin Coras9ace36d2019-10-28 13:14:17 -07001356 rv, vppcom_retval_str (rv));
1357 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001358 else if (s->session_state == VCL_STATE_DISCONNECT)
Florin Coras9ace36d2019-10-28 13:14:17 -07001359 {
Florin Coras52dd29f2020-11-18 19:02:17 -08001360 vcl_send_session_reset_reply (wrk, s, 0);
Florin Coras9ace36d2019-10-28 13:14:17 -07001361 }
Florin Coras6c3b2182020-10-19 18:36:48 -07001362 else if (s->session_state == VCL_STATE_DETACHED)
Florin Corasadcfb152020-02-12 08:50:29 +00001363 {
1364 /* Should not happen. VPP cleaned up before app confirmed close */
Florin Corasc127d5a2020-10-14 16:35:58 -07001365 VDBG (0, "vpp freed session %d before close", s->session_index);
Florin Corasadcfb152020-02-12 08:50:29 +00001366 goto free_session;
1367 }
Florin Coras9ace36d2019-10-28 13:14:17 -07001368
Florin Corasc127d5a2020-10-14 16:35:58 -07001369 s->session_state = VCL_STATE_CLOSED;
Florin Coras54140622020-02-04 19:04:34 +00001370
Florin Coras9ace36d2019-10-28 13:14:17 -07001371 /* Session is removed only after vpp confirms the disconnect */
1372 return rv;
Florin Coras0ef8ef22019-01-18 08:37:13 -08001373
Florin Corasf9240dc2019-01-15 08:03:17 -08001374cleanup:
Florin Coras6c3b2182020-10-19 18:36:48 -07001375 vcl_session_table_del_vpp_handle (wrk, s->vpp_handle);
Florin Corasadcfb152020-02-12 08:50:29 +00001376free_session:
Florin Corasc127d5a2020-10-14 16:35:58 -07001377 vcl_session_free (wrk, s);
1378 vcl_evt (VCL_EVT_CLOSE, s, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001379
Dave Wallace543852a2017-08-03 02:11:34 -04001380 return rv;
1381}
1382
1383int
Florin Corasf9240dc2019-01-15 08:03:17 -08001384vppcom_session_close (uint32_t session_handle)
1385{
1386 vcl_worker_t *wrk = vcl_worker_get_current ();
1387 vcl_session_t *session;
1388
1389 session = vcl_session_get_w_handle (wrk, session_handle);
1390 if (!session)
1391 return VPPCOM_EBADFD;
1392 return vcl_session_cleanup (wrk, session, session_handle,
1393 1 /* do_disconnect */ );
1394}
1395
1396int
Florin Coras134a9962018-08-28 11:32:04 -07001397vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001398{
Florin Coras134a9962018-08-28 11:32:04 -07001399 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001400 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001401
1402 if (!ep || !ep->ip)
1403 return VPPCOM_EINVAL;
1404
Florin Coras134a9962018-08-28 11:32:04 -07001405 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001406 if (!session)
1407 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001408
Florin Coras6c3b2182020-10-19 18:36:48 -07001409 if (session->flags & VCL_SESSION_F_IS_VEP)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001410 {
Florin Coras5e062572019-03-14 19:07:51 -07001411 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1412 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001413 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001414 }
1415
Florin Coras7e12d942018-06-27 14:32:43 -07001416 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001417 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001418 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1419 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001420 else
Dave Barach178cf492018-11-13 16:34:13 -05001421 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1422 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001423 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001424
Florin Coras5e062572019-03-14 19:07:51 -07001425 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1426 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001427 session->transport.is_ip4 ? "IPv4" : "IPv6",
1428 format_ip46_address, &session->transport.lcl_ip,
1429 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1430 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001431 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001432 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001433
1434 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001435 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001436
Florin Coras070453d2018-08-24 17:04:27 -07001437 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001438}
1439
1440int
Florin Coras134a9962018-08-28 11:32:04 -07001441vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001442{
Florin Coras134a9962018-08-28 11:32:04 -07001443 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001444 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001445 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001446 int rv;
1447
Florin Coras134a9962018-08-28 11:32:04 -07001448 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras6c3b2182020-10-19 18:36:48 -07001449 if (!listen_session || (listen_session->flags & VCL_SESSION_F_IS_VEP))
Florin Coras070453d2018-08-24 17:04:27 -07001450 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001451
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001452 if (q_len == 0 || q_len == ~0)
1453 q_len = vcm->cfg.listen_queue_size;
1454
Dave Wallaceee45d412017-11-24 21:44:06 -05001455 listen_vpp_handle = listen_session->vpp_handle;
Florin Corasc127d5a2020-10-14 16:35:58 -07001456 if (listen_session->session_state == VCL_STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001457 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001458 VDBG (0, "session %u [0x%llx]: already in listen state!",
1459 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001460 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001461 }
1462
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001463 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001464
Florin Coras070453d2018-08-24 17:04:27 -07001465 /*
1466 * Send listen request to vpp and wait for reply
1467 */
Florin Coras458089b2019-08-21 16:20:44 -07001468 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001469 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
Florin Corasc127d5a2020-10-14 16:35:58 -07001470 VCL_STATE_LISTEN,
Florin Coras134a9962018-08-28 11:32:04 -07001471 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001472
Florin Coras070453d2018-08-24 17:04:27 -07001473 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001474 {
Florin Coras134a9962018-08-28 11:32:04 -07001475 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001476 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1477 listen_sh, listen_session->vpp_handle, rv,
1478 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001479 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001480 }
1481
Florin Coras070453d2018-08-24 17:04:27 -07001482 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001483}
1484
Florin Coras134a9962018-08-28 11:32:04 -07001485static int
Florin Coras5e062572019-03-14 19:07:51 -07001486validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001487{
Florin Coras6c3b2182020-10-19 18:36:48 -07001488 if (ls->flags & VCL_SESSION_F_IS_VEP)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001489 {
Florin Coras5e062572019-03-14 19:07:51 -07001490 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1491 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001492 return VPPCOM_EBADFD;
1493 }
1494
Florin Corasc127d5a2020-10-14 16:35:58 -07001495 if ((ls->session_state != VCL_STATE_LISTEN)
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001496 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001497 {
Florin Coras6c3b2182020-10-19 18:36:48 -07001498 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001499 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001500 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001501 return VPPCOM_EBADFD;
1502 }
1503 return VPPCOM_OK;
1504}
1505
1506int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001507vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1508{
1509 if (!strcmp (proto_str, "TCP"))
1510 *proto = VPPCOM_PROTO_TCP;
1511 else if (!strcmp (proto_str, "tcp"))
1512 *proto = VPPCOM_PROTO_TCP;
1513 else if (!strcmp (proto_str, "UDP"))
1514 *proto = VPPCOM_PROTO_UDP;
1515 else if (!strcmp (proto_str, "udp"))
1516 *proto = VPPCOM_PROTO_UDP;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001517 else if (!strcmp (proto_str, "TLS"))
1518 *proto = VPPCOM_PROTO_TLS;
1519 else if (!strcmp (proto_str, "tls"))
1520 *proto = VPPCOM_PROTO_TLS;
1521 else if (!strcmp (proto_str, "QUIC"))
1522 *proto = VPPCOM_PROTO_QUIC;
1523 else if (!strcmp (proto_str, "quic"))
1524 *proto = VPPCOM_PROTO_QUIC;
1525 else
1526 return 1;
1527 return 0;
1528}
1529
1530int
Florin Coras134a9962018-08-28 11:32:04 -07001531vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001532 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001533{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001534 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001535 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001536 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001537 vcl_session_t *listen_session = 0;
1538 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001539 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001540 svm_msg_q_msg_t msg;
1541 session_event_t *e;
1542 u8 is_nonblocking;
1543 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001544
Florin Coras134a9962018-08-28 11:32:04 -07001545 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001546 if (!listen_session)
1547 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001548
Florin Coras134a9962018-08-28 11:32:04 -07001549 listen_session_index = listen_session->session_index;
1550 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001551 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001552
Florin Coras54693d22018-07-17 10:46:29 -07001553 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001554 {
Florin Coras54693d22018-07-17 10:46:29 -07001555 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001556 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001557 accepted_msg = evt->accepted_msg;
1558 goto handle;
1559 }
1560
Florin Corasac422d62020-10-19 20:51:36 -07001561 is_nonblocking = vcl_session_has_attr (listen_session,
1562 VCL_SESS_ATTR_NONBLOCK);
Florin Coras54693d22018-07-17 10:46:29 -07001563 while (1)
1564 {
Carl Smith592a9092019-11-12 14:57:37 +13001565 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
1566 return VPPCOM_EAGAIN;
1567
Florin Coras134a9962018-08-28 11:32:04 -07001568 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001569 return VPPCOM_EAGAIN;
1570
Florin Coras134a9962018-08-28 11:32:04 -07001571 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001572 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001573 {
wanghanlin96453fd2019-12-16 19:14:39 +08001574 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -07001575 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001576 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001577 }
Dave Barach178cf492018-11-13 16:34:13 -05001578 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001579 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001580 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001581 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001582
Florin Coras54693d22018-07-17 10:46:29 -07001583handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001584
Florin Coras00cca802019-06-06 09:38:44 -07001585 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1586 listen_session_index);
1587 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1588 return VPPCOM_ECONNABORTED;
1589
Florin Coras134a9962018-08-28 11:32:04 -07001590 listen_session = vcl_session_get (wrk, listen_session_index);
1591 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001592
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001593 if (flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07001594 vcl_session_set_attr (client_session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001595
Florin Coras5e062572019-03-14 19:07:51 -07001596 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1597 " flags %d, is_nonblocking %u", listen_session->session_index,
1598 listen_session->vpp_handle, client_session_index,
1599 client_session->vpp_handle, flags,
Florin Corasac422d62020-10-19 20:51:36 -07001600 vcl_session_has_attr (client_session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001601
Dave Wallace048b1d62018-01-03 22:24:41 -05001602 if (ep)
1603 {
Florin Coras7e12d942018-06-27 14:32:43 -07001604 ep->is_ip4 = client_session->transport.is_ip4;
1605 ep->port = client_session->transport.rmt_port;
1606 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001607 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1608 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001609 else
Dave Barach178cf492018-11-13 16:34:13 -05001610 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1611 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001612 }
Dave Wallace60caa062017-11-10 17:07:13 -05001613
Florin Coras05ecfcc2018-12-12 18:19:39 -08001614 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001615 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001616 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001617 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001618 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001619 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001620 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001621 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001622 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001623 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1624 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001625
Florin Coras3c7d4f92018-12-14 11:28:43 -08001626 /*
1627 * Session might have been closed already
1628 */
1629 if (accept_flags)
1630 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001631 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasc127d5a2020-10-14 16:35:58 -07001632 client_session->session_state = VCL_STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001633 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasc127d5a2020-10-14 16:35:58 -07001634 client_session->session_state = VCL_STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001635 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001636 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001637}
1638
1639int
Florin Coras134a9962018-08-28 11:32:04 -07001640vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001641{
Florin Coras134a9962018-08-28 11:32:04 -07001642 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001643 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001644 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001645 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001646
Florin Coras134a9962018-08-28 11:32:04 -07001647 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001648 if (!session)
1649 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001650 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001651
Florin Coras6c3b2182020-10-19 18:36:48 -07001652 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04001653 {
Florin Coras5e062572019-03-14 19:07:51 -07001654 VDBG (0, "ERROR: cannot connect epoll session %u!",
1655 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001656 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001657 }
1658
Florin Corasc127d5a2020-10-14 16:35:58 -07001659 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Dave Wallace543852a2017-08-03 02:11:34 -04001660 {
Florin Coras7baeb712019-01-04 17:05:43 -08001661 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001662 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001663 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001664 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001665 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001666 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001667 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001668 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001669 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001670 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001671 }
1672
Florin Coras0a1e1832020-03-29 18:54:04 +00001673 /* Attempt to connect a connectionless listener */
Florin Corasc127d5a2020-10-14 16:35:58 -07001674 if (PREDICT_FALSE (session->session_state == VCL_STATE_LISTEN))
Florin Coras0a1e1832020-03-29 18:54:04 +00001675 {
1676 if (session->session_type != VPPCOM_PROTO_UDP)
1677 return VPPCOM_EINVAL;
1678 vcl_send_session_unlisten (wrk, session);
Florin Corasc127d5a2020-10-14 16:35:58 -07001679 session->session_state = VCL_STATE_CLOSED;
Florin Coras0a1e1832020-03-29 18:54:04 +00001680 }
1681
Florin Coras7e12d942018-06-27 14:32:43 -07001682 session->transport.is_ip4 = server_ep->is_ip4;
Florin Corasef7cbf62019-10-17 09:56:27 -07001683 vcl_ip_copy_from_ep (&session->transport.rmt_ip, server_ep);
Florin Coras7e12d942018-06-27 14:32:43 -07001684 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001685 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Florin Coras0a1e1832020-03-29 18:54:04 +00001686 session->flags |= VCL_SESSION_F_CONNECTED;
Dave Wallace543852a2017-08-03 02:11:34 -04001687
Florin Coras0a1e1832020-03-29 18:54:04 +00001688 VDBG (0, "session handle %u (%s): connecting to peer %s %U "
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001689 "port %d proto %s", session_handle,
Florin Coras0a1e1832020-03-29 18:54:04 +00001690 vppcom_session_state_str (session->session_state),
Florin Coras7e12d942018-06-27 14:32:43 -07001691 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001692 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001693 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001694 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001695 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001696 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001697
Florin Coras458089b2019-08-21 16:20:44 -07001698 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001699
Florin Corasac422d62020-10-19 20:51:36 -07001700 if (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK))
Florin Corasa5ea8212020-08-24 21:23:51 -07001701 {
fanyfa49d59d2020-10-13 17:07:16 +08001702 /* State set to STATE_UPDATED to ensure the session is not assumed
Florin Corasdfffdd72020-10-15 10:54:47 -07001703 * to be ready and to also allow the app to close it prior to vpp's
fanyfa49d59d2020-10-13 17:07:16 +08001704 * connected reply. */
Florin Corasc127d5a2020-10-14 16:35:58 -07001705 session->session_state = VCL_STATE_UPDATED;
Florin Corasa5ea8212020-08-24 21:23:51 -07001706 return VPPCOM_EINPROGRESS;
1707 }
Florin Coras57c88932019-08-29 12:03:17 -07001708
1709 /*
1710 * Wait for reply from vpp if blocking
1711 */
Florin Corasdfffdd72020-10-15 10:54:47 -07001712 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Florin Coras070453d2018-08-24 17:04:27 -07001713 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001714
Florin Coras134a9962018-08-28 11:32:04 -07001715 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001716 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1717 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001718
Dave Wallace4878cbe2017-11-21 03:45:09 -05001719 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001720}
1721
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001722int
1723vppcom_session_stream_connect (uint32_t session_handle,
1724 uint32_t parent_session_handle)
1725{
1726 vcl_worker_t *wrk = vcl_worker_get_current ();
1727 vcl_session_t *session, *parent_session;
1728 u32 session_index, parent_session_index;
1729 int rv;
1730
1731 session = vcl_session_get_w_handle (wrk, session_handle);
1732 if (!session)
1733 return VPPCOM_EBADFD;
1734 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1735 if (!parent_session)
1736 return VPPCOM_EBADFD;
1737
1738 session_index = session->session_index;
1739 parent_session_index = parent_session->session_index;
Florin Coras6c3b2182020-10-19 18:36:48 -07001740 if (PREDICT_FALSE (session->flags & VCL_SESSION_F_IS_VEP))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001741 {
1742 VDBG (0, "ERROR: cannot connect epoll session %u!",
1743 session->session_index);
1744 return VPPCOM_EBADFD;
1745 }
1746
Florin Corasc127d5a2020-10-14 16:35:58 -07001747 if (PREDICT_FALSE (vcl_session_is_ready (session)))
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001748 {
1749 VDBG (0, "session handle %u [0x%llx]: session already "
1750 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1751 session_handle, session->vpp_handle,
1752 parent_session_handle, parent_session->vpp_handle,
1753 vppcom_proto_str (session->session_type), session->session_state,
1754 vppcom_session_state_str (session->session_state));
1755 return VPPCOM_OK;
1756 }
1757
1758 /* Connect to quic session specifics */
1759 session->transport.is_ip4 = parent_session->transport.is_ip4;
1760 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1761 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001762 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001763
1764 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1765 session_handle, parent_session_handle, parent_session->vpp_handle);
1766
1767 /*
1768 * Send connect request and wait for reply from vpp
1769 */
Florin Coras458089b2019-08-21 16:20:44 -07001770 vcl_send_session_connect (wrk, session);
Florin Corasdfffdd72020-10-15 10:54:47 -07001771 rv = vppcom_wait_for_session_state_change (session_index, VCL_STATE_READY,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001772 vcm->cfg.session_timeout);
1773
1774 session->listener_index = parent_session_index;
1775 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001776 if (parent_session)
1777 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001778
1779 session = vcl_session_get (wrk, session_index);
1780 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1781 session->vpp_handle, rv ? "failed" : "succeeded");
1782
1783 return rv;
1784}
1785
Florin Coras54693d22018-07-17 10:46:29 -07001786static u8
1787vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1788{
Florin Corasc0737e92019-03-04 14:19:39 -08001789 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001790}
1791
Steven58f464e2017-10-25 12:33:12 -07001792static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001793vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001794 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001795{
Florin Coras134a9962018-08-28 11:32:04 -07001796 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras4a2c7942020-09-10 12:27:14 -07001797 int rv, n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001798 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001799 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001800 svm_msg_q_msg_t msg;
1801 session_event_t *e;
1802 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001803 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001804
Florin Coras070453d2018-08-24 17:04:27 -07001805 if (PREDICT_FALSE (!buf))
1806 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001807
Florin Coras134a9962018-08-28 11:32:04 -07001808 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001809 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras070453d2018-08-24 17:04:27 -07001810 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001811
Florin Coras6d0106e2019-01-29 20:11:58 -08001812 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001813 {
Florin Coras5e062572019-03-14 19:07:51 -07001814 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001815 s->session_index, s->vpp_handle, s->session_state,
1816 vppcom_session_state_str (s->session_state));
1817 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001818 }
1819
Florin Corasac422d62020-10-19 20:51:36 -07001820 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001821 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001822 mq = wrk->app_event_queue;
1823 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001824 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001825
Sirshak Das28aa5392019-02-05 01:33:33 -06001826 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001827 {
Florin Coras54693d22018-07-17 10:46:29 -07001828 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001829 {
Yu Pingb2955352019-12-04 06:49:04 +08001830 if (vcl_session_is_closing (s))
1831 return vcl_session_closing_error (s);
Florin Corasd6894562020-10-28 00:37:15 -07001832 if (is_ct)
1833 svm_fifo_unset_event (s->rx_fifo);
1834 svm_fifo_unset_event (rx_fifo);
Florin Corasc0737e92019-03-04 14:19:39 -08001835 return VPPCOM_EWOULDBLOCK;
1836 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001837 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001838 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001839 if (vcl_session_is_closing (s))
1840 return vcl_session_closing_error (s);
1841
Florin Corasd6894562020-10-28 00:37:15 -07001842 if (is_ct)
1843 svm_fifo_unset_event (s->rx_fifo);
1844 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001845 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001846 if (svm_msg_q_is_empty (mq))
1847 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001848
Florin Coras54693d22018-07-17 10:46:29 -07001849 svm_msg_q_sub_w_lock (mq, &msg);
1850 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001851 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001852 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001853 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001854 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001855 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001856 }
Florin Coras54693d22018-07-17 10:46:29 -07001857
Florin Coras4a2c7942020-09-10 12:27:14 -07001858read_again:
1859
Florin Coras460dce62018-07-27 05:45:06 -07001860 if (s->is_dgram)
Florin Coras4a2c7942020-09-10 12:27:14 -07001861 rv = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001862 else
Florin Coras4a2c7942020-09-10 12:27:14 -07001863 rv = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
1864
1865 ASSERT (rv >= 0);
1866 n_read += rv;
Florin Coras54693d22018-07-17 10:46:29 -07001867
Sirshak Das28aa5392019-02-05 01:33:33 -06001868 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc34118b2020-08-12 18:58:25 -07001869 {
Florin Corasd6894562020-10-28 00:37:15 -07001870 if (is_ct)
1871 svm_fifo_unset_event (s->rx_fifo);
1872 svm_fifo_unset_event (rx_fifo);
Florin Corasc34118b2020-08-12 18:58:25 -07001873 if (!svm_fifo_is_empty_cons (rx_fifo)
Florin Corasd6894562020-10-28 00:37:15 -07001874 && svm_fifo_set_event (rx_fifo) && is_nonblocking)
Florin Corasc34118b2020-08-12 18:58:25 -07001875 {
Florin Corasc34118b2020-08-12 18:58:25 -07001876 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1877 e->event_type = SESSION_IO_EVT_RX;
1878 e->session_index = s->session_index;
1879 }
1880 }
Florin Coras54fe32c2020-11-25 20:26:32 -08001881 else if (PREDICT_FALSE (rv < n && !s->is_dgram))
Florin Coras4a2c7942020-09-10 12:27:14 -07001882 {
1883 /* More data enqueued while reading. Try to drain it
Florin Coras54fe32c2020-11-25 20:26:32 -08001884 * or fill the buffer. Avoid doing that for dgrams */
Florin Coras4a2c7942020-09-10 12:27:14 -07001885 buf += rv;
1886 n -= rv;
1887 goto read_again;
1888 }
Florin Coras41c9e042018-09-11 00:10:41 -07001889
Florin Coras17ec5772020-08-12 20:46:29 -07001890 if (PREDICT_FALSE (svm_fifo_needs_deq_ntf (rx_fifo, n_read)))
Florin Coras317b8e02019-04-17 09:57:46 -07001891 {
Florin Coras17ec5772020-08-12 20:46:29 -07001892 svm_fifo_clear_deq_ntf (rx_fifo);
Florin Corasc547e912020-12-08 17:50:45 -08001893 app_send_io_evt_to_vpp (s->vpp_evt_q,
1894 s->rx_fifo->shr->master_session_index,
Florin Coras317b8e02019-04-17 09:57:46 -07001895 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras317b8e02019-04-17 09:57:46 -07001896 }
1897
Florin Coras5e062572019-03-14 19:07:51 -07001898 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1899 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001900
Florin Coras54693d22018-07-17 10:46:29 -07001901 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001902}
1903
Steven58f464e2017-10-25 12:33:12 -07001904int
Florin Coras134a9962018-08-28 11:32:04 -07001905vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001906{
Florin Coras134a9962018-08-28 11:32:04 -07001907 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001908}
1909
1910static int
Florin Coras134a9962018-08-28 11:32:04 -07001911vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001912{
Florin Coras134a9962018-08-28 11:32:04 -07001913 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001914}
1915
Florin Coras2cba8532018-09-11 16:33:36 -07001916int
1917vppcom_session_read_segments (uint32_t session_handle,
Florin Corasd68faf82020-09-25 15:18:13 -07001918 vppcom_data_segment_t * ds, uint32_t n_segments,
1919 uint32_t max_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07001920{
1921 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001922 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001923 vcl_session_t *s = 0;
1924 svm_fifo_t *rx_fifo;
1925 svm_msg_q_msg_t msg;
1926 session_event_t *e;
1927 svm_msg_q_t *mq;
1928 u8 is_ct;
1929
1930 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07001931 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07001932 return VPPCOM_EBADFD;
1933
Florin Coras6d0106e2019-01-29 20:11:58 -08001934 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1935 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001936
Florin Corasac422d62020-10-19 20:51:36 -07001937 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Florin Coras2cba8532018-09-11 16:33:36 -07001938 is_ct = vcl_session_is_ct (s);
Florin Corasac422d62020-10-19 20:51:36 -07001939 mq = wrk->app_event_queue;
Florin Coras62877022020-10-28 21:22:04 -07001940 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07001941 s->flags &= ~VCL_SESSION_F_HAS_RX_EVT;
Florin Coras2cba8532018-09-11 16:33:36 -07001942
Sirshak Das28aa5392019-02-05 01:33:33 -06001943 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001944 {
1945 if (is_nonblocking)
1946 {
Florin Coras62877022020-10-28 21:22:04 -07001947 if (is_ct)
1948 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001949 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001950 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001951 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001952 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001953 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001954 if (vcl_session_is_closing (s))
1955 return vcl_session_closing_error (s);
1956
Florin Coras62877022020-10-28 21:22:04 -07001957 if (is_ct)
1958 svm_fifo_unset_event (s->rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001959 svm_fifo_unset_event (rx_fifo);
1960 svm_msg_q_lock (mq);
1961 if (svm_msg_q_is_empty (mq))
1962 svm_msg_q_wait (mq);
1963
1964 svm_msg_q_sub_w_lock (mq, &msg);
1965 e = svm_msg_q_msg_data (mq, &msg);
1966 svm_msg_q_unlock (mq);
1967 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001968 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001969 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001970 }
1971 }
1972
Florin Corasd1cc38d2020-10-11 11:05:04 -07001973 n_read = svm_fifo_segments (rx_fifo, s->rx_bytes_pending,
1974 (svm_fifo_seg_t *) ds, n_segments, max_bytes);
1975 if (n_read < 0)
1976 return VPPCOM_EAGAIN;
1977
1978 if (svm_fifo_max_dequeue_cons (rx_fifo) == n_read)
1979 {
Florin Coras62877022020-10-28 21:22:04 -07001980 if (is_ct)
1981 svm_fifo_unset_event (s->rx_fifo);
1982 svm_fifo_unset_event (rx_fifo);
Florin Corasd1cc38d2020-10-11 11:05:04 -07001983 if (svm_fifo_max_dequeue_cons (rx_fifo) != n_read
Florin Coras62877022020-10-28 21:22:04 -07001984 && svm_fifo_set_event (rx_fifo)
Florin Corasac422d62020-10-19 20:51:36 -07001985 && vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK))
Florin Corasd1cc38d2020-10-11 11:05:04 -07001986 {
1987 session_event_t *e;
1988 vec_add2 (wrk->unhandled_evts_vector, e, 1);
1989 e->event_type = SESSION_IO_EVT_RX;
1990 e->session_index = s->session_index;
1991 }
1992 }
1993
1994 s->rx_bytes_pending += n_read;
Florin Coras2cba8532018-09-11 16:33:36 -07001995 return n_read;
1996}
1997
1998void
Florin Corasd68faf82020-09-25 15:18:13 -07001999vppcom_session_free_segments (uint32_t session_handle, uint32_t n_bytes)
Florin Coras2cba8532018-09-11 16:33:36 -07002000{
2001 vcl_worker_t *wrk = vcl_worker_get_current ();
2002 vcl_session_t *s;
Florin Coras62877022020-10-28 21:22:04 -07002003 u8 is_ct;
Florin Coras2cba8532018-09-11 16:33:36 -07002004
2005 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras6c3b2182020-10-19 18:36:48 -07002006 if (PREDICT_FALSE (!s || (s->flags & VCL_SESSION_F_IS_VEP)))
Florin Coras2cba8532018-09-11 16:33:36 -07002007 return;
2008
Florin Coras62877022020-10-28 21:22:04 -07002009 is_ct = vcl_session_is_ct (s);
2010 svm_fifo_dequeue_drop (is_ct ? s->ct_rx_fifo : s->rx_fifo, n_bytes);
Florin Corasd1cc38d2020-10-11 11:05:04 -07002011
2012 ASSERT (s->rx_bytes_pending < n_bytes);
2013 s->rx_bytes_pending -= n_bytes;
Florin Coras2cba8532018-09-11 16:33:36 -07002014}
2015
Florin Coras54693d22018-07-17 10:46:29 -07002016static u8
2017vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
2018{
Florin Corasc0737e92019-03-04 14:19:39 -08002019 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04002020}
2021
Florin Coras7a2abce2020-04-05 19:25:44 +00002022always_inline u8
2023vcl_fifo_is_writeable (svm_fifo_t * f, u32 len, u8 is_dgram)
Dave Wallace543852a2017-08-03 02:11:34 -04002024{
Florin Coras7a2abce2020-04-05 19:25:44 +00002025 u32 max_enq = svm_fifo_max_enqueue_prod (f);
2026 if (is_dgram)
2027 return max_enq >= (sizeof (session_dgram_hdr_t) + len);
2028 else
2029 return max_enq > 0;
Florin Coras7a2abce2020-04-05 19:25:44 +00002030}
2031
2032always_inline int
2033vppcom_session_write_inline (vcl_worker_t * wrk, vcl_session_t * s, void *buf,
2034 size_t n, u8 is_flush, u8 is_dgram)
2035{
Florin Coras6d0106e2019-01-29 20:11:58 -08002036 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07002037 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07002038 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08002039 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07002040 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07002041 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07002042 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04002043
jiangxiaomingff31ac62019-11-21 23:34:56 +08002044 if (PREDICT_FALSE (!buf || n == 0))
Florin Coras070453d2018-08-24 17:04:27 -07002045 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04002046
Florin Coras6c3b2182020-10-19 18:36:48 -07002047 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallace543852a2017-08-03 02:11:34 -04002048 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002049 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
2050 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002051 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04002052 }
2053
Florin Coras6d0106e2019-01-29 20:11:58 -08002054 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002055 {
Florin Coras6d0106e2019-01-29 20:11:58 -08002056 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
2057 s->session_index, s->vpp_handle, s->session_state,
2058 vppcom_session_state_str (s->session_state));
2059 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002060 }
2061
Florin Coras0e88e852018-09-17 22:09:02 -07002062 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08002063 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Corasac422d62020-10-19 20:51:36 -07002064 is_nonblocking = vcl_session_has_attr (s, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06002065
Florin Coras653e43f2019-03-04 10:56:23 -08002066 mq = wrk->app_event_queue;
Florin Coras7a2abce2020-04-05 19:25:44 +00002067 if (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Dave Wallace543852a2017-08-03 02:11:34 -04002068 {
Florin Coras54693d22018-07-17 10:46:29 -07002069 if (is_nonblocking)
2070 {
Florin Coras070453d2018-08-24 17:04:27 -07002071 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07002072 }
Florin Coras7a2abce2020-04-05 19:25:44 +00002073 while (!vcl_fifo_is_writeable (tx_fifo, n, is_dgram))
Florin Coras54693d22018-07-17 10:46:29 -07002074 {
Florin Coras2d379d82019-06-28 12:45:12 -07002075 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08002076 if (vcl_session_is_closing (s))
2077 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07002078 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07002079 if (svm_msg_q_is_empty (mq))
2080 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07002081
Florin Coras54693d22018-07-17 10:46:29 -07002082 svm_msg_q_sub_w_lock (mq, &msg);
2083 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07002084 svm_msg_q_unlock (mq);
2085
Florin Coras0e88e852018-09-17 22:09:02 -07002086 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07002087 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07002088 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07002089 }
Dave Wallace543852a2017-08-03 02:11:34 -04002090 }
Dave Wallace543852a2017-08-03 02:11:34 -04002091
Florin Coras653e43f2019-03-04 10:56:23 -08002092 et = SESSION_IO_EVT_TX;
2093 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08002094 et = SESSION_IO_EVT_TX_FLUSH;
2095
Florin Coras7a2abce2020-04-05 19:25:44 +00002096 if (is_dgram)
Florin Coras460dce62018-07-27 05:45:06 -07002097 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08002098 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002099 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07002100 else
2101 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08002102 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08002103
Florin Coras14ed6df2019-03-06 21:13:42 -08002104 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc547e912020-12-08 17:50:45 -08002105 app_send_io_evt_to_vpp (
2106 s->vpp_evt_q, s->tx_fifo->shr->master_session_index, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002107
Florin Coras56230092020-09-02 20:52:58 -07002108 /* The underlying fifo segment can run out of memory */
2109 if (PREDICT_FALSE (n_write < 0))
2110 return VPPCOM_EAGAIN;
Dave Wallace543852a2017-08-03 02:11:34 -04002111
Florin Coras6d0106e2019-01-29 20:11:58 -08002112 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
2113 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07002114
Florin Coras54693d22018-07-17 10:46:29 -07002115 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04002116}
2117
Florin Coras42ceddb2018-12-12 10:56:01 -08002118int
2119vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
2120{
Florin Coras7a2abce2020-04-05 19:25:44 +00002121 vcl_worker_t *wrk = vcl_worker_get_current ();
2122 vcl_session_t *s;
2123
2124 s = vcl_session_get_w_handle (wrk, session_handle);
2125 if (PREDICT_FALSE (!s))
2126 return VPPCOM_EBADFD;
2127
2128 return vppcom_session_write_inline (wrk, s, buf, n,
2129 0 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Coras42ceddb2018-12-12 10:56:01 -08002130}
2131
Florin Corasb0f662f2018-12-27 14:51:46 -08002132int
2133vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
2134{
Florin Coras7a2abce2020-04-05 19:25:44 +00002135 vcl_worker_t *wrk = vcl_worker_get_current ();
2136 vcl_session_t *s;
2137
2138 s = vcl_session_get_w_handle (wrk, session_handle);
2139 if (PREDICT_FALSE (!s))
2140 return VPPCOM_EBADFD;
2141
2142 return vppcom_session_write_inline (wrk, s, buf, n,
2143 1 /* is_flush */ , s->is_dgram ? 1 : 0);
Florin Corasb0f662f2018-12-27 14:51:46 -08002144}
2145
Florin Corasc0737e92019-03-04 14:19:39 -08002146#define vcl_fifo_rx_evt_valid_or_break(_s) \
Florin Corasbd52e462019-10-28 13:22:37 -07002147if (PREDICT_FALSE (!_s->rx_fifo)) \
2148 break; \
Florin Corasc0737e92019-03-04 14:19:39 -08002149if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
2150 { \
2151 if (!vcl_session_is_ct (_s)) \
2152 { \
2153 svm_fifo_unset_event (_s->rx_fifo); \
2154 if (svm_fifo_is_empty (_s->rx_fifo)) \
2155 break; \
2156 } \
2157 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2158 { \
Florin Coras2f630182020-08-13 00:17:51 -07002159 svm_fifo_unset_event (_s->rx_fifo); /* rx evts on actual fifo*/ \
Florin Corasc0737e92019-03-04 14:19:39 -08002160 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
2161 break; \
2162 } \
2163 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07002164
Florin Coras86f04502018-09-12 16:08:01 -07002165static void
2166vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2167 unsigned long n_bits, unsigned long *read_map,
2168 unsigned long *write_map,
2169 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07002170{
2171 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002172 session_connected_msg_t *connected_msg;
Florin Corasb242d312020-10-26 15:35:40 -07002173 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002174 u32 sid;
2175
2176 switch (e->event_type)
2177 {
Florin Corasc0737e92019-03-04 14:19:39 -08002178 case SESSION_IO_EVT_RX:
2179 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002180 s = vcl_session_get (wrk, sid);
2181 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002182 break;
Florin Corasb242d312020-10-26 15:35:40 -07002183 vcl_fifo_rx_evt_valid_or_break (s);
Florin Coras86f04502018-09-12 16:08:01 -07002184 if (sid < n_bits && read_map)
2185 {
David Johnsond9818dd2018-12-14 14:53:41 -05002186 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002187 *bits_set += 1;
2188 }
2189 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002190 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002191 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002192 s = vcl_session_get (wrk, sid);
2193 if (!s || !vcl_session_is_open (s))
Florin Coras86f04502018-09-12 16:08:01 -07002194 break;
2195 if (sid < n_bits && write_map)
2196 {
David Johnsond9818dd2018-12-14 14:53:41 -05002197 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002198 *bits_set += 1;
2199 }
2200 break;
Florin Coras86f04502018-09-12 16:08:01 -07002201 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002202 if (!e->postponed)
2203 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2204 else
2205 s = vcl_session_get (wrk, e->session_index);
2206 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002207 break;
Florin Corasb242d312020-10-26 15:35:40 -07002208 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002209 if (sid < n_bits && read_map)
2210 {
David Johnsond9818dd2018-12-14 14:53:41 -05002211 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002212 *bits_set += 1;
2213 }
2214 break;
2215 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002216 if (!e->postponed)
2217 {
2218 connected_msg = (session_connected_msg_t *) e->data;
2219 sid = vcl_session_connected_handler (wrk, connected_msg);
2220 }
2221 else
2222 sid = e->session_index;
Florin Coras57c88932019-08-29 12:03:17 -07002223 if (sid == VCL_INVALID_SESSION_INDEX)
2224 break;
2225 if (sid < n_bits && write_map)
2226 {
2227 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2228 *bits_set += 1;
2229 }
Florin Coras86f04502018-09-12 16:08:01 -07002230 break;
2231 case SESSION_CTRL_EVT_DISCONNECTED:
2232 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002233 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2234 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002235 break;
Florin Corasb242d312020-10-26 15:35:40 -07002236 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002237 if (sid < n_bits && except_map)
2238 {
David Johnsond9818dd2018-12-14 14:53:41 -05002239 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002240 *bits_set += 1;
2241 }
2242 break;
2243 case SESSION_CTRL_EVT_RESET:
2244 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2245 if (sid < n_bits && except_map)
2246 {
David Johnsond9818dd2018-12-14 14:53:41 -05002247 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002248 *bits_set += 1;
2249 }
2250 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002251 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2252 vcl_session_unlisten_reply_handler (wrk, e->data);
2253 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002254 case SESSION_CTRL_EVT_MIGRATED:
2255 vcl_session_migrated_handler (wrk, e->data);
2256 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002257 case SESSION_CTRL_EVT_CLEANUP:
2258 vcl_session_cleanup_handler (wrk, e->data);
2259 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002260 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2261 vcl_session_worker_update_reply_handler (wrk, e->data);
2262 break;
2263 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2264 vcl_session_req_worker_update_handler (wrk, e->data);
2265 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002266 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2267 vcl_session_app_add_segment_handler (wrk, e->data);
2268 break;
2269 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2270 vcl_session_app_del_segment_handler (wrk, e->data);
2271 break;
hanlina3a48962020-07-13 11:09:15 +08002272 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002273 vcl_worker_rpc_handler (wrk, e->data);
2274 break;
Florin Coras86f04502018-09-12 16:08:01 -07002275 default:
2276 clib_warning ("unhandled: %u", e->event_type);
2277 break;
2278 }
2279}
2280
2281static int
2282vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2283 unsigned long n_bits, unsigned long *read_map,
2284 unsigned long *write_map, unsigned long *except_map,
2285 double time_to_wait, u32 * bits_set)
2286{
Florin Coras99368312018-08-02 10:45:44 -07002287 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002288 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002289 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002290
2291 svm_msg_q_lock (mq);
2292 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002293 {
Florin Coras54693d22018-07-17 10:46:29 -07002294 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002295 {
Florin Coras54693d22018-07-17 10:46:29 -07002296 svm_msg_q_unlock (mq);
2297 return 0;
2298 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002299
Florin Coras54693d22018-07-17 10:46:29 -07002300 if (!time_to_wait)
2301 {
2302 svm_msg_q_unlock (mq);
2303 return 0;
2304 }
2305 else if (time_to_wait < 0)
2306 {
2307 svm_msg_q_wait (mq);
2308 }
2309 else
2310 {
2311 if (svm_msg_q_timedwait (mq, time_to_wait))
2312 {
2313 svm_msg_q_unlock (mq);
2314 return 0;
2315 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002316 }
2317 }
Florin Corase003a1b2019-06-05 10:47:16 -07002318 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002319 svm_msg_q_unlock (mq);
2320
Florin Coras134a9962018-08-28 11:32:04 -07002321 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002322 {
Florin Coras134a9962018-08-28 11:32:04 -07002323 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002324 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002325 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2326 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002327 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002328 }
Florin Coras134a9962018-08-28 11:32:04 -07002329 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002330 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002331 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002332}
2333
Florin Coras99368312018-08-02 10:45:44 -07002334static int
Florin Coras294afe22019-01-07 17:49:17 -08002335vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2336 vcl_si_set * read_map, vcl_si_set * write_map,
2337 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002338 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002339{
Florin Coras14ed6df2019-03-06 21:13:42 -08002340 double wait = 0, start = 0;
2341
2342 if (!*bits_set)
2343 {
2344 wait = time_to_wait;
2345 start = clib_time_now (&wrk->clib_time);
2346 }
2347
2348 do
2349 {
2350 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2351 write_map, except_map, wait, bits_set);
2352 if (*bits_set)
2353 return *bits_set;
2354 if (wait == -1)
2355 continue;
2356
2357 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2358 }
2359 while (wait > 0);
2360
2361 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002362}
2363
2364static int
Florin Coras294afe22019-01-07 17:49:17 -08002365vppcom_select_eventfd (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{
2370 vcl_mq_evt_conn_t *mqc;
2371 int __clib_unused n_read;
2372 int n_mq_evts, i;
2373 u64 buf;
2374
Florin Coras134a9962018-08-28 11:32:04 -07002375 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2376 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2377 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002378 for (i = 0; i < n_mq_evts; i++)
2379 {
Florin Coras134a9962018-08-28 11:32:04 -07002380 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002381 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002382 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002383 except_map, 0, bits_set);
2384 }
2385
2386 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2387}
2388
Dave Wallace543852a2017-08-03 02:11:34 -04002389int
Florin Coras294afe22019-01-07 17:49:17 -08002390vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2391 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002392{
Florin Coras54693d22018-07-17 10:46:29 -07002393 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002394 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras096a1d52021-01-27 15:33:51 -08002395 vcl_session_t *s = 0;
Florin Corasf49cf472020-04-19 23:12:08 +00002396 int i;
Dave Wallace543852a2017-08-03 02:11:34 -04002397
Dave Wallace7876d392017-10-19 03:53:57 -04002398 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002399 {
Florin Coras134a9962018-08-28 11:32:04 -07002400 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002401 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002402 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2403 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002404 }
Dave Wallace7876d392017-10-19 03:53:57 -04002405 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002406 {
Florin Coras134a9962018-08-28 11:32:04 -07002407 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002408 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002409 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2410 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002411 }
Dave Wallace7876d392017-10-19 03:53:57 -04002412 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002413 {
Florin Coras134a9962018-08-28 11:32:04 -07002414 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002415 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002416 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2417 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002418 }
2419
Florin Coras54693d22018-07-17 10:46:29 -07002420 if (!n_bits)
2421 return 0;
2422
2423 if (!write_map)
2424 goto check_rd;
2425
Florin Coras096a1d52021-01-27 15:33:51 -08002426 clib_bitmap_foreach (sid, wrk->wr_bitmap)
2427 {
2428 if (!(s = vcl_session_get (wrk, sid)))
2429 {
2430 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2431 bits_set++;
2432 continue;
2433 }
Florin Coras54693d22018-07-17 10:46:29 -07002434
Florin Coras096a1d52021-01-27 15:33:51 -08002435 if (vcl_session_write_ready (s))
2436 {
2437 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2438 bits_set++;
2439 }
2440 else
2441 {
2442 svm_fifo_t *txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2443 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF);
2444 }
2445 }
Florin Coras54693d22018-07-17 10:46:29 -07002446
2447check_rd:
2448 if (!read_map)
2449 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002450
Florin Coras096a1d52021-01-27 15:33:51 -08002451 clib_bitmap_foreach (sid, wrk->rd_bitmap)
2452 {
2453 if (!(s = vcl_session_get (wrk, sid)))
2454 {
2455 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2456 bits_set++;
2457 continue;
2458 }
Florin Coras54693d22018-07-17 10:46:29 -07002459
Florin Coras096a1d52021-01-27 15:33:51 -08002460 if (vcl_session_read_ready (s))
2461 {
2462 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
2463 bits_set++;
2464 }
2465 }
Florin Coras54693d22018-07-17 10:46:29 -07002466
2467check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002468
Florin Coras86f04502018-09-12 16:08:01 -07002469 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2470 {
2471 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2472 read_map, write_map, except_map, &bits_set);
2473 }
2474 vec_reset_length (wrk->unhandled_evts_vector);
2475
Florin Coras99368312018-08-02 10:45:44 -07002476 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002477 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002478 time_to_wait, &bits_set);
2479 else
Florin Coras134a9962018-08-28 11:32:04 -07002480 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002481 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002482
Dave Wallace543852a2017-08-03 02:11:34 -04002483 return (bits_set);
2484}
2485
Dave Wallacef7f809c2017-10-03 01:48:42 -04002486static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002487vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002488{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002489 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002490 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002491 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002492
Florin Corasc0737e92019-03-04 14:19:39 -08002493 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002494 return;
2495
Florin Coras6c3b2182020-10-19 18:36:48 -07002496 s = vcl_session_get_w_handle (wrk, vep_handle);
2497 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002498 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002499 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002500 goto done;
2501 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002502 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002503 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002504 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002505 goto done;
2506 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002507 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002508 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002509 "{\n"
2510 " is_vep = %u\n"
2511 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002512 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002513 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2514 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002515
Florin Coras7e5e62b2019-07-30 14:08:23 -07002516 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002517 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002518 s = vcl_session_get_w_handle (wrk, sh);
2519 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002520 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002521 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002522 goto done;
2523 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002524 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002525 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002526 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002527 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002528 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002529 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002530 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002531 goto done;
2532 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002533 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002534 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2535 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002536 sh, s->vep.vep_sh, vep_handle);
2537 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002538 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002539 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002540 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002541 " next_sh = 0x%x (%u)\n"
2542 " prev_sh = 0x%x (%u)\n"
2543 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002544 " ev.events = 0x%x\n"
2545 " ev.data.u64 = 0x%llx\n"
2546 " et_mask = 0x%x\n"
2547 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002548 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002549 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2550 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002551 }
2552 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002553
2554done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002555 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002556}
2557
2558int
2559vppcom_epoll_create (void)
2560{
Florin Coras134a9962018-08-28 11:32:04 -07002561 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002562 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002563
Florin Coras134a9962018-08-28 11:32:04 -07002564 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002565
Florin Coras6c3b2182020-10-19 18:36:48 -07002566 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002567 vep_session->vep.vep_sh = ~0;
2568 vep_session->vep.next_sh = ~0;
2569 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002570 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002571
Florin Corasa7a1a222018-12-30 17:11:31 -08002572 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2573 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002574
Florin Corasab2f6db2018-08-31 14:31:41 -07002575 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002576}
2577
2578int
Florin Coras134a9962018-08-28 11:32:04 -07002579vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002580 struct epoll_event *event)
2581{
Florin Coras134a9962018-08-28 11:32:04 -07002582 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002583 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002584 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002585 vcl_session_t *s;
2586 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002587
Florin Coras134a9962018-08-28 11:32:04 -07002588 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002589 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002590 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002591 return VPPCOM_EINVAL;
2592 }
2593
Florin Coras134a9962018-08-28 11:32:04 -07002594 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002595 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002596 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002597 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002598 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002599 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002600 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002601 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002602 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002603 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002604 }
2605
Florin Coras134a9962018-08-28 11:32:04 -07002606 ASSERT (vep_session->vep.vep_sh == ~0);
2607 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002608
Florin Coras17ec5772020-08-12 20:46:29 -07002609 s = vcl_session_get_w_handle (wrk, session_handle);
2610 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002611 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002612 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002613 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002614 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002615 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002616 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002617 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002618 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002619 }
2620
2621 switch (op)
2622 {
2623 case EPOLL_CTL_ADD:
2624 if (PREDICT_FALSE (!event))
2625 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002626 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002627 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002628 }
Florin Coras134a9962018-08-28 11:32:04 -07002629 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002630 {
Florin Coras7e12d942018-06-27 14:32:43 -07002631 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002632 next_session = vcl_session_get_w_handle (wrk,
2633 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002634 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002635 {
Florin Coras5e062572019-03-14 19:07:51 -07002636 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002637 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002638 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002639 }
Florin Coras134a9962018-08-28 11:32:04 -07002640 ASSERT (next_session->vep.prev_sh == vep_handle);
2641 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002642 }
Florin Coras17ec5772020-08-12 20:46:29 -07002643 s->vep.next_sh = vep_session->vep.next_sh;
2644 s->vep.prev_sh = vep_handle;
2645 s->vep.vep_sh = vep_handle;
2646 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2647 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002648 s->flags &= ~VCL_SESSION_F_IS_VEP;
2649 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002650 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002651
Florin Coras17ec5772020-08-12 20:46:29 -07002652 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2653 if (txf && (event->events & EPOLLOUT))
2654 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002655
Florin Coras6e3c1f82020-01-15 01:30:46 +00002656 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002657 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002658 {
2659 session_event_t e = { 0 };
2660 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002661 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002662 vec_add1 (wrk->unhandled_evts_vector, e);
2663 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002664 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002665 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002666 {
2667 session_event_t e = { 0 };
2668 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002669 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002670 vec_add1 (wrk->unhandled_evts_vector, e);
2671 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002672 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2673 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002674 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002675 break;
2676
2677 case EPOLL_CTL_MOD:
2678 if (PREDICT_FALSE (!event))
2679 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002680 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002681 rv = VPPCOM_EINVAL;
2682 goto done;
2683 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002684 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002685 {
Florin Coras5e062572019-03-14 19:07:51 -07002686 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002687 rv = VPPCOM_EINVAL;
2688 goto done;
2689 }
Florin Coras17ec5772020-08-12 20:46:29 -07002690 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002691 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002692 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002693 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002694 rv = VPPCOM_EINVAL;
2695 goto done;
2696 }
hanlin475c9d72019-12-26 11:44:28 +08002697
2698 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2699 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002700 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002701 {
2702 session_event_t e = { 0 };
2703 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002704 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002705 vec_add1 (wrk->unhandled_evts_vector, e);
2706 }
Florin Coras17ec5772020-08-12 20:46:29 -07002707 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2708 s->vep.ev = *event;
2709 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2710 if (event->events & EPOLLOUT)
2711 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2712 else
2713 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasa7a1a222018-12-30 17:11:31 -08002714 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2715 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002716 break;
2717
2718 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002719 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002720 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002721 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002722 rv = VPPCOM_EINVAL;
2723 goto done;
2724 }
Florin Coras17ec5772020-08-12 20:46:29 -07002725 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002726 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002727 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002728 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002729 rv = VPPCOM_EINVAL;
2730 goto done;
2731 }
2732
Florin Coras17ec5772020-08-12 20:46:29 -07002733 if (s->vep.prev_sh == vep_handle)
2734 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002735 else
2736 {
Florin Coras7e12d942018-06-27 14:32:43 -07002737 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002738 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002739 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002740 {
Florin Coras5e062572019-03-14 19:07:51 -07002741 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002742 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002743 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002744 }
Florin Coras134a9962018-08-28 11:32:04 -07002745 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002746 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002747 }
Florin Coras17ec5772020-08-12 20:46:29 -07002748 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002749 {
Florin Coras7e12d942018-06-27 14:32:43 -07002750 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002751 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002752 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002753 {
Florin Coras5e062572019-03-14 19:07:51 -07002754 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002755 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002756 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757 }
Florin Coras134a9962018-08-28 11:32:04 -07002758 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002759 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002760 }
2761
Florin Coras17ec5772020-08-12 20:46:29 -07002762 memset (&s->vep, 0, sizeof (s->vep));
2763 s->vep.next_sh = ~0;
2764 s->vep.prev_sh = ~0;
2765 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002766 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002767
Florin Coras17ec5772020-08-12 20:46:29 -07002768 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2769 if (txf)
2770 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002771
Florin Coras5e062572019-03-14 19:07:51 -07002772 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002773 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002774 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002775 break;
2776
2777 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002778 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002779 rv = VPPCOM_EINVAL;
2780 }
2781
Florin Coras134a9962018-08-28 11:32:04 -07002782 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002783
2784done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002785 return rv;
2786}
2787
Florin Coras86f04502018-09-12 16:08:01 -07002788static inline void
2789vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2790 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002791{
2792 session_disconnected_msg_t *disconnected_msg;
2793 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002794 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002795 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002796 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002797 u8 add_event = 0;
2798
2799 switch (e->event_type)
2800 {
Florin Coras653e43f2019-03-04 10:56:23 -08002801 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002802 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002803 s = vcl_session_get (wrk, sid);
2804 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002805 break;
Florin Corasb242d312020-10-26 15:35:40 -07002806 vcl_fifo_rx_evt_valid_or_break (s);
2807 session_events = s->vep.ev.events;
2808 if (!(EPOLLIN & s->vep.ev.events)
2809 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002810 break;
2811 add_event = 1;
2812 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002813 session_evt_data = s->vep.ev.data.u64;
2814 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002815 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002816 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002817 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002818 s = vcl_session_get (wrk, sid);
2819 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002820 break;
Florin Corasb242d312020-10-26 15:35:40 -07002821 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002822 if (!(EPOLLOUT & session_events))
2823 break;
2824 add_event = 1;
2825 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002826 session_evt_data = s->vep.ev.data.u64;
2827 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2828 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002829 break;
Florin Coras86f04502018-09-12 16:08:01 -07002830 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002831 if (!e->postponed)
2832 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2833 else
2834 s = vcl_session_get (wrk, e->session_index);
2835 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002836 break;
Florin Corasb242d312020-10-26 15:35:40 -07002837 session_events = s->vep.ev.events;
2838 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002839 if (!(EPOLLIN & session_events))
2840 break;
Florin Coras86f04502018-09-12 16:08:01 -07002841 add_event = 1;
2842 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002843 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002844 break;
2845 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002846 if (!e->postponed)
2847 {
2848 connected_msg = (session_connected_msg_t *) e->data;
2849 sid = vcl_session_connected_handler (wrk, connected_msg);
2850 }
2851 else
2852 sid = e->session_index;
2853 s = vcl_session_get (wrk, sid);
2854 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002855 break;
Florin Corasb242d312020-10-26 15:35:40 -07002856 session_events = s->vep.ev.events;
2857 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002858 if (!(EPOLLOUT & session_events))
2859 break;
2860 add_event = 1;
2861 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002862 session_evt_data = s->vep.ev.data.u64;
2863 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002864 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002865 break;
2866 case SESSION_CTRL_EVT_DISCONNECTED:
2867 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002868 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2869 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002870 break;
Florin Corasb242d312020-10-26 15:35:40 -07002871 sid = s->session_index;
2872 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002873 add_event = 1;
2874 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002875 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002876 break;
2877 case SESSION_CTRL_EVT_RESET:
2878 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002879 s = vcl_session_get (wrk, sid);
2880 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002881 break;
Florin Corasb242d312020-10-26 15:35:40 -07002882 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002883 add_event = 1;
2884 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002885 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002886 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002887 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2888 vcl_session_unlisten_reply_handler (wrk, e->data);
2889 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002890 case SESSION_CTRL_EVT_MIGRATED:
2891 vcl_session_migrated_handler (wrk, e->data);
2892 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002893 case SESSION_CTRL_EVT_CLEANUP:
2894 vcl_session_cleanup_handler (wrk, e->data);
2895 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002896 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2897 vcl_session_req_worker_update_handler (wrk, e->data);
2898 break;
2899 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2900 vcl_session_worker_update_reply_handler (wrk, e->data);
2901 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002902 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2903 vcl_session_app_add_segment_handler (wrk, e->data);
2904 break;
2905 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2906 vcl_session_app_del_segment_handler (wrk, e->data);
2907 break;
hanlina3a48962020-07-13 11:09:15 +08002908 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002909 vcl_worker_rpc_handler (wrk, e->data);
2910 break;
Florin Coras86f04502018-09-12 16:08:01 -07002911 default:
2912 VDBG (0, "unhandled: %u", e->event_type);
2913 break;
2914 }
2915
2916 if (add_event)
2917 {
2918 events[*num_ev].data.u64 = session_evt_data;
2919 if (EPOLLONESHOT & session_events)
2920 {
Florin Corasb242d312020-10-26 15:35:40 -07002921 s = vcl_session_get (wrk, sid);
2922 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002923 }
2924 *num_ev += 1;
2925 }
2926}
2927
2928static int
2929vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2930 struct epoll_event *events, u32 maxevents,
2931 double wait_for_time, u32 * num_ev)
2932{
Florin Coras99368312018-08-02 10:45:44 -07002933 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002934 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002935 int i;
2936
Florin Coras539663c2018-09-28 14:59:37 -07002937 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2938 goto handle_dequeued;
2939
Florin Coras54693d22018-07-17 10:46:29 -07002940 svm_msg_q_lock (mq);
2941 if (svm_msg_q_is_empty (mq))
2942 {
2943 if (!wait_for_time)
2944 {
2945 svm_msg_q_unlock (mq);
2946 return 0;
2947 }
2948 else if (wait_for_time < 0)
2949 {
2950 svm_msg_q_wait (mq);
2951 }
2952 else
2953 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002954 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002955 {
2956 svm_msg_q_unlock (mq);
2957 return 0;
2958 }
2959 }
2960 }
Florin Corase003a1b2019-06-05 10:47:16 -07002961 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08002962 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002963 svm_msg_q_unlock (mq);
2964
Florin Coras539663c2018-09-28 14:59:37 -07002965handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002966 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002967 {
Florin Coras134a9962018-08-28 11:32:04 -07002968 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002969 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08002970 if (*num_ev < maxevents)
2971 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2972 else
2973 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07002974 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002975 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002976 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002977 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002978 return *num_ev;
2979}
2980
Florin Coras99368312018-08-02 10:45:44 -07002981static int
Florin Coras134a9962018-08-28 11:32:04 -07002982vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002983 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002984{
Florin Corase003a1b2019-06-05 10:47:16 -07002985 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002986
2987 if (!n_evts)
2988 {
2989 wait = wait_for_time;
2990 start = clib_time_now (&wrk->clib_time);
2991 }
2992
2993 do
2994 {
2995 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2996 wait, &n_evts);
2997 if (n_evts)
2998 return n_evts;
2999 if (wait == -1)
3000 continue;
3001
Florin Corase003a1b2019-06-05 10:47:16 -07003002 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07003003 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07003004 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003005 }
3006 while (wait > 0);
3007
3008 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003009}
3010
3011static int
Florin Coras134a9962018-08-28 11:32:04 -07003012vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003013 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003014{
Florin Corasb13ba132021-01-27 16:05:24 -08003015 double wait = 0, start = 0, now;
Florin Coras99368312018-08-02 10:45:44 -07003016 int __clib_unused n_read;
Florin Corasb13ba132021-01-27 16:05:24 -08003017 vcl_mq_evt_conn_t *mqc;
Florin Coras99368312018-08-02 10:45:44 -07003018 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07003019 u64 buf;
3020
Florin Coras134a9962018-08-28 11:32:04 -07003021 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasb13ba132021-01-27 16:05:24 -08003022 if (!n_evts)
Florin Coras99368312018-08-02 10:45:44 -07003023 {
Florin Corasb13ba132021-01-27 16:05:24 -08003024 wait = wait_for_time;
3025 start = clib_time_now (&wrk->clib_time);
Florin Coras99368312018-08-02 10:45:44 -07003026 }
3027
Florin Corasb13ba132021-01-27 16:05:24 -08003028 do
3029 {
3030 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
3031 vec_len (wrk->mq_events), wait);
3032 if (n_mq_evts < 0)
3033 {
3034 VDBG (0, "epoll_wait error %u", errno);
3035 return n_evts;
3036 }
3037
3038 for (i = 0; i < n_mq_evts; i++)
3039 {
3040 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
3041 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
3042 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0,
3043 &n_evts);
3044 }
3045
3046 if (n_evts)
3047 return n_evts;
3048 if (wait == -1)
3049 continue;
3050
3051 now = clib_time_now (&wrk->clib_time);
3052 wait -= (now - start) * 1e3;
3053 start = now;
3054 }
3055 while (wait > 0);
3056
3057 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003058}
3059
Dave Wallacef7f809c2017-10-03 01:48:42 -04003060int
Florin Coras134a9962018-08-28 11:32:04 -07003061vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003062 int maxevents, double wait_for_time)
3063{
Florin Coras134a9962018-08-28 11:32:04 -07003064 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003065 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003066 u32 n_evts = 0;
3067 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003068
3069 if (PREDICT_FALSE (maxevents <= 0))
3070 {
Florin Coras5e062572019-03-14 19:07:51 -07003071 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003072 return VPPCOM_EINVAL;
3073 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003074
Florin Coras134a9962018-08-28 11:32:04 -07003075 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003076 if (!vep_session)
3077 return VPPCOM_EBADFD;
3078
Florin Coras6c3b2182020-10-19 18:36:48 -07003079 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003080 {
Florin Coras5e062572019-03-14 19:07:51 -07003081 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003082 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003083 }
Florin Coras54693d22018-07-17 10:46:29 -07003084
3085 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003086
Florin Coras86f04502018-09-12 16:08:01 -07003087 if (vec_len (wrk->unhandled_evts_vector))
3088 {
3089 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3090 {
3091 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3092 events, &n_evts);
3093 if (n_evts == maxevents)
3094 {
Florin Corase003a1b2019-06-05 10:47:16 -07003095 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3096 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003097 }
3098 }
Florin Corase003a1b2019-06-05 10:47:16 -07003099 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003100 }
3101
3102 if (vcm->cfg.use_mq_eventfd)
3103 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3104 wait_for_time);
3105
3106 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3107 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003108}
3109
Dave Wallace35830af2017-10-09 01:43:42 -04003110int
Florin Coras134a9962018-08-28 11:32:04 -07003111vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003112 void *buffer, uint32_t * buflen)
3113{
Florin Coras134a9962018-08-28 11:32:04 -07003114 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08003115 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003116 vppcom_endpt_t *ep = buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003117 vcl_session_t *session;
3118 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003119
Florin Coras134a9962018-08-28 11:32:04 -07003120 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003121 if (!session)
3122 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003123
Dave Wallace35830af2017-10-09 01:43:42 -04003124 switch (op)
3125 {
3126 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003127 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003128 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3129 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003130 break;
3131
Dave Wallace227867f2017-11-13 21:21:53 -05003132 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003133 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003134 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3135 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003136 break;
3137
3138 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003139 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003140 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003141 *flags =
3142 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003143 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003144 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003145 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003146 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3147 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003148 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003149 }
3150 else
3151 rv = VPPCOM_EINVAL;
3152 break;
3153
3154 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003155 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003156 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003157 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003158 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003159 else
Florin Corasac422d62020-10-19 20:51:36 -07003160 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003161
Florin Coras5e062572019-03-14 19:07:51 -07003162 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3163 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003164 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003165 }
3166 else
3167 rv = VPPCOM_EINVAL;
3168 break;
3169
3170 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003171 if (PREDICT_TRUE (buffer && buflen &&
3172 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003173 {
Florin Coras7e12d942018-06-27 14:32:43 -07003174 ep->is_ip4 = session->transport.is_ip4;
3175 ep->port = session->transport.rmt_port;
3176 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003177 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3178 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003179 else
Dave Barach178cf492018-11-13 16:34:13 -05003180 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3181 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003182 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003183 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3184 "addr = %U, port %u", session_handle, ep->is_ip4,
3185 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003186 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3187 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003188 }
3189 else
3190 rv = VPPCOM_EINVAL;
3191 break;
3192
3193 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003194 if (PREDICT_TRUE (buffer && buflen &&
3195 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003196 {
Florin Coras7e12d942018-06-27 14:32:43 -07003197 ep->is_ip4 = session->transport.is_ip4;
3198 ep->port = session->transport.lcl_port;
3199 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003200 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3201 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003202 else
Dave Barach178cf492018-11-13 16:34:13 -05003203 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3204 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003205 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003206 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3207 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003208 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003209 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3210 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003211 }
3212 else
3213 rv = VPPCOM_EINVAL;
3214 break;
Stevenb5a11602017-10-11 09:59:30 -07003215
Florin Corasef7cbf62019-10-17 09:56:27 -07003216 case VPPCOM_ATTR_SET_LCL_ADDR:
3217 if (PREDICT_TRUE (buffer && buflen &&
3218 (*buflen >= sizeof (*ep)) && ep->ip))
3219 {
3220 session->transport.is_ip4 = ep->is_ip4;
3221 session->transport.lcl_port = ep->port;
3222 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3223 *buflen = sizeof (*ep);
3224 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3225 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3226 &session->transport.lcl_ip,
3227 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3228 clib_net_to_host_u16 (ep->port));
3229 }
3230 else
3231 rv = VPPCOM_EINVAL;
3232 break;
3233
Dave Wallace048b1d62018-01-03 22:24:41 -05003234 case VPPCOM_ATTR_GET_LIBC_EPFD:
3235 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003236 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003237 break;
3238
3239 case VPPCOM_ATTR_SET_LIBC_EPFD:
3240 if (PREDICT_TRUE (buffer && buflen &&
3241 (*buflen == sizeof (session->libc_epfd))))
3242 {
3243 session->libc_epfd = *(int *) buffer;
3244 *buflen = sizeof (session->libc_epfd);
3245
Florin Coras5e062572019-03-14 19:07:51 -07003246 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3247 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003248 }
3249 else
3250 rv = VPPCOM_EINVAL;
3251 break;
3252
3253 case VPPCOM_ATTR_GET_PROTOCOL:
3254 if (buffer && buflen && (*buflen >= sizeof (int)))
3255 {
Florin Coras7e12d942018-06-27 14:32:43 -07003256 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003257 *buflen = sizeof (int);
3258
Florin Coras5e062572019-03-14 19:07:51 -07003259 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3260 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003261 }
3262 else
3263 rv = VPPCOM_EINVAL;
3264 break;
3265
3266 case VPPCOM_ATTR_GET_LISTEN:
3267 if (buffer && buflen && (*buflen >= sizeof (int)))
3268 {
Florin Corasac422d62020-10-19 20:51:36 -07003269 *(int *) buffer = vcl_session_has_attr (session,
3270 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003271 *buflen = sizeof (int);
3272
Florin Coras5e062572019-03-14 19:07:51 -07003273 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3274 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003275 }
3276 else
3277 rv = VPPCOM_EINVAL;
3278 break;
3279
3280 case VPPCOM_ATTR_GET_ERROR:
3281 if (buffer && buflen && (*buflen >= sizeof (int)))
3282 {
3283 *(int *) buffer = 0;
3284 *buflen = sizeof (int);
3285
Florin Coras5e062572019-03-14 19:07:51 -07003286 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3287 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003288 }
3289 else
3290 rv = VPPCOM_EINVAL;
3291 break;
3292
3293 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3294 if (buffer && buflen && (*buflen >= sizeof (u32)))
3295 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003296
3297 /* VPP-TBD */
3298 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003299 session->tx_fifo ?
3300 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003301 vcm->cfg.tx_fifo_size);
3302 *buflen = sizeof (u32);
3303
Florin Coras5e062572019-03-14 19:07:51 -07003304 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3305 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3306 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003307 }
3308 else
3309 rv = VPPCOM_EINVAL;
3310 break;
3311
3312 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3313 if (buffer && buflen && (*buflen == sizeof (u32)))
3314 {
3315 /* VPP-TBD */
3316 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003317 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3318 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3319 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003320 }
3321 else
3322 rv = VPPCOM_EINVAL;
3323 break;
3324
3325 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3326 if (buffer && buflen && (*buflen >= sizeof (u32)))
3327 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003328
3329 /* VPP-TBD */
3330 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003331 session->rx_fifo ?
3332 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003333 vcm->cfg.rx_fifo_size);
3334 *buflen = sizeof (u32);
3335
Florin Coras5e062572019-03-14 19:07:51 -07003336 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3337 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003338 }
3339 else
3340 rv = VPPCOM_EINVAL;
3341 break;
3342
3343 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3344 if (buffer && buflen && (*buflen == sizeof (u32)))
3345 {
3346 /* VPP-TBD */
3347 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003348 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3349 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3350 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003351 }
3352 else
3353 rv = VPPCOM_EINVAL;
3354 break;
3355
3356 case VPPCOM_ATTR_GET_REUSEADDR:
3357 if (buffer && buflen && (*buflen >= sizeof (int)))
3358 {
3359 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003360 *(int *) buffer = vcl_session_has_attr (session,
3361 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003362 *buflen = sizeof (int);
3363
Florin Coras5e062572019-03-14 19:07:51 -07003364 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3365 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003366 }
3367 else
3368 rv = VPPCOM_EINVAL;
3369 break;
3370
Stevenb5a11602017-10-11 09:59:30 -07003371 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003372 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003373 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003374 {
3375 /* VPP-TBD */
3376 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003377 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003378 else
Florin Corasac422d62020-10-19 20:51:36 -07003379 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003380
Florin Coras5e062572019-03-14 19:07:51 -07003381 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003382 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003383 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003384 }
3385 else
3386 rv = VPPCOM_EINVAL;
3387 break;
3388
3389 case VPPCOM_ATTR_GET_REUSEPORT:
3390 if (buffer && buflen && (*buflen >= sizeof (int)))
3391 {
3392 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003393 *(int *) buffer = vcl_session_has_attr (session,
3394 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003395 *buflen = sizeof (int);
3396
Florin Coras5e062572019-03-14 19:07:51 -07003397 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3398 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003399 }
3400 else
3401 rv = VPPCOM_EINVAL;
3402 break;
3403
3404 case VPPCOM_ATTR_SET_REUSEPORT:
3405 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003406 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003407 {
3408 /* VPP-TBD */
3409 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003410 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 else
Florin Corasac422d62020-10-19 20:51:36 -07003412 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003413
Florin Coras5e062572019-03-14 19:07:51 -07003414 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003415 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003416 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003417 }
3418 else
3419 rv = VPPCOM_EINVAL;
3420 break;
3421
3422 case VPPCOM_ATTR_GET_BROADCAST:
3423 if (buffer && buflen && (*buflen >= sizeof (int)))
3424 {
3425 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003426 *(int *) buffer = vcl_session_has_attr (session,
3427 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003428 *buflen = sizeof (int);
3429
Florin Coras5e062572019-03-14 19:07:51 -07003430 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3431 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003432 }
3433 else
3434 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003435 break;
3436
3437 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003438 if (buffer && buflen && (*buflen == sizeof (int)))
3439 {
3440 /* VPP-TBD */
3441 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003442 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003443 else
Florin Corasac422d62020-10-19 20:51:36 -07003444 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003445
Florin Coras5e062572019-03-14 19:07:51 -07003446 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003447 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003448 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003449 }
3450 else
3451 rv = VPPCOM_EINVAL;
3452 break;
3453
3454 case VPPCOM_ATTR_GET_V6ONLY:
3455 if (buffer && buflen && (*buflen >= sizeof (int)))
3456 {
3457 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003458 *(int *) buffer = vcl_session_has_attr (session,
3459 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003460 *buflen = sizeof (int);
3461
Florin Coras5e062572019-03-14 19:07:51 -07003462 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3463 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003464 }
3465 else
3466 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003467 break;
3468
3469 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003470 if (buffer && buflen && (*buflen == sizeof (int)))
3471 {
3472 /* VPP-TBD */
3473 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003474 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 else
Florin Corasac422d62020-10-19 20:51:36 -07003476 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003477
Florin Coras5e062572019-03-14 19:07:51 -07003478 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003479 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003480 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003481 }
3482 else
3483 rv = VPPCOM_EINVAL;
3484 break;
3485
3486 case VPPCOM_ATTR_GET_KEEPALIVE:
3487 if (buffer && buflen && (*buflen >= sizeof (int)))
3488 {
3489 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003490 *(int *) buffer = vcl_session_has_attr (session,
3491 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003492 *buflen = sizeof (int);
3493
Florin Coras5e062572019-03-14 19:07:51 -07003494 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3495 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003496 }
3497 else
3498 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003499 break;
Stevenbccd3392017-10-12 20:42:21 -07003500
3501 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003502 if (buffer && buflen && (*buflen == sizeof (int)))
3503 {
3504 /* VPP-TBD */
3505 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003506 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003507 else
Florin Corasac422d62020-10-19 20:51:36 -07003508 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003509
Florin Coras5e062572019-03-14 19:07:51 -07003510 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003511 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003512 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003513 }
3514 else
3515 rv = VPPCOM_EINVAL;
3516 break;
3517
3518 case VPPCOM_ATTR_GET_TCP_NODELAY:
3519 if (buffer && buflen && (*buflen >= sizeof (int)))
3520 {
3521 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003522 *(int *) buffer = vcl_session_has_attr (session,
3523 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003524 *buflen = sizeof (int);
3525
Florin Coras5e062572019-03-14 19:07:51 -07003526 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3527 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003528 }
3529 else
3530 rv = VPPCOM_EINVAL;
3531 break;
3532
3533 case VPPCOM_ATTR_SET_TCP_NODELAY:
3534 if (buffer && buflen && (*buflen == sizeof (int)))
3535 {
3536 /* VPP-TBD */
3537 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003538 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003539 else
Florin Corasac422d62020-10-19 20:51:36 -07003540 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003541
Florin Coras5e062572019-03-14 19:07:51 -07003542 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003543 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003544 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003545 }
3546 else
3547 rv = VPPCOM_EINVAL;
3548 break;
3549
3550 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3551 if (buffer && buflen && (*buflen >= sizeof (int)))
3552 {
3553 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003554 *(int *) buffer = vcl_session_has_attr (session,
3555 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003556 *buflen = sizeof (int);
3557
Florin Coras5e062572019-03-14 19:07:51 -07003558 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3559 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003560 }
3561 else
3562 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003563 break;
3564
3565 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003566 if (buffer && buflen && (*buflen == sizeof (int)))
3567 {
3568 /* VPP-TBD */
3569 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003570 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003571 else
Florin Corasac422d62020-10-19 20:51:36 -07003572 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003573
Florin Coras5e062572019-03-14 19:07:51 -07003574 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003575 vcl_session_has_attr (session,
3576 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003577 }
3578 else
3579 rv = VPPCOM_EINVAL;
3580 break;
3581
3582 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3583 if (buffer && buflen && (*buflen >= sizeof (int)))
3584 {
3585 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003586 *(int *) buffer = vcl_session_has_attr (session,
3587 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003588 *buflen = sizeof (int);
3589
Florin Coras5e062572019-03-14 19:07:51 -07003590 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3591 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003592 }
3593 else
3594 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003595 break;
3596
3597 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003598 if (buffer && buflen && (*buflen == sizeof (int)))
3599 {
3600 /* VPP-TBD */
3601 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003602 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003603 else
Florin Corasac422d62020-10-19 20:51:36 -07003604 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003605
Florin Coras5e062572019-03-14 19:07:51 -07003606 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003607 vcl_session_has_attr (session,
3608 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003609 }
3610 else
3611 rv = VPPCOM_EINVAL;
3612 break;
3613
3614 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3615 if (buffer && buflen && (*buflen >= sizeof (u32)))
3616 {
3617 /* VPP-TBD */
3618 *(u32 *) buffer = session->user_mss;
3619 *buflen = sizeof (int);
3620
Florin Coras5e062572019-03-14 19:07:51 -07003621 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3622 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003623 }
3624 else
3625 rv = VPPCOM_EINVAL;
3626 break;
3627
3628 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3629 if (buffer && buflen && (*buflen == sizeof (u32)))
3630 {
3631 /* VPP-TBD */
3632 session->user_mss = *(u32 *) buffer;
3633
Florin Coras5e062572019-03-14 19:07:51 -07003634 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3635 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003636 }
3637 else
3638 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003639 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003640
Florin Coras7baeb712019-01-04 17:05:43 -08003641 case VPPCOM_ATTR_SET_SHUT:
3642 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003643 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003644 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003645 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003646 break;
3647
3648 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003649 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003650 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003651 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003652 tmp_flags |= 2;
3653 if (tmp_flags == 1)
3654 *(int *) buffer = SHUT_RD;
3655 else if (tmp_flags == 2)
3656 *(int *) buffer = SHUT_WR;
3657 else if (tmp_flags == 3)
3658 *(int *) buffer = SHUT_RDWR;
3659 *buflen = sizeof (int);
3660 break;
Florin Coras1e966172020-05-16 18:18:14 +00003661
3662 case VPPCOM_ATTR_SET_CONNECTED:
3663 session->flags |= VCL_SESSION_F_CONNECTED;
3664 break;
3665
Florin Corasa5a9efd2021-01-05 17:03:29 -08003666 case VPPCOM_ATTR_SET_CKPAIR:
3667 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3668 !vcl_session_has_crypto (session))
3669 {
3670 rv = VPPCOM_EINVAL;
3671 break;
3672 }
3673 session->ckpair_index = *(uint32_t *) buffer;
3674 break;
3675
Dave Wallacee22aa742017-10-20 12:30:38 -04003676 default:
3677 rv = VPPCOM_EINVAL;
3678 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003679 }
3680
Dave Wallace35830af2017-10-09 01:43:42 -04003681 return rv;
3682}
3683
Stevenac1f96d2017-10-24 16:03:58 -07003684int
Florin Coras134a9962018-08-28 11:32:04 -07003685vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003686 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3687{
Florin Coras134a9962018-08-28 11:32:04 -07003688 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003689 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003690 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003691
3692 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003693 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003694 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003695 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003696 else
3697 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003698 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003699 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003700 }
3701
Florin Coras0a1e1832020-03-29 18:54:04 +00003702 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003703 {
Florin Coras5da10c42020-04-01 04:31:21 +00003704 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003705 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003706 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3707 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003708 else
Dave Barach178cf492018-11-13 16:34:13 -05003709 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3710 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003711 ep->is_ip4 = session->transport.is_ip4;
3712 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003713 }
Florin Coras460dce62018-07-27 05:45:06 -07003714
Stevenac1f96d2017-10-24 16:03:58 -07003715 return rv;
3716}
3717
3718int
Florin Coras134a9962018-08-28 11:32:04 -07003719vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003720 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3721{
Florin Coras7a2abce2020-04-05 19:25:44 +00003722 vcl_worker_t *wrk = vcl_worker_get_current ();
3723 vcl_session_t *s;
3724
3725 s = vcl_session_get_w_handle (wrk, session_handle);
3726 if (!s)
3727 return VPPCOM_EBADFD;
3728
Dave Wallace617dffa2017-10-26 14:47:06 -04003729 if (!buffer)
3730 return VPPCOM_EINVAL;
3731
3732 if (ep)
3733 {
Florin Coras5824cc52020-10-20 18:44:41 -07003734 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003735 return VPPCOM_EINVAL;
3736
3737 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003738 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003739 {
Florin Coras5824cc52020-10-20 18:44:41 -07003740 u32 session_index = s->session_index;
3741 f64 timeout = vcm->cfg.session_timeout;
3742 int rv;
3743
Florin Coras5da10c42020-04-01 04:31:21 +00003744 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003745 rv = vppcom_wait_for_session_state_change (session_index,
3746 VCL_STATE_READY,
3747 timeout);
3748 if (rv < 0)
3749 return rv;
3750 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003751 }
Florin Coras5824cc52020-10-20 18:44:41 -07003752
3753 s->transport.is_ip4 = ep->is_ip4;
3754 s->transport.rmt_port = ep->port;
3755 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003756 }
3757
3758 if (flags)
3759 {
3760 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003761 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003762 }
3763
Florin Coras7a2abce2020-04-05 19:25:44 +00003764 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3765 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003766}
3767
Dave Wallace048b1d62018-01-03 22:24:41 -05003768int
3769vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3770{
Florin Coras134a9962018-08-28 11:32:04 -07003771 vcl_worker_t *wrk = vcl_worker_get_current ();
3772 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003773 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003774 svm_msg_q_msg_t msg;
3775 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003776 int rv, num_ev = 0;
3777
Florin Coras5e062572019-03-14 19:07:51 -07003778 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003779
3780 if (!vp)
3781 return VPPCOM_EFAULT;
3782
3783 do
3784 {
Florin Coras7e12d942018-06-27 14:32:43 -07003785 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003786
Florin Coras6917b942018-11-13 22:44:54 -08003787 /* Dequeue all events and drop all unhandled io events */
3788 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3789 {
3790 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3791 vcl_handle_mq_event (wrk, e);
3792 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3793 }
3794 vec_reset_length (wrk->unhandled_evts_vector);
3795
Dave Wallace048b1d62018-01-03 22:24:41 -05003796 for (i = 0; i < n_sids; i++)
3797 {
Florin Coras7baeb712019-01-04 17:05:43 -08003798 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003799 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003800 {
3801 vp[i].revents = POLLHUP;
3802 num_ev++;
3803 continue;
3804 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003805
Florin Coras6917b942018-11-13 22:44:54 -08003806 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003807
3808 if (POLLIN & vp[i].events)
3809 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003810 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003811 if (rv > 0)
3812 {
Florin Coras6917b942018-11-13 22:44:54 -08003813 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003814 num_ev++;
3815 }
3816 else if (rv < 0)
3817 {
3818 switch (rv)
3819 {
3820 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003821 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003822 break;
3823
3824 default:
Florin Coras6917b942018-11-13 22:44:54 -08003825 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003826 break;
3827 }
3828 num_ev++;
3829 }
3830 }
3831
3832 if (POLLOUT & vp[i].events)
3833 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003834 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003835 if (rv > 0)
3836 {
Florin Coras6917b942018-11-13 22:44:54 -08003837 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003838 num_ev++;
3839 }
3840 else if (rv < 0)
3841 {
3842 switch (rv)
3843 {
3844 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003845 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003846 break;
3847
3848 default:
Florin Coras6917b942018-11-13 22:44:54 -08003849 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003850 break;
3851 }
3852 num_ev++;
3853 }
3854 }
3855
Dave Wallace7e607a72018-06-18 18:41:32 -04003856 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003857 {
Florin Coras6917b942018-11-13 22:44:54 -08003858 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003859 num_ev++;
3860 }
3861 }
3862 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003863 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003864 }
3865 while ((num_ev == 0) && keep_trying);
3866
Dave Wallace048b1d62018-01-03 22:24:41 -05003867 return num_ev;
3868}
3869
Florin Coras99368312018-08-02 10:45:44 -07003870int
3871vppcom_mq_epoll_fd (void)
3872{
Florin Coras134a9962018-08-28 11:32:04 -07003873 vcl_worker_t *wrk = vcl_worker_get_current ();
3874 return wrk->mqs_epfd;
3875}
3876
3877int
Florin Coras30e79c22019-01-02 19:31:22 -08003878vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003879{
3880 return session_handle & 0xFFFFFF;
3881}
3882
3883int
Florin Coras30e79c22019-01-02 19:31:22 -08003884vppcom_session_worker (vcl_session_handle_t session_handle)
3885{
3886 return session_handle >> 24;
3887}
3888
3889int
Florin Coras134a9962018-08-28 11:32:04 -07003890vppcom_worker_register (void)
3891{
Florin Coras47c40e22018-11-26 17:01:36 -08003892 if (!vcl_worker_alloc_and_init ())
3893 return VPPCOM_EEXIST;
3894
Florin Coras47c40e22018-11-26 17:01:36 -08003895 if (vcl_worker_register_with_vpp ())
3896 return VPPCOM_EEXIST;
3897
3898 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003899}
3900
Florin Coras369db832019-07-08 12:34:45 -07003901void
3902vppcom_worker_unregister (void)
3903{
3904 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3905 vcl_set_worker_index (~0);
3906}
3907
Pivo6017ff02020-05-04 17:57:33 +02003908void
3909vppcom_worker_index_set (int index)
3910{
3911 vcl_set_worker_index (index);
3912}
3913
Florin Corasdfe4cf42018-11-28 22:13:45 -08003914int
3915vppcom_worker_index (void)
3916{
3917 return vcl_get_worker_index ();
3918}
3919
Florin Corase0982e52019-01-25 13:19:56 -08003920int
3921vppcom_worker_mqs_epfd (void)
3922{
3923 vcl_worker_t *wrk = vcl_worker_get_current ();
3924 if (!vcm->cfg.use_mq_eventfd)
3925 return -1;
3926 return wrk->mqs_epfd;
3927}
3928
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003929int
3930vppcom_session_is_connectable_listener (uint32_t session_handle)
3931{
3932 vcl_session_t *session;
3933 vcl_worker_t *wrk = vcl_worker_get_current ();
3934 session = vcl_session_get_w_handle (wrk, session_handle);
3935 if (!session)
3936 return VPPCOM_EBADFD;
3937 return vcl_session_is_connectable_listener (wrk, session);
3938}
3939
3940int
3941vppcom_session_listener (uint32_t session_handle)
3942{
3943 vcl_worker_t *wrk = vcl_worker_get_current ();
3944 vcl_session_t *listen_session, *session;
3945 session = vcl_session_get_w_handle (wrk, session_handle);
3946 if (!session)
3947 return VPPCOM_EBADFD;
3948 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3949 return VPPCOM_EBADFD;
3950 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3951 if (!listen_session)
3952 return VPPCOM_EBADFD;
3953 return vcl_session_handle (listen_session);
3954}
3955
3956int
3957vppcom_session_n_accepted (uint32_t session_handle)
3958{
3959 vcl_worker_t *wrk = vcl_worker_get_current ();
3960 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3961 if (!session)
3962 return VPPCOM_EBADFD;
3963 return session->n_accepted_sessions;
3964}
3965
Florin Coras66ec4672020-06-15 07:59:40 -07003966const char *
3967vppcom_proto_str (vppcom_proto_t proto)
3968{
3969 char const *proto_str;
3970
3971 switch (proto)
3972 {
3973 case VPPCOM_PROTO_TCP:
3974 proto_str = "TCP";
3975 break;
3976 case VPPCOM_PROTO_UDP:
3977 proto_str = "UDP";
3978 break;
3979 case VPPCOM_PROTO_TLS:
3980 proto_str = "TLS";
3981 break;
3982 case VPPCOM_PROTO_QUIC:
3983 proto_str = "QUIC";
3984 break;
3985 default:
3986 proto_str = "UNKNOWN";
3987 break;
3988 }
3989 return proto_str;
3990}
3991
3992const char *
3993vppcom_retval_str (int retval)
3994{
3995 char const *st;
3996
3997 switch (retval)
3998 {
3999 case VPPCOM_OK:
4000 st = "VPPCOM_OK";
4001 break;
4002
4003 case VPPCOM_EAGAIN:
4004 st = "VPPCOM_EAGAIN";
4005 break;
4006
4007 case VPPCOM_EFAULT:
4008 st = "VPPCOM_EFAULT";
4009 break;
4010
4011 case VPPCOM_ENOMEM:
4012 st = "VPPCOM_ENOMEM";
4013 break;
4014
4015 case VPPCOM_EINVAL:
4016 st = "VPPCOM_EINVAL";
4017 break;
4018
4019 case VPPCOM_EBADFD:
4020 st = "VPPCOM_EBADFD";
4021 break;
4022
4023 case VPPCOM_EAFNOSUPPORT:
4024 st = "VPPCOM_EAFNOSUPPORT";
4025 break;
4026
4027 case VPPCOM_ECONNABORTED:
4028 st = "VPPCOM_ECONNABORTED";
4029 break;
4030
4031 case VPPCOM_ECONNRESET:
4032 st = "VPPCOM_ECONNRESET";
4033 break;
4034
4035 case VPPCOM_ENOTCONN:
4036 st = "VPPCOM_ENOTCONN";
4037 break;
4038
4039 case VPPCOM_ECONNREFUSED:
4040 st = "VPPCOM_ECONNREFUSED";
4041 break;
4042
4043 case VPPCOM_ETIMEDOUT:
4044 st = "VPPCOM_ETIMEDOUT";
4045 break;
4046
4047 default:
4048 st = "UNKNOWN_STATE";
4049 break;
4050 }
4051
4052 return st;
4053}
4054
Florin Corasa5a9efd2021-01-05 17:03:29 -08004055int
4056vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4057{
4058 if (vcm->cfg.vpp_app_socket_api)
4059 {
4060 clib_warning ("not supported");
4061 return VPPCOM_EINVAL;
4062 }
4063 return vcl_bapi_add_cert_key_pair (ckpair);
4064}
4065
4066int
4067vppcom_del_cert_key_pair (uint32_t ckpair_index)
4068{
4069 if (vcm->cfg.vpp_app_socket_api)
4070 {
4071 clib_warning ("not supported");
4072 return VPPCOM_EINVAL;
4073 }
4074 return vcl_bapi_del_cert_key_pair (ckpair_index);
4075}
4076
Dave Wallacee22aa742017-10-20 12:30:38 -04004077/*
4078 * fd.io coding-style-patch-verification: ON
4079 *
4080 * Local Variables:
4081 * eval: (c-set-style "gnu")
4082 * End:
4083 */