blob: 3abde98288a0b94d791a1f7934fc474c93cc5583 [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:
652 session = vcl_session_get (wrk, e->fifo->client_session_index);
653 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 Coras653e43f2019-03-04 10:56:23 -08001494 return (e->event_type == SESSION_IO_EVT_RX
1495 && e->fifo->client_session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001496}
1497
Steven58f464e2017-10-25 12:33:12 -07001498static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001499vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001500 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001501{
Florin Coras134a9962018-08-28 11:32:04 -07001502 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001503 int n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001504 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001505 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001506 svm_msg_q_msg_t msg;
1507 session_event_t *e;
1508 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001509 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001510
Florin Coras070453d2018-08-24 17:04:27 -07001511 if (PREDICT_FALSE (!buf))
1512 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001513
Florin Coras134a9962018-08-28 11:32:04 -07001514 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001515 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001516 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001517
Florin Coras6d0106e2019-01-29 20:11:58 -08001518 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001519 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001520 VDBG (0, "session handle %u[0x%llx] is not open! state 0x%x (%s)",
1521 s->session_index, s->vpp_handle, s->session_state,
1522 vppcom_session_state_str (s->session_state));
1523 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001524 }
1525
Florin Coras2cba8532018-09-11 16:33:36 -07001526 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001527 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001528 mq = wrk->app_event_queue;
1529 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001530 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001531
Florin Coras653e43f2019-03-04 10:56:23 -08001532 if (is_ct)
1533 svm_fifo_unset_event (s->rx_fifo);
1534
Florin Coras54693d22018-07-17 10:46:29 -07001535 if (svm_fifo_is_empty (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001536 {
Florin Coras653e43f2019-03-04 10:56:23 -08001537 svm_fifo_unset_event (rx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07001538 if (is_nonblocking)
Florin Coras653e43f2019-03-04 10:56:23 -08001539 return VPPCOM_EWOULDBLOCK;
Florin Coras41c9e042018-09-11 00:10:41 -07001540 while (svm_fifo_is_empty (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001541 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001542 if (vcl_session_is_closing (s))
1543 return vcl_session_closing_error (s);
1544
Florin Coras41c9e042018-09-11 00:10:41 -07001545 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001546 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001547 if (svm_msg_q_is_empty (mq))
1548 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001549
Florin Coras54693d22018-07-17 10:46:29 -07001550 svm_msg_q_sub_w_lock (mq, &msg);
1551 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001552 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001553 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras653e43f2019-03-04 10:56:23 -08001554 {
1555 clib_warning ("THIS ONE type %u", e->event_type);
1556 vcl_handle_mq_event (wrk, e);
1557 }
Florin Coras54693d22018-07-17 10:46:29 -07001558 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001559 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001560 }
Florin Coras54693d22018-07-17 10:46:29 -07001561
Florin Coras460dce62018-07-27 05:45:06 -07001562 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001563 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001564 else
Florin Coras99368312018-08-02 10:45:44 -07001565 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001566
Florin Coras41c9e042018-09-11 00:10:41 -07001567 if (svm_fifo_is_empty (rx_fifo))
1568 svm_fifo_unset_event (rx_fifo);
1569
Florin Corasa7a1a222018-12-30 17:11:31 -08001570 VDBG (2, "vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1571 s->vpp_handle, session_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001572
Florin Coras54693d22018-07-17 10:46:29 -07001573 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001574}
1575
Steven58f464e2017-10-25 12:33:12 -07001576int
Florin Coras134a9962018-08-28 11:32:04 -07001577vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001578{
Florin Coras134a9962018-08-28 11:32:04 -07001579 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001580}
1581
1582static int
Florin Coras134a9962018-08-28 11:32:04 -07001583vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001584{
Florin Coras134a9962018-08-28 11:32:04 -07001585 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001586}
1587
Florin Coras2cba8532018-09-11 16:33:36 -07001588int
1589vppcom_session_read_segments (uint32_t session_handle,
1590 vppcom_data_segments_t ds)
1591{
1592 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001593 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001594 vcl_session_t *s = 0;
1595 svm_fifo_t *rx_fifo;
1596 svm_msg_q_msg_t msg;
1597 session_event_t *e;
1598 svm_msg_q_t *mq;
1599 u8 is_ct;
1600
1601 s = vcl_session_get_w_handle (wrk, session_handle);
1602 if (PREDICT_FALSE (!s || s->is_vep))
1603 return VPPCOM_EBADFD;
1604
Florin Coras6d0106e2019-01-29 20:11:58 -08001605 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1606 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001607
1608 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1609 is_ct = vcl_session_is_ct (s);
1610 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1611 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001612 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001613
Florin Coras653e43f2019-03-04 10:56:23 -08001614 if (is_ct)
1615 svm_fifo_unset_event (s->rx_fifo);
1616
Florin Coras2cba8532018-09-11 16:33:36 -07001617 if (svm_fifo_is_empty (rx_fifo))
1618 {
1619 if (is_nonblocking)
1620 {
1621 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001622 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001623 }
1624 while (svm_fifo_is_empty (rx_fifo))
1625 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001626 if (vcl_session_is_closing (s))
1627 return vcl_session_closing_error (s);
1628
Florin Coras2cba8532018-09-11 16:33:36 -07001629 svm_fifo_unset_event (rx_fifo);
1630 svm_msg_q_lock (mq);
1631 if (svm_msg_q_is_empty (mq))
1632 svm_msg_q_wait (mq);
1633
1634 svm_msg_q_sub_w_lock (mq, &msg);
1635 e = svm_msg_q_msg_data (mq, &msg);
1636 svm_msg_q_unlock (mq);
1637 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001638 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001639 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001640 }
1641 }
1642
1643 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1644 svm_fifo_unset_event (rx_fifo);
1645
Florin Coras2cba8532018-09-11 16:33:36 -07001646 return n_read;
1647}
1648
1649void
1650vppcom_session_free_segments (uint32_t session_handle,
1651 vppcom_data_segments_t ds)
1652{
1653 vcl_worker_t *wrk = vcl_worker_get_current ();
1654 vcl_session_t *s;
1655
1656 s = vcl_session_get_w_handle (wrk, session_handle);
1657 if (PREDICT_FALSE (!s || s->is_vep))
1658 return;
1659
1660 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1661}
1662
Florin Coras2cba8532018-09-11 16:33:36 -07001663int
1664vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1665{
1666 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001667 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001668 if (first_copy < max_bytes)
1669 {
Dave Barach178cf492018-11-13 16:34:13 -05001670 clib_memcpy_fast (buf + first_copy, ds[1].data,
1671 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001672 }
1673 return 0;
1674}
1675
Florin Coras54693d22018-07-17 10:46:29 -07001676static u8
1677vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1678{
Florin Coras653e43f2019-03-04 10:56:23 -08001679 return (e->event_type == SESSION_IO_EVT_TX
1680 && e->fifo->client_session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001681}
1682
Florin Coras42ceddb2018-12-12 10:56:01 -08001683static inline int
1684vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1685 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001686{
Florin Coras134a9962018-08-28 11:32:04 -07001687 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001688 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001689 vcl_session_t *s = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001690 svm_fifo_t *tx_fifo = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001691 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001692 svm_msg_q_msg_t msg;
1693 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001694 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001695 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001696
Florin Coras070453d2018-08-24 17:04:27 -07001697 if (PREDICT_FALSE (!buf))
1698 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001699
Florin Coras134a9962018-08-28 11:32:04 -07001700 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001701 if (PREDICT_FALSE (!s))
1702 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001703
Florin Coras460dce62018-07-27 05:45:06 -07001704 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001705 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001706 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1707 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001708 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001709 }
1710
Florin Coras6d0106e2019-01-29 20:11:58 -08001711 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001712 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001713 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1714 s->session_index, s->vpp_handle, s->session_state,
1715 vppcom_session_state_str (s->session_state));
1716 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001717 }
1718
Florin Coras0e88e852018-09-17 22:09:02 -07001719 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001720 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07001721 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras653e43f2019-03-04 10:56:23 -08001722 mq = wrk->app_event_queue;
Florin Coras54693d22018-07-17 10:46:29 -07001723 if (svm_fifo_is_full (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001724 {
Florin Coras54693d22018-07-17 10:46:29 -07001725 if (is_nonblocking)
1726 {
Florin Coras070453d2018-08-24 17:04:27 -07001727 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001728 }
Florin Coras60f1fc12018-08-16 14:57:31 -07001729 while (svm_fifo_is_full (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001730 {
Florin Coras1bcad5c2019-01-09 20:04:38 -08001731 svm_fifo_add_want_tx_ntf (tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001732 if (vcl_session_is_closing (s))
1733 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001734 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001735 if (svm_msg_q_is_empty (mq))
1736 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001737
Florin Coras54693d22018-07-17 10:46:29 -07001738 svm_msg_q_sub_w_lock (mq, &msg);
1739 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001740 svm_msg_q_unlock (mq);
1741
Florin Coras0e88e852018-09-17 22:09:02 -07001742 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001743 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001744 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001745 }
Dave Wallace543852a2017-08-03 02:11:34 -04001746 }
Dave Wallace543852a2017-08-03 02:11:34 -04001747
Florin Coras653e43f2019-03-04 10:56:23 -08001748 et = SESSION_IO_EVT_TX;
1749 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08001750 et = SESSION_IO_EVT_TX_FLUSH;
1751
Florin Coras460dce62018-07-27 05:45:06 -07001752 if (s->is_dgram)
1753 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08001754 s->vpp_evt_q, buf, n, et,
1755 !is_ct /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07001756 else
1757 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras653e43f2019-03-04 10:56:23 -08001758 !is_ct /* do_evt */ , SVM_Q_WAIT);
1759
1760 if (is_ct && svm_fifo_set_event (s->tx_fifo))
1761 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo, et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001762
Florin Coras460dce62018-07-27 05:45:06 -07001763 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001764
Florin Coras6d0106e2019-01-29 20:11:58 -08001765 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1766 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001767
Florin Coras54693d22018-07-17 10:46:29 -07001768 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001769}
1770
Florin Coras42ceddb2018-12-12 10:56:01 -08001771int
1772vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1773{
1774 return vppcom_session_write_inline (session_handle, buf, n,
1775 0 /* is_flush */ );
1776}
1777
Florin Corasb0f662f2018-12-27 14:51:46 -08001778int
1779vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1780{
1781 return vppcom_session_write_inline (session_handle, buf, n,
1782 1 /* is_flush */ );
1783}
1784
Florin Coras6d4bb422018-09-04 22:07:27 -07001785#define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1786if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1787 { \
1788 svm_fifo_unset_event (_fifo); \
1789 if (svm_fifo_is_empty (_fifo)) \
Florin Coras41c9e042018-09-11 00:10:41 -07001790 break; \
Florin Coras6d4bb422018-09-04 22:07:27 -07001791 } \
1792
Florin Coras86f04502018-09-12 16:08:01 -07001793static void
1794vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1795 unsigned long n_bits, unsigned long *read_map,
1796 unsigned long *write_map,
1797 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001798{
1799 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001800 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001801 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001802 u32 sid;
1803
1804 switch (e->event_type)
1805 {
1806 case FIFO_EVENT_APP_RX:
1807 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1808 sid = e->fifo->client_session_index;
1809 session = vcl_session_get (wrk, sid);
1810 if (!session)
1811 break;
1812 if (sid < n_bits && read_map)
1813 {
David Johnsond9818dd2018-12-14 14:53:41 -05001814 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001815 *bits_set += 1;
1816 }
1817 break;
1818 case FIFO_EVENT_APP_TX:
1819 sid = e->fifo->client_session_index;
1820 session = vcl_session_get (wrk, sid);
1821 if (!session)
1822 break;
1823 if (sid < n_bits && write_map)
1824 {
David Johnsond9818dd2018-12-14 14:53:41 -05001825 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001826 *bits_set += 1;
1827 }
1828 break;
Florin Coras86f04502018-09-12 16:08:01 -07001829 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001830 session = vcl_session_accepted (wrk,
1831 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001832 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001833 break;
Florin Coras86f04502018-09-12 16:08:01 -07001834 sid = session->session_index;
1835 if (sid < n_bits && read_map)
1836 {
David Johnsond9818dd2018-12-14 14:53:41 -05001837 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001838 *bits_set += 1;
1839 }
1840 break;
1841 case SESSION_CTRL_EVT_CONNECTED:
1842 connected_msg = (session_connected_msg_t *) e->data;
1843 vcl_session_connected_handler (wrk, connected_msg);
1844 break;
1845 case SESSION_CTRL_EVT_DISCONNECTED:
1846 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001847 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1848 if (!session)
1849 break;
1850 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001851 if (sid < n_bits && except_map)
1852 {
David Johnsond9818dd2018-12-14 14:53:41 -05001853 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001854 *bits_set += 1;
1855 }
1856 break;
1857 case SESSION_CTRL_EVT_RESET:
1858 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1859 if (sid < n_bits && except_map)
1860 {
David Johnsond9818dd2018-12-14 14:53:41 -05001861 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001862 *bits_set += 1;
1863 }
1864 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001865 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1866 vcl_session_unlisten_reply_handler (wrk, e->data);
1867 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001868 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1869 vcl_session_worker_update_reply_handler (wrk, e->data);
1870 break;
1871 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1872 vcl_session_req_worker_update_handler (wrk, e->data);
1873 break;
Florin Coras86f04502018-09-12 16:08:01 -07001874 default:
1875 clib_warning ("unhandled: %u", e->event_type);
1876 break;
1877 }
1878}
1879
1880static int
1881vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1882 unsigned long n_bits, unsigned long *read_map,
1883 unsigned long *write_map, unsigned long *except_map,
1884 double time_to_wait, u32 * bits_set)
1885{
Florin Coras99368312018-08-02 10:45:44 -07001886 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001887 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001888 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001889
1890 svm_msg_q_lock (mq);
1891 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001892 {
Florin Coras54693d22018-07-17 10:46:29 -07001893 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001894 {
Florin Coras54693d22018-07-17 10:46:29 -07001895 svm_msg_q_unlock (mq);
1896 return 0;
1897 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001898
Florin Coras54693d22018-07-17 10:46:29 -07001899 if (!time_to_wait)
1900 {
1901 svm_msg_q_unlock (mq);
1902 return 0;
1903 }
1904 else if (time_to_wait < 0)
1905 {
1906 svm_msg_q_wait (mq);
1907 }
1908 else
1909 {
1910 if (svm_msg_q_timedwait (mq, time_to_wait))
1911 {
1912 svm_msg_q_unlock (mq);
1913 return 0;
1914 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001915 }
1916 }
Florin Coras134a9962018-08-28 11:32:04 -07001917 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07001918 svm_msg_q_unlock (mq);
1919
Florin Coras134a9962018-08-28 11:32:04 -07001920 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001921 {
Florin Coras134a9962018-08-28 11:32:04 -07001922 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07001923 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07001924 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1925 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001926 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001927 }
Florin Coras134a9962018-08-28 11:32:04 -07001928 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08001929 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001930 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001931}
1932
Florin Coras99368312018-08-02 10:45:44 -07001933static int
Florin Coras294afe22019-01-07 17:49:17 -08001934vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
1935 vcl_si_set * read_map, vcl_si_set * write_map,
1936 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001937 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001938{
Florin Coras294afe22019-01-07 17:49:17 -08001939 time_to_wait = (time_to_wait == -1) ? 1e6 : time_to_wait;
Florin Coras653e43f2019-03-04 10:56:23 -08001940 return vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1941 write_map, except_map, time_to_wait, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001942}
1943
1944static int
Florin Coras294afe22019-01-07 17:49:17 -08001945vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
1946 vcl_si_set * read_map, vcl_si_set * write_map,
1947 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001948 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001949{
1950 vcl_mq_evt_conn_t *mqc;
1951 int __clib_unused n_read;
1952 int n_mq_evts, i;
1953 u64 buf;
1954
Florin Coras134a9962018-08-28 11:32:04 -07001955 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1956 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1957 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07001958 for (i = 0; i < n_mq_evts; i++)
1959 {
Florin Coras134a9962018-08-28 11:32:04 -07001960 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07001961 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07001962 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07001963 except_map, 0, bits_set);
1964 }
1965
1966 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1967}
1968
Dave Wallace543852a2017-08-03 02:11:34 -04001969int
Florin Coras294afe22019-01-07 17:49:17 -08001970vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1971 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04001972{
Florin Coras54693d22018-07-17 10:46:29 -07001973 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001974 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001975 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07001976 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04001977
Dave Wallace7876d392017-10-19 03:53:57 -04001978 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001979 {
Florin Coras134a9962018-08-28 11:32:04 -07001980 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001981 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08001982 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
1983 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001984 }
Dave Wallace7876d392017-10-19 03:53:57 -04001985 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001986 {
Florin Coras134a9962018-08-28 11:32:04 -07001987 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001988 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08001989 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
1990 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001991 }
Dave Wallace7876d392017-10-19 03:53:57 -04001992 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001993 {
Florin Coras134a9962018-08-28 11:32:04 -07001994 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001995 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08001996 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
1997 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001998 }
1999
Florin Coras54693d22018-07-17 10:46:29 -07002000 if (!n_bits)
2001 return 0;
2002
2003 if (!write_map)
2004 goto check_rd;
2005
2006 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002007 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2008 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002009 {
Florin Coras47c40e22018-11-26 17:01:36 -08002010 if (except_map && sid < minbits)
2011 clib_bitmap_set_no_check (except_map, sid, 1);
2012 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002013 }
2014
2015 rv = svm_fifo_is_full (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002016 if (!rv)
2017 {
David Johnsond9818dd2018-12-14 14:53:41 -05002018 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002019 bits_set++;
2020 }
Florin Coras294afe22019-01-07 17:49:17 -08002021 else
Florin Coras1bcad5c2019-01-09 20:04:38 -08002022 svm_fifo_add_want_tx_ntf (session->tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002023 }));
2024
2025check_rd:
2026 if (!read_map)
2027 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002028
Florin Coras134a9962018-08-28 11:32:04 -07002029 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2030 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002031 {
Florin Coras47c40e22018-11-26 17:01:36 -08002032 if (except_map && sid < minbits)
2033 clib_bitmap_set_no_check (except_map, sid, 1);
2034 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002035 }
2036
Florin Coras0ef8ef22019-01-18 08:37:13 -08002037 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002038 if (rv)
2039 {
David Johnsond9818dd2018-12-14 14:53:41 -05002040 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002041 bits_set++;
2042 }
2043 }));
2044 /* *INDENT-ON* */
2045
2046check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002047
Florin Coras86f04502018-09-12 16:08:01 -07002048 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2049 {
2050 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2051 read_map, write_map, except_map, &bits_set);
2052 }
2053 vec_reset_length (wrk->unhandled_evts_vector);
2054
Florin Coras99368312018-08-02 10:45:44 -07002055 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002056 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002057 time_to_wait, &bits_set);
2058 else
Florin Coras134a9962018-08-28 11:32:04 -07002059 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002060 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002061
Dave Wallace543852a2017-08-03 02:11:34 -04002062 return (bits_set);
2063}
2064
Dave Wallacef7f809c2017-10-03 01:48:42 -04002065static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002066vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002067{
Florin Coras7e12d942018-06-27 14:32:43 -07002068 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002069 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002070 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002071
Dave Wallace498b3a52017-11-09 13:00:34 -05002072 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002073 return;
2074
2075 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Florin Coras134a9962018-08-28 11:32:04 -07002076 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002077 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002078 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002079 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2080 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002081 goto done;
2082 }
2083 if (PREDICT_FALSE (!session->is_vep))
2084 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002085 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2086 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002087 goto done;
2088 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002089 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05002090 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002091 "{\n"
2092 " is_vep = %u\n"
2093 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002094 " next_sid = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002095 "}\n", getpid (), vep_idx,
2096 session->is_vep, session->is_vep_session,
Florin Corasac626262019-03-03 17:56:48 -08002097 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002098
Florin Coras134a9962018-08-28 11:32:04 -07002099 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002100 {
Florin Coras134a9962018-08-28 11:32:04 -07002101 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002102 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002103 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002104 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002105 goto done;
2106 }
2107 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05002108 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2109 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002110 else if (PREDICT_FALSE (!session->is_vep_session))
2111 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002112 clib_warning ("VCL<%d>: ERROR: session (%u) "
2113 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002114 goto done;
2115 }
2116 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002117 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05002118 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002119 "vep_idx (%u)!", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002120 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002121 if (session->is_vep_session)
2122 {
2123 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2124 "{\n"
2125 " next_sid = 0x%x (%u)\n"
2126 " prev_sid = 0x%x (%u)\n"
2127 " vep_idx = 0x%x (%u)\n"
2128 " ev.events = 0x%x\n"
2129 " ev.data.u64 = 0x%llx\n"
2130 " et_mask = 0x%x\n"
2131 "}\n",
2132 vep_idx, sid, sid,
Florin Coras134a9962018-08-28 11:32:04 -07002133 vep->next_sh, vep->next_sh,
2134 vep->prev_sh, vep->prev_sh,
2135 vep->vep_sh, vep->vep_sh,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002136 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002137 }
2138 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002139
2140done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002141 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2142 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002143}
2144
2145int
2146vppcom_epoll_create (void)
2147{
Florin Coras134a9962018-08-28 11:32:04 -07002148 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002149 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002150
Florin Coras134a9962018-08-28 11:32:04 -07002151 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002152
2153 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002154 vep_session->vep.vep_sh = ~0;
2155 vep_session->vep.next_sh = ~0;
2156 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002157 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002158
Florin Corasa7a1a222018-12-30 17:11:31 -08002159 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2160 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002161
Florin Corasab2f6db2018-08-31 14:31:41 -07002162 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002163}
2164
2165int
Florin Coras134a9962018-08-28 11:32:04 -07002166vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002167 struct epoll_event *event)
2168{
Florin Coras134a9962018-08-28 11:32:04 -07002169 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002170 vcl_session_t *vep_session;
2171 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002172 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002173
Florin Coras134a9962018-08-28 11:32:04 -07002174 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002175 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002176 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002177 return VPPCOM_EINVAL;
2178 }
2179
Florin Coras134a9962018-08-28 11:32:04 -07002180 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002181 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002182 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002183 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002184 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002185 }
2186 if (PREDICT_FALSE (!vep_session->is_vep))
2187 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002188 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002189 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002190 }
2191
Florin Coras134a9962018-08-28 11:32:04 -07002192 ASSERT (vep_session->vep.vep_sh == ~0);
2193 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002194
Florin Coras134a9962018-08-28 11:32:04 -07002195 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002196 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002197 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002198 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002199 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002200 }
2201 if (PREDICT_FALSE (session->is_vep))
2202 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002203 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002204 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002205 }
2206
2207 switch (op)
2208 {
2209 case EPOLL_CTL_ADD:
2210 if (PREDICT_FALSE (!event))
2211 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002212 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002213 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002214 }
Florin Coras134a9962018-08-28 11:32:04 -07002215 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002216 {
Florin Coras7e12d942018-06-27 14:32:43 -07002217 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002218 next_session = vcl_session_get_w_handle (wrk,
2219 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002220 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002221 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002222 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on "
2223 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002224 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002225 }
Florin Coras134a9962018-08-28 11:32:04 -07002226 ASSERT (next_session->vep.prev_sh == vep_handle);
2227 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002228 }
Florin Coras134a9962018-08-28 11:32:04 -07002229 session->vep.next_sh = vep_session->vep.next_sh;
2230 session->vep.prev_sh = vep_handle;
2231 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002232 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2233 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002234 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002235 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002236 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002237
Florin Coras1bcad5c2019-01-09 20:04:38 -08002238 if (session->tx_fifo)
2239 svm_fifo_add_want_tx_ntf (session->tx_fifo,
2240 SVM_FIFO_WANT_TX_NOTIF_IF_FULL);
2241
Florin Corasa7a1a222018-12-30 17:11:31 -08002242 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2243 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002244 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002245 break;
2246
2247 case EPOLL_CTL_MOD:
2248 if (PREDICT_FALSE (!event))
2249 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002250 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002251 rv = VPPCOM_EINVAL;
2252 goto done;
2253 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002254 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002255 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002256 VDBG (0, "sid %u EPOLL_CTL_MOD: not a vep session!",
2257 session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002258 rv = VPPCOM_EINVAL;
2259 goto done;
2260 }
Florin Coras134a9962018-08-28 11:32:04 -07002261 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002262 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002263 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2264 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002265 rv = VPPCOM_EINVAL;
2266 goto done;
2267 }
2268 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2269 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002270 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2271 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002272 break;
2273
2274 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002275 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002276 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002277 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002278 rv = VPPCOM_EINVAL;
2279 goto done;
2280 }
Florin Coras134a9962018-08-28 11:32:04 -07002281 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002282 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002283 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2284 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002285 rv = VPPCOM_EINVAL;
2286 goto done;
2287 }
2288
Florin Coras134a9962018-08-28 11:32:04 -07002289 if (session->vep.prev_sh == vep_handle)
2290 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002291 else
2292 {
Florin Coras7e12d942018-06-27 14:32:43 -07002293 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002294 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002295 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002296 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002297 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sid (%u) on sid (%u)!",
2298 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002299 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002300 }
Florin Coras134a9962018-08-28 11:32:04 -07002301 ASSERT (prev_session->vep.next_sh == session_handle);
2302 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002303 }
Florin Coras134a9962018-08-28 11:32:04 -07002304 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002305 {
Florin Coras7e12d942018-06-27 14:32:43 -07002306 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002307 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002308 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002309 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002310 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sid (%u) on sid (%u)!",
2311 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002312 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002313 }
Florin Coras134a9962018-08-28 11:32:04 -07002314 ASSERT (next_session->vep.prev_sh == session_handle);
2315 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002316 }
2317
2318 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002319 session->vep.next_sh = ~0;
2320 session->vep.prev_sh = ~0;
2321 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002322 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002323
2324 if (session->tx_fifo)
2325 svm_fifo_del_want_tx_ntf (session->tx_fifo, SVM_FIFO_NO_TX_NOTIF);
2326
Florin Corasa7a1a222018-12-30 17:11:31 -08002327 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_handle,
2328 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002329 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002330 break;
2331
2332 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002333 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002334 rv = VPPCOM_EINVAL;
2335 }
2336
Florin Coras134a9962018-08-28 11:32:04 -07002337 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002338
2339done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002340 return rv;
2341}
2342
Florin Coras86f04502018-09-12 16:08:01 -07002343static inline void
2344vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2345 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002346{
2347 session_disconnected_msg_t *disconnected_msg;
2348 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002349 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002350 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002351 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002352 u8 add_event = 0;
2353
2354 switch (e->event_type)
2355 {
Florin Coras653e43f2019-03-04 10:56:23 -08002356 case SESSION_IO_EVT_RX:
Florin Coras86f04502018-09-12 16:08:01 -07002357 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2358 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2359 sid = e->fifo->client_session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002360 if (!(session = vcl_session_get (wrk, sid)))
2361 break;
Florin Coras86f04502018-09-12 16:08:01 -07002362 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002363 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002364 break;
2365 add_event = 1;
2366 events[*num_ev].events |= EPOLLIN;
2367 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002368 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002369 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002370 case SESSION_IO_EVT_TX:
Florin Coras86f04502018-09-12 16:08:01 -07002371 sid = e->fifo->client_session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002372 if (!(session = vcl_session_get (wrk, sid)))
2373 break;
Florin Coras86f04502018-09-12 16:08:01 -07002374 session_events = session->vep.ev.events;
2375 if (!(EPOLLOUT & session_events))
2376 break;
2377 add_event = 1;
2378 events[*num_ev].events |= EPOLLOUT;
2379 session_evt_data = session->vep.ev.data.u64;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002380 svm_fifo_reset_tx_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002381 break;
Florin Coras86f04502018-09-12 16:08:01 -07002382 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002383 session = vcl_session_accepted (wrk,
2384 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002385 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002386 break;
Florin Coras86f04502018-09-12 16:08:01 -07002387
Florin Coras86f04502018-09-12 16:08:01 -07002388 session_events = session->vep.ev.events;
2389 if (!(EPOLLIN & session_events))
2390 break;
2391
2392 add_event = 1;
2393 events[*num_ev].events |= EPOLLIN;
2394 session_evt_data = session->vep.ev.data.u64;
2395 break;
2396 case SESSION_CTRL_EVT_CONNECTED:
2397 connected_msg = (session_connected_msg_t *) e->data;
2398 vcl_session_connected_handler (wrk, connected_msg);
2399 /* Generate EPOLLOUT because there's no connected event */
2400 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002401 if (!(session = vcl_session_get (wrk, sid)))
2402 break;
Florin Coras86f04502018-09-12 16:08:01 -07002403 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002404 if (!(EPOLLOUT & session_events))
2405 break;
2406 add_event = 1;
2407 events[*num_ev].events |= EPOLLOUT;
2408 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002409 break;
2410 case SESSION_CTRL_EVT_DISCONNECTED:
2411 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002412 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2413 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002414 break;
Florin Coras72f77822019-01-22 19:05:52 -08002415 session_events = session->vep.ev.events;
2416 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2417 break;
Florin Coras86f04502018-09-12 16:08:01 -07002418 add_event = 1;
2419 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2420 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002421 break;
2422 case SESSION_CTRL_EVT_RESET:
2423 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2424 if (!(session = vcl_session_get (wrk, sid)))
2425 break;
Florin Coras72f77822019-01-22 19:05:52 -08002426 session_events = session->vep.ev.events;
2427 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2428 break;
Florin Coras86f04502018-09-12 16:08:01 -07002429 add_event = 1;
2430 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2431 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002432 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002433 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2434 vcl_session_unlisten_reply_handler (wrk, e->data);
2435 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002436 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2437 vcl_session_req_worker_update_handler (wrk, e->data);
2438 break;
2439 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2440 vcl_session_worker_update_reply_handler (wrk, e->data);
2441 break;
Florin Coras86f04502018-09-12 16:08:01 -07002442 default:
2443 VDBG (0, "unhandled: %u", e->event_type);
2444 break;
2445 }
2446
2447 if (add_event)
2448 {
2449 events[*num_ev].data.u64 = session_evt_data;
2450 if (EPOLLONESHOT & session_events)
2451 {
2452 session = vcl_session_get (wrk, sid);
2453 session->vep.ev.events = 0;
2454 }
2455 *num_ev += 1;
2456 }
2457}
2458
2459static int
2460vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2461 struct epoll_event *events, u32 maxevents,
2462 double wait_for_time, u32 * num_ev)
2463{
Florin Coras99368312018-08-02 10:45:44 -07002464 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002465 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002466 int i;
2467
Florin Coras539663c2018-09-28 14:59:37 -07002468 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2469 goto handle_dequeued;
2470
Florin Coras54693d22018-07-17 10:46:29 -07002471 svm_msg_q_lock (mq);
2472 if (svm_msg_q_is_empty (mq))
2473 {
2474 if (!wait_for_time)
2475 {
2476 svm_msg_q_unlock (mq);
2477 return 0;
2478 }
2479 else if (wait_for_time < 0)
2480 {
2481 svm_msg_q_wait (mq);
2482 }
2483 else
2484 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002485 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002486 {
2487 svm_msg_q_unlock (mq);
2488 return 0;
2489 }
2490 }
2491 }
Florin Coras134a9962018-08-28 11:32:04 -07002492 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002493 svm_msg_q_unlock (mq);
2494
Florin Coras539663c2018-09-28 14:59:37 -07002495handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002496 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002497 {
Florin Coras134a9962018-08-28 11:32:04 -07002498 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002499 e = svm_msg_q_msg_data (mq, msg);
Florin Corasaa27eb92018-10-13 12:20:01 -07002500 if (*num_ev < maxevents)
2501 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2502 else
2503 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras99368312018-08-02 10:45:44 -07002504 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002505 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002506 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002507 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002508 return *num_ev;
2509}
2510
Florin Coras99368312018-08-02 10:45:44 -07002511static int
Florin Coras134a9962018-08-28 11:32:04 -07002512vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002513 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002514{
Florin Coras72f77822019-01-22 19:05:52 -08002515 wait_for_time = (wait_for_time == -1) ? (double) 1e6 : wait_for_time;
Florin Coras653e43f2019-03-04 10:56:23 -08002516 return vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
2517 maxevents, wait_for_time, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002518}
2519
2520static int
Florin Coras134a9962018-08-28 11:32:04 -07002521vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002522 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002523{
2524 vcl_mq_evt_conn_t *mqc;
2525 int __clib_unused n_read;
2526 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002527 u64 buf;
2528
Florin Coras134a9962018-08-28 11:32:04 -07002529 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002530again:
Florin Coras134a9962018-08-28 11:32:04 -07002531 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2532 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002533 for (i = 0; i < n_mq_evts; i++)
2534 {
Florin Coras134a9962018-08-28 11:32:04 -07002535 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002536 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002537 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002538 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002539 if (!n_evts && n_mq_evts > 0)
2540 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002541
2542 return (int) n_evts;
2543}
2544
Dave Wallacef7f809c2017-10-03 01:48:42 -04002545int
Florin Coras134a9962018-08-28 11:32:04 -07002546vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002547 int maxevents, double wait_for_time)
2548{
Florin Coras134a9962018-08-28 11:32:04 -07002549 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002550 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002551 u32 n_evts = 0;
2552 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002553
2554 if (PREDICT_FALSE (maxevents <= 0))
2555 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002556 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002557 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002558 return VPPCOM_EINVAL;
2559 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002560
Florin Coras134a9962018-08-28 11:32:04 -07002561 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002562 if (!vep_session)
2563 return VPPCOM_EBADFD;
2564
Florin Coras54693d22018-07-17 10:46:29 -07002565 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002566 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002567 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Florin Coras134a9962018-08-28 11:32:04 -07002568 getpid (), vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002569 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002570 }
Florin Coras54693d22018-07-17 10:46:29 -07002571
2572 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002573
Florin Coras86f04502018-09-12 16:08:01 -07002574 if (vec_len (wrk->unhandled_evts_vector))
2575 {
2576 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2577 {
2578 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2579 events, &n_evts);
2580 if (n_evts == maxevents)
2581 {
2582 i += 1;
2583 break;
2584 }
2585 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002586
Florin Coras86f04502018-09-12 16:08:01 -07002587 vec_delete (wrk->unhandled_evts_vector, i, 0);
2588 }
2589
2590 if (vcm->cfg.use_mq_eventfd)
2591 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2592 wait_for_time);
2593
2594 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2595 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002596}
2597
Dave Wallace35830af2017-10-09 01:43:42 -04002598int
Florin Coras134a9962018-08-28 11:32:04 -07002599vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002600 void *buffer, uint32_t * buflen)
2601{
Florin Coras134a9962018-08-28 11:32:04 -07002602 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002603 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002604 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002605 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002606 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002607
Florin Coras134a9962018-08-28 11:32:04 -07002608 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002609 if (!session)
2610 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002611
Dave Wallace35830af2017-10-09 01:43:42 -04002612 switch (op)
2613 {
2614 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002615 rv = vcl_session_read_ready (session);
Florin Coras7baeb712019-01-04 17:05:43 -08002616 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d", rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002617 break;
2618
Dave Wallace227867f2017-11-13 21:21:53 -05002619 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002620 rv = vcl_session_write_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002621 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
Florin Coras134a9962018-08-28 11:32:04 -07002622 getpid (), session_handle, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002623 break;
2624
2625 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002626 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002627 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002628 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2629 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002630 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07002631 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2632 "is_nonblocking = %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002633 session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002634 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002635 }
2636 else
2637 rv = VPPCOM_EINVAL;
2638 break;
2639
2640 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002641 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002642 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002643 if (*flags & O_NONBLOCK)
2644 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2645 else
2646 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2647
Florin Coras0d427d82018-06-27 03:24:07 -07002648 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2649 " is_nonblocking = %u",
Florin Coras134a9962018-08-28 11:32:04 -07002650 getpid (), session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002651 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002652 }
2653 else
2654 rv = VPPCOM_EINVAL;
2655 break;
2656
2657 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002658 if (PREDICT_TRUE (buffer && buflen &&
2659 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002660 {
Florin Coras7e12d942018-06-27 14:32:43 -07002661 ep->is_ip4 = session->transport.is_ip4;
2662 ep->port = session->transport.rmt_port;
2663 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002664 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2665 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002666 else
Dave Barach178cf492018-11-13 16:34:13 -05002667 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2668 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002669 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002670 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2671 "addr = %U, port %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002672 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002673 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002674 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2675 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002676 }
2677 else
2678 rv = VPPCOM_EINVAL;
2679 break;
2680
2681 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002682 if (PREDICT_TRUE (buffer && buflen &&
2683 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002684 {
Florin Coras7e12d942018-06-27 14:32:43 -07002685 ep->is_ip4 = session->transport.is_ip4;
2686 ep->port = session->transport.lcl_port;
2687 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002688 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2689 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002690 else
Dave Barach178cf492018-11-13 16:34:13 -05002691 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2692 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002693 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002694 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2695 " addr = %U port %d", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002696 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002697 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002698 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2699 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002700 }
2701 else
2702 rv = VPPCOM_EINVAL;
2703 break;
Stevenb5a11602017-10-11 09:59:30 -07002704
Dave Wallace048b1d62018-01-03 22:24:41 -05002705 case VPPCOM_ATTR_GET_LIBC_EPFD:
2706 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07002707 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2708 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002709 break;
2710
2711 case VPPCOM_ATTR_SET_LIBC_EPFD:
2712 if (PREDICT_TRUE (buffer && buflen &&
2713 (*buflen == sizeof (session->libc_epfd))))
2714 {
2715 session->libc_epfd = *(int *) buffer;
2716 *buflen = sizeof (session->libc_epfd);
2717
Florin Coras0d427d82018-06-27 03:24:07 -07002718 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2719 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002720 }
2721 else
2722 rv = VPPCOM_EINVAL;
2723 break;
2724
2725 case VPPCOM_ATTR_GET_PROTOCOL:
2726 if (buffer && buflen && (*buflen >= sizeof (int)))
2727 {
Florin Coras7e12d942018-06-27 14:32:43 -07002728 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002729 *buflen = sizeof (int);
2730
Florin Coras0d427d82018-06-27 03:24:07 -07002731 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2732 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2733 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002734 }
2735 else
2736 rv = VPPCOM_EINVAL;
2737 break;
2738
2739 case VPPCOM_ATTR_GET_LISTEN:
2740 if (buffer && buflen && (*buflen >= sizeof (int)))
2741 {
2742 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2743 VCL_SESS_ATTR_LISTEN);
2744 *buflen = sizeof (int);
2745
Florin Coras0d427d82018-06-27 03:24:07 -07002746 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2747 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002748 }
2749 else
2750 rv = VPPCOM_EINVAL;
2751 break;
2752
2753 case VPPCOM_ATTR_GET_ERROR:
2754 if (buffer && buflen && (*buflen >= sizeof (int)))
2755 {
2756 *(int *) buffer = 0;
2757 *buflen = sizeof (int);
2758
Florin Coras0d427d82018-06-27 03:24:07 -07002759 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2760 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002761 }
2762 else
2763 rv = VPPCOM_EINVAL;
2764 break;
2765
2766 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2767 if (buffer && buflen && (*buflen >= sizeof (u32)))
2768 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002769
2770 /* VPP-TBD */
2771 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002772 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002773 vcm->cfg.tx_fifo_size);
2774 *buflen = sizeof (u32);
2775
Florin Coras0d427d82018-06-27 03:24:07 -07002776 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2777 "buflen %d, #VPP-TBD#", getpid (),
2778 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002779 }
2780 else
2781 rv = VPPCOM_EINVAL;
2782 break;
2783
2784 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2785 if (buffer && buflen && (*buflen == sizeof (u32)))
2786 {
2787 /* VPP-TBD */
2788 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002789 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2790 "buflen %d, #VPP-TBD#", getpid (),
2791 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002792 }
2793 else
2794 rv = VPPCOM_EINVAL;
2795 break;
2796
2797 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2798 if (buffer && buflen && (*buflen >= sizeof (u32)))
2799 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002800
2801 /* VPP-TBD */
2802 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002803 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002804 vcm->cfg.rx_fifo_size);
2805 *buflen = sizeof (u32);
2806
Florin Coras0d427d82018-06-27 03:24:07 -07002807 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2808 "buflen %d, #VPP-TBD#", getpid (),
2809 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002810 }
2811 else
2812 rv = VPPCOM_EINVAL;
2813 break;
2814
2815 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2816 if (buffer && buflen && (*buflen == sizeof (u32)))
2817 {
2818 /* VPP-TBD */
2819 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002820 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2821 "buflen %d, #VPP-TBD#", getpid (),
2822 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002823 }
2824 else
2825 rv = VPPCOM_EINVAL;
2826 break;
2827
2828 case VPPCOM_ATTR_GET_REUSEADDR:
2829 if (buffer && buflen && (*buflen >= sizeof (int)))
2830 {
2831 /* VPP-TBD */
2832 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2833 VCL_SESS_ATTR_REUSEADDR);
2834 *buflen = sizeof (int);
2835
Florin Coras0d427d82018-06-27 03:24:07 -07002836 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2837 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002838 }
2839 else
2840 rv = VPPCOM_EINVAL;
2841 break;
2842
Stevenb5a11602017-10-11 09:59:30 -07002843 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002844 if (buffer && buflen && (*buflen == sizeof (int)) &&
2845 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2846 {
2847 /* VPP-TBD */
2848 if (*(int *) buffer)
2849 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2850 else
2851 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2852
Florin Coras0d427d82018-06-27 03:24:07 -07002853 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2854 " #VPP-TBD#", getpid (),
2855 VCL_SESS_ATTR_TEST (session->attr,
2856 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002857 }
2858 else
2859 rv = VPPCOM_EINVAL;
2860 break;
2861
2862 case VPPCOM_ATTR_GET_REUSEPORT:
2863 if (buffer && buflen && (*buflen >= sizeof (int)))
2864 {
2865 /* VPP-TBD */
2866 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2867 VCL_SESS_ATTR_REUSEPORT);
2868 *buflen = sizeof (int);
2869
Florin Coras0d427d82018-06-27 03:24:07 -07002870 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2871 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002872 }
2873 else
2874 rv = VPPCOM_EINVAL;
2875 break;
2876
2877 case VPPCOM_ATTR_SET_REUSEPORT:
2878 if (buffer && buflen && (*buflen == sizeof (int)) &&
2879 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2880 {
2881 /* VPP-TBD */
2882 if (*(int *) buffer)
2883 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2884 else
2885 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2886
Florin Coras0d427d82018-06-27 03:24:07 -07002887 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2888 " #VPP-TBD#", getpid (),
2889 VCL_SESS_ATTR_TEST (session->attr,
2890 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002891 }
2892 else
2893 rv = VPPCOM_EINVAL;
2894 break;
2895
2896 case VPPCOM_ATTR_GET_BROADCAST:
2897 if (buffer && buflen && (*buflen >= sizeof (int)))
2898 {
2899 /* VPP-TBD */
2900 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2901 VCL_SESS_ATTR_BROADCAST);
2902 *buflen = sizeof (int);
2903
Florin Coras0d427d82018-06-27 03:24:07 -07002904 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2905 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002906 }
2907 else
2908 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002909 break;
2910
2911 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002912 if (buffer && buflen && (*buflen == sizeof (int)))
2913 {
2914 /* VPP-TBD */
2915 if (*(int *) buffer)
2916 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2917 else
2918 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2919
Florin Coras0d427d82018-06-27 03:24:07 -07002920 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2921 "#VPP-TBD#", getpid (),
2922 VCL_SESS_ATTR_TEST (session->attr,
2923 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002924 }
2925 else
2926 rv = VPPCOM_EINVAL;
2927 break;
2928
2929 case VPPCOM_ATTR_GET_V6ONLY:
2930 if (buffer && buflen && (*buflen >= sizeof (int)))
2931 {
2932 /* VPP-TBD */
2933 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2934 VCL_SESS_ATTR_V6ONLY);
2935 *buflen = sizeof (int);
2936
Florin Coras0d427d82018-06-27 03:24:07 -07002937 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2938 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002939 }
2940 else
2941 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002942 break;
2943
2944 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002945 if (buffer && buflen && (*buflen == sizeof (int)))
2946 {
2947 /* VPP-TBD */
2948 if (*(int *) buffer)
2949 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2950 else
2951 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2952
Florin Coras0d427d82018-06-27 03:24:07 -07002953 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2954 "#VPP-TBD#", getpid (),
2955 VCL_SESS_ATTR_TEST (session->attr,
2956 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002957 }
2958 else
2959 rv = VPPCOM_EINVAL;
2960 break;
2961
2962 case VPPCOM_ATTR_GET_KEEPALIVE:
2963 if (buffer && buflen && (*buflen >= sizeof (int)))
2964 {
2965 /* VPP-TBD */
2966 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2967 VCL_SESS_ATTR_KEEPALIVE);
2968 *buflen = sizeof (int);
2969
Florin Coras0d427d82018-06-27 03:24:07 -07002970 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2971 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002972 }
2973 else
2974 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002975 break;
Stevenbccd3392017-10-12 20:42:21 -07002976
2977 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002978 if (buffer && buflen && (*buflen == sizeof (int)))
2979 {
2980 /* VPP-TBD */
2981 if (*(int *) buffer)
2982 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2983 else
2984 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2985
Florin Coras0d427d82018-06-27 03:24:07 -07002986 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2987 "#VPP-TBD#", getpid (),
2988 VCL_SESS_ATTR_TEST (session->attr,
2989 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002990 }
2991 else
2992 rv = VPPCOM_EINVAL;
2993 break;
2994
2995 case VPPCOM_ATTR_GET_TCP_NODELAY:
2996 if (buffer && buflen && (*buflen >= sizeof (int)))
2997 {
2998 /* VPP-TBD */
2999 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3000 VCL_SESS_ATTR_TCP_NODELAY);
3001 *buflen = sizeof (int);
3002
Florin Coras0d427d82018-06-27 03:24:07 -07003003 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3004 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003005 }
3006 else
3007 rv = VPPCOM_EINVAL;
3008 break;
3009
3010 case VPPCOM_ATTR_SET_TCP_NODELAY:
3011 if (buffer && buflen && (*buflen == sizeof (int)))
3012 {
3013 /* VPP-TBD */
3014 if (*(int *) buffer)
3015 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3016 else
3017 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3018
Florin Coras0d427d82018-06-27 03:24:07 -07003019 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3020 "#VPP-TBD#", getpid (),
3021 VCL_SESS_ATTR_TEST (session->attr,
3022 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003023 }
3024 else
3025 rv = VPPCOM_EINVAL;
3026 break;
3027
3028 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3029 if (buffer && buflen && (*buflen >= sizeof (int)))
3030 {
3031 /* VPP-TBD */
3032 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3033 VCL_SESS_ATTR_TCP_KEEPIDLE);
3034 *buflen = sizeof (int);
3035
Florin Coras0d427d82018-06-27 03:24:07 -07003036 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3037 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003038 }
3039 else
3040 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003041 break;
3042
3043 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003044 if (buffer && buflen && (*buflen == sizeof (int)))
3045 {
3046 /* VPP-TBD */
3047 if (*(int *) buffer)
3048 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3049 else
3050 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3051
Florin Coras0d427d82018-06-27 03:24:07 -07003052 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3053 "#VPP-TBD#", getpid (),
3054 VCL_SESS_ATTR_TEST (session->attr,
3055 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003056 }
3057 else
3058 rv = VPPCOM_EINVAL;
3059 break;
3060
3061 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3062 if (buffer && buflen && (*buflen >= sizeof (int)))
3063 {
3064 /* VPP-TBD */
3065 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3066 VCL_SESS_ATTR_TCP_KEEPINTVL);
3067 *buflen = sizeof (int);
3068
Florin Coras0d427d82018-06-27 03:24:07 -07003069 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3070 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003071 }
3072 else
3073 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003074 break;
3075
3076 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003077 if (buffer && buflen && (*buflen == sizeof (int)))
3078 {
3079 /* VPP-TBD */
3080 if (*(int *) buffer)
3081 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3082 else
3083 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3084
Florin Coras0d427d82018-06-27 03:24:07 -07003085 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3086 "#VPP-TBD#", getpid (),
3087 VCL_SESS_ATTR_TEST (session->attr,
3088 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003089 }
3090 else
3091 rv = VPPCOM_EINVAL;
3092 break;
3093
3094 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3095 if (buffer && buflen && (*buflen >= sizeof (u32)))
3096 {
3097 /* VPP-TBD */
3098 *(u32 *) buffer = session->user_mss;
3099 *buflen = sizeof (int);
3100
Florin Coras0d427d82018-06-27 03:24:07 -07003101 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3102 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003103 }
3104 else
3105 rv = VPPCOM_EINVAL;
3106 break;
3107
3108 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3109 if (buffer && buflen && (*buflen == sizeof (u32)))
3110 {
3111 /* VPP-TBD */
3112 session->user_mss = *(u32 *) buffer;
3113
Florin Coras0d427d82018-06-27 03:24:07 -07003114 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3115 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003116 }
3117 else
3118 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003119 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003120
Florin Coras7baeb712019-01-04 17:05:43 -08003121 case VPPCOM_ATTR_SET_SHUT:
3122 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3123 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3124 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3125 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3126 break;
3127
3128 case VPPCOM_ATTR_GET_SHUT:
3129 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3130 tmp_flags = 1;
3131 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3132 tmp_flags |= 2;
3133 if (tmp_flags == 1)
3134 *(int *) buffer = SHUT_RD;
3135 else if (tmp_flags == 2)
3136 *(int *) buffer = SHUT_WR;
3137 else if (tmp_flags == 3)
3138 *(int *) buffer = SHUT_RDWR;
3139 *buflen = sizeof (int);
3140 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003141 default:
3142 rv = VPPCOM_EINVAL;
3143 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003144 }
3145
Dave Wallace35830af2017-10-09 01:43:42 -04003146 return rv;
3147}
3148
Stevenac1f96d2017-10-24 16:03:58 -07003149int
Florin Coras134a9962018-08-28 11:32:04 -07003150vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003151 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3152{
Florin Coras134a9962018-08-28 11:32:04 -07003153 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003154 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003155 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003156
3157 if (ep)
3158 {
Florin Coras134a9962018-08-28 11:32:04 -07003159 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003160 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003161 {
Florin Coras0d427d82018-06-27 03:24:07 -07003162 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
Florin Coras134a9962018-08-28 11:32:04 -07003163 getpid (), session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003164 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003165 }
Florin Coras7e12d942018-06-27 14:32:43 -07003166 ep->is_ip4 = session->transport.is_ip4;
3167 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003168 }
Steven58f464e2017-10-25 12:33:12 -07003169
3170 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003171 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003172 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003173 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003174 else
3175 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003176 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003177 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003178 }
3179
Florin Coras99368312018-08-02 10:45:44 -07003180 if (ep)
3181 {
3182 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003183 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3184 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003185 else
Dave Barach178cf492018-11-13 16:34:13 -05003186 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3187 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003188 }
Florin Coras460dce62018-07-27 05:45:06 -07003189
Stevenac1f96d2017-10-24 16:03:58 -07003190 return rv;
3191}
3192
3193int
Florin Coras134a9962018-08-28 11:32:04 -07003194vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003195 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3196{
Dave Wallace617dffa2017-10-26 14:47:06 -04003197 if (!buffer)
3198 return VPPCOM_EINVAL;
3199
3200 if (ep)
3201 {
3202 // TBD
3203 return VPPCOM_EINVAL;
3204 }
3205
3206 if (flags)
3207 {
3208 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07003209 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3210 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003211 }
3212
Florin Coras42ceddb2018-12-12 10:56:01 -08003213 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003214}
3215
Dave Wallace048b1d62018-01-03 22:24:41 -05003216int
3217vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3218{
Florin Coras134a9962018-08-28 11:32:04 -07003219 vcl_worker_t *wrk = vcl_worker_get_current ();
3220 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003221 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003222 svm_msg_q_msg_t msg;
3223 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003224 int rv, num_ev = 0;
3225
Florin Coras0d427d82018-06-27 03:24:07 -07003226 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3227 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003228
3229 if (!vp)
3230 return VPPCOM_EFAULT;
3231
3232 do
3233 {
Florin Coras7e12d942018-06-27 14:32:43 -07003234 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003235
Florin Coras6917b942018-11-13 22:44:54 -08003236 /* Dequeue all events and drop all unhandled io events */
3237 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3238 {
3239 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3240 vcl_handle_mq_event (wrk, e);
3241 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3242 }
3243 vec_reset_length (wrk->unhandled_evts_vector);
3244
Dave Wallace048b1d62018-01-03 22:24:41 -05003245 for (i = 0; i < n_sids; i++)
3246 {
Florin Coras7baeb712019-01-04 17:05:43 -08003247 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003248 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003249 {
3250 vp[i].revents = POLLHUP;
3251 num_ev++;
3252 continue;
3253 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003254
Florin Coras6917b942018-11-13 22:44:54 -08003255 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003256
3257 if (POLLIN & vp[i].events)
3258 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003259 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003260 if (rv > 0)
3261 {
Florin Coras6917b942018-11-13 22:44:54 -08003262 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003263 num_ev++;
3264 }
3265 else if (rv < 0)
3266 {
3267 switch (rv)
3268 {
3269 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003270 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003271 break;
3272
3273 default:
Florin Coras6917b942018-11-13 22:44:54 -08003274 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003275 break;
3276 }
3277 num_ev++;
3278 }
3279 }
3280
3281 if (POLLOUT & vp[i].events)
3282 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003283 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003284 if (rv > 0)
3285 {
Florin Coras6917b942018-11-13 22:44:54 -08003286 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003287 num_ev++;
3288 }
3289 else if (rv < 0)
3290 {
3291 switch (rv)
3292 {
3293 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003294 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003295 break;
3296
3297 default:
Florin Coras6917b942018-11-13 22:44:54 -08003298 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003299 break;
3300 }
3301 num_ev++;
3302 }
3303 }
3304
Dave Wallace7e607a72018-06-18 18:41:32 -04003305 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003306 {
Florin Coras6917b942018-11-13 22:44:54 -08003307 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003308 num_ev++;
3309 }
3310 }
3311 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003312 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003313 }
3314 while ((num_ev == 0) && keep_trying);
3315
3316 if (VPPCOM_DEBUG > 3)
3317 {
3318 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3319 for (i = 0; i < n_sids; i++)
3320 {
3321 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
Florin Coras7baeb712019-01-04 17:05:43 -08003322 ".revents 0x%x", getpid (), i, vp[i].sh, vp[i].sh,
Florin Coras6917b942018-11-13 22:44:54 -08003323 vp[i].events, vp[i].revents);
Dave Wallace048b1d62018-01-03 22:24:41 -05003324 }
3325 }
3326 return num_ev;
3327}
3328
Florin Coras99368312018-08-02 10:45:44 -07003329int
3330vppcom_mq_epoll_fd (void)
3331{
Florin Coras134a9962018-08-28 11:32:04 -07003332 vcl_worker_t *wrk = vcl_worker_get_current ();
3333 return wrk->mqs_epfd;
3334}
3335
3336int
Florin Coras30e79c22019-01-02 19:31:22 -08003337vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003338{
3339 return session_handle & 0xFFFFFF;
3340}
3341
3342int
Florin Coras30e79c22019-01-02 19:31:22 -08003343vppcom_session_worker (vcl_session_handle_t session_handle)
3344{
3345 return session_handle >> 24;
3346}
3347
3348int
Florin Coras134a9962018-08-28 11:32:04 -07003349vppcom_worker_register (void)
3350{
Florin Coras47c40e22018-11-26 17:01:36 -08003351 if (!vcl_worker_alloc_and_init ())
3352 return VPPCOM_EEXIST;
3353
3354 if (vcl_worker_set_bapi ())
3355 return VPPCOM_EEXIST;
3356
3357 if (vcl_worker_register_with_vpp ())
3358 return VPPCOM_EEXIST;
3359
3360 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003361}
3362
Florin Corasdfe4cf42018-11-28 22:13:45 -08003363int
3364vppcom_worker_index (void)
3365{
3366 return vcl_get_worker_index ();
3367}
3368
Florin Corase0982e52019-01-25 13:19:56 -08003369int
3370vppcom_worker_mqs_epfd (void)
3371{
3372 vcl_worker_t *wrk = vcl_worker_get_current ();
3373 if (!vcm->cfg.use_mq_eventfd)
3374 return -1;
3375 return wrk->mqs_epfd;
3376}
3377
Dave Wallacee22aa742017-10-20 12:30:38 -04003378/*
3379 * fd.io coding-style-patch-verification: ON
3380 *
3381 * Local Variables:
3382 * eval: (c-set-style "gnu")
3383 * End:
3384 */