blob: d5dabb65383362e2b96b0d44c7e3eba753cea529 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Dave Wallace33e002b2017-09-06 01:20:02 -04002 * Copyright (c) 2017 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 Wallace543852a2017-08-03 02:11:34 -040018#include <svm/svm_fifo_segment.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040019#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070020#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070021#include <vcl/vcl_private.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
47vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
48{
49 svm_msg_q_msg_t *msg;
50 u32 n_msgs;
51 int i;
52
53 n_msgs = svm_msg_q_size (mq);
54 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 Coras134a9962018-08-28 11:32:04 -0700263vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700264{
265 vcl_session_t *session, *listen_session;
266 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700267 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700268 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700269
Florin Coras134a9962018-08-28 11:32:04 -0700270 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700271
Florin Coras134a9962018-08-28 11:32:04 -0700272 listen_session = vcl_session_table_lookup_listener (wrk,
273 mp->listener_handle);
Florin Coras54693d22018-07-17 10:46:29 -0700274 if (!listen_session)
275 {
Florin Coras54693d22018-07-17 10:46:29 -0700276 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
277 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
278 "unknown vpp listener handle %llx",
279 getpid (), mp->listener_handle);
280 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
281 VNET_API_ERROR_INVALID_ARGUMENT);
Florin Coras134a9962018-08-28 11:32:04 -0700282 vcl_session_free (wrk, session);
Florin Coras54693d22018-07-17 10:46:29 -0700283 return VCL_INVALID_SESSION_INDEX;
284 }
285
Florin Coras54693d22018-07-17 10:46:29 -0700286 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
287 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
288
Florin Coras653e43f2019-03-04 10:56:23 -0800289 if (vcl_wait_for_segment (mp->segment_handle))
Florin Coras54693d22018-07-17 10:46:29 -0700290 {
Florin Coras653e43f2019-03-04 10:56:23 -0800291 clib_warning ("segment for session %u couldn't be mounted!",
292 session->session_index);
293 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700294 }
Florin Coras653e43f2019-03-04 10:56:23 -0800295
296 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
297 svm_msg_q_t *);
298 rx_fifo->client_session_index = session->session_index;
299 tx_fifo->client_session_index = session->session_index;
300 rx_fifo->client_thread_index = vcl_get_worker_index ();
301 tx_fifo->client_thread_index = vcl_get_worker_index ();
302 vpp_wrk_index = tx_fifo->master_thread_index;
303 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
304 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700305
306 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800307 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700308 session->client_context = mp->context;
309 session->rx_fifo = rx_fifo;
310 session->tx_fifo = tx_fifo;
311
312 session->session_state = STATE_ACCEPT;
313 session->transport.rmt_port = mp->port;
314 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500315 clib_memcpy_fast (&session->transport.rmt_ip, mp->ip,
316 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700317
Florin Coras134a9962018-08-28 11:32:04 -0700318 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700319 session->transport.lcl_port = listen_session->transport.lcl_port;
320 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700321 session->session_type = listen_session->session_type;
322 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
Florin Coras54693d22018-07-17 10:46:29 -0700323
324 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
Florin Coras134a9962018-08-28 11:32:04 -0700325 " address %U port %d queue %p!", getpid (), mp->handle,
326 session->session_index,
Florin Coras54693d22018-07-17 10:46:29 -0700327 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
328 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
329 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
330 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
331
Florin Coras134a9962018-08-28 11:32:04 -0700332 return session->session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700333}
334
335static u32
Florin Coras134a9962018-08-28 11:32:04 -0700336vcl_session_connected_handler (vcl_worker_t * wrk,
337 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700338{
Florin Coras99368312018-08-02 10:45:44 -0700339 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700340 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700341 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -0700342
343 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700344 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700345 if (!session)
346 {
347 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
348 "Invalid session index (%u)!",
349 getpid (), mp->handle, session_index);
350 return VCL_INVALID_SESSION_INDEX;
351 }
Florin Coras54693d22018-07-17 10:46:29 -0700352 if (mp->retval)
353 {
Florin Coras070453d2018-08-24 17:04:27 -0700354 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
Florin Coras21795132018-09-09 09:40:51 -0700355 session_index, format_api_error, ntohl (mp->retval));
Florin Coras070453d2018-08-24 17:04:27 -0700356 session->session_state = STATE_FAILED;
357 session->vpp_handle = mp->handle;
358 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700359 }
360
Florin Coras460dce62018-07-27 05:45:06 -0700361 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
362 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800363 if (vcl_wait_for_segment (mp->segment_handle))
364 {
Florin Coras653e43f2019-03-04 10:56:23 -0800365 VDBG (0, "segment for session %u couldn't be mounted!",
366 session->session_index);
Florin Corasd85de682018-11-29 17:02:29 -0800367 return VCL_INVALID_SESSION_INDEX;
368 }
369
Florin Coras460dce62018-07-27 05:45:06 -0700370 rx_fifo->client_session_index = session_index;
371 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700372 rx_fifo->client_thread_index = vcl_get_worker_index ();
373 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700374
Florin Coras653e43f2019-03-04 10:56:23 -0800375 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
376 svm_msg_q_t *);
377 vpp_wrk_index = tx_fifo->master_thread_index;
378 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
379 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700380
Florin Coras653e43f2019-03-04 10:56:23 -0800381 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700382 {
Florin Coras653e43f2019-03-04 10:56:23 -0800383 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
384 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
385 if (vcl_wait_for_segment (mp->ct_segment_handle))
386 {
387 VDBG (0, "ct segment for session %u couldn't be mounted!",
388 session->session_index);
389 return VCL_INVALID_SESSION_INDEX;
390 }
Florin Coras99368312018-08-02 10:45:44 -0700391 }
Florin Coras54693d22018-07-17 10:46:29 -0700392
Florin Coras54693d22018-07-17 10:46:29 -0700393 session->rx_fifo = rx_fifo;
394 session->tx_fifo = tx_fifo;
395 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800396 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700397 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500398 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
399 sizeof (session->transport.lcl_ip));
Florin Coras54693d22018-07-17 10:46:29 -0700400 session->transport.lcl_port = mp->lcl_port;
401 session->session_state = STATE_CONNECT;
402
403 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800404 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700405
406 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
407 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
408 getpid (), mp->handle, session_index, session->rx_fifo,
409 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700410
Florin Coras54693d22018-07-17 10:46:29 -0700411 return session_index;
412}
413
Florin Coras3c7d4f92018-12-14 11:28:43 -0800414static int
415vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
416{
417 vcl_session_msg_t *accepted_msg;
418 int i;
419
420 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
421 {
422 accepted_msg = &session->accept_evts_fifo[i];
423 if (accepted_msg->accepted_msg.handle == handle)
424 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800425 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800426 return 1;
427 }
428 }
429 return 0;
430}
431
Florin Corasc9fbd662018-08-24 12:59:56 -0700432static u32
Florin Coras134a9962018-08-28 11:32:04 -0700433vcl_session_reset_handler (vcl_worker_t * wrk,
434 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700435{
436 vcl_session_t *session;
437 u32 sid;
438
Florin Coras134a9962018-08-28 11:32:04 -0700439 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
440 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700441 if (!session)
442 {
443 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
444 return VCL_INVALID_SESSION_INDEX;
445 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800446
447 /* Caught a reset before actually accepting the session */
448 if (session->session_state == STATE_LISTEN)
449 {
450
451 if (!vcl_flag_accepted_session (session, reset_msg->handle,
452 VCL_ACCEPTED_F_RESET))
453 VDBG (0, "session was not accepted!");
454 return VCL_INVALID_SESSION_INDEX;
455 }
456
457 session->session_state = STATE_DISCONNECT;
458 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700459 return sid;
460}
461
Florin Coras60116992018-08-27 09:52:18 -0700462static u32
Florin Coras134a9962018-08-28 11:32:04 -0700463vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700464{
465 vcl_session_t *session;
466 u32 sid = mp->context;
467
Florin Coras134a9962018-08-28 11:32:04 -0700468 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700469 if (mp->retval)
470 {
Florin Corasd85de682018-11-29 17:02:29 -0800471 VERR ("vpp handle 0x%llx, sid %u: bind failed: %U", mp->handle, sid,
472 format_api_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700473 if (session)
474 {
475 session->session_state = STATE_FAILED;
476 session->vpp_handle = mp->handle;
477 return sid;
478 }
479 else
480 {
481 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
482 "Invalid session index (%u)!",
483 getpid (), mp->handle, sid);
484 return VCL_INVALID_SESSION_INDEX;
485 }
486 }
487
488 session->vpp_handle = mp->handle;
489 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500490 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
491 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700492 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700493 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700494 session->session_state = STATE_LISTEN;
495
Florin Coras5f45e012019-01-23 09:21:30 -0800496 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
497 vec_validate (wrk->vpp_event_queues, 0);
498 wrk->vpp_event_queues[0] = session->vpp_evt_q;
499
Florin Coras60116992018-08-27 09:52:18 -0700500 if (session->is_dgram)
501 {
502 svm_fifo_t *rx_fifo, *tx_fifo;
503 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
504 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
505 rx_fifo->client_session_index = sid;
506 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
507 tx_fifo->client_session_index = sid;
508 session->rx_fifo = rx_fifo;
509 session->tx_fifo = tx_fifo;
510 }
511
Florin Coras05ecfcc2018-12-12 18:19:39 -0800512 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700513 return sid;
514}
515
Florin Corasdfae9f92019-02-20 19:48:31 -0800516static void
517vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
518{
519 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
520 vcl_session_t *s;
521
522 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
523 if (!s || s->session_state != STATE_DISCONNECT)
524 {
525 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
526 return;
527 }
528
529 if (mp->retval)
530 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
531 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
532
533 if (mp->context != wrk->wrk_index)
534 VDBG (0, "wrong context");
535
536 vcl_session_table_del_vpp_handle (wrk, mp->handle);
537 vcl_session_free (wrk, s);
538}
539
Florin Coras3c7d4f92018-12-14 11:28:43 -0800540static vcl_session_t *
541vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
542{
543 vcl_session_msg_t *vcl_msg;
544 vcl_session_t *session;
545
546 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
547 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800548 VWRN ("session overlap handle %lu state %u!", msg->handle,
549 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800550
551 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
552 if (!session)
553 {
554 VERR ("couldn't find listen session: listener handle %llx",
555 msg->listener_handle);
556 return 0;
557 }
558
559 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
560 vcl_msg->accepted_msg = *msg;
561 /* Session handle points to listener until fully accepted by app */
562 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
563
564 return session;
565}
566
567static vcl_session_t *
568vcl_session_disconnected_handler (vcl_worker_t * wrk,
569 session_disconnected_msg_t * msg)
570{
571 vcl_session_t *session;
572
573 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
574 if (!session)
575 {
576 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
577 return 0;
578 }
579
580 /* Caught a disconnect before actually accepting the session */
581 if (session->session_state == STATE_LISTEN)
582 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800583 if (!vcl_flag_accepted_session (session, msg->handle,
584 VCL_ACCEPTED_F_CLOSED))
585 VDBG (0, "session was not accepted!");
586 return 0;
587 }
588
589 session->session_state = STATE_VPP_CLOSING;
590 return session;
591}
592
Florin Coras30e79c22019-01-02 19:31:22 -0800593static void
594vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
595{
596 session_req_worker_update_msg_t *msg;
597 vcl_session_t *s;
598
599 msg = (session_req_worker_update_msg_t *) data;
600 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
601 if (!s)
602 return;
603
604 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
605}
606
607static void
608vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
609{
610 session_worker_update_reply_msg_t *msg;
611 vcl_session_t *s;
612
613 msg = (session_worker_update_reply_msg_t *) data;
614 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
615 if (!s)
616 {
617 VDBG (0, "unknown handle 0x%llx", msg->handle);
618 return;
619 }
620 if (vcl_wait_for_segment (msg->segment_handle))
621 {
622 clib_warning ("segment for session %u couldn't be mounted!",
623 s->session_index);
624 return;
625 }
Florin Coras30e79c22019-01-02 19:31:22 -0800626
Florin Coras5f45e012019-01-23 09:21:30 -0800627 if (s->rx_fifo)
628 {
629 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
630 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
631 s->rx_fifo->client_session_index = s->session_index;
632 s->tx_fifo->client_session_index = s->session_index;
633 s->rx_fifo->client_thread_index = wrk->wrk_index;
634 s->tx_fifo->client_thread_index = wrk->wrk_index;
635 }
Florin Coras30e79c22019-01-02 19:31:22 -0800636 s->session_state = STATE_UPDATED;
637
Florin Coras30e79c22019-01-02 19:31:22 -0800638 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
639 s->vpp_handle, wrk->wrk_index);
640}
641
Florin Coras86f04502018-09-12 16:08:01 -0700642static int
643vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700644{
Florin Coras54693d22018-07-17 10:46:29 -0700645 session_disconnected_msg_t *disconnected_msg;
Florin Coras54693d22018-07-17 10:46:29 -0700646 vcl_session_t *session;
Florin Coras54693d22018-07-17 10:46:29 -0700647
648 switch (e->event_type)
649 {
Florin Coras653e43f2019-03-04 10:56:23 -0800650 case SESSION_IO_EVT_RX:
651 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -0800652 session = vcl_session_get (wrk, e->session_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800653 if (!session || !(session->session_state & STATE_OPEN))
654 break;
Florin Coras86f04502018-09-12 16:08:01 -0700655 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700656 break;
657 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800658 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700659 break;
660 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700661 vcl_session_connected_handler (wrk,
662 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700663 break;
664 case SESSION_CTRL_EVT_DISCONNECTED:
665 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800666 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Corasc9fbd662018-08-24 12:59:56 -0700667 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800668 break;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800669 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
670 session->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700671 break;
672 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700673 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700674 break;
675 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700676 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700677 break;
Florin Corasdfae9f92019-02-20 19:48:31 -0800678 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
679 vcl_session_unlisten_reply_handler (wrk, e->data);
680 break;
Florin Coras30e79c22019-01-02 19:31:22 -0800681 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
682 vcl_session_req_worker_update_handler (wrk, e->data);
683 break;
684 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
685 vcl_session_worker_update_reply_handler (wrk, e->data);
686 break;
Florin Coras54693d22018-07-17 10:46:29 -0700687 default:
688 clib_warning ("unhandled %u", e->event_type);
689 }
690 return VPPCOM_OK;
691}
692
Florin Coras30e79c22019-01-02 19:31:22 -0800693static int
Florin Coras697faea2018-06-27 17:10:49 -0700694vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -0800695 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -0700696 f64 wait_for_time)
697{
Florin Coras134a9962018-08-28 11:32:04 -0700698 vcl_worker_t *wrk = vcl_worker_get_current ();
699 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700700 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700701 svm_msg_q_msg_t msg;
702 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400703
Florin Coras697faea2018-06-27 17:10:49 -0700704 do
Dave Wallace543852a2017-08-03 02:11:34 -0400705 {
Florin Coras134a9962018-08-28 11:32:04 -0700706 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700707 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700708 {
Florin Coras070453d2018-08-24 17:04:27 -0700709 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700710 }
711 if (session->session_state & state)
712 {
Florin Coras697faea2018-06-27 17:10:49 -0700713 return VPPCOM_OK;
714 }
715 if (session->session_state & STATE_FAILED)
716 {
Florin Coras697faea2018-06-27 17:10:49 -0700717 return VPPCOM_ECONNREFUSED;
718 }
Florin Coras54693d22018-07-17 10:46:29 -0700719
Florin Coras134a9962018-08-28 11:32:04 -0700720 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -0800721 {
722 usleep (100);
723 continue;
724 }
Florin Coras134a9962018-08-28 11:32:04 -0700725 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700726 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700727 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800728 }
Florin Coras134a9962018-08-28 11:32:04 -0700729 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800730
Florin Coras05ecfcc2018-12-12 18:19:39 -0800731 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -0700732 vppcom_session_state_str (state));
733 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400734
Florin Coras697faea2018-06-27 17:10:49 -0700735 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500736}
737
Florin Coras30e79c22019-01-02 19:31:22 -0800738static void
739vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
740{
Florin Coras288eaab2019-02-03 15:26:14 -0800741 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -0800742 vcl_session_t *s;
743 u32 *sip;
744
745 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
746 return;
747
748 vec_foreach (sip, wrk->pending_session_wrk_updates)
749 {
750 s = vcl_session_get (wrk, *sip);
751 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
752 state = s->session_state;
753 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
754 s->session_state = state;
755 }
756 vec_reset_length (wrk->pending_session_wrk_updates);
757}
758
Florin Corasf9240dc2019-01-15 08:03:17 -0800759void
Florin Coras30e79c22019-01-02 19:31:22 -0800760vcl_flush_mq_events (void)
761{
762 vcl_worker_t *wrk = vcl_worker_get_current ();
763 svm_msg_q_msg_t *msg;
764 session_event_t *e;
765 svm_msg_q_t *mq;
766 int i;
767
768 mq = wrk->app_event_queue;
769 svm_msg_q_lock (mq);
770 vcl_mq_dequeue_batch (wrk, mq);
771 svm_msg_q_unlock (mq);
772
773 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
774 {
775 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
776 e = svm_msg_q_msg_data (mq, msg);
777 vcl_handle_mq_event (wrk, e);
778 svm_msg_q_free_msg (mq, msg);
779 }
780 vec_reset_length (wrk->mq_msg_vector);
781 vcl_handle_pending_wrk_updates (wrk);
782}
783
Florin Coras697faea2018-06-27 17:10:49 -0700784static int
785vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400786{
Florin Coras697faea2018-06-27 17:10:49 -0700787 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400788
Florin Coras697faea2018-06-27 17:10:49 -0700789 if (vcm->app_state != STATE_APP_ENABLED)
790 {
791 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -0700792 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -0700793 if (PREDICT_FALSE (rv))
794 {
795 VDBG (0, "VCL<%d>: application session enable timed out! "
796 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
797 return rv;
798 }
799 }
800 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400801}
802
Florin Coras697faea2018-06-27 17:10:49 -0700803static int
804vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400805{
Florin Coras697faea2018-06-27 17:10:49 -0700806 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400807
Florin Coras697faea2018-06-27 17:10:49 -0700808 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -0700809 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -0700810 if (PREDICT_FALSE (rv))
811 {
812 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
813 getpid (), rv, vppcom_retval_str (rv));
814 return rv;
815 }
Dave Wallace543852a2017-08-03 02:11:34 -0400816
Florin Coras697faea2018-06-27 17:10:49 -0700817 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400818}
819
820static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700821vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400822{
Florin Coras134a9962018-08-28 11:32:04 -0700823 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700824 vcl_session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500825 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400826
Florin Corasab2f6db2018-08-31 14:31:41 -0700827 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700828 if (!session)
829 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500830
831 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500832 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700833 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500834
Florin Coras2d675d72019-01-28 15:54:27 -0800835 VDBG (1, "vpp handle 0x%llx, sid %u: sending unbind msg! new state"
836 " 0x%x (%s)", vpp_handle, session_handle, STATE_DISCONNECT,
Florin Coras0d427d82018-06-27 03:24:07 -0700837 vppcom_session_state_str (STATE_DISCONNECT));
838 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 Coras0d427d82018-06-27 03:24:07 -0700860 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
Florin Corasab2f6db2018-08-31 14:31:41 -0700861 vpp_handle, session_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 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500865 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500866 "Cannot disconnect a listen socket!",
Florin Corasab2f6db2018-08-31 14:31:41 -0700867 getpid (), vpp_handle, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700868 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500869 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400870
Florin Coras3c7d4f92018-12-14 11:28:43 -0800871 if (state & STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500872 {
Florin Coras134a9962018-08-28 11:32:04 -0700873 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras47c40e22018-11-26 17:01:36 -0800874 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
Florin Coras99368312018-08-02 10:45:44 -0700875 vpp_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700876 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
Florin Corasab2f6db2018-08-31 14:31:41 -0700877 "REPLY...", getpid (), vpp_handle, session_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400878 }
879 else
Dave Wallace227867f2017-11-13 21:21:53 -0500880 {
Florin Coras0d427d82018-06-27 03:24:07 -0700881 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
Florin Corasab2f6db2018-08-31 14:31:41 -0700882 getpid (), vpp_handle, session_handle);
883 vppcom_send_disconnect_session (vpp_handle);
Dave Wallace227867f2017-11-13 21:21:53 -0500884 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400885
Florin Coras070453d2018-08-24 17:04:27 -0700886 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400887}
888
Florin Coras940f78f2018-11-30 12:11:20 -0800889/**
890 * Handle app exit
891 *
892 * Notify vpp of the disconnect and mark the worker as free. If we're the
893 * last worker, do a full cleanup otherwise, since we're probably a forked
894 * child, avoid syscalls as much as possible. We might've lost privileges.
895 */
896void
897vppcom_app_exit (void)
898{
899 if (!pool_elts (vcm->workers))
900 return;
Florin Coras01f3f892018-12-02 12:45:53 -0800901 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
902 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -0800903 vcl_elog_stop (vcm);
904 if (vec_len (vcm->workers) == 1)
905 vl_client_disconnect_from_vlib ();
906 else
Florin Coraseaec2a62018-12-04 16:34:05 -0800907 vl_client_send_disconnect (1 /* vpp should cleanup */ );
Florin Coras940f78f2018-11-30 12:11:20 -0800908}
909
Dave Wallace543852a2017-08-03 02:11:34 -0400910/*
911 * VPPCOM Public API functions
912 */
913int
914vppcom_app_create (char *app_name)
915{
Dave Wallace543852a2017-08-03 02:11:34 -0400916 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400917 int rv;
918
Florin Coras47c40e22018-11-26 17:01:36 -0800919 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -0400920 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800921 VDBG (1, "already initialized");
922 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -0400923 }
924
Florin Coras47c40e22018-11-26 17:01:36 -0800925 vcm->is_init = 1;
926 vppcom_cfg (&vcm->cfg);
927 vcl_cfg = &vcm->cfg;
928
929 vcm->main_cpu = pthread_self ();
930 vcm->main_pid = getpid ();
931 vcm->app_name = format (0, "%s", app_name);
932 vppcom_init_error_string_table ();
Florin Corasadc74d72018-12-02 13:36:00 -0800933 svm_fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
Florin Coras47c40e22018-11-26 17:01:36 -0800934 20 /* timeout in secs */ );
935 pool_alloc (vcm->workers, vcl_cfg->max_workers);
936 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -0800937 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -0800938 atexit (vppcom_app_exit);
Florin Coras47c40e22018-11-26 17:01:36 -0800939
940 /* Allocate default worker */
941 vcl_worker_alloc_and_init ();
942
943 /* API hookup and connect to VPP */
944 vppcom_api_hookup ();
945 vcl_elog_init (vcm);
946 vcm->app_state = STATE_APP_START;
947 rv = vppcom_connect_to_vpp (app_name);
948 if (rv)
Dave Wallace543852a2017-08-03 02:11:34 -0400949 {
Florin Coras47c40e22018-11-26 17:01:36 -0800950 VERR ("couldn't connect to VPP!");
951 return rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400952 }
Florin Coras47c40e22018-11-26 17:01:36 -0800953 VDBG (0, "sending session enable");
954 rv = vppcom_app_session_enable ();
955 if (rv)
956 {
957 VERR ("vppcom_app_session_enable() failed!");
958 return rv;
959 }
960
961 VDBG (0, "sending app attach");
962 rv = vppcom_app_attach ();
963 if (rv)
964 {
965 VERR ("vppcom_app_attach() failed!");
966 return rv;
967 }
968
969 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
970 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400971
972 return VPPCOM_OK;
973}
974
975void
976vppcom_app_destroy (void)
977{
Dave Wallace543852a2017-08-03 02:11:34 -0400978 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400979 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400980
Florin Coras940f78f2018-11-30 12:11:20 -0800981 if (!pool_elts (vcm->workers))
982 return;
983
Florin Coras0d427d82018-06-27 03:24:07 -0700984 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800985
Florin Coras940f78f2018-11-30 12:11:20 -0800986 if (pool_elts (vcm->workers) == 1)
Florin Coras47c40e22018-11-26 17:01:36 -0800987 {
988 vppcom_app_send_detach ();
989 orig_app_timeout = vcm->cfg.app_timeout;
990 vcm->cfg.app_timeout = 2.0;
991 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
992 vcm->cfg.app_timeout = orig_app_timeout;
993 if (PREDICT_FALSE (rv))
994 VDBG (0, "application detach timed out! returning %d (%s)", rv,
995 vppcom_retval_str (rv));
Florin Coras940f78f2018-11-30 12:11:20 -0800996 vec_free (vcm->app_name);
Florin Coras01f3f892018-12-02 12:45:53 -0800997 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -0800998 }
999 else
1000 {
Florin Coras01f3f892018-12-02 12:45:53 -08001001 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001002 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001003
Florin Coras01f3f892018-12-02 12:45:53 -08001004 vcl_set_worker_index (~0);
Florin Coras0d427d82018-06-27 03:24:07 -07001005 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04001006 vl_client_disconnect_from_vlib ();
Dave Wallace543852a2017-08-03 02:11:34 -04001007}
1008
1009int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001010vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001011{
Florin Coras134a9962018-08-28 11:32:04 -07001012 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001013 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001014
Florin Coras134a9962018-08-28 11:32:04 -07001015 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001016
Florin Coras7e12d942018-06-27 14:32:43 -07001017 session->session_type = proto;
1018 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001019 session->vpp_handle = ~0;
Florin Coras460dce62018-07-27 05:45:06 -07001020 session->is_dgram = proto == VPPCOM_PROTO_UDP;
Dave Wallace543852a2017-08-03 02:11:34 -04001021
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001022 if (is_nonblocking)
1023 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001024
Florin Coras7e12d942018-06-27 14:32:43 -07001025 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1026 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001027
Florin Coras053a0e42018-11-13 15:52:38 -08001028 VDBG (0, "created sid %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001029
Florin Coras134a9962018-08-28 11:32:04 -07001030 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001031}
1032
1033int
Florin Corasf9240dc2019-01-15 08:03:17 -08001034vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1035 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001036{
Florin Coras288eaab2019-02-03 15:26:14 -08001037 vcl_session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -07001038 u32 next_sh, vep_sh;
1039 int rv = VPPCOM_OK;
1040 u64 vpp_handle;
Florin Corasf9240dc2019-01-15 08:03:17 -08001041 u8 is_vep;
Florin Coras47c40e22018-11-26 17:01:36 -08001042
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001043 is_vep = session->is_vep;
Florin Coras134a9962018-08-28 11:32:04 -07001044 next_sh = session->vep.next_sh;
1045 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -07001046 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05001047 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001048
Florin Corasf9240dc2019-01-15 08:03:17 -08001049 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001050
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001051 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04001052 {
Florin Coras134a9962018-08-28 11:32:04 -07001053 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001054 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001055 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001056 if (PREDICT_FALSE (rv < 0))
Florin Coras47c40e22018-11-26 17:01:36 -08001057 VDBG (0, "vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL vep_idx %u"
1058 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1059 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001060
Florin Coras134a9962018-08-28 11:32:04 -07001061 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001062 }
1063 }
1064 else
1065 {
Florin Coras47c40e22018-11-26 17:01:36 -08001066 if (session->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001067 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001068 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001069 if (rv < 0)
Florin Corasf9240dc2019-01-15 08:03:17 -08001070 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1071 "failed! rv %d (%s)", session->session_index, vpp_handle,
1072 vep_sh, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001073 }
1074
Florin Coras47c40e22018-11-26 17:01:36 -08001075 if (!do_disconnect)
Florin Coras30e79c22019-01-02 19:31:22 -08001076 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08001077 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Corasf9240dc2019-01-15 08:03:17 -08001078 session->session_index, vpp_handle);
Florin Coras30e79c22019-01-02 19:31:22 -08001079 goto cleanup;
1080 }
Florin Coras47c40e22018-11-26 17:01:36 -08001081
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001082 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001083 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001084 rv = vppcom_session_unbind (sh);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001085 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001086 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1087 "rv %d (%s)", session->session_index, vpp_handle, rv,
Florin Coras47c40e22018-11-26 17:01:36 -08001088 vppcom_retval_str (rv));
Florin Corasdfae9f92019-02-20 19:48:31 -08001089 return rv;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001090 }
Florin Coras070453d2018-08-24 17:04:27 -07001091 else if (state & STATE_OPEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001092 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001093 rv = vppcom_session_disconnect (sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001094 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001095 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1096 " rv %d (%s)", session->session_index, vpp_handle,
1097 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001098 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001099 else if (state == STATE_DISCONNECT)
1100 {
1101 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1102 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1103 session->vpp_handle, 0);
1104 }
Dave Wallace19481612017-09-15 18:47:44 -04001105 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001106
Florin Coras0ef8ef22019-01-18 08:37:13 -08001107 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1108
Florin Corasf9240dc2019-01-15 08:03:17 -08001109cleanup:
Florin Coras30e79c22019-01-02 19:31:22 -08001110 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Florin Coras134a9962018-08-28 11:32:04 -07001111 vcl_session_free (wrk, session);
Florin Coras0d427d82018-06-27 03:24:07 -07001112 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001113
Dave Wallace543852a2017-08-03 02:11:34 -04001114 return rv;
1115}
1116
1117int
Florin Corasf9240dc2019-01-15 08:03:17 -08001118vppcom_session_close (uint32_t session_handle)
1119{
1120 vcl_worker_t *wrk = vcl_worker_get_current ();
1121 vcl_session_t *session;
1122
1123 session = vcl_session_get_w_handle (wrk, session_handle);
1124 if (!session)
1125 return VPPCOM_EBADFD;
1126 return vcl_session_cleanup (wrk, session, session_handle,
1127 1 /* do_disconnect */ );
1128}
1129
1130int
Florin Coras134a9962018-08-28 11:32:04 -07001131vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001132{
Florin Coras134a9962018-08-28 11:32:04 -07001133 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001134 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001135
1136 if (!ep || !ep->ip)
1137 return VPPCOM_EINVAL;
1138
Florin Coras134a9962018-08-28 11:32:04 -07001139 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001140 if (!session)
1141 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001142
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001143 if (session->is_vep)
1144 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001145 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -07001146 "bind to an epoll session!", getpid (), session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001147 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001148 }
1149
Florin Coras7e12d942018-06-27 14:32:43 -07001150 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001151 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001152 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1153 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001154 else
Dave Barach178cf492018-11-13 16:34:13 -05001155 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1156 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001157 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001158
Florin Coras0d427d82018-06-27 03:24:07 -07001159 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
Florin Coras134a9962018-08-28 11:32:04 -07001160 "proto %s", getpid (), session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001161 session->transport.is_ip4 ? "IPv4" : "IPv6",
1162 format_ip46_address, &session->transport.lcl_ip,
1163 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1164 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001165 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001166 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001167
1168 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001169 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001170
Florin Coras070453d2018-08-24 17:04:27 -07001171 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001172}
1173
1174int
Florin Coras134a9962018-08-28 11:32:04 -07001175vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001176{
Florin Coras134a9962018-08-28 11:32:04 -07001177 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001178 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001179 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001180 int rv;
1181
Florin Coras134a9962018-08-28 11:32:04 -07001182 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001183 if (!listen_session || listen_session->is_vep)
Florin Coras070453d2018-08-24 17:04:27 -07001184 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001185
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001186 if (q_len == 0 || q_len == ~0)
1187 q_len = vcm->cfg.listen_queue_size;
1188
Dave Wallaceee45d412017-11-24 21:44:06 -05001189 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001190 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001191 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001192 VDBG (0, "session %u [0x%llx]: already in listen state!",
1193 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001194 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001195 }
1196
Florin Coras05ecfcc2018-12-12 18:19:39 -08001197 VDBG (0, "session %u [0x%llx]: sending vpp listen request...",
1198 listen_sh, listen_vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001199
Florin Coras070453d2018-08-24 17:04:27 -07001200 /*
1201 * Send listen request to vpp and wait for reply
1202 */
Florin Coras134a9962018-08-28 11:32:04 -07001203 vppcom_send_bind_sock (listen_session);
1204 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1205 STATE_LISTEN,
1206 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001207
Florin Coras070453d2018-08-24 17:04:27 -07001208 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001209 {
Florin Coras134a9962018-08-28 11:32:04 -07001210 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001211 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1212 listen_sh, listen_session->vpp_handle, rv,
1213 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001214 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001215 }
1216
Florin Coras070453d2018-08-24 17:04:27 -07001217 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001218}
1219
Ping Yu34a3a082018-11-30 19:16:17 -05001220int
1221vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1222 uint32_t cert_len)
1223{
1224
1225 vcl_worker_t *wrk = vcl_worker_get_current ();
1226 vcl_session_t *session = 0;
1227
1228 session = vcl_session_get_w_handle (wrk, session_handle);
1229 if (!session)
1230 return VPPCOM_EBADFD;
1231
1232 if (cert_len == 0 || cert_len == ~0)
1233 return VPPCOM_EBADFD;
1234
1235 /*
1236 * Send listen request to vpp and wait for reply
1237 */
1238 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1239
1240 return VPPCOM_OK;
1241
1242}
1243
1244int
1245vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1246 uint32_t key_len)
1247{
1248
1249 vcl_worker_t *wrk = vcl_worker_get_current ();
1250 vcl_session_t *session = 0;
1251
1252 session = vcl_session_get_w_handle (wrk, session_handle);
1253 if (!session)
1254 return VPPCOM_EBADFD;
1255
1256 if (key_len == 0 || key_len == ~0)
1257 return VPPCOM_EBADFD;
1258
1259 /*
1260 * Send listen request to vpp and wait for reply
1261 */
1262 vppcom_send_application_tls_key_add (session, key, key_len);
1263
1264 return VPPCOM_OK;
1265
1266
1267}
1268
Florin Coras134a9962018-08-28 11:32:04 -07001269static int
1270validate_args_session_accept_ (vcl_worker_t * wrk,
1271 vcl_session_t * listen_session)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001272{
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001273 /* Input validation - expects spinlock on sessions_lockp */
1274 if (listen_session->is_vep)
1275 {
1276 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
Florin Coras134a9962018-08-28 11:32:04 -07001277 "epoll session!", getpid (),
1278 listen_session->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001279 return VPPCOM_EBADFD;
1280 }
1281
Florin Coras7e12d942018-06-27 14:32:43 -07001282 if (listen_session->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001283 {
1284 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1285 "not in listen state! state 0x%x (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001286 listen_session->vpp_handle, listen_session->session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001287 listen_session->session_state,
1288 vppcom_session_state_str (listen_session->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001289 return VPPCOM_EBADFD;
1290 }
1291 return VPPCOM_OK;
1292}
1293
1294int
Florin Coras134a9962018-08-28 11:32:04 -07001295vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001296 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001297{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001298 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001299 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001300 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001301 vcl_session_t *listen_session = 0;
1302 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001303 vcl_session_msg_t *evt;
Dave Wallaceee45d412017-11-24 21:44:06 -05001304 u64 listen_vpp_handle;
Florin Coras54693d22018-07-17 10:46:29 -07001305 svm_msg_q_msg_t msg;
1306 session_event_t *e;
1307 u8 is_nonblocking;
1308 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001309
Florin Coras134a9962018-08-28 11:32:04 -07001310 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001311 if (!listen_session)
1312 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001313
Florin Coras134a9962018-08-28 11:32:04 -07001314 listen_session_index = listen_session->session_index;
1315 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001316 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001317
Florin Coras54693d22018-07-17 10:46:29 -07001318 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001319 {
Florin Coras54693d22018-07-17 10:46:29 -07001320 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001321 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001322 accepted_msg = evt->accepted_msg;
1323 goto handle;
1324 }
1325
1326 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1327 VCL_SESS_ATTR_NONBLOCK);
Florin Coras134a9962018-08-28 11:32:04 -07001328 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
Florin Coras54693d22018-07-17 10:46:29 -07001329 return VPPCOM_EAGAIN;
1330
1331 while (1)
1332 {
Florin Coras134a9962018-08-28 11:32:04 -07001333 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001334 return VPPCOM_EAGAIN;
1335
Florin Coras134a9962018-08-28 11:32:04 -07001336 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001337 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001338 {
Florin Coras54693d22018-07-17 10:46:29 -07001339 clib_warning ("discarded event: %u", e->event_type);
Florin Coras134a9962018-08-28 11:32:04 -07001340 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001341 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001342 }
Dave Barach178cf492018-11-13 16:34:13 -05001343 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001344 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001345 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001346 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001347
Florin Coras54693d22018-07-17 10:46:29 -07001348handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001349
Florin Coras134a9962018-08-28 11:32:04 -07001350 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1351 listen_session = vcl_session_get (wrk, listen_session_index);
1352 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001353
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001354 if (flags & O_NONBLOCK)
1355 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001356
Florin Coras54693d22018-07-17 10:46:29 -07001357 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras05ecfcc2018-12-12 18:19:39 -08001358 VDBG (1, "vpp handle 0x%llx, sid %u: Got a client request! "
Florin Coras0d427d82018-06-27 03:24:07 -07001359 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
Florin Corasdc2e2512018-12-03 17:47:26 -08001360 listen_vpp_handle, listen_session_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001361 client_session->vpp_handle, client_session_index,
1362 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1363 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001364
Dave Wallace048b1d62018-01-03 22:24:41 -05001365 if (ep)
1366 {
Florin Coras7e12d942018-06-27 14:32:43 -07001367 ep->is_ip4 = client_session->transport.is_ip4;
1368 ep->port = client_session->transport.rmt_port;
1369 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001370 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1371 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001372 else
Dave Barach178cf492018-11-13 16:34:13 -05001373 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1374 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001375 }
Dave Wallace60caa062017-11-10 17:07:13 -05001376
Florin Coras653e43f2019-03-04 10:56:23 -08001377 vcl_send_session_accepted_reply (client_session->vpp_evt_q,
1378 client_session->client_context,
Florin Coras54693d22018-07-17 10:46:29 -07001379 client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05001380
Florin Coras05ecfcc2018-12-12 18:19:39 -08001381 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1382 "local: %U:%u", listen_session_handle, listen_vpp_handle,
1383 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001384 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001385 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001386 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001387 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001388 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001389 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001390 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1391 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001392
Florin Coras3c7d4f92018-12-14 11:28:43 -08001393 /*
1394 * Session might have been closed already
1395 */
1396 if (accept_flags)
1397 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001398 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -08001399 client_session->session_state = STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001400 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasb0f662f2018-12-27 14:51:46 -08001401 client_session->session_state = STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001402 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001403 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001404}
1405
1406int
Florin Coras134a9962018-08-28 11:32:04 -07001407vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001408{
Florin Coras134a9962018-08-28 11:32:04 -07001409 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001410 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001411 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001412 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001413
Florin Coras134a9962018-08-28 11:32:04 -07001414 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001415 if (!session)
1416 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001417 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001418
1419 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001420 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001421 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -07001422 "connect on an epoll session!", getpid (),
1423 session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001424 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001425 }
1426
Florin Coras7e12d942018-06-27 14:32:43 -07001427 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001428 {
Florin Coras7baeb712019-01-04 17:05:43 -08001429 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001430 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001431 session_handle, session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001432 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001433 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001434 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001435 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001436 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001437 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001438 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001439 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001440 }
1441
Florin Coras7e12d942018-06-27 14:32:43 -07001442 session->transport.is_ip4 = server_ep->is_ip4;
1443 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001444 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1445 sizeof (ip4_address_t));
Dave Wallaced239f8d2018-06-19 13:37:30 -04001446 else
Dave Barach178cf492018-11-13 16:34:13 -05001447 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1448 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001449 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04001450
Florin Coras7baeb712019-01-04 17:05:43 -08001451 VDBG (0, "session handle %u [0x%llx]: connecting to server %s %U "
1452 "port %d proto %s", session_handle, session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001453 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001454 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001455 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001456 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001457 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001458 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001459
Florin Coras070453d2018-08-24 17:04:27 -07001460 /*
1461 * Send connect request and wait for reply from vpp
1462 */
Florin Coras134a9962018-08-28 11:32:04 -07001463 vppcom_send_connect_sock (session);
Florin Coras070453d2018-08-24 17:04:27 -07001464 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1465 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001466
Florin Coras134a9962018-08-28 11:32:04 -07001467 session = vcl_session_get (wrk, session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04001468
Florin Coras070453d2018-08-24 17:04:27 -07001469 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001470 {
Dave Wallaceee45d412017-11-24 21:44:06 -05001471 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001472 {
1473 if (session)
Florin Coras0d427d82018-06-27 03:24:07 -07001474 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
Florin Coras134a9962018-08-28 11:32:04 -07001475 "failed! returning %d (%s)", getpid (),
1476 session->vpp_handle, session_handle, rv,
1477 vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001478 else
1479 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1480 "returning %d (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001481 session_handle, rv, vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001482 }
Dave Wallaceee45d412017-11-24 21:44:06 -05001483 }
Florin Coras0d427d82018-06-27 03:24:07 -07001484 else
1485 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
Florin Coras134a9962018-08-28 11:32:04 -07001486 getpid (), session->vpp_handle, session_handle);
Dave Wallaceee45d412017-11-24 21:44:06 -05001487
Dave Wallace4878cbe2017-11-21 03:45:09 -05001488 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001489}
1490
Florin Coras54693d22018-07-17 10:46:29 -07001491static u8
1492vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1493{
Florin Corasc0737e92019-03-04 14:19:39 -08001494 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001495}
1496
Steven58f464e2017-10-25 12:33:12 -07001497static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001498vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001499 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001500{
Florin Coras134a9962018-08-28 11:32:04 -07001501 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001502 int n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001503 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001504 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001505 svm_msg_q_msg_t msg;
1506 session_event_t *e;
1507 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001508 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001509
Florin Coras070453d2018-08-24 17:04:27 -07001510 if (PREDICT_FALSE (!buf))
1511 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001512
Florin Coras134a9962018-08-28 11:32:04 -07001513 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001514 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001515 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001516
Florin Coras6d0106e2019-01-29 20:11:58 -08001517 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001518 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001519 VDBG (0, "session handle %u[0x%llx] is not open! state 0x%x (%s)",
1520 s->session_index, s->vpp_handle, s->session_state,
1521 vppcom_session_state_str (s->session_state));
1522 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001523 }
1524
Florin Coras2cba8532018-09-11 16:33:36 -07001525 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001526 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001527 mq = wrk->app_event_queue;
1528 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001529 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001530
Florin Coras54693d22018-07-17 10:46:29 -07001531 if (svm_fifo_is_empty (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001532 {
Florin Coras54693d22018-07-17 10:46:29 -07001533 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001534 {
1535 svm_fifo_unset_event (s->rx_fifo);
1536 return VPPCOM_EWOULDBLOCK;
1537 }
Florin Coras41c9e042018-09-11 00:10:41 -07001538 while (svm_fifo_is_empty (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001539 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001540 if (vcl_session_is_closing (s))
1541 return vcl_session_closing_error (s);
1542
Florin Corasc0737e92019-03-04 14:19:39 -08001543 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001544 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001545 if (svm_msg_q_is_empty (mq))
1546 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001547
Florin Coras54693d22018-07-17 10:46:29 -07001548 svm_msg_q_sub_w_lock (mq, &msg);
1549 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001550 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001551 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001552 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001553 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001554 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001555 }
Florin Coras54693d22018-07-17 10:46:29 -07001556
Florin Coras460dce62018-07-27 05:45:06 -07001557 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001558 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001559 else
Florin Coras99368312018-08-02 10:45:44 -07001560 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001561
Florin Coras41c9e042018-09-11 00:10:41 -07001562 if (svm_fifo_is_empty (rx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001563 svm_fifo_unset_event (s->rx_fifo);
Florin Coras41c9e042018-09-11 00:10:41 -07001564
Florin Corasa7a1a222018-12-30 17:11:31 -08001565 VDBG (2, "vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1566 s->vpp_handle, session_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001567
Florin Coras54693d22018-07-17 10:46:29 -07001568 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001569}
1570
Steven58f464e2017-10-25 12:33:12 -07001571int
Florin Coras134a9962018-08-28 11:32:04 -07001572vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001573{
Florin Coras134a9962018-08-28 11:32:04 -07001574 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001575}
1576
1577static int
Florin Coras134a9962018-08-28 11:32:04 -07001578vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001579{
Florin Coras134a9962018-08-28 11:32:04 -07001580 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001581}
1582
Florin Coras2cba8532018-09-11 16:33:36 -07001583int
1584vppcom_session_read_segments (uint32_t session_handle,
1585 vppcom_data_segments_t ds)
1586{
1587 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001588 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001589 vcl_session_t *s = 0;
1590 svm_fifo_t *rx_fifo;
1591 svm_msg_q_msg_t msg;
1592 session_event_t *e;
1593 svm_msg_q_t *mq;
1594 u8 is_ct;
1595
1596 s = vcl_session_get_w_handle (wrk, session_handle);
1597 if (PREDICT_FALSE (!s || s->is_vep))
1598 return VPPCOM_EBADFD;
1599
Florin Coras6d0106e2019-01-29 20:11:58 -08001600 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1601 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001602
1603 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1604 is_ct = vcl_session_is_ct (s);
1605 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1606 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001607 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001608
Florin Coras653e43f2019-03-04 10:56:23 -08001609 if (is_ct)
1610 svm_fifo_unset_event (s->rx_fifo);
1611
Florin Coras2cba8532018-09-11 16:33:36 -07001612 if (svm_fifo_is_empty (rx_fifo))
1613 {
1614 if (is_nonblocking)
1615 {
1616 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001617 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001618 }
1619 while (svm_fifo_is_empty (rx_fifo))
1620 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001621 if (vcl_session_is_closing (s))
1622 return vcl_session_closing_error (s);
1623
Florin Coras2cba8532018-09-11 16:33:36 -07001624 svm_fifo_unset_event (rx_fifo);
1625 svm_msg_q_lock (mq);
1626 if (svm_msg_q_is_empty (mq))
1627 svm_msg_q_wait (mq);
1628
1629 svm_msg_q_sub_w_lock (mq, &msg);
1630 e = svm_msg_q_msg_data (mq, &msg);
1631 svm_msg_q_unlock (mq);
1632 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001633 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001634 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001635 }
1636 }
1637
1638 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1639 svm_fifo_unset_event (rx_fifo);
1640
Florin Coras2cba8532018-09-11 16:33:36 -07001641 return n_read;
1642}
1643
1644void
1645vppcom_session_free_segments (uint32_t session_handle,
1646 vppcom_data_segments_t ds)
1647{
1648 vcl_worker_t *wrk = vcl_worker_get_current ();
1649 vcl_session_t *s;
1650
1651 s = vcl_session_get_w_handle (wrk, session_handle);
1652 if (PREDICT_FALSE (!s || s->is_vep))
1653 return;
1654
1655 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1656}
1657
Florin Coras2cba8532018-09-11 16:33:36 -07001658int
1659vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1660{
1661 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001662 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001663 if (first_copy < max_bytes)
1664 {
Dave Barach178cf492018-11-13 16:34:13 -05001665 clib_memcpy_fast (buf + first_copy, ds[1].data,
1666 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001667 }
1668 return 0;
1669}
1670
Florin Coras54693d22018-07-17 10:46:29 -07001671static u8
1672vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1673{
Florin Corasc0737e92019-03-04 14:19:39 -08001674 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001675}
1676
Florin Coras42ceddb2018-12-12 10:56:01 -08001677static inline int
1678vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1679 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001680{
Florin Coras134a9962018-08-28 11:32:04 -07001681 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001682 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001683 vcl_session_t *s = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001684 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001685 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08001686 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001687 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001688 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001689 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001690
Florin Coras070453d2018-08-24 17:04:27 -07001691 if (PREDICT_FALSE (!buf))
1692 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001693
Florin Coras134a9962018-08-28 11:32:04 -07001694 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001695 if (PREDICT_FALSE (!s))
1696 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001697
Florin Coras460dce62018-07-27 05:45:06 -07001698 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001699 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001700 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1701 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001702 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001703 }
1704
Florin Coras6d0106e2019-01-29 20:11:58 -08001705 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001706 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001707 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1708 s->session_index, s->vpp_handle, s->session_state,
1709 vppcom_session_state_str (s->session_state));
1710 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001711 }
1712
Florin Coras0e88e852018-09-17 22:09:02 -07001713 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001714 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07001715 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras653e43f2019-03-04 10:56:23 -08001716 mq = wrk->app_event_queue;
Florin Coras54693d22018-07-17 10:46:29 -07001717 if (svm_fifo_is_full (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001718 {
Florin Coras54693d22018-07-17 10:46:29 -07001719 if (is_nonblocking)
1720 {
Florin Coras070453d2018-08-24 17:04:27 -07001721 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001722 }
Florin Coras60f1fc12018-08-16 14:57:31 -07001723 while (svm_fifo_is_full (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001724 {
Florin Coras1bcad5c2019-01-09 20:04:38 -08001725 svm_fifo_add_want_tx_ntf (tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001726 if (vcl_session_is_closing (s))
1727 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001728 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001729 if (svm_msg_q_is_empty (mq))
1730 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001731
Florin Coras54693d22018-07-17 10:46:29 -07001732 svm_msg_q_sub_w_lock (mq, &msg);
1733 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001734 svm_msg_q_unlock (mq);
1735
Florin Coras0e88e852018-09-17 22:09:02 -07001736 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001737 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001738 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001739 }
Dave Wallace543852a2017-08-03 02:11:34 -04001740 }
Dave Wallace543852a2017-08-03 02:11:34 -04001741
Florin Coras653e43f2019-03-04 10:56:23 -08001742 et = SESSION_IO_EVT_TX;
1743 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08001744 et = SESSION_IO_EVT_TX_FLUSH;
1745
Florin Coras460dce62018-07-27 05:45:06 -07001746 if (s->is_dgram)
1747 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08001748 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001749 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07001750 else
1751 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001752 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08001753
Florin Coras14ed6df2019-03-06 21:13:42 -08001754 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001755 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1756 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001757
Florin Coras460dce62018-07-27 05:45:06 -07001758 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001759
Florin Coras6d0106e2019-01-29 20:11:58 -08001760 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1761 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001762
Florin Coras54693d22018-07-17 10:46:29 -07001763 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001764}
1765
Florin Coras42ceddb2018-12-12 10:56:01 -08001766int
1767vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1768{
1769 return vppcom_session_write_inline (session_handle, buf, n,
1770 0 /* is_flush */ );
1771}
1772
Florin Corasb0f662f2018-12-27 14:51:46 -08001773int
1774vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1775{
1776 return vppcom_session_write_inline (session_handle, buf, n,
1777 1 /* is_flush */ );
1778}
1779
Florin Corasc0737e92019-03-04 14:19:39 -08001780#define vcl_fifo_rx_evt_valid_or_break(_s) \
1781if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1782 { \
1783 if (!vcl_session_is_ct (_s)) \
1784 { \
1785 svm_fifo_unset_event (_s->rx_fifo); \
1786 if (svm_fifo_is_empty (_s->rx_fifo)) \
1787 break; \
1788 } \
1789 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1790 { \
1791 svm_fifo_unset_event (_s->ct_rx_fifo); \
1792 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1793 break; \
1794 } \
1795 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07001796
Florin Coras86f04502018-09-12 16:08:01 -07001797static void
1798vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1799 unsigned long n_bits, unsigned long *read_map,
1800 unsigned long *write_map,
1801 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001802{
1803 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001804 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001805 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001806 u32 sid;
1807
1808 switch (e->event_type)
1809 {
Florin Corasc0737e92019-03-04 14:19:39 -08001810 case SESSION_IO_EVT_RX:
1811 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001812 session = vcl_session_get (wrk, sid);
1813 if (!session)
1814 break;
Florin Corasfff68f72019-03-13 16:01:38 -07001815 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07001816 if (sid < n_bits && read_map)
1817 {
David Johnsond9818dd2018-12-14 14:53:41 -05001818 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001819 *bits_set += 1;
1820 }
1821 break;
Florin Corasfe97da32019-03-06 10:09:04 -08001822 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08001823 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001824 session = vcl_session_get (wrk, sid);
1825 if (!session)
1826 break;
1827 if (sid < n_bits && write_map)
1828 {
David Johnsond9818dd2018-12-14 14:53:41 -05001829 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001830 *bits_set += 1;
1831 }
1832 break;
Florin Coras86f04502018-09-12 16:08:01 -07001833 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001834 session = vcl_session_accepted (wrk,
1835 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001836 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001837 break;
Florin Coras86f04502018-09-12 16:08:01 -07001838 sid = session->session_index;
1839 if (sid < n_bits && read_map)
1840 {
David Johnsond9818dd2018-12-14 14:53:41 -05001841 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001842 *bits_set += 1;
1843 }
1844 break;
1845 case SESSION_CTRL_EVT_CONNECTED:
1846 connected_msg = (session_connected_msg_t *) e->data;
1847 vcl_session_connected_handler (wrk, connected_msg);
1848 break;
1849 case SESSION_CTRL_EVT_DISCONNECTED:
1850 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001851 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1852 if (!session)
1853 break;
1854 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001855 if (sid < n_bits && except_map)
1856 {
David Johnsond9818dd2018-12-14 14:53:41 -05001857 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001858 *bits_set += 1;
1859 }
1860 break;
1861 case SESSION_CTRL_EVT_RESET:
1862 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1863 if (sid < n_bits && except_map)
1864 {
David Johnsond9818dd2018-12-14 14:53:41 -05001865 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001866 *bits_set += 1;
1867 }
1868 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001869 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1870 vcl_session_unlisten_reply_handler (wrk, e->data);
1871 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001872 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1873 vcl_session_worker_update_reply_handler (wrk, e->data);
1874 break;
1875 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1876 vcl_session_req_worker_update_handler (wrk, e->data);
1877 break;
Florin Coras86f04502018-09-12 16:08:01 -07001878 default:
1879 clib_warning ("unhandled: %u", e->event_type);
1880 break;
1881 }
1882}
1883
1884static int
1885vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1886 unsigned long n_bits, unsigned long *read_map,
1887 unsigned long *write_map, unsigned long *except_map,
1888 double time_to_wait, u32 * bits_set)
1889{
Florin Coras99368312018-08-02 10:45:44 -07001890 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001891 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001892 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001893
1894 svm_msg_q_lock (mq);
1895 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001896 {
Florin Coras54693d22018-07-17 10:46:29 -07001897 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001898 {
Florin Coras54693d22018-07-17 10:46:29 -07001899 svm_msg_q_unlock (mq);
1900 return 0;
1901 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001902
Florin Coras54693d22018-07-17 10:46:29 -07001903 if (!time_to_wait)
1904 {
1905 svm_msg_q_unlock (mq);
1906 return 0;
1907 }
1908 else if (time_to_wait < 0)
1909 {
1910 svm_msg_q_wait (mq);
1911 }
1912 else
1913 {
1914 if (svm_msg_q_timedwait (mq, time_to_wait))
1915 {
1916 svm_msg_q_unlock (mq);
1917 return 0;
1918 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001919 }
1920 }
Florin Coras134a9962018-08-28 11:32:04 -07001921 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07001922 svm_msg_q_unlock (mq);
1923
Florin Coras134a9962018-08-28 11:32:04 -07001924 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001925 {
Florin Coras134a9962018-08-28 11:32:04 -07001926 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07001927 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07001928 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1929 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001930 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001931 }
Florin Coras134a9962018-08-28 11:32:04 -07001932 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08001933 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001934 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001935}
1936
Florin Coras99368312018-08-02 10:45:44 -07001937static int
Florin Coras294afe22019-01-07 17:49:17 -08001938vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
1939 vcl_si_set * read_map, vcl_si_set * write_map,
1940 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001941 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001942{
Florin Coras14ed6df2019-03-06 21:13:42 -08001943 double wait = 0, start = 0;
1944
1945 if (!*bits_set)
1946 {
1947 wait = time_to_wait;
1948 start = clib_time_now (&wrk->clib_time);
1949 }
1950
1951 do
1952 {
1953 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1954 write_map, except_map, wait, bits_set);
1955 if (*bits_set)
1956 return *bits_set;
1957 if (wait == -1)
1958 continue;
1959
1960 wait = wait - (clib_time_now (&wrk->clib_time) - start);
1961 }
1962 while (wait > 0);
1963
1964 return 0;
Florin Coras99368312018-08-02 10:45:44 -07001965}
1966
1967static int
Florin Coras294afe22019-01-07 17:49:17 -08001968vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
1969 vcl_si_set * read_map, vcl_si_set * write_map,
1970 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001971 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001972{
1973 vcl_mq_evt_conn_t *mqc;
1974 int __clib_unused n_read;
1975 int n_mq_evts, i;
1976 u64 buf;
1977
Florin Coras134a9962018-08-28 11:32:04 -07001978 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1979 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1980 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07001981 for (i = 0; i < n_mq_evts; i++)
1982 {
Florin Coras134a9962018-08-28 11:32:04 -07001983 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07001984 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07001985 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07001986 except_map, 0, bits_set);
1987 }
1988
1989 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1990}
1991
Dave Wallace543852a2017-08-03 02:11:34 -04001992int
Florin Coras294afe22019-01-07 17:49:17 -08001993vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1994 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04001995{
Florin Coras54693d22018-07-17 10:46:29 -07001996 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001997 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001998 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07001999 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04002000
Dave Wallace7876d392017-10-19 03:53:57 -04002001 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002002 {
Florin Coras134a9962018-08-28 11:32:04 -07002003 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002004 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002005 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2006 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002007 }
Dave Wallace7876d392017-10-19 03:53:57 -04002008 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002009 {
Florin Coras134a9962018-08-28 11:32:04 -07002010 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002011 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002012 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2013 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002014 }
Dave Wallace7876d392017-10-19 03:53:57 -04002015 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002016 {
Florin Coras134a9962018-08-28 11:32:04 -07002017 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002018 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002019 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2020 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002021 }
2022
Florin Coras54693d22018-07-17 10:46:29 -07002023 if (!n_bits)
2024 return 0;
2025
2026 if (!write_map)
2027 goto check_rd;
2028
2029 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002030 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2031 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002032 {
Florin Coras47c40e22018-11-26 17:01:36 -08002033 if (except_map && sid < minbits)
2034 clib_bitmap_set_no_check (except_map, sid, 1);
2035 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002036 }
2037
2038 rv = svm_fifo_is_full (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002039 if (!rv)
2040 {
David Johnsond9818dd2018-12-14 14:53:41 -05002041 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002042 bits_set++;
2043 }
Florin Coras294afe22019-01-07 17:49:17 -08002044 else
Florin Coras1bcad5c2019-01-09 20:04:38 -08002045 svm_fifo_add_want_tx_ntf (session->tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002046 }));
2047
2048check_rd:
2049 if (!read_map)
2050 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002051
Florin Coras134a9962018-08-28 11:32:04 -07002052 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2053 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002054 {
Florin Coras47c40e22018-11-26 17:01:36 -08002055 if (except_map && sid < minbits)
2056 clib_bitmap_set_no_check (except_map, sid, 1);
2057 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002058 }
2059
Florin Coras0ef8ef22019-01-18 08:37:13 -08002060 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002061 if (rv)
2062 {
David Johnsond9818dd2018-12-14 14:53:41 -05002063 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002064 bits_set++;
2065 }
2066 }));
2067 /* *INDENT-ON* */
2068
2069check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002070
Florin Coras86f04502018-09-12 16:08:01 -07002071 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2072 {
2073 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2074 read_map, write_map, except_map, &bits_set);
2075 }
2076 vec_reset_length (wrk->unhandled_evts_vector);
2077
Florin Coras99368312018-08-02 10:45:44 -07002078 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002079 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002080 time_to_wait, &bits_set);
2081 else
Florin Coras134a9962018-08-28 11:32:04 -07002082 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002083 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002084
Dave Wallace543852a2017-08-03 02:11:34 -04002085 return (bits_set);
2086}
2087
Dave Wallacef7f809c2017-10-03 01:48:42 -04002088static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002089vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002090{
Florin Coras7e12d942018-06-27 14:32:43 -07002091 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002092 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002093 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002094
Florin Corasc0737e92019-03-04 14:19:39 -08002095 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002096 return;
2097
2098 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Florin Coras134a9962018-08-28 11:32:04 -07002099 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002100 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002101 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002102 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2103 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002104 goto done;
2105 }
2106 if (PREDICT_FALSE (!session->is_vep))
2107 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002108 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2109 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002110 goto done;
2111 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002112 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05002113 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002114 "{\n"
2115 " is_vep = %u\n"
2116 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002117 " next_sid = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002118 "}\n", getpid (), vep_idx,
2119 session->is_vep, session->is_vep_session,
Florin Corasac626262019-03-03 17:56:48 -08002120 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002121
Florin Coras134a9962018-08-28 11:32:04 -07002122 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002123 {
Florin Coras134a9962018-08-28 11:32:04 -07002124 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002125 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002126 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002127 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002128 goto done;
2129 }
2130 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05002131 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2132 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002133 else if (PREDICT_FALSE (!session->is_vep_session))
2134 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002135 clib_warning ("VCL<%d>: ERROR: session (%u) "
2136 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002137 goto done;
2138 }
2139 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002140 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05002141 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002142 "vep_idx (%u)!", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002143 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002144 if (session->is_vep_session)
2145 {
2146 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2147 "{\n"
2148 " next_sid = 0x%x (%u)\n"
2149 " prev_sid = 0x%x (%u)\n"
2150 " vep_idx = 0x%x (%u)\n"
2151 " ev.events = 0x%x\n"
2152 " ev.data.u64 = 0x%llx\n"
2153 " et_mask = 0x%x\n"
2154 "}\n",
2155 vep_idx, sid, sid,
Florin Coras134a9962018-08-28 11:32:04 -07002156 vep->next_sh, vep->next_sh,
2157 vep->prev_sh, vep->prev_sh,
2158 vep->vep_sh, vep->vep_sh,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002159 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002160 }
2161 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002162
2163done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002164 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2165 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002166}
2167
2168int
2169vppcom_epoll_create (void)
2170{
Florin Coras134a9962018-08-28 11:32:04 -07002171 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002172 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002173
Florin Coras134a9962018-08-28 11:32:04 -07002174 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002175
2176 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002177 vep_session->vep.vep_sh = ~0;
2178 vep_session->vep.next_sh = ~0;
2179 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002180 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002181
Florin Corasa7a1a222018-12-30 17:11:31 -08002182 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2183 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002184
Florin Corasab2f6db2018-08-31 14:31:41 -07002185 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002186}
2187
2188int
Florin Coras134a9962018-08-28 11:32:04 -07002189vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002190 struct epoll_event *event)
2191{
Florin Coras134a9962018-08-28 11:32:04 -07002192 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002193 vcl_session_t *vep_session;
2194 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002195 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002196
Florin Coras134a9962018-08-28 11:32:04 -07002197 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002198 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002199 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002200 return VPPCOM_EINVAL;
2201 }
2202
Florin Coras134a9962018-08-28 11:32:04 -07002203 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002204 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002205 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002206 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002207 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002208 }
2209 if (PREDICT_FALSE (!vep_session->is_vep))
2210 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002211 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002212 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002213 }
2214
Florin Coras134a9962018-08-28 11:32:04 -07002215 ASSERT (vep_session->vep.vep_sh == ~0);
2216 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002217
Florin Coras134a9962018-08-28 11:32:04 -07002218 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002219 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002220 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002221 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002222 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002223 }
2224 if (PREDICT_FALSE (session->is_vep))
2225 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002226 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002227 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002228 }
2229
2230 switch (op)
2231 {
2232 case EPOLL_CTL_ADD:
2233 if (PREDICT_FALSE (!event))
2234 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002235 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002236 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002237 }
Florin Coras134a9962018-08-28 11:32:04 -07002238 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002239 {
Florin Coras7e12d942018-06-27 14:32:43 -07002240 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002241 next_session = vcl_session_get_w_handle (wrk,
2242 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002243 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002244 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002245 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on "
2246 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002247 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002248 }
Florin Coras134a9962018-08-28 11:32:04 -07002249 ASSERT (next_session->vep.prev_sh == vep_handle);
2250 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002251 }
Florin Coras134a9962018-08-28 11:32:04 -07002252 session->vep.next_sh = vep_session->vep.next_sh;
2253 session->vep.prev_sh = vep_handle;
2254 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002255 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2256 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002257 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002258 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002259 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002260
Florin Coras1bcad5c2019-01-09 20:04:38 -08002261 if (session->tx_fifo)
2262 svm_fifo_add_want_tx_ntf (session->tx_fifo,
2263 SVM_FIFO_WANT_TX_NOTIF_IF_FULL);
2264
Florin Corasa7a1a222018-12-30 17:11:31 -08002265 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2266 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002267 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002268 break;
2269
2270 case EPOLL_CTL_MOD:
2271 if (PREDICT_FALSE (!event))
2272 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002273 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002274 rv = VPPCOM_EINVAL;
2275 goto done;
2276 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002277 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002278 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002279 VDBG (0, "sid %u EPOLL_CTL_MOD: not a vep session!",
2280 session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002281 rv = VPPCOM_EINVAL;
2282 goto done;
2283 }
Florin Coras134a9962018-08-28 11:32:04 -07002284 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002285 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002286 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2287 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002288 rv = VPPCOM_EINVAL;
2289 goto done;
2290 }
2291 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2292 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002293 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2294 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002295 break;
2296
2297 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002298 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002299 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002300 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002301 rv = VPPCOM_EINVAL;
2302 goto done;
2303 }
Florin Coras134a9962018-08-28 11:32:04 -07002304 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002305 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002306 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2307 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002308 rv = VPPCOM_EINVAL;
2309 goto done;
2310 }
2311
Florin Coras134a9962018-08-28 11:32:04 -07002312 if (session->vep.prev_sh == vep_handle)
2313 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002314 else
2315 {
Florin Coras7e12d942018-06-27 14:32:43 -07002316 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002317 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002318 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002319 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002320 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sid (%u) on sid (%u)!",
2321 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002322 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002323 }
Florin Coras134a9962018-08-28 11:32:04 -07002324 ASSERT (prev_session->vep.next_sh == session_handle);
2325 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002326 }
Florin Coras134a9962018-08-28 11:32:04 -07002327 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002328 {
Florin Coras7e12d942018-06-27 14:32:43 -07002329 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002330 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002331 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002332 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002333 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sid (%u) on sid (%u)!",
2334 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002335 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002336 }
Florin Coras134a9962018-08-28 11:32:04 -07002337 ASSERT (next_session->vep.prev_sh == session_handle);
2338 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002339 }
2340
2341 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002342 session->vep.next_sh = ~0;
2343 session->vep.prev_sh = ~0;
2344 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002345 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002346
2347 if (session->tx_fifo)
2348 svm_fifo_del_want_tx_ntf (session->tx_fifo, SVM_FIFO_NO_TX_NOTIF);
2349
Florin Corasa7a1a222018-12-30 17:11:31 -08002350 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_handle,
2351 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002352 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002353 break;
2354
2355 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002356 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002357 rv = VPPCOM_EINVAL;
2358 }
2359
Florin Coras134a9962018-08-28 11:32:04 -07002360 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002361
2362done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002363 return rv;
2364}
2365
Florin Coras86f04502018-09-12 16:08:01 -07002366static inline void
2367vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2368 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002369{
2370 session_disconnected_msg_t *disconnected_msg;
2371 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002372 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002373 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002374 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002375 u8 add_event = 0;
2376
2377 switch (e->event_type)
2378 {
Florin Coras653e43f2019-03-04 10:56:23 -08002379 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002380 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002381 if (!(session = vcl_session_get (wrk, sid)))
2382 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002383 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002384 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002385 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002386 break;
2387 add_event = 1;
2388 events[*num_ev].events |= EPOLLIN;
2389 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002390 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002391 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002392 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002393 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002394 if (!(session = vcl_session_get (wrk, sid)))
2395 break;
Florin Coras86f04502018-09-12 16:08:01 -07002396 session_events = session->vep.ev.events;
2397 if (!(EPOLLOUT & session_events))
2398 break;
2399 add_event = 1;
2400 events[*num_ev].events |= EPOLLOUT;
2401 session_evt_data = session->vep.ev.data.u64;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002402 svm_fifo_reset_tx_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002403 break;
Florin Coras86f04502018-09-12 16:08:01 -07002404 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002405 session = vcl_session_accepted (wrk,
2406 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002407 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002408 break;
Florin Coras86f04502018-09-12 16:08:01 -07002409
Florin Coras86f04502018-09-12 16:08:01 -07002410 session_events = session->vep.ev.events;
2411 if (!(EPOLLIN & session_events))
2412 break;
2413
2414 add_event = 1;
2415 events[*num_ev].events |= EPOLLIN;
2416 session_evt_data = session->vep.ev.data.u64;
2417 break;
2418 case SESSION_CTRL_EVT_CONNECTED:
2419 connected_msg = (session_connected_msg_t *) e->data;
2420 vcl_session_connected_handler (wrk, connected_msg);
2421 /* Generate EPOLLOUT because there's no connected event */
2422 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002423 if (!(session = vcl_session_get (wrk, sid)))
2424 break;
Florin Coras86f04502018-09-12 16:08:01 -07002425 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002426 if (!(EPOLLOUT & session_events))
2427 break;
2428 add_event = 1;
2429 events[*num_ev].events |= EPOLLOUT;
2430 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002431 break;
2432 case SESSION_CTRL_EVT_DISCONNECTED:
2433 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002434 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2435 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002436 break;
Florin Coras72f77822019-01-22 19:05:52 -08002437 session_events = session->vep.ev.events;
2438 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2439 break;
Florin Coras86f04502018-09-12 16:08:01 -07002440 add_event = 1;
2441 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2442 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002443 break;
2444 case SESSION_CTRL_EVT_RESET:
2445 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2446 if (!(session = vcl_session_get (wrk, sid)))
2447 break;
Florin Coras72f77822019-01-22 19:05:52 -08002448 session_events = session->vep.ev.events;
2449 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2450 break;
Florin Coras86f04502018-09-12 16:08:01 -07002451 add_event = 1;
2452 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2453 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002454 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002455 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2456 vcl_session_unlisten_reply_handler (wrk, e->data);
2457 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002458 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2459 vcl_session_req_worker_update_handler (wrk, e->data);
2460 break;
2461 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2462 vcl_session_worker_update_reply_handler (wrk, e->data);
2463 break;
Florin Coras86f04502018-09-12 16:08:01 -07002464 default:
2465 VDBG (0, "unhandled: %u", e->event_type);
2466 break;
2467 }
2468
2469 if (add_event)
2470 {
2471 events[*num_ev].data.u64 = session_evt_data;
2472 if (EPOLLONESHOT & session_events)
2473 {
2474 session = vcl_session_get (wrk, sid);
2475 session->vep.ev.events = 0;
2476 }
2477 *num_ev += 1;
2478 }
2479}
2480
2481static int
2482vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2483 struct epoll_event *events, u32 maxevents,
2484 double wait_for_time, u32 * num_ev)
2485{
Florin Coras99368312018-08-02 10:45:44 -07002486 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002487 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002488 int i;
2489
Florin Coras539663c2018-09-28 14:59:37 -07002490 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2491 goto handle_dequeued;
2492
Florin Coras54693d22018-07-17 10:46:29 -07002493 svm_msg_q_lock (mq);
2494 if (svm_msg_q_is_empty (mq))
2495 {
2496 if (!wait_for_time)
2497 {
2498 svm_msg_q_unlock (mq);
2499 return 0;
2500 }
2501 else if (wait_for_time < 0)
2502 {
2503 svm_msg_q_wait (mq);
2504 }
2505 else
2506 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002507 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002508 {
2509 svm_msg_q_unlock (mq);
2510 return 0;
2511 }
2512 }
2513 }
Florin Coras134a9962018-08-28 11:32:04 -07002514 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002515 svm_msg_q_unlock (mq);
2516
Florin Coras539663c2018-09-28 14:59:37 -07002517handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002518 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002519 {
Florin Coras134a9962018-08-28 11:32:04 -07002520 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002521 e = svm_msg_q_msg_data (mq, msg);
Florin Corasaa27eb92018-10-13 12:20:01 -07002522 if (*num_ev < maxevents)
2523 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2524 else
2525 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras99368312018-08-02 10:45:44 -07002526 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002527 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002528 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002529 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002530 return *num_ev;
2531}
2532
Florin Coras99368312018-08-02 10:45:44 -07002533static int
Florin Coras134a9962018-08-28 11:32:04 -07002534vppcom_epoll_wait_condvar (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{
Florin Coras14ed6df2019-03-06 21:13:42 -08002537 double wait = 0, start = 0;
2538
2539 if (!n_evts)
2540 {
2541 wait = wait_for_time;
2542 start = clib_time_now (&wrk->clib_time);
2543 }
2544
2545 do
2546 {
2547 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2548 wait, &n_evts);
2549 if (n_evts)
2550 return n_evts;
2551 if (wait == -1)
2552 continue;
2553
2554 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2555 }
2556 while (wait > 0);
2557
2558 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002559}
2560
2561static int
Florin Coras134a9962018-08-28 11:32:04 -07002562vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002563 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002564{
2565 vcl_mq_evt_conn_t *mqc;
2566 int __clib_unused n_read;
2567 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002568 u64 buf;
2569
Florin Coras134a9962018-08-28 11:32:04 -07002570 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002571again:
Florin Coras134a9962018-08-28 11:32:04 -07002572 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2573 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002574 for (i = 0; i < n_mq_evts; i++)
2575 {
Florin Coras134a9962018-08-28 11:32:04 -07002576 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002577 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002578 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002579 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002580 if (!n_evts && n_mq_evts > 0)
2581 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002582
2583 return (int) n_evts;
2584}
2585
Dave Wallacef7f809c2017-10-03 01:48:42 -04002586int
Florin Coras134a9962018-08-28 11:32:04 -07002587vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002588 int maxevents, double wait_for_time)
2589{
Florin Coras134a9962018-08-28 11:32:04 -07002590 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002591 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002592 u32 n_evts = 0;
2593 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002594
2595 if (PREDICT_FALSE (maxevents <= 0))
2596 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002597 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002598 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002599 return VPPCOM_EINVAL;
2600 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002601
Florin Coras134a9962018-08-28 11:32:04 -07002602 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002603 if (!vep_session)
2604 return VPPCOM_EBADFD;
2605
Florin Coras54693d22018-07-17 10:46:29 -07002606 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002607 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002608 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Florin Coras134a9962018-08-28 11:32:04 -07002609 getpid (), vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002610 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002611 }
Florin Coras54693d22018-07-17 10:46:29 -07002612
2613 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002614
Florin Coras86f04502018-09-12 16:08:01 -07002615 if (vec_len (wrk->unhandled_evts_vector))
2616 {
2617 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2618 {
2619 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2620 events, &n_evts);
2621 if (n_evts == maxevents)
2622 {
2623 i += 1;
2624 break;
2625 }
2626 }
Florin Coras86f04502018-09-12 16:08:01 -07002627 vec_delete (wrk->unhandled_evts_vector, i, 0);
2628 }
2629
2630 if (vcm->cfg.use_mq_eventfd)
2631 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2632 wait_for_time);
2633
2634 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2635 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002636}
2637
Dave Wallace35830af2017-10-09 01:43:42 -04002638int
Florin Coras134a9962018-08-28 11:32:04 -07002639vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002640 void *buffer, uint32_t * buflen)
2641{
Florin Coras134a9962018-08-28 11:32:04 -07002642 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002643 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002644 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002645 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002646 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002647
Florin Coras134a9962018-08-28 11:32:04 -07002648 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002649 if (!session)
2650 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002651
Dave Wallace35830af2017-10-09 01:43:42 -04002652 switch (op)
2653 {
2654 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002655 rv = vcl_session_read_ready (session);
Florin Coras7baeb712019-01-04 17:05:43 -08002656 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d", rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002657 break;
2658
Dave Wallace227867f2017-11-13 21:21:53 -05002659 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002660 rv = vcl_session_write_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002661 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
Florin Coras134a9962018-08-28 11:32:04 -07002662 getpid (), session_handle, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002663 break;
2664
2665 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002666 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002667 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002668 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2669 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002670 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07002671 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2672 "is_nonblocking = %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002673 session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002674 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002675 }
2676 else
2677 rv = VPPCOM_EINVAL;
2678 break;
2679
2680 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002681 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002682 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002683 if (*flags & O_NONBLOCK)
2684 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2685 else
2686 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2687
Florin Coras0d427d82018-06-27 03:24:07 -07002688 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2689 " is_nonblocking = %u",
Florin Coras134a9962018-08-28 11:32:04 -07002690 getpid (), session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002691 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002692 }
2693 else
2694 rv = VPPCOM_EINVAL;
2695 break;
2696
2697 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002698 if (PREDICT_TRUE (buffer && buflen &&
2699 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002700 {
Florin Coras7e12d942018-06-27 14:32:43 -07002701 ep->is_ip4 = session->transport.is_ip4;
2702 ep->port = session->transport.rmt_port;
2703 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002704 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2705 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002706 else
Dave Barach178cf492018-11-13 16:34:13 -05002707 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2708 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002709 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002710 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2711 "addr = %U, port %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002712 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002713 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002714 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2715 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002716 }
2717 else
2718 rv = VPPCOM_EINVAL;
2719 break;
2720
2721 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002722 if (PREDICT_TRUE (buffer && buflen &&
2723 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002724 {
Florin Coras7e12d942018-06-27 14:32:43 -07002725 ep->is_ip4 = session->transport.is_ip4;
2726 ep->port = session->transport.lcl_port;
2727 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002728 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2729 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002730 else
Dave Barach178cf492018-11-13 16:34:13 -05002731 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2732 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002733 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002734 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2735 " addr = %U port %d", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002736 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002737 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002738 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2739 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002740 }
2741 else
2742 rv = VPPCOM_EINVAL;
2743 break;
Stevenb5a11602017-10-11 09:59:30 -07002744
Dave Wallace048b1d62018-01-03 22:24:41 -05002745 case VPPCOM_ATTR_GET_LIBC_EPFD:
2746 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07002747 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2748 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002749 break;
2750
2751 case VPPCOM_ATTR_SET_LIBC_EPFD:
2752 if (PREDICT_TRUE (buffer && buflen &&
2753 (*buflen == sizeof (session->libc_epfd))))
2754 {
2755 session->libc_epfd = *(int *) buffer;
2756 *buflen = sizeof (session->libc_epfd);
2757
Florin Coras0d427d82018-06-27 03:24:07 -07002758 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2759 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002760 }
2761 else
2762 rv = VPPCOM_EINVAL;
2763 break;
2764
2765 case VPPCOM_ATTR_GET_PROTOCOL:
2766 if (buffer && buflen && (*buflen >= sizeof (int)))
2767 {
Florin Coras7e12d942018-06-27 14:32:43 -07002768 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002769 *buflen = sizeof (int);
2770
Florin Coras0d427d82018-06-27 03:24:07 -07002771 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2772 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2773 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002774 }
2775 else
2776 rv = VPPCOM_EINVAL;
2777 break;
2778
2779 case VPPCOM_ATTR_GET_LISTEN:
2780 if (buffer && buflen && (*buflen >= sizeof (int)))
2781 {
2782 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2783 VCL_SESS_ATTR_LISTEN);
2784 *buflen = sizeof (int);
2785
Florin Coras0d427d82018-06-27 03:24:07 -07002786 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2787 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002788 }
2789 else
2790 rv = VPPCOM_EINVAL;
2791 break;
2792
2793 case VPPCOM_ATTR_GET_ERROR:
2794 if (buffer && buflen && (*buflen >= sizeof (int)))
2795 {
2796 *(int *) buffer = 0;
2797 *buflen = sizeof (int);
2798
Florin Coras0d427d82018-06-27 03:24:07 -07002799 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2800 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002801 }
2802 else
2803 rv = VPPCOM_EINVAL;
2804 break;
2805
2806 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2807 if (buffer && buflen && (*buflen >= sizeof (u32)))
2808 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002809
2810 /* VPP-TBD */
2811 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002812 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002813 vcm->cfg.tx_fifo_size);
2814 *buflen = sizeof (u32);
2815
Florin Coras0d427d82018-06-27 03:24:07 -07002816 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2817 "buflen %d, #VPP-TBD#", getpid (),
2818 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002819 }
2820 else
2821 rv = VPPCOM_EINVAL;
2822 break;
2823
2824 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2825 if (buffer && buflen && (*buflen == sizeof (u32)))
2826 {
2827 /* VPP-TBD */
2828 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002829 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2830 "buflen %d, #VPP-TBD#", getpid (),
2831 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002832 }
2833 else
2834 rv = VPPCOM_EINVAL;
2835 break;
2836
2837 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2838 if (buffer && buflen && (*buflen >= sizeof (u32)))
2839 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002840
2841 /* VPP-TBD */
2842 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002843 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002844 vcm->cfg.rx_fifo_size);
2845 *buflen = sizeof (u32);
2846
Florin Coras0d427d82018-06-27 03:24:07 -07002847 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2848 "buflen %d, #VPP-TBD#", getpid (),
2849 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002850 }
2851 else
2852 rv = VPPCOM_EINVAL;
2853 break;
2854
2855 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2856 if (buffer && buflen && (*buflen == sizeof (u32)))
2857 {
2858 /* VPP-TBD */
2859 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002860 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2861 "buflen %d, #VPP-TBD#", getpid (),
2862 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002863 }
2864 else
2865 rv = VPPCOM_EINVAL;
2866 break;
2867
2868 case VPPCOM_ATTR_GET_REUSEADDR:
2869 if (buffer && buflen && (*buflen >= sizeof (int)))
2870 {
2871 /* VPP-TBD */
2872 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2873 VCL_SESS_ATTR_REUSEADDR);
2874 *buflen = sizeof (int);
2875
Florin Coras0d427d82018-06-27 03:24:07 -07002876 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2877 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002878 }
2879 else
2880 rv = VPPCOM_EINVAL;
2881 break;
2882
Stevenb5a11602017-10-11 09:59:30 -07002883 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002884 if (buffer && buflen && (*buflen == sizeof (int)) &&
2885 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2886 {
2887 /* VPP-TBD */
2888 if (*(int *) buffer)
2889 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2890 else
2891 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2892
Florin Coras0d427d82018-06-27 03:24:07 -07002893 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2894 " #VPP-TBD#", getpid (),
2895 VCL_SESS_ATTR_TEST (session->attr,
2896 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002897 }
2898 else
2899 rv = VPPCOM_EINVAL;
2900 break;
2901
2902 case VPPCOM_ATTR_GET_REUSEPORT:
2903 if (buffer && buflen && (*buflen >= sizeof (int)))
2904 {
2905 /* VPP-TBD */
2906 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2907 VCL_SESS_ATTR_REUSEPORT);
2908 *buflen = sizeof (int);
2909
Florin Coras0d427d82018-06-27 03:24:07 -07002910 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2911 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002912 }
2913 else
2914 rv = VPPCOM_EINVAL;
2915 break;
2916
2917 case VPPCOM_ATTR_SET_REUSEPORT:
2918 if (buffer && buflen && (*buflen == sizeof (int)) &&
2919 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2920 {
2921 /* VPP-TBD */
2922 if (*(int *) buffer)
2923 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2924 else
2925 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2926
Florin Coras0d427d82018-06-27 03:24:07 -07002927 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2928 " #VPP-TBD#", getpid (),
2929 VCL_SESS_ATTR_TEST (session->attr,
2930 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002931 }
2932 else
2933 rv = VPPCOM_EINVAL;
2934 break;
2935
2936 case VPPCOM_ATTR_GET_BROADCAST:
2937 if (buffer && buflen && (*buflen >= sizeof (int)))
2938 {
2939 /* VPP-TBD */
2940 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2941 VCL_SESS_ATTR_BROADCAST);
2942 *buflen = sizeof (int);
2943
Florin Coras0d427d82018-06-27 03:24:07 -07002944 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2945 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002946 }
2947 else
2948 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002949 break;
2950
2951 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002952 if (buffer && buflen && (*buflen == sizeof (int)))
2953 {
2954 /* VPP-TBD */
2955 if (*(int *) buffer)
2956 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2957 else
2958 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2959
Florin Coras0d427d82018-06-27 03:24:07 -07002960 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2961 "#VPP-TBD#", getpid (),
2962 VCL_SESS_ATTR_TEST (session->attr,
2963 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002964 }
2965 else
2966 rv = VPPCOM_EINVAL;
2967 break;
2968
2969 case VPPCOM_ATTR_GET_V6ONLY:
2970 if (buffer && buflen && (*buflen >= sizeof (int)))
2971 {
2972 /* VPP-TBD */
2973 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2974 VCL_SESS_ATTR_V6ONLY);
2975 *buflen = sizeof (int);
2976
Florin Coras0d427d82018-06-27 03:24:07 -07002977 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2978 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002979 }
2980 else
2981 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002982 break;
2983
2984 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002985 if (buffer && buflen && (*buflen == sizeof (int)))
2986 {
2987 /* VPP-TBD */
2988 if (*(int *) buffer)
2989 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2990 else
2991 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2992
Florin Coras0d427d82018-06-27 03:24:07 -07002993 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2994 "#VPP-TBD#", getpid (),
2995 VCL_SESS_ATTR_TEST (session->attr,
2996 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002997 }
2998 else
2999 rv = VPPCOM_EINVAL;
3000 break;
3001
3002 case VPPCOM_ATTR_GET_KEEPALIVE:
3003 if (buffer && buflen && (*buflen >= sizeof (int)))
3004 {
3005 /* VPP-TBD */
3006 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3007 VCL_SESS_ATTR_KEEPALIVE);
3008 *buflen = sizeof (int);
3009
Florin Coras0d427d82018-06-27 03:24:07 -07003010 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3011 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003012 }
3013 else
3014 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003015 break;
Stevenbccd3392017-10-12 20:42:21 -07003016
3017 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003018 if (buffer && buflen && (*buflen == sizeof (int)))
3019 {
3020 /* VPP-TBD */
3021 if (*(int *) buffer)
3022 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3023 else
3024 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3025
Florin Coras0d427d82018-06-27 03:24:07 -07003026 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3027 "#VPP-TBD#", getpid (),
3028 VCL_SESS_ATTR_TEST (session->attr,
3029 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003030 }
3031 else
3032 rv = VPPCOM_EINVAL;
3033 break;
3034
3035 case VPPCOM_ATTR_GET_TCP_NODELAY:
3036 if (buffer && buflen && (*buflen >= sizeof (int)))
3037 {
3038 /* VPP-TBD */
3039 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3040 VCL_SESS_ATTR_TCP_NODELAY);
3041 *buflen = sizeof (int);
3042
Florin Coras0d427d82018-06-27 03:24:07 -07003043 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3044 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003045 }
3046 else
3047 rv = VPPCOM_EINVAL;
3048 break;
3049
3050 case VPPCOM_ATTR_SET_TCP_NODELAY:
3051 if (buffer && buflen && (*buflen == sizeof (int)))
3052 {
3053 /* VPP-TBD */
3054 if (*(int *) buffer)
3055 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3056 else
3057 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3058
Florin Coras0d427d82018-06-27 03:24:07 -07003059 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3060 "#VPP-TBD#", getpid (),
3061 VCL_SESS_ATTR_TEST (session->attr,
3062 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003063 }
3064 else
3065 rv = VPPCOM_EINVAL;
3066 break;
3067
3068 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3069 if (buffer && buflen && (*buflen >= sizeof (int)))
3070 {
3071 /* VPP-TBD */
3072 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3073 VCL_SESS_ATTR_TCP_KEEPIDLE);
3074 *buflen = sizeof (int);
3075
Florin Coras0d427d82018-06-27 03:24:07 -07003076 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3077 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003078 }
3079 else
3080 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003081 break;
3082
3083 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003084 if (buffer && buflen && (*buflen == sizeof (int)))
3085 {
3086 /* VPP-TBD */
3087 if (*(int *) buffer)
3088 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3089 else
3090 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3091
Florin Coras0d427d82018-06-27 03:24:07 -07003092 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3093 "#VPP-TBD#", getpid (),
3094 VCL_SESS_ATTR_TEST (session->attr,
3095 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003096 }
3097 else
3098 rv = VPPCOM_EINVAL;
3099 break;
3100
3101 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3102 if (buffer && buflen && (*buflen >= sizeof (int)))
3103 {
3104 /* VPP-TBD */
3105 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3106 VCL_SESS_ATTR_TCP_KEEPINTVL);
3107 *buflen = sizeof (int);
3108
Florin Coras0d427d82018-06-27 03:24:07 -07003109 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3110 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003111 }
3112 else
3113 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003114 break;
3115
3116 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003117 if (buffer && buflen && (*buflen == sizeof (int)))
3118 {
3119 /* VPP-TBD */
3120 if (*(int *) buffer)
3121 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3122 else
3123 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3124
Florin Coras0d427d82018-06-27 03:24:07 -07003125 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3126 "#VPP-TBD#", getpid (),
3127 VCL_SESS_ATTR_TEST (session->attr,
3128 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003129 }
3130 else
3131 rv = VPPCOM_EINVAL;
3132 break;
3133
3134 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3135 if (buffer && buflen && (*buflen >= sizeof (u32)))
3136 {
3137 /* VPP-TBD */
3138 *(u32 *) buffer = session->user_mss;
3139 *buflen = sizeof (int);
3140
Florin Coras0d427d82018-06-27 03:24:07 -07003141 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3142 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003143 }
3144 else
3145 rv = VPPCOM_EINVAL;
3146 break;
3147
3148 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3149 if (buffer && buflen && (*buflen == sizeof (u32)))
3150 {
3151 /* VPP-TBD */
3152 session->user_mss = *(u32 *) buffer;
3153
Florin Coras0d427d82018-06-27 03:24:07 -07003154 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3155 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003156 }
3157 else
3158 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003159 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003160
Florin Coras7baeb712019-01-04 17:05:43 -08003161 case VPPCOM_ATTR_SET_SHUT:
3162 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3163 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3164 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3165 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3166 break;
3167
3168 case VPPCOM_ATTR_GET_SHUT:
3169 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3170 tmp_flags = 1;
3171 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3172 tmp_flags |= 2;
3173 if (tmp_flags == 1)
3174 *(int *) buffer = SHUT_RD;
3175 else if (tmp_flags == 2)
3176 *(int *) buffer = SHUT_WR;
3177 else if (tmp_flags == 3)
3178 *(int *) buffer = SHUT_RDWR;
3179 *buflen = sizeof (int);
3180 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003181 default:
3182 rv = VPPCOM_EINVAL;
3183 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003184 }
3185
Dave Wallace35830af2017-10-09 01:43:42 -04003186 return rv;
3187}
3188
Stevenac1f96d2017-10-24 16:03:58 -07003189int
Florin Coras134a9962018-08-28 11:32:04 -07003190vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003191 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3192{
Florin Coras134a9962018-08-28 11:32:04 -07003193 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003194 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003195 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003196
3197 if (ep)
3198 {
Florin Coras134a9962018-08-28 11:32:04 -07003199 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003200 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003201 {
Florin Coras0d427d82018-06-27 03:24:07 -07003202 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
Florin Coras134a9962018-08-28 11:32:04 -07003203 getpid (), session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003204 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003205 }
Florin Coras7e12d942018-06-27 14:32:43 -07003206 ep->is_ip4 = session->transport.is_ip4;
3207 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003208 }
Steven58f464e2017-10-25 12:33:12 -07003209
3210 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003211 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003212 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003213 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003214 else
3215 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003216 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003217 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003218 }
3219
Florin Coras99368312018-08-02 10:45:44 -07003220 if (ep)
3221 {
3222 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003223 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3224 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003225 else
Dave Barach178cf492018-11-13 16:34:13 -05003226 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3227 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003228 }
Florin Coras460dce62018-07-27 05:45:06 -07003229
Stevenac1f96d2017-10-24 16:03:58 -07003230 return rv;
3231}
3232
3233int
Florin Coras134a9962018-08-28 11:32:04 -07003234vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003235 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3236{
Dave Wallace617dffa2017-10-26 14:47:06 -04003237 if (!buffer)
3238 return VPPCOM_EINVAL;
3239
3240 if (ep)
3241 {
3242 // TBD
3243 return VPPCOM_EINVAL;
3244 }
3245
3246 if (flags)
3247 {
3248 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07003249 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3250 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003251 }
3252
Florin Coras42ceddb2018-12-12 10:56:01 -08003253 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003254}
3255
Dave Wallace048b1d62018-01-03 22:24:41 -05003256int
3257vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3258{
Florin Coras134a9962018-08-28 11:32:04 -07003259 vcl_worker_t *wrk = vcl_worker_get_current ();
3260 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003261 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003262 svm_msg_q_msg_t msg;
3263 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003264 int rv, num_ev = 0;
3265
Florin Coras0d427d82018-06-27 03:24:07 -07003266 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3267 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003268
3269 if (!vp)
3270 return VPPCOM_EFAULT;
3271
3272 do
3273 {
Florin Coras7e12d942018-06-27 14:32:43 -07003274 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003275
Florin Coras6917b942018-11-13 22:44:54 -08003276 /* Dequeue all events and drop all unhandled io events */
3277 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3278 {
3279 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3280 vcl_handle_mq_event (wrk, e);
3281 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3282 }
3283 vec_reset_length (wrk->unhandled_evts_vector);
3284
Dave Wallace048b1d62018-01-03 22:24:41 -05003285 for (i = 0; i < n_sids; i++)
3286 {
Florin Coras7baeb712019-01-04 17:05:43 -08003287 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003288 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003289 {
3290 vp[i].revents = POLLHUP;
3291 num_ev++;
3292 continue;
3293 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003294
Florin Coras6917b942018-11-13 22:44:54 -08003295 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003296
3297 if (POLLIN & vp[i].events)
3298 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003299 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003300 if (rv > 0)
3301 {
Florin Coras6917b942018-11-13 22:44:54 -08003302 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003303 num_ev++;
3304 }
3305 else if (rv < 0)
3306 {
3307 switch (rv)
3308 {
3309 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003310 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003311 break;
3312
3313 default:
Florin Coras6917b942018-11-13 22:44:54 -08003314 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003315 break;
3316 }
3317 num_ev++;
3318 }
3319 }
3320
3321 if (POLLOUT & vp[i].events)
3322 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003323 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003324 if (rv > 0)
3325 {
Florin Coras6917b942018-11-13 22:44:54 -08003326 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003327 num_ev++;
3328 }
3329 else if (rv < 0)
3330 {
3331 switch (rv)
3332 {
3333 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003334 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003335 break;
3336
3337 default:
Florin Coras6917b942018-11-13 22:44:54 -08003338 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003339 break;
3340 }
3341 num_ev++;
3342 }
3343 }
3344
Dave Wallace7e607a72018-06-18 18:41:32 -04003345 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003346 {
Florin Coras6917b942018-11-13 22:44:54 -08003347 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003348 num_ev++;
3349 }
3350 }
3351 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003352 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003353 }
3354 while ((num_ev == 0) && keep_trying);
3355
3356 if (VPPCOM_DEBUG > 3)
3357 {
3358 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3359 for (i = 0; i < n_sids; i++)
3360 {
3361 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
Florin Coras7baeb712019-01-04 17:05:43 -08003362 ".revents 0x%x", getpid (), i, vp[i].sh, vp[i].sh,
Florin Coras6917b942018-11-13 22:44:54 -08003363 vp[i].events, vp[i].revents);
Dave Wallace048b1d62018-01-03 22:24:41 -05003364 }
3365 }
3366 return num_ev;
3367}
3368
Florin Coras99368312018-08-02 10:45:44 -07003369int
3370vppcom_mq_epoll_fd (void)
3371{
Florin Coras134a9962018-08-28 11:32:04 -07003372 vcl_worker_t *wrk = vcl_worker_get_current ();
3373 return wrk->mqs_epfd;
3374}
3375
3376int
Florin Coras30e79c22019-01-02 19:31:22 -08003377vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003378{
3379 return session_handle & 0xFFFFFF;
3380}
3381
3382int
Florin Coras30e79c22019-01-02 19:31:22 -08003383vppcom_session_worker (vcl_session_handle_t session_handle)
3384{
3385 return session_handle >> 24;
3386}
3387
3388int
Florin Coras134a9962018-08-28 11:32:04 -07003389vppcom_worker_register (void)
3390{
Florin Coras47c40e22018-11-26 17:01:36 -08003391 if (!vcl_worker_alloc_and_init ())
3392 return VPPCOM_EEXIST;
3393
3394 if (vcl_worker_set_bapi ())
3395 return VPPCOM_EEXIST;
3396
3397 if (vcl_worker_register_with_vpp ())
3398 return VPPCOM_EEXIST;
3399
3400 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003401}
3402
Florin Corasdfe4cf42018-11-28 22:13:45 -08003403int
3404vppcom_worker_index (void)
3405{
3406 return vcl_get_worker_index ();
3407}
3408
Florin Corase0982e52019-01-25 13:19:56 -08003409int
3410vppcom_worker_mqs_epfd (void)
3411{
3412 vcl_worker_t *wrk = vcl_worker_get_current ();
3413 if (!vcm->cfg.use_mq_eventfd)
3414 return -1;
3415 return wrk->mqs_epfd;
3416}
3417
Dave Wallacee22aa742017-10-20 12:30:38 -04003418/*
3419 * fd.io coding-style-patch-verification: ON
3420 *
3421 * Local Variables:
3422 * eval: (c-set-style "gnu")
3423 * End:
3424 */