blob: 0060922fea681ccc4f6b1a62c43c2e8399964d60 [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)
Florin Coras470b2a62019-08-03 18:53:48 -0700925 vppcom_disconnect_from_vpp ();
Florin Coras940f78f2018-11-30 12:11:20 -0800926 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 Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001494 session->parent_handle = 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;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001559 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001560
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);
Florin Coras028eaf02019-07-19 12:15:52 -07001573 if (parent_session)
1574 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001575
1576 session = vcl_session_get (wrk, session_index);
1577 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1578 session->vpp_handle, rv ? "failed" : "succeeded");
1579
1580 return rv;
1581}
1582
Florin Coras54693d22018-07-17 10:46:29 -07001583static u8
1584vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1585{
Florin Corasc0737e92019-03-04 14:19:39 -08001586 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001587}
1588
Steven58f464e2017-10-25 12:33:12 -07001589static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001590vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001591 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001592{
Florin Coras134a9962018-08-28 11:32:04 -07001593 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001594 int n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001595 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001596 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001597 svm_msg_q_msg_t msg;
1598 session_event_t *e;
1599 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001600 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001601
Florin Coras070453d2018-08-24 17:04:27 -07001602 if (PREDICT_FALSE (!buf))
1603 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001604
Florin Coras134a9962018-08-28 11:32:04 -07001605 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001606 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001607 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001608
Florin Coras6d0106e2019-01-29 20:11:58 -08001609 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001610 {
Florin Coras5e062572019-03-14 19:07:51 -07001611 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001612 s->session_index, s->vpp_handle, s->session_state,
1613 vppcom_session_state_str (s->session_state));
1614 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001615 }
1616
Florin Coras2cba8532018-09-11 16:33:36 -07001617 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001618 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001619 mq = wrk->app_event_queue;
1620 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001621 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001622
Sirshak Das28aa5392019-02-05 01:33:33 -06001623 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001624 {
Florin Coras54693d22018-07-17 10:46:29 -07001625 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001626 {
1627 svm_fifo_unset_event (s->rx_fifo);
1628 return VPPCOM_EWOULDBLOCK;
1629 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001630 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001631 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001632 if (vcl_session_is_closing (s))
1633 return vcl_session_closing_error (s);
1634
Florin Corasc0737e92019-03-04 14:19:39 -08001635 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001636 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001637 if (svm_msg_q_is_empty (mq))
1638 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001639
Florin Coras54693d22018-07-17 10:46:29 -07001640 svm_msg_q_sub_w_lock (mq, &msg);
1641 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001642 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001643 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001644 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001645 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001646 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001647 }
Florin Coras54693d22018-07-17 10:46:29 -07001648
Florin Coras460dce62018-07-27 05:45:06 -07001649 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001650 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001651 else
Florin Coras99368312018-08-02 10:45:44 -07001652 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001653
Sirshak Das28aa5392019-02-05 01:33:33 -06001654 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001655 svm_fifo_unset_event (s->rx_fifo);
Florin Coras41c9e042018-09-11 00:10:41 -07001656
Florin Coras317b8e02019-04-17 09:57:46 -07001657 /* Cut-through sessions might request tx notifications on rx fifos */
Florin Coras2d379d82019-06-28 12:45:12 -07001658 if (PREDICT_FALSE (rx_fifo->want_deq_ntf))
Florin Coras317b8e02019-04-17 09:57:46 -07001659 {
1660 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1661 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras2d379d82019-06-28 12:45:12 -07001662 svm_fifo_reset_has_deq_ntf (s->rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001663 }
1664
Florin Coras5e062572019-03-14 19:07:51 -07001665 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1666 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001667
Florin Coras54693d22018-07-17 10:46:29 -07001668 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001669}
1670
Steven58f464e2017-10-25 12:33:12 -07001671int
Florin Coras134a9962018-08-28 11:32:04 -07001672vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001673{
Florin Coras134a9962018-08-28 11:32:04 -07001674 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001675}
1676
1677static int
Florin Coras134a9962018-08-28 11:32:04 -07001678vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001679{
Florin Coras134a9962018-08-28 11:32:04 -07001680 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001681}
1682
Florin Coras2cba8532018-09-11 16:33:36 -07001683int
1684vppcom_session_read_segments (uint32_t session_handle,
1685 vppcom_data_segments_t ds)
1686{
1687 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001688 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001689 vcl_session_t *s = 0;
1690 svm_fifo_t *rx_fifo;
1691 svm_msg_q_msg_t msg;
1692 session_event_t *e;
1693 svm_msg_q_t *mq;
1694 u8 is_ct;
1695
1696 s = vcl_session_get_w_handle (wrk, session_handle);
1697 if (PREDICT_FALSE (!s || s->is_vep))
1698 return VPPCOM_EBADFD;
1699
Florin Coras6d0106e2019-01-29 20:11:58 -08001700 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1701 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001702
1703 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1704 is_ct = vcl_session_is_ct (s);
1705 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1706 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001707 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001708
Florin Coras653e43f2019-03-04 10:56:23 -08001709 if (is_ct)
1710 svm_fifo_unset_event (s->rx_fifo);
1711
Sirshak Das28aa5392019-02-05 01:33:33 -06001712 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001713 {
1714 if (is_nonblocking)
1715 {
1716 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001717 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001718 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001719 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001720 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001721 if (vcl_session_is_closing (s))
1722 return vcl_session_closing_error (s);
1723
Florin Coras2cba8532018-09-11 16:33:36 -07001724 svm_fifo_unset_event (rx_fifo);
1725 svm_msg_q_lock (mq);
1726 if (svm_msg_q_is_empty (mq))
1727 svm_msg_q_wait (mq);
1728
1729 svm_msg_q_sub_w_lock (mq, &msg);
1730 e = svm_msg_q_msg_data (mq, &msg);
1731 svm_msg_q_unlock (mq);
1732 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001733 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001734 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001735 }
1736 }
1737
Florin Coras88001c62019-04-24 14:44:46 -07001738 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001739 svm_fifo_unset_event (rx_fifo);
1740
Florin Coras2cba8532018-09-11 16:33:36 -07001741 return n_read;
1742}
1743
1744void
1745vppcom_session_free_segments (uint32_t session_handle,
1746 vppcom_data_segments_t ds)
1747{
1748 vcl_worker_t *wrk = vcl_worker_get_current ();
1749 vcl_session_t *s;
1750
1751 s = vcl_session_get_w_handle (wrk, session_handle);
1752 if (PREDICT_FALSE (!s || s->is_vep))
1753 return;
1754
Florin Coras88001c62019-04-24 14:44:46 -07001755 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001756}
1757
Florin Coras2cba8532018-09-11 16:33:36 -07001758int
1759vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1760{
1761 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001762 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001763 if (first_copy < max_bytes)
1764 {
Dave Barach178cf492018-11-13 16:34:13 -05001765 clib_memcpy_fast (buf + first_copy, ds[1].data,
1766 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001767 }
1768 return 0;
1769}
1770
Florin Coras54693d22018-07-17 10:46:29 -07001771static u8
1772vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1773{
Florin Corasc0737e92019-03-04 14:19:39 -08001774 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001775}
1776
Florin Coras42ceddb2018-12-12 10:56:01 -08001777static inline int
1778vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1779 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001780{
Florin Coras134a9962018-08-28 11:32:04 -07001781 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001782 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001783 vcl_session_t *s = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001784 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001785 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08001786 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001787 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001788 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001789 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001790
Florin Coras070453d2018-08-24 17:04:27 -07001791 if (PREDICT_FALSE (!buf))
1792 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001793
Florin Coras134a9962018-08-28 11:32:04 -07001794 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001795 if (PREDICT_FALSE (!s))
1796 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001797
Florin Coras460dce62018-07-27 05:45:06 -07001798 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001799 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001800 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1801 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001802 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001803 }
1804
Florin Coras6d0106e2019-01-29 20:11:58 -08001805 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001806 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001807 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1808 s->session_index, s->vpp_handle, s->session_state,
1809 vppcom_session_state_str (s->session_state));
1810 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001811 }
1812
Florin Coras0e88e852018-09-17 22:09:02 -07001813 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001814 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07001815 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06001816
Florin Coras653e43f2019-03-04 10:56:23 -08001817 mq = wrk->app_event_queue;
Sirshak Das28aa5392019-02-05 01:33:33 -06001818 if (svm_fifo_is_full_prod (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001819 {
Florin Coras54693d22018-07-17 10:46:29 -07001820 if (is_nonblocking)
1821 {
Florin Coras070453d2018-08-24 17:04:27 -07001822 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001823 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001824 while (svm_fifo_is_full_prod (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001825 {
Florin Coras2d379d82019-06-28 12:45:12 -07001826 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001827 if (vcl_session_is_closing (s))
1828 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001829 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001830 if (svm_msg_q_is_empty (mq))
1831 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001832
Florin Coras54693d22018-07-17 10:46:29 -07001833 svm_msg_q_sub_w_lock (mq, &msg);
1834 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001835 svm_msg_q_unlock (mq);
1836
Florin Coras0e88e852018-09-17 22:09:02 -07001837 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001838 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001839 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001840 }
Dave Wallace543852a2017-08-03 02:11:34 -04001841 }
Dave Wallace543852a2017-08-03 02:11:34 -04001842
Florin Coras653e43f2019-03-04 10:56:23 -08001843 et = SESSION_IO_EVT_TX;
1844 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08001845 et = SESSION_IO_EVT_TX_FLUSH;
1846
Florin Coras460dce62018-07-27 05:45:06 -07001847 if (s->is_dgram)
1848 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08001849 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001850 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07001851 else
1852 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001853 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08001854
Florin Coras14ed6df2019-03-06 21:13:42 -08001855 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001856 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1857 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001858
Florin Coras460dce62018-07-27 05:45:06 -07001859 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001860
Florin Coras6d0106e2019-01-29 20:11:58 -08001861 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1862 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001863
Florin Coras54693d22018-07-17 10:46:29 -07001864 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001865}
1866
Florin Coras42ceddb2018-12-12 10:56:01 -08001867int
1868vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1869{
1870 return vppcom_session_write_inline (session_handle, buf, n,
1871 0 /* is_flush */ );
1872}
1873
Florin Corasb0f662f2018-12-27 14:51:46 -08001874int
1875vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1876{
1877 return vppcom_session_write_inline (session_handle, buf, n,
1878 1 /* is_flush */ );
1879}
1880
Florin Corasc0737e92019-03-04 14:19:39 -08001881#define vcl_fifo_rx_evt_valid_or_break(_s) \
1882if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1883 { \
1884 if (!vcl_session_is_ct (_s)) \
1885 { \
1886 svm_fifo_unset_event (_s->rx_fifo); \
1887 if (svm_fifo_is_empty (_s->rx_fifo)) \
1888 break; \
1889 } \
1890 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1891 { \
1892 svm_fifo_unset_event (_s->ct_rx_fifo); \
1893 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1894 break; \
1895 } \
1896 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07001897
Florin Coras86f04502018-09-12 16:08:01 -07001898static void
1899vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1900 unsigned long n_bits, unsigned long *read_map,
1901 unsigned long *write_map,
1902 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001903{
1904 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001905 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001906 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001907 u32 sid;
1908
1909 switch (e->event_type)
1910 {
Florin Corasc0737e92019-03-04 14:19:39 -08001911 case SESSION_IO_EVT_RX:
1912 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001913 session = vcl_session_get (wrk, sid);
1914 if (!session)
1915 break;
Florin Corasfff68f72019-03-13 16:01:38 -07001916 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07001917 if (sid < n_bits && read_map)
1918 {
David Johnsond9818dd2018-12-14 14:53:41 -05001919 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001920 *bits_set += 1;
1921 }
1922 break;
Florin Corasfe97da32019-03-06 10:09:04 -08001923 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08001924 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001925 session = vcl_session_get (wrk, sid);
1926 if (!session)
1927 break;
1928 if (sid < n_bits && write_map)
1929 {
David Johnsond9818dd2018-12-14 14:53:41 -05001930 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001931 *bits_set += 1;
1932 }
1933 break;
Florin Coras86f04502018-09-12 16:08:01 -07001934 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001935 session = vcl_session_accepted (wrk,
1936 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001937 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001938 break;
Florin Coras86f04502018-09-12 16:08:01 -07001939 sid = session->session_index;
1940 if (sid < n_bits && read_map)
1941 {
David Johnsond9818dd2018-12-14 14:53:41 -05001942 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001943 *bits_set += 1;
1944 }
1945 break;
1946 case SESSION_CTRL_EVT_CONNECTED:
1947 connected_msg = (session_connected_msg_t *) e->data;
1948 vcl_session_connected_handler (wrk, connected_msg);
1949 break;
1950 case SESSION_CTRL_EVT_DISCONNECTED:
1951 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001952 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1953 if (!session)
1954 break;
1955 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001956 if (sid < n_bits && except_map)
1957 {
David Johnsond9818dd2018-12-14 14:53:41 -05001958 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001959 *bits_set += 1;
1960 }
1961 break;
1962 case SESSION_CTRL_EVT_RESET:
1963 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1964 if (sid < n_bits && except_map)
1965 {
David Johnsond9818dd2018-12-14 14:53:41 -05001966 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001967 *bits_set += 1;
1968 }
1969 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001970 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1971 vcl_session_unlisten_reply_handler (wrk, e->data);
1972 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001973 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1974 vcl_session_worker_update_reply_handler (wrk, e->data);
1975 break;
1976 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1977 vcl_session_req_worker_update_handler (wrk, e->data);
1978 break;
Florin Coras86f04502018-09-12 16:08:01 -07001979 default:
1980 clib_warning ("unhandled: %u", e->event_type);
1981 break;
1982 }
1983}
1984
1985static int
1986vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1987 unsigned long n_bits, unsigned long *read_map,
1988 unsigned long *write_map, unsigned long *except_map,
1989 double time_to_wait, u32 * bits_set)
1990{
Florin Coras99368312018-08-02 10:45:44 -07001991 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001992 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001993 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001994
1995 svm_msg_q_lock (mq);
1996 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001997 {
Florin Coras54693d22018-07-17 10:46:29 -07001998 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001999 {
Florin Coras54693d22018-07-17 10:46:29 -07002000 svm_msg_q_unlock (mq);
2001 return 0;
2002 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002003
Florin Coras54693d22018-07-17 10:46:29 -07002004 if (!time_to_wait)
2005 {
2006 svm_msg_q_unlock (mq);
2007 return 0;
2008 }
2009 else if (time_to_wait < 0)
2010 {
2011 svm_msg_q_wait (mq);
2012 }
2013 else
2014 {
2015 if (svm_msg_q_timedwait (mq, time_to_wait))
2016 {
2017 svm_msg_q_unlock (mq);
2018 return 0;
2019 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002020 }
2021 }
Florin Corase003a1b2019-06-05 10:47:16 -07002022 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002023 svm_msg_q_unlock (mq);
2024
Florin Coras134a9962018-08-28 11:32:04 -07002025 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002026 {
Florin Coras134a9962018-08-28 11:32:04 -07002027 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002028 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002029 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2030 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002031 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002032 }
Florin Coras134a9962018-08-28 11:32:04 -07002033 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002034 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002035 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002036}
2037
Florin Coras99368312018-08-02 10:45:44 -07002038static int
Florin Coras294afe22019-01-07 17:49:17 -08002039vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2040 vcl_si_set * read_map, vcl_si_set * write_map,
2041 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002042 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002043{
Florin Coras14ed6df2019-03-06 21:13:42 -08002044 double wait = 0, start = 0;
2045
2046 if (!*bits_set)
2047 {
2048 wait = time_to_wait;
2049 start = clib_time_now (&wrk->clib_time);
2050 }
2051
2052 do
2053 {
2054 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2055 write_map, except_map, wait, bits_set);
2056 if (*bits_set)
2057 return *bits_set;
2058 if (wait == -1)
2059 continue;
2060
2061 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2062 }
2063 while (wait > 0);
2064
2065 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002066}
2067
2068static int
Florin Coras294afe22019-01-07 17:49:17 -08002069vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2070 vcl_si_set * read_map, vcl_si_set * write_map,
2071 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002072 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002073{
2074 vcl_mq_evt_conn_t *mqc;
2075 int __clib_unused n_read;
2076 int n_mq_evts, i;
2077 u64 buf;
2078
Florin Coras134a9962018-08-28 11:32:04 -07002079 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2080 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2081 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002082 for (i = 0; i < n_mq_evts; i++)
2083 {
Florin Coras134a9962018-08-28 11:32:04 -07002084 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002085 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002086 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002087 except_map, 0, bits_set);
2088 }
2089
2090 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2091}
2092
Dave Wallace543852a2017-08-03 02:11:34 -04002093int
Florin Coras294afe22019-01-07 17:49:17 -08002094vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2095 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002096{
Florin Coras54693d22018-07-17 10:46:29 -07002097 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002098 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002099 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002100 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04002101
Dave Wallace7876d392017-10-19 03:53:57 -04002102 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002103 {
Florin Coras134a9962018-08-28 11:32:04 -07002104 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002105 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002106 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2107 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002108 }
Dave Wallace7876d392017-10-19 03:53:57 -04002109 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002110 {
Florin Coras134a9962018-08-28 11:32:04 -07002111 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002112 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002113 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2114 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002115 }
Dave Wallace7876d392017-10-19 03:53:57 -04002116 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002117 {
Florin Coras134a9962018-08-28 11:32:04 -07002118 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002119 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002120 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2121 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002122 }
2123
Florin Coras54693d22018-07-17 10:46:29 -07002124 if (!n_bits)
2125 return 0;
2126
2127 if (!write_map)
2128 goto check_rd;
2129
2130 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002131 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2132 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002133 {
Florin Coras47c40e22018-11-26 17:01:36 -08002134 if (except_map && sid < minbits)
2135 clib_bitmap_set_no_check (except_map, sid, 1);
2136 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002137 }
2138
Sirshak Das28aa5392019-02-05 01:33:33 -06002139 rv = svm_fifo_is_full_prod (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002140 if (!rv)
2141 {
David Johnsond9818dd2018-12-14 14:53:41 -05002142 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002143 bits_set++;
2144 }
Florin Coras294afe22019-01-07 17:49:17 -08002145 else
Florin Coras2d379d82019-06-28 12:45:12 -07002146 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002147 }));
2148
2149check_rd:
2150 if (!read_map)
2151 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002152
Florin Coras134a9962018-08-28 11:32:04 -07002153 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2154 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002155 {
Florin Coras47c40e22018-11-26 17:01:36 -08002156 if (except_map && sid < minbits)
2157 clib_bitmap_set_no_check (except_map, sid, 1);
2158 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002159 }
2160
Florin Coras0ef8ef22019-01-18 08:37:13 -08002161 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002162 if (rv)
2163 {
David Johnsond9818dd2018-12-14 14:53:41 -05002164 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002165 bits_set++;
2166 }
2167 }));
2168 /* *INDENT-ON* */
2169
2170check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002171
Florin Coras86f04502018-09-12 16:08:01 -07002172 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2173 {
2174 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2175 read_map, write_map, except_map, &bits_set);
2176 }
2177 vec_reset_length (wrk->unhandled_evts_vector);
2178
Florin Coras99368312018-08-02 10:45:44 -07002179 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002180 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002181 time_to_wait, &bits_set);
2182 else
Florin Coras134a9962018-08-28 11:32:04 -07002183 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002184 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002185
Dave Wallace543852a2017-08-03 02:11:34 -04002186 return (bits_set);
2187}
2188
Dave Wallacef7f809c2017-10-03 01:48:42 -04002189static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002190vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002191{
Florin Coras7e12d942018-06-27 14:32:43 -07002192 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002193 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002194 u32 sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002195
Florin Corasc0737e92019-03-04 14:19:39 -08002196 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002197 return;
2198
Florin Coras7e5e62b2019-07-30 14:08:23 -07002199 session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002200 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002201 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002202 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002203 goto done;
2204 }
2205 if (PREDICT_FALSE (!session->is_vep))
2206 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002207 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002208 goto done;
2209 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002210 vep = &session->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002211 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002212 "{\n"
2213 " is_vep = %u\n"
2214 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002215 " next_sh = 0x%x (%u)\n"
2216 "}\n", vep_handle, session->is_vep, session->is_vep_session,
Florin Coras5e062572019-03-14 19:07:51 -07002217 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002218
Florin Coras7e5e62b2019-07-30 14:08:23 -07002219 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002220 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002221 session = vcl_session_get_w_handle (wrk, sh);
Florin Coras070453d2018-08-24 17:04:27 -07002222 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002223 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002224 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002225 goto done;
2226 }
2227 if (PREDICT_FALSE (session->is_vep))
Florin Coras5e062572019-03-14 19:07:51 -07002228 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002229 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002230 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002231 else if (PREDICT_FALSE (!session->is_vep_session))
2232 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002233 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002234 goto done;
2235 }
2236 vep = &session->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002237 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2238 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
2239 sh, session->vep.vep_sh, vep_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002240 if (session->is_vep_session)
2241 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002242 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002243 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002244 " next_sh = 0x%x (%u)\n"
2245 " prev_sh = 0x%x (%u)\n"
2246 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002247 " ev.events = 0x%x\n"
2248 " ev.data.u64 = 0x%llx\n"
2249 " et_mask = 0x%x\n"
2250 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002251 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002252 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2253 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002254 }
2255 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002256
2257done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002258 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002259}
2260
2261int
2262vppcom_epoll_create (void)
2263{
Florin Coras134a9962018-08-28 11:32:04 -07002264 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002265 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002266
Florin Coras134a9962018-08-28 11:32:04 -07002267 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002268
2269 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002270 vep_session->vep.vep_sh = ~0;
2271 vep_session->vep.next_sh = ~0;
2272 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002273 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002274
Florin Corasa7a1a222018-12-30 17:11:31 -08002275 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2276 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002277
Florin Corasab2f6db2018-08-31 14:31:41 -07002278 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002279}
2280
2281int
Florin Coras134a9962018-08-28 11:32:04 -07002282vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002283 struct epoll_event *event)
2284{
Florin Coras134a9962018-08-28 11:32:04 -07002285 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002286 vcl_session_t *vep_session;
2287 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002288 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002289
Florin Coras134a9962018-08-28 11:32:04 -07002290 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002291 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002292 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002293 return VPPCOM_EINVAL;
2294 }
2295
Florin Coras134a9962018-08-28 11:32:04 -07002296 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002297 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002298 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002299 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002300 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002301 }
2302 if (PREDICT_FALSE (!vep_session->is_vep))
2303 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002304 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002305 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002306 }
2307
Florin Coras134a9962018-08-28 11:32:04 -07002308 ASSERT (vep_session->vep.vep_sh == ~0);
2309 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002310
Florin Coras134a9962018-08-28 11:32:04 -07002311 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002312 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002313 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002314 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002315 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002316 }
2317 if (PREDICT_FALSE (session->is_vep))
2318 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002319 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002320 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002321 }
2322
2323 switch (op)
2324 {
2325 case EPOLL_CTL_ADD:
2326 if (PREDICT_FALSE (!event))
2327 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002328 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002329 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002330 }
Florin Coras134a9962018-08-28 11:32:04 -07002331 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002332 {
Florin Coras7e12d942018-06-27 14:32:43 -07002333 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002334 next_session = vcl_session_get_w_handle (wrk,
2335 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002336 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002337 {
Florin Coras5e062572019-03-14 19:07:51 -07002338 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002339 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002340 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002341 }
Florin Coras134a9962018-08-28 11:32:04 -07002342 ASSERT (next_session->vep.prev_sh == vep_handle);
2343 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002344 }
Florin Coras134a9962018-08-28 11:32:04 -07002345 session->vep.next_sh = vep_session->vep.next_sh;
2346 session->vep.prev_sh = vep_handle;
2347 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002348 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2349 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002350 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002351 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002352 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002353
Florin Coras1bcad5c2019-01-09 20:04:38 -08002354 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002355 svm_fifo_add_want_deq_ntf (session->tx_fifo,
2356 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002357
Florin Corasa7a1a222018-12-30 17:11:31 -08002358 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2359 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002360 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002361 break;
2362
2363 case EPOLL_CTL_MOD:
2364 if (PREDICT_FALSE (!event))
2365 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002366 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002367 rv = VPPCOM_EINVAL;
2368 goto done;
2369 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002370 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002371 {
Florin Coras5e062572019-03-14 19:07:51 -07002372 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002373 rv = VPPCOM_EINVAL;
2374 goto done;
2375 }
Florin Coras134a9962018-08-28 11:32:04 -07002376 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002377 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002378 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2379 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002380 rv = VPPCOM_EINVAL;
2381 goto done;
2382 }
2383 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2384 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002385 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2386 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002387 break;
2388
2389 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002390 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002391 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002392 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002393 rv = VPPCOM_EINVAL;
2394 goto done;
2395 }
Florin Coras134a9962018-08-28 11:32:04 -07002396 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002397 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002398 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2399 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002400 rv = VPPCOM_EINVAL;
2401 goto done;
2402 }
2403
Florin Coras134a9962018-08-28 11:32:04 -07002404 if (session->vep.prev_sh == vep_handle)
2405 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002406 else
2407 {
Florin Coras7e12d942018-06-27 14:32:43 -07002408 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002409 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002410 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002411 {
Florin Coras5e062572019-03-14 19:07:51 -07002412 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002413 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002414 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002415 }
Florin Coras134a9962018-08-28 11:32:04 -07002416 ASSERT (prev_session->vep.next_sh == session_handle);
2417 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002418 }
Florin Coras134a9962018-08-28 11:32:04 -07002419 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002420 {
Florin Coras7e12d942018-06-27 14:32:43 -07002421 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002422 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002423 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002424 {
Florin Coras5e062572019-03-14 19:07:51 -07002425 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002426 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002427 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002428 }
Florin Coras134a9962018-08-28 11:32:04 -07002429 ASSERT (next_session->vep.prev_sh == session_handle);
2430 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002431 }
2432
2433 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002434 session->vep.next_sh = ~0;
2435 session->vep.prev_sh = ~0;
2436 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002437 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002438
2439 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002440 svm_fifo_del_want_deq_ntf (session->tx_fifo, SVM_FIFO_NO_DEQ_NOTIF);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002441
Florin Coras5e062572019-03-14 19:07:51 -07002442 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002443 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002444 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002445 break;
2446
2447 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002448 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002449 rv = VPPCOM_EINVAL;
2450 }
2451
Florin Coras134a9962018-08-28 11:32:04 -07002452 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002453
2454done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002455 return rv;
2456}
2457
Florin Coras86f04502018-09-12 16:08:01 -07002458static inline void
2459vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2460 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002461{
2462 session_disconnected_msg_t *disconnected_msg;
2463 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002464 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002465 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002466 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002467 u8 add_event = 0;
2468
2469 switch (e->event_type)
2470 {
Florin Coras653e43f2019-03-04 10:56:23 -08002471 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002472 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002473 if (!(session = vcl_session_get (wrk, sid)))
2474 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002475 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002476 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002477 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002478 break;
2479 add_event = 1;
2480 events[*num_ev].events |= EPOLLIN;
2481 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002482 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002483 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002484 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002485 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002486 if (!(session = vcl_session_get (wrk, sid)))
2487 break;
Florin Coras86f04502018-09-12 16:08:01 -07002488 session_events = session->vep.ev.events;
2489 if (!(EPOLLOUT & session_events))
2490 break;
2491 add_event = 1;
2492 events[*num_ev].events |= EPOLLOUT;
2493 session_evt_data = session->vep.ev.data.u64;
Florin Coras2d379d82019-06-28 12:45:12 -07002494 svm_fifo_reset_has_deq_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002495 break;
Florin Coras86f04502018-09-12 16:08:01 -07002496 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002497 session = vcl_session_accepted (wrk,
2498 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002499 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002500 break;
Florin Coras86f04502018-09-12 16:08:01 -07002501
Florin Coras86f04502018-09-12 16:08:01 -07002502 session_events = session->vep.ev.events;
2503 if (!(EPOLLIN & session_events))
2504 break;
2505
2506 add_event = 1;
2507 events[*num_ev].events |= EPOLLIN;
2508 session_evt_data = session->vep.ev.data.u64;
2509 break;
2510 case SESSION_CTRL_EVT_CONNECTED:
2511 connected_msg = (session_connected_msg_t *) e->data;
2512 vcl_session_connected_handler (wrk, connected_msg);
2513 /* Generate EPOLLOUT because there's no connected event */
2514 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002515 if (!(session = vcl_session_get (wrk, sid)))
2516 break;
Florin Coras86f04502018-09-12 16:08:01 -07002517 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002518 if (!(EPOLLOUT & session_events))
2519 break;
2520 add_event = 1;
2521 events[*num_ev].events |= EPOLLOUT;
2522 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002523 break;
2524 case SESSION_CTRL_EVT_DISCONNECTED:
2525 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002526 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2527 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002528 break;
Florin Coras72f77822019-01-22 19:05:52 -08002529 session_events = session->vep.ev.events;
2530 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2531 break;
Florin Coras86f04502018-09-12 16:08:01 -07002532 add_event = 1;
2533 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2534 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002535 break;
2536 case SESSION_CTRL_EVT_RESET:
2537 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2538 if (!(session = vcl_session_get (wrk, sid)))
2539 break;
Florin Coras72f77822019-01-22 19:05:52 -08002540 session_events = session->vep.ev.events;
2541 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2542 break;
Florin Coras86f04502018-09-12 16:08:01 -07002543 add_event = 1;
2544 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2545 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002546 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002547 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2548 vcl_session_unlisten_reply_handler (wrk, e->data);
2549 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002550 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2551 vcl_session_req_worker_update_handler (wrk, e->data);
2552 break;
2553 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2554 vcl_session_worker_update_reply_handler (wrk, e->data);
2555 break;
Florin Coras86f04502018-09-12 16:08:01 -07002556 default:
2557 VDBG (0, "unhandled: %u", e->event_type);
2558 break;
2559 }
2560
2561 if (add_event)
2562 {
2563 events[*num_ev].data.u64 = session_evt_data;
2564 if (EPOLLONESHOT & session_events)
2565 {
2566 session = vcl_session_get (wrk, sid);
2567 session->vep.ev.events = 0;
2568 }
2569 *num_ev += 1;
2570 }
2571}
2572
2573static int
2574vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2575 struct epoll_event *events, u32 maxevents,
2576 double wait_for_time, u32 * num_ev)
2577{
Florin Coras99368312018-08-02 10:45:44 -07002578 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002579 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002580 int i;
2581
Florin Coras539663c2018-09-28 14:59:37 -07002582 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2583 goto handle_dequeued;
2584
Florin Coras54693d22018-07-17 10:46:29 -07002585 svm_msg_q_lock (mq);
2586 if (svm_msg_q_is_empty (mq))
2587 {
2588 if (!wait_for_time)
2589 {
2590 svm_msg_q_unlock (mq);
2591 return 0;
2592 }
2593 else if (wait_for_time < 0)
2594 {
2595 svm_msg_q_wait (mq);
2596 }
2597 else
2598 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002599 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002600 {
2601 svm_msg_q_unlock (mq);
2602 return 0;
2603 }
2604 }
2605 }
Florin Corase003a1b2019-06-05 10:47:16 -07002606 ASSERT (maxevents > *num_ev);
2607 vcl_mq_dequeue_batch (wrk, mq, maxevents - *num_ev);
Florin Coras54693d22018-07-17 10:46:29 -07002608 svm_msg_q_unlock (mq);
2609
Florin Coras539663c2018-09-28 14:59:37 -07002610handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002611 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002612 {
Florin Coras134a9962018-08-28 11:32:04 -07002613 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002614 e = svm_msg_q_msg_data (mq, msg);
Florin Corase003a1b2019-06-05 10:47:16 -07002615 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
Florin Coras99368312018-08-02 10:45:44 -07002616 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002617 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002618 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002619 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002620 return *num_ev;
2621}
2622
Florin Coras99368312018-08-02 10:45:44 -07002623static int
Florin Coras134a9962018-08-28 11:32:04 -07002624vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002625 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002626{
Florin Corase003a1b2019-06-05 10:47:16 -07002627 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002628
2629 if (!n_evts)
2630 {
2631 wait = wait_for_time;
2632 start = clib_time_now (&wrk->clib_time);
2633 }
2634
2635 do
2636 {
2637 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2638 wait, &n_evts);
2639 if (n_evts)
2640 return n_evts;
2641 if (wait == -1)
2642 continue;
2643
Florin Corase003a1b2019-06-05 10:47:16 -07002644 now = clib_time_now (&wrk->clib_time);
2645 wait -= now - start;
2646 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002647 }
2648 while (wait > 0);
2649
2650 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002651}
2652
2653static int
Florin Coras134a9962018-08-28 11:32:04 -07002654vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002655 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002656{
2657 vcl_mq_evt_conn_t *mqc;
2658 int __clib_unused n_read;
2659 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002660 u64 buf;
2661
Florin Coras134a9962018-08-28 11:32:04 -07002662 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002663again:
Florin Coras134a9962018-08-28 11:32:04 -07002664 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2665 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002666 for (i = 0; i < n_mq_evts; i++)
2667 {
Florin Coras134a9962018-08-28 11:32:04 -07002668 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002669 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002670 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002671 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002672 if (!n_evts && n_mq_evts > 0)
2673 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002674
2675 return (int) n_evts;
2676}
2677
Dave Wallacef7f809c2017-10-03 01:48:42 -04002678int
Florin Coras134a9962018-08-28 11:32:04 -07002679vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002680 int maxevents, double wait_for_time)
2681{
Florin Coras134a9962018-08-28 11:32:04 -07002682 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002683 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002684 u32 n_evts = 0;
2685 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002686
2687 if (PREDICT_FALSE (maxevents <= 0))
2688 {
Florin Coras5e062572019-03-14 19:07:51 -07002689 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002690 return VPPCOM_EINVAL;
2691 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002692
Florin Coras134a9962018-08-28 11:32:04 -07002693 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002694 if (!vep_session)
2695 return VPPCOM_EBADFD;
2696
Florin Coras54693d22018-07-17 10:46:29 -07002697 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002698 {
Florin Coras5e062572019-03-14 19:07:51 -07002699 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002700 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002701 }
Florin Coras54693d22018-07-17 10:46:29 -07002702
2703 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002704
Florin Coras86f04502018-09-12 16:08:01 -07002705 if (vec_len (wrk->unhandled_evts_vector))
2706 {
2707 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2708 {
2709 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2710 events, &n_evts);
2711 if (n_evts == maxevents)
2712 {
Florin Corase003a1b2019-06-05 10:47:16 -07002713 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
2714 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07002715 }
2716 }
Florin Corase003a1b2019-06-05 10:47:16 -07002717 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07002718 }
2719
2720 if (vcm->cfg.use_mq_eventfd)
2721 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2722 wait_for_time);
2723
2724 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2725 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002726}
2727
Dave Wallace35830af2017-10-09 01:43:42 -04002728int
Florin Coras134a9962018-08-28 11:32:04 -07002729vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002730 void *buffer, uint32_t * buflen)
2731{
Florin Coras134a9962018-08-28 11:32:04 -07002732 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002733 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002734 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002735 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002736 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002737
Florin Coras134a9962018-08-28 11:32:04 -07002738 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002739 if (!session)
2740 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002741
Dave Wallace35830af2017-10-09 01:43:42 -04002742 switch (op)
2743 {
2744 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002745 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002746 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2747 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002748 break;
2749
Dave Wallace227867f2017-11-13 21:21:53 -05002750 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002751 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002752 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2753 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002754 break;
2755
2756 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002757 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002758 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002759 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2760 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002761 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07002762 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2763 "is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002764 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002765 }
2766 else
2767 rv = VPPCOM_EINVAL;
2768 break;
2769
2770 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002771 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002772 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002773 if (*flags & O_NONBLOCK)
2774 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2775 else
2776 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2777
Florin Coras5e062572019-03-14 19:07:51 -07002778 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2779 " is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002780 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002781 }
2782 else
2783 rv = VPPCOM_EINVAL;
2784 break;
2785
2786 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002787 if (PREDICT_TRUE (buffer && buflen &&
2788 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002789 {
Florin Coras7e12d942018-06-27 14:32:43 -07002790 ep->is_ip4 = session->transport.is_ip4;
2791 ep->port = session->transport.rmt_port;
2792 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002793 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2794 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002795 else
Dave Barach178cf492018-11-13 16:34:13 -05002796 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2797 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002798 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002799 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2800 "addr = %U, port %u", session_handle, ep->is_ip4,
2801 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002802 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2803 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002804 }
2805 else
2806 rv = VPPCOM_EINVAL;
2807 break;
2808
2809 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002810 if (PREDICT_TRUE (buffer && buflen &&
2811 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002812 {
Florin Coras7e12d942018-06-27 14:32:43 -07002813 ep->is_ip4 = session->transport.is_ip4;
2814 ep->port = session->transport.lcl_port;
2815 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002816 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2817 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002818 else
Dave Barach178cf492018-11-13 16:34:13 -05002819 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2820 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002821 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002822 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2823 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002824 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002825 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2826 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002827 }
2828 else
2829 rv = VPPCOM_EINVAL;
2830 break;
Stevenb5a11602017-10-11 09:59:30 -07002831
Dave Wallace048b1d62018-01-03 22:24:41 -05002832 case VPPCOM_ATTR_GET_LIBC_EPFD:
2833 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07002834 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002835 break;
2836
2837 case VPPCOM_ATTR_SET_LIBC_EPFD:
2838 if (PREDICT_TRUE (buffer && buflen &&
2839 (*buflen == sizeof (session->libc_epfd))))
2840 {
2841 session->libc_epfd = *(int *) buffer;
2842 *buflen = sizeof (session->libc_epfd);
2843
Florin Coras5e062572019-03-14 19:07:51 -07002844 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2845 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002846 }
2847 else
2848 rv = VPPCOM_EINVAL;
2849 break;
2850
2851 case VPPCOM_ATTR_GET_PROTOCOL:
2852 if (buffer && buflen && (*buflen >= sizeof (int)))
2853 {
Florin Coras7e12d942018-06-27 14:32:43 -07002854 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002855 *buflen = sizeof (int);
2856
Florin Coras5e062572019-03-14 19:07:51 -07002857 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2858 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002859 }
2860 else
2861 rv = VPPCOM_EINVAL;
2862 break;
2863
2864 case VPPCOM_ATTR_GET_LISTEN:
2865 if (buffer && buflen && (*buflen >= sizeof (int)))
2866 {
2867 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2868 VCL_SESS_ATTR_LISTEN);
2869 *buflen = sizeof (int);
2870
Florin Coras5e062572019-03-14 19:07:51 -07002871 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
2872 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002873 }
2874 else
2875 rv = VPPCOM_EINVAL;
2876 break;
2877
2878 case VPPCOM_ATTR_GET_ERROR:
2879 if (buffer && buflen && (*buflen >= sizeof (int)))
2880 {
2881 *(int *) buffer = 0;
2882 *buflen = sizeof (int);
2883
Florin Coras5e062572019-03-14 19:07:51 -07002884 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2885 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002886 }
2887 else
2888 rv = VPPCOM_EINVAL;
2889 break;
2890
2891 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2892 if (buffer && buflen && (*buflen >= sizeof (u32)))
2893 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002894
2895 /* VPP-TBD */
2896 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002897 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002898 vcm->cfg.tx_fifo_size);
2899 *buflen = sizeof (u32);
2900
Florin Coras5e062572019-03-14 19:07:51 -07002901 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2902 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
2903 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002904 }
2905 else
2906 rv = VPPCOM_EINVAL;
2907 break;
2908
2909 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2910 if (buffer && buflen && (*buflen == sizeof (u32)))
2911 {
2912 /* VPP-TBD */
2913 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002914 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2915 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2916 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002917 }
2918 else
2919 rv = VPPCOM_EINVAL;
2920 break;
2921
2922 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2923 if (buffer && buflen && (*buflen >= sizeof (u32)))
2924 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002925
2926 /* VPP-TBD */
2927 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002928 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002929 vcm->cfg.rx_fifo_size);
2930 *buflen = sizeof (u32);
2931
Florin Coras5e062572019-03-14 19:07:51 -07002932 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
2933 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002934 }
2935 else
2936 rv = VPPCOM_EINVAL;
2937 break;
2938
2939 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2940 if (buffer && buflen && (*buflen == sizeof (u32)))
2941 {
2942 /* VPP-TBD */
2943 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002944 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
2945 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2946 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002947 }
2948 else
2949 rv = VPPCOM_EINVAL;
2950 break;
2951
2952 case VPPCOM_ATTR_GET_REUSEADDR:
2953 if (buffer && buflen && (*buflen >= sizeof (int)))
2954 {
2955 /* VPP-TBD */
2956 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2957 VCL_SESS_ATTR_REUSEADDR);
2958 *buflen = sizeof (int);
2959
Florin Coras5e062572019-03-14 19:07:51 -07002960 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2961 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002962 }
2963 else
2964 rv = VPPCOM_EINVAL;
2965 break;
2966
Stevenb5a11602017-10-11 09:59:30 -07002967 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002968 if (buffer && buflen && (*buflen == sizeof (int)) &&
2969 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2970 {
2971 /* VPP-TBD */
2972 if (*(int *) buffer)
2973 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2974 else
2975 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2976
Florin Coras5e062572019-03-14 19:07:51 -07002977 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2978 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
2979 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002980 }
2981 else
2982 rv = VPPCOM_EINVAL;
2983 break;
2984
2985 case VPPCOM_ATTR_GET_REUSEPORT:
2986 if (buffer && buflen && (*buflen >= sizeof (int)))
2987 {
2988 /* VPP-TBD */
2989 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2990 VCL_SESS_ATTR_REUSEPORT);
2991 *buflen = sizeof (int);
2992
Florin Coras5e062572019-03-14 19:07:51 -07002993 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2994 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002995 }
2996 else
2997 rv = VPPCOM_EINVAL;
2998 break;
2999
3000 case VPPCOM_ATTR_SET_REUSEPORT:
3001 if (buffer && buflen && (*buflen == sizeof (int)) &&
3002 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3003 {
3004 /* VPP-TBD */
3005 if (*(int *) buffer)
3006 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3007 else
3008 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3009
Florin Coras5e062572019-03-14 19:07:51 -07003010 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3011 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
3012 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003013 }
3014 else
3015 rv = VPPCOM_EINVAL;
3016 break;
3017
3018 case VPPCOM_ATTR_GET_BROADCAST:
3019 if (buffer && buflen && (*buflen >= sizeof (int)))
3020 {
3021 /* VPP-TBD */
3022 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3023 VCL_SESS_ATTR_BROADCAST);
3024 *buflen = sizeof (int);
3025
Florin Coras5e062572019-03-14 19:07:51 -07003026 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3027 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003028 }
3029 else
3030 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003031 break;
3032
3033 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003034 if (buffer && buflen && (*buflen == sizeof (int)))
3035 {
3036 /* VPP-TBD */
3037 if (*(int *) buffer)
3038 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3039 else
3040 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3041
Florin Coras5e062572019-03-14 19:07:51 -07003042 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3043 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
3044 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003045 }
3046 else
3047 rv = VPPCOM_EINVAL;
3048 break;
3049
3050 case VPPCOM_ATTR_GET_V6ONLY:
3051 if (buffer && buflen && (*buflen >= sizeof (int)))
3052 {
3053 /* VPP-TBD */
3054 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3055 VCL_SESS_ATTR_V6ONLY);
3056 *buflen = sizeof (int);
3057
Florin Coras5e062572019-03-14 19:07:51 -07003058 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3059 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003060 }
3061 else
3062 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003063 break;
3064
3065 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003066 if (buffer && buflen && (*buflen == sizeof (int)))
3067 {
3068 /* VPP-TBD */
3069 if (*(int *) buffer)
3070 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3071 else
3072 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3073
Florin Coras5e062572019-03-14 19:07:51 -07003074 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3075 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
3076 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003077 }
3078 else
3079 rv = VPPCOM_EINVAL;
3080 break;
3081
3082 case VPPCOM_ATTR_GET_KEEPALIVE:
3083 if (buffer && buflen && (*buflen >= sizeof (int)))
3084 {
3085 /* VPP-TBD */
3086 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3087 VCL_SESS_ATTR_KEEPALIVE);
3088 *buflen = sizeof (int);
3089
Florin Coras5e062572019-03-14 19:07:51 -07003090 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3091 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003092 }
3093 else
3094 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003095 break;
Stevenbccd3392017-10-12 20:42:21 -07003096
3097 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003098 if (buffer && buflen && (*buflen == sizeof (int)))
3099 {
3100 /* VPP-TBD */
3101 if (*(int *) buffer)
3102 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3103 else
3104 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3105
Florin Coras5e062572019-03-14 19:07:51 -07003106 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3107 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
3108 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003109 }
3110 else
3111 rv = VPPCOM_EINVAL;
3112 break;
3113
3114 case VPPCOM_ATTR_GET_TCP_NODELAY:
3115 if (buffer && buflen && (*buflen >= sizeof (int)))
3116 {
3117 /* VPP-TBD */
3118 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3119 VCL_SESS_ATTR_TCP_NODELAY);
3120 *buflen = sizeof (int);
3121
Florin Coras5e062572019-03-14 19:07:51 -07003122 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3123 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003124 }
3125 else
3126 rv = VPPCOM_EINVAL;
3127 break;
3128
3129 case VPPCOM_ATTR_SET_TCP_NODELAY:
3130 if (buffer && buflen && (*buflen == sizeof (int)))
3131 {
3132 /* VPP-TBD */
3133 if (*(int *) buffer)
3134 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3135 else
3136 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3137
Florin Coras5e062572019-03-14 19:07:51 -07003138 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3139 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3140 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003141 }
3142 else
3143 rv = VPPCOM_EINVAL;
3144 break;
3145
3146 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3147 if (buffer && buflen && (*buflen >= sizeof (int)))
3148 {
3149 /* VPP-TBD */
3150 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3151 VCL_SESS_ATTR_TCP_KEEPIDLE);
3152 *buflen = sizeof (int);
3153
Florin Coras5e062572019-03-14 19:07:51 -07003154 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3155 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003156 }
3157 else
3158 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003159 break;
3160
3161 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003162 if (buffer && buflen && (*buflen == sizeof (int)))
3163 {
3164 /* VPP-TBD */
3165 if (*(int *) buffer)
3166 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3167 else
3168 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3169
Florin Coras5e062572019-03-14 19:07:51 -07003170 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003171 VCL_SESS_ATTR_TEST (session->attr,
3172 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003173 }
3174 else
3175 rv = VPPCOM_EINVAL;
3176 break;
3177
3178 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3179 if (buffer && buflen && (*buflen >= sizeof (int)))
3180 {
3181 /* VPP-TBD */
3182 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3183 VCL_SESS_ATTR_TCP_KEEPINTVL);
3184 *buflen = sizeof (int);
3185
Florin Coras5e062572019-03-14 19:07:51 -07003186 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3187 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003188 }
3189 else
3190 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003191 break;
3192
3193 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003194 if (buffer && buflen && (*buflen == sizeof (int)))
3195 {
3196 /* VPP-TBD */
3197 if (*(int *) buffer)
3198 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3199 else
3200 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3201
Florin Coras5e062572019-03-14 19:07:51 -07003202 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003203 VCL_SESS_ATTR_TEST (session->attr,
3204 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003205 }
3206 else
3207 rv = VPPCOM_EINVAL;
3208 break;
3209
3210 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3211 if (buffer && buflen && (*buflen >= sizeof (u32)))
3212 {
3213 /* VPP-TBD */
3214 *(u32 *) buffer = session->user_mss;
3215 *buflen = sizeof (int);
3216
Florin Coras5e062572019-03-14 19:07:51 -07003217 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3218 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003219 }
3220 else
3221 rv = VPPCOM_EINVAL;
3222 break;
3223
3224 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3225 if (buffer && buflen && (*buflen == sizeof (u32)))
3226 {
3227 /* VPP-TBD */
3228 session->user_mss = *(u32 *) buffer;
3229
Florin Coras5e062572019-03-14 19:07:51 -07003230 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3231 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003232 }
3233 else
3234 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003235 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003236
Florin Coras7baeb712019-01-04 17:05:43 -08003237 case VPPCOM_ATTR_SET_SHUT:
3238 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3239 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3240 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3241 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3242 break;
3243
3244 case VPPCOM_ATTR_GET_SHUT:
3245 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3246 tmp_flags = 1;
3247 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3248 tmp_flags |= 2;
3249 if (tmp_flags == 1)
3250 *(int *) buffer = SHUT_RD;
3251 else if (tmp_flags == 2)
3252 *(int *) buffer = SHUT_WR;
3253 else if (tmp_flags == 3)
3254 *(int *) buffer = SHUT_RDWR;
3255 *buflen = sizeof (int);
3256 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003257 default:
3258 rv = VPPCOM_EINVAL;
3259 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003260 }
3261
Dave Wallace35830af2017-10-09 01:43:42 -04003262 return rv;
3263}
3264
Stevenac1f96d2017-10-24 16:03:58 -07003265int
Florin Coras134a9962018-08-28 11:32:04 -07003266vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003267 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3268{
Florin Coras134a9962018-08-28 11:32:04 -07003269 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003270 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003271 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003272
3273 if (ep)
3274 {
Florin Coras134a9962018-08-28 11:32:04 -07003275 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003276 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003277 {
Florin Coras5e062572019-03-14 19:07:51 -07003278 VDBG (0, "sh 0x%llx is closed!", session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003279 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003280 }
Florin Coras7e12d942018-06-27 14:32:43 -07003281 ep->is_ip4 = session->transport.is_ip4;
3282 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003283 }
Steven58f464e2017-10-25 12:33:12 -07003284
3285 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003286 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003287 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003288 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003289 else
3290 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003291 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003292 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003293 }
3294
Florin Coras99368312018-08-02 10:45:44 -07003295 if (ep)
3296 {
3297 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003298 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3299 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003300 else
Dave Barach178cf492018-11-13 16:34:13 -05003301 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3302 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003303 }
Florin Coras460dce62018-07-27 05:45:06 -07003304
Stevenac1f96d2017-10-24 16:03:58 -07003305 return rv;
3306}
3307
3308int
Florin Coras134a9962018-08-28 11:32:04 -07003309vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003310 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3311{
Dave Wallace617dffa2017-10-26 14:47:06 -04003312 if (!buffer)
3313 return VPPCOM_EINVAL;
3314
3315 if (ep)
3316 {
3317 // TBD
3318 return VPPCOM_EINVAL;
3319 }
3320
3321 if (flags)
3322 {
3323 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003324 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003325 }
3326
Florin Coras42ceddb2018-12-12 10:56:01 -08003327 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003328}
3329
Dave Wallace048b1d62018-01-03 22:24:41 -05003330int
3331vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3332{
Florin Coras134a9962018-08-28 11:32:04 -07003333 vcl_worker_t *wrk = vcl_worker_get_current ();
3334 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003335 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003336 svm_msg_q_msg_t msg;
3337 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003338 int rv, num_ev = 0;
3339
Florin Coras5e062572019-03-14 19:07:51 -07003340 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003341
3342 if (!vp)
3343 return VPPCOM_EFAULT;
3344
3345 do
3346 {
Florin Coras7e12d942018-06-27 14:32:43 -07003347 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003348
Florin Coras6917b942018-11-13 22:44:54 -08003349 /* Dequeue all events and drop all unhandled io events */
3350 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3351 {
3352 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3353 vcl_handle_mq_event (wrk, e);
3354 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3355 }
3356 vec_reset_length (wrk->unhandled_evts_vector);
3357
Dave Wallace048b1d62018-01-03 22:24:41 -05003358 for (i = 0; i < n_sids; i++)
3359 {
Florin Coras7baeb712019-01-04 17:05:43 -08003360 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003361 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003362 {
3363 vp[i].revents = POLLHUP;
3364 num_ev++;
3365 continue;
3366 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003367
Florin Coras6917b942018-11-13 22:44:54 -08003368 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003369
3370 if (POLLIN & vp[i].events)
3371 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003372 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003373 if (rv > 0)
3374 {
Florin Coras6917b942018-11-13 22:44:54 -08003375 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003376 num_ev++;
3377 }
3378 else if (rv < 0)
3379 {
3380 switch (rv)
3381 {
3382 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003383 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003384 break;
3385
3386 default:
Florin Coras6917b942018-11-13 22:44:54 -08003387 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003388 break;
3389 }
3390 num_ev++;
3391 }
3392 }
3393
3394 if (POLLOUT & vp[i].events)
3395 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003396 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003397 if (rv > 0)
3398 {
Florin Coras6917b942018-11-13 22:44:54 -08003399 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003400 num_ev++;
3401 }
3402 else if (rv < 0)
3403 {
3404 switch (rv)
3405 {
3406 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003407 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003408 break;
3409
3410 default:
Florin Coras6917b942018-11-13 22:44:54 -08003411 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003412 break;
3413 }
3414 num_ev++;
3415 }
3416 }
3417
Dave Wallace7e607a72018-06-18 18:41:32 -04003418 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003419 {
Florin Coras6917b942018-11-13 22:44:54 -08003420 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003421 num_ev++;
3422 }
3423 }
3424 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003425 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003426 }
3427 while ((num_ev == 0) && keep_trying);
3428
Dave Wallace048b1d62018-01-03 22:24:41 -05003429 return num_ev;
3430}
3431
Florin Coras99368312018-08-02 10:45:44 -07003432int
3433vppcom_mq_epoll_fd (void)
3434{
Florin Coras134a9962018-08-28 11:32:04 -07003435 vcl_worker_t *wrk = vcl_worker_get_current ();
3436 return wrk->mqs_epfd;
3437}
3438
3439int
Florin Coras30e79c22019-01-02 19:31:22 -08003440vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003441{
3442 return session_handle & 0xFFFFFF;
3443}
3444
3445int
Florin Coras30e79c22019-01-02 19:31:22 -08003446vppcom_session_worker (vcl_session_handle_t session_handle)
3447{
3448 return session_handle >> 24;
3449}
3450
3451int
Florin Coras134a9962018-08-28 11:32:04 -07003452vppcom_worker_register (void)
3453{
Florin Coras47c40e22018-11-26 17:01:36 -08003454 if (!vcl_worker_alloc_and_init ())
3455 return VPPCOM_EEXIST;
3456
3457 if (vcl_worker_set_bapi ())
3458 return VPPCOM_EEXIST;
3459
3460 if (vcl_worker_register_with_vpp ())
3461 return VPPCOM_EEXIST;
3462
3463 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003464}
3465
Florin Coras369db832019-07-08 12:34:45 -07003466void
3467vppcom_worker_unregister (void)
3468{
3469 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3470 vcl_set_worker_index (~0);
3471}
3472
Florin Corasdfe4cf42018-11-28 22:13:45 -08003473int
3474vppcom_worker_index (void)
3475{
3476 return vcl_get_worker_index ();
3477}
3478
Florin Corase0982e52019-01-25 13:19:56 -08003479int
3480vppcom_worker_mqs_epfd (void)
3481{
3482 vcl_worker_t *wrk = vcl_worker_get_current ();
3483 if (!vcm->cfg.use_mq_eventfd)
3484 return -1;
3485 return wrk->mqs_epfd;
3486}
3487
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003488int
3489vppcom_session_is_connectable_listener (uint32_t session_handle)
3490{
3491 vcl_session_t *session;
3492 vcl_worker_t *wrk = vcl_worker_get_current ();
3493 session = vcl_session_get_w_handle (wrk, session_handle);
3494 if (!session)
3495 return VPPCOM_EBADFD;
3496 return vcl_session_is_connectable_listener (wrk, session);
3497}
3498
3499int
3500vppcom_session_listener (uint32_t session_handle)
3501{
3502 vcl_worker_t *wrk = vcl_worker_get_current ();
3503 vcl_session_t *listen_session, *session;
3504 session = vcl_session_get_w_handle (wrk, session_handle);
3505 if (!session)
3506 return VPPCOM_EBADFD;
3507 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3508 return VPPCOM_EBADFD;
3509 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3510 if (!listen_session)
3511 return VPPCOM_EBADFD;
3512 return vcl_session_handle (listen_session);
3513}
3514
3515int
3516vppcom_session_n_accepted (uint32_t session_handle)
3517{
3518 vcl_worker_t *wrk = vcl_worker_get_current ();
3519 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3520 if (!session)
3521 return VPPCOM_EBADFD;
3522 return session->n_accepted_sessions;
3523}
3524
Dave Wallacee22aa742017-10-20 12:30:38 -04003525/*
3526 * fd.io coding-style-patch-verification: ON
3527 *
3528 * Local Variables:
3529 * eval: (c-set-style "gnu")
3530 * End:
3531 */