blob: 3205a812ce4db7e16f8b0bbdbf49cd5de388c53c [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Corasd85de682018-11-29 17:02:29 -080025static int
26vcl_wait_for_segment (u64 segment_handle)
Florin Coras54693d22018-07-17 10:46:29 -070027{
Florin Corasd85de682018-11-29 17:02:29 -080028 vcl_worker_t *wrk = vcl_worker_get_current ();
29 u32 wait_for_seconds = 10, segment_index;
30 f64 timeout;
Florin Coras54693d22018-07-17 10:46:29 -070031
Florin Corasd85de682018-11-29 17:02:29 -080032 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
Florin Coras5f45e012019-01-23 09:21:30 -080033 return 0;
Florin Coras54693d22018-07-17 10:46:29 -070034
Florin Corasd85de682018-11-29 17:02:29 -080035 timeout = clib_time_now (&wrk->clib_time) + wait_for_seconds;
36 while (clib_time_now (&wrk->clib_time) < timeout)
Florin Coras54693d22018-07-17 10:46:29 -070037 {
Florin Corasd85de682018-11-29 17:02:29 -080038 segment_index = vcl_segment_table_lookup (segment_handle);
39 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
40 return 0;
41 usleep (10);
Florin Coras54693d22018-07-17 10:46:29 -070042 }
Florin Corasd85de682018-11-29 17:02:29 -080043 return 1;
Florin Coras54693d22018-07-17 10:46:29 -070044}
45
Florin Coras30e79c22019-01-02 19:31:22 -080046static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070047vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080048{
49 svm_msg_q_msg_t *msg;
50 u32 n_msgs;
51 int i;
52
Florin Corase003a1b2019-06-05 10:47:16 -070053 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
Florin Coras30e79c22019-01-02 19:31:22 -080054 for (i = 0; i < n_msgs; i++)
55 {
56 vec_add2 (wrk->mq_msg_vector, msg, 1);
57 svm_msg_q_sub_w_lock (mq, msg);
58 }
59 return n_msgs;
60}
61
Florin Coras697faea2018-06-27 17:10:49 -070062const char *
Florin Coras288eaab2019-02-03 15:26:14 -080063vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040064{
65 char *st;
66
67 switch (state)
68 {
69 case STATE_START:
70 st = "STATE_START";
71 break;
72
73 case STATE_CONNECT:
74 st = "STATE_CONNECT";
75 break;
76
77 case STATE_LISTEN:
78 st = "STATE_LISTEN";
79 break;
80
81 case STATE_ACCEPT:
82 st = "STATE_ACCEPT";
83 break;
84
Florin Coras3c7d4f92018-12-14 11:28:43 -080085 case STATE_VPP_CLOSING:
86 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050087 break;
88
Dave Wallace543852a2017-08-03 02:11:34 -040089 case STATE_DISCONNECT:
90 st = "STATE_DISCONNECT";
91 break;
92
93 case STATE_FAILED:
94 st = "STATE_FAILED";
95 break;
96
Florin Coras2d675d72019-01-28 15:54:27 -080097 case STATE_UPDATED:
98 st = "STATE_UPDATED";
99 break;
100
101 case STATE_LISTEN_NO_MQ:
102 st = "STATE_LISTEN_NO_MQ";
103 break;
104
Dave Wallace543852a2017-08-03 02:11:34 -0400105 default:
106 st = "UNKNOWN_STATE";
107 break;
108 }
109
110 return st;
111}
112
Dave Wallace543852a2017-08-03 02:11:34 -0400113u8 *
114format_ip4_address (u8 * s, va_list * args)
115{
116 u8 *a = va_arg (*args, u8 *);
117 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
118}
119
120u8 *
121format_ip6_address (u8 * s, va_list * args)
122{
123 ip6_address_t *a = va_arg (*args, ip6_address_t *);
124 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
125
126 i_max_n_zero = ARRAY_LEN (a->as_u16);
127 max_n_zeros = 0;
128 i_first_zero = i_max_n_zero;
129 n_zeros = 0;
130 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
131 {
132 u32 is_zero = a->as_u16[i] == 0;
133 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
134 {
135 i_first_zero = i;
136 n_zeros = 0;
137 }
138 n_zeros += is_zero;
139 if ((!is_zero && n_zeros > max_n_zeros)
140 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
141 {
142 i_max_n_zero = i_first_zero;
143 max_n_zeros = n_zeros;
144 i_first_zero = ARRAY_LEN (a->as_u16);
145 n_zeros = 0;
146 }
147 }
148
149 last_double_colon = 0;
150 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
151 {
152 if (i == i_max_n_zero && max_n_zeros > 1)
153 {
154 s = format (s, "::");
155 i += max_n_zeros - 1;
156 last_double_colon = 1;
157 }
158 else
159 {
160 s = format (s, "%s%x",
161 (last_double_colon || i == 0) ? "" : ":",
162 clib_net_to_host_u16 (a->as_u16[i]));
163 last_double_colon = 0;
164 }
165 }
166
167 return s;
168}
169
170/* Format an IP46 address. */
171u8 *
172format_ip46_address (u8 * s, va_list * args)
173{
174 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
175 ip46_type_t type = va_arg (*args, ip46_type_t);
176 int is_ip4 = 1;
177
178 switch (type)
179 {
180 case IP46_TYPE_ANY:
181 is_ip4 = ip46_address_is_ip4 (ip46);
182 break;
183 case IP46_TYPE_IP4:
184 is_ip4 = 1;
185 break;
186 case IP46_TYPE_IP6:
187 is_ip4 = 0;
188 break;
189 }
190
191 return is_ip4 ?
192 format (s, "%U", format_ip4_address, &ip46->ip4) :
193 format (s, "%U", format_ip6_address, &ip46->ip6);
194}
195
Florin Coras697faea2018-06-27 17:10:49 -0700196/*
197 * VPPCOM Utility Functions
198 */
199
Florin Coras697faea2018-06-27 17:10:49 -0700200
Florin Coras54693d22018-07-17 10:46:29 -0700201static void
202vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
203 session_handle_t handle, int retval)
204{
205 app_session_evt_t _app_evt, *app_evt = &_app_evt;
206 session_accepted_reply_msg_t *rmp;
207 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
208 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
209 rmp->handle = handle;
210 rmp->context = context;
211 rmp->retval = retval;
212 app_send_ctrl_evt_to_vpp (mq, app_evt);
213}
214
Florin Coras99368312018-08-02 10:45:44 -0700215static void
216vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
217 session_handle_t handle, int retval)
218{
219 app_session_evt_t _app_evt, *app_evt = &_app_evt;
220 session_disconnected_reply_msg_t *rmp;
221 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
222 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
223 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
224 rmp->handle = handle;
225 rmp->context = context;
226 rmp->retval = retval;
227 app_send_ctrl_evt_to_vpp (mq, app_evt);
228}
229
Florin Corasc9fbd662018-08-24 12:59:56 -0700230static void
231vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
232 session_handle_t handle, int retval)
233{
234 app_session_evt_t _app_evt, *app_evt = &_app_evt;
235 session_reset_reply_msg_t *rmp;
236 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
237 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
238 rmp->handle = handle;
239 rmp->context = context;
240 rmp->retval = retval;
241 app_send_ctrl_evt_to_vpp (mq, app_evt);
242}
243
Florin Coras30e79c22019-01-02 19:31:22 -0800244void
245vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
246 u32 wrk_index)
247{
248 app_session_evt_t _app_evt, *app_evt = &_app_evt;
249 session_worker_update_msg_t *mp;
250 svm_msg_q_t *mq;
251
252 mq = vcl_session_vpp_evt_q (wrk, s);
253 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
254 mp = (session_worker_update_msg_t *) app_evt->evt->data;
255 mp->client_index = wrk->my_client_index;
256 mp->handle = s->vpp_handle;
257 mp->req_wrk_index = wrk->vpp_wrk_index;
258 mp->wrk_index = wrk_index;
259 app_send_ctrl_evt_to_vpp (mq, app_evt);
260}
261
Florin Coras54693d22018-07-17 10:46:29 -0700262static u32
Florin Coras00cca802019-06-06 09:38:44 -0700263vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
264 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700265{
266 vcl_session_t *session, *listen_session;
267 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700268 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700269 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700270
Florin Coras134a9962018-08-28 11:32:04 -0700271 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700272
Florin Coras00cca802019-06-06 09:38:44 -0700273 listen_session = vcl_session_get (wrk, ls_index);
274 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700275 {
Florin Coras00cca802019-06-06 09:38:44 -0700276 VDBG (0, "ERROR: listener handle %lu does not match session %u",
277 mp->listener_handle, ls_index);
278 goto error;
279 }
280
281 if (vcl_wait_for_segment (mp->segment_handle))
282 {
283 VDBG (0, "ERROR: segment for session %u couldn't be mounted!",
284 session->session_index);
285 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700286 }
287
Florin Coras54693d22018-07-17 10:46:29 -0700288 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
289 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Coras653e43f2019-03-04 10:56:23 -0800290 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
291 svm_msg_q_t *);
292 rx_fifo->client_session_index = session->session_index;
293 tx_fifo->client_session_index = session->session_index;
294 rx_fifo->client_thread_index = vcl_get_worker_index ();
295 tx_fifo->client_thread_index = vcl_get_worker_index ();
296 vpp_wrk_index = tx_fifo->master_thread_index;
297 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
298 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700299
300 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800301 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700302 session->rx_fifo = rx_fifo;
303 session->tx_fifo = tx_fifo;
304
305 session->session_state = STATE_ACCEPT;
Florin Coras09d18c22019-04-24 11:10:02 -0700306 session->transport.rmt_port = mp->rmt.port;
307 session->transport.is_ip4 = mp->rmt.is_ip4;
308 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500309 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700310
Florin Coras134a9962018-08-28 11:32:04 -0700311 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700312 session->transport.lcl_port = listen_session->transport.lcl_port;
313 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700314 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200315 session->is_dgram = vcl_proto_is_dgram (session->session_type);
316 session->listener_index = listen_session->session_index;
317 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700318
Florin Coras5e062572019-03-14 19:07:51 -0700319 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
320 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700321 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
322 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
323 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700324 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
325
Florin Coras00cca802019-06-06 09:38:44 -0700326 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
327 session->vpp_handle, 0);
328
Florin Coras134a9962018-08-28 11:32:04 -0700329 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700330
331error:
332 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
333 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
334 VNET_API_ERROR_INVALID_ARGUMENT);
335 vcl_session_free (wrk, session);
336 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700337}
338
339static u32
Florin Coras134a9962018-08-28 11:32:04 -0700340vcl_session_connected_handler (vcl_worker_t * wrk,
341 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700342{
Florin Coras99368312018-08-02 10:45:44 -0700343 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700344 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700345 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -0700346
347 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700348 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700349 if (!session)
350 {
Florin Coras5e062572019-03-14 19:07:51 -0700351 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
352 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700353 return VCL_INVALID_SESSION_INDEX;
354 }
Florin Coras54693d22018-07-17 10:46:29 -0700355 if (mp->retval)
356 {
Florin Coras5e062572019-03-14 19:07:51 -0700357 VDBG (0, "ERROR: session index %u: connect failed! %U",
358 session_index, format_api_error, ntohl (mp->retval));
Florin Coras070453d2018-08-24 17:04:27 -0700359 session->session_state = STATE_FAILED;
360 session->vpp_handle = mp->handle;
361 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700362 }
363
Florin Coras460dce62018-07-27 05:45:06 -0700364 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
365 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800366 if (vcl_wait_for_segment (mp->segment_handle))
367 {
Florin Coras653e43f2019-03-04 10:56:23 -0800368 VDBG (0, "segment for session %u couldn't be mounted!",
369 session->session_index);
Florin Coras5e062572019-03-14 19:07:51 -0700370 session->session_state = STATE_FAILED;
Florin Corasd85de682018-11-29 17:02:29 -0800371 return VCL_INVALID_SESSION_INDEX;
372 }
373
Florin Coras460dce62018-07-27 05:45:06 -0700374 rx_fifo->client_session_index = session_index;
375 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700376 rx_fifo->client_thread_index = vcl_get_worker_index ();
377 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700378
Florin Coras653e43f2019-03-04 10:56:23 -0800379 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
380 svm_msg_q_t *);
381 vpp_wrk_index = tx_fifo->master_thread_index;
382 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
383 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700384
Florin Coras653e43f2019-03-04 10:56:23 -0800385 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700386 {
Florin Coras653e43f2019-03-04 10:56:23 -0800387 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
388 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
389 if (vcl_wait_for_segment (mp->ct_segment_handle))
390 {
391 VDBG (0, "ct segment for session %u couldn't be mounted!",
392 session->session_index);
Florin Coras5e062572019-03-14 19:07:51 -0700393 session->session_state = STATE_FAILED;
Florin Coras653e43f2019-03-04 10:56:23 -0800394 return VCL_INVALID_SESSION_INDEX;
395 }
Florin Coras99368312018-08-02 10:45:44 -0700396 }
Florin Coras54693d22018-07-17 10:46:29 -0700397
Florin Coras54693d22018-07-17 10:46:29 -0700398 session->rx_fifo = rx_fifo;
399 session->tx_fifo = tx_fifo;
400 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800401 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras09d18c22019-04-24 11:10:02 -0700402 session->transport.is_ip4 = mp->lcl.is_ip4;
403 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500404 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700405 session->transport.lcl_port = mp->lcl.port;
Florin Coras54693d22018-07-17 10:46:29 -0700406 session->session_state = STATE_CONNECT;
407
408 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800409 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700410
Florin Coras5e062572019-03-14 19:07:51 -0700411 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
412 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700413 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700414
Florin Coras54693d22018-07-17 10:46:29 -0700415 return session_index;
416}
417
Florin Coras3c7d4f92018-12-14 11:28:43 -0800418static int
419vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
420{
421 vcl_session_msg_t *accepted_msg;
422 int i;
423
424 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
425 {
426 accepted_msg = &session->accept_evts_fifo[i];
427 if (accepted_msg->accepted_msg.handle == handle)
428 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800429 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800430 return 1;
431 }
432 }
433 return 0;
434}
435
Florin Corasc9fbd662018-08-24 12:59:56 -0700436static u32
Florin Coras134a9962018-08-28 11:32:04 -0700437vcl_session_reset_handler (vcl_worker_t * wrk,
438 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700439{
440 vcl_session_t *session;
441 u32 sid;
442
Florin Coras134a9962018-08-28 11:32:04 -0700443 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
444 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700445 if (!session)
446 {
447 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
448 return VCL_INVALID_SESSION_INDEX;
449 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800450
451 /* Caught a reset before actually accepting the session */
452 if (session->session_state == STATE_LISTEN)
453 {
454
455 if (!vcl_flag_accepted_session (session, reset_msg->handle,
456 VCL_ACCEPTED_F_RESET))
457 VDBG (0, "session was not accepted!");
458 return VCL_INVALID_SESSION_INDEX;
459 }
460
461 session->session_state = STATE_DISCONNECT;
462 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700463 return sid;
464}
465
Florin Coras60116992018-08-27 09:52:18 -0700466static u32
Florin Coras134a9962018-08-28 11:32:04 -0700467vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700468{
469 vcl_session_t *session;
470 u32 sid = mp->context;
471
Florin Coras134a9962018-08-28 11:32:04 -0700472 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700473 if (mp->retval)
474 {
Florin Coras5e062572019-03-14 19:07:51 -0700475 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Corasd85de682018-11-29 17:02:29 -0800476 format_api_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700477 if (session)
478 {
479 session->session_state = STATE_FAILED;
480 session->vpp_handle = mp->handle;
481 return sid;
482 }
483 else
484 {
Florin Coras5e062572019-03-14 19:07:51 -0700485 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
486 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700487 return VCL_INVALID_SESSION_INDEX;
488 }
489 }
490
491 session->vpp_handle = mp->handle;
492 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500493 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
494 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700495 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700496 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700497 session->session_state = STATE_LISTEN;
498
Florin Coras5f45e012019-01-23 09:21:30 -0800499 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
500 vec_validate (wrk->vpp_event_queues, 0);
501 wrk->vpp_event_queues[0] = session->vpp_evt_q;
502
Florin Coras60116992018-08-27 09:52:18 -0700503 if (session->is_dgram)
504 {
505 svm_fifo_t *rx_fifo, *tx_fifo;
506 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
507 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
508 rx_fifo->client_session_index = sid;
509 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
510 tx_fifo->client_session_index = sid;
511 session->rx_fifo = rx_fifo;
512 session->tx_fifo = tx_fifo;
513 }
514
Florin Coras05ecfcc2018-12-12 18:19:39 -0800515 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700516 return sid;
517}
518
Florin Corasdfae9f92019-02-20 19:48:31 -0800519static void
520vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
521{
522 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
523 vcl_session_t *s;
524
525 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
526 if (!s || s->session_state != STATE_DISCONNECT)
527 {
528 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
529 return;
530 }
531
532 if (mp->retval)
533 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
534 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
535
536 if (mp->context != wrk->wrk_index)
537 VDBG (0, "wrong context");
538
539 vcl_session_table_del_vpp_handle (wrk, mp->handle);
540 vcl_session_free (wrk, s);
541}
542
Florin Coras3c7d4f92018-12-14 11:28:43 -0800543static vcl_session_t *
544vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
545{
546 vcl_session_msg_t *vcl_msg;
547 vcl_session_t *session;
548
549 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
550 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800551 VWRN ("session overlap handle %lu state %u!", msg->handle,
552 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800553
554 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
555 if (!session)
556 {
557 VERR ("couldn't find listen session: listener handle %llx",
558 msg->listener_handle);
559 return 0;
560 }
561
562 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
563 vcl_msg->accepted_msg = *msg;
564 /* Session handle points to listener until fully accepted by app */
565 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
566
567 return session;
568}
569
570static vcl_session_t *
571vcl_session_disconnected_handler (vcl_worker_t * wrk,
572 session_disconnected_msg_t * msg)
573{
574 vcl_session_t *session;
575
576 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
577 if (!session)
578 {
579 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
580 return 0;
581 }
582
583 /* Caught a disconnect before actually accepting the session */
584 if (session->session_state == STATE_LISTEN)
585 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800586 if (!vcl_flag_accepted_session (session, msg->handle,
587 VCL_ACCEPTED_F_CLOSED))
588 VDBG (0, "session was not accepted!");
589 return 0;
590 }
591
592 session->session_state = STATE_VPP_CLOSING;
593 return session;
594}
595
Florin Coras30e79c22019-01-02 19:31:22 -0800596static void
597vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
598{
599 session_req_worker_update_msg_t *msg;
600 vcl_session_t *s;
601
602 msg = (session_req_worker_update_msg_t *) data;
603 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
604 if (!s)
605 return;
606
607 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
608}
609
610static void
611vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
612{
613 session_worker_update_reply_msg_t *msg;
614 vcl_session_t *s;
615
616 msg = (session_worker_update_reply_msg_t *) data;
617 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
618 if (!s)
619 {
620 VDBG (0, "unknown handle 0x%llx", msg->handle);
621 return;
622 }
623 if (vcl_wait_for_segment (msg->segment_handle))
624 {
625 clib_warning ("segment for session %u couldn't be mounted!",
626 s->session_index);
627 return;
628 }
Florin Coras30e79c22019-01-02 19:31:22 -0800629
Florin Coras5f45e012019-01-23 09:21:30 -0800630 if (s->rx_fifo)
631 {
632 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
633 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
634 s->rx_fifo->client_session_index = s->session_index;
635 s->tx_fifo->client_session_index = s->session_index;
636 s->rx_fifo->client_thread_index = wrk->wrk_index;
637 s->tx_fifo->client_thread_index = wrk->wrk_index;
638 }
Florin Coras30e79c22019-01-02 19:31:22 -0800639 s->session_state = STATE_UPDATED;
640
Florin Coras30e79c22019-01-02 19:31:22 -0800641 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
642 s->vpp_handle, wrk->wrk_index);
643}
644
Florin Coras86f04502018-09-12 16:08:01 -0700645static int
646vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700647{
Florin Coras54693d22018-07-17 10:46:29 -0700648 session_disconnected_msg_t *disconnected_msg;
Florin Coras54693d22018-07-17 10:46:29 -0700649 vcl_session_t *session;
Florin Coras54693d22018-07-17 10:46:29 -0700650
651 switch (e->event_type)
652 {
Florin Coras653e43f2019-03-04 10:56:23 -0800653 case SESSION_IO_EVT_RX:
654 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -0800655 session = vcl_session_get (wrk, e->session_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800656 if (!session || !(session->session_state & STATE_OPEN))
657 break;
Florin Coras86f04502018-09-12 16:08:01 -0700658 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700659 break;
660 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800661 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700662 break;
663 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700664 vcl_session_connected_handler (wrk,
665 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700666 break;
667 case SESSION_CTRL_EVT_DISCONNECTED:
668 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800669 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Corasc9fbd662018-08-24 12:59:56 -0700670 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800671 break;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800672 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
673 session->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700674 break;
675 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700676 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700677 break;
678 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700679 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700680 break;
Florin Corasdfae9f92019-02-20 19:48:31 -0800681 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
682 vcl_session_unlisten_reply_handler (wrk, e->data);
683 break;
Florin Coras30e79c22019-01-02 19:31:22 -0800684 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
685 vcl_session_req_worker_update_handler (wrk, e->data);
686 break;
687 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
688 vcl_session_worker_update_reply_handler (wrk, e->data);
689 break;
Florin Coras54693d22018-07-17 10:46:29 -0700690 default:
691 clib_warning ("unhandled %u", e->event_type);
692 }
693 return VPPCOM_OK;
694}
695
Florin Coras30e79c22019-01-02 19:31:22 -0800696static int
Florin Coras697faea2018-06-27 17:10:49 -0700697vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -0800698 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -0700699 f64 wait_for_time)
700{
Florin Coras134a9962018-08-28 11:32:04 -0700701 vcl_worker_t *wrk = vcl_worker_get_current ();
702 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700703 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700704 svm_msg_q_msg_t msg;
705 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400706
Florin Coras697faea2018-06-27 17:10:49 -0700707 do
Dave Wallace543852a2017-08-03 02:11:34 -0400708 {
Florin Coras134a9962018-08-28 11:32:04 -0700709 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700710 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700711 {
Florin Coras070453d2018-08-24 17:04:27 -0700712 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700713 }
714 if (session->session_state & state)
715 {
Florin Coras697faea2018-06-27 17:10:49 -0700716 return VPPCOM_OK;
717 }
718 if (session->session_state & STATE_FAILED)
719 {
Florin Coras697faea2018-06-27 17:10:49 -0700720 return VPPCOM_ECONNREFUSED;
721 }
Florin Coras54693d22018-07-17 10:46:29 -0700722
Florin Coras134a9962018-08-28 11:32:04 -0700723 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -0800724 {
725 usleep (100);
726 continue;
727 }
Florin Coras134a9962018-08-28 11:32:04 -0700728 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700729 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700730 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800731 }
Florin Coras134a9962018-08-28 11:32:04 -0700732 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800733
Florin Coras05ecfcc2018-12-12 18:19:39 -0800734 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -0700735 vppcom_session_state_str (state));
736 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400737
Florin Coras697faea2018-06-27 17:10:49 -0700738 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500739}
740
Florin Coras30e79c22019-01-02 19:31:22 -0800741static void
742vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
743{
Florin Coras288eaab2019-02-03 15:26:14 -0800744 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -0800745 vcl_session_t *s;
746 u32 *sip;
747
748 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
749 return;
750
751 vec_foreach (sip, wrk->pending_session_wrk_updates)
752 {
753 s = vcl_session_get (wrk, *sip);
754 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
755 state = s->session_state;
756 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
757 s->session_state = state;
758 }
759 vec_reset_length (wrk->pending_session_wrk_updates);
760}
761
Florin Corasf9240dc2019-01-15 08:03:17 -0800762void
Florin Coras30e79c22019-01-02 19:31:22 -0800763vcl_flush_mq_events (void)
764{
765 vcl_worker_t *wrk = vcl_worker_get_current ();
766 svm_msg_q_msg_t *msg;
767 session_event_t *e;
768 svm_msg_q_t *mq;
769 int i;
770
771 mq = wrk->app_event_queue;
772 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -0700773 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -0800774 svm_msg_q_unlock (mq);
775
776 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
777 {
778 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
779 e = svm_msg_q_msg_data (mq, msg);
780 vcl_handle_mq_event (wrk, e);
781 svm_msg_q_free_msg (mq, msg);
782 }
783 vec_reset_length (wrk->mq_msg_vector);
784 vcl_handle_pending_wrk_updates (wrk);
785}
786
Florin Coras697faea2018-06-27 17:10:49 -0700787static int
788vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400789{
Florin Coras697faea2018-06-27 17:10:49 -0700790 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400791
Florin Coras697faea2018-06-27 17:10:49 -0700792 if (vcm->app_state != STATE_APP_ENABLED)
793 {
794 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -0700795 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -0700796 if (PREDICT_FALSE (rv))
797 {
Florin Coras5e062572019-03-14 19:07:51 -0700798 VDBG (0, "application session enable timed out! returning %d (%s)",
799 rv, vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -0700800 return rv;
801 }
802 }
803 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400804}
805
Florin Coras697faea2018-06-27 17:10:49 -0700806static int
807vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400808{
Florin Coras697faea2018-06-27 17:10:49 -0700809 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400810
Florin Coras697faea2018-06-27 17:10:49 -0700811 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -0700812 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -0700813 if (PREDICT_FALSE (rv))
814 {
Florin Coras5e062572019-03-14 19:07:51 -0700815 VDBG (0, "application attach timed out! returning %d (%s)", rv,
816 vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -0700817 return rv;
818 }
Dave Wallace543852a2017-08-03 02:11:34 -0400819
Florin Coras697faea2018-06-27 17:10:49 -0700820 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400821}
822
823static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700824vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400825{
Florin Coras134a9962018-08-28 11:32:04 -0700826 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -0700827 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -0700828 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -0700829 vcl_session_msg_t *evt;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500830 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400831
Florin Corasab2f6db2018-08-31 14:31:41 -0700832 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700833 if (!session)
834 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500835
Florin Corasaaff5ee2019-07-08 13:00:00 -0700836 /* Flush pending accept events, if any */
837 while (clib_fifo_elts (session->accept_evts_fifo))
838 {
839 clib_fifo_sub2 (session->accept_evts_fifo, evt);
840 accepted_msg = &evt->accepted_msg;
841 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
842 vcl_send_session_accepted_reply (session->vpp_evt_q,
843 accepted_msg->context,
844 session->vpp_handle, -1);
845 }
846 clib_fifo_free (session->accept_evts_fifo);
847
Dave Wallace4878cbe2017-11-21 03:45:09 -0500848 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500849 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700850 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500851
Florin Coras5e062572019-03-14 19:07:51 -0700852 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
853 vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -0700854 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras2d675d72019-01-28 15:54:27 -0800855 vppcom_send_unbind_sock (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500856
Florin Coras070453d2018-08-24 17:04:27 -0700857 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400858}
859
Florin Coras697faea2018-06-27 17:10:49 -0700860static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700861vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400862{
Florin Coras134a9962018-08-28 11:32:04 -0700863 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700864 svm_msg_q_t *vpp_evt_q;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200865 vcl_session_t *session, *listen_session;
Florin Coras288eaab2019-02-03 15:26:14 -0800866 vcl_session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700867 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400868
Florin Corasab2f6db2018-08-31 14:31:41 -0700869 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -0700870 if (!session)
871 return VPPCOM_EBADFD;
872
Dave Wallace4878cbe2017-11-21 03:45:09 -0500873 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700874 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500875
Florin Coras5e062572019-03-14 19:07:51 -0700876 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
877 vpp_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400878
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800879 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400880 {
Florin Coras5e062572019-03-14 19:07:51 -0700881 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
Florin Coras070453d2018-08-24 17:04:27 -0700882 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500883 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400884
Florin Coras3c7d4f92018-12-14 11:28:43 -0800885 if (state & STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500886 {
Florin Coras134a9962018-08-28 11:32:04 -0700887 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras47c40e22018-11-26 17:01:36 -0800888 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
Florin Coras99368312018-08-02 10:45:44 -0700889 vpp_handle, 0);
Florin Coras5e062572019-03-14 19:07:51 -0700890 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
891 session->session_index, vpp_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400892 }
893 else
Dave Wallace227867f2017-11-13 21:21:53 -0500894 {
Florin Coras5e062572019-03-14 19:07:51 -0700895 VDBG (1, "session %u [0x%llx]: sending disconnect...",
896 session->session_index, vpp_handle);
Florin Corasab2f6db2018-08-31 14:31:41 -0700897 vppcom_send_disconnect_session (vpp_handle);
Dave Wallace227867f2017-11-13 21:21:53 -0500898 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400899
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200900 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
901 {
902 listen_session = vcl_session_get (wrk, session->listener_index);
903 listen_session->n_accepted_sessions--;
904 }
905
Florin Coras070453d2018-08-24 17:04:27 -0700906 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400907}
908
Florin Coras940f78f2018-11-30 12:11:20 -0800909/**
910 * Handle app exit
911 *
912 * Notify vpp of the disconnect and mark the worker as free. If we're the
913 * last worker, do a full cleanup otherwise, since we're probably a forked
914 * child, avoid syscalls as much as possible. We might've lost privileges.
915 */
916void
917vppcom_app_exit (void)
918{
919 if (!pool_elts (vcm->workers))
920 return;
Florin Coras01f3f892018-12-02 12:45:53 -0800921 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
922 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -0800923 vcl_elog_stop (vcm);
924 if (vec_len (vcm->workers) == 1)
925 vl_client_disconnect_from_vlib ();
926 else
Florin Coraseaec2a62018-12-04 16:34:05 -0800927 vl_client_send_disconnect (1 /* vpp should cleanup */ );
Florin Coras940f78f2018-11-30 12:11:20 -0800928}
929
Dave Wallace543852a2017-08-03 02:11:34 -0400930/*
931 * VPPCOM Public API functions
932 */
933int
934vppcom_app_create (char *app_name)
935{
Dave Wallace543852a2017-08-03 02:11:34 -0400936 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400937 int rv;
938
Florin Coras47c40e22018-11-26 17:01:36 -0800939 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -0400940 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800941 VDBG (1, "already initialized");
942 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -0400943 }
944
Florin Coras47c40e22018-11-26 17:01:36 -0800945 vcm->is_init = 1;
946 vppcom_cfg (&vcm->cfg);
947 vcl_cfg = &vcm->cfg;
948
949 vcm->main_cpu = pthread_self ();
950 vcm->main_pid = getpid ();
951 vcm->app_name = format (0, "%s", app_name);
952 vppcom_init_error_string_table ();
Florin Coras88001c62019-04-24 14:44:46 -0700953 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
954 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -0800955 pool_alloc (vcm->workers, vcl_cfg->max_workers);
956 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -0800957 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -0800958 atexit (vppcom_app_exit);
Florin Coras47c40e22018-11-26 17:01:36 -0800959
960 /* Allocate default worker */
961 vcl_worker_alloc_and_init ();
962
963 /* API hookup and connect to VPP */
964 vppcom_api_hookup ();
965 vcl_elog_init (vcm);
966 vcm->app_state = STATE_APP_START;
967 rv = vppcom_connect_to_vpp (app_name);
968 if (rv)
Dave Wallace543852a2017-08-03 02:11:34 -0400969 {
Florin Coras47c40e22018-11-26 17:01:36 -0800970 VERR ("couldn't connect to VPP!");
971 return rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400972 }
Florin Coras47c40e22018-11-26 17:01:36 -0800973 VDBG (0, "sending session enable");
974 rv = vppcom_app_session_enable ();
975 if (rv)
976 {
977 VERR ("vppcom_app_session_enable() failed!");
978 return rv;
979 }
980
981 VDBG (0, "sending app attach");
982 rv = vppcom_app_attach ();
983 if (rv)
984 {
985 VERR ("vppcom_app_attach() failed!");
986 return rv;
987 }
988
989 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
990 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400991
992 return VPPCOM_OK;
993}
994
995void
996vppcom_app_destroy (void)
997{
Dave Wallace543852a2017-08-03 02:11:34 -0400998 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400999 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -04001000
Florin Coras940f78f2018-11-30 12:11:20 -08001001 if (!pool_elts (vcm->workers))
1002 return;
1003
Florin Coras0d427d82018-06-27 03:24:07 -07001004 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001005
Florin Coras940f78f2018-11-30 12:11:20 -08001006 if (pool_elts (vcm->workers) == 1)
Florin Coras47c40e22018-11-26 17:01:36 -08001007 {
1008 vppcom_app_send_detach ();
1009 orig_app_timeout = vcm->cfg.app_timeout;
1010 vcm->cfg.app_timeout = 2.0;
1011 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1012 vcm->cfg.app_timeout = orig_app_timeout;
1013 if (PREDICT_FALSE (rv))
1014 VDBG (0, "application detach timed out! returning %d (%s)", rv,
1015 vppcom_retval_str (rv));
Florin Coras940f78f2018-11-30 12:11:20 -08001016 vec_free (vcm->app_name);
Florin Coras01f3f892018-12-02 12:45:53 -08001017 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001018 }
1019 else
1020 {
Florin Coras01f3f892018-12-02 12:45:53 -08001021 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001022 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001023
Florin Coras01f3f892018-12-02 12:45:53 -08001024 vcl_set_worker_index (~0);
Florin Coras0d427d82018-06-27 03:24:07 -07001025 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04001026 vl_client_disconnect_from_vlib ();
Dave Wallace543852a2017-08-03 02:11:34 -04001027}
1028
1029int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001030vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001031{
Florin Coras134a9962018-08-28 11:32:04 -07001032 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001033 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001034
Florin Coras134a9962018-08-28 11:32:04 -07001035 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001036
Florin Coras7e12d942018-06-27 14:32:43 -07001037 session->session_type = proto;
1038 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001039 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001040 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001041
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001042 if (is_nonblocking)
1043 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001044
Florin Coras7e12d942018-06-27 14:32:43 -07001045 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1046 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001047
Florin Coras5e062572019-03-14 19:07:51 -07001048 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001049
Florin Coras134a9962018-08-28 11:32:04 -07001050 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001051}
1052
1053int
Florin Corasf9240dc2019-01-15 08:03:17 -08001054vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1055 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001056{
Florin Coras288eaab2019-02-03 15:26:14 -08001057 vcl_session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -07001058 u32 next_sh, vep_sh;
1059 int rv = VPPCOM_OK;
1060 u64 vpp_handle;
Florin Corasf9240dc2019-01-15 08:03:17 -08001061 u8 is_vep;
Florin Coras47c40e22018-11-26 17:01:36 -08001062
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001063 is_vep = session->is_vep;
Florin Coras134a9962018-08-28 11:32:04 -07001064 next_sh = session->vep.next_sh;
1065 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -07001066 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05001067 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001068
Florin Corasf9240dc2019-01-15 08:03:17 -08001069 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001070
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001071 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04001072 {
Florin Coras134a9962018-08-28 11:32:04 -07001073 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001074 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001075 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001076 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001077 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras47c40e22018-11-26 17:01:36 -08001078 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1079 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001080
Florin Coras134a9962018-08-28 11:32:04 -07001081 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001082 }
1083 }
1084 else
1085 {
Florin Coras47c40e22018-11-26 17:01:36 -08001086 if (session->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001087 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001088 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001089 if (rv < 0)
Florin Corasf9240dc2019-01-15 08:03:17 -08001090 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1091 "failed! rv %d (%s)", session->session_index, vpp_handle,
1092 vep_sh, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001093 }
1094
Florin Coras47c40e22018-11-26 17:01:36 -08001095 if (!do_disconnect)
Florin Coras30e79c22019-01-02 19:31:22 -08001096 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08001097 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Corasf9240dc2019-01-15 08:03:17 -08001098 session->session_index, vpp_handle);
Florin Coras30e79c22019-01-02 19:31:22 -08001099 goto cleanup;
1100 }
Florin Coras47c40e22018-11-26 17:01:36 -08001101
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001102 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001103 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001104 rv = vppcom_session_unbind (sh);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001105 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001106 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1107 "rv %d (%s)", session->session_index, vpp_handle, rv,
Florin Coras47c40e22018-11-26 17:01:36 -08001108 vppcom_retval_str (rv));
Florin Corasdfae9f92019-02-20 19:48:31 -08001109 return rv;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001110 }
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001111 else if ((state & STATE_OPEN)
1112 || (vcl_session_is_connectable_listener (wrk, session)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001113 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001114 rv = vppcom_session_disconnect (sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001115 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001116 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1117 " rv %d (%s)", session->session_index, vpp_handle,
1118 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001119 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001120 else if (state == STATE_DISCONNECT)
1121 {
1122 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1123 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1124 session->vpp_handle, 0);
1125 }
Dave Wallace19481612017-09-15 18:47:44 -04001126 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001127
Florin Coras0ef8ef22019-01-18 08:37:13 -08001128 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1129
Florin Corasf9240dc2019-01-15 08:03:17 -08001130cleanup:
Florin Coras30e79c22019-01-02 19:31:22 -08001131 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Florin Coras134a9962018-08-28 11:32:04 -07001132 vcl_session_free (wrk, session);
Florin Coras0d427d82018-06-27 03:24:07 -07001133 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001134
Dave Wallace543852a2017-08-03 02:11:34 -04001135 return rv;
1136}
1137
1138int
Florin Corasf9240dc2019-01-15 08:03:17 -08001139vppcom_session_close (uint32_t session_handle)
1140{
1141 vcl_worker_t *wrk = vcl_worker_get_current ();
1142 vcl_session_t *session;
1143
1144 session = vcl_session_get_w_handle (wrk, session_handle);
1145 if (!session)
1146 return VPPCOM_EBADFD;
1147 return vcl_session_cleanup (wrk, session, session_handle,
1148 1 /* do_disconnect */ );
1149}
1150
1151int
Florin Coras134a9962018-08-28 11:32:04 -07001152vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001153{
Florin Coras134a9962018-08-28 11:32:04 -07001154 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001155 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001156
1157 if (!ep || !ep->ip)
1158 return VPPCOM_EINVAL;
1159
Florin Coras134a9962018-08-28 11:32:04 -07001160 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001161 if (!session)
1162 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001163
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001164 if (session->is_vep)
1165 {
Florin Coras5e062572019-03-14 19:07:51 -07001166 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1167 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001168 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001169 }
1170
Florin Coras7e12d942018-06-27 14:32:43 -07001171 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001172 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001173 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1174 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001175 else
Dave Barach178cf492018-11-13 16:34:13 -05001176 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1177 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001178 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001179
Florin Coras5e062572019-03-14 19:07:51 -07001180 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1181 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001182 session->transport.is_ip4 ? "IPv4" : "IPv6",
1183 format_ip46_address, &session->transport.lcl_ip,
1184 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1185 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001186 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001187 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001188
1189 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001190 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001191
Florin Coras070453d2018-08-24 17:04:27 -07001192 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001193}
1194
1195int
Florin Coras134a9962018-08-28 11:32:04 -07001196vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001197{
Florin Coras134a9962018-08-28 11:32:04 -07001198 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001199 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001200 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001201 int rv;
1202
Florin Coras134a9962018-08-28 11:32:04 -07001203 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001204 if (!listen_session || listen_session->is_vep)
Florin Coras070453d2018-08-24 17:04:27 -07001205 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001206
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001207 if (q_len == 0 || q_len == ~0)
1208 q_len = vcm->cfg.listen_queue_size;
1209
Dave Wallaceee45d412017-11-24 21:44:06 -05001210 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001211 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001212 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001213 VDBG (0, "session %u [0x%llx]: already in listen state!",
1214 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001215 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001216 }
1217
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001218 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001219
Florin Coras070453d2018-08-24 17:04:27 -07001220 /*
1221 * Send listen request to vpp and wait for reply
1222 */
Florin Coras134a9962018-08-28 11:32:04 -07001223 vppcom_send_bind_sock (listen_session);
1224 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1225 STATE_LISTEN,
1226 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001227
Florin Coras070453d2018-08-24 17:04:27 -07001228 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001229 {
Florin Coras134a9962018-08-28 11:32:04 -07001230 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001231 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1232 listen_sh, listen_session->vpp_handle, rv,
1233 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001234 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001235 }
1236
Florin Coras070453d2018-08-24 17:04:27 -07001237 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001238}
1239
Ping Yu34a3a082018-11-30 19:16:17 -05001240int
1241vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1242 uint32_t cert_len)
1243{
1244
1245 vcl_worker_t *wrk = vcl_worker_get_current ();
1246 vcl_session_t *session = 0;
1247
1248 session = vcl_session_get_w_handle (wrk, session_handle);
1249 if (!session)
1250 return VPPCOM_EBADFD;
1251
1252 if (cert_len == 0 || cert_len == ~0)
1253 return VPPCOM_EBADFD;
1254
1255 /*
1256 * Send listen request to vpp and wait for reply
1257 */
1258 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1259
1260 return VPPCOM_OK;
1261
1262}
1263
1264int
1265vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1266 uint32_t key_len)
1267{
1268
1269 vcl_worker_t *wrk = vcl_worker_get_current ();
1270 vcl_session_t *session = 0;
1271
1272 session = vcl_session_get_w_handle (wrk, session_handle);
1273 if (!session)
1274 return VPPCOM_EBADFD;
1275
1276 if (key_len == 0 || key_len == ~0)
1277 return VPPCOM_EBADFD;
1278
1279 /*
1280 * Send listen request to vpp and wait for reply
1281 */
1282 vppcom_send_application_tls_key_add (session, key, key_len);
1283
1284 return VPPCOM_OK;
1285
1286
1287}
1288
Florin Coras134a9962018-08-28 11:32:04 -07001289static int
Florin Coras5e062572019-03-14 19:07:51 -07001290validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001291{
Florin Coras5e062572019-03-14 19:07:51 -07001292 if (ls->is_vep)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001293 {
Florin Coras5e062572019-03-14 19:07:51 -07001294 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1295 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001296 return VPPCOM_EBADFD;
1297 }
1298
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001299 if ((ls->session_state != STATE_LISTEN)
1300 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001301 {
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001302 VDBG (0,
1303 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1304 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001305 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001306 return VPPCOM_EBADFD;
1307 }
1308 return VPPCOM_OK;
1309}
1310
1311int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001312vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1313{
1314 if (!strcmp (proto_str, "TCP"))
1315 *proto = VPPCOM_PROTO_TCP;
1316 else if (!strcmp (proto_str, "tcp"))
1317 *proto = VPPCOM_PROTO_TCP;
1318 else if (!strcmp (proto_str, "UDP"))
1319 *proto = VPPCOM_PROTO_UDP;
1320 else if (!strcmp (proto_str, "udp"))
1321 *proto = VPPCOM_PROTO_UDP;
1322 else if (!strcmp (proto_str, "UDPC"))
1323 *proto = VPPCOM_PROTO_UDPC;
1324 else if (!strcmp (proto_str, "udpc"))
1325 *proto = VPPCOM_PROTO_UDPC;
1326 else if (!strcmp (proto_str, "SCTP"))
1327 *proto = VPPCOM_PROTO_SCTP;
1328 else if (!strcmp (proto_str, "sctp"))
1329 *proto = VPPCOM_PROTO_SCTP;
1330 else if (!strcmp (proto_str, "TLS"))
1331 *proto = VPPCOM_PROTO_TLS;
1332 else if (!strcmp (proto_str, "tls"))
1333 *proto = VPPCOM_PROTO_TLS;
1334 else if (!strcmp (proto_str, "QUIC"))
1335 *proto = VPPCOM_PROTO_QUIC;
1336 else if (!strcmp (proto_str, "quic"))
1337 *proto = VPPCOM_PROTO_QUIC;
1338 else
1339 return 1;
1340 return 0;
1341}
1342
1343int
Florin Coras134a9962018-08-28 11:32:04 -07001344vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001345 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001346{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001347 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001348 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001349 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001350 vcl_session_t *listen_session = 0;
1351 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001352 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001353 svm_msg_q_msg_t msg;
1354 session_event_t *e;
1355 u8 is_nonblocking;
1356 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001357
Florin Coras134a9962018-08-28 11:32:04 -07001358 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001359 if (!listen_session)
1360 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001361
Florin Coras134a9962018-08-28 11:32:04 -07001362 listen_session_index = listen_session->session_index;
1363 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001364 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001365
Florin Coras54693d22018-07-17 10:46:29 -07001366 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001367 {
Florin Coras54693d22018-07-17 10:46:29 -07001368 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001369 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001370 accepted_msg = evt->accepted_msg;
1371 goto handle;
1372 }
1373
1374 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1375 VCL_SESS_ATTR_NONBLOCK);
Florin Coras134a9962018-08-28 11:32:04 -07001376 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
Florin Coras54693d22018-07-17 10:46:29 -07001377 return VPPCOM_EAGAIN;
1378
1379 while (1)
1380 {
Florin Coras134a9962018-08-28 11:32:04 -07001381 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001382 return VPPCOM_EAGAIN;
1383
Florin Coras134a9962018-08-28 11:32:04 -07001384 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001385 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001386 {
Florin Coras00cca802019-06-06 09:38:44 -07001387 VDBG (0, "discarded event: %u", e->event_type);
Florin Coras134a9962018-08-28 11:32:04 -07001388 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001389 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001390 }
Dave Barach178cf492018-11-13 16:34:13 -05001391 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001392 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001393 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001394 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001395
Florin Coras54693d22018-07-17 10:46:29 -07001396handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001397
Florin Coras00cca802019-06-06 09:38:44 -07001398 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1399 listen_session_index);
1400 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1401 return VPPCOM_ECONNABORTED;
1402
Florin Coras134a9962018-08-28 11:32:04 -07001403 listen_session = vcl_session_get (wrk, listen_session_index);
1404 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001405
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001406 if (flags & O_NONBLOCK)
1407 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001408
Florin Coras5e062572019-03-14 19:07:51 -07001409 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1410 " flags %d, is_nonblocking %u", listen_session->session_index,
1411 listen_session->vpp_handle, client_session_index,
1412 client_session->vpp_handle, flags,
1413 VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001414
Dave Wallace048b1d62018-01-03 22:24:41 -05001415 if (ep)
1416 {
Florin Coras7e12d942018-06-27 14:32:43 -07001417 ep->is_ip4 = client_session->transport.is_ip4;
1418 ep->port = client_session->transport.rmt_port;
1419 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001420 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1421 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001422 else
Dave Barach178cf492018-11-13 16:34:13 -05001423 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1424 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001425 }
Dave Wallace60caa062017-11-10 17:07:13 -05001426
Florin Coras05ecfcc2018-12-12 18:19:39 -08001427 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001428 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001429 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001430 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001431 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001432 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001433 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001434 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001435 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001436 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1437 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001438
Florin Coras3c7d4f92018-12-14 11:28:43 -08001439 /*
1440 * Session might have been closed already
1441 */
1442 if (accept_flags)
1443 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001444 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -08001445 client_session->session_state = STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001446 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasb0f662f2018-12-27 14:51:46 -08001447 client_session->session_state = STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001448 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001449 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001450}
1451
1452int
Florin Coras134a9962018-08-28 11:32:04 -07001453vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001454{
Florin Coras134a9962018-08-28 11:32:04 -07001455 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001456 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001457 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001458 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001459
Florin Coras134a9962018-08-28 11:32:04 -07001460 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001461 if (!session)
1462 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001463 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001464
1465 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001466 {
Florin Coras5e062572019-03-14 19:07:51 -07001467 VDBG (0, "ERROR: cannot connect epoll session %u!",
1468 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001469 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001470 }
1471
Florin Coras7e12d942018-06-27 14:32:43 -07001472 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001473 {
Florin Coras7baeb712019-01-04 17:05:43 -08001474 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001475 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001476 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001477 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001478 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001479 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001480 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001481 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001482 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001483 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001484 }
1485
Florin Coras7e12d942018-06-27 14:32:43 -07001486 session->transport.is_ip4 = server_ep->is_ip4;
1487 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001488 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1489 sizeof (ip4_address_t));
Dave Wallaced239f8d2018-06-19 13:37:30 -04001490 else
Dave Barach178cf492018-11-13 16:34:13 -05001491 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1492 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001493 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001494 session->transport_opts = VCL_INVALID_SESSION_HANDLE;
Dave Wallace543852a2017-08-03 02:11:34 -04001495
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001496 VDBG (0, "session handle %u: connecting to server %s %U "
1497 "port %d proto %s", session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001498 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001499 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001500 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001501 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001502 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001503 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001504
Florin Coras070453d2018-08-24 17:04:27 -07001505 /*
1506 * Send connect request and wait for reply from vpp
1507 */
Florin Coras134a9962018-08-28 11:32:04 -07001508 vppcom_send_connect_sock (session);
Florin Coras070453d2018-08-24 17:04:27 -07001509 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1510 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001511
Florin Coras134a9962018-08-28 11:32:04 -07001512 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001513 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1514 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001515
Dave Wallace4878cbe2017-11-21 03:45:09 -05001516 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001517}
1518
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001519int
1520vppcom_session_stream_connect (uint32_t session_handle,
1521 uint32_t parent_session_handle)
1522{
1523 vcl_worker_t *wrk = vcl_worker_get_current ();
1524 vcl_session_t *session, *parent_session;
1525 u32 session_index, parent_session_index;
1526 int rv;
1527
1528 session = vcl_session_get_w_handle (wrk, session_handle);
1529 if (!session)
1530 return VPPCOM_EBADFD;
1531 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1532 if (!parent_session)
1533 return VPPCOM_EBADFD;
1534
1535 session_index = session->session_index;
1536 parent_session_index = parent_session->session_index;
1537 if (PREDICT_FALSE (session->is_vep))
1538 {
1539 VDBG (0, "ERROR: cannot connect epoll session %u!",
1540 session->session_index);
1541 return VPPCOM_EBADFD;
1542 }
1543
1544 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1545 {
1546 VDBG (0, "session handle %u [0x%llx]: session already "
1547 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1548 session_handle, session->vpp_handle,
1549 parent_session_handle, parent_session->vpp_handle,
1550 vppcom_proto_str (session->session_type), session->session_state,
1551 vppcom_session_state_str (session->session_state));
1552 return VPPCOM_OK;
1553 }
1554
1555 /* Connect to quic session specifics */
1556 session->transport.is_ip4 = parent_session->transport.is_ip4;
1557 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1558 session->transport.rmt_port = 0;
1559 session->transport_opts = parent_session->vpp_handle;
1560
1561 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1562 session_handle, parent_session_handle, parent_session->vpp_handle);
1563
1564 /*
1565 * Send connect request and wait for reply from vpp
1566 */
1567 vppcom_send_connect_sock (session);
1568 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1569 vcm->cfg.session_timeout);
1570
1571 session->listener_index = parent_session_index;
1572 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1573 parent_session->n_accepted_sessions++;
1574
1575 session = vcl_session_get (wrk, session_index);
1576 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1577 session->vpp_handle, rv ? "failed" : "succeeded");
1578
1579 return rv;
1580}
1581
Florin Coras54693d22018-07-17 10:46:29 -07001582static u8
1583vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1584{
Florin Corasc0737e92019-03-04 14:19:39 -08001585 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001586}
1587
Steven58f464e2017-10-25 12:33:12 -07001588static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001589vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001590 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001591{
Florin Coras134a9962018-08-28 11:32:04 -07001592 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001593 int n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001594 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001595 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001596 svm_msg_q_msg_t msg;
1597 session_event_t *e;
1598 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001599 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001600
Florin Coras070453d2018-08-24 17:04:27 -07001601 if (PREDICT_FALSE (!buf))
1602 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001603
Florin Coras134a9962018-08-28 11:32:04 -07001604 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001605 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001606 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001607
Florin Coras6d0106e2019-01-29 20:11:58 -08001608 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001609 {
Florin Coras5e062572019-03-14 19:07:51 -07001610 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001611 s->session_index, s->vpp_handle, s->session_state,
1612 vppcom_session_state_str (s->session_state));
1613 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001614 }
1615
Florin Coras2cba8532018-09-11 16:33:36 -07001616 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001617 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001618 mq = wrk->app_event_queue;
1619 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001620 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001621
Sirshak Das28aa5392019-02-05 01:33:33 -06001622 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001623 {
Florin Coras54693d22018-07-17 10:46:29 -07001624 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001625 {
1626 svm_fifo_unset_event (s->rx_fifo);
1627 return VPPCOM_EWOULDBLOCK;
1628 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001629 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001630 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001631 if (vcl_session_is_closing (s))
1632 return vcl_session_closing_error (s);
1633
Florin Corasc0737e92019-03-04 14:19:39 -08001634 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001635 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001636 if (svm_msg_q_is_empty (mq))
1637 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001638
Florin Coras54693d22018-07-17 10:46:29 -07001639 svm_msg_q_sub_w_lock (mq, &msg);
1640 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001641 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001642 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001643 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001644 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001645 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001646 }
Florin Coras54693d22018-07-17 10:46:29 -07001647
Florin Coras460dce62018-07-27 05:45:06 -07001648 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001649 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001650 else
Florin Coras99368312018-08-02 10:45:44 -07001651 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001652
Sirshak Das28aa5392019-02-05 01:33:33 -06001653 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001654 svm_fifo_unset_event (s->rx_fifo);
Florin Coras41c9e042018-09-11 00:10:41 -07001655
Florin Coras317b8e02019-04-17 09:57:46 -07001656 /* Cut-through sessions might request tx notifications on rx fifos */
Florin Coras2d379d82019-06-28 12:45:12 -07001657 if (PREDICT_FALSE (rx_fifo->want_deq_ntf))
Florin Coras317b8e02019-04-17 09:57:46 -07001658 {
1659 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1660 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras2d379d82019-06-28 12:45:12 -07001661 svm_fifo_reset_has_deq_ntf (s->rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001662 }
1663
Florin Coras5e062572019-03-14 19:07:51 -07001664 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1665 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001666
Florin Coras54693d22018-07-17 10:46:29 -07001667 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001668}
1669
Steven58f464e2017-10-25 12:33:12 -07001670int
Florin Coras134a9962018-08-28 11:32:04 -07001671vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001672{
Florin Coras134a9962018-08-28 11:32:04 -07001673 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001674}
1675
1676static int
Florin Coras134a9962018-08-28 11:32:04 -07001677vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001678{
Florin Coras134a9962018-08-28 11:32:04 -07001679 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001680}
1681
Florin Coras2cba8532018-09-11 16:33:36 -07001682int
1683vppcom_session_read_segments (uint32_t session_handle,
1684 vppcom_data_segments_t ds)
1685{
1686 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001687 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001688 vcl_session_t *s = 0;
1689 svm_fifo_t *rx_fifo;
1690 svm_msg_q_msg_t msg;
1691 session_event_t *e;
1692 svm_msg_q_t *mq;
1693 u8 is_ct;
1694
1695 s = vcl_session_get_w_handle (wrk, session_handle);
1696 if (PREDICT_FALSE (!s || s->is_vep))
1697 return VPPCOM_EBADFD;
1698
Florin Coras6d0106e2019-01-29 20:11:58 -08001699 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1700 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001701
1702 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1703 is_ct = vcl_session_is_ct (s);
1704 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1705 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001706 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001707
Florin Coras653e43f2019-03-04 10:56:23 -08001708 if (is_ct)
1709 svm_fifo_unset_event (s->rx_fifo);
1710
Sirshak Das28aa5392019-02-05 01:33:33 -06001711 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001712 {
1713 if (is_nonblocking)
1714 {
1715 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001716 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001717 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001718 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001719 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001720 if (vcl_session_is_closing (s))
1721 return vcl_session_closing_error (s);
1722
Florin Coras2cba8532018-09-11 16:33:36 -07001723 svm_fifo_unset_event (rx_fifo);
1724 svm_msg_q_lock (mq);
1725 if (svm_msg_q_is_empty (mq))
1726 svm_msg_q_wait (mq);
1727
1728 svm_msg_q_sub_w_lock (mq, &msg);
1729 e = svm_msg_q_msg_data (mq, &msg);
1730 svm_msg_q_unlock (mq);
1731 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001732 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001733 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001734 }
1735 }
1736
Florin Coras88001c62019-04-24 14:44:46 -07001737 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001738 svm_fifo_unset_event (rx_fifo);
1739
Florin Coras2cba8532018-09-11 16:33:36 -07001740 return n_read;
1741}
1742
1743void
1744vppcom_session_free_segments (uint32_t session_handle,
1745 vppcom_data_segments_t ds)
1746{
1747 vcl_worker_t *wrk = vcl_worker_get_current ();
1748 vcl_session_t *s;
1749
1750 s = vcl_session_get_w_handle (wrk, session_handle);
1751 if (PREDICT_FALSE (!s || s->is_vep))
1752 return;
1753
Florin Coras88001c62019-04-24 14:44:46 -07001754 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001755}
1756
Florin Coras2cba8532018-09-11 16:33:36 -07001757int
1758vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1759{
1760 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001761 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001762 if (first_copy < max_bytes)
1763 {
Dave Barach178cf492018-11-13 16:34:13 -05001764 clib_memcpy_fast (buf + first_copy, ds[1].data,
1765 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001766 }
1767 return 0;
1768}
1769
Florin Coras54693d22018-07-17 10:46:29 -07001770static u8
1771vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1772{
Florin Corasc0737e92019-03-04 14:19:39 -08001773 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001774}
1775
Florin Coras42ceddb2018-12-12 10:56:01 -08001776static inline int
1777vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1778 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001779{
Florin Coras134a9962018-08-28 11:32:04 -07001780 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001781 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001782 vcl_session_t *s = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001783 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001784 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08001785 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001786 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001787 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001788 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001789
Florin Coras070453d2018-08-24 17:04:27 -07001790 if (PREDICT_FALSE (!buf))
1791 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001792
Florin Coras134a9962018-08-28 11:32:04 -07001793 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001794 if (PREDICT_FALSE (!s))
1795 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001796
Florin Coras460dce62018-07-27 05:45:06 -07001797 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001798 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001799 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1800 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001801 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001802 }
1803
Florin Coras6d0106e2019-01-29 20:11:58 -08001804 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001805 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001806 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1807 s->session_index, s->vpp_handle, s->session_state,
1808 vppcom_session_state_str (s->session_state));
1809 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001810 }
1811
Florin Coras0e88e852018-09-17 22:09:02 -07001812 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001813 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07001814 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06001815
Florin Coras653e43f2019-03-04 10:56:23 -08001816 mq = wrk->app_event_queue;
Sirshak Das28aa5392019-02-05 01:33:33 -06001817 if (svm_fifo_is_full_prod (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001818 {
Florin Coras54693d22018-07-17 10:46:29 -07001819 if (is_nonblocking)
1820 {
Florin Coras070453d2018-08-24 17:04:27 -07001821 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001822 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001823 while (svm_fifo_is_full_prod (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001824 {
Florin Coras2d379d82019-06-28 12:45:12 -07001825 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001826 if (vcl_session_is_closing (s))
1827 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001828 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001829 if (svm_msg_q_is_empty (mq))
1830 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001831
Florin Coras54693d22018-07-17 10:46:29 -07001832 svm_msg_q_sub_w_lock (mq, &msg);
1833 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001834 svm_msg_q_unlock (mq);
1835
Florin Coras0e88e852018-09-17 22:09:02 -07001836 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001837 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001838 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001839 }
Dave Wallace543852a2017-08-03 02:11:34 -04001840 }
Dave Wallace543852a2017-08-03 02:11:34 -04001841
Florin Coras653e43f2019-03-04 10:56:23 -08001842 et = SESSION_IO_EVT_TX;
1843 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08001844 et = SESSION_IO_EVT_TX_FLUSH;
1845
Florin Coras460dce62018-07-27 05:45:06 -07001846 if (s->is_dgram)
1847 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08001848 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001849 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07001850 else
1851 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001852 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08001853
Florin Coras14ed6df2019-03-06 21:13:42 -08001854 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001855 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1856 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001857
Florin Coras460dce62018-07-27 05:45:06 -07001858 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001859
Florin Coras6d0106e2019-01-29 20:11:58 -08001860 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1861 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001862
Florin Coras54693d22018-07-17 10:46:29 -07001863 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001864}
1865
Florin Coras42ceddb2018-12-12 10:56:01 -08001866int
1867vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1868{
1869 return vppcom_session_write_inline (session_handle, buf, n,
1870 0 /* is_flush */ );
1871}
1872
Florin Corasb0f662f2018-12-27 14:51:46 -08001873int
1874vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1875{
1876 return vppcom_session_write_inline (session_handle, buf, n,
1877 1 /* is_flush */ );
1878}
1879
Florin Corasc0737e92019-03-04 14:19:39 -08001880#define vcl_fifo_rx_evt_valid_or_break(_s) \
1881if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1882 { \
1883 if (!vcl_session_is_ct (_s)) \
1884 { \
1885 svm_fifo_unset_event (_s->rx_fifo); \
1886 if (svm_fifo_is_empty (_s->rx_fifo)) \
1887 break; \
1888 } \
1889 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1890 { \
1891 svm_fifo_unset_event (_s->ct_rx_fifo); \
1892 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1893 break; \
1894 } \
1895 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07001896
Florin Coras86f04502018-09-12 16:08:01 -07001897static void
1898vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1899 unsigned long n_bits, unsigned long *read_map,
1900 unsigned long *write_map,
1901 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001902{
1903 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001904 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001905 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001906 u32 sid;
1907
1908 switch (e->event_type)
1909 {
Florin Corasc0737e92019-03-04 14:19:39 -08001910 case SESSION_IO_EVT_RX:
1911 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001912 session = vcl_session_get (wrk, sid);
1913 if (!session)
1914 break;
Florin Corasfff68f72019-03-13 16:01:38 -07001915 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07001916 if (sid < n_bits && read_map)
1917 {
David Johnsond9818dd2018-12-14 14:53:41 -05001918 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001919 *bits_set += 1;
1920 }
1921 break;
Florin Corasfe97da32019-03-06 10:09:04 -08001922 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08001923 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001924 session = vcl_session_get (wrk, sid);
1925 if (!session)
1926 break;
1927 if (sid < n_bits && write_map)
1928 {
David Johnsond9818dd2018-12-14 14:53:41 -05001929 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001930 *bits_set += 1;
1931 }
1932 break;
Florin Coras86f04502018-09-12 16:08:01 -07001933 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001934 session = vcl_session_accepted (wrk,
1935 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001936 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001937 break;
Florin Coras86f04502018-09-12 16:08:01 -07001938 sid = session->session_index;
1939 if (sid < n_bits && read_map)
1940 {
David Johnsond9818dd2018-12-14 14:53:41 -05001941 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001942 *bits_set += 1;
1943 }
1944 break;
1945 case SESSION_CTRL_EVT_CONNECTED:
1946 connected_msg = (session_connected_msg_t *) e->data;
1947 vcl_session_connected_handler (wrk, connected_msg);
1948 break;
1949 case SESSION_CTRL_EVT_DISCONNECTED:
1950 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001951 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1952 if (!session)
1953 break;
1954 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001955 if (sid < n_bits && except_map)
1956 {
David Johnsond9818dd2018-12-14 14:53:41 -05001957 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001958 *bits_set += 1;
1959 }
1960 break;
1961 case SESSION_CTRL_EVT_RESET:
1962 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1963 if (sid < n_bits && except_map)
1964 {
David Johnsond9818dd2018-12-14 14:53:41 -05001965 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001966 *bits_set += 1;
1967 }
1968 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001969 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1970 vcl_session_unlisten_reply_handler (wrk, e->data);
1971 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001972 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1973 vcl_session_worker_update_reply_handler (wrk, e->data);
1974 break;
1975 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1976 vcl_session_req_worker_update_handler (wrk, e->data);
1977 break;
Florin Coras86f04502018-09-12 16:08:01 -07001978 default:
1979 clib_warning ("unhandled: %u", e->event_type);
1980 break;
1981 }
1982}
1983
1984static int
1985vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1986 unsigned long n_bits, unsigned long *read_map,
1987 unsigned long *write_map, unsigned long *except_map,
1988 double time_to_wait, u32 * bits_set)
1989{
Florin Coras99368312018-08-02 10:45:44 -07001990 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001991 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001992 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001993
1994 svm_msg_q_lock (mq);
1995 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001996 {
Florin Coras54693d22018-07-17 10:46:29 -07001997 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001998 {
Florin Coras54693d22018-07-17 10:46:29 -07001999 svm_msg_q_unlock (mq);
2000 return 0;
2001 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002002
Florin Coras54693d22018-07-17 10:46:29 -07002003 if (!time_to_wait)
2004 {
2005 svm_msg_q_unlock (mq);
2006 return 0;
2007 }
2008 else if (time_to_wait < 0)
2009 {
2010 svm_msg_q_wait (mq);
2011 }
2012 else
2013 {
2014 if (svm_msg_q_timedwait (mq, time_to_wait))
2015 {
2016 svm_msg_q_unlock (mq);
2017 return 0;
2018 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002019 }
2020 }
Florin Corase003a1b2019-06-05 10:47:16 -07002021 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002022 svm_msg_q_unlock (mq);
2023
Florin Coras134a9962018-08-28 11:32:04 -07002024 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002025 {
Florin Coras134a9962018-08-28 11:32:04 -07002026 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002027 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002028 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2029 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002030 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002031 }
Florin Coras134a9962018-08-28 11:32:04 -07002032 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002033 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002034 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002035}
2036
Florin Coras99368312018-08-02 10:45:44 -07002037static int
Florin Coras294afe22019-01-07 17:49:17 -08002038vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2039 vcl_si_set * read_map, vcl_si_set * write_map,
2040 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002041 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002042{
Florin Coras14ed6df2019-03-06 21:13:42 -08002043 double wait = 0, start = 0;
2044
2045 if (!*bits_set)
2046 {
2047 wait = time_to_wait;
2048 start = clib_time_now (&wrk->clib_time);
2049 }
2050
2051 do
2052 {
2053 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2054 write_map, except_map, wait, bits_set);
2055 if (*bits_set)
2056 return *bits_set;
2057 if (wait == -1)
2058 continue;
2059
2060 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2061 }
2062 while (wait > 0);
2063
2064 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002065}
2066
2067static int
Florin Coras294afe22019-01-07 17:49:17 -08002068vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2069 vcl_si_set * read_map, vcl_si_set * write_map,
2070 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002071 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002072{
2073 vcl_mq_evt_conn_t *mqc;
2074 int __clib_unused n_read;
2075 int n_mq_evts, i;
2076 u64 buf;
2077
Florin Coras134a9962018-08-28 11:32:04 -07002078 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2079 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2080 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002081 for (i = 0; i < n_mq_evts; i++)
2082 {
Florin Coras134a9962018-08-28 11:32:04 -07002083 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002084 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002085 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002086 except_map, 0, bits_set);
2087 }
2088
2089 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2090}
2091
Dave Wallace543852a2017-08-03 02:11:34 -04002092int
Florin Coras294afe22019-01-07 17:49:17 -08002093vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2094 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002095{
Florin Coras54693d22018-07-17 10:46:29 -07002096 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002097 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002098 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002099 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04002100
Dave Wallace7876d392017-10-19 03:53:57 -04002101 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002102 {
Florin Coras134a9962018-08-28 11:32:04 -07002103 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002104 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002105 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2106 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002107 }
Dave Wallace7876d392017-10-19 03:53:57 -04002108 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002109 {
Florin Coras134a9962018-08-28 11:32:04 -07002110 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002111 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002112 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2113 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002114 }
Dave Wallace7876d392017-10-19 03:53:57 -04002115 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002116 {
Florin Coras134a9962018-08-28 11:32:04 -07002117 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002118 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002119 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2120 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002121 }
2122
Florin Coras54693d22018-07-17 10:46:29 -07002123 if (!n_bits)
2124 return 0;
2125
2126 if (!write_map)
2127 goto check_rd;
2128
2129 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002130 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2131 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002132 {
Florin Coras47c40e22018-11-26 17:01:36 -08002133 if (except_map && sid < minbits)
2134 clib_bitmap_set_no_check (except_map, sid, 1);
2135 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002136 }
2137
Sirshak Das28aa5392019-02-05 01:33:33 -06002138 rv = svm_fifo_is_full_prod (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002139 if (!rv)
2140 {
David Johnsond9818dd2018-12-14 14:53:41 -05002141 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002142 bits_set++;
2143 }
Florin Coras294afe22019-01-07 17:49:17 -08002144 else
Florin Coras2d379d82019-06-28 12:45:12 -07002145 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002146 }));
2147
2148check_rd:
2149 if (!read_map)
2150 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002151
Florin Coras134a9962018-08-28 11:32:04 -07002152 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2153 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002154 {
Florin Coras47c40e22018-11-26 17:01:36 -08002155 if (except_map && sid < minbits)
2156 clib_bitmap_set_no_check (except_map, sid, 1);
2157 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002158 }
2159
Florin Coras0ef8ef22019-01-18 08:37:13 -08002160 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002161 if (rv)
2162 {
David Johnsond9818dd2018-12-14 14:53:41 -05002163 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002164 bits_set++;
2165 }
2166 }));
2167 /* *INDENT-ON* */
2168
2169check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002170
Florin Coras86f04502018-09-12 16:08:01 -07002171 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2172 {
2173 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2174 read_map, write_map, except_map, &bits_set);
2175 }
2176 vec_reset_length (wrk->unhandled_evts_vector);
2177
Florin Coras99368312018-08-02 10:45:44 -07002178 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002179 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002180 time_to_wait, &bits_set);
2181 else
Florin Coras134a9962018-08-28 11:32:04 -07002182 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002183 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002184
Dave Wallace543852a2017-08-03 02:11:34 -04002185 return (bits_set);
2186}
2187
Dave Wallacef7f809c2017-10-03 01:48:42 -04002188static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002189vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002190{
Florin Coras7e12d942018-06-27 14:32:43 -07002191 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002192 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002193 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002194
Florin Corasc0737e92019-03-04 14:19:39 -08002195 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002196 return;
2197
Florin Coras134a9962018-08-28 11:32:04 -07002198 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002199 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002200 {
Florin Coras5e062572019-03-14 19:07:51 -07002201 VDBG (0, "ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002202 goto done;
2203 }
2204 if (PREDICT_FALSE (!session->is_vep))
2205 {
Florin Coras5e062572019-03-14 19:07:51 -07002206 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002207 goto done;
2208 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002209 vep = &session->vep;
Florin Coras5e062572019-03-14 19:07:51 -07002210 VDBG (0, "vep_idx (%u): Dumping epoll chain\n"
2211 "{\n"
2212 " is_vep = %u\n"
2213 " is_vep_session = %u\n"
2214 " next_sid = 0x%x (%u)\n"
2215 "}\n", vep_idx, session->is_vep, session->is_vep_session,
2216 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002217
Florin Coras134a9962018-08-28 11:32:04 -07002218 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002219 {
Florin Coras134a9962018-08-28 11:32:04 -07002220 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002221 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002222 {
Florin Coras5e062572019-03-14 19:07:51 -07002223 VDBG (0, "ERROR: Invalid sid (%u)!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002224 goto done;
2225 }
2226 if (PREDICT_FALSE (session->is_vep))
Florin Coras5e062572019-03-14 19:07:51 -07002227 {
2228 VDBG (0, "ERROR: sid (%u) is a vep!", vep_idx);
2229 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002230 else if (PREDICT_FALSE (!session->is_vep_session))
2231 {
Florin Coras5e062572019-03-14 19:07:51 -07002232 VDBG (0, "ERROR: session (%u) is not a vep session!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002233 goto done;
2234 }
2235 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002236 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Florin Coras5e062572019-03-14 19:07:51 -07002237 VDBG (0, "ERROR: session (%u) vep_idx (%u) != vep_idx (%u)!",
2238 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002239 if (session->is_vep_session)
2240 {
Florin Coras5e062572019-03-14 19:07:51 -07002241 VDBG (0, "vep_idx[%u]: sid 0x%x (%u)\n"
2242 "{\n"
2243 " next_sid = 0x%x (%u)\n"
2244 " prev_sid = 0x%x (%u)\n"
2245 " vep_idx = 0x%x (%u)\n"
2246 " ev.events = 0x%x\n"
2247 " ev.data.u64 = 0x%llx\n"
2248 " et_mask = 0x%x\n"
2249 "}\n",
2250 vep_idx, sid, sid, vep->next_sh, vep->next_sh, vep->prev_sh,
2251 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2252 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002253 }
2254 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002255
2256done:
Florin Coras5e062572019-03-14 19:07:51 -07002257 VDBG (0, "vep_idx (%u): Dump complete!\n", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002258}
2259
2260int
2261vppcom_epoll_create (void)
2262{
Florin Coras134a9962018-08-28 11:32:04 -07002263 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002264 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002265
Florin Coras134a9962018-08-28 11:32:04 -07002266 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002267
2268 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002269 vep_session->vep.vep_sh = ~0;
2270 vep_session->vep.next_sh = ~0;
2271 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002272 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002273
Florin Corasa7a1a222018-12-30 17:11:31 -08002274 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2275 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002276
Florin Corasab2f6db2018-08-31 14:31:41 -07002277 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002278}
2279
2280int
Florin Coras134a9962018-08-28 11:32:04 -07002281vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002282 struct epoll_event *event)
2283{
Florin Coras134a9962018-08-28 11:32:04 -07002284 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002285 vcl_session_t *vep_session;
2286 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002287 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002288
Florin Coras134a9962018-08-28 11:32:04 -07002289 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002290 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002291 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002292 return VPPCOM_EINVAL;
2293 }
2294
Florin Coras134a9962018-08-28 11:32:04 -07002295 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002296 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002297 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002298 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002299 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002300 }
2301 if (PREDICT_FALSE (!vep_session->is_vep))
2302 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002303 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002304 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002305 }
2306
Florin Coras134a9962018-08-28 11:32:04 -07002307 ASSERT (vep_session->vep.vep_sh == ~0);
2308 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002309
Florin Coras134a9962018-08-28 11:32:04 -07002310 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002311 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002312 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002313 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002314 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002315 }
2316 if (PREDICT_FALSE (session->is_vep))
2317 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002318 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002319 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002320 }
2321
2322 switch (op)
2323 {
2324 case EPOLL_CTL_ADD:
2325 if (PREDICT_FALSE (!event))
2326 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002327 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002328 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002329 }
Florin Coras134a9962018-08-28 11:32:04 -07002330 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002331 {
Florin Coras7e12d942018-06-27 14:32:43 -07002332 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002333 next_session = vcl_session_get_w_handle (wrk,
2334 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002335 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002336 {
Florin Coras5e062572019-03-14 19:07:51 -07002337 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002338 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002339 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002340 }
Florin Coras134a9962018-08-28 11:32:04 -07002341 ASSERT (next_session->vep.prev_sh == vep_handle);
2342 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002343 }
Florin Coras134a9962018-08-28 11:32:04 -07002344 session->vep.next_sh = vep_session->vep.next_sh;
2345 session->vep.prev_sh = vep_handle;
2346 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002347 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2348 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002349 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002350 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002351 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002352
Florin Coras1bcad5c2019-01-09 20:04:38 -08002353 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002354 svm_fifo_add_want_deq_ntf (session->tx_fifo,
2355 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002356
Florin Corasa7a1a222018-12-30 17:11:31 -08002357 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2358 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002359 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002360 break;
2361
2362 case EPOLL_CTL_MOD:
2363 if (PREDICT_FALSE (!event))
2364 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002365 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002366 rv = VPPCOM_EINVAL;
2367 goto done;
2368 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002369 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002370 {
Florin Coras5e062572019-03-14 19:07:51 -07002371 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002372 rv = VPPCOM_EINVAL;
2373 goto done;
2374 }
Florin Coras134a9962018-08-28 11:32:04 -07002375 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002376 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002377 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2378 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002379 rv = VPPCOM_EINVAL;
2380 goto done;
2381 }
2382 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2383 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002384 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2385 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002386 break;
2387
2388 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002389 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002390 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002391 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002392 rv = VPPCOM_EINVAL;
2393 goto done;
2394 }
Florin Coras134a9962018-08-28 11:32:04 -07002395 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002396 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002397 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2398 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002399 rv = VPPCOM_EINVAL;
2400 goto done;
2401 }
2402
Florin Coras134a9962018-08-28 11:32:04 -07002403 if (session->vep.prev_sh == vep_handle)
2404 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002405 else
2406 {
Florin Coras7e12d942018-06-27 14:32:43 -07002407 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002408 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002409 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002410 {
Florin Coras5e062572019-03-14 19:07:51 -07002411 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002412 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002413 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002414 }
Florin Coras134a9962018-08-28 11:32:04 -07002415 ASSERT (prev_session->vep.next_sh == session_handle);
2416 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002417 }
Florin Coras134a9962018-08-28 11:32:04 -07002418 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002419 {
Florin Coras7e12d942018-06-27 14:32:43 -07002420 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002421 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002422 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002423 {
Florin Coras5e062572019-03-14 19:07:51 -07002424 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002425 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002426 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002427 }
Florin Coras134a9962018-08-28 11:32:04 -07002428 ASSERT (next_session->vep.prev_sh == session_handle);
2429 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002430 }
2431
2432 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002433 session->vep.next_sh = ~0;
2434 session->vep.prev_sh = ~0;
2435 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002436 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002437
2438 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002439 svm_fifo_del_want_deq_ntf (session->tx_fifo, SVM_FIFO_NO_DEQ_NOTIF);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002440
Florin Coras5e062572019-03-14 19:07:51 -07002441 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002442 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002443 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002444 break;
2445
2446 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002447 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002448 rv = VPPCOM_EINVAL;
2449 }
2450
Florin Coras134a9962018-08-28 11:32:04 -07002451 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002452
2453done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002454 return rv;
2455}
2456
Florin Coras86f04502018-09-12 16:08:01 -07002457static inline void
2458vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2459 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002460{
2461 session_disconnected_msg_t *disconnected_msg;
2462 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002463 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002464 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002465 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002466 u8 add_event = 0;
2467
2468 switch (e->event_type)
2469 {
Florin Coras653e43f2019-03-04 10:56:23 -08002470 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002471 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002472 if (!(session = vcl_session_get (wrk, sid)))
2473 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002474 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002475 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002476 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002477 break;
2478 add_event = 1;
2479 events[*num_ev].events |= EPOLLIN;
2480 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002481 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002482 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002483 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002484 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002485 if (!(session = vcl_session_get (wrk, sid)))
2486 break;
Florin Coras86f04502018-09-12 16:08:01 -07002487 session_events = session->vep.ev.events;
2488 if (!(EPOLLOUT & session_events))
2489 break;
2490 add_event = 1;
2491 events[*num_ev].events |= EPOLLOUT;
2492 session_evt_data = session->vep.ev.data.u64;
Florin Coras2d379d82019-06-28 12:45:12 -07002493 svm_fifo_reset_has_deq_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002494 break;
Florin Coras86f04502018-09-12 16:08:01 -07002495 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002496 session = vcl_session_accepted (wrk,
2497 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002498 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002499 break;
Florin Coras86f04502018-09-12 16:08:01 -07002500
Florin Coras86f04502018-09-12 16:08:01 -07002501 session_events = session->vep.ev.events;
2502 if (!(EPOLLIN & session_events))
2503 break;
2504
2505 add_event = 1;
2506 events[*num_ev].events |= EPOLLIN;
2507 session_evt_data = session->vep.ev.data.u64;
2508 break;
2509 case SESSION_CTRL_EVT_CONNECTED:
2510 connected_msg = (session_connected_msg_t *) e->data;
2511 vcl_session_connected_handler (wrk, connected_msg);
2512 /* Generate EPOLLOUT because there's no connected event */
2513 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002514 if (!(session = vcl_session_get (wrk, sid)))
2515 break;
Florin Coras86f04502018-09-12 16:08:01 -07002516 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002517 if (!(EPOLLOUT & session_events))
2518 break;
2519 add_event = 1;
2520 events[*num_ev].events |= EPOLLOUT;
2521 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002522 break;
2523 case SESSION_CTRL_EVT_DISCONNECTED:
2524 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002525 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2526 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002527 break;
Florin Coras72f77822019-01-22 19:05:52 -08002528 session_events = session->vep.ev.events;
2529 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2530 break;
Florin Coras86f04502018-09-12 16:08:01 -07002531 add_event = 1;
2532 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2533 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002534 break;
2535 case SESSION_CTRL_EVT_RESET:
2536 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2537 if (!(session = vcl_session_get (wrk, sid)))
2538 break;
Florin Coras72f77822019-01-22 19:05:52 -08002539 session_events = session->vep.ev.events;
2540 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2541 break;
Florin Coras86f04502018-09-12 16:08:01 -07002542 add_event = 1;
2543 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2544 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002545 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002546 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2547 vcl_session_unlisten_reply_handler (wrk, e->data);
2548 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002549 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2550 vcl_session_req_worker_update_handler (wrk, e->data);
2551 break;
2552 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2553 vcl_session_worker_update_reply_handler (wrk, e->data);
2554 break;
Florin Coras86f04502018-09-12 16:08:01 -07002555 default:
2556 VDBG (0, "unhandled: %u", e->event_type);
2557 break;
2558 }
2559
2560 if (add_event)
2561 {
2562 events[*num_ev].data.u64 = session_evt_data;
2563 if (EPOLLONESHOT & session_events)
2564 {
2565 session = vcl_session_get (wrk, sid);
2566 session->vep.ev.events = 0;
2567 }
2568 *num_ev += 1;
2569 }
2570}
2571
2572static int
2573vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2574 struct epoll_event *events, u32 maxevents,
2575 double wait_for_time, u32 * num_ev)
2576{
Florin Coras99368312018-08-02 10:45:44 -07002577 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002578 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002579 int i;
2580
Florin Coras539663c2018-09-28 14:59:37 -07002581 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2582 goto handle_dequeued;
2583
Florin Coras54693d22018-07-17 10:46:29 -07002584 svm_msg_q_lock (mq);
2585 if (svm_msg_q_is_empty (mq))
2586 {
2587 if (!wait_for_time)
2588 {
2589 svm_msg_q_unlock (mq);
2590 return 0;
2591 }
2592 else if (wait_for_time < 0)
2593 {
2594 svm_msg_q_wait (mq);
2595 }
2596 else
2597 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002598 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002599 {
2600 svm_msg_q_unlock (mq);
2601 return 0;
2602 }
2603 }
2604 }
Florin Corase003a1b2019-06-05 10:47:16 -07002605 ASSERT (maxevents > *num_ev);
2606 vcl_mq_dequeue_batch (wrk, mq, maxevents - *num_ev);
Florin Coras54693d22018-07-17 10:46:29 -07002607 svm_msg_q_unlock (mq);
2608
Florin Coras539663c2018-09-28 14:59:37 -07002609handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002610 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002611 {
Florin Coras134a9962018-08-28 11:32:04 -07002612 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002613 e = svm_msg_q_msg_data (mq, msg);
Florin Corase003a1b2019-06-05 10:47:16 -07002614 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
Florin Coras99368312018-08-02 10:45:44 -07002615 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002616 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002617 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002618 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002619 return *num_ev;
2620}
2621
Florin Coras99368312018-08-02 10:45:44 -07002622static int
Florin Coras134a9962018-08-28 11:32:04 -07002623vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002624 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002625{
Florin Corase003a1b2019-06-05 10:47:16 -07002626 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002627
2628 if (!n_evts)
2629 {
2630 wait = wait_for_time;
2631 start = clib_time_now (&wrk->clib_time);
2632 }
2633
2634 do
2635 {
2636 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2637 wait, &n_evts);
2638 if (n_evts)
2639 return n_evts;
2640 if (wait == -1)
2641 continue;
2642
Florin Corase003a1b2019-06-05 10:47:16 -07002643 now = clib_time_now (&wrk->clib_time);
2644 wait -= now - start;
2645 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002646 }
2647 while (wait > 0);
2648
2649 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002650}
2651
2652static int
Florin Coras134a9962018-08-28 11:32:04 -07002653vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002654 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002655{
2656 vcl_mq_evt_conn_t *mqc;
2657 int __clib_unused n_read;
2658 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002659 u64 buf;
2660
Florin Coras134a9962018-08-28 11:32:04 -07002661 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002662again:
Florin Coras134a9962018-08-28 11:32:04 -07002663 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2664 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002665 for (i = 0; i < n_mq_evts; i++)
2666 {
Florin Coras134a9962018-08-28 11:32:04 -07002667 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002668 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002669 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002670 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002671 if (!n_evts && n_mq_evts > 0)
2672 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002673
2674 return (int) n_evts;
2675}
2676
Dave Wallacef7f809c2017-10-03 01:48:42 -04002677int
Florin Coras134a9962018-08-28 11:32:04 -07002678vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002679 int maxevents, double wait_for_time)
2680{
Florin Coras134a9962018-08-28 11:32:04 -07002681 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002682 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002683 u32 n_evts = 0;
2684 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002685
2686 if (PREDICT_FALSE (maxevents <= 0))
2687 {
Florin Coras5e062572019-03-14 19:07:51 -07002688 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002689 return VPPCOM_EINVAL;
2690 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002691
Florin Coras134a9962018-08-28 11:32:04 -07002692 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002693 if (!vep_session)
2694 return VPPCOM_EBADFD;
2695
Florin Coras54693d22018-07-17 10:46:29 -07002696 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002697 {
Florin Coras5e062572019-03-14 19:07:51 -07002698 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002699 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002700 }
Florin Coras54693d22018-07-17 10:46:29 -07002701
2702 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002703
Florin Coras86f04502018-09-12 16:08:01 -07002704 if (vec_len (wrk->unhandled_evts_vector))
2705 {
2706 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2707 {
2708 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2709 events, &n_evts);
2710 if (n_evts == maxevents)
2711 {
Florin Corase003a1b2019-06-05 10:47:16 -07002712 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
2713 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07002714 }
2715 }
Florin Corase003a1b2019-06-05 10:47:16 -07002716 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07002717 }
2718
2719 if (vcm->cfg.use_mq_eventfd)
2720 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2721 wait_for_time);
2722
2723 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2724 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002725}
2726
Dave Wallace35830af2017-10-09 01:43:42 -04002727int
Florin Coras134a9962018-08-28 11:32:04 -07002728vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002729 void *buffer, uint32_t * buflen)
2730{
Florin Coras134a9962018-08-28 11:32:04 -07002731 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002732 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002733 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002734 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002735 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002736
Florin Coras134a9962018-08-28 11:32:04 -07002737 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002738 if (!session)
2739 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002740
Dave Wallace35830af2017-10-09 01:43:42 -04002741 switch (op)
2742 {
2743 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002744 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002745 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2746 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002747 break;
2748
Dave Wallace227867f2017-11-13 21:21:53 -05002749 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002750 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002751 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2752 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002753 break;
2754
2755 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002756 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002757 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002758 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2759 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002760 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07002761 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2762 "is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002763 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002764 }
2765 else
2766 rv = VPPCOM_EINVAL;
2767 break;
2768
2769 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002770 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002771 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002772 if (*flags & O_NONBLOCK)
2773 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2774 else
2775 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2776
Florin Coras5e062572019-03-14 19:07:51 -07002777 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2778 " is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002779 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002780 }
2781 else
2782 rv = VPPCOM_EINVAL;
2783 break;
2784
2785 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002786 if (PREDICT_TRUE (buffer && buflen &&
2787 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002788 {
Florin Coras7e12d942018-06-27 14:32:43 -07002789 ep->is_ip4 = session->transport.is_ip4;
2790 ep->port = session->transport.rmt_port;
2791 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002792 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2793 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002794 else
Dave Barach178cf492018-11-13 16:34:13 -05002795 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2796 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002797 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002798 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2799 "addr = %U, port %u", session_handle, ep->is_ip4,
2800 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002801 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2802 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002803 }
2804 else
2805 rv = VPPCOM_EINVAL;
2806 break;
2807
2808 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002809 if (PREDICT_TRUE (buffer && buflen &&
2810 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002811 {
Florin Coras7e12d942018-06-27 14:32:43 -07002812 ep->is_ip4 = session->transport.is_ip4;
2813 ep->port = session->transport.lcl_port;
2814 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002815 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2816 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002817 else
Dave Barach178cf492018-11-13 16:34:13 -05002818 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2819 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002820 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002821 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2822 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002823 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002824 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2825 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002826 }
2827 else
2828 rv = VPPCOM_EINVAL;
2829 break;
Stevenb5a11602017-10-11 09:59:30 -07002830
Dave Wallace048b1d62018-01-03 22:24:41 -05002831 case VPPCOM_ATTR_GET_LIBC_EPFD:
2832 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07002833 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002834 break;
2835
2836 case VPPCOM_ATTR_SET_LIBC_EPFD:
2837 if (PREDICT_TRUE (buffer && buflen &&
2838 (*buflen == sizeof (session->libc_epfd))))
2839 {
2840 session->libc_epfd = *(int *) buffer;
2841 *buflen = sizeof (session->libc_epfd);
2842
Florin Coras5e062572019-03-14 19:07:51 -07002843 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2844 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002845 }
2846 else
2847 rv = VPPCOM_EINVAL;
2848 break;
2849
2850 case VPPCOM_ATTR_GET_PROTOCOL:
2851 if (buffer && buflen && (*buflen >= sizeof (int)))
2852 {
Florin Coras7e12d942018-06-27 14:32:43 -07002853 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002854 *buflen = sizeof (int);
2855
Florin Coras5e062572019-03-14 19:07:51 -07002856 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2857 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002858 }
2859 else
2860 rv = VPPCOM_EINVAL;
2861 break;
2862
2863 case VPPCOM_ATTR_GET_LISTEN:
2864 if (buffer && buflen && (*buflen >= sizeof (int)))
2865 {
2866 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2867 VCL_SESS_ATTR_LISTEN);
2868 *buflen = sizeof (int);
2869
Florin Coras5e062572019-03-14 19:07:51 -07002870 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
2871 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002872 }
2873 else
2874 rv = VPPCOM_EINVAL;
2875 break;
2876
2877 case VPPCOM_ATTR_GET_ERROR:
2878 if (buffer && buflen && (*buflen >= sizeof (int)))
2879 {
2880 *(int *) buffer = 0;
2881 *buflen = sizeof (int);
2882
Florin Coras5e062572019-03-14 19:07:51 -07002883 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2884 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002885 }
2886 else
2887 rv = VPPCOM_EINVAL;
2888 break;
2889
2890 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2891 if (buffer && buflen && (*buflen >= sizeof (u32)))
2892 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002893
2894 /* VPP-TBD */
2895 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002896 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002897 vcm->cfg.tx_fifo_size);
2898 *buflen = sizeof (u32);
2899
Florin Coras5e062572019-03-14 19:07:51 -07002900 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2901 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
2902 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002903 }
2904 else
2905 rv = VPPCOM_EINVAL;
2906 break;
2907
2908 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2909 if (buffer && buflen && (*buflen == sizeof (u32)))
2910 {
2911 /* VPP-TBD */
2912 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002913 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2914 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2915 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002916 }
2917 else
2918 rv = VPPCOM_EINVAL;
2919 break;
2920
2921 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2922 if (buffer && buflen && (*buflen >= sizeof (u32)))
2923 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002924
2925 /* VPP-TBD */
2926 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002927 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002928 vcm->cfg.rx_fifo_size);
2929 *buflen = sizeof (u32);
2930
Florin Coras5e062572019-03-14 19:07:51 -07002931 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
2932 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002933 }
2934 else
2935 rv = VPPCOM_EINVAL;
2936 break;
2937
2938 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2939 if (buffer && buflen && (*buflen == sizeof (u32)))
2940 {
2941 /* VPP-TBD */
2942 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002943 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
2944 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2945 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002946 }
2947 else
2948 rv = VPPCOM_EINVAL;
2949 break;
2950
2951 case VPPCOM_ATTR_GET_REUSEADDR:
2952 if (buffer && buflen && (*buflen >= sizeof (int)))
2953 {
2954 /* VPP-TBD */
2955 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2956 VCL_SESS_ATTR_REUSEADDR);
2957 *buflen = sizeof (int);
2958
Florin Coras5e062572019-03-14 19:07:51 -07002959 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2960 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002961 }
2962 else
2963 rv = VPPCOM_EINVAL;
2964 break;
2965
Stevenb5a11602017-10-11 09:59:30 -07002966 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002967 if (buffer && buflen && (*buflen == sizeof (int)) &&
2968 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2969 {
2970 /* VPP-TBD */
2971 if (*(int *) buffer)
2972 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2973 else
2974 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2975
Florin Coras5e062572019-03-14 19:07:51 -07002976 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2977 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
2978 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002979 }
2980 else
2981 rv = VPPCOM_EINVAL;
2982 break;
2983
2984 case VPPCOM_ATTR_GET_REUSEPORT:
2985 if (buffer && buflen && (*buflen >= sizeof (int)))
2986 {
2987 /* VPP-TBD */
2988 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2989 VCL_SESS_ATTR_REUSEPORT);
2990 *buflen = sizeof (int);
2991
Florin Coras5e062572019-03-14 19:07:51 -07002992 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2993 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002994 }
2995 else
2996 rv = VPPCOM_EINVAL;
2997 break;
2998
2999 case VPPCOM_ATTR_SET_REUSEPORT:
3000 if (buffer && buflen && (*buflen == sizeof (int)) &&
3001 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3002 {
3003 /* VPP-TBD */
3004 if (*(int *) buffer)
3005 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3006 else
3007 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3008
Florin Coras5e062572019-03-14 19:07:51 -07003009 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3010 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
3011 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003012 }
3013 else
3014 rv = VPPCOM_EINVAL;
3015 break;
3016
3017 case VPPCOM_ATTR_GET_BROADCAST:
3018 if (buffer && buflen && (*buflen >= sizeof (int)))
3019 {
3020 /* VPP-TBD */
3021 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3022 VCL_SESS_ATTR_BROADCAST);
3023 *buflen = sizeof (int);
3024
Florin Coras5e062572019-03-14 19:07:51 -07003025 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3026 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003027 }
3028 else
3029 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003030 break;
3031
3032 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003033 if (buffer && buflen && (*buflen == sizeof (int)))
3034 {
3035 /* VPP-TBD */
3036 if (*(int *) buffer)
3037 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3038 else
3039 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3040
Florin Coras5e062572019-03-14 19:07:51 -07003041 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3042 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
3043 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003044 }
3045 else
3046 rv = VPPCOM_EINVAL;
3047 break;
3048
3049 case VPPCOM_ATTR_GET_V6ONLY:
3050 if (buffer && buflen && (*buflen >= sizeof (int)))
3051 {
3052 /* VPP-TBD */
3053 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3054 VCL_SESS_ATTR_V6ONLY);
3055 *buflen = sizeof (int);
3056
Florin Coras5e062572019-03-14 19:07:51 -07003057 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3058 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003059 }
3060 else
3061 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003062 break;
3063
3064 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003065 if (buffer && buflen && (*buflen == sizeof (int)))
3066 {
3067 /* VPP-TBD */
3068 if (*(int *) buffer)
3069 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3070 else
3071 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3072
Florin Coras5e062572019-03-14 19:07:51 -07003073 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3074 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
3075 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003076 }
3077 else
3078 rv = VPPCOM_EINVAL;
3079 break;
3080
3081 case VPPCOM_ATTR_GET_KEEPALIVE:
3082 if (buffer && buflen && (*buflen >= sizeof (int)))
3083 {
3084 /* VPP-TBD */
3085 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3086 VCL_SESS_ATTR_KEEPALIVE);
3087 *buflen = sizeof (int);
3088
Florin Coras5e062572019-03-14 19:07:51 -07003089 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3090 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003091 }
3092 else
3093 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003094 break;
Stevenbccd3392017-10-12 20:42:21 -07003095
3096 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003097 if (buffer && buflen && (*buflen == sizeof (int)))
3098 {
3099 /* VPP-TBD */
3100 if (*(int *) buffer)
3101 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3102 else
3103 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3104
Florin Coras5e062572019-03-14 19:07:51 -07003105 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3106 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
3107 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003108 }
3109 else
3110 rv = VPPCOM_EINVAL;
3111 break;
3112
3113 case VPPCOM_ATTR_GET_TCP_NODELAY:
3114 if (buffer && buflen && (*buflen >= sizeof (int)))
3115 {
3116 /* VPP-TBD */
3117 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3118 VCL_SESS_ATTR_TCP_NODELAY);
3119 *buflen = sizeof (int);
3120
Florin Coras5e062572019-03-14 19:07:51 -07003121 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3122 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003123 }
3124 else
3125 rv = VPPCOM_EINVAL;
3126 break;
3127
3128 case VPPCOM_ATTR_SET_TCP_NODELAY:
3129 if (buffer && buflen && (*buflen == sizeof (int)))
3130 {
3131 /* VPP-TBD */
3132 if (*(int *) buffer)
3133 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3134 else
3135 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3136
Florin Coras5e062572019-03-14 19:07:51 -07003137 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3138 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3139 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003140 }
3141 else
3142 rv = VPPCOM_EINVAL;
3143 break;
3144
3145 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3146 if (buffer && buflen && (*buflen >= sizeof (int)))
3147 {
3148 /* VPP-TBD */
3149 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3150 VCL_SESS_ATTR_TCP_KEEPIDLE);
3151 *buflen = sizeof (int);
3152
Florin Coras5e062572019-03-14 19:07:51 -07003153 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3154 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003155 }
3156 else
3157 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003158 break;
3159
3160 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003161 if (buffer && buflen && (*buflen == sizeof (int)))
3162 {
3163 /* VPP-TBD */
3164 if (*(int *) buffer)
3165 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3166 else
3167 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3168
Florin Coras5e062572019-03-14 19:07:51 -07003169 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003170 VCL_SESS_ATTR_TEST (session->attr,
3171 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003172 }
3173 else
3174 rv = VPPCOM_EINVAL;
3175 break;
3176
3177 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3178 if (buffer && buflen && (*buflen >= sizeof (int)))
3179 {
3180 /* VPP-TBD */
3181 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3182 VCL_SESS_ATTR_TCP_KEEPINTVL);
3183 *buflen = sizeof (int);
3184
Florin Coras5e062572019-03-14 19:07:51 -07003185 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3186 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003187 }
3188 else
3189 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003190 break;
3191
3192 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003193 if (buffer && buflen && (*buflen == sizeof (int)))
3194 {
3195 /* VPP-TBD */
3196 if (*(int *) buffer)
3197 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3198 else
3199 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3200
Florin Coras5e062572019-03-14 19:07:51 -07003201 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003202 VCL_SESS_ATTR_TEST (session->attr,
3203 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003204 }
3205 else
3206 rv = VPPCOM_EINVAL;
3207 break;
3208
3209 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3210 if (buffer && buflen && (*buflen >= sizeof (u32)))
3211 {
3212 /* VPP-TBD */
3213 *(u32 *) buffer = session->user_mss;
3214 *buflen = sizeof (int);
3215
Florin Coras5e062572019-03-14 19:07:51 -07003216 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3217 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003218 }
3219 else
3220 rv = VPPCOM_EINVAL;
3221 break;
3222
3223 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3224 if (buffer && buflen && (*buflen == sizeof (u32)))
3225 {
3226 /* VPP-TBD */
3227 session->user_mss = *(u32 *) buffer;
3228
Florin Coras5e062572019-03-14 19:07:51 -07003229 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3230 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003231 }
3232 else
3233 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003234 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003235
Florin Coras7baeb712019-01-04 17:05:43 -08003236 case VPPCOM_ATTR_SET_SHUT:
3237 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3238 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3239 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3240 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3241 break;
3242
3243 case VPPCOM_ATTR_GET_SHUT:
3244 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3245 tmp_flags = 1;
3246 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3247 tmp_flags |= 2;
3248 if (tmp_flags == 1)
3249 *(int *) buffer = SHUT_RD;
3250 else if (tmp_flags == 2)
3251 *(int *) buffer = SHUT_WR;
3252 else if (tmp_flags == 3)
3253 *(int *) buffer = SHUT_RDWR;
3254 *buflen = sizeof (int);
3255 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003256 default:
3257 rv = VPPCOM_EINVAL;
3258 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003259 }
3260
Dave Wallace35830af2017-10-09 01:43:42 -04003261 return rv;
3262}
3263
Stevenac1f96d2017-10-24 16:03:58 -07003264int
Florin Coras134a9962018-08-28 11:32:04 -07003265vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003266 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3267{
Florin Coras134a9962018-08-28 11:32:04 -07003268 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003269 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003270 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003271
3272 if (ep)
3273 {
Florin Coras134a9962018-08-28 11:32:04 -07003274 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003275 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003276 {
Florin Coras5e062572019-03-14 19:07:51 -07003277 VDBG (0, "sh 0x%llx is closed!", session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003278 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003279 }
Florin Coras7e12d942018-06-27 14:32:43 -07003280 ep->is_ip4 = session->transport.is_ip4;
3281 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003282 }
Steven58f464e2017-10-25 12:33:12 -07003283
3284 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003285 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003286 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003287 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003288 else
3289 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003290 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003291 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003292 }
3293
Florin Coras99368312018-08-02 10:45:44 -07003294 if (ep)
3295 {
3296 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003297 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3298 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003299 else
Dave Barach178cf492018-11-13 16:34:13 -05003300 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3301 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003302 }
Florin Coras460dce62018-07-27 05:45:06 -07003303
Stevenac1f96d2017-10-24 16:03:58 -07003304 return rv;
3305}
3306
3307int
Florin Coras134a9962018-08-28 11:32:04 -07003308vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003309 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3310{
Dave Wallace617dffa2017-10-26 14:47:06 -04003311 if (!buffer)
3312 return VPPCOM_EINVAL;
3313
3314 if (ep)
3315 {
3316 // TBD
3317 return VPPCOM_EINVAL;
3318 }
3319
3320 if (flags)
3321 {
3322 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003323 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003324 }
3325
Florin Coras42ceddb2018-12-12 10:56:01 -08003326 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003327}
3328
Dave Wallace048b1d62018-01-03 22:24:41 -05003329int
3330vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3331{
Florin Coras134a9962018-08-28 11:32:04 -07003332 vcl_worker_t *wrk = vcl_worker_get_current ();
3333 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003334 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003335 svm_msg_q_msg_t msg;
3336 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003337 int rv, num_ev = 0;
3338
Florin Coras5e062572019-03-14 19:07:51 -07003339 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003340
3341 if (!vp)
3342 return VPPCOM_EFAULT;
3343
3344 do
3345 {
Florin Coras7e12d942018-06-27 14:32:43 -07003346 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003347
Florin Coras6917b942018-11-13 22:44:54 -08003348 /* Dequeue all events and drop all unhandled io events */
3349 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3350 {
3351 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3352 vcl_handle_mq_event (wrk, e);
3353 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3354 }
3355 vec_reset_length (wrk->unhandled_evts_vector);
3356
Dave Wallace048b1d62018-01-03 22:24:41 -05003357 for (i = 0; i < n_sids; i++)
3358 {
Florin Coras7baeb712019-01-04 17:05:43 -08003359 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003360 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003361 {
3362 vp[i].revents = POLLHUP;
3363 num_ev++;
3364 continue;
3365 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003366
Florin Coras6917b942018-11-13 22:44:54 -08003367 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003368
3369 if (POLLIN & vp[i].events)
3370 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003371 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003372 if (rv > 0)
3373 {
Florin Coras6917b942018-11-13 22:44:54 -08003374 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003375 num_ev++;
3376 }
3377 else if (rv < 0)
3378 {
3379 switch (rv)
3380 {
3381 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003382 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003383 break;
3384
3385 default:
Florin Coras6917b942018-11-13 22:44:54 -08003386 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003387 break;
3388 }
3389 num_ev++;
3390 }
3391 }
3392
3393 if (POLLOUT & vp[i].events)
3394 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003395 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003396 if (rv > 0)
3397 {
Florin Coras6917b942018-11-13 22:44:54 -08003398 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003399 num_ev++;
3400 }
3401 else if (rv < 0)
3402 {
3403 switch (rv)
3404 {
3405 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003406 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003407 break;
3408
3409 default:
Florin Coras6917b942018-11-13 22:44:54 -08003410 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 break;
3412 }
3413 num_ev++;
3414 }
3415 }
3416
Dave Wallace7e607a72018-06-18 18:41:32 -04003417 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003418 {
Florin Coras6917b942018-11-13 22:44:54 -08003419 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003420 num_ev++;
3421 }
3422 }
3423 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003424 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003425 }
3426 while ((num_ev == 0) && keep_trying);
3427
Dave Wallace048b1d62018-01-03 22:24:41 -05003428 return num_ev;
3429}
3430
Florin Coras99368312018-08-02 10:45:44 -07003431int
3432vppcom_mq_epoll_fd (void)
3433{
Florin Coras134a9962018-08-28 11:32:04 -07003434 vcl_worker_t *wrk = vcl_worker_get_current ();
3435 return wrk->mqs_epfd;
3436}
3437
3438int
Florin Coras30e79c22019-01-02 19:31:22 -08003439vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003440{
3441 return session_handle & 0xFFFFFF;
3442}
3443
3444int
Florin Coras30e79c22019-01-02 19:31:22 -08003445vppcom_session_worker (vcl_session_handle_t session_handle)
3446{
3447 return session_handle >> 24;
3448}
3449
3450int
Florin Coras134a9962018-08-28 11:32:04 -07003451vppcom_worker_register (void)
3452{
Florin Coras47c40e22018-11-26 17:01:36 -08003453 if (!vcl_worker_alloc_and_init ())
3454 return VPPCOM_EEXIST;
3455
3456 if (vcl_worker_set_bapi ())
3457 return VPPCOM_EEXIST;
3458
3459 if (vcl_worker_register_with_vpp ())
3460 return VPPCOM_EEXIST;
3461
3462 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003463}
3464
Florin Coras369db832019-07-08 12:34:45 -07003465void
3466vppcom_worker_unregister (void)
3467{
3468 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3469 vcl_set_worker_index (~0);
3470}
3471
Florin Corasdfe4cf42018-11-28 22:13:45 -08003472int
3473vppcom_worker_index (void)
3474{
3475 return vcl_get_worker_index ();
3476}
3477
Florin Corase0982e52019-01-25 13:19:56 -08003478int
3479vppcom_worker_mqs_epfd (void)
3480{
3481 vcl_worker_t *wrk = vcl_worker_get_current ();
3482 if (!vcm->cfg.use_mq_eventfd)
3483 return -1;
3484 return wrk->mqs_epfd;
3485}
3486
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003487int
3488vppcom_session_is_connectable_listener (uint32_t session_handle)
3489{
3490 vcl_session_t *session;
3491 vcl_worker_t *wrk = vcl_worker_get_current ();
3492 session = vcl_session_get_w_handle (wrk, session_handle);
3493 if (!session)
3494 return VPPCOM_EBADFD;
3495 return vcl_session_is_connectable_listener (wrk, session);
3496}
3497
3498int
3499vppcom_session_listener (uint32_t session_handle)
3500{
3501 vcl_worker_t *wrk = vcl_worker_get_current ();
3502 vcl_session_t *listen_session, *session;
3503 session = vcl_session_get_w_handle (wrk, session_handle);
3504 if (!session)
3505 return VPPCOM_EBADFD;
3506 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3507 return VPPCOM_EBADFD;
3508 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3509 if (!listen_session)
3510 return VPPCOM_EBADFD;
3511 return vcl_session_handle (listen_session);
3512}
3513
3514int
3515vppcom_session_n_accepted (uint32_t session_handle)
3516{
3517 vcl_worker_t *wrk = vcl_worker_get_current ();
3518 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3519 if (!session)
3520 return VPPCOM_EBADFD;
3521 return session->n_accepted_sessions;
3522}
3523
Dave Wallacee22aa742017-10-20 12:30:38 -04003524/*
3525 * fd.io coding-style-patch-verification: ON
3526 *
3527 * Local Variables:
3528 * eval: (c-set-style "gnu")
3529 * End:
3530 */