blob: 3cd2e766f63eb392cabe79c7d9b022ce4b0144aa [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>
18#include <signal.h>
19#include <svm/svm_fifo_segment.h>
20#include <vlibmemory/api.h>
21#include <vpp/api/vpe_msg_enum.h>
22#include <vnet/session/application_interface.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040023#include <vcl/vppcom.h>
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -080024#include <vcl/vcl_event.h>
Florin Coras0d427d82018-06-27 03:24:07 -070025#include <vcl/vcl_debug.h>
Dave Wallace543852a2017-08-03 02:11:34 -040026#include <vlib/unix/unix.h>
27#include <vppinfra/vec_bootstrap.h>
28
29#define vl_typedefs /* define message structures */
30#include <vpp/api/vpe_all_api_h.h>
31#undef vl_typedefs
32
33/* declare message handlers for each api */
34
35#define vl_endianfun /* define message structures */
36#include <vpp/api/vpe_all_api_h.h>
37#undef vl_endianfun
38
39/* instantiate all the print functions we know about */
40#define vl_print(handle, ...)
41#define vl_printfun
42#include <vpp/api/vpe_all_api_h.h>
43#undef vl_printfun
44
45#if (CLIB_DEBUG > 0)
Dave Wallace498b3a52017-11-09 13:00:34 -050046/* Set VPPCOM_DEBUG_INIT 2 for connection debug,
47 * 3 for read/write debug output
48 * or
49 * export VCL_DEBUG=<#> to set dynamically.
50 */
51#define VPPCOM_DEBUG_INIT 1
Dave Wallace543852a2017-08-03 02:11:34 -040052#else
Dave Wallace498b3a52017-11-09 13:00:34 -050053#define VPPCOM_DEBUG_INIT 0
Dave Wallace543852a2017-08-03 02:11:34 -040054#endif
55
Dave Wallace498b3a52017-11-09 13:00:34 -050056#define VPPCOM_DEBUG vcm->debug
57
Dave Wallace543852a2017-08-03 02:11:34 -040058/*
59 * VPPCOM Private definitions and functions.
60 */
61typedef enum
62{
63 STATE_APP_START,
64 STATE_APP_CONN_VPP,
65 STATE_APP_ENABLED,
66 STATE_APP_ATTACHED,
67} app_state_t;
68
69typedef enum
70{
Dave Wallace4878cbe2017-11-21 03:45:09 -050071 STATE_START = 0x01,
72 STATE_CONNECT = 0x02,
73 STATE_LISTEN = 0x04,
74 STATE_ACCEPT = 0x08,
75 STATE_CLOSE_ON_EMPTY = 0x10,
76 STATE_DISCONNECT = 0x20,
77 STATE_FAILED = 0x40
Dave Wallace543852a2017-08-03 02:11:34 -040078} session_state_t;
79
Dave Wallace4878cbe2017-11-21 03:45:09 -050080#define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
81#define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
82
Dave Wallacef7f809c2017-10-03 01:48:42 -040083typedef struct epoll_event vppcom_epoll_event_t;
84
85typedef struct
86{
87 u32 next_sid;
88 u32 prev_sid;
89 u32 vep_idx;
90 vppcom_epoll_event_t ev;
91#define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
Dave Wallace60caa062017-11-10 17:07:13 -050092#define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
Dave Wallacef7f809c2017-10-03 01:48:42 -040093 u32 et_mask;
94} vppcom_epoll_t;
95
Dave Wallace543852a2017-08-03 02:11:34 -040096typedef struct
97{
Dave Wallace35830af2017-10-09 01:43:42 -040098 u8 is_ip4;
99 ip46_address_t ip46;
100} vppcom_ip46_t;
101
Dave Wallace048b1d62018-01-03 22:24:41 -0500102enum
103{
104 VCL_SESS_ATTR_SERVER,
105 VCL_SESS_ATTR_CUT_THRU,
106 VCL_SESS_ATTR_VEP,
107 VCL_SESS_ATTR_VEP_SESSION,
108 VCL_SESS_ATTR_LISTEN, // SOL_SOCKET,SO_ACCEPTCONN
109 VCL_SESS_ATTR_NONBLOCK, // fcntl,O_NONBLOCK
110 VCL_SESS_ATTR_REUSEADDR, // SOL_SOCKET,SO_REUSEADDR
111 VCL_SESS_ATTR_REUSEPORT, // SOL_SOCKET,SO_REUSEPORT
112 VCL_SESS_ATTR_BROADCAST, // SOL_SOCKET,SO_BROADCAST
113 VCL_SESS_ATTR_V6ONLY, // SOL_TCP,IPV6_V6ONLY
114 VCL_SESS_ATTR_KEEPALIVE, // SOL_SOCKET,SO_KEEPALIVE
115 VCL_SESS_ATTR_TCP_NODELAY, // SOL_TCP,TCP_NODELAY
116 VCL_SESS_ATTR_TCP_KEEPIDLE, // SOL_TCP,TCP_KEEPIDLE
117 VCL_SESS_ATTR_TCP_KEEPINTVL, // SOL_TCP,TCP_KEEPINTVL
118 VCL_SESS_ATTR_MAX
119} vppcom_session_attr_t;
120
121#define VCL_SESS_ATTR_SET(ATTR, VAL) \
122do { \
123 (ATTR) |= 1 << (VAL); \
124 } while (0)
125
126#define VCL_SESS_ATTR_CLR(ATTR, VAL) \
127do { \
128 (ATTR) &= ~(1 << (VAL)); \
129 } while (0)
130
131#define VCL_SESS_ATTR_TEST(ATTR, VAL) \
132 ((ATTR) & (1 << (VAL)) ? 1 : 0)
133
Dave Wallace35830af2017-10-09 01:43:42 -0400134typedef struct
135{
Florin Coras7e12d942018-06-27 14:32:43 -0700136#define _(type, name) type name;
137 foreach_app_session_field
138#undef _
Dave Wallace048b1d62018-01-03 22:24:41 -0500139 u32 sndbuf_size; // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
140 u32 rcvbuf_size; // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
141 u32 user_mss; // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
Dave Wallace60caa062017-11-10 17:07:13 -0500142 u8 *segment_name;
Dave Wallace543852a2017-08-03 02:11:34 -0400143 u32 sm_seg_index;
Dave Wallace60caa062017-11-10 17:07:13 -0500144 u32 client_context;
145 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400146
147 /* Socket configuration state */
Dave Wallacef7f809c2017-10-03 01:48:42 -0400148 u8 is_vep;
149 u8 is_vep_session;
Dave Wallace048b1d62018-01-03 22:24:41 -0500150 u32 attr;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400151 u32 wait_cont_idx;
152 vppcom_epoll_t vep;
Dave Wallace048b1d62018-01-03 22:24:41 -0500153 int libc_epfd;
Dave Wallace543852a2017-08-03 02:11:34 -0400154 u64 client_queue_address;
155 u64 options[16];
Keith Burns (alagalah)12756512018-03-06 05:55:27 -0800156 vce_event_handler_reg_t *poll_reg;
Florin Coras7e12d942018-06-27 14:32:43 -0700157#if VCL_ELOG
158 elog_track_t elog_track;
159#endif
160} vcl_session_t;
Dave Wallace543852a2017-08-03 02:11:34 -0400161
162typedef struct vppcom_cfg_t_
163{
164 u64 heapsize;
Dave Wallacec8f1ee62017-11-29 22:46:32 -0500165 u32 vpp_api_q_length;
Dave Wallace543852a2017-08-03 02:11:34 -0400166 u64 segment_baseva;
167 u32 segment_size;
168 u32 add_segment_size;
169 u32 preallocated_fifo_pairs;
170 u32 rx_fifo_size;
171 u32 tx_fifo_size;
172 u32 event_queue_size;
173 u32 listen_queue_size;
Dave Wallace774169b2017-11-01 20:07:40 -0400174 u8 app_proxy_transport_tcp;
175 u8 app_proxy_transport_udp;
176 u8 app_scope_local;
177 u8 app_scope_global;
Dave Wallace8af20542017-10-26 03:29:30 -0400178 u8 *namespace_id;
179 u64 namespace_secret;
Dave Wallace543852a2017-08-03 02:11:34 -0400180 f64 app_timeout;
181 f64 session_timeout;
182 f64 accept_timeout;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800183 u32 event_ring_size;
184 char *event_log_path;
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500185 u8 *vpp_api_filename;
Dave Wallace543852a2017-08-03 02:11:34 -0400186} vppcom_cfg_t;
187
188typedef struct vppcom_main_t_
189{
190 u8 init;
Dave Wallace498b3a52017-11-09 13:00:34 -0500191 u32 debug;
Dave Wallace543852a2017-08-03 02:11:34 -0400192 int main_cpu;
193
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -0800194 /* FIFO for accepted connections - used in epoll/select */
195 clib_spinlock_t session_fifo_lockp;
196 u32 *client_session_index_fifo;
197
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500198 /* vpp input queue */
Florin Corase86a8ed2018-01-05 03:20:25 -0800199 svm_queue_t *vl_input_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400200
201 /* API client handle */
202 u32 my_client_index;
Dave Wallace543852a2017-08-03 02:11:34 -0400203 /* Session pool */
204 clib_spinlock_t sessions_lockp;
Florin Coras7e12d942018-06-27 14:32:43 -0700205 vcl_session_t *sessions;
Dave Wallace543852a2017-08-03 02:11:34 -0400206
207 /* Hash table for disconnect processing */
208 uword *session_index_by_vpp_handles;
209
210 /* Select bitmaps */
211 clib_bitmap_t *rd_bitmap;
212 clib_bitmap_t *wr_bitmap;
213 clib_bitmap_t *ex_bitmap;
214
215 /* Our event queue */
Florin Corase86a8ed2018-01-05 03:20:25 -0800216 svm_queue_t *app_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400217
218 /* unique segment name counter */
219 u32 unique_segment_index;
220
Dave Wallace543852a2017-08-03 02:11:34 -0400221 /* For deadman timers */
222 clib_time_t clib_time;
223
224 /* State of the connection, shared between msg RX thread and main thread */
225 volatile app_state_t app_state;
226
227 vppcom_cfg_t cfg;
228
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800229 /* Event thread */
230 vce_event_thread_t event_thread;
231
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700232 /* IO thread */
233 vppcom_session_io_thread_t session_io_thread;
234
Florin Coras0d427d82018-06-27 03:24:07 -0700235#ifdef VCL_ELOG
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800236 /* VPP Event-logger */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800237 elog_main_t elog_main;
238 elog_track_t elog_track;
Florin Coras0d427d82018-06-27 03:24:07 -0700239#endif
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800240
Dave Wallace543852a2017-08-03 02:11:34 -0400241 /* VNET_API_ERROR_FOO -> "Foo" hash table */
242 uword *error_string_by_error_number;
243} vppcom_main_t;
244
Dave Wallace2e005bb2017-11-07 01:21:39 -0500245/* NOTE: _vppcom_main is only used until the heap is allocated.
246 * Do not access it directly -- use vcm which will point to
247 * the heap allocated copy after init.
248 */
Dave Wallace498b3a52017-11-09 13:00:34 -0500249static vppcom_main_t _vppcom_main = {
250 .debug = VPPCOM_DEBUG_INIT,
251 .my_client_index = ~0
252};
Dave Wallace2e005bb2017-11-07 01:21:39 -0500253
254static vppcom_main_t *vcm = &_vppcom_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400255
Dave Wallace7e607a72018-06-18 18:41:32 -0400256#define VCL_SESSION_LOCK_AND_GET(I, S) \
Dave Wallace048b1d62018-01-03 22:24:41 -0500257do { \
258 clib_spinlock_lock (&vcm->sessions_lockp); \
259 rv = vppcom_session_at_index (I, S); \
260 if (PREDICT_FALSE (rv)) \
261 { \
262 clib_spinlock_unlock (&vcm->sessions_lockp); \
263 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
264 getpid (), I); \
265 goto done; \
266 } \
Dave Wallace60caa062017-11-10 17:07:13 -0500267} while (0)
268
Dave Wallace7e607a72018-06-18 18:41:32 -0400269#define VCL_SESSION_LOCK() clib_spinlock_lock (&(vcm->sessions_lockp))
270#define VCL_SESSION_UNLOCK() clib_spinlock_unlock (&(vcm->sessions_lockp))
271
272#define VCL_IO_SESSIONS_LOCK() \
273 clib_spinlock_lock (&(vcm->session_io_thread.io_sessions_lockp))
274#define VCL_IO_SESSIONS_UNLOCK() \
275 clib_spinlock_unlock (&(vcm->session_io_thread.io_sessions_lockp))
276
277#define VCL_ACCEPT_FIFO_LOCK() clib_spinlock_lock (&(vcm->session_fifo_lockp))
278#define VCL_ACCEPT_FIFO_UNLOCK() \
279 clib_spinlock_unlock (&(vcm->session_fifo_lockp))
280
281#define VCL_EVENTS_LOCK() \
282 clib_spinlock_lock (&(vcm->event_thread.events_lockp))
283#define VCL_EVENTS_UNLOCK() \
284 clib_spinlock_unlock (&(vcm->event_thread.events_lockp))
285
Dave Wallace543852a2017-08-03 02:11:34 -0400286static const char *
287vppcom_app_state_str (app_state_t state)
288{
289 char *st;
290
291 switch (state)
292 {
293 case STATE_APP_START:
294 st = "STATE_APP_START";
295 break;
296
297 case STATE_APP_CONN_VPP:
298 st = "STATE_APP_CONN_VPP";
299 break;
300
301 case STATE_APP_ENABLED:
302 st = "STATE_APP_ENABLED";
303 break;
304
305 case STATE_APP_ATTACHED:
306 st = "STATE_APP_ATTACHED";
307 break;
308
309 default:
310 st = "UNKNOWN_APP_STATE";
311 break;
312 }
313
314 return st;
315}
316
317static const char *
318vppcom_session_state_str (session_state_t state)
319{
320 char *st;
321
322 switch (state)
323 {
324 case STATE_START:
325 st = "STATE_START";
326 break;
327
328 case STATE_CONNECT:
329 st = "STATE_CONNECT";
330 break;
331
332 case STATE_LISTEN:
333 st = "STATE_LISTEN";
334 break;
335
336 case STATE_ACCEPT:
337 st = "STATE_ACCEPT";
338 break;
339
Dave Wallace4878cbe2017-11-21 03:45:09 -0500340 case STATE_CLOSE_ON_EMPTY:
341 st = "STATE_CLOSE_ON_EMPTY";
342 break;
343
Dave Wallace543852a2017-08-03 02:11:34 -0400344 case STATE_DISCONNECT:
345 st = "STATE_DISCONNECT";
346 break;
347
348 case STATE_FAILED:
349 st = "STATE_FAILED";
350 break;
351
352 default:
353 st = "UNKNOWN_STATE";
354 break;
355 }
356
357 return st;
358}
359
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800360
Dave Wallace543852a2017-08-03 02:11:34 -0400361/*
362 * VPPCOM Utility Functions
363 */
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700364
Dave Wallace543852a2017-08-03 02:11:34 -0400365static inline int
Florin Coras7e12d942018-06-27 14:32:43 -0700366vppcom_session_at_index (u32 session_index, vcl_session_t * volatile *sess)
Dave Wallace543852a2017-08-03 02:11:34 -0400367{
Dave Wallace543852a2017-08-03 02:11:34 -0400368 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
369 if (PREDICT_FALSE ((session_index == ~0) ||
370 pool_is_free_index (vcm->sessions, session_index)))
371 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500372 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500373 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400374 return VPPCOM_EBADFD;
375 }
376 *sess = pool_elt_at_index (vcm->sessions, session_index);
377 return VPPCOM_OK;
378}
379
Florin Corasdcf55ce2017-11-16 15:32:50 -0800380static inline void
381vppcom_session_table_add_listener (u64 listener_handle, u32 value)
382{
383 /* Session and listener handles have different formats. The latter has
384 * the thread index in the upper 32 bits while the former has the session
385 * type. Knowing that, for listeners we just flip the MSB to 1 */
386 listener_handle |= 1ULL << 63;
387 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
388}
389
Florin Coras7e12d942018-06-27 14:32:43 -0700390static inline vcl_session_t *
Florin Corasdcf55ce2017-11-16 15:32:50 -0800391vppcom_session_table_lookup_listener (u64 listener_handle)
392{
393 uword *p;
394 u64 handle = listener_handle | (1ULL << 63);
Florin Coras7e12d942018-06-27 14:32:43 -0700395 vcl_session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500396
Florin Corasdcf55ce2017-11-16 15:32:50 -0800397 p = hash_get (vcm->session_index_by_vpp_handles, handle);
398 if (!p)
399 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500400 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
Florin Corasdcf55ce2017-11-16 15:32:50 -0800401 "listener handle %llx", getpid (), listener_handle);
402 return 0;
403 }
404 if (pool_is_free_index (vcm->sessions, p[0]))
405 {
Florin Coras0d427d82018-06-27 03:24:07 -0700406 VDBG (1, "VCL<%d>: invalid listen session, sid (%u)", getpid (), p[0]);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800407 return 0;
408 }
409
Dave Wallace4878cbe2017-11-21 03:45:09 -0500410 session = pool_elt_at_index (vcm->sessions, p[0]);
Florin Coras7e12d942018-06-27 14:32:43 -0700411 ASSERT (session->session_state & STATE_LISTEN);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500412 return session;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800413}
414
415static inline void
416vppcom_session_table_del_listener (u64 listener_handle)
417{
418 listener_handle |= 1ULL << 63;
419 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
420}
421
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -0800422static inline void
423vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
424{
425 vl_api_accept_session_reply_t *rmp;
426
427 rmp = vl_msg_api_alloc (sizeof (*rmp));
428 memset (rmp, 0, sizeof (*rmp));
429 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
430 rmp->retval = htonl (retval);
431 rmp->context = context;
432 rmp->handle = handle;
433 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
434}
435
Dave Wallace543852a2017-08-03 02:11:34 -0400436static int
437vppcom_connect_to_vpp (char *app_name)
438{
439 api_main_t *am = &api_main;
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500440 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800441 int rv = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400442
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500443 if (!vcl_cfg->vpp_api_filename)
444 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500445
Florin Coras0d427d82018-06-27 03:24:07 -0700446 VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...",
447 getpid (), app_name, vcl_cfg->vpp_api_filename);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500448
449 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name,
Dave Wallacec8f1ee62017-11-29 22:46:32 -0500450 vcm->cfg.vpp_api_q_length) < 0)
Dave Wallace543852a2017-08-03 02:11:34 -0400451 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500452 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800453 rv = VPPCOM_ECONNREFUSED;
454 }
455 else
456 {
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800457 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800458 vcm->my_client_index = (u32) am->my_client_index;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800459 vcm->app_state = STATE_APP_CONN_VPP;
Dave Wallace9b954252018-01-18 17:01:40 -0500460
Florin Coras0d427d82018-06-27 03:24:07 -0700461 VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400462 }
463
Florin Coras0d427d82018-06-27 03:24:07 -0700464 vcl_evt (VCL_EVT_INIT, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800465 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400466}
467
468static u8 *
469format_api_error (u8 * s, va_list * args)
470{
Dave Wallace543852a2017-08-03 02:11:34 -0400471 i32 error = va_arg (*args, u32);
472 uword *p;
473
474 p = hash_get (vcm->error_string_by_error_number, -error);
475
476 if (p)
477 s = format (s, "%s (%d)", p[0], error);
478 else
479 s = format (s, "%d", error);
480 return s;
481}
482
483static void
484vppcom_init_error_string_table (void)
485{
Dave Wallace543852a2017-08-03 02:11:34 -0400486 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
487
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -0800488#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
Dave Wallace543852a2017-08-03 02:11:34 -0400489 foreach_vnet_api_error;
490#undef _
491
492 hash_set (vcm->error_string_by_error_number, 99, "Misc");
493}
494
495static inline int
496vppcom_wait_for_app_state_change (app_state_t app_state)
497{
Dave Wallace543852a2017-08-03 02:11:34 -0400498 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
499
500 while (clib_time_now (&vcm->clib_time) < timeout)
501 {
502 if (vcm->app_state == app_state)
503 return VPPCOM_OK;
504 }
Florin Coras0d427d82018-06-27 03:24:07 -0700505 VDBG (0, "VCL<%d>: timeout waiting for state %s (%d)", getpid (),
506 vppcom_app_state_str (app_state), app_state);
507 vcl_evt (VCL_EVT_SESSION_TIMEOUT, vcm, app_state);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800508
Dave Wallace543852a2017-08-03 02:11:34 -0400509 return VPPCOM_ETIMEDOUT;
510}
511
512static inline int
513vppcom_wait_for_session_state_change (u32 session_index,
514 session_state_t state,
515 f64 wait_for_time)
516{
Dave Wallace543852a2017-08-03 02:11:34 -0400517 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Florin Coras7e12d942018-06-27 14:32:43 -0700518 vcl_session_t *volatile session;
Dave Wallace543852a2017-08-03 02:11:34 -0400519 int rv;
520
521 do
522 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400523 VCL_SESSION_LOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400524 rv = vppcom_session_at_index (session_index, &session);
525 if (PREDICT_FALSE (rv))
526 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400527 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400528 return rv;
529 }
Florin Coras7e12d942018-06-27 14:32:43 -0700530 if (session->session_state & state)
Dave Wallace543852a2017-08-03 02:11:34 -0400531 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400532 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400533 return VPPCOM_OK;
534 }
Florin Coras7e12d942018-06-27 14:32:43 -0700535 if (session->session_state & STATE_FAILED)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500536 {
Dave Wallace7e607a72018-06-18 18:41:32 -0400537 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500538 return VPPCOM_ECONNREFUSED;
539 }
540
Dave Wallace7e607a72018-06-18 18:41:32 -0400541 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400542 }
543 while (clib_time_now (&vcm->clib_time) < timeout);
544
Florin Coras0d427d82018-06-27 03:24:07 -0700545 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
546 vppcom_session_state_str (state));
Florin Coras7e12d942018-06-27 14:32:43 -0700547 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800548
Dave Wallace543852a2017-08-03 02:11:34 -0400549 return VPPCOM_ETIMEDOUT;
550}
551
Dave Wallace543852a2017-08-03 02:11:34 -0400552/*
553 * VPP-API message functions
554 */
555static void
556vppcom_send_session_enable_disable (u8 is_enable)
557{
Dave Wallace543852a2017-08-03 02:11:34 -0400558 vl_api_session_enable_disable_t *bmp;
559 bmp = vl_msg_api_alloc (sizeof (*bmp));
560 memset (bmp, 0, sizeof (*bmp));
561
562 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
563 bmp->client_index = vcm->my_client_index;
564 bmp->context = htonl (0xfeedface);
565 bmp->is_enable = is_enable;
566 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
567}
568
569static int
570vppcom_app_session_enable (void)
571{
Dave Wallace543852a2017-08-03 02:11:34 -0400572 int rv;
573
574 if (vcm->app_state != STATE_APP_ENABLED)
575 {
576 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
577 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
578 if (PREDICT_FALSE (rv))
579 {
Florin Coras0d427d82018-06-27 03:24:07 -0700580 VDBG (0, "VCL<%d>: application session enable timed out! "
581 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -0400582 return rv;
583 }
584 }
585 return VPPCOM_OK;
586}
587
588static void
589 vl_api_session_enable_disable_reply_t_handler
590 (vl_api_session_enable_disable_reply_t * mp)
591{
Dave Wallace543852a2017-08-03 02:11:34 -0400592 if (mp->retval)
593 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500594 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400595 format_api_error, ntohl (mp->retval));
596 }
597 else
598 vcm->app_state = STATE_APP_ENABLED;
599}
600
601static void
602vppcom_app_send_attach (void)
603{
Dave Wallace543852a2017-08-03 02:11:34 -0400604 vl_api_application_attach_t *bmp;
Dave Wallace8af20542017-10-26 03:29:30 -0400605 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
Dave Wallace774169b2017-11-01 20:07:40 -0400606 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
607 vcm->cfg.app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -0400608
Dave Wallace543852a2017-08-03 02:11:34 -0400609 bmp = vl_msg_api_alloc (sizeof (*bmp));
610 memset (bmp, 0, sizeof (*bmp));
611
612 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
613 bmp->client_index = vcm->my_client_index;
614 bmp->context = htonl (0xfeedface);
615 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700616 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
Dave Wallace774169b2017-11-01 20:07:40 -0400617 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
618 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
619 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
620 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700621 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
622 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
Florin Corasff6e7692017-12-11 04:59:01 -0800623 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
624 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
625 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
626 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
Florin Corasf32cff62017-12-09 08:15:00 -0800627 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
628 vcm->cfg.preallocated_fifo_pairs;
Florin Corasff6e7692017-12-11 04:59:01 -0800629 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Dave Wallace8af20542017-10-26 03:29:30 -0400630 if (nsid_len)
631 {
632 bmp->namespace_id_len = nsid_len;
633 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
634 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
635 }
Dave Wallace543852a2017-08-03 02:11:34 -0400636 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
637}
638
639static int
640vppcom_app_attach (void)
641{
Dave Wallace543852a2017-08-03 02:11:34 -0400642 int rv;
643
644 vppcom_app_send_attach ();
645 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
646 if (PREDICT_FALSE (rv))
647 {
Florin Coras0d427d82018-06-27 03:24:07 -0700648 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
649 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -0400650 return rv;
651 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800652
Dave Wallace543852a2017-08-03 02:11:34 -0400653 return VPPCOM_OK;
654}
655
656static void
657vppcom_app_detach (void)
658{
Dave Wallace543852a2017-08-03 02:11:34 -0400659 vl_api_application_detach_t *bmp;
660 bmp = vl_msg_api_alloc (sizeof (*bmp));
661 memset (bmp, 0, sizeof (*bmp));
662
663 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
664 bmp->client_index = vcm->my_client_index;
665 bmp->context = htonl (0xfeedface);
666 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
667}
668
669static void
670vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
671 mp)
672{
Dave Wallace543852a2017-08-03 02:11:34 -0400673 static svm_fifo_segment_create_args_t _a;
674 svm_fifo_segment_create_args_t *a = &_a;
675 int rv;
676
677 memset (a, 0, sizeof (*a));
678 if (mp->retval)
679 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500680 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400681 format_api_error, ntohl (mp->retval));
682 return;
683 }
684
685 if (mp->segment_name_length == 0)
686 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500687 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400688 return;
689 }
690
691 a->segment_name = (char *) mp->segment_name;
692 a->segment_size = mp->segment_size;
693
694 ASSERT (mp->app_event_queue_address);
695
696 /* Attach to the segment vpp created */
697 rv = svm_fifo_segment_attach (a);
698 vec_reset_length (a->new_segment_indices);
699 if (PREDICT_FALSE (rv))
700 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500701 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
702 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400703 return;
704 }
705
706 vcm->app_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -0800707 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400708
709 vcm->app_state = STATE_APP_ATTACHED;
710}
711
712static void
713vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
714 mp)
715{
Dave Wallace543852a2017-08-03 02:11:34 -0400716 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -0500717 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400718 ntohl (mp->retval));
719
720 vcm->app_state = STATE_APP_ENABLED;
721}
722
723static void
724vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
725 mp)
726{
Dave Wallace543852a2017-08-03 02:11:34 -0400727 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -0500728 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
Dave Wallace4878cbe2017-11-21 03:45:09 -0500729 getpid (), mp->handle, format_api_error,
730 ntohl (mp->retval));
Dave Wallace543852a2017-08-03 02:11:34 -0400731}
732
733static void
734vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
735{
Dave Wallace543852a2017-08-03 02:11:34 -0400736 static svm_fifo_segment_create_args_t _a;
737 svm_fifo_segment_create_args_t *a = &_a;
738 int rv;
739
740 memset (a, 0, sizeof (*a));
741 a->segment_name = (char *) mp->segment_name;
742 a->segment_size = mp->segment_size;
743 /* Attach to the segment vpp created */
744 rv = svm_fifo_segment_attach (a);
745 vec_reset_length (a->new_segment_indices);
746 if (PREDICT_FALSE (rv))
747 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500748 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500749 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400750 return;
751 }
Florin Coras0d427d82018-06-27 03:24:07 -0700752
753 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
754 mp->segment_name, mp->segment_size);
Dave Wallace543852a2017-08-03 02:11:34 -0400755}
756
757static void
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800758vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
759{
760
761/*
762 * XXX Need segment_name to session_id hash,
763 * XXX - have sessionID by handle hash currently
764 */
Florin Coras0d427d82018-06-27 03:24:07 -0700765
766 VDBG (1, "Unmapped segment '%s'", mp->segment_name);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800767}
768
769static void
Dave Wallace543852a2017-08-03 02:11:34 -0400770vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
771{
Dave Wallace543852a2017-08-03 02:11:34 -0400772 uword *p;
Dave Wallace543852a2017-08-03 02:11:34 -0400773
774 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
775 if (p)
776 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500777 int rv;
Florin Coras7e12d942018-06-27 14:32:43 -0700778 vcl_session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500779 u32 session_index = p[0];
780
Dave Wallace7e607a72018-06-18 18:41:32 -0400781 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Florin Coras7e12d942018-06-27 14:32:43 -0700782 session->session_state = STATE_CLOSE_ON_EMPTY;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500783
Florin Coras0d427d82018-06-27 03:24:07 -0700784 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: setting state to 0x%x "
Florin Coras7e12d942018-06-27 14:32:43 -0700785 "(%s)", getpid (), mp->handle, session_index,
786 session->session_state,
787 vppcom_session_state_str (session->session_state));
Dave Wallace7e607a72018-06-18 18:41:32 -0400788 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -0500789 return;
790
791 done:
Florin Coras0d427d82018-06-27 03:24:07 -0700792 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session lookup failed!",
793 getpid (), mp->handle, session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400794 }
795 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500796 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500797 "handle failed!", getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400798}
799
800static void
801vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
802{
Florin Coras7e12d942018-06-27 14:32:43 -0700803 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400804 vl_api_reset_session_reply_t *rmp;
805 uword *p;
806 int rv = 0;
807
808 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
809 if (p)
810 {
811 int rval;
Dave Wallace7e607a72018-06-18 18:41:32 -0400812 VCL_SESSION_LOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400813 rval = vppcom_session_at_index (p[0], &session);
814 if (PREDICT_FALSE (rval))
815 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500816 rv = VNET_API_ERROR_INVALID_VALUE_2;
Dave Wallace048b1d62018-01-03 22:24:41 -0500817 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500818 "session lookup failed! returning %d %U",
819 getpid (), mp->handle, p[0],
820 rv, format_api_error, rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400821 }
822 else
Dave Wallace4878cbe2017-11-21 03:45:09 -0500823 {
824 /* TBD: should this disconnect immediately and
825 * flush the fifos?
826 */
Florin Coras7e12d942018-06-27 14:32:43 -0700827 session->session_state = STATE_CLOSE_ON_EMPTY;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500828
Florin Coras0d427d82018-06-27 03:24:07 -0700829 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: state set to %d "
Florin Coras7e12d942018-06-27 14:32:43 -0700830 "(%s)!", getpid (), mp->handle, p[0], session->session_state,
831 vppcom_session_state_str (session->session_state));
Dave Wallace4878cbe2017-11-21 03:45:09 -0500832 }
Dave Wallace7e607a72018-06-18 18:41:32 -0400833 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400834 }
835 else
836 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500837 rv = VNET_API_ERROR_INVALID_VALUE;
Dave Wallace048b1d62018-01-03 22:24:41 -0500838 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500839 "failed! returning %d %U",
840 getpid (), mp->handle, rv, format_api_error, rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400841 }
842
843 rmp = vl_msg_api_alloc (sizeof (*rmp));
844 memset (rmp, 0, sizeof (*rmp));
845 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
846 rmp->retval = htonl (rv);
847 rmp->handle = mp->handle;
848 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
849}
850
851static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400852vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Wallace543852a2017-08-03 02:11:34 -0400853{
Florin Coras7e12d942018-06-27 14:32:43 -0700854 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400855 u32 session_index;
856 svm_fifo_t *rx_fifo, *tx_fifo;
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -0800857 int rv = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400858
Dave Wallace4878cbe2017-11-21 03:45:09 -0500859 session_index = mp->context;
Dave Wallace7e607a72018-06-18 18:41:32 -0400860 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -0500861done:
Dave Wallace543852a2017-08-03 02:11:34 -0400862 if (mp->retval)
863 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500864 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500865 "connect failed! %U",
866 getpid (), mp->handle, session_index,
867 format_api_error, ntohl (mp->retval));
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -0800868 if (session)
Dave Wallaceee45d412017-11-24 21:44:06 -0500869 {
Florin Coras7e12d942018-06-27 14:32:43 -0700870 session->session_state = STATE_FAILED;
Dave Wallaceee45d412017-11-24 21:44:06 -0500871 session->vpp_handle = mp->handle;
872 }
873 else
874 {
875 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
876 "Invalid session index (%u)!",
877 getpid (), mp->handle, session_index);
878 }
879 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400880 }
881
Dave Wallaceee45d412017-11-24 21:44:06 -0500882 if (rv)
883 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400884
Dave Wallace543852a2017-08-03 02:11:34 -0400885 /*
886 * Setup session
887 */
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700888 if (vcm->session_io_thread.io_sessions_lockp)
889 {
890 // Add this connection to the active io sessions list
Dave Wallace7e607a72018-06-18 18:41:32 -0400891 VCL_IO_SESSIONS_LOCK ();
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700892 u32 *active_session_index;
893 pool_get (vcm->session_io_thread.active_session_indexes,
894 active_session_index);
895 *active_session_index = session_index;
Dave Wallace7e607a72018-06-18 18:41:32 -0400896 VCL_IO_SESSIONS_UNLOCK ();
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -0700897 }
Florin Coras7e12d942018-06-27 14:32:43 -0700898 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
899 svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400900
901 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
902 rx_fifo->client_session_index = session_index;
903 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
904 tx_fifo->client_session_index = session_index;
905
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800906 session->rx_fifo = rx_fifo;
907 session->tx_fifo = tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -0500908 session->vpp_handle = mp->handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700909 session->transport.is_ip4 = mp->is_ip4;
910 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
911 sizeof (session->transport.rmt_ip));
912 session->transport.lcl_port = mp->lcl_port;
913 session->session_state = STATE_CONNECT;
Dave Wallace543852a2017-08-03 02:11:34 -0400914
915 /* Add it to lookup table */
916 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Wallace60caa062017-11-10 17:07:13 -0500917
Florin Coras0d427d82018-06-27 03:24:07 -0700918 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
919 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
920 getpid (), mp->handle, session_index, session->rx_fifo,
921 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Dave Wallaceee45d412017-11-24 21:44:06 -0500922done_unlock:
Dave Wallace7e607a72018-06-18 18:41:32 -0400923 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -0400924}
925
926static void
Florin Coras7e12d942018-06-27 14:32:43 -0700927vppcom_send_connect_sock (vcl_session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -0400928{
Dave Wallace543852a2017-08-03 02:11:34 -0400929 vl_api_connect_sock_t *cmp;
930
931 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
Dave Wallace543852a2017-08-03 02:11:34 -0400932 cmp = vl_msg_api_alloc (sizeof (*cmp));
933 memset (cmp, 0, sizeof (*cmp));
934 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
935 cmp->client_index = vcm->my_client_index;
Dave Wallace33e002b2017-09-06 01:20:02 -0400936 cmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -0400937
Florin Coras7e12d942018-06-27 14:32:43 -0700938 cmp->is_ip4 = session->transport.is_ip4;
939 clib_memcpy (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
940 cmp->port = session->transport.rmt_port;
941 cmp->proto = session->session_type;
Dave Wallace543852a2017-08-03 02:11:34 -0400942 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
943 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
944}
945
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400946static inline void
Dave Wallace4878cbe2017-11-21 03:45:09 -0500947vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
948 int rv)
949{
950 vl_api_disconnect_session_reply_t *rmp;
951
Florin Coras0d427d82018-06-27 03:24:07 -0700952 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
953 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500954
955 rmp = vl_msg_api_alloc (sizeof (*rmp));
956 memset (rmp, 0, sizeof (*rmp));
957
958 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
959 rmp->retval = htonl (rv);
960 rmp->handle = vpp_handle;
961 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
962}
963
964static inline void
965vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -0400966{
Dave Wallace543852a2017-08-03 02:11:34 -0400967 vl_api_disconnect_session_t *dmp;
Dave Wallace543852a2017-08-03 02:11:34 -0400968
Florin Coras0d427d82018-06-27 03:24:07 -0700969 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect msg",
970 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500971
Dave Wallace543852a2017-08-03 02:11:34 -0400972 dmp = vl_msg_api_alloc (sizeof (*dmp));
973 memset (dmp, 0, sizeof (*dmp));
974 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
975 dmp->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500976 dmp->handle = vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400977 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
Dave Wallace543852a2017-08-03 02:11:34 -0400978}
979
980static void
981vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
982{
Florin Coras7e12d942018-06-27 14:32:43 -0700983 vcl_session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500984 u32 session_index = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -0400985 int rv;
986
Dave Wallace7e607a72018-06-18 18:41:32 -0400987 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -0500988done:
Dave Wallace543852a2017-08-03 02:11:34 -0400989 if (mp->retval)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500990 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500991 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
992 "sid %u: bind failed: %U",
993 getpid (), mp->handle, session_index,
994 format_api_error, ntohl (mp->retval));
Dave Wallace4878cbe2017-11-21 03:45:09 -0500995 rv = vppcom_session_at_index (session_index, &session);
996 if (rv == VPPCOM_OK)
Dave Wallaceee45d412017-11-24 21:44:06 -0500997 {
Florin Coras7e12d942018-06-27 14:32:43 -0700998 session->session_state = STATE_FAILED;
Dave Wallaceee45d412017-11-24 21:44:06 -0500999 session->vpp_handle = mp->handle;
1000 }
1001 else
1002 {
1003 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1004 "Invalid session index (%u)!",
1005 getpid (), mp->handle, session_index);
1006 }
1007 goto done_unlock;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001008 }
Dave Wallace543852a2017-08-03 02:11:34 -04001009
Dave Wallaceee45d412017-11-24 21:44:06 -05001010 session->vpp_handle = mp->handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001011 session->transport.is_ip4 = mp->lcl_is_ip4;
1012 session->transport.lcl_ip = to_ip46 (mp->lcl_is_ip4 ? IP46_TYPE_IP4 :
1013 IP46_TYPE_IP6, mp->lcl_ip);
1014 session->transport.lcl_port = mp->lcl_port;
Dave Wallaceee45d412017-11-24 21:44:06 -05001015 vppcom_session_table_add_listener (mp->handle, session_index);
Florin Coras7e12d942018-06-27 14:32:43 -07001016 session->session_state = STATE_LISTEN;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001017
Florin Coras0d427d82018-06-27 03:24:07 -07001018 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1019 getpid (), mp->handle, mp->context);
Dave Wallaceee45d412017-11-24 21:44:06 -05001020done_unlock:
Dave Wallace7e607a72018-06-18 18:41:32 -04001021 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -04001022}
1023
1024static void
1025vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1026{
Dave Wallace4878cbe2017-11-21 03:45:09 -05001027 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -05001028 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001029 getpid (), mp->context, format_api_error,
1030 ntohl (mp->retval));
Dave Wallace543852a2017-08-03 02:11:34 -04001031
Florin Coras0d427d82018-06-27 03:24:07 -07001032 else
1033 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
Dave Wallace543852a2017-08-03 02:11:34 -04001034}
1035
1036u8 *
1037format_ip4_address (u8 * s, va_list * args)
1038{
1039 u8 *a = va_arg (*args, u8 *);
1040 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1041}
1042
1043u8 *
1044format_ip6_address (u8 * s, va_list * args)
1045{
1046 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1047 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1048
1049 i_max_n_zero = ARRAY_LEN (a->as_u16);
1050 max_n_zeros = 0;
1051 i_first_zero = i_max_n_zero;
1052 n_zeros = 0;
1053 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1054 {
1055 u32 is_zero = a->as_u16[i] == 0;
1056 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1057 {
1058 i_first_zero = i;
1059 n_zeros = 0;
1060 }
1061 n_zeros += is_zero;
1062 if ((!is_zero && n_zeros > max_n_zeros)
1063 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1064 {
1065 i_max_n_zero = i_first_zero;
1066 max_n_zeros = n_zeros;
1067 i_first_zero = ARRAY_LEN (a->as_u16);
1068 n_zeros = 0;
1069 }
1070 }
1071
1072 last_double_colon = 0;
1073 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1074 {
1075 if (i == i_max_n_zero && max_n_zeros > 1)
1076 {
1077 s = format (s, "::");
1078 i += max_n_zeros - 1;
1079 last_double_colon = 1;
1080 }
1081 else
1082 {
1083 s = format (s, "%s%x",
1084 (last_double_colon || i == 0) ? "" : ":",
1085 clib_net_to_host_u16 (a->as_u16[i]));
1086 last_double_colon = 0;
1087 }
1088 }
1089
1090 return s;
1091}
1092
1093/* Format an IP46 address. */
1094u8 *
1095format_ip46_address (u8 * s, va_list * args)
1096{
1097 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1098 ip46_type_t type = va_arg (*args, ip46_type_t);
1099 int is_ip4 = 1;
1100
1101 switch (type)
1102 {
1103 case IP46_TYPE_ANY:
1104 is_ip4 = ip46_address_is_ip4 (ip46);
1105 break;
1106 case IP46_TYPE_IP4:
1107 is_ip4 = 1;
1108 break;
1109 case IP46_TYPE_IP6:
1110 is_ip4 = 0;
1111 break;
1112 }
1113
1114 return is_ip4 ?
1115 format (s, "%U", format_ip4_address, &ip46->ip4) :
1116 format (s, "%U", format_ip6_address, &ip46->ip6);
1117}
1118
1119static void
1120vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1121{
Dave Wallace543852a2017-08-03 02:11:34 -04001122 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras7e12d942018-06-27 14:32:43 -07001123 vcl_session_t *session, *listen_session;
Dave Wallace543852a2017-08-03 02:11:34 -04001124 u32 session_index;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001125 vce_event_connect_request_t *ecr;
1126 vce_event_t *ev;
1127 int rv;
1128 u32 ev_idx;
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001129 uword elts = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001130
Dave Wallace7e607a72018-06-18 18:41:32 -04001131 VCL_SESSION_LOCK ();
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001132
Dave Wallace7e607a72018-06-18 18:41:32 -04001133 VCL_ACCEPT_FIFO_LOCK ();
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001134 elts = clib_fifo_free_elts (vcm->client_session_index_fifo);
Dave Wallace7e607a72018-06-18 18:41:32 -04001135 VCL_ACCEPT_FIFO_UNLOCK ();
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001136
1137 if (!elts)
Dave Wallace543852a2017-08-03 02:11:34 -04001138 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001139 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
Dave Wallaced2931962017-11-25 04:17:39 -05001140 vppcom_send_accept_session_reply (mp->handle, mp->context,
Florin Corasdcf55ce2017-11-16 15:32:50 -08001141 VNET_API_ERROR_QUEUE_FULL);
Dave Wallace7e607a72018-06-18 18:41:32 -04001142 VCL_SESSION_UNLOCK ();
Florin Corasdcf55ce2017-11-16 15:32:50 -08001143 return;
1144 }
1145
1146 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1147 if (!listen_session)
1148 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001149 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1150 "unknown vpp listener handle %llx",
1151 getpid (), mp->listener_handle);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001152 vppcom_send_accept_session_reply (mp->handle, mp->context,
1153 VNET_API_ERROR_INVALID_ARGUMENT);
Dave Wallace7e607a72018-06-18 18:41:32 -04001154 VCL_SESSION_UNLOCK ();
Dave Wallace60caa062017-11-10 17:07:13 -05001155 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001156 }
1157
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001158 /* TODO check listener depth and update */
1159 /* TODO on "child" fd close, update listener depth */
1160
Dave Wallace543852a2017-08-03 02:11:34 -04001161 /* Allocate local session and set it up */
1162 pool_get (vcm->sessions, session);
1163 memset (session, 0, sizeof (*session));
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001164 session_index = (u32) (session - vcm->sessions);
Dave Wallace543852a2017-08-03 02:11:34 -04001165
1166 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1167 rx_fifo->client_session_index = session_index;
1168 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1169 tx_fifo->client_session_index = session_index;
1170
Dave Wallace60caa062017-11-10 17:07:13 -05001171 session->vpp_handle = mp->handle;
Dave Wallaced2931962017-11-25 04:17:39 -05001172 session->client_context = mp->context;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001173 session->rx_fifo = rx_fifo;
1174 session->tx_fifo = tx_fifo;
Florin Coras7e12d942018-06-27 14:32:43 -07001175 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
1176 svm_queue_t *);
1177 session->session_state = STATE_ACCEPT;
1178 session->transport.rmt_port = mp->port;
1179 session->transport.is_ip4 = mp->is_ip4;
1180 session->transport.rmt_ip = to_ip46 (mp->is_ip4 ? IP46_TYPE_IP4 :
1181 IP46_TYPE_IP6, mp->ip);
Dave Wallace543852a2017-08-03 02:11:34 -04001182
1183 /* Add it to lookup table */
1184 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Florin Coras7e12d942018-06-27 14:32:43 -07001185 session->transport.lcl_port = listen_session->transport.lcl_port;
1186 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Dave Wallace227867f2017-11-13 21:21:53 -05001187
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001188 /* Create an event for handlers */
1189
Dave Wallace7e607a72018-06-18 18:41:32 -04001190 VCL_EVENTS_LOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001191
1192 pool_get (vcm->event_thread.vce_events, ev);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001193 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001194 ecr = vce_get_event_data (ev, sizeof (*ecr));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001195 ev->evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1196 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1197 ev->evk.session_index = (u32) (listen_session - vcm->sessions);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001198 ecr->accepted_session_index = session_index;
1199
Dave Wallace7e607a72018-06-18 18:41:32 -04001200 VCL_EVENTS_UNLOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001201
1202 rv = vce_generate_event (&vcm->event_thread, ev_idx);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001203 ASSERT (rv == 0);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001204
Florin Coras0d427d82018-06-27 03:24:07 -07001205 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
1206 " address %U port %d queue %p!", getpid (), mp->handle, session_index,
1207 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
1208 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001209 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001210
Florin Coras0d427d82018-06-27 03:24:07 -07001211 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04001212 VCL_SESSION_UNLOCK ();
Dave Wallace60caa062017-11-10 17:07:13 -05001213}
1214
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001215/* VPP combines bind and listen as one operation. VCL manages the separation
1216 * of bind and listen locally via vppcom_session_bind() and
1217 * vppcom_session_listen() */
Dave Wallace60caa062017-11-10 17:07:13 -05001218static void
Florin Coras7e12d942018-06-27 14:32:43 -07001219vppcom_send_bind_sock (vcl_session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001220{
Dave Wallace543852a2017-08-03 02:11:34 -04001221 vl_api_bind_sock_t *bmp;
1222
1223 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace543852a2017-08-03 02:11:34 -04001224 bmp = vl_msg_api_alloc (sizeof (*bmp));
1225 memset (bmp, 0, sizeof (*bmp));
1226
1227 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1228 bmp->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001229 bmp->context = session_index;
Florin Coras7e12d942018-06-27 14:32:43 -07001230 bmp->is_ip4 = session->transport.is_ip4;
1231 clib_memcpy (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
1232 bmp->port = session->transport.lcl_port;
1233 bmp->proto = session->session_type;
Dave Wallace543852a2017-08-03 02:11:34 -04001234 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1235 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1236}
1237
1238static void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001239vppcom_send_unbind_sock (u64 vpp_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001240{
Dave Wallace543852a2017-08-03 02:11:34 -04001241 vl_api_unbind_sock_t *ump;
Dave Wallace543852a2017-08-03 02:11:34 -04001242
1243 ump = vl_msg_api_alloc (sizeof (*ump));
1244 memset (ump, 0, sizeof (*ump));
1245
1246 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1247 ump->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001248 ump->handle = vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001249 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1250}
1251
1252static int
Dave Wallace543852a2017-08-03 02:11:34 -04001253vppcom_session_unbind (u32 session_index)
1254{
Florin Coras7e12d942018-06-27 14:32:43 -07001255 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001256 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001257 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001258
Dave Wallace7e607a72018-06-18 18:41:32 -04001259 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001260
1261 vpp_handle = session->vpp_handle;
1262 vppcom_session_table_del_listener (vpp_handle);
1263 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -07001264 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001265
Dave Wallace7e607a72018-06-18 18:41:32 -04001266 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -04001267
Florin Coras0d427d82018-06-27 03:24:07 -07001268 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
1269 " 0x%x (%s)", getpid (), vpp_handle, session_index, STATE_DISCONNECT,
1270 vppcom_session_state_str (STATE_DISCONNECT));
1271 vcl_evt (VCL_EVT_UNBIND, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001272 vppcom_send_unbind_sock (vpp_handle);
1273
1274done:
1275 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001276}
1277
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001278static inline int
Dave Wallace543852a2017-08-03 02:11:34 -04001279vppcom_session_disconnect (u32 session_index)
1280{
Dave Wallace543852a2017-08-03 02:11:34 -04001281 int rv;
Florin Coras7e12d942018-06-27 14:32:43 -07001282 vcl_session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001283 u64 vpp_handle;
1284 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04001285
Dave Wallace7e607a72018-06-18 18:41:32 -04001286 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001287
1288 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001289 state = session->session_state;
Dave Wallace7e607a72018-06-18 18:41:32 -04001290 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05001291
Florin Coras0d427d82018-06-27 03:24:07 -07001292 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
1293 vpp_handle, session_index, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001294
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001295 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001296 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001297 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001298 "Cannot disconnect a listen socket!",
1299 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001300 rv = VPPCOM_EBADFD;
1301 goto done;
1302 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001303
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001304 /* The peer has already initiated the close,
1305 * so send the disconnect session reply.
Dave Wallace4878cbe2017-11-21 03:45:09 -05001306 */
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001307 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001308 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001309 //XXX alagalah - Check and drain here?
1310 vppcom_send_disconnect_session_reply (vpp_handle,
1311 session_index, 0 /* rv */ );
Florin Coras0d427d82018-06-27 03:24:07 -07001312 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
1313 "REPLY...", getpid (), vpp_handle, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001314 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001315
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001316 /* Otherwise, send a disconnect session msg...
Dave Wallace4878cbe2017-11-21 03:45:09 -05001317 */
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001318 else
Dave Wallace227867f2017-11-13 21:21:53 -05001319 {
Florin Coras0d427d82018-06-27 03:24:07 -07001320 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
1321 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001322
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001323 vppcom_send_disconnect_session (vpp_handle, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -05001324 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001325
Dave Wallace4878cbe2017-11-21 03:45:09 -05001326done:
1327 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001328}
1329
1330#define foreach_sock_msg \
1331_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1332_(BIND_SOCK_REPLY, bind_sock_reply) \
1333_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1334_(ACCEPT_SESSION, accept_session) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001335_(CONNECT_SESSION_REPLY, connect_session_reply) \
Dave Wallace543852a2017-08-03 02:11:34 -04001336_(DISCONNECT_SESSION, disconnect_session) \
1337_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1338_(RESET_SESSION, reset_session) \
1339_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1340_(APPLICATION_DETACH_REPLY, application_detach_reply) \
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001341_(MAP_ANOTHER_SEGMENT, map_another_segment) \
1342_(UNMAP_SEGMENT, unmap_segment)
Dave Wallace543852a2017-08-03 02:11:34 -04001343
1344static void
1345vppcom_api_hookup (void)
1346{
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001347#define _(N, n) \
Dave Wallace543852a2017-08-03 02:11:34 -04001348 vl_msg_api_set_handlers(VL_API_##N, #n, \
1349 vl_api_##n##_t_handler, \
1350 vl_noop_handler, \
1351 vl_api_##n##_t_endian, \
1352 vl_api_##n##_t_print, \
1353 sizeof(vl_api_##n##_t), 1);
1354 foreach_sock_msg;
1355#undef _
1356}
1357
1358static void
1359vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1360{
1361 ASSERT (vcl_cfg);
1362
1363 vcl_cfg->heapsize = (256ULL << 20);
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001364 vcl_cfg->vpp_api_q_length = 1024;
Dave Wallace543852a2017-08-03 02:11:34 -04001365 vcl_cfg->segment_baseva = 0x200000000ULL;
1366 vcl_cfg->segment_size = (256 << 20);
1367 vcl_cfg->add_segment_size = (128 << 20);
1368 vcl_cfg->preallocated_fifo_pairs = 8;
1369 vcl_cfg->rx_fifo_size = (1 << 20);
1370 vcl_cfg->tx_fifo_size = (1 << 20);
1371 vcl_cfg->event_queue_size = 2048;
1372 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1373 vcl_cfg->app_timeout = 10 * 60.0;
1374 vcl_cfg->session_timeout = 10 * 60.0;
1375 vcl_cfg->accept_timeout = 60.0;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001376 vcl_cfg->event_ring_size = (128 << 10);
1377 vcl_cfg->event_log_path = "/dev/shm";
Dave Wallace543852a2017-08-03 02:11:34 -04001378}
1379
1380static void
1381vppcom_cfg_heapsize (char *conf_fname)
1382{
Dave Wallace543852a2017-08-03 02:11:34 -04001383 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1384 FILE *fp;
1385 char inbuf[4096];
1386 int argc = 1;
1387 char **argv = NULL;
1388 char *arg = NULL;
1389 char *p;
1390 int i;
1391 u8 *sizep;
1392 u32 size;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001393 void *vcl_mem;
1394 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001395
1396 fp = fopen (conf_fname, "r");
1397 if (fp == NULL)
1398 {
Florin Coras0d427d82018-06-27 03:24:07 -07001399 VDBG (0, "VCL<%d>: using default heapsize %lld (0x%llx)", getpid (),
1400 vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001401 goto defaulted;
1402 }
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001403
Dave Wallace543852a2017-08-03 02:11:34 -04001404 argv = calloc (1, sizeof (char *));
1405 if (argv == NULL)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001406 {
Florin Coras0d427d82018-06-27 03:24:07 -07001407 VDBG (0, "VCL<%d>: calloc failed, using default heapsize %lld (0x%llx)",
1408 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001409 goto defaulted;
1410 }
Dave Wallace543852a2017-08-03 02:11:34 -04001411
1412 while (1)
1413 {
1414 if (fgets (inbuf, 4096, fp) == 0)
1415 break;
1416 p = strtok (inbuf, " \t\n");
1417 while (p != NULL)
1418 {
1419 if (*p == '#')
1420 break;
1421 argc++;
1422 char **tmp = realloc (argv, argc * sizeof (char *));
1423 if (tmp == NULL)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001424 {
Florin Coras0d427d82018-06-27 03:24:07 -07001425 VDBG (0, "VCL<%d>: realloc failed, using default heapsize %lld "
1426 "(0x%llx)", getpid (), vcl_cfg->heapsize,
1427 vcl_cfg->heapsize);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001428 goto defaulted;
1429 }
Dave Wallace543852a2017-08-03 02:11:34 -04001430 argv = tmp;
1431 arg = strndup (p, 1024);
1432 if (arg == NULL)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001433 {
Florin Coras0d427d82018-06-27 03:24:07 -07001434 VDBG (0, "VCL<%d>: strndup failed, using default heapsize %lld "
1435 "(0x%llx)", getpid (), vcl_cfg->heapsize,
1436 vcl_cfg->heapsize);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001437 goto defaulted;
1438 }
Dave Wallace543852a2017-08-03 02:11:34 -04001439 argv[argc - 1] = arg;
1440 p = strtok (NULL, " \t\n");
1441 }
1442 }
1443
1444 fclose (fp);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001445 fp = NULL;
Dave Wallace543852a2017-08-03 02:11:34 -04001446
1447 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1448 if (tmp == NULL)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001449 {
Florin Coras0d427d82018-06-27 03:24:07 -07001450 VDBG (0, "VCL<%d>: realloc failed, using default heapsize %lld "
1451 "(0x%llx)", getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001452 goto defaulted;
1453 }
Dave Wallace543852a2017-08-03 02:11:34 -04001454 argv = tmp;
1455 argv[argc] = NULL;
1456
1457 /*
1458 * Look for and parse the "heapsize" config parameter.
1459 * Manual since none of the clib infra has been bootstrapped yet.
1460 *
1461 * Format: heapsize <nn>[mM][gG]
1462 */
1463
1464 for (i = 1; i < (argc - 1); i++)
1465 {
1466 if (!strncmp (argv[i], "heapsize", 8))
1467 {
1468 sizep = (u8 *) argv[i + 1];
1469 size = 0;
1470 while (*sizep >= '0' && *sizep <= '9')
1471 {
1472 size *= 10;
1473 size += *sizep++ - '0';
1474 }
1475 if (size == 0)
1476 {
Florin Coras0d427d82018-06-27 03:24:07 -07001477 VDBG (0, "VCL<%d>: parse error '%s %s', using default "
1478 "heapsize %lld (0x%llx)", getpid (), argv[i], argv[i + 1],
1479 vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001480 goto defaulted;
1481 }
1482
1483 if (*sizep == 'g' || *sizep == 'G')
1484 vcl_cfg->heapsize = size << 30;
1485 else if (*sizep == 'm' || *sizep == 'M')
1486 vcl_cfg->heapsize = size << 20;
1487 else
1488 {
Florin Coras0d427d82018-06-27 03:24:07 -07001489 VDBG (0, "VCL<%d>: parse error '%s %s', using default "
1490 "heapsize %lld (0x%llx)", getpid (), argv[i], argv[i + 1],
1491 vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001492 goto defaulted;
1493 }
1494 }
1495 }
1496
1497defaulted:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001498 if (fp != NULL)
1499 fclose (fp);
1500 if (argv != NULL)
1501 free (argv);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001502
Dave Wallace2e005bb2017-11-07 01:21:39 -05001503 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1504 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Steven0cdd5bd2017-11-08 14:14:45 -08001505 if (vcl_mem == MAP_FAILED)
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001506 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001507 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001508 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1509 "-1, 0) failed!",
1510 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001511 ASSERT (vcl_mem != MAP_FAILED);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001512 return;
1513 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05001514 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1515 if (!heap)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001516 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001517 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001518 ASSERT (heap);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001519 return;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001520 }
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001521 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1522 if (!vcl_mem)
1523 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001524 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001525 ASSERT (vcl_mem);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001526 return;
1527 }
1528
1529 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1530 vcm = vcl_mem;
1531
Florin Coras0d427d82018-06-27 03:24:07 -07001532 VDBG (0, "VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)", getpid (),
1533 heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001534}
1535
1536static void
1537vppcom_cfg_read (char *conf_fname)
1538{
Dave Wallace543852a2017-08-03 02:11:34 -04001539 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1540 int fd;
1541 unformat_input_t _input, *input = &_input;
1542 unformat_input_t _line_input, *line_input = &_line_input;
1543 u8 vc_cfg_input = 0;
1544 u8 *chroot_path;
1545 struct stat s;
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001546 u32 uid, gid, q_len;
Dave Wallace543852a2017-08-03 02:11:34 -04001547
1548 fd = open (conf_fname, O_RDONLY);
1549 if (fd < 0)
1550 {
Florin Coras0d427d82018-06-27 03:24:07 -07001551 VDBG (0, "VCL<%d>: using default configuration.", getpid (),
1552 conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001553 goto file_done;
1554 }
1555
1556 if (fstat (fd, &s) < 0)
1557 {
Florin Coras0d427d82018-06-27 03:24:07 -07001558 VDBG (0, "VCL<%d>: failed to stat `%s', using default configuration",
1559 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001560 goto file_done;
1561 }
1562
1563 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1564 {
Florin Coras0d427d82018-06-27 03:24:07 -07001565 VDBG (0, "VCL<%d>: not a regular file `%s', using default "
1566 "configuration", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001567 goto file_done;
1568 }
1569
Dave Barach59b25652017-09-10 15:04:27 -04001570 unformat_init_clib_file (input, fd);
Dave Wallace543852a2017-08-03 02:11:34 -04001571
1572 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1573 {
Chris Lukeb2bcad62017-09-18 08:51:22 -04001574 (void) unformat_user (input, unformat_line_input, line_input);
Dave Wallace543852a2017-08-03 02:11:34 -04001575 unformat_skip_white_space (line_input);
1576
Dave Wallace8af20542017-10-26 03:29:30 -04001577 if (unformat (line_input, "vcl {"))
Dave Wallace543852a2017-08-03 02:11:34 -04001578 {
1579 vc_cfg_input = 1;
1580 continue;
1581 }
1582
1583 if (vc_cfg_input)
1584 {
1585 if (unformat (line_input, "heapsize %s", &chroot_path))
1586 {
1587 vec_terminate_c_string (chroot_path);
Florin Coras0d427d82018-06-27 03:24:07 -07001588 VDBG (0, "VCL<%d>: configured heapsize %s, actual heapsize %lld"
1589 " (0x%llx)", getpid (), chroot_path, vcl_cfg->heapsize,
1590 vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001591 vec_free (chroot_path);
1592 }
1593 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1594 {
1595 vec_terminate_c_string (chroot_path);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001596 if (vcl_cfg->vpp_api_filename)
1597 vec_free (vcl_cfg->vpp_api_filename);
1598 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1599 chroot_path, 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001600 vl_set_memory_root_path ((char *) chroot_path);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001601
Florin Coras0d427d82018-06-27 03:24:07 -07001602 VDBG (0, "VCL<%d>: configured api-prefix (%s) and api filename"
1603 " (%s)", getpid (), chroot_path,
1604 vcl_cfg->vpp_api_filename);
Dave Wallace543852a2017-08-03 02:11:34 -04001605 chroot_path = 0; /* Don't vec_free() it! */
1606 }
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001607 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1608 {
1609 if (q_len < vcl_cfg->vpp_api_q_length)
1610 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001611 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001612 "(%u) is too small! Using default: %u ",
1613 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1614 }
1615 else
1616 {
1617 vcl_cfg->vpp_api_q_length = q_len;
1618
Florin Coras0d427d82018-06-27 03:24:07 -07001619 VDBG (0, "VCL<%d>: configured vpp-api-q-length %u",
1620 getpid (), vcl_cfg->vpp_api_q_length);
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001621 }
1622 }
Dave Wallace543852a2017-08-03 02:11:34 -04001623 else if (unformat (line_input, "uid %d", &uid))
1624 {
1625 vl_set_memory_uid (uid);
Florin Coras0d427d82018-06-27 03:24:07 -07001626 VDBG (0, "VCL<%d>: configured uid %d", getpid (), uid);
Dave Wallace543852a2017-08-03 02:11:34 -04001627 }
1628 else if (unformat (line_input, "gid %d", &gid))
1629 {
1630 vl_set_memory_gid (gid);
Florin Coras0d427d82018-06-27 03:24:07 -07001631 VDBG (0, "VCL<%d>: configured gid %d", getpid (), gid);
Dave Wallace543852a2017-08-03 02:11:34 -04001632 }
Dave Wallace8af20542017-10-26 03:29:30 -04001633 else if (unformat (line_input, "segment-baseva 0x%lx",
Dave Wallace543852a2017-08-03 02:11:34 -04001634 &vcl_cfg->segment_baseva))
1635 {
Florin Coras0d427d82018-06-27 03:24:07 -07001636 VDBG (0, "VCL<%d>: configured segment_baseva 0x%lx", getpid (),
1637 vcl_cfg->segment_baseva);
Dave Wallace543852a2017-08-03 02:11:34 -04001638 }
1639 else if (unformat (line_input, "segment-size 0x%lx",
1640 &vcl_cfg->segment_size))
1641 {
Florin Coras0d427d82018-06-27 03:24:07 -07001642 VDBG (0, "VCL<%d>: configured segment_size 0x%lx (%ld)",
1643 getpid (), vcl_cfg->segment_size, vcl_cfg->segment_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001644 }
1645 else if (unformat (line_input, "segment-size %ld",
1646 &vcl_cfg->segment_size))
1647 {
Florin Coras0d427d82018-06-27 03:24:07 -07001648 VDBG (0, "VCL<%d>: configured segment_size %ld (0x%lx)",
1649 getpid (), vcl_cfg->segment_size, vcl_cfg->segment_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001650 }
1651 else if (unformat (line_input, "add-segment-size 0x%lx",
1652 &vcl_cfg->add_segment_size))
1653 {
Florin Coras0d427d82018-06-27 03:24:07 -07001654 VDBG (0, "VCL<%d>: configured add_segment_size 0x%lx (%ld)",
1655 getpid (), vcl_cfg->add_segment_size,
1656 vcl_cfg->add_segment_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001657 }
1658 else if (unformat (line_input, "add-segment-size %ld",
1659 &vcl_cfg->add_segment_size))
1660 {
Florin Coras0d427d82018-06-27 03:24:07 -07001661 VDBG (0, "VCL<%d>: configured add_segment_size %ld (0x%lx)",
1662 getpid (), vcl_cfg->add_segment_size,
1663 vcl_cfg->add_segment_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001664 }
1665 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1666 &vcl_cfg->preallocated_fifo_pairs))
1667 {
Florin Coras0d427d82018-06-27 03:24:07 -07001668 VDBG (0, "VCL<%d>: configured preallocated_fifo_pairs %d "
1669 "(0x%x)", getpid (), vcl_cfg->preallocated_fifo_pairs,
1670 vcl_cfg->preallocated_fifo_pairs);
Dave Wallace543852a2017-08-03 02:11:34 -04001671 }
1672 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1673 &vcl_cfg->rx_fifo_size))
1674 {
Florin Coras0d427d82018-06-27 03:24:07 -07001675 VDBG (0, "VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
1676 getpid (), vcl_cfg->rx_fifo_size, vcl_cfg->rx_fifo_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001677 }
1678 else if (unformat (line_input, "rx-fifo-size %ld",
1679 &vcl_cfg->rx_fifo_size))
1680 {
Florin Coras0d427d82018-06-27 03:24:07 -07001681 VDBG (0, "VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
1682 getpid (), vcl_cfg->rx_fifo_size, vcl_cfg->rx_fifo_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001683 }
1684 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1685 &vcl_cfg->tx_fifo_size))
1686 {
Florin Coras0d427d82018-06-27 03:24:07 -07001687 VDBG (0, "VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
1688 getpid (), vcl_cfg->tx_fifo_size, vcl_cfg->tx_fifo_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001689 }
1690 else if (unformat (line_input, "tx-fifo-size %ld",
1691 &vcl_cfg->tx_fifo_size))
1692 {
Florin Coras0d427d82018-06-27 03:24:07 -07001693 VDBG (0, "VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
1694 getpid (), vcl_cfg->tx_fifo_size, vcl_cfg->tx_fifo_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001695 }
1696 else if (unformat (line_input, "event-queue-size 0x%lx",
1697 &vcl_cfg->event_queue_size))
1698 {
Florin Coras0d427d82018-06-27 03:24:07 -07001699 VDBG (0, "VCL<%d>: configured event_queue_size 0x%lx (%ld)",
1700 getpid (), vcl_cfg->event_queue_size,
1701 vcl_cfg->event_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001702 }
1703 else if (unformat (line_input, "event-queue-size %ld",
1704 &vcl_cfg->event_queue_size))
1705 {
Florin Coras0d427d82018-06-27 03:24:07 -07001706 VDBG (0, "VCL<%d>: configured event_queue_size %ld (0x%lx)",
1707 getpid (), vcl_cfg->event_queue_size,
1708 vcl_cfg->event_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001709 }
1710 else if (unformat (line_input, "listen-queue-size 0x%lx",
1711 &vcl_cfg->listen_queue_size))
1712 {
Florin Coras0d427d82018-06-27 03:24:07 -07001713 VDBG (0, "VCL<%d>: configured listen_queue_size 0x%lx (%ld)",
1714 getpid (), vcl_cfg->listen_queue_size,
1715 vcl_cfg->listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001716 }
1717 else if (unformat (line_input, "listen-queue-size %ld",
1718 &vcl_cfg->listen_queue_size))
1719 {
Florin Coras0d427d82018-06-27 03:24:07 -07001720 VDBG (0, "VCL<%d>: configured listen_queue_size %ld (0x%lx)",
1721 getpid (), vcl_cfg->listen_queue_size,
1722 vcl_cfg->listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001723 }
1724 else if (unformat (line_input, "app-timeout %f",
1725 &vcl_cfg->app_timeout))
1726 {
Florin Coras0d427d82018-06-27 03:24:07 -07001727 VDBG (0, "VCL<%d>: configured app_timeout %f",
1728 getpid (), vcl_cfg->app_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001729 }
1730 else if (unformat (line_input, "session-timeout %f",
1731 &vcl_cfg->session_timeout))
1732 {
Florin Coras0d427d82018-06-27 03:24:07 -07001733 VDBG (0, "VCL<%d>: configured session_timeout %f",
1734 getpid (), vcl_cfg->session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001735 }
1736 else if (unformat (line_input, "accept-timeout %f",
1737 &vcl_cfg->accept_timeout))
1738 {
Florin Coras0d427d82018-06-27 03:24:07 -07001739 VDBG (0, "VCL<%d>: configured accept_timeout %f",
1740 getpid (), vcl_cfg->accept_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001741 }
Dave Wallace774169b2017-11-01 20:07:40 -04001742 else if (unformat (line_input, "app-proxy-transport-tcp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001743 {
Dave Wallace774169b2017-11-01 20:07:40 -04001744 vcl_cfg->app_proxy_transport_tcp = 1;
Florin Coras0d427d82018-06-27 03:24:07 -07001745 VDBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%d)",
1746 getpid (), vcl_cfg->app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001747 }
Dave Wallace774169b2017-11-01 20:07:40 -04001748 else if (unformat (line_input, "app-proxy-transport-udp"))
Dave Wallace8af20542017-10-26 03:29:30 -04001749 {
Dave Wallace774169b2017-11-01 20:07:40 -04001750 vcl_cfg->app_proxy_transport_udp = 1;
Florin Coras0d427d82018-06-27 03:24:07 -07001751 VDBG (0, "VCL<%d>: configured app_proxy_transport_udp (%d)",
1752 getpid (), vcl_cfg->app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -04001753 }
Dave Wallace774169b2017-11-01 20:07:40 -04001754 else if (unformat (line_input, "app-scope-local"))
Dave Wallace8af20542017-10-26 03:29:30 -04001755 {
Dave Wallace774169b2017-11-01 20:07:40 -04001756 vcl_cfg->app_scope_local = 1;
Florin Coras0d427d82018-06-27 03:24:07 -07001757 VDBG (0, "VCL<%d>: configured app_scope_local (%d)",
1758 getpid (), vcl_cfg->app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04001759 }
1760 else if (unformat (line_input, "app-scope-global"))
1761 {
1762 vcl_cfg->app_scope_global = 1;
Florin Coras0d427d82018-06-27 03:24:07 -07001763 VDBG (0, "VCL<%d>: configured app_scope_global (%d)",
1764 getpid (), vcl_cfg->app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001765 }
1766 else if (unformat (line_input, "namespace-secret %lu",
1767 &vcl_cfg->namespace_secret))
1768 {
Florin Coras0d427d82018-06-27 03:24:07 -07001769 VDBG (0, "VCL<%d>: configured namespace_secret %lu (0x%lx)",
1770 getpid (), vcl_cfg->namespace_secret,
1771 vcl_cfg->namespace_secret);
Dave Wallace8af20542017-10-26 03:29:30 -04001772 }
1773 else if (unformat (line_input, "namespace-id %v",
1774 &vcl_cfg->namespace_id))
1775 {
1776 vl_api_application_attach_t *mp;
1777 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1778 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1779 if (nsid_vec_len > max_nsid_vec_len)
1780 {
1781 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
Florin Coras0d427d82018-06-27 03:24:07 -07001782 VDBG (0, "VCL<%d>: configured namespace_id is too long,"
1783 " truncated to %d characters!",
1784 getpid (), max_nsid_vec_len);
Dave Wallace8af20542017-10-26 03:29:30 -04001785 }
1786
Florin Coras0d427d82018-06-27 03:24:07 -07001787 VDBG (0, "VCL<%d>: configured namespace_id %v",
1788 getpid (), vcl_cfg->namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04001789 }
Dave Wallace543852a2017-08-03 02:11:34 -04001790 else if (unformat (line_input, "}"))
1791 {
1792 vc_cfg_input = 0;
Florin Coras0d427d82018-06-27 03:24:07 -07001793 VDBG (0, "VCL<%d>: completed parsing vppcom config!",
1794 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001795 goto input_done;
1796 }
1797 else
1798 {
1799 if (line_input->buffer[line_input->index] != '#')
1800 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001801 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001802 getpid (), (char *)
Dave Wallace543852a2017-08-03 02:11:34 -04001803 &line_input->buffer[line_input->index]);
1804 }
1805 }
1806 }
1807 }
1808
1809input_done:
1810 unformat_free (input);
1811
1812file_done:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001813 if (fd >= 0)
Dave Wallace543852a2017-08-03 02:11:34 -04001814 close (fd);
1815}
1816
1817/*
1818 * VPPCOM Public API functions
1819 */
1820int
1821vppcom_app_create (char *app_name)
1822{
Dave Wallace543852a2017-08-03 02:11:34 -04001823 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1824 u8 *heap;
1825 mheap_t *h;
1826 int rv;
1827
1828 if (!vcm->init)
1829 {
1830 char *conf_fname;
Dave Wallace8af20542017-10-26 03:29:30 -04001831 char *env_var_str;
Dave Wallace543852a2017-08-03 02:11:34 -04001832
1833 vcm->init = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04001834 vppcom_cfg_init (vcl_cfg);
Dave Wallace498b3a52017-11-09 13:00:34 -05001835 env_var_str = getenv (VPPCOM_ENV_DEBUG);
1836 if (env_var_str)
1837 {
1838 u32 tmp;
1839 if (sscanf (env_var_str, "%u", &tmp) != 1)
Dave Wallace69d01192018-02-22 16:22:09 -05001840 clib_warning ("VCL<%d>: WARNING: Invalid debug level specified "
1841 "in the environment variable " VPPCOM_ENV_DEBUG
Dave Wallace498b3a52017-11-09 13:00:34 -05001842 " (%s)!\n", getpid (), env_var_str);
1843 else
1844 {
1845 vcm->debug = tmp;
Florin Coras0d427d82018-06-27 03:24:07 -07001846 VDBG (0, "VCL<%d>: configured VCL debug level (%u) from "
1847 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
Dave Wallace498b3a52017-11-09 13:00:34 -05001848 }
1849 }
Dave Wallace8af20542017-10-26 03:29:30 -04001850 conf_fname = getenv (VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04001851 if (!conf_fname)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001852 conf_fname = VPPCOM_CONF_DEFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04001853 vppcom_cfg_heapsize (conf_fname);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001854 vcl_cfg = &vcm->cfg;
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001855 clib_spinlock_init (&vcm->session_fifo_lockp);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001856 clib_fifo_validate (vcm->client_session_index_fifo,
1857 vcm->cfg.listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04001858 vppcom_cfg_read (conf_fname);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001859
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001860
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001861 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
1862 if (env_var_str)
1863 {
1864 if (vcl_cfg->vpp_api_filename)
1865 vec_free (vcl_cfg->vpp_api_filename);
1866 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1867 env_var_str, 0);
1868 vl_set_memory_root_path ((char *) env_var_str);
1869
Florin Coras0d427d82018-06-27 03:24:07 -07001870 VDBG (0, "VCL<%d>: configured api prefix (%s) and filename (%s) "
1871 "from " VPPCOM_ENV_API_PREFIX "!",
1872 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001873 }
Dave Wallace8af20542017-10-26 03:29:30 -04001874 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1875 if (env_var_str)
1876 {
1877 u32 ns_id_vec_len = strlen (env_var_str);
1878
1879 vec_reset_length (vcm->cfg.namespace_id);
1880 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1881 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1882
Florin Coras0d427d82018-06-27 03:24:07 -07001883 VDBG (0, "VCL<%d>: configured namespace_id (%v) from "
1884 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
1885 vcm->cfg.namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04001886 }
1887 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1888 if (env_var_str)
1889 {
1890 u64 tmp;
1891 if (sscanf (env_var_str, "%lu", &tmp) != 1)
Dave Wallace69d01192018-02-22 16:22:09 -05001892 clib_warning ("VCL<%d>: WARNING: Invalid namespace secret "
1893 "specified in the environment variable "
Dave Wallace8af20542017-10-26 03:29:30 -04001894 VPPCOM_ENV_APP_NAMESPACE_SECRET
Dave Wallace2e005bb2017-11-07 01:21:39 -05001895 " (%s)!\n", getpid (), env_var_str);
Dave Wallace8af20542017-10-26 03:29:30 -04001896 else
1897 {
1898 vcm->cfg.namespace_secret = tmp;
Florin Coras0d427d82018-06-27 03:24:07 -07001899 VDBG (0, "VCL<%d>: configured namespace secret (%lu) from "
1900 VPPCOM_ENV_APP_NAMESPACE_SECRET "!", getpid (),
1901 vcm->cfg.namespace_secret);
Dave Wallace8af20542017-10-26 03:29:30 -04001902 }
1903 }
Dave Wallace774169b2017-11-01 20:07:40 -04001904 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
Dave Wallace8af20542017-10-26 03:29:30 -04001905 {
Dave Wallace774169b2017-11-01 20:07:40 -04001906 vcm->cfg.app_proxy_transport_tcp = 1;
Florin Coras0d427d82018-06-27 03:24:07 -07001907 VDBG (0, "VCL<%d>: configured app_proxy_transport_tcp (%u) from "
1908 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", getpid (),
1909 vcm->cfg.app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04001910 }
Dave Wallace774169b2017-11-01 20:07:40 -04001911 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
Dave Wallace8af20542017-10-26 03:29:30 -04001912 {
Dave Wallace774169b2017-11-01 20:07:40 -04001913 vcm->cfg.app_proxy_transport_udp = 1;
Florin Coras0d427d82018-06-27 03:24:07 -07001914 VDBG (0, "VCL<%d>: configured app_proxy_transport_udp (%u) from "
1915 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", getpid (),
1916 vcm->cfg.app_proxy_transport_udp);
Dave Wallace774169b2017-11-01 20:07:40 -04001917 }
1918 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1919 {
1920 vcm->cfg.app_scope_local = 1;
Florin Coras0d427d82018-06-27 03:24:07 -07001921 VDBG (0, "VCL<%d>: configured app_scope_local (%u) from "
1922 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
1923 vcm->cfg.app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04001924 }
1925 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1926 {
1927 vcm->cfg.app_scope_global = 1;
Florin Coras0d427d82018-06-27 03:24:07 -07001928 VDBG (0, "VCL<%d>: configured app_scope_global (%u) from "
1929 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
1930 vcm->cfg.app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04001931 }
1932
Dave Wallace543852a2017-08-03 02:11:34 -04001933 vcm->main_cpu = os_get_thread_index ();
1934 heap = clib_mem_get_per_cpu_heap ();
1935 h = mheap_header (heap);
1936
1937 /* make the main heap thread-safe */
1938 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1939
1940 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1941
1942 clib_time_init (&vcm->clib_time);
1943 vppcom_init_error_string_table ();
Florin Corasa332c462018-01-31 06:52:17 -08001944 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
1945 20 /* timeout in secs */ );
Dave Wallace543852a2017-08-03 02:11:34 -04001946 clib_spinlock_init (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001947 }
1948
1949 if (vcm->my_client_index == ~0)
1950 {
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001951
1952 /* API hookup and connect to VPP */
Dave Wallace048b1d62018-01-03 22:24:41 -05001953 vppcom_api_hookup ();
Florin Coras0d427d82018-06-27 03:24:07 -07001954 vcl_elog_init (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04001955 vcm->app_state = STATE_APP_START;
1956 rv = vppcom_connect_to_vpp (app_name);
1957 if (rv)
1958 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001959 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001960 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001961 return rv;
1962 }
1963
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001964 /* State event handling thread */
1965
1966 rv = vce_start_event_thread (&(vcm->event_thread), 20);
1967
Florin Coras0d427d82018-06-27 03:24:07 -07001968 VDBG (0, "VCL<%d>: sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001969
Dave Wallace048b1d62018-01-03 22:24:41 -05001970 rv = vppcom_app_session_enable ();
Dave Wallace543852a2017-08-03 02:11:34 -04001971 if (rv)
1972 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001973 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
1974 "failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04001975 return rv;
1976 }
Dave Wallace543852a2017-08-03 02:11:34 -04001977
Florin Coras0d427d82018-06-27 03:24:07 -07001978 VDBG (0, "VCL<%d>: sending app attach", getpid ());
Dave Wallace048b1d62018-01-03 22:24:41 -05001979
1980 rv = vppcom_app_attach ();
1981 if (rv)
1982 {
1983 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
1984 getpid ());
1985 return rv;
1986 }
1987
Florin Coras0d427d82018-06-27 03:24:07 -07001988 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
1989 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001990 }
Dave Wallace543852a2017-08-03 02:11:34 -04001991
1992 return VPPCOM_OK;
1993}
1994
1995void
1996vppcom_app_destroy (void)
1997{
Dave Wallace543852a2017-08-03 02:11:34 -04001998 int rv;
Dave Wallacede910062018-03-20 09:22:13 -04001999 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -04002000
2001 if (vcm->my_client_index == ~0)
2002 return;
2003
Florin Coras0d427d82018-06-27 03:24:07 -07002004 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
2005 getpid (), vcm->my_client_index, vcm->my_client_index);
2006 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002007
Dave Wallace543852a2017-08-03 02:11:34 -04002008 vppcom_app_detach ();
Dave Wallacede910062018-03-20 09:22:13 -04002009 orig_app_timeout = vcm->cfg.app_timeout;
2010 vcm->cfg.app_timeout = 2.0;
Dave Wallace543852a2017-08-03 02:11:34 -04002011 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
Dave Wallacede910062018-03-20 09:22:13 -04002012 vcm->cfg.app_timeout = orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -04002013 if (PREDICT_FALSE (rv))
Florin Coras0d427d82018-06-27 03:24:07 -07002014 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
2015 getpid (), rv, vppcom_retval_str (rv));
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002016
Florin Coras0d427d82018-06-27 03:24:07 -07002017 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04002018 vl_client_disconnect_from_vlib ();
2019 vcm->my_client_index = ~0;
2020 vcm->app_state = STATE_APP_START;
2021}
2022
2023int
Dave Wallacec04cbf12018-02-07 18:14:02 -05002024vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04002025{
Florin Coras7e12d942018-06-27 14:32:43 -07002026 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04002027 u32 session_index;
2028
Dave Wallace7e607a72018-06-18 18:41:32 -04002029 VCL_SESSION_LOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -04002030 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002031 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -04002032 session_index = session - vcm->sessions;
2033
Florin Coras7e12d942018-06-27 14:32:43 -07002034 session->session_type = proto;
2035 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002036 session->vpp_handle = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002037
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002038 if (is_nonblocking)
2039 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2040 else
2041 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04002042
Florin Coras7e12d942018-06-27 14:32:43 -07002043 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
2044 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002045
Dave Wallace7e607a72018-06-18 18:41:32 -04002046 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002047
Florin Coras0d427d82018-06-27 03:24:07 -07002048 VDBG (0, "VCL<%d>: sid %u", getpid (), session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002049
Dave Wallace543852a2017-08-03 02:11:34 -04002050 return (int) session_index;
2051}
2052
2053int
2054vppcom_session_close (uint32_t session_index)
2055{
Florin Coras7e12d942018-06-27 14:32:43 -07002056 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002057 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002058 u8 is_vep;
2059 u8 is_vep_session;
2060 u32 next_sid;
2061 u32 vep_idx;
Dave Wallaceee45d412017-11-24 21:44:06 -05002062 u64 vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002063 uword *p;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002064 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04002065
Dave Wallace7e607a72018-06-18 18:41:32 -04002066 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002067 is_vep = session->is_vep;
2068 is_vep_session = session->is_vep_session;
2069 next_sid = session->vep.next_sid;
2070 vep_idx = session->vep.vep_idx;
Florin Coras7e12d942018-06-27 14:32:43 -07002071 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05002072 vpp_handle = session->vpp_handle;
Dave Wallace7e607a72018-06-18 18:41:32 -04002073 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -04002074
2075 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -05002076 {
2077 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -05002078 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2079 "closing epoll session...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002080 getpid (), session_index, session_index);
2081 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002082 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2083 "closing session...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002084 getpid (), vpp_handle, session_index);
2085 }
Dave Wallace543852a2017-08-03 02:11:34 -04002086
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002087 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04002088 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002089 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04002090 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002091 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07002092 if (PREDICT_FALSE (rv < 0))
2093 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
2094 "vep_idx %u failed! rv %d (%s)",
2095 getpid (), vpp_handle, next_sid, vep_idx,
2096 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002097
Dave Wallace7e607a72018-06-18 18:41:32 -04002098 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002099 next_sid = session->vep.next_sid;
Dave Wallace7e607a72018-06-18 18:41:32 -04002100 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002101 }
2102 }
2103 else
2104 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002105 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002106 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002107 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07002108 if (rv < 0)
2109 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
2110 "vep_idx %u failed! rv %d (%s)",
2111 getpid (), vpp_handle, session_index,
2112 vep_idx, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002113 }
2114
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002115 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002116 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002117 rv = vppcom_session_unbind (session_index);
2118 if (PREDICT_FALSE (rv < 0))
Florin Coras0d427d82018-06-27 03:24:07 -07002119 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
2120 "failed! rv %d (%s)",
2121 getpid (), vpp_handle, session_index,
2122 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002123 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002124
2125 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002126 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002127 rv = vppcom_session_disconnect (session_index);
2128 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002129 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002130 "session disconnect failed! rv %d (%s)",
2131 getpid (), vpp_handle, session_index,
2132 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002133 }
Dave Wallace19481612017-09-15 18:47:44 -04002134 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002135
Dave Wallace7e607a72018-06-18 18:41:32 -04002136 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002137 vpp_handle = session->vpp_handle;
2138 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002139 {
Dave Wallaceee45d412017-11-24 21:44:06 -05002140 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002141 if (p)
Dave Wallaceee45d412017-11-24 21:44:06 -05002142 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002143 }
Dave Wallace543852a2017-08-03 02:11:34 -04002144 pool_put_index (vcm->sessions, session_index);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002145
Dave Wallace7e607a72018-06-18 18:41:32 -04002146 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05002147
2148 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -05002149 {
2150 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -05002151 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -05002152 getpid (), session_index, session_index);
2153 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002154 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -05002155 getpid (), vpp_handle, session_index);
2156 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002157done:
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002158
Florin Coras0d427d82018-06-27 03:24:07 -07002159 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002160
Dave Wallace543852a2017-08-03 02:11:34 -04002161 return rv;
2162}
2163
2164int
2165vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2166{
Florin Coras7e12d942018-06-27 14:32:43 -07002167 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002168 int rv;
2169
2170 if (!ep || !ep->ip)
2171 return VPPCOM_EINVAL;
2172
Dave Wallace7e607a72018-06-18 18:41:32 -04002173 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -04002174
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002175 if (session->is_vep)
2176 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002177 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002178 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2179 "bind to an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002180 rv = VPPCOM_EBADFD;
2181 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002182 }
2183
Florin Coras7e12d942018-06-27 14:32:43 -07002184 session->transport.is_ip4 = ep->is_ip4;
2185 session->transport.lcl_ip = to_ip46 (ep->is_ip4 ? IP46_TYPE_IP4 :
2186 IP46_TYPE_IP6, ep->ip);
2187 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07002188
Florin Coras0d427d82018-06-27 03:24:07 -07002189 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
2190 "proto %s", getpid (), session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07002191 session->transport.is_ip4 ? "IPv4" : "IPv6",
2192 format_ip46_address, &session->transport.lcl_ip,
2193 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2194 clib_net_to_host_u16 (session->transport.lcl_port),
2195 session->session_type ? "UDP" : "TCP");
Florin Coras0d427d82018-06-27 03:24:07 -07002196 vcl_evt (VCL_EVT_BIND, session);
Dave Wallace7e607a72018-06-18 18:41:32 -04002197 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05002198done:
2199 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002200}
2201
2202int
Dave Wallace33e002b2017-09-06 01:20:02 -04002203vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04002204{
Florin Coras7e12d942018-06-27 14:32:43 -07002205 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05002206 u64 listen_vpp_handle;
2207 int rv, retval;
Dave Wallace543852a2017-08-03 02:11:34 -04002208
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08002209 if (q_len == 0 || q_len == ~0)
2210 q_len = vcm->cfg.listen_queue_size;
2211
Dave Wallace7e607a72018-06-18 18:41:32 -04002212 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002213
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002214 if (listen_session->is_vep)
2215 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002216 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002217 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002218 "epoll session!", getpid (), listen_session_index);
2219 rv = VPPCOM_EBADFD;
2220 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002221 }
2222
Dave Wallaceee45d412017-11-24 21:44:06 -05002223 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07002224 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04002225 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002226 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07002227 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
2228 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002229 rv = VPPCOM_OK;
2230 goto done;
Dave Wallacee695cb42017-11-02 22:04:42 -04002231 }
2232
Florin Coras0d427d82018-06-27 03:24:07 -07002233 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
2234 "request...", getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002235
Dave Wallace4878cbe2017-11-21 03:45:09 -05002236 vppcom_send_bind_sock (listen_session, listen_session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04002237 VCL_SESSION_UNLOCK ();
Dave Wallaceee45d412017-11-24 21:44:06 -05002238 retval =
Dave Wallace33e002b2017-09-06 01:20:02 -04002239 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2240 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002241
Dave Wallace7e607a72018-06-18 18:41:32 -04002242 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002243 if (PREDICT_FALSE (retval))
2244 {
Florin Coras0d427d82018-06-27 03:24:07 -07002245 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
2246 "returning %d (%s)", getpid (), listen_session->vpp_handle,
2247 listen_session_index, retval, vppcom_retval_str (retval));
Dave Wallace7e607a72018-06-18 18:41:32 -04002248 VCL_SESSION_UNLOCK ();
Dave Wallaceee45d412017-11-24 21:44:06 -05002249 rv = retval;
2250 goto done;
2251 }
2252
Dave Wallace7e607a72018-06-18 18:41:32 -04002253 VCL_ACCEPT_FIFO_LOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -04002254 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallace7e607a72018-06-18 18:41:32 -04002255 VCL_ACCEPT_FIFO_UNLOCK ();
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002256
Dave Wallace7e607a72018-06-18 18:41:32 -04002257 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08002258
2259done:
2260 return rv;
2261}
2262
2263int
Florin Coras7e12d942018-06-27 14:32:43 -07002264validate_args_session_accept_ (vcl_session_t * listen_session)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002265{
2266 u32 listen_session_index = listen_session - vcm->sessions;
2267
2268 /* Input validation - expects spinlock on sessions_lockp */
2269 if (listen_session->is_vep)
2270 {
2271 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2272 "epoll session!", getpid (), listen_session_index);
2273 return VPPCOM_EBADFD;
2274 }
2275
Florin Coras7e12d942018-06-27 14:32:43 -07002276 if (listen_session->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002277 {
2278 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2279 "not in listen state! state 0x%x (%s)", getpid (),
2280 listen_session->vpp_handle, listen_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07002281 listen_session->session_state,
2282 vppcom_session_state_str (listen_session->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002283 return VPPCOM_EBADFD;
2284 }
2285 return VPPCOM_OK;
2286}
2287
2288int
Dave Wallace543852a2017-08-03 02:11:34 -04002289vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05002290 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04002291{
Florin Coras7e12d942018-06-27 14:32:43 -07002292 vcl_session_t *listen_session = 0;
2293 vcl_session_t *client_session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002294 u32 client_session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002295 int rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05002296 u64 listen_vpp_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002297 vce_event_handler_reg_t *reg;
2298 vce_event_t *ev;
2299 vce_event_connect_request_t *result;
2300 struct timespec ts;
2301 struct timeval tv;
2302 int millisecond_timeout = 1;
2303 int hours_timeout = 20 * 60 * 60;
Dave Wallace543852a2017-08-03 02:11:34 -04002304
Dave Wallace7e607a72018-06-18 18:41:32 -04002305 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002306 listen_vpp_handle = listen_session->vpp_handle; // For debugging
Dave Wallace543852a2017-08-03 02:11:34 -04002307
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002308 rv = validate_args_session_accept_ (listen_session);
2309 if (rv)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002310 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002311 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05002312 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002313 }
2314
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002315 /* Using an aggressive timer of 1ms and a generous timer of
2316 * 20 hours, we can implement a blocking and non-blocking listener
2317 * as both event and time driven */
2318 gettimeofday (&tv, NULL);
2319 ts.tv_nsec = (tv.tv_usec * 1000) + (1000 * millisecond_timeout);
2320 ts.tv_sec = tv.tv_sec;
2321
2322 /* Predict that the Listener is blocking more often than not */
2323 if (PREDICT_TRUE (!VCL_SESS_ATTR_TEST (listen_session->attr,
2324 VCL_SESS_ATTR_NONBLOCK)))
2325 ts.tv_sec += hours_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -04002326
Dave Wallace7e607a72018-06-18 18:41:32 -04002327 VCL_SESSION_UNLOCK ();
Dave Wallace543852a2017-08-03 02:11:34 -04002328
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002329 /* Register handler for connect_request event on listen_session_index */
2330 vce_event_key_t evk;
2331 evk.session_index = listen_session_index;
2332 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2333 reg = vce_register_handler (&vcm->event_thread, &evk,
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08002334 vce_connect_request_handler_fn, 0);
Dave Wallace7e607a72018-06-18 18:41:32 -04002335 VCL_EVENTS_LOCK ();
Keith Burns (alagalah)7cf80e02018-03-08 16:46:25 -08002336 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002337 pthread_mutex_lock (&reg->handler_lock);
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002338 while (!ev)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002339 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002340 VCL_EVENTS_UNLOCK ();
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002341 rv = pthread_cond_timedwait (&reg->handler_cond,
2342 &reg->handler_lock, &ts);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002343 if (rv == ETIMEDOUT)
2344 {
2345 rv = VPPCOM_EAGAIN;
2346 goto cleanup;
2347 }
Dave Wallace7e607a72018-06-18 18:41:32 -04002348 VCL_EVENTS_LOCK ();
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002349 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002350 }
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002351 result = vce_get_event_data (ev, sizeof (*result));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002352 client_session_index = result->accepted_session_index;
Dave Wallace7e607a72018-06-18 18:41:32 -04002353 VCL_EVENTS_UNLOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002354
2355 /* Remove from the FIFO used to service epoll */
Dave Wallace7e607a72018-06-18 18:41:32 -04002356 VCL_ACCEPT_FIFO_LOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002357 if (clib_fifo_elts (vcm->client_session_index_fifo))
2358 {
2359 u32 tmp_client_session_index;
2360 clib_fifo_sub1 (vcm->client_session_index_fifo,
2361 tmp_client_session_index);
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002362 /* It wasn't ours... put it back ... */
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002363 if (tmp_client_session_index != client_session_index)
2364 clib_fifo_add1 (vcm->client_session_index_fifo,
2365 tmp_client_session_index);
2366 }
Dave Wallace7e607a72018-06-18 18:41:32 -04002367 VCL_ACCEPT_FIFO_UNLOCK ();
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002368
Dave Wallace7e607a72018-06-18 18:41:32 -04002369 VCL_SESSION_LOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002370
Dave Wallace33e002b2017-09-06 01:20:02 -04002371 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002372 if (PREDICT_FALSE (rv))
2373 {
2374 rv = VPPCOM_ECONNABORTED;
Dave Wallace048b1d62018-01-03 22:24:41 -05002375 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
Dave Wallaceee45d412017-11-24 21:44:06 -05002376 "lookup failed! returning %d (%s)", getpid (),
2377 listen_vpp_handle, listen_session_index,
2378 client_session_index, rv, vppcom_retval_str (rv));
Dave Wallacee5356442018-03-19 10:38:00 -04002379 goto cleanup;
Dave Wallaceee45d412017-11-24 21:44:06 -05002380 }
Dave Wallace543852a2017-08-03 02:11:34 -04002381
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002382 if (flags & O_NONBLOCK)
2383 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2384 else
2385 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2386
Florin Coras0d427d82018-06-27 03:24:07 -07002387 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2388 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2389 getpid (), listen_vpp_handle, listen_session_index,
2390 client_session->vpp_handle, client_session_index,
2391 flags, VCL_SESS_ATTR_TEST (client_session->attr,
2392 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04002393
Dave Wallace048b1d62018-01-03 22:24:41 -05002394 if (ep)
2395 {
Florin Coras7e12d942018-06-27 14:32:43 -07002396 ep->is_ip4 = client_session->transport.is_ip4;
2397 ep->port = client_session->transport.rmt_port;
2398 if (client_session->transport.is_ip4)
2399 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
Dave Wallace048b1d62018-01-03 22:24:41 -05002400 sizeof (ip4_address_t));
2401 else
Florin Coras7e12d942018-06-27 14:32:43 -07002402 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
Dave Wallace048b1d62018-01-03 22:24:41 -05002403 sizeof (ip6_address_t));
2404 }
Dave Wallace60caa062017-11-10 17:07:13 -05002405
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002406 vppcom_send_accept_session_reply (client_session->vpp_handle,
2407 client_session->client_context,
2408 0 /* retval OK */ );
Dave Wallace60caa062017-11-10 17:07:13 -05002409
Florin Coras0d427d82018-06-27 03:24:07 -07002410 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx,"
2411 " sid %u connection from peer %s address %U port %u to local %s address"
2412 " %U port %u",
2413 getpid (), listen_vpp_handle,
2414 listen_session_index, client_session->vpp_handle,
2415 client_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07002416 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
2417 format_ip46_address, &client_session->transport.rmt_ip,
2418 client_session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07002419 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07002420 clib_net_to_host_u16 (client_session->transport.rmt_port),
2421 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
2422 format_ip46_address, &client_session->transport.lcl_ip,
2423 client_session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07002424 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07002425 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07002426 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
2427 client_session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04002428 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002429
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002430 rv = (int) client_session_index;
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002431 vce_clear_event (&vcm->event_thread, reg->ev_idx);
2432 if (vcm->session_io_thread.io_sessions_lockp)
2433 {
2434 /* Throw this new accepted session index into the rx poll thread pool */
Dave Wallace7e607a72018-06-18 18:41:32 -04002435 VCL_IO_SESSIONS_LOCK ();
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002436 u32 *active_session_index;
2437 pool_get (vcm->session_io_thread.active_session_indexes,
2438 active_session_index);
2439 *active_session_index = client_session_index;
Dave Wallace7e607a72018-06-18 18:41:32 -04002440 VCL_IO_SESSIONS_UNLOCK ();
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002441 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002442cleanup:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002443 vce_unregister_handler (&vcm->event_thread, reg);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002444 pthread_mutex_unlock (&reg->handler_lock);
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08002445
Dave Wallace4878cbe2017-11-21 03:45:09 -05002446done:
2447 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002448}
2449
2450int
2451vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2452{
Florin Coras7e12d942018-06-27 14:32:43 -07002453 vcl_session_t *session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002454 u64 vpp_handle = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05002455 int rv, retval = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04002456
Dave Wallace7e607a72018-06-18 18:41:32 -04002457 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002458
2459 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04002460 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002461 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002462 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2463 "connect on an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002464 rv = VPPCOM_EBADFD;
2465 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002466 }
2467
Florin Coras7e12d942018-06-27 14:32:43 -07002468 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04002469 {
Florin Coras0d427d82018-06-27 03:24:07 -07002470 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
2471 "connected to %s %U port %d proto %s, state 0x%x (%s)",
2472 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07002473 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07002474 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002475 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07002476 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07002477 clib_net_to_host_u16 (session->transport.rmt_port),
2478 session->session_type ? "UDP" : "TCP", session->session_state,
2479 vppcom_session_state_str (session->session_state));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002480
Dave Wallace7e607a72018-06-18 18:41:32 -04002481 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05002482 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002483 }
2484
Florin Coras7e12d942018-06-27 14:32:43 -07002485 session->transport.is_ip4 = server_ep->is_ip4;
2486 if (session->transport.is_ip4)
2487 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -04002488 sizeof (ip4_address_t));
2489 else
Florin Coras7e12d942018-06-27 14:32:43 -07002490 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -04002491 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07002492 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04002493
Florin Coras0d427d82018-06-27 03:24:07 -07002494 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
2495 "port %d proto %s",
2496 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07002497 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07002498 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002499 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07002500 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07002501 clib_net_to_host_u16 (session->transport.rmt_port),
2502 session->session_type ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002503
2504 vppcom_send_connect_sock (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04002505 VCL_SESSION_UNLOCK ();
Dave Wallace4878cbe2017-11-21 03:45:09 -05002506
Dave Wallaceee45d412017-11-24 21:44:06 -05002507 retval =
2508 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2509 vcm->cfg.session_timeout);
2510
Dave Wallace7e607a72018-06-18 18:41:32 -04002511 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002512 vpp_handle = session->vpp_handle;
Dave Wallace7e607a72018-06-18 18:41:32 -04002513 VCL_SESSION_UNLOCK ();
Dave Wallace7876d392017-10-19 03:53:57 -04002514
Dave Wallace4878cbe2017-11-21 03:45:09 -05002515done:
Dave Wallaceee45d412017-11-24 21:44:06 -05002516 if (PREDICT_FALSE (retval))
2517 {
2518 rv = retval;
2519 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002520 {
2521 if (session)
Florin Coras0d427d82018-06-27 03:24:07 -07002522 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
2523 "failed! returning %d (%s)", getpid (), vpp_handle,
2524 session_index, rv, vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002525 else
2526 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
2527 "returning %d (%s)", getpid (),
2528 session_index, rv, vppcom_retval_str (rv));
2529 }
Dave Wallaceee45d412017-11-24 21:44:06 -05002530 }
Florin Coras0d427d82018-06-27 03:24:07 -07002531 else
2532 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
2533 getpid (), vpp_handle, session_index);
Dave Wallaceee45d412017-11-24 21:44:06 -05002534
Dave Wallace4878cbe2017-11-21 03:45:09 -05002535 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002536}
2537
Steven58f464e2017-10-25 12:33:12 -07002538static inline int
2539vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2540 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04002541{
Florin Coras7e12d942018-06-27 14:32:43 -07002542 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002543 svm_fifo_t *rx_fifo;
2544 int n_read = 0;
2545 int rv;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002546 int is_nonblocking;
2547
2548 u64 vpp_handle;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002549 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002550 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04002551
2552 ASSERT (buf);
2553
Dave Wallace7e607a72018-06-18 18:41:32 -04002554 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002555
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002556 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
2557 rx_fifo = session->rx_fifo;
Florin Coras7e12d942018-06-27 14:32:43 -07002558 state = session->session_state;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002559 vpp_handle = session->vpp_handle;
2560
Dave Wallace4878cbe2017-11-21 03:45:09 -05002561 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04002562 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002563 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002564 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2565 "read from an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002566 rv = VPPCOM_EBADFD;
2567 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002568 }
2569
Dave Wallace4878cbe2017-11-21 03:45:09 -05002570 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002571 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002572 VCL_SESSION_UNLOCK ();
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002573 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002574
Florin Coras0d427d82018-06-27 03:24:07 -07002575 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
2576 "state 0x%x (%s), returning %d (%s)",
2577 getpid (), vpp_handle, session_index, state,
2578 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002579 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002580 }
2581
Dave Wallace7e607a72018-06-18 18:41:32 -04002582 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04002583
2584 do
2585 {
Steven58f464e2017-10-25 12:33:12 -07002586 if (peek)
2587 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2588 else
2589 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002590 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002591 while (!is_nonblocking && (n_read <= 0));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002592
Dave Wallace4878cbe2017-11-21 03:45:09 -05002593 if (n_read <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002594 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002595 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002596
2597 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
2598 (EPOLLET | EPOLLIN));
2599 if (poll_et)
2600 session->vep.et_mask |= EPOLLIN;
2601
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002602 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002603 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002604 rv = VPPCOM_ECONNRESET;
2605
Florin Coras0d427d82018-06-27 03:24:07 -07002606 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
2607 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
2608 "returning %d (%s)",
2609 getpid (), session->vpp_handle, session_index,
2610 state, vppcom_session_state_str (state),
2611 STATE_DISCONNECT,
2612 vppcom_session_state_str (STATE_DISCONNECT), rv,
2613 vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002614
Florin Coras7e12d942018-06-27 14:32:43 -07002615 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002616 }
2617 else
2618 rv = VPPCOM_EAGAIN;
2619
Dave Wallace7e607a72018-06-18 18:41:32 -04002620 VCL_SESSION_UNLOCK ();
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002621 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002622 else
2623 rv = n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04002624
Dave Wallace4878cbe2017-11-21 03:45:09 -05002625 if (VPPCOM_DEBUG > 2)
2626 {
2627 if (rv > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002628 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002629 "from (%p)", getpid (), vpp_handle,
2630 session_index, n_read, rx_fifo);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002631 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002632 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
Dave Wallaceee45d412017-11-24 21:44:06 -05002633 "returning %d (%s)", getpid (), vpp_handle,
2634 session_index, rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002635 }
2636done:
2637 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002638}
2639
Steven58f464e2017-10-25 12:33:12 -07002640int
Dave Wallace048b1d62018-01-03 22:24:41 -05002641vppcom_session_read (uint32_t session_index, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07002642{
2643 return (vppcom_session_read_internal (session_index, buf, n, 0));
2644}
2645
2646static int
2647vppcom_session_peek (uint32_t session_index, void *buf, int n)
2648{
2649 return (vppcom_session_read_internal (session_index, buf, n, 1));
2650}
2651
Dave Wallace543852a2017-08-03 02:11:34 -04002652static inline int
Florin Coras7e12d942018-06-27 14:32:43 -07002653vppcom_session_read_ready (vcl_session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04002654{
Dave Wallace543852a2017-08-03 02:11:34 -04002655 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05002656 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002657 int rv;
Florin Coras7e12d942018-06-27 14:32:43 -07002658 session_state_t state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05002659 u64 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04002660
2661 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05002662 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002663 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002664 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002665 "epoll session!", getpid (), session_index);
2666 rv = VPPCOM_EBADFD;
2667 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002668 }
Dave Wallace33e002b2017-09-06 01:20:02 -04002669
Florin Coras7e12d942018-06-27 14:32:43 -07002670 if (session->session_state & STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002671 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002672 VCL_ACCEPT_FIFO_LOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002673 ready = clib_fifo_elts (vcm->client_session_index_fifo);
Dave Wallace7e607a72018-06-18 18:41:32 -04002674 VCL_ACCEPT_FIFO_UNLOCK ();
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002675 }
Dave Wallace543852a2017-08-03 02:11:34 -04002676 else
2677 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002678 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
2679 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002680 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
Dave Wallace4878cbe2017-11-21 03:45:09 -05002681 VPPCOM_ENOTCONN);
2682
Florin Coras0d427d82018-06-27 03:24:07 -07002683 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
2684 " state 0x%x (%s), returning %d (%s)",
2685 getpid (), vpp_handle, session_index,
2686 state, vppcom_session_state_str (state),
2687 rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002688 goto done;
2689 }
2690
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002691 ready = svm_fifo_max_dequeue (session->rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002692 }
2693
Dave Wallace4878cbe2017-11-21 03:45:09 -05002694 if (ready == 0)
Dave Wallace60caa062017-11-10 17:07:13 -05002695 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002696 poll_et =
2697 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
2698 if (poll_et)
2699 session->vep.et_mask |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05002700
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002701 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002702 {
2703 rv = VPPCOM_ECONNRESET;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002704
Florin Coras0d427d82018-06-27 03:24:07 -07002705 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
2706 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
2707 "returning %d (%s)",
2708 getpid (), session_index, vpp_handle,
2709 state, vppcom_session_state_str (state),
2710 STATE_DISCONNECT,
2711 vppcom_session_state_str (STATE_DISCONNECT), rv,
2712 vppcom_retval_str (rv));
Florin Coras7e12d942018-06-27 14:32:43 -07002713 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002714 goto done;
2715 }
2716 }
2717 rv = ready;
Dave Wallace16cb4082017-11-29 03:24:06 -05002718
2719 if (vcm->app_event_queue->cursize &&
2720 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
2721 {
2722 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
2723 session_fifo_event_t e;
2724
2725 for (i = 0; i < n_to_dequeue; i++)
Florin Corase86a8ed2018-01-05 03:20:25 -08002726 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
Dave Wallace16cb4082017-11-29 03:24:06 -05002727
2728 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
2729 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002730done:
2731 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002732}
2733
2734int
Dave Wallace048b1d62018-01-03 22:24:41 -05002735vppcom_session_write (uint32_t session_index, void *buf, size_t n)
Dave Wallace543852a2017-08-03 02:11:34 -04002736{
Florin Coras7e12d942018-06-27 14:32:43 -07002737 vcl_session_t *session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002738 svm_fifo_t *tx_fifo = 0;
Florin Corase86a8ed2018-01-05 03:20:25 -08002739 svm_queue_t *q;
Dave Wallace543852a2017-08-03 02:11:34 -04002740 session_fifo_event_t evt;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002741 session_state_t state;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002742 int rv, n_write, is_nonblocking;
2743 u32 poll_et;
Dave Wallaceee45d412017-11-24 21:44:06 -05002744 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04002745
2746 ASSERT (buf);
2747
Dave Wallace7e607a72018-06-18 18:41:32 -04002748 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002749
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002750 tx_fifo = session->tx_fifo;
2751 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
2752 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07002753 state = session->session_state;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002754
Dave Wallace4878cbe2017-11-21 03:45:09 -05002755 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04002756 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002757 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05002758 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002759 "cannot write to an epoll session!",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002760 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002761
2762 rv = VPPCOM_EBADFD;
2763 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002764 }
2765
Florin Coras7e12d942018-06-27 14:32:43 -07002766 if (!(session->session_state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002767 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002768 rv =
Florin Coras7e12d942018-06-27 14:32:43 -07002769 ((session->session_state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002770 VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002771
Dave Wallace7e607a72018-06-18 18:41:32 -04002772 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07002773 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
2774 "state 0x%x (%s)",
2775 getpid (), vpp_handle, session_index,
2776 state, vppcom_session_state_str (state));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002777 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002778 }
2779
Dave Wallace7e607a72018-06-18 18:41:32 -04002780 VCL_SESSION_UNLOCK ();
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002781
Dave Wallace543852a2017-08-03 02:11:34 -04002782 do
2783 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002784 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
Dave Wallace543852a2017-08-03 02:11:34 -04002785 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002786 while (!is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04002787
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07002788 /* If event wasn't set, add one
2789 *
2790 * To reduce context switching, can check if an
2791 * event is already there for this event_key, but for now
2792 * this will suffice. */
2793
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002794 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04002795 {
Dave Wallace543852a2017-08-03 02:11:34 -04002796 /* Fabricate TX event, send to vpp */
2797 evt.fifo = tx_fifo;
2798 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04002799
Dave Wallace7e607a72018-06-18 18:41:32 -04002800 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Florin Coras7e12d942018-06-27 14:32:43 -07002801 q = session->vpp_evt_q;
Dave Wallace543852a2017-08-03 02:11:34 -04002802 ASSERT (q);
Florin Corase86a8ed2018-01-05 03:20:25 -08002803 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
Dave Wallace7e607a72018-06-18 18:41:32 -04002804 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07002805 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: added FIFO_EVENT_APP_TX "
2806 "to vpp_event_q %p, n_write %d", getpid (),
2807 vpp_handle, session_index, q, n_write);
Dave Wallace543852a2017-08-03 02:11:34 -04002808 }
2809
Dave Wallace4878cbe2017-11-21 03:45:09 -05002810 if (n_write <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002811 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002812 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002813
2814 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2815 (EPOLLET | EPOLLOUT));
2816 if (poll_et)
2817 session->vep.et_mask |= EPOLLOUT;
2818
Florin Coras7e12d942018-06-27 14:32:43 -07002819 if (session->session_state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002820 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002821 rv = VPPCOM_ECONNRESET;
2822
Florin Coras0d427d82018-06-27 03:24:07 -07002823 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
2824 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
2825 "returning %d (%s)",
2826 getpid (), session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07002827 session->session_state,
2828 vppcom_session_state_str (session->session_state),
Florin Coras0d427d82018-06-27 03:24:07 -07002829 STATE_DISCONNECT,
2830 vppcom_session_state_str (STATE_DISCONNECT), rv,
2831 vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002832
Florin Coras7e12d942018-06-27 14:32:43 -07002833 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002834 }
2835 else
2836 rv = VPPCOM_EAGAIN;
2837
Dave Wallace7e607a72018-06-18 18:41:32 -04002838 VCL_SESSION_UNLOCK ();
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002839 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002840 else
2841 rv = n_write;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002842
Dave Wallace543852a2017-08-03 02:11:34 -04002843 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002844 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002845 if (n_write <= 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002846 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002847 "FIFO-FULL (%p)", getpid (), vpp_handle,
2848 session_index, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002849 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002850 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002851 "wrote %d bytes tx-fifo: (%p)", getpid (),
2852 vpp_handle, session_index, n_write, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002853 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002854done:
2855 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002856}
2857
2858static inline int
Florin Coras7e12d942018-06-27 14:32:43 -07002859vppcom_session_write_ready (vcl_session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04002860{
Dave Wallacef7f809c2017-10-03 01:48:42 -04002861 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05002862 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002863 int rv;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002864
2865 ASSERT (session);
Dave Wallace543852a2017-08-03 02:11:34 -04002866
2867 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05002868 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002869 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002870 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002871 "cannot write to an epoll session!",
2872 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002873 rv = VPPCOM_EBADFD;
2874 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002875 }
2876
Florin Coras7e12d942018-06-27 14:32:43 -07002877 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
Dave Wallace33e002b2017-09-06 01:20:02 -04002878 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002879 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002880 "cannot write to a listen session!",
2881 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002882 rv = VPPCOM_EBADFD;
2883 goto done;
2884 }
2885
Florin Coras7e12d942018-06-27 14:32:43 -07002886 if (!(session->session_state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002887 {
Florin Coras7e12d942018-06-27 14:32:43 -07002888 session_state_t state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002889
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002890 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002891
Dave Wallace048b1d62018-01-03 22:24:41 -05002892 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002893 "session is not open! state 0x%x (%s), "
Dave Wallaceee45d412017-11-24 21:44:06 -05002894 "returning %d (%s)", getpid (), session->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002895 session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002896 state, vppcom_session_state_str (state),
2897 rv, vppcom_retval_str (rv));
2898 goto done;
Dave Wallace33e002b2017-09-06 01:20:02 -04002899 }
2900
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002901 ready = svm_fifo_max_enqueue (session->tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04002902
Florin Coras0d427d82018-06-27 03:24:07 -07002903 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
2904 getpid (), session->vpp_handle, session_index, session->tx_fifo,
2905 ready);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002906
Dave Wallace4878cbe2017-11-21 03:45:09 -05002907 if (ready == 0)
2908 {
2909 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
2910 (EPOLLET | EPOLLOUT));
2911 if (poll_et)
2912 session->vep.et_mask |= EPOLLOUT;
2913
Florin Coras7e12d942018-06-27 14:32:43 -07002914 if (session->session_state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002915 {
2916 rv = VPPCOM_ECONNRESET;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002917
Florin Coras0d427d82018-06-27 03:24:07 -07002918 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo with "
2919 "session state 0x%x (%s)! Setting state to 0x%x (%s), "
2920 "returning %d (%s)", getpid (),
2921 session->vpp_handle, session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07002922 session->session_state,
2923 vppcom_session_state_str (session->session_state),
Florin Coras0d427d82018-06-27 03:24:07 -07002924 STATE_DISCONNECT,
2925 vppcom_session_state_str (STATE_DISCONNECT), rv,
2926 vppcom_retval_str (rv));
Florin Coras7e12d942018-06-27 14:32:43 -07002927 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002928 goto done;
2929 }
2930 }
2931 rv = ready;
2932done:
2933 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002934}
2935
2936int
2937vppcom_select (unsigned long n_bits, unsigned long *read_map,
2938 unsigned long *write_map, unsigned long *except_map,
2939 double time_to_wait)
2940{
Dave Wallace543852a2017-08-03 02:11:34 -04002941 u32 session_index;
Florin Coras7e12d942018-06-27 14:32:43 -07002942 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002943 int rv, bits_set = 0;
2944 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2945 u32 minbits = clib_max (n_bits, BITS (uword));
2946
2947 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2948
Dave Wallace7876d392017-10-19 03:53:57 -04002949 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002950 {
2951 clib_bitmap_validate (vcm->rd_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05002952 clib_memcpy (vcm->rd_bitmap, read_map,
2953 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
2954 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04002955 }
Dave Wallace7876d392017-10-19 03:53:57 -04002956 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002957 {
2958 clib_bitmap_validate (vcm->wr_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05002959 clib_memcpy (vcm->wr_bitmap, write_map,
2960 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
2961 memset (write_map, 0,
2962 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04002963 }
Dave Wallace7876d392017-10-19 03:53:57 -04002964 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002965 {
2966 clib_bitmap_validate (vcm->ex_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05002967 clib_memcpy (vcm->ex_bitmap, except_map,
2968 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
2969 memset (except_map, 0,
2970 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04002971 }
2972
2973 do
2974 {
2975 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04002976 if (n_bits)
2977 {
2978 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002979 {
Dave Wallacee22aa742017-10-20 12:30:38 -04002980 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2981 ({
Dave Wallace7e607a72018-06-18 18:41:32 -04002982 VCL_SESSION_LOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04002983 rv = vppcom_session_at_index (session_index, &session);
2984 if (rv < 0)
2985 {
Dave Wallace7e607a72018-06-18 18:41:32 -04002986 VCL_SESSION_UNLOCK();
Florin Coras0d427d82018-06-27 03:24:07 -07002987 VDBG (1, "VCL<%d>: session %d specified in read_map is"
2988 " closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04002989 session_index);
2990 bits_set = VPPCOM_EBADFD;
2991 goto select_done;
2992 }
Florin Coras7e12d942018-06-27 14:32:43 -07002993 if (session->session_state & STATE_LISTEN)
Dave Wallace8d73e852018-03-08 16:39:28 -05002994 {
2995 vce_event_handler_reg_t *reg = 0;
2996 vce_event_key_t evk;
Dave Wallacee22aa742017-10-20 12:30:38 -04002997
Dave Wallace8d73e852018-03-08 16:39:28 -05002998 /* Check if handler already registered for this
2999 * event.
3000 * If not, register handler for connect_request event
3001 * on listen_session_index
3002 */
3003 evk.session_index = session_index;
3004 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3005 reg = vce_get_event_handler (&vcm->event_thread, &evk);
3006 if (!reg)
3007 reg = vce_register_handler (&vcm->event_thread, &evk,
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08003008 vce_poll_wait_connect_request_handler_fn,
3009 0 /* No callback args */);
Dave Wallace8d73e852018-03-08 16:39:28 -05003010 rv = vppcom_session_read_ready (session, session_index);
3011 if (rv > 0)
3012 {
3013 vce_unregister_handler (&vcm->event_thread, reg);
3014 }
3015 }
3016 else
3017 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04003018 VCL_SESSION_UNLOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04003019 if (except_map && vcm->ex_bitmap &&
3020 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3021 (rv < 0))
3022 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003023 clib_bitmap_set_no_check (except_map, session_index, 1);
3024 bits_set++;
3025 }
3026 else if (rv > 0)
3027 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003028 clib_bitmap_set_no_check (read_map, session_index, 1);
3029 bits_set++;
3030 }
3031 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003032 }
3033
Dave Wallacee22aa742017-10-20 12:30:38 -04003034 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003035 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003036 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3037 ({
Dave Wallace7e607a72018-06-18 18:41:32 -04003038 VCL_SESSION_LOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04003039 rv = vppcom_session_at_index (session_index, &session);
3040 if (rv < 0)
3041 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003042 VCL_SESSION_UNLOCK();
Florin Coras0d427d82018-06-27 03:24:07 -07003043 VDBG (0, "VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003044 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003045 session_index);
3046 bits_set = VPPCOM_EBADFD;
3047 goto select_done;
3048 }
Dave Wallace543852a2017-08-03 02:11:34 -04003049
Dave Wallacee22aa742017-10-20 12:30:38 -04003050 rv = vppcom_session_write_ready (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04003051 VCL_SESSION_UNLOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04003052 if (write_map && (rv > 0))
3053 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003054 clib_bitmap_set_no_check (write_map, session_index, 1);
3055 bits_set++;
3056 }
3057 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003058 }
3059
Dave Wallacee22aa742017-10-20 12:30:38 -04003060 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003061 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003062 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3063 ({
Dave Wallace7e607a72018-06-18 18:41:32 -04003064 VCL_SESSION_LOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04003065 rv = vppcom_session_at_index (session_index, &session);
3066 if (rv < 0)
3067 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003068 VCL_SESSION_UNLOCK();
Florin Coras0d427d82018-06-27 03:24:07 -07003069 VDBG (1, "VCL<%d>: session %d specified in except_map "
3070 "is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003071 session_index);
3072 bits_set = VPPCOM_EBADFD;
3073 goto select_done;
3074 }
Dave Wallace543852a2017-08-03 02:11:34 -04003075
Dave Wallacee22aa742017-10-20 12:30:38 -04003076 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace7e607a72018-06-18 18:41:32 -04003077 VCL_SESSION_UNLOCK();
Dave Wallacee22aa742017-10-20 12:30:38 -04003078 if (rv < 0)
3079 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003080 clib_bitmap_set_no_check (except_map, session_index, 1);
3081 bits_set++;
3082 }
3083 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003084 }
Dave Wallacee22aa742017-10-20 12:30:38 -04003085 }
Dave Wallace543852a2017-08-03 02:11:34 -04003086 /* *INDENT-ON* */
3087 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003088 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
Dave Wallace543852a2017-08-03 02:11:34 -04003089
3090select_done:
3091 return (bits_set);
3092}
3093
Dave Wallacef7f809c2017-10-03 01:48:42 -04003094static inline void
3095vep_verify_epoll_chain (u32 vep_idx)
3096{
Florin Coras7e12d942018-06-27 14:32:43 -07003097 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003098 vppcom_epoll_t *vep;
3099 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05003100 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003101
Dave Wallace498b3a52017-11-09 13:00:34 -05003102 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003103 return;
3104
3105 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3106 rv = vppcom_session_at_index (vep_idx, &session);
3107 if (PREDICT_FALSE (rv))
3108 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003109 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3110 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003111 goto done;
3112 }
3113 if (PREDICT_FALSE (!session->is_vep))
3114 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003115 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3116 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003117 goto done;
3118 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003119 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05003120 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05003121 "{\n"
3122 " is_vep = %u\n"
3123 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05003124 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05003125 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05003126 "}\n", getpid (), vep_idx,
3127 session->is_vep, session->is_vep_session,
3128 vep->next_sid, vep->next_sid,
Dave Wallace498b3a52017-11-09 13:00:34 -05003129 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003130
3131 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003132 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003133 rv = vppcom_session_at_index (sid, &session);
3134 if (PREDICT_FALSE (rv))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003135 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003136 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003137 goto done;
3138 }
3139 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05003140 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3141 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003142 else if (PREDICT_FALSE (!session->is_vep_session))
3143 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003144 clib_warning ("VCL<%d>: ERROR: session (%u) "
3145 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003146 goto done;
3147 }
3148 vep = &session->vep;
3149 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05003150 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003151 "vep_idx (%u)!", getpid (),
3152 sid, session->vep.vep_idx, vep_idx);
3153 if (session->is_vep_session)
3154 {
3155 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3156 "{\n"
3157 " next_sid = 0x%x (%u)\n"
3158 " prev_sid = 0x%x (%u)\n"
3159 " vep_idx = 0x%x (%u)\n"
3160 " ev.events = 0x%x\n"
3161 " ev.data.u64 = 0x%llx\n"
3162 " et_mask = 0x%x\n"
3163 "}\n",
3164 vep_idx, sid, sid,
3165 vep->next_sid, vep->next_sid,
3166 vep->prev_sid, vep->prev_sid,
3167 vep->vep_idx, vep->vep_idx,
3168 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003169 }
3170 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003171
3172done:
Dave Wallace048b1d62018-01-03 22:24:41 -05003173 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3174 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003175}
3176
3177int
3178vppcom_epoll_create (void)
3179{
Florin Coras7e12d942018-06-27 14:32:43 -07003180 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003181 u32 vep_idx;
3182
Dave Wallace7e607a72018-06-18 18:41:32 -04003183 VCL_SESSION_LOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04003184 pool_get (vcm->sessions, vep_session);
3185 memset (vep_session, 0, sizeof (*vep_session));
3186 vep_idx = vep_session - vcm->sessions;
3187
3188 vep_session->is_vep = 1;
3189 vep_session->vep.vep_idx = ~0;
3190 vep_session->vep.next_sid = ~0;
3191 vep_session->vep.prev_sid = ~0;
3192 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003193 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)12756512018-03-06 05:55:27 -08003194 vep_session->poll_reg = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003195
Florin Coras0d427d82018-06-27 03:24:07 -07003196 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_idx);
Dave Wallace7e607a72018-06-18 18:41:32 -04003197 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04003198
Florin Coras0d427d82018-06-27 03:24:07 -07003199 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
3200 getpid (), vep_idx, vep_idx);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003201
Dave Wallacef7f809c2017-10-03 01:48:42 -04003202 return (vep_idx);
3203}
3204
3205int
3206vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3207 struct epoll_event *event)
3208{
Florin Coras7e12d942018-06-27 14:32:43 -07003209 vcl_session_t *vep_session;
3210 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003211 int rv;
3212
3213 if (vep_idx == session_index)
3214 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003215 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003216 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003217 return VPPCOM_EINVAL;
3218 }
3219
Dave Wallace7e607a72018-06-18 18:41:32 -04003220 VCL_SESSION_LOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04003221 rv = vppcom_session_at_index (vep_idx, &vep_session);
3222 if (PREDICT_FALSE (rv))
3223 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003224 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003225 goto done;
3226 }
3227 if (PREDICT_FALSE (!vep_session->is_vep))
3228 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003229 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003230 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003231 rv = VPPCOM_EINVAL;
3232 goto done;
3233 }
3234
3235 ASSERT (vep_session->vep.vep_idx == ~0);
3236 ASSERT (vep_session->vep.prev_sid == ~0);
3237
3238 rv = vppcom_session_at_index (session_index, &session);
3239 if (PREDICT_FALSE (rv))
3240 {
Florin Coras0d427d82018-06-27 03:24:07 -07003241 VDBG (0, "VCL<%d>: ERROR: Invalid session_index (%u)!",
3242 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003243 goto done;
3244 }
3245 if (PREDICT_FALSE (session->is_vep))
3246 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003247 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003248 rv = VPPCOM_EINVAL;
3249 goto done;
3250 }
3251
3252 switch (op)
3253 {
3254 case EPOLL_CTL_ADD:
3255 if (PREDICT_FALSE (!event))
3256 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003257 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003258 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003259 rv = VPPCOM_EINVAL;
3260 goto done;
3261 }
3262 if (vep_session->vep.next_sid != ~0)
3263 {
Florin Coras7e12d942018-06-27 14:32:43 -07003264 vcl_session_t *next_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003265 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3266 &next_session);
3267 if (PREDICT_FALSE (rv))
3268 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003269 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003270 "vep.next_sid (%u) on vep_idx (%u)!",
3271 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003272 goto done;
3273 }
3274 ASSERT (next_session->vep.prev_sid == vep_idx);
3275 next_session->vep.prev_sid = session_index;
3276 }
3277 session->vep.next_sid = vep_session->vep.next_sid;
3278 session->vep.prev_sid = vep_idx;
3279 session->vep.vep_idx = vep_idx;
3280 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3281 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003282 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003283 session->is_vep_session = 1;
3284 vep_session->vep.next_sid = session_index;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08003285
3286 /* VCL Event Register handler */
Florin Coras7e12d942018-06-27 14:32:43 -07003287 if (session->session_state & STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08003288 {
3289 /* Register handler for connect_request event on listen_session_index */
3290 vce_event_key_t evk;
3291 evk.session_index = session_index;
3292 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
Keith Burns (alagalah)12756512018-03-06 05:55:27 -08003293 vep_session->poll_reg =
3294 vce_register_handler (&vcm->event_thread, &evk,
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08003295 vce_poll_wait_connect_request_handler_fn,
3296 0 /* No callback args */ );
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08003297 }
Florin Coras0d427d82018-06-27 03:24:07 -07003298 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3299 "sid %u, events 0x%x, data 0x%llx!",
3300 getpid (), vep_idx, session_index,
3301 event->events, event->data.u64);
3302 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003303 break;
3304
3305 case EPOLL_CTL_MOD:
3306 if (PREDICT_FALSE (!event))
3307 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003308 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003309 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003310 rv = VPPCOM_EINVAL;
3311 goto done;
3312 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003313 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003314 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003315 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003316 "not a vep session!", getpid (), session_index);
3317 rv = VPPCOM_EINVAL;
3318 goto done;
3319 }
3320 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3321 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003322 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003323 "vep_idx (%u) != vep_idx (%u)!",
3324 getpid (), session_index,
3325 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003326 rv = VPPCOM_EINVAL;
3327 goto done;
3328 }
3329 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3330 session->vep.ev = *event;
Florin Coras0d427d82018-06-27 03:24:07 -07003331 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3332 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3333 event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003334 break;
3335
3336 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05003337 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003338 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003339 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003340 "not a vep session!", getpid (), session_index);
3341 rv = VPPCOM_EINVAL;
3342 goto done;
3343 }
3344 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3345 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003346 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003347 "vep_idx (%u) != vep_idx (%u)!",
3348 getpid (), session_index,
3349 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003350 rv = VPPCOM_EINVAL;
3351 goto done;
3352 }
3353
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08003354 /* VCL Event Un-register handler */
Florin Coras7e12d942018-06-27 14:32:43 -07003355 if ((session->session_state & STATE_LISTEN) && vep_session->poll_reg)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08003356 {
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08003357 (void) vce_unregister_handler (&vcm->event_thread,
3358 vep_session->poll_reg);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08003359 }
3360
Dave Wallacef7f809c2017-10-03 01:48:42 -04003361 vep_session->wait_cont_idx =
3362 (vep_session->wait_cont_idx == session_index) ?
3363 session->vep.next_sid : vep_session->wait_cont_idx;
3364
3365 if (session->vep.prev_sid == vep_idx)
3366 vep_session->vep.next_sid = session->vep.next_sid;
3367 else
3368 {
Florin Coras7e12d942018-06-27 14:32:43 -07003369 vcl_session_t *prev_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003370 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3371 if (PREDICT_FALSE (rv))
3372 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003373 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003374 "vep.prev_sid (%u) on sid (%u)!",
3375 getpid (), session->vep.prev_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003376 goto done;
3377 }
3378 ASSERT (prev_session->vep.next_sid == session_index);
3379 prev_session->vep.next_sid = session->vep.next_sid;
3380 }
3381 if (session->vep.next_sid != ~0)
3382 {
Florin Coras7e12d942018-06-27 14:32:43 -07003383 vcl_session_t *next_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003384 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3385 if (PREDICT_FALSE (rv))
3386 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003387 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003388 "vep.next_sid (%u) on sid (%u)!",
3389 getpid (), session->vep.next_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003390 goto done;
3391 }
3392 ASSERT (next_session->vep.prev_sid == session_index);
3393 next_session->vep.prev_sid = session->vep.prev_sid;
3394 }
3395
3396 memset (&session->vep, 0, sizeof (session->vep));
3397 session->vep.next_sid = ~0;
3398 session->vep.prev_sid = ~0;
3399 session->vep.vep_idx = ~0;
3400 session->is_vep_session = 0;
Florin Coras0d427d82018-06-27 03:24:07 -07003401 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3402 getpid (), vep_idx, session_index);
3403 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003404 break;
3405
3406 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05003407 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003408 rv = VPPCOM_EINVAL;
3409 }
3410
3411 vep_verify_epoll_chain (vep_idx);
3412
3413done:
Dave Wallace7e607a72018-06-18 18:41:32 -04003414 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04003415 return rv;
3416}
3417
Dave Wallacef7f809c2017-10-03 01:48:42 -04003418int
3419vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3420 int maxevents, double wait_for_time)
3421{
Florin Coras7e12d942018-06-27 14:32:43 -07003422 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003423 int rv;
3424 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05003425 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003426 int num_ev = 0;
3427 u32 vep_next_sid, wait_cont_idx;
3428 u8 is_vep;
3429
3430 if (PREDICT_FALSE (maxevents <= 0))
3431 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003432 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003433 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003434 return VPPCOM_EINVAL;
3435 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003436 memset (events, 0, sizeof (*events) * maxevents);
3437
Dave Wallace7e607a72018-06-18 18:41:32 -04003438 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003439 vep_next_sid = vep_session->vep.next_sid;
3440 is_vep = vep_session->is_vep;
3441 wait_cont_idx = vep_session->wait_cont_idx;
Dave Wallace7e607a72018-06-18 18:41:32 -04003442 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04003443
3444 if (PREDICT_FALSE (!is_vep))
3445 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003446 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003447 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003448 rv = VPPCOM_EINVAL;
3449 goto done;
3450 }
Dave Wallacee695cb42017-11-02 22:04:42 -04003451 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003452 {
Florin Coras0d427d82018-06-27 03:24:07 -07003453 VDBG (1, "VCL<%d>: WARNING: vep_idx (%u) is empty!",
3454 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003455 goto done;
3456 }
3457
3458 do
3459 {
3460 u32 sid;
3461 u32 next_sid = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -07003462 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003463
3464 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3465 sid != ~0; sid = next_sid)
3466 {
3467 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3468 u8 add_event, is_vep_session;
3469 int ready;
3470 u64 session_ev_data;
3471
Dave Wallace7e607a72018-06-18 18:41:32 -04003472 VCL_SESSION_LOCK_AND_GET (sid, &session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003473 next_sid = session->vep.next_sid;
3474 session_events = session->vep.ev.events;
3475 et_mask = session->vep.et_mask;
3476 is_vep = session->is_vep;
3477 is_vep_session = session->is_vep_session;
3478 session_vep_idx = session->vep.vep_idx;
3479 session_ev_data = session->vep.ev.data.u64;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003480
Dave Wallace7e607a72018-06-18 18:41:32 -04003481 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04003482
3483 if (PREDICT_FALSE (is_vep))
3484 {
Florin Coras0d427d82018-06-27 03:24:07 -07003485 VDBG (0, "VCL<%d>: ERROR: sid (%u) is a vep!",
3486 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003487 rv = VPPCOM_EINVAL;
3488 goto done;
3489 }
3490 if (PREDICT_FALSE (!is_vep_session))
3491 {
Florin Coras0d427d82018-06-27 03:24:07 -07003492 VDBG (0, "VCL<%d>: ERROR: session (%u) is not "
3493 "a vep session!", getpid (), sid);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003494 rv = VPPCOM_EINVAL;
3495 goto done;
3496 }
3497 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3498 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003499 clib_warning ("VCL<%d>: ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04003500 "vep_idx (%u) != vep_idx (%u)!",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003501 getpid (), sid, session_vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003502 rv = VPPCOM_EINVAL;
3503 goto done;
3504 }
3505
3506 add_event = clear_et_mask = 0;
3507
Dave Wallace60caa062017-11-10 17:07:13 -05003508 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003509 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003510 VCL_SESSION_LOCK_AND_GET (sid, &session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003511 ready = vppcom_session_read_ready (session, sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04003512 VCL_SESSION_UNLOCK ();
Dave Wallace60caa062017-11-10 17:07:13 -05003513 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003514 {
3515 add_event = 1;
3516 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05003517 if (((EPOLLET | EPOLLIN) & session_events) ==
3518 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003519 clear_et_mask |= EPOLLIN;
3520 }
3521 else if (ready < 0)
3522 {
3523 add_event = 1;
3524 switch (ready)
3525 {
3526 case VPPCOM_ECONNRESET:
3527 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3528 break;
3529
3530 default:
3531 events[num_ev].events |= EPOLLERR;
3532 break;
3533 }
3534 }
3535 }
3536
Dave Wallace60caa062017-11-10 17:07:13 -05003537 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003538 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003539 VCL_SESSION_LOCK_AND_GET (sid, &session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003540 ready = vppcom_session_write_ready (session, sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04003541 VCL_SESSION_UNLOCK ();
Dave Wallace60caa062017-11-10 17:07:13 -05003542 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003543 {
3544 add_event = 1;
3545 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05003546 if (((EPOLLET | EPOLLOUT) & session_events) ==
3547 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003548 clear_et_mask |= EPOLLOUT;
3549 }
3550 else if (ready < 0)
3551 {
3552 add_event = 1;
3553 switch (ready)
3554 {
3555 case VPPCOM_ECONNRESET:
3556 events[num_ev].events |= EPOLLHUP;
3557 break;
3558
3559 default:
3560 events[num_ev].events |= EPOLLERR;
3561 break;
3562 }
3563 }
3564 }
3565
3566 if (add_event)
3567 {
3568 events[num_ev].data.u64 = session_ev_data;
3569 if (EPOLLONESHOT & session_events)
3570 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003571 VCL_SESSION_LOCK_AND_GET (sid, &session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003572 session->vep.ev.events = 0;
Dave Wallace7e607a72018-06-18 18:41:32 -04003573 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04003574 }
3575 num_ev++;
3576 if (num_ev == maxevents)
3577 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003578 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003579 vep_session->wait_cont_idx = next_sid;
Dave Wallace7e607a72018-06-18 18:41:32 -04003580 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04003581 goto done;
3582 }
3583 }
3584 if (wait_cont_idx != ~0)
3585 {
3586 if (next_sid == ~0)
3587 next_sid = vep_next_sid;
3588 else if (next_sid == wait_cont_idx)
3589 next_sid = ~0;
3590 }
3591 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003592 if (wait_for_time != -1)
3593 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003594 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05003595 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003596
3597 if (wait_cont_idx != ~0)
3598 {
Dave Wallace7e607a72018-06-18 18:41:32 -04003599 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003600 vep_session->wait_cont_idx = ~0;
Dave Wallace7e607a72018-06-18 18:41:32 -04003601 VCL_SESSION_UNLOCK ();
Dave Wallacef7f809c2017-10-03 01:48:42 -04003602 }
3603done:
3604 return (rv != VPPCOM_OK) ? rv : num_ev;
3605}
3606
Dave Wallace35830af2017-10-09 01:43:42 -04003607int
3608vppcom_session_attr (uint32_t session_index, uint32_t op,
3609 void *buffer, uint32_t * buflen)
3610{
Florin Coras7e12d942018-06-27 14:32:43 -07003611 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04003612 int rv = VPPCOM_OK;
3613 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07003614 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04003615
Dave Wallace7e607a72018-06-18 18:41:32 -04003616 VCL_SESSION_LOCK_AND_GET (session_index, &session);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003617
3618 ASSERT (session);
3619
Dave Wallace35830af2017-10-09 01:43:42 -04003620 switch (op)
3621 {
3622 case VPPCOM_ATTR_GET_NREAD:
3623 rv = vppcom_session_read_ready (session, session_index);
Florin Coras0d427d82018-06-27 03:24:07 -07003624 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
3625 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003626 break;
3627
Dave Wallace227867f2017-11-13 21:21:53 -05003628 case VPPCOM_ATTR_GET_NWRITE:
3629 rv = vppcom_session_write_ready (session, session_index);
Florin Coras0d427d82018-06-27 03:24:07 -07003630 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
3631 getpid (), session_index, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04003632 break;
3633
3634 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003635 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003636 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003637 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
3638 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003639 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07003640 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
3641 "is_nonblocking = %u", getpid (),
3642 session_index, *flags,
3643 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003644 }
3645 else
3646 rv = VPPCOM_EINVAL;
3647 break;
3648
3649 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05003650 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04003651 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003652 if (*flags & O_NONBLOCK)
3653 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
3654 else
3655 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
3656
Florin Coras0d427d82018-06-27 03:24:07 -07003657 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
3658 " is_nonblocking = %u",
3659 getpid (), session_index, *flags,
3660 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04003661 }
3662 else
3663 rv = VPPCOM_EINVAL;
3664 break;
3665
3666 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003667 if (PREDICT_TRUE (buffer && buflen &&
3668 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003669 {
Florin Coras7e12d942018-06-27 14:32:43 -07003670 ep->is_ip4 = session->transport.is_ip4;
3671 ep->port = session->transport.rmt_port;
3672 if (session->transport.is_ip4)
3673 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07003674 sizeof (ip4_address_t));
3675 else
Florin Coras7e12d942018-06-27 14:32:43 -07003676 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07003677 sizeof (ip6_address_t));
3678 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07003679 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
3680 "addr = %U, port %u", getpid (),
3681 session_index, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003682 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003683 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3684 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003685 }
3686 else
3687 rv = VPPCOM_EINVAL;
3688 break;
3689
3690 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003691 if (PREDICT_TRUE (buffer && buflen &&
3692 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04003693 {
Florin Coras7e12d942018-06-27 14:32:43 -07003694 ep->is_ip4 = session->transport.is_ip4;
3695 ep->port = session->transport.lcl_port;
3696 if (session->transport.is_ip4)
3697 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07003698 sizeof (ip4_address_t));
3699 else
Florin Coras7e12d942018-06-27 14:32:43 -07003700 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07003701 sizeof (ip6_address_t));
3702 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07003703 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
3704 " addr = %U port %d", getpid (),
3705 session_index, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07003706 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07003707 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
3708 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04003709 }
3710 else
3711 rv = VPPCOM_EINVAL;
3712 break;
Stevenb5a11602017-10-11 09:59:30 -07003713
Dave Wallace048b1d62018-01-03 22:24:41 -05003714 case VPPCOM_ATTR_GET_LIBC_EPFD:
3715 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07003716 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
3717 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05003718 break;
3719
3720 case VPPCOM_ATTR_SET_LIBC_EPFD:
3721 if (PREDICT_TRUE (buffer && buflen &&
3722 (*buflen == sizeof (session->libc_epfd))))
3723 {
3724 session->libc_epfd = *(int *) buffer;
3725 *buflen = sizeof (session->libc_epfd);
3726
Florin Coras0d427d82018-06-27 03:24:07 -07003727 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
3728 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003729 }
3730 else
3731 rv = VPPCOM_EINVAL;
3732 break;
3733
3734 case VPPCOM_ATTR_GET_PROTOCOL:
3735 if (buffer && buflen && (*buflen >= sizeof (int)))
3736 {
Florin Coras7e12d942018-06-27 14:32:43 -07003737 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05003738 *buflen = sizeof (int);
3739
Florin Coras0d427d82018-06-27 03:24:07 -07003740 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
3741 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
3742 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003743 }
3744 else
3745 rv = VPPCOM_EINVAL;
3746 break;
3747
3748 case VPPCOM_ATTR_GET_LISTEN:
3749 if (buffer && buflen && (*buflen >= sizeof (int)))
3750 {
3751 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3752 VCL_SESS_ATTR_LISTEN);
3753 *buflen = sizeof (int);
3754
Florin Coras0d427d82018-06-27 03:24:07 -07003755 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
3756 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003757 }
3758 else
3759 rv = VPPCOM_EINVAL;
3760 break;
3761
3762 case VPPCOM_ATTR_GET_ERROR:
3763 if (buffer && buflen && (*buflen >= sizeof (int)))
3764 {
3765 *(int *) buffer = 0;
3766 *buflen = sizeof (int);
3767
Florin Coras0d427d82018-06-27 03:24:07 -07003768 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
3769 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003770 }
3771 else
3772 rv = VPPCOM_EINVAL;
3773 break;
3774
3775 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
3776 if (buffer && buflen && (*buflen >= sizeof (u32)))
3777 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003778
3779 /* VPP-TBD */
3780 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003781 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05003782 vcm->cfg.tx_fifo_size);
3783 *buflen = sizeof (u32);
3784
Florin Coras0d427d82018-06-27 03:24:07 -07003785 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
3786 "buflen %d, #VPP-TBD#", getpid (),
3787 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003788 }
3789 else
3790 rv = VPPCOM_EINVAL;
3791 break;
3792
3793 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
3794 if (buffer && buflen && (*buflen == sizeof (u32)))
3795 {
3796 /* VPP-TBD */
3797 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07003798 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
3799 "buflen %d, #VPP-TBD#", getpid (),
3800 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003801 }
3802 else
3803 rv = VPPCOM_EINVAL;
3804 break;
3805
3806 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3807 if (buffer && buflen && (*buflen >= sizeof (u32)))
3808 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003809
3810 /* VPP-TBD */
3811 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003812 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05003813 vcm->cfg.rx_fifo_size);
3814 *buflen = sizeof (u32);
3815
Florin Coras0d427d82018-06-27 03:24:07 -07003816 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
3817 "buflen %d, #VPP-TBD#", getpid (),
3818 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003819 }
3820 else
3821 rv = VPPCOM_EINVAL;
3822 break;
3823
3824 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3825 if (buffer && buflen && (*buflen == sizeof (u32)))
3826 {
3827 /* VPP-TBD */
3828 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07003829 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
3830 "buflen %d, #VPP-TBD#", getpid (),
3831 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003832 }
3833 else
3834 rv = VPPCOM_EINVAL;
3835 break;
3836
3837 case VPPCOM_ATTR_GET_REUSEADDR:
3838 if (buffer && buflen && (*buflen >= sizeof (int)))
3839 {
3840 /* VPP-TBD */
3841 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3842 VCL_SESS_ATTR_REUSEADDR);
3843 *buflen = sizeof (int);
3844
Florin Coras0d427d82018-06-27 03:24:07 -07003845 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
3846 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003847 }
3848 else
3849 rv = VPPCOM_EINVAL;
3850 break;
3851
Stevenb5a11602017-10-11 09:59:30 -07003852 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003853 if (buffer && buflen && (*buflen == sizeof (int)) &&
3854 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3855 {
3856 /* VPP-TBD */
3857 if (*(int *) buffer)
3858 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3859 else
3860 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3861
Florin Coras0d427d82018-06-27 03:24:07 -07003862 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
3863 " #VPP-TBD#", getpid (),
3864 VCL_SESS_ATTR_TEST (session->attr,
3865 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003866 }
3867 else
3868 rv = VPPCOM_EINVAL;
3869 break;
3870
3871 case VPPCOM_ATTR_GET_REUSEPORT:
3872 if (buffer && buflen && (*buflen >= sizeof (int)))
3873 {
3874 /* VPP-TBD */
3875 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3876 VCL_SESS_ATTR_REUSEPORT);
3877 *buflen = sizeof (int);
3878
Florin Coras0d427d82018-06-27 03:24:07 -07003879 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
3880 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003881 }
3882 else
3883 rv = VPPCOM_EINVAL;
3884 break;
3885
3886 case VPPCOM_ATTR_SET_REUSEPORT:
3887 if (buffer && buflen && (*buflen == sizeof (int)) &&
3888 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3889 {
3890 /* VPP-TBD */
3891 if (*(int *) buffer)
3892 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3893 else
3894 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3895
Florin Coras0d427d82018-06-27 03:24:07 -07003896 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
3897 " #VPP-TBD#", getpid (),
3898 VCL_SESS_ATTR_TEST (session->attr,
3899 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003900 }
3901 else
3902 rv = VPPCOM_EINVAL;
3903 break;
3904
3905 case VPPCOM_ATTR_GET_BROADCAST:
3906 if (buffer && buflen && (*buflen >= sizeof (int)))
3907 {
3908 /* VPP-TBD */
3909 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3910 VCL_SESS_ATTR_BROADCAST);
3911 *buflen = sizeof (int);
3912
Florin Coras0d427d82018-06-27 03:24:07 -07003913 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
3914 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003915 }
3916 else
3917 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003918 break;
3919
3920 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003921 if (buffer && buflen && (*buflen == sizeof (int)))
3922 {
3923 /* VPP-TBD */
3924 if (*(int *) buffer)
3925 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3926 else
3927 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3928
Florin Coras0d427d82018-06-27 03:24:07 -07003929 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
3930 "#VPP-TBD#", getpid (),
3931 VCL_SESS_ATTR_TEST (session->attr,
3932 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003933 }
3934 else
3935 rv = VPPCOM_EINVAL;
3936 break;
3937
3938 case VPPCOM_ATTR_GET_V6ONLY:
3939 if (buffer && buflen && (*buflen >= sizeof (int)))
3940 {
3941 /* VPP-TBD */
3942 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3943 VCL_SESS_ATTR_V6ONLY);
3944 *buflen = sizeof (int);
3945
Florin Coras0d427d82018-06-27 03:24:07 -07003946 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
3947 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003948 }
3949 else
3950 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003951 break;
3952
3953 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003954 if (buffer && buflen && (*buflen == sizeof (int)))
3955 {
3956 /* VPP-TBD */
3957 if (*(int *) buffer)
3958 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3959 else
3960 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3961
Florin Coras0d427d82018-06-27 03:24:07 -07003962 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
3963 "#VPP-TBD#", getpid (),
3964 VCL_SESS_ATTR_TEST (session->attr,
3965 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003966 }
3967 else
3968 rv = VPPCOM_EINVAL;
3969 break;
3970
3971 case VPPCOM_ATTR_GET_KEEPALIVE:
3972 if (buffer && buflen && (*buflen >= sizeof (int)))
3973 {
3974 /* VPP-TBD */
3975 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3976 VCL_SESS_ATTR_KEEPALIVE);
3977 *buflen = sizeof (int);
3978
Florin Coras0d427d82018-06-27 03:24:07 -07003979 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3980 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003981 }
3982 else
3983 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003984 break;
Stevenbccd3392017-10-12 20:42:21 -07003985
3986 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003987 if (buffer && buflen && (*buflen == sizeof (int)))
3988 {
3989 /* VPP-TBD */
3990 if (*(int *) buffer)
3991 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3992 else
3993 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3994
Florin Coras0d427d82018-06-27 03:24:07 -07003995 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3996 "#VPP-TBD#", getpid (),
3997 VCL_SESS_ATTR_TEST (session->attr,
3998 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003999 }
4000 else
4001 rv = VPPCOM_EINVAL;
4002 break;
4003
4004 case VPPCOM_ATTR_GET_TCP_NODELAY:
4005 if (buffer && buflen && (*buflen >= sizeof (int)))
4006 {
4007 /* VPP-TBD */
4008 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4009 VCL_SESS_ATTR_TCP_NODELAY);
4010 *buflen = sizeof (int);
4011
Florin Coras0d427d82018-06-27 03:24:07 -07004012 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
4013 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004014 }
4015 else
4016 rv = VPPCOM_EINVAL;
4017 break;
4018
4019 case VPPCOM_ATTR_SET_TCP_NODELAY:
4020 if (buffer && buflen && (*buflen == sizeof (int)))
4021 {
4022 /* VPP-TBD */
4023 if (*(int *) buffer)
4024 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
4025 else
4026 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
4027
Florin Coras0d427d82018-06-27 03:24:07 -07004028 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
4029 "#VPP-TBD#", getpid (),
4030 VCL_SESS_ATTR_TEST (session->attr,
4031 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004032 }
4033 else
4034 rv = VPPCOM_EINVAL;
4035 break;
4036
4037 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
4038 if (buffer && buflen && (*buflen >= sizeof (int)))
4039 {
4040 /* VPP-TBD */
4041 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4042 VCL_SESS_ATTR_TCP_KEEPIDLE);
4043 *buflen = sizeof (int);
4044
Florin Coras0d427d82018-06-27 03:24:07 -07004045 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
4046 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004047 }
4048 else
4049 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004050 break;
4051
4052 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05004053 if (buffer && buflen && (*buflen == sizeof (int)))
4054 {
4055 /* VPP-TBD */
4056 if (*(int *) buffer)
4057 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
4058 else
4059 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
4060
Florin Coras0d427d82018-06-27 03:24:07 -07004061 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
4062 "#VPP-TBD#", getpid (),
4063 VCL_SESS_ATTR_TEST (session->attr,
4064 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004065 }
4066 else
4067 rv = VPPCOM_EINVAL;
4068 break;
4069
4070 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
4071 if (buffer && buflen && (*buflen >= sizeof (int)))
4072 {
4073 /* VPP-TBD */
4074 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4075 VCL_SESS_ATTR_TCP_KEEPINTVL);
4076 *buflen = sizeof (int);
4077
Florin Coras0d427d82018-06-27 03:24:07 -07004078 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
4079 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004080 }
4081 else
4082 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004083 break;
4084
4085 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05004086 if (buffer && buflen && (*buflen == sizeof (int)))
4087 {
4088 /* VPP-TBD */
4089 if (*(int *) buffer)
4090 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
4091 else
4092 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
4093
Florin Coras0d427d82018-06-27 03:24:07 -07004094 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
4095 "#VPP-TBD#", getpid (),
4096 VCL_SESS_ATTR_TEST (session->attr,
4097 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004098 }
4099 else
4100 rv = VPPCOM_EINVAL;
4101 break;
4102
4103 case VPPCOM_ATTR_GET_TCP_USER_MSS:
4104 if (buffer && buflen && (*buflen >= sizeof (u32)))
4105 {
4106 /* VPP-TBD */
4107 *(u32 *) buffer = session->user_mss;
4108 *buflen = sizeof (int);
4109
Florin Coras0d427d82018-06-27 03:24:07 -07004110 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
4111 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004112 }
4113 else
4114 rv = VPPCOM_EINVAL;
4115 break;
4116
4117 case VPPCOM_ATTR_SET_TCP_USER_MSS:
4118 if (buffer && buflen && (*buflen == sizeof (u32)))
4119 {
4120 /* VPP-TBD */
4121 session->user_mss = *(u32 *) buffer;
4122
Florin Coras0d427d82018-06-27 03:24:07 -07004123 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
4124 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05004125 }
4126 else
4127 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07004128 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04004129
4130 default:
4131 rv = VPPCOM_EINVAL;
4132 break;
Dave Wallace35830af2017-10-09 01:43:42 -04004133 }
4134
4135done:
Dave Wallace7e607a72018-06-18 18:41:32 -04004136 VCL_SESSION_UNLOCK ();
Dave Wallace35830af2017-10-09 01:43:42 -04004137 return rv;
4138}
4139
Stevenac1f96d2017-10-24 16:03:58 -07004140int
4141vppcom_session_recvfrom (uint32_t session_index, void *buffer,
4142 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4143{
Stevenac1f96d2017-10-24 16:03:58 -07004144 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07004145 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07004146
4147 if (ep)
4148 {
Dave Wallace7e607a72018-06-18 18:41:32 -04004149 VCL_SESSION_LOCK ();
Stevenac1f96d2017-10-24 16:03:58 -07004150 rv = vppcom_session_at_index (session_index, &session);
4151 if (PREDICT_FALSE (rv))
4152 {
Dave Wallace7e607a72018-06-18 18:41:32 -04004153 VCL_SESSION_UNLOCK ();
Florin Coras0d427d82018-06-27 03:24:07 -07004154 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
4155 getpid (), session_index);
Dave Wallacefaf9d772017-10-26 16:12:04 -04004156 rv = VPPCOM_EBADFD;
Dave Wallace7e607a72018-06-18 18:41:32 -04004157 VCL_SESSION_UNLOCK ();
Dave Wallacefaf9d772017-10-26 16:12:04 -04004158 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07004159 }
Florin Coras7e12d942018-06-27 14:32:43 -07004160 ep->is_ip4 = session->transport.is_ip4;
4161 ep->port = session->transport.rmt_port;
4162 if (session->transport.is_ip4)
4163 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
Stevenac1f96d2017-10-24 16:03:58 -07004164 sizeof (ip4_address_t));
4165 else
Florin Coras7e12d942018-06-27 14:32:43 -07004166 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
Stevenac1f96d2017-10-24 16:03:58 -07004167 sizeof (ip6_address_t));
Dave Wallace7e607a72018-06-18 18:41:32 -04004168 VCL_SESSION_UNLOCK ();
Stevenac1f96d2017-10-24 16:03:58 -07004169 }
Steven58f464e2017-10-25 12:33:12 -07004170
4171 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07004172 rv = vppcom_session_read (session_index, buffer, buflen);
4173 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07004174 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07004175 else
4176 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004177 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
4178 getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07004179 rv = VPPCOM_EAFNOSUPPORT;
4180 }
4181
Dave Wallacefaf9d772017-10-26 16:12:04 -04004182done:
Stevenac1f96d2017-10-24 16:03:58 -07004183 return rv;
4184}
4185
4186int
4187vppcom_session_sendto (uint32_t session_index, void *buffer,
4188 uint32_t buflen, int flags, vppcom_endpt_t * ep)
4189{
Dave Wallace617dffa2017-10-26 14:47:06 -04004190 if (!buffer)
4191 return VPPCOM_EINVAL;
4192
4193 if (ep)
4194 {
4195 // TBD
4196 return VPPCOM_EINVAL;
4197 }
4198
4199 if (flags)
4200 {
4201 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07004202 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
4203 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04004204 }
4205
4206 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07004207}
4208
Dave Wallace048b1d62018-01-03 22:24:41 -05004209int
4210vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
4211{
4212 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
4213 u32 i, keep_trying = 1;
4214 int rv, num_ev = 0;
4215
Florin Coras0d427d82018-06-27 03:24:07 -07004216 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
4217 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05004218
4219 if (!vp)
4220 return VPPCOM_EFAULT;
4221
4222 do
4223 {
Florin Coras7e12d942018-06-27 14:32:43 -07004224 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05004225
4226 for (i = 0; i < n_sids; i++)
4227 {
4228 ASSERT (vp[i].revents);
4229
Dave Wallace7e607a72018-06-18 18:41:32 -04004230 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
4231 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05004232
4233 if (*vp[i].revents)
4234 *vp[i].revents = 0;
4235
4236 if (POLLIN & vp[i].events)
4237 {
Dave Wallace7e607a72018-06-18 18:41:32 -04004238 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004239 rv = vppcom_session_read_ready (session, vp[i].sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04004240 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05004241 if (rv > 0)
4242 {
4243 *vp[i].revents |= POLLIN;
4244 num_ev++;
4245 }
4246 else if (rv < 0)
4247 {
4248 switch (rv)
4249 {
4250 case VPPCOM_ECONNRESET:
4251 *vp[i].revents = POLLHUP;
4252 break;
4253
4254 default:
4255 *vp[i].revents = POLLERR;
4256 break;
4257 }
4258 num_ev++;
4259 }
4260 }
4261
4262 if (POLLOUT & vp[i].events)
4263 {
Dave Wallace7e607a72018-06-18 18:41:32 -04004264 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
Dave Wallace048b1d62018-01-03 22:24:41 -05004265 rv = vppcom_session_write_ready (session, vp[i].sid);
Dave Wallace7e607a72018-06-18 18:41:32 -04004266 VCL_SESSION_UNLOCK ();
Dave Wallace048b1d62018-01-03 22:24:41 -05004267 if (rv > 0)
4268 {
4269 *vp[i].revents |= POLLOUT;
4270 num_ev++;
4271 }
4272 else if (rv < 0)
4273 {
4274 switch (rv)
4275 {
4276 case VPPCOM_ECONNRESET:
4277 *vp[i].revents = POLLHUP;
4278 break;
4279
4280 default:
4281 *vp[i].revents = POLLERR;
4282 break;
4283 }
4284 num_ev++;
4285 }
4286 }
4287
Dave Wallace7e607a72018-06-18 18:41:32 -04004288 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05004289 {
4290 done:
4291 *vp[i].revents = POLLNVAL;
4292 num_ev++;
4293 }
4294 }
4295 if (wait_for_time != -1)
4296 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4297 }
4298 while ((num_ev == 0) && keep_trying);
4299
4300 if (VPPCOM_DEBUG > 3)
4301 {
4302 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
4303 for (i = 0; i < n_sids; i++)
4304 {
4305 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
4306 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
4307 vp[i].events, *vp[i].revents);
4308 }
4309 }
4310 return num_ev;
4311}
4312
Dave Wallacee22aa742017-10-20 12:30:38 -04004313/*
Dave Wallace7e607a72018-06-18 18:41:32 -04004314 * VPPCOM Event Functions
4315 */
4316
4317void *
4318vppcom_session_io_thread_fn (void *arg)
4319{
4320 vppcom_session_io_thread_t *evt = (vppcom_session_io_thread_t *) arg;
4321 u32 *session_indexes = 0, *session_index;
4322 int i, rv;
4323 u32 bytes = 0;
Florin Coras7e12d942018-06-27 14:32:43 -07004324 vcl_session_t *session;
Dave Wallace7e607a72018-06-18 18:41:32 -04004325
4326 while (1)
4327 {
4328 vec_reset_length (session_indexes);
4329 VCE_IO_SESSIONS_LOCK ();
4330 pool_foreach (session_index, evt->active_session_indexes, (
4331 {
4332 vec_add1
4333 (session_indexes,
4334 *session_index);
4335 }
4336 ));
4337 VCE_IO_SESSIONS_UNLOCK ();
4338 if (session_indexes)
4339 {
4340 for (i = 0; i < vec_len (session_indexes); ++i)
4341 {
4342 VCL_SESSION_LOCK_AND_GET (session_indexes[i], &session);
4343 bytes = svm_fifo_max_dequeue (session->rx_fifo);
4344 VCL_SESSION_UNLOCK ();
4345
4346 if (bytes)
4347 {
4348 vppcom_ioevent_t *eio;
4349 vce_event_t *ev;
4350 u32 ev_idx;
4351
4352 VCL_EVENTS_LOCK ();
4353
4354 pool_get (vcm->event_thread.vce_events, ev);
4355 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
4356 eio = vce_get_event_data (ev, sizeof (*eio));
4357 ev->evk.eid = VCL_EVENT_IOEVENT_RX_FIFO;
4358 ev->evk.session_index = session_indexes[i];
4359 eio->bytes = bytes;
4360 eio->session_index = session_indexes[i];
4361
4362 VCL_EVENTS_UNLOCK ();
4363
4364 rv = vce_generate_event (&vcm->event_thread, ev_idx);
4365 }
4366 }
4367 }
4368 struct timespec ts;
4369 ts.tv_sec = 0;
4370 ts.tv_nsec = 1000000; /* 1 millisecond */
4371 nanosleep (&ts, NULL);
4372 }
4373done:
4374 VCL_SESSION_UNLOCK ();
4375 return NULL;
4376}
4377
4378int
4379vppcom_start_io_event_thread (vppcom_session_io_thread_t * evt,
4380 u8 max_sessions)
4381{
4382 pthread_cond_init (&(evt->vce_io_cond), NULL);
4383 pthread_mutex_init (&(evt->vce_io_lock), NULL);
4384
4385 clib_spinlock_init (&(evt->io_sessions_lockp));
4386
4387 return pthread_create (&(evt->thread), NULL /* attr */ ,
4388 vppcom_session_io_thread_fn, evt);
4389}
4390
4391void
4392vce_registered_ioevent_handler_fn (void *arg)
4393{
4394 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
4395 vppcom_ioevent_t *eio;
4396 vce_event_t *ev;
4397 u32 ioevt_ndx = (u64) (reg->handler_fn_args);
4398 vppcom_session_ioevent_t *ioevent, ioevent_;
4399
4400 VCL_EVENTS_LOCK ();
4401 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
4402 eio = vce_get_event_data (ev, sizeof (*eio));
4403 VCL_EVENTS_UNLOCK ();
4404
4405 VCL_IO_SESSIONS_LOCK ();
4406 ioevent = pool_elt_at_index (vcm->session_io_thread.ioevents, ioevt_ndx);
4407 ioevent_ = *ioevent;
4408 VCL_IO_SESSIONS_UNLOCK ();
4409 (ioevent_.user_cb) (eio, ioevent_.user_cb_data);
4410 vce_clear_event (&vcm->event_thread, reg->ev_idx);
4411 return;
4412
4413 /*TODO - Unregister check in close for this listener */
4414
4415}
4416
4417void
4418vce_registered_listener_connect_handler_fn (void *arg)
4419{
4420 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
4421 vce_event_connect_request_t *ecr;
4422 vce_event_t *ev;
4423 vppcom_endpt_t ep;
4424
Florin Coras7e12d942018-06-27 14:32:43 -07004425 vcl_session_t *new_session;
Dave Wallace7e607a72018-06-18 18:41:32 -04004426 int rv;
4427
4428 vppcom_session_listener_t *session_listener =
4429 (vppcom_session_listener_t *) reg->handler_fn_args;
4430
4431 VCL_EVENTS_LOCK ();
4432 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
4433 ecr = vce_get_event_data (ev, sizeof (*ecr));
4434 VCL_EVENTS_UNLOCK ();
4435 VCL_SESSION_LOCK_AND_GET (ecr->accepted_session_index, &new_session);
4436
Florin Coras7e12d942018-06-27 14:32:43 -07004437 ep.is_ip4 = new_session->transport.is_ip4;
4438 ep.port = new_session->transport.rmt_port;
4439 if (new_session->transport.is_ip4)
4440 clib_memcpy (&ep.ip, &new_session->transport.rmt_ip.ip4,
Dave Wallace7e607a72018-06-18 18:41:32 -04004441 sizeof (ip4_address_t));
4442 else
Florin Coras7e12d942018-06-27 14:32:43 -07004443 clib_memcpy (&ep.ip, &new_session->transport.rmt_ip.ip6,
Dave Wallace7e607a72018-06-18 18:41:32 -04004444 sizeof (ip6_address_t));
4445
4446 vppcom_send_accept_session_reply (new_session->vpp_handle,
4447 new_session->client_context,
4448 0 /* retval OK */ );
4449 VCL_SESSION_UNLOCK ();
4450
4451 (session_listener->user_cb) (ecr->accepted_session_index, &ep,
4452 session_listener->user_cb_data);
4453
4454 if (vcm->session_io_thread.io_sessions_lockp)
4455 {
4456 /* Throw this new accepted session index into the rx poll thread pool */
4457 VCL_IO_SESSIONS_LOCK ();
4458 u32 *active_session_index;
4459 pool_get (vcm->session_io_thread.active_session_indexes,
4460 active_session_index);
4461 *active_session_index = ecr->accepted_session_index;
4462 VCL_IO_SESSIONS_UNLOCK ();
4463 }
4464
4465 /*TODO - Unregister check in close for this listener */
4466 return;
4467
4468done:
4469 ASSERT (0); // If we can't get a lock or accepted session fails, lets blow up.
4470}
4471
4472/**
4473 * @brief vce_poll_wait_connect_request_handler_fn
4474 * - used by vppcom_epoll_xxxx() for listener sessions
4475 * - when a vl_api_accept_session_t_handler() generates an event
4476 * this callback is alerted and sets the fields that vppcom_epoll_wait()
4477 * expects to see.
4478 *
4479 * @param arg - void* to be cast to vce_event_handler_reg_t*
4480 */
4481void
4482vce_poll_wait_connect_request_handler_fn (void *arg)
4483{
4484 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
4485 vce_event_t *ev;
4486 /* Retrieve the VCL_EVENT_CONNECT_REQ_ACCEPTED event */
4487 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
4488 vce_event_connect_request_t *ecr = vce_get_event_data (ev, sizeof (*ecr));
4489
4490 /* Add the accepted_session_index to the FIFO */
4491 VCL_ACCEPT_FIFO_LOCK ();
4492 clib_fifo_add1 (vcm->client_session_index_fifo,
4493 ecr->accepted_session_index);
4494 VCL_ACCEPT_FIFO_UNLOCK ();
4495
4496 /* Recycling the event. */
4497 VCL_EVENTS_LOCK ();
4498 ev->recycle = 1;
4499 clib_fifo_add1 (vcm->event_thread.event_index_fifo, reg->ev_idx);
4500 VCL_EVENTS_UNLOCK ();
4501}
4502
4503int
4504vppcom_session_register_ioevent_cb (uint32_t session_index,
4505 vppcom_session_ioevent_cb cb,
4506 uint8_t rx, void *ptr)
4507{
4508 int rv = VPPCOM_OK;
4509 vce_event_key_t evk;
4510 vppcom_session_ioevent_t *ioevent;
4511
4512 if (!vcm->session_io_thread.io_sessions_lockp)
4513 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100 /* DAW_TODO: ??? hard-coded value */
4514 );
4515
4516 if (rv == VPPCOM_OK)
4517 {
4518 void *io_evt_ndx;
4519
4520 /* Register handler for ioevent on session_index */
4521 VCL_IO_SESSIONS_LOCK ();
4522 pool_get (vcm->session_io_thread.ioevents, ioevent);
4523 io_evt_ndx = (void *) (ioevent - vcm->session_io_thread.ioevents);
4524 ioevent->user_cb = cb;
4525 ioevent->user_cb_data = ptr;
4526 VCL_IO_SESSIONS_UNLOCK ();
4527
4528 evk.session_index = session_index;
4529 evk.eid = rx ? VCL_EVENT_IOEVENT_RX_FIFO : VCL_EVENT_IOEVENT_TX_FIFO;
4530
4531 (void) vce_register_handler (&vcm->event_thread, &evk,
4532 vce_registered_ioevent_handler_fn,
4533 io_evt_ndx);
4534 }
4535 return rv;
4536}
4537
4538int
4539vppcom_session_register_listener (uint32_t session_index,
4540 vppcom_session_listener_cb cb,
4541 vppcom_session_listener_errcb
4542 errcb, uint8_t flags, int q_len, void *ptr)
4543{
4544 int rv = VPPCOM_OK;
4545 vce_event_key_t evk;
4546 vppcom_session_listener_t *listener_args;
4547
4548 if (!vcm->session_io_thread.io_sessions_lockp)
4549 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100 /* DAW_TODO: ??? hard-coded value */
4550 );
4551 if (rv)
4552 {
4553 goto done;
4554 }
4555 rv = vppcom_session_listen (session_index, q_len);
4556 if (rv)
4557 {
4558 goto done;
4559 }
4560
4561 /* Register handler for connect_request event on listen_session_index */
4562 listener_args = clib_mem_alloc (sizeof (vppcom_session_listener_t)); // DAW_TODO: Use a pool instead of thrashing the memory allocator!
4563 listener_args->user_cb = cb;
4564 listener_args->user_cb_data = ptr;
4565 listener_args->user_errcb = errcb;
4566
4567 evk.session_index = session_index;
4568 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
4569 (void) vce_register_handler (&vcm->event_thread, &evk,
4570 vce_registered_listener_connect_handler_fn,
4571 listener_args);
4572
4573done:
4574 return rv;
4575}
4576
4577/*
Dave Wallacee22aa742017-10-20 12:30:38 -04004578 * fd.io coding-style-patch-verification: ON
4579 *
4580 * Local Variables:
4581 * eval: (c-set-style "gnu")
4582 * End:
4583 */