blob: dd7e90e2a99496060c662bc7d2c41106661ac3c1 [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>
Dave Wallace543852a2017-08-03 02:11:34 -040024#include <vlib/unix/unix.h>
25#include <vppinfra/vec_bootstrap.h>
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -080026#include <vppinfra/elog.h>
Dave Wallace543852a2017-08-03 02:11:34 -040027
28#define vl_typedefs /* define message structures */
29#include <vpp/api/vpe_all_api_h.h>
30#undef vl_typedefs
31
32/* declare message handlers for each api */
33
34#define vl_endianfun /* define message structures */
35#include <vpp/api/vpe_all_api_h.h>
36#undef vl_endianfun
37
38/* instantiate all the print functions we know about */
39#define vl_print(handle, ...)
40#define vl_printfun
41#include <vpp/api/vpe_all_api_h.h>
42#undef vl_printfun
43
44#if (CLIB_DEBUG > 0)
Dave Wallace498b3a52017-11-09 13:00:34 -050045/* Set VPPCOM_DEBUG_INIT 2 for connection debug,
46 * 3 for read/write debug output
47 * or
48 * export VCL_DEBUG=<#> to set dynamically.
49 */
50#define VPPCOM_DEBUG_INIT 1
Dave Wallace543852a2017-08-03 02:11:34 -040051#else
Dave Wallace498b3a52017-11-09 13:00:34 -050052#define VPPCOM_DEBUG_INIT 0
Dave Wallace543852a2017-08-03 02:11:34 -040053#endif
54
Dave Wallace498b3a52017-11-09 13:00:34 -050055#define VPPCOM_DEBUG vcm->debug
56
Dave Wallace543852a2017-08-03 02:11:34 -040057/*
58 * VPPCOM Private definitions and functions.
59 */
60typedef enum
61{
62 STATE_APP_START,
63 STATE_APP_CONN_VPP,
64 STATE_APP_ENABLED,
65 STATE_APP_ATTACHED,
66} app_state_t;
67
68typedef enum
69{
Dave Wallace4878cbe2017-11-21 03:45:09 -050070 STATE_START = 0x01,
71 STATE_CONNECT = 0x02,
72 STATE_LISTEN = 0x04,
73 STATE_ACCEPT = 0x08,
74 STATE_CLOSE_ON_EMPTY = 0x10,
75 STATE_DISCONNECT = 0x20,
76 STATE_FAILED = 0x40
Dave Wallace543852a2017-08-03 02:11:34 -040077} session_state_t;
78
Dave Wallace4878cbe2017-11-21 03:45:09 -050079#define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
80#define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
81
Dave Wallacef7f809c2017-10-03 01:48:42 -040082typedef struct epoll_event vppcom_epoll_event_t;
83
84typedef struct
85{
86 u32 next_sid;
87 u32 prev_sid;
88 u32 vep_idx;
89 vppcom_epoll_event_t ev;
90#define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
Dave Wallace60caa062017-11-10 17:07:13 -050091#define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
Dave Wallacef7f809c2017-10-03 01:48:42 -040092 u32 et_mask;
93} vppcom_epoll_t;
94
Dave Wallace543852a2017-08-03 02:11:34 -040095typedef struct
96{
Dave Wallace35830af2017-10-09 01:43:42 -040097 u8 is_ip4;
98 ip46_address_t ip46;
99} vppcom_ip46_t;
100
Dave Wallace048b1d62018-01-03 22:24:41 -0500101enum
102{
103 VCL_SESS_ATTR_SERVER,
104 VCL_SESS_ATTR_CUT_THRU,
105 VCL_SESS_ATTR_VEP,
106 VCL_SESS_ATTR_VEP_SESSION,
107 VCL_SESS_ATTR_LISTEN, // SOL_SOCKET,SO_ACCEPTCONN
108 VCL_SESS_ATTR_NONBLOCK, // fcntl,O_NONBLOCK
109 VCL_SESS_ATTR_REUSEADDR, // SOL_SOCKET,SO_REUSEADDR
110 VCL_SESS_ATTR_REUSEPORT, // SOL_SOCKET,SO_REUSEPORT
111 VCL_SESS_ATTR_BROADCAST, // SOL_SOCKET,SO_BROADCAST
112 VCL_SESS_ATTR_V6ONLY, // SOL_TCP,IPV6_V6ONLY
113 VCL_SESS_ATTR_KEEPALIVE, // SOL_SOCKET,SO_KEEPALIVE
114 VCL_SESS_ATTR_TCP_NODELAY, // SOL_TCP,TCP_NODELAY
115 VCL_SESS_ATTR_TCP_KEEPIDLE, // SOL_TCP,TCP_KEEPIDLE
116 VCL_SESS_ATTR_TCP_KEEPINTVL, // SOL_TCP,TCP_KEEPINTVL
117 VCL_SESS_ATTR_MAX
118} vppcom_session_attr_t;
119
120#define VCL_SESS_ATTR_SET(ATTR, VAL) \
121do { \
122 (ATTR) |= 1 << (VAL); \
123 } while (0)
124
125#define VCL_SESS_ATTR_CLR(ATTR, VAL) \
126do { \
127 (ATTR) &= ~(1 << (VAL)); \
128 } while (0)
129
130#define VCL_SESS_ATTR_TEST(ATTR, VAL) \
131 ((ATTR) & (1 << (VAL)) ? 1 : 0)
132
Dave Wallace35830af2017-10-09 01:43:42 -0400133typedef struct
134{
Dave Wallace543852a2017-08-03 02:11:34 -0400135 volatile session_state_t state;
136
137 svm_fifo_t *server_rx_fifo;
138 svm_fifo_t *server_tx_fifo;
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;
Florin Corase86a8ed2018-01-05 03:20:25 -0800146 svm_queue_t *vpp_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400147
148 /* Socket configuration state */
Dave Wallace048b1d62018-01-03 22:24:41 -0500149 /* TBD: covert 'is_*' vars to bit in session->attr; */
Dave Wallace543852a2017-08-03 02:11:34 -0400150 u8 is_server;
151 u8 is_listen;
152 u8 is_cut_thru;
153 u8 is_nonblocking;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400154 u8 is_vep;
155 u8 is_vep_session;
Dave Wallace048b1d62018-01-03 22:24:41 -0500156 u32 attr;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400157 u32 wait_cont_idx;
158 vppcom_epoll_t vep;
Dave Wallace048b1d62018-01-03 22:24:41 -0500159 int libc_epfd;
Dave Wallace543852a2017-08-03 02:11:34 -0400160 u32 vrf;
Dave Wallace35830af2017-10-09 01:43:42 -0400161 vppcom_ip46_t lcl_addr;
162 vppcom_ip46_t peer_addr;
Stevenac1f96d2017-10-24 16:03:58 -0700163 u16 lcl_port; // network order
164 u16 peer_port; // network order
Dave Wallace543852a2017-08-03 02:11:34 -0400165 u8 proto;
166 u64 client_queue_address;
167 u64 options[16];
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800168 elog_track_t elog_track;
Dave Wallace543852a2017-08-03 02:11:34 -0400169} session_t;
170
171typedef struct vppcom_cfg_t_
172{
173 u64 heapsize;
Dave Wallacec8f1ee62017-11-29 22:46:32 -0500174 u32 vpp_api_q_length;
Dave Wallace543852a2017-08-03 02:11:34 -0400175 u64 segment_baseva;
176 u32 segment_size;
177 u32 add_segment_size;
178 u32 preallocated_fifo_pairs;
179 u32 rx_fifo_size;
180 u32 tx_fifo_size;
181 u32 event_queue_size;
182 u32 listen_queue_size;
Dave Wallace774169b2017-11-01 20:07:40 -0400183 u8 app_proxy_transport_tcp;
184 u8 app_proxy_transport_udp;
185 u8 app_scope_local;
186 u8 app_scope_global;
Dave Wallace8af20542017-10-26 03:29:30 -0400187 u8 *namespace_id;
188 u64 namespace_secret;
Dave Wallace543852a2017-08-03 02:11:34 -0400189 f64 app_timeout;
190 f64 session_timeout;
191 f64 accept_timeout;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800192 u32 event_ring_size;
193 char *event_log_path;
Dave Wallace543852a2017-08-03 02:11:34 -0400194} vppcom_cfg_t;
195
196typedef struct vppcom_main_t_
197{
198 u8 init;
Dave Wallace498b3a52017-11-09 13:00:34 -0500199 u32 debug;
Dave Wallace543852a2017-08-03 02:11:34 -0400200 u32 *client_session_index_fifo;
Dave Wallace543852a2017-08-03 02:11:34 -0400201 int main_cpu;
202
203 /* vpe input queue */
Florin Corase86a8ed2018-01-05 03:20:25 -0800204 svm_queue_t *vl_input_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400205
206 /* API client handle */
207 u32 my_client_index;
208
209 /* Session pool */
210 clib_spinlock_t sessions_lockp;
211 session_t *sessions;
212
213 /* Hash table for disconnect processing */
214 uword *session_index_by_vpp_handles;
215
216 /* Select bitmaps */
217 clib_bitmap_t *rd_bitmap;
218 clib_bitmap_t *wr_bitmap;
219 clib_bitmap_t *ex_bitmap;
220
221 /* Our event queue */
Florin Corase86a8ed2018-01-05 03:20:25 -0800222 svm_queue_t *app_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400223
224 /* unique segment name counter */
225 u32 unique_segment_index;
226
Dave Wallace543852a2017-08-03 02:11:34 -0400227 /* For deadman timers */
228 clib_time_t clib_time;
229
230 /* State of the connection, shared between msg RX thread and main thread */
231 volatile app_state_t app_state;
232
233 vppcom_cfg_t cfg;
234
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800235 /* Event logging */
236 elog_main_t elog_main;
237 elog_track_t elog_track;
238
Dave Wallace543852a2017-08-03 02:11:34 -0400239 /* VNET_API_ERROR_FOO -> "Foo" hash table */
240 uword *error_string_by_error_number;
241} vppcom_main_t;
242
Dave Wallace2e005bb2017-11-07 01:21:39 -0500243/* NOTE: _vppcom_main is only used until the heap is allocated.
244 * Do not access it directly -- use vcm which will point to
245 * the heap allocated copy after init.
246 */
Dave Wallace498b3a52017-11-09 13:00:34 -0500247static vppcom_main_t _vppcom_main = {
248 .debug = VPPCOM_DEBUG_INIT,
249 .my_client_index = ~0
250};
Dave Wallace2e005bb2017-11-07 01:21:39 -0500251
252static vppcom_main_t *vcm = &_vppcom_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400253
Dave Wallace048b1d62018-01-03 22:24:41 -0500254#define VCL_LOCK_AND_GET_SESSION(I, S) \
255do { \
256 clib_spinlock_lock (&vcm->sessions_lockp); \
257 rv = vppcom_session_at_index (I, S); \
258 if (PREDICT_FALSE (rv)) \
259 { \
260 clib_spinlock_unlock (&vcm->sessions_lockp); \
261 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
262 getpid (), I); \
263 goto done; \
264 } \
Dave Wallace60caa062017-11-10 17:07:13 -0500265} while (0)
266
Dave Wallace543852a2017-08-03 02:11:34 -0400267static const char *
268vppcom_app_state_str (app_state_t state)
269{
270 char *st;
271
272 switch (state)
273 {
274 case STATE_APP_START:
275 st = "STATE_APP_START";
276 break;
277
278 case STATE_APP_CONN_VPP:
279 st = "STATE_APP_CONN_VPP";
280 break;
281
282 case STATE_APP_ENABLED:
283 st = "STATE_APP_ENABLED";
284 break;
285
286 case STATE_APP_ATTACHED:
287 st = "STATE_APP_ATTACHED";
288 break;
289
290 default:
291 st = "UNKNOWN_APP_STATE";
292 break;
293 }
294
295 return st;
296}
297
298static const char *
299vppcom_session_state_str (session_state_t state)
300{
301 char *st;
302
303 switch (state)
304 {
305 case STATE_START:
306 st = "STATE_START";
307 break;
308
309 case STATE_CONNECT:
310 st = "STATE_CONNECT";
311 break;
312
313 case STATE_LISTEN:
314 st = "STATE_LISTEN";
315 break;
316
317 case STATE_ACCEPT:
318 st = "STATE_ACCEPT";
319 break;
320
Dave Wallace4878cbe2017-11-21 03:45:09 -0500321 case STATE_CLOSE_ON_EMPTY:
322 st = "STATE_CLOSE_ON_EMPTY";
323 break;
324
Dave Wallace543852a2017-08-03 02:11:34 -0400325 case STATE_DISCONNECT:
326 st = "STATE_DISCONNECT";
327 break;
328
329 case STATE_FAILED:
330 st = "STATE_FAILED";
331 break;
332
333 default:
334 st = "UNKNOWN_STATE";
335 break;
336 }
337
338 return st;
339}
340
341/*
342 * VPPCOM Utility Functions
343 */
344static inline int
345vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
346{
Dave Wallace543852a2017-08-03 02:11:34 -0400347 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
348 if (PREDICT_FALSE ((session_index == ~0) ||
349 pool_is_free_index (vcm->sessions, session_index)))
350 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500351 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500352 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400353 return VPPCOM_EBADFD;
354 }
355 *sess = pool_elt_at_index (vcm->sessions, session_index);
356 return VPPCOM_OK;
357}
358
Florin Corasdcf55ce2017-11-16 15:32:50 -0800359static inline void
360vppcom_session_table_add_listener (u64 listener_handle, u32 value)
361{
362 /* Session and listener handles have different formats. The latter has
363 * the thread index in the upper 32 bits while the former has the session
364 * type. Knowing that, for listeners we just flip the MSB to 1 */
365 listener_handle |= 1ULL << 63;
366 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
367}
368
369static inline session_t *
370vppcom_session_table_lookup_listener (u64 listener_handle)
371{
372 uword *p;
373 u64 handle = listener_handle | (1ULL << 63);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500374 session_t *session;
375
Florin Corasdcf55ce2017-11-16 15:32:50 -0800376 p = hash_get (vcm->session_index_by_vpp_handles, handle);
377 if (!p)
378 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500379 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
Florin Corasdcf55ce2017-11-16 15:32:50 -0800380 "listener handle %llx", getpid (), listener_handle);
381 return 0;
382 }
383 if (pool_is_free_index (vcm->sessions, p[0]))
384 {
385 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500386 clib_warning ("VCL<%d>: invalid listen session, sid (%u)",
387 getpid (), p[0]);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800388 return 0;
389 }
390
Dave Wallace4878cbe2017-11-21 03:45:09 -0500391 session = pool_elt_at_index (vcm->sessions, p[0]);
392 ASSERT (session->is_listen);
393 return session;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800394}
395
396static inline void
397vppcom_session_table_del_listener (u64 listener_handle)
398{
399 listener_handle |= 1ULL << 63;
400 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
401}
402
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800403static void
404write_elog (void)
405{
406 elog_main_t *em = &vcm->elog_main;
407 char *chroot_file;
408 clib_error_t *error = 0;
409
410 chroot_file =
411 (char *) format (0, "%s/%d-%d-vcl-elog%c", vcm->cfg.event_log_path,
412 vcm->my_client_index, getpid (), 0);
413 error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
414 if (error)
415 {
416 clib_error_report (error);
417 }
418 if (VPPCOM_DEBUG > 0)
419 clib_warning ("[%d] Event Log:'%s' ", getpid (), chroot_file);
420
421}
422
Dave Wallace543852a2017-08-03 02:11:34 -0400423static int
424vppcom_connect_to_vpp (char *app_name)
425{
426 api_main_t *am = &api_main;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800427 int rv = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400428
429 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500430 clib_warning ("VCL<%d>: app (%s) connecting to VPP api...",
431 getpid (), app_name);
432
Dave Wallacec8f1ee62017-11-29 22:46:32 -0500433 if (vl_client_connect_to_vlib ("/vpe-api", app_name,
434 vcm->cfg.vpp_api_q_length) < 0)
Dave Wallace543852a2017-08-03 02:11:34 -0400435 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500436 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800437 rv = VPPCOM_ECONNREFUSED;
438 }
439 else
440 {
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800441 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
442 vcm->my_client_index = am->my_client_index;
443 vcm->app_state = STATE_APP_CONN_VPP;
Dave Wallace9b954252018-01-18 17:01:40 -0500444
445 if (VPPCOM_DEBUG > 0)
446 clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
447 getpid (), app_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400448 }
449
Dave Wallace543852a2017-08-03 02:11:34 -0400450 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800451 {
452 vcm->elog_main.lock =
453 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
454 vcm->elog_main.lock[0] = 0;
455 vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
456 elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
457 elog_enable_disable (&vcm->elog_main, 1);
Dave Wallace543852a2017-08-03 02:11:34 -0400458
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800459 vcm->elog_track.name =
460 (char *) format (0, "P:%d:C:%d%c", getpid (),
461 vcm->my_client_index, 0);
462 elog_track_register (&vcm->elog_main, &vcm->elog_track);
463
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800464 /* *INDENT-OFF* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800465 ELOG_TYPE_DECLARE (e) =
466 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800467 .format = "connect_vpp:rv:%d",
468 .format_args = "i4",
469 };
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800470 struct
471 {
472 u32 data;
473 } *ed;
474 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
475 ed->data = rv;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800476 /* *INDENT-ON* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800477 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800478 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400479}
480
481static u8 *
482format_api_error (u8 * s, va_list * args)
483{
Dave Wallace543852a2017-08-03 02:11:34 -0400484 i32 error = va_arg (*args, u32);
485 uword *p;
486
487 p = hash_get (vcm->error_string_by_error_number, -error);
488
489 if (p)
490 s = format (s, "%s (%d)", p[0], error);
491 else
492 s = format (s, "%d", error);
493 return s;
494}
495
496static void
497vppcom_init_error_string_table (void)
498{
Dave Wallace543852a2017-08-03 02:11:34 -0400499 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
500
501#define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
502 foreach_vnet_api_error;
503#undef _
504
505 hash_set (vcm->error_string_by_error_number, 99, "Misc");
506}
507
508static inline int
509vppcom_wait_for_app_state_change (app_state_t app_state)
510{
Dave Wallace543852a2017-08-03 02:11:34 -0400511 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
512
513 while (clib_time_now (&vcm->clib_time) < timeout)
514 {
515 if (vcm->app_state == app_state)
516 return VPPCOM_OK;
517 }
518 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500519 clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400520 vppcom_app_state_str (app_state), app_state);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800521
522 if (VPPCOM_DEBUG > 0)
523 {
524 /* *INDENT-OFF* */
525 ELOG_TYPE_DECLARE (e) =
526 {
527 .format = "ERR: timeout state:%d",
528 .format_args = "i4",
529 };
530 struct
531 {
532 u32 data;
533 } *ed;
534
535 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
536
537 ed->data = app_state;
538 /* *INDENT-ON* */
539 }
540
Dave Wallace543852a2017-08-03 02:11:34 -0400541 return VPPCOM_ETIMEDOUT;
542}
543
544static inline int
545vppcom_wait_for_session_state_change (u32 session_index,
546 session_state_t state,
547 f64 wait_for_time)
548{
Dave Wallace543852a2017-08-03 02:11:34 -0400549 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
550 session_t *volatile session;
551 int rv;
552
553 do
554 {
555 clib_spinlock_lock (&vcm->sessions_lockp);
556 rv = vppcom_session_at_index (session_index, &session);
557 if (PREDICT_FALSE (rv))
558 {
559 clib_spinlock_unlock (&vcm->sessions_lockp);
560 return rv;
561 }
562 if (session->state == state)
563 {
564 clib_spinlock_unlock (&vcm->sessions_lockp);
565 return VPPCOM_OK;
566 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500567 if (session->state == STATE_FAILED)
568 {
569 clib_spinlock_unlock (&vcm->sessions_lockp);
570 return VPPCOM_ECONNREFUSED;
571 }
572
Dave Wallace543852a2017-08-03 02:11:34 -0400573 clib_spinlock_unlock (&vcm->sessions_lockp);
574 }
575 while (clib_time_now (&vcm->clib_time) < timeout);
576
577 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500578 clib_warning ("VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (),
Dave Wallace4878cbe2017-11-21 03:45:09 -0500579 state, vppcom_session_state_str (state));
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800580
581 if (VPPCOM_DEBUG > 0)
582 {
583 /* *INDENT-OFF* */
584 ELOG_TYPE_DECLARE (e) =
585 {
586 .format = "ERR: timeout state:%d",
587 .format_args = "i4",
588 };
589 struct
590 {
591 u32 data;
592 } *ed;
593
594 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
595
596 ed->data = state;
597 /* *INDENT-ON* */
598 }
599
Dave Wallace543852a2017-08-03 02:11:34 -0400600 return VPPCOM_ETIMEDOUT;
601}
602
603static inline int
604vppcom_wait_for_client_session_index (f64 wait_for_time)
605{
Dave Wallace543852a2017-08-03 02:11:34 -0400606 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
607
608 do
609 {
610 if (clib_fifo_elts (vcm->client_session_index_fifo))
611 return VPPCOM_OK;
612 }
613 while (clib_time_now (&vcm->clib_time) < timeout);
614
615 if (wait_for_time == 0)
616 return VPPCOM_EAGAIN;
617
618 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500619 clib_warning ("VCL<%d>: timeout waiting for client_session_index",
620 getpid ());
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800621
622 if (VPPCOM_DEBUG > 0)
623 {
624 /* *INDENT-OFF* */
625 ELOG_TYPE_DECLARE (e) =
626 {
627 .format = "ERR: timeout waiting for session index :%d",
628 .format_args = "i4",
629 };
630 struct
631 {
632 u32 data;
633 } *ed;
634
635 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
636
637 ed->data = getpid();
638 /* *INDENT-ON* */
639 }
640
Dave Wallace543852a2017-08-03 02:11:34 -0400641 return VPPCOM_ETIMEDOUT;
642}
643
644/*
645 * VPP-API message functions
646 */
647static void
648vppcom_send_session_enable_disable (u8 is_enable)
649{
Dave Wallace543852a2017-08-03 02:11:34 -0400650 vl_api_session_enable_disable_t *bmp;
651 bmp = vl_msg_api_alloc (sizeof (*bmp));
652 memset (bmp, 0, sizeof (*bmp));
653
654 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
655 bmp->client_index = vcm->my_client_index;
656 bmp->context = htonl (0xfeedface);
657 bmp->is_enable = is_enable;
658 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
659}
660
661static int
662vppcom_app_session_enable (void)
663{
Dave Wallace543852a2017-08-03 02:11:34 -0400664 int rv;
665
666 if (vcm->app_state != STATE_APP_ENABLED)
667 {
668 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
669 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
670 if (PREDICT_FALSE (rv))
671 {
672 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500673 clib_warning ("VCL<%d>: application session enable timed out! "
Dave Wallaceee45d412017-11-24 21:44:06 -0500674 "returning %d (%s)",
675 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -0400676 return rv;
677 }
678 }
679 return VPPCOM_OK;
680}
681
682static void
683 vl_api_session_enable_disable_reply_t_handler
684 (vl_api_session_enable_disable_reply_t * mp)
685{
Dave Wallace543852a2017-08-03 02:11:34 -0400686 if (mp->retval)
687 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500688 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400689 format_api_error, ntohl (mp->retval));
690 }
691 else
692 vcm->app_state = STATE_APP_ENABLED;
693}
694
695static void
696vppcom_app_send_attach (void)
697{
Dave Wallace543852a2017-08-03 02:11:34 -0400698 vl_api_application_attach_t *bmp;
Dave Wallace8af20542017-10-26 03:29:30 -0400699 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
Dave Wallace774169b2017-11-01 20:07:40 -0400700 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
701 vcm->cfg.app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -0400702
Dave Wallace543852a2017-08-03 02:11:34 -0400703 bmp = vl_msg_api_alloc (sizeof (*bmp));
704 memset (bmp, 0, sizeof (*bmp));
705
706 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
707 bmp->client_index = vcm->my_client_index;
708 bmp->context = htonl (0xfeedface);
709 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700710 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
Dave Wallace774169b2017-11-01 20:07:40 -0400711 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
712 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
713 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
714 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
715 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
716 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
Florin Corasff6e7692017-12-11 04:59:01 -0800717 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
718 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
719 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
720 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
Florin Corasf32cff62017-12-09 08:15:00 -0800721 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
722 vcm->cfg.preallocated_fifo_pairs;
Florin Corasff6e7692017-12-11 04:59:01 -0800723 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Dave Wallace8af20542017-10-26 03:29:30 -0400724 if (nsid_len)
725 {
726 bmp->namespace_id_len = nsid_len;
727 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
728 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
729 }
Dave Wallace543852a2017-08-03 02:11:34 -0400730 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
731}
732
733static int
734vppcom_app_attach (void)
735{
Dave Wallace543852a2017-08-03 02:11:34 -0400736 int rv;
737
738 vppcom_app_send_attach ();
739 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
740 if (PREDICT_FALSE (rv))
741 {
742 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500743 clib_warning ("VCL<%d>: application attach timed out! "
744 "returning %d (%s)",
Dave Wallaceee45d412017-11-24 21:44:06 -0500745 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -0400746 return rv;
747 }
748 return VPPCOM_OK;
749}
750
751static void
752vppcom_app_detach (void)
753{
Dave Wallace543852a2017-08-03 02:11:34 -0400754 vl_api_application_detach_t *bmp;
755 bmp = vl_msg_api_alloc (sizeof (*bmp));
756 memset (bmp, 0, sizeof (*bmp));
757
758 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
759 bmp->client_index = vcm->my_client_index;
760 bmp->context = htonl (0xfeedface);
761 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
762}
763
764static void
765vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
766 mp)
767{
Dave Wallace543852a2017-08-03 02:11:34 -0400768 static svm_fifo_segment_create_args_t _a;
769 svm_fifo_segment_create_args_t *a = &_a;
770 int rv;
771
772 memset (a, 0, sizeof (*a));
773 if (mp->retval)
774 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500775 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400776 format_api_error, ntohl (mp->retval));
777 return;
778 }
779
780 if (mp->segment_name_length == 0)
781 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500782 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400783 return;
784 }
785
786 a->segment_name = (char *) mp->segment_name;
787 a->segment_size = mp->segment_size;
788
789 ASSERT (mp->app_event_queue_address);
790
791 /* Attach to the segment vpp created */
792 rv = svm_fifo_segment_attach (a);
793 vec_reset_length (a->new_segment_indices);
794 if (PREDICT_FALSE (rv))
795 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500796 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
797 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400798 return;
799 }
800
801 vcm->app_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -0800802 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400803
804 vcm->app_state = STATE_APP_ATTACHED;
805}
806
807static void
808vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
809 mp)
810{
Dave Wallace543852a2017-08-03 02:11:34 -0400811 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -0500812 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400813 ntohl (mp->retval));
814
815 vcm->app_state = STATE_APP_ENABLED;
816}
817
818static void
819vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
820 mp)
821{
Dave Wallace543852a2017-08-03 02:11:34 -0400822 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -0500823 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
Dave Wallace4878cbe2017-11-21 03:45:09 -0500824 getpid (), mp->handle, format_api_error,
825 ntohl (mp->retval));
Dave Wallace543852a2017-08-03 02:11:34 -0400826}
827
828static void
829vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
830{
Dave Wallace543852a2017-08-03 02:11:34 -0400831 static svm_fifo_segment_create_args_t _a;
832 svm_fifo_segment_create_args_t *a = &_a;
833 int rv;
834
835 memset (a, 0, sizeof (*a));
836 a->segment_name = (char *) mp->segment_name;
837 a->segment_size = mp->segment_size;
838 /* Attach to the segment vpp created */
839 rv = svm_fifo_segment_attach (a);
840 vec_reset_length (a->new_segment_indices);
841 if (PREDICT_FALSE (rv))
842 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500843 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500844 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400845 return;
846 }
847 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500848 clib_warning ("VCL<%d>: mapped new segment '%s' size %d", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400849 mp->segment_name, mp->segment_size);
850}
851
852static void
853vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
854{
Dave Wallace543852a2017-08-03 02:11:34 -0400855 uword *p;
Dave Wallace543852a2017-08-03 02:11:34 -0400856
857 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
858 if (p)
859 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500860 int rv;
861 session_t *session = 0;
862 u32 session_index = p[0];
863
864 VCL_LOCK_AND_GET_SESSION (session_index, &session);
865 session->state = STATE_CLOSE_ON_EMPTY;
866
867 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500868 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500869 "setting state to 0x%x (%s)",
Dave Wallace4878cbe2017-11-21 03:45:09 -0500870 getpid (), mp->handle, session_index, session->state,
871 vppcom_session_state_str (session->state));
Dave Wallace543852a2017-08-03 02:11:34 -0400872 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500873 return;
874
875 done:
876 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500877 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500878 "session lookup failed!",
879 getpid (), mp->handle, session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400880 }
881 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500882 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500883 "handle failed!", getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400884}
885
886static void
887vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
888{
Dave Wallace543852a2017-08-03 02:11:34 -0400889 session_t *session = 0;
890 vl_api_reset_session_reply_t *rmp;
891 uword *p;
892 int rv = 0;
893
894 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
895 if (p)
896 {
897 int rval;
898 clib_spinlock_lock (&vcm->sessions_lockp);
899 rval = vppcom_session_at_index (p[0], &session);
900 if (PREDICT_FALSE (rval))
901 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500902 rv = VNET_API_ERROR_INVALID_VALUE_2;
Dave Wallace048b1d62018-01-03 22:24:41 -0500903 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500904 "session lookup failed! returning %d %U",
905 getpid (), mp->handle, p[0],
906 rv, format_api_error, rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400907 }
908 else
Dave Wallace4878cbe2017-11-21 03:45:09 -0500909 {
910 /* TBD: should this disconnect immediately and
911 * flush the fifos?
912 */
913 session->state = STATE_CLOSE_ON_EMPTY;
914
915 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500916 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500917 "state set to %d (%s)!", getpid (),
918 mp->handle, p[0], session->state,
919 vppcom_session_state_str (session->state));
920 }
Dave Wallace543852a2017-08-03 02:11:34 -0400921 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -0400922 }
923 else
924 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500925 rv = VNET_API_ERROR_INVALID_VALUE;
Dave Wallace048b1d62018-01-03 22:24:41 -0500926 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500927 "failed! returning %d %U",
928 getpid (), mp->handle, rv, format_api_error, rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400929 }
930
931 rmp = vl_msg_api_alloc (sizeof (*rmp));
932 memset (rmp, 0, sizeof (*rmp));
933 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
934 rmp->retval = htonl (rv);
935 rmp->handle = mp->handle;
936 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
937}
938
939static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400940vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Wallace543852a2017-08-03 02:11:34 -0400941{
Dave Wallaceee45d412017-11-24 21:44:06 -0500942 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400943 u32 session_index;
944 svm_fifo_t *rx_fifo, *tx_fifo;
945 u8 is_cut_thru = 0;
946 int rv;
947
Dave Wallace4878cbe2017-11-21 03:45:09 -0500948 session_index = mp->context;
Dave Wallaceee45d412017-11-24 21:44:06 -0500949 VCL_LOCK_AND_GET_SESSION (session_index, &session);
950done:
Dave Wallace543852a2017-08-03 02:11:34 -0400951 if (mp->retval)
952 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500953 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500954 "connect failed! %U",
955 getpid (), mp->handle, session_index,
956 format_api_error, ntohl (mp->retval));
957 if (rv == VPPCOM_OK)
958 {
959 session->state = STATE_FAILED;
960 session->vpp_handle = mp->handle;
961 }
962 else
963 {
964 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
965 "Invalid session index (%u)!",
966 getpid (), mp->handle, session_index);
967 }
968 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400969 }
970
Dave Wallaceee45d412017-11-24 21:44:06 -0500971 if (rv)
972 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400973
974 /* We've been redirected */
975 if (mp->segment_name_length > 0)
976 {
977 static svm_fifo_segment_create_args_t _a;
978 svm_fifo_segment_create_args_t *a = &_a;
979
980 is_cut_thru = 1;
981 memset (a, 0, sizeof (*a));
982 a->segment_name = (char *) mp->segment_name;
983 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500984 clib_warning ("VCL<%d>: cut-thru segment: %s\n",
Dave Wallace60caa062017-11-10 17:07:13 -0500985 getpid (), a->segment_name);
986
Dave Wallace543852a2017-08-03 02:11:34 -0400987 rv = svm_fifo_segment_attach (a);
988 vec_reset_length (a->new_segment_indices);
989 if (PREDICT_FALSE (rv))
990 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500991 clib_warning ("VCL<%d>: sm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500992 getpid (), a->segment_name);
Dave Wallaceee45d412017-11-24 21:44:06 -0500993 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400994 }
995 }
996
997 /*
998 * Setup session
999 */
Dave Wallace543852a2017-08-03 02:11:34 -04001000 session->is_cut_thru = is_cut_thru;
Dave Wallace33e002b2017-09-06 01:20:02 -04001001 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
Florin Corase86a8ed2018-01-05 03:20:25 -08001002 svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001003
1004 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1005 rx_fifo->client_session_index = session_index;
1006 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1007 tx_fifo->client_session_index = session_index;
1008
1009 session->server_rx_fifo = rx_fifo;
1010 session->server_tx_fifo = tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -05001011 session->vpp_handle = mp->handle;
Dave Wallace9d1d73a2017-11-20 02:31:48 -05001012 session->lcl_addr.is_ip4 = mp->is_ip4;
1013 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1014 sizeof (session->peer_addr.ip46));
1015 session->lcl_port = mp->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001016 session->state = STATE_CONNECT;
1017
1018 /* Add it to lookup table */
1019 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Wallace60caa062017-11-10 17:07:13 -05001020
1021 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001022 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded!"
Dave Wallaceee45d412017-11-24 21:44:06 -05001023 " session_rx_fifo %p, refcnt %d,"
1024 " session_tx_fifo %p, refcnt %d",
1025 getpid (), mp->handle, session_index,
Dave Wallace60caa062017-11-10 17:07:13 -05001026 session->server_rx_fifo,
1027 session->server_rx_fifo->refcnt,
1028 session->server_tx_fifo, session->server_tx_fifo->refcnt);
Dave Wallaceee45d412017-11-24 21:44:06 -05001029done_unlock:
Dave Wallace543852a2017-08-03 02:11:34 -04001030 clib_spinlock_unlock (&vcm->sessions_lockp);
1031}
1032
1033static void
1034vppcom_send_connect_sock (session_t * session, u32 session_index)
1035{
Dave Wallace543852a2017-08-03 02:11:34 -04001036 vl_api_connect_sock_t *cmp;
1037
1038 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
1039 session->is_server = 0;
1040 cmp = vl_msg_api_alloc (sizeof (*cmp));
1041 memset (cmp, 0, sizeof (*cmp));
1042 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
1043 cmp->client_index = vcm->my_client_index;
Dave Wallace33e002b2017-09-06 01:20:02 -04001044 cmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001045
Dave Wallace543852a2017-08-03 02:11:34 -04001046 cmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04001047 cmp->is_ip4 = session->peer_addr.is_ip4;
1048 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001049 cmp->port = session->peer_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001050 cmp->proto = session->proto;
1051 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
1052 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
1053}
1054
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001055static inline void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001056vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
1057 int rv)
1058{
1059 vl_api_disconnect_session_reply_t *rmp;
1060
1061 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001062 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1063 "sending disconnect msg",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001064 getpid (), vpp_handle, session_index);
1065
1066 rmp = vl_msg_api_alloc (sizeof (*rmp));
1067 memset (rmp, 0, sizeof (*rmp));
1068
1069 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
1070 rmp->retval = htonl (rv);
1071 rmp->handle = vpp_handle;
1072 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1073}
1074
1075static inline void
1076vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001077{
Dave Wallace543852a2017-08-03 02:11:34 -04001078 vl_api_disconnect_session_t *dmp;
Dave Wallace543852a2017-08-03 02:11:34 -04001079
Dave Wallace4878cbe2017-11-21 03:45:09 -05001080 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001081 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1082 "sending disconnect msg",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001083 getpid (), vpp_handle, session_index);
1084
Dave Wallace543852a2017-08-03 02:11:34 -04001085 dmp = vl_msg_api_alloc (sizeof (*dmp));
1086 memset (dmp, 0, sizeof (*dmp));
1087 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
1088 dmp->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001089 dmp->handle = vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001090 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001091}
1092
1093static void
1094vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
1095{
Dave Wallace543852a2017-08-03 02:11:34 -04001096 session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001097 u32 session_index = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -04001098 int rv;
1099
Dave Wallaceee45d412017-11-24 21:44:06 -05001100 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1101done:
Dave Wallace543852a2017-08-03 02:11:34 -04001102 if (mp->retval)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001103 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001104 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
1105 "sid %u: bind failed: %U",
1106 getpid (), mp->handle, session_index,
1107 format_api_error, ntohl (mp->retval));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001108 rv = vppcom_session_at_index (session_index, &session);
1109 if (rv == VPPCOM_OK)
Dave Wallaceee45d412017-11-24 21:44:06 -05001110 {
1111 session->state = STATE_FAILED;
1112 session->vpp_handle = mp->handle;
1113 }
1114 else
1115 {
1116 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1117 "Invalid session index (%u)!",
1118 getpid (), mp->handle, session_index);
1119 }
1120 goto done_unlock;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001121 }
Dave Wallace543852a2017-08-03 02:11:34 -04001122
Dave Wallaceee45d412017-11-24 21:44:06 -05001123 session->vpp_handle = mp->handle;
1124 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1125 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1126 sizeof (session->peer_addr.ip46));
1127 session->lcl_port = mp->lcl_port;
1128 vppcom_session_table_add_listener (mp->handle, session_index);
1129 session->is_listen = 1;
1130 session->state = STATE_LISTEN;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001131
Dave Wallaceee45d412017-11-24 21:44:06 -05001132 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001133 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
Dave Wallaceee45d412017-11-24 21:44:06 -05001134 getpid (), mp->handle, mp->context);
1135done_unlock:
Dave Wallace543852a2017-08-03 02:11:34 -04001136 clib_spinlock_unlock (&vcm->sessions_lockp);
1137}
1138
1139static void
1140vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1141{
Dave Wallace4878cbe2017-11-21 03:45:09 -05001142 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -05001143 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001144 getpid (), mp->context, format_api_error,
1145 ntohl (mp->retval));
Dave Wallace543852a2017-08-03 02:11:34 -04001146
Dave Wallace4878cbe2017-11-21 03:45:09 -05001147 else if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001148 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1149 getpid (), mp->context);
Dave Wallace543852a2017-08-03 02:11:34 -04001150}
1151
1152u8 *
1153format_ip4_address (u8 * s, va_list * args)
1154{
1155 u8 *a = va_arg (*args, u8 *);
1156 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1157}
1158
1159u8 *
1160format_ip6_address (u8 * s, va_list * args)
1161{
1162 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1163 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1164
1165 i_max_n_zero = ARRAY_LEN (a->as_u16);
1166 max_n_zeros = 0;
1167 i_first_zero = i_max_n_zero;
1168 n_zeros = 0;
1169 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1170 {
1171 u32 is_zero = a->as_u16[i] == 0;
1172 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1173 {
1174 i_first_zero = i;
1175 n_zeros = 0;
1176 }
1177 n_zeros += is_zero;
1178 if ((!is_zero && n_zeros > max_n_zeros)
1179 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1180 {
1181 i_max_n_zero = i_first_zero;
1182 max_n_zeros = n_zeros;
1183 i_first_zero = ARRAY_LEN (a->as_u16);
1184 n_zeros = 0;
1185 }
1186 }
1187
1188 last_double_colon = 0;
1189 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1190 {
1191 if (i == i_max_n_zero && max_n_zeros > 1)
1192 {
1193 s = format (s, "::");
1194 i += max_n_zeros - 1;
1195 last_double_colon = 1;
1196 }
1197 else
1198 {
1199 s = format (s, "%s%x",
1200 (last_double_colon || i == 0) ? "" : ":",
1201 clib_net_to_host_u16 (a->as_u16[i]));
1202 last_double_colon = 0;
1203 }
1204 }
1205
1206 return s;
1207}
1208
1209/* Format an IP46 address. */
1210u8 *
1211format_ip46_address (u8 * s, va_list * args)
1212{
1213 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1214 ip46_type_t type = va_arg (*args, ip46_type_t);
1215 int is_ip4 = 1;
1216
1217 switch (type)
1218 {
1219 case IP46_TYPE_ANY:
1220 is_ip4 = ip46_address_is_ip4 (ip46);
1221 break;
1222 case IP46_TYPE_IP4:
1223 is_ip4 = 1;
1224 break;
1225 case IP46_TYPE_IP6:
1226 is_ip4 = 0;
1227 break;
1228 }
1229
1230 return is_ip4 ?
1231 format (s, "%U", format_ip4_address, &ip46->ip4) :
1232 format (s, "%U", format_ip6_address, &ip46->ip6);
1233}
1234
Dave Wallace60caa062017-11-10 17:07:13 -05001235static inline void
Florin Coras50e8bdb2017-11-27 10:37:05 -08001236vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
Dave Wallace60caa062017-11-10 17:07:13 -05001237{
1238 vl_api_accept_session_reply_t *rmp;
1239
1240 rmp = vl_msg_api_alloc (sizeof (*rmp));
1241 memset (rmp, 0, sizeof (*rmp));
1242 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1243 rmp->retval = htonl (retval);
Dave Wallaced2931962017-11-25 04:17:39 -05001244 rmp->context = context;
Dave Wallace60caa062017-11-10 17:07:13 -05001245 rmp->handle = handle;
1246 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1247}
1248
Dave Wallace543852a2017-08-03 02:11:34 -04001249static void
1250vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1251{
Dave Wallace543852a2017-08-03 02:11:34 -04001252 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001253 session_t *session, *listen_session;
Dave Wallace543852a2017-08-03 02:11:34 -04001254 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001255
Dave Wallace60caa062017-11-10 17:07:13 -05001256 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001257 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1258 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001259 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
Dave Wallaced2931962017-11-25 04:17:39 -05001260 vppcom_send_accept_session_reply (mp->handle, mp->context,
Florin Corasdcf55ce2017-11-16 15:32:50 -08001261 VNET_API_ERROR_QUEUE_FULL);
1262 clib_spinlock_unlock (&vcm->sessions_lockp);
1263 return;
1264 }
1265
1266 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1267 if (!listen_session)
1268 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001269 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1270 "unknown vpp listener handle %llx",
1271 getpid (), mp->listener_handle);
Dave Wallace60caa062017-11-10 17:07:13 -05001272 clib_spinlock_unlock (&vcm->sessions_lockp);
1273 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001274 }
1275
Dave Wallace543852a2017-08-03 02:11:34 -04001276 /* Allocate local session and set it up */
1277 pool_get (vcm->sessions, session);
1278 memset (session, 0, sizeof (*session));
1279 session_index = session - vcm->sessions;
1280
1281 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1282 rx_fifo->client_session_index = session_index;
1283 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1284 tx_fifo->client_session_index = session_index;
1285
Dave Wallace60caa062017-11-10 17:07:13 -05001286 session->vpp_handle = mp->handle;
Dave Wallaced2931962017-11-25 04:17:39 -05001287 session->client_context = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -04001288 session->server_rx_fifo = rx_fifo;
1289 session->server_tx_fifo = tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04001290 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
Florin Corase86a8ed2018-01-05 03:20:25 -08001291 svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001292 session->state = STATE_ACCEPT;
1293 session->is_cut_thru = 0;
Dave Wallace19481612017-09-15 18:47:44 -04001294 session->is_server = 1;
Stevenac1f96d2017-10-24 16:03:58 -07001295 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001296 session->peer_addr.is_ip4 = mp->is_ip4;
1297 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1298 sizeof (session->peer_addr.ip46));
Dave Wallace543852a2017-08-03 02:11:34 -04001299
1300 /* Add it to lookup table */
1301 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001302 session->lcl_port = listen_session->lcl_port;
1303 session->lcl_addr = listen_session->lcl_addr;
Dave Wallace227867f2017-11-13 21:21:53 -05001304
1305 /* TBD: move client_session_index_fifo into listener session */
Dave Wallace543852a2017-08-03 02:11:34 -04001306 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001307
Dave Wallacef7f809c2017-10-03 01:48:42 -04001308 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001309
Dave Wallace60caa062017-11-10 17:07:13 -05001310 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001311 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
Florin Coras50e8bdb2017-11-27 10:37:05 -08001312 "request from %s address %U port %d queue %p!", getpid (),
Dave Wallaced2931962017-11-25 04:17:39 -05001313 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1314 format_ip46_address, &mp->ip, mp->is_ip4,
Florin Coras50e8bdb2017-11-27 10:37:05 -08001315 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001316
1317 if (VPPCOM_DEBUG > 0)
1318 {
1319 session->elog_track.name =
1320 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1321 session_index, 0);
1322 elog_track_register (&vcm->elog_main, &session->elog_track);
1323
1324 if (session->peer_addr.is_ip4)
1325 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001326 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001327 ELOG_TYPE_DECLARE (e) =
1328 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001329 .format =
1330 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1331 .format_args = "i8i1i1i1i1i2",
1332 };
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001333
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001334 CLIB_PACKED (struct {
1335 u64 handle; //8
1336 u8 addr[4]; //4
1337 u16 port; //2
1338 }) * ed;
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001339
1340 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1341
1342 ed->handle = mp->handle;
1343 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1344 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1345 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1346 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08001347 ed->port = clib_net_to_host_u16 (session->peer_port);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001348 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001349 }
1350 else
1351 {
1352 clib_warning ("ip6");
1353 }
1354 }
1355
Dave Wallace60caa062017-11-10 17:07:13 -05001356}
1357
1358static void
Dave Wallaceee45d412017-11-24 21:44:06 -05001359vppcom_send_connect_session_reply (session_t * session, u32 session_index,
Dave Wallaced2931962017-11-25 04:17:39 -05001360 u64 vpp_handle, u32 context, int retval)
Dave Wallace60caa062017-11-10 17:07:13 -05001361{
1362 vl_api_connect_session_reply_t *rmp;
1363 u32 len;
Florin Corase86a8ed2018-01-05 03:20:25 -08001364 svm_queue_t *client_q;
Dave Wallace60caa062017-11-10 17:07:13 -05001365
Dave Wallace543852a2017-08-03 02:11:34 -04001366 rmp = vl_msg_api_alloc (sizeof (*rmp));
1367 memset (rmp, 0, sizeof (*rmp));
Dave Wallace60caa062017-11-10 17:07:13 -05001368 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
Dave Wallaced2931962017-11-25 04:17:39 -05001369
1370 if (!session)
1371 {
1372 rmp->context = context;
1373 rmp->handle = vpp_handle;
1374 rmp->retval = htonl (retval);
1375 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1376 return;
1377 }
1378
Dave Wallace227867f2017-11-13 21:21:53 -05001379 rmp->context = session->client_context;
Dave Wallace60caa062017-11-10 17:07:13 -05001380 rmp->retval = htonl (retval);
Dave Wallace227867f2017-11-13 21:21:53 -05001381 rmp->handle = session->vpp_handle;
1382 rmp->server_rx_fifo = pointer_to_uword (session->server_rx_fifo);
1383 rmp->server_tx_fifo = pointer_to_uword (session->server_tx_fifo);
1384 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1385 rmp->segment_size = vcm->cfg.segment_size;
1386 len = vec_len (session->segment_name);
1387 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1388 clib_memcpy (rmp->segment_name, session->segment_name,
1389 rmp->segment_name_length - 1);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001390 clib_memcpy (rmp->lcl_ip, session->peer_addr.ip46.as_u8,
Dave Wallace227867f2017-11-13 21:21:53 -05001391 sizeof (rmp->lcl_ip));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001392 rmp->is_ip4 = session->peer_addr.is_ip4;
1393 rmp->lcl_port = session->peer_port;
Florin Corase86a8ed2018-01-05 03:20:25 -08001394 client_q = uword_to_pointer (session->client_queue_address, svm_queue_t *);
Dave Wallace60caa062017-11-10 17:07:13 -05001395 ASSERT (client_q);
1396 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001397}
1398
1399/*
1400 * Acting as server for redirected connect requests
1401 */
1402static void
1403vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1404{
Dave Wallace543852a2017-08-03 02:11:34 -04001405 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001406 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001407
Dave Wallacef7f809c2017-10-03 01:48:42 -04001408 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001409 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1410 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001411 clib_spinlock_unlock (&vcm->sessions_lockp);
1412
Dave Wallace543852a2017-08-03 02:11:34 -04001413 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001414 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
Dave Wallace4878cbe2017-11-21 03:45:09 -05001415
Dave Wallaced2931962017-11-25 04:17:39 -05001416 /* TBD: Fix api to include vpp handle */
1417 vppcom_send_connect_session_reply (0 /* session */ , 0 /* sid */ ,
1418 0 /* handle */ , mp->context,
1419 VNET_API_ERROR_QUEUE_FULL);
Dave Wallace60caa062017-11-10 17:07:13 -05001420 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001421 }
1422
Dave Wallace543852a2017-08-03 02:11:34 -04001423 pool_get (vcm->sessions, session);
1424 memset (session, 0, sizeof (*session));
1425 session_index = session - vcm->sessions;
1426
Dave Wallace60caa062017-11-10 17:07:13 -05001427 session->client_context = mp->context;
1428 session->vpp_handle = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001429 session->client_queue_address = mp->client_queue_address;
1430 session->is_cut_thru = 1;
1431 session->is_server = 1;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001432 session->lcl_port = mp->port;
1433 session->lcl_addr.is_ip4 = mp->is_ip4;
1434 clib_memcpy (&session->lcl_addr.ip46, mp->ip,
1435 sizeof (session->lcl_addr.ip46));
1436
1437 /* TBD: missing peer info in api msg.
1438 */
Dave Wallace35830af2017-10-09 01:43:42 -04001439 session->peer_addr.is_ip4 = mp->is_ip4;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001440 ASSERT (session->lcl_addr.is_ip4 == session->peer_addr.is_ip4);
Dave Wallace6d5c4cd2017-08-15 16:56:29 -04001441
Dave Wallace543852a2017-08-03 02:11:34 -04001442 session->state = STATE_ACCEPT;
Dave Wallace543852a2017-08-03 02:11:34 -04001443 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001444 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001445 clib_warning ("VCL<%d>: sid %u: Got a cut-thru connect request! "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001446 "clib_fifo_elts %u!\n", getpid (), session_index,
1447 clib_fifo_elts (vcm->client_session_index_fifo));
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001448
1449 if (VPPCOM_DEBUG > 0)
1450 {
1451 session->elog_track.name =
1452 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1453 session_index, 0);
1454 elog_track_register (&vcm->elog_main, &session->elog_track);
1455
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001456 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001457 ELOG_TYPE_DECLARE (e) =
1458 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001459 .format = "cut-thru-connect:S:%d clib_fifo_elts:%d",
1460 .format_args = "i4i4",
1461 };
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001462
1463 struct
1464 {
1465 u32 data[2];
1466 } *ed;
1467
1468 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1469
1470 ed->data[0] = session_index;
1471 ed->data[1] = clib_fifo_elts (vcm->client_session_index_fifo);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001472 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001473 }
1474
Dave Wallacef7f809c2017-10-03 01:48:42 -04001475 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001476}
1477
1478static void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001479vppcom_send_bind_sock (session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001480{
Dave Wallace543852a2017-08-03 02:11:34 -04001481 vl_api_bind_sock_t *bmp;
1482
1483 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1484 session->is_server = 1;
1485 bmp = vl_msg_api_alloc (sizeof (*bmp));
1486 memset (bmp, 0, sizeof (*bmp));
1487
1488 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1489 bmp->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001490 bmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001491 bmp->vrf = session->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04001492 bmp->is_ip4 = session->lcl_addr.is_ip4;
1493 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001494 bmp->port = session->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001495 bmp->proto = session->proto;
1496 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1497 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1498}
1499
1500static void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001501vppcom_send_unbind_sock (u64 vpp_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001502{
Dave Wallace543852a2017-08-03 02:11:34 -04001503 vl_api_unbind_sock_t *ump;
Dave Wallace543852a2017-08-03 02:11:34 -04001504
1505 ump = vl_msg_api_alloc (sizeof (*ump));
1506 memset (ump, 0, sizeof (*ump));
1507
1508 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1509 ump->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001510 ump->handle = vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001511 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1512}
1513
1514static int
Dave Wallace543852a2017-08-03 02:11:34 -04001515vppcom_session_unbind (u32 session_index)
1516{
Dave Wallace4878cbe2017-11-21 03:45:09 -05001517 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001518 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001519 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001520
Dave Wallace4878cbe2017-11-21 03:45:09 -05001521 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1522
1523 vpp_handle = session->vpp_handle;
1524 vppcom_session_table_del_listener (vpp_handle);
1525 session->vpp_handle = ~0;
1526 session->state = STATE_DISCONNECT;
1527
Dave Wallace543852a2017-08-03 02:11:34 -04001528 clib_spinlock_unlock (&vcm->sessions_lockp);
1529
Dave Wallace4878cbe2017-11-21 03:45:09 -05001530 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001531 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001532 "sending unbind msg! new state 0x%x (%s)",
1533 getpid (), vpp_handle, session_index,
1534 session->state, vppcom_session_state_str (session->state));
1535
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001536 if (VPPCOM_DEBUG > 0)
1537 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001538 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001539 ELOG_TYPE_DECLARE (e) =
1540 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001541 .format = "unbind: handle:%x",
1542 .format_args = "i8",
1543 };
1544
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001545 struct
1546 {
1547 u64 handle;
1548 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001549
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001550 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1551 ed->handle = vpp_handle;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001552 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001553 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001554 vppcom_send_unbind_sock (vpp_handle);
1555
1556done:
1557 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001558}
1559
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001560static inline int
Dave Wallace543852a2017-08-03 02:11:34 -04001561vppcom_session_disconnect (u32 session_index)
1562{
Dave Wallace543852a2017-08-03 02:11:34 -04001563 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001564 session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001565 u8 is_cut_thru, is_listen, is_server;
1566 u64 vpp_handle;
1567 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04001568
Dave Wallace4878cbe2017-11-21 03:45:09 -05001569 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1570
1571 vpp_handle = session->vpp_handle;
1572 is_server = session->is_server;
1573 is_listen = session->is_listen;
1574 is_cut_thru = session->is_cut_thru;
1575 state = session->state;
1576 clib_spinlock_unlock (&vcm->sessions_lockp);
1577
1578 if (VPPCOM_DEBUG > 1)
Dave Wallace543852a2017-08-03 02:11:34 -04001579 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001580 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s "
1581 "state 0x%x (%s), is_cut_thru %d, is_listen %d",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001582 getpid (), vpp_handle, session_index,
1583 is_server ? "server" : "client",
1584 state, vppcom_session_state_str (state),
1585 is_cut_thru, is_listen);
Dave Wallace543852a2017-08-03 02:11:34 -04001586 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001587
Dave Wallace4878cbe2017-11-21 03:45:09 -05001588 if (PREDICT_FALSE (is_listen))
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001589 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001590 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001591 "Cannot disconnect a listen socket!",
1592 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001593 rv = VPPCOM_EBADFD;
1594 goto done;
1595 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001596
Dave Wallace4878cbe2017-11-21 03:45:09 -05001597 /* Through the VPP host stack...
1598 */
1599 else if (!is_cut_thru)
1600 {
1601 /* The peer has already initiated the close,
1602 * so send the disconnect session reply.
Dave Wallace227867f2017-11-13 21:21:53 -05001603 */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001604 if (state & STATE_CLOSE_ON_EMPTY)
1605 {
1606 vppcom_send_disconnect_session_reply (vpp_handle,
1607 session_index, 0 /* rv */ );
1608 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001609 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001610 "sending disconnect REPLY...",
1611 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001612 }
1613
1614 /* Otherwise, send a disconnect session msg...
1615 */
1616 else
1617 {
1618 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001619 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001620 "sending disconnect...",
1621 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001622
1623 vppcom_send_disconnect_session (vpp_handle, session_index);
1624 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001625 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001626
1627 /* Cut-thru connections...
1628 *
1629 * server: free fifos and segment allocated during connect/redirect
1630 * client: no cleanup required
1631 */
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001632 else
Dave Wallace227867f2017-11-13 21:21:53 -05001633 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001634 if (is_server)
1635 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001636 svm_fifo_segment_private_t *seg;
1637
1638 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1639
1640 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001641 clib_warning ("VCL<%d>: sid %d: freeing cut-thru fifos in "
Dave Wallaceee45d412017-11-24 21:44:06 -05001642 "sm_seg_index %d! "
1643 " server_rx_fifo %p, refcnt = %d"
1644 " server_tx_fifo %p, refcnt = %d",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001645 getpid (), session_index, session->sm_seg_index,
1646 session->server_rx_fifo,
1647 session->server_rx_fifo->refcnt,
1648 session->server_tx_fifo,
1649 session->server_tx_fifo->refcnt);
1650
Florin Corasb384b542018-01-15 01:08:33 -08001651 seg = svm_fifo_segment_get_segment (session->sm_seg_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001652 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1653 FIFO_SEGMENT_RX_FREELIST);
1654 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1655 FIFO_SEGMENT_TX_FREELIST);
1656 svm_fifo_segment_delete (seg);
1657
1658 /* TBD: Send cut-thru disconnect event to client */
1659
1660 clib_spinlock_unlock (&vcm->sessions_lockp);
1661 }
1662 else
1663 {
1664 /* TBD: Send cut-thru disconnect event to server */
1665 }
Dave Wallace227867f2017-11-13 21:21:53 -05001666 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001667
Dave Wallace4878cbe2017-11-21 03:45:09 -05001668done:
1669 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001670}
1671
1672#define foreach_sock_msg \
1673_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1674_(BIND_SOCK_REPLY, bind_sock_reply) \
1675_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1676_(ACCEPT_SESSION, accept_session) \
1677_(CONNECT_SOCK, connect_sock) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001678_(CONNECT_SESSION_REPLY, connect_session_reply) \
Dave Wallace543852a2017-08-03 02:11:34 -04001679_(DISCONNECT_SESSION, disconnect_session) \
1680_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1681_(RESET_SESSION, reset_session) \
1682_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1683_(APPLICATION_DETACH_REPLY, application_detach_reply) \
1684_(MAP_ANOTHER_SEGMENT, map_another_segment)
1685
1686static void
1687vppcom_api_hookup (void)
1688{
1689#define _(N,n) \
1690 vl_msg_api_set_handlers(VL_API_##N, #n, \
1691 vl_api_##n##_t_handler, \
1692 vl_noop_handler, \
1693 vl_api_##n##_t_endian, \
1694 vl_api_##n##_t_print, \
1695 sizeof(vl_api_##n##_t), 1);
1696 foreach_sock_msg;
1697#undef _
1698}
1699
1700static void
1701vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1702{
1703 ASSERT (vcl_cfg);
1704
1705 vcl_cfg->heapsize = (256ULL << 20);
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001706 vcl_cfg->vpp_api_q_length = 1024;
Dave Wallace543852a2017-08-03 02:11:34 -04001707 vcl_cfg->segment_baseva = 0x200000000ULL;
1708 vcl_cfg->segment_size = (256 << 20);
1709 vcl_cfg->add_segment_size = (128 << 20);
1710 vcl_cfg->preallocated_fifo_pairs = 8;
1711 vcl_cfg->rx_fifo_size = (1 << 20);
1712 vcl_cfg->tx_fifo_size = (1 << 20);
1713 vcl_cfg->event_queue_size = 2048;
1714 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1715 vcl_cfg->app_timeout = 10 * 60.0;
1716 vcl_cfg->session_timeout = 10 * 60.0;
1717 vcl_cfg->accept_timeout = 60.0;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001718 vcl_cfg->event_ring_size = (128 << 10);
1719 vcl_cfg->event_log_path = "/dev/shm";
Dave Wallace543852a2017-08-03 02:11:34 -04001720}
1721
1722static void
1723vppcom_cfg_heapsize (char *conf_fname)
1724{
Dave Wallace543852a2017-08-03 02:11:34 -04001725 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1726 FILE *fp;
1727 char inbuf[4096];
1728 int argc = 1;
1729 char **argv = NULL;
1730 char *arg = NULL;
1731 char *p;
1732 int i;
1733 u8 *sizep;
1734 u32 size;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001735 void *vcl_mem;
1736 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001737
1738 fp = fopen (conf_fname, "r");
1739 if (fp == NULL)
1740 {
1741 if (VPPCOM_DEBUG > 0)
1742 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1743 goto defaulted;
1744 }
1745 argv = calloc (1, sizeof (char *));
1746 if (argv == NULL)
1747 goto defaulted;
1748
1749 while (1)
1750 {
1751 if (fgets (inbuf, 4096, fp) == 0)
1752 break;
1753 p = strtok (inbuf, " \t\n");
1754 while (p != NULL)
1755 {
1756 if (*p == '#')
1757 break;
1758 argc++;
1759 char **tmp = realloc (argv, argc * sizeof (char *));
1760 if (tmp == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001761 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001762 argv = tmp;
1763 arg = strndup (p, 1024);
1764 if (arg == NULL)
Chris Lukeab7b8d92017-09-07 07:40:13 -04001765 goto defaulted;
Dave Wallace543852a2017-08-03 02:11:34 -04001766 argv[argc - 1] = arg;
1767 p = strtok (NULL, " \t\n");
1768 }
1769 }
1770
1771 fclose (fp);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001772 fp = NULL;
Dave Wallace543852a2017-08-03 02:11:34 -04001773
1774 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1775 if (tmp == NULL)
1776 goto defaulted;
1777 argv = tmp;
1778 argv[argc] = NULL;
1779
1780 /*
1781 * Look for and parse the "heapsize" config parameter.
1782 * Manual since none of the clib infra has been bootstrapped yet.
1783 *
1784 * Format: heapsize <nn>[mM][gG]
1785 */
1786
1787 for (i = 1; i < (argc - 1); i++)
1788 {
1789 if (!strncmp (argv[i], "heapsize", 8))
1790 {
1791 sizep = (u8 *) argv[i + 1];
1792 size = 0;
1793 while (*sizep >= '0' && *sizep <= '9')
1794 {
1795 size *= 10;
1796 size += *sizep++ - '0';
1797 }
1798 if (size == 0)
1799 {
1800 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001801 clib_warning ("VCL<%d>: parse error '%s %s', "
Dave Wallace543852a2017-08-03 02:11:34 -04001802 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001803 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001804 vcl_cfg->heapsize, vcl_cfg->heapsize);
1805 goto defaulted;
1806 }
1807
1808 if (*sizep == 'g' || *sizep == 'G')
1809 vcl_cfg->heapsize = size << 30;
1810 else if (*sizep == 'm' || *sizep == 'M')
1811 vcl_cfg->heapsize = size << 20;
1812 else
1813 {
1814 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001815 clib_warning ("VCL<%d>: parse error '%s %s', "
Dave Wallace543852a2017-08-03 02:11:34 -04001816 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001817 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001818 vcl_cfg->heapsize, vcl_cfg->heapsize);
1819 goto defaulted;
1820 }
1821 }
1822 }
1823
1824defaulted:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001825 if (fp != NULL)
1826 fclose (fp);
1827 if (argv != NULL)
1828 free (argv);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001829
Dave Wallace2e005bb2017-11-07 01:21:39 -05001830 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1831 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Steven0cdd5bd2017-11-08 14:14:45 -08001832 if (vcl_mem == MAP_FAILED)
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001833 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001834 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001835 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1836 "-1, 0) failed!",
1837 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1838 return;
1839 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05001840 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1841 if (!heap)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001842 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001843 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001844 return;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001845 }
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001846 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1847 if (!vcl_mem)
1848 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001849 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001850 return;
1851 }
1852
1853 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1854 vcm = vcl_mem;
1855
1856 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001857 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001858 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001859}
1860
1861static void
1862vppcom_cfg_read (char *conf_fname)
1863{
Dave Wallace543852a2017-08-03 02:11:34 -04001864 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1865 int fd;
1866 unformat_input_t _input, *input = &_input;
1867 unformat_input_t _line_input, *line_input = &_line_input;
1868 u8 vc_cfg_input = 0;
1869 u8 *chroot_path;
1870 struct stat s;
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001871 u32 uid, gid, q_len;
Dave Wallace543852a2017-08-03 02:11:34 -04001872
1873 fd = open (conf_fname, O_RDONLY);
1874 if (fd < 0)
1875 {
1876 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001877 clib_warning ("VCL<%d>: open configuration file '%s' failed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001878 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001879 goto file_done;
1880 }
1881
1882 if (fstat (fd, &s) < 0)
1883 {
1884 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001885 clib_warning ("VCL<%d>: failed to stat `%s'", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001886 goto file_done;
1887 }
1888
1889 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1890 {
1891 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001892 clib_warning ("VCL<%d>: not a regular file `%s'",
1893 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001894 goto file_done;
1895 }
1896
Dave Barach59b25652017-09-10 15:04:27 -04001897 unformat_init_clib_file (input, fd);
Dave Wallace543852a2017-08-03 02:11:34 -04001898
1899 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1900 {
Chris Lukeb2bcad62017-09-18 08:51:22 -04001901 (void) unformat_user (input, unformat_line_input, line_input);
Dave Wallace543852a2017-08-03 02:11:34 -04001902 unformat_skip_white_space (line_input);
1903
Dave Wallace8af20542017-10-26 03:29:30 -04001904 if (unformat (line_input, "vcl {"))
Dave Wallace543852a2017-08-03 02:11:34 -04001905 {
1906 vc_cfg_input = 1;
1907 continue;
1908 }
1909
1910 if (vc_cfg_input)
1911 {
1912 if (unformat (line_input, "heapsize %s", &chroot_path))
1913 {
1914 vec_terminate_c_string (chroot_path);
1915 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001916 clib_warning ("VCL<%d>: configured heapsize %s, "
Dave Wallace543852a2017-08-03 02:11:34 -04001917 "actual heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001918 getpid (), chroot_path, vcl_cfg->heapsize,
Dave Wallace543852a2017-08-03 02:11:34 -04001919 vcl_cfg->heapsize);
1920 vec_free (chroot_path);
1921 }
1922 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1923 {
1924 vec_terminate_c_string (chroot_path);
1925 vl_set_memory_root_path ((char *) chroot_path);
1926 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001927 clib_warning ("VCL<%d>: configured api-prefix %s",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001928 getpid (), chroot_path);
Dave Wallace543852a2017-08-03 02:11:34 -04001929 chroot_path = 0; /* Don't vec_free() it! */
1930 }
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001931 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1932 {
1933 if (q_len < vcl_cfg->vpp_api_q_length)
1934 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001935 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001936 "(%u) is too small! Using default: %u ",
1937 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1938 }
1939 else
1940 {
1941 vcl_cfg->vpp_api_q_length = q_len;
1942
1943 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001944 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001945 getpid (), vcl_cfg->vpp_api_q_length);
1946 }
1947 }
Dave Wallace543852a2017-08-03 02:11:34 -04001948 else if (unformat (line_input, "uid %d", &uid))
1949 {
1950 vl_set_memory_uid (uid);
1951 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001952 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
Dave Wallace543852a2017-08-03 02:11:34 -04001953 }
1954 else if (unformat (line_input, "gid %d", &gid))
1955 {
1956 vl_set_memory_gid (gid);
1957 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001958 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
Dave Wallace543852a2017-08-03 02:11:34 -04001959 }
Dave Wallace8af20542017-10-26 03:29:30 -04001960 else if (unformat (line_input, "segment-baseva 0x%lx",
Dave Wallace543852a2017-08-03 02:11:34 -04001961 &vcl_cfg->segment_baseva))
1962 {
1963 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001964 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001965 getpid (), vcl_cfg->segment_baseva);
Dave Wallace543852a2017-08-03 02:11:34 -04001966 }
1967 else if (unformat (line_input, "segment-size 0x%lx",
1968 &vcl_cfg->segment_size))
1969 {
1970 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001971 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001972 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001973 vcl_cfg->segment_size);
1974 }
1975 else if (unformat (line_input, "segment-size %ld",
1976 &vcl_cfg->segment_size))
1977 {
1978 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001979 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001980 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001981 vcl_cfg->segment_size);
1982 }
1983 else if (unformat (line_input, "add-segment-size 0x%lx",
1984 &vcl_cfg->add_segment_size))
1985 {
1986 if (VPPCOM_DEBUG > 0)
1987 clib_warning
Dave Wallace048b1d62018-01-03 22:24:41 -05001988 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001989 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001990 vcl_cfg->add_segment_size);
1991 }
1992 else if (unformat (line_input, "add-segment-size %ld",
1993 &vcl_cfg->add_segment_size))
1994 {
1995 if (VPPCOM_DEBUG > 0)
1996 clib_warning
Dave Wallace048b1d62018-01-03 22:24:41 -05001997 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001998 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001999 vcl_cfg->add_segment_size);
2000 }
2001 else if (unformat (line_input, "preallocated-fifo-pairs %d",
2002 &vcl_cfg->preallocated_fifo_pairs))
2003 {
2004 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002005 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002006 "%d (0x%x)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04002007 vcl_cfg->preallocated_fifo_pairs,
2008 vcl_cfg->preallocated_fifo_pairs);
2009 }
2010 else if (unformat (line_input, "rx-fifo-size 0x%lx",
2011 &vcl_cfg->rx_fifo_size))
2012 {
2013 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002014 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002015 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002016 vcl_cfg->rx_fifo_size);
2017 }
2018 else if (unformat (line_input, "rx-fifo-size %ld",
2019 &vcl_cfg->rx_fifo_size))
2020 {
2021 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002022 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002023 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002024 vcl_cfg->rx_fifo_size);
2025 }
2026 else if (unformat (line_input, "tx-fifo-size 0x%lx",
2027 &vcl_cfg->tx_fifo_size))
2028 {
2029 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002030 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002031 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002032 vcl_cfg->tx_fifo_size);
2033 }
2034 else if (unformat (line_input, "tx-fifo-size %ld",
2035 &vcl_cfg->tx_fifo_size))
2036 {
2037 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002038 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002039 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002040 vcl_cfg->tx_fifo_size);
2041 }
2042 else if (unformat (line_input, "event-queue-size 0x%lx",
2043 &vcl_cfg->event_queue_size))
2044 {
2045 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002046 clib_warning ("VCL<%d>: configured event_queue_size "
2047 "0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002048 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002049 vcl_cfg->event_queue_size);
2050 }
2051 else if (unformat (line_input, "event-queue-size %ld",
2052 &vcl_cfg->event_queue_size))
2053 {
2054 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002055 clib_warning ("VCL<%d>: configured event_queue_size "
2056 "%ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002057 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002058 vcl_cfg->event_queue_size);
2059 }
2060 else if (unformat (line_input, "listen-queue-size 0x%lx",
2061 &vcl_cfg->listen_queue_size))
2062 {
2063 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002064 clib_warning ("VCL<%d>: configured listen_queue_size "
2065 "0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002066 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002067 vcl_cfg->listen_queue_size);
2068 }
2069 else if (unformat (line_input, "listen-queue-size %ld",
2070 &vcl_cfg->listen_queue_size))
2071 {
2072 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002073 clib_warning ("VCL<%d>: configured listen_queue_size "
2074 "%ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002075 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002076 vcl_cfg->listen_queue_size);
2077 }
2078 else if (unformat (line_input, "app-timeout %f",
2079 &vcl_cfg->app_timeout))
2080 {
2081 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002082 clib_warning ("VCL<%d>: configured app_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002083 getpid (), vcl_cfg->app_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002084 }
2085 else if (unformat (line_input, "session-timeout %f",
2086 &vcl_cfg->session_timeout))
2087 {
2088 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002089 clib_warning ("VCL<%d>: configured session_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002090 getpid (), vcl_cfg->session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002091 }
2092 else if (unformat (line_input, "accept-timeout %f",
2093 &vcl_cfg->accept_timeout))
2094 {
2095 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002096 clib_warning ("VCL<%d>: configured accept_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002097 getpid (), vcl_cfg->accept_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002098 }
Dave Wallace774169b2017-11-01 20:07:40 -04002099 else if (unformat (line_input, "app-proxy-transport-tcp"))
Dave Wallace8af20542017-10-26 03:29:30 -04002100 {
Dave Wallace774169b2017-11-01 20:07:40 -04002101 vcl_cfg->app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002102 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002103 clib_warning ("VCL<%d>: configured "
2104 "app_proxy_transport_tcp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002105 getpid (), vcl_cfg->app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04002106 }
Dave Wallace774169b2017-11-01 20:07:40 -04002107 else if (unformat (line_input, "app-proxy-transport-udp"))
Dave Wallace8af20542017-10-26 03:29:30 -04002108 {
Dave Wallace774169b2017-11-01 20:07:40 -04002109 vcl_cfg->app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002110 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002111 clib_warning ("VCL<%d>: configured "
2112 "app_proxy_transport_udp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002113 getpid (), vcl_cfg->app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -04002114 }
Dave Wallace774169b2017-11-01 20:07:40 -04002115 else if (unformat (line_input, "app-scope-local"))
Dave Wallace8af20542017-10-26 03:29:30 -04002116 {
Dave Wallace774169b2017-11-01 20:07:40 -04002117 vcl_cfg->app_scope_local = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002118 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002119 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002120 getpid (), vcl_cfg->app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04002121 }
2122 else if (unformat (line_input, "app-scope-global"))
2123 {
2124 vcl_cfg->app_scope_global = 1;
2125 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002126 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002127 getpid (), vcl_cfg->app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04002128 }
2129 else if (unformat (line_input, "namespace-secret %lu",
2130 &vcl_cfg->namespace_secret))
2131 {
2132 if (VPPCOM_DEBUG > 0)
2133 clib_warning
Dave Wallace048b1d62018-01-03 22:24:41 -05002134 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002135 getpid (), vcl_cfg->namespace_secret,
Dave Wallace8af20542017-10-26 03:29:30 -04002136 vcl_cfg->namespace_secret);
2137 }
2138 else if (unformat (line_input, "namespace-id %v",
2139 &vcl_cfg->namespace_id))
2140 {
2141 vl_api_application_attach_t *mp;
2142 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2143 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2144 if (nsid_vec_len > max_nsid_vec_len)
2145 {
2146 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2147 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002148 clib_warning ("VCL<%d>: configured namespace_id is "
2149 "too long, truncated to %d characters!",
2150 getpid (), max_nsid_vec_len);
Dave Wallace8af20542017-10-26 03:29:30 -04002151 }
2152
2153 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002154 clib_warning ("VCL<%d>: configured namespace_id %v",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002155 getpid (), vcl_cfg->namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04002156 }
Dave Wallace543852a2017-08-03 02:11:34 -04002157 else if (unformat (line_input, "}"))
2158 {
2159 vc_cfg_input = 0;
2160 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002161 clib_warning ("VCL<%d>: completed parsing vppcom config!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002162 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002163 goto input_done;
2164 }
2165 else
2166 {
2167 if (line_input->buffer[line_input->index] != '#')
2168 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002169 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002170 getpid (), (char *)
Dave Wallace543852a2017-08-03 02:11:34 -04002171 &line_input->buffer[line_input->index]);
2172 }
2173 }
2174 }
2175 }
2176
2177input_done:
2178 unformat_free (input);
2179
2180file_done:
Chris Lukeab7b8d92017-09-07 07:40:13 -04002181 if (fd >= 0)
Dave Wallace543852a2017-08-03 02:11:34 -04002182 close (fd);
2183}
2184
2185/*
2186 * VPPCOM Public API functions
2187 */
2188int
2189vppcom_app_create (char *app_name)
2190{
Dave Wallace543852a2017-08-03 02:11:34 -04002191 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2192 u8 *heap;
2193 mheap_t *h;
2194 int rv;
2195
2196 if (!vcm->init)
2197 {
2198 char *conf_fname;
Dave Wallace8af20542017-10-26 03:29:30 -04002199 char *env_var_str;
Dave Wallace543852a2017-08-03 02:11:34 -04002200
2201 vcm->init = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04002202 vppcom_cfg_init (vcl_cfg);
Dave Wallace498b3a52017-11-09 13:00:34 -05002203 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2204 if (env_var_str)
2205 {
2206 u32 tmp;
2207 if (sscanf (env_var_str, "%u", &tmp) != 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002208 clib_warning ("VCL<%d>: Invalid debug level specified in "
Dave Wallace498b3a52017-11-09 13:00:34 -05002209 "the environment variable "
2210 VPPCOM_ENV_DEBUG
2211 " (%s)!\n", getpid (), env_var_str);
2212 else
2213 {
2214 vcm->debug = tmp;
Dave Wallace048b1d62018-01-03 22:24:41 -05002215 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
Dave Wallace498b3a52017-11-09 13:00:34 -05002216 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2217 }
2218 }
Dave Wallace8af20542017-10-26 03:29:30 -04002219 conf_fname = getenv (VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04002220 if (!conf_fname)
2221 {
2222 conf_fname = VPPCOM_CONF_DEFAULT;
2223 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002224 clib_warning ("VCL<%d>: getenv '%s' failed!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04002225 VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04002226 }
2227 vppcom_cfg_heapsize (conf_fname);
Dave Wallace2e005bb2017-11-07 01:21:39 -05002228 clib_fifo_validate (vcm->client_session_index_fifo,
2229 vcm->cfg.listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04002230 vppcom_cfg_read (conf_fname);
Dave Wallace8af20542017-10-26 03:29:30 -04002231 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2232 if (env_var_str)
2233 {
2234 u32 ns_id_vec_len = strlen (env_var_str);
2235
2236 vec_reset_length (vcm->cfg.namespace_id);
2237 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2238 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2239
2240 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002241 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002242 VPPCOM_ENV_APP_NAMESPACE_ID "!", getpid (),
Dave Wallace8af20542017-10-26 03:29:30 -04002243 vcm->cfg.namespace_id);
2244 }
2245 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2246 if (env_var_str)
2247 {
2248 u64 tmp;
2249 if (sscanf (env_var_str, "%lu", &tmp) != 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002250 clib_warning ("VCL<%d>: Invalid namespace secret specified in "
Dave Wallace8af20542017-10-26 03:29:30 -04002251 "the environment variable "
2252 VPPCOM_ENV_APP_NAMESPACE_SECRET
Dave Wallace2e005bb2017-11-07 01:21:39 -05002253 " (%s)!\n", getpid (), env_var_str);
Dave Wallace8af20542017-10-26 03:29:30 -04002254 else
2255 {
2256 vcm->cfg.namespace_secret = tmp;
2257 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002258 clib_warning ("VCL<%d>: configured namespace secret "
2259 "(%lu) from " VPPCOM_ENV_APP_NAMESPACE_ID "!",
2260 getpid (), vcm->cfg.namespace_secret);
Dave Wallace8af20542017-10-26 03:29:30 -04002261 }
2262 }
Dave Wallace774169b2017-11-01 20:07:40 -04002263 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
Dave Wallace8af20542017-10-26 03:29:30 -04002264 {
Dave Wallace774169b2017-11-01 20:07:40 -04002265 vcm->cfg.app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002266 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002267 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2268 "(%u) from " VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2269 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04002270 }
Dave Wallace774169b2017-11-01 20:07:40 -04002271 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
Dave Wallace8af20542017-10-26 03:29:30 -04002272 {
Dave Wallace774169b2017-11-01 20:07:40 -04002273 vcm->cfg.app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002274 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002275 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2276 "(%u) from " VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2277 "!", getpid (), vcm->cfg.app_proxy_transport_udp);
Dave Wallace774169b2017-11-01 20:07:40 -04002278 }
2279 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2280 {
2281 vcm->cfg.app_scope_local = 1;
2282 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002283 clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002284 VPPCOM_ENV_APP_SCOPE_LOCAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04002285 vcm->cfg.app_scope_local);
2286 }
2287 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2288 {
2289 vcm->cfg.app_scope_global = 1;
2290 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002291 clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002292 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04002293 vcm->cfg.app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04002294 }
2295
Dave Wallace543852a2017-08-03 02:11:34 -04002296 vcm->main_cpu = os_get_thread_index ();
2297 heap = clib_mem_get_per_cpu_heap ();
2298 h = mheap_header (heap);
2299
2300 /* make the main heap thread-safe */
2301 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2302
2303 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2304
2305 clib_time_init (&vcm->clib_time);
2306 vppcom_init_error_string_table ();
Florin Corasa332c462018-01-31 06:52:17 -08002307 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
2308 20 /* timeout in secs */ );
Dave Wallace543852a2017-08-03 02:11:34 -04002309 clib_spinlock_init (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04002310 }
2311
2312 if (vcm->my_client_index == ~0)
2313 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002314 vppcom_api_hookup ();
Dave Wallace543852a2017-08-03 02:11:34 -04002315 vcm->app_state = STATE_APP_START;
2316 rv = vppcom_connect_to_vpp (app_name);
2317 if (rv)
2318 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002319 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002320 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002321 return rv;
2322 }
2323
2324 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002325 clib_warning ("VCL<%d>: sending session enable", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002326
Dave Wallace048b1d62018-01-03 22:24:41 -05002327 rv = vppcom_app_session_enable ();
Dave Wallace543852a2017-08-03 02:11:34 -04002328 if (rv)
2329 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002330 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2331 "failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002332 return rv;
2333 }
Dave Wallace543852a2017-08-03 02:11:34 -04002334
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002335 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002336 clib_warning ("VCL<%d>: sending app attach", getpid ());
2337
2338 rv = vppcom_app_attach ();
2339 if (rv)
2340 {
2341 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2342 getpid ());
2343 return rv;
2344 }
2345
2346 if (VPPCOM_DEBUG > 0)
2347 clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002348 getpid (), app_name, vcm->my_client_index,
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002349 vcm->my_client_index);
2350 }
Dave Wallace543852a2017-08-03 02:11:34 -04002351
2352 return VPPCOM_OK;
2353}
2354
2355void
2356vppcom_app_destroy (void)
2357{
Dave Wallace543852a2017-08-03 02:11:34 -04002358 int rv;
2359
2360 if (vcm->my_client_index == ~0)
2361 return;
2362
2363 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002364 clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002365 getpid (), vcm->my_client_index, vcm->my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002366
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002367 if (VPPCOM_DEBUG > 0)
2368 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002369 /* *INDENT-OFF* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002370 ELOG_TYPE_DECLARE (e) =
2371 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002372 .format = "app_detach:C:%d",
2373 .format_args = "i4",
2374 };
2375
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002376 struct
2377 {
2378 u32 data;
2379 } *ed;
2380 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
2381 ed->data = vcm->my_client_index;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002382 /* *INDENT-ON* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002383 }
2384
Dave Wallace543852a2017-08-03 02:11:34 -04002385 vppcom_app_detach ();
2386 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2387 if (PREDICT_FALSE (rv))
2388 {
2389 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002390 clib_warning ("VCL<%d>: application detach timed out! "
2391 "returning %d (%s)",
Dave Wallaceee45d412017-11-24 21:44:06 -05002392 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -04002393 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08002394
2395 /* Finished with logging before client gets reset to ~0 */
2396 if (VPPCOM_DEBUG > 0)
2397 write_elog ();
2398
Dave Wallace543852a2017-08-03 02:11:34 -04002399 vl_client_disconnect_from_vlib ();
2400 vcm->my_client_index = ~0;
2401 vcm->app_state = STATE_APP_START;
2402}
2403
2404int
2405vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
2406{
Dave Wallace543852a2017-08-03 02:11:34 -04002407 session_t *session;
2408 u32 session_index;
2409
2410 clib_spinlock_lock (&vcm->sessions_lockp);
2411 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002412 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -04002413 session_index = session - vcm->sessions;
2414
2415 session->vrf = vrf;
2416 session->proto = proto;
2417 session->state = STATE_START;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002418 session->is_nonblocking = is_nonblocking ? 1 : 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002419 session->vpp_handle = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002420 clib_spinlock_unlock (&vcm->sessions_lockp);
2421
2422 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002423 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002424
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002425 if (VPPCOM_DEBUG > 0)
2426 {
2427 session->elog_track.name =
2428 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2429 session_index, 0);
2430 elog_track_register (&vcm->elog_main, &session->elog_track);
2431
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002432 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002433 ELOG_TYPE_DECLARE (e) =
2434 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002435 .format = "session_create:vrf:%d proto:%d state:%d is_nonblocking:%d",
2436 .format_args = "i4i4i4i4",
2437 };
2438
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002439 struct
2440 {
2441 u32 data[4];
2442 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002443
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002444 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2445 ed->data[0] = session->vrf;
2446 ed->data[1] = session->proto;
2447 ed->data[2] = session->state;
2448 ed->data[3] = session->is_nonblocking;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002449 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002450 }
2451
Dave Wallace543852a2017-08-03 02:11:34 -04002452 return (int) session_index;
2453}
2454
2455int
2456vppcom_session_close (uint32_t session_index)
2457{
Dave Wallace543852a2017-08-03 02:11:34 -04002458 session_t *session = 0;
2459 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002460 u8 is_listen;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002461 u8 is_vep;
2462 u8 is_vep_session;
2463 u32 next_sid;
2464 u32 vep_idx;
Dave Wallaceee45d412017-11-24 21:44:06 -05002465 u64 vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002466 uword *p;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002467 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04002468
Dave Wallace4878cbe2017-11-21 03:45:09 -05002469 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002470 is_listen = session->is_listen;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002471 is_vep = session->is_vep;
2472 is_vep_session = session->is_vep_session;
2473 next_sid = session->vep.next_sid;
2474 vep_idx = session->vep.vep_idx;
2475 state = session->state;
Dave Wallaceee45d412017-11-24 21:44:06 -05002476 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04002477 clib_spinlock_unlock (&vcm->sessions_lockp);
2478
2479 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -05002480 {
2481 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -05002482 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2483 "closing epoll session...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002484 getpid (), session_index, session_index);
2485 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002486 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2487 "closing session...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002488 getpid (), vpp_handle, session_index);
2489 }
Dave Wallace543852a2017-08-03 02:11:34 -04002490
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002491 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04002492 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002493 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04002494 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002495 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Dave Wallaceee45d412017-11-24 21:44:06 -05002496 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002497 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2498 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2499 getpid (), vpp_handle, next_sid, vep_idx,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002500 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002501
Dave Wallace4878cbe2017-11-21 03:45:09 -05002502 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002503 next_sid = session->vep.next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002504 clib_spinlock_unlock (&vcm->sessions_lockp);
2505 }
2506 }
2507 else
2508 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002509 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002510 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002511 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2512 if ((VPPCOM_DEBUG > 0) && (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002513 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2514 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
Dave Wallaceee45d412017-11-24 21:44:06 -05002515 getpid (), vpp_handle, session_index,
2516 vep_idx, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002517 }
2518
Dave Wallace4878cbe2017-11-21 03:45:09 -05002519 if (is_listen)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002520 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002521 if (state == STATE_LISTEN)
2522 {
2523 rv = vppcom_session_unbind (session_index);
2524 if (PREDICT_FALSE (rv < 0))
2525 {
2526 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002527 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002528 "listener unbind failed! rv %d (%s)",
2529 getpid (), vpp_handle, session_index,
2530 rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002531 }
2532 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002533 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002534
2535 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002536 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002537 rv = vppcom_session_disconnect (session_index);
2538 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002539 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002540 "session disconnect failed! rv %d (%s)",
2541 getpid (), vpp_handle, session_index,
2542 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002543 }
Dave Wallace19481612017-09-15 18:47:44 -04002544 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002545
2546 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002547 vpp_handle = session->vpp_handle;
2548 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002549 {
Dave Wallaceee45d412017-11-24 21:44:06 -05002550 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002551 if (p)
Dave Wallaceee45d412017-11-24 21:44:06 -05002552 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002553 }
Dave Wallace543852a2017-08-03 02:11:34 -04002554 pool_put_index (vcm->sessions, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002555 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002556
2557 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -05002558 {
2559 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -05002560 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -05002561 getpid (), session_index, session_index);
2562 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002563 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -05002564 getpid (), vpp_handle, session_index);
2565 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002566done:
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002567
2568 if (VPPCOM_DEBUG > 0)
2569 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002570 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002571 ELOG_TYPE_DECLARE (e) =
2572 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002573 .format = "session_close:rv:%d",
2574 .format_args = "i4",
2575 };
2576
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002577 struct
2578 {
2579 u32 data;
2580 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002581
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002582 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2583 ed->data = rv;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002584 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002585 }
2586
Dave Wallace543852a2017-08-03 02:11:34 -04002587 return rv;
2588}
2589
2590int
2591vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2592{
Dave Wallace543852a2017-08-03 02:11:34 -04002593 session_t *session = 0;
2594 int rv;
2595
2596 if (!ep || !ep->ip)
2597 return VPPCOM_EINVAL;
2598
Dave Wallace4878cbe2017-11-21 03:45:09 -05002599 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -04002600
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002601 if (session->is_vep)
2602 {
2603 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002604 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2605 "bind to an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002606 rv = VPPCOM_EBADFD;
2607 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002608 }
2609
Dave Wallace543852a2017-08-03 02:11:34 -04002610 session->vrf = ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04002611 session->lcl_addr.is_ip4 = ep->is_ip4;
2612 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002613 session->lcl_port = ep->port;
2614
2615 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002616 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
Dave Wallaceee45d412017-11-24 21:44:06 -05002617 "port %u, proto %s", getpid (), session_index,
2618 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2619 format_ip46_address, &session->lcl_addr.ip46,
2620 session->lcl_addr.is_ip4,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002621 clib_net_to_host_u16 (session->lcl_port),
2622 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002623
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002624 if (VPPCOM_DEBUG > 0)
2625 {
2626 if (session->lcl_addr.is_ip4)
2627 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002628 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002629 ELOG_TYPE_DECLARE (e) =
2630 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002631 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2632 .format_args = "t1i1i1i1i1i2",
2633 .n_enum_strings = 2,
2634 .enum_strings = {"TCP", "UDP",},
2635 };
2636
2637 CLIB_PACKED (struct {
2638 u8 proto;
2639 u8 addr[4];
2640 u16 port;
2641 }) * ed;
2642
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002643 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2644 ed->proto = session->proto;
2645 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2646 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2647 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2648 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2649 ed->port = clib_net_to_host_u16 (session->lcl_port);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002650 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002651 }
2652 }
2653
Dave Wallace543852a2017-08-03 02:11:34 -04002654 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002655done:
2656 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002657}
2658
2659int
Dave Wallace33e002b2017-09-06 01:20:02 -04002660vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04002661{
Dave Wallace33e002b2017-09-06 01:20:02 -04002662 session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05002663 u64 listen_vpp_handle;
2664 int rv, retval;
Dave Wallace543852a2017-08-03 02:11:34 -04002665
Dave Wallace4878cbe2017-11-21 03:45:09 -05002666 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002667
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002668 if (listen_session->is_vep)
2669 {
2670 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002671 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002672 "epoll session!", getpid (), listen_session_index);
2673 rv = VPPCOM_EBADFD;
2674 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002675 }
2676
Dave Wallaceee45d412017-11-24 21:44:06 -05002677 listen_vpp_handle = listen_session->vpp_handle;
Dave Wallacee695cb42017-11-02 22:04:42 -04002678 if (listen_session->is_listen)
2679 {
2680 clib_spinlock_unlock (&vcm->sessions_lockp);
2681 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002682 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002683 "already in listen state!",
2684 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002685 rv = VPPCOM_OK;
2686 goto done;
Dave Wallacee695cb42017-11-02 22:04:42 -04002687 }
2688
Dave Wallace543852a2017-08-03 02:11:34 -04002689 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002690 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2691 "sid %u: sending bind request...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002692 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002693
Dave Wallace4878cbe2017-11-21 03:45:09 -05002694 vppcom_send_bind_sock (listen_session, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002695 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallaceee45d412017-11-24 21:44:06 -05002696 retval =
Dave Wallace33e002b2017-09-06 01:20:02 -04002697 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2698 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002699
Dave Wallace4878cbe2017-11-21 03:45:09 -05002700 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002701 if (PREDICT_FALSE (retval))
2702 {
2703 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002704 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
Dave Wallaceee45d412017-11-24 21:44:06 -05002705 "returning %d (%s)", getpid (),
2706 listen_session->vpp_handle, listen_session_index,
2707 retval, vppcom_retval_str (retval));
2708 clib_spinlock_unlock (&vcm->sessions_lockp);
2709 rv = retval;
2710 goto done;
2711 }
2712
Dave Wallace543852a2017-08-03 02:11:34 -04002713 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002714 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002715done:
2716 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002717}
2718
2719int
2720vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05002721 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04002722{
Dave Wallace33e002b2017-09-06 01:20:02 -04002723 session_t *listen_session = 0;
2724 session_t *client_session = 0;
Florin Corasa332c462018-01-31 06:52:17 -08002725 u32 client_session_index = ~0, n_fifos;
Dave Wallace543852a2017-08-03 02:11:34 -04002726 int rv;
2727 f64 wait_for;
Dave Wallace60caa062017-11-10 17:07:13 -05002728 char *cut_thru_str;
Dave Wallaceee45d412017-11-24 21:44:06 -05002729 u64 listen_vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04002730
Dave Wallace4878cbe2017-11-21 03:45:09 -05002731 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002732
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002733 if (listen_session->is_vep)
2734 {
2735 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002736 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002737 "epoll session!", getpid (), listen_session_index);
2738 rv = VPPCOM_EBADFD;
2739 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002740 }
2741
Dave Wallaceee45d412017-11-24 21:44:06 -05002742 listen_vpp_handle = listen_session->vpp_handle;
Dave Wallace33e002b2017-09-06 01:20:02 -04002743 if (listen_session->state != STATE_LISTEN)
Dave Wallace543852a2017-08-03 02:11:34 -04002744 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002745 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002746 "not in listen state! state 0x%x (%s)", getpid (),
2747 listen_vpp_handle, listen_session_index,
2748 listen_session->state,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002749 vppcom_session_state_str (listen_session->state));
Dave Wallaceee45d412017-11-24 21:44:06 -05002750 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002751 rv = VPPCOM_EBADFD;
2752 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002753 }
Dave Wallace048b1d62018-01-03 22:24:41 -05002754 wait_for = (listen_session->is_nonblocking) ? 0 : vcm->cfg.accept_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -04002755
Dave Wallace543852a2017-08-03 02:11:34 -04002756 clib_spinlock_unlock (&vcm->sessions_lockp);
2757
2758 while (1)
2759 {
2760 rv = vppcom_wait_for_client_session_index (wait_for);
2761 if (rv)
2762 {
2763 if ((VPPCOM_DEBUG > 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002764 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2765 "accept failed! returning %d (%s)", getpid (),
Dave Wallaceee45d412017-11-24 21:44:06 -05002766 listen_vpp_handle, listen_session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002767 rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05002768 if (wait_for == 0)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002769 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002770 }
2771 else
2772 break;
2773 }
2774
Dave Wallace543852a2017-08-03 02:11:34 -04002775 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002776 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002777 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002778 if (PREDICT_FALSE (rv))
2779 {
2780 rv = VPPCOM_ECONNABORTED;
Dave Wallace048b1d62018-01-03 22:24:41 -05002781 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
Dave Wallaceee45d412017-11-24 21:44:06 -05002782 "lookup failed! returning %d (%s)", getpid (),
2783 listen_vpp_handle, listen_session_index,
2784 client_session_index, rv, vppcom_retval_str (rv));
2785 goto done;
2786 }
Dave Wallace543852a2017-08-03 02:11:34 -04002787
Dave Wallace227867f2017-11-13 21:21:53 -05002788 client_session->is_nonblocking = (flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace543852a2017-08-03 02:11:34 -04002789 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002790 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
Dave Wallaceee45d412017-11-24 21:44:06 -05002791 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2792 getpid (), listen_vpp_handle, listen_session_index,
2793 client_session->vpp_handle, client_session_index,
Dave Wallace227867f2017-11-13 21:21:53 -05002794 flags, client_session->is_nonblocking);
Dave Wallace543852a2017-08-03 02:11:34 -04002795
Dave Wallace048b1d62018-01-03 22:24:41 -05002796 if (ep)
2797 {
2798 ep->vrf = client_session->vrf;
2799 ep->is_cut_thru = client_session->is_cut_thru;
2800 ep->is_ip4 = client_session->peer_addr.is_ip4;
2801 ep->port = client_session->peer_port;
2802 if (client_session->peer_addr.is_ip4)
2803 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2804 sizeof (ip4_address_t));
2805 else
2806 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2807 sizeof (ip6_address_t));
2808 }
Dave Wallace60caa062017-11-10 17:07:13 -05002809
2810 if (client_session->is_server && client_session->is_cut_thru)
2811 {
2812 static svm_fifo_segment_create_args_t _a;
2813 svm_fifo_segment_create_args_t *a = &_a;
2814 svm_fifo_segment_private_t *seg;
2815
Dave Wallace4878cbe2017-11-21 03:45:09 -05002816 cut_thru_str = " cut-thru ";
Dave Wallace60caa062017-11-10 17:07:13 -05002817
2818 /* Create the segment */
2819 memset (a, 0, sizeof (*a));
2820 a->segment_name = (char *)
2821 format ((u8 *) a->segment_name, "%d:segment%d%c",
2822 getpid (), vcm->unique_segment_index++, 0);
2823 a->segment_size = vcm->cfg.segment_size;
Dave Wallace60caa062017-11-10 17:07:13 -05002824
2825 rv = svm_fifo_segment_create (a);
2826 if (PREDICT_FALSE (rv))
2827 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002828 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002829 "client sid %u svm_fifo_segment_create ('%s') "
2830 "failed! rv %d", getpid (), listen_vpp_handle,
2831 listen_session_index, client_session_index,
2832 a->segment_name, rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002833 vec_reset_length (a->new_segment_indices);
2834 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallaceee45d412017-11-24 21:44:06 -05002835 vppcom_send_connect_session_reply (client_session,
Dave Wallaced2931962017-11-25 04:17:39 -05002836 client_session_index,
2837 client_session->vpp_handle,
2838 client_session->client_context,
2839 rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002840 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002841 rv = VPPCOM_ENOMEM;
2842 goto done;
Dave Wallace60caa062017-11-10 17:07:13 -05002843 }
2844
2845 client_session->segment_name = vec_dup ((u8 *) a->segment_name);
2846 client_session->sm_seg_index = a->new_segment_indices[0];
2847 vec_free (a->new_segment_indices);
2848
2849 seg = svm_fifo_segment_get_segment (client_session->sm_seg_index);
Florin Corasa332c462018-01-31 06:52:17 -08002850 if (vcm->cfg.preallocated_fifo_pairs)
2851 {
2852 n_fifos = vcm->cfg.preallocated_fifo_pairs;
2853 svm_fifo_segment_preallocate_fifo_pairs (seg, vcm->cfg.rx_fifo_size,
2854 vcm->cfg.tx_fifo_size,
2855 &n_fifos);
2856 }
2857
Dave Wallace60caa062017-11-10 17:07:13 -05002858 client_session->server_rx_fifo =
2859 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
2860 FIFO_SEGMENT_RX_FREELIST);
2861 if (PREDICT_FALSE (!client_session->server_rx_fifo))
2862 {
2863 svm_fifo_segment_delete (seg);
Dave Wallace048b1d62018-01-03 22:24:41 -05002864 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002865 "client sid %u rx fifo alloc failed! "
2866 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2867 listen_session_index, client_session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002868 vcm->cfg.rx_fifo_size, vcm->cfg.rx_fifo_size);
Dave Wallace60caa062017-11-10 17:07:13 -05002869 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallaceee45d412017-11-24 21:44:06 -05002870 vppcom_send_connect_session_reply (client_session,
Dave Wallaced2931962017-11-25 04:17:39 -05002871 client_session_index,
2872 client_session->vpp_handle,
2873 client_session->client_context,
2874 rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002875 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002876 rv = VPPCOM_ENOMEM;
2877 goto done;
Dave Wallace60caa062017-11-10 17:07:13 -05002878 }
2879 client_session->server_rx_fifo->master_session_index =
2880 client_session_index;
2881
2882 client_session->server_tx_fifo =
2883 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
2884 FIFO_SEGMENT_TX_FREELIST);
2885 if (PREDICT_FALSE (!client_session->server_tx_fifo))
2886 {
2887 svm_fifo_segment_delete (seg);
Dave Wallace048b1d62018-01-03 22:24:41 -05002888 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002889 "client sid %u tx fifo alloc failed! "
2890 "size %ld (0x%lx)", getpid (), listen_vpp_handle,
2891 listen_session_index, client_session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002892 vcm->cfg.tx_fifo_size, vcm->cfg.tx_fifo_size);
Dave Wallace60caa062017-11-10 17:07:13 -05002893 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
Dave Wallaceee45d412017-11-24 21:44:06 -05002894 vppcom_send_connect_session_reply (client_session,
Dave Wallaced2931962017-11-25 04:17:39 -05002895 client_session_index,
2896 client_session->vpp_handle,
2897 client_session->client_context,
2898 rv);
Dave Wallace60caa062017-11-10 17:07:13 -05002899 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002900 rv = VPPCOM_ENOMEM;
2901 goto done;
Dave Wallace60caa062017-11-10 17:07:13 -05002902 }
2903 client_session->server_tx_fifo->master_session_index =
2904 client_session_index;
2905
2906 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002907 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
Dave Wallaceee45d412017-11-24 21:44:06 -05002908 "created segment '%s', rx_fifo %p, tx_fifo %p",
2909 getpid (), listen_vpp_handle, listen_session_index,
2910 client_session_index, client_session->segment_name,
Dave Wallace60caa062017-11-10 17:07:13 -05002911 client_session->server_rx_fifo,
2912 client_session->server_tx_fifo);
2913
2914#ifdef CUT_THRU_EVENT_QUEUE /* TBD */
2915 {
2916 void *oldheap;
2917 ssvm_shared_header_t *sh = seg->ssvm.sh;
2918
2919 ssvm_lock_non_recursive (sh, 1);
2920 oldheap = ssvm_push_heap (sh);
2921 event_q = client_session->vpp_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -08002922 svm_queue_init (vcm->cfg.event_queue_size,
2923 sizeof (session_fifo_event_t),
2924 getpid (), 0 /* signal not sent */ );
Dave Wallace60caa062017-11-10 17:07:13 -05002925 ssvm_pop_heap (oldheap);
2926 ssvm_unlock_non_recursive (sh);
2927 }
2928#endif
Dave Wallaceee45d412017-11-24 21:44:06 -05002929 vppcom_send_connect_session_reply (client_session,
2930 client_session_index,
Dave Wallaced2931962017-11-25 04:17:39 -05002931 client_session->vpp_handle,
2932 client_session->client_context,
Dave Wallaceee45d412017-11-24 21:44:06 -05002933 0 /* retval OK */ );
Dave Wallace60caa062017-11-10 17:07:13 -05002934 }
2935 else
2936 {
2937 cut_thru_str = " ";
Dave Wallaced2931962017-11-25 04:17:39 -05002938 vppcom_send_accept_session_reply (client_session->vpp_handle,
2939 client_session->client_context,
2940 0 /* retval OK */ );
Dave Wallace60caa062017-11-10 17:07:13 -05002941 }
2942
Stevenac1f96d2017-10-24 16:03:58 -07002943 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002944 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
Dave Wallaceee45d412017-11-24 21:44:06 -05002945 "0x%llx, sid %u%sconnection to local %s address "
2946 "%U port %u", getpid (), listen_vpp_handle,
2947 listen_session_index, client_session->vpp_handle,
2948 client_session_index, cut_thru_str,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002949 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2950 format_ip46_address, &client_session->lcl_addr.ip46,
2951 client_session->lcl_addr.is_ip4,
2952 clib_net_to_host_u16 (client_session->lcl_port));
Dave Wallace60caa062017-11-10 17:07:13 -05002953
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002954 if (VPPCOM_DEBUG > 0)
2955 {
2956 client_session->elog_track.name =
2957 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2958 client_session_index, 0);
2959 elog_track_register (&vcm->elog_main, &client_session->elog_track);
2960
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002961 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002962 ELOG_TYPE_DECLARE (e) =
2963 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002964 .format = "accept cut-thru: listen_handle:%x from_handle:%x",
2965 .format_args = "i8i8",
2966 };
2967
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002968 struct
2969 {
2970 u64 handle[2];
2971 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002972
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002973 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2974 ed->handle[0] = listen_vpp_handle;
2975 ed->handle[1] = client_session->vpp_handle;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002976 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002977
2978 if (client_session->lcl_addr.is_ip4)
2979 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002980 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002981 ELOG_TYPE_DECLARE (e2) =
2982 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002983 .format = "accept cut-thru: S:%d %d.%d.%d.%d:%d ",
2984 .format_args = "i4i1i1i1i1i2",
2985 };
2986
2987 CLIB_PACKED (struct {
2988 u32 session;
2989 u8 addr[4];
2990 u16 port;
2991 }) * ed2;
2992
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002993 ed2 =
2994 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2995 ed2->session = client_session_index;
2996 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2997 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2998 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2999 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
3000 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003001 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003002 }
3003 }
3004
Dave Wallace543852a2017-08-03 02:11:34 -04003005 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003006 rv = (int) client_session_index;
3007done:
3008 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003009}
3010
3011int
3012vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
3013{
Dave Wallace543852a2017-08-03 02:11:34 -04003014 session_t *session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05003015 int rv, retval = VPPCOM_OK;
3016 u64 vpp_handle = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04003017
Dave Wallace4878cbe2017-11-21 03:45:09 -05003018 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3019
3020 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04003021 {
3022 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05003023 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3024 "connect on an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003025 rv = VPPCOM_EBADFD;
3026 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003027 }
3028
Dave Wallaceee45d412017-11-24 21:44:06 -05003029 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003030 if (PREDICT_FALSE (session->is_server))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003031 {
3032 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05003033 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: is in use "
Dave Wallaceee45d412017-11-24 21:44:06 -05003034 "as a server session!", getpid (), vpp_handle,
3035 session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003036 rv = VPPCOM_EBADFD;
3037 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003038 }
3039
Dave Wallace4878cbe2017-11-21 03:45:09 -05003040 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04003041 {
Dave Wallace543852a2017-08-03 02:11:34 -04003042 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003043 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
Dave Wallaceee45d412017-11-24 21:44:06 -05003044 "connected to %s %U port %d proto %s, state 0x%x (%s)",
3045 getpid (), vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003046 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3047 format_ip46_address,
3048 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3049 clib_net_to_host_u16 (session->peer_port),
3050 session->proto ? "UDP" : "TCP", session->state,
3051 vppcom_session_state_str (session->state));
3052
3053 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003054 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003055 }
3056
3057 session->vrf = server_ep->vrf;
Dave Wallace35830af2017-10-09 01:43:42 -04003058 session->peer_addr.is_ip4 = server_ep->is_ip4;
3059 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07003060 session->peer_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04003061
3062 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003063 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
Dave Wallaceee45d412017-11-24 21:44:06 -05003064 "%s %U port %d proto %s",
3065 getpid (), vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003066 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3067 format_ip46_address,
3068 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3069 clib_net_to_host_u16 (session->peer_port),
3070 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04003071
3072 vppcom_send_connect_sock (session, session_index);
3073 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003074
Dave Wallaceee45d412017-11-24 21:44:06 -05003075 retval =
3076 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
3077 vcm->cfg.session_timeout);
3078
3079 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3080 vpp_handle = session->vpp_handle;
3081 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace7876d392017-10-19 03:53:57 -04003082
Dave Wallace4878cbe2017-11-21 03:45:09 -05003083done:
Dave Wallaceee45d412017-11-24 21:44:06 -05003084 if (PREDICT_FALSE (retval))
3085 {
3086 rv = retval;
3087 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003088 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
Dave Wallaceee45d412017-11-24 21:44:06 -05003089 "returning %d (%s)", getpid (), vpp_handle,
3090 session_index, rv, vppcom_retval_str (rv));
3091 }
3092 else if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003093 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
Dave Wallaceee45d412017-11-24 21:44:06 -05003094 getpid (), vpp_handle, session_index);
3095
Dave Wallace4878cbe2017-11-21 03:45:09 -05003096 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003097}
3098
Steven58f464e2017-10-25 12:33:12 -07003099static inline int
3100vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
3101 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04003102{
Dave Wallace543852a2017-08-03 02:11:34 -04003103 session_t *session = 0;
3104 svm_fifo_t *rx_fifo;
3105 int n_read = 0;
3106 int rv;
3107 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003108 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003109 session_state_t state;
3110 u8 is_server;
3111 u8 is_nonblocking;
Dave Wallaceee45d412017-11-24 21:44:06 -05003112 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04003113
3114 ASSERT (buf);
3115
Dave Wallace4878cbe2017-11-21 03:45:09 -05003116 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3117
3118 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04003119 {
3120 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05003121 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3122 "read from an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003123 rv = VPPCOM_EBADFD;
3124 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003125 }
3126
Dave Wallaceee45d412017-11-24 21:44:06 -05003127 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003128 is_server = session->is_server;
3129 is_nonblocking = session->is_nonblocking;
3130 state = session->state;
3131 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003132 {
3133 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003134 rv = ((state == STATE_DISCONNECT) ?
3135 VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3136
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003137 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003138 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
Dave Wallaceee45d412017-11-24 21:44:06 -05003139 "not open! state 0x%x (%s), returning %d (%s)",
3140 getpid (), vpp_handle, session_index,
3141 is_server ? "server" : "client", state,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003142 vppcom_session_state_str (state),
3143 rv, vppcom_retval_str (rv));
3144 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003145 }
3146
Dave Wallace4878cbe2017-11-21 03:45:09 -05003147 rx_fifo = ((!session->is_cut_thru || is_server) ?
Dave Wallace33e002b2017-09-06 01:20:02 -04003148 session->server_rx_fifo : session->server_tx_fifo);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003149 fifo_str = ((!session->is_cut_thru || is_server) ?
Dave Wallace33e002b2017-09-06 01:20:02 -04003150 "server_rx_fifo" : "server_tx_fifo");
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003151 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003152
3153 do
3154 {
Steven58f464e2017-10-25 12:33:12 -07003155 if (peek)
3156 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3157 else
3158 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003159 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003160 while (!is_nonblocking && (n_read <= 0));
Dave Wallacef7f809c2017-10-03 01:48:42 -04003161
Dave Wallace4878cbe2017-11-21 03:45:09 -05003162 if (n_read <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003163 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003164 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3165
3166 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3167 (EPOLLET | EPOLLIN));
3168 if (poll_et)
3169 session->vep.et_mask |= EPOLLIN;
3170
3171 if (state == STATE_CLOSE_ON_EMPTY)
3172 {
3173 session_state_t new_state = STATE_DISCONNECT;
3174 rv = VPPCOM_ECONNRESET;
3175
3176 if (VPPCOM_DEBUG > 1)
3177 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003178 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
Dave Wallaceee45d412017-11-24 21:44:06 -05003179 "with %s session state 0x%x (%s)!"
3180 " Setting state to 0x%x (%s), returning %d (%s)",
3181 getpid (), vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003182 is_server ? "server" : "client",
3183 state, vppcom_session_state_str (state),
3184 new_state, vppcom_session_state_str (new_state),
3185 rv, vppcom_retval_str (rv));
3186 }
3187
3188 session->state = new_state;
3189 }
3190 else
3191 rv = VPPCOM_EAGAIN;
3192
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003193 clib_spinlock_unlock (&vcm->sessions_lockp);
3194 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003195 else
3196 rv = n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04003197
Dave Wallace4878cbe2017-11-21 03:45:09 -05003198 if (VPPCOM_DEBUG > 2)
3199 {
3200 if (rv > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003201 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
Dave Wallaceee45d412017-11-24 21:44:06 -05003202 "from %s (%p)", getpid (), vpp_handle,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003203 session_index, n_read, fifo_str, rx_fifo);
3204 else
Dave Wallace048b1d62018-01-03 22:24:41 -05003205 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
Dave Wallaceee45d412017-11-24 21:44:06 -05003206 "returning %d (%s)", getpid (), vpp_handle,
3207 session_index, rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05003208 }
3209done:
3210 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003211}
3212
Steven58f464e2017-10-25 12:33:12 -07003213int
Dave Wallace048b1d62018-01-03 22:24:41 -05003214vppcom_session_read (uint32_t session_index, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07003215{
3216 return (vppcom_session_read_internal (session_index, buf, n, 0));
3217}
3218
3219static int
3220vppcom_session_peek (uint32_t session_index, void *buf, int n)
3221{
3222 return (vppcom_session_read_internal (session_index, buf, n, 1));
3223}
3224
Dave Wallace543852a2017-08-03 02:11:34 -04003225static inline int
3226vppcom_session_read_ready (session_t * session, u32 session_index)
3227{
Dave Wallace498b3a52017-11-09 13:00:34 -05003228 svm_fifo_t *rx_fifo = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04003229 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05003230 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003231 int rv;
3232 u8 is_server = session->is_server;
3233 session_state_t state = session->state;
Dave Wallaceee45d412017-11-24 21:44:06 -05003234 u64 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04003235
3236 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05003237 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003238 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003239 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003240 "epoll session!", getpid (), session_index);
3241 rv = VPPCOM_EBADFD;
3242 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003243 }
Dave Wallace33e002b2017-09-06 01:20:02 -04003244
3245 if (session->is_listen)
Dave Wallace543852a2017-08-03 02:11:34 -04003246 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3247 else
3248 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003249 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3250 {
3251 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3252 VPPCOM_ENOTCONN);
3253
3254 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003255 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
Dave Wallaceee45d412017-11-24 21:44:06 -05003256 "not open! state 0x%x (%s), returning %d (%s)",
3257 getpid (), vpp_handle, session_index,
3258 is_server ? "server" : "client",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003259 state, vppcom_session_state_str (state),
3260 rv, vppcom_retval_str (rv));
3261 goto done;
3262 }
3263
3264 rx_fifo = ((!session->is_cut_thru || is_server) ?
Dave Wallace33e002b2017-09-06 01:20:02 -04003265 session->server_rx_fifo : session->server_tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04003266
Dave Wallace33e002b2017-09-06 01:20:02 -04003267 ready = svm_fifo_max_dequeue (rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04003268 }
3269
Dave Wallace4878cbe2017-11-21 03:45:09 -05003270 if (ready == 0)
Dave Wallace60caa062017-11-10 17:07:13 -05003271 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003272 poll_et =
3273 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3274 if (poll_et)
3275 session->vep.et_mask |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05003276
Dave Wallace4878cbe2017-11-21 03:45:09 -05003277 if (state == STATE_CLOSE_ON_EMPTY)
3278 {
3279 rv = VPPCOM_ECONNRESET;
3280 session_state_t new_state = STATE_DISCONNECT;
3281
3282 if (VPPCOM_DEBUG > 1)
3283 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003284 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3285 "sid %u: Empty fifo with"
Dave Wallaceee45d412017-11-24 21:44:06 -05003286 " %s session state 0x%x (%s)! Setting state to "
3287 "0x%x (%s), returning %d (%s)",
3288 getpid (), session_index, vpp_handle,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003289 is_server ? "server" : "client",
3290 state, vppcom_session_state_str (state),
3291 new_state, vppcom_session_state_str (new_state),
3292 rv, vppcom_retval_str (rv));
3293 }
3294 session->state = new_state;
3295 goto done;
3296 }
3297 }
3298 rv = ready;
Dave Wallace16cb4082017-11-29 03:24:06 -05003299
3300 if (vcm->app_event_queue->cursize &&
3301 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3302 {
3303 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3304 session_fifo_event_t e;
3305
3306 for (i = 0; i < n_to_dequeue; i++)
Florin Corase86a8ed2018-01-05 03:20:25 -08003307 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
Dave Wallace16cb4082017-11-29 03:24:06 -05003308
3309 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3310 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003311done:
3312 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003313}
3314
3315int
Dave Wallace048b1d62018-01-03 22:24:41 -05003316vppcom_session_write (uint32_t session_index, void *buf, size_t n)
Dave Wallace543852a2017-08-03 02:11:34 -04003317{
Dave Wallace543852a2017-08-03 02:11:34 -04003318 session_t *session = 0;
3319 svm_fifo_t *tx_fifo;
Florin Corase86a8ed2018-01-05 03:20:25 -08003320 svm_queue_t *q;
Dave Wallace543852a2017-08-03 02:11:34 -04003321 session_fifo_event_t evt;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003322 int rv, n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04003323 char *fifo_str;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003324 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003325 u8 is_server;
3326 u8 is_nonblocking;
3327 session_state_t state;
Dave Wallaceee45d412017-11-24 21:44:06 -05003328 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04003329
3330 ASSERT (buf);
3331
Dave Wallace4878cbe2017-11-21 03:45:09 -05003332 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3333
3334 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04003335 {
3336 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05003337 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003338 "cannot write to an epoll session!",
3339 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003340
3341 rv = VPPCOM_EBADFD;
3342 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003343 }
3344
Dave Wallace4878cbe2017-11-21 03:45:09 -05003345 is_server = session->is_server;
3346 is_nonblocking = session->is_nonblocking;
Dave Wallaceee45d412017-11-24 21:44:06 -05003347 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003348 state = session->state;
3349 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003350 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003351 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3352 VPPCOM_ENOTCONN);
3353
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003354 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003355 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003356 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003357 "%s session is not open! state 0x%x (%s)",
3358 getpid (), vpp_handle, session_index,
3359 is_server ? "server" : "client", state,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003360 vppcom_session_state_str (state));
3361 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003362 }
3363
Dave Wallace4878cbe2017-11-21 03:45:09 -05003364 tx_fifo = ((!session->is_cut_thru || is_server) ?
Dave Wallace543852a2017-08-03 02:11:34 -04003365 session->server_tx_fifo : session->server_rx_fifo);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003366 fifo_str = ((!session->is_cut_thru || is_server) ?
Dave Wallace543852a2017-08-03 02:11:34 -04003367 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003368 clib_spinlock_unlock (&vcm->sessions_lockp);
3369
Dave Wallace543852a2017-08-03 02:11:34 -04003370 do
3371 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003372 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
Dave Wallace543852a2017-08-03 02:11:34 -04003373 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003374 while (!is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04003375
3376 /* If event wasn't set, add one */
Dave Wallacef7f809c2017-10-03 01:48:42 -04003377 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04003378 {
Dave Wallace543852a2017-08-03 02:11:34 -04003379 /* Fabricate TX event, send to vpp */
3380 evt.fifo = tx_fifo;
3381 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04003382
Dave Wallace4878cbe2017-11-21 03:45:09 -05003383 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3384 q = session->vpp_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -04003385 ASSERT (q);
Florin Corase86a8ed2018-01-05 03:20:25 -08003386 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
Dave Wallace4878cbe2017-11-21 03:45:09 -05003387 clib_spinlock_unlock (&vcm->sessions_lockp);
3388 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003389 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003390 "added FIFO_EVENT_APP_TX to "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003391 "vpp_event_q %p, n_write %d", getpid (),
Dave Wallaceee45d412017-11-24 21:44:06 -05003392 vpp_handle, session_index, q, n_write);
Dave Wallace543852a2017-08-03 02:11:34 -04003393 }
3394
Dave Wallace4878cbe2017-11-21 03:45:09 -05003395 if (n_write <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003396 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003397 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3398
3399 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3400 (EPOLLET | EPOLLOUT));
3401 if (poll_et)
3402 session->vep.et_mask |= EPOLLOUT;
3403
3404 if (state == STATE_CLOSE_ON_EMPTY)
3405 {
3406 session_state_t new_state = STATE_DISCONNECT;
3407 rv = VPPCOM_ECONNRESET;
3408
3409 if (VPPCOM_DEBUG > 1)
3410 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003411 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003412 "Empty fifo with %s session state 0x%x (%s)!"
3413 " Setting state to 0x%x (%s), returning %d (%s)",
3414 getpid (), vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003415 is_server ? "server" : "client",
3416 state, vppcom_session_state_str (state),
3417 new_state, vppcom_session_state_str (new_state),
3418 rv, vppcom_retval_str (rv));
3419 }
3420
3421 session->state = new_state;
3422 }
3423 else
3424 rv = VPPCOM_EAGAIN;
3425
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003426 clib_spinlock_unlock (&vcm->sessions_lockp);
3427 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003428 else
3429 rv = n_write;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003430
Dave Wallace543852a2017-08-03 02:11:34 -04003431 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003432 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003433 if (n_write <= 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003434 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003435 "FIFO-FULL %s (%p)", getpid (), vpp_handle,
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003436 session_index, fifo_str, tx_fifo);
3437 else
Dave Wallace048b1d62018-01-03 22:24:41 -05003438 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003439 "wrote %d bytes to %s (%p)", getpid (), vpp_handle,
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003440 session_index, n_write, fifo_str, tx_fifo);
3441 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003442done:
3443 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003444}
3445
3446static inline int
3447vppcom_session_write_ready (session_t * session, u32 session_index)
3448{
Dave Wallace543852a2017-08-03 02:11:34 -04003449 svm_fifo_t *tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04003450 char *fifo_str;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003451 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05003452 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003453 int rv;
3454 u8 is_server = session->is_server;
3455 session_state_t state = session->state;
Dave Wallace543852a2017-08-03 02:11:34 -04003456
3457 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05003458 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003459 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003460 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003461 "cannot write to an epoll session!",
3462 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003463 rv = VPPCOM_EBADFD;
3464 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003465 }
3466
Dave Wallace4878cbe2017-11-21 03:45:09 -05003467 if (PREDICT_FALSE (session->is_listen))
Dave Wallace33e002b2017-09-06 01:20:02 -04003468 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003469 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003470 "cannot write to a listen session!",
3471 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003472 rv = VPPCOM_EBADFD;
3473 goto done;
3474 }
3475
3476 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3477 {
3478 session_state_t state = session->state;
3479
3480 rv = ((state == STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3481 VPPCOM_ENOTCONN);
3482
Dave Wallace048b1d62018-01-03 22:24:41 -05003483 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003484 "%s session is not open! state 0x%x (%s), "
3485 "returning %d (%s)", getpid (), session->vpp_handle,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003486 session_index, is_server ? "server" : "client",
3487 state, vppcom_session_state_str (state),
3488 rv, vppcom_retval_str (rv));
3489 goto done;
Dave Wallace33e002b2017-09-06 01:20:02 -04003490 }
3491
Dave Wallace543852a2017-08-03 02:11:34 -04003492 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
3493 session->server_tx_fifo : session->server_rx_fifo);
Dave Wallace33e002b2017-09-06 01:20:02 -04003494 fifo_str = ((!session->is_cut_thru || session->is_server) ?
3495 "server_tx_fifo" : "server_rx_fifo");
Dave Wallace543852a2017-08-03 02:11:34 -04003496
Dave Wallacef7f809c2017-10-03 01:48:42 -04003497 ready = svm_fifo_max_enqueue (tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04003498
Dave Wallace33e002b2017-09-06 01:20:02 -04003499 if (VPPCOM_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003500 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003501 "peek %s (%p), ready = %d", getpid (),
3502 session->vpp_handle, session_index,
3503 fifo_str, tx_fifo, ready);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003504
Dave Wallace4878cbe2017-11-21 03:45:09 -05003505 if (ready == 0)
3506 {
3507 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3508 (EPOLLET | EPOLLOUT));
3509 if (poll_et)
3510 session->vep.et_mask |= EPOLLOUT;
3511
3512 if (state == STATE_CLOSE_ON_EMPTY)
3513 {
3514 rv = VPPCOM_ECONNRESET;
3515 session_state_t new_state = STATE_DISCONNECT;
3516
3517 if (VPPCOM_DEBUG > 1)
3518 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003519 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003520 "Empty fifo with %s session "
3521 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3522 "returning %d (%s)", getpid (),
3523 session->vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003524 is_server ? "server" : "client",
3525 state, vppcom_session_state_str (state),
3526 new_state, vppcom_session_state_str (new_state),
3527 rv, vppcom_retval_str (rv));
3528 }
3529 session->state = new_state;
3530 goto done;
3531 }
3532 }
3533 rv = ready;
3534done:
3535 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003536}
3537
3538int
3539vppcom_select (unsigned long n_bits, unsigned long *read_map,
3540 unsigned long *write_map, unsigned long *except_map,
3541 double time_to_wait)
3542{
Dave Wallace543852a2017-08-03 02:11:34 -04003543 u32 session_index;
3544 session_t *session = 0;
3545 int rv, bits_set = 0;
3546 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3547 u32 minbits = clib_max (n_bits, BITS (uword));
3548
3549 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3550
Dave Wallace7876d392017-10-19 03:53:57 -04003551 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003552 {
3553 clib_bitmap_validate (vcm->rd_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05003554 clib_memcpy (vcm->rd_bitmap, read_map,
3555 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3556 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04003557 }
Dave Wallace7876d392017-10-19 03:53:57 -04003558 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003559 {
3560 clib_bitmap_validate (vcm->wr_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05003561 clib_memcpy (vcm->wr_bitmap, write_map,
3562 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3563 memset (write_map, 0,
3564 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04003565 }
Dave Wallace7876d392017-10-19 03:53:57 -04003566 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003567 {
3568 clib_bitmap_validate (vcm->ex_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05003569 clib_memcpy (vcm->ex_bitmap, except_map,
3570 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3571 memset (except_map, 0,
3572 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04003573 }
3574
3575 do
3576 {
3577 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04003578 if (n_bits)
3579 {
3580 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003581 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003582 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3583 ({
3584 clib_spinlock_lock (&vcm->sessions_lockp);
3585 rv = vppcom_session_at_index (session_index, &session);
3586 if (rv < 0)
3587 {
3588 clib_spinlock_unlock (&vcm->sessions_lockp);
3589 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003590 clib_warning ("VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003591 "read_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003592 session_index);
3593 bits_set = VPPCOM_EBADFD;
3594 goto select_done;
3595 }
3596
3597 rv = vppcom_session_read_ready (session, session_index);
3598 clib_spinlock_unlock (&vcm->sessions_lockp);
3599 if (except_map && vcm->ex_bitmap &&
3600 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3601 (rv < 0))
3602 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003603 clib_bitmap_set_no_check (except_map, session_index, 1);
3604 bits_set++;
3605 }
3606 else if (rv > 0)
3607 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003608 clib_bitmap_set_no_check (read_map, session_index, 1);
3609 bits_set++;
3610 }
3611 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003612 }
3613
Dave Wallacee22aa742017-10-20 12:30:38 -04003614 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003615 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003616 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3617 ({
3618 clib_spinlock_lock (&vcm->sessions_lockp);
3619 rv = vppcom_session_at_index (session_index, &session);
3620 if (rv < 0)
3621 {
3622 clib_spinlock_unlock (&vcm->sessions_lockp);
3623 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003624 clib_warning ("VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003625 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003626 session_index);
3627 bits_set = VPPCOM_EBADFD;
3628 goto select_done;
3629 }
Dave Wallace543852a2017-08-03 02:11:34 -04003630
Dave Wallacee22aa742017-10-20 12:30:38 -04003631 rv = vppcom_session_write_ready (session, session_index);
3632 clib_spinlock_unlock (&vcm->sessions_lockp);
3633 if (write_map && (rv > 0))
3634 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003635 clib_bitmap_set_no_check (write_map, session_index, 1);
3636 bits_set++;
3637 }
3638 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003639 }
3640
Dave Wallacee22aa742017-10-20 12:30:38 -04003641 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003642 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003643 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3644 ({
3645 clib_spinlock_lock (&vcm->sessions_lockp);
3646 rv = vppcom_session_at_index (session_index, &session);
3647 if (rv < 0)
3648 {
3649 clib_spinlock_unlock (&vcm->sessions_lockp);
3650 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003651 clib_warning ("VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003652 "except_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003653 session_index);
3654 bits_set = VPPCOM_EBADFD;
3655 goto select_done;
3656 }
Dave Wallace543852a2017-08-03 02:11:34 -04003657
Dave Wallacee22aa742017-10-20 12:30:38 -04003658 rv = vppcom_session_read_ready (session, session_index);
3659 clib_spinlock_unlock (&vcm->sessions_lockp);
3660 if (rv < 0)
3661 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003662 clib_bitmap_set_no_check (except_map, session_index, 1);
3663 bits_set++;
3664 }
3665 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003666 }
Dave Wallacee22aa742017-10-20 12:30:38 -04003667 }
Dave Wallace543852a2017-08-03 02:11:34 -04003668 /* *INDENT-ON* */
3669 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003670 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
Dave Wallace543852a2017-08-03 02:11:34 -04003671
3672select_done:
3673 return (bits_set);
3674}
3675
Dave Wallacef7f809c2017-10-03 01:48:42 -04003676static inline void
3677vep_verify_epoll_chain (u32 vep_idx)
3678{
3679 session_t *session;
3680 vppcom_epoll_t *vep;
3681 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05003682 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003683
Dave Wallace498b3a52017-11-09 13:00:34 -05003684 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003685 return;
3686
3687 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3688 rv = vppcom_session_at_index (vep_idx, &session);
3689 if (PREDICT_FALSE (rv))
3690 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003691 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3692 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003693 goto done;
3694 }
3695 if (PREDICT_FALSE (!session->is_vep))
3696 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003697 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3698 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003699 goto done;
3700 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003701 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05003702 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05003703 "{\n"
3704 " is_vep = %u\n"
3705 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05003706 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05003707 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05003708 "}\n", getpid (), vep_idx,
3709 session->is_vep, session->is_vep_session,
3710 vep->next_sid, vep->next_sid,
Dave Wallace498b3a52017-11-09 13:00:34 -05003711 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003712
3713 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003714 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003715 rv = vppcom_session_at_index (sid, &session);
3716 if (PREDICT_FALSE (rv))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003717 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003718 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003719 goto done;
3720 }
3721 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05003722 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3723 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003724 else if (PREDICT_FALSE (!session->is_vep_session))
3725 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003726 clib_warning ("VCL<%d>: ERROR: session (%u) "
3727 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003728 goto done;
3729 }
3730 vep = &session->vep;
3731 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05003732 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003733 "vep_idx (%u)!", getpid (),
3734 sid, session->vep.vep_idx, vep_idx);
3735 if (session->is_vep_session)
3736 {
3737 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3738 "{\n"
3739 " next_sid = 0x%x (%u)\n"
3740 " prev_sid = 0x%x (%u)\n"
3741 " vep_idx = 0x%x (%u)\n"
3742 " ev.events = 0x%x\n"
3743 " ev.data.u64 = 0x%llx\n"
3744 " et_mask = 0x%x\n"
3745 "}\n",
3746 vep_idx, sid, sid,
3747 vep->next_sid, vep->next_sid,
3748 vep->prev_sid, vep->prev_sid,
3749 vep->vep_idx, vep->vep_idx,
3750 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003751 }
3752 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003753
3754done:
Dave Wallace048b1d62018-01-03 22:24:41 -05003755 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3756 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003757}
3758
3759int
3760vppcom_epoll_create (void)
3761{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003762 session_t *vep_session;
3763 u32 vep_idx;
3764
3765 clib_spinlock_lock (&vcm->sessions_lockp);
3766 pool_get (vcm->sessions, vep_session);
3767 memset (vep_session, 0, sizeof (*vep_session));
3768 vep_idx = vep_session - vcm->sessions;
3769
3770 vep_session->is_vep = 1;
3771 vep_session->vep.vep_idx = ~0;
3772 vep_session->vep.next_sid = ~0;
3773 vep_session->vep.prev_sid = ~0;
3774 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003775 vep_session->vpp_handle = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003776 clib_spinlock_unlock (&vcm->sessions_lockp);
3777
3778 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003779 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
Dave Wallaceee45d412017-11-24 21:44:06 -05003780 getpid (), vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003781
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003782 if (VPPCOM_DEBUG > 0)
3783 {
3784 vep_session->elog_track.name =
3785 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3786 vep_idx, 0);
3787 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3788
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003789 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003790 ELOG_TYPE_DECLARE (e) =
3791 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003792 .format = "created epoll session:%d",
3793 .format_args = "i4",
3794 };
3795
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003796 struct
3797 {
3798 u32 data;
3799 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003800
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003801 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_session->elog_track);
3802 ed->data = vep_idx;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003803 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003804 }
3805
Dave Wallacef7f809c2017-10-03 01:48:42 -04003806 return (vep_idx);
3807}
3808
3809int
3810vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3811 struct epoll_event *event)
3812{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003813 session_t *vep_session;
3814 session_t *session;
3815 int rv;
3816
3817 if (vep_idx == session_index)
3818 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003819 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003820 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003821 return VPPCOM_EINVAL;
3822 }
3823
3824 clib_spinlock_lock (&vcm->sessions_lockp);
3825 rv = vppcom_session_at_index (vep_idx, &vep_session);
3826 if (PREDICT_FALSE (rv))
3827 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003828 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003829 goto done;
3830 }
3831 if (PREDICT_FALSE (!vep_session->is_vep))
3832 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003833 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003834 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003835 rv = VPPCOM_EINVAL;
3836 goto done;
3837 }
3838
3839 ASSERT (vep_session->vep.vep_idx == ~0);
3840 ASSERT (vep_session->vep.prev_sid == ~0);
3841
3842 rv = vppcom_session_at_index (session_index, &session);
3843 if (PREDICT_FALSE (rv))
3844 {
3845 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003846 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003847 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003848 goto done;
3849 }
3850 if (PREDICT_FALSE (session->is_vep))
3851 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003852 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003853 rv = VPPCOM_EINVAL;
3854 goto done;
3855 }
3856
3857 switch (op)
3858 {
3859 case EPOLL_CTL_ADD:
3860 if (PREDICT_FALSE (!event))
3861 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003862 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003863 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003864 rv = VPPCOM_EINVAL;
3865 goto done;
3866 }
3867 if (vep_session->vep.next_sid != ~0)
3868 {
3869 session_t *next_session;
3870 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3871 &next_session);
3872 if (PREDICT_FALSE (rv))
3873 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003874 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003875 "vep.next_sid (%u) on vep_idx (%u)!",
3876 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003877 goto done;
3878 }
3879 ASSERT (next_session->vep.prev_sid == vep_idx);
3880 next_session->vep.prev_sid = session_index;
3881 }
3882 session->vep.next_sid = vep_session->vep.next_sid;
3883 session->vep.prev_sid = vep_idx;
3884 session->vep.vep_idx = vep_idx;
3885 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3886 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003887 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003888 session->is_vep_session = 1;
3889 vep_session->vep.next_sid = session_index;
3890 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003891 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3892 "sid %u, events 0x%x, data 0x%llx!",
3893 getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003894 event->events, event->data.u64);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08003895 if (VPPCOM_DEBUG > 0)
3896 {
3897 /* *INDENT-OFF* */
3898 ELOG_TYPE_DECLARE (e) =
3899 {
3900 .format = "epoll_ctladd: events:%x data:%x",
3901 .format_args = "i4i4i8",
3902 };
3903 struct
3904 {
3905 u32 events;
3906 u64 event_data;
3907 } *ed;
3908
3909 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3910
3911 ed->events = event->events;
3912 ed->event_data = event->data.u64;
3913 /* *INDENT-ON* */
3914 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003915 break;
3916
3917 case EPOLL_CTL_MOD:
3918 if (PREDICT_FALSE (!event))
3919 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003920 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003921 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003922 rv = VPPCOM_EINVAL;
3923 goto done;
3924 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003925 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003926 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003927 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003928 "not a vep session!", getpid (), session_index);
3929 rv = VPPCOM_EINVAL;
3930 goto done;
3931 }
3932 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3933 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003934 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003935 "vep_idx (%u) != vep_idx (%u)!",
3936 getpid (), session_index,
3937 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003938 rv = VPPCOM_EINVAL;
3939 goto done;
3940 }
3941 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3942 session->vep.ev = *event;
3943 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003944 clib_warning
3945 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3946 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3947 event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003948 break;
3949
3950 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05003951 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003952 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003953 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003954 "not a vep session!", getpid (), session_index);
3955 rv = VPPCOM_EINVAL;
3956 goto done;
3957 }
3958 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3959 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003960 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003961 "vep_idx (%u) != vep_idx (%u)!",
3962 getpid (), session_index,
3963 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003964 rv = VPPCOM_EINVAL;
3965 goto done;
3966 }
3967
3968 vep_session->wait_cont_idx =
3969 (vep_session->wait_cont_idx == session_index) ?
3970 session->vep.next_sid : vep_session->wait_cont_idx;
3971
3972 if (session->vep.prev_sid == vep_idx)
3973 vep_session->vep.next_sid = session->vep.next_sid;
3974 else
3975 {
3976 session_t *prev_session;
3977 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3978 if (PREDICT_FALSE (rv))
3979 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003980 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003981 "vep.prev_sid (%u) on sid (%u)!",
3982 getpid (), session->vep.prev_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003983 goto done;
3984 }
3985 ASSERT (prev_session->vep.next_sid == session_index);
3986 prev_session->vep.next_sid = session->vep.next_sid;
3987 }
3988 if (session->vep.next_sid != ~0)
3989 {
3990 session_t *next_session;
3991 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3992 if (PREDICT_FALSE (rv))
3993 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003994 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003995 "vep.next_sid (%u) on sid (%u)!",
3996 getpid (), session->vep.next_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003997 goto done;
3998 }
3999 ASSERT (next_session->vep.prev_sid == session_index);
4000 next_session->vep.prev_sid = session->vep.prev_sid;
4001 }
4002
4003 memset (&session->vep, 0, sizeof (session->vep));
4004 session->vep.next_sid = ~0;
4005 session->vep.prev_sid = ~0;
4006 session->vep.vep_idx = ~0;
4007 session->is_vep_session = 0;
4008 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05004009 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004010 getpid (), vep_idx, session_index);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004011 if (VPPCOM_DEBUG > 0)
4012 {
4013 /* *INDENT-OFF* */
4014 ELOG_TYPE_DECLARE (e) =
4015 {
4016 .format = "epoll_ctldel: vep:%d",
4017 .format_args = "i4",
4018 };
4019 struct
4020 {
4021 u32 data;
4022 } *ed;
4023
4024 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4025
4026 ed->data = vep_idx;
4027 /* *INDENT-ON* */
4028 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04004029 break;
4030
4031 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05004032 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004033 rv = VPPCOM_EINVAL;
4034 }
4035
4036 vep_verify_epoll_chain (vep_idx);
4037
4038done:
4039 clib_spinlock_unlock (&vcm->sessions_lockp);
4040 return rv;
4041}
4042
Dave Wallacef7f809c2017-10-03 01:48:42 -04004043int
4044vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
4045 int maxevents, double wait_for_time)
4046{
Dave Wallacef7f809c2017-10-03 01:48:42 -04004047 session_t *vep_session;
4048 int rv;
4049 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05004050 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04004051 int num_ev = 0;
4052 u32 vep_next_sid, wait_cont_idx;
4053 u8 is_vep;
4054
4055 if (PREDICT_FALSE (maxevents <= 0))
4056 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004057 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05004058 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004059 return VPPCOM_EINVAL;
4060 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04004061 memset (events, 0, sizeof (*events) * maxevents);
4062
4063 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4064 vep_next_sid = vep_session->vep.next_sid;
4065 is_vep = vep_session->is_vep;
4066 wait_cont_idx = vep_session->wait_cont_idx;
4067 clib_spinlock_unlock (&vcm->sessions_lockp);
4068
4069 if (PREDICT_FALSE (!is_vep))
4070 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004071 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05004072 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004073 rv = VPPCOM_EINVAL;
4074 goto done;
4075 }
Dave Wallacee695cb42017-11-02 22:04:42 -04004076 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004077 {
Dave Wallacee695cb42017-11-02 22:04:42 -04004078 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05004079 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004080 getpid (), vep_idx);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004081 if (VPPCOM_DEBUG > 0)
4082 {
4083 /* *INDENT-OFF* */
4084 ELOG_TYPE_DECLARE (e) =
4085 {
4086 .format = "WRN: vep_idx:%d empty",
4087 .format_args = "i4",
4088 };
4089 struct
4090 {
4091 u32 data;
4092 } *ed;
4093
4094 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_session->elog_track);
4095
4096 ed->data = vep_idx;
4097 /* *INDENT-ON* */
4098 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04004099 goto done;
4100 }
4101
4102 do
4103 {
4104 u32 sid;
4105 u32 next_sid = ~0;
4106 session_t *session;
4107
4108 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
4109 sid != ~0; sid = next_sid)
4110 {
4111 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
4112 u8 add_event, is_vep_session;
4113 int ready;
4114 u64 session_ev_data;
4115
4116 VCL_LOCK_AND_GET_SESSION (sid, &session);
4117 next_sid = session->vep.next_sid;
4118 session_events = session->vep.ev.events;
4119 et_mask = session->vep.et_mask;
4120 is_vep = session->is_vep;
4121 is_vep_session = session->is_vep_session;
4122 session_vep_idx = session->vep.vep_idx;
4123 session_ev_data = session->vep.ev.data.u64;
4124 clib_spinlock_unlock (&vcm->sessions_lockp);
4125
4126 if (PREDICT_FALSE (is_vep))
4127 {
4128 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05004129 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004130 getpid (), vep_idx);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004131 if (VPPCOM_DEBUG > 0)
4132 {
4133 /* *INDENT-OFF* */
4134 ELOG_TYPE_DECLARE (e) =
4135 {
4136 .format = "ERR:vep_idx:%d is vep",
4137 .format_args = "i4",
4138 };
4139 struct
4140 {
4141 u32 data;
4142 } *ed;
4143
4144 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4145
4146 ed->data = vep_idx;
4147 /* *INDENT-ON* */
4148 }
4149
Dave Wallacef7f809c2017-10-03 01:48:42 -04004150 rv = VPPCOM_EINVAL;
4151 goto done;
4152 }
4153 if (PREDICT_FALSE (!is_vep_session))
4154 {
4155 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05004156 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
Dave Wallace2e005bb2017-11-07 01:21:39 -05004157 "a vep session!", getpid (), sid);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004158 if (VPPCOM_DEBUG > 0)
4159 {
4160 /* *INDENT-OFF* */
4161 ELOG_TYPE_DECLARE (e) =
4162 {
4163 .format = "ERR:SID:%d not vep",
4164 .format_args = "i4",
4165 };
4166 struct
4167 {
4168 u32 data;
4169 } *ed;
4170
4171 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4172
4173 ed->data = sid;
4174 /* *INDENT-ON* */
4175 }
4176
Dave Wallacef7f809c2017-10-03 01:48:42 -04004177 rv = VPPCOM_EINVAL;
4178 goto done;
4179 }
4180 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4181 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004182 clib_warning ("VCL<%d>: ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04004183 "vep_idx (%u) != vep_idx (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004184 getpid (), sid, session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004185 rv = VPPCOM_EINVAL;
4186 goto done;
4187 }
4188
4189 add_event = clear_et_mask = 0;
4190
Dave Wallace60caa062017-11-10 17:07:13 -05004191 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04004192 {
4193 VCL_LOCK_AND_GET_SESSION (sid, &session);
4194 ready = vppcom_session_read_ready (session, sid);
4195 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05004196 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004197 {
4198 add_event = 1;
4199 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05004200 if (((EPOLLET | EPOLLIN) & session_events) ==
4201 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004202 clear_et_mask |= EPOLLIN;
4203 }
4204 else if (ready < 0)
4205 {
4206 add_event = 1;
4207 switch (ready)
4208 {
4209 case VPPCOM_ECONNRESET:
4210 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4211 break;
4212
4213 default:
4214 events[num_ev].events |= EPOLLERR;
4215 break;
4216 }
4217 }
4218 }
4219
Dave Wallace60caa062017-11-10 17:07:13 -05004220 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04004221 {
4222 VCL_LOCK_AND_GET_SESSION (sid, &session);
4223 ready = vppcom_session_write_ready (session, sid);
4224 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05004225 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004226 {
4227 add_event = 1;
4228 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05004229 if (((EPOLLET | EPOLLOUT) & session_events) ==
4230 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004231 clear_et_mask |= EPOLLOUT;
4232 }
4233 else if (ready < 0)
4234 {
4235 add_event = 1;
4236 switch (ready)
4237 {
4238 case VPPCOM_ECONNRESET:
4239 events[num_ev].events |= EPOLLHUP;
4240 break;
4241
4242 default:
4243 events[num_ev].events |= EPOLLERR;
4244 break;
4245 }
4246 }
4247 }
4248
4249 if (add_event)
4250 {
4251 events[num_ev].data.u64 = session_ev_data;
4252 if (EPOLLONESHOT & session_events)
4253 {
4254 VCL_LOCK_AND_GET_SESSION (sid, &session);
4255 session->vep.ev.events = 0;
4256 clib_spinlock_unlock (&vcm->sessions_lockp);
4257 }
4258 num_ev++;
4259 if (num_ev == maxevents)
4260 {
4261 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4262 vep_session->wait_cont_idx = next_sid;
4263 clib_spinlock_unlock (&vcm->sessions_lockp);
4264 goto done;
4265 }
4266 }
4267 if (wait_cont_idx != ~0)
4268 {
4269 if (next_sid == ~0)
4270 next_sid = vep_next_sid;
4271 else if (next_sid == wait_cont_idx)
4272 next_sid = ~0;
4273 }
4274 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05004275 if (wait_for_time != -1)
4276 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04004277 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05004278 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004279
4280 if (wait_cont_idx != ~0)
4281 {
4282 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4283 vep_session->wait_cont_idx = ~0;
4284 clib_spinlock_unlock (&vcm->sessions_lockp);
4285 }
4286done:
4287 return (rv != VPPCOM_OK) ? rv : num_ev;
4288}
4289
Dave Wallace35830af2017-10-09 01:43:42 -04004290int
4291vppcom_session_attr (uint32_t session_index, uint32_t op,
4292 void *buffer, uint32_t * buflen)
4293{
Dave Wallace35830af2017-10-09 01:43:42 -04004294 session_t *session;
4295 int rv = VPPCOM_OK;
4296 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07004297 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04004298
4299 VCL_LOCK_AND_GET_SESSION (session_index, &session);
4300 switch (op)
4301 {
4302 case VPPCOM_ATTR_GET_NREAD:
4303 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -05004304 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004305 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004306 getpid (), rv);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004307 if (VPPCOM_DEBUG > 0)
4308 {
4309 /* *INDENT-OFF* */
4310 ELOG_TYPE_DECLARE (e) =
4311 {
4312 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4313 .format_args = "i4",
4314 };
4315 struct
4316 {
4317 u32 data;
4318 } *ed;
4319
4320 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4321
4322 ed->data = rv;
4323 /* *INDENT-ON* */
4324 }
4325
Dave Wallace35830af2017-10-09 01:43:42 -04004326 break;
4327
Dave Wallace227867f2017-11-13 21:21:53 -05004328 case VPPCOM_ATTR_GET_NWRITE:
4329 rv = vppcom_session_write_ready (session, session_index);
4330 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004331 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
Dave Wallace227867f2017-11-13 21:21:53 -05004332 getpid (), session_index, rv);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004333 if (VPPCOM_DEBUG > 0)
4334 {
4335 /* *INDENT-OFF* */
4336 ELOG_TYPE_DECLARE (e) =
4337 {
4338 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4339 .format_args = "i4",
4340 };
4341 struct
4342 {
4343 u32 data;
4344 } *ed;
4345
4346 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4347
4348 ed->data = rv;
4349 /* *INDENT-ON* */
4350 }
Dave Wallace35830af2017-10-09 01:43:42 -04004351 break;
4352
4353 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05004354 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04004355 {
4356 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
4357 *buflen = sizeof (*flags);
Dave Wallace227867f2017-11-13 21:21:53 -05004358 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004359 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
Dave Wallace227867f2017-11-13 21:21:53 -05004360 "flags = 0x%08x, is_nonblocking = %u", getpid (),
4361 session_index, *flags, session->is_nonblocking);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004362 if (VPPCOM_DEBUG > 0)
4363 {
4364 /* *INDENT-OFF* */
4365 ELOG_TYPE_DECLARE (e) =
4366 {
4367 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004368 .format_args = "i4i4",
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004369 };
4370 struct
4371 {
4372 u32 flags;
4373 u32 is_nonblk;
4374 } *ed;
4375
4376 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4377
4378 ed->flags = *flags;
4379 ed->is_nonblk = session->is_nonblocking;
4380 /* *INDENT-ON* */
4381 }
4382
Dave Wallace35830af2017-10-09 01:43:42 -04004383 }
4384 else
4385 rv = VPPCOM_EINVAL;
4386 break;
4387
4388 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05004389 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04004390 {
4391 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
Dave Wallace227867f2017-11-13 21:21:53 -05004392 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004393 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
Dave Wallace227867f2017-11-13 21:21:53 -05004394 "flags = 0x%08x, is_nonblocking = %u",
Dave Wallace4878cbe2017-11-21 03:45:09 -05004395 getpid (), session_index, *flags,
4396 session->is_nonblocking);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004397 if (VPPCOM_DEBUG > 0)
4398 {
4399 /* *INDENT-OFF* */
4400 ELOG_TYPE_DECLARE (e) =
4401 {
4402 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004403 .format_args = "i4i4",
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004404 };
4405 struct
4406 {
4407 u32 flags;
4408 u32 is_nonblk;
4409 } *ed;
4410
4411 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4412
4413 ed->flags = *flags;
4414 ed->is_nonblk = session->is_nonblocking;
4415 /* *INDENT-ON* */
4416 }
Dave Wallace35830af2017-10-09 01:43:42 -04004417 }
4418 else
4419 rv = VPPCOM_EINVAL;
4420 break;
4421
4422 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05004423 if (PREDICT_TRUE (buffer && buflen &&
4424 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04004425 {
Steven2199aab2017-10-15 20:18:47 -07004426 ep->vrf = session->vrf;
4427 ep->is_ip4 = session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07004428 ep->port = session->peer_port;
Steven2199aab2017-10-15 20:18:47 -07004429 if (session->peer_addr.is_ip4)
4430 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4431 sizeof (ip4_address_t));
4432 else
4433 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4434 sizeof (ip6_address_t));
4435 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04004436 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05004437 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4438 "is_ip4 = %u, addr = %U, port %u", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04004439 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07004440 &session->peer_addr.ip46, ep->is_ip4,
4441 clib_net_to_host_u16 (ep->port));
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004442 if (VPPCOM_DEBUG > 0)
4443 {
4444 if (ep->is_ip4)
4445 {
4446 /* *INDENT-OFF* */
4447 ELOG_TYPE_DECLARE (e) =
4448 {
4449 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4450 .format_args = "i1i1i1i1i2",
4451 };
4452 CLIB_PACKED (struct {
4453 u8 addr[4]; //4
4454 u16 port; //2
4455 }) * ed;
4456
4457 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4458
4459 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4460 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4461 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4462 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4463 ed->port = clib_net_to_host_u16 (session->peer_port);
4464 /* *INDENT-ON* */
4465 }
4466 else
4467 {
4468 /* *INDENT-OFF* */
4469 ELOG_TYPE_DECLARE (e) =
4470 {
4471 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4472 .format_args = "i2",
4473 };
4474 CLIB_PACKED (struct {
4475 u16 port; //2
4476 }) * ed;
4477
4478 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4479
4480 ed->port = clib_net_to_host_u16 (session->peer_port);
4481 /* *INDENT-ON* */
4482 }
4483 }
Dave Wallace35830af2017-10-09 01:43:42 -04004484 }
4485 else
4486 rv = VPPCOM_EINVAL;
4487 break;
4488
4489 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05004490 if (PREDICT_TRUE (buffer && buflen &&
4491 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04004492 {
Steven2199aab2017-10-15 20:18:47 -07004493 ep->vrf = session->vrf;
4494 ep->is_ip4 = session->lcl_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07004495 ep->port = session->lcl_port;
Steven2199aab2017-10-15 20:18:47 -07004496 if (session->lcl_addr.is_ip4)
4497 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4498 sizeof (ip4_address_t));
4499 else
4500 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4501 sizeof (ip6_address_t));
4502 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04004503 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05004504 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4505 "is_ip4 = %u, addr = %U port %d", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04004506 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07004507 &session->lcl_addr.ip46, ep->is_ip4,
4508 clib_net_to_host_u16 (ep->port));
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004509 if (VPPCOM_DEBUG > 0)
4510 {
4511 if (ep->is_ip4)
4512 {
4513 /* *INDENT-OFF* */
4514 ELOG_TYPE_DECLARE (e) =
4515 {
4516 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4517 .format_args = "i1i1i1i1i2",
4518 };
4519 CLIB_PACKED (struct {
4520 u8 addr[4]; //4
4521 u16 port; //2
4522 }) * ed;
4523
4524 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4525
4526 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4527 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4528 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4529 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4530 ed->port = clib_net_to_host_u16 (session->peer_port);
4531 /* *INDENT-ON* */
4532 }
4533 else
4534 {
4535 /* *INDENT-OFF* */
4536 ELOG_TYPE_DECLARE (e) =
4537 {
4538 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4539 .format_args = "i2",
4540 };
4541 CLIB_PACKED (struct {
4542 u16 port; //2
4543 }) * ed;
4544
4545 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4546
4547 ed->port = clib_net_to_host_u16 (session->peer_port);
4548 /* *INDENT-ON* */
4549 }
4550 }
Dave Wallace35830af2017-10-09 01:43:42 -04004551 }
4552 else
4553 rv = VPPCOM_EINVAL;
4554 break;
Stevenb5a11602017-10-11 09:59:30 -07004555
Dave Wallace048b1d62018-01-03 22:24:41 -05004556 case VPPCOM_ATTR_GET_LIBC_EPFD:
4557 rv = session->libc_epfd;
4558 if (VPPCOM_DEBUG > 2)
4559 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4560 getpid (), rv);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004561 if (VPPCOM_DEBUG > 0)
4562 {
4563 /* *INDENT-OFF* */
4564 ELOG_TYPE_DECLARE (e) =
4565 {
Florin Corasbb16d3f2018-01-29 08:55:25 -08004566 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4567 .format_args = "i4",
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004568 };
4569 CLIB_PACKED (struct {
Florin Corasbb16d3f2018-01-29 08:55:25 -08004570 i32 data;
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004571 }) * ed;
4572
4573 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
Florin Corasbb16d3f2018-01-29 08:55:25 -08004574 ed->data = session->libc_epfd;
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004575 /* *INDENT-ON* */
4576 }
4577
Dave Wallace048b1d62018-01-03 22:24:41 -05004578 break;
4579
4580 case VPPCOM_ATTR_SET_LIBC_EPFD:
4581 if (PREDICT_TRUE (buffer && buflen &&
4582 (*buflen == sizeof (session->libc_epfd))))
4583 {
4584 session->libc_epfd = *(int *) buffer;
4585 *buflen = sizeof (session->libc_epfd);
4586
4587 if (VPPCOM_DEBUG > 2)
4588 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4589 "buflen %d", getpid (), session->libc_epfd,
4590 *buflen);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004591 if (VPPCOM_DEBUG > 0)
4592 {
4593 /* *INDENT-OFF* */
4594 ELOG_TYPE_DECLARE (e) =
4595 {
4596 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4597 .format_args = "t1i4i4",
4598 .n_enum_strings = 2,
4599 .enum_strings = {"", "-",},
4600 };
4601 CLIB_PACKED (struct {
4602 u8 sign;
4603 u32 data[2];
4604 }) * ed;
4605
4606 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4607
4608 ed->sign = (session->libc_epfd < 0);
4609 ed->data[0] = abs(session->libc_epfd);
4610 ed->data[1] = *buflen;
4611 /* *INDENT-ON* */
4612 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004613 }
4614 else
4615 rv = VPPCOM_EINVAL;
4616 break;
4617
4618 case VPPCOM_ATTR_GET_PROTOCOL:
4619 if (buffer && buflen && (*buflen >= sizeof (int)))
4620 {
4621 *(int *) buffer = session->proto;
4622 *buflen = sizeof (int);
4623
4624 if (VPPCOM_DEBUG > 2)
4625 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4626 "buflen %d", getpid (), *(int *) buffer,
4627 *(int *) buffer ? "UDP" : "TCP", *buflen);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004628 if (VPPCOM_DEBUG > 0)
4629 {
4630 /* *INDENT-OFF* */
4631 ELOG_TYPE_DECLARE (e) =
4632 {
4633 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4634 .format_args = "t1i4",
4635 .n_enum_strings = 2,
4636 .enum_strings = {"TCP", "UDP",},
4637 };
4638
4639 CLIB_PACKED (struct {
4640 u8 proto;
4641 u32 buflen;
4642 }) * ed;
4643
4644 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4645 ed->proto = session->proto;
4646 ed->buflen = *(int *) buffer;
4647 /* *INDENT-ON* */
4648 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004649 }
4650 else
4651 rv = VPPCOM_EINVAL;
4652 break;
4653
4654 case VPPCOM_ATTR_GET_LISTEN:
4655 if (buffer && buflen && (*buflen >= sizeof (int)))
4656 {
4657 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4658 VCL_SESS_ATTR_LISTEN);
4659 *buflen = sizeof (int);
4660
4661 if (VPPCOM_DEBUG > 2)
4662 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4663 "buflen %d", getpid (), *(int *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004664 if (VPPCOM_DEBUG > 0)
4665 {
4666 /* *INDENT-OFF* */
4667 ELOG_TYPE_DECLARE (e) =
4668 {
4669 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4670 .format_args = "i4i4",
4671 };
4672
4673 struct {
4674 u32 data[2];
4675 } * ed;
4676
4677 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4678 ed->data[0] = *(int *) buffer;
4679 ed->data[1] = *buflen;
4680 /* *INDENT-ON* */
4681 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004682 }
4683 else
4684 rv = VPPCOM_EINVAL;
4685 break;
4686
4687 case VPPCOM_ATTR_GET_ERROR:
4688 if (buffer && buflen && (*buflen >= sizeof (int)))
4689 {
4690 *(int *) buffer = 0;
4691 *buflen = sizeof (int);
4692
4693 if (VPPCOM_DEBUG > 2)
4694 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4695 "buflen %d, #VPP-TBD#", getpid (),
4696 *(int *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004697 if (VPPCOM_DEBUG > 0)
4698 {
4699 /* *INDENT-OFF* */
4700 ELOG_TYPE_DECLARE (e) =
4701 {
4702 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4703 .format_args = "i4i4",
4704 };
4705
4706 struct {
4707 u32 data[2];
4708 } * ed;
4709
4710 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4711 ed->data[0] = *(int *) buffer;
4712 ed->data[1] = *buflen;
4713 /* *INDENT-ON* */
4714 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004715 }
4716 else
4717 rv = VPPCOM_EINVAL;
4718 break;
4719
4720 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4721 if (buffer && buflen && (*buflen >= sizeof (u32)))
4722 {
4723 svm_fifo_t *tx_fifo;
4724
4725 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
4726 session->server_tx_fifo : session->server_rx_fifo);
4727
4728 /* VPP-TBD */
4729 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4730 tx_fifo ? tx_fifo->nitems :
4731 vcm->cfg.tx_fifo_size);
4732 *buflen = sizeof (u32);
4733
4734 if (VPPCOM_DEBUG > 2)
4735 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4736 "buflen %d, #VPP-TBD#", getpid (),
4737 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004738 if (VPPCOM_DEBUG > 0)
4739 {
4740 /* *INDENT-OFF* */
4741 ELOG_TYPE_DECLARE (e) =
4742 {
4743 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4744 .format_args = "i4i4",
4745 };
4746
4747 struct {
4748 u32 data[2];
4749 } * ed;
4750
4751 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4752 ed->data[0] = *(size_t *) buffer;
4753 ed->data[1] = *buflen;
4754 /* *INDENT-ON* */
4755 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004756 }
4757 else
4758 rv = VPPCOM_EINVAL;
4759 break;
4760
4761 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4762 if (buffer && buflen && (*buflen == sizeof (u32)))
4763 {
4764 /* VPP-TBD */
4765 session->sndbuf_size = *(u32 *) buffer;
4766 if (VPPCOM_DEBUG > 2)
4767 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4768 "buflen %d, #VPP-TBD#", getpid (),
4769 session->sndbuf_size, session->sndbuf_size,
4770 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004771 if (VPPCOM_DEBUG > 0)
4772 {
4773 /* *INDENT-OFF* */
4774 ELOG_TYPE_DECLARE (e) =
4775 {
4776 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4777 .format_args = "i4i4",
4778 };
4779
4780 struct {
4781 u32 data[2];
4782 } * ed;
4783
4784 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4785 ed->data[0] = session->sndbuf_size;
4786 ed->data[1] = *buflen;
4787 /* *INDENT-ON* */
4788 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004789 }
4790 else
4791 rv = VPPCOM_EINVAL;
4792 break;
4793
4794 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4795 if (buffer && buflen && (*buflen >= sizeof (u32)))
4796 {
4797 svm_fifo_t *rx_fifo;
4798
4799 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
4800 session->server_rx_fifo : session->server_tx_fifo);
4801
4802 /* VPP-TBD */
4803 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4804 rx_fifo ? rx_fifo->nitems :
4805 vcm->cfg.rx_fifo_size);
4806 *buflen = sizeof (u32);
4807
4808 if (VPPCOM_DEBUG > 2)
4809 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4810 "buflen %d, #VPP-TBD#", getpid (),
4811 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004812 if (VPPCOM_DEBUG > 0)
4813 {
4814 /* *INDENT-OFF* */
4815 ELOG_TYPE_DECLARE (e) =
4816 {
4817 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4818 .format_args = "i4i4",
4819 };
4820
4821 struct {
4822 u32 data[2];
4823 } * ed;
4824
4825 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4826 ed->data[0] = *(size_t *) buffer;
4827 ed->data[1] = *buflen;
4828 /* *INDENT-ON* */
4829 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004830 }
4831 else
4832 rv = VPPCOM_EINVAL;
4833 break;
4834
4835 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4836 if (buffer && buflen && (*buflen == sizeof (u32)))
4837 {
4838 /* VPP-TBD */
4839 session->rcvbuf_size = *(u32 *) buffer;
4840 if (VPPCOM_DEBUG > 2)
4841 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4842 "buflen %d, #VPP-TBD#", getpid (),
4843 session->sndbuf_size, session->sndbuf_size,
4844 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004845 if (VPPCOM_DEBUG > 0)
4846 {
4847 /* *INDENT-OFF* */
4848 ELOG_TYPE_DECLARE (e) =
4849 {
4850 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4851 .format_args = "i4i4",
4852 };
4853
4854 struct {
4855 u32 data[2];
4856 } * ed;
4857
4858 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4859 ed->data[0] = session->sndbuf_size;
4860 ed->data[1] = *buflen;
4861 /* *INDENT-ON* */
4862 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004863 }
4864 else
4865 rv = VPPCOM_EINVAL;
4866 break;
4867
4868 case VPPCOM_ATTR_GET_REUSEADDR:
4869 if (buffer && buflen && (*buflen >= sizeof (int)))
4870 {
4871 /* VPP-TBD */
4872 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4873 VCL_SESS_ATTR_REUSEADDR);
4874 *buflen = sizeof (int);
4875
4876 if (VPPCOM_DEBUG > 2)
4877 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4878 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4879 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004880 if (VPPCOM_DEBUG > 0)
4881 {
4882 /* *INDENT-OFF* */
4883 ELOG_TYPE_DECLARE (e) =
4884 {
4885 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4886 .format_args = "i4i4",
4887 };
4888
4889 struct {
4890 u32 data[2];
4891 } * ed;
4892
4893 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4894 ed->data[0] = *(int *) buffer;
4895 ed->data[1] = *buflen;
4896 /* *INDENT-ON* */
4897 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004898 }
4899 else
4900 rv = VPPCOM_EINVAL;
4901 break;
4902
Stevenb5a11602017-10-11 09:59:30 -07004903 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05004904 if (buffer && buflen && (*buflen == sizeof (int)) &&
4905 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4906 {
4907 /* VPP-TBD */
4908 if (*(int *) buffer)
4909 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4910 else
4911 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4912
4913 if (VPPCOM_DEBUG > 2)
4914 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4915 "buflen %d, #VPP-TBD#", getpid (),
4916 VCL_SESS_ATTR_TEST (session->attr,
4917 VCL_SESS_ATTR_REUSEADDR),
4918 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004919 if (VPPCOM_DEBUG > 0)
4920 {
4921 /* *INDENT-OFF* */
4922 ELOG_TYPE_DECLARE (e) =
4923 {
4924 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4925 .format_args = "i4i4",
4926 };
4927
4928 struct {
4929 u32 data[2];
4930 } * ed;
4931
4932 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4933 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4934 VCL_SESS_ATTR_REUSEADDR);
4935 ed->data[1] = *buflen;
4936 /* *INDENT-ON* */
4937 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004938 }
4939 else
4940 rv = VPPCOM_EINVAL;
4941 break;
4942
4943 case VPPCOM_ATTR_GET_REUSEPORT:
4944 if (buffer && buflen && (*buflen >= sizeof (int)))
4945 {
4946 /* VPP-TBD */
4947 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4948 VCL_SESS_ATTR_REUSEPORT);
4949 *buflen = sizeof (int);
4950
4951 if (VPPCOM_DEBUG > 2)
4952 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4953 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4954 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004955 if (VPPCOM_DEBUG > 0)
4956 {
4957 /* *INDENT-OFF* */
4958 ELOG_TYPE_DECLARE (e) =
4959 {
4960 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4961 .format_args = "i4i4",
4962 };
4963
4964 struct {
4965 u32 data[2];
4966 } * ed;
4967
4968 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4969 ed->data[0] = *(int *) buffer;
4970 ed->data[1] = *buflen;
4971 /* *INDENT-ON* */
4972 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004973 }
4974 else
4975 rv = VPPCOM_EINVAL;
4976 break;
4977
4978 case VPPCOM_ATTR_SET_REUSEPORT:
4979 if (buffer && buflen && (*buflen == sizeof (int)) &&
4980 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4981 {
4982 /* VPP-TBD */
4983 if (*(int *) buffer)
4984 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4985 else
4986 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
4987
4988 if (VPPCOM_DEBUG > 2)
4989 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
4990 "buflen %d, #VPP-TBD#", getpid (),
4991 VCL_SESS_ATTR_TEST (session->attr,
4992 VCL_SESS_ATTR_REUSEPORT),
4993 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004994 if (VPPCOM_DEBUG > 0)
4995 {
4996 /* *INDENT-OFF* */
4997 ELOG_TYPE_DECLARE (e) =
4998 {
4999 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
5000 .format_args = "i4i4",
5001 };
5002
5003 struct {
5004 u32 data[2];
5005 } * ed;
5006
5007 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5008 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5009 VCL_SESS_ATTR_REUSEPORT);
5010 ed->data[1] = *buflen;
5011 /* *INDENT-ON* */
5012 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005013 }
5014 else
5015 rv = VPPCOM_EINVAL;
5016 break;
5017
5018 case VPPCOM_ATTR_GET_BROADCAST:
5019 if (buffer && buflen && (*buflen >= sizeof (int)))
5020 {
5021 /* VPP-TBD */
5022 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5023 VCL_SESS_ATTR_BROADCAST);
5024 *buflen = sizeof (int);
5025
5026 if (VPPCOM_DEBUG > 2)
5027 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
5028 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5029 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08005030 if (VPPCOM_DEBUG > 0)
5031 {
5032 /* *INDENT-OFF* */
5033 ELOG_TYPE_DECLARE (e) =
5034 {
5035 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
5036 .format_args = "i4i4",
5037 };
5038
5039 struct {
5040 u32 data[2];
5041 } * ed;
5042
5043 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5044 ed->data[0] = *(int *) buffer;
5045 ed->data[1] = *buflen;
5046 /* *INDENT-ON* */
5047 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005048 }
5049 else
5050 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07005051 break;
5052
5053 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05005054 if (buffer && buflen && (*buflen == sizeof (int)))
5055 {
5056 /* VPP-TBD */
5057 if (*(int *) buffer)
5058 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
5059 else
5060 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
5061
5062 if (VPPCOM_DEBUG > 2)
5063 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
5064 "buflen %d, #VPP-TBD#", getpid (),
5065 VCL_SESS_ATTR_TEST (session->attr,
5066 VCL_SESS_ATTR_BROADCAST),
5067 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005068 if (VPPCOM_DEBUG > 0)
5069 {
5070 /* *INDENT-OFF* */
5071 ELOG_TYPE_DECLARE (e) =
5072 {
5073 .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
5074 .format_args = "i4i4",
5075 };
5076
5077 struct {
5078 u32 data[2];
5079 } * ed;
5080
5081 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5082 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5083 VCL_SESS_ATTR_BROADCAST);
5084 ed->data[1] = *buflen;
5085 /* *INDENT-ON* */
5086 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005087 }
5088 else
5089 rv = VPPCOM_EINVAL;
5090 break;
5091
5092 case VPPCOM_ATTR_GET_V6ONLY:
5093 if (buffer && buflen && (*buflen >= sizeof (int)))
5094 {
5095 /* VPP-TBD */
5096 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5097 VCL_SESS_ATTR_V6ONLY);
5098 *buflen = sizeof (int);
5099
5100 if (VPPCOM_DEBUG > 2)
5101 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
5102 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5103 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005104 if (VPPCOM_DEBUG > 0)
5105 {
5106 /* *INDENT-OFF* */
5107 ELOG_TYPE_DECLARE (e) =
5108 {
5109 .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
5110 .format_args = "i4i4",
5111 };
5112
5113 struct {
5114 u32 data[2];
5115 } * ed;
5116
5117 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5118 ed->data[0] = *(int *) buffer;
5119 ed->data[1] = *buflen;
5120 /* *INDENT-ON* */
5121 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005122 }
5123 else
5124 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07005125 break;
5126
5127 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05005128 if (buffer && buflen && (*buflen == sizeof (int)))
5129 {
5130 /* VPP-TBD */
5131 if (*(int *) buffer)
5132 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5133 else
5134 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5135
5136 if (VPPCOM_DEBUG > 2)
5137 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5138 "buflen %d, #VPP-TBD#", getpid (),
5139 VCL_SESS_ATTR_TEST (session->attr,
5140 VCL_SESS_ATTR_V6ONLY), *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005141 if (VPPCOM_DEBUG > 0)
5142 {
5143 /* *INDENT-OFF* */
5144 ELOG_TYPE_DECLARE (e) =
5145 {
5146 .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5147 .format_args = "i4i4",
5148 };
5149
5150 struct {
5151 u32 data[2];
5152 } * ed;
5153
5154 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5155 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5156 VCL_SESS_ATTR_V6ONLY);
5157 ed->data[1] = *buflen;
5158 /* *INDENT-ON* */
5159 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005160 }
5161 else
5162 rv = VPPCOM_EINVAL;
5163 break;
5164
5165 case VPPCOM_ATTR_GET_KEEPALIVE:
5166 if (buffer && buflen && (*buflen >= sizeof (int)))
5167 {
5168 /* VPP-TBD */
5169 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5170 VCL_SESS_ATTR_KEEPALIVE);
5171 *buflen = sizeof (int);
5172
5173 if (VPPCOM_DEBUG > 2)
5174 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5175 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5176 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005177 if (VPPCOM_DEBUG > 0)
5178 {
5179 /* *INDENT-OFF* */
5180 ELOG_TYPE_DECLARE (e) =
5181 {
5182 .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5183 .format_args = "i4i4",
5184 };
5185
5186 struct {
5187 u32 data[2];
5188 } * ed;
5189
5190 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5191 ed->data[0] = *(int *) buffer;
5192 ed->data[1] = *buflen;
5193 /* *INDENT-ON* */
5194 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005195 }
5196 else
5197 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07005198 break;
Stevenbccd3392017-10-12 20:42:21 -07005199
5200 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05005201 if (buffer && buflen && (*buflen == sizeof (int)))
5202 {
5203 /* VPP-TBD */
5204 if (*(int *) buffer)
5205 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5206 else
5207 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5208
5209 if (VPPCOM_DEBUG > 2)
5210 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5211 "buflen %d, #VPP-TBD#", getpid (),
5212 VCL_SESS_ATTR_TEST (session->attr,
5213 VCL_SESS_ATTR_KEEPALIVE),
5214 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005215 if (VPPCOM_DEBUG > 0)
5216 {
5217 /* *INDENT-OFF* */
5218 ELOG_TYPE_DECLARE (e) =
5219 {
5220 .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5221 .format_args = "i4i4",
5222 };
5223
5224 struct {
5225 u32 data[2];
5226 } * ed;
5227
5228 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5229 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5230 VCL_SESS_ATTR_KEEPALIVE);
5231 ed->data[1] = *buflen;
5232 /* *INDENT-ON* */
5233 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005234 }
5235 else
5236 rv = VPPCOM_EINVAL;
5237 break;
5238
5239 case VPPCOM_ATTR_GET_TCP_NODELAY:
5240 if (buffer && buflen && (*buflen >= sizeof (int)))
5241 {
5242 /* VPP-TBD */
5243 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5244 VCL_SESS_ATTR_TCP_NODELAY);
5245 *buflen = sizeof (int);
5246
5247 if (VPPCOM_DEBUG > 2)
5248 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5249 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5250 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005251 if (VPPCOM_DEBUG > 0)
5252 {
5253 /* *INDENT-OFF* */
5254 ELOG_TYPE_DECLARE (e) =
5255 {
5256 .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5257 .format_args = "i4i4",
5258 };
5259
5260 struct {
5261 u32 data[2];
5262 } * ed;
5263
5264 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5265 ed->data[0] = *(int *) buffer;
5266 ed->data[1] = *buflen;
5267 /* *INDENT-ON* */
5268 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005269 }
5270 else
5271 rv = VPPCOM_EINVAL;
5272 break;
5273
5274 case VPPCOM_ATTR_SET_TCP_NODELAY:
5275 if (buffer && buflen && (*buflen == sizeof (int)))
5276 {
5277 /* VPP-TBD */
5278 if (*(int *) buffer)
5279 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5280 else
5281 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5282
5283 if (VPPCOM_DEBUG > 2)
5284 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5285 "buflen %d, #VPP-TBD#", getpid (),
5286 VCL_SESS_ATTR_TEST (session->attr,
5287 VCL_SESS_ATTR_TCP_NODELAY),
5288 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005289 if (VPPCOM_DEBUG > 0)
5290 {
5291 /* *INDENT-OFF* */
5292 ELOG_TYPE_DECLARE (e) =
5293 {
5294 .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5295 .format_args = "i4i4",
5296 };
5297
5298 struct {
5299 u32 data[2];
5300 } * ed;
5301
5302 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5303 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5304 VCL_SESS_ATTR_TCP_NODELAY);
5305 ed->data[1] = *buflen;
5306 /* *INDENT-ON* */
5307 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005308 }
5309 else
5310 rv = VPPCOM_EINVAL;
5311 break;
5312
5313 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5314 if (buffer && buflen && (*buflen >= sizeof (int)))
5315 {
5316 /* VPP-TBD */
5317 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5318 VCL_SESS_ATTR_TCP_KEEPIDLE);
5319 *buflen = sizeof (int);
5320
5321 if (VPPCOM_DEBUG > 2)
5322 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5323 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5324 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005325 if (VPPCOM_DEBUG > 0)
5326 {
5327 /* *INDENT-OFF* */
5328 ELOG_TYPE_DECLARE (e) =
5329 {
5330 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5331 .format_args = "i4i4",
5332 };
5333
5334 struct {
5335 u32 data[2];
5336 } * ed;
5337
5338 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5339 ed->data[0] = *(int *) buffer;
5340 ed->data[1] = *buflen;
5341 /* *INDENT-ON* */
5342 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005343 }
5344 else
5345 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07005346 break;
5347
5348 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05005349 if (buffer && buflen && (*buflen == sizeof (int)))
5350 {
5351 /* VPP-TBD */
5352 if (*(int *) buffer)
5353 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5354 else
5355 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5356
5357 if (VPPCOM_DEBUG > 2)
5358 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5359 "buflen %d, #VPP-TBD#", getpid (),
5360 VCL_SESS_ATTR_TEST (session->attr,
5361 VCL_SESS_ATTR_TCP_KEEPIDLE),
5362 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005363 if (VPPCOM_DEBUG > 0)
5364 {
5365 /* *INDENT-OFF* */
5366 ELOG_TYPE_DECLARE (e) =
5367 {
5368 .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5369 .format_args = "i4i4",
5370 };
5371
5372 struct {
5373 u32 data[2];
5374 } * ed;
5375
5376 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5377 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5378 VCL_SESS_ATTR_TCP_KEEPIDLE);
5379 ed->data[1] = *buflen;
5380 /* *INDENT-ON* */
5381 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005382 }
5383 else
5384 rv = VPPCOM_EINVAL;
5385 break;
5386
5387 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5388 if (buffer && buflen && (*buflen >= sizeof (int)))
5389 {
5390 /* VPP-TBD */
5391 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5392 VCL_SESS_ATTR_TCP_KEEPINTVL);
5393 *buflen = sizeof (int);
5394
5395 if (VPPCOM_DEBUG > 2)
5396 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5397 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5398 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005399 if (VPPCOM_DEBUG > 0)
5400 {
5401 /* *INDENT-OFF* */
5402 ELOG_TYPE_DECLARE (e) =
5403 {
5404 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5405 .format_args = "i4i4",
5406 };
5407
5408 struct {
5409 u32 data[2];
5410 } * ed;
5411
5412 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5413 ed->data[0] = *(int *) buffer;
5414 ed->data[1] = *buflen;
5415 /* *INDENT-ON* */
5416 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005417 }
5418 else
5419 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07005420 break;
5421
5422 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05005423 if (buffer && buflen && (*buflen == sizeof (int)))
5424 {
5425 /* VPP-TBD */
5426 if (*(int *) buffer)
5427 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5428 else
5429 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5430
5431 if (VPPCOM_DEBUG > 2)
5432 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5433 "buflen %d, #VPP-TBD#", getpid (),
5434 VCL_SESS_ATTR_TEST (session->attr,
5435 VCL_SESS_ATTR_TCP_KEEPINTVL),
5436 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005437 if (VPPCOM_DEBUG > 0)
5438 {
5439 /* *INDENT-OFF* */
5440 ELOG_TYPE_DECLARE (e) =
5441 {
5442 .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5443 .format_args = "i4i4",
5444 };
5445
5446 struct {
5447 u32 data[2];
5448 } * ed;
5449
5450 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5451 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5452 VCL_SESS_ATTR_TCP_KEEPINTVL);
5453 ed->data[1] = *buflen;
5454 /* *INDENT-ON* */
5455 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005456 }
5457 else
5458 rv = VPPCOM_EINVAL;
5459 break;
5460
5461 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5462 if (buffer && buflen && (*buflen >= sizeof (u32)))
5463 {
5464 /* VPP-TBD */
5465 *(u32 *) buffer = session->user_mss;
5466 *buflen = sizeof (int);
5467
5468 if (VPPCOM_DEBUG > 2)
5469 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5470 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5471 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005472 if (VPPCOM_DEBUG > 0)
5473 {
5474 /* *INDENT-OFF* */
5475 ELOG_TYPE_DECLARE (e) =
5476 {
5477 .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5478 .format_args = "i4i4",
5479 };
5480
5481 struct {
5482 u32 data[2];
5483 } * ed;
5484
5485 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5486 ed->data[0] = *(int *) buffer;
5487 ed->data[1] = *buflen;
5488 /* *INDENT-ON* */
5489 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005490 }
5491 else
5492 rv = VPPCOM_EINVAL;
5493 break;
5494
5495 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5496 if (buffer && buflen && (*buflen == sizeof (u32)))
5497 {
5498 /* VPP-TBD */
5499 session->user_mss = *(u32 *) buffer;
5500
5501 if (VPPCOM_DEBUG > 2)
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005502 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
Dave Wallace048b1d62018-01-03 22:24:41 -05005503 "buflen %d, #VPP-TBD#", getpid (),
5504 session->user_mss, *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005505 if (VPPCOM_DEBUG > 0)
5506 {
5507 /* *INDENT-OFF* */
5508 ELOG_TYPE_DECLARE (e) =
5509 {
5510 .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5511 .format_args = "i4i4",
5512 };
5513
5514 struct {
5515 u32 data[2];
5516 } * ed;
5517
5518 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5519 ed->data[0] = session->user_mss;
5520 ed->data[1] = *buflen;
5521 /* *INDENT-ON* */
5522 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005523 }
5524 else
5525 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07005526 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04005527
5528 default:
5529 rv = VPPCOM_EINVAL;
5530 break;
Dave Wallace35830af2017-10-09 01:43:42 -04005531 }
5532
5533done:
5534 clib_spinlock_unlock (&vcm->sessions_lockp);
5535 return rv;
5536}
5537
Stevenac1f96d2017-10-24 16:03:58 -07005538int
5539vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5540 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5541{
Stevenac1f96d2017-10-24 16:03:58 -07005542 int rv = VPPCOM_OK;
5543 session_t *session = 0;
5544
5545 if (ep)
5546 {
5547 clib_spinlock_lock (&vcm->sessions_lockp);
5548 rv = vppcom_session_at_index (session_index, &session);
5549 if (PREDICT_FALSE (rv))
5550 {
5551 clib_spinlock_unlock (&vcm->sessions_lockp);
5552 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05005553 clib_warning ("VCL<%d>: invalid session, "
5554 "sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05005555 getpid (), session_index);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005556 if (VPPCOM_DEBUG > 0)
5557 {
5558 /* *INDENT-OFF* */
5559 ELOG_TYPE_DECLARE (e) =
5560 {
5561 .format = "invalid session: %d closed",
5562 .format_args = "i4",
5563 };
5564
5565 struct {
5566 u32 data;
5567 } * ed;
5568
5569 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5570 ed->data = session_index;
5571 /* *INDENT-ON* */
5572 }
Dave Wallacefaf9d772017-10-26 16:12:04 -04005573 rv = VPPCOM_EBADFD;
5574 clib_spinlock_unlock (&vcm->sessions_lockp);
5575 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07005576 }
5577 ep->vrf = session->vrf;
5578 ep->is_ip4 = session->peer_addr.is_ip4;
5579 ep->port = session->peer_port;
5580 if (session->peer_addr.is_ip4)
5581 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5582 sizeof (ip4_address_t));
5583 else
5584 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5585 sizeof (ip6_address_t));
5586 clib_spinlock_unlock (&vcm->sessions_lockp);
Stevenac1f96d2017-10-24 16:03:58 -07005587 }
Steven58f464e2017-10-25 12:33:12 -07005588
5589 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07005590 rv = vppcom_session_read (session_index, buffer, buflen);
5591 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07005592 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07005593 else
5594 {
Dave Wallace048b1d62018-01-03 22:24:41 -05005595 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5596 getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07005597 rv = VPPCOM_EAFNOSUPPORT;
5598 }
5599
Dave Wallacefaf9d772017-10-26 16:12:04 -04005600done:
Stevenac1f96d2017-10-24 16:03:58 -07005601 return rv;
5602}
5603
5604int
5605vppcom_session_sendto (uint32_t session_index, void *buffer,
5606 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5607{
Dave Wallace617dffa2017-10-26 14:47:06 -04005608 if (!buffer)
5609 return VPPCOM_EINVAL;
5610
5611 if (ep)
5612 {
5613 // TBD
5614 return VPPCOM_EINVAL;
5615 }
5616
5617 if (flags)
5618 {
5619 // TBD check the flags and do the right thing
5620 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05005621 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5622 "not implemented yet.", getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04005623 }
5624
5625 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07005626}
5627
Dave Wallace048b1d62018-01-03 22:24:41 -05005628int
5629vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5630{
5631 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5632 u32 i, keep_trying = 1;
5633 int rv, num_ev = 0;
5634
5635 if (VPPCOM_DEBUG > 3)
5636 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5637 getpid (), vp, n_sids, wait_for_time);
5638
5639 if (!vp)
5640 return VPPCOM_EFAULT;
5641
5642 do
5643 {
5644 session_t *session;
5645
5646 for (i = 0; i < n_sids; i++)
5647 {
5648 ASSERT (vp[i].revents);
5649
5650 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5651 clib_spinlock_unlock (&vcm->sessions_lockp);
5652
5653 if (*vp[i].revents)
5654 *vp[i].revents = 0;
5655
5656 if (POLLIN & vp[i].events)
5657 {
5658 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5659 rv = vppcom_session_read_ready (session, vp[i].sid);
5660 clib_spinlock_unlock (&vcm->sessions_lockp);
5661 if (rv > 0)
5662 {
5663 *vp[i].revents |= POLLIN;
5664 num_ev++;
5665 }
5666 else if (rv < 0)
5667 {
5668 switch (rv)
5669 {
5670 case VPPCOM_ECONNRESET:
5671 *vp[i].revents = POLLHUP;
5672 break;
5673
5674 default:
5675 *vp[i].revents = POLLERR;
5676 break;
5677 }
5678 num_ev++;
5679 }
5680 }
5681
5682 if (POLLOUT & vp[i].events)
5683 {
5684 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5685 rv = vppcom_session_write_ready (session, vp[i].sid);
5686 clib_spinlock_unlock (&vcm->sessions_lockp);
5687 if (rv > 0)
5688 {
5689 *vp[i].revents |= POLLOUT;
5690 num_ev++;
5691 }
5692 else if (rv < 0)
5693 {
5694 switch (rv)
5695 {
5696 case VPPCOM_ECONNRESET:
5697 *vp[i].revents = POLLHUP;
5698 break;
5699
5700 default:
5701 *vp[i].revents = POLLERR;
5702 break;
5703 }
5704 num_ev++;
5705 }
5706 }
5707
5708 if (0)
5709 {
5710 done:
5711 *vp[i].revents = POLLNVAL;
5712 num_ev++;
5713 }
5714 }
5715 if (wait_for_time != -1)
5716 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5717 }
5718 while ((num_ev == 0) && keep_trying);
5719
5720 if (VPPCOM_DEBUG > 3)
5721 {
5722 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5723 for (i = 0; i < n_sids; i++)
5724 {
5725 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5726 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5727 vp[i].events, *vp[i].revents);
5728 }
5729 }
5730 return num_ev;
5731}
5732
Dave Wallacee22aa742017-10-20 12:30:38 -04005733/*
5734 * fd.io coding-style-patch-verification: ON
5735 *
5736 * Local Variables:
5737 * eval: (c-set-style "gnu")
5738 * End:
5739 */