blob: 72ec4f4b966f265e8d6a49bc3e49dc5b2b01167b [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Corasd85de682018-11-29 17:02:29 -080025static int
26vcl_wait_for_segment (u64 segment_handle)
Florin Coras54693d22018-07-17 10:46:29 -070027{
Florin Corasd85de682018-11-29 17:02:29 -080028 vcl_worker_t *wrk = vcl_worker_get_current ();
29 u32 wait_for_seconds = 10, segment_index;
30 f64 timeout;
Florin Coras54693d22018-07-17 10:46:29 -070031
Florin Corasd85de682018-11-29 17:02:29 -080032 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
Florin Coras5f45e012019-01-23 09:21:30 -080033 return 0;
Florin Coras54693d22018-07-17 10:46:29 -070034
Florin Corasd85de682018-11-29 17:02:29 -080035 timeout = clib_time_now (&wrk->clib_time) + wait_for_seconds;
36 while (clib_time_now (&wrk->clib_time) < timeout)
Florin Coras54693d22018-07-17 10:46:29 -070037 {
Florin Corasd85de682018-11-29 17:02:29 -080038 segment_index = vcl_segment_table_lookup (segment_handle);
39 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
40 return 0;
41 usleep (10);
Florin Coras54693d22018-07-17 10:46:29 -070042 }
Florin Corasd85de682018-11-29 17:02:29 -080043 return 1;
Florin Coras54693d22018-07-17 10:46:29 -070044}
45
Florin Coras30e79c22019-01-02 19:31:22 -080046static inline int
Florin Corase003a1b2019-06-05 10:47:16 -070047vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq, u32 n_max_msg)
Florin Coras30e79c22019-01-02 19:31:22 -080048{
49 svm_msg_q_msg_t *msg;
50 u32 n_msgs;
51 int i;
52
Florin Corase003a1b2019-06-05 10:47:16 -070053 n_msgs = clib_min (svm_msg_q_size (mq), n_max_msg);
Florin Coras30e79c22019-01-02 19:31:22 -080054 for (i = 0; i < n_msgs; i++)
55 {
56 vec_add2 (wrk->mq_msg_vector, msg, 1);
57 svm_msg_q_sub_w_lock (mq, msg);
58 }
59 return n_msgs;
60}
61
Florin Coras697faea2018-06-27 17:10:49 -070062const char *
Florin Coras288eaab2019-02-03 15:26:14 -080063vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040064{
65 char *st;
66
67 switch (state)
68 {
69 case STATE_START:
70 st = "STATE_START";
71 break;
72
73 case STATE_CONNECT:
74 st = "STATE_CONNECT";
75 break;
76
77 case STATE_LISTEN:
78 st = "STATE_LISTEN";
79 break;
80
81 case STATE_ACCEPT:
82 st = "STATE_ACCEPT";
83 break;
84
Florin Coras3c7d4f92018-12-14 11:28:43 -080085 case STATE_VPP_CLOSING:
86 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050087 break;
88
Dave Wallace543852a2017-08-03 02:11:34 -040089 case STATE_DISCONNECT:
90 st = "STATE_DISCONNECT";
91 break;
92
93 case STATE_FAILED:
94 st = "STATE_FAILED";
95 break;
96
Florin Coras2d675d72019-01-28 15:54:27 -080097 case STATE_UPDATED:
98 st = "STATE_UPDATED";
99 break;
100
101 case STATE_LISTEN_NO_MQ:
102 st = "STATE_LISTEN_NO_MQ";
103 break;
104
Dave Wallace543852a2017-08-03 02:11:34 -0400105 default:
106 st = "UNKNOWN_STATE";
107 break;
108 }
109
110 return st;
111}
112
Dave Wallace543852a2017-08-03 02:11:34 -0400113u8 *
114format_ip4_address (u8 * s, va_list * args)
115{
116 u8 *a = va_arg (*args, u8 *);
117 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
118}
119
120u8 *
121format_ip6_address (u8 * s, va_list * args)
122{
123 ip6_address_t *a = va_arg (*args, ip6_address_t *);
124 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
125
126 i_max_n_zero = ARRAY_LEN (a->as_u16);
127 max_n_zeros = 0;
128 i_first_zero = i_max_n_zero;
129 n_zeros = 0;
130 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
131 {
132 u32 is_zero = a->as_u16[i] == 0;
133 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
134 {
135 i_first_zero = i;
136 n_zeros = 0;
137 }
138 n_zeros += is_zero;
139 if ((!is_zero && n_zeros > max_n_zeros)
140 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
141 {
142 i_max_n_zero = i_first_zero;
143 max_n_zeros = n_zeros;
144 i_first_zero = ARRAY_LEN (a->as_u16);
145 n_zeros = 0;
146 }
147 }
148
149 last_double_colon = 0;
150 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
151 {
152 if (i == i_max_n_zero && max_n_zeros > 1)
153 {
154 s = format (s, "::");
155 i += max_n_zeros - 1;
156 last_double_colon = 1;
157 }
158 else
159 {
160 s = format (s, "%s%x",
161 (last_double_colon || i == 0) ? "" : ":",
162 clib_net_to_host_u16 (a->as_u16[i]));
163 last_double_colon = 0;
164 }
165 }
166
167 return s;
168}
169
170/* Format an IP46 address. */
171u8 *
172format_ip46_address (u8 * s, va_list * args)
173{
174 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
175 ip46_type_t type = va_arg (*args, ip46_type_t);
176 int is_ip4 = 1;
177
178 switch (type)
179 {
180 case IP46_TYPE_ANY:
181 is_ip4 = ip46_address_is_ip4 (ip46);
182 break;
183 case IP46_TYPE_IP4:
184 is_ip4 = 1;
185 break;
186 case IP46_TYPE_IP6:
187 is_ip4 = 0;
188 break;
189 }
190
191 return is_ip4 ?
192 format (s, "%U", format_ip4_address, &ip46->ip4) :
193 format (s, "%U", format_ip6_address, &ip46->ip6);
194}
195
Florin Coras697faea2018-06-27 17:10:49 -0700196/*
197 * VPPCOM Utility Functions
198 */
199
Florin Coras458089b2019-08-21 16:20:44 -0700200static void
201vcl_send_session_listen (vcl_worker_t * wrk, vcl_session_t * s)
202{
203 app_session_evt_t _app_evt, *app_evt = &_app_evt;
204 session_listen_msg_t *mp;
205 svm_msg_q_t *mq;
206
207 mq = vcl_worker_ctrl_mq (wrk);
208 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_LISTEN);
209 mp = (session_listen_msg_t *) app_evt->evt->data;
210 memset (mp, 0, sizeof (*mp));
211 mp->client_index = wrk->my_client_index;
212 mp->context = s->session_index;
213 mp->wrk_index = wrk->vpp_wrk_index;
214 mp->is_ip4 = s->transport.is_ip4;
215 clib_memcpy_fast (&mp->ip, &s->transport.lcl_ip, sizeof (mp->ip));
216 mp->port = s->transport.lcl_port;
217 mp->proto = s->session_type;
218 app_send_ctrl_evt_to_vpp (mq, app_evt);
219}
220
221static void
222vcl_send_session_connect (vcl_worker_t * wrk, vcl_session_t * s)
223{
224 app_session_evt_t _app_evt, *app_evt = &_app_evt;
225 session_connect_msg_t *mp;
226 svm_msg_q_t *mq;
227
228 mq = vcl_worker_ctrl_mq (wrk);
229 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_CONNECT);
230 mp = (session_connect_msg_t *) app_evt->evt->data;
231 memset (mp, 0, sizeof (*mp));
232 mp->client_index = wrk->my_client_index;
233 mp->context = s->session_index;
234 mp->wrk_index = wrk->vpp_wrk_index;
235 mp->is_ip4 = s->transport.is_ip4;
236 mp->parent_handle = s->parent_handle;
237 clib_memcpy_fast (&mp->ip, &s->transport.rmt_ip, sizeof (mp->ip));
238 mp->port = s->transport.rmt_port;
239 mp->proto = s->session_type;
240 app_send_ctrl_evt_to_vpp (mq, app_evt);
241}
242
243void
244vcl_send_session_unlisten (vcl_worker_t * wrk, vcl_session_t * s)
245{
246 app_session_evt_t _app_evt, *app_evt = &_app_evt;
247 session_unlisten_msg_t *mp;
248 svm_msg_q_t *mq;
249
250 mq = vcl_worker_ctrl_mq (wrk);
251 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_UNLISTEN);
252 mp = (session_unlisten_msg_t *) app_evt->evt->data;
253 memset (mp, 0, sizeof (*mp));
254 mp->client_index = wrk->my_client_index;
255 mp->wrk_index = wrk->vpp_wrk_index;
256 mp->handle = s->vpp_handle;
257 mp->context = wrk->wrk_index;
258 app_send_ctrl_evt_to_vpp (mq, app_evt);
259}
260
261static void
262vcl_send_session_disconnect (vcl_worker_t * wrk, vcl_session_t * s)
263{
264 app_session_evt_t _app_evt, *app_evt = &_app_evt;
265 session_disconnect_msg_t *mp;
266 svm_msg_q_t *mq;
267
268 /* Send to thread that owns the session */
269 mq = s->vpp_evt_q;
270 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_DISCONNECT);
271 mp = (session_disconnect_msg_t *) app_evt->evt->data;
272 memset (mp, 0, sizeof (*mp));
273 mp->client_index = wrk->my_client_index;
274 mp->handle = s->vpp_handle;
275 app_send_ctrl_evt_to_vpp (mq, app_evt);
276}
277
278static void
279vcl_send_app_detach (vcl_worker_t * wrk)
280{
281 app_session_evt_t _app_evt, *app_evt = &_app_evt;
282 session_app_detach_msg_t *mp;
283 svm_msg_q_t *mq;
284
285 mq = vcl_worker_ctrl_mq (wrk);
286 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_APP_DETACH);
287 mp = (session_app_detach_msg_t *) app_evt->evt->data;
288 memset (mp, 0, sizeof (*mp));
289 mp->client_index = wrk->my_client_index;
290 app_send_ctrl_evt_to_vpp (mq, app_evt);
291}
Florin Coras697faea2018-06-27 17:10:49 -0700292
Florin Coras54693d22018-07-17 10:46:29 -0700293static void
294vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
295 session_handle_t handle, int retval)
296{
297 app_session_evt_t _app_evt, *app_evt = &_app_evt;
298 session_accepted_reply_msg_t *rmp;
299 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
300 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
301 rmp->handle = handle;
302 rmp->context = context;
303 rmp->retval = retval;
304 app_send_ctrl_evt_to_vpp (mq, app_evt);
305}
306
Florin Coras99368312018-08-02 10:45:44 -0700307static void
308vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
309 session_handle_t handle, int retval)
310{
311 app_session_evt_t _app_evt, *app_evt = &_app_evt;
312 session_disconnected_reply_msg_t *rmp;
313 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
314 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
315 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
316 rmp->handle = handle;
317 rmp->context = context;
318 rmp->retval = retval;
319 app_send_ctrl_evt_to_vpp (mq, app_evt);
320}
321
Florin Corasc9fbd662018-08-24 12:59:56 -0700322static void
323vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
324 session_handle_t handle, int retval)
325{
326 app_session_evt_t _app_evt, *app_evt = &_app_evt;
327 session_reset_reply_msg_t *rmp;
328 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
329 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
330 rmp->handle = handle;
331 rmp->context = context;
332 rmp->retval = retval;
333 app_send_ctrl_evt_to_vpp (mq, app_evt);
334}
335
Florin Coras30e79c22019-01-02 19:31:22 -0800336void
337vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
338 u32 wrk_index)
339{
340 app_session_evt_t _app_evt, *app_evt = &_app_evt;
341 session_worker_update_msg_t *mp;
342 svm_msg_q_t *mq;
343
344 mq = vcl_session_vpp_evt_q (wrk, s);
345 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
346 mp = (session_worker_update_msg_t *) app_evt->evt->data;
347 mp->client_index = wrk->my_client_index;
348 mp->handle = s->vpp_handle;
349 mp->req_wrk_index = wrk->vpp_wrk_index;
350 mp->wrk_index = wrk_index;
351 app_send_ctrl_evt_to_vpp (mq, app_evt);
352}
353
Florin Coras54693d22018-07-17 10:46:29 -0700354static u32
Florin Coras00cca802019-06-06 09:38:44 -0700355vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp,
356 u32 ls_index)
Florin Coras54693d22018-07-17 10:46:29 -0700357{
358 vcl_session_t *session, *listen_session;
359 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700360 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700361 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700362
Florin Coras134a9962018-08-28 11:32:04 -0700363 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700364
Florin Coras00cca802019-06-06 09:38:44 -0700365 listen_session = vcl_session_get (wrk, ls_index);
366 if (listen_session->vpp_handle != mp->listener_handle)
Florin Coras54693d22018-07-17 10:46:29 -0700367 {
Florin Coras00cca802019-06-06 09:38:44 -0700368 VDBG (0, "ERROR: listener handle %lu does not match session %u",
369 mp->listener_handle, ls_index);
370 goto error;
371 }
372
373 if (vcl_wait_for_segment (mp->segment_handle))
374 {
375 VDBG (0, "ERROR: segment for session %u couldn't be mounted!",
376 session->session_index);
377 goto error;
Florin Coras54693d22018-07-17 10:46:29 -0700378 }
379
Florin Coras54693d22018-07-17 10:46:29 -0700380 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
381 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Coras653e43f2019-03-04 10:56:23 -0800382 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
383 svm_msg_q_t *);
384 rx_fifo->client_session_index = session->session_index;
385 tx_fifo->client_session_index = session->session_index;
386 rx_fifo->client_thread_index = vcl_get_worker_index ();
387 tx_fifo->client_thread_index = vcl_get_worker_index ();
388 vpp_wrk_index = tx_fifo->master_thread_index;
389 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
390 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700391
392 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800393 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700394 session->rx_fifo = rx_fifo;
395 session->tx_fifo = tx_fifo;
396
397 session->session_state = STATE_ACCEPT;
Florin Coras09d18c22019-04-24 11:10:02 -0700398 session->transport.rmt_port = mp->rmt.port;
399 session->transport.is_ip4 = mp->rmt.is_ip4;
400 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500401 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700402
Florin Coras134a9962018-08-28 11:32:04 -0700403 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700404 session->transport.lcl_port = listen_session->transport.lcl_port;
405 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700406 session->session_type = listen_session->session_type;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200407 session->is_dgram = vcl_proto_is_dgram (session->session_type);
408 session->listener_index = listen_session->session_index;
409 listen_session->n_accepted_sessions++;
Florin Coras54693d22018-07-17 10:46:29 -0700410
Florin Coras5e062572019-03-14 19:07:51 -0700411 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
412 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras09d18c22019-04-24 11:10:02 -0700413 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
414 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
415 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700416 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
417
Florin Coras00cca802019-06-06 09:38:44 -0700418 vcl_send_session_accepted_reply (session->vpp_evt_q, mp->context,
419 session->vpp_handle, 0);
420
Florin Coras134a9962018-08-28 11:32:04 -0700421 return session->session_index;
Florin Coras00cca802019-06-06 09:38:44 -0700422
423error:
424 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
425 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
426 VNET_API_ERROR_INVALID_ARGUMENT);
427 vcl_session_free (wrk, session);
428 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700429}
430
431static u32
Florin Coras134a9962018-08-28 11:32:04 -0700432vcl_session_connected_handler (vcl_worker_t * wrk,
433 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700434{
Florin Coras99368312018-08-02 10:45:44 -0700435 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700436 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700437 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -0700438
439 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700440 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700441 if (!session)
442 {
Florin Coras5e062572019-03-14 19:07:51 -0700443 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
444 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700445 return VCL_INVALID_SESSION_INDEX;
446 }
Florin Coras54693d22018-07-17 10:46:29 -0700447 if (mp->retval)
448 {
Florin Coras5e062572019-03-14 19:07:51 -0700449 VDBG (0, "ERROR: session index %u: connect failed! %U",
450 session_index, format_api_error, ntohl (mp->retval));
Florin Coras070453d2018-08-24 17:04:27 -0700451 session->session_state = STATE_FAILED;
452 session->vpp_handle = mp->handle;
453 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700454 }
455
Florin Coras460dce62018-07-27 05:45:06 -0700456 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
457 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800458 if (vcl_wait_for_segment (mp->segment_handle))
459 {
Florin Coras653e43f2019-03-04 10:56:23 -0800460 VDBG (0, "segment for session %u couldn't be mounted!",
461 session->session_index);
Florin Coras5e062572019-03-14 19:07:51 -0700462 session->session_state = STATE_FAILED;
Florin Corasd85de682018-11-29 17:02:29 -0800463 return VCL_INVALID_SESSION_INDEX;
464 }
465
Florin Coras460dce62018-07-27 05:45:06 -0700466 rx_fifo->client_session_index = session_index;
467 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700468 rx_fifo->client_thread_index = vcl_get_worker_index ();
469 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700470
Florin Coras653e43f2019-03-04 10:56:23 -0800471 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
472 svm_msg_q_t *);
473 vpp_wrk_index = tx_fifo->master_thread_index;
474 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
475 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700476
Florin Coras653e43f2019-03-04 10:56:23 -0800477 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700478 {
Florin Coras653e43f2019-03-04 10:56:23 -0800479 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
480 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
481 if (vcl_wait_for_segment (mp->ct_segment_handle))
482 {
483 VDBG (0, "ct segment for session %u couldn't be mounted!",
484 session->session_index);
Florin Coras5e062572019-03-14 19:07:51 -0700485 session->session_state = STATE_FAILED;
Florin Coras653e43f2019-03-04 10:56:23 -0800486 return VCL_INVALID_SESSION_INDEX;
487 }
Florin Coras99368312018-08-02 10:45:44 -0700488 }
Florin Coras54693d22018-07-17 10:46:29 -0700489
Florin Coras54693d22018-07-17 10:46:29 -0700490 session->rx_fifo = rx_fifo;
491 session->tx_fifo = tx_fifo;
492 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800493 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras09d18c22019-04-24 11:10:02 -0700494 session->transport.is_ip4 = mp->lcl.is_ip4;
495 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500496 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700497 session->transport.lcl_port = mp->lcl.port;
Florin Coras54693d22018-07-17 10:46:29 -0700498 session->session_state = STATE_CONNECT;
499
500 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800501 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700502
Florin Coras5e062572019-03-14 19:07:51 -0700503 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
504 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700505 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700506
Florin Coras54693d22018-07-17 10:46:29 -0700507 return session_index;
508}
509
Florin Coras3c7d4f92018-12-14 11:28:43 -0800510static int
511vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
512{
513 vcl_session_msg_t *accepted_msg;
514 int i;
515
516 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
517 {
518 accepted_msg = &session->accept_evts_fifo[i];
519 if (accepted_msg->accepted_msg.handle == handle)
520 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800521 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800522 return 1;
523 }
524 }
525 return 0;
526}
527
Florin Corasc9fbd662018-08-24 12:59:56 -0700528static u32
Florin Coras134a9962018-08-28 11:32:04 -0700529vcl_session_reset_handler (vcl_worker_t * wrk,
530 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700531{
532 vcl_session_t *session;
533 u32 sid;
534
Florin Coras134a9962018-08-28 11:32:04 -0700535 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
536 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700537 if (!session)
538 {
539 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
540 return VCL_INVALID_SESSION_INDEX;
541 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800542
543 /* Caught a reset before actually accepting the session */
544 if (session->session_state == STATE_LISTEN)
545 {
546
547 if (!vcl_flag_accepted_session (session, reset_msg->handle,
548 VCL_ACCEPTED_F_RESET))
549 VDBG (0, "session was not accepted!");
550 return VCL_INVALID_SESSION_INDEX;
551 }
552
553 session->session_state = STATE_DISCONNECT;
554 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700555 return sid;
556}
557
Florin Coras60116992018-08-27 09:52:18 -0700558static u32
Florin Coras134a9962018-08-28 11:32:04 -0700559vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700560{
561 vcl_session_t *session;
562 u32 sid = mp->context;
563
Florin Coras134a9962018-08-28 11:32:04 -0700564 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700565 if (mp->retval)
566 {
Florin Coras5e062572019-03-14 19:07:51 -0700567 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Corasd85de682018-11-29 17:02:29 -0800568 format_api_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700569 if (session)
570 {
571 session->session_state = STATE_FAILED;
572 session->vpp_handle = mp->handle;
573 return sid;
574 }
575 else
576 {
Florin Coras5e062572019-03-14 19:07:51 -0700577 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
578 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700579 return VCL_INVALID_SESSION_INDEX;
580 }
581 }
582
583 session->vpp_handle = mp->handle;
584 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500585 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
586 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700587 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700588 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700589 session->session_state = STATE_LISTEN;
590
Florin Coras5f45e012019-01-23 09:21:30 -0800591 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
592 vec_validate (wrk->vpp_event_queues, 0);
593 wrk->vpp_event_queues[0] = session->vpp_evt_q;
594
Florin Coras60116992018-08-27 09:52:18 -0700595 if (session->is_dgram)
596 {
597 svm_fifo_t *rx_fifo, *tx_fifo;
598 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
599 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
600 rx_fifo->client_session_index = sid;
601 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
602 tx_fifo->client_session_index = sid;
603 session->rx_fifo = rx_fifo;
604 session->tx_fifo = tx_fifo;
605 }
606
Florin Coras05ecfcc2018-12-12 18:19:39 -0800607 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700608 return sid;
609}
610
Florin Corasdfae9f92019-02-20 19:48:31 -0800611static void
612vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
613{
614 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
615 vcl_session_t *s;
616
617 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
618 if (!s || s->session_state != STATE_DISCONNECT)
619 {
620 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
621 return;
622 }
623
624 if (mp->retval)
625 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
626 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
627
628 if (mp->context != wrk->wrk_index)
629 VDBG (0, "wrong context");
630
631 vcl_session_table_del_vpp_handle (wrk, mp->handle);
632 vcl_session_free (wrk, s);
633}
634
Florin Coras3c7d4f92018-12-14 11:28:43 -0800635static vcl_session_t *
636vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
637{
638 vcl_session_msg_t *vcl_msg;
639 vcl_session_t *session;
640
641 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
642 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800643 VWRN ("session overlap handle %lu state %u!", msg->handle,
644 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800645
646 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
647 if (!session)
648 {
649 VERR ("couldn't find listen session: listener handle %llx",
650 msg->listener_handle);
651 return 0;
652 }
653
654 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
655 vcl_msg->accepted_msg = *msg;
656 /* Session handle points to listener until fully accepted by app */
657 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
658
659 return session;
660}
661
662static vcl_session_t *
663vcl_session_disconnected_handler (vcl_worker_t * wrk,
664 session_disconnected_msg_t * msg)
665{
666 vcl_session_t *session;
667
668 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
669 if (!session)
670 {
671 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
672 return 0;
673 }
674
675 /* Caught a disconnect before actually accepting the session */
676 if (session->session_state == STATE_LISTEN)
677 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800678 if (!vcl_flag_accepted_session (session, msg->handle,
679 VCL_ACCEPTED_F_CLOSED))
680 VDBG (0, "session was not accepted!");
681 return 0;
682 }
683
684 session->session_state = STATE_VPP_CLOSING;
685 return session;
686}
687
Florin Coras30e79c22019-01-02 19:31:22 -0800688static void
689vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
690{
691 session_req_worker_update_msg_t *msg;
692 vcl_session_t *s;
693
694 msg = (session_req_worker_update_msg_t *) data;
695 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
696 if (!s)
697 return;
698
699 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
700}
701
702static void
703vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
704{
705 session_worker_update_reply_msg_t *msg;
706 vcl_session_t *s;
707
708 msg = (session_worker_update_reply_msg_t *) data;
709 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
710 if (!s)
711 {
712 VDBG (0, "unknown handle 0x%llx", msg->handle);
713 return;
714 }
715 if (vcl_wait_for_segment (msg->segment_handle))
716 {
717 clib_warning ("segment for session %u couldn't be mounted!",
718 s->session_index);
719 return;
720 }
Florin Coras30e79c22019-01-02 19:31:22 -0800721
Florin Coras5f45e012019-01-23 09:21:30 -0800722 if (s->rx_fifo)
723 {
724 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
725 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
726 s->rx_fifo->client_session_index = s->session_index;
727 s->tx_fifo->client_session_index = s->session_index;
728 s->rx_fifo->client_thread_index = wrk->wrk_index;
729 s->tx_fifo->client_thread_index = wrk->wrk_index;
730 }
Florin Coras30e79c22019-01-02 19:31:22 -0800731 s->session_state = STATE_UPDATED;
732
Florin Coras30e79c22019-01-02 19:31:22 -0800733 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
734 s->vpp_handle, wrk->wrk_index);
735}
736
Florin Coras86f04502018-09-12 16:08:01 -0700737static int
738vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700739{
Florin Coras54693d22018-07-17 10:46:29 -0700740 session_disconnected_msg_t *disconnected_msg;
Florin Coras54693d22018-07-17 10:46:29 -0700741 vcl_session_t *session;
Florin Coras54693d22018-07-17 10:46:29 -0700742
743 switch (e->event_type)
744 {
Florin Coras653e43f2019-03-04 10:56:23 -0800745 case SESSION_IO_EVT_RX:
746 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -0800747 session = vcl_session_get (wrk, e->session_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800748 if (!session || !(session->session_state & STATE_OPEN))
749 break;
Florin Coras86f04502018-09-12 16:08:01 -0700750 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700751 break;
752 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800753 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700754 break;
755 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700756 vcl_session_connected_handler (wrk,
757 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700758 break;
759 case SESSION_CTRL_EVT_DISCONNECTED:
760 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800761 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Corasc9fbd662018-08-24 12:59:56 -0700762 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800763 break;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800764 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
765 session->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700766 break;
767 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700768 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700769 break;
770 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700771 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700772 break;
Florin Corasdfae9f92019-02-20 19:48:31 -0800773 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
774 vcl_session_unlisten_reply_handler (wrk, e->data);
775 break;
Florin Coras30e79c22019-01-02 19:31:22 -0800776 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
777 vcl_session_req_worker_update_handler (wrk, e->data);
778 break;
779 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
780 vcl_session_worker_update_reply_handler (wrk, e->data);
781 break;
Florin Coras54693d22018-07-17 10:46:29 -0700782 default:
783 clib_warning ("unhandled %u", e->event_type);
784 }
785 return VPPCOM_OK;
786}
787
Florin Coras30e79c22019-01-02 19:31:22 -0800788static int
Florin Coras697faea2018-06-27 17:10:49 -0700789vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -0800790 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -0700791 f64 wait_for_time)
792{
Florin Coras134a9962018-08-28 11:32:04 -0700793 vcl_worker_t *wrk = vcl_worker_get_current ();
794 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700795 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700796 svm_msg_q_msg_t msg;
797 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400798
Florin Coras697faea2018-06-27 17:10:49 -0700799 do
Dave Wallace543852a2017-08-03 02:11:34 -0400800 {
Florin Coras134a9962018-08-28 11:32:04 -0700801 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700802 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700803 {
Florin Coras070453d2018-08-24 17:04:27 -0700804 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700805 }
806 if (session->session_state & state)
807 {
Florin Coras697faea2018-06-27 17:10:49 -0700808 return VPPCOM_OK;
809 }
810 if (session->session_state & STATE_FAILED)
811 {
Florin Coras697faea2018-06-27 17:10:49 -0700812 return VPPCOM_ECONNREFUSED;
813 }
Florin Coras54693d22018-07-17 10:46:29 -0700814
Florin Coras134a9962018-08-28 11:32:04 -0700815 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -0800816 {
817 usleep (100);
818 continue;
819 }
Florin Coras134a9962018-08-28 11:32:04 -0700820 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700821 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700822 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800823 }
Florin Coras134a9962018-08-28 11:32:04 -0700824 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800825
Florin Coras05ecfcc2018-12-12 18:19:39 -0800826 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -0700827 vppcom_session_state_str (state));
828 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400829
Florin Coras697faea2018-06-27 17:10:49 -0700830 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500831}
832
Florin Coras30e79c22019-01-02 19:31:22 -0800833static void
834vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
835{
Florin Coras288eaab2019-02-03 15:26:14 -0800836 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -0800837 vcl_session_t *s;
838 u32 *sip;
839
840 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
841 return;
842
843 vec_foreach (sip, wrk->pending_session_wrk_updates)
844 {
845 s = vcl_session_get (wrk, *sip);
846 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
847 state = s->session_state;
848 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
849 s->session_state = state;
850 }
851 vec_reset_length (wrk->pending_session_wrk_updates);
852}
853
Florin Corasf9240dc2019-01-15 08:03:17 -0800854void
Florin Coras30e79c22019-01-02 19:31:22 -0800855vcl_flush_mq_events (void)
856{
857 vcl_worker_t *wrk = vcl_worker_get_current ();
858 svm_msg_q_msg_t *msg;
859 session_event_t *e;
860 svm_msg_q_t *mq;
861 int i;
862
863 mq = wrk->app_event_queue;
864 svm_msg_q_lock (mq);
Florin Corase003a1b2019-06-05 10:47:16 -0700865 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras30e79c22019-01-02 19:31:22 -0800866 svm_msg_q_unlock (mq);
867
868 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
869 {
870 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
871 e = svm_msg_q_msg_data (mq, msg);
872 vcl_handle_mq_event (wrk, e);
873 svm_msg_q_free_msg (mq, msg);
874 }
875 vec_reset_length (wrk->mq_msg_vector);
876 vcl_handle_pending_wrk_updates (wrk);
877}
878
Florin Coras697faea2018-06-27 17:10:49 -0700879static int
880vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400881{
Florin Coras697faea2018-06-27 17:10:49 -0700882 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400883
Florin Coras697faea2018-06-27 17:10:49 -0700884 if (vcm->app_state != STATE_APP_ENABLED)
885 {
886 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -0700887 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -0700888 if (PREDICT_FALSE (rv))
889 {
Florin Coras5e062572019-03-14 19:07:51 -0700890 VDBG (0, "application session enable timed out! returning %d (%s)",
891 rv, vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -0700892 return rv;
893 }
894 }
895 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400896}
897
Florin Coras697faea2018-06-27 17:10:49 -0700898static int
899vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400900{
Florin Coras697faea2018-06-27 17:10:49 -0700901 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400902
Florin Coras697faea2018-06-27 17:10:49 -0700903 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -0700904 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -0700905 if (PREDICT_FALSE (rv))
906 {
Florin Coras5e062572019-03-14 19:07:51 -0700907 VDBG (0, "application attach timed out! returning %d (%s)", rv,
908 vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -0700909 return rv;
910 }
Dave Wallace543852a2017-08-03 02:11:34 -0400911
Florin Coras697faea2018-06-27 17:10:49 -0700912 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400913}
914
915static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700916vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400917{
Florin Coras134a9962018-08-28 11:32:04 -0700918 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Corasaaff5ee2019-07-08 13:00:00 -0700919 session_accepted_msg_t *accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -0700920 vcl_session_t *session = 0;
Florin Corasaaff5ee2019-07-08 13:00:00 -0700921 vcl_session_msg_t *evt;
Dave Wallace543852a2017-08-03 02:11:34 -0400922
Florin Corasab2f6db2018-08-31 14:31:41 -0700923 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700924 if (!session)
925 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500926
Florin Corasaaff5ee2019-07-08 13:00:00 -0700927 /* Flush pending accept events, if any */
928 while (clib_fifo_elts (session->accept_evts_fifo))
929 {
930 clib_fifo_sub2 (session->accept_evts_fifo, evt);
931 accepted_msg = &evt->accepted_msg;
932 vcl_session_table_del_vpp_handle (wrk, accepted_msg->handle);
933 vcl_send_session_accepted_reply (session->vpp_evt_q,
934 accepted_msg->context,
935 session->vpp_handle, -1);
936 }
937 clib_fifo_free (session->accept_evts_fifo);
938
Florin Coras458089b2019-08-21 16:20:44 -0700939 vcl_send_session_unlisten (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500940
Florin Coras5e062572019-03-14 19:07:51 -0700941 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
Florin Coras458089b2019-08-21 16:20:44 -0700942 session->vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -0700943 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras458089b2019-08-21 16:20:44 -0700944
945 session->vpp_handle = ~0;
946 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500947
Florin Coras070453d2018-08-24 17:04:27 -0700948 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400949}
950
Florin Coras697faea2018-06-27 17:10:49 -0700951static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700952vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400953{
Florin Coras134a9962018-08-28 11:32:04 -0700954 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700955 svm_msg_q_t *vpp_evt_q;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200956 vcl_session_t *session, *listen_session;
Florin Coras288eaab2019-02-03 15:26:14 -0800957 vcl_session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700958 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400959
Florin Corasab2f6db2018-08-31 14:31:41 -0700960 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -0700961 if (!session)
962 return VPPCOM_EBADFD;
963
Dave Wallace4878cbe2017-11-21 03:45:09 -0500964 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700965 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500966
Florin Coras5e062572019-03-14 19:07:51 -0700967 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
968 vpp_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400969
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800970 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400971 {
Florin Coras5e062572019-03-14 19:07:51 -0700972 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
Florin Coras070453d2018-08-24 17:04:27 -0700973 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500974 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400975
Florin Coras3c7d4f92018-12-14 11:28:43 -0800976 if (state & STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500977 {
Florin Coras134a9962018-08-28 11:32:04 -0700978 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras47c40e22018-11-26 17:01:36 -0800979 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
Florin Coras99368312018-08-02 10:45:44 -0700980 vpp_handle, 0);
Florin Coras5e062572019-03-14 19:07:51 -0700981 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
982 session->session_index, vpp_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400983 }
984 else
Dave Wallace227867f2017-11-13 21:21:53 -0500985 {
Florin Coras5e062572019-03-14 19:07:51 -0700986 VDBG (1, "session %u [0x%llx]: sending disconnect...",
987 session->session_index, vpp_handle);
Florin Coras458089b2019-08-21 16:20:44 -0700988 vcl_send_session_disconnect (wrk, session);
Dave Wallace227867f2017-11-13 21:21:53 -0500989 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400990
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +0200991 if (session->listener_index != VCL_INVALID_SESSION_INDEX)
992 {
993 listen_session = vcl_session_get (wrk, session->listener_index);
994 listen_session->n_accepted_sessions--;
995 }
996
Florin Coras070453d2018-08-24 17:04:27 -0700997 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400998}
999
Florin Coras940f78f2018-11-30 12:11:20 -08001000/**
1001 * Handle app exit
1002 *
1003 * Notify vpp of the disconnect and mark the worker as free. If we're the
1004 * last worker, do a full cleanup otherwise, since we're probably a forked
1005 * child, avoid syscalls as much as possible. We might've lost privileges.
1006 */
1007void
1008vppcom_app_exit (void)
1009{
1010 if (!pool_elts (vcm->workers))
1011 return;
Florin Coras01f3f892018-12-02 12:45:53 -08001012 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
1013 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -08001014 vcl_elog_stop (vcm);
1015 if (vec_len (vcm->workers) == 1)
Florin Coras470b2a62019-08-03 18:53:48 -07001016 vppcom_disconnect_from_vpp ();
Florin Coras940f78f2018-11-30 12:11:20 -08001017 else
Florin Coraseaec2a62018-12-04 16:34:05 -08001018 vl_client_send_disconnect (1 /* vpp should cleanup */ );
Florin Coras940f78f2018-11-30 12:11:20 -08001019}
1020
Dave Wallace543852a2017-08-03 02:11:34 -04001021/*
1022 * VPPCOM Public API functions
1023 */
1024int
1025vppcom_app_create (char *app_name)
1026{
Dave Wallace543852a2017-08-03 02:11:34 -04001027 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -04001028 int rv;
1029
Florin Coras47c40e22018-11-26 17:01:36 -08001030 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -04001031 {
Florin Coras955bfbb2018-12-04 13:43:45 -08001032 VDBG (1, "already initialized");
1033 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -04001034 }
1035
Florin Coras47c40e22018-11-26 17:01:36 -08001036 vcm->is_init = 1;
1037 vppcom_cfg (&vcm->cfg);
1038 vcl_cfg = &vcm->cfg;
1039
1040 vcm->main_cpu = pthread_self ();
1041 vcm->main_pid = getpid ();
1042 vcm->app_name = format (0, "%s", app_name);
1043 vppcom_init_error_string_table ();
Florin Coras88001c62019-04-24 14:44:46 -07001044 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
1045 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001046 pool_alloc (vcm->workers, vcl_cfg->max_workers);
1047 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -08001048 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -08001049 atexit (vppcom_app_exit);
Florin Coras47c40e22018-11-26 17:01:36 -08001050
1051 /* Allocate default worker */
1052 vcl_worker_alloc_and_init ();
1053
1054 /* API hookup and connect to VPP */
1055 vppcom_api_hookup ();
1056 vcl_elog_init (vcm);
1057 vcm->app_state = STATE_APP_START;
1058 rv = vppcom_connect_to_vpp (app_name);
1059 if (rv)
Dave Wallace543852a2017-08-03 02:11:34 -04001060 {
Florin Coras47c40e22018-11-26 17:01:36 -08001061 VERR ("couldn't connect to VPP!");
1062 return rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001063 }
Florin Coras47c40e22018-11-26 17:01:36 -08001064 VDBG (0, "sending session enable");
1065 rv = vppcom_app_session_enable ();
1066 if (rv)
1067 {
1068 VERR ("vppcom_app_session_enable() failed!");
1069 return rv;
1070 }
1071
1072 VDBG (0, "sending app attach");
1073 rv = vppcom_app_attach ();
1074 if (rv)
1075 {
1076 VERR ("vppcom_app_attach() failed!");
1077 return rv;
1078 }
1079
1080 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
1081 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001082
1083 return VPPCOM_OK;
1084}
1085
1086void
1087vppcom_app_destroy (void)
1088{
Dave Wallace543852a2017-08-03 02:11:34 -04001089 int rv;
Dave Wallacede910062018-03-20 09:22:13 -04001090 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -04001091
Florin Coras940f78f2018-11-30 12:11:20 -08001092 if (!pool_elts (vcm->workers))
1093 return;
1094
Florin Coras0d427d82018-06-27 03:24:07 -07001095 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001096
Florin Coras940f78f2018-11-30 12:11:20 -08001097 if (pool_elts (vcm->workers) == 1)
Florin Coras47c40e22018-11-26 17:01:36 -08001098 {
Florin Coras458089b2019-08-21 16:20:44 -07001099 vcl_send_app_detach (vcl_worker_get_current ());
Florin Coras47c40e22018-11-26 17:01:36 -08001100 orig_app_timeout = vcm->cfg.app_timeout;
1101 vcm->cfg.app_timeout = 2.0;
1102 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1103 vcm->cfg.app_timeout = orig_app_timeout;
1104 if (PREDICT_FALSE (rv))
1105 VDBG (0, "application detach timed out! returning %d (%s)", rv,
1106 vppcom_retval_str (rv));
Florin Coras940f78f2018-11-30 12:11:20 -08001107 vec_free (vcm->app_name);
Florin Coras01f3f892018-12-02 12:45:53 -08001108 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001109 }
1110 else
1111 {
Florin Coras01f3f892018-12-02 12:45:53 -08001112 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001113 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001114
Florin Coras01f3f892018-12-02 12:45:53 -08001115 vcl_set_worker_index (~0);
Florin Coras0d427d82018-06-27 03:24:07 -07001116 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04001117 vl_client_disconnect_from_vlib ();
Dave Wallace543852a2017-08-03 02:11:34 -04001118}
1119
1120int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001121vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001122{
Florin Coras134a9962018-08-28 11:32:04 -07001123 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001124 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001125
Florin Coras134a9962018-08-28 11:32:04 -07001126 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001127
Florin Coras7e12d942018-06-27 14:32:43 -07001128 session->session_type = proto;
1129 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001130 session->vpp_handle = ~0;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001131 session->is_dgram = vcl_proto_is_dgram (proto);
Dave Wallace543852a2017-08-03 02:11:34 -04001132
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001133 if (is_nonblocking)
1134 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001135
Florin Coras7e12d942018-06-27 14:32:43 -07001136 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1137 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001138
Florin Coras5e062572019-03-14 19:07:51 -07001139 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001140
Florin Coras134a9962018-08-28 11:32:04 -07001141 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001142}
1143
1144int
Florin Corasf9240dc2019-01-15 08:03:17 -08001145vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1146 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001147{
Florin Coras288eaab2019-02-03 15:26:14 -08001148 vcl_session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -07001149 u32 next_sh, vep_sh;
1150 int rv = VPPCOM_OK;
1151 u64 vpp_handle;
Florin Corasf9240dc2019-01-15 08:03:17 -08001152 u8 is_vep;
Florin Coras47c40e22018-11-26 17:01:36 -08001153
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001154 is_vep = session->is_vep;
Florin Coras134a9962018-08-28 11:32:04 -07001155 next_sh = session->vep.next_sh;
1156 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -07001157 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05001158 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001159
Florin Corasf9240dc2019-01-15 08:03:17 -08001160 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001161
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001162 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04001163 {
Florin Coras134a9962018-08-28 11:32:04 -07001164 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001165 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001166 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001167 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001168 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras47c40e22018-11-26 17:01:36 -08001169 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1170 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001171
Florin Coras134a9962018-08-28 11:32:04 -07001172 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001173 }
1174 }
1175 else
1176 {
Florin Coras47c40e22018-11-26 17:01:36 -08001177 if (session->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001178 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001179 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001180 if (rv < 0)
Florin Corasf9240dc2019-01-15 08:03:17 -08001181 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1182 "failed! rv %d (%s)", session->session_index, vpp_handle,
1183 vep_sh, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001184 }
1185
Florin Coras47c40e22018-11-26 17:01:36 -08001186 if (!do_disconnect)
Florin Coras30e79c22019-01-02 19:31:22 -08001187 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08001188 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Corasf9240dc2019-01-15 08:03:17 -08001189 session->session_index, vpp_handle);
Florin Coras30e79c22019-01-02 19:31:22 -08001190 goto cleanup;
1191 }
Florin Coras47c40e22018-11-26 17:01:36 -08001192
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001193 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001194 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001195 rv = vppcom_session_unbind (sh);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001196 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001197 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1198 "rv %d (%s)", session->session_index, vpp_handle, rv,
Florin Coras47c40e22018-11-26 17:01:36 -08001199 vppcom_retval_str (rv));
Florin Corasdfae9f92019-02-20 19:48:31 -08001200 return rv;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001201 }
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001202 else if ((state & STATE_OPEN)
1203 || (vcl_session_is_connectable_listener (wrk, session)))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001204 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001205 rv = vppcom_session_disconnect (sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001206 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001207 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1208 " rv %d (%s)", session->session_index, vpp_handle,
1209 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001210 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001211 else if (state == STATE_DISCONNECT)
1212 {
1213 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1214 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1215 session->vpp_handle, 0);
1216 }
Dave Wallace19481612017-09-15 18:47:44 -04001217 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001218
Florin Coras0ef8ef22019-01-18 08:37:13 -08001219 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1220
Florin Corasf9240dc2019-01-15 08:03:17 -08001221cleanup:
Florin Coras30e79c22019-01-02 19:31:22 -08001222 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Florin Coras134a9962018-08-28 11:32:04 -07001223 vcl_session_free (wrk, session);
Florin Coras0d427d82018-06-27 03:24:07 -07001224 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001225
Dave Wallace543852a2017-08-03 02:11:34 -04001226 return rv;
1227}
1228
1229int
Florin Corasf9240dc2019-01-15 08:03:17 -08001230vppcom_session_close (uint32_t session_handle)
1231{
1232 vcl_worker_t *wrk = vcl_worker_get_current ();
1233 vcl_session_t *session;
1234
1235 session = vcl_session_get_w_handle (wrk, session_handle);
1236 if (!session)
1237 return VPPCOM_EBADFD;
1238 return vcl_session_cleanup (wrk, session, session_handle,
1239 1 /* do_disconnect */ );
1240}
1241
1242int
Florin Coras134a9962018-08-28 11:32:04 -07001243vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001244{
Florin Coras134a9962018-08-28 11:32:04 -07001245 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001246 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001247
1248 if (!ep || !ep->ip)
1249 return VPPCOM_EINVAL;
1250
Florin Coras134a9962018-08-28 11:32:04 -07001251 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001252 if (!session)
1253 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001254
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001255 if (session->is_vep)
1256 {
Florin Coras5e062572019-03-14 19:07:51 -07001257 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1258 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001259 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001260 }
1261
Florin Coras7e12d942018-06-27 14:32:43 -07001262 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001263 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001264 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1265 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001266 else
Dave Barach178cf492018-11-13 16:34:13 -05001267 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1268 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001269 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001270
Florin Coras5e062572019-03-14 19:07:51 -07001271 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1272 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001273 session->transport.is_ip4 ? "IPv4" : "IPv6",
1274 format_ip46_address, &session->transport.lcl_ip,
1275 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1276 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001277 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001278 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001279
1280 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001281 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001282
Florin Coras070453d2018-08-24 17:04:27 -07001283 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001284}
1285
1286int
Florin Coras134a9962018-08-28 11:32:04 -07001287vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001288{
Florin Coras134a9962018-08-28 11:32:04 -07001289 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001290 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001291 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001292 int rv;
1293
Florin Coras134a9962018-08-28 11:32:04 -07001294 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001295 if (!listen_session || listen_session->is_vep)
Florin Coras070453d2018-08-24 17:04:27 -07001296 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001297
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001298 if (q_len == 0 || q_len == ~0)
1299 q_len = vcm->cfg.listen_queue_size;
1300
Dave Wallaceee45d412017-11-24 21:44:06 -05001301 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001302 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001303 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001304 VDBG (0, "session %u [0x%llx]: already in listen state!",
1305 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001306 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001307 }
1308
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001309 VDBG (0, "session %u: sending vpp listen request...", listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001310
Florin Coras070453d2018-08-24 17:04:27 -07001311 /*
1312 * Send listen request to vpp and wait for reply
1313 */
Florin Coras458089b2019-08-21 16:20:44 -07001314 vcl_send_session_listen (wrk, listen_session);
Florin Coras134a9962018-08-28 11:32:04 -07001315 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1316 STATE_LISTEN,
1317 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001318
Florin Coras070453d2018-08-24 17:04:27 -07001319 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001320 {
Florin Coras134a9962018-08-28 11:32:04 -07001321 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001322 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1323 listen_sh, listen_session->vpp_handle, rv,
1324 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001325 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001326 }
1327
Florin Coras070453d2018-08-24 17:04:27 -07001328 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001329}
1330
Ping Yu34a3a082018-11-30 19:16:17 -05001331int
1332vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1333 uint32_t cert_len)
1334{
1335
1336 vcl_worker_t *wrk = vcl_worker_get_current ();
1337 vcl_session_t *session = 0;
1338
1339 session = vcl_session_get_w_handle (wrk, session_handle);
1340 if (!session)
1341 return VPPCOM_EBADFD;
1342
1343 if (cert_len == 0 || cert_len == ~0)
1344 return VPPCOM_EBADFD;
1345
1346 /*
1347 * Send listen request to vpp and wait for reply
1348 */
1349 vppcom_send_application_tls_cert_add (session, cert, cert_len);
Florin Coras458089b2019-08-21 16:20:44 -07001350 vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1351 vcl_wait_for_app_state_change (STATE_APP_READY);
Ping Yu34a3a082018-11-30 19:16:17 -05001352 return VPPCOM_OK;
1353
1354}
1355
1356int
1357vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1358 uint32_t key_len)
1359{
1360
1361 vcl_worker_t *wrk = vcl_worker_get_current ();
1362 vcl_session_t *session = 0;
1363
1364 session = vcl_session_get_w_handle (wrk, session_handle);
1365 if (!session)
1366 return VPPCOM_EBADFD;
1367
1368 if (key_len == 0 || key_len == ~0)
1369 return VPPCOM_EBADFD;
1370
Ping Yu34a3a082018-11-30 19:16:17 -05001371 vppcom_send_application_tls_key_add (session, key, key_len);
Florin Coras458089b2019-08-21 16:20:44 -07001372 vcm->app_state = STATE_APP_ADDING_TLS_DATA;
1373 vcl_wait_for_app_state_change (STATE_APP_READY);
Ping Yu34a3a082018-11-30 19:16:17 -05001374 return VPPCOM_OK;
Ping Yu34a3a082018-11-30 19:16:17 -05001375}
1376
Florin Coras134a9962018-08-28 11:32:04 -07001377static int
Florin Coras5e062572019-03-14 19:07:51 -07001378validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001379{
Florin Coras5e062572019-03-14 19:07:51 -07001380 if (ls->is_vep)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001381 {
Florin Coras5e062572019-03-14 19:07:51 -07001382 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1383 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001384 return VPPCOM_EBADFD;
1385 }
1386
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001387 if ((ls->session_state != STATE_LISTEN)
1388 && (!vcl_session_is_connectable_listener (wrk, ls)))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001389 {
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001390 VDBG (0,
1391 "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1392 " (%s)", ls->vpp_handle, ls->session_state,
Florin Coras5e062572019-03-14 19:07:51 -07001393 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001394 return VPPCOM_EBADFD;
1395 }
1396 return VPPCOM_OK;
1397}
1398
1399int
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001400vppcom_unformat_proto (uint8_t * proto, char *proto_str)
1401{
1402 if (!strcmp (proto_str, "TCP"))
1403 *proto = VPPCOM_PROTO_TCP;
1404 else if (!strcmp (proto_str, "tcp"))
1405 *proto = VPPCOM_PROTO_TCP;
1406 else if (!strcmp (proto_str, "UDP"))
1407 *proto = VPPCOM_PROTO_UDP;
1408 else if (!strcmp (proto_str, "udp"))
1409 *proto = VPPCOM_PROTO_UDP;
1410 else if (!strcmp (proto_str, "UDPC"))
1411 *proto = VPPCOM_PROTO_UDPC;
1412 else if (!strcmp (proto_str, "udpc"))
1413 *proto = VPPCOM_PROTO_UDPC;
1414 else if (!strcmp (proto_str, "SCTP"))
1415 *proto = VPPCOM_PROTO_SCTP;
1416 else if (!strcmp (proto_str, "sctp"))
1417 *proto = VPPCOM_PROTO_SCTP;
1418 else if (!strcmp (proto_str, "TLS"))
1419 *proto = VPPCOM_PROTO_TLS;
1420 else if (!strcmp (proto_str, "tls"))
1421 *proto = VPPCOM_PROTO_TLS;
1422 else if (!strcmp (proto_str, "QUIC"))
1423 *proto = VPPCOM_PROTO_QUIC;
1424 else if (!strcmp (proto_str, "quic"))
1425 *proto = VPPCOM_PROTO_QUIC;
1426 else
1427 return 1;
1428 return 0;
1429}
1430
1431int
Florin Coras134a9962018-08-28 11:32:04 -07001432vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001433 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001434{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001435 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001436 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001437 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001438 vcl_session_t *listen_session = 0;
1439 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001440 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001441 svm_msg_q_msg_t msg;
1442 session_event_t *e;
1443 u8 is_nonblocking;
1444 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001445
Florin Coras134a9962018-08-28 11:32:04 -07001446 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001447 if (!listen_session)
1448 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001449
Florin Coras134a9962018-08-28 11:32:04 -07001450 listen_session_index = listen_session->session_index;
1451 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001452 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001453
Florin Coras54693d22018-07-17 10:46:29 -07001454 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001455 {
Florin Coras54693d22018-07-17 10:46:29 -07001456 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001457 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001458 accepted_msg = evt->accepted_msg;
1459 goto handle;
1460 }
1461
1462 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1463 VCL_SESS_ATTR_NONBLOCK);
Florin Coras134a9962018-08-28 11:32:04 -07001464 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
Florin Coras54693d22018-07-17 10:46:29 -07001465 return VPPCOM_EAGAIN;
1466
1467 while (1)
1468 {
Florin Coras134a9962018-08-28 11:32:04 -07001469 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001470 return VPPCOM_EAGAIN;
1471
Florin Coras134a9962018-08-28 11:32:04 -07001472 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001473 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001474 {
Florin Coras00cca802019-06-06 09:38:44 -07001475 VDBG (0, "discarded event: %u", e->event_type);
Florin Coras134a9962018-08-28 11:32:04 -07001476 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001477 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001478 }
Dave Barach178cf492018-11-13 16:34:13 -05001479 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001480 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001481 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001482 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001483
Florin Coras54693d22018-07-17 10:46:29 -07001484handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001485
Florin Coras00cca802019-06-06 09:38:44 -07001486 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg,
1487 listen_session_index);
1488 if (client_session_index == VCL_INVALID_SESSION_INDEX)
1489 return VPPCOM_ECONNABORTED;
1490
Florin Coras134a9962018-08-28 11:32:04 -07001491 listen_session = vcl_session_get (wrk, listen_session_index);
1492 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001493
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001494 if (flags & O_NONBLOCK)
1495 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001496
Florin Coras5e062572019-03-14 19:07:51 -07001497 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1498 " flags %d, is_nonblocking %u", listen_session->session_index,
1499 listen_session->vpp_handle, client_session_index,
1500 client_session->vpp_handle, flags,
1501 VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001502
Dave Wallace048b1d62018-01-03 22:24:41 -05001503 if (ep)
1504 {
Florin Coras7e12d942018-06-27 14:32:43 -07001505 ep->is_ip4 = client_session->transport.is_ip4;
1506 ep->port = client_session->transport.rmt_port;
1507 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001508 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1509 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001510 else
Dave Barach178cf492018-11-13 16:34:13 -05001511 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1512 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001513 }
Dave Wallace60caa062017-11-10 17:07:13 -05001514
Florin Coras05ecfcc2018-12-12 18:19:39 -08001515 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001516 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001517 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001518 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001519 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001520 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001521 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001522 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001523 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001524 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1525 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001526
Florin Coras3c7d4f92018-12-14 11:28:43 -08001527 /*
1528 * Session might have been closed already
1529 */
1530 if (accept_flags)
1531 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001532 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -08001533 client_session->session_state = STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001534 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasb0f662f2018-12-27 14:51:46 -08001535 client_session->session_state = STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001536 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001537 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001538}
1539
1540int
Florin Coras134a9962018-08-28 11:32:04 -07001541vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001542{
Florin Coras134a9962018-08-28 11:32:04 -07001543 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001544 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001545 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001546 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001547
Florin Coras134a9962018-08-28 11:32:04 -07001548 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001549 if (!session)
1550 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001551 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001552
1553 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001554 {
Florin Coras5e062572019-03-14 19:07:51 -07001555 VDBG (0, "ERROR: cannot connect epoll session %u!",
1556 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001557 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001558 }
1559
Florin Coras7e12d942018-06-27 14:32:43 -07001560 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001561 {
Florin Coras7baeb712019-01-04 17:05:43 -08001562 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001563 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001564 session_handle, session->vpp_handle,
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001565 session->transport.is_ip4 ? "IPv4" : "IPv6", format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001566 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001567 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001568 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001569 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001570 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001571 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001572 }
1573
Florin Coras7e12d942018-06-27 14:32:43 -07001574 session->transport.is_ip4 = server_ep->is_ip4;
1575 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001576 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1577 sizeof (ip4_address_t));
Dave Wallaced239f8d2018-06-19 13:37:30 -04001578 else
Dave Barach178cf492018-11-13 16:34:13 -05001579 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1580 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001581 session->transport.rmt_port = server_ep->port;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001582 session->parent_handle = VCL_INVALID_SESSION_HANDLE;
Dave Wallace543852a2017-08-03 02:11:34 -04001583
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001584 VDBG (0, "session handle %u: connecting to server %s %U "
1585 "port %d proto %s", session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001586 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001587 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001588 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001589 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001590 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001591 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001592
Florin Coras458089b2019-08-21 16:20:44 -07001593 vcl_send_session_connect (wrk, session);
Florin Coras57c88932019-08-29 12:03:17 -07001594
1595 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK))
1596 return VPPCOM_EINPROGRESS;
1597
1598 /*
1599 * Wait for reply from vpp if blocking
1600 */
Florin Coras070453d2018-08-24 17:04:27 -07001601 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1602 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001603
Florin Coras134a9962018-08-28 11:32:04 -07001604 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001605 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1606 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001607
Dave Wallace4878cbe2017-11-21 03:45:09 -05001608 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001609}
1610
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001611int
1612vppcom_session_stream_connect (uint32_t session_handle,
1613 uint32_t parent_session_handle)
1614{
1615 vcl_worker_t *wrk = vcl_worker_get_current ();
1616 vcl_session_t *session, *parent_session;
1617 u32 session_index, parent_session_index;
1618 int rv;
1619
1620 session = vcl_session_get_w_handle (wrk, session_handle);
1621 if (!session)
1622 return VPPCOM_EBADFD;
1623 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
1624 if (!parent_session)
1625 return VPPCOM_EBADFD;
1626
1627 session_index = session->session_index;
1628 parent_session_index = parent_session->session_index;
1629 if (PREDICT_FALSE (session->is_vep))
1630 {
1631 VDBG (0, "ERROR: cannot connect epoll session %u!",
1632 session->session_index);
1633 return VPPCOM_EBADFD;
1634 }
1635
1636 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
1637 {
1638 VDBG (0, "session handle %u [0x%llx]: session already "
1639 "connected to session %u [0x%llx] proto %s, state 0x%x (%s)",
1640 session_handle, session->vpp_handle,
1641 parent_session_handle, parent_session->vpp_handle,
1642 vppcom_proto_str (session->session_type), session->session_state,
1643 vppcom_session_state_str (session->session_state));
1644 return VPPCOM_OK;
1645 }
1646
1647 /* Connect to quic session specifics */
1648 session->transport.is_ip4 = parent_session->transport.is_ip4;
1649 session->transport.rmt_ip.ip4.as_u32 = (uint32_t) 1;
1650 session->transport.rmt_port = 0;
Nathan Skrzypczak8ac1d6d2019-07-17 11:02:20 +02001651 session->parent_handle = parent_session->vpp_handle;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001652
1653 VDBG (0, "session handle %u: connecting to session %u [0x%llx]",
1654 session_handle, parent_session_handle, parent_session->vpp_handle);
1655
1656 /*
1657 * Send connect request and wait for reply from vpp
1658 */
Florin Coras458089b2019-08-21 16:20:44 -07001659 vcl_send_session_connect (wrk, session);
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001660 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1661 vcm->cfg.session_timeout);
1662
1663 session->listener_index = parent_session_index;
1664 parent_session = vcl_session_get_w_handle (wrk, parent_session_handle);
Florin Coras028eaf02019-07-19 12:15:52 -07001665 if (parent_session)
1666 parent_session->n_accepted_sessions++;
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02001667
1668 session = vcl_session_get (wrk, session_index);
1669 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1670 session->vpp_handle, rv ? "failed" : "succeeded");
1671
1672 return rv;
1673}
1674
Florin Coras54693d22018-07-17 10:46:29 -07001675static u8
1676vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1677{
Florin Corasc0737e92019-03-04 14:19:39 -08001678 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001679}
1680
Steven58f464e2017-10-25 12:33:12 -07001681static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001682vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001683 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001684{
Florin Coras134a9962018-08-28 11:32:04 -07001685 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001686 int n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001687 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001688 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001689 svm_msg_q_msg_t msg;
1690 session_event_t *e;
1691 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001692 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001693
Florin Coras070453d2018-08-24 17:04:27 -07001694 if (PREDICT_FALSE (!buf))
1695 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001696
Florin Coras134a9962018-08-28 11:32:04 -07001697 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001698 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001699 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001700
Florin Coras6d0106e2019-01-29 20:11:58 -08001701 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001702 {
Florin Coras5e062572019-03-14 19:07:51 -07001703 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001704 s->session_index, s->vpp_handle, s->session_state,
1705 vppcom_session_state_str (s->session_state));
1706 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001707 }
1708
Florin Coras2cba8532018-09-11 16:33:36 -07001709 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001710 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001711 mq = wrk->app_event_queue;
1712 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001713 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001714
Sirshak Das28aa5392019-02-05 01:33:33 -06001715 if (svm_fifo_is_empty_cons (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001716 {
Florin Coras54693d22018-07-17 10:46:29 -07001717 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001718 {
1719 svm_fifo_unset_event (s->rx_fifo);
1720 return VPPCOM_EWOULDBLOCK;
1721 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001722 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001723 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001724 if (vcl_session_is_closing (s))
1725 return vcl_session_closing_error (s);
1726
Florin Corasc0737e92019-03-04 14:19:39 -08001727 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001728 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001729 if (svm_msg_q_is_empty (mq))
1730 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001731
Florin Coras54693d22018-07-17 10:46:29 -07001732 svm_msg_q_sub_w_lock (mq, &msg);
1733 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001734 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001735 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001736 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001737 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001738 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001739 }
Florin Coras54693d22018-07-17 10:46:29 -07001740
Florin Coras460dce62018-07-27 05:45:06 -07001741 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001742 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001743 else
Florin Coras99368312018-08-02 10:45:44 -07001744 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001745
Sirshak Das28aa5392019-02-05 01:33:33 -06001746 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001747 svm_fifo_unset_event (s->rx_fifo);
Florin Coras41c9e042018-09-11 00:10:41 -07001748
Florin Coras317b8e02019-04-17 09:57:46 -07001749 /* Cut-through sessions might request tx notifications on rx fifos */
Florin Coras2d379d82019-06-28 12:45:12 -07001750 if (PREDICT_FALSE (rx_fifo->want_deq_ntf))
Florin Coras317b8e02019-04-17 09:57:46 -07001751 {
1752 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1753 SESSION_IO_EVT_RX, SVM_Q_WAIT);
Florin Coras2d379d82019-06-28 12:45:12 -07001754 svm_fifo_reset_has_deq_ntf (s->rx_fifo);
Florin Coras317b8e02019-04-17 09:57:46 -07001755 }
1756
Florin Coras5e062572019-03-14 19:07:51 -07001757 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1758 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001759
Florin Coras54693d22018-07-17 10:46:29 -07001760 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001761}
1762
Steven58f464e2017-10-25 12:33:12 -07001763int
Florin Coras134a9962018-08-28 11:32:04 -07001764vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001765{
Florin Coras134a9962018-08-28 11:32:04 -07001766 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001767}
1768
1769static int
Florin Coras134a9962018-08-28 11:32:04 -07001770vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001771{
Florin Coras134a9962018-08-28 11:32:04 -07001772 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001773}
1774
Florin Coras2cba8532018-09-11 16:33:36 -07001775int
1776vppcom_session_read_segments (uint32_t session_handle,
1777 vppcom_data_segments_t ds)
1778{
1779 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001780 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001781 vcl_session_t *s = 0;
1782 svm_fifo_t *rx_fifo;
1783 svm_msg_q_msg_t msg;
1784 session_event_t *e;
1785 svm_msg_q_t *mq;
1786 u8 is_ct;
1787
1788 s = vcl_session_get_w_handle (wrk, session_handle);
1789 if (PREDICT_FALSE (!s || s->is_vep))
1790 return VPPCOM_EBADFD;
1791
Florin Coras6d0106e2019-01-29 20:11:58 -08001792 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1793 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001794
1795 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1796 is_ct = vcl_session_is_ct (s);
1797 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1798 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001799 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001800
Florin Coras653e43f2019-03-04 10:56:23 -08001801 if (is_ct)
1802 svm_fifo_unset_event (s->rx_fifo);
1803
Sirshak Das28aa5392019-02-05 01:33:33 -06001804 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001805 {
1806 if (is_nonblocking)
1807 {
1808 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001809 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001810 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001811 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001812 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001813 if (vcl_session_is_closing (s))
1814 return vcl_session_closing_error (s);
1815
Florin Coras2cba8532018-09-11 16:33:36 -07001816 svm_fifo_unset_event (rx_fifo);
1817 svm_msg_q_lock (mq);
1818 if (svm_msg_q_is_empty (mq))
1819 svm_msg_q_wait (mq);
1820
1821 svm_msg_q_sub_w_lock (mq, &msg);
1822 e = svm_msg_q_msg_data (mq, &msg);
1823 svm_msg_q_unlock (mq);
1824 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001825 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001826 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001827 }
1828 }
1829
Florin Coras88001c62019-04-24 14:44:46 -07001830 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001831 svm_fifo_unset_event (rx_fifo);
1832
Florin Coras2cba8532018-09-11 16:33:36 -07001833 return n_read;
1834}
1835
1836void
1837vppcom_session_free_segments (uint32_t session_handle,
1838 vppcom_data_segments_t ds)
1839{
1840 vcl_worker_t *wrk = vcl_worker_get_current ();
1841 vcl_session_t *s;
1842
1843 s = vcl_session_get_w_handle (wrk, session_handle);
1844 if (PREDICT_FALSE (!s || s->is_vep))
1845 return;
1846
Florin Coras88001c62019-04-24 14:44:46 -07001847 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001848}
1849
Florin Coras2cba8532018-09-11 16:33:36 -07001850int
1851vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1852{
1853 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001854 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001855 if (first_copy < max_bytes)
1856 {
Dave Barach178cf492018-11-13 16:34:13 -05001857 clib_memcpy_fast (buf + first_copy, ds[1].data,
1858 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001859 }
1860 return 0;
1861}
1862
Florin Coras54693d22018-07-17 10:46:29 -07001863static u8
1864vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1865{
Florin Corasc0737e92019-03-04 14:19:39 -08001866 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001867}
1868
Florin Coras42ceddb2018-12-12 10:56:01 -08001869static inline int
1870vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1871 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001872{
Florin Coras134a9962018-08-28 11:32:04 -07001873 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001874 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001875 vcl_session_t *s = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001876 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001877 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08001878 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001879 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001880 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001881 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001882
Florin Coras070453d2018-08-24 17:04:27 -07001883 if (PREDICT_FALSE (!buf))
1884 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001885
Florin Coras134a9962018-08-28 11:32:04 -07001886 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001887 if (PREDICT_FALSE (!s))
1888 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001889
Florin Coras460dce62018-07-27 05:45:06 -07001890 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001891 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001892 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1893 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001894 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001895 }
1896
Florin Coras6d0106e2019-01-29 20:11:58 -08001897 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001898 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001899 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1900 s->session_index, s->vpp_handle, s->session_state,
1901 vppcom_session_state_str (s->session_state));
1902 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001903 }
1904
Florin Coras0e88e852018-09-17 22:09:02 -07001905 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001906 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07001907 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06001908
Florin Coras653e43f2019-03-04 10:56:23 -08001909 mq = wrk->app_event_queue;
Sirshak Das28aa5392019-02-05 01:33:33 -06001910 if (svm_fifo_is_full_prod (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001911 {
Florin Coras54693d22018-07-17 10:46:29 -07001912 if (is_nonblocking)
1913 {
Florin Coras070453d2018-08-24 17:04:27 -07001914 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001915 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001916 while (svm_fifo_is_full_prod (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001917 {
Florin Coras2d379d82019-06-28 12:45:12 -07001918 svm_fifo_add_want_deq_ntf (tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001919 if (vcl_session_is_closing (s))
1920 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001921 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001922 if (svm_msg_q_is_empty (mq))
1923 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001924
Florin Coras54693d22018-07-17 10:46:29 -07001925 svm_msg_q_sub_w_lock (mq, &msg);
1926 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001927 svm_msg_q_unlock (mq);
1928
Florin Coras0e88e852018-09-17 22:09:02 -07001929 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001930 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001931 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001932 }
Dave Wallace543852a2017-08-03 02:11:34 -04001933 }
Dave Wallace543852a2017-08-03 02:11:34 -04001934
Florin Coras653e43f2019-03-04 10:56:23 -08001935 et = SESSION_IO_EVT_TX;
1936 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08001937 et = SESSION_IO_EVT_TX_FLUSH;
1938
Florin Coras460dce62018-07-27 05:45:06 -07001939 if (s->is_dgram)
1940 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08001941 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001942 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07001943 else
1944 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001945 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08001946
Florin Coras14ed6df2019-03-06 21:13:42 -08001947 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001948 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1949 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001950
Florin Coras460dce62018-07-27 05:45:06 -07001951 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001952
Florin Coras6d0106e2019-01-29 20:11:58 -08001953 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1954 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001955
Florin Coras54693d22018-07-17 10:46:29 -07001956 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001957}
1958
Florin Coras42ceddb2018-12-12 10:56:01 -08001959int
1960vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1961{
1962 return vppcom_session_write_inline (session_handle, buf, n,
1963 0 /* is_flush */ );
1964}
1965
Florin Corasb0f662f2018-12-27 14:51:46 -08001966int
1967vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1968{
1969 return vppcom_session_write_inline (session_handle, buf, n,
1970 1 /* is_flush */ );
1971}
1972
Florin Corasc0737e92019-03-04 14:19:39 -08001973#define vcl_fifo_rx_evt_valid_or_break(_s) \
1974if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1975 { \
1976 if (!vcl_session_is_ct (_s)) \
1977 { \
1978 svm_fifo_unset_event (_s->rx_fifo); \
1979 if (svm_fifo_is_empty (_s->rx_fifo)) \
1980 break; \
1981 } \
1982 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1983 { \
1984 svm_fifo_unset_event (_s->ct_rx_fifo); \
1985 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1986 break; \
1987 } \
1988 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07001989
Florin Coras86f04502018-09-12 16:08:01 -07001990static void
1991vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1992 unsigned long n_bits, unsigned long *read_map,
1993 unsigned long *write_map,
1994 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001995{
1996 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001997 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001998 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001999 u32 sid;
2000
2001 switch (e->event_type)
2002 {
Florin Corasc0737e92019-03-04 14:19:39 -08002003 case SESSION_IO_EVT_RX:
2004 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002005 session = vcl_session_get (wrk, sid);
2006 if (!session)
2007 break;
Florin Corasfff68f72019-03-13 16:01:38 -07002008 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002009 if (sid < n_bits && read_map)
2010 {
David Johnsond9818dd2018-12-14 14:53:41 -05002011 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002012 *bits_set += 1;
2013 }
2014 break;
Florin Corasfe97da32019-03-06 10:09:04 -08002015 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002016 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002017 session = vcl_session_get (wrk, sid);
2018 if (!session)
2019 break;
2020 if (sid < n_bits && write_map)
2021 {
David Johnsond9818dd2018-12-14 14:53:41 -05002022 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002023 *bits_set += 1;
2024 }
2025 break;
Florin Coras86f04502018-09-12 16:08:01 -07002026 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002027 session = vcl_session_accepted (wrk,
2028 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002029 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002030 break;
Florin Coras86f04502018-09-12 16:08:01 -07002031 sid = session->session_index;
2032 if (sid < n_bits && read_map)
2033 {
David Johnsond9818dd2018-12-14 14:53:41 -05002034 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002035 *bits_set += 1;
2036 }
2037 break;
2038 case SESSION_CTRL_EVT_CONNECTED:
2039 connected_msg = (session_connected_msg_t *) e->data;
Florin Coras57c88932019-08-29 12:03:17 -07002040 sid = vcl_session_connected_handler (wrk, connected_msg);
2041 if (sid == VCL_INVALID_SESSION_INDEX)
2042 break;
2043 if (sid < n_bits && write_map)
2044 {
2045 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
2046 *bits_set += 1;
2047 }
Florin Coras86f04502018-09-12 16:08:01 -07002048 break;
2049 case SESSION_CTRL_EVT_DISCONNECTED:
2050 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002051 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2052 if (!session)
2053 break;
2054 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002055 if (sid < n_bits && except_map)
2056 {
David Johnsond9818dd2018-12-14 14:53:41 -05002057 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002058 *bits_set += 1;
2059 }
2060 break;
2061 case SESSION_CTRL_EVT_RESET:
2062 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2063 if (sid < n_bits && except_map)
2064 {
David Johnsond9818dd2018-12-14 14:53:41 -05002065 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07002066 *bits_set += 1;
2067 }
2068 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002069 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2070 vcl_session_unlisten_reply_handler (wrk, e->data);
2071 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002072 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2073 vcl_session_worker_update_reply_handler (wrk, e->data);
2074 break;
2075 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2076 vcl_session_req_worker_update_handler (wrk, e->data);
2077 break;
Florin Coras86f04502018-09-12 16:08:01 -07002078 default:
2079 clib_warning ("unhandled: %u", e->event_type);
2080 break;
2081 }
2082}
2083
2084static int
2085vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2086 unsigned long n_bits, unsigned long *read_map,
2087 unsigned long *write_map, unsigned long *except_map,
2088 double time_to_wait, u32 * bits_set)
2089{
Florin Coras99368312018-08-02 10:45:44 -07002090 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002091 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002092 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002093
2094 svm_msg_q_lock (mq);
2095 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002096 {
Florin Coras54693d22018-07-17 10:46:29 -07002097 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002098 {
Florin Coras54693d22018-07-17 10:46:29 -07002099 svm_msg_q_unlock (mq);
2100 return 0;
2101 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002102
Florin Coras54693d22018-07-17 10:46:29 -07002103 if (!time_to_wait)
2104 {
2105 svm_msg_q_unlock (mq);
2106 return 0;
2107 }
2108 else if (time_to_wait < 0)
2109 {
2110 svm_msg_q_wait (mq);
2111 }
2112 else
2113 {
2114 if (svm_msg_q_timedwait (mq, time_to_wait))
2115 {
2116 svm_msg_q_unlock (mq);
2117 return 0;
2118 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002119 }
2120 }
Florin Corase003a1b2019-06-05 10:47:16 -07002121 vcl_mq_dequeue_batch (wrk, mq, ~0);
Florin Coras54693d22018-07-17 10:46:29 -07002122 svm_msg_q_unlock (mq);
2123
Florin Coras134a9962018-08-28 11:32:04 -07002124 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002125 {
Florin Coras134a9962018-08-28 11:32:04 -07002126 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002127 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002128 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2129 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002130 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002131 }
Florin Coras134a9962018-08-28 11:32:04 -07002132 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002133 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002134 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002135}
2136
Florin Coras99368312018-08-02 10:45:44 -07002137static int
Florin Coras294afe22019-01-07 17:49:17 -08002138vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2139 vcl_si_set * read_map, vcl_si_set * write_map,
2140 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002141 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002142{
Florin Coras14ed6df2019-03-06 21:13:42 -08002143 double wait = 0, start = 0;
2144
2145 if (!*bits_set)
2146 {
2147 wait = time_to_wait;
2148 start = clib_time_now (&wrk->clib_time);
2149 }
2150
2151 do
2152 {
2153 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2154 write_map, except_map, wait, bits_set);
2155 if (*bits_set)
2156 return *bits_set;
2157 if (wait == -1)
2158 continue;
2159
2160 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2161 }
2162 while (wait > 0);
2163
2164 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002165}
2166
2167static int
Florin Coras294afe22019-01-07 17:49:17 -08002168vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2169 vcl_si_set * read_map, vcl_si_set * write_map,
2170 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002171 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002172{
2173 vcl_mq_evt_conn_t *mqc;
2174 int __clib_unused n_read;
2175 int n_mq_evts, i;
2176 u64 buf;
2177
Florin Coras134a9962018-08-28 11:32:04 -07002178 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2179 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2180 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002181 for (i = 0; i < n_mq_evts; i++)
2182 {
Florin Coras134a9962018-08-28 11:32:04 -07002183 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002184 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002185 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002186 except_map, 0, bits_set);
2187 }
2188
2189 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2190}
2191
Dave Wallace543852a2017-08-03 02:11:34 -04002192int
Florin Coras294afe22019-01-07 17:49:17 -08002193vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2194 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002195{
Florin Coras54693d22018-07-17 10:46:29 -07002196 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002197 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002198 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002199 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04002200
Dave Wallace7876d392017-10-19 03:53:57 -04002201 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002202 {
Florin Coras134a9962018-08-28 11:32:04 -07002203 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002204 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002205 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2206 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002207 }
Dave Wallace7876d392017-10-19 03:53:57 -04002208 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002209 {
Florin Coras134a9962018-08-28 11:32:04 -07002210 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002211 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002212 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2213 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002214 }
Dave Wallace7876d392017-10-19 03:53:57 -04002215 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002216 {
Florin Coras134a9962018-08-28 11:32:04 -07002217 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002218 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002219 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2220 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002221 }
2222
Florin Coras54693d22018-07-17 10:46:29 -07002223 if (!n_bits)
2224 return 0;
2225
2226 if (!write_map)
2227 goto check_rd;
2228
2229 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002230 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2231 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002232 {
Florin Coras47c40e22018-11-26 17:01:36 -08002233 if (except_map && sid < minbits)
2234 clib_bitmap_set_no_check (except_map, sid, 1);
2235 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002236 }
2237
Sirshak Das28aa5392019-02-05 01:33:33 -06002238 rv = svm_fifo_is_full_prod (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002239 if (!rv)
2240 {
David Johnsond9818dd2018-12-14 14:53:41 -05002241 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002242 bits_set++;
2243 }
Florin Coras294afe22019-01-07 17:49:17 -08002244 else
Florin Coras2d379d82019-06-28 12:45:12 -07002245 svm_fifo_add_want_deq_ntf (session->tx_fifo, SVM_FIFO_WANT_DEQ_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002246 }));
2247
2248check_rd:
2249 if (!read_map)
2250 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002251
Florin Coras134a9962018-08-28 11:32:04 -07002252 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2253 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002254 {
Florin Coras47c40e22018-11-26 17:01:36 -08002255 if (except_map && sid < minbits)
2256 clib_bitmap_set_no_check (except_map, sid, 1);
2257 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002258 }
2259
Florin Coras0ef8ef22019-01-18 08:37:13 -08002260 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002261 if (rv)
2262 {
David Johnsond9818dd2018-12-14 14:53:41 -05002263 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002264 bits_set++;
2265 }
2266 }));
2267 /* *INDENT-ON* */
2268
2269check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002270
Florin Coras86f04502018-09-12 16:08:01 -07002271 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2272 {
2273 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2274 read_map, write_map, except_map, &bits_set);
2275 }
2276 vec_reset_length (wrk->unhandled_evts_vector);
2277
Florin Coras99368312018-08-02 10:45:44 -07002278 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002279 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002280 time_to_wait, &bits_set);
2281 else
Florin Coras134a9962018-08-28 11:32:04 -07002282 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002283 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002284
Dave Wallace543852a2017-08-03 02:11:34 -04002285 return (bits_set);
2286}
2287
Dave Wallacef7f809c2017-10-03 01:48:42 -04002288static inline void
Florin Coras7e5e62b2019-07-30 14:08:23 -07002289vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002290{
Florin Coras7e12d942018-06-27 14:32:43 -07002291 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002292 vppcom_epoll_t *vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002293 u32 sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002294
Florin Corasc0737e92019-03-04 14:19:39 -08002295 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002296 return;
2297
Florin Coras7e5e62b2019-07-30 14:08:23 -07002298 session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002299 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002300 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002301 VDBG (0, "ERROR: Invalid vep_sh (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002302 goto done;
2303 }
2304 if (PREDICT_FALSE (!session->is_vep))
2305 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002306 VDBG (0, "ERROR: vep_sh (%u) is not a vep!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002307 goto done;
2308 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002309 vep = &session->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002310 VDBG (0, "vep_sh (%u): Dumping epoll chain\n"
Florin Coras5e062572019-03-14 19:07:51 -07002311 "{\n"
2312 " is_vep = %u\n"
2313 " is_vep_session = %u\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002314 " next_sh = 0x%x (%u)\n"
2315 "}\n", vep_handle, session->is_vep, session->is_vep_session,
Florin Coras5e062572019-03-14 19:07:51 -07002316 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002317
Florin Coras7e5e62b2019-07-30 14:08:23 -07002318 for (sh = vep->next_sh; sh != ~0; sh = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002319 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002320 session = vcl_session_get_w_handle (wrk, sh);
Florin Coras070453d2018-08-24 17:04:27 -07002321 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002322 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002323 VDBG (0, "ERROR: Invalid sh (%u)!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002324 goto done;
2325 }
2326 if (PREDICT_FALSE (session->is_vep))
Florin Coras5e062572019-03-14 19:07:51 -07002327 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002328 VDBG (0, "ERROR: sh (%u) is a vep!", vep_handle);
Florin Coras5e062572019-03-14 19:07:51 -07002329 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002330 else if (PREDICT_FALSE (!session->is_vep_session))
2331 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002332 VDBG (0, "ERROR: sh (%u) is not a vep session handle!", sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002333 goto done;
2334 }
2335 vep = &session->vep;
Florin Coras7e5e62b2019-07-30 14:08:23 -07002336 if (PREDICT_FALSE (vep->vep_sh != vep_handle))
2337 VDBG (0, "ERROR: session (%u) vep_sh (%u) != vep_sh (%u)!",
2338 sh, session->vep.vep_sh, vep_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002339 if (session->is_vep_session)
2340 {
Florin Coras7e5e62b2019-07-30 14:08:23 -07002341 VDBG (0, "vep_sh[%u]: sh 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002342 "{\n"
Florin Coras7e5e62b2019-07-30 14:08:23 -07002343 " next_sh = 0x%x (%u)\n"
2344 " prev_sh = 0x%x (%u)\n"
2345 " vep_sh = 0x%x (%u)\n"
Florin Coras5e062572019-03-14 19:07:51 -07002346 " ev.events = 0x%x\n"
2347 " ev.data.u64 = 0x%llx\n"
2348 " et_mask = 0x%x\n"
2349 "}\n",
Florin Coras7e5e62b2019-07-30 14:08:23 -07002350 vep_handle, sh, sh, vep->next_sh, vep->next_sh, vep->prev_sh,
Florin Coras5e062572019-03-14 19:07:51 -07002351 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2352 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002353 }
2354 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002355
2356done:
Florin Coras7e5e62b2019-07-30 14:08:23 -07002357 VDBG (0, "vep_sh (%u): Dump complete!\n", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002358}
2359
2360int
2361vppcom_epoll_create (void)
2362{
Florin Coras134a9962018-08-28 11:32:04 -07002363 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002364 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002365
Florin Coras134a9962018-08-28 11:32:04 -07002366 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002367
2368 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002369 vep_session->vep.vep_sh = ~0;
2370 vep_session->vep.next_sh = ~0;
2371 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002372 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002373
Florin Corasa7a1a222018-12-30 17:11:31 -08002374 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2375 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002376
Florin Corasab2f6db2018-08-31 14:31:41 -07002377 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002378}
2379
2380int
Florin Coras134a9962018-08-28 11:32:04 -07002381vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002382 struct epoll_event *event)
2383{
Florin Coras134a9962018-08-28 11:32:04 -07002384 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002385 vcl_session_t *vep_session;
2386 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002387 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002388
Florin Coras134a9962018-08-28 11:32:04 -07002389 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002390 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002391 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002392 return VPPCOM_EINVAL;
2393 }
2394
Florin Coras134a9962018-08-28 11:32:04 -07002395 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002396 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002397 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002398 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002399 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002400 }
2401 if (PREDICT_FALSE (!vep_session->is_vep))
2402 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002403 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002404 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002405 }
2406
Florin Coras134a9962018-08-28 11:32:04 -07002407 ASSERT (vep_session->vep.vep_sh == ~0);
2408 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002409
Florin Coras134a9962018-08-28 11:32:04 -07002410 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002411 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002412 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002413 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002414 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002415 }
2416 if (PREDICT_FALSE (session->is_vep))
2417 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002418 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002419 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002420 }
2421
2422 switch (op)
2423 {
2424 case EPOLL_CTL_ADD:
2425 if (PREDICT_FALSE (!event))
2426 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002427 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002428 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002429 }
Florin Coras134a9962018-08-28 11:32:04 -07002430 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002431 {
Florin Coras7e12d942018-06-27 14:32:43 -07002432 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002433 next_session = vcl_session_get_w_handle (wrk,
2434 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002435 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002436 {
Florin Coras5e062572019-03-14 19:07:51 -07002437 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002438 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002439 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002440 }
Florin Coras134a9962018-08-28 11:32:04 -07002441 ASSERT (next_session->vep.prev_sh == vep_handle);
2442 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002443 }
Florin Coras134a9962018-08-28 11:32:04 -07002444 session->vep.next_sh = vep_session->vep.next_sh;
2445 session->vep.prev_sh = vep_handle;
2446 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002447 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2448 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002449 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002450 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002451 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002452
Florin Coras1bcad5c2019-01-09 20:04:38 -08002453 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002454 svm_fifo_add_want_deq_ntf (session->tx_fifo,
2455 SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002456
Florin Corasa7a1a222018-12-30 17:11:31 -08002457 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2458 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002459 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002460 break;
2461
2462 case EPOLL_CTL_MOD:
2463 if (PREDICT_FALSE (!event))
2464 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002465 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002466 rv = VPPCOM_EINVAL;
2467 goto done;
2468 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002469 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002470 {
Florin Coras5e062572019-03-14 19:07:51 -07002471 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002472 rv = VPPCOM_EINVAL;
2473 goto done;
2474 }
Florin Coras134a9962018-08-28 11:32:04 -07002475 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002476 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002477 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2478 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002479 rv = VPPCOM_EINVAL;
2480 goto done;
2481 }
2482 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2483 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002484 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2485 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002486 break;
2487
2488 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002489 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002490 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002491 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002492 rv = VPPCOM_EINVAL;
2493 goto done;
2494 }
Florin Coras134a9962018-08-28 11:32:04 -07002495 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002496 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002497 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2498 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002499 rv = VPPCOM_EINVAL;
2500 goto done;
2501 }
2502
Florin Coras134a9962018-08-28 11:32:04 -07002503 if (session->vep.prev_sh == vep_handle)
2504 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002505 else
2506 {
Florin Coras7e12d942018-06-27 14:32:43 -07002507 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002508 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002509 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002510 {
Florin Coras5e062572019-03-14 19:07:51 -07002511 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002512 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002513 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002514 }
Florin Coras134a9962018-08-28 11:32:04 -07002515 ASSERT (prev_session->vep.next_sh == session_handle);
2516 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002517 }
Florin Coras134a9962018-08-28 11:32:04 -07002518 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002519 {
Florin Coras7e12d942018-06-27 14:32:43 -07002520 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002521 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002522 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002523 {
Florin Coras5e062572019-03-14 19:07:51 -07002524 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002525 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002526 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002527 }
Florin Coras134a9962018-08-28 11:32:04 -07002528 ASSERT (next_session->vep.prev_sh == session_handle);
2529 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002530 }
2531
2532 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002533 session->vep.next_sh = ~0;
2534 session->vep.prev_sh = ~0;
2535 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002536 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002537
2538 if (session->tx_fifo)
Florin Coras2d379d82019-06-28 12:45:12 -07002539 svm_fifo_del_want_deq_ntf (session->tx_fifo, SVM_FIFO_NO_DEQ_NOTIF);
Florin Coras1bcad5c2019-01-09 20:04:38 -08002540
Florin Coras5e062572019-03-14 19:07:51 -07002541 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002542 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002543 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002544 break;
2545
2546 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002547 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002548 rv = VPPCOM_EINVAL;
2549 }
2550
Florin Coras134a9962018-08-28 11:32:04 -07002551 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002552
2553done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002554 return rv;
2555}
2556
Florin Coras86f04502018-09-12 16:08:01 -07002557static inline void
2558vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2559 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002560{
2561 session_disconnected_msg_t *disconnected_msg;
2562 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002563 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002564 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002565 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002566 u8 add_event = 0;
2567
2568 switch (e->event_type)
2569 {
Florin Coras653e43f2019-03-04 10:56:23 -08002570 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002571 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002572 if (!(session = vcl_session_get (wrk, sid)))
2573 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002574 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002575 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002576 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002577 break;
2578 add_event = 1;
2579 events[*num_ev].events |= EPOLLIN;
2580 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002581 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002582 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002583 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002584 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002585 if (!(session = vcl_session_get (wrk, sid)))
2586 break;
Florin Coras86f04502018-09-12 16:08:01 -07002587 session_events = session->vep.ev.events;
2588 if (!(EPOLLOUT & session_events))
2589 break;
2590 add_event = 1;
2591 events[*num_ev].events |= EPOLLOUT;
2592 session_evt_data = session->vep.ev.data.u64;
Florin Coras2d379d82019-06-28 12:45:12 -07002593 svm_fifo_reset_has_deq_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002594 break;
Florin Coras86f04502018-09-12 16:08:01 -07002595 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002596 session = vcl_session_accepted (wrk,
2597 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002598 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002599 break;
Florin Coras86f04502018-09-12 16:08:01 -07002600
Florin Coras86f04502018-09-12 16:08:01 -07002601 session_events = session->vep.ev.events;
2602 if (!(EPOLLIN & session_events))
2603 break;
2604
2605 add_event = 1;
2606 events[*num_ev].events |= EPOLLIN;
2607 session_evt_data = session->vep.ev.data.u64;
2608 break;
2609 case SESSION_CTRL_EVT_CONNECTED:
2610 connected_msg = (session_connected_msg_t *) e->data;
2611 vcl_session_connected_handler (wrk, connected_msg);
2612 /* Generate EPOLLOUT because there's no connected event */
2613 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002614 if (!(session = vcl_session_get (wrk, sid)))
2615 break;
Florin Coras86f04502018-09-12 16:08:01 -07002616 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002617 if (!(EPOLLOUT & session_events))
2618 break;
2619 add_event = 1;
2620 events[*num_ev].events |= EPOLLOUT;
2621 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002622 break;
2623 case SESSION_CTRL_EVT_DISCONNECTED:
2624 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002625 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2626 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002627 break;
Florin Coras72f77822019-01-22 19:05:52 -08002628 session_events = session->vep.ev.events;
2629 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2630 break;
Florin Coras86f04502018-09-12 16:08:01 -07002631 add_event = 1;
2632 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2633 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002634 break;
2635 case SESSION_CTRL_EVT_RESET:
2636 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2637 if (!(session = vcl_session_get (wrk, sid)))
2638 break;
Florin Coras72f77822019-01-22 19:05:52 -08002639 session_events = session->vep.ev.events;
2640 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2641 break;
Florin Coras86f04502018-09-12 16:08:01 -07002642 add_event = 1;
2643 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2644 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002645 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002646 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2647 vcl_session_unlisten_reply_handler (wrk, e->data);
2648 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002649 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2650 vcl_session_req_worker_update_handler (wrk, e->data);
2651 break;
2652 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2653 vcl_session_worker_update_reply_handler (wrk, e->data);
2654 break;
Florin Coras86f04502018-09-12 16:08:01 -07002655 default:
2656 VDBG (0, "unhandled: %u", e->event_type);
2657 break;
2658 }
2659
2660 if (add_event)
2661 {
2662 events[*num_ev].data.u64 = session_evt_data;
2663 if (EPOLLONESHOT & session_events)
2664 {
2665 session = vcl_session_get (wrk, sid);
2666 session->vep.ev.events = 0;
2667 }
2668 *num_ev += 1;
2669 }
2670}
2671
2672static int
2673vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2674 struct epoll_event *events, u32 maxevents,
2675 double wait_for_time, u32 * num_ev)
2676{
Florin Coras99368312018-08-02 10:45:44 -07002677 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002678 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002679 int i;
2680
Florin Coras539663c2018-09-28 14:59:37 -07002681 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2682 goto handle_dequeued;
2683
Florin Coras54693d22018-07-17 10:46:29 -07002684 svm_msg_q_lock (mq);
2685 if (svm_msg_q_is_empty (mq))
2686 {
2687 if (!wait_for_time)
2688 {
2689 svm_msg_q_unlock (mq);
2690 return 0;
2691 }
2692 else if (wait_for_time < 0)
2693 {
2694 svm_msg_q_wait (mq);
2695 }
2696 else
2697 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002698 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002699 {
2700 svm_msg_q_unlock (mq);
2701 return 0;
2702 }
2703 }
2704 }
Florin Corase003a1b2019-06-05 10:47:16 -07002705 ASSERT (maxevents > *num_ev);
2706 vcl_mq_dequeue_batch (wrk, mq, maxevents - *num_ev);
Florin Coras54693d22018-07-17 10:46:29 -07002707 svm_msg_q_unlock (mq);
2708
Florin Coras539663c2018-09-28 14:59:37 -07002709handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002710 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002711 {
Florin Coras134a9962018-08-28 11:32:04 -07002712 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002713 e = svm_msg_q_msg_data (mq, msg);
Florin Corase003a1b2019-06-05 10:47:16 -07002714 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
Florin Coras99368312018-08-02 10:45:44 -07002715 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002716 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002717 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002718 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002719 return *num_ev;
2720}
2721
Florin Coras99368312018-08-02 10:45:44 -07002722static int
Florin Coras134a9962018-08-28 11:32:04 -07002723vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002724 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002725{
Florin Corase003a1b2019-06-05 10:47:16 -07002726 double wait = 0, start = 0, now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002727
2728 if (!n_evts)
2729 {
2730 wait = wait_for_time;
2731 start = clib_time_now (&wrk->clib_time);
2732 }
2733
2734 do
2735 {
2736 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2737 wait, &n_evts);
2738 if (n_evts)
2739 return n_evts;
2740 if (wait == -1)
2741 continue;
2742
Florin Corase003a1b2019-06-05 10:47:16 -07002743 now = clib_time_now (&wrk->clib_time);
2744 wait -= now - start;
2745 start = now;
Florin Coras14ed6df2019-03-06 21:13:42 -08002746 }
2747 while (wait > 0);
2748
2749 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002750}
2751
2752static int
Florin Coras134a9962018-08-28 11:32:04 -07002753vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002754 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002755{
2756 vcl_mq_evt_conn_t *mqc;
2757 int __clib_unused n_read;
2758 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002759 u64 buf;
2760
Florin Coras134a9962018-08-28 11:32:04 -07002761 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002762again:
Florin Coras134a9962018-08-28 11:32:04 -07002763 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2764 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002765 for (i = 0; i < n_mq_evts; i++)
2766 {
Florin Coras134a9962018-08-28 11:32:04 -07002767 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002768 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002769 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002770 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002771 if (!n_evts && n_mq_evts > 0)
2772 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002773
2774 return (int) n_evts;
2775}
2776
Dave Wallacef7f809c2017-10-03 01:48:42 -04002777int
Florin Coras134a9962018-08-28 11:32:04 -07002778vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002779 int maxevents, double wait_for_time)
2780{
Florin Coras134a9962018-08-28 11:32:04 -07002781 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002782 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002783 u32 n_evts = 0;
2784 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002785
2786 if (PREDICT_FALSE (maxevents <= 0))
2787 {
Florin Coras5e062572019-03-14 19:07:51 -07002788 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002789 return VPPCOM_EINVAL;
2790 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002791
Florin Coras134a9962018-08-28 11:32:04 -07002792 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002793 if (!vep_session)
2794 return VPPCOM_EBADFD;
2795
Florin Coras54693d22018-07-17 10:46:29 -07002796 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002797 {
Florin Coras5e062572019-03-14 19:07:51 -07002798 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002799 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002800 }
Florin Coras54693d22018-07-17 10:46:29 -07002801
2802 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002803
Florin Coras86f04502018-09-12 16:08:01 -07002804 if (vec_len (wrk->unhandled_evts_vector))
2805 {
2806 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2807 {
2808 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2809 events, &n_evts);
2810 if (n_evts == maxevents)
2811 {
Florin Corase003a1b2019-06-05 10:47:16 -07002812 vec_delete (wrk->unhandled_evts_vector, i + 1, 0);
2813 return n_evts;
Florin Coras86f04502018-09-12 16:08:01 -07002814 }
2815 }
Florin Corase003a1b2019-06-05 10:47:16 -07002816 vec_reset_length (wrk->unhandled_evts_vector);
Florin Coras86f04502018-09-12 16:08:01 -07002817 }
2818
2819 if (vcm->cfg.use_mq_eventfd)
2820 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2821 wait_for_time);
2822
2823 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2824 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002825}
2826
Dave Wallace35830af2017-10-09 01:43:42 -04002827int
Florin Coras134a9962018-08-28 11:32:04 -07002828vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002829 void *buffer, uint32_t * buflen)
2830{
Florin Coras134a9962018-08-28 11:32:04 -07002831 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002832 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002833 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002834 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002835 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002836
Florin Coras134a9962018-08-28 11:32:04 -07002837 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002838 if (!session)
2839 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002840
Dave Wallace35830af2017-10-09 01:43:42 -04002841 switch (op)
2842 {
2843 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002844 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002845 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2846 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002847 break;
2848
Dave Wallace227867f2017-11-13 21:21:53 -05002849 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002850 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002851 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2852 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002853 break;
2854
2855 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002856 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002857 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002858 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2859 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002860 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07002861 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2862 "is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002863 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002864 }
2865 else
2866 rv = VPPCOM_EINVAL;
2867 break;
2868
2869 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002870 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002871 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002872 if (*flags & O_NONBLOCK)
2873 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2874 else
2875 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2876
Florin Coras5e062572019-03-14 19:07:51 -07002877 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2878 " is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002879 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002880 }
2881 else
2882 rv = VPPCOM_EINVAL;
2883 break;
2884
2885 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002886 if (PREDICT_TRUE (buffer && buflen &&
2887 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002888 {
Florin Coras7e12d942018-06-27 14:32:43 -07002889 ep->is_ip4 = session->transport.is_ip4;
2890 ep->port = session->transport.rmt_port;
2891 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002892 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2893 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002894 else
Dave Barach178cf492018-11-13 16:34:13 -05002895 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2896 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002897 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002898 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2899 "addr = %U, port %u", session_handle, ep->is_ip4,
2900 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002901 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2902 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002903 }
2904 else
2905 rv = VPPCOM_EINVAL;
2906 break;
2907
2908 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002909 if (PREDICT_TRUE (buffer && buflen &&
2910 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002911 {
Florin Coras7e12d942018-06-27 14:32:43 -07002912 ep->is_ip4 = session->transport.is_ip4;
2913 ep->port = session->transport.lcl_port;
2914 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002915 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2916 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002917 else
Dave Barach178cf492018-11-13 16:34:13 -05002918 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2919 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002920 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002921 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2922 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002923 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002924 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2925 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002926 }
2927 else
2928 rv = VPPCOM_EINVAL;
2929 break;
Stevenb5a11602017-10-11 09:59:30 -07002930
Dave Wallace048b1d62018-01-03 22:24:41 -05002931 case VPPCOM_ATTR_GET_LIBC_EPFD:
2932 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07002933 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002934 break;
2935
2936 case VPPCOM_ATTR_SET_LIBC_EPFD:
2937 if (PREDICT_TRUE (buffer && buflen &&
2938 (*buflen == sizeof (session->libc_epfd))))
2939 {
2940 session->libc_epfd = *(int *) buffer;
2941 *buflen = sizeof (session->libc_epfd);
2942
Florin Coras5e062572019-03-14 19:07:51 -07002943 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2944 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002945 }
2946 else
2947 rv = VPPCOM_EINVAL;
2948 break;
2949
2950 case VPPCOM_ATTR_GET_PROTOCOL:
2951 if (buffer && buflen && (*buflen >= sizeof (int)))
2952 {
Florin Coras7e12d942018-06-27 14:32:43 -07002953 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002954 *buflen = sizeof (int);
2955
Florin Coras5e062572019-03-14 19:07:51 -07002956 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2957 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002958 }
2959 else
2960 rv = VPPCOM_EINVAL;
2961 break;
2962
2963 case VPPCOM_ATTR_GET_LISTEN:
2964 if (buffer && buflen && (*buflen >= sizeof (int)))
2965 {
2966 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2967 VCL_SESS_ATTR_LISTEN);
2968 *buflen = sizeof (int);
2969
Florin Coras5e062572019-03-14 19:07:51 -07002970 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
2971 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002972 }
2973 else
2974 rv = VPPCOM_EINVAL;
2975 break;
2976
2977 case VPPCOM_ATTR_GET_ERROR:
2978 if (buffer && buflen && (*buflen >= sizeof (int)))
2979 {
2980 *(int *) buffer = 0;
2981 *buflen = sizeof (int);
2982
Florin Coras5e062572019-03-14 19:07:51 -07002983 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2984 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002985 }
2986 else
2987 rv = VPPCOM_EINVAL;
2988 break;
2989
2990 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2991 if (buffer && buflen && (*buflen >= sizeof (u32)))
2992 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002993
2994 /* VPP-TBD */
2995 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002996 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002997 vcm->cfg.tx_fifo_size);
2998 *buflen = sizeof (u32);
2999
Florin Coras5e062572019-03-14 19:07:51 -07003000 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3001 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
3002 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003003 }
3004 else
3005 rv = VPPCOM_EINVAL;
3006 break;
3007
3008 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3009 if (buffer && buflen && (*buflen == sizeof (u32)))
3010 {
3011 /* VPP-TBD */
3012 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003013 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
3014 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3015 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003016 }
3017 else
3018 rv = VPPCOM_EINVAL;
3019 break;
3020
3021 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3022 if (buffer && buflen && (*buflen >= sizeof (u32)))
3023 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003024
3025 /* VPP-TBD */
3026 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003027 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05003028 vcm->cfg.rx_fifo_size);
3029 *buflen = sizeof (u32);
3030
Florin Coras5e062572019-03-14 19:07:51 -07003031 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
3032 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003033 }
3034 else
3035 rv = VPPCOM_EINVAL;
3036 break;
3037
3038 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3039 if (buffer && buflen && (*buflen == sizeof (u32)))
3040 {
3041 /* VPP-TBD */
3042 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07003043 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
3044 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
3045 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003046 }
3047 else
3048 rv = VPPCOM_EINVAL;
3049 break;
3050
3051 case VPPCOM_ATTR_GET_REUSEADDR:
3052 if (buffer && buflen && (*buflen >= sizeof (int)))
3053 {
3054 /* VPP-TBD */
3055 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3056 VCL_SESS_ATTR_REUSEADDR);
3057 *buflen = sizeof (int);
3058
Florin Coras5e062572019-03-14 19:07:51 -07003059 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3060 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003061 }
3062 else
3063 rv = VPPCOM_EINVAL;
3064 break;
3065
Stevenb5a11602017-10-11 09:59:30 -07003066 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003067 if (buffer && buflen && (*buflen == sizeof (int)) &&
3068 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3069 {
3070 /* VPP-TBD */
3071 if (*(int *) buffer)
3072 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3073 else
3074 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3075
Florin Coras5e062572019-03-14 19:07:51 -07003076 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
3077 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
3078 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003079 }
3080 else
3081 rv = VPPCOM_EINVAL;
3082 break;
3083
3084 case VPPCOM_ATTR_GET_REUSEPORT:
3085 if (buffer && buflen && (*buflen >= sizeof (int)))
3086 {
3087 /* VPP-TBD */
3088 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3089 VCL_SESS_ATTR_REUSEPORT);
3090 *buflen = sizeof (int);
3091
Florin Coras5e062572019-03-14 19:07:51 -07003092 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3093 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003094 }
3095 else
3096 rv = VPPCOM_EINVAL;
3097 break;
3098
3099 case VPPCOM_ATTR_SET_REUSEPORT:
3100 if (buffer && buflen && (*buflen == sizeof (int)) &&
3101 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3102 {
3103 /* VPP-TBD */
3104 if (*(int *) buffer)
3105 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3106 else
3107 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3108
Florin Coras5e062572019-03-14 19:07:51 -07003109 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
3110 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
3111 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003112 }
3113 else
3114 rv = VPPCOM_EINVAL;
3115 break;
3116
3117 case VPPCOM_ATTR_GET_BROADCAST:
3118 if (buffer && buflen && (*buflen >= sizeof (int)))
3119 {
3120 /* VPP-TBD */
3121 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3122 VCL_SESS_ATTR_BROADCAST);
3123 *buflen = sizeof (int);
3124
Florin Coras5e062572019-03-14 19:07:51 -07003125 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3126 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003127 }
3128 else
3129 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003130 break;
3131
3132 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003133 if (buffer && buflen && (*buflen == sizeof (int)))
3134 {
3135 /* VPP-TBD */
3136 if (*(int *) buffer)
3137 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3138 else
3139 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3140
Florin Coras5e062572019-03-14 19:07:51 -07003141 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
3142 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
3143 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003144 }
3145 else
3146 rv = VPPCOM_EINVAL;
3147 break;
3148
3149 case VPPCOM_ATTR_GET_V6ONLY:
3150 if (buffer && buflen && (*buflen >= sizeof (int)))
3151 {
3152 /* VPP-TBD */
3153 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3154 VCL_SESS_ATTR_V6ONLY);
3155 *buflen = sizeof (int);
3156
Florin Coras5e062572019-03-14 19:07:51 -07003157 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3158 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003159 }
3160 else
3161 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003162 break;
3163
3164 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003165 if (buffer && buflen && (*buflen == sizeof (int)))
3166 {
3167 /* VPP-TBD */
3168 if (*(int *) buffer)
3169 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3170 else
3171 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3172
Florin Coras5e062572019-03-14 19:07:51 -07003173 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
3174 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
3175 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003176 }
3177 else
3178 rv = VPPCOM_EINVAL;
3179 break;
3180
3181 case VPPCOM_ATTR_GET_KEEPALIVE:
3182 if (buffer && buflen && (*buflen >= sizeof (int)))
3183 {
3184 /* VPP-TBD */
3185 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3186 VCL_SESS_ATTR_KEEPALIVE);
3187 *buflen = sizeof (int);
3188
Florin Coras5e062572019-03-14 19:07:51 -07003189 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3190 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003191 }
3192 else
3193 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003194 break;
Stevenbccd3392017-10-12 20:42:21 -07003195
3196 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003197 if (buffer && buflen && (*buflen == sizeof (int)))
3198 {
3199 /* VPP-TBD */
3200 if (*(int *) buffer)
3201 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3202 else
3203 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3204
Florin Coras5e062572019-03-14 19:07:51 -07003205 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
3206 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
3207 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003208 }
3209 else
3210 rv = VPPCOM_EINVAL;
3211 break;
3212
3213 case VPPCOM_ATTR_GET_TCP_NODELAY:
3214 if (buffer && buflen && (*buflen >= sizeof (int)))
3215 {
3216 /* VPP-TBD */
3217 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3218 VCL_SESS_ATTR_TCP_NODELAY);
3219 *buflen = sizeof (int);
3220
Florin Coras5e062572019-03-14 19:07:51 -07003221 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3222 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003223 }
3224 else
3225 rv = VPPCOM_EINVAL;
3226 break;
3227
3228 case VPPCOM_ATTR_SET_TCP_NODELAY:
3229 if (buffer && buflen && (*buflen == sizeof (int)))
3230 {
3231 /* VPP-TBD */
3232 if (*(int *) buffer)
3233 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3234 else
3235 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3236
Florin Coras5e062572019-03-14 19:07:51 -07003237 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3238 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3239 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003240 }
3241 else
3242 rv = VPPCOM_EINVAL;
3243 break;
3244
3245 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3246 if (buffer && buflen && (*buflen >= sizeof (int)))
3247 {
3248 /* VPP-TBD */
3249 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3250 VCL_SESS_ATTR_TCP_KEEPIDLE);
3251 *buflen = sizeof (int);
3252
Florin Coras5e062572019-03-14 19:07:51 -07003253 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3254 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003255 }
3256 else
3257 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003258 break;
3259
3260 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003261 if (buffer && buflen && (*buflen == sizeof (int)))
3262 {
3263 /* VPP-TBD */
3264 if (*(int *) buffer)
3265 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3266 else
3267 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3268
Florin Coras5e062572019-03-14 19:07:51 -07003269 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003270 VCL_SESS_ATTR_TEST (session->attr,
3271 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003272 }
3273 else
3274 rv = VPPCOM_EINVAL;
3275 break;
3276
3277 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3278 if (buffer && buflen && (*buflen >= sizeof (int)))
3279 {
3280 /* VPP-TBD */
3281 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3282 VCL_SESS_ATTR_TCP_KEEPINTVL);
3283 *buflen = sizeof (int);
3284
Florin Coras5e062572019-03-14 19:07:51 -07003285 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3286 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003287 }
3288 else
3289 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003290 break;
3291
3292 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003293 if (buffer && buflen && (*buflen == sizeof (int)))
3294 {
3295 /* VPP-TBD */
3296 if (*(int *) buffer)
3297 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3298 else
3299 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3300
Florin Coras5e062572019-03-14 19:07:51 -07003301 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003302 VCL_SESS_ATTR_TEST (session->attr,
3303 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003304 }
3305 else
3306 rv = VPPCOM_EINVAL;
3307 break;
3308
3309 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3310 if (buffer && buflen && (*buflen >= sizeof (u32)))
3311 {
3312 /* VPP-TBD */
3313 *(u32 *) buffer = session->user_mss;
3314 *buflen = sizeof (int);
3315
Florin Coras5e062572019-03-14 19:07:51 -07003316 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3317 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003318 }
3319 else
3320 rv = VPPCOM_EINVAL;
3321 break;
3322
3323 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3324 if (buffer && buflen && (*buflen == sizeof (u32)))
3325 {
3326 /* VPP-TBD */
3327 session->user_mss = *(u32 *) buffer;
3328
Florin Coras5e062572019-03-14 19:07:51 -07003329 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3330 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003331 }
3332 else
3333 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003334 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003335
Florin Coras7baeb712019-01-04 17:05:43 -08003336 case VPPCOM_ATTR_SET_SHUT:
3337 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3338 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3339 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3340 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3341 break;
3342
3343 case VPPCOM_ATTR_GET_SHUT:
3344 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3345 tmp_flags = 1;
3346 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3347 tmp_flags |= 2;
3348 if (tmp_flags == 1)
3349 *(int *) buffer = SHUT_RD;
3350 else if (tmp_flags == 2)
3351 *(int *) buffer = SHUT_WR;
3352 else if (tmp_flags == 3)
3353 *(int *) buffer = SHUT_RDWR;
3354 *buflen = sizeof (int);
3355 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003356 default:
3357 rv = VPPCOM_EINVAL;
3358 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003359 }
3360
Dave Wallace35830af2017-10-09 01:43:42 -04003361 return rv;
3362}
3363
Stevenac1f96d2017-10-24 16:03:58 -07003364int
Florin Coras134a9962018-08-28 11:32:04 -07003365vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003366 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3367{
Florin Coras134a9962018-08-28 11:32:04 -07003368 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003369 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003370 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003371
3372 if (ep)
3373 {
Florin Coras134a9962018-08-28 11:32:04 -07003374 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003375 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003376 {
Florin Coras5e062572019-03-14 19:07:51 -07003377 VDBG (0, "sh 0x%llx is closed!", session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003378 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003379 }
Florin Coras7e12d942018-06-27 14:32:43 -07003380 ep->is_ip4 = session->transport.is_ip4;
3381 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003382 }
Steven58f464e2017-10-25 12:33:12 -07003383
3384 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003385 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003386 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003387 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003388 else
3389 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003390 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003391 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003392 }
3393
Florin Coras99368312018-08-02 10:45:44 -07003394 if (ep)
3395 {
3396 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003397 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3398 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003399 else
Dave Barach178cf492018-11-13 16:34:13 -05003400 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3401 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003402 }
Florin Coras460dce62018-07-27 05:45:06 -07003403
Stevenac1f96d2017-10-24 16:03:58 -07003404 return rv;
3405}
3406
3407int
Florin Coras134a9962018-08-28 11:32:04 -07003408vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003409 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3410{
Dave Wallace617dffa2017-10-26 14:47:06 -04003411 if (!buffer)
3412 return VPPCOM_EINVAL;
3413
3414 if (ep)
3415 {
3416 // TBD
3417 return VPPCOM_EINVAL;
3418 }
3419
3420 if (flags)
3421 {
3422 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003423 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003424 }
3425
Florin Coras42ceddb2018-12-12 10:56:01 -08003426 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003427}
3428
Dave Wallace048b1d62018-01-03 22:24:41 -05003429int
3430vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3431{
Florin Coras134a9962018-08-28 11:32:04 -07003432 vcl_worker_t *wrk = vcl_worker_get_current ();
3433 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003434 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003435 svm_msg_q_msg_t msg;
3436 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003437 int rv, num_ev = 0;
3438
Florin Coras5e062572019-03-14 19:07:51 -07003439 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003440
3441 if (!vp)
3442 return VPPCOM_EFAULT;
3443
3444 do
3445 {
Florin Coras7e12d942018-06-27 14:32:43 -07003446 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003447
Florin Coras6917b942018-11-13 22:44:54 -08003448 /* Dequeue all events and drop all unhandled io events */
3449 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3450 {
3451 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3452 vcl_handle_mq_event (wrk, e);
3453 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3454 }
3455 vec_reset_length (wrk->unhandled_evts_vector);
3456
Dave Wallace048b1d62018-01-03 22:24:41 -05003457 for (i = 0; i < n_sids; i++)
3458 {
Florin Coras7baeb712019-01-04 17:05:43 -08003459 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003460 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003461 {
3462 vp[i].revents = POLLHUP;
3463 num_ev++;
3464 continue;
3465 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003466
Florin Coras6917b942018-11-13 22:44:54 -08003467 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003468
3469 if (POLLIN & vp[i].events)
3470 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003471 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003472 if (rv > 0)
3473 {
Florin Coras6917b942018-11-13 22:44:54 -08003474 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003475 num_ev++;
3476 }
3477 else if (rv < 0)
3478 {
3479 switch (rv)
3480 {
3481 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003482 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003483 break;
3484
3485 default:
Florin Coras6917b942018-11-13 22:44:54 -08003486 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003487 break;
3488 }
3489 num_ev++;
3490 }
3491 }
3492
3493 if (POLLOUT & vp[i].events)
3494 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003495 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003496 if (rv > 0)
3497 {
Florin Coras6917b942018-11-13 22:44:54 -08003498 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003499 num_ev++;
3500 }
3501 else if (rv < 0)
3502 {
3503 switch (rv)
3504 {
3505 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003506 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003507 break;
3508
3509 default:
Florin Coras6917b942018-11-13 22:44:54 -08003510 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003511 break;
3512 }
3513 num_ev++;
3514 }
3515 }
3516
Dave Wallace7e607a72018-06-18 18:41:32 -04003517 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003518 {
Florin Coras6917b942018-11-13 22:44:54 -08003519 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003520 num_ev++;
3521 }
3522 }
3523 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003524 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003525 }
3526 while ((num_ev == 0) && keep_trying);
3527
Dave Wallace048b1d62018-01-03 22:24:41 -05003528 return num_ev;
3529}
3530
Florin Coras99368312018-08-02 10:45:44 -07003531int
3532vppcom_mq_epoll_fd (void)
3533{
Florin Coras134a9962018-08-28 11:32:04 -07003534 vcl_worker_t *wrk = vcl_worker_get_current ();
3535 return wrk->mqs_epfd;
3536}
3537
3538int
Florin Coras30e79c22019-01-02 19:31:22 -08003539vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003540{
3541 return session_handle & 0xFFFFFF;
3542}
3543
3544int
Florin Coras30e79c22019-01-02 19:31:22 -08003545vppcom_session_worker (vcl_session_handle_t session_handle)
3546{
3547 return session_handle >> 24;
3548}
3549
3550int
Florin Coras134a9962018-08-28 11:32:04 -07003551vppcom_worker_register (void)
3552{
Florin Coras47c40e22018-11-26 17:01:36 -08003553 if (!vcl_worker_alloc_and_init ())
3554 return VPPCOM_EEXIST;
3555
3556 if (vcl_worker_set_bapi ())
3557 return VPPCOM_EEXIST;
3558
3559 if (vcl_worker_register_with_vpp ())
3560 return VPPCOM_EEXIST;
3561
3562 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003563}
3564
Florin Coras369db832019-07-08 12:34:45 -07003565void
3566vppcom_worker_unregister (void)
3567{
3568 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
3569 vcl_set_worker_index (~0);
3570}
3571
Florin Corasdfe4cf42018-11-28 22:13:45 -08003572int
3573vppcom_worker_index (void)
3574{
3575 return vcl_get_worker_index ();
3576}
3577
Florin Corase0982e52019-01-25 13:19:56 -08003578int
3579vppcom_worker_mqs_epfd (void)
3580{
3581 vcl_worker_t *wrk = vcl_worker_get_current ();
3582 if (!vcm->cfg.use_mq_eventfd)
3583 return -1;
3584 return wrk->mqs_epfd;
3585}
3586
Nathan Skrzypczak9fd99622019-05-16 14:38:44 +02003587int
3588vppcom_session_is_connectable_listener (uint32_t session_handle)
3589{
3590 vcl_session_t *session;
3591 vcl_worker_t *wrk = vcl_worker_get_current ();
3592 session = vcl_session_get_w_handle (wrk, session_handle);
3593 if (!session)
3594 return VPPCOM_EBADFD;
3595 return vcl_session_is_connectable_listener (wrk, session);
3596}
3597
3598int
3599vppcom_session_listener (uint32_t session_handle)
3600{
3601 vcl_worker_t *wrk = vcl_worker_get_current ();
3602 vcl_session_t *listen_session, *session;
3603 session = vcl_session_get_w_handle (wrk, session_handle);
3604 if (!session)
3605 return VPPCOM_EBADFD;
3606 if (session->listener_index == VCL_INVALID_SESSION_INDEX)
3607 return VPPCOM_EBADFD;
3608 listen_session = vcl_session_get_w_handle (wrk, session->listener_index);
3609 if (!listen_session)
3610 return VPPCOM_EBADFD;
3611 return vcl_session_handle (listen_session);
3612}
3613
3614int
3615vppcom_session_n_accepted (uint32_t session_handle)
3616{
3617 vcl_worker_t *wrk = vcl_worker_get_current ();
3618 vcl_session_t *session = vcl_session_get_w_handle (wrk, session_handle);
3619 if (!session)
3620 return VPPCOM_EBADFD;
3621 return session->n_accepted_sessions;
3622}
3623
Dave Wallacee22aa742017-10-20 12:30:38 -04003624/*
3625 * fd.io coding-style-patch-verification: ON
3626 *
3627 * Local Variables:
3628 * eval: (c-set-style "gnu")
3629 * End:
3630 */