blob: 29936aa5207c035ae00f96c465e7de6f7002e0b6 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Dave Wallace33e002b2017-09-06 01:20:02 -04002 * Copyright (c) 2017 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace543852a2017-08-03 02:11:34 -040018#include <svm/svm_fifo_segment.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040019#include <vcl/vppcom.h>
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -080020#include <vcl/vcl_event.h>
Florin Coras0d427d82018-06-27 03:24:07 -070021#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070022#include <vcl/vcl_private.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040023
Florin Coras54693d22018-07-17 10:46:29 -070024static u8 not_ready;
25
26void
27sigsegv_signal (int signum)
28{
29 not_ready = 1;
30}
31
32static void
33vcl_wait_for_memory (void *mem)
34{
35 u8 __clib_unused test;
Florin Coras460dce62018-07-27 05:45:06 -070036 if (vcm->mounting_segment)
37 {
38 while (vcm->mounting_segment)
39 ;
40 return;
41 }
Florin Coras54693d22018-07-17 10:46:29 -070042 if (1 || vcm->debug)
43 {
Florin Coras460dce62018-07-27 05:45:06 -070044 usleep (1e5);
Florin Coras54693d22018-07-17 10:46:29 -070045 return;
46 }
47 if (signal (SIGSEGV, sigsegv_signal))
48 {
49 perror ("signal()");
50 return;
51 }
52 not_ready = 0;
53
54again:
55 test = *(u8 *) mem;
56 if (not_ready)
57 {
58 not_ready = 0;
59 usleep (1);
60 goto again;
61 }
62
63 signal (SIGSEGV, SIG_DFL);
64}
65
Dave Wallace543852a2017-08-03 02:11:34 -040066static const char *
67vppcom_app_state_str (app_state_t state)
68{
69 char *st;
70
71 switch (state)
72 {
73 case STATE_APP_START:
74 st = "STATE_APP_START";
75 break;
76
77 case STATE_APP_CONN_VPP:
78 st = "STATE_APP_CONN_VPP";
79 break;
80
81 case STATE_APP_ENABLED:
82 st = "STATE_APP_ENABLED";
83 break;
84
85 case STATE_APP_ATTACHED:
86 st = "STATE_APP_ATTACHED";
87 break;
88
89 default:
90 st = "UNKNOWN_APP_STATE";
91 break;
92 }
93
94 return st;
95}
96
Florin Coras697faea2018-06-27 17:10:49 -070097const char *
Dave Wallace543852a2017-08-03 02:11:34 -040098vppcom_session_state_str (session_state_t state)
99{
100 char *st;
101
102 switch (state)
103 {
104 case STATE_START:
105 st = "STATE_START";
106 break;
107
108 case STATE_CONNECT:
109 st = "STATE_CONNECT";
110 break;
111
112 case STATE_LISTEN:
113 st = "STATE_LISTEN";
114 break;
115
116 case STATE_ACCEPT:
117 st = "STATE_ACCEPT";
118 break;
119
Dave Wallace4878cbe2017-11-21 03:45:09 -0500120 case STATE_CLOSE_ON_EMPTY:
121 st = "STATE_CLOSE_ON_EMPTY";
122 break;
123
Dave Wallace543852a2017-08-03 02:11:34 -0400124 case STATE_DISCONNECT:
125 st = "STATE_DISCONNECT";
126 break;
127
128 case STATE_FAILED:
129 st = "STATE_FAILED";
130 break;
131
132 default:
133 st = "UNKNOWN_STATE";
134 break;
135 }
136
137 return st;
138}
139
Dave Wallace543852a2017-08-03 02:11:34 -0400140u8 *
141format_ip4_address (u8 * s, va_list * args)
142{
143 u8 *a = va_arg (*args, u8 *);
144 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
145}
146
147u8 *
148format_ip6_address (u8 * s, va_list * args)
149{
150 ip6_address_t *a = va_arg (*args, ip6_address_t *);
151 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
152
153 i_max_n_zero = ARRAY_LEN (a->as_u16);
154 max_n_zeros = 0;
155 i_first_zero = i_max_n_zero;
156 n_zeros = 0;
157 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
158 {
159 u32 is_zero = a->as_u16[i] == 0;
160 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
161 {
162 i_first_zero = i;
163 n_zeros = 0;
164 }
165 n_zeros += is_zero;
166 if ((!is_zero && n_zeros > max_n_zeros)
167 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
168 {
169 i_max_n_zero = i_first_zero;
170 max_n_zeros = n_zeros;
171 i_first_zero = ARRAY_LEN (a->as_u16);
172 n_zeros = 0;
173 }
174 }
175
176 last_double_colon = 0;
177 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
178 {
179 if (i == i_max_n_zero && max_n_zeros > 1)
180 {
181 s = format (s, "::");
182 i += max_n_zeros - 1;
183 last_double_colon = 1;
184 }
185 else
186 {
187 s = format (s, "%s%x",
188 (last_double_colon || i == 0) ? "" : ":",
189 clib_net_to_host_u16 (a->as_u16[i]));
190 last_double_colon = 0;
191 }
192 }
193
194 return s;
195}
196
197/* Format an IP46 address. */
198u8 *
199format_ip46_address (u8 * s, va_list * args)
200{
201 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
202 ip46_type_t type = va_arg (*args, ip46_type_t);
203 int is_ip4 = 1;
204
205 switch (type)
206 {
207 case IP46_TYPE_ANY:
208 is_ip4 = ip46_address_is_ip4 (ip46);
209 break;
210 case IP46_TYPE_IP4:
211 is_ip4 = 1;
212 break;
213 case IP46_TYPE_IP6:
214 is_ip4 = 0;
215 break;
216 }
217
218 return is_ip4 ?
219 format (s, "%U", format_ip4_address, &ip46->ip4) :
220 format (s, "%U", format_ip6_address, &ip46->ip6);
221}
222
Florin Coras697faea2018-06-27 17:10:49 -0700223/*
224 * VPPCOM Utility Functions
225 */
226
227static inline void
228vppcom_session_table_del_listener (u64 listener_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400229{
Florin Coras697faea2018-06-27 17:10:49 -0700230 listener_handle |= 1ULL << 63;
231 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
232}
233
234static inline int
235vppcom_wait_for_app_state_change (app_state_t app_state)
236{
237 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
238
239 while (clib_time_now (&vcm->clib_time) < timeout)
240 {
241 if (vcm->app_state == app_state)
242 return VPPCOM_OK;
243 }
244 VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (),
245 vppcom_app_state_str (app_state), app_state);
246 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state);
247
248 return VPPCOM_ETIMEDOUT;
249}
250
Florin Coras54693d22018-07-17 10:46:29 -0700251static void
252vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
253 session_handle_t handle, int retval)
254{
255 app_session_evt_t _app_evt, *app_evt = &_app_evt;
256 session_accepted_reply_msg_t *rmp;
257 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
258 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
259 rmp->handle = handle;
260 rmp->context = context;
261 rmp->retval = retval;
262 app_send_ctrl_evt_to_vpp (mq, app_evt);
263}
264
Florin Coras99368312018-08-02 10:45:44 -0700265static void
266vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
267 session_handle_t handle, int retval)
268{
269 app_session_evt_t _app_evt, *app_evt = &_app_evt;
270 session_disconnected_reply_msg_t *rmp;
271 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
272 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
273 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
274 rmp->handle = handle;
275 rmp->context = context;
276 rmp->retval = retval;
277 app_send_ctrl_evt_to_vpp (mq, app_evt);
278}
279
Florin Coras54693d22018-07-17 10:46:29 -0700280static u32
281vcl_session_accepted_handler (session_accepted_msg_t * mp)
282{
283 vcl_session_t *session, *listen_session;
284 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700285 u32 session_index, vpp_wrk_index;
286 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700287
288 VCL_SESSION_LOCK ();
289
Florin Coras99368312018-08-02 10:45:44 -0700290 session = vcl_session_alloc ();
291 session_index = vcl_session_index (session);
292
Florin Coras54693d22018-07-17 10:46:29 -0700293 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
294 if (!listen_session)
295 {
296 svm_msg_q_t *evt_q;
297 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
298 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
299 "unknown vpp listener handle %llx",
300 getpid (), mp->listener_handle);
301 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
302 VNET_API_ERROR_INVALID_ARGUMENT);
Florin Coras99368312018-08-02 10:45:44 -0700303 vcl_session_free (session);
304 VCL_SESSION_UNLOCK ();
Florin Coras54693d22018-07-17 10:46:29 -0700305 return VCL_INVALID_SESSION_INDEX;
306 }
307
Florin Coras54693d22018-07-17 10:46:29 -0700308 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
309 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
310
311 if (mp->server_event_queue_address)
312 {
313 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
314 svm_msg_q_t *);
315 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
316 svm_msg_q_t *);
317 vcl_wait_for_memory (session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700318 rx_fifo->master_session_index = session_index;
319 tx_fifo->master_session_index = session_index;
Florin Coras99368312018-08-02 10:45:44 -0700320 vec_validate (vcm->vpp_event_queues, 0);
321 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
322 vcm->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700323 }
324 else
325 {
326 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
327 svm_msg_q_t *);
328 rx_fifo->client_session_index = session_index;
329 tx_fifo->client_session_index = session_index;
Florin Coras99368312018-08-02 10:45:44 -0700330
331 vpp_wrk_index = tx_fifo->master_thread_index;
332 vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
333 vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700334 }
335
336 session->vpp_handle = mp->handle;
337 session->client_context = mp->context;
338 session->rx_fifo = rx_fifo;
339 session->tx_fifo = tx_fifo;
340
341 session->session_state = STATE_ACCEPT;
342 session->transport.rmt_port = mp->port;
343 session->transport.is_ip4 = mp->is_ip4;
344 clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
345
346 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
347 session->transport.lcl_port = listen_session->transport.lcl_port;
348 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700349 session->session_type = listen_session->session_type;
350 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
Florin Coras54693d22018-07-17 10:46:29 -0700351
352 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
353 " address %U port %d queue %p!", getpid (), mp->handle, session_index,
354 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
355 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
356 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
357 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
358
359 VCL_SESSION_UNLOCK ();
360 return session_index;
361}
362
363static u32
364vcl_session_connected_handler (session_connected_msg_t * mp)
365{
Florin Coras99368312018-08-02 10:45:44 -0700366 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700367 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700368 vcl_session_t *session = 0;
369 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700370 int rv = VPPCOM_OK;
371
372 session_index = mp->context;
373 VCL_SESSION_LOCK_AND_GET (session_index, &session);
374done:
375 if (mp->retval)
376 {
377 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
378 "connect failed! %U",
379 getpid (), mp->handle, session_index,
380 format_api_error, ntohl (mp->retval));
381 if (session)
382 {
383 session->session_state = STATE_FAILED;
384 session->vpp_handle = mp->handle;
385 }
386 else
387 {
388 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
389 "Invalid session index (%u)!",
390 getpid (), mp->handle, session_index);
391 }
392 goto done_unlock;
393 }
394
395 if (rv)
396 goto done_unlock;
397
Florin Coras460dce62018-07-27 05:45:06 -0700398 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
399 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
400 vcl_wait_for_memory (rx_fifo);
401 rx_fifo->client_session_index = session_index;
402 tx_fifo->client_session_index = session_index;
403
Florin Coras54693d22018-07-17 10:46:29 -0700404 if (mp->client_event_queue_address)
405 {
406 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
407 svm_msg_q_t *);
408 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
409 svm_msg_q_t *);
Florin Coras99368312018-08-02 10:45:44 -0700410
411 vec_validate (vcm->vpp_event_queues, 0);
412 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
413 vcm->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700414 }
415 else
Florin Coras99368312018-08-02 10:45:44 -0700416 {
417 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
418 svm_msg_q_t *);
419 vpp_wrk_index = tx_fifo->master_thread_index;
420 vec_validate (vcm->vpp_event_queues, vpp_wrk_index);
421 vcm->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
422 }
Florin Coras54693d22018-07-17 10:46:29 -0700423
Florin Coras54693d22018-07-17 10:46:29 -0700424 session->rx_fifo = rx_fifo;
425 session->tx_fifo = tx_fifo;
426 session->vpp_handle = mp->handle;
427 session->transport.is_ip4 = mp->is_ip4;
428 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
429 sizeof (session->transport.lcl_ip));
430 session->transport.lcl_port = mp->lcl_port;
431 session->session_state = STATE_CONNECT;
432
433 /* Add it to lookup table */
434 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
435
436 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
437 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
438 getpid (), mp->handle, session_index, session->rx_fifo,
439 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
440done_unlock:
441 VCL_SESSION_UNLOCK ();
442 return session_index;
443}
444
445int
446vcl_handle_mq_ctrl_event (session_event_t * e)
447{
448 session_accepted_msg_t *accepted_msg;
449 session_disconnected_msg_t *disconnected_msg;
450 vcl_session_msg_t *vcl_msg;
451 vcl_session_t *session;
452 u64 handle;
453 u32 sid;
454
455 switch (e->event_type)
456 {
457 case FIFO_EVENT_APP_RX:
458 clib_warning ("unhandled rx: sid %u (0x%x)",
459 e->fifo->client_session_index,
460 e->fifo->client_session_index);
461 break;
462 case SESSION_CTRL_EVT_ACCEPTED:
463 accepted_msg = (session_accepted_msg_t *) e->data;
464 handle = accepted_msg->listener_handle;
465 session = vppcom_session_table_lookup_listener (handle);
466 if (!session)
467 {
468 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
469 "listener handle %llx", getpid (), handle);
470 break;
471 }
472
473 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
474 vcl_msg->accepted_msg = *accepted_msg;
475 break;
476 case SESSION_CTRL_EVT_CONNECTED:
477 vcl_session_connected_handler ((session_connected_msg_t *) e->data);
478 break;
479 case SESSION_CTRL_EVT_DISCONNECTED:
480 disconnected_msg = (session_disconnected_msg_t *) e->data;
481 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
482 session = vcl_session_get (sid);
483 session->session_state = STATE_DISCONNECT;
484 VDBG (0, "disconnected %u", sid);
485 break;
486 default:
487 clib_warning ("unhandled %u", e->event_type);
488 }
489 return VPPCOM_OK;
490}
491
Florin Coras697faea2018-06-27 17:10:49 -0700492static inline int
493vppcom_wait_for_session_state_change (u32 session_index,
494 session_state_t state,
495 f64 wait_for_time)
496{
497 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
498 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700499 svm_msg_q_msg_t msg;
500 session_event_t *e;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800501 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400502
Florin Coras697faea2018-06-27 17:10:49 -0700503 do
Dave Wallace543852a2017-08-03 02:11:34 -0400504 {
Florin Coras697faea2018-06-27 17:10:49 -0700505 VCL_SESSION_LOCK ();
506 rv = vppcom_session_at_index (session_index, &session);
507 if (PREDICT_FALSE (rv))
508 {
509 VCL_SESSION_UNLOCK ();
510 return rv;
511 }
512 if (session->session_state & state)
513 {
514 VCL_SESSION_UNLOCK ();
515 return VPPCOM_OK;
516 }
517 if (session->session_state & STATE_FAILED)
518 {
519 VCL_SESSION_UNLOCK ();
520 return VPPCOM_ECONNREFUSED;
521 }
Dave Wallace7e607a72018-06-18 18:41:32 -0400522 VCL_SESSION_UNLOCK ();
Florin Coras54693d22018-07-17 10:46:29 -0700523
524 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
525 continue;
526 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
527 vcl_handle_mq_ctrl_event (e);
528 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800529 }
Florin Coras697faea2018-06-27 17:10:49 -0700530 while (clib_time_now (&vcm->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800531
Florin Coras697faea2018-06-27 17:10:49 -0700532 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
533 vppcom_session_state_str (state));
534 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400535
Florin Coras697faea2018-06-27 17:10:49 -0700536 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500537}
538
Florin Coras697faea2018-06-27 17:10:49 -0700539static int
540vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400541{
Florin Coras697faea2018-06-27 17:10:49 -0700542 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400543
Florin Coras697faea2018-06-27 17:10:49 -0700544 if (vcm->app_state != STATE_APP_ENABLED)
545 {
546 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
547 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
548 if (PREDICT_FALSE (rv))
549 {
550 VDBG (0, "VCL<%d>: application session enable timed out! "
551 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
552 return rv;
553 }
554 }
555 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400556}
557
Florin Coras697faea2018-06-27 17:10:49 -0700558static int
559vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400560{
Florin Coras697faea2018-06-27 17:10:49 -0700561 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400562
Florin Coras697faea2018-06-27 17:10:49 -0700563 vppcom_app_send_attach ();
564 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
565 if (PREDICT_FALSE (rv))
566 {
567 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
568 getpid (), rv, vppcom_retval_str (rv));
569 return rv;
570 }
Dave Wallace543852a2017-08-03 02:11:34 -0400571
Florin Coras697faea2018-06-27 17:10:49 -0700572 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400573}
574
575static int
Dave Wallace543852a2017-08-03 02:11:34 -0400576vppcom_session_unbind (u32 session_index)
577{
Florin Coras7e12d942018-06-27 14:32:43 -0700578 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400579 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500580 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400581
Dave Wallace7e607a72018-06-18 18:41:32 -0400582 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500583
584 vpp_handle = session->vpp_handle;
585 vppcom_session_table_del_listener (vpp_handle);
586 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700587 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500588
Dave Wallace7e607a72018-06-18 18:41:32 -0400589 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400590
Florin Coras0d427d82018-06-27 03:24:07 -0700591 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
592 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
593 vppcom_session_state_str (STATE_DISCONNECT));
594 vcl_evt (VCL_EVT_UNBIND, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500595 vppcom_send_unbind_sock (vpp_handle);
596
597done:
598 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400599}
600
Florin Coras99368312018-08-02 10:45:44 -0700601static svm_msg_q_t *
602vcl_session_vpp_evt_q (vcl_session_t * s)
603{
604 if (vcl_session_is_ct (s))
605 return vcm->vpp_event_queues[0];
606 else
607 return vcm->vpp_event_queues[s->tx_fifo->master_thread_index];
608}
609
Florin Coras697faea2018-06-27 17:10:49 -0700610static int
Dave Wallace543852a2017-08-03 02:11:34 -0400611vppcom_session_disconnect (u32 session_index)
612{
Florin Coras99368312018-08-02 10:45:44 -0700613 svm_msg_q_t *vpp_evt_q;
Florin Coras7e12d942018-06-27 14:32:43 -0700614 vcl_session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500615 session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700616 u64 vpp_handle;
617 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400618
Dave Wallace7e607a72018-06-18 18:41:32 -0400619 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500620
621 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700622 state = session->session_state;
Dave Wallace7e607a72018-06-18 18:41:32 -0400623 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500624
Florin Coras0d427d82018-06-27 03:24:07 -0700625 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
626 vpp_handle, session_index, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400627
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800628 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400629 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500630 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500631 "Cannot disconnect a listen socket!",
632 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500633 rv = VPPCOM_EBADFD;
634 goto done;
635 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400636
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800637 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500638 {
Florin Coras99368312018-08-02 10:45:44 -0700639 vpp_evt_q = vcl_session_vpp_evt_q (session);
640 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
641 vpp_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700642 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
643 "REPLY...", getpid (), vpp_handle, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400644 }
645 else
Dave Wallace227867f2017-11-13 21:21:53 -0500646 {
Florin Coras0d427d82018-06-27 03:24:07 -0700647 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
648 getpid (), vpp_handle, session_index);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800649 vppcom_send_disconnect_session (vpp_handle, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -0500650 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400651
Dave Wallace4878cbe2017-11-21 03:45:09 -0500652done:
653 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400654}
655
Dave Wallace543852a2017-08-03 02:11:34 -0400656/*
657 * VPPCOM Public API functions
658 */
659int
660vppcom_app_create (char *app_name)
661{
Dave Wallace543852a2017-08-03 02:11:34 -0400662 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400663 int rv;
664
665 if (!vcm->init)
666 {
Dave Wallace543852a2017-08-03 02:11:34 -0400667 vcm->init = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -0400668 vppcom_cfg (&vcm->cfg);
Florin Coras99368312018-08-02 10:45:44 -0700669 vcl_cfg = &vcm->cfg;
670
671 vcm->mqs_epfd = -1;
672 if (vcl_cfg->use_mq_eventfd)
673 vcm->mqs_epfd = epoll_create (1);
Dave Barach6a5adc32018-07-04 10:56:23 -0400674
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800675 clib_spinlock_init (&vcm->session_fifo_lockp);
Dave Wallace2e005bb2017-11-07 01:21:39 -0500676 clib_fifo_validate (vcm->client_session_index_fifo,
677 vcm->cfg.listen_queue_size);
Florin Coras697faea2018-06-27 17:10:49 -0700678 clib_spinlock_init (&vcm->sessions_lockp);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500679
Dave Wallace8af20542017-10-26 03:29:30 -0400680
Dave Wallace543852a2017-08-03 02:11:34 -0400681 vcm->main_cpu = os_get_thread_index ();
Dave Wallace543852a2017-08-03 02:11:34 -0400682
683 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
Florin Coras99368312018-08-02 10:45:44 -0700684 vcm->ct_registration_by_mq = hash_create (0, sizeof (uword));
685 clib_spinlock_init (&vcm->ct_registration_lock);
Dave Wallace543852a2017-08-03 02:11:34 -0400686
687 clib_time_init (&vcm->clib_time);
688 vppcom_init_error_string_table ();
Florin Corasa332c462018-01-31 06:52:17 -0800689 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
690 20 /* timeout in secs */ );
Florin Coras99368312018-08-02 10:45:44 -0700691 vec_validate (vcm->mq_events, 64);
692 vec_validate (vcm->mq_msg_vector, 128);
693 vec_reset_length (vcm->mq_msg_vector);
Dave Wallace543852a2017-08-03 02:11:34 -0400694 }
695
696 if (vcm->my_client_index == ~0)
697 {
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800698 /* API hookup and connect to VPP */
Dave Wallace048b1d62018-01-03 22:24:41 -0500699 vppcom_api_hookup ();
Florin Coras0d427d82018-06-27 03:24:07 -0700700 vcl_elog_init (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -0400701 vcm->app_state = STATE_APP_START;
702 rv = vppcom_connect_to_vpp (app_name);
703 if (rv)
704 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500705 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500706 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400707 return rv;
708 }
709
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800710 /* State event handling thread */
711
712 rv = vce_start_event_thread (&(vcm->event_thread), 20);
713
Florin Coras0d427d82018-06-27 03:24:07 -0700714 VDBG (0, "VCL<%d>: sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400715
Dave Wallace048b1d62018-01-03 22:24:41 -0500716 rv = vppcom_app_session_enable ();
Dave Wallace543852a2017-08-03 02:11:34 -0400717 if (rv)
718 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500719 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
720 "failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400721 return rv;
722 }
Dave Wallace543852a2017-08-03 02:11:34 -0400723
Florin Coras0d427d82018-06-27 03:24:07 -0700724 VDBG (0, "VCL<%d>: sending app attach", getpid ());
Dave Wallace048b1d62018-01-03 22:24:41 -0500725
726 rv = vppcom_app_attach ();
727 if (rv)
728 {
729 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
730 getpid ());
731 return rv;
732 }
733
Florin Coras0d427d82018-06-27 03:24:07 -0700734 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
735 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400736 }
Dave Wallace543852a2017-08-03 02:11:34 -0400737
738 return VPPCOM_OK;
739}
740
741void
742vppcom_app_destroy (void)
743{
Dave Wallace543852a2017-08-03 02:11:34 -0400744 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400745 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400746
747 if (vcm->my_client_index == ~0)
748 return;
749
Florin Coras0d427d82018-06-27 03:24:07 -0700750 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
751 getpid (), vcm->my_client_index, vcm->my_client_index);
752 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800753
Florin Coras697faea2018-06-27 17:10:49 -0700754 vppcom_app_send_detach ();
Dave Wallacede910062018-03-20 09:22:13 -0400755 orig_app_timeout = vcm->cfg.app_timeout;
756 vcm->cfg.app_timeout = 2.0;
Dave Wallace543852a2017-08-03 02:11:34 -0400757 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
Dave Wallacede910062018-03-20 09:22:13 -0400758 vcm->cfg.app_timeout = orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400759 if (PREDICT_FALSE (rv))
Florin Coras0d427d82018-06-27 03:24:07 -0700760 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
761 getpid (), rv, vppcom_retval_str (rv));
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800762
Florin Coras0d427d82018-06-27 03:24:07 -0700763 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -0400764 vl_client_disconnect_from_vlib ();
765 vcm->my_client_index = ~0;
766 vcm->app_state = STATE_APP_START;
767}
768
769int
Dave Wallacec04cbf12018-02-07 18:14:02 -0500770vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -0400771{
Florin Coras7e12d942018-06-27 14:32:43 -0700772 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -0400773 u32 session_index;
774
Dave Wallace7e607a72018-06-18 18:41:32 -0400775 VCL_SESSION_LOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400776 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400777 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -0400778 session_index = session - vcm->sessions;
779
Florin Coras7e12d942018-06-27 14:32:43 -0700780 session->session_type = proto;
781 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500782 session->vpp_handle = ~0;
Florin Coras460dce62018-07-27 05:45:06 -0700783 session->is_dgram = proto == VPPCOM_PROTO_UDP;
Dave Wallace543852a2017-08-03 02:11:34 -0400784
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800785 if (is_nonblocking)
786 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -0400787
Florin Coras7e12d942018-06-27 14:32:43 -0700788 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
789 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800790
Dave Wallace7e607a72018-06-18 18:41:32 -0400791 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800792
Florin Coras0d427d82018-06-27 03:24:07 -0700793 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800794
Dave Wallace543852a2017-08-03 02:11:34 -0400795 return (int) session_index;
796}
797
798int
799vppcom_session_close (uint32_t session_index)
800{
Florin Coras7e12d942018-06-27 14:32:43 -0700801 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400802 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400803 u8 is_vep;
804 u8 is_vep_session;
805 u32 next_sid;
806 u32 vep_idx;
Dave Wallaceee45d412017-11-24 21:44:06 -0500807 u64 vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500808 uword *p;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400809 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -0400810
Dave Wallace7e607a72018-06-18 18:41:32 -0400811 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400812 is_vep = session->is_vep;
813 is_vep_session = session->is_vep_session;
814 next_sid = session->vep.next_sid;
815 vep_idx = session->vep.vep_idx;
Florin Coras7e12d942018-06-27 14:32:43 -0700816 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -0500817 vpp_handle = session->vpp_handle;
Dave Wallace7e607a72018-06-18 18:41:32 -0400818 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400819
820 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -0500821 {
822 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -0500823 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
824 "closing epoll session...",
Dave Wallaceee45d412017-11-24 21:44:06 -0500825 getpid (), session_index, session_index);
826 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500827 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
828 "closing session...",
Dave Wallaceee45d412017-11-24 21:44:06 -0500829 getpid (), vpp_handle, session_index);
830 }
Dave Wallace543852a2017-08-03 02:11:34 -0400831
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400832 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -0400833 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400834 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -0400835 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400836 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700837 if (PREDICT_FALSE (rv < 0))
838 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
839 "vep_idx %u failed! rv %d (%s)",
840 getpid (), vpp_handle, next_sid, vep_idx,
841 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400842
Dave Wallace7e607a72018-06-18 18:41:32 -0400843 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400844 next_sid = session->vep.next_sid;
Dave Wallace7e607a72018-06-18 18:41:32 -0400845 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -0400846 }
847 }
848 else
849 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400850 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400851 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400852 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700853 if (rv < 0)
854 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
855 "vep_idx %u failed! rv %d (%s)",
856 getpid (), vpp_handle, session_index,
857 vep_idx, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400858 }
859
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800860 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400861 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800862 rv = vppcom_session_unbind (session_index);
863 if (PREDICT_FALSE (rv < 0))
Florin Coras0d427d82018-06-27 03:24:07 -0700864 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
865 "failed! rv %d (%s)",
866 getpid (), vpp_handle, session_index,
867 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400868 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500869 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
Dave Wallacef7f809c2017-10-03 01:48:42 -0400870 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500871 rv = vppcom_session_disconnect (session_index);
872 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -0500873 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500874 "session disconnect failed! rv %d (%s)",
875 getpid (), vpp_handle, session_index,
876 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400877 }
Dave Wallace19481612017-09-15 18:47:44 -0400878 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500879
Dave Wallace7e607a72018-06-18 18:41:32 -0400880 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Florin Coras99368312018-08-02 10:45:44 -0700881 if (vcl_session_is_ct (session))
882 {
883 vcl_cut_through_registration_t *ctr;
884 uword mq_addr;
885
886 mq_addr = pointer_to_uword (session->our_evt_q);
887 ctr = vcl_ct_registration_lock_and_lookup (mq_addr);
888 ASSERT (ctr);
889 if (ctr->epoll_evt_conn_index != ~0)
890 vcl_mq_epoll_del_evfd (ctr->epoll_evt_conn_index);
891 VDBG (0, "Removing ct registration %u",
892 vcl_ct_registration_index (ctr));
893 vcl_ct_registration_del (ctr);
894 vcl_ct_registration_unlock ();
895 }
Florin Coras54693d22018-07-17 10:46:29 -0700896
Dave Wallaceee45d412017-11-24 21:44:06 -0500897 vpp_handle = session->vpp_handle;
898 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500899 {
Dave Wallaceee45d412017-11-24 21:44:06 -0500900 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500901 if (p)
Dave Wallaceee45d412017-11-24 21:44:06 -0500902 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500903 }
Dave Wallace543852a2017-08-03 02:11:34 -0400904 pool_put_index (vcm->sessions, session_index);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800905
Dave Wallace7e607a72018-06-18 18:41:32 -0400906 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500907
908 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -0500909 {
910 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -0500911 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -0500912 getpid (), session_index, session_index);
913 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500914 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -0500915 getpid (), vpp_handle, session_index);
916 }
Dave Wallacef7f809c2017-10-03 01:48:42 -0400917done:
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800918
Florin Coras0d427d82018-06-27 03:24:07 -0700919 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800920
Dave Wallace543852a2017-08-03 02:11:34 -0400921 return rv;
922}
923
924int
925vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
926{
Florin Coras7e12d942018-06-27 14:32:43 -0700927 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400928 int rv;
929
930 if (!ep || !ep->ip)
931 return VPPCOM_EINVAL;
932
Dave Wallace7e607a72018-06-18 18:41:32 -0400933 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -0400934
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400935 if (session->is_vep)
936 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400937 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500938 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
939 "bind to an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500940 rv = VPPCOM_EBADFD;
941 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400942 }
943
Florin Coras7e12d942018-06-27 14:32:43 -0700944 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -0700945 if (ep->is_ip4)
946 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
947 sizeof (ip4_address_t));
948 else
949 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
950 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -0700951 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -0700952
Florin Coras0d427d82018-06-27 03:24:07 -0700953 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
954 "proto %s", getpid (), session_index,
Florin Coras7e12d942018-06-27 14:32:43 -0700955 session->transport.is_ip4 ? "IPv4" : "IPv6",
956 format_ip46_address, &session->transport.lcl_ip,
957 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
958 clib_net_to_host_u16 (session->transport.lcl_port),
959 session->session_type ? "UDP" : "TCP");
Florin Coras0d427d82018-06-27 03:24:07 -0700960 vcl_evt (VCL_EVT_BIND, session);
Dave Wallace7e607a72018-06-18 18:41:32 -0400961 VCL_SESSION_UNLOCK ();
Florin Coras460dce62018-07-27 05:45:06 -0700962
963 if (session->session_type == VPPCOM_PROTO_UDP)
964 vppcom_session_listen (session_index, 10);
965
Dave Wallace4878cbe2017-11-21 03:45:09 -0500966done:
967 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400968}
969
970int
Dave Wallace33e002b2017-09-06 01:20:02 -0400971vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -0400972{
Florin Coras7e12d942018-06-27 14:32:43 -0700973 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -0500974 u64 listen_vpp_handle;
975 int rv, retval;
Dave Wallace543852a2017-08-03 02:11:34 -0400976
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -0800977 if (q_len == 0 || q_len == ~0)
978 q_len = vcm->cfg.listen_queue_size;
979
Dave Wallace7e607a72018-06-18 18:41:32 -0400980 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -0400981
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400982 if (listen_session->is_vep)
983 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400984 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500985 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500986 "epoll session!", getpid (), listen_session_index);
987 rv = VPPCOM_EBADFD;
988 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400989 }
990
Dave Wallaceee45d412017-11-24 21:44:06 -0500991 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700992 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -0400993 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400994 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -0700995 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
996 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500997 rv = VPPCOM_OK;
998 goto done;
Dave Wallacee695cb42017-11-02 22:04:42 -0400999 }
1000
Florin Coras0d427d82018-06-27 03:24:07 -07001001 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
1002 "request...", getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001003
Dave Wallace4878cbe2017-11-21 03:45:09 -05001004 vppcom_send_bind_sock (listen_session, listen_session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001005 VCL_SESSION_UNLOCK ();
Florin Coras54693d22018-07-17 10:46:29 -07001006 retval = vppcom_wait_for_session_state_change (listen_session_index,
1007 STATE_LISTEN,
1008 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001009
Dave Wallace7e607a72018-06-18 18:41:32 -04001010 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05001011 if (PREDICT_FALSE (retval))
1012 {
Florin Coras0d427d82018-06-27 03:24:07 -07001013 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1014 "returning %d (%s)", getpid (), listen_session->vpp_handle,
1015 listen_session_index, retval, vppcom_retval_str (retval));
Dave Wallace7e607a72018-06-18 18:41:32 -04001016 VCL_SESSION_UNLOCK ();
Dave Wallaceee45d412017-11-24 21:44:06 -05001017 rv = retval;
1018 goto done;
1019 }
1020
Dave Wallace7e607a72018-06-18 18:41:32 -04001021 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001022
1023done:
1024 return rv;
1025}
1026
1027int
Florin Coras7e12d942018-06-27 14:32:43 -07001028validate_args_session_accept_ (vcl_session_t * listen_session)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001029{
1030 u32 listen_session_index = listen_session - vcm->sessions;
1031
1032 /* Input validation - expects spinlock on sessions_lockp */
1033 if (listen_session->is_vep)
1034 {
1035 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
1036 "epoll session!", getpid (), listen_session_index);
1037 return VPPCOM_EBADFD;
1038 }
1039
Florin Coras7e12d942018-06-27 14:32:43 -07001040 if (listen_session->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001041 {
1042 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1043 "not in listen state! state 0x%x (%s)", getpid (),
1044 listen_session->vpp_handle, listen_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001045 listen_session->session_state,
1046 vppcom_session_state_str (listen_session->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001047 return VPPCOM_EBADFD;
1048 }
1049 return VPPCOM_OK;
1050}
1051
1052int
Dave Wallace543852a2017-08-03 02:11:34 -04001053vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001054 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001055{
Florin Coras54693d22018-07-17 10:46:29 -07001056 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001057 vcl_session_t *listen_session = 0;
1058 vcl_session_t *client_session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001059 u32 client_session_index = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07001060 svm_msg_q_t *vpp_evt_q;
1061 vcl_session_msg_t *evt;
Dave Wallaceee45d412017-11-24 21:44:06 -05001062 u64 listen_vpp_handle;
Florin Coras54693d22018-07-17 10:46:29 -07001063 svm_msg_q_msg_t msg;
1064 session_event_t *e;
1065 u8 is_nonblocking;
1066 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001067
Dave Wallace7e607a72018-06-18 18:41:32 -04001068 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001069
Florin Coras54693d22018-07-17 10:46:29 -07001070 if (validate_args_session_accept_ (listen_session))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001071 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001072 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05001073 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001074 }
1075
Dave Wallace7e607a72018-06-18 18:41:32 -04001076 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -04001077
Florin Coras54693d22018-07-17 10:46:29 -07001078 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001079 {
Florin Coras54693d22018-07-17 10:46:29 -07001080 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1081 accepted_msg = evt->accepted_msg;
1082 goto handle;
1083 }
1084
1085 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1086 VCL_SESS_ATTR_NONBLOCK);
1087 if (svm_msg_q_is_empty (vcm->app_event_queue) && is_nonblocking)
1088 return VPPCOM_EAGAIN;
1089
1090 while (1)
1091 {
1092 if (svm_msg_q_sub (vcm->app_event_queue, &msg, SVM_Q_WAIT, 0))
1093 return VPPCOM_EAGAIN;
1094
1095 e = svm_msg_q_msg_data (vcm->app_event_queue, &msg);
1096 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001097 {
Florin Coras54693d22018-07-17 10:46:29 -07001098 clib_warning ("discarded event: %u", e->event_type);
1099 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1100 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001101 }
Florin Coras54693d22018-07-17 10:46:29 -07001102 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
1103 svm_msg_q_free_msg (vcm->app_event_queue, &msg);
1104 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001105 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001106
Florin Coras54693d22018-07-17 10:46:29 -07001107handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001108
Florin Coras54693d22018-07-17 10:46:29 -07001109 client_session_index = vcl_session_accepted_handler (&accepted_msg);
Florin Coras99368312018-08-02 10:45:44 -07001110 listen_session = vcl_session_get (listen_session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001111 VCL_SESSION_LOCK_AND_GET (client_session_index, &client_session);
1112 rv = client_session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001113
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001114 if (flags & O_NONBLOCK)
1115 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001116
Florin Coras54693d22018-07-17 10:46:29 -07001117 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras0d427d82018-06-27 03:24:07 -07001118 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1119 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
1120 getpid (), listen_vpp_handle, listen_session_index,
1121 client_session->vpp_handle, client_session_index,
1122 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1123 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001124
Dave Wallace048b1d62018-01-03 22:24:41 -05001125 if (ep)
1126 {
Florin Coras7e12d942018-06-27 14:32:43 -07001127 ep->is_ip4 = client_session->transport.is_ip4;
1128 ep->port = client_session->transport.rmt_port;
1129 if (client_session->transport.is_ip4)
1130 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
Dave Wallace048b1d62018-01-03 22:24:41 -05001131 sizeof (ip4_address_t));
1132 else
Florin Coras7e12d942018-06-27 14:32:43 -07001133 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
Dave Wallace048b1d62018-01-03 22:24:41 -05001134 sizeof (ip6_address_t));
1135 }
Dave Wallace60caa062017-11-10 17:07:13 -05001136
Florin Coras54693d22018-07-17 10:46:29 -07001137 if (accepted_msg.server_event_queue_address)
1138 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1139 svm_msg_q_t *);
1140 else
1141 vpp_evt_q = client_session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -07001142
Florin Coras54693d22018-07-17 10:46:29 -07001143 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1144 client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05001145
Florin Coras54693d22018-07-17 10:46:29 -07001146 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1147 "sid %u connection from peer %s address %U port %u to local %s "
1148 "address %U port %u", getpid (), listen_vpp_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001149 listen_session_index, client_session->vpp_handle,
1150 client_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001151 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1152 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001153 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001154 clib_net_to_host_u16 (client_session->transport.rmt_port),
1155 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1156 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001157 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001158 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001159 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1160 client_session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001161 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001162
Dave Wallace4878cbe2017-11-21 03:45:09 -05001163done:
1164 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001165}
1166
1167int
1168vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
1169{
Florin Coras7e12d942018-06-27 14:32:43 -07001170 vcl_session_t *session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001171 u64 vpp_handle = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001172 int rv, retval = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001173
Dave Wallace7e607a72018-06-18 18:41:32 -04001174 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001175
1176 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001177 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001178 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001179 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1180 "connect on an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001181 rv = VPPCOM_EBADFD;
1182 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001183 }
1184
Florin Coras7e12d942018-06-27 14:32:43 -07001185 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001186 {
Florin Coras0d427d82018-06-27 03:24:07 -07001187 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1188 "connected to %s %U port %d proto %s, state 0x%x (%s)",
1189 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001190 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001191 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001192 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001193 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001194 clib_net_to_host_u16 (session->transport.rmt_port),
1195 session->session_type ? "UDP" : "TCP", session->session_state,
1196 vppcom_session_state_str (session->session_state));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001197
Dave Wallace7e607a72018-06-18 18:41:32 -04001198 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05001199 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001200 }
1201
Florin Coras7e12d942018-06-27 14:32:43 -07001202 session->transport.is_ip4 = server_ep->is_ip4;
1203 if (session->transport.is_ip4)
1204 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -04001205 sizeof (ip4_address_t));
1206 else
Florin Coras7e12d942018-06-27 14:32:43 -07001207 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -04001208 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001209 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04001210
Florin Coras0d427d82018-06-27 03:24:07 -07001211 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1212 "port %d proto %s",
1213 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001214 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001215 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001216 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001217 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001218 clib_net_to_host_u16 (session->transport.rmt_port),
1219 session->session_type ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04001220
1221 vppcom_send_connect_sock (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001222 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05001223
Florin Coras54693d22018-07-17 10:46:29 -07001224 retval = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1225 vcm->cfg.session_timeout);
Dave Wallaceee45d412017-11-24 21:44:06 -05001226
Dave Wallace7e607a72018-06-18 18:41:32 -04001227 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -05001228 vpp_handle = session->vpp_handle;
Dave Wallace7e607a72018-06-18 18:41:32 -04001229 VCL_SESSION_UNLOCK ();
Dave Wallace7876d392017-10-19 03:53:57 -04001230
Dave Wallace4878cbe2017-11-21 03:45:09 -05001231done:
Dave Wallaceee45d412017-11-24 21:44:06 -05001232 if (PREDICT_FALSE (retval))
1233 {
1234 rv = retval;
1235 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001236 {
1237 if (session)
Florin Coras0d427d82018-06-27 03:24:07 -07001238 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
1239 "failed! returning %d (%s)", getpid (), vpp_handle,
1240 session_index, rv, vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001241 else
1242 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1243 "returning %d (%s)", getpid (),
1244 session_index, rv, vppcom_retval_str (rv));
1245 }
Dave Wallaceee45d412017-11-24 21:44:06 -05001246 }
Florin Coras0d427d82018-06-27 03:24:07 -07001247 else
1248 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
1249 getpid (), vpp_handle, session_index);
Dave Wallaceee45d412017-11-24 21:44:06 -05001250
Dave Wallace4878cbe2017-11-21 03:45:09 -05001251 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001252}
1253
Florin Coras54693d22018-07-17 10:46:29 -07001254static u8
1255vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1256{
1257 if (!is_ct)
1258 return (e->event_type == FIFO_EVENT_APP_RX
1259 && e->fifo->client_session_index == sid);
1260 else
1261 return (e->event_type == SESSION_IO_EVT_CT_TX);
1262}
1263
Florin Coras460dce62018-07-27 05:45:06 -07001264static inline u8
1265vcl_session_is_readable (vcl_session_t * s)
1266{
1267 return ((s->session_state & STATE_OPEN)
1268 || (s->session_state == STATE_LISTEN
1269 && s->session_type == VPPCOM_PROTO_UDP));
1270}
1271
Steven58f464e2017-10-25 12:33:12 -07001272static inline int
1273vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
1274 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001275{
Florin Coras54693d22018-07-17 10:46:29 -07001276 int n_read = 0, rv, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001277 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001278 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001279 svm_msg_q_msg_t msg;
1280 session_event_t *e;
1281 svm_msg_q_t *mq;
1282 u8 is_full;
Dave Wallace543852a2017-08-03 02:11:34 -04001283
1284 ASSERT (buf);
1285
Florin Coras460dce62018-07-27 05:45:06 -07001286 VCL_SESSION_LOCK_AND_GET (session_index, &s);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001287
Florin Coras460dce62018-07-27 05:45:06 -07001288 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001289 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001290 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001291 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1292 "read from an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001293 rv = VPPCOM_EBADFD;
1294 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001295 }
1296
Florin Coras460dce62018-07-27 05:45:06 -07001297 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1298 rx_fifo = s->rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001299
Florin Coras460dce62018-07-27 05:45:06 -07001300 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001301 {
Florin Coras460dce62018-07-27 05:45:06 -07001302 session_state_t state = s->session_state;
Dave Wallace7e607a72018-06-18 18:41:32 -04001303 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001304 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001305
Florin Coras0d427d82018-06-27 03:24:07 -07001306 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1307 "state 0x%x (%s), returning %d (%s)",
Florin Coras460dce62018-07-27 05:45:06 -07001308 getpid (), s->vpp_handle, session_index, state,
Florin Coras0d427d82018-06-27 03:24:07 -07001309 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001310 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001311 }
1312
Dave Wallace7e607a72018-06-18 18:41:32 -04001313 VCL_SESSION_UNLOCK ();
Florin Coras460dce62018-07-27 05:45:06 -07001314 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
Florin Coras99368312018-08-02 10:45:44 -07001315 svm_fifo_unset_event (rx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07001316 is_full = svm_fifo_is_full (rx_fifo);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001317
Florin Coras54693d22018-07-17 10:46:29 -07001318 if (svm_fifo_is_empty (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001319 {
Florin Coras54693d22018-07-17 10:46:29 -07001320 if (is_nonblocking)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001321 {
Florin Coras54693d22018-07-17 10:46:29 -07001322 rv = VPPCOM_OK;
1323 goto done;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001324 }
Florin Coras54693d22018-07-17 10:46:29 -07001325 while (1)
1326 {
Florin Coras99368312018-08-02 10:45:44 -07001327 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001328 if (svm_msg_q_is_empty (mq))
1329 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001330
Florin Coras54693d22018-07-17 10:46:29 -07001331 svm_msg_q_sub_w_lock (mq, &msg);
1332 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001333 svm_msg_q_unlock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001334 if (!vcl_is_rx_evt_for_session (e, session_index,
Florin Coras460dce62018-07-27 05:45:06 -07001335 s->our_evt_q != 0))
Florin Coras54693d22018-07-17 10:46:29 -07001336 {
1337 vcl_handle_mq_ctrl_event (e);
1338 svm_msg_q_free_msg (mq, &msg);
1339 continue;
1340 }
Florin Coras99368312018-08-02 10:45:44 -07001341 svm_fifo_unset_event (rx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07001342 svm_msg_q_free_msg (mq, &msg);
Florin Coras60f1fc12018-08-16 14:57:31 -07001343 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1344 return 0;
1345 if (svm_fifo_is_empty (rx_fifo))
1346 continue;
Florin Coras54693d22018-07-17 10:46:29 -07001347 break;
1348 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001349 }
Florin Coras54693d22018-07-17 10:46:29 -07001350
Florin Coras460dce62018-07-27 05:45:06 -07001351 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001352 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001353 else
Florin Coras99368312018-08-02 10:45:44 -07001354 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001355
Florin Coras460dce62018-07-27 05:45:06 -07001356 if (vcl_session_is_ct (s) && is_full)
Florin Coras99368312018-08-02 10:45:44 -07001357 {
1358 /* If the peer is not polling send notification */
1359 if (!svm_fifo_has_event (s->rx_fifo))
1360 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1361 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1362 }
Florin Coras54693d22018-07-17 10:46:29 -07001363
Dave Wallace4878cbe2017-11-21 03:45:09 -05001364 if (VPPCOM_DEBUG > 2)
1365 {
Florin Coras54693d22018-07-17 10:46:29 -07001366 if (n_read > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001367 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
Florin Coras460dce62018-07-27 05:45:06 -07001368 "from (%p)", getpid (), s->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001369 session_index, n_read, rx_fifo);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001370 else
Dave Wallace048b1d62018-01-03 22:24:41 -05001371 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
Florin Coras460dce62018-07-27 05:45:06 -07001372 "returning %d (%s)", getpid (), s->vpp_handle,
Dave Wallaceee45d412017-11-24 21:44:06 -05001373 session_index, rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001374 }
Florin Coras54693d22018-07-17 10:46:29 -07001375 return n_read;
1376
Dave Wallace4878cbe2017-11-21 03:45:09 -05001377done:
1378 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001379}
1380
Steven58f464e2017-10-25 12:33:12 -07001381int
Dave Wallace048b1d62018-01-03 22:24:41 -05001382vppcom_session_read (uint32_t session_index, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001383{
1384 return (vppcom_session_read_internal (session_index, buf, n, 0));
1385}
1386
1387static int
1388vppcom_session_peek (uint32_t session_index, void *buf, int n)
1389{
1390 return (vppcom_session_read_internal (session_index, buf, n, 1));
1391}
1392
Dave Wallace543852a2017-08-03 02:11:34 -04001393static inline int
Florin Coras54693d22018-07-17 10:46:29 -07001394vppcom_session_read_ready (vcl_session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -04001395{
Dave Wallace543852a2017-08-03 02:11:34 -04001396 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001397 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001398 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001399 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Florin Coras54693d22018-07-17 10:46:29 -07001400 "epoll session!", getpid (), vcl_session_index (session));
1401 return VPPCOM_EBADFD;
1402 }
1403
1404 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1405 {
1406 session_state_t state = session->session_state;
1407 int rv;
1408
1409 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1410
1411 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1412 " state 0x%x (%s), returning %d (%s)", getpid (),
1413 session->vpp_handle, vcl_session_index (session), state,
1414 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1415 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001416 }
Dave Wallace33e002b2017-09-06 01:20:02 -04001417
Florin Coras7e12d942018-06-27 14:32:43 -07001418 if (session->session_state & STATE_LISTEN)
Florin Coras54693d22018-07-17 10:46:29 -07001419 return clib_fifo_elts (session->accept_evts_fifo);
1420
1421 return svm_fifo_max_dequeue (session->rx_fifo);
1422}
1423
1424static u8
1425vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1426{
1427 if (!is_ct)
1428 return (e->event_type == FIFO_EVENT_APP_TX
1429 && e->fifo->client_session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001430 else
Florin Coras54693d22018-07-17 10:46:29 -07001431 return (e->event_type == SESSION_IO_EVT_CT_RX);
Dave Wallace543852a2017-08-03 02:11:34 -04001432}
1433
1434int
Dave Wallace048b1d62018-01-03 22:24:41 -05001435vppcom_session_write (uint32_t session_index, void *buf, size_t n)
Dave Wallace543852a2017-08-03 02:11:34 -04001436{
Florin Coras54693d22018-07-17 10:46:29 -07001437 int rv, n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001438 vcl_session_t *s = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001439 svm_fifo_t *tx_fifo = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001440 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001441 svm_msg_q_msg_t msg;
1442 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001443 svm_msg_q_t *mq;
Dave Wallace543852a2017-08-03 02:11:34 -04001444
1445 ASSERT (buf);
1446
Florin Coras460dce62018-07-27 05:45:06 -07001447 VCL_SESSION_LOCK_AND_GET (session_index, &s);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001448
Florin Coras460dce62018-07-27 05:45:06 -07001449 tx_fifo = s->tx_fifo;
1450 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001451
Florin Coras460dce62018-07-27 05:45:06 -07001452 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001453 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001454 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001455 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001456 "cannot write to an epoll session!",
Florin Coras460dce62018-07-27 05:45:06 -07001457 getpid (), s->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001458
1459 rv = VPPCOM_EBADFD;
1460 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001461 }
1462
Florin Coras460dce62018-07-27 05:45:06 -07001463 if (!(s->session_state & STATE_OPEN))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001464 {
Florin Coras460dce62018-07-27 05:45:06 -07001465 session_state_t state = s->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001466 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace7e607a72018-06-18 18:41:32 -04001467 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07001468 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1469 "state 0x%x (%s)",
Florin Coras460dce62018-07-27 05:45:06 -07001470 getpid (), s->vpp_handle, session_index,
Florin Coras0d427d82018-06-27 03:24:07 -07001471 state, vppcom_session_state_str (state));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001472 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001473 }
1474
Dave Wallace7e607a72018-06-18 18:41:32 -04001475 VCL_SESSION_UNLOCK ();
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001476
Florin Coras460dce62018-07-27 05:45:06 -07001477 mq = vcl_session_is_ct (s) ? s->our_evt_q : vcm->app_event_queue;
Florin Coras54693d22018-07-17 10:46:29 -07001478 if (svm_fifo_is_full (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001479 {
Florin Coras54693d22018-07-17 10:46:29 -07001480 if (is_nonblocking)
1481 {
1482 rv = VPPCOM_EWOULDBLOCK;
1483 goto done;
1484 }
Florin Coras60f1fc12018-08-16 14:57:31 -07001485 while (svm_fifo_is_full (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001486 {
Florin Coras99368312018-08-02 10:45:44 -07001487 svm_msg_q_lock (mq);
Florin Coras99368312018-08-02 10:45:44 -07001488 while (svm_msg_q_is_empty (mq) && svm_msg_q_timedwait (mq, 10e-6))
1489 ;
Florin Coras54693d22018-07-17 10:46:29 -07001490 svm_msg_q_sub_w_lock (mq, &msg);
1491 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001492 svm_msg_q_unlock (mq);
1493
Florin Coras54693d22018-07-17 10:46:29 -07001494 if (!vcl_is_tx_evt_for_session (e, session_index,
Florin Coras460dce62018-07-27 05:45:06 -07001495 s->our_evt_q != 0))
Florin Coras60f1fc12018-08-16 14:57:31 -07001496 vcl_handle_mq_ctrl_event (e);
Florin Coras54693d22018-07-17 10:46:29 -07001497 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001498 }
Dave Wallace543852a2017-08-03 02:11:34 -04001499 }
Dave Wallace543852a2017-08-03 02:11:34 -04001500
Florin Coras460dce62018-07-27 05:45:06 -07001501 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1502 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1503 if (s->is_dgram)
1504 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1505 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1506 else
1507 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1508 SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001509
Florin Coras460dce62018-07-27 05:45:06 -07001510 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001511
1512 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001513 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001514 if (n_write <= 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001515 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Florin Coras460dce62018-07-27 05:45:06 -07001516 "FIFO-FULL (%p)", getpid (), s->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001517 session_index, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001518 else
Dave Wallace048b1d62018-01-03 22:24:41 -05001519 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001520 "wrote %d bytes tx-fifo: (%p)", getpid (),
Florin Coras460dce62018-07-27 05:45:06 -07001521 s->vpp_handle, session_index, n_write, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001522 }
Florin Coras54693d22018-07-17 10:46:29 -07001523 return n_write;
1524
Dave Wallace4878cbe2017-11-21 03:45:09 -05001525done:
1526 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001527}
1528
Florin Coras99368312018-08-02 10:45:44 -07001529static vcl_session_t *
1530vcl_ct_session_get_from_fifo (svm_fifo_t * f, u8 type)
1531{
1532 vcl_session_t *s;
1533 s = vcl_session_get (f->client_session_index);
1534 if (s)
1535 {
1536 /* rx fifo */
1537 if (type == 0 && s->rx_fifo == f)
1538 return s;
1539 /* tx fifo */
1540 if (type == 1 && s->tx_fifo == f)
1541 return s;
1542 }
1543 s = vcl_session_get (f->master_session_index);
1544 if (s)
1545 {
1546 if (type == 0 && s->rx_fifo == f)
1547 return s;
1548 if (type == 1 && s->tx_fifo == f)
1549 return s;
1550 }
1551 return 0;
1552}
1553
Dave Wallace543852a2017-08-03 02:11:34 -04001554static inline int
Florin Coras7e12d942018-06-27 14:32:43 -07001555vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001556{
Dave Wallace543852a2017-08-03 02:11:34 -04001557 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001558 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001559 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001560 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001561 "cannot write to an epoll session!",
1562 getpid (), session->vpp_handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001563 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001564 }
1565
Florin Coras7e12d942018-06-27 14:32:43 -07001566 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
Dave Wallace33e002b2017-09-06 01:20:02 -04001567 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001568 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001569 "cannot write to a listen session!",
1570 getpid (), session->vpp_handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001571 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001572 }
1573
Florin Coras54693d22018-07-17 10:46:29 -07001574 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001575 {
Florin Coras7e12d942018-06-27 14:32:43 -07001576 session_state_t state = session->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001577 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001578
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001579 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace048b1d62018-01-03 22:24:41 -05001580 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001581 "session is not open! state 0x%x (%s), "
Dave Wallaceee45d412017-11-24 21:44:06 -05001582 "returning %d (%s)", getpid (), session->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001583 session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05001584 state, vppcom_session_state_str (state),
1585 rv, vppcom_retval_str (rv));
Florin Coras54693d22018-07-17 10:46:29 -07001586 return rv;
Dave Wallace33e002b2017-09-06 01:20:02 -04001587 }
1588
Florin Coras0d427d82018-06-27 03:24:07 -07001589 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1590 getpid (), session->vpp_handle, session_index, session->tx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -07001591 svm_fifo_max_enqueue (session->tx_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001592
Florin Coras54693d22018-07-17 10:46:29 -07001593 return svm_fifo_max_enqueue (session->tx_fifo);
1594}
1595
Florin Coras99368312018-08-02 10:45:44 -07001596static inline int
1597vcl_mq_dequeue_batch (svm_msg_q_t * mq)
1598{
1599 svm_msg_q_msg_t *msg;
1600 u32 n_msgs;
1601 int i;
1602
1603 n_msgs = svm_msg_q_size (mq);
1604 for (i = 0; i < n_msgs; i++)
1605 {
1606 vec_add2 (vcm->mq_msg_vector, msg, 1);
1607 svm_msg_q_sub_w_lock (mq, msg);
1608 }
1609 return n_msgs;
1610}
1611
Florin Coras54693d22018-07-17 10:46:29 -07001612static int
1613vcl_select_handle_mq (svm_msg_q_t * mq, unsigned long n_bits,
1614 unsigned long *read_map, unsigned long *write_map,
1615 unsigned long *except_map, double time_to_wait,
1616 u32 * bits_set)
1617{
1618 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001619 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001620 session_accepted_msg_t *accepted_msg;
1621 vcl_session_msg_t *vcl_msg;
1622 vcl_session_t *session;
Florin Coras99368312018-08-02 10:45:44 -07001623 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001624 session_event_t *e;
Florin Coras99368312018-08-02 10:45:44 -07001625 u32 i, sid;
Florin Coras54693d22018-07-17 10:46:29 -07001626 u64 handle;
1627
1628 svm_msg_q_lock (mq);
1629 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001630 {
Florin Coras54693d22018-07-17 10:46:29 -07001631 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001632 {
Florin Coras54693d22018-07-17 10:46:29 -07001633 svm_msg_q_unlock (mq);
1634 return 0;
1635 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001636
Florin Coras54693d22018-07-17 10:46:29 -07001637 if (!time_to_wait)
1638 {
1639 svm_msg_q_unlock (mq);
1640 return 0;
1641 }
1642 else if (time_to_wait < 0)
1643 {
1644 svm_msg_q_wait (mq);
1645 }
1646 else
1647 {
1648 if (svm_msg_q_timedwait (mq, time_to_wait))
1649 {
1650 svm_msg_q_unlock (mq);
1651 return 0;
1652 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001653 }
1654 }
Florin Coras99368312018-08-02 10:45:44 -07001655 vcl_mq_dequeue_batch (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001656 svm_msg_q_unlock (mq);
1657
Florin Coras99368312018-08-02 10:45:44 -07001658 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001659 {
Florin Coras99368312018-08-02 10:45:44 -07001660 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
1661 e = svm_msg_q_msg_data (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001662 switch (e->event_type)
1663 {
1664 case FIFO_EVENT_APP_RX:
1665 sid = e->fifo->client_session_index;
1666 session = vcl_session_get (sid);
Florin Coras54693d22018-07-17 10:46:29 -07001667 if (sid < n_bits && read_map)
1668 {
1669 clib_bitmap_set_no_check (read_map, sid, 1);
1670 *bits_set += 1;
1671 }
1672 break;
1673 case FIFO_EVENT_APP_TX:
1674 sid = e->fifo->client_session_index;
1675 session = vcl_session_get (sid);
Florin Coras60f1fc12018-08-16 14:57:31 -07001676 if (!session)
Florin Coras54693d22018-07-17 10:46:29 -07001677 break;
1678 if (sid < n_bits && write_map)
1679 {
1680 clib_bitmap_set_no_check (write_map, sid, 1);
1681 *bits_set += 1;
1682 }
1683 break;
1684 case SESSION_IO_EVT_CT_TX:
1685 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
1686 sid = vcl_session_index (session);
Florin Coras54693d22018-07-17 10:46:29 -07001687 if (sid < n_bits && read_map)
1688 {
1689 clib_bitmap_set_no_check (read_map, sid, 1);
1690 *bits_set += 1;
1691 }
1692 break;
1693 break;
1694 case SESSION_IO_EVT_CT_RX:
1695 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
1696 sid = vcl_session_index (session);
Florin Coras60f1fc12018-08-16 14:57:31 -07001697 if (!session)
Florin Coras54693d22018-07-17 10:46:29 -07001698 break;
1699 if (sid < n_bits && write_map)
1700 {
1701 clib_bitmap_set_no_check (write_map, sid, 1);
1702 *bits_set += 1;
1703 }
1704 break;
1705 case SESSION_CTRL_EVT_ACCEPTED:
1706 accepted_msg = (session_accepted_msg_t *) e->data;
1707 handle = accepted_msg->listener_handle;
1708 session = vppcom_session_table_lookup_listener (handle);
1709 if (!session)
1710 {
1711 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1712 "listener handle %llx", getpid (), handle);
1713 break;
1714 }
1715
1716 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1717 vcl_msg->accepted_msg = *accepted_msg;
1718 sid = session - vcm->sessions;
1719 if (sid < n_bits && read_map)
1720 {
1721 clib_bitmap_set_no_check (read_map, sid, 1);
1722 *bits_set += 1;
1723 }
1724 break;
Florin Coras99368312018-08-02 10:45:44 -07001725 case SESSION_CTRL_EVT_CONNECTED:
1726 connected_msg = (session_connected_msg_t *) e->data;
1727 vcl_session_connected_handler (connected_msg);
1728 break;
Florin Coras54693d22018-07-17 10:46:29 -07001729 case SESSION_CTRL_EVT_DISCONNECTED:
1730 disconnected_msg = (session_disconnected_msg_t *) e->data;
1731 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
1732 if (sid < n_bits && except_map)
1733 {
1734 clib_bitmap_set_no_check (except_map, sid, 1);
1735 *bits_set += 1;
1736 }
1737 break;
1738 default:
1739 clib_warning ("unhandled: %u", e->event_type);
1740 break;
1741 }
Florin Coras99368312018-08-02 10:45:44 -07001742 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001743 }
1744
Florin Coras99368312018-08-02 10:45:44 -07001745 vec_reset_length (vcm->mq_msg_vector);
Florin Coras54693d22018-07-17 10:46:29 -07001746 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001747}
1748
Florin Coras99368312018-08-02 10:45:44 -07001749static int
1750vppcom_select_condvar (unsigned long n_bits, unsigned long *read_map,
1751 unsigned long *write_map, unsigned long *except_map,
1752 double time_to_wait, u32 * bits_set)
1753{
1754 double total_wait = 0, wait_slice;
1755 vcl_cut_through_registration_t *cr;
1756
1757 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
1758 wait_slice = vcm->cut_through_registrations ? 10e-6 : time_to_wait;
1759 do
1760 {
1761 /* *INDENT-OFF* */
1762 pool_foreach (cr, vcm->cut_through_registrations, ({
1763 vcl_select_handle_mq (cr->mq, n_bits, read_map, write_map, except_map,
1764 0, bits_set);
1765 }));
1766 /* *INDENT-ON* */
1767
1768 vcl_select_handle_mq (vcm->app_event_queue, n_bits, read_map, write_map,
1769 except_map, time_to_wait, bits_set);
1770 total_wait += wait_slice;
1771 if (*bits_set)
1772 return *bits_set;
1773 }
1774 while (total_wait < time_to_wait);
1775
1776 return 0;
1777}
1778
1779static int
1780vppcom_select_eventfd (unsigned long n_bits, unsigned long *read_map,
1781 unsigned long *write_map, unsigned long *except_map,
1782 double time_to_wait, u32 * bits_set)
1783{
1784 vcl_mq_evt_conn_t *mqc;
1785 int __clib_unused n_read;
1786 int n_mq_evts, i;
1787 u64 buf;
1788
1789 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
1790 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
1791 vec_len (vcm->mq_events), time_to_wait);
1792 for (i = 0; i < n_mq_evts; i++)
1793 {
1794 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
1795 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
1796 vcl_select_handle_mq (mqc->mq, n_bits, read_map, write_map,
1797 except_map, 0, bits_set);
1798 }
1799
1800 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1801}
1802
Dave Wallace543852a2017-08-03 02:11:34 -04001803int
1804vppcom_select (unsigned long n_bits, unsigned long *read_map,
1805 unsigned long *write_map, unsigned long *except_map,
1806 double time_to_wait)
1807{
Florin Coras54693d22018-07-17 10:46:29 -07001808 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07001809 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001810 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001811
1812 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1813
Dave Wallace7876d392017-10-19 03:53:57 -04001814 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001815 {
1816 clib_bitmap_validate (vcm->rd_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05001817 clib_memcpy (vcm->rd_bitmap, read_map,
1818 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1819 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001820 }
Dave Wallace7876d392017-10-19 03:53:57 -04001821 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001822 {
1823 clib_bitmap_validate (vcm->wr_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05001824 clib_memcpy (vcm->wr_bitmap, write_map,
1825 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1826 memset (write_map, 0,
1827 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001828 }
Dave Wallace7876d392017-10-19 03:53:57 -04001829 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001830 {
1831 clib_bitmap_validate (vcm->ex_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05001832 clib_memcpy (vcm->ex_bitmap, except_map,
1833 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1834 memset (except_map, 0,
1835 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001836 }
1837
Florin Coras54693d22018-07-17 10:46:29 -07001838 if (!n_bits)
1839 return 0;
1840
1841 if (!write_map)
1842 goto check_rd;
1843
1844 /* *INDENT-OFF* */
1845 clib_bitmap_foreach (sid, vcm->wr_bitmap, ({
Florin Coras54693d22018-07-17 10:46:29 -07001846 if (!(session = vcl_session_get (sid)))
1847 {
Florin Coras54693d22018-07-17 10:46:29 -07001848 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1849 getpid (), sid);
1850 return VPPCOM_EBADFD;
1851 }
1852
1853 rv = svm_fifo_is_full (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07001854 if (!rv)
1855 {
1856 clib_bitmap_set_no_check (write_map, sid, 1);
1857 bits_set++;
1858 }
1859 }));
1860
1861check_rd:
1862 if (!read_map)
1863 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07001864
Florin Coras54693d22018-07-17 10:46:29 -07001865 clib_bitmap_foreach (sid, vcm->rd_bitmap, ({
Florin Coras54693d22018-07-17 10:46:29 -07001866 if (!(session = vcl_session_get (sid)))
1867 {
Florin Coras54693d22018-07-17 10:46:29 -07001868 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1869 getpid (), sid);
1870 return VPPCOM_EBADFD;
1871 }
1872
1873 rv = vppcom_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07001874 if (rv)
1875 {
1876 clib_bitmap_set_no_check (read_map, sid, 1);
1877 bits_set++;
1878 }
1879 }));
1880 /* *INDENT-ON* */
1881
1882check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04001883
Florin Coras99368312018-08-02 10:45:44 -07001884 if (vcm->cfg.use_mq_eventfd)
1885 vppcom_select_eventfd (n_bits, read_map, write_map, except_map,
1886 time_to_wait, &bits_set);
1887 else
1888 vppcom_select_condvar (n_bits, read_map, write_map, except_map,
1889 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07001890
Dave Wallace543852a2017-08-03 02:11:34 -04001891 return (bits_set);
1892}
1893
Dave Wallacef7f809c2017-10-03 01:48:42 -04001894static inline void
1895vep_verify_epoll_chain (u32 vep_idx)
1896{
Florin Coras7e12d942018-06-27 14:32:43 -07001897 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001898 vppcom_epoll_t *vep;
1899 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05001900 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001901
Dave Wallace498b3a52017-11-09 13:00:34 -05001902 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001903 return;
1904
1905 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1906 rv = vppcom_session_at_index (vep_idx, &session);
1907 if (PREDICT_FALSE (rv))
1908 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001909 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
1910 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001911 goto done;
1912 }
1913 if (PREDICT_FALSE (!session->is_vep))
1914 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001915 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1916 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001917 goto done;
1918 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001919 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05001920 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05001921 "{\n"
1922 " is_vep = %u\n"
1923 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05001924 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05001925 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05001926 "}\n", getpid (), vep_idx,
1927 session->is_vep, session->is_vep_session,
1928 vep->next_sid, vep->next_sid,
Dave Wallace498b3a52017-11-09 13:00:34 -05001929 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001930
1931 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001932 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001933 rv = vppcom_session_at_index (sid, &session);
1934 if (PREDICT_FALSE (rv))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001935 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001936 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001937 goto done;
1938 }
1939 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05001940 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
1941 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001942 else if (PREDICT_FALSE (!session->is_vep_session))
1943 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001944 clib_warning ("VCL<%d>: ERROR: session (%u) "
1945 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001946 goto done;
1947 }
1948 vep = &session->vep;
1949 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05001950 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001951 "vep_idx (%u)!", getpid (),
1952 sid, session->vep.vep_idx, vep_idx);
1953 if (session->is_vep_session)
1954 {
1955 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
1956 "{\n"
1957 " next_sid = 0x%x (%u)\n"
1958 " prev_sid = 0x%x (%u)\n"
1959 " vep_idx = 0x%x (%u)\n"
1960 " ev.events = 0x%x\n"
1961 " ev.data.u64 = 0x%llx\n"
1962 " et_mask = 0x%x\n"
1963 "}\n",
1964 vep_idx, sid, sid,
1965 vep->next_sid, vep->next_sid,
1966 vep->prev_sid, vep->prev_sid,
1967 vep->vep_idx, vep->vep_idx,
1968 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001969 }
1970 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04001971
1972done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001973 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
1974 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001975}
1976
1977int
1978vppcom_epoll_create (void)
1979{
Florin Coras7e12d942018-06-27 14:32:43 -07001980 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001981 u32 vep_idx;
1982
Dave Wallace7e607a72018-06-18 18:41:32 -04001983 VCL_SESSION_LOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04001984 pool_get (vcm->sessions, vep_session);
1985 memset (vep_session, 0, sizeof (*vep_session));
1986 vep_idx = vep_session - vcm->sessions;
1987
1988 vep_session->is_vep = 1;
1989 vep_session->vep.vep_idx = ~0;
1990 vep_session->vep.next_sid = ~0;
1991 vep_session->vep.prev_sid = ~0;
1992 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001993 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)12756512018-03-06 05:55:27 -08001994 vep_session->poll_reg = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001995
Florin Coras0d427d82018-06-27 03:24:07 -07001996 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
Dave Wallace7e607a72018-06-18 18:41:32 -04001997 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04001998
Florin Coras0d427d82018-06-27 03:24:07 -07001999 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
2000 getpid (), vep_idx, vep_idx);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002001
Dave Wallacef7f809c2017-10-03 01:48:42 -04002002 return (vep_idx);
2003}
2004
2005int
2006vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2007 struct epoll_event *event)
2008{
Florin Coras7e12d942018-06-27 14:32:43 -07002009 vcl_session_t *vep_session;
2010 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002011 int rv;
2012
2013 if (vep_idx == session_index)
2014 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002015 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002016 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002017 return VPPCOM_EINVAL;
2018 }
2019
Dave Wallace7e607a72018-06-18 18:41:32 -04002020 VCL_SESSION_LOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002021 rv = vppcom_session_at_index (vep_idx, &vep_session);
2022 if (PREDICT_FALSE (rv))
2023 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002024 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002025 goto done;
2026 }
2027 if (PREDICT_FALSE (!vep_session->is_vep))
2028 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002029 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002030 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002031 rv = VPPCOM_EINVAL;
2032 goto done;
2033 }
2034
2035 ASSERT (vep_session->vep.vep_idx == ~0);
2036 ASSERT (vep_session->vep.prev_sid == ~0);
2037
2038 rv = vppcom_session_at_index (session_index, &session);
2039 if (PREDICT_FALSE (rv))
2040 {
Florin Coras0d427d82018-06-27 03:24:07 -07002041 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
2042 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002043 goto done;
2044 }
2045 if (PREDICT_FALSE (session->is_vep))
2046 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002047 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002048 rv = VPPCOM_EINVAL;
2049 goto done;
2050 }
2051
2052 switch (op)
2053 {
2054 case EPOLL_CTL_ADD:
2055 if (PREDICT_FALSE (!event))
2056 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002057 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002058 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04002059 rv = VPPCOM_EINVAL;
2060 goto done;
2061 }
2062 if (vep_session->vep.next_sid != ~0)
2063 {
Florin Coras7e12d942018-06-27 14:32:43 -07002064 vcl_session_t *next_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002065 rv = vppcom_session_at_index (vep_session->vep.next_sid,
2066 &next_session);
2067 if (PREDICT_FALSE (rv))
2068 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002069 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002070 "vep.next_sid (%u) on vep_idx (%u)!",
2071 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002072 goto done;
2073 }
2074 ASSERT (next_session->vep.prev_sid == vep_idx);
2075 next_session->vep.prev_sid = session_index;
2076 }
2077 session->vep.next_sid = vep_session->vep.next_sid;
2078 session->vep.prev_sid = vep_idx;
2079 session->vep.vep_idx = vep_idx;
2080 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2081 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002082 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002083 session->is_vep_session = 1;
2084 vep_session->vep.next_sid = session_index;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002085
2086 /* VCL Event Register handler */
Florin Coras7e12d942018-06-27 14:32:43 -07002087 if (session->session_state & STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002088 {
2089 /* Register handler for connect_request event on listen_session_index */
2090 vce_event_key_t evk;
2091 evk.session_index = session_index;
2092 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
Keith Burns (alagalah)12756512018-03-06 05:55:27 -08002093 vep_session->poll_reg =
2094 vce_register_handler (&vcm->event_thread, &evk,
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08002095 vce_poll_wait_connect_request_handler_fn,
2096 0 /* No callback args */ );
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002097 }
Florin Coras0d427d82018-06-27 03:24:07 -07002098 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
2099 "sid %u, events 0x%x, data 0x%llx!",
2100 getpid (), vep_idx, session_index,
2101 event->events, event->data.u64);
2102 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002103 break;
2104
2105 case EPOLL_CTL_MOD:
2106 if (PREDICT_FALSE (!event))
2107 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002108 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002109 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04002110 rv = VPPCOM_EINVAL;
2111 goto done;
2112 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002113 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002114 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002115 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002116 "not a vep session!", getpid (), session_index);
2117 rv = VPPCOM_EINVAL;
2118 goto done;
2119 }
2120 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2121 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002122 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002123 "vep_idx (%u) != vep_idx (%u)!",
2124 getpid (), session_index,
2125 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002126 rv = VPPCOM_EINVAL;
2127 goto done;
2128 }
2129 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2130 session->vep.ev = *event;
Florin Coras0d427d82018-06-27 03:24:07 -07002131 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2132 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
2133 event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002134 break;
2135
2136 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002137 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002138 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002139 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002140 "not a vep session!", getpid (), session_index);
2141 rv = VPPCOM_EINVAL;
2142 goto done;
2143 }
2144 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2145 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002146 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002147 "vep_idx (%u) != vep_idx (%u)!",
2148 getpid (), session_index,
2149 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002150 rv = VPPCOM_EINVAL;
2151 goto done;
2152 }
2153
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002154 /* VCL Event Un-register handler */
Florin Coras7e12d942018-06-27 14:32:43 -07002155 if ((session->session_state & STATE_LISTEN) && vep_session->poll_reg)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002156 {
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002157 (void) vce_unregister_handler (&vcm->event_thread,
2158 vep_session->poll_reg);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002159 }
2160
Dave Wallacef7f809c2017-10-03 01:48:42 -04002161 vep_session->wait_cont_idx =
2162 (vep_session->wait_cont_idx == session_index) ?
2163 session->vep.next_sid : vep_session->wait_cont_idx;
2164
2165 if (session->vep.prev_sid == vep_idx)
2166 vep_session->vep.next_sid = session->vep.next_sid;
2167 else
2168 {
Florin Coras7e12d942018-06-27 14:32:43 -07002169 vcl_session_t *prev_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002170 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
2171 if (PREDICT_FALSE (rv))
2172 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002173 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002174 "vep.prev_sid (%u) on sid (%u)!",
2175 getpid (), session->vep.prev_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002176 goto done;
2177 }
2178 ASSERT (prev_session->vep.next_sid == session_index);
2179 prev_session->vep.next_sid = session->vep.next_sid;
2180 }
2181 if (session->vep.next_sid != ~0)
2182 {
Florin Coras7e12d942018-06-27 14:32:43 -07002183 vcl_session_t *next_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002184 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
2185 if (PREDICT_FALSE (rv))
2186 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002187 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002188 "vep.next_sid (%u) on sid (%u)!",
2189 getpid (), session->vep.next_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002190 goto done;
2191 }
2192 ASSERT (next_session->vep.prev_sid == session_index);
2193 next_session->vep.prev_sid = session->vep.prev_sid;
2194 }
2195
2196 memset (&session->vep, 0, sizeof (session->vep));
2197 session->vep.next_sid = ~0;
2198 session->vep.prev_sid = ~0;
2199 session->vep.vep_idx = ~0;
2200 session->is_vep_session = 0;
Florin Coras0d427d82018-06-27 03:24:07 -07002201 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
2202 getpid (), vep_idx, session_index);
2203 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002204 break;
2205
2206 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05002207 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002208 rv = VPPCOM_EINVAL;
2209 }
2210
2211 vep_verify_epoll_chain (vep_idx);
2212
2213done:
Dave Wallace7e607a72018-06-18 18:41:32 -04002214 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002215 return rv;
2216}
2217
Florin Coras54693d22018-07-17 10:46:29 -07002218static int
2219vcl_epoll_wait_handle_mq (svm_msg_q_t * mq, struct epoll_event *events,
2220 u32 maxevents, double wait_for_time, u32 * num_ev)
2221{
2222 session_disconnected_msg_t *disconnected_msg;
2223 session_connected_msg_t *connected_msg;
2224 session_accepted_msg_t *accepted_msg;
Florin Coras54693d22018-07-17 10:46:29 -07002225 u64 session_evt_data = ~0, handle;
Florin Coras99368312018-08-02 10:45:44 -07002226 u32 sid = ~0, session_events;
Florin Coras54693d22018-07-17 10:46:29 -07002227 vcl_session_msg_t *vcl_msg;
2228 vcl_session_t *session;
Florin Coras99368312018-08-02 10:45:44 -07002229 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002230 session_event_t *e;
2231 u8 add_event;
2232 int i;
2233
2234 svm_msg_q_lock (mq);
2235 if (svm_msg_q_is_empty (mq))
2236 {
2237 if (!wait_for_time)
2238 {
2239 svm_msg_q_unlock (mq);
2240 return 0;
2241 }
2242 else if (wait_for_time < 0)
2243 {
2244 svm_msg_q_wait (mq);
2245 }
2246 else
2247 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002248 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002249 {
2250 svm_msg_q_unlock (mq);
2251 return 0;
2252 }
2253 }
2254 }
Florin Coras99368312018-08-02 10:45:44 -07002255 vcl_mq_dequeue_batch (mq);
Florin Coras54693d22018-07-17 10:46:29 -07002256 svm_msg_q_unlock (mq);
2257
Florin Coras99368312018-08-02 10:45:44 -07002258 for (i = 0; i < vec_len (vcm->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002259 {
Florin Coras99368312018-08-02 10:45:44 -07002260 msg = vec_elt_at_index (vcm->mq_msg_vector, i);
2261 e = svm_msg_q_msg_data (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002262 add_event = 0;
2263 switch (e->event_type)
2264 {
2265 case FIFO_EVENT_APP_RX:
2266 sid = e->fifo->client_session_index;
Florin Coras54693d22018-07-17 10:46:29 -07002267 session = vcl_session_get (sid);
2268 session_events = session->vep.ev.events;
Florin Coras99368312018-08-02 10:45:44 -07002269 if (!(EPOLLIN & session->vep.ev.events))
2270 break;
Florin Coras60f1fc12018-08-16 14:57:31 -07002271 add_event = 1;
2272 events[*num_ev].events |= EPOLLIN;
2273 session_evt_data = session->vep.ev.data.u64;
Florin Coras54693d22018-07-17 10:46:29 -07002274 break;
2275 case FIFO_EVENT_APP_TX:
2276 sid = e->fifo->client_session_index;
Florin Coras54693d22018-07-17 10:46:29 -07002277 session = vcl_session_get (sid);
2278 session_events = session->vep.ev.events;
Florin Coras99368312018-08-02 10:45:44 -07002279 if (!(EPOLLOUT & session_events))
2280 break;
Florin Coras60f1fc12018-08-16 14:57:31 -07002281 add_event = 1;
2282 events[*num_ev].events |= EPOLLOUT;
2283 session_evt_data = session->vep.ev.data.u64;
Florin Coras54693d22018-07-17 10:46:29 -07002284 break;
2285 case SESSION_IO_EVT_CT_TX:
2286 session = vcl_ct_session_get_from_fifo (e->fifo, 0);
2287 sid = vcl_session_index (session);
2288 session_events = session->vep.ev.events;
Florin Coras99368312018-08-02 10:45:44 -07002289 if (!(EPOLLIN & session->vep.ev.events))
2290 break;
Florin Coras60f1fc12018-08-16 14:57:31 -07002291 add_event = 1;
2292 events[*num_ev].events |= EPOLLIN;
2293 session_evt_data = session->vep.ev.data.u64;
Florin Coras54693d22018-07-17 10:46:29 -07002294 break;
2295 case SESSION_IO_EVT_CT_RX:
2296 session = vcl_ct_session_get_from_fifo (e->fifo, 1);
2297 sid = vcl_session_index (session);
2298 session_events = session->vep.ev.events;
Florin Coras99368312018-08-02 10:45:44 -07002299 if (!(EPOLLOUT & session_events))
2300 break;
Florin Coras60f1fc12018-08-16 14:57:31 -07002301 add_event = 1;
2302 events[*num_ev].events |= EPOLLOUT;
2303 session_evt_data = session->vep.ev.data.u64;
Florin Coras54693d22018-07-17 10:46:29 -07002304 break;
2305 case SESSION_CTRL_EVT_ACCEPTED:
2306 accepted_msg = (session_accepted_msg_t *) e->data;
2307 handle = accepted_msg->listener_handle;
2308 session = vppcom_session_table_lookup_listener (handle);
2309 if (!session)
2310 {
2311 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2312 "listener handle %llx", getpid (), handle);
2313 break;
2314 }
2315
2316 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2317 vcl_msg->accepted_msg = *accepted_msg;
2318 session_events = session->vep.ev.events;
2319 if (!(EPOLLIN & session_events))
2320 break;
2321
2322 add_event = 1;
2323 events[*num_ev].events |= EPOLLIN;
2324 session_evt_data = session->vep.ev.data.u64;
2325 break;
2326 case SESSION_CTRL_EVT_CONNECTED:
2327 connected_msg = (session_connected_msg_t *) e->data;
2328 vcl_session_connected_handler (connected_msg);
2329 /* Generate EPOLLOUT because there's no connected event */
2330 sid = vcl_session_get_index_from_handle (connected_msg->handle);
2331 clib_spinlock_lock (&vcm->sessions_lockp);
2332 session = vcl_session_get (sid);
2333 session_events = session->vep.ev.events;
2334 if (EPOLLOUT & session_events)
2335 {
2336 add_event = 1;
2337 events[*num_ev].events |= EPOLLOUT;
2338 session_evt_data = session->vep.ev.data.u64;
2339 }
2340 clib_spinlock_unlock (&vcm->sessions_lockp);
2341 break;
2342 case SESSION_CTRL_EVT_DISCONNECTED:
2343 disconnected_msg = (session_disconnected_msg_t *) e->data;
2344 sid = vcl_session_get_index_from_handle (disconnected_msg->handle);
2345 clib_spinlock_lock (&vcm->sessions_lockp);
2346 session = vcl_session_get (sid);
2347 add_event = 1;
2348 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2349 session_evt_data = session->vep.ev.data.u64;
2350 session_events = session->vep.ev.events;
2351 clib_spinlock_unlock (&vcm->sessions_lockp);
2352 break;
2353 default:
2354 clib_warning ("unhandled: %u", e->event_type);
Florin Coras99368312018-08-02 10:45:44 -07002355 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002356 continue;
2357 }
Florin Coras99368312018-08-02 10:45:44 -07002358 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002359
2360 if (add_event)
2361 {
2362 events[*num_ev].data.u64 = session_evt_data;
2363 if (EPOLLONESHOT & session_events)
2364 {
2365 clib_spinlock_lock (&vcm->sessions_lockp);
2366 session = vcl_session_get (sid);
2367 session->vep.ev.events = 0;
2368 clib_spinlock_unlock (&vcm->sessions_lockp);
2369 }
2370 *num_ev += 1;
2371 if (*num_ev == maxevents)
2372 break;
2373 }
2374 }
Florin Coras99368312018-08-02 10:45:44 -07002375
2376 vec_reset_length (vcm->mq_msg_vector);
Florin Coras54693d22018-07-17 10:46:29 -07002377 return *num_ev;
2378}
2379
Florin Coras99368312018-08-02 10:45:44 -07002380static int
2381vppcom_epoll_wait_condvar (struct epoll_event *events, int maxevents,
2382 double wait_for_time)
2383{
2384 vcl_cut_through_registration_t *cr;
2385 double total_wait = 0, wait_slice;
2386 u32 num_ev = 0;
2387 int rv;
2388
2389 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
2390 wait_slice = vcm->cut_through_registrations ? 10e-6 : wait_for_time;
2391
2392 do
2393 {
2394 /* *INDENT-OFF* */
2395 pool_foreach (cr, vcm->cut_through_registrations, ({
2396 vcl_epoll_wait_handle_mq (cr->mq, events, maxevents, 0, &num_ev);
2397 }));
2398 /* *INDENT-ON* */
2399
2400 rv = vcl_epoll_wait_handle_mq (vcm->app_event_queue, events, maxevents,
2401 num_ev ? 0 : wait_slice, &num_ev);
2402 if (rv)
2403 total_wait += wait_slice;
2404 if (num_ev)
2405 return num_ev;
2406 }
2407 while (total_wait < wait_for_time);
2408 return (int) num_ev;
2409}
2410
2411static int
2412vppcom_epoll_wait_eventfd (struct epoll_event *events, int maxevents,
2413 double wait_for_time)
2414{
2415 vcl_mq_evt_conn_t *mqc;
2416 int __clib_unused n_read;
2417 int n_mq_evts, i;
2418 u32 n_evts = 0;
2419 u64 buf;
2420
2421 vec_validate (vcm->mq_events, pool_elts (vcm->mq_evt_conns));
2422 n_mq_evts = epoll_wait (vcm->mqs_epfd, vcm->mq_events,
2423 vec_len (vcm->mq_events), wait_for_time);
2424 for (i = 0; i < n_mq_evts; i++)
2425 {
2426 mqc = vcl_mq_evt_conn_get (vcm->mq_events[i].data.u32);
2427 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
2428 vcl_epoll_wait_handle_mq (mqc->mq, events, maxevents, 0, &n_evts);
2429 }
2430
2431 return (int) n_evts;
2432}
2433
Dave Wallacef7f809c2017-10-03 01:48:42 -04002434int
2435vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
2436 int maxevents, double wait_for_time)
2437{
Florin Coras7e12d942018-06-27 14:32:43 -07002438 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002439
2440 if (PREDICT_FALSE (maxevents <= 0))
2441 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002442 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002443 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002444 return VPPCOM_EINVAL;
2445 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002446
Florin Coras54693d22018-07-17 10:46:29 -07002447 clib_spinlock_lock (&vcm->sessions_lockp);
2448 vep_session = vcl_session_get (vep_idx);
2449 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002450 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002451 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002452 getpid (), vep_idx);
Florin Coras54693d22018-07-17 10:46:29 -07002453 clib_spinlock_unlock (&vcm->sessions_lockp);
2454 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002455 }
Florin Coras54693d22018-07-17 10:46:29 -07002456 clib_spinlock_unlock (&vcm->sessions_lockp);
2457
2458 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002459
Florin Coras99368312018-08-02 10:45:44 -07002460 if (vcm->cfg.use_mq_eventfd)
2461 return vppcom_epoll_wait_eventfd (events, maxevents, wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002462
Florin Coras99368312018-08-02 10:45:44 -07002463 return vppcom_epoll_wait_condvar (events, maxevents, wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002464}
2465
Dave Wallace35830af2017-10-09 01:43:42 -04002466int
2467vppcom_session_attr (uint32_t session_index, uint32_t op,
2468 void *buffer, uint32_t * buflen)
2469{
Florin Coras7e12d942018-06-27 14:32:43 -07002470 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002471 int rv = VPPCOM_OK;
2472 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07002473 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002474
Dave Wallace7e607a72018-06-18 18:41:32 -04002475 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002476
2477 ASSERT (session);
2478
Dave Wallace35830af2017-10-09 01:43:42 -04002479 switch (op)
2480 {
2481 case VPPCOM_ATTR_GET_NREAD:
Florin Coras54693d22018-07-17 10:46:29 -07002482 rv = vppcom_session_read_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002483 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2484 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002485 break;
2486
Dave Wallace227867f2017-11-13 21:21:53 -05002487 case VPPCOM_ATTR_GET_NWRITE:
2488 rv = vppcom_session_write_ready (session, session_index);
Florin Coras0d427d82018-06-27 03:24:07 -07002489 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2490 getpid (), session_index, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002491 break;
2492
2493 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002494 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002495 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002496 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2497 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002498 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07002499 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2500 "is_nonblocking = %u", getpid (),
2501 session_index, *flags,
2502 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002503 }
2504 else
2505 rv = VPPCOM_EINVAL;
2506 break;
2507
2508 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002509 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002510 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002511 if (*flags & O_NONBLOCK)
2512 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2513 else
2514 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2515
Florin Coras0d427d82018-06-27 03:24:07 -07002516 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2517 " is_nonblocking = %u",
2518 getpid (), session_index, *flags,
2519 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002520 }
2521 else
2522 rv = VPPCOM_EINVAL;
2523 break;
2524
2525 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002526 if (PREDICT_TRUE (buffer && buflen &&
2527 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002528 {
Florin Coras7e12d942018-06-27 14:32:43 -07002529 ep->is_ip4 = session->transport.is_ip4;
2530 ep->port = session->transport.rmt_port;
2531 if (session->transport.is_ip4)
2532 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07002533 sizeof (ip4_address_t));
2534 else
Florin Coras7e12d942018-06-27 14:32:43 -07002535 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07002536 sizeof (ip6_address_t));
2537 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002538 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2539 "addr = %U, port %u", getpid (),
2540 session_index, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002541 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002542 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2543 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002544 }
2545 else
2546 rv = VPPCOM_EINVAL;
2547 break;
2548
2549 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002550 if (PREDICT_TRUE (buffer && buflen &&
2551 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002552 {
Florin Coras7e12d942018-06-27 14:32:43 -07002553 ep->is_ip4 = session->transport.is_ip4;
2554 ep->port = session->transport.lcl_port;
2555 if (session->transport.is_ip4)
2556 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07002557 sizeof (ip4_address_t));
2558 else
Florin Coras7e12d942018-06-27 14:32:43 -07002559 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07002560 sizeof (ip6_address_t));
2561 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002562 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2563 " addr = %U port %d", getpid (),
2564 session_index, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002565 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002566 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2567 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002568 }
2569 else
2570 rv = VPPCOM_EINVAL;
2571 break;
Stevenb5a11602017-10-11 09:59:30 -07002572
Dave Wallace048b1d62018-01-03 22:24:41 -05002573 case VPPCOM_ATTR_GET_LIBC_EPFD:
2574 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07002575 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2576 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002577 break;
2578
2579 case VPPCOM_ATTR_SET_LIBC_EPFD:
2580 if (PREDICT_TRUE (buffer && buflen &&
2581 (*buflen == sizeof (session->libc_epfd))))
2582 {
2583 session->libc_epfd = *(int *) buffer;
2584 *buflen = sizeof (session->libc_epfd);
2585
Florin Coras0d427d82018-06-27 03:24:07 -07002586 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2587 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002588 }
2589 else
2590 rv = VPPCOM_EINVAL;
2591 break;
2592
2593 case VPPCOM_ATTR_GET_PROTOCOL:
2594 if (buffer && buflen && (*buflen >= sizeof (int)))
2595 {
Florin Coras7e12d942018-06-27 14:32:43 -07002596 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002597 *buflen = sizeof (int);
2598
Florin Coras0d427d82018-06-27 03:24:07 -07002599 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2600 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2601 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002602 }
2603 else
2604 rv = VPPCOM_EINVAL;
2605 break;
2606
2607 case VPPCOM_ATTR_GET_LISTEN:
2608 if (buffer && buflen && (*buflen >= sizeof (int)))
2609 {
2610 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2611 VCL_SESS_ATTR_LISTEN);
2612 *buflen = sizeof (int);
2613
Florin Coras0d427d82018-06-27 03:24:07 -07002614 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2615 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002616 }
2617 else
2618 rv = VPPCOM_EINVAL;
2619 break;
2620
2621 case VPPCOM_ATTR_GET_ERROR:
2622 if (buffer && buflen && (*buflen >= sizeof (int)))
2623 {
2624 *(int *) buffer = 0;
2625 *buflen = sizeof (int);
2626
Florin Coras0d427d82018-06-27 03:24:07 -07002627 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2628 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002629 }
2630 else
2631 rv = VPPCOM_EINVAL;
2632 break;
2633
2634 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2635 if (buffer && buflen && (*buflen >= sizeof (u32)))
2636 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002637
2638 /* VPP-TBD */
2639 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002640 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002641 vcm->cfg.tx_fifo_size);
2642 *buflen = sizeof (u32);
2643
Florin Coras0d427d82018-06-27 03:24:07 -07002644 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2645 "buflen %d, #VPP-TBD#", getpid (),
2646 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002647 }
2648 else
2649 rv = VPPCOM_EINVAL;
2650 break;
2651
2652 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2653 if (buffer && buflen && (*buflen == sizeof (u32)))
2654 {
2655 /* VPP-TBD */
2656 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002657 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2658 "buflen %d, #VPP-TBD#", getpid (),
2659 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002660 }
2661 else
2662 rv = VPPCOM_EINVAL;
2663 break;
2664
2665 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2666 if (buffer && buflen && (*buflen >= sizeof (u32)))
2667 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002668
2669 /* VPP-TBD */
2670 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002671 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002672 vcm->cfg.rx_fifo_size);
2673 *buflen = sizeof (u32);
2674
Florin Coras0d427d82018-06-27 03:24:07 -07002675 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2676 "buflen %d, #VPP-TBD#", getpid (),
2677 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002678 }
2679 else
2680 rv = VPPCOM_EINVAL;
2681 break;
2682
2683 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2684 if (buffer && buflen && (*buflen == sizeof (u32)))
2685 {
2686 /* VPP-TBD */
2687 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002688 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2689 "buflen %d, #VPP-TBD#", getpid (),
2690 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002691 }
2692 else
2693 rv = VPPCOM_EINVAL;
2694 break;
2695
2696 case VPPCOM_ATTR_GET_REUSEADDR:
2697 if (buffer && buflen && (*buflen >= sizeof (int)))
2698 {
2699 /* VPP-TBD */
2700 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2701 VCL_SESS_ATTR_REUSEADDR);
2702 *buflen = sizeof (int);
2703
Florin Coras0d427d82018-06-27 03:24:07 -07002704 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2705 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002706 }
2707 else
2708 rv = VPPCOM_EINVAL;
2709 break;
2710
Stevenb5a11602017-10-11 09:59:30 -07002711 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002712 if (buffer && buflen && (*buflen == sizeof (int)) &&
2713 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2714 {
2715 /* VPP-TBD */
2716 if (*(int *) buffer)
2717 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2718 else
2719 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2720
Florin Coras0d427d82018-06-27 03:24:07 -07002721 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2722 " #VPP-TBD#", getpid (),
2723 VCL_SESS_ATTR_TEST (session->attr,
2724 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002725 }
2726 else
2727 rv = VPPCOM_EINVAL;
2728 break;
2729
2730 case VPPCOM_ATTR_GET_REUSEPORT:
2731 if (buffer && buflen && (*buflen >= sizeof (int)))
2732 {
2733 /* VPP-TBD */
2734 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2735 VCL_SESS_ATTR_REUSEPORT);
2736 *buflen = sizeof (int);
2737
Florin Coras0d427d82018-06-27 03:24:07 -07002738 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2739 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002740 }
2741 else
2742 rv = VPPCOM_EINVAL;
2743 break;
2744
2745 case VPPCOM_ATTR_SET_REUSEPORT:
2746 if (buffer && buflen && (*buflen == sizeof (int)) &&
2747 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2748 {
2749 /* VPP-TBD */
2750 if (*(int *) buffer)
2751 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2752 else
2753 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2754
Florin Coras0d427d82018-06-27 03:24:07 -07002755 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2756 " #VPP-TBD#", getpid (),
2757 VCL_SESS_ATTR_TEST (session->attr,
2758 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002759 }
2760 else
2761 rv = VPPCOM_EINVAL;
2762 break;
2763
2764 case VPPCOM_ATTR_GET_BROADCAST:
2765 if (buffer && buflen && (*buflen >= sizeof (int)))
2766 {
2767 /* VPP-TBD */
2768 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2769 VCL_SESS_ATTR_BROADCAST);
2770 *buflen = sizeof (int);
2771
Florin Coras0d427d82018-06-27 03:24:07 -07002772 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2773 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002774 }
2775 else
2776 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002777 break;
2778
2779 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002780 if (buffer && buflen && (*buflen == sizeof (int)))
2781 {
2782 /* VPP-TBD */
2783 if (*(int *) buffer)
2784 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2785 else
2786 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2787
Florin Coras0d427d82018-06-27 03:24:07 -07002788 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2789 "#VPP-TBD#", getpid (),
2790 VCL_SESS_ATTR_TEST (session->attr,
2791 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002792 }
2793 else
2794 rv = VPPCOM_EINVAL;
2795 break;
2796
2797 case VPPCOM_ATTR_GET_V6ONLY:
2798 if (buffer && buflen && (*buflen >= sizeof (int)))
2799 {
2800 /* VPP-TBD */
2801 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2802 VCL_SESS_ATTR_V6ONLY);
2803 *buflen = sizeof (int);
2804
Florin Coras0d427d82018-06-27 03:24:07 -07002805 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2806 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002807 }
2808 else
2809 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002810 break;
2811
2812 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002813 if (buffer && buflen && (*buflen == sizeof (int)))
2814 {
2815 /* VPP-TBD */
2816 if (*(int *) buffer)
2817 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2818 else
2819 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2820
Florin Coras0d427d82018-06-27 03:24:07 -07002821 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2822 "#VPP-TBD#", getpid (),
2823 VCL_SESS_ATTR_TEST (session->attr,
2824 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002825 }
2826 else
2827 rv = VPPCOM_EINVAL;
2828 break;
2829
2830 case VPPCOM_ATTR_GET_KEEPALIVE:
2831 if (buffer && buflen && (*buflen >= sizeof (int)))
2832 {
2833 /* VPP-TBD */
2834 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2835 VCL_SESS_ATTR_KEEPALIVE);
2836 *buflen = sizeof (int);
2837
Florin Coras0d427d82018-06-27 03:24:07 -07002838 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2839 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002840 }
2841 else
2842 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002843 break;
Stevenbccd3392017-10-12 20:42:21 -07002844
2845 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002846 if (buffer && buflen && (*buflen == sizeof (int)))
2847 {
2848 /* VPP-TBD */
2849 if (*(int *) buffer)
2850 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2851 else
2852 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2853
Florin Coras0d427d82018-06-27 03:24:07 -07002854 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2855 "#VPP-TBD#", getpid (),
2856 VCL_SESS_ATTR_TEST (session->attr,
2857 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002858 }
2859 else
2860 rv = VPPCOM_EINVAL;
2861 break;
2862
2863 case VPPCOM_ATTR_GET_TCP_NODELAY:
2864 if (buffer && buflen && (*buflen >= sizeof (int)))
2865 {
2866 /* VPP-TBD */
2867 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2868 VCL_SESS_ATTR_TCP_NODELAY);
2869 *buflen = sizeof (int);
2870
Florin Coras0d427d82018-06-27 03:24:07 -07002871 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2872 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002873 }
2874 else
2875 rv = VPPCOM_EINVAL;
2876 break;
2877
2878 case VPPCOM_ATTR_SET_TCP_NODELAY:
2879 if (buffer && buflen && (*buflen == sizeof (int)))
2880 {
2881 /* VPP-TBD */
2882 if (*(int *) buffer)
2883 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2884 else
2885 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2886
Florin Coras0d427d82018-06-27 03:24:07 -07002887 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
2888 "#VPP-TBD#", getpid (),
2889 VCL_SESS_ATTR_TEST (session->attr,
2890 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002891 }
2892 else
2893 rv = VPPCOM_EINVAL;
2894 break;
2895
2896 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
2897 if (buffer && buflen && (*buflen >= sizeof (int)))
2898 {
2899 /* VPP-TBD */
2900 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2901 VCL_SESS_ATTR_TCP_KEEPIDLE);
2902 *buflen = sizeof (int);
2903
Florin Coras0d427d82018-06-27 03:24:07 -07002904 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
2905 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002906 }
2907 else
2908 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07002909 break;
2910
2911 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002912 if (buffer && buflen && (*buflen == sizeof (int)))
2913 {
2914 /* VPP-TBD */
2915 if (*(int *) buffer)
2916 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2917 else
2918 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2919
Florin Coras0d427d82018-06-27 03:24:07 -07002920 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
2921 "#VPP-TBD#", getpid (),
2922 VCL_SESS_ATTR_TEST (session->attr,
2923 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002924 }
2925 else
2926 rv = VPPCOM_EINVAL;
2927 break;
2928
2929 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
2930 if (buffer && buflen && (*buflen >= sizeof (int)))
2931 {
2932 /* VPP-TBD */
2933 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2934 VCL_SESS_ATTR_TCP_KEEPINTVL);
2935 *buflen = sizeof (int);
2936
Florin Coras0d427d82018-06-27 03:24:07 -07002937 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
2938 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002939 }
2940 else
2941 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07002942 break;
2943
2944 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05002945 if (buffer && buflen && (*buflen == sizeof (int)))
2946 {
2947 /* VPP-TBD */
2948 if (*(int *) buffer)
2949 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2950 else
2951 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2952
Florin Coras0d427d82018-06-27 03:24:07 -07002953 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
2954 "#VPP-TBD#", getpid (),
2955 VCL_SESS_ATTR_TEST (session->attr,
2956 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002957 }
2958 else
2959 rv = VPPCOM_EINVAL;
2960 break;
2961
2962 case VPPCOM_ATTR_GET_TCP_USER_MSS:
2963 if (buffer && buflen && (*buflen >= sizeof (u32)))
2964 {
2965 /* VPP-TBD */
2966 *(u32 *) buffer = session->user_mss;
2967 *buflen = sizeof (int);
2968
Florin Coras0d427d82018-06-27 03:24:07 -07002969 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
2970 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002971 }
2972 else
2973 rv = VPPCOM_EINVAL;
2974 break;
2975
2976 case VPPCOM_ATTR_SET_TCP_USER_MSS:
2977 if (buffer && buflen && (*buflen == sizeof (u32)))
2978 {
2979 /* VPP-TBD */
2980 session->user_mss = *(u32 *) buffer;
2981
Florin Coras0d427d82018-06-27 03:24:07 -07002982 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
2983 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002984 }
2985 else
2986 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07002987 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04002988
2989 default:
2990 rv = VPPCOM_EINVAL;
2991 break;
Dave Wallace35830af2017-10-09 01:43:42 -04002992 }
2993
2994done:
Dave Wallace7e607a72018-06-18 18:41:32 -04002995 VCL_SESSION_UNLOCK ();
Dave Wallace35830af2017-10-09 01:43:42 -04002996 return rv;
2997}
2998
Stevenac1f96d2017-10-24 16:03:58 -07002999int
3000vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3001 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3002{
Stevenac1f96d2017-10-24 16:03:58 -07003003 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003004 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003005
3006 if (ep)
3007 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003008 VCL_SESSION_LOCK ();
Stevenac1f96d2017-10-24 16:03:58 -07003009 rv = vppcom_session_at_index (session_index, &session);
3010 if (PREDICT_FALSE (rv))
3011 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003012 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07003013 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
3014 getpid (), session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04003015 VCL_SESSION_UNLOCK ();
Florin Coras460dce62018-07-27 05:45:06 -07003016 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003017 }
Florin Coras7e12d942018-06-27 14:32:43 -07003018 ep->is_ip4 = session->transport.is_ip4;
3019 ep->port = session->transport.rmt_port;
Dave Wallace7e607a72018-06-18 18:41:32 -04003020 VCL_SESSION_UNLOCK ();
Stevenac1f96d2017-10-24 16:03:58 -07003021 }
Steven58f464e2017-10-25 12:33:12 -07003022
3023 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07003024 rv = vppcom_session_read (session_index, buffer, buflen);
3025 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07003026 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003027 else
3028 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003029 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3030 getpid (), flags);
Florin Coras460dce62018-07-27 05:45:06 -07003031 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003032 }
3033
Florin Coras99368312018-08-02 10:45:44 -07003034 if (ep)
3035 {
3036 if (session->transport.is_ip4)
3037 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
3038 sizeof (ip4_address_t));
3039 else
3040 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
3041 sizeof (ip6_address_t));
3042 }
Florin Coras460dce62018-07-27 05:45:06 -07003043
Stevenac1f96d2017-10-24 16:03:58 -07003044 return rv;
3045}
3046
3047int
3048vppcom_session_sendto (uint32_t session_index, void *buffer,
3049 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3050{
Dave Wallace617dffa2017-10-26 14:47:06 -04003051 if (!buffer)
3052 return VPPCOM_EINVAL;
3053
3054 if (ep)
3055 {
3056 // TBD
3057 return VPPCOM_EINVAL;
3058 }
3059
3060 if (flags)
3061 {
3062 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07003063 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3064 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003065 }
3066
3067 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07003068}
3069
Dave Wallace048b1d62018-01-03 22:24:41 -05003070int
3071vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3072{
3073 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3074 u32 i, keep_trying = 1;
3075 int rv, num_ev = 0;
3076
Florin Coras0d427d82018-06-27 03:24:07 -07003077 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3078 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003079
3080 if (!vp)
3081 return VPPCOM_EFAULT;
3082
3083 do
3084 {
Florin Coras7e12d942018-06-27 14:32:43 -07003085 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003086
3087 for (i = 0; i < n_sids; i++)
3088 {
3089 ASSERT (vp[i].revents);
3090
Dave Wallace7e607a72018-06-18 18:41:32 -04003091 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
3092 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05003093
3094 if (*vp[i].revents)
3095 *vp[i].revents = 0;
3096
3097 if (POLLIN & vp[i].events)
3098 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003099 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
Florin Coras54693d22018-07-17 10:46:29 -07003100 rv = vppcom_session_read_ready (session);
Dave Wallace7e607a72018-06-18 18:41:32 -04003101 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05003102 if (rv > 0)
3103 {
3104 *vp[i].revents |= POLLIN;
3105 num_ev++;
3106 }
3107 else if (rv < 0)
3108 {
3109 switch (rv)
3110 {
3111 case VPPCOM_ECONNRESET:
3112 *vp[i].revents = POLLHUP;
3113 break;
3114
3115 default:
3116 *vp[i].revents = POLLERR;
3117 break;
3118 }
3119 num_ev++;
3120 }
3121 }
3122
3123 if (POLLOUT & vp[i].events)
3124 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003125 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003126 rv = vppcom_session_write_ready (session, vp[i].sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04003127 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05003128 if (rv > 0)
3129 {
3130 *vp[i].revents |= POLLOUT;
3131 num_ev++;
3132 }
3133 else if (rv < 0)
3134 {
3135 switch (rv)
3136 {
3137 case VPPCOM_ECONNRESET:
3138 *vp[i].revents = POLLHUP;
3139 break;
3140
3141 default:
3142 *vp[i].revents = POLLERR;
3143 break;
3144 }
3145 num_ev++;
3146 }
3147 }
3148
Dave Wallace7e607a72018-06-18 18:41:32 -04003149 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003150 {
3151 done:
3152 *vp[i].revents = POLLNVAL;
3153 num_ev++;
3154 }
3155 }
3156 if (wait_for_time != -1)
3157 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
3158 }
3159 while ((num_ev == 0) && keep_trying);
3160
3161 if (VPPCOM_DEBUG > 3)
3162 {
3163 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3164 for (i = 0; i < n_sids; i++)
3165 {
3166 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3167 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3168 vp[i].events, *vp[i].revents);
3169 }
3170 }
3171 return num_ev;
3172}
3173
Florin Coras99368312018-08-02 10:45:44 -07003174int
3175vppcom_mq_epoll_fd (void)
3176{
3177 return vcm->mqs_epfd;
3178}
3179
Dave Wallacee22aa742017-10-20 12:30:38 -04003180/*
3181 * fd.io coding-style-patch-verification: ON
3182 *
3183 * Local Variables:
3184 * eval: (c-set-style "gnu")
3185 * End:
3186 */