blob: 9a351d8aca6f67ed2c0ab03ccaf4c28b6ebbc675 [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;
315 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
Florin Coras54693d22018-07-17 10:46:29 -0700316
Florin Coras5e062572019-03-14 19:07:51 -0700317 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
318 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700319 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
320 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
321 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700322 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
323
Florin Coras00cca802019-06-06 09:38:44 -0700324 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
325 session->vpp_handle, 0);
326
Florin Coras134a9962018-08-28 11:32:04 -0700327 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700328
329error:
330 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
331 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
332 VNET_API_ERROR_INVALID_ARGUMENT);
333 vcl_session_free (wrk, session);
334 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700335}
336
337static u32
Florin Coras134a9962018-08-28 11:32:04 -0700338vcl_session_connected_handler (vcl_worker_t * wrk,
339 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700340{
Florin Coras99368312018-08-02 10:45:44 -0700341 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700342 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700343 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -0700344
345 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700346 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700347 if (!session)
348 {
Florin Coras5e062572019-03-14 19:07:51 -0700349 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
350 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700351 return VCL_INVALID_SESSION_INDEX;
352 }
Florin Coras54693d22018-07-17 10:46:29 -0700353 if (mp->retval)
354 {
Florin Coras5e062572019-03-14 19:07:51 -0700355 VDBG (0, "ERROR: session index %u: connect failed! %U",
356 session_index, format_api_error, ntohl (mp->retval));
Florin Coras070453d2018-08-24 17:04:27 -0700357 session->session_state = STATE_FAILED;
358 session->vpp_handle = mp->handle;
359 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700360 }
361
Florin Coras460dce62018-07-27 05:45:06 -0700362 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
363 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800364 if (vcl_wait_for_segment (mp->segment_handle))
365 {
Florin Coras653e43f2019-03-04 10:56:23 -0800366 VDBG (0, "segment for session %u couldn't be mounted!",
367 session->session_index);
Florin Coras5e062572019-03-14 19:07:51 -0700368 session->session_state = STATE_FAILED;
Florin Corasd85de682018-11-29 17:02:29 -0800369 return VCL_INVALID_SESSION_INDEX;
370 }
371
Florin Coras460dce62018-07-27 05:45:06 -0700372 rx_fifo->client_session_index = session_index;
373 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700374 rx_fifo->client_thread_index = vcl_get_worker_index ();
375 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700376
Florin Coras653e43f2019-03-04 10:56:23 -0800377 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
378 svm_msg_q_t *);
379 vpp_wrk_index = tx_fifo->master_thread_index;
380 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
381 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700382
Florin Coras653e43f2019-03-04 10:56:23 -0800383 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700384 {
Florin Coras653e43f2019-03-04 10:56:23 -0800385 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
386 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
387 if (vcl_wait_for_segment (mp->ct_segment_handle))
388 {
389 VDBG (0, "ct segment for session %u couldn't be mounted!",
390 session->session_index);
Florin Coras5e062572019-03-14 19:07:51 -0700391 session->session_state = STATE_FAILED;
Florin Coras653e43f2019-03-04 10:56:23 -0800392 return VCL_INVALID_SESSION_INDEX;
393 }
Florin Coras99368312018-08-02 10:45:44 -0700394 }
Florin Coras54693d22018-07-17 10:46:29 -0700395
Florin Coras54693d22018-07-17 10:46:29 -0700396 session->rx_fifo = rx_fifo;
397 session->tx_fifo = tx_fifo;
398 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800399 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras09d18c22019-04-24 11:10:02 -0700400 session->transport.is_ip4 = mp->lcl.is_ip4;
401 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500402 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700403 session->transport.lcl_port = mp->lcl.port;
Florin Coras54693d22018-07-17 10:46:29 -0700404 session->session_state = STATE_CONNECT;
405
406 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800407 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700408
Florin Coras5e062572019-03-14 19:07:51 -0700409 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
410 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700411 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700412
Florin Coras54693d22018-07-17 10:46:29 -0700413 return session_index;
414}
415
Florin Coras3c7d4f92018-12-14 11:28:43 -0800416static int
417vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
418{
419 vcl_session_msg_t *accepted_msg;
420 int i;
421
422 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
423 {
424 accepted_msg = &session->accept_evts_fifo[i];
425 if (accepted_msg->accepted_msg.handle == handle)
426 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800427 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800428 return 1;
429 }
430 }
431 return 0;
432}
433
Florin Corasc9fbd662018-08-24 12:59:56 -0700434static u32
Florin Coras134a9962018-08-28 11:32:04 -0700435vcl_session_reset_handler (vcl_worker_t * wrk,
436 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700437{
438 vcl_session_t *session;
439 u32 sid;
440
Florin Coras134a9962018-08-28 11:32:04 -0700441 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
442 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700443 if (!session)
444 {
445 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
446 return VCL_INVALID_SESSION_INDEX;
447 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800448
449 /* Caught a reset before actually accepting the session */
450 if (session->session_state == STATE_LISTEN)
451 {
452
453 if (!vcl_flag_accepted_session (session, reset_msg->handle,
454 VCL_ACCEPTED_F_RESET))
455 VDBG (0, "session was not accepted!");
456 return VCL_INVALID_SESSION_INDEX;
457 }
458
459 session->session_state = STATE_DISCONNECT;
460 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700461 return sid;
462}
463
Florin Coras60116992018-08-27 09:52:18 -0700464static u32
Florin Coras134a9962018-08-28 11:32:04 -0700465vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700466{
467 vcl_session_t *session;
468 u32 sid = mp->context;
469
Florin Coras134a9962018-08-28 11:32:04 -0700470 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700471 if (mp->retval)
472 {
Florin Coras5e062572019-03-14 19:07:51 -0700473 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Corasd85de682018-11-29 17:02:29 -0800474 format_api_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700475 if (session)
476 {
477 session->session_state = STATE_FAILED;
478 session->vpp_handle = mp->handle;
479 return sid;
480 }
481 else
482 {
Florin Coras5e062572019-03-14 19:07:51 -0700483 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
484 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700485 return VCL_INVALID_SESSION_INDEX;
486 }
487 }
488
489 session->vpp_handle = mp->handle;
490 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500491 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
492 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700493 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700494 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700495 session->session_state = STATE_LISTEN;
496
Florin Coras5f45e012019-01-23 09:21:30 -0800497 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
498 vec_validate (wrk->vpp_event_queues, 0);
499 wrk->vpp_event_queues[0] = session->vpp_evt_q;
500
Florin Coras60116992018-08-27 09:52:18 -0700501 if (session->is_dgram)
502 {
503 svm_fifo_t *rx_fifo, *tx_fifo;
504 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
505 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
506 rx_fifo->client_session_index = sid;
507 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
508 tx_fifo->client_session_index = sid;
509 session->rx_fifo = rx_fifo;
510 session->tx_fifo = tx_fifo;
511 }
512
Florin Coras05ecfcc2018-12-12 18:19:39 -0800513 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700514 return sid;
515}
516
Florin Corasdfae9f92019-02-20 19:48:31 -0800517static void
518vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
519{
520 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
521 vcl_session_t *s;
522
523 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
524 if (!s || s->session_state != STATE_DISCONNECT)
525 {
526 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
527 return;
528 }
529
530 if (mp->retval)
531 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
532 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
533
534 if (mp->context != wrk->wrk_index)
535 VDBG (0, "wrong context");
536
537 vcl_session_table_del_vpp_handle (wrk, mp->handle);
538 vcl_session_free (wrk, s);
539}
540
Florin Coras3c7d4f92018-12-14 11:28:43 -0800541static vcl_session_t *
542vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
543{
544 vcl_session_msg_t *vcl_msg;
545 vcl_session_t *session;
546
547 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
548 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800549 VWRN ("session overlap handle %lu state %u!", msg->handle,
550 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800551
552 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
553 if (!session)
554 {
555 VERR ("couldn't find listen session: listener handle %llx",
556 msg->listener_handle);
557 return 0;
558 }
559
560 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
561 vcl_msg->accepted_msg = *msg;
562 /* Session handle points to listener until fully accepted by app */
563 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
564
565 return session;
566}
567
568static vcl_session_t *
569vcl_session_disconnected_handler (vcl_worker_t * wrk,
570 session_disconnected_msg_t * msg)
571{
572 vcl_session_t *session;
573
574 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
575 if (!session)
576 {
577 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
578 return 0;
579 }
580
581 /* Caught a disconnect before actually accepting the session */
582 if (session->session_state == STATE_LISTEN)
583 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800584 if (!vcl_flag_accepted_session (session, msg->handle,
585 VCL_ACCEPTED_F_CLOSED))
586 VDBG (0, "session was not accepted!");
587 return 0;
588 }
589
590 session->session_state = STATE_VPP_CLOSING;
591 return session;
592}
593
Florin Coras30e79c22019-01-02 19:31:22 -0800594static void
595vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
596{
597 session_req_worker_update_msg_t *msg;
598 vcl_session_t *s;
599
600 msg = (session_req_worker_update_msg_t *) data;
601 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
602 if (!s)
603 return;
604
605 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
606}
607
608static void
609vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
610{
611 session_worker_update_reply_msg_t *msg;
612 vcl_session_t *s;
613
614 msg = (session_worker_update_reply_msg_t *) data;
615 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
616 if (!s)
617 {
618 VDBG (0, "unknown handle 0x%llx", msg->handle);
619 return;
620 }
621 if (vcl_wait_for_segment (msg->segment_handle))
622 {
623 clib_warning ("segment for session %u couldn't be mounted!",
624 s->session_index);
625 return;
626 }
Florin Coras30e79c22019-01-02 19:31:22 -0800627
Florin Coras5f45e012019-01-23 09:21:30 -0800628 if (s->rx_fifo)
629 {
630 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
631 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
632 s->rx_fifo->client_session_index = s->session_index;
633 s->tx_fifo->client_session_index = s->session_index;
634 s->rx_fifo->client_thread_index = wrk->wrk_index;
635 s->tx_fifo->client_thread_index = wrk->wrk_index;
636 }
Florin Coras30e79c22019-01-02 19:31:22 -0800637 s->session_state = STATE_UPDATED;
638
Florin Coras30e79c22019-01-02 19:31:22 -0800639 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
640 s->vpp_handle, wrk->wrk_index);
641}
642
Florin Coras86f04502018-09-12 16:08:01 -0700643static int
644vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700645{
Florin Coras54693d22018-07-17 10:46:29 -0700646 session_disconnected_msg_t *disconnected_msg;
Florin Coras54693d22018-07-17 10:46:29 -0700647 vcl_session_t *session;
Florin Coras54693d22018-07-17 10:46:29 -0700648
649 switch (e->event_type)
650 {
Florin Coras653e43f2019-03-04 10:56:23 -0800651 case SESSION_IO_EVT_RX:
652 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -0800653 session = vcl_session_get (wrk, e->session_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800654 if (!session || !(session->session_state & STATE_OPEN))
655 break;
Florin Coras86f04502018-09-12 16:08:01 -0700656 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700657 break;
658 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800659 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700660 break;
661 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700662 vcl_session_connected_handler (wrk,
663 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700664 break;
665 case SESSION_CTRL_EVT_DISCONNECTED:
666 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800667 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Corasc9fbd662018-08-24 12:59:56 -0700668 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800669 break;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800670 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
671 session->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700672 break;
673 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700674 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700675 break;
676 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700677 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700678 break;
Florin Corasdfae9f92019-02-20 19:48:31 -0800679 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
680 vcl_session_unlisten_reply_handler (wrk, e->data);
681 break;
Florin Coras30e79c22019-01-02 19:31:22 -0800682 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
683 vcl_session_req_worker_update_handler (wrk, e->data);
684 break;
685 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
686 vcl_session_worker_update_reply_handler (wrk, e->data);
687 break;
Florin Coras54693d22018-07-17 10:46:29 -0700688 default:
689 clib_warning ("unhandled %u", e->event_type);
690 }
691 return VPPCOM_OK;
692}
693
Florin Coras30e79c22019-01-02 19:31:22 -0800694static int
Florin Coras697faea2018-06-27 17:10:49 -0700695vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -0800696 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -0700697 f64 wait_for_time)
698{
Florin Coras134a9962018-08-28 11:32:04 -0700699 vcl_worker_t *wrk = vcl_worker_get_current ();
700 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700701 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700702 svm_msg_q_msg_t msg;
703 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400704
Florin Coras697faea2018-06-27 17:10:49 -0700705 do
Dave Wallace543852a2017-08-03 02:11:34 -0400706 {
Florin Coras134a9962018-08-28 11:32:04 -0700707 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700708 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700709 {
Florin Coras070453d2018-08-24 17:04:27 -0700710 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700711 }
712 if (session->session_state & state)
713 {
Florin Coras697faea2018-06-27 17:10:49 -0700714 return VPPCOM_OK;
715 }
716 if (session->session_state & STATE_FAILED)
717 {
Florin Coras697faea2018-06-27 17:10:49 -0700718 return VPPCOM_ECONNREFUSED;
719 }
Florin Coras54693d22018-07-17 10:46:29 -0700720
Florin Coras134a9962018-08-28 11:32:04 -0700721 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -0800722 {
723 usleep (100);
724 continue;
725 }
Florin Coras134a9962018-08-28 11:32:04 -0700726 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700727 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700728 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800729 }
Florin Coras134a9962018-08-28 11:32:04 -0700730 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800731
Florin Coras05ecfcc2018-12-12 18:19:39 -0800732 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -0700733 vppcom_session_state_str (state));
734 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400735
Florin Coras697faea2018-06-27 17:10:49 -0700736 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500737}
738
Florin Coras30e79c22019-01-02 19:31:22 -0800739static void
740vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
741{
Florin Coras288eaab2019-02-03 15:26:14 -0800742 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -0800743 vcl_session_t *s;
744 u32 *sip;
745
746 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
747 return;
748
749 vec_foreach (sip, wrk->pending_session_wrk_updates)
750 {
751 s = vcl_session_get (wrk, *sip);
752 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
753 state = s->session_state;
754 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
755 s->session_state = state;
756 }
757 vec_reset_length (wrk->pending_session_wrk_updates);
758}
759
Florin Corasf9240dc2019-01-15 08:03:17 -0800760void
Florin Coras30e79c22019-01-02 19:31:22 -0800761vcl_flush_mq_events (void)
762{
763 vcl_worker_t *wrk = vcl_worker_get_current ();
764 svm_msg_q_msg_t *msg;
765 session_event_t *e;
766 svm_msg_q_t *mq;
767 int i;
768
769 mq = wrk->app_event_queue;
770 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -0700771 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -0800772 svm_msg_q_unlock (mq);
773
774 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
775 {
776 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
777 e = svm_msg_q_msg_data (mq, msg);
778 vcl_handle_mq_event (wrk, e);
779 svm_msg_q_free_msg (mq, msg);
780 }
781 vec_reset_length (wrk->mq_msg_vector);
782 vcl_handle_pending_wrk_updates (wrk);
783}
784
Florin Coras697faea2018-06-27 17:10:49 -0700785static int
786vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400787{
Florin Coras697faea2018-06-27 17:10:49 -0700788 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400789
Florin Coras697faea2018-06-27 17:10:49 -0700790 if (vcm->app_state != STATE_APP_ENABLED)
791 {
792 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -0700793 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -0700794 if (PREDICT_FALSE (rv))
795 {
Florin Coras5e062572019-03-14 19:07:51 -0700796 VDBG (0, "application session enable timed out! returning %d (%s)",
797 rv, vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -0700798 return rv;
799 }
800 }
801 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400802}
803
Florin Coras697faea2018-06-27 17:10:49 -0700804static int
805vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400806{
Florin Coras697faea2018-06-27 17:10:49 -0700807 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400808
Florin Coras697faea2018-06-27 17:10:49 -0700809 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -0700810 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -0700811 if (PREDICT_FALSE (rv))
812 {
Florin Coras5e062572019-03-14 19:07:51 -0700813 VDBG (0, "application attach timed out! returning %d (%s)", rv,
814 vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -0700815 return rv;
816 }
Dave Wallace543852a2017-08-03 02:11:34 -0400817
Florin Coras697faea2018-06-27 17:10:49 -0700818 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400819}
820
821static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700822vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400823{
Florin Coras134a9962018-08-28 11:32:04 -0700824 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700825 vcl_session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500826 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400827
Florin Corasab2f6db2018-08-31 14:31:41 -0700828 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700829 if (!session)
830 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500831
832 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500833 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700834 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500835
Florin Coras5e062572019-03-14 19:07:51 -0700836 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
837 vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -0700838 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras2d675d72019-01-28 15:54:27 -0800839 vppcom_send_unbind_sock (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500840
Florin Coras070453d2018-08-24 17:04:27 -0700841 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400842}
843
Florin Coras697faea2018-06-27 17:10:49 -0700844static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700845vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400846{
Florin Coras134a9962018-08-28 11:32:04 -0700847 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700848 svm_msg_q_t *vpp_evt_q;
Florin Coras7e12d942018-06-27 14:32:43 -0700849 vcl_session_t *session;
Florin Coras288eaab2019-02-03 15:26:14 -0800850 vcl_session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700851 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400852
Florin Corasab2f6db2018-08-31 14:31:41 -0700853 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -0700854 if (!session)
855 return VPPCOM_EBADFD;
856
Dave Wallace4878cbe2017-11-21 03:45:09 -0500857 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700858 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500859
Florin Coras5e062572019-03-14 19:07:51 -0700860 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
861 vpp_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400862
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800863 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400864 {
Florin Coras5e062572019-03-14 19:07:51 -0700865 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
Florin Coras070453d2018-08-24 17:04:27 -0700866 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500867 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400868
Florin Coras3c7d4f92018-12-14 11:28:43 -0800869 if (state & STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500870 {
Florin Coras134a9962018-08-28 11:32:04 -0700871 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras47c40e22018-11-26 17:01:36 -0800872 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
Florin Coras99368312018-08-02 10:45:44 -0700873 vpp_handle, 0);
Florin Coras5e062572019-03-14 19:07:51 -0700874 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
875 session->session_index, vpp_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400876 }
877 else
Dave Wallace227867f2017-11-13 21:21:53 -0500878 {
Florin Coras5e062572019-03-14 19:07:51 -0700879 VDBG (1, "session %u [0x%llx]: sending disconnect...",
880 session->session_index, vpp_handle);
Florin Corasab2f6db2018-08-31 14:31:41 -0700881 vppcom_send_disconnect_session (vpp_handle);
Dave Wallace227867f2017-11-13 21:21:53 -0500882 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400883
Florin Coras070453d2018-08-24 17:04:27 -0700884 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400885}
886
Florin Coras940f78f2018-11-30 12:11:20 -0800887/**
888 * Handle app exit
889 *
890 * Notify vpp of the disconnect and mark the worker as free. If we're the
891 * last worker, do a full cleanup otherwise, since we're probably a forked
892 * child, avoid syscalls as much as possible. We might've lost privileges.
893 */
894void
895vppcom_app_exit (void)
896{
897 if (!pool_elts (vcm->workers))
898 return;
Florin Coras01f3f892018-12-02 12:45:53 -0800899 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
900 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -0800901 vcl_elog_stop (vcm);
902 if (vec_len (vcm->workers) == 1)
903 vl_client_disconnect_from_vlib ();
904 else
Florin Coraseaec2a62018-12-04 16:34:05 -0800905 vl_client_send_disconnect (1 /* vpp should cleanup */ );
Florin Coras940f78f2018-11-30 12:11:20 -0800906}
907
Dave Wallace543852a2017-08-03 02:11:34 -0400908/*
909 * VPPCOM Public API functions
910 */
911int
912vppcom_app_create (char *app_name)
913{
Dave Wallace543852a2017-08-03 02:11:34 -0400914 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400915 int rv;
916
Florin Coras47c40e22018-11-26 17:01:36 -0800917 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -0400918 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800919 VDBG (1, "already initialized");
920 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -0400921 }
922
Florin Coras47c40e22018-11-26 17:01:36 -0800923 vcm->is_init = 1;
924 vppcom_cfg (&vcm->cfg);
925 vcl_cfg = &vcm->cfg;
926
927 vcm->main_cpu = pthread_self ();
928 vcm->main_pid = getpid ();
929 vcm->app_name = format (0, "%s", app_name);
930 vppcom_init_error_string_table ();
Florin Coras88001c62019-04-24 14:44:46 -0700931 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
932 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -0800933 pool_alloc (vcm->workers, vcl_cfg->max_workers);
934 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -0800935 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -0800936 atexit (vppcom_app_exit);
Florin Coras47c40e22018-11-26 17:01:36 -0800937
938 /* Allocate default worker */
939 vcl_worker_alloc_and_init ();
940
941 /* API hookup and connect to VPP */
942 vppcom_api_hookup ();
943 vcl_elog_init (vcm);
944 vcm->app_state = STATE_APP_START;
945 rv = vppcom_connect_to_vpp (app_name);
946 if (rv)
Dave Wallace543852a2017-08-03 02:11:34 -0400947 {
Florin Coras47c40e22018-11-26 17:01:36 -0800948 VERR ("couldn't connect to VPP!");
949 return rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400950 }
Florin Coras47c40e22018-11-26 17:01:36 -0800951 VDBG (0, "sending session enable");
952 rv = vppcom_app_session_enable ();
953 if (rv)
954 {
955 VERR ("vppcom_app_session_enable() failed!");
956 return rv;
957 }
958
959 VDBG (0, "sending app attach");
960 rv = vppcom_app_attach ();
961 if (rv)
962 {
963 VERR ("vppcom_app_attach() failed!");
964 return rv;
965 }
966
967 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
968 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400969
970 return VPPCOM_OK;
971}
972
973void
974vppcom_app_destroy (void)
975{
Dave Wallace543852a2017-08-03 02:11:34 -0400976 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400977 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400978
Florin Coras940f78f2018-11-30 12:11:20 -0800979 if (!pool_elts (vcm->workers))
980 return;
981
Florin Coras0d427d82018-06-27 03:24:07 -0700982 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800983
Florin Coras940f78f2018-11-30 12:11:20 -0800984 if (pool_elts (vcm->workers) == 1)
Florin Coras47c40e22018-11-26 17:01:36 -0800985 {
986 vppcom_app_send_detach ();
987 orig_app_timeout = vcm->cfg.app_timeout;
988 vcm->cfg.app_timeout = 2.0;
989 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
990 vcm->cfg.app_timeout = orig_app_timeout;
991 if (PREDICT_FALSE (rv))
992 VDBG (0, "application detach timed out! returning %d (%s)", rv,
993 vppcom_retval_str (rv));
Florin Coras940f78f2018-11-30 12:11:20 -0800994 vec_free (vcm->app_name);
Florin Coras01f3f892018-12-02 12:45:53 -0800995 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -0800996 }
997 else
998 {
Florin Coras01f3f892018-12-02 12:45:53 -0800999 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001000 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001001
Florin Coras01f3f892018-12-02 12:45:53 -08001002 vcl_set_worker_index (~0);
Florin Coras0d427d82018-06-27 03:24:07 -07001003 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04001004 vl_client_disconnect_from_vlib ();
Dave Wallace543852a2017-08-03 02:11:34 -04001005}
1006
1007int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001008vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001009{
Florin Coras134a9962018-08-28 11:32:04 -07001010 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001011 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001012
Florin Coras134a9962018-08-28 11:32:04 -07001013 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001014
Florin Coras7e12d942018-06-27 14:32:43 -07001015 session->session_type = proto;
1016 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001017 session->vpp_handle = ~0;
Florin Coras460dce62018-07-27 05:45:06 -07001018 session->is_dgram = proto == VPPCOM_PROTO_UDP;
Dave Wallace543852a2017-08-03 02:11:34 -04001019
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001020 if (is_nonblocking)
1021 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001022
Florin Coras7e12d942018-06-27 14:32:43 -07001023 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1024 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001025
Florin Coras5e062572019-03-14 19:07:51 -07001026 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001027
Florin Coras134a9962018-08-28 11:32:04 -07001028 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001029}
1030
1031int
Florin Corasf9240dc2019-01-15 08:03:17 -08001032vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1033 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001034{
Florin Coras288eaab2019-02-03 15:26:14 -08001035 vcl_session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -07001036 u32 next_sh, vep_sh;
1037 int rv = VPPCOM_OK;
1038 u64 vpp_handle;
Florin Corasf9240dc2019-01-15 08:03:17 -08001039 u8 is_vep;
Florin Coras47c40e22018-11-26 17:01:36 -08001040
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001041 is_vep = session->is_vep;
Florin Coras134a9962018-08-28 11:32:04 -07001042 next_sh = session->vep.next_sh;
1043 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -07001044 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05001045 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001046
Florin Corasf9240dc2019-01-15 08:03:17 -08001047 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001048
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001049 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04001050 {
Florin Coras134a9962018-08-28 11:32:04 -07001051 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001052 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001053 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001054 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001055 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras47c40e22018-11-26 17:01:36 -08001056 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1057 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001058
Florin Coras134a9962018-08-28 11:32:04 -07001059 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001060 }
1061 }
1062 else
1063 {
Florin Coras47c40e22018-11-26 17:01:36 -08001064 if (session->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001065 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001066 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001067 if (rv < 0)
Florin Corasf9240dc2019-01-15 08:03:17 -08001068 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1069 "failed! rv %d (%s)", session->session_index, vpp_handle,
1070 vep_sh, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001071 }
1072
Florin Coras47c40e22018-11-26 17:01:36 -08001073 if (!do_disconnect)
Florin Coras30e79c22019-01-02 19:31:22 -08001074 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08001075 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Corasf9240dc2019-01-15 08:03:17 -08001076 session->session_index, vpp_handle);
Florin Coras30e79c22019-01-02 19:31:22 -08001077 goto cleanup;
1078 }
Florin Coras47c40e22018-11-26 17:01:36 -08001079
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001080 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001081 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001082 rv = vppcom_session_unbind (sh);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001083 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001084 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1085 "rv %d (%s)", session->session_index, vpp_handle, rv,
Florin Coras47c40e22018-11-26 17:01:36 -08001086 vppcom_retval_str (rv));
Florin Corasdfae9f92019-02-20 19:48:31 -08001087 return rv;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001088 }
Florin Coras070453d2018-08-24 17:04:27 -07001089 else if (state & STATE_OPEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001090 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001091 rv = vppcom_session_disconnect (sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001092 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001093 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1094 " rv %d (%s)", session->session_index, vpp_handle,
1095 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001096 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001097 else if (state == STATE_DISCONNECT)
1098 {
1099 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1100 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1101 session->vpp_handle, 0);
1102 }
Dave Wallace19481612017-09-15 18:47:44 -04001103 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001104
Florin Coras0ef8ef22019-01-18 08:37:13 -08001105 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1106
Florin Corasf9240dc2019-01-15 08:03:17 -08001107cleanup:
Florin Coras30e79c22019-01-02 19:31:22 -08001108 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Florin Coras134a9962018-08-28 11:32:04 -07001109 vcl_session_free (wrk, session);
Florin Coras0d427d82018-06-27 03:24:07 -07001110 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001111
Dave Wallace543852a2017-08-03 02:11:34 -04001112 return rv;
1113}
1114
1115int
Florin Corasf9240dc2019-01-15 08:03:17 -08001116vppcom_session_close (uint32_t session_handle)
1117{
1118 vcl_worker_t *wrk = vcl_worker_get_current ();
1119 vcl_session_t *session;
1120
1121 session = vcl_session_get_w_handle (wrk, session_handle);
1122 if (!session)
1123 return VPPCOM_EBADFD;
1124 return vcl_session_cleanup (wrk, session, session_handle,
1125 1 /* do_disconnect */ );
1126}
1127
1128int
Florin Coras134a9962018-08-28 11:32:04 -07001129vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001130{
Florin Coras134a9962018-08-28 11:32:04 -07001131 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001132 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001133
1134 if (!ep || !ep->ip)
1135 return VPPCOM_EINVAL;
1136
Florin Coras134a9962018-08-28 11:32:04 -07001137 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001138 if (!session)
1139 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001140
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001141 if (session->is_vep)
1142 {
Florin Coras5e062572019-03-14 19:07:51 -07001143 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1144 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001145 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001146 }
1147
Florin Coras7e12d942018-06-27 14:32:43 -07001148 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001149 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001150 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1151 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001152 else
Dave Barach178cf492018-11-13 16:34:13 -05001153 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1154 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001155 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001156
Florin Coras5e062572019-03-14 19:07:51 -07001157 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1158 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001159 session->transport.is_ip4 ? "IPv4" : "IPv6",
1160 format_ip46_address, &session->transport.lcl_ip,
1161 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1162 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001163 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001164 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001165
1166 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001167 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001168
Florin Coras070453d2018-08-24 17:04:27 -07001169 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001170}
1171
1172int
Florin Coras134a9962018-08-28 11:32:04 -07001173vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001174{
Florin Coras134a9962018-08-28 11:32:04 -07001175 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001176 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001177 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001178 int rv;
1179
Florin Coras134a9962018-08-28 11:32:04 -07001180 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001181 if (!listen_session || listen_session->is_vep)
Florin Coras070453d2018-08-24 17:04:27 -07001182 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001183
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001184 if (q_len == 0 || q_len == ~0)
1185 q_len = vcm->cfg.listen_queue_size;
1186
Dave Wallaceee45d412017-11-24 21:44:06 -05001187 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001188 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001189 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001190 VDBG (0, "session %u [0x%llx]: already in listen state!",
1191 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001192 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001193 }
1194
Florin Coras05ecfcc2018-12-12 18:19:39 -08001195 VDBG (0, "session %u [0x%llx]: sending vpp listen request...",
1196 listen_sh, listen_vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001197
Florin Coras070453d2018-08-24 17:04:27 -07001198 /*
1199 * Send listen request to vpp and wait for reply
1200 */
Florin Coras134a9962018-08-28 11:32:04 -07001201 vppcom_send_bind_sock (listen_session);
1202 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1203 STATE_LISTEN,
1204 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001205
Florin Coras070453d2018-08-24 17:04:27 -07001206 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001207 {
Florin Coras134a9962018-08-28 11:32:04 -07001208 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001209 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1210 listen_sh, listen_session->vpp_handle, rv,
1211 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001212 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001213 }
1214
Florin Coras070453d2018-08-24 17:04:27 -07001215 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001216}
1217
Ping Yu34a3a082018-11-30 19:16:17 -05001218int
1219vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1220 uint32_t cert_len)
1221{
1222
1223 vcl_worker_t *wrk = vcl_worker_get_current ();
1224 vcl_session_t *session = 0;
1225
1226 session = vcl_session_get_w_handle (wrk, session_handle);
1227 if (!session)
1228 return VPPCOM_EBADFD;
1229
1230 if (cert_len == 0 || cert_len == ~0)
1231 return VPPCOM_EBADFD;
1232
1233 /*
1234 * Send listen request to vpp and wait for reply
1235 */
1236 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1237
1238 return VPPCOM_OK;
1239
1240}
1241
1242int
1243vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1244 uint32_t key_len)
1245{
1246
1247 vcl_worker_t *wrk = vcl_worker_get_current ();
1248 vcl_session_t *session = 0;
1249
1250 session = vcl_session_get_w_handle (wrk, session_handle);
1251 if (!session)
1252 return VPPCOM_EBADFD;
1253
1254 if (key_len == 0 || key_len == ~0)
1255 return VPPCOM_EBADFD;
1256
1257 /*
1258 * Send listen request to vpp and wait for reply
1259 */
1260 vppcom_send_application_tls_key_add (session, key, key_len);
1261
1262 return VPPCOM_OK;
1263
1264
1265}
1266
Florin Coras134a9962018-08-28 11:32:04 -07001267static int
Florin Coras5e062572019-03-14 19:07:51 -07001268validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001269{
Florin Coras5e062572019-03-14 19:07:51 -07001270 if (ls->is_vep)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001271 {
Florin Coras5e062572019-03-14 19:07:51 -07001272 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1273 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001274 return VPPCOM_EBADFD;
1275 }
1276
Florin Coras5e062572019-03-14 19:07:51 -07001277 if (ls->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001278 {
Florin Coras5e062572019-03-14 19:07:51 -07001279 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1280 " (%s)", ls->vpp_handle, ls->session_index, ls->session_state,
1281 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001282 return VPPCOM_EBADFD;
1283 }
1284 return VPPCOM_OK;
1285}
1286
1287int
Florin Coras134a9962018-08-28 11:32:04 -07001288vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001289 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001290{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001291 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001292 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001293 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001294 vcl_session_t *listen_session = 0;
1295 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001296 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001297 svm_msg_q_msg_t msg;
1298 session_event_t *e;
1299 u8 is_nonblocking;
1300 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001301
Florin Coras134a9962018-08-28 11:32:04 -07001302 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001303 if (!listen_session)
1304 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001305
Florin Coras134a9962018-08-28 11:32:04 -07001306 listen_session_index = listen_session->session_index;
1307 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001308 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001309
Florin Coras54693d22018-07-17 10:46:29 -07001310 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001311 {
Florin Coras54693d22018-07-17 10:46:29 -07001312 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001313 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001314 accepted_msg = evt->accepted_msg;
1315 goto handle;
1316 }
1317
1318 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1319 VCL_SESS_ATTR_NONBLOCK);
Florin Coras134a9962018-08-28 11:32:04 -07001320 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
Florin Coras54693d22018-07-17 10:46:29 -07001321 return VPPCOM_EAGAIN;
1322
1323 while (1)
1324 {
Florin Coras134a9962018-08-28 11:32:04 -07001325 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001326 return VPPCOM_EAGAIN;
1327
Florin Coras134a9962018-08-28 11:32:04 -07001328 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001329 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001330 {
Florin Coras00cca802019-06-06 09:38:44 -07001331 VDBG (0, "discarded event: %u", e->event_type);
Florin Coras134a9962018-08-28 11:32:04 -07001332 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001333 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001334 }
Dave Barach178cf492018-11-13 16:34:13 -05001335 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001336 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001337 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001338 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001339
Florin Coras54693d22018-07-17 10:46:29 -07001340handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001341
Florin Coras00cca802019-06-06 09:38:44 -07001342 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1343 listen_session_index);
1344 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1345 return VPPCOM_ECONNABORTED;
1346
Florin Coras134a9962018-08-28 11:32:04 -07001347 listen_session = vcl_session_get (wrk, listen_session_index);
1348 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001349
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001350 if (flags & O_NONBLOCK)
1351 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001352
Florin Coras5e062572019-03-14 19:07:51 -07001353 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1354 " flags %d, is_nonblocking %u", listen_session->session_index,
1355 listen_session->vpp_handle, client_session_index,
1356 client_session->vpp_handle, flags,
1357 VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001358
Dave Wallace048b1d62018-01-03 22:24:41 -05001359 if (ep)
1360 {
Florin Coras7e12d942018-06-27 14:32:43 -07001361 ep->is_ip4 = client_session->transport.is_ip4;
1362 ep->port = client_session->transport.rmt_port;
1363 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001364 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1365 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001366 else
Dave Barach178cf492018-11-13 16:34:13 -05001367 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1368 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001369 }
Dave Wallace60caa062017-11-10 17:07:13 -05001370
Florin Coras05ecfcc2018-12-12 18:19:39 -08001371 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001372 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001373 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001374 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001375 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001376 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001377 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001378 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001379 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001380 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1381 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001382
Florin Coras3c7d4f92018-12-14 11:28:43 -08001383 /*
1384 * Session might have been closed already
1385 */
1386 if (accept_flags)
1387 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001388 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -08001389 client_session->session_state = STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001390 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasb0f662f2018-12-27 14:51:46 -08001391 client_session->session_state = STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001392 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001393 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001394}
1395
1396int
Florin Coras134a9962018-08-28 11:32:04 -07001397vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001398{
Florin Coras134a9962018-08-28 11:32:04 -07001399 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001400 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001401 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001402 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001403
Florin Coras134a9962018-08-28 11:32:04 -07001404 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001405 if (!session)
1406 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001407 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001408
1409 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001410 {
Florin Coras5e062572019-03-14 19:07:51 -07001411 VDBG (0, "ERROR: cannot connect epoll session %u!",
1412 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001413 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001414 }
1415
Florin Coras7e12d942018-06-27 14:32:43 -07001416 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001417 {
Florin Coras7baeb712019-01-04 17:05:43 -08001418 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001419 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001420 session_handle, session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001421 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001422 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001423 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001424 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001425 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001426 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001427 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001428 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001429 }
1430
Florin Coras7e12d942018-06-27 14:32:43 -07001431 session->transport.is_ip4 = server_ep->is_ip4;
1432 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001433 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1434 sizeof (ip4_address_t));
Dave Wallaced239f8d2018-06-19 13:37:30 -04001435 else
Dave Barach178cf492018-11-13 16:34:13 -05001436 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1437 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001438 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04001439
Florin Coras7baeb712019-01-04 17:05:43 -08001440 VDBG (0, "session handle %u [0x%llx]: connecting to server %s %U "
1441 "port %d proto %s", session_handle, session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001442 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001443 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001444 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001445 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001446 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001447 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001448
Florin Coras070453d2018-08-24 17:04:27 -07001449 /*
1450 * Send connect request and wait for reply from vpp
1451 */
Florin Coras134a9962018-08-28 11:32:04 -07001452 vppcom_send_connect_sock (session);
Florin Coras070453d2018-08-24 17:04:27 -07001453 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1454 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001455
Florin Coras134a9962018-08-28 11:32:04 -07001456 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001457 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1458 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001459
Dave Wallace4878cbe2017-11-21 03:45:09 -05001460 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001461}
1462
Florin Coras54693d22018-07-17 10:46:29 -07001463static u8
1464vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1465{
Florin Corasc0737e92019-03-04 14:19:39 -08001466 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001467}
1468
Steven58f464e2017-10-25 12:33:12 -07001469static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001470vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001471 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001472{
Florin Coras134a9962018-08-28 11:32:04 -07001473 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001474 int n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001475 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001476 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001477 svm_msg_q_msg_t msg;
1478 session_event_t *e;
1479 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001480 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001481
Florin Coras070453d2018-08-24 17:04:27 -07001482 if (PREDICT_FALSE (!buf))
1483 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001484
Florin Coras134a9962018-08-28 11:32:04 -07001485 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001486 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001487 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001488
Florin Coras6d0106e2019-01-29 20:11:58 -08001489 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001490 {
Florin Coras5e062572019-03-14 19:07:51 -07001491 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001492 s->session_index, s->vpp_handle, s->session_state,
1493 vppcom_session_state_str (s->session_state));
1494 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001495 }
1496
Florin Coras2cba8532018-09-11 16:33:36 -07001497 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001498 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001499 mq = wrk->app_event_queue;
1500 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001501 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001502
Sirshak Das28aa5392019-02-05 01:33:33 -06001503 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001504 {
Florin Coras54693d22018-07-17 10:46:29 -07001505 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001506 {
1507 svm_fifo_unset_event (s->rx_fifo);
1508 return VPPCOM_EWOULDBLOCK;
1509 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001510 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001511 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001512 if (vcl_session_is_closing (s))
1513 return vcl_session_closing_error (s);
1514
Florin Corasc0737e92019-03-04 14:19:39 -08001515 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001516 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001517 if (svm_msg_q_is_empty (mq))
1518 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001519
Florin Coras54693d22018-07-17 10:46:29 -07001520 svm_msg_q_sub_w_lock (mq, &msg);
1521 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001522 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001523 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001524 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001525 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001526 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001527 }
Florin Coras54693d22018-07-17 10:46:29 -07001528
Florin Coras460dce62018-07-27 05:45:06 -07001529 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001530 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001531 else
Florin Coras99368312018-08-02 10:45:44 -07001532 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001533
Sirshak Das28aa5392019-02-05 01:33:33 -06001534 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001535 svm_fifo_unset_event (s->rx_fifo);
Florin Coras41c9e042018-09-11 00:10:41 -07001536
Florin Coras317b8e02019-04-17 09:57:46 -07001537 /* Cut-through sessions might request tx notifications on rx fifos */
Florin Coras2d379d82019-06-28 12:45:12 -07001538 if (PREDICT_FALSE (rx_fifo->want_deq_ntf))
Florin Coras317b8e02019-04-17 09:57:46 -07001539 {
1540 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1541 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras2d379d82019-06-28 12:45:12 -07001542 svm_fifo_reset_has_deq_ntf (s->rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001543 }
1544
Florin Coras5e062572019-03-14 19:07:51 -07001545 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1546 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001547
Florin Coras54693d22018-07-17 10:46:29 -07001548 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001549}
1550
Steven58f464e2017-10-25 12:33:12 -07001551int
Florin Coras134a9962018-08-28 11:32:04 -07001552vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001553{
Florin Coras134a9962018-08-28 11:32:04 -07001554 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001555}
1556
1557static int
Florin Coras134a9962018-08-28 11:32:04 -07001558vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001559{
Florin Coras134a9962018-08-28 11:32:04 -07001560 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001561}
1562
Florin Coras2cba8532018-09-11 16:33:36 -07001563int
1564vppcom_session_read_segments (uint32_t session_handle,
1565 vppcom_data_segments_t ds)
1566{
1567 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001568 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001569 vcl_session_t *s = 0;
1570 svm_fifo_t *rx_fifo;
1571 svm_msg_q_msg_t msg;
1572 session_event_t *e;
1573 svm_msg_q_t *mq;
1574 u8 is_ct;
1575
1576 s = vcl_session_get_w_handle (wrk, session_handle);
1577 if (PREDICT_FALSE (!s || s->is_vep))
1578 return VPPCOM_EBADFD;
1579
Florin Coras6d0106e2019-01-29 20:11:58 -08001580 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1581 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001582
1583 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1584 is_ct = vcl_session_is_ct (s);
1585 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1586 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001587 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001588
Florin Coras653e43f2019-03-04 10:56:23 -08001589 if (is_ct)
1590 svm_fifo_unset_event (s->rx_fifo);
1591
Sirshak Das28aa5392019-02-05 01:33:33 -06001592 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001593 {
1594 if (is_nonblocking)
1595 {
1596 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001597 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001598 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001599 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001600 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001601 if (vcl_session_is_closing (s))
1602 return vcl_session_closing_error (s);
1603
Florin Coras2cba8532018-09-11 16:33:36 -07001604 svm_fifo_unset_event (rx_fifo);
1605 svm_msg_q_lock (mq);
1606 if (svm_msg_q_is_empty (mq))
1607 svm_msg_q_wait (mq);
1608
1609 svm_msg_q_sub_w_lock (mq, &msg);
1610 e = svm_msg_q_msg_data (mq, &msg);
1611 svm_msg_q_unlock (mq);
1612 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001613 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001614 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001615 }
1616 }
1617
Florin Coras88001c62019-04-24 14:44:46 -07001618 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001619 svm_fifo_unset_event (rx_fifo);
1620
Florin Coras2cba8532018-09-11 16:33:36 -07001621 return n_read;
1622}
1623
1624void
1625vppcom_session_free_segments (uint32_t session_handle,
1626 vppcom_data_segments_t ds)
1627{
1628 vcl_worker_t *wrk = vcl_worker_get_current ();
1629 vcl_session_t *s;
1630
1631 s = vcl_session_get_w_handle (wrk, session_handle);
1632 if (PREDICT_FALSE (!s || s->is_vep))
1633 return;
1634
Florin Coras88001c62019-04-24 14:44:46 -07001635 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001636}
1637
Florin Coras2cba8532018-09-11 16:33:36 -07001638int
1639vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1640{
1641 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001642 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001643 if (first_copy < max_bytes)
1644 {
Dave Barach178cf492018-11-13 16:34:13 -05001645 clib_memcpy_fast (buf + first_copy, ds[1].data,
1646 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001647 }
1648 return 0;
1649}
1650
Florin Coras54693d22018-07-17 10:46:29 -07001651static u8
1652vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1653{
Florin Corasc0737e92019-03-04 14:19:39 -08001654 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001655}
1656
Florin Coras42ceddb2018-12-12 10:56:01 -08001657static inline int
1658vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1659 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001660{
Florin Coras134a9962018-08-28 11:32:04 -07001661 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001662 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001663 vcl_session_t *s = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001664 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001665 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08001666 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001667 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001668 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001669 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001670
Florin Coras070453d2018-08-24 17:04:27 -07001671 if (PREDICT_FALSE (!buf))
1672 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001673
Florin Coras134a9962018-08-28 11:32:04 -07001674 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001675 if (PREDICT_FALSE (!s))
1676 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001677
Florin Coras460dce62018-07-27 05:45:06 -07001678 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001679 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001680 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1681 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001682 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001683 }
1684
Florin Coras6d0106e2019-01-29 20:11:58 -08001685 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001686 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001687 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1688 s->session_index, s->vpp_handle, s->session_state,
1689 vppcom_session_state_str (s->session_state));
1690 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001691 }
1692
Florin Coras0e88e852018-09-17 22:09:02 -07001693 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001694 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07001695 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06001696
Florin Coras653e43f2019-03-04 10:56:23 -08001697 mq = wrk->app_event_queue;
Sirshak Das28aa5392019-02-05 01:33:33 -06001698 if (svm_fifo_is_full_prod (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001699 {
Florin Coras54693d22018-07-17 10:46:29 -07001700 if (is_nonblocking)
1701 {
Florin Coras070453d2018-08-24 17:04:27 -07001702 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001703 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001704 while (svm_fifo_is_full_prod (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001705 {
Florin Coras2d379d82019-06-28 12:45:12 -07001706 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001707 if (vcl_session_is_closing (s))
1708 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001709 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001710 if (svm_msg_q_is_empty (mq))
1711 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001712
Florin Coras54693d22018-07-17 10:46:29 -07001713 svm_msg_q_sub_w_lock (mq, &msg);
1714 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001715 svm_msg_q_unlock (mq);
1716
Florin Coras0e88e852018-09-17 22:09:02 -07001717 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001718 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001719 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001720 }
Dave Wallace543852a2017-08-03 02:11:34 -04001721 }
Dave Wallace543852a2017-08-03 02:11:34 -04001722
Florin Coras653e43f2019-03-04 10:56:23 -08001723 et = SESSION_IO_EVT_TX;
1724 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08001725 et = SESSION_IO_EVT_TX_FLUSH;
1726
Florin Coras460dce62018-07-27 05:45:06 -07001727 if (s->is_dgram)
1728 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08001729 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001730 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07001731 else
1732 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001733 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08001734
Florin Coras14ed6df2019-03-06 21:13:42 -08001735 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001736 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1737 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001738
Florin Coras460dce62018-07-27 05:45:06 -07001739 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001740
Florin Coras6d0106e2019-01-29 20:11:58 -08001741 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1742 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001743
Florin Coras54693d22018-07-17 10:46:29 -07001744 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001745}
1746
Florin Coras42ceddb2018-12-12 10:56:01 -08001747int
1748vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1749{
1750 return vppcom_session_write_inline (session_handle, buf, n,
1751 0 /* is_flush */ );
1752}
1753
Florin Corasb0f662f2018-12-27 14:51:46 -08001754int
1755vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1756{
1757 return vppcom_session_write_inline (session_handle, buf, n,
1758 1 /* is_flush */ );
1759}
1760
Florin Corasc0737e92019-03-04 14:19:39 -08001761#define vcl_fifo_rx_evt_valid_or_break(_s) \
1762if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1763 { \
1764 if (!vcl_session_is_ct (_s)) \
1765 { \
1766 svm_fifo_unset_event (_s->rx_fifo); \
1767 if (svm_fifo_is_empty (_s->rx_fifo)) \
1768 break; \
1769 } \
1770 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1771 { \
1772 svm_fifo_unset_event (_s->ct_rx_fifo); \
1773 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1774 break; \
1775 } \
1776 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07001777
Florin Coras86f04502018-09-12 16:08:01 -07001778static void
1779vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1780 unsigned long n_bits, unsigned long *read_map,
1781 unsigned long *write_map,
1782 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001783{
1784 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001785 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001786 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001787 u32 sid;
1788
1789 switch (e->event_type)
1790 {
Florin Corasc0737e92019-03-04 14:19:39 -08001791 case SESSION_IO_EVT_RX:
1792 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001793 session = vcl_session_get (wrk, sid);
1794 if (!session)
1795 break;
Florin Corasfff68f72019-03-13 16:01:38 -07001796 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07001797 if (sid < n_bits && read_map)
1798 {
David Johnsond9818dd2018-12-14 14:53:41 -05001799 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001800 *bits_set += 1;
1801 }
1802 break;
Florin Corasfe97da32019-03-06 10:09:04 -08001803 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08001804 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001805 session = vcl_session_get (wrk, sid);
1806 if (!session)
1807 break;
1808 if (sid < n_bits && write_map)
1809 {
David Johnsond9818dd2018-12-14 14:53:41 -05001810 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001811 *bits_set += 1;
1812 }
1813 break;
Florin Coras86f04502018-09-12 16:08:01 -07001814 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001815 session = vcl_session_accepted (wrk,
1816 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001817 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001818 break;
Florin Coras86f04502018-09-12 16:08:01 -07001819 sid = session->session_index;
1820 if (sid < n_bits && read_map)
1821 {
David Johnsond9818dd2018-12-14 14:53:41 -05001822 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001823 *bits_set += 1;
1824 }
1825 break;
1826 case SESSION_CTRL_EVT_CONNECTED:
1827 connected_msg = (session_connected_msg_t *) e->data;
1828 vcl_session_connected_handler (wrk, connected_msg);
1829 break;
1830 case SESSION_CTRL_EVT_DISCONNECTED:
1831 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001832 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1833 if (!session)
1834 break;
1835 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001836 if (sid < n_bits && except_map)
1837 {
David Johnsond9818dd2018-12-14 14:53:41 -05001838 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001839 *bits_set += 1;
1840 }
1841 break;
1842 case SESSION_CTRL_EVT_RESET:
1843 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1844 if (sid < n_bits && except_map)
1845 {
David Johnsond9818dd2018-12-14 14:53:41 -05001846 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001847 *bits_set += 1;
1848 }
1849 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001850 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1851 vcl_session_unlisten_reply_handler (wrk, e->data);
1852 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001853 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1854 vcl_session_worker_update_reply_handler (wrk, e->data);
1855 break;
1856 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1857 vcl_session_req_worker_update_handler (wrk, e->data);
1858 break;
Florin Coras86f04502018-09-12 16:08:01 -07001859 default:
1860 clib_warning ("unhandled: %u", e->event_type);
1861 break;
1862 }
1863}
1864
1865static int
1866vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1867 unsigned long n_bits, unsigned long *read_map,
1868 unsigned long *write_map, unsigned long *except_map,
1869 double time_to_wait, u32 * bits_set)
1870{
Florin Coras99368312018-08-02 10:45:44 -07001871 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001872 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001873 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001874
1875 svm_msg_q_lock (mq);
1876 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001877 {
Florin Coras54693d22018-07-17 10:46:29 -07001878 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001879 {
Florin Coras54693d22018-07-17 10:46:29 -07001880 svm_msg_q_unlock (mq);
1881 return 0;
1882 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001883
Florin Coras54693d22018-07-17 10:46:29 -07001884 if (!time_to_wait)
1885 {
1886 svm_msg_q_unlock (mq);
1887 return 0;
1888 }
1889 else if (time_to_wait < 0)
1890 {
1891 svm_msg_q_wait (mq);
1892 }
1893 else
1894 {
1895 if (svm_msg_q_timedwait (mq, time_to_wait))
1896 {
1897 svm_msg_q_unlock (mq);
1898 return 0;
1899 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001900 }
1901 }
Florin Corase003a1b2019-06-05 10:47:16 -07001902 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07001903 svm_msg_q_unlock (mq);
1904
Florin Coras134a9962018-08-28 11:32:04 -07001905 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001906 {
Florin Coras134a9962018-08-28 11:32:04 -07001907 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07001908 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07001909 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1910 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001911 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001912 }
Florin Coras134a9962018-08-28 11:32:04 -07001913 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08001914 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001915 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001916}
1917
Florin Coras99368312018-08-02 10:45:44 -07001918static int
Florin Coras294afe22019-01-07 17:49:17 -08001919vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
1920 vcl_si_set * read_map, vcl_si_set * write_map,
1921 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001922 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001923{
Florin Coras14ed6df2019-03-06 21:13:42 -08001924 double wait = 0, start = 0;
1925
1926 if (!*bits_set)
1927 {
1928 wait = time_to_wait;
1929 start = clib_time_now (&wrk->clib_time);
1930 }
1931
1932 do
1933 {
1934 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1935 write_map, except_map, wait, bits_set);
1936 if (*bits_set)
1937 return *bits_set;
1938 if (wait == -1)
1939 continue;
1940
1941 wait = wait - (clib_time_now (&wrk->clib_time) - start);
1942 }
1943 while (wait > 0);
1944
1945 return 0;
Florin Coras99368312018-08-02 10:45:44 -07001946}
1947
1948static int
Florin Coras294afe22019-01-07 17:49:17 -08001949vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
1950 vcl_si_set * read_map, vcl_si_set * write_map,
1951 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001952 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001953{
1954 vcl_mq_evt_conn_t *mqc;
1955 int __clib_unused n_read;
1956 int n_mq_evts, i;
1957 u64 buf;
1958
Florin Coras134a9962018-08-28 11:32:04 -07001959 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1960 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1961 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07001962 for (i = 0; i < n_mq_evts; i++)
1963 {
Florin Coras134a9962018-08-28 11:32:04 -07001964 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07001965 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07001966 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07001967 except_map, 0, bits_set);
1968 }
1969
1970 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1971}
1972
Dave Wallace543852a2017-08-03 02:11:34 -04001973int
Florin Coras294afe22019-01-07 17:49:17 -08001974vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1975 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04001976{
Florin Coras54693d22018-07-17 10:46:29 -07001977 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001978 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001979 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07001980 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04001981
Dave Wallace7876d392017-10-19 03:53:57 -04001982 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001983 {
Florin Coras134a9962018-08-28 11:32:04 -07001984 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001985 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08001986 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
1987 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001988 }
Dave Wallace7876d392017-10-19 03:53:57 -04001989 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001990 {
Florin Coras134a9962018-08-28 11:32:04 -07001991 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001992 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08001993 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
1994 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001995 }
Dave Wallace7876d392017-10-19 03:53:57 -04001996 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001997 {
Florin Coras134a9962018-08-28 11:32:04 -07001998 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001999 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002000 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2001 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002002 }
2003
Florin Coras54693d22018-07-17 10:46:29 -07002004 if (!n_bits)
2005 return 0;
2006
2007 if (!write_map)
2008 goto check_rd;
2009
2010 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002011 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2012 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002013 {
Florin Coras47c40e22018-11-26 17:01:36 -08002014 if (except_map && sid < minbits)
2015 clib_bitmap_set_no_check (except_map, sid, 1);
2016 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002017 }
2018
Sirshak Das28aa5392019-02-05 01:33:33 -06002019 rv = svm_fifo_is_full_prod (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002020 if (!rv)
2021 {
David Johnsond9818dd2018-12-14 14:53:41 -05002022 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002023 bits_set++;
2024 }
Florin Coras294afe22019-01-07 17:49:17 -08002025 else
Florin Coras2d379d82019-06-28 12:45:12 -07002026 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002027 }));
2028
2029check_rd:
2030 if (!read_map)
2031 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002032
Florin Coras134a9962018-08-28 11:32:04 -07002033 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2034 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002035 {
Florin Coras47c40e22018-11-26 17:01:36 -08002036 if (except_map && sid < minbits)
2037 clib_bitmap_set_no_check (except_map, sid, 1);
2038 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002039 }
2040
Florin Coras0ef8ef22019-01-18 08:37:13 -08002041 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002042 if (rv)
2043 {
David Johnsond9818dd2018-12-14 14:53:41 -05002044 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002045 bits_set++;
2046 }
2047 }));
2048 /* *INDENT-ON* */
2049
2050check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002051
Florin Coras86f04502018-09-12 16:08:01 -07002052 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2053 {
2054 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2055 read_map, write_map, except_map, &bits_set);
2056 }
2057 vec_reset_length (wrk->unhandled_evts_vector);
2058
Florin Coras99368312018-08-02 10:45:44 -07002059 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002060 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002061 time_to_wait, &bits_set);
2062 else
Florin Coras134a9962018-08-28 11:32:04 -07002063 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002064 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002065
Dave Wallace543852a2017-08-03 02:11:34 -04002066 return (bits_set);
2067}
2068
Dave Wallacef7f809c2017-10-03 01:48:42 -04002069static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002070vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002071{
Florin Coras7e12d942018-06-27 14:32:43 -07002072 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002073 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002074 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002075
Florin Corasc0737e92019-03-04 14:19:39 -08002076 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002077 return;
2078
Florin Coras134a9962018-08-28 11:32:04 -07002079 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002080 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002081 {
Florin Coras5e062572019-03-14 19:07:51 -07002082 VDBG (0, "ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002083 goto done;
2084 }
2085 if (PREDICT_FALSE (!session->is_vep))
2086 {
Florin Coras5e062572019-03-14 19:07:51 -07002087 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002088 goto done;
2089 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002090 vep = &session->vep;
Florin Coras5e062572019-03-14 19:07:51 -07002091 VDBG (0, "vep_idx (%u): Dumping epoll chain\n"
2092 "{\n"
2093 " is_vep = %u\n"
2094 " is_vep_session = %u\n"
2095 " next_sid = 0x%x (%u)\n"
2096 "}\n", vep_idx, session->is_vep, session->is_vep_session,
2097 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002098
Florin Coras134a9962018-08-28 11:32:04 -07002099 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002100 {
Florin Coras134a9962018-08-28 11:32:04 -07002101 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002102 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002103 {
Florin Coras5e062572019-03-14 19:07:51 -07002104 VDBG (0, "ERROR: Invalid sid (%u)!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002105 goto done;
2106 }
2107 if (PREDICT_FALSE (session->is_vep))
Florin Coras5e062572019-03-14 19:07:51 -07002108 {
2109 VDBG (0, "ERROR: sid (%u) is a vep!", vep_idx);
2110 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002111 else if (PREDICT_FALSE (!session->is_vep_session))
2112 {
Florin Coras5e062572019-03-14 19:07:51 -07002113 VDBG (0, "ERROR: session (%u) is not a vep session!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002114 goto done;
2115 }
2116 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002117 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Florin Coras5e062572019-03-14 19:07:51 -07002118 VDBG (0, "ERROR: session (%u) vep_idx (%u) != vep_idx (%u)!",
2119 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002120 if (session->is_vep_session)
2121 {
Florin Coras5e062572019-03-14 19:07:51 -07002122 VDBG (0, "vep_idx[%u]: sid 0x%x (%u)\n"
2123 "{\n"
2124 " next_sid = 0x%x (%u)\n"
2125 " prev_sid = 0x%x (%u)\n"
2126 " vep_idx = 0x%x (%u)\n"
2127 " ev.events = 0x%x\n"
2128 " ev.data.u64 = 0x%llx\n"
2129 " et_mask = 0x%x\n"
2130 "}\n",
2131 vep_idx, sid, sid, vep->next_sh, vep->next_sh, vep->prev_sh,
2132 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2133 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002134 }
2135 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002136
2137done:
Florin Coras5e062572019-03-14 19:07:51 -07002138 VDBG (0, "vep_idx (%u): Dump complete!\n", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002139}
2140
2141int
2142vppcom_epoll_create (void)
2143{
Florin Coras134a9962018-08-28 11:32:04 -07002144 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002145 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002146
Florin Coras134a9962018-08-28 11:32:04 -07002147 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002148
2149 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002150 vep_session->vep.vep_sh = ~0;
2151 vep_session->vep.next_sh = ~0;
2152 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002153 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002154
Florin Corasa7a1a222018-12-30 17:11:31 -08002155 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2156 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002157
Florin Corasab2f6db2018-08-31 14:31:41 -07002158 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002159}
2160
2161int
Florin Coras134a9962018-08-28 11:32:04 -07002162vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002163 struct epoll_event *event)
2164{
Florin Coras134a9962018-08-28 11:32:04 -07002165 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002166 vcl_session_t *vep_session;
2167 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002168 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002169
Florin Coras134a9962018-08-28 11:32:04 -07002170 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002171 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002172 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002173 return VPPCOM_EINVAL;
2174 }
2175
Florin Coras134a9962018-08-28 11:32:04 -07002176 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002177 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002178 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002179 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002180 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002181 }
2182 if (PREDICT_FALSE (!vep_session->is_vep))
2183 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002184 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002185 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002186 }
2187
Florin Coras134a9962018-08-28 11:32:04 -07002188 ASSERT (vep_session->vep.vep_sh == ~0);
2189 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002190
Florin Coras134a9962018-08-28 11:32:04 -07002191 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002192 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002193 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002194 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002195 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002196 }
2197 if (PREDICT_FALSE (session->is_vep))
2198 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002199 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002200 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002201 }
2202
2203 switch (op)
2204 {
2205 case EPOLL_CTL_ADD:
2206 if (PREDICT_FALSE (!event))
2207 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002208 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002209 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002210 }
Florin Coras134a9962018-08-28 11:32:04 -07002211 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002212 {
Florin Coras7e12d942018-06-27 14:32:43 -07002213 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002214 next_session = vcl_session_get_w_handle (wrk,
2215 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002216 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002217 {
Florin Coras5e062572019-03-14 19:07:51 -07002218 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002219 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002220 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002221 }
Florin Coras134a9962018-08-28 11:32:04 -07002222 ASSERT (next_session->vep.prev_sh == vep_handle);
2223 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002224 }
Florin Coras134a9962018-08-28 11:32:04 -07002225 session->vep.next_sh = vep_session->vep.next_sh;
2226 session->vep.prev_sh = vep_handle;
2227 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002228 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2229 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002230 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002231 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002232 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002233
Florin Coras1bcad5c2019-01-09 20:04:38 -08002234 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002235 svm_fifo_add_want_deq_ntf (session->tx_fifo,
2236 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002237
Florin Corasa7a1a222018-12-30 17:11:31 -08002238 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2239 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002240 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002241 break;
2242
2243 case EPOLL_CTL_MOD:
2244 if (PREDICT_FALSE (!event))
2245 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002246 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002247 rv = VPPCOM_EINVAL;
2248 goto done;
2249 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002250 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002251 {
Florin Coras5e062572019-03-14 19:07:51 -07002252 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002253 rv = VPPCOM_EINVAL;
2254 goto done;
2255 }
Florin Coras134a9962018-08-28 11:32:04 -07002256 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002257 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002258 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2259 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002260 rv = VPPCOM_EINVAL;
2261 goto done;
2262 }
2263 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2264 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002265 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2266 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002267 break;
2268
2269 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002270 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002271 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002272 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002273 rv = VPPCOM_EINVAL;
2274 goto done;
2275 }
Florin Coras134a9962018-08-28 11:32:04 -07002276 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002277 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002278 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2279 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002280 rv = VPPCOM_EINVAL;
2281 goto done;
2282 }
2283
Florin Coras134a9962018-08-28 11:32:04 -07002284 if (session->vep.prev_sh == vep_handle)
2285 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002286 else
2287 {
Florin Coras7e12d942018-06-27 14:32:43 -07002288 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002289 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002290 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002291 {
Florin Coras5e062572019-03-14 19:07:51 -07002292 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002293 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002294 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002295 }
Florin Coras134a9962018-08-28 11:32:04 -07002296 ASSERT (prev_session->vep.next_sh == session_handle);
2297 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002298 }
Florin Coras134a9962018-08-28 11:32:04 -07002299 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002300 {
Florin Coras7e12d942018-06-27 14:32:43 -07002301 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002302 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002303 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002304 {
Florin Coras5e062572019-03-14 19:07:51 -07002305 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002306 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002307 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002308 }
Florin Coras134a9962018-08-28 11:32:04 -07002309 ASSERT (next_session->vep.prev_sh == session_handle);
2310 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002311 }
2312
2313 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002314 session->vep.next_sh = ~0;
2315 session->vep.prev_sh = ~0;
2316 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002317 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002318
2319 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002320 svm_fifo_del_want_deq_ntf (session->tx_fifo, SVM_FIFO_NO_DEQ_NOTIF);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002321
Florin Coras5e062572019-03-14 19:07:51 -07002322 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002323 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002324 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002325 break;
2326
2327 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002328 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002329 rv = VPPCOM_EINVAL;
2330 }
2331
Florin Coras134a9962018-08-28 11:32:04 -07002332 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002333
2334done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002335 return rv;
2336}
2337
Florin Coras86f04502018-09-12 16:08:01 -07002338static inline void
2339vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2340 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002341{
2342 session_disconnected_msg_t *disconnected_msg;
2343 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002344 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002345 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002346 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002347 u8 add_event = 0;
2348
2349 switch (e->event_type)
2350 {
Florin Coras653e43f2019-03-04 10:56:23 -08002351 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002352 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002353 if (!(session = vcl_session_get (wrk, sid)))
2354 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002355 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002356 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002357 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002358 break;
2359 add_event = 1;
2360 events[*num_ev].events |= EPOLLIN;
2361 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002362 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002363 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002364 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002365 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002366 if (!(session = vcl_session_get (wrk, sid)))
2367 break;
Florin Coras86f04502018-09-12 16:08:01 -07002368 session_events = session->vep.ev.events;
2369 if (!(EPOLLOUT & session_events))
2370 break;
2371 add_event = 1;
2372 events[*num_ev].events |= EPOLLOUT;
2373 session_evt_data = session->vep.ev.data.u64;
Florin Coras2d379d82019-06-28 12:45:12 -07002374 svm_fifo_reset_has_deq_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002375 break;
Florin Coras86f04502018-09-12 16:08:01 -07002376 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002377 session = vcl_session_accepted (wrk,
2378 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002379 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002380 break;
Florin Coras86f04502018-09-12 16:08:01 -07002381
Florin Coras86f04502018-09-12 16:08:01 -07002382 session_events = session->vep.ev.events;
2383 if (!(EPOLLIN & session_events))
2384 break;
2385
2386 add_event = 1;
2387 events[*num_ev].events |= EPOLLIN;
2388 session_evt_data = session->vep.ev.data.u64;
2389 break;
2390 case SESSION_CTRL_EVT_CONNECTED:
2391 connected_msg = (session_connected_msg_t *) e->data;
2392 vcl_session_connected_handler (wrk, connected_msg);
2393 /* Generate EPOLLOUT because there's no connected event */
2394 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002395 if (!(session = vcl_session_get (wrk, sid)))
2396 break;
Florin Coras86f04502018-09-12 16:08:01 -07002397 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002398 if (!(EPOLLOUT & session_events))
2399 break;
2400 add_event = 1;
2401 events[*num_ev].events |= EPOLLOUT;
2402 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002403 break;
2404 case SESSION_CTRL_EVT_DISCONNECTED:
2405 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002406 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2407 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002408 break;
Florin Coras72f77822019-01-22 19:05:52 -08002409 session_events = session->vep.ev.events;
2410 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2411 break;
Florin Coras86f04502018-09-12 16:08:01 -07002412 add_event = 1;
2413 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2414 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002415 break;
2416 case SESSION_CTRL_EVT_RESET:
2417 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2418 if (!(session = vcl_session_get (wrk, sid)))
2419 break;
Florin Coras72f77822019-01-22 19:05:52 -08002420 session_events = session->vep.ev.events;
2421 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2422 break;
Florin Coras86f04502018-09-12 16:08:01 -07002423 add_event = 1;
2424 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2425 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002426 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002427 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2428 vcl_session_unlisten_reply_handler (wrk, e->data);
2429 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002430 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2431 vcl_session_req_worker_update_handler (wrk, e->data);
2432 break;
2433 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2434 vcl_session_worker_update_reply_handler (wrk, e->data);
2435 break;
Florin Coras86f04502018-09-12 16:08:01 -07002436 default:
2437 VDBG (0, "unhandled: %u", e->event_type);
2438 break;
2439 }
2440
2441 if (add_event)
2442 {
2443 events[*num_ev].data.u64 = session_evt_data;
2444 if (EPOLLONESHOT & session_events)
2445 {
2446 session = vcl_session_get (wrk, sid);
2447 session->vep.ev.events = 0;
2448 }
2449 *num_ev += 1;
2450 }
2451}
2452
2453static int
2454vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2455 struct epoll_event *events, u32 maxevents,
2456 double wait_for_time, u32 * num_ev)
2457{
Florin Coras99368312018-08-02 10:45:44 -07002458 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002459 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002460 int i;
2461
Florin Coras539663c2018-09-28 14:59:37 -07002462 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2463 goto handle_dequeued;
2464
Florin Coras54693d22018-07-17 10:46:29 -07002465 svm_msg_q_lock (mq);
2466 if (svm_msg_q_is_empty (mq))
2467 {
2468 if (!wait_for_time)
2469 {
2470 svm_msg_q_unlock (mq);
2471 return 0;
2472 }
2473 else if (wait_for_time < 0)
2474 {
2475 svm_msg_q_wait (mq);
2476 }
2477 else
2478 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002479 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002480 {
2481 svm_msg_q_unlock (mq);
2482 return 0;
2483 }
2484 }
2485 }
Florin Corase003a1b2019-06-05 10:47:16 -07002486 ASSERT (maxevents > *num_ev);
2487 vcl_mq_dequeue_batch (wrk, mq, maxevents - *num_ev);
Florin Coras54693d22018-07-17 10:46:29 -07002488 svm_msg_q_unlock (mq);
2489
Florin Coras539663c2018-09-28 14:59:37 -07002490handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002491 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002492 {
Florin Coras134a9962018-08-28 11:32:04 -07002493 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002494 e = svm_msg_q_msg_data (mq, msg);
Florin Corase003a1b2019-06-05 10:47:16 -07002495 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
Florin Coras99368312018-08-02 10:45:44 -07002496 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002497 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002498 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002499 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002500 return *num_ev;
2501}
2502
Florin Coras99368312018-08-02 10:45:44 -07002503static int
Florin Coras134a9962018-08-28 11:32:04 -07002504vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002505 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002506{
Florin Corase003a1b2019-06-05 10:47:16 -07002507 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002508
2509 if (!n_evts)
2510 {
2511 wait = wait_for_time;
2512 start = clib_time_now (&wrk->clib_time);
2513 }
2514
2515 do
2516 {
2517 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2518 wait, &n_evts);
2519 if (n_evts)
2520 return n_evts;
2521 if (wait == -1)
2522 continue;
2523
Florin Corase003a1b2019-06-05 10:47:16 -07002524 now = clib_time_now (&wrk->clib_time);
2525 wait -= now - start;
2526 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002527 }
2528 while (wait > 0);
2529
2530 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002531}
2532
2533static int
Florin Coras134a9962018-08-28 11:32:04 -07002534vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002535 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002536{
2537 vcl_mq_evt_conn_t *mqc;
2538 int __clib_unused n_read;
2539 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002540 u64 buf;
2541
Florin Coras134a9962018-08-28 11:32:04 -07002542 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002543again:
Florin Coras134a9962018-08-28 11:32:04 -07002544 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2545 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002546 for (i = 0; i < n_mq_evts; i++)
2547 {
Florin Coras134a9962018-08-28 11:32:04 -07002548 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002549 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002550 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002551 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002552 if (!n_evts && n_mq_evts > 0)
2553 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002554
2555 return (int) n_evts;
2556}
2557
Dave Wallacef7f809c2017-10-03 01:48:42 -04002558int
Florin Coras134a9962018-08-28 11:32:04 -07002559vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002560 int maxevents, double wait_for_time)
2561{
Florin Coras134a9962018-08-28 11:32:04 -07002562 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002563 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002564 u32 n_evts = 0;
2565 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002566
2567 if (PREDICT_FALSE (maxevents <= 0))
2568 {
Florin Coras5e062572019-03-14 19:07:51 -07002569 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002570 return VPPCOM_EINVAL;
2571 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002572
Florin Coras134a9962018-08-28 11:32:04 -07002573 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002574 if (!vep_session)
2575 return VPPCOM_EBADFD;
2576
Florin Coras54693d22018-07-17 10:46:29 -07002577 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002578 {
Florin Coras5e062572019-03-14 19:07:51 -07002579 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002580 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002581 }
Florin Coras54693d22018-07-17 10:46:29 -07002582
2583 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002584
Florin Coras86f04502018-09-12 16:08:01 -07002585 if (vec_len (wrk->unhandled_evts_vector))
2586 {
2587 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2588 {
2589 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2590 events, &n_evts);
2591 if (n_evts == maxevents)
2592 {
Florin Corase003a1b2019-06-05 10:47:16 -07002593 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
2594 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07002595 }
2596 }
Florin Corase003a1b2019-06-05 10:47:16 -07002597 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07002598 }
2599
2600 if (vcm->cfg.use_mq_eventfd)
2601 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2602 wait_for_time);
2603
2604 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2605 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002606}
2607
Dave Wallace35830af2017-10-09 01:43:42 -04002608int
Florin Coras134a9962018-08-28 11:32:04 -07002609vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002610 void *buffer, uint32_t * buflen)
2611{
Florin Coras134a9962018-08-28 11:32:04 -07002612 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002613 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002614 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002615 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002616 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002617
Florin Coras134a9962018-08-28 11:32:04 -07002618 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002619 if (!session)
2620 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002621
Dave Wallace35830af2017-10-09 01:43:42 -04002622 switch (op)
2623 {
2624 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002625 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002626 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2627 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002628 break;
2629
Dave Wallace227867f2017-11-13 21:21:53 -05002630 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002631 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002632 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2633 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002634 break;
2635
2636 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002637 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002638 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002639 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2640 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002641 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07002642 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2643 "is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002644 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002645 }
2646 else
2647 rv = VPPCOM_EINVAL;
2648 break;
2649
2650 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002651 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002652 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002653 if (*flags & O_NONBLOCK)
2654 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2655 else
2656 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2657
Florin Coras5e062572019-03-14 19:07:51 -07002658 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2659 " is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002660 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002661 }
2662 else
2663 rv = VPPCOM_EINVAL;
2664 break;
2665
2666 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002667 if (PREDICT_TRUE (buffer && buflen &&
2668 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002669 {
Florin Coras7e12d942018-06-27 14:32:43 -07002670 ep->is_ip4 = session->transport.is_ip4;
2671 ep->port = session->transport.rmt_port;
2672 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002673 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2674 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002675 else
Dave Barach178cf492018-11-13 16:34:13 -05002676 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2677 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002678 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002679 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2680 "addr = %U, port %u", session_handle, ep->is_ip4,
2681 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002682 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2683 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002684 }
2685 else
2686 rv = VPPCOM_EINVAL;
2687 break;
2688
2689 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002690 if (PREDICT_TRUE (buffer && buflen &&
2691 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002692 {
Florin Coras7e12d942018-06-27 14:32:43 -07002693 ep->is_ip4 = session->transport.is_ip4;
2694 ep->port = session->transport.lcl_port;
2695 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002696 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2697 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002698 else
Dave Barach178cf492018-11-13 16:34:13 -05002699 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2700 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002701 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002702 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2703 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002704 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002705 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2706 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002707 }
2708 else
2709 rv = VPPCOM_EINVAL;
2710 break;
Stevenb5a11602017-10-11 09:59:30 -07002711
Dave Wallace048b1d62018-01-03 22:24:41 -05002712 case VPPCOM_ATTR_GET_LIBC_EPFD:
2713 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07002714 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002715 break;
2716
2717 case VPPCOM_ATTR_SET_LIBC_EPFD:
2718 if (PREDICT_TRUE (buffer && buflen &&
2719 (*buflen == sizeof (session->libc_epfd))))
2720 {
2721 session->libc_epfd = *(int *) buffer;
2722 *buflen = sizeof (session->libc_epfd);
2723
Florin Coras5e062572019-03-14 19:07:51 -07002724 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2725 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002726 }
2727 else
2728 rv = VPPCOM_EINVAL;
2729 break;
2730
2731 case VPPCOM_ATTR_GET_PROTOCOL:
2732 if (buffer && buflen && (*buflen >= sizeof (int)))
2733 {
Florin Coras7e12d942018-06-27 14:32:43 -07002734 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002735 *buflen = sizeof (int);
2736
Florin Coras5e062572019-03-14 19:07:51 -07002737 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2738 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002739 }
2740 else
2741 rv = VPPCOM_EINVAL;
2742 break;
2743
2744 case VPPCOM_ATTR_GET_LISTEN:
2745 if (buffer && buflen && (*buflen >= sizeof (int)))
2746 {
2747 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2748 VCL_SESS_ATTR_LISTEN);
2749 *buflen = sizeof (int);
2750
Florin Coras5e062572019-03-14 19:07:51 -07002751 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
2752 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002753 }
2754 else
2755 rv = VPPCOM_EINVAL;
2756 break;
2757
2758 case VPPCOM_ATTR_GET_ERROR:
2759 if (buffer && buflen && (*buflen >= sizeof (int)))
2760 {
2761 *(int *) buffer = 0;
2762 *buflen = sizeof (int);
2763
Florin Coras5e062572019-03-14 19:07:51 -07002764 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2765 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002766 }
2767 else
2768 rv = VPPCOM_EINVAL;
2769 break;
2770
2771 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2772 if (buffer && buflen && (*buflen >= sizeof (u32)))
2773 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002774
2775 /* VPP-TBD */
2776 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002777 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002778 vcm->cfg.tx_fifo_size);
2779 *buflen = sizeof (u32);
2780
Florin Coras5e062572019-03-14 19:07:51 -07002781 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2782 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
2783 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002784 }
2785 else
2786 rv = VPPCOM_EINVAL;
2787 break;
2788
2789 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2790 if (buffer && buflen && (*buflen == sizeof (u32)))
2791 {
2792 /* VPP-TBD */
2793 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002794 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2795 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2796 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002797 }
2798 else
2799 rv = VPPCOM_EINVAL;
2800 break;
2801
2802 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2803 if (buffer && buflen && (*buflen >= sizeof (u32)))
2804 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002805
2806 /* VPP-TBD */
2807 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002808 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002809 vcm->cfg.rx_fifo_size);
2810 *buflen = sizeof (u32);
2811
Florin Coras5e062572019-03-14 19:07:51 -07002812 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
2813 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002814 }
2815 else
2816 rv = VPPCOM_EINVAL;
2817 break;
2818
2819 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2820 if (buffer && buflen && (*buflen == sizeof (u32)))
2821 {
2822 /* VPP-TBD */
2823 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002824 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
2825 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2826 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002827 }
2828 else
2829 rv = VPPCOM_EINVAL;
2830 break;
2831
2832 case VPPCOM_ATTR_GET_REUSEADDR:
2833 if (buffer && buflen && (*buflen >= sizeof (int)))
2834 {
2835 /* VPP-TBD */
2836 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2837 VCL_SESS_ATTR_REUSEADDR);
2838 *buflen = sizeof (int);
2839
Florin Coras5e062572019-03-14 19:07:51 -07002840 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2841 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002842 }
2843 else
2844 rv = VPPCOM_EINVAL;
2845 break;
2846
Stevenb5a11602017-10-11 09:59:30 -07002847 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002848 if (buffer && buflen && (*buflen == sizeof (int)) &&
2849 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2850 {
2851 /* VPP-TBD */
2852 if (*(int *) buffer)
2853 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2854 else
2855 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2856
Florin Coras5e062572019-03-14 19:07:51 -07002857 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2858 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
2859 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002860 }
2861 else
2862 rv = VPPCOM_EINVAL;
2863 break;
2864
2865 case VPPCOM_ATTR_GET_REUSEPORT:
2866 if (buffer && buflen && (*buflen >= sizeof (int)))
2867 {
2868 /* VPP-TBD */
2869 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2870 VCL_SESS_ATTR_REUSEPORT);
2871 *buflen = sizeof (int);
2872
Florin Coras5e062572019-03-14 19:07:51 -07002873 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2874 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002875 }
2876 else
2877 rv = VPPCOM_EINVAL;
2878 break;
2879
2880 case VPPCOM_ATTR_SET_REUSEPORT:
2881 if (buffer && buflen && (*buflen == sizeof (int)) &&
2882 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2883 {
2884 /* VPP-TBD */
2885 if (*(int *) buffer)
2886 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2887 else
2888 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2889
Florin Coras5e062572019-03-14 19:07:51 -07002890 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2891 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
2892 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002893 }
2894 else
2895 rv = VPPCOM_EINVAL;
2896 break;
2897
2898 case VPPCOM_ATTR_GET_BROADCAST:
2899 if (buffer && buflen && (*buflen >= sizeof (int)))
2900 {
2901 /* VPP-TBD */
2902 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2903 VCL_SESS_ATTR_BROADCAST);
2904 *buflen = sizeof (int);
2905
Florin Coras5e062572019-03-14 19:07:51 -07002906 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
2907 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002908 }
2909 else
2910 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002911 break;
2912
2913 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002914 if (buffer && buflen && (*buflen == sizeof (int)))
2915 {
2916 /* VPP-TBD */
2917 if (*(int *) buffer)
2918 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2919 else
2920 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2921
Florin Coras5e062572019-03-14 19:07:51 -07002922 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
2923 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
2924 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002925 }
2926 else
2927 rv = VPPCOM_EINVAL;
2928 break;
2929
2930 case VPPCOM_ATTR_GET_V6ONLY:
2931 if (buffer && buflen && (*buflen >= sizeof (int)))
2932 {
2933 /* VPP-TBD */
2934 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2935 VCL_SESS_ATTR_V6ONLY);
2936 *buflen = sizeof (int);
2937
Florin Coras5e062572019-03-14 19:07:51 -07002938 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
2939 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002940 }
2941 else
2942 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002943 break;
2944
2945 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002946 if (buffer && buflen && (*buflen == sizeof (int)))
2947 {
2948 /* VPP-TBD */
2949 if (*(int *) buffer)
2950 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2951 else
2952 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2953
Florin Coras5e062572019-03-14 19:07:51 -07002954 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
2955 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
2956 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002957 }
2958 else
2959 rv = VPPCOM_EINVAL;
2960 break;
2961
2962 case VPPCOM_ATTR_GET_KEEPALIVE:
2963 if (buffer && buflen && (*buflen >= sizeof (int)))
2964 {
2965 /* VPP-TBD */
2966 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2967 VCL_SESS_ATTR_KEEPALIVE);
2968 *buflen = sizeof (int);
2969
Florin Coras5e062572019-03-14 19:07:51 -07002970 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
2971 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002972 }
2973 else
2974 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002975 break;
Stevenbccd3392017-10-12 20:42:21 -07002976
2977 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002978 if (buffer && buflen && (*buflen == sizeof (int)))
2979 {
2980 /* VPP-TBD */
2981 if (*(int *) buffer)
2982 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2983 else
2984 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2985
Florin Coras5e062572019-03-14 19:07:51 -07002986 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
2987 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
2988 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002989 }
2990 else
2991 rv = VPPCOM_EINVAL;
2992 break;
2993
2994 case VPPCOM_ATTR_GET_TCP_NODELAY:
2995 if (buffer && buflen && (*buflen >= sizeof (int)))
2996 {
2997 /* VPP-TBD */
2998 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2999 VCL_SESS_ATTR_TCP_NODELAY);
3000 *buflen = sizeof (int);
3001
Florin Coras5e062572019-03-14 19:07:51 -07003002 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3003 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003004 }
3005 else
3006 rv = VPPCOM_EINVAL;
3007 break;
3008
3009 case VPPCOM_ATTR_SET_TCP_NODELAY:
3010 if (buffer && buflen && (*buflen == sizeof (int)))
3011 {
3012 /* VPP-TBD */
3013 if (*(int *) buffer)
3014 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3015 else
3016 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3017
Florin Coras5e062572019-03-14 19:07:51 -07003018 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3019 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3020 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003021 }
3022 else
3023 rv = VPPCOM_EINVAL;
3024 break;
3025
3026 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3027 if (buffer && buflen && (*buflen >= sizeof (int)))
3028 {
3029 /* VPP-TBD */
3030 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3031 VCL_SESS_ATTR_TCP_KEEPIDLE);
3032 *buflen = sizeof (int);
3033
Florin Coras5e062572019-03-14 19:07:51 -07003034 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3035 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003036 }
3037 else
3038 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003039 break;
3040
3041 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003042 if (buffer && buflen && (*buflen == sizeof (int)))
3043 {
3044 /* VPP-TBD */
3045 if (*(int *) buffer)
3046 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3047 else
3048 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3049
Florin Coras5e062572019-03-14 19:07:51 -07003050 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003051 VCL_SESS_ATTR_TEST (session->attr,
3052 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003053 }
3054 else
3055 rv = VPPCOM_EINVAL;
3056 break;
3057
3058 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3059 if (buffer && buflen && (*buflen >= sizeof (int)))
3060 {
3061 /* VPP-TBD */
3062 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3063 VCL_SESS_ATTR_TCP_KEEPINTVL);
3064 *buflen = sizeof (int);
3065
Florin Coras5e062572019-03-14 19:07:51 -07003066 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3067 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003068 }
3069 else
3070 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003071 break;
3072
3073 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003074 if (buffer && buflen && (*buflen == sizeof (int)))
3075 {
3076 /* VPP-TBD */
3077 if (*(int *) buffer)
3078 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3079 else
3080 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3081
Florin Coras5e062572019-03-14 19:07:51 -07003082 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003083 VCL_SESS_ATTR_TEST (session->attr,
3084 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003085 }
3086 else
3087 rv = VPPCOM_EINVAL;
3088 break;
3089
3090 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3091 if (buffer && buflen && (*buflen >= sizeof (u32)))
3092 {
3093 /* VPP-TBD */
3094 *(u32 *) buffer = session->user_mss;
3095 *buflen = sizeof (int);
3096
Florin Coras5e062572019-03-14 19:07:51 -07003097 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3098 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003099 }
3100 else
3101 rv = VPPCOM_EINVAL;
3102 break;
3103
3104 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3105 if (buffer && buflen && (*buflen == sizeof (u32)))
3106 {
3107 /* VPP-TBD */
3108 session->user_mss = *(u32 *) buffer;
3109
Florin Coras5e062572019-03-14 19:07:51 -07003110 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3111 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003112 }
3113 else
3114 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003115 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003116
Florin Coras7baeb712019-01-04 17:05:43 -08003117 case VPPCOM_ATTR_SET_SHUT:
3118 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3119 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3120 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3121 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3122 break;
3123
3124 case VPPCOM_ATTR_GET_SHUT:
3125 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3126 tmp_flags = 1;
3127 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3128 tmp_flags |= 2;
3129 if (tmp_flags == 1)
3130 *(int *) buffer = SHUT_RD;
3131 else if (tmp_flags == 2)
3132 *(int *) buffer = SHUT_WR;
3133 else if (tmp_flags == 3)
3134 *(int *) buffer = SHUT_RDWR;
3135 *buflen = sizeof (int);
3136 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003137 default:
3138 rv = VPPCOM_EINVAL;
3139 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003140 }
3141
Dave Wallace35830af2017-10-09 01:43:42 -04003142 return rv;
3143}
3144
Stevenac1f96d2017-10-24 16:03:58 -07003145int
Florin Coras134a9962018-08-28 11:32:04 -07003146vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003147 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3148{
Florin Coras134a9962018-08-28 11:32:04 -07003149 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003150 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003151 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003152
3153 if (ep)
3154 {
Florin Coras134a9962018-08-28 11:32:04 -07003155 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003156 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003157 {
Florin Coras5e062572019-03-14 19:07:51 -07003158 VDBG (0, "sh 0x%llx is closed!", session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003159 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003160 }
Florin Coras7e12d942018-06-27 14:32:43 -07003161 ep->is_ip4 = session->transport.is_ip4;
3162 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003163 }
Steven58f464e2017-10-25 12:33:12 -07003164
3165 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003166 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003167 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003168 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003169 else
3170 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003171 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003172 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003173 }
3174
Florin Coras99368312018-08-02 10:45:44 -07003175 if (ep)
3176 {
3177 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003178 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3179 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003180 else
Dave Barach178cf492018-11-13 16:34:13 -05003181 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3182 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003183 }
Florin Coras460dce62018-07-27 05:45:06 -07003184
Stevenac1f96d2017-10-24 16:03:58 -07003185 return rv;
3186}
3187
3188int
Florin Coras134a9962018-08-28 11:32:04 -07003189vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003190 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3191{
Dave Wallace617dffa2017-10-26 14:47:06 -04003192 if (!buffer)
3193 return VPPCOM_EINVAL;
3194
3195 if (ep)
3196 {
3197 // TBD
3198 return VPPCOM_EINVAL;
3199 }
3200
3201 if (flags)
3202 {
3203 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003204 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003205 }
3206
Florin Coras42ceddb2018-12-12 10:56:01 -08003207 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003208}
3209
Dave Wallace048b1d62018-01-03 22:24:41 -05003210int
3211vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3212{
Florin Coras134a9962018-08-28 11:32:04 -07003213 vcl_worker_t *wrk = vcl_worker_get_current ();
3214 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003215 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003216 svm_msg_q_msg_t msg;
3217 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003218 int rv, num_ev = 0;
3219
Florin Coras5e062572019-03-14 19:07:51 -07003220 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003221
3222 if (!vp)
3223 return VPPCOM_EFAULT;
3224
3225 do
3226 {
Florin Coras7e12d942018-06-27 14:32:43 -07003227 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003228
Florin Coras6917b942018-11-13 22:44:54 -08003229 /* Dequeue all events and drop all unhandled io events */
3230 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3231 {
3232 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3233 vcl_handle_mq_event (wrk, e);
3234 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3235 }
3236 vec_reset_length (wrk->unhandled_evts_vector);
3237
Dave Wallace048b1d62018-01-03 22:24:41 -05003238 for (i = 0; i < n_sids; i++)
3239 {
Florin Coras7baeb712019-01-04 17:05:43 -08003240 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003241 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003242 {
3243 vp[i].revents = POLLHUP;
3244 num_ev++;
3245 continue;
3246 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003247
Florin Coras6917b942018-11-13 22:44:54 -08003248 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003249
3250 if (POLLIN & vp[i].events)
3251 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003252 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003253 if (rv > 0)
3254 {
Florin Coras6917b942018-11-13 22:44:54 -08003255 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003256 num_ev++;
3257 }
3258 else if (rv < 0)
3259 {
3260 switch (rv)
3261 {
3262 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003263 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003264 break;
3265
3266 default:
Florin Coras6917b942018-11-13 22:44:54 -08003267 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003268 break;
3269 }
3270 num_ev++;
3271 }
3272 }
3273
3274 if (POLLOUT & vp[i].events)
3275 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003276 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003277 if (rv > 0)
3278 {
Florin Coras6917b942018-11-13 22:44:54 -08003279 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003280 num_ev++;
3281 }
3282 else if (rv < 0)
3283 {
3284 switch (rv)
3285 {
3286 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003287 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003288 break;
3289
3290 default:
Florin Coras6917b942018-11-13 22:44:54 -08003291 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003292 break;
3293 }
3294 num_ev++;
3295 }
3296 }
3297
Dave Wallace7e607a72018-06-18 18:41:32 -04003298 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003299 {
Florin Coras6917b942018-11-13 22:44:54 -08003300 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003301 num_ev++;
3302 }
3303 }
3304 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003305 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003306 }
3307 while ((num_ev == 0) && keep_trying);
3308
Dave Wallace048b1d62018-01-03 22:24:41 -05003309 return num_ev;
3310}
3311
Florin Coras99368312018-08-02 10:45:44 -07003312int
3313vppcom_mq_epoll_fd (void)
3314{
Florin Coras134a9962018-08-28 11:32:04 -07003315 vcl_worker_t *wrk = vcl_worker_get_current ();
3316 return wrk->mqs_epfd;
3317}
3318
3319int
Florin Coras30e79c22019-01-02 19:31:22 -08003320vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003321{
3322 return session_handle & 0xFFFFFF;
3323}
3324
3325int
Florin Coras30e79c22019-01-02 19:31:22 -08003326vppcom_session_worker (vcl_session_handle_t session_handle)
3327{
3328 return session_handle >> 24;
3329}
3330
3331int
Florin Coras134a9962018-08-28 11:32:04 -07003332vppcom_worker_register (void)
3333{
Florin Coras47c40e22018-11-26 17:01:36 -08003334 if (!vcl_worker_alloc_and_init ())
3335 return VPPCOM_EEXIST;
3336
3337 if (vcl_worker_set_bapi ())
3338 return VPPCOM_EEXIST;
3339
3340 if (vcl_worker_register_with_vpp ())
3341 return VPPCOM_EEXIST;
3342
3343 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003344}
3345
Florin Corasdfe4cf42018-11-28 22:13:45 -08003346int
3347vppcom_worker_index (void)
3348{
3349 return vcl_get_worker_index ();
3350}
3351
Florin Corase0982e52019-01-25 13:19:56 -08003352int
3353vppcom_worker_mqs_epfd (void)
3354{
3355 vcl_worker_t *wrk = vcl_worker_get_current ();
3356 if (!vcm->cfg.use_mq_eventfd)
3357 return -1;
3358 return wrk->mqs_epfd;
3359}
3360
Dave Wallacee22aa742017-10-20 12:30:38 -04003361/*
3362 * fd.io coding-style-patch-verification: ON
3363 *
3364 * Local Variables:
3365 * eval: (c-set-style "gnu")
3366 * End:
3367 */