blob: 412b6a4b995a220e342c32fc8b946cd013b4859d [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 Coras7e12d942018-06-27 14:32:43 -07002395 vcl_session_t *session = 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
2426 /* *INDENT-OFF* */
Damjan Marionf0ca1e82020-12-13 23:26:56 +01002427 clib_bitmap_foreach (sid, wrk->wr_bitmap) {
Florin Coras134a9962018-08-28 11:32:04 -07002428 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002429 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002430 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
2431 bits_set++;
2432 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002433 }
2434
Florin Coras5e6222a2020-04-24 17:09:25 +00002435 if (vcl_session_write_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002436 {
David Johnsond9818dd2018-12-14 14:53:41 -05002437 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002438 bits_set++;
2439 }
Florin Coras294afe22019-01-07 17:49:17 -08002440 else
Florin Coras2d379d82019-06-28 12:45:12 -07002441 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Damjan Marionf0ca1e82020-12-13 23:26:56 +01002442 }
Florin Coras54693d22018-07-17 10:46:29 -07002443
2444check_rd:
2445 if (!read_map)
2446 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002447
Damjan Marionf0ca1e82020-12-13 23:26:56 +01002448 clib_bitmap_foreach (sid, wrk->rd_bitmap) {
Florin Coras134a9962018-08-28 11:32:04 -07002449 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002450 {
Florin Coras5e6222a2020-04-24 17:09:25 +00002451 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
2452 bits_set++;
2453 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002454 }
2455
Florin Coras5e6222a2020-04-24 17:09:25 +00002456 if (vcl_session_read_ready (session))
Florin Coras54693d22018-07-17 10:46:29 -07002457 {
David Johnsond9818dd2018-12-14 14:53:41 -05002458 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002459 bits_set++;
2460 }
Damjan Marionf0ca1e82020-12-13 23:26:56 +01002461 }
Florin Coras54693d22018-07-17 10:46:29 -07002462 /* *INDENT-ON* */
2463
2464check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002465
Florin Coras86f04502018-09-12 16:08:01 -07002466 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2467 {
2468 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2469 read_map, write_map, except_map, &bits_set);
2470 }
2471 vec_reset_length (wrk->unhandled_evts_vector);
2472
Florin Coras99368312018-08-02 10:45:44 -07002473 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002474 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002475 time_to_wait, &bits_set);
2476 else
Florin Coras134a9962018-08-28 11:32:04 -07002477 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002478 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002479
Dave Wallace543852a2017-08-03 02:11:34 -04002480 return (bits_set);
2481}
2482
Dave Wallacef7f809c2017-10-03 01:48:42 -04002483static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002484vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002485{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002486 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002487 u32 sh = vep_handle;
Florin Coras6c3b2182020-10-19 18:36:48 -07002488 vcl_session_t *s;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002489
Florin Corasc0737e92019-03-04 14:19:39 -08002490 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002491 return;
2492
Florin Coras6c3b2182020-10-19 18:36:48 -07002493 s = vcl_session_get_w_handle (wrk, vep_handle);
2494 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002495 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002496 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002497 goto done;
2498 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002499 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002500 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002501 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002502 goto done;
2503 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002504 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002505 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002506 "{\n"
2507 " is_vep = %u\n"
2508 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002509 " next_sh = 0x%x (%u)\n"
Florin Coras6c3b2182020-10-19 18:36:48 -07002510 "}\n", vep_handle, s->flags & VCL_SESSION_F_IS_VEP,
2511 s->flags & VCL_SESSION_F_IS_VEP_SESSION, vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002512
Florin Coras7e5e62b2019-07-30 14:08:23 -07002513 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002514 {
Florin Coras6c3b2182020-10-19 18:36:48 -07002515 s = vcl_session_get_w_handle (wrk, sh);
2516 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002517 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002518 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002519 goto done;
2520 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002521 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Florin Coras5e062572019-03-14 19:07:51 -07002522 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002523 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002524 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002525 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002526 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002527 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002528 goto done;
2529 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002530 vep = &s->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002531 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2532 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
Florin Coras6c3b2182020-10-19 18:36:48 -07002533 sh, s->vep.vep_sh, vep_handle);
2534 if (s->flags & VCL_SESSION_F_IS_VEP_SESSION)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002535 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002536 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002537 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002538 " next_sh = 0x%x (%u)\n"
2539 " prev_sh = 0x%x (%u)\n"
2540 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002541 " ev.events = 0x%x\n"
2542 " ev.data.u64 = 0x%llx\n"
2543 " et_mask = 0x%x\n"
2544 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002545 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002546 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2547 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002548 }
2549 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002550
2551done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002552 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002553}
2554
2555int
2556vppcom_epoll_create (void)
2557{
Florin Coras134a9962018-08-28 11:32:04 -07002558 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002559 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002560
Florin Coras134a9962018-08-28 11:32:04 -07002561 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002562
Florin Coras6c3b2182020-10-19 18:36:48 -07002563 vep_session->flags |= VCL_SESSION_F_IS_VEP;
Florin Coras134a9962018-08-28 11:32:04 -07002564 vep_session->vep.vep_sh = ~0;
2565 vep_session->vep.next_sh = ~0;
2566 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002567 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002568
Florin Corasa7a1a222018-12-30 17:11:31 -08002569 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2570 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002571
Florin Corasab2f6db2018-08-31 14:31:41 -07002572 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002573}
2574
2575int
Florin Coras134a9962018-08-28 11:32:04 -07002576vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002577 struct epoll_event *event)
2578{
Florin Coras134a9962018-08-28 11:32:04 -07002579 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002580 vcl_session_t *vep_session;
Florin Coras070453d2018-08-24 17:04:27 -07002581 int rv = VPPCOM_OK;
Florin Coras17ec5772020-08-12 20:46:29 -07002582 vcl_session_t *s;
2583 svm_fifo_t *txf;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002584
Florin Coras134a9962018-08-28 11:32:04 -07002585 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002586 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002587 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002588 return VPPCOM_EINVAL;
2589 }
2590
Florin Coras134a9962018-08-28 11:32:04 -07002591 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002592 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002593 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002594 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002595 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002596 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002597 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002598 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002599 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002600 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002601 }
2602
Florin Coras134a9962018-08-28 11:32:04 -07002603 ASSERT (vep_session->vep.vep_sh == ~0);
2604 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002605
Florin Coras17ec5772020-08-12 20:46:29 -07002606 s = vcl_session_get_w_handle (wrk, session_handle);
2607 if (PREDICT_FALSE (!s))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002608 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002609 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002610 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002611 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002612 if (PREDICT_FALSE (s->flags & VCL_SESSION_F_IS_VEP))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002613 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002614 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002615 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002616 }
2617
2618 switch (op)
2619 {
2620 case EPOLL_CTL_ADD:
2621 if (PREDICT_FALSE (!event))
2622 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002623 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002624 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002625 }
Florin Coras134a9962018-08-28 11:32:04 -07002626 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002627 {
Florin Coras7e12d942018-06-27 14:32:43 -07002628 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002629 next_session = vcl_session_get_w_handle (wrk,
2630 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002631 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002632 {
Florin Coras5e062572019-03-14 19:07:51 -07002633 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002634 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002635 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002636 }
Florin Coras134a9962018-08-28 11:32:04 -07002637 ASSERT (next_session->vep.prev_sh == vep_handle);
2638 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002639 }
Florin Coras17ec5772020-08-12 20:46:29 -07002640 s->vep.next_sh = vep_session->vep.next_sh;
2641 s->vep.prev_sh = vep_handle;
2642 s->vep.vep_sh = vep_handle;
2643 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2644 s->vep.ev = *event;
Florin Coras6c3b2182020-10-19 18:36:48 -07002645 s->flags &= ~VCL_SESSION_F_IS_VEP;
2646 s->flags |= VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras134a9962018-08-28 11:32:04 -07002647 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002648
Florin Coras17ec5772020-08-12 20:46:29 -07002649 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2650 if (txf && (event->events & EPOLLOUT))
2651 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002652
Florin Coras6e3c1f82020-01-15 01:30:46 +00002653 /* Generate EPOLLOUT if tx fifo not full */
Florin Coras17ec5772020-08-12 20:46:29 -07002654 if ((event->events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002655 {
2656 session_event_t e = { 0 };
2657 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002658 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002659 vec_add1 (wrk->unhandled_evts_vector, e);
2660 }
Florin Coras6e3c1f82020-01-15 01:30:46 +00002661 /* Generate EPOLLIN if rx fifo has data */
Florin Coras17ec5772020-08-12 20:46:29 -07002662 if ((event->events & EPOLLIN) && (vcl_session_read_ready (s) > 0))
Florin Coras6e3c1f82020-01-15 01:30:46 +00002663 {
2664 session_event_t e = { 0 };
2665 e.event_type = SESSION_IO_EVT_RX;
Florin Coras17ec5772020-08-12 20:46:29 -07002666 e.session_index = s->session_index;
Florin Coras6e3c1f82020-01-15 01:30:46 +00002667 vec_add1 (wrk->unhandled_evts_vector, e);
2668 }
Florin Corasa7a1a222018-12-30 17:11:31 -08002669 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2670 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras17ec5772020-08-12 20:46:29 -07002671 vcl_evt (VCL_EVT_EPOLL_CTLADD, s, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002672 break;
2673
2674 case EPOLL_CTL_MOD:
2675 if (PREDICT_FALSE (!event))
2676 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002677 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002678 rv = VPPCOM_EINVAL;
2679 goto done;
2680 }
Florin Coras6c3b2182020-10-19 18:36:48 -07002681 else if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002682 {
Florin Coras5e062572019-03-14 19:07:51 -07002683 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002684 rv = VPPCOM_EINVAL;
2685 goto done;
2686 }
Florin Coras17ec5772020-08-12 20:46:29 -07002687 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002688 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002689 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002690 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002691 rv = VPPCOM_EINVAL;
2692 goto done;
2693 }
hanlin475c9d72019-12-26 11:44:28 +08002694
2695 /* Generate EPOLLOUT when tx_fifo/ct_tx_fifo not full */
2696 if ((event->events & EPOLLOUT) &&
Florin Coras17ec5772020-08-12 20:46:29 -07002697 !(s->vep.ev.events & EPOLLOUT) && (vcl_session_write_ready (s) > 0))
hanlin475c9d72019-12-26 11:44:28 +08002698 {
2699 session_event_t e = { 0 };
2700 e.event_type = SESSION_IO_EVT_TX;
Florin Coras17ec5772020-08-12 20:46:29 -07002701 e.session_index = s->session_index;
hanlin475c9d72019-12-26 11:44:28 +08002702 vec_add1 (wrk->unhandled_evts_vector, e);
2703 }
Florin Coras17ec5772020-08-12 20:46:29 -07002704 s->vep.et_mask = VEP_DEFAULT_ET_MASK;
2705 s->vep.ev = *event;
2706 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2707 if (event->events & EPOLLOUT)
2708 svm_fifo_add_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
2709 else
2710 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Corasa7a1a222018-12-30 17:11:31 -08002711 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2712 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002713 break;
2714
2715 case EPOLL_CTL_DEL:
Florin Coras6c3b2182020-10-19 18:36:48 -07002716 if (PREDICT_FALSE (!(s->flags & VCL_SESSION_F_IS_VEP_SESSION)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002717 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002718 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002719 rv = VPPCOM_EINVAL;
2720 goto done;
2721 }
Florin Coras17ec5772020-08-12 20:46:29 -07002722 else if (PREDICT_FALSE (s->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002723 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002724 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002725 session_handle, s->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002726 rv = VPPCOM_EINVAL;
2727 goto done;
2728 }
2729
Florin Coras17ec5772020-08-12 20:46:29 -07002730 if (s->vep.prev_sh == vep_handle)
2731 vep_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002732 else
2733 {
Florin Coras7e12d942018-06-27 14:32:43 -07002734 vcl_session_t *prev_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002735 prev_session = vcl_session_get_w_handle (wrk, s->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002736 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002737 {
Florin Coras5e062572019-03-14 19:07:51 -07002738 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002739 s->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002740 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002741 }
Florin Coras134a9962018-08-28 11:32:04 -07002742 ASSERT (prev_session->vep.next_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002743 prev_session->vep.next_sh = s->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002744 }
Florin Coras17ec5772020-08-12 20:46:29 -07002745 if (s->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002746 {
Florin Coras7e12d942018-06-27 14:32:43 -07002747 vcl_session_t *next_session;
Florin Coras17ec5772020-08-12 20:46:29 -07002748 next_session = vcl_session_get_w_handle (wrk, s->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002749 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002750 {
Florin Coras5e062572019-03-14 19:07:51 -07002751 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Coras17ec5772020-08-12 20:46:29 -07002752 s->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002753 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002754 }
Florin Coras134a9962018-08-28 11:32:04 -07002755 ASSERT (next_session->vep.prev_sh == session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002756 next_session->vep.prev_sh = s->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002757 }
2758
Florin Coras17ec5772020-08-12 20:46:29 -07002759 memset (&s->vep, 0, sizeof (s->vep));
2760 s->vep.next_sh = ~0;
2761 s->vep.prev_sh = ~0;
2762 s->vep.vep_sh = ~0;
Florin Coras6c3b2182020-10-19 18:36:48 -07002763 s->flags &= ~VCL_SESSION_F_IS_VEP_SESSION;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002764
Florin Coras17ec5772020-08-12 20:46:29 -07002765 txf = vcl_session_is_ct (s) ? s->ct_tx_fifo : s->tx_fifo;
2766 if (txf)
2767 svm_fifo_del_want_deq_ntf (txf, SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002768
Florin Coras5e062572019-03-14 19:07:51 -07002769 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002770 session_handle);
Florin Coras17ec5772020-08-12 20:46:29 -07002771 vcl_evt (VCL_EVT_EPOLL_CTLDEL, s, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002772 break;
2773
2774 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002775 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776 rv = VPPCOM_EINVAL;
2777 }
2778
Florin Coras134a9962018-08-28 11:32:04 -07002779 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002780
2781done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002782 return rv;
2783}
2784
Florin Coras86f04502018-09-12 16:08:01 -07002785static inline void
2786vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2787 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002788{
2789 session_disconnected_msg_t *disconnected_msg;
2790 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002791 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002792 u64 session_evt_data = ~0;
Florin Corasb242d312020-10-26 15:35:40 -07002793 vcl_session_t *s;
Florin Coras86f04502018-09-12 16:08:01 -07002794 u8 add_event = 0;
2795
2796 switch (e->event_type)
2797 {
Florin Coras653e43f2019-03-04 10:56:23 -08002798 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002799 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002800 s = vcl_session_get (wrk, sid);
2801 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002802 break;
Florin Corasb242d312020-10-26 15:35:40 -07002803 vcl_fifo_rx_evt_valid_or_break (s);
2804 session_events = s->vep.ev.events;
2805 if (!(EPOLLIN & s->vep.ev.events)
2806 || (s->flags & VCL_SESSION_F_HAS_RX_EVT))
Florin Coras86f04502018-09-12 16:08:01 -07002807 break;
2808 add_event = 1;
2809 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002810 session_evt_data = s->vep.ev.data.u64;
2811 s->flags |= VCL_SESSION_F_HAS_RX_EVT;
Florin Coras86f04502018-09-12 16:08:01 -07002812 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002813 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002814 sid = e->session_index;
Florin Corasb242d312020-10-26 15:35:40 -07002815 s = vcl_session_get (wrk, sid);
2816 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002817 break;
Florin Corasb242d312020-10-26 15:35:40 -07002818 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002819 if (!(EPOLLOUT & session_events))
2820 break;
2821 add_event = 1;
2822 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002823 session_evt_data = s->vep.ev.data.u64;
2824 svm_fifo_reset_has_deq_ntf (vcl_session_is_ct (s) ?
2825 s->ct_tx_fifo : s->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002826 break;
Florin Coras86f04502018-09-12 16:08:01 -07002827 case SESSION_CTRL_EVT_ACCEPTED:
Florin Corasb242d312020-10-26 15:35:40 -07002828 if (!e->postponed)
2829 s = vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
2830 else
2831 s = vcl_session_get (wrk, e->session_index);
2832 if (!s)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002833 break;
Florin Corasb242d312020-10-26 15:35:40 -07002834 session_events = s->vep.ev.events;
2835 sid = s->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002836 if (!(EPOLLIN & session_events))
2837 break;
Florin Coras86f04502018-09-12 16:08:01 -07002838 add_event = 1;
2839 events[*num_ev].events |= EPOLLIN;
Florin Corasb242d312020-10-26 15:35:40 -07002840 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002841 break;
2842 case SESSION_CTRL_EVT_CONNECTED:
Florin Corasb242d312020-10-26 15:35:40 -07002843 if (!e->postponed)
2844 {
2845 connected_msg = (session_connected_msg_t *) e->data;
2846 sid = vcl_session_connected_handler (wrk, connected_msg);
2847 }
2848 else
2849 sid = e->session_index;
2850 s = vcl_session_get (wrk, sid);
2851 if (vcl_session_is_closed (s))
Florin Corasfa915f82018-12-26 16:29:06 -08002852 break;
Florin Corasb242d312020-10-26 15:35:40 -07002853 session_events = s->vep.ev.events;
2854 /* Generate EPOLLOUT because there's no connected event */
Florin Coras72f77822019-01-22 19:05:52 -08002855 if (!(EPOLLOUT & session_events))
2856 break;
2857 add_event = 1;
2858 events[*num_ev].events |= EPOLLOUT;
Florin Corasb242d312020-10-26 15:35:40 -07002859 session_evt_data = s->vep.ev.data.u64;
2860 if (s->session_state == VCL_STATE_DETACHED)
Florin Corasdbc9c592019-09-25 16:37:43 -07002861 events[*num_ev].events |= EPOLLHUP;
Florin Coras86f04502018-09-12 16:08:01 -07002862 break;
2863 case SESSION_CTRL_EVT_DISCONNECTED:
2864 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Corasb242d312020-10-26 15:35:40 -07002865 s = vcl_session_disconnected_handler (wrk, disconnected_msg);
2866 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002867 break;
Florin Corasb242d312020-10-26 15:35:40 -07002868 sid = s->session_index;
2869 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002870 add_event = 1;
2871 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002872 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002873 break;
2874 case SESSION_CTRL_EVT_RESET:
2875 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Corasb242d312020-10-26 15:35:40 -07002876 s = vcl_session_get (wrk, sid);
2877 if (vcl_session_is_closed (s))
Florin Coras86f04502018-09-12 16:08:01 -07002878 break;
Florin Corasb242d312020-10-26 15:35:40 -07002879 session_events = s->vep.ev.events;
Florin Coras86f04502018-09-12 16:08:01 -07002880 add_event = 1;
2881 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
Florin Corasb242d312020-10-26 15:35:40 -07002882 session_evt_data = s->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002883 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002884 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2885 vcl_session_unlisten_reply_handler (wrk, e->data);
2886 break;
Florin Coras68b7e582020-01-21 18:33:23 -08002887 case SESSION_CTRL_EVT_MIGRATED:
2888 vcl_session_migrated_handler (wrk, e->data);
2889 break;
Florin Coras9ace36d2019-10-28 13:14:17 -07002890 case SESSION_CTRL_EVT_CLEANUP:
2891 vcl_session_cleanup_handler (wrk, e->data);
2892 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002893 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2894 vcl_session_req_worker_update_handler (wrk, e->data);
2895 break;
2896 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2897 vcl_session_worker_update_reply_handler (wrk, e->data);
2898 break;
Florin Corasc4c4cf52019-08-24 18:17:34 -07002899 case SESSION_CTRL_EVT_APP_ADD_SEGMENT:
2900 vcl_session_app_add_segment_handler (wrk, e->data);
2901 break;
2902 case SESSION_CTRL_EVT_APP_DEL_SEGMENT:
2903 vcl_session_app_del_segment_handler (wrk, e->data);
2904 break;
hanlina3a48962020-07-13 11:09:15 +08002905 case SESSION_CTRL_EVT_APP_WRK_RPC:
Florin Coras40c07ce2020-07-16 20:46:17 -07002906 vcl_worker_rpc_handler (wrk, e->data);
2907 break;
Florin Coras86f04502018-09-12 16:08:01 -07002908 default:
2909 VDBG (0, "unhandled: %u", e->event_type);
2910 break;
2911 }
2912
2913 if (add_event)
2914 {
2915 events[*num_ev].data.u64 = session_evt_data;
2916 if (EPOLLONESHOT & session_events)
2917 {
Florin Corasb242d312020-10-26 15:35:40 -07002918 s = vcl_session_get (wrk, sid);
2919 s->vep.ev.events = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002920 }
2921 *num_ev += 1;
2922 }
2923}
2924
2925static int
2926vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2927 struct epoll_event *events, u32 maxevents,
2928 double wait_for_time, u32 * num_ev)
2929{
Florin Coras99368312018-08-02 10:45:44 -07002930 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002931 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002932 int i;
2933
Florin Coras539663c2018-09-28 14:59:37 -07002934 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2935 goto handle_dequeued;
2936
Florin Coras54693d22018-07-17 10:46:29 -07002937 svm_msg_q_lock (mq);
2938 if (svm_msg_q_is_empty (mq))
2939 {
2940 if (!wait_for_time)
2941 {
2942 svm_msg_q_unlock (mq);
2943 return 0;
2944 }
2945 else if (wait_for_time < 0)
2946 {
2947 svm_msg_q_wait (mq);
2948 }
2949 else
2950 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002951 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002952 {
2953 svm_msg_q_unlock (mq);
2954 return 0;
2955 }
2956 }
2957 }
Florin Corase003a1b2019-06-05 10:47:16 -07002958 ASSERT (maxevents > *num_ev);
hanlind0e646f2020-05-11 22:20:37 +08002959 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002960 svm_msg_q_unlock (mq);
2961
Florin Coras539663c2018-09-28 14:59:37 -07002962handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002963 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002964 {
Florin Coras134a9962018-08-28 11:32:04 -07002965 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002966 e = svm_msg_q_msg_data (mq, msg);
hanlind0e646f2020-05-11 22:20:37 +08002967 if (*num_ev < maxevents)
2968 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2969 else
2970 vcl_handle_mq_event (wrk, e);
Florin Coras99368312018-08-02 10:45:44 -07002971 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002972 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002973 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002974 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002975 return *num_ev;
2976}
2977
Florin Coras99368312018-08-02 10:45:44 -07002978static int
Florin Coras134a9962018-08-28 11:32:04 -07002979vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002980 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002981{
Florin Corase003a1b2019-06-05 10:47:16 -07002982 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002983
2984 if (!n_evts)
2985 {
2986 wait = wait_for_time;
2987 start = clib_time_now (&wrk->clib_time);
2988 }
2989
2990 do
2991 {
2992 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2993 wait, &n_evts);
2994 if (n_evts)
2995 return n_evts;
2996 if (wait == -1)
2997 continue;
2998
Florin Corase003a1b2019-06-05 10:47:16 -07002999 now = clib_time_now (&wrk->clib_time);
Florin Coras0edfb1a2020-08-04 22:45:45 -07003000 wait -= (now - start) * 1e3;
Florin Corase003a1b2019-06-05 10:47:16 -07003001 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08003002 }
3003 while (wait > 0);
3004
3005 return 0;
Florin Coras99368312018-08-02 10:45:44 -07003006}
3007
3008static int
Florin Coras134a9962018-08-28 11:32:04 -07003009vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07003010 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07003011{
3012 vcl_mq_evt_conn_t *mqc;
3013 int __clib_unused n_read;
3014 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07003015 u64 buf;
3016
Florin Coras134a9962018-08-28 11:32:04 -07003017 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07003018again:
Florin Coras134a9962018-08-28 11:32:04 -07003019 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
3020 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07003021 for (i = 0; i < n_mq_evts; i++)
3022 {
Florin Coras134a9962018-08-28 11:32:04 -07003023 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07003024 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07003025 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07003026 }
Florin Corasa4878ed2018-10-12 13:09:36 -07003027 if (!n_evts && n_mq_evts > 0)
3028 goto again;
Florin Coras99368312018-08-02 10:45:44 -07003029
3030 return (int) n_evts;
3031}
3032
Dave Wallacef7f809c2017-10-03 01:48:42 -04003033int
Florin Coras134a9962018-08-28 11:32:04 -07003034vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003035 int maxevents, double wait_for_time)
3036{
Florin Coras134a9962018-08-28 11:32:04 -07003037 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07003038 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07003039 u32 n_evts = 0;
3040 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003041
3042 if (PREDICT_FALSE (maxevents <= 0))
3043 {
Florin Coras5e062572019-03-14 19:07:51 -07003044 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003045 return VPPCOM_EINVAL;
3046 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003047
Florin Coras134a9962018-08-28 11:32:04 -07003048 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07003049 if (!vep_session)
3050 return VPPCOM_EBADFD;
3051
Florin Coras6c3b2182020-10-19 18:36:48 -07003052 if (PREDICT_FALSE (!(vep_session->flags & VCL_SESSION_F_IS_VEP)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003053 {
Florin Coras5e062572019-03-14 19:07:51 -07003054 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07003055 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003056 }
Florin Coras54693d22018-07-17 10:46:29 -07003057
3058 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003059
Florin Coras86f04502018-09-12 16:08:01 -07003060 if (vec_len (wrk->unhandled_evts_vector))
3061 {
3062 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
3063 {
3064 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
3065 events, &n_evts);
3066 if (n_evts == maxevents)
3067 {
Florin Corase003a1b2019-06-05 10:47:16 -07003068 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
3069 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07003070 }
3071 }
Florin Corase003a1b2019-06-05 10:47:16 -07003072 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07003073 }
3074
3075 if (vcm->cfg.use_mq_eventfd)
3076 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
3077 wait_for_time);
3078
3079 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
3080 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003081}
3082
Dave Wallace35830af2017-10-09 01:43:42 -04003083int
Florin Coras134a9962018-08-28 11:32:04 -07003084vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04003085 void *buffer, uint32_t * buflen)
3086{
Florin Coras134a9962018-08-28 11:32:04 -07003087 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7baeb712019-01-04 17:05:43 -08003088 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07003089 vppcom_endpt_t *ep = buffer;
Florin Corasa5a9efd2021-01-05 17:03:29 -08003090 vcl_session_t *session;
3091 int rv = VPPCOM_OK;
Dave Wallace35830af2017-10-09 01:43:42 -04003092
Florin Coras134a9962018-08-28 11:32:04 -07003093 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003094 if (!session)
3095 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003096
Dave Wallace35830af2017-10-09 01:43:42 -04003097 switch (op)
3098 {
3099 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003100 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003101 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
3102 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003103 break;
3104
Dave Wallace227867f2017-11-13 21:21:53 -05003105 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08003106 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07003107 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
3108 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003109 break;
3110
3111 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003112 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003113 {
Simon Zhang53ec9672020-08-07 05:20:47 +08003114 *flags =
3115 O_RDWR |
Florin Corasac422d62020-10-19 20:51:36 -07003116 (vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK) ?
Simon Zhang53ec9672020-08-07 05:20:47 +08003117 O_NONBLOCK : 0);
Dave Wallace35830af2017-10-09 01:43:42 -04003118 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07003119 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
3120 "is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003121 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003122 }
3123 else
3124 rv = VPPCOM_EINVAL;
3125 break;
3126
3127 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003128 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003129 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003130 if (*flags & O_NONBLOCK)
Florin Corasac422d62020-10-19 20:51:36 -07003131 vcl_session_set_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003132 else
Florin Corasac422d62020-10-19 20:51:36 -07003133 vcl_session_clear_attr (session, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003134
Florin Coras5e062572019-03-14 19:07:51 -07003135 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
3136 " is_nonblocking = %u", session_handle, *flags,
Florin Corasac422d62020-10-19 20:51:36 -07003137 vcl_session_has_attr (session, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003138 }
3139 else
3140 rv = VPPCOM_EINVAL;
3141 break;
3142
3143 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003144 if (PREDICT_TRUE (buffer && buflen &&
3145 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003146 {
Florin Coras7e12d942018-06-27 14:32:43 -07003147 ep->is_ip4 = session->transport.is_ip4;
3148 ep->port = session->transport.rmt_port;
3149 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003150 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3151 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003152 else
Dave Barach178cf492018-11-13 16:34:13 -05003153 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3154 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003155 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003156 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
3157 "addr = %U, port %u", session_handle, ep->is_ip4,
3158 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003159 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3160 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003161 }
3162 else
3163 rv = VPPCOM_EINVAL;
3164 break;
3165
3166 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003167 if (PREDICT_TRUE (buffer && buflen &&
3168 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003169 {
Florin Coras7e12d942018-06-27 14:32:43 -07003170 ep->is_ip4 = session->transport.is_ip4;
3171 ep->port = session->transport.lcl_port;
3172 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003173 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
3174 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07003175 else
Dave Barach178cf492018-11-13 16:34:13 -05003176 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
3177 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07003178 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07003179 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3180 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003181 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003182 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3183 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003184 }
3185 else
3186 rv = VPPCOM_EINVAL;
3187 break;
Stevenb5a11602017-10-11 09:59:30 -07003188
Florin Corasef7cbf62019-10-17 09:56:27 -07003189 case VPPCOM_ATTR_SET_LCL_ADDR:
3190 if (PREDICT_TRUE (buffer && buflen &&
3191 (*buflen >= sizeof (*ep)) && ep->ip))
3192 {
3193 session->transport.is_ip4 = ep->is_ip4;
3194 session->transport.lcl_port = ep->port;
3195 vcl_ip_copy_from_ep (&session->transport.lcl_ip, ep);
3196 *buflen = sizeof (*ep);
3197 VDBG (1, "VPPCOM_ATTR_SET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
3198 " port %d", session_handle, ep->is_ip4, format_ip46_address,
3199 &session->transport.lcl_ip,
3200 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3201 clib_net_to_host_u16 (ep->port));
3202 }
3203 else
3204 rv = VPPCOM_EINVAL;
3205 break;
3206
Dave Wallace048b1d62018-01-03 22:24:41 -05003207 case VPPCOM_ATTR_GET_LIBC_EPFD:
3208 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07003209 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003210 break;
3211
3212 case VPPCOM_ATTR_SET_LIBC_EPFD:
3213 if (PREDICT_TRUE (buffer && buflen &&
3214 (*buflen == sizeof (session->libc_epfd))))
3215 {
3216 session->libc_epfd = *(int *) buffer;
3217 *buflen = sizeof (session->libc_epfd);
3218
Florin Coras5e062572019-03-14 19:07:51 -07003219 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
3220 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003221 }
3222 else
3223 rv = VPPCOM_EINVAL;
3224 break;
3225
3226 case VPPCOM_ATTR_GET_PROTOCOL:
3227 if (buffer && buflen && (*buflen >= sizeof (int)))
3228 {
Florin Coras7e12d942018-06-27 14:32:43 -07003229 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003230 *buflen = sizeof (int);
3231
Florin Coras5e062572019-03-14 19:07:51 -07003232 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3233 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003234 }
3235 else
3236 rv = VPPCOM_EINVAL;
3237 break;
3238
3239 case VPPCOM_ATTR_GET_LISTEN:
3240 if (buffer && buflen && (*buflen >= sizeof (int)))
3241 {
Florin Corasac422d62020-10-19 20:51:36 -07003242 *(int *) buffer = vcl_session_has_attr (session,
3243 VCL_SESS_ATTR_LISTEN);
Dave Wallace048b1d62018-01-03 22:24:41 -05003244 *buflen = sizeof (int);
3245
Florin Coras5e062572019-03-14 19:07:51 -07003246 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
3247 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003248 }
3249 else
3250 rv = VPPCOM_EINVAL;
3251 break;
3252
3253 case VPPCOM_ATTR_GET_ERROR:
3254 if (buffer && buflen && (*buflen >= sizeof (int)))
3255 {
3256 *(int *) buffer = 0;
3257 *buflen = sizeof (int);
3258
Florin Coras5e062572019-03-14 19:07:51 -07003259 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3260 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003261 }
3262 else
3263 rv = VPPCOM_EINVAL;
3264 break;
3265
3266 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3267 if (buffer && buflen && (*buflen >= sizeof (u32)))
3268 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003269
3270 /* VPP-TBD */
3271 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003272 session->tx_fifo ?
3273 svm_fifo_size (session->tx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003274 vcm->cfg.tx_fifo_size);
3275 *buflen = sizeof (u32);
3276
Florin Coras5e062572019-03-14 19:07:51 -07003277 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3278 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3279 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003280 }
3281 else
3282 rv = VPPCOM_EINVAL;
3283 break;
3284
3285 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3286 if (buffer && buflen && (*buflen == sizeof (u32)))
3287 {
3288 /* VPP-TBD */
3289 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003290 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3291 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3292 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003293 }
3294 else
3295 rv = VPPCOM_EINVAL;
3296 break;
3297
3298 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3299 if (buffer && buflen && (*buflen >= sizeof (u32)))
3300 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003301
3302 /* VPP-TBD */
3303 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Florin Corasf22f4e52019-12-19 16:10:58 -08003304 session->rx_fifo ?
3305 svm_fifo_size (session->rx_fifo) :
Dave Wallace048b1d62018-01-03 22:24:41 -05003306 vcm->cfg.rx_fifo_size);
3307 *buflen = sizeof (u32);
3308
Florin Coras5e062572019-03-14 19:07:51 -07003309 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3310 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003311 }
3312 else
3313 rv = VPPCOM_EINVAL;
3314 break;
3315
3316 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3317 if (buffer && buflen && (*buflen == sizeof (u32)))
3318 {
3319 /* VPP-TBD */
3320 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003321 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3322 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3323 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003324 }
3325 else
3326 rv = VPPCOM_EINVAL;
3327 break;
3328
3329 case VPPCOM_ATTR_GET_REUSEADDR:
3330 if (buffer && buflen && (*buflen >= sizeof (int)))
3331 {
3332 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003333 *(int *) buffer = vcl_session_has_attr (session,
3334 VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003335 *buflen = sizeof (int);
3336
Florin Coras5e062572019-03-14 19:07:51 -07003337 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3338 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003339 }
3340 else
3341 rv = VPPCOM_EINVAL;
3342 break;
3343
Stevenb5a11602017-10-11 09:59:30 -07003344 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003345 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003346 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003347 {
3348 /* VPP-TBD */
3349 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003350 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003351 else
Florin Corasac422d62020-10-19 20:51:36 -07003352 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEADDR);
Dave Wallace048b1d62018-01-03 22:24:41 -05003353
Florin Coras5e062572019-03-14 19:07:51 -07003354 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003355 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEADDR),
Florin Coras5e062572019-03-14 19:07:51 -07003356 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003357 }
3358 else
3359 rv = VPPCOM_EINVAL;
3360 break;
3361
3362 case VPPCOM_ATTR_GET_REUSEPORT:
3363 if (buffer && buflen && (*buflen >= sizeof (int)))
3364 {
3365 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003366 *(int *) buffer = vcl_session_has_attr (session,
3367 VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003368 *buflen = sizeof (int);
3369
Florin Coras5e062572019-03-14 19:07:51 -07003370 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3371 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003372 }
3373 else
3374 rv = VPPCOM_EINVAL;
3375 break;
3376
3377 case VPPCOM_ATTR_SET_REUSEPORT:
3378 if (buffer && buflen && (*buflen == sizeof (int)) &&
Florin Corasac422d62020-10-19 20:51:36 -07003379 !vcl_session_has_attr (session, VCL_SESS_ATTR_LISTEN))
Dave Wallace048b1d62018-01-03 22:24:41 -05003380 {
3381 /* VPP-TBD */
3382 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003383 vcl_session_set_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003384 else
Florin Corasac422d62020-10-19 20:51:36 -07003385 vcl_session_clear_attr (session, VCL_SESS_ATTR_REUSEPORT);
Dave Wallace048b1d62018-01-03 22:24:41 -05003386
Florin Coras5e062572019-03-14 19:07:51 -07003387 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003388 vcl_session_has_attr (session, VCL_SESS_ATTR_REUSEPORT),
Florin Coras5e062572019-03-14 19:07:51 -07003389 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003390 }
3391 else
3392 rv = VPPCOM_EINVAL;
3393 break;
3394
3395 case VPPCOM_ATTR_GET_BROADCAST:
3396 if (buffer && buflen && (*buflen >= sizeof (int)))
3397 {
3398 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003399 *(int *) buffer = vcl_session_has_attr (session,
3400 VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003401 *buflen = sizeof (int);
3402
Florin Coras5e062572019-03-14 19:07:51 -07003403 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3404 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003405 }
3406 else
3407 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003408 break;
3409
3410 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 if (buffer && buflen && (*buflen == sizeof (int)))
3412 {
3413 /* VPP-TBD */
3414 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003415 vcl_session_set_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003416 else
Florin Corasac422d62020-10-19 20:51:36 -07003417 vcl_session_clear_attr (session, VCL_SESS_ATTR_BROADCAST);
Dave Wallace048b1d62018-01-03 22:24:41 -05003418
Florin Coras5e062572019-03-14 19:07:51 -07003419 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003420 vcl_session_has_attr (session, VCL_SESS_ATTR_BROADCAST),
Florin Coras5e062572019-03-14 19:07:51 -07003421 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003422 }
3423 else
3424 rv = VPPCOM_EINVAL;
3425 break;
3426
3427 case VPPCOM_ATTR_GET_V6ONLY:
3428 if (buffer && buflen && (*buflen >= sizeof (int)))
3429 {
3430 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003431 *(int *) buffer = vcl_session_has_attr (session,
3432 VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003433 *buflen = sizeof (int);
3434
Florin Coras5e062572019-03-14 19:07:51 -07003435 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3436 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003437 }
3438 else
3439 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003440 break;
3441
3442 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003443 if (buffer && buflen && (*buflen == sizeof (int)))
3444 {
3445 /* VPP-TBD */
3446 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003447 vcl_session_set_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003448 else
Florin Corasac422d62020-10-19 20:51:36 -07003449 vcl_session_clear_attr (session, VCL_SESS_ATTR_V6ONLY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003450
Florin Coras5e062572019-03-14 19:07:51 -07003451 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003452 vcl_session_has_attr (session, VCL_SESS_ATTR_V6ONLY),
Florin Coras5e062572019-03-14 19:07:51 -07003453 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003454 }
3455 else
3456 rv = VPPCOM_EINVAL;
3457 break;
3458
3459 case VPPCOM_ATTR_GET_KEEPALIVE:
3460 if (buffer && buflen && (*buflen >= sizeof (int)))
3461 {
3462 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003463 *(int *) buffer = vcl_session_has_attr (session,
3464 VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003465 *buflen = sizeof (int);
3466
Florin Coras5e062572019-03-14 19:07:51 -07003467 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3468 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003469 }
3470 else
3471 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003472 break;
Stevenbccd3392017-10-12 20:42:21 -07003473
3474 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 if (buffer && buflen && (*buflen == sizeof (int)))
3476 {
3477 /* VPP-TBD */
3478 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003479 vcl_session_set_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003480 else
Florin Corasac422d62020-10-19 20:51:36 -07003481 vcl_session_clear_attr (session, VCL_SESS_ATTR_KEEPALIVE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003482
Florin Coras5e062572019-03-14 19:07:51 -07003483 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003484 vcl_session_has_attr (session, VCL_SESS_ATTR_KEEPALIVE),
Florin Coras5e062572019-03-14 19:07:51 -07003485 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003486 }
3487 else
3488 rv = VPPCOM_EINVAL;
3489 break;
3490
3491 case VPPCOM_ATTR_GET_TCP_NODELAY:
3492 if (buffer && buflen && (*buflen >= sizeof (int)))
3493 {
3494 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003495 *(int *) buffer = vcl_session_has_attr (session,
3496 VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003497 *buflen = sizeof (int);
3498
Florin Coras5e062572019-03-14 19:07:51 -07003499 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3500 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003501 }
3502 else
3503 rv = VPPCOM_EINVAL;
3504 break;
3505
3506 case VPPCOM_ATTR_SET_TCP_NODELAY:
3507 if (buffer && buflen && (*buflen == sizeof (int)))
3508 {
3509 /* VPP-TBD */
3510 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003511 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003512 else
Florin Corasac422d62020-10-19 20:51:36 -07003513 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_NODELAY);
Dave Wallace048b1d62018-01-03 22:24:41 -05003514
Florin Coras5e062572019-03-14 19:07:51 -07003515 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003516 vcl_session_has_attr (session, VCL_SESS_ATTR_TCP_NODELAY),
Florin Coras5e062572019-03-14 19:07:51 -07003517 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003518 }
3519 else
3520 rv = VPPCOM_EINVAL;
3521 break;
3522
3523 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3524 if (buffer && buflen && (*buflen >= sizeof (int)))
3525 {
3526 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003527 *(int *) buffer = vcl_session_has_attr (session,
3528 VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003529 *buflen = sizeof (int);
3530
Florin Coras5e062572019-03-14 19:07:51 -07003531 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3532 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003533 }
3534 else
3535 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003536 break;
3537
3538 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003539 if (buffer && buflen && (*buflen == sizeof (int)))
3540 {
3541 /* VPP-TBD */
3542 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003543 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003544 else
Florin Corasac422d62020-10-19 20:51:36 -07003545 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPIDLE);
Dave Wallace048b1d62018-01-03 22:24:41 -05003546
Florin Coras5e062572019-03-14 19:07:51 -07003547 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003548 vcl_session_has_attr (session,
3549 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003550 }
3551 else
3552 rv = VPPCOM_EINVAL;
3553 break;
3554
3555 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3556 if (buffer && buflen && (*buflen >= sizeof (int)))
3557 {
3558 /* VPP-TBD */
Florin Corasac422d62020-10-19 20:51:36 -07003559 *(int *) buffer = vcl_session_has_attr (session,
3560 VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003561 *buflen = sizeof (int);
3562
Florin Coras5e062572019-03-14 19:07:51 -07003563 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3564 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003565 }
3566 else
3567 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003568 break;
3569
3570 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003571 if (buffer && buflen && (*buflen == sizeof (int)))
3572 {
3573 /* VPP-TBD */
3574 if (*(int *) buffer)
Florin Corasac422d62020-10-19 20:51:36 -07003575 vcl_session_set_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003576 else
Florin Corasac422d62020-10-19 20:51:36 -07003577 vcl_session_clear_attr (session, VCL_SESS_ATTR_TCP_KEEPINTVL);
Dave Wallace048b1d62018-01-03 22:24:41 -05003578
Florin Coras5e062572019-03-14 19:07:51 -07003579 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Corasac422d62020-10-19 20:51:36 -07003580 vcl_session_has_attr (session,
3581 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003582 }
3583 else
3584 rv = VPPCOM_EINVAL;
3585 break;
3586
3587 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3588 if (buffer && buflen && (*buflen >= sizeof (u32)))
3589 {
3590 /* VPP-TBD */
3591 *(u32 *) buffer = session->user_mss;
3592 *buflen = sizeof (int);
3593
Florin Coras5e062572019-03-14 19:07:51 -07003594 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3595 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003596 }
3597 else
3598 rv = VPPCOM_EINVAL;
3599 break;
3600
3601 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3602 if (buffer && buflen && (*buflen == sizeof (u32)))
3603 {
3604 /* VPP-TBD */
3605 session->user_mss = *(u32 *) buffer;
3606
Florin Coras5e062572019-03-14 19:07:51 -07003607 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3608 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003609 }
3610 else
3611 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003612 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003613
Florin Coras7baeb712019-01-04 17:05:43 -08003614 case VPPCOM_ATTR_SET_SHUT:
3615 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003616 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_RD);
Florin Coras7baeb712019-01-04 17:05:43 -08003617 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
Florin Corasac422d62020-10-19 20:51:36 -07003618 vcl_session_set_attr (session, VCL_SESS_ATTR_SHUT_WR);
Florin Coras7baeb712019-01-04 17:05:43 -08003619 break;
3620
3621 case VPPCOM_ATTR_GET_SHUT:
Florin Corasac422d62020-10-19 20:51:36 -07003622 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_RD))
Florin Coras7baeb712019-01-04 17:05:43 -08003623 tmp_flags = 1;
Florin Corasac422d62020-10-19 20:51:36 -07003624 if (vcl_session_has_attr (session, VCL_SESS_ATTR_SHUT_WR))
Florin Coras7baeb712019-01-04 17:05:43 -08003625 tmp_flags |= 2;
3626 if (tmp_flags == 1)
3627 *(int *) buffer = SHUT_RD;
3628 else if (tmp_flags == 2)
3629 *(int *) buffer = SHUT_WR;
3630 else if (tmp_flags == 3)
3631 *(int *) buffer = SHUT_RDWR;
3632 *buflen = sizeof (int);
3633 break;
Florin Coras1e966172020-05-16 18:18:14 +00003634
3635 case VPPCOM_ATTR_SET_CONNECTED:
3636 session->flags |= VCL_SESSION_F_CONNECTED;
3637 break;
3638
Florin Corasa5a9efd2021-01-05 17:03:29 -08003639 case VPPCOM_ATTR_SET_CKPAIR:
3640 if (!(buffer && buflen && (*buflen == sizeof (int))) ||
3641 !vcl_session_has_crypto (session))
3642 {
3643 rv = VPPCOM_EINVAL;
3644 break;
3645 }
3646 session->ckpair_index = *(uint32_t *) buffer;
3647 break;
3648
Dave Wallacee22aa742017-10-20 12:30:38 -04003649 default:
3650 rv = VPPCOM_EINVAL;
3651 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003652 }
3653
Dave Wallace35830af2017-10-09 01:43:42 -04003654 return rv;
3655}
3656
Stevenac1f96d2017-10-24 16:03:58 -07003657int
Florin Coras134a9962018-08-28 11:32:04 -07003658vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003659 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3660{
Florin Coras134a9962018-08-28 11:32:04 -07003661 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras5da10c42020-04-01 04:31:21 +00003662 vcl_session_t *session;
Stevenac1f96d2017-10-24 16:03:58 -07003663 int rv = VPPCOM_OK;
Steven58f464e2017-10-25 12:33:12 -07003664
3665 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003666 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003667 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003668 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003669 else
3670 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003671 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003672 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003673 }
3674
Florin Coras0a1e1832020-03-29 18:54:04 +00003675 if (ep && rv > 0)
Florin Coras99368312018-08-02 10:45:44 -07003676 {
Florin Coras5da10c42020-04-01 04:31:21 +00003677 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras99368312018-08-02 10:45:44 -07003678 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003679 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3680 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003681 else
Dave Barach178cf492018-11-13 16:34:13 -05003682 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3683 sizeof (ip6_address_t));
Florin Coras5da10c42020-04-01 04:31:21 +00003684 ep->is_ip4 = session->transport.is_ip4;
3685 ep->port = session->transport.rmt_port;
Florin Coras99368312018-08-02 10:45:44 -07003686 }
Florin Coras460dce62018-07-27 05:45:06 -07003687
Stevenac1f96d2017-10-24 16:03:58 -07003688 return rv;
3689}
3690
3691int
Florin Coras134a9962018-08-28 11:32:04 -07003692vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003693 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3694{
Florin Coras7a2abce2020-04-05 19:25:44 +00003695 vcl_worker_t *wrk = vcl_worker_get_current ();
3696 vcl_session_t *s;
3697
3698 s = vcl_session_get_w_handle (wrk, session_handle);
3699 if (!s)
3700 return VPPCOM_EBADFD;
3701
Dave Wallace617dffa2017-10-26 14:47:06 -04003702 if (!buffer)
3703 return VPPCOM_EINVAL;
3704
3705 if (ep)
3706 {
Florin Coras5824cc52020-10-20 18:44:41 -07003707 if (!vcl_session_is_cl (s))
Florin Coras5da10c42020-04-01 04:31:21 +00003708 return VPPCOM_EINVAL;
3709
3710 /* Session not connected/bound in vpp. Create it by 'connecting' it */
Florin Corasc127d5a2020-10-14 16:35:58 -07003711 if (PREDICT_FALSE (s->session_state == VCL_STATE_CLOSED))
Florin Coras5da10c42020-04-01 04:31:21 +00003712 {
Florin Coras5824cc52020-10-20 18:44:41 -07003713 u32 session_index = s->session_index;
3714 f64 timeout = vcm->cfg.session_timeout;
3715 int rv;
3716
Florin Coras5da10c42020-04-01 04:31:21 +00003717 vcl_send_session_connect (wrk, s);
Florin Coras5824cc52020-10-20 18:44:41 -07003718 rv = vppcom_wait_for_session_state_change (session_index,
3719 VCL_STATE_READY,
3720 timeout);
3721 if (rv < 0)
3722 return rv;
3723 s = vcl_session_get (wrk, session_index);
Florin Coras5da10c42020-04-01 04:31:21 +00003724 }
Florin Coras5824cc52020-10-20 18:44:41 -07003725
3726 s->transport.is_ip4 = ep->is_ip4;
3727 s->transport.rmt_port = ep->port;
3728 vcl_ip_copy_from_ep (&s->transport.rmt_ip, ep);
Dave Wallace617dffa2017-10-26 14:47:06 -04003729 }
3730
3731 if (flags)
3732 {
3733 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003734 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003735 }
3736
Florin Coras7a2abce2020-04-05 19:25:44 +00003737 return (vppcom_session_write_inline (wrk, s, buffer, buflen, 1,
3738 s->is_dgram ? 1 : 0));
Stevenac1f96d2017-10-24 16:03:58 -07003739}
3740
Dave Wallace048b1d62018-01-03 22:24:41 -05003741int
3742vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3743{
Florin Coras134a9962018-08-28 11:32:04 -07003744 vcl_worker_t *wrk = vcl_worker_get_current ();
3745 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003746 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003747 svm_msg_q_msg_t msg;
3748 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003749 int rv, num_ev = 0;
3750
Florin Coras5e062572019-03-14 19:07:51 -07003751 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003752
3753 if (!vp)
3754 return VPPCOM_EFAULT;
3755
3756 do
3757 {
Florin Coras7e12d942018-06-27 14:32:43 -07003758 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003759
Florin Coras6917b942018-11-13 22:44:54 -08003760 /* Dequeue all events and drop all unhandled io events */
3761 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3762 {
3763 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3764 vcl_handle_mq_event (wrk, e);
3765 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3766 }
3767 vec_reset_length (wrk->unhandled_evts_vector);
3768
Dave Wallace048b1d62018-01-03 22:24:41 -05003769 for (i = 0; i < n_sids; i++)
3770 {
Florin Coras7baeb712019-01-04 17:05:43 -08003771 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003772 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003773 {
3774 vp[i].revents = POLLHUP;
3775 num_ev++;
3776 continue;
3777 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003778
Florin Coras6917b942018-11-13 22:44:54 -08003779 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003780
3781 if (POLLIN & vp[i].events)
3782 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003783 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003784 if (rv > 0)
3785 {
Florin Coras6917b942018-11-13 22:44:54 -08003786 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003787 num_ev++;
3788 }
3789 else if (rv < 0)
3790 {
3791 switch (rv)
3792 {
3793 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003794 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003795 break;
3796
3797 default:
Florin Coras6917b942018-11-13 22:44:54 -08003798 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003799 break;
3800 }
3801 num_ev++;
3802 }
3803 }
3804
3805 if (POLLOUT & vp[i].events)
3806 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003807 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003808 if (rv > 0)
3809 {
Florin Coras6917b942018-11-13 22:44:54 -08003810 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003811 num_ev++;
3812 }
3813 else if (rv < 0)
3814 {
3815 switch (rv)
3816 {
3817 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003818 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003819 break;
3820
3821 default:
Florin Coras6917b942018-11-13 22:44:54 -08003822 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003823 break;
3824 }
3825 num_ev++;
3826 }
3827 }
3828
Dave Wallace7e607a72018-06-18 18:41:32 -04003829 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003830 {
Florin Coras6917b942018-11-13 22:44:54 -08003831 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003832 num_ev++;
3833 }
3834 }
3835 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003836 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003837 }
3838 while ((num_ev == 0) && keep_trying);
3839
Dave Wallace048b1d62018-01-03 22:24:41 -05003840 return num_ev;
3841}
3842
Florin Coras99368312018-08-02 10:45:44 -07003843int
3844vppcom_mq_epoll_fd (void)
3845{
Florin Coras134a9962018-08-28 11:32:04 -07003846 vcl_worker_t *wrk = vcl_worker_get_current ();
3847 return wrk->mqs_epfd;
3848}
3849
3850int
Florin Coras30e79c22019-01-02 19:31:22 -08003851vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003852{
3853 return session_handle & 0xFFFFFF;
3854}
3855
3856int
Florin Coras30e79c22019-01-02 19:31:22 -08003857vppcom_session_worker (vcl_session_handle_t session_handle)
3858{
3859 return session_handle >> 24;
3860}
3861
3862int
Florin Coras134a9962018-08-28 11:32:04 -07003863vppcom_worker_register (void)
3864{
Florin Coras47c40e22018-11-26 17:01:36 -08003865 if (!vcl_worker_alloc_and_init ())
3866 return VPPCOM_EEXIST;
3867
Florin Coras47c40e22018-11-26 17:01:36 -08003868 if (vcl_worker_register_with_vpp ())
3869 return VPPCOM_EEXIST;
3870
3871 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003872}
3873
Florin Coras369db832019-07-08 12:34:45 -07003874void
3875vppcom_worker_unregister (void)
3876{
3877 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3878 vcl_set_worker_index (~0);
3879}
3880
Pivo6017ff02020-05-04 17:57:33 +02003881void
3882vppcom_worker_index_set (int index)
3883{
3884 vcl_set_worker_index (index);
3885}
3886
Florin Corasdfe4cf42018-11-28 22:13:45 -08003887int
3888vppcom_worker_index (void)
3889{
3890 return vcl_get_worker_index ();
3891}
3892
Florin Corase0982e52019-01-25 13:19:56 -08003893int
3894vppcom_worker_mqs_epfd (void)
3895{
3896 vcl_worker_t *wrk = vcl_worker_get_current ();
3897 if (!vcm->cfg.use_mq_eventfd)
3898 return -1;
3899 return wrk->mqs_epfd;
3900}
3901
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003902int
3903vppcom_session_is_connectable_listener (uint32_t session_handle)
3904{
3905 vcl_session_t *session;
3906 vcl_worker_t *wrk = vcl_worker_get_current ();
3907 session = vcl_session_get_w_handle (wrk, session_handle);
3908 if (!session)
3909 return VPPCOM_EBADFD;
3910 return vcl_session_is_connectable_listener (wrk, session);
3911}
3912
3913int
3914vppcom_session_listener (uint32_t session_handle)
3915{
3916 vcl_worker_t *wrk = vcl_worker_get_current ();
3917 vcl_session_t *listen_session, *session;
3918 session = vcl_session_get_w_handle (wrk, session_handle);
3919 if (!session)
3920 return VPPCOM_EBADFD;
3921 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3922 return VPPCOM_EBADFD;
3923 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3924 if (!listen_session)
3925 return VPPCOM_EBADFD;
3926 return vcl_session_handle (listen_session);
3927}
3928
3929int
3930vppcom_session_n_accepted (uint32_t session_handle)
3931{
3932 vcl_worker_t *wrk = vcl_worker_get_current ();
3933 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3934 if (!session)
3935 return VPPCOM_EBADFD;
3936 return session->n_accepted_sessions;
3937}
3938
Florin Coras66ec4672020-06-15 07:59:40 -07003939const char *
3940vppcom_proto_str (vppcom_proto_t proto)
3941{
3942 char const *proto_str;
3943
3944 switch (proto)
3945 {
3946 case VPPCOM_PROTO_TCP:
3947 proto_str = "TCP";
3948 break;
3949 case VPPCOM_PROTO_UDP:
3950 proto_str = "UDP";
3951 break;
3952 case VPPCOM_PROTO_TLS:
3953 proto_str = "TLS";
3954 break;
3955 case VPPCOM_PROTO_QUIC:
3956 proto_str = "QUIC";
3957 break;
3958 default:
3959 proto_str = "UNKNOWN";
3960 break;
3961 }
3962 return proto_str;
3963}
3964
3965const char *
3966vppcom_retval_str (int retval)
3967{
3968 char const *st;
3969
3970 switch (retval)
3971 {
3972 case VPPCOM_OK:
3973 st = "VPPCOM_OK";
3974 break;
3975
3976 case VPPCOM_EAGAIN:
3977 st = "VPPCOM_EAGAIN";
3978 break;
3979
3980 case VPPCOM_EFAULT:
3981 st = "VPPCOM_EFAULT";
3982 break;
3983
3984 case VPPCOM_ENOMEM:
3985 st = "VPPCOM_ENOMEM";
3986 break;
3987
3988 case VPPCOM_EINVAL:
3989 st = "VPPCOM_EINVAL";
3990 break;
3991
3992 case VPPCOM_EBADFD:
3993 st = "VPPCOM_EBADFD";
3994 break;
3995
3996 case VPPCOM_EAFNOSUPPORT:
3997 st = "VPPCOM_EAFNOSUPPORT";
3998 break;
3999
4000 case VPPCOM_ECONNABORTED:
4001 st = "VPPCOM_ECONNABORTED";
4002 break;
4003
4004 case VPPCOM_ECONNRESET:
4005 st = "VPPCOM_ECONNRESET";
4006 break;
4007
4008 case VPPCOM_ENOTCONN:
4009 st = "VPPCOM_ENOTCONN";
4010 break;
4011
4012 case VPPCOM_ECONNREFUSED:
4013 st = "VPPCOM_ECONNREFUSED";
4014 break;
4015
4016 case VPPCOM_ETIMEDOUT:
4017 st = "VPPCOM_ETIMEDOUT";
4018 break;
4019
4020 default:
4021 st = "UNKNOWN_STATE";
4022 break;
4023 }
4024
4025 return st;
4026}
4027
Florin Corasa5a9efd2021-01-05 17:03:29 -08004028int
4029vppcom_add_cert_key_pair (vppcom_cert_key_pair_t *ckpair)
4030{
4031 if (vcm->cfg.vpp_app_socket_api)
4032 {
4033 clib_warning ("not supported");
4034 return VPPCOM_EINVAL;
4035 }
4036 return vcl_bapi_add_cert_key_pair (ckpair);
4037}
4038
4039int
4040vppcom_del_cert_key_pair (uint32_t ckpair_index)
4041{
4042 if (vcm->cfg.vpp_app_socket_api)
4043 {
4044 clib_warning ("not supported");
4045 return VPPCOM_EINVAL;
4046 }
4047 return vcl_bapi_del_cert_key_pair (ckpair_index);
4048}
4049
Dave Wallacee22aa742017-10-20 12:30:38 -04004050/*
4051 * fd.io coding-style-patch-verification: ON
4052 *
4053 * Local Variables:
4054 * eval: (c-set-style "gnu")
4055 * End:
4056 */