blob: 1fd138e6dd3c0d98209239dc0e480b4e3f59a6ed [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
Dave Wallace543852a2017-08-03 02:11:34 -040024static const char *
25vppcom_app_state_str (app_state_t state)
26{
27 char *st;
28
29 switch (state)
30 {
31 case STATE_APP_START:
32 st = "STATE_APP_START";
33 break;
34
35 case STATE_APP_CONN_VPP:
36 st = "STATE_APP_CONN_VPP";
37 break;
38
39 case STATE_APP_ENABLED:
40 st = "STATE_APP_ENABLED";
41 break;
42
43 case STATE_APP_ATTACHED:
44 st = "STATE_APP_ATTACHED";
45 break;
46
47 default:
48 st = "UNKNOWN_APP_STATE";
49 break;
50 }
51
52 return st;
53}
54
Florin Coras697faea2018-06-27 17:10:49 -070055const char *
Dave Wallace543852a2017-08-03 02:11:34 -040056vppcom_session_state_str (session_state_t state)
57{
58 char *st;
59
60 switch (state)
61 {
62 case STATE_START:
63 st = "STATE_START";
64 break;
65
66 case STATE_CONNECT:
67 st = "STATE_CONNECT";
68 break;
69
70 case STATE_LISTEN:
71 st = "STATE_LISTEN";
72 break;
73
74 case STATE_ACCEPT:
75 st = "STATE_ACCEPT";
76 break;
77
Dave Wallace4878cbe2017-11-21 03:45:09 -050078 case STATE_CLOSE_ON_EMPTY:
79 st = "STATE_CLOSE_ON_EMPTY";
80 break;
81
Dave Wallace543852a2017-08-03 02:11:34 -040082 case STATE_DISCONNECT:
83 st = "STATE_DISCONNECT";
84 break;
85
86 case STATE_FAILED:
87 st = "STATE_FAILED";
88 break;
89
90 default:
91 st = "UNKNOWN_STATE";
92 break;
93 }
94
95 return st;
96}
97
Dave Wallace543852a2017-08-03 02:11:34 -040098u8 *
99format_ip4_address (u8 * s, va_list * args)
100{
101 u8 *a = va_arg (*args, u8 *);
102 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
103}
104
105u8 *
106format_ip6_address (u8 * s, va_list * args)
107{
108 ip6_address_t *a = va_arg (*args, ip6_address_t *);
109 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
110
111 i_max_n_zero = ARRAY_LEN (a->as_u16);
112 max_n_zeros = 0;
113 i_first_zero = i_max_n_zero;
114 n_zeros = 0;
115 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
116 {
117 u32 is_zero = a->as_u16[i] == 0;
118 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
119 {
120 i_first_zero = i;
121 n_zeros = 0;
122 }
123 n_zeros += is_zero;
124 if ((!is_zero && n_zeros > max_n_zeros)
125 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
126 {
127 i_max_n_zero = i_first_zero;
128 max_n_zeros = n_zeros;
129 i_first_zero = ARRAY_LEN (a->as_u16);
130 n_zeros = 0;
131 }
132 }
133
134 last_double_colon = 0;
135 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
136 {
137 if (i == i_max_n_zero && max_n_zeros > 1)
138 {
139 s = format (s, "::");
140 i += max_n_zeros - 1;
141 last_double_colon = 1;
142 }
143 else
144 {
145 s = format (s, "%s%x",
146 (last_double_colon || i == 0) ? "" : ":",
147 clib_net_to_host_u16 (a->as_u16[i]));
148 last_double_colon = 0;
149 }
150 }
151
152 return s;
153}
154
155/* Format an IP46 address. */
156u8 *
157format_ip46_address (u8 * s, va_list * args)
158{
159 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
160 ip46_type_t type = va_arg (*args, ip46_type_t);
161 int is_ip4 = 1;
162
163 switch (type)
164 {
165 case IP46_TYPE_ANY:
166 is_ip4 = ip46_address_is_ip4 (ip46);
167 break;
168 case IP46_TYPE_IP4:
169 is_ip4 = 1;
170 break;
171 case IP46_TYPE_IP6:
172 is_ip4 = 0;
173 break;
174 }
175
176 return is_ip4 ?
177 format (s, "%U", format_ip4_address, &ip46->ip4) :
178 format (s, "%U", format_ip6_address, &ip46->ip6);
179}
180
Florin Coras697faea2018-06-27 17:10:49 -0700181/*
182 * VPPCOM Utility Functions
183 */
184
185static inline void
186vppcom_session_table_del_listener (u64 listener_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400187{
Florin Coras697faea2018-06-27 17:10:49 -0700188 listener_handle |= 1ULL << 63;
189 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
190}
191
192static inline int
193vppcom_wait_for_app_state_change (app_state_t app_state)
194{
195 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
196
197 while (clib_time_now (&vcm->clib_time) < timeout)
198 {
199 if (vcm->app_state == app_state)
200 return VPPCOM_OK;
201 }
202 VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (),
203 vppcom_app_state_str (app_state), app_state);
204 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state);
205
206 return VPPCOM_ETIMEDOUT;
207}
208
209static inline int
210vppcom_wait_for_session_state_change (u32 session_index,
211 session_state_t state,
212 f64 wait_for_time)
213{
214 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
215 vcl_session_t *volatile session;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800216 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400217
Florin Coras697faea2018-06-27 17:10:49 -0700218 do
Dave Wallace543852a2017-08-03 02:11:34 -0400219 {
Florin Coras697faea2018-06-27 17:10:49 -0700220 VCL_SESSION_LOCK ();
221 rv = vppcom_session_at_index (session_index, &session);
222 if (PREDICT_FALSE (rv))
223 {
224 VCL_SESSION_UNLOCK ();
225 return rv;
226 }
227 if (session->session_state & state)
228 {
229 VCL_SESSION_UNLOCK ();
230 return VPPCOM_OK;
231 }
232 if (session->session_state & STATE_FAILED)
233 {
234 VCL_SESSION_UNLOCK ();
235 return VPPCOM_ECONNREFUSED;
236 }
237
Dave Wallace7e607a72018-06-18 18:41:32 -0400238 VCL_SESSION_UNLOCK ();
Florin Corasdcf55ce2017-11-16 15:32:50 -0800239 }
Florin Coras697faea2018-06-27 17:10:49 -0700240 while (clib_time_now (&vcm->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800241
Florin Coras697faea2018-06-27 17:10:49 -0700242 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
243 vppcom_session_state_str (state));
244 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400245
Florin Coras697faea2018-06-27 17:10:49 -0700246 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500247}
248
Florin Coras697faea2018-06-27 17:10:49 -0700249static int
250vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400251{
Florin Coras697faea2018-06-27 17:10:49 -0700252 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400253
Florin Coras697faea2018-06-27 17:10:49 -0700254 if (vcm->app_state != STATE_APP_ENABLED)
255 {
256 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
257 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
258 if (PREDICT_FALSE (rv))
259 {
260 VDBG (0, "VCL<%d>: application session enable timed out! "
261 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
262 return rv;
263 }
264 }
265 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400266}
267
Florin Coras697faea2018-06-27 17:10:49 -0700268static int
269vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400270{
Florin Coras697faea2018-06-27 17:10:49 -0700271 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400272
Florin Coras697faea2018-06-27 17:10:49 -0700273 vppcom_app_send_attach ();
274 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
275 if (PREDICT_FALSE (rv))
276 {
277 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
278 getpid (), rv, vppcom_retval_str (rv));
279 return rv;
280 }
Dave Wallace543852a2017-08-03 02:11:34 -0400281
Florin Coras697faea2018-06-27 17:10:49 -0700282 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400283}
284
285static int
Dave Wallace543852a2017-08-03 02:11:34 -0400286vppcom_session_unbind (u32 session_index)
287{
Florin Coras7e12d942018-06-27 14:32:43 -0700288 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400289 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500290 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400291
Dave Wallace7e607a72018-06-18 18:41:32 -0400292 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500293
294 vpp_handle = session->vpp_handle;
295 vppcom_session_table_del_listener (vpp_handle);
296 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700297 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500298
Dave Wallace7e607a72018-06-18 18:41:32 -0400299 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400300
Florin Coras0d427d82018-06-27 03:24:07 -0700301 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
302 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
303 vppcom_session_state_str (STATE_DISCONNECT));
304 vcl_evt (VCL_EVT_UNBIND, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500305 vppcom_send_unbind_sock (vpp_handle);
306
307done:
308 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400309}
310
Florin Coras697faea2018-06-27 17:10:49 -0700311static int
Dave Wallace543852a2017-08-03 02:11:34 -0400312vppcom_session_disconnect (u32 session_index)
313{
Dave Wallace543852a2017-08-03 02:11:34 -0400314 int rv;
Florin Coras7e12d942018-06-27 14:32:43 -0700315 vcl_session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500316 u64 vpp_handle;
317 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -0400318
Dave Wallace7e607a72018-06-18 18:41:32 -0400319 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500320
321 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700322 state = session->session_state;
Dave Wallace7e607a72018-06-18 18:41:32 -0400323 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500324
Florin Coras0d427d82018-06-27 03:24:07 -0700325 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
326 vpp_handle, session_index, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400327
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800328 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400329 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500330 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500331 "Cannot disconnect a listen socket!",
332 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500333 rv = VPPCOM_EBADFD;
334 goto done;
335 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400336
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800337 /* The peer has already initiated the close,
338 * so send the disconnect session reply.
Dave Wallace4878cbe2017-11-21 03:45:09 -0500339 */
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800340 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500341 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800342 //XXX alagalah - Check and drain here?
343 vppcom_send_disconnect_session_reply (vpp_handle,
344 session_index, 0 /* rv */ );
Florin Coras0d427d82018-06-27 03:24:07 -0700345 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
346 "REPLY...", getpid (), vpp_handle, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400347 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500348
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800349 /* Otherwise, send a disconnect session msg...
Dave Wallace4878cbe2017-11-21 03:45:09 -0500350 */
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400351 else
Dave Wallace227867f2017-11-13 21:21:53 -0500352 {
Florin Coras0d427d82018-06-27 03:24:07 -0700353 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
354 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500355
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800356 vppcom_send_disconnect_session (vpp_handle, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -0500357 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400358
Dave Wallace4878cbe2017-11-21 03:45:09 -0500359done:
360 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400361}
362
Dave Wallace543852a2017-08-03 02:11:34 -0400363/*
364 * VPPCOM Public API functions
365 */
366int
367vppcom_app_create (char *app_name)
368{
Dave Wallace543852a2017-08-03 02:11:34 -0400369 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
370 u8 *heap;
371 mheap_t *h;
372 int rv;
373
374 if (!vcm->init)
375 {
Dave Wallace543852a2017-08-03 02:11:34 -0400376 vcm->init = 1;
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800377 clib_spinlock_init (&vcm->session_fifo_lockp);
Dave Wallace2e005bb2017-11-07 01:21:39 -0500378 clib_fifo_validate (vcm->client_session_index_fifo,
379 vcm->cfg.listen_queue_size);
Florin Coras697faea2018-06-27 17:10:49 -0700380 clib_spinlock_init (&vcm->sessions_lockp);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500381
Florin Coras697faea2018-06-27 17:10:49 -0700382 vppcom_cfg (&vcm->cfg);
Dave Wallace8af20542017-10-26 03:29:30 -0400383
Dave Wallace543852a2017-08-03 02:11:34 -0400384 vcm->main_cpu = os_get_thread_index ();
385 heap = clib_mem_get_per_cpu_heap ();
386 h = mheap_header (heap);
Dave Wallace543852a2017-08-03 02:11:34 -0400387 /* make the main heap thread-safe */
388 h->flags |= MHEAP_FLAG_THREAD_SAFE;
389
390 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
391
392 clib_time_init (&vcm->clib_time);
393 vppcom_init_error_string_table ();
Florin Corasa332c462018-01-31 06:52:17 -0800394 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
395 20 /* timeout in secs */ );
Dave Wallace543852a2017-08-03 02:11:34 -0400396 }
397
398 if (vcm->my_client_index == ~0)
399 {
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800400 /* API hookup and connect to VPP */
Dave Wallace048b1d62018-01-03 22:24:41 -0500401 vppcom_api_hookup ();
Florin Coras0d427d82018-06-27 03:24:07 -0700402 vcl_elog_init (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -0400403 vcm->app_state = STATE_APP_START;
404 rv = vppcom_connect_to_vpp (app_name);
405 if (rv)
406 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500407 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500408 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400409 return rv;
410 }
411
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800412 /* State event handling thread */
413
414 rv = vce_start_event_thread (&(vcm->event_thread), 20);
415
Florin Coras0d427d82018-06-27 03:24:07 -0700416 VDBG (0, "VCL<%d>: sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400417
Dave Wallace048b1d62018-01-03 22:24:41 -0500418 rv = vppcom_app_session_enable ();
Dave Wallace543852a2017-08-03 02:11:34 -0400419 if (rv)
420 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500421 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
422 "failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400423 return rv;
424 }
Dave Wallace543852a2017-08-03 02:11:34 -0400425
Florin Coras0d427d82018-06-27 03:24:07 -0700426 VDBG (0, "VCL<%d>: sending app attach", getpid ());
Dave Wallace048b1d62018-01-03 22:24:41 -0500427
428 rv = vppcom_app_attach ();
429 if (rv)
430 {
431 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
432 getpid ());
433 return rv;
434 }
435
Florin Coras0d427d82018-06-27 03:24:07 -0700436 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
437 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400438 }
Dave Wallace543852a2017-08-03 02:11:34 -0400439
440 return VPPCOM_OK;
441}
442
443void
444vppcom_app_destroy (void)
445{
Dave Wallace543852a2017-08-03 02:11:34 -0400446 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400447 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400448
449 if (vcm->my_client_index == ~0)
450 return;
451
Florin Coras0d427d82018-06-27 03:24:07 -0700452 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
453 getpid (), vcm->my_client_index, vcm->my_client_index);
454 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800455
Florin Coras697faea2018-06-27 17:10:49 -0700456 vppcom_app_send_detach ();
Dave Wallacede910062018-03-20 09:22:13 -0400457 orig_app_timeout = vcm->cfg.app_timeout;
458 vcm->cfg.app_timeout = 2.0;
Dave Wallace543852a2017-08-03 02:11:34 -0400459 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
Dave Wallacede910062018-03-20 09:22:13 -0400460 vcm->cfg.app_timeout = orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400461 if (PREDICT_FALSE (rv))
Florin Coras0d427d82018-06-27 03:24:07 -0700462 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
463 getpid (), rv, vppcom_retval_str (rv));
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800464
Florin Coras0d427d82018-06-27 03:24:07 -0700465 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -0400466 vl_client_disconnect_from_vlib ();
467 vcm->my_client_index = ~0;
468 vcm->app_state = STATE_APP_START;
469}
470
471int
Dave Wallacec04cbf12018-02-07 18:14:02 -0500472vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -0400473{
Florin Coras7e12d942018-06-27 14:32:43 -0700474 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -0400475 u32 session_index;
476
Dave Wallace7e607a72018-06-18 18:41:32 -0400477 VCL_SESSION_LOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400478 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -0400479 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -0400480 session_index = session - vcm->sessions;
481
Florin Coras7e12d942018-06-27 14:32:43 -0700482 session->session_type = proto;
483 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500484 session->vpp_handle = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -0400485
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800486 if (is_nonblocking)
487 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
488 else
489 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -0400490
Florin Coras7e12d942018-06-27 14:32:43 -0700491 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
492 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800493
Dave Wallace7e607a72018-06-18 18:41:32 -0400494 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800495
Florin Coras0d427d82018-06-27 03:24:07 -0700496 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800497
Dave Wallace543852a2017-08-03 02:11:34 -0400498 return (int) session_index;
499}
500
501int
502vppcom_session_close (uint32_t session_index)
503{
Florin Coras7e12d942018-06-27 14:32:43 -0700504 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400505 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400506 u8 is_vep;
507 u8 is_vep_session;
508 u32 next_sid;
509 u32 vep_idx;
Dave Wallaceee45d412017-11-24 21:44:06 -0500510 u64 vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500511 uword *p;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400512 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -0400513
Dave Wallace7e607a72018-06-18 18:41:32 -0400514 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400515 is_vep = session->is_vep;
516 is_vep_session = session->is_vep_session;
517 next_sid = session->vep.next_sid;
518 vep_idx = session->vep.vep_idx;
Florin Coras7e12d942018-06-27 14:32:43 -0700519 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -0500520 vpp_handle = session->vpp_handle;
Dave Wallace7e607a72018-06-18 18:41:32 -0400521 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400522
523 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -0500524 {
525 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -0500526 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
527 "closing epoll session...",
Dave Wallaceee45d412017-11-24 21:44:06 -0500528 getpid (), session_index, session_index);
529 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500530 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
531 "closing session...",
Dave Wallaceee45d412017-11-24 21:44:06 -0500532 getpid (), vpp_handle, session_index);
533 }
Dave Wallace543852a2017-08-03 02:11:34 -0400534
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400535 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -0400536 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400537 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -0400538 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400539 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700540 if (PREDICT_FALSE (rv < 0))
541 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
542 "vep_idx %u failed! rv %d (%s)",
543 getpid (), vpp_handle, next_sid, vep_idx,
544 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400545
Dave Wallace7e607a72018-06-18 18:41:32 -0400546 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400547 next_sid = session->vep.next_sid;
Dave Wallace7e607a72018-06-18 18:41:32 -0400548 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -0400549 }
550 }
551 else
552 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400553 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400554 {
Dave Wallacef7f809c2017-10-03 01:48:42 -0400555 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700556 if (rv < 0)
557 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
558 "vep_idx %u failed! rv %d (%s)",
559 getpid (), vpp_handle, session_index,
560 vep_idx, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400561 }
562
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800563 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400564 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800565 rv = vppcom_session_unbind (session_index);
566 if (PREDICT_FALSE (rv < 0))
Florin Coras0d427d82018-06-27 03:24:07 -0700567 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
568 "failed! rv %d (%s)",
569 getpid (), vpp_handle, session_index,
570 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400571 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500572
573 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
Dave Wallacef7f809c2017-10-03 01:48:42 -0400574 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500575 rv = vppcom_session_disconnect (session_index);
576 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -0500577 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500578 "session disconnect failed! rv %d (%s)",
579 getpid (), vpp_handle, session_index,
580 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400581 }
Dave Wallace19481612017-09-15 18:47:44 -0400582 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500583
Dave Wallace7e607a72018-06-18 18:41:32 -0400584 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -0500585 vpp_handle = session->vpp_handle;
586 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500587 {
Dave Wallaceee45d412017-11-24 21:44:06 -0500588 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500589 if (p)
Dave Wallaceee45d412017-11-24 21:44:06 -0500590 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500591 }
Dave Wallace543852a2017-08-03 02:11:34 -0400592 pool_put_index (vcm->sessions, session_index);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800593
Dave Wallace7e607a72018-06-18 18:41:32 -0400594 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500595
596 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -0500597 {
598 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -0500599 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -0500600 getpid (), session_index, session_index);
601 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500602 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -0500603 getpid (), vpp_handle, session_index);
604 }
Dave Wallacef7f809c2017-10-03 01:48:42 -0400605done:
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800606
Florin Coras0d427d82018-06-27 03:24:07 -0700607 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800608
Dave Wallace543852a2017-08-03 02:11:34 -0400609 return rv;
610}
611
612int
613vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
614{
Florin Coras7e12d942018-06-27 14:32:43 -0700615 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400616 int rv;
617
618 if (!ep || !ep->ip)
619 return VPPCOM_EINVAL;
620
Dave Wallace7e607a72018-06-18 18:41:32 -0400621 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -0400622
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400623 if (session->is_vep)
624 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400625 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500626 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
627 "bind to an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500628 rv = VPPCOM_EBADFD;
629 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400630 }
631
Florin Coras7e12d942018-06-27 14:32:43 -0700632 session->transport.is_ip4 = ep->is_ip4;
633 session->transport.lcl_ip = to_ip46 (ep->is_ip4 ? IP46_TYPE_IP4 :
634 IP46_TYPE_IP6, ep->ip);
635 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -0700636
Florin Coras0d427d82018-06-27 03:24:07 -0700637 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
638 "proto %s", getpid (), session_index,
Florin Coras7e12d942018-06-27 14:32:43 -0700639 session->transport.is_ip4 ? "IPv4" : "IPv6",
640 format_ip46_address, &session->transport.lcl_ip,
641 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
642 clib_net_to_host_u16 (session->transport.lcl_port),
643 session->session_type ? "UDP" : "TCP");
Florin Coras0d427d82018-06-27 03:24:07 -0700644 vcl_evt (VCL_EVT_BIND, session);
Dave Wallace7e607a72018-06-18 18:41:32 -0400645 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500646done:
647 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400648}
649
650int
Dave Wallace33e002b2017-09-06 01:20:02 -0400651vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -0400652{
Florin Coras7e12d942018-06-27 14:32:43 -0700653 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -0500654 u64 listen_vpp_handle;
655 int rv, retval;
Dave Wallace543852a2017-08-03 02:11:34 -0400656
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -0800657 if (q_len == 0 || q_len == ~0)
658 q_len = vcm->cfg.listen_queue_size;
659
Dave Wallace7e607a72018-06-18 18:41:32 -0400660 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -0400661
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400662 if (listen_session->is_vep)
663 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400664 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500665 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500666 "epoll session!", getpid (), listen_session_index);
667 rv = VPPCOM_EBADFD;
668 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400669 }
670
Dave Wallaceee45d412017-11-24 21:44:06 -0500671 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700672 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -0400673 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400674 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -0700675 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
676 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500677 rv = VPPCOM_OK;
678 goto done;
Dave Wallacee695cb42017-11-02 22:04:42 -0400679 }
680
Florin Coras0d427d82018-06-27 03:24:07 -0700681 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
682 "request...", getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400683
Dave Wallace4878cbe2017-11-21 03:45:09 -0500684 vppcom_send_bind_sock (listen_session, listen_session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -0400685 VCL_SESSION_UNLOCK ();
Dave Wallaceee45d412017-11-24 21:44:06 -0500686 retval =
Dave Wallace33e002b2017-09-06 01:20:02 -0400687 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
688 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -0400689
Dave Wallace7e607a72018-06-18 18:41:32 -0400690 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallaceee45d412017-11-24 21:44:06 -0500691 if (PREDICT_FALSE (retval))
692 {
Florin Coras0d427d82018-06-27 03:24:07 -0700693 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
694 "returning %d (%s)", getpid (), listen_session->vpp_handle,
695 listen_session_index, retval, vppcom_retval_str (retval));
Dave Wallace7e607a72018-06-18 18:41:32 -0400696 VCL_SESSION_UNLOCK ();
Dave Wallaceee45d412017-11-24 21:44:06 -0500697 rv = retval;
698 goto done;
699 }
700
Dave Wallace7e607a72018-06-18 18:41:32 -0400701 VCL_ACCEPT_FIFO_LOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400702 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallace7e607a72018-06-18 18:41:32 -0400703 VCL_ACCEPT_FIFO_UNLOCK ();
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800704
Dave Wallace7e607a72018-06-18 18:41:32 -0400705 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -0800706
707done:
708 return rv;
709}
710
711int
Florin Coras7e12d942018-06-27 14:32:43 -0700712validate_args_session_accept_ (vcl_session_t * listen_session)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800713{
714 u32 listen_session_index = listen_session - vcm->sessions;
715
716 /* Input validation - expects spinlock on sessions_lockp */
717 if (listen_session->is_vep)
718 {
719 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
720 "epoll session!", getpid (), listen_session_index);
721 return VPPCOM_EBADFD;
722 }
723
Florin Coras7e12d942018-06-27 14:32:43 -0700724 if (listen_session->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800725 {
726 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
727 "not in listen state! state 0x%x (%s)", getpid (),
728 listen_session->vpp_handle, listen_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -0700729 listen_session->session_state,
730 vppcom_session_state_str (listen_session->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800731 return VPPCOM_EBADFD;
732 }
733 return VPPCOM_OK;
734}
735
736int
Dave Wallace543852a2017-08-03 02:11:34 -0400737vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -0500738 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -0400739{
Florin Coras7e12d942018-06-27 14:32:43 -0700740 vcl_session_t *listen_session = 0;
741 vcl_session_t *client_session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800742 u32 client_session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -0400743 int rv;
Dave Wallaceee45d412017-11-24 21:44:06 -0500744 u64 listen_vpp_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800745 vce_event_handler_reg_t *reg;
746 vce_event_t *ev;
747 vce_event_connect_request_t *result;
748 struct timespec ts;
749 struct timeval tv;
750 int millisecond_timeout = 1;
751 int hours_timeout = 20 * 60 * 60;
Dave Wallace543852a2017-08-03 02:11:34 -0400752
Dave Wallace7e607a72018-06-18 18:41:32 -0400753 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800754 listen_vpp_handle = listen_session->vpp_handle; // For debugging
Dave Wallace543852a2017-08-03 02:11:34 -0400755
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800756 rv = validate_args_session_accept_ (listen_session);
757 if (rv)
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400758 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400759 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500760 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400761 }
762
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800763 /* Using an aggressive timer of 1ms and a generous timer of
764 * 20 hours, we can implement a blocking and non-blocking listener
765 * as both event and time driven */
766 gettimeofday (&tv, NULL);
767 ts.tv_nsec = (tv.tv_usec * 1000) + (1000 * millisecond_timeout);
768 ts.tv_sec = tv.tv_sec;
769
770 /* Predict that the Listener is blocking more often than not */
771 if (PREDICT_TRUE (!VCL_SESS_ATTR_TEST (listen_session->attr,
772 VCL_SESS_ATTR_NONBLOCK)))
773 ts.tv_sec += hours_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400774
Dave Wallace7e607a72018-06-18 18:41:32 -0400775 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400776
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800777 /* Register handler for connect_request event on listen_session_index */
778 vce_event_key_t evk;
779 evk.session_index = listen_session_index;
780 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
781 reg = vce_register_handler (&vcm->event_thread, &evk,
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -0800782 vce_connect_request_handler_fn, 0);
Dave Wallace7e607a72018-06-18 18:41:32 -0400783 VCL_EVENTS_LOCK ();
Keith Burns (alagalah)7cf80e02018-03-08 16:46:25 -0800784 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800785 pthread_mutex_lock (&reg->handler_lock);
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800786 while (!ev)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800787 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400788 VCL_EVENTS_UNLOCK ();
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700789 rv = pthread_cond_timedwait (&reg->handler_cond,
790 &reg->handler_lock, &ts);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800791 if (rv == ETIMEDOUT)
792 {
793 rv = VPPCOM_EAGAIN;
794 goto cleanup;
795 }
Dave Wallace7e607a72018-06-18 18:41:32 -0400796 VCL_EVENTS_LOCK ();
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800797 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800798 }
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700799 result = vce_get_event_data (ev, sizeof (*result));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800800 client_session_index = result->accepted_session_index;
Dave Wallace7e607a72018-06-18 18:41:32 -0400801 VCL_EVENTS_UNLOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800802
803 /* Remove from the FIFO used to service epoll */
Dave Wallace7e607a72018-06-18 18:41:32 -0400804 VCL_ACCEPT_FIFO_LOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800805 if (clib_fifo_elts (vcm->client_session_index_fifo))
806 {
807 u32 tmp_client_session_index;
808 clib_fifo_sub1 (vcm->client_session_index_fifo,
809 tmp_client_session_index);
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800810 /* It wasn't ours... put it back ... */
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800811 if (tmp_client_session_index != client_session_index)
812 clib_fifo_add1 (vcm->client_session_index_fifo,
813 tmp_client_session_index);
814 }
Dave Wallace7e607a72018-06-18 18:41:32 -0400815 VCL_ACCEPT_FIFO_UNLOCK ();
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800816
Dave Wallace7e607a72018-06-18 18:41:32 -0400817 VCL_SESSION_LOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800818
Dave Wallace33e002b2017-09-06 01:20:02 -0400819 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallaceee45d412017-11-24 21:44:06 -0500820 if (PREDICT_FALSE (rv))
821 {
822 rv = VPPCOM_ECONNABORTED;
Dave Wallace048b1d62018-01-03 22:24:41 -0500823 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
Dave Wallaceee45d412017-11-24 21:44:06 -0500824 "lookup failed! returning %d (%s)", getpid (),
825 listen_vpp_handle, listen_session_index,
826 client_session_index, rv, vppcom_retval_str (rv));
Dave Wallacee5356442018-03-19 10:38:00 -0400827 goto cleanup;
Dave Wallaceee45d412017-11-24 21:44:06 -0500828 }
Dave Wallace543852a2017-08-03 02:11:34 -0400829
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800830 if (flags & O_NONBLOCK)
831 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
832 else
833 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
834
Florin Coras0d427d82018-06-27 03:24:07 -0700835 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
836 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
837 getpid (), listen_vpp_handle, listen_session_index,
838 client_session->vpp_handle, client_session_index,
839 flags, VCL_SESS_ATTR_TEST (client_session->attr,
840 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -0400841
Dave Wallace048b1d62018-01-03 22:24:41 -0500842 if (ep)
843 {
Florin Coras7e12d942018-06-27 14:32:43 -0700844 ep->is_ip4 = client_session->transport.is_ip4;
845 ep->port = client_session->transport.rmt_port;
846 if (client_session->transport.is_ip4)
847 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
Dave Wallace048b1d62018-01-03 22:24:41 -0500848 sizeof (ip4_address_t));
849 else
Florin Coras7e12d942018-06-27 14:32:43 -0700850 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
Dave Wallace048b1d62018-01-03 22:24:41 -0500851 sizeof (ip6_address_t));
852 }
Dave Wallace60caa062017-11-10 17:07:13 -0500853
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800854 vppcom_send_accept_session_reply (client_session->vpp_handle,
855 client_session->client_context,
856 0 /* retval OK */ );
Dave Wallace60caa062017-11-10 17:07:13 -0500857
Florin Coras0d427d82018-06-27 03:24:07 -0700858 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx,"
859 " sid %u connection from peer %s address %U port %u to local %s address"
860 " %U port %u",
861 getpid (), listen_vpp_handle,
862 listen_session_index, client_session->vpp_handle,
863 client_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -0700864 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
865 format_ip46_address, &client_session->transport.rmt_ip,
866 client_session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -0700867 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -0700868 clib_net_to_host_u16 (client_session->transport.rmt_port),
869 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
870 format_ip46_address, &client_session->transport.lcl_ip,
871 client_session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -0700872 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -0700873 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -0700874 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
875 client_session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -0400876 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800877
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800878 rv = (int) client_session_index;
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700879 vce_clear_event (&vcm->event_thread, reg->ev_idx);
880 if (vcm->session_io_thread.io_sessions_lockp)
881 {
882 /* Throw this new accepted session index into the rx poll thread pool */
Dave Wallace7e607a72018-06-18 18:41:32 -0400883 VCL_IO_SESSIONS_LOCK ();
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700884 u32 *active_session_index;
885 pool_get (vcm->session_io_thread.active_session_indexes,
886 active_session_index);
887 *active_session_index = client_session_index;
Dave Wallace7e607a72018-06-18 18:41:32 -0400888 VCL_IO_SESSIONS_UNLOCK ();
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700889 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800890cleanup:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800891 vce_unregister_handler (&vcm->event_thread, reg);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800892 pthread_mutex_unlock (&reg->handler_lock);
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800893
Dave Wallace4878cbe2017-11-21 03:45:09 -0500894done:
895 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400896}
897
898int
899vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
900{
Florin Coras7e12d942018-06-27 14:32:43 -0700901 vcl_session_t *session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800902 u64 vpp_handle = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -0500903 int rv, retval = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400904
Dave Wallace7e607a72018-06-18 18:41:32 -0400905 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500906
907 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -0400908 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400909 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -0500910 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
911 "connect on an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500912 rv = VPPCOM_EBADFD;
913 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -0400914 }
915
Florin Coras7e12d942018-06-27 14:32:43 -0700916 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -0400917 {
Florin Coras0d427d82018-06-27 03:24:07 -0700918 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
919 "connected to %s %U port %d proto %s, state 0x%x (%s)",
920 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -0700921 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -0700922 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -0700923 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -0700924 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -0700925 clib_net_to_host_u16 (session->transport.rmt_port),
926 session->session_type ? "UDP" : "TCP", session->session_state,
927 vppcom_session_state_str (session->session_state));
Dave Wallace4878cbe2017-11-21 03:45:09 -0500928
Dave Wallace7e607a72018-06-18 18:41:32 -0400929 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500930 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -0400931 }
932
Florin Coras7e12d942018-06-27 14:32:43 -0700933 session->transport.is_ip4 = server_ep->is_ip4;
934 if (session->transport.is_ip4)
935 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -0400936 sizeof (ip4_address_t));
937 else
Florin Coras7e12d942018-06-27 14:32:43 -0700938 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -0400939 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -0700940 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -0400941
Florin Coras0d427d82018-06-27 03:24:07 -0700942 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
943 "port %d proto %s",
944 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -0700945 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -0700946 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -0700947 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -0700948 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -0700949 clib_net_to_host_u16 (session->transport.rmt_port),
950 session->session_type ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -0400951
952 vppcom_send_connect_sock (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -0400953 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500954
Dave Wallaceee45d412017-11-24 21:44:06 -0500955 retval =
956 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
957 vcm->cfg.session_timeout);
958
Dave Wallace7e607a72018-06-18 18:41:32 -0400959 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -0500960 vpp_handle = session->vpp_handle;
Dave Wallace7e607a72018-06-18 18:41:32 -0400961 VCL_SESSION_UNLOCK ();
Dave Wallace7876d392017-10-19 03:53:57 -0400962
Dave Wallace4878cbe2017-11-21 03:45:09 -0500963done:
Dave Wallaceee45d412017-11-24 21:44:06 -0500964 if (PREDICT_FALSE (retval))
965 {
966 rv = retval;
967 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800968 {
969 if (session)
Florin Coras0d427d82018-06-27 03:24:07 -0700970 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
971 "failed! returning %d (%s)", getpid (), vpp_handle,
972 session_index, rv, vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800973 else
974 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
975 "returning %d (%s)", getpid (),
976 session_index, rv, vppcom_retval_str (rv));
977 }
Dave Wallaceee45d412017-11-24 21:44:06 -0500978 }
Florin Coras0d427d82018-06-27 03:24:07 -0700979 else
980 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
981 getpid (), vpp_handle, session_index);
Dave Wallaceee45d412017-11-24 21:44:06 -0500982
Dave Wallace4878cbe2017-11-21 03:45:09 -0500983 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400984}
985
Steven58f464e2017-10-25 12:33:12 -0700986static inline int
987vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
988 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -0400989{
Florin Coras7e12d942018-06-27 14:32:43 -0700990 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400991 svm_fifo_t *rx_fifo;
992 int n_read = 0;
993 int rv;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800994 int is_nonblocking;
995
996 u64 vpp_handle;
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400997 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500998 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -0400999
1000 ASSERT (buf);
1001
Dave Wallace7e607a72018-06-18 18:41:32 -04001002 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001003
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001004 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
1005 rx_fifo = session->rx_fifo;
Florin Coras7e12d942018-06-27 14:32:43 -07001006 state = session->session_state;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001007 vpp_handle = session->vpp_handle;
1008
Dave Wallace4878cbe2017-11-21 03:45:09 -05001009 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001010 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001011 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001012 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
1013 "read from an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001014 rv = VPPCOM_EBADFD;
1015 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001016 }
1017
Dave Wallace4878cbe2017-11-21 03:45:09 -05001018 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001019 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001020 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001021 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001022
Florin Coras0d427d82018-06-27 03:24:07 -07001023 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1024 "state 0x%x (%s), returning %d (%s)",
1025 getpid (), vpp_handle, session_index, state,
1026 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001027 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001028 }
1029
Dave Wallace7e607a72018-06-18 18:41:32 -04001030 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04001031
1032 do
1033 {
Steven58f464e2017-10-25 12:33:12 -07001034 if (peek)
1035 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
1036 else
1037 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001038 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001039 while (!is_nonblocking && (n_read <= 0));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001040
Dave Wallace4878cbe2017-11-21 03:45:09 -05001041 if (n_read <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001042 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001043 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001044
1045 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
1046 (EPOLLET | EPOLLIN));
1047 if (poll_et)
1048 session->vep.et_mask |= EPOLLIN;
1049
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001050 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001051 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001052 rv = VPPCOM_ECONNRESET;
1053
Florin Coras0d427d82018-06-27 03:24:07 -07001054 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
1055 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
1056 "returning %d (%s)",
1057 getpid (), session->vpp_handle, session_index,
1058 state, vppcom_session_state_str (state),
1059 STATE_DISCONNECT,
1060 vppcom_session_state_str (STATE_DISCONNECT), rv,
1061 vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001062
Florin Coras7e12d942018-06-27 14:32:43 -07001063 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001064 }
1065 else
1066 rv = VPPCOM_EAGAIN;
1067
Dave Wallace7e607a72018-06-18 18:41:32 -04001068 VCL_SESSION_UNLOCK ();
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001069 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001070 else
1071 rv = n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001072
Dave Wallace4878cbe2017-11-21 03:45:09 -05001073 if (VPPCOM_DEBUG > 2)
1074 {
1075 if (rv > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001076 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001077 "from (%p)", getpid (), vpp_handle,
1078 session_index, n_read, rx_fifo);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001079 else
Dave Wallace048b1d62018-01-03 22:24:41 -05001080 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
Dave Wallaceee45d412017-11-24 21:44:06 -05001081 "returning %d (%s)", getpid (), vpp_handle,
1082 session_index, rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001083 }
1084done:
1085 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001086}
1087
Steven58f464e2017-10-25 12:33:12 -07001088int
Dave Wallace048b1d62018-01-03 22:24:41 -05001089vppcom_session_read (uint32_t session_index, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001090{
1091 return (vppcom_session_read_internal (session_index, buf, n, 0));
1092}
1093
1094static int
1095vppcom_session_peek (uint32_t session_index, void *buf, int n)
1096{
1097 return (vppcom_session_read_internal (session_index, buf, n, 1));
1098}
1099
Dave Wallace543852a2017-08-03 02:11:34 -04001100static inline int
Florin Coras7e12d942018-06-27 14:32:43 -07001101vppcom_session_read_ready (vcl_session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001102{
Dave Wallace543852a2017-08-03 02:11:34 -04001103 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05001104 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001105 int rv;
Florin Coras7e12d942018-06-27 14:32:43 -07001106 session_state_t state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05001107 u64 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001108
1109 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001110 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001111 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001112 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001113 "epoll session!", getpid (), session_index);
1114 rv = VPPCOM_EBADFD;
1115 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001116 }
Dave Wallace33e002b2017-09-06 01:20:02 -04001117
Florin Coras7e12d942018-06-27 14:32:43 -07001118 if (session->session_state & STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001119 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001120 VCL_ACCEPT_FIFO_LOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001121 ready = clib_fifo_elts (vcm->client_session_index_fifo);
Dave Wallace7e607a72018-06-18 18:41:32 -04001122 VCL_ACCEPT_FIFO_UNLOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001123 }
Dave Wallace543852a2017-08-03 02:11:34 -04001124 else
1125 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001126 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
1127 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001128 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
Dave Wallace4878cbe2017-11-21 03:45:09 -05001129 VPPCOM_ENOTCONN);
1130
Florin Coras0d427d82018-06-27 03:24:07 -07001131 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1132 " state 0x%x (%s), returning %d (%s)",
1133 getpid (), vpp_handle, session_index,
1134 state, vppcom_session_state_str (state),
1135 rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001136 goto done;
1137 }
1138
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001139 ready = svm_fifo_max_dequeue (session->rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04001140 }
1141
Dave Wallace4878cbe2017-11-21 03:45:09 -05001142 if (ready == 0)
Dave Wallace60caa062017-11-10 17:07:13 -05001143 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001144 poll_et =
1145 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
1146 if (poll_et)
1147 session->vep.et_mask |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05001148
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001149 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001150 {
1151 rv = VPPCOM_ECONNRESET;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001152
Florin Coras0d427d82018-06-27 03:24:07 -07001153 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
1154 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
1155 "returning %d (%s)",
1156 getpid (), session_index, vpp_handle,
1157 state, vppcom_session_state_str (state),
1158 STATE_DISCONNECT,
1159 vppcom_session_state_str (STATE_DISCONNECT), rv,
1160 vppcom_retval_str (rv));
Florin Coras7e12d942018-06-27 14:32:43 -07001161 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001162 goto done;
1163 }
1164 }
1165 rv = ready;
Dave Wallace16cb4082017-11-29 03:24:06 -05001166
1167 if (vcm->app_event_queue->cursize &&
1168 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
1169 {
1170 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
1171 session_fifo_event_t e;
1172
1173 for (i = 0; i < n_to_dequeue; i++)
Florin Corase86a8ed2018-01-05 03:20:25 -08001174 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
Dave Wallace16cb4082017-11-29 03:24:06 -05001175
1176 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
1177 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001178done:
1179 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001180}
1181
1182int
Dave Wallace048b1d62018-01-03 22:24:41 -05001183vppcom_session_write (uint32_t session_index, void *buf, size_t n)
Dave Wallace543852a2017-08-03 02:11:34 -04001184{
Florin Coras7e12d942018-06-27 14:32:43 -07001185 vcl_session_t *session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001186 svm_fifo_t *tx_fifo = 0;
Florin Corase86a8ed2018-01-05 03:20:25 -08001187 svm_queue_t *q;
Dave Wallace543852a2017-08-03 02:11:34 -04001188 session_fifo_event_t evt;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001189 session_state_t state;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001190 int rv, n_write, is_nonblocking;
1191 u32 poll_et;
Dave Wallaceee45d412017-11-24 21:44:06 -05001192 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001193
1194 ASSERT (buf);
1195
Dave Wallace7e607a72018-06-18 18:41:32 -04001196 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001197
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001198 tx_fifo = session->tx_fifo;
1199 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
1200 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001201 state = session->session_state;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001202
Dave Wallace4878cbe2017-11-21 03:45:09 -05001203 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001204 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001205 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05001206 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001207 "cannot write to an epoll session!",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001208 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001209
1210 rv = VPPCOM_EBADFD;
1211 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04001212 }
1213
Florin Coras7e12d942018-06-27 14:32:43 -07001214 if (!(session->session_state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001215 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001216 rv =
Florin Coras7e12d942018-06-27 14:32:43 -07001217 ((session->session_state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001218 VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001219
Dave Wallace7e607a72018-06-18 18:41:32 -04001220 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07001221 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
1222 "state 0x%x (%s)",
1223 getpid (), vpp_handle, session_index,
1224 state, vppcom_session_state_str (state));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001225 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001226 }
1227
Dave Wallace7e607a72018-06-18 18:41:32 -04001228 VCL_SESSION_UNLOCK ();
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001229
Dave Wallace543852a2017-08-03 02:11:34 -04001230 do
1231 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001232 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
Dave Wallace543852a2017-08-03 02:11:34 -04001233 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001234 while (!is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04001235
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001236 /* If event wasn't set, add one
1237 *
1238 * To reduce context switching, can check if an
1239 * event is already there for this event_key, but for now
1240 * this will suffice. */
1241
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001242 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001243 {
Dave Wallace543852a2017-08-03 02:11:34 -04001244 /* Fabricate TX event, send to vpp */
1245 evt.fifo = tx_fifo;
1246 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04001247
Dave Wallace7e607a72018-06-18 18:41:32 -04001248 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Florin Coras7e12d942018-06-27 14:32:43 -07001249 q = session->vpp_evt_q;
Dave Wallace543852a2017-08-03 02:11:34 -04001250 ASSERT (q);
Florin Corase86a8ed2018-01-05 03:20:25 -08001251 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
Dave Wallace7e607a72018-06-18 18:41:32 -04001252 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07001253 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: added FIFO_EVENT_APP_TX "
1254 "to vpp_event_q %p, n_write %d", getpid (),
1255 vpp_handle, session_index, q, n_write);
Dave Wallace543852a2017-08-03 02:11:34 -04001256 }
1257
Dave Wallace4878cbe2017-11-21 03:45:09 -05001258 if (n_write <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001259 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001260 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001261
1262 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
1263 (EPOLLET | EPOLLOUT));
1264 if (poll_et)
1265 session->vep.et_mask |= EPOLLOUT;
1266
Florin Coras7e12d942018-06-27 14:32:43 -07001267 if (session->session_state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001268 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001269 rv = VPPCOM_ECONNRESET;
1270
Florin Coras0d427d82018-06-27 03:24:07 -07001271 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
1272 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
1273 "returning %d (%s)",
1274 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001275 session->session_state,
1276 vppcom_session_state_str (session->session_state),
Florin Coras0d427d82018-06-27 03:24:07 -07001277 STATE_DISCONNECT,
1278 vppcom_session_state_str (STATE_DISCONNECT), rv,
1279 vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001280
Florin Coras7e12d942018-06-27 14:32:43 -07001281 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001282 }
1283 else
1284 rv = VPPCOM_EAGAIN;
1285
Dave Wallace7e607a72018-06-18 18:41:32 -04001286 VCL_SESSION_UNLOCK ();
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001287 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001288 else
1289 rv = n_write;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001290
Dave Wallace543852a2017-08-03 02:11:34 -04001291 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001292 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001293 if (n_write <= 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001294 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001295 "FIFO-FULL (%p)", getpid (), vpp_handle,
1296 session_index, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001297 else
Dave Wallace048b1d62018-01-03 22:24:41 -05001298 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001299 "wrote %d bytes tx-fifo: (%p)", getpid (),
1300 vpp_handle, session_index, n_write, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001301 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001302done:
1303 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001304}
1305
1306static inline int
Florin Coras7e12d942018-06-27 14:32:43 -07001307vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001308{
Dave Wallacef7f809c2017-10-03 01:48:42 -04001309 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05001310 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001311 int rv;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001312
1313 ASSERT (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001314
1315 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001316 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001317 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001318 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001319 "cannot write to an epoll session!",
1320 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001321 rv = VPPCOM_EBADFD;
1322 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001323 }
1324
Florin Coras7e12d942018-06-27 14:32:43 -07001325 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
Dave Wallace33e002b2017-09-06 01:20:02 -04001326 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001327 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001328 "cannot write to a listen session!",
1329 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001330 rv = VPPCOM_EBADFD;
1331 goto done;
1332 }
1333
Florin Coras7e12d942018-06-27 14:32:43 -07001334 if (!(session->session_state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001335 {
Florin Coras7e12d942018-06-27 14:32:43 -07001336 session_state_t state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001337
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001338 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001339
Dave Wallace048b1d62018-01-03 22:24:41 -05001340 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001341 "session is not open! state 0x%x (%s), "
Dave Wallaceee45d412017-11-24 21:44:06 -05001342 "returning %d (%s)", getpid (), session->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001343 session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05001344 state, vppcom_session_state_str (state),
1345 rv, vppcom_retval_str (rv));
1346 goto done;
Dave Wallace33e002b2017-09-06 01:20:02 -04001347 }
1348
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001349 ready = svm_fifo_max_enqueue (session->tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04001350
Florin Coras0d427d82018-06-27 03:24:07 -07001351 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
1352 getpid (), session->vpp_handle, session_index, session->tx_fifo,
1353 ready);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001354
Dave Wallace4878cbe2017-11-21 03:45:09 -05001355 if (ready == 0)
1356 {
1357 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
1358 (EPOLLET | EPOLLOUT));
1359 if (poll_et)
1360 session->vep.et_mask |= EPOLLOUT;
1361
Florin Coras7e12d942018-06-27 14:32:43 -07001362 if (session->session_state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001363 {
1364 rv = VPPCOM_ECONNRESET;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001365
Florin Coras0d427d82018-06-27 03:24:07 -07001366 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
1367 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
1368 "returning %d (%s)", getpid (),
1369 session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001370 session->session_state,
1371 vppcom_session_state_str (session->session_state),
Florin Coras0d427d82018-06-27 03:24:07 -07001372 STATE_DISCONNECT,
1373 vppcom_session_state_str (STATE_DISCONNECT), rv,
1374 vppcom_retval_str (rv));
Florin Coras7e12d942018-06-27 14:32:43 -07001375 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001376 goto done;
1377 }
1378 }
1379 rv = ready;
1380done:
1381 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001382}
1383
1384int
1385vppcom_select (unsigned long n_bits, unsigned long *read_map,
1386 unsigned long *write_map, unsigned long *except_map,
1387 double time_to_wait)
1388{
Dave Wallace543852a2017-08-03 02:11:34 -04001389 u32 session_index;
Florin Coras7e12d942018-06-27 14:32:43 -07001390 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001391 int rv, bits_set = 0;
1392 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
1393 u32 minbits = clib_max (n_bits, BITS (uword));
1394
1395 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1396
Dave Wallace7876d392017-10-19 03:53:57 -04001397 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001398 {
1399 clib_bitmap_validate (vcm->rd_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05001400 clib_memcpy (vcm->rd_bitmap, read_map,
1401 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
1402 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001403 }
Dave Wallace7876d392017-10-19 03:53:57 -04001404 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001405 {
1406 clib_bitmap_validate (vcm->wr_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05001407 clib_memcpy (vcm->wr_bitmap, write_map,
1408 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
1409 memset (write_map, 0,
1410 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001411 }
Dave Wallace7876d392017-10-19 03:53:57 -04001412 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001413 {
1414 clib_bitmap_validate (vcm->ex_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05001415 clib_memcpy (vcm->ex_bitmap, except_map,
1416 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
1417 memset (except_map, 0,
1418 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001419 }
1420
1421 do
1422 {
1423 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04001424 if (n_bits)
1425 {
1426 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001427 {
Dave Wallacee22aa742017-10-20 12:30:38 -04001428 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
1429 ({
Dave Wallace7e607a72018-06-18 18:41:32 -04001430 VCL_SESSION_LOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04001431 rv = vppcom_session_at_index (session_index, &session);
1432 if (rv < 0)
1433 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001434 VCL_SESSION_UNLOCK();
Florin Coras0d427d82018-06-27 03:24:07 -07001435 VDBG (1, "VCL<%d>: session %d specified in read_map is"
1436 " closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04001437 session_index);
1438 bits_set = VPPCOM_EBADFD;
1439 goto select_done;
1440 }
Florin Coras7e12d942018-06-27 14:32:43 -07001441 if (session->session_state & STATE_LISTEN)
Dave Wallace8d73e852018-03-08 16:39:28 -05001442 {
1443 vce_event_handler_reg_t *reg = 0;
1444 vce_event_key_t evk;
Dave Wallacee22aa742017-10-20 12:30:38 -04001445
Dave Wallace8d73e852018-03-08 16:39:28 -05001446 /* Check if handler already registered for this
1447 * event.
1448 * If not, register handler for connect_request event
1449 * on listen_session_index
1450 */
1451 evk.session_index = session_index;
1452 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1453 reg = vce_get_event_handler (&vcm->event_thread, &evk);
1454 if (!reg)
1455 reg = vce_register_handler (&vcm->event_thread, &evk,
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001456 vce_poll_wait_connect_request_handler_fn,
1457 0 /* No callback args */);
Dave Wallace8d73e852018-03-08 16:39:28 -05001458 rv = vppcom_session_read_ready (session, session_index);
1459 if (rv > 0)
1460 {
1461 vce_unregister_handler (&vcm->event_thread, reg);
1462 }
1463 }
1464 else
1465 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001466 VCL_SESSION_UNLOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04001467 if (except_map && vcm->ex_bitmap &&
1468 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
1469 (rv < 0))
1470 {
Dave Wallacee22aa742017-10-20 12:30:38 -04001471 clib_bitmap_set_no_check (except_map, session_index, 1);
1472 bits_set++;
1473 }
1474 else if (rv > 0)
1475 {
Dave Wallacee22aa742017-10-20 12:30:38 -04001476 clib_bitmap_set_no_check (read_map, session_index, 1);
1477 bits_set++;
1478 }
1479 }));
Dave Wallace543852a2017-08-03 02:11:34 -04001480 }
1481
Dave Wallacee22aa742017-10-20 12:30:38 -04001482 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001483 {
Dave Wallacee22aa742017-10-20 12:30:38 -04001484 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
1485 ({
Dave Wallace7e607a72018-06-18 18:41:32 -04001486 VCL_SESSION_LOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04001487 rv = vppcom_session_at_index (session_index, &session);
1488 if (rv < 0)
1489 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001490 VCL_SESSION_UNLOCK();
Florin Coras0d427d82018-06-27 03:24:07 -07001491 VDBG (0, "VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001492 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04001493 session_index);
1494 bits_set = VPPCOM_EBADFD;
1495 goto select_done;
1496 }
Dave Wallace543852a2017-08-03 02:11:34 -04001497
Dave Wallacee22aa742017-10-20 12:30:38 -04001498 rv = vppcom_session_write_ready (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001499 VCL_SESSION_UNLOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04001500 if (write_map && (rv > 0))
1501 {
Dave Wallacee22aa742017-10-20 12:30:38 -04001502 clib_bitmap_set_no_check (write_map, session_index, 1);
1503 bits_set++;
1504 }
1505 }));
Dave Wallace543852a2017-08-03 02:11:34 -04001506 }
1507
Dave Wallacee22aa742017-10-20 12:30:38 -04001508 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001509 {
Dave Wallacee22aa742017-10-20 12:30:38 -04001510 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
1511 ({
Dave Wallace7e607a72018-06-18 18:41:32 -04001512 VCL_SESSION_LOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04001513 rv = vppcom_session_at_index (session_index, &session);
1514 if (rv < 0)
1515 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001516 VCL_SESSION_UNLOCK();
Florin Coras0d427d82018-06-27 03:24:07 -07001517 VDBG (1, "VCL<%d>: session %d specified in except_map "
1518 "is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04001519 session_index);
1520 bits_set = VPPCOM_EBADFD;
1521 goto select_done;
1522 }
Dave Wallace543852a2017-08-03 02:11:34 -04001523
Dave Wallacee22aa742017-10-20 12:30:38 -04001524 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001525 VCL_SESSION_UNLOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04001526 if (rv < 0)
1527 {
Dave Wallacee22aa742017-10-20 12:30:38 -04001528 clib_bitmap_set_no_check (except_map, session_index, 1);
1529 bits_set++;
1530 }
1531 }));
Dave Wallace543852a2017-08-03 02:11:34 -04001532 }
Dave Wallacee22aa742017-10-20 12:30:38 -04001533 }
Dave Wallace543852a2017-08-03 02:11:34 -04001534 /* *INDENT-ON* */
1535 }
Dave Wallace048b1d62018-01-03 22:24:41 -05001536 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
Dave Wallace543852a2017-08-03 02:11:34 -04001537
1538select_done:
1539 return (bits_set);
1540}
1541
Dave Wallacef7f809c2017-10-03 01:48:42 -04001542static inline void
1543vep_verify_epoll_chain (u32 vep_idx)
1544{
Florin Coras7e12d942018-06-27 14:32:43 -07001545 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001546 vppcom_epoll_t *vep;
1547 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05001548 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001549
Dave Wallace498b3a52017-11-09 13:00:34 -05001550 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001551 return;
1552
1553 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1554 rv = vppcom_session_at_index (vep_idx, &session);
1555 if (PREDICT_FALSE (rv))
1556 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001557 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
1558 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001559 goto done;
1560 }
1561 if (PREDICT_FALSE (!session->is_vep))
1562 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001563 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
1564 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001565 goto done;
1566 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001567 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05001568 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05001569 "{\n"
1570 " is_vep = %u\n"
1571 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05001572 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05001573 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05001574 "}\n", getpid (), vep_idx,
1575 session->is_vep, session->is_vep_session,
1576 vep->next_sid, vep->next_sid,
Dave Wallace498b3a52017-11-09 13:00:34 -05001577 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001578
1579 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001580 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001581 rv = vppcom_session_at_index (sid, &session);
1582 if (PREDICT_FALSE (rv))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001583 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001584 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001585 goto done;
1586 }
1587 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05001588 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
1589 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001590 else if (PREDICT_FALSE (!session->is_vep_session))
1591 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001592 clib_warning ("VCL<%d>: ERROR: session (%u) "
1593 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001594 goto done;
1595 }
1596 vep = &session->vep;
1597 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05001598 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001599 "vep_idx (%u)!", getpid (),
1600 sid, session->vep.vep_idx, vep_idx);
1601 if (session->is_vep_session)
1602 {
1603 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
1604 "{\n"
1605 " next_sid = 0x%x (%u)\n"
1606 " prev_sid = 0x%x (%u)\n"
1607 " vep_idx = 0x%x (%u)\n"
1608 " ev.events = 0x%x\n"
1609 " ev.data.u64 = 0x%llx\n"
1610 " et_mask = 0x%x\n"
1611 "}\n",
1612 vep_idx, sid, sid,
1613 vep->next_sid, vep->next_sid,
1614 vep->prev_sid, vep->prev_sid,
1615 vep->vep_idx, vep->vep_idx,
1616 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001617 }
1618 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04001619
1620done:
Dave Wallace048b1d62018-01-03 22:24:41 -05001621 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
1622 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001623}
1624
1625int
1626vppcom_epoll_create (void)
1627{
Florin Coras7e12d942018-06-27 14:32:43 -07001628 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001629 u32 vep_idx;
1630
Dave Wallace7e607a72018-06-18 18:41:32 -04001631 VCL_SESSION_LOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04001632 pool_get (vcm->sessions, vep_session);
1633 memset (vep_session, 0, sizeof (*vep_session));
1634 vep_idx = vep_session - vcm->sessions;
1635
1636 vep_session->is_vep = 1;
1637 vep_session->vep.vep_idx = ~0;
1638 vep_session->vep.next_sid = ~0;
1639 vep_session->vep.prev_sid = ~0;
1640 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001641 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)12756512018-03-06 05:55:27 -08001642 vep_session->poll_reg = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001643
Florin Coras0d427d82018-06-27 03:24:07 -07001644 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
Dave Wallace7e607a72018-06-18 18:41:32 -04001645 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04001646
Florin Coras0d427d82018-06-27 03:24:07 -07001647 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
1648 getpid (), vep_idx, vep_idx);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001649
Dave Wallacef7f809c2017-10-03 01:48:42 -04001650 return (vep_idx);
1651}
1652
1653int
1654vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
1655 struct epoll_event *event)
1656{
Florin Coras7e12d942018-06-27 14:32:43 -07001657 vcl_session_t *vep_session;
1658 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001659 int rv;
1660
1661 if (vep_idx == session_index)
1662 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001663 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001664 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001665 return VPPCOM_EINVAL;
1666 }
1667
Dave Wallace7e607a72018-06-18 18:41:32 -04001668 VCL_SESSION_LOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04001669 rv = vppcom_session_at_index (vep_idx, &vep_session);
1670 if (PREDICT_FALSE (rv))
1671 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001672 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001673 goto done;
1674 }
1675 if (PREDICT_FALSE (!vep_session->is_vep))
1676 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001677 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001678 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001679 rv = VPPCOM_EINVAL;
1680 goto done;
1681 }
1682
1683 ASSERT (vep_session->vep.vep_idx == ~0);
1684 ASSERT (vep_session->vep.prev_sid == ~0);
1685
1686 rv = vppcom_session_at_index (session_index, &session);
1687 if (PREDICT_FALSE (rv))
1688 {
Florin Coras0d427d82018-06-27 03:24:07 -07001689 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
1690 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001691 goto done;
1692 }
1693 if (PREDICT_FALSE (session->is_vep))
1694 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001695 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001696 rv = VPPCOM_EINVAL;
1697 goto done;
1698 }
1699
1700 switch (op)
1701 {
1702 case EPOLL_CTL_ADD:
1703 if (PREDICT_FALSE (!event))
1704 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001705 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001706 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04001707 rv = VPPCOM_EINVAL;
1708 goto done;
1709 }
1710 if (vep_session->vep.next_sid != ~0)
1711 {
Florin Coras7e12d942018-06-27 14:32:43 -07001712 vcl_session_t *next_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001713 rv = vppcom_session_at_index (vep_session->vep.next_sid,
1714 &next_session);
1715 if (PREDICT_FALSE (rv))
1716 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001717 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001718 "vep.next_sid (%u) on vep_idx (%u)!",
1719 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001720 goto done;
1721 }
1722 ASSERT (next_session->vep.prev_sid == vep_idx);
1723 next_session->vep.prev_sid = session_index;
1724 }
1725 session->vep.next_sid = vep_session->vep.next_sid;
1726 session->vep.prev_sid = vep_idx;
1727 session->vep.vep_idx = vep_idx;
1728 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
1729 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001730 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001731 session->is_vep_session = 1;
1732 vep_session->vep.next_sid = session_index;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001733
1734 /* VCL Event Register handler */
Florin Coras7e12d942018-06-27 14:32:43 -07001735 if (session->session_state & STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001736 {
1737 /* Register handler for connect_request event on listen_session_index */
1738 vce_event_key_t evk;
1739 evk.session_index = session_index;
1740 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
Keith Burns (alagalah)12756512018-03-06 05:55:27 -08001741 vep_session->poll_reg =
1742 vce_register_handler (&vcm->event_thread, &evk,
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001743 vce_poll_wait_connect_request_handler_fn,
1744 0 /* No callback args */ );
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001745 }
Florin Coras0d427d82018-06-27 03:24:07 -07001746 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
1747 "sid %u, events 0x%x, data 0x%llx!",
1748 getpid (), vep_idx, session_index,
1749 event->events, event->data.u64);
1750 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001751 break;
1752
1753 case EPOLL_CTL_MOD:
1754 if (PREDICT_FALSE (!event))
1755 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001756 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001757 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04001758 rv = VPPCOM_EINVAL;
1759 goto done;
1760 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001761 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001762 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001763 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001764 "not a vep session!", getpid (), session_index);
1765 rv = VPPCOM_EINVAL;
1766 goto done;
1767 }
1768 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
1769 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001770 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001771 "vep_idx (%u) != vep_idx (%u)!",
1772 getpid (), session_index,
1773 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001774 rv = VPPCOM_EINVAL;
1775 goto done;
1776 }
1777 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
1778 session->vep.ev = *event;
Florin Coras0d427d82018-06-27 03:24:07 -07001779 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
1780 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
1781 event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001782 break;
1783
1784 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05001785 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001786 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001787 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001788 "not a vep session!", getpid (), session_index);
1789 rv = VPPCOM_EINVAL;
1790 goto done;
1791 }
1792 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
1793 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001794 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001795 "vep_idx (%u) != vep_idx (%u)!",
1796 getpid (), session_index,
1797 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001798 rv = VPPCOM_EINVAL;
1799 goto done;
1800 }
1801
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001802 /* VCL Event Un-register handler */
Florin Coras7e12d942018-06-27 14:32:43 -07001803 if ((session->session_state & STATE_LISTEN) && vep_session->poll_reg)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001804 {
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001805 (void) vce_unregister_handler (&vcm->event_thread,
1806 vep_session->poll_reg);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001807 }
1808
Dave Wallacef7f809c2017-10-03 01:48:42 -04001809 vep_session->wait_cont_idx =
1810 (vep_session->wait_cont_idx == session_index) ?
1811 session->vep.next_sid : vep_session->wait_cont_idx;
1812
1813 if (session->vep.prev_sid == vep_idx)
1814 vep_session->vep.next_sid = session->vep.next_sid;
1815 else
1816 {
Florin Coras7e12d942018-06-27 14:32:43 -07001817 vcl_session_t *prev_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001818 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
1819 if (PREDICT_FALSE (rv))
1820 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001821 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001822 "vep.prev_sid (%u) on sid (%u)!",
1823 getpid (), session->vep.prev_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001824 goto done;
1825 }
1826 ASSERT (prev_session->vep.next_sid == session_index);
1827 prev_session->vep.next_sid = session->vep.next_sid;
1828 }
1829 if (session->vep.next_sid != ~0)
1830 {
Florin Coras7e12d942018-06-27 14:32:43 -07001831 vcl_session_t *next_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001832 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
1833 if (PREDICT_FALSE (rv))
1834 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001835 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001836 "vep.next_sid (%u) on sid (%u)!",
1837 getpid (), session->vep.next_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001838 goto done;
1839 }
1840 ASSERT (next_session->vep.prev_sid == session_index);
1841 next_session->vep.prev_sid = session->vep.prev_sid;
1842 }
1843
1844 memset (&session->vep, 0, sizeof (session->vep));
1845 session->vep.next_sid = ~0;
1846 session->vep.prev_sid = ~0;
1847 session->vep.vep_idx = ~0;
1848 session->is_vep_session = 0;
Florin Coras0d427d82018-06-27 03:24:07 -07001849 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
1850 getpid (), vep_idx, session_index);
1851 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001852 break;
1853
1854 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05001855 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001856 rv = VPPCOM_EINVAL;
1857 }
1858
1859 vep_verify_epoll_chain (vep_idx);
1860
1861done:
Dave Wallace7e607a72018-06-18 18:41:32 -04001862 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04001863 return rv;
1864}
1865
Dave Wallacef7f809c2017-10-03 01:48:42 -04001866int
1867vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
1868 int maxevents, double wait_for_time)
1869{
Florin Coras7e12d942018-06-27 14:32:43 -07001870 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001871 int rv;
1872 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001873 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001874 int num_ev = 0;
1875 u32 vep_next_sid, wait_cont_idx;
1876 u8 is_vep;
1877
1878 if (PREDICT_FALSE (maxevents <= 0))
1879 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001880 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001881 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001882 return VPPCOM_EINVAL;
1883 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04001884 memset (events, 0, sizeof (*events) * maxevents);
1885
Dave Wallace7e607a72018-06-18 18:41:32 -04001886 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001887 vep_next_sid = vep_session->vep.next_sid;
1888 is_vep = vep_session->is_vep;
1889 wait_cont_idx = vep_session->wait_cont_idx;
Dave Wallace7e607a72018-06-18 18:41:32 -04001890 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04001891
1892 if (PREDICT_FALSE (!is_vep))
1893 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001894 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001895 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001896 rv = VPPCOM_EINVAL;
1897 goto done;
1898 }
Dave Wallacee695cb42017-11-02 22:04:42 -04001899 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001900 {
Florin Coras0d427d82018-06-27 03:24:07 -07001901 VDBG (1, "VCL<%d>: WARNING: vep_idx (%u) is empty!",
1902 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001903 goto done;
1904 }
1905
1906 do
1907 {
1908 u32 sid;
1909 u32 next_sid = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -07001910 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001911
1912 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
1913 sid != ~0; sid = next_sid)
1914 {
1915 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
1916 u8 add_event, is_vep_session;
1917 int ready;
1918 u64 session_ev_data;
1919
Dave Wallace7e607a72018-06-18 18:41:32 -04001920 VCL_SESSION_LOCK_AND_GET (sid, &session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001921 next_sid = session->vep.next_sid;
1922 session_events = session->vep.ev.events;
1923 et_mask = session->vep.et_mask;
1924 is_vep = session->is_vep;
1925 is_vep_session = session->is_vep_session;
1926 session_vep_idx = session->vep.vep_idx;
1927 session_ev_data = session->vep.ev.data.u64;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001928
Dave Wallace7e607a72018-06-18 18:41:32 -04001929 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04001930
1931 if (PREDICT_FALSE (is_vep))
1932 {
Florin Coras0d427d82018-06-27 03:24:07 -07001933 VDBG (0, "VCL<%d>: ERROR: sid (%u) is a vep!",
1934 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001935 rv = VPPCOM_EINVAL;
1936 goto done;
1937 }
1938 if (PREDICT_FALSE (!is_vep_session))
1939 {
Florin Coras0d427d82018-06-27 03:24:07 -07001940 VDBG (0, "VCL<%d>: ERROR: session (%u) is not "
1941 "a vep session!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001942 rv = VPPCOM_EINVAL;
1943 goto done;
1944 }
1945 if (PREDICT_FALSE (session_vep_idx != vep_idx))
1946 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001947 clib_warning ("VCL<%d>: ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04001948 "vep_idx (%u) != vep_idx (%u)!",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001949 getpid (), sid, session_vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001950 rv = VPPCOM_EINVAL;
1951 goto done;
1952 }
1953
1954 add_event = clear_et_mask = 0;
1955
Dave Wallace60caa062017-11-10 17:07:13 -05001956 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001957 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001958 VCL_SESSION_LOCK_AND_GET (sid, &session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001959 ready = vppcom_session_read_ready (session, sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04001960 VCL_SESSION_UNLOCK ();
Dave Wallace60caa062017-11-10 17:07:13 -05001961 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001962 {
1963 add_event = 1;
1964 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05001965 if (((EPOLLET | EPOLLIN) & session_events) ==
1966 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001967 clear_et_mask |= EPOLLIN;
1968 }
1969 else if (ready < 0)
1970 {
1971 add_event = 1;
1972 switch (ready)
1973 {
1974 case VPPCOM_ECONNRESET:
1975 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
1976 break;
1977
1978 default:
1979 events[num_ev].events |= EPOLLERR;
1980 break;
1981 }
1982 }
1983 }
1984
Dave Wallace60caa062017-11-10 17:07:13 -05001985 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001986 {
Dave Wallace7e607a72018-06-18 18:41:32 -04001987 VCL_SESSION_LOCK_AND_GET (sid, &session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04001988 ready = vppcom_session_write_ready (session, sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04001989 VCL_SESSION_UNLOCK ();
Dave Wallace60caa062017-11-10 17:07:13 -05001990 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001991 {
1992 add_event = 1;
1993 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05001994 if (((EPOLLET | EPOLLOUT) & session_events) ==
1995 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001996 clear_et_mask |= EPOLLOUT;
1997 }
1998 else if (ready < 0)
1999 {
2000 add_event = 1;
2001 switch (ready)
2002 {
2003 case VPPCOM_ECONNRESET:
2004 events[num_ev].events |= EPOLLHUP;
2005 break;
2006
2007 default:
2008 events[num_ev].events |= EPOLLERR;
2009 break;
2010 }
2011 }
2012 }
2013
2014 if (add_event)
2015 {
2016 events[num_ev].data.u64 = session_ev_data;
2017 if (EPOLLONESHOT & session_events)
2018 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002019 VCL_SESSION_LOCK_AND_GET (sid, &session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002020 session->vep.ev.events = 0;
Dave Wallace7e607a72018-06-18 18:41:32 -04002021 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002022 }
2023 num_ev++;
2024 if (num_ev == maxevents)
2025 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002026 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002027 vep_session->wait_cont_idx = next_sid;
Dave Wallace7e607a72018-06-18 18:41:32 -04002028 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002029 goto done;
2030 }
2031 }
2032 if (wait_cont_idx != ~0)
2033 {
2034 if (next_sid == ~0)
2035 next_sid = vep_next_sid;
2036 else if (next_sid == wait_cont_idx)
2037 next_sid = ~0;
2038 }
2039 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05002040 if (wait_for_time != -1)
2041 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002042 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05002043 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002044
2045 if (wait_cont_idx != ~0)
2046 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002047 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002048 vep_session->wait_cont_idx = ~0;
Dave Wallace7e607a72018-06-18 18:41:32 -04002049 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002050 }
2051done:
2052 return (rv != VPPCOM_OK) ? rv : num_ev;
2053}
2054
Dave Wallace35830af2017-10-09 01:43:42 -04002055int
2056vppcom_session_attr (uint32_t session_index, uint32_t op,
2057 void *buffer, uint32_t * buflen)
2058{
Florin Coras7e12d942018-06-27 14:32:43 -07002059 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002060 int rv = VPPCOM_OK;
2061 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07002062 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002063
Dave Wallace7e607a72018-06-18 18:41:32 -04002064 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002065
2066 ASSERT (session);
2067
Dave Wallace35830af2017-10-09 01:43:42 -04002068 switch (op)
2069 {
2070 case VPPCOM_ATTR_GET_NREAD:
2071 rv = vppcom_session_read_ready (session, session_index);
Florin Coras0d427d82018-06-27 03:24:07 -07002072 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2073 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002074 break;
2075
Dave Wallace227867f2017-11-13 21:21:53 -05002076 case VPPCOM_ATTR_GET_NWRITE:
2077 rv = vppcom_session_write_ready (session, session_index);
Florin Coras0d427d82018-06-27 03:24:07 -07002078 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
2079 getpid (), session_index, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002080 break;
2081
2082 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002083 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002084 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002085 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2086 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002087 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07002088 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2089 "is_nonblocking = %u", getpid (),
2090 session_index, *flags,
2091 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002092 }
2093 else
2094 rv = VPPCOM_EINVAL;
2095 break;
2096
2097 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002098 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002099 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002100 if (*flags & O_NONBLOCK)
2101 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2102 else
2103 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2104
Florin Coras0d427d82018-06-27 03:24:07 -07002105 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2106 " is_nonblocking = %u",
2107 getpid (), session_index, *flags,
2108 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002109 }
2110 else
2111 rv = VPPCOM_EINVAL;
2112 break;
2113
2114 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002115 if (PREDICT_TRUE (buffer && buflen &&
2116 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002117 {
Florin Coras7e12d942018-06-27 14:32:43 -07002118 ep->is_ip4 = session->transport.is_ip4;
2119 ep->port = session->transport.rmt_port;
2120 if (session->transport.is_ip4)
2121 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07002122 sizeof (ip4_address_t));
2123 else
Florin Coras7e12d942018-06-27 14:32:43 -07002124 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07002125 sizeof (ip6_address_t));
2126 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002127 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2128 "addr = %U, port %u", getpid (),
2129 session_index, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002130 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002131 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2132 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002133 }
2134 else
2135 rv = VPPCOM_EINVAL;
2136 break;
2137
2138 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002139 if (PREDICT_TRUE (buffer && buflen &&
2140 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002141 {
Florin Coras7e12d942018-06-27 14:32:43 -07002142 ep->is_ip4 = session->transport.is_ip4;
2143 ep->port = session->transport.lcl_port;
2144 if (session->transport.is_ip4)
2145 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07002146 sizeof (ip4_address_t));
2147 else
Florin Coras7e12d942018-06-27 14:32:43 -07002148 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07002149 sizeof (ip6_address_t));
2150 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002151 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2152 " addr = %U port %d", getpid (),
2153 session_index, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002154 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002155 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2156 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002157 }
2158 else
2159 rv = VPPCOM_EINVAL;
2160 break;
Stevenb5a11602017-10-11 09:59:30 -07002161
Dave Wallace048b1d62018-01-03 22:24:41 -05002162 case VPPCOM_ATTR_GET_LIBC_EPFD:
2163 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07002164 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2165 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002166 break;
2167
2168 case VPPCOM_ATTR_SET_LIBC_EPFD:
2169 if (PREDICT_TRUE (buffer && buflen &&
2170 (*buflen == sizeof (session->libc_epfd))))
2171 {
2172 session->libc_epfd = *(int *) buffer;
2173 *buflen = sizeof (session->libc_epfd);
2174
Florin Coras0d427d82018-06-27 03:24:07 -07002175 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2176 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002177 }
2178 else
2179 rv = VPPCOM_EINVAL;
2180 break;
2181
2182 case VPPCOM_ATTR_GET_PROTOCOL:
2183 if (buffer && buflen && (*buflen >= sizeof (int)))
2184 {
Florin Coras7e12d942018-06-27 14:32:43 -07002185 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002186 *buflen = sizeof (int);
2187
Florin Coras0d427d82018-06-27 03:24:07 -07002188 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2189 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2190 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002191 }
2192 else
2193 rv = VPPCOM_EINVAL;
2194 break;
2195
2196 case VPPCOM_ATTR_GET_LISTEN:
2197 if (buffer && buflen && (*buflen >= sizeof (int)))
2198 {
2199 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2200 VCL_SESS_ATTR_LISTEN);
2201 *buflen = sizeof (int);
2202
Florin Coras0d427d82018-06-27 03:24:07 -07002203 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2204 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002205 }
2206 else
2207 rv = VPPCOM_EINVAL;
2208 break;
2209
2210 case VPPCOM_ATTR_GET_ERROR:
2211 if (buffer && buflen && (*buflen >= sizeof (int)))
2212 {
2213 *(int *) buffer = 0;
2214 *buflen = sizeof (int);
2215
Florin Coras0d427d82018-06-27 03:24:07 -07002216 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2217 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002218 }
2219 else
2220 rv = VPPCOM_EINVAL;
2221 break;
2222
2223 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2224 if (buffer && buflen && (*buflen >= sizeof (u32)))
2225 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002226
2227 /* VPP-TBD */
2228 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002229 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002230 vcm->cfg.tx_fifo_size);
2231 *buflen = sizeof (u32);
2232
Florin Coras0d427d82018-06-27 03:24:07 -07002233 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2234 "buflen %d, #VPP-TBD#", getpid (),
2235 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002236 }
2237 else
2238 rv = VPPCOM_EINVAL;
2239 break;
2240
2241 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2242 if (buffer && buflen && (*buflen == sizeof (u32)))
2243 {
2244 /* VPP-TBD */
2245 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002246 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2247 "buflen %d, #VPP-TBD#", getpid (),
2248 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002249 }
2250 else
2251 rv = VPPCOM_EINVAL;
2252 break;
2253
2254 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2255 if (buffer && buflen && (*buflen >= sizeof (u32)))
2256 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002257
2258 /* VPP-TBD */
2259 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002260 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002261 vcm->cfg.rx_fifo_size);
2262 *buflen = sizeof (u32);
2263
Florin Coras0d427d82018-06-27 03:24:07 -07002264 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2265 "buflen %d, #VPP-TBD#", getpid (),
2266 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002267 }
2268 else
2269 rv = VPPCOM_EINVAL;
2270 break;
2271
2272 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2273 if (buffer && buflen && (*buflen == sizeof (u32)))
2274 {
2275 /* VPP-TBD */
2276 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002277 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2278 "buflen %d, #VPP-TBD#", getpid (),
2279 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002280 }
2281 else
2282 rv = VPPCOM_EINVAL;
2283 break;
2284
2285 case VPPCOM_ATTR_GET_REUSEADDR:
2286 if (buffer && buflen && (*buflen >= sizeof (int)))
2287 {
2288 /* VPP-TBD */
2289 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2290 VCL_SESS_ATTR_REUSEADDR);
2291 *buflen = sizeof (int);
2292
Florin Coras0d427d82018-06-27 03:24:07 -07002293 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2294 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002295 }
2296 else
2297 rv = VPPCOM_EINVAL;
2298 break;
2299
Stevenb5a11602017-10-11 09:59:30 -07002300 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002301 if (buffer && buflen && (*buflen == sizeof (int)) &&
2302 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2303 {
2304 /* VPP-TBD */
2305 if (*(int *) buffer)
2306 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2307 else
2308 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2309
Florin Coras0d427d82018-06-27 03:24:07 -07002310 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2311 " #VPP-TBD#", getpid (),
2312 VCL_SESS_ATTR_TEST (session->attr,
2313 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002314 }
2315 else
2316 rv = VPPCOM_EINVAL;
2317 break;
2318
2319 case VPPCOM_ATTR_GET_REUSEPORT:
2320 if (buffer && buflen && (*buflen >= sizeof (int)))
2321 {
2322 /* VPP-TBD */
2323 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2324 VCL_SESS_ATTR_REUSEPORT);
2325 *buflen = sizeof (int);
2326
Florin Coras0d427d82018-06-27 03:24:07 -07002327 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2328 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002329 }
2330 else
2331 rv = VPPCOM_EINVAL;
2332 break;
2333
2334 case VPPCOM_ATTR_SET_REUSEPORT:
2335 if (buffer && buflen && (*buflen == sizeof (int)) &&
2336 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2337 {
2338 /* VPP-TBD */
2339 if (*(int *) buffer)
2340 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2341 else
2342 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2343
Florin Coras0d427d82018-06-27 03:24:07 -07002344 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2345 " #VPP-TBD#", getpid (),
2346 VCL_SESS_ATTR_TEST (session->attr,
2347 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002348 }
2349 else
2350 rv = VPPCOM_EINVAL;
2351 break;
2352
2353 case VPPCOM_ATTR_GET_BROADCAST:
2354 if (buffer && buflen && (*buflen >= sizeof (int)))
2355 {
2356 /* VPP-TBD */
2357 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2358 VCL_SESS_ATTR_BROADCAST);
2359 *buflen = sizeof (int);
2360
Florin Coras0d427d82018-06-27 03:24:07 -07002361 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2362 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002363 }
2364 else
2365 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002366 break;
2367
2368 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002369 if (buffer && buflen && (*buflen == sizeof (int)))
2370 {
2371 /* VPP-TBD */
2372 if (*(int *) buffer)
2373 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2374 else
2375 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2376
Florin Coras0d427d82018-06-27 03:24:07 -07002377 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2378 "#VPP-TBD#", getpid (),
2379 VCL_SESS_ATTR_TEST (session->attr,
2380 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002381 }
2382 else
2383 rv = VPPCOM_EINVAL;
2384 break;
2385
2386 case VPPCOM_ATTR_GET_V6ONLY:
2387 if (buffer && buflen && (*buflen >= sizeof (int)))
2388 {
2389 /* VPP-TBD */
2390 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2391 VCL_SESS_ATTR_V6ONLY);
2392 *buflen = sizeof (int);
2393
Florin Coras0d427d82018-06-27 03:24:07 -07002394 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2395 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002396 }
2397 else
2398 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002399 break;
2400
2401 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002402 if (buffer && buflen && (*buflen == sizeof (int)))
2403 {
2404 /* VPP-TBD */
2405 if (*(int *) buffer)
2406 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2407 else
2408 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2409
Florin Coras0d427d82018-06-27 03:24:07 -07002410 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2411 "#VPP-TBD#", getpid (),
2412 VCL_SESS_ATTR_TEST (session->attr,
2413 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002414 }
2415 else
2416 rv = VPPCOM_EINVAL;
2417 break;
2418
2419 case VPPCOM_ATTR_GET_KEEPALIVE:
2420 if (buffer && buflen && (*buflen >= sizeof (int)))
2421 {
2422 /* VPP-TBD */
2423 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2424 VCL_SESS_ATTR_KEEPALIVE);
2425 *buflen = sizeof (int);
2426
Florin Coras0d427d82018-06-27 03:24:07 -07002427 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2428 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002429 }
2430 else
2431 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002432 break;
Stevenbccd3392017-10-12 20:42:21 -07002433
2434 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002435 if (buffer && buflen && (*buflen == sizeof (int)))
2436 {
2437 /* VPP-TBD */
2438 if (*(int *) buffer)
2439 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2440 else
2441 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2442
Florin Coras0d427d82018-06-27 03:24:07 -07002443 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2444 "#VPP-TBD#", getpid (),
2445 VCL_SESS_ATTR_TEST (session->attr,
2446 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002447 }
2448 else
2449 rv = VPPCOM_EINVAL;
2450 break;
2451
2452 case VPPCOM_ATTR_GET_TCP_NODELAY:
2453 if (buffer && buflen && (*buflen >= sizeof (int)))
2454 {
2455 /* VPP-TBD */
2456 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2457 VCL_SESS_ATTR_TCP_NODELAY);
2458 *buflen = sizeof (int);
2459
Florin Coras0d427d82018-06-27 03:24:07 -07002460 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2461 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002462 }
2463 else
2464 rv = VPPCOM_EINVAL;
2465 break;
2466
2467 case VPPCOM_ATTR_SET_TCP_NODELAY:
2468 if (buffer && buflen && (*buflen == sizeof (int)))
2469 {
2470 /* VPP-TBD */
2471 if (*(int *) buffer)
2472 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2473 else
2474 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2475
Florin Coras0d427d82018-06-27 03:24:07 -07002476 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
2477 "#VPP-TBD#", getpid (),
2478 VCL_SESS_ATTR_TEST (session->attr,
2479 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002480 }
2481 else
2482 rv = VPPCOM_EINVAL;
2483 break;
2484
2485 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
2486 if (buffer && buflen && (*buflen >= sizeof (int)))
2487 {
2488 /* VPP-TBD */
2489 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2490 VCL_SESS_ATTR_TCP_KEEPIDLE);
2491 *buflen = sizeof (int);
2492
Florin Coras0d427d82018-06-27 03:24:07 -07002493 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
2494 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002495 }
2496 else
2497 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07002498 break;
2499
2500 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002501 if (buffer && buflen && (*buflen == sizeof (int)))
2502 {
2503 /* VPP-TBD */
2504 if (*(int *) buffer)
2505 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2506 else
2507 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
2508
Florin Coras0d427d82018-06-27 03:24:07 -07002509 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
2510 "#VPP-TBD#", getpid (),
2511 VCL_SESS_ATTR_TEST (session->attr,
2512 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002513 }
2514 else
2515 rv = VPPCOM_EINVAL;
2516 break;
2517
2518 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
2519 if (buffer && buflen && (*buflen >= sizeof (int)))
2520 {
2521 /* VPP-TBD */
2522 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2523 VCL_SESS_ATTR_TCP_KEEPINTVL);
2524 *buflen = sizeof (int);
2525
Florin Coras0d427d82018-06-27 03:24:07 -07002526 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
2527 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002528 }
2529 else
2530 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07002531 break;
2532
2533 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05002534 if (buffer && buflen && (*buflen == sizeof (int)))
2535 {
2536 /* VPP-TBD */
2537 if (*(int *) buffer)
2538 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2539 else
2540 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
2541
Florin Coras0d427d82018-06-27 03:24:07 -07002542 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
2543 "#VPP-TBD#", getpid (),
2544 VCL_SESS_ATTR_TEST (session->attr,
2545 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002546 }
2547 else
2548 rv = VPPCOM_EINVAL;
2549 break;
2550
2551 case VPPCOM_ATTR_GET_TCP_USER_MSS:
2552 if (buffer && buflen && (*buflen >= sizeof (u32)))
2553 {
2554 /* VPP-TBD */
2555 *(u32 *) buffer = session->user_mss;
2556 *buflen = sizeof (int);
2557
Florin Coras0d427d82018-06-27 03:24:07 -07002558 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
2559 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002560 }
2561 else
2562 rv = VPPCOM_EINVAL;
2563 break;
2564
2565 case VPPCOM_ATTR_SET_TCP_USER_MSS:
2566 if (buffer && buflen && (*buflen == sizeof (u32)))
2567 {
2568 /* VPP-TBD */
2569 session->user_mss = *(u32 *) buffer;
2570
Florin Coras0d427d82018-06-27 03:24:07 -07002571 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
2572 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002573 }
2574 else
2575 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07002576 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04002577
2578 default:
2579 rv = VPPCOM_EINVAL;
2580 break;
Dave Wallace35830af2017-10-09 01:43:42 -04002581 }
2582
2583done:
Dave Wallace7e607a72018-06-18 18:41:32 -04002584 VCL_SESSION_UNLOCK ();
Dave Wallace35830af2017-10-09 01:43:42 -04002585 return rv;
2586}
2587
Stevenac1f96d2017-10-24 16:03:58 -07002588int
2589vppcom_session_recvfrom (uint32_t session_index, void *buffer,
2590 uint32_t buflen, int flags, vppcom_endpt_t * ep)
2591{
Stevenac1f96d2017-10-24 16:03:58 -07002592 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07002593 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07002594
2595 if (ep)
2596 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002597 VCL_SESSION_LOCK ();
Stevenac1f96d2017-10-24 16:03:58 -07002598 rv = vppcom_session_at_index (session_index, &session);
2599 if (PREDICT_FALSE (rv))
2600 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002601 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07002602 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
2603 getpid (), session_index);
Dave Wallacefaf9d772017-10-26 16:12:04 -04002604 rv = VPPCOM_EBADFD;
Dave Wallace7e607a72018-06-18 18:41:32 -04002605 VCL_SESSION_UNLOCK ();
Dave Wallacefaf9d772017-10-26 16:12:04 -04002606 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07002607 }
Florin Coras7e12d942018-06-27 14:32:43 -07002608 ep->is_ip4 = session->transport.is_ip4;
2609 ep->port = session->transport.rmt_port;
2610 if (session->transport.is_ip4)
2611 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
Stevenac1f96d2017-10-24 16:03:58 -07002612 sizeof (ip4_address_t));
2613 else
Florin Coras7e12d942018-06-27 14:32:43 -07002614 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
Stevenac1f96d2017-10-24 16:03:58 -07002615 sizeof (ip6_address_t));
Dave Wallace7e607a72018-06-18 18:41:32 -04002616 VCL_SESSION_UNLOCK ();
Stevenac1f96d2017-10-24 16:03:58 -07002617 }
Steven58f464e2017-10-25 12:33:12 -07002618
2619 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07002620 rv = vppcom_session_read (session_index, buffer, buflen);
2621 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07002622 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07002623 else
2624 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002625 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
2626 getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07002627 rv = VPPCOM_EAFNOSUPPORT;
2628 }
2629
Dave Wallacefaf9d772017-10-26 16:12:04 -04002630done:
Stevenac1f96d2017-10-24 16:03:58 -07002631 return rv;
2632}
2633
2634int
2635vppcom_session_sendto (uint32_t session_index, void *buffer,
2636 uint32_t buflen, int flags, vppcom_endpt_t * ep)
2637{
Dave Wallace617dffa2017-10-26 14:47:06 -04002638 if (!buffer)
2639 return VPPCOM_EINVAL;
2640
2641 if (ep)
2642 {
2643 // TBD
2644 return VPPCOM_EINVAL;
2645 }
2646
2647 if (flags)
2648 {
2649 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07002650 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
2651 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04002652 }
2653
2654 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07002655}
2656
Dave Wallace048b1d62018-01-03 22:24:41 -05002657int
2658vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
2659{
2660 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
2661 u32 i, keep_trying = 1;
2662 int rv, num_ev = 0;
2663
Florin Coras0d427d82018-06-27 03:24:07 -07002664 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
2665 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05002666
2667 if (!vp)
2668 return VPPCOM_EFAULT;
2669
2670 do
2671 {
Florin Coras7e12d942018-06-27 14:32:43 -07002672 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05002673
2674 for (i = 0; i < n_sids; i++)
2675 {
2676 ASSERT (vp[i].revents);
2677
Dave Wallace7e607a72018-06-18 18:41:32 -04002678 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
2679 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002680
2681 if (*vp[i].revents)
2682 *vp[i].revents = 0;
2683
2684 if (POLLIN & vp[i].events)
2685 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002686 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
Dave Wallace048b1d62018-01-03 22:24:41 -05002687 rv = vppcom_session_read_ready (session, vp[i].sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04002688 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002689 if (rv > 0)
2690 {
2691 *vp[i].revents |= POLLIN;
2692 num_ev++;
2693 }
2694 else if (rv < 0)
2695 {
2696 switch (rv)
2697 {
2698 case VPPCOM_ECONNRESET:
2699 *vp[i].revents = POLLHUP;
2700 break;
2701
2702 default:
2703 *vp[i].revents = POLLERR;
2704 break;
2705 }
2706 num_ev++;
2707 }
2708 }
2709
2710 if (POLLOUT & vp[i].events)
2711 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002712 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
Dave Wallace048b1d62018-01-03 22:24:41 -05002713 rv = vppcom_session_write_ready (session, vp[i].sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04002714 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002715 if (rv > 0)
2716 {
2717 *vp[i].revents |= POLLOUT;
2718 num_ev++;
2719 }
2720 else if (rv < 0)
2721 {
2722 switch (rv)
2723 {
2724 case VPPCOM_ECONNRESET:
2725 *vp[i].revents = POLLHUP;
2726 break;
2727
2728 default:
2729 *vp[i].revents = POLLERR;
2730 break;
2731 }
2732 num_ev++;
2733 }
2734 }
2735
Dave Wallace7e607a72018-06-18 18:41:32 -04002736 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05002737 {
2738 done:
2739 *vp[i].revents = POLLNVAL;
2740 num_ev++;
2741 }
2742 }
2743 if (wait_for_time != -1)
2744 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
2745 }
2746 while ((num_ev == 0) && keep_trying);
2747
2748 if (VPPCOM_DEBUG > 3)
2749 {
2750 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
2751 for (i = 0; i < n_sids; i++)
2752 {
2753 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
2754 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
2755 vp[i].events, *vp[i].revents);
2756 }
2757 }
2758 return num_ev;
2759}
2760
Dave Wallacee22aa742017-10-20 12:30:38 -04002761/*
2762 * fd.io coding-style-patch-verification: ON
2763 *
2764 * Local Variables:
2765 * eval: (c-set-style "gnu")
2766 * End:
2767 */