blob: 4b3581a664ccbd1eb899ec59a38411e2dd0e0bd8 [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
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800137 svm_fifo_t *rx_fifo;
138 svm_fifo_t *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 Wallacef7f809c2017-10-03 01:48:42 -0400149 u8 is_vep;
150 u8 is_vep_session;
Dave Wallace048b1d62018-01-03 22:24:41 -0500151 u32 attr;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400152 u32 wait_cont_idx;
153 vppcom_epoll_t vep;
Dave Wallace048b1d62018-01-03 22:24:41 -0500154 int libc_epfd;
Dave Wallace35830af2017-10-09 01:43:42 -0400155 vppcom_ip46_t lcl_addr;
156 vppcom_ip46_t peer_addr;
Stevenac1f96d2017-10-24 16:03:58 -0700157 u16 lcl_port; // network order
158 u16 peer_port; // network order
Dave Wallace543852a2017-08-03 02:11:34 -0400159 u8 proto;
160 u64 client_queue_address;
161 u64 options[16];
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800162 elog_track_t elog_track;
Dave Wallace543852a2017-08-03 02:11:34 -0400163} session_t;
164
165typedef struct vppcom_cfg_t_
166{
167 u64 heapsize;
Dave Wallacec8f1ee62017-11-29 22:46:32 -0500168 u32 vpp_api_q_length;
Dave Wallace543852a2017-08-03 02:11:34 -0400169 u64 segment_baseva;
170 u32 segment_size;
171 u32 add_segment_size;
172 u32 preallocated_fifo_pairs;
173 u32 rx_fifo_size;
174 u32 tx_fifo_size;
175 u32 event_queue_size;
176 u32 listen_queue_size;
Dave Wallace774169b2017-11-01 20:07:40 -0400177 u8 app_proxy_transport_tcp;
178 u8 app_proxy_transport_udp;
179 u8 app_scope_local;
180 u8 app_scope_global;
Dave Wallace8af20542017-10-26 03:29:30 -0400181 u8 *namespace_id;
182 u64 namespace_secret;
Dave Wallace543852a2017-08-03 02:11:34 -0400183 f64 app_timeout;
184 f64 session_timeout;
185 f64 accept_timeout;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800186 u32 event_ring_size;
187 char *event_log_path;
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500188 u8 *vpp_api_filename;
Dave Wallace543852a2017-08-03 02:11:34 -0400189} vppcom_cfg_t;
190
191typedef struct vppcom_main_t_
192{
193 u8 init;
Dave Wallace498b3a52017-11-09 13:00:34 -0500194 u32 debug;
Dave Wallace543852a2017-08-03 02:11:34 -0400195 u32 *client_session_index_fifo;
Dave Wallace543852a2017-08-03 02:11:34 -0400196 int main_cpu;
197
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500198 /* vpp input queue */
Florin Corase86a8ed2018-01-05 03:20:25 -0800199 svm_queue_t *vl_input_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400200
201 /* API client handle */
202 u32 my_client_index;
203
204 /* Session pool */
205 clib_spinlock_t sessions_lockp;
206 session_t *sessions;
207
208 /* Hash table for disconnect processing */
209 uword *session_index_by_vpp_handles;
210
211 /* Select bitmaps */
212 clib_bitmap_t *rd_bitmap;
213 clib_bitmap_t *wr_bitmap;
214 clib_bitmap_t *ex_bitmap;
215
216 /* Our event queue */
Florin Corase86a8ed2018-01-05 03:20:25 -0800217 svm_queue_t *app_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -0400218
219 /* unique segment name counter */
220 u32 unique_segment_index;
221
Dave Wallace543852a2017-08-03 02:11:34 -0400222 /* For deadman timers */
223 clib_time_t clib_time;
224
225 /* State of the connection, shared between msg RX thread and main thread */
226 volatile app_state_t app_state;
227
228 vppcom_cfg_t cfg;
229
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800230 /* Event logging */
231 elog_main_t elog_main;
232 elog_track_t elog_track;
233
Dave Wallace543852a2017-08-03 02:11:34 -0400234 /* VNET_API_ERROR_FOO -> "Foo" hash table */
235 uword *error_string_by_error_number;
236} vppcom_main_t;
237
Dave Wallace2e005bb2017-11-07 01:21:39 -0500238/* NOTE: _vppcom_main is only used until the heap is allocated.
239 * Do not access it directly -- use vcm which will point to
240 * the heap allocated copy after init.
241 */
Dave Wallace498b3a52017-11-09 13:00:34 -0500242static vppcom_main_t _vppcom_main = {
243 .debug = VPPCOM_DEBUG_INIT,
244 .my_client_index = ~0
245};
Dave Wallace2e005bb2017-11-07 01:21:39 -0500246
247static vppcom_main_t *vcm = &_vppcom_main;
Dave Wallace543852a2017-08-03 02:11:34 -0400248
Dave Wallace048b1d62018-01-03 22:24:41 -0500249#define VCL_LOCK_AND_GET_SESSION(I, S) \
250do { \
251 clib_spinlock_lock (&vcm->sessions_lockp); \
252 rv = vppcom_session_at_index (I, S); \
253 if (PREDICT_FALSE (rv)) \
254 { \
255 clib_spinlock_unlock (&vcm->sessions_lockp); \
256 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
257 getpid (), I); \
258 goto done; \
259 } \
Dave Wallace60caa062017-11-10 17:07:13 -0500260} while (0)
261
Dave Wallace543852a2017-08-03 02:11:34 -0400262static const char *
263vppcom_app_state_str (app_state_t state)
264{
265 char *st;
266
267 switch (state)
268 {
269 case STATE_APP_START:
270 st = "STATE_APP_START";
271 break;
272
273 case STATE_APP_CONN_VPP:
274 st = "STATE_APP_CONN_VPP";
275 break;
276
277 case STATE_APP_ENABLED:
278 st = "STATE_APP_ENABLED";
279 break;
280
281 case STATE_APP_ATTACHED:
282 st = "STATE_APP_ATTACHED";
283 break;
284
285 default:
286 st = "UNKNOWN_APP_STATE";
287 break;
288 }
289
290 return st;
291}
292
293static const char *
294vppcom_session_state_str (session_state_t state)
295{
296 char *st;
297
298 switch (state)
299 {
300 case STATE_START:
301 st = "STATE_START";
302 break;
303
304 case STATE_CONNECT:
305 st = "STATE_CONNECT";
306 break;
307
308 case STATE_LISTEN:
309 st = "STATE_LISTEN";
310 break;
311
312 case STATE_ACCEPT:
313 st = "STATE_ACCEPT";
314 break;
315
Dave Wallace4878cbe2017-11-21 03:45:09 -0500316 case STATE_CLOSE_ON_EMPTY:
317 st = "STATE_CLOSE_ON_EMPTY";
318 break;
319
Dave Wallace543852a2017-08-03 02:11:34 -0400320 case STATE_DISCONNECT:
321 st = "STATE_DISCONNECT";
322 break;
323
324 case STATE_FAILED:
325 st = "STATE_FAILED";
326 break;
327
328 default:
329 st = "UNKNOWN_STATE";
330 break;
331 }
332
333 return st;
334}
335
336/*
337 * VPPCOM Utility Functions
338 */
339static inline int
340vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
341{
Dave Wallace543852a2017-08-03 02:11:34 -0400342 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
343 if (PREDICT_FALSE ((session_index == ~0) ||
344 pool_is_free_index (vcm->sessions, session_index)))
345 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500346 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500347 getpid (), session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400348 return VPPCOM_EBADFD;
349 }
350 *sess = pool_elt_at_index (vcm->sessions, session_index);
351 return VPPCOM_OK;
352}
353
Florin Corasdcf55ce2017-11-16 15:32:50 -0800354static inline void
355vppcom_session_table_add_listener (u64 listener_handle, u32 value)
356{
357 /* Session and listener handles have different formats. The latter has
358 * the thread index in the upper 32 bits while the former has the session
359 * type. Knowing that, for listeners we just flip the MSB to 1 */
360 listener_handle |= 1ULL << 63;
361 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
362}
363
364static inline session_t *
365vppcom_session_table_lookup_listener (u64 listener_handle)
366{
367 uword *p;
368 u64 handle = listener_handle | (1ULL << 63);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500369 session_t *session;
370
Florin Corasdcf55ce2017-11-16 15:32:50 -0800371 p = hash_get (vcm->session_index_by_vpp_handles, handle);
372 if (!p)
373 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500374 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
Florin Corasdcf55ce2017-11-16 15:32:50 -0800375 "listener handle %llx", getpid (), listener_handle);
376 return 0;
377 }
378 if (pool_is_free_index (vcm->sessions, p[0]))
379 {
380 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500381 clib_warning ("VCL<%d>: invalid listen session, sid (%u)",
382 getpid (), p[0]);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800383 return 0;
384 }
385
Dave Wallace4878cbe2017-11-21 03:45:09 -0500386 session = pool_elt_at_index (vcm->sessions, p[0]);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800387 ASSERT (session->state & STATE_LISTEN);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500388 return session;
Florin Corasdcf55ce2017-11-16 15:32:50 -0800389}
390
391static inline void
392vppcom_session_table_del_listener (u64 listener_handle)
393{
394 listener_handle |= 1ULL << 63;
395 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
396}
397
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800398static void
399write_elog (void)
400{
401 elog_main_t *em = &vcm->elog_main;
402 char *chroot_file;
403 clib_error_t *error = 0;
404
405 chroot_file =
406 (char *) format (0, "%s/%d-%d-vcl-elog%c", vcm->cfg.event_log_path,
407 vcm->my_client_index, getpid (), 0);
408 error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
409 if (error)
410 {
411 clib_error_report (error);
412 }
413 if (VPPCOM_DEBUG > 0)
414 clib_warning ("[%d] Event Log:'%s' ", getpid (), chroot_file);
415
416}
417
Dave Wallace543852a2017-08-03 02:11:34 -0400418static int
419vppcom_connect_to_vpp (char *app_name)
420{
421 api_main_t *am = &api_main;
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500422 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800423 int rv = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400424
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500425 if (!vcl_cfg->vpp_api_filename)
426 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Dave Wallace048b1d62018-01-03 22:24:41 -0500427
Dave Wallaceb5a86ee2018-02-16 18:26:11 -0500428 if (VPPCOM_DEBUG > 0)
429 clib_warning ("VCL<%d>: app (%s) connecting to VPP api (%s)...",
430 getpid (), app_name, vcl_cfg->vpp_api_filename);
431
432 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name,
Dave Wallacec8f1ee62017-11-29 22:46:32 -0500433 vcm->cfg.vpp_api_q_length) < 0)
Dave Wallace543852a2017-08-03 02:11:34 -0400434 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500435 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800436 rv = VPPCOM_ECONNREFUSED;
437 }
438 else
439 {
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800440 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
441 vcm->my_client_index = am->my_client_index;
442 vcm->app_state = STATE_APP_CONN_VPP;
Dave Wallace9b954252018-01-18 17:01:40 -0500443
444 if (VPPCOM_DEBUG > 0)
445 clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
446 getpid (), app_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400447 }
448
Dave Wallace543852a2017-08-03 02:11:34 -0400449 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800450 {
451 vcm->elog_main.lock =
452 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
453 vcm->elog_main.lock[0] = 0;
454 vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
455 elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
456 elog_enable_disable (&vcm->elog_main, 1);
Dave Wallace543852a2017-08-03 02:11:34 -0400457
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800458 vcm->elog_track.name =
459 (char *) format (0, "P:%d:C:%d%c", getpid (),
460 vcm->my_client_index, 0);
461 elog_track_register (&vcm->elog_main, &vcm->elog_track);
462
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800463 /* *INDENT-OFF* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800464 ELOG_TYPE_DECLARE (e) =
465 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800466 .format = "connect_vpp:rv:%d",
467 .format_args = "i4",
468 };
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800469 struct
470 {
471 u32 data;
472 } *ed;
473 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
474 ed->data = rv;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800475 /* *INDENT-ON* */
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800476 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800477 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400478}
479
480static u8 *
481format_api_error (u8 * s, va_list * args)
482{
Dave Wallace543852a2017-08-03 02:11:34 -0400483 i32 error = va_arg (*args, u32);
484 uword *p;
485
486 p = hash_get (vcm->error_string_by_error_number, -error);
487
488 if (p)
489 s = format (s, "%s (%d)", p[0], error);
490 else
491 s = format (s, "%d", error);
492 return s;
493}
494
495static void
496vppcom_init_error_string_table (void)
497{
Dave Wallace543852a2017-08-03 02:11:34 -0400498 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
499
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -0800500#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
Dave Wallace543852a2017-08-03 02:11:34 -0400501 foreach_vnet_api_error;
502#undef _
503
504 hash_set (vcm->error_string_by_error_number, 99, "Misc");
505}
506
507static inline int
508vppcom_wait_for_app_state_change (app_state_t app_state)
509{
Dave Wallace543852a2017-08-03 02:11:34 -0400510 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
511
512 while (clib_time_now (&vcm->clib_time) < timeout)
513 {
514 if (vcm->app_state == app_state)
515 return VPPCOM_OK;
516 }
517 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500518 clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400519 vppcom_app_state_str (app_state), app_state);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800520
521 if (VPPCOM_DEBUG > 0)
522 {
523 /* *INDENT-OFF* */
524 ELOG_TYPE_DECLARE (e) =
525 {
526 .format = "ERR: timeout state:%d",
527 .format_args = "i4",
528 };
529 struct
530 {
531 u32 data;
532 } *ed;
533
534 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
535
536 ed->data = app_state;
537 /* *INDENT-ON* */
538 }
539
Dave Wallace543852a2017-08-03 02:11:34 -0400540 return VPPCOM_ETIMEDOUT;
541}
542
543static inline int
544vppcom_wait_for_session_state_change (u32 session_index,
545 session_state_t state,
546 f64 wait_for_time)
547{
Dave Wallace543852a2017-08-03 02:11:34 -0400548 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
549 session_t *volatile session;
550 int rv;
551
552 do
553 {
554 clib_spinlock_lock (&vcm->sessions_lockp);
555 rv = vppcom_session_at_index (session_index, &session);
556 if (PREDICT_FALSE (rv))
557 {
558 clib_spinlock_unlock (&vcm->sessions_lockp);
559 return rv;
560 }
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -0800561 if (session->state & state)
Dave Wallace543852a2017-08-03 02:11:34 -0400562 {
563 clib_spinlock_unlock (&vcm->sessions_lockp);
564 return VPPCOM_OK;
565 }
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -0800566 if (session->state & STATE_FAILED)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500567 {
568 clib_spinlock_unlock (&vcm->sessions_lockp);
569 return VPPCOM_ECONNREFUSED;
570 }
571
Dave Wallace543852a2017-08-03 02:11:34 -0400572 clib_spinlock_unlock (&vcm->sessions_lockp);
573 }
574 while (clib_time_now (&vcm->clib_time) < timeout);
575
576 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500577 clib_warning ("VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (),
Dave Wallace4878cbe2017-11-21 03:45:09 -0500578 state, vppcom_session_state_str (state));
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800579
580 if (VPPCOM_DEBUG > 0)
581 {
582 /* *INDENT-OFF* */
583 ELOG_TYPE_DECLARE (e) =
584 {
585 .format = "ERR: timeout state:%d",
586 .format_args = "i4",
587 };
588 struct
589 {
590 u32 data;
591 } *ed;
592
593 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
594
595 ed->data = state;
596 /* *INDENT-ON* */
597 }
598
Dave Wallace543852a2017-08-03 02:11:34 -0400599 return VPPCOM_ETIMEDOUT;
600}
601
602static inline int
603vppcom_wait_for_client_session_index (f64 wait_for_time)
604{
Dave Wallace543852a2017-08-03 02:11:34 -0400605 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
606
607 do
608 {
609 if (clib_fifo_elts (vcm->client_session_index_fifo))
610 return VPPCOM_OK;
611 }
612 while (clib_time_now (&vcm->clib_time) < timeout);
613
614 if (wait_for_time == 0)
615 return VPPCOM_EAGAIN;
616
617 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500618 clib_warning ("VCL<%d>: timeout waiting for client_session_index",
619 getpid ());
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -0800620
621 if (VPPCOM_DEBUG > 0)
622 {
623 /* *INDENT-OFF* */
624 ELOG_TYPE_DECLARE (e) =
625 {
626 .format = "ERR: timeout waiting for session index :%d",
627 .format_args = "i4",
628 };
629 struct
630 {
631 u32 data;
632 } *ed;
633
634 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
635
636 ed->data = getpid();
637 /* *INDENT-ON* */
638 }
639
Dave Wallace543852a2017-08-03 02:11:34 -0400640 return VPPCOM_ETIMEDOUT;
641}
642
643/*
644 * VPP-API message functions
645 */
646static void
647vppcom_send_session_enable_disable (u8 is_enable)
648{
Dave Wallace543852a2017-08-03 02:11:34 -0400649 vl_api_session_enable_disable_t *bmp;
650 bmp = vl_msg_api_alloc (sizeof (*bmp));
651 memset (bmp, 0, sizeof (*bmp));
652
653 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
654 bmp->client_index = vcm->my_client_index;
655 bmp->context = htonl (0xfeedface);
656 bmp->is_enable = is_enable;
657 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
658}
659
660static int
661vppcom_app_session_enable (void)
662{
Dave Wallace543852a2017-08-03 02:11:34 -0400663 int rv;
664
665 if (vcm->app_state != STATE_APP_ENABLED)
666 {
667 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
668 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
669 if (PREDICT_FALSE (rv))
670 {
671 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500672 clib_warning ("VCL<%d>: application session enable timed out! "
Dave Wallaceee45d412017-11-24 21:44:06 -0500673 "returning %d (%s)",
674 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -0400675 return rv;
676 }
677 }
678 return VPPCOM_OK;
679}
680
681static void
682 vl_api_session_enable_disable_reply_t_handler
683 (vl_api_session_enable_disable_reply_t * mp)
684{
Dave Wallace543852a2017-08-03 02:11:34 -0400685 if (mp->retval)
686 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500687 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400688 format_api_error, ntohl (mp->retval));
689 }
690 else
691 vcm->app_state = STATE_APP_ENABLED;
692}
693
694static void
695vppcom_app_send_attach (void)
696{
Dave Wallace543852a2017-08-03 02:11:34 -0400697 vl_api_application_attach_t *bmp;
Dave Wallace8af20542017-10-26 03:29:30 -0400698 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
Dave Wallace774169b2017-11-01 20:07:40 -0400699 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
700 vcm->cfg.app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -0400701
Dave Wallace543852a2017-08-03 02:11:34 -0400702 bmp = vl_msg_api_alloc (sizeof (*bmp));
703 memset (bmp, 0, sizeof (*bmp));
704
705 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
706 bmp->client_index = vcm->my_client_index;
707 bmp->context = htonl (0xfeedface);
708 bmp->options[APP_OPTIONS_FLAGS] =
Florin Corascea194d2017-10-02 00:18:51 -0700709 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
Dave Wallace774169b2017-11-01 20:07:40 -0400710 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
711 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
712 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
713 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
714 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
715 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
Florin Corasff6e7692017-12-11 04:59:01 -0800716 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
717 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
718 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
719 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
Florin Corasf32cff62017-12-09 08:15:00 -0800720 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
721 vcm->cfg.preallocated_fifo_pairs;
Florin Corasff6e7692017-12-11 04:59:01 -0800722 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
Dave Wallace8af20542017-10-26 03:29:30 -0400723 if (nsid_len)
724 {
725 bmp->namespace_id_len = nsid_len;
726 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
727 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
728 }
Dave Wallace543852a2017-08-03 02:11:34 -0400729 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
730}
731
732static int
733vppcom_app_attach (void)
734{
Dave Wallace543852a2017-08-03 02:11:34 -0400735 int rv;
736
737 vppcom_app_send_attach ();
738 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
739 if (PREDICT_FALSE (rv))
740 {
741 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -0500742 clib_warning ("VCL<%d>: application attach timed out! "
743 "returning %d (%s)",
Dave Wallaceee45d412017-11-24 21:44:06 -0500744 getpid (), rv, vppcom_retval_str (rv));
Dave Wallace543852a2017-08-03 02:11:34 -0400745 return rv;
746 }
747 return VPPCOM_OK;
748}
749
750static void
751vppcom_app_detach (void)
752{
Dave Wallace543852a2017-08-03 02:11:34 -0400753 vl_api_application_detach_t *bmp;
754 bmp = vl_msg_api_alloc (sizeof (*bmp));
755 memset (bmp, 0, sizeof (*bmp));
756
757 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
758 bmp->client_index = vcm->my_client_index;
759 bmp->context = htonl (0xfeedface);
760 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
761}
762
763static void
764vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
765 mp)
766{
Dave Wallace543852a2017-08-03 02:11:34 -0400767 static svm_fifo_segment_create_args_t _a;
768 svm_fifo_segment_create_args_t *a = &_a;
769 int rv;
770
771 memset (a, 0, sizeof (*a));
772 if (mp->retval)
773 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500774 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400775 format_api_error, ntohl (mp->retval));
776 return;
777 }
778
779 if (mp->segment_name_length == 0)
780 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500781 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400782 return;
783 }
784
785 a->segment_name = (char *) mp->segment_name;
786 a->segment_size = mp->segment_size;
787
788 ASSERT (mp->app_event_queue_address);
789
790 /* Attach to the segment vpp created */
791 rv = svm_fifo_segment_attach (a);
792 vec_reset_length (a->new_segment_indices);
793 if (PREDICT_FALSE (rv))
794 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500795 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
796 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400797 return;
798 }
799
800 vcm->app_event_queue =
Florin Corase86a8ed2018-01-05 03:20:25 -0800801 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400802
803 vcm->app_state = STATE_APP_ATTACHED;
804}
805
806static void
807vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
808 mp)
809{
Dave Wallace543852a2017-08-03 02:11:34 -0400810 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -0500811 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
Dave Wallace543852a2017-08-03 02:11:34 -0400812 ntohl (mp->retval));
813
814 vcm->app_state = STATE_APP_ENABLED;
815}
816
817static void
818vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
819 mp)
820{
Dave Wallace543852a2017-08-03 02:11:34 -0400821 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -0500822 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
Dave Wallace4878cbe2017-11-21 03:45:09 -0500823 getpid (), mp->handle, format_api_error,
824 ntohl (mp->retval));
Dave Wallace543852a2017-08-03 02:11:34 -0400825}
826
827static void
828vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
829{
Dave Wallace543852a2017-08-03 02:11:34 -0400830 static svm_fifo_segment_create_args_t _a;
831 svm_fifo_segment_create_args_t *a = &_a;
832 int rv;
833
834 memset (a, 0, sizeof (*a));
835 a->segment_name = (char *) mp->segment_name;
836 a->segment_size = mp->segment_size;
837 /* Attach to the segment vpp created */
838 rv = svm_fifo_segment_attach (a);
839 vec_reset_length (a->new_segment_indices);
840 if (PREDICT_FALSE (rv))
841 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500842 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500843 getpid (), mp->segment_name);
Dave Wallace543852a2017-08-03 02:11:34 -0400844 return;
845 }
846 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500847 clib_warning ("VCL<%d>: mapped new segment '%s' size %d", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -0400848 mp->segment_name, mp->segment_size);
849}
850
851static void
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800852vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
853{
854
855/*
856 * XXX Need segment_name to session_id hash,
857 * XXX - have sessionID by handle hash currently
858 */
859 clib_warning ("Unmapped segment '%s'", mp->segment_name);
860}
861
862static void
Dave Wallace543852a2017-08-03 02:11:34 -0400863vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
864{
Dave Wallace543852a2017-08-03 02:11:34 -0400865 uword *p;
Dave Wallace543852a2017-08-03 02:11:34 -0400866
867 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
868 if (p)
869 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500870 int rv;
871 session_t *session = 0;
872 u32 session_index = p[0];
873
874 VCL_LOCK_AND_GET_SESSION (session_index, &session);
875 session->state = STATE_CLOSE_ON_EMPTY;
876
877 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500878 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500879 "setting state to 0x%x (%s)",
Dave Wallace4878cbe2017-11-21 03:45:09 -0500880 getpid (), mp->handle, session_index, session->state,
881 vppcom_session_state_str (session->state));
Dave Wallace543852a2017-08-03 02:11:34 -0400882 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500883 return;
884
885 done:
886 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500887 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500888 "session lookup failed!",
889 getpid (), mp->handle, session_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400890 }
891 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500892 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500893 "handle failed!", getpid (), mp->handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400894}
895
896static void
897vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
898{
Dave Wallace543852a2017-08-03 02:11:34 -0400899 session_t *session = 0;
900 vl_api_reset_session_reply_t *rmp;
901 uword *p;
902 int rv = 0;
903
904 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
905 if (p)
906 {
907 int rval;
908 clib_spinlock_lock (&vcm->sessions_lockp);
909 rval = vppcom_session_at_index (p[0], &session);
910 if (PREDICT_FALSE (rval))
911 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500912 rv = VNET_API_ERROR_INVALID_VALUE_2;
Dave Wallace048b1d62018-01-03 22:24:41 -0500913 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500914 "session lookup failed! returning %d %U",
915 getpid (), mp->handle, p[0],
916 rv, format_api_error, rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400917 }
918 else
Dave Wallace4878cbe2017-11-21 03:45:09 -0500919 {
920 /* TBD: should this disconnect immediately and
921 * flush the fifos?
922 */
923 session->state = STATE_CLOSE_ON_EMPTY;
924
925 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -0500926 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500927 "state set to %d (%s)!", getpid (),
928 mp->handle, p[0], session->state,
929 vppcom_session_state_str (session->state));
930 }
Dave Wallace543852a2017-08-03 02:11:34 -0400931 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -0400932 }
933 else
934 {
Dave Wallace4878cbe2017-11-21 03:45:09 -0500935 rv = VNET_API_ERROR_INVALID_VALUE;
Dave Wallace048b1d62018-01-03 22:24:41 -0500936 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
Dave Wallace4878cbe2017-11-21 03:45:09 -0500937 "failed! returning %d %U",
938 getpid (), mp->handle, rv, format_api_error, rv);
Dave Wallace543852a2017-08-03 02:11:34 -0400939 }
940
941 rmp = vl_msg_api_alloc (sizeof (*rmp));
942 memset (rmp, 0, sizeof (*rmp));
943 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
944 rmp->retval = htonl (rv);
945 rmp->handle = mp->handle;
946 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
947}
948
949static void
Dave Wallace33e002b2017-09-06 01:20:02 -0400950vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
Dave Wallace543852a2017-08-03 02:11:34 -0400951{
Dave Wallaceee45d412017-11-24 21:44:06 -0500952 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400953 u32 session_index;
954 svm_fifo_t *rx_fifo, *tx_fifo;
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -0800955 int rv = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400956
Dave Wallace4878cbe2017-11-21 03:45:09 -0500957 session_index = mp->context;
Dave Wallaceee45d412017-11-24 21:44:06 -0500958 VCL_LOCK_AND_GET_SESSION (session_index, &session);
959done:
Dave Wallace543852a2017-08-03 02:11:34 -0400960 if (mp->retval)
961 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500962 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500963 "connect failed! %U",
964 getpid (), mp->handle, session_index,
965 format_api_error, ntohl (mp->retval));
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -0800966 if (session)
Dave Wallaceee45d412017-11-24 21:44:06 -0500967 {
968 session->state = STATE_FAILED;
969 session->vpp_handle = mp->handle;
970 }
971 else
972 {
973 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
974 "Invalid session index (%u)!",
975 getpid (), mp->handle, session_index);
976 }
977 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400978 }
979
Dave Wallaceee45d412017-11-24 21:44:06 -0500980 if (rv)
981 goto done_unlock;
Dave Wallace543852a2017-08-03 02:11:34 -0400982
Dave Wallace543852a2017-08-03 02:11:34 -0400983 /*
984 * Setup session
985 */
Dave Wallace33e002b2017-09-06 01:20:02 -0400986 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
Florin Corase86a8ed2018-01-05 03:20:25 -0800987 svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -0400988
989 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
990 rx_fifo->client_session_index = session_index;
991 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
992 tx_fifo->client_session_index = session_index;
993
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800994 session->rx_fifo = rx_fifo;
995 session->tx_fifo = tx_fifo;
Dave Wallace60caa062017-11-10 17:07:13 -0500996 session->vpp_handle = mp->handle;
Dave Wallace9d1d73a2017-11-20 02:31:48 -0500997 session->lcl_addr.is_ip4 = mp->is_ip4;
998 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
999 sizeof (session->peer_addr.ip46));
1000 session->lcl_port = mp->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001001 session->state = STATE_CONNECT;
1002
1003 /* Add it to lookup table */
1004 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Dave Wallace60caa062017-11-10 17:07:13 -05001005
1006 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001007 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded!"
Dave Wallaceee45d412017-11-24 21:44:06 -05001008 " session_rx_fifo %p, refcnt %d,"
1009 " session_tx_fifo %p, refcnt %d",
1010 getpid (), mp->handle, session_index,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001011 session->rx_fifo,
1012 session->rx_fifo->refcnt,
1013 session->tx_fifo, session->tx_fifo->refcnt);
Dave Wallaceee45d412017-11-24 21:44:06 -05001014done_unlock:
Dave Wallace543852a2017-08-03 02:11:34 -04001015 clib_spinlock_unlock (&vcm->sessions_lockp);
1016}
1017
1018static void
1019vppcom_send_connect_sock (session_t * session, u32 session_index)
1020{
Dave Wallace543852a2017-08-03 02:11:34 -04001021 vl_api_connect_sock_t *cmp;
1022
1023 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
Dave Wallace543852a2017-08-03 02:11:34 -04001024 cmp = vl_msg_api_alloc (sizeof (*cmp));
1025 memset (cmp, 0, sizeof (*cmp));
1026 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
1027 cmp->client_index = vcm->my_client_index;
Dave Wallace33e002b2017-09-06 01:20:02 -04001028 cmp->context = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001029
Dave Wallace35830af2017-10-09 01:43:42 -04001030 cmp->is_ip4 = session->peer_addr.is_ip4;
1031 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001032 cmp->port = session->peer_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001033 cmp->proto = session->proto;
1034 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
1035 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
1036}
1037
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001038static inline void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001039vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
1040 int rv)
1041{
1042 vl_api_disconnect_session_reply_t *rmp;
1043
1044 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001045 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1046 "sending disconnect msg",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001047 getpid (), vpp_handle, session_index);
1048
1049 rmp = vl_msg_api_alloc (sizeof (*rmp));
1050 memset (rmp, 0, sizeof (*rmp));
1051
1052 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
1053 rmp->retval = htonl (rv);
1054 rmp->handle = vpp_handle;
1055 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1056}
1057
1058static inline void
1059vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001060{
Dave Wallace543852a2017-08-03 02:11:34 -04001061 vl_api_disconnect_session_t *dmp;
Dave Wallace543852a2017-08-03 02:11:34 -04001062
Dave Wallace4878cbe2017-11-21 03:45:09 -05001063 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001064 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1065 "sending disconnect msg",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001066 getpid (), vpp_handle, session_index);
1067
Dave Wallace543852a2017-08-03 02:11:34 -04001068 dmp = vl_msg_api_alloc (sizeof (*dmp));
1069 memset (dmp, 0, sizeof (*dmp));
1070 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
1071 dmp->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001072 dmp->handle = vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001073 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001074}
1075
1076static void
1077vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
1078{
Dave Wallace543852a2017-08-03 02:11:34 -04001079 session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001080 u32 session_index = mp->context;
Dave Wallace543852a2017-08-03 02:11:34 -04001081 int rv;
1082
Dave Wallaceee45d412017-11-24 21:44:06 -05001083 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1084done:
Dave Wallace543852a2017-08-03 02:11:34 -04001085 if (mp->retval)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001086 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001087 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
1088 "sid %u: bind failed: %U",
1089 getpid (), mp->handle, session_index,
1090 format_api_error, ntohl (mp->retval));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001091 rv = vppcom_session_at_index (session_index, &session);
1092 if (rv == VPPCOM_OK)
Dave Wallaceee45d412017-11-24 21:44:06 -05001093 {
1094 session->state = STATE_FAILED;
1095 session->vpp_handle = mp->handle;
1096 }
1097 else
1098 {
1099 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1100 "Invalid session index (%u)!",
1101 getpid (), mp->handle, session_index);
1102 }
1103 goto done_unlock;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001104 }
Dave Wallace543852a2017-08-03 02:11:34 -04001105
Dave Wallaceee45d412017-11-24 21:44:06 -05001106 session->vpp_handle = mp->handle;
1107 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1108 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1109 sizeof (session->peer_addr.ip46));
1110 session->lcl_port = mp->lcl_port;
1111 vppcom_session_table_add_listener (mp->handle, session_index);
Dave Wallaceee45d412017-11-24 21:44:06 -05001112 session->state = STATE_LISTEN;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001113
Dave Wallaceee45d412017-11-24 21:44:06 -05001114 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001115 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
Dave Wallaceee45d412017-11-24 21:44:06 -05001116 getpid (), mp->handle, mp->context);
1117done_unlock:
Dave Wallace543852a2017-08-03 02:11:34 -04001118 clib_spinlock_unlock (&vcm->sessions_lockp);
1119}
1120
1121static void
1122vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1123{
Dave Wallace4878cbe2017-11-21 03:45:09 -05001124 if (mp->retval)
Dave Wallace048b1d62018-01-03 22:24:41 -05001125 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001126 getpid (), mp->context, format_api_error,
1127 ntohl (mp->retval));
Dave Wallace543852a2017-08-03 02:11:34 -04001128
Dave Wallace4878cbe2017-11-21 03:45:09 -05001129 else if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001130 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1131 getpid (), mp->context);
Dave Wallace543852a2017-08-03 02:11:34 -04001132}
1133
1134u8 *
1135format_ip4_address (u8 * s, va_list * args)
1136{
1137 u8 *a = va_arg (*args, u8 *);
1138 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1139}
1140
1141u8 *
1142format_ip6_address (u8 * s, va_list * args)
1143{
1144 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1145 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1146
1147 i_max_n_zero = ARRAY_LEN (a->as_u16);
1148 max_n_zeros = 0;
1149 i_first_zero = i_max_n_zero;
1150 n_zeros = 0;
1151 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1152 {
1153 u32 is_zero = a->as_u16[i] == 0;
1154 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1155 {
1156 i_first_zero = i;
1157 n_zeros = 0;
1158 }
1159 n_zeros += is_zero;
1160 if ((!is_zero && n_zeros > max_n_zeros)
1161 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1162 {
1163 i_max_n_zero = i_first_zero;
1164 max_n_zeros = n_zeros;
1165 i_first_zero = ARRAY_LEN (a->as_u16);
1166 n_zeros = 0;
1167 }
1168 }
1169
1170 last_double_colon = 0;
1171 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1172 {
1173 if (i == i_max_n_zero && max_n_zeros > 1)
1174 {
1175 s = format (s, "::");
1176 i += max_n_zeros - 1;
1177 last_double_colon = 1;
1178 }
1179 else
1180 {
1181 s = format (s, "%s%x",
1182 (last_double_colon || i == 0) ? "" : ":",
1183 clib_net_to_host_u16 (a->as_u16[i]));
1184 last_double_colon = 0;
1185 }
1186 }
1187
1188 return s;
1189}
1190
1191/* Format an IP46 address. */
1192u8 *
1193format_ip46_address (u8 * s, va_list * args)
1194{
1195 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1196 ip46_type_t type = va_arg (*args, ip46_type_t);
1197 int is_ip4 = 1;
1198
1199 switch (type)
1200 {
1201 case IP46_TYPE_ANY:
1202 is_ip4 = ip46_address_is_ip4 (ip46);
1203 break;
1204 case IP46_TYPE_IP4:
1205 is_ip4 = 1;
1206 break;
1207 case IP46_TYPE_IP6:
1208 is_ip4 = 0;
1209 break;
1210 }
1211
1212 return is_ip4 ?
1213 format (s, "%U", format_ip4_address, &ip46->ip4) :
1214 format (s, "%U", format_ip6_address, &ip46->ip6);
1215}
1216
Dave Wallace60caa062017-11-10 17:07:13 -05001217static inline void
Florin Coras50e8bdb2017-11-27 10:37:05 -08001218vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
Dave Wallace60caa062017-11-10 17:07:13 -05001219{
1220 vl_api_accept_session_reply_t *rmp;
1221
1222 rmp = vl_msg_api_alloc (sizeof (*rmp));
1223 memset (rmp, 0, sizeof (*rmp));
1224 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1225 rmp->retval = htonl (retval);
Dave Wallaced2931962017-11-25 04:17:39 -05001226 rmp->context = context;
Dave Wallace60caa062017-11-10 17:07:13 -05001227 rmp->handle = handle;
1228 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1229}
1230
Dave Wallace543852a2017-08-03 02:11:34 -04001231static void
1232vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1233{
Dave Wallace543852a2017-08-03 02:11:34 -04001234 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Corasdcf55ce2017-11-16 15:32:50 -08001235 session_t *session, *listen_session;
Dave Wallace543852a2017-08-03 02:11:34 -04001236 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001237
Dave Wallace60caa062017-11-10 17:07:13 -05001238 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001239 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1240 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001241 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
Dave Wallaced2931962017-11-25 04:17:39 -05001242 vppcom_send_accept_session_reply (mp->handle, mp->context,
Florin Corasdcf55ce2017-11-16 15:32:50 -08001243 VNET_API_ERROR_QUEUE_FULL);
1244 clib_spinlock_unlock (&vcm->sessions_lockp);
1245 return;
1246 }
1247
1248 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1249 if (!listen_session)
1250 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001251 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1252 "unknown vpp listener handle %llx",
1253 getpid (), mp->listener_handle);
Dave Wallace60caa062017-11-10 17:07:13 -05001254 clib_spinlock_unlock (&vcm->sessions_lockp);
1255 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001256 }
1257
Dave Wallace543852a2017-08-03 02:11:34 -04001258 /* Allocate local session and set it up */
1259 pool_get (vcm->sessions, session);
1260 memset (session, 0, sizeof (*session));
1261 session_index = session - vcm->sessions;
1262
1263 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1264 rx_fifo->client_session_index = session_index;
1265 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1266 tx_fifo->client_session_index = session_index;
1267
Dave Wallace60caa062017-11-10 17:07:13 -05001268 session->vpp_handle = mp->handle;
Dave Wallaced2931962017-11-25 04:17:39 -05001269 session->client_context = mp->context;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001270 session->rx_fifo = rx_fifo;
1271 session->tx_fifo = tx_fifo;
Dave Wallace33e002b2017-09-06 01:20:02 -04001272 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
Florin Corase86a8ed2018-01-05 03:20:25 -08001273 svm_queue_t *);
Dave Wallace543852a2017-08-03 02:11:34 -04001274 session->state = STATE_ACCEPT;
Stevenac1f96d2017-10-24 16:03:58 -07001275 session->peer_port = mp->port;
Dave Wallace35830af2017-10-09 01:43:42 -04001276 session->peer_addr.is_ip4 = mp->is_ip4;
1277 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1278 sizeof (session->peer_addr.ip46));
Dave Wallace543852a2017-08-03 02:11:34 -04001279
1280 /* Add it to lookup table */
1281 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001282 session->lcl_port = listen_session->lcl_port;
1283 session->lcl_addr = listen_session->lcl_addr;
Dave Wallace227867f2017-11-13 21:21:53 -05001284
1285 /* TBD: move client_session_index_fifo into listener session */
Dave Wallace543852a2017-08-03 02:11:34 -04001286 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Florin Corasdcf55ce2017-11-16 15:32:50 -08001287
Dave Wallace60caa062017-11-10 17:07:13 -05001288 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001289 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
Florin Coras50e8bdb2017-11-27 10:37:05 -08001290 "request from %s address %U port %d queue %p!", getpid (),
Dave Wallaced2931962017-11-25 04:17:39 -05001291 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1292 format_ip46_address, &mp->ip, mp->is_ip4,
Florin Coras50e8bdb2017-11-27 10:37:05 -08001293 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001294
1295 if (VPPCOM_DEBUG > 0)
1296 {
1297 session->elog_track.name =
1298 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1299 session_index, 0);
1300 elog_track_register (&vcm->elog_main, &session->elog_track);
1301
1302 if (session->peer_addr.is_ip4)
1303 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001304 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001305 ELOG_TYPE_DECLARE (e) =
1306 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001307 .format =
1308 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1309 .format_args = "i8i1i1i1i1i2",
1310 };
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001311
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001312 CLIB_PACKED (struct {
1313 u64 handle; //8
1314 u8 addr[4]; //4
1315 u16 port; //2
1316 }) * ed;
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001317
1318 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1319
1320 ed->handle = mp->handle;
1321 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1322 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1323 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1324 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08001325 ed->port = clib_net_to_host_u16 (session->peer_port);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001326 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001327 }
1328 else
1329 {
1330 clib_warning ("ip6");
1331 }
1332 }
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001333 clib_spinlock_unlock (&vcm->sessions_lockp);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001334
Dave Wallace60caa062017-11-10 17:07:13 -05001335}
1336
1337static void
Dave Wallaceee45d412017-11-24 21:44:06 -05001338vppcom_send_connect_session_reply (session_t * session, u32 session_index,
Dave Wallaced2931962017-11-25 04:17:39 -05001339 u64 vpp_handle, u32 context, int retval)
Dave Wallace60caa062017-11-10 17:07:13 -05001340{
1341 vl_api_connect_session_reply_t *rmp;
1342 u32 len;
Florin Corase86a8ed2018-01-05 03:20:25 -08001343 svm_queue_t *client_q;
Dave Wallace60caa062017-11-10 17:07:13 -05001344
Dave Wallace543852a2017-08-03 02:11:34 -04001345 rmp = vl_msg_api_alloc (sizeof (*rmp));
1346 memset (rmp, 0, sizeof (*rmp));
Dave Wallace60caa062017-11-10 17:07:13 -05001347 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
Dave Wallaced2931962017-11-25 04:17:39 -05001348
1349 if (!session)
1350 {
1351 rmp->context = context;
1352 rmp->handle = vpp_handle;
1353 rmp->retval = htonl (retval);
1354 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1355 return;
1356 }
1357
Dave Wallace227867f2017-11-13 21:21:53 -05001358 rmp->context = session->client_context;
Dave Wallace60caa062017-11-10 17:07:13 -05001359 rmp->retval = htonl (retval);
Dave Wallace227867f2017-11-13 21:21:53 -05001360 rmp->handle = session->vpp_handle;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001361 rmp->server_rx_fifo = pointer_to_uword (session->rx_fifo);
1362 rmp->server_tx_fifo = pointer_to_uword (session->tx_fifo);
Dave Wallace227867f2017-11-13 21:21:53 -05001363 rmp->vpp_event_queue_address = pointer_to_uword (session->vpp_event_queue);
1364 rmp->segment_size = vcm->cfg.segment_size;
1365 len = vec_len (session->segment_name);
1366 rmp->segment_name_length = clib_min (len, sizeof (rmp->segment_name));
1367 clib_memcpy (rmp->segment_name, session->segment_name,
1368 rmp->segment_name_length - 1);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001369 clib_memcpy (rmp->lcl_ip, session->peer_addr.ip46.as_u8,
Dave Wallace227867f2017-11-13 21:21:53 -05001370 sizeof (rmp->lcl_ip));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001371 rmp->is_ip4 = session->peer_addr.is_ip4;
1372 rmp->lcl_port = session->peer_port;
Florin Corase86a8ed2018-01-05 03:20:25 -08001373 client_q = uword_to_pointer (session->client_queue_address, svm_queue_t *);
Dave Wallace60caa062017-11-10 17:07:13 -05001374 ASSERT (client_q);
1375 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
Dave Wallace543852a2017-08-03 02:11:34 -04001376}
1377
1378/*
1379 * Acting as server for redirected connect requests
1380 */
1381static void
1382vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1383{
Dave Wallace543852a2017-08-03 02:11:34 -04001384 u32 session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001385 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001386
Dave Wallacef7f809c2017-10-03 01:48:42 -04001387 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001388 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1389 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05001390 clib_spinlock_unlock (&vcm->sessions_lockp);
1391
Dave Wallace543852a2017-08-03 02:11:34 -04001392 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001393 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
Dave Wallace4878cbe2017-11-21 03:45:09 -05001394
Dave Wallaced2931962017-11-25 04:17:39 -05001395 /* TBD: Fix api to include vpp handle */
1396 vppcom_send_connect_session_reply (0 /* session */ , 0 /* sid */ ,
1397 0 /* handle */ , mp->context,
1398 VNET_API_ERROR_QUEUE_FULL);
Dave Wallace60caa062017-11-10 17:07:13 -05001399 return;
Dave Wallace543852a2017-08-03 02:11:34 -04001400 }
1401
Dave Wallace543852a2017-08-03 02:11:34 -04001402 pool_get (vcm->sessions, session);
1403 memset (session, 0, sizeof (*session));
1404 session_index = session - vcm->sessions;
1405
Dave Wallace60caa062017-11-10 17:07:13 -05001406 session->client_context = mp->context;
1407 session->vpp_handle = session_index;
Dave Wallace543852a2017-08-03 02:11:34 -04001408 session->client_queue_address = mp->client_queue_address;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001409 session->lcl_port = mp->port;
1410 session->lcl_addr.is_ip4 = mp->is_ip4;
1411 clib_memcpy (&session->lcl_addr.ip46, mp->ip,
1412 sizeof (session->lcl_addr.ip46));
1413
1414 /* TBD: missing peer info in api msg.
1415 */
Dave Wallace35830af2017-10-09 01:43:42 -04001416 session->peer_addr.is_ip4 = mp->is_ip4;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001417 ASSERT (session->lcl_addr.is_ip4 == session->peer_addr.is_ip4);
Dave Wallace6d5c4cd2017-08-15 16:56:29 -04001418
Dave Wallace543852a2017-08-03 02:11:34 -04001419 session->state = STATE_ACCEPT;
Dave Wallace543852a2017-08-03 02:11:34 -04001420 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
Dave Wallace2e005bb2017-11-07 01:21:39 -05001421 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001422 clib_warning ("VCL<%d>: sid %u: Got a cut-thru connect request! "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001423 "clib_fifo_elts %u!\n", getpid (), session_index,
1424 clib_fifo_elts (vcm->client_session_index_fifo));
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001425
1426 if (VPPCOM_DEBUG > 0)
1427 {
1428 session->elog_track.name =
1429 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1430 session_index, 0);
1431 elog_track_register (&vcm->elog_main, &session->elog_track);
1432
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001433 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001434 ELOG_TYPE_DECLARE (e) =
1435 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001436 .format = "cut-thru-connect:S:%d clib_fifo_elts:%d",
1437 .format_args = "i4i4",
1438 };
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001439
1440 struct
1441 {
1442 u32 data[2];
1443 } *ed;
1444
1445 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1446
1447 ed->data[0] = session_index;
1448 ed->data[1] = clib_fifo_elts (vcm->client_session_index_fifo);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001449 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001450 }
1451
Dave Wallacef7f809c2017-10-03 01:48:42 -04001452 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace543852a2017-08-03 02:11:34 -04001453}
1454
1455static void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001456vppcom_send_bind_sock (session_t * session, u32 session_index)
Dave Wallace543852a2017-08-03 02:11:34 -04001457{
Dave Wallace543852a2017-08-03 02:11:34 -04001458 vl_api_bind_sock_t *bmp;
1459
1460 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace543852a2017-08-03 02:11:34 -04001461 bmp = vl_msg_api_alloc (sizeof (*bmp));
1462 memset (bmp, 0, sizeof (*bmp));
1463
1464 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1465 bmp->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001466 bmp->context = session_index;
Dave Wallace35830af2017-10-09 01:43:42 -04001467 bmp->is_ip4 = session->lcl_addr.is_ip4;
1468 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
Stevenac1f96d2017-10-24 16:03:58 -07001469 bmp->port = session->lcl_port;
Dave Wallace543852a2017-08-03 02:11:34 -04001470 bmp->proto = session->proto;
1471 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1472 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1473}
1474
1475static void
Dave Wallace4878cbe2017-11-21 03:45:09 -05001476vppcom_send_unbind_sock (u64 vpp_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001477{
Dave Wallace543852a2017-08-03 02:11:34 -04001478 vl_api_unbind_sock_t *ump;
Dave Wallace543852a2017-08-03 02:11:34 -04001479
1480 ump = vl_msg_api_alloc (sizeof (*ump));
1481 memset (ump, 0, sizeof (*ump));
1482
1483 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1484 ump->client_index = vcm->my_client_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001485 ump->handle = vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001486 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1487}
1488
1489static int
Dave Wallace543852a2017-08-03 02:11:34 -04001490vppcom_session_unbind (u32 session_index)
1491{
Dave Wallace4878cbe2017-11-21 03:45:09 -05001492 session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001493 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001494 u64 vpp_handle;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001495 elog_track_t session_elog_track;
Dave Wallace543852a2017-08-03 02:11:34 -04001496
Dave Wallace4878cbe2017-11-21 03:45:09 -05001497 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1498
1499 vpp_handle = session->vpp_handle;
1500 vppcom_session_table_del_listener (vpp_handle);
1501 session->vpp_handle = ~0;
1502 session->state = STATE_DISCONNECT;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001503 session_elog_track = session->elog_track;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001504
Dave Wallace543852a2017-08-03 02:11:34 -04001505 clib_spinlock_unlock (&vcm->sessions_lockp);
1506
Dave Wallace4878cbe2017-11-21 03:45:09 -05001507 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05001508 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05001509 "sending unbind msg! new state 0x%x (%s)",
1510 getpid (), vpp_handle, session_index,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001511 STATE_DISCONNECT,
1512 vppcom_session_state_str (STATE_DISCONNECT));
Dave Wallace4878cbe2017-11-21 03:45:09 -05001513
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001514 if (VPPCOM_DEBUG > 0)
1515 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001516 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001517 ELOG_TYPE_DECLARE (e) =
1518 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001519 .format = "unbind: handle:%x",
1520 .format_args = "i8",
1521 };
1522
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001523 struct
1524 {
1525 u64 handle;
1526 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001527
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001528 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001529 ed->handle = vpp_handle;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08001530 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001531 }
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001532
Dave Wallace4878cbe2017-11-21 03:45:09 -05001533 vppcom_send_unbind_sock (vpp_handle);
1534
1535done:
1536 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001537}
1538
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001539static inline int
Dave Wallace543852a2017-08-03 02:11:34 -04001540vppcom_session_disconnect (u32 session_index)
1541{
Dave Wallace543852a2017-08-03 02:11:34 -04001542 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001543 session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001544 u64 vpp_handle;
1545 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04001546
Dave Wallace4878cbe2017-11-21 03:45:09 -05001547 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1548
1549 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001550 state = session->state;
1551 clib_spinlock_unlock (&vcm->sessions_lockp);
1552
1553 if (VPPCOM_DEBUG > 1)
Dave Wallace543852a2017-08-03 02:11:34 -04001554 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001555 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u "
1556 "state 0x%x (%s)",
Dave Wallace4878cbe2017-11-21 03:45:09 -05001557 getpid (), vpp_handle, session_index,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001558 state, vppcom_session_state_str (state));
Dave Wallace543852a2017-08-03 02:11:34 -04001559 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001560
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001561 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001562 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001563 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001564 "Cannot disconnect a listen socket!",
1565 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001566 rv = VPPCOM_EBADFD;
1567 goto done;
1568 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001569
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001570 /* The peer has already initiated the close,
1571 * so send the disconnect session reply.
Dave Wallace4878cbe2017-11-21 03:45:09 -05001572 */
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001573 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001574 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001575 //XXX alagalah - Check and drain here?
1576 vppcom_send_disconnect_session_reply (vpp_handle,
1577 session_index, 0 /* rv */ );
1578 if (VPPCOM_DEBUG > 1)
1579 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1580 "sending disconnect REPLY...",
1581 getpid (), vpp_handle, session_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001582 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001583
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001584 /* Otherwise, send a disconnect session msg...
Dave Wallace4878cbe2017-11-21 03:45:09 -05001585 */
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001586 else
Dave Wallace227867f2017-11-13 21:21:53 -05001587 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001588 if (VPPCOM_DEBUG > 1)
1589 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1590 "sending disconnect...",
1591 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001592
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001593 vppcom_send_disconnect_session (vpp_handle, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -05001594 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001595
Dave Wallace4878cbe2017-11-21 03:45:09 -05001596done:
1597 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001598}
1599
1600#define foreach_sock_msg \
1601_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1602_(BIND_SOCK_REPLY, bind_sock_reply) \
1603_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1604_(ACCEPT_SESSION, accept_session) \
1605_(CONNECT_SOCK, connect_sock) \
Dave Wallace33e002b2017-09-06 01:20:02 -04001606_(CONNECT_SESSION_REPLY, connect_session_reply) \
Dave Wallace543852a2017-08-03 02:11:34 -04001607_(DISCONNECT_SESSION, disconnect_session) \
1608_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1609_(RESET_SESSION, reset_session) \
1610_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1611_(APPLICATION_DETACH_REPLY, application_detach_reply) \
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001612_(MAP_ANOTHER_SEGMENT, map_another_segment) \
1613_(UNMAP_SEGMENT, unmap_segment)
Dave Wallace543852a2017-08-03 02:11:34 -04001614
1615static void
1616vppcom_api_hookup (void)
1617{
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001618#define _(N, n) \
Dave Wallace543852a2017-08-03 02:11:34 -04001619 vl_msg_api_set_handlers(VL_API_##N, #n, \
1620 vl_api_##n##_t_handler, \
1621 vl_noop_handler, \
1622 vl_api_##n##_t_endian, \
1623 vl_api_##n##_t_print, \
1624 sizeof(vl_api_##n##_t), 1);
1625 foreach_sock_msg;
1626#undef _
1627}
1628
1629static void
1630vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1631{
1632 ASSERT (vcl_cfg);
1633
1634 vcl_cfg->heapsize = (256ULL << 20);
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001635 vcl_cfg->vpp_api_q_length = 1024;
Dave Wallace543852a2017-08-03 02:11:34 -04001636 vcl_cfg->segment_baseva = 0x200000000ULL;
1637 vcl_cfg->segment_size = (256 << 20);
1638 vcl_cfg->add_segment_size = (128 << 20);
1639 vcl_cfg->preallocated_fifo_pairs = 8;
1640 vcl_cfg->rx_fifo_size = (1 << 20);
1641 vcl_cfg->tx_fifo_size = (1 << 20);
1642 vcl_cfg->event_queue_size = 2048;
1643 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1644 vcl_cfg->app_timeout = 10 * 60.0;
1645 vcl_cfg->session_timeout = 10 * 60.0;
1646 vcl_cfg->accept_timeout = 60.0;
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001647 vcl_cfg->event_ring_size = (128 << 10);
1648 vcl_cfg->event_log_path = "/dev/shm";
Dave Wallace543852a2017-08-03 02:11:34 -04001649}
1650
1651static void
1652vppcom_cfg_heapsize (char *conf_fname)
1653{
Dave Wallace543852a2017-08-03 02:11:34 -04001654 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1655 FILE *fp;
1656 char inbuf[4096];
1657 int argc = 1;
1658 char **argv = NULL;
1659 char *arg = NULL;
1660 char *p;
1661 int i;
1662 u8 *sizep;
1663 u32 size;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001664 void *vcl_mem;
1665 void *heap;
Dave Wallace543852a2017-08-03 02:11:34 -04001666
1667 fp = fopen (conf_fname, "r");
1668 if (fp == NULL)
1669 {
1670 if (VPPCOM_DEBUG > 0)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001671 clib_warning ("VCL<%d>: using default heapsize %lld (0x%llx)",
1672 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001673 goto defaulted;
1674 }
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001675
Dave Wallace543852a2017-08-03 02:11:34 -04001676 argv = calloc (1, sizeof (char *));
1677 if (argv == NULL)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001678 {
1679 if (VPPCOM_DEBUG > 0)
1680 clib_warning ("VCL<%d>: calloc failed, using default "
1681 "heapsize %lld (0x%llx)",
1682 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1683 goto defaulted;
1684 }
Dave Wallace543852a2017-08-03 02:11:34 -04001685
1686 while (1)
1687 {
1688 if (fgets (inbuf, 4096, fp) == 0)
1689 break;
1690 p = strtok (inbuf, " \t\n");
1691 while (p != NULL)
1692 {
1693 if (*p == '#')
1694 break;
1695 argc++;
1696 char **tmp = realloc (argv, argc * sizeof (char *));
1697 if (tmp == NULL)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001698 {
1699 if (VPPCOM_DEBUG > 0)
1700 clib_warning ("VCL<%d>: realloc failed, "
1701 "using default heapsize %lld (0x%llx)",
1702 getpid (), vcl_cfg->heapsize,
1703 vcl_cfg->heapsize);
1704 goto defaulted;
1705 }
Dave Wallace543852a2017-08-03 02:11:34 -04001706 argv = tmp;
1707 arg = strndup (p, 1024);
1708 if (arg == NULL)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001709 {
1710 if (VPPCOM_DEBUG > 0)
1711 clib_warning ("VCL<%d>: strndup failed, "
1712 "using default heapsize %lld (0x%llx)",
1713 getpid (), vcl_cfg->heapsize,
1714 vcl_cfg->heapsize);
1715 goto defaulted;
1716 }
Dave Wallace543852a2017-08-03 02:11:34 -04001717 argv[argc - 1] = arg;
1718 p = strtok (NULL, " \t\n");
1719 }
1720 }
1721
1722 fclose (fp);
Chris Lukeab7b8d92017-09-07 07:40:13 -04001723 fp = NULL;
Dave Wallace543852a2017-08-03 02:11:34 -04001724
1725 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1726 if (tmp == NULL)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001727 {
1728 if (VPPCOM_DEBUG > 0)
1729 clib_warning ("VCL<%d>: realloc failed, "
1730 "using default heapsize %lld (0x%llx)",
1731 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1732 goto defaulted;
1733 }
Dave Wallace543852a2017-08-03 02:11:34 -04001734 argv = tmp;
1735 argv[argc] = NULL;
1736
1737 /*
1738 * Look for and parse the "heapsize" config parameter.
1739 * Manual since none of the clib infra has been bootstrapped yet.
1740 *
1741 * Format: heapsize <nn>[mM][gG]
1742 */
1743
1744 for (i = 1; i < (argc - 1); i++)
1745 {
1746 if (!strncmp (argv[i], "heapsize", 8))
1747 {
1748 sizep = (u8 *) argv[i + 1];
1749 size = 0;
1750 while (*sizep >= '0' && *sizep <= '9')
1751 {
1752 size *= 10;
1753 size += *sizep++ - '0';
1754 }
1755 if (size == 0)
1756 {
1757 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001758 clib_warning ("VCL<%d>: parse error '%s %s', "
Dave Wallace543852a2017-08-03 02:11:34 -04001759 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001760 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001761 vcl_cfg->heapsize, vcl_cfg->heapsize);
1762 goto defaulted;
1763 }
1764
1765 if (*sizep == 'g' || *sizep == 'G')
1766 vcl_cfg->heapsize = size << 30;
1767 else if (*sizep == 'm' || *sizep == 'M')
1768 vcl_cfg->heapsize = size << 20;
1769 else
1770 {
1771 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001772 clib_warning ("VCL<%d>: parse error '%s %s', "
Dave Wallace543852a2017-08-03 02:11:34 -04001773 "using default heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001774 getpid (), argv[i], argv[i + 1],
Dave Wallace543852a2017-08-03 02:11:34 -04001775 vcl_cfg->heapsize, vcl_cfg->heapsize);
1776 goto defaulted;
1777 }
1778 }
1779 }
1780
1781defaulted:
Chris Lukeab7b8d92017-09-07 07:40:13 -04001782 if (fp != NULL)
1783 fclose (fp);
1784 if (argv != NULL)
1785 free (argv);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001786
Dave Wallace2e005bb2017-11-07 01:21:39 -05001787 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1788 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
Steven0cdd5bd2017-11-08 14:14:45 -08001789 if (vcl_mem == MAP_FAILED)
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001790 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001791 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001792 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1793 "-1, 0) failed!",
1794 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001795 ASSERT (vcl_mem != MAP_FAILED);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001796 return;
1797 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05001798 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1799 if (!heap)
Dave Wallace2e005bb2017-11-07 01:21:39 -05001800 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001801 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001802 ASSERT (heap);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001803 return;
Dave Wallace2e005bb2017-11-07 01:21:39 -05001804 }
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001805 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1806 if (!vcl_mem)
1807 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001808 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001809 ASSERT (vcl_mem);
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001810 return;
1811 }
1812
1813 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1814 vcm = vcl_mem;
1815
1816 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001817 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
Dave Wallacee7fa24e2017-11-07 13:07:44 -05001818 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
Dave Wallace543852a2017-08-03 02:11:34 -04001819}
1820
1821static void
1822vppcom_cfg_read (char *conf_fname)
1823{
Dave Wallace543852a2017-08-03 02:11:34 -04001824 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1825 int fd;
1826 unformat_input_t _input, *input = &_input;
1827 unformat_input_t _line_input, *line_input = &_line_input;
1828 u8 vc_cfg_input = 0;
1829 u8 *chroot_path;
1830 struct stat s;
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001831 u32 uid, gid, q_len;
Dave Wallace543852a2017-08-03 02:11:34 -04001832
1833 fd = open (conf_fname, O_RDONLY);
1834 if (fd < 0)
1835 {
1836 if (VPPCOM_DEBUG > 0)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001837 clib_warning ("VCL<%d>: using default configuration.",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001838 getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001839 goto file_done;
1840 }
1841
1842 if (fstat (fd, &s) < 0)
1843 {
1844 if (VPPCOM_DEBUG > 0)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001845 clib_warning ("VCL<%d>: failed to stat `%s', "
1846 "using default configuration", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001847 goto file_done;
1848 }
1849
1850 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1851 {
1852 if (VPPCOM_DEBUG > 0)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001853 clib_warning ("VCL<%d>: not a regular file `%s', "
1854 "using default configuration", getpid (), conf_fname);
Dave Wallace543852a2017-08-03 02:11:34 -04001855 goto file_done;
1856 }
1857
Dave Barach59b25652017-09-10 15:04:27 -04001858 unformat_init_clib_file (input, fd);
Dave Wallace543852a2017-08-03 02:11:34 -04001859
1860 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1861 {
Chris Lukeb2bcad62017-09-18 08:51:22 -04001862 (void) unformat_user (input, unformat_line_input, line_input);
Dave Wallace543852a2017-08-03 02:11:34 -04001863 unformat_skip_white_space (line_input);
1864
Dave Wallace8af20542017-10-26 03:29:30 -04001865 if (unformat (line_input, "vcl {"))
Dave Wallace543852a2017-08-03 02:11:34 -04001866 {
1867 vc_cfg_input = 1;
1868 continue;
1869 }
1870
1871 if (vc_cfg_input)
1872 {
1873 if (unformat (line_input, "heapsize %s", &chroot_path))
1874 {
1875 vec_terminate_c_string (chroot_path);
1876 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001877 clib_warning ("VCL<%d>: configured heapsize %s, "
Dave Wallace543852a2017-08-03 02:11:34 -04001878 "actual heapsize %lld (0x%llx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001879 getpid (), chroot_path, vcl_cfg->heapsize,
Dave Wallace543852a2017-08-03 02:11:34 -04001880 vcl_cfg->heapsize);
1881 vec_free (chroot_path);
1882 }
1883 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1884 {
1885 vec_terminate_c_string (chroot_path);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001886 if (vcl_cfg->vpp_api_filename)
1887 vec_free (vcl_cfg->vpp_api_filename);
1888 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1889 chroot_path, 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001890 vl_set_memory_root_path ((char *) chroot_path);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001891
Dave Wallace543852a2017-08-03 02:11:34 -04001892 if (VPPCOM_DEBUG > 0)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05001893 clib_warning ("VCL<%d>: configured api-prefix (%s) and "
1894 "api filename (%s)", getpid (), chroot_path,
1895 vcl_cfg->vpp_api_filename);
Dave Wallace543852a2017-08-03 02:11:34 -04001896 chroot_path = 0; /* Don't vec_free() it! */
1897 }
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001898 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1899 {
1900 if (q_len < vcl_cfg->vpp_api_q_length)
1901 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001902 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001903 "(%u) is too small! Using default: %u ",
1904 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1905 }
1906 else
1907 {
1908 vcl_cfg->vpp_api_q_length = q_len;
1909
1910 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001911 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
Dave Wallacec8f1ee62017-11-29 22:46:32 -05001912 getpid (), vcl_cfg->vpp_api_q_length);
1913 }
1914 }
Dave Wallace543852a2017-08-03 02:11:34 -04001915 else if (unformat (line_input, "uid %d", &uid))
1916 {
1917 vl_set_memory_uid (uid);
1918 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001919 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
Dave Wallace543852a2017-08-03 02:11:34 -04001920 }
1921 else if (unformat (line_input, "gid %d", &gid))
1922 {
1923 vl_set_memory_gid (gid);
1924 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001925 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
Dave Wallace543852a2017-08-03 02:11:34 -04001926 }
Dave Wallace8af20542017-10-26 03:29:30 -04001927 else if (unformat (line_input, "segment-baseva 0x%lx",
Dave Wallace543852a2017-08-03 02:11:34 -04001928 &vcl_cfg->segment_baseva))
1929 {
1930 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001931 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001932 getpid (), vcl_cfg->segment_baseva);
Dave Wallace543852a2017-08-03 02:11:34 -04001933 }
1934 else if (unformat (line_input, "segment-size 0x%lx",
1935 &vcl_cfg->segment_size))
1936 {
1937 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001938 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001939 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001940 vcl_cfg->segment_size);
1941 }
1942 else if (unformat (line_input, "segment-size %ld",
1943 &vcl_cfg->segment_size))
1944 {
1945 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001946 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001947 getpid (), vcl_cfg->segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001948 vcl_cfg->segment_size);
1949 }
1950 else if (unformat (line_input, "add-segment-size 0x%lx",
1951 &vcl_cfg->add_segment_size))
1952 {
1953 if (VPPCOM_DEBUG > 0)
1954 clib_warning
Dave Wallace048b1d62018-01-03 22:24:41 -05001955 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001956 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001957 vcl_cfg->add_segment_size);
1958 }
1959 else if (unformat (line_input, "add-segment-size %ld",
1960 &vcl_cfg->add_segment_size))
1961 {
1962 if (VPPCOM_DEBUG > 0)
1963 clib_warning
Dave Wallace048b1d62018-01-03 22:24:41 -05001964 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001965 getpid (), vcl_cfg->add_segment_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001966 vcl_cfg->add_segment_size);
1967 }
1968 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1969 &vcl_cfg->preallocated_fifo_pairs))
1970 {
1971 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001972 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
Dave Wallace2e005bb2017-11-07 01:21:39 -05001973 "%d (0x%x)", getpid (),
Dave Wallace543852a2017-08-03 02:11:34 -04001974 vcl_cfg->preallocated_fifo_pairs,
1975 vcl_cfg->preallocated_fifo_pairs);
1976 }
1977 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1978 &vcl_cfg->rx_fifo_size))
1979 {
1980 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001981 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001982 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001983 vcl_cfg->rx_fifo_size);
1984 }
1985 else if (unformat (line_input, "rx-fifo-size %ld",
1986 &vcl_cfg->rx_fifo_size))
1987 {
1988 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001989 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001990 getpid (), vcl_cfg->rx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001991 vcl_cfg->rx_fifo_size);
1992 }
1993 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1994 &vcl_cfg->tx_fifo_size))
1995 {
1996 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05001997 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05001998 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04001999 vcl_cfg->tx_fifo_size);
2000 }
2001 else if (unformat (line_input, "tx-fifo-size %ld",
2002 &vcl_cfg->tx_fifo_size))
2003 {
2004 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002005 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002006 getpid (), vcl_cfg->tx_fifo_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002007 vcl_cfg->tx_fifo_size);
2008 }
2009 else if (unformat (line_input, "event-queue-size 0x%lx",
2010 &vcl_cfg->event_queue_size))
2011 {
2012 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002013 clib_warning ("VCL<%d>: configured event_queue_size "
2014 "0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002015 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002016 vcl_cfg->event_queue_size);
2017 }
2018 else if (unformat (line_input, "event-queue-size %ld",
2019 &vcl_cfg->event_queue_size))
2020 {
2021 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002022 clib_warning ("VCL<%d>: configured event_queue_size "
2023 "%ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002024 getpid (), vcl_cfg->event_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002025 vcl_cfg->event_queue_size);
2026 }
2027 else if (unformat (line_input, "listen-queue-size 0x%lx",
2028 &vcl_cfg->listen_queue_size))
2029 {
2030 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002031 clib_warning ("VCL<%d>: configured listen_queue_size "
2032 "0x%lx (%ld)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002033 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002034 vcl_cfg->listen_queue_size);
2035 }
2036 else if (unformat (line_input, "listen-queue-size %ld",
2037 &vcl_cfg->listen_queue_size))
2038 {
2039 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002040 clib_warning ("VCL<%d>: configured listen_queue_size "
2041 "%ld (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002042 getpid (), vcl_cfg->listen_queue_size,
Dave Wallace543852a2017-08-03 02:11:34 -04002043 vcl_cfg->listen_queue_size);
2044 }
2045 else if (unformat (line_input, "app-timeout %f",
2046 &vcl_cfg->app_timeout))
2047 {
2048 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002049 clib_warning ("VCL<%d>: configured app_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002050 getpid (), vcl_cfg->app_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002051 }
2052 else if (unformat (line_input, "session-timeout %f",
2053 &vcl_cfg->session_timeout))
2054 {
2055 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002056 clib_warning ("VCL<%d>: configured session_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002057 getpid (), vcl_cfg->session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002058 }
2059 else if (unformat (line_input, "accept-timeout %f",
2060 &vcl_cfg->accept_timeout))
2061 {
2062 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002063 clib_warning ("VCL<%d>: configured accept_timeout %f",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002064 getpid (), vcl_cfg->accept_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002065 }
Dave Wallace774169b2017-11-01 20:07:40 -04002066 else if (unformat (line_input, "app-proxy-transport-tcp"))
Dave Wallace8af20542017-10-26 03:29:30 -04002067 {
Dave Wallace774169b2017-11-01 20:07:40 -04002068 vcl_cfg->app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002069 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002070 clib_warning ("VCL<%d>: configured "
2071 "app_proxy_transport_tcp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002072 getpid (), vcl_cfg->app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04002073 }
Dave Wallace774169b2017-11-01 20:07:40 -04002074 else if (unformat (line_input, "app-proxy-transport-udp"))
Dave Wallace8af20542017-10-26 03:29:30 -04002075 {
Dave Wallace774169b2017-11-01 20:07:40 -04002076 vcl_cfg->app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002077 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002078 clib_warning ("VCL<%d>: configured "
2079 "app_proxy_transport_udp (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002080 getpid (), vcl_cfg->app_proxy_transport_udp);
Dave Wallace8af20542017-10-26 03:29:30 -04002081 }
Dave Wallace774169b2017-11-01 20:07:40 -04002082 else if (unformat (line_input, "app-scope-local"))
Dave Wallace8af20542017-10-26 03:29:30 -04002083 {
Dave Wallace774169b2017-11-01 20:07:40 -04002084 vcl_cfg->app_scope_local = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002085 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002086 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002087 getpid (), vcl_cfg->app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04002088 }
2089 else if (unformat (line_input, "app-scope-global"))
2090 {
2091 vcl_cfg->app_scope_global = 1;
2092 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002093 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002094 getpid (), vcl_cfg->app_scope_global);
Dave Wallace8af20542017-10-26 03:29:30 -04002095 }
2096 else if (unformat (line_input, "namespace-secret %lu",
2097 &vcl_cfg->namespace_secret))
2098 {
2099 if (VPPCOM_DEBUG > 0)
2100 clib_warning
Dave Wallace048b1d62018-01-03 22:24:41 -05002101 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002102 getpid (), vcl_cfg->namespace_secret,
Dave Wallace8af20542017-10-26 03:29:30 -04002103 vcl_cfg->namespace_secret);
2104 }
2105 else if (unformat (line_input, "namespace-id %v",
2106 &vcl_cfg->namespace_id))
2107 {
2108 vl_api_application_attach_t *mp;
2109 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2110 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2111 if (nsid_vec_len > max_nsid_vec_len)
2112 {
2113 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2114 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002115 clib_warning ("VCL<%d>: configured namespace_id is "
2116 "too long, truncated to %d characters!",
2117 getpid (), max_nsid_vec_len);
Dave Wallace8af20542017-10-26 03:29:30 -04002118 }
2119
2120 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 clib_warning ("VCL<%d>: configured namespace_id %v",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002122 getpid (), vcl_cfg->namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04002123 }
Dave Wallace543852a2017-08-03 02:11:34 -04002124 else if (unformat (line_input, "}"))
2125 {
2126 vc_cfg_input = 0;
2127 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002128 clib_warning ("VCL<%d>: completed parsing vppcom config!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002129 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -04002130 goto input_done;
2131 }
2132 else
2133 {
2134 if (line_input->buffer[line_input->index] != '#')
2135 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002136 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
Dave Wallace2e005bb2017-11-07 01:21:39 -05002137 getpid (), (char *)
Dave Wallace543852a2017-08-03 02:11:34 -04002138 &line_input->buffer[line_input->index]);
2139 }
2140 }
2141 }
2142 }
2143
2144input_done:
2145 unformat_free (input);
2146
2147file_done:
Chris Lukeab7b8d92017-09-07 07:40:13 -04002148 if (fd >= 0)
Dave Wallace543852a2017-08-03 02:11:34 -04002149 close (fd);
2150}
2151
2152/*
2153 * VPPCOM Public API functions
2154 */
2155int
2156vppcom_app_create (char *app_name)
2157{
Dave Wallace543852a2017-08-03 02:11:34 -04002158 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2159 u8 *heap;
2160 mheap_t *h;
2161 int rv;
2162
2163 if (!vcm->init)
2164 {
2165 char *conf_fname;
Dave Wallace8af20542017-10-26 03:29:30 -04002166 char *env_var_str;
Dave Wallace543852a2017-08-03 02:11:34 -04002167
2168 vcm->init = 1;
Dave Wallace543852a2017-08-03 02:11:34 -04002169 vppcom_cfg_init (vcl_cfg);
Dave Wallace498b3a52017-11-09 13:00:34 -05002170 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2171 if (env_var_str)
2172 {
2173 u32 tmp;
2174 if (sscanf (env_var_str, "%u", &tmp) != 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002175 clib_warning ("VCL<%d>: Invalid debug level specified in "
Dave Wallace498b3a52017-11-09 13:00:34 -05002176 "the environment variable "
2177 VPPCOM_ENV_DEBUG
2178 " (%s)!\n", getpid (), env_var_str);
2179 else
2180 {
2181 vcm->debug = tmp;
Dave Wallace048b1d62018-01-03 22:24:41 -05002182 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
Dave Wallace498b3a52017-11-09 13:00:34 -05002183 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2184 }
2185 }
Dave Wallace8af20542017-10-26 03:29:30 -04002186 conf_fname = getenv (VPPCOM_ENV_CONF);
Dave Wallace543852a2017-08-03 02:11:34 -04002187 if (!conf_fname)
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05002188 conf_fname = VPPCOM_CONF_DEFAULT;
Dave Wallace543852a2017-08-03 02:11:34 -04002189 vppcom_cfg_heapsize (conf_fname);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05002190 vcl_cfg = &vcm->cfg;
Dave Wallace2e005bb2017-11-07 01:21:39 -05002191 clib_fifo_validate (vcm->client_session_index_fifo,
2192 vcm->cfg.listen_queue_size);
Dave Wallace543852a2017-08-03 02:11:34 -04002193 vppcom_cfg_read (conf_fname);
Dave Wallaceb5a86ee2018-02-16 18:26:11 -05002194
2195 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
2196 if (env_var_str)
2197 {
2198 if (vcl_cfg->vpp_api_filename)
2199 vec_free (vcl_cfg->vpp_api_filename);
2200 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
2201 env_var_str, 0);
2202 vl_set_memory_root_path ((char *) env_var_str);
2203
2204 if (VPPCOM_DEBUG > 0)
2205 clib_warning ("VCL<%d>: configured api prefix (%s) and "
2206 "filename (%s) from " VPPCOM_ENV_API_PREFIX "!",
2207 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
2208 }
2209
2210 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2211 if (env_var_str)
2212 {
2213 u64 tmp;
2214 if (sscanf (env_var_str, "%lu", &tmp) != 1)
2215 clib_warning ("VCL<%d>: Invalid namespace secret specified in "
2216 "the environment variable "
2217 VPPCOM_ENV_APP_NAMESPACE_SECRET
2218 " (%s)!\n", getpid (), env_var_str);
2219 else
2220 {
2221 vcm->cfg.namespace_secret = tmp;
2222 if (VPPCOM_DEBUG > 0)
2223 clib_warning ("VCL<%d>: configured namespace secret "
2224 "(%lu) from " VPPCOM_ENV_APP_NAMESPACE_ID "!",
2225 getpid (), vcm->cfg.namespace_secret);
2226 }
2227 }
Dave Wallace8af20542017-10-26 03:29:30 -04002228 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2229 if (env_var_str)
2230 {
2231 u32 ns_id_vec_len = strlen (env_var_str);
2232
2233 vec_reset_length (vcm->cfg.namespace_id);
2234 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2235 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2236
2237 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002238 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002239 VPPCOM_ENV_APP_NAMESPACE_ID
2240 "!", getpid (), vcm->cfg.namespace_id);
Dave Wallace8af20542017-10-26 03:29:30 -04002241 }
2242 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2243 if (env_var_str)
2244 {
2245 u64 tmp;
2246 if (sscanf (env_var_str, "%lu", &tmp) != 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05002247 clib_warning ("VCL<%d>: Invalid namespace secret specified in "
Dave Wallace8af20542017-10-26 03:29:30 -04002248 "the environment variable "
2249 VPPCOM_ENV_APP_NAMESPACE_SECRET
Dave Wallace2e005bb2017-11-07 01:21:39 -05002250 " (%s)!\n", getpid (), env_var_str);
Dave Wallace8af20542017-10-26 03:29:30 -04002251 else
2252 {
2253 vcm->cfg.namespace_secret = tmp;
2254 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002255 clib_warning ("VCL<%d>: configured namespace secret "
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002256 "(%lu) from "
2257 VPPCOM_ENV_APP_NAMESPACE_ID
2258 "!", getpid (), vcm->cfg.namespace_secret);
Dave Wallace8af20542017-10-26 03:29:30 -04002259 }
2260 }
Dave Wallace774169b2017-11-01 20:07:40 -04002261 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
Dave Wallace8af20542017-10-26 03:29:30 -04002262 {
Dave Wallace774169b2017-11-01 20:07:40 -04002263 vcm->cfg.app_proxy_transport_tcp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002264 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002265 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002266 "(%u) from "
2267 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
Dave Wallace048b1d62018-01-03 22:24:41 -05002268 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
Dave Wallace8af20542017-10-26 03:29:30 -04002269 }
Dave Wallace774169b2017-11-01 20:07:40 -04002270 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
Dave Wallace8af20542017-10-26 03:29:30 -04002271 {
Dave Wallace774169b2017-11-01 20:07:40 -04002272 vcm->cfg.app_proxy_transport_udp = 1;
Dave Wallace8af20542017-10-26 03:29:30 -04002273 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002274 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002275 "(%u) from "
2276 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
Dave Wallace048b1d62018-01-03 22:24:41 -05002277 "!", 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 "
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002284 VPPCOM_ENV_APP_SCOPE_LOCAL
2285 "!", getpid (), vcm->cfg.app_scope_local);
Dave Wallace774169b2017-11-01 20:07:40 -04002286 }
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 "
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002292 VPPCOM_ENV_APP_SCOPE_GLOBAL
2293 "!", getpid (), 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
Dave Wallacec04cbf12018-02-07 18:14:02 -05002405vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04002406{
Dave Wallace543852a2017-08-03 02:11:34 -04002407 session_t *session;
2408 u32 session_index;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002409 session_state_t state;
2410 elog_track_t session_elog_track;
Dave Wallace543852a2017-08-03 02:11:34 -04002411
2412 clib_spinlock_lock (&vcm->sessions_lockp);
2413 pool_get (vcm->sessions, session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002414 memset (session, 0, sizeof (*session));
Dave Wallace543852a2017-08-03 02:11:34 -04002415 session_index = session - vcm->sessions;
2416
Dave Wallace543852a2017-08-03 02:11:34 -04002417 session->proto = proto;
2418 session->state = STATE_START;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002419 state = session->state;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002420 session->vpp_handle = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002421
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002422 if (is_nonblocking)
2423 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2424 else
2425 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04002426
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002427 if (VPPCOM_DEBUG > 0)
2428 {
2429 session->elog_track.name =
2430 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2431 session_index, 0);
2432 elog_track_register (&vcm->elog_main, &session->elog_track);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002433 session_elog_track = session->elog_track;
2434 }
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002435
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002436 clib_spinlock_unlock (&vcm->sessions_lockp);
2437
2438 if (VPPCOM_DEBUG > 0)
2439 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
2440
2441 if (VPPCOM_DEBUG > 0)
2442 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002443 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002444 ELOG_TYPE_DECLARE (e) =
2445 {
Dave Wallacec04cbf12018-02-07 18:14:02 -05002446 .format = "session_create:proto:%d state:%d is_nonblocking:%d",
2447 .format_args = "i4i4i4",
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002448 };
2449
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002450 struct
2451 {
Dave Wallacec04cbf12018-02-07 18:14:02 -05002452 u32 data[3];
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002453 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002454
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002455 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2456 ed->data[0] = proto;
2457 ed->data[1] = state;
2458 ed->data[2] = is_nonblocking;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002459 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002460 }
2461
Dave Wallace543852a2017-08-03 02:11:34 -04002462 return (int) session_index;
2463}
2464
2465int
2466vppcom_session_close (uint32_t session_index)
2467{
Dave Wallace543852a2017-08-03 02:11:34 -04002468 session_t *session = 0;
2469 int rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002470 u8 is_vep;
2471 u8 is_vep_session;
2472 u32 next_sid;
2473 u32 vep_idx;
Dave Wallaceee45d412017-11-24 21:44:06 -05002474 u64 vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002475 uword *p;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002476 session_state_t state;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002477 elog_track_t session_elog_track;
Dave Wallace543852a2017-08-03 02:11:34 -04002478
Dave Wallace4878cbe2017-11-21 03:45:09 -05002479 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002480 is_vep = session->is_vep;
2481 is_vep_session = session->is_vep_session;
2482 next_sid = session->vep.next_sid;
2483 vep_idx = session->vep.vep_idx;
2484 state = session->state;
Dave Wallaceee45d412017-11-24 21:44:06 -05002485 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04002486 clib_spinlock_unlock (&vcm->sessions_lockp);
2487
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002488 /*
2489 * Why two if(VPPCOM_DEBUG) checks?
2490 *
2491 * Eventually all clib_warnings need their own way of being
2492 * logged and signalled (like severity) where event logging
2493 * is a separate debugging tool. It will make the separation
2494 * easier. ... parting is such sweet sorrow ...
2495 */
2496 if (VPPCOM_DEBUG > 0)
2497 {
2498 session_elog_track = session->elog_track;
2499 }
2500
Dave Wallace543852a2017-08-03 02:11:34 -04002501 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -05002502 {
2503 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -05002504 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2505 "closing epoll session...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002506 getpid (), session_index, session_index);
2507 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002508 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2509 "closing session...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002510 getpid (), vpp_handle, session_index);
2511 }
Dave Wallace543852a2017-08-03 02:11:34 -04002512
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002513 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04002514 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002515 while (next_sid != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04002516 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002517 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
Dave Wallaceee45d412017-11-24 21:44:06 -05002518 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002519 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2520 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2521 getpid (), vpp_handle, next_sid, vep_idx,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002522 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002523
Dave Wallace4878cbe2017-11-21 03:45:09 -05002524 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002525 next_sid = session->vep.next_sid;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002526 clib_spinlock_unlock (&vcm->sessions_lockp);
2527 }
2528 }
2529 else
2530 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002531 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002532 {
Dave Wallacef7f809c2017-10-03 01:48:42 -04002533 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2534 if ((VPPCOM_DEBUG > 0) && (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002535 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2536 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
Dave Wallaceee45d412017-11-24 21:44:06 -05002537 getpid (), vpp_handle, session_index,
2538 vep_idx, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002539 }
2540
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002541 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002542 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002543 rv = vppcom_session_unbind (session_index);
2544 if (PREDICT_FALSE (rv < 0))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002545 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002546 if (VPPCOM_DEBUG > 0)
2547 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2548 "listener unbind failed! rv %d (%s)",
2549 getpid (), vpp_handle, session_index,
2550 rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05002551 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002552 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002553
2554 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002555 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05002556 rv = vppcom_session_disconnect (session_index);
2557 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002558 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002559 "session disconnect failed! rv %d (%s)",
2560 getpid (), vpp_handle, session_index,
2561 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04002562 }
Dave Wallace19481612017-09-15 18:47:44 -04002563 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002564
2565 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002566 vpp_handle = session->vpp_handle;
2567 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002568 {
Dave Wallaceee45d412017-11-24 21:44:06 -05002569 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002570 if (p)
Dave Wallaceee45d412017-11-24 21:44:06 -05002571 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002572 }
Dave Wallace543852a2017-08-03 02:11:34 -04002573 pool_put_index (vcm->sessions, session_index);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002574
Dave Wallace66cf6eb2017-10-26 02:51:07 -04002575 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002576
2577 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -05002578 {
2579 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -05002580 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -05002581 getpid (), session_index, session_index);
2582 else
Dave Wallace048b1d62018-01-03 22:24:41 -05002583 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
Dave Wallaceee45d412017-11-24 21:44:06 -05002584 getpid (), vpp_handle, session_index);
2585 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002586done:
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002587
2588 if (VPPCOM_DEBUG > 0)
2589 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002590 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002591 ELOG_TYPE_DECLARE (e) =
2592 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002593 .format = "session_close:rv:%d",
2594 .format_args = "i4",
2595 };
2596
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002597 struct
2598 {
2599 u32 data;
2600 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002601
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002602 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002603 ed->data = rv;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002604 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002605 }
2606
Dave Wallace543852a2017-08-03 02:11:34 -04002607 return rv;
2608}
2609
2610int
2611vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2612{
Dave Wallace543852a2017-08-03 02:11:34 -04002613 session_t *session = 0;
2614 int rv;
2615
2616 if (!ep || !ep->ip)
2617 return VPPCOM_EINVAL;
2618
Dave Wallace4878cbe2017-11-21 03:45:09 -05002619 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Dave Wallace543852a2017-08-03 02:11:34 -04002620
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002621 if (session->is_vep)
2622 {
2623 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002624 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2625 "bind to an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002626 rv = VPPCOM_EBADFD;
2627 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002628 }
2629
Dave Wallace35830af2017-10-09 01:43:42 -04002630 session->lcl_addr.is_ip4 = ep->is_ip4;
2631 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002632 session->lcl_port = ep->port;
2633
2634 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002635 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
Dave Wallaceee45d412017-11-24 21:44:06 -05002636 "port %u, proto %s", getpid (), session_index,
2637 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2638 format_ip46_address, &session->lcl_addr.ip46,
2639 session->lcl_addr.is_ip4,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002640 clib_net_to_host_u16 (session->lcl_port),
2641 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002642
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002643 if (VPPCOM_DEBUG > 0)
2644 {
2645 if (session->lcl_addr.is_ip4)
2646 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002647 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002648 ELOG_TYPE_DECLARE (e) =
2649 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002650 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2651 .format_args = "t1i1i1i1i1i2",
2652 .n_enum_strings = 2,
2653 .enum_strings = {"TCP", "UDP",},
2654 };
2655
2656 CLIB_PACKED (struct {
2657 u8 proto;
2658 u8 addr[4];
2659 u16 port;
2660 }) * ed;
2661
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002662 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2663 ed->proto = session->proto;
2664 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2665 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2666 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2667 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2668 ed->port = clib_net_to_host_u16 (session->lcl_port);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002669 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002670 }
2671 }
2672
Dave Wallace543852a2017-08-03 02:11:34 -04002673 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002674done:
2675 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002676}
2677
2678int
Dave Wallace33e002b2017-09-06 01:20:02 -04002679vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04002680{
Dave Wallace33e002b2017-09-06 01:20:02 -04002681 session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05002682 u64 listen_vpp_handle;
2683 int rv, retval;
Dave Wallace543852a2017-08-03 02:11:34 -04002684
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08002685 if (q_len == 0 || q_len == ~0)
2686 q_len = vcm->cfg.listen_queue_size;
2687
Dave Wallace4878cbe2017-11-21 03:45:09 -05002688 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002689
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002690 if (listen_session->is_vep)
2691 {
2692 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002693 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002694 "epoll session!", getpid (), listen_session_index);
2695 rv = VPPCOM_EBADFD;
2696 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002697 }
2698
Dave Wallaceee45d412017-11-24 21:44:06 -05002699 listen_vpp_handle = listen_session->vpp_handle;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002700 if (listen_session->state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04002701 {
2702 clib_spinlock_unlock (&vcm->sessions_lockp);
2703 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002704 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002705 "already in listen state!",
2706 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002707 rv = VPPCOM_OK;
2708 goto done;
Dave Wallacee695cb42017-11-02 22:04:42 -04002709 }
2710
Dave Wallace543852a2017-08-03 02:11:34 -04002711 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002712 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2713 "sid %u: sending bind request...",
Dave Wallaceee45d412017-11-24 21:44:06 -05002714 getpid (), listen_vpp_handle, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002715
Dave Wallace4878cbe2017-11-21 03:45:09 -05002716 vppcom_send_bind_sock (listen_session, listen_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04002717 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallaceee45d412017-11-24 21:44:06 -05002718 retval =
Dave Wallace33e002b2017-09-06 01:20:02 -04002719 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2720 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04002721
Dave Wallace4878cbe2017-11-21 03:45:09 -05002722 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002723 if (PREDICT_FALSE (retval))
2724 {
2725 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002726 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
Dave Wallaceee45d412017-11-24 21:44:06 -05002727 "returning %d (%s)", getpid (),
2728 listen_session->vpp_handle, listen_session_index,
2729 retval, vppcom_retval_str (retval));
2730 clib_spinlock_unlock (&vcm->sessions_lockp);
2731 rv = retval;
2732 goto done;
2733 }
2734
Dave Wallace543852a2017-08-03 02:11:34 -04002735 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002736 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002737done:
2738 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002739}
2740
2741int
2742vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05002743 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04002744{
Dave Wallace33e002b2017-09-06 01:20:02 -04002745 session_t *listen_session = 0;
2746 session_t *client_session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002747 u32 client_session_index = ~0;
Dave Wallace543852a2017-08-03 02:11:34 -04002748 int rv;
2749 f64 wait_for;
Dave Wallaceee45d412017-11-24 21:44:06 -05002750 u64 listen_vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04002751
Dave Wallace4878cbe2017-11-21 03:45:09 -05002752 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
Dave Wallace543852a2017-08-03 02:11:34 -04002753
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002754 if (listen_session->is_vep)
2755 {
2756 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002757 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002758 "epoll session!", getpid (), listen_session_index);
2759 rv = VPPCOM_EBADFD;
2760 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04002761 }
2762
Dave Wallaceee45d412017-11-24 21:44:06 -05002763 listen_vpp_handle = listen_session->vpp_handle;
Dave Wallace33e002b2017-09-06 01:20:02 -04002764 if (listen_session->state != STATE_LISTEN)
Dave Wallace543852a2017-08-03 02:11:34 -04002765 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002766 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05002767 "not in listen state! state 0x%x (%s)", getpid (),
2768 listen_vpp_handle, listen_session_index,
2769 listen_session->state,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002770 vppcom_session_state_str (listen_session->state));
Dave Wallaceee45d412017-11-24 21:44:06 -05002771 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002772 rv = VPPCOM_EBADFD;
2773 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002774 }
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002775 wait_for = (VCL_SESS_ATTR_TEST (listen_session->attr,
2776 VCL_SESS_ATTR_NONBLOCK))
2777 ? 0 : vcm->cfg.accept_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -04002778
Dave Wallace543852a2017-08-03 02:11:34 -04002779 clib_spinlock_unlock (&vcm->sessions_lockp);
2780
2781 while (1)
2782 {
2783 rv = vppcom_wait_for_client_session_index (wait_for);
2784 if (rv)
2785 {
2786 if ((VPPCOM_DEBUG > 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05002787 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2788 "accept failed! returning %d (%s)", getpid (),
Dave Wallaceee45d412017-11-24 21:44:06 -05002789 listen_vpp_handle, listen_session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002790 rv, vppcom_retval_str (rv));
Dave Wallace048b1d62018-01-03 22:24:41 -05002791 if (wait_for == 0)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002792 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002793 }
2794 else
2795 break;
2796 }
2797
Dave Wallace543852a2017-08-03 02:11:34 -04002798 clib_spinlock_lock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002799 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
Dave Wallace33e002b2017-09-06 01:20:02 -04002800 rv = vppcom_session_at_index (client_session_index, &client_session);
Dave Wallaceee45d412017-11-24 21:44:06 -05002801 if (PREDICT_FALSE (rv))
2802 {
2803 rv = VPPCOM_ECONNABORTED;
Dave Wallace048b1d62018-01-03 22:24:41 -05002804 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
Dave Wallaceee45d412017-11-24 21:44:06 -05002805 "lookup failed! returning %d (%s)", getpid (),
2806 listen_vpp_handle, listen_session_index,
2807 client_session_index, rv, vppcom_retval_str (rv));
2808 goto done;
2809 }
Dave Wallace543852a2017-08-03 02:11:34 -04002810
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002811 if (flags & O_NONBLOCK)
2812 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2813 else
2814 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2815
Dave Wallace543852a2017-08-03 02:11:34 -04002816 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002817 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
Dave Wallaceee45d412017-11-24 21:44:06 -05002818 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2819 getpid (), listen_vpp_handle, listen_session_index,
2820 client_session->vpp_handle, client_session_index,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002821 flags, VCL_SESS_ATTR_TEST (client_session->attr,
2822 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04002823
Dave Wallace048b1d62018-01-03 22:24:41 -05002824 if (ep)
2825 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002826 ep->is_ip4 = client_session->peer_addr.is_ip4;
2827 ep->port = client_session->peer_port;
2828 if (client_session->peer_addr.is_ip4)
2829 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2830 sizeof (ip4_address_t));
2831 else
2832 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2833 sizeof (ip6_address_t));
2834 }
Dave Wallace60caa062017-11-10 17:07:13 -05002835
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002836 vppcom_send_accept_session_reply (client_session->vpp_handle,
2837 client_session->client_context,
2838 0 /* retval OK */ );
Dave Wallace60caa062017-11-10 17:07:13 -05002839
Stevenac1f96d2017-10-24 16:03:58 -07002840 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002841 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002842 "0x%llx, sid %u connection to local %s address "
Dave Wallaceee45d412017-11-24 21:44:06 -05002843 "%U port %u", getpid (), listen_vpp_handle,
2844 listen_session_index, client_session->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002845 client_session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002846 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2847 format_ip46_address, &client_session->lcl_addr.ip46,
2848 client_session->lcl_addr.is_ip4,
2849 clib_net_to_host_u16 (client_session->lcl_port));
Dave Wallace60caa062017-11-10 17:07:13 -05002850
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002851 if (VPPCOM_DEBUG > 0)
2852 {
2853 client_session->elog_track.name =
2854 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2855 client_session_index, 0);
2856 elog_track_register (&vcm->elog_main, &client_session->elog_track);
2857
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002858 // Two elog entries due to 20-byte per entry constraint.
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002859 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002860 ELOG_TYPE_DECLARE (e) =
2861 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002862 .format = "accept: listen_handle:%x from_handle:%x",
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002863 .format_args = "i8i8",
2864 };
2865
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002866 struct
2867 {
2868 u64 handle[2];
2869 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002870
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002871 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2872 ed->handle[0] = listen_vpp_handle;
2873 ed->handle[1] = client_session->vpp_handle;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002874 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002875
2876 if (client_session->lcl_addr.is_ip4)
2877 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08002878 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002879 ELOG_TYPE_DECLARE (e2) =
2880 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002881 .format = "accept: S:%d %d.%d.%d.%d:%d ",
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002882 .format_args = "i4i1i1i1i1i2",
2883 };
2884
2885 CLIB_PACKED (struct {
2886 u32 session;
2887 u8 addr[4];
2888 u16 port;
2889 }) * ed2;
2890
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002891 ed2 =
2892 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2893 ed2->session = client_session_index;
2894 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2895 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2896 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2897 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2898 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08002899 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002900 }
2901 }
2902
Dave Wallace543852a2017-08-03 02:11:34 -04002903 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002904 rv = (int) client_session_index;
2905done:
2906 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002907}
2908
2909int
2910vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2911{
Dave Wallace543852a2017-08-03 02:11:34 -04002912 session_t *session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002913 u64 vpp_handle = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05002914 int rv, retval = VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04002915
Dave Wallace4878cbe2017-11-21 03:45:09 -05002916 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2917
2918 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04002919 {
2920 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05002921 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2922 "connect on an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002923 rv = VPPCOM_EBADFD;
2924 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002925 }
2926
Dave Wallace4878cbe2017-11-21 03:45:09 -05002927 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04002928 {
Dave Wallace543852a2017-08-03 02:11:34 -04002929 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002930 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
Dave Wallaceee45d412017-11-24 21:44:06 -05002931 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002932 getpid (), session->vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002933 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2934 format_ip46_address,
2935 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2936 clib_net_to_host_u16 (session->peer_port),
2937 session->proto ? "UDP" : "TCP", session->state,
2938 vppcom_session_state_str (session->state));
2939
2940 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002941 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04002942 }
2943
Dave Wallace35830af2017-10-09 01:43:42 -04002944 session->peer_addr.is_ip4 = server_ep->is_ip4;
2945 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
Stevenac1f96d2017-10-24 16:03:58 -07002946 session->peer_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04002947
2948 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002949 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
Dave Wallaceee45d412017-11-24 21:44:06 -05002950 "%s %U port %d proto %s",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002951 getpid (), session->vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002952 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2953 format_ip46_address,
2954 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2955 clib_net_to_host_u16 (session->peer_port),
2956 session->proto ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04002957
2958 vppcom_send_connect_sock (session, session_index);
2959 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002960
Dave Wallaceee45d412017-11-24 21:44:06 -05002961 retval =
2962 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2963 vcm->cfg.session_timeout);
2964
2965 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2966 vpp_handle = session->vpp_handle;
2967 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace7876d392017-10-19 03:53:57 -04002968
Dave Wallace4878cbe2017-11-21 03:45:09 -05002969done:
Dave Wallaceee45d412017-11-24 21:44:06 -05002970 if (PREDICT_FALSE (retval))
2971 {
2972 rv = retval;
2973 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002974 {
2975 if (session)
2976 clib_warning
2977 ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
2978 "returning %d (%s)", getpid (), vpp_handle,
2979 session_index, rv, vppcom_retval_str (rv));
2980 else
2981 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
2982 "returning %d (%s)", getpid (),
2983 session_index, rv, vppcom_retval_str (rv));
2984 }
Dave Wallaceee45d412017-11-24 21:44:06 -05002985 }
2986 else if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05002987 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
Dave Wallaceee45d412017-11-24 21:44:06 -05002988 getpid (), vpp_handle, session_index);
2989
Dave Wallace4878cbe2017-11-21 03:45:09 -05002990 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04002991}
2992
Steven58f464e2017-10-25 12:33:12 -07002993static inline int
2994vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2995 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04002996{
Dave Wallace543852a2017-08-03 02:11:34 -04002997 session_t *session = 0;
2998 svm_fifo_t *rx_fifo;
2999 int n_read = 0;
3000 int rv;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003001 int is_nonblocking;
3002
3003 u64 vpp_handle;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003004 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003005 session_state_t state;
Dave Wallace543852a2017-08-03 02:11:34 -04003006
3007 ASSERT (buf);
3008
Dave Wallace4878cbe2017-11-21 03:45:09 -05003009 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3010
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003011 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3012 rx_fifo = session->rx_fifo;
3013 state = session->state;
3014 vpp_handle = session->vpp_handle;
3015
Dave Wallace4878cbe2017-11-21 03:45:09 -05003016 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04003017 {
3018 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05003019 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3020 "read from an epoll session!", getpid (), session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003021 rv = VPPCOM_EBADFD;
3022 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003023 }
3024
Dave Wallace4878cbe2017-11-21 03:45:09 -05003025 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003026 {
3027 clib_spinlock_unlock (&vcm->sessions_lockp);
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08003028 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003029
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003030 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003031 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
Dave Wallaceee45d412017-11-24 21:44:06 -05003032 "not open! state 0x%x (%s), returning %d (%s)",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003033 getpid (), vpp_handle, session_index, state,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003034 vppcom_session_state_str (state),
3035 rv, vppcom_retval_str (rv));
3036 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003037 }
3038
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003039 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003040
3041 do
3042 {
Steven58f464e2017-10-25 12:33:12 -07003043 if (peek)
3044 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3045 else
3046 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003047 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003048 while (!is_nonblocking && (n_read <= 0));
Dave Wallacef7f809c2017-10-03 01:48:42 -04003049
Dave Wallace4878cbe2017-11-21 03:45:09 -05003050 if (n_read <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003051 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003052 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3053
3054 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3055 (EPOLLET | EPOLLIN));
3056 if (poll_et)
3057 session->vep.et_mask |= EPOLLIN;
3058
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08003059 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05003060 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003061 rv = VPPCOM_ECONNRESET;
3062
3063 if (VPPCOM_DEBUG > 1)
3064 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003065 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003066 "with session state 0x%x (%s)!"
Dave Wallaceee45d412017-11-24 21:44:06 -05003067 " Setting state to 0x%x (%s), returning %d (%s)",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003068 getpid (), session->vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003069 state, vppcom_session_state_str (state),
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003070 STATE_DISCONNECT,
3071 vppcom_session_state_str (STATE_DISCONNECT), rv,
3072 vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05003073 }
3074
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003075 session->state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003076 }
3077 else
3078 rv = VPPCOM_EAGAIN;
3079
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003080 clib_spinlock_unlock (&vcm->sessions_lockp);
3081 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003082 else
3083 rv = n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04003084
Dave Wallace4878cbe2017-11-21 03:45:09 -05003085 if (VPPCOM_DEBUG > 2)
3086 {
3087 if (rv > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003088 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003089 "from (%p)", getpid (), vpp_handle,
3090 session_index, n_read, rx_fifo);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003091 else
Dave Wallace048b1d62018-01-03 22:24:41 -05003092 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
Dave Wallaceee45d412017-11-24 21:44:06 -05003093 "returning %d (%s)", getpid (), vpp_handle,
3094 session_index, rv, vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05003095 }
3096done:
3097 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003098}
3099
Steven58f464e2017-10-25 12:33:12 -07003100int
Dave Wallace048b1d62018-01-03 22:24:41 -05003101vppcom_session_read (uint32_t session_index, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07003102{
3103 return (vppcom_session_read_internal (session_index, buf, n, 0));
3104}
3105
3106static int
3107vppcom_session_peek (uint32_t session_index, void *buf, int n)
3108{
3109 return (vppcom_session_read_internal (session_index, buf, n, 1));
3110}
3111
Dave Wallace543852a2017-08-03 02:11:34 -04003112static inline int
3113vppcom_session_read_ready (session_t * session, u32 session_index)
3114{
Dave Wallace543852a2017-08-03 02:11:34 -04003115 int ready = 0;
Dave Wallace60caa062017-11-10 17:07:13 -05003116 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003117 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003118 session_state_t state = session->state;
Dave Wallaceee45d412017-11-24 21:44:06 -05003119 u64 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04003120
3121 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05003122 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003123 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003124 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003125 "epoll session!", getpid (), session_index);
3126 rv = VPPCOM_EBADFD;
3127 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003128 }
Dave Wallace33e002b2017-09-06 01:20:02 -04003129
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003130 if (session->state & STATE_LISTEN)
Dave Wallace543852a2017-08-03 02:11:34 -04003131 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3132 else
3133 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003134 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3135 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08003136 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
Dave Wallace4878cbe2017-11-21 03:45:09 -05003137 VPPCOM_ENOTCONN);
3138
3139 if (VPPCOM_DEBUG > 1)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003140 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session is "
Dave Wallaceee45d412017-11-24 21:44:06 -05003141 "not open! state 0x%x (%s), returning %d (%s)",
3142 getpid (), vpp_handle, session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003143 state, vppcom_session_state_str (state),
3144 rv, vppcom_retval_str (rv));
3145 goto done;
3146 }
3147
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003148 ready = svm_fifo_max_dequeue (session->rx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04003149 }
3150
Dave Wallace4878cbe2017-11-21 03:45:09 -05003151 if (ready == 0)
Dave Wallace60caa062017-11-10 17:07:13 -05003152 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003153 poll_et =
3154 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3155 if (poll_et)
3156 session->vep.et_mask |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05003157
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08003158 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05003159 {
3160 rv = VPPCOM_ECONNRESET;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003161
3162 if (VPPCOM_DEBUG > 1)
3163 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003164 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3165 "sid %u: Empty fifo with"
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003166 " session state 0x%x (%s)! Setting state to "
Dave Wallaceee45d412017-11-24 21:44:06 -05003167 "0x%x (%s), returning %d (%s)",
3168 getpid (), session_index, vpp_handle,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003169 state, vppcom_session_state_str (state),
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003170 STATE_DISCONNECT,
3171 vppcom_session_state_str (STATE_DISCONNECT), rv,
3172 vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05003173 }
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003174 session->state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003175 goto done;
3176 }
3177 }
3178 rv = ready;
Dave Wallace16cb4082017-11-29 03:24:06 -05003179
3180 if (vcm->app_event_queue->cursize &&
3181 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3182 {
3183 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3184 session_fifo_event_t e;
3185
3186 for (i = 0; i < n_to_dequeue; i++)
Florin Corase86a8ed2018-01-05 03:20:25 -08003187 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
Dave Wallace16cb4082017-11-29 03:24:06 -05003188
3189 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3190 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003191done:
3192 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003193}
3194
3195int
Dave Wallace048b1d62018-01-03 22:24:41 -05003196vppcom_session_write (uint32_t session_index, void *buf, size_t n)
Dave Wallace543852a2017-08-03 02:11:34 -04003197{
Dave Wallace543852a2017-08-03 02:11:34 -04003198 session_t *session = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003199 svm_fifo_t *tx_fifo = 0;
Florin Corase86a8ed2018-01-05 03:20:25 -08003200 svm_queue_t *q;
Dave Wallace543852a2017-08-03 02:11:34 -04003201 session_fifo_event_t evt;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003202 session_state_t state;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003203 int rv, n_write, is_nonblocking;
3204 u32 poll_et;
Dave Wallaceee45d412017-11-24 21:44:06 -05003205 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04003206
3207 ASSERT (buf);
3208
Dave Wallace4878cbe2017-11-21 03:45:09 -05003209 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3210
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003211 tx_fifo = session->tx_fifo;
3212 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3213 vpp_handle = session->vpp_handle;
3214 state = session->state;
3215
Dave Wallace4878cbe2017-11-21 03:45:09 -05003216 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04003217 {
3218 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace048b1d62018-01-03 22:24:41 -05003219 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003220 "cannot write to an epoll session!",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003221 getpid (), vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003222
3223 rv = VPPCOM_EBADFD;
3224 goto done;
Dave Wallace543852a2017-08-03 02:11:34 -04003225 }
3226
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003227 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003228 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003229 rv =
3230 ((session->state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3231 VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003232
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003233 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003234 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003235 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003236 "session is not open! state 0x%x (%s)",
Dave Wallaceee45d412017-11-24 21:44:06 -05003237 getpid (), vpp_handle, session_index,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003238 state, vppcom_session_state_str (state));
Dave Wallace4878cbe2017-11-21 03:45:09 -05003239 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003240 }
3241
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003242 clib_spinlock_unlock (&vcm->sessions_lockp);
3243
Dave Wallace543852a2017-08-03 02:11:34 -04003244 do
3245 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003246 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
Dave Wallace543852a2017-08-03 02:11:34 -04003247 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003248 while (!is_nonblocking && (n_write <= 0));
Dave Wallace543852a2017-08-03 02:11:34 -04003249
3250 /* If event wasn't set, add one */
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003251 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04003252 {
Dave Wallace543852a2017-08-03 02:11:34 -04003253 /* Fabricate TX event, send to vpp */
3254 evt.fifo = tx_fifo;
3255 evt.event_type = FIFO_EVENT_APP_TX;
Dave Wallace543852a2017-08-03 02:11:34 -04003256
Dave Wallace4878cbe2017-11-21 03:45:09 -05003257 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3258 q = session->vpp_event_queue;
Dave Wallace543852a2017-08-03 02:11:34 -04003259 ASSERT (q);
Florin Corase86a8ed2018-01-05 03:20:25 -08003260 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
Dave Wallace4878cbe2017-11-21 03:45:09 -05003261 clib_spinlock_unlock (&vcm->sessions_lockp);
3262 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003263 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003264 "added FIFO_EVENT_APP_TX to "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003265 "vpp_event_q %p, n_write %d", getpid (),
Dave Wallaceee45d412017-11-24 21:44:06 -05003266 vpp_handle, session_index, q, n_write);
Dave Wallace543852a2017-08-03 02:11:34 -04003267 }
3268
Dave Wallace4878cbe2017-11-21 03:45:09 -05003269 if (n_write <= 0)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003270 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003271 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3272
3273 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3274 (EPOLLET | EPOLLOUT));
3275 if (poll_et)
3276 session->vep.et_mask |= EPOLLOUT;
3277
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003278 if (session->state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05003279 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003280 rv = VPPCOM_ECONNRESET;
3281
3282 if (VPPCOM_DEBUG > 1)
3283 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003284 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003285 "Empty fifo with session state 0x%x (%s)!"
Dave Wallaceee45d412017-11-24 21:44:06 -05003286 " Setting state to 0x%x (%s), returning %d (%s)",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003287 getpid (), session->vpp_handle, session_index,
3288 session->state,
3289 vppcom_session_state_str (session->state),
3290 STATE_DISCONNECT,
3291 vppcom_session_state_str (STATE_DISCONNECT), rv,
3292 vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05003293 }
3294
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003295 session->state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003296 }
3297 else
3298 rv = VPPCOM_EAGAIN;
3299
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003300 clib_spinlock_unlock (&vcm->sessions_lockp);
3301 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003302 else
3303 rv = n_write;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003304
Dave Wallace543852a2017-08-03 02:11:34 -04003305 if (VPPCOM_DEBUG > 2)
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003306 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003307 if (n_write <= 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003308 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003309 "FIFO-FULL (%p)", getpid (), vpp_handle,
3310 session_index, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003311 else
Dave Wallace048b1d62018-01-03 22:24:41 -05003312 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003313 "wrote %d bytes tx-fifo: (%p)", getpid (),
3314 vpp_handle, session_index, n_write, tx_fifo);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003315 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003316done:
3317 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003318}
3319
3320static inline int
3321vppcom_session_write_ready (session_t * session, u32 session_index)
3322{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003323 int ready;
Dave Wallace60caa062017-11-10 17:07:13 -05003324 u32 poll_et;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003325 int rv;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003326
3327 ASSERT (session);
Dave Wallace543852a2017-08-03 02:11:34 -04003328
3329 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05003330 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003331 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003332 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003333 "cannot write to an epoll session!",
3334 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003335 rv = VPPCOM_EBADFD;
3336 goto done;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04003337 }
3338
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003339 if (PREDICT_FALSE (session->state & STATE_LISTEN))
Dave Wallace33e002b2017-09-06 01:20:02 -04003340 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003341 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003342 "cannot write to a listen session!",
3343 getpid (), session->vpp_handle, session_index);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003344 rv = VPPCOM_EBADFD;
3345 goto done;
3346 }
3347
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003348 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05003349 {
3350 session_state_t state = session->state;
3351
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08003352 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003353
Dave Wallace048b1d62018-01-03 22:24:41 -05003354 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003355 "session is not open! state 0x%x (%s), "
Dave Wallaceee45d412017-11-24 21:44:06 -05003356 "returning %d (%s)", getpid (), session->vpp_handle,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003357 session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05003358 state, vppcom_session_state_str (state),
3359 rv, vppcom_retval_str (rv));
3360 goto done;
Dave Wallace33e002b2017-09-06 01:20:02 -04003361 }
3362
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003363 ready = svm_fifo_max_enqueue (session->tx_fifo);
Dave Wallace543852a2017-08-03 02:11:34 -04003364
Dave Wallace33e002b2017-09-06 01:20:02 -04003365 if (VPPCOM_DEBUG > 3)
Dave Wallace048b1d62018-01-03 22:24:41 -05003366 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05003367 "peek %s (%p), ready = %d", getpid (),
3368 session->vpp_handle, session_index,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003369 session->tx_fifo, ready);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003370
Dave Wallace4878cbe2017-11-21 03:45:09 -05003371 if (ready == 0)
3372 {
3373 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3374 (EPOLLET | EPOLLOUT));
3375 if (poll_et)
3376 session->vep.et_mask |= EPOLLOUT;
3377
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003378 if (session->state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -05003379 {
3380 rv = VPPCOM_ECONNRESET;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003381
3382 if (VPPCOM_DEBUG > 1)
3383 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003384 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003385 "Empty fifo with session "
Dave Wallaceee45d412017-11-24 21:44:06 -05003386 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3387 "returning %d (%s)", getpid (),
3388 session->vpp_handle, session_index,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003389 session->state,
3390 vppcom_session_state_str (session->state),
3391 STATE_DISCONNECT,
3392 vppcom_session_state_str (STATE_DISCONNECT), rv,
3393 vppcom_retval_str (rv));
Dave Wallace4878cbe2017-11-21 03:45:09 -05003394 }
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003395 session->state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003396 goto done;
3397 }
3398 }
3399 rv = ready;
3400done:
3401 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04003402}
3403
3404int
3405vppcom_select (unsigned long n_bits, unsigned long *read_map,
3406 unsigned long *write_map, unsigned long *except_map,
3407 double time_to_wait)
3408{
Dave Wallace543852a2017-08-03 02:11:34 -04003409 u32 session_index;
3410 session_t *session = 0;
3411 int rv, bits_set = 0;
3412 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3413 u32 minbits = clib_max (n_bits, BITS (uword));
3414
3415 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3416
Dave Wallace7876d392017-10-19 03:53:57 -04003417 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003418 {
3419 clib_bitmap_validate (vcm->rd_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05003420 clib_memcpy (vcm->rd_bitmap, read_map,
3421 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3422 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04003423 }
Dave Wallace7876d392017-10-19 03:53:57 -04003424 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003425 {
3426 clib_bitmap_validate (vcm->wr_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05003427 clib_memcpy (vcm->wr_bitmap, write_map,
3428 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3429 memset (write_map, 0,
3430 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04003431 }
Dave Wallace7876d392017-10-19 03:53:57 -04003432 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003433 {
3434 clib_bitmap_validate (vcm->ex_bitmap, minbits);
Dave Wallace048b1d62018-01-03 22:24:41 -05003435 clib_memcpy (vcm->ex_bitmap, except_map,
3436 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3437 memset (except_map, 0,
3438 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04003439 }
3440
3441 do
3442 {
3443 /* *INDENT-OFF* */
Dave Wallacee22aa742017-10-20 12:30:38 -04003444 if (n_bits)
3445 {
3446 if (read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003447 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003448 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3449 ({
3450 clib_spinlock_lock (&vcm->sessions_lockp);
3451 rv = vppcom_session_at_index (session_index, &session);
3452 if (rv < 0)
3453 {
3454 clib_spinlock_unlock (&vcm->sessions_lockp);
3455 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003456 clib_warning ("VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003457 "read_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003458 session_index);
3459 bits_set = VPPCOM_EBADFD;
3460 goto select_done;
3461 }
3462
3463 rv = vppcom_session_read_ready (session, session_index);
3464 clib_spinlock_unlock (&vcm->sessions_lockp);
3465 if (except_map && vcm->ex_bitmap &&
3466 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3467 (rv < 0))
3468 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003469 clib_bitmap_set_no_check (except_map, session_index, 1);
3470 bits_set++;
3471 }
3472 else if (rv > 0)
3473 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003474 clib_bitmap_set_no_check (read_map, session_index, 1);
3475 bits_set++;
3476 }
3477 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003478 }
3479
Dave Wallacee22aa742017-10-20 12:30:38 -04003480 if (write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003481 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003482 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3483 ({
3484 clib_spinlock_lock (&vcm->sessions_lockp);
3485 rv = vppcom_session_at_index (session_index, &session);
3486 if (rv < 0)
3487 {
3488 clib_spinlock_unlock (&vcm->sessions_lockp);
3489 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003490 clib_warning ("VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003491 "write_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003492 session_index);
3493 bits_set = VPPCOM_EBADFD;
3494 goto select_done;
3495 }
Dave Wallace543852a2017-08-03 02:11:34 -04003496
Dave Wallacee22aa742017-10-20 12:30:38 -04003497 rv = vppcom_session_write_ready (session, session_index);
3498 clib_spinlock_unlock (&vcm->sessions_lockp);
3499 if (write_map && (rv > 0))
3500 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003501 clib_bitmap_set_no_check (write_map, session_index, 1);
3502 bits_set++;
3503 }
3504 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003505 }
3506
Dave Wallacee22aa742017-10-20 12:30:38 -04003507 if (except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04003508 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003509 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3510 ({
3511 clib_spinlock_lock (&vcm->sessions_lockp);
3512 rv = vppcom_session_at_index (session_index, &session);
3513 if (rv < 0)
3514 {
3515 clib_spinlock_unlock (&vcm->sessions_lockp);
3516 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003517 clib_warning ("VCL<%d>: session %d specified in "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003518 "except_map is closed.", getpid (),
Dave Wallacee22aa742017-10-20 12:30:38 -04003519 session_index);
3520 bits_set = VPPCOM_EBADFD;
3521 goto select_done;
3522 }
Dave Wallace543852a2017-08-03 02:11:34 -04003523
Dave Wallacee22aa742017-10-20 12:30:38 -04003524 rv = vppcom_session_read_ready (session, session_index);
3525 clib_spinlock_unlock (&vcm->sessions_lockp);
3526 if (rv < 0)
3527 {
Dave Wallacee22aa742017-10-20 12:30:38 -04003528 clib_bitmap_set_no_check (except_map, session_index, 1);
3529 bits_set++;
3530 }
3531 }));
Dave Wallace543852a2017-08-03 02:11:34 -04003532 }
Dave Wallacee22aa742017-10-20 12:30:38 -04003533 }
Dave Wallace543852a2017-08-03 02:11:34 -04003534 /* *INDENT-ON* */
3535 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003536 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
Dave Wallace543852a2017-08-03 02:11:34 -04003537
3538select_done:
3539 return (bits_set);
3540}
3541
Dave Wallacef7f809c2017-10-03 01:48:42 -04003542static inline void
3543vep_verify_epoll_chain (u32 vep_idx)
3544{
3545 session_t *session;
3546 vppcom_epoll_t *vep;
3547 int rv;
Dave Wallace498b3a52017-11-09 13:00:34 -05003548 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003549
Dave Wallace498b3a52017-11-09 13:00:34 -05003550 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003551 return;
3552
3553 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3554 rv = vppcom_session_at_index (vep_idx, &session);
3555 if (PREDICT_FALSE (rv))
3556 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003557 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3558 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003559 goto done;
3560 }
3561 if (PREDICT_FALSE (!session->is_vep))
3562 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003563 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3564 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003565 goto done;
3566 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003567 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05003568 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05003569 "{\n"
3570 " is_vep = %u\n"
3571 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05003572 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05003573 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05003574 "}\n", getpid (), vep_idx,
3575 session->is_vep, session->is_vep_session,
3576 vep->next_sid, vep->next_sid,
Dave Wallace498b3a52017-11-09 13:00:34 -05003577 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003578
3579 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
Dave Wallacef7f809c2017-10-03 01:48:42 -04003580 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003581 rv = vppcom_session_at_index (sid, &session);
3582 if (PREDICT_FALSE (rv))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003583 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003584 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003585 goto done;
3586 }
3587 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05003588 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3589 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003590 else if (PREDICT_FALSE (!session->is_vep_session))
3591 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003592 clib_warning ("VCL<%d>: ERROR: session (%u) "
3593 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05003594 goto done;
3595 }
3596 vep = &session->vep;
3597 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05003598 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003599 "vep_idx (%u)!", getpid (),
3600 sid, session->vep.vep_idx, vep_idx);
3601 if (session->is_vep_session)
3602 {
3603 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3604 "{\n"
3605 " next_sid = 0x%x (%u)\n"
3606 " prev_sid = 0x%x (%u)\n"
3607 " vep_idx = 0x%x (%u)\n"
3608 " ev.events = 0x%x\n"
3609 " ev.data.u64 = 0x%llx\n"
3610 " et_mask = 0x%x\n"
3611 "}\n",
3612 vep_idx, sid, sid,
3613 vep->next_sid, vep->next_sid,
3614 vep->prev_sid, vep->prev_sid,
3615 vep->vep_idx, vep->vep_idx,
3616 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003617 }
3618 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003619
3620done:
Dave Wallace048b1d62018-01-03 22:24:41 -05003621 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3622 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003623}
3624
3625int
3626vppcom_epoll_create (void)
3627{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003628 session_t *vep_session;
3629 u32 vep_idx;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003630 elog_track_t vep_elog_track;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003631
3632 clib_spinlock_lock (&vcm->sessions_lockp);
3633 pool_get (vcm->sessions, vep_session);
3634 memset (vep_session, 0, sizeof (*vep_session));
3635 vep_idx = vep_session - vcm->sessions;
3636
3637 vep_session->is_vep = 1;
3638 vep_session->vep.vep_idx = ~0;
3639 vep_session->vep.next_sid = ~0;
3640 vep_session->vep.prev_sid = ~0;
3641 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003642 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003643
3644 if (VPPCOM_DEBUG > 0)
3645 {
3646 vep_session->elog_track.name =
3647 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3648 vep_idx, 0);
3649 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3650 vep_elog_track = vep_session->elog_track;
3651 }
3652
Dave Wallacef7f809c2017-10-03 01:48:42 -04003653 clib_spinlock_unlock (&vcm->sessions_lockp);
3654
3655 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003656 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
Dave Wallaceee45d412017-11-24 21:44:06 -05003657 getpid (), vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003658
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003659 if (VPPCOM_DEBUG > 0)
3660 {
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003661
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003662 /* *INDENT-OFF* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003663 ELOG_TYPE_DECLARE (e) =
3664 {
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003665 .format = "created epoll session:%d",
3666 .format_args = "i4",
3667 };
3668
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003669 struct
3670 {
3671 u32 data;
3672 } *ed;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003673
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003674 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003675 ed->data = vep_idx;
Keith Burns (alagalah)d3046592018-01-16 14:20:34 -08003676 /* *INDENT-ON* */
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08003677 }
3678
Dave Wallacef7f809c2017-10-03 01:48:42 -04003679 return (vep_idx);
3680}
3681
3682int
3683vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3684 struct epoll_event *event)
3685{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003686 session_t *vep_session;
3687 session_t *session;
3688 int rv;
3689
3690 if (vep_idx == session_index)
3691 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003692 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003693 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003694 return VPPCOM_EINVAL;
3695 }
3696
3697 clib_spinlock_lock (&vcm->sessions_lockp);
3698 rv = vppcom_session_at_index (vep_idx, &vep_session);
3699 if (PREDICT_FALSE (rv))
3700 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003701 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003702 goto done;
3703 }
3704 if (PREDICT_FALSE (!vep_session->is_vep))
3705 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003706 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003707 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003708 rv = VPPCOM_EINVAL;
3709 goto done;
3710 }
3711
3712 ASSERT (vep_session->vep.vep_idx == ~0);
3713 ASSERT (vep_session->vep.prev_sid == ~0);
3714
3715 rv = vppcom_session_at_index (session_index, &session);
3716 if (PREDICT_FALSE (rv))
3717 {
3718 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003719 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003720 getpid (), session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003721 goto done;
3722 }
3723 if (PREDICT_FALSE (session->is_vep))
3724 {
Dave Wallace4878cbe2017-11-21 03:45:09 -05003725 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003726 rv = VPPCOM_EINVAL;
3727 goto done;
3728 }
3729
3730 switch (op)
3731 {
3732 case EPOLL_CTL_ADD:
3733 if (PREDICT_FALSE (!event))
3734 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003735 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003736 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003737 rv = VPPCOM_EINVAL;
3738 goto done;
3739 }
3740 if (vep_session->vep.next_sid != ~0)
3741 {
3742 session_t *next_session;
3743 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3744 &next_session);
3745 if (PREDICT_FALSE (rv))
3746 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003747 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003748 "vep.next_sid (%u) on vep_idx (%u)!",
3749 getpid (), vep_session->vep.next_sid, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003750 goto done;
3751 }
3752 ASSERT (next_session->vep.prev_sid == vep_idx);
3753 next_session->vep.prev_sid = session_index;
3754 }
3755 session->vep.next_sid = vep_session->vep.next_sid;
3756 session->vep.prev_sid = vep_idx;
3757 session->vep.vep_idx = vep_idx;
3758 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3759 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05003760 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003761 session->is_vep_session = 1;
3762 vep_session->vep.next_sid = session_index;
3763 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003764 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3765 "sid %u, events 0x%x, data 0x%llx!",
3766 getpid (), vep_idx, session_index,
Dave Wallacef7f809c2017-10-03 01:48:42 -04003767 event->events, event->data.u64);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08003768 if (VPPCOM_DEBUG > 0)
3769 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08003770 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08003771 ELOG_TYPE_DECLARE (e) =
3772 {
3773 .format = "epoll_ctladd: events:%x data:%x",
3774 .format_args = "i4i4i8",
3775 };
3776 struct
3777 {
3778 u32 events;
3779 u64 event_data;
3780 } *ed;
3781
3782 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3783
3784 ed->events = event->events;
3785 ed->event_data = event->data.u64;
3786 /* *INDENT-ON* */
3787 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003788 break;
3789
3790 case EPOLL_CTL_MOD:
3791 if (PREDICT_FALSE (!event))
3792 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003793 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05003794 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04003795 rv = VPPCOM_EINVAL;
3796 goto done;
3797 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05003798 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003799 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003800 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003801 "not a vep session!", getpid (), session_index);
3802 rv = VPPCOM_EINVAL;
3803 goto done;
3804 }
3805 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3806 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003807 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003808 "vep_idx (%u) != vep_idx (%u)!",
3809 getpid (), session_index,
3810 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003811 rv = VPPCOM_EINVAL;
3812 goto done;
3813 }
3814 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3815 session->vep.ev = *event;
3816 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003817 clib_warning
3818 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3819 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3820 event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003821 break;
3822
3823 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05003824 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003825 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003826 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003827 "not a vep session!", getpid (), session_index);
3828 rv = VPPCOM_EINVAL;
3829 goto done;
3830 }
3831 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3832 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003833 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003834 "vep_idx (%u) != vep_idx (%u)!",
3835 getpid (), session_index,
3836 session->vep.vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003837 rv = VPPCOM_EINVAL;
3838 goto done;
3839 }
3840
3841 vep_session->wait_cont_idx =
3842 (vep_session->wait_cont_idx == session_index) ?
3843 session->vep.next_sid : vep_session->wait_cont_idx;
3844
3845 if (session->vep.prev_sid == vep_idx)
3846 vep_session->vep.next_sid = session->vep.next_sid;
3847 else
3848 {
3849 session_t *prev_session;
3850 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3851 if (PREDICT_FALSE (rv))
3852 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003853 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003854 "vep.prev_sid (%u) on sid (%u)!",
3855 getpid (), session->vep.prev_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003856 goto done;
3857 }
3858 ASSERT (prev_session->vep.next_sid == session_index);
3859 prev_session->vep.next_sid = session->vep.next_sid;
3860 }
3861 if (session->vep.next_sid != ~0)
3862 {
3863 session_t *next_session;
3864 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3865 if (PREDICT_FALSE (rv))
3866 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003867 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05003868 "vep.next_sid (%u) on sid (%u)!",
3869 getpid (), session->vep.next_sid, session_index);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003870 goto done;
3871 }
3872 ASSERT (next_session->vep.prev_sid == session_index);
3873 next_session->vep.prev_sid = session->vep.prev_sid;
3874 }
3875
3876 memset (&session->vep, 0, sizeof (session->vep));
3877 session->vep.next_sid = ~0;
3878 session->vep.prev_sid = ~0;
3879 session->vep.vep_idx = ~0;
3880 session->is_vep_session = 0;
3881 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05003882 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003883 getpid (), vep_idx, session_index);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08003884 if (VPPCOM_DEBUG > 0)
3885 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08003886 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08003887 ELOG_TYPE_DECLARE (e) =
3888 {
3889 .format = "epoll_ctldel: vep:%d",
3890 .format_args = "i4",
3891 };
3892 struct
3893 {
3894 u32 data;
3895 } *ed;
3896
3897 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3898
3899 ed->data = vep_idx;
3900 /* *INDENT-ON* */
3901 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003902 break;
3903
3904 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05003905 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003906 rv = VPPCOM_EINVAL;
3907 }
3908
3909 vep_verify_epoll_chain (vep_idx);
3910
3911done:
3912 clib_spinlock_unlock (&vcm->sessions_lockp);
3913 return rv;
3914}
3915
Dave Wallacef7f809c2017-10-03 01:48:42 -04003916int
3917vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3918 int maxevents, double wait_for_time)
3919{
Dave Wallacef7f809c2017-10-03 01:48:42 -04003920 session_t *vep_session;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003921 elog_track_t vep_elog_track;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003922 int rv;
3923 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
Dave Wallace2e005bb2017-11-07 01:21:39 -05003924 u32 keep_trying = 1;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003925 int num_ev = 0;
3926 u32 vep_next_sid, wait_cont_idx;
3927 u8 is_vep;
3928
3929 if (PREDICT_FALSE (maxevents <= 0))
3930 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003931 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003932 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003933 return VPPCOM_EINVAL;
3934 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003935 memset (events, 0, sizeof (*events) * maxevents);
3936
3937 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3938 vep_next_sid = vep_session->vep.next_sid;
3939 is_vep = vep_session->is_vep;
3940 wait_cont_idx = vep_session->wait_cont_idx;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003941 vep_elog_track = vep_session->elog_track;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003942 clib_spinlock_unlock (&vcm->sessions_lockp);
3943
3944 if (PREDICT_FALSE (!is_vep))
3945 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003946 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05003947 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04003948 rv = VPPCOM_EINVAL;
3949 goto done;
3950 }
Dave Wallacee695cb42017-11-02 22:04:42 -04003951 if (PREDICT_FALSE (vep_next_sid == ~0))
Dave Wallacef7f809c2017-10-03 01:48:42 -04003952 {
Dave Wallacee695cb42017-11-02 22:04:42 -04003953 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05003954 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05003955 getpid (), vep_idx);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08003956 if (VPPCOM_DEBUG > 0)
3957 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08003958 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08003959 ELOG_TYPE_DECLARE (e) =
3960 {
3961 .format = "WRN: vep_idx:%d empty",
3962 .format_args = "i4",
3963 };
3964 struct
3965 {
3966 u32 data;
3967 } *ed;
3968
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003969 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08003970
3971 ed->data = vep_idx;
3972 /* *INDENT-ON* */
3973 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04003974 goto done;
3975 }
3976
3977 do
3978 {
3979 u32 sid;
3980 u32 next_sid = ~0;
3981 session_t *session;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003982 elog_track_t session_elog_track;
Dave Wallacef7f809c2017-10-03 01:48:42 -04003983
3984 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3985 sid != ~0; sid = next_sid)
3986 {
3987 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3988 u8 add_event, is_vep_session;
3989 int ready;
3990 u64 session_ev_data;
3991
3992 VCL_LOCK_AND_GET_SESSION (sid, &session);
3993 next_sid = session->vep.next_sid;
3994 session_events = session->vep.ev.events;
3995 et_mask = session->vep.et_mask;
3996 is_vep = session->is_vep;
3997 is_vep_session = session->is_vep_session;
3998 session_vep_idx = session->vep.vep_idx;
3999 session_ev_data = session->vep.ev.data.u64;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004000
4001 if (VPPCOM_DEBUG > 0)
4002 {
4003 session_elog_track = session->elog_track;
4004 }
4005
Dave Wallacef7f809c2017-10-03 01:48:42 -04004006 clib_spinlock_unlock (&vcm->sessions_lockp);
4007
4008 if (PREDICT_FALSE (is_vep))
4009 {
4010 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05004011 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004012 getpid (), vep_idx);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004013 if (VPPCOM_DEBUG > 0)
4014 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004015 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004016 ELOG_TYPE_DECLARE (e) =
4017 {
4018 .format = "ERR:vep_idx:%d is vep",
4019 .format_args = "i4",
4020 };
4021 struct
4022 {
4023 u32 data;
4024 } *ed;
4025
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004026 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004027
4028 ed->data = vep_idx;
4029 /* *INDENT-ON* */
4030 }
4031
Dave Wallacef7f809c2017-10-03 01:48:42 -04004032 rv = VPPCOM_EINVAL;
4033 goto done;
4034 }
4035 if (PREDICT_FALSE (!is_vep_session))
4036 {
4037 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05004038 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
Dave Wallace2e005bb2017-11-07 01:21:39 -05004039 "a vep session!", getpid (), sid);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004040 if (VPPCOM_DEBUG > 0)
4041 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004042 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004043 ELOG_TYPE_DECLARE (e) =
4044 {
4045 .format = "ERR:SID:%d not vep",
4046 .format_args = "i4",
4047 };
4048 struct
4049 {
4050 u32 data;
4051 } *ed;
4052
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004053 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004054
4055 ed->data = sid;
4056 /* *INDENT-ON* */
4057 }
4058
Dave Wallacef7f809c2017-10-03 01:48:42 -04004059 rv = VPPCOM_EINVAL;
4060 goto done;
4061 }
4062 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4063 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004064 clib_warning ("VCL<%d>: ERROR: session (%u) "
Dave Wallacef7f809c2017-10-03 01:48:42 -04004065 "vep_idx (%u) != vep_idx (%u)!",
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004066 getpid (), sid, session_vep_idx, vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004067 rv = VPPCOM_EINVAL;
4068 goto done;
4069 }
4070
4071 add_event = clear_et_mask = 0;
4072
Dave Wallace60caa062017-11-10 17:07:13 -05004073 if (EPOLLIN & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04004074 {
4075 VCL_LOCK_AND_GET_SESSION (sid, &session);
4076 ready = vppcom_session_read_ready (session, sid);
4077 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05004078 if ((ready > 0) && (EPOLLIN & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004079 {
4080 add_event = 1;
4081 events[num_ev].events |= EPOLLIN;
Dave Wallace60caa062017-11-10 17:07:13 -05004082 if (((EPOLLET | EPOLLIN) & session_events) ==
4083 (EPOLLET | EPOLLIN))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004084 clear_et_mask |= EPOLLIN;
4085 }
4086 else if (ready < 0)
4087 {
4088 add_event = 1;
4089 switch (ready)
4090 {
4091 case VPPCOM_ECONNRESET:
4092 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4093 break;
4094
4095 default:
4096 events[num_ev].events |= EPOLLERR;
4097 break;
4098 }
4099 }
4100 }
4101
Dave Wallace60caa062017-11-10 17:07:13 -05004102 if (EPOLLOUT & session_events)
Dave Wallacef7f809c2017-10-03 01:48:42 -04004103 {
4104 VCL_LOCK_AND_GET_SESSION (sid, &session);
4105 ready = vppcom_session_write_ready (session, sid);
4106 clib_spinlock_unlock (&vcm->sessions_lockp);
Dave Wallace60caa062017-11-10 17:07:13 -05004107 if ((ready > 0) && (EPOLLOUT & et_mask))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004108 {
4109 add_event = 1;
4110 events[num_ev].events |= EPOLLOUT;
Dave Wallace60caa062017-11-10 17:07:13 -05004111 if (((EPOLLET | EPOLLOUT) & session_events) ==
4112 (EPOLLET | EPOLLOUT))
Dave Wallacef7f809c2017-10-03 01:48:42 -04004113 clear_et_mask |= EPOLLOUT;
4114 }
4115 else if (ready < 0)
4116 {
4117 add_event = 1;
4118 switch (ready)
4119 {
4120 case VPPCOM_ECONNRESET:
4121 events[num_ev].events |= EPOLLHUP;
4122 break;
4123
4124 default:
4125 events[num_ev].events |= EPOLLERR;
4126 break;
4127 }
4128 }
4129 }
4130
4131 if (add_event)
4132 {
4133 events[num_ev].data.u64 = session_ev_data;
4134 if (EPOLLONESHOT & session_events)
4135 {
4136 VCL_LOCK_AND_GET_SESSION (sid, &session);
4137 session->vep.ev.events = 0;
4138 clib_spinlock_unlock (&vcm->sessions_lockp);
4139 }
4140 num_ev++;
4141 if (num_ev == maxevents)
4142 {
4143 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4144 vep_session->wait_cont_idx = next_sid;
4145 clib_spinlock_unlock (&vcm->sessions_lockp);
4146 goto done;
4147 }
4148 }
4149 if (wait_cont_idx != ~0)
4150 {
4151 if (next_sid == ~0)
4152 next_sid = vep_next_sid;
4153 else if (next_sid == wait_cont_idx)
4154 next_sid = ~0;
4155 }
4156 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05004157 if (wait_for_time != -1)
4158 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04004159 }
Dave Wallace2e005bb2017-11-07 01:21:39 -05004160 while ((num_ev == 0) && keep_trying);
Dave Wallacef7f809c2017-10-03 01:48:42 -04004161
4162 if (wait_cont_idx != ~0)
4163 {
4164 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4165 vep_session->wait_cont_idx = ~0;
4166 clib_spinlock_unlock (&vcm->sessions_lockp);
4167 }
4168done:
4169 return (rv != VPPCOM_OK) ? rv : num_ev;
4170}
4171
Dave Wallace35830af2017-10-09 01:43:42 -04004172int
4173vppcom_session_attr (uint32_t session_index, uint32_t op,
4174 void *buffer, uint32_t * buflen)
4175{
Dave Wallace35830af2017-10-09 01:43:42 -04004176 session_t *session;
4177 int rv = VPPCOM_OK;
4178 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07004179 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04004180
4181 VCL_LOCK_AND_GET_SESSION (session_index, &session);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004182
4183 ASSERT (session);
4184
Dave Wallace35830af2017-10-09 01:43:42 -04004185 switch (op)
4186 {
4187 case VPPCOM_ATTR_GET_NREAD:
4188 rv = vppcom_session_read_ready (session, session_index);
Dave Wallace227867f2017-11-13 21:21:53 -05004189 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004190 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
Dave Wallace2e005bb2017-11-07 01:21:39 -05004191 getpid (), rv);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004192 if (VPPCOM_DEBUG > 0)
4193 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004194 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004195 ELOG_TYPE_DECLARE (e) =
4196 {
4197 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4198 .format_args = "i4",
4199 };
4200 struct
4201 {
4202 u32 data;
4203 } *ed;
4204
4205 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4206
4207 ed->data = rv;
4208 /* *INDENT-ON* */
4209 }
4210
Dave Wallace35830af2017-10-09 01:43:42 -04004211 break;
4212
Dave Wallace227867f2017-11-13 21:21:53 -05004213 case VPPCOM_ATTR_GET_NWRITE:
4214 rv = vppcom_session_write_ready (session, session_index);
4215 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004216 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
Dave Wallace227867f2017-11-13 21:21:53 -05004217 getpid (), session_index, rv);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004218 if (VPPCOM_DEBUG > 0)
4219 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004220 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004221 ELOG_TYPE_DECLARE (e) =
4222 {
4223 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4224 .format_args = "i4",
4225 };
4226 struct
4227 {
4228 u32 data;
4229 } *ed;
4230
4231 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4232
4233 ed->data = rv;
4234 /* *INDENT-ON* */
4235 }
Dave Wallace35830af2017-10-09 01:43:42 -04004236 break;
4237
4238 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05004239 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04004240 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004241 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
4242 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04004243 *buflen = sizeof (*flags);
Dave Wallace227867f2017-11-13 21:21:53 -05004244 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004245 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
Dave Wallace227867f2017-11-13 21:21:53 -05004246 "flags = 0x%08x, is_nonblocking = %u", getpid (),
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004247 session_index, *flags,
4248 VCL_SESS_ATTR_TEST (session->attr,
4249 VCL_SESS_ATTR_NONBLOCK));
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004250 if (VPPCOM_DEBUG > 0)
4251 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004252 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004253 ELOG_TYPE_DECLARE (e) =
4254 {
4255 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004256 .format_args = "i4i4",
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004257 };
4258 struct
4259 {
4260 u32 flags;
4261 u32 is_nonblk;
4262 } *ed;
4263
4264 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4265
4266 ed->flags = *flags;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004267 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004268 /* *INDENT-ON* */
4269 }
4270
Dave Wallace35830af2017-10-09 01:43:42 -04004271 }
4272 else
4273 rv = VPPCOM_EINVAL;
4274 break;
4275
4276 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05004277 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04004278 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004279 if (*flags & O_NONBLOCK)
4280 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
4281 else
4282 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
4283
Dave Wallace227867f2017-11-13 21:21:53 -05004284 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05004285 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
Dave Wallace227867f2017-11-13 21:21:53 -05004286 "flags = 0x%08x, is_nonblocking = %u",
Dave Wallace4878cbe2017-11-21 03:45:09 -05004287 getpid (), session_index, *flags,
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004288 VCL_SESS_ATTR_TEST (session->attr,
4289 VCL_SESS_ATTR_NONBLOCK));
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004290 if (VPPCOM_DEBUG > 0)
4291 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004292 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004293 ELOG_TYPE_DECLARE (e) =
4294 {
4295 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004296 .format_args = "i4i4",
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004297 };
4298 struct
4299 {
4300 u32 flags;
4301 u32 is_nonblk;
4302 } *ed;
4303
4304 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4305
4306 ed->flags = *flags;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004307 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004308 /* *INDENT-ON* */
4309 }
Dave Wallace35830af2017-10-09 01:43:42 -04004310 }
4311 else
4312 rv = VPPCOM_EINVAL;
4313 break;
4314
4315 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05004316 if (PREDICT_TRUE (buffer && buflen &&
4317 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04004318 {
Steven2199aab2017-10-15 20:18:47 -07004319 ep->is_ip4 = session->peer_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07004320 ep->port = session->peer_port;
Steven2199aab2017-10-15 20:18:47 -07004321 if (session->peer_addr.is_ip4)
4322 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4323 sizeof (ip4_address_t));
4324 else
4325 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4326 sizeof (ip6_address_t));
4327 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04004328 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05004329 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4330 "is_ip4 = %u, addr = %U, port %u", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04004331 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07004332 &session->peer_addr.ip46, ep->is_ip4,
4333 clib_net_to_host_u16 (ep->port));
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004334 if (VPPCOM_DEBUG > 0)
4335 {
4336 if (ep->is_ip4)
4337 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004338 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004339 ELOG_TYPE_DECLARE (e) =
4340 {
4341 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4342 .format_args = "i1i1i1i1i2",
4343 };
4344 CLIB_PACKED (struct {
4345 u8 addr[4]; //4
4346 u16 port; //2
4347 }) * ed;
4348
4349 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4350
4351 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4352 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4353 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4354 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4355 ed->port = clib_net_to_host_u16 (session->peer_port);
4356 /* *INDENT-ON* */
4357 }
4358 else
4359 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004360 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004361 ELOG_TYPE_DECLARE (e) =
4362 {
4363 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4364 .format_args = "i2",
4365 };
4366 CLIB_PACKED (struct {
4367 u16 port; //2
4368 }) * ed;
4369
4370 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4371
4372 ed->port = clib_net_to_host_u16 (session->peer_port);
4373 /* *INDENT-ON* */
4374 }
4375 }
Dave Wallace35830af2017-10-09 01:43:42 -04004376 }
4377 else
4378 rv = VPPCOM_EINVAL;
4379 break;
4380
4381 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05004382 if (PREDICT_TRUE (buffer && buflen &&
4383 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04004384 {
Steven2199aab2017-10-15 20:18:47 -07004385 ep->is_ip4 = session->lcl_addr.is_ip4;
Stevenac1f96d2017-10-24 16:03:58 -07004386 ep->port = session->lcl_port;
Steven2199aab2017-10-15 20:18:47 -07004387 if (session->lcl_addr.is_ip4)
4388 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4389 sizeof (ip4_address_t));
4390 else
4391 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4392 sizeof (ip6_address_t));
4393 *buflen = sizeof (*ep);
Dave Wallacefaf9d772017-10-26 16:12:04 -04004394 if (VPPCOM_DEBUG > 1)
Dave Wallace048b1d62018-01-03 22:24:41 -05004395 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4396 "is_ip4 = %u, addr = %U port %d", getpid (),
Dave Wallace774169b2017-11-01 20:07:40 -04004397 session_index, ep->is_ip4, format_ip46_address,
Stevenac1f96d2017-10-24 16:03:58 -07004398 &session->lcl_addr.ip46, ep->is_ip4,
4399 clib_net_to_host_u16 (ep->port));
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004400 if (VPPCOM_DEBUG > 0)
4401 {
4402 if (ep->is_ip4)
4403 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004404 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004405 ELOG_TYPE_DECLARE (e) =
4406 {
4407 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4408 .format_args = "i1i1i1i1i2",
4409 };
4410 CLIB_PACKED (struct {
4411 u8 addr[4]; //4
4412 u16 port; //2
4413 }) * ed;
4414
4415 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4416
4417 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4418 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4419 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4420 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4421 ed->port = clib_net_to_host_u16 (session->peer_port);
4422 /* *INDENT-ON* */
4423 }
4424 else
4425 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004426 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004427 ELOG_TYPE_DECLARE (e) =
4428 {
4429 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4430 .format_args = "i2",
4431 };
4432 CLIB_PACKED (struct {
4433 u16 port; //2
4434 }) * ed;
4435
4436 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4437
4438 ed->port = clib_net_to_host_u16 (session->peer_port);
4439 /* *INDENT-ON* */
4440 }
4441 }
Dave Wallace35830af2017-10-09 01:43:42 -04004442 }
4443 else
4444 rv = VPPCOM_EINVAL;
4445 break;
Stevenb5a11602017-10-11 09:59:30 -07004446
Dave Wallace048b1d62018-01-03 22:24:41 -05004447 case VPPCOM_ATTR_GET_LIBC_EPFD:
4448 rv = session->libc_epfd;
4449 if (VPPCOM_DEBUG > 2)
4450 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4451 getpid (), rv);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004452 if (VPPCOM_DEBUG > 0)
4453 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004454 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004455 ELOG_TYPE_DECLARE (e) =
4456 {
Florin Corasbb16d3f2018-01-29 08:55:25 -08004457 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4458 .format_args = "i4",
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004459 };
4460 CLIB_PACKED (struct {
Florin Corasbb16d3f2018-01-29 08:55:25 -08004461 i32 data;
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004462 }) * ed;
4463
4464 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
Florin Corasbb16d3f2018-01-29 08:55:25 -08004465 ed->data = session->libc_epfd;
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004466 /* *INDENT-ON* */
4467 }
4468
Dave Wallace048b1d62018-01-03 22:24:41 -05004469 break;
4470
4471 case VPPCOM_ATTR_SET_LIBC_EPFD:
4472 if (PREDICT_TRUE (buffer && buflen &&
4473 (*buflen == sizeof (session->libc_epfd))))
4474 {
4475 session->libc_epfd = *(int *) buffer;
4476 *buflen = sizeof (session->libc_epfd);
4477
4478 if (VPPCOM_DEBUG > 2)
4479 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4480 "buflen %d", getpid (), session->libc_epfd,
4481 *buflen);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004482 if (VPPCOM_DEBUG > 0)
4483 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004484 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004485 ELOG_TYPE_DECLARE (e) =
4486 {
4487 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4488 .format_args = "t1i4i4",
4489 .n_enum_strings = 2,
4490 .enum_strings = {"", "-",},
4491 };
4492 CLIB_PACKED (struct {
4493 u8 sign;
4494 u32 data[2];
4495 }) * ed;
4496
4497 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4498
4499 ed->sign = (session->libc_epfd < 0);
4500 ed->data[0] = abs(session->libc_epfd);
4501 ed->data[1] = *buflen;
4502 /* *INDENT-ON* */
4503 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004504 }
4505 else
4506 rv = VPPCOM_EINVAL;
4507 break;
4508
4509 case VPPCOM_ATTR_GET_PROTOCOL:
4510 if (buffer && buflen && (*buflen >= sizeof (int)))
4511 {
4512 *(int *) buffer = session->proto;
4513 *buflen = sizeof (int);
4514
4515 if (VPPCOM_DEBUG > 2)
4516 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4517 "buflen %d", getpid (), *(int *) buffer,
4518 *(int *) buffer ? "UDP" : "TCP", *buflen);
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004519 if (VPPCOM_DEBUG > 0)
4520 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004521 /* *INDENT-OFF* */
Keith Burns (alagalah)504a71f2018-01-17 15:16:32 -08004522 ELOG_TYPE_DECLARE (e) =
4523 {
4524 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4525 .format_args = "t1i4",
4526 .n_enum_strings = 2,
4527 .enum_strings = {"TCP", "UDP",},
4528 };
4529
4530 CLIB_PACKED (struct {
4531 u8 proto;
4532 u32 buflen;
4533 }) * ed;
4534
4535 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4536 ed->proto = session->proto;
4537 ed->buflen = *(int *) buffer;
4538 /* *INDENT-ON* */
4539 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004540 }
4541 else
4542 rv = VPPCOM_EINVAL;
4543 break;
4544
4545 case VPPCOM_ATTR_GET_LISTEN:
4546 if (buffer && buflen && (*buflen >= sizeof (int)))
4547 {
4548 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4549 VCL_SESS_ATTR_LISTEN);
4550 *buflen = sizeof (int);
4551
4552 if (VPPCOM_DEBUG > 2)
4553 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4554 "buflen %d", getpid (), *(int *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004555 if (VPPCOM_DEBUG > 0)
4556 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004557 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004558 ELOG_TYPE_DECLARE (e) =
4559 {
4560 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4561 .format_args = "i4i4",
4562 };
4563
4564 struct {
4565 u32 data[2];
4566 } * ed;
4567
4568 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4569 ed->data[0] = *(int *) buffer;
4570 ed->data[1] = *buflen;
4571 /* *INDENT-ON* */
4572 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004573 }
4574 else
4575 rv = VPPCOM_EINVAL;
4576 break;
4577
4578 case VPPCOM_ATTR_GET_ERROR:
4579 if (buffer && buflen && (*buflen >= sizeof (int)))
4580 {
4581 *(int *) buffer = 0;
4582 *buflen = sizeof (int);
4583
4584 if (VPPCOM_DEBUG > 2)
4585 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4586 "buflen %d, #VPP-TBD#", getpid (),
4587 *(int *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004588 if (VPPCOM_DEBUG > 0)
4589 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004590 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004591 ELOG_TYPE_DECLARE (e) =
4592 {
4593 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4594 .format_args = "i4i4",
4595 };
4596
4597 struct {
4598 u32 data[2];
4599 } * ed;
4600
4601 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4602 ed->data[0] = *(int *) buffer;
4603 ed->data[1] = *buflen;
4604 /* *INDENT-ON* */
4605 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004606 }
4607 else
4608 rv = VPPCOM_EINVAL;
4609 break;
4610
4611 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4612 if (buffer && buflen && (*buflen >= sizeof (u32)))
4613 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004614
4615 /* VPP-TBD */
4616 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004617 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05004618 vcm->cfg.tx_fifo_size);
4619 *buflen = sizeof (u32);
4620
4621 if (VPPCOM_DEBUG > 2)
4622 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4623 "buflen %d, #VPP-TBD#", getpid (),
4624 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004625 if (VPPCOM_DEBUG > 0)
4626 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004627 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004628 ELOG_TYPE_DECLARE (e) =
4629 {
4630 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4631 .format_args = "i4i4",
4632 };
4633
4634 struct {
4635 u32 data[2];
4636 } * ed;
4637
4638 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4639 ed->data[0] = *(size_t *) buffer;
4640 ed->data[1] = *buflen;
4641 /* *INDENT-ON* */
4642 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004643 }
4644 else
4645 rv = VPPCOM_EINVAL;
4646 break;
4647
4648 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4649 if (buffer && buflen && (*buflen == sizeof (u32)))
4650 {
4651 /* VPP-TBD */
4652 session->sndbuf_size = *(u32 *) buffer;
4653 if (VPPCOM_DEBUG > 2)
4654 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4655 "buflen %d, #VPP-TBD#", getpid (),
4656 session->sndbuf_size, session->sndbuf_size,
4657 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004658 if (VPPCOM_DEBUG > 0)
4659 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004660 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004661 ELOG_TYPE_DECLARE (e) =
4662 {
4663 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4664 .format_args = "i4i4",
4665 };
4666
4667 struct {
4668 u32 data[2];
4669 } * ed;
4670
4671 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4672 ed->data[0] = session->sndbuf_size;
4673 ed->data[1] = *buflen;
4674 /* *INDENT-ON* */
4675 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004676 }
4677 else
4678 rv = VPPCOM_EINVAL;
4679 break;
4680
4681 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4682 if (buffer && buflen && (*buflen >= sizeof (u32)))
4683 {
Dave Wallace048b1d62018-01-03 22:24:41 -05004684
4685 /* VPP-TBD */
4686 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08004687 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05004688 vcm->cfg.rx_fifo_size);
4689 *buflen = sizeof (u32);
4690
4691 if (VPPCOM_DEBUG > 2)
4692 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4693 "buflen %d, #VPP-TBD#", getpid (),
4694 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004695 if (VPPCOM_DEBUG > 0)
4696 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004697 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004698 ELOG_TYPE_DECLARE (e) =
4699 {
4700 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4701 .format_args = "i4i4",
4702 };
4703
4704 struct {
4705 u32 data[2];
4706 } * ed;
4707
4708 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4709 ed->data[0] = *(size_t *) buffer;
4710 ed->data[1] = *buflen;
4711 /* *INDENT-ON* */
4712 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004713 }
4714 else
4715 rv = VPPCOM_EINVAL;
4716 break;
4717
4718 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4719 if (buffer && buflen && (*buflen == sizeof (u32)))
4720 {
4721 /* VPP-TBD */
4722 session->rcvbuf_size = *(u32 *) buffer;
4723 if (VPPCOM_DEBUG > 2)
Dave Wallace7e2c31a2018-02-05 20:03:01 -05004724 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
Dave Wallace048b1d62018-01-03 22:24:41 -05004725 "buflen %d, #VPP-TBD#", getpid (),
4726 session->sndbuf_size, session->sndbuf_size,
4727 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004728 if (VPPCOM_DEBUG > 0)
4729 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004730 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004731 ELOG_TYPE_DECLARE (e) =
4732 {
Dave Wallace7e2c31a2018-02-05 20:03:01 -05004733 .format = "VPPCOM_ATTR_SET_RX_FIFO_LEN: 0x%x buflen=%d",
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004734 .format_args = "i4i4",
4735 };
4736
4737 struct {
4738 u32 data[2];
4739 } * ed;
4740
4741 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4742 ed->data[0] = session->sndbuf_size;
4743 ed->data[1] = *buflen;
4744 /* *INDENT-ON* */
4745 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004746 }
4747 else
4748 rv = VPPCOM_EINVAL;
4749 break;
4750
4751 case VPPCOM_ATTR_GET_REUSEADDR:
4752 if (buffer && buflen && (*buflen >= sizeof (int)))
4753 {
4754 /* VPP-TBD */
4755 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4756 VCL_SESS_ATTR_REUSEADDR);
4757 *buflen = sizeof (int);
4758
4759 if (VPPCOM_DEBUG > 2)
4760 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4761 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4762 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004763 if (VPPCOM_DEBUG > 0)
4764 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004765 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004766 ELOG_TYPE_DECLARE (e) =
4767 {
4768 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4769 .format_args = "i4i4",
4770 };
4771
4772 struct {
4773 u32 data[2];
4774 } * ed;
4775
4776 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4777 ed->data[0] = *(int *) buffer;
4778 ed->data[1] = *buflen;
4779 /* *INDENT-ON* */
4780 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004781 }
4782 else
4783 rv = VPPCOM_EINVAL;
4784 break;
4785
Stevenb5a11602017-10-11 09:59:30 -07004786 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05004787 if (buffer && buflen && (*buflen == sizeof (int)) &&
4788 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4789 {
4790 /* VPP-TBD */
4791 if (*(int *) buffer)
4792 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4793 else
4794 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4795
4796 if (VPPCOM_DEBUG > 2)
4797 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4798 "buflen %d, #VPP-TBD#", getpid (),
4799 VCL_SESS_ATTR_TEST (session->attr,
4800 VCL_SESS_ATTR_REUSEADDR),
4801 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004802 if (VPPCOM_DEBUG > 0)
4803 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004804 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004805 ELOG_TYPE_DECLARE (e) =
4806 {
4807 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4808 .format_args = "i4i4",
4809 };
4810
4811 struct {
4812 u32 data[2];
4813 } * ed;
4814
4815 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4816 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4817 VCL_SESS_ATTR_REUSEADDR);
4818 ed->data[1] = *buflen;
4819 /* *INDENT-ON* */
4820 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004821 }
4822 else
4823 rv = VPPCOM_EINVAL;
4824 break;
4825
4826 case VPPCOM_ATTR_GET_REUSEPORT:
4827 if (buffer && buflen && (*buflen >= sizeof (int)))
4828 {
4829 /* VPP-TBD */
4830 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4831 VCL_SESS_ATTR_REUSEPORT);
4832 *buflen = sizeof (int);
4833
4834 if (VPPCOM_DEBUG > 2)
4835 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4836 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4837 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004838 if (VPPCOM_DEBUG > 0)
4839 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004840 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004841 ELOG_TYPE_DECLARE (e) =
4842 {
4843 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4844 .format_args = "i4i4",
4845 };
4846
4847 struct {
4848 u32 data[2];
4849 } * ed;
4850
4851 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4852 ed->data[0] = *(int *) buffer;
4853 ed->data[1] = *buflen;
4854 /* *INDENT-ON* */
4855 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004856 }
4857 else
4858 rv = VPPCOM_EINVAL;
4859 break;
4860
4861 case VPPCOM_ATTR_SET_REUSEPORT:
4862 if (buffer && buflen && (*buflen == sizeof (int)) &&
4863 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4864 {
4865 /* VPP-TBD */
4866 if (*(int *) buffer)
4867 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4868 else
4869 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
4870
4871 if (VPPCOM_DEBUG > 2)
4872 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
4873 "buflen %d, #VPP-TBD#", getpid (),
4874 VCL_SESS_ATTR_TEST (session->attr,
4875 VCL_SESS_ATTR_REUSEPORT),
4876 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004877 if (VPPCOM_DEBUG > 0)
4878 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004879 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004880 ELOG_TYPE_DECLARE (e) =
4881 {
4882 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
4883 .format_args = "i4i4",
4884 };
4885
4886 struct {
4887 u32 data[2];
4888 } * ed;
4889
4890 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4891 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4892 VCL_SESS_ATTR_REUSEPORT);
4893 ed->data[1] = *buflen;
4894 /* *INDENT-ON* */
4895 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004896 }
4897 else
4898 rv = VPPCOM_EINVAL;
4899 break;
4900
4901 case VPPCOM_ATTR_GET_BROADCAST:
4902 if (buffer && buflen && (*buflen >= sizeof (int)))
4903 {
4904 /* VPP-TBD */
4905 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4906 VCL_SESS_ATTR_BROADCAST);
4907 *buflen = sizeof (int);
4908
4909 if (VPPCOM_DEBUG > 2)
4910 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
4911 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4912 *buflen);
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004913 if (VPPCOM_DEBUG > 0)
4914 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004915 /* *INDENT-OFF* */
Keith Burns (alagalah)21673552018-01-18 16:01:34 -08004916 ELOG_TYPE_DECLARE (e) =
4917 {
4918 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
4919 .format_args = "i4i4",
4920 };
4921
4922 struct {
4923 u32 data[2];
4924 } * ed;
4925
4926 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4927 ed->data[0] = *(int *) buffer;
4928 ed->data[1] = *buflen;
4929 /* *INDENT-ON* */
4930 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004931 }
4932 else
4933 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07004934 break;
4935
4936 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05004937 if (buffer && buflen && (*buflen == sizeof (int)))
4938 {
4939 /* VPP-TBD */
4940 if (*(int *) buffer)
4941 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
4942 else
4943 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
4944
4945 if (VPPCOM_DEBUG > 2)
4946 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
4947 "buflen %d, #VPP-TBD#", getpid (),
4948 VCL_SESS_ATTR_TEST (session->attr,
4949 VCL_SESS_ATTR_BROADCAST),
4950 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08004951 if (VPPCOM_DEBUG > 0)
4952 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004953 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08004954 ELOG_TYPE_DECLARE (e) =
4955 {
4956 .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
4957 .format_args = "i4i4",
4958 };
4959
4960 struct {
4961 u32 data[2];
4962 } * ed;
4963
4964 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4965 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4966 VCL_SESS_ATTR_BROADCAST);
4967 ed->data[1] = *buflen;
4968 /* *INDENT-ON* */
4969 }
Dave Wallace048b1d62018-01-03 22:24:41 -05004970 }
4971 else
4972 rv = VPPCOM_EINVAL;
4973 break;
4974
4975 case VPPCOM_ATTR_GET_V6ONLY:
4976 if (buffer && buflen && (*buflen >= sizeof (int)))
4977 {
4978 /* VPP-TBD */
4979 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4980 VCL_SESS_ATTR_V6ONLY);
4981 *buflen = sizeof (int);
4982
4983 if (VPPCOM_DEBUG > 2)
4984 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
4985 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4986 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08004987 if (VPPCOM_DEBUG > 0)
4988 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08004989 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08004990 ELOG_TYPE_DECLARE (e) =
4991 {
4992 .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
4993 .format_args = "i4i4",
4994 };
4995
4996 struct {
4997 u32 data[2];
4998 } * ed;
4999
5000 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5001 ed->data[0] = *(int *) buffer;
5002 ed->data[1] = *buflen;
5003 /* *INDENT-ON* */
5004 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005005 }
5006 else
5007 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07005008 break;
5009
5010 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05005011 if (buffer && buflen && (*buflen == sizeof (int)))
5012 {
5013 /* VPP-TBD */
5014 if (*(int *) buffer)
5015 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5016 else
5017 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5018
5019 if (VPPCOM_DEBUG > 2)
5020 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5021 "buflen %d, #VPP-TBD#", getpid (),
5022 VCL_SESS_ATTR_TEST (session->attr,
5023 VCL_SESS_ATTR_V6ONLY), *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005024 if (VPPCOM_DEBUG > 0)
5025 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005026 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005027 ELOG_TYPE_DECLARE (e) =
5028 {
5029 .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5030 .format_args = "i4i4",
5031 };
5032
5033 struct {
5034 u32 data[2];
5035 } * ed;
5036
5037 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5038 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5039 VCL_SESS_ATTR_V6ONLY);
5040 ed->data[1] = *buflen;
5041 /* *INDENT-ON* */
5042 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005043 }
5044 else
5045 rv = VPPCOM_EINVAL;
5046 break;
5047
5048 case VPPCOM_ATTR_GET_KEEPALIVE:
5049 if (buffer && buflen && (*buflen >= sizeof (int)))
5050 {
5051 /* VPP-TBD */
5052 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5053 VCL_SESS_ATTR_KEEPALIVE);
5054 *buflen = sizeof (int);
5055
5056 if (VPPCOM_DEBUG > 2)
5057 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5058 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5059 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005060 if (VPPCOM_DEBUG > 0)
5061 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005062 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005063 ELOG_TYPE_DECLARE (e) =
5064 {
5065 .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5066 .format_args = "i4i4",
5067 };
5068
5069 struct {
5070 u32 data[2];
5071 } * ed;
5072
5073 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5074 ed->data[0] = *(int *) buffer;
5075 ed->data[1] = *buflen;
5076 /* *INDENT-ON* */
5077 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005078 }
5079 else
5080 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07005081 break;
Stevenbccd3392017-10-12 20:42:21 -07005082
5083 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05005084 if (buffer && buflen && (*buflen == sizeof (int)))
5085 {
5086 /* VPP-TBD */
5087 if (*(int *) buffer)
5088 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5089 else
5090 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5091
5092 if (VPPCOM_DEBUG > 2)
5093 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5094 "buflen %d, #VPP-TBD#", getpid (),
5095 VCL_SESS_ATTR_TEST (session->attr,
5096 VCL_SESS_ATTR_KEEPALIVE),
5097 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005098 if (VPPCOM_DEBUG > 0)
5099 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005100 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005101 ELOG_TYPE_DECLARE (e) =
5102 {
5103 .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5104 .format_args = "i4i4",
5105 };
5106
5107 struct {
5108 u32 data[2];
5109 } * ed;
5110
5111 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5112 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5113 VCL_SESS_ATTR_KEEPALIVE);
5114 ed->data[1] = *buflen;
5115 /* *INDENT-ON* */
5116 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005117 }
5118 else
5119 rv = VPPCOM_EINVAL;
5120 break;
5121
5122 case VPPCOM_ATTR_GET_TCP_NODELAY:
5123 if (buffer && buflen && (*buflen >= sizeof (int)))
5124 {
5125 /* VPP-TBD */
5126 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5127 VCL_SESS_ATTR_TCP_NODELAY);
5128 *buflen = sizeof (int);
5129
5130 if (VPPCOM_DEBUG > 2)
5131 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5132 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5133 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005134 if (VPPCOM_DEBUG > 0)
5135 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005136 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005137 ELOG_TYPE_DECLARE (e) =
5138 {
5139 .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5140 .format_args = "i4i4",
5141 };
5142
5143 struct {
5144 u32 data[2];
5145 } * ed;
5146
5147 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5148 ed->data[0] = *(int *) buffer;
5149 ed->data[1] = *buflen;
5150 /* *INDENT-ON* */
5151 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005152 }
5153 else
5154 rv = VPPCOM_EINVAL;
5155 break;
5156
5157 case VPPCOM_ATTR_SET_TCP_NODELAY:
5158 if (buffer && buflen && (*buflen == sizeof (int)))
5159 {
5160 /* VPP-TBD */
5161 if (*(int *) buffer)
5162 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5163 else
5164 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5165
5166 if (VPPCOM_DEBUG > 2)
5167 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5168 "buflen %d, #VPP-TBD#", getpid (),
5169 VCL_SESS_ATTR_TEST (session->attr,
5170 VCL_SESS_ATTR_TCP_NODELAY),
5171 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005172 if (VPPCOM_DEBUG > 0)
5173 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005174 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005175 ELOG_TYPE_DECLARE (e) =
5176 {
5177 .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5178 .format_args = "i4i4",
5179 };
5180
5181 struct {
5182 u32 data[2];
5183 } * ed;
5184
5185 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5186 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5187 VCL_SESS_ATTR_TCP_NODELAY);
5188 ed->data[1] = *buflen;
5189 /* *INDENT-ON* */
5190 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005191 }
5192 else
5193 rv = VPPCOM_EINVAL;
5194 break;
5195
5196 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5197 if (buffer && buflen && (*buflen >= sizeof (int)))
5198 {
5199 /* VPP-TBD */
5200 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5201 VCL_SESS_ATTR_TCP_KEEPIDLE);
5202 *buflen = sizeof (int);
5203
5204 if (VPPCOM_DEBUG > 2)
5205 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5206 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5207 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005208 if (VPPCOM_DEBUG > 0)
5209 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005210 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005211 ELOG_TYPE_DECLARE (e) =
5212 {
5213 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5214 .format_args = "i4i4",
5215 };
5216
5217 struct {
5218 u32 data[2];
5219 } * ed;
5220
5221 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5222 ed->data[0] = *(int *) buffer;
5223 ed->data[1] = *buflen;
5224 /* *INDENT-ON* */
5225 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005226 }
5227 else
5228 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07005229 break;
5230
5231 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05005232 if (buffer && buflen && (*buflen == sizeof (int)))
5233 {
5234 /* VPP-TBD */
5235 if (*(int *) buffer)
5236 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5237 else
5238 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5239
5240 if (VPPCOM_DEBUG > 2)
5241 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5242 "buflen %d, #VPP-TBD#", getpid (),
5243 VCL_SESS_ATTR_TEST (session->attr,
5244 VCL_SESS_ATTR_TCP_KEEPIDLE),
5245 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005246 if (VPPCOM_DEBUG > 0)
5247 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005248 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005249 ELOG_TYPE_DECLARE (e) =
5250 {
5251 .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5252 .format_args = "i4i4",
5253 };
5254
5255 struct {
5256 u32 data[2];
5257 } * ed;
5258
5259 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5260 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5261 VCL_SESS_ATTR_TCP_KEEPIDLE);
5262 ed->data[1] = *buflen;
5263 /* *INDENT-ON* */
5264 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005265 }
5266 else
5267 rv = VPPCOM_EINVAL;
5268 break;
5269
5270 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5271 if (buffer && buflen && (*buflen >= sizeof (int)))
5272 {
5273 /* VPP-TBD */
5274 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5275 VCL_SESS_ATTR_TCP_KEEPINTVL);
5276 *buflen = sizeof (int);
5277
5278 if (VPPCOM_DEBUG > 2)
5279 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5280 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5281 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005282 if (VPPCOM_DEBUG > 0)
5283 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005284 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005285 ELOG_TYPE_DECLARE (e) =
5286 {
5287 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5288 .format_args = "i4i4",
5289 };
5290
5291 struct {
5292 u32 data[2];
5293 } * ed;
5294
5295 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5296 ed->data[0] = *(int *) buffer;
5297 ed->data[1] = *buflen;
5298 /* *INDENT-ON* */
5299 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005300 }
5301 else
5302 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07005303 break;
5304
5305 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05005306 if (buffer && buflen && (*buflen == sizeof (int)))
5307 {
5308 /* VPP-TBD */
5309 if (*(int *) buffer)
5310 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5311 else
5312 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5313
5314 if (VPPCOM_DEBUG > 2)
5315 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5316 "buflen %d, #VPP-TBD#", getpid (),
5317 VCL_SESS_ATTR_TEST (session->attr,
5318 VCL_SESS_ATTR_TCP_KEEPINTVL),
5319 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005320 if (VPPCOM_DEBUG > 0)
5321 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005322 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005323 ELOG_TYPE_DECLARE (e) =
5324 {
5325 .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5326 .format_args = "i4i4",
5327 };
5328
5329 struct {
5330 u32 data[2];
5331 } * ed;
5332
5333 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5334 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5335 VCL_SESS_ATTR_TCP_KEEPINTVL);
5336 ed->data[1] = *buflen;
5337 /* *INDENT-ON* */
5338 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005339 }
5340 else
5341 rv = VPPCOM_EINVAL;
5342 break;
5343
5344 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5345 if (buffer && buflen && (*buflen >= sizeof (u32)))
5346 {
5347 /* VPP-TBD */
5348 *(u32 *) buffer = session->user_mss;
5349 *buflen = sizeof (int);
5350
5351 if (VPPCOM_DEBUG > 2)
5352 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5353 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5354 *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005355 if (VPPCOM_DEBUG > 0)
5356 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005357 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005358 ELOG_TYPE_DECLARE (e) =
5359 {
5360 .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5361 .format_args = "i4i4",
5362 };
5363
5364 struct {
5365 u32 data[2];
5366 } * ed;
5367
5368 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5369 ed->data[0] = *(int *) buffer;
5370 ed->data[1] = *buflen;
5371 /* *INDENT-ON* */
5372 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005373 }
5374 else
5375 rv = VPPCOM_EINVAL;
5376 break;
5377
5378 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5379 if (buffer && buflen && (*buflen == sizeof (u32)))
5380 {
5381 /* VPP-TBD */
5382 session->user_mss = *(u32 *) buffer;
5383
5384 if (VPPCOM_DEBUG > 2)
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005385 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
Dave Wallace048b1d62018-01-03 22:24:41 -05005386 "buflen %d, #VPP-TBD#", getpid (),
5387 session->user_mss, *buflen);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005388 if (VPPCOM_DEBUG > 0)
5389 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005390 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005391 ELOG_TYPE_DECLARE (e) =
5392 {
5393 .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5394 .format_args = "i4i4",
5395 };
5396
5397 struct {
5398 u32 data[2];
5399 } * ed;
5400
5401 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5402 ed->data[0] = session->user_mss;
5403 ed->data[1] = *buflen;
5404 /* *INDENT-ON* */
5405 }
Dave Wallace048b1d62018-01-03 22:24:41 -05005406 }
5407 else
5408 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07005409 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04005410
5411 default:
5412 rv = VPPCOM_EINVAL;
5413 break;
Dave Wallace35830af2017-10-09 01:43:42 -04005414 }
5415
5416done:
5417 clib_spinlock_unlock (&vcm->sessions_lockp);
5418 return rv;
5419}
5420
Stevenac1f96d2017-10-24 16:03:58 -07005421int
5422vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5423 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5424{
Stevenac1f96d2017-10-24 16:03:58 -07005425 int rv = VPPCOM_OK;
5426 session_t *session = 0;
5427
5428 if (ep)
5429 {
5430 clib_spinlock_lock (&vcm->sessions_lockp);
5431 rv = vppcom_session_at_index (session_index, &session);
5432 if (PREDICT_FALSE (rv))
5433 {
5434 clib_spinlock_unlock (&vcm->sessions_lockp);
5435 if (VPPCOM_DEBUG > 0)
Dave Wallace048b1d62018-01-03 22:24:41 -05005436 clib_warning ("VCL<%d>: invalid session, "
5437 "sid (%u) has been closed!",
Dave Wallace2e005bb2017-11-07 01:21:39 -05005438 getpid (), session_index);
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005439 if (VPPCOM_DEBUG > 0)
5440 {
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08005441 /* *INDENT-OFF* */
Keith Burns (alagalah)4e578062018-01-19 14:26:35 -08005442 ELOG_TYPE_DECLARE (e) =
5443 {
5444 .format = "invalid session: %d closed",
5445 .format_args = "i4",
5446 };
5447
5448 struct {
5449 u32 data;
5450 } * ed;
5451
5452 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5453 ed->data = session_index;
5454 /* *INDENT-ON* */
5455 }
Dave Wallacefaf9d772017-10-26 16:12:04 -04005456 rv = VPPCOM_EBADFD;
5457 clib_spinlock_unlock (&vcm->sessions_lockp);
5458 goto done;
Stevenac1f96d2017-10-24 16:03:58 -07005459 }
Stevenac1f96d2017-10-24 16:03:58 -07005460 ep->is_ip4 = session->peer_addr.is_ip4;
5461 ep->port = session->peer_port;
5462 if (session->peer_addr.is_ip4)
5463 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5464 sizeof (ip4_address_t));
5465 else
5466 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5467 sizeof (ip6_address_t));
5468 clib_spinlock_unlock (&vcm->sessions_lockp);
Stevenac1f96d2017-10-24 16:03:58 -07005469 }
Steven58f464e2017-10-25 12:33:12 -07005470
5471 if (flags == 0)
Stevenac1f96d2017-10-24 16:03:58 -07005472 rv = vppcom_session_read (session_index, buffer, buflen);
5473 else if (flags & MSG_PEEK)
Steven58f464e2017-10-25 12:33:12 -07005474 rv = vppcom_session_peek (session_index, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07005475 else
5476 {
Dave Wallace048b1d62018-01-03 22:24:41 -05005477 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5478 getpid (), flags);
Stevenac1f96d2017-10-24 16:03:58 -07005479 rv = VPPCOM_EAFNOSUPPORT;
5480 }
5481
Dave Wallacefaf9d772017-10-26 16:12:04 -04005482done:
Stevenac1f96d2017-10-24 16:03:58 -07005483 return rv;
5484}
5485
5486int
5487vppcom_session_sendto (uint32_t session_index, void *buffer,
5488 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5489{
Dave Wallace617dffa2017-10-26 14:47:06 -04005490 if (!buffer)
5491 return VPPCOM_EINVAL;
5492
5493 if (ep)
5494 {
5495 // TBD
5496 return VPPCOM_EINVAL;
5497 }
5498
5499 if (flags)
5500 {
5501 // TBD check the flags and do the right thing
5502 if (VPPCOM_DEBUG > 2)
Dave Wallace048b1d62018-01-03 22:24:41 -05005503 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5504 "not implemented yet.", getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04005505 }
5506
5507 return (vppcom_session_write (session_index, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07005508}
5509
Dave Wallace048b1d62018-01-03 22:24:41 -05005510int
5511vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5512{
5513 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5514 u32 i, keep_trying = 1;
5515 int rv, num_ev = 0;
5516
5517 if (VPPCOM_DEBUG > 3)
5518 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5519 getpid (), vp, n_sids, wait_for_time);
5520
5521 if (!vp)
5522 return VPPCOM_EFAULT;
5523
5524 do
5525 {
5526 session_t *session;
5527
5528 for (i = 0; i < n_sids; i++)
5529 {
5530 ASSERT (vp[i].revents);
5531
5532 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5533 clib_spinlock_unlock (&vcm->sessions_lockp);
5534
5535 if (*vp[i].revents)
5536 *vp[i].revents = 0;
5537
5538 if (POLLIN & vp[i].events)
5539 {
5540 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5541 rv = vppcom_session_read_ready (session, vp[i].sid);
5542 clib_spinlock_unlock (&vcm->sessions_lockp);
5543 if (rv > 0)
5544 {
5545 *vp[i].revents |= POLLIN;
5546 num_ev++;
5547 }
5548 else if (rv < 0)
5549 {
5550 switch (rv)
5551 {
5552 case VPPCOM_ECONNRESET:
5553 *vp[i].revents = POLLHUP;
5554 break;
5555
5556 default:
5557 *vp[i].revents = POLLERR;
5558 break;
5559 }
5560 num_ev++;
5561 }
5562 }
5563
5564 if (POLLOUT & vp[i].events)
5565 {
5566 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5567 rv = vppcom_session_write_ready (session, vp[i].sid);
5568 clib_spinlock_unlock (&vcm->sessions_lockp);
5569 if (rv > 0)
5570 {
5571 *vp[i].revents |= POLLOUT;
5572 num_ev++;
5573 }
5574 else if (rv < 0)
5575 {
5576 switch (rv)
5577 {
5578 case VPPCOM_ECONNRESET:
5579 *vp[i].revents = POLLHUP;
5580 break;
5581
5582 default:
5583 *vp[i].revents = POLLERR;
5584 break;
5585 }
5586 num_ev++;
5587 }
5588 }
5589
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08005590 if (0) // Note "done:" label used by VCL_LOCK_AND_GET_SESSION()
Dave Wallace048b1d62018-01-03 22:24:41 -05005591 {
5592 done:
5593 *vp[i].revents = POLLNVAL;
5594 num_ev++;
5595 }
5596 }
5597 if (wait_for_time != -1)
5598 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5599 }
5600 while ((num_ev == 0) && keep_trying);
5601
5602 if (VPPCOM_DEBUG > 3)
5603 {
5604 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5605 for (i = 0; i < n_sids; i++)
5606 {
5607 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5608 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5609 vp[i].events, *vp[i].revents);
5610 }
5611 }
5612 return num_ev;
5613}
5614
Dave Wallacee22aa742017-10-20 12:30:38 -04005615/*
5616 * fd.io coding-style-patch-verification: ON
5617 *
5618 * Local Variables:
5619 * eval: (c-set-style "gnu")
5620 * End:
5621 */